1 diff -urN linux-2.6.21.1/fs/yaffs2/Kconfig linux-2.6.21.1.new/fs/yaffs2/Kconfig
2 --- linux-2.6.21.1/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.new/fs/yaffs2/Kconfig 2007-05-30 13:17:16.000000000 +0200
6 +# YAFFS file system configurations
10 + tristate "YAFFS2 file system support"
16 + YAFFS2, or Yet Another Flash Filing System, is a filing system
17 + optimised for NAND Flash chips.
19 + To compile the YAFFS2 file system support as a module, choose M
20 + here: the module will be called yaffs2.
24 + Further information on YAFFS2 is available at
25 + <http://www.aleph1.co.uk/yaffs/>.
28 + bool "512 byte / page devices"
32 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
34 + Not needed for 2K-page devices.
38 +config YAFFS_9BYTE_TAGS
39 + bool "Use older-style on-NAND data format with pageStatus byte"
40 + depends on YAFFS_YAFFS1
44 + Older-style on-NAND data format has a "pageStatus" byte to record
45 + chunk/page state. This byte is zero when the page is discarded.
46 + Choose this option if you have existing on-NAND data using this
47 + format that you need to continue to support. New data written
48 + also uses the older-style format. Note: Use of this option
49 + generally requires that MTD's oob layout be adjusted to use the
50 + older-style format. See notes on tags formats and MTD versions.
54 +config YAFFS_DOES_ECC
55 + bool "Lets Yaffs do its own ECC"
56 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
59 + This enables Yaffs to use its own ECC functions instead of using
60 + the ones from the generic MTD-NAND driver.
64 +config YAFFS_ECC_WRONG_ORDER
65 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
66 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
69 + This makes yaffs_ecc.c use the same ecc byte order as Steven
70 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
71 + order as SmartMedia.
76 + bool "2048 byte (or larger) / page devices"
80 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
84 +config YAFFS_AUTO_YAFFS2
85 + bool "Autoselect yaffs2 format"
86 + depends on YAFFS_YAFFS2
89 + Without this, you need to explicitely use yaffs2 as the file
90 + system type. With this, you can say "yaffs" and yaffs or yaffs2
91 + will be used depending on the device page size (yaffs on
92 + 512-byte page devices, yaffs2 on 2K page devices).
96 +config YAFFS_DISABLE_LAZY_LOAD
97 + bool "Disable lazy loading"
98 + depends on YAFFS_YAFFS2
101 + "Lazy loading" defers loading file details until they are
102 + required. This saves mount time, but makes the first look-up
105 + Lazy loading will only happen if enabled by this option being 'n'
106 + and if the appropriate tags are available, else yaffs2 will
107 + automatically fall back to immediate loading and do the right
110 + Lazy laoding will be required by checkpointing.
112 + Setting this to 'y' will disable lazy loading.
116 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
117 + int "Reserved blocks for checkpointing"
118 + depends on YAFFS_YAFFS2
121 + Give the number of Blocks to reserve for checkpointing.
122 + Checkpointing saves the state at unmount so that mounting is
123 + much faster as a scan of all the flash to regenerate this state
124 + is not needed. These Blocks are reserved per partition, so if
125 + you have very small partitions the default (10) may be a mess
126 + for you. You can set this value to 0, but that does not mean
127 + checkpointing is disabled at all. There only won't be any
128 + specially reserved blocks for checkpointing, so if there is
129 + enough free space on the filesystem, it will be used for
132 + If unsure, leave at default (10), but don't wonder if there are
133 + always 2MB used on your large page device partition (10 x 2k
134 + pagesize). When using small partitions or when being very small
135 + on space, you probably want to set this to zero.
137 +config YAFFS_DISABLE_WIDE_TNODES
138 + bool "Turn off wide tnodes"
139 + depends on YAFFS_FS
142 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
143 + page devices and >=128MB for 2k page devices. They use slightly
144 + more RAM but are faster since they eliminate chunk group
147 + Setting this to 'y' will force tnode width to 16 bits and save
148 + memory but make large arrays slower.
152 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
153 + bool "Force chunk erase check"
154 + depends on YAFFS_FS
157 + Normally YAFFS only checks chunks before writing until an erased
158 + chunk is found. This helps to detect any partially written
159 + chunks that might have happened due to power loss.
161 + Enabling this forces on the test that chunks are erased in flash
162 + before writing to them. This takes more time but is potentially
165 + Suggest setting Y during development and ironing out driver
166 + issues etc. Suggest setting to N if you want faster writing.
170 +config YAFFS_SHORT_NAMES_IN_RAM
171 + bool "Cache short names in RAM"
172 + depends on YAFFS_FS
175 + If this config is set, then short names are stored with the
176 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
177 + but makes look-ups faster.
180 diff -urN linux-2.6.21.1/fs/yaffs2/Makefile linux-2.6.21.1.new/fs/yaffs2/Makefile
181 --- linux-2.6.21.1/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
182 +++ linux-2.6.21.1.new/fs/yaffs2/Makefile 2007-05-30 13:17:16.000000000 +0200
185 +# Makefile for the linux YAFFS filesystem routines.
188 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
190 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
191 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
192 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
193 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
194 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
195 diff -urN linux-2.6.21.1/fs/yaffs2/devextras.h linux-2.6.21.1.new/fs/yaffs2/devextras.h
196 --- linux-2.6.21.1/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
197 +++ linux-2.6.21.1.new/fs/yaffs2/devextras.h 2007-05-30 13:17:16.000000000 +0200
200 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
202 + * Copyright (C) 2002-2007 Aleph One Ltd.
203 + * for Toby Churchill Ltd and Brightstar Engineering
205 + * Created by Charles Manning <charles@aleph1.co.uk>
207 + * This program is free software; you can redistribute it and/or modify
208 + * it under the terms of the GNU Lesser General Public License version 2.1 as
209 + * published by the Free Software Foundation.
211 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
215 + * This file is just holds extra declarations used during development.
216 + * Most of these are from kernel includes placed here so we can use them in
221 +#ifndef __EXTRAS_H__
222 +#define __EXTRAS_H__
225 +#define __inline__ __inline
229 +#if !(defined __KERNEL__) || (defined WIN32)
231 +/* User space defines */
233 +typedef unsigned char __u8;
234 +typedef unsigned short __u16;
235 +typedef unsigned __u32;
238 + * Simple doubly linked list implementation.
240 + * Some of the internal functions ("__xxx") are useful when
241 + * manipulating whole lists rather than single entries, as
242 + * sometimes we already know the next/prev entries and we can
243 + * generate better code by using them directly rather than
244 + * using the generic single-entry routines.
247 +#define prefetch(x) 1
250 + struct list_head *next, *prev;
253 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
255 +#define LIST_HEAD(name) \
256 + struct list_head name = LIST_HEAD_INIT(name)
258 +#define INIT_LIST_HEAD(ptr) do { \
259 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
263 + * Insert a new entry between two known consecutive entries.
265 + * This is only for internal list manipulation where we know
266 + * the prev/next entries already!
268 +static __inline__ void __list_add(struct list_head *new,
269 + struct list_head *prev,
270 + struct list_head *next)
279 + * list_add - add a new entry
280 + * @new: new entry to be added
281 + * @head: list head to add it after
283 + * Insert a new entry after the specified head.
284 + * This is good for implementing stacks.
286 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
288 + __list_add(new, head, head->next);
292 + * list_add_tail - add a new entry
293 + * @new: new entry to be added
294 + * @head: list head to add it before
296 + * Insert a new entry before the specified head.
297 + * This is useful for implementing queues.
299 +static __inline__ void list_add_tail(struct list_head *new,
300 + struct list_head *head)
302 + __list_add(new, head->prev, head);
306 + * Delete a list entry by making the prev/next entries
307 + * point to each other.
309 + * This is only for internal list manipulation where we know
310 + * the prev/next entries already!
312 +static __inline__ void __list_del(struct list_head *prev,
313 + struct list_head *next)
320 + * list_del - deletes entry from list.
321 + * @entry: the element to delete from the list.
322 + * Note: list_empty on entry does not return true after this, the entry is
323 + * in an undefined state.
325 +static __inline__ void list_del(struct list_head *entry)
327 + __list_del(entry->prev, entry->next);
331 + * list_del_init - deletes entry from list and reinitialize it.
332 + * @entry: the element to delete from the list.
334 +static __inline__ void list_del_init(struct list_head *entry)
336 + __list_del(entry->prev, entry->next);
337 + INIT_LIST_HEAD(entry);
341 + * list_empty - tests whether a list is empty
342 + * @head: the list to test.
344 +static __inline__ int list_empty(struct list_head *head)
346 + return head->next == head;
350 + * list_splice - join two lists
351 + * @list: the new list to add.
352 + * @head: the place to add it in the first list.
354 +static __inline__ void list_splice(struct list_head *list,
355 + struct list_head *head)
357 + struct list_head *first = list->next;
359 + if (first != list) {
360 + struct list_head *last = list->prev;
361 + struct list_head *at = head->next;
363 + first->prev = head;
364 + head->next = first;
372 + * list_entry - get the struct for this entry
373 + * @ptr: the &struct list_head pointer.
374 + * @type: the type of the struct this is embedded in.
375 + * @member: the name of the list_struct within the struct.
377 +#define list_entry(ptr, type, member) \
378 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
381 + * list_for_each - iterate over a list
382 + * @pos: the &struct list_head to use as a loop counter.
383 + * @head: the head for your list.
385 +#define list_for_each(pos, head) \
386 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
387 + pos = pos->next, prefetch(pos->next))
390 + * list_for_each_safe - iterate over a list safe against removal
392 + * @pos: the &struct list_head to use as a loop counter.
393 + * @n: another &struct list_head to use as temporary storage
394 + * @head: the head for your list.
396 +#define list_for_each_safe(pos, n, head) \
397 + for (pos = (head)->next, n = pos->next; pos != (head); \
398 + pos = n, n = pos->next)
403 +#define DT_UNKNOWN 0
414 +#include <sys/stat.h>
418 + * Attribute flags. These should be or-ed together to figure out what
419 + * has been changed!
425 +#define ATTR_ATIME 16
426 +#define ATTR_MTIME 32
427 +#define ATTR_CTIME 64
428 +#define ATTR_ATIME_SET 128
429 +#define ATTR_MTIME_SET 256
430 +#define ATTR_FORCE 512 /* Not a change, but a change it */
431 +#define ATTR_ATTR_FLAG 1024
434 + unsigned int ia_valid;
442 + unsigned int ia_attr_flags;
450 +#include <linux/types.h>
451 +#include <linux/list.h>
452 +#include <linux/fs.h>
453 +#include <linux/stat.h>
463 diff -urN linux-2.6.21.1/fs/yaffs2/moduleconfig.h linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h
464 --- linux-2.6.21.1/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
465 +++ linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h 2007-05-30 13:17:16.000000000 +0200
468 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
470 + * Copyright (C) 2002-2007 Aleph One Ltd.
471 + * for Toby Churchill Ltd and Brightstar Engineering
473 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
475 + * This program is free software; you can redistribute it and/or modify
476 + * it under the terms of the GNU Lesser General Public License version 2.1 as
477 + * published by the Free Software Foundation.
479 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
482 +#ifndef __YAFFS_CONFIG_H__
483 +#define __YAFFS_CONFIG_H__
485 +#ifdef YAFFS_OUT_OF_TREE
487 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
488 +#define CONFIG_YAFFS_FS
489 +#define CONFIG_YAFFS_YAFFS1
490 +#define CONFIG_YAFFS_YAFFS2
492 +/* These options are independent of each other. Select those that matter. */
494 +/* Default: Not selected */
495 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
496 +//#define CONFIG_YAFFS_DOES_ECC
498 +/* Default: Not selected */
499 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
500 +/* CONFIG_YAFFS_DOES_ECC is set */
501 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
503 +/* Default: Selected */
504 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
505 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
507 +/* Default: Selected */
508 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
509 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
512 +/* Meaning: set the count of blocks to reserve for checkpointing */
513 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
516 +Older-style on-NAND data format has a "pageStatus" byte to record
517 +chunk/page state. This byte is zeroed when the page is discarded.
518 +Choose this option if you have existing on-NAND data in this format
519 +that you need to continue to support. New data written also uses the
521 +Note: Use of this option generally requires that MTD's oob layout be
522 +adjusted to use the older-style format. See notes on tags formats and
525 +/* Default: Not selected */
526 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
527 +#define CONFIG_YAFFS_9BYTE_TAGS
529 +#endif /* YAFFS_OUT_OF_TREE */
531 +#endif /* __YAFFS_CONFIG_H__ */
532 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c
533 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
534 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c 2007-05-30 13:17:16.000000000 +0200
537 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
539 + * Copyright (C) 2002-2007 Aleph One Ltd.
540 + * for Toby Churchill Ltd and Brightstar Engineering
542 + * Created by Charles Manning <charles@aleph1.co.uk>
544 + * This program is free software; you can redistribute it and/or modify
545 + * it under the terms of the GNU General Public License version 2 as
546 + * published by the Free Software Foundation.
549 +const char *yaffs_checkptrw_c_version =
550 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
553 +#include "yaffs_checkptrw.h"
556 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
559 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
561 + T(YAFFS_TRACE_CHECKPOINT,
562 + (TSTR("checkpt blocks available = %d" TENDSTR),
566 + return (blocksAvailable <= 0) ? 0 : 1;
570 +static int yaffs_CheckpointErase(yaffs_Device *dev)
576 + if(!dev->eraseBlockInNAND)
578 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
579 + dev->internalStartBlock,dev->internalEndBlock));
581 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
582 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
583 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
584 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
585 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
586 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
587 + dev->nErasedBlocks++;
588 + dev->nFreeChunks += dev->nChunksPerBlock;
591 + dev->markNANDBlockBad(dev,i);
592 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
597 + dev->blocksInCheckpoint = 0;
603 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
606 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
607 + T(YAFFS_TRACE_CHECKPOINT,
608 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
609 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
611 + if(dev->checkpointNextBlock >= 0 &&
612 + dev->checkpointNextBlock <= dev->internalEndBlock &&
613 + blocksAvailable > 0){
615 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
616 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
617 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
618 + dev->checkpointNextBlock = i + 1;
619 + dev->checkpointCurrentBlock = i;
620 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
625 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
627 + dev->checkpointNextBlock = -1;
628 + dev->checkpointCurrentBlock = -1;
631 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
634 + yaffs_ExtendedTags tags;
636 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
637 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
639 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
640 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
641 + int chunk = i * dev->nChunksPerBlock;
642 + int realignedChunk = chunk - dev->chunkOffset;
644 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
645 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
646 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
648 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
649 + /* Right kind of block */
650 + dev->checkpointNextBlock = tags.objectId;
651 + dev->checkpointCurrentBlock = i;
652 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
653 + dev->blocksInCheckpoint++;
654 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
659 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
661 + dev->checkpointNextBlock = -1;
662 + dev->checkpointCurrentBlock = -1;
666 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
669 + /* Got the functions we need? */
670 + if (!dev->writeChunkWithTagsToNAND ||
671 + !dev->readChunkWithTagsFromNAND ||
672 + !dev->eraseBlockInNAND ||
673 + !dev->markNANDBlockBad)
676 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
679 + if(!dev->checkpointBuffer)
680 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
681 + if(!dev->checkpointBuffer)
685 + dev->checkpointPageSequence = 0;
687 + dev->checkpointOpenForWrite = forWriting;
689 + dev->checkpointByteCount = 0;
690 + dev->checkpointSum = 0;
691 + dev->checkpointXor = 0;
692 + dev->checkpointCurrentBlock = -1;
693 + dev->checkpointCurrentChunk = -1;
694 + dev->checkpointNextBlock = dev->internalStartBlock;
696 + /* Erase all the blocks in the checkpoint area */
698 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
699 + dev->checkpointByteOffset = 0;
700 + return yaffs_CheckpointErase(dev);
705 + /* Set to a value that will kick off a read */
706 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
707 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
708 + * going to be way more than we need */
709 + dev->blocksInCheckpoint = 0;
710 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
711 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
712 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
713 + dev->checkpointBlockList[i] = -1;
719 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
721 + __u32 compositeSum;
722 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
723 + *sum = compositeSum;
727 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
731 + int realignedChunk;
733 + yaffs_ExtendedTags tags;
735 + if(dev->checkpointCurrentBlock < 0){
736 + yaffs_CheckpointFindNextErasedBlock(dev);
737 + dev->checkpointCurrentChunk = 0;
740 + if(dev->checkpointCurrentBlock < 0)
743 + tags.chunkDeleted = 0;
744 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
745 + tags.chunkId = dev->checkpointPageSequence + 1;
746 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
747 + tags.byteCount = dev->nDataBytesPerChunk;
748 + if(dev->checkpointCurrentChunk == 0){
749 + /* First chunk we write for the block? Set block state to
751 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
752 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
753 + dev->blocksInCheckpoint++;
756 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
759 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
760 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
762 + realignedChunk = chunk - dev->chunkOffset;
764 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
765 + dev->checkpointByteOffset = 0;
766 + dev->checkpointPageSequence++;
767 + dev->checkpointCurrentChunk++;
768 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
769 + dev->checkpointCurrentChunk = 0;
770 + dev->checkpointCurrentBlock = -1;
772 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
778 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
784 + __u8 * dataBytes = (__u8 *)data;
788 + if(!dev->checkpointBuffer)
791 + if(!dev->checkpointOpenForWrite)
794 + while(i < nBytes && ok) {
798 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
799 + dev->checkpointSum += *dataBytes;
800 + dev->checkpointXor ^= *dataBytes;
802 + dev->checkpointByteOffset++;
805 + dev->checkpointByteCount++;
808 + if(dev->checkpointByteOffset < 0 ||
809 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
810 + ok = yaffs_CheckpointFlushBuffer(dev);
817 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
821 + yaffs_ExtendedTags tags;
825 + int realignedChunk;
827 + __u8 *dataBytes = (__u8 *)data;
829 + if(!dev->checkpointBuffer)
832 + if(dev->checkpointOpenForWrite)
835 + while(i < nBytes && ok) {
838 + if(dev->checkpointByteOffset < 0 ||
839 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
841 + if(dev->checkpointCurrentBlock < 0){
842 + yaffs_CheckpointFindNextCheckpointBlock(dev);
843 + dev->checkpointCurrentChunk = 0;
846 + if(dev->checkpointCurrentBlock < 0)
850 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
851 + dev->checkpointCurrentChunk;
853 + realignedChunk = chunk - dev->chunkOffset;
855 + /* read in the next chunk */
856 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
857 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
858 + dev->checkpointBuffer,
861 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
862 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
865 + dev->checkpointByteOffset = 0;
866 + dev->checkpointPageSequence++;
867 + dev->checkpointCurrentChunk++;
869 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
870 + dev->checkpointCurrentBlock = -1;
875 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
876 + dev->checkpointSum += *dataBytes;
877 + dev->checkpointXor ^= *dataBytes;
878 + dev->checkpointByteOffset++;
881 + dev->checkpointByteCount++;
888 +int yaffs_CheckpointClose(yaffs_Device *dev)
891 + if(dev->checkpointOpenForWrite){
892 + if(dev->checkpointByteOffset != 0)
893 + yaffs_CheckpointFlushBuffer(dev);
896 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
897 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
898 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
899 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
901 + // Todo this looks odd...
904 + YFREE(dev->checkpointBlockList);
905 + dev->checkpointBlockList = NULL;
908 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
909 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
912 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
913 + dev->checkpointByteCount));
915 + if(dev->checkpointBuffer){
916 + /* free the buffer */
917 + YFREE(dev->checkpointBuffer);
918 + dev->checkpointBuffer = NULL;
926 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
928 + /* Erase the first checksum block */
930 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
932 + if(!yaffs_CheckpointSpaceOk(dev))
935 + return yaffs_CheckpointErase(dev);
940 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h
941 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
942 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h 2007-05-30 13:17:16.000000000 +0200
945 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
947 + * Copyright (C) 2002-2007 Aleph One Ltd.
948 + * for Toby Churchill Ltd and Brightstar Engineering
950 + * Created by Charles Manning <charles@aleph1.co.uk>
952 + * This program is free software; you can redistribute it and/or modify
953 + * it under the terms of the GNU Lesser General Public License version 2.1 as
954 + * published by the Free Software Foundation.
956 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
959 +#ifndef __YAFFS_CHECKPTRW_H__
960 +#define __YAFFS_CHECKPTRW_H__
962 +#include "yaffs_guts.h"
964 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
966 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
968 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
970 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
972 +int yaffs_CheckpointClose(yaffs_Device *dev);
974 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
979 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c
980 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
981 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c 2007-05-30 13:17:16.000000000 +0200
984 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
986 + * Copyright (C) 2002-2007 Aleph One Ltd.
987 + * for Toby Churchill Ltd and Brightstar Engineering
989 + * Created by Charles Manning <charles@aleph1.co.uk>
991 + * This program is free software; you can redistribute it and/or modify
992 + * it under the terms of the GNU General Public License version 2 as
993 + * published by the Free Software Foundation.
997 + * This code implements the ECC algorithm used in SmartMedia.
999 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1000 + * The two unused bit are set to 1.
1001 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1002 + * blocks are used on a 512-byte NAND page.
1006 +/* Table generated by gen-ecc.c
1007 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1008 + * for each byte of data. These are instead provided in a table in bits7..2.
1009 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1010 + * this bytes influence on the line parity.
1013 +const char *yaffs_ecc_c_version =
1014 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1016 +#include "yportenv.h"
1018 +#include "yaffs_ecc.h"
1020 +static const unsigned char column_parity_table[] = {
1021 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1022 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1023 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1024 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1025 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1026 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1027 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1028 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1029 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1030 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1031 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1032 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1033 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1034 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1035 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1036 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1037 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1038 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1039 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1040 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1041 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1042 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1043 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1044 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1045 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1046 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1047 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1048 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1049 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1050 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1051 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1052 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1055 +/* Count the bits in an unsigned char or a U32 */
1057 +static int yaffs_CountBits(unsigned char x)
1068 +static int yaffs_CountBits32(unsigned x)
1079 +/* Calculate the ECC for a 256-byte block of data */
1080 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1084 + unsigned char col_parity = 0;
1085 + unsigned char line_parity = 0;
1086 + unsigned char line_parity_prime = 0;
1090 + for (i = 0; i < 256; i++) {
1091 + b = column_parity_table[*data++];
1094 + if (b & 0x01) // odd number of bits in the byte
1097 + line_parity_prime ^= ~i;
1102 + ecc[2] = (~col_parity) | 0x03;
1105 + if (line_parity & 0x80)
1107 + if (line_parity_prime & 0x80)
1109 + if (line_parity & 0x40)
1111 + if (line_parity_prime & 0x40)
1113 + if (line_parity & 0x20)
1115 + if (line_parity_prime & 0x20)
1117 + if (line_parity & 0x10)
1119 + if (line_parity_prime & 0x10)
1124 + if (line_parity & 0x08)
1126 + if (line_parity_prime & 0x08)
1128 + if (line_parity & 0x04)
1130 + if (line_parity_prime & 0x04)
1132 + if (line_parity & 0x02)
1134 + if (line_parity_prime & 0x02)
1136 + if (line_parity & 0x01)
1138 + if (line_parity_prime & 0x01)
1142 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1143 + // Swap the bytes into the wrong order
1151 +/* Correct the ECC on a 256 byte block of data */
1153 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1154 + const unsigned char *test_ecc)
1156 + unsigned char d0, d1, d2; /* deltas */
1158 + d0 = read_ecc[0] ^ test_ecc[0];
1159 + d1 = read_ecc[1] ^ test_ecc[1];
1160 + d2 = read_ecc[2] ^ test_ecc[2];
1162 + if ((d0 | d1 | d2) == 0)
1163 + return 0; /* no error */
1165 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1166 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1167 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1168 + /* Single bit (recoverable) error in data */
1173 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1174 + // swap the bytes to correct for the wrong order
1208 + data[byte] ^= (1 << bit);
1210 + return 1; /* Corrected the error */
1213 + if ((yaffs_CountBits(d0) +
1214 + yaffs_CountBits(d1) +
1215 + yaffs_CountBits(d2)) == 1) {
1216 + /* Reccoverable error in ecc */
1218 + read_ecc[0] = test_ecc[0];
1219 + read_ecc[1] = test_ecc[1];
1220 + read_ecc[2] = test_ecc[2];
1222 + return 1; /* Corrected the error */
1225 + /* Unrecoverable error */
1233 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1235 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1236 + yaffs_ECCOther * eccOther)
1240 + unsigned char col_parity = 0;
1241 + unsigned line_parity = 0;
1242 + unsigned line_parity_prime = 0;
1245 + for (i = 0; i < nBytes; i++) {
1246 + b = column_parity_table[*data++];
1250 + /* odd number of bits in the byte */
1252 + line_parity_prime ^= ~i;
1257 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1258 + eccOther->lineParity = line_parity;
1259 + eccOther->lineParityPrime = line_parity_prime;
1262 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1263 + yaffs_ECCOther * read_ecc,
1264 + const yaffs_ECCOther * test_ecc)
1266 + unsigned char cDelta; /* column parity delta */
1267 + unsigned lDelta; /* line parity delta */
1268 + unsigned lDeltaPrime; /* line parity delta */
1271 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1272 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1273 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1275 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1276 + return 0; /* no error */
1278 + if (lDelta == ~lDeltaPrime &&
1279 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1281 + /* Single bit (recoverable) error in data */
1285 + if (cDelta & 0x20)
1287 + if (cDelta & 0x08)
1289 + if (cDelta & 0x02)
1292 + if(lDelta >= nBytes)
1295 + data[lDelta] ^= (1 << bit);
1297 + return 1; /* corrected */
1300 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1301 + yaffs_CountBits(cDelta)) == 1) {
1302 + /* Reccoverable error in ecc */
1304 + *read_ecc = *test_ecc;
1305 + return 1; /* corrected */
1308 + /* Unrecoverable error */
1314 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h
1315 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1316 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h 2007-05-30 13:17:16.000000000 +0200
1319 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1321 + * Copyright (C) 2002-2007 Aleph One Ltd.
1322 + * for Toby Churchill Ltd and Brightstar Engineering
1324 + * Created by Charles Manning <charles@aleph1.co.uk>
1326 + * This program is free software; you can redistribute it and/or modify
1327 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1328 + * published by the Free Software Foundation.
1330 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1334 + * This code implements the ECC algorithm used in SmartMedia.
1336 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1337 + * The two unused bit are set to 1.
1338 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1339 + * blocks are used on a 512-byte NAND page.
1343 +#ifndef __YAFFS_ECC_H__
1344 +#define __YAFFS_ECC_H__
1347 + unsigned char colParity;
1348 + unsigned lineParity;
1349 + unsigned lineParityPrime;
1352 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1353 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1354 + const unsigned char *test_ecc);
1356 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1357 + yaffs_ECCOther * ecc);
1358 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1359 + yaffs_ECCOther * read_ecc,
1360 + const yaffs_ECCOther * test_ecc);
1362 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_fs.c linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c
1363 --- linux-2.6.21.1/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1364 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c 2007-05-30 13:17:16.000000000 +0200
1367 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1369 + * Copyright (C) 2002-2007 Aleph One Ltd.
1370 + * for Toby Churchill Ltd and Brightstar Engineering
1372 + * Created by Charles Manning <charles@aleph1.co.uk>
1373 + * Acknowledgements:
1374 + * Luc van OostenRyck for numerous patches.
1375 + * Nick Bane for numerous patches.
1376 + * Nick Bane for 2.5/2.6 integration.
1377 + * Andras Toth for mknod rdev issue.
1378 + * Michael Fischer for finding the problem with inode inconsistency.
1379 + * Some code bodily lifted from JFFS
1381 + * This program is free software; you can redistribute it and/or modify
1382 + * it under the terms of the GNU General Public License version 2 as
1383 + * published by the Free Software Foundation.
1388 + * This is the file system front-end to YAFFS that hooks it up to
1392 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1394 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1396 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1399 +const char *yaffs_fs_c_version =
1400 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1401 +extern const char *yaffs_guts_c_version;
1403 +#include <linux/version.h>
1404 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1405 +#include <linux/config.h>
1407 +#include <linux/kernel.h>
1408 +#include <linux/module.h>
1409 +#include <linux/slab.h>
1410 +#include <linux/init.h>
1411 +#include <linux/list.h>
1412 +#include <linux/fs.h>
1413 +#include <linux/proc_fs.h>
1414 +#include <linux/smp_lock.h>
1415 +#include <linux/pagemap.h>
1416 +#include <linux/mtd/mtd.h>
1417 +#include <linux/interrupt.h>
1418 +#include <linux/string.h>
1419 +#include <linux/ctype.h>
1421 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1423 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1424 +#include <asm/statfs.h>
1425 +#define UnlockPage(p) unlock_page(p)
1426 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1428 +/* FIXME: use sb->s_id instead ? */
1429 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1433 +#include <linux/locks.h>
1434 +#define BDEVNAME_SIZE 0
1435 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1437 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1438 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1444 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1445 +#define WRITE_SIZE_STR "writesize"
1446 +#define WRITE_SIZE(mtd) (mtd)->writesize
1448 +#define WRITE_SIZE_STR "oobblock"
1449 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1452 +#include <asm/uaccess.h>
1454 +#include "yportenv.h"
1455 +#include "yaffs_guts.h"
1457 +#include <linux/mtd/mtd.h>
1458 +#include "yaffs_mtdif.h"
1459 +#include "yaffs_mtdif1.h"
1460 +#include "yaffs_mtdif2.h"
1462 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1463 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1465 +/* Module Parameters */
1466 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1467 +module_param(yaffs_traceMask,uint,0644);
1468 +module_param(yaffs_wr_attempts,uint,0644);
1470 +MODULE_PARM(yaffs_traceMask,"i");
1471 +MODULE_PARM(yaffs_wr_attempts,"i");
1474 +/*#define T(x) printk x */
1476 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1477 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1479 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1482 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1483 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1485 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1486 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1488 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1491 +static void yaffs_put_super(struct super_block *sb);
1493 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1496 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1497 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1499 +static int yaffs_file_flush(struct file *file);
1502 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1505 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1507 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1508 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1509 + struct nameidata *n);
1510 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1511 + struct nameidata *n);
1513 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1514 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1516 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1517 + struct dentry *dentry);
1518 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1519 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1520 + const char *symname);
1521 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1523 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1524 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1527 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1530 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1531 + struct inode *new_dir, struct dentry *new_dentry);
1532 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1534 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1535 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1536 +static void yaffs_write_super(struct super_block *sb);
1538 +static int yaffs_sync_fs(struct super_block *sb);
1539 +static int yaffs_write_super(struct super_block *sb);
1542 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1543 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1544 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1545 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1547 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1549 +static void yaffs_read_inode(struct inode *inode);
1551 +static void yaffs_put_inode(struct inode *inode);
1552 +static void yaffs_delete_inode(struct inode *);
1553 +static void yaffs_clear_inode(struct inode *);
1555 +static int yaffs_readpage(struct file *file, struct page *page);
1556 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1557 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1559 +static int yaffs_writepage(struct page *page);
1561 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1562 + unsigned offset, unsigned to);
1563 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1566 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1568 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1569 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1571 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1574 +static struct address_space_operations yaffs_file_address_operations = {
1575 + .readpage = yaffs_readpage,
1576 + .writepage = yaffs_writepage,
1577 + .prepare_write = yaffs_prepare_write,
1578 + .commit_write = yaffs_commit_write,
1581 +static struct file_operations yaffs_file_operations = {
1582 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1583 + .read = do_sync_read,
1584 + .write = do_sync_write,
1585 + .aio_read = generic_file_aio_read,
1586 + .aio_write = generic_file_aio_write,
1588 + .read = generic_file_read,
1589 + .write = generic_file_write,
1591 + .mmap = generic_file_mmap,
1592 + .flush = yaffs_file_flush,
1593 + .fsync = yaffs_sync_object,
1594 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1595 + .sendfile = generic_file_sendfile,
1600 +static struct inode_operations yaffs_file_inode_operations = {
1601 + .setattr = yaffs_setattr,
1604 +static struct inode_operations yaffs_symlink_inode_operations = {
1605 + .readlink = yaffs_readlink,
1606 + .follow_link = yaffs_follow_link,
1607 + .setattr = yaffs_setattr,
1610 +static struct inode_operations yaffs_dir_inode_operations = {
1611 + .create = yaffs_create,
1612 + .lookup = yaffs_lookup,
1613 + .link = yaffs_link,
1614 + .unlink = yaffs_unlink,
1615 + .symlink = yaffs_symlink,
1616 + .mkdir = yaffs_mkdir,
1617 + .rmdir = yaffs_unlink,
1618 + .mknod = yaffs_mknod,
1619 + .rename = yaffs_rename,
1620 + .setattr = yaffs_setattr,
1623 +static struct file_operations yaffs_dir_operations = {
1624 + .read = generic_read_dir,
1625 + .readdir = yaffs_readdir,
1626 + .fsync = yaffs_sync_object,
1629 +static struct super_operations yaffs_super_ops = {
1630 + .statfs = yaffs_statfs,
1631 + .read_inode = yaffs_read_inode,
1632 + .put_inode = yaffs_put_inode,
1633 + .put_super = yaffs_put_super,
1634 + .delete_inode = yaffs_delete_inode,
1635 + .clear_inode = yaffs_clear_inode,
1636 + .sync_fs = yaffs_sync_fs,
1637 + .write_super = yaffs_write_super,
1640 +static void yaffs_GrossLock(yaffs_Device * dev)
1642 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1644 + down(&dev->grossLock);
1647 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1649 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1650 + up(&dev->grossLock);
1654 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1657 + unsigned char *alias;
1660 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1662 + yaffs_GrossLock(dev);
1664 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1666 + yaffs_GrossUnlock(dev);
1671 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1676 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1677 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1679 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1682 + unsigned char *alias;
1684 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1686 + yaffs_GrossLock(dev);
1688 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1690 + yaffs_GrossUnlock(dev);
1698 + ret = vfs_follow_link(nd, alias);
1701 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1702 + return ERR_PTR (ret);
1708 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1709 + yaffs_Object * obj);
1712 + * Lookup is used to find objects in the fs
1714 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1716 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1717 + struct nameidata *n)
1719 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1722 + yaffs_Object *obj;
1723 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1725 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1727 + yaffs_GrossLock(dev);
1730 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1731 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1734 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1735 + dentry->d_name.name);
1737 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1739 + /* Can't hold gross lock when calling yaffs_get_inode() */
1740 + yaffs_GrossUnlock(dev);
1744 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1746 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1750 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1751 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1752 + * d_add even if NULL inode */
1754 + /*dget(dentry); // try to solve directory bug */
1755 + d_add(dentry, inode);
1757 + /* return dentry; */
1763 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1767 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1768 + * NULL which creates dentry hash */
1769 + d_add(dentry, inode);
1772 + /* return (ERR_PTR(-EIO)); */
1776 +/* For now put inode is just for debugging
1777 + * Put inode is called when the inode **structure** is put.
1779 +static void yaffs_put_inode(struct inode *inode)
1782 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1783 + atomic_read(&inode->i_count)));
1787 +/* clear is called to tell the fs to release any per-inode data it holds */
1788 +static void yaffs_clear_inode(struct inode *inode)
1790 + yaffs_Object *obj;
1791 + yaffs_Device *dev;
1793 + obj = yaffs_InodeToObject(inode);
1796 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1797 + atomic_read(&inode->i_count),
1798 + obj ? "object exists" : "null object"));
1802 + yaffs_GrossLock(dev);
1804 + /* Clear the association between the inode and
1805 + * the yaffs_Object.
1807 + obj->myInode = NULL;
1808 + yaffs_InodeToObjectLV(inode) = NULL;
1810 + /* If the object freeing was deferred, then the real
1811 + * free happens now.
1812 + * This should fix the inode inconsistency problem.
1815 + yaffs_HandleDeferedFree(obj);
1817 + yaffs_GrossUnlock(dev);
1822 +/* delete is called when the link count is zero and the inode
1823 + * is put (ie. nobody wants to know about it anymore, time to
1824 + * delete the file).
1825 + * NB Must call clear_inode()
1827 +static void yaffs_delete_inode(struct inode *inode)
1829 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1830 + yaffs_Device *dev;
1833 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1834 + atomic_read(&inode->i_count),
1835 + obj ? "object exists" : "null object"));
1839 + yaffs_GrossLock(dev);
1840 + yaffs_DeleteFile(obj);
1841 + yaffs_GrossUnlock(dev);
1843 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1844 + truncate_inode_pages (&inode->i_data, 0);
1846 + clear_inode(inode);
1849 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1850 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1852 +static int yaffs_file_flush(struct file *file)
1855 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1857 + yaffs_Device *dev = obj->myDev;
1860 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1861 + obj->dirty ? "dirty" : "clean"));
1863 + yaffs_GrossLock(dev);
1865 + yaffs_FlushFile(obj, 1);
1867 + yaffs_GrossUnlock(dev);
1872 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1874 + /* Lifted from jffs2 */
1876 + yaffs_Object *obj;
1877 + unsigned char *pg_buf;
1880 + yaffs_Device *dev;
1882 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1883 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1884 + (unsigned)PAGE_CACHE_SIZE));
1886 + obj = yaffs_DentryToObject(f->f_dentry);
1890 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1891 + BUG_ON(!PageLocked(pg));
1893 + if (!PageLocked(pg))
1897 + pg_buf = kmap(pg);
1898 + /* FIXME: Can kmap fail? */
1900 + yaffs_GrossLock(dev);
1903 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1906 + yaffs_GrossUnlock(dev);
1912 + ClearPageUptodate(pg);
1915 + SetPageUptodate(pg);
1916 + ClearPageError(pg);
1919 + flush_dcache_page(pg);
1922 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1926 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1928 + int ret = yaffs_readpage_nolock(f, pg);
1933 +static int yaffs_readpage(struct file *f, struct page *pg)
1935 + return yaffs_readpage_unlock(f, pg);
1938 +/* writepage inspired by/stolen from smbfs */
1940 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1941 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1943 +static int yaffs_writepage(struct page *page)
1946 + struct address_space *mapping = page->mapping;
1947 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1948 + struct inode *inode;
1949 + unsigned long end_index;
1951 + yaffs_Object *obj;
1957 + inode = mapping->host;
1961 + if (offset > inode->i_size) {
1964 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1965 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1966 + (unsigned)inode->i_size));
1968 + (KERN_DEBUG " -> don't care!!\n"));
1969 + unlock_page(page);
1973 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1976 + if (page->index < end_index) {
1977 + nBytes = PAGE_CACHE_SIZE;
1979 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1984 + buffer = kmap(page);
1986 + obj = yaffs_InodeToObject(inode);
1987 + yaffs_GrossLock(obj->myDev);
1990 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
1991 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1993 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
1994 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1997 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2001 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2002 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2004 + yaffs_GrossUnlock(obj->myDev);
2007 + SetPageUptodate(page);
2011 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2014 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2015 + unsigned offset, unsigned to)
2018 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2019 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2020 + return yaffs_readpage_nolock(f, pg);
2026 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2030 + void *addr = page_address(pg) + offset;
2031 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2032 + int nBytes = to - offset;
2035 + unsigned spos = pos;
2036 + unsigned saddr = (unsigned)addr;
2039 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2042 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2044 + if (nWritten != nBytes) {
2047 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2048 + nWritten, nBytes));
2050 + ClearPageUptodate(pg);
2052 + SetPageUptodate(pg);
2056 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2057 + nWritten == nBytes ? 0 : nWritten));
2059 + return nWritten == nBytes ? 0 : nWritten;
2063 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2065 + if (inode && obj) {
2068 + /* Check mode against the variant type and attempt to repair if broken. */
2069 + __u32 mode = obj->yst_mode;
2070 + switch( obj->variantType ){
2071 + case YAFFS_OBJECT_TYPE_FILE :
2072 + if( ! S_ISREG(mode) ){
2073 + obj->yst_mode &= ~S_IFMT;
2074 + obj->yst_mode |= S_IFREG;
2078 + case YAFFS_OBJECT_TYPE_SYMLINK :
2079 + if( ! S_ISLNK(mode) ){
2080 + obj->yst_mode &= ~S_IFMT;
2081 + obj->yst_mode |= S_IFLNK;
2085 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2086 + if( ! S_ISDIR(mode) ){
2087 + obj->yst_mode &= ~S_IFMT;
2088 + obj->yst_mode |= S_IFDIR;
2092 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2093 + case YAFFS_OBJECT_TYPE_HARDLINK :
2094 + case YAFFS_OBJECT_TYPE_SPECIAL :
2100 + inode->i_ino = obj->objectId;
2101 + inode->i_mode = obj->yst_mode;
2102 + inode->i_uid = obj->yst_uid;
2103 + inode->i_gid = obj->yst_gid;
2104 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2105 + inode->i_blksize = inode->i_sb->s_blocksize;
2107 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2109 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2110 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2111 + inode->i_atime.tv_nsec = 0;
2112 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2113 + inode->i_mtime.tv_nsec = 0;
2114 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2115 + inode->i_ctime.tv_nsec = 0;
2117 + inode->i_rdev = obj->yst_rdev;
2118 + inode->i_atime = obj->yst_atime;
2119 + inode->i_mtime = obj->yst_mtime;
2120 + inode->i_ctime = obj->yst_ctime;
2122 + inode->i_size = yaffs_GetObjectFileLength(obj);
2123 + inode->i_blocks = (inode->i_size + 511) >> 9;
2125 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2129 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2130 + inode->i_mode, inode->i_uid, inode->i_gid,
2131 + (int)inode->i_size, atomic_read(&inode->i_count)));
2133 + switch (obj->yst_mode & S_IFMT) {
2134 + default: /* fifo, device or socket */
2135 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2136 + init_special_inode(inode, obj->yst_mode,
2137 + old_decode_dev(obj->yst_rdev));
2139 + init_special_inode(inode, obj->yst_mode,
2140 + (dev_t) (obj->yst_rdev));
2143 + case S_IFREG: /* file */
2144 + inode->i_op = &yaffs_file_inode_operations;
2145 + inode->i_fop = &yaffs_file_operations;
2146 + inode->i_mapping->a_ops =
2147 + &yaffs_file_address_operations;
2149 + case S_IFDIR: /* directory */
2150 + inode->i_op = &yaffs_dir_inode_operations;
2151 + inode->i_fop = &yaffs_dir_operations;
2153 + case S_IFLNK: /* symlink */
2154 + inode->i_op = &yaffs_symlink_inode_operations;
2158 + yaffs_InodeToObjectLV(inode) = obj;
2160 + obj->myInode = inode;
2164 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2169 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2170 + yaffs_Object * obj)
2172 + struct inode *inode;
2176 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2183 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2189 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2191 + inode = iget(sb, obj->objectId);
2193 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2194 + /* iget also increments the inode's i_count */
2195 + /* NB You can't be holding grossLock or deadlock will happen! */
2200 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2203 + yaffs_Object *obj;
2204 + int nWritten, ipos;
2205 + struct inode *inode;
2206 + yaffs_Device *dev;
2208 + obj = yaffs_DentryToObject(f->f_dentry);
2212 + yaffs_GrossLock(dev);
2214 + inode = f->f_dentry->d_inode;
2216 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2217 + ipos = inode->i_size;
2224 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2228 + "yaffs_file_write about to write writing %d bytes"
2229 + "to object %d at %d\n",
2230 + n, obj->objectId, ipos));
2233 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2236 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2237 + n, nWritten, ipos));
2238 + if (nWritten > 0) {
2241 + if (ipos > inode->i_size) {
2242 + inode->i_size = ipos;
2243 + inode->i_blocks = (ipos + 511) >> 9;
2247 + "yaffs_file_write size updated to %d bytes, "
2249 + ipos, (int)(inode->i_blocks)));
2253 + yaffs_GrossUnlock(dev);
2254 + return nWritten == 0 ? -ENOSPC : nWritten;
2257 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2259 + yaffs_Object *obj;
2260 + yaffs_Device *dev;
2261 + struct inode *inode = f->f_dentry->d_inode;
2262 + unsigned long offset, curoffs;
2263 + struct list_head *i;
2266 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2268 + obj = yaffs_DentryToObject(f->f_dentry);
2271 + yaffs_GrossLock(dev);
2273 + offset = f->f_pos;
2275 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2277 + if (offset == 0) {
2279 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2280 + (int)inode->i_ino));
2281 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2288 + if (offset == 1) {
2290 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2291 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2293 + (dirent, "..", 2, offset,
2294 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2303 + /* If the directory has changed since the open or last call to
2304 + readdir, rewind to after the 2 canned entries. */
2306 + if (f->f_version != inode->i_version) {
2308 + f->f_pos = offset;
2309 + f->f_version = inode->i_version;
2312 + list_for_each(i, &obj->variant.directoryVariant.children) {
2314 + if (curoffs >= offset) {
2315 + l = list_entry(i, yaffs_Object, siblings);
2317 + yaffs_GetObjectName(l, name,
2318 + YAFFS_MAX_NAME_LENGTH + 1);
2320 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2321 + yaffs_GetObjectInode(l)));
2323 + if (filldir(dirent,
2327 + yaffs_GetObjectInode(l),
2328 + yaffs_GetObjectType(l))
2341 + yaffs_GrossUnlock(dev);
2347 + * File creation. Allocate an inode, and we're done..
2349 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2350 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2353 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2357 + struct inode *inode;
2359 + yaffs_Object *obj = NULL;
2360 + yaffs_Device *dev;
2362 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2364 + int error = -ENOSPC;
2365 + uid_t uid = current->fsuid;
2366 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2368 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2373 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2374 + parent->objectId, parent->variantType));
2377 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2381 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2382 + "mode %x dev %x\n",
2383 + dentry->d_name.name, mode, rdev));
2385 + dev = parent->myDev;
2387 + yaffs_GrossLock(dev);
2389 + switch (mode & S_IFMT) {
2391 + /* Special (socket, fifo, device...) */
2392 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2393 + "yaffs_mknod: making special\n"));
2394 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2396 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2397 + gid, old_encode_dev(rdev));
2400 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2404 + case S_IFREG: /* file */
2405 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2407 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2410 + case S_IFDIR: /* directory */
2412 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2414 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2417 + case S_IFLNK: /* symlink */
2418 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2419 + obj = NULL; /* Do we ever get here? */
2423 + /* Can not call yaffs_get_inode() with gross lock held */
2424 + yaffs_GrossUnlock(dev);
2427 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2428 + d_instantiate(dentry, inode);
2430 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2431 + obj->objectId, atomic_read(&inode->i_count)));
2435 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2442 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2445 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2446 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2448 + /* attempt to fix dir bug - didn't work */
2456 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2457 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2458 + struct nameidata *n)
2460 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2463 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2464 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2467 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2471 + yaffs_Device *dev;
2474 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2475 + dentry->d_name.name));
2477 + dev = yaffs_InodeToObject(dir)->myDev;
2479 + yaffs_GrossLock(dev);
2481 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2483 + if (retVal == YAFFS_OK) {
2484 + dentry->d_inode->i_nlink--;
2486 + yaffs_GrossUnlock(dev);
2487 + mark_inode_dirty(dentry->d_inode);
2490 + yaffs_GrossUnlock(dev);
2491 + return -ENOTEMPTY;
2495 + * Create a link...
2497 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2498 + struct dentry *dentry)
2500 + struct inode *inode = old_dentry->d_inode;
2501 + yaffs_Object *obj = NULL;
2502 + yaffs_Object *link = NULL;
2503 + yaffs_Device *dev;
2505 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2507 + obj = yaffs_InodeToObject(inode);
2510 + yaffs_GrossLock(dev);
2512 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2515 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2520 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2521 + d_instantiate(dentry, old_dentry->d_inode);
2522 + atomic_inc(&old_dentry->d_inode->i_count);
2524 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2525 + old_dentry->d_inode->i_nlink,
2526 + atomic_read(&old_dentry->d_inode->i_count)));
2530 + yaffs_GrossUnlock(dev);
2540 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2541 + const char *symname)
2543 + yaffs_Object *obj;
2544 + yaffs_Device *dev;
2545 + uid_t uid = current->fsuid;
2546 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2548 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2550 + dev = yaffs_InodeToObject(dir)->myDev;
2551 + yaffs_GrossLock(dev);
2552 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2553 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2554 + yaffs_GrossUnlock(dev);
2558 + struct inode *inode;
2560 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2561 + d_instantiate(dentry, inode);
2562 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2565 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2572 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2576 + yaffs_Object *obj;
2577 + yaffs_Device *dev;
2579 + obj = yaffs_DentryToObject(dentry);
2583 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2584 + yaffs_GrossLock(dev);
2585 + yaffs_FlushFile(obj, 1);
2586 + yaffs_GrossUnlock(dev);
2591 + * The VFS layer already does all the dentry stuff for rename.
2593 + * NB: POSIX says you can rename an object over an old object of the same name
2595 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2596 + struct inode *new_dir, struct dentry *new_dentry)
2598 + yaffs_Device *dev;
2599 + int retVal = YAFFS_FAIL;
2600 + yaffs_Object *target;
2602 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2603 + dev = yaffs_InodeToObject(old_dir)->myDev;
2605 + yaffs_GrossLock(dev);
2607 + /* Check if the target is an existing directory that is not empty. */
2609 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2610 + new_dentry->d_name.name);
2615 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2616 + !list_empty(&target->variant.directoryVariant.children)) {
2618 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2620 + retVal = YAFFS_FAIL;
2623 + /* Now does unlinking internally using shadowing mechanism */
2624 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2627 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2628 + old_dentry->d_name.name,
2629 + yaffs_InodeToObject(new_dir),
2630 + new_dentry->d_name.name);
2633 + yaffs_GrossUnlock(dev);
2635 + if (retVal == YAFFS_OK) {
2637 + new_dentry->d_inode->i_nlink--;
2638 + mark_inode_dirty(new_dentry->d_inode);
2643 + return -ENOTEMPTY;
2648 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2650 + struct inode *inode = dentry->d_inode;
2652 + yaffs_Device *dev;
2655 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2656 + yaffs_InodeToObject(inode)->objectId));
2658 + if ((error = inode_change_ok(inode, attr)) == 0) {
2660 + dev = yaffs_InodeToObject(inode)->myDev;
2661 + yaffs_GrossLock(dev);
2662 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2668 + yaffs_GrossUnlock(dev);
2670 + error = inode_setattr(inode, attr);
2675 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2676 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2678 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2679 + struct super_block *sb = dentry->d_sb;
2680 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2681 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2683 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2685 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2687 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2690 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2692 + yaffs_GrossLock(dev);
2694 + buf->f_type = YAFFS_MAGIC;
2695 + buf->f_bsize = sb->s_blocksize;
2696 + buf->f_namelen = 255;
2697 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2700 + (dev->endBlock - dev->startBlock +
2701 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2702 + dev->nDataBytesPerChunk);
2704 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2705 + dev->nDataBytesPerChunk);
2709 + (dev->endBlock - dev->startBlock +
2710 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2713 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2718 + buf->f_bavail = buf->f_bfree;
2720 + yaffs_GrossUnlock(dev);
2726 +static int yaffs_do_sync_fs(struct super_block *sb)
2729 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2730 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2733 + yaffs_GrossLock(dev);
2736 + yaffs_CheckpointSave(dev);
2738 + yaffs_GrossUnlock(dev);
2746 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2747 +static void yaffs_write_super(struct super_block *sb)
2749 +static int yaffs_write_super(struct super_block *sb)
2753 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2754 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2755 + return 0; /* yaffs_do_sync_fs(sb);*/
2760 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2761 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2763 +static int yaffs_sync_fs(struct super_block *sb)
2767 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2769 + return 0; /* yaffs_do_sync_fs(sb);*/
2774 +static void yaffs_read_inode(struct inode *inode)
2776 + /* NB This is called as a side effect of other functions, but
2777 + * we had to release the lock to prevent deadlocks, so
2778 + * need to lock again.
2781 + yaffs_Object *obj;
2782 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2785 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2787 + yaffs_GrossLock(dev);
2789 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2791 + yaffs_FillInodeFromObject(inode, obj);
2793 + yaffs_GrossUnlock(dev);
2796 +static LIST_HEAD(yaffs_dev_list);
2798 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2800 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2802 + if( *flags & MS_RDONLY ) {
2803 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2806 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2808 + yaffs_GrossLock(dev);
2810 + yaffs_FlushEntireDeviceCache(dev);
2812 + yaffs_CheckpointSave(dev);
2817 + yaffs_GrossUnlock(dev);
2821 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2827 +static void yaffs_put_super(struct super_block *sb)
2829 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2831 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2833 + yaffs_GrossLock(dev);
2835 + yaffs_FlushEntireDeviceCache(dev);
2837 + yaffs_CheckpointSave(dev);
2839 + if (dev->putSuperFunc) {
2840 + dev->putSuperFunc(sb);
2843 + yaffs_Deinitialise(dev);
2845 + yaffs_GrossUnlock(dev);
2847 + /* we assume this is protected by lock_kernel() in mount/umount */
2848 + list_del(&dev->devList);
2850 + if(dev->spareBuffer){
2851 + YFREE(dev->spareBuffer);
2852 + dev->spareBuffer = NULL;
2859 +static void yaffs_MTDPutSuper(struct super_block *sb)
2862 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2868 + put_mtd_device(mtd);
2872 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2874 + struct super_block *sb = (struct super_block *)vsb;
2876 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2883 + int skip_checkpoint_read;
2884 + int skip_checkpoint_write;
2888 +#define MAX_OPT_LEN 20
2889 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2891 + char cur_opt[MAX_OPT_LEN+1];
2895 + /* Parse through the options which is a comma seperated list */
2897 + while(options_str && *options_str && !error){
2898 + memset(cur_opt,0,MAX_OPT_LEN+1);
2901 + while(*options_str && *options_str != ','){
2902 + if(p < MAX_OPT_LEN){
2903 + cur_opt[p] = *options_str;
2909 + if(!strcmp(cur_opt,"inband-tags"))
2910 + options->inband_tags = 1;
2911 + else if(!strcmp(cur_opt,"no-cache"))
2912 + options->no_cache = 1;
2913 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2914 + options->skip_checkpoint_read = 1;
2915 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2916 + options->skip_checkpoint_write = 1;
2917 + else if(!strcmp(cur_opt,"no-checkpoint")){
2918 + options->skip_checkpoint_read = 1;
2919 + options->skip_checkpoint_write = 1;
2921 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2930 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2931 + struct super_block *sb,
2932 + void *data, int silent)
2935 + struct inode *inode = NULL;
2936 + struct dentry *root;
2937 + yaffs_Device *dev = 0;
2938 + char devname_buf[BDEVNAME_SIZE + 1];
2939 + struct mtd_info *mtd;
2941 + char *data_str = (char *)data;
2943 + yaffs_options options;
2945 + sb->s_magic = YAFFS_MAGIC;
2946 + sb->s_op = &yaffs_super_ops;
2949 + printk(KERN_INFO "yaffs: sb is NULL\n");
2950 + else if (!sb->s_dev)
2951 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2952 + else if (!yaffs_devname(sb, devname_buf))
2953 + printk(KERN_INFO "yaffs: devname is NULL\n");
2955 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2957 + yaffs_devname(sb, devname_buf));
2962 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2964 + memset(&options,0,sizeof(options));
2966 + if(yaffs_parse_options(&options,data_str)){
2967 + /* Option parsing failed */
2972 + sb->s_blocksize = PAGE_CACHE_SIZE;
2973 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2974 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2976 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2978 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2980 + ("yaffs: Write verification disabled. All guarantees "
2981 + "null and void\n"));
2984 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2986 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
2987 + yaffs_devname(sb, devname_buf)));
2989 + /* Check it's an mtd device..... */
2990 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
2991 + return NULL; /* This isn't an mtd device */
2993 + /* Get the device */
2994 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2996 + T(YAFFS_TRACE_ALWAYS,
2997 + ("yaffs: MTD device #%u doesn't appear to exist\n",
2998 + MINOR(sb->s_dev)));
3001 + /* Check it's NAND */
3002 + if (mtd->type != MTD_NANDFLASH) {
3003 + T(YAFFS_TRACE_ALWAYS,
3004 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3008 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3009 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3010 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3011 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3012 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3013 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3014 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3015 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3016 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3017 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3018 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3020 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3022 + if (yaffsVersion == 1 &&
3023 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3024 + mtd->writesize >= 2048) {
3026 + mtd->oobblock >= 2048) {
3028 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3032 + /* Added NCB 26/5/2006 for completeness */
3033 + if (yaffsVersion == 2 &&
3034 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3035 + mtd->writesize == 512) {
3037 + mtd->oobblock == 512) {
3039 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3045 + if (yaffsVersion == 2) {
3046 + /* Check for version 2 style functions */
3047 + if (!mtd->erase ||
3048 + !mtd->block_isbad ||
3049 + !mtd->block_markbad ||
3052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3053 + !mtd->read_oob || !mtd->write_oob) {
3055 + !mtd->write_ecc ||
3056 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3058 + T(YAFFS_TRACE_ALWAYS,
3059 + ("yaffs: MTD device does not support required "
3064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3065 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3067 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3069 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3070 + T(YAFFS_TRACE_ALWAYS,
3071 + ("yaffs: MTD device does not have the "
3072 + "right page sizes\n"));
3076 + /* Check for V1 style functions */
3077 + if (!mtd->erase ||
3080 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3081 + !mtd->read_oob || !mtd->write_oob) {
3083 + !mtd->write_ecc ||
3084 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3086 + T(YAFFS_TRACE_ALWAYS,
3087 + ("yaffs: MTD device does not support required "
3092 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3093 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3094 + T(YAFFS_TRACE_ALWAYS,
3095 + ("yaffs: MTD device does not support have the "
3096 + "right page sizes\n"));
3101 + /* OK, so if we got here, we have an MTD that's NAND and looks
3102 + * like it has the right capabilities
3103 + * Set the yaffs_Device up for mtd
3106 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3107 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3109 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3112 + /* Deep shit could not allocate device structure */
3113 + T(YAFFS_TRACE_ALWAYS,
3114 + ("yaffs_read_super: Failed trying to allocate "
3115 + "yaffs_Device. \n"));
3119 + memset(dev, 0, sizeof(yaffs_Device));
3120 + dev->genericDevice = mtd;
3121 + dev->name = mtd->name;
3123 + /* Set up the memory size parameters.... */
3125 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3126 + dev->startBlock = 0;
3127 + dev->endBlock = nBlocks - 1;
3128 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3129 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3130 + dev->nReservedBlocks = 5;
3131 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3133 + /* ... and the functions. */
3134 + if (yaffsVersion == 2) {
3135 + dev->writeChunkWithTagsToNAND =
3136 + nandmtd2_WriteChunkWithTagsToNAND;
3137 + dev->readChunkWithTagsFromNAND =
3138 + nandmtd2_ReadChunkWithTagsFromNAND;
3139 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3140 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3141 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3142 + dev->isYaffs2 = 1;
3143 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3144 + dev->nDataBytesPerChunk = mtd->writesize;
3145 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3147 + dev->nDataBytesPerChunk = mtd->oobblock;
3148 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3150 + nBlocks = mtd->size / mtd->erasesize;
3152 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3153 + dev->startBlock = 0;
3154 + dev->endBlock = nBlocks - 1;
3156 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3157 + /* use the MTD interface in yaffs_mtdif1.c */
3158 + dev->writeChunkWithTagsToNAND =
3159 + nandmtd1_WriteChunkWithTagsToNAND;
3160 + dev->readChunkWithTagsFromNAND =
3161 + nandmtd1_ReadChunkWithTagsFromNAND;
3162 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3163 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3165 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3166 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3168 + dev->isYaffs2 = 0;
3170 + /* ... and common functions */
3171 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3172 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3174 + dev->putSuperFunc = yaffs_MTDPutSuper;
3176 + dev->superBlock = (void *)sb;
3177 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3180 +#ifndef CONFIG_YAFFS_DOES_ECC
3181 + dev->useNANDECC = 1;
3184 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3185 + dev->wideTnodesDisabled = 1;
3188 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3189 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3191 + /* we assume this is protected by lock_kernel() in mount/umount */
3192 + list_add_tail(&dev->devList, &yaffs_dev_list);
3194 + init_MUTEX(&dev->grossLock);
3196 + yaffs_GrossLock(dev);
3198 + err = yaffs_GutsInitialise(dev);
3201 + ("yaffs_read_super: guts initialised %s\n",
3202 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3204 + /* Release lock before yaffs_get_inode() */
3205 + yaffs_GrossUnlock(dev);
3207 + /* Create root inode */
3208 + if (err == YAFFS_OK)
3209 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3215 + inode->i_op = &yaffs_dir_inode_operations;
3216 + inode->i_fop = &yaffs_dir_operations;
3218 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3220 + root = d_alloc_root(inode);
3222 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3228 + sb->s_root = root;
3230 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3235 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3236 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3239 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3242 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3243 +static int yaffs_read_super(struct file_system_type *fs,
3244 + int flags, const char *dev_name,
3245 + void *data, struct vfsmount *mnt)
3248 + return get_sb_bdev(fs, flags, dev_name, data,
3249 + yaffs_internal_read_super_mtd, mnt);
3252 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3253 + int flags, const char *dev_name,
3257 + return get_sb_bdev(fs, flags, dev_name, data,
3258 + yaffs_internal_read_super_mtd);
3262 +static struct file_system_type yaffs_fs_type = {
3263 + .owner = THIS_MODULE,
3265 + .get_sb = yaffs_read_super,
3266 + .kill_sb = kill_block_super,
3267 + .fs_flags = FS_REQUIRES_DEV,
3270 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3273 + return yaffs_internal_read_super(1, sb, data, silent);
3276 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3281 +#ifdef CONFIG_YAFFS_YAFFS2
3283 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3284 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3287 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3290 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3291 +static int yaffs2_read_super(struct file_system_type *fs,
3292 + int flags, const char *dev_name, void *data,
3293 + struct vfsmount *mnt)
3295 + return get_sb_bdev(fs, flags, dev_name, data,
3296 + yaffs2_internal_read_super_mtd, mnt);
3299 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3300 + int flags, const char *dev_name,
3304 + return get_sb_bdev(fs, flags, dev_name, data,
3305 + yaffs2_internal_read_super_mtd);
3309 +static struct file_system_type yaffs2_fs_type = {
3310 + .owner = THIS_MODULE,
3312 + .get_sb = yaffs2_read_super,
3313 + .kill_sb = kill_block_super,
3314 + .fs_flags = FS_REQUIRES_DEV,
3317 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3318 + void *data, int silent)
3320 + return yaffs_internal_read_super(2, sb, data, silent);
3323 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3327 +#endif /* CONFIG_YAFFS_YAFFS2 */
3329 +static struct proc_dir_entry *my_proc_entry;
3331 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3333 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3334 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3335 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3336 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3337 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3338 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3339 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3340 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3341 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3342 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3343 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3344 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3345 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3346 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3347 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3348 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3349 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3350 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3352 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3354 + sprintf(buf, "passiveGCs......... %d\n",
3355 + dev->passiveGarbageCollections);
3356 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3357 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3358 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3359 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3360 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3361 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3362 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3363 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3364 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3365 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3367 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3368 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3369 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3374 +static int yaffs_proc_read(char *page,
3376 + off_t offset, int count, int *eof, void *data)
3378 + struct list_head *item;
3380 + int step = offset;
3383 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3384 + * We use 'offset' (*ppos) to indicate where we are in devList.
3385 + * This also assumes the user has posted a read buffer large
3386 + * enough to hold the complete output; but that's life in /proc.
3389 + *(int *)start = 1;
3391 + /* Print header first */
3393 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3394 + "\n%s\n%s\n", yaffs_fs_c_version,
3395 + yaffs_guts_c_version);
3398 + /* hold lock_kernel while traversing yaffs_dev_list */
3401 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3402 + list_for_each(item, &yaffs_dev_list) {
3403 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3408 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3409 + buf = yaffs_dump_dev(buf, dev);
3414 + return buf - page < count ? buf - page : count;
3418 + * Set the verbosity of the warnings and error messages.
3420 + * Note that the names can only be a..z or _ with the current code.
3425 + unsigned mask_bitfield;
3427 + {"allocate", YAFFS_TRACE_ALLOCATE},
3428 + {"always", YAFFS_TRACE_ALWAYS},
3429 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3430 + {"buffers", YAFFS_TRACE_BUFFERS},
3431 + {"bug", YAFFS_TRACE_BUG},
3432 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3433 + {"deletion", YAFFS_TRACE_DELETION},
3434 + {"erase", YAFFS_TRACE_ERASE},
3435 + {"error", YAFFS_TRACE_ERROR},
3436 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3437 + {"gc", YAFFS_TRACE_GC},
3438 + {"mtd", YAFFS_TRACE_MTD},
3439 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3440 + {"os", YAFFS_TRACE_OS},
3441 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3442 + {"scan", YAFFS_TRACE_SCAN},
3443 + {"tracing", YAFFS_TRACE_TRACING},
3445 + {"verify", YAFFS_TRACE_VERIFY},
3446 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3447 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3448 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3450 + {"write", YAFFS_TRACE_WRITE},
3451 + {"all", 0xffffffff},
3456 +#define MAX_MASK_NAME_LENGTH 40
3457 +static int yaffs_proc_write(struct file *file, const char *buf,
3458 + unsigned long count, void *data)
3460 + unsigned rg = 0, mask_bitfield;
3464 + char substring[MAX_MASK_NAME_LENGTH+1];
3470 + rg = yaffs_traceMask;
3472 + while (!done && (pos < count)) {
3474 + while ((pos < count) && isspace(buf[pos])) {
3478 + switch (buf[pos]) {
3492 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3493 + if (end > buf + pos) {
3494 + mask_name = "numeral";
3495 + len = end - (buf + pos);
3498 + for(x = buf + pos, i = 0;
3499 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3500 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3501 + substring[i] = *x;
3502 + substring[i] = '\0';
3504 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3505 + //len = strlen(mask_flags[i].mask_name);
3506 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3507 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3508 + mask_name = mask_flags[i].mask_name;
3509 + mask_bitfield = mask_flags[i].mask_bitfield;
3516 + if (mask_name != NULL) {
3521 + rg &= ~mask_bitfield;
3524 + rg |= mask_bitfield;
3527 + rg = mask_bitfield;
3530 + rg |= mask_bitfield;
3536 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3538 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3540 + if (rg & YAFFS_TRACE_ALWAYS) {
3541 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3543 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3544 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3551 +/* Stuff to handle installation of file systems */
3552 +struct file_system_to_install {
3553 + struct file_system_type *fst;
3557 +static struct file_system_to_install fs_to_install[] = {
3558 +//#ifdef CONFIG_YAFFS_YAFFS1
3559 + {&yaffs_fs_type, 0},
3561 +//#ifdef CONFIG_YAFFS_YAFFS2
3562 + {&yaffs2_fs_type, 0},
3567 +static int __init init_yaffs_fs(void)
3570 + struct file_system_to_install *fsinst;
3572 + T(YAFFS_TRACE_ALWAYS,
3573 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3575 + /* Install the proc_fs entry */
3576 + my_proc_entry = create_proc_entry("yaffs",
3577 + S_IRUGO | S_IFREG,
3580 + if (my_proc_entry) {
3581 + my_proc_entry->write_proc = yaffs_proc_write;
3582 + my_proc_entry->read_proc = yaffs_proc_read;
3583 + my_proc_entry->data = NULL;
3588 + /* Now add the file system entries */
3590 + fsinst = fs_to_install;
3592 + while (fsinst->fst && !error) {
3593 + error = register_filesystem(fsinst->fst);
3595 + fsinst->installed = 1;
3600 + /* Any errors? uninstall */
3602 + fsinst = fs_to_install;
3604 + while (fsinst->fst) {
3605 + if (fsinst->installed) {
3606 + unregister_filesystem(fsinst->fst);
3607 + fsinst->installed = 0;
3616 +static void __exit exit_yaffs_fs(void)
3619 + struct file_system_to_install *fsinst;
3621 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3622 + " removing. \n"));
3624 + remove_proc_entry("yaffs", &proc_root);
3626 + fsinst = fs_to_install;
3628 + while (fsinst->fst) {
3629 + if (fsinst->installed) {
3630 + unregister_filesystem(fsinst->fst);
3631 + fsinst->installed = 0;
3638 +module_init(init_yaffs_fs)
3639 +module_exit(exit_yaffs_fs)
3641 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3642 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3643 +MODULE_LICENSE("GPL");
3644 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.c linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c
3645 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3646 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c 2007-05-30 13:17:16.000000000 +0200
3649 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3651 + * Copyright (C) 2002-2007 Aleph One Ltd.
3652 + * for Toby Churchill Ltd and Brightstar Engineering
3654 + * Created by Charles Manning <charles@aleph1.co.uk>
3656 + * This program is free software; you can redistribute it and/or modify
3657 + * it under the terms of the GNU General Public License version 2 as
3658 + * published by the Free Software Foundation.
3661 +const char *yaffs_guts_c_version =
3662 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3664 +#include "yportenv.h"
3666 +#include "yaffsinterface.h"
3667 +#include "yaffs_guts.h"
3668 +#include "yaffs_tagsvalidity.h"
3670 +#include "yaffs_tagscompat.h"
3671 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3672 +#include "yaffs_qsort.h"
3674 +#include "yaffs_nand.h"
3676 +#include "yaffs_checkptrw.h"
3678 +#include "yaffs_nand.h"
3679 +#include "yaffs_packedtags2.h"
3682 +#ifdef CONFIG_YAFFS_WINCE
3683 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3684 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3687 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3689 +#include "yaffs_ecc.h"
3692 +/* Robustification (if it ever comes about...) */
3693 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3694 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3695 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3696 + const __u8 * data,
3697 + const yaffs_ExtendedTags * tags);
3698 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3699 + const yaffs_ExtendedTags * tags);
3701 +/* Other local prototypes */
3702 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3703 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3705 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3707 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3708 + const __u8 * buffer,
3709 + yaffs_ExtendedTags * tags,
3711 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3712 + int chunkInNAND, int inScan);
3714 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3715 + yaffs_ObjectType type);
3716 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3717 + yaffs_Object * obj);
3718 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3719 + int force, int isShrink, int shadows);
3720 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3721 +static int yaffs_CheckStructures(void);
3722 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3723 + int chunkOffset, int *limit);
3724 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3726 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3728 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3729 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3732 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3735 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3736 +static void yaffs_DestroyObject(yaffs_Object * obj);
3738 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3739 + int chunkInObject);
3741 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3743 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3745 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3747 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3749 +#ifdef YAFFS_PARANOID
3750 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3752 +#define yaffs_CheckFileSanity(in)
3755 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3756 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3758 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3760 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3761 + yaffs_ExtendedTags * tags);
3763 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
3764 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
3765 + yaffs_FileStructure * fStruct,
3769 +/* Function to calculate chunk and offset */
3771 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3773 + if(dev->chunkShift){
3774 + /* Easy-peasy power of 2 case */
3775 + *chunk = (__u32)(addr >> dev->chunkShift);
3776 + *offset = (__u32)(addr & dev->chunkMask);
3778 + else if(dev->crumbsPerChunk)
3780 + /* Case where we're using "crumbs" */
3781 + *offset = (__u32)(addr & dev->crumbMask);
3782 + addr >>= dev->crumbShift;
3783 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3784 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3790 +/* Function to return the number of shifts for a power of 2 greater than or equal
3791 + * to the given number
3792 + * Note we don't try to cater for all possible numbers and this does not have to
3793 + * be hellishly efficient.
3796 +static __u32 ShiftsGE(__u32 x)
3801 + nShifts = extraBits = 0;
3804 + if(x & 1) extraBits++;
3815 +/* Function to return the number of shifts to get a 1 in bit 0
3818 +static __u32 ShiftDiv(__u32 x)
3837 + * Temporary buffer manipulations.
3840 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3843 + __u8 *buf = (__u8 *)1;
3845 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3847 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
3848 + dev->tempBuffer[i].line = 0; /* not in use */
3849 + dev->tempBuffer[i].buffer = buf =
3850 + YMALLOC_DMA(dev->nDataBytesPerChunk);
3853 + return buf ? YAFFS_OK : YAFFS_FAIL;
3857 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3860 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3861 + if (dev->tempBuffer[i].line == 0) {
3862 + dev->tempBuffer[i].line = lineNo;
3863 + if ((i + 1) > dev->maxTemp) {
3864 + dev->maxTemp = i + 1;
3865 + for (j = 0; j <= i; j++)
3866 + dev->tempBuffer[j].maxLine =
3867 + dev->tempBuffer[j].line;
3870 + return dev->tempBuffer[i].buffer;
3874 + T(YAFFS_TRACE_BUFFERS,
3875 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3877 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3878 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3880 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3883 + * If we got here then we have to allocate an unmanaged one
3884 + * This is not good.
3887 + dev->unmanagedTempAllocations++;
3888 + return YMALLOC(dev->nDataBytesPerChunk);
3892 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3896 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3897 + if (dev->tempBuffer[i].buffer == buffer) {
3898 + dev->tempBuffer[i].line = 0;
3904 + /* assume it is an unmanaged one. */
3905 + T(YAFFS_TRACE_BUFFERS,
3906 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3909 + dev->unmanagedTempDeallocations++;
3915 + * Determine if we have a managed buffer.
3917 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3920 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3921 + if (dev->tempBuffer[i].buffer == buffer)
3926 + for (i = 0; i < dev->nShortOpCaches; i++) {
3927 + if( dev->srCache[i].data == buffer )
3932 + if (buffer == dev->checkpointBuffer)
3935 + T(YAFFS_TRACE_ALWAYS,
3936 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3943 + * Chunk bitmap manipulations
3946 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3948 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3949 + T(YAFFS_TRACE_ERROR,
3950 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3954 + return dev->chunkBits +
3955 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3958 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3960 + if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
3961 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
3962 + T(YAFFS_TRACE_ERROR,
3963 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
3968 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3970 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3972 + memset(blkBits, 0, dev->chunkBitmapStride);
3975 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3977 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3979 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3981 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
3984 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
3986 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3988 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3990 + blkBits[chunk / 8] |= (1 << (chunk & 7));
3993 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
3995 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3996 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3998 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4001 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4003 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4005 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4013 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4015 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4018 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4019 + __u8 x = *blkBits;
4032 + * Verification code
4035 +static int yaffs_SkipVerification(yaffs_Device *dev)
4037 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4040 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4042 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4045 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4047 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4050 +static const char * blockStateName[] = {
4063 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4068 + if(yaffs_SkipVerification(dev))
4071 + /* Report illegal runtime states */
4072 + if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4073 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
4075 + switch(bi->blockState){
4076 + case YAFFS_BLOCK_STATE_UNKNOWN:
4077 + case YAFFS_BLOCK_STATE_SCANNING:
4078 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4079 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
4080 + n,blockStateName[bi->blockState]));
4083 + /* Check pages in use and soft deletions are legal */
4085 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4087 + if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4088 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4089 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4090 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4091 + n,bi->pagesInUse,bi->softDeletions));
4094 + /* Check chunk bitmap legal */
4095 + inUse = yaffs_CountChunkBits(dev,n);
4096 + if(inUse != bi->pagesInUse)
4097 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4098 + n,bi->pagesInUse,inUse));
4100 + /* Check that the sequence number is valid.
4101 + * Ten million is legal, but is very unlikely
4103 + if(dev->isYaffs2 &&
4104 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4105 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
4106 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4107 + n,bi->sequenceNumber));
4111 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4113 + yaffs_VerifyBlock(dev,bi,n);
4115 + /* After collection the block should be in the erased state */
4116 + /* TODO: This will need to change if we do partial gc */
4118 + if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
4119 + T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4120 + n,bi->blockState));
4124 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4127 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4128 + int nIllegalBlockStates = 0;
4131 + if(yaffs_SkipVerification(dev))
4134 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4137 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4138 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4139 + yaffs_VerifyBlock(dev,bi,i);
4141 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4142 + nBlocksPerState[bi->blockState]++;
4144 + nIllegalBlockStates++;
4148 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4149 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4151 + T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
4152 + if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4153 + T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
4155 + for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4156 + T(YAFFS_TRACE_VERIFY,
4157 + (TSTR("%s %d blocks"TENDSTR),
4158 + blockStateName[i],nBlocksPerState[i]));
4160 + if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4161 + T(YAFFS_TRACE_VERIFY,
4162 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4163 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4165 + if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4166 + T(YAFFS_TRACE_VERIFY,
4167 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4168 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4170 + if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4171 + T(YAFFS_TRACE_VERIFY,
4172 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4173 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4175 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4180 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4181 + * case those tests will not be performed.
4183 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4185 + if(yaffs_SkipVerification(obj->myDev))
4188 + if(!(tags && obj && oh)){
4189 + T(YAFFS_TRACE_VERIFY,
4190 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4191 + (__u32)tags,(__u32)obj,(__u32)oh));
4195 + if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4196 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4197 + T(YAFFS_TRACE_VERIFY,
4198 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4199 + tags->objectId, oh->type));
4201 + if(tags->objectId != obj->objectId)
4202 + T(YAFFS_TRACE_VERIFY,
4203 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4204 + tags->objectId, obj->objectId));
4208 + * Check that the object's parent ids match if parentCheck requested.
4210 + * Tests do not apply to the root object.
4213 + if(parentCheck && tags->objectId > 1 && !obj->parent)
4214 + T(YAFFS_TRACE_VERIFY,
4215 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4216 + tags->objectId, oh->parentObjectId));
4219 + if(parentCheck && obj->parent &&
4220 + oh->parentObjectId != obj->parent->objectId &&
4221 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4222 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4223 + T(YAFFS_TRACE_VERIFY,
4224 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4225 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4228 + if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4229 + T(YAFFS_TRACE_VERIFY,
4230 + (TSTR("Obj %d header name is NULL"TENDSTR),
4233 + if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4234 + T(YAFFS_TRACE_VERIFY,
4235 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4241 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4242 + __u32 level, int chunkOffset)
4245 + yaffs_Device *dev = obj->myDev;
4247 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4252 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4253 + if (tn->internal[i]) {
4254 + ok = yaffs_VerifyTnodeWorker(obj,
4257 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4260 + } else if (level == 0) {
4262 + yaffs_ExtendedTags tags;
4263 + __u32 objectId = obj->objectId;
4265 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4267 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4268 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4271 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4272 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk