Rename 'refresh' to 'update' and make the new 'refresh' actually refresh all the...
[openwrt.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,NULL, &tags);
4273 + if(tags.objectId != objectId || tags.chunkId != chunkOffset){
4274 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4275 + objectId, chunkOffset, theChunk,
4276 + tags.objectId, tags.chunkId));
4277 + }
4278 + }
4279 + chunkOffset++;
4280 + }
4281 + }
4282 + }
4283 +
4284 + return ok;
4285 +
4286 +}
4287 +
4288 +
4289 +static void yaffs_VerifyFile(yaffs_Object *obj)
4290 +{
4291 + int requiredTallness;
4292 + int actualTallness;
4293 + __u32 lastChunk;
4294 + __u32 x;
4295 + __u32 i;
4296 + int ok;
4297 + yaffs_Device *dev;
4298 + yaffs_ExtendedTags tags;
4299 + yaffs_Tnode *tn;
4300 + __u32 objectId;
4301 +
4302 + if(obj && yaffs_SkipVerification(obj->myDev))
4303 + return;
4304 +
4305 + dev = obj->myDev;
4306 + objectId = obj->objectId;
4307 +
4308 + /* Check file size is consistent with tnode depth */
4309 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4310 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4311 + requiredTallness = 0;
4312 + while (x> 0) {
4313 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4314 + requiredTallness++;
4315 + }
4316 +
4317 + actualTallness = obj->variant.fileVariant.topLevel;
4318 +
4319 + if(requiredTallness > actualTallness )
4320 + T(YAFFS_TRACE_VERIFY,
4321 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4322 + obj->objectId,actualTallness, requiredTallness));
4323 +
4324 +
4325 + /* Check that the chunks in the tnode tree are all correct.
4326 + * We do this by scanning through the tnode tree and
4327 + * checking the tags for every chunk match.
4328 + */
4329 +
4330 + if(yaffs_SkipNANDVerification(dev))
4331 + return;
4332 +
4333 + for(i = 1; i <= lastChunk; i++){
4334 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
4335 +
4336 + if (tn) {
4337 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4338 + if(theChunk > 0){
4339 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4340 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4341 + if(tags.objectId != objectId || tags.chunkId != i){
4342 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4343 + objectId, i, theChunk,
4344 + tags.objectId, tags.chunkId));
4345 + }
4346 + }
4347 + }
4348 +
4349 + }
4350 +
4351 +}
4352 +
4353 +static void yaffs_VerifyDirectory(yaffs_Object *obj)
4354 +{
4355 + if(obj && yaffs_SkipVerification(obj->myDev))
4356 + return;
4357 +
4358 +}
4359 +
4360 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4361 +{
4362 + if(obj && yaffs_SkipVerification(obj->myDev))
4363 + return;
4364 +
4365 + /* Verify sane equivalent object */
4366 +}
4367 +
4368 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4369 +{
4370 + if(obj && yaffs_SkipVerification(obj->myDev))
4371 + return;
4372 +
4373 + /* Verify symlink string */
4374 +}
4375 +
4376 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4377 +{
4378 + if(obj && yaffs_SkipVerification(obj->myDev))
4379 + return;
4380 +}
4381 +
4382 +static void yaffs_VerifyObject(yaffs_Object *obj)
4383 +{
4384 + yaffs_Device *dev;
4385 +
4386 + __u32 chunkMin;
4387 + __u32 chunkMax;
4388 +
4389 + __u32 chunkIdOk;
4390 + __u32 chunkIsLive;
4391 +
4392 + if(!obj)
4393 + return;
4394 +
4395 + dev = obj->myDev;
4396 +
4397 + if(yaffs_SkipVerification(dev))
4398 + return;
4399 +
4400 + /* Check sane object header chunk */
4401 +
4402 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4403 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4404 +
4405 + chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
4406 + chunkIsLive = chunkIdOk &&
4407 + yaffs_CheckChunkBit(dev,
4408 + obj->chunkId / dev->nChunksPerBlock,
4409 + obj->chunkId % dev->nChunksPerBlock);
4410 + if(!obj->fake &&
4411 + (!chunkIdOk || !chunkIsLive)) {
4412 + T(YAFFS_TRACE_VERIFY,
4413 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4414 + obj->objectId,obj->chunkId,
4415 + chunkIdOk ? "" : ",out of range",
4416 + chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
4417 + }
4418 +
4419 + if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
4420 + yaffs_ExtendedTags tags;
4421 + yaffs_ObjectHeader *oh;
4422 + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
4423 +
4424 + oh = (yaffs_ObjectHeader *)buffer;
4425 +
4426 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
4427 +
4428 + yaffs_VerifyObjectHeader(obj,oh,&tags,1);
4429 +
4430 + yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
4431 + }
4432 +
4433 + /* Verify it has a parent */
4434 + if(obj && !obj->fake &&
4435 + (!obj->parent || obj->parent->myDev != dev)){
4436 + T(YAFFS_TRACE_VERIFY,
4437 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4438 + obj->objectId,obj->parent));
4439 + }
4440 +
4441 + /* Verify parent is a directory */
4442 + if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
4443 + T(YAFFS_TRACE_VERIFY,
4444 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4445 + obj->objectId,obj->parent->variantType));
4446 + }
4447 +
4448 + switch(obj->variantType){
4449 + case YAFFS_OBJECT_TYPE_FILE:
4450 + yaffs_VerifyFile(obj);
4451 + break;
4452 + case YAFFS_OBJECT_TYPE_SYMLINK:
4453 + yaffs_VerifySymlink(obj);
4454 + break;
4455 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4456 + yaffs_VerifyDirectory(obj);
4457 + break;
4458 + case YAFFS_OBJECT_TYPE_HARDLINK:
4459 + yaffs_VerifyHardLink(obj);
4460 + break;
4461 + case YAFFS_OBJECT_TYPE_SPECIAL:
4462 + yaffs_VerifySpecial(obj);
4463 + break;
4464 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4465 + default:
4466 + T(YAFFS_TRACE_VERIFY,
4467 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4468 + obj->objectId,obj->variantType));
4469 + break;
4470 + }
4471 +
4472 +
4473 +}
4474 +
4475 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4476 +{
4477 + yaffs_Object *obj;
4478 + int i;
4479 + struct list_head *lh;
4480 +
4481 + if(yaffs_SkipVerification(dev))
4482 + return;
4483 +
4484 + /* Iterate through the objects in each hash entry */
4485 +
4486 + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
4487 + list_for_each(lh, &dev->objectBucket[i].list) {
4488 + if (lh) {
4489 + obj = list_entry(lh, yaffs_Object, hashLink);
4490 + yaffs_VerifyObject(obj);
4491 + }
4492 + }
4493 + }
4494 +
4495 +}
4496 +
4497 +
4498 +/*
4499 + * Simple hash function. Needs to have a reasonable spread
4500 + */
4501 +
4502 +static Y_INLINE int yaffs_HashFunction(int n)
4503 +{
4504 + n = abs(n);
4505 + return (n % YAFFS_NOBJECT_BUCKETS);
4506 +}
4507 +
4508 +/*
4509 + * Access functions to useful fake objects
4510 + */
4511 +
4512 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
4513 +{
4514 + return dev->rootDir;
4515 +}
4516 +
4517 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
4518 +{
4519 + return dev->lostNFoundDir;
4520 +}
4521 +
4522 +
4523 +/*
4524 + * Erased NAND checking functions
4525 + */
4526 +
4527 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
4528 +{
4529 + /* Horrible, slow implementation */
4530 + while (nBytes--) {
4531 + if (*buffer != 0xFF)
4532 + return 0;
4533 + buffer++;
4534 + }
4535 + return 1;
4536 +}
4537 +
4538 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4539 + int chunkInNAND)
4540 +{
4541 +
4542 + int retval = YAFFS_OK;
4543 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4544 + yaffs_ExtendedTags tags;
4545 + int result;
4546 +
4547 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4548 +
4549 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4550 + retval = YAFFS_FAIL;
4551 +
4552 +
4553 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4554 + T(YAFFS_TRACE_NANDACCESS,
4555 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4556 + retval = YAFFS_FAIL;
4557 + }
4558 +
4559 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4560 +
4561 + return retval;
4562 +
4563 +}
4564 +
4565 +
4566 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4567 + const __u8 * data,
4568 + yaffs_ExtendedTags * tags,
4569 + int useReserve)
4570 +{
4571 + int attempts = 0;
4572 + int writeOk = 0;
4573 + int chunk;
4574 +
4575 + yaffs_InvalidateCheckpoint(dev);
4576 +
4577 + do {
4578 + yaffs_BlockInfo *bi = 0;
4579 + int erasedOk = 0;
4580 +
4581 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4582 + if (chunk < 0) {
4583 + /* no space */
4584 + break;
4585 + }
4586 +
4587 + /* First check this chunk is erased, if it needs
4588 + * checking. The checking policy (unless forced
4589 + * always on) is as follows:
4590 + *
4591 + * Check the first page we try to write in a block.
4592 + * If the check passes then we don't need to check any
4593 + * more. If the check fails, we check again...
4594 + * If the block has been erased, we don't need to check.
4595 + *
4596 + * However, if the block has been prioritised for gc,
4597 + * then we think there might be something odd about
4598 + * this block and stop using it.
4599 + *
4600 + * Rationale: We should only ever see chunks that have
4601 + * not been erased if there was a partially written
4602 + * chunk due to power loss. This checking policy should
4603 + * catch that case with very few checks and thus save a
4604 + * lot of checks that are most likely not needed.
4605 + */
4606 + if (bi->gcPrioritise) {
4607 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4608 + /* try another chunk */
4609 + continue;
4610 + }
4611 +
4612 + /* let's give it a try */
4613 + attempts++;
4614 +
4615 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4616 + bi->skipErasedCheck = 0;
4617 +#endif
4618 + if (!bi->skipErasedCheck) {
4619 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4620 + if (erasedOk != YAFFS_OK) {
4621 + T(YAFFS_TRACE_ERROR,
4622 + (TSTR ("**>> yaffs chunk %d was not erased"
4623 + TENDSTR), chunk));
4624 +
4625 + /* try another chunk */
4626 + continue;
4627 + }
4628 + bi->skipErasedCheck = 1;
4629 + }
4630 +
4631 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4632 + data, tags);
4633 + if (writeOk != YAFFS_OK) {
4634 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4635 + /* try another chunk */
4636 + continue;
4637 + }
4638 +
4639 + /* Copy the data into the robustification buffer */
4640 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4641 +
4642 + } while (writeOk != YAFFS_OK && attempts < yaffs_wr_attempts);
4643 +
4644 + if (attempts > 1) {
4645 + T(YAFFS_TRACE_ERROR,
4646 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4647 + attempts));
4648 +
4649 + dev->nRetriedWrites += (attempts - 1);
4650 + }
4651 +
4652 + return chunk;
4653 +}
4654 +
4655 +/*
4656 + * Block retiring for handling a broken block.
4657 + */
4658 +
4659 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
4660 +{
4661 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4662 +
4663 + yaffs_InvalidateCheckpoint(dev);
4664 +
4665 + yaffs_MarkBlockBad(dev, blockInNAND);
4666 +
4667 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
4668 + bi->gcPrioritise = 0;
4669 + bi->needsRetiring = 0;
4670 +
4671 + dev->nRetiredBlocks++;
4672 +}
4673 +
4674 +/*
4675 + * Functions for robustisizing TODO
4676 + *
4677 + */
4678 +
4679 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
4680 + const __u8 * data,
4681 + const yaffs_ExtendedTags * tags)
4682 +{
4683 +}
4684 +
4685 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
4686 + const yaffs_ExtendedTags * tags)
4687 +{
4688 +}
4689 +
4690 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
4691 +{
4692 + if(!bi->gcPrioritise){
4693 + bi->gcPrioritise = 1;
4694 + dev->hasPendingPrioritisedGCs = 1;
4695 + bi->chunkErrorStrikes ++;
4696 +
4697 + if(bi->chunkErrorStrikes > 3){
4698 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
4699 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
4700 +
4701 + }
4702 +
4703 + }
4704 +}
4705 +
4706 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
4707 +{
4708 +
4709 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
4710 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4711 +
4712 + yaffs_HandleChunkError(dev,bi);
4713 +
4714 +
4715 + if(erasedOk ) {
4716 + /* Was an actual write failure, so mark the block for retirement */
4717 + bi->needsRetiring = 1;
4718 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4719 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
4720 +
4721 +
4722 + }
4723 +
4724 + /* Delete the chunk */
4725 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
4726 +}
4727 +
4728 +
4729 +/*---------------- Name handling functions ------------*/
4730 +
4731 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
4732 +{
4733 + __u16 sum = 0;
4734 + __u16 i = 1;
4735 +
4736 + YUCHAR *bname = (YUCHAR *) name;
4737 + if (bname) {
4738 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
4739 +
4740 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4741 + sum += yaffs_toupper(*bname) * i;
4742 +#else
4743 + sum += (*bname) * i;
4744 +#endif
4745 + i++;
4746 + bname++;
4747 + }
4748 + }
4749 + return sum;
4750 +}
4751 +
4752 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4753 +{
4754 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4755 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4756 + yaffs_strcpy(obj->shortName, name);
4757 + } else {
4758 + obj->shortName[0] = _Y('\0');
4759 + }
4760 +#endif
4761 + obj->sum = yaffs_CalcNameSum(name);
4762 +}
4763 +
4764 +/*-------------------- TNODES -------------------
4765 +
4766 + * List of spare tnodes
4767 + * The list is hooked together using the first pointer
4768 + * in the tnode.
4769 + */
4770 +
4771 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4772 + * adds them to the tnode free list.
4773 + * Don't use this function directly
4774 + */
4775 +
4776 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4777 +{
4778 + int i;
4779 + int tnodeSize;
4780 + yaffs_Tnode *newTnodes;
4781 + __u8 *mem;
4782 + yaffs_Tnode *curr;
4783 + yaffs_Tnode *next;
4784 + yaffs_TnodeList *tnl;
4785 +
4786 + if (nTnodes < 1)
4787 + return YAFFS_OK;
4788 +
4789 + /* Calculate the tnode size in bytes for variable width tnode support.
4790 + * Must be a multiple of 32-bits */
4791 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4792 +
4793 + /* make these things */
4794 +
4795 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4796 + mem = (__u8 *)newTnodes;
4797 +
4798 + if (!newTnodes) {
4799 + T(YAFFS_TRACE_ERROR,
4800 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4801 + return YAFFS_FAIL;
4802 + }
4803 +
4804 + /* Hook them into the free list */
4805 +#if 0
4806 + for (i = 0; i < nTnodes - 1; i++) {
4807 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4808 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4809 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4810 +#endif
4811 + }
4812 +
4813 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4814 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4815 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4816 +#endif
4817 + dev->freeTnodes = newTnodes;
4818 +#else
4819 + /* New hookup for wide tnodes */
4820 + for(i = 0; i < nTnodes -1; i++) {
4821 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4822 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4823 + curr->internal[0] = next;
4824 + }
4825 +
4826 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4827 + curr->internal[0] = dev->freeTnodes;
4828 + dev->freeTnodes = (yaffs_Tnode *)mem;
4829 +
4830 +#endif
4831 +
4832 +
4833 + dev->nFreeTnodes += nTnodes;
4834 + dev->nTnodesCreated += nTnodes;
4835 +
4836 + /* Now add this bunch of tnodes to a list for freeing up.
4837 + * NB If we can't add this to the management list it isn't fatal
4838 + * but it just means we can't free this bunch of tnodes later.
4839 + */
4840 +
4841 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4842 + if (!tnl) {
4843 + T(YAFFS_TRACE_ERROR,
4844 + (TSTR
4845 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4846 + return YAFFS_FAIL;
4847 +
4848 + } else {
4849 + tnl->tnodes = newTnodes;
4850 + tnl->next = dev->allocatedTnodeList;
4851 + dev->allocatedTnodeList = tnl;
4852 + }
4853 +
4854 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4855 +
4856 + return YAFFS_OK;
4857 +}
4858 +
4859 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4860 +
4861 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4862 +{
4863 + yaffs_Tnode *tn = NULL;
4864 +
4865 + /* If there are none left make more */
4866 + if (!dev->freeTnodes) {
4867 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4868 + }
4869 +
4870 + if (dev->freeTnodes) {
4871 + tn = dev->freeTnodes;
4872 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4873 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4874 + /* Hoosterman, this thing looks like it isn't in the list */
4875 + T(YAFFS_TRACE_ALWAYS,
4876 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4877 + }
4878 +#endif
4879 + dev->freeTnodes = dev->freeTnodes->internal[0];
4880 + dev->nFreeTnodes--;
4881 + }
4882 +
4883 + return tn;
4884 +}
4885 +
4886 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4887 +{
4888 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4889 +
4890 + if(tn)
4891 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4892 +
4893 + return tn;
4894 +}
4895 +
4896 +/* FreeTnode frees up a tnode and puts it back on the free list */
4897 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4898 +{
4899 + if (tn) {
4900 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4901 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4902 + /* Hoosterman, this thing looks like it is already in the list */
4903 + T(YAFFS_TRACE_ALWAYS,
4904 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4905 + }
4906 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4907 +#endif
4908 + tn->internal[0] = dev->freeTnodes;
4909 + dev->freeTnodes = tn;
4910 + dev->nFreeTnodes++;
4911 + }
4912 +}
4913 +
4914 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4915 +{
4916 + /* Free the list of allocated tnodes */
4917 + yaffs_TnodeList *tmp;
4918 +
4919 + while (dev->allocatedTnodeList) {
4920 + tmp = dev->allocatedTnodeList->next;
4921 +
4922 + YFREE(dev->allocatedTnodeList->tnodes);
4923 + YFREE(dev->allocatedTnodeList);
4924 + dev->allocatedTnodeList = tmp;
4925 +
4926 + }
4927 +
4928 + dev->freeTnodes = NULL;
4929 + dev->nFreeTnodes = 0;
4930 +}
4931 +
4932 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4933 +{
4934 + dev->allocatedTnodeList = NULL;
4935 + dev->freeTnodes = NULL;
4936 + dev->nFreeTnodes = 0;
4937 + dev->nTnodesCreated = 0;
4938 +
4939 +}
4940 +
4941 +
4942 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4943 +{
4944 + __u32 *map = (__u32 *)tn;
4945 + __u32 bitInMap;
4946 + __u32 bitInWord;
4947 + __u32 wordInMap;
4948 + __u32 mask;
4949 +
4950 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4951 + val >>= dev->chunkGroupBits;
4952 +
4953 + bitInMap = pos * dev->tnodeWidth;
4954 + wordInMap = bitInMap /32;
4955 + bitInWord = bitInMap & (32 -1);
4956 +
4957 + mask = dev->tnodeMask << bitInWord;
4958 +
4959 + map[wordInMap] &= ~mask;
4960 + map[wordInMap] |= (mask & (val << bitInWord));
4961 +
4962 + if(dev->tnodeWidth > (32-bitInWord)) {
4963 + bitInWord = (32 - bitInWord);
4964 + wordInMap++;;
4965 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4966 + map[wordInMap] &= ~mask;
4967 + map[wordInMap] |= (mask & (val >> bitInWord));
4968 + }
4969 +}
4970 +
4971 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
4972 +{
4973 + __u32 *map = (__u32 *)tn;
4974 + __u32 bitInMap;
4975 + __u32 bitInWord;
4976 + __u32 wordInMap;
4977 + __u32 val;
4978 +
4979 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4980 +
4981 + bitInMap = pos * dev->tnodeWidth;
4982 + wordInMap = bitInMap /32;
4983 + bitInWord = bitInMap & (32 -1);
4984 +
4985 + val = map[wordInMap] >> bitInWord;
4986 +
4987 + if(dev->tnodeWidth > (32-bitInWord)) {
4988 + bitInWord = (32 - bitInWord);
4989 + wordInMap++;;
4990 + val |= (map[wordInMap] << bitInWord);
4991 + }
4992 +
4993 + val &= dev->tnodeMask;
4994 + val <<= dev->chunkGroupBits;
4995 +
4996 + return val;
4997 +}
4998 +
4999 +/* ------------------- End of individual tnode manipulation -----------------*/
5000 +
5001 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5002 + * The look up tree is represented by the top tnode and the number of topLevel
5003 + * in the tree. 0 means only the level 0 tnode is in the tree.
5004 + */
5005 +
5006 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5007 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
5008 + yaffs_FileStructure * fStruct,
5009 + __u32 chunkId)
5010 +{
5011 +
5012 + yaffs_Tnode *tn = fStruct->top;
5013 + __u32 i;
5014 + int requiredTallness;
5015 + int level = fStruct->topLevel;
5016 +
5017 + /* Check sane level and chunk Id */
5018 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
5019 + return NULL;
5020 + }
5021 +
5022 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5023 + return NULL;
5024 + }
5025 +
5026 + /* First check we're tall enough (ie enough topLevel) */
5027 +
5028 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5029 + requiredTallness = 0;
5030 + while (i) {
5031 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5032 + requiredTallness++;
5033 + }
5034 +
5035 + if (requiredTallness > fStruct->topLevel) {
5036 + /* Not tall enough, so we can't find it, return NULL. */
5037 + return NULL;
5038 + }
5039 +
5040 + /* Traverse down to level 0 */
5041 + while (level > 0 && tn) {
5042 + tn = tn->
5043 + internal[(chunkId >>
5044 + ( YAFFS_TNODES_LEVEL0_BITS +
5045 + (level - 1) *
5046 + YAFFS_TNODES_INTERNAL_BITS)
5047 + ) &
5048 + YAFFS_TNODES_INTERNAL_MASK];
5049 + level--;
5050 +
5051 + }
5052 +
5053 + return tn;
5054 +}
5055 +
5056 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5057 + * This happens in two steps:
5058 + * 1. If the tree isn't tall enough, then make it taller.
5059 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5060 + *
5061 + * Used when modifying the tree.
5062 + *
5063 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5064 + * be plugged into the ttree.
5065 + */
5066 +
5067 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
5068 + yaffs_FileStructure * fStruct,
5069 + __u32 chunkId,
5070 + yaffs_Tnode *passedTn)
5071 +{
5072 +
5073 + int requiredTallness;
5074 + int i;
5075 + int l;
5076 + yaffs_Tnode *tn;
5077 +
5078 + __u32 x;
5079 +
5080 +
5081 + /* Check sane level and page Id */
5082 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
5083 + return NULL;
5084 + }
5085 +
5086 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5087 + return NULL;
5088 + }
5089 +
5090 + /* First check we're tall enough (ie enough topLevel) */
5091 +
5092 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5093 + requiredTallness = 0;
5094 + while (x) {
5095 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5096 + requiredTallness++;
5097 + }
5098 +
5099 +
5100 + if (requiredTallness > fStruct->topLevel) {
5101 + /* Not tall enough,gotta make the tree taller */
5102 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5103 +
5104 + tn = yaffs_GetTnode(dev);
5105 +
5106 + if (tn) {
5107 + tn->internal[0] = fStruct->top;
5108 + fStruct->top = tn;
5109 + } else {
5110 + T(YAFFS_TRACE_ERROR,
5111 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5112 + }
5113 + }
5114 +
5115 + fStruct->topLevel = requiredTallness;
5116 + }
5117 +
5118 + /* Traverse down to level 0, adding anything we need */
5119 +
5120 + l = fStruct->topLevel;
5121 + tn = fStruct->top;
5122 +
5123 + if(l > 0) {
5124 + while (l > 0 && tn) {
5125 + x = (chunkId >>
5126 + ( YAFFS_TNODES_LEVEL0_BITS +
5127 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5128 + YAFFS_TNODES_INTERNAL_MASK;
5129 +
5130 +
5131 + if((l>1) && !tn->internal[x]){
5132 + /* Add missing non-level-zero tnode */
5133 + tn->internal[x] = yaffs_GetTnode(dev);
5134 +
5135 + } else if(l == 1) {
5136 + /* Looking from level 1 at level 0 */
5137 + if (passedTn) {
5138 + /* If we already have one, then release it.*/
5139 + if(tn->internal[x])
5140 + yaffs_FreeTnode(dev,tn->internal[x]);
5141 + tn->internal[x] = passedTn;
5142 +
5143 + } else if(!tn->internal[x]) {
5144 + /* Don't have one, none passed in */
5145 + tn->internal[x] = yaffs_GetTnode(dev);
5146 + }
5147 + }
5148 +
5149 + tn = tn->internal[x];
5150 + l--;
5151 + }
5152 + } else {
5153 + /* top is level 0 */
5154 + if(passedTn) {
5155 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5156 + yaffs_FreeTnode(dev,passedTn);
5157 + }
5158 + }
5159 +
5160 + return tn;
5161 +}
5162 +
5163 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
5164 + yaffs_ExtendedTags * tags, int objectId,
5165 + int chunkInInode)
5166 +{
5167 + int j;
5168 +
5169 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5170 + if (yaffs_CheckChunkBit
5171 + (dev, theChunk / dev->nChunksPerBlock,
5172 + theChunk % dev->nChunksPerBlock)) {
5173 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5174 + tags);
5175 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5176 + /* found it; */
5177 + return theChunk;
5178 +
5179 + }
5180 + }
5181 + theChunk++;
5182 + }
5183 + return -1;
5184 +}
5185 +
5186 +
5187 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5188 + * chunks and tnodes in the file
5189 + * Returns 1 if the tree was deleted.
5190 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5191 + */
5192 +
5193 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
5194 + int chunkOffset, int *limit)
5195 +{
5196 + int i;
5197 + int chunkInInode;
5198 + int theChunk;
5199 + yaffs_ExtendedTags tags;
5200 + int foundChunk;
5201 + yaffs_Device *dev = in->myDev;
5202 +
5203 + int allDone = 1;
5204 +
5205 + if (tn) {
5206 + if (level > 0) {
5207 +
5208 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5209 + i--) {
5210 + if (tn->internal[i]) {
5211 + if (limit && (*limit) < 0) {
5212 + allDone = 0;
5213 + } else {
5214 + allDone =
5215 + yaffs_DeleteWorker(in,
5216 + tn->
5217 + internal
5218 + [i],
5219 + level -
5220 + 1,
5221 + (chunkOffset
5222 + <<
5223 + YAFFS_TNODES_INTERNAL_BITS)
5224 + + i,
5225 + limit);
5226 + }
5227 + if (allDone) {
5228 + yaffs_FreeTnode(dev,
5229 + tn->
5230 + internal[i]);
5231 + tn->internal[i] = NULL;
5232 + }
5233 + }
5234 +
5235 + }
5236 + return (allDone) ? 1 : 0;
5237 + } else if (level == 0) {
5238 + int hitLimit = 0;
5239 +
5240 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5241 + i--) {
5242 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5243 + if (theChunk) {
5244 +
5245 + chunkInInode =
5246 + (chunkOffset <<
5247 + YAFFS_TNODES_LEVEL0_BITS) + i;
5248 +
5249 + foundChunk =
5250 + yaffs_FindChunkInGroup(dev,
5251 + theChunk,
5252 + &tags,
5253 + in->objectId,
5254 + chunkInInode);
5255 +
5256 + if (foundChunk > 0) {
5257 + yaffs_DeleteChunk(dev,
5258 + foundChunk, 1,
5259 + __LINE__);
5260 + in->nDataChunks--;
5261 + if (limit) {
5262 + *limit = *limit - 1;
5263 + if (*limit <= 0) {
5264 + hitLimit = 1;
5265 + }
5266 + }
5267 +
5268 + }
5269 +
5270 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5271 + }
5272 +
5273 + }
5274 + return (i < 0) ? 1 : 0;
5275 +
5276 + }
5277 +
5278 + }
5279 +
5280 + return 1;
5281 +
5282 +}
5283 +
5284 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
5285 +{
5286 +
5287 + yaffs_BlockInfo *theBlock;
5288 +
5289 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5290 +
5291 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5292 + if (theBlock) {
5293 + theBlock->softDeletions++;
5294 + dev->nFreeChunks++;
5295 + }
5296 +}
5297 +
5298 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5299 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5300 + * of the tnode.
5301 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5302 + */
5303 +
5304 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
5305 + __u32 level, int chunkOffset)
5306 +{
5307 + int i;
5308 + int theChunk;
5309 + int allDone = 1;
5310 + yaffs_Device *dev = in->myDev;
5311 +
5312 + if (tn) {
5313 + if (level > 0) {
5314 +
5315 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5316 + i--) {
5317 + if (tn->internal[i]) {
5318 + allDone =
5319 + yaffs_SoftDeleteWorker(in,
5320 + tn->
5321 + internal[i],
5322 + level - 1,
5323 + (chunkOffset
5324 + <<
5325 + YAFFS_TNODES_INTERNAL_BITS)
5326 + + i);
5327 + if (allDone) {
5328 + yaffs_FreeTnode(dev,
5329 + tn->
5330 + internal[i]);
5331 + tn->internal[i] = NULL;
5332 + } else {
5333 + /* Hoosterman... how could this happen? */
5334 + }
5335 + }
5336 + }
5337 + return (allDone) ? 1 : 0;
5338 + } else if (level == 0) {
5339 +
5340 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5341 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5342 + if (theChunk) {
5343 + /* Note this does not find the real chunk, only the chunk group.
5344 + * We make an assumption that a chunk group is not larger than
5345 + * a block.
5346 + */
5347 + yaffs_SoftDeleteChunk(dev, theChunk);
5348 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5349 + }
5350 +
5351 + }
5352 + return 1;
5353 +
5354 + }
5355 +
5356 + }
5357 +
5358 + return 1;
5359 +
5360 +}
5361 +
5362 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
5363 +{
5364 + if (obj->deleted &&
5365 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5366 + if (obj->nDataChunks <= 0) {
5367 + /* Empty file with no duplicate object headers, just delete it immediately */
5368 + yaffs_FreeTnode(obj->myDev,
5369 + obj->variant.fileVariant.top);
5370 + obj->variant.fileVariant.top = NULL;
5371 + T(YAFFS_TRACE_TRACING,
5372 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5373 + obj->objectId));
5374 + yaffs_DoGenericObjectDeletion(obj);
5375 + } else {
5376 + yaffs_SoftDeleteWorker(obj,
5377 + obj->variant.fileVariant.top,
5378 + obj->variant.fileVariant.
5379 + topLevel, 0);
5380 + obj->softDeleted = 1;
5381 + }
5382 + }
5383 +}
5384 +
5385 +/* Pruning removes any part of the file structure tree that is beyond the
5386 + * bounds of the file (ie that does not point to chunks).
5387 + *
5388 + * A file should only get pruned when its size is reduced.
5389 + *
5390 + * Before pruning, the chunks must be pulled from the tree and the
5391 + * level 0 tnode entries must be zeroed out.
5392 + * Could also use this for file deletion, but that's probably better handled
5393 + * by a special case.
5394 + */
5395 +
5396 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
5397 + __u32 level, int del0)
5398 +{
5399 + int i;
5400 + int hasData;
5401 +
5402 + if (tn) {
5403 + hasData = 0;
5404 +
5405 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5406 + if (tn->internal[i] && level > 0) {
5407 + tn->internal[i] =
5408 + yaffs_PruneWorker(dev, tn->internal[i],
5409 + level - 1,
5410 + (i == 0) ? del0 : 1);
5411 + }
5412 +
5413 + if (tn->internal[i]) {
5414 + hasData++;
5415 + }
5416 + }
5417 +
5418 + if (hasData == 0 && del0) {
5419 + /* Free and return NULL */
5420 +
5421 + yaffs_FreeTnode(dev, tn);
5422 + tn = NULL;
5423 + }
5424 +
5425 + }
5426 +
5427 + return tn;
5428 +
5429 +}
5430 +
5431 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
5432 + yaffs_FileStructure * fStruct)
5433 +{
5434 + int i;
5435 + int hasData;
5436 + int done = 0;
5437 + yaffs_Tnode *tn;
5438 +
5439 + if (fStruct->topLevel > 0) {
5440 + fStruct->top =
5441 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5442 +
5443 + /* Now we have a tree with all the non-zero branches NULL but the height
5444 + * is the same as it was.
5445 + * Let's see if we can trim internal tnodes to shorten the tree.
5446 + * We can do this if only the 0th element in the tnode is in use
5447 + * (ie all the non-zero are NULL)
5448 + */
5449 +
5450 + while (fStruct->topLevel && !done) {
5451 + tn = fStruct->top;
5452 +
5453 + hasData = 0;
5454 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5455 + if (tn->internal[i]) {
5456 + hasData++;
5457 + }
5458 + }
5459 +
5460 + if (!hasData) {
5461 + fStruct->top = tn->internal[0];
5462 + fStruct->topLevel--;
5463 + yaffs_FreeTnode(dev, tn);
5464 + } else {
5465 + done = 1;
5466 + }
5467 + }
5468 + }
5469 +
5470 + return YAFFS_OK;
5471 +}
5472 +
5473 +/*-------------------- End of File Structure functions.-------------------*/
5474 +
5475 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5476 + * adds them to the object free list.
5477 + */
5478 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
5479 +{
5480 + int i;
5481 + yaffs_Object *newObjects;
5482 + yaffs_ObjectList *list;
5483 +
5484 + if (nObjects < 1)
5485 + return YAFFS_OK;
5486 +
5487 + /* make these things */
5488 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5489 + list = YMALLOC(sizeof(yaffs_ObjectList));
5490 +
5491 + if (!newObjects || !list) {
5492 + if(newObjects)
5493 + YFREE(newObjects);
5494 + if(list)
5495 + YFREE(list);
5496 + T(YAFFS_TRACE_ALLOCATE,
5497 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5498 + return YAFFS_FAIL;
5499 + }
5500 +
5501 + /* Hook them into the free list */
5502 + for (i = 0; i < nObjects - 1; i++) {
5503 + newObjects[i].siblings.next =
5504 + (struct list_head *)(&newObjects[i + 1]);
5505 + }
5506 +
5507 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5508 + dev->freeObjects = newObjects;
5509 + dev->nFreeObjects += nObjects;
5510 + dev->nObjectsCreated += nObjects;
5511 +
5512 + /* Now add this bunch of Objects to a list for freeing up. */
5513 +
5514 + list->objects = newObjects;
5515 + list->next = dev->allocatedObjectList;
5516 + dev->allocatedObjectList = list;
5517 +
5518 + return YAFFS_OK;
5519 +}
5520 +
5521 +
5522 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5523 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
5524 +{
5525 + yaffs_Object *tn = NULL;
5526 +
5527 + /* If there are none left make more */
5528 + if (!dev->freeObjects) {
5529 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5530 + }
5531 +
5532 + if (dev->freeObjects) {
5533 + tn = dev->freeObjects;
5534 + dev->freeObjects =
5535 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5536 + dev->nFreeObjects--;
5537 +
5538 + /* Now sweeten it up... */
5539 +
5540 + memset(tn, 0, sizeof(yaffs_Object));
5541 + tn->myDev = dev;
5542 + tn->chunkId = -1;
5543 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5544 + INIT_LIST_HEAD(&(tn->hardLinks));
5545 + INIT_LIST_HEAD(&(tn->hashLink));
5546 + INIT_LIST_HEAD(&tn->siblings);
5547 +
5548 + /* Add it to the lost and found directory.
5549 + * NB Can't put root or lostNFound in lostNFound so
5550 + * check if lostNFound exists first
5551 + */
5552 + if (dev->lostNFoundDir) {
5553 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5554 + }
5555 + }
5556 +
5557 + return tn;
5558 +}
5559 +
5560 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
5561 + __u32 mode)
5562 +{
5563 +
5564 + yaffs_Object *obj =
5565 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5566 + if (obj) {
5567 + obj->fake = 1; /* it is fake so it has no NAND presence... */
5568 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5569 + obj->unlinkAllowed = 0; /* ... or unlink it */
5570 + obj->deleted = 0;
5571 + obj->unlinked = 0;
5572 + obj->yst_mode = mode;
5573 + obj->myDev = dev;
5574 + obj->chunkId = 0; /* Not a valid chunk. */
5575 + }
5576 +
5577 + return obj;
5578 +
5579 +}
5580 +
5581 +static void yaffs_UnhashObject(yaffs_Object * tn)
5582 +{
5583 + int bucket;
5584 + yaffs_Device *dev = tn->myDev;
5585 +
5586 + /* If it is still linked into the bucket list, free from the list */
5587 + if (!list_empty(&tn->hashLink)) {
5588 + list_del_init(&tn->hashLink);
5589 + bucket = yaffs_HashFunction(tn->objectId);
5590 + dev->objectBucket[bucket].count--;
5591 + }
5592 +
5593 +}
5594 +
5595 +/* FreeObject frees up a Object and puts it back on the free list */
5596 +static void yaffs_FreeObject(yaffs_Object * tn)
5597 +{
5598 +
5599 + yaffs_Device *dev = tn->myDev;
5600 +
5601 +#ifdef __KERNEL__
5602 + if (tn->myInode) {
5603 + /* We're still hooked up to a cached inode.
5604 + * Don't delete now, but mark for later deletion
5605 + */
5606 + tn->deferedFree = 1;
5607 + return;
5608 + }
5609 +#endif
5610 +
5611 + yaffs_UnhashObject(tn);
5612 +
5613 + /* Link into the free list. */
5614 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
5615 + dev->freeObjects = tn;
5616 + dev->nFreeObjects++;
5617 +}
5618 +
5619 +#ifdef __KERNEL__
5620 +
5621 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
5622 +{
5623 + if (obj->deferedFree) {
5624 + yaffs_FreeObject(obj);
5625 + }
5626 +}
5627 +
5628 +#endif
5629 +
5630 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
5631 +{
5632 + /* Free the list of allocated Objects */
5633 +
5634 + yaffs_ObjectList *tmp;
5635 +
5636 + while (dev->allocatedObjectList) {
5637 + tmp = dev->allocatedObjectList->next;
5638 + YFREE(dev->allocatedObjectList->objects);
5639 + YFREE(dev->allocatedObjectList);
5640 +
5641 + dev->allocatedObjectList = tmp;
5642 + }
5643 +
5644 + dev->freeObjects = NULL;
5645 + dev->nFreeObjects = 0;
5646 +}
5647 +
5648 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
5649 +{
5650 + int i;
5651 +
5652 + dev->allocatedObjectList = NULL;
5653 + dev->freeObjects = NULL;
5654 + dev->nFreeObjects = 0;
5655 +
5656 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5657 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
5658 + dev->objectBucket[i].count = 0;
5659 + }
5660 +
5661 +}
5662 +
5663 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
5664 +{
5665 + static int x = 0;
5666 + int i;
5667 + int l = 999;
5668 + int lowest = 999999;
5669 +
5670 + /* First let's see if we can find one that's empty. */
5671 +
5672 + for (i = 0; i < 10 && lowest > 0; i++) {
5673 + x++;
5674 + x %= YAFFS_NOBJECT_BUCKETS;
5675 + if (dev->objectBucket[x].count < lowest) {
5676 + lowest = dev->objectBucket[x].count;
5677 + l = x;
5678 + }
5679 +
5680 + }
5681 +
5682 + /* If we didn't find an empty list, then try
5683 + * looking a bit further for a short one
5684 + */
5685 +
5686 + for (i = 0; i < 10 && lowest > 3; i++) {
5687 + x++;
5688 + x %= YAFFS_NOBJECT_BUCKETS;
5689 + if (dev->objectBucket[x].count < lowest) {
5690 + lowest = dev->objectBucket[x].count;
5691 + l = x;
5692 + }
5693 +
5694 + }
5695 +
5696 + return l;
5697 +}
5698 +
5699 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
5700 +{
5701 + int bucket = yaffs_FindNiceObjectBucket(dev);
5702 +
5703 + /* Now find an object value that has not already been taken
5704 + * by scanning the list.
5705 + */
5706 +
5707 + int found = 0;
5708 + struct list_head *i;
5709 +
5710 + __u32 n = (__u32) bucket;
5711 +
5712 + /* yaffs_CheckObjectHashSanity(); */
5713 +
5714 + while (!found) {
5715 + found = 1;
5716 + n += YAFFS_NOBJECT_BUCKETS;
5717 + if (1 || dev->objectBucket[bucket].count > 0) {
5718 + list_for_each(i, &dev->objectBucket[bucket].list) {
5719 + /* If there is already one in the list */
5720 + if (i
5721 + && list_entry(i, yaffs_Object,
5722 + hashLink)->objectId == n) {
5723 + found = 0;
5724 + }
5725 + }
5726 + }
5727 + }
5728 +
5729 +
5730 + return n;
5731 +}
5732 +
5733 +static void yaffs_HashObject(yaffs_Object * in)
5734 +{
5735 + int bucket = yaffs_HashFunction(in->objectId);
5736 + yaffs_Device *dev = in->myDev;
5737 +
5738 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
5739 + dev->objectBucket[bucket].count++;
5740 +
5741 +}
5742 +
5743 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5744 +{
5745 + int bucket = yaffs_HashFunction(number);
5746 + struct list_head *i;
5747 + yaffs_Object *in;
5748 +
5749 + list_for_each(i, &dev->objectBucket[bucket].list) {
5750 + /* Look if it is in the list */
5751 + if (i) {
5752 + in = list_entry(i, yaffs_Object, hashLink);
5753 + if (in->objectId == number) {
5754 +#ifdef __KERNEL__
5755 + /* Don't tell the VFS about this one if it is defered free */
5756 + if (in->deferedFree)
5757 + return NULL;
5758 +#endif
5759 +
5760 + return in;
5761 + }
5762 + }
5763 + }
5764 +
5765 + return NULL;
5766 +}
5767 +
5768 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5769 + yaffs_ObjectType type)
5770 +{
5771 +
5772 + yaffs_Object *theObject;
5773 + yaffs_Tnode *tn;
5774 +
5775 + if (number < 0) {
5776 + number = yaffs_CreateNewObjectNumber(dev);
5777 + }
5778 +
5779 + theObject = yaffs_AllocateEmptyObject(dev);
5780 + if(!theObject)
5781 + return NULL;
5782 +
5783 + if(type == YAFFS_OBJECT_TYPE_FILE){
5784 + tn = yaffs_GetTnode(dev);
5785 + if(!tn){
5786 + yaffs_FreeObject(theObject);
5787 + return NULL;
5788 + }
5789 + }
5790 +
5791 +
5792 +
5793 + if (theObject) {
5794 + theObject->fake = 0;
5795 + theObject->renameAllowed = 1;
5796 + theObject->unlinkAllowed = 1;
5797 + theObject->objectId = number;
5798 + yaffs_HashObject(theObject);
5799 + theObject->variantType = type;
5800 +#ifdef CONFIG_YAFFS_WINCE
5801 + yfsd_WinFileTimeNow(theObject->win_atime);
5802 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5803 + theObject->win_atime[0];
5804 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5805 + theObject->win_atime[1];
5806 +
5807 +#else
5808 +
5809 + theObject->yst_atime = theObject->yst_mtime =
5810 + theObject->yst_ctime = Y_CURRENT_TIME;
5811 +#endif
5812 + switch (type) {
5813 + case YAFFS_OBJECT_TYPE_FILE:
5814 + theObject->variant.fileVariant.fileSize = 0;
5815 + theObject->variant.fileVariant.scannedFileSize = 0;
5816 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5817 + theObject->variant.fileVariant.topLevel = 0;
5818 + theObject->variant.fileVariant.top = tn;
5819 + break;
5820 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5821 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5822 + children);
5823 + break;
5824 + case YAFFS_OBJECT_TYPE_SYMLINK:
5825 + case YAFFS_OBJECT_TYPE_HARDLINK:
5826 + case YAFFS_OBJECT_TYPE_SPECIAL:
5827 + /* No action required */
5828 + break;
5829 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5830 + /* todo this should not happen */
5831 + break;
5832 + }
5833 + }
5834 +
5835 + return theObject;
5836 +}
5837 +
5838 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5839 + int number,
5840 + yaffs_ObjectType type)
5841 +{
5842 + yaffs_Object *theObject = NULL;
5843 +
5844 + if (number > 0) {
5845 + theObject = yaffs_FindObjectByNumber(dev, number);
5846 + }
5847 +
5848 + if (!theObject) {
5849 + theObject = yaffs_CreateNewObject(dev, number, type);
5850 + }
5851 +
5852 + return theObject;
5853 +
5854 +}
5855 +
5856 +
5857 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5858 +{
5859 + YCHAR *newStr = NULL;
5860 +
5861 + if (str && *str) {
5862 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5863 + if(newStr)
5864 + yaffs_strcpy(newStr, str);
5865 + }
5866 +
5867 + return newStr;
5868 +
5869 +}
5870 +
5871 +/*
5872 + * Mknod (create) a new object.
5873 + * equivalentObject only has meaning for a hard link;
5874 + * aliasString only has meaning for a sumlink.
5875 + * rdev only has meaning for devices (a subset of special objects)
5876 + */
5877 +
5878 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5879 + yaffs_Object * parent,
5880 + const YCHAR * name,
5881 + __u32 mode,
5882 + __u32 uid,
5883 + __u32 gid,
5884 + yaffs_Object * equivalentObject,
5885 + const YCHAR * aliasString, __u32 rdev)
5886 +{
5887 + yaffs_Object *in;
5888 + YCHAR *str;
5889 +
5890 + yaffs_Device *dev = parent->myDev;
5891 +
5892 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5893 + if (yaffs_FindObjectByName(parent, name)) {
5894 + return NULL;
5895 + }
5896 +
5897 + in = yaffs_CreateNewObject(dev, -1, type);
5898 +
5899 + if(type == YAFFS_OBJECT_TYPE_SYMLINK){
5900 + str = yaffs_CloneString(aliasString);
5901 + if(!str){
5902 + yaffs_FreeObject(in);
5903 + return NULL;
5904 + }
5905 + }
5906 +
5907 +
5908 +
5909 + if (in) {
5910 + in->chunkId = -1;
5911 + in->valid = 1;
5912 + in->variantType = type;
5913 +
5914 + in->yst_mode = mode;
5915 +
5916 +#ifdef CONFIG_YAFFS_WINCE
5917 + yfsd_WinFileTimeNow(in->win_atime);
5918 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5919 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5920 +
5921 +#else
5922 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5923 +
5924 + in->yst_rdev = rdev;
5925 + in->yst_uid = uid;
5926 + in->yst_gid = gid;
5927 +#endif
5928 + in->nDataChunks = 0;
5929 +
5930 + yaffs_SetObjectName(in, name);
5931 + in->dirty = 1;
5932 +
5933 + yaffs_AddObjectToDirectory(parent, in);
5934 +
5935 + in->myDev = parent->myDev;
5936 +
5937 + switch (type) {
5938 + case YAFFS_OBJECT_TYPE_SYMLINK:
5939 + in->variant.symLinkVariant.alias = str;
5940 + break;
5941 + case YAFFS_OBJECT_TYPE_HARDLINK:
5942 + in->variant.hardLinkVariant.equivalentObject =
5943 + equivalentObject;
5944 + in->variant.hardLinkVariant.equivalentObjectId =
5945 + equivalentObject->objectId;
5946 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5947 + break;
5948 + case YAFFS_OBJECT_TYPE_FILE:
5949 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5950 + case YAFFS_OBJECT_TYPE_SPECIAL:
5951 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5952 + /* do nothing */
5953 + break;
5954 + }
5955 +
5956 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5957 + /* Could not create the object header, fail the creation */
5958 + yaffs_DestroyObject(in);
5959 + in = NULL;
5960 + }
5961 +
5962 + }
5963 +
5964 + return in;
5965 +}
5966 +
5967 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5968 + __u32 mode, __u32 uid, __u32 gid)
5969 +{
5970 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
5971 + uid, gid, NULL, NULL, 0);
5972 +}
5973 +
5974 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
5975 + __u32 mode, __u32 uid, __u32 gid)
5976 +{
5977 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
5978 + mode, uid, gid, NULL, NULL, 0);
5979 +}
5980 +
5981 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
5982 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
5983 +{
5984 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
5985 + uid, gid, NULL, NULL, rdev);
5986 +}
5987 +
5988 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
5989 + __u32 mode, __u32 uid, __u32 gid,
5990 + const YCHAR * alias)
5991 +{
5992 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
5993 + uid, gid, NULL, alias, 0);
5994 +}
5995 +
5996 +/* yaffs_Link returns the object id of the equivalent object.*/
5997 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
5998 + yaffs_Object * equivalentObject)
5999 +{
6000 + /* Get the real object in case we were fed a hard link as an equivalent object */
6001 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6002 +
6003 + if (yaffs_MknodObject
6004 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6005 + equivalentObject, NULL, 0)) {
6006 + return equivalentObject;
6007 + } else {
6008 + return NULL;
6009 + }
6010 +
6011 +}
6012 +
6013 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
6014 + const YCHAR * newName, int force, int shadows)
6015 +{
6016 + int unlinkOp;
6017 + int deleteOp;
6018 +
6019 + yaffs_Object *existingTarget;
6020 +
6021 + if (newDir == NULL) {
6022 + newDir = obj->parent; /* use the old directory */
6023 + }
6024 +
6025 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6026 + T(YAFFS_TRACE_ALWAYS,
6027 + (TSTR
6028 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
6029 + TENDSTR)));
6030 + YBUG();
6031 + }
6032 +
6033 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6034 + if (obj->myDev->isYaffs2) {
6035 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6036 + } else {
6037 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6038 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6039 + }
6040 +
6041 + deleteOp = (newDir == obj->myDev->deletedDir);
6042 +
6043 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6044 +
6045 + /* If the object is a file going into the unlinked directory,
6046 + * then it is OK to just stuff it in since duplicate names are allowed.
6047 + * else only proceed if the new name does not exist and if we're putting
6048 + * it into a directory.
6049 + */
6050 + if ((unlinkOp ||
6051 + deleteOp ||
6052 + force ||
6053 + (shadows > 0) ||
6054 + !existingTarget) &&
6055 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6056 + yaffs_SetObjectName(obj, newName);
6057 + obj->dirty = 1;
6058 +
6059 + yaffs_AddObjectToDirectory(newDir, obj);
6060 +
6061 + if (unlinkOp)
6062 + obj->unlinked = 1;
6063 +
6064 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6065 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
6066 + return YAFFS_OK;
6067 + }
6068 +
6069 + return YAFFS_FAIL;
6070 +}
6071 +
6072 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
6073 + yaffs_Object * newDir, const YCHAR * newName)
6074 +{
6075 + yaffs_Object *obj;
6076 + yaffs_Object *existingTarget;
6077 + int force = 0;
6078 +
6079 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6080 + /* Special case for case insemsitive systems (eg. WinCE).
6081 + * While look-up is case insensitive, the name isn't.
6082 + * Therefore we might want to change x.txt to X.txt
6083 + */
6084 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
6085 + force = 1;
6086 + }
6087 +#endif
6088 +
6089 + obj = yaffs_FindObjectByName(oldDir, oldName);
6090 + /* Check new name to long. */
6091 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
6092 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
6093 + /* ENAMETOOLONG */
6094 + return YAFFS_FAIL;
6095 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
6096 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6097 + /* ENAMETOOLONG */
6098 + return YAFFS_FAIL;
6099 +
6100 + if (obj && obj->renameAllowed) {
6101 +
6102 + /* Now do the handling for an existing target, if there is one */
6103 +
6104 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6105 + if (existingTarget &&
6106 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6107 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
6108 + /* There is a target that is a non-empty directory, so we fail */
6109 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6110 + } else if (existingTarget && existingTarget != obj) {
6111 + /* Nuke the target first, using shadowing,
6112 + * but only if it isn't the same object
6113 + */
6114 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6115 + existingTarget->objectId);
6116 + yaffs_UnlinkObject(existingTarget);
6117 + }
6118 +
6119 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6120 + }
6121 + return YAFFS_FAIL;
6122 +}
6123 +
6124 +/*------------------------- Block Management and Page Allocation ----------------*/
6125 +
6126 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
6127 +{
6128 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6129 +
6130 + dev->blockInfo = NULL;
6131 + dev->chunkBits = NULL;
6132 +
6133 + dev->allocationBlock = -1; /* force it to get a new one */
6134 +
6135 + /* If the first allocation strategy fails, thry the alternate one */
6136 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6137 + if(!dev->blockInfo){
6138 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6139 + dev->blockInfoAlt = 1;
6140 + }
6141 + else
6142 + dev->blockInfoAlt = 0;
6143 +
6144 + if(dev->blockInfo){
6145 +
6146 + /* Set up dynamic blockinfo stuff. */
6147 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6148 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6149 + if(!dev->chunkBits){
6150 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6151 + dev->chunkBitsAlt = 1;
6152 + }
6153 + else
6154 + dev->chunkBitsAlt = 0;
6155 + }
6156 +
6157 + if (dev->blockInfo && dev->chunkBits) {
6158 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6159 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6160 + return YAFFS_OK;
6161 + }
6162 +
6163 + return YAFFS_FAIL;
6164 +
6165 +}
6166 +
6167 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
6168 +{
6169 + if(dev->blockInfoAlt && dev->blockInfo)
6170 + YFREE_ALT(dev->blockInfo);
6171 + else if(dev->blockInfo)
6172 + YFREE(dev->blockInfo);
6173 +
6174 + dev->blockInfoAlt = 0;
6175 +
6176 + dev->blockInfo = NULL;
6177 +
6178 + if(dev->chunkBitsAlt && dev->chunkBits)
6179 + YFREE_ALT(dev->chunkBits);
6180 + else if(dev->chunkBits)
6181 + YFREE(dev->chunkBits);
6182 + dev->chunkBitsAlt = 0;
6183 + dev->chunkBits = NULL;
6184 +}
6185 +
6186 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
6187 + yaffs_BlockInfo * bi)
6188 +{
6189 + int i;
6190 + __u32 seq;
6191 + yaffs_BlockInfo *b;
6192 +
6193 + if (!dev->isYaffs2)
6194 + return 1; /* disqualification only applies to yaffs2. */
6195 +
6196 + if (!bi->hasShrinkHeader)
6197 + return 1; /* can gc */
6198 +
6199 + /* Find the oldest dirty sequence number if we don't know it and save it
6200 + * so we don't have to keep recomputing it.
6201 + */
6202 + if (!dev->oldestDirtySequence) {
6203 + seq = dev->sequenceNumber;
6204 +
6205 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6206 + i++) {
6207 + b = yaffs_GetBlockInfo(dev, i);
6208 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6209 + (b->pagesInUse - b->softDeletions) <
6210 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6211 + seq = b->sequenceNumber;
6212 + }
6213 + }
6214 + dev->oldestDirtySequence = seq;
6215 + }
6216 +
6217 + /* Can't do gc of this block if there are any blocks older than this one that have
6218 + * discarded pages.
6219 + */
6220 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6221 +
6222 +}
6223 +
6224 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6225 + * for garbage collection.
6226 + */
6227 +
6228 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
6229 + int aggressive)
6230 +{
6231 +
6232 + int b = dev->currentDirtyChecker;
6233 +
6234 + int i;
6235 + int iterations;
6236 + int dirtiest = -1;
6237 + int pagesInUse = 0;
6238 + int prioritised=0;
6239 + yaffs_BlockInfo *bi;
6240 + int pendingPrioritisedExist = 0;
6241 +
6242 + /* First let's see if we need to grab a prioritised block */
6243 + if(dev->hasPendingPrioritisedGCs){
6244 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
6245 +
6246 + bi = yaffs_GetBlockInfo(dev, i);
6247 + //yaffs_VerifyBlock(dev,bi,i);
6248 +
6249 + if(bi->gcPrioritise) {
6250 + pendingPrioritisedExist = 1;
6251 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6252 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
6253 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6254 + dirtiest = i;
6255 + prioritised = 1;
6256 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6257 + }
6258 + }
6259 + }
6260 +
6261 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
6262 + dev->hasPendingPrioritisedGCs = 0;
6263 + }
6264 +
6265 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6266 + * search harder.
6267 + * else (we're doing a leasurely gc), then we only bother to do this if the
6268 + * block has only a few pages in use.
6269 + */
6270 +
6271 + dev->nonAggressiveSkip--;
6272 +
6273 + if (!aggressive && (dev->nonAggressiveSkip > 0)) {
6274 + return -1;
6275 + }
6276 +
6277 + if(!prioritised)
6278 + pagesInUse =
6279 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6280 +
6281 + if (aggressive) {
6282 + iterations =
6283 + dev->internalEndBlock - dev->internalStartBlock + 1;
6284 + } else {
6285 + iterations =
6286 + dev->internalEndBlock - dev->internalStartBlock + 1;
6287 + iterations = iterations / 16;
6288 + if (iterations > 200) {
6289 + iterations = 200;
6290 + }
6291 + }
6292 +
6293 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6294 + b++;
6295 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6296 + b = dev->internalStartBlock;
6297 + }
6298 +
6299 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6300 + T(YAFFS_TRACE_ERROR,
6301 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6302 + YBUG();
6303 + }
6304 +
6305 + bi = yaffs_GetBlockInfo(dev, b);
6306 +
6307 +#if 0
6308 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
6309 + dirtiest = b;
6310 + pagesInUse = 0;
6311 + }
6312 + else
6313 +#endif
6314 +
6315 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6316 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6317 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6318 + dirtiest = b;
6319 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6320 + }
6321 + }
6322 +
6323 + dev->currentDirtyChecker = b;
6324 +
6325 + if (dirtiest > 0) {
6326 + T(YAFFS_TRACE_GC,
6327 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6328 + dev->nChunksPerBlock - pagesInUse,prioritised));
6329 + }
6330 +
6331 + dev->oldestDirtySequence = 0;
6332 +
6333 + if (dirtiest > 0) {
6334 + dev->nonAggressiveSkip = 4;
6335 + }
6336 +
6337 + return dirtiest;
6338 +}
6339 +
6340 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
6341 +{
6342 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6343 +
6344 + int erasedOk = 0;
6345 +
6346 + /* If the block is still healthy erase it and mark as clean.
6347 + * If the block has had a data failure, then retire it.
6348 + */
6349 +
6350 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6351 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6352 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6353 +
6354 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6355 +
6356 + if (!bi->needsRetiring) {
6357 + yaffs_InvalidateCheckpoint(dev);
6358 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6359 + if (!erasedOk) {
6360 + dev->nErasureFailures++;
6361 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6362 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6363 + }
6364 + }
6365 +
6366 + if (erasedOk &&
6367 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6368 + int i;
6369 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6370 + if (!yaffs_CheckChunkErased
6371 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6372 + T(YAFFS_TRACE_ERROR,
6373 + (TSTR
6374 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6375 + TENDSTR), blockNo, i));
6376 + }
6377 + }
6378 + }
6379 +
6380 + if (erasedOk) {
6381 + /* Clean it up... */
6382 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6383 + dev->nErasedBlocks++;
6384 + bi->pagesInUse = 0;
6385 + bi->softDeletions = 0;
6386 + bi->hasShrinkHeader = 0;
6387 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6388 + bi->gcPrioritise = 0;
6389 + yaffs_ClearChunkBits(dev, blockNo);
6390 +
6391 + T(YAFFS_TRACE_ERASE,
6392 + (TSTR("Erased block %d" TENDSTR), blockNo));
6393 + } else {
6394 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6395 +
6396 + yaffs_RetireBlock(dev, blockNo);
6397 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6398 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6399 + }
6400 +}
6401 +
6402 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
6403 +{
6404 + int i;
6405 +
6406 + yaffs_BlockInfo *bi;
6407 +
6408 + if (dev->nErasedBlocks < 1) {
6409 + /* Hoosterman we've got a problem.
6410 + * Can't get space to gc
6411 + */
6412 + T(YAFFS_TRACE_ERROR,
6413 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
6414 +
6415 + return -1;
6416 + }
6417 +
6418 + /* Find an empty block. */
6419 +
6420 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6421 + dev->allocationBlockFinder++;
6422 + if (dev->allocationBlockFinder < dev->internalStartBlock
6423 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6424 + dev->allocationBlockFinder = dev->internalStartBlock;
6425 + }
6426 +
6427 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6428 +
6429 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6430 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6431 + dev->sequenceNumber++;
6432 + bi->sequenceNumber = dev->sequenceNumber;
6433 + dev->nErasedBlocks--;
6434 + T(YAFFS_TRACE_ALLOCATE,
6435 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6436 + dev->allocationBlockFinder, dev->sequenceNumber,
6437 + dev->nErasedBlocks));
6438 + return dev->allocationBlockFinder;
6439 + }
6440 + }
6441 +
6442 + T(YAFFS_TRACE_ALWAYS,
6443 + (TSTR
6444 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
6445 + TENDSTR), dev->nErasedBlocks));
6446 +
6447 + return -1;
6448 +}
6449 +
6450 +
6451 +// Check if there's space to allocate...
6452 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6453 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
6454 +{
6455 + int reservedChunks;
6456 + int reservedBlocks = dev->nReservedBlocks;
6457 + int checkpointBlocks;
6458 +
6459 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
6460 + if(checkpointBlocks < 0)
6461 + checkpointBlocks = 0;
6462 +
6463 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6464 +
6465 + return (dev->nFreeChunks > reservedChunks);
6466 +}
6467 +
6468 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
6469 +{
6470 + int retVal;
6471 + yaffs_BlockInfo *bi;
6472 +
6473 + if (dev->allocationBlock < 0) {
6474 + /* Get next block to allocate off */
6475 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6476 + dev->allocationPage = 0;
6477 + }
6478 +
6479 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6480 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6481 + return -1;
6482 + }
6483 +
6484 + if (dev->nErasedBlocks < dev->nReservedBlocks
6485 + && dev->allocationPage == 0) {
6486 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6487 + }
6488 +
6489 + /* Next page please.... */
6490 + if (dev->allocationBlock >= 0) {
6491 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6492 +
6493 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6494 + dev->allocationPage;
6495 + bi->pagesInUse++;
6496 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6497 + dev->allocationPage);
6498 +
6499 + dev->allocationPage++;
6500 +
6501 + dev->nFreeChunks--;
6502 +
6503 + /* If the block is full set the state to full */
6504 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6505 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6506 + dev->allocationBlock = -1;
6507 + }
6508 +
6509 + if(blockUsedPtr)
6510 + *blockUsedPtr = bi;
6511 +
6512 + return retVal;
6513 + }
6514 +
6515 + T(YAFFS_TRACE_ERROR,
6516 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6517 +
6518 + return -1;
6519 +}
6520 +
6521 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
6522 +{
6523 + int n;
6524 +
6525 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6526 +
6527 + if (dev->allocationBlock > 0) {
6528 + n += (dev->nChunksPerBlock - dev->allocationPage);
6529 + }
6530 +
6531 + return n;
6532 +
6533 +}
6534 +
6535 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
6536 +{
6537 + int oldChunk;
6538 + int newChunk;
6539 + int chunkInBlock;
6540 + int markNAND;
6541 + int retVal = YAFFS_OK;
6542 + int cleanups = 0;
6543 + int i;
6544 + int isCheckpointBlock;
6545 + int matchingChunk;
6546 +
6547 + int chunksBefore = yaffs_GetErasedChunks(dev);
6548 + int chunksAfter;
6549 +
6550 + yaffs_ExtendedTags tags;
6551 +
6552 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6553 +
6554 + yaffs_Object *object;
6555 +
6556 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6557 +
6558 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6559 +
6560 + T(YAFFS_TRACE_TRACING,
6561 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
6562 + bi->pagesInUse, bi->hasShrinkHeader));
6563 +
6564 + /*yaffs_VerifyFreeChunks(dev); */
6565 +
6566 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6567 +
6568 + /* Take off the number of soft deleted entries because
6569 + * they're going to get really deleted during GC.
6570 + */
6571 + dev->nFreeChunks -= bi->softDeletions;
6572 +
6573 + dev->isDoingGC = 1;
6574 +
6575 + if (isCheckpointBlock ||
6576 + !yaffs_StillSomeChunkBits(dev, block)) {
6577 + T(YAFFS_TRACE_TRACING,
6578 + (TSTR
6579 + ("Collecting block %d that has no chunks in use" TENDSTR),
6580 + block));
6581 + yaffs_BlockBecameDirty(dev, block);
6582 + } else {
6583 +
6584 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6585 +
6586 + yaffs_VerifyBlock(dev,bi,block);
6587 +
6588 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
6589 + chunkInBlock < dev->nChunksPerBlock
6590 + && yaffs_StillSomeChunkBits(dev, block);
6591 + chunkInBlock++, oldChunk++) {
6592 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
6593 +
6594 + /* This page is in use and might need to be copied off */
6595 +
6596 + markNAND = 1;
6597 +
6598 + yaffs_InitialiseTags(&tags);
6599 +
6600 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
6601 + buffer, &tags);
6602 +
6603 + object =
6604 + yaffs_FindObjectByNumber(dev,
6605 + tags.objectId);
6606 +
6607 + T(YAFFS_TRACE_GC_DETAIL,
6608 + (TSTR
6609 + ("Collecting page %d, %d %d %d " TENDSTR),
6610 + chunkInBlock, tags.objectId, tags.chunkId,
6611 + tags.byteCount));
6612 +
6613 + if(object && !yaffs_SkipVerification(dev)){
6614 + if(tags.chunkId == 0)
6615 + matchingChunk = object->chunkId;
6616 + else if(object->softDeleted)
6617 + matchingChunk = oldChunk; /* Defeat the test */
6618 + else
6619 + matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
6620 +
6621 + if(oldChunk != matchingChunk)
6622 + T(YAFFS_TRACE_ERROR,
6623 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
6624 + oldChunk,matchingChunk,tags.objectId, tags.chunkId));
6625 +
6626 + }
6627 +
6628 + if (!object) {
6629 + T(YAFFS_TRACE_ERROR,
6630 + (TSTR
6631 + ("page %d in gc has no object: %d %d %d "
6632 + TENDSTR), oldChunk,
6633 + tags.objectId, tags.chunkId, tags.byteCount));
6634 + }
6635 +
6636 + if (object && object->deleted
6637 + && tags.chunkId != 0) {
6638 + /* Data chunk in a deleted file, throw it away
6639 + * It's a soft deleted data chunk,
6640 + * No need to copy this, just forget about it and
6641 + * fix up the object.
6642 + */
6643 +
6644 + object->nDataChunks--;
6645 +
6646 + if (object->nDataChunks <= 0) {
6647 + /* remeber to clean up the object */
6648 + dev->gcCleanupList[cleanups] =
6649 + tags.objectId;
6650 + cleanups++;
6651 + }
6652 + markNAND = 0;
6653 + } else if (0
6654 + /* Todo object && object->deleted && object->nDataChunks == 0 */
6655 + ) {
6656 + /* Deleted object header with no data chunks.
6657 + * Can be discarded and the file deleted.
6658 + */
6659 + object->chunkId = 0;
6660 + yaffs_FreeTnode(object->myDev,
6661 + object->variant.
6662 + fileVariant.top);
6663 + object->variant.fileVariant.top = NULL;
6664 + yaffs_DoGenericObjectDeletion(object);
6665 +
6666 + } else if (object) {
6667 + /* It's either a data chunk in a live file or
6668 + * an ObjectHeader, so we're interested in it.
6669 + * NB Need to keep the ObjectHeaders of deleted files
6670 + * until the whole file has been deleted off
6671 + */
6672 + tags.serialNumber++;
6673 +
6674 + dev->nGCCopies++;
6675 +
6676 + if (tags.chunkId == 0) {
6677 + /* It is an object Id,
6678 + * We need to nuke the shrinkheader flags first
6679 + * We no longer want the shrinkHeader flag since its work is done
6680 + * and if it is left in place it will mess up scanning.
6681 + * Also, clear out any shadowing stuff
6682 + */
6683 +
6684 + yaffs_ObjectHeader *oh;
6685 + oh = (yaffs_ObjectHeader *)buffer;
6686 + oh->isShrink = 0;
6687 + oh->shadowsObject = -1;
6688 + tags.extraShadows = 0;
6689 + tags.extraIsShrinkHeader = 0;
6690 +
6691 + yaffs_VerifyObjectHeader(object,oh,&tags,1);
6692 + }
6693 +
6694 + newChunk =
6695 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
6696 +
6697 + if (newChunk < 0) {
6698 + retVal = YAFFS_FAIL;
6699 + } else {
6700 +
6701 + /* Ok, now fix up the Tnodes etc. */
6702 +
6703 + if (tags.chunkId == 0) {
6704 + /* It's a header */
6705 + object->chunkId = newChunk;
6706 + object->serial = tags.serialNumber;
6707 + } else {
6708 + /* It's a data chunk */
6709 + yaffs_PutChunkIntoFile
6710 + (object,
6711 + tags.chunkId,
6712 + newChunk, 0);
6713 + }
6714 + }
6715 + }
6716 +
6717 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
6718 +
6719 + }
6720 + }
6721 +
6722 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6723 +
6724 +
6725 + /* Do any required cleanups */
6726 + for (i = 0; i < cleanups; i++) {
6727 + /* Time to delete the file too */
6728 + object =
6729 + yaffs_FindObjectByNumber(dev,
6730 + dev->gcCleanupList[i]);
6731 + if (object) {
6732 + yaffs_FreeTnode(dev,
6733 + object->variant.fileVariant.
6734 + top);
6735 + object->variant.fileVariant.top = NULL;
6736 + T(YAFFS_TRACE_GC,
6737 + (TSTR
6738 + ("yaffs: About to finally delete object %d"
6739 + TENDSTR), object->objectId));
6740 + yaffs_DoGenericObjectDeletion(object);
6741 + object->myDev->nDeletedFiles--;
6742 + }
6743 +
6744 + }
6745 +
6746 + }
6747 +
6748 + yaffs_VerifyCollectedBlock(dev,bi,block);
6749 +
6750 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
6751 + T(YAFFS_TRACE_GC,
6752 + (TSTR
6753 + ("gc did not increase free chunks before %d after %d"
6754 + TENDSTR), chunksBefore, chunksAfter));
6755 + }
6756 +
6757 + dev->isDoingGC = 0;
6758 +
6759 + return YAFFS_OK;
6760 +}
6761 +
6762 +/* New garbage collector
6763 + * If we're very low on erased blocks then we do aggressive garbage collection
6764 + * otherwise we do "leasurely" garbage collection.
6765 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
6766 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
6767 + *
6768 + * The idea is to help clear out space in a more spread-out manner.
6769 + * Dunno if it really does anything useful.
6770 + */
6771 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
6772 +{
6773 + int block;
6774 + int aggressive;
6775 + int gcOk = YAFFS_OK;
6776 + int maxTries = 0;
6777 +
6778 + int checkpointBlockAdjust;
6779 +
6780 + if (dev->isDoingGC) {
6781 + /* Bail out so we don't get recursive gc */
6782 + return YAFFS_OK;
6783 + }
6784 +
6785 + /* This loop should pass the first time.
6786 + * We'll only see looping here if the erase of the collected block fails.
6787 + */
6788 +
6789 + do {
6790 + maxTries++;
6791 +
6792 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
6793 + if(checkpointBlockAdjust < 0)
6794 + checkpointBlockAdjust = 0;
6795 +
6796 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
6797 + /* We need a block soon...*/
6798 + aggressive = 1;
6799 + } else {
6800 + /* We're in no hurry */
6801 + aggressive = 0;
6802 + }
6803 +
6804 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6805 +
6806 + if (block > 0) {
6807 + dev->garbageCollections++;
6808 + if (!aggressive) {
6809 + dev->passiveGarbageCollections++;
6810 + }
6811 +
6812 + T(YAFFS_TRACE_GC,
6813 + (TSTR
6814 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6815 + dev->nErasedBlocks, aggressive));
6816 +
6817 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6818 + }
6819 +
6820 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6821 + T(YAFFS_TRACE_GC,
6822 + (TSTR
6823 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6824 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6825 + }
6826 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6827 + && (maxTries < 2));
6828 +
6829 + return aggressive ? gcOk : YAFFS_OK;
6830 +}
6831 +
6832 +/*------------------------- TAGS --------------------------------*/
6833 +
6834 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6835 + int chunkInObject)
6836 +{
6837 + return (tags->chunkId == chunkInObject &&
6838 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6839 +
6840 +}
6841 +
6842 +
6843 +/*-------------------- Data file manipulation -----------------*/
6844 +
6845 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6846 + yaffs_ExtendedTags * tags)
6847 +{
6848 + /*Get the Tnode, then get the level 0 offset chunk offset */
6849 + yaffs_Tnode *tn;
6850 + int theChunk = -1;
6851 + yaffs_ExtendedTags localTags;
6852 + int retVal = -1;
6853 +
6854 + yaffs_Device *dev = in->myDev;
6855 +
6856 + if (!tags) {
6857 + /* Passed a NULL, so use our own tags space */
6858 + tags = &localTags;
6859 + }
6860 +
6861 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6862 +
6863 + if (tn) {
6864 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6865 +
6866 + retVal =
6867 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6868 + chunkInInode);
6869 + }
6870 + return retVal;
6871 +}
6872 +
6873 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6874 + yaffs_ExtendedTags * tags)
6875 +{
6876 + /* Get the Tnode, then get the level 0 offset chunk offset */
6877 + yaffs_Tnode *tn;
6878 + int theChunk = -1;
6879 + yaffs_ExtendedTags localTags;
6880 +
6881 + yaffs_Device *dev = in->myDev;
6882 + int retVal = -1;
6883 +
6884 + if (!tags) {
6885 + /* Passed a NULL, so use our own tags space */
6886 + tags = &localTags;
6887 + }
6888 +
6889 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6890 +
6891 + if (tn) {
6892 +
6893 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6894 +
6895 + retVal =
6896 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6897 + chunkInInode);
6898 +
6899 + /* Delete the entry in the filestructure (if found) */
6900 + if (retVal != -1) {
6901 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6902 + }
6903 + } else {
6904 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6905 + }
6906 +
6907 + if (retVal == -1) {
6908 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6909 + }
6910 + return retVal;
6911 +}
6912 +
6913 +#ifdef YAFFS_PARANOID
6914 +
6915 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6916 +{
6917 + int chunk;
6918 + int nChunks;
6919 + int fSize;
6920 + int failed = 0;
6921 + int objId;
6922 + yaffs_Tnode *tn;
6923 + yaffs_Tags localTags;
6924 + yaffs_Tags *tags = &localTags;
6925 + int theChunk;
6926 + int chunkDeleted;
6927 +
6928 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6929 + /* T(("Object not a file\n")); */
6930 + return YAFFS_FAIL;
6931 + }
6932 +
6933 + objId = in->objectId;
6934 + fSize = in->variant.fileVariant.fileSize;
6935 + nChunks =
6936 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6937 +
6938 + for (chunk = 1; chunk <= nChunks; chunk++) {
6939 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6940 + chunk);
6941 +
6942 + if (tn) {
6943 +
6944 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6945 +
6946 + if (yaffs_CheckChunkBits
6947 + (dev, theChunk / dev->nChunksPerBlock,
6948 + theChunk % dev->nChunksPerBlock)) {
6949 +
6950 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6951 + tags,
6952 + &chunkDeleted);
6953 + if (yaffs_TagsMatch
6954 + (tags, in->objectId, chunk, chunkDeleted)) {
6955 + /* found it; */
6956 +
6957 + }
6958 + } else {
6959 +
6960 + failed = 1;
6961 + }
6962 +
6963 + } else {
6964 + /* T(("No level 0 found for %d\n", chunk)); */
6965 + }
6966 + }
6967 +
6968 + return failed ? YAFFS_FAIL : YAFFS_OK;
6969 +}
6970 +
6971 +#endif
6972 +
6973 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
6974 + int chunkInNAND, int inScan)
6975 +{
6976 + /* NB inScan is zero unless scanning.
6977 + * For forward scanning, inScan is > 0;
6978 + * for backward scanning inScan is < 0
6979 + */
6980 +
6981 + yaffs_Tnode *tn;
6982 + yaffs_Device *dev = in->myDev;
6983 + int existingChunk;
6984 + yaffs_ExtendedTags existingTags;
6985 + yaffs_ExtendedTags newTags;
6986 + unsigned existingSerial, newSerial;
6987 +
6988 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6989 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
6990 + * If it is not during Scanning then something went wrong!
6991 + */
6992 + if (!inScan) {
6993 + T(YAFFS_TRACE_ERROR,
6994 + (TSTR
6995 + ("yaffs tragedy:attempt to put data chunk into a non-file"
6996 + TENDSTR)));
6997 + YBUG();
6998 + }
6999 +
7000 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7001 + return YAFFS_OK;
7002 + }
7003 +
7004 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7005 + &in->variant.fileVariant,
7006 + chunkInInode,
7007 + NULL);
7008 + if (!tn) {
7009 + return YAFFS_FAIL;
7010 + }
7011 +
7012 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
7013 +
7014 + if (inScan != 0) {
7015 + /* If we're scanning then we need to test for duplicates
7016 + * NB This does not need to be efficient since it should only ever
7017 + * happen when the power fails during a write, then only one
7018 + * chunk should ever be affected.
7019 + *
7020 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7021 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7022 + */
7023 +
7024 + if (existingChunk != 0) {
7025 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7026 + * thus we have to do a FindChunkInFile to get the real chunk id.
7027 + *
7028 + * We have a duplicate now we need to decide which one to use:
7029 + *
7030 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7031 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7032 + * YAFFS1: Get both sets of tags and compare serial numbers.
7033 + */
7034 +
7035 + if (inScan > 0) {
7036 + /* Only do this for forward scanning */
7037 + yaffs_ReadChunkWithTagsFromNAND(dev,
7038 + chunkInNAND,
7039 + NULL, &newTags);
7040 +
7041 + /* Do a proper find */
7042 + existingChunk =
7043 + yaffs_FindChunkInFile(in, chunkInInode,
7044 + &existingTags);
7045 + }
7046 +
7047 + if (existingChunk <= 0) {
7048 + /*Hoosterman - how did this happen? */
7049 +
7050 + T(YAFFS_TRACE_ERROR,
7051 + (TSTR
7052 + ("yaffs tragedy: existing chunk < 0 in scan"
7053 + TENDSTR)));
7054 +
7055 + }
7056 +
7057 + /* NB The deleted flags should be false, otherwise the chunks will
7058 + * not be loaded during a scan
7059 + */
7060 +
7061 + newSerial = newTags.serialNumber;
7062 + existingSerial = existingTags.serialNumber;
7063 +
7064 + if ((inScan > 0) &&
7065 + (in->myDev->isYaffs2 ||
7066 + existingChunk <= 0 ||
7067 + ((existingSerial + 1) & 3) == newSerial)) {
7068 + /* Forward scanning.
7069 + * Use new
7070 + * Delete the old one and drop through to update the tnode
7071 + */
7072 + yaffs_DeleteChunk(dev, existingChunk, 1,
7073 + __LINE__);
7074 + } else {
7075 + /* Backward scanning or we want to use the existing one
7076 + * Use existing.
7077 + * Delete the new one and return early so that the tnode isn't changed
7078 + */
7079 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7080 + __LINE__);
7081 + return YAFFS_OK;
7082 + }
7083 + }
7084 +
7085 + }
7086 +
7087 + if (existingChunk == 0) {
7088 + in->nDataChunks++;
7089 + }
7090 +
7091 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
7092 +
7093 + return YAFFS_OK;
7094 +}
7095 +
7096 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
7097 + __u8 * buffer)
7098 +{
7099 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7100 +
7101 + if (chunkInNAND >= 0) {
7102 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7103 + buffer,NULL);
7104 + } else {
7105 + T(YAFFS_TRACE_NANDACCESS,
7106 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7107 + chunkInNAND));
7108 + /* get sane (zero) data if you read a hole */
7109 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7110 + return 0;
7111 + }
7112 +
7113 +}
7114 +
7115 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
7116 +{
7117 + int block;
7118 + int page;
7119 + yaffs_ExtendedTags tags;
7120 + yaffs_BlockInfo *bi;
7121 +
7122 + if (chunkId <= 0)
7123 + return;
7124 +
7125 +
7126 + dev->nDeletions++;
7127 + block = chunkId / dev->nChunksPerBlock;
7128 + page = chunkId % dev->nChunksPerBlock;
7129 +
7130 +
7131 + if(!yaffs_CheckChunkBit(dev,block,page))
7132 + T(YAFFS_TRACE_VERIFY,
7133 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7134 + chunkId));
7135 +
7136 + bi = yaffs_GetBlockInfo(dev, block);
7137 +
7138 + T(YAFFS_TRACE_DELETION,
7139 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7140 +
7141 + if (markNAND &&
7142 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7143 +
7144 + yaffs_InitialiseTags(&tags);
7145 +
7146 + tags.chunkDeleted = 1;
7147 +
7148 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7149 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7150 + } else {
7151 + dev->nUnmarkedDeletions++;
7152 + }
7153 +
7154 + /* Pull out of the management area.
7155 + * If the whole block became dirty, this will kick off an erasure.
7156 + */
7157 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7158 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7159 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7160 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7161 + dev->nFreeChunks++;
7162 +
7163 + yaffs_ClearChunkBit(dev, block, page);
7164 +
7165 + bi->pagesInUse--;
7166 +
7167 + if (bi->pagesInUse == 0 &&
7168 + !bi->hasShrinkHeader &&
7169 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7170 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7171 + yaffs_BlockBecameDirty(dev, block);
7172 + }
7173 +
7174 + } else {
7175 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
7176 + }
7177 +
7178 +}
7179 +
7180 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
7181 + const __u8 * buffer, int nBytes,
7182 + int useReserve)
7183 +{
7184 + /* Find old chunk Need to do this to get serial number
7185 + * Write new one and patch into tree.
7186 + * Invalidate old tags.
7187 + */
7188 +
7189 + int prevChunkId;
7190 + yaffs_ExtendedTags prevTags;
7191 +
7192 + int newChunkId;
7193 + yaffs_ExtendedTags newTags;
7194 +
7195 + yaffs_Device *dev = in->myDev;
7196 +
7197 + yaffs_CheckGarbageCollection(dev);
7198 +
7199 + /* Get the previous chunk at this location in the file if it exists */
7200 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7201 +
7202 + /* Set up new tags */
7203 + yaffs_InitialiseTags(&newTags);
7204 +
7205 + newTags.chunkId = chunkInInode;
7206 + newTags.objectId = in->objectId;
7207 + newTags.serialNumber =
7208 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7209 + newTags.byteCount = nBytes;
7210 +
7211 + newChunkId =
7212 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7213 + useReserve);
7214 +
7215 + if (newChunkId >= 0) {
7216 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7217 +
7218 + if (prevChunkId >= 0) {
7219 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7220 +
7221 + }
7222 +
7223 + yaffs_CheckFileSanity(in);
7224 + }
7225 + return newChunkId;
7226 +
7227 +}
7228 +
7229 +/* UpdateObjectHeader updates the header on NAND for an object.
7230 + * If name is not NULL, then that new name is used.
7231 + */
7232 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
7233 + int isShrink, int shadows)
7234 +{
7235 +
7236 + yaffs_BlockInfo *bi;
7237 +
7238 + yaffs_Device *dev = in->myDev;
7239 +
7240 + int prevChunkId;
7241 + int retVal = 0;
7242 + int result = 0;
7243 +
7244 + int newChunkId;
7245 + yaffs_ExtendedTags newTags;
7246 + yaffs_ExtendedTags oldTags;
7247 +
7248 + __u8 *buffer = NULL;
7249 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7250 +
7251 + yaffs_ObjectHeader *oh = NULL;
7252 +
7253 + yaffs_strcpy(oldName,"silly old name");
7254 +
7255 + if (!in->fake || force) {
7256 +
7257 + yaffs_CheckGarbageCollection(dev);
7258 + yaffs_CheckObjectDetailsLoaded(in);
7259 +
7260 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7261 + oh = (yaffs_ObjectHeader *) buffer;
7262 +
7263 + prevChunkId = in->chunkId;
7264 +
7265 + if (prevChunkId >= 0) {
7266 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7267 + buffer, &oldTags);
7268 +
7269 + yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
7270 +
7271 + memcpy(oldName, oh->name, sizeof(oh->name));
7272 + }
7273 +
7274 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7275 +
7276 + oh->type = in->variantType;
7277 + oh->yst_mode = in->yst_mode;
7278 + oh->shadowsObject = shadows;
7279 +
7280 +#ifdef CONFIG_YAFFS_WINCE
7281 + oh->win_atime[0] = in->win_atime[0];
7282 + oh->win_ctime[0] = in->win_ctime[0];
7283 + oh->win_mtime[0] = in->win_mtime[0];
7284 + oh->win_atime[1] = in->win_atime[1];
7285 + oh->win_ctime[1] = in->win_ctime[1];
7286 + oh->win_mtime[1] = in->win_mtime[1];
7287 +#else
7288 + oh->yst_uid = in->yst_uid;
7289 + oh->yst_gid = in->yst_gid;
7290 + oh->yst_atime = in->yst_atime;
7291 + oh->yst_mtime = in->yst_mtime;
7292 + oh->yst_ctime = in->yst_ctime;
7293 + oh->yst_rdev = in->yst_rdev;
7294 +#endif
7295 + if (in->parent) {
7296 + oh->parentObjectId = in->parent->objectId;
7297 + } else {
7298 + oh->parentObjectId = 0;
7299 + }
7300 +
7301 + if (name && *name) {
7302 + memset(oh->name, 0, sizeof(oh->name));
7303 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7304 + } else if (prevChunkId>=0) {
7305 + memcpy(oh->name, oldName, sizeof(oh->name));
7306 + } else {
7307 + memset(oh->name, 0, sizeof(oh->name));
7308 + }
7309 +
7310 + oh->isShrink = isShrink;
7311 +
7312 + switch (in->variantType) {
7313 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7314 + /* Should not happen */
7315 + break;
7316 + case YAFFS_OBJECT_TYPE_FILE:
7317 + oh->fileSize =
7318 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7319 + || oh->parentObjectId ==
7320 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7321 + fileVariant.fileSize;
7322 + break;
7323 + case YAFFS_OBJECT_TYPE_HARDLINK:
7324 + oh->equivalentObjectId =
7325 + in->variant.hardLinkVariant.equivalentObjectId;
7326 + break;
7327 + case YAFFS_OBJECT_TYPE_SPECIAL:
7328 + /* Do nothing */
7329 + break;
7330 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7331 + /* Do nothing */
7332 + break;
7333 + case YAFFS_OBJECT_TYPE_SYMLINK:
7334 + yaffs_strncpy(oh->alias,
7335 + in->variant.symLinkVariant.alias,
7336 + YAFFS_MAX_ALIAS_LENGTH);
7337 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7338 + break;
7339 + }
7340 +
7341 + /* Tags */
7342 + yaffs_InitialiseTags(&newTags);
7343 + in->serial++;
7344 + newTags.chunkId = 0;
7345 + newTags.objectId = in->objectId;
7346 + newTags.serialNumber = in->serial;
7347 +
7348 + /* Add extra info for file header */
7349 +
7350 + newTags.extraHeaderInfoAvailable = 1;
7351 + newTags.extraParentObjectId = oh->parentObjectId;
7352 + newTags.extraFileLength = oh->fileSize;
7353 + newTags.extraIsShrinkHeader = oh->isShrink;
7354 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7355 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7356 + newTags.extraObjectType = in->variantType;
7357 +
7358 + yaffs_VerifyObjectHeader(in,oh,&newTags,1);
7359 +
7360 + /* Create new chunk in NAND */
7361 + newChunkId =
7362 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7363 + (prevChunkId >= 0) ? 1 : 0);
7364 +
7365 + if (newChunkId >= 0) {
7366 +
7367 + in->chunkId = newChunkId;
7368 +
7369 + if (prevChunkId >= 0) {
7370 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7371 + __LINE__);
7372 + }
7373 +
7374 + if(!yaffs_ObjectHasCachedWriteData(in))
7375 + in->dirty = 0;
7376 +
7377 + /* If this was a shrink, then mark the block that the chunk lives on */
7378 + if (isShrink) {
7379 + bi = yaffs_GetBlockInfo(in->myDev,
7380 + newChunkId /in->myDev-> nChunksPerBlock);
7381 + bi->hasShrinkHeader = 1;
7382 + }
7383 +
7384 + }
7385 +
7386 + retVal = newChunkId;
7387 +
7388 + }
7389 +
7390 + if (buffer)
7391 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7392 +
7393 + return retVal;
7394 +}
7395 +
7396 +/*------------------------ Short Operations Cache ----------------------------------------
7397 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7398 + * reads might be short sequential reads, and a lot of writes may be short
7399 + * sequential writes. eg. scanning/writing a jpeg file.
7400 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7401 + * with dumb-as-a-rock code.
7402 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7403 + * buffering.
7404 + *
7405 + * There are a limited number (~10) of cache chunks per device so that we don't
7406 + * need a very intelligent search.
7407 + */
7408 +
7409 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7410 +{
7411 + yaffs_Device *dev = obj->myDev;
7412 + int i;
7413 + yaffs_ChunkCache *cache;
7414 + int nCaches = obj->myDev->nShortOpCaches;
7415 +
7416 + for(i = 0; i < nCaches; i++){
7417 + cache = &dev->srCache[i];
7418 + if (cache->object == obj &&
7419 + cache->dirty)
7420 + return 1;
7421 + }
7422 +
7423 + return 0;
7424 +}
7425 +
7426 +
7427 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
7428 +{
7429 + yaffs_Device *dev = obj->myDev;
7430 + int lowest = -99; /* Stop compiler whining. */
7431 + int i;
7432 + yaffs_ChunkCache *cache;
7433 + int chunkWritten = 0;
7434 + int nCaches = obj->myDev->nShortOpCaches;
7435 +
7436 + if (nCaches > 0) {
7437 + do {
7438 + cache = NULL;
7439 +
7440 + /* Find the dirty cache for this object with the lowest chunk id. */
7441 + for (i = 0; i < nCaches; i++) {
7442 + if (dev->srCache[i].object == obj &&
7443 + dev->srCache[i].dirty) {
7444 + if (!cache
7445 + || dev->srCache[i].chunkId <
7446 + lowest) {
7447 + cache = &dev->srCache[i];
7448 + lowest = cache->chunkId;
7449 + }
7450 + }
7451 + }
7452 +
7453 + if (cache && !cache->locked) {
7454 + /* Write it out and free it up */
7455 +
7456 + chunkWritten =
7457 + yaffs_WriteChunkDataToObject(cache->object,
7458 + cache->chunkId,
7459 + cache->data,
7460 + cache->nBytes,
7461 + 1);
7462 + cache->dirty = 0;
7463 + cache->object = NULL;
7464 + }
7465 +
7466 + } while (cache && chunkWritten > 0);
7467 +
7468 + if (cache) {
7469 + /* Hoosterman, disk full while writing cache out. */
7470 + T(YAFFS_TRACE_ERROR,
7471 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7472 +
7473 + }
7474 + }
7475 +
7476 +}
7477 +
7478 +/*yaffs_FlushEntireDeviceCache(dev)
7479 + *
7480 + *
7481 + */
7482 +
7483 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7484 +{
7485 + yaffs_Object *obj;
7486 + int nCaches = dev->nShortOpCaches;
7487 + int i;
7488 +
7489 + /* Find a dirty object in the cache and flush it...
7490 + * until there are no further dirty objects.
7491 + */
7492 + do {
7493 + obj = NULL;
7494 + for( i = 0; i < nCaches && !obj; i++) {
7495 + if (dev->srCache[i].object &&
7496 + dev->srCache[i].dirty)
7497 + obj = dev->srCache[i].object;
7498 +
7499 + }
7500 + if(obj)
7501 + yaffs_FlushFilesChunkCache(obj);
7502 +
7503 + } while(obj);
7504 +
7505 +}
7506 +
7507 +
7508 +/* Grab us a cache chunk for use.
7509 + * First look for an empty one.
7510 + * Then look for the least recently used non-dirty one.
7511 + * Then look for the least recently used dirty one...., flush and look again.
7512 + */
7513 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
7514 +{
7515 + int i;
7516 + int usage;
7517 + int theOne;
7518 +
7519 + if (dev->nShortOpCaches > 0) {
7520 + for (i = 0; i < dev->nShortOpCaches; i++) {
7521 + if (!dev->srCache[i].object)
7522 + return &dev->srCache[i];
7523 + }
7524 +
7525 + return NULL;
7526 +
7527 + theOne = -1;
7528 + usage = 0; /* just to stop the compiler grizzling */
7529 +
7530 + for (i = 0; i < dev->nShortOpCaches; i++) {
7531 + if (!dev->srCache[i].dirty &&
7532 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
7533 + theOne < 0)) {
7534 + usage = dev->srCache[i].lastUse;
7535 + theOne = i;
7536 + }
7537 + }
7538 +
7539 +
7540 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
7541 + } else {
7542 + return NULL;
7543 + }
7544 +
7545 +}
7546 +
7547 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
7548 +{
7549 + yaffs_ChunkCache *cache;
7550 + yaffs_Object *theObj;
7551 + int usage;
7552 + int i;
7553 + int pushout;
7554 +
7555 + if (dev->nShortOpCaches > 0) {
7556 + /* Try find a non-dirty one... */
7557 +
7558 + cache = yaffs_GrabChunkCacheWorker(dev);
7559 +
7560 + if (!cache) {
7561 + /* They were all dirty, find the last recently used object and flush
7562 + * its cache, then find again.
7563 + * NB what's here is not very accurate, we actually flush the object
7564 + * the last recently used page.
7565 + */
7566 +
7567 + /* With locking we can't assume we can use entry zero */
7568 +
7569 + theObj = NULL;
7570 + usage = -1;
7571 + cache = NULL;
7572 + pushout = -1;
7573 +
7574 + for (i = 0; i < dev->nShortOpCaches; i++) {
7575 + if (dev->srCache[i].object &&
7576 + !dev->srCache[i].locked &&
7577 + (dev->srCache[i].lastUse < usage || !cache))
7578 + {
7579 + usage = dev->srCache[i].lastUse;
7580 + theObj = dev->srCache[i].object;
7581 + cache = &dev->srCache[i];
7582 + pushout = i;
7583 + }
7584 + }
7585 +
7586 + if (!cache || cache->dirty) {
7587 + /* Flush and try again */
7588 + yaffs_FlushFilesChunkCache(theObj);
7589 + cache = yaffs_GrabChunkCacheWorker(dev);
7590 + }
7591 +
7592 + }
7593 + return cache;
7594 + } else
7595 + return NULL;
7596 +
7597 +}
7598 +
7599 +/* Find a cached chunk */
7600 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
7601 + int chunkId)
7602 +{
7603 + yaffs_Device *dev = obj->myDev;
7604 + int i;
7605 + if (dev->nShortOpCaches > 0) {
7606 + for (i = 0; i < dev->nShortOpCaches; i++) {
7607 + if (dev->srCache[i].object == obj &&
7608 + dev->srCache[i].chunkId == chunkId) {
7609 + dev->cacheHits++;
7610 +
7611 + return &dev->srCache[i];
7612 + }
7613 + }
7614 + }
7615 + return NULL;
7616 +}
7617 +
7618 +/* Mark the chunk for the least recently used algorithym */
7619 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
7620 + int isAWrite)
7621 +{
7622 +
7623 + if (dev->nShortOpCaches > 0) {
7624 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
7625 + /* Reset the cache usages */
7626 + int i;
7627 + for (i = 1; i < dev->nShortOpCaches; i++) {
7628 + dev->srCache[i].lastUse = 0;
7629 + }
7630 + dev->srLastUse = 0;
7631 + }
7632 +
7633 + dev->srLastUse++;
7634 +
7635 + cache->lastUse = dev->srLastUse;
7636 +
7637 + if (isAWrite) {
7638 + cache->dirty = 1;
7639 + }
7640 + }
7641 +}
7642 +
7643 +/* Invalidate a single cache page.
7644 + * Do this when a whole page gets written,
7645 + * ie the short cache for this page is no longer valid.
7646 + */
7647 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
7648 +{
7649 + if (object->myDev->nShortOpCaches > 0) {
7650 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
7651 +
7652 + if (cache) {
7653 + cache->object = NULL;
7654 + }
7655 + }
7656 +}
7657 +
7658 +/* Invalidate all the cache pages associated with this object
7659 + * Do this whenever ther file is deleted or resized.
7660 + */
7661 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
7662 +{
7663 + int i;
7664 + yaffs_Device *dev = in->myDev;
7665 +
7666 + if (dev->nShortOpCaches > 0) {
7667 + /* Invalidate it. */
7668 + for (i = 0; i < dev->nShortOpCaches; i++) {
7669 + if (dev->srCache[i].object == in) {
7670 + dev->srCache[i].object = NULL;
7671 + }
7672 + }
7673 + }
7674 +}
7675 +
7676 +/*--------------------- Checkpointing --------------------*/
7677 +
7678 +
7679 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
7680 +{
7681 + yaffs_CheckpointValidity cp;
7682 +
7683 + memset(&cp,0,sizeof(cp));
7684 +
7685 + cp.structType = sizeof(cp);
7686 + cp.magic = YAFFS_MAGIC;
7687 + cp.version = YAFFS_CHECKPOINT_VERSION;
7688 + cp.head = (head) ? 1 : 0;
7689 +
7690 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
7691 + 1 : 0;
7692 +}
7693 +
7694 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
7695 +{
7696 + yaffs_CheckpointValidity cp;
7697 + int ok;
7698 +
7699 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7700 +
7701 + if(ok)
7702 + ok = (cp.structType == sizeof(cp)) &&
7703 + (cp.magic == YAFFS_MAGIC) &&
7704 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
7705 + (cp.head == ((head) ? 1 : 0));
7706 + return ok ? 1 : 0;
7707 +}
7708 +
7709 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
7710 + yaffs_Device *dev)
7711 +{
7712 + cp->nErasedBlocks = dev->nErasedBlocks;
7713 + cp->allocationBlock = dev->allocationBlock;
7714 + cp->allocationPage = dev->allocationPage;
7715 + cp->nFreeChunks = dev->nFreeChunks;
7716 +
7717 + cp->nDeletedFiles = dev->nDeletedFiles;
7718 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
7719 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
7720 + cp->sequenceNumber = dev->sequenceNumber;
7721 + cp->oldestDirtySequence = dev->oldestDirtySequence;
7722 +
7723 +}
7724 +
7725 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
7726 + yaffs_CheckpointDevice *cp)
7727 +{
7728 + dev->nErasedBlocks = cp->nErasedBlocks;
7729 + dev->allocationBlock = cp->allocationBlock;
7730 + dev->allocationPage = cp->allocationPage;
7731 + dev->nFreeChunks = cp->nFreeChunks;
7732 +
7733 + dev->nDeletedFiles = cp->nDeletedFiles;
7734 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
7735 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
7736 + dev->sequenceNumber = cp->sequenceNumber;
7737 + dev->oldestDirtySequence = cp->oldestDirtySequence;
7738 +}
7739 +
7740 +
7741 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
7742 +{
7743 + yaffs_CheckpointDevice cp;
7744 + __u32 nBytes;
7745 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7746 +
7747 + int ok;
7748 +
7749 + /* Write device runtime values*/
7750 + yaffs_DeviceToCheckpointDevice(&cp,dev);
7751 + cp.structType = sizeof(cp);
7752 +
7753 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7754 +
7755 + /* Write block info */
7756 + if(ok) {
7757 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7758 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
7759 + }
7760 +
7761 + /* Write chunk bits */
7762 + if(ok) {
7763 + nBytes = nBlocks * dev->chunkBitmapStride;
7764 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
7765 + }
7766 + return ok ? 1 : 0;
7767 +
7768 +}
7769 +
7770 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
7771 +{
7772 + yaffs_CheckpointDevice cp;
7773 + __u32 nBytes;
7774 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7775 +
7776 + int ok;
7777 +
7778 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7779 + if(!ok)
7780 + return 0;
7781 +
7782 + if(cp.structType != sizeof(cp))
7783 + return 0;
7784 +
7785 +
7786 + yaffs_CheckpointDeviceToDevice(dev,&cp);
7787 +
7788 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7789 +
7790 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
7791 +
7792 + if(!ok)
7793 + return 0;
7794 + nBytes = nBlocks * dev->chunkBitmapStride;
7795 +
7796 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
7797 +
7798 + return ok ? 1 : 0;
7799 +}
7800 +
7801 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
7802 + yaffs_Object *obj)
7803 +{
7804 +
7805 + cp->objectId = obj->objectId;
7806 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
7807 + cp->chunkId = obj->chunkId;
7808 + cp->variantType = obj->variantType;
7809 + cp->deleted = obj->deleted;
7810 + cp->softDeleted = obj->softDeleted;
7811 + cp->unlinked = obj->unlinked;
7812 + cp->fake = obj->fake;
7813 + cp->renameAllowed = obj->renameAllowed;
7814 + cp->unlinkAllowed = obj->unlinkAllowed;
7815 + cp->serial = obj->serial;
7816 + cp->nDataChunks = obj->nDataChunks;
7817 +
7818 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7819 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7820 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7821 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7822 +}
7823 +
7824 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7825 +{
7826 +
7827 + yaffs_Object *parent;
7828 +
7829 + obj->objectId = cp->objectId;
7830 +
7831 + if(cp->parentId)
7832 + parent = yaffs_FindOrCreateObjectByNumber(
7833 + obj->myDev,
7834 + cp->parentId,
7835 + YAFFS_OBJECT_TYPE_DIRECTORY);
7836 + else
7837 + parent = NULL;
7838 +
7839 + if(parent)
7840 + yaffs_AddObjectToDirectory(parent, obj);
7841 +
7842 + obj->chunkId = cp->chunkId;
7843 + obj->variantType = cp->variantType;
7844 + obj->deleted = cp->deleted;
7845 + obj->softDeleted = cp->softDeleted;
7846 + obj->unlinked = cp->unlinked;
7847 + obj->fake = cp->fake;
7848 + obj->renameAllowed = cp->renameAllowed;
7849 + obj->unlinkAllowed = cp->unlinkAllowed;
7850 + obj->serial = cp->serial;
7851 + obj->nDataChunks = cp->nDataChunks;
7852 +
7853 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7854 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7855 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7856 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7857 +
7858 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7859 + obj->lazyLoaded = 1;
7860 +}
7861 +
7862 +
7863 +
7864 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7865 + __u32 level, int chunkOffset)
7866 +{
7867 + int i;
7868 + yaffs_Device *dev = in->myDev;
7869 + int ok = 1;
7870 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7871 +
7872 + if (tn) {
7873 + if (level > 0) {
7874 +
7875 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7876 + if (tn->internal[i]) {
7877 + ok = yaffs_CheckpointTnodeWorker(in,
7878 + tn->internal[i],
7879 + level - 1,
7880 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7881 + }
7882 + }
7883 + } else if (level == 0) {
7884 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7885 + /* printf("write tnode at %d\n",baseOffset); */
7886 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7887 + if(ok)
7888 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7889 + }
7890 + }
7891 +
7892 + return ok;
7893 +
7894 +}
7895 +
7896 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7897 +{
7898 + __u32 endMarker = ~0;
7899 + int ok = 1;
7900 +
7901 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7902 + ok = yaffs_CheckpointTnodeWorker(obj,
7903 + obj->variant.fileVariant.top,
7904 + obj->variant.fileVariant.topLevel,
7905 + 0);
7906 + if(ok)
7907 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7908 + sizeof(endMarker));
7909 + }
7910 +
7911 + return ok ? 1 : 0;
7912 +}
7913 +
7914 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7915 +{
7916 + __u32 baseChunk;
7917 + int ok = 1;
7918 + yaffs_Device *dev = obj->myDev;
7919 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7920 + yaffs_Tnode *tn;
7921 + int nread = 0;
7922 +
7923 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7924 +
7925 + while(ok && (~baseChunk)){
7926 + nread++;
7927 + /* Read level 0 tnode */
7928 +
7929 +
7930 + /* printf("read tnode at %d\n",baseChunk); */
7931 + tn = yaffs_GetTnodeRaw(dev);
7932 + if(tn)
7933 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7934 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7935 + else
7936 + ok = 0;
7937 +
7938 + if(tn && ok){
7939 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7940 + fileStructPtr,
7941 + baseChunk,
7942 + tn) ? 1 : 0;
7943 +
7944 + }
7945 +
7946 + if(ok)
7947 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7948 +
7949 + }
7950 +
7951 + T(YAFFS_TRACE_CHECKPOINT,(
7952 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
7953 + nread,baseChunk,ok));
7954 +
7955 + return ok ? 1 : 0;
7956 +}
7957 +
7958 +
7959 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7960 +{
7961 + yaffs_Object *obj;
7962 + yaffs_CheckpointObject cp;
7963 + int i;
7964 + int ok = 1;
7965 + struct list_head *lh;
7966 +
7967 +
7968 + /* Iterate through the objects in each hash entry,
7969 + * dumping them to the checkpointing stream.
7970 + */
7971 +
7972 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
7973 + list_for_each(lh, &dev->objectBucket[i].list) {
7974 + if (lh) {
7975 + obj = list_entry(lh, yaffs_Object, hashLink);
7976 + if (!obj->deferedFree) {
7977 + yaffs_ObjectToCheckpointObject(&cp,obj);
7978 + cp.structType = sizeof(cp);
7979 +
7980 + T(YAFFS_TRACE_CHECKPOINT,(
7981 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7982 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7983 +
7984 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7985 +
7986 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7987 + ok = yaffs_WriteCheckpointTnodes(obj);
7988 + }
7989 + }
7990 + }
7991 + }
7992 + }
7993 +
7994 + /* Dump end of list */
7995 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
7996 + cp.structType = sizeof(cp);
7997 +
7998 + if(ok)
7999 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8000 +
8001 + return ok ? 1 : 0;
8002 +}
8003 +
8004 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8005 +{
8006 + yaffs_Object *obj;
8007 + yaffs_CheckpointObject cp;
8008 + int ok = 1;
8009 + int done = 0;
8010 + yaffs_Object *hardList = NULL;
8011 +
8012 + while(ok && !done) {
8013 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
8014 + if(cp.structType != sizeof(cp)) {
8015 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
8016 + cp.structType,sizeof(cp),ok));
8017 + ok = 0;
8018 + }
8019 +
8020 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8021 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
8022 +
8023 + if(ok && cp.objectId == ~0)
8024 + done = 1;
8025 + else if(ok){
8026 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
8027 + if(obj) {
8028 + yaffs_CheckpointObjectToObject(obj,&cp);
8029 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8030 + ok = yaffs_ReadCheckpointTnodes(obj);
8031 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8032 + obj->hardLinks.next =
8033 + (struct list_head *)
8034 + hardList;
8035 + hardList = obj;
8036 + }
8037 +
8038 + }
8039 + }
8040 + }
8041 +
8042 + if(ok)
8043 + yaffs_HardlinkFixup(dev,hardList);
8044 +
8045 + return ok ? 1 : 0;
8046 +}
8047 +
8048 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8049 +{
8050 + __u32 checkpointSum;
8051 + int ok;
8052 +
8053 + yaffs_GetCheckpointSum(dev,&checkpointSum);
8054 +
8055 + ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
8056 +
8057 + if(!ok)
8058 + return 0;
8059 +
8060 + return 1;
8061 +}
8062 +
8063 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8064 +{
8065 + __u32 checkpointSum0;
8066 + __u32 checkpointSum1;
8067 + int ok;
8068 +
8069 + yaffs_GetCheckpointSum(dev,&checkpointSum0);
8070 +
8071 + ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8072 +
8073 + if(!ok)
8074 + return 0;
8075 +
8076 + if(checkpointSum0 != checkpointSum1)
8077 + return 0;
8078 +
8079 + return 1;
8080 +}
8081 +
8082 +
8083 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8084 +{
8085 +
8086 + int ok = 1;
8087 +
8088 + if(dev->skipCheckpointWrite || !dev->isYaffs2){
8089 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
8090 + ok = 0;
8091 + }
8092 +
8093 + if(ok)
8094 + ok = yaffs_CheckpointOpen(dev,1);
8095 +
8096 + if(ok){
8097 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8098 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
8099 + }
8100 + if(ok){
8101 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
8102 + ok = yaffs_WriteCheckpointDevice(dev);
8103 + }
8104 + if(ok){
8105 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
8106 + ok = yaffs_WriteCheckpointObjects(dev);
8107 + }
8108 + if(ok){
8109 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8110 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
8111 + }
8112 +
8113 + if(ok){
8114 + ok = yaffs_WriteCheckpointSum(dev);
8115 + }
8116 +
8117 +
8118 + if(!yaffs_CheckpointClose(dev))
8119 + ok = 0;
8120 +
8121 + if(ok)
8122 + dev->isCheckpointed = 1;
8123 + else
8124 + dev->isCheckpointed = 0;
8125 +
8126 + return dev->isCheckpointed;
8127 +}
8128 +
8129 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8130 +{
8131 + int ok = 1;
8132 +
8133 + if(dev->skipCheckpointRead || !dev->isYaffs2){
8134 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
8135 + ok = 0;
8136 + }
8137 +
8138 + if(ok)
8139 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
8140 +
8141 + if(ok){
8142 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8143 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
8144 + }
8145 + if(ok){
8146 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
8147 + ok = yaffs_ReadCheckpointDevice(dev);
8148 + }
8149 + if(ok){
8150 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
8151 + ok = yaffs_ReadCheckpointObjects(dev);
8152 + }
8153 + if(ok){
8154 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8155 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
8156 + }
8157 +
8158 + if(ok){
8159 + ok = yaffs_ReadCheckpointSum(dev);
8160 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
8161 + }
8162 +
8163 + if(!yaffs_CheckpointClose(dev))
8164 + ok = 0;
8165 +
8166 + if(ok)
8167 + dev->isCheckpointed = 1;
8168 + else
8169 + dev->isCheckpointed = 0;
8170 +
8171 + return ok ? 1 : 0;
8172 +
8173 +}
8174 +
8175 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8176 +{
8177 + if(dev->isCheckpointed ||
8178 + dev->blocksInCheckpoint > 0){
8179 + dev->isCheckpointed = 0;
8180 + yaffs_CheckpointInvalidateStream(dev);
8181 + if(dev->superBlock && dev->markSuperBlockDirty)
8182 + dev->markSuperBlockDirty(dev->superBlock);
8183 + }
8184 +}
8185 +
8186 +
8187 +int yaffs_CheckpointSave(yaffs_Device *dev)
8188 +{
8189 +
8190 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8191 +
8192 + yaffs_VerifyObjects(dev);
8193 + yaffs_VerifyBlocks(dev);
8194 + yaffs_VerifyFreeChunks(dev);
8195 +
8196 + if(!dev->isCheckpointed) {
8197 + yaffs_InvalidateCheckpoint(dev);
8198 + yaffs_WriteCheckpointData(dev);
8199 + }
8200 +
8201 + T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8202 +
8203 + return dev->isCheckpointed;
8204 +}
8205 +
8206 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8207 +{
8208 + int retval;
8209 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8210 +
8211 + retval = yaffs_ReadCheckpointData(dev);
8212 +
8213 + if(dev->isCheckpointed){
8214 + yaffs_VerifyObjects(dev);
8215 + yaffs_VerifyBlocks(dev);
8216 + yaffs_VerifyFreeChunks(dev);
8217 + }
8218 +
8219 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8220 +
8221 + return retval;
8222 +}
8223 +
8224 +/*--------------------- File read/write ------------------------
8225 + * Read and write have very similar structures.
8226 + * In general the read/write has three parts to it
8227 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8228 + * Some complete chunks
8229 + * An incomplete chunk to end off with
8230 + *
8231 + * Curve-balls: the first chunk might also be the last chunk.
8232 + */
8233 +
8234 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
8235 + int nBytes)
8236 +{
8237 +
8238 + int chunk;
8239 + int start;
8240 + int nToCopy;
8241 + int n = nBytes;
8242 + int nDone = 0;
8243 + yaffs_ChunkCache *cache;
8244 +
8245 + yaffs_Device *dev;
8246 +
8247 + dev = in->myDev;
8248 +
8249 + while (n > 0) {
8250 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8251 + //start = offset % dev->nDataBytesPerChunk;
8252 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8253 + chunk++;
8254 +
8255 + /* OK now check for the curveball where the start and end are in
8256 + * the same chunk.
8257 + */
8258 + if ((start + n) < dev->nDataBytesPerChunk) {
8259 + nToCopy = n;
8260 + } else {
8261 + nToCopy = dev->nDataBytesPerChunk - start;
8262 + }
8263 +
8264 + cache = yaffs_FindChunkCache(in, chunk);
8265 +
8266 + /* If the chunk is already in the cache or it is less than a whole chunk
8267 + * then use the cache (if there is caching)
8268 + * else bypass the cache.
8269 + */
8270 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
8271 + if (dev->nShortOpCaches > 0) {
8272 +
8273 + /* If we can't find the data in the cache, then load it up. */
8274 +
8275 + if (!cache) {
8276 + cache = yaffs_GrabChunkCache(in->myDev);
8277 + cache->object = in;
8278 + cache->chunkId = chunk;
8279 + cache->dirty = 0;
8280 + cache->locked = 0;
8281 + yaffs_ReadChunkDataFromObject(in, chunk,
8282 + cache->
8283 + data);
8284 + cache->nBytes = 0;
8285 + }
8286 +
8287 + yaffs_UseChunkCache(dev, cache, 0);
8288 +
8289 + cache->locked = 1;
8290 +
8291 +#ifdef CONFIG_YAFFS_WINCE
8292 + yfsd_UnlockYAFFS(TRUE);
8293 +#endif
8294 + memcpy(buffer, &cache->data[start], nToCopy);
8295 +
8296 +#ifdef CONFIG_YAFFS_WINCE
8297 + yfsd_LockYAFFS(TRUE);
8298 +#endif
8299 + cache->locked = 0;
8300 + } else {
8301 + /* Read into the local buffer then copy..*/
8302 +
8303 + __u8 *localBuffer =
8304 + yaffs_GetTempBuffer(dev, __LINE__);
8305 + yaffs_ReadChunkDataFromObject(in, chunk,
8306 + localBuffer);
8307 +#ifdef CONFIG_YAFFS_WINCE
8308 + yfsd_UnlockYAFFS(TRUE);
8309 +#endif
8310 + memcpy(buffer, &localBuffer[start], nToCopy);
8311 +
8312 +#ifdef CONFIG_YAFFS_WINCE
8313 + yfsd_LockYAFFS(TRUE);
8314 +#endif
8315 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8316 + __LINE__);
8317 + }
8318 +
8319 + } else {
8320 +#ifdef CONFIG_YAFFS_WINCE
8321 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8322 +
8323 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8324 + * This is because we otherwise screw up WinCE's memory mapper
8325 + */
8326 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
8327 +
8328 +#ifdef CONFIG_YAFFS_WINCE
8329 + yfsd_UnlockYAFFS(TRUE);
8330 +#endif
8331 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
8332 +
8333 +#ifdef CONFIG_YAFFS_WINCE
8334 + yfsd_LockYAFFS(TRUE);
8335 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8336 +#endif
8337 +
8338 +#else
8339 + /* A full chunk. Read directly into the supplied buffer. */
8340 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8341 +#endif
8342 + }
8343 +
8344 + n -= nToCopy;
8345 + offset += nToCopy;
8346 + buffer += nToCopy;
8347 + nDone += nToCopy;
8348 +
8349 + }
8350 +
8351 + return nDone;
8352 +}
8353 +
8354 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
8355 + int nBytes, int writeThrough)
8356 +{
8357 +
8358 + int chunk;
8359 + int start;
8360 + int nToCopy;
8361 + int n = nBytes;
8362 + int nDone = 0;
8363 + int nToWriteBack;
8364 + int startOfWrite = offset;
8365 + int chunkWritten = 0;
8366 + int nBytesRead;
8367 +
8368 + yaffs_Device *dev;
8369 +
8370 + dev = in->myDev;
8371 +
8372 + while (n > 0 && chunkWritten >= 0) {
8373 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8374 + //start = offset % dev->nDataBytesPerChunk;
8375 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8376 + chunk++;
8377 +
8378 + /* OK now check for the curveball where the start and end are in
8379 + * the same chunk.
8380 + */
8381 +
8382 + if ((start + n) < dev->nDataBytesPerChunk) {
8383 + nToCopy = n;
8384 +
8385 + /* Now folks, to calculate how many bytes to write back....
8386 + * If we're overwriting and not writing to then end of file then
8387 + * we need to write back as much as was there before.
8388 + */
8389 +
8390 + nBytesRead =
8391 + in->variant.fileVariant.fileSize -
8392 + ((chunk - 1) * dev->nDataBytesPerChunk);
8393 +
8394 + if (nBytesRead > dev->nDataBytesPerChunk) {
8395 + nBytesRead = dev->nDataBytesPerChunk;
8396 + }
8397 +
8398 + nToWriteBack =
8399 + (nBytesRead >
8400 + (start + n)) ? nBytesRead : (start + n);
8401 +
8402 + } else {
8403 + nToCopy = dev->nDataBytesPerChunk - start;
8404 + nToWriteBack = dev->nDataBytesPerChunk;
8405 + }
8406 +
8407 + if (nToCopy != dev->nDataBytesPerChunk) {
8408 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
8409 + if (dev->nShortOpCaches > 0) {
8410 + yaffs_ChunkCache *cache;
8411 + /* If we can't find the data in the cache, then load the cache */
8412 + cache = yaffs_FindChunkCache(in, chunk);
8413 +
8414 + if (!cache
8415 + && yaffs_CheckSpaceForAllocation(in->
8416 + myDev)) {
8417 + cache = yaffs_GrabChunkCache(in->myDev);
8418 + cache->object = in;
8419 + cache->chunkId = chunk;
8420 + cache->dirty = 0;
8421 + cache->locked = 0;
8422 + yaffs_ReadChunkDataFromObject(in, chunk,
8423 + cache->
8424 + data);
8425 + }
8426 + else if(cache &&
8427 + !cache->dirty &&
8428 + !yaffs_CheckSpaceForAllocation(in->myDev)){
8429 + /* Drop the cache if it was a read cache item and
8430 + * no space check has been made for it.
8431 + */
8432 + cache = NULL;
8433 + }
8434 +
8435 + if (cache) {
8436 + yaffs_UseChunkCache(dev, cache, 1);
8437 + cache->locked = 1;
8438 +#ifdef CONFIG_YAFFS_WINCE
8439 + yfsd_UnlockYAFFS(TRUE);
8440 +#endif
8441 +
8442 + memcpy(&cache->data[start], buffer,
8443 + nToCopy);
8444 +
8445 +#ifdef CONFIG_YAFFS_WINCE
8446 + yfsd_LockYAFFS(TRUE);
8447 +#endif
8448 + cache->locked = 0;
8449 + cache->nBytes = nToWriteBack;
8450 +
8451 + if (writeThrough) {
8452 + chunkWritten =
8453 + yaffs_WriteChunkDataToObject
8454 + (cache->object,
8455 + cache->chunkId,
8456 + cache->data, cache->nBytes,
8457 + 1);
8458 + cache->dirty = 0;
8459 + }
8460 +
8461 + } else {
8462 + chunkWritten = -1; /* fail the write */
8463 + }
8464 + } else {
8465 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8466 + * Read into the local buffer then copy, then copy over and write back.
8467 + */
8468 +
8469 + __u8 *localBuffer =
8470 + yaffs_GetTempBuffer(dev, __LINE__);
8471 +
8472 + yaffs_ReadChunkDataFromObject(in, chunk,
8473 + localBuffer);
8474 +
8475 +#ifdef CONFIG_YAFFS_WINCE
8476 + yfsd_UnlockYAFFS(TRUE);
8477 +#endif
8478 +
8479 + memcpy(&localBuffer[start], buffer, nToCopy);
8480 +
8481 +#ifdef CONFIG_YAFFS_WINCE
8482 + yfsd_LockYAFFS(TRUE);
8483 +#endif
8484 + chunkWritten =
8485 + yaffs_WriteChunkDataToObject(in, chunk,
8486 + localBuffer,
8487 + nToWriteBack,
8488 + 0);
8489 +
8490 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8491 + __LINE__);
8492 +
8493 + }
8494 +
8495 + } else {
8496 +
8497 +#ifdef CONFIG_YAFFS_WINCE
8498 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8499 + * This is because we otherwise screw up WinCE's memory mapper
8500 + */
8501 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8502 +#ifdef CONFIG_YAFFS_WINCE
8503 + yfsd_UnlockYAFFS(TRUE);
8504 +#endif
8505 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
8506 +#ifdef CONFIG_YAFFS_WINCE
8507 + yfsd_LockYAFFS(TRUE);
8508 +#endif
8509 + chunkWritten =
8510 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
8511 + dev->nDataBytesPerChunk,
8512 + 0);
8513 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8514 +#else
8515 + /* A full chunk. Write directly from the supplied buffer. */
8516 + chunkWritten =
8517 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8518 + dev->nDataBytesPerChunk,
8519 + 0);
8520 +#endif
8521 + /* Since we've overwritten the cached data, we better invalidate it. */
8522 + yaffs_InvalidateChunkCache(in, chunk);
8523 + }
8524 +
8525 + if (chunkWritten >= 0) {
8526 + n -= nToCopy;
8527 + offset += nToCopy;
8528 + buffer += nToCopy;
8529 + nDone += nToCopy;
8530 + }
8531 +
8532 + }
8533 +
8534 + /* Update file object */
8535 +
8536 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
8537 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8538 + }
8539 +
8540 + in->dirty = 1;
8541 +
8542 + return nDone;
8543 +}
8544 +
8545 +
8546 +/* ---------------------- File resizing stuff ------------------ */
8547 +
8548 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
8549 +{
8550 +
8551 + yaffs_Device *dev = in->myDev;
8552 + int oldFileSize = in->variant.fileVariant.fileSize;
8553 +
8554 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8555 +
8556 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8557 + dev->nDataBytesPerChunk;
8558 + int i;
8559 + int chunkId;
8560 +
8561 + /* Delete backwards so that we don't end up with holes if
8562 + * power is lost part-way through the operation.
8563 + */
8564 + for (i = lastDel; i >= startDel; i--) {
8565 + /* NB this could be optimised somewhat,
8566 + * eg. could retrieve the tags and write them without
8567 + * using yaffs_DeleteChunk
8568 + */
8569 +
8570 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8571 + if (chunkId > 0) {
8572 + if (chunkId <
8573 + (dev->internalStartBlock * dev->nChunksPerBlock)
8574 + || chunkId >=
8575 + ((dev->internalEndBlock +
8576 + 1) * dev->nChunksPerBlock)) {
8577 + T(YAFFS_TRACE_ALWAYS,
8578 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8579 + chunkId, i));
8580 + } else {
8581 + in->nDataChunks--;
8582 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8583 + }
8584 + }
8585 + }
8586 +
8587 +}
8588 +
8589 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
8590 +{
8591 +
8592 + int oldFileSize = in->variant.fileVariant.fileSize;
8593 + int newSizeOfPartialChunk;
8594 + int newFullChunks;
8595 +
8596 + yaffs_Device *dev = in->myDev;
8597 +
8598 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8599 +
8600 + yaffs_FlushFilesChunkCache(in);
8601 + yaffs_InvalidateWholeChunkCache(in);
8602 +
8603 + yaffs_CheckGarbageCollection(dev);
8604 +
8605 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8606 + return yaffs_GetFileSize(in);
8607 + }
8608 +
8609 + if (newSize == oldFileSize) {
8610 + return oldFileSize;
8611 + }
8612 +
8613 + if (newSize < oldFileSize) {
8614 +
8615 + yaffs_PruneResizedChunks(in, newSize);
8616 +
8617 + if (newSizeOfPartialChunk != 0) {
8618 + int lastChunk = 1 + newFullChunks;
8619 +
8620 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8621 +
8622 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8623 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8624 + localBuffer);
8625 +
8626 + memset(localBuffer + newSizeOfPartialChunk, 0,
8627 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8628 +
8629 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8630 + newSizeOfPartialChunk, 1);
8631 +
8632 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8633 + }
8634 +
8635 + in->variant.fileVariant.fileSize = newSize;
8636 +
8637 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
8638 + } else {
8639 + /* newsSize > oldFileSize */
8640 + in->variant.fileVariant.fileSize = newSize;
8641 + }
8642 +
8643 +
8644 +
8645 + /* Write a new object header.
8646 + * show we've shrunk the file, if need be
8647 + * Do this only if the file is not in the deleted directories.
8648 + */
8649 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
8650 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
8651 + yaffs_UpdateObjectHeader(in, NULL, 0,
8652 + (newSize < oldFileSize) ? 1 : 0, 0);
8653 + }
8654 +
8655 + return newSize;
8656 +}
8657 +
8658 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
8659 +{
8660 + obj = yaffs_GetEquivalentObject(obj);
8661 +
8662 + switch (obj->variantType) {
8663 + case YAFFS_OBJECT_TYPE_FILE:
8664 + return obj->variant.fileVariant.fileSize;
8665 + case YAFFS_OBJECT_TYPE_SYMLINK:
8666 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
8667 + default:
8668 + return 0;
8669 + }
8670 +}
8671 +
8672 +
8673 +
8674 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
8675 +{
8676 + int retVal;
8677 + if (in->dirty) {
8678 + yaffs_FlushFilesChunkCache(in);
8679 + if (updateTime) {
8680 +#ifdef CONFIG_YAFFS_WINCE
8681 + yfsd_WinFileTimeNow(in->win_mtime);
8682 +#else
8683 +
8684 + in->yst_mtime = Y_CURRENT_TIME;
8685 +
8686 +#endif
8687 + }
8688 +
8689 + retVal =
8690 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
8691 + 0) ? YAFFS_OK : YAFFS_FAIL;
8692 + } else {
8693 + retVal = YAFFS_OK;
8694 + }
8695 +
8696 + return retVal;
8697 +
8698 +}
8699 +
8700 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
8701 +{
8702 +
8703 + /* First off, invalidate the file's data in the cache, without flushing. */
8704 + yaffs_InvalidateWholeChunkCache(in);
8705 +
8706 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
8707 + /* Move to the unlinked directory so we have a record that it was deleted. */
8708 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
8709 +
8710 + }
8711 +
8712 + yaffs_RemoveObjectFromDirectory(in);
8713 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
8714 + in->chunkId = -1;
8715 +
8716 + yaffs_FreeObject(in);
8717 + return YAFFS_OK;
8718 +
8719 +}
8720 +
8721 +/* yaffs_DeleteFile deletes the whole file data
8722 + * and the inode associated with the file.
8723 + * It does not delete the links associated with the file.
8724 + */
8725 +static int yaffs_UnlinkFile(yaffs_Object * in)
8726 +{
8727 +
8728 + int retVal;
8729 + int immediateDeletion = 0;
8730 +
8731 + if (1) {
8732 +#ifdef __KERNEL__
8733 + if (!in->myInode) {
8734 + immediateDeletion = 1;
8735 +
8736 + }
8737 +#else
8738 + if (in->inUse <= 0) {
8739 + immediateDeletion = 1;
8740 +
8741 + }
8742 +#endif
8743 + if (immediateDeletion) {
8744 + retVal =
8745 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
8746 + "deleted", 0, 0);
8747 + T(YAFFS_TRACE_TRACING,
8748 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
8749 + in->objectId));
8750 + in->deleted = 1;
8751 + in->myDev->nDeletedFiles++;
8752 + if (0 && in->myDev->isYaffs2) {
8753 + yaffs_ResizeFile(in, 0);
8754 + }
8755 + yaffs_SoftDeleteFile(in);
8756 + } else {
8757 + retVal =
8758 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
8759 + "unlinked", 0, 0);
8760 + }
8761 +
8762 + }
8763 + return retVal;
8764 +}
8765 +
8766 +int yaffs_DeleteFile(yaffs_Object * in)
8767 +{
8768 + int retVal = YAFFS_OK;
8769 +
8770 + if (in->nDataChunks > 0) {
8771 + /* Use soft deletion if there is data in the file */
8772 + if (!in->unlinked) {
8773 + retVal = yaffs_UnlinkFile(in);
8774 + }
8775 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
8776 + in->deleted = 1;
8777 + in->myDev->nDeletedFiles++;
8778 + yaffs_SoftDeleteFile(in);
8779 + }
8780 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
8781 + } else {
8782 + /* The file has no data chunks so we toss it immediately */
8783 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
8784 + in->variant.fileVariant.top = NULL;
8785 + yaffs_DoGenericObjectDeletion(in);
8786 +
8787 + return YAFFS_OK;
8788 + }
8789 +}
8790 +
8791 +static int yaffs_DeleteDirectory(yaffs_Object * in)
8792 +{
8793 + /* First check that the directory is empty. */
8794 + if (list_empty(&in->variant.directoryVariant.children)) {
8795 + return yaffs_DoGenericObjectDeletion(in);
8796 + }
8797 +
8798 + return YAFFS_FAIL;
8799 +
8800 +}
8801 +
8802 +static int yaffs_DeleteSymLink(yaffs_Object * in)
8803 +{
8804 + YFREE(in->variant.symLinkVariant.alias);
8805 +
8806 + return yaffs_DoGenericObjectDeletion(in);
8807 +}
8808 +
8809 +static int yaffs_DeleteHardLink(yaffs_Object * in)
8810 +{
8811 + /* remove this hardlink from the list assocaited with the equivalent
8812 + * object
8813 + */
8814 + list_del(&in->hardLinks);
8815 + return yaffs_DoGenericObjectDeletion(in);
8816 +}
8817 +
8818 +static void yaffs_DestroyObject(yaffs_Object * obj)
8819 +{
8820 + switch (obj->variantType) {
8821 + case YAFFS_OBJECT_TYPE_FILE:
8822 + yaffs_DeleteFile(obj);
8823 + break;
8824 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8825 + yaffs_DeleteDirectory(obj);
8826 + break;
8827 + case YAFFS_OBJECT_TYPE_SYMLINK:
8828 + yaffs_DeleteSymLink(obj);
8829 + break;
8830 + case YAFFS_OBJECT_TYPE_HARDLINK:
8831 + yaffs_DeleteHardLink(obj);
8832 + break;
8833 + case YAFFS_OBJECT_TYPE_SPECIAL:
8834 + yaffs_DoGenericObjectDeletion(obj);
8835 + break;
8836 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8837 + break; /* should not happen. */
8838 + }
8839 +}
8840 +
8841 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
8842 +{
8843 +
8844 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8845 + return yaffs_DeleteHardLink(obj);
8846 + } else if (!list_empty(&obj->hardLinks)) {
8847 + /* Curve ball: We're unlinking an object that has a hardlink.
8848 + *
8849 + * This problem arises because we are not strictly following
8850 + * The Linux link/inode model.
8851 + *
8852 + * We can't really delete the object.
8853 + * Instead, we do the following:
8854 + * - Select a hardlink.
8855 + * - Unhook it from the hard links
8856 + * - Unhook it from its parent directory (so that the rename can work)
8857 + * - Rename the object to the hardlink's name.
8858 + * - Delete the hardlink
8859 + */
8860 +
8861 + yaffs_Object *hl;
8862 + int retVal;
8863 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
8864 +
8865 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
8866 +
8867 + list_del_init(&hl->hardLinks);
8868 + list_del_init(&hl->siblings);
8869 +
8870 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
8871 +
8872 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
8873 +
8874 + if (retVal == YAFFS_OK) {
8875 + retVal = yaffs_DoGenericObjectDeletion(hl);
8876 + }
8877 + return retVal;
8878 +
8879 + } else {
8880 + switch (obj->variantType) {
8881 + case YAFFS_OBJECT_TYPE_FILE:
8882 + return yaffs_UnlinkFile(obj);
8883 + break;
8884 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8885 + return yaffs_DeleteDirectory(obj);
8886 + break;
8887 + case YAFFS_OBJECT_TYPE_SYMLINK:
8888 + return yaffs_DeleteSymLink(obj);
8889 + break;
8890 + case YAFFS_OBJECT_TYPE_SPECIAL:
8891 + return yaffs_DoGenericObjectDeletion(obj);
8892 + break;
8893 + case YAFFS_OBJECT_TYPE_HARDLINK:
8894 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8895 + default:
8896 + return YAFFS_FAIL;
8897 + }
8898 + }
8899 +}
8900 +
8901 +
8902 +static int yaffs_UnlinkObject( yaffs_Object *obj)
8903 +{
8904 +
8905 + if (obj && obj->unlinkAllowed) {
8906 + return yaffs_UnlinkWorker(obj);
8907 + }
8908 +
8909 + return YAFFS_FAIL;
8910 +
8911 +}
8912 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8913 +{
8914 + yaffs_Object *obj;
8915 +
8916 + obj = yaffs_FindObjectByName(dir, name);
8917 + return yaffs_UnlinkObject(obj);
8918 +}
8919 +
8920 +/*----------------------- Initialisation Scanning ---------------------- */
8921 +
8922 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8923 + int backwardScanning)
8924 +{
8925 + yaffs_Object *obj;
8926 +
8927 + if (!backwardScanning) {
8928 + /* Handle YAFFS1 forward scanning case
8929 + * For YAFFS1 we always do the deletion
8930 + */
8931 +
8932 + } else {
8933 + /* Handle YAFFS2 case (backward scanning)
8934 + * If the shadowed object exists then ignore.
8935 + */
8936 + if (yaffs_FindObjectByNumber(dev, objId)) {
8937 + return;
8938 + }
8939 + }
8940 +
8941 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8942 + * We put it in unlinked dir to be cleaned up after the scanning
8943 + */
8944 + obj =
8945 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8946 + YAFFS_OBJECT_TYPE_FILE);
8947 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8948 + obj->variant.fileVariant.shrinkSize = 0;
8949 + obj->valid = 1; /* So that we don't read any other info for this file */
8950 +
8951 +}
8952 +
8953 +typedef struct {
8954 + int seq;
8955 + int block;
8956 +} yaffs_BlockIndex;
8957 +
8958 +
8959 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8960 +{
8961 + yaffs_Object *hl;
8962 + yaffs_Object *in;
8963 +
8964 + while (hardList) {
8965 + hl = hardList;
8966 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8967 +
8968 + in = yaffs_FindObjectByNumber(dev,
8969 + hl->variant.hardLinkVariant.
8970 + equivalentObjectId);
8971 +
8972 + if (in) {
8973 + /* Add the hardlink pointers */
8974 + hl->variant.hardLinkVariant.equivalentObject = in;
8975 + list_add(&hl->hardLinks, &in->hardLinks);
8976 + } else {
8977 + /* Todo Need to report/handle this better.
8978 + * Got a problem... hardlink to a non-existant object
8979 + */
8980 + hl->variant.hardLinkVariant.equivalentObject = NULL;
8981 + INIT_LIST_HEAD(&hl->hardLinks);
8982 +
8983 + }
8984 +
8985 + }
8986 +
8987 +}
8988 +
8989 +
8990 +
8991 +
8992 +
8993 +static int ybicmp(const void *a, const void *b){
8994 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
8995 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
8996 + register int ablock = ((yaffs_BlockIndex *)a)->block;
8997 + register int bblock = ((yaffs_BlockIndex *)b)->block;
8998 + if( aseq == bseq )
8999 + return ablock - bblock;
9000 + else
9001 + return aseq - bseq;
9002 +
9003 +}
9004 +
9005 +static int yaffs_Scan(yaffs_Device * dev)
9006 +{
9007 + yaffs_ExtendedTags tags;
9008 + int blk;
9009 + int blockIterator;
9010 + int startIterator;
9011 + int endIterator;
9012 + int nBlocksToScan = 0;
9013 + int result;
9014 +
9015 + int chunk;
9016 + int c;
9017 + int deleted;
9018 + yaffs_BlockState state;
9019 + yaffs_Object *hardList = NULL;
9020 + yaffs_BlockInfo *bi;
9021 + int sequenceNumber;
9022 + yaffs_ObjectHeader *oh;
9023 + yaffs_Object *in;
9024 + yaffs_Object *parent;
9025 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9026 +
9027 + int alloc_failed = 0;
9028 +
9029 +
9030 + __u8 *chunkData;
9031 +
9032 + yaffs_BlockIndex *blockIndex = NULL;
9033 +
9034 + if (dev->isYaffs2) {
9035 + T(YAFFS_TRACE_SCAN,
9036 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
9037 + return YAFFS_FAIL;
9038 + }
9039 +
9040 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
9041 +
9042 + T(YAFFS_TRACE_SCAN,
9043 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9044 + dev->internalStartBlock, dev->internalEndBlock));
9045 +
9046 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9047 +
9048 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9049 +
9050 + if (dev->isYaffs2) {
9051 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9052 + if(!blockIndex)
9053 + return YAFFS_FAIL;
9054 + }
9055 +
9056 + /* Scan all the blocks to determine their state */
9057 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9058 + bi = yaffs_GetBlockInfo(dev, blk);
9059 + yaffs_ClearChunkBits(dev, blk);
9060 + bi->pagesInUse = 0;
9061 + bi->softDeletions = 0;
9062 +
9063 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9064 +
9065 + bi->blockState = state;
9066 + bi->sequenceNumber = sequenceNumber;
9067 +
9068 + T(YAFFS_TRACE_SCAN_DEBUG,
9069 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9070 + state, sequenceNumber));
9071 +
9072 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9073 + T(YAFFS_TRACE_BAD_BLOCKS,
9074 + (TSTR("block %d is bad" TENDSTR), blk));
9075 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9076 + T(YAFFS_TRACE_SCAN_DEBUG,
9077 + (TSTR("Block empty " TENDSTR)));
9078 + dev->nErasedBlocks++;
9079 + dev->nFreeChunks += dev->nChunksPerBlock;
9080 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9081 +
9082 + /* Determine the highest sequence number */
9083 + if (dev->isYaffs2 &&
9084 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9085 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9086 +
9087 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9088 + blockIndex[nBlocksToScan].block = blk;
9089 +
9090 + nBlocksToScan++;
9091 +
9092 + if (sequenceNumber >= dev->sequenceNumber) {
9093 + dev->sequenceNumber = sequenceNumber;
9094 + }
9095 + } else if (dev->isYaffs2) {
9096 + /* TODO: Nasty sequence number! */
9097 + T(YAFFS_TRACE_SCAN,
9098 + (TSTR
9099 + ("Block scanning block %d has bad sequence number %d"
9100 + TENDSTR), blk, sequenceNumber));
9101 +
9102 + }
9103 + }
9104 + }
9105 +
9106 + /* Sort the blocks
9107 + * Dungy old bubble sort for now...
9108 + */
9109 + if (dev->isYaffs2) {
9110 + yaffs_BlockIndex temp;
9111 + int i;
9112 + int j;
9113 +
9114 + for (i = 0; i < nBlocksToScan; i++)
9115 + for (j = i + 1; j < nBlocksToScan; j++)
9116 + if (blockIndex[i].seq > blockIndex[j].seq) {
9117 + temp = blockIndex[j];
9118 + blockIndex[j] = blockIndex[i];
9119 + blockIndex[i] = temp;
9120 + }
9121 + }
9122 +
9123 + /* Now scan the blocks looking at the data. */
9124 + if (dev->isYaffs2) {
9125 + startIterator = 0;
9126 + endIterator = nBlocksToScan - 1;
9127 + T(YAFFS_TRACE_SCAN_DEBUG,
9128 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9129 + } else {
9130 + startIterator = dev->internalStartBlock;
9131 + endIterator = dev->internalEndBlock;
9132 + }
9133 +
9134 + /* For each block.... */
9135 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9136 + blockIterator++) {
9137 +
9138 + if (dev->isYaffs2) {
9139 + /* get the block to scan in the correct order */
9140 + blk = blockIndex[blockIterator].block;
9141 + } else {
9142 + blk = blockIterator;
9143 + }
9144 +
9145 + bi = yaffs_GetBlockInfo(dev, blk);
9146 + state = bi->blockState;
9147 +
9148 + deleted = 0;
9149 +
9150 + /* For each chunk in each block that needs scanning....*/
9151 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9152 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9153 + /* Read the tags and decide what to do */
9154 + chunk = blk * dev->nChunksPerBlock + c;
9155 +
9156 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9157 + &tags);
9158 +
9159 + /* Let's have a good look at this chunk... */
9160 +
9161 + if (!dev->isYaffs2 && tags.chunkDeleted) {
9162 + /* YAFFS1 only...
9163 + * A deleted chunk
9164 + */
9165 + deleted++;
9166 + dev->nFreeChunks++;
9167 + /*T((" %d %d deleted\n",blk,c)); */
9168 + } else if (!tags.chunkUsed) {
9169 + /* An unassigned chunk in the block
9170 + * This means that either the block is empty or
9171 + * this is the one being allocated from
9172 + */
9173 +
9174 + if (c == 0) {
9175 + /* We're looking at the first chunk in the block so the block is unused */
9176 + state = YAFFS_BLOCK_STATE_EMPTY;
9177 + dev->nErasedBlocks++;
9178 + } else {
9179 + /* this is the block being allocated from */
9180 + T(YAFFS_TRACE_SCAN,
9181 + (TSTR
9182 + (" Allocating from %d %d" TENDSTR),
9183 + blk, c));
9184 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9185 + dev->allocationBlock = blk;
9186 + dev->allocationPage = c;
9187 + dev->allocationBlockFinder = blk;
9188 + /* Set it to here to encourage the allocator to go forth from here. */
9189 +
9190 + /* Yaffs2 sanity check:
9191 + * This should be the one with the highest sequence number
9192 + */
9193 + if (dev->isYaffs2
9194 + && (dev->sequenceNumber !=
9195 + bi->sequenceNumber)) {
9196 + T(YAFFS_TRACE_ALWAYS,
9197 + (TSTR
9198 + ("yaffs: Allocation block %d was not highest sequence id:"
9199 + " block seq = %d, dev seq = %d"
9200 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
9201 + }
9202 + }
9203 +
9204 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9205 + } else if (tags.chunkId > 0) {
9206 + /* chunkId > 0 so it is a data chunk... */
9207 + unsigned int endpos;
9208 +
9209 + yaffs_SetChunkBit(dev, blk, c);
9210 + bi->pagesInUse++;
9211 +
9212 + in = yaffs_FindOrCreateObjectByNumber(dev,
9213 + tags.
9214 + objectId,
9215 + YAFFS_OBJECT_TYPE_FILE);
9216 + /* PutChunkIntoFile checks for a clash (two data chunks with
9217 + * the same chunkId).
9218 + */
9219 +
9220 + if(!in)
9221 + alloc_failed = 1;
9222 +
9223 + if(in){
9224 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
9225 + alloc_failed = 1;
9226 + }
9227 +
9228 + endpos =
9229 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9230 + tags.byteCount;
9231 + if (in &&
9232 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9233 + && in->variant.fileVariant.scannedFileSize <
9234 + endpos) {
9235 + in->variant.fileVariant.
9236 + scannedFileSize = endpos;
9237 + if (!dev->useHeaderFileSize) {
9238 + in->variant.fileVariant.
9239 + fileSize =
9240 + in->variant.fileVariant.
9241 + scannedFileSize;
9242 + }
9243 +
9244 + }
9245 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9246 + } else {
9247 + /* chunkId == 0, so it is an ObjectHeader.
9248 + * Thus, we read in the object header and make the object
9249 + */
9250 + yaffs_SetChunkBit(dev, blk, c);
9251 + bi->pagesInUse++;
9252 +
9253 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9254 + chunkData,
9255 + NULL);
9256 +
9257 + oh = (yaffs_ObjectHeader *) chunkData;
9258 +
9259 + in = yaffs_FindObjectByNumber(dev,
9260 + tags.objectId);
9261 + if (in && in->variantType != oh->type) {
9262 + /* This should not happen, but somehow
9263 + * Wev'e ended up with an objectId that has been reused but not yet
9264 + * deleted, and worse still it has changed type. Delete the old object.
9265 + */
9266 +
9267 + yaffs_DestroyObject(in);
9268 +
9269 + in = 0;
9270 + }
9271 +
9272 + in = yaffs_FindOrCreateObjectByNumber(dev,
9273 + tags.
9274 + objectId,
9275 + oh->type);
9276 +
9277 + if(!in)
9278 + alloc_failed = 1;
9279 +
9280 + if (in && oh->shadowsObject > 0) {
9281 + yaffs_HandleShadowedObject(dev,
9282 + oh->
9283 + shadowsObject,
9284 + 0);
9285 + }
9286 +
9287 + if (in && in->valid) {
9288 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9289 +
9290 + unsigned existingSerial = in->serial;
9291 + unsigned newSerial = tags.serialNumber;
9292 +
9293 + if (dev->isYaffs2 ||
9294 + ((existingSerial + 1) & 3) ==
9295 + newSerial) {
9296 + /* Use new one - destroy the exisiting one */
9297 + yaffs_DeleteChunk(dev,
9298 + in->chunkId,
9299 + 1, __LINE__);
9300 + in->valid = 0;
9301 + } else {
9302 + /* Use existing - destroy this one. */
9303 + yaffs_DeleteChunk(dev, chunk, 1,
9304 + __LINE__);
9305 + }
9306 + }
9307 +
9308 + if (in && !in->valid &&
9309 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9310 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9311 + /* We only load some info, don't fiddle with directory structure */
9312 + in->valid = 1;
9313 + in->variantType = oh->type;
9314 +
9315 + in->yst_mode = oh->yst_mode;
9316 +#ifdef CONFIG_YAFFS_WINCE
9317 + in->win_atime[0] = oh->win_atime[0];
9318 + in->win_ctime[0] = oh->win_ctime[0];
9319 + in->win_mtime[0] = oh->win_mtime[0];
9320 + in->win_atime[1] = oh->win_atime[1];
9321 + in->win_ctime[1] = oh->win_ctime[1];
9322 + in->win_mtime[1] = oh->win_mtime[1];
9323 +#else
9324 + in->yst_uid = oh->yst_uid;
9325 + in->yst_gid = oh->yst_gid;
9326 + in->yst_atime = oh->yst_atime;
9327 + in->yst_mtime = oh->yst_mtime;
9328 + in->yst_ctime = oh->yst_ctime;
9329 + in->yst_rdev = oh->yst_rdev;
9330 +#endif
9331 + in->chunkId = chunk;
9332 +
9333 + } else if (in && !in->valid) {
9334 + /* we need to load this info */
9335 +
9336 + in->valid = 1;
9337 + in->variantType = oh->type;
9338 +
9339 + in->yst_mode = oh->yst_mode;
9340 +#ifdef CONFIG_YAFFS_WINCE
9341 + in->win_atime[0] = oh->win_atime[0];
9342 + in->win_ctime[0] = oh->win_ctime[0];
9343 + in->win_mtime[0] = oh->win_mtime[0];
9344 + in->win_atime[1] = oh->win_atime[1];
9345 + in->win_ctime[1] = oh->win_ctime[1];
9346 + in->win_mtime[1] = oh->win_mtime[1];
9347 +#else
9348 + in->yst_uid = oh->yst_uid;
9349 + in->yst_gid = oh->yst_gid;
9350 + in->yst_atime = oh->yst_atime;
9351 + in->yst_mtime = oh->yst_mtime;
9352 + in->yst_ctime = oh->yst_ctime;
9353 + in->yst_rdev = oh->yst_rdev;
9354 +#endif
9355 + in->chunkId = chunk;
9356 +
9357 + yaffs_SetObjectName(in, oh->name);
9358 + in->dirty = 0;
9359 +
9360 + /* directory stuff...
9361 + * hook up to parent
9362 + */
9363 +
9364 + parent =
9365 + yaffs_FindOrCreateObjectByNumber
9366 + (dev, oh->parentObjectId,
9367 + YAFFS_OBJECT_TYPE_DIRECTORY);
9368 + if (parent->variantType ==
9369 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9370 + /* Set up as a directory */
9371 + parent->variantType =
9372 + YAFFS_OBJECT_TYPE_DIRECTORY;
9373 + INIT_LIST_HEAD(&parent->variant.
9374 + directoryVariant.
9375 + children);
9376 + } else if (parent->variantType !=
9377 + YAFFS_OBJECT_TYPE_DIRECTORY)
9378 + {
9379 + /* Hoosterman, another problem....
9380 + * We're trying to use a non-directory as a directory
9381 + */
9382 +
9383 + T(YAFFS_TRACE_ERROR,
9384 + (TSTR
9385 + ("yaffs tragedy: attempting to use non-directory as"
9386 + " a directory in scan. Put in lost+found."
9387 + TENDSTR)));
9388 + parent = dev->lostNFoundDir;
9389 + }
9390 +
9391 + yaffs_AddObjectToDirectory(parent, in);
9392 +
9393 + if (0 && (parent == dev->deletedDir ||
9394 + parent == dev->unlinkedDir)) {
9395 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9396 + dev->nDeletedFiles++;
9397 + }
9398 + /* Note re hardlinks.
9399 + * Since we might scan a hardlink before its equivalent object is scanned
9400 + * we put them all in a list.
9401 + * After scanning is complete, we should have all the objects, so we run through this
9402 + * list and fix up all the chains.
9403 + */
9404 +
9405 + switch (in->variantType) {
9406 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9407 + /* Todo got a problem */
9408 + break;
9409 + case YAFFS_OBJECT_TYPE_FILE:
9410 + if (dev->isYaffs2
9411 + && oh->isShrink) {
9412 + /* Prune back the shrunken chunks */
9413 + yaffs_PruneResizedChunks
9414 + (in, oh->fileSize);
9415 + /* Mark the block as having a shrinkHeader */
9416 + bi->hasShrinkHeader = 1;
9417 + }
9418 +
9419 + if (dev->useHeaderFileSize)
9420 +
9421 + in->variant.fileVariant.
9422 + fileSize =
9423 + oh->fileSize;
9424 +
9425 + break;
9426 + case YAFFS_OBJECT_TYPE_HARDLINK:
9427 + in->variant.hardLinkVariant.
9428 + equivalentObjectId =
9429 + oh->equivalentObjectId;
9430 + in->hardLinks.next =
9431 + (struct list_head *)
9432 + hardList;
9433 + hardList = in;
9434 + break;
9435 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9436 + /* Do nothing */
9437 + break;
9438 + case YAFFS_OBJECT_TYPE_SPECIAL:
9439 + /* Do nothing */
9440 + break;
9441 + case YAFFS_OBJECT_TYPE_SYMLINK:
9442 + in->variant.symLinkVariant.alias =
9443 + yaffs_CloneString(oh->alias);
9444 + if(!in->variant.symLinkVariant.alias)
9445 + alloc_failed = 1;
9446 + break;
9447 + }
9448 +
9449 + if (parent == dev->deletedDir) {
9450 + yaffs_DestroyObject(in);
9451 + bi->hasShrinkHeader = 1;
9452 + }
9453 + }
9454 + }
9455 + }
9456 +
9457 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9458 + /* If we got this far while scanning, then the block is fully allocated.*/
9459 + state = YAFFS_BLOCK_STATE_FULL;
9460 + }
9461 +
9462 + bi->blockState = state;
9463 +
9464 + /* Now let's see if it was dirty */
9465 + if (bi->pagesInUse == 0 &&
9466 + !bi->hasShrinkHeader &&
9467 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9468 + yaffs_BlockBecameDirty(dev, blk);
9469 + }
9470 +
9471 + }
9472 +
9473 + if (blockIndex) {
9474 + YFREE(blockIndex);
9475 + }
9476 +
9477 +
9478 + /* Ok, we've done all the scanning.
9479 + * Fix up the hard link chains.
9480 + * We should now have scanned all the objects, now it's time to add these
9481 + * hardlinks.
9482 + */
9483 +
9484 + yaffs_HardlinkFixup(dev,hardList);
9485 +
9486 + /* Handle the unlinked files. Since they were left in an unlinked state we should
9487 + * just delete them.
9488 + */
9489 + {
9490 + struct list_head *i;
9491 + struct list_head *n;
9492 +
9493 + yaffs_Object *l;
9494 + /* Soft delete all the unlinked files */
9495 + list_for_each_safe(i, n,
9496 + &dev->unlinkedDir->variant.directoryVariant.
9497 + children) {
9498 + if (i) {
9499 + l = list_entry(i, yaffs_Object, siblings);
9500 + yaffs_DestroyObject(l);
9501 + }
9502 + }
9503 + }
9504 +
9505 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9506 +
9507 + if(alloc_failed){
9508 + return YAFFS_FAIL;
9509 + }
9510 +
9511 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9512 +
9513 +
9514 + return YAFFS_OK;
9515 +}
9516 +
9517 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9518 +{
9519 + __u8 *chunkData;
9520 + yaffs_ObjectHeader *oh;
9521 + yaffs_Device *dev = in->myDev;
9522 + yaffs_ExtendedTags tags;
9523 + int result;
9524 + int alloc_failed = 0;
9525 +
9526 + if(!in)
9527 + return;
9528 +
9529 +#if 0
9530 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
9531 + in->objectId,
9532 + in->lazyLoaded ? "not yet" : "already"));
9533 +#endif
9534 +
9535 + if(in->lazyLoaded){
9536 + in->lazyLoaded = 0;
9537 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9538 +
9539 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
9540 + oh = (yaffs_ObjectHeader *) chunkData;
9541 +
9542 + in->yst_mode = oh->yst_mode;
9543 +#ifdef CONFIG_YAFFS_WINCE
9544 + in->win_atime[0] = oh->win_atime[0];
9545 + in->win_ctime[0] = oh->win_ctime[0];
9546 + in->win_mtime[0] = oh->win_mtime[0];
9547 + in->win_atime[1] = oh->win_atime[1];
9548 + in->win_ctime[1] = oh->win_ctime[1];
9549 + in->win_mtime[1] = oh->win_mtime[1];
9550 +#else
9551 + in->yst_uid = oh->yst_uid;
9552 + in->yst_gid = oh->yst_gid;
9553 + in->yst_atime = oh->yst_atime;
9554 + in->yst_mtime = oh->yst_mtime;
9555 + in->yst_ctime = oh->yst_ctime;
9556 + in->yst_rdev = oh->yst_rdev;
9557 +
9558 +#endif
9559 + yaffs_SetObjectName(in, oh->name);
9560 +
9561 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
9562 + in->variant.symLinkVariant.alias =
9563 + yaffs_CloneString(oh->alias);
9564 + if(!in->variant.symLinkVariant.alias)
9565 + alloc_failed = 1; /* Not returned to caller */
9566 + }
9567 +
9568 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
9569 + }
9570 +}
9571 +
9572 +static int yaffs_ScanBackwards(yaffs_Device * dev)
9573 +{
9574 + yaffs_ExtendedTags tags;
9575 + int blk;
9576 + int blockIterator;
9577 + int startIterator;
9578 + int endIterator;
9579 + int nBlocksToScan = 0;
9580 +
9581 + int chunk;
9582 + int result;
9583 + int c;
9584 + int deleted;
9585 + yaffs_BlockState state;
9586 + yaffs_Object *hardList = NULL;
9587 + yaffs_BlockInfo *bi;
9588 + int sequenceNumber;
9589 + yaffs_ObjectHeader *oh;
9590 + yaffs_Object *in;
9591 + yaffs_Object *parent;
9592 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9593 + int itsUnlinked;
9594 + __u8 *chunkData;
9595 +
9596 + int fileSize;
9597 + int isShrink;
9598 + int foundChunksInBlock;
9599 + int equivalentObjectId;
9600 + int alloc_failed = 0;
9601 +
9602 +
9603 + yaffs_BlockIndex *blockIndex = NULL;
9604 + int altBlockIndex = 0;
9605 +
9606 + if (!dev->isYaffs2) {
9607 + T(YAFFS_TRACE_SCAN,
9608 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9609 + return YAFFS_FAIL;
9610 + }
9611 +
9612 + T(YAFFS_TRACE_SCAN,
9613 + (TSTR
9614 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9615 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9616 +
9617 +
9618 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9619 +
9620 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9621 +
9622 + if(!blockIndex) {
9623 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9624 + altBlockIndex = 1;
9625 + }
9626 +
9627 + if(!blockIndex) {
9628 + T(YAFFS_TRACE_SCAN,
9629 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9630 + return YAFFS_FAIL;
9631 + }
9632 +
9633 + dev->blocksInCheckpoint = 0;
9634 +
9635 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9636 +
9637 + /* Scan all the blocks to determine their state */
9638 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9639 + bi = yaffs_GetBlockInfo(dev, blk);
9640 + yaffs_ClearChunkBits(dev, blk);
9641 + bi->pagesInUse = 0;
9642 + bi->softDeletions = 0;
9643 +
9644 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9645 +
9646 + bi->blockState = state;
9647 + bi->sequenceNumber = sequenceNumber;
9648 +
9649 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9650 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9651 +
9652 + T(YAFFS_TRACE_SCAN_DEBUG,
9653 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9654 + state, sequenceNumber));
9655 +
9656 +
9657 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
9658 + dev->blocksInCheckpoint++;
9659 +
9660 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
9661 + T(YAFFS_TRACE_BAD_BLOCKS,
9662 + (TSTR("block %d is bad" TENDSTR), blk));
9663 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9664 + T(YAFFS_TRACE_SCAN_DEBUG,
9665 + (TSTR("Block empty " TENDSTR)));
9666 + dev->nErasedBlocks++;
9667 + dev->nFreeChunks += dev->nChunksPerBlock;
9668 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9669 +
9670 + /* Determine the highest sequence number */
9671 + if (dev->isYaffs2 &&
9672 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9673 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9674 +
9675 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9676 + blockIndex[nBlocksToScan].block = blk;
9677 +
9678 + nBlocksToScan++;
9679 +
9680 + if (sequenceNumber >= dev->sequenceNumber) {
9681 + dev->sequenceNumber = sequenceNumber;
9682 + }
9683 + } else if (dev->isYaffs2) {
9684 + /* TODO: Nasty sequence number! */
9685 + T(YAFFS_TRACE_SCAN,
9686 + (TSTR
9687 + ("Block scanning block %d has bad sequence number %d"
9688 + TENDSTR), blk, sequenceNumber));
9689 +
9690 + }
9691 + }
9692 + }
9693 +
9694 + T(YAFFS_TRACE_SCAN,
9695 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
9696 +
9697 +
9698 +
9699 + YYIELD();
9700 +
9701 + /* Sort the blocks */
9702 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
9703 + yaffs_qsort(blockIndex, nBlocksToScan,
9704 + sizeof(yaffs_BlockIndex), ybicmp);
9705 +#else
9706 + {
9707 + /* Dungy old bubble sort... */
9708 +
9709 + yaffs_BlockIndex temp;
9710 + int i;
9711 + int j;
9712 +
9713 + for (i = 0; i < nBlocksToScan; i++)
9714 + for (j = i + 1; j < nBlocksToScan; j++)
9715 + if (blockIndex[i].seq > blockIndex[j].seq) {
9716 + temp = blockIndex[j];
9717 + blockIndex[j] = blockIndex[i];
9718 + blockIndex[i] = temp;
9719 + }
9720 + }
9721 +#endif
9722 +
9723 + YYIELD();
9724 +
9725 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
9726 +
9727 + /* Now scan the blocks looking at the data. */
9728 + startIterator = 0;
9729 + endIterator = nBlocksToScan - 1;
9730 + T(YAFFS_TRACE_SCAN_DEBUG,
9731 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9732 +
9733 + /* For each block.... backwards */
9734 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
9735 + blockIterator--) {
9736 + /* Cooperative multitasking! This loop can run for so
9737 + long that watchdog timers expire. */
9738 + YYIELD();
9739 +
9740 + /* get the block to scan in the correct order */
9741 + blk = blockIndex[blockIterator].block;
9742 +
9743 + bi = yaffs_GetBlockInfo(dev, blk);
9744 +
9745 +
9746 + state = bi->blockState;
9747 +
9748 + deleted = 0;
9749 +
9750 + /* For each chunk in each block that needs scanning.... */
9751 + foundChunksInBlock = 0;
9752 + for (c = dev->nChunksPerBlock - 1;
9753 + !alloc_failed && c >= 0 &&
9754 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9755 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
9756 + /* Scan backwards...
9757 + * Read the tags and decide what to do
9758 + */
9759 +
9760 + chunk = blk * dev->nChunksPerBlock + c;
9761 +
9762 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9763 + &tags);
9764 +
9765 + /* Let's have a good look at this chunk... */
9766 +
9767 + if (!tags.chunkUsed) {
9768 + /* An unassigned chunk in the block.
9769 + * If there are used chunks after this one, then
9770 + * it is a chunk that was skipped due to failing the erased
9771 + * check. Just skip it so that it can be deleted.
9772 + * But, more typically, We get here when this is an unallocated
9773 + * chunk and his means that either the block is empty or
9774 + * this is the one being allocated from
9775 + */
9776 +
9777 + if(foundChunksInBlock)
9778 + {
9779 + /* This is a chunk that was skipped due to failing the erased check */
9780 +
9781 + } else if (c == 0) {
9782 + /* We're looking at the first chunk in the block so the block is unused */
9783 + state = YAFFS_BLOCK_STATE_EMPTY;
9784 + dev->nErasedBlocks++;
9785 + } else {
9786 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9787 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
9788 + if(dev->sequenceNumber == bi->sequenceNumber) {
9789 + /* this is the block being allocated from */
9790 +
9791 + T(YAFFS_TRACE_SCAN,
9792 + (TSTR
9793 + (" Allocating from %d %d"
9794 + TENDSTR), blk, c));
9795 +
9796 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9797 + dev->allocationBlock = blk;
9798 + dev->allocationPage = c;
9799 + dev->allocationBlockFinder = blk;
9800 + }
9801 + else {
9802 + /* This is a partially written block that is not
9803 + * the current allocation block. This block must have
9804 + * had a write failure, so set up for retirement.
9805 + */
9806 +
9807 + bi->needsRetiring = 1;
9808 + bi->gcPrioritise = 1;
9809 +
9810 + T(YAFFS_TRACE_ALWAYS,
9811 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
9812 + blk));
9813 + }
9814 +
9815 + }
9816 +
9817 + }
9818 +
9819 + dev->nFreeChunks++;
9820 +
9821 + } else if (tags.chunkId > 0) {
9822 + /* chunkId > 0 so it is a data chunk... */
9823 + unsigned int endpos;
9824 + __u32 chunkBase =
9825 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
9826 +
9827 + foundChunksInBlock = 1;
9828 +
9829 +
9830 + yaffs_SetChunkBit(dev, blk, c);
9831 + bi->pagesInUse++;
9832 +
9833 + in = yaffs_FindOrCreateObjectByNumber(dev,
9834 + tags.
9835 + objectId,
9836 + YAFFS_OBJECT_TYPE_FILE);
9837 + if(!in){
9838 + /* Out of memory */
9839 + alloc_failed = 1;
9840 + }
9841 +
9842 + if (in &&
9843 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9844 + && chunkBase <
9845 + in->variant.fileVariant.shrinkSize) {
9846 + /* This has not been invalidated by a resize */
9847 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
9848 + chunk, -1)){
9849 + alloc_failed = 1;
9850 + }
9851 +
9852 + /* File size is calculated by looking at the data chunks if we have not
9853 + * seen an object header yet. Stop this practice once we find an object header.
9854 + */
9855 + endpos =
9856 + (tags.chunkId -
9857 + 1) * dev->nDataBytesPerChunk +
9858 + tags.byteCount;
9859 +
9860 + if (!in->valid && /* have not got an object header yet */
9861 + in->variant.fileVariant.
9862 + scannedFileSize < endpos) {
9863 + in->variant.fileVariant.
9864 + scannedFileSize = endpos;
9865 + in->variant.fileVariant.
9866 + fileSize =
9867 + in->variant.fileVariant.
9868 + scannedFileSize;
9869 + }
9870 +
9871 + } else if(in) {
9872 + /* This chunk has been invalidated by a resize, so delete */
9873 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9874 +
9875 + }
9876 + } else {
9877 + /* chunkId == 0, so it is an ObjectHeader.
9878 + * Thus, we read in the object header and make the object
9879 + */
9880 + foundChunksInBlock = 1;
9881 +
9882 + yaffs_SetChunkBit(dev, blk, c);
9883 + bi->pagesInUse++;
9884 +
9885 + oh = NULL;
9886 + in = NULL;
9887 +
9888 + if (tags.extraHeaderInfoAvailable) {
9889 + in = yaffs_FindOrCreateObjectByNumber
9890 + (dev, tags.objectId,
9891 + tags.extraObjectType);
9892 + }
9893 +
9894 + if (!in ||
9895 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
9896 + !in->valid ||
9897 +#endif
9898 + tags.extraShadows ||
9899 + (!in->valid &&
9900 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9901 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
9902 + ) {
9903 +
9904 + /* If we don't have valid info then we need to read the chunk
9905 + * TODO In future we can probably defer reading the chunk and
9906 + * living with invalid data until needed.
9907 + */
9908 +
9909 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
9910 + chunk,
9911 + chunkData,
9912 + NULL);
9913 +
9914 + oh = (yaffs_ObjectHeader *) chunkData;
9915 +
9916 + if (!in)
9917 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
9918 +
9919 + }
9920 +
9921 + if (!in) {
9922 + /* TODO Hoosterman we have a problem! */
9923 + T(YAFFS_TRACE_ERROR,
9924 + (TSTR
9925 + ("yaffs tragedy: Could not make object for object %d "
9926 + "at chunk %d during scan"
9927 + TENDSTR), tags.objectId, chunk));
9928 +
9929 + }
9930 +
9931 + if (in->valid) {
9932 + /* We have already filled this one.
9933 + * We have a duplicate that will be discarded, but
9934 + * we first have to suck out resize info if it is a file.
9935 + */
9936 +
9937 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
9938 + ((oh &&
9939 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
9940 + (tags.extraHeaderInfoAvailable &&
9941 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
9942 + ) {
9943 + __u32 thisSize =
9944 + (oh) ? oh->fileSize : tags.
9945 + extraFileLength;
9946 + __u32 parentObjectId =
9947 + (oh) ? oh->
9948 + parentObjectId : tags.
9949 + extraParentObjectId;
9950 + unsigned isShrink =
9951 + (oh) ? oh->isShrink : tags.
9952 + extraIsShrinkHeader;
9953 +
9954 + /* If it is deleted (unlinked at start also means deleted)
9955 + * we treat the file size as being zeroed at this point.
9956 + */
9957 + if (parentObjectId ==
9958 + YAFFS_OBJECTID_DELETED
9959 + || parentObjectId ==
9960 + YAFFS_OBJECTID_UNLINKED) {
9961 + thisSize = 0;
9962 + isShrink = 1;
9963 + }
9964 +
9965 + if (isShrink &&
9966 + in->variant.fileVariant.
9967 + shrinkSize > thisSize) {
9968 + in->variant.fileVariant.
9969 + shrinkSize =
9970 + thisSize;
9971 + }
9972 +
9973 + if (isShrink) {
9974 + bi->hasShrinkHeader = 1;
9975 + }
9976 +
9977 + }
9978 + /* Use existing - destroy this one. */
9979 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9980 +
9981 + }
9982 +
9983 + if (!in->valid &&
9984 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9985 + tags.objectId ==
9986 + YAFFS_OBJECTID_LOSTNFOUND)) {
9987 + /* We only load some info, don't fiddle with directory structure */
9988 + in->valid = 1;
9989 +
9990 + if(oh) {
9991 + in->variantType = oh->type;
9992 +
9993 + in->yst_mode = oh->yst_mode;
9994 +#ifdef CONFIG_YAFFS_WINCE
9995 + in->win_atime[0] = oh->win_atime[0];
9996 + in->win_ctime[0] = oh->win_ctime[0];
9997 + in->win_mtime[0] = oh->win_mtime[0];
9998 + in->win_atime[1] = oh->win_atime[1];
9999 + in->win_ctime[1] = oh->win_ctime[1];
10000 + in->win_mtime[1] = oh->win_mtime[1];
10001 +#else
10002 + in->yst_uid = oh->yst_uid;
10003 + in->yst_gid = oh->yst_gid;
10004 + in->yst_atime = oh->yst_atime;
10005 + in->yst_mtime = oh->yst_mtime;
10006 + in->yst_ctime = oh->yst_ctime;
10007 + in->yst_rdev = oh->yst_rdev;
10008 +
10009 +#endif
10010 + } else {
10011 + in->variantType = tags.extraObjectType;
10012 + in->lazyLoaded = 1;
10013 + }
10014 +
10015 + in->chunkId = chunk;
10016 +
10017 + } else if (!in->valid) {
10018 + /* we need to load this info */
10019 +
10020 + in->valid = 1;
10021 + in->chunkId = chunk;
10022 +
10023 + if(oh) {
10024 + in->variantType = oh->type;
10025 +
10026 + in->yst_mode = oh->yst_mode;
10027 +#ifdef CONFIG_YAFFS_WINCE
10028 + in->win_atime[0] = oh->win_atime[0];
10029 + in->win_ctime[0] = oh->win_ctime[0];
10030 + in->win_mtime[0] = oh->win_mtime[0];
10031 + in->win_atime[1] = oh->win_atime[1];
10032 + in->win_ctime[1] = oh->win_ctime[1];
10033 + in->win_mtime[1] = oh->win_mtime[1];
10034 +#else
10035 + in->yst_uid = oh->yst_uid;
10036 + in->yst_gid = oh->yst_gid;
10037 + in->yst_atime = oh->yst_atime;
10038 + in->yst_mtime = oh->yst_mtime;
10039 + in->yst_ctime = oh->yst_ctime;
10040 + in->yst_rdev = oh->yst_rdev;
10041 +#endif
10042 +
10043 + if (oh->shadowsObject > 0)
10044 + yaffs_HandleShadowedObject(dev,
10045 + oh->
10046 + shadowsObject,
10047 + 1);
10048 +
10049 +
10050 + yaffs_SetObjectName(in, oh->name);
10051 + parent =
10052 + yaffs_FindOrCreateObjectByNumber
10053 + (dev, oh->parentObjectId,
10054 + YAFFS_OBJECT_TYPE_DIRECTORY);
10055 +
10056 + fileSize = oh->fileSize;
10057 + isShrink = oh->isShrink;
10058 + equivalentObjectId = oh->equivalentObjectId;
10059 +
10060 + }
10061 + else {
10062 + in->variantType = tags.extraObjectType;
10063 + parent =
10064 + yaffs_FindOrCreateObjectByNumber
10065 + (dev, tags.extraParentObjectId,
10066 + YAFFS_OBJECT_TYPE_DIRECTORY);
10067 + fileSize = tags.extraFileLength;
10068 + isShrink = tags.extraIsShrinkHeader;
10069 + equivalentObjectId = tags.extraEquivalentObjectId;
10070 + in->lazyLoaded = 1;
10071 +
10072 + }
10073 + in->dirty = 0;
10074 +
10075 + /* directory stuff...
10076 + * hook up to parent
10077 + */
10078 +
10079 + if (parent->variantType ==
10080 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10081 + /* Set up as a directory */
10082 + parent->variantType =
10083 + YAFFS_OBJECT_TYPE_DIRECTORY;
10084 + INIT_LIST_HEAD(&parent->variant.
10085 + directoryVariant.
10086 + children);
10087 + } else if (parent->variantType !=
10088 + YAFFS_OBJECT_TYPE_DIRECTORY)
10089 + {
10090 + /* Hoosterman, another problem....
10091 + * We're trying to use a non-directory as a directory
10092 + */
10093 +
10094 + T(YAFFS_TRACE_ERROR,
10095 + (TSTR
10096 + ("yaffs tragedy: attempting to use non-directory as"
10097 + " a directory in scan. Put in lost+found."
10098 + TENDSTR)));
10099 + parent = dev->lostNFoundDir;
10100 + }
10101 +
10102 + yaffs_AddObjectToDirectory(parent, in);
10103 +
10104 + itsUnlinked = (parent == dev->deletedDir) ||
10105 + (parent == dev->unlinkedDir);
10106 +
10107 + if (isShrink) {
10108 + /* Mark the block as having a shrinkHeader */
10109 + bi->hasShrinkHeader = 1;
10110 + }
10111 +
10112 + /* Note re hardlinks.
10113 + * Since we might scan a hardlink before its equivalent object is scanned
10114 + * we put them all in a list.
10115 + * After scanning is complete, we should have all the objects, so we run
10116 + * through this list and fix up all the chains.
10117 + */
10118 +
10119 + switch (in->variantType) {
10120 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10121 + /* Todo got a problem */
10122 + break;
10123 + case YAFFS_OBJECT_TYPE_FILE:
10124 +
10125 + if (in->variant.fileVariant.
10126 + scannedFileSize < fileSize) {
10127 + /* This covers the case where the file size is greater
10128 + * than where the data is
10129 + * This will happen if the file is resized to be larger
10130 + * than its current data extents.
10131 + */
10132 + in->variant.fileVariant.fileSize = fileSize;
10133 + in->variant.fileVariant.scannedFileSize =
10134 + in->variant.fileVariant.fileSize;
10135 + }
10136 +
10137 + if (isShrink &&
10138 + in->variant.fileVariant.shrinkSize > fileSize) {
10139 + in->variant.fileVariant.shrinkSize = fileSize;
10140 + }
10141 +
10142 + break;
10143 + case YAFFS_OBJECT_TYPE_HARDLINK:
10144 + if(!itsUnlinked) {
10145 + in->variant.hardLinkVariant.equivalentObjectId =
10146 + equivalentObjectId;
10147 + in->hardLinks.next =
10148 + (struct list_head *) hardList;
10149 + hardList = in;
10150 + }
10151 + break;
10152 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10153 + /* Do nothing */
10154 + break;
10155 + case YAFFS_OBJECT_TYPE_SPECIAL:
10156 + /* Do nothing */
10157 + break;
10158 + case YAFFS_OBJECT_TYPE_SYMLINK:
10159 + if(oh){
10160 + in->variant.symLinkVariant.alias =
10161 + yaffs_CloneString(oh->
10162 + alias);
10163 + if(!in->variant.symLinkVariant.alias)
10164 + alloc_failed = 1;
10165 + }
10166 + break;
10167 + }
10168 +
10169 + }
10170 +
10171 + }
10172 +
10173 + } /* End of scanning for each chunk */
10174 +
10175 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10176 + /* If we got this far while scanning, then the block is fully allocated. */
10177 + state = YAFFS_BLOCK_STATE_FULL;
10178 + }
10179 +
10180 + bi->blockState = state;
10181 +
10182 + /* Now let's see if it was dirty */
10183 + if (bi->pagesInUse == 0 &&
10184 + !bi->hasShrinkHeader &&
10185 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10186 + yaffs_BlockBecameDirty(dev, blk);
10187 + }
10188 +
10189 + }
10190 +
10191 + if (altBlockIndex)
10192 + YFREE_ALT(blockIndex);
10193 + else
10194 + YFREE(blockIndex);
10195 +
10196 + /* Ok, we've done all the scanning.
10197 + * Fix up the hard link chains.
10198 + * We should now have scanned all the objects, now it's time to add these
10199 + * hardlinks.
10200 + */
10201 + yaffs_HardlinkFixup(dev,hardList);
10202 +
10203 +
10204 + /*
10205 + * Sort out state of unlinked and deleted objects.
10206 + */
10207 + {
10208 + struct list_head *i;
10209 + struct list_head *n;
10210 +
10211 + yaffs_Object *l;
10212 +
10213 + /* Soft delete all the unlinked files */
10214 + list_for_each_safe(i, n,
10215 + &dev->unlinkedDir->variant.directoryVariant.
10216 + children) {
10217 + if (i) {
10218 + l = list_entry(i, yaffs_Object, siblings);
10219 + yaffs_DestroyObject(l);
10220 + }
10221 + }
10222 +
10223 + /* Soft delete all the deletedDir files */
10224 + list_for_each_safe(i, n,
10225 + &dev->deletedDir->variant.directoryVariant.
10226 + children) {
10227 + if (i) {
10228 + l = list_entry(i, yaffs_Object, siblings);
10229 + yaffs_DestroyObject(l);
10230 +
10231 + }
10232 + }
10233 + }
10234 +
10235 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10236 +
10237 + if(alloc_failed){
10238 + return YAFFS_FAIL;
10239 + }
10240 +
10241 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10242 +
10243 + return YAFFS_OK;
10244 +}
10245 +
10246 +/*------------------------------ Directory Functions ----------------------------- */
10247 +
10248 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
10249 +{
10250 + yaffs_Device *dev = obj->myDev;
10251 +
10252 + if(dev && dev->removeObjectCallback)
10253 + dev->removeObjectCallback(obj);
10254 +
10255 + list_del_init(&obj->siblings);
10256 + obj->parent = NULL;
10257 +}
10258 +
10259 +
10260 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
10261 + yaffs_Object * obj)
10262 +{
10263 +
10264 + if (!directory) {
10265 + T(YAFFS_TRACE_ALWAYS,
10266 + (TSTR
10267 + ("tragedy: Trying to add an object to a null pointer directory"
10268 + TENDSTR)));
10269 + YBUG();
10270 + }
10271 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10272 + T(YAFFS_TRACE_ALWAYS,
10273 + (TSTR
10274 + ("tragedy: Trying to add an object to a non-directory"
10275 + TENDSTR)));
10276 + YBUG();
10277 + }
10278 +
10279 + if (obj->siblings.prev == NULL) {
10280 + /* Not initialised */
10281 + INIT_LIST_HEAD(&obj->siblings);
10282 +
10283 + } else if (!list_empty(&obj->siblings)) {
10284 + /* If it is holed up somewhere else, un hook it */
10285 + yaffs_RemoveObjectFromDirectory(obj);
10286 + }
10287 + /* Now add it */
10288 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
10289 + obj->parent = directory;
10290 +
10291 + if (directory == obj->myDev->unlinkedDir
10292 + || directory == obj->myDev->deletedDir) {
10293 + obj->unlinked = 1;
10294 + obj->myDev->nUnlinkedFiles++;
10295 + obj->renameAllowed = 0;
10296 + }
10297 +}
10298 +
10299 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
10300 + const YCHAR * name)
10301 +{
10302 + int sum;
10303 +
10304 + struct list_head *i;
10305 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10306 +
10307 + yaffs_Object *l;
10308 +
10309 + if (!name) {
10310 + return NULL;
10311 + }
10312 +
10313 + if (!directory) {
10314 + T(YAFFS_TRACE_ALWAYS,
10315 + (TSTR
10316 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10317 + TENDSTR)));
10318 + YBUG();
10319 + }
10320 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10321 + T(YAFFS_TRACE_ALWAYS,
10322 + (TSTR
10323 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10324 + YBUG();
10325 + }
10326 +
10327 + sum = yaffs_CalcNameSum(name);
10328 +
10329 + list_for_each(i, &directory->variant.directoryVariant.children) {
10330 + if (i) {
10331 + l = list_entry(i, yaffs_Object, siblings);
10332 +
10333 + yaffs_CheckObjectDetailsLoaded(l);
10334 +
10335 + /* Special case for lost-n-found */
10336 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10337 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
10338 + return l;
10339 + }
10340 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
10341 + {
10342 + /* LostnFound cunk called Objxxx
10343 + * Do a real check
10344 + */
10345 + yaffs_GetObjectName(l, buffer,
10346 + YAFFS_MAX_NAME_LENGTH);
10347 + if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
10348 + return l;
10349 + }
10350 +
10351 + }
10352 + }
10353 + }
10354 +
10355 + return NULL;
10356 +}
10357 +
10358 +
10359 +#if 0
10360 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10361 + int (*fn) (yaffs_Object *))
10362 +{
10363 + struct list_head *i;
10364 + yaffs_Object *l;
10365 +
10366 + if (!theDir) {
10367 + T(YAFFS_TRACE_ALWAYS,
10368 + (TSTR
10369 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10370 + TENDSTR)));
10371 + YBUG();
10372 + }
10373 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10374 + T(YAFFS_TRACE_ALWAYS,
10375 + (TSTR
10376 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10377 + YBUG();
10378 + }
10379 +
10380 + list_for_each(i, &theDir->variant.directoryVariant.children) {
10381 + if (i) {
10382 + l = list_entry(i, yaffs_Object, siblings);
10383 + if (l && !fn(l)) {
10384 + return YAFFS_FAIL;
10385 + }
10386 + }
10387 + }
10388 +
10389 + return YAFFS_OK;
10390 +
10391 +}
10392 +#endif
10393 +
10394 +/* GetEquivalentObject dereferences any hard links to get to the
10395 + * actual object.
10396 + */
10397 +
10398 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
10399 +{
10400 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10401 + /* We want the object id of the equivalent object, not this one */
10402 + obj = obj->variant.hardLinkVariant.equivalentObject;
10403 + yaffs_CheckObjectDetailsLoaded(obj);
10404 + }
10405 + return obj;
10406 +
10407 +}
10408 +
10409 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
10410 +{
10411 + memset(name, 0, buffSize * sizeof(YCHAR));
10412 +
10413 + yaffs_CheckObjectDetailsLoaded(obj);
10414 +
10415 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10416 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10417 + } else if (obj->chunkId <= 0) {
10418 + YCHAR locName[20];
10419 + /* make up a name */
10420 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
10421 + obj->objectId);
10422 + yaffs_strncpy(name, locName, buffSize - 1);
10423 +
10424 + }
10425 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10426 + else if (obj->shortName[0]) {
10427 + yaffs_strcpy(name, obj->shortName);
10428 + }
10429 +#endif
10430 + else {
10431 + int result;
10432 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10433 +
10434 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10435 +
10436 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10437 +
10438 + if (obj->chunkId >= 0) {
10439 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10440 + obj->chunkId, buffer,
10441 + NULL);
10442 + }
10443 + yaffs_strncpy(name, oh->name, buffSize - 1);
10444 +
10445 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10446 + }
10447 +
10448 + return yaffs_strlen(name);
10449 +}
10450 +
10451 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
10452 +{
10453 +
10454 + /* Dereference any hard linking */
10455 + obj = yaffs_GetEquivalentObject(obj);
10456 +
10457 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10458 + return obj->variant.fileVariant.fileSize;
10459 + }
10460 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10461 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10462 + } else {
10463 + /* Only a directory should drop through to here */
10464 + return obj->myDev->nDataBytesPerChunk;
10465 + }
10466 +}
10467 +
10468 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
10469 +{
10470 + int count = 0;
10471 + struct list_head *i;
10472 +
10473 + if (!obj->unlinked) {
10474 + count++; /* the object itself */
10475 + }
10476 + list_for_each(i, &obj->hardLinks) {
10477 + count++; /* add the hard links; */
10478 + }
10479 + return count;
10480 +
10481 +}
10482 +
10483 +int yaffs_GetObjectInode(yaffs_Object * obj)
10484 +{
10485 + obj = yaffs_GetEquivalentObject(obj);
10486 +
10487 + return obj->objectId;
10488 +}
10489 +
10490 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
10491 +{
10492 + obj = yaffs_GetEquivalentObject(obj);
10493 +
10494 + switch (obj->variantType) {
10495 + case YAFFS_OBJECT_TYPE_FILE:
10496 + return DT_REG;
10497 + break;
10498 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10499 + return DT_DIR;
10500 + break;
10501 + case YAFFS_OBJECT_TYPE_SYMLINK:
10502 + return DT_LNK;
10503 + break;
10504 + case YAFFS_OBJECT_TYPE_HARDLINK:
10505 + return DT_REG;
10506 + break;
10507 + case YAFFS_OBJECT_TYPE_SPECIAL:
10508 + if (S_ISFIFO(obj->yst_mode))
10509 + return DT_FIFO;
10510 + if (S_ISCHR(obj->yst_mode))
10511 + return DT_CHR;
10512 + if (S_ISBLK(obj->yst_mode))
10513 + return DT_BLK;
10514 + if (S_ISSOCK(obj->yst_mode))
10515 + return DT_SOCK;
10516 + default:
10517 + return DT_REG;
10518 + break;
10519 + }
10520 +}
10521 +
10522 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
10523 +{
10524 + obj = yaffs_GetEquivalentObject(obj);
10525 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10526 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10527 + } else {
10528 + return yaffs_CloneString(_Y(""));
10529 + }
10530 +}
10531 +
10532 +#ifndef CONFIG_YAFFS_WINCE
10533 +
10534 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
10535 +{
10536 + unsigned int valid = attr->ia_valid;
10537 +
10538 + if (valid & ATTR_MODE)
10539 + obj->yst_mode = attr->ia_mode;
10540 + if (valid & ATTR_UID)
10541 + obj->yst_uid = attr->ia_uid;
10542 + if (valid & ATTR_GID)
10543 + obj->yst_gid = attr->ia_gid;
10544 +
10545 + if (valid & ATTR_ATIME)
10546 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10547 + if (valid & ATTR_CTIME)
10548 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10549 + if (valid & ATTR_MTIME)
10550 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10551 +
10552 + if (valid & ATTR_SIZE)
10553 + yaffs_ResizeFile(obj, attr->ia_size);
10554 +
10555 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10556 +
10557 + return YAFFS_OK;
10558 +
10559 +}
10560 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
10561 +{
10562 + unsigned int valid = 0;
10563 +
10564 + attr->ia_mode = obj->yst_mode;
10565 + valid |= ATTR_MODE;
10566 + attr->ia_uid = obj->yst_uid;
10567 + valid |= ATTR_UID;
10568 + attr->ia_gid = obj->yst_gid;
10569 + valid |= ATTR_GID;
10570 +
10571 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10572 + valid |= ATTR_ATIME;
10573 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10574 + valid |= ATTR_CTIME;
10575 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
10576 + valid |= ATTR_MTIME;
10577 +
10578 + attr->ia_size = yaffs_GetFileSize(obj);
10579 + valid |= ATTR_SIZE;
10580 +
10581 + attr->ia_valid = valid;
10582 +
10583 + return YAFFS_OK;
10584 +
10585 +}
10586 +
10587 +#endif
10588 +
10589 +#if 0
10590 +int yaffs_DumpObject(yaffs_Object * obj)
10591 +{
10592 + YCHAR name[257];
10593 +
10594 + yaffs_GetObjectName(obj, name, 256);
10595 +
10596 + T(YAFFS_TRACE_ALWAYS,
10597 + (TSTR
10598 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
10599 + " chunk %d type %d size %d\n"
10600 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
10601 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
10602 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
10603 +
10604 + return YAFFS_OK;
10605 +}
10606 +#endif
10607 +
10608 +/*---------------------------- Initialisation code -------------------------------------- */
10609 +
10610 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
10611 +{
10612 +
10613 + /* Common functions, gotta have */
10614 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
10615 + return 0;
10616 +
10617 +#ifdef CONFIG_YAFFS_YAFFS2
10618 +
10619 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
10620 + if (dev->writeChunkWithTagsToNAND &&
10621 + dev->readChunkWithTagsFromNAND &&
10622 + !dev->writeChunkToNAND &&
10623 + !dev->readChunkFromNAND &&
10624 + dev->markNANDBlockBad && dev->queryNANDBlock)
10625 + return 1;
10626 +#endif
10627 +
10628 + /* Can use the "spare" style interface for yaffs1 */
10629 + if (!dev->isYaffs2 &&
10630 + !dev->writeChunkWithTagsToNAND &&
10631 + !dev->readChunkWithTagsFromNAND &&
10632 + dev->writeChunkToNAND &&
10633 + dev->readChunkFromNAND &&
10634 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
10635 + return 1;
10636 +
10637 + return 0; /* bad */
10638 +}
10639 +
10640 +
10641 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
10642 +{
10643 + /* Initialise the unlinked, deleted, root and lost and found directories */
10644 +
10645 + dev->lostNFoundDir = dev->rootDir = NULL;
10646 + dev->unlinkedDir = dev->deletedDir = NULL;
10647 +
10648 + dev->unlinkedDir =
10649 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
10650 +
10651 + dev->deletedDir =
10652 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
10653 +
10654 + dev->rootDir =
10655 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
10656 + YAFFS_ROOT_MODE | S_IFDIR);
10657 + dev->lostNFoundDir =
10658 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
10659 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
10660 +
10661 + if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
10662 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
10663 + return YAFFS_OK;
10664 + }
10665 +
10666 + return YAFFS_FAIL;
10667 +}
10668 +
10669 +int yaffs_GutsInitialise(yaffs_Device * dev)
10670 +{
10671 + int init_failed = 0;
10672 + unsigned x;
10673 + int bits;
10674 +
10675 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
10676 +
10677 + /* Check stuff that must be set */
10678 +
10679 + if (!dev) {
10680 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
10681 + return YAFFS_FAIL;
10682 + }
10683 +
10684 + dev->internalStartBlock = dev->startBlock;
10685 + dev->internalEndBlock = dev->endBlock;
10686 + dev->blockOffset = 0;
10687 + dev->chunkOffset = 0;
10688 + dev->nFreeChunks = 0;
10689 +
10690 + if (dev->startBlock == 0) {
10691 + dev->internalStartBlock = dev->startBlock + 1;
10692 + dev->internalEndBlock = dev->endBlock + 1;
10693 + dev->blockOffset = 1;
10694 + dev->chunkOffset = dev->nChunksPerBlock;
10695 + }
10696 +
10697 + /* Check geometry parameters. */
10698 +
10699 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
10700 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
10701 + dev->nChunksPerBlock < 2 ||
10702 + dev->nReservedBlocks < 2 ||
10703 + dev->internalStartBlock <= 0 ||
10704 + dev->internalEndBlock <= 0 ||
10705 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
10706 + ) {
10707 + T(YAFFS_TRACE_ALWAYS,
10708 + (TSTR
10709 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
10710 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
10711 + return YAFFS_FAIL;
10712 + }
10713 +
10714 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
10715 + T(YAFFS_TRACE_ALWAYS,
10716 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
10717 + return YAFFS_FAIL;
10718 + }
10719 +
10720 + /* Got the right mix of functions? */
10721 + if (!yaffs_CheckDevFunctions(dev)) {
10722 + /* Function missing */
10723 + T(YAFFS_TRACE_ALWAYS,
10724 + (TSTR
10725 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
10726 +
10727 + return YAFFS_FAIL;
10728 + }
10729 +
10730 + /* This is really a compilation check. */
10731 + if (!yaffs_CheckStructures()) {
10732 + T(YAFFS_TRACE_ALWAYS,
10733 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
10734 + return YAFFS_FAIL;
10735 + }
10736 +
10737 + if (dev->isMounted) {
10738 + T(YAFFS_TRACE_ALWAYS,
10739 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
10740 + return YAFFS_FAIL;
10741 + }
10742 +
10743 + /* Finished with most checks. One or two more checks happen later on too. */
10744 +
10745 + dev->isMounted = 1;
10746 +
10747 +
10748 +
10749 + /* OK now calculate a few things for the device */
10750 +
10751 + /*
10752 + * Calculate all the chunk size manipulation numbers:
10753 + */
10754 + /* Start off assuming it is a power of 2 */
10755 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
10756 + dev->chunkMask = (1<<dev->chunkShift) - 1;
10757 +
10758 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
10759 + /* Yes it is a power of 2, disable crumbs */
10760 + dev->crumbMask = 0;
10761 + dev->crumbShift = 0;
10762 + dev->crumbsPerChunk = 0;
10763 + } else {
10764 + /* Not a power of 2, use crumbs instead */
10765 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
10766 + dev->crumbMask = (1<<dev->crumbShift)-1;
10767 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
10768 + dev->chunkShift = 0;
10769 + dev->chunkMask = 0;
10770 + }
10771 +
10772 +
10773 + /*
10774 + * Calculate chunkGroupBits.
10775 + * We need to find the next power of 2 > than internalEndBlock
10776 + */
10777 +
10778 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
10779 +
10780 + bits = ShiftsGE(x);
10781 +
10782 + /* Set up tnode width if wide tnodes are enabled. */
10783 + if(!dev->wideTnodesDisabled){
10784 + /* bits must be even so that we end up with 32-bit words */
10785 + if(bits & 1)
10786 + bits++;
10787 + if(bits < 16)
10788 + dev->tnodeWidth = 16;
10789 + else
10790 + dev->tnodeWidth = bits;
10791 + }
10792 + else
10793 + dev->tnodeWidth = 16;
10794 +
10795 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
10796 +
10797 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
10798 + * so if the bitwidth of the
10799 + * chunk range we're using is greater than 16 we need
10800 + * to figure out chunk shift and chunkGroupSize
10801 + */
10802 +
10803 + if (bits <= dev->tnodeWidth)
10804 + dev->chunkGroupBits = 0;
10805 + else
10806 + dev->chunkGroupBits = bits - dev->tnodeWidth;
10807 +
10808 +
10809 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
10810 +
10811 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
10812 + /* We have a problem because the soft delete won't work if
10813 + * the chunk group size > chunks per block.
10814 + * This can be remedied by using larger "virtual blocks".
10815 + */
10816 + T(YAFFS_TRACE_ALWAYS,
10817 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
10818 +
10819 + return YAFFS_FAIL;
10820 + }
10821 +
10822 + /* OK, we've finished verifying the device, lets continue with initialisation */
10823 +
10824 + /* More device initialisation */
10825 + dev->garbageCollections = 0;
10826 + dev->passiveGarbageCollections = 0;
10827 + dev->currentDirtyChecker = 0;
10828 + dev->bufferedBlock = -1;
10829 + dev->doingBufferedBlockRewrite = 0;
10830 + dev->nDeletedFiles = 0;
10831 + dev->nBackgroundDeletions = 0;
10832 + dev->nUnlinkedFiles = 0;
10833 + dev->eccFixed = 0;
10834 + dev->eccUnfixed = 0;
10835 + dev->tagsEccFixed = 0;
10836 + dev->tagsEccUnfixed = 0;
10837 + dev->nErasureFailures = 0;
10838 + dev->nErasedBlocks = 0;
10839 + dev->isDoingGC = 0;
10840 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
10841 +
10842 + /* Initialise temporary buffers and caches. */
10843 + if(!yaffs_InitialiseTempBuffers(dev))
10844 + init_failed = 1;
10845 +
10846 + dev->srCache = NULL;
10847 + dev->gcCleanupList = NULL;
10848 +
10849 +
10850 + if (!init_failed &&
10851 + dev->nShortOpCaches > 0) {
10852 + int i;
10853 + __u8 *buf;
10854 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
10855 +
10856 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
10857 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
10858 + }
10859 +
10860 + buf = dev->srCache = YMALLOC(srCacheBytes);
10861 +
10862 + if(dev->srCache)
10863 + memset(dev->srCache,0,srCacheBytes);
10864 +
10865 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
10866 + dev->srCache[i].object = NULL;
10867 + dev->srCache[i].lastUse = 0;
10868 + dev->srCache[i].dirty = 0;
10869 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
10870 + }
10871 + if(!buf)
10872 + init_failed = 1;
10873 +
10874 + dev->srLastUse = 0;
10875 + }
10876 +
10877 + dev->cacheHits = 0;
10878 +
10879 + if(!init_failed){
10880 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
10881 + if(!dev->gcCleanupList)
10882 + init_failed = 1;
10883 + }
10884 +
10885 + if (dev->isYaffs2) {
10886 + dev->useHeaderFileSize = 1;
10887 + }
10888 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10889 + init_failed = 1;
10890 +
10891 + yaffs_InitialiseTnodes(dev);
10892 + yaffs_InitialiseObjects(dev);
10893 +
10894 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10895 + init_failed = 1;
10896 +
10897 +
10898 + if(!init_failed){
10899 + /* Now scan the flash. */
10900 + if (dev->isYaffs2) {
10901 + if(yaffs_CheckpointRestore(dev)) {
10902 + T(YAFFS_TRACE_ALWAYS,
10903 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
10904 + } else {
10905 +
10906 + /* Clean up the mess caused by an aborted checkpoint load
10907 + * and scan backwards.
10908 + */
10909 + yaffs_DeinitialiseBlocks(dev);
10910 + yaffs_DeinitialiseTnodes(dev);
10911 + yaffs_DeinitialiseObjects(dev);
10912 +
10913 +
10914 + dev->nErasedBlocks = 0;
10915 + dev->nFreeChunks = 0;
10916 + dev->allocationBlock = -1;
10917 + dev->allocationPage = -1;
10918 + dev->nDeletedFiles = 0;
10919 + dev->nUnlinkedFiles = 0;
10920 + dev->nBackgroundDeletions = 0;
10921 + dev->oldestDirtySequence = 0;
10922 +
10923 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10924 + init_failed = 1;
10925 +
10926 + yaffs_InitialiseTnodes(dev);
10927 + yaffs_InitialiseObjects(dev);
10928 +
10929 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10930 + init_failed = 1;
10931 +
10932 + if(!init_failed && !yaffs_ScanBackwards(dev))
10933 + init_failed = 1;
10934 + }
10935 + }else
10936 + if(!yaffs_Scan(dev))
10937 + init_failed = 1;
10938 + }
10939 +
10940 + if(init_failed){
10941 + /* Clean up the mess */
10942 + T(YAFFS_TRACE_TRACING,
10943 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
10944 +
10945 + yaffs_Deinitialise(dev);
10946 + return YAFFS_FAIL;
10947 + }
10948 +
10949 + /* Zero out stats */
10950 + dev->nPageReads = 0;
10951 + dev->nPageWrites = 0;
10952 + dev->nBlockErasures = 0;
10953 + dev->nGCCopies = 0;
10954 + dev->nRetriedWrites = 0;
10955 +
10956 + dev->nRetiredBlocks = 0;
10957 +
10958 + yaffs_VerifyFreeChunks(dev);
10959 + yaffs_VerifyBlocks(dev);
10960 +
10961 +
10962 + T(YAFFS_TRACE_TRACING,
10963 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
10964 + return YAFFS_OK;
10965 +
10966 +}
10967 +
10968 +void yaffs_Deinitialise(yaffs_Device * dev)
10969 +{
10970 + if (dev->isMounted) {
10971 + int i;
10972 +
10973 + yaffs_DeinitialiseBlocks(dev);
10974 + yaffs_DeinitialiseTnodes(dev);
10975 + yaffs_DeinitialiseObjects(dev);
10976 + if (dev->nShortOpCaches > 0 &&
10977 + dev->srCache) {
10978 +
10979 + for (i = 0; i < dev->nShortOpCaches; i++) {
10980 + if(dev->srCache[i].data)
10981 + YFREE(dev->srCache[i].data);
10982 + dev->srCache[i].data = NULL;
10983 + }
10984 +
10985 + YFREE(dev->srCache);
10986 + dev->srCache = NULL;
10987 + }
10988 +
10989 + YFREE(dev->gcCleanupList);
10990 +
10991 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
10992 + YFREE(dev->tempBuffer[i].buffer);
10993 + }
10994 +
10995 + dev->isMounted = 0;
10996 + }
10997 +
10998 +}
10999 +
11000 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
11001 +{
11002 + int nFree;
11003 + int b;
11004 +
11005 + yaffs_BlockInfo *blk;
11006 +
11007 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11008 + b++) {
11009 + blk = yaffs_GetBlockInfo(dev, b);
11010 +
11011 + switch (blk->blockState) {
11012 + case YAFFS_BLOCK_STATE_EMPTY:
11013 + case YAFFS_BLOCK_STATE_ALLOCATING:
11014 + case YAFFS_BLOCK_STATE_COLLECTING:
11015 + case YAFFS_BLOCK_STATE_FULL:
11016 + nFree +=
11017 + (dev->nChunksPerBlock - blk->pagesInUse +
11018 + blk->softDeletions);
11019 + break;
11020 + default:
11021 + break;
11022 + }
11023 +
11024 + }
11025 +
11026 + return nFree;
11027 +}
11028 +
11029 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
11030 +{
11031 + /* This is what we report to the outside world */
11032 +
11033 + int nFree;
11034 + int nDirtyCacheChunks;
11035 + int blocksForCheckpoint;
11036 +
11037 +#if 1
11038 + nFree = dev->nFreeChunks;
11039 +#else
11040 + nFree = yaffs_CountFreeChunks(dev);
11041 +#endif
11042 +
11043 + nFree += dev->nDeletedFiles;
11044 +
11045 + /* Now count the number of dirty chunks in the cache and subtract those */
11046 +
11047 + {
11048 + int i;
11049 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11050 + if (dev->srCache[i].dirty)
11051 + nDirtyCacheChunks++;
11052 + }
11053 + }
11054 +
11055 + nFree -= nDirtyCacheChunks;
11056 +
11057 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11058 +
11059 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11060 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
11061 + if(blocksForCheckpoint < 0)
11062 + blocksForCheckpoint = 0;
11063 +
11064 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11065 +
11066 + if (nFree < 0)
11067 + nFree = 0;
11068 +
11069 + return nFree;
11070 +
11071 +}
11072 +
11073 +static int yaffs_freeVerificationFailures;
11074 +
11075 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
11076 +{
11077 + int counted;
11078 + int difference;
11079 +
11080 + if(yaffs_SkipVerification(dev))
11081 + return;
11082 +
11083 + counted = yaffs_CountFreeChunks(dev);
11084 +
11085 + difference = dev->nFreeChunks - counted;
11086 +
11087 + if (difference) {
11088 + T(YAFFS_TRACE_ALWAYS,
11089 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11090 + dev->nFreeChunks, counted, difference));
11091 + yaffs_freeVerificationFailures++;
11092 + }
11093 +}
11094 +
11095 +/*---------------------------------------- YAFFS test code ----------------------*/
11096 +
11097 +#define yaffs_CheckStruct(structure,syze, name) \
11098 + if(sizeof(structure) != syze) \
11099 + { \
11100 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
11101 + name,syze,sizeof(structure))); \
11102 + return YAFFS_FAIL; \
11103 + }
11104 +
11105 +static int yaffs_CheckStructures(void)
11106 +{
11107 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
11108 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
11109 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
11110 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11111 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
11112 +#endif
11113 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
11114 +
11115 + return YAFFS_OK;
11116 +}
11117 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.h linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h
11118 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
11119 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h 2007-05-30 13:17:16.000000000 +0200
11120 @@ -0,0 +1,902 @@
11121 +/*
11122 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11123 + *
11124 + * Copyright (C) 2002-2007 Aleph One Ltd.
11125 + * for Toby Churchill Ltd and Brightstar Engineering
11126 + *
11127 + * Created by Charles Manning <charles@aleph1.co.uk>
11128 + *
11129 + * This program is free software; you can redistribute it and/or modify
11130 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11131 + * published by the Free Software Foundation.
11132 + *
11133 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11134 + */
11135 +
11136 +#ifndef __YAFFS_GUTS_H__
11137 +#define __YAFFS_GUTS_H__
11138 +
11139 +#include "devextras.h"
11140 +#include "yportenv.h"
11141 +
11142 +#define YAFFS_OK 1
11143 +#define YAFFS_FAIL 0
11144 +
11145 +/* Give us a Y=0x59,
11146 + * Give us an A=0x41,
11147 + * Give us an FF=0xFF
11148 + * Give us an S=0x53
11149 + * And what have we got...
11150 + */
11151 +#define YAFFS_MAGIC 0x5941FF53
11152 +
11153 +#define YAFFS_NTNODES_LEVEL0 16
11154 +#define YAFFS_TNODES_LEVEL0_BITS 4
11155 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11156 +
11157 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11158 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11159 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11160 +#define YAFFS_TNODES_MAX_LEVEL 6
11161 +
11162 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11163 +#define YAFFS_BYTES_PER_SPARE 16
11164 +#define YAFFS_BYTES_PER_CHUNK 512
11165 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11166 +#define YAFFS_CHUNKS_PER_BLOCK 32
11167 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11168 +#endif
11169 +
11170 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11171 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11172 +
11173 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11174 +
11175 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11176 +
11177 +#define YAFFS_ALLOCATION_NOBJECTS 100
11178 +#define YAFFS_ALLOCATION_NTNODES 100
11179 +#define YAFFS_ALLOCATION_NLINKS 100
11180 +
11181 +#define YAFFS_NOBJECT_BUCKETS 256
11182 +
11183 +
11184 +#define YAFFS_OBJECT_SPACE 0x40000
11185 +
11186 +#define YAFFS_CHECKPOINT_VERSION 3
11187 +
11188 +#ifdef CONFIG_YAFFS_UNICODE
11189 +#define YAFFS_MAX_NAME_LENGTH 127
11190 +#define YAFFS_MAX_ALIAS_LENGTH 79
11191 +#else
11192 +#define YAFFS_MAX_NAME_LENGTH 255
11193 +#define YAFFS_MAX_ALIAS_LENGTH 159
11194 +#endif
11195 +
11196 +#define YAFFS_SHORT_NAME_LENGTH 15
11197 +
11198 +/* Some special object ids for pseudo objects */
11199 +#define YAFFS_OBJECTID_ROOT 1
11200 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11201 +#define YAFFS_OBJECTID_UNLINKED 3
11202 +#define YAFFS_OBJECTID_DELETED 4
11203 +
11204 +/* Sseudo object ids for checkpointing */
11205 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11206 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11207 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11208 +
11209 +/* */
11210 +
11211 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11212 +
11213 +#define YAFFS_N_TEMP_BUFFERS 4
11214 +
11215 +/* We limit the number attempts at sucessfully saving a chunk of data.
11216 + * Small-page devices have 32 pages per block; large-page devices have 64.
11217 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11218 + */
11219 +#define YAFFS_WR_ATTEMPTS (5*64)
11220 +
11221 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11222 + * The range is limited slightly to help distinguish bad numbers from good.
11223 + * This also allows us to perhaps in the future use special numbers for
11224 + * special purposes.
11225 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11226 + * and is a larger number than the lifetime of a 2GB device.
11227 + */
11228 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11229 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11230 +
11231 +/* ChunkCache is used for short read/write operations.*/
11232 +typedef struct {
11233 + struct yaffs_ObjectStruct *object;
11234 + int chunkId;
11235 + int lastUse;
11236 + int dirty;
11237 + int nBytes; /* Only valid if the cache is dirty */
11238 + int locked; /* Can't push out or flush while locked. */
11239 +#ifdef CONFIG_YAFFS_YAFFS2
11240 + __u8 *data;
11241 +#else
11242 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11243 +#endif
11244 +} yaffs_ChunkCache;
11245 +
11246 +
11247 +
11248 +/* Tags structures in RAM
11249 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11250 + * the structure size will get blown out.
11251 + */
11252 +
11253 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11254 +typedef struct {
11255 + unsigned chunkId:20;
11256 + unsigned serialNumber:2;
11257 + unsigned byteCount:10;
11258 + unsigned objectId:18;
11259 + unsigned ecc:12;
11260 + unsigned unusedStuff:2;
11261 +
11262 +} yaffs_Tags;
11263 +
11264 +typedef union {
11265 + yaffs_Tags asTags;
11266 + __u8 asBytes[8];
11267 +} yaffs_TagsUnion;
11268 +
11269 +#endif
11270 +
11271 +/* Stuff used for extended tags in YAFFS2 */
11272 +
11273 +typedef enum {
11274 + YAFFS_ECC_RESULT_UNKNOWN,
11275 + YAFFS_ECC_RESULT_NO_ERROR,
11276 + YAFFS_ECC_RESULT_FIXED,
11277 + YAFFS_ECC_RESULT_UNFIXED
11278 +} yaffs_ECCResult;
11279 +
11280 +typedef enum {
11281 + YAFFS_OBJECT_TYPE_UNKNOWN,
11282 + YAFFS_OBJECT_TYPE_FILE,
11283 + YAFFS_OBJECT_TYPE_SYMLINK,
11284 + YAFFS_OBJECT_TYPE_DIRECTORY,
11285 + YAFFS_OBJECT_TYPE_HARDLINK,
11286 + YAFFS_OBJECT_TYPE_SPECIAL
11287 +} yaffs_ObjectType;
11288 +
11289 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11290 +
11291 +typedef struct {
11292 +
11293 + unsigned validMarker0;
11294 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11295 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11296 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11297 + unsigned byteCount; /* Only valid for data chunks */
11298 +
11299 + /* The following stuff only has meaning when we read */
11300 + yaffs_ECCResult eccResult;
11301 + unsigned blockBad;
11302 +
11303 + /* YAFFS 1 stuff */
11304 + unsigned chunkDeleted; /* The chunk is marked deleted */
11305 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11306 +
11307 + /* YAFFS2 stuff */
11308 + unsigned sequenceNumber; /* The sequence number of this block */
11309 +
11310 + /* Extra info if this is an object header (YAFFS2 only) */
11311 +
11312 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11313 + unsigned extraParentObjectId; /* The parent object */
11314 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11315 + unsigned extraShadows; /* Does this shadow another object? */
11316 +
11317 + yaffs_ObjectType extraObjectType; /* What object type? */
11318 +
11319 + unsigned extraFileLength; /* Length if it is a file */
11320 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11321 +
11322 + unsigned validMarker1;
11323 +
11324 +} yaffs_ExtendedTags;
11325 +
11326 +/* Spare structure for YAFFS1 */
11327 +typedef struct {
11328 + __u8 tagByte0;
11329 + __u8 tagByte1;
11330 + __u8 tagByte2;
11331 + __u8 tagByte3;
11332 + __u8 pageStatus; /* set to 0 to delete the chunk */
11333 + __u8 blockStatus;
11334 + __u8 tagByte4;
11335 + __u8 tagByte5;
11336 + __u8 ecc1[3];
11337 + __u8 tagByte6;
11338 + __u8 tagByte7;
11339 + __u8 ecc2[3];
11340 +} yaffs_Spare;
11341 +
11342 +/*Special structure for passing through to mtd */
11343 +struct yaffs_NANDSpare {
11344 + yaffs_Spare spare;
11345 + int eccres1;
11346 + int eccres2;
11347 +};
11348 +
11349 +/* Block data in RAM */
11350 +
11351 +typedef enum {
11352 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11353 +
11354 + YAFFS_BLOCK_STATE_SCANNING,
11355 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11356 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11357 + * but it needs to be scanned to determine its true state.
11358 + * This state is only valid during yaffs_Scan.
11359 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11360 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11361 + */
11362 +
11363 + YAFFS_BLOCK_STATE_EMPTY,
11364 + /* This block is empty */
11365 +
11366 + YAFFS_BLOCK_STATE_ALLOCATING,
11367 + /* This block is partially allocated.
11368 + * At least one page holds valid data.
11369 + * This is the one currently being used for page
11370 + * allocation. Should never be more than one of these
11371 + */
11372 +
11373 + YAFFS_BLOCK_STATE_FULL,
11374 + /* All the pages in this block have been allocated.
11375 + */
11376 +
11377 + YAFFS_BLOCK_STATE_DIRTY,
11378 + /* All pages have been allocated and deleted.
11379 + * Erase me, reuse me.
11380 + */
11381 +
11382 + YAFFS_BLOCK_STATE_CHECKPOINT,
11383 + /* This block is assigned to holding checkpoint data.
11384 + */
11385 +
11386 + YAFFS_BLOCK_STATE_COLLECTING,
11387 + /* This block is being garbage collected */
11388 +
11389 + YAFFS_BLOCK_STATE_DEAD
11390 + /* This block has failed and is not in use */
11391 +} yaffs_BlockState;
11392 +
11393 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11394 +
11395 +
11396 +typedef struct {
11397 +
11398 + int softDeletions:10; /* number of soft deleted pages */
11399 + int pagesInUse:10; /* number of pages in use */
11400 + yaffs_BlockState blockState:4; /* One of the above block states */
11401 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11402 + /* and retire the block. */
11403 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
11404 + __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
11405 + It should be prioritised for GC */
11406 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11407 +
11408 +#ifdef CONFIG_YAFFS_YAFFS2
11409 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11410 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11411 +#endif
11412 +
11413 +} yaffs_BlockInfo;
11414 +
11415 +/* -------------------------- Object structure -------------------------------*/
11416 +/* This is the object structure as stored on NAND */
11417 +
11418 +typedef struct {
11419 + yaffs_ObjectType type;
11420 +
11421 + /* Apply to everything */
11422 + int parentObjectId;
11423 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11424 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11425 +
11426 + /* Thes following apply to directories, files, symlinks - not hard links */
11427 + __u32 yst_mode; /* protection */
11428 +
11429 +#ifdef CONFIG_YAFFS_WINCE
11430 + __u32 notForWinCE[5];
11431 +#else
11432 + __u32 yst_uid;
11433 + __u32 yst_gid;
11434 + __u32 yst_atime;
11435 + __u32 yst_mtime;
11436 + __u32 yst_ctime;
11437 +#endif
11438 +
11439 + /* File size applies to files only */
11440 + int fileSize;
11441 +
11442 + /* Equivalent object id applies to hard links only. */
11443 + int equivalentObjectId;
11444 +
11445 + /* Alias is for symlinks only. */
11446 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11447 +
11448 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11449 +
11450 +#ifdef CONFIG_YAFFS_WINCE
11451 + __u32 win_ctime[2];
11452 + __u32 win_atime[2];
11453 + __u32 win_mtime[2];
11454 + __u32 roomToGrow[4];
11455 +#else
11456 + __u32 roomToGrow[10];
11457 +#endif
11458 +
11459 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11460 +
11461 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11462 + __u32 isShrink;
11463 +
11464 +} yaffs_ObjectHeader;
11465 +
11466 +/*--------------------------- Tnode -------------------------- */
11467 +
11468 +union yaffs_Tnode_union {
11469 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11470 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11471 +#else
11472 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11473 +#endif
11474 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11475 +
11476 +};
11477 +
11478 +typedef union yaffs_Tnode_union yaffs_Tnode;
11479 +
11480 +struct yaffs_TnodeList_struct {
11481 + struct yaffs_TnodeList_struct *next;
11482 + yaffs_Tnode *tnodes;
11483 +};
11484 +
11485 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11486 +
11487 +/*------------------------ Object -----------------------------*/
11488 +/* An object can be one of:
11489 + * - a directory (no data, has children links
11490 + * - a regular file (data.... not prunes :->).
11491 + * - a symlink [symbolic link] (the alias).
11492 + * - a hard link
11493 + */
11494 +
11495 +typedef struct {
11496 + __u32 fileSize;
11497 + __u32 scannedFileSize;
11498 + __u32 shrinkSize;
11499 + int topLevel;
11500 + yaffs_Tnode *top;
11501 +} yaffs_FileStructure;
11502 +
11503 +typedef struct {
11504 + struct list_head children; /* list of child links */
11505 +} yaffs_DirectoryStructure;
11506 +
11507 +typedef struct {
11508 + YCHAR *alias;
11509 +} yaffs_SymLinkStructure;
11510 +
11511 +typedef struct {
11512 + struct yaffs_ObjectStruct *equivalentObject;
11513 + __u32 equivalentObjectId;
11514 +} yaffs_HardLinkStructure;
11515 +
11516 +typedef union {
11517 + yaffs_FileStructure fileVariant;
11518 + yaffs_DirectoryStructure directoryVariant;
11519 + yaffs_SymLinkStructure symLinkVariant;
11520 + yaffs_HardLinkStructure hardLinkVariant;
11521 +} yaffs_ObjectVariant;
11522 +
11523 +struct yaffs_ObjectStruct {
11524 + __u8 deleted:1; /* This should only apply to unlinked files. */
11525 + __u8 softDeleted:1; /* it has also been soft deleted */
11526 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11527 + __u8 fake:1; /* A fake object has no presence on NAND. */
11528 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11529 + __u8 unlinkAllowed:1;
11530 + __u8 dirty:1; /* the object needs to be written to flash */
11531 + __u8 valid:1; /* When the file system is being loaded up, this
11532 + * object might be created before the data
11533 + * is available (ie. file data records appear before the header).
11534 + */
11535 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11536 +
11537 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11538 + * still in the inode cache. Free of object is defered.
11539 + * until the inode is released.
11540 + */
11541 +
11542 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11543 + __u16 sum; /* sum of the name to speed searching */
11544 +
11545 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11546 +
11547 + struct list_head hashLink; /* list of objects in this hash bucket */
11548 +
11549 + struct list_head hardLinks; /* all the equivalent hard linked objects */
11550 +
11551 + /* directory structure stuff */
11552 + /* also used for linking up the free list */
11553 + struct yaffs_ObjectStruct *parent;
11554 + struct list_head siblings;
11555 +
11556 + /* Where's my object header in NAND? */
11557 + int chunkId;
11558 +
11559 + int nDataChunks; /* Number of data chunks attached to the file. */
11560 +
11561 + __u32 objectId; /* the object id value */
11562 +
11563 + __u32 yst_mode;
11564 +
11565 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11566 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11567 +#endif
11568 +
11569 +#ifndef __KERNEL__
11570 + __u32 inUse;
11571 +#endif
11572 +
11573 +#ifdef CONFIG_YAFFS_WINCE
11574 + __u32 win_ctime[2];
11575 + __u32 win_mtime[2];
11576 + __u32 win_atime[2];
11577 +#else
11578 + __u32 yst_uid;
11579 + __u32 yst_gid;
11580 + __u32 yst_atime;
11581 + __u32 yst_mtime;
11582 + __u32 yst_ctime;
11583 +#endif
11584 +
11585 + __u32 yst_rdev;
11586 +
11587 +#ifdef __KERNEL__
11588 + struct inode *myInode;
11589 +
11590 +#endif
11591 +
11592 + yaffs_ObjectType variantType;
11593 +
11594 + yaffs_ObjectVariant variant;
11595 +
11596 +};
11597 +
11598 +typedef struct yaffs_ObjectStruct yaffs_Object;
11599 +
11600 +struct yaffs_ObjectList_struct {
11601 + yaffs_Object *objects;
11602 + struct yaffs_ObjectList_struct *next;
11603 +};
11604 +
11605 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
11606 +
11607 +typedef struct {
11608 + struct list_head list;
11609 + int count;
11610 +} yaffs_ObjectBucket;
11611 +
11612 +
11613 +/* yaffs_CheckpointObject holds the definition of an object as dumped
11614 + * by checkpointing.
11615 + */
11616 +
11617 +typedef struct {
11618 + int structType;
11619 + __u32 objectId;
11620 + __u32 parentId;
11621 + int chunkId;
11622 +
11623 + yaffs_ObjectType variantType:3;
11624 + __u8 deleted:1;
11625 + __u8 softDeleted:1;
11626 + __u8 unlinked:1;
11627 + __u8 fake:1;
11628 + __u8 renameAllowed:1;
11629 + __u8 unlinkAllowed:1;
11630 + __u8 serial;
11631 +
11632 + int nDataChunks;
11633 + __u32 fileSizeOrEquivalentObjectId;
11634 +
11635 +}yaffs_CheckpointObject;
11636 +
11637 +/*--------------------- Temporary buffers ----------------
11638 + *
11639 + * These are chunk-sized working buffers. Each device has a few
11640 + */
11641 +
11642 +typedef struct {
11643 + __u8 *buffer;
11644 + int line; /* track from whence this buffer was allocated */
11645 + int maxLine;
11646 +} yaffs_TempBuffer;
11647 +
11648 +/*----------------- Device ---------------------------------*/
11649 +
11650 +struct yaffs_DeviceStruct {
11651 + struct list_head devList;
11652 + const char *name;
11653 +
11654 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
11655 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
11656 + int nChunksPerBlock; /* does not need to be a power of 2 */
11657 + int nBytesPerSpare; /* spare area size */
11658 + int startBlock; /* Start block we're allowed to use */
11659 + int endBlock; /* End block we're allowed to use */
11660 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
11661 + /* reserved blocks on NOR and RAM. */
11662 +
11663 +
11664 + /* Stuff used by the shared space checkpointing mechanism */
11665 + /* If this value is zero, then this mechanism is disabled */
11666 +
11667 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
11668 +
11669 +
11670 +
11671 +
11672 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
11673 + * the number of short op caches (don't use too many)
11674 + */
11675 +
11676 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
11677 +
11678 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
11679 +
11680 + void *genericDevice; /* Pointer to device context
11681 + * On an mtd this holds the mtd pointer.
11682 + */
11683 + void *superBlock;
11684 +
11685 + /* NAND access functions (Must be set before calling YAFFS)*/
11686 +
11687 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
11688 + int chunkInNAND, const __u8 * data,
11689 + const yaffs_Spare * spare);
11690 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
11691 + int chunkInNAND, __u8 * data,
11692 + yaffs_Spare * spare);
11693 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
11694 + int blockInNAND);
11695 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
11696 +
11697 +#ifdef CONFIG_YAFFS_YAFFS2
11698 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
11699 + int chunkInNAND, const __u8 * data,
11700 + const yaffs_ExtendedTags * tags);
11701 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
11702 + int chunkInNAND, __u8 * data,
11703 + yaffs_ExtendedTags * tags);
11704 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
11705 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
11706 + yaffs_BlockState * state, int *sequenceNumber);
11707 +#endif
11708 +
11709 + int isYaffs2;
11710 +
11711 + /* The removeObjectCallback function must be supplied by OS flavours that
11712 + * need it. The Linux kernel does not use this, but yaffs direct does use
11713 + * it to implement the faster readdir
11714 + */
11715 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
11716 +
11717 + /* Callback to mark the superblock dirsty */
11718 + void (*markSuperBlockDirty)(void * superblock);
11719 +
11720 + int wideTnodesDisabled; /* Set to disable wide tnodes */
11721 +
11722 +
11723 + /* End of stuff that must be set before initialisation. */
11724 +
11725 + /* Checkpoint control. Can be set before or after initialisation */
11726 + __u8 skipCheckpointRead;
11727 + __u8 skipCheckpointWrite;
11728 +
11729 + /* Runtime parameters. Set up by YAFFS. */
11730 +
11731 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
11732 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
11733 +
11734 + /* Stuff to support wide tnodes */
11735 + __u32 tnodeWidth;
11736 + __u32 tnodeMask;
11737 +
11738 + /* Stuff to support various file offses to chunk/offset translations */
11739 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
11740 + __u32 crumbMask;
11741 + __u32 crumbShift;
11742 + __u32 crumbsPerChunk;
11743 +
11744 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
11745 + __u32 chunkShift;
11746 + __u32 chunkMask;
11747 +
11748 +
11749 +#ifdef __KERNEL__
11750 +
11751 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
11752 + struct semaphore grossLock; /* Gross locking semaphore */
11753 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
11754 + * at compile time so we have to allocate it.
11755 + */
11756 + void (*putSuperFunc) (struct super_block * sb);
11757 +#endif
11758 +
11759 + int isMounted;
11760 +
11761 + int isCheckpointed;
11762 +
11763 +
11764 + /* Stuff to support block offsetting to support start block zero */
11765 + int internalStartBlock;
11766 + int internalEndBlock;
11767 + int blockOffset;
11768 + int chunkOffset;
11769 +
11770 +
11771 + /* Runtime checkpointing stuff */
11772 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
11773 + int checkpointByteCount;
11774 + int checkpointByteOffset;
11775 + __u8 *checkpointBuffer;
11776 + int checkpointOpenForWrite;
11777 + int blocksInCheckpoint;
11778 + int checkpointCurrentChunk;
11779 + int checkpointCurrentBlock;
11780 + int checkpointNextBlock;
11781 + int *checkpointBlockList;
11782 + int checkpointMaxBlocks;
11783 + __u32 checkpointSum;
11784 + __u32 checkpointXor;
11785 +
11786 + /* Block Info */
11787 + yaffs_BlockInfo *blockInfo;
11788 + __u8 *chunkBits; /* bitmap of chunks in use */
11789 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
11790 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
11791 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
11792 + * Must be consistent with nChunksPerBlock.
11793 + */
11794 +
11795 + int nErasedBlocks;
11796 + int allocationBlock; /* Current block being allocated off */
11797 + __u32 allocationPage;
11798 + int allocationBlockFinder; /* Used to search for next allocation block */
11799 +
11800 + /* Runtime state */
11801 + int nTnodesCreated;
11802 + yaffs_Tnode *freeTnodes;
11803 + int nFreeTnodes;
11804 + yaffs_TnodeList *allocatedTnodeList;
11805 +
11806 + int isDoingGC;
11807 +
11808 + int nObjectsCreated;
11809 + yaffs_Object *freeObjects;
11810 + int nFreeObjects;
11811 +
11812 + yaffs_ObjectList *allocatedObjectList;
11813 +
11814 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
11815 +
11816 + int nFreeChunks;
11817 +
11818 + int currentDirtyChecker; /* Used to find current dirtiest block */
11819 +
11820 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
11821 + int nonAggressiveSkip; /* GC state/mode */
11822 +
11823 + /* Statistcs */
11824 + int nPageWrites;
11825 + int nPageReads;
11826 + int nBlockErasures;
11827 + int nErasureFailures;
11828 + int nGCCopies;
11829 + int garbageCollections;
11830 + int passiveGarbageCollections;
11831 + int nRetriedWrites;
11832 + int nRetiredBlocks;
11833 + int eccFixed;
11834 + int eccUnfixed;
11835 + int tagsEccFixed;
11836 + int tagsEccUnfixed;
11837 + int nDeletions;
11838 + int nUnmarkedDeletions;
11839 +
11840 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
11841 +
11842 + /* Special directories */
11843 + yaffs_Object *rootDir;
11844 + yaffs_Object *lostNFoundDir;
11845 +
11846 + /* Buffer areas for storing data to recover from write failures TODO
11847 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
11848 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
11849 + */
11850 +
11851 + int bufferedBlock; /* Which block is buffered here? */
11852 + int doingBufferedBlockRewrite;
11853 +
11854 + yaffs_ChunkCache *srCache;
11855 + int srLastUse;
11856 +
11857 + int cacheHits;
11858 +
11859 + /* Stuff for background deletion and unlinked files.*/
11860 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
11861 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
11862 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
11863 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11864 + int nUnlinkedFiles; /* Count of unlinked files. */
11865 + int nBackgroundDeletions; /* Count of background deletions. */
11866 +
11867 +
11868 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
11869 + int maxTemp;
11870 + int unmanagedTempAllocations;
11871 + int unmanagedTempDeallocations;
11872 +
11873 + /* yaffs2 runtime stuff */
11874 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11875 + unsigned oldestDirtySequence;
11876 +
11877 +};
11878 +
11879 +typedef struct yaffs_DeviceStruct yaffs_Device;
11880 +
11881 +/* The static layout of bllock usage etc is stored in the super block header */
11882 +typedef struct {
11883 + int StructType;
11884 + int version;
11885 + int checkpointStartBlock;
11886 + int checkpointEndBlock;
11887 + int startBlock;
11888 + int endBlock;
11889 + int rfu[100];
11890 +} yaffs_SuperBlockHeader;
11891 +
11892 +/* The CheckpointDevice structure holds the device information that changes at runtime and
11893 + * must be preserved over unmount/mount cycles.
11894 + */
11895 +typedef struct {
11896 + int structType;
11897 + int nErasedBlocks;
11898 + int allocationBlock; /* Current block being allocated off */
11899 + __u32 allocationPage;
11900 + int nFreeChunks;
11901 +
11902 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11903 + int nUnlinkedFiles; /* Count of unlinked files. */
11904 + int nBackgroundDeletions; /* Count of background deletions. */
11905 +
11906 + /* yaffs2 runtime stuff */
11907 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11908 + unsigned oldestDirtySequence;
11909 +
11910 +} yaffs_CheckpointDevice;
11911 +
11912 +
11913 +typedef struct {
11914 + int structType;
11915 + __u32 magic;
11916 + __u32 version;
11917 + __u32 head;
11918 +} yaffs_CheckpointValidity;
11919 +
11920 +/* Function to manipulate block info */
11921 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
11922 +{
11923 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
11924 + T(YAFFS_TRACE_ERROR,
11925 + (TSTR
11926 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
11927 + blk));
11928 + YBUG();
11929 + }
11930 + return &dev->blockInfo[blk - dev->internalStartBlock];
11931 +}
11932 +
11933 +/*----------------------- YAFFS Functions -----------------------*/
11934 +
11935 +int yaffs_GutsInitialise(yaffs_Device * dev);
11936 +void yaffs_Deinitialise(yaffs_Device * dev);
11937 +
11938 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
11939 +
11940 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
11941 + yaffs_Object * newDir, const YCHAR * newName);
11942 +
11943 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
11944 +int yaffs_DeleteFile(yaffs_Object * obj);
11945 +
11946 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
11947 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
11948 +int yaffs_GetObjectInode(yaffs_Object * obj);
11949 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
11950 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
11951 +
11952 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
11953 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
11954 +
11955 +/* File operations */
11956 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
11957 + int nBytes);
11958 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
11959 + int nBytes, int writeThrough);
11960 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
11961 +
11962 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
11963 + __u32 mode, __u32 uid, __u32 gid);
11964 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
11965 +
11966 +/* Flushing and checkpointing */
11967 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
11968 +
11969 +int yaffs_CheckpointSave(yaffs_Device *dev);
11970 +int yaffs_CheckpointRestore(yaffs_Device *dev);
11971 +
11972 +/* Directory operations */
11973 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
11974 + __u32 mode, __u32 uid, __u32 gid);
11975 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
11976 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
11977 + int (*fn) (yaffs_Object *));
11978 +
11979 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
11980 +
11981 +/* Link operations */
11982 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
11983 + yaffs_Object * equivalentObject);
11984 +
11985 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
11986 +
11987 +/* Symlink operations */
11988 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
11989 + __u32 mode, __u32 uid, __u32 gid,
11990 + const YCHAR * alias);
11991 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
11992 +
11993 +/* Special inodes (fifos, sockets and devices) */
11994 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
11995 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
11996 +
11997 +/* Special directories */
11998 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
11999 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
12000 +
12001 +#ifdef CONFIG_YAFFS_WINCE
12002 +/* CONFIG_YAFFS_WINCE special stuff */
12003 +void yfsd_WinFileTimeNow(__u32 target[2]);
12004 +#endif
12005 +
12006 +#ifdef __KERNEL__
12007 +
12008 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
12009 +#endif
12010 +
12011 +/* Debug dump */
12012 +int yaffs_DumpObject(yaffs_Object * obj);
12013 +
12014 +void yaffs_GutsTest(yaffs_Device * dev);
12015 +
12016 +/* A few useful functions */
12017 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12018 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
12019 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
12020 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12021 +
12022 +#endif
12023 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c
12024 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
12025 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c 2007-05-30 13:17:17.000000000 +0200
12026 @@ -0,0 +1,241 @@
12027 +/*
12028 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12029 + *
12030 + * Copyright (C) 2002-2007 Aleph One Ltd.
12031 + * for Toby Churchill Ltd and Brightstar Engineering
12032 + *
12033 + * Created by Charles Manning <charles@aleph1.co.uk>
12034 + *
12035 + * This program is free software; you can redistribute it and/or modify
12036 + * it under the terms of the GNU General Public License version 2 as
12037 + * published by the Free Software Foundation.
12038 + */
12039 +
12040 +const char *yaffs_mtdif_c_version =
12041 + "$Id: yaffs_mtdif.c,v 1.19 2007-02-14 01:09:06 wookey Exp $";
12042 +
12043 +#include "yportenv.h"
12044 +
12045 +
12046 +#include "yaffs_mtdif.h"
12047 +
12048 +#include "linux/mtd/mtd.h"
12049 +#include "linux/types.h"
12050 +#include "linux/time.h"
12051 +#include "linux/mtd/nand.h"
12052 +
12053 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
12054 +static struct nand_oobinfo yaffs_oobinfo = {
12055 + .useecc = 1,
12056 + .eccbytes = 6,
12057 + .eccpos = {8, 9, 10, 13, 14, 15}
12058 +};
12059 +
12060 +static struct nand_oobinfo yaffs_noeccinfo = {
12061 + .useecc = 0,
12062 +};
12063 +#endif
12064 +
12065 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12066 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
12067 +{
12068 + oob[0] = spare->tagByte0;
12069 + oob[1] = spare->tagByte1;
12070 + oob[2] = spare->tagByte2;
12071 + oob[3] = spare->tagByte3;
12072 + oob[4] = spare->tagByte4;
12073 + oob[5] = spare->tagByte5 & 0x3f;
12074 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
12075 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
12076 + oob[6] = spare->tagByte6;
12077 + oob[7] = spare->tagByte7;
12078 +}
12079 +
12080 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
12081 +{
12082 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
12083 + spare->tagByte0 = oob[0];
12084 + spare->tagByte1 = oob[1];
12085 + spare->tagByte2 = oob[2];
12086 + spare->tagByte3 = oob[3];
12087 + spare->tagByte4 = oob[4];
12088 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
12089 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
12090 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
12091 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
12092 + spare->tagByte6 = oob[6];
12093 + spare->tagByte7 = oob[7];
12094 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
12095 +
12096 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
12097 +}
12098 +#endif
12099 +
12100 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12101 + const __u8 * data, const yaffs_Spare * spare)
12102 +{
12103 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12104 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12105 + struct mtd_oob_ops ops;
12106 +#endif
12107 + size_t dummy;
12108 + int retval = 0;
12109 +
12110 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12111 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12112 + __u8 spareAsBytes[8]; /* OOB */
12113 +
12114 + if (data && !spare)
12115 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
12116 + &dummy, data);
12117 + else if (spare) {
12118 + if (dev->useNANDECC) {
12119 + translate_spare2oob(spare, spareAsBytes);
12120 + ops.mode = MTD_OOB_AUTO;
12121 + ops.ooblen = 8; /* temp hack */
12122 + } else {
12123 + ops.mode = MTD_OOB_RAW;
12124 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12125 + }
12126 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12127 + ops.datbuf = (u8 *)data;
12128 + ops.ooboffs = 0;
12129 + ops.oobbuf = spareAsBytes;
12130 + retval = mtd->write_oob(mtd, addr, &ops);
12131 + }
12132 +#else
12133 + __u8 *spareAsBytes = (__u8 *) spare;
12134 +
12135 + if (data && spare) {
12136 + if (dev->useNANDECC)
12137 + retval =
12138 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12139 + &dummy, data, spareAsBytes,
12140 + &yaffs_oobinfo);
12141 + else
12142 + retval =
12143 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12144 + &dummy, data, spareAsBytes,
12145 + &yaffs_noeccinfo);
12146 + } else {
12147 + if (data)
12148 + retval =
12149 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12150 + data);
12151 + if (spare)
12152 + retval =
12153 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12154 + &dummy, spareAsBytes);
12155 + }
12156 +#endif
12157 +
12158 + if (retval == 0)
12159 + return YAFFS_OK;
12160 + else
12161 + return YAFFS_FAIL;
12162 +}
12163 +
12164 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12165 + yaffs_Spare * spare)
12166 +{
12167 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12168 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12169 + struct mtd_oob_ops ops;
12170 +#endif
12171 + size_t dummy;
12172 + int retval = 0;
12173 +
12174 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12175 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12176 + __u8 spareAsBytes[8]; /* OOB */
12177 +
12178 + if (data && !spare)
12179 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
12180 + &dummy, data);
12181 + else if (spare) {
12182 + if (dev->useNANDECC) {
12183 + ops.mode = MTD_OOB_AUTO;
12184 + ops.ooblen = 8; /* temp hack */
12185 + } else {
12186 + ops.mode = MTD_OOB_RAW;
12187 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12188 + }
12189 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12190 + ops.datbuf = data;
12191 + ops.ooboffs = 0;
12192 + ops.oobbuf = spareAsBytes;
12193 + retval = mtd->read_oob(mtd, addr, &ops);
12194 + if (dev->useNANDECC)
12195 + translate_oob2spare(spare, spareAsBytes);
12196 + }
12197 +#else
12198 + __u8 *spareAsBytes = (__u8 *) spare;
12199 +
12200 + if (data && spare) {
12201 + if (dev->useNANDECC) {
12202 + /* Careful, this call adds 2 ints */
12203 + /* to the end of the spare data. Calling function */
12204 + /* should allocate enough memory for spare, */
12205 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
12206 + retval =
12207 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12208 + &dummy, data, spareAsBytes,
12209 + &yaffs_oobinfo);
12210 + } else {
12211 + retval =
12212 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12213 + &dummy, data, spareAsBytes,
12214 + &yaffs_noeccinfo);
12215 + }
12216 + } else {
12217 + if (data)
12218 + retval =
12219 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12220 + data);
12221 + if (spare)
12222 + retval =
12223 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12224 + &dummy, spareAsBytes);
12225 + }
12226 +#endif
12227 +
12228 + if (retval == 0)
12229 + return YAFFS_OK;
12230 + else
12231 + return YAFFS_FAIL;
12232 +}
12233 +
12234 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
12235 +{
12236 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12237 + __u32 addr =
12238 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
12239 + * dev->nChunksPerBlock;
12240 + struct erase_info ei;
12241 + int retval = 0;
12242 +
12243 + ei.mtd = mtd;
12244 + ei.addr = addr;
12245 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
12246 + ei.time = 1000;
12247 + ei.retries = 2;
12248 + ei.callback = NULL;
12249 + ei.priv = (u_long) dev;
12250 +
12251 + /* Todo finish off the ei if required */
12252 +
12253 + sema_init(&dev->sem, 0);
12254 +
12255 + retval = mtd->erase(mtd, &ei);
12256 +
12257 + if (retval == 0)
12258 + return YAFFS_OK;
12259 + else
12260 + return YAFFS_FAIL;
12261 +}
12262 +
12263 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
12264 +{
12265 + return YAFFS_OK;
12266 +}
12267 +
12268 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h
12269 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
12270 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h 2007-05-30 13:17:17.000000000 +0200
12271 @@ -0,0 +1,27 @@
12272 +/*
12273 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12274 + *
12275 + * Copyright (C) 2002-2007 Aleph One Ltd.
12276 + * for Toby Churchill Ltd and Brightstar Engineering
12277 + *
12278 + * Created by Charles Manning <charles@aleph1.co.uk>
12279 + *
12280 + * This program is free software; you can redistribute it and/or modify
12281 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12282 + * published by the Free Software Foundation.
12283 + *
12284 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12285 + */
12286 +
12287 +#ifndef __YAFFS_MTDIF_H__
12288 +#define __YAFFS_MTDIF_H__
12289 +
12290 +#include "yaffs_guts.h"
12291 +
12292 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12293 + const __u8 * data, const yaffs_Spare * spare);
12294 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12295 + yaffs_Spare * spare);
12296 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
12297 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
12298 +#endif
12299 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c
12300 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c 1970-01-01 01:00:00.000000000 +0100
12301 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c 2007-05-30 13:17:16.000000000 +0200
12302 @@ -0,0 +1,434 @@
12303 +From ian@brightstareng.com Fri May 18 15:06:49 2007
12304 +From ian@brightstareng.com Fri May 18 15:08:21 2007
12305 +Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
12306 + by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
12307 + (envelope-from <ian@brightstareng.com>)
12308 + id 1Hp380-00011e-T6
12309 + for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
12310 +Received: from localhost (localhost.localdomain [127.0.0.1])
12311 + by zebra.brightstareng.com (Postfix) with ESMTP
12312 + id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
12313 +Received: from zebra.brightstareng.com ([127.0.0.1])
12314 + by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
12315 + id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
12316 +Received: from pippin (unknown [192.168.1.25])
12317 + by zebra.brightstareng.com (Postfix) with ESMTP
12318 + id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
12319 +From: Ian McDonnell <ian@brightstareng.com>
12320 +To: David Goodenough <david.goodenough@linkchoose.co.uk>
12321 +Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
12322 +Date: Fri, 18 May 2007 10:06:49 -0400
12323 +User-Agent: KMail/1.9.1
12324 +References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
12325 +In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
12326 +Cc: Andrea Conti <alyf@alyf.net>,
12327 + Charles Manning <manningc2@actrix.gen.nz>
12328 +MIME-Version: 1.0
12329 +Content-Type: Multipart/Mixed;
12330 + boundary="Boundary-00=_5LbTGmt62YoutxM"
12331 +Message-Id: <200705181006.49860.ian@brightstareng.com>
12332 +X-Virus-Scanned: by amavisd-new at brightstareng.com
12333 +Status: R
12334 +X-Status: NT
12335 +X-KMail-EncryptionState:
12336 +X-KMail-SignatureState:
12337 +X-KMail-MDN-Sent:
12338 +
12339 +--Boundary-00=_5LbTGmt62YoutxM
12340 +Content-Type: text/plain;
12341 + charset="iso-8859-15"
12342 +Content-Transfer-Encoding: 7bit
12343 +Content-Disposition: inline
12344 +
12345 +David, Andrea,
12346 +
12347 +On Friday 18 May 2007 08:34, you wrote:
12348 +> Yea team. With this fix in place (I put it in the wrong place
12349 +> at first) I can now mount and ls the Yaffs partition without
12350 +> an error messages!
12351 +
12352 +Good news!
12353 +
12354 +Attached is a newer yaffs_mtdif1.c with a bandaid to help the
12355 +2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
12356 +See the LINUX_VERSION_CODE conditional in
12357 +nandmtd1_ReadChunkWithTagsFromNAND.
12358 +
12359 +-imcd
12360 +
12361 +--Boundary-00=_5LbTGmt62YoutxM
12362 +Content-Type: text/x-csrc;
12363 + charset="iso-8859-15";
12364 + name="yaffs_mtdif1.c"
12365 +Content-Transfer-Encoding: 7bit
12366 +Content-Disposition: attachment;
12367 + filename="yaffs_mtdif1.c"
12368 +
12369 +/*
12370 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12371 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12372 + *
12373 + * Copyright (C) 2002 Aleph One Ltd.
12374 + * for Toby Churchill Ltd and Brightstar Engineering
12375 + *
12376 + * This program is free software; you can redistribute it and/or modify
12377 + * it under the terms of the GNU General Public License version 2 as
12378 + * published by the Free Software Foundation.
12379 + */
12380 +
12381 +/*
12382 + * This module provides the interface between yaffs_nand.c and the
12383 + * MTD API. This version is used when the MTD interface supports the
12384 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12385 + * and we have small-page NAND device.
12386 + *
12387 + * These functions are invoked via function pointers in yaffs_nand.c.
12388 + * This replaces functionality provided by functions in yaffs_mtdif.c
12389 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12390 + * called in yaffs_mtdif.c when the function pointers are NULL.
12391 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12392 + */
12393 +
12394 +#include "yportenv.h"
12395 +#include "yaffs_guts.h"
12396 +#include "yaffs_packedtags1.h"
12397 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12398 +
12399 +#include "linux/kernel.h"
12400 +#include "linux/version.h"
12401 +#include "linux/types.h"
12402 +#include "linux/mtd/mtd.h"
12403 +
12404 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12405 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12406 +
12407 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12408 +
12409 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12410 +# define YTAG1_SIZE 8
12411 +#else
12412 +# define YTAG1_SIZE 9
12413 +#endif
12414 +
12415 +#if 0
12416 +/* Use the following nand_ecclayout with MTD when using
12417 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12418 + * If you have existing Yaffs images and the byte order differs from this,
12419 + * adjust 'oobfree' to match your existing Yaffs data.
12420 + *
12421 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12422 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12423 + * the 9th byte.
12424 + *
12425 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12426 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12427 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12428 + * byte and B is the small-page bad-block indicator byte.
12429 + */
12430 +static struct nand_ecclayout nand_oob_16 = {
12431 + .eccbytes = 6,
12432 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12433 + .oobavail = 9,
12434 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12435 +};
12436 +#endif
12437 +
12438 +/* Write a chunk (page) of data to NAND.
12439 + *
12440 + * Caller always provides ExtendedTags data which are converted to a more
12441 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12442 + * contents of the tags meta-data; used to valid the tags when read.
12443 + *
12444 + * - Pack ExtendedTags to PackedTags1 form
12445 + * - Compute mini-ECC for PackedTags1
12446 + * - Write data and packed tags to NAND.
12447 + *
12448 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12449 + * a full sequence number (as found in the larger PackedTags2 form) it is
12450 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12451 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12452 + * to be written just once. When Yaffs performs this operation, this
12453 + * function is called with a NULL data pointer -- calling MTD write_oob
12454 + * without data is valid usage (2.6.17).
12455 + *
12456 + * Any underlying MTD error results in YAFFS_FAIL.
12457 + * Returns YAFFS_OK or YAFFS_FAIL.
12458 + */
12459 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12460 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12461 +{
12462 + struct mtd_info * mtd = dev->genericDevice;
12463 + int chunkBytes = dev->nDataBytesPerChunk;
12464 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12465 + struct mtd_oob_ops ops;
12466 + yaffs_PackedTags1 pt1;
12467 + int retval;
12468 +
12469 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12470 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12471 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12472 +
12473 + yaffs_PackTags1(&pt1, etags);
12474 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12475 +
12476 + /* When deleting a chunk, the upper layer provides only skeletal
12477 + * etags, one with chunkDeleted set. However, we need to update the
12478 + * tags, not erase them completely. So we use the NAND write property
12479 + * that only zeroed-bits stick and set tag bytes to all-ones and
12480 + * zero just the (not) deleted bit.
12481 + */
12482 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12483 + if (etags->chunkDeleted) {
12484 + memset(&pt1, 0xff, 8);
12485 + /* clear delete status bit to indicate deleted */
12486 + pt1.deleted = 0;
12487 + }
12488 +#else
12489 + ((__u8 *)&pt1)[8] = 0xff;
12490 + if (etags->chunkDeleted) {
12491 + memset(&pt1, 0xff, 8);
12492 + /* zero pageStatus byte to indicate deleted */
12493 + ((__u8 *)&pt1)[8] = 0;
12494 + }
12495 +#endif
12496 +
12497 + memset(&ops, 0, sizeof(ops));
12498 + ops.mode = MTD_OOB_AUTO;
12499 + ops.len = (data) ? chunkBytes : 0;
12500 + ops.ooblen = YTAG1_SIZE;
12501 + ops.datbuf = (__u8 *)data;
12502 + ops.oobbuf = (__u8 *)&pt1;
12503 +
12504 + retval = mtd->write_oob(mtd, addr, &ops);
12505 + if (retval) {
12506 + yaffs_trace(YAFFS_TRACE_MTD,
12507 + "write_oob failed, chunk %d, mtd error %d\n",
12508 + chunkInNAND, retval);
12509 + }
12510 + return retval ? YAFFS_FAIL : YAFFS_OK;
12511 +}
12512 +
12513 +/* Return with empty ExtendedTags but add eccResult.
12514 + */
12515 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12516 +{
12517 + if (etags) {
12518 + memset(etags, 0, sizeof(*etags));
12519 + etags->eccResult = eccResult;
12520 + }
12521 + return retval;
12522 +}
12523 +
12524 +/* Read a chunk (page) from NAND.
12525 + *
12526 + * Caller expects ExtendedTags data to be usable even on error; that is,
12527 + * all members except eccResult and blockBad are zeroed.
12528 + *
12529 + * - Check ECC results for data (if applicable)
12530 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12531 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12532 + * - Convert PackedTags1 to ExtendedTags
12533 + * - Update eccResult and blockBad members to refect state.
12534 + *
12535 + * Returns YAFFS_OK or YAFFS_FAIL.
12536 + */
12537 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12538 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12539 +{
12540 + struct mtd_info * mtd = dev->genericDevice;
12541 + int chunkBytes = dev->nDataBytesPerChunk;
12542 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12543 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12544 + struct mtd_oob_ops ops;
12545 + yaffs_PackedTags1 pt1;
12546 + int retval;
12547 + int deleted;
12548 +
12549 + memset(&ops, 0, sizeof(ops));
12550 + ops.mode = MTD_OOB_AUTO;
12551 + ops.len = (data) ? chunkBytes : 0;
12552 + ops.ooblen = YTAG1_SIZE;
12553 + ops.datbuf = data;
12554 + ops.oobbuf = (__u8 *)&pt1;
12555 +
12556 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12557 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12558 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12559 + */
12560 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12561 +#endif
12562 + /* Read page and oob using MTD.
12563 + * Check status and determine ECC result.
12564 + */
12565 + retval = mtd->read_oob(mtd, addr, &ops);
12566 + if (retval) {
12567 + yaffs_trace(YAFFS_TRACE_MTD,
12568 + "read_oob failed, chunk %d, mtd error %d\n",
12569 + chunkInNAND, retval);
12570 + }
12571 +
12572 + switch (retval) {
12573 + case 0:
12574 + /* no error */
12575 + break;
12576 +
12577 + case -EUCLEAN:
12578 + /* MTD's ECC fixed the data */
12579 + eccres = YAFFS_ECC_RESULT_FIXED;
12580 + dev->eccFixed++;
12581 + break;
12582 +
12583 + case -EBADMSG:
12584 + /* MTD's ECC could not fix the data */
12585 + dev->eccUnfixed++;
12586 + /* fall into... */
12587 + default:
12588 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12589 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12590 + return YAFFS_FAIL;
12591 + }
12592 +
12593 + /* Check for a blank/erased chunk.
12594 + */
12595 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12596 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12597 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12598 + }
12599 +
12600 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12601 + /* Read deleted status (bit) then return it to it's non-deleted
12602 + * state before performing tags mini-ECC check. pt1.deleted is
12603 + * inverted.
12604 + */
12605 + deleted = !pt1.deleted;
12606 + pt1.deleted = 1;
12607 +#else
12608 + (void) deleted; /* not used */
12609 +#endif
12610 +
12611 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12612 + */
12613 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12614 + switch (retval) {
12615 + case 0:
12616 + /* no tags error, use MTD result */
12617 + break;
12618 + case 1:
12619 + /* recovered tags-ECC error */
12620 + dev->tagsEccFixed++;
12621 + eccres = YAFFS_ECC_RESULT_FIXED;
12622 + break;
12623 + default:
12624 + /* unrecovered tags-ECC error */
12625 + dev->tagsEccUnfixed++;
12626 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12627 + }
12628 +
12629 + /* Unpack the tags to extended form and set ECC result.
12630 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12631 + */
12632 + pt1.shouldBeFF = 0xFFFFFFFF;
12633 + yaffs_UnpackTags1(etags, &pt1);
12634 + etags->eccResult = eccres;
12635 +
12636 + /* Set deleted state.
12637 + */
12638 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12639 + etags->chunkDeleted = deleted;
12640 +#else
12641 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12642 +#endif
12643 + return YAFFS_OK;
12644 +}
12645 +
12646 +/* Mark a block bad.
12647 + *
12648 + * This is a persistant state.
12649 + * Use of this function should be rare.
12650 + *
12651 + * Returns YAFFS_OK or YAFFS_FAIL.
12652 + */
12653 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12654 +{
12655 + struct mtd_info * mtd = dev->genericDevice;
12656 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12657 + int retval;
12658 +
12659 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
12660 +
12661 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12662 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12663 +}
12664 +
12665 +/* Check any MTD prerequists.
12666 + *
12667 + * Returns YAFFS_OK or YAFFS_FAIL.
12668 + */
12669 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
12670 +{
12671 + /* 2.6.18 has mtd->ecclayout->oobavail */
12672 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12673 + int oobavail = mtd->ecclayout->oobavail;
12674 +
12675 + if (oobavail < YTAG1_SIZE) {
12676 + yaffs_trace(YAFFS_TRACE_ERROR,
12677 + "mtd device has only %d bytes for tags, need %d",
12678 + oobavail, YTAG1_SIZE);
12679 + return YAFFS_FAIL;
12680 + }
12681 + return YAFFS_OK;
12682 +}
12683 +
12684 +/* Query for the current state of a specific block.
12685 + *
12686 + * Examine the tags of the first chunk of the block and return the state:
12687 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12688 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12689 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12690 + *
12691 + * Always returns YAFFS_OK.
12692 + */
12693 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12694 + yaffs_BlockState * pState, int *pSequenceNumber)
12695 +{
12696 + struct mtd_info * mtd = dev->genericDevice;
12697 + int chunkNo = blockNo * dev->nChunksPerBlock;
12698 + yaffs_ExtendedTags etags;
12699 + int state = YAFFS_BLOCK_STATE_DEAD;
12700 + int seqnum = 0;
12701 + int retval;
12702 +
12703 + /* We don't yet have a good place to test for MTD config prerequists.
12704 + * Do it here as we are called during the initial scan.
12705 + */
12706 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
12707 + return YAFFS_FAIL;
12708 + }
12709 +
12710 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12711 + if (etags.blockBad) {
12712 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12713 + "block %d is marked bad", blockNo);
12714 + state = YAFFS_BLOCK_STATE_DEAD;
12715 + }
12716 + else if (etags.chunkUsed) {
12717 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12718 + seqnum = etags.sequenceNumber;
12719 + }
12720 + else {
12721 + state = YAFFS_BLOCK_STATE_EMPTY;
12722 + }
12723 +
12724 + *pState = state;
12725 + *pSequenceNumber = seqnum;
12726 +
12727 + /* query always succeeds */
12728 + return YAFFS_OK;
12729 +}
12730 +
12731 +#endif /*KERNEL_VERSION*/
12732 +
12733 +--Boundary-00=_5LbTGmt62YoutxM--
12734 +
12735 +
12736 +
12737 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c
12738 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
12739 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c 2007-05-30 13:17:16.000000000 +0200
12740 @@ -0,0 +1,339 @@
12741 +/*
12742 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12743 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12744 + *
12745 + * Copyright (C) 2002 Aleph One Ltd.
12746 + * for Toby Churchill Ltd and Brightstar Engineering
12747 + *
12748 + * This program is free software; you can redistribute it and/or modify
12749 + * it under the terms of the GNU General Public License version 2 as
12750 + * published by the Free Software Foundation.
12751 + */
12752 +
12753 +/*
12754 + * This module provides the interface between yaffs_nand.c and the
12755 + * MTD API. This version is used when the MTD interface supports the
12756 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12757 + * and we have small-page NAND device.
12758 + *
12759 + * These functions are invoked via function pointers in yaffs_nand.c.
12760 + * This replaces functionality provided by functions in yaffs_mtdif.c
12761 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12762 + * called in yaffs_mtdif.c when the function pointers are NULL.
12763 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12764 + */
12765 +
12766 +#include "yportenv.h"
12767 +#include "yaffs_guts.h"
12768 +#include "yaffs_packedtags1.h"
12769 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12770 +
12771 +#include "linux/kernel.h"
12772 +#include "linux/version.h"
12773 +#include "linux/types.h"
12774 +#include "linux/mtd/mtd.h"
12775 +
12776 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12777 +/* should really be >= .17, but elsewhere > .17 is used, so be consistent */
12778 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12779 +
12780 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12781 +
12782 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12783 +# define YTAG1_SIZE 8
12784 +#else
12785 +# define YTAG1_SIZE 9
12786 +#endif
12787 +
12788 +#if 0
12789 +/* Use the following nand_ecclayout with MTD when using
12790 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12791 + * If you have existing Yaffs images and the byte order differs from this,
12792 + * adjust 'oobfree' to match your existing Yaffs data.
12793 + *
12794 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12795 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12796 + * the 9th byte.
12797 + *
12798 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12799 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12800 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12801 + * byte and B is the small-page bad-block indicator byte.
12802 + */
12803 +static struct nand_ecclayout nand_oob_16 = {
12804 + .eccbytes = 6,
12805 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12806 + .oobavail = 9,
12807 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12808 +};
12809 +#endif
12810 +
12811 +/* Write a chunk (page) of data to NAND.
12812 + *
12813 + * Caller always provides ExtendedTags data which are converted to a more
12814 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12815 + * contents of the tags meta-data; used to valid the tags when read.
12816 + *
12817 + * - Pack ExtendedTags to PackedTags1 form
12818 + * - Compute mini-ECC for PackedTags1
12819 + * - Write data and packed tags to NAND.
12820 + *
12821 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12822 + * a full sequence number (as found in the larger PackedTags2 form) it is
12823 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12824 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12825 + * to be written just once. When Yaffs performs this operation, this
12826 + * function is called with a NULL data pointer -- calling MTD write_oob
12827 + * without data is valid usage (2.6.17).
12828 + *
12829 + * Any underlying MTD error results in YAFFS_FAIL.
12830 + * Returns YAFFS_OK or YAFFS_FAIL.
12831 + */
12832 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12833 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12834 +{
12835 + struct mtd_info * mtd = dev->genericDevice;
12836 + int chunkBytes = dev->nDataBytesPerChunk;
12837 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12838 + struct mtd_oob_ops ops;
12839 + yaffs_PackedTags1 pt1;
12840 + int retval;
12841 +
12842 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12843 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12844 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12845 +
12846 + yaffs_PackTags1(&pt1, etags);
12847 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12848 +
12849 + /* When deleting a chunk, the upper layer provides only skeletal
12850 + * etags, one with chunkDeleted set. However, we need to update the
12851 + * tags, not erase them completely. So we use the NAND write property
12852 + * that only zeroed-bits stick and set tag bytes to all-ones and
12853 + * zero just the (not) deleted bit.
12854 + */
12855 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12856 + if (etags->chunkDeleted) {
12857 + memset(&pt1, 0xff, 8);
12858 + /* clear delete status bit to indicate deleted */
12859 + pt1.deleted = 0;
12860 + }
12861 +#else
12862 + ((__u8 *)&pt1)[8] = 0xff;
12863 + if (etags->chunkDeleted) {
12864 + memset(&pt1, 0xff, 8);
12865 + /* zero pageStatus byte to indicate deleted */
12866 + ((__u8 *)&pt1)[8] = 0;
12867 + }
12868 +#endif
12869 +
12870 + memset(&ops, 0, sizeof(ops));
12871 + ops.mode = MTD_OOB_AUTO;
12872 + ops.len = (data) ? chunkBytes : 0;
12873 + ops.ooblen = YTAG1_SIZE;
12874 + ops.datbuf = (__u8 *)data;
12875 + ops.oobbuf = (__u8 *)&pt1;
12876 +
12877 + retval = mtd->write_oob(mtd, addr, &ops);
12878 + if (retval) {
12879 + yaffs_trace(YAFFS_TRACE_MTD,
12880 + "write_oob failed, chunk %d, mtd error %d\n",
12881 + chunkInNAND, retval);
12882 + }
12883 + return retval ? YAFFS_FAIL : YAFFS_OK;
12884 +}
12885 +
12886 +/* Return with empty ExtendedTags but add eccResult.
12887 + */
12888 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12889 +{
12890 + if (etags) {
12891 + memset(etags, 0, sizeof(*etags));
12892 + etags->eccResult = eccResult;
12893 + }
12894 + return retval;
12895 +}
12896 +
12897 +/* Read a chunk (page) from NAND.
12898 + *
12899 + * Caller expects ExtendedTags data to be usable even on error; that is,
12900 + * all members except eccResult and blockBad are zeroed.
12901 + *
12902 + * - Check ECC results for data (if applicable)
12903 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12904 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12905 + * - Convert PackedTags1 to ExtendedTags
12906 + * - Update eccResult and blockBad members to refect state.
12907 + *
12908 + * Returns YAFFS_OK or YAFFS_FAIL.
12909 + */
12910 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12911 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12912 +{
12913 + struct mtd_info * mtd = dev->genericDevice;
12914 + int chunkBytes = dev->nDataBytesPerChunk;
12915 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12916 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12917 + struct mtd_oob_ops ops;
12918 + yaffs_PackedTags1 pt1;
12919 + int retval;
12920 + int deleted;
12921 +
12922 + memset(&ops, 0, sizeof(ops));
12923 + ops.mode = MTD_OOB_AUTO;
12924 + ops.len = (data) ? chunkBytes : 0;
12925 + ops.ooblen = YTAG1_SIZE;
12926 + ops.datbuf = data;
12927 + ops.oobbuf = (__u8 *)&pt1;
12928 +
12929 + /* Read page and oob using MTD.
12930 + * Check status and determine ECC result.
12931 + */
12932 + retval = mtd->read_oob(mtd, addr, &ops);
12933 + if (retval) {
12934 + yaffs_trace(YAFFS_TRACE_MTD,
12935 + "read_oob failed, chunk %d, mtd error %d\n",
12936 + chunkInNAND, retval);
12937 + }
12938 +
12939 + switch (retval) {
12940 + case 0:
12941 + /* no error */
12942 + break;
12943 +
12944 + case -EUCLEAN:
12945 + /* MTD's ECC fixed the data */
12946 + eccres = YAFFS_ECC_RESULT_FIXED;
12947 + dev->eccFixed++;
12948 + break;
12949 +
12950 + case -EBADMSG:
12951 + /* MTD's ECC could not fix the data */
12952 + dev->eccUnfixed++;
12953 + /* fall into... */
12954 + default:
12955 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12956 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12957 + return YAFFS_FAIL;
12958 + }
12959 +
12960 + /* Check for a blank/erased chunk.
12961 + */
12962 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12963 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12964 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12965 + }
12966 +
12967 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12968 + /* Read deleted status (bit) then return it to it's non-deleted
12969 + * state before performing tags mini-ECC check. pt1.deleted is
12970 + * inverted.
12971 + */
12972 + deleted = !pt1.deleted;
12973 + pt1.deleted = 1;
12974 +#else
12975 + (void) deleted; /* not used */
12976 +#endif
12977 +
12978 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12979 + */
12980 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12981 + switch (retval) {
12982 + case 0:
12983 + /* no tags error, use MTD result */
12984 + break;
12985 + case 1:
12986 + /* recovered tags-ECC error */
12987 + dev->tagsEccFixed++;
12988 + eccres = YAFFS_ECC_RESULT_FIXED;
12989 + break;
12990 + default:
12991 + /* unrecovered tags-ECC error */
12992 + dev->tagsEccUnfixed++;
12993 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12994 + }
12995 +
12996 + /* Unpack the tags to extended form and set ECC result.
12997 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12998 + */
12999 + pt1.shouldBeFF = 0xFFFFFFFF;
13000 + yaffs_UnpackTags1(etags, &pt1);
13001 + etags->eccResult = eccres;
13002 +
13003 + /* Set deleted state.
13004 + */
13005 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
13006 + etags->chunkDeleted = deleted;
13007 +#else
13008 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
13009 +#endif
13010 + return YAFFS_OK;
13011 +}
13012 +
13013 +/* Mark a block bad.
13014 + *
13015 + * This is a persistant state.
13016 + * Use of this function should be rare.
13017 + *
13018 + * Returns YAFFS_OK or YAFFS_FAIL.
13019 + */
13020 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13021 +{
13022 + struct mtd_info * mtd = dev->genericDevice;
13023 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
13024 + int retval;
13025 +
13026 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
13027 +
13028 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
13029 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
13030 +}
13031 +
13032 +/* Query for the current state of a specific block.
13033 + *
13034 + * Examine the tags of the first chunk of the block and return the state:
13035 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
13036 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
13037 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
13038 + *
13039 + * Always returns YAFFS_OK.
13040 + */
13041 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13042 + yaffs_BlockState * pState, int *pSequenceNumber)
13043 +{
13044 + struct mtd_info * mtd = dev->genericDevice;
13045 + int chunkNo = blockNo * dev->nChunksPerBlock;
13046 + yaffs_ExtendedTags etags;
13047 + int state = YAFFS_BLOCK_STATE_DEAD;
13048 + int seqnum = 0;
13049 + int retval;
13050 +#if 0
13051 + if (mtd->oobavail < YTAG1_SIZE) {
13052 + yaffs_trace(YAFFS_TRACE_ERROR,
13053 + "mtd device has only %d bytes for tags, need %d",
13054 + mtd->oobavail, YTAG1_SIZE);
13055 + return YAFFS_FAIL;
13056 + }
13057 +#endif
13058 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
13059 + if (etags.blockBad) {
13060 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
13061 + "block %d is marked bad", blockNo);
13062 + state = YAFFS_BLOCK_STATE_DEAD;
13063 + }
13064 + else if (etags.chunkUsed) {
13065 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13066 + seqnum = etags.sequenceNumber;
13067 + }
13068 + else {
13069 + state = YAFFS_BLOCK_STATE_EMPTY;
13070 + }
13071 +
13072 + *pState = state;
13073 + *pSequenceNumber = seqnum;
13074 +
13075 + /* query always succeeds */
13076 + return YAFFS_OK;
13077 +}
13078 +
13079 +#endif /*KERNEL_VERSION*/
13080 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h
13081 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
13082 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h 2007-05-30 13:17:17.000000000 +0200
13083 @@ -0,0 +1,28 @@
13084 +/*
13085 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
13086 + *
13087 + * Copyright (C) 2002-2007 Aleph One Ltd.
13088 + * for Toby Churchill Ltd and Brightstar Engineering
13089 + *
13090 + * This program is free software; you can redistribute it and/or modify
13091 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13092 + * published by the Free Software Foundation.
13093 + *
13094 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13095 + */
13096 +
13097 +#ifndef __YAFFS_MTDIF1_H__
13098 +#define __YAFFS_MTDIF1_H__
13099 +
13100 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13101 + const __u8 * data, const yaffs_ExtendedTags * tags);
13102 +
13103 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13104 + __u8 * data, yaffs_ExtendedTags * tags);
13105 +
13106 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13107 +
13108 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13109 + yaffs_BlockState * state, int *sequenceNumber);
13110 +
13111 +#endif
13112 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c
13113 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
13114 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c 2007-05-30 13:17:17.000000000 +0200
13115 @@ -0,0 +1,232 @@
13116 +/*
13117 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13118 + *
13119 + * Copyright (C) 2002-2007 Aleph One Ltd.
13120 + * for Toby Churchill Ltd and Brightstar Engineering
13121 + *
13122 + * Created by Charles Manning <charles@aleph1.co.uk>
13123 + *
13124 + * This program is free software; you can redistribute it and/or modify
13125 + * it under the terms of the GNU General Public License version 2 as
13126 + * published by the Free Software Foundation.
13127 + */
13128 +
13129 +/* mtd interface for YAFFS2 */
13130 +
13131 +const char *yaffs_mtdif2_c_version =
13132 + "$Id: yaffs_mtdif2.c,v 1.17 2007-02-14 01:09:06 wookey Exp $";
13133 +
13134 +#include "yportenv.h"
13135 +
13136 +
13137 +#include "yaffs_mtdif2.h"
13138 +
13139 +#include "linux/mtd/mtd.h"
13140 +#include "linux/types.h"
13141 +#include "linux/time.h"
13142 +
13143 +#include "yaffs_packedtags2.h"
13144 +
13145 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13146 + const __u8 * data,
13147 + const yaffs_ExtendedTags * tags)
13148 +{
13149 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13150 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13151 + struct mtd_oob_ops ops;
13152 +#else
13153 + size_t dummy;
13154 +#endif
13155 + int retval = 0;
13156 +
13157 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13158 +
13159 + yaffs_PackedTags2 pt;
13160 +
13161 + T(YAFFS_TRACE_MTD,
13162 + (TSTR
13163 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
13164 + TENDSTR), chunkInNAND, data, tags));
13165 +
13166 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13167 + if (tags)
13168 + yaffs_PackTags2(&pt, tags);
13169 + else
13170 + BUG(); /* both tags and data should always be present */
13171 +
13172 + if (data) {
13173 + ops.mode = MTD_OOB_AUTO;
13174 + ops.ooblen = sizeof(pt);
13175 + ops.len = dev->nDataBytesPerChunk;
13176 + ops.ooboffs = 0;
13177 + ops.datbuf = (__u8 *)data;
13178 + ops.oobbuf = (void *)&pt;
13179 + retval = mtd->write_oob(mtd, addr, &ops);
13180 + } else
13181 + BUG(); /* both tags and data should always be present */
13182 +#else
13183 + if (tags) {
13184 + yaffs_PackTags2(&pt, tags);
13185 + }
13186 +
13187 + if (data && tags) {
13188 + if (dev->useNANDECC)
13189 + retval =
13190 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13191 + &dummy, data, (__u8 *) & pt, NULL);
13192 + else
13193 + retval =
13194 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13195 + &dummy, data, (__u8 *) & pt, NULL);
13196 + } else {
13197 + if (data)
13198 + retval =
13199 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13200 + data);
13201 + if (tags)
13202 + retval =
13203 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
13204 + (__u8 *) & pt);
13205 +
13206 + }
13207 +#endif
13208 +
13209 + if (retval == 0)
13210 + return YAFFS_OK;
13211 + else
13212 + return YAFFS_FAIL;
13213 +}
13214 +
13215 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13216 + __u8 * data, yaffs_ExtendedTags * tags)
13217 +{
13218 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13219 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13220 + struct mtd_oob_ops ops;
13221 +#endif
13222 + size_t dummy;
13223 + int retval = 0;
13224 +
13225 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13226 +
13227 + yaffs_PackedTags2 pt;
13228 +
13229 + T(YAFFS_TRACE_MTD,
13230 + (TSTR
13231 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
13232 + TENDSTR), chunkInNAND, data, tags));
13233 +
13234 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13235 + if (data && !tags)
13236 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13237 + &dummy, data);
13238 + else if (tags) {
13239 + ops.mode = MTD_OOB_AUTO;
13240 + ops.ooblen = sizeof(pt);
13241 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13242 + ops.ooboffs = 0;
13243 + ops.datbuf = data;
13244 + ops.oobbuf = dev->spareBuffer;
13245 + retval = mtd->read_oob(mtd, addr, &ops);
13246 + }
13247 +#else
13248 + if (data && tags) {
13249 + if (dev->useNANDECC) {
13250 + retval =
13251 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13252 + &dummy, data, dev->spareBuffer,
13253 + NULL);
13254 + } else {
13255 + retval =
13256 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13257 + &dummy, data, dev->spareBuffer,
13258 + NULL);
13259 + }
13260 + } else {
13261 + if (data)
13262 + retval =
13263 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13264 + data);
13265 + if (tags)
13266 + retval =
13267 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13268 + dev->spareBuffer);
13269 + }
13270 +#endif
13271 +
13272 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13273 +
13274 + if (tags)
13275 + yaffs_UnpackTags2(tags, &pt);
13276 +
13277 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13278 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13279 +
13280 + if (retval == 0)
13281 + return YAFFS_OK;
13282 + else
13283 + return YAFFS_FAIL;
13284 +}
13285 +
13286 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13287 +{
13288 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13289 + int retval;
13290 + T(YAFFS_TRACE_MTD,
13291 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13292 +
13293 + retval =
13294 + mtd->block_markbad(mtd,
13295 + blockNo * dev->nChunksPerBlock *
13296 + dev->nDataBytesPerChunk);
13297 +
13298 + if (retval == 0)
13299 + return YAFFS_OK;
13300 + else
13301 + return YAFFS_FAIL;
13302 +
13303 +}
13304 +
13305 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13306 + yaffs_BlockState * state, int *sequenceNumber)
13307 +{
13308 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13309 + int retval;
13310 +
13311 + T(YAFFS_TRACE_MTD,
13312 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13313 + retval =
13314 + mtd->block_isbad(mtd,
13315 + blockNo * dev->nChunksPerBlock *
13316 + dev->nDataBytesPerChunk);
13317 +
13318 + if (retval) {
13319 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13320 +
13321 + *state = YAFFS_BLOCK_STATE_DEAD;
13322 + *sequenceNumber = 0;
13323 + } else {
13324 + yaffs_ExtendedTags t;
13325 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13326 + blockNo *
13327 + dev->nChunksPerBlock, NULL,
13328 + &t);
13329 +
13330 + if (t.chunkUsed) {
13331 + *sequenceNumber = t.sequenceNumber;
13332 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13333 + } else {
13334 + *sequenceNumber = 0;
13335 + *state = YAFFS_BLOCK_STATE_EMPTY;
13336 + }
13337 + }
13338 + T(YAFFS_TRACE_MTD,
13339 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13340 + *state));
13341 +
13342 + if (retval == 0)
13343 + return YAFFS_OK;
13344 + else
13345 + return YAFFS_FAIL;
13346 +}
13347 +
13348 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h
13349 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
13350 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h 2007-05-30 13:17:17.000000000 +0200
13351 @@ -0,0 +1,29 @@
13352 +/*
13353 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13354 + *
13355 + * Copyright (C) 2002-2007 Aleph One Ltd.
13356 + * for Toby Churchill Ltd and Brightstar Engineering
13357 + *
13358 + * Created by Charles Manning <charles@aleph1.co.uk>
13359 + *
13360 + * This program is free software; you can redistribute it and/or modify
13361 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13362 + * published by the Free Software Foundation.
13363 + *
13364 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13365 + */
13366 +
13367 +#ifndef __YAFFS_MTDIF2_H__
13368 +#define __YAFFS_MTDIF2_H__
13369 +
13370 +#include "yaffs_guts.h"
13371 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13372 + const __u8 * data,
13373 + const yaffs_ExtendedTags * tags);
13374 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13375 + __u8 * data, yaffs_ExtendedTags * tags);
13376 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13377 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13378 + yaffs_BlockState * state, int *sequenceNumber);
13379 +
13380 +#endif
13381 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.c linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c
13382 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
13383 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c 2007-05-30 13:17:17.000000000 +0200
13384 @@ -0,0 +1,134 @@
13385 +/*
13386 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13387 + *
13388 + * Copyright (C) 2002-2007 Aleph One Ltd.
13389 + * for Toby Churchill Ltd and Brightstar Engineering
13390 + *
13391 + * Created by Charles Manning <charles@aleph1.co.uk>
13392 + *
13393 + * This program is free software; you can redistribute it and/or modify
13394 + * it under the terms of the GNU General Public License version 2 as
13395 + * published by the Free Software Foundation.
13396 + */
13397 +
13398 +const char *yaffs_nand_c_version =
13399 + "$Id: yaffs_nand.c,v 1.7 2007-02-14 01:09:06 wookey Exp $";
13400 +
13401 +#include "yaffs_nand.h"
13402 +#include "yaffs_tagscompat.h"
13403 +#include "yaffs_tagsvalidity.h"
13404 +
13405 +
13406 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13407 + __u8 * buffer,
13408 + yaffs_ExtendedTags * tags)
13409 +{
13410 + int result;
13411 + yaffs_ExtendedTags localTags;
13412 +
13413 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13414 +
13415 + /* If there are no tags provided, use local tags to get prioritised gc working */
13416 + if(!tags)
13417 + tags = &localTags;
13418 +
13419 + if (dev->readChunkWithTagsFromNAND)
13420 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13421 + tags);
13422 + else
13423 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13424 + realignedChunkInNAND,
13425 + buffer,
13426 + tags);
13427 + if(tags &&
13428 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
13429 +
13430 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13431 + yaffs_HandleChunkError(dev,bi);
13432 + }
13433 +
13434 + return result;
13435 +}
13436 +
13437 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13438 + int chunkInNAND,
13439 + const __u8 * buffer,
13440 + yaffs_ExtendedTags * tags)
13441 +{
13442 + chunkInNAND -= dev->chunkOffset;
13443 +
13444 +
13445 + if (tags) {
13446 + tags->sequenceNumber = dev->sequenceNumber;
13447 + tags->chunkUsed = 1;
13448 + if (!yaffs_ValidateTags(tags)) {
13449 + T(YAFFS_TRACE_ERROR,
13450 + (TSTR("Writing uninitialised tags" TENDSTR)));
13451 + YBUG();
13452 + }
13453 + T(YAFFS_TRACE_WRITE,
13454 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13455 + tags->objectId, tags->chunkId));
13456 + } else {
13457 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13458 + YBUG();
13459 + }
13460 +
13461 + if (dev->writeChunkWithTagsToNAND)
13462 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13463 + tags);
13464 + else
13465 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13466 + chunkInNAND,
13467 + buffer,
13468 + tags);
13469 +}
13470 +
13471 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
13472 +{
13473 + blockNo -= dev->blockOffset;
13474 +
13475 +;
13476 + if (dev->markNANDBlockBad)
13477 + return dev->markNANDBlockBad(dev, blockNo);
13478 + else
13479 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13480 +}
13481 +
13482 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13483 + int blockNo,
13484 + yaffs_BlockState * state,
13485 + unsigned *sequenceNumber)
13486 +{
13487 + blockNo -= dev->blockOffset;
13488 +
13489 + if (dev->queryNANDBlock)
13490 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13491 + else
13492 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13493 + state,
13494 + sequenceNumber);
13495 +}
13496 +
13497 +
13498 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13499 + int blockInNAND)
13500 +{
13501 + int result;
13502 +
13503 + blockInNAND -= dev->blockOffset;
13504 +
13505 +
13506 + dev->nBlockErasures++;
13507 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13508 +
13509 + return result;
13510 +}
13511 +
13512 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13513 +{
13514 + return dev->initialiseNAND(dev);
13515 +}
13516 +
13517 +
13518 +
13519 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h
13520 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
13521 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h 2007-05-30 13:17:17.000000000 +0200
13522 @@ -0,0 +1,44 @@
13523 +/*
13524 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13525 + *
13526 + * Copyright (C) 2002-2007 Aleph One Ltd.
13527 + * for Toby Churchill Ltd and Brightstar Engineering
13528 + *
13529 + * Created by Charles Manning <charles@aleph1.co.uk>
13530 + *
13531 + * This program is free software; you can redistribute it and/or modify
13532 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13533 + * published by the Free Software Foundation.
13534 + *
13535 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13536 + */
13537 +
13538 +#ifndef __YAFFS_NAND_H__
13539 +#define __YAFFS_NAND_H__
13540 +#include "yaffs_guts.h"
13541 +
13542 +
13543 +
13544 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13545 + __u8 * buffer,
13546 + yaffs_ExtendedTags * tags);
13547 +
13548 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13549 + int chunkInNAND,
13550 + const __u8 * buffer,
13551 + yaffs_ExtendedTags * tags);
13552 +
13553 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
13554 +
13555 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13556 + int blockNo,
13557 + yaffs_BlockState * state,
13558 + unsigned *sequenceNumber);
13559 +
13560 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13561 + int blockInNAND);
13562 +
13563 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13564 +
13565 +#endif
13566 +
13567 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h
13568 --- linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
13569 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h 2007-05-30 13:17:17.000000000 +0200
13570 @@ -0,0 +1,39 @@
13571 +/*
13572 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13573 + *
13574 + * Copyright (C) 2002-2007 Aleph One Ltd.
13575 + * for Toby Churchill Ltd and Brightstar Engineering
13576 + *
13577 + * Created by Charles Manning <charles@aleph1.co.uk>
13578 + *
13579 + * This program is free software; you can redistribute it and/or modify
13580 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13581 + * published by the Free Software Foundation.
13582 + *
13583 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13584 + */
13585 +
13586 +/* Interface to emulated NAND functions (2k page size) */
13587 +
13588 +#ifndef __YAFFS_NANDEMUL2K_H__
13589 +#define __YAFFS_NANDEMUL2K_H__
13590 +
13591 +#include "yaffs_guts.h"
13592 +
13593 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13594 + int chunkInNAND, const __u8 * data,
13595 + yaffs_ExtendedTags * tags);
13596 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13597 + int chunkInNAND, __u8 * data,
13598 + yaffs_ExtendedTags * tags);
13599 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13600 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13601 + yaffs_BlockState * state, int *sequenceNumber);
13602 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13603 + int blockInNAND);
13604 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13605 +int nandemul2k_GetBytesPerChunk(void);
13606 +int nandemul2k_GetChunksPerBlock(void);
13607 +int nandemul2k_GetNumberOfBlocks(void);
13608 +
13609 +#endif
13610 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c
13611 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
13612 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c 2007-05-30 13:17:17.000000000 +0200
13613 @@ -0,0 +1,52 @@
13614 +/*
13615 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13616 + *
13617 + * Copyright (C) 2002-2007 Aleph One Ltd.
13618 + * for Toby Churchill Ltd and Brightstar Engineering
13619 + *
13620 + * Created by Charles Manning <charles@aleph1.co.uk>
13621 + *
13622 + * This program is free software; you can redistribute it and/or modify
13623 + * it under the terms of the GNU General Public License version 2 as
13624 + * published by the Free Software Foundation.
13625 + */
13626 +
13627 +#include "yaffs_packedtags1.h"
13628 +#include "yportenv.h"
13629 +
13630 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
13631 +{
13632 + pt->chunkId = t->chunkId;
13633 + pt->serialNumber = t->serialNumber;
13634 + pt->byteCount = t->byteCount;
13635 + pt->objectId = t->objectId;
13636 + pt->ecc = 0;
13637 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13638 + pt->unusedStuff = 0;
13639 + pt->shouldBeFF = 0xFFFFFFFF;
13640 +
13641 +}
13642 +
13643 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
13644 +{
13645 + static const __u8 allFF[] =
13646 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13647 +0xff };
13648 +
13649 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13650 + t->blockBad = 0;
13651 + if (pt->shouldBeFF != 0xFFFFFFFF) {
13652 + t->blockBad = 1;
13653 + }
13654 + t->chunkUsed = 1;
13655 + t->objectId = pt->objectId;
13656 + t->chunkId = pt->chunkId;
13657 + t->byteCount = pt->byteCount;
13658 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13659 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13660 + t->serialNumber = pt->serialNumber;
13661 + } else {
13662 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13663 +
13664 + }
13665 +}
13666 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h
13667 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
13668 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h 2007-05-30 13:17:17.000000000 +0200
13669 @@ -0,0 +1,37 @@
13670 +/*
13671 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13672 + *
13673 + * Copyright (C) 2002-2007 Aleph One Ltd.
13674 + * for Toby Churchill Ltd and Brightstar Engineering
13675 + *
13676 + * Created by Charles Manning <charles@aleph1.co.uk>
13677 + *
13678 + * This program is free software; you can redistribute it and/or modify
13679 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13680 + * published by the Free Software Foundation.
13681 + *
13682 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13683 + */
13684 +
13685 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13686 +
13687 +#ifndef __YAFFS_PACKEDTAGS1_H__
13688 +#define __YAFFS_PACKEDTAGS1_H__
13689 +
13690 +#include "yaffs_guts.h"
13691 +
13692 +typedef struct {
13693 + unsigned chunkId:20;
13694 + unsigned serialNumber:2;
13695 + unsigned byteCount:10;
13696 + unsigned objectId:18;
13697 + unsigned ecc:12;
13698 + unsigned deleted:1;
13699 + unsigned unusedStuff:1;
13700 + unsigned shouldBeFF;
13701 +
13702 +} yaffs_PackedTags1;
13703 +
13704 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
13705 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
13706 +#endif
13707 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c
13708 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
13709 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c 2007-05-30 13:17:17.000000000 +0200
13710 @@ -0,0 +1,182 @@
13711 +/*
13712 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13713 + *
13714 + * Copyright (C) 2002-2007 Aleph One Ltd.
13715 + * for Toby Churchill Ltd and Brightstar Engineering
13716 + *
13717 + * Created by Charles Manning <charles@aleph1.co.uk>
13718 + *
13719 + * This program is free software; you can redistribute it and/or modify
13720 + * it under the terms of the GNU General Public License version 2 as
13721 + * published by the Free Software Foundation.
13722 + */
13723 +
13724 +#include "yaffs_packedtags2.h"
13725 +#include "yportenv.h"
13726 +#include "yaffs_tagsvalidity.h"
13727 +
13728 +/* This code packs a set of extended tags into a binary structure for
13729 + * NAND storage
13730 + */
13731 +
13732 +/* Some of the information is "extra" struff which can be packed in to
13733 + * speed scanning
13734 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13735 + */
13736 +
13737 +/* Extra flags applied to chunkId */
13738 +
13739 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13740 +#define EXTRA_SHRINK_FLAG 0x40000000
13741 +#define EXTRA_SHADOWS_FLAG 0x20000000
13742 +#define EXTRA_SPARE_FLAGS 0x10000000
13743 +
13744 +#define ALL_EXTRA_FLAGS 0xF0000000
13745 +
13746 +/* Also, the top 4 bits of the object Id are set to the object type. */
13747 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13748 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13749 +
13750 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
13751 +{
13752 + T(YAFFS_TRACE_MTD,
13753 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13754 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
13755 + pt->t.sequenceNumber));
13756 +}
13757 +
13758 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
13759 +{
13760 + T(YAFFS_TRACE_MTD,
13761 + (TSTR
13762 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
13763 + "%d del %d ser %d seq %d"
13764 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13765 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13766 + t->sequenceNumber));
13767 +
13768 +}
13769 +
13770 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
13771 +{
13772 + pt->t.chunkId = t->chunkId;
13773 + pt->t.sequenceNumber = t->sequenceNumber;
13774 + pt->t.byteCount = t->byteCount;
13775 + pt->t.objectId = t->objectId;
13776 +
13777 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13778 + /* Store the extra header info instead */
13779 + /* We save the parent object in the chunkId */
13780 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
13781 + | t->extraParentObjectId;
13782 + if (t->extraIsShrinkHeader) {
13783 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
13784 + }
13785 + if (t->extraShadows) {
13786 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
13787 + }
13788 +
13789 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13790 + pt->t.objectId |=
13791 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13792 +
13793 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13794 + pt->t.byteCount = t->extraEquivalentObjectId;
13795 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
13796 + pt->t.byteCount = t->extraFileLength;
13797 + } else {
13798 + pt->t.byteCount = 0;
13799 + }
13800 + }
13801 +
13802 + yaffs_DumpPackedTags2(pt);
13803 + yaffs_DumpTags2(t);
13804 +
13805 +#ifndef YAFFS_IGNORE_TAGS_ECC
13806 + {
13807 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13808 + sizeof(yaffs_PackedTags2TagsPart),
13809 + &pt->ecc);
13810 + }
13811 +#endif
13812 +}
13813 +
13814 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
13815 +{
13816 +
13817 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13818 +
13819 + yaffs_InitialiseTags(t);
13820 +
13821 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13822 + /* Page is in use */
13823 +#ifdef YAFFS_IGNORE_TAGS_ECC
13824 + {
13825 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13826 + }
13827 +#else
13828 + {
13829 + yaffs_ECCOther ecc;
13830 + int result;
13831 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13832 + sizeof
13833 + (yaffs_PackedTags2TagsPart),
13834 + &ecc);
13835 + result =
13836 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13837 + sizeof
13838 + (yaffs_PackedTags2TagsPart),
13839 + &pt->ecc, &ecc);
13840 + switch(result){
13841 + case 0:
13842 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13843 + break;
13844 + case 1:
13845 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
13846 + break;
13847 + case -1:
13848 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13849 + break;
13850 + default:
13851 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13852 + }
13853 + }
13854 +#endif
13855 + t->blockBad = 0;
13856 + t->chunkUsed = 1;
13857 + t->objectId = pt->t.objectId;
13858 + t->chunkId = pt->t.chunkId;
13859 + t->byteCount = pt->t.byteCount;
13860 + t->chunkDeleted = 0;
13861 + t->serialNumber = 0;
13862 + t->sequenceNumber = pt->t.sequenceNumber;
13863 +
13864 + /* Do extra header info stuff */
13865 +
13866 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
13867 + t->chunkId = 0;
13868 + t->byteCount = 0;
13869 +
13870 + t->extraHeaderInfoAvailable = 1;
13871 + t->extraParentObjectId =
13872 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
13873 + t->extraIsShrinkHeader =
13874 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13875 + t->extraShadows =
13876 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13877 + t->extraObjectType =
13878 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13879 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13880 +
13881 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13882 + t->extraEquivalentObjectId = pt->t.byteCount;
13883 + } else {
13884 + t->extraFileLength = pt->t.byteCount;
13885 + }
13886 + }
13887 + }
13888 +
13889 + yaffs_DumpPackedTags2(pt);
13890 + yaffs_DumpTags2(t);
13891 +
13892 +}
13893 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h
13894 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
13895 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h 2007-05-30 13:17:17.000000000 +0200
13896 @@ -0,0 +1,38 @@
13897 +/*
13898 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13899 + *
13900 + * Copyright (C) 2002-2007 Aleph One Ltd.
13901 + * for Toby Churchill Ltd and Brightstar Engineering
13902 + *
13903 + * Created by Charles Manning <charles@aleph1.co.uk>
13904 + *
13905 + * This program is free software; you can redistribute it and/or modify
13906 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13907 + * published by the Free Software Foundation.
13908 + *
13909 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13910 + */
13911 +
13912 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13913 +
13914 +#ifndef __YAFFS_PACKEDTAGS2_H__
13915 +#define __YAFFS_PACKEDTAGS2_H__
13916 +
13917 +#include "yaffs_guts.h"
13918 +#include "yaffs_ecc.h"
13919 +
13920 +typedef struct {
13921 + unsigned sequenceNumber;
13922 + unsigned objectId;
13923 + unsigned chunkId;
13924 + unsigned byteCount;
13925 +} yaffs_PackedTags2TagsPart;
13926 +
13927 +typedef struct {
13928 + yaffs_PackedTags2TagsPart t;
13929 + yaffs_ECCOther ecc;
13930 +} yaffs_PackedTags2;
13931 +
13932 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
13933 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
13934 +#endif
13935 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c
13936 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
13937 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c 2007-05-30 13:17:17.000000000 +0200
13938 @@ -0,0 +1,160 @@
13939 +/*
13940 + * Copyright (c) 1992, 1993
13941 + * The Regents of the University of California. All rights reserved.
13942 + *
13943 + * Redistribution and use in source and binary forms, with or without
13944 + * modification, are permitted provided that the following conditions
13945 + * are met:
13946 + * 1. Redistributions of source code must retain the above copyright
13947 + * notice, this list of conditions and the following disclaimer.
13948 + * 2. Redistributions in binary form must reproduce the above copyright
13949 + * notice, this list of conditions and the following disclaimer in the
13950 + * documentation and/or other materials provided with the distribution.
13951 + * 3. Neither the name of the University nor the names of its contributors
13952 + * may be used to endorse or promote products derived from this software
13953 + * without specific prior written permission.
13954 + *
13955 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
13956 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13957 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13958 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
13959 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13960 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13961 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13962 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13963 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13964 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
13965 + * SUCH DAMAGE.
13966 + */
13967 +
13968 +#include "yportenv.h"
13969 +//#include <linux/string.h>
13970 +
13971 +/*
13972 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
13973 + */
13974 +#define swapcode(TYPE, parmi, parmj, n) { \
13975 + long i = (n) / sizeof (TYPE); \
13976 + register TYPE *pi = (TYPE *) (parmi); \
13977 + register TYPE *pj = (TYPE *) (parmj); \
13978 + do { \
13979 + register TYPE t = *pi; \
13980 + *pi++ = *pj; \
13981 + *pj++ = t; \
13982 + } while (--i > 0); \
13983 +}
13984 +
13985 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
13986 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
13987 +
13988 +static __inline void
13989 +swapfunc(char *a, char *b, int n, int swaptype)
13990 +{
13991 + if (swaptype <= 1)
13992 + swapcode(long, a, b, n)
13993 + else
13994 + swapcode(char, a, b, n)
13995 +}
13996 +
13997 +#define swap(a, b) \
13998 + if (swaptype == 0) { \
13999 + long t = *(long *)(a); \
14000 + *(long *)(a) = *(long *)(b); \
14001 + *(long *)(b) = t; \
14002 + } else \
14003 + swapfunc(a, b, es, swaptype)
14004 +
14005 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14006 +
14007 +static __inline char *
14008 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14009 +{
14010 + return cmp(a, b) < 0 ?
14011 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
14012 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
14013 +}
14014 +
14015 +#ifndef min
14016 +#define min(a,b) (((a) < (b)) ? (a) : (b))
14017 +#endif
14018 +
14019 +void
14020 +yaffs_qsort(void *aa, size_t n, size_t es,
14021 + int (*cmp)(const void *, const void *))
14022 +{
14023 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14024 + int d, r, swaptype, swap_cnt;
14025 + register char *a = aa;
14026 +
14027 +loop: SWAPINIT(a, es);
14028 + swap_cnt = 0;
14029 + if (n < 7) {
14030 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14031 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14032 + pl -= es)
14033 + swap(pl, pl - es);
14034 + return;
14035 + }
14036 + pm = (char *)a + (n / 2) * es;
14037 + if (n > 7) {
14038 + pl = (char *)a;
14039 + pn = (char *)a + (n - 1) * es;
14040 + if (n > 40) {
14041 + d = (n / 8) * es;
14042 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14043 + pm = med3(pm - d, pm, pm + d, cmp);
14044 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14045 + }
14046 + pm = med3(pl, pm, pn, cmp);
14047 + }
14048 + swap(a, pm);
14049 + pa = pb = (char *)a + es;
14050 +
14051 + pc = pd = (char *)a + (n - 1) * es;
14052 + for (;;) {
14053 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14054 + if (r == 0) {
14055 + swap_cnt = 1;
14056 + swap(pa, pb);
14057 + pa += es;
14058 + }
14059 + pb += es;
14060 + }
14061 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14062 + if (r == 0) {
14063 + swap_cnt = 1;
14064 + swap(pc, pd);
14065 + pd -= es;
14066 + }
14067 + pc -= es;
14068 + }
14069 + if (pb > pc)
14070 + break;
14071 + swap(pb, pc);
14072 + swap_cnt = 1;
14073 + pb += es;
14074 + pc -= es;
14075 + }
14076 + if (swap_cnt == 0) { /* Switch to insertion sort */
14077 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14078 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14079 + pl -= es)
14080 + swap(pl, pl - es);
14081 + return;
14082 + }
14083 +
14084 + pn = (char *)a + n * es;
14085 + r = min(pa - (char *)a, pb - pa);
14086 + vecswap(a, pb - r, r);
14087 + r = min((long)(pd - pc), (long)(pn - pd - es));
14088 + vecswap(pb, pn - r, r);
14089 + if ((r = pb - pa) > es)
14090 + yaffs_qsort(a, r / es, es, cmp);
14091 + if ((r = pd - pc) > es) {
14092 + /* Iterate rather than recurse to save stack space */
14093 + a = pn - r;
14094 + n = r / es;
14095 + goto loop;
14096 + }
14097 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14098 +}
14099 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h
14100 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
14101 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h 2007-05-30 13:17:17.000000000 +0200
14102 @@ -0,0 +1,23 @@
14103 +/*
14104 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14105 + *
14106 + * Copyright (C) 2002-2007 Aleph One Ltd.
14107 + * for Toby Churchill Ltd and Brightstar Engineering
14108 + *
14109 + * Created by Charles Manning <charles@aleph1.co.uk>
14110 + *
14111 + * This program is free software; you can redistribute it and/or modify
14112 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14113 + * published by the Free Software Foundation.
14114 + *
14115 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14116 + */
14117 +
14118 +
14119 +#ifndef __YAFFS_QSORT_H__
14120 +#define __YAFFS_QSORT_H__
14121 +
14122 +extern void yaffs_qsort (void *const base, size_t total_elems, size_t size,
14123 + int (*cmp)(const void *, const void *));
14124 +
14125 +#endif
14126 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c
14127 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
14128 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c 2007-05-30 13:17:17.000000000 +0200
14129 @@ -0,0 +1,530 @@
14130 +/*
14131 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14132 + *
14133 + * Copyright (C) 2002-2007 Aleph One Ltd.
14134 + * for Toby Churchill Ltd and Brightstar Engineering
14135 + *
14136 + * Created by Charles Manning <charles@aleph1.co.uk>
14137 + *
14138 + * This program is free software; you can redistribute it and/or modify
14139 + * it under the terms of the GNU General Public License version 2 as
14140 + * published by the Free Software Foundation.
14141 + */
14142 +
14143 +#include "yaffs_guts.h"
14144 +#include "yaffs_tagscompat.h"
14145 +#include "yaffs_ecc.h"
14146 +
14147 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
14148 +#ifdef NOTYET
14149 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
14150 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14151 + const __u8 * data,
14152 + const yaffs_Spare * spare);
14153 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14154 + const yaffs_Spare * spare);
14155 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
14156 +#endif
14157 +
14158 +static const char yaffs_countBitsTable[256] = {
14159 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14160 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14161 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14162 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14163 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14164 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14165 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14166 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14167 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14168 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14169 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14170 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14171 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14172 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14173 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14174 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14175 +};
14176 +
14177 +int yaffs_CountBits(__u8 x)
14178 +{
14179 + int retVal;
14180 + retVal = yaffs_countBitsTable[x];
14181 + return retVal;
14182 +}
14183 +
14184 +/********** Tags ECC calculations *********/
14185 +
14186 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
14187 +{
14188 + yaffs_ECCCalculate(data, spare->ecc1);
14189 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14190 +}
14191 +
14192 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
14193 +{
14194 + /* Calculate an ecc */
14195 +
14196 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14197 + unsigned i, j;
14198 + unsigned ecc = 0;
14199 + unsigned bit = 0;
14200 +
14201 + tags->ecc = 0;
14202 +
14203 + for (i = 0; i < 8; i++) {
14204 + for (j = 1; j & 0xff; j <<= 1) {
14205 + bit++;
14206 + if (b[i] & j) {
14207 + ecc ^= bit;
14208 + }
14209 + }
14210 + }
14211 +
14212 + tags->ecc = ecc;
14213 +
14214 +}
14215 +
14216 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
14217 +{
14218 + unsigned ecc = tags->ecc;
14219 +
14220 + yaffs_CalcTagsECC(tags);
14221 +
14222 + ecc ^= tags->ecc;
14223 +
14224 + if (ecc && ecc <= 64) {
14225 + /* TODO: Handle the failure better. Retire? */
14226 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14227 +
14228 + ecc--;
14229 +
14230 + b[ecc / 8] ^= (1 << (ecc & 7));
14231 +
14232 + /* Now recvalc the ecc */
14233 + yaffs_CalcTagsECC(tags);
14234 +
14235 + return 1; /* recovered error */
14236 + } else if (ecc) {
14237 + /* Wierd ecc failure value */
14238 + /* TODO Need to do somethiong here */
14239 + return -1; /* unrecovered error */
14240 + }
14241 +
14242 + return 0;
14243 +}
14244 +
14245 +/********** Tags **********/
14246 +
14247 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
14248 + yaffs_Tags * tagsPtr)
14249 +{
14250 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14251 +
14252 + yaffs_CalcTagsECC(tagsPtr);
14253 +
14254 + sparePtr->tagByte0 = tu->asBytes[0];
14255 + sparePtr->tagByte1 = tu->asBytes[1];
14256 + sparePtr->tagByte2 = tu->asBytes[2];
14257 + sparePtr->tagByte3 = tu->asBytes[3];
14258 + sparePtr->tagByte4 = tu->asBytes[4];
14259 + sparePtr->tagByte5 = tu->asBytes[5];
14260 + sparePtr->tagByte6 = tu->asBytes[6];
14261 + sparePtr->tagByte7 = tu->asBytes[7];
14262 +}
14263 +
14264 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
14265 + yaffs_Tags * tagsPtr)
14266 +{
14267 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14268 + int result;
14269 +
14270 + tu->asBytes[0] = sparePtr->tagByte0;
14271 + tu->asBytes[1] = sparePtr->tagByte1;
14272 + tu->asBytes[2] = sparePtr->tagByte2;
14273 + tu->asBytes[3] = sparePtr->tagByte3;
14274 + tu->asBytes[4] = sparePtr->tagByte4;
14275 + tu->asBytes[5] = sparePtr->tagByte5;
14276 + tu->asBytes[6] = sparePtr->tagByte6;
14277 + tu->asBytes[7] = sparePtr->tagByte7;
14278 +
14279 + result = yaffs_CheckECCOnTags(tagsPtr);
14280 + if (result > 0) {
14281 + dev->tagsEccFixed++;
14282 + } else if (result < 0) {
14283 + dev->tagsEccUnfixed++;
14284 + }
14285 +}
14286 +
14287 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
14288 +{
14289 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14290 +}
14291 +
14292 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14293 + int chunkInNAND, const __u8 * data,
14294 + yaffs_Spare * spare)
14295 +{
14296 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14297 + T(YAFFS_TRACE_ERROR,
14298 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14299 + chunkInNAND));
14300 + return YAFFS_FAIL;
14301 + }
14302 +
14303 + dev->nPageWrites++;
14304 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14305 +}
14306 +
14307 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14308 + int chunkInNAND,
14309 + __u8 * data,
14310 + yaffs_Spare * spare,
14311 + yaffs_ECCResult * eccResult,
14312 + int doErrorCorrection)
14313 +{
14314 + int retVal;
14315 + yaffs_Spare localSpare;
14316 +
14317 + dev->nPageReads++;
14318 +
14319 + if (!spare && data) {
14320 + /* If we don't have a real spare, then we use a local one. */
14321 + /* Need this for the calculation of the ecc */
14322 + spare = &localSpare;
14323 + }
14324 +
14325 + if (!dev->useNANDECC) {
14326 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14327 + if (data && doErrorCorrection) {
14328 + /* Do ECC correction */
14329 + /* Todo handle any errors */
14330 + int eccResult1, eccResult2;
14331 + __u8 calcEcc[3];
14332 +
14333 + yaffs_ECCCalculate(data, calcEcc);
14334 + eccResult1 =
14335 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14336 + yaffs_ECCCalculate(&data[256], calcEcc);
14337 + eccResult2 =
14338 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14339 +
14340 + if (eccResult1 > 0) {
14341 + T(YAFFS_TRACE_ERROR,
14342 + (TSTR
14343 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14344 + TENDSTR), chunkInNAND));
14345 + dev->eccFixed++;
14346 + } else if (eccResult1 < 0) {
14347 + T(YAFFS_TRACE_ERROR,
14348 + (TSTR
14349 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14350 + TENDSTR), chunkInNAND));
14351 + dev->eccUnfixed++;
14352 + }
14353 +
14354 + if (eccResult2 > 0) {
14355 + T(YAFFS_TRACE_ERROR,
14356 + (TSTR
14357 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14358 + TENDSTR), chunkInNAND));
14359 + dev->eccFixed++;
14360 + } else if (eccResult2 < 0) {
14361 + T(YAFFS_TRACE_ERROR,
14362 + (TSTR
14363 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14364 + TENDSTR), chunkInNAND));
14365 + dev->eccUnfixed++;
14366 + }
14367 +
14368 + if (eccResult1 || eccResult2) {
14369 + /* We had a data problem on this page */
14370 + yaffs_HandleReadDataError(dev, chunkInNAND);
14371 + }
14372 +
14373 + if (eccResult1 < 0 || eccResult2 < 0)
14374 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14375 + else if (eccResult1 > 0 || eccResult2 > 0)
14376 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14377 + else
14378 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14379 + }
14380 + } else {
14381 + /* Must allocate enough memory for spare+2*sizeof(int) */
14382 + /* for ecc results from device. */
14383 + struct yaffs_NANDSpare nspare;
14384 + retVal =
14385 + dev->readChunkFromNAND(dev, chunkInNAND, data,
14386 + (yaffs_Spare *) & nspare);
14387 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14388 + if (data && doErrorCorrection) {
14389 + if (nspare.eccres1 > 0) {
14390 + T(YAFFS_TRACE_ERROR,
14391 + (TSTR
14392 + ("**>>mtd ecc error fix performed on chunk %d:0"
14393 + TENDSTR), chunkInNAND));
14394 + } else if (nspare.eccres1 < 0) {
14395 + T(YAFFS_TRACE_ERROR,
14396 + (TSTR
14397 + ("**>>mtd ecc error unfixed on chunk %d:0"
14398 + TENDSTR), chunkInNAND));
14399 + }
14400 +
14401 + if (nspare.eccres2 > 0) {
14402 + T(YAFFS_TRACE_ERROR,
14403 + (TSTR
14404 + ("**>>mtd ecc error fix performed on chunk %d:1"
14405 + TENDSTR), chunkInNAND));
14406 + } else if (nspare.eccres2 < 0) {
14407 + T(YAFFS_TRACE_ERROR,
14408 + (TSTR
14409 + ("**>>mtd ecc error unfixed on chunk %d:1"
14410 + TENDSTR), chunkInNAND));
14411 + }
14412 +
14413 + if (nspare.eccres1 || nspare.eccres2) {
14414 + /* We had a data problem on this page */
14415 + yaffs_HandleReadDataError(dev, chunkInNAND);
14416 + }
14417 +
14418 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14419 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14420 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14421 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14422 + else
14423 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14424 +
14425 + }
14426 + }
14427 + return retVal;
14428 +}
14429 +
14430 +#ifdef NOTYET
14431 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14432 + int chunkInNAND)
14433 +{
14434 +
14435 + static int init = 0;
14436 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14437 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14438 + /* Might as well always allocate the larger size for */
14439 + /* dev->useNANDECC == true; */
14440 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14441 +
14442 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14443 +
14444 + if (!init) {
14445 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14446 + init = 1;
14447 + }
14448 +
14449 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14450 + return YAFFS_FAIL;
14451 + if (memcmp(cmpbuf, spare, 16))
14452 + return YAFFS_FAIL;
14453 +
14454 + return YAFFS_OK;
14455 +
14456 +}
14457 +#endif
14458 +
14459 +/*
14460 + * Functions for robustisizing
14461 + */
14462 +
14463 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
14464 +{
14465 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14466 +
14467 + /* Mark the block for retirement */
14468 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14469 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14470 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14471 +
14472 + /* TODO:
14473 + * Just do a garbage collection on the affected block
14474 + * then retire the block
14475 + * NB recursion
14476 + */
14477 +}
14478 +
14479 +#ifdef NOTYET
14480 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
14481 +{
14482 +}
14483 +
14484 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14485 + const __u8 * data,
14486 + const yaffs_Spare * spare)
14487 +{
14488 +}
14489 +
14490 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14491 + const yaffs_Spare * spare)
14492 +{
14493 +}
14494 +
14495 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
14496 +{
14497 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14498 +
14499 + /* Mark the block for retirement */
14500 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14501 + /* Delete the chunk */
14502 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14503 +}
14504 +
14505 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
14506 + const yaffs_Spare * s0, const yaffs_Spare * s1)
14507 +{
14508 +
14509 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14510 + s0->tagByte0 != s1->tagByte0 ||
14511 + s0->tagByte1 != s1->tagByte1 ||
14512 + s0->tagByte2 != s1->tagByte2 ||
14513 + s0->tagByte3 != s1->tagByte3 ||
14514 + s0->tagByte4 != s1->tagByte4 ||
14515 + s0->tagByte5 != s1->tagByte5 ||
14516 + s0->tagByte6 != s1->tagByte6 ||
14517 + s0->tagByte7 != s1->tagByte7 ||
14518 + s0->ecc1[0] != s1->ecc1[0] ||
14519 + s0->ecc1[1] != s1->ecc1[1] ||
14520 + s0->ecc1[2] != s1->ecc1[2] ||
14521 + s0->ecc2[0] != s1->ecc2[0] ||
14522 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14523 + return 0;
14524 + }
14525 +
14526 + return 1;
14527 +}
14528 +#endif /* NOTYET */
14529 +
14530 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14531 + int chunkInNAND,
14532 + const __u8 * data,
14533 + const yaffs_ExtendedTags *
14534 + eTags)
14535 +{
14536 + yaffs_Spare spare;
14537 + yaffs_Tags tags;
14538 +
14539 + yaffs_SpareInitialise(&spare);
14540 +
14541 + if (eTags->chunkDeleted) {
14542 + spare.pageStatus = 0;
14543 + } else {
14544 + tags.objectId = eTags->objectId;
14545 + tags.chunkId = eTags->chunkId;
14546 + tags.byteCount = eTags->byteCount;
14547 + tags.serialNumber = eTags->serialNumber;
14548 +
14549 + if (!dev->useNANDECC && data) {
14550 + yaffs_CalcECC(data, &spare);
14551 + }
14552 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14553 +
14554 + }
14555 +
14556 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14557 +}
14558 +
14559 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14560 + int chunkInNAND,
14561 + __u8 * data,
14562 + yaffs_ExtendedTags * eTags)
14563 +{
14564 +
14565 + yaffs_Spare spare;
14566 + yaffs_Tags tags;
14567 + yaffs_ECCResult eccResult;
14568 +
14569 + static yaffs_Spare spareFF;
14570 + static int init;
14571 +
14572 + if (!init) {
14573 + memset(&spareFF, 0xFF, sizeof(spareFF));
14574 + init = 1;
14575 + }
14576 +
14577 + if (yaffs_ReadChunkFromNAND
14578 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14579 + /* eTags may be NULL */
14580 + if (eTags) {
14581 +
14582 + int deleted =
14583 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14584 +
14585 + eTags->chunkDeleted = deleted;
14586 + eTags->eccResult = eccResult;
14587 + eTags->blockBad = 0; /* We're reading it */
14588 + /* therefore it is not a bad block */
14589 + eTags->chunkUsed =
14590 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14591 + 0) ? 1 : 0;
14592 +
14593 + if (eTags->chunkUsed) {
14594 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14595 +
14596 + eTags->objectId = tags.objectId;
14597 + eTags->chunkId = tags.chunkId;
14598 + eTags->byteCount = tags.byteCount;
14599 + eTags->serialNumber = tags.serialNumber;
14600 + }
14601 + }
14602 +
14603 + return YAFFS_OK;
14604 + } else {
14605 + return YAFFS_FAIL;
14606 + }
14607 +}
14608 +
14609 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14610 + int blockInNAND)
14611 +{
14612 +
14613 + yaffs_Spare spare;
14614 +
14615 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14616 +
14617 + spare.blockStatus = 'Y';
14618 +
14619 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14620 + &spare);
14621 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14622 + NULL, &spare);
14623 +
14624 + return YAFFS_OK;
14625 +
14626 +}
14627 +
14628 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14629 + int blockNo, yaffs_BlockState *
14630 + state,
14631 + int *sequenceNumber)
14632 +{
14633 +
14634 + yaffs_Spare spare0, spare1;
14635 + static yaffs_Spare spareFF;
14636 + static int init;
14637 + yaffs_ECCResult dummy;
14638 +
14639 + if (!init) {
14640 + memset(&spareFF, 0xFF, sizeof(spareFF));
14641 + init = 1;
14642 + }
14643 +
14644 + *sequenceNumber = 0;
14645 +
14646 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14647 + &spare0, &dummy, 1);
14648 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14649 + &spare1, &dummy, 1);
14650 +
14651 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14652 + *state = YAFFS_BLOCK_STATE_DEAD;
14653 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14654 + *state = YAFFS_BLOCK_STATE_EMPTY;
14655 + else
14656 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14657 +
14658 + return YAFFS_OK;
14659 +}
14660 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h
14661 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
14662 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h 2007-05-30 13:17:17.000000000 +0200
14663 @@ -0,0 +1,40 @@
14664 +/*
14665 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14666 + *
14667 + * Copyright (C) 2002-2007 Aleph One Ltd.
14668 + * for Toby Churchill Ltd and Brightstar Engineering
14669 + *
14670 + * Created by Charles Manning <charles@aleph1.co.uk>
14671 + *
14672 + * This program is free software; you can redistribute it and/or modify
14673 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14674 + * published by the Free Software Foundation.
14675 + *
14676 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14677 + */
14678 +
14679 +#ifndef __YAFFS_TAGSCOMPAT_H__
14680 +#define __YAFFS_TAGSCOMPAT_H__
14681 +
14682 +#include "yaffs_guts.h"
14683 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14684 + int chunkInNAND,
14685 + const __u8 * data,
14686 + const yaffs_ExtendedTags *
14687 + tags);
14688 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14689 + int chunkInNAND,
14690 + __u8 * data,
14691 + yaffs_ExtendedTags *
14692 + tags);
14693 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14694 + int blockNo);
14695 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14696 + int blockNo, yaffs_BlockState *
14697 + state, int *sequenceNumber);
14698 +
14699 +void yaffs_CalcTagsECC(yaffs_Tags * tags);
14700 +int yaffs_CheckECCOnTags(yaffs_Tags * tags);
14701 +int yaffs_CountBits(__u8 byte);
14702 +
14703 +#endif
14704 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c
14705 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
14706 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c 2007-05-30 13:17:17.000000000 +0200
14707 @@ -0,0 +1,28 @@
14708 +/*
14709 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14710 + *
14711 + * Copyright (C) 2002-2007 Aleph One Ltd.
14712 + * for Toby Churchill Ltd and Brightstar Engineering
14713 + *
14714 + * Created by Charles Manning <charles@aleph1.co.uk>
14715 + *
14716 + * This program is free software; you can redistribute it and/or modify
14717 + * it under the terms of the GNU General Public License version 2 as
14718 + * published by the Free Software Foundation.
14719 + */
14720 +
14721 +#include "yaffs_tagsvalidity.h"
14722 +
14723 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
14724 +{
14725 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14726 + tags->validMarker0 = 0xAAAAAAAA;
14727 + tags->validMarker1 = 0x55555555;
14728 +}
14729 +
14730 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
14731 +{
14732 + return (tags->validMarker0 == 0xAAAAAAAA &&
14733 + tags->validMarker1 == 0x55555555);
14734 +
14735 +}
14736 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h
14737 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
14738 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h 2007-05-30 13:17:17.000000000 +0200
14739 @@ -0,0 +1,24 @@
14740 +/*
14741 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14742 + *
14743 + * Copyright (C) 2002-2007 Aleph One Ltd.
14744 + * for Toby Churchill Ltd and Brightstar Engineering
14745 + *
14746 + * Created by Charles Manning <charles@aleph1.co.uk>
14747 + *
14748 + * This program is free software; you can redistribute it and/or modify
14749 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14750 + * published by the Free Software Foundation.
14751 + *
14752 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14753 + */
14754 +
14755 +
14756 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14757 +#define __YAFFS_TAGS_VALIDITY_H__
14758 +
14759 +#include "yaffs_guts.h"
14760 +
14761 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
14762 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
14763 +#endif
14764 diff -urN linux-2.6.21.1/fs/yaffs2/yaffsinterface.h linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h
14765 --- linux-2.6.21.1/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
14766 +++ linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h 2007-05-30 13:17:16.000000000 +0200
14767 @@ -0,0 +1,21 @@
14768 +/*
14769 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14770 + *
14771 + * Copyright (C) 2002-2007 Aleph One Ltd.
14772 + * for Toby Churchill Ltd and Brightstar Engineering
14773 + *
14774 + * Created by Charles Manning <charles@aleph1.co.uk>
14775 + *
14776 + * This program is free software; you can redistribute it and/or modify
14777 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14778 + * published by the Free Software Foundation.
14779 + *
14780 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14781 + */
14782 +
14783 +#ifndef __YAFFSINTERFACE_H__
14784 +#define __YAFFSINTERFACE_H__
14785 +
14786 +int yaffs_Initialise(unsigned nBlocks);
14787 +
14788 +#endif
14789 diff -urN linux-2.6.21.1/fs/yaffs2/yportenv.h linux-2.6.21.1.new/fs/yaffs2/yportenv.h
14790 --- linux-2.6.21.1/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
14791 +++ linux-2.6.21.1.new/fs/yaffs2/yportenv.h 2007-05-30 13:17:17.000000000 +0200
14792 @@ -0,0 +1,186 @@
14793 +/*
14794 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14795 + *
14796 + * Copyright (C) 2002-2007 Aleph One Ltd.
14797 + * for Toby Churchill Ltd and Brightstar Engineering
14798 + *
14799 + * Created by Charles Manning <charles@aleph1.co.uk>
14800 + *
14801 + * This program is free software; you can redistribute it and/or modify
14802 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14803 + * published by the Free Software Foundation.
14804 + *
14805 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14806 + */
14807 +
14808 +
14809 +#ifndef __YPORTENV_H__
14810 +#define __YPORTENV_H__
14811 +
14812 +#if defined CONFIG_YAFFS_WINCE
14813 +
14814 +#include "ywinceenv.h"
14815 +
14816 +#elif defined __KERNEL__
14817 +
14818 +#include "moduleconfig.h"
14819 +
14820 +/* Linux kernel */
14821 +#include <linux/version.h>
14822 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
14823 +#include <linux/config.h>
14824 +#endif
14825 +#include <linux/kernel.h>
14826 +#include <linux/mm.h>
14827 +#include <linux/string.h>
14828 +#include <linux/slab.h>
14829 +#include <linux/vmalloc.h>
14830 +
14831 +#define YCHAR char
14832 +#define YUCHAR unsigned char
14833 +#define _Y(x) x
14834 +#define yaffs_strcpy(a,b) strcpy(a,b)
14835 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14836 +#define yaffs_strncmp(a,b,c) strncmp(a,b,c)
14837 +#define yaffs_strlen(s) strlen(s)
14838 +#define yaffs_sprintf sprintf
14839 +#define yaffs_toupper(a) toupper(a)
14840 +
14841 +#define Y_INLINE inline
14842 +
14843 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14844 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14845 +
14846 +/* #define YPRINTF(x) printk x */
14847 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
14848 +#define YFREE(x) kfree(x)
14849 +#define YMALLOC_ALT(x) vmalloc(x)
14850 +#define YFREE_ALT(x) vfree(x)
14851 +#define YMALLOC_DMA(x) YMALLOC(x)
14852 +
14853 +// KR - added for use in scan so processes aren't blocked indefinitely.
14854 +#define YYIELD() schedule()
14855 +
14856 +#define YAFFS_ROOT_MODE 0666
14857 +#define YAFFS_LOSTNFOUND_MODE 0666
14858 +
14859 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
14860 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14861 +#define Y_TIME_CONVERT(x) (x).tv_sec
14862 +#else
14863 +#define Y_CURRENT_TIME CURRENT_TIME
14864 +#define Y_TIME_CONVERT(x) (x)
14865 +#endif
14866 +
14867 +#define yaffs_SumCompare(x,y) ((x) == (y))
14868 +#define yaffs_strcmp(a,b) strcmp(a,b)
14869 +
14870 +#define TENDSTR "\n"
14871 +#define TSTR(x) KERN_WARNING x
14872 +#define TOUT(p) printk p
14873 +
14874 +#define yaffs_trace(mask, fmt, args...) \
14875 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14876 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14877 + } while (0)
14878 +
14879 +#define compile_time_assertion(assertion) \
14880 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14881 +
14882 +#elif defined CONFIG_YAFFS_DIRECT
14883 +
14884 +/* Direct interface */
14885 +#include "ydirectenv.h"
14886 +
14887 +#elif defined CONFIG_YAFFS_UTIL
14888 +
14889 +/* Stuff for YAFFS utilities */
14890 +
14891 +#include "stdlib.h"
14892 +#include "stdio.h"
14893 +#include "string.h"
14894 +
14895 +#include "devextras.h"
14896 +
14897 +#define YMALLOC(x) malloc(x)
14898 +#define YFREE(x) free(x)
14899 +#define YMALLOC_ALT(x) malloc(x)
14900 +#define YFREE_ALT(x) free(x)
14901 +
14902 +#define YCHAR char
14903 +#define YUCHAR unsigned char
14904 +#define _Y(x) x
14905 +#define yaffs_strcpy(a,b) strcpy(a,b)
14906 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14907 +#define yaffs_strlen(s) strlen(s)
14908 +#define yaffs_sprintf sprintf
14909 +#define yaffs_toupper(a) toupper(a)
14910 +
14911 +#define Y_INLINE inline
14912 +
14913 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
14914 +/* #define YALERT(s) YINFO(s) */
14915 +
14916 +#define TENDSTR "\n"
14917 +#define TSTR(x) x
14918 +#define TOUT(p) printf p
14919 +
14920 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14921 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14922 +/* #define YPRINTF(x) printf x */
14923 +
14924 +#define YAFFS_ROOT_MODE 0666
14925 +#define YAFFS_LOSTNFOUND_MODE 0666
14926 +
14927 +#define yaffs_SumCompare(x,y) ((x) == (y))
14928 +#define yaffs_strcmp(a,b) strcmp(a,b)
14929 +
14930 +#else
14931 +/* Should have specified a configuration type */
14932 +#error Unknown configuration
14933 +
14934 +#endif
14935 +
14936 +/* see yaffs_fs.c */
14937 +extern unsigned int yaffs_traceMask;
14938 +extern unsigned int yaffs_wr_attempts;
14939 +
14940 +/*
14941 + * Tracing flags.
14942 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
14943 + */
14944 +
14945 +#define YAFFS_TRACE_OS 0x00000002
14946 +#define YAFFS_TRACE_ALLOCATE 0x00000004
14947 +#define YAFFS_TRACE_SCAN 0x00000008
14948 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
14949 +#define YAFFS_TRACE_ERASE 0x00000020
14950 +#define YAFFS_TRACE_GC 0x00000040
14951 +#define YAFFS_TRACE_WRITE 0x00000080
14952 +#define YAFFS_TRACE_TRACING 0x00000100
14953 +#define YAFFS_TRACE_DELETION 0x00000200
14954 +#define YAFFS_TRACE_BUFFERS 0x00000400
14955 +#define YAFFS_TRACE_NANDACCESS 0x00000800
14956 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
14957 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
14958 +#define YAFFS_TRACE_MTD 0x00004000
14959 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
14960 +
14961 +#define YAFFS_TRACE_VERIFY 0x00010000
14962 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
14963 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
14964 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
14965 +
14966 +
14967 +#define YAFFS_TRACE_ERROR 0x40000000
14968 +#define YAFFS_TRACE_BUG 0x80000000
14969 +#define YAFFS_TRACE_ALWAYS 0xF0000000
14970 +
14971 +
14972 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p);} while(0)
14973 +
14974 +#ifndef CONFIG_YAFFS_WINCE
14975 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))
14976 +#endif
14977 +
14978 +#endif
14979 --- linux-2.6.21.1/fs/Makefile 2007-05-30 13:16:21.000000000 +0200
14980 +++ linux-2.6.21.1.new/fs/Makefile 2007-05-30 13:27:34.000000000 +0200
14981 @@ -116,3 +116,4 @@
14982 obj-$(CONFIG_DEBUG_FS) += debugfs/
14983 obj-$(CONFIG_OCFS2_FS) += ocfs2/
14984 obj-$(CONFIG_GFS2_FS) += gfs2/
14985 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
14986 --- linux-2.6.21.1/fs/Kconfig 2007-05-30 13:16:21.000000000 +0200
14987 +++ linux-2.6.21.1.new/fs/Kconfig 2007-05-30 13:29:14.000000000 +0200
14988 @@ -419,6 +419,7 @@
14989
14990 source "fs/xfs/Kconfig"
14991 source "fs/gfs2/Kconfig"
14992 +source "fs/yaffs2/Kconfig"
14993
14994 config OCFS2_FS
14995 tristate "OCFS2 file system support"
This page took 0.599483 seconds and 5 git commands to generate.