1 diff -urN linux.old/fs/Kconfig linux.dev/fs/Kconfig
2 --- linux.old/fs/Kconfig 2006-11-29 22:57:37.000000000 +0100
3 +++ linux.dev/fs/Kconfig 2006-12-14 04:21:47.000000000 +0100
5 To compile the EFS file system support as a module, choose M here: the
6 module will be called efs.
8 +source "fs/yaffs2/Kconfig"
11 tristate "Journalling Flash File System (JFFS) support"
12 depends on MTD && BLOCK
13 diff -urN linux.old/fs/Makefile linux.dev/fs/Makefile
14 --- linux.old/fs/Makefile 2006-11-29 22:57:37.000000000 +0100
15 +++ linux.dev/fs/Makefile 2006-12-14 04:21:47.000000000 +0100
17 obj-$(CONFIG_DEBUG_FS) += debugfs/
18 obj-$(CONFIG_OCFS2_FS) += ocfs2/
19 obj-$(CONFIG_GFS2_FS) += gfs2/
20 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
21 diff -urN linux.old/fs/yaffs2/devextras.h linux.dev/fs/yaffs2/devextras.h
22 --- linux.old/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
23 +++ linux.dev/fs/yaffs2/devextras.h 2006-12-14 04:21:47.000000000 +0100
26 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
29 + * Copyright (C) 2002 Aleph One Ltd.
30 + * for Toby Churchill Ltd and Brightstar Engineering
32 + * Created by Charles Manning <charles@aleph1.co.uk>
34 + * This program is free software; you can redistribute it and/or modify
35 + * it under the terms of the GNU Lesser General Public License version 2.1 as
36 + * published by the Free Software Foundation.
38 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
40 + * This file is just holds extra declarations used during development.
41 + * Most of these are from kernel includes placed here so we can use them in
44 + * $Id: devextras.h,v 1.2 2005/08/11 02:37:49 marty Exp $
52 +#define __inline__ __inline
56 +#if !(defined __KERNEL__) || (defined WIN32)
58 +/* User space defines */
60 +typedef unsigned char __u8;
61 +typedef unsigned short __u16;
62 +typedef unsigned __u32;
65 + * Simple doubly linked list implementation.
67 + * Some of the internal functions ("__xxx") are useful when
68 + * manipulating whole lists rather than single entries, as
69 + * sometimes we already know the next/prev entries and we can
70 + * generate better code by using them directly rather than
71 + * using the generic single-entry routines.
74 +#define prefetch(x) 1
77 + struct list_head *next, *prev;
80 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
82 +#define LIST_HEAD(name) \
83 + struct list_head name = LIST_HEAD_INIT(name)
85 +#define INIT_LIST_HEAD(ptr) do { \
86 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
90 + * Insert a new entry between two known consecutive entries.
92 + * This is only for internal list manipulation where we know
93 + * the prev/next entries already!
95 +static __inline__ void __list_add(struct list_head *new,
96 + struct list_head *prev,
97 + struct list_head *next)
106 + * list_add - add a new entry
107 + * @new: new entry to be added
108 + * @head: list head to add it after
110 + * Insert a new entry after the specified head.
111 + * This is good for implementing stacks.
113 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
115 + __list_add(new, head, head->next);
119 + * list_add_tail - add a new entry
120 + * @new: new entry to be added
121 + * @head: list head to add it before
123 + * Insert a new entry before the specified head.
124 + * This is useful for implementing queues.
126 +static __inline__ void list_add_tail(struct list_head *new,
127 + struct list_head *head)
129 + __list_add(new, head->prev, head);
133 + * Delete a list entry by making the prev/next entries
134 + * point to each other.
136 + * This is only for internal list manipulation where we know
137 + * the prev/next entries already!
139 +static __inline__ void __list_del(struct list_head *prev,
140 + struct list_head *next)
147 + * list_del - deletes entry from list.
148 + * @entry: the element to delete from the list.
149 + * Note: list_empty on entry does not return true after this, the entry is
150 + * in an undefined state.
152 +static __inline__ void list_del(struct list_head *entry)
154 + __list_del(entry->prev, entry->next);
158 + * list_del_init - deletes entry from list and reinitialize it.
159 + * @entry: the element to delete from the list.
161 +static __inline__ void list_del_init(struct list_head *entry)
163 + __list_del(entry->prev, entry->next);
164 + INIT_LIST_HEAD(entry);
168 + * list_empty - tests whether a list is empty
169 + * @head: the list to test.
171 +static __inline__ int list_empty(struct list_head *head)
173 + return head->next == head;
177 + * list_splice - join two lists
178 + * @list: the new list to add.
179 + * @head: the place to add it in the first list.
181 +static __inline__ void list_splice(struct list_head *list,
182 + struct list_head *head)
184 + struct list_head *first = list->next;
186 + if (first != list) {
187 + struct list_head *last = list->prev;
188 + struct list_head *at = head->next;
190 + first->prev = head;
191 + head->next = first;
199 + * list_entry - get the struct for this entry
200 + * @ptr: the &struct list_head pointer.
201 + * @type: the type of the struct this is embedded in.
202 + * @member: the name of the list_struct within the struct.
204 +#define list_entry(ptr, type, member) \
205 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
208 + * list_for_each - iterate over a list
209 + * @pos: the &struct list_head to use as a loop counter.
210 + * @head: the head for your list.
212 +#define list_for_each(pos, head) \
213 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
214 + pos = pos->next, prefetch(pos->next))
217 + * list_for_each_safe - iterate over a list safe against removal
219 + * @pos: the &struct list_head to use as a loop counter.
220 + * @n: another &struct list_head to use as temporary storage
221 + * @head: the head for your list.
223 +#define list_for_each_safe(pos, n, head) \
224 + for (pos = (head)->next, n = pos->next; pos != (head); \
225 + pos = n, n = pos->next)
230 +#define DT_UNKNOWN 0
241 +#include <sys/stat.h>
245 + * Attribute flags. These should be or-ed together to figure out what
246 + * has been changed!
252 +#define ATTR_ATIME 16
253 +#define ATTR_MTIME 32
254 +#define ATTR_CTIME 64
255 +#define ATTR_ATIME_SET 128
256 +#define ATTR_MTIME_SET 256
257 +#define ATTR_FORCE 512 /* Not a change, but a change it */
258 +#define ATTR_ATTR_FLAG 1024
261 + unsigned int ia_valid;
269 + unsigned int ia_attr_flags;
277 +#include <linux/types.h>
278 +#include <linux/list.h>
279 +#include <linux/fs.h>
280 +#include <linux/stat.h>
290 diff -urN linux.old/fs/yaffs2/Kconfig linux.dev/fs/yaffs2/Kconfig
291 --- linux.old/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
292 +++ linux.dev/fs/yaffs2/Kconfig 2006-12-14 04:21:47.000000000 +0100
295 +# YAFFS file system configurations
299 + tristate "YAFFS2 file system support"
302 + select YAFFS_YAFFS1
303 + select YAFFS_YAFFS2
305 + YAFFS2, or Yet Another Flash Filing System, is a filing system
306 + optimised for NAND Flash chips.
308 + To compile the YAFFS2 file system support as a module, choose M here:
309 + the module will be called yaffs2.
313 + Further information on YAFFS2 is available at
314 + <http://www.aleph1.co.uk/yaffs/>.
317 + bool "512 byte / page devices"
318 + depends on YAFFS_FS
321 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
325 +config YAFFS_DOES_ECC
326 + bool "Lets Yaffs do its own ECC"
327 + depends on YAFFS_FS && YAFFS_YAFFS1
330 + This enables Yaffs to use its own ECC functions instead of using
331 + the ones from the generic MTD-NAND driver.
335 +config YAFFS_ECC_WRONG_ORDER
336 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
337 + depends on YAFFS_FS && YAFFS_DOES_ECC
340 + This makes yaffs_ecc.c use the same ecc byte order as
341 + Steven Hill's nand_ecc.c. If not set, then you get the
342 + same ecc byte order as SmartMedia.
347 + bool "2048 byte (or larger) / page devices"
348 + depends on YAFFS_FS
351 + Enable YAFFS2 support -- yaffs for >= 2048 byte / page larger devices
355 +config YAFFS_AUTO_YAFFS2
356 + bool "Autoselect yaffs2 format"
357 + depends on YAFFS_YAFFS2
360 + Without this, you need to explicitely use yaffs2 as the file
361 + system type. With this, you can say "yaffs" and yaffs or yaffs2
362 + will be used depending on the device page size.
366 +config YAFFS_DISABLE_LAZY_LOAD
367 + bool "Disable lazy loading"
368 + depends on YAFFS_YAFFS2
371 + "Lazy loading" defers loading file details until they are
372 + required. This saves mount time, but makes the first look-up
375 + Lazy loading will only happen if enabled by this option being 'n'
376 + and if the appropriate tags are available, else yaffs2 will
377 + automatically fall back to immediate loading and do the right
380 + Lazy laoding will be required by checkpointing.
382 + Setting this to 'y' will disable lazy loading.
386 +config YAFFS_DISABLE_WIDE_TNODES
387 + bool "Turn off wide tnodes"
388 + depends on YAFFS_FS
391 + Wide tnodes are only used for large NAND arrays (>=32MB for
392 + 512-byte page devices and >=128MB for 2k page devices). They use
393 + slightly more RAM but are faster since they eliminate chunk group
396 + Setting this to 'y' will force tnode width to 16 bits and make
397 + large arrays slower.
401 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
402 + bool "Force chunk erase check"
403 + depends on YAFFS_FS
406 + Normally YAFFS only checks chunks before writing until an erased
407 + chunk is found. This helps to detect any partially written chunks
408 + that might have happened due to power loss.
410 + Enabling this forces on the test that chunks are erased in flash
411 + before writing to them. This takes more time but is potentially a
414 + Suggest setting Y during development and ironing out driver issues
415 + etc. Suggest setting to N if you want faster writing.
419 +config YAFFS_SHORT_NAMES_IN_RAM
420 + bool "Cache short names in RAM"
421 + depends on YAFFS_FS
424 + If this config is set, then short names are stored with the
425 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
426 + but makes look-ups faster.
429 diff -urN linux.old/fs/yaffs2/Makefile linux.dev/fs/yaffs2/Makefile
430 --- linux.old/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
431 +++ linux.dev/fs/yaffs2/Makefile 2006-12-14 04:21:47.000000000 +0100
434 +# Makefile for the linux YAFFS filesystem routines.
437 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
439 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
440 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
441 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
442 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
443 diff -urN linux.old/fs/yaffs2/moduleconfig.h linux.dev/fs/yaffs2/moduleconfig.h
444 --- linux.old/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
445 +++ linux.dev/fs/yaffs2/moduleconfig.h 2006-12-14 04:21:47.000000000 +0100
447 +#ifndef __YAFFS_CONFIG_H__
448 +#define __YAFFS_CONFIG_H__
450 +#ifdef YAFFS_OUT_OF_TREE
452 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
453 +#define CONFIG_YAFFS_FS
454 +#define CONFIG_YAFFS_YAFFS1
455 +#define CONFIG_YAFFS_YAFFS2
457 +/* These options are independent of each other. Select those that matter. */
459 +/* Default: Not selected */
460 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
461 +//#define CONFIG_YAFFS_DOES_ECC
463 +/* Default: Not selected */
464 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
465 +/* CONFIG_YAFFS_DOES_ECC is set */
466 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
468 +/* Default: Selected */
469 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
470 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
472 +/* Default: Selected */
473 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
474 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
476 +#endif /* YAFFS_OUT_OF_TREE */
478 +#endif /* __YAFFS_CONFIG_H__ */
479 diff -urN linux.old/fs/yaffs2/yaffs_checkptrw.c linux.dev/fs/yaffs2/yaffs_checkptrw.c
480 --- linux.old/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
481 +++ linux.dev/fs/yaffs2/yaffs_checkptrw.c 2006-12-14 04:21:47.000000000 +0100
483 +/* YAFFS: Yet another FFS. A NAND-flash specific file system.
485 + * Copyright (C) 2002 Aleph One Ltd.
486 + * for Toby Churchill Ltd and Brightstar Engineering
488 + * Created by Charles Manning <charles@aleph1.co.uk>
490 + * This program is free software; you can redistribute it and/or modify
491 + * it under the terms of the GNU General Public License version 2 as
492 + * published by the Free Software Foundation.
496 +const char *yaffs_checkptrw_c_version =
497 + "$Id: yaffs_checkptrw.c,v 1.11 2006/11/11 23:27:04 charles Exp $";
500 +#include "yaffs_checkptrw.h"
503 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
506 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
508 + T(YAFFS_TRACE_CHECKPOINT,
509 + (TSTR("checkpt blocks available = %d" TENDSTR),
513 + return (blocksAvailable <= 0) ? 0 : 1;
518 +static int yaffs_CheckpointErase(yaffs_Device *dev)
524 + if(!dev->eraseBlockInNAND)
526 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
527 + dev->internalStartBlock,dev->internalEndBlock));
529 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
530 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
531 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
532 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
533 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
534 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
535 + dev->nErasedBlocks++;
536 + dev->nFreeChunks += dev->nChunksPerBlock;
539 + dev->markNANDBlockBad(dev,i);
540 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
545 + dev->blocksInCheckpoint = 0;
551 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
554 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
555 + T(YAFFS_TRACE_CHECKPOINT,
556 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
557 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
559 + if(dev->checkpointNextBlock >= 0 &&
560 + dev->checkpointNextBlock <= dev->internalEndBlock &&
561 + blocksAvailable > 0){
563 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
564 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
565 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
566 + dev->checkpointNextBlock = i + 1;
567 + dev->checkpointCurrentBlock = i;
568 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
573 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
575 + dev->checkpointNextBlock = -1;
576 + dev->checkpointCurrentBlock = -1;
579 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
582 + yaffs_ExtendedTags tags;
584 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
585 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
587 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
588 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
589 + int chunk = i * dev->nChunksPerBlock;
590 + int realignedChunk = chunk - dev->chunkOffset;
592 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
593 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
594 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
596 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
597 + /* Right kind of block */
598 + dev->checkpointNextBlock = tags.objectId;
599 + dev->checkpointCurrentBlock = i;
600 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
601 + dev->blocksInCheckpoint++;
602 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
607 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
609 + dev->checkpointNextBlock = -1;
610 + dev->checkpointCurrentBlock = -1;
614 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
617 + /* Got the functions we need? */
618 + if (!dev->writeChunkWithTagsToNAND ||
619 + !dev->readChunkWithTagsFromNAND ||
620 + !dev->eraseBlockInNAND ||
621 + !dev->markNANDBlockBad)
624 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
627 + if(!dev->checkpointBuffer)
628 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
629 + if(!dev->checkpointBuffer)
633 + dev->checkpointPageSequence = 0;
635 + dev->checkpointOpenForWrite = forWriting;
637 + dev->checkpointByteCount = 0;
638 + dev->checkpointCurrentBlock = -1;
639 + dev->checkpointCurrentChunk = -1;
640 + dev->checkpointNextBlock = dev->internalStartBlock;
642 + /* Erase all the blocks in the checkpoint area */
644 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
645 + dev->checkpointByteOffset = 0;
646 + return yaffs_CheckpointErase(dev);
651 + /* Set to a value that will kick off a read */
652 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
653 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
654 + * going to be way more than we need */
655 + dev->blocksInCheckpoint = 0;
656 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
657 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
658 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
659 + dev->checkpointBlockList[i] = -1;
665 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
669 + int realignedChunk;
671 + yaffs_ExtendedTags tags;
673 + if(dev->checkpointCurrentBlock < 0){
674 + yaffs_CheckpointFindNextErasedBlock(dev);
675 + dev->checkpointCurrentChunk = 0;
678 + if(dev->checkpointCurrentBlock < 0)
681 + tags.chunkDeleted = 0;
682 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
683 + tags.chunkId = dev->checkpointPageSequence + 1;
684 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
685 + tags.byteCount = dev->nDataBytesPerChunk;
686 + if(dev->checkpointCurrentChunk == 0){
687 + /* First chunk we write for the block? Set block state to
689 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
690 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
691 + dev->blocksInCheckpoint++;
694 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
697 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
698 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
700 + realignedChunk = chunk - dev->chunkOffset;
702 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
703 + dev->checkpointByteOffset = 0;
704 + dev->checkpointPageSequence++;
705 + dev->checkpointCurrentChunk++;
706 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
707 + dev->checkpointCurrentChunk = 0;
708 + dev->checkpointCurrentBlock = -1;
710 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
716 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
722 + __u8 * dataBytes = (__u8 *)data;
726 + if(!dev->checkpointBuffer)
729 + while(i < nBytes && ok) {
733 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
734 + dev->checkpointByteOffset++;
737 + dev->checkpointByteCount++;
740 + if(dev->checkpointByteOffset < 0 ||
741 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
742 + ok = yaffs_CheckpointFlushBuffer(dev);
749 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
753 + yaffs_ExtendedTags tags;
757 + int realignedChunk;
759 + __u8 *dataBytes = (__u8 *)data;
761 + if(!dev->checkpointBuffer)
764 + while(i < nBytes && ok) {
767 + if(dev->checkpointByteOffset < 0 ||
768 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
770 + if(dev->checkpointCurrentBlock < 0){
771 + yaffs_CheckpointFindNextCheckpointBlock(dev);
772 + dev->checkpointCurrentChunk = 0;
775 + if(dev->checkpointCurrentBlock < 0)
779 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
780 + dev->checkpointCurrentChunk;
782 + realignedChunk = chunk - dev->chunkOffset;
784 + /* read in the next chunk */
785 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
786 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
787 + dev->checkpointBuffer,
790 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
791 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
794 + dev->checkpointByteOffset = 0;
795 + dev->checkpointPageSequence++;
796 + dev->checkpointCurrentChunk++;
798 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
799 + dev->checkpointCurrentBlock = -1;
804 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
805 + dev->checkpointByteOffset++;
808 + dev->checkpointByteCount++;
815 +int yaffs_CheckpointClose(yaffs_Device *dev)
818 + if(dev->checkpointOpenForWrite){
819 + if(dev->checkpointByteOffset != 0)
820 + yaffs_CheckpointFlushBuffer(dev);
823 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
824 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
825 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
826 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
828 + // Todo this looks odd...
831 + YFREE(dev->checkpointBlockList);
832 + dev->checkpointBlockList = NULL;
835 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
836 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
839 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
840 + dev->checkpointByteCount));
842 + if(dev->checkpointBuffer){
843 + /* free the buffer */
844 + YFREE(dev->checkpointBuffer);
845 + dev->checkpointBuffer = NULL;
853 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
855 + /* Erase the first checksum block */
857 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
859 + if(!yaffs_CheckpointSpaceOk(dev))
862 + return yaffs_CheckpointErase(dev);
867 diff -urN linux.old/fs/yaffs2/yaffs_checkptrw.h linux.dev/fs/yaffs2/yaffs_checkptrw.h
868 --- linux.old/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
869 +++ linux.dev/fs/yaffs2/yaffs_checkptrw.h 2006-12-14 04:21:47.000000000 +0100
871 +#ifndef __YAFFS_CHECKPTRW_H__
872 +#define __YAFFS_CHECKPTRW_H__
874 +#include "yaffs_guts.h"
876 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
878 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
880 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
882 +int yaffs_CheckpointClose(yaffs_Device *dev);
884 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
889 diff -urN linux.old/fs/yaffs2/yaffs_ecc.c linux.dev/fs/yaffs2/yaffs_ecc.c
890 --- linux.old/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
891 +++ linux.dev/fs/yaffs2/yaffs_ecc.c 2006-12-14 04:21:47.000000000 +0100
894 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
896 + * yaffs_ecc.c: ECC generation/correction algorithms.
898 + * Copyright (C) 2002 Aleph One Ltd.
900 + * Created by Charles Manning <charles@aleph1.co.uk>
903 + * This program is free software; you can redistribute it and/or
904 + * modify it under the terms of the GNU Lesser General Public License
905 + * version 2.1 as published by the Free Software Foundation.
909 + * This code implements the ECC algorithm used in SmartMedia.
911 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
912 + * The two unused bit are set to 1.
913 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
914 + * blocks are used on a 512-byte NAND page.
918 +/* Table generated by gen-ecc.c
919 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
920 + * for each byte of data. These are instead provided in a table in bits7..2.
921 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
922 + * this bytes influence on the line parity.
925 +const char *yaffs_ecc_c_version =
926 + "$Id: yaffs_ecc.c,v 1.7 2006/09/14 22:02:46 charles Exp $";
928 +#include "yportenv.h"
930 +#include "yaffs_ecc.h"
932 +static const unsigned char column_parity_table[] = {
933 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
934 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
935 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
936 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
937 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
938 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
939 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
940 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
941 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
942 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
943 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
944 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
945 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
946 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
947 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
948 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
949 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
950 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
951 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
952 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
953 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
954 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
955 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
956 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
957 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
958 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
959 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
960 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
961 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
962 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
963 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
964 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
967 +/* Count the bits in an unsigned char or a U32 */
969 +static int yaffs_CountBits(unsigned char x)
980 +static int yaffs_CountBits32(unsigned x)
991 +/* Calculate the ECC for a 256-byte block of data */
992 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
996 + unsigned char col_parity = 0;
997 + unsigned char line_parity = 0;
998 + unsigned char line_parity_prime = 0;
1002 + for (i = 0; i < 256; i++) {
1003 + b = column_parity_table[*data++];
1006 + if (b & 0x01) // odd number of bits in the byte
1009 + line_parity_prime ^= ~i;
1014 + ecc[2] = (~col_parity) | 0x03;
1017 + if (line_parity & 0x80)
1019 + if (line_parity_prime & 0x80)
1021 + if (line_parity & 0x40)
1023 + if (line_parity_prime & 0x40)
1025 + if (line_parity & 0x20)
1027 + if (line_parity_prime & 0x20)
1029 + if (line_parity & 0x10)
1031 + if (line_parity_prime & 0x10)
1036 + if (line_parity & 0x08)
1038 + if (line_parity_prime & 0x08)
1040 + if (line_parity & 0x04)
1042 + if (line_parity_prime & 0x04)
1044 + if (line_parity & 0x02)
1046 + if (line_parity_prime & 0x02)
1048 + if (line_parity & 0x01)
1050 + if (line_parity_prime & 0x01)
1054 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1055 + // Swap the bytes into the wrong order
1063 +/* Correct the ECC on a 256 byte block of data */
1065 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1066 + const unsigned char *test_ecc)
1068 + unsigned char d0, d1, d2; /* deltas */
1070 + d0 = read_ecc[0] ^ test_ecc[0];
1071 + d1 = read_ecc[1] ^ test_ecc[1];
1072 + d2 = read_ecc[2] ^ test_ecc[2];
1074 + if ((d0 | d1 | d2) == 0)
1075 + return 0; /* no error */
1077 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1078 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1079 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1080 + /* Single bit (recoverable) error in data */
1085 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1086 + // swap the bytes to correct for the wrong order
1120 + data[byte] ^= (1 << bit);
1122 + return 1; /* Corrected the error */
1125 + if ((yaffs_CountBits(d0) +
1126 + yaffs_CountBits(d1) +
1127 + yaffs_CountBits(d2)) == 1) {
1128 + /* Reccoverable error in ecc */
1130 + read_ecc[0] = test_ecc[0];
1131 + read_ecc[1] = test_ecc[1];
1132 + read_ecc[2] = test_ecc[2];
1134 + return 1; /* Corrected the error */
1137 + /* Unrecoverable error */
1145 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1147 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1148 + yaffs_ECCOther * eccOther)
1152 + unsigned char col_parity = 0;
1153 + unsigned line_parity = 0;
1154 + unsigned line_parity_prime = 0;
1157 + for (i = 0; i < nBytes; i++) {
1158 + b = column_parity_table[*data++];
1162 + /* odd number of bits in the byte */
1164 + line_parity_prime ^= ~i;
1169 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1170 + eccOther->lineParity = line_parity;
1171 + eccOther->lineParityPrime = line_parity_prime;
1174 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1175 + yaffs_ECCOther * read_ecc,
1176 + const yaffs_ECCOther * test_ecc)
1178 + unsigned char cDelta; /* column parity delta */
1179 + unsigned lDelta; /* line parity delta */
1180 + unsigned lDeltaPrime; /* line parity delta */
1183 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1184 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1185 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1187 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1188 + return 0; /* no error */
1190 + if (lDelta == ~lDeltaPrime &&
1191 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1193 + /* Single bit (recoverable) error in data */
1197 + if (cDelta & 0x20)
1199 + if (cDelta & 0x08)
1201 + if (cDelta & 0x02)
1204 + if(lDelta >= nBytes)
1207 + data[lDelta] ^= (1 << bit);
1209 + return 1; /* corrected */
1212 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1213 + yaffs_CountBits(cDelta)) == 1) {
1214 + /* Reccoverable error in ecc */
1216 + *read_ecc = *test_ecc;
1217 + return 1; /* corrected */
1220 + /* Unrecoverable error */
1226 diff -urN linux.old/fs/yaffs2/yaffs_ecc.h linux.dev/fs/yaffs2/yaffs_ecc.h
1227 --- linux.old/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1228 +++ linux.dev/fs/yaffs2/yaffs_ecc.h 2006-12-14 04:21:47.000000000 +0100
1231 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
1233 + * yaffs_ecc.c: ECC generation/correction algorithms.
1235 + * Copyright (C) 2002 Aleph One Ltd.
1237 + * Created by Charles Manning <charles@aleph1.co.uk>
1239 + * This program is free software; you can redistribute it and/or modify
1240 + * it under the terms of the GNU General Public License version 2 as
1241 + * published by the Free Software Foundation.
1246 + * This code implements the ECC algorithm used in SmartMedia.
1248 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1249 + * The two unused bit are set to 1.
1250 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1251 + * blocks are used on a 512-byte NAND page.
1255 +#ifndef __YAFFS_ECC_H__
1256 +#define __YAFFS_ECC_H__
1259 + unsigned char colParity;
1260 + unsigned lineParity;
1261 + unsigned lineParityPrime;
1264 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1265 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1266 + const unsigned char *test_ecc);
1268 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1269 + yaffs_ECCOther * ecc);
1270 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1271 + yaffs_ECCOther * read_ecc,
1272 + const yaffs_ECCOther * test_ecc);
1274 diff -urN linux.old/fs/yaffs2/yaffs_fs.c linux.dev/fs/yaffs2/yaffs_fs.c
1275 --- linux.old/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1276 +++ linux.dev/fs/yaffs2/yaffs_fs.c 2006-12-14 04:33:02.000000000 +0100
1279 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
1282 + * Copyright (C) 2002 Aleph One Ltd.
1283 + * for Toby Churchill Ltd and Brightstar Engineering
1285 + * Created by Charles Manning <charles@aleph1.co.uk>
1287 + * This program is free software; you can redistribute it and/or modify
1288 + * it under the terms of the GNU General Public License version 2 as
1289 + * published by the Free Software Foundation.
1291 + * This is the file system front-end to YAFFS that hooks it up to
1295 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1297 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1299 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1301 + * Acknowledgements:
1302 + * * Luc van OostenRyck for numerous patches.
1303 + * * Nick Bane for numerous patches.
1304 + * * Nick Bane for 2.5/2.6 integration.
1305 + * * Andras Toth for mknod rdev issue.
1306 + * * Michael Fischer for finding the problem with inode inconsistency.
1307 + * * Some code bodily lifted from JFFS2.
1310 +const char *yaffs_fs_c_version =
1311 + "$Id: yaffs_fs.c,v 1.54 2006/10/24 18:09:15 charles Exp $";
1312 +extern const char *yaffs_guts_c_version;
1314 +#include <linux/autoconf.h>
1315 +#include <linux/kernel.h>
1316 +#include <linux/module.h>
1317 +#include <linux/version.h>
1318 +#include <linux/slab.h>
1319 +#include <linux/init.h>
1320 +#include <linux/list.h>
1321 +#include <linux/fs.h>
1322 +#include <linux/proc_fs.h>
1323 +#include <linux/smp_lock.h>
1324 +#include <linux/pagemap.h>
1325 +#include <linux/mtd/mtd.h>
1326 +#include <linux/interrupt.h>
1327 +#include <linux/string.h>
1328 +#include <linux/ctype.h>
1330 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1332 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1333 +#include <asm/statfs.h>
1334 +#define UnlockPage(p) unlock_page(p)
1335 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1337 +/* FIXME: use sb->s_id instead ? */
1338 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1342 +#include <linux/locks.h>
1343 +#define BDEVNAME_SIZE 0
1344 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1346 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1347 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1353 +#include <asm/uaccess.h>
1355 +#include "yportenv.h"
1356 +#include "yaffs_guts.h"
1358 +unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
1359 + YAFFS_TRACE_BAD_BLOCKS
1360 + /* | 0xFFFFFFFF */;
1362 +#include <linux/mtd/mtd.h>
1363 +#include "yaffs_mtdif.h"
1364 +#include "yaffs_mtdif2.h"
1366 +/*#define T(x) printk x */
1368 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1369 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->i_private))
1371 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
1373 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1375 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1376 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1378 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1381 +static void yaffs_put_super(struct super_block *sb);
1383 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1386 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1387 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1389 +static int yaffs_file_flush(struct file *file);
1392 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1395 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1397 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1398 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1399 + struct nameidata *n);
1400 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1401 + struct nameidata *n);
1403 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1404 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1406 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1407 + struct dentry *dentry);
1408 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1409 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1410 + const char *symname);
1411 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1413 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1414 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1417 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1420 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1421 + struct inode *new_dir, struct dentry *new_dentry);
1422 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1424 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1425 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1426 +static void yaffs_write_super(struct super_block *sb);
1428 +static int yaffs_sync_fs(struct super_block *sb);
1429 +static int yaffs_write_super(struct super_block *sb);
1432 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1433 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1434 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1435 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1437 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1439 +static void yaffs_read_inode(struct inode *inode);
1441 +static void yaffs_put_inode(struct inode *inode);
1442 +static void yaffs_delete_inode(struct inode *);
1443 +static void yaffs_clear_inode(struct inode *);
1445 +static int yaffs_readpage(struct file *file, struct page *page);
1446 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1447 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1449 +static int yaffs_writepage(struct page *page);
1451 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1452 + unsigned offset, unsigned to);
1453 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1456 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1458 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1459 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1461 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1464 +static struct address_space_operations yaffs_file_address_operations = {
1465 + .readpage = yaffs_readpage,
1466 + .writepage = yaffs_writepage,
1467 + .prepare_write = yaffs_prepare_write,
1468 + .commit_write = yaffs_commit_write,
1471 +static struct file_operations yaffs_file_operations = {
1472 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1473 + .read = do_sync_read,
1474 + .write = do_sync_write,
1475 + .aio_read = generic_file_aio_read,
1476 + .aio_write = generic_file_aio_write,
1478 + .read = generic_file_read,
1479 + .write = generic_file_write,
1481 + .mmap = generic_file_mmap,
1482 + .flush = yaffs_file_flush,
1483 + .fsync = yaffs_sync_object,
1484 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1485 + .sendfile = generic_file_sendfile,
1490 +static struct inode_operations yaffs_file_inode_operations = {
1491 + .setattr = yaffs_setattr,
1494 +static struct inode_operations yaffs_symlink_inode_operations = {
1495 + .readlink = yaffs_readlink,
1496 + .follow_link = yaffs_follow_link,
1497 + .setattr = yaffs_setattr,
1500 +static struct inode_operations yaffs_dir_inode_operations = {
1501 + .create = yaffs_create,
1502 + .lookup = yaffs_lookup,
1503 + .link = yaffs_link,
1504 + .unlink = yaffs_unlink,
1505 + .symlink = yaffs_symlink,
1506 + .mkdir = yaffs_mkdir,
1507 + .rmdir = yaffs_unlink,
1508 + .mknod = yaffs_mknod,
1509 + .rename = yaffs_rename,
1510 + .setattr = yaffs_setattr,
1513 +static struct file_operations yaffs_dir_operations = {
1514 + .read = generic_read_dir,
1515 + .readdir = yaffs_readdir,
1516 + .fsync = yaffs_sync_object,
1519 +static struct super_operations yaffs_super_ops = {
1520 + .statfs = yaffs_statfs,
1521 + .read_inode = yaffs_read_inode,
1522 + .put_inode = yaffs_put_inode,
1523 + .put_super = yaffs_put_super,
1524 + .delete_inode = yaffs_delete_inode,
1525 + .clear_inode = yaffs_clear_inode,
1526 + .sync_fs = yaffs_sync_fs,
1527 + .write_super = yaffs_write_super,
1530 +static void yaffs_GrossLock(yaffs_Device * dev)
1532 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1534 + down(&dev->grossLock);
1537 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1539 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1540 + up(&dev->grossLock);
1544 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1547 + unsigned char *alias;
1550 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1552 + yaffs_GrossLock(dev);
1554 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1556 + yaffs_GrossUnlock(dev);
1561 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1566 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1567 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1569 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1572 + unsigned char *alias;
1574 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1576 + yaffs_GrossLock(dev);
1578 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1580 + yaffs_GrossUnlock(dev);
1588 + ret = vfs_follow_link(nd, alias);
1591 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1592 + return ERR_PTR (ret);
1598 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1599 + yaffs_Object * obj);
1602 + * Lookup is used to find objects in the fs
1604 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1606 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1607 + struct nameidata *n)
1609 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1612 + yaffs_Object *obj;
1613 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1615 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1617 + yaffs_GrossLock(dev);
1620 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1621 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1624 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1625 + dentry->d_name.name);
1627 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1629 + /* Can't hold gross lock when calling yaffs_get_inode() */
1630 + yaffs_GrossUnlock(dev);
1634 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1636 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1640 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1641 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1642 + * d_add even if NULL inode */
1644 + /*dget(dentry); // try to solve directory bug */
1645 + d_add(dentry, inode);
1647 + /* return dentry; */
1653 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1657 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1658 + * NULL which creates dentry hash */
1659 + d_add(dentry, inode);
1662 + /* return (ERR_PTR(-EIO)); */
1666 +/* For now put inode is just for debugging
1667 + * Put inode is called when the inode **structure** is put.
1669 +static void yaffs_put_inode(struct inode *inode)
1672 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1673 + atomic_read(&inode->i_count)));
1677 +/* clear is called to tell the fs to release any per-inode data it holds */
1678 +static void yaffs_clear_inode(struct inode *inode)
1680 + yaffs_Object *obj;
1681 + yaffs_Device *dev;
1683 + obj = yaffs_InodeToObject(inode);
1686 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1687 + atomic_read(&inode->i_count),
1688 + obj ? "object exists" : "null object"));
1692 + yaffs_GrossLock(dev);
1694 + /* Clear the association between the inode and
1695 + * the yaffs_Object.
1697 + obj->myInode = NULL;
1698 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1699 + inode->i_private = NULL;
1701 + inode->u.generic_ip = NULL;
1704 + /* If the object freeing was deferred, then the real
1705 + * free happens now.
1706 + * This should fix the inode inconsistency problem.
1709 + yaffs_HandleDeferedFree(obj);
1711 + yaffs_GrossUnlock(dev);
1716 +/* delete is called when the link count is zero and the inode
1717 + * is put (ie. nobody wants to know about it anymore, time to
1718 + * delete the file).
1719 + * NB Must call clear_inode()
1721 +static void yaffs_delete_inode(struct inode *inode)
1723 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1724 + yaffs_Device *dev;
1727 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1728 + atomic_read(&inode->i_count),
1729 + obj ? "object exists" : "null object"));
1733 + yaffs_GrossLock(dev);
1734 + yaffs_DeleteFile(obj);
1735 + yaffs_GrossUnlock(dev);
1737 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1738 + truncate_inode_pages (&inode->i_data, 0);
1740 + clear_inode(inode);
1743 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1744 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1746 +static int yaffs_file_flush(struct file *file)
1749 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1751 + yaffs_Device *dev = obj->myDev;
1754 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1755 + obj->dirty ? "dirty" : "clean"));
1757 + yaffs_GrossLock(dev);
1759 + yaffs_FlushFile(obj, 1);
1761 + yaffs_GrossUnlock(dev);
1766 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1768 + /* Lifted from jffs2 */
1770 + yaffs_Object *obj;
1771 + unsigned char *pg_buf;
1774 + yaffs_Device *dev;
1776 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1777 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1778 + (unsigned)PAGE_CACHE_SIZE));
1780 + obj = yaffs_DentryToObject(f->f_dentry);
1784 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1785 + BUG_ON(!PageLocked(pg));
1787 + if (!PageLocked(pg))
1791 + pg_buf = kmap(pg);
1792 + /* FIXME: Can kmap fail? */
1794 + yaffs_GrossLock(dev);
1797 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1800 + yaffs_GrossUnlock(dev);
1806 + ClearPageUptodate(pg);
1809 + SetPageUptodate(pg);
1810 + ClearPageError(pg);
1813 + flush_dcache_page(pg);
1816 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1820 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1822 + int ret = yaffs_readpage_nolock(f, pg);
1827 +static int yaffs_readpage(struct file *f, struct page *pg)
1829 + return yaffs_readpage_unlock(f, pg);
1832 +/* writepage inspired by/stolen from smbfs */
1834 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1835 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1837 +static int yaffs_writepage(struct page *page)
1840 + struct address_space *mapping = page->mapping;
1841 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1842 + struct inode *inode;
1843 + unsigned long end_index;
1845 + yaffs_Object *obj;
1851 + inode = mapping->host;
1855 + if (offset > inode->i_size) {
1858 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1859 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1860 + (unsigned)inode->i_size));
1862 + (KERN_DEBUG " -> don't care!!\n"));
1863 + unlock_page(page);
1867 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1870 + if (page->index < end_index) {
1871 + nBytes = PAGE_CACHE_SIZE;
1873 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1878 + buffer = kmap(page);
1880 + obj = yaffs_InodeToObject(inode);
1881 + yaffs_GrossLock(obj->myDev);
1884 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
1885 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1887 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
1888 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1891 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
1895 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
1896 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1898 + yaffs_GrossUnlock(obj->myDev);
1901 + SetPageUptodate(page);
1905 + return (nWritten == nBytes) ? 0 : -ENOSPC;
1908 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1909 + unsigned offset, unsigned to)
1912 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
1913 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
1914 + return yaffs_readpage_nolock(f, pg);
1920 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1924 + void *addr = page_address(pg) + offset;
1925 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1926 + int nBytes = to - offset;
1929 + unsigned spos = pos;
1930 + unsigned saddr = (unsigned)addr;
1933 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
1936 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1938 + if (nWritten != nBytes) {
1941 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
1942 + nWritten, nBytes));
1944 + ClearPageUptodate(pg);
1946 + SetPageUptodate(pg);
1950 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
1951 + nWritten == nBytes ? 0 : nWritten));
1953 + return nWritten == nBytes ? 0 : nWritten;
1957 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
1959 + if (inode && obj) {
1962 + /* Check mode against the variant type and attempt to repair if broken. */
1963 + __u32 mode = obj->yst_mode;
1964 + switch( obj->variantType ){
1965 + case YAFFS_OBJECT_TYPE_FILE :
1966 + if( ! S_ISREG(mode) ){
1967 + obj->yst_mode &= ~S_IFMT;
1968 + obj->yst_mode |= S_IFREG;
1972 + case YAFFS_OBJECT_TYPE_SYMLINK :
1973 + if( ! S_ISLNK(mode) ){
1974 + obj->yst_mode &= ~S_IFMT;
1975 + obj->yst_mode |= S_IFLNK;
1979 + case YAFFS_OBJECT_TYPE_DIRECTORY :
1980 + if( ! S_ISDIR(mode) ){
1981 + obj->yst_mode &= ~S_IFMT;
1982 + obj->yst_mode |= S_IFDIR;
1986 + case YAFFS_OBJECT_TYPE_UNKNOWN :
1987 + case YAFFS_OBJECT_TYPE_HARDLINK :
1988 + case YAFFS_OBJECT_TYPE_SPECIAL :
1994 + inode->i_ino = obj->objectId;
1995 + inode->i_mode = obj->yst_mode;
1996 + inode->i_uid = obj->yst_uid;
1997 + inode->i_gid = obj->yst_gid;
1998 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1999 + inode->i_blksize = inode->i_sb->s_blocksize;
2001 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2003 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2004 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2005 + inode->i_atime.tv_nsec = 0;
2006 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2007 + inode->i_mtime.tv_nsec = 0;
2008 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2009 + inode->i_ctime.tv_nsec = 0;
2011 + inode->i_rdev = obj->yst_rdev;
2012 + inode->i_atime = obj->yst_atime;
2013 + inode->i_mtime = obj->yst_mtime;
2014 + inode->i_ctime = obj->yst_ctime;
2016 + inode->i_size = yaffs_GetObjectFileLength(obj);
2017 + inode->i_blocks = (inode->i_size + 511) >> 9;
2019 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2023 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2024 + inode->i_mode, inode->i_uid, inode->i_gid,
2025 + (int)inode->i_size, atomic_read(&inode->i_count)));
2027 + switch (obj->yst_mode & S_IFMT) {
2028 + default: /* fifo, device or socket */
2029 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2030 + init_special_inode(inode, obj->yst_mode,
2031 + old_decode_dev(obj->yst_rdev));
2033 + init_special_inode(inode, obj->yst_mode,
2034 + (dev_t) (obj->yst_rdev));
2037 + case S_IFREG: /* file */
2038 + inode->i_op = &yaffs_file_inode_operations;
2039 + inode->i_fop = &yaffs_file_operations;
2040 + inode->i_mapping->a_ops =
2041 + &yaffs_file_address_operations;
2043 + case S_IFDIR: /* directory */
2044 + inode->i_op = &yaffs_dir_inode_operations;
2045 + inode->i_fop = &yaffs_dir_operations;
2047 + case S_IFLNK: /* symlink */
2048 + inode->i_op = &yaffs_symlink_inode_operations;
2052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
2053 + inode->i_private = obj;
2055 + inode->u.generic_ip = obj;
2057 + obj->myInode = inode;
2061 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2066 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2067 + yaffs_Object * obj)
2069 + struct inode *inode;
2073 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2080 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2086 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2088 + inode = iget(sb, obj->objectId);
2090 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2091 + /* iget also increments the inode's i_count */
2092 + /* NB You can't be holding grossLock or deadlock will happen! */
2097 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2100 + yaffs_Object *obj;
2101 + int nWritten, ipos;
2102 + struct inode *inode;
2103 + yaffs_Device *dev;
2105 + obj = yaffs_DentryToObject(f->f_dentry);
2109 + yaffs_GrossLock(dev);
2111 + inode = f->f_dentry->d_inode;
2113 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2114 + ipos = inode->i_size;
2121 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2125 + "yaffs_file_write about to write writing %d bytes"
2126 + "to object %d at %d\n",
2127 + n, obj->objectId, ipos));
2130 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2133 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2134 + n, nWritten, ipos));
2135 + if (nWritten > 0) {
2138 + if (ipos > inode->i_size) {
2139 + inode->i_size = ipos;
2140 + inode->i_blocks = (ipos + 511) >> 9;
2144 + "yaffs_file_write size updated to %d bytes, "
2146 + ipos, (int)(inode->i_blocks)));
2150 + yaffs_GrossUnlock(dev);
2151 + return nWritten == 0 ? -ENOSPC : nWritten;
2154 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2156 + yaffs_Object *obj;
2157 + yaffs_Device *dev;
2158 + struct inode *inode = f->f_dentry->d_inode;
2159 + unsigned long offset, curoffs;
2160 + struct list_head *i;
2163 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2165 + obj = yaffs_DentryToObject(f->f_dentry);
2168 + yaffs_GrossLock(dev);
2170 + offset = f->f_pos;
2172 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2174 + if (offset == 0) {
2176 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2177 + (int)inode->i_ino));
2178 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2185 + if (offset == 1) {
2187 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2188 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2190 + (dirent, "..", 2, offset,
2191 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2200 + /* If the directory has changed since the open or last call to
2201 + readdir, rewind to after the 2 canned entries. */
2203 + if (f->f_version != inode->i_version) {
2205 + f->f_pos = offset;
2206 + f->f_version = inode->i_version;
2209 + list_for_each(i, &obj->variant.directoryVariant.children) {
2211 + if (curoffs >= offset) {
2212 + l = list_entry(i, yaffs_Object, siblings);
2214 + yaffs_GetObjectName(l, name,
2215 + YAFFS_MAX_NAME_LENGTH + 1);
2217 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2218 + yaffs_GetObjectInode(l)));
2220 + if (filldir(dirent,
2224 + yaffs_GetObjectInode(l),
2225 + yaffs_GetObjectType(l))
2238 + yaffs_GrossUnlock(dev);
2244 + * File creation. Allocate an inode, and we're done..
2246 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2247 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2250 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2254 + struct inode *inode;
2256 + yaffs_Object *obj = NULL;
2257 + yaffs_Device *dev;
2259 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2261 + int error = -ENOSPC;
2262 + uid_t uid = current->fsuid;
2263 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2265 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2270 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2271 + parent->objectId, parent->variantType));
2274 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2278 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2279 + "mode %x dev %x\n",
2280 + dentry->d_name.name, mode, rdev));
2282 + dev = parent->myDev;
2284 + yaffs_GrossLock(dev);
2286 + switch (mode & S_IFMT) {
2288 + /* Special (socket, fifo, device...) */
2289 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2290 + "yaffs_mknod: making special\n"));
2291 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2293 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2294 + gid, old_encode_dev(rdev));
2297 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2301 + case S_IFREG: /* file */
2302 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2304 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2307 + case S_IFDIR: /* directory */
2309 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2311 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2314 + case S_IFLNK: /* symlink */
2315 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2316 + obj = NULL; /* Do we ever get here? */
2320 + /* Can not call yaffs_get_inode() with gross lock held */
2321 + yaffs_GrossUnlock(dev);
2324 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2325 + d_instantiate(dentry, inode);
2327 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2328 + obj->objectId, atomic_read(&inode->i_count)));
2332 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2339 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2342 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2343 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2345 + /* attempt to fix dir bug - didn't work */
2353 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2354 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2355 + struct nameidata *n)
2357 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2360 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2361 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2364 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2368 + yaffs_Device *dev;
2371 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2372 + dentry->d_name.name));
2374 + dev = yaffs_InodeToObject(dir)->myDev;
2376 + yaffs_GrossLock(dev);
2378 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2380 + if (retVal == YAFFS_OK) {
2381 + dentry->d_inode->i_nlink--;
2383 + yaffs_GrossUnlock(dev);
2384 + mark_inode_dirty(dentry->d_inode);
2387 + yaffs_GrossUnlock(dev);
2388 + return -ENOTEMPTY;
2392 + * Create a link...
2394 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2395 + struct dentry *dentry)
2397 + struct inode *inode = old_dentry->d_inode;
2398 + yaffs_Object *obj = NULL;
2399 + yaffs_Object *link = NULL;
2400 + yaffs_Device *dev;
2402 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2404 + obj = yaffs_InodeToObject(inode);
2407 + yaffs_GrossLock(dev);
2409 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2412 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2417 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2418 + d_instantiate(dentry, old_dentry->d_inode);
2419 + atomic_inc(&old_dentry->d_inode->i_count);
2421 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2422 + old_dentry->d_inode->i_nlink,
2423 + atomic_read(&old_dentry->d_inode->i_count)));
2427 + yaffs_GrossUnlock(dev);
2437 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2438 + const char *symname)
2440 + yaffs_Object *obj;
2441 + yaffs_Device *dev;
2442 + uid_t uid = current->fsuid;
2443 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2445 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2447 + dev = yaffs_InodeToObject(dir)->myDev;
2448 + yaffs_GrossLock(dev);
2449 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2450 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2451 + yaffs_GrossUnlock(dev);
2455 + struct inode *inode;
2457 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2458 + d_instantiate(dentry, inode);
2459 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2462 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2469 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2473 + yaffs_Object *obj;
2474 + yaffs_Device *dev;
2476 + obj = yaffs_DentryToObject(dentry);
2480 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2481 + yaffs_GrossLock(dev);
2482 + yaffs_FlushFile(obj, 1);
2483 + yaffs_GrossUnlock(dev);
2488 + * The VFS layer already does all the dentry stuff for rename.
2490 + * NB: POSIX says you can rename an object over an old object of the same name
2492 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2493 + struct inode *new_dir, struct dentry *new_dentry)
2495 + yaffs_Device *dev;
2496 + int retVal = YAFFS_FAIL;
2497 + yaffs_Object *target;
2499 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2500 + dev = yaffs_InodeToObject(old_dir)->myDev;
2502 + yaffs_GrossLock(dev);
2504 + /* Check if the target is an existing directory that is not empty. */
2506 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2507 + new_dentry->d_name.name);
2512 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2513 + !list_empty(&target->variant.directoryVariant.children)) {
2515 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2517 + retVal = YAFFS_FAIL;
2520 + /* Now does unlinking internally using shadowing mechanism */
2521 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2524 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2525 + old_dentry->d_name.name,
2526 + yaffs_InodeToObject(new_dir),
2527 + new_dentry->d_name.name);
2530 + yaffs_GrossUnlock(dev);
2532 + if (retVal == YAFFS_OK) {
2534 + new_dentry->d_inode->i_nlink--;
2535 + mark_inode_dirty(new_dentry->d_inode);
2540 + return -ENOTEMPTY;
2545 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2547 + struct inode *inode = dentry->d_inode;
2549 + yaffs_Device *dev;
2552 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2553 + yaffs_InodeToObject(inode)->objectId));
2555 + if ((error = inode_change_ok(inode, attr)) == 0) {
2557 + dev = yaffs_InodeToObject(inode)->myDev;
2558 + yaffs_GrossLock(dev);
2559 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2565 + yaffs_GrossUnlock(dev);
2567 + error = inode_setattr(inode, attr);
2572 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2573 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2575 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2576 + struct super_block *sb = dentry->d_sb;
2577 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2578 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2580 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2582 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2584 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2587 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2589 + yaffs_GrossLock(dev);
2591 + buf->f_type = YAFFS_MAGIC;
2592 + buf->f_bsize = sb->s_blocksize;
2593 + buf->f_namelen = 255;
2594 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2597 + (dev->endBlock - dev->startBlock +
2598 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2599 + dev->nDataBytesPerChunk);
2601 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2602 + dev->nDataBytesPerChunk);
2606 + (dev->endBlock - dev->startBlock +
2607 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2610 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2615 + buf->f_bavail = buf->f_bfree;
2617 + yaffs_GrossUnlock(dev);
2623 +static int yaffs_do_sync_fs(struct super_block *sb)
2626 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2627 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2630 + yaffs_GrossLock(dev);
2633 + yaffs_CheckpointSave(dev);
2635 + yaffs_GrossUnlock(dev);
2643 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2644 +static void yaffs_write_super(struct super_block *sb)
2646 +static int yaffs_write_super(struct super_block *sb)
2650 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2651 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2652 + return 0; /* yaffs_do_sync_fs(sb);*/
2657 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2658 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2660 +static int yaffs_sync_fs(struct super_block *sb)
2664 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2666 + return 0; /* yaffs_do_sync_fs(sb);*/
2671 +static void yaffs_read_inode(struct inode *inode)
2673 + /* NB This is called as a side effect of other functions, but
2674 + * we had to release the lock to prevent deadlocks, so
2675 + * need to lock again.
2678 + yaffs_Object *obj;
2679 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2682 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2684 + yaffs_GrossLock(dev);
2686 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2688 + yaffs_FillInodeFromObject(inode, obj);
2690 + yaffs_GrossUnlock(dev);
2693 +static LIST_HEAD(yaffs_dev_list);
2695 +static void yaffs_put_super(struct super_block *sb)
2697 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2699 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2701 + yaffs_GrossLock(dev);
2703 + yaffs_FlushEntireDeviceCache(dev);
2705 + if (dev->putSuperFunc) {
2706 + dev->putSuperFunc(sb);
2709 + yaffs_CheckpointSave(dev);
2710 + yaffs_Deinitialise(dev);
2712 + yaffs_GrossUnlock(dev);
2714 + /* we assume this is protected by lock_kernel() in mount/umount */
2715 + list_del(&dev->devList);
2717 + if(dev->spareBuffer){
2718 + YFREE(dev->spareBuffer);
2719 + dev->spareBuffer = NULL;
2726 +static void yaffs_MTDPutSuper(struct super_block *sb)
2729 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2735 + put_mtd_device(mtd);
2739 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2741 + struct super_block *sb = (struct super_block *)vsb;
2743 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2748 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2749 + struct super_block *sb,
2750 + void *data, int silent)
2753 + struct inode *inode = NULL;
2754 + struct dentry *root;
2755 + yaffs_Device *dev = 0;
2756 + char devname_buf[BDEVNAME_SIZE + 1];
2757 + struct mtd_info *mtd;
2760 + sb->s_magic = YAFFS_MAGIC;
2761 + sb->s_op = &yaffs_super_ops;
2764 + printk(KERN_INFO "yaffs: sb is NULL\n");
2765 + else if (!sb->s_dev)
2766 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2767 + else if (!yaffs_devname(sb, devname_buf))
2768 + printk(KERN_INFO "yaffs: devname is NULL\n");
2770 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2772 + yaffs_devname(sb, devname_buf));
2774 + sb->s_blocksize = PAGE_CACHE_SIZE;
2775 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2776 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2778 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2780 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2782 + ("yaffs: Write verification disabled. All guarantees "
2783 + "null and void\n"));
2786 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2788 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
2789 + yaffs_devname(sb, devname_buf)));
2791 + /* Check it's an mtd device..... */
2792 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
2793 + return NULL; /* This isn't an mtd device */
2795 + /* Get the device */
2796 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2798 + T(YAFFS_TRACE_ALWAYS,
2799 + ("yaffs: MTD device #%u doesn't appear to exist\n",
2800 + MINOR(sb->s_dev)));
2803 + /* Check it's NAND */
2804 + if (mtd->type != MTD_NANDFLASH) {
2805 + T(YAFFS_TRACE_ALWAYS,
2806 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2810 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2811 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2812 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2813 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2814 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2815 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2816 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2817 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2818 + T(YAFFS_TRACE_OS, (" writesize %d\n", mtd->writesize));
2820 + T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
2822 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2823 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2824 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
2826 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
2828 + if (yaffsVersion == 1 &&
2829 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2830 + mtd->writesize >= 2048) {
2832 + mtd->oobblock >= 2048) {
2834 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
2838 + /* Added NCB 26/5/2006 for completeness */
2839 + if (yaffsVersion == 2 &&
2840 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2841 + mtd->writesize == 512) {
2843 + mtd->oobblock == 512) {
2845 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
2851 + if (yaffsVersion == 2) {
2852 + /* Check for version 2 style functions */
2853 + if (!mtd->erase ||
2854 + !mtd->block_isbad ||
2855 + !mtd->block_markbad ||
2858 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2859 + !mtd->read_oob || !mtd->write_oob) {
2861 + !mtd->write_ecc ||
2862 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2864 + T(YAFFS_TRACE_ALWAYS,
2865 + ("yaffs: MTD device does not support required "
2870 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2871 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2873 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2875 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
2876 + T(YAFFS_TRACE_ALWAYS,
2877 + ("yaffs: MTD device does not have the "
2878 + "right page sizes\n"));
2882 + /* Check for V1 style functions */
2883 + if (!mtd->erase ||
2886 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2887 + !mtd->read_oob || !mtd->write_oob) {
2889 + !mtd->write_ecc ||
2890 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2892 + T(YAFFS_TRACE_ALWAYS,
2893 + ("yaffs: MTD device does not support required "
2898 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2899 + if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
2901 + if (mtd->oobblock < YAFFS_BYTES_PER_CHUNK ||
2903 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2904 + T(YAFFS_TRACE_ALWAYS,
2905 + ("yaffs: MTD device does not support have the "
2906 + "right page sizes\n"));
2911 + /* OK, so if we got here, we have an MTD that's NAND and looks
2912 + * like it has the right capabilities
2913 + * Set the yaffs_Device up for mtd
2916 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2917 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2919 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2922 + /* Deep shit could not allocate device structure */
2923 + T(YAFFS_TRACE_ALWAYS,
2924 + ("yaffs_read_super: Failed trying to allocate "
2925 + "yaffs_Device. \n"));
2929 + memset(dev, 0, sizeof(yaffs_Device));
2930 + dev->genericDevice = mtd;
2931 + dev->name = mtd->name;
2933 + /* Set up the memory size parameters.... */
2935 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
2936 + dev->startBlock = 0;
2937 + dev->endBlock = nBlocks - 1;
2938 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2939 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2940 + dev->nReservedBlocks = 5;
2941 + dev->nShortOpCaches = 10; /* Enable short op caching */
2943 + /* ... and the functions. */
2944 + if (yaffsVersion == 2) {
2945 + dev->writeChunkWithTagsToNAND =
2946 + nandmtd2_WriteChunkWithTagsToNAND;
2947 + dev->readChunkWithTagsFromNAND =
2948 + nandmtd2_ReadChunkWithTagsFromNAND;
2949 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2950 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2951 + dev->spareBuffer = YMALLOC(mtd->oobsize);
2952 + dev->isYaffs2 = 1;
2953 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2954 + dev->nDataBytesPerChunk = mtd->writesize;
2955 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2957 + dev->nDataBytesPerChunk = mtd->oobblock;
2958 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2960 + nBlocks = mtd->size / mtd->erasesize;
2962 + dev->nCheckpointReservedBlocks = 0;
2963 + dev->startBlock = 0;
2964 + dev->endBlock = nBlocks - 1;
2966 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2967 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2968 + dev->isYaffs2 = 0;
2970 + /* ... and common functions */
2971 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2972 + dev->initialiseNAND = nandmtd_InitialiseNAND;
2974 + dev->putSuperFunc = yaffs_MTDPutSuper;
2976 + dev->superBlock = (void *)sb;
2977 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2980 +#ifndef CONFIG_YAFFS_DOES_ECC
2981 + dev->useNANDECC = 1;
2984 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2985 + dev->wideTnodesDisabled = 1;
2988 + /* we assume this is protected by lock_kernel() in mount/umount */
2989 + list_add_tail(&dev->devList, &yaffs_dev_list);
2991 + init_MUTEX(&dev->grossLock);
2993 + yaffs_GrossLock(dev);
2995 + err = yaffs_GutsInitialise(dev);
2998 + ("yaffs_read_super: guts initialised %s\n",
2999 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3001 + /* Release lock before yaffs_get_inode() */
3002 + yaffs_GrossUnlock(dev);
3004 + /* Create root inode */
3005 + if (err == YAFFS_OK)
3006 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3012 + inode->i_op = &yaffs_dir_inode_operations;
3013 + inode->i_fop = &yaffs_dir_operations;
3015 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3017 + root = d_alloc_root(inode);
3019 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3025 + sb->s_root = root;
3027 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3032 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3033 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3036 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3039 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3040 +static int yaffs_read_super(struct file_system_type *fs,
3041 + int flags, const char *dev_name,
3042 + void *data, struct vfsmount *mnt)
3045 + return get_sb_bdev(fs, flags, dev_name, data,
3046 + yaffs_internal_read_super_mtd, mnt);
3049 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3050 + int flags, const char *dev_name,
3054 + return get_sb_bdev(fs, flags, dev_name, data,
3055 + yaffs_internal_read_super_mtd);
3059 +static struct file_system_type yaffs_fs_type = {
3060 + .owner = THIS_MODULE,
3062 + .get_sb = yaffs_read_super,
3063 + .kill_sb = kill_block_super,
3064 + .fs_flags = FS_REQUIRES_DEV,
3067 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3070 + return yaffs_internal_read_super(1, sb, data, silent);
3073 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3078 +#ifdef CONFIG_YAFFS_YAFFS2
3080 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3081 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3084 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3087 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3088 +static int yaffs2_read_super(struct file_system_type *fs,
3089 + int flags, const char *dev_name, void *data,
3090 + struct vfsmount *mnt)
3092 + return get_sb_bdev(fs, flags, dev_name, data,
3093 + yaffs2_internal_read_super_mtd, mnt);
3096 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3097 + int flags, const char *dev_name,
3101 + return get_sb_bdev(fs, flags, dev_name, data,
3102 + yaffs2_internal_read_super_mtd);
3106 +static struct file_system_type yaffs2_fs_type = {
3107 + .owner = THIS_MODULE,
3109 + .get_sb = yaffs2_read_super,
3110 + .kill_sb = kill_block_super,
3111 + .fs_flags = FS_REQUIRES_DEV,
3114 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3115 + void *data, int silent)
3117 + return yaffs_internal_read_super(2, sb, data, silent);
3120 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3124 +#endif /* CONFIG_YAFFS_YAFFS2 */
3126 +static struct proc_dir_entry *my_proc_entry;
3128 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3130 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3131 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3132 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3133 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3134 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3135 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3136 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3137 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3138 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3139 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3140 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3141 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3142 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3143 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3145 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3147 + sprintf(buf, "passiveGCs......... %d\n",
3148 + dev->passiveGarbageCollections);
3149 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3150 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3151 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3152 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3153 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3154 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3155 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3156 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3157 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3159 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3160 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3161 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3166 +static int yaffs_proc_read(char *page,
3168 + off_t offset, int count, int *eof, void *data)
3170 + struct list_head *item;
3172 + int step = offset;
3175 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3176 + * We use 'offset' (*ppos) to indicate where we are in devList.
3177 + * This also assumes the user has posted a read buffer large
3178 + * enough to hold the complete output; but that's life in /proc.
3181 + *(int *)start = 1;
3183 + /* Print header first */
3185 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3186 + "\n%s\n%s\n", yaffs_fs_c_version,
3187 + yaffs_guts_c_version);
3190 + /* hold lock_kernel while traversing yaffs_dev_list */
3193 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3194 + list_for_each(item, &yaffs_dev_list) {
3195 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3200 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3201 + buf = yaffs_dump_dev(buf, dev);
3206 + return buf - page < count ? buf - page : count;
3210 + * Set the verbosity of the warnings and error messages.
3216 + unsigned mask_bitfield;
3218 + {"allocate", YAFFS_TRACE_ALLOCATE},
3219 + {"always", YAFFS_TRACE_ALWAYS},
3220 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3221 + {"buffers", YAFFS_TRACE_BUFFERS},
3222 + {"bug", YAFFS_TRACE_BUG},
3223 + {"deletion", YAFFS_TRACE_DELETION},
3224 + {"erase", YAFFS_TRACE_ERASE},
3225 + {"error", YAFFS_TRACE_ERROR},
3226 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3227 + {"gc", YAFFS_TRACE_GC},
3228 + {"mtd", YAFFS_TRACE_MTD},
3229 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3230 + {"os", YAFFS_TRACE_OS},
3231 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3232 + {"scan", YAFFS_TRACE_SCAN},
3233 + {"tracing", YAFFS_TRACE_TRACING},
3234 + {"write", YAFFS_TRACE_WRITE},
3235 + {"all", 0xffffffff},
3240 +static int yaffs_proc_write(struct file *file, const char *buf,
3241 + unsigned long count, void *data)
3243 + unsigned rg = 0, mask_bitfield;
3244 + char *end, *mask_name;
3250 + rg = yaffs_traceMask;
3252 + while (!done && (pos < count)) {
3254 + while ((pos < count) && isspace(buf[pos])) {
3258 + switch (buf[pos]) {
3271 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3272 + if (end > buf + pos) {
3273 + mask_name = "numeral";
3274 + len = end - (buf + pos);
3278 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3279 + len = strlen(mask_flags[i].mask_name);
3280 + if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3281 + mask_name = mask_flags[i].mask_name;
3282 + mask_bitfield = mask_flags[i].mask_bitfield;
3289 + if (mask_name != NULL) {
3294 + rg &= ~mask_bitfield;
3297 + rg |= mask_bitfield;
3300 + rg = mask_bitfield;
3303 + rg |= mask_bitfield;
3309 + yaffs_traceMask = rg;
3310 + if (rg & YAFFS_TRACE_ALWAYS) {
3311 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3313 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3314 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3321 +/* Stuff to handle installation of file systems */
3322 +struct file_system_to_install {
3323 + struct file_system_type *fst;
3327 +static struct file_system_to_install fs_to_install[] = {
3328 +//#ifdef CONFIG_YAFFS_YAFFS1
3329 + {&yaffs_fs_type, 0},
3331 +//#ifdef CONFIG_YAFFS_YAFFS2
3332 + {&yaffs2_fs_type, 0},
3337 +static int __init init_yaffs_fs(void)
3340 + struct file_system_to_install *fsinst;
3342 + T(YAFFS_TRACE_ALWAYS,
3343 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3345 + /* Install the proc_fs entry */
3346 + my_proc_entry = create_proc_entry("yaffs",
3347 + S_IRUGO | S_IFREG,
3350 + if (my_proc_entry) {
3351 + my_proc_entry->write_proc = yaffs_proc_write;
3352 + my_proc_entry->read_proc = yaffs_proc_read;
3353 + my_proc_entry->data = NULL;
3358 + /* Now add the file system entries */
3360 + fsinst = fs_to_install;
3362 + while (fsinst->fst && !error) {
3363 + error = register_filesystem(fsinst->fst);
3365 + fsinst->installed = 1;
3370 + /* Any errors? uninstall */
3372 + fsinst = fs_to_install;
3374 + while (fsinst->fst) {
3375 + if (fsinst->installed) {
3376 + unregister_filesystem(fsinst->fst);
3377 + fsinst->installed = 0;
3386 +static void __exit exit_yaffs_fs(void)
3389 + struct file_system_to_install *fsinst;
3391 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3392 + " removing. \n"));
3394 + remove_proc_entry("yaffs", &proc_root);
3396 + fsinst = fs_to_install;
3398 + while (fsinst->fst) {
3399 + if (fsinst->installed) {
3400 + unregister_filesystem(fsinst->fst);
3401 + fsinst->installed = 0;
3408 +module_init(init_yaffs_fs)
3409 +module_exit(exit_yaffs_fs)
3411 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3412 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3413 +MODULE_LICENSE("GPL");
3414 diff -urN linux.old/fs/yaffs2/yaffs_guts.c linux.dev/fs/yaffs2/yaffs_guts.c
3415 --- linux.old/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3416 +++ linux.dev/fs/yaffs2/yaffs_guts.c 2006-12-14 04:21:47.000000000 +0100
3419 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
3421 + * Copyright (C) 2002 Aleph One Ltd.
3422 + * for Toby Churchill Ltd and Brightstar Engineering
3424 + * Created by Charles Manning <charles@aleph1.co.uk>
3426 + * This program is free software; you can redistribute it and/or modify
3427 + * it under the terms of the GNU General Public License version 2 as
3428 + * published by the Free Software Foundation.
3432 +const char *yaffs_guts_c_version =
3433 + "$Id: yaffs_guts.c,v 1.45 2006/11/14 03:07:17 charles Exp $";
3435 +#include "yportenv.h"
3437 +#include "yaffsinterface.h"
3438 +#include "yaffs_guts.h"
3439 +#include "yaffs_tagsvalidity.h"
3441 +#include "yaffs_tagscompat.h"
3442 +#ifndef CONFIG_YAFFS_OWN_SORT
3443 +#include "yaffs_qsort.h"
3445 +#include "yaffs_nand.h"
3447 +#include "yaffs_checkptrw.h"
3449 +#include "yaffs_nand.h"
3450 +#include "yaffs_packedtags2.h"
3453 +#ifdef CONFIG_YAFFS_WINCE
3454 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3455 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3458 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3460 +#include "yaffs_ecc.h"
3463 +/* Robustification (if it ever comes about...) */
3464 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3465 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3466 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3467 + const __u8 * data,
3468 + const yaffs_ExtendedTags * tags);
3469 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3470 + const yaffs_ExtendedTags * tags);
3472 +/* Other local prototypes */
3473 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3474 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3476 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3478 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3479 + const __u8 * buffer,
3480 + yaffs_ExtendedTags * tags,
3482 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3483 + int chunkInNAND, int inScan);
3485 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3486 + yaffs_ObjectType type);
3487 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3488 + yaffs_Object * obj);
3489 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3490 + int force, int isShrink, int shadows);
3491 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3492 +static int yaffs_CheckStructures(void);
3493 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3494 + int chunkOffset, int *limit);
3495 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3497 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3499 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3500 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3503 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3506 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3507 +static void yaffs_DestroyObject(yaffs_Object * obj);
3509 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3510 + int chunkInObject);
3512 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3514 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3516 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3518 +#ifdef YAFFS_PARANOID
3519 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3521 +#define yaffs_CheckFileSanity(in)
3524 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3525 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3527 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3531 +/* Function to calculate chunk and offset */
3533 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3535 + if(dev->chunkShift){
3536 + /* Easy-peasy power of 2 case */
3537 + *chunk = (__u32)(addr >> dev->chunkShift);
3538 + *offset = (__u32)(addr & dev->chunkMask);
3540 + else if(dev->crumbsPerChunk)
3542 + /* Case where we're using "crumbs" */
3543 + *offset = (__u32)(addr & dev->crumbMask);
3544 + addr >>= dev->crumbShift;
3545 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3546 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3552 +/* Function to return the number of shifts for a power of 2 greater than or equal
3553 + * to the given number
3554 + * Note we don't try to cater for all possible numbers and this does not have to
3555 + * be hellishly efficient.
3558 +static __u32 ShiftsGE(__u32 x)
3563 + nShifts = extraBits = 0;
3566 + if(x & 1) extraBits++;
3577 +/* Function to return the number of shifts to get a 1 in bit 0
3580 +static __u32 ShiftDiv(__u32 x)
3599 + * Temporary buffer manipulations.
3602 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3605 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3606 + if (dev->tempBuffer[i].line == 0) {
3607 + dev->tempBuffer[i].line = lineNo;
3608 + if ((i + 1) > dev->maxTemp) {
3609 + dev->maxTemp = i + 1;
3610 + for (j = 0; j <= i; j++)
3611 + dev->tempBuffer[j].maxLine =
3612 + dev->tempBuffer[j].line;
3615 + return dev->tempBuffer[i].buffer;
3619 + T(YAFFS_TRACE_BUFFERS,
3620 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3622 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3623 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3625 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3628 + * If we got here then we have to allocate an unmanaged one
3629 + * This is not good.
3632 + dev->unmanagedTempAllocations++;
3633 + return YMALLOC(dev->nDataBytesPerChunk);
3637 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3641 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3642 + if (dev->tempBuffer[i].buffer == buffer) {
3643 + dev->tempBuffer[i].line = 0;
3649 + /* assume it is an unmanaged one. */
3650 + T(YAFFS_TRACE_BUFFERS,
3651 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3654 + dev->unmanagedTempDeallocations++;
3660 + * Determine if we have a managed buffer.
3662 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3665 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3666 + if (dev->tempBuffer[i].buffer == buffer)
3671 + for (i = 0; i < dev->nShortOpCaches; i++) {
3672 + if( dev->srCache[i].data == buffer )
3677 + if (buffer == dev->checkpointBuffer)
3680 + T(YAFFS_TRACE_ALWAYS,
3681 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3686 + * Chunk bitmap manipulations
3689 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3691 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3692 + T(YAFFS_TRACE_ERROR,
3693 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3697 + return dev->chunkBits +
3698 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3701 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3703 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3705 + memset(blkBits, 0, dev->chunkBitmapStride);
3708 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3710 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3712 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
3715 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
3717 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3719 + blkBits[chunk / 8] |= (1 << (chunk & 7));
3722 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
3724 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3725 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
3728 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
3730 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3732 + for (i = 0; i < dev->chunkBitmapStride; i++) {
3741 + * Simple hash function. Needs to have a reasonable spread
3744 +static Y_INLINE int yaffs_HashFunction(int n)
3747 + return (n % YAFFS_NOBJECT_BUCKETS);
3751 + * Access functions to useful fake objects
3754 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
3756 + return dev->rootDir;
3759 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
3761 + return dev->lostNFoundDir;
3766 + * Erased NAND checking functions
3769 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
3771 + /* Horrible, slow implementation */
3772 + while (nBytes--) {
3773 + if (*buffer != 0xFF)
3780 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3784 + int retval = YAFFS_OK;
3785 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
3786 + yaffs_ExtendedTags tags;
3789 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
3791 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
3792 + retval = YAFFS_FAIL;
3795 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
3796 + T(YAFFS_TRACE_NANDACCESS,
3797 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
3798 + retval = YAFFS_FAIL;
3801 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
3807 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
3808 + const __u8 * data,
3809 + yaffs_ExtendedTags * tags,
3817 + yaffs_BlockInfo *bi;
3819 + yaffs_InvalidateCheckpoint(dev);
3822 + chunk = yaffs_AllocateChunk(dev, useReserve,&bi);
3825 + /* First check this chunk is erased, if it needs checking.
3826 + * The checking policy (unless forced always on) is as follows:
3827 + * Check the first page we try to write in a block.
3828 + * - If the check passes then we don't need to check any more.
3829 + * - If the check fails, we check again...
3830 + * If the block has been erased, we don't need to check.
3832 + * However, if the block has been prioritised for gc, then
3833 + * we think there might be something odd about this block
3834 + * and stop using it.
3837 + * We should only ever see chunks that have not been erased
3838 + * if there was a partially written chunk due to power loss
3839 + * This checking policy should catch that case with very
3840 + * few checks and thus save a lot of checks that are most likely not
3844 + if(bi->gcPrioritise){
3845 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
3847 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3849 + bi->skipErasedCheck = 0;
3852 + if(!bi->skipErasedCheck){
3853 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
3854 + if(erasedOk && !bi->gcPrioritise)
3855 + bi->skipErasedCheck = 1;
3859 + T(YAFFS_TRACE_ERROR,
3861 + ("**>> yaffs chunk %d was not erased"
3862 + TENDSTR), chunk));
3865 + yaffs_WriteChunkWithTagsToNAND(dev, chunk,
3873 + * Copy the data into the robustification buffer.
3874 + * NB We do this at the end to prevent duplicates in the case of a write error.
3877 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
3880 + /* The erased check or write failed */
3881 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
3886 + } while (chunk >= 0 && !writeOk);
3888 + if (attempts > 1) {
3889 + T(YAFFS_TRACE_ERROR,
3890 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
3892 + dev->nRetriedWrites += (attempts - 1);
3899 + * Block retiring for handling a broken block.
3902 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
3904 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
3906 + yaffs_InvalidateCheckpoint(dev);
3908 + yaffs_MarkBlockBad(dev, blockInNAND);
3910 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
3911 + bi->gcPrioritise = 0;
3912 + bi->needsRetiring = 0;
3914 + dev->nRetiredBlocks++;
3918 + * Functions for robustisizing TODO
3922 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3923 + const __u8 * data,
3924 + const yaffs_ExtendedTags * tags)
3928 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3929 + const yaffs_ExtendedTags * tags)
3933 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
3935 + if(!bi->gcPrioritise){
3936 + bi->gcPrioritise = 1;
3937 + dev->hasPendingPrioritisedGCs = 1;
3938 + bi->chunkErrorStrikes ++;
3940 + if(bi->chunkErrorStrikes > 3){
3941 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
3942 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
3949 +static void yaffs_ReportOddballBlocks(yaffs_Device *dev)
3953 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock && (yaffs_traceMask & YAFFS_TRACE_BAD_BLOCKS); i++){
3954 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
3955 + if(bi->needsRetiring || bi->gcPrioritise)
3956 + T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("yaffs block %d%s%s" TENDSTR),
3958 + bi->needsRetiring ? " needs retiring" : "",
3959 + bi->gcPrioritise ? " gc prioritised" : ""));
3964 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
3967 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
3968 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
3970 + yaffs_HandleChunkError(dev,bi);
3974 + /* Was an actual write failure, so mark the block for retirement */
3975 + bi->needsRetiring = 1;
3976 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
3977 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
3982 + /* Delete the chunk */
3983 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3987 +/*---------------- Name handling functions ------------*/
3989 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
3994 + YUCHAR *bname = (YUCHAR *) name;
3996 + while ((*bname) && (i <= YAFFS_MAX_NAME_LENGTH)) {
3998 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
3999 + sum += yaffs_toupper(*bname) * i;
4001 + sum += (*bname) * i;
4010 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4012 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4013 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4014 + yaffs_strcpy(obj->shortName, name);
4016 + obj->shortName[0] = _Y('\0');
4019 + obj->sum = yaffs_CalcNameSum(name);
4022 +/*-------------------- TNODES -------------------
4024 + * List of spare tnodes
4025 + * The list is hooked together using the first pointer
4029 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4030 + * adds them to the tnode free list.
4031 + * Don't use this function directly
4034 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4038 + yaffs_Tnode *newTnodes;
4040 + yaffs_Tnode *curr;
4041 + yaffs_Tnode *next;
4042 + yaffs_TnodeList *tnl;
4047 + /* Calculate the tnode size in bytes for variable width tnode support.
4048 + * Must be a multiple of 32-bits */
4049 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4051 + /* make these things */
4053 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4054 + mem = (__u8 *)newTnodes;
4057 + T(YAFFS_TRACE_ERROR,
4058 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4059 + return YAFFS_FAIL;
4062 + /* Hook them into the free list */
4064 + for (i = 0; i < nTnodes - 1; i++) {
4065 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4066 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4067 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4071 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4072 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4073 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4075 + dev->freeTnodes = newTnodes;
4077 + /* New hookup for wide tnodes */
4078 + for(i = 0; i < nTnodes -1; i++) {
4079 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4080 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4081 + curr->internal[0] = next;
4084 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4085 + curr->internal[0] = dev->freeTnodes;
4086 + dev->freeTnodes = (yaffs_Tnode *)mem;
4091 + dev->nFreeTnodes += nTnodes;
4092 + dev->nTnodesCreated += nTnodes;
4094 + /* Now add this bunch of tnodes to a list for freeing up.
4095 + * NB If we can't add this to the management list it isn't fatal
4096 + * but it just means we can't free this bunch of tnodes later.
4099 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4101 + T(YAFFS_TRACE_ERROR,
4103 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4106 + tnl->tnodes = newTnodes;
4107 + tnl->next = dev->allocatedTnodeList;
4108 + dev->allocatedTnodeList = tnl;
4111 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4116 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4118 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4120 + yaffs_Tnode *tn = NULL;
4122 + /* If there are none left make more */
4123 + if (!dev->freeTnodes) {
4124 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4127 + if (dev->freeTnodes) {
4128 + tn = dev->freeTnodes;
4129 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4130 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4131 + /* Hoosterman, this thing looks like it isn't in the list */
4132 + T(YAFFS_TRACE_ALWAYS,
4133 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4136 + dev->freeTnodes = dev->freeTnodes->internal[0];
4137 + dev->nFreeTnodes--;
4143 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4145 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4148 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4153 +/* FreeTnode frees up a tnode and puts it back on the free list */
4154 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4157 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4158 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4159 + /* Hoosterman, this thing looks like it is already in the list */
4160 + T(YAFFS_TRACE_ALWAYS,
4161 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4163 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4165 + tn->internal[0] = dev->freeTnodes;
4166 + dev->freeTnodes = tn;
4167 + dev->nFreeTnodes++;
4171 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4173 + /* Free the list of allocated tnodes */
4174 + yaffs_TnodeList *tmp;
4176 + while (dev->allocatedTnodeList) {
4177 + tmp = dev->allocatedTnodeList->next;
4179 + YFREE(dev->allocatedTnodeList->tnodes);
4180 + YFREE(dev->allocatedTnodeList);
4181 + dev->allocatedTnodeList = tmp;
4185 + dev->freeTnodes = NULL;
4186 + dev->nFreeTnodes = 0;
4189 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4191 + dev->allocatedTnodeList = NULL;
4192 + dev->freeTnodes = NULL;
4193 + dev->nFreeTnodes = 0;
4194 + dev->nTnodesCreated = 0;
4199 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4201 + __u32 *map = (__u32 *)tn;
4207 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4208 + val >>= dev->chunkGroupBits;
4210 + bitInMap = pos * dev->tnodeWidth;
4211 + wordInMap = bitInMap /32;
4212 + bitInWord = bitInMap & (32 -1);
4214 + mask = dev->tnodeMask << bitInWord;
4216 + map[wordInMap] &= ~mask;
4217 + map[wordInMap] |= (mask & (val << bitInWord));
4219 + if(dev->tnodeWidth > (32-bitInWord)) {
4220 + bitInWord = (32 - bitInWord);
4222 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4223 + map[wordInMap] &= ~mask;
4224 + map[wordInMap] |= (mask & (val >> bitInWord));
4228 +__u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
4230 + __u32 *map = (__u32 *)tn;
4236 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4238 + bitInMap = pos * dev->tnodeWidth;
4239 + wordInMap = bitInMap /32;
4240 + bitInWord = bitInMap & (32 -1);
4242 + val = map[wordInMap] >> bitInWord;
4244 + if(dev->tnodeWidth > (32-bitInWord)) {
4245 + bitInWord = (32 - bitInWord);
4247 + val |= (map[wordInMap] << bitInWord);
4250 + val &= dev->tnodeMask;
4251 + val <<= dev->chunkGroupBits;
4256 +/* ------------------- End of individual tnode manipulation -----------------*/
4258 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
4259 + * The look up tree is represented by the top tnode and the number of topLevel
4260 + * in the tree. 0 means only the level 0 tnode is in the tree.
4263 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
4264 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
4265 + yaffs_FileStructure * fStruct,
4269 + yaffs_Tnode *tn = fStruct->top;
4271 + int requiredTallness;
4272 + int level = fStruct->topLevel;
4274 + /* Check sane level and chunk Id */
4275 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
4279 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
4283 + /* First check we're tall enough (ie enough topLevel) */
4285 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
4286 + requiredTallness = 0;
4288 + i >>= YAFFS_TNODES_INTERNAL_BITS;
4289 + requiredTallness++;
4292 + if (requiredTallness > fStruct->topLevel) {
4293 + /* Not tall enough, so we can't find it, return NULL. */
4297 + /* Traverse down to level 0 */
4298 + while (level > 0 && tn) {
4300 + internal[(chunkId >>
4301 + ( YAFFS_TNODES_LEVEL0_BITS +
4303 + YAFFS_TNODES_INTERNAL_BITS)
4305 + YAFFS_TNODES_INTERNAL_MASK];
4313 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
4314 + * This happens in two steps:
4315 + * 1. If the tree isn't tall enough, then make it taller.
4316 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
4318 + * Used when modifying the tree.
4320 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
4321 + * be plugged into the ttree.
4324 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
4325 + yaffs_FileStructure * fStruct,
4327 + yaffs_Tnode *passedTn)
4330 + int requiredTallness;
4338 + /* Check sane level and page Id */
4339 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
4343 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
4347 + /* First check we're tall enough (ie enough topLevel) */
4349 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
4350 + requiredTallness = 0;
4352 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4353 + requiredTallness++;
4357 + if (requiredTallness > fStruct->topLevel) {
4358 + /* Not tall enough,gotta make the tree taller */
4359 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
4361 + tn = yaffs_GetTnode(dev);
4364 + tn->internal[0] = fStruct->top;
4365 + fStruct->top = tn;
4367 + T(YAFFS_TRACE_ERROR,
4368 + (TSTR("yaffs: no more tnodes" TENDSTR)));
4372 + fStruct->topLevel = requiredTallness;
4375 + /* Traverse down to level 0, adding anything we need */
4377 + l = fStruct->topLevel;
4378 + tn = fStruct->top;
4381 + while (l > 0 && tn) {
4383 + ( YAFFS_TNODES_LEVEL0_BITS +
4384 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
4385 + YAFFS_TNODES_INTERNAL_MASK;
4388 + if((l>1) && !tn->internal[x]){
4389 + /* Add missing non-level-zero tnode */
4390 + tn->internal[x] = yaffs_GetTnode(dev);
4392 + } else if(l == 1) {
4393 + /* Looking from level 1 at level 0 */
4395 + /* If we already have one, then release it.*/
4396 + if(tn->internal[x])
4397 + yaffs_FreeTnode(dev,tn->internal[x]);
4398 + tn->internal[x] = passedTn;
4400 + } else if(!tn->internal[x]) {
4401 + /* Don't have one, none passed in */
4402 + tn->internal[x] = yaffs_GetTnode(dev);
4406 + tn = tn->internal[x];
4410 + /* top is level 0 */
4412 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4413 + yaffs_FreeTnode(dev,passedTn);
4420 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
4421 + yaffs_ExtendedTags * tags, int objectId,
4426 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
4427 + if (yaffs_CheckChunkBit
4428 + (dev, theChunk / dev->nChunksPerBlock,
4429 + theChunk % dev->nChunksPerBlock)) {
4430 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
4432 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
4444 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
4445 + * chunks and tnodes in the file
4446 + * Returns 1 if the tree was deleted.
4447 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
4450 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
4451 + int chunkOffset, int *limit)
4456 + yaffs_ExtendedTags tags;
4458 + yaffs_Device *dev = in->myDev;
4465 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
4467 + if (tn->internal[i]) {
4468 + if (limit && (*limit) < 0) {
4472 + yaffs_DeleteWorker(in,
4480 + YAFFS_TNODES_INTERNAL_BITS)
4485 + yaffs_FreeTnode(dev,
4488 + tn->internal[i] = NULL;
4493 + return (allDone) ? 1 : 0;
4494 + } else if (level == 0) {
4497 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
4499 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4504 + YAFFS_TNODES_LEVEL0_BITS) + i;
4507 + yaffs_FindChunkInGroup(dev,
4513 + if (foundChunk > 0) {
4514 + yaffs_DeleteChunk(dev,
4517 + in->nDataChunks--;
4519 + *limit = *limit - 1;
4520 + if (*limit <= 0) {
4527 + yaffs_PutLevel0Tnode(dev,tn,i,0);
4531 + return (i < 0) ? 1 : 0;
4541 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
4544 + yaffs_BlockInfo *theBlock;
4546 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
4548 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
4550 + theBlock->softDeletions++;
4551 + dev->nFreeChunks++;
4555 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
4556 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
4558 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
4561 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
4562 + __u32 level, int chunkOffset)
4567 + yaffs_Device *dev = in->myDev;
4572 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
4574 + if (tn->internal[i]) {
4576 + yaffs_SoftDeleteWorker(in,
4582 + YAFFS_TNODES_INTERNAL_BITS)
4585 + yaffs_FreeTnode(dev,
4588 + tn->internal[i] = NULL;
4590 + /* Hoosterman... how could this happen? */
4594 + return (allDone) ? 1 : 0;
4595 + } else if (level == 0) {
4597 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
4598 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4600 + /* Note this does not find the real chunk, only the chunk group.
4601 + * We make an assumption that a chunk group is not larger than
4604 + yaffs_SoftDeleteChunk(dev, theChunk);
4605 + yaffs_PutLevel0Tnode(dev,tn,i,0);
4619 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
4621 + if (obj->deleted &&
4622 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
4623 + if (obj->nDataChunks <= 0) {
4624 + /* Empty file with no duplicate object headers, just delete it immediately */
4625 + yaffs_FreeTnode(obj->myDev,
4626 + obj->variant.fileVariant.top);
4627 + obj->variant.fileVariant.top = NULL;
4628 + T(YAFFS_TRACE_TRACING,
4629 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
4631 + yaffs_DoGenericObjectDeletion(obj);
4633 + yaffs_SoftDeleteWorker(obj,
4634 + obj->variant.fileVariant.top,
4635 + obj->variant.fileVariant.
4637 + obj->softDeleted = 1;
4642 +/* Pruning removes any part of the file structure tree that is beyond the
4643 + * bounds of the file (ie that does not point to chunks).
4645 + * A file should only get pruned when its size is reduced.
4647 + * Before pruning, the chunks must be pulled from the tree and the
4648 + * level 0 tnode entries must be zeroed out.
4649 + * Could also use this for file deletion, but that's probably better handled
4650 + * by a special case.
4653 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
4654 + __u32 level, int del0)
4662 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
4663 + if (tn->internal[i] && level > 0) {
4665 + yaffs_PruneWorker(dev, tn->internal[i],
4667 + (i == 0) ? del0 : 1);
4670 + if (tn->internal[i]) {
4675 + if (hasData == 0 && del0) {
4676 + /* Free and return NULL */
4678 + yaffs_FreeTnode(dev, tn);
4688 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
4689 + yaffs_FileStructure * fStruct)
4696 + if (fStruct->topLevel > 0) {
4698 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
4700 + /* Now we have a tree with all the non-zero branches NULL but the height
4701 + * is the same as it was.
4702 + * Let's see if we can trim internal tnodes to shorten the tree.
4703 + * We can do this if only the 0th element in the tnode is in use
4704 + * (ie all the non-zero are NULL)
4707 + while (fStruct->topLevel && !done) {
4708 + tn = fStruct->top;
4711 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
4712 + if (tn->internal[i]) {
4718 + fStruct->top = tn->internal[0];
4719 + fStruct->topLevel--;
4720 + yaffs_FreeTnode(dev, tn);
4730 +/*-------------------- End of File Structure functions.-------------------*/
4732 +/* yaffs_CreateFreeObjects creates a bunch more objects and
4733 + * adds them to the object free list.
4735 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
4738 + yaffs_Object *newObjects;
4739 + yaffs_ObjectList *list;
4744 + /* make these things */
4745 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
4747 + if (!newObjects) {
4748 + T(YAFFS_TRACE_ALLOCATE,
4749 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
4750 + return YAFFS_FAIL;
4753 + /* Hook them into the free list */
4754 + for (i = 0; i < nObjects - 1; i++) {
4755 + newObjects[i].siblings.next =
4756 + (struct list_head *)(&newObjects[i + 1]);
4759 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
4760 + dev->freeObjects = newObjects;
4761 + dev->nFreeObjects += nObjects;
4762 + dev->nObjectsCreated += nObjects;
4764 + /* Now add this bunch of Objects to a list for freeing up. */
4766 + list = YMALLOC(sizeof(yaffs_ObjectList));
4768 + T(YAFFS_TRACE_ALLOCATE,
4769 + (TSTR("Could not add objects to management list" TENDSTR)));
4771 + list->objects = newObjects;
4772 + list->next = dev->allocatedObjectList;
4773 + dev->allocatedObjectList = list;
4780 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
4781 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
4783 + yaffs_Object *tn = NULL;
4785 + /* If there are none left make more */
4786 + if (!dev->freeObjects) {
4787 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
4790 + if (dev->freeObjects) {
4791 + tn = dev->freeObjects;
4792 + dev->freeObjects =
4793 + (yaffs_Object *) (dev->freeObjects->siblings.next);
4794 + dev->nFreeObjects--;
4796 + /* Now sweeten it up... */
4798 + memset(tn, 0, sizeof(yaffs_Object));
4801 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
4802 + INIT_LIST_HEAD(&(tn->hardLinks));
4803 + INIT_LIST_HEAD(&(tn->hashLink));
4804 + INIT_LIST_HEAD(&tn->siblings);
4806 + /* Add it to the lost and found directory.
4807 + * NB Can't put root or lostNFound in lostNFound so
4808 + * check if lostNFound exists first
4810 + if (dev->lostNFoundDir) {
4811 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
4818 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
4822 + yaffs_Object *obj =
4823 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
4825 + obj->fake = 1; /* it is fake so it has no NAND presence... */
4826 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
4827 + obj->unlinkAllowed = 0; /* ... or unlink it */
4829 + obj->unlinked = 0;
4830 + obj->yst_mode = mode;
4832 + obj->chunkId = 0; /* Not a valid chunk. */
4839 +static void yaffs_UnhashObject(yaffs_Object * tn)
4842 + yaffs_Device *dev = tn->myDev;
4844 + /* If it is still linked into the bucket list, free from the list */
4845 + if (!list_empty(&tn->hashLink)) {
4846 + list_del_init(&tn->hashLink);
4847 + bucket = yaffs_HashFunction(tn->objectId);
4848 + dev->objectBucket[bucket].count--;
4853 +/* FreeObject frees up a Object and puts it back on the free list */
4854 +static void yaffs_FreeObject(yaffs_Object * tn)
4857 + yaffs_Device *dev = tn->myDev;
4860 + if (tn->myInode) {
4861 + /* We're still hooked up to a cached inode.
4862 + * Don't delete now, but mark for later deletion
4864 + tn->deferedFree = 1;
4869 + yaffs_UnhashObject(tn);
4871 + /* Link into the free list. */
4872 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
4873 + dev->freeObjects = tn;
4874 + dev->nFreeObjects++;
4879 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
4881 + if (obj->deferedFree) {
4882 + yaffs_FreeObject(obj);
4888 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
4890 + /* Free the list of allocated Objects */
4892 + yaffs_ObjectList *tmp;
4894 + while (dev->allocatedObjectList) {
4895 + tmp = dev->allocatedObjectList->next;
4896 + YFREE(dev->allocatedObjectList->objects);
4897 + YFREE(dev->allocatedObjectList);
4899 + dev->allocatedObjectList = tmp;
4902 + dev->freeObjects = NULL;
4903 + dev->nFreeObjects = 0;
4906 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
4910 + dev->allocatedObjectList = NULL;
4911 + dev->freeObjects = NULL;
4912 + dev->nFreeObjects = 0;
4914 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4915 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
4916 + dev->objectBucket[i].count = 0;
4921 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
4926 + int lowest = 999999;
4928 + /* First let's see if we can find one that's empty. */
4930 + for (i = 0; i < 10 && lowest > 0; i++) {
4932 + x %= YAFFS_NOBJECT_BUCKETS;
4933 + if (dev->objectBucket[x].count < lowest) {
4934 + lowest = dev->objectBucket[x].count;
4940 + /* If we didn't find an empty list, then try
4941 + * looking a bit further for a short one
4944 + for (i = 0; i < 10 && lowest > 3; i++) {
4946 + x %= YAFFS_NOBJECT_BUCKETS;
4947 + if (dev->objectBucket[x].count < lowest) {
4948 + lowest = dev->objectBucket[x].count;
4957 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
4959 + int bucket = yaffs_FindNiceObjectBucket(dev);
4961 + /* Now find an object value that has not already been taken
4962 + * by scanning the list.
4966 + struct list_head *i;
4968 + __u32 n = (__u32) bucket;
4970 + /* yaffs_CheckObjectHashSanity(); */
4974 + n += YAFFS_NOBJECT_BUCKETS;
4975 + if (1 || dev->objectBucket[bucket].count > 0) {
4976 + list_for_each(i, &dev->objectBucket[bucket].list) {
4977 + /* If there is already one in the list */
4979 + && list_entry(i, yaffs_Object,
4980 + hashLink)->objectId == n) {
4991 +static void yaffs_HashObject(yaffs_Object * in)
4993 + int bucket = yaffs_HashFunction(in->objectId);
4994 + yaffs_Device *dev = in->myDev;
4996 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
4997 + dev->objectBucket[bucket].count++;
5001 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5003 + int bucket = yaffs_HashFunction(number);
5004 + struct list_head *i;
5007 + list_for_each(i, &dev->objectBucket[bucket].list) {
5008 + /* Look if it is in the list */
5010 + in = list_entry(i, yaffs_Object, hashLink);
5011 + if (in->objectId == number) {
5013 + /* Don't tell the VFS about this one if it is defered free */
5014 + if (in->deferedFree)
5026 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5027 + yaffs_ObjectType type)
5030 + yaffs_Object *theObject;
5033 + number = yaffs_CreateNewObjectNumber(dev);
5036 + theObject = yaffs_AllocateEmptyObject(dev);
5039 + theObject->fake = 0;
5040 + theObject->renameAllowed = 1;
5041 + theObject->unlinkAllowed = 1;
5042 + theObject->objectId = number;
5043 + yaffs_HashObject(theObject);
5044 + theObject->variantType = type;
5045 +#ifdef CONFIG_YAFFS_WINCE
5046 + yfsd_WinFileTimeNow(theObject->win_atime);
5047 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5048 + theObject->win_atime[0];
5049 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5050 + theObject->win_atime[1];
5054 + theObject->yst_atime = theObject->yst_mtime =
5055 + theObject->yst_ctime = Y_CURRENT_TIME;
5058 + case YAFFS_OBJECT_TYPE_FILE:
5059 + theObject->variant.fileVariant.fileSize = 0;
5060 + theObject->variant.fileVariant.scannedFileSize = 0;
5061 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5062 + theObject->variant.fileVariant.topLevel = 0;
5063 + theObject->variant.fileVariant.top =
5064 + yaffs_GetTnode(dev);
5066 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5067 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5070 + case YAFFS_OBJECT_TYPE_SYMLINK:
5071 + case YAFFS_OBJECT_TYPE_HARDLINK:
5072 + case YAFFS_OBJECT_TYPE_SPECIAL:
5073 + /* No action required */
5075 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5076 + /* todo this should not happen */
5084 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5086 + yaffs_ObjectType type)
5088 + yaffs_Object *theObject = NULL;
5091 + theObject = yaffs_FindObjectByNumber(dev, number);
5095 + theObject = yaffs_CreateNewObject(dev, number, type);
5103 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5105 + YCHAR *newStr = NULL;
5107 + if (str && *str) {
5108 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5109 + yaffs_strcpy(newStr, str);
5117 + * Mknod (create) a new object.
5118 + * equivalentObject only has meaning for a hard link;
5119 + * aliasString only has meaning for a sumlink.
5120 + * rdev only has meaning for devices (a subset of special objects)
5123 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5124 + yaffs_Object * parent,
5125 + const YCHAR * name,
5129 + yaffs_Object * equivalentObject,
5130 + const YCHAR * aliasString, __u32 rdev)
5134 + yaffs_Device *dev = parent->myDev;
5136 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5137 + if (yaffs_FindObjectByName(parent, name)) {
5141 + in = yaffs_CreateNewObject(dev, -1, type);
5146 + in->variantType = type;
5148 + in->yst_mode = mode;
5150 +#ifdef CONFIG_YAFFS_WINCE
5151 + yfsd_WinFileTimeNow(in->win_atime);
5152 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5153 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5156 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5158 + in->yst_rdev = rdev;
5159 + in->yst_uid = uid;
5160 + in->yst_gid = gid;
5162 + in->nDataChunks = 0;
5164 + yaffs_SetObjectName(in, name);
5167 + yaffs_AddObjectToDirectory(parent, in);
5169 + in->myDev = parent->myDev;
5172 + case YAFFS_OBJECT_TYPE_SYMLINK:
5173 + in->variant.symLinkVariant.alias =
5174 + yaffs_CloneString(aliasString);
5176 + case YAFFS_OBJECT_TYPE_HARDLINK:
5177 + in->variant.hardLinkVariant.equivalentObject =
5179 + in->variant.hardLinkVariant.equivalentObjectId =
5180 + equivalentObject->objectId;
5181 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5183 + case YAFFS_OBJECT_TYPE_FILE:
5184 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5185 + case YAFFS_OBJECT_TYPE_SPECIAL:
5186 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5191 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5192 + /* Could not create the object header, fail the creation */
5193 + yaffs_DestroyObject(in);
5202 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5203 + __u32 mode, __u32 uid, __u32 gid)
5205 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
5206 + uid, gid, NULL, NULL, 0);
5209 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
5210 + __u32 mode, __u32 uid, __u32 gid)
5212 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
5213 + mode, uid, gid, NULL, NULL, 0);
5216 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
5217 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
5219 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
5220 + uid, gid, NULL, NULL, rdev);
5223 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
5224 + __u32 mode, __u32 uid, __u32 gid,
5225 + const YCHAR * alias)
5227 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
5228 + uid, gid, NULL, alias, 0);
5231 +/* yaffs_Link returns the object id of the equivalent object.*/
5232 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
5233 + yaffs_Object * equivalentObject)
5235 + /* Get the real object in case we were fed a hard link as an equivalent object */
5236 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
5238 + if (yaffs_MknodObject
5239 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
5240 + equivalentObject, NULL, 0)) {
5241 + return equivalentObject;
5248 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
5249 + const YCHAR * newName, int force, int shadows)
5254 + yaffs_Object *existingTarget;
5256 + if (newDir == NULL) {
5257 + newDir = obj->parent; /* use the old directory */
5260 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5261 + T(YAFFS_TRACE_ALWAYS,
5263 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
5268 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
5269 + if (obj->myDev->isYaffs2) {
5270 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
5272 + unlinkOp = (newDir == obj->myDev->unlinkedDir
5273 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
5276 + deleteOp = (newDir == obj->myDev->deletedDir);
5278 + existingTarget = yaffs_FindObjectByName(newDir, newName);
5280 + /* If the object is a file going into the unlinked directory,
5281 + * then it is OK to just stuff it in since duplicate names are allowed.
5282 + * else only proceed if the new name does not exist and if we're putting
5283 + * it into a directory.
5289 + !existingTarget) &&
5290 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
5291 + yaffs_SetObjectName(obj, newName);
5294 + yaffs_AddObjectToDirectory(newDir, obj);
5297 + obj->unlinked = 1;
5299 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
5300 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
5304 + return YAFFS_FAIL;
5307 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
5308 + yaffs_Object * newDir, const YCHAR * newName)
5310 + yaffs_Object *obj;
5311 + yaffs_Object *existingTarget;
5314 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
5315 + /* Special case for case insemsitive systems (eg. WinCE).
5316 + * While look-up is case insensitive, the name isn't.
5317 + * Therefore we might want to change x.txt to X.txt
5319 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
5324 + obj = yaffs_FindObjectByName(oldDir, oldName);
5325 + /* Check new name to long. */
5326 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
5327 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
5328 + /* ENAMETOOLONG */
5329 + return YAFFS_FAIL;
5330 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
5331 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
5332 + /* ENAMETOOLONG */
5333 + return YAFFS_FAIL;
5335 + if (obj && obj->renameAllowed) {
5337 + /* Now do the handling for an existing target, if there is one */
5339 + existingTarget = yaffs_FindObjectByName(newDir, newName);
5340 + if (existingTarget &&
5341 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
5342 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
5343 + /* There is a target that is a non-empty directory, so we fail */
5344 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
5345 + } else if (existingTarget && existingTarget != obj) {
5346 + /* Nuke the target first, using shadowing,
5347 + * but only if it isn't the same object
5349 + yaffs_ChangeObjectName(obj, newDir, newName, force,
5350 + existingTarget->objectId);
5351 + yaffs_UnlinkObject(existingTarget);
5354 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
5356 + return YAFFS_FAIL;
5359 +/*------------------------- Block Management and Page Allocation ----------------*/
5361 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
5363 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5365 + dev->allocationBlock = -1; /* force it to get a new one */
5367 + /* Todo we're assuming the malloc will pass. */
5368 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
5369 + if(!dev->blockInfo){
5370 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
5371 + dev->blockInfoAlt = 1;
5374 + dev->blockInfoAlt = 0;
5376 + /* Set up dynamic blockinfo stuff. */
5377 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
5378 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
5379 + if(!dev->chunkBits){
5380 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
5381 + dev->chunkBitsAlt = 1;
5384 + dev->chunkBitsAlt = 0;
5386 + if (dev->blockInfo && dev->chunkBits) {
5387 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
5388 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
5392 + return YAFFS_FAIL;
5396 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
5398 + if(dev->blockInfoAlt)
5399 + YFREE_ALT(dev->blockInfo);
5401 + YFREE(dev->blockInfo);
5402 + dev->blockInfoAlt = 0;
5404 + dev->blockInfo = NULL;
5406 + if(dev->chunkBitsAlt)
5407 + YFREE_ALT(dev->chunkBits);
5409 + YFREE(dev->chunkBits);
5410 + dev->chunkBitsAlt = 0;
5411 + dev->chunkBits = NULL;
5414 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
5415 + yaffs_BlockInfo * bi)
5419 + yaffs_BlockInfo *b;
5421 + if (!dev->isYaffs2)
5422 + return 1; /* disqualification only applies to yaffs2. */
5424 + if (!bi->hasShrinkHeader)
5425 + return 1; /* can gc */
5427 + /* Find the oldest dirty sequence number if we don't know it and save it
5428 + * so we don't have to keep recomputing it.
5430 + if (!dev->oldestDirtySequence) {
5431 + seq = dev->sequenceNumber;
5433 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
5435 + b = yaffs_GetBlockInfo(dev, i);
5436 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
5437 + (b->pagesInUse - b->softDeletions) <
5438 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
5439 + seq = b->sequenceNumber;
5442 + dev->oldestDirtySequence = seq;
5445 + /* Can't do gc of this block if there are any blocks older than this one that have
5446 + * discarded pages.
5448 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
5452 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
5453 + * for garbage collection.
5456 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
5460 + int b = dev->currentDirtyChecker;
5464 + int dirtiest = -1;
5466 + int prioritised=0;
5467 + yaffs_BlockInfo *bi;
5468 + static int nonAggressiveSkip = 0;
5469 + int pendingPrioritisedExist = 0;
5471 + /* First let's see if we need to grab a prioritised block */
5472 + if(dev->hasPendingPrioritisedGCs){
5473 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
5475 + bi = yaffs_GetBlockInfo(dev, i);
5476 + if(bi->gcPrioritise) {
5477 + pendingPrioritisedExist = 1;
5478 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
5479 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
5480 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
5483 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
5488 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
5489 + dev->hasPendingPrioritisedGCs = 0;
5492 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
5494 + * else (we're doing a leasurely gc), then we only bother to do this if the
5495 + * block has only a few pages in use.
5498 + nonAggressiveSkip--;
5500 + if (!aggressive && (nonAggressiveSkip > 0)) {
5506 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
5510 + dev->internalEndBlock - dev->internalStartBlock + 1;
5513 + dev->internalEndBlock - dev->internalStartBlock + 1;
5514 + iterations = iterations / 16;
5515 + if (iterations > 200) {
5520 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
5522 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
5523 + b = dev->internalStartBlock;
5526 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
5527 + T(YAFFS_TRACE_ERROR,
5528 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
5532 + bi = yaffs_GetBlockInfo(dev, b);
5535 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
5542 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
5543 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
5544 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
5546 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
5550 + dev->currentDirtyChecker = b;
5552 + if (dirtiest > 0) {
5554 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
5555 + dev->nChunksPerBlock - pagesInUse,prioritised));
5558 + dev->oldestDirtySequence = 0;
5560 + if (dirtiest > 0) {
5561 + nonAggressiveSkip = 4;
5567 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
5569 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
5573 + /* If the block is still healthy erase it and mark as clean.
5574 + * If the block has had a data failure, then retire it.
5577 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
5578 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
5579 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
5581 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
5583 + if (!bi->needsRetiring) {
5584 + yaffs_InvalidateCheckpoint(dev);
5585 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
5587 + dev->nErasureFailures++;
5588 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5589 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
5593 + if (erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE)) {
5595 + for (i = 0; i < dev->nChunksPerBlock; i++) {
5596 + if (!yaffs_CheckChunkErased
5597 + (dev, blockNo * dev->nChunksPerBlock + i)) {
5598 + T(YAFFS_TRACE_ERROR,
5600 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
5601 + TENDSTR), blockNo, i));
5607 + /* Clean it up... */
5608 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
5609 + dev->nErasedBlocks++;
5610 + bi->pagesInUse = 0;
5611 + bi->softDeletions = 0;
5612 + bi->hasShrinkHeader = 0;
5613 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
5614 + bi->gcPrioritise = 0;
5615 + yaffs_ClearChunkBits(dev, blockNo);
5617 + T(YAFFS_TRACE_ERASE,
5618 + (TSTR("Erased block %d" TENDSTR), blockNo));
5620 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
5622 + yaffs_RetireBlock(dev, blockNo);
5623 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5624 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
5628 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
5632 + yaffs_BlockInfo *bi;
5634 + if (dev->nErasedBlocks < 1) {
5635 + /* Hoosterman we've got a problem.
5636 + * Can't get space to gc
5638 + T(YAFFS_TRACE_ERROR,
5639 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
5644 + /* Find an empty block. */
5646 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
5647 + dev->allocationBlockFinder++;
5648 + if (dev->allocationBlockFinder < dev->internalStartBlock
5649 + || dev->allocationBlockFinder > dev->internalEndBlock) {
5650 + dev->allocationBlockFinder = dev->internalStartBlock;
5653 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
5655 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
5656 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
5657 + dev->sequenceNumber++;
5658 + bi->sequenceNumber = dev->sequenceNumber;
5659 + dev->nErasedBlocks--;
5660 + T(YAFFS_TRACE_ALLOCATE,
5661 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
5662 + dev->allocationBlockFinder, dev->sequenceNumber,
5663 + dev->nErasedBlocks));
5664 + return dev->allocationBlockFinder;
5668 + T(YAFFS_TRACE_ALWAYS,
5670 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
5671 + TENDSTR), dev->nErasedBlocks));
5677 +// Check if there's space to allocate...
5678 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
5679 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
5681 + int reservedChunks;
5682 + int reservedBlocks = dev->nReservedBlocks;
5683 + int checkpointBlocks;
5685 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
5686 + if(checkpointBlocks < 0)
5687 + checkpointBlocks = 0;
5689 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
5691 + return (dev->nFreeChunks > reservedChunks);
5694 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
5697 + yaffs_BlockInfo *bi;
5699 + if (dev->allocationBlock < 0) {
5700 + /* Get next block to allocate off */
5701 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
5702 + dev->allocationPage = 0;
5705 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
5706 + /* Not enough space to allocate unless we're allowed to use the reserve. */
5710 + if (dev->nErasedBlocks < dev->nReservedBlocks
5711 + && dev->allocationPage == 0) {
5712 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
5715 + /* Next page please.... */
5716 + if (dev->allocationBlock >= 0) {
5717 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
5719 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
5720 + dev->allocationPage;
5722 + yaffs_SetChunkBit(dev, dev->allocationBlock,
5723 + dev->allocationPage);
5725 + dev->allocationPage++;
5727 + dev->nFreeChunks--;
5729 + /* If the block is full set the state to full */
5730 + if (dev->allocationPage >= dev->nChunksPerBlock) {
5731 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
5732 + dev->allocationBlock = -1;
5736 + *blockUsedPtr = bi;
5741 + T(YAFFS_TRACE_ERROR,
5742 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
5747 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
5751 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
5753 + if (dev->allocationBlock > 0) {
5754 + n += (dev->nChunksPerBlock - dev->allocationPage);
5761 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
5767 + int retVal = YAFFS_OK;
5770 + int isCheckpointBlock;
5772 + int chunksBefore = yaffs_GetErasedChunks(dev);
5775 + yaffs_ExtendedTags tags;
5777 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
5779 + yaffs_Object *object;
5781 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
5783 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
5785 + T(YAFFS_TRACE_TRACING,
5786 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
5787 + bi->pagesInUse, bi->hasShrinkHeader));
5789 + /*yaffs_VerifyFreeChunks(dev); */
5791 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
5793 + /* Take off the number of soft deleted entries because
5794 + * they're going to get really deleted during GC.
5796 + dev->nFreeChunks -= bi->softDeletions;
5798 + dev->isDoingGC = 1;
5800 + if (isCheckpointBlock ||
5801 + !yaffs_StillSomeChunkBits(dev, block)) {
5802 + T(YAFFS_TRACE_TRACING,
5804 + ("Collecting block %d that has no chunks in use" TENDSTR),
5806 + yaffs_BlockBecameDirty(dev, block);
5809 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
5811 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
5812 + chunkInBlock < dev->nChunksPerBlock
5813 + && yaffs_StillSomeChunkBits(dev, block);
5814 + chunkInBlock++, oldChunk++) {
5815 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
5817 + /* This page is in use and might need to be copied off */
5821 + yaffs_InitialiseTags(&tags);
5823 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
5827 + yaffs_FindObjectByNumber(dev,
5830 + T(YAFFS_TRACE_GC_DETAIL,
5832 + ("Collecting page %d, %d %d %d " TENDSTR),
5833 + chunkInBlock, tags.objectId, tags.chunkId,
5837 + T(YAFFS_TRACE_ERROR,
5839 + ("page %d in gc has no object "
5840 + TENDSTR), oldChunk));
5843 + if (object && object->deleted
5844 + && tags.chunkId != 0) {
5845 + /* Data chunk in a deleted file, throw it away
5846 + * It's a soft deleted data chunk,
5847 + * No need to copy this, just forget about it and
5848 + * fix up the object.
5851 + object->nDataChunks--;
5853 + if (object->nDataChunks <= 0) {
5854 + /* remeber to clean up the object */
5855 + dev->gcCleanupList[cleanups] =
5861 + /* Todo object && object->deleted && object->nDataChunks == 0 */
5863 + /* Deleted object header with no data chunks.
5864 + * Can be discarded and the file deleted.
5866 + object->chunkId = 0;
5867 + yaffs_FreeTnode(object->myDev,
5870 + object->variant.fileVariant.top = NULL;
5871 + yaffs_DoGenericObjectDeletion(object);
5873 + } else if (object) {
5874 + /* It's either a data chunk in a live file or
5875 + * an ObjectHeader, so we're interested in it.
5876 + * NB Need to keep the ObjectHeaders of deleted files
5877 + * until the whole file has been deleted off
5879 + tags.serialNumber++;
5883 + if (tags.chunkId == 0) {
5884 + /* It is an object Id,
5885 + * We need to nuke the shrinkheader flags first
5886 + * We no longer want the shrinkHeader flag since its work is done
5887 + * and if it is left in place it will mess up scanning.
5888 + * Also, clear out any shadowing stuff
5891 + yaffs_ObjectHeader *oh;
5892 + oh = (yaffs_ObjectHeader *)buffer;
5894 + oh->shadowsObject = -1;
5895 + tags.extraShadows = 0;
5896 + tags.extraIsShrinkHeader = 0;
5900 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
5902 + if (newChunk < 0) {
5903 + retVal = YAFFS_FAIL;
5906 + /* Ok, now fix up the Tnodes etc. */
5908 + if (tags.chunkId == 0) {
5909 + /* It's a header */
5910 + object->chunkId = newChunk;
5911 + object->serial = tags.serialNumber;
5913 + /* It's a data chunk */
5914 + yaffs_PutChunkIntoFile
5922 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
5927 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
5930 + /* Do any required cleanups */
5931 + for (i = 0; i < cleanups; i++) {
5932 + /* Time to delete the file too */
5934 + yaffs_FindObjectByNumber(dev,
5935 + dev->gcCleanupList[i]);
5937 + yaffs_FreeTnode(dev,
5938 + object->variant.fileVariant.
5940 + object->variant.fileVariant.top = NULL;
5943 + ("yaffs: About to finally delete object %d"
5944 + TENDSTR), object->objectId));
5945 + yaffs_DoGenericObjectDeletion(object);
5946 + object->myDev->nDeletedFiles--;
5953 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
5956 + ("gc did not increase free chunks before %d after %d"
5957 + TENDSTR), chunksBefore, chunksAfter));
5960 + dev->isDoingGC = 0;
5965 +/* New garbage collector
5966 + * If we're very low on erased blocks then we do aggressive garbage collection
5967 + * otherwise we do "leasurely" garbage collection.
5968 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
5969 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
5971 + * The idea is to help clear out space in a more spread-out manner.
5972 + * Dunno if it really does anything useful.
5974 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
5978 + int gcOk = YAFFS_OK;
5981 + int checkpointBlockAdjust;
5983 + if (dev->isDoingGC) {
5984 + /* Bail out so we don't get recursive gc */
5988 + /* This loop should pass the first time.
5989 + * We'll only see looping here if the erase of the collected block fails.
5995 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
5996 + if(checkpointBlockAdjust < 0)
5997 + checkpointBlockAdjust = 0;
5999 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust)) {
6000 + /* We need a block soon...*/
6003 + /* We're in no hurry */
6007 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6010 + dev->garbageCollections++;
6011 + if (!aggressive) {
6012 + dev->passiveGarbageCollections++;
6017 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6018 + dev->nErasedBlocks, aggressive));
6020 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6023 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6026 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6027 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6029 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6030 + && (maxTries < 2));
6032 + return aggressive ? gcOk : YAFFS_OK;
6035 +/*------------------------- TAGS --------------------------------*/
6037 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6038 + int chunkInObject)
6040 + return (tags->chunkId == chunkInObject &&
6041 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6046 +/*-------------------- Data file manipulation -----------------*/
6048 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6049 + yaffs_ExtendedTags * tags)
6051 + /*Get the Tnode, then get the level 0 offset chunk offset */
6053 + int theChunk = -1;
6054 + yaffs_ExtendedTags localTags;
6057 + yaffs_Device *dev = in->myDev;
6060 + /* Passed a NULL, so use our own tags space */
6061 + tags = &localTags;
6064 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6067 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6070 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6076 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6077 + yaffs_ExtendedTags * tags)
6079 + /* Get the Tnode, then get the level 0 offset chunk offset */
6081 + int theChunk = -1;
6082 + yaffs_ExtendedTags localTags;
6084 + yaffs_Device *dev = in->myDev;
6088 + /* Passed a NULL, so use our own tags space */
6089 + tags = &localTags;
6092 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6096 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6099 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6102 + /* Delete the entry in the filestructure (if found) */
6103 + if (retVal != -1) {
6104 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6107 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6110 + if (retVal == -1) {
6111 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6116 +#ifdef YAFFS_PARANOID
6118 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6126 + yaffs_Tags localTags;
6127 + yaffs_Tags *tags = &localTags;
6131 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6132 + /* T(("Object not a file\n")); */
6133 + return YAFFS_FAIL;
6136 + objId = in->objectId;
6137 + fSize = in->variant.fileVariant.fileSize;
6139 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6141 + for (chunk = 1; chunk <= nChunks; chunk++) {
6142 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6147 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6149 + if (yaffs_CheckChunkBits
6150 + (dev, theChunk / dev->nChunksPerBlock,
6151 + theChunk % dev->nChunksPerBlock)) {
6153 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6156 + if (yaffs_TagsMatch
6157 + (tags, in->objectId, chunk, chunkDeleted)) {
6167 + /* T(("No level 0 found for %d\n", chunk)); */
6171 + return failed ? YAFFS_FAIL : YAFFS_OK;
6176 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
6177 + int chunkInNAND, int inScan)
6179 + /* NB inScan is zero unless scanning.
6180 + * For forward scanning, inScan is > 0;
6181 + * for backward scanning inScan is < 0
6185 + yaffs_Device *dev = in->myDev;
6186 + int existingChunk;
6187 + yaffs_ExtendedTags existingTags;
6188 + yaffs_ExtendedTags newTags;
6189 + unsigned existingSerial, newSerial;
6191 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6192 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
6193 + * If it is not during Scanning then something went wrong!
6196 + T(YAFFS_TRACE_ERROR,
6198 + ("yaffs tragedy:attempt to put data chunk into a non-file"
6203 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
6207 + tn = yaffs_AddOrFindLevel0Tnode(dev,
6208 + &in->variant.fileVariant,
6212 + return YAFFS_FAIL;
6215 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6217 + if (inScan != 0) {
6218 + /* If we're scanning then we need to test for duplicates
6219 + * NB This does not need to be efficient since it should only ever
6220 + * happen when the power fails during a write, then only one
6221 + * chunk should ever be affected.
6223 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
6224 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
6227 + if (existingChunk != 0) {
6228 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
6229 + * thus we have to do a FindChunkInFile to get the real chunk id.
6231 + * We have a duplicate now we need to decide which one to use:
6233 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
6234 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
6235 + * YAFFS1: Get both sets of tags and compare serial numbers.
6239 + /* Only do this for forward scanning */
6240 + yaffs_ReadChunkWithTagsFromNAND(dev,
6244 + /* Do a proper find */
6246 + yaffs_FindChunkInFile(in, chunkInInode,
6250 + if (existingChunk <= 0) {
6251 + /*Hoosterman - how did this happen? */
6253 + T(YAFFS_TRACE_ERROR,
6255 + ("yaffs tragedy: existing chunk < 0 in scan"
6260 + /* NB The deleted flags should be false, otherwise the chunks will
6261 + * not be loaded during a scan
6264 + newSerial = newTags.serialNumber;
6265 + existingSerial = existingTags.serialNumber;
6267 + if ((inScan > 0) &&
6268 + (in->myDev->isYaffs2 ||
6269 + existingChunk <= 0 ||
6270 + ((existingSerial + 1) & 3) == newSerial)) {
6271 + /* Forward scanning.
6273 + * Delete the old one and drop through to update the tnode
6275 + yaffs_DeleteChunk(dev, existingChunk, 1,
6278 + /* Backward scanning or we want to use the existing one
6280 + * Delete the new one and return early so that the tnode isn't changed
6282 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
6290 + if (existingChunk == 0) {
6291 + in->nDataChunks++;
6294 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
6299 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
6302 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
6304 + if (chunkInNAND >= 0) {
6305 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
6308 + T(YAFFS_TRACE_NANDACCESS,
6309 + (TSTR("Chunk %d not found zero instead" TENDSTR),
6311 + /* get sane (zero) data if you read a hole */
6312 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
6318 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
6322 + yaffs_ExtendedTags tags;
6323 + yaffs_BlockInfo *bi;
6328 + dev->nDeletions++;
6329 + block = chunkId / dev->nChunksPerBlock;
6330 + page = chunkId % dev->nChunksPerBlock;
6332 + bi = yaffs_GetBlockInfo(dev, block);
6334 + T(YAFFS_TRACE_DELETION,
6335 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
6338 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
6340 + yaffs_InitialiseTags(&tags);
6342 + tags.chunkDeleted = 1;
6344 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
6345 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
6347 + dev->nUnmarkedDeletions++;
6350 + /* Pull out of the management area.
6351 + * If the whole block became dirty, this will kick off an erasure.
6353 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
6354 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
6355 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6356 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
6357 + dev->nFreeChunks++;
6359 + yaffs_ClearChunkBit(dev, block, page);
6363 + if (bi->pagesInUse == 0 &&
6364 + !bi->hasShrinkHeader &&
6365 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
6366 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6367 + yaffs_BlockBecameDirty(dev, block);
6371 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
6376 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
6377 + const __u8 * buffer, int nBytes,
6380 + /* Find old chunk Need to do this to get serial number
6381 + * Write new one and patch into tree.
6382 + * Invalidate old tags.
6386 + yaffs_ExtendedTags prevTags;
6389 + yaffs_ExtendedTags newTags;
6391 + yaffs_Device *dev = in->myDev;
6393 + yaffs_CheckGarbageCollection(dev);
6395 + /* Get the previous chunk at this location in the file if it exists */
6396 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
6398 + /* Set up new tags */
6399 + yaffs_InitialiseTags(&newTags);
6401 + newTags.chunkId = chunkInInode;
6402 + newTags.objectId = in->objectId;
6403 + newTags.serialNumber =
6404 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
6405 + newTags.byteCount = nBytes;
6408 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
6411 + if (newChunkId >= 0) {
6412 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
6414 + if (prevChunkId >= 0) {
6415 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
6419 + yaffs_CheckFileSanity(in);
6421 + return newChunkId;
6425 +/* UpdateObjectHeader updates the header on NAND for an object.
6426 + * If name is not NULL, then that new name is used.
6428 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
6429 + int isShrink, int shadows)
6432 + yaffs_BlockInfo *bi;
6434 + yaffs_Device *dev = in->myDev;
6441 + yaffs_ExtendedTags newTags;
6443 + __u8 *buffer = NULL;
6444 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
6446 + yaffs_ObjectHeader *oh = NULL;
6448 + if (!in->fake || force) {
6450 + yaffs_CheckGarbageCollection(dev);
6452 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
6453 + oh = (yaffs_ObjectHeader *) buffer;
6455 + prevChunkId = in->chunkId;
6457 + if (prevChunkId >= 0) {
6458 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
6460 + memcpy(oldName, oh->name, sizeof(oh->name));
6463 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
6465 + oh->type = in->variantType;
6466 + oh->yst_mode = in->yst_mode;
6467 + oh->shadowsObject = shadows;
6469 +#ifdef CONFIG_YAFFS_WINCE
6470 + oh->win_atime[0] = in->win_atime[0];
6471 + oh->win_ctime[0] = in->win_ctime[0];
6472 + oh->win_mtime[0] = in->win_mtime[0];
6473 + oh->win_atime[1] = in->win_atime[1];
6474 + oh->win_ctime[1] = in->win_ctime[1];
6475 + oh->win_mtime[1] = in->win_mtime[1];
6477 + oh->yst_uid = in->yst_uid;
6478 + oh->yst_gid = in->yst_gid;
6479 + oh->yst_atime = in->yst_atime;
6480 + oh->yst_mtime = in->yst_mtime;
6481 + oh->yst_ctime = in->yst_ctime;
6482 + oh->yst_rdev = in->yst_rdev;
6485 + oh->parentObjectId = in->parent->objectId;
6487 + oh->parentObjectId = 0;
6490 + if (name && *name) {
6491 + memset(oh->name, 0, sizeof(oh->name));
6492 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
6493 + } else if (prevChunkId) {
6494 + memcpy(oh->name, oldName, sizeof(oh->name));
6496 + memset(oh->name, 0, sizeof(oh->name));
6499 + oh->isShrink = isShrink;
6501 + switch (in->variantType) {
6502 + case YAFFS_OBJECT_TYPE_UNKNOWN:
6503 + /* Should not happen */
6505 + case YAFFS_OBJECT_TYPE_FILE:
6507 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
6508 + || oh->parentObjectId ==
6509 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
6510 + fileVariant.fileSize;
6512 + case YAFFS_OBJECT_TYPE_HARDLINK:
6513 + oh->equivalentObjectId =
6514 + in->variant.hardLinkVariant.equivalentObjectId;
6516 + case YAFFS_OBJECT_TYPE_SPECIAL:
6519 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6522 + case YAFFS_OBJECT_TYPE_SYMLINK:
6523 + yaffs_strncpy(oh->alias,
6524 + in->variant.symLinkVariant.alias,
6525 + YAFFS_MAX_ALIAS_LENGTH);
6526 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
6531 + yaffs_InitialiseTags(&newTags);
6533 + newTags.chunkId = 0;
6534 + newTags.objectId = in->objectId;
6535 + newTags.serialNumber = in->serial;
6537 + /* Add extra info for file header */
6539 + newTags.extraHeaderInfoAvailable = 1;
6540 + newTags.extraParentObjectId = oh->parentObjectId;
6541 + newTags.extraFileLength = oh->fileSize;
6542 + newTags.extraIsShrinkHeader = oh->isShrink;
6543 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
6544 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
6545 + newTags.extraObjectType = in->variantType;
6547 + /* Create new chunk in NAND */
6549 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
6550 + (prevChunkId >= 0) ? 1 : 0);
6552 + if (newChunkId >= 0) {
6554 + in->chunkId = newChunkId;
6556 + if (prevChunkId >= 0) {
6557 + yaffs_DeleteChunk(dev, prevChunkId, 1,
6561 + if(!yaffs_ObjectHasCachedWriteData(in))
6564 + /* If this was a shrink, then mark the block that the chunk lives on */
6566 + bi = yaffs_GetBlockInfo(in->myDev,
6567 + newChunkId /in->myDev-> nChunksPerBlock);
6568 + bi->hasShrinkHeader = 1;
6573 + retVal = newChunkId;
6578 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6583 +/*------------------------ Short Operations Cache ----------------------------------------
6584 + * In many situations where there is no high level buffering (eg WinCE) a lot of
6585 + * reads might be short sequential reads, and a lot of writes may be short
6586 + * sequential writes. eg. scanning/writing a jpeg file.
6587 + * In these cases, a short read/write cache can provide a huge perfomance benefit
6588 + * with dumb-as-a-rock code.
6589 + * In Linux, the page cache provides read buffering aand the short op cache provides write
6592 + * There are a limited number (~10) of cache chunks per device so that we don't
6593 + * need a very intelligent search.
6596 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
6598 + yaffs_Device *dev = obj->myDev;
6600 + yaffs_ChunkCache *cache;
6601 + int nCaches = obj->myDev->nShortOpCaches;
6603 + for(i = 0; i < nCaches; i++){
6604 + cache = &dev->srCache[i];
6605 + if (cache->object == obj &&
6614 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
6616 + yaffs_Device *dev = obj->myDev;
6617 + int lowest = -99; /* Stop compiler whining. */
6619 + yaffs_ChunkCache *cache;
6620 + int chunkWritten = 0;
6621 + int nCaches = obj->myDev->nShortOpCaches;
6623 + if (nCaches > 0) {
6627 + /* Find the dirty cache for this object with the lowest chunk id. */
6628 + for (i = 0; i < nCaches; i++) {
6629 + if (dev->srCache[i].object == obj &&
6630 + dev->srCache[i].dirty) {
6632 + || dev->srCache[i].chunkId <
6634 + cache = &dev->srCache[i];
6635 + lowest = cache->chunkId;
6640 + if (cache && !cache->locked) {
6641 + /* Write it out and free it up */
6644 + yaffs_WriteChunkDataToObject(cache->object,
6650 + cache->object = NULL;
6653 + } while (cache && chunkWritten > 0);
6656 + /* Hoosterman, disk full while writing cache out. */
6657 + T(YAFFS_TRACE_ERROR,
6658 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
6665 +/*yaffs_FlushEntireDeviceCache(dev)
6670 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
6672 + yaffs_Object *obj;
6673 + int nCaches = dev->nShortOpCaches;
6676 + /* Find a dirty object in the cache and flush it...
6677 + * until there are no further dirty objects.
6681 + for( i = 0; i < nCaches && !obj; i++) {
6682 + if (dev->srCache[i].object &&
6683 + dev->srCache[i].dirty)
6684 + obj = dev->srCache[i].object;
6688 + yaffs_FlushFilesChunkCache(obj);
6695 +/* Grab us a cache chunk for use.
6696 + * First look for an empty one.
6697 + * Then look for the least recently used non-dirty one.
6698 + * Then look for the least recently used dirty one...., flush and look again.
6700 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
6706 + if (dev->nShortOpCaches > 0) {
6707 + for (i = 0; i < dev->nShortOpCaches; i++) {
6708 + if (!dev->srCache[i].object)
6709 + return &dev->srCache[i];
6715 + usage = 0; /* just to stop the compiler grizzling */
6717 + for (i = 0; i < dev->nShortOpCaches; i++) {
6718 + if (!dev->srCache[i].dirty &&
6719 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
6721 + usage = dev->srCache[i].lastUse;
6727 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
6734 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
6736 + yaffs_ChunkCache *cache;
6737 + yaffs_Object *theObj;
6742 + if (dev->nShortOpCaches > 0) {
6743 + /* Try find a non-dirty one... */
6745 + cache = yaffs_GrabChunkCacheWorker(dev);
6748 + /* They were all dirty, find the last recently used object and flush
6749 + * its cache, then find again.
6750 + * NB what's here is not very accurate, we actually flush the object
6751 + * the last recently used page.
6754 + /* With locking we can't assume we can use entry zero */
6761 + for (i = 0; i < dev->nShortOpCaches; i++) {
6762 + if (dev->srCache[i].object &&
6763 + !dev->srCache[i].locked &&
6764 + (dev->srCache[i].lastUse < usage || !cache))
6766 + usage = dev->srCache[i].lastUse;
6767 + theObj = dev->srCache[i].object;
6768 + cache = &dev->srCache[i];
6773 + if (!cache || cache->dirty) {
6774 + /* Flush and try again */
6775 + yaffs_FlushFilesChunkCache(theObj);
6776 + cache = yaffs_GrabChunkCacheWorker(dev);
6786 +/* Find a cached chunk */
6787 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
6790 + yaffs_Device *dev = obj->myDev;
6792 + if (dev->nShortOpCaches > 0) {
6793 + for (i = 0; i < dev->nShortOpCaches; i++) {
6794 + if (dev->srCache[i].object == obj &&
6795 + dev->srCache[i].chunkId == chunkId) {
6798 + return &dev->srCache[i];
6805 +/* Mark the chunk for the least recently used algorithym */
6806 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
6810 + if (dev->nShortOpCaches > 0) {
6811 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
6812 + /* Reset the cache usages */
6814 + for (i = 1; i < dev->nShortOpCaches; i++) {
6815 + dev->srCache[i].lastUse = 0;
6817 + dev->srLastUse = 0;
6822 + cache->lastUse = dev->srLastUse;
6830 +/* Invalidate a single cache page.
6831 + * Do this when a whole page gets written,
6832 + * ie the short cache for this page is no longer valid.
6834 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
6836 + if (object->myDev->nShortOpCaches > 0) {
6837 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
6840 + cache->object = NULL;
6845 +/* Invalidate all the cache pages associated with this object
6846 + * Do this whenever ther file is deleted or resized.
6848 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
6851 + yaffs_Device *dev = in->myDev;
6853 + if (dev->nShortOpCaches > 0) {
6854 + /* Invalidate it. */
6855 + for (i = 0; i < dev->nShortOpCaches; i++) {
6856 + if (dev->srCache[i].object == in) {
6857 + dev->srCache[i].object = NULL;
6863 +/*--------------------- Checkpointing --------------------*/
6866 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
6868 + yaffs_CheckpointValidity cp;
6869 + cp.structType = sizeof(cp);
6870 + cp.magic = YAFFS_MAGIC;
6871 + cp.version = YAFFS_CHECKPOINT_VERSION;
6872 + cp.head = (head) ? 1 : 0;
6874 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
6878 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
6880 + yaffs_CheckpointValidity cp;
6883 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
6886 + ok = (cp.structType == sizeof(cp)) &&
6887 + (cp.magic == YAFFS_MAGIC) &&
6888 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
6889 + (cp.head == ((head) ? 1 : 0));
6890 + return ok ? 1 : 0;
6893 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
6894 + yaffs_Device *dev)
6896 + cp->nErasedBlocks = dev->nErasedBlocks;
6897 + cp->allocationBlock = dev->allocationBlock;
6898 + cp->allocationPage = dev->allocationPage;
6899 + cp->nFreeChunks = dev->nFreeChunks;
6901 + cp->nDeletedFiles = dev->nDeletedFiles;
6902 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
6903 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
6904 + cp->sequenceNumber = dev->sequenceNumber;
6905 + cp->oldestDirtySequence = dev->oldestDirtySequence;
6909 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
6910 + yaffs_CheckpointDevice *cp)
6912 + dev->nErasedBlocks = cp->nErasedBlocks;
6913 + dev->allocationBlock = cp->allocationBlock;
6914 + dev->allocationPage = cp->allocationPage;
6915 + dev->nFreeChunks = cp->nFreeChunks;
6917 + dev->nDeletedFiles = cp->nDeletedFiles;
6918 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
6919 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
6920 + dev->sequenceNumber = cp->sequenceNumber;
6921 + dev->oldestDirtySequence = cp->oldestDirtySequence;
6925 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
6927 + yaffs_CheckpointDevice cp;
6929 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
6933 + /* Write device runtime values*/
6934 + yaffs_DeviceToCheckpointDevice(&cp,dev);
6935 + cp.structType = sizeof(cp);
6937 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
6939 + /* Write block info */
6941 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
6942 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
6945 + /* Write chunk bits */
6947 + nBytes = nBlocks * dev->chunkBitmapStride;
6948 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
6950 + return ok ? 1 : 0;
6954 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
6956 + yaffs_CheckpointDevice cp;
6958 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
6962 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
6966 + if(cp.structType != sizeof(cp))
6970 + yaffs_CheckpointDeviceToDevice(dev,&cp);
6972 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
6974 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
6978 + nBytes = nBlocks * dev->chunkBitmapStride;
6980 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
6982 + return ok ? 1 : 0;
6985 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
6986 + yaffs_Object *obj)
6989 + cp->objectId = obj->objectId;
6990 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
6991 + cp->chunkId = obj->chunkId;
6992 + cp->variantType = obj->variantType;
6993 + cp->deleted = obj->deleted;
6994 + cp->softDeleted = obj->softDeleted;
6995 + cp->unlinked = obj->unlinked;
6996 + cp->fake = obj->fake;
6997 + cp->renameAllowed = obj->renameAllowed;
6998 + cp->unlinkAllowed = obj->unlinkAllowed;
6999 + cp->serial = obj->serial;
7000 + cp->nDataChunks = obj->nDataChunks;
7002 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7003 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7004 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7005 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7008 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7011 + yaffs_Object *parent;
7013 + obj->objectId = cp->objectId;
7016 + parent = yaffs_FindOrCreateObjectByNumber(
7019 + YAFFS_OBJECT_TYPE_DIRECTORY);
7024 + yaffs_AddObjectToDirectory(parent, obj);
7026 + obj->chunkId = cp->chunkId;
7027 + obj->variantType = cp->variantType;
7028 + obj->deleted = cp->deleted;
7029 + obj->softDeleted = cp->softDeleted;
7030 + obj->unlinked = cp->unlinked;
7031 + obj->fake = cp->fake;
7032 + obj->renameAllowed = cp->renameAllowed;
7033 + obj->unlinkAllowed = cp->unlinkAllowed;
7034 + obj->serial = cp->serial;
7035 + obj->nDataChunks = cp->nDataChunks;
7037 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7038 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7039 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7040 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7042 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7043 + obj->lazyLoaded = 1;
7048 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7049 + __u32 level, int chunkOffset)
7052 + yaffs_Device *dev = in->myDev;
7054 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7059 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7060 + if (tn->internal[i]) {
7061 + ok = yaffs_CheckpointTnodeWorker(in,
7064 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7067 + } else if (level == 0) {
7068 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7069 + /* printf("write tnode at %d\n",baseOffset); */
7070 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7072 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7080 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7082 + __u32 endMarker = ~0;
7085 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7086 + ok = yaffs_CheckpointTnodeWorker(obj,
7087 + obj->variant.fileVariant.top,
7088 + obj->variant.fileVariant.topLevel,
7091 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7092 + sizeof(endMarker));
7095 + return ok ? 1 : 0;
7098 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7102 + yaffs_Device *dev = obj->myDev;
7103 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7106 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7108 + while(ok && (~baseChunk)){
7109 + /* Read level 0 tnode */
7111 + /* printf("read tnode at %d\n",baseChunk); */
7112 + tn = yaffs_GetTnodeRaw(dev);
7114 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7115 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7120 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7127 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7131 + return ok ? 1 : 0;
7135 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7137 + yaffs_Object *obj;
7138 + yaffs_CheckpointObject cp;
7141 + struct list_head *lh;
7144 + /* Iterate through the objects in each hash entry,
7145 + * dumping them to the checkpointing stream.
7148 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
7149 + list_for_each(lh, &dev->objectBucket[i].list) {
7151 + obj = list_entry(lh, yaffs_Object, hashLink);
7152 + if (!obj->deferedFree) {
7153 + yaffs_ObjectToCheckpointObject(&cp,obj);
7154 + cp.structType = sizeof(cp);
7156 + T(YAFFS_TRACE_CHECKPOINT,(
7157 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7158 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7160 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7162 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7163 + ok = yaffs_WriteCheckpointTnodes(obj);
7170 + /* Dump end of list */
7171 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
7172 + cp.structType = sizeof(cp);
7175 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7177 + return ok ? 1 : 0;
7180 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
7182 + yaffs_Object *obj;
7183 + yaffs_CheckpointObject cp;
7186 + yaffs_Object *hardList = NULL;
7188 + while(ok && !done) {
7189 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7190 + if(cp.structType != sizeof(cp)) {
7191 + /* printf("structure parsing failed\n"); */
7195 + if(ok && cp.objectId == ~0)
7198 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
7199 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7200 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7202 + yaffs_CheckpointObjectToObject(obj,&cp);
7203 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
7204 + ok = yaffs_ReadCheckpointTnodes(obj);
7205 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
7206 + obj->hardLinks.next =
7207 + (struct list_head *)
7217 + yaffs_HardlinkFixup(dev,hardList);
7219 + return ok ? 1 : 0;
7222 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
7227 + ok = yaffs_CheckpointOpen(dev,1);
7230 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
7232 + ok = yaffs_WriteCheckpointDevice(dev);
7234 + ok = yaffs_WriteCheckpointObjects(dev);
7236 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
7238 + if(!yaffs_CheckpointClose(dev))
7242 + dev->isCheckpointed = 1;
7244 + dev->isCheckpointed = 0;
7246 + return dev->isCheckpointed;
7249 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
7253 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
7256 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
7258 + ok = yaffs_ReadCheckpointDevice(dev);
7260 + ok = yaffs_ReadCheckpointObjects(dev);
7262 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
7266 + if(!yaffs_CheckpointClose(dev))
7270 + dev->isCheckpointed = 1;
7272 + dev->isCheckpointed = 0;
7274 + return ok ? 1 : 0;
7278 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
7280 + if(dev->isCheckpointed ||
7281 + dev->blocksInCheckpoint > 0){
7282 + dev->isCheckpointed = 0;
7283 + yaffs_CheckpointInvalidateStream(dev);
7284 + if(dev->superBlock && dev->markSuperBlockDirty)
7285 + dev->markSuperBlockDirty(dev->superBlock);
7290 +int yaffs_CheckpointSave(yaffs_Device *dev)
7292 + yaffs_ReportOddballBlocks(dev);
7293 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7295 + if(!dev->isCheckpointed)
7296 + yaffs_WriteCheckpointData(dev);
7298 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7300 + return dev->isCheckpointed;
7303 +int yaffs_CheckpointRestore(yaffs_Device *dev)
7306 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7308 + retval = yaffs_ReadCheckpointData(dev);
7310 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7312 + yaffs_ReportOddballBlocks(dev);
7317 +/*--------------------- File read/write ------------------------
7318 + * Read and write have very similar structures.
7319 + * In general the read/write has three parts to it
7320 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
7321 + * Some complete chunks
7322 + * An incomplete chunk to end off with
7324 + * Curve-balls: the first chunk might also be the last chunk.
7327 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
7336 + yaffs_ChunkCache *cache;
7338 + yaffs_Device *dev;
7343 + //chunk = offset / dev->nDataBytesPerChunk + 1;
7344 + //start = offset % dev->nDataBytesPerChunk;
7345 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
7348 + /* OK now check for the curveball where the start and end are in
7351 + if ((start + n) < dev->nDataBytesPerChunk) {
7354 + nToCopy = dev->nDataBytesPerChunk - start;
7357 + cache = yaffs_FindChunkCache(in, chunk);
7359 + /* If the chunk is already in the cache or it is less than a whole chunk
7360 + * then use the cache (if there is caching)
7361 + * else bypass the cache.
7363 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
7364 + if (dev->nShortOpCaches > 0) {
7366 + /* If we can't find the data in the cache, then load it up. */
7369 + cache = yaffs_GrabChunkCache(in->myDev);
7370 + cache->object = in;
7371 + cache->chunkId = chunk;
7373 + cache->locked = 0;
7374 + yaffs_ReadChunkDataFromObject(in, chunk,
7377 + cache->nBytes = 0;
7380 + yaffs_UseChunkCache(dev, cache, 0);
7382 + cache->locked = 1;
7384 +#ifdef CONFIG_YAFFS_WINCE
7385 + yfsd_UnlockYAFFS(TRUE);
7387 + memcpy(buffer, &cache->data[start], nToCopy);
7389 +#ifdef CONFIG_YAFFS_WINCE
7390 + yfsd_LockYAFFS(TRUE);
7392 + cache->locked = 0;
7394 + /* Read into the local buffer then copy..*/
7396 + __u8 *localBuffer =
7397 + yaffs_GetTempBuffer(dev, __LINE__);
7398 + yaffs_ReadChunkDataFromObject(in, chunk,
7400 +#ifdef CONFIG_YAFFS_WINCE
7401 + yfsd_UnlockYAFFS(TRUE);
7403 + memcpy(buffer, &localBuffer[start], nToCopy);
7405 +#ifdef CONFIG_YAFFS_WINCE
7406 + yfsd_LockYAFFS(TRUE);
7408 + yaffs_ReleaseTempBuffer(dev, localBuffer,
7413 +#ifdef CONFIG_YAFFS_WINCE
7414 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7416 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
7417 + * This is because we otherwise screw up WinCE's memory mapper
7419 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
7421 +#ifdef CONFIG_YAFFS_WINCE
7422 + yfsd_UnlockYAFFS(TRUE);
7424 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
7426 +#ifdef CONFIG_YAFFS_WINCE
7427 + yfsd_LockYAFFS(TRUE);
7428 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7432 + /* A full chunk. Read directly into the supplied buffer. */
7433 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
7438 + offset += nToCopy;
7439 + buffer += nToCopy;
7447 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
7448 + int nBytes, int writeThrough)
7457 + int startOfWrite = offset;
7458 + int chunkWritten = 0;
7461 + yaffs_Device *dev;
7465 + while (n > 0 && chunkWritten >= 0) {
7466 + //chunk = offset / dev->nDataBytesPerChunk + 1;
7467 + //start = offset % dev->nDataBytesPerChunk;
7468 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
7471 + /* OK now check for the curveball where the start and end are in
7475 + if ((start + n) < dev->nDataBytesPerChunk) {
7478 + /* Now folks, to calculate how many bytes to write back....
7479 + * If we're overwriting and not writing to then end of file then
7480 + * we need to write back as much as was there before.
7484 + in->variant.fileVariant.fileSize -
7485 + ((chunk - 1) * dev->nDataBytesPerChunk);
7487 + if (nBytesRead > dev->nDataBytesPerChunk) {
7488 + nBytesRead = dev->nDataBytesPerChunk;
7493 + (start + n)) ? nBytesRead : (start + n);
7496 + nToCopy = dev->nDataBytesPerChunk - start;
7497 + nToWriteBack = dev->nDataBytesPerChunk;
7500 + if (nToCopy != dev->nDataBytesPerChunk) {
7501 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
7502 + if (dev->nShortOpCaches > 0) {
7503 + yaffs_ChunkCache *cache;
7504 + /* If we can't find the data in the cache, then load the cache */
7505 + cache = yaffs_FindChunkCache(in, chunk);
7508 + && yaffs_CheckSpaceForAllocation(in->
7510 + cache = yaffs_GrabChunkCache(in->myDev);
7511 + cache->object = in;
7512 + cache->chunkId = chunk;
7514 + cache->locked = 0;
7515 + yaffs_ReadChunkDataFromObject(in, chunk,
7521 + !yaffs_CheckSpaceForAllocation(in->myDev)){
7522 + /* Drop the cache if it was a read cache item and
7523 + * no space check has been made for it.
7529 + yaffs_UseChunkCache(dev, cache, 1);
7530 + cache->locked = 1;
7531 +#ifdef CONFIG_YAFFS_WINCE
7532 + yfsd_UnlockYAFFS(TRUE);
7535 + memcpy(&cache->data[start], buffer,
7538 +#ifdef CONFIG_YAFFS_WINCE
7539 + yfsd_LockYAFFS(TRUE);
7541 + cache->locked = 0;
7542 + cache->nBytes = nToWriteBack;
7544 + if (writeThrough) {
7546 + yaffs_WriteChunkDataToObject
7549 + cache->data, cache->nBytes,
7555 + chunkWritten = -1; /* fail the write */
7558 + /* An incomplete start or end chunk (or maybe both start and end chunk)
7559 + * Read into the local buffer then copy, then copy over and write back.
7562 + __u8 *localBuffer =
7563 + yaffs_GetTempBuffer(dev, __LINE__);
7565 + yaffs_ReadChunkDataFromObject(in, chunk,
7568 +#ifdef CONFIG_YAFFS_WINCE
7569 + yfsd_UnlockYAFFS(TRUE);
7572 + memcpy(&localBuffer[start], buffer, nToCopy);
7574 +#ifdef CONFIG_YAFFS_WINCE
7575 + yfsd_LockYAFFS(TRUE);
7578 + yaffs_WriteChunkDataToObject(in, chunk,
7583 + yaffs_ReleaseTempBuffer(dev, localBuffer,
7590 +#ifdef CONFIG_YAFFS_WINCE
7591 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
7592 + * This is because we otherwise screw up WinCE's memory mapper
7594 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7595 +#ifdef CONFIG_YAFFS_WINCE
7596 + yfsd_UnlockYAFFS(TRUE);
7598 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
7599 +#ifdef CONFIG_YAFFS_WINCE
7600 + yfsd_LockYAFFS(TRUE);
7603 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
7604 + dev->nDataBytesPerChunk,
7606 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7608 + /* A full chunk. Write directly from the supplied buffer. */
7610 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
7611 + dev->nDataBytesPerChunk,
7614 + /* Since we've overwritten the cached data, we better invalidate it. */
7615 + yaffs_InvalidateChunkCache(in, chunk);
7618 + if (chunkWritten >= 0) {
7620 + offset += nToCopy;
7621 + buffer += nToCopy;
7627 + /* Update file object */
7629 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
7630 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
7639 +/* ---------------------- File resizing stuff ------------------ */
7641 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
7644 + yaffs_Device *dev = in->myDev;
7645 + int oldFileSize = in->variant.fileVariant.fileSize;
7647 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
7649 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
7650 + dev->nDataBytesPerChunk;
7654 + /* Delete backwards so that we don't end up with holes if
7655 + * power is lost part-way through the operation.
7657 + for (i = lastDel; i >= startDel; i--) {
7658 + /* NB this could be optimised somewhat,
7659 + * eg. could retrieve the tags and write them without
7660 + * using yaffs_DeleteChunk
7663 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
7664 + if (chunkId > 0) {
7666 + (dev->internalStartBlock * dev->nChunksPerBlock)
7668 + ((dev->internalEndBlock +
7669 + 1) * dev->nChunksPerBlock)) {
7670 + T(YAFFS_TRACE_ALWAYS,
7671 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
7674 + in->nDataChunks--;
7675 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
7682 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
7685 + int oldFileSize = in->variant.fileVariant.fileSize;
7686 + int newSizeOfPartialChunk;
7687 + int newFullChunks;
7689 + yaffs_Device *dev = in->myDev;
7691 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
7693 + yaffs_FlushFilesChunkCache(in);
7694 + yaffs_InvalidateWholeChunkCache(in);
7696 + yaffs_CheckGarbageCollection(dev);
7698 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7699 + return yaffs_GetFileSize(in);
7702 + if (newSize == oldFileSize) {
7703 + return oldFileSize;
7706 + if (newSize < oldFileSize) {
7708 + yaffs_PruneResizedChunks(in, newSize);
7710 + if (newSizeOfPartialChunk != 0) {
7711 + int lastChunk = 1 + newFullChunks;
7713 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7715 + /* Got to read and rewrite the last chunk with its new size and zero pad */
7716 + yaffs_ReadChunkDataFromObject(in, lastChunk,
7719 + memset(localBuffer + newSizeOfPartialChunk, 0,
7720 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
7722 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
7723 + newSizeOfPartialChunk, 1);
7725 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7728 + in->variant.fileVariant.fileSize = newSize;
7730 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
7732 + /* Write a new object header.
7733 + * show we've shrunk the file, if need be
7734 + * Do this only if the file is not in the deleted directories.
7736 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
7737 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
7738 + yaffs_UpdateObjectHeader(in, NULL, 0,
7739 + (newSize < oldFileSize) ? 1 : 0, 0);
7745 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
7747 + obj = yaffs_GetEquivalentObject(obj);
7749 + switch (obj->variantType) {
7750 + case YAFFS_OBJECT_TYPE_FILE:
7751 + return obj->variant.fileVariant.fileSize;
7752 + case YAFFS_OBJECT_TYPE_SYMLINK:
7753 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
7761 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
7765 + yaffs_FlushFilesChunkCache(in);
7767 +#ifdef CONFIG_YAFFS_WINCE
7768 + yfsd_WinFileTimeNow(in->win_mtime);
7771 + in->yst_mtime = Y_CURRENT_TIME;
7777 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
7778 + 0) ? YAFFS_OK : YAFFS_FAIL;
7780 + retVal = YAFFS_OK;
7787 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
7790 + /* First off, invalidate the file's data in the cache, without flushing. */
7791 + yaffs_InvalidateWholeChunkCache(in);
7793 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
7794 + /* Move to the unlinked directory so we have a record that it was deleted. */
7795 + yaffs_ChangeObjectName(in, in->myDev->deletedDir, NULL, 0, 0);
7799 + yaffs_RemoveObjectFromDirectory(in);
7800 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
7803 + yaffs_FreeObject(in);
7808 +/* yaffs_DeleteFile deletes the whole file data
7809 + * and the inode associated with the file.
7810 + * It does not delete the links associated with the file.
7812 +static int yaffs_UnlinkFile(yaffs_Object * in)
7816 + int immediateDeletion = 0;
7820 + if (!in->myInode) {
7821 + immediateDeletion = 1;
7825 + if (in->inUse <= 0) {
7826 + immediateDeletion = 1;
7830 + if (immediateDeletion) {
7832 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
7834 + T(YAFFS_TRACE_TRACING,
7835 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
7838 + in->myDev->nDeletedFiles++;
7839 + if (0 && in->myDev->isYaffs2) {
7840 + yaffs_ResizeFile(in, 0);
7842 + yaffs_SoftDeleteFile(in);
7845 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
7853 +int yaffs_DeleteFile(yaffs_Object * in)
7855 + int retVal = YAFFS_OK;
7857 + if (in->nDataChunks > 0) {
7858 + /* Use soft deletion if there is data in the file */
7859 + if (!in->unlinked) {
7860 + retVal = yaffs_UnlinkFile(in);
7862 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
7864 + in->myDev->nDeletedFiles++;
7865 + yaffs_SoftDeleteFile(in);
7867 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
7869 + /* The file has no data chunks so we toss it immediately */
7870 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
7871 + in->variant.fileVariant.top = NULL;
7872 + yaffs_DoGenericObjectDeletion(in);
7878 +static int yaffs_DeleteDirectory(yaffs_Object * in)
7880 + /* First check that the directory is empty. */
7881 + if (list_empty(&in->variant.directoryVariant.children)) {
7882 + return yaffs_DoGenericObjectDeletion(in);
7885 + return YAFFS_FAIL;
7889 +static int yaffs_DeleteSymLink(yaffs_Object * in)
7891 + YFREE(in->variant.symLinkVariant.alias);
7893 + return yaffs_DoGenericObjectDeletion(in);
7896 +static int yaffs_DeleteHardLink(yaffs_Object * in)
7898 + /* remove this hardlink from the list assocaited with the equivalent
7901 + list_del(&in->hardLinks);
7902 + return yaffs_DoGenericObjectDeletion(in);
7905 +static void yaffs_DestroyObject(yaffs_Object * obj)
7907 + switch (obj->variantType) {
7908 + case YAFFS_OBJECT_TYPE_FILE:
7909 + yaffs_DeleteFile(obj);
7911 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7912 + yaffs_DeleteDirectory(obj);
7914 + case YAFFS_OBJECT_TYPE_SYMLINK:
7915 + yaffs_DeleteSymLink(obj);
7917 + case YAFFS_OBJECT_TYPE_HARDLINK:
7918 + yaffs_DeleteHardLink(obj);
7920 + case YAFFS_OBJECT_TYPE_SPECIAL:
7921 + yaffs_DoGenericObjectDeletion(obj);
7923 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7924 + break; /* should not happen. */
7928 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
7931 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
7932 + return yaffs_DeleteHardLink(obj);
7933 + } else if (!list_empty(&obj->hardLinks)) {
7934 + /* Curve ball: We're unlinking an object that has a hardlink.
7936 + * This problem arises because we are not strictly following
7937 + * The Linux link/inode model.
7939 + * We can't really delete the object.
7940 + * Instead, we do the following:
7941 + * - Select a hardlink.
7942 + * - Unhook it from the hard links
7943 + * - Unhook it from its parent directory (so that the rename can work)
7944 + * - Rename the object to the hardlink's name.
7945 + * - Delete the hardlink
7950 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
7952 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
7954 + list_del_init(&hl->hardLinks);
7955 + list_del_init(&hl->siblings);
7957 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
7959 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
7961 + if (retVal == YAFFS_OK) {
7962 + retVal = yaffs_DoGenericObjectDeletion(hl);
7967 + switch (obj->variantType) {
7968 + case YAFFS_OBJECT_TYPE_FILE:
7969 + return yaffs_UnlinkFile(obj);
7971 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7972 + return yaffs_DeleteDirectory(obj);
7974 + case YAFFS_OBJECT_TYPE_SYMLINK:
7975 + return yaffs_DeleteSymLink(obj);
7977 + case YAFFS_OBJECT_TYPE_SPECIAL:
7978 + return yaffs_DoGenericObjectDeletion(obj);
7980 + case YAFFS_OBJECT_TYPE_HARDLINK:
7981 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7983 + return YAFFS_FAIL;
7989 +static int yaffs_UnlinkObject( yaffs_Object *obj)
7992 + if (obj && obj->unlinkAllowed) {
7993 + return yaffs_UnlinkWorker(obj);
7996 + return YAFFS_FAIL;
7999 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8001 + yaffs_Object *obj;
8003 + obj = yaffs_FindObjectByName(dir, name);
8004 + return yaffs_UnlinkObject(obj);
8007 +/*----------------------- Initialisation Scanning ---------------------- */
8009 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8010 + int backwardScanning)
8012 + yaffs_Object *obj;
8014 + if (!backwardScanning) {
8015 + /* Handle YAFFS1 forward scanning case
8016 + * For YAFFS1 we always do the deletion
8020 + /* Handle YAFFS2 case (backward scanning)
8021 + * If the shadowed object exists then ignore.
8023 + if (yaffs_FindObjectByNumber(dev, objId)) {
8028 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8029 + * We put it in unlinked dir to be cleaned up after the scanning
8032 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8033 + YAFFS_OBJECT_TYPE_FILE);
8034 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8035 + obj->variant.fileVariant.shrinkSize = 0;
8036 + obj->valid = 1; /* So that we don't read any other info for this file */
8043 +} yaffs_BlockIndex;
8046 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8051 + while (hardList) {
8053 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8055 + in = yaffs_FindObjectByNumber(dev,
8056 + hl->variant.hardLinkVariant.
8057 + equivalentObjectId);
8060 + /* Add the hardlink pointers */
8061 + hl->variant.hardLinkVariant.equivalentObject = in;
8062 + list_add(&hl->hardLinks, &in->hardLinks);
8064 + /* Todo Need to report/handle this better.
8065 + * Got a problem... hardlink to a non-existant object
8067 + hl->variant.hardLinkVariant.equivalentObject = NULL;
8068 + INIT_LIST_HEAD(&hl->hardLinks);
8080 +static int ybicmp(const void *a, const void *b){
8081 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
8082 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
8083 + register int ablock = ((yaffs_BlockIndex *)a)->block;
8084 + register int bblock = ((yaffs_BlockIndex *)b)->block;
8085 + if( aseq == bseq )
8086 + return ablock - bblock;
8088 + return aseq - bseq;
8092 +static int yaffs_Scan(yaffs_Device * dev)
8094 + yaffs_ExtendedTags tags;
8096 + int blockIterator;
8097 + int startIterator;
8099 + int nBlocksToScan = 0;
8105 + yaffs_BlockState state;
8106 + yaffs_Object *hardList = NULL;
8108 + yaffs_BlockInfo *bi;
8109 + int sequenceNumber;
8110 + yaffs_ObjectHeader *oh;
8112 + yaffs_Object *parent;
8113 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
8117 + yaffs_BlockIndex *blockIndex = NULL;
8119 + if (dev->isYaffs2) {
8120 + T(YAFFS_TRACE_SCAN,
8121 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
8122 + return YAFFS_FAIL;
8125 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
8127 + T(YAFFS_TRACE_SCAN,
8128 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
8129 + dev->internalStartBlock, dev->internalEndBlock));
8131 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8133 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
8135 + if (dev->isYaffs2) {
8136 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
8139 + /* Scan all the blocks to determine their state */
8140 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
8141 + bi = yaffs_GetBlockInfo(dev, blk);
8142 + yaffs_ClearChunkBits(dev, blk);
8143 + bi->pagesInUse = 0;
8144 + bi->softDeletions = 0;
8146 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
8148 + bi->blockState = state;
8149 + bi->sequenceNumber = sequenceNumber;
8151 + T(YAFFS_TRACE_SCAN_DEBUG,
8152 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
8153 + state, sequenceNumber));
8155 + if (state == YAFFS_BLOCK_STATE_DEAD) {
8156 + T(YAFFS_TRACE_BAD_BLOCKS,
8157 + (TSTR("block %d is bad" TENDSTR), blk));
8158 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
8159 + T(YAFFS_TRACE_SCAN_DEBUG,
8160 + (TSTR("Block empty " TENDSTR)));
8161 + dev->nErasedBlocks++;
8162 + dev->nFreeChunks += dev->nChunksPerBlock;
8163 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8165 + /* Determine the highest sequence number */
8166 + if (dev->isYaffs2 &&
8167 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
8168 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
8170 + blockIndex[nBlocksToScan].seq = sequenceNumber;
8171 + blockIndex[nBlocksToScan].block = blk;
8175 + if (sequenceNumber >= dev->sequenceNumber) {
8176 + dev->sequenceNumber = sequenceNumber;
8178 + } else if (dev->isYaffs2) {
8179 + /* TODO: Nasty sequence number! */
8180 + T(YAFFS_TRACE_SCAN,
8182 + ("Block scanning block %d has bad sequence number %d"
8183 + TENDSTR), blk, sequenceNumber));
8189 + /* Sort the blocks
8190 + * Dungy old bubble sort for now...
8192 + if (dev->isYaffs2) {
8193 + yaffs_BlockIndex temp;
8197 + for (i = 0; i < nBlocksToScan; i++)
8198 + for (j = i + 1; j < nBlocksToScan; j++)
8199 + if (blockIndex[i].seq > blockIndex[j].seq) {
8200 + temp = blockIndex[j];
8201 + blockIndex[j] = blockIndex[i];
8202 + blockIndex[i] = temp;
8206 + /* Now scan the blocks looking at the data. */
8207 + if (dev->isYaffs2) {
8208 + startIterator = 0;
8209 + endIterator = nBlocksToScan - 1;
8210 + T(YAFFS_TRACE_SCAN_DEBUG,
8211 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
8213 + startIterator = dev->internalStartBlock;
8214 + endIterator = dev->internalEndBlock;
8217 + /* For each block.... */
8218 + for (blockIterator = startIterator; blockIterator <= endIterator;
8219 + blockIterator++) {
8221 + if (dev->isYaffs2) {
8222 + /* get the block to scan in the correct order */
8223 + blk = blockIndex[blockIterator].block;
8225 + blk = blockIterator;
8228 + bi = yaffs_GetBlockInfo(dev, blk);
8229 + state = bi->blockState;
8233 + /* For each chunk in each block that needs scanning....*/
8234 + for (c = 0; c < dev->nChunksPerBlock &&
8235 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
8236 + /* Read the tags and decide what to do */
8237 + chunk = blk * dev->nChunksPerBlock + c;
8239 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
8242 + /* Let's have a good look at this chunk... */
8244 + if (!dev->isYaffs2 && tags.chunkDeleted) {
8249 + dev->nFreeChunks++;
8250 + /*T((" %d %d deleted\n",blk,c)); */
8251 + } else if (!tags.chunkUsed) {
8252 + /* An unassigned chunk in the block
8253 + * This means that either the block is empty or
8254 + * this is the one being allocated from
8258 + /* We're looking at the first chunk in the block so the block is unused */
8259 + state = YAFFS_BLOCK_STATE_EMPTY;
8260 + dev->nErasedBlocks++;
8262 + /* this is the block being allocated from */
8263 + T(YAFFS_TRACE_SCAN,
8265 + (" Allocating from %d %d" TENDSTR),
8267 + state = YAFFS_BLOCK_STATE_ALLOCATING;
8268 + dev->allocationBlock = blk;
8269 + dev->allocationPage = c;
8270 + dev->allocationBlockFinder = blk;
8271 + /* Set it to here to encourage the allocator to go forth from here. */
8273 + /* Yaffs2 sanity check:
8274 + * This should be the one with the highest sequence number
8277 + && (dev->sequenceNumber !=
8278 + bi->sequenceNumber)) {
8279 + T(YAFFS_TRACE_ALWAYS,
8281 + ("yaffs: Allocation block %d was not highest sequence id:"
8282 + " block seq = %d, dev seq = %d"
8283 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
8287 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
8288 + } else if (tags.chunkId > 0) {
8289 + /* chunkId > 0 so it is a data chunk... */
8290 + unsigned int endpos;
8292 + yaffs_SetChunkBit(dev, blk, c);
8295 + in = yaffs_FindOrCreateObjectByNumber(dev,
8298 + YAFFS_OBJECT_TYPE_FILE);
8299 + /* PutChunkIntoFile checks for a clash (two data chunks with
8300 + * the same chunkId).
8302 + yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,
8305 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
8307 + if (in->variantType == YAFFS_OBJECT_TYPE_FILE
8308 + && in->variant.fileVariant.scannedFileSize <
8310 + in->variant.fileVariant.
8311 + scannedFileSize = endpos;
8312 + if (!dev->useHeaderFileSize) {
8313 + in->variant.fileVariant.
8315 + in->variant.fileVariant.
8320 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
8322 + /* chunkId == 0, so it is an ObjectHeader.
8323 + * Thus, we read in the object header and make the object
8325 + yaffs_SetChunkBit(dev, blk, c);
8328 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
8332 + oh = (yaffs_ObjectHeader *) chunkData;
8334 + in = yaffs_FindObjectByNumber(dev,
8336 + if (in && in->variantType != oh->type) {
8337 + /* This should not happen, but somehow
8338 + * Wev'e ended up with an objectId that has been reused but not yet
8339 + * deleted, and worse still it has changed type. Delete the old object.
8342 + yaffs_DestroyObject(in);
8347 + in = yaffs_FindOrCreateObjectByNumber(dev,
8352 + if (oh->shadowsObject > 0) {
8353 + yaffs_HandleShadowedObject(dev,
8360 + /* We have already filled this one. We have a duplicate and need to resolve it. */
8362 + unsigned existingSerial = in->serial;
8363 + unsigned newSerial = tags.serialNumber;
8365 + if (dev->isYaffs2 ||
8366 + ((existingSerial + 1) & 3) ==
8368 + /* Use new one - destroy the exisiting one */
8369 + yaffs_DeleteChunk(dev,
8374 + /* Use existing - destroy this one. */
8375 + yaffs_DeleteChunk(dev, chunk, 1,
8381 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
8382 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
8383 + /* We only load some info, don't fiddle with directory structure */
8385 + in->variantType = oh->type;
8387 + in->yst_mode = oh->yst_mode;
8388 +#ifdef CONFIG_YAFFS_WINCE
8389 + in->win_atime[0] = oh->win_atime[0];
8390 + in->win_ctime[0] = oh->win_ctime[0];
8391 + in->win_mtime[0] = oh->win_mtime[0];
8392 + in->win_atime[1] = oh->win_atime[1];
8393 + in->win_ctime[1] = oh->win_ctime[1];
8394 + in->win_mtime[1] = oh->win_mtime[1];
8396 + in->yst_uid = oh->yst_uid;
8397 + in->yst_gid = oh->yst_gid;
8398 + in->yst_atime = oh->yst_atime;
8399 + in->yst_mtime = oh->yst_mtime;
8400 + in->yst_ctime = oh->yst_ctime;
8401 + in->yst_rdev = oh->yst_rdev;
8403 + in->chunkId = chunk;
8405 + } else if (!in->valid) {
8406 + /* we need to load this info */
8409 + in->variantType = oh->type;
8411 + in->yst_mode = oh->yst_mode;
8412 +#ifdef CONFIG_YAFFS_WINCE
8413 + in->win_atime[0] = oh->win_atime[0];
8414 + in->win_ctime[0] = oh->win_ctime[0];
8415 + in->win_mtime[0] = oh->win_mtime[0];
8416 + in->win_atime[1] = oh->win_atime[1];
8417 + in->win_ctime[1] = oh->win_ctime[1];
8418 + in->win_mtime[1] = oh->win_mtime[1];
8420 + in->yst_uid = oh->yst_uid;
8421 + in->yst_gid = oh->yst_gid;
8422 + in->yst_atime = oh->yst_atime;
8423 + in->yst_mtime = oh->yst_mtime;
8424 + in->yst_ctime = oh->yst_ctime;
8425 + in->yst_rdev = oh->yst_rdev;
8427 + in->chunkId = chunk;
8429 + yaffs_SetObjectName(in, oh->name);
8432 + /* directory stuff...
8433 + * hook up to parent
8437 + yaffs_FindOrCreateObjectByNumber
8438 + (dev, oh->parentObjectId,
8439 + YAFFS_OBJECT_TYPE_DIRECTORY);
8440 + if (parent->variantType ==
8441 + YAFFS_OBJECT_TYPE_UNKNOWN) {
8442 + /* Set up as a directory */
8443 + parent->variantType =
8444 + YAFFS_OBJECT_TYPE_DIRECTORY;
8445 + INIT_LIST_HEAD(&parent->variant.
8448 + } else if (parent->variantType !=
8449 + YAFFS_OBJECT_TYPE_DIRECTORY)
8451 + /* Hoosterman, another problem....
8452 + * We're trying to use a non-directory as a directory
8455 + T(YAFFS_TRACE_ERROR,
8457 + ("yaffs tragedy: attempting to use non-directory as"
8458 + " a directory in scan. Put in lost+found."
8460 + parent = dev->lostNFoundDir;
8463 + yaffs_AddObjectToDirectory(parent, in);
8465 + if (0 && (parent == dev->deletedDir ||
8466 + parent == dev->unlinkedDir)) {
8467 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
8468 + dev->nDeletedFiles++;
8470 + /* Note re hardlinks.
8471 + * Since we might scan a hardlink before its equivalent object is scanned
8472 + * we put them all in a list.
8473 + * After scanning is complete, we should have all the objects, so we run through this
8474 + * list and fix up all the chains.
8477 + switch (in->variantType) {
8478 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8479 + /* Todo got a problem */
8481 + case YAFFS_OBJECT_TYPE_FILE:
8483 + && oh->isShrink) {
8484 + /* Prune back the shrunken chunks */
8485 + yaffs_PruneResizedChunks
8486 + (in, oh->fileSize);
8487 + /* Mark the block as having a shrinkHeader */
8488 + bi->hasShrinkHeader = 1;
8491 + if (dev->useHeaderFileSize)
8493 + in->variant.fileVariant.
8498 + case YAFFS_OBJECT_TYPE_HARDLINK:
8499 + in->variant.hardLinkVariant.
8500 + equivalentObjectId =
8501 + oh->equivalentObjectId;
8502 + in->hardLinks.next =
8503 + (struct list_head *)
8507 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8510 + case YAFFS_OBJECT_TYPE_SPECIAL:
8513 + case YAFFS_OBJECT_TYPE_SYMLINK:
8514 + in->variant.symLinkVariant.
8516 + yaffs_CloneString(oh->alias);
8520 + if (parent == dev->deletedDir) {
8521 + yaffs_DestroyObject(in);
8522 + bi->hasShrinkHeader = 1;
8528 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8529 + /* If we got this far while scanning, then the block is fully allocated.*/
8530 + state = YAFFS_BLOCK_STATE_FULL;
8533 + bi->blockState = state;
8535 + /* Now let's see if it was dirty */
8536 + if (bi->pagesInUse == 0 &&
8537 + !bi->hasShrinkHeader &&
8538 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
8539 + yaffs_BlockBecameDirty(dev, blk);
8545 + YFREE(blockIndex);
8549 + /* Ok, we've done all the scanning.
8550 + * Fix up the hard link chains.
8551 + * We should now have scanned all the objects, now it's time to add these
8555 + yaffs_HardlinkFixup(dev,hardList);
8557 + /* Handle the unlinked files. Since they were left in an unlinked state we should
8558 + * just delete them.
8561 + struct list_head *i;
8562 + struct list_head *n;
8565 + /* Soft delete all the unlinked files */
8566 + list_for_each_safe(i, n,
8567 + &dev->unlinkedDir->variant.directoryVariant.
8570 + l = list_entry(i, yaffs_Object, siblings);
8571 + yaffs_DestroyObject(l);
8576 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
8578 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
8583 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
8586 + yaffs_ObjectHeader *oh;
8587 + yaffs_Device *dev = in->myDev;
8588 + yaffs_ExtendedTags tags;
8592 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
8594 + in->lazyLoaded ? "not yet" : "already"));
8597 + if(in->lazyLoaded){
8598 + in->lazyLoaded = 0;
8599 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8601 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
8602 + oh = (yaffs_ObjectHeader *) chunkData;
8604 + in->yst_mode = oh->yst_mode;
8605 +#ifdef CONFIG_YAFFS_WINCE
8606 + in->win_atime[0] = oh->win_atime[0];
8607 + in->win_ctime[0] = oh->win_ctime[0];
8608 + in->win_mtime[0] = oh->win_mtime[0];
8609 + in->win_atime[1] = oh->win_atime[1];
8610 + in->win_ctime[1] = oh->win_ctime[1];
8611 + in->win_mtime[1] = oh->win_mtime[1];
8613 + in->yst_uid = oh->yst_uid;
8614 + in->yst_gid = oh->yst_gid;
8615 + in->yst_atime = oh->yst_atime;
8616 + in->yst_mtime = oh->yst_mtime;
8617 + in->yst_ctime = oh->yst_ctime;
8618 + in->yst_rdev = oh->yst_rdev;
8621 + yaffs_SetObjectName(in, oh->name);
8623 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
8624 + in->variant.symLinkVariant.alias =
8625 + yaffs_CloneString(oh->alias);
8627 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
8631 +static int yaffs_ScanBackwards(yaffs_Device * dev)
8633 + yaffs_ExtendedTags tags;
8635 + int blockIterator;
8636 + int startIterator;
8638 + int nBlocksToScan = 0;
8644 + yaffs_BlockState state;
8645 + yaffs_Object *hardList = NULL;
8646 + yaffs_BlockInfo *bi;
8647 + int sequenceNumber;
8648 + yaffs_ObjectHeader *oh;
8650 + yaffs_Object *parent;
8651 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
8657 + int foundChunksInBlock;
8658 + int equivalentObjectId;
8661 + yaffs_BlockIndex *blockIndex = NULL;
8662 + int altBlockIndex = 0;
8664 + if (!dev->isYaffs2) {
8665 + T(YAFFS_TRACE_SCAN,
8666 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
8667 + return YAFFS_FAIL;
8670 + T(YAFFS_TRACE_SCAN,
8672 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
8673 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
8676 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
8678 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
8681 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
8682 + altBlockIndex = 1;
8686 + T(YAFFS_TRACE_SCAN,
8687 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
8688 + return YAFFS_FAIL;
8691 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8693 + /* Scan all the blocks to determine their state */
8694 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
8695 + bi = yaffs_GetBlockInfo(dev, blk);
8696 + yaffs_ClearChunkBits(dev, blk);
8697 + bi->pagesInUse = 0;
8698 + bi->softDeletions = 0;
8700 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
8702 + bi->blockState = state;
8703 + bi->sequenceNumber = sequenceNumber;
8705 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
8706 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
8708 + T(YAFFS_TRACE_SCAN_DEBUG,
8709 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
8710 + state, sequenceNumber));
8713 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
8714 + /* todo .. fix free space ? */
8716 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
8717 + T(YAFFS_TRACE_BAD_BLOCKS,
8718 + (TSTR("block %d is bad" TENDSTR), blk));
8719 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
8720 + T(YAFFS_TRACE_SCAN_DEBUG,
8721 + (TSTR("Block empty " TENDSTR)));
8722 + dev->nErasedBlocks++;
8723 + dev->nFreeChunks += dev->nChunksPerBlock;
8724 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8726 + /* Determine the highest sequence number */
8727 + if (dev->isYaffs2 &&
8728 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
8729 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
8731 + blockIndex[nBlocksToScan].seq = sequenceNumber;
8732 + blockIndex[nBlocksToScan].block = blk;
8736 + if (sequenceNumber >= dev->sequenceNumber) {
8737 + dev->sequenceNumber = sequenceNumber;
8739 + } else if (dev->isYaffs2) {
8740 + /* TODO: Nasty sequence number! */
8741 + T(YAFFS_TRACE_SCAN,
8743 + ("Block scanning block %d has bad sequence number %d"
8744 + TENDSTR), blk, sequenceNumber));
8750 + T(YAFFS_TRACE_SCAN,
8751 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
8757 + /* Sort the blocks */
8758 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
8760 + /* Use qsort now. */
8761 + qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
8765 + /* Dungy old bubble sort... */
8767 + yaffs_BlockIndex temp;
8771 + for (i = 0; i < nBlocksToScan; i++)
8772 + for (j = i + 1; j < nBlocksToScan; j++)
8773 + if (blockIndex[i].seq > blockIndex[j].seq) {
8774 + temp = blockIndex[j];
8775 + blockIndex[j] = blockIndex[i];
8776 + blockIndex[i] = temp;
8783 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
8785 + /* Now scan the blocks looking at the data. */
8786 + startIterator = 0;
8787 + endIterator = nBlocksToScan - 1;
8788 + T(YAFFS_TRACE_SCAN_DEBUG,
8789 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
8791 + /* For each block.... backwards */
8792 + for (blockIterator = endIterator; blockIterator >= startIterator;
8793 + blockIterator--) {
8794 + /* Cooperative multitasking! This loop can run for so
8795 + long that watchdog timers expire. */
8798 + /* get the block to scan in the correct order */
8799 + blk = blockIndex[blockIterator].block;
8801 + bi = yaffs_GetBlockInfo(dev, blk);
8802 + state = bi->blockState;
8806 + /* For each chunk in each block that needs scanning.... */
8807 + foundChunksInBlock = 0;
8808 + for (c = dev->nChunksPerBlock - 1; c >= 0 &&
8809 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
8810 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
8811 + /* Scan backwards...
8812 + * Read the tags and decide what to do
8814 + chunk = blk * dev->nChunksPerBlock + c;
8816 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
8819 + /* Let's have a good look at this chunk... */
8821 + if (!tags.chunkUsed) {
8822 + /* An unassigned chunk in the block.
8823 + * If there are used chunks after this one, then
8824 + * it is a chunk that was skipped due to failing the erased
8825 + * check. Just skip it so that it can be deleted.
8826 + * But, more typically, We get here when this is an unallocated
8827 + * chunk and his means that either the block is empty or
8828 + * this is the one being allocated from
8831 + if(foundChunksInBlock)
8833 + /* This is a chunk that was skipped due to failing the erased check */
8835 + } else if (c == 0) {
8836 + /* We're looking at the first chunk in the block so the block is unused */
8837 + state = YAFFS_BLOCK_STATE_EMPTY;
8838 + dev->nErasedBlocks++;
8840 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
8841 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
8842 + if(dev->sequenceNumber == bi->sequenceNumber) {
8843 + /* this is the block being allocated from */
8845 + T(YAFFS_TRACE_SCAN,
8847 + (" Allocating from %d %d"
8848 + TENDSTR), blk, c));
8850 + state = YAFFS_BLOCK_STATE_ALLOCATING;
8851 + dev->allocationBlock = blk;
8852 + dev->allocationPage = c;
8853 + dev->allocationBlockFinder = blk;
8856 + /* This is a partially written block that is not
8857 + * the current allocation block. This block must have
8858 + * had a write failure, so set up for retirement.
8861 + bi->needsRetiring = 1;
8862 + bi->gcPrioritise = 1;
8864 + T(YAFFS_TRACE_ALWAYS,
8865 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
8873 + dev->nFreeChunks++;
8875 + } else if (tags.chunkId > 0) {
8876 + /* chunkId > 0 so it is a data chunk... */
8877 + unsigned int endpos;
8879 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
8881 + foundChunksInBlock = 1;
8884 + yaffs_SetChunkBit(dev, blk, c);
8887 + in = yaffs_FindOrCreateObjectByNumber(dev,
8890 + YAFFS_OBJECT_TYPE_FILE);
8891 + if (in->variantType == YAFFS_OBJECT_TYPE_FILE
8893 + in->variant.fileVariant.shrinkSize) {
8894 + /* This has not been invalidated by a resize */
8895 + yaffs_PutChunkIntoFile(in, tags.chunkId,
8898 + /* File size is calculated by looking at the data chunks if we have not
8899 + * seen an object header yet. Stop this practice once we find an object header.
8903 + 1) * dev->nDataBytesPerChunk +
8906 + if (!in->valid && /* have not got an object header yet */
8907 + in->variant.fileVariant.
8908 + scannedFileSize < endpos) {
8909 + in->variant.fileVariant.
8910 + scannedFileSize = endpos;
8911 + in->variant.fileVariant.
8913 + in->variant.fileVariant.
8918 + /* This chunk has been invalidated by a resize, so delete */
8919 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
8923 + /* chunkId == 0, so it is an ObjectHeader.
8924 + * Thus, we read in the object header and make the object
8926 + foundChunksInBlock = 1;
8928 + yaffs_SetChunkBit(dev, blk, c);
8934 + if (tags.extraHeaderInfoAvailable) {
8935 + in = yaffs_FindOrCreateObjectByNumber
8936 + (dev, tags.objectId,
8937 + tags.extraObjectType);
8941 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
8944 + tags.extraShadows ||
8946 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
8947 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
8950 + /* If we don't have valid info then we need to read the chunk
8951 + * TODO In future we can probably defer reading the chunk and
8952 + * living with invalid data until needed.
8955 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
8960 + oh = (yaffs_ObjectHeader *) chunkData;
8963 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
8968 + /* TODO Hoosterman we have a problem! */
8969 + T(YAFFS_TRACE_ERROR,
8971 + ("yaffs tragedy: Could not make object for object %d "
8972 + "at chunk %d during scan"
8973 + TENDSTR), tags.objectId, chunk));
8978 + /* We have already filled this one.
8979 + * We have a duplicate that will be discarded, but
8980 + * we first have to suck out resize info if it is a file.
8983 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
8985 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
8986 + (tags.extraHeaderInfoAvailable &&
8987 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
8990 + (oh) ? oh->fileSize : tags.
8992 + __u32 parentObjectId =
8994 + parentObjectId : tags.
8995 + extraParentObjectId;
8996 + unsigned isShrink =
8997 + (oh) ? oh->isShrink : tags.
8998 + extraIsShrinkHeader;
9000 + /* If it is deleted (unlinked at start also means deleted)
9001 + * we treat the file size as being zeroed at this point.
9003 + if (parentObjectId ==
9004 + YAFFS_OBJECTID_DELETED
9005 + || parentObjectId ==
9006 + YAFFS_OBJECTID_UNLINKED) {
9012 + in->variant.fileVariant.
9013 + shrinkSize > thisSize) {
9014 + in->variant.fileVariant.
9020 + bi->hasShrinkHeader = 1;
9024 + /* Use existing - destroy this one. */
9025 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9030 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9032 + YAFFS_OBJECTID_LOSTNFOUND)) {
9033 + /* We only load some info, don't fiddle with directory structure */
9037 + in->variantType = oh->type;
9039 + in->yst_mode = oh->yst_mode;
9040 +#ifdef CONFIG_YAFFS_WINCE
9041 + in->win_atime[0] = oh->win_atime[0];
9042 + in->win_ctime[0] = oh->win_ctime[0];
9043 + in->win_mtime[0] = oh->win_mtime[0];
9044 + in->win_atime[1] = oh->win_atime[1];
9045 + in->win_ctime[1] = oh->win_ctime[1];
9046 + in->win_mtime[1] = oh->win_mtime[1];
9048 + in->yst_uid = oh->yst_uid;
9049 + in->yst_gid = oh->yst_gid;
9050 + in->yst_atime = oh->yst_atime;
9051 + in->yst_mtime = oh->yst_mtime;
9052 + in->yst_ctime = oh->yst_ctime;
9053 + in->yst_rdev = oh->yst_rdev;
9057 + in->variantType = tags.extraObjectType;
9058 + in->lazyLoaded = 1;
9061 + in->chunkId = chunk;
9063 + } else if (!in->valid) {
9064 + /* we need to load this info */
9067 + in->chunkId = chunk;
9070 + in->variantType = oh->type;
9072 + in->yst_mode = oh->yst_mode;
9073 +#ifdef CONFIG_YAFFS_WINCE
9074 + in->win_atime[0] = oh->win_atime[0];
9075 + in->win_ctime[0] = oh->win_ctime[0];
9076 + in->win_mtime[0] = oh->win_mtime[0];
9077 + in->win_atime[1] = oh->win_atime[1];
9078 + in->win_ctime[1] = oh->win_ctime[1];
9079 + in->win_mtime[1] = oh->win_mtime[1];
9081 + in->yst_uid = oh->yst_uid;
9082 + in->yst_gid = oh->yst_gid;
9083 + in->yst_atime = oh->yst_atime;
9084 + in->yst_mtime = oh->yst_mtime;
9085 + in->yst_ctime = oh->yst_ctime;
9086 + in->yst_rdev = oh->yst_rdev;
9089 + if (oh->shadowsObject > 0)
9090 + yaffs_HandleShadowedObject(dev,
9096 + yaffs_SetObjectName(in, oh->name);
9098 + yaffs_FindOrCreateObjectByNumber
9099 + (dev, oh->parentObjectId,
9100 + YAFFS_OBJECT_TYPE_DIRECTORY);
9102 + fileSize = oh->fileSize;
9103 + isShrink = oh->isShrink;
9104 + equivalentObjectId = oh->equivalentObjectId;
9108 + in->variantType = tags.extraObjectType;
9110 + yaffs_FindOrCreateObjectByNumber
9111 + (dev, tags.extraParentObjectId,
9112 + YAFFS_OBJECT_TYPE_DIRECTORY);
9113 + fileSize = tags.extraFileLength;
9114 + isShrink = tags.extraIsShrinkHeader;
9115 + equivalentObjectId = tags.extraEquivalentObjectId;
9116 + in->lazyLoaded = 1;
9121 + /* directory stuff...
9122 + * hook up to parent
9125 + if (parent->variantType ==
9126 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9127 + /* Set up as a directory */
9128 + parent->variantType =
9129 + YAFFS_OBJECT_TYPE_DIRECTORY;
9130 + INIT_LIST_HEAD(&parent->variant.
9133 + } else if (parent->variantType !=
9134 + YAFFS_OBJECT_TYPE_DIRECTORY)
9136 + /* Hoosterman, another problem....
9137 + * We're trying to use a non-directory as a directory
9140 + T(YAFFS_TRACE_ERROR,
9142 + ("yaffs tragedy: attempting to use non-directory as"
9143 + " a directory in scan. Put in lost+found."
9145 + parent = dev->lostNFoundDir;
9148 + yaffs_AddObjectToDirectory(parent, in);
9150 + itsUnlinked = (parent == dev->deletedDir) ||
9151 + (parent == dev->unlinkedDir);
9154 + /* Mark the block as having a shrinkHeader */
9155 + bi->hasShrinkHeader = 1;
9158 + /* Note re hardlinks.
9159 + * Since we might scan a hardlink before its equivalent object is scanned
9160 + * we put them all in a list.
9161 + * After scanning is complete, we should have all the objects, so we run
9162 + * through this list and fix up all the chains.
9165 + switch (in->variantType) {
9166 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9167 + /* Todo got a problem */
9169 + case YAFFS_OBJECT_TYPE_FILE:
9171 + if (in->variant.fileVariant.
9172 + scannedFileSize < fileSize) {
9173 + /* This covers the case where the file size is greater
9174 + * than where the data is
9175 + * This will happen if the file is resized to be larger
9176 + * than its current data extents.
9178 + in->variant.fileVariant.fileSize = fileSize;
9179 + in->variant.fileVariant.scannedFileSize =
9180 + in->variant.fileVariant.fileSize;
9184 + in->variant.fileVariant.shrinkSize > fileSize) {
9185 + in->variant.fileVariant.shrinkSize = fileSize;
9189 + case YAFFS_OBJECT_TYPE_HARDLINK:
9190 + if(!itsUnlinked) {
9191 + in->variant.hardLinkVariant.equivalentObjectId =
9192 + equivalentObjectId;
9193 + in->hardLinks.next =
9194 + (struct list_head *) hardList;
9198 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9201 + case YAFFS_OBJECT_TYPE_SPECIAL:
9204 + case YAFFS_OBJECT_TYPE_SYMLINK:
9206 + in->variant.symLinkVariant.alias =
9207 + yaffs_CloneString(oh->
9216 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9217 + /* If we got this far while scanning, then the block is fully allocated. */
9218 + state = YAFFS_BLOCK_STATE_FULL;
9221 + bi->blockState = state;
9223 + /* Now let's see if it was dirty */
9224 + if (bi->pagesInUse == 0 &&
9225 + !bi->hasShrinkHeader &&
9226 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9227 + yaffs_BlockBecameDirty(dev, blk);
9232 + if (altBlockIndex)
9233 + YFREE_ALT(blockIndex);
9235 + YFREE(blockIndex);
9237 + /* Ok, we've done all the scanning.
9238 + * Fix up the hard link chains.
9239 + * We should now have scanned all the objects, now it's time to add these
9242 + yaffs_HardlinkFixup(dev,hardList);
9246 + * Sort out state of unlinked and deleted objects.
9249 + struct list_head *i;
9250 + struct list_head *n;
9254 + /* Soft delete all the unlinked files */
9255 + list_for_each_safe(i, n,
9256 + &dev->unlinkedDir->variant.directoryVariant.
9259 + l = list_entry(i, yaffs_Object, siblings);
9260 + yaffs_DestroyObject(l);
9264 + /* Soft delete all the deletedDir files */
9265 + list_for_each_safe(i, n,
9266 + &dev->deletedDir->variant.directoryVariant.
9269 + l = list_entry(i, yaffs_Object, siblings);
9270 + yaffs_DestroyObject(l);
9276 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9278 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
9283 +/*------------------------------ Directory Functions ----------------------------- */
9285 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
9287 + yaffs_Device *dev = obj->myDev;
9289 + if(dev && dev->removeObjectCallback)
9290 + dev->removeObjectCallback(obj);
9292 + list_del_init(&obj->siblings);
9293 + obj->parent = NULL;
9297 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
9298 + yaffs_Object * obj)
9302 + T(YAFFS_TRACE_ALWAYS,
9304 + ("tragedy: Trying to add an object to a null pointer directory"
9308 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9309 + T(YAFFS_TRACE_ALWAYS,
9311 + ("tragedy: Trying to add an object to a non-directory"
9316 + if (obj->siblings.prev == NULL) {
9317 + /* Not initialised */
9318 + INIT_LIST_HEAD(&obj->siblings);
9320 + } else if (!list_empty(&obj->siblings)) {
9321 + /* If it is holed up somewhere else, un hook it */
9322 + yaffs_RemoveObjectFromDirectory(obj);
9325 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
9326 + obj->parent = directory;
9328 + if (directory == obj->myDev->unlinkedDir
9329 + || directory == obj->myDev->deletedDir) {
9330 + obj->unlinked = 1;
9331 + obj->myDev->nUnlinkedFiles++;
9332 + obj->renameAllowed = 0;
9336 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
9337 + const YCHAR * name)
9341 + struct list_head *i;
9342 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
9351 + T(YAFFS_TRACE_ALWAYS,
9353 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
9357 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9358 + T(YAFFS_TRACE_ALWAYS,
9360 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
9364 + sum = yaffs_CalcNameSum(name);
9366 + list_for_each(i, &directory->variant.directoryVariant.children) {
9368 + l = list_entry(i, yaffs_Object, siblings);
9370 + yaffs_CheckObjectDetailsLoaded(l);
9372 + /* Special case for lost-n-found */
9373 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
9374 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
9377 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
9379 + /* LostnFound cunk called Objxxx
9382 + yaffs_GetObjectName(l, buffer,
9383 + YAFFS_MAX_NAME_LENGTH);
9384 + if (yaffs_strcmp(name, buffer) == 0) {
9397 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
9398 + int (*fn) (yaffs_Object *))
9400 + struct list_head *i;
9404 + T(YAFFS_TRACE_ALWAYS,
9406 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
9410 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9411 + T(YAFFS_TRACE_ALWAYS,
9413 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
9417 + list_for_each(i, &theDir->variant.directoryVariant.children) {
9419 + l = list_entry(i, yaffs_Object, siblings);
9420 + if (l && !fn(l)) {
9421 + return YAFFS_FAIL;
9431 +/* GetEquivalentObject dereferences any hard links to get to the
9435 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
9437 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
9438 + /* We want the object id of the equivalent object, not this one */
9439 + obj = obj->variant.hardLinkVariant.equivalentObject;
9445 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
9447 + memset(name, 0, buffSize * sizeof(YCHAR));
9449 + yaffs_CheckObjectDetailsLoaded(obj);
9451 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
9452 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
9453 + } else if (obj->chunkId <= 0) {
9454 + YCHAR locName[20];
9455 + /* make up a name */
9456 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
9458 + yaffs_strncpy(name, locName, buffSize - 1);
9461 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
9462 + else if (obj->shortName[0]) {
9463 + yaffs_strcpy(name, obj->shortName);
9468 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
9470 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
9472 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
9474 + if (obj->chunkId >= 0) {
9475 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
9476 + obj->chunkId, buffer,
9479 + yaffs_strncpy(name, oh->name, buffSize - 1);
9481 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
9484 + return yaffs_strlen(name);
9487 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
9490 + /* Dereference any hard linking */
9491 + obj = yaffs_GetEquivalentObject(obj);
9493 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
9494 + return obj->variant.fileVariant.fileSize;
9496 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
9497 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
9499 + /* Only a directory should drop through to here */
9500 + return obj->myDev->nDataBytesPerChunk;
9504 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
9507 + struct list_head *i;
9509 + if (!obj->unlinked) {
9510 + count++; /* the object itself */
9512 + list_for_each(i, &obj->hardLinks) {
9513 + count++; /* add the hard links; */
9519 +int yaffs_GetObjectInode(yaffs_Object * obj)
9521 + obj = yaffs_GetEquivalentObject(obj);
9523 + return obj->objectId;
9526 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
9528 + obj = yaffs_GetEquivalentObject(obj);
9530 + switch (obj->variantType) {
9531 + case YAFFS_OBJECT_TYPE_FILE:
9534 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9537 + case YAFFS_OBJECT_TYPE_SYMLINK:
9540 + case YAFFS_OBJECT_TYPE_HARDLINK:
9543 + case YAFFS_OBJECT_TYPE_SPECIAL:
9544 + if (S_ISFIFO(obj->yst_mode))
9546 + if (S_ISCHR(obj->yst_mode))
9548 + if (S_ISBLK(obj->yst_mode))
9550 + if (S_ISSOCK(obj->yst_mode))
9558 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
9560 + obj = yaffs_GetEquivalentObject(obj);
9561 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
9562 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
9564 + return yaffs_CloneString(_Y(""));
9568 +#ifndef CONFIG_YAFFS_WINCE
9570 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
9572 + unsigned int valid = attr->ia_valid;
9574 + if (valid & ATTR_MODE)
9575 + obj->yst_mode = attr->ia_mode;
9576 + if (valid & ATTR_UID)
9577 + obj->yst_uid = attr->ia_uid;
9578 + if (valid & ATTR_GID)
9579 + obj->yst_gid = attr->ia_gid;
9581 + if (valid & ATTR_ATIME)
9582 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
9583 + if (valid & ATTR_CTIME)
9584 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
9585 + if (valid & ATTR_MTIME)
9586 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
9588 + if (valid & ATTR_SIZE)
9589 + yaffs_ResizeFile(obj, attr->ia_size);
9591 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
9596 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
9598 + unsigned int valid = 0;
9600 + attr->ia_mode = obj->yst_mode;
9601 + valid |= ATTR_MODE;
9602 + attr->ia_uid = obj->yst_uid;
9603 + valid |= ATTR_UID;
9604 + attr->ia_gid = obj->yst_gid;
9605 + valid |= ATTR_GID;
9607 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
9608 + valid |= ATTR_ATIME;
9609 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
9610 + valid |= ATTR_CTIME;
9611 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
9612 + valid |= ATTR_MTIME;
9614 + attr->ia_size = yaffs_GetFileSize(obj);
9615 + valid |= ATTR_SIZE;
9617 + attr->ia_valid = valid;
9626 +int yaffs_DumpObject(yaffs_Object * obj)
9630 + yaffs_GetObjectName(obj, name, 256);
9632 + T(YAFFS_TRACE_ALWAYS,
9634 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
9635 + " chunk %d type %d size %d\n"
9636 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
9637 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
9638 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
9644 +/*---------------------------- Initialisation code -------------------------------------- */
9646 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
9649 + /* Common functions, gotta have */
9650 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
9653 +#ifdef CONFIG_YAFFS_YAFFS2
9655 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
9656 + if (dev->writeChunkWithTagsToNAND &&
9657 + dev->readChunkWithTagsFromNAND &&
9658 + !dev->writeChunkToNAND &&
9659 + !dev->readChunkFromNAND &&
9660 + dev->markNANDBlockBad && dev->queryNANDBlock)
9664 + /* Can use the "spare" style interface for yaffs1 */
9665 + if (!dev->isYaffs2 &&
9666 + !dev->writeChunkWithTagsToNAND &&
9667 + !dev->readChunkWithTagsFromNAND &&
9668 + dev->writeChunkToNAND &&
9669 + dev->readChunkFromNAND &&
9670 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
9673 + return 0; /* bad */
9677 +static void yaffs_CreateInitialDirectories(yaffs_Device *dev)
9679 + /* Initialise the unlinked, deleted, root and lost and found directories */
9681 + dev->lostNFoundDir = dev->rootDir = NULL;
9682 + dev->unlinkedDir = dev->deletedDir = NULL;
9684 + dev->unlinkedDir =
9685 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
9687 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
9690 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
9691 + YAFFS_ROOT_MODE | S_IFDIR);
9692 + dev->lostNFoundDir =
9693 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
9694 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
9695 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
9698 +int yaffs_GutsInitialise(yaffs_Device * dev)
9703 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
9705 + /* Check stuff that must be set */
9708 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
9709 + return YAFFS_FAIL;
9712 + dev->internalStartBlock = dev->startBlock;
9713 + dev->internalEndBlock = dev->endBlock;
9714 + dev->blockOffset = 0;
9715 + dev->chunkOffset = 0;
9716 + dev->nFreeChunks = 0;
9718 + if (dev->startBlock == 0) {
9719 + dev->internalStartBlock = dev->startBlock + 1;
9720 + dev->internalEndBlock = dev->endBlock + 1;
9721 + dev->blockOffset = 1;
9722 + dev->chunkOffset = dev->nChunksPerBlock;
9725 + /* Check geometry parameters. */
9727 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
9728 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
9729 + dev->nChunksPerBlock < 2 ||
9730 + dev->nReservedBlocks < 2 ||
9731 + dev->internalStartBlock <= 0 ||
9732 + dev->internalEndBlock <= 0 ||
9733 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
9735 + T(YAFFS_TRACE_ALWAYS,
9737 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
9738 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
9739 + return YAFFS_FAIL;
9742 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
9743 + T(YAFFS_TRACE_ALWAYS,
9744 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
9745 + return YAFFS_FAIL;
9748 + /* Got the right mix of functions? */
9749 + if (!yaffs_CheckDevFunctions(dev)) {
9750 + /* Function missing */
9751 + T(YAFFS_TRACE_ALWAYS,
9753 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
9755 + return YAFFS_FAIL;
9758 + /* This is really a compilation check. */
9759 + if (!yaffs_CheckStructures()) {
9760 + T(YAFFS_TRACE_ALWAYS,
9761 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
9762 + return YAFFS_FAIL;
9765 + if (dev->isMounted) {
9766 + T(YAFFS_TRACE_ALWAYS,
9767 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
9768 + return YAFFS_FAIL;
9771 + /* Finished with most checks. One or two more checks happen later on too. */
9773 + dev->isMounted = 1;
9777 + /* OK now calculate a few things for the device */
9780 + * Calculate all the chunk size manipulation numbers:
9782 + /* Start off assuming it is a power of 2 */
9783 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
9784 + dev->chunkMask = (1<<dev->chunkShift) - 1;
9786 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
9787 + /* Yes it is a power of 2, disable crumbs */
9788 + dev->crumbMask = 0;
9789 + dev->crumbShift = 0;
9790 + dev->crumbsPerChunk = 0;
9792 + /* Not a power of 2, use crumbs instead */
9793 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
9794 + dev->crumbMask = (1<<dev->crumbShift)-1;
9795 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
9796 + dev->chunkShift = 0;
9797 + dev->chunkMask = 0;
9802 + * Calculate chunkGroupBits.
9803 + * We need to find the next power of 2 > than internalEndBlock
9806 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
9808 + bits = ShiftsGE(x);
9810 + /* Set up tnode width if wide tnodes are enabled. */
9811 + if(!dev->wideTnodesDisabled){
9812 + /* bits must be even so that we end up with 32-bit words */
9816 + dev->tnodeWidth = 16;
9818 + dev->tnodeWidth = bits;
9821 + dev->tnodeWidth = 16;
9823 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
9825 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
9826 + * so if the bitwidth of the
9827 + * chunk range we're using is greater than 16 we need
9828 + * to figure out chunk shift and chunkGroupSize
9831 + if (bits <= dev->tnodeWidth)
9832 + dev->chunkGroupBits = 0;
9834 + dev->chunkGroupBits = bits - dev->tnodeWidth;
9837 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
9839 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
9840 + /* We have a problem because the soft delete won't work if
9841 + * the chunk group size > chunks per block.
9842 + * This can be remedied by using larger "virtual blocks".
9844 + T(YAFFS_TRACE_ALWAYS,
9845 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
9847 + return YAFFS_FAIL;
9850 + /* OK, we've finished verifying the device, lets continue with initialisation */
9852 + /* More device initialisation */
9853 + dev->garbageCollections = 0;
9854 + dev->passiveGarbageCollections = 0;
9855 + dev->currentDirtyChecker = 0;
9856 + dev->bufferedBlock = -1;
9857 + dev->doingBufferedBlockRewrite = 0;
9858 + dev->nDeletedFiles = 0;
9859 + dev->nBackgroundDeletions = 0;
9860 + dev->nUnlinkedFiles = 0;
9861 + dev->eccFixed = 0;
9862 + dev->eccUnfixed = 0;
9863 + dev->tagsEccFixed = 0;
9864 + dev->tagsEccUnfixed = 0;
9865 + dev->nErasureFailures = 0;
9866 + dev->nErasedBlocks = 0;
9867 + dev->isDoingGC = 0;
9868 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
9870 + /* Initialise temporary buffers and caches. */
9873 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
9874 + dev->tempBuffer[i].line = 0; /* not in use */
9875 + dev->tempBuffer[i].buffer =
9876 + YMALLOC_DMA(dev->nDataBytesPerChunk);
9880 + if (dev->nShortOpCaches > 0) {
9883 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
9884 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
9888 + YMALLOC(dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
9890 + for (i = 0; i < dev->nShortOpCaches; i++) {
9891 + dev->srCache[i].object = NULL;
9892 + dev->srCache[i].lastUse = 0;
9893 + dev->srCache[i].dirty = 0;
9894 + dev->srCache[i].data = YMALLOC_DMA(dev->nDataBytesPerChunk);
9896 + dev->srLastUse = 0;
9899 + dev->cacheHits = 0;
9901 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
9903 + if (dev->isYaffs2) {
9904 + dev->useHeaderFileSize = 1;
9907 + yaffs_InitialiseBlocks(dev);
9908 + yaffs_InitialiseTnodes(dev);
9909 + yaffs_InitialiseObjects(dev);
9911 + yaffs_CreateInitialDirectories(dev);
9914 + /* Now scan the flash. */
9915 + if (dev->isYaffs2) {
9916 + if(yaffs_CheckpointRestore(dev)) {
9917 + T(YAFFS_TRACE_CHECKPOINT,
9918 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
9921 + /* Clean up the mess caused by an aborted checkpoint load
9922 + * and scan backwards.
9924 + yaffs_DeinitialiseBlocks(dev);
9925 + yaffs_DeinitialiseTnodes(dev);
9926 + yaffs_DeinitialiseObjects(dev);
9927 + yaffs_InitialiseBlocks(dev);
9928 + yaffs_InitialiseTnodes(dev);
9929 + yaffs_InitialiseObjects(dev);
9930 + yaffs_CreateInitialDirectories(dev);
9932 + yaffs_ScanBackwards(dev);
9937 + /* Zero out stats */
9938 + dev->nPageReads = 0;
9939 + dev->nPageWrites = 0;
9940 + dev->nBlockErasures = 0;
9941 + dev->nGCCopies = 0;
9942 + dev->nRetriedWrites = 0;
9944 + dev->nRetiredBlocks = 0;
9946 + yaffs_VerifyFreeChunks(dev);
9948 + T(YAFFS_TRACE_TRACING,
9949 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
9954 +void yaffs_Deinitialise(yaffs_Device * dev)
9956 + if (dev->isMounted) {
9959 + yaffs_DeinitialiseBlocks(dev);
9960 + yaffs_DeinitialiseTnodes(dev);
9961 + yaffs_DeinitialiseObjects(dev);
9962 + if (dev->nShortOpCaches > 0) {
9964 + for (i = 0; i < dev->nShortOpCaches; i++) {
9965 + YFREE(dev->srCache[i].data);
9968 + YFREE(dev->srCache);
9971 + YFREE(dev->gcCleanupList);
9973 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
9974 + YFREE(dev->tempBuffer[i].buffer);
9977 + dev->isMounted = 0;
9982 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
9987 + yaffs_BlockInfo *blk;
9989 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
9991 + blk = yaffs_GetBlockInfo(dev, b);
9993 + switch (blk->blockState) {
9994 + case YAFFS_BLOCK_STATE_EMPTY:
9995 + case YAFFS_BLOCK_STATE_ALLOCATING:
9996 + case YAFFS_BLOCK_STATE_COLLECTING:
9997 + case YAFFS_BLOCK_STATE_FULL:
9999 + (dev->nChunksPerBlock - blk->pagesInUse +
10000 + blk->softDeletions);
10011 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
10013 + /* This is what we report to the outside world */
10016 + int nDirtyCacheChunks;
10017 + int blocksForCheckpoint;
10020 + nFree = dev->nFreeChunks;
10022 + nFree = yaffs_CountFreeChunks(dev);
10025 + nFree += dev->nDeletedFiles;
10027 + /* Now count the number of dirty chunks in the cache and subtract those */
10031 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
10032 + if (dev->srCache[i].dirty)
10033 + nDirtyCacheChunks++;
10037 + nFree -= nDirtyCacheChunks;
10039 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
10041 + /* Now we figure out how much to reserve for the checkpoint and report that... */
10042 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
10043 + if(blocksForCheckpoint < 0)
10044 + blocksForCheckpoint = 0;
10046 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
10055 +static int yaffs_freeVerificationFailures;
10057 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
10059 + int counted = yaffs_CountFreeChunks(dev);
10061 + int difference = dev->nFreeChunks - counted;
10063 + if (difference) {
10064 + T(YAFFS_TRACE_ALWAYS,
10065 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
10066 + dev->nFreeChunks, counted, difference));
10067 + yaffs_freeVerificationFailures++;
10071 +/*---------------------------------------- YAFFS test code ----------------------*/
10073 +#define yaffs_CheckStruct(structure,syze, name) \
10074 + if(sizeof(structure) != syze) \
10076 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
10077 + name,syze,sizeof(structure))); \
10078 + return YAFFS_FAIL; \
10081 +static int yaffs_CheckStructures(void)
10083 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
10084 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
10085 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
10086 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
10087 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
10089 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
10093 diff -urN linux.old/fs/yaffs2/yaffs_guts.h linux.dev/fs/yaffs2/yaffs_guts.h
10094 --- linux.old/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
10095 +++ linux.dev/fs/yaffs2/yaffs_guts.h 2006-12-14 04:21:47.000000000 +0100
10098 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
10099 + * yaffs_guts.h: Configuration etc for yaffs_guts
10101 + * Copyright (C) 2002 Aleph One Ltd.
10102 + * for Toby Churchill Ltd and Brightstar Engineering
10104 + * Created by Charles Manning <charles@aleph1.co.uk>
10106 + * This program is free software; you can redistribute it and/or modify
10107 + * it under the terms of the GNU Lesser General Public License version 2.1 as
10108 + * published by the Free Software Foundation.
10111 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
10113 + * $Id: yaffs_guts.h,v 1.25 2006/10/13 08:52:49 charles Exp $
10116 +#ifndef __YAFFS_GUTS_H__
10117 +#define __YAFFS_GUTS_H__
10119 +#include "devextras.h"
10120 +#include "yportenv.h"
10122 +#define YAFFS_OK 1
10123 +#define YAFFS_FAIL 0
10125 +/* Give us a Y=0x59,
10126 + * Give us an A=0x41,
10127 + * Give us an FF=0xFF
10128 + * Give us an S=0x53
10129 + * And what have we got...
10131 +#define YAFFS_MAGIC 0x5941FF53
10133 +#define YAFFS_NTNODES_LEVEL0 16
10134 +#define YAFFS_TNODES_LEVEL0_BITS 4
10135 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
10137 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
10138 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
10139 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
10140 +#define YAFFS_TNODES_MAX_LEVEL 6
10142 +#ifndef CONFIG_YAFFS_NO_YAFFS1
10143 +#define YAFFS_BYTES_PER_SPARE 16
10144 +#define YAFFS_BYTES_PER_CHUNK 512
10145 +#define YAFFS_CHUNK_SIZE_SHIFT 9
10146 +#define YAFFS_CHUNKS_PER_BLOCK 32
10147 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
10150 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
10151 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
10153 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
10155 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
10157 +#define YAFFS_ALLOCATION_NOBJECTS 100
10158 +#define YAFFS_ALLOCATION_NTNODES 100
10159 +#define YAFFS_ALLOCATION_NLINKS 100
10161 +#define YAFFS_NOBJECT_BUCKETS 256
10164 +#define YAFFS_OBJECT_SPACE 0x40000
10166 +#define YAFFS_NCHECKPOINT_OBJECTS 5000
10168 +#define YAFFS_CHECKPOINT_VERSION 2
10170 +#ifdef CONFIG_YAFFS_UNICODE
10171 +#define YAFFS_MAX_NAME_LENGTH 127
10172 +#define YAFFS_MAX_ALIAS_LENGTH 79
10174 +#define YAFFS_MAX_NAME_LENGTH 255
10175 +#define YAFFS_MAX_ALIAS_LENGTH 159
10178 +#define YAFFS_SHORT_NAME_LENGTH 15
10180 +/* Some special object ids for pseudo objects */
10181 +#define YAFFS_OBJECTID_ROOT 1
10182 +#define YAFFS_OBJECTID_LOSTNFOUND 2
10183 +#define YAFFS_OBJECTID_UNLINKED 3
10184 +#define YAFFS_OBJECTID_DELETED 4
10186 +/* Sseudo object ids for checkpointing */
10187 +#define YAFFS_OBJECTID_SB_HEADER 0x10
10188 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
10189 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
10193 +#define YAFFS_MAX_SHORT_OP_CACHES 20
10195 +#define YAFFS_N_TEMP_BUFFERS 4
10197 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
10198 + * The range is limited slightly to help distinguish bad numbers from good.
10199 + * This also allows us to perhaps in the future use special numbers for
10200 + * special purposes.
10201 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
10202 + * and is a larger number than the lifetime of a 2GB device.
10204 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
10205 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
10207 +/* ChunkCache is used for short read/write operations.*/
10209 + struct yaffs_ObjectStruct *object;
10213 + int nBytes; /* Only valid if the cache is dirty */
10214 + int locked; /* Can't push out or flush while locked. */
10215 +#ifdef CONFIG_YAFFS_YAFFS2
10218 + __u8 data[YAFFS_BYTES_PER_CHUNK];
10220 +} yaffs_ChunkCache;
10224 +/* Tags structures in RAM
10225 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
10226 + * the structure size will get blown out.
10229 +#ifndef CONFIG_YAFFS_NO_YAFFS1
10231 + unsigned chunkId:20;
10232 + unsigned serialNumber:2;
10233 + unsigned byteCount:10;
10234 + unsigned objectId:18;
10236 + unsigned unusedStuff:2;
10241 + yaffs_Tags asTags;
10243 +} yaffs_TagsUnion;
10247 +/* Stuff used for extended tags in YAFFS2 */
10250 + YAFFS_ECC_RESULT_UNKNOWN,
10251 + YAFFS_ECC_RESULT_NO_ERROR,
10252 + YAFFS_ECC_RESULT_FIXED,
10253 + YAFFS_ECC_RESULT_UNFIXED
10254 +} yaffs_ECCResult;
10257 + YAFFS_OBJECT_TYPE_UNKNOWN,
10258 + YAFFS_OBJECT_TYPE_FILE,
10259 + YAFFS_OBJECT_TYPE_SYMLINK,
10260 + YAFFS_OBJECT_TYPE_DIRECTORY,
10261 + YAFFS_OBJECT_TYPE_HARDLINK,
10262 + YAFFS_OBJECT_TYPE_SPECIAL
10263 +} yaffs_ObjectType;
10267 + unsigned validMarker0;
10268 + unsigned chunkUsed; /* Status of the chunk: used or unused */
10269 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
10270 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
10271 + unsigned byteCount; /* Only valid for data chunks */
10273 + /* The following stuff only has meaning when we read */
10274 + yaffs_ECCResult eccResult;
10275 + unsigned blockBad;
10277 + /* YAFFS 1 stuff */
10278 + unsigned chunkDeleted; /* The chunk is marked deleted */
10279 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
10281 + /* YAFFS2 stuff */
10282 + unsigned sequenceNumber; /* The sequence number of this block */
10284 + /* Extra info if this is an object header (YAFFS2 only) */
10286 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
10287 + unsigned extraParentObjectId; /* The parent object */
10288 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
10289 + unsigned extraShadows; /* Does this shadow another object? */
10291 + yaffs_ObjectType extraObjectType; /* What object type? */
10293 + unsigned extraFileLength; /* Length if it is a file */
10294 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
10296 + unsigned validMarker1;
10298 +} yaffs_ExtendedTags;
10300 +/* Spare structure for YAFFS1 */
10306 + __u8 pageStatus; /* set to 0 to delete the chunk */
10307 + __u8 blockStatus;
10316 +/*Special structure for passing through to mtd */
10317 +struct yaffs_NANDSpare {
10318 + yaffs_Spare spare;
10323 +/* Block data in RAM */
10326 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
10328 + YAFFS_BLOCK_STATE_SCANNING,
10329 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
10330 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
10331 + * but it needs to be scanned to determine its true state.
10332 + * This state is only valid during yaffs_Scan.
10333 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
10334 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
10337 + YAFFS_BLOCK_STATE_EMPTY,
10338 + /* This block is empty */
10340 + YAFFS_BLOCK_STATE_ALLOCATING,
10341 + /* This block is partially allocated.
10342 + * At least one page holds valid data.
10343 + * This is the one currently being used for page
10344 + * allocation. Should never be more than one of these
10347 + YAFFS_BLOCK_STATE_FULL,
10348 + /* All the pages in this block have been allocated.
10351 + YAFFS_BLOCK_STATE_DIRTY,
10352 + /* All pages have been allocated and deleted.
10353 + * Erase me, reuse me.
10356 + YAFFS_BLOCK_STATE_CHECKPOINT,
10357 + /* This block is assigned to holding checkpoint data.
10360 + YAFFS_BLOCK_STATE_COLLECTING,
10361 + /* This block is being garbage collected */
10363 + YAFFS_BLOCK_STATE_DEAD
10364 + /* This block has failed and is not in use */
10365 +} yaffs_BlockState;
10369 + int softDeletions:10; /* number of soft deleted pages */
10370 + int pagesInUse:10; /* number of pages in use */
10371 + yaffs_BlockState blockState:4; /* One of the above block states */
10372 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
10373 + /* and retire the block. */
10374 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
10375 + __u32 gcPrioritise: 1; /* An ECC check or bank check has failed on this block.
10376 + It should be prioritised for GC */
10377 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
10379 +#ifdef CONFIG_YAFFS_YAFFS2
10380 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
10381 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
10384 +} yaffs_BlockInfo;
10386 +/* -------------------------- Object structure -------------------------------*/
10387 +/* This is the object structure as stored on NAND */
10390 + yaffs_ObjectType type;
10392 + /* Apply to everything */
10393 + int parentObjectId;
10394 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
10395 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
10397 + /* Thes following apply to directories, files, symlinks - not hard links */
10398 + __u32 yst_mode; /* protection */
10400 +#ifdef CONFIG_YAFFS_WINCE
10401 + __u32 notForWinCE[5];
10410 + /* File size applies to files only */
10413 + /* Equivalent object id applies to hard links only. */
10414 + int equivalentObjectId;
10416 + /* Alias is for symlinks only. */
10417 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
10419 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
10421 +#ifdef CONFIG_YAFFS_WINCE
10422 + __u32 win_ctime[2];
10423 + __u32 win_atime[2];
10424 + __u32 win_mtime[2];
10425 + __u32 roomToGrow[4];
10427 + __u32 roomToGrow[10];
10430 + int shadowsObject; /* This object header shadows the specified object if > 0 */
10432 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
10435 +} yaffs_ObjectHeader;
10437 +/*--------------------------- Tnode -------------------------- */
10439 +union yaffs_Tnode_union {
10440 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
10441 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
10443 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
10445 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
10449 +typedef union yaffs_Tnode_union yaffs_Tnode;
10451 +struct yaffs_TnodeList_struct {
10452 + struct yaffs_TnodeList_struct *next;
10453 + yaffs_Tnode *tnodes;
10456 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
10458 +/*------------------------ Object -----------------------------*/
10459 +/* An object can be one of:
10460 + * - a directory (no data, has children links
10461 + * - a regular file (data.... not prunes :->).
10462 + * - a symlink [symbolic link] (the alias).
10468 + __u32 scannedFileSize;
10469 + __u32 shrinkSize;
10471 + yaffs_Tnode *top;
10472 +} yaffs_FileStructure;
10475 + struct list_head children; /* list of child links */
10476 +} yaffs_DirectoryStructure;
10480 +} yaffs_SymLinkStructure;
10483 + struct yaffs_ObjectStruct *equivalentObject;
10484 + __u32 equivalentObjectId;
10485 +} yaffs_HardLinkStructure;
10488 + yaffs_FileStructure fileVariant;
10489 + yaffs_DirectoryStructure directoryVariant;
10490 + yaffs_SymLinkStructure symLinkVariant;
10491 + yaffs_HardLinkStructure hardLinkVariant;
10492 +} yaffs_ObjectVariant;
10494 +struct yaffs_ObjectStruct {
10495 + __u8 deleted:1; /* This should only apply to unlinked files. */
10496 + __u8 softDeleted:1; /* it has also been soft deleted */
10497 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
10498 + __u8 fake:1; /* A fake object has no presence on NAND. */
10499 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
10500 + __u8 unlinkAllowed:1;
10501 + __u8 dirty:1; /* the object needs to be written to flash */
10502 + __u8 valid:1; /* When the file system is being loaded up, this
10503 + * object might be created before the data
10504 + * is available (ie. file data records appear before the header).
10506 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
10508 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
10509 + * still in the inode cache. Free of object is defered.
10510 + * until the inode is released.
10513 + __u8 serial; /* serial number of chunk in NAND. Cached here */
10514 + __u16 sum; /* sum of the name to speed searching */
10516 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
10518 + struct list_head hashLink; /* list of objects in this hash bucket */
10520 + struct list_head hardLinks; /* all the equivalent hard linked objects */
10522 + /* directory structure stuff */
10523 + /* also used for linking up the free list */
10524 + struct yaffs_ObjectStruct *parent;
10525 + struct list_head siblings;
10527 + /* Where's my object header in NAND? */
10530 + int nDataChunks; /* Number of data chunks attached to the file. */
10532 + __u32 objectId; /* the object id value */
10536 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10537 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
10540 +#ifndef __KERNEL__
10544 +#ifdef CONFIG_YAFFS_WINCE
10545 + __u32 win_ctime[2];
10546 + __u32 win_mtime[2];
10547 + __u32 win_atime[2];
10559 + struct inode *myInode;
10563 + yaffs_ObjectType variantType;
10565 + yaffs_ObjectVariant variant;
10569 +typedef struct yaffs_ObjectStruct yaffs_Object;
10571 +struct yaffs_ObjectList_struct {
10572 + yaffs_Object *objects;
10573 + struct yaffs_ObjectList_struct *next;
10576 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
10579 + struct list_head list;
10581 +} yaffs_ObjectBucket;
10584 +/* yaffs_CheckpointObject holds the definition of an object as dumped
10585 + * by checkpointing.
10594 + yaffs_ObjectType variantType:3;
10596 + __u8 softDeleted:1;
10599 + __u8 renameAllowed:1;
10600 + __u8 unlinkAllowed:1;
10604 + __u32 fileSizeOrEquivalentObjectId;
10606 +}yaffs_CheckpointObject;
10608 +/*--------------------- Temporary buffers ----------------
10610 + * These are chunk-sized working buffers. Each device has a few
10615 + int line; /* track from whence this buffer was allocated */
10617 +} yaffs_TempBuffer;
10619 +/*----------------- Device ---------------------------------*/
10621 +struct yaffs_DeviceStruct {
10622 + struct list_head devList;
10623 + const char *name;
10625 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
10626 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
10627 + int nChunksPerBlock; /* does not need to be a power of 2 */
10628 + int nBytesPerSpare; /* spare area size */
10629 + int startBlock; /* Start block we're allowed to use */
10630 + int endBlock; /* End block we're allowed to use */
10631 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
10632 + /* reserved blocks on NOR and RAM. */
10634 + /* Stuff used by the partitioned checkpointing mechanism */
10635 + int checkpointStartBlock;
10636 + int checkpointEndBlock;
10638 + /* Stuff used by the shared space checkpointing mechanism */
10639 + /* If this value is zero, then this mechanism is disabled */
10641 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
10646 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
10647 + * the number of short op caches (don't use too many)
10650 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
10652 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
10654 + void *genericDevice; /* Pointer to device context
10655 + * On an mtd this holds the mtd pointer.
10657 + void *superBlock;
10659 + /* NAND access functions (Must be set before calling YAFFS)*/
10661 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
10662 + int chunkInNAND, const __u8 * data,
10663 + const yaffs_Spare * spare);
10664 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
10665 + int chunkInNAND, __u8 * data,
10666 + yaffs_Spare * spare);
10667 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
10668 + int blockInNAND);
10669 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
10671 +#ifdef CONFIG_YAFFS_YAFFS2
10672 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
10673 + int chunkInNAND, const __u8 * data,
10674 + const yaffs_ExtendedTags * tags);
10675 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
10676 + int chunkInNAND, __u8 * data,
10677 + yaffs_ExtendedTags * tags);
10678 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
10679 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
10680 + yaffs_BlockState * state, int *sequenceNumber);
10685 + /* The removeObjectCallback function must be supplied by OS flavours that
10686 + * need it. The Linux kernel does not use this, but yaffs direct does use
10687 + * it to implement the faster readdir
10689 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
10691 + /* Callback to mark the superblock dirsty */
10692 + void (*markSuperBlockDirty)(void * superblock);
10694 + int wideTnodesDisabled; /* Set to disable wide tnodes */
10697 + /* End of stuff that must be set before initialisation. */
10699 + /* Runtime parameters. Set up by YAFFS. */
10701 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
10702 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
10704 + /* Stuff to support wide tnodes */
10705 + __u32 tnodeWidth;
10708 + /* Stuff to support various file offses to chunk/offset translations */
10709 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
10711 + __u32 crumbShift;
10712 + __u32 crumbsPerChunk;
10714 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
10715 + __u32 chunkShift;
10721 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
10722 + struct semaphore grossLock; /* Gross locking semaphore */
10723 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
10724 + * at compile time so we have to allocate it.
10726 + void (*putSuperFunc) (struct super_block * sb);
10731 + int isCheckpointed;
10734 + /* Stuff to support block offsetting to support start block zero */
10735 + int internalStartBlock;
10736 + int internalEndBlock;
10741 + /* Runtime checkpointing stuff */
10742 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
10743 + int checkpointByteCount;
10744 + int checkpointByteOffset;
10745 + __u8 *checkpointBuffer;
10746 + int checkpointOpenForWrite;
10747 + int blocksInCheckpoint;
10748 + int checkpointCurrentChunk;
10749 + int checkpointCurrentBlock;
10750 + int checkpointNextBlock;
10751 + int *checkpointBlockList;
10752 + int checkpointMaxBlocks;
10755 + yaffs_BlockInfo *blockInfo;
10756 + __u8 *chunkBits; /* bitmap of chunks in use */
10757 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
10758 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
10759 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
10760 + * Must be consistent with nChunksPerBlock.
10763 + int nErasedBlocks;
10764 + int allocationBlock; /* Current block being allocated off */
10765 + __u32 allocationPage;
10766 + int allocationBlockFinder; /* Used to search for next allocation block */
10768 + /* Runtime state */
10769 + int nTnodesCreated;
10770 + yaffs_Tnode *freeTnodes;
10772 + yaffs_TnodeList *allocatedTnodeList;
10776 + int nObjectsCreated;
10777 + yaffs_Object *freeObjects;
10778 + int nFreeObjects;
10780 + yaffs_ObjectList *allocatedObjectList;
10782 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
10786 + int currentDirtyChecker; /* Used to find current dirtiest block */
10788 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
10793 + int nBlockErasures;
10794 + int nErasureFailures;
10796 + int garbageCollections;
10797 + int passiveGarbageCollections;
10798 + int nRetriedWrites;
10799 + int nRetiredBlocks;
10802 + int tagsEccFixed;
10803 + int tagsEccUnfixed;
10805 + int nUnmarkedDeletions;
10807 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
10809 + /* Special directories */
10810 + yaffs_Object *rootDir;
10811 + yaffs_Object *lostNFoundDir;
10813 + /* Buffer areas for storing data to recover from write failures TODO
10814 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
10815 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
10818 + int bufferedBlock; /* Which block is buffered here? */
10819 + int doingBufferedBlockRewrite;
10821 + yaffs_ChunkCache *srCache;
10826 + /* Stuff for background deletion and unlinked files.*/
10827 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
10828 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
10829 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
10830 + int nDeletedFiles; /* Count of files awaiting deletion;*/
10831 + int nUnlinkedFiles; /* Count of unlinked files. */
10832 + int nBackgroundDeletions; /* Count of background deletions. */
10835 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
10837 + int unmanagedTempAllocations;
10838 + int unmanagedTempDeallocations;
10840 + /* yaffs2 runtime stuff */
10841 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
10842 + unsigned oldestDirtySequence;
10846 +typedef struct yaffs_DeviceStruct yaffs_Device;
10848 +/* The static layout of bllock usage etc is stored in the super block header */
10852 + int checkpointStartBlock;
10853 + int checkpointEndBlock;
10857 +} yaffs_SuperBlockHeader;
10859 +/* The CheckpointDevice structure holds the device information that changes at runtime and
10860 + * must be preserved over unmount/mount cycles.
10864 + int nErasedBlocks;
10865 + int allocationBlock; /* Current block being allocated off */
10866 + __u32 allocationPage;
10869 + int nDeletedFiles; /* Count of files awaiting deletion;*/
10870 + int nUnlinkedFiles; /* Count of unlinked files. */
10871 + int nBackgroundDeletions; /* Count of background deletions. */
10873 + /* yaffs2 runtime stuff */
10874 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
10875 + unsigned oldestDirtySequence;
10877 +} yaffs_CheckpointDevice;
10885 +} yaffs_CheckpointValidity;
10887 +/* Function to manipulate block info */
10888 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
10890 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
10891 + T(YAFFS_TRACE_ERROR,
10893 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
10897 + return &dev->blockInfo[blk - dev->internalStartBlock];
10900 +/*----------------------- YAFFS Functions -----------------------*/
10902 +int yaffs_GutsInitialise(yaffs_Device * dev);
10903 +void yaffs_Deinitialise(yaffs_Device * dev);
10905 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
10907 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
10908 + yaffs_Object * newDir, const YCHAR * newName);
10910 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
10911 +int yaffs_DeleteFile(yaffs_Object * obj);
10913 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
10914 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
10915 +int yaffs_GetObjectInode(yaffs_Object * obj);
10916 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
10917 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
10919 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
10920 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
10922 +/* File operations */
10923 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
10925 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
10926 + int nBytes, int writeThrough);
10927 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
10929 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
10930 + __u32 mode, __u32 uid, __u32 gid);
10931 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
10933 +/* Flushing and checkpointing */
10934 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
10936 +int yaffs_CheckpointSave(yaffs_Device *dev);
10937 +int yaffs_CheckpointRestore(yaffs_Device *dev);
10939 +/* Directory operations */
10940 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
10941 + __u32 mode, __u32 uid, __u32 gid);
10942 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
10943 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10944 + int (*fn) (yaffs_Object *));
10946 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
10948 +/* Link operations */
10949 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
10950 + yaffs_Object * equivalentObject);
10952 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
10954 +/* Symlink operations */
10955 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
10956 + __u32 mode, __u32 uid, __u32 gid,
10957 + const YCHAR * alias);
10958 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
10960 +/* Special inodes (fifos, sockets and devices) */
10961 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
10962 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
10964 +/* Special directories */
10965 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
10966 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
10968 +#ifdef CONFIG_YAFFS_WINCE
10969 +/* CONFIG_YAFFS_WINCE special stuff */
10970 +void yfsd_WinFileTimeNow(__u32 target[2]);
10975 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
10979 +int yaffs_DumpObject(yaffs_Object * obj);
10981 +void yaffs_GutsTest(yaffs_Device * dev);
10983 +/* A few useful functions */
10984 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
10985 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
10986 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
10987 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
10990 diff -urN linux.old/fs/yaffs2/yaffsinterface.h linux.dev/fs/yaffs2/yaffsinterface.h
10991 --- linux.old/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
10992 +++ linux.dev/fs/yaffs2/yaffsinterface.h 2006-12-14 04:21:47.000000000 +0100
10995 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
10996 + * yaffsinterface.h: Interface to the guts of yaffs.
10998 + * Copyright (C) 2002 Aleph One Ltd.
10999 + * for Toby Churchill Ltd and Brightstar Engineering
11001 + * Created by Charles Manning <charles@aleph1.co.uk>
11003 + * This program is free software; you can redistribute it and/or modify
11004 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11005 + * published by the Free Software Foundation.
11007 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11011 +#ifndef __YAFFSINTERFACE_H__
11012 +#define __YAFFSINTERFACE_H__
11014 +int yaffs_Initialise(unsigned nBlocks);
11017 diff -urN linux.old/fs/yaffs2/yaffs_mtdif2.c linux.dev/fs/yaffs2/yaffs_mtdif2.c
11018 --- linux.old/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
11019 +++ linux.dev/fs/yaffs2/yaffs_mtdif2.c 2006-12-14 04:21:47.000000000 +0100
11022 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11023 + * yaffs_mtdif.c NAND mtd wrapper functions.
11025 + * Copyright (C) 2002 Aleph One Ltd.
11026 + * for Toby Churchill Ltd and Brightstar Engineering
11028 + * Created by Charles Manning <charles@aleph1.co.uk>
11030 + * This program is free software; you can redistribute it and/or modify
11031 + * it under the terms of the GNU General Public License version 2 as
11032 + * published by the Free Software Foundation.
11036 +/* mtd interface for YAFFS2 */
11038 +const char *yaffs_mtdif2_c_version =
11039 + "$Id: yaffs_mtdif2.c,v 1.15 2006/11/08 06:24:34 charles Exp $";
11041 +#include "yportenv.h"
11044 +#include "yaffs_mtdif2.h"
11046 +#include "linux/mtd/mtd.h"
11047 +#include "linux/types.h"
11048 +#include "linux/time.h"
11050 +#include "yaffs_packedtags2.h"
11052 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
11053 + const __u8 * data,
11054 + const yaffs_ExtendedTags * tags)
11056 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11057 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11058 + struct mtd_oob_ops ops;
11064 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11066 + yaffs_PackedTags2 pt;
11068 + T(YAFFS_TRACE_MTD,
11070 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
11071 + TENDSTR), chunkInNAND, data, tags));
11073 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11075 + yaffs_PackTags2(&pt, tags);
11077 + BUG(); /* both tags and data should always be present */
11080 + ops.mode = MTD_OOB_AUTO;
11081 + ops.ooblen = sizeof(pt);
11082 + ops.len = dev->nDataBytesPerChunk;
11084 + ops.datbuf = (__u8 *)data;
11085 + ops.oobbuf = (void *)&pt;
11086 + retval = mtd->write_oob(mtd, addr, &ops);
11088 + BUG(); /* both tags and data should always be present */
11091 + yaffs_PackTags2(&pt, tags);
11094 + if (data && tags) {
11095 + if (dev->useNANDECC)
11097 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11098 + &dummy, data, (__u8 *) & pt, NULL);
11101 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11102 + &dummy, data, (__u8 *) & pt, NULL);
11106 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11110 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
11119 + return YAFFS_FAIL;
11122 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11123 + __u8 * data, yaffs_ExtendedTags * tags)
11125 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11126 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11127 + struct mtd_oob_ops ops;
11132 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11134 + yaffs_PackedTags2 pt;
11136 + T(YAFFS_TRACE_MTD,
11138 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
11139 + TENDSTR), chunkInNAND, data, tags));
11141 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11142 + if (data && !tags)
11143 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
11146 + ops.mode = MTD_OOB_AUTO;
11147 + ops.ooblen = sizeof(pt);
11148 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
11150 + ops.datbuf = data;
11151 + ops.oobbuf = dev->spareBuffer;
11152 + retval = mtd->read_oob(mtd, addr, &ops);
11155 + if (data && tags) {
11156 + if (dev->useNANDECC) {
11158 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11159 + &dummy, data, dev->spareBuffer,
11163 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11164 + &dummy, data, dev->spareBuffer,
11170 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11174 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
11175 + dev->spareBuffer);
11179 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
11182 + yaffs_UnpackTags2(tags, &pt);
11184 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
11185 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
11190 + return YAFFS_FAIL;
11193 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
11195 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11197 + T(YAFFS_TRACE_MTD,
11198 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
11201 + mtd->block_markbad(mtd,
11202 + blockNo * dev->nChunksPerBlock *
11203 + dev->nDataBytesPerChunk);
11208 + return YAFFS_FAIL;
11212 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11213 + yaffs_BlockState * state, int *sequenceNumber)
11215 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11218 + T(YAFFS_TRACE_MTD,
11219 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
11221 + mtd->block_isbad(mtd,
11222 + blockNo * dev->nChunksPerBlock *
11223 + dev->nDataBytesPerChunk);
11226 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
11228 + *state = YAFFS_BLOCK_STATE_DEAD;
11229 + *sequenceNumber = 0;
11231 + yaffs_ExtendedTags t;
11232 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
11234 + dev->nChunksPerBlock, NULL,
11237 + if (t.chunkUsed) {
11238 + *sequenceNumber = t.sequenceNumber;
11239 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
11241 + *sequenceNumber = 0;
11242 + *state = YAFFS_BLOCK_STATE_EMPTY;
11245 + T(YAFFS_TRACE_MTD,
11246 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
11252 + return YAFFS_FAIL;
11255 diff -urN linux.old/fs/yaffs2/yaffs_mtdif2.h linux.dev/fs/yaffs2/yaffs_mtdif2.h
11256 --- linux.old/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
11257 +++ linux.dev/fs/yaffs2/yaffs_mtdif2.h 2006-12-14 04:21:47.000000000 +0100
11260 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11261 + * yaffs_mtdif.c NAND mtd wrapper functions.
11263 + * Copyright (C) 2002 Aleph One Ltd.
11264 + * for Toby Churchill Ltd and Brightstar Engineering
11266 + * Created by Charles Manning <charles@aleph1.co.uk>
11268 + * This program is free software; you can redistribute it and/or modify
11269 + * it under the terms of the GNU General Public License version 2 as
11270 + * published by the Free Software Foundation.
11274 +#ifndef __YAFFS_MTDIF2_H__
11275 +#define __YAFFS_MTDIF2_H__
11277 +#include "yaffs_guts.h"
11278 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
11279 + const __u8 * data,
11280 + const yaffs_ExtendedTags * tags);
11281 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11282 + __u8 * data, yaffs_ExtendedTags * tags);
11283 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
11284 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11285 + yaffs_BlockState * state, int *sequenceNumber);
11288 diff -urN linux.old/fs/yaffs2/yaffs_mtdif.c linux.dev/fs/yaffs2/yaffs_mtdif.c
11289 --- linux.old/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
11290 +++ linux.dev/fs/yaffs2/yaffs_mtdif.c 2006-12-14 04:21:47.000000000 +0100
11293 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11294 + * yaffs_mtdif.c NAND mtd wrapper functions.
11296 + * Copyright (C) 2002 Aleph One Ltd.
11297 + * for Toby Churchill Ltd and Brightstar Engineering
11299 + * Created by Charles Manning <charles@aleph1.co.uk>
11301 + * This program is free software; you can redistribute it and/or modify
11302 + * it under the terms of the GNU General Public License version 2 as
11303 + * published by the Free Software Foundation.
11307 +const char *yaffs_mtdif_c_version =
11308 + "$Id: yaffs_mtdif.c,v 1.17 2006/11/29 20:21:12 charles Exp $";
11310 +#include "yportenv.h"
11313 +#include "yaffs_mtdif.h"
11315 +#include "linux/mtd/mtd.h"
11316 +#include "linux/types.h"
11317 +#include "linux/time.h"
11318 +#include "linux/mtd/nand.h"
11320 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
11321 +static struct nand_oobinfo yaffs_oobinfo = {
11324 + .eccpos = {8, 9, 10, 13, 14, 15}
11327 +static struct nand_oobinfo yaffs_noeccinfo = {
11332 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11333 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
11335 + oob[0] = spare->tagByte0;
11336 + oob[1] = spare->tagByte1;
11337 + oob[2] = spare->tagByte2;
11338 + oob[3] = spare->tagByte3;
11339 + oob[4] = spare->tagByte4;
11340 + oob[5] = spare->tagByte5 & 0x3f;
11341 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
11342 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
11343 + oob[6] = spare->tagByte6;
11344 + oob[7] = spare->tagByte7;
11347 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
11349 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
11350 + spare->tagByte0 = oob[0];
11351 + spare->tagByte1 = oob[1];
11352 + spare->tagByte2 = oob[2];
11353 + spare->tagByte3 = oob[3];
11354 + spare->tagByte4 = oob[4];
11355 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
11356 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
11357 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
11358 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
11359 + spare->tagByte6 = oob[6];
11360 + spare->tagByte7 = oob[7];
11361 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
11363 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
11367 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
11368 + const __u8 * data, const yaffs_Spare * spare)
11370 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11371 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11372 + struct mtd_oob_ops ops;
11377 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11378 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11379 + __u8 spareAsBytes[8]; /* OOB */
11381 + if (data && !spare)
11382 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
11384 + else if (spare) {
11385 + if (dev->useNANDECC) {
11386 + translate_spare2oob(spare, spareAsBytes);
11387 + ops.mode = MTD_OOB_AUTO;
11388 + ops.ooblen = 8; /* temp hack */
11390 + ops.mode = MTD_OOB_RAW;
11391 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
11393 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
11394 + ops.datbuf = (u8 *)data;
11396 + ops.oobbuf = spareAsBytes;
11397 + retval = mtd->write_oob(mtd, addr, &ops);
11400 + __u8 *spareAsBytes = (__u8 *) spare;
11402 + if (data && spare) {
11403 + if (dev->useNANDECC)
11405 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11406 + &dummy, data, spareAsBytes,
11410 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11411 + &dummy, data, spareAsBytes,
11412 + &yaffs_noeccinfo);
11416 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11420 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
11421 + &dummy, spareAsBytes);
11428 + return YAFFS_FAIL;
11431 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
11432 + yaffs_Spare * spare)
11434 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11435 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11436 + struct mtd_oob_ops ops;
11441 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11442 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11443 + __u8 spareAsBytes[8]; /* OOB */
11445 + if (data && !spare)
11446 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
11448 + else if (spare) {
11449 + if (dev->useNANDECC) {
11450 + ops.mode = MTD_OOB_AUTO;
11451 + ops.ooblen = 8; /* temp hack */
11453 + ops.mode = MTD_OOB_RAW;
11454 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
11456 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
11457 + ops.datbuf = data;
11459 + ops.oobbuf = spareAsBytes;
11460 + retval = mtd->read_oob(mtd, addr, &ops);
11461 + if (dev->useNANDECC)
11462 + translate_oob2spare(spare, spareAsBytes);
11465 + __u8 *spareAsBytes = (__u8 *) spare;
11467 + if (data && spare) {
11468 + if (dev->useNANDECC) {
11469 + /* Careful, this call adds 2 ints */
11470 + /* to the end of the spare data. Calling function */
11471 + /* should allocate enough memory for spare, */
11472 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
11474 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11475 + &dummy, data, spareAsBytes,
11479 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11480 + &dummy, data, spareAsBytes,
11481 + &yaffs_noeccinfo);
11486 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11490 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
11491 + &dummy, spareAsBytes);
11498 + return YAFFS_FAIL;
11501 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
11503 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11505 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
11506 + * dev->nChunksPerBlock;
11507 + struct erase_info ei;
11512 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
11515 + ei.callback = NULL;
11516 + ei.priv = (u_long) dev;
11518 + /* Todo finish off the ei if required */
11520 + sema_init(&dev->sem, 0);
11522 + retval = mtd->erase(mtd, &ei);
11527 + return YAFFS_FAIL;
11530 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
11535 diff -urN linux.old/fs/yaffs2/yaffs_mtdif.h linux.dev/fs/yaffs2/yaffs_mtdif.h
11536 --- linux.old/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
11537 +++ linux.dev/fs/yaffs2/yaffs_mtdif.h 2006-12-14 04:21:47.000000000 +0100
11540 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11541 + * yaffs_mtdif.h NAND mtd interface wrappers
11543 + * Copyright (C) 2002 Aleph One Ltd.
11544 + * for Toby Churchill Ltd and Brightstar Engineering
11546 + * Created by Charles Manning <charles@aleph1.co.uk>
11548 + * This program is free software; you can redistribute it and/or modify
11549 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11550 + * published by the Free Software Foundation.
11553 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11555 + * $Id: yaffs_mtdif.h,v 1.3 2005/08/11 01:07:43 marty Exp $
11558 +#ifndef __YAFFS_MTDIF_H__
11559 +#define __YAFFS_MTDIF_H__
11561 +#include "yaffs_guts.h"
11563 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
11564 + const __u8 * data, const yaffs_Spare * spare);
11565 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
11566 + yaffs_Spare * spare);
11567 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
11568 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
11570 diff -urN linux.old/fs/yaffs2/yaffs_nand.c linux.dev/fs/yaffs2/yaffs_nand.c
11571 --- linux.old/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
11572 +++ linux.dev/fs/yaffs2/yaffs_nand.c 2006-12-14 04:21:47.000000000 +0100
11575 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11577 + * Copyright (C) 2002 Aleph One Ltd.
11578 + * for Toby Churchill Ltd and Brightstar Engineering
11580 + * Created by Charles Manning <charles@aleph1.co.uk>
11582 + * This program is free software; you can redistribute it and/or modify
11583 + * it under the terms of the GNU General Public License version 2 as
11584 + * published by the Free Software Foundation.
11588 +const char *yaffs_nand_c_version =
11589 + "$Id: yaffs_nand.c,v 1.5 2006/11/08 09:52:12 charles Exp $";
11591 +#include "yaffs_nand.h"
11592 +#include "yaffs_tagscompat.h"
11593 +#include "yaffs_tagsvalidity.h"
11596 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11598 + yaffs_ExtendedTags * tags)
11601 + yaffs_ExtendedTags localTags;
11603 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
11605 + /* If there are no tags provided, use local tags to get prioritised gc working */
11607 + tags = &localTags;
11609 + if (dev->readChunkWithTagsFromNAND)
11610 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
11613 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
11614 + realignedChunkInNAND,
11618 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
11620 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
11621 + yaffs_HandleChunkError(dev,bi);
11627 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
11629 + const __u8 * buffer,
11630 + yaffs_ExtendedTags * tags)
11632 + chunkInNAND -= dev->chunkOffset;
11636 + tags->sequenceNumber = dev->sequenceNumber;
11637 + tags->chunkUsed = 1;
11638 + if (!yaffs_ValidateTags(tags)) {
11639 + T(YAFFS_TRACE_ERROR,
11640 + (TSTR("Writing uninitialised tags" TENDSTR)));
11643 + T(YAFFS_TRACE_WRITE,
11644 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
11645 + tags->objectId, tags->chunkId));
11647 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
11651 + if (dev->writeChunkWithTagsToNAND)
11652 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
11655 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
11661 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
11663 + blockNo -= dev->blockOffset;
11666 + if (dev->markNANDBlockBad)
11667 + return dev->markNANDBlockBad(dev, blockNo);
11669 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
11672 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
11674 + yaffs_BlockState * state,
11675 + unsigned *sequenceNumber)
11677 + blockNo -= dev->blockOffset;
11679 + if (dev->queryNANDBlock)
11680 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
11682 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
11688 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11693 + blockInNAND -= dev->blockOffset;
11696 + dev->nBlockErasures++;
11697 + result = dev->eraseBlockInNAND(dev, blockInNAND);
11702 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
11704 + return dev->initialiseNAND(dev);
11709 diff -urN linux.old/fs/yaffs2/yaffs_nandemul2k.h linux.dev/fs/yaffs2/yaffs_nandemul2k.h
11710 --- linux.old/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
11711 +++ linux.dev/fs/yaffs2/yaffs_nandemul2k.h 2006-12-14 04:21:47.000000000 +0100
11714 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11716 + * Copyright (C) 2002 Aleph One Ltd.
11717 + * for Toby Churchill Ltd and Brightstar Engineering
11719 + * Created by Charles Manning <charles@aleph1.co.uk>
11721 + * This program is free software; you can redistribute it and/or modify
11722 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11723 + * published by the Free Software Foundation.
11726 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11728 + * yaffs_nandemul2k.h: Interface to emulated NAND functions (2k page size)
11730 + * $Id: yaffs_nandemul2k.h,v 1.2 2005/08/11 02:37:49 marty Exp $
11733 +#ifndef __YAFFS_NANDEMUL2K_H__
11734 +#define __YAFFS_NANDEMUL2K_H__
11736 +#include "yaffs_guts.h"
11738 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
11739 + int chunkInNAND, const __u8 * data,
11740 + yaffs_ExtendedTags * tags);
11741 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
11742 + int chunkInNAND, __u8 * data,
11743 + yaffs_ExtendedTags * tags);
11744 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
11745 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11746 + yaffs_BlockState * state, int *sequenceNumber);
11747 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11748 + int blockInNAND);
11749 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
11750 +int nandemul2k_GetBytesPerChunk(void);
11751 +int nandemul2k_GetChunksPerBlock(void);
11752 +int nandemul2k_GetNumberOfBlocks(void);
11755 diff -urN linux.old/fs/yaffs2/yaffs_nand.h linux.dev/fs/yaffs2/yaffs_nand.h
11756 --- linux.old/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
11757 +++ linux.dev/fs/yaffs2/yaffs_nand.h 2006-12-14 04:21:47.000000000 +0100
11760 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11762 + * Copyright (C) 2002 Aleph One Ltd.
11763 + * for Toby Churchill Ltd and Brightstar Engineering
11765 + * Created by Charles Manning <charles@aleph1.co.uk>
11767 + * This program is free software; you can redistribute it and/or modify
11768 + * it under the terms of the GNU General Public License version 2 as
11769 + * published by the Free Software Foundation.
11773 +#ifndef __YAFFS_NAND_H__
11774 +#define __YAFFS_NAND_H__
11775 +#include "yaffs_guts.h"
11779 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11781 + yaffs_ExtendedTags * tags);
11783 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
11785 + const __u8 * buffer,
11786 + yaffs_ExtendedTags * tags);
11788 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
11790 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
11792 + yaffs_BlockState * state,
11793 + unsigned *sequenceNumber);
11795 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11796 + int blockInNAND);
11798 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
11802 diff -urN linux.old/fs/yaffs2/yaffs_packedtags1.c linux.dev/fs/yaffs2/yaffs_packedtags1.c
11803 --- linux.old/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
11804 +++ linux.dev/fs/yaffs2/yaffs_packedtags1.c 2006-12-14 04:21:47.000000000 +0100
11806 +#include "yaffs_packedtags1.h"
11807 +#include "yportenv.h"
11809 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
11811 + pt->chunkId = t->chunkId;
11812 + pt->serialNumber = t->serialNumber;
11813 + pt->byteCount = t->byteCount;
11814 + pt->objectId = t->objectId;
11816 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
11817 + pt->unusedStuff = 0;
11818 + pt->shouldBeFF = 0xFFFFFFFF;
11822 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
11824 + static const __u8 allFF[] =
11825 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
11828 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
11830 + if (pt->shouldBeFF != 0xFFFFFFFF) {
11833 + t->chunkUsed = 1;
11834 + t->objectId = pt->objectId;
11835 + t->chunkId = pt->chunkId;
11836 + t->byteCount = pt->byteCount;
11837 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
11838 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
11839 + t->serialNumber = pt->serialNumber;
11841 + memset(t, 0, sizeof(yaffs_ExtendedTags));
11845 diff -urN linux.old/fs/yaffs2/yaffs_packedtags1.h linux.dev/fs/yaffs2/yaffs_packedtags1.h
11846 --- linux.old/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
11847 +++ linux.dev/fs/yaffs2/yaffs_packedtags1.h 2006-12-14 04:21:47.000000000 +0100
11849 +// This is used to pack YAFFS1 tags, not YAFFS2 tags.
11851 +#ifndef __YAFFS_PACKEDTAGS1_H__
11852 +#define __YAFFS_PACKEDTAGS1_H__
11854 +#include "yaffs_guts.h"
11857 + unsigned chunkId:20;
11858 + unsigned serialNumber:2;
11859 + unsigned byteCount:10;
11860 + unsigned objectId:18;
11862 + unsigned deleted:1;
11863 + unsigned unusedStuff:1;
11864 + unsigned shouldBeFF;
11866 +} yaffs_PackedTags1;
11868 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
11869 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
11871 diff -urN linux.old/fs/yaffs2/yaffs_packedtags2.c linux.dev/fs/yaffs2/yaffs_packedtags2.c
11872 --- linux.old/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
11873 +++ linux.dev/fs/yaffs2/yaffs_packedtags2.c 2006-12-14 04:21:47.000000000 +0100
11876 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11878 + * yaffs_packedtags2.c: Tags packing for YAFFS2
11880 + * Copyright (C) 2002 Aleph One Ltd.
11882 + * Created by Charles Manning <charles@aleph1.co.uk>
11885 + * This program is free software; you can redistribute it and/or
11886 + * modify it under the terms of the GNU Lesser General Public License
11887 + * version 2.1 as published by the Free Software Foundation.
11890 +#include "yaffs_packedtags2.h"
11891 +#include "yportenv.h"
11892 +#include "yaffs_tagsvalidity.h"
11894 +/* This code packs a set of extended tags into a binary structure for
11898 +/* Some of the information is "extra" struff which can be packed in to
11900 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
11903 +/* Extra flags applied to chunkId */
11905 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
11906 +#define EXTRA_SHRINK_FLAG 0x40000000
11907 +#define EXTRA_SHADOWS_FLAG 0x20000000
11908 +#define EXTRA_SPARE_FLAGS 0x10000000
11910 +#define ALL_EXTRA_FLAGS 0xF0000000
11912 +/* Also, the top 4 bits of the object Id are set to the object type. */
11913 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
11914 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
11916 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
11918 + T(YAFFS_TRACE_MTD,
11919 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
11920 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
11921 + pt->t.sequenceNumber));
11924 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
11926 + T(YAFFS_TRACE_MTD,
11928 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
11929 + "%d del %d ser %d seq %d"
11930 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
11931 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
11932 + t->sequenceNumber));
11936 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
11938 + pt->t.chunkId = t->chunkId;
11939 + pt->t.sequenceNumber = t->sequenceNumber;
11940 + pt->t.byteCount = t->byteCount;
11941 + pt->t.objectId = t->objectId;
11943 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
11944 + /* Store the extra header info instead */
11945 + /* We save the parent object in the chunkId */
11946 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
11947 + | t->extraParentObjectId;
11948 + if (t->extraIsShrinkHeader) {
11949 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
11951 + if (t->extraShadows) {
11952 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
11955 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
11956 + pt->t.objectId |=
11957 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
11959 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
11960 + pt->t.byteCount = t->extraEquivalentObjectId;
11961 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
11962 + pt->t.byteCount = t->extraFileLength;
11964 + pt->t.byteCount = 0;
11968 + yaffs_DumpPackedTags2(pt);
11969 + yaffs_DumpTags2(t);
11971 +#ifndef YAFFS_IGNORE_TAGS_ECC
11973 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
11974 + sizeof(yaffs_PackedTags2TagsPart),
11980 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
11983 + memset(t, 0, sizeof(yaffs_ExtendedTags));
11985 + yaffs_InitialiseTags(t);
11987 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
11988 + /* Page is in use */
11989 +#ifdef YAFFS_IGNORE_TAGS_ECC
11991 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
11995 + yaffs_ECCOther ecc;
11997 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
11999 + (yaffs_PackedTags2TagsPart),
12002 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
12004 + (yaffs_PackedTags2TagsPart),
12008 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12011 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
12014 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
12017 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
12022 + t->chunkUsed = 1;
12023 + t->objectId = pt->t.objectId;
12024 + t->chunkId = pt->t.chunkId;
12025 + t->byteCount = pt->t.byteCount;
12026 + t->chunkDeleted = 0;
12027 + t->serialNumber = 0;
12028 + t->sequenceNumber = pt->t.sequenceNumber;
12030 + /* Do extra header info stuff */
12032 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
12034 + t->byteCount = 0;
12036 + t->extraHeaderInfoAvailable = 1;
12037 + t->extraParentObjectId =
12038 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
12039 + t->extraIsShrinkHeader =
12040 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
12041 + t->extraShadows =
12042 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
12043 + t->extraObjectType =
12044 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
12045 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
12047 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
12048 + t->extraEquivalentObjectId = pt->t.byteCount;
12050 + t->extraFileLength = pt->t.byteCount;
12055 + yaffs_DumpPackedTags2(pt);
12056 + yaffs_DumpTags2(t);
12059 diff -urN linux.old/fs/yaffs2/yaffs_packedtags2.h linux.dev/fs/yaffs2/yaffs_packedtags2.h
12060 --- linux.old/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
12061 +++ linux.dev/fs/yaffs2/yaffs_packedtags2.h 2006-12-14 04:21:47.000000000 +0100
12063 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
12065 +#ifndef __YAFFS_PACKEDTAGS2_H__
12066 +#define __YAFFS_PACKEDTAGS2_H__
12068 +#include "yaffs_guts.h"
12069 +#include "yaffs_ecc.h"
12072 + unsigned sequenceNumber;
12073 + unsigned objectId;
12074 + unsigned chunkId;
12075 + unsigned byteCount;
12076 +} yaffs_PackedTags2TagsPart;
12079 + yaffs_PackedTags2TagsPart t;
12080 + yaffs_ECCOther ecc;
12081 +} yaffs_PackedTags2;
12083 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
12084 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
12086 diff -urN linux.old/fs/yaffs2/yaffs_qsort.c linux.dev/fs/yaffs2/yaffs_qsort.c
12087 --- linux.old/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
12088 +++ linux.dev/fs/yaffs2/yaffs_qsort.c 2006-12-14 04:21:47.000000000 +0100
12091 + * Copyright (c) 1992, 1993
12092 + * The Regents of the University of California. All rights reserved.
12094 + * Redistribution and use in source and binary forms, with or without
12095 + * modification, are permitted provided that the following conditions
12097 + * 1. Redistributions of source code must retain the above copyright
12098 + * notice, this list of conditions and the following disclaimer.
12099 + * 2. Redistributions in binary form must reproduce the above copyright
12100 + * notice, this list of conditions and the following disclaimer in the
12101 + * documentation and/or other materials provided with the distribution.
12102 + * 3. Neither the name of the University nor the names of its contributors
12103 + * may be used to endorse or promote products derived from this software
12104 + * without specific prior written permission.
12106 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
12107 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12108 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12109 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
12110 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
12111 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12112 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
12113 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12114 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
12115 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
12119 +#include "yportenv.h"
12120 +//#include <linux/string.h>
12123 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
12125 +#define swapcode(TYPE, parmi, parmj, n) { \
12126 + long i = (n) / sizeof (TYPE); \
12127 + register TYPE *pi = (TYPE *) (parmi); \
12128 + register TYPE *pj = (TYPE *) (parmj); \
12130 + register TYPE t = *pi; \
12133 + } while (--i > 0); \
12136 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
12137 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
12139 +static __inline void
12140 +swapfunc(char *a, char *b, int n, int swaptype)
12142 + if (swaptype <= 1)
12143 + swapcode(long, a, b, n)
12145 + swapcode(char, a, b, n)
12148 +#define swap(a, b) \
12149 + if (swaptype == 0) { \
12150 + long t = *(long *)(a); \
12151 + *(long *)(a) = *(long *)(b); \
12152 + *(long *)(b) = t; \
12154 + swapfunc(a, b, es, swaptype)
12156 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
12158 +static __inline char *
12159 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
12161 + return cmp(a, b) < 0 ?
12162 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
12163 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
12166 +#define min(a,b) (((a) < (b)) ? (a) : (b))
12168 +qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
12170 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
12171 + int d, r, swaptype, swap_cnt;
12172 + register char *a = aa;
12174 +loop: SWAPINIT(a, es);
12177 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
12178 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
12180 + swap(pl, pl - es);
12183 + pm = (char *)a + (n / 2) * es;
12186 + pn = (char *)a + (n - 1) * es;
12188 + d = (n / 8) * es;
12189 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
12190 + pm = med3(pm - d, pm, pm + d, cmp);
12191 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
12193 + pm = med3(pl, pm, pn, cmp);
12196 + pa = pb = (char *)a + es;
12198 + pc = pd = (char *)a + (n - 1) * es;
12200 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
12208 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
12223 + if (swap_cnt == 0) { /* Switch to insertion sort */
12224 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
12225 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
12227 + swap(pl, pl - es);
12231 + pn = (char *)a + n * es;
12232 + r = min(pa - (char *)a, pb - pa);
12233 + vecswap(a, pb - r, r);
12234 + r = min((long)(pd - pc), (long)(pn - pd - es));
12235 + vecswap(pb, pn - r, r);
12236 + if ((r = pb - pa) > es)
12237 + qsort(a, r / es, es, cmp);
12238 + if ((r = pd - pc) > es) {
12239 + /* Iterate rather than recurse to save stack space */
12244 +/* qsort(pn - r, r / es, es, cmp);*/
12246 diff -urN linux.old/fs/yaffs2/yaffs_qsort.h linux.dev/fs/yaffs2/yaffs_qsort.h
12247 --- linux.old/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
12248 +++ linux.dev/fs/yaffs2/yaffs_qsort.h 2006-12-14 04:21:47.000000000 +0100
12251 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12252 + * yaffs_qsort.h: Interface to BSD-licensed qsort routine.
12254 + * Copyright (C) 2002 Aleph One Ltd.
12255 + * for Toby Churchill Ltd and Brightstar Engineering
12257 + * Created by Charles Manning <charles@aleph1.co.uk>
12259 + * This program is free software; you can redistribute it and/or modify
12260 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12261 + * published by the Free Software Foundation.
12263 + * $Id: yaffs_qsort.h,v 1.2 2006/11/07 23:20:09 charles Exp $
12266 +#ifndef __YAFFS_QSORT_H__
12267 +#define __YAFFS_QSORT_H__
12269 +extern void qsort (void *const base, size_t total_elems, size_t size,
12270 + int (*cmp)(const void *, const void *));
12273 diff -urN linux.old/fs/yaffs2/yaffs_tagscompat.c linux.dev/fs/yaffs2/yaffs_tagscompat.c
12274 --- linux.old/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
12275 +++ linux.dev/fs/yaffs2/yaffs_tagscompat.c 2006-12-14 04:21:47.000000000 +0100
12278 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12279 + * yaffs_tagscompat.h: Tags compatability layer to use YAFFS1 formatted NAND.
12281 + * Copyright (C) 2002 Aleph One Ltd.
12283 + * Created by Charles Manning <charles@aleph1.co.uk>
12285 + * This program is free software; you can redistribute it and/or modify
12286 + * it under the terms of the GNU General Public License version 2 as
12287 + * published by the Free Software Foundation.
12289 + * $Id: yaffs_tagscompat.c,v 1.8 2005/11/29 20:54:32 marty Exp $
12292 +#include "yaffs_guts.h"
12293 +#include "yaffs_tagscompat.h"
12294 +#include "yaffs_ecc.h"
12296 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
12298 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
12299 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
12300 + const __u8 * data,
12301 + const yaffs_Spare * spare);
12302 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
12303 + const yaffs_Spare * spare);
12304 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
12307 +static const char yaffs_countBitsTable[256] = {
12308 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
12309 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12310 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12311 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12312 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12313 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12314 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12315 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12316 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12317 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12318 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12319 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12320 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12321 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12322 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12323 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
12326 +static int yaffs_CountBits(__u8 x)
12329 + retVal = yaffs_countBitsTable[x];
12333 +/********** Tags ECC calculations *********/
12335 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
12337 + yaffs_ECCCalculate(data, spare->ecc1);
12338 + yaffs_ECCCalculate(&data[256], spare->ecc2);
12341 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
12343 + /* Calculate an ecc */
12345 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
12347 + unsigned ecc = 0;
12348 + unsigned bit = 0;
12352 + for (i = 0; i < 8; i++) {
12353 + for (j = 1; j & 0xff; j <<= 1) {
12365 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
12367 + unsigned ecc = tags->ecc;
12369 + yaffs_CalcTagsECC(tags);
12371 + ecc ^= tags->ecc;
12373 + if (ecc && ecc <= 64) {
12374 + /* TODO: Handle the failure better. Retire? */
12375 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
12379 + b[ecc / 8] ^= (1 << (ecc & 7));
12381 + /* Now recvalc the ecc */
12382 + yaffs_CalcTagsECC(tags);
12384 + return 1; /* recovered error */
12385 + } else if (ecc) {
12386 + /* Wierd ecc failure value */
12387 + /* TODO Need to do somethiong here */
12388 + return -1; /* unrecovered error */
12394 +/********** Tags **********/
12396 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
12397 + yaffs_Tags * tagsPtr)
12399 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
12401 + yaffs_CalcTagsECC(tagsPtr);
12403 + sparePtr->tagByte0 = tu->asBytes[0];
12404 + sparePtr->tagByte1 = tu->asBytes[1];
12405 + sparePtr->tagByte2 = tu->asBytes[2];
12406 + sparePtr->tagByte3 = tu->asBytes[3];
12407 + sparePtr->tagByte4 = tu->asBytes[4];
12408 + sparePtr->tagByte5 = tu->asBytes[5];
12409 + sparePtr->tagByte6 = tu->asBytes[6];
12410 + sparePtr->tagByte7 = tu->asBytes[7];
12413 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
12414 + yaffs_Tags * tagsPtr)
12416 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
12419 + tu->asBytes[0] = sparePtr->tagByte0;
12420 + tu->asBytes[1] = sparePtr->tagByte1;
12421 + tu->asBytes[2] = sparePtr->tagByte2;
12422 + tu->asBytes[3] = sparePtr->tagByte3;
12423 + tu->asBytes[4] = sparePtr->tagByte4;
12424 + tu->asBytes[5] = sparePtr->tagByte5;
12425 + tu->asBytes[6] = sparePtr->tagByte6;
12426 + tu->asBytes[7] = sparePtr->tagByte7;
12428 + result = yaffs_CheckECCOnTags(tagsPtr);
12429 + if (result > 0) {
12430 + dev->tagsEccFixed++;
12431 + } else if (result < 0) {
12432 + dev->tagsEccUnfixed++;
12436 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
12438 + memset(spare, 0xFF, sizeof(yaffs_Spare));
12441 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
12442 + int chunkInNAND, const __u8 * data,
12443 + yaffs_Spare * spare)
12445 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
12446 + T(YAFFS_TRACE_ERROR,
12447 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
12449 + return YAFFS_FAIL;
12452 + dev->nPageWrites++;
12453 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
12456 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
12459 + yaffs_Spare * spare,
12460 + yaffs_ECCResult * eccResult,
12461 + int doErrorCorrection)
12464 + yaffs_Spare localSpare;
12466 + dev->nPageReads++;
12468 + if (!spare && data) {
12469 + /* If we don't have a real spare, then we use a local one. */
12470 + /* Need this for the calculation of the ecc */
12471 + spare = &localSpare;
12474 + if (!dev->useNANDECC) {
12475 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
12476 + if (data && doErrorCorrection) {
12477 + /* Do ECC correction */
12478 + /* Todo handle any errors */
12479 + int eccResult1, eccResult2;
12482 + yaffs_ECCCalculate(data, calcEcc);
12484 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
12485 + yaffs_ECCCalculate(&data[256], calcEcc);
12487 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
12489 + if (eccResult1 > 0) {
12490 + T(YAFFS_TRACE_ERROR,
12492 + ("**>>yaffs ecc error fix performed on chunk %d:0"
12493 + TENDSTR), chunkInNAND));
12495 + } else if (eccResult1 < 0) {
12496 + T(YAFFS_TRACE_ERROR,
12498 + ("**>>yaffs ecc error unfixed on chunk %d:0"
12499 + TENDSTR), chunkInNAND));
12500 + dev->eccUnfixed++;
12503 + if (eccResult2 > 0) {
12504 + T(YAFFS_TRACE_ERROR,
12506 + ("**>>yaffs ecc error fix performed on chunk %d:1"
12507 + TENDSTR), chunkInNAND));
12509 + } else if (eccResult2 < 0) {
12510 + T(YAFFS_TRACE_ERROR,
12512 + ("**>>yaffs ecc error unfixed on chunk %d:1"
12513 + TENDSTR), chunkInNAND));
12514 + dev->eccUnfixed++;
12517 + if (eccResult1 || eccResult2) {
12518 + /* We had a data problem on this page */
12519 + yaffs_HandleReadDataError(dev, chunkInNAND);
12522 + if (eccResult1 < 0 || eccResult2 < 0)
12523 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
12524 + else if (eccResult1 > 0 || eccResult2 > 0)
12525 + *eccResult = YAFFS_ECC_RESULT_FIXED;
12527 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12530 + /* Must allocate enough memory for spare+2*sizeof(int) */
12531 + /* for ecc results from device. */
12532 + struct yaffs_NANDSpare nspare;
12534 + dev->readChunkFromNAND(dev, chunkInNAND, data,
12535 + (yaffs_Spare *) & nspare);
12536 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
12537 + if (data && doErrorCorrection) {
12538 + if (nspare.eccres1 > 0) {
12539 + T(YAFFS_TRACE_ERROR,
12541 + ("**>>mtd ecc error fix performed on chunk %d:0"
12542 + TENDSTR), chunkInNAND));
12543 + } else if (nspare.eccres1 < 0) {
12544 + T(YAFFS_TRACE_ERROR,
12546 + ("**>>mtd ecc error unfixed on chunk %d:0"
12547 + TENDSTR), chunkInNAND));
12550 + if (nspare.eccres2 > 0) {
12551 + T(YAFFS_TRACE_ERROR,
12553 + ("**>>mtd ecc error fix performed on chunk %d:1"
12554 + TENDSTR), chunkInNAND));
12555 + } else if (nspare.eccres2 < 0) {
12556 + T(YAFFS_TRACE_ERROR,
12558 + ("**>>mtd ecc error unfixed on chunk %d:1"
12559 + TENDSTR), chunkInNAND));
12562 + if (nspare.eccres1 || nspare.eccres2) {
12563 + /* We had a data problem on this page */
12564 + yaffs_HandleReadDataError(dev, chunkInNAND);
12567 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
12568 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
12569 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
12570 + *eccResult = YAFFS_ECC_RESULT_FIXED;
12572 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12580 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
12584 + static int init = 0;
12585 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
12586 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
12587 + /* Might as well always allocate the larger size for */
12588 + /* dev->useNANDECC == true; */
12589 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
12591 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
12594 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
12598 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
12599 + return YAFFS_FAIL;
12600 + if (memcmp(cmpbuf, spare, 16))
12601 + return YAFFS_FAIL;
12609 + * Functions for robustisizing
12612 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
12614 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
12616 + /* Mark the block for retirement */
12617 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
12618 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
12619 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
12622 + * Just do a garbage collection on the affected block
12623 + * then retire the block
12629 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
12633 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
12634 + const __u8 * data,
12635 + const yaffs_Spare * spare)
12639 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
12640 + const yaffs_Spare * spare)
12644 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
12646 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
12648 + /* Mark the block for retirement */
12649 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
12650 + /* Delete the chunk */
12651 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
12654 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
12655 + const yaffs_Spare * s0, const yaffs_Spare * s1)
12658 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
12659 + s0->tagByte0 != s1->tagByte0 ||
12660 + s0->tagByte1 != s1->tagByte1 ||
12661 + s0->tagByte2 != s1->tagByte2 ||
12662 + s0->tagByte3 != s1->tagByte3 ||
12663 + s0->tagByte4 != s1->tagByte4 ||
12664 + s0->tagByte5 != s1->tagByte5 ||
12665 + s0->tagByte6 != s1->tagByte6 ||
12666 + s0->tagByte7 != s1->tagByte7 ||
12667 + s0->ecc1[0] != s1->ecc1[0] ||
12668 + s0->ecc1[1] != s1->ecc1[1] ||
12669 + s0->ecc1[2] != s1->ecc1[2] ||
12670 + s0->ecc2[0] != s1->ecc2[0] ||
12671 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
12677 +#endif /* NOTYET */
12679 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
12681 + const __u8 * data,
12682 + const yaffs_ExtendedTags *
12685 + yaffs_Spare spare;
12688 + yaffs_SpareInitialise(&spare);
12690 + if (eTags->chunkDeleted) {
12691 + spare.pageStatus = 0;
12693 + tags.objectId = eTags->objectId;
12694 + tags.chunkId = eTags->chunkId;
12695 + tags.byteCount = eTags->byteCount;
12696 + tags.serialNumber = eTags->serialNumber;
12698 + if (!dev->useNANDECC && data) {
12699 + yaffs_CalcECC(data, &spare);
12701 + yaffs_LoadTagsIntoSpare(&spare, &tags);
12705 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
12708 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
12711 + yaffs_ExtendedTags * eTags)
12714 + yaffs_Spare spare;
12716 + yaffs_ECCResult eccResult;
12718 + static yaffs_Spare spareFF;
12722 + memset(&spareFF, 0xFF, sizeof(spareFF));
12726 + if (yaffs_ReadChunkFromNAND
12727 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
12728 + /* eTags may be NULL */
12732 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
12734 + eTags->chunkDeleted = deleted;
12735 + eTags->eccResult = eccResult;
12736 + eTags->blockBad = 0; /* We're reading it */
12737 + /* therefore it is not a bad block */
12738 + eTags->chunkUsed =
12739 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
12742 + if (eTags->chunkUsed) {
12743 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
12745 + eTags->objectId = tags.objectId;
12746 + eTags->chunkId = tags.chunkId;
12747 + eTags->byteCount = tags.byteCount;
12748 + eTags->serialNumber = tags.serialNumber;
12754 + return YAFFS_FAIL;
12758 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
12762 + yaffs_Spare spare;
12764 + memset(&spare, 0xff, sizeof(yaffs_Spare));
12766 + spare.blockStatus = 'Y';
12768 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
12770 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
12777 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
12778 + int blockNo, yaffs_BlockState *
12780 + int *sequenceNumber)
12783 + yaffs_Spare spare0, spare1;
12784 + static yaffs_Spare spareFF;
12786 + yaffs_ECCResult dummy;
12789 + memset(&spareFF, 0xFF, sizeof(spareFF));
12793 + *sequenceNumber = 0;
12795 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
12796 + &spare0, &dummy, 1);
12797 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
12798 + &spare1, &dummy, 1);
12800 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
12801 + *state = YAFFS_BLOCK_STATE_DEAD;
12802 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
12803 + *state = YAFFS_BLOCK_STATE_EMPTY;
12805 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12809 diff -urN linux.old/fs/yaffs2/yaffs_tagscompat.h linux.dev/fs/yaffs2/yaffs_tagscompat.h
12810 --- linux.old/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
12811 +++ linux.dev/fs/yaffs2/yaffs_tagscompat.h 2006-12-14 04:21:47.000000000 +0100
12814 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12815 + * yaffs_ramdisk.h: yaffs ram disk component
12817 + * Copyright (C) 2002 Aleph One Ltd.
12819 + * Created by Charles Manning <charles@aleph1.co.uk>
12821 + * This program is free software; you can redistribute it and/or modify
12822 + * it under the terms of the GNU General Public License version 2 as
12823 + * published by the Free Software Foundation.
12825 + * $Id: yaffs_tagscompat.h,v 1.2 2005/08/11 02:33:03 marty Exp $
12828 +/* This provides a ram disk under yaffs.
12829 + * NB this is not intended for NAND emulation.
12830 + * Use this with dev->useNANDECC enabled, then ECC overheads are not required.
12832 +#ifndef __YAFFS_TAGSCOMPAT_H__
12833 +#define __YAFFS_TAGSCOMPAT_H__
12835 +#include "yaffs_guts.h"
12836 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
12838 + const __u8 * data,
12839 + const yaffs_ExtendedTags *
12841 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
12844 + yaffs_ExtendedTags *
12846 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
12848 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
12849 + int blockNo, yaffs_BlockState *
12850 + state, int *sequenceNumber);
12853 diff -urN linux.old/fs/yaffs2/yaffs_tagsvalidity.c linux.dev/fs/yaffs2/yaffs_tagsvalidity.c
12854 --- linux.old/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
12855 +++ linux.dev/fs/yaffs2/yaffs_tagsvalidity.c 2006-12-14 04:21:47.000000000 +0100
12859 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12861 + * Copyright (C) 2002 Aleph One Ltd.
12862 + * for Toby Churchill Ltd and Brightstar Engineering
12864 + * Created by Charles Manning <charles@aleph1.co.uk>
12866 + * This program is free software; you can redistribute it and/or modify
12867 + * it under the terms of the GNU General Public License version 2 as
12868 + * published by the Free Software Foundation.
12870 + * $Id: yaffs_tagsvalidity.c,v 1.2 2005/08/11 02:33:03 marty Exp $
12873 +#include "yaffs_tagsvalidity.h"
12875 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
12877 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
12878 + tags->validMarker0 = 0xAAAAAAAA;
12879 + tags->validMarker1 = 0x55555555;
12882 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
12884 + return (tags->validMarker0 == 0xAAAAAAAA &&
12885 + tags->validMarker1 == 0x55555555);
12888 diff -urN linux.old/fs/yaffs2/yaffs_tagsvalidity.h linux.dev/fs/yaffs2/yaffs_tagsvalidity.h
12889 --- linux.old/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
12890 +++ linux.dev/fs/yaffs2/yaffs_tagsvalidity.h 2006-12-14 04:21:47.000000000 +0100
12894 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12896 + * Copyright (C) 2002 Aleph One Ltd.
12897 + * for Toby Churchill Ltd and Brightstar Engineering
12899 + * Created by Charles Manning <charles@aleph1.co.uk>
12901 + * This program is free software; you can redistribute it and/or modify
12902 + * it under the terms of the GNU General Public License version 2 as
12903 + * published by the Free Software Foundation.
12905 + * $Id: yaffs_tagsvalidity.h,v 1.2 2005/08/11 02:33:03 marty Exp $
12907 +//yaffs_tagsvalidity.h
12909 +#ifndef __YAFFS_TAGS_VALIDITY_H__
12910 +#define __YAFFS_TAGS_VALIDITY_H__
12912 +#include "yaffs_guts.h"
12914 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12915 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
12917 diff -urN linux.old/fs/yaffs2/yportenv.h linux.dev/fs/yaffs2/yportenv.h
12918 --- linux.old/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
12919 +++ linux.dev/fs/yaffs2/yportenv.h 2006-12-14 04:26:06.000000000 +0100
12922 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12923 + * yportenv.h: Portable services used by yaffs. This is done to allow
12924 + * simple migration from kernel space into app space for testing.
12926 + * Copyright (C) 2002 Aleph One Ltd.
12927 + * for Toby Churchill Ltd and Brightstar Engineering
12929 + * Created by Charles Manning <charles@aleph1.co.uk>
12931 + * This program is free software; you can redistribute it and/or modify
12932 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12933 + * published by the Free Software Foundation.
12936 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12938 + * $Id: yportenv.h,v 1.11 2006/05/21 09:39:12 charles Exp $
12942 +#ifndef __YPORTENV_H__
12943 +#define __YPORTENV_H__
12945 +#if defined CONFIG_YAFFS_WINCE
12947 +#include "ywinceenv.h"
12949 +#elif defined __KERNEL__
12951 +#include "moduleconfig.h"
12953 +/* Linux kernel */
12954 +#include <linux/autoconf.h>
12955 +#include <linux/kernel.h>
12956 +#include <linux/version.h>
12957 +#include <linux/mm.h>
12958 +#include <linux/string.h>
12959 +#include <linux/slab.h>
12960 +#include <linux/vmalloc.h>
12962 +#define YCHAR char
12963 +#define YUCHAR unsigned char
12965 +#define yaffs_strcpy(a,b) strcpy(a,b)
12966 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
12967 +#define yaffs_strlen(s) strlen(s)
12968 +#define yaffs_sprintf sprintf
12969 +#define yaffs_toupper(a) toupper(a)
12971 +#define Y_INLINE inline
12973 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
12974 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
12976 +/* #define YPRINTF(x) printk x */
12977 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
12978 +#define YFREE(x) kfree(x)
12979 +#define YMALLOC_ALT(x) vmalloc(x)
12980 +#define YFREE_ALT(x) vfree(x)
12981 +#define YMALLOC_DMA(x) YMALLOC(x)
12983 +// KR - added for use in scan so processes aren't blocked indefinitely.
12984 +#define YYIELD() schedule()
12986 +#define YAFFS_ROOT_MODE 0666
12987 +#define YAFFS_LOSTNFOUND_MODE 0666
12989 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
12990 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
12991 +#define Y_TIME_CONVERT(x) (x).tv_sec
12993 +#define Y_CURRENT_TIME CURRENT_TIME
12994 +#define Y_TIME_CONVERT(x) (x)
12997 +#define yaffs_SumCompare(x,y) ((x) == (y))
12998 +#define yaffs_strcmp(a,b) strcmp(a,b)
13000 +#define TENDSTR "\n"
13001 +#define TSTR(x) KERN_WARNING x
13002 +#define TOUT(p) printk p
13004 +#elif defined CONFIG_YAFFS_DIRECT
13006 +/* Direct interface */
13007 +#include "ydirectenv.h"
13009 +#elif defined CONFIG_YAFFS_UTIL
13011 +/* Stuff for YAFFS utilities */
13013 +#include "stdlib.h"
13014 +#include "stdio.h"
13015 +#include "string.h"
13017 +#include "devextras.h"
13019 +#define YMALLOC(x) malloc(x)
13020 +#define YFREE(x) free(x)
13021 +#define YMALLOC_ALT(x) malloc(x)
13022 +#define YFREE_ALT(x) free(x)
13024 +#define YCHAR char
13025 +#define YUCHAR unsigned char
13027 +#define yaffs_strcpy(a,b) strcpy(a,b)
13028 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
13029 +#define yaffs_strlen(s) strlen(s)
13030 +#define yaffs_sprintf sprintf
13031 +#define yaffs_toupper(a) toupper(a)
13033 +#define Y_INLINE inline
13035 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
13036 +/* #define YALERT(s) YINFO(s) */
13038 +#define TENDSTR "\n"
13040 +#define TOUT(p) printf p
13042 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
13043 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
13044 +/* #define YPRINTF(x) printf x */
13046 +#define YAFFS_ROOT_MODE 0666
13047 +#define YAFFS_LOSTNFOUND_MODE 0666
13049 +#define yaffs_SumCompare(x,y) ((x) == (y))
13050 +#define yaffs_strcmp(a,b) strcmp(a,b)
13053 +/* Should have specified a configuration type */
13054 +#error Unknown configuration
13058 +extern unsigned yaffs_traceMask;
13060 +#define YAFFS_TRACE_ERROR 0x00000001
13061 +#define YAFFS_TRACE_OS 0x00000002
13062 +#define YAFFS_TRACE_ALLOCATE 0x00000004
13063 +#define YAFFS_TRACE_SCAN 0x00000008
13064 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
13065 +#define YAFFS_TRACE_ERASE 0x00000020
13066 +#define YAFFS_TRACE_GC 0x00000040
13067 +#define YAFFS_TRACE_WRITE 0x00000080
13068 +#define YAFFS_TRACE_TRACING 0x00000100
13069 +#define YAFFS_TRACE_DELETION 0x00000200
13070 +#define YAFFS_TRACE_BUFFERS 0x00000400
13071 +#define YAFFS_TRACE_NANDACCESS 0x00000800
13072 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
13073 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
13074 +#define YAFFS_TRACE_MTD 0x00004000
13075 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
13076 +#define YAFFS_TRACE_ALWAYS 0x40000000
13077 +#define YAFFS_TRACE_BUG 0x80000000
13079 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ERROR)) TOUT(p);} while(0)
13081 +#ifndef CONFIG_YAFFS_WINCE
13082 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))