fix rtsp patch
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.23 / 510-Yaffs.patch
1 Index: linux-2.6.23-rc6/fs/Kconfig
2 ===================================================================
3 --- linux-2.6.23-rc6.orig/fs/Kconfig 2007-09-21 16:24:06.000000000 +0800
4 +++ linux-2.6.23-rc6/fs/Kconfig 2007-09-21 16:24:08.000000000 +0800
5 @@ -419,6 +419,7 @@
6
7 source "fs/xfs/Kconfig"
8 source "fs/gfs2/Kconfig"
9 +source "fs/yaffs2/Kconfig"
10
11 config OCFS2_FS
12 tristate "OCFS2 file system support"
13 Index: linux-2.6.23-rc6/fs/Makefile
14 ===================================================================
15 --- linux-2.6.23-rc6.orig/fs/Makefile 2007-09-21 16:24:06.000000000 +0800
16 +++ linux-2.6.23-rc6/fs/Makefile 2007-09-21 16:24:08.000000000 +0800
17 @@ -120,3 +120,4 @@
18 obj-$(CONFIG_DEBUG_FS) += debugfs/
19 obj-$(CONFIG_OCFS2_FS) += ocfs2/
20 obj-$(CONFIG_GFS2_FS) += gfs2/
21 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
22 Index: linux-2.6.23-rc6/fs/yaffs2/Kconfig
23 ===================================================================
24 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
25 +++ linux-2.6.23-rc6/fs/yaffs2/Kconfig 2007-09-21 16:24:08.000000000 +0800
26 @@ -0,0 +1,175 @@
27 +#
28 +# YAFFS file system configurations
29 +#
30 +
31 +config YAFFS_FS
32 + tristate "YAFFS2 file system support"
33 + default n
34 + depends on MTD
35 + select YAFFS_YAFFS1
36 + select YAFFS_YAFFS2
37 + help
38 + YAFFS2, or Yet Another Flash Filing System, is a filing system
39 + optimised for NAND Flash chips.
40 +
41 + To compile the YAFFS2 file system support as a module, choose M
42 + here: the module will be called yaffs2.
43 +
44 + If unsure, say N.
45 +
46 + Further information on YAFFS2 is available at
47 + <http://www.aleph1.co.uk/yaffs/>.
48 +
49 +config YAFFS_YAFFS1
50 + bool "512 byte / page devices"
51 + depends on YAFFS_FS
52 + default y
53 + help
54 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
55 +
56 + Not needed for 2K-page devices.
57 +
58 + If unsure, say Y.
59 +
60 +config YAFFS_9BYTE_TAGS
61 + bool "Use older-style on-NAND data format with pageStatus byte"
62 + depends on YAFFS_YAFFS1
63 + default n
64 + help
65 +
66 + Older-style on-NAND data format has a "pageStatus" byte to record
67 + chunk/page state. This byte is zero when the page is discarded.
68 + Choose this option if you have existing on-NAND data using this
69 + format that you need to continue to support. New data written
70 + also uses the older-style format. Note: Use of this option
71 + generally requires that MTD's oob layout be adjusted to use the
72 + older-style format. See notes on tags formats and MTD versions.
73 +
74 + If unsure, say N.
75 +
76 +config YAFFS_DOES_ECC
77 + bool "Lets Yaffs do its own ECC"
78 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
79 + default n
80 + help
81 + This enables Yaffs to use its own ECC functions instead of using
82 + the ones from the generic MTD-NAND driver.
83 +
84 + If unsure, say N.
85 +
86 +config YAFFS_ECC_WRONG_ORDER
87 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
88 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
89 + default n
90 + help
91 + This makes yaffs_ecc.c use the same ecc byte order as Steven
92 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
93 + order as SmartMedia.
94 +
95 + If unsure, say N.
96 +
97 +config YAFFS_YAFFS2
98 + bool "2048 byte (or larger) / page devices"
99 + depends on YAFFS_FS
100 + default y
101 + help
102 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
103 +
104 + If unsure, say Y.
105 +
106 +config YAFFS_AUTO_YAFFS2
107 + bool "Autoselect yaffs2 format"
108 + depends on YAFFS_YAFFS2
109 + default y
110 + help
111 + Without this, you need to explicitely use yaffs2 as the file
112 + system type. With this, you can say "yaffs" and yaffs or yaffs2
113 + will be used depending on the device page size (yaffs on
114 + 512-byte page devices, yaffs2 on 2K page devices).
115 +
116 + If unsure, say Y.
117 +
118 +config YAFFS_DISABLE_LAZY_LOAD
119 + bool "Disable lazy loading"
120 + depends on YAFFS_YAFFS2
121 + default n
122 + help
123 + "Lazy loading" defers loading file details until they are
124 + required. This saves mount time, but makes the first look-up
125 + a bit longer.
126 +
127 + Lazy loading will only happen if enabled by this option being 'n'
128 + and if the appropriate tags are available, else yaffs2 will
129 + automatically fall back to immediate loading and do the right
130 + thing.
131 +
132 + Lazy laoding will be required by checkpointing.
133 +
134 + Setting this to 'y' will disable lazy loading.
135 +
136 + If unsure, say N.
137 +
138 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
139 + int "Reserved blocks for checkpointing"
140 + depends on YAFFS_YAFFS2
141 + default 10
142 + help
143 + Give the number of Blocks to reserve for checkpointing.
144 + Checkpointing saves the state at unmount so that mounting is
145 + much faster as a scan of all the flash to regenerate this state
146 + is not needed. These Blocks are reserved per partition, so if
147 + you have very small partitions the default (10) may be a mess
148 + for you. You can set this value to 0, but that does not mean
149 + checkpointing is disabled at all. There only won't be any
150 + specially reserved blocks for checkpointing, so if there is
151 + enough free space on the filesystem, it will be used for
152 + checkpointing.
153 +
154 + If unsure, leave at default (10), but don't wonder if there are
155 + always 2MB used on your large page device partition (10 x 2k
156 + pagesize). When using small partitions or when being very small
157 + on space, you probably want to set this to zero.
158 +
159 +config YAFFS_DISABLE_WIDE_TNODES
160 + bool "Turn off wide tnodes"
161 + depends on YAFFS_FS
162 + default n
163 + help
164 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
165 + page devices and >=128MB for 2k page devices. They use slightly
166 + more RAM but are faster since they eliminate chunk group
167 + searching.
168 +
169 + Setting this to 'y' will force tnode width to 16 bits and save
170 + memory but make large arrays slower.
171 +
172 + If unsure, say N.
173 +
174 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
175 + bool "Force chunk erase check"
176 + depends on YAFFS_FS
177 + default n
178 + help
179 + Normally YAFFS only checks chunks before writing until an erased
180 + chunk is found. This helps to detect any partially written
181 + chunks that might have happened due to power loss.
182 +
183 + Enabling this forces on the test that chunks are erased in flash
184 + before writing to them. This takes more time but is potentially
185 + a bit more secure.
186 +
187 + Suggest setting Y during development and ironing out driver
188 + issues etc. Suggest setting to N if you want faster writing.
189 +
190 + If unsure, say Y.
191 +
192 +config YAFFS_SHORT_NAMES_IN_RAM
193 + bool "Cache short names in RAM"
194 + depends on YAFFS_FS
195 + default y
196 + help
197 + If this config is set, then short names are stored with the
198 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
199 + but makes look-ups faster.
200 +
201 + If unsure, say Y.
202 Index: linux-2.6.23-rc6/fs/yaffs2/Makefile
203 ===================================================================
204 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
205 +++ linux-2.6.23-rc6/fs/yaffs2/Makefile 2007-09-21 16:24:08.000000000 +0800
206 @@ -0,0 +1,11 @@
207 +#
208 +# Makefile for the linux YAFFS filesystem routines.
209 +#
210 +
211 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
212 +
213 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
214 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
215 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
216 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
217 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
218 Index: linux-2.6.23-rc6/fs/yaffs2/devextras.h
219 ===================================================================
220 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
221 +++ linux-2.6.23-rc6/fs/yaffs2/devextras.h 2007-09-21 16:24:08.000000000 +0800
222 @@ -0,0 +1,264 @@
223 +/*
224 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
225 + *
226 + * Copyright (C) 2002-2007 Aleph One Ltd.
227 + * for Toby Churchill Ltd and Brightstar Engineering
228 + *
229 + * Created by Charles Manning <charles@aleph1.co.uk>
230 + *
231 + * This program is free software; you can redistribute it and/or modify
232 + * it under the terms of the GNU Lesser General Public License version 2.1 as
233 + * published by the Free Software Foundation.
234 + *
235 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
236 + */
237 +
238 +/*
239 + * This file is just holds extra declarations used during development.
240 + * Most of these are from kernel includes placed here so we can use them in
241 + * applications.
242 + *
243 + */
244 +
245 +#ifndef __EXTRAS_H__
246 +#define __EXTRAS_H__
247 +
248 +#if defined WIN32
249 +#define __inline__ __inline
250 +#define new newHack
251 +#endif
252 +
253 +#if !(defined __KERNEL__) || (defined WIN32)
254 +
255 +/* User space defines */
256 +
257 +typedef unsigned char __u8;
258 +typedef unsigned short __u16;
259 +typedef unsigned __u32;
260 +
261 +/*
262 + * Simple doubly linked list implementation.
263 + *
264 + * Some of the internal functions ("__xxx") are useful when
265 + * manipulating whole lists rather than single entries, as
266 + * sometimes we already know the next/prev entries and we can
267 + * generate better code by using them directly rather than
268 + * using the generic single-entry routines.
269 + */
270 +
271 +#define prefetch(x) 1
272 +
273 +struct list_head {
274 + struct list_head *next, *prev;
275 +};
276 +
277 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
278 +
279 +#define LIST_HEAD(name) \
280 + struct list_head name = LIST_HEAD_INIT(name)
281 +
282 +#define INIT_LIST_HEAD(ptr) do { \
283 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
284 +} while (0)
285 +
286 +/*
287 + * Insert a new entry between two known consecutive entries.
288 + *
289 + * This is only for internal list manipulation where we know
290 + * the prev/next entries already!
291 + */
292 +static __inline__ void __list_add(struct list_head *new,
293 + struct list_head *prev,
294 + struct list_head *next)
295 +{
296 + next->prev = new;
297 + new->next = next;
298 + new->prev = prev;
299 + prev->next = new;
300 +}
301 +
302 +/**
303 + * list_add - add a new entry
304 + * @new: new entry to be added
305 + * @head: list head to add it after
306 + *
307 + * Insert a new entry after the specified head.
308 + * This is good for implementing stacks.
309 + */
310 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
311 +{
312 + __list_add(new, head, head->next);
313 +}
314 +
315 +/**
316 + * list_add_tail - add a new entry
317 + * @new: new entry to be added
318 + * @head: list head to add it before
319 + *
320 + * Insert a new entry before the specified head.
321 + * This is useful for implementing queues.
322 + */
323 +static __inline__ void list_add_tail(struct list_head *new,
324 + struct list_head *head)
325 +{
326 + __list_add(new, head->prev, head);
327 +}
328 +
329 +/*
330 + * Delete a list entry by making the prev/next entries
331 + * point to each other.
332 + *
333 + * This is only for internal list manipulation where we know
334 + * the prev/next entries already!
335 + */
336 +static __inline__ void __list_del(struct list_head *prev,
337 + struct list_head *next)
338 +{
339 + next->prev = prev;
340 + prev->next = next;
341 +}
342 +
343 +/**
344 + * list_del - deletes entry from list.
345 + * @entry: the element to delete from the list.
346 + * Note: list_empty on entry does not return true after this, the entry is
347 + * in an undefined state.
348 + */
349 +static __inline__ void list_del(struct list_head *entry)
350 +{
351 + __list_del(entry->prev, entry->next);
352 +}
353 +
354 +/**
355 + * list_del_init - deletes entry from list and reinitialize it.
356 + * @entry: the element to delete from the list.
357 + */
358 +static __inline__ void list_del_init(struct list_head *entry)
359 +{
360 + __list_del(entry->prev, entry->next);
361 + INIT_LIST_HEAD(entry);
362 +}
363 +
364 +/**
365 + * list_empty - tests whether a list is empty
366 + * @head: the list to test.
367 + */
368 +static __inline__ int list_empty(struct list_head *head)
369 +{
370 + return head->next == head;
371 +}
372 +
373 +/**
374 + * list_splice - join two lists
375 + * @list: the new list to add.
376 + * @head: the place to add it in the first list.
377 + */
378 +static __inline__ void list_splice(struct list_head *list,
379 + struct list_head *head)
380 +{
381 + struct list_head *first = list->next;
382 +
383 + if (first != list) {
384 + struct list_head *last = list->prev;
385 + struct list_head *at = head->next;
386 +
387 + first->prev = head;
388 + head->next = first;
389 +
390 + last->next = at;
391 + at->prev = last;
392 + }
393 +}
394 +
395 +/**
396 + * list_entry - get the struct for this entry
397 + * @ptr: the &struct list_head pointer.
398 + * @type: the type of the struct this is embedded in.
399 + * @member: the name of the list_struct within the struct.
400 + */
401 +#define list_entry(ptr, type, member) \
402 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
403 +
404 +/**
405 + * list_for_each - iterate over a list
406 + * @pos: the &struct list_head to use as a loop counter.
407 + * @head: the head for your list.
408 + */
409 +#define list_for_each(pos, head) \
410 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
411 + pos = pos->next, prefetch(pos->next))
412 +
413 +/**
414 + * list_for_each_safe - iterate over a list safe against removal
415 + * of list entry
416 + * @pos: the &struct list_head to use as a loop counter.
417 + * @n: another &struct list_head to use as temporary storage
418 + * @head: the head for your list.
419 + */
420 +#define list_for_each_safe(pos, n, head) \
421 + for (pos = (head)->next, n = pos->next; pos != (head); \
422 + pos = n, n = pos->next)
423 +
424 +/*
425 + * File types
426 + */
427 +#define DT_UNKNOWN 0
428 +#define DT_FIFO 1
429 +#define DT_CHR 2
430 +#define DT_DIR 4
431 +#define DT_BLK 6
432 +#define DT_REG 8
433 +#define DT_LNK 10
434 +#define DT_SOCK 12
435 +#define DT_WHT 14
436 +
437 +#ifndef WIN32
438 +#include <sys/stat.h>
439 +#endif
440 +
441 +/*
442 + * Attribute flags. These should be or-ed together to figure out what
443 + * has been changed!
444 + */
445 +#define ATTR_MODE 1
446 +#define ATTR_UID 2
447 +#define ATTR_GID 4
448 +#define ATTR_SIZE 8
449 +#define ATTR_ATIME 16
450 +#define ATTR_MTIME 32
451 +#define ATTR_CTIME 64
452 +#define ATTR_ATIME_SET 128
453 +#define ATTR_MTIME_SET 256
454 +#define ATTR_FORCE 512 /* Not a change, but a change it */
455 +#define ATTR_ATTR_FLAG 1024
456 +
457 +struct iattr {
458 + unsigned int ia_valid;
459 + unsigned ia_mode;
460 + unsigned ia_uid;
461 + unsigned ia_gid;
462 + unsigned ia_size;
463 + unsigned ia_atime;
464 + unsigned ia_mtime;
465 + unsigned ia_ctime;
466 + unsigned int ia_attr_flags;
467 +};
468 +
469 +#define KERN_DEBUG
470 +
471 +#else
472 +
473 +#ifndef WIN32
474 +#include <linux/types.h>
475 +#include <linux/list.h>
476 +#include <linux/fs.h>
477 +#include <linux/stat.h>
478 +#endif
479 +
480 +#endif
481 +
482 +#if defined WIN32
483 +#undef new
484 +#endif
485 +
486 +#endif
487 Index: linux-2.6.23-rc6/fs/yaffs2/moduleconfig.h
488 ===================================================================
489 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
490 +++ linux-2.6.23-rc6/fs/yaffs2/moduleconfig.h 2007-09-21 16:24:08.000000000 +0800
491 @@ -0,0 +1,65 @@
492 +/*
493 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
494 + *
495 + * Copyright (C) 2002-2007 Aleph One Ltd.
496 + * for Toby Churchill Ltd and Brightstar Engineering
497 + *
498 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
499 + *
500 + * This program is free software; you can redistribute it and/or modify
501 + * it under the terms of the GNU Lesser General Public License version 2.1 as
502 + * published by the Free Software Foundation.
503 + *
504 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
505 + */
506 +
507 +#ifndef __YAFFS_CONFIG_H__
508 +#define __YAFFS_CONFIG_H__
509 +
510 +#ifdef YAFFS_OUT_OF_TREE
511 +
512 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
513 +#define CONFIG_YAFFS_FS
514 +#define CONFIG_YAFFS_YAFFS1
515 +#define CONFIG_YAFFS_YAFFS2
516 +
517 +/* These options are independent of each other. Select those that matter. */
518 +
519 +/* Default: Not selected */
520 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
521 +//#define CONFIG_YAFFS_DOES_ECC
522 +
523 +/* Default: Not selected */
524 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
525 +/* CONFIG_YAFFS_DOES_ECC is set */
526 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
527 +
528 +/* Default: Selected */
529 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
530 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
531 +
532 +/* Default: Selected */
533 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
534 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
535 +
536 +/* Default: 10 */
537 +/* Meaning: set the count of blocks to reserve for checkpointing */
538 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
539 +
540 +/*
541 +Older-style on-NAND data format has a "pageStatus" byte to record
542 +chunk/page state. This byte is zeroed when the page is discarded.
543 +Choose this option if you have existing on-NAND data in this format
544 +that you need to continue to support. New data written also uses the
545 +older-style format.
546 +Note: Use of this option generally requires that MTD's oob layout be
547 +adjusted to use the older-style format. See notes on tags formats and
548 +MTD versions.
549 +*/
550 +/* Default: Not selected */
551 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
552 +#define CONFIG_YAFFS_9BYTE_TAGS
553 +
554 +#endif /* YAFFS_OUT_OF_TREE */
555 +
556 +#endif /* __YAFFS_CONFIG_H__ */
557 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.c
558 ===================================================================
559 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
560 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.c 2007-09-21 16:24:08.000000000 +0800
561 @@ -0,0 +1,404 @@
562 +/*
563 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
564 + *
565 + * Copyright (C) 2002-2007 Aleph One Ltd.
566 + * for Toby Churchill Ltd and Brightstar Engineering
567 + *
568 + * Created by Charles Manning <charles@aleph1.co.uk>
569 + *
570 + * This program is free software; you can redistribute it and/or modify
571 + * it under the terms of the GNU General Public License version 2 as
572 + * published by the Free Software Foundation.
573 + */
574 +
575 +const char *yaffs_checkptrw_c_version =
576 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
577 +
578 +
579 +#include "yaffs_checkptrw.h"
580 +
581 +
582 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
583 +{
584 +
585 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
586 +
587 + T(YAFFS_TRACE_CHECKPOINT,
588 + (TSTR("checkpt blocks available = %d" TENDSTR),
589 + blocksAvailable));
590 +
591 +
592 + return (blocksAvailable <= 0) ? 0 : 1;
593 +}
594 +
595 +
596 +static int yaffs_CheckpointErase(yaffs_Device *dev)
597 +{
598 +
599 + int i;
600 +
601 +
602 + if(!dev->eraseBlockInNAND)
603 + return 0;
604 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
605 + dev->internalStartBlock,dev->internalEndBlock));
606 +
607 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
608 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
609 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
610 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
611 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
612 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
613 + dev->nErasedBlocks++;
614 + dev->nFreeChunks += dev->nChunksPerBlock;
615 + }
616 + else {
617 + dev->markNANDBlockBad(dev,i);
618 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
619 + }
620 + }
621 + }
622 +
623 + dev->blocksInCheckpoint = 0;
624 +
625 + return 1;
626 +}
627 +
628 +
629 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
630 +{
631 + int i;
632 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
633 + T(YAFFS_TRACE_CHECKPOINT,
634 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
635 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
636 +
637 + if(dev->checkpointNextBlock >= 0 &&
638 + dev->checkpointNextBlock <= dev->internalEndBlock &&
639 + blocksAvailable > 0){
640 +
641 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
642 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
643 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
644 + dev->checkpointNextBlock = i + 1;
645 + dev->checkpointCurrentBlock = i;
646 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
647 + return;
648 + }
649 + }
650 + }
651 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
652 +
653 + dev->checkpointNextBlock = -1;
654 + dev->checkpointCurrentBlock = -1;
655 +}
656 +
657 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
658 +{
659 + int i;
660 + yaffs_ExtendedTags tags;
661 +
662 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
663 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
664 +
665 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
666 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
667 + int chunk = i * dev->nChunksPerBlock;
668 + int realignedChunk = chunk - dev->chunkOffset;
669 +
670 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
671 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
672 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
673 +
674 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
675 + /* Right kind of block */
676 + dev->checkpointNextBlock = tags.objectId;
677 + dev->checkpointCurrentBlock = i;
678 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
679 + dev->blocksInCheckpoint++;
680 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
681 + return;
682 + }
683 + }
684 +
685 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
686 +
687 + dev->checkpointNextBlock = -1;
688 + dev->checkpointCurrentBlock = -1;
689 +}
690 +
691 +
692 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
693 +{
694 +
695 + /* Got the functions we need? */
696 + if (!dev->writeChunkWithTagsToNAND ||
697 + !dev->readChunkWithTagsFromNAND ||
698 + !dev->eraseBlockInNAND ||
699 + !dev->markNANDBlockBad)
700 + return 0;
701 +
702 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
703 + return 0;
704 +
705 + if(!dev->checkpointBuffer)
706 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
707 + if(!dev->checkpointBuffer)
708 + return 0;
709 +
710 +
711 + dev->checkpointPageSequence = 0;
712 +
713 + dev->checkpointOpenForWrite = forWriting;
714 +
715 + dev->checkpointByteCount = 0;
716 + dev->checkpointSum = 0;
717 + dev->checkpointXor = 0;
718 + dev->checkpointCurrentBlock = -1;
719 + dev->checkpointCurrentChunk = -1;
720 + dev->checkpointNextBlock = dev->internalStartBlock;
721 +
722 + /* Erase all the blocks in the checkpoint area */
723 + if(forWriting){
724 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
725 + dev->checkpointByteOffset = 0;
726 + return yaffs_CheckpointErase(dev);
727 +
728 +
729 + } else {
730 + int i;
731 + /* Set to a value that will kick off a read */
732 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
733 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
734 + * going to be way more than we need */
735 + dev->blocksInCheckpoint = 0;
736 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
737 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
738 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
739 + dev->checkpointBlockList[i] = -1;
740 + }
741 +
742 + return 1;
743 +}
744 +
745 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
746 +{
747 + __u32 compositeSum;
748 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
749 + *sum = compositeSum;
750 + return 1;
751 +}
752 +
753 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
754 +{
755 +
756 + int chunk;
757 + int realignedChunk;
758 +
759 + yaffs_ExtendedTags tags;
760 +
761 + if(dev->checkpointCurrentBlock < 0){
762 + yaffs_CheckpointFindNextErasedBlock(dev);
763 + dev->checkpointCurrentChunk = 0;
764 + }
765 +
766 + if(dev->checkpointCurrentBlock < 0)
767 + return 0;
768 +
769 + tags.chunkDeleted = 0;
770 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
771 + tags.chunkId = dev->checkpointPageSequence + 1;
772 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
773 + tags.byteCount = dev->nDataBytesPerChunk;
774 + if(dev->checkpointCurrentChunk == 0){
775 + /* First chunk we write for the block? Set block state to
776 + checkpoint */
777 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
778 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
779 + dev->blocksInCheckpoint++;
780 + }
781 +
782 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
783 +
784 +
785 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
786 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
787 +
788 + realignedChunk = chunk - dev->chunkOffset;
789 +
790 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
791 + dev->checkpointByteOffset = 0;
792 + dev->checkpointPageSequence++;
793 + dev->checkpointCurrentChunk++;
794 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
795 + dev->checkpointCurrentChunk = 0;
796 + dev->checkpointCurrentBlock = -1;
797 + }
798 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
799 +
800 + return 1;
801 +}
802 +
803 +
804 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
805 +{
806 + int i=0;
807 + int ok = 1;
808 +
809 +
810 + __u8 * dataBytes = (__u8 *)data;
811 +
812 +
813 +
814 + if(!dev->checkpointBuffer)
815 + return 0;
816 +
817 + if(!dev->checkpointOpenForWrite)
818 + return -1;
819 +
820 + while(i < nBytes && ok) {
821 +
822 +
823 +
824 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
825 + dev->checkpointSum += *dataBytes;
826 + dev->checkpointXor ^= *dataBytes;
827 +
828 + dev->checkpointByteOffset++;
829 + i++;
830 + dataBytes++;
831 + dev->checkpointByteCount++;
832 +
833 +
834 + if(dev->checkpointByteOffset < 0 ||
835 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
836 + ok = yaffs_CheckpointFlushBuffer(dev);
837 +
838 + }
839 +
840 + return i;
841 +}
842 +
843 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
844 +{
845 + int i=0;
846 + int ok = 1;
847 + yaffs_ExtendedTags tags;
848 +
849 +
850 + int chunk;
851 + int realignedChunk;
852 +
853 + __u8 *dataBytes = (__u8 *)data;
854 +
855 + if(!dev->checkpointBuffer)
856 + return 0;
857 +
858 + if(dev->checkpointOpenForWrite)
859 + return -1;
860 +
861 + while(i < nBytes && ok) {
862 +
863 +
864 + if(dev->checkpointByteOffset < 0 ||
865 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
866 +
867 + if(dev->checkpointCurrentBlock < 0){
868 + yaffs_CheckpointFindNextCheckpointBlock(dev);
869 + dev->checkpointCurrentChunk = 0;
870 + }
871 +
872 + if(dev->checkpointCurrentBlock < 0)
873 + ok = 0;
874 + else {
875 +
876 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
877 + dev->checkpointCurrentChunk;
878 +
879 + realignedChunk = chunk - dev->chunkOffset;
880 +
881 + /* read in the next chunk */
882 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
883 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
884 + dev->checkpointBuffer,
885 + &tags);
886 +
887 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
888 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
889 + ok = 0;
890 +
891 + dev->checkpointByteOffset = 0;
892 + dev->checkpointPageSequence++;
893 + dev->checkpointCurrentChunk++;
894 +
895 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
896 + dev->checkpointCurrentBlock = -1;
897 + }
898 + }
899 +
900 + if(ok){
901 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
902 + dev->checkpointSum += *dataBytes;
903 + dev->checkpointXor ^= *dataBytes;
904 + dev->checkpointByteOffset++;
905 + i++;
906 + dataBytes++;
907 + dev->checkpointByteCount++;
908 + }
909 + }
910 +
911 + return i;
912 +}
913 +
914 +int yaffs_CheckpointClose(yaffs_Device *dev)
915 +{
916 +
917 + if(dev->checkpointOpenForWrite){
918 + if(dev->checkpointByteOffset != 0)
919 + yaffs_CheckpointFlushBuffer(dev);
920 + } else {
921 + int i;
922 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
923 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
924 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
925 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
926 + else {
927 + // Todo this looks odd...
928 + }
929 + }
930 + YFREE(dev->checkpointBlockList);
931 + dev->checkpointBlockList = NULL;
932 + }
933 +
934 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
935 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
936 +
937 +
938 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
939 + dev->checkpointByteCount));
940 +
941 + if(dev->checkpointBuffer){
942 + /* free the buffer */
943 + YFREE(dev->checkpointBuffer);
944 + dev->checkpointBuffer = NULL;
945 + return 1;
946 + }
947 + else
948 + return 0;
949 +
950 +}
951 +
952 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
953 +{
954 + /* Erase the first checksum block */
955 +
956 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
957 +
958 + if(!yaffs_CheckpointSpaceOk(dev))
959 + return 0;
960 +
961 + return yaffs_CheckpointErase(dev);
962 +}
963 +
964 +
965 +
966 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.h
967 ===================================================================
968 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
969 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.h 2007-09-21 16:24:08.000000000 +0800
970 @@ -0,0 +1,35 @@
971 +/*
972 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
973 + *
974 + * Copyright (C) 2002-2007 Aleph One Ltd.
975 + * for Toby Churchill Ltd and Brightstar Engineering
976 + *
977 + * Created by Charles Manning <charles@aleph1.co.uk>
978 + *
979 + * This program is free software; you can redistribute it and/or modify
980 + * it under the terms of the GNU Lesser General Public License version 2.1 as
981 + * published by the Free Software Foundation.
982 + *
983 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
984 + */
985 +
986 +#ifndef __YAFFS_CHECKPTRW_H__
987 +#define __YAFFS_CHECKPTRW_H__
988 +
989 +#include "yaffs_guts.h"
990 +
991 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
992 +
993 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
994 +
995 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
996 +
997 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
998 +
999 +int yaffs_CheckpointClose(yaffs_Device *dev);
1000 +
1001 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
1002 +
1003 +
1004 +#endif
1005 +
1006 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.c
1007 ===================================================================
1008 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1009 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.c 2007-09-21 16:24:08.000000000 +0800
1010 @@ -0,0 +1,331 @@
1011 +/*
1012 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1013 + *
1014 + * Copyright (C) 2002-2007 Aleph One Ltd.
1015 + * for Toby Churchill Ltd and Brightstar Engineering
1016 + *
1017 + * Created by Charles Manning <charles@aleph1.co.uk>
1018 + *
1019 + * This program is free software; you can redistribute it and/or modify
1020 + * it under the terms of the GNU General Public License version 2 as
1021 + * published by the Free Software Foundation.
1022 + */
1023 +
1024 +/*
1025 + * This code implements the ECC algorithm used in SmartMedia.
1026 + *
1027 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1028 + * The two unused bit are set to 1.
1029 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1030 + * blocks are used on a 512-byte NAND page.
1031 + *
1032 + */
1033 +
1034 +/* Table generated by gen-ecc.c
1035 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1036 + * for each byte of data. These are instead provided in a table in bits7..2.
1037 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1038 + * this bytes influence on the line parity.
1039 + */
1040 +
1041 +const char *yaffs_ecc_c_version =
1042 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1043 +
1044 +#include "yportenv.h"
1045 +
1046 +#include "yaffs_ecc.h"
1047 +
1048 +static const unsigned char column_parity_table[] = {
1049 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1050 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1051 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1052 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1053 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1054 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1055 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1056 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1057 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1058 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1059 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1060 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1061 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1062 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1063 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1064 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1065 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1066 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1067 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1068 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1069 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1070 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1071 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1072 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1073 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1074 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1075 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1076 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1077 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1078 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1079 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1080 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1081 +};
1082 +
1083 +/* Count the bits in an unsigned char or a U32 */
1084 +
1085 +static int yaffs_CountBits(unsigned char x)
1086 +{
1087 + int r = 0;
1088 + while (x) {
1089 + if (x & 1)
1090 + r++;
1091 + x >>= 1;
1092 + }
1093 + return r;
1094 +}
1095 +
1096 +static int yaffs_CountBits32(unsigned x)
1097 +{
1098 + int r = 0;
1099 + while (x) {
1100 + if (x & 1)
1101 + r++;
1102 + x >>= 1;
1103 + }
1104 + return r;
1105 +}
1106 +
1107 +/* Calculate the ECC for a 256-byte block of data */
1108 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1109 +{
1110 + unsigned int i;
1111 +
1112 + unsigned char col_parity = 0;
1113 + unsigned char line_parity = 0;
1114 + unsigned char line_parity_prime = 0;
1115 + unsigned char t;
1116 + unsigned char b;
1117 +
1118 + for (i = 0; i < 256; i++) {
1119 + b = column_parity_table[*data++];
1120 + col_parity ^= b;
1121 +
1122 + if (b & 0x01) // odd number of bits in the byte
1123 + {
1124 + line_parity ^= i;
1125 + line_parity_prime ^= ~i;
1126 + }
1127 +
1128 + }
1129 +
1130 + ecc[2] = (~col_parity) | 0x03;
1131 +
1132 + t = 0;
1133 + if (line_parity & 0x80)
1134 + t |= 0x80;
1135 + if (line_parity_prime & 0x80)
1136 + t |= 0x40;
1137 + if (line_parity & 0x40)
1138 + t |= 0x20;
1139 + if (line_parity_prime & 0x40)
1140 + t |= 0x10;
1141 + if (line_parity & 0x20)
1142 + t |= 0x08;
1143 + if (line_parity_prime & 0x20)
1144 + t |= 0x04;
1145 + if (line_parity & 0x10)
1146 + t |= 0x02;
1147 + if (line_parity_prime & 0x10)
1148 + t |= 0x01;
1149 + ecc[1] = ~t;
1150 +
1151 + t = 0;
1152 + if (line_parity & 0x08)
1153 + t |= 0x80;
1154 + if (line_parity_prime & 0x08)
1155 + t |= 0x40;
1156 + if (line_parity & 0x04)
1157 + t |= 0x20;
1158 + if (line_parity_prime & 0x04)
1159 + t |= 0x10;
1160 + if (line_parity & 0x02)
1161 + t |= 0x08;
1162 + if (line_parity_prime & 0x02)
1163 + t |= 0x04;
1164 + if (line_parity & 0x01)
1165 + t |= 0x02;
1166 + if (line_parity_prime & 0x01)
1167 + t |= 0x01;
1168 + ecc[0] = ~t;
1169 +
1170 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1171 + // Swap the bytes into the wrong order
1172 + t = ecc[0];
1173 + ecc[0] = ecc[1];
1174 + ecc[1] = t;
1175 +#endif
1176 +}
1177 +
1178 +
1179 +/* Correct the ECC on a 256 byte block of data */
1180 +
1181 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1182 + const unsigned char *test_ecc)
1183 +{
1184 + unsigned char d0, d1, d2; /* deltas */
1185 +
1186 + d0 = read_ecc[0] ^ test_ecc[0];
1187 + d1 = read_ecc[1] ^ test_ecc[1];
1188 + d2 = read_ecc[2] ^ test_ecc[2];
1189 +
1190 + if ((d0 | d1 | d2) == 0)
1191 + return 0; /* no error */
1192 +
1193 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1194 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1195 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1196 + /* Single bit (recoverable) error in data */
1197 +
1198 + unsigned byte;
1199 + unsigned bit;
1200 +
1201 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1202 + // swap the bytes to correct for the wrong order
1203 + unsigned char t;
1204 +
1205 + t = d0;
1206 + d0 = d1;
1207 + d1 = t;
1208 +#endif
1209 +
1210 + bit = byte = 0;
1211 +
1212 + if (d1 & 0x80)
1213 + byte |= 0x80;
1214 + if (d1 & 0x20)
1215 + byte |= 0x40;
1216 + if (d1 & 0x08)
1217 + byte |= 0x20;
1218 + if (d1 & 0x02)
1219 + byte |= 0x10;
1220 + if (d0 & 0x80)
1221 + byte |= 0x08;
1222 + if (d0 & 0x20)
1223 + byte |= 0x04;
1224 + if (d0 & 0x08)
1225 + byte |= 0x02;
1226 + if (d0 & 0x02)
1227 + byte |= 0x01;
1228 +
1229 + if (d2 & 0x80)
1230 + bit |= 0x04;
1231 + if (d2 & 0x20)
1232 + bit |= 0x02;
1233 + if (d2 & 0x08)
1234 + bit |= 0x01;
1235 +
1236 + data[byte] ^= (1 << bit);
1237 +
1238 + return 1; /* Corrected the error */
1239 + }
1240 +
1241 + if ((yaffs_CountBits(d0) +
1242 + yaffs_CountBits(d1) +
1243 + yaffs_CountBits(d2)) == 1) {
1244 + /* Reccoverable error in ecc */
1245 +
1246 + read_ecc[0] = test_ecc[0];
1247 + read_ecc[1] = test_ecc[1];
1248 + read_ecc[2] = test_ecc[2];
1249 +
1250 + return 1; /* Corrected the error */
1251 + }
1252 +
1253 + /* Unrecoverable error */
1254 +
1255 + return -1;
1256 +
1257 +}
1258 +
1259 +
1260 +/*
1261 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1262 + */
1263 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1264 + yaffs_ECCOther * eccOther)
1265 +{
1266 + unsigned int i;
1267 +
1268 + unsigned char col_parity = 0;
1269 + unsigned line_parity = 0;
1270 + unsigned line_parity_prime = 0;
1271 + unsigned char b;
1272 +
1273 + for (i = 0; i < nBytes; i++) {
1274 + b = column_parity_table[*data++];
1275 + col_parity ^= b;
1276 +
1277 + if (b & 0x01) {
1278 + /* odd number of bits in the byte */
1279 + line_parity ^= i;
1280 + line_parity_prime ^= ~i;
1281 + }
1282 +
1283 + }
1284 +
1285 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1286 + eccOther->lineParity = line_parity;
1287 + eccOther->lineParityPrime = line_parity_prime;
1288 +}
1289 +
1290 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1291 + yaffs_ECCOther * read_ecc,
1292 + const yaffs_ECCOther * test_ecc)
1293 +{
1294 + unsigned char cDelta; /* column parity delta */
1295 + unsigned lDelta; /* line parity delta */
1296 + unsigned lDeltaPrime; /* line parity delta */
1297 + unsigned bit;
1298 +
1299 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1300 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1301 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1302 +
1303 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1304 + return 0; /* no error */
1305 +
1306 + if (lDelta == ~lDeltaPrime &&
1307 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1308 + {
1309 + /* Single bit (recoverable) error in data */
1310 +
1311 + bit = 0;
1312 +
1313 + if (cDelta & 0x20)
1314 + bit |= 0x04;
1315 + if (cDelta & 0x08)
1316 + bit |= 0x02;
1317 + if (cDelta & 0x02)
1318 + bit |= 0x01;
1319 +
1320 + if(lDelta >= nBytes)
1321 + return -1;
1322 +
1323 + data[lDelta] ^= (1 << bit);
1324 +
1325 + return 1; /* corrected */
1326 + }
1327 +
1328 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1329 + yaffs_CountBits(cDelta)) == 1) {
1330 + /* Reccoverable error in ecc */
1331 +
1332 + *read_ecc = *test_ecc;
1333 + return 1; /* corrected */
1334 + }
1335 +
1336 + /* Unrecoverable error */
1337 +
1338 + return -1;
1339 +
1340 +}
1341 +
1342 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.h
1343 ===================================================================
1344 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1345 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.h 2007-09-21 16:24:08.000000000 +0800
1346 @@ -0,0 +1,44 @@
1347 +/*
1348 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1349 + *
1350 + * Copyright (C) 2002-2007 Aleph One Ltd.
1351 + * for Toby Churchill Ltd and Brightstar Engineering
1352 + *
1353 + * Created by Charles Manning <charles@aleph1.co.uk>
1354 + *
1355 + * This program is free software; you can redistribute it and/or modify
1356 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1357 + * published by the Free Software Foundation.
1358 + *
1359 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1360 + */
1361 +
1362 + /*
1363 + * This code implements the ECC algorithm used in SmartMedia.
1364 + *
1365 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1366 + * The two unused bit are set to 1.
1367 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1368 + * blocks are used on a 512-byte NAND page.
1369 + *
1370 + */
1371 +
1372 +#ifndef __YAFFS_ECC_H__
1373 +#define __YAFFS_ECC_H__
1374 +
1375 +typedef struct {
1376 + unsigned char colParity;
1377 + unsigned lineParity;
1378 + unsigned lineParityPrime;
1379 +} yaffs_ECCOther;
1380 +
1381 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1382 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1383 + const unsigned char *test_ecc);
1384 +
1385 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1386 + yaffs_ECCOther * ecc);
1387 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1388 + yaffs_ECCOther * read_ecc,
1389 + const yaffs_ECCOther * test_ecc);
1390 +#endif
1391 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_fs.c
1392 ===================================================================
1393 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1394 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_fs.c 2007-09-21 16:24:08.000000000 +0800
1395 @@ -0,0 +1,2278 @@
1396 +/*
1397 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1398 + *
1399 + * Copyright (C) 2002-2007 Aleph One Ltd.
1400 + * for Toby Churchill Ltd and Brightstar Engineering
1401 + *
1402 + * Created by Charles Manning <charles@aleph1.co.uk>
1403 + * Acknowledgements:
1404 + * Luc van OostenRyck for numerous patches.
1405 + * Nick Bane for numerous patches.
1406 + * Nick Bane for 2.5/2.6 integration.
1407 + * Andras Toth for mknod rdev issue.
1408 + * Michael Fischer for finding the problem with inode inconsistency.
1409 + * Some code bodily lifted from JFFS
1410 + *
1411 + * This program is free software; you can redistribute it and/or modify
1412 + * it under the terms of the GNU General Public License version 2 as
1413 + * published by the Free Software Foundation.
1414 + */
1415 +
1416 +/*
1417 + *
1418 + * This is the file system front-end to YAFFS that hooks it up to
1419 + * the VFS.
1420 + *
1421 + * Special notes:
1422 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1423 + * this superblock
1424 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1425 + * superblock
1426 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1427 + */
1428 +
1429 +const char *yaffs_fs_c_version =
1430 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1431 +extern const char *yaffs_guts_c_version;
1432 +
1433 +#include <linux/version.h>
1434 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1435 +#include <linux/config.h>
1436 +#endif
1437 +#include <linux/kernel.h>
1438 +#include <linux/module.h>
1439 +#include <linux/slab.h>
1440 +#include <linux/init.h>
1441 +#include <linux/list.h>
1442 +#include <linux/fs.h>
1443 +#include <linux/proc_fs.h>
1444 +#include <linux/smp_lock.h>
1445 +#include <linux/pagemap.h>
1446 +#include <linux/mtd/mtd.h>
1447 +#include <linux/interrupt.h>
1448 +#include <linux/string.h>
1449 +#include <linux/ctype.h>
1450 +
1451 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1452 +
1453 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1454 +#include <asm/statfs.h>
1455 +#define UnlockPage(p) unlock_page(p)
1456 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1457 +
1458 +/* FIXME: use sb->s_id instead ? */
1459 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1460 +
1461 +#else
1462 +
1463 +#include <linux/locks.h>
1464 +#define BDEVNAME_SIZE 0
1465 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1466 +
1467 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1468 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1469 +#define __user
1470 +#endif
1471 +
1472 +#endif
1473 +
1474 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1475 +#define WRITE_SIZE_STR "writesize"
1476 +#define WRITE_SIZE(mtd) (mtd)->writesize
1477 +#else
1478 +#define WRITE_SIZE_STR "oobblock"
1479 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1480 +#endif
1481 +
1482 +#include <asm/uaccess.h>
1483 +
1484 +#include "yportenv.h"
1485 +#include "yaffs_guts.h"
1486 +
1487 +#include <linux/mtd/mtd.h>
1488 +#include "yaffs_mtdif.h"
1489 +#include "yaffs_mtdif1.h"
1490 +#include "yaffs_mtdif2.h"
1491 +
1492 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1493 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1494 +
1495 +/* Module Parameters */
1496 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1497 +module_param(yaffs_traceMask,uint,0644);
1498 +module_param(yaffs_wr_attempts,uint,0644);
1499 +#else
1500 +MODULE_PARM(yaffs_traceMask,"i");
1501 +MODULE_PARM(yaffs_wr_attempts,"i");
1502 +#endif
1503 +
1504 +/*#define T(x) printk x */
1505 +
1506 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1507 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1508 +#else
1509 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1510 +#endif
1511 +
1512 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1513 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1514 +
1515 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1516 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1517 +#else
1518 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1519 +#endif
1520 +
1521 +static void yaffs_put_super(struct super_block *sb);
1522 +
1523 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1524 + loff_t * pos);
1525 +
1526 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1527 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1528 +#else
1529 +static int yaffs_file_flush(struct file *file);
1530 +#endif
1531 +
1532 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1533 + int datasync);
1534 +
1535 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1536 +
1537 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1538 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1539 + struct nameidata *n);
1540 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1541 + struct nameidata *n);
1542 +#else
1543 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1544 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1545 +#endif
1546 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1547 + struct dentry *dentry);
1548 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1549 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1550 + const char *symname);
1551 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1552 +
1553 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1554 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1555 + dev_t dev);
1556 +#else
1557 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1558 + int dev);
1559 +#endif
1560 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1561 + struct inode *new_dir, struct dentry *new_dentry);
1562 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1563 +
1564 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1565 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1566 +static void yaffs_write_super(struct super_block *sb);
1567 +#else
1568 +static int yaffs_sync_fs(struct super_block *sb);
1569 +static int yaffs_write_super(struct super_block *sb);
1570 +#endif
1571 +
1572 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1573 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1574 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1575 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1576 +#else
1577 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1578 +#endif
1579 +static void yaffs_read_inode(struct inode *inode);
1580 +
1581 +static void yaffs_put_inode(struct inode *inode);
1582 +static void yaffs_delete_inode(struct inode *);
1583 +static void yaffs_clear_inode(struct inode *);
1584 +
1585 +static int yaffs_readpage(struct file *file, struct page *page);
1586 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1587 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1588 +#else
1589 +static int yaffs_writepage(struct page *page);
1590 +#endif
1591 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1592 + unsigned offset, unsigned to);
1593 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1594 + unsigned to);
1595 +
1596 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1597 + int buflen);
1598 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1599 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1600 +#else
1601 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1602 +#endif
1603 +
1604 +static struct address_space_operations yaffs_file_address_operations = {
1605 + .readpage = yaffs_readpage,
1606 + .writepage = yaffs_writepage,
1607 + .prepare_write = yaffs_prepare_write,
1608 + .commit_write = yaffs_commit_write,
1609 +};
1610 +
1611 +static struct file_operations yaffs_file_operations = {
1612 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1613 + .read = do_sync_read,
1614 + .write = do_sync_write,
1615 + .aio_read = generic_file_aio_read,
1616 + .aio_write = generic_file_aio_write,
1617 +#else
1618 + .read = generic_file_read,
1619 + .write = generic_file_write,
1620 +#endif
1621 + .mmap = generic_file_mmap,
1622 + .flush = yaffs_file_flush,
1623 + .fsync = yaffs_sync_object,
1624 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1625 + .sendfile = generic_file_sendfile,
1626 +#endif
1627 +
1628 +};
1629 +
1630 +static struct inode_operations yaffs_file_inode_operations = {
1631 + .setattr = yaffs_setattr,
1632 +};
1633 +
1634 +static struct inode_operations yaffs_symlink_inode_operations = {
1635 + .readlink = yaffs_readlink,
1636 + .follow_link = yaffs_follow_link,
1637 + .setattr = yaffs_setattr,
1638 +};
1639 +
1640 +static struct inode_operations yaffs_dir_inode_operations = {
1641 + .create = yaffs_create,
1642 + .lookup = yaffs_lookup,
1643 + .link = yaffs_link,
1644 + .unlink = yaffs_unlink,
1645 + .symlink = yaffs_symlink,
1646 + .mkdir = yaffs_mkdir,
1647 + .rmdir = yaffs_unlink,
1648 + .mknod = yaffs_mknod,
1649 + .rename = yaffs_rename,
1650 + .setattr = yaffs_setattr,
1651 +};
1652 +
1653 +static struct file_operations yaffs_dir_operations = {
1654 + .read = generic_read_dir,
1655 + .readdir = yaffs_readdir,
1656 + .fsync = yaffs_sync_object,
1657 +};
1658 +
1659 +static struct super_operations yaffs_super_ops = {
1660 + .statfs = yaffs_statfs,
1661 + .read_inode = yaffs_read_inode,
1662 + .put_inode = yaffs_put_inode,
1663 + .put_super = yaffs_put_super,
1664 + .delete_inode = yaffs_delete_inode,
1665 + .clear_inode = yaffs_clear_inode,
1666 + .sync_fs = yaffs_sync_fs,
1667 + .write_super = yaffs_write_super,
1668 +};
1669 +
1670 +static void yaffs_GrossLock(yaffs_Device * dev)
1671 +{
1672 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1673 +
1674 + down(&dev->grossLock);
1675 +}
1676 +
1677 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1678 +{
1679 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1680 + up(&dev->grossLock);
1681 +
1682 +}
1683 +
1684 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1685 + int buflen)
1686 +{
1687 + unsigned char *alias;
1688 + int ret;
1689 +
1690 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1691 +
1692 + yaffs_GrossLock(dev);
1693 +
1694 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1695 +
1696 + yaffs_GrossUnlock(dev);
1697 +
1698 + if (!alias)
1699 + return -ENOMEM;
1700 +
1701 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1702 + kfree(alias);
1703 + return ret;
1704 +}
1705 +
1706 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1707 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1708 +#else
1709 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1710 +#endif
1711 +{
1712 + unsigned char *alias;
1713 + int ret;
1714 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1715 +
1716 + yaffs_GrossLock(dev);
1717 +
1718 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1719 +
1720 + yaffs_GrossUnlock(dev);
1721 +
1722 + if (!alias)
1723 + {
1724 + ret = -ENOMEM;
1725 + goto out;
1726 + }
1727 +
1728 + ret = vfs_follow_link(nd, alias);
1729 + kfree(alias);
1730 +out:
1731 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1732 + return ERR_PTR (ret);
1733 +#else
1734 + return ret;
1735 +#endif
1736 +}
1737 +
1738 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1739 + yaffs_Object * obj);
1740 +
1741 +/*
1742 + * Lookup is used to find objects in the fs
1743 + */
1744 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1745 +
1746 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1747 + struct nameidata *n)
1748 +#else
1749 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1750 +#endif
1751 +{
1752 + yaffs_Object *obj;
1753 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1754 +
1755 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1756 +
1757 + yaffs_GrossLock(dev);
1758 +
1759 + T(YAFFS_TRACE_OS,
1760 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1761 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1762 +
1763 + obj =
1764 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1765 + dentry->d_name.name);
1766 +
1767 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1768 +
1769 + /* Can't hold gross lock when calling yaffs_get_inode() */
1770 + yaffs_GrossUnlock(dev);
1771 +
1772 + if (obj) {
1773 + T(YAFFS_TRACE_OS,
1774 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1775 +
1776 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1777 +
1778 + if (inode) {
1779 + T(YAFFS_TRACE_OS,
1780 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1781 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1782 + * d_add even if NULL inode */
1783 +#if 0
1784 + /*dget(dentry); // try to solve directory bug */
1785 + d_add(dentry, inode);
1786 +
1787 + /* return dentry; */
1788 + return NULL;
1789 +#endif
1790 + }
1791 +
1792 + } else {
1793 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1794 +
1795 + }
1796 +
1797 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1798 + * NULL which creates dentry hash */
1799 + d_add(dentry, inode);
1800 +
1801 + return NULL;
1802 + /* return (ERR_PTR(-EIO)); */
1803 +
1804 +}
1805 +
1806 +/* For now put inode is just for debugging
1807 + * Put inode is called when the inode **structure** is put.
1808 + */
1809 +static void yaffs_put_inode(struct inode *inode)
1810 +{
1811 + T(YAFFS_TRACE_OS,
1812 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1813 + atomic_read(&inode->i_count)));
1814 +
1815 +}
1816 +
1817 +/* clear is called to tell the fs to release any per-inode data it holds */
1818 +static void yaffs_clear_inode(struct inode *inode)
1819 +{
1820 + yaffs_Object *obj;
1821 + yaffs_Device *dev;
1822 +
1823 + obj = yaffs_InodeToObject(inode);
1824 +
1825 + T(YAFFS_TRACE_OS,
1826 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1827 + atomic_read(&inode->i_count),
1828 + obj ? "object exists" : "null object"));
1829 +
1830 + if (obj) {
1831 + dev = obj->myDev;
1832 + yaffs_GrossLock(dev);
1833 +
1834 + /* Clear the association between the inode and
1835 + * the yaffs_Object.
1836 + */
1837 + obj->myInode = NULL;
1838 + yaffs_InodeToObjectLV(inode) = NULL;
1839 +
1840 + /* If the object freeing was deferred, then the real
1841 + * free happens now.
1842 + * This should fix the inode inconsistency problem.
1843 + */
1844 +
1845 + yaffs_HandleDeferedFree(obj);
1846 +
1847 + yaffs_GrossUnlock(dev);
1848 + }
1849 +
1850 +}
1851 +
1852 +/* delete is called when the link count is zero and the inode
1853 + * is put (ie. nobody wants to know about it anymore, time to
1854 + * delete the file).
1855 + * NB Must call clear_inode()
1856 + */
1857 +static void yaffs_delete_inode(struct inode *inode)
1858 +{
1859 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1860 + yaffs_Device *dev;
1861 +
1862 + T(YAFFS_TRACE_OS,
1863 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1864 + atomic_read(&inode->i_count),
1865 + obj ? "object exists" : "null object"));
1866 +
1867 + if (obj) {
1868 + dev = obj->myDev;
1869 + yaffs_GrossLock(dev);
1870 + yaffs_DeleteFile(obj);
1871 + yaffs_GrossUnlock(dev);
1872 + }
1873 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1874 + truncate_inode_pages (&inode->i_data, 0);
1875 +#endif
1876 + clear_inode(inode);
1877 +}
1878 +
1879 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1880 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1881 +#else
1882 +static int yaffs_file_flush(struct file *file)
1883 +#endif
1884 +{
1885 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1886 +
1887 + yaffs_Device *dev = obj->myDev;
1888 +
1889 + T(YAFFS_TRACE_OS,
1890 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1891 + obj->dirty ? "dirty" : "clean"));
1892 +
1893 + yaffs_GrossLock(dev);
1894 +
1895 + yaffs_FlushFile(obj, 1);
1896 +
1897 + yaffs_GrossUnlock(dev);
1898 +
1899 + return 0;
1900 +}
1901 +
1902 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1903 +{
1904 + /* Lifted from jffs2 */
1905 +
1906 + yaffs_Object *obj;
1907 + unsigned char *pg_buf;
1908 + int ret;
1909 +
1910 + yaffs_Device *dev;
1911 +
1912 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1913 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1914 + (unsigned)PAGE_CACHE_SIZE));
1915 +
1916 + obj = yaffs_DentryToObject(f->f_dentry);
1917 +
1918 + dev = obj->myDev;
1919 +
1920 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1921 + BUG_ON(!PageLocked(pg));
1922 +#else
1923 + if (!PageLocked(pg))
1924 + PAGE_BUG(pg);
1925 +#endif
1926 +
1927 + pg_buf = kmap(pg);
1928 + /* FIXME: Can kmap fail? */
1929 +
1930 + yaffs_GrossLock(dev);
1931 +
1932 + ret =
1933 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1934 + PAGE_CACHE_SIZE);
1935 +
1936 + yaffs_GrossUnlock(dev);
1937 +
1938 + if (ret >= 0)
1939 + ret = 0;
1940 +
1941 + if (ret) {
1942 + ClearPageUptodate(pg);
1943 + SetPageError(pg);
1944 + } else {
1945 + SetPageUptodate(pg);
1946 + ClearPageError(pg);
1947 + }
1948 +
1949 + flush_dcache_page(pg);
1950 + kunmap(pg);
1951 +
1952 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1953 + return ret;
1954 +}
1955 +
1956 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1957 +{
1958 + int ret = yaffs_readpage_nolock(f, pg);
1959 + UnlockPage(pg);
1960 + return ret;
1961 +}
1962 +
1963 +static int yaffs_readpage(struct file *f, struct page *pg)
1964 +{
1965 + return yaffs_readpage_unlock(f, pg);
1966 +}
1967 +
1968 +/* writepage inspired by/stolen from smbfs */
1969 +
1970 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1971 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1972 +#else
1973 +static int yaffs_writepage(struct page *page)
1974 +#endif
1975 +{
1976 + struct address_space *mapping = page->mapping;
1977 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1978 + struct inode *inode;
1979 + unsigned long end_index;
1980 + char *buffer;
1981 + yaffs_Object *obj;
1982 + int nWritten = 0;
1983 + unsigned nBytes;
1984 +
1985 + if (!mapping)
1986 + BUG();
1987 + inode = mapping->host;
1988 + if (!inode)
1989 + BUG();
1990 +
1991 + if (offset > inode->i_size) {
1992 + T(YAFFS_TRACE_OS,
1993 + (KERN_DEBUG
1994 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1995 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1996 + (unsigned)inode->i_size));
1997 + T(YAFFS_TRACE_OS,
1998 + (KERN_DEBUG " -> don't care!!\n"));
1999 + unlock_page(page);
2000 + return 0;
2001 + }
2002 +
2003 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
2004 +
2005 + /* easy case */
2006 + if (page->index < end_index) {
2007 + nBytes = PAGE_CACHE_SIZE;
2008 + } else {
2009 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
2010 + }
2011 +
2012 + get_page(page);
2013 +
2014 + buffer = kmap(page);
2015 +
2016 + obj = yaffs_InodeToObject(inode);
2017 + yaffs_GrossLock(obj->myDev);
2018 +
2019 + T(YAFFS_TRACE_OS,
2020 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
2021 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2022 + T(YAFFS_TRACE_OS,
2023 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
2024 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2025 +
2026 + nWritten =
2027 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2028 + nBytes, 0);
2029 +
2030 + T(YAFFS_TRACE_OS,
2031 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2032 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2033 +
2034 + yaffs_GrossUnlock(obj->myDev);
2035 +
2036 + kunmap(page);
2037 + SetPageUptodate(page);
2038 + UnlockPage(page);
2039 + put_page(page);
2040 +
2041 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2042 +}
2043 +
2044 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2045 + unsigned offset, unsigned to)
2046 +{
2047 +
2048 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2049 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2050 + return yaffs_readpage_nolock(f, pg);
2051 +
2052 + return 0;
2053 +
2054 +}
2055 +
2056 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2057 + unsigned to)
2058 +{
2059 +
2060 + void *addr = page_address(pg) + offset;
2061 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2062 + int nBytes = to - offset;
2063 + int nWritten;
2064 +
2065 + unsigned spos = pos;
2066 + unsigned saddr = (unsigned)addr;
2067 +
2068 + T(YAFFS_TRACE_OS,
2069 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2070 + spos, nBytes));
2071 +
2072 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2073 +
2074 + if (nWritten != nBytes) {
2075 + T(YAFFS_TRACE_OS,
2076 + (KERN_DEBUG
2077 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2078 + nWritten, nBytes));
2079 + SetPageError(pg);
2080 + ClearPageUptodate(pg);
2081 + } else {
2082 + SetPageUptodate(pg);
2083 + }
2084 +
2085 + T(YAFFS_TRACE_OS,
2086 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2087 + nWritten == nBytes ? 0 : nWritten));
2088 +
2089 + return nWritten == nBytes ? 0 : nWritten;
2090 +
2091 +}
2092 +
2093 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2094 +{
2095 + if (inode && obj) {
2096 +
2097 +
2098 + /* Check mode against the variant type and attempt to repair if broken. */
2099 + __u32 mode = obj->yst_mode;
2100 + switch( obj->variantType ){
2101 + case YAFFS_OBJECT_TYPE_FILE :
2102 + if( ! S_ISREG(mode) ){
2103 + obj->yst_mode &= ~S_IFMT;
2104 + obj->yst_mode |= S_IFREG;
2105 + }
2106 +
2107 + break;
2108 + case YAFFS_OBJECT_TYPE_SYMLINK :
2109 + if( ! S_ISLNK(mode) ){
2110 + obj->yst_mode &= ~S_IFMT;
2111 + obj->yst_mode |= S_IFLNK;
2112 + }
2113 +
2114 + break;
2115 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2116 + if( ! S_ISDIR(mode) ){
2117 + obj->yst_mode &= ~S_IFMT;
2118 + obj->yst_mode |= S_IFDIR;
2119 + }
2120 +
2121 + break;
2122 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2123 + case YAFFS_OBJECT_TYPE_HARDLINK :
2124 + case YAFFS_OBJECT_TYPE_SPECIAL :
2125 + default:
2126 + /* TODO? */
2127 + break;
2128 + }
2129 +
2130 + inode->i_ino = obj->objectId;
2131 + inode->i_mode = obj->yst_mode;
2132 + inode->i_uid = obj->yst_uid;
2133 + inode->i_gid = obj->yst_gid;
2134 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2135 + inode->i_blksize = inode->i_sb->s_blocksize;
2136 +#endif
2137 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2138 +
2139 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2140 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2141 + inode->i_atime.tv_nsec = 0;
2142 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2143 + inode->i_mtime.tv_nsec = 0;
2144 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2145 + inode->i_ctime.tv_nsec = 0;
2146 +#else
2147 + inode->i_rdev = obj->yst_rdev;
2148 + inode->i_atime = obj->yst_atime;
2149 + inode->i_mtime = obj->yst_mtime;
2150 + inode->i_ctime = obj->yst_ctime;
2151 +#endif
2152 + inode->i_size = yaffs_GetObjectFileLength(obj);
2153 + inode->i_blocks = (inode->i_size + 511) >> 9;
2154 +
2155 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2156 +
2157 + T(YAFFS_TRACE_OS,
2158 + (KERN_DEBUG
2159 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2160 + inode->i_mode, inode->i_uid, inode->i_gid,
2161 + (int)inode->i_size, atomic_read(&inode->i_count)));
2162 +
2163 + switch (obj->yst_mode & S_IFMT) {
2164 + default: /* fifo, device or socket */
2165 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2166 + init_special_inode(inode, obj->yst_mode,
2167 + old_decode_dev(obj->yst_rdev));
2168 +#else
2169 + init_special_inode(inode, obj->yst_mode,
2170 + (dev_t) (obj->yst_rdev));
2171 +#endif
2172 + break;
2173 + case S_IFREG: /* file */
2174 + inode->i_op = &yaffs_file_inode_operations;
2175 + inode->i_fop = &yaffs_file_operations;
2176 + inode->i_mapping->a_ops =
2177 + &yaffs_file_address_operations;
2178 + break;
2179 + case S_IFDIR: /* directory */
2180 + inode->i_op = &yaffs_dir_inode_operations;
2181 + inode->i_fop = &yaffs_dir_operations;
2182 + break;
2183 + case S_IFLNK: /* symlink */
2184 + inode->i_op = &yaffs_symlink_inode_operations;
2185 + break;
2186 + }
2187 +
2188 + yaffs_InodeToObjectLV(inode) = obj;
2189 +
2190 + obj->myInode = inode;
2191 +
2192 + } else {
2193 + T(YAFFS_TRACE_OS,
2194 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2195 + }
2196 +
2197 +}
2198 +
2199 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2200 + yaffs_Object * obj)
2201 +{
2202 + struct inode *inode;
2203 +
2204 + if (!sb) {
2205 + T(YAFFS_TRACE_OS,
2206 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2207 + return NULL;
2208 +
2209 + }
2210 +
2211 + if (!obj) {
2212 + T(YAFFS_TRACE_OS,
2213 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2214 + return NULL;
2215 +
2216 + }
2217 +
2218 + T(YAFFS_TRACE_OS,
2219 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2220 +
2221 + inode = iget(sb, obj->objectId);
2222 +
2223 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2224 + /* iget also increments the inode's i_count */
2225 + /* NB You can't be holding grossLock or deadlock will happen! */
2226 +
2227 + return inode;
2228 +}
2229 +
2230 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2231 + loff_t * pos)
2232 +{
2233 + yaffs_Object *obj;
2234 + int nWritten, ipos;
2235 + struct inode *inode;
2236 + yaffs_Device *dev;
2237 +
2238 + obj = yaffs_DentryToObject(f->f_dentry);
2239 +
2240 + dev = obj->myDev;
2241 +
2242 + yaffs_GrossLock(dev);
2243 +
2244 + inode = f->f_dentry->d_inode;
2245 +
2246 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2247 + ipos = inode->i_size;
2248 + } else {
2249 + ipos = *pos;
2250 + }
2251 +
2252 + if (!obj) {
2253 + T(YAFFS_TRACE_OS,
2254 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2255 + } else {
2256 + T(YAFFS_TRACE_OS,
2257 + (KERN_DEBUG
2258 + "yaffs_file_write about to write writing %d bytes"
2259 + "to object %d at %d\n",
2260 + n, obj->objectId, ipos));
2261 + }
2262 +
2263 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2264 +
2265 + T(YAFFS_TRACE_OS,
2266 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2267 + n, nWritten, ipos));
2268 + if (nWritten > 0) {
2269 + ipos += nWritten;
2270 + *pos = ipos;
2271 + if (ipos > inode->i_size) {
2272 + inode->i_size = ipos;
2273 + inode->i_blocks = (ipos + 511) >> 9;
2274 +
2275 + T(YAFFS_TRACE_OS,
2276 + (KERN_DEBUG
2277 + "yaffs_file_write size updated to %d bytes, "
2278 + "%d blocks\n",
2279 + ipos, (int)(inode->i_blocks)));
2280 + }
2281 +
2282 + }
2283 + yaffs_GrossUnlock(dev);
2284 + return nWritten == 0 ? -ENOSPC : nWritten;
2285 +}
2286 +
2287 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2288 +{
2289 + yaffs_Object *obj;
2290 + yaffs_Device *dev;
2291 + struct inode *inode = f->f_dentry->d_inode;
2292 + unsigned long offset, curoffs;
2293 + struct list_head *i;
2294 + yaffs_Object *l;
2295 +
2296 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2297 +
2298 + obj = yaffs_DentryToObject(f->f_dentry);
2299 + dev = obj->myDev;
2300 +
2301 + yaffs_GrossLock(dev);
2302 +
2303 + offset = f->f_pos;
2304 +
2305 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2306 +
2307 + if (offset == 0) {
2308 + T(YAFFS_TRACE_OS,
2309 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2310 + (int)inode->i_ino));
2311 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2312 + < 0) {
2313 + goto out;
2314 + }
2315 + offset++;
2316 + f->f_pos++;
2317 + }
2318 + if (offset == 1) {
2319 + T(YAFFS_TRACE_OS,
2320 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2321 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2322 + if (filldir
2323 + (dirent, "..", 2, offset,
2324 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2325 + goto out;
2326 + }
2327 + offset++;
2328 + f->f_pos++;
2329 + }
2330 +
2331 + curoffs = 1;
2332 +
2333 + /* If the directory has changed since the open or last call to
2334 + readdir, rewind to after the 2 canned entries. */
2335 +
2336 + if (f->f_version != inode->i_version) {
2337 + offset = 2;
2338 + f->f_pos = offset;
2339 + f->f_version = inode->i_version;
2340 + }
2341 +
2342 + list_for_each(i, &obj->variant.directoryVariant.children) {
2343 + curoffs++;
2344 + if (curoffs >= offset) {
2345 + l = list_entry(i, yaffs_Object, siblings);
2346 +
2347 + yaffs_GetObjectName(l, name,
2348 + YAFFS_MAX_NAME_LENGTH + 1);
2349 + T(YAFFS_TRACE_OS,
2350 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2351 + yaffs_GetObjectInode(l)));
2352 +
2353 + if (filldir(dirent,
2354 + name,
2355 + strlen(name),
2356 + offset,
2357 + yaffs_GetObjectInode(l),
2358 + yaffs_GetObjectType(l))
2359 + < 0) {
2360 + goto up_and_out;
2361 + }
2362 +
2363 + offset++;
2364 + f->f_pos++;
2365 + }
2366 + }
2367 +
2368 + up_and_out:
2369 + out:
2370 +
2371 + yaffs_GrossUnlock(dev);
2372 +
2373 + return 0;
2374 +}
2375 +
2376 +/*
2377 + * File creation. Allocate an inode, and we're done..
2378 + */
2379 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2380 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2381 + dev_t rdev)
2382 +#else
2383 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2384 + int rdev)
2385 +#endif
2386 +{
2387 + struct inode *inode;
2388 +
2389 + yaffs_Object *obj = NULL;
2390 + yaffs_Device *dev;
2391 +
2392 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2393 +
2394 + int error = -ENOSPC;
2395 + uid_t uid = current->fsuid;
2396 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2397 +
2398 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2399 + mode |= S_ISGID;
2400 +
2401 + if (parent) {
2402 + T(YAFFS_TRACE_OS,
2403 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2404 + parent->objectId, parent->variantType));
2405 + } else {
2406 + T(YAFFS_TRACE_OS,
2407 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2408 + return -EPERM;
2409 + }
2410 +
2411 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2412 + "mode %x dev %x\n",
2413 + dentry->d_name.name, mode, rdev));
2414 +
2415 + dev = parent->myDev;
2416 +
2417 + yaffs_GrossLock(dev);
2418 +
2419 + switch (mode & S_IFMT) {
2420 + default:
2421 + /* Special (socket, fifo, device...) */
2422 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2423 + "yaffs_mknod: making special\n"));
2424 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2425 + obj =
2426 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2427 + gid, old_encode_dev(rdev));
2428 +#else
2429 + obj =
2430 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2431 + gid, rdev);
2432 +#endif
2433 + break;
2434 + case S_IFREG: /* file */
2435 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2436 + obj =
2437 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2438 + gid);
2439 + break;
2440 + case S_IFDIR: /* directory */
2441 + T(YAFFS_TRACE_OS,
2442 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2443 + obj =
2444 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2445 + uid, gid);
2446 + break;
2447 + case S_IFLNK: /* symlink */
2448 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2449 + obj = NULL; /* Do we ever get here? */
2450 + break;
2451 + }
2452 +
2453 + /* Can not call yaffs_get_inode() with gross lock held */
2454 + yaffs_GrossUnlock(dev);
2455 +
2456 + if (obj) {
2457 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2458 + d_instantiate(dentry, inode);
2459 + T(YAFFS_TRACE_OS,
2460 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2461 + obj->objectId, atomic_read(&inode->i_count)));
2462 + error = 0;
2463 + } else {
2464 + T(YAFFS_TRACE_OS,
2465 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2466 + error = -ENOMEM;
2467 + }
2468 +
2469 + return error;
2470 +}
2471 +
2472 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2473 +{
2474 + int retVal;
2475 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2476 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2477 +#if 0
2478 + /* attempt to fix dir bug - didn't work */
2479 + if (!retVal) {
2480 + dget(dentry);
2481 + }
2482 +#endif
2483 + return retVal;
2484 +}
2485 +
2486 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2487 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2488 + struct nameidata *n)
2489 +#else
2490 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2491 +#endif
2492 +{
2493 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2494 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2495 +}
2496 +
2497 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2498 +{
2499 + int retVal;
2500 +
2501 + yaffs_Device *dev;
2502 +
2503 + T(YAFFS_TRACE_OS,
2504 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2505 + dentry->d_name.name));
2506 +
2507 + dev = yaffs_InodeToObject(dir)->myDev;
2508 +
2509 + yaffs_GrossLock(dev);
2510 +
2511 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2512 +
2513 + if (retVal == YAFFS_OK) {
2514 + dentry->d_inode->i_nlink--;
2515 + dir->i_version++;
2516 + yaffs_GrossUnlock(dev);
2517 + mark_inode_dirty(dentry->d_inode);
2518 + return 0;
2519 + }
2520 + yaffs_GrossUnlock(dev);
2521 + return -ENOTEMPTY;
2522 +}
2523 +
2524 +/*
2525 + * Create a link...
2526 + */
2527 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2528 + struct dentry *dentry)
2529 +{
2530 + struct inode *inode = old_dentry->d_inode;
2531 + yaffs_Object *obj = NULL;
2532 + yaffs_Object *link = NULL;
2533 + yaffs_Device *dev;
2534 +
2535 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2536 +
2537 + obj = yaffs_InodeToObject(inode);
2538 + dev = obj->myDev;
2539 +
2540 + yaffs_GrossLock(dev);
2541 +
2542 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2543 + {
2544 + link =
2545 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2546 + obj);
2547 + }
2548 +
2549 + if (link) {
2550 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2551 + d_instantiate(dentry, old_dentry->d_inode);
2552 + atomic_inc(&old_dentry->d_inode->i_count);
2553 + T(YAFFS_TRACE_OS,
2554 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2555 + old_dentry->d_inode->i_nlink,
2556 + atomic_read(&old_dentry->d_inode->i_count)));
2557 +
2558 + }
2559 +
2560 + yaffs_GrossUnlock(dev);
2561 +
2562 + if (link) {
2563 +
2564 + return 0;
2565 + }
2566 +
2567 + return -EPERM;
2568 +}
2569 +
2570 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2571 + const char *symname)
2572 +{
2573 + yaffs_Object *obj;
2574 + yaffs_Device *dev;
2575 + uid_t uid = current->fsuid;
2576 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2577 +
2578 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2579 +
2580 + dev = yaffs_InodeToObject(dir)->myDev;
2581 + yaffs_GrossLock(dev);
2582 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2583 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2584 + yaffs_GrossUnlock(dev);
2585 +
2586 + if (obj) {
2587 +
2588 + struct inode *inode;
2589 +
2590 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2591 + d_instantiate(dentry, inode);
2592 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2593 + return 0;
2594 + } else {
2595 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2596 +
2597 + }
2598 +
2599 + return -ENOMEM;
2600 +}
2601 +
2602 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2603 + int datasync)
2604 +{
2605 +
2606 + yaffs_Object *obj;
2607 + yaffs_Device *dev;
2608 +
2609 + obj = yaffs_DentryToObject(dentry);
2610 +
2611 + dev = obj->myDev;
2612 +
2613 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2614 + yaffs_GrossLock(dev);
2615 + yaffs_FlushFile(obj, 1);
2616 + yaffs_GrossUnlock(dev);
2617 + return 0;
2618 +}
2619 +
2620 +/*
2621 + * The VFS layer already does all the dentry stuff for rename.
2622 + *
2623 + * NB: POSIX says you can rename an object over an old object of the same name
2624 + */
2625 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2626 + struct inode *new_dir, struct dentry *new_dentry)
2627 +{
2628 + yaffs_Device *dev;
2629 + int retVal = YAFFS_FAIL;
2630 + yaffs_Object *target;
2631 +
2632 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2633 + dev = yaffs_InodeToObject(old_dir)->myDev;
2634 +
2635 + yaffs_GrossLock(dev);
2636 +
2637 + /* Check if the target is an existing directory that is not empty. */
2638 + target =
2639 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2640 + new_dentry->d_name.name);
2641 +
2642 +
2643 +
2644 + if (target &&
2645 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2646 + !list_empty(&target->variant.directoryVariant.children)) {
2647 +
2648 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2649 +
2650 + retVal = YAFFS_FAIL;
2651 + } else {
2652 +
2653 + /* Now does unlinking internally using shadowing mechanism */
2654 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2655 +
2656 + retVal =
2657 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2658 + old_dentry->d_name.name,
2659 + yaffs_InodeToObject(new_dir),
2660 + new_dentry->d_name.name);
2661 +
2662 + }
2663 + yaffs_GrossUnlock(dev);
2664 +
2665 + if (retVal == YAFFS_OK) {
2666 + if(target) {
2667 + new_dentry->d_inode->i_nlink--;
2668 + mark_inode_dirty(new_dentry->d_inode);
2669 + }
2670 +
2671 + return 0;
2672 + } else {
2673 + return -ENOTEMPTY;
2674 + }
2675 +
2676 +}
2677 +
2678 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2679 +{
2680 + struct inode *inode = dentry->d_inode;
2681 + int error;
2682 + yaffs_Device *dev;
2683 +
2684 + T(YAFFS_TRACE_OS,
2685 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2686 + yaffs_InodeToObject(inode)->objectId));
2687 +
2688 + if ((error = inode_change_ok(inode, attr)) == 0) {
2689 +
2690 + dev = yaffs_InodeToObject(inode)->myDev;
2691 + yaffs_GrossLock(dev);
2692 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2693 + YAFFS_OK) {
2694 + error = 0;
2695 + } else {
2696 + error = -EPERM;
2697 + }
2698 + yaffs_GrossUnlock(dev);
2699 + if (!error)
2700 + error = inode_setattr(inode, attr);
2701 + }
2702 + return error;
2703 +}
2704 +
2705 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2706 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2707 +{
2708 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2709 + struct super_block *sb = dentry->d_sb;
2710 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2711 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2712 +{
2713 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2714 +#else
2715 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2716 +{
2717 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2718 +#endif
2719 +
2720 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2721 +
2722 + yaffs_GrossLock(dev);
2723 +
2724 + buf->f_type = YAFFS_MAGIC;
2725 + buf->f_bsize = sb->s_blocksize;
2726 + buf->f_namelen = 255;
2727 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2728 +
2729 + buf->f_blocks =
2730 + (dev->endBlock - dev->startBlock +
2731 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2732 + dev->nDataBytesPerChunk);
2733 + buf->f_bfree =
2734 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2735 + dev->nDataBytesPerChunk);
2736 + } else {
2737 +
2738 + buf->f_blocks =
2739 + (dev->endBlock - dev->startBlock +
2740 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2741 + sb->s_blocksize);
2742 + buf->f_bfree =
2743 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2744 + sb->s_blocksize);
2745 + }
2746 + buf->f_files = 0;
2747 + buf->f_ffree = 0;
2748 + buf->f_bavail = buf->f_bfree;
2749 +
2750 + yaffs_GrossUnlock(dev);
2751 + return 0;
2752 +}
2753 +
2754 +
2755 +/**
2756 +static int yaffs_do_sync_fs(struct super_block *sb)
2757 +{
2758 +
2759 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2760 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2761 +
2762 + if(sb->s_dirt) {
2763 + yaffs_GrossLock(dev);
2764 +
2765 + if(dev)
2766 + yaffs_CheckpointSave(dev);
2767 +
2768 + yaffs_GrossUnlock(dev);
2769 +
2770 + sb->s_dirt = 0;
2771 + }
2772 + return 0;
2773 +}
2774 +**/
2775 +
2776 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2777 +static void yaffs_write_super(struct super_block *sb)
2778 +#else
2779 +static int yaffs_write_super(struct super_block *sb)
2780 +#endif
2781 +{
2782 +
2783 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2784 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2785 + return 0; /* yaffs_do_sync_fs(sb);*/
2786 +#endif
2787 +}
2788 +
2789 +
2790 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2791 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2792 +#else
2793 +static int yaffs_sync_fs(struct super_block *sb)
2794 +#endif
2795 +{
2796 +
2797 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2798 +
2799 + return 0; /* yaffs_do_sync_fs(sb);*/
2800 +
2801 +}
2802 +
2803 +
2804 +static void yaffs_read_inode(struct inode *inode)
2805 +{
2806 + /* NB This is called as a side effect of other functions, but
2807 + * we had to release the lock to prevent deadlocks, so
2808 + * need to lock again.
2809 + */
2810 +
2811 + yaffs_Object *obj;
2812 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2813 +
2814 + T(YAFFS_TRACE_OS,
2815 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2816 +
2817 + yaffs_GrossLock(dev);
2818 +
2819 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2820 +
2821 + yaffs_FillInodeFromObject(inode, obj);
2822 +
2823 + yaffs_GrossUnlock(dev);
2824 +}
2825 +
2826 +static LIST_HEAD(yaffs_dev_list);
2827 +
2828 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2829 +{
2830 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2831 +
2832 + if( *flags & MS_RDONLY ) {
2833 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2834 +
2835 + T(YAFFS_TRACE_OS,
2836 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2837 +
2838 + yaffs_GrossLock(dev);
2839 +
2840 + yaffs_FlushEntireDeviceCache(dev);
2841 +
2842 + yaffs_CheckpointSave(dev);
2843 +
2844 + if (mtd->sync)
2845 + mtd->sync(mtd);
2846 +
2847 + yaffs_GrossUnlock(dev);
2848 + }
2849 + else {
2850 + T(YAFFS_TRACE_OS,
2851 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2852 + }
2853 +
2854 + return 0;
2855 +}
2856 +
2857 +static void yaffs_put_super(struct super_block *sb)
2858 +{
2859 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2860 +
2861 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2862 +
2863 + yaffs_GrossLock(dev);
2864 +
2865 + yaffs_FlushEntireDeviceCache(dev);
2866 +
2867 + yaffs_CheckpointSave(dev);
2868 +
2869 + if (dev->putSuperFunc) {
2870 + dev->putSuperFunc(sb);
2871 + }
2872 +
2873 + yaffs_Deinitialise(dev);
2874 +
2875 + yaffs_GrossUnlock(dev);
2876 +
2877 + /* we assume this is protected by lock_kernel() in mount/umount */
2878 + list_del(&dev->devList);
2879 +
2880 + if(dev->spareBuffer){
2881 + YFREE(dev->spareBuffer);
2882 + dev->spareBuffer = NULL;
2883 + }
2884 +
2885 + kfree(dev);
2886 +}
2887 +
2888 +
2889 +static void yaffs_MTDPutSuper(struct super_block *sb)
2890 +{
2891 +
2892 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2893 +
2894 + if (mtd->sync) {
2895 + mtd->sync(mtd);
2896 + }
2897 +
2898 + put_mtd_device(mtd);
2899 +}
2900 +
2901 +
2902 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2903 +{
2904 + struct super_block *sb = (struct super_block *)vsb;
2905 +
2906 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2907 +// if(sb)
2908 +// sb->s_dirt = 1;
2909 +}
2910 +
2911 +typedef struct {
2912 + int inband_tags;
2913 + int skip_checkpoint_read;
2914 + int skip_checkpoint_write;
2915 + int no_cache;
2916 +} yaffs_options;
2917 +
2918 +#define MAX_OPT_LEN 20
2919 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2920 +{
2921 + char cur_opt[MAX_OPT_LEN+1];
2922 + int p;
2923 + int error = 0;
2924 +
2925 + /* Parse through the options which is a comma seperated list */
2926 +
2927 + while(options_str && *options_str && !error){
2928 + memset(cur_opt,0,MAX_OPT_LEN+1);
2929 + p = 0;
2930 +
2931 + while(*options_str && *options_str != ','){
2932 + if(p < MAX_OPT_LEN){
2933 + cur_opt[p] = *options_str;
2934 + p++;
2935 + }
2936 + options_str++;
2937 + }
2938 +
2939 + if(!strcmp(cur_opt,"inband-tags"))
2940 + options->inband_tags = 1;
2941 + else if(!strcmp(cur_opt,"no-cache"))
2942 + options->no_cache = 1;
2943 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2944 + options->skip_checkpoint_read = 1;
2945 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2946 + options->skip_checkpoint_write = 1;
2947 + else if(!strcmp(cur_opt,"no-checkpoint")){
2948 + options->skip_checkpoint_read = 1;
2949 + options->skip_checkpoint_write = 1;
2950 + } else {
2951 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2952 + error = 1;
2953 + }
2954 +
2955 + }
2956 +
2957 + return error;
2958 +}
2959 +
2960 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2961 + struct super_block *sb,
2962 + void *data, int silent)
2963 +{
2964 + int nBlocks;
2965 + struct inode *inode = NULL;
2966 + struct dentry *root;
2967 + yaffs_Device *dev = 0;
2968 + char devname_buf[BDEVNAME_SIZE + 1];
2969 + struct mtd_info *mtd;
2970 + int err;
2971 + char *data_str = (char *)data;
2972 +
2973 + yaffs_options options;
2974 +
2975 + sb->s_magic = YAFFS_MAGIC;
2976 + sb->s_op = &yaffs_super_ops;
2977 +
2978 + if (!sb)
2979 + printk(KERN_INFO "yaffs: sb is NULL\n");
2980 + else if (!sb->s_dev)
2981 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2982 + else if (!yaffs_devname(sb, devname_buf))
2983 + printk(KERN_INFO "yaffs: devname is NULL\n");
2984 + else
2985 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2986 + sb->s_dev,
2987 + yaffs_devname(sb, devname_buf));
2988 +
2989 + if(!data_str)
2990 + data_str = "";
2991 +
2992 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2993 +
2994 + memset(&options,0,sizeof(options));
2995 +
2996 + if(yaffs_parse_options(&options,data_str)){
2997 + /* Option parsing failed */
2998 + return NULL;
2999 + }
3000 +
3001 +
3002 + sb->s_blocksize = PAGE_CACHE_SIZE;
3003 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3004 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
3005 + T(YAFFS_TRACE_OS,
3006 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
3007 +
3008 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
3009 + T(YAFFS_TRACE_OS,
3010 + ("yaffs: Write verification disabled. All guarantees "
3011 + "null and void\n"));
3012 +#endif
3013 +
3014 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3015 + "\"%s\"\n",
3016 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
3017 + yaffs_devname(sb, devname_buf)));
3018 +
3019 + /* Check it's an mtd device..... */
3020 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
3021 + return NULL; /* This isn't an mtd device */
3022 + }
3023 + /* Get the device */
3024 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3025 + if (!mtd) {
3026 + T(YAFFS_TRACE_ALWAYS,
3027 + ("yaffs: MTD device #%u doesn't appear to exist\n",
3028 + MINOR(sb->s_dev)));
3029 + return NULL;
3030 + }
3031 + /* Check it's NAND */
3032 + if (mtd->type != MTD_NANDFLASH) {
3033 + T(YAFFS_TRACE_ALWAYS,
3034 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3035 + return NULL;
3036 + }
3037 +
3038 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3039 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3040 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3041 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3042 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3043 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3044 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3045 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3046 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3047 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3048 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3049 +
3050 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3051 +
3052 + if (yaffsVersion == 1 &&
3053 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3054 + mtd->writesize >= 2048) {
3055 +#else
3056 + mtd->oobblock >= 2048) {
3057 +#endif
3058 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3059 + yaffsVersion = 2;
3060 + }
3061 +
3062 + /* Added NCB 26/5/2006 for completeness */
3063 + if (yaffsVersion == 2 &&
3064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3065 + mtd->writesize == 512) {
3066 +#else
3067 + mtd->oobblock == 512) {
3068 +#endif
3069 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3070 + yaffsVersion = 1;
3071 + }
3072 +
3073 +#endif
3074 +
3075 + if (yaffsVersion == 2) {
3076 + /* Check for version 2 style functions */
3077 + if (!mtd->erase ||
3078 + !mtd->block_isbad ||
3079 + !mtd->block_markbad ||
3080 + !mtd->read ||
3081 + !mtd->write ||
3082 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3083 + !mtd->read_oob || !mtd->write_oob) {
3084 +#else
3085 + !mtd->write_ecc ||
3086 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3087 +#endif
3088 + T(YAFFS_TRACE_ALWAYS,
3089 + ("yaffs: MTD device does not support required "
3090 + "functions\n"));;
3091 + return NULL;
3092 + }
3093 +
3094 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3095 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3096 +#else
3097 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3098 +#endif
3099 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3100 + T(YAFFS_TRACE_ALWAYS,
3101 + ("yaffs: MTD device does not have the "
3102 + "right page sizes\n"));
3103 + return NULL;
3104 + }
3105 + } else {
3106 + /* Check for V1 style functions */
3107 + if (!mtd->erase ||
3108 + !mtd->read ||
3109 + !mtd->write ||
3110 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3111 + !mtd->read_oob || !mtd->write_oob) {
3112 +#else
3113 + !mtd->write_ecc ||
3114 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3115 +#endif
3116 + T(YAFFS_TRACE_ALWAYS,
3117 + ("yaffs: MTD device does not support required "
3118 + "functions\n"));;
3119 + return NULL;
3120 + }
3121 +
3122 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3123 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3124 + T(YAFFS_TRACE_ALWAYS,
3125 + ("yaffs: MTD device does not support have the "
3126 + "right page sizes\n"));
3127 + return NULL;
3128 + }
3129 + }
3130 +
3131 + /* OK, so if we got here, we have an MTD that's NAND and looks
3132 + * like it has the right capabilities
3133 + * Set the yaffs_Device up for mtd
3134 + */
3135 +
3136 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3137 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3138 +#else
3139 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3140 +#endif
3141 + if (!dev) {
3142 + /* Deep shit could not allocate device structure */
3143 + T(YAFFS_TRACE_ALWAYS,
3144 + ("yaffs_read_super: Failed trying to allocate "
3145 + "yaffs_Device. \n"));
3146 + return NULL;
3147 + }
3148 +
3149 + memset(dev, 0, sizeof(yaffs_Device));
3150 + dev->genericDevice = mtd;
3151 + dev->name = mtd->name;
3152 +
3153 + /* Set up the memory size parameters.... */
3154 +
3155 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3156 + dev->startBlock = 0;
3157 + dev->endBlock = nBlocks - 1;
3158 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3159 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3160 + dev->nReservedBlocks = 5;
3161 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3162 +
3163 + /* ... and the functions. */
3164 + if (yaffsVersion == 2) {
3165 + dev->writeChunkWithTagsToNAND =
3166 + nandmtd2_WriteChunkWithTagsToNAND;
3167 + dev->readChunkWithTagsFromNAND =
3168 + nandmtd2_ReadChunkWithTagsFromNAND;
3169 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3170 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3171 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3172 + dev->isYaffs2 = 1;
3173 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3174 + dev->nDataBytesPerChunk = mtd->writesize;
3175 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3176 +#else
3177 + dev->nDataBytesPerChunk = mtd->oobblock;
3178 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3179 +#endif
3180 + nBlocks = mtd->size / mtd->erasesize;
3181 +
3182 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3183 + dev->startBlock = 0;
3184 + dev->endBlock = nBlocks - 1;
3185 + } else {
3186 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3187 + /* use the MTD interface in yaffs_mtdif1.c */
3188 + dev->writeChunkWithTagsToNAND =
3189 + nandmtd1_WriteChunkWithTagsToNAND;
3190 + dev->readChunkWithTagsFromNAND =
3191 + nandmtd1_ReadChunkWithTagsFromNAND;
3192 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3193 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3194 +#else
3195 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3196 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3197 +#endif
3198 + dev->isYaffs2 = 0;
3199 + }
3200 + /* ... and common functions */
3201 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3202 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3203 +
3204 + dev->putSuperFunc = yaffs_MTDPutSuper;
3205 +
3206 + dev->superBlock = (void *)sb;
3207 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3208 +
3209 +
3210 +#ifndef CONFIG_YAFFS_DOES_ECC
3211 + dev->useNANDECC = 1;
3212 +#endif
3213 +
3214 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3215 + dev->wideTnodesDisabled = 1;
3216 +#endif
3217 +
3218 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3219 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3220 +
3221 + /* we assume this is protected by lock_kernel() in mount/umount */
3222 + list_add_tail(&dev->devList, &yaffs_dev_list);
3223 +
3224 + init_MUTEX(&dev->grossLock);
3225 +
3226 + yaffs_GrossLock(dev);
3227 +
3228 + err = yaffs_GutsInitialise(dev);
3229 +
3230 + T(YAFFS_TRACE_OS,
3231 + ("yaffs_read_super: guts initialised %s\n",
3232 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3233 +
3234 + /* Release lock before yaffs_get_inode() */
3235 + yaffs_GrossUnlock(dev);
3236 +
3237 + /* Create root inode */
3238 + if (err == YAFFS_OK)
3239 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3240 + yaffs_Root(dev));
3241 +
3242 + if (!inode)
3243 + return NULL;
3244 +
3245 + inode->i_op = &yaffs_dir_inode_operations;
3246 + inode->i_fop = &yaffs_dir_operations;
3247 +
3248 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3249 +
3250 + root = d_alloc_root(inode);
3251 +
3252 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3253 +
3254 + if (!root) {
3255 + iput(inode);
3256 + return NULL;
3257 + }
3258 + sb->s_root = root;
3259 +
3260 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3261 + return sb;
3262 +}
3263 +
3264 +
3265 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3266 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3267 + int silent)
3268 +{
3269 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3270 +}
3271 +
3272 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3273 +static int yaffs_read_super(struct file_system_type *fs,
3274 + int flags, const char *dev_name,
3275 + void *data, struct vfsmount *mnt)
3276 +{
3277 +
3278 + return get_sb_bdev(fs, flags, dev_name, data,
3279 + yaffs_internal_read_super_mtd, mnt);
3280 +}
3281 +#else
3282 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3283 + int flags, const char *dev_name,
3284 + void *data)
3285 +{
3286 +
3287 + return get_sb_bdev(fs, flags, dev_name, data,
3288 + yaffs_internal_read_super_mtd);
3289 +}
3290 +#endif
3291 +
3292 +static struct file_system_type yaffs_fs_type = {
3293 + .owner = THIS_MODULE,
3294 + .name = "yaffs",
3295 + .get_sb = yaffs_read_super,
3296 + .kill_sb = kill_block_super,
3297 + .fs_flags = FS_REQUIRES_DEV,
3298 +};
3299 +#else
3300 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3301 + int silent)
3302 +{
3303 + return yaffs_internal_read_super(1, sb, data, silent);
3304 +}
3305 +
3306 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3307 + FS_REQUIRES_DEV);
3308 +#endif
3309 +
3310 +
3311 +#ifdef CONFIG_YAFFS_YAFFS2
3312 +
3313 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3314 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3315 + int silent)
3316 +{
3317 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3318 +}
3319 +
3320 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3321 +static int yaffs2_read_super(struct file_system_type *fs,
3322 + int flags, const char *dev_name, void *data,
3323 + struct vfsmount *mnt)
3324 +{
3325 + return get_sb_bdev(fs, flags, dev_name, data,
3326 + yaffs2_internal_read_super_mtd, mnt);
3327 +}
3328 +#else
3329 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3330 + int flags, const char *dev_name,
3331 + void *data)
3332 +{
3333 +
3334 + return get_sb_bdev(fs, flags, dev_name, data,
3335 + yaffs2_internal_read_super_mtd);
3336 +}
3337 +#endif
3338 +
3339 +static struct file_system_type yaffs2_fs_type = {
3340 + .owner = THIS_MODULE,
3341 + .name = "yaffs2",
3342 + .get_sb = yaffs2_read_super,
3343 + .kill_sb = kill_block_super,
3344 + .fs_flags = FS_REQUIRES_DEV,
3345 +};
3346 +#else
3347 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3348 + void *data, int silent)
3349 +{
3350 + return yaffs_internal_read_super(2, sb, data, silent);
3351 +}
3352 +
3353 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3354 + FS_REQUIRES_DEV);
3355 +#endif
3356 +
3357 +#endif /* CONFIG_YAFFS_YAFFS2 */
3358 +
3359 +static struct proc_dir_entry *my_proc_entry;
3360 +
3361 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3362 +{
3363 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3364 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3365 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3366 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3367 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3368 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3369 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3370 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3371 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3372 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3373 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3374 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3375 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3376 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3377 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3378 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3379 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3380 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3381 + buf +=
3382 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3383 + buf +=
3384 + sprintf(buf, "passiveGCs......... %d\n",
3385 + dev->passiveGarbageCollections);
3386 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3387 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3388 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3389 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3390 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3391 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3392 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3393 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3394 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3395 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3396 + buf +=
3397 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3398 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3399 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3400 +
3401 + return buf;
3402 +}
3403 +
3404 +static int yaffs_proc_read(char *page,
3405 + char **start,
3406 + off_t offset, int count, int *eof, void *data)
3407 +{
3408 + struct list_head *item;
3409 + char *buf = page;
3410 + int step = offset;
3411 + int n = 0;
3412 +
3413 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3414 + * We use 'offset' (*ppos) to indicate where we are in devList.
3415 + * This also assumes the user has posted a read buffer large
3416 + * enough to hold the complete output; but that's life in /proc.
3417 + */
3418 +
3419 + *(int *)start = 1;
3420 +
3421 + /* Print header first */
3422 + if (step == 0) {
3423 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3424 + "\n%s\n%s\n", yaffs_fs_c_version,
3425 + yaffs_guts_c_version);
3426 + }
3427 +
3428 + /* hold lock_kernel while traversing yaffs_dev_list */
3429 + lock_kernel();
3430 +
3431 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3432 + list_for_each(item, &yaffs_dev_list) {
3433 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3434 + if (n < step) {
3435 + n++;
3436 + continue;
3437 + }
3438 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3439 + buf = yaffs_dump_dev(buf, dev);
3440 + break;
3441 + }
3442 + unlock_kernel();
3443 +
3444 + return buf - page < count ? buf - page : count;
3445 +}
3446 +
3447 +/**
3448 + * Set the verbosity of the warnings and error messages.
3449 + *
3450 + * Note that the names can only be a..z or _ with the current code.
3451 + */
3452 +
3453 +static struct {
3454 + char *mask_name;
3455 + unsigned mask_bitfield;
3456 +} mask_flags[] = {
3457 + {"allocate", YAFFS_TRACE_ALLOCATE},
3458 + {"always", YAFFS_TRACE_ALWAYS},
3459 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3460 + {"buffers", YAFFS_TRACE_BUFFERS},
3461 + {"bug", YAFFS_TRACE_BUG},
3462 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3463 + {"deletion", YAFFS_TRACE_DELETION},
3464 + {"erase", YAFFS_TRACE_ERASE},
3465 + {"error", YAFFS_TRACE_ERROR},
3466 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3467 + {"gc", YAFFS_TRACE_GC},
3468 + {"mtd", YAFFS_TRACE_MTD},
3469 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3470 + {"os", YAFFS_TRACE_OS},
3471 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3472 + {"scan", YAFFS_TRACE_SCAN},
3473 + {"tracing", YAFFS_TRACE_TRACING},
3474 +
3475 + {"verify", YAFFS_TRACE_VERIFY},
3476 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3477 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3478 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3479 +
3480 + {"write", YAFFS_TRACE_WRITE},
3481 + {"all", 0xffffffff},
3482 + {"none", 0},
3483 + {NULL, 0},
3484 +};
3485 +
3486 +#define MAX_MASK_NAME_LENGTH 40
3487 +static int yaffs_proc_write(struct file *file, const char *buf,
3488 + unsigned long count, void *data)
3489 +{
3490 + unsigned rg = 0, mask_bitfield;
3491 + char *end;
3492 + char *mask_name;
3493 + char *x;
3494 + char substring[MAX_MASK_NAME_LENGTH+1];
3495 + int i;
3496 + int done = 0;
3497 + int add, len = 0;
3498 + int pos = 0;
3499 +
3500 + rg = yaffs_traceMask;
3501 +
3502 + while (!done && (pos < count)) {
3503 + done = 1;
3504 + while ((pos < count) && isspace(buf[pos])) {
3505 + pos++;
3506 + }
3507 +
3508 + switch (buf[pos]) {
3509 + case '+':
3510 + case '-':
3511 + case '=':
3512 + add = buf[pos];
3513 + pos++;
3514 + break;
3515 +
3516 + default:
3517 + add = ' ';
3518 + break;
3519 + }
3520 + mask_name = NULL;
3521 +
3522 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3523 + if (end > buf + pos) {
3524 + mask_name = "numeral";
3525 + len = end - (buf + pos);
3526 + done = 0;
3527 + } else {
3528 + for(x = buf + pos, i = 0;
3529 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3530 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3531 + substring[i] = *x;
3532 + substring[i] = '\0';
3533 +
3534 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3535 + //len = strlen(mask_flags[i].mask_name);
3536 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3537 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3538 + mask_name = mask_flags[i].mask_name;
3539 + mask_bitfield = mask_flags[i].mask_bitfield;
3540 + done = 0;
3541 + break;
3542 + }
3543 + }
3544 + }
3545 +
3546 + if (mask_name != NULL) {
3547 + // pos += len;
3548 + done = 0;
3549 + switch(add) {
3550 + case '-':
3551 + rg &= ~mask_bitfield;
3552 + break;
3553 + case '+':
3554 + rg |= mask_bitfield;
3555 + break;
3556 + case '=':
3557 + rg = mask_bitfield;
3558 + break;
3559 + default:
3560 + rg |= mask_bitfield;
3561 + break;
3562 + }
3563 + }
3564 + }
3565 +
3566 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3567 +
3568 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3569 +
3570 + if (rg & YAFFS_TRACE_ALWAYS) {
3571 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3572 + char flag;
3573 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3574 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3575 + }
3576 + }
3577 +
3578 + return count;
3579 +}
3580 +
3581 +/* Stuff to handle installation of file systems */
3582 +struct file_system_to_install {
3583 + struct file_system_type *fst;
3584 + int installed;
3585 +};
3586 +
3587 +static struct file_system_to_install fs_to_install[] = {
3588 +//#ifdef CONFIG_YAFFS_YAFFS1
3589 + {&yaffs_fs_type, 0},
3590 +//#endif
3591 +//#ifdef CONFIG_YAFFS_YAFFS2
3592 + {&yaffs2_fs_type, 0},
3593 +//#endif
3594 + {NULL, 0}
3595 +};
3596 +
3597 +static int __init init_yaffs_fs(void)
3598 +{
3599 + int error = 0;
3600 + struct file_system_to_install *fsinst;
3601 +
3602 + T(YAFFS_TRACE_ALWAYS,
3603 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3604 +
3605 + /* Install the proc_fs entry */
3606 + my_proc_entry = create_proc_entry("yaffs",
3607 + S_IRUGO | S_IFREG,
3608 + &proc_root);
3609 +
3610 + if (my_proc_entry) {
3611 + my_proc_entry->write_proc = yaffs_proc_write;
3612 + my_proc_entry->read_proc = yaffs_proc_read;
3613 + my_proc_entry->data = NULL;
3614 + } else {
3615 + return -ENOMEM;
3616 + }
3617 +
3618 + /* Now add the file system entries */
3619 +
3620 + fsinst = fs_to_install;
3621 +
3622 + while (fsinst->fst && !error) {
3623 + error = register_filesystem(fsinst->fst);
3624 + if (!error) {
3625 + fsinst->installed = 1;
3626 + }
3627 + fsinst++;
3628 + }
3629 +
3630 + /* Any errors? uninstall */
3631 + if (error) {
3632 + fsinst = fs_to_install;
3633 +
3634 + while (fsinst->fst) {
3635 + if (fsinst->installed) {
3636 + unregister_filesystem(fsinst->fst);
3637 + fsinst->installed = 0;
3638 + }
3639 + fsinst++;
3640 + }
3641 + }
3642 +
3643 + return error;
3644 +}
3645 +
3646 +static void __exit exit_yaffs_fs(void)
3647 +{
3648 +
3649 + struct file_system_to_install *fsinst;
3650 +
3651 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3652 + " removing. \n"));
3653 +
3654 + remove_proc_entry("yaffs", &proc_root);
3655 +
3656 + fsinst = fs_to_install;
3657 +
3658 + while (fsinst->fst) {
3659 + if (fsinst->installed) {
3660 + unregister_filesystem(fsinst->fst);
3661 + fsinst->installed = 0;
3662 + }
3663 + fsinst++;
3664 + }
3665 +
3666 +}
3667 +
3668 +module_init(init_yaffs_fs)
3669 +module_exit(exit_yaffs_fs)
3670 +
3671 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3672 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3673 +MODULE_LICENSE("GPL");
3674 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.c
3675 ===================================================================
3676 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3677 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.c 2007-09-21 16:24:08.000000000 +0800
3678 @@ -0,0 +1,7469 @@
3679 +/*
3680 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3681 + *
3682 + * Copyright (C) 2002-2007 Aleph One Ltd.
3683 + * for Toby Churchill Ltd and Brightstar Engineering
3684 + *
3685 + * Created by Charles Manning <charles@aleph1.co.uk>
3686 + *
3687 + * This program is free software; you can redistribute it and/or modify
3688 + * it under the terms of the GNU General Public License version 2 as
3689 + * published by the Free Software Foundation.
3690 + */
3691 +
3692 +const char *yaffs_guts_c_version =
3693 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3694 +
3695 +#include "yportenv.h"
3696 +
3697 +#include "yaffsinterface.h"
3698 +#include "yaffs_guts.h"
3699 +#include "yaffs_tagsvalidity.h"
3700 +
3701 +#include "yaffs_tagscompat.h"
3702 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3703 +#include "yaffs_qsort.h"
3704 +#endif
3705 +#include "yaffs_nand.h"
3706 +
3707 +#include "yaffs_checkptrw.h"
3708 +
3709 +#include "yaffs_nand.h"
3710 +#include "yaffs_packedtags2.h"
3711 +
3712 +
3713 +#ifdef CONFIG_YAFFS_WINCE
3714 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3715 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3716 +#endif
3717 +
3718 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3719 +
3720 +#include "yaffs_ecc.h"
3721 +
3722 +
3723 +/* Robustification (if it ever comes about...) */
3724 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3725 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3726 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3727 + const __u8 * data,
3728 + const yaffs_ExtendedTags * tags);
3729 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3730 + const yaffs_ExtendedTags * tags);
3731 +
3732 +/* Other local prototypes */
3733 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3734 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3735 +
3736 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3737 +
3738 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3739 + const __u8 * buffer,
3740 + yaffs_ExtendedTags * tags,
3741 + int useReserve);
3742 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3743 + int chunkInNAND, int inScan);
3744 +
3745 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3746 + yaffs_ObjectType type);
3747 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3748 + yaffs_Object * obj);
3749 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3750 + int force, int isShrink, int shadows);
3751 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3752 +static int yaffs_CheckStructures(void);
3753 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3754 + int chunkOffset, int *limit);
3755 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3756 +
3757 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3758 +
3759 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3760 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3761 + int lineNo);
3762 +
3763 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3764 + int chunkInNAND);
3765 +
3766 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3767 +static void yaffs_DestroyObject(yaffs_Object * obj);
3768 +
3769 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3770 + int chunkInObject);
3771 +
3772 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3773 +
3774 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3775 +
3776 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3777 +
3778 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3779 +
3780 +#ifdef YAFFS_PARANOID
3781 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3782 +#else
3783 +#define yaffs_CheckFileSanity(in)
3784 +#endif
3785 +
3786 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3787 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3788 +
3789 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3790 +
3791 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3792 + yaffs_ExtendedTags * tags);
3793 +
3794 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
3795 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
3796 + yaffs_FileStructure * fStruct,
3797 + __u32 chunkId);
3798 +
3799 +
3800 +/* Function to calculate chunk and offset */
3801 +
3802 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3803 +{
3804 + if(dev->chunkShift){
3805 + /* Easy-peasy power of 2 case */
3806 + *chunk = (__u32)(addr >> dev->chunkShift);
3807 + *offset = (__u32)(addr & dev->chunkMask);
3808 + }
3809 + else if(dev->crumbsPerChunk)
3810 + {
3811 + /* Case where we're using "crumbs" */
3812 + *offset = (__u32)(addr & dev->crumbMask);
3813 + addr >>= dev->crumbShift;
3814 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3815 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3816 + }
3817 + else
3818 + YBUG();
3819 +}
3820 +
3821 +/* Function to return the number of shifts for a power of 2 greater than or equal
3822 + * to the given number
3823 + * Note we don't try to cater for all possible numbers and this does not have to
3824 + * be hellishly efficient.
3825 + */
3826 +
3827 +static __u32 ShiftsGE(__u32 x)
3828 +{
3829 + int extraBits;
3830 + int nShifts;
3831 +
3832 + nShifts = extraBits = 0;
3833 +
3834 + while(x>1){
3835 + if(x & 1) extraBits++;
3836 + x>>=1;
3837 + nShifts++;
3838 + }
3839 +
3840 + if(extraBits)
3841 + nShifts++;
3842 +
3843 + return nShifts;
3844 +}
3845 +
3846 +/* Function to return the number of shifts to get a 1 in bit 0
3847 + */
3848 +
3849 +static __u32 ShiftDiv(__u32 x)
3850 +{
3851 + int nShifts;
3852 +
3853 + nShifts = 0;
3854 +
3855 + if(!x) return 0;
3856 +
3857 + while( !(x&1)){
3858 + x>>=1;
3859 + nShifts++;
3860 + }
3861 +
3862 + return nShifts;
3863 +}
3864 +
3865 +
3866 +
3867 +/*
3868 + * Temporary buffer manipulations.
3869 + */
3870 +
3871 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3872 +{
3873 + int i;
3874 + __u8 *buf = (__u8 *)1;
3875 +
3876 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3877 +
3878 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
3879 + dev->tempBuffer[i].line = 0; /* not in use */
3880 + dev->tempBuffer[i].buffer = buf =
3881 + YMALLOC_DMA(dev->nDataBytesPerChunk);
3882 + }
3883 +
3884 + return buf ? YAFFS_OK : YAFFS_FAIL;
3885 +
3886 +}
3887 +
3888 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3889 +{
3890 + int i, j;
3891 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3892 + if (dev->tempBuffer[i].line == 0) {
3893 + dev->tempBuffer[i].line = lineNo;
3894 + if ((i + 1) > dev->maxTemp) {
3895 + dev->maxTemp = i + 1;
3896 + for (j = 0; j <= i; j++)
3897 + dev->tempBuffer[j].maxLine =
3898 + dev->tempBuffer[j].line;
3899 + }
3900 +
3901 + return dev->tempBuffer[i].buffer;
3902 + }
3903 + }
3904 +
3905 + T(YAFFS_TRACE_BUFFERS,
3906 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3907 + lineNo));
3908 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3909 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3910 + }
3911 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3912 +
3913 + /*
3914 + * If we got here then we have to allocate an unmanaged one
3915 + * This is not good.
3916 + */
3917 +
3918 + dev->unmanagedTempAllocations++;
3919 + return YMALLOC(dev->nDataBytesPerChunk);
3920 +
3921 +}
3922 +
3923 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3924 + int lineNo)
3925 +{
3926 + int i;
3927 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3928 + if (dev->tempBuffer[i].buffer == buffer) {
3929 + dev->tempBuffer[i].line = 0;
3930 + return;
3931 + }
3932 + }
3933 +
3934 + if (buffer) {
3935 + /* assume it is an unmanaged one. */
3936 + T(YAFFS_TRACE_BUFFERS,
3937 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3938 + lineNo));
3939 + YFREE(buffer);
3940 + dev->unmanagedTempDeallocations++;
3941 + }
3942 +
3943 +}
3944 +
3945 +/*
3946 + * Determine if we have a managed buffer.
3947 + */
3948 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3949 +{
3950 + int i;
3951 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3952 + if (dev->tempBuffer[i].buffer == buffer)
3953 + return 1;
3954 +
3955 + }
3956 +
3957 + for (i = 0; i < dev->nShortOpCaches; i++) {
3958 + if( dev->srCache[i].data == buffer )
3959 + return 1;
3960 +
3961 + }
3962 +
3963 + if (buffer == dev->checkpointBuffer)
3964 + return 1;
3965 +
3966 + T(YAFFS_TRACE_ALWAYS,
3967 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3968 + return 0;
3969 +}
3970 +
3971 +
3972 +
3973 +/*
3974 + * Chunk bitmap manipulations
3975 + */
3976 +
3977 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3978 +{
3979 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3980 + T(YAFFS_TRACE_ERROR,
3981 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3982 + blk));
3983 + YBUG();
3984 + }
3985 + return dev->chunkBits +
3986 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3987 +}
3988 +
3989 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3990 +{
3991 + if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
3992 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
3993 + T(YAFFS_TRACE_ERROR,
3994 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
3995 + YBUG();
3996 + }
3997 +}
3998 +
3999 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
4000 +{
4001 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4002 +
4003 + memset(blkBits, 0, dev->chunkBitmapStride);
4004 +}
4005 +
4006 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
4007 +{
4008 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4009 +
4010 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4011 +
4012 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
4013 +}
4014 +
4015 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
4016 +{
4017 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4018 +
4019 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4020 +
4021 + blkBits[chunk / 8] |= (1 << (chunk & 7));
4022 +}
4023 +
4024 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
4025 +{
4026 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4027 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4028 +
4029 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4030 +}
4031 +
4032 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4033 +{
4034 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4035 + int i;
4036 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4037 + if (*blkBits)
4038 + return 1;
4039 + blkBits++;
4040 + }
4041 + return 0;
4042 +}
4043 +
4044 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4045 +{
4046 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4047 + int i;
4048 + int n = 0;
4049 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4050 + __u8 x = *blkBits;
4051 + while(x){
4052 + if(x & 1)
4053 + n++;
4054 + x >>=1;
4055 + }
4056 +
4057 + blkBits++;
4058 + }
4059 + return n;
4060 +}
4061 +
4062 +/*
4063 + * Verification code
4064 + */
4065 +
4066 +static int yaffs_SkipVerification(yaffs_Device *dev)
4067 +{
4068 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4069 +}
4070 +
4071 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4072 +{
4073 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4074 +}
4075 +
4076 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4077 +{
4078 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4079 +}
4080 +
4081 +static const char * blockStateName[] = {
4082 +"Unknown",
4083 +"Needs scanning",
4084 +"Scanning",
4085 +"Empty",
4086 +"Allocating",
4087 +"Full",
4088 +"Dirty",
4089 +"Checkpoint",
4090 +"Collecting",
4091 +"Dead"
4092 +};
4093 +
4094 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4095 +{
4096 + int actuallyUsed;
4097 + int inUse;
4098 +
4099 + if(yaffs_SkipVerification(dev))
4100 + return;
4101 +
4102 + /* Report illegal runtime states */
4103 + if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4104 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
4105 +
4106 + switch(bi->blockState){
4107 + case YAFFS_BLOCK_STATE_UNKNOWN:
4108 + case YAFFS_BLOCK_STATE_SCANNING:
4109 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4110 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
4111 + n,blockStateName[bi->blockState]));
4112 + }
4113 +
4114 + /* Check pages in use and soft deletions are legal */
4115 +
4116 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4117 +
4118 + if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4119 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4120 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4121 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4122 + n,bi->pagesInUse,bi->softDeletions));
4123 +
4124 +
4125 + /* Check chunk bitmap legal */
4126 + inUse = yaffs_CountChunkBits(dev,n);
4127 + if(inUse != bi->pagesInUse)
4128 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4129 + n,bi->pagesInUse,inUse));
4130 +
4131 + /* Check that the sequence number is valid.
4132 + * Ten million is legal, but is very unlikely
4133 + */
4134 + if(dev->isYaffs2 &&
4135 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4136 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
4137 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4138 + n,bi->sequenceNumber));
4139 +
4140 +}
4141 +
4142 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4143 +{
4144 + yaffs_VerifyBlock(dev,bi,n);
4145 +
4146 + /* After collection the block should be in the erased state */
4147 + /* TODO: This will need to change if we do partial gc */
4148 +
4149 + if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
4150 + T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4151 + n,bi->blockState));
4152 + }
4153 +}
4154 +
4155 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4156 +{
4157 + int i;
4158 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4159 + int nIllegalBlockStates = 0;
4160 +
4161 +
4162 + if(yaffs_SkipVerification(dev))
4163 + return;
4164 +
4165 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4166 +
4167 +
4168 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4169 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4170 + yaffs_VerifyBlock(dev,bi,i);
4171 +
4172 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4173 + nBlocksPerState[bi->blockState]++;
4174 + else
4175 + nIllegalBlockStates++;
4176 +
4177 + }
4178 +
4179 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4180 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4181 +
4182 + T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
4183 + if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4184 + T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
4185 +
4186 + for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4187 + T(YAFFS_TRACE_VERIFY,
4188 + (TSTR("%s %d blocks"TENDSTR),
4189 + blockStateName[i],nBlocksPerState[i]));
4190 +
4191 + if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4192 + T(YAFFS_TRACE_VERIFY,
4193 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4194 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4195 +
4196 + if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4197 + T(YAFFS_TRACE_VERIFY,
4198 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4199 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4200 +
4201 + if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4202 + T(YAFFS_TRACE_VERIFY,
4203 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4204 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4205 +
4206 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4207 +
4208 +}
4209 +
4210 +/*
4211 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4212 + * case those tests will not be performed.
4213 + */
4214 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4215 +{
4216 + if(yaffs_SkipVerification(obj->myDev))
4217 + return;
4218 +
4219 + if(!(tags && obj && oh)){
4220 + T(YAFFS_TRACE_VERIFY,
4221 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4222 + (__u32)tags,(__u32)obj,(__u32)oh));
4223 + return;
4224 + }
4225 +
4226 + if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4227 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4228 + T(YAFFS_TRACE_VERIFY,
4229 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4230 + tags->objectId, oh->type));
4231 +
4232 + if(tags->objectId != obj->objectId)
4233 + T(YAFFS_TRACE_VERIFY,
4234 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4235 + tags->objectId, obj->objectId));
4236 +
4237 +
4238 + /*
4239 + * Check that the object's parent ids match if parentCheck requested.
4240 + *
4241 + * Tests do not apply to the root object.
4242 + */
4243 +
4244 + if(parentCheck && tags->objectId > 1 && !obj->parent)
4245 + T(YAFFS_TRACE_VERIFY,
4246 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4247 + tags->objectId, oh->parentObjectId));
4248 +
4249 +
4250 + if(parentCheck && obj->parent &&
4251 + oh->parentObjectId != obj->parent->objectId &&
4252 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4253 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4254 + T(YAFFS_TRACE_VERIFY,
4255 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4256 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4257 +
4258 +
4259 + if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4260 + T(YAFFS_TRACE_VERIFY,
4261 + (TSTR("Obj %d header name is NULL"TENDSTR),
4262 + obj->objectId));
4263 +
4264 + if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4265 + T(YAFFS_TRACE_VERIFY,
4266 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4267 + obj->objectId));
4268 +}
4269 +
4270 +
4271 +
4272 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4273 + __u32 level, int chunkOffset)
4274 +{
4275 + int i;
4276 + yaffs_Device *dev = obj->myDev;
4277 + int ok = 1;
4278 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4279 +
4280 + if (tn) {
4281 + if (level > 0) {
4282 +
4283 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4284 + if (tn->internal[i]) {
4285 + ok = yaffs_VerifyTnodeWorker(obj,
4286 + tn->internal[i],
4287 + level - 1,
4288 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4289 + }
4290 + }
4291 + } else if (level == 0) {
4292 + int i;
4293 + yaffs_ExtendedTags tags;
4294 + __u32 objectId = obj->objectId;
4295 +
4296 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4297 +
4298 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4299 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4300 +
4301 + if(theChunk > 0){
4302 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4303 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4304 + if(tags.objectId != objectId || tags.chunkId != chunkOffset){
4305 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4306 + objectId, chunkOffset, theChunk,
4307 + tags.objectId, tags.chunkId));
4308 + }
4309 + }
4310 + chunkOffset++;
4311 + }
4312 + }
4313 + }
4314 +
4315 + return ok;
4316 +
4317 +}
4318 +
4319 +
4320 +static void yaffs_VerifyFile(yaffs_Object *obj)
4321 +{
4322 + int requiredTallness;
4323 + int actualTallness;
4324 + __u32 lastChunk;
4325 + __u32 x;
4326 + __u32 i;
4327 + int ok;
4328 + yaffs_Device *dev;
4329 + yaffs_ExtendedTags tags;
4330 + yaffs_Tnode *tn;
4331 + __u32 objectId;
4332 +
4333 + if(obj && yaffs_SkipVerification(obj->myDev))
4334 + return;
4335 +
4336 + dev = obj->myDev;
4337 + objectId = obj->objectId;
4338 +
4339 + /* Check file size is consistent with tnode depth */
4340 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4341 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4342 + requiredTallness = 0;
4343 + while (x> 0) {
4344 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4345 + requiredTallness++;
4346 + }
4347 +
4348 + actualTallness = obj->variant.fileVariant.topLevel;
4349 +
4350 + if(requiredTallness > actualTallness )
4351 + T(YAFFS_TRACE_VERIFY,
4352 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4353 + obj->objectId,actualTallness, requiredTallness));
4354 +
4355 +
4356 + /* Check that the chunks in the tnode tree are all correct.
4357 + * We do this by scanning through the tnode tree and
4358 + * checking the tags for every chunk match.
4359 + */
4360 +
4361 + if(yaffs_SkipNANDVerification(dev))
4362 + return;
4363 +
4364 + for(i = 1; i <= lastChunk; i++){
4365 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
4366 +
4367 + if (tn) {
4368 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4369 + if(theChunk > 0){
4370 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4371 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4372 + if(tags.objectId != objectId || tags.chunkId != i){
4373 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4374 + objectId, i, theChunk,
4375 + tags.objectId, tags.chunkId));
4376 + }
4377 + }
4378 + }
4379 +
4380 + }
4381 +
4382 +}
4383 +
4384 +static void yaffs_VerifyDirectory(yaffs_Object *obj)
4385 +{
4386 + if(obj && yaffs_SkipVerification(obj->myDev))
4387 + return;
4388 +
4389 +}
4390 +
4391 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4392 +{
4393 + if(obj && yaffs_SkipVerification(obj->myDev))
4394 + return;
4395 +
4396 + /* Verify sane equivalent object */
4397 +}
4398 +
4399 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4400 +{
4401 + if(obj && yaffs_SkipVerification(obj->myDev))
4402 + return;
4403 +
4404 + /* Verify symlink string */
4405 +}
4406 +
4407 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4408 +{
4409 + if(obj && yaffs_SkipVerification(obj->myDev))
4410 + return;
4411 +}
4412 +
4413 +static void yaffs_VerifyObject(yaffs_Object *obj)
4414 +{
4415 + yaffs_Device *dev;
4416 +
4417 + __u32 chunkMin;
4418 + __u32 chunkMax;
4419 +
4420 + __u32 chunkIdOk;
4421 + __u32 chunkIsLive;
4422 +
4423 + if(!obj)
4424 + return;
4425 +
4426 + dev = obj->myDev;
4427 +
4428 + if(yaffs_SkipVerification(dev))
4429 + return;
4430 +
4431 + /* Check sane object header chunk */
4432 +
4433 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4434 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4435 +
4436 + chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
4437 + chunkIsLive = chunkIdOk &&
4438 + yaffs_CheckChunkBit(dev,
4439 + obj->chunkId / dev->nChunksPerBlock,
4440 + obj->chunkId % dev->nChunksPerBlock);
4441 + if(!obj->fake &&
4442 + (!chunkIdOk || !chunkIsLive)) {
4443 + T(YAFFS_TRACE_VERIFY,
4444 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4445 + obj->objectId,obj->chunkId,
4446 + chunkIdOk ? "" : ",out of range",
4447 + chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
4448 + }
4449 +
4450 + if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
4451 + yaffs_ExtendedTags tags;
4452 + yaffs_ObjectHeader *oh;
4453 + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
4454 +
4455 + oh = (yaffs_ObjectHeader *)buffer;
4456 +
4457 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
4458 +
4459 + yaffs_VerifyObjectHeader(obj,oh,&tags,1);
4460 +
4461 + yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
4462 + }
4463 +
4464 + /* Verify it has a parent */
4465 + if(obj && !obj->fake &&
4466 + (!obj->parent || obj->parent->myDev != dev)){
4467 + T(YAFFS_TRACE_VERIFY,
4468 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4469 + obj->objectId,obj->parent));
4470 + }
4471 +
4472 + /* Verify parent is a directory */
4473 + if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
4474 + T(YAFFS_TRACE_VERIFY,
4475 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4476 + obj->objectId,obj->parent->variantType));
4477 + }
4478 +
4479 + switch(obj->variantType){
4480 + case YAFFS_OBJECT_TYPE_FILE:
4481 + yaffs_VerifyFile(obj);
4482 + break;
4483 + case YAFFS_OBJECT_TYPE_SYMLINK:
4484 + yaffs_VerifySymlink(obj);
4485 + break;
4486 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4487 + yaffs_VerifyDirectory(obj);
4488 + break;
4489 + case YAFFS_OBJECT_TYPE_HARDLINK:
4490 + yaffs_VerifyHardLink(obj);
4491 + break;
4492 + case YAFFS_OBJECT_TYPE_SPECIAL:
4493 + yaffs_VerifySpecial(obj);
4494 + break;
4495 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4496 + default:
4497 + T(YAFFS_TRACE_VERIFY,
4498 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4499 + obj->objectId,obj->variantType));
4500 + break;
4501 + }
4502 +
4503 +
4504 +}
4505 +
4506 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4507 +{
4508 + yaffs_Object *obj;
4509 + int i;
4510 + struct list_head *lh;
4511 +
4512 + if(yaffs_SkipVerification(dev))
4513 + return;
4514 +
4515 + /* Iterate through the objects in each hash entry */
4516 +
4517 + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
4518 + list_for_each(lh, &dev->objectBucket[i].list) {
4519 + if (lh) {
4520 + obj = list_entry(lh, yaffs_Object, hashLink);
4521 + yaffs_VerifyObject(obj);
4522 + }
4523 + }
4524 + }
4525 +
4526 +}
4527 +
4528 +
4529 +/*
4530 + * Simple hash function. Needs to have a reasonable spread
4531 + */
4532 +
4533 +static Y_INLINE int yaffs_HashFunction(int n)
4534 +{
4535 + n = abs(n);
4536 + return (n % YAFFS_NOBJECT_BUCKETS);
4537 +}
4538 +
4539 +/*
4540 + * Access functions to useful fake objects
4541 + */
4542 +
4543 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
4544 +{
4545 + return dev->rootDir;
4546 +}
4547 +
4548 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
4549 +{
4550 + return dev->lostNFoundDir;
4551 +}
4552 +
4553 +
4554 +/*
4555 + * Erased NAND checking functions
4556 + */
4557 +
4558 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
4559 +{
4560 + /* Horrible, slow implementation */
4561 + while (nBytes--) {
4562 + if (*buffer != 0xFF)
4563 + return 0;
4564 + buffer++;
4565 + }
4566 + return 1;
4567 +}
4568 +
4569 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4570 + int chunkInNAND)
4571 +{
4572 +
4573 + int retval = YAFFS_OK;
4574 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4575 + yaffs_ExtendedTags tags;
4576 + int result;
4577 +
4578 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4579 +
4580 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4581 + retval = YAFFS_FAIL;
4582 +
4583 +
4584 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4585 + T(YAFFS_TRACE_NANDACCESS,
4586 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4587 + retval = YAFFS_FAIL;
4588 + }
4589 +
4590 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4591 +
4592 + return retval;
4593 +
4594 +}
4595 +
4596 +
4597 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4598 + const __u8 * data,
4599 + yaffs_ExtendedTags * tags,
4600 + int useReserve)
4601 +{
4602 + int attempts = 0;
4603 + int writeOk = 0;
4604 + int chunk;
4605 +
4606 + yaffs_InvalidateCheckpoint(dev);
4607 +
4608 + do {
4609 + yaffs_BlockInfo *bi = 0;
4610 + int erasedOk = 0;
4611 +
4612 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4613 + if (chunk < 0) {
4614 + /* no space */
4615 + break;
4616 + }
4617 +
4618 + /* First check this chunk is erased, if it needs
4619 + * checking. The checking policy (unless forced
4620 + * always on) is as follows:
4621 + *
4622 + * Check the first page we try to write in a block.
4623 + * If the check passes then we don't need to check any
4624 + * more. If the check fails, we check again...
4625 + * If the block has been erased, we don't need to check.
4626 + *
4627 + * However, if the block has been prioritised for gc,
4628 + * then we think there might be something odd about
4629 + * this block and stop using it.
4630 + *
4631 + * Rationale: We should only ever see chunks that have
4632 + * not been erased if there was a partially written
4633 + * chunk due to power loss. This checking policy should
4634 + * catch that case with very few checks and thus save a
4635 + * lot of checks that are most likely not needed.
4636 + */
4637 + if (bi->gcPrioritise) {
4638 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4639 + /* try another chunk */
4640 + continue;
4641 + }
4642 +
4643 + /* let's give it a try */
4644 + attempts++;
4645 +
4646 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4647 + bi->skipErasedCheck = 0;
4648 +#endif
4649 + if (!bi->skipErasedCheck) {
4650 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4651 + if (erasedOk != YAFFS_OK) {
4652 + T(YAFFS_TRACE_ERROR,
4653 + (TSTR ("**>> yaffs chunk %d was not erased"
4654 + TENDSTR), chunk));
4655 +
4656 + /* try another chunk */
4657 + continue;
4658 + }
4659 + bi->skipErasedCheck = 1;
4660 + }
4661 +
4662 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4663 + data, tags);
4664 + if (writeOk != YAFFS_OK) {
4665 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4666 + /* try another chunk */
4667 + continue;
4668 + }
4669 +
4670 + /* Copy the data into the robustification buffer */
4671 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4672 +
4673 + } while (writeOk != YAFFS_OK && attempts < yaffs_wr_attempts);
4674 +
4675 + if (attempts > 1) {
4676 + T(YAFFS_TRACE_ERROR,
4677 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4678 + attempts));
4679 +
4680 + dev->nRetriedWrites += (attempts - 1);
4681 + }
4682 +
4683 + return chunk;
4684 +}
4685 +
4686 +/*
4687 + * Block retiring for handling a broken block.
4688 + */
4689 +
4690 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
4691 +{
4692 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4693 +
4694 + yaffs_InvalidateCheckpoint(dev);
4695 +
4696 + yaffs_MarkBlockBad(dev, blockInNAND);
4697 +
4698 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
4699 + bi->gcPrioritise = 0;
4700 + bi->needsRetiring = 0;
4701 +
4702 + dev->nRetiredBlocks++;
4703 +}
4704 +
4705 +/*
4706 + * Functions for robustisizing TODO
4707 + *
4708 + */
4709 +
4710 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
4711 + const __u8 * data,
4712 + const yaffs_ExtendedTags * tags)
4713 +{
4714 +}
4715 +
4716 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
4717 + const yaffs_ExtendedTags * tags)
4718 +{
4719 +}
4720 +
4721 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
4722 +{
4723 + if(!bi->gcPrioritise){
4724 + bi->gcPrioritise = 1;
4725 + dev->hasPendingPrioritisedGCs = 1;
4726 + bi->chunkErrorStrikes ++;
4727 +
4728 + if(bi->chunkErrorStrikes > 3){
4729 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
4730 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
4731 +
4732 + }
4733 +
4734 + }
4735 +}
4736 +
4737 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
4738 +{
4739 +
4740 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
4741 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4742 +
4743 + yaffs_HandleChunkError(dev,bi);
4744 +
4745 +
4746 + if(erasedOk ) {
4747 + /* Was an actual write failure, so mark the block for retirement */
4748 + bi->needsRetiring = 1;
4749 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4750 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
4751 +
4752 +
4753 + }
4754 +
4755 + /* Delete the chunk */
4756 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
4757 +}
4758 +
4759 +
4760 +/*---------------- Name handling functions ------------*/
4761 +
4762 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
4763 +{
4764 + __u16 sum = 0;
4765 + __u16 i = 1;
4766 +
4767 + YUCHAR *bname = (YUCHAR *) name;
4768 + if (bname) {
4769 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
4770 +
4771 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4772 + sum += yaffs_toupper(*bname) * i;
4773 +#else
4774 + sum += (*bname) * i;
4775 +#endif
4776 + i++;
4777 + bname++;
4778 + }
4779 + }
4780 + return sum;
4781 +}
4782 +
4783 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4784 +{
4785 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4786 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4787 + yaffs_strcpy(obj->shortName, name);
4788 + } else {
4789 + obj->shortName[0] = _Y('\0');
4790 + }
4791 +#endif
4792 + obj->sum = yaffs_CalcNameSum(name);
4793 +}
4794 +
4795 +/*-------------------- TNODES -------------------
4796 +
4797 + * List of spare tnodes
4798 + * The list is hooked together using the first pointer
4799 + * in the tnode.
4800 + */
4801 +
4802 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4803 + * adds them to the tnode free list.
4804 + * Don't use this function directly
4805 + */
4806 +
4807 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4808 +{
4809 + int i;
4810 + int tnodeSize;
4811 + yaffs_Tnode *newTnodes;
4812 + __u8 *mem;
4813 + yaffs_Tnode *curr;
4814 + yaffs_Tnode *next;
4815 + yaffs_TnodeList *tnl;
4816 +
4817 + if (nTnodes < 1)
4818 + return YAFFS_OK;
4819 +
4820 + /* Calculate the tnode size in bytes for variable width tnode support.
4821 + * Must be a multiple of 32-bits */
4822 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4823 +
4824 + /* make these things */
4825 +
4826 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4827 + mem = (__u8 *)newTnodes;
4828 +
4829 + if (!newTnodes) {
4830 + T(YAFFS_TRACE_ERROR,
4831 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4832 + return YAFFS_FAIL;
4833 + }
4834 +
4835 + /* Hook them into the free list */
4836 +#if 0
4837 + for (i = 0; i < nTnodes - 1; i++) {
4838 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4839 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4840 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4841 +#endif
4842 + }
4843 +
4844 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4845 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4846 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4847 +#endif
4848 + dev->freeTnodes = newTnodes;
4849 +#else
4850 + /* New hookup for wide tnodes */
4851 + for(i = 0; i < nTnodes -1; i++) {
4852 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4853 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4854 + curr->internal[0] = next;
4855 + }
4856 +
4857 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4858 + curr->internal[0] = dev->freeTnodes;
4859 + dev->freeTnodes = (yaffs_Tnode *)mem;
4860 +
4861 +#endif
4862 +
4863 +
4864 + dev->nFreeTnodes += nTnodes;
4865 + dev->nTnodesCreated += nTnodes;
4866 +
4867 + /* Now add this bunch of tnodes to a list for freeing up.
4868 + * NB If we can't add this to the management list it isn't fatal
4869 + * but it just means we can't free this bunch of tnodes later.
4870 + */
4871 +
4872 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4873 + if (!tnl) {
4874 + T(YAFFS_TRACE_ERROR,
4875 + (TSTR
4876 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4877 + return YAFFS_FAIL;
4878 +
4879 + } else {
4880 + tnl->tnodes = newTnodes;
4881 + tnl->next = dev->allocatedTnodeList;
4882 + dev->allocatedTnodeList = tnl;
4883 + }
4884 +
4885 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4886 +
4887 + return YAFFS_OK;
4888 +}
4889 +
4890 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4891 +
4892 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4893 +{
4894 + yaffs_Tnode *tn = NULL;
4895 +
4896 + /* If there are none left make more */
4897 + if (!dev->freeTnodes) {
4898 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4899 + }
4900 +
4901 + if (dev->freeTnodes) {
4902 + tn = dev->freeTnodes;
4903 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4904 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4905 + /* Hoosterman, this thing looks like it isn't in the list */
4906 + T(YAFFS_TRACE_ALWAYS,
4907 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4908 + }
4909 +#endif
4910 + dev->freeTnodes = dev->freeTnodes->internal[0];
4911 + dev->nFreeTnodes--;
4912 + }
4913 +
4914 + return tn;
4915 +}
4916 +
4917 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4918 +{
4919 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4920 +
4921 + if(tn)
4922 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4923 +
4924 + return tn;
4925 +}
4926 +
4927 +/* FreeTnode frees up a tnode and puts it back on the free list */
4928 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4929 +{
4930 + if (tn) {
4931 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4932 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4933 + /* Hoosterman, this thing looks like it is already in the list */
4934 + T(YAFFS_TRACE_ALWAYS,
4935 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4936 + }
4937 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4938 +#endif
4939 + tn->internal[0] = dev->freeTnodes;
4940 + dev->freeTnodes = tn;
4941 + dev->nFreeTnodes++;
4942 + }
4943 +}
4944 +
4945 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4946 +{
4947 + /* Free the list of allocated tnodes */
4948 + yaffs_TnodeList *tmp;
4949 +
4950 + while (dev->allocatedTnodeList) {
4951 + tmp = dev->allocatedTnodeList->next;
4952 +
4953 + YFREE(dev->allocatedTnodeList->tnodes);
4954 + YFREE(dev->allocatedTnodeList);
4955 + dev->allocatedTnodeList = tmp;
4956 +
4957 + }
4958 +
4959 + dev->freeTnodes = NULL;
4960 + dev->nFreeTnodes = 0;
4961 +}
4962 +
4963 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4964 +{
4965 + dev->allocatedTnodeList = NULL;
4966 + dev->freeTnodes = NULL;
4967 + dev->nFreeTnodes = 0;
4968 + dev->nTnodesCreated = 0;
4969 +
4970 +}
4971 +
4972 +
4973 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4974 +{
4975 + __u32 *map = (__u32 *)tn;
4976 + __u32 bitInMap;
4977 + __u32 bitInWord;
4978 + __u32 wordInMap;
4979 + __u32 mask;
4980 +
4981 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4982 + val >>= dev->chunkGroupBits;
4983 +
4984 + bitInMap = pos * dev->tnodeWidth;
4985 + wordInMap = bitInMap /32;
4986 + bitInWord = bitInMap & (32 -1);
4987 +
4988 + mask = dev->tnodeMask << bitInWord;
4989 +
4990 + map[wordInMap] &= ~mask;
4991 + map[wordInMap] |= (mask & (val << bitInWord));
4992 +
4993 + if(dev->tnodeWidth > (32-bitInWord)) {
4994 + bitInWord = (32 - bitInWord);
4995 + wordInMap++;;
4996 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4997 + map[wordInMap] &= ~mask;
4998 + map[wordInMap] |= (mask & (val >> bitInWord));
4999 + }
5000 +}
5001 +
5002 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
5003 +{
5004 + __u32 *map = (__u32 *)tn;
5005 + __u32 bitInMap;
5006 + __u32 bitInWord;
5007 + __u32 wordInMap;
5008 + __u32 val;
5009 +
5010 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5011 +
5012 + bitInMap = pos * dev->tnodeWidth;
5013 + wordInMap = bitInMap /32;
5014 + bitInWord = bitInMap & (32 -1);
5015 +
5016 + val = map[wordInMap] >> bitInWord;
5017 +
5018 + if(dev->tnodeWidth > (32-bitInWord)) {
5019 + bitInWord = (32 - bitInWord);
5020 + wordInMap++;;
5021 + val |= (map[wordInMap] << bitInWord);
5022 + }
5023 +
5024 + val &= dev->tnodeMask;
5025 + val <<= dev->chunkGroupBits;
5026 +
5027 + return val;
5028 +}
5029 +
5030 +/* ------------------- End of individual tnode manipulation -----------------*/
5031 +
5032 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5033 + * The look up tree is represented by the top tnode and the number of topLevel
5034 + * in the tree. 0 means only the level 0 tnode is in the tree.
5035 + */
5036 +
5037 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5038 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
5039 + yaffs_FileStructure * fStruct,
5040 + __u32 chunkId)
5041 +{
5042 +
5043 + yaffs_Tnode *tn = fStruct->top;
5044 + __u32 i;
5045 + int requiredTallness;
5046 + int level = fStruct->topLevel;
5047 +
5048 + /* Check sane level and chunk Id */
5049 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
5050 + return NULL;
5051 + }
5052 +
5053 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5054 + return NULL;
5055 + }
5056 +
5057 + /* First check we're tall enough (ie enough topLevel) */
5058 +
5059 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5060 + requiredTallness = 0;
5061 + while (i) {
5062 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5063 + requiredTallness++;
5064 + }
5065 +
5066 + if (requiredTallness > fStruct->topLevel) {
5067 + /* Not tall enough, so we can't find it, return NULL. */
5068 + return NULL;
5069 + }
5070 +
5071 + /* Traverse down to level 0 */
5072 + while (level > 0 && tn) {
5073 + tn = tn->
5074 + internal[(chunkId >>
5075 + ( YAFFS_TNODES_LEVEL0_BITS +
5076 + (level - 1) *
5077 + YAFFS_TNODES_INTERNAL_BITS)
5078 + ) &
5079 + YAFFS_TNODES_INTERNAL_MASK];
5080 + level--;
5081 +
5082 + }
5083 +
5084 + return tn;
5085 +}
5086 +
5087 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5088 + * This happens in two steps:
5089 + * 1. If the tree isn't tall enough, then make it taller.
5090 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5091 + *
5092 + * Used when modifying the tree.
5093 + *
5094 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5095 + * be plugged into the ttree.
5096 + */
5097 +
5098 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
5099 + yaffs_FileStructure * fStruct,
5100 + __u32 chunkId,
5101 + yaffs_Tnode *passedTn)
5102 +{
5103 +
5104 + int requiredTallness;
5105 + int i;
5106 + int l;
5107 + yaffs_Tnode *tn;
5108 +
5109 + __u32 x;
5110 +
5111 +
5112 + /* Check sane level and page Id */
5113 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
5114 + return NULL;
5115 + }
5116 +
5117 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5118 + return NULL;
5119 + }
5120 +
5121 + /* First check we're tall enough (ie enough topLevel) */
5122 +
5123 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5124 + requiredTallness = 0;
5125 + while (x) {
5126 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5127 + requiredTallness++;
5128 + }
5129 +
5130 +
5131 + if (requiredTallness > fStruct->topLevel) {
5132 + /* Not tall enough,gotta make the tree taller */
5133 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5134 +
5135 + tn = yaffs_GetTnode(dev);
5136 +
5137 + if (tn) {
5138 + tn->internal[0] = fStruct->top;
5139 + fStruct->top = tn;
5140 + } else {
5141 + T(YAFFS_TRACE_ERROR,
5142 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5143 + }
5144 + }
5145 +
5146 + fStruct->topLevel = requiredTallness;
5147 + }
5148 +
5149 + /* Traverse down to level 0, adding anything we need */
5150 +
5151 + l = fStruct->topLevel;
5152 + tn = fStruct->top;
5153 +
5154 + if(l > 0) {
5155 + while (l > 0 && tn) {
5156 + x = (chunkId >>
5157 + ( YAFFS_TNODES_LEVEL0_BITS +
5158 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5159 + YAFFS_TNODES_INTERNAL_MASK;
5160 +
5161 +
5162 + if((l>1) && !tn->internal[x]){
5163 + /* Add missing non-level-zero tnode */
5164 + tn->internal[x] = yaffs_GetTnode(dev);
5165 +
5166 + } else if(l == 1) {
5167 + /* Looking from level 1 at level 0 */
5168 + if (passedTn) {
5169 + /* If we already have one, then release it.*/
5170 + if(tn->internal[x])
5171 + yaffs_FreeTnode(dev,tn->internal[x]);
5172 + tn->internal[x] = passedTn;
5173 +
5174 + } else if(!tn->internal[x]) {
5175 + /* Don't have one, none passed in */
5176 + tn->internal[x] = yaffs_GetTnode(dev);
5177 + }
5178 + }
5179 +
5180 + tn = tn->internal[x];
5181 + l--;
5182 + }
5183 + } else {
5184 + /* top is level 0 */
5185 + if(passedTn) {
5186 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5187 + yaffs_FreeTnode(dev,passedTn);
5188 + }
5189 + }
5190 +
5191 + return tn;
5192 +}
5193 +
5194 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
5195 + yaffs_ExtendedTags * tags, int objectId,
5196 + int chunkInInode)
5197 +{
5198 + int j;
5199 +
5200 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5201 + if (yaffs_CheckChunkBit
5202 + (dev, theChunk / dev->nChunksPerBlock,
5203 + theChunk % dev->nChunksPerBlock)) {
5204 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5205 + tags);
5206 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5207 + /* found it; */
5208 + return theChunk;
5209 +
5210 + }
5211 + }
5212 + theChunk++;
5213 + }
5214 + return -1;
5215 +}
5216 +
5217 +
5218 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5219 + * chunks and tnodes in the file
5220 + * Returns 1 if the tree was deleted.
5221 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5222 + */
5223 +
5224 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
5225 + int chunkOffset, int *limit)
5226 +{
5227 + int i;
5228 + int chunkInInode;
5229 + int theChunk;
5230 + yaffs_ExtendedTags tags;
5231 + int foundChunk;
5232 + yaffs_Device *dev = in->myDev;
5233 +
5234 + int allDone = 1;
5235 +
5236 + if (tn) {
5237 + if (level > 0) {
5238 +
5239 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5240 + i--) {
5241 + if (tn->internal[i]) {
5242 + if (limit && (*limit) < 0) {
5243 + allDone = 0;
5244 + } else {
5245 + allDone =
5246 + yaffs_DeleteWorker(in,
5247 + tn->
5248 + internal
5249 + [i],
5250 + level -
5251 + 1,
5252 + (chunkOffset
5253 + <<
5254 + YAFFS_TNODES_INTERNAL_BITS)
5255 + + i,
5256 + limit);
5257 + }
5258 + if (allDone) {
5259 + yaffs_FreeTnode(dev,
5260 + tn->
5261 + internal[i]);
5262 + tn->internal[i] = NULL;
5263 + }
5264 + }
5265 +
5266 + }
5267 + return (allDone) ? 1 : 0;
5268 + } else if (level == 0) {
5269 + int hitLimit = 0;
5270 +
5271 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5272 + i--) {
5273 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5274 + if (theChunk) {
5275 +
5276 + chunkInInode =
5277 + (chunkOffset <<
5278 + YAFFS_TNODES_LEVEL0_BITS) + i;
5279 +
5280 + foundChunk =
5281 + yaffs_FindChunkInGroup(dev,
5282 + theChunk,
5283 + &tags,
5284 + in->objectId,
5285 + chunkInInode);
5286 +
5287 + if (foundChunk > 0) {
5288 + yaffs_DeleteChunk(dev,
5289 + foundChunk, 1,
5290 + __LINE__);
5291 + in->nDataChunks--;
5292 + if (limit) {
5293 + *limit = *limit - 1;
5294 + if (*limit <= 0) {
5295 + hitLimit = 1;
5296 + }
5297 + }
5298 +
5299 + }
5300 +
5301 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5302 + }
5303 +
5304 + }
5305 + return (i < 0) ? 1 : 0;
5306 +
5307 + }
5308 +
5309 + }
5310 +
5311 + return 1;
5312 +
5313 +}
5314 +
5315 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
5316 +{
5317 +
5318 + yaffs_BlockInfo *theBlock;
5319 +
5320 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5321 +
5322 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5323 + if (theBlock) {
5324 + theBlock->softDeletions++;
5325 + dev->nFreeChunks++;
5326 + }
5327 +}
5328 +
5329 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5330 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5331 + * of the tnode.
5332 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5333 + */
5334 +
5335 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
5336 + __u32 level, int chunkOffset)
5337 +{
5338 + int i;
5339 + int theChunk;
5340 + int allDone = 1;
5341 + yaffs_Device *dev = in->myDev;
5342 +
5343 + if (tn) {
5344 + if (level > 0) {
5345 +
5346 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5347 + i--) {
5348 + if (tn->internal[i]) {
5349 + allDone =
5350 + yaffs_SoftDeleteWorker(in,
5351 + tn->
5352 + internal[i],
5353 + level - 1,
5354 + (chunkOffset
5355 + <<
5356 + YAFFS_TNODES_INTERNAL_BITS)
5357 + + i);
5358 + if (allDone) {
5359 + yaffs_FreeTnode(dev,
5360 + tn->
5361 + internal[i]);
5362 + tn->internal[i] = NULL;
5363 + } else {
5364 + /* Hoosterman... how could this happen? */
5365 + }
5366 + }
5367 + }
5368 + return (allDone) ? 1 : 0;
5369 + } else if (level == 0) {
5370 +
5371 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5372 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5373 + if (theChunk) {
5374 + /* Note this does not find the real chunk, only the chunk group.
5375 + * We make an assumption that a chunk group is not larger than
5376 + * a block.
5377 + */
5378 + yaffs_SoftDeleteChunk(dev, theChunk);
5379 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5380 + }
5381 +
5382 + }
5383 + return 1;
5384 +
5385 + }
5386 +
5387 + }
5388 +
5389 + return 1;
5390 +
5391 +}
5392 +
5393 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
5394 +{
5395 + if (obj->deleted &&
5396 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5397 + if (obj->nDataChunks <= 0) {
5398 + /* Empty file with no duplicate object headers, just delete it immediately */
5399 + yaffs_FreeTnode(obj->myDev,
5400 + obj->variant.fileVariant.top);
5401 + obj->variant.fileVariant.top = NULL;
5402 + T(YAFFS_TRACE_TRACING,
5403 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5404 + obj->objectId));
5405 + yaffs_DoGenericObjectDeletion(obj);
5406 + } else {
5407 + yaffs_SoftDeleteWorker(obj,
5408 + obj->variant.fileVariant.top,
5409 + obj->variant.fileVariant.
5410 + topLevel, 0);
5411 + obj->softDeleted = 1;
5412 + }
5413 + }
5414 +}
5415 +
5416 +/* Pruning removes any part of the file structure tree that is beyond the
5417 + * bounds of the file (ie that does not point to chunks).
5418 + *
5419 + * A file should only get pruned when its size is reduced.
5420 + *
5421 + * Before pruning, the chunks must be pulled from the tree and the
5422 + * level 0 tnode entries must be zeroed out.
5423 + * Could also use this for file deletion, but that's probably better handled
5424 + * by a special case.
5425 + */
5426 +
5427 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
5428 + __u32 level, int del0)
5429 +{
5430 + int i;
5431 + int hasData;
5432 +
5433 + if (tn) {
5434 + hasData = 0;
5435 +
5436 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5437 + if (tn->internal[i] && level > 0) {
5438 + tn->internal[i] =
5439 + yaffs_PruneWorker(dev, tn->internal[i],
5440 + level - 1,
5441 + (i == 0) ? del0 : 1);
5442 + }
5443 +
5444 + if (tn->internal[i]) {
5445 + hasData++;
5446 + }
5447 + }
5448 +
5449 + if (hasData == 0 && del0) {
5450 + /* Free and return NULL */
5451 +
5452 + yaffs_FreeTnode(dev, tn);
5453 + tn = NULL;
5454 + }
5455 +
5456 + }
5457 +
5458 + return tn;
5459 +
5460 +}
5461 +
5462 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
5463 + yaffs_FileStructure * fStruct)
5464 +{
5465 + int i;
5466 + int hasData;
5467 + int done = 0;
5468 + yaffs_Tnode *tn;
5469 +
5470 + if (fStruct->topLevel > 0) {
5471 + fStruct->top =
5472 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5473 +
5474 + /* Now we have a tree with all the non-zero branches NULL but the height
5475 + * is the same as it was.
5476 + * Let's see if we can trim internal tnodes to shorten the tree.
5477 + * We can do this if only the 0th element in the tnode is in use
5478 + * (ie all the non-zero are NULL)
5479 + */
5480 +
5481 + while (fStruct->topLevel && !done) {
5482 + tn = fStruct->top;
5483 +
5484 + hasData = 0;
5485 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5486 + if (tn->internal[i]) {
5487 + hasData++;
5488 + }
5489 + }
5490 +
5491 + if (!hasData) {
5492 + fStruct->top = tn->internal[0];
5493 + fStruct->topLevel--;
5494 + yaffs_FreeTnode(dev, tn);
5495 + } else {
5496 + done = 1;
5497 + }
5498 + }
5499 + }
5500 +
5501 + return YAFFS_OK;
5502 +}
5503 +
5504 +/*-------------------- End of File Structure functions.-------------------*/
5505 +
5506 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5507 + * adds them to the object free list.
5508 + */
5509 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
5510 +{
5511 + int i;
5512 + yaffs_Object *newObjects;
5513 + yaffs_ObjectList *list;
5514 +
5515 + if (nObjects < 1)
5516 + return YAFFS_OK;
5517 +
5518 + /* make these things */
5519 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5520 + list = YMALLOC(sizeof(yaffs_ObjectList));
5521 +
5522 + if (!newObjects || !list) {
5523 + if(newObjects)
5524 + YFREE(newObjects);
5525 + if(list)
5526 + YFREE(list);
5527 + T(YAFFS_TRACE_ALLOCATE,
5528 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5529 + return YAFFS_FAIL;
5530 + }
5531 +
5532 + /* Hook them into the free list */
5533 + for (i = 0; i < nObjects - 1; i++) {
5534 + newObjects[i].siblings.next =
5535 + (struct list_head *)(&newObjects[i + 1]);
5536 + }
5537 +
5538 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5539 + dev->freeObjects = newObjects;
5540 + dev->nFreeObjects += nObjects;
5541 + dev->nObjectsCreated += nObjects;
5542 +
5543 + /* Now add this bunch of Objects to a list for freeing up. */
5544 +
5545 + list->objects = newObjects;
5546 + list->next = dev->allocatedObjectList;
5547 + dev->allocatedObjectList = list;
5548 +
5549 + return YAFFS_OK;
5550 +}
5551 +
5552 +
5553 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5554 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
5555 +{
5556 + yaffs_Object *tn = NULL;
5557 +
5558 + /* If there are none left make more */
5559 + if (!dev->freeObjects) {
5560 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5561 + }
5562 +
5563 + if (dev->freeObjects) {
5564 + tn = dev->freeObjects;
5565 + dev->freeObjects =
5566 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5567 + dev->nFreeObjects--;
5568 +
5569 + /* Now sweeten it up... */
5570 +
5571 + memset(tn, 0, sizeof(yaffs_Object));
5572 + tn->myDev = dev;
5573 + tn->chunkId = -1;
5574 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5575 + INIT_LIST_HEAD(&(tn->hardLinks));
5576 + INIT_LIST_HEAD(&(tn->hashLink));
5577 + INIT_LIST_HEAD(&tn->siblings);
5578 +
5579 + /* Add it to the lost and found directory.
5580 + * NB Can't put root or lostNFound in lostNFound so
5581 + * check if lostNFound exists first
5582 + */
5583 + if (dev->lostNFoundDir) {
5584 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5585 + }
5586 + }
5587 +
5588 + return tn;
5589 +}
5590 +
5591 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
5592 + __u32 mode)
5593 +{
5594 +
5595 + yaffs_Object *obj =
5596 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5597 + if (obj) {
5598 + obj->fake = 1; /* it is fake so it has no NAND presence... */
5599 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5600 + obj->unlinkAllowed = 0; /* ... or unlink it */
5601 + obj->deleted = 0;
5602 + obj->unlinked = 0;
5603 + obj->yst_mode = mode;
5604 + obj->myDev = dev;
5605 + obj->chunkId = 0; /* Not a valid chunk. */
5606 + }
5607 +
5608 + return obj;
5609 +
5610 +}
5611 +
5612 +static void yaffs_UnhashObject(yaffs_Object * tn)
5613 +{
5614 + int bucket;
5615 + yaffs_Device *dev = tn->myDev;
5616 +
5617 + /* If it is still linked into the bucket list, free from the list */
5618 + if (!list_empty(&tn->hashLink)) {
5619 + list_del_init(&tn->hashLink);
5620 + bucket = yaffs_HashFunction(tn->objectId);
5621 + dev->objectBucket[bucket].count--;
5622 + }
5623 +
5624 +}
5625 +
5626 +/* FreeObject frees up a Object and puts it back on the free list */
5627 +static void yaffs_FreeObject(yaffs_Object * tn)
5628 +{
5629 +
5630 + yaffs_Device *dev = tn->myDev;
5631 +
5632 +#ifdef __KERNEL__
5633 + if (tn->myInode) {
5634 + /* We're still hooked up to a cached inode.
5635 + * Don't delete now, but mark for later deletion
5636 + */
5637 + tn->deferedFree = 1;
5638 + return;
5639 + }
5640 +#endif
5641 +
5642 + yaffs_UnhashObject(tn);
5643 +
5644 + /* Link into the free list. */
5645 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
5646 + dev->freeObjects = tn;
5647 + dev->nFreeObjects++;
5648 +}
5649 +
5650 +#ifdef __KERNEL__
5651 +
5652 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
5653 +{
5654 + if (obj->deferedFree) {
5655 + yaffs_FreeObject(obj);
5656 + }
5657 +}
5658 +
5659 +#endif
5660 +
5661 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
5662 +{
5663 + /* Free the list of allocated Objects */
5664 +
5665 + yaffs_ObjectList *tmp;
5666 +
5667 + while (dev->allocatedObjectList) {
5668 + tmp = dev->allocatedObjectList->next;
5669 + YFREE(dev->allocatedObjectList->objects);
5670 + YFREE(dev->allocatedObjectList);
5671 +
5672 + dev->allocatedObjectList = tmp;
5673 + }
5674 +
5675 + dev->freeObjects = NULL;
5676 + dev->nFreeObjects = 0;
5677 +}
5678 +
5679 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
5680 +{
5681 + int i;
5682 +
5683 + dev->allocatedObjectList = NULL;
5684 + dev->freeObjects = NULL;
5685 + dev->nFreeObjects = 0;
5686 +
5687 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5688 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
5689 + dev->objectBucket[i].count = 0;
5690 + }
5691 +
5692 +}
5693 +
5694 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
5695 +{
5696 + static int x = 0;
5697 + int i;
5698 + int l = 999;
5699 + int lowest = 999999;
5700 +
5701 + /* First let's see if we can find one that's empty. */
5702 +
5703 + for (i = 0; i < 10 && lowest > 0; i++) {
5704 + x++;
5705 + x %= YAFFS_NOBJECT_BUCKETS;
5706 + if (dev->objectBucket[x].count < lowest) {
5707 + lowest = dev->objectBucket[x].count;
5708 + l = x;
5709 + }
5710 +
5711 + }
5712 +
5713 + /* If we didn't find an empty list, then try
5714 + * looking a bit further for a short one
5715 + */
5716 +
5717 + for (i = 0; i < 10 && lowest > 3; i++) {
5718 + x++;
5719 + x %= YAFFS_NOBJECT_BUCKETS;
5720 + if (dev->objectBucket[x].count < lowest) {
5721 + lowest = dev->objectBucket[x].count;
5722 + l = x;
5723 + }
5724 +
5725 + }
5726 +
5727 + return l;
5728 +}
5729 +
5730 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
5731 +{
5732 + int bucket = yaffs_FindNiceObjectBucket(dev);
5733 +
5734 + /* Now find an object value that has not already been taken
5735 + * by scanning the list.
5736 + */
5737 +
5738 + int found = 0;
5739 + struct list_head *i;
5740 +
5741 + __u32 n = (__u32) bucket;
5742 +
5743 + /* yaffs_CheckObjectHashSanity(); */
5744 +
5745 + while (!found) {
5746 + found = 1;
5747 + n += YAFFS_NOBJECT_BUCKETS;
5748 + if (1 || dev->objectBucket[bucket].count > 0) {
5749 + list_for_each(i, &dev->objectBucket[bucket].list) {
5750 + /* If there is already one in the list */
5751 + if (i
5752 + && list_entry(i, yaffs_Object,
5753 + hashLink)->objectId == n) {
5754 + found = 0;
5755 + }
5756 + }
5757 + }
5758 + }
5759 +
5760 +
5761 + return n;
5762 +}
5763 +
5764 +static void yaffs_HashObject(yaffs_Object * in)
5765 +{
5766 + int bucket = yaffs_HashFunction(in->objectId);
5767 + yaffs_Device *dev = in->myDev;
5768 +
5769 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
5770 + dev->objectBucket[bucket].count++;
5771 +
5772 +}
5773 +
5774 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5775 +{
5776 + int bucket = yaffs_HashFunction(number);
5777 + struct list_head *i;
5778 + yaffs_Object *in;
5779 +
5780 + list_for_each(i, &dev->objectBucket[bucket].list) {
5781 + /* Look if it is in the list */
5782 + if (i) {
5783 + in = list_entry(i, yaffs_Object, hashLink);
5784 + if (in->objectId == number) {
5785 +#ifdef __KERNEL__
5786 + /* Don't tell the VFS about this one if it is defered free */
5787 + if (in->deferedFree)
5788 + return NULL;
5789 +#endif
5790 +
5791 + return in;
5792 + }
5793 + }
5794 + }
5795 +
5796 + return NULL;
5797 +}
5798 +
5799 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5800 + yaffs_ObjectType type)
5801 +{
5802 +
5803 + yaffs_Object *theObject;
5804 + yaffs_Tnode *tn;
5805 +
5806 + if (number < 0) {
5807 + number = yaffs_CreateNewObjectNumber(dev);
5808 + }
5809 +
5810 + theObject = yaffs_AllocateEmptyObject(dev);
5811 + if(!theObject)
5812 + return NULL;
5813 +
5814 + if(type == YAFFS_OBJECT_TYPE_FILE){
5815 + tn = yaffs_GetTnode(dev);
5816 + if(!tn){
5817 + yaffs_FreeObject(theObject);
5818 + return NULL;
5819 + }
5820 + }
5821 +
5822 +
5823 +
5824 + if (theObject) {
5825 + theObject->fake = 0;
5826 + theObject->renameAllowed = 1;
5827 + theObject->unlinkAllowed = 1;
5828 + theObject->objectId = number;
5829 + yaffs_HashObject(theObject);
5830 + theObject->variantType = type;
5831 +#ifdef CONFIG_YAFFS_WINCE
5832 + yfsd_WinFileTimeNow(theObject->win_atime);
5833 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5834 + theObject->win_atime[0];
5835 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5836 + theObject->win_atime[1];
5837 +
5838 +#else
5839 +
5840 + theObject->yst_atime = theObject->yst_mtime =
5841 + theObject->yst_ctime = Y_CURRENT_TIME;
5842 +#endif
5843 + switch (type) {
5844 + case YAFFS_OBJECT_TYPE_FILE:
5845 + theObject->variant.fileVariant.fileSize = 0;
5846 + theObject->variant.fileVariant.scannedFileSize = 0;
5847 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5848 + theObject->variant.fileVariant.topLevel = 0;
5849 + theObject->variant.fileVariant.top = tn;
5850 + break;
5851 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5852 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5853 + children);
5854 + break;
5855 + case YAFFS_OBJECT_TYPE_SYMLINK:
5856 + case YAFFS_OBJECT_TYPE_HARDLINK:
5857 + case YAFFS_OBJECT_TYPE_SPECIAL:
5858 + /* No action required */
5859 + break;
5860 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5861 + /* todo this should not happen */
5862 + break;
5863 + }
5864 + }
5865 +
5866 + return theObject;
5867 +}
5868 +
5869 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5870 + int number,
5871 + yaffs_ObjectType type)
5872 +{
5873 + yaffs_Object *theObject = NULL;
5874 +
5875 + if (number > 0) {
5876 + theObject = yaffs_FindObjectByNumber(dev, number);
5877 + }
5878 +
5879 + if (!theObject) {
5880 + theObject = yaffs_CreateNewObject(dev, number, type);
5881 + }
5882 +
5883 + return theObject;
5884 +
5885 +}
5886 +
5887 +
5888 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5889 +{
5890 + YCHAR *newStr = NULL;
5891 +
5892 + if (str && *str) {
5893 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5894 + if(newStr)
5895 + yaffs_strcpy(newStr, str);
5896 + }
5897 +
5898 + return newStr;
5899 +
5900 +}
5901 +
5902 +/*
5903 + * Mknod (create) a new object.
5904 + * equivalentObject only has meaning for a hard link;
5905 + * aliasString only has meaning for a sumlink.
5906 + * rdev only has meaning for devices (a subset of special objects)
5907 + */
5908 +
5909 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5910 + yaffs_Object * parent,
5911 + const YCHAR * name,
5912 + __u32 mode,
5913 + __u32 uid,
5914 + __u32 gid,
5915 + yaffs_Object * equivalentObject,
5916 + const YCHAR * aliasString, __u32 rdev)
5917 +{
5918 + yaffs_Object *in;
5919 + YCHAR *str;
5920 +
5921 + yaffs_Device *dev = parent->myDev;
5922 +
5923 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5924 + if (yaffs_FindObjectByName(parent, name)) {
5925 + return NULL;
5926 + }
5927 +
5928 + in = yaffs_CreateNewObject(dev, -1, type);
5929 +
5930 + if(type == YAFFS_OBJECT_TYPE_SYMLINK){
5931 + str = yaffs_CloneString(aliasString);
5932 + if(!str){
5933 + yaffs_FreeObject(in);
5934 + return NULL;
5935 + }
5936 + }
5937 +
5938 +
5939 +
5940 + if (in) {
5941 + in->chunkId = -1;
5942 + in->valid = 1;
5943 + in->variantType = type;
5944 +
5945 + in->yst_mode = mode;
5946 +
5947 +#ifdef CONFIG_YAFFS_WINCE
5948 + yfsd_WinFileTimeNow(in->win_atime);
5949 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5950 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5951 +
5952 +#else
5953 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5954 +
5955 + in->yst_rdev = rdev;
5956 + in->yst_uid = uid;
5957 + in->yst_gid = gid;
5958 +#endif
5959 + in->nDataChunks = 0;
5960 +
5961 + yaffs_SetObjectName(in, name);
5962 + in->dirty = 1;
5963 +
5964 + yaffs_AddObjectToDirectory(parent, in);
5965 +
5966 + in->myDev = parent->myDev;
5967 +
5968 + switch (type) {
5969 + case YAFFS_OBJECT_TYPE_SYMLINK:
5970 + in->variant.symLinkVariant.alias = str;
5971 + break;
5972 + case YAFFS_OBJECT_TYPE_HARDLINK:
5973 + in->variant.hardLinkVariant.equivalentObject =
5974 + equivalentObject;
5975 + in->variant.hardLinkVariant.equivalentObjectId =
5976 + equivalentObject->objectId;
5977 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5978 + break;
5979 + case YAFFS_OBJECT_TYPE_FILE:
5980 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5981 + case YAFFS_OBJECT_TYPE_SPECIAL:
5982 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5983 + /* do nothing */
5984 + break;
5985 + }
5986 +
5987 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5988 + /* Could not create the object header, fail the creation */
5989 + yaffs_DestroyObject(in);
5990 + in = NULL;
5991 + }
5992 +
5993 + }
5994 +
5995 + return in;
5996 +}
5997 +
5998 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5999 + __u32 mode, __u32 uid, __u32 gid)
6000 +{
6001 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
6002 + uid, gid, NULL, NULL, 0);
6003 +}
6004 +
6005 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
6006 + __u32 mode, __u32 uid, __u32 gid)
6007 +{
6008 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
6009 + mode, uid, gid, NULL, NULL, 0);
6010 +}
6011 +
6012 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
6013 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
6014 +{
6015 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
6016 + uid, gid, NULL, NULL, rdev);
6017 +}
6018 +
6019 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
6020 + __u32 mode, __u32 uid, __u32 gid,
6021 + const YCHAR * alias)
6022 +{
6023 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
6024 + uid, gid, NULL, alias, 0);
6025 +}
6026 +
6027 +/* yaffs_Link returns the object id of the equivalent object.*/
6028 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
6029 + yaffs_Object * equivalentObject)
6030 +{
6031 + /* Get the real object in case we were fed a hard link as an equivalent object */
6032 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6033 +
6034 + if (yaffs_MknodObject
6035 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6036 + equivalentObject, NULL, 0)) {
6037 + return equivalentObject;
6038 + } else {
6039 + return NULL;
6040 + }
6041 +
6042 +}
6043 +
6044 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
6045 + const YCHAR * newName, int force, int shadows)
6046 +{
6047 + int unlinkOp;
6048 + int deleteOp;
6049 +
6050 + yaffs_Object *existingTarget;
6051 +
6052 + if (newDir == NULL) {
6053 + newDir = obj->parent; /* use the old directory */
6054 + }
6055 +
6056 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6057 + T(YAFFS_TRACE_ALWAYS,
6058 + (TSTR
6059 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
6060 + TENDSTR)));
6061 + YBUG();
6062 + }
6063 +
6064 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6065 + if (obj->myDev->isYaffs2) {
6066 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6067 + } else {
6068 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6069 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6070 + }
6071 +
6072 + deleteOp = (newDir == obj->myDev->deletedDir);
6073 +
6074 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6075 +
6076 + /* If the object is a file going into the unlinked directory,
6077 + * then it is OK to just stuff it in since duplicate names are allowed.
6078 + * else only proceed if the new name does not exist and if we're putting
6079 + * it into a directory.
6080 + */
6081 + if ((unlinkOp ||
6082 + deleteOp ||
6083 + force ||
6084 + (shadows > 0) ||
6085 + !existingTarget) &&
6086 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6087 + yaffs_SetObjectName(obj, newName);
6088 + obj->dirty = 1;
6089 +
6090 + yaffs_AddObjectToDirectory(newDir, obj);
6091 +
6092 + if (unlinkOp)
6093 + obj->unlinked = 1;
6094 +
6095 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6096 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
6097 + return YAFFS_OK;
6098 + }
6099 +
6100 + return YAFFS_FAIL;
6101 +}
6102 +
6103 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
6104 + yaffs_Object * newDir, const YCHAR * newName)
6105 +{
6106 + yaffs_Object *obj;
6107 + yaffs_Object *existingTarget;
6108 + int force = 0;
6109 +
6110 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6111 + /* Special case for case insemsitive systems (eg. WinCE).
6112 + * While look-up is case insensitive, the name isn't.
6113 + * Therefore we might want to change x.txt to X.txt
6114 + */
6115 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
6116 + force = 1;
6117 + }
6118 +#endif
6119 +
6120 + obj = yaffs_FindObjectByName(oldDir, oldName);
6121 + /* Check new name to long. */
6122 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
6123 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
6124 + /* ENAMETOOLONG */
6125 + return YAFFS_FAIL;
6126 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
6127 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6128 + /* ENAMETOOLONG */
6129 + return YAFFS_FAIL;
6130 +
6131 + if (obj && obj->renameAllowed) {
6132 +
6133 + /* Now do the handling for an existing target, if there is one */
6134 +
6135 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6136 + if (existingTarget &&
6137 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6138 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
6139 + /* There is a target that is a non-empty directory, so we fail */
6140 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6141 + } else if (existingTarget && existingTarget != obj) {
6142 + /* Nuke the target first, using shadowing,
6143 + * but only if it isn't the same object
6144 + */
6145 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6146 + existingTarget->objectId);
6147 + yaffs_UnlinkObject(existingTarget);
6148 + }
6149 +
6150 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6151 + }
6152 + return YAFFS_FAIL;
6153 +}
6154 +
6155 +/*------------------------- Block Management and Page Allocation ----------------*/
6156 +
6157 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
6158 +{
6159 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6160 +
6161 + dev->blockInfo = NULL;
6162 + dev->chunkBits = NULL;
6163 +
6164 + dev->allocationBlock = -1; /* force it to get a new one */
6165 +
6166 + /* If the first allocation strategy fails, thry the alternate one */
6167 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6168 + if(!dev->blockInfo){
6169 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6170 + dev->blockInfoAlt = 1;
6171 + }
6172 + else
6173 + dev->blockInfoAlt = 0;
6174 +
6175 + if(dev->blockInfo){
6176 +
6177 + /* Set up dynamic blockinfo stuff. */
6178 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6179 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6180 + if(!dev->chunkBits){
6181 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6182 + dev->chunkBitsAlt = 1;
6183 + }
6184 + else
6185 + dev->chunkBitsAlt = 0;
6186 + }
6187 +
6188 + if (dev->blockInfo && dev->chunkBits) {
6189 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6190 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6191 + return YAFFS_OK;
6192 + }
6193 +
6194 + return YAFFS_FAIL;
6195 +
6196 +}
6197 +
6198 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
6199 +{
6200 + if(dev->blockInfoAlt && dev->blockInfo)
6201 + YFREE_ALT(dev->blockInfo);
6202 + else if(dev->blockInfo)
6203 + YFREE(dev->blockInfo);
6204 +
6205 + dev->blockInfoAlt = 0;
6206 +
6207 + dev->blockInfo = NULL;
6208 +
6209 + if(dev->chunkBitsAlt && dev->chunkBits)
6210 + YFREE_ALT(dev->chunkBits);
6211 + else if(dev->chunkBits)
6212 + YFREE(dev->chunkBits);
6213 + dev->chunkBitsAlt = 0;
6214 + dev->chunkBits = NULL;
6215 +}
6216 +
6217 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
6218 + yaffs_BlockInfo * bi)
6219 +{
6220 + int i;
6221 + __u32 seq;
6222 + yaffs_BlockInfo *b;
6223 +
6224 + if (!dev->isYaffs2)
6225 + return 1; /* disqualification only applies to yaffs2. */
6226 +
6227 + if (!bi->hasShrinkHeader)
6228 + return 1; /* can gc */
6229 +
6230 + /* Find the oldest dirty sequence number if we don't know it and save it
6231 + * so we don't have to keep recomputing it.
6232 + */
6233 + if (!dev->oldestDirtySequence) {
6234 + seq = dev->sequenceNumber;
6235 +
6236 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6237 + i++) {
6238 + b = yaffs_GetBlockInfo(dev, i);
6239 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6240 + (b->pagesInUse - b->softDeletions) <
6241 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6242 + seq = b->sequenceNumber;
6243 + }
6244 + }
6245 + dev->oldestDirtySequence = seq;
6246 + }
6247 +
6248 + /* Can't do gc of this block if there are any blocks older than this one that have
6249 + * discarded pages.
6250 + */
6251 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6252 +
6253 +}
6254 +
6255 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6256 + * for garbage collection.
6257 + */
6258 +
6259 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
6260 + int aggressive)
6261 +{
6262 +
6263 + int b = dev->currentDirtyChecker;
6264 +
6265 + int i;
6266 + int iterations;
6267 + int dirtiest = -1;
6268 + int pagesInUse = 0;
6269 + int prioritised=0;
6270 + yaffs_BlockInfo *bi;
6271 + int pendingPrioritisedExist = 0;
6272 +
6273 + /* First let's see if we need to grab a prioritised block */
6274 + if(dev->hasPendingPrioritisedGCs){
6275 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
6276 +
6277 + bi = yaffs_GetBlockInfo(dev, i);
6278 + //yaffs_VerifyBlock(dev,bi,i);
6279 +
6280 + if(bi->gcPrioritise) {
6281 + pendingPrioritisedExist = 1;
6282 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6283 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
6284 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6285 + dirtiest = i;
6286 + prioritised = 1;
6287 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6288 + }
6289 + }
6290 + }
6291 +
6292 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
6293 + dev->hasPendingPrioritisedGCs = 0;
6294 + }
6295 +
6296 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6297 + * search harder.
6298 + * else (we're doing a leasurely gc), then we only bother to do this if the
6299 + * block has only a few pages in use.
6300 + */
6301 +
6302 + dev->nonAggressiveSkip--;
6303 +
6304 + if (!aggressive && (dev->nonAggressiveSkip > 0)) {
6305 + return -1;
6306 + }
6307 +
6308 + if(!prioritised)
6309 + pagesInUse =
6310 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6311 +
6312 + if (aggressive) {
6313 + iterations =
6314 + dev->internalEndBlock - dev->internalStartBlock + 1;
6315 + } else {
6316 + iterations =
6317 + dev->internalEndBlock - dev->internalStartBlock + 1;
6318 + iterations = iterations / 16;
6319 + if (iterations > 200) {
6320 + iterations = 200;
6321 + }
6322 + }
6323 +
6324 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6325 + b++;
6326 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6327 + b = dev->internalStartBlock;
6328 + }
6329 +
6330 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6331 + T(YAFFS_TRACE_ERROR,
6332 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6333 + YBUG();
6334 + }
6335 +
6336 + bi = yaffs_GetBlockInfo(dev, b);
6337 +
6338 +#if 0
6339 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
6340 + dirtiest = b;
6341 + pagesInUse = 0;
6342 + }
6343 + else
6344 +#endif
6345 +
6346 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6347 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6348 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6349 + dirtiest = b;
6350 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6351 + }
6352 + }
6353 +
6354 + dev->currentDirtyChecker = b;
6355 +
6356 + if (dirtiest > 0) {
6357 + T(YAFFS_TRACE_GC,
6358 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6359 + dev->nChunksPerBlock - pagesInUse,prioritised));
6360 + }
6361 +
6362 + dev->oldestDirtySequence = 0;
6363 +
6364 + if (dirtiest > 0) {
6365 + dev->nonAggressiveSkip = 4;
6366 + }
6367 +
6368 + return dirtiest;
6369 +}
6370 +
6371 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
6372 +{
6373 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6374 +
6375 + int erasedOk = 0;
6376 +
6377 + /* If the block is still healthy erase it and mark as clean.
6378 + * If the block has had a data failure, then retire it.
6379 + */
6380 +
6381 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6382 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6383 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6384 +
6385 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6386 +
6387 + if (!bi->needsRetiring) {
6388 + yaffs_InvalidateCheckpoint(dev);
6389 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6390 + if (!erasedOk) {
6391 + dev->nErasureFailures++;
6392 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6393 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6394 + }
6395 + }
6396 +
6397 + if (erasedOk &&
6398 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6399 + int i;
6400 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6401 + if (!yaffs_CheckChunkErased
6402 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6403 + T(YAFFS_TRACE_ERROR,
6404 + (TSTR
6405 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6406 + TENDSTR), blockNo, i));
6407 + }
6408 + }
6409 + }
6410 +
6411 + if (erasedOk) {
6412 + /* Clean it up... */
6413 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6414 + dev->nErasedBlocks++;
6415 + bi->pagesInUse = 0;
6416 + bi->softDeletions = 0;
6417 + bi->hasShrinkHeader = 0;
6418 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6419 + bi->gcPrioritise = 0;
6420 + yaffs_ClearChunkBits(dev, blockNo);
6421 +
6422 + T(YAFFS_TRACE_ERASE,
6423 + (TSTR("Erased block %d" TENDSTR), blockNo));
6424 + } else {
6425 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6426 +
6427 + yaffs_RetireBlock(dev, blockNo);
6428 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6429 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6430 + }
6431 +}
6432 +
6433 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
6434 +{
6435 + int i;
6436 +
6437 + yaffs_BlockInfo *bi;
6438 +
6439 + if (dev->nErasedBlocks < 1) {
6440 + /* Hoosterman we've got a problem.
6441 + * Can't get space to gc
6442 + */
6443 + T(YAFFS_TRACE_ERROR,
6444 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
6445 +
6446 + return -1;
6447 + }
6448 +
6449 + /* Find an empty block. */
6450 +
6451 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6452 + dev->allocationBlockFinder++;
6453 + if (dev->allocationBlockFinder < dev->internalStartBlock
6454 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6455 + dev->allocationBlockFinder = dev->internalStartBlock;
6456 + }
6457 +
6458 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6459 +
6460 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6461 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6462 + dev->sequenceNumber++;
6463 + bi->sequenceNumber = dev->sequenceNumber;
6464 + dev->nErasedBlocks--;
6465 + T(YAFFS_TRACE_ALLOCATE,
6466 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6467 + dev->allocationBlockFinder, dev->sequenceNumber,
6468 + dev->nErasedBlocks));
6469 + return dev->allocationBlockFinder;
6470 + }
6471 + }
6472 +
6473 + T(YAFFS_TRACE_ALWAYS,
6474 + (TSTR
6475 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
6476 + TENDSTR), dev->nErasedBlocks));
6477 +
6478 + return -1;
6479 +}
6480 +
6481 +
6482 +// Check if there's space to allocate...
6483 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6484 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
6485 +{
6486 + int reservedChunks;
6487 + int reservedBlocks = dev->nReservedBlocks;
6488 + int checkpointBlocks;
6489 +
6490 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
6491 + if(checkpointBlocks < 0)
6492 + checkpointBlocks = 0;
6493 +
6494 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6495 +
6496 + return (dev->nFreeChunks > reservedChunks);
6497 +}
6498 +
6499 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
6500 +{
6501 + int retVal;
6502 + yaffs_BlockInfo *bi;
6503 +
6504 + if (dev->allocationBlock < 0) {
6505 + /* Get next block to allocate off */
6506 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6507 + dev->allocationPage = 0;
6508 + }
6509 +
6510 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6511 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6512 + return -1;
6513 + }
6514 +
6515 + if (dev->nErasedBlocks < dev->nReservedBlocks
6516 + && dev->allocationPage == 0) {
6517 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6518 + }
6519 +
6520 + /* Next page please.... */
6521 + if (dev->allocationBlock >= 0) {
6522 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6523 +
6524 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6525 + dev->allocationPage;
6526 + bi->pagesInUse++;
6527 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6528 + dev->allocationPage);
6529 +
6530 + dev->allocationPage++;
6531 +
6532 + dev->nFreeChunks--;
6533 +
6534 + /* If the block is full set the state to full */
6535 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6536 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6537 + dev->allocationBlock = -1;
6538 + }
6539 +
6540 + if(blockUsedPtr)
6541 + *blockUsedPtr = bi;
6542 +
6543 + return retVal;
6544 + }
6545 +
6546 + T(YAFFS_TRACE_ERROR,
6547 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6548 +
6549 + return -1;
6550 +}
6551 +
6552 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
6553 +{
6554 + int n;
6555 +
6556 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6557 +
6558 + if (dev->allocationBlock > 0) {
6559 + n += (dev->nChunksPerBlock - dev->allocationPage);
6560 + }
6561 +
6562 + return n;
6563 +
6564 +}
6565 +
6566 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
6567 +{
6568 + int oldChunk;
6569 + int newChunk;
6570 + int chunkInBlock;
6571 + int markNAND;
6572 + int retVal = YAFFS_OK;
6573 + int cleanups = 0;
6574 + int i;
6575 + int isCheckpointBlock;
6576 + int matchingChunk;
6577 +
6578 + int chunksBefore = yaffs_GetErasedChunks(dev);
6579 + int chunksAfter;
6580 +
6581 + yaffs_ExtendedTags tags;
6582 +
6583 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6584 +
6585 + yaffs_Object *object;
6586 +
6587 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6588 +
6589 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6590 +
6591 + T(YAFFS_TRACE_TRACING,
6592 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
6593 + bi->pagesInUse, bi->hasShrinkHeader));
6594 +
6595 + /*yaffs_VerifyFreeChunks(dev); */
6596 +
6597 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6598 +
6599 + /* Take off the number of soft deleted entries because
6600 + * they're going to get really deleted during GC.
6601 + */
6602 + dev->nFreeChunks -= bi->softDeletions;
6603 +
6604 + dev->isDoingGC = 1;
6605 +
6606 + if (isCheckpointBlock ||
6607 + !yaffs_StillSomeChunkBits(dev, block)) {
6608 + T(YAFFS_TRACE_TRACING,
6609 + (TSTR
6610 + ("Collecting block %d that has no chunks in use" TENDSTR),
6611 + block));
6612 + yaffs_BlockBecameDirty(dev, block);
6613 + } else {
6614 +
6615 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6616 +
6617 + yaffs_VerifyBlock(dev,bi,block);
6618 +
6619 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
6620 + chunkInBlock < dev->nChunksPerBlock
6621 + && yaffs_StillSomeChunkBits(dev, block);
6622 + chunkInBlock++, oldChunk++) {
6623 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
6624 +
6625 + /* This page is in use and might need to be copied off */
6626 +
6627 + markNAND = 1;
6628 +
6629 + yaffs_InitialiseTags(&tags);
6630 +
6631 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
6632 + buffer, &tags);
6633 +
6634 + object =
6635 + yaffs_FindObjectByNumber(dev,
6636 + tags.objectId);
6637 +
6638 + T(YAFFS_TRACE_GC_DETAIL,
6639 + (TSTR
6640 + ("Collecting page %d, %d %d %d " TENDSTR),
6641 + chunkInBlock, tags.objectId, tags.chunkId,
6642 + tags.byteCount));
6643 +
6644 + if(object && !yaffs_SkipVerification(dev)){
6645 + if(tags.chunkId == 0)
6646 + matchingChunk = object->chunkId;
6647 + else if(object->softDeleted)
6648 + matchingChunk = oldChunk; /* Defeat the test */
6649 + else
6650 + matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
6651 +
6652 + if(oldChunk != matchingChunk)
6653 + T(YAFFS_TRACE_ERROR,
6654 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
6655 + oldChunk,matchingChunk,tags.objectId, tags.chunkId));
6656 +
6657 + }
6658 +
6659 + if (!object) {
6660 + T(YAFFS_TRACE_ERROR,
6661 + (TSTR
6662 + ("page %d in gc has no object: %d %d %d "
6663 + TENDSTR), oldChunk,
6664 + tags.objectId, tags.chunkId, tags.byteCount));
6665 + }
6666 +
6667 + if (object && object->deleted
6668 + && tags.chunkId != 0) {
6669 + /* Data chunk in a deleted file, throw it away
6670 + * It's a soft deleted data chunk,
6671 + * No need to copy this, just forget about it and
6672 + * fix up the object.
6673 + */
6674 +
6675 + object->nDataChunks--;
6676 +
6677 + if (object->nDataChunks <= 0) {
6678 + /* remeber to clean up the object */
6679 + dev->gcCleanupList[cleanups] =
6680 + tags.objectId;
6681 + cleanups++;
6682 + }
6683 + markNAND = 0;
6684 + } else if (0
6685 + /* Todo object && object->deleted && object->nDataChunks == 0 */
6686 + ) {
6687 + /* Deleted object header with no data chunks.
6688 + * Can be discarded and the file deleted.
6689 + */
6690 + object->chunkId = 0;
6691 + yaffs_FreeTnode(object->myDev,
6692 + object->variant.
6693 + fileVariant.top);
6694 + object->variant.fileVariant.top = NULL;
6695 + yaffs_DoGenericObjectDeletion(object);
6696 +
6697 + } else if (object) {
6698 + /* It's either a data chunk in a live file or
6699 + * an ObjectHeader, so we're interested in it.
6700 + * NB Need to keep the ObjectHeaders of deleted files
6701 + * until the whole file has been deleted off
6702 + */
6703 + tags.serialNumber++;
6704 +
6705 + dev->nGCCopies++;
6706 +
6707 + if (tags.chunkId == 0) {
6708 + /* It is an object Id,
6709 + * We need to nuke the shrinkheader flags first
6710 + * We no longer want the shrinkHeader flag since its work is done
6711 + * and if it is left in place it will mess up scanning.
6712 + * Also, clear out any shadowing stuff
6713 + */
6714 +
6715 + yaffs_ObjectHeader *oh;
6716 + oh = (yaffs_ObjectHeader *)buffer;
6717 + oh->isShrink = 0;
6718 + oh->shadowsObject = -1;
6719 + tags.extraShadows = 0;
6720 + tags.extraIsShrinkHeader = 0;
6721 +
6722 + yaffs_VerifyObjectHeader(object,oh,&tags,1);
6723 + }
6724 +
6725 + newChunk =
6726 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
6727 +
6728 + if (newChunk < 0) {
6729 + retVal = YAFFS_FAIL;
6730 + } else {
6731 +
6732 + /* Ok, now fix up the Tnodes etc. */
6733 +
6734 + if (tags.chunkId == 0) {
6735 + /* It's a header */
6736 + object->chunkId = newChunk;
6737 + object->serial = tags.serialNumber;
6738 + } else {
6739 + /* It's a data chunk */
6740 + yaffs_PutChunkIntoFile
6741 + (object,
6742 + tags.chunkId,
6743 + newChunk, 0);
6744 + }
6745 + }
6746 + }
6747 +
6748 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
6749 +
6750 + }
6751 + }
6752 +
6753 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6754 +
6755 +
6756 + /* Do any required cleanups */
6757 + for (i = 0; i < cleanups; i++) {
6758 + /* Time to delete the file too */
6759 + object =
6760 + yaffs_FindObjectByNumber(dev,
6761 + dev->gcCleanupList[i]);
6762 + if (object) {
6763 + yaffs_FreeTnode(dev,
6764 + object->variant.fileVariant.
6765 + top);
6766 + object->variant.fileVariant.top = NULL;
6767 + T(YAFFS_TRACE_GC,
6768 + (TSTR
6769 + ("yaffs: About to finally delete object %d"
6770 + TENDSTR), object->objectId));
6771 + yaffs_DoGenericObjectDeletion(object);
6772 + object->myDev->nDeletedFiles--;
6773 + }
6774 +
6775 + }
6776 +
6777 + }
6778 +
6779 + yaffs_VerifyCollectedBlock(dev,bi,block);
6780 +
6781 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
6782 + T(YAFFS_TRACE_GC,
6783 + (TSTR
6784 + ("gc did not increase free chunks before %d after %d"
6785 + TENDSTR), chunksBefore, chunksAfter));
6786 + }
6787 +
6788 + dev->isDoingGC = 0;
6789 +
6790 + return YAFFS_OK;
6791 +}
6792 +
6793 +/* New garbage collector
6794 + * If we're very low on erased blocks then we do aggressive garbage collection
6795 + * otherwise we do "leasurely" garbage collection.
6796 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
6797 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
6798 + *
6799 + * The idea is to help clear out space in a more spread-out manner.
6800 + * Dunno if it really does anything useful.
6801 + */
6802 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
6803 +{
6804 + int block;
6805 + int aggressive;
6806 + int gcOk = YAFFS_OK;
6807 + int maxTries = 0;
6808 +
6809 + int checkpointBlockAdjust;
6810 +
6811 + if (dev->isDoingGC) {
6812 + /* Bail out so we don't get recursive gc */
6813 + return YAFFS_OK;
6814 + }
6815 +
6816 + /* This loop should pass the first time.
6817 + * We'll only see looping here if the erase of the collected block fails.
6818 + */
6819 +
6820 + do {
6821 + maxTries++;
6822 +
6823 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
6824 + if(checkpointBlockAdjust < 0)
6825 + checkpointBlockAdjust = 0;
6826 +
6827 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
6828 + /* We need a block soon...*/
6829 + aggressive = 1;
6830 + } else {
6831 + /* We're in no hurry */
6832 + aggressive = 0;
6833 + }
6834 +
6835 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6836 +
6837 + if (block > 0) {
6838 + dev->garbageCollections++;
6839 + if (!aggressive) {
6840 + dev->passiveGarbageCollections++;
6841 + }
6842 +
6843 + T(YAFFS_TRACE_GC,
6844 + (TSTR
6845 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6846 + dev->nErasedBlocks, aggressive));
6847 +
6848 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6849 + }
6850 +
6851 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6852 + T(YAFFS_TRACE_GC,
6853 + (TSTR
6854 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6855 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6856 + }
6857 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6858 + && (maxTries < 2));
6859 +
6860 + return aggressive ? gcOk : YAFFS_OK;
6861 +}
6862 +
6863 +/*------------------------- TAGS --------------------------------*/
6864 +
6865 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6866 + int chunkInObject)
6867 +{
6868 + return (tags->chunkId == chunkInObject &&
6869 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6870 +
6871 +}
6872 +
6873 +
6874 +/*-------------------- Data file manipulation -----------------*/
6875 +
6876 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6877 + yaffs_ExtendedTags * tags)
6878 +{
6879 + /*Get the Tnode, then get the level 0 offset chunk offset */
6880 + yaffs_Tnode *tn;
6881 + int theChunk = -1;
6882 + yaffs_ExtendedTags localTags;
6883 + int retVal = -1;
6884 +
6885 + yaffs_Device *dev = in->myDev;
6886 +
6887 + if (!tags) {
6888 + /* Passed a NULL, so use our own tags space */
6889 + tags = &localTags;
6890 + }
6891 +
6892 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6893 +
6894 + if (tn) {
6895 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6896 +
6897 + retVal =
6898 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6899 + chunkInInode);
6900 + }
6901 + return retVal;
6902 +}
6903 +
6904 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6905 + yaffs_ExtendedTags * tags)
6906 +{
6907 + /* Get the Tnode, then get the level 0 offset chunk offset */
6908 + yaffs_Tnode *tn;
6909 + int theChunk = -1;
6910 + yaffs_ExtendedTags localTags;
6911 +
6912 + yaffs_Device *dev = in->myDev;
6913 + int retVal = -1;
6914 +
6915 + if (!tags) {
6916 + /* Passed a NULL, so use our own tags space */
6917 + tags = &localTags;
6918 + }
6919 +
6920 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6921 +
6922 + if (tn) {
6923 +
6924 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6925 +
6926 + retVal =
6927 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6928 + chunkInInode);
6929 +
6930 + /* Delete the entry in the filestructure (if found) */
6931 + if (retVal != -1) {
6932 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6933 + }
6934 + } else {
6935 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6936 + }
6937 +
6938 + if (retVal == -1) {
6939 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6940 + }
6941 + return retVal;
6942 +}
6943 +
6944 +#ifdef YAFFS_PARANOID
6945 +
6946 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6947 +{
6948 + int chunk;
6949 + int nChunks;
6950 + int fSize;
6951 + int failed = 0;
6952 + int objId;
6953 + yaffs_Tnode *tn;
6954 + yaffs_Tags localTags;
6955 + yaffs_Tags *tags = &localTags;
6956 + int theChunk;
6957 + int chunkDeleted;
6958 +
6959 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6960 + /* T(("Object not a file\n")); */
6961 + return YAFFS_FAIL;
6962 + }
6963 +
6964 + objId = in->objectId;
6965 + fSize = in->variant.fileVariant.fileSize;
6966 + nChunks =
6967 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6968 +
6969 + for (chunk = 1; chunk <= nChunks; chunk++) {
6970 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6971 + chunk);
6972 +
6973 + if (tn) {
6974 +
6975 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6976 +
6977 + if (yaffs_CheckChunkBits
6978 + (dev, theChunk / dev->nChunksPerBlock,
6979 + theChunk % dev->nChunksPerBlock)) {
6980 +
6981 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6982 + tags,
6983 + &chunkDeleted);
6984 + if (yaffs_TagsMatch
6985 + (tags, in->objectId, chunk, chunkDeleted)) {
6986 + /* found it; */
6987 +
6988 + }
6989 + } else {
6990 +
6991 + failed = 1;
6992 + }
6993 +
6994 + } else {
6995 + /* T(("No level 0 found for %d\n", chunk)); */
6996 + }
6997 + }
6998 +
6999 + return failed ? YAFFS_FAIL : YAFFS_OK;
7000 +}
7001 +
7002 +#endif
7003 +
7004 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
7005 + int chunkInNAND, int inScan)
7006 +{
7007 + /* NB inScan is zero unless scanning.
7008 + * For forward scanning, inScan is > 0;
7009 + * for backward scanning inScan is < 0
7010 + */
7011 +
7012 + yaffs_Tnode *tn;
7013 + yaffs_Device *dev = in->myDev;
7014 + int existingChunk;
7015 + yaffs_ExtendedTags existingTags;
7016 + yaffs_ExtendedTags newTags;
7017 + unsigned existingSerial, newSerial;
7018 +
7019 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7020 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
7021 + * If it is not during Scanning then something went wrong!
7022 + */
7023 + if (!inScan) {
7024 + T(YAFFS_TRACE_ERROR,
7025 + (TSTR
7026 + ("yaffs tragedy:attempt to put data chunk into a non-file"
7027 + TENDSTR)));
7028 + YBUG();
7029 + }
7030 +
7031 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7032 + return YAFFS_OK;
7033 + }
7034 +
7035 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7036 + &in->variant.fileVariant,
7037 + chunkInInode,
7038 + NULL);
7039 + if (!tn) {
7040 + return YAFFS_FAIL;
7041 + }
7042 +
7043 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
7044 +
7045 + if (inScan != 0) {
7046 + /* If we're scanning then we need to test for duplicates
7047 + * NB This does not need to be efficient since it should only ever
7048 + * happen when the power fails during a write, then only one
7049 + * chunk should ever be affected.
7050 + *
7051 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7052 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7053 + */
7054 +
7055 + if (existingChunk != 0) {
7056 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7057 + * thus we have to do a FindChunkInFile to get the real chunk id.
7058 + *
7059 + * We have a duplicate now we need to decide which one to use:
7060 + *
7061 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7062 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7063 + * YAFFS1: Get both sets of tags and compare serial numbers.
7064 + */
7065 +
7066 + if (inScan > 0) {
7067 + /* Only do this for forward scanning */
7068 + yaffs_ReadChunkWithTagsFromNAND(dev,
7069 + chunkInNAND,
7070 + NULL, &newTags);
7071 +
7072 + /* Do a proper find */
7073 + existingChunk =
7074 + yaffs_FindChunkInFile(in, chunkInInode,
7075 + &existingTags);
7076 + }
7077 +
7078 + if (existingChunk <= 0) {
7079 + /*Hoosterman - how did this happen? */
7080 +
7081 + T(YAFFS_TRACE_ERROR,
7082 + (TSTR
7083 + ("yaffs tragedy: existing chunk < 0 in scan"
7084 + TENDSTR)));
7085 +
7086 + }
7087 +
7088 + /* NB The deleted flags should be false, otherwise the chunks will
7089 + * not be loaded during a scan
7090 + */
7091 +
7092 + newSerial = newTags.serialNumber;
7093 + existingSerial = existingTags.serialNumber;
7094 +
7095 + if ((inScan > 0) &&
7096 + (in->myDev->isYaffs2 ||
7097 + existingChunk <= 0 ||
7098 + ((existingSerial + 1) & 3) == newSerial)) {
7099 + /* Forward scanning.
7100 + * Use new
7101 + * Delete the old one and drop through to update the tnode
7102 + */
7103 + yaffs_DeleteChunk(dev, existingChunk, 1,
7104 + __LINE__);
7105 + } else {
7106 + /* Backward scanning or we want to use the existing one
7107 + * Use existing.
7108 + * Delete the new one and return early so that the tnode isn't changed
7109 + */
7110 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7111 + __LINE__);
7112 + return YAFFS_OK;
7113 + }
7114 + }
7115 +
7116 + }
7117 +
7118 + if (existingChunk == 0) {
7119 + in->nDataChunks++;
7120 + }
7121 +
7122 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
7123 +
7124 + return YAFFS_OK;
7125 +}
7126 +
7127 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
7128 + __u8 * buffer)
7129 +{
7130 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7131 +
7132 + if (chunkInNAND >= 0) {
7133 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7134 + buffer,NULL);
7135 + } else {
7136 + T(YAFFS_TRACE_NANDACCESS,
7137 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7138 + chunkInNAND));
7139 + /* get sane (zero) data if you read a hole */
7140 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7141 + return 0;
7142 + }
7143 +
7144 +}
7145 +
7146 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
7147 +{
7148 + int block;
7149 + int page;
7150 + yaffs_ExtendedTags tags;
7151 + yaffs_BlockInfo *bi;
7152 +
7153 + if (chunkId <= 0)
7154 + return;
7155 +
7156 +
7157 + dev->nDeletions++;
7158 + block = chunkId / dev->nChunksPerBlock;
7159 + page = chunkId % dev->nChunksPerBlock;
7160 +
7161 +
7162 + if(!yaffs_CheckChunkBit(dev,block,page))
7163 + T(YAFFS_TRACE_VERIFY,
7164 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7165 + chunkId));
7166 +
7167 + bi = yaffs_GetBlockInfo(dev, block);
7168 +
7169 + T(YAFFS_TRACE_DELETION,
7170 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7171 +
7172 + if (markNAND &&
7173 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7174 +
7175 + yaffs_InitialiseTags(&tags);
7176 +
7177 + tags.chunkDeleted = 1;
7178 +
7179 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7180 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7181 + } else {
7182 + dev->nUnmarkedDeletions++;
7183 + }
7184 +
7185 + /* Pull out of the management area.
7186 + * If the whole block became dirty, this will kick off an erasure.
7187 + */
7188 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7189 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7190 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7191 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7192 + dev->nFreeChunks++;
7193 +
7194 + yaffs_ClearChunkBit(dev, block, page);
7195 +
7196 + bi->pagesInUse--;
7197 +
7198 + if (bi->pagesInUse == 0 &&
7199 + !bi->hasShrinkHeader &&
7200 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7201 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7202 + yaffs_BlockBecameDirty(dev, block);
7203 + }
7204 +
7205 + } else {
7206 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
7207 + }
7208 +
7209 +}
7210 +
7211 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
7212 + const __u8 * buffer, int nBytes,
7213 + int useReserve)
7214 +{
7215 + /* Find old chunk Need to do this to get serial number
7216 + * Write new one and patch into tree.
7217 + * Invalidate old tags.
7218 + */
7219 +
7220 + int prevChunkId;
7221 + yaffs_ExtendedTags prevTags;
7222 +
7223 + int newChunkId;
7224 + yaffs_ExtendedTags newTags;
7225 +
7226 + yaffs_Device *dev = in->myDev;
7227 +
7228 + yaffs_CheckGarbageCollection(dev);
7229 +
7230 + /* Get the previous chunk at this location in the file if it exists */
7231 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7232 +
7233 + /* Set up new tags */
7234 + yaffs_InitialiseTags(&newTags);
7235 +
7236 + newTags.chunkId = chunkInInode;
7237 + newTags.objectId = in->objectId;
7238 + newTags.serialNumber =
7239 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7240 + newTags.byteCount = nBytes;
7241 +
7242 + newChunkId =
7243 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7244 + useReserve);
7245 +
7246 + if (newChunkId >= 0) {
7247 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7248 +
7249 + if (prevChunkId >= 0) {
7250 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7251 +
7252 + }
7253 +
7254 + yaffs_CheckFileSanity(in);
7255 + }
7256 + return newChunkId;
7257 +
7258 +}
7259 +
7260 +/* UpdateObjectHeader updates the header on NAND for an object.
7261 + * If name is not NULL, then that new name is used.
7262 + */
7263 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
7264 + int isShrink, int shadows)
7265 +{
7266 +
7267 + yaffs_BlockInfo *bi;
7268 +
7269 + yaffs_Device *dev = in->myDev;
7270 +
7271 + int prevChunkId;
7272 + int retVal = 0;
7273 + int result = 0;
7274 +
7275 + int newChunkId;
7276 + yaffs_ExtendedTags newTags;
7277 + yaffs_ExtendedTags oldTags;
7278 +
7279 + __u8 *buffer = NULL;
7280 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7281 +
7282 + yaffs_ObjectHeader *oh = NULL;
7283 +
7284 + yaffs_strcpy(oldName,"silly old name");
7285 +
7286 + if (!in->fake || force) {
7287 +
7288 + yaffs_CheckGarbageCollection(dev);
7289 + yaffs_CheckObjectDetailsLoaded(in);
7290 +
7291 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7292 + oh = (yaffs_ObjectHeader *) buffer;
7293 +
7294 + prevChunkId = in->chunkId;
7295 +
7296 + if (prevChunkId >= 0) {
7297 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7298 + buffer, &oldTags);
7299 +
7300 + yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
7301 +
7302 + memcpy(oldName, oh->name, sizeof(oh->name));
7303 + }
7304 +
7305 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7306 +
7307 + oh->type = in->variantType;
7308 + oh->yst_mode = in->yst_mode;
7309 + oh->shadowsObject = shadows;
7310 +
7311 +#ifdef CONFIG_YAFFS_WINCE
7312 + oh->win_atime[0] = in->win_atime[0];
7313 + oh->win_ctime[0] = in->win_ctime[0];
7314 + oh->win_mtime[0] = in->win_mtime[0];
7315 + oh->win_atime[1] = in->win_atime[1];
7316 + oh->win_ctime[1] = in->win_ctime[1];
7317 + oh->win_mtime[1] = in->win_mtime[1];
7318 +#else
7319 + oh->yst_uid = in->yst_uid;
7320 + oh->yst_gid = in->yst_gid;
7321 + oh->yst_atime = in->yst_atime;
7322 + oh->yst_mtime = in->yst_mtime;
7323 + oh->yst_ctime = in->yst_ctime;
7324 + oh->yst_rdev = in->yst_rdev;
7325 +#endif
7326 + if (in->parent) {
7327 + oh->parentObjectId = in->parent->objectId;
7328 + } else {
7329 + oh->parentObjectId = 0;
7330 + }
7331 +
7332 + if (name && *name) {
7333 + memset(oh->name, 0, sizeof(oh->name));
7334 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7335 + } else if (prevChunkId>=0) {
7336 + memcpy(oh->name, oldName, sizeof(oh->name));
7337 + } else {
7338 + memset(oh->name, 0, sizeof(oh->name));
7339 + }
7340 +
7341 + oh->isShrink = isShrink;
7342 +
7343 + switch (in->variantType) {
7344 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7345 + /* Should not happen */
7346 + break;
7347 + case YAFFS_OBJECT_TYPE_FILE:
7348 + oh->fileSize =
7349 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7350 + || oh->parentObjectId ==
7351 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7352 + fileVariant.fileSize;
7353 + break;
7354 + case YAFFS_OBJECT_TYPE_HARDLINK:
7355 + oh->equivalentObjectId =
7356 + in->variant.hardLinkVariant.equivalentObjectId;
7357 + break;
7358 + case YAFFS_OBJECT_TYPE_SPECIAL:
7359 + /* Do nothing */
7360 + break;
7361 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7362 + /* Do nothing */
7363 + break;
7364 + case YAFFS_OBJECT_TYPE_SYMLINK:
7365 + yaffs_strncpy(oh->alias,
7366 + in->variant.symLinkVariant.alias,
7367 + YAFFS_MAX_ALIAS_LENGTH);
7368 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7369 + break;
7370 + }
7371 +
7372 + /* Tags */
7373 + yaffs_InitialiseTags(&newTags);
7374 + in->serial++;
7375 + newTags.chunkId = 0;
7376 + newTags.objectId = in->objectId;
7377 + newTags.serialNumber = in->serial;
7378 +
7379 + /* Add extra info for file header */
7380 +
7381 + newTags.extraHeaderInfoAvailable = 1;
7382 + newTags.extraParentObjectId = oh->parentObjectId;
7383 + newTags.extraFileLength = oh->fileSize;
7384 + newTags.extraIsShrinkHeader = oh->isShrink;
7385 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7386 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7387 + newTags.extraObjectType = in->variantType;
7388 +
7389 + yaffs_VerifyObjectHeader(in,oh,&newTags,1);
7390 +
7391 + /* Create new chunk in NAND */
7392 + newChunkId =
7393 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7394 + (prevChunkId >= 0) ? 1 : 0);
7395 +
7396 + if (newChunkId >= 0) {
7397 +
7398 + in->chunkId = newChunkId;
7399 +
7400 + if (prevChunkId >= 0) {
7401 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7402 + __LINE__);
7403 + }
7404 +
7405 + if(!yaffs_ObjectHasCachedWriteData(in))
7406 + in->dirty = 0;
7407 +
7408 + /* If this was a shrink, then mark the block that the chunk lives on */
7409 + if (isShrink) {
7410 + bi = yaffs_GetBlockInfo(in->myDev,
7411 + newChunkId /in->myDev-> nChunksPerBlock);
7412 + bi->hasShrinkHeader = 1;
7413 + }
7414 +
7415 + }
7416 +
7417 + retVal = newChunkId;
7418 +
7419 + }
7420 +
7421 + if (buffer)
7422 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7423 +
7424 + return retVal;
7425 +}
7426 +
7427 +/*------------------------ Short Operations Cache ----------------------------------------
7428 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7429 + * reads might be short sequential reads, and a lot of writes may be short
7430 + * sequential writes. eg. scanning/writing a jpeg file.
7431 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7432 + * with dumb-as-a-rock code.
7433 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7434 + * buffering.
7435 + *
7436 + * There are a limited number (~10) of cache chunks per device so that we don't
7437 + * need a very intelligent search.
7438 + */
7439 +
7440 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7441 +{
7442 + yaffs_Device *dev = obj->myDev;
7443 + int i;
7444 + yaffs_ChunkCache *cache;
7445 + int nCaches = obj->myDev->nShortOpCaches;
7446 +
7447 + for(i = 0; i < nCaches; i++){
7448 + cache = &dev->srCache[i];
7449 + if (cache->object == obj &&
7450 + cache->dirty)
7451 + return 1;
7452 + }
7453 +
7454 + return 0;
7455 +}
7456 +
7457 +
7458 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
7459 +{
7460 + yaffs_Device *dev = obj->myDev;
7461 + int lowest = -99; /* Stop compiler whining. */
7462 + int i;
7463 + yaffs_ChunkCache *cache;
7464 + int chunkWritten = 0;
7465 + int nCaches = obj->myDev->nShortOpCaches;
7466 +
7467 + if (nCaches > 0) {
7468 + do {
7469 + cache = NULL;
7470 +
7471 + /* Find the dirty cache for this object with the lowest chunk id. */
7472 + for (i = 0; i < nCaches; i++) {
7473 + if (dev->srCache[i].object == obj &&
7474 + dev->srCache[i].dirty) {
7475 + if (!cache
7476 + || dev->srCache[i].chunkId <
7477 + lowest) {
7478 + cache = &dev->srCache[i];
7479 + lowest = cache->chunkId;
7480 + }
7481 + }
7482 + }
7483 +
7484 + if (cache && !cache->locked) {
7485 + /* Write it out and free it up */
7486 +
7487 + chunkWritten =
7488 + yaffs_WriteChunkDataToObject(cache->object,
7489 + cache->chunkId,
7490 + cache->data,
7491 + cache->nBytes,
7492 + 1);
7493 + cache->dirty = 0;
7494 + cache->object = NULL;
7495 + }
7496 +
7497 + } while (cache && chunkWritten > 0);
7498 +
7499 + if (cache) {
7500 + /* Hoosterman, disk full while writing cache out. */
7501 + T(YAFFS_TRACE_ERROR,
7502 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7503 +
7504 + }
7505 + }
7506 +
7507 +}
7508 +
7509 +/*yaffs_FlushEntireDeviceCache(dev)
7510 + *
7511 + *
7512 + */
7513 +
7514 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7515 +{
7516 + yaffs_Object *obj;
7517 + int nCaches = dev->nShortOpCaches;
7518 + int i;
7519 +
7520 + /* Find a dirty object in the cache and flush it...
7521 + * until there are no further dirty objects.
7522 + */
7523 + do {
7524 + obj = NULL;
7525 + for( i = 0; i < nCaches && !obj; i++) {
7526 + if (dev->srCache[i].object &&
7527 + dev->srCache[i].dirty)
7528 + obj = dev->srCache[i].object;
7529 +
7530 + }
7531 + if(obj)
7532 + yaffs_FlushFilesChunkCache(obj);
7533 +
7534 + } while(obj);
7535 +
7536 +}
7537 +
7538 +
7539 +/* Grab us a cache chunk for use.
7540 + * First look for an empty one.
7541 + * Then look for the least recently used non-dirty one.
7542 + * Then look for the least recently used dirty one...., flush and look again.
7543 + */
7544 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
7545 +{
7546 + int i;
7547 + int usage;
7548 + int theOne;
7549 +
7550 + if (dev->nShortOpCaches > 0) {
7551 + for (i = 0; i < dev->nShortOpCaches; i++) {
7552 + if (!dev->srCache[i].object)
7553 + return &dev->srCache[i];
7554 + }
7555 +
7556 + return NULL;
7557 +
7558 + theOne = -1;
7559 + usage = 0; /* just to stop the compiler grizzling */
7560 +
7561 + for (i = 0; i < dev->nShortOpCaches; i++) {
7562 + if (!dev->srCache[i].dirty &&
7563 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
7564 + theOne < 0)) {
7565 + usage = dev->srCache[i].lastUse;
7566 + theOne = i;
7567 + }
7568 + }
7569 +
7570 +
7571 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
7572 + } else {
7573 + return NULL;
7574 + }
7575 +
7576 +}
7577 +
7578 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
7579 +{
7580 + yaffs_ChunkCache *cache;
7581 + yaffs_Object *theObj;
7582 + int usage;
7583 + int i;
7584 + int pushout;
7585 +
7586 + if (dev->nShortOpCaches > 0) {
7587 + /* Try find a non-dirty one... */
7588 +
7589 + cache = yaffs_GrabChunkCacheWorker(dev);
7590 +
7591 + if (!cache) {
7592 + /* They were all dirty, find the last recently used object and flush
7593 + * its cache, then find again.
7594 + * NB what's here is not very accurate, we actually flush the object
7595 + * the last recently used page.
7596 + */
7597 +
7598 + /* With locking we can't assume we can use entry zero */
7599 +
7600 + theObj = NULL;
7601 + usage = -1;
7602 + cache = NULL;
7603 + pushout = -1;
7604 +
7605 + for (i = 0; i < dev->nShortOpCaches; i++) {
7606 + if (dev->srCache[i].object &&
7607 + !dev->srCache[i].locked &&
7608 + (dev->srCache[i].lastUse < usage || !cache))
7609 + {
7610 + usage = dev->srCache[i].lastUse;
7611 + theObj = dev->srCache[i].object;
7612 + cache = &dev->srCache[i];
7613 + pushout = i;
7614 + }
7615 + }
7616 +
7617 + if (!cache || cache->dirty) {
7618 + /* Flush and try again */
7619 + yaffs_FlushFilesChunkCache(theObj);
7620 + cache = yaffs_GrabChunkCacheWorker(dev);
7621 + }
7622 +
7623 + }
7624 + return cache;
7625 + } else
7626 + return NULL;
7627 +
7628 +}
7629 +
7630 +/* Find a cached chunk */
7631 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
7632 + int chunkId)
7633 +{
7634 + yaffs_Device *dev = obj->myDev;
7635 + int i;
7636 + if (dev->nShortOpCaches > 0) {
7637 + for (i = 0; i < dev->nShortOpCaches; i++) {
7638 + if (dev->srCache[i].object == obj &&
7639 + dev->srCache[i].chunkId == chunkId) {
7640 + dev->cacheHits++;
7641 +
7642 + return &dev->srCache[i];
7643 + }
7644 + }
7645 + }
7646 + return NULL;
7647 +}
7648 +
7649 +/* Mark the chunk for the least recently used algorithym */
7650 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
7651 + int isAWrite)
7652 +{
7653 +
7654 + if (dev->nShortOpCaches > 0) {
7655 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
7656 + /* Reset the cache usages */
7657 + int i;
7658 + for (i = 1; i < dev->nShortOpCaches; i++) {
7659 + dev->srCache[i].lastUse = 0;
7660 + }
7661 + dev->srLastUse = 0;
7662 + }
7663 +
7664 + dev->srLastUse++;
7665 +
7666 + cache->lastUse = dev->srLastUse;
7667 +
7668 + if (isAWrite) {
7669 + cache->dirty = 1;
7670 + }
7671 + }
7672 +}
7673 +
7674 +/* Invalidate a single cache page.
7675 + * Do this when a whole page gets written,
7676 + * ie the short cache for this page is no longer valid.
7677 + */
7678 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
7679 +{
7680 + if (object->myDev->nShortOpCaches > 0) {
7681 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
7682 +
7683 + if (cache) {
7684 + cache->object = NULL;
7685 + }
7686 + }
7687 +}
7688 +
7689 +/* Invalidate all the cache pages associated with this object
7690 + * Do this whenever ther file is deleted or resized.
7691 + */
7692 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
7693 +{
7694 + int i;
7695 + yaffs_Device *dev = in->myDev;
7696 +
7697 + if (dev->nShortOpCaches > 0) {
7698 + /* Invalidate it. */
7699 + for (i = 0; i < dev->nShortOpCaches; i++) {
7700 + if (dev->srCache[i].object == in) {
7701 + dev->srCache[i].object = NULL;
7702 + }
7703 + }
7704 + }
7705 +}
7706 +
7707 +/*--------------------- Checkpointing --------------------*/
7708 +
7709 +
7710 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
7711 +{
7712 + yaffs_CheckpointValidity cp;
7713 +
7714 + memset(&cp,0,sizeof(cp));
7715 +
7716 + cp.structType = sizeof(cp);
7717 + cp.magic = YAFFS_MAGIC;
7718 + cp.version = YAFFS_CHECKPOINT_VERSION;
7719 + cp.head = (head) ? 1 : 0;
7720 +
7721 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
7722 + 1 : 0;
7723 +}
7724 +
7725 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
7726 +{
7727 + yaffs_CheckpointValidity cp;
7728 + int ok;
7729 +
7730 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7731 +
7732 + if(ok)
7733 + ok = (cp.structType == sizeof(cp)) &&
7734 + (cp.magic == YAFFS_MAGIC) &&
7735 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
7736 + (cp.head == ((head) ? 1 : 0));
7737 + return ok ? 1 : 0;
7738 +}
7739 +
7740 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
7741 + yaffs_Device *dev)
7742 +{
7743 + cp->nErasedBlocks = dev->nErasedBlocks;
7744 + cp->allocationBlock = dev->allocationBlock;
7745 + cp->allocationPage = dev->allocationPage;
7746 + cp->nFreeChunks = dev->nFreeChunks;
7747 +
7748 + cp->nDeletedFiles = dev->nDeletedFiles;
7749 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
7750 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
7751 + cp->sequenceNumber = dev->sequenceNumber;
7752 + cp->oldestDirtySequence = dev->oldestDirtySequence;
7753 +
7754 +}
7755 +
7756 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
7757 + yaffs_CheckpointDevice *cp)
7758 +{
7759 + dev->nErasedBlocks = cp->nErasedBlocks;
7760 + dev->allocationBlock = cp->allocationBlock;
7761 + dev->allocationPage = cp->allocationPage;
7762 + dev->nFreeChunks = cp->nFreeChunks;
7763 +
7764 + dev->nDeletedFiles = cp->nDeletedFiles;
7765 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
7766 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
7767 + dev->sequenceNumber = cp->sequenceNumber;
7768 + dev->oldestDirtySequence = cp->oldestDirtySequence;
7769 +}
7770 +
7771 +
7772 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
7773 +{
7774 + yaffs_CheckpointDevice cp;
7775 + __u32 nBytes;
7776 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7777 +
7778 + int ok;
7779 +
7780 + /* Write device runtime values*/
7781 + yaffs_DeviceToCheckpointDevice(&cp,dev);
7782 + cp.structType = sizeof(cp);
7783 +
7784 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7785 +
7786 + /* Write block info */
7787 + if(ok) {
7788 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7789 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
7790 + }
7791 +
7792 + /* Write chunk bits */
7793 + if(ok) {
7794 + nBytes = nBlocks * dev->chunkBitmapStride;
7795 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
7796 + }
7797 + return ok ? 1 : 0;
7798 +
7799 +}
7800 +
7801 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
7802 +{
7803 + yaffs_CheckpointDevice cp;
7804 + __u32 nBytes;
7805 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7806 +
7807 + int ok;
7808 +
7809 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7810 + if(!ok)
7811 + return 0;
7812 +
7813 + if(cp.structType != sizeof(cp))
7814 + return 0;
7815 +
7816 +
7817 + yaffs_CheckpointDeviceToDevice(dev,&cp);
7818 +
7819 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7820 +
7821 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
7822 +
7823 + if(!ok)
7824 + return 0;
7825 + nBytes = nBlocks * dev->chunkBitmapStride;
7826 +
7827 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
7828 +
7829 + return ok ? 1 : 0;
7830 +}
7831 +
7832 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
7833 + yaffs_Object *obj)
7834 +{
7835 +
7836 + cp->objectId = obj->objectId;
7837 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
7838 + cp->chunkId = obj->chunkId;
7839 + cp->variantType = obj->variantType;
7840 + cp->deleted = obj->deleted;
7841 + cp->softDeleted = obj->softDeleted;
7842 + cp->unlinked = obj->unlinked;
7843 + cp->fake = obj->fake;
7844 + cp->renameAllowed = obj->renameAllowed;
7845 + cp->unlinkAllowed = obj->unlinkAllowed;
7846 + cp->serial = obj->serial;
7847 + cp->nDataChunks = obj->nDataChunks;
7848 +
7849 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7850 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7851 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7852 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7853 +}
7854 +
7855 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7856 +{
7857 +
7858 + yaffs_Object *parent;
7859 +
7860 + obj->objectId = cp->objectId;
7861 +
7862 + if(cp->parentId)
7863 + parent = yaffs_FindOrCreateObjectByNumber(
7864 + obj->myDev,
7865 + cp->parentId,
7866 + YAFFS_OBJECT_TYPE_DIRECTORY);
7867 + else
7868 + parent = NULL;
7869 +
7870 + if(parent)
7871 + yaffs_AddObjectToDirectory(parent, obj);
7872 +
7873 + obj->chunkId = cp->chunkId;
7874 + obj->variantType = cp->variantType;
7875 + obj->deleted = cp->deleted;
7876 + obj->softDeleted = cp->softDeleted;
7877 + obj->unlinked = cp->unlinked;
7878 + obj->fake = cp->fake;
7879 + obj->renameAllowed = cp->renameAllowed;
7880 + obj->unlinkAllowed = cp->unlinkAllowed;
7881 + obj->serial = cp->serial;
7882 + obj->nDataChunks = cp->nDataChunks;
7883 +
7884 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7885 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7886 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7887 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7888 +
7889 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7890 + obj->lazyLoaded = 1;
7891 +}
7892 +
7893 +
7894 +
7895 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7896 + __u32 level, int chunkOffset)
7897 +{
7898 + int i;
7899 + yaffs_Device *dev = in->myDev;
7900 + int ok = 1;
7901 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7902 +
7903 + if (tn) {
7904 + if (level > 0) {
7905 +
7906 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7907 + if (tn->internal[i]) {
7908 + ok = yaffs_CheckpointTnodeWorker(in,
7909 + tn->internal[i],
7910 + level - 1,
7911 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7912 + }
7913 + }
7914 + } else if (level == 0) {
7915 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7916 + /* printf("write tnode at %d\n",baseOffset); */
7917 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7918 + if(ok)
7919 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7920 + }
7921 + }
7922 +
7923 + return ok;
7924 +
7925 +}
7926 +
7927 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7928 +{
7929 + __u32 endMarker = ~0;
7930 + int ok = 1;
7931 +
7932 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7933 + ok = yaffs_CheckpointTnodeWorker(obj,
7934 + obj->variant.fileVariant.top,
7935 + obj->variant.fileVariant.topLevel,
7936 + 0);
7937 + if(ok)
7938 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7939 + sizeof(endMarker));
7940 + }
7941 +
7942 + return ok ? 1 : 0;
7943 +}
7944 +
7945 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7946 +{
7947 + __u32 baseChunk;
7948 + int ok = 1;
7949 + yaffs_Device *dev = obj->myDev;
7950 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7951 + yaffs_Tnode *tn;
7952 + int nread = 0;
7953 +
7954 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7955 +
7956 + while(ok && (~baseChunk)){
7957 + nread++;
7958 + /* Read level 0 tnode */
7959 +
7960 +
7961 + /* printf("read tnode at %d\n",baseChunk); */
7962 + tn = yaffs_GetTnodeRaw(dev);
7963 + if(tn)
7964 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7965 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7966 + else
7967 + ok = 0;
7968 +
7969 + if(tn && ok){
7970 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7971 + fileStructPtr,
7972 + baseChunk,
7973 + tn) ? 1 : 0;
7974 +
7975 + }
7976 +
7977 + if(ok)
7978 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7979 +
7980 + }
7981 +
7982 + T(YAFFS_TRACE_CHECKPOINT,(
7983 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
7984 + nread,baseChunk,ok));
7985 +
7986 + return ok ? 1 : 0;
7987 +}
7988 +
7989 +
7990 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7991 +{
7992 + yaffs_Object *obj;
7993 + yaffs_CheckpointObject cp;
7994 + int i;
7995 + int ok = 1;
7996 + struct list_head *lh;
7997 +
7998 +
7999 + /* Iterate through the objects in each hash entry,
8000 + * dumping them to the checkpointing stream.
8001 + */
8002 +
8003 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
8004 + list_for_each(lh, &dev->objectBucket[i].list) {
8005 + if (lh) {
8006 + obj = list_entry(lh, yaffs_Object, hashLink);
8007 + if (!obj->deferedFree) {
8008 + yaffs_ObjectToCheckpointObject(&cp,obj);
8009 + cp.structType = sizeof(cp);
8010 +
8011 + T(YAFFS_TRACE_CHECKPOINT,(
8012 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
8013 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
8014 +
8015 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8016 +
8017 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
8018 + ok = yaffs_WriteCheckpointTnodes(obj);
8019 + }
8020 + }
8021 + }
8022 + }
8023 + }
8024 +
8025 + /* Dump end of list */
8026 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
8027 + cp.structType = sizeof(cp);
8028 +
8029 + if(ok)
8030 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8031 +
8032 + return ok ? 1 : 0;
8033 +}
8034 +
8035 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8036 +{
8037 + yaffs_Object *obj;
8038 + yaffs_CheckpointObject cp;
8039 + int ok = 1;
8040 + int done = 0;
8041 + yaffs_Object *hardList = NULL;
8042 +
8043 + while(ok && !done) {
8044 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
8045 + if(cp.structType != sizeof(cp)) {
8046 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
8047 + cp.structType,sizeof(cp),ok));
8048 + ok = 0;
8049 + }
8050 +
8051 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8052 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
8053 +
8054 + if(ok && cp.objectId == ~0)
8055 + done = 1;
8056 + else if(ok){
8057 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
8058 + if(obj) {
8059 + yaffs_CheckpointObjectToObject(obj,&cp);
8060 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8061 + ok = yaffs_ReadCheckpointTnodes(obj);
8062 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8063 + obj->hardLinks.next =
8064 + (struct list_head *)
8065 + hardList;
8066 + hardList = obj;
8067 + }
8068 +
8069 + }
8070 + }
8071 + }
8072 +
8073 + if(ok)
8074 + yaffs_HardlinkFixup(dev,hardList);
8075 +
8076 + return ok ? 1 : 0;
8077 +}
8078 +
8079 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8080 +{
8081 + __u32 checkpointSum;
8082 + int ok;
8083 +
8084 + yaffs_GetCheckpointSum(dev,&checkpointSum);
8085 +
8086 + ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
8087 +
8088 + if(!ok)
8089 + return 0;
8090 +
8091 + return 1;
8092 +}
8093 +
8094 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8095 +{
8096 + __u32 checkpointSum0;
8097 + __u32 checkpointSum1;
8098 + int ok;
8099 +
8100 + yaffs_GetCheckpointSum(dev,&checkpointSum0);
8101 +
8102 + ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8103 +
8104 + if(!ok)
8105 + return 0;
8106 +
8107 + if(checkpointSum0 != checkpointSum1)
8108 + return 0;
8109 +
8110 + return 1;
8111 +}
8112 +
8113 +
8114 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8115 +{
8116 +
8117 + int ok = 1;
8118 +
8119 + if(dev->skipCheckpointWrite || !dev->isYaffs2){
8120 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
8121 + ok = 0;
8122 + }
8123 +
8124 + if(ok)
8125 + ok = yaffs_CheckpointOpen(dev,1);
8126 +
8127 + if(ok){
8128 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8129 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
8130 + }
8131 + if(ok){
8132 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
8133 + ok = yaffs_WriteCheckpointDevice(dev);
8134 + }
8135 + if(ok){
8136 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
8137 + ok = yaffs_WriteCheckpointObjects(dev);
8138 + }
8139 + if(ok){
8140 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8141 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
8142 + }
8143 +
8144 + if(ok){
8145 + ok = yaffs_WriteCheckpointSum(dev);
8146 + }
8147 +
8148 +
8149 + if(!yaffs_CheckpointClose(dev))
8150 + ok = 0;
8151 +
8152 + if(ok)
8153 + dev->isCheckpointed = 1;
8154 + else
8155 + dev->isCheckpointed = 0;
8156 +
8157 + return dev->isCheckpointed;
8158 +}
8159 +
8160 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8161 +{
8162 + int ok = 1;
8163 +
8164 + if(dev->skipCheckpointRead || !dev->isYaffs2){
8165 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
8166 + ok = 0;
8167 + }
8168 +
8169 + if(ok)
8170 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
8171 +
8172 + if(ok){
8173 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8174 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
8175 + }
8176 + if(ok){
8177 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
8178 + ok = yaffs_ReadCheckpointDevice(dev);
8179 + }
8180 + if(ok){
8181 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
8182 + ok = yaffs_ReadCheckpointObjects(dev);
8183 + }
8184 + if(ok){
8185 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8186 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
8187 + }
8188 +
8189 + if(ok){
8190 + ok = yaffs_ReadCheckpointSum(dev);
8191 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
8192 + }
8193 +
8194 + if(!yaffs_CheckpointClose(dev))
8195 + ok = 0;
8196 +
8197 + if(ok)
8198 + dev->isCheckpointed = 1;
8199 + else
8200 + dev->isCheckpointed = 0;
8201 +
8202 + return ok ? 1 : 0;
8203 +
8204 +}
8205 +
8206 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8207 +{
8208 + if(dev->isCheckpointed ||
8209 + dev->blocksInCheckpoint > 0){
8210 + dev->isCheckpointed = 0;
8211 + yaffs_CheckpointInvalidateStream(dev);
8212 + if(dev->superBlock && dev->markSuperBlockDirty)
8213 + dev->markSuperBlockDirty(dev->superBlock);
8214 + }
8215 +}
8216 +
8217 +
8218 +int yaffs_CheckpointSave(yaffs_Device *dev)
8219 +{
8220 +
8221 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8222 +
8223 + yaffs_VerifyObjects(dev);
8224 + yaffs_VerifyBlocks(dev);
8225 + yaffs_VerifyFreeChunks(dev);
8226 +
8227 + if(!dev->isCheckpointed) {
8228 + yaffs_InvalidateCheckpoint(dev);
8229 + yaffs_WriteCheckpointData(dev);
8230 + }
8231 +
8232 + T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8233 +
8234 + return dev->isCheckpointed;
8235 +}
8236 +
8237 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8238 +{
8239 + int retval;
8240 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8241 +
8242 + retval = yaffs_ReadCheckpointData(dev);
8243 +
8244 + if(dev->isCheckpointed){
8245 + yaffs_VerifyObjects(dev);
8246 + yaffs_VerifyBlocks(dev);
8247 + yaffs_VerifyFreeChunks(dev);
8248 + }
8249 +
8250 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8251 +
8252 + return retval;
8253 +}
8254 +
8255 +/*--------------------- File read/write ------------------------
8256 + * Read and write have very similar structures.
8257 + * In general the read/write has three parts to it
8258 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8259 + * Some complete chunks
8260 + * An incomplete chunk to end off with
8261 + *
8262 + * Curve-balls: the first chunk might also be the last chunk.
8263 + */
8264 +
8265 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
8266 + int nBytes)
8267 +{
8268 +
8269 + int chunk;
8270 + int start;
8271 + int nToCopy;
8272 + int n = nBytes;
8273 + int nDone = 0;
8274 + yaffs_ChunkCache *cache;
8275 +
8276 + yaffs_Device *dev;
8277 +
8278 + dev = in->myDev;
8279 +
8280 + while (n > 0) {
8281 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8282 + //start = offset % dev->nDataBytesPerChunk;
8283 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8284 + chunk++;
8285 +
8286 + /* OK now check for the curveball where the start and end are in
8287 + * the same chunk.
8288 + */
8289 + if ((start + n) < dev->nDataBytesPerChunk) {
8290 + nToCopy = n;
8291 + } else {
8292 + nToCopy = dev->nDataBytesPerChunk - start;
8293 + }
8294 +
8295 + cache = yaffs_FindChunkCache(in, chunk);
8296 +
8297 + /* If the chunk is already in the cache or it is less than a whole chunk
8298 + * then use the cache (if there is caching)
8299 + * else bypass the cache.
8300 + */
8301 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
8302 + if (dev->nShortOpCaches > 0) {
8303 +
8304 + /* If we can't find the data in the cache, then load it up. */
8305 +
8306 + if (!cache) {
8307 + cache = yaffs_GrabChunkCache(in->myDev);
8308 + cache->object = in;
8309 + cache->chunkId = chunk;
8310 + cache->dirty = 0;
8311 + cache->locked = 0;
8312 + yaffs_ReadChunkDataFromObject(in, chunk,
8313 + cache->
8314 + data);
8315 + cache->nBytes = 0;
8316 + }
8317 +
8318 + yaffs_UseChunkCache(dev, cache, 0);
8319 +
8320 + cache->locked = 1;
8321 +
8322 +#ifdef CONFIG_YAFFS_WINCE
8323 + yfsd_UnlockYAFFS(TRUE);
8324 +#endif
8325 + memcpy(buffer, &cache->data[start], nToCopy);
8326 +
8327 +#ifdef CONFIG_YAFFS_WINCE
8328 + yfsd_LockYAFFS(TRUE);
8329 +#endif
8330 + cache->locked = 0;
8331 + } else {
8332 + /* Read into the local buffer then copy..*/
8333 +
8334 + __u8 *localBuffer =
8335 + yaffs_GetTempBuffer(dev, __LINE__);
8336 + yaffs_ReadChunkDataFromObject(in, chunk,
8337 + localBuffer);
8338 +#ifdef CONFIG_YAFFS_WINCE
8339 + yfsd_UnlockYAFFS(TRUE);
8340 +#endif
8341 + memcpy(buffer, &localBuffer[start], nToCopy);
8342 +
8343 +#ifdef CONFIG_YAFFS_WINCE
8344 + yfsd_LockYAFFS(TRUE);
8345 +#endif
8346 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8347 + __LINE__);
8348 + }
8349 +
8350 + } else {
8351 +#ifdef CONFIG_YAFFS_WINCE
8352 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8353 +
8354 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8355 + * This is because we otherwise screw up WinCE's memory mapper
8356 + */
8357 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
8358 +
8359 +#ifdef CONFIG_YAFFS_WINCE
8360 + yfsd_UnlockYAFFS(TRUE);
8361 +#endif
8362 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
8363 +
8364 +#ifdef CONFIG_YAFFS_WINCE
8365 + yfsd_LockYAFFS(TRUE);
8366 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8367 +#endif
8368 +
8369 +#else
8370 + /* A full chunk. Read directly into the supplied buffer. */
8371 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8372 +#endif
8373 + }
8374 +
8375 + n -= nToCopy;
8376 + offset += nToCopy;
8377 + buffer += nToCopy;
8378 + nDone += nToCopy;
8379 +
8380 + }
8381 +
8382 + return nDone;
8383 +}
8384 +
8385 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
8386 + int nBytes, int writeThrough)
8387 +{
8388 +
8389 + int chunk;
8390 + int start;
8391 + int nToCopy;
8392 + int n = nBytes;
8393 + int nDone = 0;
8394 + int nToWriteBack;
8395 + int startOfWrite = offset;
8396 + int chunkWritten = 0;
8397 + int nBytesRead;
8398 +
8399 + yaffs_Device *dev;
8400 +
8401 + dev = in->myDev;
8402 +
8403 + while (n > 0 && chunkWritten >= 0) {
8404 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8405 + //start = offset % dev->nDataBytesPerChunk;
8406 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8407 + chunk++;
8408 +
8409 + /* OK now check for the curveball where the start and end are in
8410 + * the same chunk.
8411 + */
8412 +
8413 + if ((start + n) < dev->nDataBytesPerChunk) {
8414 + nToCopy = n;
8415 +
8416 + /* Now folks, to calculate how many bytes to write back....
8417 + * If we're overwriting and not writing to then end of file then
8418 + * we need to write back as much as was there before.
8419 + */
8420 +
8421 + nBytesRead =
8422 + in->variant.fileVariant.fileSize -
8423 + ((chunk - 1) * dev->nDataBytesPerChunk);
8424 +
8425 + if (nBytesRead > dev->nDataBytesPerChunk) {
8426 + nBytesRead = dev->nDataBytesPerChunk;
8427 + }
8428 +
8429 + nToWriteBack =
8430 + (nBytesRead >
8431 + (start + n)) ? nBytesRead : (start + n);
8432 +
8433 + } else {
8434 + nToCopy = dev->nDataBytesPerChunk - start;
8435 + nToWriteBack = dev->nDataBytesPerChunk;
8436 + }
8437 +
8438 + if (nToCopy != dev->nDataBytesPerChunk) {
8439 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
8440 + if (dev->nShortOpCaches > 0) {
8441 + yaffs_ChunkCache *cache;
8442 + /* If we can't find the data in the cache, then load the cache */
8443 + cache = yaffs_FindChunkCache(in, chunk);
8444 +
8445 + if (!cache
8446 + && yaffs_CheckSpaceForAllocation(in->
8447 + myDev)) {
8448 + cache = yaffs_GrabChunkCache(in->myDev);
8449 + cache->object = in;
8450 + cache->chunkId = chunk;
8451 + cache->dirty = 0;
8452 + cache->locked = 0;
8453 + yaffs_ReadChunkDataFromObject(in, chunk,
8454 + cache->
8455 + data);
8456 + }
8457 + else if(cache &&
8458 + !cache->dirty &&
8459 + !yaffs_CheckSpaceForAllocation(in->myDev)){
8460 + /* Drop the cache if it was a read cache item and
8461 + * no space check has been made for it.
8462 + */
8463 + cache = NULL;
8464 + }
8465 +
8466 + if (cache) {
8467 + yaffs_UseChunkCache(dev, cache, 1);
8468 + cache->locked = 1;
8469 +#ifdef CONFIG_YAFFS_WINCE
8470 + yfsd_UnlockYAFFS(TRUE);
8471 +#endif
8472 +
8473 + memcpy(&cache->data[start], buffer,
8474 + nToCopy);
8475 +
8476 +#ifdef CONFIG_YAFFS_WINCE
8477 + yfsd_LockYAFFS(TRUE);
8478 +#endif
8479 + cache->locked = 0;
8480 + cache->nBytes = nToWriteBack;
8481 +
8482 + if (writeThrough) {
8483 + chunkWritten =
8484 + yaffs_WriteChunkDataToObject
8485 + (cache->object,
8486 + cache->chunkId,
8487 + cache->data, cache->nBytes,
8488 + 1);
8489 + cache->dirty = 0;
8490 + }
8491 +
8492 + } else {
8493 + chunkWritten = -1; /* fail the write */
8494 + }
8495 + } else {
8496 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8497 + * Read into the local buffer then copy, then copy over and write back.
8498 + */
8499 +
8500 + __u8 *localBuffer =
8501 + yaffs_GetTempBuffer(dev, __LINE__);
8502 +
8503 + yaffs_ReadChunkDataFromObject(in, chunk,
8504 + localBuffer);
8505 +
8506 +#ifdef CONFIG_YAFFS_WINCE
8507 + yfsd_UnlockYAFFS(TRUE);
8508 +#endif
8509 +
8510 + memcpy(&localBuffer[start], buffer, nToCopy);
8511 +
8512 +#ifdef CONFIG_YAFFS_WINCE
8513 + yfsd_LockYAFFS(TRUE);
8514 +#endif
8515 + chunkWritten =
8516 + yaffs_WriteChunkDataToObject(in, chunk,
8517 + localBuffer,
8518 + nToWriteBack,
8519 + 0);
8520 +
8521 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8522 + __LINE__);
8523 +
8524 + }
8525 +
8526 + } else {
8527 +
8528 +#ifdef CONFIG_YAFFS_WINCE
8529 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8530 + * This is because we otherwise screw up WinCE's memory mapper
8531 + */
8532 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8533 +#ifdef CONFIG_YAFFS_WINCE
8534 + yfsd_UnlockYAFFS(TRUE);
8535 +#endif
8536 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
8537 +#ifdef CONFIG_YAFFS_WINCE
8538 + yfsd_LockYAFFS(TRUE);
8539 +#endif
8540 + chunkWritten =
8541 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
8542 + dev->nDataBytesPerChunk,
8543 + 0);
8544 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8545 +#else
8546 + /* A full chunk. Write directly from the supplied buffer. */
8547 + chunkWritten =
8548 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8549 + dev->nDataBytesPerChunk,
8550 + 0);
8551 +#endif
8552 + /* Since we've overwritten the cached data, we better invalidate it. */
8553 + yaffs_InvalidateChunkCache(in, chunk);
8554 + }
8555 +
8556 + if (chunkWritten >= 0) {
8557 + n -= nToCopy;
8558 + offset += nToCopy;
8559 + buffer += nToCopy;
8560 + nDone += nToCopy;
8561 + }
8562 +
8563 + }
8564 +
8565 + /* Update file object */
8566 +
8567 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
8568 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8569 + }
8570 +
8571 + in->dirty = 1;
8572 +
8573 + return nDone;
8574 +}
8575 +
8576 +
8577 +/* ---------------------- File resizing stuff ------------------ */
8578 +
8579 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
8580 +{
8581 +
8582 + yaffs_Device *dev = in->myDev;
8583 + int oldFileSize = in->variant.fileVariant.fileSize;
8584 +
8585 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8586 +
8587 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8588 + dev->nDataBytesPerChunk;
8589 + int i;
8590 + int chunkId;
8591 +
8592 + /* Delete backwards so that we don't end up with holes if
8593 + * power is lost part-way through the operation.
8594 + */
8595 + for (i = lastDel; i >= startDel; i--) {
8596 + /* NB this could be optimised somewhat,
8597 + * eg. could retrieve the tags and write them without
8598 + * using yaffs_DeleteChunk
8599 + */
8600 +
8601 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8602 + if (chunkId > 0) {
8603 + if (chunkId <
8604 + (dev->internalStartBlock * dev->nChunksPerBlock)
8605 + || chunkId >=
8606 + ((dev->internalEndBlock +
8607 + 1) * dev->nChunksPerBlock)) {
8608 + T(YAFFS_TRACE_ALWAYS,
8609 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8610 + chunkId, i));
8611 + } else {
8612 + in->nDataChunks--;
8613 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8614 + }
8615 + }
8616 + }
8617 +
8618 +}
8619 +
8620 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
8621 +{
8622 +
8623 + int oldFileSize = in->variant.fileVariant.fileSize;
8624 + int newSizeOfPartialChunk;
8625 + int newFullChunks;
8626 +
8627 + yaffs_Device *dev = in->myDev;
8628 +
8629 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8630 +
8631 + yaffs_FlushFilesChunkCache(in);
8632 + yaffs_InvalidateWholeChunkCache(in);
8633 +
8634 + yaffs_CheckGarbageCollection(dev);
8635 +
8636 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8637 + return yaffs_GetFileSize(in);
8638 + }
8639 +
8640 + if (newSize == oldFileSize) {
8641 + return oldFileSize;
8642 + }
8643 +
8644 + if (newSize < oldFileSize) {
8645 +
8646 + yaffs_PruneResizedChunks(in, newSize);
8647 +
8648 + if (newSizeOfPartialChunk != 0) {
8649 + int lastChunk = 1 + newFullChunks;
8650 +
8651 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8652 +
8653 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8654 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8655 + localBuffer);
8656 +
8657 + memset(localBuffer + newSizeOfPartialChunk, 0,
8658 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8659 +
8660 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8661 + newSizeOfPartialChunk, 1);
8662 +
8663 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8664 + }
8665 +
8666 + in->variant.fileVariant.fileSize = newSize;
8667 +
8668 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
8669 + } else {
8670 + /* newsSize > oldFileSize */
8671 + in->variant.fileVariant.fileSize = newSize;
8672 + }
8673 +
8674 +
8675 +
8676 + /* Write a new object header.
8677 + * show we've shrunk the file, if need be
8678 + * Do this only if the file is not in the deleted directories.
8679 + */
8680 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
8681 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
8682 + yaffs_UpdateObjectHeader(in, NULL, 0,
8683 + (newSize < oldFileSize) ? 1 : 0, 0);
8684 + }
8685 +
8686 + return newSize;
8687 +}
8688 +
8689 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
8690 +{
8691 + obj = yaffs_GetEquivalentObject(obj);
8692 +
8693 + switch (obj->variantType) {
8694 + case YAFFS_OBJECT_TYPE_FILE:
8695 + return obj->variant.fileVariant.fileSize;
8696 + case YAFFS_OBJECT_TYPE_SYMLINK:
8697 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
8698 + default:
8699 + return 0;
8700 + }
8701 +}
8702 +
8703 +
8704 +
8705 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
8706 +{
8707 + int retVal;
8708 + if (in->dirty) {
8709 + yaffs_FlushFilesChunkCache(in);
8710 + if (updateTime) {
8711 +#ifdef CONFIG_YAFFS_WINCE
8712 + yfsd_WinFileTimeNow(in->win_mtime);
8713 +#else
8714 +
8715 + in->yst_mtime = Y_CURRENT_TIME;
8716 +
8717 +#endif
8718 + }
8719 +
8720 + retVal =
8721 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
8722 + 0) ? YAFFS_OK : YAFFS_FAIL;
8723 + } else {
8724 + retVal = YAFFS_OK;
8725 + }
8726 +
8727 + return retVal;
8728 +
8729 +}
8730 +
8731 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
8732 +{
8733 +
8734 + /* First off, invalidate the file's data in the cache, without flushing. */
8735 + yaffs_InvalidateWholeChunkCache(in);
8736 +
8737 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
8738 + /* Move to the unlinked directory so we have a record that it was deleted. */
8739 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
8740 +
8741 + }
8742 +
8743 + yaffs_RemoveObjectFromDirectory(in);
8744 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
8745 + in->chunkId = -1;
8746 +
8747 + yaffs_FreeObject(in);
8748 + return YAFFS_OK;
8749 +
8750 +}
8751 +
8752 +/* yaffs_DeleteFile deletes the whole file data
8753 + * and the inode associated with the file.
8754 + * It does not delete the links associated with the file.
8755 + */
8756 +static int yaffs_UnlinkFile(yaffs_Object * in)
8757 +{
8758 +
8759 + int retVal;
8760 + int immediateDeletion = 0;
8761 +
8762 + if (1) {
8763 +#ifdef __KERNEL__
8764 + if (!in->myInode) {
8765 + immediateDeletion = 1;
8766 +
8767 + }
8768 +#else
8769 + if (in->inUse <= 0) {
8770 + immediateDeletion = 1;
8771 +
8772 + }
8773 +#endif
8774 + if (immediateDeletion) {
8775 + retVal =
8776 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
8777 + "deleted", 0, 0);
8778 + T(YAFFS_TRACE_TRACING,
8779 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
8780 + in->objectId));
8781 + in->deleted = 1;
8782 + in->myDev->nDeletedFiles++;
8783 + if (0 && in->myDev->isYaffs2) {
8784 + yaffs_ResizeFile(in, 0);
8785 + }
8786 + yaffs_SoftDeleteFile(in);
8787 + } else {
8788 + retVal =
8789 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
8790 + "unlinked", 0, 0);
8791 + }
8792 +
8793 + }
8794 + return retVal;
8795 +}
8796 +
8797 +int yaffs_DeleteFile(yaffs_Object * in)
8798 +{
8799 + int retVal = YAFFS_OK;
8800 +
8801 + if (in->nDataChunks > 0) {
8802 + /* Use soft deletion if there is data in the file */
8803 + if (!in->unlinked) {
8804 + retVal = yaffs_UnlinkFile(in);
8805 + }
8806 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
8807 + in->deleted = 1;
8808 + in->myDev->nDeletedFiles++;
8809 + yaffs_SoftDeleteFile(in);
8810 + }
8811 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
8812 + } else {
8813 + /* The file has no data chunks so we toss it immediately */
8814 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
8815 + in->variant.fileVariant.top = NULL;
8816 + yaffs_DoGenericObjectDeletion(in);
8817 +
8818 + return YAFFS_OK;
8819 + }
8820 +}
8821 +
8822 +static int yaffs_DeleteDirectory(yaffs_Object * in)
8823 +{
8824 + /* First check that the directory is empty. */
8825 + if (list_empty(&in->variant.directoryVariant.children)) {
8826 + return yaffs_DoGenericObjectDeletion(in);
8827 + }
8828 +
8829 + return YAFFS_FAIL;
8830 +
8831 +}
8832 +
8833 +static int yaffs_DeleteSymLink(yaffs_Object * in)
8834 +{
8835 + YFREE(in->variant.symLinkVariant.alias);
8836 +
8837 + return yaffs_DoGenericObjectDeletion(in);
8838 +}
8839 +
8840 +static int yaffs_DeleteHardLink(yaffs_Object * in)
8841 +{
8842 + /* remove this hardlink from the list assocaited with the equivalent
8843 + * object
8844 + */
8845 + list_del(&in->hardLinks);
8846 + return yaffs_DoGenericObjectDeletion(in);
8847 +}
8848 +
8849 +static void yaffs_DestroyObject(yaffs_Object * obj)
8850 +{
8851 + switch (obj->variantType) {
8852 + case YAFFS_OBJECT_TYPE_FILE:
8853 + yaffs_DeleteFile(obj);
8854 + break;
8855 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8856 + yaffs_DeleteDirectory(obj);
8857 + break;
8858 + case YAFFS_OBJECT_TYPE_SYMLINK:
8859 + yaffs_DeleteSymLink(obj);
8860 + break;
8861 + case YAFFS_OBJECT_TYPE_HARDLINK:
8862 + yaffs_DeleteHardLink(obj);
8863 + break;
8864 + case YAFFS_OBJECT_TYPE_SPECIAL:
8865 + yaffs_DoGenericObjectDeletion(obj);
8866 + break;
8867 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8868 + break; /* should not happen. */
8869 + }
8870 +}
8871 +
8872 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
8873 +{
8874 +
8875 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8876 + return yaffs_DeleteHardLink(obj);
8877 + } else if (!list_empty(&obj->hardLinks)) {
8878 + /* Curve ball: We're unlinking an object that has a hardlink.
8879 + *
8880 + * This problem arises because we are not strictly following
8881 + * The Linux link/inode model.
8882 + *
8883 + * We can't really delete the object.
8884 + * Instead, we do the following:
8885 + * - Select a hardlink.
8886 + * - Unhook it from the hard links
8887 + * - Unhook it from its parent directory (so that the rename can work)
8888 + * - Rename the object to the hardlink's name.
8889 + * - Delete the hardlink
8890 + */
8891 +
8892 + yaffs_Object *hl;
8893 + int retVal;
8894 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
8895 +
8896 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
8897 +
8898 + list_del_init(&hl->hardLinks);
8899 + list_del_init(&hl->siblings);
8900 +
8901 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
8902 +
8903 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
8904 +
8905 + if (retVal == YAFFS_OK) {
8906 + retVal = yaffs_DoGenericObjectDeletion(hl);
8907 + }
8908 + return retVal;
8909 +
8910 + } else {
8911 + switch (obj->variantType) {
8912 + case YAFFS_OBJECT_TYPE_FILE:
8913 + return yaffs_UnlinkFile(obj);
8914 + break;
8915 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8916 + return yaffs_DeleteDirectory(obj);
8917 + break;
8918 + case YAFFS_OBJECT_TYPE_SYMLINK:
8919 + return yaffs_DeleteSymLink(obj);
8920 + break;
8921 + case YAFFS_OBJECT_TYPE_SPECIAL:
8922 + return yaffs_DoGenericObjectDeletion(obj);
8923 + break;
8924 + case YAFFS_OBJECT_TYPE_HARDLINK:
8925 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8926 + default:
8927 + return YAFFS_FAIL;
8928 + }
8929 + }
8930 +}
8931 +
8932 +
8933 +static int yaffs_UnlinkObject( yaffs_Object *obj)
8934 +{
8935 +
8936 + if (obj && obj->unlinkAllowed) {
8937 + return yaffs_UnlinkWorker(obj);
8938 + }
8939 +
8940 + return YAFFS_FAIL;
8941 +
8942 +}
8943 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8944 +{
8945 + yaffs_Object *obj;
8946 +
8947 + obj = yaffs_FindObjectByName(dir, name);
8948 + return yaffs_UnlinkObject(obj);
8949 +}
8950 +
8951 +/*----------------------- Initialisation Scanning ---------------------- */
8952 +
8953 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8954 + int backwardScanning)
8955 +{
8956 + yaffs_Object *obj;
8957 +
8958 + if (!backwardScanning) {
8959 + /* Handle YAFFS1 forward scanning case
8960 + * For YAFFS1 we always do the deletion
8961 + */
8962 +
8963 + } else {
8964 + /* Handle YAFFS2 case (backward scanning)
8965 + * If the shadowed object exists then ignore.
8966 + */
8967 + if (yaffs_FindObjectByNumber(dev, objId)) {
8968 + return;
8969 + }
8970 + }
8971 +
8972 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8973 + * We put it in unlinked dir to be cleaned up after the scanning
8974 + */
8975 + obj =
8976 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8977 + YAFFS_OBJECT_TYPE_FILE);
8978 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8979 + obj->variant.fileVariant.shrinkSize = 0;
8980 + obj->valid = 1; /* So that we don't read any other info for this file */
8981 +
8982 +}
8983 +
8984 +typedef struct {
8985 + int seq;
8986 + int block;
8987 +} yaffs_BlockIndex;
8988 +
8989 +
8990 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8991 +{
8992 + yaffs_Object *hl;
8993 + yaffs_Object *in;
8994 +
8995 + while (hardList) {
8996 + hl = hardList;
8997 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8998 +
8999 + in = yaffs_FindObjectByNumber(dev,
9000 + hl->variant.hardLinkVariant.
9001 + equivalentObjectId);
9002 +
9003 + if (in) {
9004 + /* Add the hardlink pointers */
9005 + hl->variant.hardLinkVariant.equivalentObject = in;
9006 + list_add(&hl->hardLinks, &in->hardLinks);
9007 + } else {
9008 + /* Todo Need to report/handle this better.
9009 + * Got a problem... hardlink to a non-existant object
9010 + */
9011 + hl->variant.hardLinkVariant.equivalentObject = NULL;
9012 + INIT_LIST_HEAD(&hl->hardLinks);
9013 +
9014 + }
9015 +
9016 + }
9017 +
9018 +}
9019 +
9020 +
9021 +
9022 +
9023 +
9024 +static int ybicmp(const void *a, const void *b){
9025 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
9026 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
9027 + register int ablock = ((yaffs_BlockIndex *)a)->block;
9028 + register int bblock = ((yaffs_BlockIndex *)b)->block;
9029 + if( aseq == bseq )
9030 + return ablock - bblock;
9031 + else
9032 + return aseq - bseq;
9033 +
9034 +}
9035 +
9036 +static int yaffs_Scan(yaffs_Device * dev)
9037 +{
9038 + yaffs_ExtendedTags tags;
9039 + int blk;
9040 + int blockIterator;
9041 + int startIterator;
9042 + int endIterator;
9043 + int nBlocksToScan = 0;
9044 + int result;
9045 +
9046 + int chunk;
9047 + int c;
9048 + int deleted;
9049 + yaffs_BlockState state;
9050 + yaffs_Object *hardList = NULL;
9051 + yaffs_BlockInfo *bi;
9052 + int sequenceNumber;
9053 + yaffs_ObjectHeader *oh;
9054 + yaffs_Object *in;
9055 + yaffs_Object *parent;
9056 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9057 +
9058 + int alloc_failed = 0;
9059 +
9060 +
9061 + __u8 *chunkData;
9062 +
9063 + yaffs_BlockIndex *blockIndex = NULL;
9064 +
9065 + if (dev->isYaffs2) {
9066 + T(YAFFS_TRACE_SCAN,
9067 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
9068 + return YAFFS_FAIL;
9069 + }
9070 +
9071 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
9072 +
9073 + T(YAFFS_TRACE_SCAN,
9074 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9075 + dev->internalStartBlock, dev->internalEndBlock));
9076 +
9077 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9078 +
9079 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9080 +
9081 + if (dev->isYaffs2) {
9082 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9083 + if(!blockIndex)
9084 + return YAFFS_FAIL;
9085 + }
9086 +
9087 + /* Scan all the blocks to determine their state */
9088 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9089 + bi = yaffs_GetBlockInfo(dev, blk);
9090 + yaffs_ClearChunkBits(dev, blk);
9091 + bi->pagesInUse = 0;
9092 + bi->softDeletions = 0;
9093 +
9094 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9095 +
9096 + bi->blockState = state;
9097 + bi->sequenceNumber = sequenceNumber;
9098 +
9099 + T(YAFFS_TRACE_SCAN_DEBUG,
9100 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9101 + state, sequenceNumber));
9102 +
9103 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9104 + T(YAFFS_TRACE_BAD_BLOCKS,
9105 + (TSTR("block %d is bad" TENDSTR), blk));
9106 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9107 + T(YAFFS_TRACE_SCAN_DEBUG,
9108 + (TSTR("Block empty " TENDSTR)));
9109 + dev->nErasedBlocks++;
9110 + dev->nFreeChunks += dev->nChunksPerBlock;
9111 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9112 +
9113 + /* Determine the highest sequence number */
9114 + if (dev->isYaffs2 &&
9115 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9116 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9117 +
9118 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9119 + blockIndex[nBlocksToScan].block = blk;
9120 +
9121 + nBlocksToScan++;
9122 +
9123 + if (sequenceNumber >= dev->sequenceNumber) {
9124 + dev->sequenceNumber = sequenceNumber;
9125 + }
9126 + } else if (dev->isYaffs2) {
9127 + /* TODO: Nasty sequence number! */
9128 + T(YAFFS_TRACE_SCAN,
9129 + (TSTR
9130 + ("Block scanning block %d has bad sequence number %d"
9131 + TENDSTR), blk, sequenceNumber));
9132 +
9133 + }
9134 + }
9135 + }
9136 +
9137 + /* Sort the blocks
9138 + * Dungy old bubble sort for now...
9139 + */
9140 + if (dev->isYaffs2) {
9141 + yaffs_BlockIndex temp;
9142 + int i;
9143 + int j;
9144 +
9145 + for (i = 0; i < nBlocksToScan; i++)
9146 + for (j = i + 1; j < nBlocksToScan; j++)
9147 + if (blockIndex[i].seq > blockIndex[j].seq) {
9148 + temp = blockIndex[j];
9149 + blockIndex[j] = blockIndex[i];
9150 + blockIndex[i] = temp;
9151 + }
9152 + }
9153 +
9154 + /* Now scan the blocks looking at the data. */
9155 + if (dev->isYaffs2) {
9156 + startIterator = 0;
9157 + endIterator = nBlocksToScan - 1;
9158 + T(YAFFS_TRACE_SCAN_DEBUG,
9159 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9160 + } else {
9161 + startIterator = dev->internalStartBlock;
9162 + endIterator = dev->internalEndBlock;
9163 + }
9164 +
9165 + /* For each block.... */
9166 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9167 + blockIterator++) {
9168 +
9169 + if (dev->isYaffs2) {
9170 + /* get the block to scan in the correct order */
9171 + blk = blockIndex[blockIterator].block;
9172 + } else {
9173 + blk = blockIterator;
9174 + }
9175 +
9176 + bi = yaffs_GetBlockInfo(dev, blk);
9177 + state = bi->blockState;
9178 +
9179 + deleted = 0;
9180 +
9181 + /* For each chunk in each block that needs scanning....*/
9182 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9183 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9184 + /* Read the tags and decide what to do */
9185 + chunk = blk * dev->nChunksPerBlock + c;
9186 +
9187 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9188 + &tags);
9189 +
9190 + /* Let's have a good look at this chunk... */
9191 +
9192 + if (!dev->isYaffs2 && tags.chunkDeleted) {
9193 + /* YAFFS1 only...
9194 + * A deleted chunk
9195 + */
9196 + deleted++;
9197 + dev->nFreeChunks++;
9198 + /*T((" %d %d deleted\n",blk,c)); */
9199 + } else if (!tags.chunkUsed) {
9200 + /* An unassigned chunk in the block
9201 + * This means that either the block is empty or
9202 + * this is the one being allocated from
9203 + */
9204 +
9205 + if (c == 0) {
9206 + /* We're looking at the first chunk in the block so the block is unused */
9207 + state = YAFFS_BLOCK_STATE_EMPTY;
9208 + dev->nErasedBlocks++;
9209 + } else {
9210 + /* this is the block being allocated from */
9211 + T(YAFFS_TRACE_SCAN,
9212 + (TSTR
9213 + (" Allocating from %d %d" TENDSTR),
9214 + blk, c));
9215 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9216 + dev->allocationBlock = blk;
9217 + dev->allocationPage = c;
9218 + dev->allocationBlockFinder = blk;
9219 + /* Set it to here to encourage the allocator to go forth from here. */
9220 +
9221 + /* Yaffs2 sanity check:
9222 + * This should be the one with the highest sequence number
9223 + */
9224 + if (dev->isYaffs2
9225 + && (dev->sequenceNumber !=
9226 + bi->sequenceNumber)) {
9227 + T(YAFFS_TRACE_ALWAYS,
9228 + (TSTR
9229 + ("yaffs: Allocation block %d was not highest sequence id:"
9230 + " block seq = %d, dev seq = %d"
9231 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
9232 + }
9233 + }
9234 +
9235 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9236 + } else if (tags.chunkId > 0) {
9237 + /* chunkId > 0 so it is a data chunk... */
9238 + unsigned int endpos;
9239 +
9240 + yaffs_SetChunkBit(dev, blk, c);
9241 + bi->pagesInUse++;
9242 +
9243 + in = yaffs_FindOrCreateObjectByNumber(dev,
9244 + tags.
9245 + objectId,
9246 + YAFFS_OBJECT_TYPE_FILE);
9247 + /* PutChunkIntoFile checks for a clash (two data chunks with
9248 + * the same chunkId).
9249 + */
9250 +
9251 + if(!in)
9252 + alloc_failed = 1;
9253 +
9254 + if(in){
9255 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
9256 + alloc_failed = 1;
9257 + }
9258 +
9259 + endpos =
9260 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9261 + tags.byteCount;
9262 + if (in &&
9263 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9264 + && in->variant.fileVariant.scannedFileSize <
9265 + endpos) {
9266 + in->variant.fileVariant.
9267 + scannedFileSize = endpos;
9268 + if (!dev->useHeaderFileSize) {
9269 + in->variant.fileVariant.
9270 + fileSize =
9271 + in->variant.fileVariant.
9272 + scannedFileSize;
9273 + }
9274 +
9275 + }
9276 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9277 + } else {
9278 + /* chunkId == 0, so it is an ObjectHeader.
9279 + * Thus, we read in the object header and make the object
9280 + */
9281 + yaffs_SetChunkBit(dev, blk, c);
9282 + bi->pagesInUse++;
9283 +
9284 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9285 + chunkData,
9286 + NULL);
9287 +
9288 + oh = (yaffs_ObjectHeader *) chunkData;
9289 +
9290 + in = yaffs_FindObjectByNumber(dev,
9291 + tags.objectId);
9292 + if (in && in->variantType != oh->type) {
9293 + /* This should not happen, but somehow
9294 + * Wev'e ended up with an objectId that has been reused but not yet
9295 + * deleted, and worse still it has changed type. Delete the old object.
9296 + */
9297 +
9298 + yaffs_DestroyObject(in);
9299 +
9300 + in = 0;
9301 + }
9302 +
9303 + in = yaffs_FindOrCreateObjectByNumber(dev,
9304 + tags.
9305 + objectId,
9306 + oh->type);
9307 +
9308 + if(!in)
9309 + alloc_failed = 1;
9310 +
9311 + if (in && oh->shadowsObject > 0) {
9312 + yaffs_HandleShadowedObject(dev,
9313 + oh->
9314 + shadowsObject,
9315 + 0);
9316 + }
9317 +
9318 + if (in && in->valid) {
9319 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9320 +
9321 + unsigned existingSerial = in->serial;
9322 + unsigned newSerial = tags.serialNumber;
9323 +
9324 + if (dev->isYaffs2 ||
9325 + ((existingSerial + 1) & 3) ==
9326 + newSerial) {
9327 + /* Use new one - destroy the exisiting one */
9328 + yaffs_DeleteChunk(dev,
9329 + in->chunkId,
9330 + 1, __LINE__);
9331 + in->valid = 0;
9332 + } else {
9333 + /* Use existing - destroy this one. */
9334 + yaffs_DeleteChunk(dev, chunk, 1,
9335 + __LINE__);
9336 + }
9337 + }
9338 +
9339 + if (in && !in->valid &&
9340 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9341 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9342 + /* We only load some info, don't fiddle with directory structure */
9343 + in->valid = 1;
9344 + in->variantType = oh->type;
9345 +
9346 + in->yst_mode = oh->yst_mode;
9347 +#ifdef CONFIG_YAFFS_WINCE
9348 + in->win_atime[0] = oh->win_atime[0];
9349 + in->win_ctime[0] = oh->win_ctime[0];
9350 + in->win_mtime[0] = oh->win_mtime[0];
9351 + in->win_atime[1] = oh->win_atime[1];
9352 + in->win_ctime[1] = oh->win_ctime[1];
9353 + in->win_mtime[1] = oh->win_mtime[1];
9354 +#else
9355 + in->yst_uid = oh->yst_uid;
9356 + in->yst_gid = oh->yst_gid;
9357 + in->yst_atime = oh->yst_atime;
9358 + in->yst_mtime = oh->yst_mtime;
9359 + in->yst_ctime = oh->yst_ctime;
9360 + in->yst_rdev = oh->yst_rdev;
9361 +#endif
9362 + in->chunkId = chunk;
9363 +
9364 + } else if (in && !in->valid) {
9365 + /* we need to load this info */
9366 +
9367 + in->valid = 1;
9368 + in->variantType = oh->type;
9369 +
9370 + in->yst_mode = oh->yst_mode;
9371 +#ifdef CONFIG_YAFFS_WINCE
9372 + in->win_atime[0] = oh->win_atime[0];
9373 + in->win_ctime[0] = oh->win_ctime[0];
9374 + in->win_mtime[0] = oh->win_mtime[0];
9375 + in->win_atime[1] = oh->win_atime[1];
9376 + in->win_ctime[1] = oh->win_ctime[1];
9377 + in->win_mtime[1] = oh->win_mtime[1];
9378 +#else
9379 + in->yst_uid = oh->yst_uid;
9380 + in->yst_gid = oh->yst_gid;
9381 + in->yst_atime = oh->yst_atime;
9382 + in->yst_mtime = oh->yst_mtime;
9383 + in->yst_ctime = oh->yst_ctime;
9384 + in->yst_rdev = oh->yst_rdev;
9385 +#endif
9386 + in->chunkId = chunk;
9387 +
9388 + yaffs_SetObjectName(in, oh->name);
9389 + in->dirty = 0;
9390 +
9391 + /* directory stuff...
9392 + * hook up to parent
9393 + */
9394 +
9395 + parent =
9396 + yaffs_FindOrCreateObjectByNumber
9397 + (dev, oh->parentObjectId,
9398 + YAFFS_OBJECT_TYPE_DIRECTORY);
9399 + if (parent->variantType ==
9400 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9401 + /* Set up as a directory */
9402 + parent->variantType =
9403 + YAFFS_OBJECT_TYPE_DIRECTORY;
9404 + INIT_LIST_HEAD(&parent->variant.
9405 + directoryVariant.
9406 + children);
9407 + } else if (parent->variantType !=
9408 + YAFFS_OBJECT_TYPE_DIRECTORY)
9409 + {
9410 + /* Hoosterman, another problem....
9411 + * We're trying to use a non-directory as a directory
9412 + */
9413 +
9414 + T(YAFFS_TRACE_ERROR,
9415 + (TSTR
9416 + ("yaffs tragedy: attempting to use non-directory as"
9417 + " a directory in scan. Put in lost+found."
9418 + TENDSTR)));
9419 + parent = dev->lostNFoundDir;
9420 + }
9421 +
9422 + yaffs_AddObjectToDirectory(parent, in);
9423 +
9424 + if (0 && (parent == dev->deletedDir ||
9425 + parent == dev->unlinkedDir)) {
9426 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9427 + dev->nDeletedFiles++;
9428 + }
9429 + /* Note re hardlinks.
9430 + * Since we might scan a hardlink before its equivalent object is scanned
9431 + * we put them all in a list.
9432 + * After scanning is complete, we should have all the objects, so we run through this
9433 + * list and fix up all the chains.
9434 + */
9435 +
9436 + switch (in->variantType) {
9437 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9438 + /* Todo got a problem */
9439 + break;
9440 + case YAFFS_OBJECT_TYPE_FILE:
9441 + if (dev->isYaffs2
9442 + && oh->isShrink) {
9443 + /* Prune back the shrunken chunks */
9444 + yaffs_PruneResizedChunks
9445 + (in, oh->fileSize);
9446 + /* Mark the block as having a shrinkHeader */
9447 + bi->hasShrinkHeader = 1;
9448 + }
9449 +
9450 + if (dev->useHeaderFileSize)
9451 +
9452 + in->variant.fileVariant.
9453 + fileSize =
9454 + oh->fileSize;
9455 +
9456 + break;
9457 + case YAFFS_OBJECT_TYPE_HARDLINK:
9458 + in->variant.hardLinkVariant.
9459 + equivalentObjectId =
9460 + oh->equivalentObjectId;
9461 + in->hardLinks.next =
9462 + (struct list_head *)
9463 + hardList;
9464 + hardList = in;
9465 + break;
9466 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9467 + /* Do nothing */
9468 + break;
9469 + case YAFFS_OBJECT_TYPE_SPECIAL:
9470 + /* Do nothing */
9471 + break;
9472 + case YAFFS_OBJECT_TYPE_SYMLINK:
9473 + in->variant.symLinkVariant.alias =
9474 + yaffs_CloneString(oh->alias);
9475 + if(!in->variant.symLinkVariant.alias)
9476 + alloc_failed = 1;
9477 + break;
9478 + }
9479 +
9480 + if (parent == dev->deletedDir) {
9481 + yaffs_DestroyObject(in);
9482 + bi->hasShrinkHeader = 1;
9483 + }
9484 + }
9485 + }
9486 + }
9487 +
9488 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9489 + /* If we got this far while scanning, then the block is fully allocated.*/
9490 + state = YAFFS_BLOCK_STATE_FULL;
9491 + }
9492 +
9493 + bi->blockState = state;
9494 +
9495 + /* Now let's see if it was dirty */
9496 + if (bi->pagesInUse == 0 &&
9497 + !bi->hasShrinkHeader &&
9498 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9499 + yaffs_BlockBecameDirty(dev, blk);
9500 + }
9501 +
9502 + }
9503 +
9504 + if (blockIndex) {
9505 + YFREE(blockIndex);
9506 + }
9507 +
9508 +
9509 + /* Ok, we've done all the scanning.
9510 + * Fix up the hard link chains.
9511 + * We should now have scanned all the objects, now it's time to add these
9512 + * hardlinks.
9513 + */
9514 +
9515 + yaffs_HardlinkFixup(dev,hardList);
9516 +
9517 + /* Handle the unlinked files. Since they were left in an unlinked state we should
9518 + * just delete them.
9519 + */
9520 + {
9521 + struct list_head *i;
9522 + struct list_head *n;
9523 +
9524 + yaffs_Object *l;
9525 + /* Soft delete all the unlinked files */
9526 + list_for_each_safe(i, n,
9527 + &dev->unlinkedDir->variant.directoryVariant.
9528 + children) {
9529 + if (i) {
9530 + l = list_entry(i, yaffs_Object, siblings);
9531 + yaffs_DestroyObject(l);
9532 + }
9533 + }
9534 + }
9535 +
9536 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9537 +
9538 + if(alloc_failed){
9539 + return YAFFS_FAIL;
9540 + }
9541 +
9542 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9543 +
9544 +
9545 + return YAFFS_OK;
9546 +}
9547 +
9548 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9549 +{
9550 + __u8 *chunkData;
9551 + yaffs_ObjectHeader *oh;
9552 + yaffs_Device *dev = in->myDev;
9553 + yaffs_ExtendedTags tags;
9554 + int result;
9555 + int alloc_failed = 0;
9556 +
9557 + if(!in)
9558 + return;
9559 +
9560 +#if 0
9561 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
9562 + in->objectId,
9563 + in->lazyLoaded ? "not yet" : "already"));
9564 +#endif
9565 +
9566 + if(in->lazyLoaded){
9567 + in->lazyLoaded = 0;
9568 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9569 +
9570 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
9571 + oh = (yaffs_ObjectHeader *) chunkData;
9572 +
9573 + in->yst_mode = oh->yst_mode;
9574 +#ifdef CONFIG_YAFFS_WINCE
9575 + in->win_atime[0] = oh->win_atime[0];
9576 + in->win_ctime[0] = oh->win_ctime[0];
9577 + in->win_mtime[0] = oh->win_mtime[0];
9578 + in->win_atime[1] = oh->win_atime[1];
9579 + in->win_ctime[1] = oh->win_ctime[1];
9580 + in->win_mtime[1] = oh->win_mtime[1];
9581 +#else
9582 + in->yst_uid = oh->yst_uid;
9583 + in->yst_gid = oh->yst_gid;
9584 + in->yst_atime = oh->yst_atime;
9585 + in->yst_mtime = oh->yst_mtime;
9586 + in->yst_ctime = oh->yst_ctime;
9587 + in->yst_rdev = oh->yst_rdev;
9588 +
9589 +#endif
9590 + yaffs_SetObjectName(in, oh->name);
9591 +
9592 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
9593 + in->variant.symLinkVariant.alias =
9594 + yaffs_CloneString(oh->alias);
9595 + if(!in->variant.symLinkVariant.alias)
9596 + alloc_failed = 1; /* Not returned to caller */
9597 + }
9598 +
9599 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
9600 + }
9601 +}
9602 +
9603 +static int yaffs_ScanBackwards(yaffs_Device * dev)
9604 +{
9605 + yaffs_ExtendedTags tags;
9606 + int blk;
9607 + int blockIterator;
9608 + int startIterator;
9609 + int endIterator;
9610 + int nBlocksToScan = 0;
9611 +
9612 + int chunk;
9613 + int result;
9614 + int c;
9615 + int deleted;
9616 + yaffs_BlockState state;
9617 + yaffs_Object *hardList = NULL;
9618 + yaffs_BlockInfo *bi;
9619 + int sequenceNumber;
9620 + yaffs_ObjectHeader *oh;
9621 + yaffs_Object *in;
9622 + yaffs_Object *parent;
9623 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9624 + int itsUnlinked;
9625 + __u8 *chunkData;
9626 +
9627 + int fileSize;
9628 + int isShrink;
9629 + int foundChunksInBlock;
9630 + int equivalentObjectId;
9631 + int alloc_failed = 0;
9632 +
9633 +
9634 + yaffs_BlockIndex *blockIndex = NULL;
9635 + int altBlockIndex = 0;
9636 +
9637 + if (!dev->isYaffs2) {
9638 + T(YAFFS_TRACE_SCAN,
9639 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9640 + return YAFFS_FAIL;
9641 + }
9642 +
9643 + T(YAFFS_TRACE_SCAN,
9644 + (TSTR
9645 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9646 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9647 +
9648 +
9649 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9650 +
9651 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9652 +
9653 + if(!blockIndex) {
9654 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9655 + altBlockIndex = 1;
9656 + }
9657 +
9658 + if(!blockIndex) {
9659 + T(YAFFS_TRACE_SCAN,
9660 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9661 + return YAFFS_FAIL;
9662 + }
9663 +
9664 + dev->blocksInCheckpoint = 0;
9665 +
9666 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9667 +
9668 + /* Scan all the blocks to determine their state */
9669 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9670 + bi = yaffs_GetBlockInfo(dev, blk);
9671 + yaffs_ClearChunkBits(dev, blk);
9672 + bi->pagesInUse = 0;
9673 + bi->softDeletions = 0;
9674 +
9675 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9676 +
9677 + bi->blockState = state;
9678 + bi->sequenceNumber = sequenceNumber;
9679 +
9680 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9681 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9682 +
9683 + T(YAFFS_TRACE_SCAN_DEBUG,
9684 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9685 + state, sequenceNumber));
9686 +
9687 +
9688 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
9689 + dev->blocksInCheckpoint++;
9690 +
9691 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
9692 + T(YAFFS_TRACE_BAD_BLOCKS,
9693 + (TSTR("block %d is bad" TENDSTR), blk));
9694 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9695 + T(YAFFS_TRACE_SCAN_DEBUG,
9696 + (TSTR("Block empty " TENDSTR)));
9697 + dev->nErasedBlocks++;
9698 + dev->nFreeChunks += dev->nChunksPerBlock;
9699 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9700 +
9701 + /* Determine the highest sequence number */
9702 + if (dev->isYaffs2 &&
9703 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9704 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9705 +
9706 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9707 + blockIndex[nBlocksToScan].block = blk;
9708 +
9709 + nBlocksToScan++;
9710 +
9711 + if (sequenceNumber >= dev->sequenceNumber) {
9712 + dev->sequenceNumber = sequenceNumber;
9713 + }
9714 + } else if (dev->isYaffs2) {
9715 + /* TODO: Nasty sequence number! */
9716 + T(YAFFS_TRACE_SCAN,
9717 + (TSTR
9718 + ("Block scanning block %d has bad sequence number %d"
9719 + TENDSTR), blk, sequenceNumber));
9720 +
9721 + }
9722 + }
9723 + }
9724 +
9725 + T(YAFFS_TRACE_SCAN,
9726 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
9727 +
9728 +
9729 +
9730 + YYIELD();
9731 +
9732 + /* Sort the blocks */
9733 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
9734 + yaffs_qsort(blockIndex, nBlocksToScan,
9735 + sizeof(yaffs_BlockIndex), ybicmp);
9736 +#else
9737 + {
9738 + /* Dungy old bubble sort... */
9739 +
9740 + yaffs_BlockIndex temp;
9741 + int i;
9742 + int j;
9743 +
9744 + for (i = 0; i < nBlocksToScan; i++)
9745 + for (j = i + 1; j < nBlocksToScan; j++)
9746 + if (blockIndex[i].seq > blockIndex[j].seq) {
9747 + temp = blockIndex[j];
9748 + blockIndex[j] = blockIndex[i];
9749 + blockIndex[i] = temp;
9750 + }
9751 + }
9752 +#endif
9753 +
9754 + YYIELD();
9755 +
9756 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
9757 +
9758 + /* Now scan the blocks looking at the data. */
9759 + startIterator = 0;
9760 + endIterator = nBlocksToScan - 1;
9761 + T(YAFFS_TRACE_SCAN_DEBUG,
9762 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9763 +
9764 + /* For each block.... backwards */
9765 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
9766 + blockIterator--) {
9767 + /* Cooperative multitasking! This loop can run for so
9768 + long that watchdog timers expire. */
9769 + YYIELD();
9770 +
9771 + /* get the block to scan in the correct order */
9772 + blk = blockIndex[blockIterator].block;
9773 +
9774 + bi = yaffs_GetBlockInfo(dev, blk);
9775 +
9776 +
9777 + state = bi->blockState;
9778 +
9779 + deleted = 0;
9780 +
9781 + /* For each chunk in each block that needs scanning.... */
9782 + foundChunksInBlock = 0;
9783 + for (c = dev->nChunksPerBlock - 1;
9784 + !alloc_failed && c >= 0 &&
9785 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9786 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
9787 + /* Scan backwards...
9788 + * Read the tags and decide what to do
9789 + */
9790 +
9791 + chunk = blk * dev->nChunksPerBlock + c;
9792 +
9793 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9794 + &tags);
9795 +
9796 + /* Let's have a good look at this chunk... */
9797 +
9798 + if (!tags.chunkUsed) {
9799 + /* An unassigned chunk in the block.
9800 + * If there are used chunks after this one, then
9801 + * it is a chunk that was skipped due to failing the erased
9802 + * check. Just skip it so that it can be deleted.
9803 + * But, more typically, We get here when this is an unallocated
9804 + * chunk and his means that either the block is empty or
9805 + * this is the one being allocated from
9806 + */
9807 +
9808 + if(foundChunksInBlock)
9809 + {
9810 + /* This is a chunk that was skipped due to failing the erased check */
9811 +
9812 + } else if (c == 0) {
9813 + /* We're looking at the first chunk in the block so the block is unused */
9814 + state = YAFFS_BLOCK_STATE_EMPTY;
9815 + dev->nErasedBlocks++;
9816 + } else {
9817 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9818 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
9819 + if(dev->sequenceNumber == bi->sequenceNumber) {
9820 + /* this is the block being allocated from */
9821 +
9822 + T(YAFFS_TRACE_SCAN,
9823 + (TSTR
9824 + (" Allocating from %d %d"
9825 + TENDSTR), blk, c));
9826 +
9827 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9828 + dev->allocationBlock = blk;
9829 + dev->allocationPage = c;
9830 + dev->allocationBlockFinder = blk;
9831 + }
9832 + else {
9833 + /* This is a partially written block that is not
9834 + * the current allocation block. This block must have
9835 + * had a write failure, so set up for retirement.
9836 + */
9837 +
9838 + bi->needsRetiring = 1;
9839 + bi->gcPrioritise = 1;
9840 +
9841 + T(YAFFS_TRACE_ALWAYS,
9842 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
9843 + blk));
9844 + }
9845 +
9846 + }
9847 +
9848 + }
9849 +
9850 + dev->nFreeChunks++;
9851 +
9852 + } else if (tags.chunkId > 0) {
9853 + /* chunkId > 0 so it is a data chunk... */
9854 + unsigned int endpos;
9855 + __u32 chunkBase =
9856 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
9857 +
9858 + foundChunksInBlock = 1;
9859 +
9860 +
9861 + yaffs_SetChunkBit(dev, blk, c);
9862 + bi->pagesInUse++;
9863 +
9864 + in = yaffs_FindOrCreateObjectByNumber(dev,
9865 + tags.
9866 + objectId,
9867 + YAFFS_OBJECT_TYPE_FILE);
9868 + if(!in){
9869 + /* Out of memory */
9870 + alloc_failed = 1;
9871 + }
9872 +
9873 + if (in &&
9874 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9875 + && chunkBase <
9876 + in->variant.fileVariant.shrinkSize) {
9877 + /* This has not been invalidated by a resize */
9878 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
9879 + chunk, -1)){
9880 + alloc_failed = 1;
9881 + }
9882 +
9883 + /* File size is calculated by looking at the data chunks if we have not
9884 + * seen an object header yet. Stop this practice once we find an object header.
9885 + */
9886 + endpos =
9887 + (tags.chunkId -
9888 + 1) * dev->nDataBytesPerChunk +
9889 + tags.byteCount;
9890 +
9891 + if (!in->valid && /* have not got an object header yet */
9892 + in->variant.fileVariant.
9893 + scannedFileSize < endpos) {
9894 + in->variant.fileVariant.
9895 + scannedFileSize = endpos;
9896 + in->variant.fileVariant.
9897 + fileSize =
9898 + in->variant.fileVariant.
9899 + scannedFileSize;
9900 + }
9901 +
9902 + } else if(in) {
9903 + /* This chunk has been invalidated by a resize, so delete */
9904 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9905 +
9906 + }
9907 + } else {
9908 + /* chunkId == 0, so it is an ObjectHeader.
9909 + * Thus, we read in the object header and make the object
9910 + */
9911 + foundChunksInBlock = 1;
9912 +
9913 + yaffs_SetChunkBit(dev, blk, c);
9914 + bi->pagesInUse++;
9915 +
9916 + oh = NULL;
9917 + in = NULL;
9918 +
9919 + if (tags.extraHeaderInfoAvailable) {
9920 + in = yaffs_FindOrCreateObjectByNumber
9921 + (dev, tags.objectId,
9922 + tags.extraObjectType);
9923 + }
9924 +
9925 + if (!in ||
9926 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
9927 + !in->valid ||
9928 +#endif
9929 + tags.extraShadows ||
9930 + (!in->valid &&
9931 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9932 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
9933 + ) {
9934 +
9935 + /* If we don't have valid info then we need to read the chunk
9936 + * TODO In future we can probably defer reading the chunk and
9937 + * living with invalid data until needed.
9938 + */
9939 +
9940 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
9941 + chunk,
9942 + chunkData,
9943 + NULL);
9944 +
9945 + oh = (yaffs_ObjectHeader *) chunkData;
9946 +
9947 + if (!in)
9948 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
9949 +
9950 + }
9951 +
9952 + if (!in) {
9953 + /* TODO Hoosterman we have a problem! */
9954 + T(YAFFS_TRACE_ERROR,
9955 + (TSTR
9956 + ("yaffs tragedy: Could not make object for object %d "
9957 + "at chunk %d during scan"
9958 + TENDSTR), tags.objectId, chunk));
9959 +
9960 + }
9961 +
9962 + if (in->valid) {
9963 + /* We have already filled this one.
9964 + * We have a duplicate that will be discarded, but
9965 + * we first have to suck out resize info if it is a file.
9966 + */
9967 +
9968 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
9969 + ((oh &&
9970 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
9971 + (tags.extraHeaderInfoAvailable &&
9972 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
9973 + ) {
9974 + __u32 thisSize =
9975 + (oh) ? oh->fileSize : tags.
9976 + extraFileLength;
9977 + __u32 parentObjectId =
9978 + (oh) ? oh->
9979 + parentObjectId : tags.
9980 + extraParentObjectId;
9981 + unsigned isShrink =
9982 + (oh) ? oh->isShrink : tags.
9983 + extraIsShrinkHeader;
9984 +
9985 + /* If it is deleted (unlinked at start also means deleted)
9986 + * we treat the file size as being zeroed at this point.
9987 + */
9988 + if (parentObjectId ==
9989 + YAFFS_OBJECTID_DELETED
9990 + || parentObjectId ==
9991 + YAFFS_OBJECTID_UNLINKED) {
9992 + thisSize = 0;
9993 + isShrink = 1;
9994 + }
9995 +
9996 + if (isShrink &&
9997 + in->variant.fileVariant.
9998 + shrinkSize > thisSize) {
9999 + in->variant.fileVariant.
10000 + shrinkSize =
10001 + thisSize;
10002 + }
10003 +
10004 + if (isShrink) {
10005 + bi->hasShrinkHeader = 1;
10006 + }
10007 +
10008 + }
10009 + /* Use existing - destroy this one. */
10010 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
10011 +
10012 + }
10013 +
10014 + if (!in->valid &&
10015 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
10016 + tags.objectId ==
10017 + YAFFS_OBJECTID_LOSTNFOUND)) {
10018 + /* We only load some info, don't fiddle with directory structure */
10019 + in->valid = 1;
10020 +
10021 + if(oh) {
10022 + in->variantType = oh->type;
10023 +
10024 + in->yst_mode = oh->yst_mode;
10025 +#ifdef CONFIG_YAFFS_WINCE
10026 + in->win_atime[0] = oh->win_atime[0];
10027 + in->win_ctime[0] = oh->win_ctime[0];
10028 + in->win_mtime[0] = oh->win_mtime[0];
10029 + in->win_atime[1] = oh->win_atime[1];
10030 + in->win_ctime[1] = oh->win_ctime[1];
10031 + in->win_mtime[1] = oh->win_mtime[1];
10032 +#else
10033 + in->yst_uid = oh->yst_uid;
10034 + in->yst_gid = oh->yst_gid;
10035 + in->yst_atime = oh->yst_atime;
10036 + in->yst_mtime = oh->yst_mtime;
10037 + in->yst_ctime = oh->yst_ctime;
10038 + in->yst_rdev = oh->yst_rdev;
10039 +
10040 +#endif
10041 + } else {
10042 + in->variantType = tags.extraObjectType;
10043 + in->lazyLoaded = 1;
10044 + }
10045 +
10046 + in->chunkId = chunk;
10047 +
10048 + } else if (!in->valid) {
10049 + /* we need to load this info */
10050 +
10051 + in->valid = 1;
10052 + in->chunkId = chunk;
10053 +
10054 + if(oh) {
10055 + in->variantType = oh->type;
10056 +
10057 + in->yst_mode = oh->yst_mode;
10058 +#ifdef CONFIG_YAFFS_WINCE
10059 + in->win_atime[0] = oh->win_atime[0];
10060 + in->win_ctime[0] = oh->win_ctime[0];
10061 + in->win_mtime[0] = oh->win_mtime[0];
10062 + in->win_atime[1] = oh->win_atime[1];
10063 + in->win_ctime[1] = oh->win_ctime[1];
10064 + in->win_mtime[1] = oh->win_mtime[1];
10065 +#else
10066 + in->yst_uid = oh->yst_uid;
10067 + in->yst_gid = oh->yst_gid;
10068 + in->yst_atime = oh->yst_atime;
10069 + in->yst_mtime = oh->yst_mtime;
10070 + in->yst_ctime = oh->yst_ctime;
10071 + in->yst_rdev = oh->yst_rdev;
10072 +#endif
10073 +
10074 + if (oh->shadowsObject > 0)
10075 + yaffs_HandleShadowedObject(dev,
10076 + oh->
10077 + shadowsObject,
10078 + 1);
10079 +
10080 +
10081 + yaffs_SetObjectName(in, oh->name);
10082 + parent =
10083 + yaffs_FindOrCreateObjectByNumber
10084 + (dev, oh->parentObjectId,
10085 + YAFFS_OBJECT_TYPE_DIRECTORY);
10086 +
10087 + fileSize = oh->fileSize;
10088 + isShrink = oh->isShrink;
10089 + equivalentObjectId = oh->equivalentObjectId;
10090 +
10091 + }
10092 + else {
10093 + in->variantType = tags.extraObjectType;
10094 + parent =
10095 + yaffs_FindOrCreateObjectByNumber
10096 + (dev, tags.extraParentObjectId,
10097 + YAFFS_OBJECT_TYPE_DIRECTORY);
10098 + fileSize = tags.extraFileLength;
10099 + isShrink = tags.extraIsShrinkHeader;
10100 + equivalentObjectId = tags.extraEquivalentObjectId;
10101 + in->lazyLoaded = 1;
10102 +
10103 + }
10104 + in->dirty = 0;
10105 +
10106 + /* directory stuff...
10107 + * hook up to parent
10108 + */
10109 +
10110 + if (parent->variantType ==
10111 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10112 + /* Set up as a directory */
10113 + parent->variantType =
10114 + YAFFS_OBJECT_TYPE_DIRECTORY;
10115 + INIT_LIST_HEAD(&parent->variant.
10116 + directoryVariant.
10117 + children);
10118 + } else if (parent->variantType !=
10119 + YAFFS_OBJECT_TYPE_DIRECTORY)
10120 + {
10121 + /* Hoosterman, another problem....
10122 + * We're trying to use a non-directory as a directory
10123 + */
10124 +
10125 + T(YAFFS_TRACE_ERROR,
10126 + (TSTR
10127 + ("yaffs tragedy: attempting to use non-directory as"
10128 + " a directory in scan. Put in lost+found."
10129 + TENDSTR)));
10130 + parent = dev->lostNFoundDir;
10131 + }
10132 +
10133 + yaffs_AddObjectToDirectory(parent, in);
10134 +
10135 + itsUnlinked = (parent == dev->deletedDir) ||
10136 + (parent == dev->unlinkedDir);
10137 +
10138 + if (isShrink) {
10139 + /* Mark the block as having a shrinkHeader */
10140 + bi->hasShrinkHeader = 1;
10141 + }
10142 +
10143 + /* Note re hardlinks.
10144 + * Since we might scan a hardlink before its equivalent object is scanned
10145 + * we put them all in a list.
10146 + * After scanning is complete, we should have all the objects, so we run
10147 + * through this list and fix up all the chains.
10148 + */
10149 +
10150 + switch (in->variantType) {
10151 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10152 + /* Todo got a problem */
10153 + break;
10154 + case YAFFS_OBJECT_TYPE_FILE:
10155 +
10156 + if (in->variant.fileVariant.
10157 + scannedFileSize < fileSize) {
10158 + /* This covers the case where the file size is greater
10159 + * than where the data is
10160 + * This will happen if the file is resized to be larger
10161 + * than its current data extents.
10162 + */
10163 + in->variant.fileVariant.fileSize = fileSize;
10164 + in->variant.fileVariant.scannedFileSize =
10165 + in->variant.fileVariant.fileSize;
10166 + }
10167 +
10168 + if (isShrink &&
10169 + in->variant.fileVariant.shrinkSize > fileSize) {
10170 + in->variant.fileVariant.shrinkSize = fileSize;
10171 + }
10172 +
10173 + break;
10174 + case YAFFS_OBJECT_TYPE_HARDLINK:
10175 + if(!itsUnlinked) {
10176 + in->variant.hardLinkVariant.equivalentObjectId =
10177 + equivalentObjectId;
10178 + in->hardLinks.next =
10179 + (struct list_head *) hardList;
10180 + hardList = in;
10181 + }
10182 + break;
10183 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10184 + /* Do nothing */
10185 + break;
10186 + case YAFFS_OBJECT_TYPE_SPECIAL:
10187 + /* Do nothing */
10188 + break;
10189 + case YAFFS_OBJECT_TYPE_SYMLINK:
10190 + if(oh){
10191 + in->variant.symLinkVariant.alias =
10192 + yaffs_CloneString(oh->
10193 + alias);
10194 + if(!in->variant.symLinkVariant.alias)
10195 + alloc_failed = 1;
10196 + }
10197 + break;
10198 + }
10199 +
10200 + }
10201 +
10202 + }
10203 +
10204 + } /* End of scanning for each chunk */
10205 +
10206 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10207 + /* If we got this far while scanning, then the block is fully allocated. */
10208 + state = YAFFS_BLOCK_STATE_FULL;
10209 + }
10210 +
10211 + bi->blockState = state;
10212 +
10213 + /* Now let's see if it was dirty */
10214 + if (bi->pagesInUse == 0 &&
10215 + !bi->hasShrinkHeader &&
10216 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10217 + yaffs_BlockBecameDirty(dev, blk);
10218 + }
10219 +
10220 + }
10221 +
10222 + if (altBlockIndex)
10223 + YFREE_ALT(blockIndex);
10224 + else
10225 + YFREE(blockIndex);
10226 +
10227 + /* Ok, we've done all the scanning.
10228 + * Fix up the hard link chains.
10229 + * We should now have scanned all the objects, now it's time to add these
10230 + * hardlinks.
10231 + */
10232 + yaffs_HardlinkFixup(dev,hardList);
10233 +
10234 +
10235 + /*
10236 + * Sort out state of unlinked and deleted objects.
10237 + */
10238 + {
10239 + struct list_head *i;
10240 + struct list_head *n;
10241 +
10242 + yaffs_Object *l;
10243 +
10244 + /* Soft delete all the unlinked files */
10245 + list_for_each_safe(i, n,
10246 + &dev->unlinkedDir->variant.directoryVariant.
10247 + children) {
10248 + if (i) {
10249 + l = list_entry(i, yaffs_Object, siblings);
10250 + yaffs_DestroyObject(l);
10251 + }
10252 + }
10253 +
10254 + /* Soft delete all the deletedDir files */
10255 + list_for_each_safe(i, n,
10256 + &dev->deletedDir->variant.directoryVariant.
10257 + children) {
10258 + if (i) {
10259 + l = list_entry(i, yaffs_Object, siblings);
10260 + yaffs_DestroyObject(l);
10261 +
10262 + }
10263 + }
10264 + }
10265 +
10266 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10267 +
10268 + if(alloc_failed){
10269 + return YAFFS_FAIL;
10270 + }
10271 +
10272 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10273 +
10274 + return YAFFS_OK;
10275 +}
10276 +
10277 +/*------------------------------ Directory Functions ----------------------------- */
10278 +
10279 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
10280 +{
10281 + yaffs_Device *dev = obj->myDev;
10282 +
10283 + if(dev && dev->removeObjectCallback)
10284 + dev->removeObjectCallback(obj);
10285 +
10286 + list_del_init(&obj->siblings);
10287 + obj->parent = NULL;
10288 +}
10289 +
10290 +
10291 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
10292 + yaffs_Object * obj)
10293 +{
10294 +
10295 + if (!directory) {
10296 + T(YAFFS_TRACE_ALWAYS,
10297 + (TSTR
10298 + ("tragedy: Trying to add an object to a null pointer directory"
10299 + TENDSTR)));
10300 + YBUG();
10301 + }
10302 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10303 + T(YAFFS_TRACE_ALWAYS,
10304 + (TSTR
10305 + ("tragedy: Trying to add an object to a non-directory"
10306 + TENDSTR)));
10307 + YBUG();
10308 + }
10309 +
10310 + if (obj->siblings.prev == NULL) {
10311 + /* Not initialised */
10312 + INIT_LIST_HEAD(&obj->siblings);
10313 +
10314 + } else if (!list_empty(&obj->siblings)) {
10315 + /* If it is holed up somewhere else, un hook it */
10316 + yaffs_RemoveObjectFromDirectory(obj);
10317 + }
10318 + /* Now add it */
10319 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
10320 + obj->parent = directory;
10321 +
10322 + if (directory == obj->myDev->unlinkedDir
10323 + || directory == obj->myDev->deletedDir) {
10324 + obj->unlinked = 1;
10325 + obj->myDev->nUnlinkedFiles++;
10326 + obj->renameAllowed = 0;
10327 + }
10328 +}
10329 +
10330 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
10331 + const YCHAR * name)
10332 +{
10333 + int sum;
10334 +
10335 + struct list_head *i;
10336 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10337 +
10338 + yaffs_Object *l;
10339 +
10340 + if (!name) {
10341 + return NULL;
10342 + }
10343 +
10344 + if (!directory) {
10345 + T(YAFFS_TRACE_ALWAYS,
10346 + (TSTR
10347 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10348 + TENDSTR)));
10349 + YBUG();
10350 + }
10351 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10352 + T(YAFFS_TRACE_ALWAYS,
10353 + (TSTR
10354 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10355 + YBUG();
10356 + }
10357 +
10358 + sum = yaffs_CalcNameSum(name);
10359 +
10360 + list_for_each(i, &directory->variant.directoryVariant.children) {
10361 + if (i) {
10362 + l = list_entry(i, yaffs_Object, siblings);
10363 +
10364 + yaffs_CheckObjectDetailsLoaded(l);
10365 +
10366 + /* Special case for lost-n-found */
10367 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10368 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
10369 + return l;
10370 + }
10371 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
10372 + {
10373 + /* LostnFound cunk called Objxxx
10374 + * Do a real check
10375 + */
10376 + yaffs_GetObjectName(l, buffer,
10377 + YAFFS_MAX_NAME_LENGTH);
10378 + if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
10379 + return l;
10380 + }
10381 +
10382 + }
10383 + }
10384 + }
10385 +
10386 + return NULL;
10387 +}
10388 +
10389 +
10390 +#if 0
10391 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10392 + int (*fn) (yaffs_Object *))
10393 +{
10394 + struct list_head *i;
10395 + yaffs_Object *l;
10396 +
10397 + if (!theDir) {
10398 + T(YAFFS_TRACE_ALWAYS,
10399 + (TSTR
10400 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10401 + TENDSTR)));
10402 + YBUG();
10403 + }
10404 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10405 + T(YAFFS_TRACE_ALWAYS,
10406 + (TSTR
10407 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10408 + YBUG();
10409 + }
10410 +
10411 + list_for_each(i, &theDir->variant.directoryVariant.children) {
10412 + if (i) {
10413 + l = list_entry(i, yaffs_Object, siblings);
10414 + if (l && !fn(l)) {
10415 + return YAFFS_FAIL;
10416 + }
10417 + }
10418 + }
10419 +
10420 + return YAFFS_OK;
10421 +
10422 +}
10423 +#endif
10424 +
10425 +/* GetEquivalentObject dereferences any hard links to get to the
10426 + * actual object.
10427 + */
10428 +
10429 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
10430 +{
10431 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10432 + /* We want the object id of the equivalent object, not this one */
10433 + obj = obj->variant.hardLinkVariant.equivalentObject;
10434 + yaffs_CheckObjectDetailsLoaded(obj);
10435 + }
10436 + return obj;
10437 +
10438 +}
10439 +
10440 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
10441 +{
10442 + memset(name, 0, buffSize * sizeof(YCHAR));
10443 +
10444 + yaffs_CheckObjectDetailsLoaded(obj);
10445 +
10446 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10447 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10448 + } else if (obj->chunkId <= 0) {
10449 + YCHAR locName[20];
10450 + /* make up a name */
10451 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
10452 + obj->objectId);
10453 + yaffs_strncpy(name, locName, buffSize - 1);
10454 +
10455 + }
10456 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10457 + else if (obj->shortName[0]) {
10458 + yaffs_strcpy(name, obj->shortName);
10459 + }
10460 +#endif
10461 + else {
10462 + int result;
10463 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10464 +
10465 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10466 +
10467 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10468 +
10469 + if (obj->chunkId >= 0) {
10470 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10471 + obj->chunkId, buffer,
10472 + NULL);
10473 + }
10474 + yaffs_strncpy(name, oh->name, buffSize - 1);
10475 +
10476 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10477 + }
10478 +
10479 + return yaffs_strlen(name);
10480 +}
10481 +
10482 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
10483 +{
10484 +
10485 + /* Dereference any hard linking */
10486 + obj = yaffs_GetEquivalentObject(obj);
10487 +
10488 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10489 + return obj->variant.fileVariant.fileSize;
10490 + }
10491 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10492 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10493 + } else {
10494 + /* Only a directory should drop through to here */
10495 + return obj->myDev->nDataBytesPerChunk;
10496 + }
10497 +}
10498 +
10499 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
10500 +{
10501 + int count = 0;
10502 + struct list_head *i;
10503 +
10504 + if (!obj->unlinked) {
10505 + count++; /* the object itself */
10506 + }
10507 + list_for_each(i, &obj->hardLinks) {
10508 + count++; /* add the hard links; */
10509 + }
10510 + return count;
10511 +
10512 +}
10513 +
10514 +int yaffs_GetObjectInode(yaffs_Object * obj)
10515 +{
10516 + obj = yaffs_GetEquivalentObject(obj);
10517 +
10518 + return obj->objectId;
10519 +}
10520 +
10521 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
10522 +{
10523 + obj = yaffs_GetEquivalentObject(obj);
10524 +
10525 + switch (obj->variantType) {
10526 + case YAFFS_OBJECT_TYPE_FILE:
10527 + return DT_REG;
10528 + break;
10529 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10530 + return DT_DIR;
10531 + break;
10532 + case YAFFS_OBJECT_TYPE_SYMLINK:
10533 + return DT_LNK;
10534 + break;
10535 + case YAFFS_OBJECT_TYPE_HARDLINK:
10536 + return DT_REG;
10537 + break;
10538 + case YAFFS_OBJECT_TYPE_SPECIAL:
10539 + if (S_ISFIFO(obj->yst_mode))
10540 + return DT_FIFO;
10541 + if (S_ISCHR(obj->yst_mode))
10542 + return DT_CHR;
10543 + if (S_ISBLK(obj->yst_mode))
10544 + return DT_BLK;
10545 + if (S_ISSOCK(obj->yst_mode))
10546 + return DT_SOCK;
10547 + default:
10548 + return DT_REG;
10549 + break;
10550 + }
10551 +}
10552 +
10553 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
10554 +{
10555 + obj = yaffs_GetEquivalentObject(obj);
10556 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10557 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10558 + } else {
10559 + return yaffs_CloneString(_Y(""));
10560 + }
10561 +}
10562 +
10563 +#ifndef CONFIG_YAFFS_WINCE
10564 +
10565 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
10566 +{
10567 + unsigned int valid = attr->ia_valid;
10568 +
10569 + if (valid & ATTR_MODE)
10570 + obj->yst_mode = attr->ia_mode;
10571 + if (valid & ATTR_UID)
10572 + obj->yst_uid = attr->ia_uid;
10573 + if (valid & ATTR_GID)
10574 + obj->yst_gid = attr->ia_gid;
10575 +
10576 + if (valid & ATTR_ATIME)
10577 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10578 + if (valid & ATTR_CTIME)
10579 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10580 + if (valid & ATTR_MTIME)
10581 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10582 +
10583 + if (valid & ATTR_SIZE)
10584 + yaffs_ResizeFile(obj, attr->ia_size);
10585 +
10586 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10587 +
10588 + return YAFFS_OK;
10589 +
10590 +}
10591 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
10592 +{
10593 + unsigned int valid = 0;
10594 +
10595 + attr->ia_mode = obj->yst_mode;
10596 + valid |= ATTR_MODE;
10597 + attr->ia_uid = obj->yst_uid;
10598 + valid |= ATTR_UID;
10599 + attr->ia_gid = obj->yst_gid;
10600 + valid |= ATTR_GID;
10601 +
10602 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10603 + valid |= ATTR_ATIME;
10604 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10605 + valid |= ATTR_CTIME;
10606 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
10607 + valid |= ATTR_MTIME;
10608 +
10609 + attr->ia_size = yaffs_GetFileSize(obj);
10610 + valid |= ATTR_SIZE;
10611 +
10612 + attr->ia_valid = valid;
10613 +
10614 + return YAFFS_OK;
10615 +
10616 +}
10617 +
10618 +#endif
10619 +
10620 +#if 0
10621 +int yaffs_DumpObject(yaffs_Object * obj)
10622 +{
10623 + YCHAR name[257];
10624 +
10625 + yaffs_GetObjectName(obj, name, 256);
10626 +
10627 + T(YAFFS_TRACE_ALWAYS,
10628 + (TSTR
10629 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
10630 + " chunk %d type %d size %d\n"
10631 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
10632 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
10633 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
10634 +
10635 + return YAFFS_OK;
10636 +}
10637 +#endif
10638 +
10639 +/*---------------------------- Initialisation code -------------------------------------- */
10640 +
10641 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
10642 +{
10643 +
10644 + /* Common functions, gotta have */
10645 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
10646 + return 0;
10647 +
10648 +#ifdef CONFIG_YAFFS_YAFFS2
10649 +
10650 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
10651 + if (dev->writeChunkWithTagsToNAND &&
10652 + dev->readChunkWithTagsFromNAND &&
10653 + !dev->writeChunkToNAND &&
10654 + !dev->readChunkFromNAND &&
10655 + dev->markNANDBlockBad && dev->queryNANDBlock)
10656 + return 1;
10657 +#endif
10658 +
10659 + /* Can use the "spare" style interface for yaffs1 */
10660 + if (!dev->isYaffs2 &&
10661 + !dev->writeChunkWithTagsToNAND &&
10662 + !dev->readChunkWithTagsFromNAND &&
10663 + dev->writeChunkToNAND &&
10664 + dev->readChunkFromNAND &&
10665 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
10666 + return 1;
10667 +
10668 + return 0; /* bad */
10669 +}
10670 +
10671 +
10672 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
10673 +{
10674 + /* Initialise the unlinked, deleted, root and lost and found directories */
10675 +
10676 + dev->lostNFoundDir = dev->rootDir = NULL;
10677 + dev->unlinkedDir = dev->deletedDir = NULL;
10678 +
10679 + dev->unlinkedDir =
10680 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
10681 +
10682 + dev->deletedDir =
10683 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
10684 +
10685 + dev->rootDir =
10686 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
10687 + YAFFS_ROOT_MODE | S_IFDIR);
10688 + dev->lostNFoundDir =
10689 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
10690 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
10691 +
10692 + if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
10693 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
10694 + return YAFFS_OK;
10695 + }
10696 +
10697 + return YAFFS_FAIL;
10698 +}
10699 +
10700 +int yaffs_GutsInitialise(yaffs_Device * dev)
10701 +{
10702 + int init_failed = 0;
10703 + unsigned x;
10704 + int bits;
10705 +
10706 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
10707 +
10708 + /* Check stuff that must be set */
10709 +
10710 + if (!dev) {
10711 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
10712 + return YAFFS_FAIL;
10713 + }
10714 +
10715 + dev->internalStartBlock = dev->startBlock;
10716 + dev->internalEndBlock = dev->endBlock;
10717 + dev->blockOffset = 0;
10718 + dev->chunkOffset = 0;
10719 + dev->nFreeChunks = 0;
10720 +
10721 + if (dev->startBlock == 0) {
10722 + dev->internalStartBlock = dev->startBlock + 1;
10723 + dev->internalEndBlock = dev->endBlock + 1;
10724 + dev->blockOffset = 1;
10725 + dev->chunkOffset = dev->nChunksPerBlock;
10726 + }
10727 +
10728 + /* Check geometry parameters. */
10729 +
10730 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
10731 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
10732 + dev->nChunksPerBlock < 2 ||
10733 + dev->nReservedBlocks < 2 ||
10734 + dev->internalStartBlock <= 0 ||
10735 + dev->internalEndBlock <= 0 ||
10736 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
10737 + ) {
10738 + T(YAFFS_TRACE_ALWAYS,
10739 + (TSTR
10740 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
10741 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
10742 + return YAFFS_FAIL;
10743 + }
10744 +
10745 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
10746 + T(YAFFS_TRACE_ALWAYS,
10747 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
10748 + return YAFFS_FAIL;
10749 + }
10750 +
10751 + /* Got the right mix of functions? */
10752 + if (!yaffs_CheckDevFunctions(dev)) {
10753 + /* Function missing */
10754 + T(YAFFS_TRACE_ALWAYS,
10755 + (TSTR
10756 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
10757 +
10758 + return YAFFS_FAIL;
10759 + }
10760 +
10761 + /* This is really a compilation check. */
10762 + if (!yaffs_CheckStructures()) {
10763 + T(YAFFS_TRACE_ALWAYS,
10764 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
10765 + return YAFFS_FAIL;
10766 + }
10767 +
10768 + if (dev->isMounted) {
10769 + T(YAFFS_TRACE_ALWAYS,
10770 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
10771 + return YAFFS_FAIL;
10772 + }
10773 +
10774 + /* Finished with most checks. One or two more checks happen later on too. */
10775 +
10776 + dev->isMounted = 1;
10777 +
10778 +
10779 +
10780 + /* OK now calculate a few things for the device */
10781 +
10782 + /*
10783 + * Calculate all the chunk size manipulation numbers:
10784 + */
10785 + /* Start off assuming it is a power of 2 */
10786 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
10787 + dev->chunkMask = (1<<dev->chunkShift) - 1;
10788 +
10789 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
10790 + /* Yes it is a power of 2, disable crumbs */
10791 + dev->crumbMask = 0;
10792 + dev->crumbShift = 0;
10793 + dev->crumbsPerChunk = 0;
10794 + } else {
10795 + /* Not a power of 2, use crumbs instead */
10796 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
10797 + dev->crumbMask = (1<<dev->crumbShift)-1;
10798 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
10799 + dev->chunkShift = 0;
10800 + dev->chunkMask = 0;
10801 + }
10802 +
10803 +
10804 + /*
10805 + * Calculate chunkGroupBits.
10806 + * We need to find the next power of 2 > than internalEndBlock
10807 + */
10808 +
10809 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
10810 +
10811 + bits = ShiftsGE(x);
10812 +
10813 + /* Set up tnode width if wide tnodes are enabled. */
10814 + if(!dev->wideTnodesDisabled){
10815 + /* bits must be even so that we end up with 32-bit words */
10816 + if(bits & 1)
10817 + bits++;
10818 + if(bits < 16)
10819 + dev->tnodeWidth = 16;
10820 + else
10821 + dev->tnodeWidth = bits;
10822 + }
10823 + else
10824 + dev->tnodeWidth = 16;
10825 +
10826 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
10827 +
10828 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
10829 + * so if the bitwidth of the
10830 + * chunk range we're using is greater than 16 we need
10831 + * to figure out chunk shift and chunkGroupSize
10832 + */
10833 +
10834 + if (bits <= dev->tnodeWidth)
10835 + dev->chunkGroupBits = 0;
10836 + else
10837 + dev->chunkGroupBits = bits - dev->tnodeWidth;
10838 +
10839 +
10840 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
10841 +
10842 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
10843 + /* We have a problem because the soft delete won't work if
10844 + * the chunk group size > chunks per block.
10845 + * This can be remedied by using larger "virtual blocks".
10846 + */
10847 + T(YAFFS_TRACE_ALWAYS,
10848 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
10849 +
10850 + return YAFFS_FAIL;
10851 + }
10852 +
10853 + /* OK, we've finished verifying the device, lets continue with initialisation */
10854 +
10855 + /* More device initialisation */
10856 + dev->garbageCollections = 0;
10857 + dev->passiveGarbageCollections = 0;
10858 + dev->currentDirtyChecker = 0;
10859 + dev->bufferedBlock = -1;
10860 + dev->doingBufferedBlockRewrite = 0;
10861 + dev->nDeletedFiles = 0;
10862 + dev->nBackgroundDeletions = 0;
10863 + dev->nUnlinkedFiles = 0;
10864 + dev->eccFixed = 0;
10865 + dev->eccUnfixed = 0;
10866 + dev->tagsEccFixed = 0;
10867 + dev->tagsEccUnfixed = 0;
10868 + dev->nErasureFailures = 0;
10869 + dev->nErasedBlocks = 0;
10870 + dev->isDoingGC = 0;
10871 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
10872 +
10873 + /* Initialise temporary buffers and caches. */
10874 + if(!yaffs_InitialiseTempBuffers(dev))
10875 + init_failed = 1;
10876 +
10877 + dev->srCache = NULL;
10878 + dev->gcCleanupList = NULL;
10879 +
10880 +
10881 + if (!init_failed &&
10882 + dev->nShortOpCaches > 0) {
10883 + int i;
10884 + __u8 *buf;
10885 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
10886 +
10887 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
10888 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
10889 + }
10890 +
10891 + buf = dev->srCache = YMALLOC(srCacheBytes);
10892 +
10893 + if(dev->srCache)
10894 + memset(dev->srCache,0,srCacheBytes);
10895 +
10896 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
10897 + dev->srCache[i].object = NULL;
10898 + dev->srCache[i].lastUse = 0;
10899 + dev->srCache[i].dirty = 0;
10900 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
10901 + }
10902 + if(!buf)
10903 + init_failed = 1;
10904 +
10905 + dev->srLastUse = 0;
10906 + }
10907 +
10908 + dev->cacheHits = 0;
10909 +
10910 + if(!init_failed){
10911 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
10912 + if(!dev->gcCleanupList)
10913 + init_failed = 1;
10914 + }
10915 +
10916 + if (dev->isYaffs2) {
10917 + dev->useHeaderFileSize = 1;
10918 + }
10919 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10920 + init_failed = 1;
10921 +
10922 + yaffs_InitialiseTnodes(dev);
10923 + yaffs_InitialiseObjects(dev);
10924 +
10925 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10926 + init_failed = 1;
10927 +
10928 +
10929 + if(!init_failed){
10930 + /* Now scan the flash. */
10931 + if (dev->isYaffs2) {
10932 + if(yaffs_CheckpointRestore(dev)) {
10933 + T(YAFFS_TRACE_ALWAYS,
10934 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
10935 + } else {
10936 +
10937 + /* Clean up the mess caused by an aborted checkpoint load
10938 + * and scan backwards.
10939 + */
10940 + yaffs_DeinitialiseBlocks(dev);
10941 + yaffs_DeinitialiseTnodes(dev);
10942 + yaffs_DeinitialiseObjects(dev);
10943 +
10944 +
10945 + dev->nErasedBlocks = 0;
10946 + dev->nFreeChunks = 0;
10947 + dev->allocationBlock = -1;
10948 + dev->allocationPage = -1;
10949 + dev->nDeletedFiles = 0;
10950 + dev->nUnlinkedFiles = 0;
10951 + dev->nBackgroundDeletions = 0;
10952 + dev->oldestDirtySequence = 0;
10953 +
10954 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10955 + init_failed = 1;
10956 +
10957 + yaffs_InitialiseTnodes(dev);
10958 + yaffs_InitialiseObjects(dev);
10959 +
10960 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10961 + init_failed = 1;
10962 +
10963 + if(!init_failed && !yaffs_ScanBackwards(dev))
10964 + init_failed = 1;
10965 + }
10966 + }else
10967 + if(!yaffs_Scan(dev))
10968 + init_failed = 1;
10969 + }
10970 +
10971 + if(init_failed){
10972 + /* Clean up the mess */
10973 + T(YAFFS_TRACE_TRACING,
10974 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
10975 +
10976 + yaffs_Deinitialise(dev);
10977 + return YAFFS_FAIL;
10978 + }
10979 +
10980 + /* Zero out stats */
10981 + dev->nPageReads = 0;
10982 + dev->nPageWrites = 0;
10983 + dev->nBlockErasures = 0;
10984 + dev->nGCCopies = 0;
10985 + dev->nRetriedWrites = 0;
10986 +
10987 + dev->nRetiredBlocks = 0;
10988 +
10989 + yaffs_VerifyFreeChunks(dev);
10990 + yaffs_VerifyBlocks(dev);
10991 +
10992 +
10993 + T(YAFFS_TRACE_TRACING,
10994 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
10995 + return YAFFS_OK;
10996 +
10997 +}
10998 +
10999 +void yaffs_Deinitialise(yaffs_Device * dev)
11000 +{
11001 + if (dev->isMounted) {
11002 + int i;
11003 +
11004 + yaffs_DeinitialiseBlocks(dev);
11005 + yaffs_DeinitialiseTnodes(dev);
11006 + yaffs_DeinitialiseObjects(dev);
11007 + if (dev->nShortOpCaches > 0 &&
11008 + dev->srCache) {
11009 +
11010 + for (i = 0; i < dev->nShortOpCaches; i++) {
11011 + if(dev->srCache[i].data)
11012 + YFREE(dev->srCache[i].data);
11013 + dev->srCache[i].data = NULL;
11014 + }
11015 +
11016 + YFREE(dev->srCache);
11017 + dev->srCache = NULL;
11018 + }
11019 +
11020 + YFREE(dev->gcCleanupList);
11021 +
11022 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
11023 + YFREE(dev->tempBuffer[i].buffer);
11024 + }
11025 +
11026 + dev->isMounted = 0;
11027 + }
11028 +
11029 +}
11030 +
11031 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
11032 +{
11033 + int nFree;
11034 + int b;
11035 +
11036 + yaffs_BlockInfo *blk;
11037 +
11038 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11039 + b++) {
11040 + blk = yaffs_GetBlockInfo(dev, b);
11041 +
11042 + switch (blk->blockState) {
11043 + case YAFFS_BLOCK_STATE_EMPTY:
11044 + case YAFFS_BLOCK_STATE_ALLOCATING:
11045 + case YAFFS_BLOCK_STATE_COLLECTING:
11046 + case YAFFS_BLOCK_STATE_FULL:
11047 + nFree +=
11048 + (dev->nChunksPerBlock - blk->pagesInUse +
11049 + blk->softDeletions);
11050 + break;
11051 + default:
11052 + break;
11053 + }
11054 +
11055 + }
11056 +
11057 + return nFree;
11058 +}
11059 +
11060 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
11061 +{
11062 + /* This is what we report to the outside world */
11063 +
11064 + int nFree;
11065 + int nDirtyCacheChunks;
11066 + int blocksForCheckpoint;
11067 +
11068 +#if 1
11069 + nFree = dev->nFreeChunks;
11070 +#else
11071 + nFree = yaffs_CountFreeChunks(dev);
11072 +#endif
11073 +
11074 + nFree += dev->nDeletedFiles;
11075 +
11076 + /* Now count the number of dirty chunks in the cache and subtract those */
11077 +
11078 + {
11079 + int i;
11080 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11081 + if (dev->srCache[i].dirty)
11082 + nDirtyCacheChunks++;
11083 + }
11084 + }
11085 +
11086 + nFree -= nDirtyCacheChunks;
11087 +
11088 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11089 +
11090 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11091 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
11092 + if(blocksForCheckpoint < 0)
11093 + blocksForCheckpoint = 0;
11094 +
11095 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11096 +
11097 + if (nFree < 0)
11098 + nFree = 0;
11099 +
11100 + return nFree;
11101 +
11102 +}
11103 +
11104 +static int yaffs_freeVerificationFailures;
11105 +
11106 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
11107 +{
11108 + int counted;
11109 + int difference;
11110 +
11111 + if(yaffs_SkipVerification(dev))
11112 + return;
11113 +
11114 + counted = yaffs_CountFreeChunks(dev);
11115 +
11116 + difference = dev->nFreeChunks - counted;
11117 +
11118 + if (difference) {
11119 + T(YAFFS_TRACE_ALWAYS,
11120 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11121 + dev->nFreeChunks, counted, difference));
11122 + yaffs_freeVerificationFailures++;
11123 + }
11124 +}
11125 +
11126 +/*---------------------------------------- YAFFS test code ----------------------*/
11127 +
11128 +#define yaffs_CheckStruct(structure,syze, name) \
11129 + if(sizeof(structure) != syze) \
11130 + { \
11131 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
11132 + name,syze,sizeof(structure))); \
11133 + return YAFFS_FAIL; \
11134 + }
11135 +
11136 +static int yaffs_CheckStructures(void)
11137 +{
11138 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
11139 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
11140 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
11141 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11142 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
11143 +#endif
11144 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
11145 +
11146 + return YAFFS_OK;
11147 +}
11148 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.h
11149 ===================================================================
11150 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
11151 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.h 2007-09-21 16:24:08.000000000 +0800
11152 @@ -0,0 +1,902 @@
11153 +/*
11154 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11155 + *
11156 + * Copyright (C) 2002-2007 Aleph One Ltd.
11157 + * for Toby Churchill Ltd and Brightstar Engineering
11158 + *
11159 + * Created by Charles Manning <charles@aleph1.co.uk>
11160 + *
11161 + * This program is free software; you can redistribute it and/or modify
11162 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11163 + * published by the Free Software Foundation.
11164 + *
11165 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11166 + */
11167 +
11168 +#ifndef __YAFFS_GUTS_H__
11169 +#define __YAFFS_GUTS_H__
11170 +
11171 +#include "devextras.h"
11172 +#include "yportenv.h"
11173 +
11174 +#define YAFFS_OK 1
11175 +#define YAFFS_FAIL 0
11176 +
11177 +/* Give us a Y=0x59,
11178 + * Give us an A=0x41,
11179 + * Give us an FF=0xFF
11180 + * Give us an S=0x53
11181 + * And what have we got...
11182 + */
11183 +#define YAFFS_MAGIC 0x5941FF53
11184 +
11185 +#define YAFFS_NTNODES_LEVEL0 16
11186 +#define YAFFS_TNODES_LEVEL0_BITS 4
11187 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11188 +
11189 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11190 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11191 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11192 +#define YAFFS_TNODES_MAX_LEVEL 6
11193 +
11194 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11195 +#define YAFFS_BYTES_PER_SPARE 16
11196 +#define YAFFS_BYTES_PER_CHUNK 512
11197 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11198 +#define YAFFS_CHUNKS_PER_BLOCK 32
11199 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11200 +#endif
11201 +
11202 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11203 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11204 +
11205 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11206 +
11207 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11208 +
11209 +#define YAFFS_ALLOCATION_NOBJECTS 100
11210 +#define YAFFS_ALLOCATION_NTNODES 100
11211 +#define YAFFS_ALLOCATION_NLINKS 100
11212 +
11213 +#define YAFFS_NOBJECT_BUCKETS 256
11214 +
11215 +
11216 +#define YAFFS_OBJECT_SPACE 0x40000
11217 +
11218 +#define YAFFS_CHECKPOINT_VERSION 3
11219 +
11220 +#ifdef CONFIG_YAFFS_UNICODE
11221 +#define YAFFS_MAX_NAME_LENGTH 127
11222 +#define YAFFS_MAX_ALIAS_LENGTH 79
11223 +#else
11224 +#define YAFFS_MAX_NAME_LENGTH 255
11225 +#define YAFFS_MAX_ALIAS_LENGTH 159
11226 +#endif
11227 +
11228 +#define YAFFS_SHORT_NAME_LENGTH 15
11229 +
11230 +/* Some special object ids for pseudo objects */
11231 +#define YAFFS_OBJECTID_ROOT 1
11232 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11233 +#define YAFFS_OBJECTID_UNLINKED 3
11234 +#define YAFFS_OBJECTID_DELETED 4
11235 +
11236 +/* Sseudo object ids for checkpointing */
11237 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11238 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11239 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11240 +
11241 +/* */
11242 +
11243 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11244 +
11245 +#define YAFFS_N_TEMP_BUFFERS 4
11246 +
11247 +/* We limit the number attempts at sucessfully saving a chunk of data.
11248 + * Small-page devices have 32 pages per block; large-page devices have 64.
11249 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11250 + */
11251 +#define YAFFS_WR_ATTEMPTS (5*64)
11252 +
11253 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11254 + * The range is limited slightly to help distinguish bad numbers from good.
11255 + * This also allows us to perhaps in the future use special numbers for
11256 + * special purposes.
11257 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11258 + * and is a larger number than the lifetime of a 2GB device.
11259 + */
11260 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11261 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11262 +
11263 +/* ChunkCache is used for short read/write operations.*/
11264 +typedef struct {
11265 + struct yaffs_ObjectStruct *object;
11266 + int chunkId;
11267 + int lastUse;
11268 + int dirty;
11269 + int nBytes; /* Only valid if the cache is dirty */
11270 + int locked; /* Can't push out or flush while locked. */
11271 +#ifdef CONFIG_YAFFS_YAFFS2
11272 + __u8 *data;
11273 +#else
11274 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11275 +#endif
11276 +} yaffs_ChunkCache;
11277 +
11278 +
11279 +
11280 +/* Tags structures in RAM
11281 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11282 + * the structure size will get blown out.
11283 + */
11284 +
11285 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11286 +typedef struct {
11287 + unsigned chunkId:20;
11288 + unsigned serialNumber:2;
11289 + unsigned byteCount:10;
11290 + unsigned objectId:18;
11291 + unsigned ecc:12;
11292 + unsigned unusedStuff:2;
11293 +
11294 +} yaffs_Tags;
11295 +
11296 +typedef union {
11297 + yaffs_Tags asTags;
11298 + __u8 asBytes[8];
11299 +} yaffs_TagsUnion;
11300 +
11301 +#endif
11302 +
11303 +/* Stuff used for extended tags in YAFFS2 */
11304 +
11305 +typedef enum {
11306 + YAFFS_ECC_RESULT_UNKNOWN,
11307 + YAFFS_ECC_RESULT_NO_ERROR,
11308 + YAFFS_ECC_RESULT_FIXED,
11309 + YAFFS_ECC_RESULT_UNFIXED
11310 +} yaffs_ECCResult;
11311 +
11312 +typedef enum {
11313 + YAFFS_OBJECT_TYPE_UNKNOWN,
11314 + YAFFS_OBJECT_TYPE_FILE,
11315 + YAFFS_OBJECT_TYPE_SYMLINK,
11316 + YAFFS_OBJECT_TYPE_DIRECTORY,
11317 + YAFFS_OBJECT_TYPE_HARDLINK,
11318 + YAFFS_OBJECT_TYPE_SPECIAL
11319 +} yaffs_ObjectType;
11320 +
11321 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11322 +
11323 +typedef struct {
11324 +
11325 + unsigned validMarker0;
11326 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11327 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11328 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11329 + unsigned byteCount; /* Only valid for data chunks */
11330 +
11331 + /* The following stuff only has meaning when we read */
11332 + yaffs_ECCResult eccResult;
11333 + unsigned blockBad;
11334 +
11335 + /* YAFFS 1 stuff */
11336 + unsigned chunkDeleted; /* The chunk is marked deleted */
11337 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11338 +
11339 + /* YAFFS2 stuff */
11340 + unsigned sequenceNumber; /* The sequence number of this block */
11341 +
11342 + /* Extra info if this is an object header (YAFFS2 only) */
11343 +
11344 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11345 + unsigned extraParentObjectId; /* The parent object */
11346 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11347 + unsigned extraShadows; /* Does this shadow another object? */
11348 +
11349 + yaffs_ObjectType extraObjectType; /* What object type? */
11350 +
11351 + unsigned extraFileLength; /* Length if it is a file */
11352 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11353 +
11354 + unsigned validMarker1;
11355 +
11356 +} yaffs_ExtendedTags;
11357 +
11358 +/* Spare structure for YAFFS1 */
11359 +typedef struct {
11360 + __u8 tagByte0;
11361 + __u8 tagByte1;
11362 + __u8 tagByte2;
11363 + __u8 tagByte3;
11364 + __u8 pageStatus; /* set to 0 to delete the chunk */
11365 + __u8 blockStatus;
11366 + __u8 tagByte4;
11367 + __u8 tagByte5;
11368 + __u8 ecc1[3];
11369 + __u8 tagByte6;
11370 + __u8 tagByte7;
11371 + __u8 ecc2[3];
11372 +} yaffs_Spare;
11373 +
11374 +/*Special structure for passing through to mtd */
11375 +struct yaffs_NANDSpare {
11376 + yaffs_Spare spare;
11377 + int eccres1;
11378 + int eccres2;
11379 +};
11380 +
11381 +/* Block data in RAM */
11382 +
11383 +typedef enum {
11384 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11385 +
11386 + YAFFS_BLOCK_STATE_SCANNING,
11387 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11388 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11389 + * but it needs to be scanned to determine its true state.
11390 + * This state is only valid during yaffs_Scan.
11391 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11392 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11393 + */
11394 +
11395 + YAFFS_BLOCK_STATE_EMPTY,
11396 + /* This block is empty */
11397 +
11398 + YAFFS_BLOCK_STATE_ALLOCATING,
11399 + /* This block is partially allocated.
11400 + * At least one page holds valid data.
11401 + * This is the one currently being used for page
11402 + * allocation. Should never be more than one of these
11403 + */
11404 +
11405 + YAFFS_BLOCK_STATE_FULL,
11406 + /* All the pages in this block have been allocated.
11407 + */
11408 +
11409 + YAFFS_BLOCK_STATE_DIRTY,
11410 + /* All pages have been allocated and deleted.
11411 + * Erase me, reuse me.
11412 + */
11413 +
11414 + YAFFS_BLOCK_STATE_CHECKPOINT,
11415 + /* This block is assigned to holding checkpoint data.
11416 + */
11417 +
11418 + YAFFS_BLOCK_STATE_COLLECTING,
11419 + /* This block is being garbage collected */
11420 +
11421 + YAFFS_BLOCK_STATE_DEAD
11422 + /* This block has failed and is not in use */
11423 +} yaffs_BlockState;
11424 +
11425 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11426 +
11427 +
11428 +typedef struct {
11429 +
11430 + int softDeletions:10; /* number of soft deleted pages */
11431 + int pagesInUse:10; /* number of pages in use */
11432 + yaffs_BlockState blockState:4; /* One of the above block states */
11433 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11434 + /* and retire the block. */
11435 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
11436 + __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
11437 + It should be prioritised for GC */
11438 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11439 +
11440 +#ifdef CONFIG_YAFFS_YAFFS2
11441 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11442 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11443 +#endif
11444 +
11445 +} yaffs_BlockInfo;
11446 +
11447 +/* -------------------------- Object structure -------------------------------*/
11448 +/* This is the object structure as stored on NAND */
11449 +
11450 +typedef struct {
11451 + yaffs_ObjectType type;
11452 +
11453 + /* Apply to everything */
11454 + int parentObjectId;
11455 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11456 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11457 +
11458 + /* Thes following apply to directories, files, symlinks - not hard links */
11459 + __u32 yst_mode; /* protection */
11460 +
11461 +#ifdef CONFIG_YAFFS_WINCE
11462 + __u32 notForWinCE[5];
11463 +#else
11464 + __u32 yst_uid;
11465 + __u32 yst_gid;
11466 + __u32 yst_atime;
11467 + __u32 yst_mtime;
11468 + __u32 yst_ctime;
11469 +#endif
11470 +
11471 + /* File size applies to files only */
11472 + int fileSize;
11473 +
11474 + /* Equivalent object id applies to hard links only. */
11475 + int equivalentObjectId;
11476 +
11477 + /* Alias is for symlinks only. */
11478 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11479 +
11480 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11481 +
11482 +#ifdef CONFIG_YAFFS_WINCE
11483 + __u32 win_ctime[2];
11484 + __u32 win_atime[2];
11485 + __u32 win_mtime[2];
11486 + __u32 roomToGrow[4];
11487 +#else
11488 + __u32 roomToGrow[10];
11489 +#endif
11490 +
11491 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11492 +
11493 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11494 + __u32 isShrink;
11495 +
11496 +} yaffs_ObjectHeader;
11497 +
11498 +/*--------------------------- Tnode -------------------------- */
11499 +
11500 +union yaffs_Tnode_union {
11501 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11502 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11503 +#else
11504 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11505 +#endif
11506 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11507 +
11508 +};
11509 +
11510 +typedef union yaffs_Tnode_union yaffs_Tnode;
11511 +
11512 +struct yaffs_TnodeList_struct {
11513 + struct yaffs_TnodeList_struct *next;
11514 + yaffs_Tnode *tnodes;
11515 +};
11516 +
11517 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11518 +
11519 +/*------------------------ Object -----------------------------*/
11520 +/* An object can be one of:
11521 + * - a directory (no data, has children links
11522 + * - a regular file (data.... not prunes :->).
11523 + * - a symlink [symbolic link] (the alias).
11524 + * - a hard link
11525 + */
11526 +
11527 +typedef struct {
11528 + __u32 fileSize;
11529 + __u32 scannedFileSize;
11530 + __u32 shrinkSize;
11531 + int topLevel;
11532 + yaffs_Tnode *top;
11533 +} yaffs_FileStructure;
11534 +
11535 +typedef struct {
11536 + struct list_head children; /* list of child links */
11537 +} yaffs_DirectoryStructure;
11538 +
11539 +typedef struct {
11540 + YCHAR *alias;
11541 +} yaffs_SymLinkStructure;
11542 +
11543 +typedef struct {
11544 + struct yaffs_ObjectStruct *equivalentObject;
11545 + __u32 equivalentObjectId;
11546 +} yaffs_HardLinkStructure;
11547 +
11548 +typedef union {
11549 + yaffs_FileStructure fileVariant;
11550 + yaffs_DirectoryStructure directoryVariant;
11551 + yaffs_SymLinkStructure symLinkVariant;
11552 + yaffs_HardLinkStructure hardLinkVariant;
11553 +} yaffs_ObjectVariant;
11554 +
11555 +struct yaffs_ObjectStruct {
11556 + __u8 deleted:1; /* This should only apply to unlinked files. */
11557 + __u8 softDeleted:1; /* it has also been soft deleted */
11558 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11559 + __u8 fake:1; /* A fake object has no presence on NAND. */
11560 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11561 + __u8 unlinkAllowed:1;
11562 + __u8 dirty:1; /* the object needs to be written to flash */
11563 + __u8 valid:1; /* When the file system is being loaded up, this
11564 + * object might be created before the data
11565 + * is available (ie. file data records appear before the header).
11566 + */
11567 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11568 +
11569 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11570 + * still in the inode cache. Free of object is defered.
11571 + * until the inode is released.
11572 + */
11573 +
11574 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11575 + __u16 sum; /* sum of the name to speed searching */
11576 +
11577 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11578 +
11579 + struct list_head hashLink; /* list of objects in this hash bucket */
11580 +
11581 + struct list_head hardLinks; /* all the equivalent hard linked objects */
11582 +
11583 + /* directory structure stuff */
11584 + /* also used for linking up the free list */
11585 + struct yaffs_ObjectStruct *parent;
11586 + struct list_head siblings;
11587 +
11588 + /* Where's my object header in NAND? */
11589 + int chunkId;
11590 +
11591 + int nDataChunks; /* Number of data chunks attached to the file. */
11592 +
11593 + __u32 objectId; /* the object id value */
11594 +
11595 + __u32 yst_mode;
11596 +
11597 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11598 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11599 +#endif
11600 +
11601 +#ifndef __KERNEL__
11602 + __u32 inUse;
11603 +#endif
11604 +
11605 +#ifdef CONFIG_YAFFS_WINCE
11606 + __u32 win_ctime[2];
11607 + __u32 win_mtime[2];
11608 + __u32 win_atime[2];
11609 +#else
11610 + __u32 yst_uid;
11611 + __u32 yst_gid;
11612 + __u32 yst_atime;
11613 + __u32 yst_mtime;
11614 + __u32 yst_ctime;
11615 +#endif
11616 +
11617 + __u32 yst_rdev;
11618 +
11619 +#ifdef __KERNEL__
11620 + struct inode *myInode;
11621 +
11622 +#endif
11623 +
11624 + yaffs_ObjectType variantType;
11625 +
11626 + yaffs_ObjectVariant variant;
11627 +
11628 +};
11629 +
11630 +typedef struct yaffs_ObjectStruct yaffs_Object;
11631 +
11632 +struct yaffs_ObjectList_struct {
11633 + yaffs_Object *objects;
11634 + struct yaffs_ObjectList_struct *next;
11635 +};
11636 +
11637 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
11638 +
11639 +typedef struct {
11640 + struct list_head list;
11641 + int count;
11642 +} yaffs_ObjectBucket;
11643 +
11644 +
11645 +/* yaffs_CheckpointObject holds the definition of an object as dumped
11646 + * by checkpointing.
11647 + */
11648 +
11649 +typedef struct {
11650 + int structType;
11651 + __u32 objectId;
11652 + __u32 parentId;
11653 + int chunkId;
11654 +
11655 + yaffs_ObjectType variantType:3;
11656 + __u8 deleted:1;
11657 + __u8 softDeleted:1;
11658 + __u8 unlinked:1;
11659 + __u8 fake:1;
11660 + __u8 renameAllowed:1;
11661 + __u8 unlinkAllowed:1;
11662 + __u8 serial;
11663 +
11664 + int nDataChunks;
11665 + __u32 fileSizeOrEquivalentObjectId;
11666 +
11667 +}yaffs_CheckpointObject;
11668 +
11669 +/*--------------------- Temporary buffers ----------------
11670 + *
11671 + * These are chunk-sized working buffers. Each device has a few
11672 + */
11673 +
11674 +typedef struct {
11675 + __u8 *buffer;
11676 + int line; /* track from whence this buffer was allocated */
11677 + int maxLine;
11678 +} yaffs_TempBuffer;
11679 +
11680 +/*----------------- Device ---------------------------------*/
11681 +
11682 +struct yaffs_DeviceStruct {
11683 + struct list_head devList;
11684 + const char *name;
11685 +
11686 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
11687 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
11688 + int nChunksPerBlock; /* does not need to be a power of 2 */
11689 + int nBytesPerSpare; /* spare area size */
11690 + int startBlock; /* Start block we're allowed to use */
11691 + int endBlock; /* End block we're allowed to use */
11692 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
11693 + /* reserved blocks on NOR and RAM. */
11694 +
11695 +
11696 + /* Stuff used by the shared space checkpointing mechanism */
11697 + /* If this value is zero, then this mechanism is disabled */
11698 +
11699 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
11700 +
11701 +
11702 +
11703 +
11704 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
11705 + * the number of short op caches (don't use too many)
11706 + */
11707 +
11708 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
11709 +
11710 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
11711 +
11712 + void *genericDevice; /* Pointer to device context
11713 + * On an mtd this holds the mtd pointer.
11714 + */
11715 + void *superBlock;
11716 +
11717 + /* NAND access functions (Must be set before calling YAFFS)*/
11718 +
11719 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
11720 + int chunkInNAND, const __u8 * data,
11721 + const yaffs_Spare * spare);
11722 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
11723 + int chunkInNAND, __u8 * data,
11724 + yaffs_Spare * spare);
11725 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
11726 + int blockInNAND);
11727 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
11728 +
11729 +#ifdef CONFIG_YAFFS_YAFFS2
11730 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
11731 + int chunkInNAND, const __u8 * data,
11732 + const yaffs_ExtendedTags * tags);
11733 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
11734 + int chunkInNAND, __u8 * data,
11735 + yaffs_ExtendedTags * tags);
11736 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
11737 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
11738 + yaffs_BlockState * state, int *sequenceNumber);
11739 +#endif
11740 +
11741 + int isYaffs2;
11742 +
11743 + /* The removeObjectCallback function must be supplied by OS flavours that
11744 + * need it. The Linux kernel does not use this, but yaffs direct does use
11745 + * it to implement the faster readdir
11746 + */
11747 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
11748 +
11749 + /* Callback to mark the superblock dirsty */
11750 + void (*markSuperBlockDirty)(void * superblock);
11751 +
11752 + int wideTnodesDisabled; /* Set to disable wide tnodes */
11753 +
11754 +
11755 + /* End of stuff that must be set before initialisation. */
11756 +
11757 + /* Checkpoint control. Can be set before or after initialisation */
11758 + __u8 skipCheckpointRead;
11759 + __u8 skipCheckpointWrite;
11760 +
11761 + /* Runtime parameters. Set up by YAFFS. */
11762 +
11763 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
11764 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
11765 +
11766 + /* Stuff to support wide tnodes */
11767 + __u32 tnodeWidth;
11768 + __u32 tnodeMask;
11769 +
11770 + /* Stuff to support various file offses to chunk/offset translations */
11771 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
11772 + __u32 crumbMask;
11773 + __u32 crumbShift;
11774 + __u32 crumbsPerChunk;
11775 +
11776 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
11777 + __u32 chunkShift;
11778 + __u32 chunkMask;
11779 +
11780 +
11781 +#ifdef __KERNEL__
11782 +
11783 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
11784 + struct semaphore grossLock; /* Gross locking semaphore */
11785 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
11786 + * at compile time so we have to allocate it.
11787 + */
11788 + void (*putSuperFunc) (struct super_block * sb);
11789 +#endif
11790 +
11791 + int isMounted;
11792 +
11793 + int isCheckpointed;
11794 +
11795 +
11796 + /* Stuff to support block offsetting to support start block zero */
11797 + int internalStartBlock;
11798 + int internalEndBlock;
11799 + int blockOffset;
11800 + int chunkOffset;
11801 +
11802 +
11803 + /* Runtime checkpointing stuff */
11804 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
11805 + int checkpointByteCount;
11806 + int checkpointByteOffset;
11807 + __u8 *checkpointBuffer;
11808 + int checkpointOpenForWrite;
11809 + int blocksInCheckpoint;
11810 + int checkpointCurrentChunk;
11811 + int checkpointCurrentBlock;
11812 + int checkpointNextBlock;
11813 + int *checkpointBlockList;
11814 + int checkpointMaxBlocks;
11815 + __u32 checkpointSum;
11816 + __u32 checkpointXor;
11817 +
11818 + /* Block Info */
11819 + yaffs_BlockInfo *blockInfo;
11820 + __u8 *chunkBits; /* bitmap of chunks in use */
11821 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
11822 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
11823 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
11824 + * Must be consistent with nChunksPerBlock.
11825 + */
11826 +
11827 + int nErasedBlocks;
11828 + int allocationBlock; /* Current block being allocated off */
11829 + __u32 allocationPage;
11830 + int allocationBlockFinder; /* Used to search for next allocation block */
11831 +
11832 + /* Runtime state */
11833 + int nTnodesCreated;
11834 + yaffs_Tnode *freeTnodes;
11835 + int nFreeTnodes;
11836 + yaffs_TnodeList *allocatedTnodeList;
11837 +
11838 + int isDoingGC;
11839 +
11840 + int nObjectsCreated;
11841 + yaffs_Object *freeObjects;
11842 + int nFreeObjects;
11843 +
11844 + yaffs_ObjectList *allocatedObjectList;
11845 +
11846 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
11847 +
11848 + int nFreeChunks;
11849 +
11850 + int currentDirtyChecker; /* Used to find current dirtiest block */
11851 +
11852 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
11853 + int nonAggressiveSkip; /* GC state/mode */
11854 +
11855 + /* Statistcs */
11856 + int nPageWrites;
11857 + int nPageReads;
11858 + int nBlockErasures;
11859 + int nErasureFailures;
11860 + int nGCCopies;
11861 + int garbageCollections;
11862 + int passiveGarbageCollections;
11863 + int nRetriedWrites;
11864 + int nRetiredBlocks;
11865 + int eccFixed;
11866 + int eccUnfixed;
11867 + int tagsEccFixed;
11868 + int tagsEccUnfixed;
11869 + int nDeletions;
11870 + int nUnmarkedDeletions;
11871 +
11872 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
11873 +
11874 + /* Special directories */
11875 + yaffs_Object *rootDir;
11876 + yaffs_Object *lostNFoundDir;
11877 +
11878 + /* Buffer areas for storing data to recover from write failures TODO
11879 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
11880 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
11881 + */
11882 +
11883 + int bufferedBlock; /* Which block is buffered here? */
11884 + int doingBufferedBlockRewrite;
11885 +
11886 + yaffs_ChunkCache *srCache;
11887 + int srLastUse;
11888 +
11889 + int cacheHits;
11890 +
11891 + /* Stuff for background deletion and unlinked files.*/
11892 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
11893 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
11894 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
11895 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11896 + int nUnlinkedFiles; /* Count of unlinked files. */
11897 + int nBackgroundDeletions; /* Count of background deletions. */
11898 +
11899 +
11900 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
11901 + int maxTemp;
11902 + int unmanagedTempAllocations;
11903 + int unmanagedTempDeallocations;
11904 +
11905 + /* yaffs2 runtime stuff */
11906 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11907 + unsigned oldestDirtySequence;
11908 +
11909 +};
11910 +
11911 +typedef struct yaffs_DeviceStruct yaffs_Device;
11912 +
11913 +/* The static layout of bllock usage etc is stored in the super block header */
11914 +typedef struct {
11915 + int StructType;
11916 + int version;
11917 + int checkpointStartBlock;
11918 + int checkpointEndBlock;
11919 + int startBlock;
11920 + int endBlock;
11921 + int rfu[100];
11922 +} yaffs_SuperBlockHeader;
11923 +
11924 +/* The CheckpointDevice structure holds the device information that changes at runtime and
11925 + * must be preserved over unmount/mount cycles.
11926 + */
11927 +typedef struct {
11928 + int structType;
11929 + int nErasedBlocks;
11930 + int allocationBlock; /* Current block being allocated off */
11931 + __u32 allocationPage;
11932 + int nFreeChunks;
11933 +
11934 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11935 + int nUnlinkedFiles; /* Count of unlinked files. */
11936 + int nBackgroundDeletions; /* Count of background deletions. */
11937 +
11938 + /* yaffs2 runtime stuff */
11939 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11940 + unsigned oldestDirtySequence;
11941 +
11942 +} yaffs_CheckpointDevice;
11943 +
11944 +
11945 +typedef struct {
11946 + int structType;
11947 + __u32 magic;
11948 + __u32 version;
11949 + __u32 head;
11950 +} yaffs_CheckpointValidity;
11951 +
11952 +/* Function to manipulate block info */
11953 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
11954 +{
11955 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
11956 + T(YAFFS_TRACE_ERROR,
11957 + (TSTR
11958 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
11959 + blk));
11960 + YBUG();
11961 + }
11962 + return &dev->blockInfo[blk - dev->internalStartBlock];
11963 +}
11964 +
11965 +/*----------------------- YAFFS Functions -----------------------*/
11966 +
11967 +int yaffs_GutsInitialise(yaffs_Device * dev);
11968 +void yaffs_Deinitialise(yaffs_Device * dev);
11969 +
11970 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
11971 +
11972 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
11973 + yaffs_Object * newDir, const YCHAR * newName);
11974 +
11975 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
11976 +int yaffs_DeleteFile(yaffs_Object * obj);
11977 +
11978 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
11979 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
11980 +int yaffs_GetObjectInode(yaffs_Object * obj);
11981 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
11982 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
11983 +
11984 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
11985 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
11986 +
11987 +/* File operations */
11988 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
11989 + int nBytes);
11990 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
11991 + int nBytes, int writeThrough);
11992 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
11993 +
11994 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
11995 + __u32 mode, __u32 uid, __u32 gid);
11996 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
11997 +
11998 +/* Flushing and checkpointing */
11999 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
12000 +
12001 +int yaffs_CheckpointSave(yaffs_Device *dev);
12002 +int yaffs_CheckpointRestore(yaffs_Device *dev);
12003 +
12004 +/* Directory operations */
12005 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
12006 + __u32 mode, __u32 uid, __u32 gid);
12007 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
12008 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
12009 + int (*fn) (yaffs_Object *));
12010 +
12011 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
12012 +
12013 +/* Link operations */
12014 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
12015 + yaffs_Object * equivalentObject);
12016 +
12017 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
12018 +
12019 +/* Symlink operations */
12020 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
12021 + __u32 mode, __u32 uid, __u32 gid,
12022 + const YCHAR * alias);
12023 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
12024 +
12025 +/* Special inodes (fifos, sockets and devices) */
12026 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
12027 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
12028 +
12029 +/* Special directories */
12030 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
12031 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
12032 +
12033 +#ifdef CONFIG_YAFFS_WINCE
12034 +/* CONFIG_YAFFS_WINCE special stuff */
12035 +void yfsd_WinFileTimeNow(__u32 target[2]);
12036 +#endif
12037 +
12038 +#ifdef __KERNEL__
12039 +
12040 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
12041 +#endif
12042 +
12043 +/* Debug dump */
12044 +int yaffs_DumpObject(yaffs_Object * obj);
12045 +
12046 +void yaffs_GutsTest(yaffs_Device * dev);
12047 +
12048 +/* A few useful functions */
12049 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12050 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
12051 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
12052 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12053 +
12054 +#endif
12055 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.c
12056 ===================================================================
12057 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12058 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.c 2007-09-21 16:24:08.000000000 +0800
12059 @@ -0,0 +1,241 @@
12060 +/*
12061 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12062 + *
12063 + * Copyright (C) 2002-2007 Aleph One Ltd.
12064 + * for Toby Churchill Ltd and Brightstar Engineering
12065 + *
12066 + * Created by Charles Manning <charles@aleph1.co.uk>
12067 + *
12068 + * This program is free software; you can redistribute it and/or modify
12069 + * it under the terms of the GNU General Public License version 2 as
12070 + * published by the Free Software Foundation.
12071 + */
12072 +
12073 +const char *yaffs_mtdif_c_version =
12074 + "$Id: yaffs_mtdif.c,v 1.19 2007-02-14 01:09:06 wookey Exp $";
12075 +
12076 +#include "yportenv.h"
12077 +
12078 +
12079 +#include "yaffs_mtdif.h"
12080 +
12081 +#include "linux/mtd/mtd.h"
12082 +#include "linux/types.h"
12083 +#include "linux/time.h"
12084 +#include "linux/mtd/nand.h"
12085 +
12086 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
12087 +static struct nand_oobinfo yaffs_oobinfo = {
12088 + .useecc = 1,
12089 + .eccbytes = 6,
12090 + .eccpos = {8, 9, 10, 13, 14, 15}
12091 +};
12092 +
12093 +static struct nand_oobinfo yaffs_noeccinfo = {
12094 + .useecc = 0,
12095 +};
12096 +#endif
12097 +
12098 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12099 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
12100 +{
12101 + oob[0] = spare->tagByte0;
12102 + oob[1] = spare->tagByte1;
12103 + oob[2] = spare->tagByte2;
12104 + oob[3] = spare->tagByte3;
12105 + oob[4] = spare->tagByte4;
12106 + oob[5] = spare->tagByte5 & 0x3f;
12107 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
12108 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
12109 + oob[6] = spare->tagByte6;
12110 + oob[7] = spare->tagByte7;
12111 +}
12112 +
12113 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
12114 +{
12115 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
12116 + spare->tagByte0 = oob[0];
12117 + spare->tagByte1 = oob[1];
12118 + spare->tagByte2 = oob[2];
12119 + spare->tagByte3 = oob[3];
12120 + spare->tagByte4 = oob[4];
12121 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
12122 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
12123 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
12124 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
12125 + spare->tagByte6 = oob[6];
12126 + spare->tagByte7 = oob[7];
12127 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
12128 +
12129 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
12130 +}
12131 +#endif
12132 +
12133 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12134 + const __u8 * data, const yaffs_Spare * spare)
12135 +{
12136 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12137 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12138 + struct mtd_oob_ops ops;
12139 +#endif
12140 + size_t dummy;
12141 + int retval = 0;
12142 +
12143 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12144 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12145 + __u8 spareAsBytes[8]; /* OOB */
12146 +
12147 + if (data && !spare)
12148 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
12149 + &dummy, data);
12150 + else if (spare) {
12151 + if (dev->useNANDECC) {
12152 + translate_spare2oob(spare, spareAsBytes);
12153 + ops.mode = MTD_OOB_AUTO;
12154 + ops.ooblen = 8; /* temp hack */
12155 + } else {
12156 + ops.mode = MTD_OOB_RAW;
12157 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12158 + }
12159 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12160 + ops.datbuf = (u8 *)data;
12161 + ops.ooboffs = 0;
12162 + ops.oobbuf = spareAsBytes;
12163 + retval = mtd->write_oob(mtd, addr, &ops);
12164 + }
12165 +#else
12166 + __u8 *spareAsBytes = (__u8 *) spare;
12167 +
12168 + if (data && spare) {
12169 + if (dev->useNANDECC)
12170 + retval =
12171 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12172 + &dummy, data, spareAsBytes,
12173 + &yaffs_oobinfo);
12174 + else
12175 + retval =
12176 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12177 + &dummy, data, spareAsBytes,
12178 + &yaffs_noeccinfo);
12179 + } else {
12180 + if (data)
12181 + retval =
12182 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12183 + data);
12184 + if (spare)
12185 + retval =
12186 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12187 + &dummy, spareAsBytes);
12188 + }
12189 +#endif
12190 +
12191 + if (retval == 0)
12192 + return YAFFS_OK;
12193 + else
12194 + return YAFFS_FAIL;
12195 +}
12196 +
12197 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12198 + yaffs_Spare * spare)
12199 +{
12200 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12201 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12202 + struct mtd_oob_ops ops;
12203 +#endif
12204 + size_t dummy;
12205 + int retval = 0;
12206 +
12207 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12208 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12209 + __u8 spareAsBytes[8]; /* OOB */
12210 +
12211 + if (data && !spare)
12212 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
12213 + &dummy, data);
12214 + else if (spare) {
12215 + if (dev->useNANDECC) {
12216 + ops.mode = MTD_OOB_AUTO;
12217 + ops.ooblen = 8; /* temp hack */
12218 + } else {
12219 + ops.mode = MTD_OOB_RAW;
12220 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12221 + }
12222 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12223 + ops.datbuf = data;
12224 + ops.ooboffs = 0;
12225 + ops.oobbuf = spareAsBytes;
12226 + retval = mtd->read_oob(mtd, addr, &ops);
12227 + if (dev->useNANDECC)
12228 + translate_oob2spare(spare, spareAsBytes);
12229 + }
12230 +#else
12231 + __u8 *spareAsBytes = (__u8 *) spare;
12232 +
12233 + if (data && spare) {
12234 + if (dev->useNANDECC) {
12235 + /* Careful, this call adds 2 ints */
12236 + /* to the end of the spare data. Calling function */
12237 + /* should allocate enough memory for spare, */
12238 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
12239 + retval =
12240 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12241 + &dummy, data, spareAsBytes,
12242 + &yaffs_oobinfo);
12243 + } else {
12244 + retval =
12245 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12246 + &dummy, data, spareAsBytes,
12247 + &yaffs_noeccinfo);
12248 + }
12249 + } else {
12250 + if (data)
12251 + retval =
12252 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12253 + data);
12254 + if (spare)
12255 + retval =
12256 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12257 + &dummy, spareAsBytes);
12258 + }
12259 +#endif
12260 +
12261 + if (retval == 0)
12262 + return YAFFS_OK;
12263 + else
12264 + return YAFFS_FAIL;
12265 +}
12266 +
12267 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
12268 +{
12269 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12270 + __u32 addr =
12271 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
12272 + * dev->nChunksPerBlock;
12273 + struct erase_info ei;
12274 + int retval = 0;
12275 +
12276 + ei.mtd = mtd;
12277 + ei.addr = addr;
12278 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
12279 + ei.time = 1000;
12280 + ei.retries = 2;
12281 + ei.callback = NULL;
12282 + ei.priv = (u_long) dev;
12283 +
12284 + /* Todo finish off the ei if required */
12285 +
12286 + sema_init(&dev->sem, 0);
12287 +
12288 + retval = mtd->erase(mtd, &ei);
12289 +
12290 + if (retval == 0)
12291 + return YAFFS_OK;
12292 + else
12293 + return YAFFS_FAIL;
12294 +}
12295 +
12296 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
12297 +{
12298 + return YAFFS_OK;
12299 +}
12300 +
12301 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.h
12302 ===================================================================
12303 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12304 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.h 2007-09-21 16:24:08.000000000 +0800
12305 @@ -0,0 +1,27 @@
12306 +/*
12307 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12308 + *
12309 + * Copyright (C) 2002-2007 Aleph One Ltd.
12310 + * for Toby Churchill Ltd and Brightstar Engineering
12311 + *
12312 + * Created by Charles Manning <charles@aleph1.co.uk>
12313 + *
12314 + * This program is free software; you can redistribute it and/or modify
12315 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12316 + * published by the Free Software Foundation.
12317 + *
12318 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12319 + */
12320 +
12321 +#ifndef __YAFFS_MTDIF_H__
12322 +#define __YAFFS_MTDIF_H__
12323 +
12324 +#include "yaffs_guts.h"
12325 +
12326 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12327 + const __u8 * data, const yaffs_Spare * spare);
12328 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12329 + yaffs_Spare * spare);
12330 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
12331 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
12332 +#endif
12333 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1-compat.c
12334 ===================================================================
12335 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12336 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1-compat.c 2007-09-21 16:24:08.000000000 +0800
12337 @@ -0,0 +1,434 @@
12338 +From ian@brightstareng.com Fri May 18 15:06:49 2007
12339 +From ian@brightstareng.com Fri May 18 15:08:21 2007
12340 +Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
12341 + by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
12342 + (envelope-from <ian@brightstareng.com>)
12343 + id 1Hp380-00011e-T6
12344 + for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
12345 +Received: from localhost (localhost.localdomain [127.0.0.1])
12346 + by zebra.brightstareng.com (Postfix) with ESMTP
12347 + id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
12348 +Received: from zebra.brightstareng.com ([127.0.0.1])
12349 + by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
12350 + id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
12351 +Received: from pippin (unknown [192.168.1.25])
12352 + by zebra.brightstareng.com (Postfix) with ESMTP
12353 + id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
12354 +From: Ian McDonnell <ian@brightstareng.com>
12355 +To: David Goodenough <david.goodenough@linkchoose.co.uk>
12356 +Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
12357 +Date: Fri, 18 May 2007 10:06:49 -0400
12358 +User-Agent: KMail/1.9.1
12359 +References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
12360 +In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
12361 +Cc: Andrea Conti <alyf@alyf.net>,
12362 + Charles Manning <manningc2@actrix.gen.nz>
12363 +MIME-Version: 1.0
12364 +Content-Type: Multipart/Mixed;
12365 + boundary="Boundary-00=_5LbTGmt62YoutxM"
12366 +Message-Id: <200705181006.49860.ian@brightstareng.com>
12367 +X-Virus-Scanned: by amavisd-new at brightstareng.com
12368 +Status: R
12369 +X-Status: NT
12370 +X-KMail-EncryptionState:
12371 +X-KMail-SignatureState:
12372 +X-KMail-MDN-Sent:
12373 +
12374 +--Boundary-00=_5LbTGmt62YoutxM
12375 +Content-Type: text/plain;
12376 + charset="iso-8859-15"
12377 +Content-Transfer-Encoding: 7bit
12378 +Content-Disposition: inline
12379 +
12380 +David, Andrea,
12381 +
12382 +On Friday 18 May 2007 08:34, you wrote:
12383 +> Yea team. With this fix in place (I put it in the wrong place
12384 +> at first) I can now mount and ls the Yaffs partition without
12385 +> an error messages!
12386 +
12387 +Good news!
12388 +
12389 +Attached is a newer yaffs_mtdif1.c with a bandaid to help the
12390 +2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
12391 +See the LINUX_VERSION_CODE conditional in
12392 +nandmtd1_ReadChunkWithTagsFromNAND.
12393 +
12394 +-imcd
12395 +
12396 +--Boundary-00=_5LbTGmt62YoutxM
12397 +Content-Type: text/x-csrc;
12398 + charset="iso-8859-15";
12399 + name="yaffs_mtdif1.c"
12400 +Content-Transfer-Encoding: 7bit
12401 +Content-Disposition: attachment;
12402 + filename="yaffs_mtdif1.c"
12403 +
12404 +/*
12405 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12406 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12407 + *
12408 + * Copyright (C) 2002 Aleph One Ltd.
12409 + * for Toby Churchill Ltd and Brightstar Engineering
12410 + *
12411 + * This program is free software; you can redistribute it and/or modify
12412 + * it under the terms of the GNU General Public License version 2 as
12413 + * published by the Free Software Foundation.
12414 + */
12415 +
12416 +/*
12417 + * This module provides the interface between yaffs_nand.c and the
12418 + * MTD API. This version is used when the MTD interface supports the
12419 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12420 + * and we have small-page NAND device.
12421 + *
12422 + * These functions are invoked via function pointers in yaffs_nand.c.
12423 + * This replaces functionality provided by functions in yaffs_mtdif.c
12424 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12425 + * called in yaffs_mtdif.c when the function pointers are NULL.
12426 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12427 + */
12428 +
12429 +#include "yportenv.h"
12430 +#include "yaffs_guts.h"
12431 +#include "yaffs_packedtags1.h"
12432 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12433 +
12434 +#include "linux/kernel.h"
12435 +#include "linux/version.h"
12436 +#include "linux/types.h"
12437 +#include "linux/mtd/mtd.h"
12438 +
12439 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12440 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12441 +
12442 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12443 +
12444 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12445 +# define YTAG1_SIZE 8
12446 +#else
12447 +# define YTAG1_SIZE 9
12448 +#endif
12449 +
12450 +#if 0
12451 +/* Use the following nand_ecclayout with MTD when using
12452 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12453 + * If you have existing Yaffs images and the byte order differs from this,
12454 + * adjust 'oobfree' to match your existing Yaffs data.
12455 + *
12456 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12457 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12458 + * the 9th byte.
12459 + *
12460 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12461 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12462 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12463 + * byte and B is the small-page bad-block indicator byte.
12464 + */
12465 +static struct nand_ecclayout nand_oob_16 = {
12466 + .eccbytes = 6,
12467 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12468 + .oobavail = 9,
12469 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12470 +};
12471 +#endif
12472 +
12473 +/* Write a chunk (page) of data to NAND.
12474 + *
12475 + * Caller always provides ExtendedTags data which are converted to a more
12476 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12477 + * contents of the tags meta-data; used to valid the tags when read.
12478 + *
12479 + * - Pack ExtendedTags to PackedTags1 form
12480 + * - Compute mini-ECC for PackedTags1
12481 + * - Write data and packed tags to NAND.
12482 + *
12483 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12484 + * a full sequence number (as found in the larger PackedTags2 form) it is
12485 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12486 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12487 + * to be written just once. When Yaffs performs this operation, this
12488 + * function is called with a NULL data pointer -- calling MTD write_oob
12489 + * without data is valid usage (2.6.17).
12490 + *
12491 + * Any underlying MTD error results in YAFFS_FAIL.
12492 + * Returns YAFFS_OK or YAFFS_FAIL.
12493 + */
12494 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12495 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12496 +{
12497 + struct mtd_info * mtd = dev->genericDevice;
12498 + int chunkBytes = dev->nDataBytesPerChunk;
12499 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12500 + struct mtd_oob_ops ops;
12501 + yaffs_PackedTags1 pt1;
12502 + int retval;
12503 +
12504 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12505 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12506 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12507 +
12508 + yaffs_PackTags1(&pt1, etags);
12509 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12510 +
12511 + /* When deleting a chunk, the upper layer provides only skeletal
12512 + * etags, one with chunkDeleted set. However, we need to update the
12513 + * tags, not erase them completely. So we use the NAND write property
12514 + * that only zeroed-bits stick and set tag bytes to all-ones and
12515 + * zero just the (not) deleted bit.
12516 + */
12517 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12518 + if (etags->chunkDeleted) {
12519 + memset(&pt1, 0xff, 8);
12520 + /* clear delete status bit to indicate deleted */
12521 + pt1.deleted = 0;
12522 + }
12523 +#else
12524 + ((__u8 *)&pt1)[8] = 0xff;
12525 + if (etags->chunkDeleted) {
12526 + memset(&pt1, 0xff, 8);
12527 + /* zero pageStatus byte to indicate deleted */
12528 + ((__u8 *)&pt1)[8] = 0;
12529 + }
12530 +#endif
12531 +
12532 + memset(&ops, 0, sizeof(ops));
12533 + ops.mode = MTD_OOB_AUTO;
12534 + ops.len = (data) ? chunkBytes : 0;
12535 + ops.ooblen = YTAG1_SIZE;
12536 + ops.datbuf = (__u8 *)data;
12537 + ops.oobbuf = (__u8 *)&pt1;
12538 +
12539 + retval = mtd->write_oob(mtd, addr, &ops);
12540 + if (retval) {
12541 + yaffs_trace(YAFFS_TRACE_MTD,
12542 + "write_oob failed, chunk %d, mtd error %d\n",
12543 + chunkInNAND, retval);
12544 + }
12545 + return retval ? YAFFS_FAIL : YAFFS_OK;
12546 +}
12547 +
12548 +/* Return with empty ExtendedTags but add eccResult.
12549 + */
12550 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12551 +{
12552 + if (etags) {
12553 + memset(etags, 0, sizeof(*etags));
12554 + etags->eccResult = eccResult;
12555 + }
12556 + return retval;
12557 +}
12558 +
12559 +/* Read a chunk (page) from NAND.
12560 + *
12561 + * Caller expects ExtendedTags data to be usable even on error; that is,
12562 + * all members except eccResult and blockBad are zeroed.
12563 + *
12564 + * - Check ECC results for data (if applicable)
12565 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12566 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12567 + * - Convert PackedTags1 to ExtendedTags
12568 + * - Update eccResult and blockBad members to refect state.
12569 + *
12570 + * Returns YAFFS_OK or YAFFS_FAIL.
12571 + */
12572 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12573 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12574 +{
12575 + struct mtd_info * mtd = dev->genericDevice;
12576 + int chunkBytes = dev->nDataBytesPerChunk;
12577 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12578 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12579 + struct mtd_oob_ops ops;
12580 + yaffs_PackedTags1 pt1;
12581 + int retval;
12582 + int deleted;
12583 +
12584 + memset(&ops, 0, sizeof(ops));
12585 + ops.mode = MTD_OOB_AUTO;
12586 + ops.len = (data) ? chunkBytes : 0;
12587 + ops.ooblen = YTAG1_SIZE;
12588 + ops.datbuf = data;
12589 + ops.oobbuf = (__u8 *)&pt1;
12590 +
12591 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12592 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12593 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12594 + */
12595 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12596 +#endif
12597 + /* Read page and oob using MTD.
12598 + * Check status and determine ECC result.
12599 + */
12600 + retval = mtd->read_oob(mtd, addr, &ops);
12601 + if (retval) {
12602 + yaffs_trace(YAFFS_TRACE_MTD,
12603 + "read_oob failed, chunk %d, mtd error %d\n",
12604 + chunkInNAND, retval);
12605 + }
12606 +
12607 + switch (retval) {
12608 + case 0:
12609 + /* no error */
12610 + break;
12611 +
12612 + case -EUCLEAN:
12613 + /* MTD's ECC fixed the data */
12614 + eccres = YAFFS_ECC_RESULT_FIXED;
12615 + dev->eccFixed++;
12616 + break;
12617 +
12618 + case -EBADMSG:
12619 + /* MTD's ECC could not fix the data */
12620 + dev->eccUnfixed++;
12621 + /* fall into... */
12622 + default:
12623 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12624 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12625 + return YAFFS_FAIL;
12626 + }
12627 +
12628 + /* Check for a blank/erased chunk.
12629 + */
12630 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12631 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12632 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12633 + }
12634 +
12635 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12636 + /* Read deleted status (bit) then return it to it's non-deleted
12637 + * state before performing tags mini-ECC check. pt1.deleted is
12638 + * inverted.
12639 + */
12640 + deleted = !pt1.deleted;
12641 + pt1.deleted = 1;
12642 +#else
12643 + (void) deleted; /* not used */
12644 +#endif
12645 +
12646 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12647 + */
12648 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12649 + switch (retval) {
12650 + case 0:
12651 + /* no tags error, use MTD result */
12652 + break;
12653 + case 1:
12654 + /* recovered tags-ECC error */
12655 + dev->tagsEccFixed++;
12656 + eccres = YAFFS_ECC_RESULT_FIXED;
12657 + break;
12658 + default:
12659 + /* unrecovered tags-ECC error */
12660 + dev->tagsEccUnfixed++;
12661 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12662 + }
12663 +
12664 + /* Unpack the tags to extended form and set ECC result.
12665 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12666 + */
12667 + pt1.shouldBeFF = 0xFFFFFFFF;
12668 + yaffs_UnpackTags1(etags, &pt1);
12669 + etags->eccResult = eccres;
12670 +
12671 + /* Set deleted state.
12672 + */
12673 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12674 + etags->chunkDeleted = deleted;
12675 +#else
12676 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12677 +#endif
12678 + return YAFFS_OK;
12679 +}
12680 +
12681 +/* Mark a block bad.
12682 + *
12683 + * This is a persistant state.
12684 + * Use of this function should be rare.
12685 + *
12686 + * Returns YAFFS_OK or YAFFS_FAIL.
12687 + */
12688 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12689 +{
12690 + struct mtd_info * mtd = dev->genericDevice;
12691 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12692 + int retval;
12693 +
12694 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
12695 +
12696 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12697 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12698 +}
12699 +
12700 +/* Check any MTD prerequists.
12701 + *
12702 + * Returns YAFFS_OK or YAFFS_FAIL.
12703 + */
12704 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
12705 +{
12706 + /* 2.6.18 has mtd->ecclayout->oobavail */
12707 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12708 + int oobavail = mtd->ecclayout->oobavail;
12709 +
12710 + if (oobavail < YTAG1_SIZE) {
12711 + yaffs_trace(YAFFS_TRACE_ERROR,
12712 + "mtd device has only %d bytes for tags, need %d",
12713 + oobavail, YTAG1_SIZE);
12714 + return YAFFS_FAIL;
12715 + }
12716 + return YAFFS_OK;
12717 +}
12718 +
12719 +/* Query for the current state of a specific block.
12720 + *
12721 + * Examine the tags of the first chunk of the block and return the state:
12722 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12723 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12724 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12725 + *
12726 + * Always returns YAFFS_OK.
12727 + */
12728 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12729 + yaffs_BlockState * pState, int *pSequenceNumber)
12730 +{
12731 + struct mtd_info * mtd = dev->genericDevice;
12732 + int chunkNo = blockNo * dev->nChunksPerBlock;
12733 + yaffs_ExtendedTags etags;
12734 + int state = YAFFS_BLOCK_STATE_DEAD;
12735 + int seqnum = 0;
12736 + int retval;
12737 +
12738 + /* We don't yet have a good place to test for MTD config prerequists.
12739 + * Do it here as we are called during the initial scan.
12740 + */
12741 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
12742 + return YAFFS_FAIL;
12743 + }
12744 +
12745 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12746 + if (etags.blockBad) {
12747 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12748 + "block %d is marked bad", blockNo);
12749 + state = YAFFS_BLOCK_STATE_DEAD;
12750 + }
12751 + else if (etags.chunkUsed) {
12752 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12753 + seqnum = etags.sequenceNumber;
12754 + }
12755 + else {
12756 + state = YAFFS_BLOCK_STATE_EMPTY;
12757 + }
12758 +
12759 + *pState = state;
12760 + *pSequenceNumber = seqnum;
12761 +
12762 + /* query always succeeds */
12763 + return YAFFS_OK;
12764 +}
12765 +
12766 +#endif /*KERNEL_VERSION*/
12767 +
12768 +--Boundary-00=_5LbTGmt62YoutxM--
12769 +
12770 +
12771 +
12772 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.c
12773 ===================================================================
12774 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12775 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.c 2007-09-21 16:24:08.000000000 +0800
12776 @@ -0,0 +1,363 @@
12777 +/*
12778 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12779 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12780 + *
12781 + * Copyright (C) 2002 Aleph One Ltd.
12782 + * for Toby Churchill Ltd and Brightstar Engineering
12783 + *
12784 + * This program is free software; you can redistribute it and/or modify
12785 + * it under the terms of the GNU General Public License version 2 as
12786 + * published by the Free Software Foundation.
12787 + */
12788 +
12789 +/*
12790 + * This module provides the interface between yaffs_nand.c and the
12791 + * MTD API. This version is used when the MTD interface supports the
12792 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12793 + * and we have small-page NAND device.
12794 + *
12795 + * These functions are invoked via function pointers in yaffs_nand.c.
12796 + * This replaces functionality provided by functions in yaffs_mtdif.c
12797 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12798 + * called in yaffs_mtdif.c when the function pointers are NULL.
12799 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12800 + */
12801 +
12802 +#include "yportenv.h"
12803 +#include "yaffs_guts.h"
12804 +#include "yaffs_packedtags1.h"
12805 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12806 +
12807 +#include "linux/kernel.h"
12808 +#include "linux/version.h"
12809 +#include "linux/types.h"
12810 +#include "linux/mtd/mtd.h"
12811 +
12812 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12813 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12814 +
12815 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12816 +
12817 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12818 +# define YTAG1_SIZE 8
12819 +#else
12820 +# define YTAG1_SIZE 9
12821 +#endif
12822 +
12823 +#if 0
12824 +/* Use the following nand_ecclayout with MTD when using
12825 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12826 + * If you have existing Yaffs images and the byte order differs from this,
12827 + * adjust 'oobfree' to match your existing Yaffs data.
12828 + *
12829 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12830 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12831 + * the 9th byte.
12832 + *
12833 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12834 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12835 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12836 + * byte and B is the small-page bad-block indicator byte.
12837 + */
12838 +static struct nand_ecclayout nand_oob_16 = {
12839 + .eccbytes = 6,
12840 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12841 + .oobavail = 9,
12842 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12843 +};
12844 +#endif
12845 +
12846 +/* Write a chunk (page) of data to NAND.
12847 + *
12848 + * Caller always provides ExtendedTags data which are converted to a more
12849 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12850 + * contents of the tags meta-data; used to valid the tags when read.
12851 + *
12852 + * - Pack ExtendedTags to PackedTags1 form
12853 + * - Compute mini-ECC for PackedTags1
12854 + * - Write data and packed tags to NAND.
12855 + *
12856 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12857 + * a full sequence number (as found in the larger PackedTags2 form) it is
12858 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12859 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12860 + * to be written just once. When Yaffs performs this operation, this
12861 + * function is called with a NULL data pointer -- calling MTD write_oob
12862 + * without data is valid usage (2.6.17).
12863 + *
12864 + * Any underlying MTD error results in YAFFS_FAIL.
12865 + * Returns YAFFS_OK or YAFFS_FAIL.
12866 + */
12867 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12868 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12869 +{
12870 + struct mtd_info * mtd = dev->genericDevice;
12871 + int chunkBytes = dev->nDataBytesPerChunk;
12872 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12873 + struct mtd_oob_ops ops;
12874 + yaffs_PackedTags1 pt1;
12875 + int retval;
12876 +
12877 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12878 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12879 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12880 +
12881 + dev->nPageWrites++;
12882 +
12883 + yaffs_PackTags1(&pt1, etags);
12884 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12885 +
12886 + /* When deleting a chunk, the upper layer provides only skeletal
12887 + * etags, one with chunkDeleted set. However, we need to update the
12888 + * tags, not erase them completely. So we use the NAND write property
12889 + * that only zeroed-bits stick and set tag bytes to all-ones and
12890 + * zero just the (not) deleted bit.
12891 + */
12892 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12893 + if (etags->chunkDeleted) {
12894 + memset(&pt1, 0xff, 8);
12895 + /* clear delete status bit to indicate deleted */
12896 + pt1.deleted = 0;
12897 + }
12898 +#else
12899 + ((__u8 *)&pt1)[8] = 0xff;
12900 + if (etags->chunkDeleted) {
12901 + memset(&pt1, 0xff, 8);
12902 + /* zero pageStatus byte to indicate deleted */
12903 + ((__u8 *)&pt1)[8] = 0;
12904 + }
12905 +#endif
12906 +
12907 + memset(&ops, 0, sizeof(ops));
12908 + ops.mode = MTD_OOB_AUTO;
12909 + ops.len = (data) ? chunkBytes : 0;
12910 + ops.ooblen = YTAG1_SIZE;
12911 + ops.datbuf = (__u8 *)data;
12912 + ops.oobbuf = (__u8 *)&pt1;
12913 +
12914 + retval = mtd->write_oob(mtd, addr, &ops);
12915 + if (retval) {
12916 + yaffs_trace(YAFFS_TRACE_MTD,
12917 + "write_oob failed, chunk %d, mtd error %d\n",
12918 + chunkInNAND, retval);
12919 + }
12920 + return retval ? YAFFS_FAIL : YAFFS_OK;
12921 +}
12922 +
12923 +/* Return with empty ExtendedTags but add eccResult.
12924 + */
12925 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12926 +{
12927 + if (etags) {
12928 + memset(etags, 0, sizeof(*etags));
12929 + etags->eccResult = eccResult;
12930 + }
12931 + return retval;
12932 +}
12933 +
12934 +/* Read a chunk (page) from NAND.
12935 + *
12936 + * Caller expects ExtendedTags data to be usable even on error; that is,
12937 + * all members except eccResult and blockBad are zeroed.
12938 + *
12939 + * - Check ECC results for data (if applicable)
12940 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12941 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12942 + * - Convert PackedTags1 to ExtendedTags
12943 + * - Update eccResult and blockBad members to refect state.
12944 + *
12945 + * Returns YAFFS_OK or YAFFS_FAIL.
12946 + */
12947 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12948 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12949 +{
12950 + struct mtd_info * mtd = dev->genericDevice;
12951 + int chunkBytes = dev->nDataBytesPerChunk;
12952 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12953 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12954 + struct mtd_oob_ops ops;
12955 + yaffs_PackedTags1 pt1;
12956 + int retval;
12957 + int deleted;
12958 +
12959 + dev->nPageReads++;
12960 +
12961 + memset(&ops, 0, sizeof(ops));
12962 + ops.mode = MTD_OOB_AUTO;
12963 + ops.len = (data) ? chunkBytes : 0;
12964 + ops.ooblen = YTAG1_SIZE;
12965 + ops.datbuf = data;
12966 + ops.oobbuf = (__u8 *)&pt1;
12967 +
12968 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12969 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12970 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12971 + */
12972 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12973 +#endif
12974 + /* Read page and oob using MTD.
12975 + * Check status and determine ECC result.
12976 + */
12977 + retval = mtd->read_oob(mtd, addr, &ops);
12978 + if (retval) {
12979 + yaffs_trace(YAFFS_TRACE_MTD,
12980 + "read_oob failed, chunk %d, mtd error %d\n",
12981 + chunkInNAND, retval);
12982 + }
12983 +
12984 + switch (retval) {
12985 + case 0:
12986 + /* no error */
12987 + break;
12988 +
12989 + case -EUCLEAN:
12990 + /* MTD's ECC fixed the data */
12991 + eccres = YAFFS_ECC_RESULT_FIXED;
12992 + dev->eccFixed++;
12993 + break;
12994 +
12995 + case -EBADMSG:
12996 + /* MTD's ECC could not fix the data */
12997 + dev->eccUnfixed++;
12998 + /* fall into... */
12999 + default:
13000 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
13001 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
13002 + return YAFFS_FAIL;
13003 + }
13004 +
13005 + /* Check for a blank/erased chunk.
13006 + */
13007 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
13008 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
13009 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
13010 + }
13011 +
13012 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
13013 + /* Read deleted status (bit) then return it to it's non-deleted
13014 + * state before performing tags mini-ECC check. pt1.deleted is
13015 + * inverted.
13016 + */
13017 + deleted = !pt1.deleted;
13018 + pt1.deleted = 1;
13019 +#else
13020 + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
13021 +#endif
13022 +
13023 + /* Check the packed tags mini-ECC and correct if necessary/possible.
13024 + */
13025 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
13026 + switch (retval) {
13027 + case 0:
13028 + /* no tags error, use MTD result */
13029 + break;
13030 + case 1:
13031 + /* recovered tags-ECC error */
13032 + dev->tagsEccFixed++;
13033 + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
13034 + eccres = YAFFS_ECC_RESULT_FIXED;
13035 + break;
13036 + default:
13037 + /* unrecovered tags-ECC error */
13038 + dev->tagsEccUnfixed++;
13039 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
13040 + }
13041 +
13042 + /* Unpack the tags to extended form and set ECC result.
13043 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
13044 + */
13045 + pt1.shouldBeFF = 0xFFFFFFFF;
13046 + yaffs_UnpackTags1(etags, &pt1);
13047 + etags->eccResult = eccres;
13048 +
13049 + /* Set deleted state */
13050 + etags->chunkDeleted = deleted;
13051 + return YAFFS_OK;
13052 +}
13053 +
13054 +/* Mark a block bad.
13055 + *
13056 + * This is a persistant state.
13057 + * Use of this function should be rare.
13058 + *
13059 + * Returns YAFFS_OK or YAFFS_FAIL.
13060 + */
13061 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13062 +{
13063 + struct mtd_info * mtd = dev->genericDevice;
13064 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
13065 + int retval;
13066 +
13067 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
13068 +
13069 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
13070 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
13071 +}
13072 +
13073 +/* Check any MTD prerequists.
13074 + *
13075 + * Returns YAFFS_OK or YAFFS_FAIL.
13076 + */
13077 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
13078 +{
13079 + /* 2.6.18 has mtd->ecclayout->oobavail */
13080 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
13081 + int oobavail = mtd->ecclayout->oobavail;
13082 +
13083 + if (oobavail < YTAG1_SIZE) {
13084 + yaffs_trace(YAFFS_TRACE_ERROR,
13085 + "mtd device has only %d bytes for tags, need %d\n",
13086 + oobavail, YTAG1_SIZE);
13087 + return YAFFS_FAIL;
13088 + }
13089 + return YAFFS_OK;
13090 +}
13091 +
13092 +/* Query for the current state of a specific block.
13093 + *
13094 + * Examine the tags of the first chunk of the block and return the state:
13095 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
13096 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
13097 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
13098 + *
13099 + * Always returns YAFFS_OK.
13100 + */
13101 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13102 + yaffs_BlockState * pState, int *pSequenceNumber)
13103 +{
13104 + struct mtd_info * mtd = dev->genericDevice;
13105 + int chunkNo = blockNo * dev->nChunksPerBlock;
13106 + yaffs_ExtendedTags etags;
13107 + int state = YAFFS_BLOCK_STATE_DEAD;
13108 + int seqnum = 0;
13109 + int retval;
13110 +
13111 + /* We don't yet have a good place to test for MTD config prerequists.
13112 + * Do it here as we are called during the initial scan.
13113 + */
13114 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
13115 + return YAFFS_FAIL;
13116 + }
13117 +
13118 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
13119 + if (etags.blockBad) {
13120 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
13121 + "block %d is marked bad", blockNo);
13122 + state = YAFFS_BLOCK_STATE_DEAD;
13123 + }
13124 + else if (etags.chunkUsed) {
13125 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13126 + seqnum = etags.sequenceNumber;
13127 + }
13128 + else {
13129 + state = YAFFS_BLOCK_STATE_EMPTY;
13130 + }
13131 +
13132 + *pState = state;
13133 + *pSequenceNumber = seqnum;
13134 +
13135 + /* query always succeeds */
13136 + return YAFFS_OK;
13137 +}
13138 +
13139 +#endif /*KERNEL_VERSION*/
13140 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.h
13141 ===================================================================
13142 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13143 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.h 2007-09-21 16:24:08.000000000 +0800
13144 @@ -0,0 +1,28 @@
13145 +/*
13146 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
13147 + *
13148 + * Copyright (C) 2002-2007 Aleph One Ltd.
13149 + * for Toby Churchill Ltd and Brightstar Engineering
13150 + *
13151 + * This program is free software; you can redistribute it and/or modify
13152 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13153 + * published by the Free Software Foundation.
13154 + *
13155 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13156 + */
13157 +
13158 +#ifndef __YAFFS_MTDIF1_H__
13159 +#define __YAFFS_MTDIF1_H__
13160 +
13161 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13162 + const __u8 * data, const yaffs_ExtendedTags * tags);
13163 +
13164 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13165 + __u8 * data, yaffs_ExtendedTags * tags);
13166 +
13167 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13168 +
13169 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13170 + yaffs_BlockState * state, int *sequenceNumber);
13171 +
13172 +#endif
13173 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.c
13174 ===================================================================
13175 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13176 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.c 2007-09-21 16:24:08.000000000 +0800
13177 @@ -0,0 +1,232 @@
13178 +/*
13179 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13180 + *
13181 + * Copyright (C) 2002-2007 Aleph One Ltd.
13182 + * for Toby Churchill Ltd and Brightstar Engineering
13183 + *
13184 + * Created by Charles Manning <charles@aleph1.co.uk>
13185 + *
13186 + * This program is free software; you can redistribute it and/or modify
13187 + * it under the terms of the GNU General Public License version 2 as
13188 + * published by the Free Software Foundation.
13189 + */
13190 +
13191 +/* mtd interface for YAFFS2 */
13192 +
13193 +const char *yaffs_mtdif2_c_version =
13194 + "$Id: yaffs_mtdif2.c,v 1.17 2007-02-14 01:09:06 wookey Exp $";
13195 +
13196 +#include "yportenv.h"
13197 +
13198 +
13199 +#include "yaffs_mtdif2.h"
13200 +
13201 +#include "linux/mtd/mtd.h"
13202 +#include "linux/types.h"
13203 +#include "linux/time.h"
13204 +
13205 +#include "yaffs_packedtags2.h"
13206 +
13207 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13208 + const __u8 * data,
13209 + const yaffs_ExtendedTags * tags)
13210 +{
13211 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13212 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13213 + struct mtd_oob_ops ops;
13214 +#else
13215 + size_t dummy;
13216 +#endif
13217 + int retval = 0;
13218 +
13219 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13220 +
13221 + yaffs_PackedTags2 pt;
13222 +
13223 + T(YAFFS_TRACE_MTD,
13224 + (TSTR
13225 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
13226 + TENDSTR), chunkInNAND, data, tags));
13227 +
13228 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13229 + if (tags)
13230 + yaffs_PackTags2(&pt, tags);
13231 + else
13232 + BUG(); /* both tags and data should always be present */
13233 +
13234 + if (data) {
13235 + ops.mode = MTD_OOB_AUTO;
13236 + ops.ooblen = sizeof(pt);
13237 + ops.len = dev->nDataBytesPerChunk;
13238 + ops.ooboffs = 0;
13239 + ops.datbuf = (__u8 *)data;
13240 + ops.oobbuf = (void *)&pt;
13241 + retval = mtd->write_oob(mtd, addr, &ops);
13242 + } else
13243 + BUG(); /* both tags and data should always be present */
13244 +#else
13245 + if (tags) {
13246 + yaffs_PackTags2(&pt, tags);
13247 + }
13248 +
13249 + if (data && tags) {
13250 + if (dev->useNANDECC)
13251 + retval =
13252 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13253 + &dummy, data, (__u8 *) & pt, NULL);
13254 + else
13255 + retval =
13256 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13257 + &dummy, data, (__u8 *) & pt, NULL);
13258 + } else {
13259 + if (data)
13260 + retval =
13261 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13262 + data);
13263 + if (tags)
13264 + retval =
13265 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
13266 + (__u8 *) & pt);
13267 +
13268 + }
13269 +#endif
13270 +
13271 + if (retval == 0)
13272 + return YAFFS_OK;
13273 + else
13274 + return YAFFS_FAIL;
13275 +}
13276 +
13277 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13278 + __u8 * data, yaffs_ExtendedTags * tags)
13279 +{
13280 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13281 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13282 + struct mtd_oob_ops ops;
13283 +#endif
13284 + size_t dummy;
13285 + int retval = 0;
13286 +
13287 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13288 +
13289 + yaffs_PackedTags2 pt;
13290 +
13291 + T(YAFFS_TRACE_MTD,
13292 + (TSTR
13293 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
13294 + TENDSTR), chunkInNAND, data, tags));
13295 +
13296 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13297 + if (data && !tags)
13298 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13299 + &dummy, data);
13300 + else if (tags) {
13301 + ops.mode = MTD_OOB_AUTO;
13302 + ops.ooblen = sizeof(pt);
13303 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13304 + ops.ooboffs = 0;
13305 + ops.datbuf = data;
13306 + ops.oobbuf = dev->spareBuffer;
13307 + retval = mtd->read_oob(mtd, addr, &ops);
13308 + }
13309 +#else
13310 + if (data && tags) {
13311 + if (dev->useNANDECC) {
13312 + retval =
13313 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13314 + &dummy, data, dev->spareBuffer,
13315 + NULL);
13316 + } else {
13317 + retval =
13318 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13319 + &dummy, data, dev->spareBuffer,
13320 + NULL);
13321 + }
13322 + } else {
13323 + if (data)
13324 + retval =
13325 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13326 + data);
13327 + if (tags)
13328 + retval =
13329 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13330 + dev->spareBuffer);
13331 + }
13332 +#endif
13333 +
13334 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13335 +
13336 + if (tags)
13337 + yaffs_UnpackTags2(tags, &pt);
13338 +
13339 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13340 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13341 +
13342 + if (retval == 0)
13343 + return YAFFS_OK;
13344 + else
13345 + return YAFFS_FAIL;
13346 +}
13347 +
13348 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13349 +{
13350 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13351 + int retval;
13352 + T(YAFFS_TRACE_MTD,
13353 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13354 +
13355 + retval =
13356 + mtd->block_markbad(mtd,
13357 + blockNo * dev->nChunksPerBlock *
13358 + dev->nDataBytesPerChunk);
13359 +
13360 + if (retval == 0)
13361 + return YAFFS_OK;
13362 + else
13363 + return YAFFS_FAIL;
13364 +
13365 +}
13366 +
13367 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13368 + yaffs_BlockState * state, int *sequenceNumber)
13369 +{
13370 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13371 + int retval;
13372 +
13373 + T(YAFFS_TRACE_MTD,
13374 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13375 + retval =
13376 + mtd->block_isbad(mtd,
13377 + blockNo * dev->nChunksPerBlock *
13378 + dev->nDataBytesPerChunk);
13379 +
13380 + if (retval) {
13381 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13382 +
13383 + *state = YAFFS_BLOCK_STATE_DEAD;
13384 + *sequenceNumber = 0;
13385 + } else {
13386 + yaffs_ExtendedTags t;
13387 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13388 + blockNo *
13389 + dev->nChunksPerBlock, NULL,
13390 + &t);
13391 +
13392 + if (t.chunkUsed) {
13393 + *sequenceNumber = t.sequenceNumber;
13394 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13395 + } else {
13396 + *sequenceNumber = 0;
13397 + *state = YAFFS_BLOCK_STATE_EMPTY;
13398 + }
13399 + }
13400 + T(YAFFS_TRACE_MTD,
13401 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13402 + *state));
13403 +
13404 + if (retval == 0)
13405 + return YAFFS_OK;
13406 + else
13407 + return YAFFS_FAIL;
13408 +}
13409 +
13410 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.h
13411 ===================================================================
13412 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13413 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.h 2007-09-21 16:24:08.000000000 +0800
13414 @@ -0,0 +1,29 @@
13415 +/*
13416 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13417 + *
13418 + * Copyright (C) 2002-2007 Aleph One Ltd.
13419 + * for Toby Churchill Ltd and Brightstar Engineering
13420 + *
13421 + * Created by Charles Manning <charles@aleph1.co.uk>
13422 + *
13423 + * This program is free software; you can redistribute it and/or modify
13424 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13425 + * published by the Free Software Foundation.
13426 + *
13427 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13428 + */
13429 +
13430 +#ifndef __YAFFS_MTDIF2_H__
13431 +#define __YAFFS_MTDIF2_H__
13432 +
13433 +#include "yaffs_guts.h"
13434 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13435 + const __u8 * data,
13436 + const yaffs_ExtendedTags * tags);
13437 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13438 + __u8 * data, yaffs_ExtendedTags * tags);
13439 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13440 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13441 + yaffs_BlockState * state, int *sequenceNumber);
13442 +
13443 +#endif
13444 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.c
13445 ===================================================================
13446 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13447 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.c 2007-09-21 16:24:08.000000000 +0800
13448 @@ -0,0 +1,134 @@
13449 +/*
13450 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13451 + *
13452 + * Copyright (C) 2002-2007 Aleph One Ltd.
13453 + * for Toby Churchill Ltd and Brightstar Engineering
13454 + *
13455 + * Created by Charles Manning <charles@aleph1.co.uk>
13456 + *
13457 + * This program is free software; you can redistribute it and/or modify
13458 + * it under the terms of the GNU General Public License version 2 as
13459 + * published by the Free Software Foundation.
13460 + */
13461 +
13462 +const char *yaffs_nand_c_version =
13463 + "$Id: yaffs_nand.c,v 1.7 2007-02-14 01:09:06 wookey Exp $";
13464 +
13465 +#include "yaffs_nand.h"
13466 +#include "yaffs_tagscompat.h"
13467 +#include "yaffs_tagsvalidity.h"
13468 +
13469 +
13470 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13471 + __u8 * buffer,
13472 + yaffs_ExtendedTags * tags)
13473 +{
13474 + int result;
13475 + yaffs_ExtendedTags localTags;
13476 +
13477 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13478 +
13479 + /* If there are no tags provided, use local tags to get prioritised gc working */
13480 + if(!tags)
13481 + tags = &localTags;
13482 +
13483 + if (dev->readChunkWithTagsFromNAND)
13484 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13485 + tags);
13486 + else
13487 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13488 + realignedChunkInNAND,
13489 + buffer,
13490 + tags);
13491 + if(tags &&
13492 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
13493 +
13494 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13495 + yaffs_HandleChunkError(dev,bi);
13496 + }
13497 +
13498 + return result;
13499 +}
13500 +
13501 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13502 + int chunkInNAND,
13503 + const __u8 * buffer,
13504 + yaffs_ExtendedTags * tags)
13505 +{
13506 + chunkInNAND -= dev->chunkOffset;
13507 +
13508 +
13509 + if (tags) {
13510 + tags->sequenceNumber = dev->sequenceNumber;
13511 + tags->chunkUsed = 1;
13512 + if (!yaffs_ValidateTags(tags)) {
13513 + T(YAFFS_TRACE_ERROR,
13514 + (TSTR("Writing uninitialised tags" TENDSTR)));
13515 + YBUG();
13516 + }
13517 + T(YAFFS_TRACE_WRITE,
13518 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13519 + tags->objectId, tags->chunkId));
13520 + } else {
13521 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13522 + YBUG();
13523 + }
13524 +
13525 + if (dev->writeChunkWithTagsToNAND)
13526 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13527 + tags);
13528 + else
13529 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13530 + chunkInNAND,
13531 + buffer,
13532 + tags);
13533 +}
13534 +
13535 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
13536 +{
13537 + blockNo -= dev->blockOffset;
13538 +
13539 +;
13540 + if (dev->markNANDBlockBad)
13541 + return dev->markNANDBlockBad(dev, blockNo);
13542 + else
13543 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13544 +}
13545 +
13546 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13547 + int blockNo,
13548 + yaffs_BlockState * state,
13549 + unsigned *sequenceNumber)
13550 +{
13551 + blockNo -= dev->blockOffset;
13552 +
13553 + if (dev->queryNANDBlock)
13554 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13555 + else
13556 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13557 + state,
13558 + sequenceNumber);
13559 +}
13560 +
13561 +
13562 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13563 + int blockInNAND)
13564 +{
13565 + int result;
13566 +
13567 + blockInNAND -= dev->blockOffset;
13568 +
13569 +
13570 + dev->nBlockErasures++;
13571 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13572 +
13573 + return result;
13574 +}
13575 +
13576 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13577 +{
13578 + return dev->initialiseNAND(dev);
13579 +}
13580 +
13581 +
13582 +
13583 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.h
13584 ===================================================================
13585 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13586 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.h 2007-09-21 16:24:08.000000000 +0800
13587 @@ -0,0 +1,44 @@
13588 +/*
13589 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13590 + *
13591 + * Copyright (C) 2002-2007 Aleph One Ltd.
13592 + * for Toby Churchill Ltd and Brightstar Engineering
13593 + *
13594 + * Created by Charles Manning <charles@aleph1.co.uk>
13595 + *
13596 + * This program is free software; you can redistribute it and/or modify
13597 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13598 + * published by the Free Software Foundation.
13599 + *
13600 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13601 + */
13602 +
13603 +#ifndef __YAFFS_NAND_H__
13604 +#define __YAFFS_NAND_H__
13605 +#include "yaffs_guts.h"
13606 +
13607 +
13608 +
13609 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13610 + __u8 * buffer,
13611 + yaffs_ExtendedTags * tags);
13612 +
13613 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13614 + int chunkInNAND,
13615 + const __u8 * buffer,
13616 + yaffs_ExtendedTags * tags);
13617 +
13618 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
13619 +
13620 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13621 + int blockNo,
13622 + yaffs_BlockState * state,
13623 + unsigned *sequenceNumber);
13624 +
13625 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13626 + int blockInNAND);
13627 +
13628 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13629 +
13630 +#endif
13631 +
13632 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nandemul2k.h
13633 ===================================================================
13634 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13635 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nandemul2k.h 2007-09-21 16:24:08.000000000 +0800
13636 @@ -0,0 +1,39 @@
13637 +/*
13638 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13639 + *
13640 + * Copyright (C) 2002-2007 Aleph One Ltd.
13641 + * for Toby Churchill Ltd and Brightstar Engineering
13642 + *
13643 + * Created by Charles Manning <charles@aleph1.co.uk>
13644 + *
13645 + * This program is free software; you can redistribute it and/or modify
13646 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13647 + * published by the Free Software Foundation.
13648 + *
13649 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13650 + */
13651 +
13652 +/* Interface to emulated NAND functions (2k page size) */
13653 +
13654 +#ifndef __YAFFS_NANDEMUL2K_H__
13655 +#define __YAFFS_NANDEMUL2K_H__
13656 +
13657 +#include "yaffs_guts.h"
13658 +
13659 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13660 + int chunkInNAND, const __u8 * data,
13661 + yaffs_ExtendedTags * tags);
13662 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13663 + int chunkInNAND, __u8 * data,
13664 + yaffs_ExtendedTags * tags);
13665 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13666 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13667 + yaffs_BlockState * state, int *sequenceNumber);
13668 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13669 + int blockInNAND);
13670 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13671 +int nandemul2k_GetBytesPerChunk(void);
13672 +int nandemul2k_GetChunksPerBlock(void);
13673 +int nandemul2k_GetNumberOfBlocks(void);
13674 +
13675 +#endif
13676 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.c
13677 ===================================================================
13678 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13679 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.c 2007-09-21 16:24:08.000000000 +0800
13680 @@ -0,0 +1,52 @@
13681 +/*
13682 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13683 + *
13684 + * Copyright (C) 2002-2007 Aleph One Ltd.
13685 + * for Toby Churchill Ltd and Brightstar Engineering
13686 + *
13687 + * Created by Charles Manning <charles@aleph1.co.uk>
13688 + *
13689 + * This program is free software; you can redistribute it and/or modify
13690 + * it under the terms of the GNU General Public License version 2 as
13691 + * published by the Free Software Foundation.
13692 + */
13693 +
13694 +#include "yaffs_packedtags1.h"
13695 +#include "yportenv.h"
13696 +
13697 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
13698 +{
13699 + pt->chunkId = t->chunkId;
13700 + pt->serialNumber = t->serialNumber;
13701 + pt->byteCount = t->byteCount;
13702 + pt->objectId = t->objectId;
13703 + pt->ecc = 0;
13704 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13705 + pt->unusedStuff = 0;
13706 + pt->shouldBeFF = 0xFFFFFFFF;
13707 +
13708 +}
13709 +
13710 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
13711 +{
13712 + static const __u8 allFF[] =
13713 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13714 +0xff };
13715 +
13716 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13717 + t->blockBad = 0;
13718 + if (pt->shouldBeFF != 0xFFFFFFFF) {
13719 + t->blockBad = 1;
13720 + }
13721 + t->chunkUsed = 1;
13722 + t->objectId = pt->objectId;
13723 + t->chunkId = pt->chunkId;
13724 + t->byteCount = pt->byteCount;
13725 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13726 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13727 + t->serialNumber = pt->serialNumber;
13728 + } else {
13729 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13730 +
13731 + }
13732 +}
13733 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.h
13734 ===================================================================
13735 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13736 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.h 2007-09-21 16:24:08.000000000 +0800
13737 @@ -0,0 +1,37 @@
13738 +/*
13739 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13740 + *
13741 + * Copyright (C) 2002-2007 Aleph One Ltd.
13742 + * for Toby Churchill Ltd and Brightstar Engineering
13743 + *
13744 + * Created by Charles Manning <charles@aleph1.co.uk>
13745 + *
13746 + * This program is free software; you can redistribute it and/or modify
13747 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13748 + * published by the Free Software Foundation.
13749 + *
13750 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13751 + */
13752 +
13753 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13754 +
13755 +#ifndef __YAFFS_PACKEDTAGS1_H__
13756 +#define __YAFFS_PACKEDTAGS1_H__
13757 +
13758 +#include "yaffs_guts.h"
13759 +
13760 +typedef struct {
13761 + unsigned chunkId:20;
13762 + unsigned serialNumber:2;
13763 + unsigned byteCount:10;
13764 + unsigned objectId:18;
13765 + unsigned ecc:12;
13766 + unsigned deleted:1;
13767 + unsigned unusedStuff:1;
13768 + unsigned shouldBeFF;
13769 +
13770 +} yaffs_PackedTags1;
13771 +
13772 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
13773 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
13774 +#endif
13775 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.c
13776 ===================================================================
13777 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13778 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.c 2007-09-21 16:24:08.000000000 +0800
13779 @@ -0,0 +1,182 @@
13780 +/*
13781 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13782 + *
13783 + * Copyright (C) 2002-2007 Aleph One Ltd.
13784 + * for Toby Churchill Ltd and Brightstar Engineering
13785 + *
13786 + * Created by Charles Manning <charles@aleph1.co.uk>
13787 + *
13788 + * This program is free software; you can redistribute it and/or modify
13789 + * it under the terms of the GNU General Public License version 2 as
13790 + * published by the Free Software Foundation.
13791 + */
13792 +
13793 +#include "yaffs_packedtags2.h"
13794 +#include "yportenv.h"
13795 +#include "yaffs_tagsvalidity.h"
13796 +
13797 +/* This code packs a set of extended tags into a binary structure for
13798 + * NAND storage
13799 + */
13800 +
13801 +/* Some of the information is "extra" struff which can be packed in to
13802 + * speed scanning
13803 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13804 + */
13805 +
13806 +/* Extra flags applied to chunkId */
13807 +
13808 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13809 +#define EXTRA_SHRINK_FLAG 0x40000000
13810 +#define EXTRA_SHADOWS_FLAG 0x20000000
13811 +#define EXTRA_SPARE_FLAGS 0x10000000
13812 +
13813 +#define ALL_EXTRA_FLAGS 0xF0000000
13814 +
13815 +/* Also, the top 4 bits of the object Id are set to the object type. */
13816 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13817 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13818 +
13819 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
13820 +{
13821 + T(YAFFS_TRACE_MTD,
13822 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13823 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
13824 + pt->t.sequenceNumber));
13825 +}
13826 +
13827 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
13828 +{
13829 + T(YAFFS_TRACE_MTD,
13830 + (TSTR
13831 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
13832 + "%d del %d ser %d seq %d"
13833 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13834 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13835 + t->sequenceNumber));
13836 +
13837 +}
13838 +
13839 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
13840 +{
13841 + pt->t.chunkId = t->chunkId;
13842 + pt->t.sequenceNumber = t->sequenceNumber;
13843 + pt->t.byteCount = t->byteCount;
13844 + pt->t.objectId = t->objectId;
13845 +
13846 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13847 + /* Store the extra header info instead */
13848 + /* We save the parent object in the chunkId */
13849 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
13850 + | t->extraParentObjectId;
13851 + if (t->extraIsShrinkHeader) {
13852 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
13853 + }
13854 + if (t->extraShadows) {
13855 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
13856 + }
13857 +
13858 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13859 + pt->t.objectId |=
13860 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13861 +
13862 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13863 + pt->t.byteCount = t->extraEquivalentObjectId;
13864 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
13865 + pt->t.byteCount = t->extraFileLength;
13866 + } else {
13867 + pt->t.byteCount = 0;
13868 + }
13869 + }
13870 +
13871 + yaffs_DumpPackedTags2(pt);
13872 + yaffs_DumpTags2(t);
13873 +
13874 +#ifndef YAFFS_IGNORE_TAGS_ECC
13875 + {
13876 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13877 + sizeof(yaffs_PackedTags2TagsPart),
13878 + &pt->ecc);
13879 + }
13880 +#endif
13881 +}
13882 +
13883 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
13884 +{
13885 +
13886 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13887 +
13888 + yaffs_InitialiseTags(t);
13889 +
13890 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13891 + /* Page is in use */
13892 +#ifdef YAFFS_IGNORE_TAGS_ECC
13893 + {
13894 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13895 + }
13896 +#else
13897 + {
13898 + yaffs_ECCOther ecc;
13899 + int result;
13900 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13901 + sizeof
13902 + (yaffs_PackedTags2TagsPart),
13903 + &ecc);
13904 + result =
13905 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13906 + sizeof
13907 + (yaffs_PackedTags2TagsPart),
13908 + &pt->ecc, &ecc);
13909 + switch(result){
13910 + case 0:
13911 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13912 + break;
13913 + case 1:
13914 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
13915 + break;
13916 + case -1:
13917 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13918 + break;
13919 + default:
13920 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13921 + }
13922 + }
13923 +#endif
13924 + t->blockBad = 0;
13925 + t->chunkUsed = 1;
13926 + t->objectId = pt->t.objectId;
13927 + t->chunkId = pt->t.chunkId;
13928 + t->byteCount = pt->t.byteCount;
13929 + t->chunkDeleted = 0;
13930 + t->serialNumber = 0;
13931 + t->sequenceNumber = pt->t.sequenceNumber;
13932 +
13933 + /* Do extra header info stuff */
13934 +
13935 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
13936 + t->chunkId = 0;
13937 + t->byteCount = 0;
13938 +
13939 + t->extraHeaderInfoAvailable = 1;
13940 + t->extraParentObjectId =
13941 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
13942 + t->extraIsShrinkHeader =
13943 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13944 + t->extraShadows =
13945 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13946 + t->extraObjectType =
13947 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13948 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13949 +
13950 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13951 + t->extraEquivalentObjectId = pt->t.byteCount;
13952 + } else {
13953 + t->extraFileLength = pt->t.byteCount;
13954 + }
13955 + }
13956 + }
13957 +
13958 + yaffs_DumpPackedTags2(pt);
13959 + yaffs_DumpTags2(t);
13960 +
13961 +}
13962 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.h
13963 ===================================================================
13964 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13965 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.h 2007-09-21 16:24:08.000000000 +0800
13966 @@ -0,0 +1,38 @@
13967 +/*
13968 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13969 + *
13970 + * Copyright (C) 2002-2007 Aleph One Ltd.
13971 + * for Toby Churchill Ltd and Brightstar Engineering
13972 + *
13973 + * Created by Charles Manning <charles@aleph1.co.uk>
13974 + *
13975 + * This program is free software; you can redistribute it and/or modify
13976 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13977 + * published by the Free Software Foundation.
13978 + *
13979 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13980 + */
13981 +
13982 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13983 +
13984 +#ifndef __YAFFS_PACKEDTAGS2_H__
13985 +#define __YAFFS_PACKEDTAGS2_H__
13986 +
13987 +#include "yaffs_guts.h"
13988 +#include "yaffs_ecc.h"
13989 +
13990 +typedef struct {
13991 + unsigned sequenceNumber;
13992 + unsigned objectId;
13993 + unsigned chunkId;
13994 + unsigned byteCount;
13995 +} yaffs_PackedTags2TagsPart;
13996 +
13997 +typedef struct {
13998 + yaffs_PackedTags2TagsPart t;
13999 + yaffs_ECCOther ecc;
14000 +} yaffs_PackedTags2;
14001 +
14002 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
14003 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
14004 +#endif
14005 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.c
14006 ===================================================================
14007 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14008 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.c 2007-09-21 16:24:08.000000000 +0800
14009 @@ -0,0 +1,160 @@
14010 +/*
14011 + * Copyright (c) 1992, 1993
14012 + * The Regents of the University of California. All rights reserved.
14013 + *
14014 + * Redistribution and use in source and binary forms, with or without
14015 + * modification, are permitted provided that the following conditions
14016 + * are met:
14017 + * 1. Redistributions of source code must retain the above copyright
14018 + * notice, this list of conditions and the following disclaimer.
14019 + * 2. Redistributions in binary form must reproduce the above copyright
14020 + * notice, this list of conditions and the following disclaimer in the
14021 + * documentation and/or other materials provided with the distribution.
14022 + * 3. Neither the name of the University nor the names of its contributors
14023 + * may be used to endorse or promote products derived from this software
14024 + * without specific prior written permission.
14025 + *
14026 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14027 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14028 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14029 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14030 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14031 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14032 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14033 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14034 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14035 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14036 + * SUCH DAMAGE.
14037 + */
14038 +
14039 +#include "yportenv.h"
14040 +//#include <linux/string.h>
14041 +
14042 +/*
14043 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
14044 + */
14045 +#define swapcode(TYPE, parmi, parmj, n) { \
14046 + long i = (n) / sizeof (TYPE); \
14047 + register TYPE *pi = (TYPE *) (parmi); \
14048 + register TYPE *pj = (TYPE *) (parmj); \
14049 + do { \
14050 + register TYPE t = *pi; \
14051 + *pi++ = *pj; \
14052 + *pj++ = t; \
14053 + } while (--i > 0); \
14054 +}
14055 +
14056 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
14057 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
14058 +
14059 +static __inline void
14060 +swapfunc(char *a, char *b, int n, int swaptype)
14061 +{
14062 + if (swaptype <= 1)
14063 + swapcode(long, a, b, n)
14064 + else
14065 + swapcode(char, a, b, n)
14066 +}
14067 +
14068 +#define swap(a, b) \
14069 + if (swaptype == 0) { \
14070 + long t = *(long *)(a); \
14071 + *(long *)(a) = *(long *)(b); \
14072 + *(long *)(b) = t; \
14073 + } else \
14074 + swapfunc(a, b, es, swaptype)
14075 +
14076 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14077 +
14078 +static __inline char *
14079 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14080 +{
14081 + return cmp(a, b) < 0 ?
14082 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
14083 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
14084 +}
14085 +
14086 +#ifndef min
14087 +#define min(a,b) (((a) < (b)) ? (a) : (b))
14088 +#endif
14089 +
14090 +void
14091 +yaffs_qsort(void *aa, size_t n, size_t es,
14092 + int (*cmp)(const void *, const void *))
14093 +{
14094 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14095 + int d, r, swaptype, swap_cnt;
14096 + register char *a = aa;
14097 +
14098 +loop: SWAPINIT(a, es);
14099 + swap_cnt = 0;
14100 + if (n < 7) {
14101 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14102 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14103 + pl -= es)
14104 + swap(pl, pl - es);
14105 + return;
14106 + }
14107 + pm = (char *)a + (n / 2) * es;
14108 + if (n > 7) {
14109 + pl = (char *)a;
14110 + pn = (char *)a + (n - 1) * es;
14111 + if (n > 40) {
14112 + d = (n / 8) * es;
14113 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14114 + pm = med3(pm - d, pm, pm + d, cmp);
14115 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14116 + }
14117 + pm = med3(pl, pm, pn, cmp);
14118 + }
14119 + swap(a, pm);
14120 + pa = pb = (char *)a + es;
14121 +
14122 + pc = pd = (char *)a + (n - 1) * es;
14123 + for (;;) {
14124 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14125 + if (r == 0) {
14126 + swap_cnt = 1;
14127 + swap(pa, pb);
14128 + pa += es;
14129 + }
14130 + pb += es;
14131 + }
14132 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14133 + if (r == 0) {
14134 + swap_cnt = 1;
14135 + swap(pc, pd);
14136 + pd -= es;
14137 + }
14138 + pc -= es;
14139 + }
14140 + if (pb > pc)
14141 + break;
14142 + swap(pb, pc);
14143 + swap_cnt = 1;
14144 + pb += es;
14145 + pc -= es;
14146 + }
14147 + if (swap_cnt == 0) { /* Switch to insertion sort */
14148 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14149 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14150 + pl -= es)
14151 + swap(pl, pl - es);
14152 + return;
14153 + }
14154 +
14155 + pn = (char *)a + n * es;
14156 + r = min(pa - (char *)a, pb - pa);
14157 + vecswap(a, pb - r, r);
14158 + r = min((long)(pd - pc), (long)(pn - pd - es));
14159 + vecswap(pb, pn - r, r);
14160 + if ((r = pb - pa) > es)
14161 + yaffs_qsort(a, r / es, es, cmp);
14162 + if ((r = pd - pc) > es) {
14163 + /* Iterate rather than recurse to save stack space */
14164 + a = pn - r;
14165 + n = r / es;
14166 + goto loop;
14167 + }
14168 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14169 +}
14170 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.h
14171 ===================================================================
14172 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14173 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.h 2007-09-21 16:24:08.000000000 +0800
14174 @@ -0,0 +1,23 @@
14175 +/*
14176 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14177 + *
14178 + * Copyright (C) 2002-2007 Aleph One Ltd.
14179 + * for Toby Churchill Ltd and Brightstar Engineering
14180 + *
14181 + * Created by Charles Manning <charles@aleph1.co.uk>
14182 + *
14183 + * This program is free software; you can redistribute it and/or modify
14184 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14185 + * published by the Free Software Foundation.
14186 + *
14187 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14188 + */
14189 +
14190 +
14191 +#ifndef __YAFFS_QSORT_H__
14192 +#define __YAFFS_QSORT_H__
14193 +
14194 +extern void yaffs_qsort (void *const base, size_t total_elems, size_t size,
14195 + int (*cmp)(const void *, const void *));
14196 +
14197 +#endif
14198 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.c
14199 ===================================================================
14200 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14201 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.c 2007-09-21 16:24:08.000000000 +0800
14202 @@ -0,0 +1,530 @@
14203 +/*
14204 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14205 + *
14206 + * Copyright (C) 2002-2007 Aleph One Ltd.
14207 + * for Toby Churchill Ltd and Brightstar Engineering
14208 + *
14209 + * Created by Charles Manning <charles@aleph1.co.uk>
14210 + *
14211 + * This program is free software; you can redistribute it and/or modify
14212 + * it under the terms of the GNU General Public License version 2 as
14213 + * published by the Free Software Foundation.
14214 + */
14215 +
14216 +#include "yaffs_guts.h"
14217 +#include "yaffs_tagscompat.h"
14218 +#include "yaffs_ecc.h"
14219 +
14220 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
14221 +#ifdef NOTYET
14222 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
14223 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14224 + const __u8 * data,
14225 + const yaffs_Spare * spare);
14226 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14227 + const yaffs_Spare * spare);
14228 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
14229 +#endif
14230 +
14231 +static const char yaffs_countBitsTable[256] = {
14232 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14233 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14234 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14235 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14236 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14237 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14238 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14239 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14240 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14241 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14242 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14243 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14244 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14245 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14246 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14247 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14248 +};
14249 +
14250 +int yaffs_CountBits(__u8 x)
14251 +{
14252 + int retVal;
14253 + retVal = yaffs_countBitsTable[x];
14254 + return retVal;
14255 +}
14256 +
14257 +/********** Tags ECC calculations *********/
14258 +
14259 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
14260 +{
14261 + yaffs_ECCCalculate(data, spare->ecc1);
14262 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14263 +}
14264 +
14265 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
14266 +{
14267 + /* Calculate an ecc */
14268 +
14269 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14270 + unsigned i, j;
14271 + unsigned ecc = 0;
14272 + unsigned bit = 0;
14273 +
14274 + tags->ecc = 0;
14275 +
14276 + for (i = 0; i < 8; i++) {
14277 + for (j = 1; j & 0xff; j <<= 1) {
14278 + bit++;
14279 + if (b[i] & j) {
14280 + ecc ^= bit;
14281 + }
14282 + }
14283 + }
14284 +
14285 + tags->ecc = ecc;
14286 +
14287 +}
14288 +
14289 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
14290 +{
14291 + unsigned ecc = tags->ecc;
14292 +
14293 + yaffs_CalcTagsECC(tags);
14294 +
14295 + ecc ^= tags->ecc;
14296 +
14297 + if (ecc && ecc <= 64) {
14298 + /* TODO: Handle the failure better. Retire? */
14299 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14300 +
14301 + ecc--;
14302 +
14303 + b[ecc / 8] ^= (1 << (ecc & 7));
14304 +
14305 + /* Now recvalc the ecc */
14306 + yaffs_CalcTagsECC(tags);
14307 +
14308 + return 1; /* recovered error */
14309 + } else if (ecc) {
14310 + /* Wierd ecc failure value */
14311 + /* TODO Need to do somethiong here */
14312 + return -1; /* unrecovered error */
14313 + }
14314 +
14315 + return 0;
14316 +}
14317 +
14318 +/********** Tags **********/
14319 +
14320 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
14321 + yaffs_Tags * tagsPtr)
14322 +{
14323 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14324 +
14325 + yaffs_CalcTagsECC(tagsPtr);
14326 +
14327 + sparePtr->tagByte0 = tu->asBytes[0];
14328 + sparePtr->tagByte1 = tu->asBytes[1];
14329 + sparePtr->tagByte2 = tu->asBytes[2];
14330 + sparePtr->tagByte3 = tu->asBytes[3];
14331 + sparePtr->tagByte4 = tu->asBytes[4];
14332 + sparePtr->tagByte5 = tu->asBytes[5];
14333 + sparePtr->tagByte6 = tu->asBytes[6];
14334 + sparePtr->tagByte7 = tu->asBytes[7];
14335 +}
14336 +
14337 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
14338 + yaffs_Tags * tagsPtr)
14339 +{
14340 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14341 + int result;
14342 +
14343 + tu->asBytes[0] = sparePtr->tagByte0;
14344 + tu->asBytes[1] = sparePtr->tagByte1;
14345 + tu->asBytes[2] = sparePtr->tagByte2;
14346 + tu->asBytes[3] = sparePtr->tagByte3;
14347 + tu->asBytes[4] = sparePtr->tagByte4;
14348 + tu->asBytes[5] = sparePtr->tagByte5;
14349 + tu->asBytes[6] = sparePtr->tagByte6;
14350 + tu->asBytes[7] = sparePtr->tagByte7;
14351 +
14352 + result = yaffs_CheckECCOnTags(tagsPtr);
14353 + if (result > 0) {
14354 + dev->tagsEccFixed++;
14355 + } else if (result < 0) {
14356 + dev->tagsEccUnfixed++;
14357 + }
14358 +}
14359 +
14360 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
14361 +{
14362 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14363 +}
14364 +
14365 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14366 + int chunkInNAND, const __u8 * data,
14367 + yaffs_Spare * spare)
14368 +{
14369 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14370 + T(YAFFS_TRACE_ERROR,
14371 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14372 + chunkInNAND));
14373 + return YAFFS_FAIL;
14374 + }
14375 +
14376 + dev->nPageWrites++;
14377 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14378 +}
14379 +
14380 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14381 + int chunkInNAND,
14382 + __u8 * data,
14383 + yaffs_Spare * spare,
14384 + yaffs_ECCResult * eccResult,
14385 + int doErrorCorrection)
14386 +{
14387 + int retVal;
14388 + yaffs_Spare localSpare;
14389 +
14390 + dev->nPageReads++;
14391 +
14392 + if (!spare && data) {
14393 + /* If we don't have a real spare, then we use a local one. */
14394 + /* Need this for the calculation of the ecc */
14395 + spare = &localSpare;
14396 + }
14397 +
14398 + if (!dev->useNANDECC) {
14399 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14400 + if (data && doErrorCorrection) {
14401 + /* Do ECC correction */
14402 + /* Todo handle any errors */
14403 + int eccResult1, eccResult2;
14404 + __u8 calcEcc[3];
14405 +
14406 + yaffs_ECCCalculate(data, calcEcc);
14407 + eccResult1 =
14408 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14409 + yaffs_ECCCalculate(&data[256], calcEcc);
14410 + eccResult2 =
14411 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14412 +
14413 + if (eccResult1 > 0) {
14414 + T(YAFFS_TRACE_ERROR,
14415 + (TSTR
14416 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14417 + TENDSTR), chunkInNAND));
14418 + dev->eccFixed++;
14419 + } else if (eccResult1 < 0) {
14420 + T(YAFFS_TRACE_ERROR,
14421 + (TSTR
14422 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14423 + TENDSTR), chunkInNAND));
14424 + dev->eccUnfixed++;
14425 + }
14426 +
14427 + if (eccResult2 > 0) {
14428 + T(YAFFS_TRACE_ERROR,
14429 + (TSTR
14430 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14431 + TENDSTR), chunkInNAND));
14432 + dev->eccFixed++;
14433 + } else if (eccResult2 < 0) {
14434 + T(YAFFS_TRACE_ERROR,
14435 + (TSTR
14436 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14437 + TENDSTR), chunkInNAND));
14438 + dev->eccUnfixed++;
14439 + }
14440 +
14441 + if (eccResult1 || eccResult2) {
14442 + /* We had a data problem on this page */
14443 + yaffs_HandleReadDataError(dev, chunkInNAND);
14444 + }
14445 +
14446 + if (eccResult1 < 0 || eccResult2 < 0)
14447 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14448 + else if (eccResult1 > 0 || eccResult2 > 0)
14449 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14450 + else
14451 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14452 + }
14453 + } else {
14454 + /* Must allocate enough memory for spare+2*sizeof(int) */
14455 + /* for ecc results from device. */
14456 + struct yaffs_NANDSpare nspare;
14457 + retVal =
14458 + dev->readChunkFromNAND(dev, chunkInNAND, data,
14459 + (yaffs_Spare *) & nspare);
14460 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14461 + if (data && doErrorCorrection) {
14462 + if (nspare.eccres1 > 0) {
14463 + T(YAFFS_TRACE_ERROR,
14464 + (TSTR
14465 + ("**>>mtd ecc error fix performed on chunk %d:0"
14466 + TENDSTR), chunkInNAND));
14467 + } else if (nspare.eccres1 < 0) {
14468 + T(YAFFS_TRACE_ERROR,
14469 + (TSTR
14470 + ("**>>mtd ecc error unfixed on chunk %d:0"
14471 + TENDSTR), chunkInNAND));
14472 + }
14473 +
14474 + if (nspare.eccres2 > 0) {
14475 + T(YAFFS_TRACE_ERROR,
14476 + (TSTR
14477 + ("**>>mtd ecc error fix performed on chunk %d:1"
14478 + TENDSTR), chunkInNAND));
14479 + } else if (nspare.eccres2 < 0) {
14480 + T(YAFFS_TRACE_ERROR,
14481 + (TSTR
14482 + ("**>>mtd ecc error unfixed on chunk %d:1"
14483 + TENDSTR), chunkInNAND));
14484 + }
14485 +
14486 + if (nspare.eccres1 || nspare.eccres2) {
14487 + /* We had a data problem on this page */
14488 + yaffs_HandleReadDataError(dev, chunkInNAND);
14489 + }
14490 +
14491 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14492 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14493 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14494 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14495 + else
14496 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14497 +
14498 + }
14499 + }
14500 + return retVal;
14501 +}
14502 +
14503 +#ifdef NOTYET
14504 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14505 + int chunkInNAND)
14506 +{
14507 +
14508 + static int init = 0;
14509 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14510 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14511 + /* Might as well always allocate the larger size for */
14512 + /* dev->useNANDECC == true; */
14513 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14514 +
14515 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14516 +
14517 + if (!init) {
14518 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14519 + init = 1;
14520 + }
14521 +
14522 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14523 + return YAFFS_FAIL;
14524 + if (memcmp(cmpbuf, spare, 16))
14525 + return YAFFS_FAIL;
14526 +
14527 + return YAFFS_OK;
14528 +
14529 +}
14530 +#endif
14531 +
14532 +/*
14533 + * Functions for robustisizing
14534 + */
14535 +
14536 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
14537 +{
14538 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14539 +
14540 + /* Mark the block for retirement */
14541 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14542 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14543 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14544 +
14545 + /* TODO:
14546 + * Just do a garbage collection on the affected block
14547 + * then retire the block
14548 + * NB recursion
14549 + */
14550 +}
14551 +
14552 +#ifdef NOTYET
14553 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
14554 +{
14555 +}
14556 +
14557 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14558 + const __u8 * data,
14559 + const yaffs_Spare * spare)
14560 +{
14561 +}
14562 +
14563 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14564 + const yaffs_Spare * spare)
14565 +{
14566 +}
14567 +
14568 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
14569 +{
14570 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14571 +
14572 + /* Mark the block for retirement */
14573 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14574 + /* Delete the chunk */
14575 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14576 +}
14577 +
14578 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
14579 + const yaffs_Spare * s0, const yaffs_Spare * s1)
14580 +{
14581 +
14582 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14583 + s0->tagByte0 != s1->tagByte0 ||
14584 + s0->tagByte1 != s1->tagByte1 ||
14585 + s0->tagByte2 != s1->tagByte2 ||
14586 + s0->tagByte3 != s1->tagByte3 ||
14587 + s0->tagByte4 != s1->tagByte4 ||
14588 + s0->tagByte5 != s1->tagByte5 ||
14589 + s0->tagByte6 != s1->tagByte6 ||
14590 + s0->tagByte7 != s1->tagByte7 ||
14591 + s0->ecc1[0] != s1->ecc1[0] ||
14592 + s0->ecc1[1] != s1->ecc1[1] ||
14593 + s0->ecc1[2] != s1->ecc1[2] ||
14594 + s0->ecc2[0] != s1->ecc2[0] ||
14595 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14596 + return 0;
14597 + }
14598 +
14599 + return 1;
14600 +}
14601 +#endif /* NOTYET */
14602 +
14603 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14604 + int chunkInNAND,
14605 + const __u8 * data,
14606 + const yaffs_ExtendedTags *
14607 + eTags)
14608 +{
14609 + yaffs_Spare spare;
14610 + yaffs_Tags tags;
14611 +
14612 + yaffs_SpareInitialise(&spare);
14613 +
14614 + if (eTags->chunkDeleted) {
14615 + spare.pageStatus = 0;
14616 + } else {
14617 + tags.objectId = eTags->objectId;
14618 + tags.chunkId = eTags->chunkId;
14619 + tags.byteCount = eTags->byteCount;
14620 + tags.serialNumber = eTags->serialNumber;
14621 +
14622 + if (!dev->useNANDECC && data) {
14623 + yaffs_CalcECC(data, &spare);
14624 + }
14625 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14626 +
14627 + }
14628 +
14629 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14630 +}
14631 +
14632 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14633 + int chunkInNAND,
14634 + __u8 * data,
14635 + yaffs_ExtendedTags * eTags)
14636 +{
14637 +
14638 + yaffs_Spare spare;
14639 + yaffs_Tags tags;
14640 + yaffs_ECCResult eccResult;
14641 +
14642 + static yaffs_Spare spareFF;
14643 + static int init;
14644 +
14645 + if (!init) {
14646 + memset(&spareFF, 0xFF, sizeof(spareFF));
14647 + init = 1;
14648 + }
14649 +
14650 + if (yaffs_ReadChunkFromNAND
14651 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14652 + /* eTags may be NULL */
14653 + if (eTags) {
14654 +
14655 + int deleted =
14656 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14657 +
14658 + eTags->chunkDeleted = deleted;
14659 + eTags->eccResult = eccResult;
14660 + eTags->blockBad = 0; /* We're reading it */
14661 + /* therefore it is not a bad block */
14662 + eTags->chunkUsed =
14663 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14664 + 0) ? 1 : 0;
14665 +
14666 + if (eTags->chunkUsed) {
14667 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14668 +
14669 + eTags->objectId = tags.objectId;
14670 + eTags->chunkId = tags.chunkId;
14671 + eTags->byteCount = tags.byteCount;
14672 + eTags->serialNumber = tags.serialNumber;
14673 + }
14674 + }
14675 +
14676 + return YAFFS_OK;
14677 + } else {
14678 + return YAFFS_FAIL;
14679 + }
14680 +}
14681 +
14682 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14683 + int blockInNAND)
14684 +{
14685 +
14686 + yaffs_Spare spare;
14687 +
14688 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14689 +
14690 + spare.blockStatus = 'Y';
14691 +
14692 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14693 + &spare);
14694 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14695 + NULL, &spare);
14696 +
14697 + return YAFFS_OK;
14698 +
14699 +}
14700 +
14701 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14702 + int blockNo, yaffs_BlockState *
14703 + state,
14704 + int *sequenceNumber)
14705 +{
14706 +
14707 + yaffs_Spare spare0, spare1;
14708 + static yaffs_Spare spareFF;
14709 + static int init;
14710 + yaffs_ECCResult dummy;
14711 +
14712 + if (!init) {
14713 + memset(&spareFF, 0xFF, sizeof(spareFF));
14714 + init = 1;
14715 + }
14716 +
14717 + *sequenceNumber = 0;
14718 +
14719 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14720 + &spare0, &dummy, 1);
14721 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14722 + &spare1, &dummy, 1);
14723 +
14724 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14725 + *state = YAFFS_BLOCK_STATE_DEAD;
14726 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14727 + *state = YAFFS_BLOCK_STATE_EMPTY;
14728 + else
14729 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14730 +
14731 + return YAFFS_OK;
14732 +}
14733 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.h
14734 ===================================================================
14735 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14736 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.h 2007-09-21 16:24:08.000000000 +0800
14737 @@ -0,0 +1,40 @@
14738 +/*
14739 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14740 + *
14741 + * Copyright (C) 2002-2007 Aleph One Ltd.
14742 + * for Toby Churchill Ltd and Brightstar Engineering
14743 + *
14744 + * Created by Charles Manning <charles@aleph1.co.uk>
14745 + *
14746 + * This program is free software; you can redistribute it and/or modify
14747 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14748 + * published by the Free Software Foundation.
14749 + *
14750 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14751 + */
14752 +
14753 +#ifndef __YAFFS_TAGSCOMPAT_H__
14754 +#define __YAFFS_TAGSCOMPAT_H__
14755 +
14756 +#include "yaffs_guts.h"
14757 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14758 + int chunkInNAND,
14759 + const __u8 * data,
14760 + const yaffs_ExtendedTags *
14761 + tags);
14762 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14763 + int chunkInNAND,
14764 + __u8 * data,
14765 + yaffs_ExtendedTags *
14766 + tags);
14767 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14768 + int blockNo);
14769 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14770 + int blockNo, yaffs_BlockState *
14771 + state, int *sequenceNumber);
14772 +
14773 +void yaffs_CalcTagsECC(yaffs_Tags * tags);
14774 +int yaffs_CheckECCOnTags(yaffs_Tags * tags);
14775 +int yaffs_CountBits(__u8 byte);
14776 +
14777 +#endif
14778 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.c
14779 ===================================================================
14780 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14781 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.c 2007-09-21 16:24:08.000000000 +0800
14782 @@ -0,0 +1,28 @@
14783 +/*
14784 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14785 + *
14786 + * Copyright (C) 2002-2007 Aleph One Ltd.
14787 + * for Toby Churchill Ltd and Brightstar Engineering
14788 + *
14789 + * Created by Charles Manning <charles@aleph1.co.uk>
14790 + *
14791 + * This program is free software; you can redistribute it and/or modify
14792 + * it under the terms of the GNU General Public License version 2 as
14793 + * published by the Free Software Foundation.
14794 + */
14795 +
14796 +#include "yaffs_tagsvalidity.h"
14797 +
14798 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
14799 +{
14800 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14801 + tags->validMarker0 = 0xAAAAAAAA;
14802 + tags->validMarker1 = 0x55555555;
14803 +}
14804 +
14805 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
14806 +{
14807 + return (tags->validMarker0 == 0xAAAAAAAA &&
14808 + tags->validMarker1 == 0x55555555);
14809 +
14810 +}
14811 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.h
14812 ===================================================================
14813 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14814 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.h 2007-09-21 16:24:08.000000000 +0800
14815 @@ -0,0 +1,24 @@
14816 +/*
14817 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14818 + *
14819 + * Copyright (C) 2002-2007 Aleph One Ltd.
14820 + * for Toby Churchill Ltd and Brightstar Engineering
14821 + *
14822 + * Created by Charles Manning <charles@aleph1.co.uk>
14823 + *
14824 + * This program is free software; you can redistribute it and/or modify
14825 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14826 + * published by the Free Software Foundation.
14827 + *
14828 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14829 + */
14830 +
14831 +
14832 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14833 +#define __YAFFS_TAGS_VALIDITY_H__
14834 +
14835 +#include "yaffs_guts.h"
14836 +
14837 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
14838 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
14839 +#endif
14840 Index: linux-2.6.23-rc6/fs/yaffs2/yaffsinterface.h
14841 ===================================================================
14842 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14843 +++ linux-2.6.23-rc6/fs/yaffs2/yaffsinterface.h 2007-09-21 16:24:08.000000000 +0800
14844 @@ -0,0 +1,21 @@
14845 +/*
14846 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14847 + *
14848 + * Copyright (C) 2002-2007 Aleph One Ltd.
14849 + * for Toby Churchill Ltd and Brightstar Engineering
14850 + *
14851 + * Created by Charles Manning <charles@aleph1.co.uk>
14852 + *
14853 + * This program is free software; you can redistribute it and/or modify
14854 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14855 + * published by the Free Software Foundation.
14856 + *
14857 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14858 + */
14859 +
14860 +#ifndef __YAFFSINTERFACE_H__
14861 +#define __YAFFSINTERFACE_H__
14862 +
14863 +int yaffs_Initialise(unsigned nBlocks);
14864 +
14865 +#endif
14866 Index: linux-2.6.23-rc6/fs/yaffs2/yportenv.h
14867 ===================================================================
14868 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14869 +++ linux-2.6.23-rc6/fs/yaffs2/yportenv.h 2007-09-21 16:24:08.000000000 +0800
14870 @@ -0,0 +1,186 @@
14871 +/*
14872 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14873 + *
14874 + * Copyright (C) 2002-2007 Aleph One Ltd.
14875 + * for Toby Churchill Ltd and Brightstar Engineering
14876 + *
14877 + * Created by Charles Manning <charles@aleph1.co.uk>
14878 + *
14879 + * This program is free software; you can redistribute it and/or modify
14880 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14881 + * published by the Free Software Foundation.
14882 + *
14883 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14884 + */
14885 +
14886 +
14887 +#ifndef __YPORTENV_H__
14888 +#define __YPORTENV_H__
14889 +
14890 +#if defined CONFIG_YAFFS_WINCE
14891 +
14892 +#include "ywinceenv.h"
14893 +
14894 +#elif defined __KERNEL__
14895 +
14896 +#include "moduleconfig.h"
14897 +
14898 +/* Linux kernel */
14899 +#include <linux/version.h>
14900 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
14901 +#include <linux/config.h>
14902 +#endif
14903 +#include <linux/kernel.h>
14904 +#include <linux/mm.h>
14905 +#include <linux/string.h>
14906 +#include <linux/slab.h>
14907 +#include <linux/vmalloc.h>
14908 +
14909 +#define YCHAR char
14910 +#define YUCHAR unsigned char
14911 +#define _Y(x) x
14912 +#define yaffs_strcpy(a,b) strcpy(a,b)
14913 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14914 +#define yaffs_strncmp(a,b,c) strncmp(a,b,c)
14915 +#define yaffs_strlen(s) strlen(s)
14916 +#define yaffs_sprintf sprintf
14917 +#define yaffs_toupper(a) toupper(a)
14918 +
14919 +#define Y_INLINE inline
14920 +
14921 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14922 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14923 +
14924 +/* #define YPRINTF(x) printk x */
14925 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
14926 +#define YFREE(x) kfree(x)
14927 +#define YMALLOC_ALT(x) vmalloc(x)
14928 +#define YFREE_ALT(x) vfree(x)
14929 +#define YMALLOC_DMA(x) YMALLOC(x)
14930 +
14931 +// KR - added for use in scan so processes aren't blocked indefinitely.
14932 +#define YYIELD() schedule()
14933 +
14934 +#define YAFFS_ROOT_MODE 0666
14935 +#define YAFFS_LOSTNFOUND_MODE 0666
14936 +
14937 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
14938 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14939 +#define Y_TIME_CONVERT(x) (x).tv_sec
14940 +#else
14941 +#define Y_CURRENT_TIME CURRENT_TIME
14942 +#define Y_TIME_CONVERT(x) (x)
14943 +#endif
14944 +
14945 +#define yaffs_SumCompare(x,y) ((x) == (y))
14946 +#define yaffs_strcmp(a,b) strcmp(a,b)
14947 +
14948 +#define TENDSTR "\n"
14949 +#define TSTR(x) KERN_WARNING x
14950 +#define TOUT(p) printk p
14951 +
14952 +#define yaffs_trace(mask, fmt, args...) \
14953 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14954 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14955 + } while (0)
14956 +
14957 +#define compile_time_assertion(assertion) \
14958 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14959 +
14960 +#elif defined CONFIG_YAFFS_DIRECT
14961 +
14962 +/* Direct interface */
14963 +#include "ydirectenv.h"
14964 +
14965 +#elif defined CONFIG_YAFFS_UTIL
14966 +
14967 +/* Stuff for YAFFS utilities */
14968 +
14969 +#include "stdlib.h"
14970 +#include "stdio.h"
14971 +#include "string.h"
14972 +
14973 +#include "devextras.h"
14974 +
14975 +#define YMALLOC(x) malloc(x)
14976 +#define YFREE(x) free(x)
14977 +#define YMALLOC_ALT(x) malloc(x)
14978 +#define YFREE_ALT(x) free(x)
14979 +
14980 +#define YCHAR char
14981 +#define YUCHAR unsigned char
14982 +#define _Y(x) x
14983 +#define yaffs_strcpy(a,b) strcpy(a,b)
14984 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14985 +#define yaffs_strlen(s) strlen(s)
14986 +#define yaffs_sprintf sprintf
14987 +#define yaffs_toupper(a) toupper(a)
14988 +
14989 +#define Y_INLINE inline
14990 +
14991 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
14992 +/* #define YALERT(s) YINFO(s) */
14993 +
14994 +#define TENDSTR "\n"
14995 +#define TSTR(x) x
14996 +#define TOUT(p) printf p
14997 +
14998 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14999 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
15000 +/* #define YPRINTF(x) printf x */
15001 +
15002 +#define YAFFS_ROOT_MODE 0666
15003 +#define YAFFS_LOSTNFOUND_MODE 0666
15004 +
15005 +#define yaffs_SumCompare(x,y) ((x) == (y))
15006 +#define yaffs_strcmp(a,b) strcmp(a,b)
15007 +
15008 +#else
15009 +/* Should have specified a configuration type */
15010 +#error Unknown configuration
15011 +
15012 +#endif
15013 +
15014 +/* see yaffs_fs.c */
15015 +extern unsigned int yaffs_traceMask;
15016 +extern unsigned int yaffs_wr_attempts;
15017 +
15018 +/*
15019 + * Tracing flags.
15020 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
15021 + */
15022 +
15023 +#define YAFFS_TRACE_OS 0x00000002
15024 +#define YAFFS_TRACE_ALLOCATE 0x00000004
15025 +#define YAFFS_TRACE_SCAN 0x00000008
15026 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
15027 +#define YAFFS_TRACE_ERASE 0x00000020
15028 +#define YAFFS_TRACE_GC 0x00000040
15029 +#define YAFFS_TRACE_WRITE 0x00000080
15030 +#define YAFFS_TRACE_TRACING 0x00000100
15031 +#define YAFFS_TRACE_DELETION 0x00000200
15032 +#define YAFFS_TRACE_BUFFERS 0x00000400
15033 +#define YAFFS_TRACE_NANDACCESS 0x00000800
15034 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
15035 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
15036 +#define YAFFS_TRACE_MTD 0x00004000
15037 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
15038 +
15039 +#define YAFFS_TRACE_VERIFY 0x00010000
15040 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
15041 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
15042 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
15043 +
15044 +
15045 +#define YAFFS_TRACE_ERROR 0x40000000
15046 +#define YAFFS_TRACE_BUG 0x80000000
15047 +#define YAFFS_TRACE_ALWAYS 0xF0000000
15048 +
15049 +
15050 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p);} while(0)
15051 +
15052 +#ifndef CONFIG_YAFFS_WINCE
15053 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))
15054 +#endif
15055 +
15056 +#endif