1 diff -urN linux-2.6.21.1/fs/Kconfig linux-2.6.21.1.new/fs/Kconfig
2 --- linux-2.6.21.1/fs/Kconfig 2007-06-08 14:07:09.000000000 +0200
3 +++ linux-2.6.21.1.new/fs/Kconfig 2007-06-08 14:09:26.000000000 +0200
6 source "fs/xfs/Kconfig"
7 source "fs/gfs2/Kconfig"
8 +source "fs/yaffs2/Kconfig"
11 tristate "OCFS2 file system support"
12 diff -urN linux-2.6.21.1/fs/Makefile linux-2.6.21.1.new/fs/Makefile
13 --- linux-2.6.21.1/fs/Makefile 2007-06-08 14:07:09.000000000 +0200
14 +++ linux-2.6.21.1.new/fs/Makefile 2007-06-08 14:09:51.000000000 +0200
16 obj-$(CONFIG_DEBUG_FS) += debugfs/
17 obj-$(CONFIG_OCFS2_FS) += ocfs2/
18 obj-$(CONFIG_GFS2_FS) += gfs2/
19 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
20 diff -urN linux-2.6.21.1/fs/yaffs2/Kconfig linux-2.6.21.1.new/fs/yaffs2/Kconfig
21 --- linux-2.6.21.1/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
22 +++ linux-2.6.21.1.new/fs/yaffs2/Kconfig 2007-06-08 14:07:26.000000000 +0200
25 +# YAFFS file system configurations
29 + tristate "YAFFS2 file system support"
35 + YAFFS2, or Yet Another Flash Filing System, is a filing system
36 + optimised for NAND Flash chips.
38 + To compile the YAFFS2 file system support as a module, choose M
39 + here: the module will be called yaffs2.
43 + Further information on YAFFS2 is available at
44 + <http://www.aleph1.co.uk/yaffs/>.
47 + bool "512 byte / page devices"
51 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
53 + Not needed for 2K-page devices.
57 +config YAFFS_9BYTE_TAGS
58 + bool "Use older-style on-NAND data format with pageStatus byte"
59 + depends on YAFFS_YAFFS1
63 + Older-style on-NAND data format has a "pageStatus" byte to record
64 + chunk/page state. This byte is zero when the page is discarded.
65 + Choose this option if you have existing on-NAND data using this
66 + format that you need to continue to support. New data written
67 + also uses the older-style format. Note: Use of this option
68 + generally requires that MTD's oob layout be adjusted to use the
69 + older-style format. See notes on tags formats and MTD versions.
73 +config YAFFS_DOES_ECC
74 + bool "Lets Yaffs do its own ECC"
75 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
78 + This enables Yaffs to use its own ECC functions instead of using
79 + the ones from the generic MTD-NAND driver.
83 +config YAFFS_ECC_WRONG_ORDER
84 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
85 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
88 + This makes yaffs_ecc.c use the same ecc byte order as Steven
89 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
90 + order as SmartMedia.
95 + bool "2048 byte (or larger) / page devices"
99 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
103 +config YAFFS_AUTO_YAFFS2
104 + bool "Autoselect yaffs2 format"
105 + depends on YAFFS_YAFFS2
108 + Without this, you need to explicitely use yaffs2 as the file
109 + system type. With this, you can say "yaffs" and yaffs or yaffs2
110 + will be used depending on the device page size (yaffs on
111 + 512-byte page devices, yaffs2 on 2K page devices).
115 +config YAFFS_DISABLE_LAZY_LOAD
116 + bool "Disable lazy loading"
117 + depends on YAFFS_YAFFS2
120 + "Lazy loading" defers loading file details until they are
121 + required. This saves mount time, but makes the first look-up
124 + Lazy loading will only happen if enabled by this option being 'n'
125 + and if the appropriate tags are available, else yaffs2 will
126 + automatically fall back to immediate loading and do the right
129 + Lazy laoding will be required by checkpointing.
131 + Setting this to 'y' will disable lazy loading.
135 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
136 + int "Reserved blocks for checkpointing"
137 + depends on YAFFS_YAFFS2
140 + Give the number of Blocks to reserve for checkpointing.
141 + Checkpointing saves the state at unmount so that mounting is
142 + much faster as a scan of all the flash to regenerate this state
143 + is not needed. These Blocks are reserved per partition, so if
144 + you have very small partitions the default (10) may be a mess
145 + for you. You can set this value to 0, but that does not mean
146 + checkpointing is disabled at all. There only won't be any
147 + specially reserved blocks for checkpointing, so if there is
148 + enough free space on the filesystem, it will be used for
151 + If unsure, leave at default (10), but don't wonder if there are
152 + always 2MB used on your large page device partition (10 x 2k
153 + pagesize). When using small partitions or when being very small
154 + on space, you probably want to set this to zero.
156 +config YAFFS_DISABLE_WIDE_TNODES
157 + bool "Turn off wide tnodes"
158 + depends on YAFFS_FS
161 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
162 + page devices and >=128MB for 2k page devices. They use slightly
163 + more RAM but are faster since they eliminate chunk group
166 + Setting this to 'y' will force tnode width to 16 bits and save
167 + memory but make large arrays slower.
171 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
172 + bool "Force chunk erase check"
173 + depends on YAFFS_FS
176 + Normally YAFFS only checks chunks before writing until an erased
177 + chunk is found. This helps to detect any partially written
178 + chunks that might have happened due to power loss.
180 + Enabling this forces on the test that chunks are erased in flash
181 + before writing to them. This takes more time but is potentially
184 + Suggest setting Y during development and ironing out driver
185 + issues etc. Suggest setting to N if you want faster writing.
189 +config YAFFS_SHORT_NAMES_IN_RAM
190 + bool "Cache short names in RAM"
191 + depends on YAFFS_FS
194 + If this config is set, then short names are stored with the
195 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
196 + but makes look-ups faster.
199 diff -urN linux-2.6.21.1/fs/yaffs2/Makefile linux-2.6.21.1.new/fs/yaffs2/Makefile
200 --- linux-2.6.21.1/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
201 +++ linux-2.6.21.1.new/fs/yaffs2/Makefile 2007-06-08 14:07:26.000000000 +0200
204 +# Makefile for the linux YAFFS filesystem routines.
207 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
209 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
210 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
211 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
212 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
213 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
214 diff -urN linux-2.6.21.1/fs/yaffs2/devextras.h linux-2.6.21.1.new/fs/yaffs2/devextras.h
215 --- linux-2.6.21.1/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
216 +++ linux-2.6.21.1.new/fs/yaffs2/devextras.h 2007-06-08 14:07:26.000000000 +0200
219 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
221 + * Copyright (C) 2002-2007 Aleph One Ltd.
222 + * for Toby Churchill Ltd and Brightstar Engineering
224 + * Created by Charles Manning <charles@aleph1.co.uk>
226 + * This program is free software; you can redistribute it and/or modify
227 + * it under the terms of the GNU Lesser General Public License version 2.1 as
228 + * published by the Free Software Foundation.
230 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
234 + * This file is just holds extra declarations used during development.
235 + * Most of these are from kernel includes placed here so we can use them in
240 +#ifndef __EXTRAS_H__
241 +#define __EXTRAS_H__
244 +#define __inline__ __inline
248 +#if !(defined __KERNEL__) || (defined WIN32)
250 +/* User space defines */
252 +typedef unsigned char __u8;
253 +typedef unsigned short __u16;
254 +typedef unsigned __u32;
257 + * Simple doubly linked list implementation.
259 + * Some of the internal functions ("__xxx") are useful when
260 + * manipulating whole lists rather than single entries, as
261 + * sometimes we already know the next/prev entries and we can
262 + * generate better code by using them directly rather than
263 + * using the generic single-entry routines.
266 +#define prefetch(x) 1
269 + struct list_head *next, *prev;
272 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
274 +#define LIST_HEAD(name) \
275 + struct list_head name = LIST_HEAD_INIT(name)
277 +#define INIT_LIST_HEAD(ptr) do { \
278 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
282 + * Insert a new entry between two known consecutive entries.
284 + * This is only for internal list manipulation where we know
285 + * the prev/next entries already!
287 +static __inline__ void __list_add(struct list_head *new,
288 + struct list_head *prev,
289 + struct list_head *next)
298 + * list_add - add a new entry
299 + * @new: new entry to be added
300 + * @head: list head to add it after
302 + * Insert a new entry after the specified head.
303 + * This is good for implementing stacks.
305 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
307 + __list_add(new, head, head->next);
311 + * list_add_tail - add a new entry
312 + * @new: new entry to be added
313 + * @head: list head to add it before
315 + * Insert a new entry before the specified head.
316 + * This is useful for implementing queues.
318 +static __inline__ void list_add_tail(struct list_head *new,
319 + struct list_head *head)
321 + __list_add(new, head->prev, head);
325 + * Delete a list entry by making the prev/next entries
326 + * point to each other.
328 + * This is only for internal list manipulation where we know
329 + * the prev/next entries already!
331 +static __inline__ void __list_del(struct list_head *prev,
332 + struct list_head *next)
339 + * list_del - deletes entry from list.
340 + * @entry: the element to delete from the list.
341 + * Note: list_empty on entry does not return true after this, the entry is
342 + * in an undefined state.
344 +static __inline__ void list_del(struct list_head *entry)
346 + __list_del(entry->prev, entry->next);
350 + * list_del_init - deletes entry from list and reinitialize it.
351 + * @entry: the element to delete from the list.
353 +static __inline__ void list_del_init(struct list_head *entry)
355 + __list_del(entry->prev, entry->next);
356 + INIT_LIST_HEAD(entry);
360 + * list_empty - tests whether a list is empty
361 + * @head: the list to test.
363 +static __inline__ int list_empty(struct list_head *head)
365 + return head->next == head;
369 + * list_splice - join two lists
370 + * @list: the new list to add.
371 + * @head: the place to add it in the first list.
373 +static __inline__ void list_splice(struct list_head *list,
374 + struct list_head *head)
376 + struct list_head *first = list->next;
378 + if (first != list) {
379 + struct list_head *last = list->prev;
380 + struct list_head *at = head->next;
382 + first->prev = head;
383 + head->next = first;
391 + * list_entry - get the struct for this entry
392 + * @ptr: the &struct list_head pointer.
393 + * @type: the type of the struct this is embedded in.
394 + * @member: the name of the list_struct within the struct.
396 +#define list_entry(ptr, type, member) \
397 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
400 + * list_for_each - iterate over a list
401 + * @pos: the &struct list_head to use as a loop counter.
402 + * @head: the head for your list.
404 +#define list_for_each(pos, head) \
405 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
406 + pos = pos->next, prefetch(pos->next))
409 + * list_for_each_safe - iterate over a list safe against removal
411 + * @pos: the &struct list_head to use as a loop counter.
412 + * @n: another &struct list_head to use as temporary storage
413 + * @head: the head for your list.
415 +#define list_for_each_safe(pos, n, head) \
416 + for (pos = (head)->next, n = pos->next; pos != (head); \
417 + pos = n, n = pos->next)
422 +#define DT_UNKNOWN 0
433 +#include <sys/stat.h>
437 + * Attribute flags. These should be or-ed together to figure out what
438 + * has been changed!
444 +#define ATTR_ATIME 16
445 +#define ATTR_MTIME 32
446 +#define ATTR_CTIME 64
447 +#define ATTR_ATIME_SET 128
448 +#define ATTR_MTIME_SET 256
449 +#define ATTR_FORCE 512 /* Not a change, but a change it */
450 +#define ATTR_ATTR_FLAG 1024
453 + unsigned int ia_valid;
461 + unsigned int ia_attr_flags;
469 +#include <linux/types.h>
470 +#include <linux/list.h>
471 +#include <linux/fs.h>
472 +#include <linux/stat.h>
482 diff -urN linux-2.6.21.1/fs/yaffs2/moduleconfig.h linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h
483 --- linux-2.6.21.1/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
484 +++ linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h 2007-06-08 14:07:26.000000000 +0200
487 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
489 + * Copyright (C) 2002-2007 Aleph One Ltd.
490 + * for Toby Churchill Ltd and Brightstar Engineering
492 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
494 + * This program is free software; you can redistribute it and/or modify
495 + * it under the terms of the GNU Lesser General Public License version 2.1 as
496 + * published by the Free Software Foundation.
498 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
501 +#ifndef __YAFFS_CONFIG_H__
502 +#define __YAFFS_CONFIG_H__
504 +#ifdef YAFFS_OUT_OF_TREE
506 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
507 +#define CONFIG_YAFFS_FS
508 +#define CONFIG_YAFFS_YAFFS1
509 +#define CONFIG_YAFFS_YAFFS2
511 +/* These options are independent of each other. Select those that matter. */
513 +/* Default: Not selected */
514 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
515 +//#define CONFIG_YAFFS_DOES_ECC
517 +/* Default: Not selected */
518 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
519 +/* CONFIG_YAFFS_DOES_ECC is set */
520 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
522 +/* Default: Selected */
523 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
524 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
526 +/* Default: Selected */
527 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
528 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
531 +/* Meaning: set the count of blocks to reserve for checkpointing */
532 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
535 +Older-style on-NAND data format has a "pageStatus" byte to record
536 +chunk/page state. This byte is zeroed when the page is discarded.
537 +Choose this option if you have existing on-NAND data in this format
538 +that you need to continue to support. New data written also uses the
540 +Note: Use of this option generally requires that MTD's oob layout be
541 +adjusted to use the older-style format. See notes on tags formats and
544 +/* Default: Not selected */
545 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
546 +#define CONFIG_YAFFS_9BYTE_TAGS
548 +#endif /* YAFFS_OUT_OF_TREE */
550 +#endif /* __YAFFS_CONFIG_H__ */
551 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c
552 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
553 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c 2007-06-08 14:07:26.000000000 +0200
556 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
558 + * Copyright (C) 2002-2007 Aleph One Ltd.
559 + * for Toby Churchill Ltd and Brightstar Engineering
561 + * Created by Charles Manning <charles@aleph1.co.uk>
563 + * This program is free software; you can redistribute it and/or modify
564 + * it under the terms of the GNU General Public License version 2 as
565 + * published by the Free Software Foundation.
568 +const char *yaffs_checkptrw_c_version =
569 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
572 +#include "yaffs_checkptrw.h"
575 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
578 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
580 + T(YAFFS_TRACE_CHECKPOINT,
581 + (TSTR("checkpt blocks available = %d" TENDSTR),
585 + return (blocksAvailable <= 0) ? 0 : 1;
589 +static int yaffs_CheckpointErase(yaffs_Device *dev)
595 + if(!dev->eraseBlockInNAND)
597 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
598 + dev->internalStartBlock,dev->internalEndBlock));
600 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
601 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
602 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
603 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
604 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
605 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
606 + dev->nErasedBlocks++;
607 + dev->nFreeChunks += dev->nChunksPerBlock;
610 + dev->markNANDBlockBad(dev,i);
611 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
616 + dev->blocksInCheckpoint = 0;
622 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
625 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
626 + T(YAFFS_TRACE_CHECKPOINT,
627 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
628 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
630 + if(dev->checkpointNextBlock >= 0 &&
631 + dev->checkpointNextBlock <= dev->internalEndBlock &&
632 + blocksAvailable > 0){
634 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
635 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
636 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
637 + dev->checkpointNextBlock = i + 1;
638 + dev->checkpointCurrentBlock = i;
639 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
644 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
646 + dev->checkpointNextBlock = -1;
647 + dev->checkpointCurrentBlock = -1;
650 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
653 + yaffs_ExtendedTags tags;
655 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
656 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
658 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
659 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
660 + int chunk = i * dev->nChunksPerBlock;
661 + int realignedChunk = chunk - dev->chunkOffset;
663 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
664 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
665 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
667 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
668 + /* Right kind of block */
669 + dev->checkpointNextBlock = tags.objectId;
670 + dev->checkpointCurrentBlock = i;
671 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
672 + dev->blocksInCheckpoint++;
673 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
678 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
680 + dev->checkpointNextBlock = -1;
681 + dev->checkpointCurrentBlock = -1;
685 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
688 + /* Got the functions we need? */
689 + if (!dev->writeChunkWithTagsToNAND ||
690 + !dev->readChunkWithTagsFromNAND ||
691 + !dev->eraseBlockInNAND ||
692 + !dev->markNANDBlockBad)
695 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
698 + if(!dev->checkpointBuffer)
699 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
700 + if(!dev->checkpointBuffer)
704 + dev->checkpointPageSequence = 0;
706 + dev->checkpointOpenForWrite = forWriting;
708 + dev->checkpointByteCount = 0;
709 + dev->checkpointSum = 0;
710 + dev->checkpointXor = 0;
711 + dev->checkpointCurrentBlock = -1;
712 + dev->checkpointCurrentChunk = -1;
713 + dev->checkpointNextBlock = dev->internalStartBlock;
715 + /* Erase all the blocks in the checkpoint area */
717 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
718 + dev->checkpointByteOffset = 0;
719 + return yaffs_CheckpointErase(dev);
724 + /* Set to a value that will kick off a read */
725 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
726 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
727 + * going to be way more than we need */
728 + dev->blocksInCheckpoint = 0;
729 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
730 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
731 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
732 + dev->checkpointBlockList[i] = -1;
738 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
740 + __u32 compositeSum;
741 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
742 + *sum = compositeSum;
746 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
750 + int realignedChunk;
752 + yaffs_ExtendedTags tags;
754 + if(dev->checkpointCurrentBlock < 0){
755 + yaffs_CheckpointFindNextErasedBlock(dev);
756 + dev->checkpointCurrentChunk = 0;
759 + if(dev->checkpointCurrentBlock < 0)
762 + tags.chunkDeleted = 0;
763 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
764 + tags.chunkId = dev->checkpointPageSequence + 1;
765 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
766 + tags.byteCount = dev->nDataBytesPerChunk;
767 + if(dev->checkpointCurrentChunk == 0){
768 + /* First chunk we write for the block? Set block state to
770 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
771 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
772 + dev->blocksInCheckpoint++;
775 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
778 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
779 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
781 + realignedChunk = chunk - dev->chunkOffset;
783 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
784 + dev->checkpointByteOffset = 0;
785 + dev->checkpointPageSequence++;
786 + dev->checkpointCurrentChunk++;
787 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
788 + dev->checkpointCurrentChunk = 0;
789 + dev->checkpointCurrentBlock = -1;
791 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
797 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
803 + __u8 * dataBytes = (__u8 *)data;
807 + if(!dev->checkpointBuffer)
810 + if(!dev->checkpointOpenForWrite)
813 + while(i < nBytes && ok) {
817 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
818 + dev->checkpointSum += *dataBytes;
819 + dev->checkpointXor ^= *dataBytes;
821 + dev->checkpointByteOffset++;
824 + dev->checkpointByteCount++;
827 + if(dev->checkpointByteOffset < 0 ||
828 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
829 + ok = yaffs_CheckpointFlushBuffer(dev);
836 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
840 + yaffs_ExtendedTags tags;
844 + int realignedChunk;
846 + __u8 *dataBytes = (__u8 *)data;
848 + if(!dev->checkpointBuffer)
851 + if(dev->checkpointOpenForWrite)
854 + while(i < nBytes && ok) {
857 + if(dev->checkpointByteOffset < 0 ||
858 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
860 + if(dev->checkpointCurrentBlock < 0){
861 + yaffs_CheckpointFindNextCheckpointBlock(dev);
862 + dev->checkpointCurrentChunk = 0;
865 + if(dev->checkpointCurrentBlock < 0)
869 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
870 + dev->checkpointCurrentChunk;
872 + realignedChunk = chunk - dev->chunkOffset;
874 + /* read in the next chunk */
875 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
876 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
877 + dev->checkpointBuffer,
880 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
881 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
884 + dev->checkpointByteOffset = 0;
885 + dev->checkpointPageSequence++;
886 + dev->checkpointCurrentChunk++;
888 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
889 + dev->checkpointCurrentBlock = -1;
894 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
895 + dev->checkpointSum += *dataBytes;
896 + dev->checkpointXor ^= *dataBytes;
897 + dev->checkpointByteOffset++;
900 + dev->checkpointByteCount++;
907 +int yaffs_CheckpointClose(yaffs_Device *dev)
910 + if(dev->checkpointOpenForWrite){
911 + if(dev->checkpointByteOffset != 0)
912 + yaffs_CheckpointFlushBuffer(dev);
915 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
916 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
917 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
918 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
920 + // Todo this looks odd...
923 + YFREE(dev->checkpointBlockList);
924 + dev->checkpointBlockList = NULL;
927 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
928 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
931 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
932 + dev->checkpointByteCount));
934 + if(dev->checkpointBuffer){
935 + /* free the buffer */
936 + YFREE(dev->checkpointBuffer);
937 + dev->checkpointBuffer = NULL;
945 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
947 + /* Erase the first checksum block */
949 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
951 + if(!yaffs_CheckpointSpaceOk(dev))
954 + return yaffs_CheckpointErase(dev);
959 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h
960 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
961 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h 2007-06-08 14:07:26.000000000 +0200
964 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
966 + * Copyright (C) 2002-2007 Aleph One Ltd.
967 + * for Toby Churchill Ltd and Brightstar Engineering
969 + * Created by Charles Manning <charles@aleph1.co.uk>
971 + * This program is free software; you can redistribute it and/or modify
972 + * it under the terms of the GNU Lesser General Public License version 2.1 as
973 + * published by the Free Software Foundation.
975 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
978 +#ifndef __YAFFS_CHECKPTRW_H__
979 +#define __YAFFS_CHECKPTRW_H__
981 +#include "yaffs_guts.h"
983 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
985 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
987 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
989 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
991 +int yaffs_CheckpointClose(yaffs_Device *dev);
993 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
998 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c
999 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
1000 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c 2007-06-08 14:07:26.000000000 +0200
1003 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1005 + * Copyright (C) 2002-2007 Aleph One Ltd.
1006 + * for Toby Churchill Ltd and Brightstar Engineering
1008 + * Created by Charles Manning <charles@aleph1.co.uk>
1010 + * This program is free software; you can redistribute it and/or modify
1011 + * it under the terms of the GNU General Public License version 2 as
1012 + * published by the Free Software Foundation.
1016 + * This code implements the ECC algorithm used in SmartMedia.
1018 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1019 + * The two unused bit are set to 1.
1020 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1021 + * blocks are used on a 512-byte NAND page.
1025 +/* Table generated by gen-ecc.c
1026 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1027 + * for each byte of data. These are instead provided in a table in bits7..2.
1028 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1029 + * this bytes influence on the line parity.
1032 +const char *yaffs_ecc_c_version =
1033 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1035 +#include "yportenv.h"
1037 +#include "yaffs_ecc.h"
1039 +static const unsigned char column_parity_table[] = {
1040 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1041 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1042 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1043 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1044 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1045 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1046 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1047 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1048 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1049 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1050 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1051 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1052 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1053 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1054 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1055 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1056 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1057 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1058 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1059 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1060 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1061 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1062 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1063 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1064 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1065 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1066 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1067 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1068 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1069 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1070 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1071 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1074 +/* Count the bits in an unsigned char or a U32 */
1076 +static int yaffs_CountBits(unsigned char x)
1087 +static int yaffs_CountBits32(unsigned x)
1098 +/* Calculate the ECC for a 256-byte block of data */
1099 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1103 + unsigned char col_parity = 0;
1104 + unsigned char line_parity = 0;
1105 + unsigned char line_parity_prime = 0;
1109 + for (i = 0; i < 256; i++) {
1110 + b = column_parity_table[*data++];
1113 + if (b & 0x01) // odd number of bits in the byte
1116 + line_parity_prime ^= ~i;
1121 + ecc[2] = (~col_parity) | 0x03;
1124 + if (line_parity & 0x80)
1126 + if (line_parity_prime & 0x80)
1128 + if (line_parity & 0x40)
1130 + if (line_parity_prime & 0x40)
1132 + if (line_parity & 0x20)
1134 + if (line_parity_prime & 0x20)
1136 + if (line_parity & 0x10)
1138 + if (line_parity_prime & 0x10)
1143 + if (line_parity & 0x08)
1145 + if (line_parity_prime & 0x08)
1147 + if (line_parity & 0x04)
1149 + if (line_parity_prime & 0x04)
1151 + if (line_parity & 0x02)
1153 + if (line_parity_prime & 0x02)
1155 + if (line_parity & 0x01)
1157 + if (line_parity_prime & 0x01)
1161 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1162 + // Swap the bytes into the wrong order
1170 +/* Correct the ECC on a 256 byte block of data */
1172 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1173 + const unsigned char *test_ecc)
1175 + unsigned char d0, d1, d2; /* deltas */
1177 + d0 = read_ecc[0] ^ test_ecc[0];
1178 + d1 = read_ecc[1] ^ test_ecc[1];
1179 + d2 = read_ecc[2] ^ test_ecc[2];
1181 + if ((d0 | d1 | d2) == 0)
1182 + return 0; /* no error */
1184 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1185 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1186 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1187 + /* Single bit (recoverable) error in data */
1192 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1193 + // swap the bytes to correct for the wrong order
1227 + data[byte] ^= (1 << bit);
1229 + return 1; /* Corrected the error */
1232 + if ((yaffs_CountBits(d0) +
1233 + yaffs_CountBits(d1) +
1234 + yaffs_CountBits(d2)) == 1) {
1235 + /* Reccoverable error in ecc */
1237 + read_ecc[0] = test_ecc[0];
1238 + read_ecc[1] = test_ecc[1];
1239 + read_ecc[2] = test_ecc[2];
1241 + return 1; /* Corrected the error */
1244 + /* Unrecoverable error */
1252 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1254 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1255 + yaffs_ECCOther * eccOther)
1259 + unsigned char col_parity = 0;
1260 + unsigned line_parity = 0;
1261 + unsigned line_parity_prime = 0;
1264 + for (i = 0; i < nBytes; i++) {
1265 + b = column_parity_table[*data++];
1269 + /* odd number of bits in the byte */
1271 + line_parity_prime ^= ~i;
1276 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1277 + eccOther->lineParity = line_parity;
1278 + eccOther->lineParityPrime = line_parity_prime;
1281 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1282 + yaffs_ECCOther * read_ecc,
1283 + const yaffs_ECCOther * test_ecc)
1285 + unsigned char cDelta; /* column parity delta */
1286 + unsigned lDelta; /* line parity delta */
1287 + unsigned lDeltaPrime; /* line parity delta */
1290 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1291 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1292 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1294 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1295 + return 0; /* no error */
1297 + if (lDelta == ~lDeltaPrime &&
1298 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1300 + /* Single bit (recoverable) error in data */
1304 + if (cDelta & 0x20)
1306 + if (cDelta & 0x08)
1308 + if (cDelta & 0x02)
1311 + if(lDelta >= nBytes)
1314 + data[lDelta] ^= (1 << bit);
1316 + return 1; /* corrected */
1319 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1320 + yaffs_CountBits(cDelta)) == 1) {
1321 + /* Reccoverable error in ecc */
1323 + *read_ecc = *test_ecc;
1324 + return 1; /* corrected */
1327 + /* Unrecoverable error */
1333 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h
1334 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1335 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h 2007-06-08 14:07:26.000000000 +0200
1338 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1340 + * Copyright (C) 2002-2007 Aleph One Ltd.
1341 + * for Toby Churchill Ltd and Brightstar Engineering
1343 + * Created by Charles Manning <charles@aleph1.co.uk>
1345 + * This program is free software; you can redistribute it and/or modify
1346 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1347 + * published by the Free Software Foundation.
1349 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1353 + * This code implements the ECC algorithm used in SmartMedia.
1355 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1356 + * The two unused bit are set to 1.
1357 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1358 + * blocks are used on a 512-byte NAND page.
1362 +#ifndef __YAFFS_ECC_H__
1363 +#define __YAFFS_ECC_H__
1366 + unsigned char colParity;
1367 + unsigned lineParity;
1368 + unsigned lineParityPrime;
1371 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1372 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1373 + const unsigned char *test_ecc);
1375 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1376 + yaffs_ECCOther * ecc);
1377 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1378 + yaffs_ECCOther * read_ecc,
1379 + const yaffs_ECCOther * test_ecc);
1381 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_fs.c linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c
1382 --- linux-2.6.21.1/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1383 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c 2007-06-08 14:07:26.000000000 +0200
1386 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1388 + * Copyright (C) 2002-2007 Aleph One Ltd.
1389 + * for Toby Churchill Ltd and Brightstar Engineering
1391 + * Created by Charles Manning <charles@aleph1.co.uk>
1392 + * Acknowledgements:
1393 + * Luc van OostenRyck for numerous patches.
1394 + * Nick Bane for numerous patches.
1395 + * Nick Bane for 2.5/2.6 integration.
1396 + * Andras Toth for mknod rdev issue.
1397 + * Michael Fischer for finding the problem with inode inconsistency.
1398 + * Some code bodily lifted from JFFS
1400 + * This program is free software; you can redistribute it and/or modify
1401 + * it under the terms of the GNU General Public License version 2 as
1402 + * published by the Free Software Foundation.
1407 + * This is the file system front-end to YAFFS that hooks it up to
1411 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1413 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1415 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1418 +const char *yaffs_fs_c_version =
1419 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1420 +extern const char *yaffs_guts_c_version;
1422 +#include <linux/version.h>
1423 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1424 +#include <linux/config.h>
1426 +#include <linux/kernel.h>
1427 +#include <linux/module.h>
1428 +#include <linux/slab.h>
1429 +#include <linux/init.h>
1430 +#include <linux/list.h>
1431 +#include <linux/fs.h>
1432 +#include <linux/proc_fs.h>
1433 +#include <linux/smp_lock.h>
1434 +#include <linux/pagemap.h>
1435 +#include <linux/mtd/mtd.h>
1436 +#include <linux/interrupt.h>
1437 +#include <linux/string.h>
1438 +#include <linux/ctype.h>
1440 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1442 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1443 +#include <asm/statfs.h>
1444 +#define UnlockPage(p) unlock_page(p)
1445 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1447 +/* FIXME: use sb->s_id instead ? */
1448 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1452 +#include <linux/locks.h>
1453 +#define BDEVNAME_SIZE 0
1454 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1456 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1457 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1463 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1464 +#define WRITE_SIZE_STR "writesize"
1465 +#define WRITE_SIZE(mtd) (mtd)->writesize
1467 +#define WRITE_SIZE_STR "oobblock"
1468 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1471 +#include <asm/uaccess.h>
1473 +#include "yportenv.h"
1474 +#include "yaffs_guts.h"
1476 +#include <linux/mtd/mtd.h>
1477 +#include "yaffs_mtdif.h"
1478 +#include "yaffs_mtdif1.h"
1479 +#include "yaffs_mtdif2.h"
1481 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1482 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1484 +/* Module Parameters */
1485 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1486 +module_param(yaffs_traceMask,uint,0644);
1487 +module_param(yaffs_wr_attempts,uint,0644);
1489 +MODULE_PARM(yaffs_traceMask,"i");
1490 +MODULE_PARM(yaffs_wr_attempts,"i");
1493 +/*#define T(x) printk x */
1495 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1496 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1498 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1501 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1502 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1504 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1505 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1507 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1510 +static void yaffs_put_super(struct super_block *sb);
1512 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1515 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1516 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1518 +static int yaffs_file_flush(struct file *file);
1521 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1524 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1526 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1527 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1528 + struct nameidata *n);
1529 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1530 + struct nameidata *n);
1532 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1533 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1535 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1536 + struct dentry *dentry);
1537 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1538 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1539 + const char *symname);
1540 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1542 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1543 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1546 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1549 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1550 + struct inode *new_dir, struct dentry *new_dentry);
1551 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1553 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1554 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1555 +static void yaffs_write_super(struct super_block *sb);
1557 +static int yaffs_sync_fs(struct super_block *sb);
1558 +static int yaffs_write_super(struct super_block *sb);
1561 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1562 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1563 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1564 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1566 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1568 +static void yaffs_read_inode(struct inode *inode);
1570 +static void yaffs_put_inode(struct inode *inode);
1571 +static void yaffs_delete_inode(struct inode *);
1572 +static void yaffs_clear_inode(struct inode *);
1574 +static int yaffs_readpage(struct file *file, struct page *page);
1575 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1576 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1578 +static int yaffs_writepage(struct page *page);
1580 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1581 + unsigned offset, unsigned to);
1582 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1585 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1587 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1588 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1590 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1593 +static struct address_space_operations yaffs_file_address_operations = {
1594 + .readpage = yaffs_readpage,
1595 + .writepage = yaffs_writepage,
1596 + .prepare_write = yaffs_prepare_write,
1597 + .commit_write = yaffs_commit_write,
1600 +static struct file_operations yaffs_file_operations = {
1601 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1602 + .read = do_sync_read,
1603 + .write = do_sync_write,
1604 + .aio_read = generic_file_aio_read,
1605 + .aio_write = generic_file_aio_write,
1607 + .read = generic_file_read,
1608 + .write = generic_file_write,
1610 + .mmap = generic_file_mmap,
1611 + .flush = yaffs_file_flush,
1612 + .fsync = yaffs_sync_object,
1613 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1614 + .sendfile = generic_file_sendfile,
1619 +static struct inode_operations yaffs_file_inode_operations = {
1620 + .setattr = yaffs_setattr,
1623 +static struct inode_operations yaffs_symlink_inode_operations = {
1624 + .readlink = yaffs_readlink,
1625 + .follow_link = yaffs_follow_link,
1626 + .setattr = yaffs_setattr,
1629 +static struct inode_operations yaffs_dir_inode_operations = {
1630 + .create = yaffs_create,
1631 + .lookup = yaffs_lookup,
1632 + .link = yaffs_link,
1633 + .unlink = yaffs_unlink,
1634 + .symlink = yaffs_symlink,
1635 + .mkdir = yaffs_mkdir,
1636 + .rmdir = yaffs_unlink,
1637 + .mknod = yaffs_mknod,
1638 + .rename = yaffs_rename,
1639 + .setattr = yaffs_setattr,
1642 +static struct file_operations yaffs_dir_operations = {
1643 + .read = generic_read_dir,
1644 + .readdir = yaffs_readdir,
1645 + .fsync = yaffs_sync_object,
1648 +static struct super_operations yaffs_super_ops = {
1649 + .statfs = yaffs_statfs,
1650 + .read_inode = yaffs_read_inode,
1651 + .put_inode = yaffs_put_inode,
1652 + .put_super = yaffs_put_super,
1653 + .delete_inode = yaffs_delete_inode,
1654 + .clear_inode = yaffs_clear_inode,
1655 + .sync_fs = yaffs_sync_fs,
1656 + .write_super = yaffs_write_super,
1659 +static void yaffs_GrossLock(yaffs_Device * dev)
1661 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1663 + down(&dev->grossLock);
1666 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1668 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1669 + up(&dev->grossLock);
1673 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1676 + unsigned char *alias;
1679 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1681 + yaffs_GrossLock(dev);
1683 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1685 + yaffs_GrossUnlock(dev);
1690 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1695 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1696 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1698 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1701 + unsigned char *alias;
1703 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1705 + yaffs_GrossLock(dev);
1707 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1709 + yaffs_GrossUnlock(dev);
1717 + ret = vfs_follow_link(nd, alias);
1720 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1721 + return ERR_PTR (ret);
1727 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1728 + yaffs_Object * obj);
1731 + * Lookup is used to find objects in the fs
1733 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1735 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1736 + struct nameidata *n)
1738 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1741 + yaffs_Object *obj;
1742 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1744 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1746 + yaffs_GrossLock(dev);
1749 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1750 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1753 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1754 + dentry->d_name.name);
1756 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1758 + /* Can't hold gross lock when calling yaffs_get_inode() */
1759 + yaffs_GrossUnlock(dev);
1763 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1765 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1769 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1770 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1771 + * d_add even if NULL inode */
1773 + /*dget(dentry); // try to solve directory bug */
1774 + d_add(dentry, inode);
1776 + /* return dentry; */
1782 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1786 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1787 + * NULL which creates dentry hash */
1788 + d_add(dentry, inode);
1791 + /* return (ERR_PTR(-EIO)); */
1795 +/* For now put inode is just for debugging
1796 + * Put inode is called when the inode **structure** is put.
1798 +static void yaffs_put_inode(struct inode *inode)
1801 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1802 + atomic_read(&inode->i_count)));
1806 +/* clear is called to tell the fs to release any per-inode data it holds */
1807 +static void yaffs_clear_inode(struct inode *inode)
1809 + yaffs_Object *obj;
1810 + yaffs_Device *dev;
1812 + obj = yaffs_InodeToObject(inode);
1815 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1816 + atomic_read(&inode->i_count),
1817 + obj ? "object exists" : "null object"));
1821 + yaffs_GrossLock(dev);
1823 + /* Clear the association between the inode and
1824 + * the yaffs_Object.
1826 + obj->myInode = NULL;
1827 + yaffs_InodeToObjectLV(inode) = NULL;
1829 + /* If the object freeing was deferred, then the real
1830 + * free happens now.
1831 + * This should fix the inode inconsistency problem.
1834 + yaffs_HandleDeferedFree(obj);
1836 + yaffs_GrossUnlock(dev);
1841 +/* delete is called when the link count is zero and the inode
1842 + * is put (ie. nobody wants to know about it anymore, time to
1843 + * delete the file).
1844 + * NB Must call clear_inode()
1846 +static void yaffs_delete_inode(struct inode *inode)
1848 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1849 + yaffs_Device *dev;
1852 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1853 + atomic_read(&inode->i_count),
1854 + obj ? "object exists" : "null object"));
1858 + yaffs_GrossLock(dev);
1859 + yaffs_DeleteFile(obj);
1860 + yaffs_GrossUnlock(dev);
1862 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1863 + truncate_inode_pages (&inode->i_data, 0);
1865 + clear_inode(inode);
1868 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1869 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1871 +static int yaffs_file_flush(struct file *file)
1874 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1876 + yaffs_Device *dev = obj->myDev;
1879 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1880 + obj->dirty ? "dirty" : "clean"));
1882 + yaffs_GrossLock(dev);
1884 + yaffs_FlushFile(obj, 1);
1886 + yaffs_GrossUnlock(dev);
1891 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1893 + /* Lifted from jffs2 */
1895 + yaffs_Object *obj;
1896 + unsigned char *pg_buf;
1899 + yaffs_Device *dev;
1901 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1902 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1903 + (unsigned)PAGE_CACHE_SIZE));
1905 + obj = yaffs_DentryToObject(f->f_dentry);
1909 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1910 + BUG_ON(!PageLocked(pg));
1912 + if (!PageLocked(pg))
1916 + pg_buf = kmap(pg);
1917 + /* FIXME: Can kmap fail? */
1919 + yaffs_GrossLock(dev);
1922 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1925 + yaffs_GrossUnlock(dev);
1931 + ClearPageUptodate(pg);
1934 + SetPageUptodate(pg);
1935 + ClearPageError(pg);
1938 + flush_dcache_page(pg);
1941 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1945 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1947 + int ret = yaffs_readpage_nolock(f, pg);
1952 +static int yaffs_readpage(struct file *f, struct page *pg)
1954 + return yaffs_readpage_unlock(f, pg);
1957 +/* writepage inspired by/stolen from smbfs */
1959 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1960 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1962 +static int yaffs_writepage(struct page *page)
1965 + struct address_space *mapping = page->mapping;
1966 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1967 + struct inode *inode;
1968 + unsigned long end_index;
1970 + yaffs_Object *obj;
1976 + inode = mapping->host;
1980 + if (offset > inode->i_size) {
1983 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1984 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1985 + (unsigned)inode->i_size));
1987 + (KERN_DEBUG " -> don't care!!\n"));
1988 + unlock_page(page);
1992 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1995 + if (page->index < end_index) {
1996 + nBytes = PAGE_CACHE_SIZE;
1998 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
2003 + buffer = kmap(page);
2005 + obj = yaffs_InodeToObject(inode);
2006 + yaffs_GrossLock(obj->myDev);
2009 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
2010 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2012 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
2013 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2016 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2020 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2021 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2023 + yaffs_GrossUnlock(obj->myDev);
2026 + SetPageUptodate(page);
2030 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2033 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2034 + unsigned offset, unsigned to)
2037 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2038 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2039 + return yaffs_readpage_nolock(f, pg);
2045 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2049 + void *addr = page_address(pg) + offset;
2050 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2051 + int nBytes = to - offset;
2054 + unsigned spos = pos;
2055 + unsigned saddr = (unsigned)addr;
2058 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2061 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2063 + if (nWritten != nBytes) {
2066 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2067 + nWritten, nBytes));
2069 + ClearPageUptodate(pg);
2071 + SetPageUptodate(pg);
2075 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2076 + nWritten == nBytes ? 0 : nWritten));
2078 + return nWritten == nBytes ? 0 : nWritten;
2082 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2084 + if (inode && obj) {
2087 + /* Check mode against the variant type and attempt to repair if broken. */
2088 + __u32 mode = obj->yst_mode;
2089 + switch( obj->variantType ){
2090 + case YAFFS_OBJECT_TYPE_FILE :
2091 + if( ! S_ISREG(mode) ){
2092 + obj->yst_mode &= ~S_IFMT;
2093 + obj->yst_mode |= S_IFREG;
2097 + case YAFFS_OBJECT_TYPE_SYMLINK :
2098 + if( ! S_ISLNK(mode) ){
2099 + obj->yst_mode &= ~S_IFMT;
2100 + obj->yst_mode |= S_IFLNK;
2104 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2105 + if( ! S_ISDIR(mode) ){
2106 + obj->yst_mode &= ~S_IFMT;
2107 + obj->yst_mode |= S_IFDIR;
2111 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2112 + case YAFFS_OBJECT_TYPE_HARDLINK :
2113 + case YAFFS_OBJECT_TYPE_SPECIAL :
2119 + inode->i_ino = obj->objectId;
2120 + inode->i_mode = obj->yst_mode;
2121 + inode->i_uid = obj->yst_uid;
2122 + inode->i_gid = obj->yst_gid;
2123 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2124 + inode->i_blksize = inode->i_sb->s_blocksize;
2126 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2128 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2129 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2130 + inode->i_atime.tv_nsec = 0;
2131 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2132 + inode->i_mtime.tv_nsec = 0;
2133 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2134 + inode->i_ctime.tv_nsec = 0;
2136 + inode->i_rdev = obj->yst_rdev;
2137 + inode->i_atime = obj->yst_atime;
2138 + inode->i_mtime = obj->yst_mtime;
2139 + inode->i_ctime = obj->yst_ctime;
2141 + inode->i_size = yaffs_GetObjectFileLength(obj);
2142 + inode->i_blocks = (inode->i_size + 511) >> 9;
2144 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2148 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2149 + inode->i_mode, inode->i_uid, inode->i_gid,
2150 + (int)inode->i_size, atomic_read(&inode->i_count)));
2152 + switch (obj->yst_mode & S_IFMT) {
2153 + default: /* fifo, device or socket */
2154 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2155 + init_special_inode(inode, obj->yst_mode,
2156 + old_decode_dev(obj->yst_rdev));
2158 + init_special_inode(inode, obj->yst_mode,
2159 + (dev_t) (obj->yst_rdev));
2162 + case S_IFREG: /* file */
2163 + inode->i_op = &yaffs_file_inode_operations;
2164 + inode->i_fop = &yaffs_file_operations;
2165 + inode->i_mapping->a_ops =
2166 + &yaffs_file_address_operations;
2168 + case S_IFDIR: /* directory */
2169 + inode->i_op = &yaffs_dir_inode_operations;
2170 + inode->i_fop = &yaffs_dir_operations;
2172 + case S_IFLNK: /* symlink */
2173 + inode->i_op = &yaffs_symlink_inode_operations;
2177 + yaffs_InodeToObjectLV(inode) = obj;
2179 + obj->myInode = inode;
2183 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2188 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2189 + yaffs_Object * obj)
2191 + struct inode *inode;
2195 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2202 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2208 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2210 + inode = iget(sb, obj->objectId);
2212 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2213 + /* iget also increments the inode's i_count */
2214 + /* NB You can't be holding grossLock or deadlock will happen! */
2219 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2222 + yaffs_Object *obj;
2223 + int nWritten, ipos;
2224 + struct inode *inode;
2225 + yaffs_Device *dev;
2227 + obj = yaffs_DentryToObject(f->f_dentry);
2231 + yaffs_GrossLock(dev);
2233 + inode = f->f_dentry->d_inode;
2235 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2236 + ipos = inode->i_size;
2243 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2247 + "yaffs_file_write about to write writing %d bytes"
2248 + "to object %d at %d\n",
2249 + n, obj->objectId, ipos));
2252 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2255 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2256 + n, nWritten, ipos));
2257 + if (nWritten > 0) {
2260 + if (ipos > inode->i_size) {
2261 + inode->i_size = ipos;
2262 + inode->i_blocks = (ipos + 511) >> 9;
2266 + "yaffs_file_write size updated to %d bytes, "
2268 + ipos, (int)(inode->i_blocks)));
2272 + yaffs_GrossUnlock(dev);
2273 + return nWritten == 0 ? -ENOSPC : nWritten;
2276 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2278 + yaffs_Object *obj;
2279 + yaffs_Device *dev;
2280 + struct inode *inode = f->f_dentry->d_inode;
2281 + unsigned long offset, curoffs;
2282 + struct list_head *i;
2285 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2287 + obj = yaffs_DentryToObject(f->f_dentry);
2290 + yaffs_GrossLock(dev);
2292 + offset = f->f_pos;
2294 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2296 + if (offset == 0) {
2298 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2299 + (int)inode->i_ino));
2300 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2307 + if (offset == 1) {
2309 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2310 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2312 + (dirent, "..", 2, offset,
2313 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2322 + /* If the directory has changed since the open or last call to
2323 + readdir, rewind to after the 2 canned entries. */
2325 + if (f->f_version != inode->i_version) {
2327 + f->f_pos = offset;
2328 + f->f_version = inode->i_version;
2331 + list_for_each(i, &obj->variant.directoryVariant.children) {
2333 + if (curoffs >= offset) {
2334 + l = list_entry(i, yaffs_Object, siblings);
2336 + yaffs_GetObjectName(l, name,
2337 + YAFFS_MAX_NAME_LENGTH + 1);
2339 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2340 + yaffs_GetObjectInode(l)));
2342 + if (filldir(dirent,
2346 + yaffs_GetObjectInode(l),
2347 + yaffs_GetObjectType(l))
2360 + yaffs_GrossUnlock(dev);
2366 + * File creation. Allocate an inode, and we're done..
2368 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2369 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2372 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2376 + struct inode *inode;
2378 + yaffs_Object *obj = NULL;
2379 + yaffs_Device *dev;
2381 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2383 + int error = -ENOSPC;
2384 + uid_t uid = current->fsuid;
2385 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2387 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2392 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2393 + parent->objectId, parent->variantType));
2396 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2400 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2401 + "mode %x dev %x\n",
2402 + dentry->d_name.name, mode, rdev));
2404 + dev = parent->myDev;
2406 + yaffs_GrossLock(dev);
2408 + switch (mode & S_IFMT) {
2410 + /* Special (socket, fifo, device...) */
2411 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2412 + "yaffs_mknod: making special\n"));
2413 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2415 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2416 + gid, old_encode_dev(rdev));
2419 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2423 + case S_IFREG: /* file */
2424 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2426 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2429 + case S_IFDIR: /* directory */
2431 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2433 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2436 + case S_IFLNK: /* symlink */
2437 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2438 + obj = NULL; /* Do we ever get here? */
2442 + /* Can not call yaffs_get_inode() with gross lock held */
2443 + yaffs_GrossUnlock(dev);
2446 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2447 + d_instantiate(dentry, inode);
2449 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2450 + obj->objectId, atomic_read(&inode->i_count)));
2454 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2461 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2464 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2465 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2467 + /* attempt to fix dir bug - didn't work */
2475 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2476 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2477 + struct nameidata *n)
2479 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2482 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2483 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2486 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2490 + yaffs_Device *dev;
2493 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2494 + dentry->d_name.name));
2496 + dev = yaffs_InodeToObject(dir)->myDev;
2498 + yaffs_GrossLock(dev);
2500 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2502 + if (retVal == YAFFS_OK) {
2503 + dentry->d_inode->i_nlink--;
2505 + yaffs_GrossUnlock(dev);
2506 + mark_inode_dirty(dentry->d_inode);
2509 + yaffs_GrossUnlock(dev);
2510 + return -ENOTEMPTY;
2514 + * Create a link...
2516 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2517 + struct dentry *dentry)
2519 + struct inode *inode = old_dentry->d_inode;
2520 + yaffs_Object *obj = NULL;
2521 + yaffs_Object *link = NULL;
2522 + yaffs_Device *dev;
2524 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2526 + obj = yaffs_InodeToObject(inode);
2529 + yaffs_GrossLock(dev);
2531 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2534 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2539 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2540 + d_instantiate(dentry, old_dentry->d_inode);
2541 + atomic_inc(&old_dentry->d_inode->i_count);
2543 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2544 + old_dentry->d_inode->i_nlink,
2545 + atomic_read(&old_dentry->d_inode->i_count)));
2549 + yaffs_GrossUnlock(dev);
2559 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2560 + const char *symname)
2562 + yaffs_Object *obj;
2563 + yaffs_Device *dev;
2564 + uid_t uid = current->fsuid;
2565 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2567 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2569 + dev = yaffs_InodeToObject(dir)->myDev;
2570 + yaffs_GrossLock(dev);
2571 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2572 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2573 + yaffs_GrossUnlock(dev);
2577 + struct inode *inode;
2579 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2580 + d_instantiate(dentry, inode);
2581 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2584 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2591 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2595 + yaffs_Object *obj;
2596 + yaffs_Device *dev;
2598 + obj = yaffs_DentryToObject(dentry);
2602 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2603 + yaffs_GrossLock(dev);
2604 + yaffs_FlushFile(obj, 1);
2605 + yaffs_GrossUnlock(dev);
2610 + * The VFS layer already does all the dentry stuff for rename.
2612 + * NB: POSIX says you can rename an object over an old object of the same name
2614 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2615 + struct inode *new_dir, struct dentry *new_dentry)
2617 + yaffs_Device *dev;
2618 + int retVal = YAFFS_FAIL;
2619 + yaffs_Object *target;
2621 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2622 + dev = yaffs_InodeToObject(old_dir)->myDev;
2624 + yaffs_GrossLock(dev);
2626 + /* Check if the target is an existing directory that is not empty. */
2628 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2629 + new_dentry->d_name.name);
2634 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2635 + !list_empty(&target->variant.directoryVariant.children)) {
2637 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2639 + retVal = YAFFS_FAIL;
2642 + /* Now does unlinking internally using shadowing mechanism */
2643 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2646 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2647 + old_dentry->d_name.name,
2648 + yaffs_InodeToObject(new_dir),
2649 + new_dentry->d_name.name);
2652 + yaffs_GrossUnlock(dev);
2654 + if (retVal == YAFFS_OK) {
2656 + new_dentry->d_inode->i_nlink--;
2657 + mark_inode_dirty(new_dentry->d_inode);
2662 + return -ENOTEMPTY;
2667 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2669 + struct inode *inode = dentry->d_inode;
2671 + yaffs_Device *dev;
2674 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2675 + yaffs_InodeToObject(inode)->objectId));
2677 + if ((error = inode_change_ok(inode, attr)) == 0) {
2679 + dev = yaffs_InodeToObject(inode)->myDev;
2680 + yaffs_GrossLock(dev);
2681 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2687 + yaffs_GrossUnlock(dev);
2689 + error = inode_setattr(inode, attr);
2694 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2695 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2697 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2698 + struct super_block *sb = dentry->d_sb;
2699 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2700 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2702 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2704 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2706 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2709 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2711 + yaffs_GrossLock(dev);
2713 + buf->f_type = YAFFS_MAGIC;
2714 + buf->f_bsize = sb->s_blocksize;
2715 + buf->f_namelen = 255;
2716 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2719 + (dev->endBlock - dev->startBlock +
2720 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2721 + dev->nDataBytesPerChunk);
2723 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2724 + dev->nDataBytesPerChunk);
2728 + (dev->endBlock - dev->startBlock +
2729 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2732 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2737 + buf->f_bavail = buf->f_bfree;
2739 + yaffs_GrossUnlock(dev);
2745 +static int yaffs_do_sync_fs(struct super_block *sb)
2748 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2749 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2752 + yaffs_GrossLock(dev);
2755 + yaffs_CheckpointSave(dev);
2757 + yaffs_GrossUnlock(dev);
2765 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2766 +static void yaffs_write_super(struct super_block *sb)
2768 +static int yaffs_write_super(struct super_block *sb)
2772 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2773 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2774 + return 0; /* yaffs_do_sync_fs(sb);*/
2779 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2780 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2782 +static int yaffs_sync_fs(struct super_block *sb)
2786 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2788 + return 0; /* yaffs_do_sync_fs(sb);*/
2793 +static void yaffs_read_inode(struct inode *inode)
2795 + /* NB This is called as a side effect of other functions, but
2796 + * we had to release the lock to prevent deadlocks, so
2797 + * need to lock again.
2800 + yaffs_Object *obj;
2801 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2804 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2806 + yaffs_GrossLock(dev);
2808 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2810 + yaffs_FillInodeFromObject(inode, obj);
2812 + yaffs_GrossUnlock(dev);
2815 +static LIST_HEAD(yaffs_dev_list);
2817 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2819 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2821 + if( *flags & MS_RDONLY ) {
2822 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2825 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2827 + yaffs_GrossLock(dev);
2829 + yaffs_FlushEntireDeviceCache(dev);
2831 + yaffs_CheckpointSave(dev);
2836 + yaffs_GrossUnlock(dev);
2840 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2846 +static void yaffs_put_super(struct super_block *sb)
2848 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2850 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2852 + yaffs_GrossLock(dev);
2854 + yaffs_FlushEntireDeviceCache(dev);
2856 + yaffs_CheckpointSave(dev);
2858 + if (dev->putSuperFunc) {
2859 + dev->putSuperFunc(sb);
2862 + yaffs_Deinitialise(dev);
2864 + yaffs_GrossUnlock(dev);
2866 + /* we assume this is protected by lock_kernel() in mount/umount */
2867 + list_del(&dev->devList);
2869 + if(dev->spareBuffer){
2870 + YFREE(dev->spareBuffer);
2871 + dev->spareBuffer = NULL;
2878 +static void yaffs_MTDPutSuper(struct super_block *sb)
2881 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2887 + put_mtd_device(mtd);
2891 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2893 + struct super_block *sb = (struct super_block *)vsb;
2895 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2902 + int skip_checkpoint_read;
2903 + int skip_checkpoint_write;
2907 +#define MAX_OPT_LEN 20
2908 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2910 + char cur_opt[MAX_OPT_LEN+1];
2914 + /* Parse through the options which is a comma seperated list */
2916 + while(options_str && *options_str && !error){
2917 + memset(cur_opt,0,MAX_OPT_LEN+1);
2920 + while(*options_str && *options_str != ','){
2921 + if(p < MAX_OPT_LEN){
2922 + cur_opt[p] = *options_str;
2928 + if(!strcmp(cur_opt,"inband-tags"))
2929 + options->inband_tags = 1;
2930 + else if(!strcmp(cur_opt,"no-cache"))
2931 + options->no_cache = 1;
2932 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2933 + options->skip_checkpoint_read = 1;
2934 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2935 + options->skip_checkpoint_write = 1;
2936 + else if(!strcmp(cur_opt,"no-checkpoint")){
2937 + options->skip_checkpoint_read = 1;
2938 + options->skip_checkpoint_write = 1;
2940 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2949 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2950 + struct super_block *sb,
2951 + void *data, int silent)
2954 + struct inode *inode = NULL;
2955 + struct dentry *root;
2956 + yaffs_Device *dev = 0;
2957 + char devname_buf[BDEVNAME_SIZE + 1];
2958 + struct mtd_info *mtd;
2960 + char *data_str = (char *)data;
2962 + yaffs_options options;
2964 + sb->s_magic = YAFFS_MAGIC;
2965 + sb->s_op = &yaffs_super_ops;
2968 + printk(KERN_INFO "yaffs: sb is NULL\n");
2969 + else if (!sb->s_dev)
2970 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2971 + else if (!yaffs_devname(sb, devname_buf))
2972 + printk(KERN_INFO "yaffs: devname is NULL\n");
2974 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2976 + yaffs_devname(sb, devname_buf));
2981 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2983 + memset(&options,0,sizeof(options));
2985 + if(yaffs_parse_options(&options,data_str)){
2986 + /* Option parsing failed */
2991 + sb->s_blocksize = PAGE_CACHE_SIZE;
2992 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2993 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2995 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2997 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2999 + ("yaffs: Write verification disabled. All guarantees "
3000 + "null and void\n"));
3003 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3005 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
3006 + yaffs_devname(sb, devname_buf)));
3008 + /* Check it's an mtd device..... */
3009 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
3010 + return NULL; /* This isn't an mtd device */
3012 + /* Get the device */
3013 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3015 + T(YAFFS_TRACE_ALWAYS,
3016 + ("yaffs: MTD device #%u doesn't appear to exist\n",
3017 + MINOR(sb->s_dev)));
3020 + /* Check it's NAND */
3021 + if (mtd->type != MTD_NANDFLASH) {
3022 + T(YAFFS_TRACE_ALWAYS,
3023 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3027 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3028 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3029 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3030 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3031 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3032 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3033 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3034 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3035 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3036 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3037 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3039 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3041 + if (yaffsVersion == 1 &&
3042 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3043 + mtd->writesize >= 2048) {
3045 + mtd->oobblock >= 2048) {
3047 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3051 + /* Added NCB 26/5/2006 for completeness */
3052 + if (yaffsVersion == 2 &&
3053 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3054 + mtd->writesize == 512) {
3056 + mtd->oobblock == 512) {
3058 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3064 + if (yaffsVersion == 2) {
3065 + /* Check for version 2 style functions */
3066 + if (!mtd->erase ||
3067 + !mtd->block_isbad ||
3068 + !mtd->block_markbad ||
3071 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3072 + !mtd->read_oob || !mtd->write_oob) {
3074 + !mtd->write_ecc ||
3075 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3077 + T(YAFFS_TRACE_ALWAYS,
3078 + ("yaffs: MTD device does not support required "
3083 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3084 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3086 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3088 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3089 + T(YAFFS_TRACE_ALWAYS,
3090 + ("yaffs: MTD device does not have the "
3091 + "right page sizes\n"));
3095 + /* Check for V1 style functions */
3096 + if (!mtd->erase ||
3099 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3100 + !mtd->read_oob || !mtd->write_oob) {
3102 + !mtd->write_ecc ||
3103 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3105 + T(YAFFS_TRACE_ALWAYS,
3106 + ("yaffs: MTD device does not support required "
3111 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3112 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3113 + T(YAFFS_TRACE_ALWAYS,
3114 + ("yaffs: MTD device does not support have the "
3115 + "right page sizes\n"));
3120 + /* OK, so if we got here, we have an MTD that's NAND and looks
3121 + * like it has the right capabilities
3122 + * Set the yaffs_Device up for mtd
3125 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3126 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3128 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3131 + /* Deep shit could not allocate device structure */
3132 + T(YAFFS_TRACE_ALWAYS,
3133 + ("yaffs_read_super: Failed trying to allocate "
3134 + "yaffs_Device. \n"));
3138 + memset(dev, 0, sizeof(yaffs_Device));
3139 + dev->genericDevice = mtd;
3140 + dev->name = mtd->name;
3142 + /* Set up the memory size parameters.... */
3144 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3145 + dev->startBlock = 0;
3146 + dev->endBlock = nBlocks - 1;
3147 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3148 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3149 + dev->nReservedBlocks = 5;
3150 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3152 + /* ... and the functions. */
3153 + if (yaffsVersion == 2) {
3154 + dev->writeChunkWithTagsToNAND =
3155 + nandmtd2_WriteChunkWithTagsToNAND;
3156 + dev->readChunkWithTagsFromNAND =
3157 + nandmtd2_ReadChunkWithTagsFromNAND;
3158 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3159 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3160 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3161 + dev->isYaffs2 = 1;
3162 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3163 + dev->nDataBytesPerChunk = mtd->writesize;
3164 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3166 + dev->nDataBytesPerChunk = mtd->oobblock;
3167 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3169 + nBlocks = mtd->size / mtd->erasesize;
3171 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3172 + dev->startBlock = 0;
3173 + dev->endBlock = nBlocks - 1;
3175 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3176 + /* use the MTD interface in yaffs_mtdif1.c */
3177 + dev->writeChunkWithTagsToNAND =
3178 + nandmtd1_WriteChunkWithTagsToNAND;
3179 + dev->readChunkWithTagsFromNAND =
3180 + nandmtd1_ReadChunkWithTagsFromNAND;
3181 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3182 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3184 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3185 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3187 + dev->isYaffs2 = 0;
3189 + /* ... and common functions */
3190 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3191 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3193 + dev->putSuperFunc = yaffs_MTDPutSuper;
3195 + dev->superBlock = (void *)sb;
3196 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3199 +#ifndef CONFIG_YAFFS_DOES_ECC
3200 + dev->useNANDECC = 1;
3203 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3204 + dev->wideTnodesDisabled = 1;
3207 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3208 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3210 + /* we assume this is protected by lock_kernel() in mount/umount */
3211 + list_add_tail(&dev->devList, &yaffs_dev_list);
3213 + init_MUTEX(&dev->grossLock);
3215 + yaffs_GrossLock(dev);
3217 + err = yaffs_GutsInitialise(dev);
3220 + ("yaffs_read_super: guts initialised %s\n",
3221 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3223 + /* Release lock before yaffs_get_inode() */
3224 + yaffs_GrossUnlock(dev);
3226 + /* Create root inode */
3227 + if (err == YAFFS_OK)
3228 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3234 + inode->i_op = &yaffs_dir_inode_operations;
3235 + inode->i_fop = &yaffs_dir_operations;
3237 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3239 + root = d_alloc_root(inode);
3241 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3247 + sb->s_root = root;
3249 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3254 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3255 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3258 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3261 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3262 +static int yaffs_read_super(struct file_system_type *fs,
3263 + int flags, const char *dev_name,
3264 + void *data, struct vfsmount *mnt)
3267 + return get_sb_bdev(fs, flags, dev_name, data,
3268 + yaffs_internal_read_super_mtd, mnt);
3271 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3272 + int flags, const char *dev_name,
3276 + return get_sb_bdev(fs, flags, dev_name, data,
3277 + yaffs_internal_read_super_mtd);
3281 +static struct file_system_type yaffs_fs_type = {
3282 + .owner = THIS_MODULE,
3284 + .get_sb = yaffs_read_super,
3285 + .kill_sb = kill_block_super,
3286 + .fs_flags = FS_REQUIRES_DEV,
3289 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3292 + return yaffs_internal_read_super(1, sb, data, silent);
3295 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3300 +#ifdef CONFIG_YAFFS_YAFFS2
3302 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3303 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3306 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3309 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3310 +static int yaffs2_read_super(struct file_system_type *fs,
3311 + int flags, const char *dev_name, void *data,
3312 + struct vfsmount *mnt)
3314 + return get_sb_bdev(fs, flags, dev_name, data,
3315 + yaffs2_internal_read_super_mtd, mnt);
3318 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3319 + int flags, const char *dev_name,
3323 + return get_sb_bdev(fs, flags, dev_name, data,
3324 + yaffs2_internal_read_super_mtd);
3328 +static struct file_system_type yaffs2_fs_type = {
3329 + .owner = THIS_MODULE,
3331 + .get_sb = yaffs2_read_super,
3332 + .kill_sb = kill_block_super,
3333 + .fs_flags = FS_REQUIRES_DEV,
3336 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3337 + void *data, int silent)
3339 + return yaffs_internal_read_super(2, sb, data, silent);
3342 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3346 +#endif /* CONFIG_YAFFS_YAFFS2 */
3348 +static struct proc_dir_entry *my_proc_entry;
3350 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3352 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3353 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3354 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3355 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3356 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3357 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3358 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3359 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3360 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3361 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3362 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3363 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3364 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3365 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3366 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3367 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3368 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3369 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3371 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3373 + sprintf(buf, "passiveGCs......... %d\n",
3374 + dev->passiveGarbageCollections);
3375 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3376 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3377 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3378 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3379 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3380 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3381 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3382 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3383 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3384 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3386 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3387 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3388 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3393 +static int yaffs_proc_read(char *page,
3395 + off_t offset, int count, int *eof, void *data)
3397 + struct list_head *item;
3399 + int step = offset;
3402 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3403 + * We use 'offset' (*ppos) to indicate where we are in devList.
3404 + * This also assumes the user has posted a read buffer large
3405 + * enough to hold the complete output; but that's life in /proc.
3408 + *(int *)start = 1;
3410 + /* Print header first */
3412 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3413 + "\n%s\n%s\n", yaffs_fs_c_version,
3414 + yaffs_guts_c_version);
3417 + /* hold lock_kernel while traversing yaffs_dev_list */
3420 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3421 + list_for_each(item, &yaffs_dev_list) {
3422 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3427 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3428 + buf = yaffs_dump_dev(buf, dev);
3433 + return buf - page < count ? buf - page : count;
3437 + * Set the verbosity of the warnings and error messages.
3439 + * Note that the names can only be a..z or _ with the current code.
3444 + unsigned mask_bitfield;
3446 + {"allocate", YAFFS_TRACE_ALLOCATE},
3447 + {"always", YAFFS_TRACE_ALWAYS},
3448 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3449 + {"buffers", YAFFS_TRACE_BUFFERS},
3450 + {"bug", YAFFS_TRACE_BUG},
3451 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3452 + {"deletion", YAFFS_TRACE_DELETION},
3453 + {"erase", YAFFS_TRACE_ERASE},
3454 + {"error", YAFFS_TRACE_ERROR},
3455 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3456 + {"gc", YAFFS_TRACE_GC},
3457 + {"mtd", YAFFS_TRACE_MTD},
3458 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3459 + {"os", YAFFS_TRACE_OS},
3460 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3461 + {"scan", YAFFS_TRACE_SCAN},
3462 + {"tracing", YAFFS_TRACE_TRACING},
3464 + {"verify", YAFFS_TRACE_VERIFY},
3465 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3466 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3467 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3469 + {"write", YAFFS_TRACE_WRITE},
3470 + {"all", 0xffffffff},
3475 +#define MAX_MASK_NAME_LENGTH 40
3476 +static int yaffs_proc_write(struct file *file, const char *buf,
3477 + unsigned long count, void *data)
3479 + unsigned rg = 0, mask_bitfield;
3483 + char substring[MAX_MASK_NAME_LENGTH+1];
3489 + rg = yaffs_traceMask;
3491 + while (!done && (pos < count)) {
3493 + while ((pos < count) && isspace(buf[pos])) {
3497 + switch (buf[pos]) {
3511 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3512 + if (end > buf + pos) {
3513 + mask_name = "numeral";
3514 + len = end - (buf + pos);
3517 + for(x = buf + pos, i = 0;
3518 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3519 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3520 + substring[i] = *x;
3521 + substring[i] = '\0';
3523 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3524 + //len = strlen(mask_flags[i].mask_name);
3525 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3526 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3527 + mask_name = mask_flags[i].mask_name;
3528 + mask_bitfield = mask_flags[i].mask_bitfield;
3535 + if (mask_name != NULL) {
3540 + rg &= ~mask_bitfield;
3543 + rg |= mask_bitfield;
3546 + rg = mask_bitfield;
3549 + rg |= mask_bitfield;
3555 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3557 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3559 + if (rg & YAFFS_TRACE_ALWAYS) {
3560 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3562 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3563 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3570 +/* Stuff to handle installation of file systems */
3571 +struct file_system_to_install {
3572 + struct file_system_type *fst;
3576 +static struct file_system_to_install fs_to_install[] = {
3577 +//#ifdef CONFIG_YAFFS_YAFFS1
3578 + {&yaffs_fs_type, 0},
3580 +//#ifdef CONFIG_YAFFS_YAFFS2
3581 + {&yaffs2_fs_type, 0},
3586 +static int __init init_yaffs_fs(void)
3589 + struct file_system_to_install *fsinst;
3591 + T(YAFFS_TRACE_ALWAYS,
3592 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3594 + /* Install the proc_fs entry */
3595 + my_proc_entry = create_proc_entry("yaffs",
3596 + S_IRUGO | S_IFREG,
3599 + if (my_proc_entry) {
3600 + my_proc_entry->write_proc = yaffs_proc_write;
3601 + my_proc_entry->read_proc = yaffs_proc_read;
3602 + my_proc_entry->data = NULL;
3607 + /* Now add the file system entries */
3609 + fsinst = fs_to_install;
3611 + while (fsinst->fst && !error) {
3612 + error = register_filesystem(fsinst->fst);
3614 + fsinst->installed = 1;