fb0299e6e1b99b09d1691e1b2999131a3ea68e50
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches / 510-Yaffs.patch
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
4 @@ -0,0 +1,175 @@
5 +#
6 +# YAFFS file system configurations
7 +#
8 +
9 +config YAFFS_FS
10 + tristate "YAFFS2 file system support"
11 + default n
12 + depends on MTD
13 + select YAFFS_YAFFS1
14 + select YAFFS_YAFFS2
15 + help
16 + YAFFS2, or Yet Another Flash Filing System, is a filing system
17 + optimised for NAND Flash chips.
18 +
19 + To compile the YAFFS2 file system support as a module, choose M
20 + here: the module will be called yaffs2.
21 +
22 + If unsure, say N.
23 +
24 + Further information on YAFFS2 is available at
25 + <http://www.aleph1.co.uk/yaffs/>.
26 +
27 +config YAFFS_YAFFS1
28 + bool "512 byte / page devices"
29 + depends on YAFFS_FS
30 + default y
31 + help
32 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
33 +
34 + Not needed for 2K-page devices.
35 +
36 + If unsure, say Y.
37 +
38 +config YAFFS_9BYTE_TAGS
39 + bool "Use older-style on-NAND data format with pageStatus byte"
40 + depends on YAFFS_YAFFS1
41 + default n
42 + help
43 +
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.
51 +
52 + If unsure, say N.
53 +
54 +config YAFFS_DOES_ECC
55 + bool "Lets Yaffs do its own ECC"
56 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
57 + default n
58 + help
59 + This enables Yaffs to use its own ECC functions instead of using
60 + the ones from the generic MTD-NAND driver.
61 +
62 + If unsure, say N.
63 +
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
67 + default n
68 + help
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.
72 +
73 + If unsure, say N.
74 +
75 +config YAFFS_YAFFS2
76 + bool "2048 byte (or larger) / page devices"
77 + depends on YAFFS_FS
78 + default y
79 + help
80 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
81 +
82 + If unsure, say Y.
83 +
84 +config YAFFS_AUTO_YAFFS2
85 + bool "Autoselect yaffs2 format"
86 + depends on YAFFS_YAFFS2
87 + default y
88 + help
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).
93 +
94 + If unsure, say Y.
95 +
96 +config YAFFS_DISABLE_LAZY_LOAD
97 + bool "Disable lazy loading"
98 + depends on YAFFS_YAFFS2
99 + default n
100 + help
101 + "Lazy loading" defers loading file details until they are
102 + required. This saves mount time, but makes the first look-up
103 + a bit longer.
104 +
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
108 + thing.
109 +
110 + Lazy laoding will be required by checkpointing.
111 +
112 + Setting this to 'y' will disable lazy loading.
113 +
114 + If unsure, say N.
115 +
116 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
117 + int "Reserved blocks for checkpointing"
118 + depends on YAFFS_YAFFS2
119 + default 10
120 + help
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
130 + checkpointing.
131 +
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.
136 +
137 +config YAFFS_DISABLE_WIDE_TNODES
138 + bool "Turn off wide tnodes"
139 + depends on YAFFS_FS
140 + default n
141 + help
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
145 + searching.
146 +
147 + Setting this to 'y' will force tnode width to 16 bits and save
148 + memory but make large arrays slower.
149 +
150 + If unsure, say N.
151 +
152 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
153 + bool "Force chunk erase check"
154 + depends on YAFFS_FS
155 + default n
156 + help
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.
160 +
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
163 + a bit more secure.
164 +
165 + Suggest setting Y during development and ironing out driver
166 + issues etc. Suggest setting to N if you want faster writing.
167 +
168 + If unsure, say Y.
169 +
170 +config YAFFS_SHORT_NAMES_IN_RAM
171 + bool "Cache short names in RAM"
172 + depends on YAFFS_FS
173 + default y
174 + help
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.
178 +
179 + If unsure, say Y.
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
183 @@ -0,0 +1,11 @@
184 +#
185 +# Makefile for the linux YAFFS filesystem routines.
186 +#
187 +
188 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
189 +
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
198 @@ -0,0 +1,264 @@
199 +/*
200 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
201 + *
202 + * Copyright (C) 2002-2007 Aleph One Ltd.
203 + * for Toby Churchill Ltd and Brightstar Engineering
204 + *
205 + * Created by Charles Manning <charles@aleph1.co.uk>
206 + *
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.
210 + *
211 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
212 + */
213 +
214 +/*
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
217 + * applications.
218 + *
219 + */
220 +
221 +#ifndef __EXTRAS_H__
222 +#define __EXTRAS_H__
223 +
224 +#if defined WIN32
225 +#define __inline__ __inline
226 +#define new newHack
227 +#endif
228 +
229 +#if !(defined __KERNEL__) || (defined WIN32)
230 +
231 +/* User space defines */
232 +
233 +typedef unsigned char __u8;
234 +typedef unsigned short __u16;
235 +typedef unsigned __u32;
236 +
237 +/*
238 + * Simple doubly linked list implementation.
239 + *
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.
245 + */
246 +
247 +#define prefetch(x) 1
248 +
249 +struct list_head {
250 + struct list_head *next, *prev;
251 +};
252 +
253 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
254 +
255 +#define LIST_HEAD(name) \
256 + struct list_head name = LIST_HEAD_INIT(name)
257 +
258 +#define INIT_LIST_HEAD(ptr) do { \
259 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
260 +} while (0)
261 +
262 +/*
263 + * Insert a new entry between two known consecutive entries.
264 + *
265 + * This is only for internal list manipulation where we know
266 + * the prev/next entries already!
267 + */
268 +static __inline__ void __list_add(struct list_head *new,
269 + struct list_head *prev,
270 + struct list_head *next)
271 +{
272 + next->prev = new;
273 + new->next = next;
274 + new->prev = prev;
275 + prev->next = new;
276 +}
277 +
278 +/**
279 + * list_add - add a new entry
280 + * @new: new entry to be added
281 + * @head: list head to add it after
282 + *
283 + * Insert a new entry after the specified head.
284 + * This is good for implementing stacks.
285 + */
286 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
287 +{
288 + __list_add(new, head, head->next);
289 +}
290 +
291 +/**
292 + * list_add_tail - add a new entry
293 + * @new: new entry to be added
294 + * @head: list head to add it before
295 + *
296 + * Insert a new entry before the specified head.
297 + * This is useful for implementing queues.
298 + */
299 +static __inline__ void list_add_tail(struct list_head *new,
300 + struct list_head *head)
301 +{
302 + __list_add(new, head->prev, head);
303 +}
304 +
305 +/*
306 + * Delete a list entry by making the prev/next entries
307 + * point to each other.
308 + *
309 + * This is only for internal list manipulation where we know
310 + * the prev/next entries already!
311 + */
312 +static __inline__ void __list_del(struct list_head *prev,
313 + struct list_head *next)
314 +{
315 + next->prev = prev;
316 + prev->next = next;
317 +}
318 +
319 +/**
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.
324 + */
325 +static __inline__ void list_del(struct list_head *entry)
326 +{
327 + __list_del(entry->prev, entry->next);
328 +}
329 +
330 +/**
331 + * list_del_init - deletes entry from list and reinitialize it.
332 + * @entry: the element to delete from the list.
333 + */
334 +static __inline__ void list_del_init(struct list_head *entry)
335 +{
336 + __list_del(entry->prev, entry->next);
337 + INIT_LIST_HEAD(entry);
338 +}
339 +
340 +/**
341 + * list_empty - tests whether a list is empty
342 + * @head: the list to test.
343 + */
344 +static __inline__ int list_empty(struct list_head *head)
345 +{
346 + return head->next == head;
347 +}
348 +
349 +/**
350 + * list_splice - join two lists
351 + * @list: the new list to add.
352 + * @head: the place to add it in the first list.
353 + */
354 +static __inline__ void list_splice(struct list_head *list,
355 + struct list_head *head)
356 +{
357 + struct list_head *first = list->next;
358 +
359 + if (first != list) {
360 + struct list_head *last = list->prev;
361 + struct list_head *at = head->next;
362 +
363 + first->prev = head;
364 + head->next = first;
365 +
366 + last->next = at;
367 + at->prev = last;
368 + }
369 +}
370 +
371 +/**
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.
376 + */
377 +#define list_entry(ptr, type, member) \
378 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
379 +
380 +/**
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.
384 + */
385 +#define list_for_each(pos, head) \
386 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
387 + pos = pos->next, prefetch(pos->next))
388 +
389 +/**
390 + * list_for_each_safe - iterate over a list safe against removal
391 + * of list entry
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.
395 + */
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)
399 +
400 +/*
401 + * File types
402 + */
403 +#define DT_UNKNOWN 0
404 +#define DT_FIFO 1
405 +#define DT_CHR 2
406 +#define DT_DIR 4
407 +#define DT_BLK 6
408 +#define DT_REG 8
409 +#define DT_LNK 10
410 +#define DT_SOCK 12
411 +#define DT_WHT 14
412 +
413 +#ifndef WIN32
414 +#include <sys/stat.h>
415 +#endif
416 +
417 +/*
418 + * Attribute flags. These should be or-ed together to figure out what
419 + * has been changed!
420 + */
421 +#define ATTR_MODE 1
422 +#define ATTR_UID 2
423 +#define ATTR_GID 4
424 +#define ATTR_SIZE 8
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
432 +
433 +struct iattr {
434 + unsigned int ia_valid;
435 + unsigned ia_mode;
436 + unsigned ia_uid;
437 + unsigned ia_gid;
438 + unsigned ia_size;
439 + unsigned ia_atime;
440 + unsigned ia_mtime;
441 + unsigned ia_ctime;
442 + unsigned int ia_attr_flags;
443 +};
444 +
445 +#define KERN_DEBUG
446 +
447 +#else
448 +
449 +#ifndef WIN32
450 +#include <linux/types.h>
451 +#include <linux/list.h>
452 +#include <linux/fs.h>
453 +#include <linux/stat.h>
454 +#endif
455 +
456 +#endif
457 +
458 +#if defined WIN32
459 +#undef new
460 +#endif
461 +
462 +#endif
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
466 @@ -0,0 +1,65 @@
467 +/*
468 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
469 + *
470 + * Copyright (C) 2002-2007 Aleph One Ltd.
471 + * for Toby Churchill Ltd and Brightstar Engineering
472 + *
473 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
474 + *
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.
478 + *
479 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
480 + */
481 +
482 +#ifndef __YAFFS_CONFIG_H__
483 +#define __YAFFS_CONFIG_H__
484 +
485 +#ifdef YAFFS_OUT_OF_TREE
486 +
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
491 +
492 +/* These options are independent of each other. Select those that matter. */
493 +
494 +/* Default: Not selected */
495 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
496 +//#define CONFIG_YAFFS_DOES_ECC
497 +
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
502 +
503 +/* Default: Selected */
504 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
505 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
506 +
507 +/* Default: Selected */
508 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
509 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
510 +
511 +/* Default: 10 */
512 +/* Meaning: set the count of blocks to reserve for checkpointing */
513 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
514 +
515 +/*
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
520 +older-style format.
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
523 +MTD versions.
524 +*/
525 +/* Default: Not selected */
526 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
527 +#define CONFIG_YAFFS_9BYTE_TAGS
528 +
529 +#endif /* YAFFS_OUT_OF_TREE */
530 +
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
535 @@ -0,0 +1,404 @@
536 +/*
537 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
538 + *
539 + * Copyright (C) 2002-2007 Aleph One Ltd.
540 + * for Toby Churchill Ltd and Brightstar Engineering
541 + *
542 + * Created by Charles Manning <charles@aleph1.co.uk>
543 + *
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.
547 + */
548 +
549 +const char *yaffs_checkptrw_c_version =
550 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
551 +
552 +
553 +#include "yaffs_checkptrw.h"
554 +
555 +
556 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
557 +{
558 +
559 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
560 +
561 + T(YAFFS_TRACE_CHECKPOINT,
562 + (TSTR("checkpt blocks available = %d" TENDSTR),
563 + blocksAvailable));
564 +
565 +
566 + return (blocksAvailable <= 0) ? 0 : 1;
567 +}
568 +
569 +
570 +static int yaffs_CheckpointErase(yaffs_Device *dev)
571 +{
572 +
573 + int i;
574 +
575 +
576 + if(!dev->eraseBlockInNAND)
577 + return 0;
578 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
579 + dev->internalStartBlock,dev->internalEndBlock));
580 +
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;
589 + }
590 + else {
591 + dev->markNANDBlockBad(dev,i);
592 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
593 + }
594 + }
595 + }
596 +
597 + dev->blocksInCheckpoint = 0;
598 +
599 + return 1;
600 +}
601 +
602 +
603 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
604 +{
605 + int i;
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));
610 +
611 + if(dev->checkpointNextBlock >= 0 &&
612 + dev->checkpointNextBlock <= dev->internalEndBlock &&
613 + blocksAvailable > 0){
614 +
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));
621 + return;
622 + }
623 + }
624 + }
625 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
626 +
627 + dev->checkpointNextBlock = -1;
628 + dev->checkpointCurrentBlock = -1;
629 +}
630 +
631 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
632 +{
633 + int i;
634 + yaffs_ExtendedTags tags;
635 +
636 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
637 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
638 +
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;
643 +
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));
647 +
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));
655 + return;
656 + }
657 + }
658 +
659 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
660 +
661 + dev->checkpointNextBlock = -1;
662 + dev->checkpointCurrentBlock = -1;
663 +}
664 +
665 +
666 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
667 +{
668 +
669 + /* Got the functions we need? */
670 + if (!dev->writeChunkWithTagsToNAND ||
671 + !dev->readChunkWithTagsFromNAND ||
672 + !dev->eraseBlockInNAND ||
673 + !dev->markNANDBlockBad)
674 + return 0;
675 +
676 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
677 + return 0;
678 +
679 + if(!dev->checkpointBuffer)
680 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
681 + if(!dev->checkpointBuffer)
682 + return 0;
683 +
684 +
685 + dev->checkpointPageSequence = 0;
686 +
687 + dev->checkpointOpenForWrite = forWriting;
688 +
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;
695 +
696 + /* Erase all the blocks in the checkpoint area */
697 + if(forWriting){
698 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
699 + dev->checkpointByteOffset = 0;
700 + return yaffs_CheckpointErase(dev);
701 +
702 +
703 + } else {
704 + int i;
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;
714 + }
715 +
716 + return 1;
717 +}
718 +
719 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
720 +{
721 + __u32 compositeSum;
722 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
723 + *sum = compositeSum;
724 + return 1;
725 +}
726 +
727 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
728 +{
729 +
730 + int chunk;
731 + int realignedChunk;
732 +
733 + yaffs_ExtendedTags tags;
734 +
735 + if(dev->checkpointCurrentBlock < 0){
736 + yaffs_CheckpointFindNextErasedBlock(dev);
737 + dev->checkpointCurrentChunk = 0;
738 + }
739 +
740 + if(dev->checkpointCurrentBlock < 0)
741 + return 0;
742 +
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
750 + checkpoint */
751 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
752 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
753 + dev->blocksInCheckpoint++;
754 + }
755 +
756 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
757 +
758 +
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));
761 +
762 + realignedChunk = chunk - dev->chunkOffset;
763 +
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;
771 + }
772 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
773 +
774 + return 1;
775 +}
776 +
777 +
778 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
779 +{
780 + int i=0;
781 + int ok = 1;
782 +
783 +
784 + __u8 * dataBytes = (__u8 *)data;
785 +
786 +
787 +
788 + if(!dev->checkpointBuffer)
789 + return 0;
790 +
791 + if(!dev->checkpointOpenForWrite)
792 + return -1;
793 +
794 + while(i < nBytes && ok) {
795 +
796 +
797 +
798 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
799 + dev->checkpointSum += *dataBytes;
800 + dev->checkpointXor ^= *dataBytes;
801 +
802 + dev->checkpointByteOffset++;
803 + i++;
804 + dataBytes++;
805 + dev->checkpointByteCount++;
806 +
807 +
808 + if(dev->checkpointByteOffset < 0 ||
809 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
810 + ok = yaffs_CheckpointFlushBuffer(dev);
811 +
812 + }
813 +
814 + return i;
815 +}
816 +
817 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
818 +{
819 + int i=0;
820 + int ok = 1;
821 + yaffs_ExtendedTags tags;
822 +
823 +
824 + int chunk;
825 + int realignedChunk;
826 +
827 + __u8 *dataBytes = (__u8 *)data;
828 +
829 + if(!dev->checkpointBuffer)
830 + return 0;
831 +
832 + if(dev->checkpointOpenForWrite)
833 + return -1;
834 +
835 + while(i < nBytes && ok) {
836 +
837 +
838 + if(dev->checkpointByteOffset < 0 ||
839 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
840 +
841 + if(dev->checkpointCurrentBlock < 0){
842 + yaffs_CheckpointFindNextCheckpointBlock(dev);
843 + dev->checkpointCurrentChunk = 0;
844 + }
845 +
846 + if(dev->checkpointCurrentBlock < 0)
847 + ok = 0;
848 + else {
849 +
850 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
851 + dev->checkpointCurrentChunk;
852 +
853 + realignedChunk = chunk - dev->chunkOffset;
854 +
855 + /* read in the next chunk */
856 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
857 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
858 + dev->checkpointBuffer,
859 + &tags);
860 +
861 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
862 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
863 + ok = 0;
864 +
865 + dev->checkpointByteOffset = 0;
866 + dev->checkpointPageSequence++;
867 + dev->checkpointCurrentChunk++;
868 +
869 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
870 + dev->checkpointCurrentBlock = -1;
871 + }
872 + }
873 +
874 + if(ok){
875 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
876 + dev->checkpointSum += *dataBytes;
877 + dev->checkpointXor ^= *dataBytes;
878 + dev->checkpointByteOffset++;
879 + i++;
880 + dataBytes++;
881 + dev->checkpointByteCount++;
882 + }
883 + }
884 +
885 + return i;
886 +}
887 +
888 +int yaffs_CheckpointClose(yaffs_Device *dev)
889 +{
890 +
891 + if(dev->checkpointOpenForWrite){
892 + if(dev->checkpointByteOffset != 0)
893 + yaffs_CheckpointFlushBuffer(dev);
894 + } else {
895 + int i;
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;
900 + else {
901 + // Todo this looks odd...
902 + }
903 + }
904 + YFREE(dev->checkpointBlockList);
905 + dev->checkpointBlockList = NULL;
906 + }
907 +
908 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
909 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
910 +
911 +
912 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
913 + dev->checkpointByteCount));
914 +
915 + if(dev->checkpointBuffer){
916 + /* free the buffer */
917 + YFREE(dev->checkpointBuffer);
918 + dev->checkpointBuffer = NULL;
919 + return 1;
920 + }
921 + else
922 + return 0;
923 +
924 +}
925 +
926 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
927 +{
928 + /* Erase the first checksum block */
929 +
930 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
931 +
932 + if(!yaffs_CheckpointSpaceOk(dev))
933 + return 0;
934 +
935 + return yaffs_CheckpointErase(dev);
936 +}
937 +
938 +
939 +
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
943 @@ -0,0 +1,35 @@
944 +/*
945 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
946 + *
947 + * Copyright (C) 2002-2007 Aleph One Ltd.
948 + * for Toby Churchill Ltd and Brightstar Engineering
949 + *
950 + * Created by Charles Manning <charles@aleph1.co.uk>
951 + *
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.
955 + *
956 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
957 + */
958 +
959 +#ifndef __YAFFS_CHECKPTRW_H__
960 +#define __YAFFS_CHECKPTRW_H__
961 +
962 +#include "yaffs_guts.h"
963 +
964 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
965 +
966 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
967 +
968 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
969 +
970 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
971 +
972 +int yaffs_CheckpointClose(yaffs_Device *dev);
973 +
974 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
975 +
976 +
977 +#endif
978 +
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
982 @@ -0,0 +1,331 @@
983 +/*
984 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
985 + *
986 + * Copyright (C) 2002-2007 Aleph One Ltd.
987 + * for Toby Churchill Ltd and Brightstar Engineering
988 + *
989 + * Created by Charles Manning <charles@aleph1.co.uk>
990 + *
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.
994 + */
995 +
996 +/*
997 + * This code implements the ECC algorithm used in SmartMedia.
998 + *
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.
1003 + *
1004 + */
1005 +
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.
1011 + */
1012 +
1013 +const char *yaffs_ecc_c_version =
1014 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1015 +
1016 +#include "yportenv.h"
1017 +
1018 +#include "yaffs_ecc.h"
1019 +
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,
1053 +};
1054 +
1055 +/* Count the bits in an unsigned char or a U32 */
1056 +
1057 +static int yaffs_CountBits(unsigned char x)
1058 +{
1059 + int r = 0;
1060 + while (x) {
1061 + if (x & 1)
1062 + r++;
1063 + x >>= 1;
1064 + }
1065 + return r;
1066 +}
1067 +
1068 +static int yaffs_CountBits32(unsigned x)
1069 +{
1070 + int r = 0;
1071 + while (x) {
1072 + if (x & 1)
1073 + r++;
1074 + x >>= 1;
1075 + }
1076 + return r;
1077 +}
1078 +
1079 +/* Calculate the ECC for a 256-byte block of data */
1080 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1081 +{
1082 + unsigned int i;
1083 +
1084 + unsigned char col_parity = 0;
1085 + unsigned char line_parity = 0;
1086 + unsigned char line_parity_prime = 0;
1087 + unsigned char t;
1088 + unsigned char b;
1089 +
1090 + for (i = 0; i < 256; i++) {
1091 + b = column_parity_table[*data++];
1092 + col_parity ^= b;
1093 +
1094 + if (b & 0x01) // odd number of bits in the byte
1095 + {
1096 + line_parity ^= i;
1097 + line_parity_prime ^= ~i;
1098 + }
1099 +
1100 + }
1101 +
1102 + ecc[2] = (~col_parity) | 0x03;
1103 +
1104 + t = 0;
1105 + if (line_parity & 0x80)
1106 + t |= 0x80;
1107 + if (line_parity_prime & 0x80)
1108 + t |= 0x40;
1109 + if (line_parity & 0x40)
1110 + t |= 0x20;
1111 + if (line_parity_prime & 0x40)
1112 + t |= 0x10;
1113 + if (line_parity & 0x20)
1114 + t |= 0x08;
1115 + if (line_parity_prime & 0x20)
1116 + t |= 0x04;
1117 + if (line_parity & 0x10)
1118 + t |= 0x02;
1119 + if (line_parity_prime & 0x10)
1120 + t |= 0x01;
1121 + ecc[1] = ~t;
1122 +
1123 + t = 0;
1124 + if (line_parity & 0x08)
1125 + t |= 0x80;
1126 + if (line_parity_prime & 0x08)
1127 + t |= 0x40;
1128 + if (line_parity & 0x04)
1129 + t |= 0x20;
1130 + if (line_parity_prime & 0x04)
1131 + t |= 0x10;
1132 + if (line_parity & 0x02)
1133 + t |= 0x08;
1134 + if (line_parity_prime & 0x02)
1135 + t |= 0x04;
1136 + if (line_parity & 0x01)
1137 + t |= 0x02;
1138 + if (line_parity_prime & 0x01)
1139 + t |= 0x01;
1140 + ecc[0] = ~t;
1141 +
1142 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1143 + // Swap the bytes into the wrong order
1144 + t = ecc[0];
1145 + ecc[0] = ecc[1];
1146 + ecc[1] = t;
1147 +#endif
1148 +}
1149 +
1150 +
1151 +/* Correct the ECC on a 256 byte block of data */
1152 +
1153 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1154 + const unsigned char *test_ecc)
1155 +{
1156 + unsigned char d0, d1, d2; /* deltas */
1157 +
1158 + d0 = read_ecc[0] ^ test_ecc[0];
1159 + d1 = read_ecc[1] ^ test_ecc[1];
1160 + d2 = read_ecc[2] ^ test_ecc[2];
1161 +
1162 + if ((d0 | d1 | d2) == 0)
1163 + return 0; /* no error */
1164 +
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 */
1169 +
1170 + unsigned byte;
1171 + unsigned bit;
1172 +
1173 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1174 + // swap the bytes to correct for the wrong order
1175 + unsigned char t;
1176 +
1177 + t = d0;
1178 + d0 = d1;
1179 + d1 = t;
1180 +#endif
1181 +
1182 + bit = byte = 0;
1183 +
1184 + if (d1 & 0x80)
1185 + byte |= 0x80;
1186 + if (d1 & 0x20)
1187 + byte |= 0x40;
1188 + if (d1 & 0x08)
1189 + byte |= 0x20;
1190 + if (d1 & 0x02)
1191 + byte |= 0x10;
1192 + if (d0 & 0x80)
1193 + byte |= 0x08;
1194 + if (d0 & 0x20)
1195 + byte |= 0x04;
1196 + if (d0 & 0x08)
1197 + byte |= 0x02;
1198 + if (d0 & 0x02)
1199 + byte |= 0x01;
1200 +
1201 + if (d2 & 0x80)
1202 + bit |= 0x04;
1203 + if (d2 & 0x20)
1204 + bit |= 0x02;
1205 + if (d2 & 0x08)
1206 + bit |= 0x01;
1207 +
1208 + data[byte] ^= (1 << bit);
1209 +
1210 + return 1; /* Corrected the error */
1211 + }
1212 +
1213 + if ((yaffs_CountBits(d0) +
1214 + yaffs_CountBits(d1) +
1215 + yaffs_CountBits(d2)) == 1) {
1216 + /* Reccoverable error in ecc */
1217 +
1218 + read_ecc[0] = test_ecc[0];
1219 + read_ecc[1] = test_ecc[1];
1220 + read_ecc[2] = test_ecc[2];
1221 +
1222 + return 1; /* Corrected the error */
1223 + }
1224 +
1225 + /* Unrecoverable error */
1226 +
1227 + return -1;
1228 +
1229 +}
1230 +
1231 +
1232 +/*
1233 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1234 + */
1235 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1236 + yaffs_ECCOther * eccOther)
1237 +{
1238 + unsigned int i;
1239 +
1240 + unsigned char col_parity = 0;
1241 + unsigned line_parity = 0;
1242 + unsigned line_parity_prime = 0;
1243 + unsigned char b;
1244 +
1245 + for (i = 0; i < nBytes; i++) {
1246 + b = column_parity_table[*data++];
1247 + col_parity ^= b;
1248 +
1249 + if (b & 0x01) {
1250 + /* odd number of bits in the byte */
1251 + line_parity ^= i;
1252 + line_parity_prime ^= ~i;
1253 + }
1254 +
1255 + }
1256 +
1257 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1258 + eccOther->lineParity = line_parity;
1259 + eccOther->lineParityPrime = line_parity_prime;
1260 +}
1261 +
1262 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1263 + yaffs_ECCOther * read_ecc,
1264 + const yaffs_ECCOther * test_ecc)
1265 +{
1266 + unsigned char cDelta; /* column parity delta */
1267 + unsigned lDelta; /* line parity delta */
1268 + unsigned lDeltaPrime; /* line parity delta */
1269 + unsigned bit;
1270 +
1271 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1272 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1273 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1274 +
1275 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1276 + return 0; /* no error */
1277 +
1278 + if (lDelta == ~lDeltaPrime &&
1279 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1280 + {
1281 + /* Single bit (recoverable) error in data */
1282 +
1283 + bit = 0;
1284 +
1285 + if (cDelta & 0x20)
1286 + bit |= 0x04;
1287 + if (cDelta & 0x08)
1288 + bit |= 0x02;
1289 + if (cDelta & 0x02)
1290 + bit |= 0x01;
1291 +
1292 + if(lDelta >= nBytes)
1293 + return -1;
1294 +
1295 + data[lDelta] ^= (1 << bit);
1296 +
1297 + return 1; /* corrected */
1298 + }
1299 +
1300 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1301 + yaffs_CountBits(cDelta)) == 1) {
1302 + /* Reccoverable error in ecc */
1303 +
1304 + *read_ecc = *test_ecc;
1305 + return 1; /* corrected */
1306 + }
1307 +
1308 + /* Unrecoverable error */
1309 +
1310 + return -1;
1311 +
1312 +}
1313 +
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
1317 @@ -0,0 +1,44 @@
1318 +/*
1319 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1320 + *
1321 + * Copyright (C) 2002-2007 Aleph One Ltd.
1322 + * for Toby Churchill Ltd and Brightstar Engineering
1323 + *
1324 + * Created by Charles Manning <charles@aleph1.co.uk>
1325 + *
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.
1329 + *
1330 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1331 + */
1332 +
1333 + /*
1334 + * This code implements the ECC algorithm used in SmartMedia.
1335 + *
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.
1340 + *
1341 + */
1342 +
1343 +#ifndef __YAFFS_ECC_H__
1344 +#define __YAFFS_ECC_H__
1345 +
1346 +typedef struct {
1347 + unsigned char colParity;
1348 + unsigned lineParity;
1349 + unsigned lineParityPrime;
1350 +} yaffs_ECCOther;
1351 +
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);
1355 +
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);
1361 +#endif
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
1365 @@ -0,0 +1,2278 @@
1366 +/*
1367 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1368 + *
1369 + * Copyright (C) 2002-2007 Aleph One Ltd.
1370 + * for Toby Churchill Ltd and Brightstar Engineering
1371 + *
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
1380 + *
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.
1384 + */
1385 +
1386 +/*
1387 + *
1388 + * This is the file system front-end to YAFFS that hooks it up to
1389 + * the VFS.
1390 + *
1391 + * Special notes:
1392 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1393 + * this superblock
1394 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1395 + * superblock
1396 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1397 + */
1398 +
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;
1402 +
1403 +#include <linux/version.h>
1404 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1405 +#include <linux/config.h>
1406 +#endif
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>
1420 +
1421 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1422 +
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)
1427 +
1428 +/* FIXME: use sb->s_id instead ? */
1429 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1430 +
1431 +#else
1432 +
1433 +#include <linux/locks.h>
1434 +#define BDEVNAME_SIZE 0
1435 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1436 +
1437 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1438 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1439 +#define __user
1440 +#endif
1441 +
1442 +#endif
1443 +
1444 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1445 +#define WRITE_SIZE_STR "writesize"
1446 +#define WRITE_SIZE(mtd) (mtd)->writesize
1447 +#else
1448 +#define WRITE_SIZE_STR "oobblock"
1449 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1450 +#endif
1451 +
1452 +#include <asm/uaccess.h>
1453 +
1454 +#include "yportenv.h"
1455 +#include "yaffs_guts.h"
1456 +
1457 +#include <linux/mtd/mtd.h>
1458 +#include "yaffs_mtdif.h"
1459 +#include "yaffs_mtdif1.h"
1460 +#include "yaffs_mtdif2.h"
1461 +
1462 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1463 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1464 +
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);
1469 +#else
1470 +MODULE_PARM(yaffs_traceMask,"i");
1471 +MODULE_PARM(yaffs_wr_attempts,"i");
1472 +#endif
1473 +
1474 +/*#define T(x) printk x */
1475 +
1476 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1477 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1478 +#else
1479 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1480 +#endif
1481 +
1482 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1483 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1484 +
1485 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1486 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1487 +#else
1488 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1489 +#endif
1490 +
1491 +static void yaffs_put_super(struct super_block *sb);
1492 +
1493 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1494 + loff_t * pos);
1495 +
1496 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1497 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1498 +#else
1499 +static int yaffs_file_flush(struct file *file);
1500 +#endif
1501 +
1502 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1503 + int datasync);
1504 +
1505 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1506 +
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);
1512 +#else
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);
1515 +#endif
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);
1522 +
1523 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1524 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1525 + dev_t dev);
1526 +#else
1527 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1528 + int dev);
1529 +#endif
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);
1533 +
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);
1537 +#else
1538 +static int yaffs_sync_fs(struct super_block *sb);
1539 +static int yaffs_write_super(struct super_block *sb);
1540 +#endif
1541 +
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);
1546 +#else
1547 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1548 +#endif
1549 +static void yaffs_read_inode(struct inode *inode);
1550 +
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 *);
1554 +
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);
1558 +#else
1559 +static int yaffs_writepage(struct page *page);
1560 +#endif
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,
1564 + unsigned to);
1565 +
1566 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1567 + int buflen);
1568 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1569 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1570 +#else
1571 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1572 +#endif
1573 +
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,
1579 +};
1580 +
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,
1587 +#else
1588 + .read = generic_file_read,
1589 + .write = generic_file_write,
1590 +#endif
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,
1596 +#endif
1597 +
1598 +};
1599 +
1600 +static struct inode_operations yaffs_file_inode_operations = {
1601 + .setattr = yaffs_setattr,
1602 +};
1603 +
1604 +static struct inode_operations yaffs_symlink_inode_operations = {
1605 + .readlink = yaffs_readlink,
1606 + .follow_link = yaffs_follow_link,
1607 + .setattr = yaffs_setattr,
1608 +};
1609 +
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,
1621 +};
1622 +
1623 +static struct file_operations yaffs_dir_operations = {
1624 + .read = generic_read_dir,
1625 + .readdir = yaffs_readdir,
1626 + .fsync = yaffs_sync_object,
1627 +};
1628 +
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,
1638 +};
1639 +
1640 +static void yaffs_GrossLock(yaffs_Device * dev)
1641 +{
1642 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1643 +
1644 + down(&dev->grossLock);
1645 +}
1646 +
1647 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1648 +{
1649 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1650 + up(&dev->grossLock);
1651 +
1652 +}
1653 +
1654 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1655 + int buflen)
1656 +{
1657 + unsigned char *alias;
1658 + int ret;
1659 +
1660 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1661 +
1662 + yaffs_GrossLock(dev);
1663 +
1664 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1665 +
1666 + yaffs_GrossUnlock(dev);
1667 +
1668 + if (!alias)
1669 + return -ENOMEM;
1670 +
1671 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1672 + kfree(alias);
1673 + return ret;
1674 +}
1675 +
1676 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1677 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1678 +#else
1679 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1680 +#endif
1681 +{
1682 + unsigned char *alias;
1683 + int ret;
1684 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1685 +
1686 + yaffs_GrossLock(dev);
1687 +
1688 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1689 +
1690 + yaffs_GrossUnlock(dev);
1691 +
1692 + if (!alias)
1693 + {
1694 + ret = -ENOMEM;
1695 + goto out;
1696 + }
1697 +
1698 + ret = vfs_follow_link(nd, alias);
1699 + kfree(alias);
1700 +out:
1701 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1702 + return ERR_PTR (ret);
1703 +#else
1704 + return ret;
1705 +#endif
1706 +}
1707 +
1708 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1709 + yaffs_Object * obj);
1710 +
1711 +/*
1712 + * Lookup is used to find objects in the fs
1713 + */
1714 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1715 +
1716 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1717 + struct nameidata *n)
1718 +#else
1719 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1720 +#endif
1721 +{
1722 + yaffs_Object *obj;
1723 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1724 +
1725 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1726 +
1727 + yaffs_GrossLock(dev);
1728 +
1729 + T(YAFFS_TRACE_OS,
1730 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1731 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1732 +
1733 + obj =
1734 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1735 + dentry->d_name.name);
1736 +
1737 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1738 +
1739 + /* Can't hold gross lock when calling yaffs_get_inode() */
1740 + yaffs_GrossUnlock(dev);
1741 +
1742 + if (obj) {
1743 + T(YAFFS_TRACE_OS,
1744 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1745 +
1746 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1747 +
1748 + if (inode) {
1749 + T(YAFFS_TRACE_OS,
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 */
1753 +#if 0
1754 + /*dget(dentry); // try to solve directory bug */
1755 + d_add(dentry, inode);
1756 +
1757 + /* return dentry; */
1758 + return NULL;
1759 +#endif
1760 + }
1761 +
1762 + } else {
1763 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1764 +
1765 + }
1766 +
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);
1770 +
1771 + return NULL;
1772 + /* return (ERR_PTR(-EIO)); */
1773 +
1774 +}
1775 +
1776 +/* For now put inode is just for debugging
1777 + * Put inode is called when the inode **structure** is put.
1778 + */
1779 +static void yaffs_put_inode(struct inode *inode)
1780 +{
1781 + T(YAFFS_TRACE_OS,
1782 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1783 + atomic_read(&inode->i_count)));
1784 +
1785 +}
1786 +
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)
1789 +{
1790 + yaffs_Object *obj;
1791 + yaffs_Device *dev;
1792 +
1793 + obj = yaffs_InodeToObject(inode);
1794 +
1795 + T(YAFFS_TRACE_OS,
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"));
1799 +
1800 + if (obj) {
1801 + dev = obj->myDev;
1802 + yaffs_GrossLock(dev);
1803 +
1804 + /* Clear the association between the inode and
1805 + * the yaffs_Object.
1806 + */
1807 + obj->myInode = NULL;
1808 + yaffs_InodeToObjectLV(inode) = NULL;
1809 +
1810 + /* If the object freeing was deferred, then the real
1811 + * free happens now.
1812 + * This should fix the inode inconsistency problem.
1813 + */
1814 +
1815 + yaffs_HandleDeferedFree(obj);
1816 +
1817 + yaffs_GrossUnlock(dev);
1818 + }
1819 +
1820 +}
1821 +
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()
1826 + */
1827 +static void yaffs_delete_inode(struct inode *inode)
1828 +{
1829 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1830 + yaffs_Device *dev;
1831 +
1832 + T(YAFFS_TRACE_OS,
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"));
1836 +
1837 + if (obj) {
1838 + dev = obj->myDev;
1839 + yaffs_GrossLock(dev);
1840 + yaffs_DeleteFile(obj);
1841 + yaffs_GrossUnlock(dev);
1842 + }
1843 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1844 + truncate_inode_pages (&inode->i_data, 0);
1845 +#endif
1846 + clear_inode(inode);
1847 +}
1848 +
1849 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1850 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1851 +#else
1852 +static int yaffs_file_flush(struct file *file)
1853 +#endif
1854 +{
1855 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1856 +
1857 + yaffs_Device *dev = obj->myDev;
1858 +
1859 + T(YAFFS_TRACE_OS,
1860 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1861 + obj->dirty ? "dirty" : "clean"));
1862 +
1863 + yaffs_GrossLock(dev);
1864 +
1865 + yaffs_FlushFile(obj, 1);
1866 +
1867 + yaffs_GrossUnlock(dev);
1868 +
1869 + return 0;
1870 +}
1871 +
1872 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1873 +{
1874 + /* Lifted from jffs2 */
1875 +
1876 + yaffs_Object *obj;
1877 + unsigned char *pg_buf;
1878 + int ret;
1879 +
1880 + yaffs_Device *dev;
1881 +
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));
1885 +
1886 + obj = yaffs_DentryToObject(f->f_dentry);
1887 +
1888 + dev = obj->myDev;
1889 +
1890 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1891 + BUG_ON(!PageLocked(pg));
1892 +#else
1893 + if (!PageLocked(pg))
1894 + PAGE_BUG(pg);
1895 +#endif
1896 +
1897 + pg_buf = kmap(pg);
1898 + /* FIXME: Can kmap fail? */
1899 +
1900 + yaffs_GrossLock(dev);
1901 +
1902 + ret =
1903 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1904 + PAGE_CACHE_SIZE);
1905 +
1906 + yaffs_GrossUnlock(dev);
1907 +
1908 + if (ret >= 0)
1909 + ret = 0;
1910 +
1911 + if (ret) {
1912 + ClearPageUptodate(pg);
1913 + SetPageError(pg);
1914 + } else {
1915 + SetPageUptodate(pg);
1916 + ClearPageError(pg);
1917 + }
1918 +
1919 + flush_dcache_page(pg);
1920 + kunmap(pg);
1921 +
1922 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1923 + return ret;
1924 +}
1925 +
1926 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1927 +{
1928 + int ret = yaffs_readpage_nolock(f, pg);
1929 + UnlockPage(pg);
1930 + return ret;
1931 +}
1932 +
1933 +static int yaffs_readpage(struct file *f, struct page *pg)
1934 +{
1935 + return yaffs_readpage_unlock(f, pg);
1936 +}
1937 +
1938 +/* writepage inspired by/stolen from smbfs */
1939 +
1940 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1941 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1942 +#else
1943 +static int yaffs_writepage(struct page *page)
1944 +#endif
1945 +{
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;
1950 + char *buffer;
1951 + yaffs_Object *obj;
1952 + int nWritten = 0;
1953 + unsigned nBytes;
1954 +
1955 + if (!mapping)
1956 + BUG();
1957 + inode = mapping->host;
1958 + if (!inode)
1959 + BUG();
1960 +
1961 + if (offset > inode->i_size) {
1962 + T(YAFFS_TRACE_OS,
1963 + (KERN_DEBUG
1964 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1965 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1966 + (unsigned)inode->i_size));
1967 + T(YAFFS_TRACE_OS,
1968 + (KERN_DEBUG " -> don't care!!\n"));
1969 + unlock_page(page);
1970 + return 0;
1971 + }
1972 +
1973 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1974 +
1975 + /* easy case */
1976 + if (page->index < end_index) {
1977 + nBytes = PAGE_CACHE_SIZE;
1978 + } else {
1979 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1980 + }
1981 +
1982 + get_page(page);
1983 +
1984 + buffer = kmap(page);
1985 +
1986 + obj = yaffs_InodeToObject(inode);
1987 + yaffs_GrossLock(obj->myDev);
1988 +
1989 + T(YAFFS_TRACE_OS,
1990 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
1991 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1992 + T(YAFFS_TRACE_OS,
1993 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
1994 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1995 +
1996 + nWritten =
1997 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
1998 + nBytes, 0);
1999 +
2000 + T(YAFFS_TRACE_OS,
2001 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2002 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2003 +
2004 + yaffs_GrossUnlock(obj->myDev);
2005 +
2006 + kunmap(page);
2007 + SetPageUptodate(page);
2008 + UnlockPage(page);
2009 + put_page(page);
2010 +
2011 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2012 +}
2013 +
2014 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2015 + unsigned offset, unsigned to)
2016 +{
2017 +
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);
2021 +
2022 + return 0;
2023 +
2024 +}
2025 +
2026 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2027 + unsigned to)
2028 +{
2029 +
2030 + void *addr = page_address(pg) + offset;
2031 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2032 + int nBytes = to - offset;
2033 + int nWritten;
2034 +
2035 + unsigned spos = pos;
2036 + unsigned saddr = (unsigned)addr;
2037 +
2038 + T(YAFFS_TRACE_OS,
2039 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2040 + spos, nBytes));
2041 +
2042 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2043 +
2044 + if (nWritten != nBytes) {
2045 + T(YAFFS_TRACE_OS,
2046 + (KERN_DEBUG
2047 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2048 + nWritten, nBytes));
2049 + SetPageError(pg);
2050 + ClearPageUptodate(pg);
2051 + } else {
2052 + SetPageUptodate(pg);
2053 + }
2054 +
2055 + T(YAFFS_TRACE_OS,
2056 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2057 + nWritten == nBytes ? 0 : nWritten));
2058 +
2059 + return nWritten == nBytes ? 0 : nWritten;
2060 +
2061 +}
2062 +
2063 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2064 +{
2065 + if (inode && obj) {
2066 +
2067 +
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;
2075 + }
2076 +
2077 + break;
2078 + case YAFFS_OBJECT_TYPE_SYMLINK :
2079 + if( ! S_ISLNK(mode) ){
2080 + obj->yst_mode &= ~S_IFMT;
2081 + obj->yst_mode |= S_IFLNK;
2082 + }
2083 +
2084 + break;
2085 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2086 + if( ! S_ISDIR(mode) ){
2087 + obj->yst_mode &= ~S_IFMT;
2088 + obj->yst_mode |= S_IFDIR;
2089 + }
2090 +
2091 + break;
2092 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2093 + case YAFFS_OBJECT_TYPE_HARDLINK :
2094 + case YAFFS_OBJECT_TYPE_SPECIAL :
2095 + default:
2096 + /* TODO? */
2097 + break;
2098 + }
2099 +
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;
2106 +#endif
2107 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2108 +
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;
2116 +#else
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;
2121 +#endif
2122 + inode->i_size = yaffs_GetObjectFileLength(obj);
2123 + inode->i_blocks = (inode->i_size + 511) >> 9;
2124 +
2125 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2126 +
2127 + T(YAFFS_TRACE_OS,
2128 + (KERN_DEBUG
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)));
2132 +
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));
2138 +#else
2139 + init_special_inode(inode, obj->yst_mode,
2140 + (dev_t) (obj->yst_rdev));
2141 +#endif
2142 + break;
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;
2148 + break;
2149 + case S_IFDIR: /* directory */
2150 + inode->i_op = &yaffs_dir_inode_operations;
2151 + inode->i_fop = &yaffs_dir_operations;
2152 + break;
2153 + case S_IFLNK: /* symlink */
2154 + inode->i_op = &yaffs_symlink_inode_operations;
2155 + break;
2156 + }
2157 +
2158 + yaffs_InodeToObjectLV(inode) = obj;
2159 +
2160 + obj->myInode = inode;
2161 +
2162 + } else {
2163 + T(YAFFS_TRACE_OS,
2164 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2165 + }
2166 +
2167 +}
2168 +
2169 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2170 + yaffs_Object * obj)
2171 +{
2172 + struct inode *inode;
2173 +
2174 + if (!sb) {
2175 + T(YAFFS_TRACE_OS,
2176 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2177 + return NULL;
2178 +
2179 + }
2180 +
2181 + if (!obj) {
2182 + T(YAFFS_TRACE_OS,
2183 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2184 + return NULL;
2185 +
2186 + }
2187 +
2188 + T(YAFFS_TRACE_OS,
2189 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2190 +
2191 + inode = iget(sb, obj->objectId);
2192 +
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! */
2196 +
2197 + return inode;
2198 +}
2199 +
2200 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2201 + loff_t * pos)
2202 +{
2203 + yaffs_Object *obj;
2204 + int nWritten, ipos;
2205 + struct inode *inode;
2206 + yaffs_Device *dev;
2207 +
2208 + obj = yaffs_DentryToObject(f->f_dentry);
2209 +
2210 + dev = obj->myDev;
2211 +
2212 + yaffs_GrossLock(dev);
2213 +
2214 + inode = f->f_dentry->d_inode;
2215 +
2216 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2217 + ipos = inode->i_size;
2218 + } else {
2219 + ipos = *pos;
2220 + }
2221 +
2222 + if (!obj) {
2223 + T(YAFFS_TRACE_OS,
2224 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2225 + } else {
2226 + T(YAFFS_TRACE_OS,
2227 + (KERN_DEBUG
2228 + "yaffs_file_write about to write writing %d bytes"
2229 + "to object %d at %d\n",
2230 + n, obj->objectId, ipos));
2231 + }
2232 +
2233 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2234 +
2235 + T(YAFFS_TRACE_OS,
2236 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2237 + n, nWritten, ipos));
2238 + if (nWritten > 0) {
2239 + ipos += nWritten;
2240 + *pos = ipos;
2241 + if (ipos > inode->i_size) {
2242 + inode->i_size = ipos;
2243 + inode->i_blocks = (ipos + 511) >> 9;
2244 +
2245 + T(YAFFS_TRACE_OS,
2246 + (KERN_DEBUG
2247 + "yaffs_file_write size updated to %d bytes, "
2248 + "%d blocks\n",
2249 + ipos, (int)(inode->i_blocks)));
2250 + }
2251 +
2252 + }
2253 + yaffs_GrossUnlock(dev);
2254 + return nWritten == 0 ? -ENOSPC : nWritten;
2255 +}
2256 +
2257 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2258 +{
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;
2264 + yaffs_Object *l;
2265 +
2266 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2267 +
2268 + obj = yaffs_DentryToObject(f->f_dentry);
2269 + dev = obj->myDev;
2270 +
2271 + yaffs_GrossLock(dev);
2272 +
2273 + offset = f->f_pos;
2274 +
2275 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2276 +
2277 + if (offset == 0) {
2278 + T(YAFFS_TRACE_OS,
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)
2282 + < 0) {
2283 + goto out;
2284 + }
2285 + offset++;
2286 + f->f_pos++;
2287 + }
2288 + if (offset == 1) {
2289 + T(YAFFS_TRACE_OS,
2290 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2291 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2292 + if (filldir
2293 + (dirent, "..", 2, offset,
2294 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2295 + goto out;
2296 + }
2297 + offset++;
2298 + f->f_pos++;
2299 + }
2300 +
2301 + curoffs = 1;
2302 +
2303 + /* If the directory has changed since the open or last call to
2304 + readdir, rewind to after the 2 canned entries. */
2305 +
2306 + if (f->f_version != inode->i_version) {
2307 + offset = 2;
2308 + f->f_pos = offset;
2309 + f->f_version = inode->i_version;
2310 + }
2311 +
2312 + list_for_each(i, &obj->variant.directoryVariant.children) {
2313 + curoffs++;
2314 + if (curoffs >= offset) {
2315 + l = list_entry(i, yaffs_Object, siblings);
2316 +
2317 + yaffs_GetObjectName(l, name,
2318 + YAFFS_MAX_NAME_LENGTH + 1);
2319 + T(YAFFS_TRACE_OS,
2320 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2321 + yaffs_GetObjectInode(l)));
2322 +
2323 + if (filldir(dirent,
2324 + name,
2325 + strlen(name),
2326 + offset,
2327 + yaffs_GetObjectInode(l),
2328 + yaffs_GetObjectType(l))
2329 + < 0) {
2330 + goto up_and_out;
2331 + }
2332 +
2333 + offset++;
2334 + f->f_pos++;
2335 + }
2336 + }
2337 +
2338 + up_and_out:
2339 + out:
2340 +
2341 + yaffs_GrossUnlock(dev);
2342 +
2343 + return 0;
2344 +}
2345 +
2346 +/*
2347 + * File creation. Allocate an inode, and we're done..
2348 + */
2349 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2350 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2351 + dev_t rdev)
2352 +#else
2353 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2354 + int rdev)
2355 +#endif
2356 +{
2357 + struct inode *inode;
2358 +
2359 + yaffs_Object *obj = NULL;
2360 + yaffs_Device *dev;
2361 +
2362 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2363 +
2364 + int error = -ENOSPC;
2365 + uid_t uid = current->fsuid;
2366 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2367 +
2368 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2369 + mode |= S_ISGID;
2370 +
2371 + if (parent) {
2372 + T(YAFFS_TRACE_OS,
2373 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2374 + parent->objectId, parent->variantType));
2375 + } else {
2376 + T(YAFFS_TRACE_OS,
2377 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2378 + return -EPERM;
2379 + }
2380 +
2381 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2382 + "mode %x dev %x\n",
2383 + dentry->d_name.name, mode, rdev));
2384 +
2385 + dev = parent->myDev;
2386 +
2387 + yaffs_GrossLock(dev);
2388 +
2389 + switch (mode & S_IFMT) {
2390 + default:
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))
2395 + obj =
2396 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2397 + gid, old_encode_dev(rdev));
2398 +#else
2399 + obj =
2400 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2401 + gid, rdev);
2402 +#endif
2403 + break;
2404 + case S_IFREG: /* file */
2405 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2406 + obj =
2407 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2408 + gid);
2409 + break;
2410 + case S_IFDIR: /* directory */
2411 + T(YAFFS_TRACE_OS,
2412 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2413 + obj =
2414 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2415 + uid, gid);
2416 + break;
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? */
2420 + break;
2421 + }
2422 +
2423 + /* Can not call yaffs_get_inode() with gross lock held */
2424 + yaffs_GrossUnlock(dev);
2425 +
2426 + if (obj) {
2427 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2428 + d_instantiate(dentry, inode);
2429 + T(YAFFS_TRACE_OS,
2430 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2431 + obj->objectId, atomic_read(&inode->i_count)));
2432 + error = 0;
2433 + } else {
2434 + T(YAFFS_TRACE_OS,
2435 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2436 + error = -ENOMEM;
2437 + }
2438 +
2439 + return error;
2440 +}
2441 +
2442 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2443 +{
2444 + int retVal;
2445 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2446 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2447 +#if 0
2448 + /* attempt to fix dir bug - didn't work */
2449 + if (!retVal) {
2450 + dget(dentry);
2451 + }
2452 +#endif
2453 + return retVal;
2454 +}
2455 +
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)
2459 +#else
2460 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2461 +#endif
2462 +{
2463 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2464 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2465 +}
2466 +
2467 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2468 +{
2469 + int retVal;
2470 +
2471 + yaffs_Device *dev;
2472 +
2473 + T(YAFFS_TRACE_OS,
2474 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2475 + dentry->d_name.name));
2476 +
2477 + dev = yaffs_InodeToObject(dir)->myDev;
2478 +
2479 + yaffs_GrossLock(dev);
2480 +
2481 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2482 +
2483 + if (retVal == YAFFS_OK) {
2484 + dentry->d_inode->i_nlink--;
2485 + dir->i_version++;
2486 + yaffs_GrossUnlock(dev);
2487 + mark_inode_dirty(dentry->d_inode);
2488 + return 0;
2489 + }
2490 + yaffs_GrossUnlock(dev);
2491 + return -ENOTEMPTY;
2492 +}
2493 +
2494 +/*
2495 + * Create a link...
2496 + */
2497 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2498 + struct dentry *dentry)
2499 +{
2500 + struct inode *inode = old_dentry->d_inode;
2501 + yaffs_Object *obj = NULL;
2502 + yaffs_Object *link = NULL;
2503 + yaffs_Device *dev;
2504 +
2505 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2506 +
2507 + obj = yaffs_InodeToObject(inode);
2508 + dev = obj->myDev;
2509 +
2510 + yaffs_GrossLock(dev);
2511 +
2512 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2513 + {
2514 + link =
2515 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2516 + obj);
2517 + }
2518 +
2519 + if (link) {
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);
2523 + T(YAFFS_TRACE_OS,
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)));
2527 +
2528 + }
2529 +
2530 + yaffs_GrossUnlock(dev);
2531 +
2532 + if (link) {
2533 +
2534 + return 0;
2535 + }
2536 +
2537 + return -EPERM;
2538 +}
2539 +
2540 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2541 + const char *symname)
2542 +{
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;
2547 +
2548 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2549 +
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);
2555 +
2556 + if (obj) {
2557 +
2558 + struct inode *inode;
2559 +
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"));
2563 + return 0;
2564 + } else {
2565 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2566 +
2567 + }
2568 +
2569 + return -ENOMEM;
2570 +}
2571 +
2572 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2573 + int datasync)
2574 +{
2575 +
2576 + yaffs_Object *obj;
2577 + yaffs_Device *dev;
2578 +
2579 + obj = yaffs_DentryToObject(dentry);
2580 +
2581 + dev = obj->myDev;
2582 +
2583 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2584 + yaffs_GrossLock(dev);
2585 + yaffs_FlushFile(obj, 1);
2586 + yaffs_GrossUnlock(dev);
2587 + return 0;
2588 +}
2589 +
2590 +/*
2591 + * The VFS layer already does all the dentry stuff for rename.
2592 + *
2593 + * NB: POSIX says you can rename an object over an old object of the same name
2594 + */
2595 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2596 + struct inode *new_dir, struct dentry *new_dentry)
2597 +{
2598 + yaffs_Device *dev;
2599 + int retVal = YAFFS_FAIL;
2600 + yaffs_Object *target;
2601 +
2602 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2603 + dev = yaffs_InodeToObject(old_dir)->myDev;
2604 +
2605 + yaffs_GrossLock(dev);
2606 +
2607 + /* Check if the target is an existing directory that is not empty. */
2608 + target =
2609 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2610 + new_dentry->d_name.name);
2611 +
2612 +
2613 +
2614 + if (target &&
2615 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2616 + !list_empty(&target->variant.directoryVariant.children)) {
2617 +
2618 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2619 +
2620 + retVal = YAFFS_FAIL;
2621 + } else {
2622 +
2623 + /* Now does unlinking internally using shadowing mechanism */
2624 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2625 +
2626 + retVal =
2627 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2628 + old_dentry->d_name.name,
2629 + yaffs_InodeToObject(new_dir),
2630 + new_dentry->d_name.name);
2631 +
2632 + }
2633 + yaffs_GrossUnlock(dev);
2634 +
2635 + if (retVal == YAFFS_OK) {
2636 + if(target) {
2637 + new_dentry->d_inode->i_nlink--;
2638 + mark_inode_dirty(new_dentry->d_inode);
2639 + }
2640 +
2641 + return 0;
2642 + } else {
2643 + return -ENOTEMPTY;
2644 + }
2645 +
2646 +}
2647 +
2648 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2649 +{
2650 + struct inode *inode = dentry->d_inode;
2651 + int error;
2652 + yaffs_Device *dev;
2653 +
2654 + T(YAFFS_TRACE_OS,
2655 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2656 + yaffs_InodeToObject(inode)->objectId));
2657 +
2658 + if ((error = inode_change_ok(inode, attr)) == 0) {
2659 +
2660 + dev = yaffs_InodeToObject(inode)->myDev;
2661 + yaffs_GrossLock(dev);
2662 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2663 + YAFFS_OK) {
2664 + error = 0;
2665 + } else {
2666 + error = -EPERM;
2667 + }
2668 + yaffs_GrossUnlock(dev);
2669 + if (!error)
2670 + error = inode_setattr(inode, attr);
2671 + }
2672 + return error;
2673 +}
2674 +
2675 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2676 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2677 +{
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)
2682 +{
2683 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2684 +#else
2685 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2686 +{
2687 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2688 +#endif
2689 +
2690 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2691 +
2692 + yaffs_GrossLock(dev);
2693 +
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) {
2698 +
2699 + buf->f_blocks =
2700 + (dev->endBlock - dev->startBlock +
2701 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2702 + dev->nDataBytesPerChunk);
2703 + buf->f_bfree =
2704 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2705 + dev->nDataBytesPerChunk);
2706 + } else {
2707 +
2708 + buf->f_blocks =
2709 + (dev->endBlock - dev->startBlock +
2710 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2711 + sb->s_blocksize);
2712 + buf->f_bfree =
2713 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2714 + sb->s_blocksize);
2715 + }
2716 + buf->f_files = 0;
2717 + buf->f_ffree = 0;
2718 + buf->f_bavail = buf->f_bfree;
2719 +
2720 + yaffs_GrossUnlock(dev);
2721 + return 0;
2722 +}
2723 +
2724 +
2725 +/**
2726 +static int yaffs_do_sync_fs(struct super_block *sb)
2727 +{
2728 +
2729 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2730 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2731 +
2732 + if(sb->s_dirt) {
2733 + yaffs_GrossLock(dev);
2734 +
2735 + if(dev)
2736 + yaffs_CheckpointSave(dev);
2737 +
2738 + yaffs_GrossUnlock(dev);
2739 +
2740 + sb->s_dirt = 0;
2741 + }
2742 + return 0;
2743 +}
2744 +**/
2745 +
2746 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2747 +static void yaffs_write_super(struct super_block *sb)
2748 +#else
2749 +static int yaffs_write_super(struct super_block *sb)
2750 +#endif
2751 +{
2752 +
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);*/
2756 +#endif
2757 +}
2758 +
2759 +
2760 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2761 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2762 +#else
2763 +static int yaffs_sync_fs(struct super_block *sb)
2764 +#endif
2765 +{
2766 +
2767 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2768 +
2769 + return 0; /* yaffs_do_sync_fs(sb);*/
2770 +
2771 +}
2772 +
2773 +
2774 +static void yaffs_read_inode(struct inode *inode)
2775 +{
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.
2779 + */
2780 +
2781 + yaffs_Object *obj;
2782 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2783 +
2784 + T(YAFFS_TRACE_OS,
2785 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2786 +
2787 + yaffs_GrossLock(dev);
2788 +
2789 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2790 +
2791 + yaffs_FillInodeFromObject(inode, obj);
2792 +
2793 + yaffs_GrossUnlock(dev);
2794 +}
2795 +
2796 +static LIST_HEAD(yaffs_dev_list);
2797 +
2798 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2799 +{
2800 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2801 +
2802 + if( *flags & MS_RDONLY ) {
2803 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2804 +
2805 + T(YAFFS_TRACE_OS,
2806 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2807 +
2808 + yaffs_GrossLock(dev);
2809 +
2810 + yaffs_FlushEntireDeviceCache(dev);
2811 +
2812 + yaffs_CheckpointSave(dev);
2813 +
2814 + if (mtd->sync)
2815 + mtd->sync(mtd);
2816 +
2817 + yaffs_GrossUnlock(dev);
2818 + }
2819 + else {
2820 + T(YAFFS_TRACE_OS,
2821 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2822 + }
2823 +
2824 + return 0;
2825 +}
2826 +
2827 +static void yaffs_put_super(struct super_block *sb)
2828 +{
2829 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2830 +
2831 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2832 +
2833 + yaffs_GrossLock(dev);
2834 +
2835 + yaffs_FlushEntireDeviceCache(dev);
2836 +
2837 + yaffs_CheckpointSave(dev);
2838 +
2839 + if (dev->putSuperFunc) {
2840 + dev->putSuperFunc(sb);
2841 + }
2842 +
2843 + yaffs_Deinitialise(dev);
2844 +
2845 + yaffs_GrossUnlock(dev);
2846 +
2847 + /* we assume this is protected by lock_kernel() in mount/umount */
2848 + list_del(&dev->devList);
2849 +
2850 + if(dev->spareBuffer){
2851 + YFREE(dev->spareBuffer);
2852 + dev->spareBuffer = NULL;
2853 + }
2854 +
2855 + kfree(dev);
2856 +}
2857 +
2858 +
2859 +static void yaffs_MTDPutSuper(struct super_block *sb)
2860 +{
2861 +
2862 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2863 +
2864 + if (mtd->sync) {
2865 + mtd->sync(mtd);
2866 + }
2867 +
2868 + put_mtd_device(mtd);
2869 +}
2870 +
2871 +
2872 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2873 +{
2874 + struct super_block *sb = (struct super_block *)vsb;
2875 +
2876 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2877 +// if(sb)
2878 +// sb->s_dirt = 1;
2879 +}
2880 +
2881 +typedef struct {
2882 + int inband_tags;
2883 + int skip_checkpoint_read;
2884 + int skip_checkpoint_write;
2885 + int no_cache;
2886 +} yaffs_options;
2887 +
2888 +#define MAX_OPT_LEN 20
2889 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2890 +{
2891 + char cur_opt[MAX_OPT_LEN+1];
2892 + int p;
2893 + int error = 0;
2894 +
2895 + /* Parse through the options which is a comma seperated list */
2896 +
2897 + while(options_str && *options_str && !error){
2898 + memset(cur_opt,0,MAX_OPT_LEN+1);
2899 + p = 0;
2900 +
2901 + while(*options_str && *options_str != ','){
2902 + if(p < MAX_OPT_LEN){
2903 + cur_opt[p] = *options_str;
2904 + p++;
2905 + }
2906 + options_str++;
2907 + }
2908 +
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;
2920 + } else {
2921 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2922 + error = 1;
2923 + }
2924 +
2925 + }
2926 +
2927 + return error;
2928 +}
2929 +
2930 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2931 + struct super_block *sb,
2932 + void *data, int silent)
2933 +{
2934 + int nBlocks;
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;
2940 + int err;
2941 + char *data_str = (char *)data;
2942 +
2943 + yaffs_options options;
2944 +
2945 + sb->s_magic = YAFFS_MAGIC;
2946 + sb->s_op = &yaffs_super_ops;
2947 +
2948 + if (!sb)
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");
2954 + else
2955 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2956 + sb->s_dev,
2957 + yaffs_devname(sb, devname_buf));
2958 +
2959 + if(!data_str)
2960 + data_str = "";
2961 +
2962 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2963 +
2964 + memset(&options,0,sizeof(options));
2965 +
2966 + if(yaffs_parse_options(&options,data_str)){
2967 + /* Option parsing failed */
2968 + return NULL;
2969 + }
2970 +
2971 +
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));
2975 + T(YAFFS_TRACE_OS,
2976 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2977 +
2978 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2979 + T(YAFFS_TRACE_OS,
2980 + ("yaffs: Write verification disabled. All guarantees "
2981 + "null and void\n"));
2982 +#endif
2983 +
2984 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2985 + "\"%s\"\n",
2986 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
2987 + yaffs_devname(sb, devname_buf)));
2988 +
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 */
2992 + }
2993 + /* Get the device */
2994 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2995 + if (!mtd) {
2996 + T(YAFFS_TRACE_ALWAYS,
2997 + ("yaffs: MTD device #%u doesn't appear to exist\n",
2998 + MINOR(sb->s_dev)));
2999 + return NULL;
3000 + }
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));
3005 + return NULL;
3006 + }
3007 +
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));
3019 +
3020 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3021 +
3022 + if (yaffsVersion == 1 &&
3023 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3024 + mtd->writesize >= 2048) {
3025 +#else
3026 + mtd->oobblock >= 2048) {
3027 +#endif
3028 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3029 + yaffsVersion = 2;
3030 + }
3031 +
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) {
3036 +#else
3037 + mtd->oobblock == 512) {
3038 +#endif
3039 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3040 + yaffsVersion = 1;
3041 + }
3042 +
3043 +#endif
3044 +
3045 + if (yaffsVersion == 2) {
3046 + /* Check for version 2 style functions */
3047 + if (!mtd->erase ||
3048 + !mtd->block_isbad ||
3049 + !mtd->block_markbad ||
3050 + !mtd->read ||
3051 + !mtd->write ||
3052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3053 + !mtd->read_oob || !mtd->write_oob) {
3054 +#else
3055 + !mtd->write_ecc ||
3056 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3057 +#endif
3058 + T(YAFFS_TRACE_ALWAYS,
3059 + ("yaffs: MTD device does not support required "
3060 + "functions\n"));;
3061 + return NULL;
3062 + }
3063 +
3064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3065 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3066 +#else
3067 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3068 +#endif
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"));
3073 + return NULL;
3074 + }
3075 + } else {
3076 + /* Check for V1 style functions */
3077 + if (!mtd->erase ||
3078 + !mtd->read ||
3079 + !mtd->write ||
3080 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3081 + !mtd->read_oob || !mtd->write_oob) {
3082 +#else
3083 + !mtd->write_ecc ||
3084 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3085 +#endif
3086 + T(YAFFS_TRACE_ALWAYS,
3087 + ("yaffs: MTD device does not support required "
3088 + "functions\n"));;
3089 + return NULL;
3090 + }
3091 +
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"));
3097 + return NULL;
3098 + }
3099 + }
3100 +
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
3104 + */
3105 +
3106 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3107 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3108 +#else
3109 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3110 +#endif
3111 + if (!dev) {
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"));
3116 + return NULL;
3117 + }
3118 +
3119 + memset(dev, 0, sizeof(yaffs_Device));
3120 + dev->genericDevice = mtd;
3121 + dev->name = mtd->name;
3122 +
3123 + /* Set up the memory size parameters.... */
3124 +
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;
3132 +
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;
3146 +#else
3147 + dev->nDataBytesPerChunk = mtd->oobblock;
3148 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3149 +#endif
3150 + nBlocks = mtd->size / mtd->erasesize;
3151 +
3152 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3153 + dev->startBlock = 0;
3154 + dev->endBlock = nBlocks - 1;
3155 + } else {
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;
3164 +#else
3165 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3166 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3167 +#endif
3168 + dev->isYaffs2 = 0;
3169 + }
3170 + /* ... and common functions */
3171 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3172 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3173 +
3174 + dev->putSuperFunc = yaffs_MTDPutSuper;
3175 +
3176 + dev->superBlock = (void *)sb;
3177 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3178 +
3179 +
3180 +#ifndef CONFIG_YAFFS_DOES_ECC
3181 + dev->useNANDECC = 1;
3182 +#endif
3183 +
3184 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3185 + dev->wideTnodesDisabled = 1;
3186 +#endif
3187 +
3188 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3189 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3190 +
3191 + /* we assume this is protected by lock_kernel() in mount/umount */
3192 + list_add_tail(&dev->devList, &yaffs_dev_list);
3193 +
3194 + init_MUTEX(&dev->grossLock);
3195 +
3196 + yaffs_GrossLock(dev);
3197 +
3198 + err = yaffs_GutsInitialise(dev);
3199 +
3200 + T(YAFFS_TRACE_OS,
3201 + ("yaffs_read_super: guts initialised %s\n",
3202 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3203 +
3204 + /* Release lock before yaffs_get_inode() */
3205 + yaffs_GrossUnlock(dev);
3206 +
3207 + /* Create root inode */
3208 + if (err == YAFFS_OK)
3209 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3210 + yaffs_Root(dev));
3211 +
3212 + if (!inode)
3213 + return NULL;
3214 +
3215 + inode->i_op = &yaffs_dir_inode_operations;
3216 + inode->i_fop = &yaffs_dir_operations;
3217 +
3218 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3219 +
3220 + root = d_alloc_root(inode);
3221 +
3222 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3223 +
3224 + if (!root) {
3225 + iput(inode);
3226 + return NULL;
3227 + }
3228 + sb->s_root = root;
3229 +
3230 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3231 + return sb;
3232 +}
3233 +
3234 +
3235 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3236 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3237 + int silent)
3238 +{
3239 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3240 +}
3241 +
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)
3246 +{
3247 +
3248 + return get_sb_bdev(fs, flags, dev_name, data,
3249 + yaffs_internal_read_super_mtd, mnt);
3250 +}
3251 +#else
3252 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3253 + int flags, const char *dev_name,
3254 + void *data)
3255 +{
3256 +
3257 + return get_sb_bdev(fs, flags, dev_name, data,
3258 + yaffs_internal_read_super_mtd);
3259 +}
3260 +#endif
3261 +
3262 +static struct file_system_type yaffs_fs_type = {
3263 + .owner = THIS_MODULE,
3264 + .name = "yaffs",
3265 + .get_sb = yaffs_read_super,
3266 + .kill_sb = kill_block_super,
3267 + .fs_flags = FS_REQUIRES_DEV,
3268 +};
3269 +#else
3270 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3271 + int silent)
3272 +{
3273 + return yaffs_internal_read_super(1, sb, data, silent);
3274 +}
3275 +
3276 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3277 + FS_REQUIRES_DEV);
3278 +#endif
3279 +
3280 +
3281 +#ifdef CONFIG_YAFFS_YAFFS2
3282 +
3283 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3284 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3285 + int silent)
3286 +{
3287 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3288 +}
3289 +
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)
3294 +{
3295 + return get_sb_bdev(fs, flags, dev_name, data,
3296 + yaffs2_internal_read_super_mtd, mnt);
3297 +}
3298 +#else
3299 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3300 + int flags, const char *dev_name,
3301 + void *data)
3302 +{
3303 +
3304 + return get_sb_bdev(fs, flags, dev_name, data,
3305 + yaffs2_internal_read_super_mtd);
3306 +}
3307 +#endif
3308 +
3309 +static struct file_system_type yaffs2_fs_type = {
3310 + .owner = THIS_MODULE,
3311 + .name = "yaffs2",
3312 + .get_sb = yaffs2_read_super,
3313 + .kill_sb = kill_block_super,
3314 + .fs_flags = FS_REQUIRES_DEV,
3315 +};
3316 +#else
3317 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3318 + void *data, int silent)
3319 +{
3320 + return yaffs_internal_read_super(2, sb, data, silent);
3321 +}
3322 +
3323 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3324 + FS_REQUIRES_DEV);
3325 +#endif
3326 +
3327 +#endif /* CONFIG_YAFFS_YAFFS2 */
3328 +
3329 +static struct proc_dir_entry *my_proc_entry;
3330 +
3331 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3332 +{
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);
3351 + buf +=
3352 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3353 + buf +=
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);
3366 + buf +=
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);
3370 +
3371 + return buf;
3372 +}
3373 +
3374 +static int yaffs_proc_read(char *page,
3375 + char **start,
3376 + off_t offset, int count, int *eof, void *data)
3377 +{
3378 + struct list_head *item;
3379 + char *buf = page;
3380 + int step = offset;
3381 + int n = 0;
3382 +
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.
3387 + */
3388 +
3389 + *(int *)start = 1;
3390 +
3391 + /* Print header first */
3392 + if (step == 0) {
3393 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3394 + "\n%s\n%s\n", yaffs_fs_c_version,
3395 + yaffs_guts_c_version);
3396 + }
3397 +
3398 + /* hold lock_kernel while traversing yaffs_dev_list */
3399 + lock_kernel();
3400 +
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);
3404 + if (n < step) {
3405 + n++;
3406 + continue;
3407 + }
3408 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3409 + buf = yaffs_dump_dev(buf, dev);
3410 + break;
3411 + }
3412 + unlock_kernel();
3413 +
3414 + return buf - page < count ? buf - page : count;
3415 +}
3416 +
3417 +/**
3418 + * Set the verbosity of the warnings and error messages.
3419 + *
3420 + * Note that the names can only be a..z or _ with the current code.
3421 + */
3422 +
3423 +static struct {
3424 + char *mask_name;
3425 + unsigned mask_bitfield;
3426 +} mask_flags[] = {
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},
3444 +
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},
3449 +
3450 + {"write", YAFFS_TRACE_WRITE},
3451 + {"all", 0xffffffff},
3452 + {"none", 0},
3453 + {NULL, 0},
3454 +};
3455 +
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)
3459 +{
3460 + unsigned rg = 0, mask_bitfield;
3461 + char *end;
3462 + char *mask_name;
3463 + char *x;
3464 + char substring[MAX_MASK_NAME_LENGTH+1];
3465 + int i;
3466 + int done = 0;
3467 + int add, len = 0;
3468 + int pos = 0;
3469 +
3470 + rg = yaffs_traceMask;
3471 +
3472 + while (!done && (pos < count)) {
3473 + done = 1;
3474 + while ((pos < count) && isspace(buf[pos])) {
3475 + pos++;
3476 + }
3477 +
3478 + switch (buf[pos]) {
3479 + case '+':
3480 + case '-':
3481 + case '=':
3482 + add = buf[pos];
3483 + pos++;
3484 + break;
3485 +
3486 + default:
3487 + add = ' ';
3488 + break;
3489 + }
3490 + mask_name = NULL;
3491 +
3492 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3493 + if (end > buf + pos) {
3494 + mask_name = "numeral";
3495 + len = end - (buf + pos);
3496 + done = 0;
3497 + } else {
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';
3503 +
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;
3510 + done = 0;
3511 + break;
3512 + }
3513 + }
3514 + }
3515 +
3516 + if (mask_name != NULL) {
3517 + // pos += len;
3518 + done = 0;
3519 + switch(add) {
3520 + case '-':
3521 + rg &= ~mask_bitfield;
3522 + break;
3523 + case '+':
3524 + rg |= mask_bitfield;
3525 + break;
3526 + case '=':
3527 + rg = mask_bitfield;
3528 + break;
3529 + default:
3530 + rg |= mask_bitfield;
3531 + break;
3532 + }
3533 + }
3534 + }
3535 +
3536 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3537 +
3538 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3539 +
3540 + if (rg & YAFFS_TRACE_ALWAYS) {
3541 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3542 + char flag;
3543 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3544 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3545 + }
3546 + }
3547 +
3548 + return count;
3549 +}
3550 +
3551 +/* Stuff to handle installation of file systems */
3552 +struct file_system_to_install {
3553 + struct file_system_type *fst;
3554 + int installed;
3555 +};
3556 +
3557 +static struct file_system_to_install fs_to_install[] = {
3558 +//#ifdef CONFIG_YAFFS_YAFFS1
3559 + {&yaffs_fs_type, 0},
3560 +//#endif
3561 +//#ifdef CONFIG_YAFFS_YAFFS2
3562 + {&yaffs2_fs_type, 0},
3563 +//#endif
3564 + {NULL, 0}
3565 +};
3566 +
3567 +static int __init init_yaffs_fs(void)
3568 +{
3569 + int error = 0;
3570 + struct file_system_to_install *fsinst;
3571 +
3572 + T(YAFFS_TRACE_ALWAYS,
3573 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3574 +
3575 + /* Install the proc_fs entry */
3576 + my_proc_entry = create_proc_entry("yaffs",
3577 + S_IRUGO | S_IFREG,
3578 + &proc_root);
3579 +
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;
3584 + } else {
3585 + return -ENOMEM;
3586 + }
3587 +
3588 + /* Now add the file system entries */
3589 +
3590 + fsinst = fs_to_install;
3591 +
3592 + while (fsinst->fst && !error) {
3593 + error = register_filesystem(fsinst->fst);
3594 + if (!error) {
3595 + fsinst->installed = 1;
3596 + }
3597 + fsinst++;
3598 + }
3599 +
3600 + /* Any errors? uninstall */
3601 + if (error) {
3602 + fsinst = fs_to_install;
3603 +
3604 + while (fsinst->fst) {
3605 + if (fsinst->installed) {
3606 + unregister_filesystem(fsinst->fst);
3607 + fsinst->installed = 0;
3608 + }
3609 + fsinst++;
3610 + }
3611 + }
3612 +
3613 + return error;
3614 +}
3615 +
3616 +static void __exit exit_yaffs_fs(void)
3617 +{
3618 +
3619 + struct file_system_to_install *fsinst;
3620 +
3621 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3622 + " removing. \n"));
3623 +
3624 + remove_proc_entry("yaffs", &proc_root);
3625 +
3626 + fsinst = fs_to_install;
3627 +
3628 + while (fsinst->fst) {
3629 + if (fsinst->installed) {
3630 + unregister_filesystem(fsinst->fst);
3631 + fsinst->installed = 0;
3632 + }
3633 + fsinst++;
3634 + }
3635 +
3636 +}
3637 +
3638 +module_init(init_yaffs_fs)
3639 +module_exit(exit_yaffs_fs)
3640 +
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
3647 @@ -0,0 +1,7469 @@
3648 +/*
3649 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3650 + *
3651 + * Copyright (C) 2002-2007 Aleph One Ltd.
3652 + * for Toby Churchill Ltd and Brightstar Engineering
3653 + *
3654 + * Created by Charles Manning <charles@aleph1.co.uk>
3655 + *
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.
3659 + */
3660 +
3661 +const char *yaffs_guts_c_version =
3662 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3663 +
3664 +#include "yportenv.h"
3665 +
3666 +#include "yaffsinterface.h"
3667 +#include "yaffs_guts.h"
3668 +#include "yaffs_tagsvalidity.h"
3669 +
3670 +#include "yaffs_tagscompat.h"
3671 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3672 +#include "yaffs_qsort.h"
3673 +#endif
3674 +#include "yaffs_nand.h"
3675 +
3676 +#include "yaffs_checkptrw.h"
3677 +
3678 +#include "yaffs_nand.h"
3679 +#include "yaffs_packedtags2.h"
3680 +
3681 +
3682 +#ifdef CONFIG_YAFFS_WINCE
3683 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3684 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3685 +#endif
3686 +
3687 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3688 +
3689 +#include "yaffs_ecc.h"
3690 +
3691 +
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);
3700 +
3701 +/* Other local prototypes */
3702 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3703 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3704 +
3705 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3706 +
3707 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3708 + const __u8 * buffer,
3709 + yaffs_ExtendedTags * tags,
3710 + int useReserve);
3711 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3712 + int chunkInNAND, int inScan);
3713 +
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);
3725 +
3726 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3727 +
3728 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3729 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3730 + int lineNo);
3731 +
3732 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3733 + int chunkInNAND);
3734 +
3735 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3736 +static void yaffs_DestroyObject(yaffs_Object * obj);
3737 +
3738 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3739 + int chunkInObject);
3740 +
3741 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3742 +
3743 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3744 +
3745 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3746 +
3747 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3748 +
3749 +#ifdef YAFFS_PARANOID
3750 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3751 +#else
3752 +#define yaffs_CheckFileSanity(in)
3753 +#endif
3754 +
3755 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3756 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3757 +
3758 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3759 +
3760 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3761 + yaffs_ExtendedTags * tags);
3762 +
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,
3766 + __u32 chunkId);
3767 +
3768 +
3769 +/* Function to calculate chunk and offset */
3770 +
3771 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3772 +{
3773 + if(dev->chunkShift){
3774 + /* Easy-peasy power of 2 case */
3775 + *chunk = (__u32)(addr >> dev->chunkShift);
3776 + *offset = (__u32)(addr & dev->chunkMask);
3777 + }
3778 + else if(dev->crumbsPerChunk)
3779 + {
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);
3785 + }
3786 + else
3787 + YBUG();
3788 +}
3789 +
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.
3794 + */
3795 +
3796 +static __u32 ShiftsGE(__u32 x)
3797 +{
3798 + int extraBits;
3799 + int nShifts;
3800 +
3801 + nShifts = extraBits = 0;
3802 +
3803 + while(x>1){
3804 + if(x & 1) extraBits++;
3805 + x>>=1;
3806 + nShifts++;
3807 + }
3808 +
3809 + if(extraBits)
3810 + nShifts++;
3811 +
3812 + return nShifts;
3813 +}
3814 +
3815 +/* Function to return the number of shifts to get a 1 in bit 0
3816 + */
3817 +
3818 +static __u32 ShiftDiv(__u32 x)
3819 +{
3820 + int nShifts;
3821 +
3822 + nShifts = 0;
3823 +
3824 + if(!x) return 0;
3825 +
3826 + while( !(x&1)){
3827 + x>>=1;
3828 + nShifts++;
3829 + }
3830 +
3831 + return nShifts;
3832 +}
3833 +
3834 +
3835 +
3836 +/*
3837 + * Temporary buffer manipulations.
3838 + */
3839 +
3840 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3841 +{
3842 + int i;
3843 + __u8 *buf = (__u8 *)1;
3844 +
3845 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3846 +
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);
3851 + }
3852 +
3853 + return buf ? YAFFS_OK : YAFFS_FAIL;
3854 +
3855 +}
3856 +
3857 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3858 +{
3859 + int i, j;
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;
3868 + }
3869 +
3870 + return dev->tempBuffer[i].buffer;
3871 + }
3872 + }
3873 +
3874 + T(YAFFS_TRACE_BUFFERS,
3875 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3876 + lineNo));
3877 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3878 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3879 + }
3880 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3881 +
3882 + /*
3883 + * If we got here then we have to allocate an unmanaged one
3884 + * This is not good.
3885 + */
3886 +
3887 + dev->unmanagedTempAllocations++;
3888 + return YMALLOC(dev->nDataBytesPerChunk);
3889 +
3890 +}
3891 +
3892 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3893 + int lineNo)
3894 +{
3895 + int i;
3896 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3897 + if (dev->tempBuffer[i].buffer == buffer) {
3898 + dev->tempBuffer[i].line = 0;
3899 + return;
3900 + }
3901 + }
3902 +
3903 + if (buffer) {
3904 + /* assume it is an unmanaged one. */
3905 + T(YAFFS_TRACE_BUFFERS,
3906 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3907 + lineNo));
3908 + YFREE(buffer);
3909 + dev->unmanagedTempDeallocations++;
3910 + }
3911 +
3912 +}
3913 +
3914 +/*
3915 + * Determine if we have a managed buffer.
3916 + */
3917 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3918 +{
3919 + int i;
3920 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3921 + if (dev->tempBuffer[i].buffer == buffer)
3922 + return 1;
3923 +
3924 + }
3925 +
3926 + for (i = 0; i < dev->nShortOpCaches; i++) {
3927 + if( dev->srCache[i].data == buffer )
3928 + return 1;
3929 +
3930 + }
3931 +
3932 + if (buffer == dev->checkpointBuffer)
3933 + return 1;
3934 +
3935 + T(YAFFS_TRACE_ALWAYS,
3936 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3937 + return 0;
3938 +}
3939 +
3940 +
3941 +
3942 +/*
3943 + * Chunk bitmap manipulations
3944 + */
3945 +
3946 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3947 +{
3948 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3949 + T(YAFFS_TRACE_ERROR,
3950 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3951 + blk));
3952 + YBUG();
3953 + }
3954 + return dev->chunkBits +
3955 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3956 +}
3957 +
3958 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3959 +{
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));
3964 + YBUG();
3965 + }
3966 +}
3967 +
3968 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3969 +{
3970 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3971 +
3972 + memset(blkBits, 0, dev->chunkBitmapStride);
3973 +}
3974 +
3975 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3976 +{
3977 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3978 +
3979 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3980 +
3981 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
3982 +}
3983 +
3984 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
3985 +{
3986 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3987 +
3988 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3989 +
3990 + blkBits[chunk / 8] |= (1 << (chunk & 7));
3991 +}
3992 +
3993 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
3994 +{
3995 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3996 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3997 +
3998 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
3999 +}
4000 +
4001 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4002 +{
4003 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4004 + int i;
4005 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4006 + if (*blkBits)
4007 + return 1;
4008 + blkBits++;
4009 + }
4010 + return 0;
4011 +}
4012 +
4013 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4014 +{
4015 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4016 + int i;
4017 + int n = 0;
4018 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4019 + __u8 x = *blkBits;
4020 + while(x){
4021 + if(x & 1)
4022 + n++;
4023 + x >>=1;
4024 + }
4025 +
4026 + blkBits++;
4027 + }
4028 + return n;
4029 +}
4030 +
4031 +/*
4032 + * Verification code
4033 + */
4034 +
4035 +static int yaffs_SkipVerification(yaffs_Device *dev)
4036 +{
4037 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4038 +}
4039 +
4040 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4041 +{
4042 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4043 +}
4044 +
4045 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4046 +{
4047 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4048 +}
4049 +
4050 +static const char * blockStateName[] = {
4051 +"Unknown",
4052 +"Needs scanning",
4053 +"Scanning",
4054 +"Empty",
4055 +"Allocating",
4056 +"Full",
4057 +"Dirty",
4058 +"Checkpoint",
4059 +"Collecting",
4060 +"Dead"
4061 +};
4062 +
4063 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4064 +{
4065 + int actuallyUsed;
4066 + int inUse;
4067 +
4068 + if(yaffs_SkipVerification(dev))
4069 + return;
4070 +
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));
4074 +
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]));
4081 + }
4082 +
4083 + /* Check pages in use and soft deletions are legal */
4084 +
4085 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4086 +
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));
4092 +
4093 +
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));
4099 +
4100 + /* Check that the sequence number is valid.
4101 + * Ten million is legal, but is very unlikely
4102 + */
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));
4108 +
4109 +}
4110 +
4111 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4112 +{
4113 + yaffs_VerifyBlock(dev,bi,n);
4114 +
4115 + /* After collection the block should be in the erased state */
4116 + /* TODO: This will need to change if we do partial gc */
4117 +
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));
4121 + }
4122 +}
4123 +
4124 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4125 +{
4126 + int i;
4127 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4128 + int nIllegalBlockStates = 0;
4129 +
4130 +
4131 + if(yaffs_SkipVerification(dev))
4132 + return;
4133 +
4134 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4135 +
4136 +
4137 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4138 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4139 + yaffs_VerifyBlock(dev,bi,i);
4140 +
4141 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4142 + nBlocksPerState[bi->blockState]++;
4143 + else
4144 + nIllegalBlockStates++;
4145 +
4146 + }
4147 +
4148 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4149 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4150 +
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)));
4154 +
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]));
4159 +
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]));
4164 +
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]));
4169 +
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]));
4174 +
4175 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4176 +
4177 +}
4178 +
4179 +/*
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.
4182 + */
4183 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4184 +{
4185 + if(yaffs_SkipVerification(obj->myDev))
4186 + return;
4187 +
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));
4192 + return;
4193 + }
4194 +
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));
4200 +
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));
4205 +
4206 +
4207 + /*
4208 + * Check that the object's parent ids match if parentCheck requested.
4209 + *
4210 + * Tests do not apply to the root object.
4211 + */
4212 +
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));
4217 +
4218 +
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));
4226 +
4227 +
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),
4231 + obj->objectId));
4232 +
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),
4236 + obj->objectId));
4237 +}
4238 +
4239 +
4240 +
4241 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4242 + __u32 level, int chunkOffset)
4243 +{
4244 + int i;
4245 + yaffs_Device *dev = obj->myDev;
4246 + int ok = 1;
4247 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4248 +
4249 + if (tn) {
4250 + if (level > 0) {
4251 +
4252 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4253 + if (tn->internal[i]) {
4254 + ok = yaffs_VerifyTnodeWorker(obj,
4255 + tn->internal[i],
4256 + level - 1,
4257 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4258 + }
4259 + }
4260 + } else if (level == 0) {
4261 + int i;
4262 + yaffs_ExtendedTags tags;
4263 + __u32 objectId = obj->objectId;
4264 +
4265 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4266 +
4267 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4268 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4269 +
4270 + if(theChunk > 0){
4271 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4272 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk