fix ar7 image installation (patch by sw, fixes #2197)
[openwrt.git] / target / linux / generic-2.6 / patches-2.6.22 / 510-Yaffs.patch
1 diff -urN linux-2.6.21.1/fs/Kconfig linux-2.6.21.1.new/fs/Kconfig
2 --- linux-2.6.21.1/fs/Kconfig 2007-06-08 14:07:09.000000000 +0200
3 +++ linux-2.6.21.1.new/fs/Kconfig 2007-06-08 14:09:26.000000000 +0200
4 @@ -419,6 +419,7 @@
5
6 source "fs/xfs/Kconfig"
7 source "fs/gfs2/Kconfig"
8 +source "fs/yaffs2/Kconfig"
9
10 config OCFS2_FS
11 tristate "OCFS2 file system support"
12 diff -urN linux-2.6.21.1/fs/Makefile linux-2.6.21.1.new/fs/Makefile
13 --- linux-2.6.21.1/fs/Makefile 2007-06-08 14:07:09.000000000 +0200
14 +++ linux-2.6.21.1.new/fs/Makefile 2007-06-08 14:09:51.000000000 +0200
15 @@ -116,3 +116,4 @@
16 obj-$(CONFIG_DEBUG_FS) += debugfs/
17 obj-$(CONFIG_OCFS2_FS) += ocfs2/
18 obj-$(CONFIG_GFS2_FS) += gfs2/
19 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
20 diff -urN linux-2.6.21.1/fs/yaffs2/Kconfig linux-2.6.21.1.new/fs/yaffs2/Kconfig
21 --- linux-2.6.21.1/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
22 +++ linux-2.6.21.1.new/fs/yaffs2/Kconfig 2007-06-08 14:07:26.000000000 +0200
23 @@ -0,0 +1,175 @@
24 +#
25 +# YAFFS file system configurations
26 +#
27 +
28 +config YAFFS_FS
29 + tristate "YAFFS2 file system support"
30 + default n
31 + depends on MTD
32 + select YAFFS_YAFFS1
33 + select YAFFS_YAFFS2
34 + help
35 + YAFFS2, or Yet Another Flash Filing System, is a filing system
36 + optimised for NAND Flash chips.
37 +
38 + To compile the YAFFS2 file system support as a module, choose M
39 + here: the module will be called yaffs2.
40 +
41 + If unsure, say N.
42 +
43 + Further information on YAFFS2 is available at
44 + <http://www.aleph1.co.uk/yaffs/>.
45 +
46 +config YAFFS_YAFFS1
47 + bool "512 byte / page devices"
48 + depends on YAFFS_FS
49 + default y
50 + help
51 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
52 +
53 + Not needed for 2K-page devices.
54 +
55 + If unsure, say Y.
56 +
57 +config YAFFS_9BYTE_TAGS
58 + bool "Use older-style on-NAND data format with pageStatus byte"
59 + depends on YAFFS_YAFFS1
60 + default n
61 + help
62 +
63 + Older-style on-NAND data format has a "pageStatus" byte to record
64 + chunk/page state. This byte is zero when the page is discarded.
65 + Choose this option if you have existing on-NAND data using this
66 + format that you need to continue to support. New data written
67 + also uses the older-style format. Note: Use of this option
68 + generally requires that MTD's oob layout be adjusted to use the
69 + older-style format. See notes on tags formats and MTD versions.
70 +
71 + If unsure, say N.
72 +
73 +config YAFFS_DOES_ECC
74 + bool "Lets Yaffs do its own ECC"
75 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
76 + default n
77 + help
78 + This enables Yaffs to use its own ECC functions instead of using
79 + the ones from the generic MTD-NAND driver.
80 +
81 + If unsure, say N.
82 +
83 +config YAFFS_ECC_WRONG_ORDER
84 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
85 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
86 + default n
87 + help
88 + This makes yaffs_ecc.c use the same ecc byte order as Steven
89 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
90 + order as SmartMedia.
91 +
92 + If unsure, say N.
93 +
94 +config YAFFS_YAFFS2
95 + bool "2048 byte (or larger) / page devices"
96 + depends on YAFFS_FS
97 + default y
98 + help
99 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
100 +
101 + If unsure, say Y.
102 +
103 +config YAFFS_AUTO_YAFFS2
104 + bool "Autoselect yaffs2 format"
105 + depends on YAFFS_YAFFS2
106 + default y
107 + help
108 + Without this, you need to explicitely use yaffs2 as the file
109 + system type. With this, you can say "yaffs" and yaffs or yaffs2
110 + will be used depending on the device page size (yaffs on
111 + 512-byte page devices, yaffs2 on 2K page devices).
112 +
113 + If unsure, say Y.
114 +
115 +config YAFFS_DISABLE_LAZY_LOAD
116 + bool "Disable lazy loading"
117 + depends on YAFFS_YAFFS2
118 + default n
119 + help
120 + "Lazy loading" defers loading file details until they are
121 + required. This saves mount time, but makes the first look-up
122 + a bit longer.
123 +
124 + Lazy loading will only happen if enabled by this option being 'n'
125 + and if the appropriate tags are available, else yaffs2 will
126 + automatically fall back to immediate loading and do the right
127 + thing.
128 +
129 + Lazy laoding will be required by checkpointing.
130 +
131 + Setting this to 'y' will disable lazy loading.
132 +
133 + If unsure, say N.
134 +
135 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
136 + int "Reserved blocks for checkpointing"
137 + depends on YAFFS_YAFFS2
138 + default 10
139 + help
140 + Give the number of Blocks to reserve for checkpointing.
141 + Checkpointing saves the state at unmount so that mounting is
142 + much faster as a scan of all the flash to regenerate this state
143 + is not needed. These Blocks are reserved per partition, so if
144 + you have very small partitions the default (10) may be a mess
145 + for you. You can set this value to 0, but that does not mean
146 + checkpointing is disabled at all. There only won't be any
147 + specially reserved blocks for checkpointing, so if there is
148 + enough free space on the filesystem, it will be used for
149 + checkpointing.
150 +
151 + If unsure, leave at default (10), but don't wonder if there are
152 + always 2MB used on your large page device partition (10 x 2k
153 + pagesize). When using small partitions or when being very small
154 + on space, you probably want to set this to zero.
155 +
156 +config YAFFS_DISABLE_WIDE_TNODES
157 + bool "Turn off wide tnodes"
158 + depends on YAFFS_FS
159 + default n
160 + help
161 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
162 + page devices and >=128MB for 2k page devices. They use slightly
163 + more RAM but are faster since they eliminate chunk group
164 + searching.
165 +
166 + Setting this to 'y' will force tnode width to 16 bits and save
167 + memory but make large arrays slower.
168 +
169 + If unsure, say N.
170 +
171 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
172 + bool "Force chunk erase check"
173 + depends on YAFFS_FS
174 + default n
175 + help
176 + Normally YAFFS only checks chunks before writing until an erased
177 + chunk is found. This helps to detect any partially written
178 + chunks that might have happened due to power loss.
179 +
180 + Enabling this forces on the test that chunks are erased in flash
181 + before writing to them. This takes more time but is potentially
182 + a bit more secure.
183 +
184 + Suggest setting Y during development and ironing out driver
185 + issues etc. Suggest setting to N if you want faster writing.
186 +
187 + If unsure, say Y.
188 +
189 +config YAFFS_SHORT_NAMES_IN_RAM
190 + bool "Cache short names in RAM"
191 + depends on YAFFS_FS
192 + default y
193 + help
194 + If this config is set, then short names are stored with the
195 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
196 + but makes look-ups faster.
197 +
198 + If unsure, say Y.
199 diff -urN linux-2.6.21.1/fs/yaffs2/Makefile linux-2.6.21.1.new/fs/yaffs2/Makefile
200 --- linux-2.6.21.1/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
201 +++ linux-2.6.21.1.new/fs/yaffs2/Makefile 2007-06-08 14:07:26.000000000 +0200
202 @@ -0,0 +1,11 @@
203 +#
204 +# Makefile for the linux YAFFS filesystem routines.
205 +#
206 +
207 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
208 +
209 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
210 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
211 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
212 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
213 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
214 diff -urN linux-2.6.21.1/fs/yaffs2/devextras.h linux-2.6.21.1.new/fs/yaffs2/devextras.h
215 --- linux-2.6.21.1/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
216 +++ linux-2.6.21.1.new/fs/yaffs2/devextras.h 2007-06-08 14:07:26.000000000 +0200
217 @@ -0,0 +1,264 @@
218 +/*
219 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
220 + *
221 + * Copyright (C) 2002-2007 Aleph One Ltd.
222 + * for Toby Churchill Ltd and Brightstar Engineering
223 + *
224 + * Created by Charles Manning <charles@aleph1.co.uk>
225 + *
226 + * This program is free software; you can redistribute it and/or modify
227 + * it under the terms of the GNU Lesser General Public License version 2.1 as
228 + * published by the Free Software Foundation.
229 + *
230 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
231 + */
232 +
233 +/*
234 + * This file is just holds extra declarations used during development.
235 + * Most of these are from kernel includes placed here so we can use them in
236 + * applications.
237 + *
238 + */
239 +
240 +#ifndef __EXTRAS_H__
241 +#define __EXTRAS_H__
242 +
243 +#if defined WIN32
244 +#define __inline__ __inline
245 +#define new newHack
246 +#endif
247 +
248 +#if !(defined __KERNEL__) || (defined WIN32)
249 +
250 +/* User space defines */
251 +
252 +typedef unsigned char __u8;
253 +typedef unsigned short __u16;
254 +typedef unsigned __u32;
255 +
256 +/*
257 + * Simple doubly linked list implementation.
258 + *
259 + * Some of the internal functions ("__xxx") are useful when
260 + * manipulating whole lists rather than single entries, as
261 + * sometimes we already know the next/prev entries and we can
262 + * generate better code by using them directly rather than
263 + * using the generic single-entry routines.
264 + */
265 +
266 +#define prefetch(x) 1
267 +
268 +struct list_head {
269 + struct list_head *next, *prev;
270 +};
271 +
272 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
273 +
274 +#define LIST_HEAD(name) \
275 + struct list_head name = LIST_HEAD_INIT(name)
276 +
277 +#define INIT_LIST_HEAD(ptr) do { \
278 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
279 +} while (0)
280 +
281 +/*
282 + * Insert a new entry between two known consecutive entries.
283 + *
284 + * This is only for internal list manipulation where we know
285 + * the prev/next entries already!
286 + */
287 +static __inline__ void __list_add(struct list_head *new,
288 + struct list_head *prev,
289 + struct list_head *next)
290 +{
291 + next->prev = new;
292 + new->next = next;
293 + new->prev = prev;
294 + prev->next = new;
295 +}
296 +
297 +/**
298 + * list_add - add a new entry
299 + * @new: new entry to be added
300 + * @head: list head to add it after
301 + *
302 + * Insert a new entry after the specified head.
303 + * This is good for implementing stacks.
304 + */
305 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
306 +{
307 + __list_add(new, head, head->next);
308 +}
309 +
310 +/**
311 + * list_add_tail - add a new entry
312 + * @new: new entry to be added
313 + * @head: list head to add it before
314 + *
315 + * Insert a new entry before the specified head.
316 + * This is useful for implementing queues.
317 + */
318 +static __inline__ void list_add_tail(struct list_head *new,
319 + struct list_head *head)
320 +{
321 + __list_add(new, head->prev, head);
322 +}
323 +
324 +/*
325 + * Delete a list entry by making the prev/next entries
326 + * point to each other.
327 + *
328 + * This is only for internal list manipulation where we know
329 + * the prev/next entries already!
330 + */
331 +static __inline__ void __list_del(struct list_head *prev,
332 + struct list_head *next)
333 +{
334 + next->prev = prev;
335 + prev->next = next;
336 +}
337 +
338 +/**
339 + * list_del - deletes entry from list.
340 + * @entry: the element to delete from the list.
341 + * Note: list_empty on entry does not return true after this, the entry is
342 + * in an undefined state.
343 + */
344 +static __inline__ void list_del(struct list_head *entry)
345 +{
346 + __list_del(entry->prev, entry->next);
347 +}
348 +
349 +/**
350 + * list_del_init - deletes entry from list and reinitialize it.
351 + * @entry: the element to delete from the list.
352 + */
353 +static __inline__ void list_del_init(struct list_head *entry)
354 +{
355 + __list_del(entry->prev, entry->next);
356 + INIT_LIST_HEAD(entry);
357 +}
358 +
359 +/**
360 + * list_empty - tests whether a list is empty
361 + * @head: the list to test.
362 + */
363 +static __inline__ int list_empty(struct list_head *head)
364 +{
365 + return head->next == head;
366 +}
367 +
368 +/**
369 + * list_splice - join two lists
370 + * @list: the new list to add.
371 + * @head: the place to add it in the first list.
372 + */
373 +static __inline__ void list_splice(struct list_head *list,
374 + struct list_head *head)
375 +{
376 + struct list_head *first = list->next;
377 +
378 + if (first != list) {
379 + struct list_head *last = list->prev;
380 + struct list_head *at = head->next;
381 +
382 + first->prev = head;
383 + head->next = first;
384 +
385 + last->next = at;
386 + at->prev = last;
387 + }
388 +}
389 +
390 +/**
391 + * list_entry - get the struct for this entry
392 + * @ptr: the &struct list_head pointer.
393 + * @type: the type of the struct this is embedded in.
394 + * @member: the name of the list_struct within the struct.
395 + */
396 +#define list_entry(ptr, type, member) \
397 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
398 +
399 +/**
400 + * list_for_each - iterate over a list
401 + * @pos: the &struct list_head to use as a loop counter.
402 + * @head: the head for your list.
403 + */
404 +#define list_for_each(pos, head) \
405 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
406 + pos = pos->next, prefetch(pos->next))
407 +
408 +/**
409 + * list_for_each_safe - iterate over a list safe against removal
410 + * of list entry
411 + * @pos: the &struct list_head to use as a loop counter.
412 + * @n: another &struct list_head to use as temporary storage
413 + * @head: the head for your list.
414 + */
415 +#define list_for_each_safe(pos, n, head) \
416 + for (pos = (head)->next, n = pos->next; pos != (head); \
417 + pos = n, n = pos->next)
418 +
419 +/*
420 + * File types
421 + */
422 +#define DT_UNKNOWN 0
423 +#define DT_FIFO 1
424 +#define DT_CHR 2
425 +#define DT_DIR 4
426 +#define DT_BLK 6
427 +#define DT_REG 8
428 +#define DT_LNK 10
429 +#define DT_SOCK 12
430 +#define DT_WHT 14
431 +
432 +#ifndef WIN32
433 +#include <sys/stat.h>
434 +#endif
435 +
436 +/*
437 + * Attribute flags. These should be or-ed together to figure out what
438 + * has been changed!
439 + */
440 +#define ATTR_MODE 1
441 +#define ATTR_UID 2
442 +#define ATTR_GID 4
443 +#define ATTR_SIZE 8
444 +#define ATTR_ATIME 16
445 +#define ATTR_MTIME 32
446 +#define ATTR_CTIME 64
447 +#define ATTR_ATIME_SET 128
448 +#define ATTR_MTIME_SET 256
449 +#define ATTR_FORCE 512 /* Not a change, but a change it */
450 +#define ATTR_ATTR_FLAG 1024
451 +
452 +struct iattr {
453 + unsigned int ia_valid;
454 + unsigned ia_mode;
455 + unsigned ia_uid;
456 + unsigned ia_gid;
457 + unsigned ia_size;
458 + unsigned ia_atime;
459 + unsigned ia_mtime;
460 + unsigned ia_ctime;
461 + unsigned int ia_attr_flags;
462 +};
463 +
464 +#define KERN_DEBUG
465 +
466 +#else
467 +
468 +#ifndef WIN32
469 +#include <linux/types.h>
470 +#include <linux/list.h>
471 +#include <linux/fs.h>
472 +#include <linux/stat.h>
473 +#endif
474 +
475 +#endif
476 +
477 +#if defined WIN32
478 +#undef new
479 +#endif
480 +
481 +#endif
482 diff -urN linux-2.6.21.1/fs/yaffs2/moduleconfig.h linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h
483 --- linux-2.6.21.1/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
484 +++ linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h 2007-06-08 14:07:26.000000000 +0200
485 @@ -0,0 +1,65 @@
486 +/*
487 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
488 + *
489 + * Copyright (C) 2002-2007 Aleph One Ltd.
490 + * for Toby Churchill Ltd and Brightstar Engineering
491 + *
492 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
493 + *
494 + * This program is free software; you can redistribute it and/or modify
495 + * it under the terms of the GNU Lesser General Public License version 2.1 as
496 + * published by the Free Software Foundation.
497 + *
498 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
499 + */
500 +
501 +#ifndef __YAFFS_CONFIG_H__
502 +#define __YAFFS_CONFIG_H__
503 +
504 +#ifdef YAFFS_OUT_OF_TREE
505 +
506 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
507 +#define CONFIG_YAFFS_FS
508 +#define CONFIG_YAFFS_YAFFS1
509 +#define CONFIG_YAFFS_YAFFS2
510 +
511 +/* These options are independent of each other. Select those that matter. */
512 +
513 +/* Default: Not selected */
514 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
515 +//#define CONFIG_YAFFS_DOES_ECC
516 +
517 +/* Default: Not selected */
518 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
519 +/* CONFIG_YAFFS_DOES_ECC is set */
520 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
521 +
522 +/* Default: Selected */
523 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
524 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
525 +
526 +/* Default: Selected */
527 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
528 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
529 +
530 +/* Default: 10 */
531 +/* Meaning: set the count of blocks to reserve for checkpointing */
532 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
533 +
534 +/*
535 +Older-style on-NAND data format has a "pageStatus" byte to record
536 +chunk/page state. This byte is zeroed when the page is discarded.
537 +Choose this option if you have existing on-NAND data in this format
538 +that you need to continue to support. New data written also uses the
539 +older-style format.
540 +Note: Use of this option generally requires that MTD's oob layout be
541 +adjusted to use the older-style format. See notes on tags formats and
542 +MTD versions.
543 +*/
544 +/* Default: Not selected */
545 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
546 +#define CONFIG_YAFFS_9BYTE_TAGS
547 +
548 +#endif /* YAFFS_OUT_OF_TREE */
549 +
550 +#endif /* __YAFFS_CONFIG_H__ */
551 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c
552 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
553 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c 2007-06-08 14:07:26.000000000 +0200
554 @@ -0,0 +1,404 @@
555 +/*
556 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
557 + *
558 + * Copyright (C) 2002-2007 Aleph One Ltd.
559 + * for Toby Churchill Ltd and Brightstar Engineering
560 + *
561 + * Created by Charles Manning <charles@aleph1.co.uk>
562 + *
563 + * This program is free software; you can redistribute it and/or modify
564 + * it under the terms of the GNU General Public License version 2 as
565 + * published by the Free Software Foundation.
566 + */
567 +
568 +const char *yaffs_checkptrw_c_version =
569 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
570 +
571 +
572 +#include "yaffs_checkptrw.h"
573 +
574 +
575 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
576 +{
577 +
578 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
579 +
580 + T(YAFFS_TRACE_CHECKPOINT,
581 + (TSTR("checkpt blocks available = %d" TENDSTR),
582 + blocksAvailable));
583 +
584 +
585 + return (blocksAvailable <= 0) ? 0 : 1;
586 +}
587 +
588 +
589 +static int yaffs_CheckpointErase(yaffs_Device *dev)
590 +{
591 +
592 + int i;
593 +
594 +
595 + if(!dev->eraseBlockInNAND)
596 + return 0;
597 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
598 + dev->internalStartBlock,dev->internalEndBlock));
599 +
600 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
601 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
602 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
603 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
604 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
605 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
606 + dev->nErasedBlocks++;
607 + dev->nFreeChunks += dev->nChunksPerBlock;
608 + }
609 + else {
610 + dev->markNANDBlockBad(dev,i);
611 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
612 + }
613 + }
614 + }
615 +
616 + dev->blocksInCheckpoint = 0;
617 +
618 + return 1;
619 +}
620 +
621 +
622 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
623 +{
624 + int i;
625 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
626 + T(YAFFS_TRACE_CHECKPOINT,
627 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
628 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
629 +
630 + if(dev->checkpointNextBlock >= 0 &&
631 + dev->checkpointNextBlock <= dev->internalEndBlock &&
632 + blocksAvailable > 0){
633 +
634 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
635 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
636 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
637 + dev->checkpointNextBlock = i + 1;
638 + dev->checkpointCurrentBlock = i;
639 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
640 + return;
641 + }
642 + }
643 + }
644 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
645 +
646 + dev->checkpointNextBlock = -1;
647 + dev->checkpointCurrentBlock = -1;
648 +}
649 +
650 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
651 +{
652 + int i;
653 + yaffs_ExtendedTags tags;
654 +
655 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
656 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
657 +
658 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
659 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
660 + int chunk = i * dev->nChunksPerBlock;
661 + int realignedChunk = chunk - dev->chunkOffset;
662 +
663 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
664 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
665 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
666 +
667 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
668 + /* Right kind of block */
669 + dev->checkpointNextBlock = tags.objectId;
670 + dev->checkpointCurrentBlock = i;
671 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
672 + dev->blocksInCheckpoint++;
673 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
674 + return;
675 + }
676 + }
677 +
678 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
679 +
680 + dev->checkpointNextBlock = -1;
681 + dev->checkpointCurrentBlock = -1;
682 +}
683 +
684 +
685 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
686 +{
687 +
688 + /* Got the functions we need? */
689 + if (!dev->writeChunkWithTagsToNAND ||
690 + !dev->readChunkWithTagsFromNAND ||
691 + !dev->eraseBlockInNAND ||
692 + !dev->markNANDBlockBad)
693 + return 0;
694 +
695 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
696 + return 0;
697 +
698 + if(!dev->checkpointBuffer)
699 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
700 + if(!dev->checkpointBuffer)
701 + return 0;
702 +
703 +
704 + dev->checkpointPageSequence = 0;
705 +
706 + dev->checkpointOpenForWrite = forWriting;
707 +
708 + dev->checkpointByteCount = 0;
709 + dev->checkpointSum = 0;
710 + dev->checkpointXor = 0;
711 + dev->checkpointCurrentBlock = -1;
712 + dev->checkpointCurrentChunk = -1;
713 + dev->checkpointNextBlock = dev->internalStartBlock;
714 +
715 + /* Erase all the blocks in the checkpoint area */
716 + if(forWriting){
717 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
718 + dev->checkpointByteOffset = 0;
719 + return yaffs_CheckpointErase(dev);
720 +
721 +
722 + } else {
723 + int i;
724 + /* Set to a value that will kick off a read */
725 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
726 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
727 + * going to be way more than we need */
728 + dev->blocksInCheckpoint = 0;
729 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
730 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
731 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
732 + dev->checkpointBlockList[i] = -1;
733 + }
734 +
735 + return 1;
736 +}
737 +
738 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
739 +{
740 + __u32 compositeSum;
741 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
742 + *sum = compositeSum;
743 + return 1;
744 +}
745 +
746 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
747 +{
748 +
749 + int chunk;
750 + int realignedChunk;
751 +
752 + yaffs_ExtendedTags tags;
753 +
754 + if(dev->checkpointCurrentBlock < 0){
755 + yaffs_CheckpointFindNextErasedBlock(dev);
756 + dev->checkpointCurrentChunk = 0;
757 + }
758 +
759 + if(dev->checkpointCurrentBlock < 0)
760 + return 0;
761 +
762 + tags.chunkDeleted = 0;
763 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
764 + tags.chunkId = dev->checkpointPageSequence + 1;
765 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
766 + tags.byteCount = dev->nDataBytesPerChunk;
767 + if(dev->checkpointCurrentChunk == 0){
768 + /* First chunk we write for the block? Set block state to
769 + checkpoint */
770 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
771 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
772 + dev->blocksInCheckpoint++;
773 + }
774 +
775 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
776 +
777 +
778 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
779 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
780 +
781 + realignedChunk = chunk - dev->chunkOffset;
782 +
783 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
784 + dev->checkpointByteOffset = 0;
785 + dev->checkpointPageSequence++;
786 + dev->checkpointCurrentChunk++;
787 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
788 + dev->checkpointCurrentChunk = 0;
789 + dev->checkpointCurrentBlock = -1;
790 + }
791 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
792 +
793 + return 1;
794 +}
795 +
796 +
797 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
798 +{
799 + int i=0;
800 + int ok = 1;
801 +
802 +
803 + __u8 * dataBytes = (__u8 *)data;
804 +
805 +
806 +
807 + if(!dev->checkpointBuffer)
808 + return 0;
809 +
810 + if(!dev->checkpointOpenForWrite)
811 + return -1;
812 +
813 + while(i < nBytes && ok) {
814 +
815 +
816 +
817 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
818 + dev->checkpointSum += *dataBytes;
819 + dev->checkpointXor ^= *dataBytes;
820 +
821 + dev->checkpointByteOffset++;
822 + i++;
823 + dataBytes++;
824 + dev->checkpointByteCount++;
825 +
826 +
827 + if(dev->checkpointByteOffset < 0 ||
828 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
829 + ok = yaffs_CheckpointFlushBuffer(dev);
830 +
831 + }
832 +
833 + return i;
834 +}
835 +
836 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
837 +{
838 + int i=0;
839 + int ok = 1;
840 + yaffs_ExtendedTags tags;
841 +
842 +
843 + int chunk;
844 + int realignedChunk;
845 +
846 + __u8 *dataBytes = (__u8 *)data;
847 +
848 + if(!dev->checkpointBuffer)
849 + return 0;
850 +
851 + if(dev->checkpointOpenForWrite)
852 + return -1;
853 +
854 + while(i < nBytes && ok) {
855 +
856 +
857 + if(dev->checkpointByteOffset < 0 ||
858 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
859 +
860 + if(dev->checkpointCurrentBlock < 0){
861 + yaffs_CheckpointFindNextCheckpointBlock(dev);
862 + dev->checkpointCurrentChunk = 0;
863 + }
864 +
865 + if(dev->checkpointCurrentBlock < 0)
866 + ok = 0;
867 + else {
868 +
869 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
870 + dev->checkpointCurrentChunk;
871 +
872 + realignedChunk = chunk - dev->chunkOffset;
873 +
874 + /* read in the next chunk */
875 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
876 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
877 + dev->checkpointBuffer,
878 + &tags);
879 +
880 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
881 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
882 + ok = 0;
883 +
884 + dev->checkpointByteOffset = 0;
885 + dev->checkpointPageSequence++;
886 + dev->checkpointCurrentChunk++;
887 +
888 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
889 + dev->checkpointCurrentBlock = -1;
890 + }
891 + }
892 +
893 + if(ok){
894 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
895 + dev->checkpointSum += *dataBytes;
896 + dev->checkpointXor ^= *dataBytes;
897 + dev->checkpointByteOffset++;
898 + i++;
899 + dataBytes++;
900 + dev->checkpointByteCount++;
901 + }
902 + }
903 +
904 + return i;
905 +}
906 +
907 +int yaffs_CheckpointClose(yaffs_Device *dev)
908 +{
909 +
910 + if(dev->checkpointOpenForWrite){
911 + if(dev->checkpointByteOffset != 0)
912 + yaffs_CheckpointFlushBuffer(dev);
913 + } else {
914 + int i;
915 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
916 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
917 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
918 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
919 + else {
920 + // Todo this looks odd...
921 + }
922 + }
923 + YFREE(dev->checkpointBlockList);
924 + dev->checkpointBlockList = NULL;
925 + }
926 +
927 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
928 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
929 +
930 +
931 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
932 + dev->checkpointByteCount));
933 +
934 + if(dev->checkpointBuffer){
935 + /* free the buffer */
936 + YFREE(dev->checkpointBuffer);
937 + dev->checkpointBuffer = NULL;
938 + return 1;
939 + }
940 + else
941 + return 0;
942 +
943 +}
944 +
945 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
946 +{
947 + /* Erase the first checksum block */
948 +
949 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
950 +
951 + if(!yaffs_CheckpointSpaceOk(dev))
952 + return 0;
953 +
954 + return yaffs_CheckpointErase(dev);
955 +}
956 +
957 +
958 +
959 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h
960 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
961 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h 2007-06-08 14:07:26.000000000 +0200
962 @@ -0,0 +1,35 @@
963 +/*
964 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
965 + *
966 + * Copyright (C) 2002-2007 Aleph One Ltd.
967 + * for Toby Churchill Ltd and Brightstar Engineering
968 + *
969 + * Created by Charles Manning <charles@aleph1.co.uk>
970 + *
971 + * This program is free software; you can redistribute it and/or modify
972 + * it under the terms of the GNU Lesser General Public License version 2.1 as
973 + * published by the Free Software Foundation.
974 + *
975 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
976 + */
977 +
978 +#ifndef __YAFFS_CHECKPTRW_H__
979 +#define __YAFFS_CHECKPTRW_H__
980 +
981 +#include "yaffs_guts.h"
982 +
983 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
984 +
985 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
986 +
987 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
988 +
989 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
990 +
991 +int yaffs_CheckpointClose(yaffs_Device *dev);
992 +
993 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
994 +
995 +
996 +#endif
997 +
998 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c
999 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
1000 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c 2007-06-08 14:07:26.000000000 +0200
1001 @@ -0,0 +1,331 @@
1002 +/*
1003 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1004 + *
1005 + * Copyright (C) 2002-2007 Aleph One Ltd.
1006 + * for Toby Churchill Ltd and Brightstar Engineering
1007 + *
1008 + * Created by Charles Manning <charles@aleph1.co.uk>
1009 + *
1010 + * This program is free software; you can redistribute it and/or modify
1011 + * it under the terms of the GNU General Public License version 2 as
1012 + * published by the Free Software Foundation.
1013 + */
1014 +
1015 +/*
1016 + * This code implements the ECC algorithm used in SmartMedia.
1017 + *
1018 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1019 + * The two unused bit are set to 1.
1020 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1021 + * blocks are used on a 512-byte NAND page.
1022 + *
1023 + */
1024 +
1025 +/* Table generated by gen-ecc.c
1026 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1027 + * for each byte of data. These are instead provided in a table in bits7..2.
1028 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1029 + * this bytes influence on the line parity.
1030 + */
1031 +
1032 +const char *yaffs_ecc_c_version =
1033 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1034 +
1035 +#include "yportenv.h"
1036 +
1037 +#include "yaffs_ecc.h"
1038 +
1039 +static const unsigned char column_parity_table[] = {
1040 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1041 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1042 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1043 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1044 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1045 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1046 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1047 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1048 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1049 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1050 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1051 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1052 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1053 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1054 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1055 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1056 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1057 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1058 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1059 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1060 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1061 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1062 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1063 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1064 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1065 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1066 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1067 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1068 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1069 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1070 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1071 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1072 +};
1073 +
1074 +/* Count the bits in an unsigned char or a U32 */
1075 +
1076 +static int yaffs_CountBits(unsigned char x)
1077 +{
1078 + int r = 0;
1079 + while (x) {
1080 + if (x & 1)
1081 + r++;
1082 + x >>= 1;
1083 + }
1084 + return r;
1085 +}
1086 +
1087 +static int yaffs_CountBits32(unsigned x)
1088 +{
1089 + int r = 0;
1090 + while (x) {
1091 + if (x & 1)
1092 + r++;
1093 + x >>= 1;
1094 + }
1095 + return r;
1096 +}
1097 +
1098 +/* Calculate the ECC for a 256-byte block of data */
1099 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1100 +{
1101 + unsigned int i;
1102 +
1103 + unsigned char col_parity = 0;
1104 + unsigned char line_parity = 0;
1105 + unsigned char line_parity_prime = 0;
1106 + unsigned char t;
1107 + unsigned char b;
1108 +
1109 + for (i = 0; i < 256; i++) {
1110 + b = column_parity_table[*data++];
1111 + col_parity ^= b;
1112 +
1113 + if (b & 0x01) // odd number of bits in the byte
1114 + {
1115 + line_parity ^= i;
1116 + line_parity_prime ^= ~i;
1117 + }
1118 +
1119 + }
1120 +
1121 + ecc[2] = (~col_parity) | 0x03;
1122 +
1123 + t = 0;
1124 + if (line_parity & 0x80)
1125 + t |= 0x80;
1126 + if (line_parity_prime & 0x80)
1127 + t |= 0x40;
1128 + if (line_parity & 0x40)
1129 + t |= 0x20;
1130 + if (line_parity_prime & 0x40)
1131 + t |= 0x10;
1132 + if (line_parity & 0x20)
1133 + t |= 0x08;
1134 + if (line_parity_prime & 0x20)
1135 + t |= 0x04;
1136 + if (line_parity & 0x10)
1137 + t |= 0x02;
1138 + if (line_parity_prime & 0x10)
1139 + t |= 0x01;
1140 + ecc[1] = ~t;
1141 +
1142 + t = 0;
1143 + if (line_parity & 0x08)
1144 + t |= 0x80;
1145 + if (line_parity_prime & 0x08)
1146 + t |= 0x40;
1147 + if (line_parity & 0x04)
1148 + t |= 0x20;
1149 + if (line_parity_prime & 0x04)
1150 + t |= 0x10;
1151 + if (line_parity & 0x02)
1152 + t |= 0x08;
1153 + if (line_parity_prime & 0x02)
1154 + t |= 0x04;
1155 + if (line_parity & 0x01)
1156 + t |= 0x02;
1157 + if (line_parity_prime & 0x01)
1158 + t |= 0x01;
1159 + ecc[0] = ~t;
1160 +
1161 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1162 + // Swap the bytes into the wrong order
1163 + t = ecc[0];
1164 + ecc[0] = ecc[1];
1165 + ecc[1] = t;
1166 +#endif
1167 +}
1168 +
1169 +
1170 +/* Correct the ECC on a 256 byte block of data */
1171 +
1172 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1173 + const unsigned char *test_ecc)
1174 +{
1175 + unsigned char d0, d1, d2; /* deltas */
1176 +
1177 + d0 = read_ecc[0] ^ test_ecc[0];
1178 + d1 = read_ecc[1] ^ test_ecc[1];
1179 + d2 = read_ecc[2] ^ test_ecc[2];
1180 +
1181 + if ((d0 | d1 | d2) == 0)
1182 + return 0; /* no error */
1183 +
1184 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1185 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1186 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1187 + /* Single bit (recoverable) error in data */
1188 +
1189 + unsigned byte;
1190 + unsigned bit;
1191 +
1192 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1193 + // swap the bytes to correct for the wrong order
1194 + unsigned char t;
1195 +
1196 + t = d0;
1197 + d0 = d1;
1198 + d1 = t;
1199 +#endif
1200 +
1201 + bit = byte = 0;
1202 +
1203 + if (d1 & 0x80)
1204 + byte |= 0x80;
1205 + if (d1 & 0x20)
1206 + byte |= 0x40;
1207 + if (d1 & 0x08)
1208 + byte |= 0x20;
1209 + if (d1 & 0x02)
1210 + byte |= 0x10;
1211 + if (d0 & 0x80)
1212 + byte |= 0x08;
1213 + if (d0 & 0x20)
1214 + byte |= 0x04;
1215 + if (d0 & 0x08)
1216 + byte |= 0x02;
1217 + if (d0 & 0x02)
1218 + byte |= 0x01;
1219 +
1220 + if (d2 & 0x80)
1221 + bit |= 0x04;
1222 + if (d2 & 0x20)
1223 + bit |= 0x02;
1224 + if (d2 & 0x08)
1225 + bit |= 0x01;
1226 +
1227 + data[byte] ^= (1 << bit);
1228 +
1229 + return 1; /* Corrected the error */
1230 + }
1231 +
1232 + if ((yaffs_CountBits(d0) +
1233 + yaffs_CountBits(d1) +
1234 + yaffs_CountBits(d2)) == 1) {
1235 + /* Reccoverable error in ecc */
1236 +
1237 + read_ecc[0] = test_ecc[0];
1238 + read_ecc[1] = test_ecc[1];
1239 + read_ecc[2] = test_ecc[2];
1240 +
1241 + return 1; /* Corrected the error */
1242 + }
1243 +
1244 + /* Unrecoverable error */
1245 +
1246 + return -1;
1247 +
1248 +}
1249 +
1250 +
1251 +/*
1252 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1253 + */
1254 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1255 + yaffs_ECCOther * eccOther)
1256 +{
1257 + unsigned int i;
1258 +
1259 + unsigned char col_parity = 0;
1260 + unsigned line_parity = 0;
1261 + unsigned line_parity_prime = 0;
1262 + unsigned char b;
1263 +
1264 + for (i = 0; i < nBytes; i++) {
1265 + b = column_parity_table[*data++];
1266 + col_parity ^= b;
1267 +
1268 + if (b & 0x01) {
1269 + /* odd number of bits in the byte */
1270 + line_parity ^= i;
1271 + line_parity_prime ^= ~i;
1272 + }
1273 +
1274 + }
1275 +
1276 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1277 + eccOther->lineParity = line_parity;
1278 + eccOther->lineParityPrime = line_parity_prime;
1279 +}
1280 +
1281 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1282 + yaffs_ECCOther * read_ecc,
1283 + const yaffs_ECCOther * test_ecc)
1284 +{
1285 + unsigned char cDelta; /* column parity delta */
1286 + unsigned lDelta; /* line parity delta */
1287 + unsigned lDeltaPrime; /* line parity delta */
1288 + unsigned bit;
1289 +
1290 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1291 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1292 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1293 +
1294 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1295 + return 0; /* no error */
1296 +
1297 + if (lDelta == ~lDeltaPrime &&
1298 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1299 + {
1300 + /* Single bit (recoverable) error in data */
1301 +
1302 + bit = 0;
1303 +
1304 + if (cDelta & 0x20)
1305 + bit |= 0x04;
1306 + if (cDelta & 0x08)
1307 + bit |= 0x02;
1308 + if (cDelta & 0x02)
1309 + bit |= 0x01;
1310 +
1311 + if(lDelta >= nBytes)
1312 + return -1;
1313 +
1314 + data[lDelta] ^= (1 << bit);
1315 +
1316 + return 1; /* corrected */
1317 + }
1318 +
1319 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1320 + yaffs_CountBits(cDelta)) == 1) {
1321 + /* Reccoverable error in ecc */
1322 +
1323 + *read_ecc = *test_ecc;
1324 + return 1; /* corrected */
1325 + }
1326 +
1327 + /* Unrecoverable error */
1328 +
1329 + return -1;
1330 +
1331 +}
1332 +
1333 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h
1334 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1335 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h 2007-06-08 14:07:26.000000000 +0200
1336 @@ -0,0 +1,44 @@
1337 +/*
1338 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1339 + *
1340 + * Copyright (C) 2002-2007 Aleph One Ltd.
1341 + * for Toby Churchill Ltd and Brightstar Engineering
1342 + *
1343 + * Created by Charles Manning <charles@aleph1.co.uk>
1344 + *
1345 + * This program is free software; you can redistribute it and/or modify
1346 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1347 + * published by the Free Software Foundation.
1348 + *
1349 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1350 + */
1351 +
1352 + /*
1353 + * This code implements the ECC algorithm used in SmartMedia.
1354 + *
1355 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1356 + * The two unused bit are set to 1.
1357 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1358 + * blocks are used on a 512-byte NAND page.
1359 + *
1360 + */
1361 +
1362 +#ifndef __YAFFS_ECC_H__
1363 +#define __YAFFS_ECC_H__
1364 +
1365 +typedef struct {
1366 + unsigned char colParity;
1367 + unsigned lineParity;
1368 + unsigned lineParityPrime;
1369 +} yaffs_ECCOther;
1370 +
1371 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1372 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1373 + const unsigned char *test_ecc);
1374 +
1375 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1376 + yaffs_ECCOther * ecc);
1377 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1378 + yaffs_ECCOther * read_ecc,
1379 + const yaffs_ECCOther * test_ecc);
1380 +#endif
1381 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_fs.c linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c
1382 --- linux-2.6.21.1/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1383 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c 2007-06-08 14:07:26.000000000 +0200
1384 @@ -0,0 +1,2278 @@
1385 +/*
1386 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1387 + *
1388 + * Copyright (C) 2002-2007 Aleph One Ltd.
1389 + * for Toby Churchill Ltd and Brightstar Engineering
1390 + *
1391 + * Created by Charles Manning <charles@aleph1.co.uk>
1392 + * Acknowledgements:
1393 + * Luc van OostenRyck for numerous patches.
1394 + * Nick Bane for numerous patches.
1395 + * Nick Bane for 2.5/2.6 integration.
1396 + * Andras Toth for mknod rdev issue.
1397 + * Michael Fischer for finding the problem with inode inconsistency.
1398 + * Some code bodily lifted from JFFS
1399 + *
1400 + * This program is free software; you can redistribute it and/or modify
1401 + * it under the terms of the GNU General Public License version 2 as
1402 + * published by the Free Software Foundation.
1403 + */
1404 +
1405 +/*
1406 + *
1407 + * This is the file system front-end to YAFFS that hooks it up to
1408 + * the VFS.
1409 + *
1410 + * Special notes:
1411 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1412 + * this superblock
1413 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1414 + * superblock
1415 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1416 + */
1417 +
1418 +const char *yaffs_fs_c_version =
1419 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1420 +extern const char *yaffs_guts_c_version;
1421 +
1422 +#include <linux/version.h>
1423 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1424 +#include <linux/config.h>
1425 +#endif
1426 +#include <linux/kernel.h>
1427 +#include <linux/module.h>
1428 +#include <linux/slab.h>
1429 +#include <linux/init.h>
1430 +#include <linux/list.h>
1431 +#include <linux/fs.h>
1432 +#include <linux/proc_fs.h>
1433 +#include <linux/smp_lock.h>
1434 +#include <linux/pagemap.h>
1435 +#include <linux/mtd/mtd.h>
1436 +#include <linux/interrupt.h>
1437 +#include <linux/string.h>
1438 +#include <linux/ctype.h>
1439 +
1440 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1441 +
1442 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1443 +#include <asm/statfs.h>
1444 +#define UnlockPage(p) unlock_page(p)
1445 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1446 +
1447 +/* FIXME: use sb->s_id instead ? */
1448 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1449 +
1450 +#else
1451 +
1452 +#include <linux/locks.h>
1453 +#define BDEVNAME_SIZE 0
1454 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1455 +
1456 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1457 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1458 +#define __user
1459 +#endif
1460 +
1461 +#endif
1462 +
1463 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1464 +#define WRITE_SIZE_STR "writesize"
1465 +#define WRITE_SIZE(mtd) (mtd)->writesize
1466 +#else
1467 +#define WRITE_SIZE_STR "oobblock"
1468 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1469 +#endif
1470 +
1471 +#include <asm/uaccess.h>
1472 +
1473 +#include "yportenv.h"
1474 +#include "yaffs_guts.h"
1475 +
1476 +#include <linux/mtd/mtd.h>
1477 +#include "yaffs_mtdif.h"
1478 +#include "yaffs_mtdif1.h"
1479 +#include "yaffs_mtdif2.h"
1480 +
1481 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1482 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1483 +
1484 +/* Module Parameters */
1485 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1486 +module_param(yaffs_traceMask,uint,0644);
1487 +module_param(yaffs_wr_attempts,uint,0644);
1488 +#else
1489 +MODULE_PARM(yaffs_traceMask,"i");
1490 +MODULE_PARM(yaffs_wr_attempts,"i");
1491 +#endif
1492 +
1493 +/*#define T(x) printk x */
1494 +
1495 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1496 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1497 +#else
1498 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1499 +#endif
1500 +
1501 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1502 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1503 +
1504 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1505 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1506 +#else
1507 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1508 +#endif
1509 +
1510 +static void yaffs_put_super(struct super_block *sb);
1511 +
1512 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1513 + loff_t * pos);
1514 +
1515 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1516 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1517 +#else
1518 +static int yaffs_file_flush(struct file *file);
1519 +#endif
1520 +
1521 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1522 + int datasync);
1523 +
1524 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1525 +
1526 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1527 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1528 + struct nameidata *n);
1529 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1530 + struct nameidata *n);
1531 +#else
1532 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1533 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1534 +#endif
1535 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1536 + struct dentry *dentry);
1537 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1538 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1539 + const char *symname);
1540 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1541 +
1542 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1543 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1544 + dev_t dev);
1545 +#else
1546 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1547 + int dev);
1548 +#endif
1549 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1550 + struct inode *new_dir, struct dentry *new_dentry);
1551 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1552 +
1553 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1554 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1555 +static void yaffs_write_super(struct super_block *sb);
1556 +#else
1557 +static int yaffs_sync_fs(struct super_block *sb);
1558 +static int yaffs_write_super(struct super_block *sb);
1559 +#endif
1560 +
1561 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1562 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1563 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1564 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1565 +#else
1566 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1567 +#endif
1568 +static void yaffs_read_inode(struct inode *inode);
1569 +
1570 +static void yaffs_put_inode(struct inode *inode);
1571 +static void yaffs_delete_inode(struct inode *);
1572 +static void yaffs_clear_inode(struct inode *);
1573 +
1574 +static int yaffs_readpage(struct file *file, struct page *page);
1575 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1576 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1577 +#else
1578 +static int yaffs_writepage(struct page *page);
1579 +#endif
1580 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1581 + unsigned offset, unsigned to);
1582 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1583 + unsigned to);
1584 +
1585 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1586 + int buflen);
1587 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1588 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1589 +#else
1590 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1591 +#endif
1592 +
1593 +static struct address_space_operations yaffs_file_address_operations = {
1594 + .readpage = yaffs_readpage,
1595 + .writepage = yaffs_writepage,
1596 + .prepare_write = yaffs_prepare_write,
1597 + .commit_write = yaffs_commit_write,
1598 +};
1599 +
1600 +static struct file_operations yaffs_file_operations = {
1601 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1602 + .read = do_sync_read,
1603 + .write = do_sync_write,
1604 + .aio_read = generic_file_aio_read,
1605 + .aio_write = generic_file_aio_write,
1606 +#else
1607 + .read = generic_file_read,
1608 + .write = generic_file_write,
1609 +#endif
1610 + .mmap = generic_file_mmap,
1611 + .flush = yaffs_file_flush,
1612 + .fsync = yaffs_sync_object,
1613 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1614 + .sendfile = generic_file_sendfile,
1615 +#endif
1616 +
1617 +};
1618 +
1619 +static struct inode_operations yaffs_file_inode_operations = {
1620 + .setattr = yaffs_setattr,
1621 +};
1622 +
1623 +static struct inode_operations yaffs_symlink_inode_operations = {
1624 + .readlink = yaffs_readlink,
1625 + .follow_link = yaffs_follow_link,
1626 + .setattr = yaffs_setattr,
1627 +};
1628 +
1629 +static struct inode_operations yaffs_dir_inode_operations = {
1630 + .create = yaffs_create,
1631 + .lookup = yaffs_lookup,
1632 + .link = yaffs_link,
1633 + .unlink = yaffs_unlink,
1634 + .symlink = yaffs_symlink,
1635 + .mkdir = yaffs_mkdir,
1636 + .rmdir = yaffs_unlink,
1637 + .mknod = yaffs_mknod,
1638 + .rename = yaffs_rename,
1639 + .setattr = yaffs_setattr,
1640 +};
1641 +
1642 +static struct file_operations yaffs_dir_operations = {
1643 + .read = generic_read_dir,
1644 + .readdir = yaffs_readdir,
1645 + .fsync = yaffs_sync_object,
1646 +};
1647 +
1648 +static struct super_operations yaffs_super_ops = {
1649 + .statfs = yaffs_statfs,
1650 + .read_inode = yaffs_read_inode,
1651 + .put_inode = yaffs_put_inode,
1652 + .put_super = yaffs_put_super,
1653 + .delete_inode = yaffs_delete_inode,
1654 + .clear_inode = yaffs_clear_inode,
1655 + .sync_fs = yaffs_sync_fs,
1656 + .write_super = yaffs_write_super,
1657 +};
1658 +
1659 +static void yaffs_GrossLock(yaffs_Device * dev)
1660 +{
1661 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1662 +
1663 + down(&dev->grossLock);
1664 +}
1665 +
1666 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1667 +{
1668 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1669 + up(&dev->grossLock);
1670 +
1671 +}
1672 +
1673 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1674 + int buflen)
1675 +{
1676 + unsigned char *alias;
1677 + int ret;
1678 +
1679 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1680 +
1681 + yaffs_GrossLock(dev);
1682 +
1683 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1684 +
1685 + yaffs_GrossUnlock(dev);
1686 +
1687 + if (!alias)
1688 + return -ENOMEM;
1689 +
1690 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1691 + kfree(alias);
1692 + return ret;
1693 +}
1694 +
1695 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1696 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1697 +#else
1698 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1699 +#endif
1700 +{
1701 + unsigned char *alias;
1702 + int ret;
1703 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1704 +
1705 + yaffs_GrossLock(dev);
1706 +
1707 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1708 +
1709 + yaffs_GrossUnlock(dev);
1710 +
1711 + if (!alias)
1712 + {
1713 + ret = -ENOMEM;
1714 + goto out;
1715 + }
1716 +
1717 + ret = vfs_follow_link(nd, alias);
1718 + kfree(alias);
1719 +out:
1720 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1721 + return ERR_PTR (ret);
1722 +#else
1723 + return ret;
1724 +#endif
1725 +}
1726 +
1727 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1728 + yaffs_Object * obj);
1729 +
1730 +/*
1731 + * Lookup is used to find objects in the fs
1732 + */
1733 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1734 +
1735 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1736 + struct nameidata *n)
1737 +#else
1738 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1739 +#endif
1740 +{
1741 + yaffs_Object *obj;
1742 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1743 +
1744 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1745 +
1746 + yaffs_GrossLock(dev);
1747 +
1748 + T(YAFFS_TRACE_OS,
1749 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1750 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1751 +
1752 + obj =
1753 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1754 + dentry->d_name.name);
1755 +
1756 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1757 +
1758 + /* Can't hold gross lock when calling yaffs_get_inode() */
1759 + yaffs_GrossUnlock(dev);
1760 +
1761 + if (obj) {
1762 + T(YAFFS_TRACE_OS,
1763 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1764 +
1765 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1766 +
1767 + if (inode) {
1768 + T(YAFFS_TRACE_OS,
1769 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1770 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1771 + * d_add even if NULL inode */
1772 +#if 0
1773 + /*dget(dentry); // try to solve directory bug */
1774 + d_add(dentry, inode);
1775 +
1776 + /* return dentry; */
1777 + return NULL;
1778 +#endif
1779 + }
1780 +
1781 + } else {
1782 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1783 +
1784 + }
1785 +
1786 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1787 + * NULL which creates dentry hash */
1788 + d_add(dentry, inode);
1789 +
1790 + return NULL;
1791 + /* return (ERR_PTR(-EIO)); */
1792 +
1793 +}
1794 +
1795 +/* For now put inode is just for debugging
1796 + * Put inode is called when the inode **structure** is put.
1797 + */
1798 +static void yaffs_put_inode(struct inode *inode)
1799 +{
1800 + T(YAFFS_TRACE_OS,
1801 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1802 + atomic_read(&inode->i_count)));
1803 +
1804 +}
1805 +
1806 +/* clear is called to tell the fs to release any per-inode data it holds */
1807 +static void yaffs_clear_inode(struct inode *inode)
1808 +{
1809 + yaffs_Object *obj;
1810 + yaffs_Device *dev;
1811 +
1812 + obj = yaffs_InodeToObject(inode);
1813 +
1814 + T(YAFFS_TRACE_OS,
1815 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1816 + atomic_read(&inode->i_count),
1817 + obj ? "object exists" : "null object"));
1818 +
1819 + if (obj) {
1820 + dev = obj->myDev;
1821 + yaffs_GrossLock(dev);
1822 +
1823 + /* Clear the association between the inode and
1824 + * the yaffs_Object.
1825 + */
1826 + obj->myInode = NULL;
1827 + yaffs_InodeToObjectLV(inode) = NULL;
1828 +
1829 + /* If the object freeing was deferred, then the real
1830 + * free happens now.
1831 + * This should fix the inode inconsistency problem.
1832 + */
1833 +
1834 + yaffs_HandleDeferedFree(obj);
1835 +
1836 + yaffs_GrossUnlock(dev);
1837 + }
1838 +
1839 +}
1840 +
1841 +/* delete is called when the link count is zero and the inode
1842 + * is put (ie. nobody wants to know about it anymore, time to
1843 + * delete the file).
1844 + * NB Must call clear_inode()
1845 + */
1846 +static void yaffs_delete_inode(struct inode *inode)
1847 +{
1848 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1849 + yaffs_Device *dev;
1850 +
1851 + T(YAFFS_TRACE_OS,
1852 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1853 + atomic_read(&inode->i_count),
1854 + obj ? "object exists" : "null object"));
1855 +
1856 + if (obj) {
1857 + dev = obj->myDev;
1858 + yaffs_GrossLock(dev);
1859 + yaffs_DeleteFile(obj);
1860 + yaffs_GrossUnlock(dev);
1861 + }
1862 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1863 + truncate_inode_pages (&inode->i_data, 0);
1864 +#endif
1865 + clear_inode(inode);
1866 +}
1867 +
1868 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1869 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1870 +#else
1871 +static int yaffs_file_flush(struct file *file)
1872 +#endif
1873 +{
1874 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1875 +
1876 + yaffs_Device *dev = obj->myDev;
1877 +
1878 + T(YAFFS_TRACE_OS,
1879 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1880 + obj->dirty ? "dirty" : "clean"));
1881 +
1882 + yaffs_GrossLock(dev);
1883 +
1884 + yaffs_FlushFile(obj, 1);
1885 +
1886 + yaffs_GrossUnlock(dev);
1887 +
1888 + return 0;
1889 +}
1890 +
1891 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1892 +{
1893 + /* Lifted from jffs2 */
1894 +
1895 + yaffs_Object *obj;
1896 + unsigned char *pg_buf;
1897 + int ret;
1898 +
1899 + yaffs_Device *dev;
1900 +
1901 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1902 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1903 + (unsigned)PAGE_CACHE_SIZE));
1904 +
1905 + obj = yaffs_DentryToObject(f->f_dentry);
1906 +
1907 + dev = obj->myDev;
1908 +
1909 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1910 + BUG_ON(!PageLocked(pg));
1911 +#else
1912 + if (!PageLocked(pg))
1913 + PAGE_BUG(pg);
1914 +#endif
1915 +
1916 + pg_buf = kmap(pg);
1917 + /* FIXME: Can kmap fail? */
1918 +
1919 + yaffs_GrossLock(dev);
1920 +
1921 + ret =
1922 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1923 + PAGE_CACHE_SIZE);
1924 +
1925 + yaffs_GrossUnlock(dev);
1926 +
1927 + if (ret >= 0)
1928 + ret = 0;
1929 +
1930 + if (ret) {
1931 + ClearPageUptodate(pg);
1932 + SetPageError(pg);
1933 + } else {
1934 + SetPageUptodate(pg);
1935 + ClearPageError(pg);
1936 + }
1937 +
1938 + flush_dcache_page(pg);
1939 + kunmap(pg);
1940 +
1941 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1942 + return ret;
1943 +}
1944 +
1945 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1946 +{
1947 + int ret = yaffs_readpage_nolock(f, pg);
1948 + UnlockPage(pg);
1949 + return ret;
1950 +}
1951 +
1952 +static int yaffs_readpage(struct file *f, struct page *pg)
1953 +{
1954 + return yaffs_readpage_unlock(f, pg);
1955 +}
1956 +
1957 +/* writepage inspired by/stolen from smbfs */
1958 +
1959 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1960 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1961 +#else
1962 +static int yaffs_writepage(struct page *page)
1963 +#endif
1964 +{
1965 + struct address_space *mapping = page->mapping;
1966 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1967 + struct inode *inode;
1968 + unsigned long end_index;
1969 + char *buffer;
1970 + yaffs_Object *obj;
1971 + int nWritten = 0;
1972 + unsigned nBytes;
1973 +
1974 + if (!mapping)
1975 + BUG();
1976 + inode = mapping->host;
1977 + if (!inode)
1978 + BUG();
1979 +
1980 + if (offset > inode->i_size) {
1981 + T(YAFFS_TRACE_OS,
1982 + (KERN_DEBUG
1983 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1984 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1985 + (unsigned)inode->i_size));
1986 + T(YAFFS_TRACE_OS,
1987 + (KERN_DEBUG " -> don't care!!\n"));
1988 + unlock_page(page);
1989 + return 0;
1990 + }
1991 +
1992 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1993 +
1994 + /* easy case */
1995 + if (page->index < end_index) {
1996 + nBytes = PAGE_CACHE_SIZE;
1997 + } else {
1998 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1999 + }
2000 +
2001 + get_page(page);
2002 +
2003 + buffer = kmap(page);
2004 +
2005 + obj = yaffs_InodeToObject(inode);
2006 + yaffs_GrossLock(obj->myDev);
2007 +
2008 + T(YAFFS_TRACE_OS,
2009 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
2010 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2011 + T(YAFFS_TRACE_OS,
2012 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
2013 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2014 +
2015 + nWritten =
2016 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2017 + nBytes, 0);
2018 +
2019 + T(YAFFS_TRACE_OS,
2020 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2021 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2022 +
2023 + yaffs_GrossUnlock(obj->myDev);
2024 +
2025 + kunmap(page);
2026 + SetPageUptodate(page);
2027 + UnlockPage(page);
2028 + put_page(page);
2029 +
2030 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2031 +}
2032 +
2033 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2034 + unsigned offset, unsigned to)
2035 +{
2036 +
2037 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2038 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2039 + return yaffs_readpage_nolock(f, pg);
2040 +
2041 + return 0;
2042 +
2043 +}
2044 +
2045 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2046 + unsigned to)
2047 +{
2048 +
2049 + void *addr = page_address(pg) + offset;
2050 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2051 + int nBytes = to - offset;
2052 + int nWritten;
2053 +
2054 + unsigned spos = pos;
2055 + unsigned saddr = (unsigned)addr;
2056 +
2057 + T(YAFFS_TRACE_OS,
2058 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2059 + spos, nBytes));
2060 +
2061 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2062 +
2063 + if (nWritten != nBytes) {
2064 + T(YAFFS_TRACE_OS,
2065 + (KERN_DEBUG
2066 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2067 + nWritten, nBytes));
2068 + SetPageError(pg);
2069 + ClearPageUptodate(pg);
2070 + } else {
2071 + SetPageUptodate(pg);
2072 + }
2073 +
2074 + T(YAFFS_TRACE_OS,
2075 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2076 + nWritten == nBytes ? 0 : nWritten));
2077 +
2078 + return nWritten == nBytes ? 0 : nWritten;
2079 +
2080 +}
2081 +
2082 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2083 +{
2084 + if (inode && obj) {
2085 +
2086 +
2087 + /* Check mode against the variant type and attempt to repair if broken. */
2088 + __u32 mode = obj->yst_mode;
2089 + switch( obj->variantType ){
2090 + case YAFFS_OBJECT_TYPE_FILE :
2091 + if( ! S_ISREG(mode) ){
2092 + obj->yst_mode &= ~S_IFMT;
2093 + obj->yst_mode |= S_IFREG;
2094 + }
2095 +
2096 + break;
2097 + case YAFFS_OBJECT_TYPE_SYMLINK :
2098 + if( ! S_ISLNK(mode) ){
2099 + obj->yst_mode &= ~S_IFMT;
2100 + obj->yst_mode |= S_IFLNK;
2101 + }
2102 +
2103 + break;
2104 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2105 + if( ! S_ISDIR(mode) ){
2106 + obj->yst_mode &= ~S_IFMT;
2107 + obj->yst_mode |= S_IFDIR;
2108 + }
2109 +
2110 + break;
2111 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2112 + case YAFFS_OBJECT_TYPE_HARDLINK :
2113 + case YAFFS_OBJECT_TYPE_SPECIAL :
2114 + default:
2115 + /* TODO? */
2116 + break;
2117 + }
2118 +
2119 + inode->i_ino = obj->objectId;
2120 + inode->i_mode = obj->yst_mode;
2121 + inode->i_uid = obj->yst_uid;
2122 + inode->i_gid = obj->yst_gid;
2123 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2124 + inode->i_blksize = inode->i_sb->s_blocksize;
2125 +#endif
2126 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2127 +
2128 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2129 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2130 + inode->i_atime.tv_nsec = 0;
2131 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2132 + inode->i_mtime.tv_nsec = 0;
2133 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2134 + inode->i_ctime.tv_nsec = 0;
2135 +#else
2136 + inode->i_rdev = obj->yst_rdev;
2137 + inode->i_atime = obj->yst_atime;
2138 + inode->i_mtime = obj->yst_mtime;
2139 + inode->i_ctime = obj->yst_ctime;
2140 +#endif
2141 + inode->i_size = yaffs_GetObjectFileLength(obj);
2142 + inode->i_blocks = (inode->i_size + 511) >> 9;
2143 +
2144 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2145 +
2146 + T(YAFFS_TRACE_OS,
2147 + (KERN_DEBUG
2148 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2149 + inode->i_mode, inode->i_uid, inode->i_gid,
2150 + (int)inode->i_size, atomic_read(&inode->i_count)));
2151 +
2152 + switch (obj->yst_mode & S_IFMT) {
2153 + default: /* fifo, device or socket */
2154 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2155 + init_special_inode(inode, obj->yst_mode,
2156 + old_decode_dev(obj->yst_rdev));
2157 +#else
2158 + init_special_inode(inode, obj->yst_mode,
2159 + (dev_t) (obj->yst_rdev));
2160 +#endif
2161 + break;
2162 + case S_IFREG: /* file */
2163 + inode->i_op = &yaffs_file_inode_operations;
2164 + inode->i_fop = &yaffs_file_operations;
2165 + inode->i_mapping->a_ops =
2166 + &yaffs_file_address_operations;
2167 + break;
2168 + case S_IFDIR: /* directory */
2169 + inode->i_op = &yaffs_dir_inode_operations;
2170 + inode->i_fop = &yaffs_dir_operations;
2171 + break;
2172 + case S_IFLNK: /* symlink */
2173 + inode->i_op = &yaffs_symlink_inode_operations;
2174 + break;
2175 + }
2176 +
2177 + yaffs_InodeToObjectLV(inode) = obj;
2178 +
2179 + obj->myInode = inode;
2180 +
2181 + } else {
2182 + T(YAFFS_TRACE_OS,
2183 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2184 + }
2185 +
2186 +}
2187 +
2188 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2189 + yaffs_Object * obj)
2190 +{
2191 + struct inode *inode;
2192 +
2193 + if (!sb) {
2194 + T(YAFFS_TRACE_OS,
2195 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2196 + return NULL;
2197 +
2198 + }
2199 +
2200 + if (!obj) {
2201 + T(YAFFS_TRACE_OS,
2202 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2203 + return NULL;
2204 +
2205 + }
2206 +
2207 + T(YAFFS_TRACE_OS,
2208 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2209 +
2210 + inode = iget(sb, obj->objectId);
2211 +
2212 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2213 + /* iget also increments the inode's i_count */
2214 + /* NB You can't be holding grossLock or deadlock will happen! */
2215 +
2216 + return inode;
2217 +}
2218 +
2219 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2220 + loff_t * pos)
2221 +{
2222 + yaffs_Object *obj;
2223 + int nWritten, ipos;
2224 + struct inode *inode;
2225 + yaffs_Device *dev;
2226 +
2227 + obj = yaffs_DentryToObject(f->f_dentry);
2228 +
2229 + dev = obj->myDev;
2230 +
2231 + yaffs_GrossLock(dev);
2232 +
2233 + inode = f->f_dentry->d_inode;
2234 +
2235 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2236 + ipos = inode->i_size;
2237 + } else {
2238 + ipos = *pos;
2239 + }
2240 +
2241 + if (!obj) {
2242 + T(YAFFS_TRACE_OS,
2243 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2244 + } else {
2245 + T(YAFFS_TRACE_OS,
2246 + (KERN_DEBUG
2247 + "yaffs_file_write about to write writing %d bytes"
2248 + "to object %d at %d\n",
2249 + n, obj->objectId, ipos));
2250 + }
2251 +
2252 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2253 +
2254 + T(YAFFS_TRACE_OS,
2255 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2256 + n, nWritten, ipos));
2257 + if (nWritten > 0) {
2258 + ipos += nWritten;
2259 + *pos = ipos;
2260 + if (ipos > inode->i_size) {
2261 + inode->i_size = ipos;
2262 + inode->i_blocks = (ipos + 511) >> 9;
2263 +
2264 + T(YAFFS_TRACE_OS,
2265 + (KERN_DEBUG
2266 + "yaffs_file_write size updated to %d bytes, "
2267 + "%d blocks\n",
2268 + ipos, (int)(inode->i_blocks)));
2269 + }
2270 +
2271 + }
2272 + yaffs_GrossUnlock(dev);
2273 + return nWritten == 0 ? -ENOSPC : nWritten;
2274 +}
2275 +
2276 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2277 +{
2278 + yaffs_Object *obj;
2279 + yaffs_Device *dev;
2280 + struct inode *inode = f->f_dentry->d_inode;
2281 + unsigned long offset, curoffs;
2282 + struct list_head *i;
2283 + yaffs_Object *l;
2284 +
2285 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2286 +
2287 + obj = yaffs_DentryToObject(f->f_dentry);
2288 + dev = obj->myDev;
2289 +
2290 + yaffs_GrossLock(dev);
2291 +
2292 + offset = f->f_pos;
2293 +
2294 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2295 +
2296 + if (offset == 0) {
2297 + T(YAFFS_TRACE_OS,
2298 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2299 + (int)inode->i_ino));
2300 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2301 + < 0) {
2302 + goto out;
2303 + }
2304 + offset++;
2305 + f->f_pos++;
2306 + }
2307 + if (offset == 1) {
2308 + T(YAFFS_TRACE_OS,
2309 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2310 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2311 + if (filldir
2312 + (dirent, "..", 2, offset,
2313 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2314 + goto out;
2315 + }
2316 + offset++;
2317 + f->f_pos++;
2318 + }
2319 +
2320 + curoffs = 1;
2321 +
2322 + /* If the directory has changed since the open or last call to
2323 + readdir, rewind to after the 2 canned entries. */
2324 +
2325 + if (f->f_version != inode->i_version) {
2326 + offset = 2;
2327 + f->f_pos = offset;
2328 + f->f_version = inode->i_version;
2329 + }
2330 +
2331 + list_for_each(i, &obj->variant.directoryVariant.children) {
2332 + curoffs++;
2333 + if (curoffs >= offset) {
2334 + l = list_entry(i, yaffs_Object, siblings);
2335 +
2336 + yaffs_GetObjectName(l, name,
2337 + YAFFS_MAX_NAME_LENGTH + 1);
2338 + T(YAFFS_TRACE_OS,
2339 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2340 + yaffs_GetObjectInode(l)));
2341 +
2342 + if (filldir(dirent,
2343 + name,
2344 + strlen(name),
2345 + offset,
2346 + yaffs_GetObjectInode(l),
2347 + yaffs_GetObjectType(l))
2348 + < 0) {
2349 + goto up_and_out;
2350 + }
2351 +
2352 + offset++;
2353 + f->f_pos++;
2354 + }
2355 + }
2356 +
2357 + up_and_out:
2358 + out:
2359 +
2360 + yaffs_GrossUnlock(dev);
2361 +
2362 + return 0;
2363 +}
2364 +
2365 +/*
2366 + * File creation. Allocate an inode, and we're done..
2367 + */
2368 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2369 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2370 + dev_t rdev)
2371 +#else
2372 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2373 + int rdev)
2374 +#endif
2375 +{
2376 + struct inode *inode;
2377 +
2378 + yaffs_Object *obj = NULL;
2379 + yaffs_Device *dev;
2380 +
2381 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2382 +
2383 + int error = -ENOSPC;
2384 + uid_t uid = current->fsuid;
2385 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2386 +
2387 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2388 + mode |= S_ISGID;
2389 +
2390 + if (parent) {
2391 + T(YAFFS_TRACE_OS,
2392 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2393 + parent->objectId, parent->variantType));
2394 + } else {
2395 + T(YAFFS_TRACE_OS,
2396 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2397 + return -EPERM;
2398 + }
2399 +
2400 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2401 + "mode %x dev %x\n",
2402 + dentry->d_name.name, mode, rdev));
2403 +
2404 + dev = parent->myDev;
2405 +
2406 + yaffs_GrossLock(dev);
2407 +
2408 + switch (mode & S_IFMT) {
2409 + default:
2410 + /* Special (socket, fifo, device...) */
2411 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2412 + "yaffs_mknod: making special\n"));
2413 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2414 + obj =
2415 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2416 + gid, old_encode_dev(rdev));
2417 +#else
2418 + obj =
2419 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2420 + gid, rdev);
2421 +#endif
2422 + break;
2423 + case S_IFREG: /* file */
2424 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2425 + obj =
2426 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2427 + gid);
2428 + break;
2429 + case S_IFDIR: /* directory */
2430 + T(YAFFS_TRACE_OS,
2431 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2432 + obj =
2433 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2434 + uid, gid);
2435 + break;
2436 + case S_IFLNK: /* symlink */
2437 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2438 + obj = NULL; /* Do we ever get here? */
2439 + break;
2440 + }
2441 +
2442 + /* Can not call yaffs_get_inode() with gross lock held */
2443 + yaffs_GrossUnlock(dev);
2444 +
2445 + if (obj) {
2446 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2447 + d_instantiate(dentry, inode);
2448 + T(YAFFS_TRACE_OS,
2449 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2450 + obj->objectId, atomic_read(&inode->i_count)));
2451 + error = 0;
2452 + } else {
2453 + T(YAFFS_TRACE_OS,
2454 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2455 + error = -ENOMEM;
2456 + }
2457 +
2458 + return error;
2459 +}
2460 +
2461 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2462 +{
2463 + int retVal;
2464 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2465 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2466 +#if 0
2467 + /* attempt to fix dir bug - didn't work */
2468 + if (!retVal) {
2469 + dget(dentry);
2470 + }
2471 +#endif
2472 + return retVal;
2473 +}
2474 +
2475 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2476 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2477 + struct nameidata *n)
2478 +#else
2479 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2480 +#endif
2481 +{
2482 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2483 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2484 +}
2485 +
2486 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2487 +{
2488 + int retVal;
2489 +
2490 + yaffs_Device *dev;
2491 +
2492 + T(YAFFS_TRACE_OS,
2493 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2494 + dentry->d_name.name));
2495 +
2496 + dev = yaffs_InodeToObject(dir)->myDev;
2497 +
2498 + yaffs_GrossLock(dev);
2499 +
2500 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2501 +
2502 + if (retVal == YAFFS_OK) {
2503 + dentry->d_inode->i_nlink--;
2504 + dir->i_version++;
2505 + yaffs_GrossUnlock(dev);
2506 + mark_inode_dirty(dentry->d_inode);
2507 + return 0;
2508 + }
2509 + yaffs_GrossUnlock(dev);
2510 + return -ENOTEMPTY;
2511 +}
2512 +
2513 +/*
2514 + * Create a link...
2515 + */
2516 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2517 + struct dentry *dentry)
2518 +{
2519 + struct inode *inode = old_dentry->d_inode;
2520 + yaffs_Object *obj = NULL;
2521 + yaffs_Object *link = NULL;
2522 + yaffs_Device *dev;
2523 +
2524 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2525 +
2526 + obj = yaffs_InodeToObject(inode);
2527 + dev = obj->myDev;
2528 +
2529 + yaffs_GrossLock(dev);
2530 +
2531 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2532 + {
2533 + link =
2534 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2535 + obj);
2536 + }
2537 +
2538 + if (link) {
2539 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2540 + d_instantiate(dentry, old_dentry->d_inode);
2541 + atomic_inc(&old_dentry->d_inode->i_count);
2542 + T(YAFFS_TRACE_OS,
2543 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2544 + old_dentry->d_inode->i_nlink,
2545 + atomic_read(&old_dentry->d_inode->i_count)));
2546 +
2547 + }
2548 +
2549 + yaffs_GrossUnlock(dev);
2550 +
2551 + if (link) {
2552 +
2553 + return 0;
2554 + }
2555 +
2556 + return -EPERM;
2557 +}
2558 +
2559 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2560 + const char *symname)
2561 +{
2562 + yaffs_Object *obj;
2563 + yaffs_Device *dev;
2564 + uid_t uid = current->fsuid;
2565 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2566 +
2567 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2568 +
2569 + dev = yaffs_InodeToObject(dir)->myDev;
2570 + yaffs_GrossLock(dev);
2571 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2572 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2573 + yaffs_GrossUnlock(dev);
2574 +
2575 + if (obj) {
2576 +
2577 + struct inode *inode;
2578 +
2579 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2580 + d_instantiate(dentry, inode);
2581 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2582 + return 0;
2583 + } else {
2584 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2585 +
2586 + }
2587 +
2588 + return -ENOMEM;
2589 +}
2590 +
2591 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2592 + int datasync)
2593 +{
2594 +
2595 + yaffs_Object *obj;
2596 + yaffs_Device *dev;
2597 +
2598 + obj = yaffs_DentryToObject(dentry);
2599 +
2600 + dev = obj->myDev;
2601 +
2602 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2603 + yaffs_GrossLock(dev);
2604 + yaffs_FlushFile(obj, 1);
2605 + yaffs_GrossUnlock(dev);
2606 + return 0;
2607 +}
2608 +
2609 +/*
2610 + * The VFS layer already does all the dentry stuff for rename.
2611 + *
2612 + * NB: POSIX says you can rename an object over an old object of the same name
2613 + */
2614 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2615 + struct inode *new_dir, struct dentry *new_dentry)
2616 +{
2617 + yaffs_Device *dev;
2618 + int retVal = YAFFS_FAIL;
2619 + yaffs_Object *target;
2620 +
2621 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2622 + dev = yaffs_InodeToObject(old_dir)->myDev;
2623 +
2624 + yaffs_GrossLock(dev);
2625 +
2626 + /* Check if the target is an existing directory that is not empty. */
2627 + target =
2628 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2629 + new_dentry->d_name.name);
2630 +
2631 +
2632 +
2633 + if (target &&
2634 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2635 + !list_empty(&target->variant.directoryVariant.children)) {
2636 +
2637 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2638 +
2639 + retVal = YAFFS_FAIL;
2640 + } else {
2641 +
2642 + /* Now does unlinking internally using shadowing mechanism */
2643 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2644 +
2645 + retVal =
2646 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2647 + old_dentry->d_name.name,
2648 + yaffs_InodeToObject(new_dir),
2649 + new_dentry->d_name.name);
2650 +
2651 + }
2652 + yaffs_GrossUnlock(dev);
2653 +
2654 + if (retVal == YAFFS_OK) {
2655 + if(target) {
2656 + new_dentry->d_inode->i_nlink--;
2657 + mark_inode_dirty(new_dentry->d_inode);
2658 + }
2659 +
2660 + return 0;
2661 + } else {
2662 + return -ENOTEMPTY;
2663 + }
2664 +
2665 +}
2666 +
2667 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2668 +{
2669 + struct inode *inode = dentry->d_inode;
2670 + int error;
2671 + yaffs_Device *dev;
2672 +
2673 + T(YAFFS_TRACE_OS,
2674 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2675 + yaffs_InodeToObject(inode)->objectId));
2676 +
2677 + if ((error = inode_change_ok(inode, attr)) == 0) {
2678 +
2679 + dev = yaffs_InodeToObject(inode)->myDev;
2680 + yaffs_GrossLock(dev);
2681 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2682 + YAFFS_OK) {
2683 + error = 0;
2684 + } else {
2685 + error = -EPERM;
2686 + }
2687 + yaffs_GrossUnlock(dev);
2688 + if (!error)
2689 + error = inode_setattr(inode, attr);
2690 + }
2691 + return error;
2692 +}
2693 +
2694 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2695 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2696 +{
2697 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2698 + struct super_block *sb = dentry->d_sb;
2699 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2700 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2701 +{
2702 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2703 +#else
2704 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2705 +{
2706 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2707 +#endif
2708 +
2709 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2710 +
2711 + yaffs_GrossLock(dev);
2712 +
2713 + buf->f_type = YAFFS_MAGIC;
2714 + buf->f_bsize = sb->s_blocksize;
2715 + buf->f_namelen = 255;
2716 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2717 +
2718 + buf->f_blocks =
2719 + (dev->endBlock - dev->startBlock +
2720 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2721 + dev->nDataBytesPerChunk);
2722 + buf->f_bfree =
2723 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2724 + dev->nDataBytesPerChunk);
2725 + } else {
2726 +
2727 + buf->f_blocks =
2728 + (dev->endBlock - dev->startBlock +
2729 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2730 + sb->s_blocksize);
2731 + buf->f_bfree =
2732 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2733 + sb->s_blocksize);
2734 + }
2735 + buf->f_files = 0;
2736 + buf->f_ffree = 0;
2737 + buf->f_bavail = buf->f_bfree;
2738 +
2739 + yaffs_GrossUnlock(dev);
2740 + return 0;
2741 +}
2742 +
2743 +
2744 +/**
2745 +static int yaffs_do_sync_fs(struct super_block *sb)
2746 +{
2747 +
2748 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2749 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2750 +
2751 + if(sb->s_dirt) {
2752 + yaffs_GrossLock(dev);
2753 +
2754 + if(dev)
2755 + yaffs_CheckpointSave(dev);
2756 +
2757 + yaffs_GrossUnlock(dev);
2758 +
2759 + sb->s_dirt = 0;
2760 + }
2761 + return 0;
2762 +}
2763 +**/
2764 +
2765 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2766 +static void yaffs_write_super(struct super_block *sb)
2767 +#else
2768 +static int yaffs_write_super(struct super_block *sb)
2769 +#endif
2770 +{
2771 +
2772 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2773 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2774 + return 0; /* yaffs_do_sync_fs(sb);*/
2775 +#endif
2776 +}
2777 +
2778 +
2779 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2780 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2781 +#else
2782 +static int yaffs_sync_fs(struct super_block *sb)
2783 +#endif
2784 +{
2785 +
2786 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2787 +
2788 + return 0; /* yaffs_do_sync_fs(sb);*/
2789 +
2790 +}
2791 +
2792 +
2793 +static void yaffs_read_inode(struct inode *inode)
2794 +{
2795 + /* NB This is called as a side effect of other functions, but
2796 + * we had to release the lock to prevent deadlocks, so
2797 + * need to lock again.
2798 + */
2799 +
2800 + yaffs_Object *obj;
2801 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2802 +
2803 + T(YAFFS_TRACE_OS,
2804 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2805 +
2806 + yaffs_GrossLock(dev);
2807 +
2808 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2809 +
2810 + yaffs_FillInodeFromObject(inode, obj);
2811 +
2812 + yaffs_GrossUnlock(dev);
2813 +}
2814 +
2815 +static LIST_HEAD(yaffs_dev_list);
2816 +
2817 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2818 +{
2819 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2820 +
2821 + if( *flags & MS_RDONLY ) {
2822 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2823 +
2824 + T(YAFFS_TRACE_OS,
2825 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2826 +
2827 + yaffs_GrossLock(dev);
2828 +
2829 + yaffs_FlushEntireDeviceCache(dev);
2830 +
2831 + yaffs_CheckpointSave(dev);
2832 +
2833 + if (mtd->sync)
2834 + mtd->sync(mtd);
2835 +
2836 + yaffs_GrossUnlock(dev);
2837 + }
2838 + else {
2839 + T(YAFFS_TRACE_OS,
2840 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2841 + }
2842 +
2843 + return 0;
2844 +}
2845 +
2846 +static void yaffs_put_super(struct super_block *sb)
2847 +{
2848 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2849 +
2850 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2851 +
2852 + yaffs_GrossLock(dev);
2853 +
2854 + yaffs_FlushEntireDeviceCache(dev);
2855 +
2856 + yaffs_CheckpointSave(dev);
2857 +
2858 + if (dev->putSuperFunc) {
2859 + dev->putSuperFunc(sb);
2860 + }
2861 +
2862 + yaffs_Deinitialise(dev);
2863 +
2864 + yaffs_GrossUnlock(dev);
2865 +
2866 + /* we assume this is protected by lock_kernel() in mount/umount */
2867 + list_del(&dev->devList);
2868 +
2869 + if(dev->spareBuffer){
2870 + YFREE(dev->spareBuffer);
2871 + dev->spareBuffer = NULL;
2872 + }
2873 +
2874 + kfree(dev);
2875 +}
2876 +
2877 +
2878 +static void yaffs_MTDPutSuper(struct super_block *sb)
2879 +{
2880 +
2881 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2882 +
2883 + if (mtd->sync) {
2884 + mtd->sync(mtd);
2885 + }
2886 +
2887 + put_mtd_device(mtd);
2888 +}
2889 +
2890 +
2891 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2892 +{
2893 + struct super_block *sb = (struct super_block *)vsb;
2894 +
2895 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2896 +// if(sb)
2897 +// sb->s_dirt = 1;
2898 +}
2899 +
2900 +typedef struct {
2901 + int inband_tags;
2902 + int skip_checkpoint_read;
2903 + int skip_checkpoint_write;
2904 + int no_cache;
2905 +} yaffs_options;
2906 +
2907 +#define MAX_OPT_LEN 20
2908 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2909 +{
2910 + char cur_opt[MAX_OPT_LEN+1];
2911 + int p;
2912 + int error = 0;
2913 +
2914 + /* Parse through the options which is a comma seperated list */
2915 +
2916 + while(options_str && *options_str && !error){
2917 + memset(cur_opt,0,MAX_OPT_LEN+1);
2918 + p = 0;
2919 +
2920 + while(*options_str && *options_str != ','){
2921 + if(p < MAX_OPT_LEN){
2922 + cur_opt[p] = *options_str;
2923 + p++;
2924 + }
2925 + options_str++;
2926 + }
2927 +
2928 + if(!strcmp(cur_opt,"inband-tags"))
2929 + options->inband_tags = 1;
2930 + else if(!strcmp(cur_opt,"no-cache"))
2931 + options->no_cache = 1;
2932 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2933 + options->skip_checkpoint_read = 1;
2934 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2935 + options->skip_checkpoint_write = 1;
2936 + else if(!strcmp(cur_opt,"no-checkpoint")){
2937 + options->skip_checkpoint_read = 1;
2938 + options->skip_checkpoint_write = 1;
2939 + } else {
2940 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2941 + error = 1;
2942 + }
2943 +
2944 + }
2945 +
2946 + return error;
2947 +}
2948 +
2949 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2950 + struct super_block *sb,
2951 + void *data, int silent)
2952 +{
2953 + int nBlocks;
2954 + struct inode *inode = NULL;
2955 + struct dentry *root;
2956 + yaffs_Device *dev = 0;
2957 + char devname_buf[BDEVNAME_SIZE + 1];
2958 + struct mtd_info *mtd;
2959 + int err;
2960 + char *data_str = (char *)data;
2961 +
2962 + yaffs_options options;
2963 +
2964 + sb->s_magic = YAFFS_MAGIC;
2965 + sb->s_op = &yaffs_super_ops;
2966 +
2967 + if (!sb)
2968 + printk(KERN_INFO "yaffs: sb is NULL\n");
2969 + else if (!sb->s_dev)
2970 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2971 + else if (!yaffs_devname(sb, devname_buf))
2972 + printk(KERN_INFO "yaffs: devname is NULL\n");
2973 + else
2974 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2975 + sb->s_dev,
2976 + yaffs_devname(sb, devname_buf));
2977 +
2978 + if(!data_str)
2979 + data_str = "";
2980 +
2981 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2982 +
2983 + memset(&options,0,sizeof(options));
2984 +
2985 + if(yaffs_parse_options(&options,data_str)){
2986 + /* Option parsing failed */
2987 + return NULL;
2988 + }
2989 +
2990 +
2991 + sb->s_blocksize = PAGE_CACHE_SIZE;
2992 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2993 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2994 + T(YAFFS_TRACE_OS,
2995 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2996 +
2997 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2998 + T(YAFFS_TRACE_OS,
2999 + ("yaffs: Write verification disabled. All guarantees "
3000 + "null and void\n"));
3001 +#endif
3002 +
3003 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3004 + "\"%s\"\n",
3005 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
3006 + yaffs_devname(sb, devname_buf)));
3007 +
3008 + /* Check it's an mtd device..... */
3009 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
3010 + return NULL; /* This isn't an mtd device */
3011 + }
3012 + /* Get the device */
3013 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3014 + if (!mtd) {
3015 + T(YAFFS_TRACE_ALWAYS,
3016 + ("yaffs: MTD device #%u doesn't appear to exist\n",
3017 + MINOR(sb->s_dev)));
3018 + return NULL;
3019 + }
3020 + /* Check it's NAND */
3021 + if (mtd->type != MTD_NANDFLASH) {
3022 + T(YAFFS_TRACE_ALWAYS,
3023 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3024 + return NULL;
3025 + }
3026 +
3027 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3028 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3029 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3030 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3031 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3032 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3033 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3034 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3035 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3036 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3037 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3038 +
3039 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3040 +
3041 + if (yaffsVersion == 1 &&
3042 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3043 + mtd->writesize >= 2048) {
3044 +#else
3045 + mtd->oobblock >= 2048) {
3046 +#endif
3047 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3048 + yaffsVersion = 2;
3049 + }
3050 +
3051 + /* Added NCB 26/5/2006 for completeness */
3052 + if (yaffsVersion == 2 &&
3053 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3054 + mtd->writesize == 512) {
3055 +#else
3056 + mtd->oobblock == 512) {
3057 +#endif
3058 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3059 + yaffsVersion = 1;
3060 + }
3061 +
3062 +#endif
3063 +
3064 + if (yaffsVersion == 2) {
3065 + /* Check for version 2 style functions */
3066 + if (!mtd->erase ||
3067 + !mtd->block_isbad ||
3068 + !mtd->block_markbad ||
3069 + !mtd->read ||
3070 + !mtd->write ||
3071 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3072 + !mtd->read_oob || !mtd->write_oob) {
3073 +#else
3074 + !mtd->write_ecc ||
3075 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3076 +#endif
3077 + T(YAFFS_TRACE_ALWAYS,
3078 + ("yaffs: MTD device does not support required "
3079 + "functions\n"));;
3080 + return NULL;
3081 + }
3082 +
3083 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3084 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3085 +#else
3086 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3087 +#endif
3088 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3089 + T(YAFFS_TRACE_ALWAYS,
3090 + ("yaffs: MTD device does not have the "
3091 + "right page sizes\n"));
3092 + return NULL;
3093 + }
3094 + } else {
3095 + /* Check for V1 style functions */
3096 + if (!mtd->erase ||
3097 + !mtd->read ||
3098 + !mtd->write ||
3099 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3100 + !mtd->read_oob || !mtd->write_oob) {
3101 +#else
3102 + !mtd->write_ecc ||
3103 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3104 +#endif
3105 + T(YAFFS_TRACE_ALWAYS,
3106 + ("yaffs: MTD device does not support required "
3107 + "functions\n"));;
3108 + return NULL;
3109 + }
3110 +
3111 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3112 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3113 + T(YAFFS_TRACE_ALWAYS,
3114 + ("yaffs: MTD device does not support have the "
3115 + "right page sizes\n"));
3116 + return NULL;
3117 + }
3118 + }
3119 +
3120 + /* OK, so if we got here, we have an MTD that's NAND and looks
3121 + * like it has the right capabilities
3122 + * Set the yaffs_Device up for mtd
3123 + */
3124 +
3125 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3126 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3127 +#else
3128 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3129 +#endif
3130 + if (!dev) {
3131 + /* Deep shit could not allocate device structure */
3132 + T(YAFFS_TRACE_ALWAYS,
3133 + ("yaffs_read_super: Failed trying to allocate "
3134 + "yaffs_Device. \n"));
3135 + return NULL;
3136 + }
3137 +
3138 + memset(dev, 0, sizeof(yaffs_Device));
3139 + dev->genericDevice = mtd;
3140 + dev->name = mtd->name;
3141 +
3142 + /* Set up the memory size parameters.... */
3143 +
3144 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3145 + dev->startBlock = 0;
3146 + dev->endBlock = nBlocks - 1;
3147 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3148 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3149 + dev->nReservedBlocks = 5;
3150 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3151 +
3152 + /* ... and the functions. */
3153 + if (yaffsVersion == 2) {
3154 + dev->writeChunkWithTagsToNAND =
3155 + nandmtd2_WriteChunkWithTagsToNAND;
3156 + dev->readChunkWithTagsFromNAND =
3157 + nandmtd2_ReadChunkWithTagsFromNAND;
3158 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3159 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3160 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3161 + dev->isYaffs2 = 1;
3162 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3163 + dev->nDataBytesPerChunk = mtd->writesize;
3164 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3165 +#else
3166 + dev->nDataBytesPerChunk = mtd->oobblock;
3167 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3168 +#endif
3169 + nBlocks = mtd->size / mtd->erasesize;
3170 +
3171 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3172 + dev->startBlock = 0;
3173 + dev->endBlock = nBlocks - 1;
3174 + } else {
3175 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3176 + /* use the MTD interface in yaffs_mtdif1.c */
3177 + dev->writeChunkWithTagsToNAND =
3178 + nandmtd1_WriteChunkWithTagsToNAND;
3179 + dev->readChunkWithTagsFromNAND =
3180 + nandmtd1_ReadChunkWithTagsFromNAND;
3181 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3182 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3183 +#else
3184 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3185 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3186 +#endif
3187 + dev->isYaffs2 = 0;
3188 + }
3189 + /* ... and common functions */
3190 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3191 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3192 +
3193 + dev->putSuperFunc = yaffs_MTDPutSuper;
3194 +
3195 + dev->superBlock = (void *)sb;
3196 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3197 +
3198 +
3199 +#ifndef CONFIG_YAFFS_DOES_ECC
3200 + dev->useNANDECC = 1;
3201 +#endif
3202 +
3203 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3204 + dev->wideTnodesDisabled = 1;
3205 +#endif
3206 +
3207 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3208 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3209 +
3210 + /* we assume this is protected by lock_kernel() in mount/umount */
3211 + list_add_tail(&dev->devList, &yaffs_dev_list);
3212 +
3213 + init_MUTEX(&dev->grossLock);
3214 +
3215 + yaffs_GrossLock(dev);
3216 +
3217 + err = yaffs_GutsInitialise(dev);
3218 +
3219 + T(YAFFS_TRACE_OS,
3220 + ("yaffs_read_super: guts initialised %s\n",
3221 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3222 +
3223 + /* Release lock before yaffs_get_inode() */
3224 + yaffs_GrossUnlock(dev);
3225 +
3226 + /* Create root inode */
3227 + if (err == YAFFS_OK)
3228 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3229 + yaffs_Root(dev));
3230 +
3231 + if (!inode)
3232 + return NULL;
3233 +
3234 + inode->i_op = &yaffs_dir_inode_operations;
3235 + inode->i_fop = &yaffs_dir_operations;
3236 +
3237 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3238 +
3239 + root = d_alloc_root(inode);
3240 +
3241 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3242 +
3243 + if (!root) {
3244 + iput(inode);
3245 + return NULL;
3246 + }
3247 + sb->s_root = root;
3248 +
3249 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3250 + return sb;
3251 +}
3252 +
3253 +
3254 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3255 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3256 + int silent)
3257 +{
3258 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3259 +}
3260 +
3261 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3262 +static int yaffs_read_super(struct file_system_type *fs,
3263 + int flags, const char *dev_name,
3264 + void *data, struct vfsmount *mnt)
3265 +{
3266 +
3267 + return get_sb_bdev(fs, flags, dev_name, data,
3268 + yaffs_internal_read_super_mtd, mnt);
3269 +}
3270 +#else
3271 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3272 + int flags, const char *dev_name,
3273 + void *data)
3274 +{
3275 +
3276 + return get_sb_bdev(fs, flags, dev_name, data,
3277 + yaffs_internal_read_super_mtd);
3278 +}
3279 +#endif
3280 +
3281 +static struct file_system_type yaffs_fs_type = {
3282 + .owner = THIS_MODULE,
3283 + .name = "yaffs",
3284 + .get_sb = yaffs_read_super,
3285 + .kill_sb = kill_block_super,
3286 + .fs_flags = FS_REQUIRES_DEV,
3287 +};
3288 +#else
3289 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3290 + int silent)
3291 +{
3292 + return yaffs_internal_read_super(1, sb, data, silent);
3293 +}
3294 +
3295 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3296 + FS_REQUIRES_DEV);
3297 +#endif
3298 +
3299 +
3300 +#ifdef CONFIG_YAFFS_YAFFS2
3301 +
3302 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3303 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3304 + int silent)
3305 +{
3306 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3307 +}
3308 +
3309 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3310 +static int yaffs2_read_super(struct file_system_type *fs,
3311 + int flags, const char *dev_name, void *data,
3312 + struct vfsmount *mnt)
3313 +{
3314 + return get_sb_bdev(fs, flags, dev_name, data,
3315 + yaffs2_internal_read_super_mtd, mnt);
3316 +}
3317 +#else
3318 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3319 + int flags, const char *dev_name,
3320 + void *data)
3321 +{
3322 +
3323 + return get_sb_bdev(fs, flags, dev_name, data,
3324 + yaffs2_internal_read_super_mtd);
3325 +}
3326 +#endif
3327 +
3328 +static struct file_system_type yaffs2_fs_type = {
3329 + .owner = THIS_MODULE,
3330 + .name = "yaffs2",
3331 + .get_sb = yaffs2_read_super,
3332 + .kill_sb = kill_block_super,
3333 + .fs_flags = FS_REQUIRES_DEV,
3334 +};
3335 +#else
3336 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3337 + void *data, int silent)
3338 +{
3339 + return yaffs_internal_read_super(2, sb, data, silent);
3340 +}
3341 +
3342 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3343 + FS_REQUIRES_DEV);
3344 +#endif
3345 +
3346 +#endif /* CONFIG_YAFFS_YAFFS2 */
3347 +
3348 +static struct proc_dir_entry *my_proc_entry;
3349 +
3350 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3351 +{
3352 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3353 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3354 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3355 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3356 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3357 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3358 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3359 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3360 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3361 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3362 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3363 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3364 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3365 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3366 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3367 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3368 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3369 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3370 + buf +=
3371 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3372 + buf +=
3373 + sprintf(buf, "passiveGCs......... %d\n",
3374 + dev->passiveGarbageCollections);
3375 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3376 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3377 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3378 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3379 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3380 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3381 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3382 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3383 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3384 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3385 + buf +=
3386 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3387 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3388 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3389 +
3390 + return buf;
3391 +}
3392 +
3393 +static int yaffs_proc_read(char *page,
3394 + char **start,
3395 + off_t offset, int count, int *eof, void *data)
3396 +{
3397 + struct list_head *item;
3398 + char *buf = page;
3399 + int step = offset;
3400 + int n = 0;
3401 +
3402 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3403 + * We use 'offset' (*ppos) to indicate where we are in devList.
3404 + * This also assumes the user has posted a read buffer large
3405 + * enough to hold the complete output; but that's life in /proc.
3406 + */
3407 +
3408 + *(int *)start = 1;
3409 +
3410 + /* Print header first */
3411 + if (step == 0) {
3412 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3413 + "\n%s\n%s\n", yaffs_fs_c_version,
3414 + yaffs_guts_c_version);
3415 + }
3416 +
3417 + /* hold lock_kernel while traversing yaffs_dev_list */
3418 + lock_kernel();
3419 +
3420 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3421 + list_for_each(item, &yaffs_dev_list) {
3422 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3423 + if (n < step) {
3424 + n++;
3425 + continue;
3426 + }
3427 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3428 + buf = yaffs_dump_dev(buf, dev);
3429 + break;
3430 + }
3431 + unlock_kernel();
3432 +
3433 + return buf - page < count ? buf - page : count;
3434 +}
3435 +
3436 +/**
3437 + * Set the verbosity of the warnings and error messages.
3438 + *
3439 + * Note that the names can only be a..z or _ with the current code.
3440 + */
3441 +
3442 +static struct {
3443 + char *mask_name;
3444 + unsigned mask_bitfield;
3445 +} mask_flags[] = {
3446 + {"allocate", YAFFS_TRACE_ALLOCATE},
3447 + {"always", YAFFS_TRACE_ALWAYS},
3448 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3449 + {"buffers", YAFFS_TRACE_BUFFERS},
3450 + {"bug", YAFFS_TRACE_BUG},
3451 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3452 + {"deletion", YAFFS_TRACE_DELETION},
3453 + {"erase", YAFFS_TRACE_ERASE},
3454 + {"error", YAFFS_TRACE_ERROR},
3455 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3456 + {"gc", YAFFS_TRACE_GC},
3457 + {"mtd", YAFFS_TRACE_MTD},
3458 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3459 + {"os", YAFFS_TRACE_OS},
3460 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3461 + {"scan", YAFFS_TRACE_SCAN},
3462 + {"tracing", YAFFS_TRACE_TRACING},
3463 +
3464 + {"verify", YAFFS_TRACE_VERIFY},
3465 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3466 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3467 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3468 +
3469 + {"write", YAFFS_TRACE_WRITE},
3470 + {"all", 0xffffffff},
3471 + {"none", 0},
3472 + {NULL, 0},
3473 +};
3474 +
3475 +#define MAX_MASK_NAME_LENGTH 40
3476 +static int yaffs_proc_write(struct file *file, const char *buf,
3477 + unsigned long count, void *data)
3478 +{
3479 + unsigned rg = 0, mask_bitfield;
3480 + char *end;
3481 + char *mask_name;
3482 + char *x;
3483 + char substring[MAX_MASK_NAME_LENGTH+1];
3484 + int i;
3485 + int done = 0;
3486 + int add, len = 0;
3487 + int pos = 0;
3488 +
3489 + rg = yaffs_traceMask;
3490 +
3491 + while (!done && (pos < count)) {
3492 + done = 1;
3493 + while ((pos < count) && isspace(buf[pos])) {
3494 + pos++;
3495 + }
3496 +
3497 + switch (buf[pos]) {
3498 + case '+':
3499 + case '-':
3500 + case '=':
3501 + add = buf[pos];
3502 + pos++;
3503 + break;
3504 +
3505 + default:
3506 + add = ' ';
3507 + break;
3508 + }
3509 + mask_name = NULL;
3510 +
3511 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3512 + if (end > buf + pos) {
3513 + mask_name = "numeral";
3514 + len = end - (buf + pos);
3515 + done = 0;
3516 + } else {
3517 + for(x = buf + pos, i = 0;
3518 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3519 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3520 + substring[i] = *x;
3521 + substring[i] = '\0';
3522 +
3523 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3524 + //len = strlen(mask_flags[i].mask_name);
3525 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3526 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3527 + mask_name = mask_flags[i].mask_name;
3528 + mask_bitfield = mask_flags[i].mask_bitfield;
3529 + done = 0;
3530 + break;
3531 + }
3532 + }
3533 + }
3534 +
3535 + if (mask_name != NULL) {
3536 + // pos += len;
3537 + done = 0;
3538 + switch(add) {
3539 + case '-':
3540 + rg &= ~mask_bitfield;
3541 + break;
3542 + case '+':
3543 + rg |= mask_bitfield;
3544 + break;
3545 + case '=':
3546 + rg = mask_bitfield;
3547 + break;
3548 + default:
3549 + rg |= mask_bitfield;
3550 + break;
3551 + }
3552 + }
3553 + }
3554 +
3555 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3556 +
3557 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3558 +
3559 + if (rg & YAFFS_TRACE_ALWAYS) {
3560 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3561 + char flag;
3562 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3563 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3564 + }
3565 + }
3566 +
3567 + return count;
3568 +}
3569 +
3570 +/* Stuff to handle installation of file systems */
3571 +struct file_system_to_install {
3572 + struct file_system_type *fst;
3573 + int installed;
3574 +};
3575 +
3576 +static struct file_system_to_install fs_to_install[] = {
3577 +//#ifdef CONFIG_YAFFS_YAFFS1
3578 + {&yaffs_fs_type, 0},
3579 +//#endif
3580 +//#ifdef CONFIG_YAFFS_YAFFS2
3581 + {&yaffs2_fs_type, 0},
3582 +//#endif
3583 + {NULL, 0}
3584 +};
3585 +
3586 +static int __init init_yaffs_fs(void)
3587 +{
3588 + int error = 0;
3589 + struct file_system_to_install *fsinst;
3590 +
3591 + T(YAFFS_TRACE_ALWAYS,
3592 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3593 +
3594 + /* Install the proc_fs entry */
3595 + my_proc_entry = create_proc_entry("yaffs",
3596 + S_IRUGO | S_IFREG,
3597 + &proc_root);
3598 +
3599 + if (my_proc_entry) {
3600 + my_proc_entry->write_proc = yaffs_proc_write;
3601 + my_proc_entry->read_proc = yaffs_proc_read;
3602 + my_proc_entry->data = NULL;
3603 + } else {
3604 + return -ENOMEM;
3605 + }
3606 +
3607 + /* Now add the file system entries */
3608 +
3609 + fsinst = fs_to_install;
3610 +
3611 + while (fsinst->fst && !error) {
3612 + error = register_filesystem(fsinst->fst);
3613 + if (!error) {
3614 + fsinst->installed = 1;
3615 + }
3616 + fsinst++;
3617 + }
3618 +
3619 + /* Any errors? uninstall */
3620 + if (error) {
3621 + fsinst = fs_to_install;
3622 +
3623 + while (fsinst->fst) {
3624 + if (fsinst->installed) {
3625 + unregister_filesystem(fsinst->fst);
3626 + fsinst->installed = 0;
3627 + }
3628 + fsinst++;
3629 + }
3630 + }
3631 +
3632 + return error;
3633 +}
3634 +
3635 +static void __exit exit_yaffs_fs(void)
3636 +{
3637 +
3638 + struct file_system_to_install *fsinst;
3639 +
3640 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3641 + " removing. \n"));
3642 +
3643 + remove_proc_entry("yaffs", &proc_root);
3644 +
3645 + fsinst = fs_to_install;
3646 +
3647 + while (fsinst->fst) {
3648 + if (fsinst->installed) {
3649 + unregister_filesystem(fsinst->fst);
3650 + fsinst->installed = 0;
3651 + }
3652 + fsinst++;
3653 + }
3654 +
3655 +}
3656 +
3657 +module_init(init_yaffs_fs)
3658 +module_exit(exit_yaffs_fs)
3659 +
3660 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3661 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3662 +MODULE_LICENSE("GPL");
3663 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.c linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c
3664 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3665 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c 2007-06-08 14:07:26.000000000 +0200
3666 @@ -0,0 +1,7469 @@
3667 +/*
3668 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3669 + *
3670 + * Copyright (C) 2002-2007 Aleph One Ltd.
3671 + * for Toby Churchill Ltd and Brightstar Engineering
3672 + *
3673 + * Created by Charles Manning <charles@aleph1.co.uk>
3674 + *
3675 + * This program is free software; you can redistribute it and/or modify
3676 + * it under the terms of the GNU General Public License version 2 as
3677 + * published by the Free Software Foundation.
3678 + */
3679 +
3680 +const char *yaffs_guts_c_version =
3681 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3682 +
3683 +#include "yportenv.h"
3684 +
3685 +#include "yaffsinterface.h"
3686 +#include "yaffs_guts.h"
3687 +#include "yaffs_tagsvalidity.h"
3688 +
3689 +#include "yaffs_tagscompat.h"
3690 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3691 +#include "yaffs_qsort.h"
3692 +#endif
3693 +#include "yaffs_nand.h"
3694 +
3695 +#include "yaffs_checkptrw.h"
3696 +
3697 +#include "yaffs_nand.h"
3698 +#include "yaffs_packedtags2.h"
3699 +
3700 +
3701 +#ifdef CONFIG_YAFFS_WINCE
3702 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3703 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3704 +#endif
3705 +
3706 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3707 +
3708 +#include "yaffs_ecc.h"
3709 +
3710 +
3711 +/* Robustification (if it ever comes about...) */
3712 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3713 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3714 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3715 + const __u8 * data,
3716 + const yaffs_ExtendedTags * tags);
3717 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3718 + const yaffs_ExtendedTags * tags);
3719 +
3720 +/* Other local prototypes */
3721 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3722 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3723 +
3724 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3725 +
3726 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3727 + const __u8 * buffer,
3728 + yaffs_ExtendedTags * tags,
3729 + int useReserve);
3730 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3731 + int chunkInNAND, int inScan);
3732 +
3733 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3734 + yaffs_ObjectType type);
3735 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3736 + yaffs_Object * obj);
3737 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3738 + int force, int isShrink, int shadows);
3739 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3740 +static int yaffs_CheckStructures(void);
3741 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3742 + int chunkOffset, int *limit);
3743 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3744 +
3745 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3746 +
3747 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3748 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3749 + int lineNo);
3750 +
3751 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3752 + int chunkInNAND);
3753 +
3754 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3755 +static void yaffs_DestroyObject(yaffs_Object * obj);
3756 +
3757 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3758 + int chunkInObject);
3759 +
3760 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3761 +
3762 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3763 +
3764 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3765 +
3766 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3767 +
3768 +#ifdef YAFFS_PARANOID
3769 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3770 +#else
3771 +#define yaffs_CheckFileSanity(in)
3772 +#endif
3773 +
3774 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3775 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3776 +
3777 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3778 +
3779 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3780 + yaffs_ExtendedTags * tags);
3781 +
3782 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
3783 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
3784 + yaffs_FileStructure * fStruct,
3785 + __u32 chunkId);
3786 +
3787 +
3788 +/* Function to calculate chunk and offset */
3789 +
3790 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3791 +{
3792 + if(dev->chunkShift){
3793 + /* Easy-peasy power of 2 case */
3794 + *chunk = (__u32)(addr >> dev->chunkShift);
3795 + *offset = (__u32)(addr & dev->chunkMask);
3796 + }
3797 + else if(dev->crumbsPerChunk)
3798 + {
3799 + /* Case where we're using "crumbs" */
3800 + *offset = (__u32)(addr & dev->crumbMask);
3801 + addr >>= dev->crumbShift;
3802 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3803 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3804 + }
3805 + else
3806 + YBUG();
3807 +}
3808 +
3809 +/* Function to return the number of shifts for a power of 2 greater than or equal
3810 + * to the given number
3811 + * Note we don't try to cater for all possible numbers and this does not have to
3812 + * be hellishly efficient.
3813 + */
3814 +
3815 +static __u32 ShiftsGE(__u32 x)
3816 +{
3817 + int extraBits;
3818 + int nShifts;
3819 +
3820 + nShifts = extraBits = 0;
3821 +
3822 + while(x>1){
3823 + if(x & 1) extraBits++;
3824 + x>>=1;
3825 + nShifts++;
3826 + }
3827 +
3828 + if(extraBits)
3829 + nShifts++;
3830 +
3831 + return nShifts;
3832 +}
3833 +
3834 +/* Function to return the number of shifts to get a 1 in bit 0
3835 + */
3836 +
3837 +static __u32 ShiftDiv(__u32 x)
3838 +{
3839 + int nShifts;
3840 +
3841 + nShifts = 0;
3842 +
3843 + if(!x) return 0;
3844 +
3845 + while( !(x&1)){
3846 + x>>=1;
3847 + nShifts++;
3848 + }
3849 +
3850 + return nShifts;
3851 +}
3852 +
3853 +
3854 +
3855 +/*
3856 + * Temporary buffer manipulations.
3857 + */
3858 +
3859 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3860 +{
3861 + int i;
3862 + __u8 *buf = (__u8 *)1;
3863 +
3864 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3865 +
3866 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
3867 + dev->tempBuffer[i].line = 0; /* not in use */
3868 + dev->tempBuffer[i].buffer = buf =
3869 + YMALLOC_DMA(dev->nDataBytesPerChunk);
3870 + }
3871 +
3872 + return buf ? YAFFS_OK : YAFFS_FAIL;
3873 +
3874 +}
3875 +
3876 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3877 +{
3878 + int i, j;
3879 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3880 + if (dev->tempBuffer[i].line == 0) {
3881 + dev->tempBuffer[i].line = lineNo;
3882 + if ((i + 1) > dev->maxTemp) {
3883 + dev->maxTemp = i + 1;
3884 + for (j = 0; j <= i; j++)
3885 + dev->tempBuffer[j].maxLine =
3886 + dev->tempBuffer[j].line;
3887 + }
3888 +
3889 + return dev->tempBuffer[i].buffer;
3890 + }
3891 + }
3892 +
3893 + T(YAFFS_TRACE_BUFFERS,
3894 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3895 + lineNo));
3896 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3897 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3898 + }
3899 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3900 +
3901 + /*
3902 + * If we got here then we have to allocate an unmanaged one
3903 + * This is not good.
3904 + */
3905 +
3906 + dev->unmanagedTempAllocations++;
3907 + return YMALLOC(dev->nDataBytesPerChunk);
3908 +
3909 +}
3910 +
3911 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3912 + int lineNo)
3913 +{
3914 + int i;
3915 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3916 + if (dev->tempBuffer[i].buffer == buffer) {
3917 + dev->tempBuffer[i].line = 0;
3918 + return;
3919 + }
3920 + }
3921 +
3922 + if (buffer) {
3923 + /* assume it is an unmanaged one. */
3924 + T(YAFFS_TRACE_BUFFERS,
3925 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3926 + lineNo));
3927 + YFREE(buffer);
3928 + dev->unmanagedTempDeallocations++;
3929 + }
3930 +
3931 +}
3932 +
3933 +/*
3934 + * Determine if we have a managed buffer.
3935 + */
3936 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3937 +{
3938 + int i;
3939 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3940 + if (dev->tempBuffer[i].buffer == buffer)
3941 + return 1;
3942 +
3943 + }
3944 +
3945 + for (i = 0; i < dev->nShortOpCaches; i++) {
3946 + if( dev->srCache[i].data == buffer )
3947 + return 1;
3948 +
3949 + }
3950 +
3951 + if (buffer == dev->checkpointBuffer)
3952 + return 1;
3953 +
3954 + T(YAFFS_TRACE_ALWAYS,
3955 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3956 + return 0;
3957 +}
3958 +
3959 +
3960 +
3961 +/*
3962 + * Chunk bitmap manipulations
3963 + */
3964 +
3965 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3966 +{
3967 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3968 + T(YAFFS_TRACE_ERROR,
3969 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3970 + blk));
3971 + YBUG();
3972 + }
3973 + return dev->chunkBits +
3974 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3975 +}
3976 +
3977 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3978 +{
3979 + if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
3980 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
3981 + T(YAFFS_TRACE_ERROR,
3982 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
3983 + YBUG();
3984 + }
3985 +}
3986 +
3987 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3988 +{
3989 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3990 +
3991 + memset(blkBits, 0, dev->chunkBitmapStride);
3992 +}
3993 +
3994 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3995 +{
3996 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3997 +
3998 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3999 +
4000 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
4001 +}
4002 +
4003 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
4004 +{
4005 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4006 +
4007 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4008 +
4009 + blkBits[chunk / 8] |= (1 << (chunk & 7));
4010 +}
4011 +
4012 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
4013 +{
4014 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4015 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4016 +
4017 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4018 +}
4019 +
4020 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4021 +{
4022 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4023 + int i;
4024 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4025 + if (*blkBits)
4026 + return 1;
4027 + blkBits++;
4028 + }
4029 + return 0;
4030 +}
4031 +
4032 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4033 +{
4034 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4035 + int i;
4036 + int n = 0;
4037 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4038 + __u8 x = *blkBits;
4039 + while(x){
4040 + if(x & 1)
4041 + n++;
4042 + x >>=1;
4043 + }
4044 +
4045 + blkBits++;
4046 + }
4047 + return n;
4048 +}
4049 +
4050 +/*
4051 + * Verification code
4052 + */
4053 +
4054 +static int yaffs_SkipVerification(yaffs_Device *dev)
4055 +{
4056 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4057 +}
4058 +
4059 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4060 +{
4061 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4062 +}
4063 +
4064 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4065 +{
4066 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4067 +}
4068 +
4069 +static const char * blockStateName[] = {
4070 +"Unknown",
4071 +"Needs scanning",
4072 +"Scanning",
4073 +"Empty",
4074 +"Allocating",
4075 +"Full",
4076 +"Dirty",
4077 +"Checkpoint",
4078 +"Collecting",
4079 +"Dead"
4080 +};
4081 +
4082 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4083 +{
4084 + int actuallyUsed;
4085 + int inUse;
4086 +
4087 + if(yaffs_SkipVerification(dev))
4088 + return;
4089 +
4090 + /* Report illegal runtime states */
4091 + if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4092 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
4093 +
4094 + switch(bi->blockState){
4095 + case YAFFS_BLOCK_STATE_UNKNOWN:
4096 + case YAFFS_BLOCK_STATE_SCANNING:
4097 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4098 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
4099 + n,blockStateName[bi->blockState]));
4100 + }
4101 +
4102 + /* Check pages in use and soft deletions are legal */
4103 +
4104 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4105 +
4106 + if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4107 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4108 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4109 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4110 + n,bi->pagesInUse,bi->softDeletions));
4111 +
4112 +
4113 + /* Check chunk bitmap legal */
4114 + inUse = yaffs_CountChunkBits(dev,n);
4115 + if(inUse != bi->pagesInUse)
4116 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4117 + n,bi->pagesInUse,inUse));
4118 +
4119 + /* Check that the sequence number is valid.
4120 + * Ten million is legal, but is very unlikely
4121 + */
4122 + if(dev->isYaffs2 &&
4123 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4124 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
4125 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4126 + n,bi->sequenceNumber));
4127 +
4128 +}
4129 +
4130 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4131 +{
4132 + yaffs_VerifyBlock(dev,bi,n);
4133 +
4134 + /* After collection the block should be in the erased state */
4135 + /* TODO: This will need to change if we do partial gc */
4136 +
4137 + if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
4138 + T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4139 + n,bi->blockState));
4140 + }
4141 +}
4142 +
4143 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4144 +{
4145 + int i;
4146 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4147 + int nIllegalBlockStates = 0;
4148 +
4149 +
4150 + if(yaffs_SkipVerification(dev))
4151 + return;
4152 +
4153 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4154 +
4155 +
4156 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4157 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4158 + yaffs_VerifyBlock(dev,bi,i);
4159 +
4160 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4161 + nBlocksPerState[bi->blockState]++;
4162 + else
4163 + nIllegalBlockStates++;
4164 +
4165 + }
4166 +
4167 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4168 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4169 +
4170 + T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
4171 + if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4172 + T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
4173 +
4174 + for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4175 + T(YAFFS_TRACE_VERIFY,
4176 + (TSTR("%s %d blocks"TENDSTR),
4177 + blockStateName[i],nBlocksPerState[i]));
4178 +
4179 + if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4180 + T(YAFFS_TRACE_VERIFY,
4181 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4182 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4183 +
4184 + if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4185 + T(YAFFS_TRACE_VERIFY,
4186 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4187 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4188 +
4189 + if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4190 + T(YAFFS_TRACE_VERIFY,
4191 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4192 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4193 +
4194 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4195 +
4196 +}
4197 +
4198 +/*
4199 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4200 + * case those tests will not be performed.
4201 + */
4202 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4203 +{
4204 + if(yaffs_SkipVerification(obj->myDev))
4205 + return;
4206 +
4207 + if(!(tags && obj && oh)){
4208 + T(YAFFS_TRACE_VERIFY,
4209 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4210 + (__u32)tags,(__u32)obj,(__u32)oh));
4211 + return;
4212 + }
4213 +
4214 + if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4215 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4216 + T(YAFFS_TRACE_VERIFY,
4217 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4218 + tags->objectId, oh->type));
4219 +
4220 + if(tags->objectId != obj->objectId)
4221 + T(YAFFS_TRACE_VERIFY,
4222 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4223 + tags->objectId, obj->objectId));
4224 +
4225 +
4226 + /*
4227 + * Check that the object's parent ids match if parentCheck requested.
4228 + *
4229 + * Tests do not apply to the root object.
4230 + */
4231 +
4232 + if(parentCheck && tags->objectId > 1 && !obj->parent)
4233 + T(YAFFS_TRACE_VERIFY,
4234 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4235 + tags->objectId, oh->parentObjectId));
4236 +
4237 +
4238 + if(parentCheck && obj->parent &&
4239 + oh->parentObjectId != obj->parent->objectId &&
4240 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4241 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4242 + T(YAFFS_TRACE_VERIFY,
4243 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4244 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4245 +
4246 +
4247 + if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4248 + T(YAFFS_TRACE_VERIFY,
4249 + (TSTR("Obj %d header name is NULL"TENDSTR),
4250 + obj->objectId));
4251 +
4252 + if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4253 + T(YAFFS_TRACE_VERIFY,
4254 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4255 + obj->objectId));
4256 +}
4257 +
4258 +
4259 +
4260 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4261 + __u32 level, int chunkOffset)
4262 +{
4263 + int i;
4264 + yaffs_Device *dev = obj->myDev;
4265 + int ok = 1;
4266 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4267 +
4268 + if (tn) {
4269 + if (level > 0) {
4270 +
4271 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4272 + if (tn->internal[i]) {
4273 + ok = yaffs_VerifyTnodeWorker(obj,
4274 + tn->internal[i],
4275 + level - 1,
4276 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4277 + }
4278 + }
4279 + } else if (level == 0) {
4280 + int i;
4281 + yaffs_ExtendedTags tags;
4282 + __u32 objectId = obj->objectId;
4283 +
4284 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4285 +
4286 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4287 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4288 +
4289 + if(theChunk > 0){
4290 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4291 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4292 + if(tags.objectId != objectId || tags.chunkId != chunkOffset){
4293 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4294 + objectId, chunkOffset, theChunk,
4295 + tags.objectId, tags.chunkId));
4296 + }
4297 + }
4298 + chunkOffset++;
4299 + }
4300 + }
4301 + }
4302 +
4303 + return ok;
4304 +
4305 +}
4306 +
4307 +
4308 +static void yaffs_VerifyFile(yaffs_Object *obj)
4309 +{
4310 + int requiredTallness;
4311 + int actualTallness;
4312 + __u32 lastChunk;
4313 + __u32 x;
4314 + __u32 i;
4315 + int ok;
4316 + yaffs_Device *dev;
4317 + yaffs_ExtendedTags tags;
4318 + yaffs_Tnode *tn;
4319 + __u32 objectId;
4320 +
4321 + if(obj && yaffs_SkipVerification(obj->myDev))
4322 + return;
4323 +
4324 + dev = obj->myDev;
4325 + objectId = obj->objectId;
4326 +
4327 + /* Check file size is consistent with tnode depth */
4328 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4329 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4330 + requiredTallness = 0;
4331 + while (x> 0) {
4332 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4333 + requiredTallness++;
4334 + }
4335 +
4336 + actualTallness = obj->variant.fileVariant.topLevel;
4337 +
4338 + if(requiredTallness > actualTallness )
4339 + T(YAFFS_TRACE_VERIFY,
4340 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4341 + obj->objectId,actualTallness, requiredTallness));
4342 +
4343 +
4344 + /* Check that the chunks in the tnode tree are all correct.
4345 + * We do this by scanning through the tnode tree and
4346 + * checking the tags for every chunk match.
4347 + */
4348 +
4349 + if(yaffs_SkipNANDVerification(dev))
4350 + return;
4351 +
4352 + for(i = 1; i <= lastChunk; i++){
4353 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
4354 +
4355 + if (tn) {
4356 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4357 + if(theChunk > 0){
4358 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4359 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4360 + if(tags.objectId != objectId || tags.chunkId != i){
4361 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4362 + objectId, i, theChunk,
4363 + tags.objectId, tags.chunkId));
4364 + }
4365 + }
4366 + }
4367 +
4368 + }
4369 +
4370 +}
4371 +
4372 +static void yaffs_VerifyDirectory(yaffs_Object *obj)
4373 +{
4374 + if(obj && yaffs_SkipVerification(obj->myDev))
4375 + return;
4376 +
4377 +}
4378 +
4379 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4380 +{
4381 + if(obj && yaffs_SkipVerification(obj->myDev))
4382 + return;
4383 +
4384 + /* Verify sane equivalent object */
4385 +}
4386 +
4387 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4388 +{
4389 + if(obj && yaffs_SkipVerification(obj->myDev))
4390 + return;
4391 +
4392 + /* Verify symlink string */
4393 +}
4394 +
4395 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4396 +{
4397 + if(obj && yaffs_SkipVerification(obj->myDev))
4398 + return;
4399 +}
4400 +
4401 +static void yaffs_VerifyObject(yaffs_Object *obj)
4402 +{
4403 + yaffs_Device *dev;
4404 +
4405 + __u32 chunkMin;
4406 + __u32 chunkMax;
4407 +
4408 + __u32 chunkIdOk;
4409 + __u32 chunkIsLive;
4410 +
4411 + if(!obj)
4412 + return;
4413 +
4414 + dev = obj->myDev;
4415 +
4416 + if(yaffs_SkipVerification(dev))
4417 + return;
4418 +
4419 + /* Check sane object header chunk */
4420 +
4421 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4422 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4423 +
4424 + chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
4425 + chunkIsLive = chunkIdOk &&
4426 + yaffs_CheckChunkBit(dev,
4427 + obj->chunkId / dev->nChunksPerBlock,
4428 + obj->chunkId % dev->nChunksPerBlock);
4429 + if(!obj->fake &&
4430 + (!chunkIdOk || !chunkIsLive)) {
4431 + T(YAFFS_TRACE_VERIFY,
4432 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4433 + obj->objectId,obj->chunkId,
4434 + chunkIdOk ? "" : ",out of range",
4435 + chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
4436 + }
4437 +
4438 + if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
4439 + yaffs_ExtendedTags tags;
4440 + yaffs_ObjectHeader *oh;
4441 + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
4442 +
4443 + oh = (yaffs_ObjectHeader *)buffer;
4444 +
4445 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
4446 +
4447 + yaffs_VerifyObjectHeader(obj,oh,&tags,1);
4448 +
4449 + yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
4450 + }
4451 +
4452 + /* Verify it has a parent */
4453 + if(obj && !obj->fake &&
4454 + (!obj->parent || obj->parent->myDev != dev)){
4455 + T(YAFFS_TRACE_VERIFY,
4456 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4457 + obj->objectId,obj->parent));
4458 + }
4459 +
4460 + /* Verify parent is a directory */
4461 + if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
4462 + T(YAFFS_TRACE_VERIFY,
4463 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4464 + obj->objectId,obj->parent->variantType));
4465 + }
4466 +
4467 + switch(obj->variantType){
4468 + case YAFFS_OBJECT_TYPE_FILE:
4469 + yaffs_VerifyFile(obj);
4470 + break;
4471 + case YAFFS_OBJECT_TYPE_SYMLINK:
4472 + yaffs_VerifySymlink(obj);
4473 + break;
4474 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4475 + yaffs_VerifyDirectory(obj);
4476 + break;
4477 + case YAFFS_OBJECT_TYPE_HARDLINK:
4478 + yaffs_VerifyHardLink(obj);
4479 + break;
4480 + case YAFFS_OBJECT_TYPE_SPECIAL:
4481 + yaffs_VerifySpecial(obj);
4482 + break;
4483 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4484 + default:
4485 + T(YAFFS_TRACE_VERIFY,
4486 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4487 + obj->objectId,obj->variantType));
4488 + break;
4489 + }
4490 +
4491 +
4492 +}
4493 +
4494 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4495 +{
4496 + yaffs_Object *obj;
4497 + int i;
4498 + struct list_head *lh;
4499 +
4500 + if(yaffs_SkipVerification(dev))
4501 + return;
4502 +
4503 + /* Iterate through the objects in each hash entry */
4504 +
4505 + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
4506 + list_for_each(lh, &dev->objectBucket[i].list) {
4507 + if (lh) {
4508 + obj = list_entry(lh, yaffs_Object, hashLink);
4509 + yaffs_VerifyObject(obj);
4510 + }
4511 + }
4512 + }
4513 +
4514 +}
4515 +
4516 +
4517 +/*
4518 + * Simple hash function. Needs to have a reasonable spread
4519 + */
4520 +
4521 +static Y_INLINE int yaffs_HashFunction(int n)
4522 +{
4523 + n = abs(n);
4524 + return (n % YAFFS_NOBJECT_BUCKETS);
4525 +}
4526 +
4527 +/*
4528 + * Access functions to useful fake objects
4529 + */
4530 +
4531 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
4532 +{
4533 + return dev->rootDir;
4534 +}
4535 +
4536 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
4537 +{
4538 + return dev->lostNFoundDir;
4539 +}
4540 +
4541 +
4542 +/*
4543 + * Erased NAND checking functions
4544 + */
4545 +
4546 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
4547 +{
4548 + /* Horrible, slow implementation */
4549 + while (nBytes--) {
4550 + if (*buffer != 0xFF)
4551 + return 0;
4552 + buffer++;
4553 + }
4554 + return 1;
4555 +}
4556 +
4557 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4558 + int chunkInNAND)
4559 +{
4560 +
4561 + int retval = YAFFS_OK;
4562 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4563 + yaffs_ExtendedTags tags;
4564 + int result;
4565 +
4566 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4567 +
4568 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4569 + retval = YAFFS_FAIL;
4570 +
4571 +
4572 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4573 + T(YAFFS_TRACE_NANDACCESS,
4574 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4575 + retval = YAFFS_FAIL;
4576 + }
4577 +
4578 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4579 +
4580 + return retval;
4581 +
4582 +}
4583 +
4584 +
4585 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4586 + const __u8 * data,
4587 + yaffs_ExtendedTags * tags,
4588 + int useReserve)
4589 +{
4590 + int attempts = 0;
4591 + int writeOk = 0;
4592 + int chunk;
4593 +
4594 + yaffs_InvalidateCheckpoint(dev);
4595 +
4596 + do {
4597 + yaffs_BlockInfo *bi = 0;
4598 + int erasedOk = 0;
4599 +
4600 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4601 + if (chunk < 0) {
4602 + /* no space */
4603 + break;
4604 + }
4605 +
4606 + /* First check this chunk is erased, if it needs
4607 + * checking. The checking policy (unless forced
4608 + * always on) is as follows:
4609 + *
4610 + * Check the first page we try to write in a block.
4611 + * If the check passes then we don't need to check any
4612 + * more. If the check fails, we check again...
4613 + * If the block has been erased, we don't need to check.
4614 + *
4615 + * However, if the block has been prioritised for gc,
4616 + * then we think there might be something odd about
4617 + * this block and stop using it.
4618 + *
4619 + * Rationale: We should only ever see chunks that have
4620 + * not been erased if there was a partially written
4621 + * chunk due to power loss. This checking policy should
4622 + * catch that case with very few checks and thus save a
4623 + * lot of checks that are most likely not needed.
4624 + */
4625 + if (bi->gcPrioritise) {
4626 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4627 + /* try another chunk */
4628 + continue;
4629 + }
4630 +
4631 + /* let's give it a try */
4632 + attempts++;
4633 +
4634 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4635 + bi->skipErasedCheck = 0;
4636 +#endif
4637 + if (!bi->skipErasedCheck) {
4638 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4639 + if (erasedOk != YAFFS_OK) {
4640 + T(YAFFS_TRACE_ERROR,
4641 + (TSTR ("**>> yaffs chunk %d was not erased"
4642 + TENDSTR), chunk));
4643 +
4644 + /* try another chunk */
4645 + continue;
4646 + }
4647 + bi->skipErasedCheck = 1;
4648 + }
4649 +
4650 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4651 + data, tags);
4652 + if (writeOk != YAFFS_OK) {
4653 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4654 + /* try another chunk */
4655 + continue;
4656 + }
4657 +
4658 + /* Copy the data into the robustification buffer */
4659 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4660 +
4661 + } while (writeOk != YAFFS_OK && attempts < yaffs_wr_attempts);
4662 +
4663 + if (attempts > 1) {
4664 + T(YAFFS_TRACE_ERROR,
4665 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4666 + attempts));
4667 +
4668 + dev->nRetriedWrites += (attempts - 1);
4669 + }
4670 +
4671 + return chunk;
4672 +}
4673 +
4674 +/*
4675 + * Block retiring for handling a broken block.
4676 + */
4677 +
4678 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
4679 +{
4680 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4681 +
4682 + yaffs_InvalidateCheckpoint(dev);
4683 +
4684 + yaffs_MarkBlockBad(dev, blockInNAND);
4685 +
4686 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
4687 + bi->gcPrioritise = 0;
4688 + bi->needsRetiring = 0;
4689 +
4690 + dev->nRetiredBlocks++;
4691 +}
4692 +
4693 +/*
4694 + * Functions for robustisizing TODO
4695 + *
4696 + */
4697 +
4698 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
4699 + const __u8 * data,
4700 + const yaffs_ExtendedTags * tags)
4701 +{
4702 +}
4703 +
4704 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
4705 + const yaffs_ExtendedTags * tags)
4706 +{
4707 +}
4708 +
4709 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
4710 +{
4711 + if(!bi->gcPrioritise){
4712 + bi->gcPrioritise = 1;
4713 + dev->hasPendingPrioritisedGCs = 1;
4714 + bi->chunkErrorStrikes ++;
4715 +
4716 + if(bi->chunkErrorStrikes > 3){
4717 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
4718 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
4719 +
4720 + }
4721 +
4722 + }
4723 +}
4724 +
4725 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
4726 +{
4727 +
4728 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
4729 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4730 +
4731 + yaffs_HandleChunkError(dev,bi);
4732 +
4733 +
4734 + if(erasedOk ) {
4735 + /* Was an actual write failure, so mark the block for retirement */
4736 + bi->needsRetiring = 1;
4737 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4738 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
4739 +
4740 +
4741 + }
4742 +
4743 + /* Delete the chunk */
4744 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
4745 +}
4746 +
4747 +
4748 +/*---------------- Name handling functions ------------*/
4749 +
4750 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
4751 +{
4752 + __u16 sum = 0;
4753 + __u16 i = 1;
4754 +
4755 + YUCHAR *bname = (YUCHAR *) name;
4756 + if (bname) {
4757 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
4758 +
4759 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4760 + sum += yaffs_toupper(*bname) * i;
4761 +#else
4762 + sum += (*bname) * i;
4763 +#endif
4764 + i++;
4765 + bname++;
4766 + }
4767 + }
4768 + return sum;
4769 +}
4770 +
4771 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4772 +{
4773 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4774 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4775 + yaffs_strcpy(obj->shortName, name);
4776 + } else {
4777 + obj->shortName[0] = _Y('\0');
4778 + }
4779 +#endif
4780 + obj->sum = yaffs_CalcNameSum(name);
4781 +}
4782 +
4783 +/*-------------------- TNODES -------------------
4784 +
4785 + * List of spare tnodes
4786 + * The list is hooked together using the first pointer
4787 + * in the tnode.
4788 + */
4789 +
4790 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4791 + * adds them to the tnode free list.
4792 + * Don't use this function directly
4793 + */
4794 +
4795 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4796 +{
4797 + int i;
4798 + int tnodeSize;
4799 + yaffs_Tnode *newTnodes;
4800 + __u8 *mem;
4801 + yaffs_Tnode *curr;
4802 + yaffs_Tnode *next;
4803 + yaffs_TnodeList *tnl;
4804 +
4805 + if (nTnodes < 1)
4806 + return YAFFS_OK;
4807 +
4808 + /* Calculate the tnode size in bytes for variable width tnode support.
4809 + * Must be a multiple of 32-bits */
4810 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4811 +
4812 + /* make these things */
4813 +
4814 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4815 + mem = (__u8 *)newTnodes;
4816 +
4817 + if (!newTnodes) {
4818 + T(YAFFS_TRACE_ERROR,
4819 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4820 + return YAFFS_FAIL;
4821 + }
4822 +
4823 + /* Hook them into the free list */
4824 +#if 0
4825 + for (i = 0; i < nTnodes - 1; i++) {
4826 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4827 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4828 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4829 +#endif
4830 + }
4831 +
4832 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4833 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4834 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4835 +#endif
4836 + dev->freeTnodes = newTnodes;
4837 +#else
4838 + /* New hookup for wide tnodes */
4839 + for(i = 0; i < nTnodes -1; i++) {
4840 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4841 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4842 + curr->internal[0] = next;
4843 + }
4844 +
4845 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4846 + curr->internal[0] = dev->freeTnodes;
4847 + dev->freeTnodes = (yaffs_Tnode *)mem;
4848 +
4849 +#endif
4850 +
4851 +
4852 + dev->nFreeTnodes += nTnodes;
4853 + dev->nTnodesCreated += nTnodes;
4854 +
4855 + /* Now add this bunch of tnodes to a list for freeing up.
4856 + * NB If we can't add this to the management list it isn't fatal
4857 + * but it just means we can't free this bunch of tnodes later.
4858 + */
4859 +
4860 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4861 + if (!tnl) {
4862 + T(YAFFS_TRACE_ERROR,
4863 + (TSTR
4864 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4865 + return YAFFS_FAIL;
4866 +
4867 + } else {
4868 + tnl->tnodes = newTnodes;
4869 + tnl->next = dev->allocatedTnodeList;
4870 + dev->allocatedTnodeList = tnl;
4871 + }
4872 +
4873 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4874 +
4875 + return YAFFS_OK;
4876 +}
4877 +
4878 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4879 +
4880 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4881 +{
4882 + yaffs_Tnode *tn = NULL;
4883 +
4884 + /* If there are none left make more */
4885 + if (!dev->freeTnodes) {
4886 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4887 + }
4888 +
4889 + if (dev->freeTnodes) {
4890 + tn = dev->freeTnodes;
4891 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4892 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4893 + /* Hoosterman, this thing looks like it isn't in the list */
4894 + T(YAFFS_TRACE_ALWAYS,
4895 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4896 + }
4897 +#endif
4898 + dev->freeTnodes = dev->freeTnodes->internal[0];
4899 + dev->nFreeTnodes--;
4900 + }
4901 +
4902 + return tn;
4903 +}
4904 +
4905 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4906 +{
4907 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4908 +
4909 + if(tn)
4910 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4911 +
4912 + return tn;
4913 +}
4914 +
4915 +/* FreeTnode frees up a tnode and puts it back on the free list */
4916 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4917 +{
4918 + if (tn) {
4919 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4920 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4921 + /* Hoosterman, this thing looks like it is already in the list */
4922 + T(YAFFS_TRACE_ALWAYS,
4923 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4924 + }
4925 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4926 +#endif
4927 + tn->internal[0] = dev->freeTnodes;
4928 + dev->freeTnodes = tn;
4929 + dev->nFreeTnodes++;
4930 + }
4931 +}
4932 +
4933 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4934 +{
4935 + /* Free the list of allocated tnodes */
4936 + yaffs_TnodeList *tmp;
4937 +
4938 + while (dev->allocatedTnodeList) {
4939 + tmp = dev->allocatedTnodeList->next;
4940 +
4941 + YFREE(dev->allocatedTnodeList->tnodes);
4942 + YFREE(dev->allocatedTnodeList);
4943 + dev->allocatedTnodeList = tmp;
4944 +
4945 + }
4946 +
4947 + dev->freeTnodes = NULL;
4948 + dev->nFreeTnodes = 0;
4949 +}
4950 +
4951 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4952 +{
4953 + dev->allocatedTnodeList = NULL;
4954 + dev->freeTnodes = NULL;
4955 + dev->nFreeTnodes = 0;
4956 + dev->nTnodesCreated = 0;
4957 +
4958 +}
4959 +
4960 +
4961 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4962 +{
4963 + __u32 *map = (__u32 *)tn;
4964 + __u32 bitInMap;
4965 + __u32 bitInWord;
4966 + __u32 wordInMap;
4967 + __u32 mask;
4968 +
4969 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4970 + val >>= dev->chunkGroupBits;
4971 +
4972 + bitInMap = pos * dev->tnodeWidth;
4973 + wordInMap = bitInMap /32;
4974 + bitInWord = bitInMap & (32 -1);
4975 +
4976 + mask = dev->tnodeMask << bitInWord;
4977 +
4978 + map[wordInMap] &= ~mask;
4979 + map[wordInMap] |= (mask & (val << bitInWord));
4980 +
4981 + if(dev->tnodeWidth > (32-bitInWord)) {
4982 + bitInWord = (32 - bitInWord);
4983 + wordInMap++;;
4984 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4985 + map[wordInMap] &= ~mask;
4986 + map[wordInMap] |= (mask & (val >> bitInWord));
4987 + }
4988 +}
4989 +
4990 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
4991 +{
4992 + __u32 *map = (__u32 *)tn;
4993 + __u32 bitInMap;
4994 + __u32 bitInWord;
4995 + __u32 wordInMap;
4996 + __u32 val;
4997 +
4998 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4999 +
5000 + bitInMap = pos * dev->tnodeWidth;
5001 + wordInMap = bitInMap /32;
5002 + bitInWord = bitInMap & (32 -1);
5003 +
5004 + val = map[wordInMap] >> bitInWord;
5005 +
5006 + if(dev->tnodeWidth > (32-bitInWord)) {
5007 + bitInWord = (32 - bitInWord);
5008 + wordInMap++;;
5009 + val |= (map[wordInMap] << bitInWord);
5010 + }
5011 +
5012 + val &= dev->tnodeMask;
5013 + val <<= dev->chunkGroupBits;
5014 +
5015 + return val;
5016 +}
5017 +
5018 +/* ------------------- End of individual tnode manipulation -----------------*/
5019 +
5020 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5021 + * The look up tree is represented by the top tnode and the number of topLevel
5022 + * in the tree. 0 means only the level 0 tnode is in the tree.
5023 + */
5024 +
5025 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5026 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
5027 + yaffs_FileStructure * fStruct,
5028 + __u32 chunkId)
5029 +{
5030 +
5031 + yaffs_Tnode *tn = fStruct->top;
5032 + __u32 i;
5033 + int requiredTallness;
5034 + int level = fStruct->topLevel;
5035 +
5036 + /* Check sane level and chunk Id */
5037 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
5038 + return NULL;
5039 + }
5040 +
5041 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5042 + return NULL;
5043 + }
5044 +
5045 + /* First check we're tall enough (ie enough topLevel) */
5046 +
5047 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5048 + requiredTallness = 0;
5049 + while (i) {
5050 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5051 + requiredTallness++;
5052 + }
5053 +
5054 + if (requiredTallness > fStruct->topLevel) {
5055 + /* Not tall enough, so we can't find it, return NULL. */
5056 + return NULL;
5057 + }
5058 +
5059 + /* Traverse down to level 0 */
5060 + while (level > 0 && tn) {
5061 + tn = tn->
5062 + internal[(chunkId >>
5063 + ( YAFFS_TNODES_LEVEL0_BITS +
5064 + (level - 1) *
5065 + YAFFS_TNODES_INTERNAL_BITS)
5066 + ) &
5067 + YAFFS_TNODES_INTERNAL_MASK];
5068 + level--;
5069 +
5070 + }
5071 +
5072 + return tn;
5073 +}
5074 +
5075 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5076 + * This happens in two steps:
5077 + * 1. If the tree isn't tall enough, then make it taller.
5078 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5079 + *
5080 + * Used when modifying the tree.
5081 + *
5082 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5083 + * be plugged into the ttree.
5084 + */
5085 +
5086 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
5087 + yaffs_FileStructure * fStruct,
5088 + __u32 chunkId,
5089 + yaffs_Tnode *passedTn)
5090 +{
5091 +
5092 + int requiredTallness;
5093 + int i;
5094 + int l;
5095 + yaffs_Tnode *tn;
5096 +
5097 + __u32 x;
5098 +
5099 +
5100 + /* Check sane level and page Id */
5101 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
5102 + return NULL;
5103 + }
5104 +
5105 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5106 + return NULL;
5107 + }
5108 +
5109 + /* First check we're tall enough (ie enough topLevel) */
5110 +
5111 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5112 + requiredTallness = 0;
5113 + while (x) {
5114 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5115 + requiredTallness++;
5116 + }
5117 +
5118 +
5119 + if (requiredTallness > fStruct->topLevel) {
5120 + /* Not tall enough,gotta make the tree taller */
5121 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5122 +
5123 + tn = yaffs_GetTnode(dev);
5124 +
5125 + if (tn) {
5126 + tn->internal[0] = fStruct->top;
5127 + fStruct->top = tn;
5128 + } else {
5129 + T(YAFFS_TRACE_ERROR,
5130 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5131 + }
5132 + }
5133 +
5134 + fStruct->topLevel = requiredTallness;
5135 + }
5136 +
5137 + /* Traverse down to level 0, adding anything we need */
5138 +
5139 + l = fStruct->topLevel;
5140 + tn = fStruct->top;
5141 +
5142 + if(l > 0) {
5143 + while (l > 0 && tn) {
5144 + x = (chunkId >>
5145 + ( YAFFS_TNODES_LEVEL0_BITS +
5146 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5147 + YAFFS_TNODES_INTERNAL_MASK;
5148 +
5149 +
5150 + if((l>1) && !tn->internal[x]){
5151 + /* Add missing non-level-zero tnode */
5152 + tn->internal[x] = yaffs_GetTnode(dev);
5153 +
5154 + } else if(l == 1) {
5155 + /* Looking from level 1 at level 0 */
5156 + if (passedTn) {
5157 + /* If we already have one, then release it.*/
5158 + if(tn->internal[x])
5159 + yaffs_FreeTnode(dev,tn->internal[x]);
5160 + tn->internal[x] = passedTn;
5161 +
5162 + } else if(!tn->internal[x]) {
5163 + /* Don't have one, none passed in */
5164 + tn->internal[x] = yaffs_GetTnode(dev);
5165 + }
5166 + }
5167 +
5168 + tn = tn->internal[x];
5169 + l--;
5170 + }
5171 + } else {
5172 + /* top is level 0 */
5173 + if(passedTn) {
5174 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5175 + yaffs_FreeTnode(dev,passedTn);
5176 + }
5177 + }
5178 +
5179 + return tn;
5180 +}
5181 +
5182 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
5183 + yaffs_ExtendedTags * tags, int objectId,
5184 + int chunkInInode)
5185 +{
5186 + int j;
5187 +
5188 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5189 + if (yaffs_CheckChunkBit
5190 + (dev, theChunk / dev->nChunksPerBlock,
5191 + theChunk % dev->nChunksPerBlock)) {
5192 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5193 + tags);
5194 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5195 + /* found it; */
5196 + return theChunk;
5197 +
5198 + }
5199 + }
5200 + theChunk++;
5201 + }
5202 + return -1;
5203 +}
5204 +
5205 +
5206 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5207 + * chunks and tnodes in the file
5208 + * Returns 1 if the tree was deleted.
5209 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5210 + */
5211 +
5212 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
5213 + int chunkOffset, int *limit)
5214 +{
5215 + int i;
5216 + int chunkInInode;
5217 + int theChunk;
5218 + yaffs_ExtendedTags tags;
5219 + int foundChunk;
5220 + yaffs_Device *dev = in->myDev;
5221 +
5222 + int allDone = 1;
5223 +
5224 + if (tn) {
5225 + if (level > 0) {
5226 +
5227 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5228 + i--) {
5229 + if (tn->internal[i]) {
5230 + if (limit && (*limit) < 0) {
5231 + allDone = 0;
5232 + } else {
5233 + allDone =
5234 + yaffs_DeleteWorker(in,
5235 + tn->
5236 + internal
5237 + [i],
5238 + level -
5239 + 1,
5240 + (chunkOffset
5241 + <<
5242 + YAFFS_TNODES_INTERNAL_BITS)
5243 + + i,
5244 + limit);
5245 + }
5246 + if (allDone) {
5247 + yaffs_FreeTnode(dev,
5248 + tn->
5249 + internal[i]);
5250 + tn->internal[i] = NULL;
5251 + }
5252 + }
5253 +
5254 + }
5255 + return (allDone) ? 1 : 0;
5256 + } else if (level == 0) {
5257 + int hitLimit = 0;
5258 +
5259 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5260 + i--) {
5261 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5262 + if (theChunk) {
5263 +
5264 + chunkInInode =
5265 + (chunkOffset <<
5266 + YAFFS_TNODES_LEVEL0_BITS) + i;
5267 +
5268 + foundChunk =
5269 + yaffs_FindChunkInGroup(dev,
5270 + theChunk,
5271 + &tags,
5272 + in->objectId,
5273 + chunkInInode);
5274 +
5275 + if (foundChunk > 0) {
5276 + yaffs_DeleteChunk(dev,
5277 + foundChunk, 1,
5278 + __LINE__);
5279 + in->nDataChunks--;
5280 + if (limit) {
5281 + *limit = *limit - 1;
5282 + if (*limit <= 0) {
5283 + hitLimit = 1;
5284 + }
5285 + }
5286 +
5287 + }
5288 +
5289 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5290 + }
5291 +
5292 + }
5293 + return (i < 0) ? 1 : 0;
5294 +
5295 + }
5296 +
5297 + }
5298 +
5299 + return 1;
5300 +
5301 +}
5302 +
5303 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
5304 +{
5305 +
5306 + yaffs_BlockInfo *theBlock;
5307 +
5308 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5309 +
5310 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5311 + if (theBlock) {
5312 + theBlock->softDeletions++;
5313 + dev->nFreeChunks++;
5314 + }
5315 +}
5316 +
5317 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5318 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5319 + * of the tnode.
5320 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5321 + */
5322 +
5323 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
5324 + __u32 level, int chunkOffset)
5325 +{
5326 + int i;
5327 + int theChunk;
5328 + int allDone = 1;
5329 + yaffs_Device *dev = in->myDev;
5330 +
5331 + if (tn) {
5332 + if (level > 0) {
5333 +
5334 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5335 + i--) {
5336 + if (tn->internal[i]) {
5337 + allDone =
5338 + yaffs_SoftDeleteWorker(in,
5339 + tn->
5340 + internal[i],
5341 + level - 1,
5342 + (chunkOffset
5343 + <<
5344 + YAFFS_TNODES_INTERNAL_BITS)
5345 + + i);
5346 + if (allDone) {
5347 + yaffs_FreeTnode(dev,
5348 + tn->
5349 + internal[i]);
5350 + tn->internal[i] = NULL;
5351 + } else {
5352 + /* Hoosterman... how could this happen? */
5353 + }
5354 + }
5355 + }
5356 + return (allDone) ? 1 : 0;
5357 + } else if (level == 0) {
5358 +
5359 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5360 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5361 + if (theChunk) {
5362 + /* Note this does not find the real chunk, only the chunk group.
5363 + * We make an assumption that a chunk group is not larger than
5364 + * a block.
5365 + */
5366 + yaffs_SoftDeleteChunk(dev, theChunk);
5367 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5368 + }
5369 +
5370 + }
5371 + return 1;
5372 +
5373 + }
5374 +
5375 + }
5376 +
5377 + return 1;
5378 +
5379 +}
5380 +
5381 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
5382 +{
5383 + if (obj->deleted &&
5384 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5385 + if (obj->nDataChunks <= 0) {
5386 + /* Empty file with no duplicate object headers, just delete it immediately */
5387 + yaffs_FreeTnode(obj->myDev,
5388 + obj->variant.fileVariant.top);
5389 + obj->variant.fileVariant.top = NULL;
5390 + T(YAFFS_TRACE_TRACING,
5391 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5392 + obj->objectId));
5393 + yaffs_DoGenericObjectDeletion(obj);
5394 + } else {
5395 + yaffs_SoftDeleteWorker(obj,
5396 + obj->variant.fileVariant.top,
5397 + obj->variant.fileVariant.
5398 + topLevel, 0);
5399 + obj->softDeleted = 1;
5400 + }
5401 + }
5402 +}
5403 +
5404 +/* Pruning removes any part of the file structure tree that is beyond the
5405 + * bounds of the file (ie that does not point to chunks).
5406 + *
5407 + * A file should only get pruned when its size is reduced.
5408 + *
5409 + * Before pruning, the chunks must be pulled from the tree and the
5410 + * level 0 tnode entries must be zeroed out.
5411 + * Could also use this for file deletion, but that's probably better handled
5412 + * by a special case.
5413 + */
5414 +
5415 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
5416 + __u32 level, int del0)
5417 +{
5418 + int i;
5419 + int hasData;
5420 +
5421 + if (tn) {
5422 + hasData = 0;
5423 +
5424 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5425 + if (tn->internal[i] && level > 0) {
5426 + tn->internal[i] =
5427 + yaffs_PruneWorker(dev, tn->internal[i],
5428 + level - 1,
5429 + (i == 0) ? del0 : 1);
5430 + }
5431 +
5432 + if (tn->internal[i]) {
5433 + hasData++;
5434 + }
5435 + }
5436 +
5437 + if (hasData == 0 && del0) {
5438 + /* Free and return NULL */
5439 +
5440 + yaffs_FreeTnode(dev, tn);
5441 + tn = NULL;
5442 + }
5443 +
5444 + }
5445 +
5446 + return tn;
5447 +
5448 +}
5449 +
5450 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
5451 + yaffs_FileStructure * fStruct)
5452 +{
5453 + int i;
5454 + int hasData;
5455 + int done = 0;
5456 + yaffs_Tnode *tn;
5457 +
5458 + if (fStruct->topLevel > 0) {
5459 + fStruct->top =
5460 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5461 +
5462 + /* Now we have a tree with all the non-zero branches NULL but the height
5463 + * is the same as it was.
5464 + * Let's see if we can trim internal tnodes to shorten the tree.
5465 + * We can do this if only the 0th element in the tnode is in use
5466 + * (ie all the non-zero are NULL)
5467 + */
5468 +
5469 + while (fStruct->topLevel && !done) {
5470 + tn = fStruct->top;
5471 +
5472 + hasData = 0;
5473 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5474 + if (tn->internal[i]) {
5475 + hasData++;
5476 + }
5477 + }
5478 +
5479 + if (!hasData) {
5480 + fStruct->top = tn->internal[0];
5481 + fStruct->topLevel--;
5482 + yaffs_FreeTnode(dev, tn);
5483 + } else {
5484 + done = 1;
5485 + }
5486 + }
5487 + }
5488 +
5489 + return YAFFS_OK;
5490 +}
5491 +
5492 +/*-------------------- End of File Structure functions.-------------------*/
5493 +
5494 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5495 + * adds them to the object free list.
5496 + */
5497 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
5498 +{
5499 + int i;
5500 + yaffs_Object *newObjects;
5501 + yaffs_ObjectList *list;
5502 +
5503 + if (nObjects < 1)
5504 + return YAFFS_OK;
5505 +
5506 + /* make these things */
5507 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5508 + list = YMALLOC(sizeof(yaffs_ObjectList));
5509 +
5510 + if (!newObjects || !list) {
5511 + if(newObjects)
5512 + YFREE(newObjects);
5513 + if(list)
5514 + YFREE(list);
5515 + T(YAFFS_TRACE_ALLOCATE,
5516 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5517 + return YAFFS_FAIL;
5518 + }
5519 +
5520 + /* Hook them into the free list */
5521 + for (i = 0; i < nObjects - 1; i++) {
5522 + newObjects[i].siblings.next =
5523 + (struct list_head *)(&newObjects[i + 1]);
5524 + }
5525 +
5526 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5527 + dev->freeObjects = newObjects;
5528 + dev->nFreeObjects += nObjects;
5529 + dev->nObjectsCreated += nObjects;
5530 +
5531 + /* Now add this bunch of Objects to a list for freeing up. */
5532 +
5533 + list->objects = newObjects;
5534 + list->next = dev->allocatedObjectList;
5535 + dev->allocatedObjectList = list;
5536 +
5537 + return YAFFS_OK;
5538 +}
5539 +
5540 +
5541 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5542 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
5543 +{
5544 + yaffs_Object *tn = NULL;
5545 +
5546 + /* If there are none left make more */
5547 + if (!dev->freeObjects) {
5548 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5549 + }
5550 +
5551 + if (dev->freeObjects) {
5552 + tn = dev->freeObjects;
5553 + dev->freeObjects =
5554 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5555 + dev->nFreeObjects--;
5556 +
5557 + /* Now sweeten it up... */
5558 +
5559 + memset(tn, 0, sizeof(yaffs_Object));
5560 + tn->myDev = dev;
5561 + tn->chunkId = -1;
5562 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5563 + INIT_LIST_HEAD(&(tn->hardLinks));
5564 + INIT_LIST_HEAD(&(tn->hashLink));
5565 + INIT_LIST_HEAD(&tn->siblings);
5566 +
5567 + /* Add it to the lost and found directory.
5568 + * NB Can't put root or lostNFound in lostNFound so
5569 + * check if lostNFound exists first
5570 + */
5571 + if (dev->lostNFoundDir) {
5572 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5573 + }
5574 + }
5575 +
5576 + return tn;
5577 +}
5578 +
5579 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
5580 + __u32 mode)
5581 +{
5582 +
5583 + yaffs_Object *obj =
5584 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5585 + if (obj) {
5586 + obj->fake = 1; /* it is fake so it has no NAND presence... */
5587 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5588 + obj->unlinkAllowed = 0; /* ... or unlink it */
5589 + obj->deleted = 0;
5590 + obj->unlinked = 0;
5591 + obj->yst_mode = mode;
5592 + obj->myDev = dev;
5593 + obj->chunkId = 0; /* Not a valid chunk. */
5594 + }
5595 +
5596 + return obj;
5597 +
5598 +}
5599 +
5600 +static void yaffs_UnhashObject(yaffs_Object * tn)
5601 +{
5602 + int bucket;
5603 + yaffs_Device *dev = tn->myDev;
5604 +
5605 + /* If it is still linked into the bucket list, free from the list */
5606 + if (!list_empty(&tn->hashLink)) {
5607 + list_del_init(&tn->hashLink);
5608 + bucket = yaffs_HashFunction(tn->objectId);
5609 + dev->objectBucket[bucket].count--;
5610 + }
5611 +
5612 +}
5613 +
5614 +/* FreeObject frees up a Object and puts it back on the free list */
5615 +static void yaffs_FreeObject(yaffs_Object * tn)
5616 +{
5617 +
5618 + yaffs_Device *dev = tn->myDev;
5619 +
5620 +#ifdef __KERNEL__
5621 + if (tn->myInode) {
5622 + /* We're still hooked up to a cached inode.
5623 + * Don't delete now, but mark for later deletion
5624 + */
5625 + tn->deferedFree = 1;
5626 + return;
5627 + }
5628 +#endif
5629 +
5630 + yaffs_UnhashObject(tn);
5631 +
5632 + /* Link into the free list. */
5633 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
5634 + dev->freeObjects = tn;
5635 + dev->nFreeObjects++;
5636 +}
5637 +
5638 +#ifdef __KERNEL__
5639 +
5640 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
5641 +{
5642 + if (obj->deferedFree) {
5643 + yaffs_FreeObject(obj);
5644 + }
5645 +}
5646 +
5647 +#endif
5648 +
5649 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
5650 +{
5651 + /* Free the list of allocated Objects */
5652 +
5653 + yaffs_ObjectList *tmp;
5654 +
5655 + while (dev->allocatedObjectList) {
5656 + tmp = dev->allocatedObjectList->next;
5657 + YFREE(dev->allocatedObjectList->objects);
5658 + YFREE(dev->allocatedObjectList);
5659 +
5660 + dev->allocatedObjectList = tmp;
5661 + }
5662 +
5663 + dev->freeObjects = NULL;
5664 + dev->nFreeObjects = 0;
5665 +}
5666 +
5667 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
5668 +{
5669 + int i;
5670 +
5671 + dev->allocatedObjectList = NULL;
5672 + dev->freeObjects = NULL;
5673 + dev->nFreeObjects = 0;
5674 +
5675 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5676 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
5677 + dev->objectBucket[i].count = 0;
5678 + }
5679 +
5680 +}
5681 +
5682 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
5683 +{
5684 + static int x = 0;
5685 + int i;
5686 + int l = 999;
5687 + int lowest = 999999;
5688 +
5689 + /* First let's see if we can find one that's empty. */
5690 +
5691 + for (i = 0; i < 10 && lowest > 0; i++) {
5692 + x++;
5693 + x %= YAFFS_NOBJECT_BUCKETS;
5694 + if (dev->objectBucket[x].count < lowest) {
5695 + lowest = dev->objectBucket[x].count;
5696 + l = x;
5697 + }
5698 +
5699 + }
5700 +
5701 + /* If we didn't find an empty list, then try
5702 + * looking a bit further for a short one
5703 + */
5704 +
5705 + for (i = 0; i < 10 && lowest > 3; i++) {
5706 + x++;
5707 + x %= YAFFS_NOBJECT_BUCKETS;
5708 + if (dev->objectBucket[x].count < lowest) {
5709 + lowest = dev->objectBucket[x].count;
5710 + l = x;
5711 + }
5712 +
5713 + }
5714 +
5715 + return l;
5716 +}
5717 +
5718 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
5719 +{
5720 + int bucket = yaffs_FindNiceObjectBucket(dev);
5721 +
5722 + /* Now find an object value that has not already been taken
5723 + * by scanning the list.
5724 + */
5725 +
5726 + int found = 0;
5727 + struct list_head *i;
5728 +
5729 + __u32 n = (__u32) bucket;
5730 +
5731 + /* yaffs_CheckObjectHashSanity(); */
5732 +
5733 + while (!found) {
5734 + found = 1;
5735 + n += YAFFS_NOBJECT_BUCKETS;
5736 + if (1 || dev->objectBucket[bucket].count > 0) {
5737 + list_for_each(i, &dev->objectBucket[bucket].list) {
5738 + /* If there is already one in the list */
5739 + if (i
5740 + && list_entry(i, yaffs_Object,
5741 + hashLink)->objectId == n) {
5742 + found = 0;
5743 + }
5744 + }
5745 + }
5746 + }
5747 +
5748 +
5749 + return n;
5750 +}
5751 +
5752 +static void yaffs_HashObject(yaffs_Object * in)
5753 +{
5754 + int bucket = yaffs_HashFunction(in->objectId);
5755 + yaffs_Device *dev = in->myDev;
5756 +
5757 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
5758 + dev->objectBucket[bucket].count++;
5759 +
5760 +}
5761 +
5762 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5763 +{
5764 + int bucket = yaffs_HashFunction(number);
5765 + struct list_head *i;
5766 + yaffs_Object *in;
5767 +
5768 + list_for_each(i, &dev->objectBucket[bucket].list) {
5769 + /* Look if it is in the list */
5770 + if (i) {
5771 + in = list_entry(i, yaffs_Object, hashLink);
5772 + if (in->objectId == number) {
5773 +#ifdef __KERNEL__
5774 + /* Don't tell the VFS about this one if it is defered free */
5775 + if (in->deferedFree)
5776 + return NULL;
5777 +#endif
5778 +
5779 + return in;
5780 + }
5781 + }
5782 + }
5783 +
5784 + return NULL;
5785 +}
5786 +
5787 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5788 + yaffs_ObjectType type)
5789 +{
5790 +
5791 + yaffs_Object *theObject;
5792 + yaffs_Tnode *tn;
5793 +
5794 + if (number < 0) {
5795 + number = yaffs_CreateNewObjectNumber(dev);
5796 + }
5797 +
5798 + theObject = yaffs_AllocateEmptyObject(dev);
5799 + if(!theObject)
5800 + return NULL;
5801 +
5802 + if(type == YAFFS_OBJECT_TYPE_FILE){
5803 + tn = yaffs_GetTnode(dev);
5804 + if(!tn){
5805 + yaffs_FreeObject(theObject);
5806 + return NULL;
5807 + }
5808 + }
5809 +
5810 +
5811 +
5812 + if (theObject) {
5813 + theObject->fake = 0;
5814 + theObject->renameAllowed = 1;
5815 + theObject->unlinkAllowed = 1;
5816 + theObject->objectId = number;
5817 + yaffs_HashObject(theObject);
5818 + theObject->variantType = type;
5819 +#ifdef CONFIG_YAFFS_WINCE
5820 + yfsd_WinFileTimeNow(theObject->win_atime);
5821 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5822 + theObject->win_atime[0];
5823 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5824 + theObject->win_atime[1];
5825 +
5826 +#else
5827 +
5828 + theObject->yst_atime = theObject->yst_mtime =
5829 + theObject->yst_ctime = Y_CURRENT_TIME;
5830 +#endif
5831 + switch (type) {
5832 + case YAFFS_OBJECT_TYPE_FILE:
5833 + theObject->variant.fileVariant.fileSize = 0;
5834 + theObject->variant.fileVariant.scannedFileSize = 0;
5835 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5836 + theObject->variant.fileVariant.topLevel = 0;
5837 + theObject->variant.fileVariant.top = tn;
5838 + break;
5839 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5840 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5841 + children);
5842 + break;
5843 + case YAFFS_OBJECT_TYPE_SYMLINK:
5844 + case YAFFS_OBJECT_TYPE_HARDLINK:
5845 + case YAFFS_OBJECT_TYPE_SPECIAL:
5846 + /* No action required */
5847 + break;
5848 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5849 + /* todo this should not happen */
5850 + break;
5851 + }
5852 + }
5853 +
5854 + return theObject;
5855 +}
5856 +
5857 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5858 + int number,
5859 + yaffs_ObjectType type)
5860 +{
5861 + yaffs_Object *theObject = NULL;
5862 +
5863 + if (number > 0) {
5864 + theObject = yaffs_FindObjectByNumber(dev, number);
5865 + }
5866 +
5867 + if (!theObject) {
5868 + theObject = yaffs_CreateNewObject(dev, number, type);
5869 + }
5870 +
5871 + return theObject;
5872 +
5873 +}
5874 +
5875 +
5876 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5877 +{
5878 + YCHAR *newStr = NULL;
5879 +
5880 + if (str && *str) {
5881 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5882 + if(newStr)
5883 + yaffs_strcpy(newStr, str);
5884 + }
5885 +
5886 + return newStr;
5887 +
5888 +}
5889 +
5890 +/*
5891 + * Mknod (create) a new object.
5892 + * equivalentObject only has meaning for a hard link;
5893 + * aliasString only has meaning for a sumlink.
5894 + * rdev only has meaning for devices (a subset of special objects)
5895 + */
5896 +
5897 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5898 + yaffs_Object * parent,
5899 + const YCHAR * name,
5900 + __u32 mode,
5901 + __u32 uid,
5902 + __u32 gid,
5903 + yaffs_Object * equivalentObject,
5904 + const YCHAR * aliasString, __u32 rdev)
5905 +{
5906 + yaffs_Object *in;
5907 + YCHAR *str;
5908 +
5909 + yaffs_Device *dev = parent->myDev;
5910 +
5911 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5912 + if (yaffs_FindObjectByName(parent, name)) {
5913 + return NULL;
5914 + }
5915 +
5916 + in = yaffs_CreateNewObject(dev, -1, type);
5917 +
5918 + if(type == YAFFS_OBJECT_TYPE_SYMLINK){
5919 + str = yaffs_CloneString(aliasString);
5920 + if(!str){
5921 + yaffs_FreeObject(in);
5922 + return NULL;
5923 + }
5924 + }
5925 +
5926 +
5927 +
5928 + if (in) {
5929 + in->chunkId = -1;
5930 + in->valid = 1;
5931 + in->variantType = type;
5932 +
5933 + in->yst_mode = mode;
5934 +
5935 +#ifdef CONFIG_YAFFS_WINCE
5936 + yfsd_WinFileTimeNow(in->win_atime);
5937 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5938 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5939 +
5940 +#else
5941 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5942 +
5943 + in->yst_rdev = rdev;
5944 + in->yst_uid = uid;
5945 + in->yst_gid = gid;
5946 +#endif
5947 + in->nDataChunks = 0;
5948 +
5949 + yaffs_SetObjectName(in, name);
5950 + in->dirty = 1;
5951 +
5952 + yaffs_AddObjectToDirectory(parent, in);
5953 +
5954 + in->myDev = parent->myDev;
5955 +
5956 + switch (type) {
5957 + case YAFFS_OBJECT_TYPE_SYMLINK:
5958 + in->variant.symLinkVariant.alias = str;
5959 + break;
5960 + case YAFFS_OBJECT_TYPE_HARDLINK:
5961 + in->variant.hardLinkVariant.equivalentObject =
5962 + equivalentObject;
5963 + in->variant.hardLinkVariant.equivalentObjectId =
5964 + equivalentObject->objectId;
5965 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5966 + break;
5967 + case YAFFS_OBJECT_TYPE_FILE:
5968 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5969 + case YAFFS_OBJECT_TYPE_SPECIAL:
5970 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5971 + /* do nothing */
5972 + break;
5973 + }
5974 +
5975 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5976 + /* Could not create the object header, fail the creation */
5977 + yaffs_DestroyObject(in);
5978 + in = NULL;
5979 + }
5980 +
5981 + }
5982 +
5983 + return in;
5984 +}
5985 +
5986 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5987 + __u32 mode, __u32 uid, __u32 gid)
5988 +{
5989 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
5990 + uid, gid, NULL, NULL, 0);
5991 +}
5992 +
5993 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
5994 + __u32 mode, __u32 uid, __u32 gid)
5995 +{
5996 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
5997 + mode, uid, gid, NULL, NULL, 0);
5998 +}
5999 +
6000 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
6001 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
6002 +{
6003 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
6004 + uid, gid, NULL, NULL, rdev);
6005 +}
6006 +
6007 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
6008 + __u32 mode, __u32 uid, __u32 gid,
6009 + const YCHAR * alias)
6010 +{
6011 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
6012 + uid, gid, NULL, alias, 0);
6013 +}
6014 +
6015 +/* yaffs_Link returns the object id of the equivalent object.*/
6016 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
6017 + yaffs_Object * equivalentObject)
6018 +{
6019 + /* Get the real object in case we were fed a hard link as an equivalent object */
6020 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6021 +
6022 + if (yaffs_MknodObject
6023 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6024 + equivalentObject, NULL, 0)) {
6025 + return equivalentObject;
6026 + } else {
6027 + return NULL;
6028 + }
6029 +
6030 +}
6031 +
6032 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
6033 + const YCHAR * newName, int force, int shadows)
6034 +{
6035 + int unlinkOp;
6036 + int deleteOp;
6037 +
6038 + yaffs_Object *existingTarget;
6039 +
6040 + if (newDir == NULL) {
6041 + newDir = obj->parent; /* use the old directory */
6042 + }
6043 +
6044 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6045 + T(YAFFS_TRACE_ALWAYS,
6046 + (TSTR
6047 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
6048 + TENDSTR)));
6049 + YBUG();
6050 + }
6051 +
6052 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6053 + if (obj->myDev->isYaffs2) {
6054 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6055 + } else {
6056 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6057 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6058 + }
6059 +
6060 + deleteOp = (newDir == obj->myDev->deletedDir);
6061 +
6062 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6063 +
6064 + /* If the object is a file going into the unlinked directory,
6065 + * then it is OK to just stuff it in since duplicate names are allowed.
6066 + * else only proceed if the new name does not exist and if we're putting
6067 + * it into a directory.
6068 + */
6069 + if ((unlinkOp ||
6070 + deleteOp ||
6071 + force ||
6072 + (shadows > 0) ||
6073 + !existingTarget) &&
6074 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6075 + yaffs_SetObjectName(obj, newName);
6076 + obj->dirty = 1;
6077 +
6078 + yaffs_AddObjectToDirectory(newDir, obj);
6079 +
6080 + if (unlinkOp)
6081 + obj->unlinked = 1;
6082 +
6083 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6084 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
6085 + return YAFFS_OK;
6086 + }
6087 +
6088 + return YAFFS_FAIL;
6089 +}
6090 +
6091 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
6092 + yaffs_Object * newDir, const YCHAR * newName)
6093 +{
6094 + yaffs_Object *obj;
6095 + yaffs_Object *existingTarget;
6096 + int force = 0;
6097 +
6098 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6099 + /* Special case for case insemsitive systems (eg. WinCE).
6100 + * While look-up is case insensitive, the name isn't.
6101 + * Therefore we might want to change x.txt to X.txt
6102 + */
6103 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
6104 + force = 1;
6105 + }
6106 +#endif
6107 +
6108 + obj = yaffs_FindObjectByName(oldDir, oldName);
6109 + /* Check new name to long. */
6110 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
6111 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
6112 + /* ENAMETOOLONG */
6113 + return YAFFS_FAIL;
6114 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
6115 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6116 + /* ENAMETOOLONG */
6117 + return YAFFS_FAIL;
6118 +
6119 + if (obj && obj->renameAllowed) {
6120 +
6121 + /* Now do the handling for an existing target, if there is one */
6122 +
6123 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6124 + if (existingTarget &&
6125 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6126 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
6127 + /* There is a target that is a non-empty directory, so we fail */
6128 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6129 + } else if (existingTarget && existingTarget != obj) {
6130 + /* Nuke the target first, using shadowing,
6131 + * but only if it isn't the same object
6132 + */
6133 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6134 + existingTarget->objectId);
6135 + yaffs_UnlinkObject(existingTarget);
6136 + }
6137 +
6138 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6139 + }
6140 + return YAFFS_FAIL;
6141 +}
6142 +
6143 +/*------------------------- Block Management and Page Allocation ----------------*/
6144 +
6145 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
6146 +{
6147 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6148 +
6149 + dev->blockInfo = NULL;
6150 + dev->chunkBits = NULL;
6151 +
6152 + dev->allocationBlock = -1; /* force it to get a new one */
6153 +
6154 + /* If the first allocation strategy fails, thry the alternate one */
6155 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6156 + if(!dev->blockInfo){
6157 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6158 + dev->blockInfoAlt = 1;
6159 + }
6160 + else
6161 + dev->blockInfoAlt = 0;
6162 +
6163 + if(dev->blockInfo){
6164 +
6165 + /* Set up dynamic blockinfo stuff. */
6166 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6167 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6168 + if(!dev->chunkBits){
6169 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6170 + dev->chunkBitsAlt = 1;
6171 + }
6172 + else
6173 + dev->chunkBitsAlt = 0;
6174 + }
6175 +
6176 + if (dev->blockInfo && dev->chunkBits) {
6177 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6178 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6179 + return YAFFS_OK;
6180 + }
6181 +
6182 + return YAFFS_FAIL;
6183 +
6184 +}
6185 +
6186 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
6187 +{
6188 + if(dev->blockInfoAlt && dev->blockInfo)
6189 + YFREE_ALT(dev->blockInfo);
6190 + else if(dev->blockInfo)
6191 + YFREE(dev->blockInfo);
6192 +
6193 + dev->blockInfoAlt = 0;
6194 +
6195 + dev->blockInfo = NULL;
6196 +
6197 + if(dev->chunkBitsAlt && dev->chunkBits)
6198 + YFREE_ALT(dev->chunkBits);
6199 + else if(dev->chunkBits)
6200 + YFREE(dev->chunkBits);
6201 + dev->chunkBitsAlt = 0;
6202 + dev->chunkBits = NULL;
6203 +}
6204 +
6205 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
6206 + yaffs_BlockInfo * bi)
6207 +{
6208 + int i;
6209 + __u32 seq;
6210 + yaffs_BlockInfo *b;
6211 +
6212 + if (!dev->isYaffs2)
6213 + return 1; /* disqualification only applies to yaffs2. */
6214 +
6215 + if (!bi->hasShrinkHeader)
6216 + return 1; /* can gc */
6217 +
6218 + /* Find the oldest dirty sequence number if we don't know it and save it
6219 + * so we don't have to keep recomputing it.
6220 + */
6221 + if (!dev->oldestDirtySequence) {
6222 + seq = dev->sequenceNumber;
6223 +
6224 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6225 + i++) {
6226 + b = yaffs_GetBlockInfo(dev, i);
6227 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6228 + (b->pagesInUse - b->softDeletions) <
6229 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6230 + seq = b->sequenceNumber;
6231 + }
6232 + }
6233 + dev->oldestDirtySequence = seq;
6234 + }
6235 +
6236 + /* Can't do gc of this block if there are any blocks older than this one that have
6237 + * discarded pages.
6238 + */
6239 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6240 +
6241 +}
6242 +
6243 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6244 + * for garbage collection.
6245 + */
6246 +
6247 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
6248 + int aggressive)
6249 +{
6250 +
6251 + int b = dev->currentDirtyChecker;
6252 +
6253 + int i;
6254 + int iterations;
6255 + int dirtiest = -1;
6256 + int pagesInUse = 0;
6257 + int prioritised=0;
6258 + yaffs_BlockInfo *bi;
6259 + int pendingPrioritisedExist = 0;
6260 +
6261 + /* First let's see if we need to grab a prioritised block */
6262 + if(dev->hasPendingPrioritisedGCs){
6263 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
6264 +
6265 + bi = yaffs_GetBlockInfo(dev, i);
6266 + //yaffs_VerifyBlock(dev,bi,i);
6267 +
6268 + if(bi->gcPrioritise) {
6269 + pendingPrioritisedExist = 1;
6270 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6271 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
6272 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6273 + dirtiest = i;
6274 + prioritised = 1;
6275 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6276 + }
6277 + }
6278 + }
6279 +
6280 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
6281 + dev->hasPendingPrioritisedGCs = 0;
6282 + }
6283 +
6284 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6285 + * search harder.
6286 + * else (we're doing a leasurely gc), then we only bother to do this if the
6287 + * block has only a few pages in use.
6288 + */
6289 +
6290 + dev->nonAggressiveSkip--;
6291 +
6292 + if (!aggressive && (dev->nonAggressiveSkip > 0)) {
6293 + return -1;
6294 + }
6295 +
6296 + if(!prioritised)
6297 + pagesInUse =
6298 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6299 +
6300 + if (aggressive) {
6301 + iterations =
6302 + dev->internalEndBlock - dev->internalStartBlock + 1;
6303 + } else {
6304 + iterations =
6305 + dev->internalEndBlock - dev->internalStartBlock + 1;
6306 + iterations = iterations / 16;
6307 + if (iterations > 200) {
6308 + iterations = 200;
6309 + }
6310 + }
6311 +
6312 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6313 + b++;
6314 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6315 + b = dev->internalStartBlock;
6316 + }
6317 +
6318 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6319 + T(YAFFS_TRACE_ERROR,
6320 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6321 + YBUG();
6322 + }
6323 +
6324 + bi = yaffs_GetBlockInfo(dev, b);
6325 +
6326 +#if 0
6327 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
6328 + dirtiest = b;
6329 + pagesInUse = 0;
6330 + }
6331 + else
6332 +#endif
6333 +
6334 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6335 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6336 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6337 + dirtiest = b;
6338 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6339 + }
6340 + }
6341 +
6342 + dev->currentDirtyChecker = b;
6343 +
6344 + if (dirtiest > 0) {
6345 + T(YAFFS_TRACE_GC,
6346 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6347 + dev->nChunksPerBlock - pagesInUse,prioritised));
6348 + }
6349 +
6350 + dev->oldestDirtySequence = 0;
6351 +
6352 + if (dirtiest > 0) {
6353 + dev->nonAggressiveSkip = 4;
6354 + }
6355 +
6356 + return dirtiest;
6357 +}
6358 +
6359 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
6360 +{
6361 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6362 +
6363 + int erasedOk = 0;
6364 +
6365 + /* If the block is still healthy erase it and mark as clean.
6366 + * If the block has had a data failure, then retire it.
6367 + */
6368 +
6369 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6370 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6371 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6372 +
6373 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6374 +
6375 + if (!bi->needsRetiring) {
6376 + yaffs_InvalidateCheckpoint(dev);
6377 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6378 + if (!erasedOk) {
6379 + dev->nErasureFailures++;
6380 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6381 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6382 + }
6383 + }
6384 +
6385 + if (erasedOk &&
6386 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6387 + int i;
6388 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6389 + if (!yaffs_CheckChunkErased
6390 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6391 + T(YAFFS_TRACE_ERROR,
6392 + (TSTR
6393 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6394 + TENDSTR), blockNo, i));
6395 + }
6396 + }
6397 + }
6398 +
6399 + if (erasedOk) {
6400 + /* Clean it up... */
6401 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6402 + dev->nErasedBlocks++;
6403 + bi->pagesInUse = 0;
6404 + bi->softDeletions = 0;
6405 + bi->hasShrinkHeader = 0;
6406 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6407 + bi->gcPrioritise = 0;
6408 + yaffs_ClearChunkBits(dev, blockNo);
6409 +
6410 + T(YAFFS_TRACE_ERASE,
6411 + (TSTR("Erased block %d" TENDSTR), blockNo));
6412 + } else {
6413 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6414 +
6415 + yaffs_RetireBlock(dev, blockNo);
6416 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6417 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6418 + }
6419 +}
6420 +
6421 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
6422 +{
6423 + int i;
6424 +
6425 + yaffs_BlockInfo *bi;
6426 +
6427 + if (dev->nErasedBlocks < 1) {
6428 + /* Hoosterman we've got a problem.
6429 + * Can't get space to gc
6430 + */
6431 + T(YAFFS_TRACE_ERROR,
6432 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
6433 +
6434 + return -1;
6435 + }
6436 +
6437 + /* Find an empty block. */
6438 +
6439 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6440 + dev->allocationBlockFinder++;
6441 + if (dev->allocationBlockFinder < dev->internalStartBlock
6442 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6443 + dev->allocationBlockFinder = dev->internalStartBlock;
6444 + }
6445 +
6446 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6447 +
6448 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6449 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6450 + dev->sequenceNumber++;
6451 + bi->sequenceNumber = dev->sequenceNumber;
6452 + dev->nErasedBlocks--;
6453 + T(YAFFS_TRACE_ALLOCATE,
6454 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6455 + dev->allocationBlockFinder, dev->sequenceNumber,
6456 + dev->nErasedBlocks));
6457 + return dev->allocationBlockFinder;
6458 + }
6459 + }
6460 +
6461 + T(YAFFS_TRACE_ALWAYS,
6462 + (TSTR
6463 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
6464 + TENDSTR), dev->nErasedBlocks));
6465 +
6466 + return -1;
6467 +}
6468 +
6469 +
6470 +// Check if there's space to allocate...
6471 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6472 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
6473 +{
6474 + int reservedChunks;
6475 + int reservedBlocks = dev->nReservedBlocks;
6476 + int checkpointBlocks;
6477 +
6478 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
6479 + if(checkpointBlocks < 0)
6480 + checkpointBlocks = 0;
6481 +
6482 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6483 +
6484 + return (dev->nFreeChunks > reservedChunks);
6485 +}
6486 +
6487 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
6488 +{
6489 + int retVal;
6490 + yaffs_BlockInfo *bi;
6491 +
6492 + if (dev->allocationBlock < 0) {
6493 + /* Get next block to allocate off */
6494 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6495 + dev->allocationPage = 0;
6496 + }
6497 +
6498 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6499 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6500 + return -1;
6501 + }
6502 +
6503 + if (dev->nErasedBlocks < dev->nReservedBlocks
6504 + && dev->allocationPage == 0) {
6505 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6506 + }
6507 +
6508 + /* Next page please.... */
6509 + if (dev->allocationBlock >= 0) {
6510 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6511 +
6512 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6513 + dev->allocationPage;
6514 + bi->pagesInUse++;
6515 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6516 + dev->allocationPage);
6517 +
6518 + dev->allocationPage++;
6519 +
6520 + dev->nFreeChunks--;
6521 +
6522 + /* If the block is full set the state to full */
6523 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6524 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6525 + dev->allocationBlock = -1;
6526 + }
6527 +
6528 + if(blockUsedPtr)
6529 + *blockUsedPtr = bi;
6530 +
6531 + return retVal;
6532 + }
6533 +
6534 + T(YAFFS_TRACE_ERROR,
6535 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6536 +
6537 + return -1;
6538 +}
6539 +
6540 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
6541 +{
6542 + int n;
6543 +
6544 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6545 +
6546 + if (dev->allocationBlock > 0) {
6547 + n += (dev->nChunksPerBlock - dev->allocationPage);
6548 + }
6549 +
6550 + return n;
6551 +
6552 +}
6553 +
6554 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
6555 +{
6556 + int oldChunk;
6557 + int newChunk;
6558 + int chunkInBlock;
6559 + int markNAND;
6560 + int retVal = YAFFS_OK;
6561 + int cleanups = 0;
6562 + int i;
6563 + int isCheckpointBlock;
6564 + int matchingChunk;
6565 +
6566 + int chunksBefore = yaffs_GetErasedChunks(dev);
6567 + int chunksAfter;
6568 +
6569 + yaffs_ExtendedTags tags;
6570 +
6571 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6572 +
6573 + yaffs_Object *object;
6574 +
6575 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6576 +
6577 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6578 +
6579 + T(YAFFS_TRACE_TRACING,
6580 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
6581 + bi->pagesInUse, bi->hasShrinkHeader));
6582 +
6583 + /*yaffs_VerifyFreeChunks(dev); */
6584 +
6585 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6586 +
6587 + /* Take off the number of soft deleted entries because
6588 + * they're going to get really deleted during GC.
6589 + */
6590 + dev->nFreeChunks -= bi->softDeletions;
6591 +
6592 + dev->isDoingGC = 1;
6593 +
6594 + if (isCheckpointBlock ||
6595 + !yaffs_StillSomeChunkBits(dev, block)) {
6596 + T(YAFFS_TRACE_TRACING,
6597 + (TSTR
6598 + ("Collecting block %d that has no chunks in use" TENDSTR),
6599 + block));
6600 + yaffs_BlockBecameDirty(dev, block);
6601 + } else {
6602 +
6603 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6604 +
6605 + yaffs_VerifyBlock(dev,bi,block);
6606 +
6607 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
6608 + chunkInBlock < dev->nChunksPerBlock
6609 + && yaffs_StillSomeChunkBits(dev, block);
6610 + chunkInBlock++, oldChunk++) {
6611 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
6612 +
6613 + /* This page is in use and might need to be copied off */
6614 +
6615 + markNAND = 1;
6616 +
6617 + yaffs_InitialiseTags(&tags);
6618 +
6619 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
6620 + buffer, &tags);
6621 +
6622 + object =
6623 + yaffs_FindObjectByNumber(dev,
6624 + tags.objectId);
6625 +
6626 + T(YAFFS_TRACE_GC_DETAIL,
6627 + (TSTR
6628 + ("Collecting page %d, %d %d %d " TENDSTR),
6629 + chunkInBlock, tags.objectId, tags.chunkId,
6630 + tags.byteCount));
6631 +
6632 + if(object && !yaffs_SkipVerification(dev)){
6633 + if(tags.chunkId == 0)
6634 + matchingChunk = object->chunkId;
6635 + else if(object->softDeleted)
6636 + matchingChunk = oldChunk; /* Defeat the test */
6637 + else
6638 + matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
6639 +
6640 + if(oldChunk != matchingChunk)
6641 + T(YAFFS_TRACE_ERROR,
6642 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
6643 + oldChunk,matchingChunk,tags.objectId, tags.chunkId));
6644 +
6645 + }
6646 +
6647 + if (!object) {
6648 + T(YAFFS_TRACE_ERROR,
6649 + (TSTR
6650 + ("page %d in gc has no object: %d %d %d "
6651 + TENDSTR), oldChunk,
6652 + tags.objectId, tags.chunkId, tags.byteCount));
6653 + }
6654 +
6655 + if (object && object->deleted
6656 + && tags.chunkId != 0) {
6657 + /* Data chunk in a deleted file, throw it away
6658 + * It's a soft deleted data chunk,
6659 + * No need to copy this, just forget about it and
6660 + * fix up the object.
6661 + */
6662 +
6663 + object->nDataChunks--;
6664 +
6665 + if (object->nDataChunks <= 0) {
6666 + /* remeber to clean up the object */
6667 + dev->gcCleanupList[cleanups] =
6668 + tags.objectId;
6669 + cleanups++;
6670 + }
6671 + markNAND = 0;
6672 + } else if (0
6673 + /* Todo object && object->deleted && object->nDataChunks == 0 */
6674 + ) {
6675 + /* Deleted object header with no data chunks.
6676 + * Can be discarded and the file deleted.
6677 + */
6678 + object->chunkId = 0;
6679 + yaffs_FreeTnode(object->myDev,
6680 + object->variant.
6681 + fileVariant.top);
6682 + object->variant.fileVariant.top = NULL;
6683 + yaffs_DoGenericObjectDeletion(object);
6684 +
6685 + } else if (object) {
6686 + /* It's either a data chunk in a live file or
6687 + * an ObjectHeader, so we're interested in it.
6688 + * NB Need to keep the ObjectHeaders of deleted files
6689 + * until the whole file has been deleted off
6690 + */
6691 + tags.serialNumber++;
6692 +
6693 + dev->nGCCopies++;
6694 +
6695 + if (tags.chunkId == 0) {
6696 + /* It is an object Id,
6697 + * We need to nuke the shrinkheader flags first
6698 + * We no longer want the shrinkHeader flag since its work is done
6699 + * and if it is left in place it will mess up scanning.
6700 + * Also, clear out any shadowing stuff
6701 + */
6702 +
6703 + yaffs_ObjectHeader *oh;
6704 + oh = (yaffs_ObjectHeader *)buffer;
6705 + oh->isShrink = 0;
6706 + oh->shadowsObject = -1;
6707 + tags.extraShadows = 0;
6708 + tags.extraIsShrinkHeader = 0;
6709 +
6710 + yaffs_VerifyObjectHeader(object,oh,&tags,1);
6711 + }
6712 +
6713 + newChunk =
6714 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
6715 +
6716 + if (newChunk < 0) {
6717 + retVal = YAFFS_FAIL;
6718 + } else {
6719 +
6720 + /* Ok, now fix up the Tnodes etc. */
6721 +
6722 + if (tags.chunkId == 0) {
6723 + /* It's a header */
6724 + object->chunkId = newChunk;
6725 + object->serial = tags.serialNumber;
6726 + } else {
6727 + /* It's a data chunk */
6728 + yaffs_PutChunkIntoFile
6729 + (object,
6730 + tags.chunkId,
6731 + newChunk, 0);
6732 + }
6733 + }
6734 + }
6735 +
6736 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
6737 +
6738 + }
6739 + }
6740 +
6741 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6742 +
6743 +
6744 + /* Do any required cleanups */
6745 + for (i = 0; i < cleanups; i++) {
6746 + /* Time to delete the file too */
6747 + object =
6748 + yaffs_FindObjectByNumber(dev,
6749 + dev->gcCleanupList[i]);
6750 + if (object) {
6751 + yaffs_FreeTnode(dev,
6752 + object->variant.fileVariant.
6753 + top);
6754 + object->variant.fileVariant.top = NULL;
6755 + T(YAFFS_TRACE_GC,
6756 + (TSTR
6757 + ("yaffs: About to finally delete object %d"
6758 + TENDSTR), object->objectId));
6759 + yaffs_DoGenericObjectDeletion(object);
6760 + object->myDev->nDeletedFiles--;
6761 + }
6762 +
6763 + }
6764 +
6765 + }
6766 +
6767 + yaffs_VerifyCollectedBlock(dev,bi,block);
6768 +
6769 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
6770 + T(YAFFS_TRACE_GC,
6771 + (TSTR
6772 + ("gc did not increase free chunks before %d after %d"
6773 + TENDSTR), chunksBefore, chunksAfter));
6774 + }
6775 +
6776 + dev->isDoingGC = 0;
6777 +
6778 + return YAFFS_OK;
6779 +}
6780 +
6781 +/* New garbage collector
6782 + * If we're very low on erased blocks then we do aggressive garbage collection
6783 + * otherwise we do "leasurely" garbage collection.
6784 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
6785 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
6786 + *
6787 + * The idea is to help clear out space in a more spread-out manner.
6788 + * Dunno if it really does anything useful.
6789 + */
6790 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
6791 +{
6792 + int block;
6793 + int aggressive;
6794 + int gcOk = YAFFS_OK;
6795 + int maxTries = 0;
6796 +
6797 + int checkpointBlockAdjust;
6798 +
6799 + if (dev->isDoingGC) {
6800 + /* Bail out so we don't get recursive gc */
6801 + return YAFFS_OK;
6802 + }
6803 +
6804 + /* This loop should pass the first time.
6805 + * We'll only see looping here if the erase of the collected block fails.
6806 + */
6807 +
6808 + do {
6809 + maxTries++;
6810 +
6811 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
6812 + if(checkpointBlockAdjust < 0)
6813 + checkpointBlockAdjust = 0;
6814 +
6815 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
6816 + /* We need a block soon...*/
6817 + aggressive = 1;
6818 + } else {
6819 + /* We're in no hurry */
6820 + aggressive = 0;
6821 + }
6822 +
6823 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6824 +
6825 + if (block > 0) {
6826 + dev->garbageCollections++;
6827 + if (!aggressive) {
6828 + dev->passiveGarbageCollections++;
6829 + }
6830 +
6831 + T(YAFFS_TRACE_GC,
6832 + (TSTR
6833 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6834 + dev->nErasedBlocks, aggressive));
6835 +
6836 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6837 + }
6838 +
6839 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6840 + T(YAFFS_TRACE_GC,
6841 + (TSTR
6842 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6843 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6844 + }
6845 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6846 + && (maxTries < 2));
6847 +
6848 + return aggressive ? gcOk : YAFFS_OK;
6849 +}
6850 +
6851 +/*------------------------- TAGS --------------------------------*/
6852 +
6853 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6854 + int chunkInObject)
6855 +{
6856 + return (tags->chunkId == chunkInObject &&
6857 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6858 +
6859 +}
6860 +
6861 +
6862 +/*-------------------- Data file manipulation -----------------*/
6863 +
6864 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6865 + yaffs_ExtendedTags * tags)
6866 +{
6867 + /*Get the Tnode, then get the level 0 offset chunk offset */
6868 + yaffs_Tnode *tn;
6869 + int theChunk = -1;
6870 + yaffs_ExtendedTags localTags;
6871 + int retVal = -1;
6872 +
6873 + yaffs_Device *dev = in->myDev;
6874 +
6875 + if (!tags) {
6876 + /* Passed a NULL, so use our own tags space */
6877 + tags = &localTags;
6878 + }
6879 +
6880 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6881 +
6882 + if (tn) {
6883 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6884 +
6885 + retVal =
6886 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6887 + chunkInInode);
6888 + }
6889 + return retVal;
6890 +}
6891 +
6892 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6893 + yaffs_ExtendedTags * tags)
6894 +{
6895 + /* Get the Tnode, then get the level 0 offset chunk offset */
6896 + yaffs_Tnode *tn;
6897 + int theChunk = -1;
6898 + yaffs_ExtendedTags localTags;
6899 +
6900 + yaffs_Device *dev = in->myDev;
6901 + int retVal = -1;
6902 +
6903 + if (!tags) {
6904 + /* Passed a NULL, so use our own tags space */
6905 + tags = &localTags;
6906 + }
6907 +
6908 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6909 +
6910 + if (tn) {
6911 +
6912 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6913 +
6914 + retVal =
6915 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6916 + chunkInInode);
6917 +
6918 + /* Delete the entry in the filestructure (if found) */
6919 + if (retVal != -1) {
6920 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6921 + }
6922 + } else {
6923 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6924 + }
6925 +
6926 + if (retVal == -1) {
6927 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6928 + }
6929 + return retVal;
6930 +}
6931 +
6932 +#ifdef YAFFS_PARANOID
6933 +
6934 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6935 +{
6936 + int chunk;
6937 + int nChunks;
6938 + int fSize;
6939 + int failed = 0;
6940 + int objId;
6941 + yaffs_Tnode *tn;
6942 + yaffs_Tags localTags;
6943 + yaffs_Tags *tags = &localTags;
6944 + int theChunk;
6945 + int chunkDeleted;
6946 +
6947 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6948 + /* T(("Object not a file\n")); */
6949 + return YAFFS_FAIL;
6950 + }
6951 +
6952 + objId = in->objectId;
6953 + fSize = in->variant.fileVariant.fileSize;
6954 + nChunks =
6955 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6956 +
6957 + for (chunk = 1; chunk <= nChunks; chunk++) {
6958 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6959 + chunk);
6960 +
6961 + if (tn) {
6962 +
6963 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6964 +
6965 + if (yaffs_CheckChunkBits
6966 + (dev, theChunk / dev->nChunksPerBlock,
6967 + theChunk % dev->nChunksPerBlock)) {
6968 +
6969 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6970 + tags,
6971 + &chunkDeleted);
6972 + if (yaffs_TagsMatch
6973 + (tags, in->objectId, chunk, chunkDeleted)) {
6974 + /* found it; */
6975 +
6976 + }
6977 + } else {
6978 +
6979 + failed = 1;
6980 + }
6981 +
6982 + } else {
6983 + /* T(("No level 0 found for %d\n", chunk)); */
6984 + }
6985 + }
6986 +
6987 + return failed ? YAFFS_FAIL : YAFFS_OK;
6988 +}
6989 +
6990 +#endif
6991 +
6992 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
6993 + int chunkInNAND, int inScan)
6994 +{
6995 + /* NB inScan is zero unless scanning.
6996 + * For forward scanning, inScan is > 0;
6997 + * for backward scanning inScan is < 0
6998 + */
6999 +
7000 + yaffs_Tnode *tn;
7001 + yaffs_Device *dev = in->myDev;
7002 + int existingChunk;
7003 + yaffs_ExtendedTags existingTags;
7004 + yaffs_ExtendedTags newTags;
7005 + unsigned existingSerial, newSerial;
7006 +
7007 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7008 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
7009 + * If it is not during Scanning then something went wrong!
7010 + */
7011 + if (!inScan) {
7012 + T(YAFFS_TRACE_ERROR,
7013 + (TSTR
7014 + ("yaffs tragedy:attempt to put data chunk into a non-file"
7015 + TENDSTR)));
7016 + YBUG();
7017 + }
7018 +
7019 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7020 + return YAFFS_OK;
7021 + }
7022 +
7023 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7024 + &in->variant.fileVariant,
7025 + chunkInInode,
7026 + NULL);
7027 + if (!tn) {
7028 + return YAFFS_FAIL;
7029 + }
7030 +
7031 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
7032 +
7033 + if (inScan != 0) {
7034 + /* If we're scanning then we need to test for duplicates
7035 + * NB This does not need to be efficient since it should only ever
7036 + * happen when the power fails during a write, then only one
7037 + * chunk should ever be affected.
7038 + *
7039 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7040 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7041 + */
7042 +
7043 + if (existingChunk != 0) {
7044 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7045 + * thus we have to do a FindChunkInFile to get the real chunk id.
7046 + *
7047 + * We have a duplicate now we need to decide which one to use:
7048 + *
7049 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7050 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7051 + * YAFFS1: Get both sets of tags and compare serial numbers.
7052 + */
7053 +
7054 + if (inScan > 0) {
7055 + /* Only do this for forward scanning */
7056 + yaffs_ReadChunkWithTagsFromNAND(dev,
7057 + chunkInNAND,
7058 + NULL, &newTags);
7059 +
7060 + /* Do a proper find */
7061 + existingChunk =
7062 + yaffs_FindChunkInFile(in, chunkInInode,
7063 + &existingTags);
7064 + }
7065 +
7066 + if (existingChunk <= 0) {
7067 + /*Hoosterman - how did this happen? */
7068 +
7069 + T(YAFFS_TRACE_ERROR,
7070 + (TSTR
7071 + ("yaffs tragedy: existing chunk < 0 in scan"
7072 + TENDSTR)));
7073 +
7074 + }
7075 +
7076 + /* NB The deleted flags should be false, otherwise the chunks will
7077 + * not be loaded during a scan
7078 + */
7079 +
7080 + newSerial = newTags.serialNumber;
7081 + existingSerial = existingTags.serialNumber;
7082 +
7083 + if ((inScan > 0) &&
7084 + (in->myDev->isYaffs2 ||
7085 + existingChunk <= 0 ||
7086 + ((existingSerial + 1) & 3) == newSerial)) {
7087 + /* Forward scanning.
7088 + * Use new
7089 + * Delete the old one and drop through to update the tnode
7090 + */
7091 + yaffs_DeleteChunk(dev, existingChunk, 1,
7092 + __LINE__);
7093 + } else {
7094 + /* Backward scanning or we want to use the existing one
7095 + * Use existing.
7096 + * Delete the new one and return early so that the tnode isn't changed
7097 + */
7098 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7099 + __LINE__);
7100 + return YAFFS_OK;
7101 + }
7102 + }
7103 +
7104 + }
7105 +
7106 + if (existingChunk == 0) {
7107 + in->nDataChunks++;
7108 + }
7109 +
7110 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
7111 +
7112 + return YAFFS_OK;
7113 +}
7114 +
7115 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
7116 + __u8 * buffer)
7117 +{
7118 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7119 +
7120 + if (chunkInNAND >= 0) {
7121 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7122 + buffer,NULL);
7123 + } else {
7124 + T(YAFFS_TRACE_NANDACCESS,
7125 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7126 + chunkInNAND));
7127 + /* get sane (zero) data if you read a hole */
7128 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7129 + return 0;
7130 + }
7131 +
7132 +}
7133 +
7134 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
7135 +{
7136 + int block;
7137 + int page;
7138 + yaffs_ExtendedTags tags;
7139 + yaffs_BlockInfo *bi;
7140 +
7141 + if (chunkId <= 0)
7142 + return;
7143 +
7144 +
7145 + dev->nDeletions++;
7146 + block = chunkId / dev->nChunksPerBlock;
7147 + page = chunkId % dev->nChunksPerBlock;
7148 +
7149 +
7150 + if(!yaffs_CheckChunkBit(dev,block,page))
7151 + T(YAFFS_TRACE_VERIFY,
7152 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7153 + chunkId));
7154 +
7155 + bi = yaffs_GetBlockInfo(dev, block);
7156 +
7157 + T(YAFFS_TRACE_DELETION,
7158 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7159 +
7160 + if (markNAND &&
7161 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7162 +
7163 + yaffs_InitialiseTags(&tags);
7164 +
7165 + tags.chunkDeleted = 1;
7166 +
7167 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7168 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7169 + } else {
7170 + dev->nUnmarkedDeletions++;
7171 + }
7172 +
7173 + /* Pull out of the management area.
7174 + * If the whole block became dirty, this will kick off an erasure.
7175 + */
7176 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7177 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7178 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7179 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7180 + dev->nFreeChunks++;
7181 +
7182 + yaffs_ClearChunkBit(dev, block, page);
7183 +
7184 + bi->pagesInUse--;
7185 +
7186 + if (bi->pagesInUse == 0 &&
7187 + !bi->hasShrinkHeader &&
7188 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7189 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7190 + yaffs_BlockBecameDirty(dev, block);
7191 + }
7192 +
7193 + } else {
7194 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
7195 + }
7196 +
7197 +}
7198 +
7199 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
7200 + const __u8 * buffer, int nBytes,
7201 + int useReserve)
7202 +{
7203 + /* Find old chunk Need to do this to get serial number
7204 + * Write new one and patch into tree.
7205 + * Invalidate old tags.
7206 + */
7207 +
7208 + int prevChunkId;
7209 + yaffs_ExtendedTags prevTags;
7210 +
7211 + int newChunkId;
7212 + yaffs_ExtendedTags newTags;
7213 +
7214 + yaffs_Device *dev = in->myDev;
7215 +
7216 + yaffs_CheckGarbageCollection(dev);
7217 +
7218 + /* Get the previous chunk at this location in the file if it exists */
7219 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7220 +
7221 + /* Set up new tags */
7222 + yaffs_InitialiseTags(&newTags);
7223 +
7224 + newTags.chunkId = chunkInInode;
7225 + newTags.objectId = in->objectId;
7226 + newTags.serialNumber =
7227 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7228 + newTags.byteCount = nBytes;
7229 +
7230 + newChunkId =
7231 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7232 + useReserve);
7233 +
7234 + if (newChunkId >= 0) {
7235 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7236 +
7237 + if (prevChunkId >= 0) {
7238 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7239 +
7240 + }
7241 +
7242 + yaffs_CheckFileSanity(in);
7243 + }
7244 + return newChunkId;
7245 +
7246 +}
7247 +
7248 +/* UpdateObjectHeader updates the header on NAND for an object.
7249 + * If name is not NULL, then that new name is used.
7250 + */
7251 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
7252 + int isShrink, int shadows)
7253 +{
7254 +
7255 + yaffs_BlockInfo *bi;
7256 +
7257 + yaffs_Device *dev = in->myDev;
7258 +
7259 + int prevChunkId;
7260 + int retVal = 0;
7261 + int result = 0;
7262 +
7263 + int newChunkId;
7264 + yaffs_ExtendedTags newTags;
7265 + yaffs_ExtendedTags oldTags;
7266 +
7267 + __u8 *buffer = NULL;
7268 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7269 +
7270 + yaffs_ObjectHeader *oh = NULL;
7271 +
7272 + yaffs_strcpy(oldName,"silly old name");
7273 +
7274 + if (!in->fake || force) {
7275 +
7276 + yaffs_CheckGarbageCollection(dev);
7277 + yaffs_CheckObjectDetailsLoaded(in);
7278 +
7279 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7280 + oh = (yaffs_ObjectHeader *) buffer;
7281 +
7282 + prevChunkId = in->chunkId;
7283 +
7284 + if (prevChunkId >= 0) {
7285 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7286 + buffer, &oldTags);
7287 +
7288 + yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
7289 +
7290 + memcpy(oldName, oh->name, sizeof(oh->name));
7291 + }
7292 +
7293 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7294 +
7295 + oh->type = in->variantType;
7296 + oh->yst_mode = in->yst_mode;
7297 + oh->shadowsObject = shadows;
7298 +
7299 +#ifdef CONFIG_YAFFS_WINCE
7300 + oh->win_atime[0] = in->win_atime[0];
7301 + oh->win_ctime[0] = in->win_ctime[0];
7302 + oh->win_mtime[0] = in->win_mtime[0];
7303 + oh->win_atime[1] = in->win_atime[1];
7304 + oh->win_ctime[1] = in->win_ctime[1];
7305 + oh->win_mtime[1] = in->win_mtime[1];
7306 +#else
7307 + oh->yst_uid = in->yst_uid;
7308 + oh->yst_gid = in->yst_gid;
7309 + oh->yst_atime = in->yst_atime;
7310 + oh->yst_mtime = in->yst_mtime;
7311 + oh->yst_ctime = in->yst_ctime;
7312 + oh->yst_rdev = in->yst_rdev;
7313 +#endif
7314 + if (in->parent) {
7315 + oh->parentObjectId = in->parent->objectId;
7316 + } else {
7317 + oh->parentObjectId = 0;
7318 + }
7319 +
7320 + if (name && *name) {
7321 + memset(oh->name, 0, sizeof(oh->name));
7322 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7323 + } else if (prevChunkId>=0) {
7324 + memcpy(oh->name, oldName, sizeof(oh->name));
7325 + } else {
7326 + memset(oh->name, 0, sizeof(oh->name));
7327 + }
7328 +
7329 + oh->isShrink = isShrink;
7330 +
7331 + switch (in->variantType) {
7332 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7333 + /* Should not happen */
7334 + break;
7335 + case YAFFS_OBJECT_TYPE_FILE:
7336 + oh->fileSize =
7337 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7338 + || oh->parentObjectId ==
7339 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7340 + fileVariant.fileSize;
7341 + break;
7342 + case YAFFS_OBJECT_TYPE_HARDLINK:
7343 + oh->equivalentObjectId =
7344 + in->variant.hardLinkVariant.equivalentObjectId;
7345 + break;
7346 + case YAFFS_OBJECT_TYPE_SPECIAL:
7347 + /* Do nothing */
7348 + break;
7349 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7350 + /* Do nothing */
7351 + break;
7352 + case YAFFS_OBJECT_TYPE_SYMLINK:
7353 + yaffs_strncpy(oh->alias,
7354 + in->variant.symLinkVariant.alias,
7355 + YAFFS_MAX_ALIAS_LENGTH);
7356 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7357 + break;
7358 + }
7359 +
7360 + /* Tags */
7361 + yaffs_InitialiseTags(&newTags);
7362 + in->serial++;
7363 + newTags.chunkId = 0;
7364 + newTags.objectId = in->objectId;
7365 + newTags.serialNumber = in->serial;
7366 +
7367 + /* Add extra info for file header */
7368 +
7369 + newTags.extraHeaderInfoAvailable = 1;
7370 + newTags.extraParentObjectId = oh->parentObjectId;
7371 + newTags.extraFileLength = oh->fileSize;
7372 + newTags.extraIsShrinkHeader = oh->isShrink;
7373 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7374 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7375 + newTags.extraObjectType = in->variantType;
7376 +
7377 + yaffs_VerifyObjectHeader(in,oh,&newTags,1);
7378 +
7379 + /* Create new chunk in NAND */
7380 + newChunkId =
7381 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7382 + (prevChunkId >= 0) ? 1 : 0);
7383 +
7384 + if (newChunkId >= 0) {
7385 +
7386 + in->chunkId = newChunkId;
7387 +
7388 + if (prevChunkId >= 0) {
7389 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7390 + __LINE__);
7391 + }
7392 +
7393 + if(!yaffs_ObjectHasCachedWriteData(in))
7394 + in->dirty = 0;
7395 +
7396 + /* If this was a shrink, then mark the block that the chunk lives on */
7397 + if (isShrink) {
7398 + bi = yaffs_GetBlockInfo(in->myDev,
7399 + newChunkId /in->myDev-> nChunksPerBlock);
7400 + bi->hasShrinkHeader = 1;
7401 + }
7402 +
7403 + }
7404 +
7405 + retVal = newChunkId;
7406 +
7407 + }
7408 +
7409 + if (buffer)
7410 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7411 +
7412 + return retVal;
7413 +}
7414 +
7415 +/*------------------------ Short Operations Cache ----------------------------------------
7416 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7417 + * reads might be short sequential reads, and a lot of writes may be short
7418 + * sequential writes. eg. scanning/writing a jpeg file.
7419 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7420 + * with dumb-as-a-rock code.
7421 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7422 + * buffering.
7423 + *
7424 + * There are a limited number (~10) of cache chunks per device so that we don't
7425 + * need a very intelligent search.
7426 + */
7427 +
7428 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7429 +{
7430 + yaffs_Device *dev = obj->myDev;
7431 + int i;
7432 + yaffs_ChunkCache *cache;
7433 + int nCaches = obj->myDev->nShortOpCaches;
7434 +
7435 + for(i = 0; i < nCaches; i++){
7436 + cache = &dev->srCache[i];
7437 + if (cache->object == obj &&
7438 + cache->dirty)
7439 + return 1;
7440 + }
7441 +
7442 + return 0;
7443 +}
7444 +
7445 +
7446 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
7447 +{
7448 + yaffs_Device *dev = obj->myDev;
7449 + int lowest = -99; /* Stop compiler whining. */
7450 + int i;
7451 + yaffs_ChunkCache *cache;
7452 + int chunkWritten = 0;
7453 + int nCaches = obj->myDev->nShortOpCaches;
7454 +
7455 + if (nCaches > 0) {
7456 + do {
7457 + cache = NULL;
7458 +
7459 + /* Find the dirty cache for this object with the lowest chunk id. */
7460 + for (i = 0; i < nCaches; i++) {
7461 + if (dev->srCache[i].object == obj &&
7462 + dev->srCache[i].dirty) {
7463 + if (!cache
7464 + || dev->srCache[i].chunkId <
7465 + lowest) {
7466 + cache = &dev->srCache[i];
7467 + lowest = cache->chunkId;
7468 + }
7469 + }
7470 + }
7471 +
7472 + if (cache && !cache->locked) {
7473 + /* Write it out and free it up */
7474 +
7475 + chunkWritten =
7476 + yaffs_WriteChunkDataToObject(cache->object,
7477 + cache->chunkId,
7478 + cache->data,
7479 + cache->nBytes,
7480 + 1);
7481 + cache->dirty = 0;
7482 + cache->object = NULL;
7483 + }
7484 +
7485 + } while (cache && chunkWritten > 0);
7486 +
7487 + if (cache) {
7488 + /* Hoosterman, disk full while writing cache out. */
7489 + T(YAFFS_TRACE_ERROR,
7490 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7491 +
7492 + }
7493 + }
7494 +
7495 +}
7496 +
7497 +/*yaffs_FlushEntireDeviceCache(dev)
7498 + *
7499 + *
7500 + */
7501 +
7502 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7503 +{
7504 + yaffs_Object *obj;
7505 + int nCaches = dev->nShortOpCaches;
7506 + int i;
7507 +
7508 + /* Find a dirty object in the cache and flush it...
7509 + * until there are no further dirty objects.
7510 + */
7511 + do {
7512 + obj = NULL;
7513 + for( i = 0; i < nCaches && !obj; i++) {
7514 + if (dev->srCache[i].object &&
7515 + dev->srCache[i].dirty)
7516 + obj = dev->srCache[i].object;
7517 +
7518 + }
7519 + if(obj)
7520 + yaffs_FlushFilesChunkCache(obj);
7521 +
7522 + } while(obj);
7523 +
7524 +}
7525 +
7526 +
7527 +/* Grab us a cache chunk for use.
7528 + * First look for an empty one.
7529 + * Then look for the least recently used non-dirty one.
7530 + * Then look for the least recently used dirty one...., flush and look again.
7531 + */
7532 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
7533 +{
7534 + int i;
7535 + int usage;
7536 + int theOne;
7537 +
7538 + if (dev->nShortOpCaches > 0) {
7539 + for (i = 0; i < dev->nShortOpCaches; i++) {
7540 + if (!dev->srCache[i].object)
7541 + return &dev->srCache[i];
7542 + }
7543 +
7544 + return NULL;
7545 +
7546 + theOne = -1;
7547 + usage = 0; /* just to stop the compiler grizzling */
7548 +
7549 + for (i = 0; i < dev->nShortOpCaches; i++) {
7550 + if (!dev->srCache[i].dirty &&
7551 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
7552 + theOne < 0)) {
7553 + usage = dev->srCache[i].lastUse;
7554 + theOne = i;
7555 + }
7556 + }
7557 +
7558 +
7559 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
7560 + } else {
7561 + return NULL;
7562 + }
7563 +
7564 +}
7565 +
7566 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
7567 +{
7568 + yaffs_ChunkCache *cache;
7569 + yaffs_Object *theObj;
7570 + int usage;
7571 + int i;
7572 + int pushout;
7573 +
7574 + if (dev->nShortOpCaches > 0) {
7575 + /* Try find a non-dirty one... */
7576 +
7577 + cache = yaffs_GrabChunkCacheWorker(dev);
7578 +
7579 + if (!cache) {
7580 + /* They were all dirty, find the last recently used object and flush
7581 + * its cache, then find again.
7582 + * NB what's here is not very accurate, we actually flush the object
7583 + * the last recently used page.
7584 + */
7585 +
7586 + /* With locking we can't assume we can use entry zero */
7587 +
7588 + theObj = NULL;
7589 + usage = -1;
7590 + cache = NULL;
7591 + pushout = -1;
7592 +
7593 + for (i = 0; i < dev->nShortOpCaches; i++) {
7594 + if (dev->srCache[i].object &&
7595 + !dev->srCache[i].locked &&
7596 + (dev->srCache[i].lastUse < usage || !cache))
7597 + {
7598 + usage = dev->srCache[i].lastUse;
7599 + theObj = dev->srCache[i].object;
7600 + cache = &dev->srCache[i];
7601 + pushout = i;
7602 + }
7603 + }
7604 +
7605 + if (!cache || cache->dirty) {
7606 + /* Flush and try again */
7607 + yaffs_FlushFilesChunkCache(theObj);
7608 + cache = yaffs_GrabChunkCacheWorker(dev);
7609 + }
7610 +
7611 + }
7612 + return cache;
7613 + } else
7614 + return NULL;
7615 +
7616 +}
7617 +
7618 +/* Find a cached chunk */
7619 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
7620 + int chunkId)
7621 +{
7622 + yaffs_Device *dev = obj->myDev;
7623 + int i;
7624 + if (dev->nShortOpCaches > 0) {
7625 + for (i = 0; i < dev->nShortOpCaches; i++) {
7626 + if (dev->srCache[i].object == obj &&
7627 + dev->srCache[i].chunkId == chunkId) {
7628 + dev->cacheHits++;
7629 +
7630 + return &dev->srCache[i];
7631 + }
7632 + }
7633 + }
7634 + return NULL;
7635 +}
7636 +
7637 +/* Mark the chunk for the least recently used algorithym */
7638 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
7639 + int isAWrite)
7640 +{
7641 +
7642 + if (dev->nShortOpCaches > 0) {
7643 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
7644 + /* Reset the cache usages */
7645 + int i;
7646 + for (i = 1; i < dev->nShortOpCaches; i++) {
7647 + dev->srCache[i].lastUse = 0;
7648 + }
7649 + dev->srLastUse = 0;
7650 + }
7651 +
7652 + dev->srLastUse++;
7653 +
7654 + cache->lastUse = dev->srLastUse;
7655 +
7656 + if (isAWrite) {
7657 + cache->dirty = 1;
7658 + }
7659 + }
7660 +}
7661 +
7662 +/* Invalidate a single cache page.
7663 + * Do this when a whole page gets written,
7664 + * ie the short cache for this page is no longer valid.
7665 + */
7666 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
7667 +{
7668 + if (object->myDev->nShortOpCaches > 0) {
7669 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
7670 +
7671 + if (cache) {
7672 + cache->object = NULL;
7673 + }
7674 + }
7675 +}
7676 +
7677 +/* Invalidate all the cache pages associated with this object
7678 + * Do this whenever ther file is deleted or resized.
7679 + */
7680 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
7681 +{
7682 + int i;
7683 + yaffs_Device *dev = in->myDev;
7684 +
7685 + if (dev->nShortOpCaches > 0) {
7686 + /* Invalidate it. */
7687 + for (i = 0; i < dev->nShortOpCaches; i++) {
7688 + if (dev->srCache[i].object == in) {
7689 + dev->srCache[i].object = NULL;
7690 + }
7691 + }
7692 + }
7693 +}
7694 +
7695 +/*--------------------- Checkpointing --------------------*/
7696 +
7697 +
7698 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
7699 +{
7700 + yaffs_CheckpointValidity cp;
7701 +
7702 + memset(&cp,0,sizeof(cp));
7703 +
7704 + cp.structType = sizeof(cp);
7705 + cp.magic = YAFFS_MAGIC;
7706 + cp.version = YAFFS_CHECKPOINT_VERSION;
7707 + cp.head = (head) ? 1 : 0;
7708 +
7709 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
7710 + 1 : 0;
7711 +}
7712 +
7713 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
7714 +{
7715 + yaffs_CheckpointValidity cp;
7716 + int ok;
7717 +
7718 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7719 +
7720 + if(ok)
7721 + ok = (cp.structType == sizeof(cp)) &&
7722 + (cp.magic == YAFFS_MAGIC) &&
7723 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
7724 + (cp.head == ((head) ? 1 : 0));
7725 + return ok ? 1 : 0;
7726 +}
7727 +
7728 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
7729 + yaffs_Device *dev)
7730 +{
7731 + cp->nErasedBlocks = dev->nErasedBlocks;
7732 + cp->allocationBlock = dev->allocationBlock;
7733 + cp->allocationPage = dev->allocationPage;
7734 + cp->nFreeChunks = dev->nFreeChunks;
7735 +
7736 + cp->nDeletedFiles = dev->nDeletedFiles;
7737 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
7738 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
7739 + cp->sequenceNumber = dev->sequenceNumber;
7740 + cp->oldestDirtySequence = dev->oldestDirtySequence;
7741 +
7742 +}
7743 +
7744 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
7745 + yaffs_CheckpointDevice *cp)
7746 +{
7747 + dev->nErasedBlocks = cp->nErasedBlocks;
7748 + dev->allocationBlock = cp->allocationBlock;
7749 + dev->allocationPage = cp->allocationPage;
7750 + dev->nFreeChunks = cp->nFreeChunks;
7751 +
7752 + dev->nDeletedFiles = cp->nDeletedFiles;
7753 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
7754 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
7755 + dev->sequenceNumber = cp->sequenceNumber;
7756 + dev->oldestDirtySequence = cp->oldestDirtySequence;
7757 +}
7758 +
7759 +
7760 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
7761 +{
7762 + yaffs_CheckpointDevice cp;
7763 + __u32 nBytes;
7764 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7765 +
7766 + int ok;
7767 +
7768 + /* Write device runtime values*/
7769 + yaffs_DeviceToCheckpointDevice(&cp,dev);
7770 + cp.structType = sizeof(cp);
7771 +
7772 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7773 +
7774 + /* Write block info */
7775 + if(ok) {
7776 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7777 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
7778 + }
7779 +
7780 + /* Write chunk bits */
7781 + if(ok) {
7782 + nBytes = nBlocks * dev->chunkBitmapStride;
7783 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
7784 + }
7785 + return ok ? 1 : 0;
7786 +
7787 +}
7788 +
7789 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
7790 +{
7791 + yaffs_CheckpointDevice cp;
7792 + __u32 nBytes;
7793 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7794 +
7795 + int ok;
7796 +
7797 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7798 + if(!ok)
7799 + return 0;
7800 +
7801 + if(cp.structType != sizeof(cp))
7802 + return 0;
7803 +
7804 +
7805 + yaffs_CheckpointDeviceToDevice(dev,&cp);
7806 +
7807 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7808 +
7809 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
7810 +
7811 + if(!ok)
7812 + return 0;
7813 + nBytes = nBlocks * dev->chunkBitmapStride;
7814 +
7815 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
7816 +
7817 + return ok ? 1 : 0;
7818 +}
7819 +
7820 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
7821 + yaffs_Object *obj)
7822 +{
7823 +
7824 + cp->objectId = obj->objectId;
7825 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
7826 + cp->chunkId = obj->chunkId;
7827 + cp->variantType = obj->variantType;
7828 + cp->deleted = obj->deleted;
7829 + cp->softDeleted = obj->softDeleted;
7830 + cp->unlinked = obj->unlinked;
7831 + cp->fake = obj->fake;
7832 + cp->renameAllowed = obj->renameAllowed;
7833 + cp->unlinkAllowed = obj->unlinkAllowed;
7834 + cp->serial = obj->serial;
7835 + cp->nDataChunks = obj->nDataChunks;
7836 +
7837 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7838 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7839 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7840 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7841 +}
7842 +
7843 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7844 +{
7845 +
7846 + yaffs_Object *parent;
7847 +
7848 + obj->objectId = cp->objectId;
7849 +
7850 + if(cp->parentId)
7851 + parent = yaffs_FindOrCreateObjectByNumber(
7852 + obj->myDev,
7853 + cp->parentId,
7854 + YAFFS_OBJECT_TYPE_DIRECTORY);
7855 + else
7856 + parent = NULL;
7857 +
7858 + if(parent)
7859 + yaffs_AddObjectToDirectory(parent, obj);
7860 +
7861 + obj->chunkId = cp->chunkId;
7862 + obj->variantType = cp->variantType;
7863 + obj->deleted = cp->deleted;
7864 + obj->softDeleted = cp->softDeleted;
7865 + obj->unlinked = cp->unlinked;
7866 + obj->fake = cp->fake;
7867 + obj->renameAllowed = cp->renameAllowed;
7868 + obj->unlinkAllowed = cp->unlinkAllowed;
7869 + obj->serial = cp->serial;
7870 + obj->nDataChunks = cp->nDataChunks;
7871 +
7872 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7873 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7874 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7875 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7876 +
7877 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7878 + obj->lazyLoaded = 1;
7879 +}
7880 +
7881 +
7882 +
7883 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7884 + __u32 level, int chunkOffset)
7885 +{
7886 + int i;
7887 + yaffs_Device *dev = in->myDev;
7888 + int ok = 1;
7889 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7890 +
7891 + if (tn) {
7892 + if (level > 0) {
7893 +
7894 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7895 + if (tn->internal[i]) {
7896 + ok = yaffs_CheckpointTnodeWorker(in,
7897 + tn->internal[i],
7898 + level - 1,
7899 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7900 + }
7901 + }
7902 + } else if (level == 0) {
7903 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7904 + /* printf("write tnode at %d\n",baseOffset); */
7905 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7906 + if(ok)
7907 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7908 + }
7909 + }
7910 +
7911 + return ok;
7912 +
7913 +}
7914 +
7915 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7916 +{
7917 + __u32 endMarker = ~0;
7918 + int ok = 1;
7919 +
7920 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7921 + ok = yaffs_CheckpointTnodeWorker(obj,
7922 + obj->variant.fileVariant.top,
7923 + obj->variant.fileVariant.topLevel,
7924 + 0);
7925 + if(ok)
7926 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7927 + sizeof(endMarker));
7928 + }
7929 +
7930 + return ok ? 1 : 0;
7931 +}
7932 +
7933 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7934 +{
7935 + __u32 baseChunk;
7936 + int ok = 1;
7937 + yaffs_Device *dev = obj->myDev;
7938 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7939 + yaffs_Tnode *tn;
7940 + int nread = 0;
7941 +
7942 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7943 +
7944 + while(ok && (~baseChunk)){
7945 + nread++;
7946 + /* Read level 0 tnode */
7947 +
7948 +
7949 + /* printf("read tnode at %d\n",baseChunk); */
7950 + tn = yaffs_GetTnodeRaw(dev);
7951 + if(tn)
7952 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7953 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7954 + else
7955 + ok = 0;
7956 +
7957 + if(tn && ok){
7958 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7959 + fileStructPtr,
7960 + baseChunk,
7961 + tn) ? 1 : 0;
7962 +
7963 + }
7964 +
7965 + if(ok)
7966 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7967 +
7968 + }
7969 +
7970 + T(YAFFS_TRACE_CHECKPOINT,(
7971 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
7972 + nread,baseChunk,ok));
7973 +
7974 + return ok ? 1 : 0;
7975 +}
7976 +
7977 +
7978 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7979 +{
7980 + yaffs_Object *obj;
7981 + yaffs_CheckpointObject cp;
7982 + int i;
7983 + int ok = 1;
7984 + struct list_head *lh;
7985 +
7986 +
7987 + /* Iterate through the objects in each hash entry,
7988 + * dumping them to the checkpointing stream.
7989 + */
7990 +
7991 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
7992 + list_for_each(lh, &dev->objectBucket[i].list) {
7993 + if (lh) {
7994 + obj = list_entry(lh, yaffs_Object, hashLink);
7995 + if (!obj->deferedFree) {
7996 + yaffs_ObjectToCheckpointObject(&cp,obj);
7997 + cp.structType = sizeof(cp);
7998 +
7999 + T(YAFFS_TRACE_CHECKPOINT,(
8000 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
8001 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
8002 +
8003 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8004 +
8005 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
8006 + ok = yaffs_WriteCheckpointTnodes(obj);
8007 + }
8008 + }
8009 + }
8010 + }
8011 + }
8012 +
8013 + /* Dump end of list */
8014 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
8015 + cp.structType = sizeof(cp);
8016 +
8017 + if(ok)
8018 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8019 +
8020 + return ok ? 1 : 0;
8021 +}
8022 +
8023 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8024 +{
8025 + yaffs_Object *obj;
8026 + yaffs_CheckpointObject cp;
8027 + int ok = 1;
8028 + int done = 0;
8029 + yaffs_Object *hardList = NULL;
8030 +
8031 + while(ok && !done) {
8032 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
8033 + if(cp.structType != sizeof(cp)) {
8034 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
8035 + cp.structType,sizeof(cp),ok));
8036 + ok = 0;
8037 + }
8038 +
8039 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8040 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
8041 +
8042 + if(ok && cp.objectId == ~0)
8043 + done = 1;
8044 + else if(ok){
8045 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
8046 + if(obj) {
8047 + yaffs_CheckpointObjectToObject(obj,&cp);
8048 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8049 + ok = yaffs_ReadCheckpointTnodes(obj);
8050 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8051 + obj->hardLinks.next =
8052 + (struct list_head *)
8053 + hardList;
8054 + hardList = obj;
8055 + }
8056 +
8057 + }
8058 + }
8059 + }
8060 +
8061 + if(ok)
8062 + yaffs_HardlinkFixup(dev,hardList);
8063 +
8064 + return ok ? 1 : 0;
8065 +}
8066 +
8067 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8068 +{
8069 + __u32 checkpointSum;
8070 + int ok;
8071 +
8072 + yaffs_GetCheckpointSum(dev,&checkpointSum);
8073 +
8074 + ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
8075 +
8076 + if(!ok)
8077 + return 0;
8078 +
8079 + return 1;
8080 +}
8081 +
8082 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8083 +{
8084 + __u32 checkpointSum0;
8085 + __u32 checkpointSum1;
8086 + int ok;
8087 +
8088 + yaffs_GetCheckpointSum(dev,&checkpointSum0);
8089 +
8090 + ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8091 +
8092 + if(!ok)
8093 + return 0;
8094 +
8095 + if(checkpointSum0 != checkpointSum1)
8096 + return 0;
8097 +
8098 + return 1;
8099 +}
8100 +
8101 +
8102 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8103 +{
8104 +
8105 + int ok = 1;
8106 +
8107 + if(dev->skipCheckpointWrite || !dev->isYaffs2){
8108 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
8109 + ok = 0;
8110 + }
8111 +
8112 + if(ok)
8113 + ok = yaffs_CheckpointOpen(dev,1);
8114 +
8115 + if(ok){
8116 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8117 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
8118 + }
8119 + if(ok){
8120 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
8121 + ok = yaffs_WriteCheckpointDevice(dev);
8122 + }
8123 + if(ok){
8124 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
8125 + ok = yaffs_WriteCheckpointObjects(dev);
8126 + }
8127 + if(ok){
8128 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8129 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
8130 + }
8131 +
8132 + if(ok){
8133 + ok = yaffs_WriteCheckpointSum(dev);
8134 + }
8135 +
8136 +
8137 + if(!yaffs_CheckpointClose(dev))
8138 + ok = 0;
8139 +
8140 + if(ok)
8141 + dev->isCheckpointed = 1;
8142 + else
8143 + dev->isCheckpointed = 0;
8144 +
8145 + return dev->isCheckpointed;
8146 +}
8147 +
8148 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8149 +{
8150 + int ok = 1;
8151 +
8152 + if(dev->skipCheckpointRead || !dev->isYaffs2){
8153 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
8154 + ok = 0;
8155 + }
8156 +
8157 + if(ok)
8158 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
8159 +
8160 + if(ok){
8161 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8162 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
8163 + }
8164 + if(ok){
8165 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
8166 + ok = yaffs_ReadCheckpointDevice(dev);
8167 + }
8168 + if(ok){
8169 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
8170 + ok = yaffs_ReadCheckpointObjects(dev);
8171 + }
8172 + if(ok){
8173 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8174 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
8175 + }
8176 +
8177 + if(ok){
8178 + ok = yaffs_ReadCheckpointSum(dev);
8179 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
8180 + }
8181 +
8182 + if(!yaffs_CheckpointClose(dev))
8183 + ok = 0;
8184 +
8185 + if(ok)
8186 + dev->isCheckpointed = 1;
8187 + else
8188 + dev->isCheckpointed = 0;
8189 +
8190 + return ok ? 1 : 0;
8191 +
8192 +}
8193 +
8194 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8195 +{
8196 + if(dev->isCheckpointed ||
8197 + dev->blocksInCheckpoint > 0){
8198 + dev->isCheckpointed = 0;
8199 + yaffs_CheckpointInvalidateStream(dev);
8200 + if(dev->superBlock && dev->markSuperBlockDirty)
8201 + dev->markSuperBlockDirty(dev->superBlock);
8202 + }
8203 +}
8204 +
8205 +
8206 +int yaffs_CheckpointSave(yaffs_Device *dev)
8207 +{
8208 +
8209 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8210 +
8211 + yaffs_VerifyObjects(dev);
8212 + yaffs_VerifyBlocks(dev);
8213 + yaffs_VerifyFreeChunks(dev);
8214 +
8215 + if(!dev->isCheckpointed) {
8216 + yaffs_InvalidateCheckpoint(dev);
8217 + yaffs_WriteCheckpointData(dev);
8218 + }
8219 +
8220 + T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8221 +
8222 + return dev->isCheckpointed;
8223 +}
8224 +
8225 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8226 +{
8227 + int retval;
8228 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8229 +
8230 + retval = yaffs_ReadCheckpointData(dev);
8231 +
8232 + if(dev->isCheckpointed){
8233 + yaffs_VerifyObjects(dev);
8234 + yaffs_VerifyBlocks(dev);
8235 + yaffs_VerifyFreeChunks(dev);
8236 + }
8237 +
8238 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8239 +
8240 + return retval;
8241 +}
8242 +
8243 +/*--------------------- File read/write ------------------------
8244 + * Read and write have very similar structures.
8245 + * In general the read/write has three parts to it
8246 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8247 + * Some complete chunks
8248 + * An incomplete chunk to end off with
8249 + *
8250 + * Curve-balls: the first chunk might also be the last chunk.
8251 + */
8252 +
8253 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
8254 + int nBytes)
8255 +{
8256 +
8257 + int chunk;
8258 + int start;
8259 + int nToCopy;
8260 + int n = nBytes;
8261 + int nDone = 0;
8262 + yaffs_ChunkCache *cache;
8263 +
8264 + yaffs_Device *dev;
8265 +
8266 + dev = in->myDev;
8267 +
8268 + while (n > 0) {
8269 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8270 + //start = offset % dev->nDataBytesPerChunk;
8271 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8272 + chunk++;
8273 +
8274 + /* OK now check for the curveball where the start and end are in
8275 + * the same chunk.
8276 + */
8277 + if ((start + n) < dev->nDataBytesPerChunk) {
8278 + nToCopy = n;
8279 + } else {
8280 + nToCopy = dev->nDataBytesPerChunk - start;
8281 + }
8282 +
8283 + cache = yaffs_FindChunkCache(in, chunk);
8284 +
8285 + /* If the chunk is already in the cache or it is less than a whole chunk
8286 + * then use the cache (if there is caching)
8287 + * else bypass the cache.
8288 + */
8289 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
8290 + if (dev->nShortOpCaches > 0) {
8291 +
8292 + /* If we can't find the data in the cache, then load it up. */
8293 +
8294 + if (!cache) {
8295 + cache = yaffs_GrabChunkCache(in->myDev);
8296 + cache->object = in;
8297 + cache->chunkId = chunk;
8298 + cache->dirty = 0;
8299 + cache->locked = 0;
8300 + yaffs_ReadChunkDataFromObject(in, chunk,
8301 + cache->
8302 + data);
8303 + cache->nBytes = 0;
8304 + }
8305 +
8306 + yaffs_UseChunkCache(dev, cache, 0);
8307 +
8308 + cache->locked = 1;
8309 +
8310 +#ifdef CONFIG_YAFFS_WINCE
8311 + yfsd_UnlockYAFFS(TRUE);
8312 +#endif
8313 + memcpy(buffer, &cache->data[start], nToCopy);
8314 +
8315 +#ifdef CONFIG_YAFFS_WINCE
8316 + yfsd_LockYAFFS(TRUE);
8317 +#endif
8318 + cache->locked = 0;
8319 + } else {
8320 + /* Read into the local buffer then copy..*/
8321 +
8322 + __u8 *localBuffer =
8323 + yaffs_GetTempBuffer(dev, __LINE__);
8324 + yaffs_ReadChunkDataFromObject(in, chunk,
8325 + localBuffer);
8326 +#ifdef CONFIG_YAFFS_WINCE
8327 + yfsd_UnlockYAFFS(TRUE);
8328 +#endif
8329 + memcpy(buffer, &localBuffer[start], nToCopy);
8330 +
8331 +#ifdef CONFIG_YAFFS_WINCE
8332 + yfsd_LockYAFFS(TRUE);
8333 +#endif
8334 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8335 + __LINE__);
8336 + }
8337 +
8338 + } else {
8339 +#ifdef CONFIG_YAFFS_WINCE
8340 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8341 +
8342 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8343 + * This is because we otherwise screw up WinCE's memory mapper
8344 + */
8345 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
8346 +
8347 +#ifdef CONFIG_YAFFS_WINCE
8348 + yfsd_UnlockYAFFS(TRUE);
8349 +#endif
8350 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
8351 +
8352 +#ifdef CONFIG_YAFFS_WINCE
8353 + yfsd_LockYAFFS(TRUE);
8354 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8355 +#endif
8356 +
8357 +#else
8358 + /* A full chunk. Read directly into the supplied buffer. */
8359 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8360 +#endif
8361 + }
8362 +
8363 + n -= nToCopy;
8364 + offset += nToCopy;
8365 + buffer += nToCopy;
8366 + nDone += nToCopy;
8367 +
8368 + }
8369 +
8370 + return nDone;
8371 +}
8372 +
8373 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
8374 + int nBytes, int writeThrough)
8375 +{
8376 +
8377 + int chunk;
8378 + int start;
8379 + int nToCopy;
8380 + int n = nBytes;
8381 + int nDone = 0;
8382 + int nToWriteBack;
8383 + int startOfWrite = offset;
8384 + int chunkWritten = 0;
8385 + int nBytesRead;
8386 +
8387 + yaffs_Device *dev;
8388 +
8389 + dev = in->myDev;
8390 +
8391 + while (n > 0 && chunkWritten >= 0) {
8392 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8393 + //start = offset % dev->nDataBytesPerChunk;
8394 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8395 + chunk++;
8396 +
8397 + /* OK now check for the curveball where the start and end are in
8398 + * the same chunk.
8399 + */
8400 +
8401 + if ((start + n) < dev->nDataBytesPerChunk) {
8402 + nToCopy = n;
8403 +
8404 + /* Now folks, to calculate how many bytes to write back....
8405 + * If we're overwriting and not writing to then end of file then
8406 + * we need to write back as much as was there before.
8407 + */
8408 +
8409 + nBytesRead =
8410 + in->variant.fileVariant.fileSize -
8411 + ((chunk - 1) * dev->nDataBytesPerChunk);
8412 +
8413 + if (nBytesRead > dev->nDataBytesPerChunk) {
8414 + nBytesRead = dev->nDataBytesPerChunk;
8415 + }
8416 +
8417 + nToWriteBack =
8418 + (nBytesRead >
8419 + (start + n)) ? nBytesRead : (start + n);
8420 +
8421 + } else {
8422 + nToCopy = dev->nDataBytesPerChunk - start;
8423 + nToWriteBack = dev->nDataBytesPerChunk;
8424 + }
8425 +
8426 + if (nToCopy != dev->nDataBytesPerChunk) {
8427 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
8428 + if (dev->nShortOpCaches > 0) {
8429 + yaffs_ChunkCache *cache;
8430 + /* If we can't find the data in the cache, then load the cache */
8431 + cache = yaffs_FindChunkCache(in, chunk);
8432 +
8433 + if (!cache
8434 + && yaffs_CheckSpaceForAllocation(in->
8435 + myDev)) {
8436 + cache = yaffs_GrabChunkCache(in->myDev);
8437 + cache->object = in;
8438 + cache->chunkId = chunk;
8439 + cache->dirty = 0;
8440 + cache->locked = 0;
8441 + yaffs_ReadChunkDataFromObject(in, chunk,
8442 + cache->
8443 + data);
8444 + }
8445 + else if(cache &&
8446 + !cache->dirty &&
8447 + !yaffs_CheckSpaceForAllocation(in->myDev)){
8448 + /* Drop the cache if it was a read cache item and
8449 + * no space check has been made for it.
8450 + */
8451 + cache = NULL;
8452 + }
8453 +
8454 + if (cache) {
8455 + yaffs_UseChunkCache(dev, cache, 1);
8456 + cache->locked = 1;
8457 +#ifdef CONFIG_YAFFS_WINCE
8458 + yfsd_UnlockYAFFS(TRUE);
8459 +#endif
8460 +
8461 + memcpy(&cache->data[start], buffer,
8462 + nToCopy);
8463 +
8464 +#ifdef CONFIG_YAFFS_WINCE
8465 + yfsd_LockYAFFS(TRUE);
8466 +#endif
8467 + cache->locked = 0;
8468 + cache->nBytes = nToWriteBack;
8469 +
8470 + if (writeThrough) {
8471 + chunkWritten =
8472 + yaffs_WriteChunkDataToObject
8473 + (cache->object,
8474 + cache->chunkId,
8475 + cache->data, cache->nBytes,
8476 + 1);
8477 + cache->dirty = 0;
8478 + }
8479 +
8480 + } else {
8481 + chunkWritten = -1; /* fail the write */
8482 + }
8483 + } else {
8484 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8485 + * Read into the local buffer then copy, then copy over and write back.
8486 + */
8487 +
8488 + __u8 *localBuffer =
8489 + yaffs_GetTempBuffer(dev, __LINE__);
8490 +
8491 + yaffs_ReadChunkDataFromObject(in, chunk,
8492 + localBuffer);
8493 +
8494 +#ifdef CONFIG_YAFFS_WINCE
8495 + yfsd_UnlockYAFFS(TRUE);
8496 +#endif
8497 +
8498 + memcpy(&localBuffer[start], buffer, nToCopy);
8499 +
8500 +#ifdef CONFIG_YAFFS_WINCE
8501 + yfsd_LockYAFFS(TRUE);
8502 +#endif
8503 + chunkWritten =
8504 + yaffs_WriteChunkDataToObject(in, chunk,
8505 + localBuffer,
8506 + nToWriteBack,
8507 + 0);
8508 +
8509 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8510 + __LINE__);
8511 +
8512 + }
8513 +
8514 + } else {
8515 +
8516 +#ifdef CONFIG_YAFFS_WINCE
8517 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8518 + * This is because we otherwise screw up WinCE's memory mapper
8519 + */
8520 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8521 +#ifdef CONFIG_YAFFS_WINCE
8522 + yfsd_UnlockYAFFS(TRUE);
8523 +#endif
8524 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
8525 +#ifdef CONFIG_YAFFS_WINCE
8526 + yfsd_LockYAFFS(TRUE);
8527 +#endif
8528 + chunkWritten =
8529 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
8530 + dev->nDataBytesPerChunk,
8531 + 0);
8532 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8533 +#else
8534 + /* A full chunk. Write directly from the supplied buffer. */
8535 + chunkWritten =
8536 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8537 + dev->nDataBytesPerChunk,
8538 + 0);
8539 +#endif
8540 + /* Since we've overwritten the cached data, we better invalidate it. */
8541 + yaffs_InvalidateChunkCache(in, chunk);
8542 + }
8543 +
8544 + if (chunkWritten >= 0) {
8545 + n -= nToCopy;
8546 + offset += nToCopy;
8547 + buffer += nToCopy;
8548 + nDone += nToCopy;
8549 + }
8550 +
8551 + }
8552 +
8553 + /* Update file object */
8554 +
8555 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
8556 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8557 + }
8558 +
8559 + in->dirty = 1;
8560 +
8561 + return nDone;
8562 +}
8563 +
8564 +
8565 +/* ---------------------- File resizing stuff ------------------ */
8566 +
8567 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
8568 +{
8569 +
8570 + yaffs_Device *dev = in->myDev;
8571 + int oldFileSize = in->variant.fileVariant.fileSize;
8572 +
8573 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8574 +
8575 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8576 + dev->nDataBytesPerChunk;
8577 + int i;
8578 + int chunkId;
8579 +
8580 + /* Delete backwards so that we don't end up with holes if
8581 + * power is lost part-way through the operation.
8582 + */
8583 + for (i = lastDel; i >= startDel; i--) {
8584 + /* NB this could be optimised somewhat,
8585 + * eg. could retrieve the tags and write them without
8586 + * using yaffs_DeleteChunk
8587 + */
8588 +
8589 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8590 + if (chunkId > 0) {
8591 + if (chunkId <
8592 + (dev->internalStartBlock * dev->nChunksPerBlock)
8593 + || chunkId >=
8594 + ((dev->internalEndBlock +
8595 + 1) * dev->nChunksPerBlock)) {
8596 + T(YAFFS_TRACE_ALWAYS,
8597 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8598 + chunkId, i));
8599 + } else {
8600 + in->nDataChunks--;
8601 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8602 + }
8603 + }
8604 + }
8605 +
8606 +}
8607 +
8608 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
8609 +{
8610 +
8611 + int oldFileSize = in->variant.fileVariant.fileSize;
8612 + int newSizeOfPartialChunk;
8613 + int newFullChunks;
8614 +
8615 + yaffs_Device *dev = in->myDev;
8616 +
8617 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8618 +
8619 + yaffs_FlushFilesChunkCache(in);
8620 + yaffs_InvalidateWholeChunkCache(in);
8621 +
8622 + yaffs_CheckGarbageCollection(dev);
8623 +
8624 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8625 + return yaffs_GetFileSize(in);
8626 + }
8627 +
8628 + if (newSize == oldFileSize) {
8629 + return oldFileSize;
8630 + }
8631 +
8632 + if (newSize < oldFileSize) {
8633 +
8634 + yaffs_PruneResizedChunks(in, newSize);
8635 +
8636 + if (newSizeOfPartialChunk != 0) {
8637 + int lastChunk = 1 + newFullChunks;
8638 +
8639 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8640 +
8641 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8642 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8643 + localBuffer);
8644 +
8645 + memset(localBuffer + newSizeOfPartialChunk, 0,
8646 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8647 +
8648 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8649 + newSizeOfPartialChunk, 1);
8650 +
8651 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8652 + }
8653 +
8654 + in->variant.fileVariant.fileSize = newSize;
8655 +
8656 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
8657 + } else {
8658 + /* newsSize > oldFileSize */
8659 + in->variant.fileVariant.fileSize = newSize;
8660 + }
8661 +
8662 +
8663 +
8664 + /* Write a new object header.
8665 + * show we've shrunk the file, if need be
8666 + * Do this only if the file is not in the deleted directories.
8667 + */
8668 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
8669 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
8670 + yaffs_UpdateObjectHeader(in, NULL, 0,
8671 + (newSize < oldFileSize) ? 1 : 0, 0);
8672 + }
8673 +
8674 + return newSize;
8675 +}
8676 +
8677 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
8678 +{
8679 + obj = yaffs_GetEquivalentObject(obj);
8680 +
8681 + switch (obj->variantType) {
8682 + case YAFFS_OBJECT_TYPE_FILE:
8683 + return obj->variant.fileVariant.fileSize;
8684 + case YAFFS_OBJECT_TYPE_SYMLINK:
8685 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
8686 + default:
8687 + return 0;
8688 + }
8689 +}
8690 +
8691 +
8692 +
8693 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
8694 +{
8695 + int retVal;
8696 + if (in->dirty) {
8697 + yaffs_FlushFilesChunkCache(in);
8698 + if (updateTime) {
8699 +#ifdef CONFIG_YAFFS_WINCE
8700 + yfsd_WinFileTimeNow(in->win_mtime);
8701 +#else
8702 +
8703 + in->yst_mtime = Y_CURRENT_TIME;
8704 +
8705 +#endif
8706 + }
8707 +
8708 + retVal =
8709 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
8710 + 0) ? YAFFS_OK : YAFFS_FAIL;
8711 + } else {
8712 + retVal = YAFFS_OK;
8713 + }
8714 +
8715 + return retVal;
8716 +
8717 +}
8718 +
8719 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
8720 +{
8721 +
8722 + /* First off, invalidate the file's data in the cache, without flushing. */
8723 + yaffs_InvalidateWholeChunkCache(in);
8724 +
8725 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
8726 + /* Move to the unlinked directory so we have a record that it was deleted. */
8727 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
8728 +
8729 + }
8730 +
8731 + yaffs_RemoveObjectFromDirectory(in);
8732 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
8733 + in->chunkId = -1;
8734 +
8735 + yaffs_FreeObject(in);
8736 + return YAFFS_OK;
8737 +
8738 +}
8739 +
8740 +/* yaffs_DeleteFile deletes the whole file data
8741 + * and the inode associated with the file.
8742 + * It does not delete the links associated with the file.
8743 + */
8744 +static int yaffs_UnlinkFile(yaffs_Object * in)
8745 +{
8746 +
8747 + int retVal;
8748 + int immediateDeletion = 0;
8749 +
8750 + if (1) {
8751 +#ifdef __KERNEL__
8752 + if (!in->myInode) {
8753 + immediateDeletion = 1;
8754 +
8755 + }
8756 +#else
8757 + if (in->inUse <= 0) {
8758 + immediateDeletion = 1;
8759 +
8760 + }
8761 +#endif
8762 + if (immediateDeletion) {
8763 + retVal =
8764 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
8765 + "deleted", 0, 0);
8766 + T(YAFFS_TRACE_TRACING,
8767 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
8768 + in->objectId));
8769 + in->deleted = 1;
8770 + in->myDev->nDeletedFiles++;
8771 + if (0 && in->myDev->isYaffs2) {
8772 + yaffs_ResizeFile(in, 0);
8773 + }
8774 + yaffs_SoftDeleteFile(in);
8775 + } else {
8776 + retVal =
8777 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
8778 + "unlinked", 0, 0);
8779 + }
8780 +
8781 + }
8782 + return retVal;
8783 +}
8784 +
8785 +int yaffs_DeleteFile(yaffs_Object * in)
8786 +{
8787 + int retVal = YAFFS_OK;
8788 +
8789 + if (in->nDataChunks > 0) {
8790 + /* Use soft deletion if there is data in the file */
8791 + if (!in->unlinked) {
8792 + retVal = yaffs_UnlinkFile(in);
8793 + }
8794 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
8795 + in->deleted = 1;
8796 + in->myDev->nDeletedFiles++;
8797 + yaffs_SoftDeleteFile(in);
8798 + }
8799 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
8800 + } else {
8801 + /* The file has no data chunks so we toss it immediately */
8802 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
8803 + in->variant.fileVariant.top = NULL;
8804 + yaffs_DoGenericObjectDeletion(in);
8805 +
8806 + return YAFFS_OK;
8807 + }
8808 +}
8809 +
8810 +static int yaffs_DeleteDirectory(yaffs_Object * in)
8811 +{
8812 + /* First check that the directory is empty. */
8813 + if (list_empty(&in->variant.directoryVariant.children)) {
8814 + return yaffs_DoGenericObjectDeletion(in);
8815 + }
8816 +
8817 + return YAFFS_FAIL;
8818 +
8819 +}
8820 +
8821 +static int yaffs_DeleteSymLink(yaffs_Object * in)
8822 +{
8823 + YFREE(in->variant.symLinkVariant.alias);
8824 +
8825 + return yaffs_DoGenericObjectDeletion(in);
8826 +}
8827 +
8828 +static int yaffs_DeleteHardLink(yaffs_Object * in)
8829 +{
8830 + /* remove this hardlink from the list assocaited with the equivalent
8831 + * object
8832 + */
8833 + list_del(&in->hardLinks);
8834 + return yaffs_DoGenericObjectDeletion(in);
8835 +}
8836 +
8837 +static void yaffs_DestroyObject(yaffs_Object * obj)
8838 +{
8839 + switch (obj->variantType) {
8840 + case YAFFS_OBJECT_TYPE_FILE:
8841 + yaffs_DeleteFile(obj);
8842 + break;
8843 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8844 + yaffs_DeleteDirectory(obj);
8845 + break;
8846 + case YAFFS_OBJECT_TYPE_SYMLINK:
8847 + yaffs_DeleteSymLink(obj);
8848 + break;
8849 + case YAFFS_OBJECT_TYPE_HARDLINK:
8850 + yaffs_DeleteHardLink(obj);
8851 + break;
8852 + case YAFFS_OBJECT_TYPE_SPECIAL:
8853 + yaffs_DoGenericObjectDeletion(obj);
8854 + break;
8855 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8856 + break; /* should not happen. */
8857 + }
8858 +}
8859 +
8860 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
8861 +{
8862 +
8863 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8864 + return yaffs_DeleteHardLink(obj);
8865 + } else if (!list_empty(&obj->hardLinks)) {
8866 + /* Curve ball: We're unlinking an object that has a hardlink.
8867 + *
8868 + * This problem arises because we are not strictly following
8869 + * The Linux link/inode model.
8870 + *
8871 + * We can't really delete the object.
8872 + * Instead, we do the following:
8873 + * - Select a hardlink.
8874 + * - Unhook it from the hard links
8875 + * - Unhook it from its parent directory (so that the rename can work)
8876 + * - Rename the object to the hardlink's name.
8877 + * - Delete the hardlink
8878 + */
8879 +
8880 + yaffs_Object *hl;
8881 + int retVal;
8882 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
8883 +
8884 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
8885 +
8886 + list_del_init(&hl->hardLinks);
8887 + list_del_init(&hl->siblings);
8888 +
8889 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
8890 +
8891 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
8892 +
8893 + if (retVal == YAFFS_OK) {
8894 + retVal = yaffs_DoGenericObjectDeletion(hl);
8895 + }
8896 + return retVal;
8897 +
8898 + } else {
8899 + switch (obj->variantType) {
8900 + case YAFFS_OBJECT_TYPE_FILE:
8901 + return yaffs_UnlinkFile(obj);
8902 + break;
8903 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8904 + return yaffs_DeleteDirectory(obj);
8905 + break;
8906 + case YAFFS_OBJECT_TYPE_SYMLINK:
8907 + return yaffs_DeleteSymLink(obj);
8908 + break;
8909 + case YAFFS_OBJECT_TYPE_SPECIAL:
8910 + return yaffs_DoGenericObjectDeletion(obj);
8911 + break;
8912 + case YAFFS_OBJECT_TYPE_HARDLINK:
8913 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8914 + default:
8915 + return YAFFS_FAIL;
8916 + }
8917 + }
8918 +}
8919 +
8920 +
8921 +static int yaffs_UnlinkObject( yaffs_Object *obj)
8922 +{
8923 +
8924 + if (obj && obj->unlinkAllowed) {
8925 + return yaffs_UnlinkWorker(obj);
8926 + }
8927 +
8928 + return YAFFS_FAIL;
8929 +
8930 +}
8931 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8932 +{
8933 + yaffs_Object *obj;
8934 +
8935 + obj = yaffs_FindObjectByName(dir, name);
8936 + return yaffs_UnlinkObject(obj);
8937 +}
8938 +
8939 +/*----------------------- Initialisation Scanning ---------------------- */
8940 +
8941 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8942 + int backwardScanning)
8943 +{
8944 + yaffs_Object *obj;
8945 +
8946 + if (!backwardScanning) {
8947 + /* Handle YAFFS1 forward scanning case
8948 + * For YAFFS1 we always do the deletion
8949 + */
8950 +
8951 + } else {
8952 + /* Handle YAFFS2 case (backward scanning)
8953 + * If the shadowed object exists then ignore.
8954 + */
8955 + if (yaffs_FindObjectByNumber(dev, objId)) {
8956 + return;
8957 + }
8958 + }
8959 +
8960 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8961 + * We put it in unlinked dir to be cleaned up after the scanning
8962 + */
8963 + obj =
8964 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8965 + YAFFS_OBJECT_TYPE_FILE);
8966 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8967 + obj->variant.fileVariant.shrinkSize = 0;
8968 + obj->valid = 1; /* So that we don't read any other info for this file */
8969 +
8970 +}
8971 +
8972 +typedef struct {
8973 + int seq;
8974 + int block;
8975 +} yaffs_BlockIndex;
8976 +
8977 +
8978 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8979 +{
8980 + yaffs_Object *hl;
8981 + yaffs_Object *in;
8982 +
8983 + while (hardList) {
8984 + hl = hardList;
8985 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8986 +
8987 + in = yaffs_FindObjectByNumber(dev,
8988 + hl->variant.hardLinkVariant.
8989 + equivalentObjectId);
8990 +
8991 + if (in) {
8992 + /* Add the hardlink pointers */
8993 + hl->variant.hardLinkVariant.equivalentObject = in;
8994 + list_add(&hl->hardLinks, &in->hardLinks);
8995 + } else {
8996 + /* Todo Need to report/handle this better.
8997 + * Got a problem... hardlink to a non-existant object
8998 + */
8999 + hl->variant.hardLinkVariant.equivalentObject = NULL;
9000 + INIT_LIST_HEAD(&hl->hardLinks);
9001 +
9002 + }
9003 +
9004 + }
9005 +
9006 +}
9007 +
9008 +
9009 +
9010 +
9011 +
9012 +static int ybicmp(const void *a, const void *b){
9013 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
9014 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
9015 + register int ablock = ((yaffs_BlockIndex *)a)->block;
9016 + register int bblock = ((yaffs_BlockIndex *)b)->block;
9017 + if( aseq == bseq )
9018 + return ablock - bblock;
9019 + else
9020 + return aseq - bseq;
9021 +
9022 +}
9023 +
9024 +static int yaffs_Scan(yaffs_Device * dev)
9025 +{
9026 + yaffs_ExtendedTags tags;
9027 + int blk;
9028 + int blockIterator;
9029 + int startIterator;
9030 + int endIterator;
9031 + int nBlocksToScan = 0;
9032 + int result;
9033 +
9034 + int chunk;
9035 + int c;
9036 + int deleted;
9037 + yaffs_BlockState state;
9038 + yaffs_Object *hardList = NULL;
9039 + yaffs_BlockInfo *bi;
9040 + int sequenceNumber;
9041 + yaffs_ObjectHeader *oh;
9042 + yaffs_Object *in;
9043 + yaffs_Object *parent;
9044 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9045 +
9046 + int alloc_failed = 0;
9047 +
9048 +
9049 + __u8 *chunkData;
9050 +
9051 + yaffs_BlockIndex *blockIndex = NULL;
9052 +
9053 + if (dev->isYaffs2) {
9054 + T(YAFFS_TRACE_SCAN,
9055 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
9056 + return YAFFS_FAIL;
9057 + }
9058 +
9059 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
9060 +
9061 + T(YAFFS_TRACE_SCAN,
9062 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9063 + dev->internalStartBlock, dev->internalEndBlock));
9064 +
9065 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9066 +
9067 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9068 +
9069 + if (dev->isYaffs2) {
9070 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9071 + if(!blockIndex)
9072 + return YAFFS_FAIL;
9073 + }
9074 +
9075 + /* Scan all the blocks to determine their state */
9076 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9077 + bi = yaffs_GetBlockInfo(dev, blk);
9078 + yaffs_ClearChunkBits(dev, blk);
9079 + bi->pagesInUse = 0;
9080 + bi->softDeletions = 0;
9081 +
9082 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9083 +
9084 + bi->blockState = state;
9085 + bi->sequenceNumber = sequenceNumber;
9086 +
9087 + T(YAFFS_TRACE_SCAN_DEBUG,
9088 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9089 + state, sequenceNumber));
9090 +
9091 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9092 + T(YAFFS_TRACE_BAD_BLOCKS,
9093 + (TSTR("block %d is bad" TENDSTR), blk));
9094 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9095 + T(YAFFS_TRACE_SCAN_DEBUG,
9096 + (TSTR("Block empty " TENDSTR)));
9097 + dev->nErasedBlocks++;
9098 + dev->nFreeChunks += dev->nChunksPerBlock;
9099 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9100 +
9101 + /* Determine the highest sequence number */
9102 + if (dev->isYaffs2 &&
9103 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9104 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9105 +
9106 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9107 + blockIndex[nBlocksToScan].block = blk;
9108 +
9109 + nBlocksToScan++;
9110 +
9111 + if (sequenceNumber >= dev->sequenceNumber) {
9112 + dev->sequenceNumber = sequenceNumber;
9113 + }
9114 + } else if (dev->isYaffs2) {
9115 + /* TODO: Nasty sequence number! */
9116 + T(YAFFS_TRACE_SCAN,
9117 + (TSTR
9118 + ("Block scanning block %d has bad sequence number %d"
9119 + TENDSTR), blk, sequenceNumber));
9120 +
9121 + }
9122 + }
9123 + }
9124 +
9125 + /* Sort the blocks
9126 + * Dungy old bubble sort for now...
9127 + */
9128 + if (dev->isYaffs2) {
9129 + yaffs_BlockIndex temp;
9130 + int i;
9131 + int j;
9132 +
9133 + for (i = 0; i < nBlocksToScan; i++)
9134 + for (j = i + 1; j < nBlocksToScan; j++)
9135 + if (blockIndex[i].seq > blockIndex[j].seq) {
9136 + temp = blockIndex[j];
9137 + blockIndex[j] = blockIndex[i];
9138 + blockIndex[i] = temp;
9139 + }
9140 + }
9141 +
9142 + /* Now scan the blocks looking at the data. */
9143 + if (dev->isYaffs2) {
9144 + startIterator = 0;
9145 + endIterator = nBlocksToScan - 1;
9146 + T(YAFFS_TRACE_SCAN_DEBUG,
9147 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9148 + } else {
9149 + startIterator = dev->internalStartBlock;
9150 + endIterator = dev->internalEndBlock;
9151 + }
9152 +
9153 + /* For each block.... */
9154 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9155 + blockIterator++) {
9156 +
9157 + if (dev->isYaffs2) {
9158 + /* get the block to scan in the correct order */
9159 + blk = blockIndex[blockIterator].block;
9160 + } else {
9161 + blk = blockIterator;
9162 + }
9163 +
9164 + bi = yaffs_GetBlockInfo(dev, blk);
9165 + state = bi->blockState;
9166 +
9167 + deleted = 0;
9168 +
9169 + /* For each chunk in each block that needs scanning....*/
9170 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9171 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9172 + /* Read the tags and decide what to do */
9173 + chunk = blk * dev->nChunksPerBlock + c;
9174 +
9175 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9176 + &tags);
9177 +
9178 + /* Let's have a good look at this chunk... */
9179 +
9180 + if (!dev->isYaffs2 && tags.chunkDeleted) {
9181 + /* YAFFS1 only...
9182 + * A deleted chunk
9183 + */
9184 + deleted++;
9185 + dev->nFreeChunks++;
9186 + /*T((" %d %d deleted\n",blk,c)); */
9187 + } else if (!tags.chunkUsed) {
9188 + /* An unassigned chunk in the block
9189 + * This means that either the block is empty or
9190 + * this is the one being allocated from
9191 + */
9192 +
9193 + if (c == 0) {
9194 + /* We're looking at the first chunk in the block so the block is unused */
9195 + state = YAFFS_BLOCK_STATE_EMPTY;
9196 + dev->nErasedBlocks++;
9197 + } else {
9198 + /* this is the block being allocated from */
9199 + T(YAFFS_TRACE_SCAN,
9200 + (TSTR
9201 + (" Allocating from %d %d" TENDSTR),
9202 + blk, c));
9203 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9204 + dev->allocationBlock = blk;
9205 + dev->allocationPage = c;
9206 + dev->allocationBlockFinder = blk;
9207 + /* Set it to here to encourage the allocator to go forth from here. */
9208 +
9209 + /* Yaffs2 sanity check:
9210 + * This should be the one with the highest sequence number
9211 + */
9212 + if (dev->isYaffs2
9213 + && (dev->sequenceNumber !=
9214 + bi->sequenceNumber)) {
9215 + T(YAFFS_TRACE_ALWAYS,
9216 + (TSTR
9217 + ("yaffs: Allocation block %d was not highest sequence id:"
9218 + " block seq = %d, dev seq = %d"
9219 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
9220 + }
9221 + }
9222 +
9223 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9224 + } else if (tags.chunkId > 0) {
9225 + /* chunkId > 0 so it is a data chunk... */
9226 + unsigned int endpos;
9227 +
9228 + yaffs_SetChunkBit(dev, blk, c);
9229 + bi->pagesInUse++;
9230 +
9231 + in = yaffs_FindOrCreateObjectByNumber(dev,
9232 + tags.
9233 + objectId,
9234 + YAFFS_OBJECT_TYPE_FILE);
9235 + /* PutChunkIntoFile checks for a clash (two data chunks with
9236 + * the same chunkId).
9237 + */
9238 +
9239 + if(!in)
9240 + alloc_failed = 1;
9241 +
9242 + if(in){
9243 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
9244 + alloc_failed = 1;
9245 + }
9246 +
9247 + endpos =
9248 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9249 + tags.byteCount;
9250 + if (in &&
9251 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9252 + && in->variant.fileVariant.scannedFileSize <
9253 + endpos) {
9254 + in->variant.fileVariant.
9255 + scannedFileSize = endpos;
9256 + if (!dev->useHeaderFileSize) {
9257 + in->variant.fileVariant.
9258 + fileSize =
9259 + in->variant.fileVariant.
9260 + scannedFileSize;
9261 + }
9262 +
9263 + }
9264 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9265 + } else {
9266 + /* chunkId == 0, so it is an ObjectHeader.
9267 + * Thus, we read in the object header and make the object
9268 + */
9269 + yaffs_SetChunkBit(dev, blk, c);
9270 + bi->pagesInUse++;
9271 +
9272 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9273 + chunkData,
9274 + NULL);
9275 +
9276 + oh = (yaffs_ObjectHeader *) chunkData;
9277 +
9278 + in = yaffs_FindObjectByNumber(dev,
9279 + tags.objectId);
9280 + if (in && in->variantType != oh->type) {
9281 + /* This should not happen, but somehow
9282 + * Wev'e ended up with an objectId that has been reused but not yet
9283 + * deleted, and worse still it has changed type. Delete the old object.
9284 + */
9285 +
9286 + yaffs_DestroyObject(in);
9287 +
9288 + in = 0;
9289 + }
9290 +
9291 + in = yaffs_FindOrCreateObjectByNumber(dev,
9292 + tags.
9293 + objectId,
9294 + oh->type);
9295 +
9296 + if(!in)
9297 + alloc_failed = 1;
9298 +
9299 + if (in && oh->shadowsObject > 0) {
9300 + yaffs_HandleShadowedObject(dev,
9301 + oh->
9302 + shadowsObject,
9303 + 0);
9304 + }
9305 +
9306 + if (in && in->valid) {
9307 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9308 +
9309 + unsigned existingSerial = in->serial;
9310 + unsigned newSerial = tags.serialNumber;
9311 +
9312 + if (dev->isYaffs2 ||
9313 + ((existingSerial + 1) & 3) ==
9314 + newSerial) {
9315 + /* Use new one - destroy the exisiting one */
9316 + yaffs_DeleteChunk(dev,
9317 + in->chunkId,
9318 + 1, __LINE__);
9319 + in->valid = 0;
9320 + } else {
9321 + /* Use existing - destroy this one. */
9322 + yaffs_DeleteChunk(dev, chunk, 1,
9323 + __LINE__);
9324 + }
9325 + }
9326 +
9327 + if (in && !in->valid &&
9328 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9329 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9330 + /* We only load some info, don't fiddle with directory structure */
9331 + in->valid = 1;
9332 + in->variantType = oh->type;
9333 +
9334 + in->yst_mode = oh->yst_mode;
9335 +#ifdef CONFIG_YAFFS_WINCE
9336 + in->win_atime[0] = oh->win_atime[0];
9337 + in->win_ctime[0] = oh->win_ctime[0];
9338 + in->win_mtime[0] = oh->win_mtime[0];
9339 + in->win_atime[1] = oh->win_atime[1];
9340 + in->win_ctime[1] = oh->win_ctime[1];
9341 + in->win_mtime[1] = oh->win_mtime[1];
9342 +#else
9343 + in->yst_uid = oh->yst_uid;
9344 + in->yst_gid = oh->yst_gid;
9345 + in->yst_atime = oh->yst_atime;
9346 + in->yst_mtime = oh->yst_mtime;
9347 + in->yst_ctime = oh->yst_ctime;
9348 + in->yst_rdev = oh->yst_rdev;
9349 +#endif
9350 + in->chunkId = chunk;
9351 +
9352 + } else if (in && !in->valid) {
9353 + /* we need to load this info */
9354 +
9355 + in->valid = 1;
9356 + in->variantType = oh->type;
9357 +
9358 + in->yst_mode = oh->yst_mode;
9359 +#ifdef CONFIG_YAFFS_WINCE
9360 + in->win_atime[0] = oh->win_atime[0];
9361 + in->win_ctime[0] = oh->win_ctime[0];
9362 + in->win_mtime[0] = oh->win_mtime[0];
9363 + in->win_atime[1] = oh->win_atime[1];
9364 + in->win_ctime[1] = oh->win_ctime[1];
9365 + in->win_mtime[1] = oh->win_mtime[1];
9366 +#else
9367 + in->yst_uid = oh->yst_uid;
9368 + in->yst_gid = oh->yst_gid;
9369 + in->yst_atime = oh->yst_atime;
9370 + in->yst_mtime = oh->yst_mtime;
9371 + in->yst_ctime = oh->yst_ctime;
9372 + in->yst_rdev = oh->yst_rdev;
9373 +#endif
9374 + in->chunkId = chunk;
9375 +
9376 + yaffs_SetObjectName(in, oh->name);
9377 + in->dirty = 0;
9378 +
9379 + /* directory stuff...
9380 + * hook up to parent
9381 + */
9382 +
9383 + parent =
9384 + yaffs_FindOrCreateObjectByNumber
9385 + (dev, oh->parentObjectId,
9386 + YAFFS_OBJECT_TYPE_DIRECTORY);
9387 + if (parent->variantType ==
9388 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9389 + /* Set up as a directory */
9390 + parent->variantType =
9391 + YAFFS_OBJECT_TYPE_DIRECTORY;
9392 + INIT_LIST_HEAD(&parent->variant.
9393 + directoryVariant.
9394 + children);
9395 + } else if (parent->variantType !=
9396 + YAFFS_OBJECT_TYPE_DIRECTORY)
9397 + {
9398 + /* Hoosterman, another problem....
9399 + * We're trying to use a non-directory as a directory
9400 + */
9401 +
9402 + T(YAFFS_TRACE_ERROR,
9403 + (TSTR
9404 + ("yaffs tragedy: attempting to use non-directory as"
9405 + " a directory in scan. Put in lost+found."
9406 + TENDSTR)));
9407 + parent = dev->lostNFoundDir;
9408 + }
9409 +
9410 + yaffs_AddObjectToDirectory(parent, in);
9411 +
9412 + if (0 && (parent == dev->deletedDir ||
9413 + parent == dev->unlinkedDir)) {
9414 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9415 + dev->nDeletedFiles++;
9416 + }
9417 + /* Note re hardlinks.
9418 + * Since we might scan a hardlink before its equivalent object is scanned
9419 + * we put them all in a list.
9420 + * After scanning is complete, we should have all the objects, so we run through this
9421 + * list and fix up all the chains.
9422 + */
9423 +
9424 + switch (in->variantType) {
9425 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9426 + /* Todo got a problem */
9427 + break;
9428 + case YAFFS_OBJECT_TYPE_FILE:
9429 + if (dev->isYaffs2
9430 + && oh->isShrink) {
9431 + /* Prune back the shrunken chunks */
9432 + yaffs_PruneResizedChunks
9433 + (in, oh->fileSize);
9434 + /* Mark the block as having a shrinkHeader */
9435 + bi->hasShrinkHeader = 1;
9436 + }
9437 +
9438 + if (dev->useHeaderFileSize)
9439 +
9440 + in->variant.fileVariant.
9441 + fileSize =
9442 + oh->fileSize;
9443 +
9444 + break;
9445 + case YAFFS_OBJECT_TYPE_HARDLINK:
9446 + in->variant.hardLinkVariant.
9447 + equivalentObjectId =
9448 + oh->equivalentObjectId;
9449 + in->hardLinks.next =
9450 + (struct list_head *)
9451 + hardList;
9452 + hardList = in;
9453 + break;
9454 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9455 + /* Do nothing */
9456 + break;
9457 + case YAFFS_OBJECT_TYPE_SPECIAL:
9458 + /* Do nothing */
9459 + break;
9460 + case YAFFS_OBJECT_TYPE_SYMLINK:
9461 + in->variant.symLinkVariant.alias =
9462 + yaffs_CloneString(oh->alias);
9463 + if(!in->variant.symLinkVariant.alias)
9464 + alloc_failed = 1;
9465 + break;
9466 + }
9467 +
9468 + if (parent == dev->deletedDir) {
9469 + yaffs_DestroyObject(in);
9470 + bi->hasShrinkHeader = 1;
9471 + }
9472 + }
9473 + }
9474 + }
9475 +
9476 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9477 + /* If we got this far while scanning, then the block is fully allocated.*/
9478 + state = YAFFS_BLOCK_STATE_FULL;
9479 + }
9480 +
9481 + bi->blockState = state;
9482 +
9483 + /* Now let's see if it was dirty */
9484 + if (bi->pagesInUse == 0 &&
9485 + !bi->hasShrinkHeader &&
9486 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9487 + yaffs_BlockBecameDirty(dev, blk);
9488 + }
9489 +
9490 + }
9491 +
9492 + if (blockIndex) {
9493 + YFREE(blockIndex);
9494 + }
9495 +
9496 +
9497 + /* Ok, we've done all the scanning.
9498 + * Fix up the hard link chains.
9499 + * We should now have scanned all the objects, now it's time to add these
9500 + * hardlinks.
9501 + */
9502 +
9503 + yaffs_HardlinkFixup(dev,hardList);
9504 +
9505 + /* Handle the unlinked files. Since they were left in an unlinked state we should
9506 + * just delete them.
9507 + */
9508 + {
9509 + struct list_head *i;
9510 + struct list_head *n;
9511 +
9512 + yaffs_Object *l;
9513 + /* Soft delete all the unlinked files */
9514 + list_for_each_safe(i, n,
9515 + &dev->unlinkedDir->variant.directoryVariant.
9516 + children) {
9517 + if (i) {
9518 + l = list_entry(i, yaffs_Object, siblings);
9519 + yaffs_DestroyObject(l);
9520 + }
9521 + }
9522 + }
9523 +
9524 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9525 +
9526 + if(alloc_failed){
9527 + return YAFFS_FAIL;
9528 + }
9529 +
9530 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9531 +
9532 +
9533 + return YAFFS_OK;
9534 +}
9535 +
9536 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9537 +{
9538 + __u8 *chunkData;
9539 + yaffs_ObjectHeader *oh;
9540 + yaffs_Device *dev = in->myDev;
9541 + yaffs_ExtendedTags tags;
9542 + int result;
9543 + int alloc_failed = 0;
9544 +
9545 + if(!in)
9546 + return;
9547 +
9548 +#if 0
9549 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
9550 + in->objectId,
9551 + in->lazyLoaded ? "not yet" : "already"));
9552 +#endif
9553 +
9554 + if(in->lazyLoaded){
9555 + in->lazyLoaded = 0;
9556 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9557 +
9558 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
9559 + oh = (yaffs_ObjectHeader *) chunkData;
9560 +
9561 + in->yst_mode = oh->yst_mode;
9562 +#ifdef CONFIG_YAFFS_WINCE
9563 + in->win_atime[0] = oh->win_atime[0];
9564 + in->win_ctime[0] = oh->win_ctime[0];
9565 + in->win_mtime[0] = oh->win_mtime[0];
9566 + in->win_atime[1] = oh->win_atime[1];
9567 + in->win_ctime[1] = oh->win_ctime[1];
9568 + in->win_mtime[1] = oh->win_mtime[1];
9569 +#else
9570 + in->yst_uid = oh->yst_uid;
9571 + in->yst_gid = oh->yst_gid;
9572 + in->yst_atime = oh->yst_atime;
9573 + in->yst_mtime = oh->yst_mtime;
9574 + in->yst_ctime = oh->yst_ctime;
9575 + in->yst_rdev = oh->yst_rdev;
9576 +
9577 +#endif
9578 + yaffs_SetObjectName(in, oh->name);
9579 +
9580 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
9581 + in->variant.symLinkVariant.alias =
9582 + yaffs_CloneString(oh->alias);
9583 + if(!in->variant.symLinkVariant.alias)
9584 + alloc_failed = 1; /* Not returned to caller */
9585 + }
9586 +
9587 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
9588 + }
9589 +}
9590 +
9591 +static int yaffs_ScanBackwards(yaffs_Device * dev)
9592 +{
9593 + yaffs_ExtendedTags tags;
9594 + int blk;
9595 + int blockIterator;
9596 + int startIterator;
9597 + int endIterator;
9598 + int nBlocksToScan = 0;
9599 +
9600 + int chunk;
9601 + int result;
9602 + int c;
9603 + int deleted;
9604 + yaffs_BlockState state;
9605 + yaffs_Object *hardList = NULL;
9606 + yaffs_BlockInfo *bi;
9607 + int sequenceNumber;
9608 + yaffs_ObjectHeader *oh;
9609 + yaffs_Object *in;
9610 + yaffs_Object *parent;
9611 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9612 + int itsUnlinked;
9613 + __u8 *chunkData;
9614 +
9615 + int fileSize;
9616 + int isShrink;
9617 + int foundChunksInBlock;
9618 + int equivalentObjectId;
9619 + int alloc_failed = 0;
9620 +
9621 +
9622 + yaffs_BlockIndex *blockIndex = NULL;
9623 + int altBlockIndex = 0;
9624 +
9625 + if (!dev->isYaffs2) {
9626 + T(YAFFS_TRACE_SCAN,
9627 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9628 + return YAFFS_FAIL;
9629 + }
9630 +
9631 + T(YAFFS_TRACE_SCAN,
9632 + (TSTR
9633 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9634 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9635 +
9636 +
9637 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9638 +
9639 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9640 +
9641 + if(!blockIndex) {
9642 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9643 + altBlockIndex = 1;
9644 + }
9645 +
9646 + if(!blockIndex) {
9647 + T(YAFFS_TRACE_SCAN,
9648 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9649 + return YAFFS_FAIL;
9650 + }
9651 +
9652 + dev->blocksInCheckpoint = 0;
9653 +
9654 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9655 +
9656 + /* Scan all the blocks to determine their state */
9657 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9658 + bi = yaffs_GetBlockInfo(dev, blk);
9659 + yaffs_ClearChunkBits(dev, blk);
9660 + bi->pagesInUse = 0;
9661 + bi->softDeletions = 0;
9662 +
9663 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9664 +
9665 + bi->blockState = state;
9666 + bi->sequenceNumber = sequenceNumber;
9667 +
9668 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9669 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9670 +
9671 + T(YAFFS_TRACE_SCAN_DEBUG,
9672 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9673 + state, sequenceNumber));
9674 +
9675 +
9676 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
9677 + dev->blocksInCheckpoint++;
9678 +
9679 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
9680 + T(YAFFS_TRACE_BAD_BLOCKS,
9681 + (TSTR("block %d is bad" TENDSTR), blk));
9682 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9683 + T(YAFFS_TRACE_SCAN_DEBUG,
9684 + (TSTR("Block empty " TENDSTR)));
9685 + dev->nErasedBlocks++;
9686 + dev->nFreeChunks += dev->nChunksPerBlock;
9687 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9688 +
9689 + /* Determine the highest sequence number */
9690 + if (dev->isYaffs2 &&
9691 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9692 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9693 +
9694 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9695 + blockIndex[nBlocksToScan].block = blk;
9696 +
9697 + nBlocksToScan++;
9698 +
9699 + if (sequenceNumber >= dev->sequenceNumber) {
9700 + dev->sequenceNumber = sequenceNumber;
9701 + }
9702 + } else if (dev->isYaffs2) {
9703 + /* TODO: Nasty sequence number! */
9704 + T(YAFFS_TRACE_SCAN,
9705 + (TSTR
9706 + ("Block scanning block %d has bad sequence number %d"
9707 + TENDSTR), blk, sequenceNumber));
9708 +
9709 + }
9710 + }
9711 + }
9712 +
9713 + T(YAFFS_TRACE_SCAN,
9714 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
9715 +
9716 +
9717 +
9718 + YYIELD();
9719 +
9720 + /* Sort the blocks */
9721 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
9722 + yaffs_qsort(blockIndex, nBlocksToScan,
9723 + sizeof(yaffs_BlockIndex), ybicmp);
9724 +#else
9725 + {
9726 + /* Dungy old bubble sort... */
9727 +
9728 + yaffs_BlockIndex temp;
9729 + int i;
9730 + int j;
9731 +
9732 + for (i = 0; i < nBlocksToScan; i++)
9733 + for (j = i + 1; j < nBlocksToScan; j++)
9734 + if (blockIndex[i].seq > blockIndex[j].seq) {
9735 + temp = blockIndex[j];
9736 + blockIndex[j] = blockIndex[i];
9737 + blockIndex[i] = temp;
9738 + }
9739 + }
9740 +#endif
9741 +
9742 + YYIELD();
9743 +
9744 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
9745 +
9746 + /* Now scan the blocks looking at the data. */
9747 + startIterator = 0;
9748 + endIterator = nBlocksToScan - 1;
9749 + T(YAFFS_TRACE_SCAN_DEBUG,
9750 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9751 +
9752 + /* For each block.... backwards */
9753 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
9754 + blockIterator--) {
9755 + /* Cooperative multitasking! This loop can run for so
9756 + long that watchdog timers expire. */
9757 + YYIELD();
9758 +
9759 + /* get the block to scan in the correct order */
9760 + blk = blockIndex[blockIterator].block;
9761 +
9762 + bi = yaffs_GetBlockInfo(dev, blk);
9763 +
9764 +
9765 + state = bi->blockState;
9766 +
9767 + deleted = 0;
9768 +
9769 + /* For each chunk in each block that needs scanning.... */
9770 + foundChunksInBlock = 0;
9771 + for (c = dev->nChunksPerBlock - 1;
9772 + !alloc_failed && c >= 0 &&
9773 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9774 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
9775 + /* Scan backwards...
9776 + * Read the tags and decide what to do
9777 + */
9778 +
9779 + chunk = blk * dev->nChunksPerBlock + c;
9780 +
9781 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9782 + &tags);
9783 +
9784 + /* Let's have a good look at this chunk... */
9785 +
9786 + if (!tags.chunkUsed) {
9787 + /* An unassigned chunk in the block.
9788 + * If there are used chunks after this one, then
9789 + * it is a chunk that was skipped due to failing the erased
9790 + * check. Just skip it so that it can be deleted.
9791 + * But, more typically, We get here when this is an unallocated
9792 + * chunk and his means that either the block is empty or
9793 + * this is the one being allocated from
9794 + */
9795 +
9796 + if(foundChunksInBlock)
9797 + {
9798 + /* This is a chunk that was skipped due to failing the erased check */
9799 +
9800 + } else if (c == 0) {
9801 + /* We're looking at the first chunk in the block so the block is unused */
9802 + state = YAFFS_BLOCK_STATE_EMPTY;
9803 + dev->nErasedBlocks++;
9804 + } else {
9805 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9806 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
9807 + if(dev->sequenceNumber == bi->sequenceNumber) {
9808 + /* this is the block being allocated from */
9809 +
9810 + T(YAFFS_TRACE_SCAN,
9811 + (TSTR
9812 + (" Allocating from %d %d"
9813 + TENDSTR), blk, c));
9814 +
9815 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9816 + dev->allocationBlock = blk;
9817 + dev->allocationPage = c;
9818 + dev->allocationBlockFinder = blk;
9819 + }
9820 + else {
9821 + /* This is a partially written block that is not
9822 + * the current allocation block. This block must have
9823 + * had a write failure, so set up for retirement.
9824 + */
9825 +
9826 + bi->needsRetiring = 1;
9827 + bi->gcPrioritise = 1;
9828 +
9829 + T(YAFFS_TRACE_ALWAYS,
9830 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
9831 + blk));
9832 + }
9833 +
9834 + }
9835 +
9836 + }
9837 +
9838 + dev->nFreeChunks++;
9839 +
9840 + } else if (tags.chunkId > 0) {
9841 + /* chunkId > 0 so it is a data chunk... */
9842 + unsigned int endpos;
9843 + __u32 chunkBase =
9844 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
9845 +
9846 + foundChunksInBlock = 1;
9847 +
9848 +
9849 + yaffs_SetChunkBit(dev, blk, c);
9850 + bi->pagesInUse++;
9851 +
9852 + in = yaffs_FindOrCreateObjectByNumber(dev,
9853 + tags.
9854 + objectId,
9855 + YAFFS_OBJECT_TYPE_FILE);
9856 + if(!in){
9857 + /* Out of memory */
9858 + alloc_failed = 1;
9859 + }
9860 +
9861 + if (in &&
9862 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9863 + && chunkBase <
9864 + in->variant.fileVariant.shrinkSize) {
9865 + /* This has not been invalidated by a resize */
9866 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
9867 + chunk, -1)){
9868 + alloc_failed = 1;
9869 + }
9870 +
9871 + /* File size is calculated by looking at the data chunks if we have not
9872 + * seen an object header yet. Stop this practice once we find an object header.
9873 + */
9874 + endpos =
9875 + (tags.chunkId -
9876 + 1) * dev->nDataBytesPerChunk +
9877 + tags.byteCount;
9878 +
9879 + if (!in->valid && /* have not got an object header yet */
9880 + in->variant.fileVariant.
9881 + scannedFileSize < endpos) {
9882 + in->variant.fileVariant.
9883 + scannedFileSize = endpos;
9884 + in->variant.fileVariant.
9885 + fileSize =
9886 + in->variant.fileVariant.
9887 + scannedFileSize;
9888 + }
9889 +
9890 + } else if(in) {
9891 + /* This chunk has been invalidated by a resize, so delete */
9892 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9893 +
9894 + }
9895 + } else {
9896 + /* chunkId == 0, so it is an ObjectHeader.
9897 + * Thus, we read in the object header and make the object
9898 + */
9899 + foundChunksInBlock = 1;
9900 +
9901 + yaffs_SetChunkBit(dev, blk, c);
9902 + bi->pagesInUse++;
9903 +
9904 + oh = NULL;
9905 + in = NULL;
9906 +
9907 + if (tags.extraHeaderInfoAvailable) {
9908 + in = yaffs_FindOrCreateObjectByNumber
9909 + (dev, tags.objectId,
9910 + tags.extraObjectType);
9911 + }
9912 +
9913 + if (!in ||
9914 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
9915 + !in->valid ||
9916 +#endif
9917 + tags.extraShadows ||
9918 + (!in->valid &&
9919 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9920 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
9921 + ) {
9922 +
9923 + /* If we don't have valid info then we need to read the chunk
9924 + * TODO In future we can probably defer reading the chunk and
9925 + * living with invalid data until needed.
9926 + */
9927 +
9928 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
9929 + chunk,
9930 + chunkData,
9931 + NULL);
9932 +
9933 + oh = (yaffs_ObjectHeader *) chunkData;
9934 +
9935 + if (!in)
9936 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
9937 +
9938 + }
9939 +
9940 + if (!in) {
9941 + /* TODO Hoosterman we have a problem! */
9942 + T(YAFFS_TRACE_ERROR,
9943 + (TSTR
9944 + ("yaffs tragedy: Could not make object for object %d "
9945 + "at chunk %d during scan"
9946 + TENDSTR), tags.objectId, chunk));
9947 +
9948 + }
9949 +
9950 + if (in->valid) {
9951 + /* We have already filled this one.
9952 + * We have a duplicate that will be discarded, but
9953 + * we first have to suck out resize info if it is a file.
9954 + */
9955 +
9956 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
9957 + ((oh &&
9958 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
9959 + (tags.extraHeaderInfoAvailable &&
9960 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
9961 + ) {
9962 + __u32 thisSize =
9963 + (oh) ? oh->fileSize : tags.
9964 + extraFileLength;
9965 + __u32 parentObjectId =
9966 + (oh) ? oh->
9967 + parentObjectId : tags.
9968 + extraParentObjectId;
9969 + unsigned isShrink =
9970 + (oh) ? oh->isShrink : tags.
9971 + extraIsShrinkHeader;
9972 +
9973 + /* If it is deleted (unlinked at start also means deleted)
9974 + * we treat the file size as being zeroed at this point.
9975 + */
9976 + if (parentObjectId ==
9977 + YAFFS_OBJECTID_DELETED
9978 + || parentObjectId ==
9979 + YAFFS_OBJECTID_UNLINKED) {
9980 + thisSize = 0;
9981 + isShrink = 1;
9982 + }
9983 +
9984 + if (isShrink &&
9985 + in->variant.fileVariant.
9986 + shrinkSize > thisSize) {
9987 + in->variant.fileVariant.
9988 + shrinkSize =
9989 + thisSize;
9990 + }
9991 +
9992 + if (isShrink) {
9993 + bi->hasShrinkHeader = 1;
9994 + }
9995 +
9996 + }
9997 + /* Use existing - destroy this one. */
9998 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9999 +
10000 + }
10001 +
10002 + if (!in->valid &&
10003 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
10004 + tags.objectId ==
10005 + YAFFS_OBJECTID_LOSTNFOUND)) {
10006 + /* We only load some info, don't fiddle with directory structure */
10007 + in->valid = 1;
10008 +
10009 + if(oh) {
10010 + in->variantType = oh->type;
10011 +
10012 + in->yst_mode = oh->yst_mode;
10013 +#ifdef CONFIG_YAFFS_WINCE
10014 + in->win_atime[0] = oh->win_atime[0];
10015 + in->win_ctime[0] = oh->win_ctime[0];
10016 + in->win_mtime[0] = oh->win_mtime[0];
10017 + in->win_atime[1] = oh->win_atime[1];
10018 + in->win_ctime[1] = oh->win_ctime[1];
10019 + in->win_mtime[1] = oh->win_mtime[1];
10020 +#else
10021 + in->yst_uid = oh->yst_uid;
10022 + in->yst_gid = oh->yst_gid;
10023 + in->yst_atime = oh->yst_atime;
10024 + in->yst_mtime = oh->yst_mtime;
10025 + in->yst_ctime = oh->yst_ctime;
10026 + in->yst_rdev = oh->yst_rdev;
10027 +
10028 +#endif
10029 + } else {
10030 + in->variantType = tags.extraObjectType;
10031 + in->lazyLoaded = 1;
10032 + }
10033 +
10034 + in->chunkId = chunk;
10035 +
10036 + } else if (!in->valid) {
10037 + /* we need to load this info */
10038 +
10039 + in->valid = 1;
10040 + in->chunkId = chunk;
10041 +
10042 + if(oh) {
10043 + in->variantType = oh->type;
10044 +
10045 + in->yst_mode = oh->yst_mode;
10046 +#ifdef CONFIG_YAFFS_WINCE
10047 + in->win_atime[0] = oh->win_atime[0];
10048 + in->win_ctime[0] = oh->win_ctime[0];
10049 + in->win_mtime[0] = oh->win_mtime[0];
10050 + in->win_atime[1] = oh->win_atime[1];
10051 + in->win_ctime[1] = oh->win_ctime[1];
10052 + in->win_mtime[1] = oh->win_mtime[1];
10053 +#else
10054 + in->yst_uid = oh->yst_uid;
10055 + in->yst_gid = oh->yst_gid;
10056 + in->yst_atime = oh->yst_atime;
10057 + in->yst_mtime = oh->yst_mtime;
10058 + in->yst_ctime = oh->yst_ctime;
10059 + in->yst_rdev = oh->yst_rdev;
10060 +#endif
10061 +
10062 + if (oh->shadowsObject > 0)
10063 + yaffs_HandleShadowedObject(dev,
10064 + oh->
10065 + shadowsObject,
10066 + 1);
10067 +
10068 +
10069 + yaffs_SetObjectName(in, oh->name);
10070 + parent =
10071 + yaffs_FindOrCreateObjectByNumber
10072 + (dev, oh->parentObjectId,
10073 + YAFFS_OBJECT_TYPE_DIRECTORY);
10074 +
10075 + fileSize = oh->fileSize;
10076 + isShrink = oh->isShrink;
10077 + equivalentObjectId = oh->equivalentObjectId;
10078 +
10079 + }
10080 + else {
10081 + in->variantType = tags.extraObjectType;
10082 + parent =
10083 + yaffs_FindOrCreateObjectByNumber
10084 + (dev, tags.extraParentObjectId,
10085 + YAFFS_OBJECT_TYPE_DIRECTORY);
10086 + fileSize = tags.extraFileLength;
10087 + isShrink = tags.extraIsShrinkHeader;
10088 + equivalentObjectId = tags.extraEquivalentObjectId;
10089 + in->lazyLoaded = 1;
10090 +
10091 + }
10092 + in->dirty = 0;
10093 +
10094 + /* directory stuff...
10095 + * hook up to parent
10096 + */
10097 +
10098 + if (parent->variantType ==
10099 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10100 + /* Set up as a directory */
10101 + parent->variantType =
10102 + YAFFS_OBJECT_TYPE_DIRECTORY;
10103 + INIT_LIST_HEAD(&parent->variant.
10104 + directoryVariant.
10105 + children);
10106 + } else if (parent->variantType !=
10107 + YAFFS_OBJECT_TYPE_DIRECTORY)
10108 + {
10109 + /* Hoosterman, another problem....
10110 + * We're trying to use a non-directory as a directory
10111 + */
10112 +
10113 + T(YAFFS_TRACE_ERROR,
10114 + (TSTR
10115 + ("yaffs tragedy: attempting to use non-directory as"
10116 + " a directory in scan. Put in lost+found."
10117 + TENDSTR)));
10118 + parent = dev->lostNFoundDir;
10119 + }
10120 +
10121 + yaffs_AddObjectToDirectory(parent, in);
10122 +
10123 + itsUnlinked = (parent == dev->deletedDir) ||
10124 + (parent == dev->unlinkedDir);
10125 +
10126 + if (isShrink) {
10127 + /* Mark the block as having a shrinkHeader */
10128 + bi->hasShrinkHeader = 1;
10129 + }
10130 +
10131 + /* Note re hardlinks.
10132 + * Since we might scan a hardlink before its equivalent object is scanned
10133 + * we put them all in a list.
10134 + * After scanning is complete, we should have all the objects, so we run
10135 + * through this list and fix up all the chains.
10136 + */
10137 +
10138 + switch (in->variantType) {
10139 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10140 + /* Todo got a problem */
10141 + break;
10142 + case YAFFS_OBJECT_TYPE_FILE:
10143 +
10144 + if (in->variant.fileVariant.
10145 + scannedFileSize < fileSize) {
10146 + /* This covers the case where the file size is greater
10147 + * than where the data is
10148 + * This will happen if the file is resized to be larger
10149 + * than its current data extents.
10150 + */
10151 + in->variant.fileVariant.fileSize = fileSize;
10152 + in->variant.fileVariant.scannedFileSize =
10153 + in->variant.fileVariant.fileSize;
10154 + }
10155 +
10156 + if (isShrink &&
10157 + in->variant.fileVariant.shrinkSize > fileSize) {
10158 + in->variant.fileVariant.shrinkSize = fileSize;
10159 + }
10160 +
10161 + break;
10162 + case YAFFS_OBJECT_TYPE_HARDLINK:
10163 + if(!itsUnlinked) {
10164 + in->variant.hardLinkVariant.equivalentObjectId =
10165 + equivalentObjectId;
10166 + in->hardLinks.next =
10167 + (struct list_head *) hardList;
10168 + hardList = in;
10169 + }
10170 + break;
10171 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10172 + /* Do nothing */
10173 + break;
10174 + case YAFFS_OBJECT_TYPE_SPECIAL:
10175 + /* Do nothing */
10176 + break;
10177 + case YAFFS_OBJECT_TYPE_SYMLINK:
10178 + if(oh){
10179 + in->variant.symLinkVariant.alias =
10180 + yaffs_CloneString(oh->
10181 + alias);
10182 + if(!in->variant.symLinkVariant.alias)
10183 + alloc_failed = 1;
10184 + }
10185 + break;
10186 + }
10187 +
10188 + }
10189 +
10190 + }
10191 +
10192 + } /* End of scanning for each chunk */
10193 +
10194 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10195 + /* If we got this far while scanning, then the block is fully allocated. */
10196 + state = YAFFS_BLOCK_STATE_FULL;
10197 + }
10198 +
10199 + bi->blockState = state;
10200 +
10201 + /* Now let's see if it was dirty */
10202 + if (bi->pagesInUse == 0 &&
10203 + !bi->hasShrinkHeader &&
10204 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10205 + yaffs_BlockBecameDirty(dev, blk);
10206 + }
10207 +
10208 + }
10209 +
10210 + if (altBlockIndex)
10211 + YFREE_ALT(blockIndex);
10212 + else
10213 + YFREE(blockIndex);
10214 +
10215 + /* Ok, we've done all the scanning.
10216 + * Fix up the hard link chains.
10217 + * We should now have scanned all the objects, now it's time to add these
10218 + * hardlinks.
10219 + */
10220 + yaffs_HardlinkFixup(dev,hardList);
10221 +
10222 +
10223 + /*
10224 + * Sort out state of unlinked and deleted objects.
10225 + */
10226 + {
10227 + struct list_head *i;
10228 + struct list_head *n;
10229 +
10230 + yaffs_Object *l;
10231 +
10232 + /* Soft delete all the unlinked files */
10233 + list_for_each_safe(i, n,
10234 + &dev->unlinkedDir->variant.directoryVariant.
10235 + children) {
10236 + if (i) {
10237 + l = list_entry(i, yaffs_Object, siblings);
10238 + yaffs_DestroyObject(l);
10239 + }
10240 + }
10241 +
10242 + /* Soft delete all the deletedDir files */
10243 + list_for_each_safe(i, n,
10244 + &dev->deletedDir->variant.directoryVariant.
10245 + children) {
10246 + if (i) {
10247 + l = list_entry(i, yaffs_Object, siblings);
10248 + yaffs_DestroyObject(l);
10249 +
10250 + }
10251 + }
10252 + }
10253 +
10254 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10255 +
10256 + if(alloc_failed){
10257 + return YAFFS_FAIL;
10258 + }
10259 +
10260 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10261 +
10262 + return YAFFS_OK;
10263 +}
10264 +
10265 +/*------------------------------ Directory Functions ----------------------------- */
10266 +
10267 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
10268 +{
10269 + yaffs_Device *dev = obj->myDev;
10270 +
10271 + if(dev && dev->removeObjectCallback)
10272 + dev->removeObjectCallback(obj);
10273 +
10274 + list_del_init(&obj->siblings);
10275 + obj->parent = NULL;
10276 +}
10277 +
10278 +
10279 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
10280 + yaffs_Object * obj)
10281 +{
10282 +
10283 + if (!directory) {
10284 + T(YAFFS_TRACE_ALWAYS,
10285 + (TSTR
10286 + ("tragedy: Trying to add an object to a null pointer directory"
10287 + TENDSTR)));
10288 + YBUG();
10289 + }
10290 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10291 + T(YAFFS_TRACE_ALWAYS,
10292 + (TSTR
10293 + ("tragedy: Trying to add an object to a non-directory"
10294 + TENDSTR)));
10295 + YBUG();
10296 + }
10297 +
10298 + if (obj->siblings.prev == NULL) {
10299 + /* Not initialised */
10300 + INIT_LIST_HEAD(&obj->siblings);
10301 +
10302 + } else if (!list_empty(&obj->siblings)) {
10303 + /* If it is holed up somewhere else, un hook it */
10304 + yaffs_RemoveObjectFromDirectory(obj);
10305 + }
10306 + /* Now add it */
10307 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
10308 + obj->parent = directory;
10309 +
10310 + if (directory == obj->myDev->unlinkedDir
10311 + || directory == obj->myDev->deletedDir) {
10312 + obj->unlinked = 1;
10313 + obj->myDev->nUnlinkedFiles++;
10314 + obj->renameAllowed = 0;
10315 + }
10316 +}
10317 +
10318 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
10319 + const YCHAR * name)
10320 +{
10321 + int sum;
10322 +
10323 + struct list_head *i;
10324 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10325 +
10326 + yaffs_Object *l;
10327 +
10328 + if (!name) {
10329 + return NULL;
10330 + }
10331 +
10332 + if (!directory) {
10333 + T(YAFFS_TRACE_ALWAYS,
10334 + (TSTR
10335 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10336 + TENDSTR)));
10337 + YBUG();
10338 + }
10339 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10340 + T(YAFFS_TRACE_ALWAYS,
10341 + (TSTR
10342 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10343 + YBUG();
10344 + }
10345 +
10346 + sum = yaffs_CalcNameSum(name);
10347 +
10348 + list_for_each(i, &directory->variant.directoryVariant.children) {
10349 + if (i) {
10350 + l = list_entry(i, yaffs_Object, siblings);
10351 +
10352 + yaffs_CheckObjectDetailsLoaded(l);
10353 +
10354 + /* Special case for lost-n-found */
10355 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10356 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
10357 + return l;
10358 + }
10359 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
10360 + {
10361 + /* LostnFound cunk called Objxxx
10362 + * Do a real check
10363 + */
10364 + yaffs_GetObjectName(l, buffer,
10365 + YAFFS_MAX_NAME_LENGTH);
10366 + if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
10367 + return l;
10368 + }
10369 +
10370 + }
10371 + }
10372 + }
10373 +
10374 + return NULL;
10375 +}
10376 +
10377 +
10378 +#if 0
10379 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10380 + int (*fn) (yaffs_Object *))
10381 +{
10382 + struct list_head *i;
10383 + yaffs_Object *l;
10384 +
10385 + if (!theDir) {
10386 + T(YAFFS_TRACE_ALWAYS,
10387 + (TSTR
10388 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10389 + TENDSTR)));
10390 + YBUG();
10391 + }
10392 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10393 + T(YAFFS_TRACE_ALWAYS,
10394 + (TSTR
10395 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10396 + YBUG();
10397 + }
10398 +
10399 + list_for_each(i, &theDir->variant.directoryVariant.children) {
10400 + if (i) {
10401 + l = list_entry(i, yaffs_Object, siblings);
10402 + if (l && !fn(l)) {
10403 + return YAFFS_FAIL;
10404 + }
10405 + }
10406 + }
10407 +
10408 + return YAFFS_OK;
10409 +
10410 +}
10411 +#endif
10412 +
10413 +/* GetEquivalentObject dereferences any hard links to get to the
10414 + * actual object.
10415 + */
10416 +
10417 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
10418 +{
10419 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10420 + /* We want the object id of the equivalent object, not this one */
10421 + obj = obj->variant.hardLinkVariant.equivalentObject;
10422 + yaffs_CheckObjectDetailsLoaded(obj);
10423 + }
10424 + return obj;
10425 +
10426 +}
10427 +
10428 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
10429 +{
10430 + memset(name, 0, buffSize * sizeof(YCHAR));
10431 +
10432 + yaffs_CheckObjectDetailsLoaded(obj);
10433 +
10434 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10435 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10436 + } else if (obj->chunkId <= 0) {
10437 + YCHAR locName[20];
10438 + /* make up a name */
10439 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
10440 + obj->objectId);
10441 + yaffs_strncpy(name, locName, buffSize - 1);
10442 +
10443 + }
10444 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10445 + else if (obj->shortName[0]) {
10446 + yaffs_strcpy(name, obj->shortName);
10447 + }
10448 +#endif
10449 + else {
10450 + int result;
10451 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10452 +
10453 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10454 +
10455 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10456 +
10457 + if (obj->chunkId >= 0) {
10458 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10459 + obj->chunkId, buffer,
10460 + NULL);
10461 + }
10462 + yaffs_strncpy(name, oh->name, buffSize - 1);
10463 +
10464 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10465 + }
10466 +
10467 + return yaffs_strlen(name);
10468 +}
10469 +
10470 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
10471 +{
10472 +
10473 + /* Dereference any hard linking */
10474 + obj = yaffs_GetEquivalentObject(obj);
10475 +
10476 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10477 + return obj->variant.fileVariant.fileSize;
10478 + }
10479 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10480 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10481 + } else {
10482 + /* Only a directory should drop through to here */
10483 + return obj->myDev->nDataBytesPerChunk;
10484 + }
10485 +}
10486 +
10487 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
10488 +{
10489 + int count = 0;
10490 + struct list_head *i;
10491 +
10492 + if (!obj->unlinked) {
10493 + count++; /* the object itself */
10494 + }
10495 + list_for_each(i, &obj->hardLinks) {
10496 + count++; /* add the hard links; */
10497 + }
10498 + return count;
10499 +
10500 +}
10501 +
10502 +int yaffs_GetObjectInode(yaffs_Object * obj)
10503 +{
10504 + obj = yaffs_GetEquivalentObject(obj);
10505 +
10506 + return obj->objectId;
10507 +}
10508 +
10509 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
10510 +{
10511 + obj = yaffs_GetEquivalentObject(obj);
10512 +
10513 + switch (obj->variantType) {
10514 + case YAFFS_OBJECT_TYPE_FILE:
10515 + return DT_REG;
10516 + break;
10517 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10518 + return DT_DIR;
10519 + break;
10520 + case YAFFS_OBJECT_TYPE_SYMLINK:
10521 + return DT_LNK;
10522 + break;
10523 + case YAFFS_OBJECT_TYPE_HARDLINK:
10524 + return DT_REG;
10525 + break;
10526 + case YAFFS_OBJECT_TYPE_SPECIAL:
10527 + if (S_ISFIFO(obj->yst_mode))
10528 + return DT_FIFO;
10529 + if (S_ISCHR(obj->yst_mode))
10530 + return DT_CHR;
10531 + if (S_ISBLK(obj->yst_mode))
10532 + return DT_BLK;
10533 + if (S_ISSOCK(obj->yst_mode))
10534 + return DT_SOCK;
10535 + default:
10536 + return DT_REG;
10537 + break;
10538 + }
10539 +}
10540 +
10541 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
10542 +{
10543 + obj = yaffs_GetEquivalentObject(obj);
10544 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10545 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10546 + } else {
10547 + return yaffs_CloneString(_Y(""));
10548 + }
10549 +}
10550 +
10551 +#ifndef CONFIG_YAFFS_WINCE
10552 +
10553 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
10554 +{
10555 + unsigned int valid = attr->ia_valid;
10556 +
10557 + if (valid & ATTR_MODE)
10558 + obj->yst_mode = attr->ia_mode;
10559 + if (valid & ATTR_UID)
10560 + obj->yst_uid = attr->ia_uid;
10561 + if (valid & ATTR_GID)
10562 + obj->yst_gid = attr->ia_gid;
10563 +
10564 + if (valid & ATTR_ATIME)
10565 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10566 + if (valid & ATTR_CTIME)
10567 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10568 + if (valid & ATTR_MTIME)
10569 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10570 +
10571 + if (valid & ATTR_SIZE)
10572 + yaffs_ResizeFile(obj, attr->ia_size);
10573 +
10574 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10575 +
10576 + return YAFFS_OK;
10577 +
10578 +}
10579 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
10580 +{
10581 + unsigned int valid = 0;
10582 +
10583 + attr->ia_mode = obj->yst_mode;
10584 + valid |= ATTR_MODE;
10585 + attr->ia_uid = obj->yst_uid;
10586 + valid |= ATTR_UID;
10587 + attr->ia_gid = obj->yst_gid;
10588 + valid |= ATTR_GID;
10589 +
10590 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10591 + valid |= ATTR_ATIME;
10592 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10593 + valid |= ATTR_CTIME;
10594 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
10595 + valid |= ATTR_MTIME;
10596 +
10597 + attr->ia_size = yaffs_GetFileSize(obj);
10598 + valid |= ATTR_SIZE;
10599 +
10600 + attr->ia_valid = valid;
10601 +
10602 + return YAFFS_OK;
10603 +
10604 +}
10605 +
10606 +#endif
10607 +
10608 +#if 0
10609 +int yaffs_DumpObject(yaffs_Object * obj)
10610 +{
10611 + YCHAR name[257];
10612 +
10613 + yaffs_GetObjectName(obj, name, 256);
10614 +
10615 + T(YAFFS_TRACE_ALWAYS,
10616 + (TSTR
10617 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
10618 + " chunk %d type %d size %d\n"
10619 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
10620 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
10621 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
10622 +
10623 + return YAFFS_OK;
10624 +}
10625 +#endif
10626 +
10627 +/*---------------------------- Initialisation code -------------------------------------- */
10628 +
10629 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
10630 +{
10631 +
10632 + /* Common functions, gotta have */
10633 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
10634 + return 0;
10635 +
10636 +#ifdef CONFIG_YAFFS_YAFFS2
10637 +
10638 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
10639 + if (dev->writeChunkWithTagsToNAND &&
10640 + dev->readChunkWithTagsFromNAND &&
10641 + !dev->writeChunkToNAND &&
10642 + !dev->readChunkFromNAND &&
10643 + dev->markNANDBlockBad && dev->queryNANDBlock)
10644 + return 1;
10645 +#endif
10646 +
10647 + /* Can use the "spare" style interface for yaffs1 */
10648 + if (!dev->isYaffs2 &&
10649 + !dev->writeChunkWithTagsToNAND &&
10650 + !dev->readChunkWithTagsFromNAND &&
10651 + dev->writeChunkToNAND &&
10652 + dev->readChunkFromNAND &&
10653 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
10654 + return 1;
10655 +
10656 + return 0; /* bad */
10657 +}
10658 +
10659 +
10660 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
10661 +{
10662 + /* Initialise the unlinked, deleted, root and lost and found directories */
10663 +
10664 + dev->lostNFoundDir = dev->rootDir = NULL;
10665 + dev->unlinkedDir = dev->deletedDir = NULL;
10666 +
10667 + dev->unlinkedDir =
10668 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
10669 +
10670 + dev->deletedDir =
10671 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
10672 +
10673 + dev->rootDir =
10674 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
10675 + YAFFS_ROOT_MODE | S_IFDIR);
10676 + dev->lostNFoundDir =
10677 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
10678 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
10679 +
10680 + if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
10681 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
10682 + return YAFFS_OK;
10683 + }
10684 +
10685 + return YAFFS_FAIL;
10686 +}
10687 +
10688 +int yaffs_GutsInitialise(yaffs_Device * dev)
10689 +{
10690 + int init_failed = 0;
10691 + unsigned x;
10692 + int bits;
10693 +
10694 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
10695 +
10696 + /* Check stuff that must be set */
10697 +
10698 + if (!dev) {
10699 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
10700 + return YAFFS_FAIL;
10701 + }
10702 +
10703 + dev->internalStartBlock = dev->startBlock;
10704 + dev->internalEndBlock = dev->endBlock;
10705 + dev->blockOffset = 0;
10706 + dev->chunkOffset = 0;
10707 + dev->nFreeChunks = 0;
10708 +
10709 + if (dev->startBlock == 0) {
10710 + dev->internalStartBlock = dev->startBlock + 1;
10711 + dev->internalEndBlock = dev->endBlock + 1;
10712 + dev->blockOffset = 1;
10713 + dev->chunkOffset = dev->nChunksPerBlock;
10714 + }
10715 +
10716 + /* Check geometry parameters. */
10717 +
10718 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
10719 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
10720 + dev->nChunksPerBlock < 2 ||
10721 + dev->nReservedBlocks < 2 ||
10722 + dev->internalStartBlock <= 0 ||
10723 + dev->internalEndBlock <= 0 ||
10724 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
10725 + ) {
10726 + T(YAFFS_TRACE_ALWAYS,
10727 + (TSTR
10728 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
10729 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
10730 + return YAFFS_FAIL;
10731 + }
10732 +
10733 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
10734 + T(YAFFS_TRACE_ALWAYS,
10735 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
10736 + return YAFFS_FAIL;
10737 + }
10738 +
10739 + /* Got the right mix of functions? */
10740 + if (!yaffs_CheckDevFunctions(dev)) {
10741 + /* Function missing */
10742 + T(YAFFS_TRACE_ALWAYS,
10743 + (TSTR
10744 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
10745 +
10746 + return YAFFS_FAIL;
10747 + }
10748 +
10749 + /* This is really a compilation check. */
10750 + if (!yaffs_CheckStructures()) {
10751 + T(YAFFS_TRACE_ALWAYS,
10752 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
10753 + return YAFFS_FAIL;
10754 + }
10755 +
10756 + if (dev->isMounted) {
10757 + T(YAFFS_TRACE_ALWAYS,
10758 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
10759 + return YAFFS_FAIL;
10760 + }
10761 +
10762 + /* Finished with most checks. One or two more checks happen later on too. */
10763 +
10764 + dev->isMounted = 1;
10765 +
10766 +
10767 +
10768 + /* OK now calculate a few things for the device */
10769 +
10770 + /*
10771 + * Calculate all the chunk size manipulation numbers:
10772 + */
10773 + /* Start off assuming it is a power of 2 */
10774 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
10775 + dev->chunkMask = (1<<dev->chunkShift) - 1;
10776 +
10777 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
10778 + /* Yes it is a power of 2, disable crumbs */
10779 + dev->crumbMask = 0;
10780 + dev->crumbShift = 0;
10781 + dev->crumbsPerChunk = 0;
10782 + } else {
10783 + /* Not a power of 2, use crumbs instead */
10784 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
10785 + dev->crumbMask = (1<<dev->crumbShift)-1;
10786 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
10787 + dev->chunkShift = 0;
10788 + dev->chunkMask = 0;
10789 + }
10790 +
10791 +
10792 + /*
10793 + * Calculate chunkGroupBits.
10794 + * We need to find the next power of 2 > than internalEndBlock
10795 + */
10796 +
10797 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
10798 +
10799 + bits = ShiftsGE(x);
10800 +
10801 + /* Set up tnode width if wide tnodes are enabled. */
10802 + if(!dev->wideTnodesDisabled){
10803 + /* bits must be even so that we end up with 32-bit words */
10804 + if(bits & 1)
10805 + bits++;
10806 + if(bits < 16)
10807 + dev->tnodeWidth = 16;
10808 + else
10809 + dev->tnodeWidth = bits;
10810 + }
10811 + else
10812 + dev->tnodeWidth = 16;
10813 +
10814 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
10815 +
10816 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
10817 + * so if the bitwidth of the
10818 + * chunk range we're using is greater than 16 we need
10819 + * to figure out chunk shift and chunkGroupSize
10820 + */
10821 +
10822 + if (bits <= dev->tnodeWidth)
10823 + dev->chunkGroupBits = 0;
10824 + else
10825 + dev->chunkGroupBits = bits - dev->tnodeWidth;
10826 +
10827 +
10828 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
10829 +
10830 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
10831 + /* We have a problem because the soft delete won't work if
10832 + * the chunk group size > chunks per block.
10833 + * This can be remedied by using larger "virtual blocks".
10834 + */
10835 + T(YAFFS_TRACE_ALWAYS,
10836 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
10837 +
10838 + return YAFFS_FAIL;
10839 + }
10840 +
10841 + /* OK, we've finished verifying the device, lets continue with initialisation */
10842 +
10843 + /* More device initialisation */
10844 + dev->garbageCollections = 0;
10845 + dev->passiveGarbageCollections = 0;
10846 + dev->currentDirtyChecker = 0;
10847 + dev->bufferedBlock = -1;
10848 + dev->doingBufferedBlockRewrite = 0;
10849 + dev->nDeletedFiles = 0;
10850 + dev->nBackgroundDeletions = 0;
10851 + dev->nUnlinkedFiles = 0;
10852 + dev->eccFixed = 0;
10853 + dev->eccUnfixed = 0;
10854 + dev->tagsEccFixed = 0;
10855 + dev->tagsEccUnfixed = 0;
10856 + dev->nErasureFailures = 0;
10857 + dev->nErasedBlocks = 0;
10858 + dev->isDoingGC = 0;
10859 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
10860 +
10861 + /* Initialise temporary buffers and caches. */
10862 + if(!yaffs_InitialiseTempBuffers(dev))
10863 + init_failed = 1;
10864 +
10865 + dev->srCache = NULL;
10866 + dev->gcCleanupList = NULL;
10867 +
10868 +
10869 + if (!init_failed &&
10870 + dev->nShortOpCaches > 0) {
10871 + int i;
10872 + __u8 *buf;
10873 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
10874 +
10875 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
10876 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
10877 + }
10878 +
10879 + buf = dev->srCache = YMALLOC(srCacheBytes);
10880 +
10881 + if(dev->srCache)
10882 + memset(dev->srCache,0,srCacheBytes);
10883 +
10884 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
10885 + dev->srCache[i].object = NULL;
10886 + dev->srCache[i].lastUse = 0;
10887 + dev->srCache[i].dirty = 0;
10888 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
10889 + }
10890 + if(!buf)
10891 + init_failed = 1;
10892 +
10893 + dev->srLastUse = 0;
10894 + }
10895 +
10896 + dev->cacheHits = 0;
10897 +
10898 + if(!init_failed){
10899 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
10900 + if(!dev->gcCleanupList)
10901 + init_failed = 1;
10902 + }
10903 +
10904 + if (dev->isYaffs2) {
10905 + dev->useHeaderFileSize = 1;
10906 + }
10907 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10908 + init_failed = 1;
10909 +
10910 + yaffs_InitialiseTnodes(dev);
10911 + yaffs_InitialiseObjects(dev);
10912 +
10913 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10914 + init_failed = 1;
10915 +
10916 +
10917 + if(!init_failed){
10918 + /* Now scan the flash. */
10919 + if (dev->isYaffs2) {
10920 + if(yaffs_CheckpointRestore(dev)) {
10921 + T(YAFFS_TRACE_ALWAYS,
10922 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
10923 + } else {
10924 +
10925 + /* Clean up the mess caused by an aborted checkpoint load
10926 + * and scan backwards.
10927 + */
10928 + yaffs_DeinitialiseBlocks(dev);
10929 + yaffs_DeinitialiseTnodes(dev);
10930 + yaffs_DeinitialiseObjects(dev);
10931 +
10932 +
10933 + dev->nErasedBlocks = 0;
10934 + dev->nFreeChunks = 0;
10935 + dev->allocationBlock = -1;
10936 + dev->allocationPage = -1;
10937 + dev->nDeletedFiles = 0;
10938 + dev->nUnlinkedFiles = 0;
10939 + dev->nBackgroundDeletions = 0;
10940 + dev->oldestDirtySequence = 0;
10941 +
10942 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10943 + init_failed = 1;
10944 +
10945 + yaffs_InitialiseTnodes(dev);
10946 + yaffs_InitialiseObjects(dev);
10947 +
10948 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10949 + init_failed = 1;
10950 +
10951 + if(!init_failed && !yaffs_ScanBackwards(dev))
10952 + init_failed = 1;
10953 + }
10954 + }else
10955 + if(!yaffs_Scan(dev))
10956 + init_failed = 1;
10957 + }
10958 +
10959 + if(init_failed){
10960 + /* Clean up the mess */
10961 + T(YAFFS_TRACE_TRACING,
10962 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
10963 +
10964 + yaffs_Deinitialise(dev);
10965 + return YAFFS_FAIL;
10966 + }
10967 +
10968 + /* Zero out stats */
10969 + dev->nPageReads = 0;
10970 + dev->nPageWrites = 0;
10971 + dev->nBlockErasures = 0;
10972 + dev->nGCCopies = 0;
10973 + dev->nRetriedWrites = 0;
10974 +
10975 + dev->nRetiredBlocks = 0;
10976 +
10977 + yaffs_VerifyFreeChunks(dev);
10978 + yaffs_VerifyBlocks(dev);
10979 +
10980 +
10981 + T(YAFFS_TRACE_TRACING,
10982 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
10983 + return YAFFS_OK;
10984 +
10985 +}
10986 +
10987 +void yaffs_Deinitialise(yaffs_Device * dev)
10988 +{
10989 + if (dev->isMounted) {
10990 + int i;
10991 +
10992 + yaffs_DeinitialiseBlocks(dev);
10993 + yaffs_DeinitialiseTnodes(dev);
10994 + yaffs_DeinitialiseObjects(dev);
10995 + if (dev->nShortOpCaches > 0 &&
10996 + dev->srCache) {
10997 +
10998 + for (i = 0; i < dev->nShortOpCaches; i++) {
10999 + if(dev->srCache[i].data)
11000 + YFREE(dev->srCache[i].data);
11001 + dev->srCache[i].data = NULL;
11002 + }
11003 +
11004 + YFREE(dev->srCache);
11005 + dev->srCache = NULL;
11006 + }
11007 +
11008 + YFREE(dev->gcCleanupList);
11009 +
11010 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
11011 + YFREE(dev->tempBuffer[i].buffer);
11012 + }
11013 +
11014 + dev->isMounted = 0;
11015 + }
11016 +
11017 +}
11018 +
11019 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
11020 +{
11021 + int nFree;
11022 + int b;
11023 +
11024 + yaffs_BlockInfo *blk;
11025 +
11026 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11027 + b++) {
11028 + blk = yaffs_GetBlockInfo(dev, b);
11029 +
11030 + switch (blk->blockState) {
11031 + case YAFFS_BLOCK_STATE_EMPTY:
11032 + case YAFFS_BLOCK_STATE_ALLOCATING:
11033 + case YAFFS_BLOCK_STATE_COLLECTING:
11034 + case YAFFS_BLOCK_STATE_FULL:
11035 + nFree +=
11036 + (dev->nChunksPerBlock - blk->pagesInUse +
11037 + blk->softDeletions);
11038 + break;
11039 + default:
11040 + break;
11041 + }
11042 +
11043 + }
11044 +
11045 + return nFree;
11046 +}
11047 +
11048 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
11049 +{
11050 + /* This is what we report to the outside world */
11051 +
11052 + int nFree;
11053 + int nDirtyCacheChunks;
11054 + int blocksForCheckpoint;
11055 +
11056 +#if 1
11057 + nFree = dev->nFreeChunks;
11058 +#else
11059 + nFree = yaffs_CountFreeChunks(dev);
11060 +#endif
11061 +
11062 + nFree += dev->nDeletedFiles;
11063 +
11064 + /* Now count the number of dirty chunks in the cache and subtract those */
11065 +
11066 + {
11067 + int i;
11068 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11069 + if (dev->srCache[i].dirty)
11070 + nDirtyCacheChunks++;
11071 + }
11072 + }
11073 +
11074 + nFree -= nDirtyCacheChunks;
11075 +
11076 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11077 +
11078 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11079 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
11080 + if(blocksForCheckpoint < 0)
11081 + blocksForCheckpoint = 0;
11082 +
11083 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11084 +
11085 + if (nFree < 0)
11086 + nFree = 0;
11087 +
11088 + return nFree;
11089 +
11090 +}
11091 +
11092 +static int yaffs_freeVerificationFailures;
11093 +
11094 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
11095 +{
11096 + int counted;
11097 + int difference;
11098 +
11099 + if(yaffs_SkipVerification(dev))
11100 + return;
11101 +
11102 + counted = yaffs_CountFreeChunks(dev);
11103 +
11104 + difference = dev->nFreeChunks - counted;
11105 +
11106 + if (difference) {
11107 + T(YAFFS_TRACE_ALWAYS,
11108 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11109 + dev->nFreeChunks, counted, difference));
11110 + yaffs_freeVerificationFailures++;
11111 + }
11112 +}
11113 +
11114 +/*---------------------------------------- YAFFS test code ----------------------*/
11115 +
11116 +#define yaffs_CheckStruct(structure,syze, name) \
11117 + if(sizeof(structure) != syze) \
11118 + { \
11119 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
11120 + name,syze,sizeof(structure))); \
11121 + return YAFFS_FAIL; \
11122 + }
11123 +
11124 +static int yaffs_CheckStructures(void)
11125 +{
11126 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
11127 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
11128 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
11129 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11130 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
11131 +#endif
11132 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
11133 +
11134 + return YAFFS_OK;
11135 +}
11136 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.h linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h
11137 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
11138 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h 2007-06-08 14:07:26.000000000 +0200
11139 @@ -0,0 +1,902 @@
11140 +/*
11141 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11142 + *
11143 + * Copyright (C) 2002-2007 Aleph One Ltd.
11144 + * for Toby Churchill Ltd and Brightstar Engineering
11145 + *
11146 + * Created by Charles Manning <charles@aleph1.co.uk>
11147 + *
11148 + * This program is free software; you can redistribute it and/or modify
11149 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11150 + * published by the Free Software Foundation.
11151 + *
11152 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11153 + */
11154 +
11155 +#ifndef __YAFFS_GUTS_H__
11156 +#define __YAFFS_GUTS_H__
11157 +
11158 +#include "devextras.h"
11159 +#include "yportenv.h"
11160 +
11161 +#define YAFFS_OK 1
11162 +#define YAFFS_FAIL 0
11163 +
11164 +/* Give us a Y=0x59,
11165 + * Give us an A=0x41,
11166 + * Give us an FF=0xFF
11167 + * Give us an S=0x53
11168 + * And what have we got...
11169 + */
11170 +#define YAFFS_MAGIC 0x5941FF53
11171 +
11172 +#define YAFFS_NTNODES_LEVEL0 16
11173 +#define YAFFS_TNODES_LEVEL0_BITS 4
11174 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11175 +
11176 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11177 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11178 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11179 +#define YAFFS_TNODES_MAX_LEVEL 6
11180 +
11181 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11182 +#define YAFFS_BYTES_PER_SPARE 16
11183 +#define YAFFS_BYTES_PER_CHUNK 512
11184 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11185 +#define YAFFS_CHUNKS_PER_BLOCK 32
11186 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11187 +#endif
11188 +
11189 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11190 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11191 +
11192 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11193 +
11194 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11195 +
11196 +#define YAFFS_ALLOCATION_NOBJECTS 100
11197 +#define YAFFS_ALLOCATION_NTNODES 100
11198 +#define YAFFS_ALLOCATION_NLINKS 100
11199 +
11200 +#define YAFFS_NOBJECT_BUCKETS 256
11201 +
11202 +
11203 +#define YAFFS_OBJECT_SPACE 0x40000
11204 +
11205 +#define YAFFS_CHECKPOINT_VERSION 3
11206 +
11207 +#ifdef CONFIG_YAFFS_UNICODE
11208 +#define YAFFS_MAX_NAME_LENGTH 127
11209 +#define YAFFS_MAX_ALIAS_LENGTH 79
11210 +#else
11211 +#define YAFFS_MAX_NAME_LENGTH 255
11212 +#define YAFFS_MAX_ALIAS_LENGTH 159
11213 +#endif
11214 +
11215 +#define YAFFS_SHORT_NAME_LENGTH 15
11216 +
11217 +/* Some special object ids for pseudo objects */
11218 +#define YAFFS_OBJECTID_ROOT 1
11219 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11220 +#define YAFFS_OBJECTID_UNLINKED 3
11221 +#define YAFFS_OBJECTID_DELETED 4
11222 +
11223 +/* Sseudo object ids for checkpointing */
11224 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11225 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11226 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11227 +
11228 +/* */
11229 +
11230 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11231 +
11232 +#define YAFFS_N_TEMP_BUFFERS 4
11233 +
11234 +/* We limit the number attempts at sucessfully saving a chunk of data.
11235 + * Small-page devices have 32 pages per block; large-page devices have 64.
11236 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11237 + */
11238 +#define YAFFS_WR_ATTEMPTS (5*64)
11239 +
11240 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11241 + * The range is limited slightly to help distinguish bad numbers from good.
11242 + * This also allows us to perhaps in the future use special numbers for
11243 + * special purposes.
11244 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11245 + * and is a larger number than the lifetime of a 2GB device.
11246 + */
11247 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11248 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11249 +
11250 +/* ChunkCache is used for short read/write operations.*/
11251 +typedef struct {
11252 + struct yaffs_ObjectStruct *object;
11253 + int chunkId;
11254 + int lastUse;
11255 + int dirty;
11256 + int nBytes; /* Only valid if the cache is dirty */
11257 + int locked; /* Can't push out or flush while locked. */
11258 +#ifdef CONFIG_YAFFS_YAFFS2
11259 + __u8 *data;
11260 +#else
11261 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11262 +#endif
11263 +} yaffs_ChunkCache;
11264 +
11265 +
11266 +
11267 +/* Tags structures in RAM
11268 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11269 + * the structure size will get blown out.
11270 + */
11271 +
11272 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11273 +typedef struct {
11274 + unsigned chunkId:20;
11275 + unsigned serialNumber:2;
11276 + unsigned byteCount:10;
11277 + unsigned objectId:18;
11278 + unsigned ecc:12;
11279 + unsigned unusedStuff:2;
11280 +
11281 +} yaffs_Tags;
11282 +
11283 +typedef union {
11284 + yaffs_Tags asTags;
11285 + __u8 asBytes[8];
11286 +} yaffs_TagsUnion;
11287 +
11288 +#endif
11289 +
11290 +/* Stuff used for extended tags in YAFFS2 */
11291 +
11292 +typedef enum {
11293 + YAFFS_ECC_RESULT_UNKNOWN,
11294 + YAFFS_ECC_RESULT_NO_ERROR,
11295 + YAFFS_ECC_RESULT_FIXED,
11296 + YAFFS_ECC_RESULT_UNFIXED
11297 +} yaffs_ECCResult;
11298 +
11299 +typedef enum {
11300 + YAFFS_OBJECT_TYPE_UNKNOWN,
11301 + YAFFS_OBJECT_TYPE_FILE,
11302 + YAFFS_OBJECT_TYPE_SYMLINK,
11303 + YAFFS_OBJECT_TYPE_DIRECTORY,
11304 + YAFFS_OBJECT_TYPE_HARDLINK,
11305 + YAFFS_OBJECT_TYPE_SPECIAL
11306 +} yaffs_ObjectType;
11307 +
11308 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11309 +
11310 +typedef struct {
11311 +
11312 + unsigned validMarker0;
11313 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11314 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11315 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11316 + unsigned byteCount; /* Only valid for data chunks */
11317 +
11318 + /* The following stuff only has meaning when we read */
11319 + yaffs_ECCResult eccResult;
11320 + unsigned blockBad;
11321 +
11322 + /* YAFFS 1 stuff */
11323 + unsigned chunkDeleted; /* The chunk is marked deleted */
11324 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11325 +
11326 + /* YAFFS2 stuff */
11327 + unsigned sequenceNumber; /* The sequence number of this block */
11328 +
11329 + /* Extra info if this is an object header (YAFFS2 only) */
11330 +
11331 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11332 + unsigned extraParentObjectId; /* The parent object */
11333 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11334 + unsigned extraShadows; /* Does this shadow another object? */
11335 +
11336 + yaffs_ObjectType extraObjectType; /* What object type? */
11337 +
11338 + unsigned extraFileLength; /* Length if it is a file */
11339 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11340 +
11341 + unsigned validMarker1;
11342 +
11343 +} yaffs_ExtendedTags;
11344 +
11345 +/* Spare structure for YAFFS1 */
11346 +typedef struct {
11347 + __u8 tagByte0;
11348 + __u8 tagByte1;
11349 + __u8 tagByte2;
11350 + __u8 tagByte3;
11351 + __u8 pageStatus; /* set to 0 to delete the chunk */
11352 + __u8 blockStatus;
11353 + __u8 tagByte4;
11354 + __u8 tagByte5;
11355 + __u8 ecc1[3];
11356 + __u8 tagByte6;
11357 + __u8 tagByte7;
11358 + __u8 ecc2[3];
11359 +} yaffs_Spare;
11360 +
11361 +/*Special structure for passing through to mtd */
11362 +struct yaffs_NANDSpare {
11363 + yaffs_Spare spare;
11364 + int eccres1;
11365 + int eccres2;
11366 +};
11367 +
11368 +/* Block data in RAM */
11369 +
11370 +typedef enum {
11371 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11372 +
11373 + YAFFS_BLOCK_STATE_SCANNING,
11374 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11375 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11376 + * but it needs to be scanned to determine its true state.
11377 + * This state is only valid during yaffs_Scan.
11378 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11379 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11380 + */
11381 +
11382 + YAFFS_BLOCK_STATE_EMPTY,
11383 + /* This block is empty */
11384 +
11385 + YAFFS_BLOCK_STATE_ALLOCATING,
11386 + /* This block is partially allocated.
11387 + * At least one page holds valid data.
11388 + * This is the one currently being used for page
11389 + * allocation. Should never be more than one of these
11390 + */
11391 +
11392 + YAFFS_BLOCK_STATE_FULL,
11393 + /* All the pages in this block have been allocated.
11394 + */
11395 +
11396 + YAFFS_BLOCK_STATE_DIRTY,
11397 + /* All pages have been allocated and deleted.
11398 + * Erase me, reuse me.
11399 + */
11400 +
11401 + YAFFS_BLOCK_STATE_CHECKPOINT,
11402 + /* This block is assigned to holding checkpoint data.
11403 + */
11404 +
11405 + YAFFS_BLOCK_STATE_COLLECTING,
11406 + /* This block is being garbage collected */
11407 +
11408 + YAFFS_BLOCK_STATE_DEAD
11409 + /* This block has failed and is not in use */
11410 +} yaffs_BlockState;
11411 +
11412 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11413 +
11414 +
11415 +typedef struct {
11416 +
11417 + int softDeletions:10; /* number of soft deleted pages */
11418 + int pagesInUse:10; /* number of pages in use */
11419 + yaffs_BlockState blockState:4; /* One of the above block states */
11420 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11421 + /* and retire the block. */
11422 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
11423 + __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
11424 + It should be prioritised for GC */
11425 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11426 +
11427 +#ifdef CONFIG_YAFFS_YAFFS2
11428 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11429 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11430 +#endif
11431 +
11432 +} yaffs_BlockInfo;
11433 +
11434 +/* -------------------------- Object structure -------------------------------*/
11435 +/* This is the object structure as stored on NAND */
11436 +
11437 +typedef struct {
11438 + yaffs_ObjectType type;
11439 +
11440 + /* Apply to everything */
11441 + int parentObjectId;
11442 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11443 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11444 +
11445 + /* Thes following apply to directories, files, symlinks - not hard links */
11446 + __u32 yst_mode; /* protection */
11447 +
11448 +#ifdef CONFIG_YAFFS_WINCE
11449 + __u32 notForWinCE[5];
11450 +#else
11451 + __u32 yst_uid;
11452 + __u32 yst_gid;
11453 + __u32 yst_atime;
11454 + __u32 yst_mtime;
11455 + __u32 yst_ctime;
11456 +#endif
11457 +
11458 + /* File size applies to files only */
11459 + int fileSize;
11460 +
11461 + /* Equivalent object id applies to hard links only. */
11462 + int equivalentObjectId;
11463 +
11464 + /* Alias is for symlinks only. */
11465 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11466 +
11467 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11468 +
11469 +#ifdef CONFIG_YAFFS_WINCE
11470 + __u32 win_ctime[2];
11471 + __u32 win_atime[2];
11472 + __u32 win_mtime[2];
11473 + __u32 roomToGrow[4];
11474 +#else
11475 + __u32 roomToGrow[10];
11476 +#endif
11477 +
11478 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11479 +
11480 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11481 + __u32 isShrink;
11482 +
11483 +} yaffs_ObjectHeader;
11484 +
11485 +/*--------------------------- Tnode -------------------------- */
11486 +
11487 +union yaffs_Tnode_union {
11488 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11489 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11490 +#else
11491 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11492 +#endif
11493 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11494 +
11495 +};
11496 +
11497 +typedef union yaffs_Tnode_union yaffs_Tnode;
11498 +
11499 +struct yaffs_TnodeList_struct {
11500 + struct yaffs_TnodeList_struct *next;
11501 + yaffs_Tnode *tnodes;
11502 +};
11503 +
11504 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11505 +
11506 +/*------------------------ Object -----------------------------*/
11507 +/* An object can be one of:
11508 + * - a directory (no data, has children links
11509 + * - a regular file (data.... not prunes :->).
11510 + * - a symlink [symbolic link] (the alias).
11511 + * - a hard link
11512 + */
11513 +
11514 +typedef struct {
11515 + __u32 fileSize;
11516 + __u32 scannedFileSize;
11517 + __u32 shrinkSize;
11518 + int topLevel;
11519 + yaffs_Tnode *top;
11520 +} yaffs_FileStructure;
11521 +
11522 +typedef struct {
11523 + struct list_head children; /* list of child links */
11524 +} yaffs_DirectoryStructure;
11525 +
11526 +typedef struct {
11527 + YCHAR *alias;
11528 +} yaffs_SymLinkStructure;
11529 +
11530 +typedef struct {
11531 + struct yaffs_ObjectStruct *equivalentObject;
11532 + __u32 equivalentObjectId;
11533 +} yaffs_HardLinkStructure;
11534 +
11535 +typedef union {
11536 + yaffs_FileStructure fileVariant;
11537 + yaffs_DirectoryStructure directoryVariant;
11538 + yaffs_SymLinkStructure symLinkVariant;
11539 + yaffs_HardLinkStructure hardLinkVariant;
11540 +} yaffs_ObjectVariant;
11541 +
11542 +struct yaffs_ObjectStruct {
11543 + __u8 deleted:1; /* This should only apply to unlinked files. */
11544 + __u8 softDeleted:1; /* it has also been soft deleted */
11545 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11546 + __u8 fake:1; /* A fake object has no presence on NAND. */
11547 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11548 + __u8 unlinkAllowed:1;
11549 + __u8 dirty:1; /* the object needs to be written to flash */
11550 + __u8 valid:1; /* When the file system is being loaded up, this
11551 + * object might be created before the data
11552 + * is available (ie. file data records appear before the header).
11553 + */
11554 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11555 +
11556 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11557 + * still in the inode cache. Free of object is defered.
11558 + * until the inode is released.
11559 + */
11560 +
11561 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11562 + __u16 sum; /* sum of the name to speed searching */
11563 +
11564 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11565 +
11566 + struct list_head hashLink; /* list of objects in this hash bucket */
11567 +
11568 + struct list_head hardLinks; /* all the equivalent hard linked objects */
11569 +
11570 + /* directory structure stuff */
11571 + /* also used for linking up the free list */
11572 + struct yaffs_ObjectStruct *parent;
11573 + struct list_head siblings;
11574 +
11575 + /* Where's my object header in NAND? */
11576 + int chunkId;
11577 +
11578 + int nDataChunks; /* Number of data chunks attached to the file. */
11579 +
11580 + __u32 objectId; /* the object id value */
11581 +
11582 + __u32 yst_mode;
11583 +
11584 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11585 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11586 +#endif
11587 +
11588 +#ifndef __KERNEL__
11589 + __u32 inUse;
11590 +#endif
11591 +
11592 +#ifdef CONFIG_YAFFS_WINCE
11593 + __u32 win_ctime[2];
11594 + __u32 win_mtime[2];
11595 + __u32 win_atime[2];
11596 +#else
11597 + __u32 yst_uid;
11598 + __u32 yst_gid;
11599 + __u32 yst_atime;
11600 + __u32 yst_mtime;
11601 + __u32 yst_ctime;
11602 +#endif
11603 +
11604 + __u32 yst_rdev;
11605 +
11606 +#ifdef __KERNEL__
11607 + struct inode *myInode;
11608 +
11609 +#endif
11610 +
11611 + yaffs_ObjectType variantType;
11612 +
11613 + yaffs_ObjectVariant variant;
11614 +
11615 +};
11616 +
11617 +typedef struct yaffs_ObjectStruct yaffs_Object;
11618 +
11619 +struct yaffs_ObjectList_struct {
11620 + yaffs_Object *objects;
11621 + struct yaffs_ObjectList_struct *next;
11622 +};
11623 +
11624 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
11625 +
11626 +typedef struct {
11627 + struct list_head list;
11628 + int count;
11629 +} yaffs_ObjectBucket;
11630 +
11631 +
11632 +/* yaffs_CheckpointObject holds the definition of an object as dumped
11633 + * by checkpointing.
11634 + */
11635 +
11636 +typedef struct {
11637 + int structType;
11638 + __u32 objectId;
11639 + __u32 parentId;
11640 + int chunkId;
11641 +
11642 + yaffs_ObjectType variantType:3;
11643 + __u8 deleted:1;
11644 + __u8 softDeleted:1;
11645 + __u8 unlinked:1;
11646 + __u8 fake:1;
11647 + __u8 renameAllowed:1;
11648 + __u8 unlinkAllowed:1;
11649 + __u8 serial;
11650 +
11651 + int nDataChunks;
11652 + __u32 fileSizeOrEquivalentObjectId;
11653 +
11654 +}yaffs_CheckpointObject;
11655 +
11656 +/*--------------------- Temporary buffers ----------------
11657 + *
11658 + * These are chunk-sized working buffers. Each device has a few
11659 + */
11660 +
11661 +typedef struct {
11662 + __u8 *buffer;
11663 + int line; /* track from whence this buffer was allocated */
11664 + int maxLine;
11665 +} yaffs_TempBuffer;
11666 +
11667 +/*----------------- Device ---------------------------------*/
11668 +
11669 +struct yaffs_DeviceStruct {
11670 + struct list_head devList;
11671 + const char *name;
11672 +
11673 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
11674 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
11675 + int nChunksPerBlock; /* does not need to be a power of 2 */
11676 + int nBytesPerSpare; /* spare area size */
11677 + int startBlock; /* Start block we're allowed to use */
11678 + int endBlock; /* End block we're allowed to use */
11679 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
11680 + /* reserved blocks on NOR and RAM. */
11681 +
11682 +
11683 + /* Stuff used by the shared space checkpointing mechanism */
11684 + /* If this value is zero, then this mechanism is disabled */
11685 +
11686 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
11687 +
11688 +
11689 +
11690 +
11691 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
11692 + * the number of short op caches (don't use too many)
11693 + */
11694 +
11695 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
11696 +
11697 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
11698 +
11699 + void *genericDevice; /* Pointer to device context
11700 + * On an mtd this holds the mtd pointer.
11701 + */
11702 + void *superBlock;
11703 +
11704 + /* NAND access functions (Must be set before calling YAFFS)*/
11705 +
11706 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
11707 + int chunkInNAND, const __u8 * data,
11708 + const yaffs_Spare * spare);
11709 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
11710 + int chunkInNAND, __u8 * data,
11711 + yaffs_Spare * spare);
11712 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
11713 + int blockInNAND);
11714 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
11715 +
11716 +#ifdef CONFIG_YAFFS_YAFFS2
11717 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
11718 + int chunkInNAND, const __u8 * data,
11719 + const yaffs_ExtendedTags * tags);
11720 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
11721 + int chunkInNAND, __u8 * data,
11722 + yaffs_ExtendedTags * tags);
11723 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
11724 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
11725 + yaffs_BlockState * state, int *sequenceNumber);
11726 +#endif
11727 +
11728 + int isYaffs2;
11729 +
11730 + /* The removeObjectCallback function must be supplied by OS flavours that
11731 + * need it. The Linux kernel does not use this, but yaffs direct does use
11732 + * it to implement the faster readdir
11733 + */
11734 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
11735 +
11736 + /* Callback to mark the superblock dirsty */
11737 + void (*markSuperBlockDirty)(void * superblock);
11738 +
11739 + int wideTnodesDisabled; /* Set to disable wide tnodes */
11740 +
11741 +
11742 + /* End of stuff that must be set before initialisation. */
11743 +
11744 + /* Checkpoint control. Can be set before or after initialisation */
11745 + __u8 skipCheckpointRead;
11746 + __u8 skipCheckpointWrite;
11747 +
11748 + /* Runtime parameters. Set up by YAFFS. */
11749 +
11750 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
11751 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
11752 +
11753 + /* Stuff to support wide tnodes */
11754 + __u32 tnodeWidth;
11755 + __u32 tnodeMask;
11756 +
11757 + /* Stuff to support various file offses to chunk/offset translations */
11758 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
11759 + __u32 crumbMask;
11760 + __u32 crumbShift;
11761 + __u32 crumbsPerChunk;
11762 +
11763 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
11764 + __u32 chunkShift;
11765 + __u32 chunkMask;
11766 +
11767 +
11768 +#ifdef __KERNEL__
11769 +
11770 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
11771 + struct semaphore grossLock; /* Gross locking semaphore */
11772 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
11773 + * at compile time so we have to allocate it.
11774 + */
11775 + void (*putSuperFunc) (struct super_block * sb);
11776 +#endif
11777 +
11778 + int isMounted;
11779 +
11780 + int isCheckpointed;
11781 +
11782 +
11783 + /* Stuff to support block offsetting to support start block zero */
11784 + int internalStartBlock;
11785 + int internalEndBlock;
11786 + int blockOffset;
11787 + int chunkOffset;
11788 +
11789 +
11790 + /* Runtime checkpointing stuff */
11791 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
11792 + int checkpointByteCount;
11793 + int checkpointByteOffset;
11794 + __u8 *checkpointBuffer;
11795 + int checkpointOpenForWrite;
11796 + int blocksInCheckpoint;
11797 + int checkpointCurrentChunk;
11798 + int checkpointCurrentBlock;
11799 + int checkpointNextBlock;
11800 + int *checkpointBlockList;
11801 + int checkpointMaxBlocks;
11802 + __u32 checkpointSum;
11803 + __u32 checkpointXor;
11804 +
11805 + /* Block Info */
11806 + yaffs_BlockInfo *blockInfo;
11807 + __u8 *chunkBits; /* bitmap of chunks in use */
11808 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
11809 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
11810 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
11811 + * Must be consistent with nChunksPerBlock.
11812 + */
11813 +
11814 + int nErasedBlocks;
11815 + int allocationBlock; /* Current block being allocated off */
11816 + __u32 allocationPage;
11817 + int allocationBlockFinder; /* Used to search for next allocation block */
11818 +
11819 + /* Runtime state */
11820 + int nTnodesCreated;
11821 + yaffs_Tnode *freeTnodes;
11822 + int nFreeTnodes;
11823 + yaffs_TnodeList *allocatedTnodeList;
11824 +
11825 + int isDoingGC;
11826 +
11827 + int nObjectsCreated;
11828 + yaffs_Object *freeObjects;
11829 + int nFreeObjects;
11830 +
11831 + yaffs_ObjectList *allocatedObjectList;
11832 +
11833 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
11834 +
11835 + int nFreeChunks;
11836 +
11837 + int currentDirtyChecker; /* Used to find current dirtiest block */
11838 +
11839 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
11840 + int nonAggressiveSkip; /* GC state/mode */
11841 +
11842 + /* Statistcs */
11843 + int nPageWrites;
11844 + int nPageReads;
11845 + int nBlockErasures;
11846 + int nErasureFailures;
11847 + int nGCCopies;
11848 + int garbageCollections;
11849 + int passiveGarbageCollections;
11850 + int nRetriedWrites;
11851 + int nRetiredBlocks;
11852 + int eccFixed;
11853 + int eccUnfixed;
11854 + int tagsEccFixed;
11855 + int tagsEccUnfixed;
11856 + int nDeletions;
11857 + int nUnmarkedDeletions;
11858 +
11859 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
11860 +
11861 + /* Special directories */
11862 + yaffs_Object *rootDir;
11863 + yaffs_Object *lostNFoundDir;
11864 +
11865 + /* Buffer areas for storing data to recover from write failures TODO
11866 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
11867 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
11868 + */
11869 +
11870 + int bufferedBlock; /* Which block is buffered here? */
11871 + int doingBufferedBlockRewrite;
11872 +
11873 + yaffs_ChunkCache *srCache;
11874 + int srLastUse;
11875 +
11876 + int cacheHits;
11877 +
11878 + /* Stuff for background deletion and unlinked files.*/
11879 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
11880 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
11881 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
11882 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11883 + int nUnlinkedFiles; /* Count of unlinked files. */
11884 + int nBackgroundDeletions; /* Count of background deletions. */
11885 +
11886 +
11887 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
11888 + int maxTemp;
11889 + int unmanagedTempAllocations;
11890 + int unmanagedTempDeallocations;
11891 +
11892 + /* yaffs2 runtime stuff */
11893 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11894 + unsigned oldestDirtySequence;
11895 +
11896 +};
11897 +
11898 +typedef struct yaffs_DeviceStruct yaffs_Device;
11899 +
11900 +/* The static layout of bllock usage etc is stored in the super block header */
11901 +typedef struct {
11902 + int StructType;
11903 + int version;
11904 + int checkpointStartBlock;
11905 + int checkpointEndBlock;
11906 + int startBlock;
11907 + int endBlock;
11908 + int rfu[100];
11909 +} yaffs_SuperBlockHeader;
11910 +
11911 +/* The CheckpointDevice structure holds the device information that changes at runtime and
11912 + * must be preserved over unmount/mount cycles.
11913 + */
11914 +typedef struct {
11915 + int structType;
11916 + int nErasedBlocks;
11917 + int allocationBlock; /* Current block being allocated off */
11918 + __u32 allocationPage;
11919 + int nFreeChunks;
11920 +
11921 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11922 + int nUnlinkedFiles; /* Count of unlinked files. */
11923 + int nBackgroundDeletions; /* Count of background deletions. */
11924 +
11925 + /* yaffs2 runtime stuff */
11926 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11927 + unsigned oldestDirtySequence;
11928 +
11929 +} yaffs_CheckpointDevice;
11930 +
11931 +
11932 +typedef struct {
11933 + int structType;
11934 + __u32 magic;
11935 + __u32 version;
11936 + __u32 head;
11937 +} yaffs_CheckpointValidity;
11938 +
11939 +/* Function to manipulate block info */
11940 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
11941 +{
11942 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
11943 + T(YAFFS_TRACE_ERROR,
11944 + (TSTR
11945 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
11946 + blk));
11947 + YBUG();
11948 + }
11949 + return &dev->blockInfo[blk - dev->internalStartBlock];
11950 +}
11951 +
11952 +/*----------------------- YAFFS Functions -----------------------*/
11953 +
11954 +int yaffs_GutsInitialise(yaffs_Device * dev);
11955 +void yaffs_Deinitialise(yaffs_Device * dev);
11956 +
11957 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
11958 +
11959 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
11960 + yaffs_Object * newDir, const YCHAR * newName);
11961 +
11962 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
11963 +int yaffs_DeleteFile(yaffs_Object * obj);
11964 +
11965 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
11966 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
11967 +int yaffs_GetObjectInode(yaffs_Object * obj);
11968 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
11969 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
11970 +
11971 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
11972 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
11973 +
11974 +/* File operations */
11975 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
11976 + int nBytes);
11977 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
11978 + int nBytes, int writeThrough);
11979 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
11980 +
11981 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
11982 + __u32 mode, __u32 uid, __u32 gid);
11983 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
11984 +
11985 +/* Flushing and checkpointing */
11986 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
11987 +
11988 +int yaffs_CheckpointSave(yaffs_Device *dev);
11989 +int yaffs_CheckpointRestore(yaffs_Device *dev);
11990 +
11991 +/* Directory operations */
11992 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
11993 + __u32 mode, __u32 uid, __u32 gid);
11994 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
11995 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
11996 + int (*fn) (yaffs_Object *));
11997 +
11998 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
11999 +
12000 +/* Link operations */
12001 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
12002 + yaffs_Object * equivalentObject);
12003 +
12004 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
12005 +
12006 +/* Symlink operations */
12007 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
12008 + __u32 mode, __u32 uid, __u32 gid,
12009 + const YCHAR * alias);
12010 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
12011 +
12012 +/* Special inodes (fifos, sockets and devices) */
12013 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
12014 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
12015 +
12016 +/* Special directories */
12017 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
12018 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
12019 +
12020 +#ifdef CONFIG_YAFFS_WINCE
12021 +/* CONFIG_YAFFS_WINCE special stuff */
12022 +void yfsd_WinFileTimeNow(__u32 target[2]);
12023 +#endif
12024 +
12025 +#ifdef __KERNEL__
12026 +
12027 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
12028 +#endif
12029 +
12030 +/* Debug dump */
12031 +int yaffs_DumpObject(yaffs_Object * obj);
12032 +
12033 +void yaffs_GutsTest(yaffs_Device * dev);
12034 +
12035 +/* A few useful functions */
12036 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12037 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
12038 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
12039 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12040 +
12041 +#endif
12042 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c
12043 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
12044 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c 2007-06-08 14:07:26.000000000 +0200
12045 @@ -0,0 +1,241 @@
12046 +/*
12047 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12048 + *
12049 + * Copyright (C) 2002-2007 Aleph One Ltd.
12050 + * for Toby Churchill Ltd and Brightstar Engineering
12051 + *
12052 + * Created by Charles Manning <charles@aleph1.co.uk>
12053 + *
12054 + * This program is free software; you can redistribute it and/or modify
12055 + * it under the terms of the GNU General Public License version 2 as
12056 + * published by the Free Software Foundation.
12057 + */
12058 +
12059 +const char *yaffs_mtdif_c_version =
12060 + "$Id: yaffs_mtdif.c,v 1.19 2007-02-14 01:09:06 wookey Exp $";
12061 +
12062 +#include "yportenv.h"
12063 +
12064 +
12065 +#include "yaffs_mtdif.h"
12066 +
12067 +#include "linux/mtd/mtd.h"
12068 +#include "linux/types.h"
12069 +#include "linux/time.h"
12070 +#include "linux/mtd/nand.h"
12071 +
12072 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
12073 +static struct nand_oobinfo yaffs_oobinfo = {
12074 + .useecc = 1,
12075 + .eccbytes = 6,
12076 + .eccpos = {8, 9, 10, 13, 14, 15}
12077 +};
12078 +
12079 +static struct nand_oobinfo yaffs_noeccinfo = {
12080 + .useecc = 0,
12081 +};
12082 +#endif
12083 +
12084 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12085 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
12086 +{
12087 + oob[0] = spare->tagByte0;
12088 + oob[1] = spare->tagByte1;
12089 + oob[2] = spare->tagByte2;
12090 + oob[3] = spare->tagByte3;
12091 + oob[4] = spare->tagByte4;
12092 + oob[5] = spare->tagByte5 & 0x3f;
12093 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
12094 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
12095 + oob[6] = spare->tagByte6;
12096 + oob[7] = spare->tagByte7;
12097 +}
12098 +
12099 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
12100 +{
12101 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
12102 + spare->tagByte0 = oob[0];
12103 + spare->tagByte1 = oob[1];
12104 + spare->tagByte2 = oob[2];
12105 + spare->tagByte3 = oob[3];
12106 + spare->tagByte4 = oob[4];
12107 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
12108 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
12109 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
12110 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
12111 + spare->tagByte6 = oob[6];
12112 + spare->tagByte7 = oob[7];
12113 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
12114 +
12115 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
12116 +}
12117 +#endif
12118 +
12119 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12120 + const __u8 * data, const yaffs_Spare * spare)
12121 +{
12122 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12123 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12124 + struct mtd_oob_ops ops;
12125 +#endif
12126 + size_t dummy;
12127 + int retval = 0;
12128 +
12129 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12130 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12131 + __u8 spareAsBytes[8]; /* OOB */
12132 +
12133 + if (data && !spare)
12134 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
12135 + &dummy, data);
12136 + else if (spare) {
12137 + if (dev->useNANDECC) {
12138 + translate_spare2oob(spare, spareAsBytes);
12139 + ops.mode = MTD_OOB_AUTO;
12140 + ops.ooblen = 8; /* temp hack */
12141 + } else {
12142 + ops.mode = MTD_OOB_RAW;
12143 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12144 + }
12145 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12146 + ops.datbuf = (u8 *)data;
12147 + ops.ooboffs = 0;
12148 + ops.oobbuf = spareAsBytes;
12149 + retval = mtd->write_oob(mtd, addr, &ops);
12150 + }
12151 +#else
12152 + __u8 *spareAsBytes = (__u8 *) spare;
12153 +
12154 + if (data && spare) {
12155 + if (dev->useNANDECC)
12156 + retval =
12157 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12158 + &dummy, data, spareAsBytes,
12159 + &yaffs_oobinfo);
12160 + else
12161 + retval =
12162 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12163 + &dummy, data, spareAsBytes,
12164 + &yaffs_noeccinfo);
12165 + } else {
12166 + if (data)
12167 + retval =
12168 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12169 + data);
12170 + if (spare)
12171 + retval =
12172 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12173 + &dummy, spareAsBytes);
12174 + }
12175 +#endif
12176 +
12177 + if (retval == 0)
12178 + return YAFFS_OK;
12179 + else
12180 + return YAFFS_FAIL;
12181 +}
12182 +
12183 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12184 + yaffs_Spare * spare)
12185 +{
12186 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12187 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12188 + struct mtd_oob_ops ops;
12189 +#endif
12190 + size_t dummy;
12191 + int retval = 0;
12192 +
12193 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12194 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12195 + __u8 spareAsBytes[8]; /* OOB */
12196 +
12197 + if (data && !spare)
12198 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
12199 + &dummy, data);
12200 + else if (spare) {
12201 + if (dev->useNANDECC) {
12202 + ops.mode = MTD_OOB_AUTO;
12203 + ops.ooblen = 8; /* temp hack */
12204 + } else {
12205 + ops.mode = MTD_OOB_RAW;
12206 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12207 + }
12208 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12209 + ops.datbuf = data;
12210 + ops.ooboffs = 0;
12211 + ops.oobbuf = spareAsBytes;
12212 + retval = mtd->read_oob(mtd, addr, &ops);
12213 + if (dev->useNANDECC)
12214 + translate_oob2spare(spare, spareAsBytes);
12215 + }
12216 +#else
12217 + __u8 *spareAsBytes = (__u8 *) spare;
12218 +
12219 + if (data && spare) {
12220 + if (dev->useNANDECC) {
12221 + /* Careful, this call adds 2 ints */
12222 + /* to the end of the spare data. Calling function */
12223 + /* should allocate enough memory for spare, */
12224 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
12225 + retval =
12226 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12227 + &dummy, data, spareAsBytes,
12228 + &yaffs_oobinfo);
12229 + } else {
12230 + retval =
12231 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12232 + &dummy, data, spareAsBytes,
12233 + &yaffs_noeccinfo);
12234 + }
12235 + } else {
12236 + if (data)
12237 + retval =
12238 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12239 + data);
12240 + if (spare)
12241 + retval =
12242 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12243 + &dummy, spareAsBytes);
12244 + }
12245 +#endif
12246 +
12247 + if (retval == 0)
12248 + return YAFFS_OK;
12249 + else
12250 + return YAFFS_FAIL;
12251 +}
12252 +
12253 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
12254 +{
12255 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12256 + __u32 addr =
12257 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
12258 + * dev->nChunksPerBlock;
12259 + struct erase_info ei;
12260 + int retval = 0;
12261 +
12262 + ei.mtd = mtd;
12263 + ei.addr = addr;
12264 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
12265 + ei.time = 1000;
12266 + ei.retries = 2;
12267 + ei.callback = NULL;
12268 + ei.priv = (u_long) dev;
12269 +
12270 + /* Todo finish off the ei if required */
12271 +
12272 + sema_init(&dev->sem, 0);
12273 +
12274 + retval = mtd->erase(mtd, &ei);
12275 +
12276 + if (retval == 0)
12277 + return YAFFS_OK;
12278 + else
12279 + return YAFFS_FAIL;
12280 +}
12281 +
12282 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
12283 +{
12284 + return YAFFS_OK;
12285 +}
12286 +
12287 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h
12288 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
12289 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h 2007-06-08 14:07:26.000000000 +0200
12290 @@ -0,0 +1,27 @@
12291 +/*
12292 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12293 + *
12294 + * Copyright (C) 2002-2007 Aleph One Ltd.
12295 + * for Toby Churchill Ltd and Brightstar Engineering
12296 + *
12297 + * Created by Charles Manning <charles@aleph1.co.uk>
12298 + *
12299 + * This program is free software; you can redistribute it and/or modify
12300 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12301 + * published by the Free Software Foundation.
12302 + *
12303 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12304 + */
12305 +
12306 +#ifndef __YAFFS_MTDIF_H__
12307 +#define __YAFFS_MTDIF_H__
12308 +
12309 +#include "yaffs_guts.h"
12310 +
12311 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12312 + const __u8 * data, const yaffs_Spare * spare);
12313 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12314 + yaffs_Spare * spare);
12315 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
12316 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
12317 +#endif
12318 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c
12319 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c 1970-01-01 01:00:00.000000000 +0100
12320 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c 2007-06-08 14:07:26.000000000 +0200
12321 @@ -0,0 +1,434 @@
12322 +From ian@brightstareng.com Fri May 18 15:06:49 2007
12323 +From ian@brightstareng.com Fri May 18 15:08:21 2007
12324 +Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
12325 + by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
12326 + (envelope-from <ian@brightstareng.com>)
12327 + id 1Hp380-00011e-T6
12328 + for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
12329 +Received: from localhost (localhost.localdomain [127.0.0.1])
12330 + by zebra.brightstareng.com (Postfix) with ESMTP
12331 + id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
12332 +Received: from zebra.brightstareng.com ([127.0.0.1])
12333 + by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
12334 + id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
12335 +Received: from pippin (unknown [192.168.1.25])
12336 + by zebra.brightstareng.com (Postfix) with ESMTP
12337 + id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
12338 +From: Ian McDonnell <ian@brightstareng.com>
12339 +To: David Goodenough <david.goodenough@linkchoose.co.uk>
12340 +Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
12341 +Date: Fri, 18 May 2007 10:06:49 -0400
12342 +User-Agent: KMail/1.9.1
12343 +References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
12344 +In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
12345 +Cc: Andrea Conti <alyf@alyf.net>,
12346 + Charles Manning <manningc2@actrix.gen.nz>
12347 +MIME-Version: 1.0
12348 +Content-Type: Multipart/Mixed;
12349 + boundary="Boundary-00=_5LbTGmt62YoutxM"
12350 +Message-Id: <200705181006.49860.ian@brightstareng.com>
12351 +X-Virus-Scanned: by amavisd-new at brightstareng.com
12352 +Status: R
12353 +X-Status: NT
12354 +X-KMail-EncryptionState:
12355 +X-KMail-SignatureState:
12356 +X-KMail-MDN-Sent:
12357 +
12358 +--Boundary-00=_5LbTGmt62YoutxM
12359 +Content-Type: text/plain;
12360 + charset="iso-8859-15"
12361 +Content-Transfer-Encoding: 7bit
12362 +Content-Disposition: inline
12363 +
12364 +David, Andrea,
12365 +
12366 +On Friday 18 May 2007 08:34, you wrote:
12367 +> Yea team. With this fix in place (I put it in the wrong place
12368 +> at first) I can now mount and ls the Yaffs partition without
12369 +> an error messages!
12370 +
12371 +Good news!
12372 +
12373 +Attached is a newer yaffs_mtdif1.c with a bandaid to help the
12374 +2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
12375 +See the LINUX_VERSION_CODE conditional in
12376 +nandmtd1_ReadChunkWithTagsFromNAND.
12377 +
12378 +-imcd
12379 +
12380 +--Boundary-00=_5LbTGmt62YoutxM
12381 +Content-Type: text/x-csrc;
12382 + charset="iso-8859-15";
12383 + name="yaffs_mtdif1.c"
12384 +Content-Transfer-Encoding: 7bit
12385 +Content-Disposition: attachment;
12386 + filename="yaffs_mtdif1.c"
12387 +
12388 +/*
12389 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12390 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12391 + *
12392 + * Copyright (C) 2002 Aleph One Ltd.
12393 + * for Toby Churchill Ltd and Brightstar Engineering
12394 + *
12395 + * This program is free software; you can redistribute it and/or modify
12396 + * it under the terms of the GNU General Public License version 2 as
12397 + * published by the Free Software Foundation.
12398 + */
12399 +
12400 +/*
12401 + * This module provides the interface between yaffs_nand.c and the
12402 + * MTD API. This version is used when the MTD interface supports the
12403 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12404 + * and we have small-page NAND device.
12405 + *
12406 + * These functions are invoked via function pointers in yaffs_nand.c.
12407 + * This replaces functionality provided by functions in yaffs_mtdif.c
12408 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12409 + * called in yaffs_mtdif.c when the function pointers are NULL.
12410 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12411 + */
12412 +
12413 +#include "yportenv.h"
12414 +#include "yaffs_guts.h"
12415 +#include "yaffs_packedtags1.h"
12416 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12417 +
12418 +#include "linux/kernel.h"
12419 +#include "linux/version.h"
12420 +#include "linux/types.h"
12421 +#include "linux/mtd/mtd.h"
12422 +
12423 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12424 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12425 +
12426 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12427 +
12428 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12429 +# define YTAG1_SIZE 8
12430 +#else
12431 +# define YTAG1_SIZE 9
12432 +#endif
12433 +
12434 +#if 0
12435 +/* Use the following nand_ecclayout with MTD when using
12436 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12437 + * If you have existing Yaffs images and the byte order differs from this,
12438 + * adjust 'oobfree' to match your existing Yaffs data.
12439 + *
12440 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12441 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12442 + * the 9th byte.
12443 + *
12444 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12445 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12446 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12447 + * byte and B is the small-page bad-block indicator byte.
12448 + */
12449 +static struct nand_ecclayout nand_oob_16 = {
12450 + .eccbytes = 6,
12451 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12452 + .oobavail = 9,
12453 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12454 +};
12455 +#endif
12456 +
12457 +/* Write a chunk (page) of data to NAND.
12458 + *
12459 + * Caller always provides ExtendedTags data which are converted to a more
12460 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12461 + * contents of the tags meta-data; used to valid the tags when read.
12462 + *
12463 + * - Pack ExtendedTags to PackedTags1 form
12464 + * - Compute mini-ECC for PackedTags1
12465 + * - Write data and packed tags to NAND.
12466 + *
12467 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12468 + * a full sequence number (as found in the larger PackedTags2 form) it is
12469 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12470 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12471 + * to be written just once. When Yaffs performs this operation, this
12472 + * function is called with a NULL data pointer -- calling MTD write_oob
12473 + * without data is valid usage (2.6.17).
12474 + *
12475 + * Any underlying MTD error results in YAFFS_FAIL.
12476 + * Returns YAFFS_OK or YAFFS_FAIL.
12477 + */
12478 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12479 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12480 +{
12481 + struct mtd_info * mtd = dev->genericDevice;
12482 + int chunkBytes = dev->nDataBytesPerChunk;
12483 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12484 + struct mtd_oob_ops ops;
12485 + yaffs_PackedTags1 pt1;
12486 + int retval;
12487 +
12488 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12489 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12490 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12491 +
12492 + yaffs_PackTags1(&pt1, etags);
12493 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12494 +
12495 + /* When deleting a chunk, the upper layer provides only skeletal
12496 + * etags, one with chunkDeleted set. However, we need to update the
12497 + * tags, not erase them completely. So we use the NAND write property
12498 + * that only zeroed-bits stick and set tag bytes to all-ones and
12499 + * zero just the (not) deleted bit.
12500 + */
12501 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12502 + if (etags->chunkDeleted) {
12503 + memset(&pt1, 0xff, 8);
12504 + /* clear delete status bit to indicate deleted */
12505 + pt1.deleted = 0;
12506 + }
12507 +#else
12508 + ((__u8 *)&pt1)[8] = 0xff;
12509 + if (etags->chunkDeleted) {
12510 + memset(&pt1, 0xff, 8);
12511 + /* zero pageStatus byte to indicate deleted */
12512 + ((__u8 *)&pt1)[8] = 0;
12513 + }
12514 +#endif
12515 +
12516 + memset(&ops, 0, sizeof(ops));
12517 + ops.mode = MTD_OOB_AUTO;
12518 + ops.len = (data) ? chunkBytes : 0;
12519 + ops.ooblen = YTAG1_SIZE;
12520 + ops.datbuf = (__u8 *)data;
12521 + ops.oobbuf = (__u8 *)&pt1;
12522 +
12523 + retval = mtd->write_oob(mtd, addr, &ops);
12524 + if (retval) {
12525 + yaffs_trace(YAFFS_TRACE_MTD,
12526 + "write_oob failed, chunk %d, mtd error %d\n",
12527 + chunkInNAND, retval);
12528 + }
12529 + return retval ? YAFFS_FAIL : YAFFS_OK;
12530 +}
12531 +
12532 +/* Return with empty ExtendedTags but add eccResult.
12533 + */
12534 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12535 +{
12536 + if (etags) {
12537 + memset(etags, 0, sizeof(*etags));
12538 + etags->eccResult = eccResult;
12539 + }
12540 + return retval;
12541 +}
12542 +
12543 +/* Read a chunk (page) from NAND.
12544 + *
12545 + * Caller expects ExtendedTags data to be usable even on error; that is,
12546 + * all members except eccResult and blockBad are zeroed.
12547 + *
12548 + * - Check ECC results for data (if applicable)
12549 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12550 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12551 + * - Convert PackedTags1 to ExtendedTags
12552 + * - Update eccResult and blockBad members to refect state.
12553 + *
12554 + * Returns YAFFS_OK or YAFFS_FAIL.
12555 + */
12556 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12557 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12558 +{
12559 + struct mtd_info * mtd = dev->genericDevice;
12560 + int chunkBytes = dev->nDataBytesPerChunk;
12561 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12562 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12563 + struct mtd_oob_ops ops;
12564 + yaffs_PackedTags1 pt1;
12565 + int retval;
12566 + int deleted;
12567 +
12568 + memset(&ops, 0, sizeof(ops));
12569 + ops.mode = MTD_OOB_AUTO;
12570 + ops.len = (data) ? chunkBytes : 0;
12571 + ops.ooblen = YTAG1_SIZE;
12572 + ops.datbuf = data;
12573 + ops.oobbuf = (__u8 *)&pt1;
12574 +
12575 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12576 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12577 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12578 + */
12579 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12580 +#endif
12581 + /* Read page and oob using MTD.
12582 + * Check status and determine ECC result.
12583 + */
12584 + retval = mtd->read_oob(mtd, addr, &ops);
12585 + if (retval) {
12586 + yaffs_trace(YAFFS_TRACE_MTD,
12587 + "read_oob failed, chunk %d, mtd error %d\n",
12588 + chunkInNAND, retval);
12589 + }
12590 +
12591 + switch (retval) {
12592 + case 0:
12593 + /* no error */
12594 + break;
12595 +
12596 + case -EUCLEAN:
12597 + /* MTD's ECC fixed the data */
12598 + eccres = YAFFS_ECC_RESULT_FIXED;
12599 + dev->eccFixed++;
12600 + break;
12601 +
12602 + case -EBADMSG:
12603 + /* MTD's ECC could not fix the data */
12604 + dev->eccUnfixed++;
12605 + /* fall into... */
12606 + default:
12607 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12608 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12609 + return YAFFS_FAIL;
12610 + }
12611 +
12612 + /* Check for a blank/erased chunk.
12613 + */
12614 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12615 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12616 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12617 + }
12618 +
12619 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12620 + /* Read deleted status (bit) then return it to it's non-deleted
12621 + * state before performing tags mini-ECC check. pt1.deleted is
12622 + * inverted.
12623 + */
12624 + deleted = !pt1.deleted;
12625 + pt1.deleted = 1;
12626 +#else
12627 + (void) deleted; /* not used */
12628 +#endif
12629 +
12630 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12631 + */
12632 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12633 + switch (retval) {
12634 + case 0:
12635 + /* no tags error, use MTD result */
12636 + break;
12637 + case 1:
12638 + /* recovered tags-ECC error */
12639 + dev->tagsEccFixed++;
12640 + eccres = YAFFS_ECC_RESULT_FIXED;
12641 + break;
12642 + default:
12643 + /* unrecovered tags-ECC error */
12644 + dev->tagsEccUnfixed++;
12645 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12646 + }
12647 +
12648 + /* Unpack the tags to extended form and set ECC result.
12649 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12650 + */
12651 + pt1.shouldBeFF = 0xFFFFFFFF;
12652 + yaffs_UnpackTags1(etags, &pt1);
12653 + etags->eccResult = eccres;
12654 +
12655 + /* Set deleted state.
12656 + */
12657 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12658 + etags->chunkDeleted = deleted;
12659 +#else
12660 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12661 +#endif
12662 + return YAFFS_OK;
12663 +}
12664 +
12665 +/* Mark a block bad.
12666 + *
12667 + * This is a persistant state.
12668 + * Use of this function should be rare.
12669 + *
12670 + * Returns YAFFS_OK or YAFFS_FAIL.
12671 + */
12672 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12673 +{
12674 + struct mtd_info * mtd = dev->genericDevice;
12675 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12676 + int retval;
12677 +
12678 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
12679 +
12680 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12681 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12682 +}
12683 +
12684 +/* Check any MTD prerequists.
12685 + *
12686 + * Returns YAFFS_OK or YAFFS_FAIL.
12687 + */
12688 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
12689 +{
12690 + /* 2.6.18 has mtd->ecclayout->oobavail */
12691 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12692 + int oobavail = mtd->ecclayout->oobavail;
12693 +
12694 + if (oobavail < YTAG1_SIZE) {
12695 + yaffs_trace(YAFFS_TRACE_ERROR,
12696 + "mtd device has only %d bytes for tags, need %d",
12697 + oobavail, YTAG1_SIZE);
12698 + return YAFFS_FAIL;
12699 + }
12700 + return YAFFS_OK;
12701 +}
12702 +
12703 +/* Query for the current state of a specific block.
12704 + *
12705 + * Examine the tags of the first chunk of the block and return the state:
12706 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12707 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12708 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12709 + *
12710 + * Always returns YAFFS_OK.
12711 + */
12712 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12713 + yaffs_BlockState * pState, int *pSequenceNumber)
12714 +{
12715 + struct mtd_info * mtd = dev->genericDevice;
12716 + int chunkNo = blockNo * dev->nChunksPerBlock;
12717 + yaffs_ExtendedTags etags;
12718 + int state = YAFFS_BLOCK_STATE_DEAD;
12719 + int seqnum = 0;
12720 + int retval;
12721 +
12722 + /* We don't yet have a good place to test for MTD config prerequists.
12723 + * Do it here as we are called during the initial scan.
12724 + */
12725 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
12726 + return YAFFS_FAIL;
12727 + }
12728 +
12729 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12730 + if (etags.blockBad) {
12731 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12732 + "block %d is marked bad", blockNo);
12733 + state = YAFFS_BLOCK_STATE_DEAD;
12734 + }
12735 + else if (etags.chunkUsed) {
12736 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12737 + seqnum = etags.sequenceNumber;
12738 + }
12739 + else {
12740 + state = YAFFS_BLOCK_STATE_EMPTY;
12741 + }
12742 +
12743 + *pState = state;
12744 + *pSequenceNumber = seqnum;
12745 +
12746 + /* query always succeeds */
12747 + return YAFFS_OK;
12748 +}
12749 +
12750 +#endif /*KERNEL_VERSION*/
12751 +
12752 +--Boundary-00=_5LbTGmt62YoutxM--
12753 +
12754 +
12755 +
12756 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c
12757 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
12758 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c 2007-06-08 14:07:26.000000000 +0200
12759 @@ -0,0 +1,363 @@
12760 +/*
12761 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12762 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12763 + *
12764 + * Copyright (C) 2002 Aleph One Ltd.
12765 + * for Toby Churchill Ltd and Brightstar Engineering
12766 + *
12767 + * This program is free software; you can redistribute it and/or modify
12768 + * it under the terms of the GNU General Public License version 2 as
12769 + * published by the Free Software Foundation.
12770 + */
12771 +
12772 +/*
12773 + * This module provides the interface between yaffs_nand.c and the
12774 + * MTD API. This version is used when the MTD interface supports the
12775 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12776 + * and we have small-page NAND device.
12777 + *
12778 + * These functions are invoked via function pointers in yaffs_nand.c.
12779 + * This replaces functionality provided by functions in yaffs_mtdif.c
12780 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12781 + * called in yaffs_mtdif.c when the function pointers are NULL.
12782 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12783 + */
12784 +
12785 +#include "yportenv.h"
12786 +#include "yaffs_guts.h"
12787 +#include "yaffs_packedtags1.h"
12788 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12789 +
12790 +#include "linux/kernel.h"
12791 +#include "linux/version.h"
12792 +#include "linux/types.h"
12793 +#include "linux/mtd/mtd.h"
12794 +
12795 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12796 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12797 +
12798 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12799 +
12800 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12801 +# define YTAG1_SIZE 8
12802 +#else
12803 +# define YTAG1_SIZE 9
12804 +#endif
12805 +
12806 +#if 0
12807 +/* Use the following nand_ecclayout with MTD when using
12808 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12809 + * If you have existing Yaffs images and the byte order differs from this,
12810 + * adjust 'oobfree' to match your existing Yaffs data.
12811 + *
12812 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12813 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12814 + * the 9th byte.
12815 + *
12816 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12817 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12818 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12819 + * byte and B is the small-page bad-block indicator byte.
12820 + */
12821 +static struct nand_ecclayout nand_oob_16 = {
12822 + .eccbytes = 6,
12823 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12824 + .oobavail = 9,
12825 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12826 +};
12827 +#endif
12828 +
12829 +/* Write a chunk (page) of data to NAND.
12830 + *
12831 + * Caller always provides ExtendedTags data which are converted to a more
12832 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12833 + * contents of the tags meta-data; used to valid the tags when read.
12834 + *
12835 + * - Pack ExtendedTags to PackedTags1 form
12836 + * - Compute mini-ECC for PackedTags1
12837 + * - Write data and packed tags to NAND.
12838 + *
12839 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12840 + * a full sequence number (as found in the larger PackedTags2 form) it is
12841 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12842 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12843 + * to be written just once. When Yaffs performs this operation, this
12844 + * function is called with a NULL data pointer -- calling MTD write_oob
12845 + * without data is valid usage (2.6.17).
12846 + *
12847 + * Any underlying MTD error results in YAFFS_FAIL.
12848 + * Returns YAFFS_OK or YAFFS_FAIL.
12849 + */
12850 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12851 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12852 +{
12853 + struct mtd_info * mtd = dev->genericDevice;
12854 + int chunkBytes = dev->nDataBytesPerChunk;
12855 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12856 + struct mtd_oob_ops ops;
12857 + yaffs_PackedTags1 pt1;
12858 + int retval;
12859 +
12860 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12861 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12862 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12863 +
12864 + dev->nPageWrites++;
12865 +
12866 + yaffs_PackTags1(&pt1, etags);
12867 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12868 +
12869 + /* When deleting a chunk, the upper layer provides only skeletal
12870 + * etags, one with chunkDeleted set. However, we need to update the
12871 + * tags, not erase them completely. So we use the NAND write property
12872 + * that only zeroed-bits stick and set tag bytes to all-ones and
12873 + * zero just the (not) deleted bit.
12874 + */
12875 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12876 + if (etags->chunkDeleted) {
12877 + memset(&pt1, 0xff, 8);
12878 + /* clear delete status bit to indicate deleted */
12879 + pt1.deleted = 0;
12880 + }
12881 +#else
12882 + ((__u8 *)&pt1)[8] = 0xff;
12883 + if (etags->chunkDeleted) {
12884 + memset(&pt1, 0xff, 8);
12885 + /* zero pageStatus byte to indicate deleted */
12886 + ((__u8 *)&pt1)[8] = 0;
12887 + }
12888 +#endif
12889 +
12890 + memset(&ops, 0, sizeof(ops));
12891 + ops.mode = MTD_OOB_AUTO;
12892 + ops.len = (data) ? chunkBytes : 0;
12893 + ops.ooblen = YTAG1_SIZE;
12894 + ops.datbuf = (__u8 *)data;
12895 + ops.oobbuf = (__u8 *)&pt1;
12896 +
12897 + retval = mtd->write_oob(mtd, addr, &ops);
12898 + if (retval) {
12899 + yaffs_trace(YAFFS_TRACE_MTD,
12900 + "write_oob failed, chunk %d, mtd error %d\n",
12901 + chunkInNAND, retval);
12902 + }
12903 + return retval ? YAFFS_FAIL : YAFFS_OK;
12904 +}
12905 +
12906 +/* Return with empty ExtendedTags but add eccResult.
12907 + */
12908 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12909 +{
12910 + if (etags) {
12911 + memset(etags, 0, sizeof(*etags));
12912 + etags->eccResult = eccResult;
12913 + }
12914 + return retval;
12915 +}
12916 +
12917 +/* Read a chunk (page) from NAND.
12918 + *
12919 + * Caller expects ExtendedTags data to be usable even on error; that is,
12920 + * all members except eccResult and blockBad are zeroed.
12921 + *
12922 + * - Check ECC results for data (if applicable)
12923 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12924 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12925 + * - Convert PackedTags1 to ExtendedTags
12926 + * - Update eccResult and blockBad members to refect state.
12927 + *
12928 + * Returns YAFFS_OK or YAFFS_FAIL.
12929 + */
12930 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12931 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12932 +{
12933 + struct mtd_info * mtd = dev->genericDevice;
12934 + int chunkBytes = dev->nDataBytesPerChunk;
12935 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12936 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12937 + struct mtd_oob_ops ops;
12938 + yaffs_PackedTags1 pt1;
12939 + int retval;
12940 + int deleted;
12941 +
12942 + dev->nPageReads++;
12943 +
12944 + memset(&ops, 0, sizeof(ops));
12945 + ops.mode = MTD_OOB_AUTO;
12946 + ops.len = (data) ? chunkBytes : 0;
12947 + ops.ooblen = YTAG1_SIZE;
12948 + ops.datbuf = data;
12949 + ops.oobbuf = (__u8 *)&pt1;
12950 +
12951 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12952 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12953 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12954 + */
12955 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12956 +#endif
12957 + /* Read page and oob using MTD.
12958 + * Check status and determine ECC result.
12959 + */
12960 + retval = mtd->read_oob(mtd, addr, &ops);
12961 + if (retval) {
12962 + yaffs_trace(YAFFS_TRACE_MTD,
12963 + "read_oob failed, chunk %d, mtd error %d\n",
12964 + chunkInNAND, retval);
12965 + }
12966 +
12967 + switch (retval) {
12968 + case 0:
12969 + /* no error */
12970 + break;
12971 +
12972 + case -EUCLEAN:
12973 + /* MTD's ECC fixed the data */
12974 + eccres = YAFFS_ECC_RESULT_FIXED;
12975 + dev->eccFixed++;
12976 + break;
12977 +
12978 + case -EBADMSG:
12979 + /* MTD's ECC could not fix the data */
12980 + dev->eccUnfixed++;
12981 + /* fall into... */
12982 + default:
12983 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12984 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12985 + return YAFFS_FAIL;
12986 + }
12987 +
12988 + /* Check for a blank/erased chunk.
12989 + */
12990 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12991 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12992 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12993 + }
12994 +
12995 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12996 + /* Read deleted status (bit) then return it to it's non-deleted
12997 + * state before performing tags mini-ECC check. pt1.deleted is
12998 + * inverted.
12999 + */
13000 + deleted = !pt1.deleted;
13001 + pt1.deleted = 1;
13002 +#else
13003 + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
13004 +#endif
13005 +
13006 + /* Check the packed tags mini-ECC and correct if necessary/possible.
13007 + */
13008 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
13009 + switch (retval) {
13010 + case 0:
13011 + /* no tags error, use MTD result */
13012 + break;
13013 + case 1:
13014 + /* recovered tags-ECC error */
13015 + dev->tagsEccFixed++;
13016 + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
13017 + eccres = YAFFS_ECC_RESULT_FIXED;
13018 + break;
13019 + default:
13020 + /* unrecovered tags-ECC error */
13021 + dev->tagsEccUnfixed++;
13022 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
13023 + }
13024 +
13025 + /* Unpack the tags to extended form and set ECC result.
13026 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
13027 + */
13028 + pt1.shouldBeFF = 0xFFFFFFFF;
13029 + yaffs_UnpackTags1(etags, &pt1);
13030 + etags->eccResult = eccres;
13031 +
13032 + /* Set deleted state */
13033 + etags->chunkDeleted = deleted;
13034 + return YAFFS_OK;
13035 +}
13036 +
13037 +/* Mark a block bad.
13038 + *
13039 + * This is a persistant state.
13040 + * Use of this function should be rare.
13041 + *
13042 + * Returns YAFFS_OK or YAFFS_FAIL.
13043 + */
13044 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13045 +{
13046 + struct mtd_info * mtd = dev->genericDevice;
13047 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
13048 + int retval;
13049 +
13050 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
13051 +
13052 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
13053 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
13054 +}
13055 +
13056 +/* Check any MTD prerequists.
13057 + *
13058 + * Returns YAFFS_OK or YAFFS_FAIL.
13059 + */
13060 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
13061 +{
13062 + /* 2.6.18 has mtd->ecclayout->oobavail */
13063 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
13064 + int oobavail = mtd->ecclayout->oobavail;
13065 +
13066 + if (oobavail < YTAG1_SIZE) {
13067 + yaffs_trace(YAFFS_TRACE_ERROR,
13068 + "mtd device has only %d bytes for tags, need %d\n",
13069 + oobavail, YTAG1_SIZE);
13070 + return YAFFS_FAIL;
13071 + }
13072 + return YAFFS_OK;
13073 +}
13074 +
13075 +/* Query for the current state of a specific block.
13076 + *
13077 + * Examine the tags of the first chunk of the block and return the state:
13078 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
13079 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
13080 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
13081 + *
13082 + * Always returns YAFFS_OK.
13083 + */
13084 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13085 + yaffs_BlockState * pState, int *pSequenceNumber)
13086 +{
13087 + struct mtd_info * mtd = dev->genericDevice;
13088 + int chunkNo = blockNo * dev->nChunksPerBlock;
13089 + yaffs_ExtendedTags etags;
13090 + int state = YAFFS_BLOCK_STATE_DEAD;
13091 + int seqnum = 0;
13092 + int retval;
13093 +
13094 + /* We don't yet have a good place to test for MTD config prerequists.
13095 + * Do it here as we are called during the initial scan.
13096 + */
13097 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
13098 + return YAFFS_FAIL;
13099 + }
13100 +
13101 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
13102 + if (etags.blockBad) {
13103 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
13104 + "block %d is marked bad", blockNo);
13105 + state = YAFFS_BLOCK_STATE_DEAD;
13106 + }
13107 + else if (etags.chunkUsed) {
13108 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13109 + seqnum = etags.sequenceNumber;
13110 + }
13111 + else {
13112 + state = YAFFS_BLOCK_STATE_EMPTY;
13113 + }
13114 +
13115 + *pState = state;
13116 + *pSequenceNumber = seqnum;
13117 +
13118 + /* query always succeeds */
13119 + return YAFFS_OK;
13120 +}
13121 +
13122 +#endif /*KERNEL_VERSION*/
13123 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h
13124 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
13125 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h 2007-06-08 14:07:26.000000000 +0200
13126 @@ -0,0 +1,28 @@
13127 +/*
13128 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
13129 + *
13130 + * Copyright (C) 2002-2007 Aleph One Ltd.
13131 + * for Toby Churchill Ltd and Brightstar Engineering
13132 + *
13133 + * This program is free software; you can redistribute it and/or modify
13134 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13135 + * published by the Free Software Foundation.
13136 + *
13137 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13138 + */
13139 +
13140 +#ifndef __YAFFS_MTDIF1_H__
13141 +#define __YAFFS_MTDIF1_H__
13142 +
13143 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13144 + const __u8 * data, const yaffs_ExtendedTags * tags);
13145 +
13146 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13147 + __u8 * data, yaffs_ExtendedTags * tags);
13148 +
13149 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13150 +
13151 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13152 + yaffs_BlockState * state, int *sequenceNumber);
13153 +
13154 +#endif
13155 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c
13156 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
13157 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c 2007-06-08 14:07:26.000000000 +0200
13158 @@ -0,0 +1,232 @@
13159 +/*
13160 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13161 + *
13162 + * Copyright (C) 2002-2007 Aleph One Ltd.
13163 + * for Toby Churchill Ltd and Brightstar Engineering
13164 + *
13165 + * Created by Charles Manning <charles@aleph1.co.uk>
13166 + *
13167 + * This program is free software; you can redistribute it and/or modify
13168 + * it under the terms of the GNU General Public License version 2 as
13169 + * published by the Free Software Foundation.
13170 + */
13171 +
13172 +/* mtd interface for YAFFS2 */
13173 +
13174 +const char *yaffs_mtdif2_c_version =
13175 + "$Id: yaffs_mtdif2.c,v 1.17 2007-02-14 01:09:06 wookey Exp $";
13176 +
13177 +#include "yportenv.h"
13178 +
13179 +
13180 +#include "yaffs_mtdif2.h"
13181 +
13182 +#include "linux/mtd/mtd.h"
13183 +#include "linux/types.h"
13184 +#include "linux/time.h"
13185 +
13186 +#include "yaffs_packedtags2.h"
13187 +
13188 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13189 + const __u8 * data,
13190 + const yaffs_ExtendedTags * tags)
13191 +{
13192 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13193 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13194 + struct mtd_oob_ops ops;
13195 +#else
13196 + size_t dummy;
13197 +#endif
13198 + int retval = 0;
13199 +
13200 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13201 +
13202 + yaffs_PackedTags2 pt;
13203 +
13204 + T(YAFFS_TRACE_MTD,
13205 + (TSTR
13206 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
13207 + TENDSTR), chunkInNAND, data, tags));
13208 +
13209 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13210 + if (tags)
13211 + yaffs_PackTags2(&pt, tags);
13212 + else
13213 + BUG(); /* both tags and data should always be present */
13214 +
13215 + if (data) {
13216 + ops.mode = MTD_OOB_AUTO;
13217 + ops.ooblen = sizeof(pt);
13218 + ops.len = dev->nDataBytesPerChunk;
13219 + ops.ooboffs = 0;
13220 + ops.datbuf = (__u8 *)data;
13221 + ops.oobbuf = (void *)&pt;
13222 + retval = mtd->write_oob(mtd, addr, &ops);
13223 + } else
13224 + BUG(); /* both tags and data should always be present */
13225 +#else
13226 + if (tags) {
13227 + yaffs_PackTags2(&pt, tags);
13228 + }
13229 +
13230 + if (data && tags) {
13231 + if (dev->useNANDECC)
13232 + retval =
13233 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13234 + &dummy, data, (__u8 *) & pt, NULL);
13235 + else
13236 + retval =
13237 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13238 + &dummy, data, (__u8 *) & pt, NULL);
13239 + } else {
13240 + if (data)
13241 + retval =
13242 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13243 + data);
13244 + if (tags)
13245 + retval =
13246 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
13247 + (__u8 *) & pt);
13248 +
13249 + }
13250 +#endif
13251 +
13252 + if (retval == 0)
13253 + return YAFFS_OK;
13254 + else
13255 + return YAFFS_FAIL;
13256 +}
13257 +
13258 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13259 + __u8 * data, yaffs_ExtendedTags * tags)
13260 +{
13261 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13262 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13263 + struct mtd_oob_ops ops;
13264 +#endif
13265 + size_t dummy;
13266 + int retval = 0;
13267 +
13268 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13269 +
13270 + yaffs_PackedTags2 pt;
13271 +
13272 + T(YAFFS_TRACE_MTD,
13273 + (TSTR
13274 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
13275 + TENDSTR), chunkInNAND, data, tags));
13276 +
13277 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13278 + if (data && !tags)
13279 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13280 + &dummy, data);
13281 + else if (tags) {
13282 + ops.mode = MTD_OOB_AUTO;
13283 + ops.ooblen = sizeof(pt);
13284 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13285 + ops.ooboffs = 0;
13286 + ops.datbuf = data;
13287 + ops.oobbuf = dev->spareBuffer;
13288 + retval = mtd->read_oob(mtd, addr, &ops);
13289 + }
13290 +#else
13291 + if (data && tags) {
13292 + if (dev->useNANDECC) {
13293 + retval =
13294 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13295 + &dummy, data, dev->spareBuffer,
13296 + NULL);
13297 + } else {
13298 + retval =
13299 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13300 + &dummy, data, dev->spareBuffer,
13301 + NULL);
13302 + }
13303 + } else {
13304 + if (data)
13305 + retval =
13306 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13307 + data);
13308 + if (tags)
13309 + retval =
13310 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13311 + dev->spareBuffer);
13312 + }
13313 +#endif
13314 +
13315 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13316 +
13317 + if (tags)
13318 + yaffs_UnpackTags2(tags, &pt);
13319 +
13320 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13321 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13322 +
13323 + if (retval == 0)
13324 + return YAFFS_OK;
13325 + else
13326 + return YAFFS_FAIL;
13327 +}
13328 +
13329 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13330 +{
13331 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13332 + int retval;
13333 + T(YAFFS_TRACE_MTD,
13334 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13335 +
13336 + retval =
13337 + mtd->block_markbad(mtd,
13338 + blockNo * dev->nChunksPerBlock *
13339 + dev->nDataBytesPerChunk);
13340 +
13341 + if (retval == 0)
13342 + return YAFFS_OK;
13343 + else
13344 + return YAFFS_FAIL;
13345 +
13346 +}
13347 +
13348 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13349 + yaffs_BlockState * state, int *sequenceNumber)
13350 +{
13351 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13352 + int retval;
13353 +
13354 + T(YAFFS_TRACE_MTD,
13355 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13356 + retval =
13357 + mtd->block_isbad(mtd,
13358 + blockNo * dev->nChunksPerBlock *
13359 + dev->nDataBytesPerChunk);
13360 +
13361 + if (retval) {
13362 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13363 +
13364 + *state = YAFFS_BLOCK_STATE_DEAD;
13365 + *sequenceNumber = 0;
13366 + } else {
13367 + yaffs_ExtendedTags t;
13368 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13369 + blockNo *
13370 + dev->nChunksPerBlock, NULL,
13371 + &t);
13372 +
13373 + if (t.chunkUsed) {
13374 + *sequenceNumber = t.sequenceNumber;
13375 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13376 + } else {
13377 + *sequenceNumber = 0;
13378 + *state = YAFFS_BLOCK_STATE_EMPTY;
13379 + }
13380 + }
13381 + T(YAFFS_TRACE_MTD,
13382 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13383 + *state));
13384 +
13385 + if (retval == 0)
13386 + return YAFFS_OK;
13387 + else
13388 + return YAFFS_FAIL;
13389 +}
13390 +
13391 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h
13392 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
13393 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h 2007-06-08 14:07:26.000000000 +0200
13394 @@ -0,0 +1,29 @@
13395 +/*
13396 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13397 + *
13398 + * Copyright (C) 2002-2007 Aleph One Ltd.
13399 + * for Toby Churchill Ltd and Brightstar Engineering
13400 + *
13401 + * Created by Charles Manning <charles@aleph1.co.uk>
13402 + *
13403 + * This program is free software; you can redistribute it and/or modify
13404 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13405 + * published by the Free Software Foundation.
13406 + *
13407 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13408 + */
13409 +
13410 +#ifndef __YAFFS_MTDIF2_H__
13411 +#define __YAFFS_MTDIF2_H__
13412 +
13413 +#include "yaffs_guts.h"
13414 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13415 + const __u8 * data,
13416 + const yaffs_ExtendedTags * tags);
13417 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13418 + __u8 * data, yaffs_ExtendedTags * tags);
13419 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13420 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13421 + yaffs_BlockState * state, int *sequenceNumber);
13422 +
13423 +#endif
13424 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.c linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c
13425 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
13426 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c 2007-06-08 14:07:26.000000000 +0200
13427 @@ -0,0 +1,134 @@
13428 +/*
13429 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13430 + *
13431 + * Copyright (C) 2002-2007 Aleph One Ltd.
13432 + * for Toby Churchill Ltd and Brightstar Engineering
13433 + *
13434 + * Created by Charles Manning <charles@aleph1.co.uk>
13435 + *
13436 + * This program is free software; you can redistribute it and/or modify
13437 + * it under the terms of the GNU General Public License version 2 as
13438 + * published by the Free Software Foundation.
13439 + */
13440 +
13441 +const char *yaffs_nand_c_version =
13442 + "$Id: yaffs_nand.c,v 1.7 2007-02-14 01:09:06 wookey Exp $";
13443 +
13444 +#include "yaffs_nand.h"
13445 +#include "yaffs_tagscompat.h"
13446 +#include "yaffs_tagsvalidity.h"
13447 +
13448 +
13449 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13450 + __u8 * buffer,
13451 + yaffs_ExtendedTags * tags)
13452 +{
13453 + int result;
13454 + yaffs_ExtendedTags localTags;
13455 +
13456 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13457 +
13458 + /* If there are no tags provided, use local tags to get prioritised gc working */
13459 + if(!tags)
13460 + tags = &localTags;
13461 +
13462 + if (dev->readChunkWithTagsFromNAND)
13463 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13464 + tags);
13465 + else
13466 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13467 + realignedChunkInNAND,
13468 + buffer,
13469 + tags);
13470 + if(tags &&
13471 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
13472 +
13473 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13474 + yaffs_HandleChunkError(dev,bi);
13475 + }
13476 +
13477 + return result;
13478 +}
13479 +
13480 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13481 + int chunkInNAND,
13482 + const __u8 * buffer,
13483 + yaffs_ExtendedTags * tags)
13484 +{
13485 + chunkInNAND -= dev->chunkOffset;
13486 +
13487 +
13488 + if (tags) {
13489 + tags->sequenceNumber = dev->sequenceNumber;
13490 + tags->chunkUsed = 1;
13491 + if (!yaffs_ValidateTags(tags)) {
13492 + T(YAFFS_TRACE_ERROR,
13493 + (TSTR("Writing uninitialised tags" TENDSTR)));
13494 + YBUG();
13495 + }
13496 + T(YAFFS_TRACE_WRITE,
13497 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13498 + tags->objectId, tags->chunkId));
13499 + } else {
13500 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13501 + YBUG();
13502 + }
13503 +
13504 + if (dev->writeChunkWithTagsToNAND)
13505 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13506 + tags);
13507 + else
13508 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13509 + chunkInNAND,
13510 + buffer,
13511 + tags);
13512 +}
13513 +
13514 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
13515 +{
13516 + blockNo -= dev->blockOffset;
13517 +
13518 +;
13519 + if (dev->markNANDBlockBad)
13520 + return dev->markNANDBlockBad(dev, blockNo);
13521 + else
13522 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13523 +}
13524 +
13525 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13526 + int blockNo,
13527 + yaffs_BlockState * state,
13528 + unsigned *sequenceNumber)
13529 +{
13530 + blockNo -= dev->blockOffset;
13531 +
13532 + if (dev->queryNANDBlock)
13533 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13534 + else
13535 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13536 + state,
13537 + sequenceNumber);
13538 +}
13539 +
13540 +
13541 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13542 + int blockInNAND)
13543 +{
13544 + int result;
13545 +
13546 + blockInNAND -= dev->blockOffset;
13547 +
13548 +
13549 + dev->nBlockErasures++;
13550 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13551 +
13552 + return result;
13553 +}
13554 +
13555 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13556 +{
13557 + return dev->initialiseNAND(dev);
13558 +}
13559 +
13560 +
13561 +
13562 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h
13563 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
13564 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h 2007-06-08 14:07:26.000000000 +0200
13565 @@ -0,0 +1,44 @@
13566 +/*
13567 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13568 + *
13569 + * Copyright (C) 2002-2007 Aleph One Ltd.
13570 + * for Toby Churchill Ltd and Brightstar Engineering
13571 + *
13572 + * Created by Charles Manning <charles@aleph1.co.uk>
13573 + *
13574 + * This program is free software; you can redistribute it and/or modify
13575 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13576 + * published by the Free Software Foundation.
13577 + *
13578 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13579 + */
13580 +
13581 +#ifndef __YAFFS_NAND_H__
13582 +#define __YAFFS_NAND_H__
13583 +#include "yaffs_guts.h"
13584 +
13585 +
13586 +
13587 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13588 + __u8 * buffer,
13589 + yaffs_ExtendedTags * tags);
13590 +
13591 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13592 + int chunkInNAND,
13593 + const __u8 * buffer,
13594 + yaffs_ExtendedTags * tags);
13595 +
13596 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
13597 +
13598 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13599 + int blockNo,
13600 + yaffs_BlockState * state,
13601 + unsigned *sequenceNumber);
13602 +
13603 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13604 + int blockInNAND);
13605 +
13606 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13607 +
13608 +#endif
13609 +
13610 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h
13611 --- linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
13612 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h 2007-06-08 14:07:26.000000000 +0200
13613 @@ -0,0 +1,39 @@
13614 +/*
13615 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13616 + *
13617 + * Copyright (C) 2002-2007 Aleph One Ltd.
13618 + * for Toby Churchill Ltd and Brightstar Engineering
13619 + *
13620 + * Created by Charles Manning <charles@aleph1.co.uk>
13621 + *
13622 + * This program is free software; you can redistribute it and/or modify
13623 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13624 + * published by the Free Software Foundation.
13625 + *
13626 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13627 + */
13628 +
13629 +/* Interface to emulated NAND functions (2k page size) */
13630 +
13631 +#ifndef __YAFFS_NANDEMUL2K_H__
13632 +#define __YAFFS_NANDEMUL2K_H__
13633 +
13634 +#include "yaffs_guts.h"
13635 +
13636 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13637 + int chunkInNAND, const __u8 * data,
13638 + yaffs_ExtendedTags * tags);
13639 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13640 + int chunkInNAND, __u8 * data,
13641 + yaffs_ExtendedTags * tags);
13642 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13643 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13644 + yaffs_BlockState * state, int *sequenceNumber);
13645 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13646 + int blockInNAND);
13647 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13648 +int nandemul2k_GetBytesPerChunk(void);
13649 +int nandemul2k_GetChunksPerBlock(void);
13650 +int nandemul2k_GetNumberOfBlocks(void);
13651 +
13652 +#endif
13653 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c
13654 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
13655 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c 2007-06-08 14:07:26.000000000 +0200
13656 @@ -0,0 +1,52 @@
13657 +/*
13658 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13659 + *
13660 + * Copyright (C) 2002-2007 Aleph One Ltd.
13661 + * for Toby Churchill Ltd and Brightstar Engineering
13662 + *
13663 + * Created by Charles Manning <charles@aleph1.co.uk>
13664 + *
13665 + * This program is free software; you can redistribute it and/or modify
13666 + * it under the terms of the GNU General Public License version 2 as
13667 + * published by the Free Software Foundation.
13668 + */
13669 +
13670 +#include "yaffs_packedtags1.h"
13671 +#include "yportenv.h"
13672 +
13673 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
13674 +{
13675 + pt->chunkId = t->chunkId;
13676 + pt->serialNumber = t->serialNumber;
13677 + pt->byteCount = t->byteCount;
13678 + pt->objectId = t->objectId;
13679 + pt->ecc = 0;
13680 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13681 + pt->unusedStuff = 0;
13682 + pt->shouldBeFF = 0xFFFFFFFF;
13683 +
13684 +}
13685 +
13686 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
13687 +{
13688 + static const __u8 allFF[] =
13689 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13690 +0xff };
13691 +
13692 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13693 + t->blockBad = 0;
13694 + if (pt->shouldBeFF != 0xFFFFFFFF) {
13695 + t->blockBad = 1;
13696 + }
13697 + t->chunkUsed = 1;
13698 + t->objectId = pt->objectId;
13699 + t->chunkId = pt->chunkId;
13700 + t->byteCount = pt->byteCount;
13701 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13702 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13703 + t->serialNumber = pt->serialNumber;
13704 + } else {
13705 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13706 +
13707 + }
13708 +}
13709 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h
13710 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
13711 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h 2007-06-08 14:07:27.000000000 +0200
13712 @@ -0,0 +1,37 @@
13713 +/*
13714 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13715 + *
13716 + * Copyright (C) 2002-2007 Aleph One Ltd.
13717 + * for Toby Churchill Ltd and Brightstar Engineering
13718 + *
13719 + * Created by Charles Manning <charles@aleph1.co.uk>
13720 + *
13721 + * This program is free software; you can redistribute it and/or modify
13722 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13723 + * published by the Free Software Foundation.
13724 + *
13725 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13726 + */
13727 +
13728 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13729 +
13730 +#ifndef __YAFFS_PACKEDTAGS1_H__
13731 +#define __YAFFS_PACKEDTAGS1_H__
13732 +
13733 +#include "yaffs_guts.h"
13734 +
13735 +typedef struct {
13736 + unsigned chunkId:20;
13737 + unsigned serialNumber:2;
13738 + unsigned byteCount:10;
13739 + unsigned objectId:18;
13740 + unsigned ecc:12;
13741 + unsigned deleted:1;
13742 + unsigned unusedStuff:1;
13743 + unsigned shouldBeFF;
13744 +
13745 +} yaffs_PackedTags1;
13746 +
13747 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
13748 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
13749 +#endif
13750 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c
13751 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
13752 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c 2007-06-08 14:07:27.000000000 +0200
13753 @@ -0,0 +1,182 @@
13754 +/*
13755 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13756 + *
13757 + * Copyright (C) 2002-2007 Aleph One Ltd.
13758 + * for Toby Churchill Ltd and Brightstar Engineering
13759 + *
13760 + * Created by Charles Manning <charles@aleph1.co.uk>
13761 + *
13762 + * This program is free software; you can redistribute it and/or modify
13763 + * it under the terms of the GNU General Public License version 2 as
13764 + * published by the Free Software Foundation.
13765 + */
13766 +
13767 +#include "yaffs_packedtags2.h"
13768 +#include "yportenv.h"
13769 +#include "yaffs_tagsvalidity.h"
13770 +
13771 +/* This code packs a set of extended tags into a binary structure for
13772 + * NAND storage
13773 + */
13774 +
13775 +/* Some of the information is "extra" struff which can be packed in to
13776 + * speed scanning
13777 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13778 + */
13779 +
13780 +/* Extra flags applied to chunkId */
13781 +
13782 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13783 +#define EXTRA_SHRINK_FLAG 0x40000000
13784 +#define EXTRA_SHADOWS_FLAG 0x20000000
13785 +#define EXTRA_SPARE_FLAGS 0x10000000
13786 +
13787 +#define ALL_EXTRA_FLAGS 0xF0000000
13788 +
13789 +/* Also, the top 4 bits of the object Id are set to the object type. */
13790 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13791 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13792 +
13793 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
13794 +{
13795 + T(YAFFS_TRACE_MTD,
13796 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13797 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
13798 + pt->t.sequenceNumber));
13799 +}
13800 +
13801 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
13802 +{
13803 + T(YAFFS_TRACE_MTD,
13804 + (TSTR
13805 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
13806 + "%d del %d ser %d seq %d"
13807 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13808 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13809 + t->sequenceNumber));
13810 +
13811 +}
13812 +
13813 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
13814 +{
13815 + pt->t.chunkId = t->chunkId;
13816 + pt->t.sequenceNumber = t->sequenceNumber;
13817 + pt->t.byteCount = t->byteCount;
13818 + pt->t.objectId = t->objectId;
13819 +
13820 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13821 + /* Store the extra header info instead */
13822 + /* We save the parent object in the chunkId */
13823 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
13824 + | t->extraParentObjectId;
13825 + if (t->extraIsShrinkHeader) {
13826 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
13827 + }
13828 + if (t->extraShadows) {
13829 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
13830 + }
13831 +
13832 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13833 + pt->t.objectId |=
13834 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13835 +
13836 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13837 + pt->t.byteCount = t->extraEquivalentObjectId;
13838 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
13839 + pt->t.byteCount = t->extraFileLength;
13840 + } else {
13841 + pt->t.byteCount = 0;
13842 + }
13843 + }
13844 +
13845 + yaffs_DumpPackedTags2(pt);
13846 + yaffs_DumpTags2(t);
13847 +
13848 +#ifndef YAFFS_IGNORE_TAGS_ECC
13849 + {
13850 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13851 + sizeof(yaffs_PackedTags2TagsPart),
13852 + &pt->ecc);
13853 + }
13854 +#endif
13855 +}
13856 +
13857 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
13858 +{
13859 +
13860 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13861 +
13862 + yaffs_InitialiseTags(t);
13863 +
13864 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13865 + /* Page is in use */
13866 +#ifdef YAFFS_IGNORE_TAGS_ECC
13867 + {
13868 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13869 + }
13870 +#else
13871 + {
13872 + yaffs_ECCOther ecc;
13873 + int result;
13874 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13875 + sizeof
13876 + (yaffs_PackedTags2TagsPart),
13877 + &ecc);
13878 + result =
13879 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13880 + sizeof
13881 + (yaffs_PackedTags2TagsPart),
13882 + &pt->ecc, &ecc);
13883 + switch(result){
13884 + case 0:
13885 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13886 + break;
13887 + case 1:
13888 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
13889 + break;
13890 + case -1:
13891 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13892 + break;
13893 + default:
13894 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13895 + }
13896 + }
13897 +#endif
13898 + t->blockBad = 0;
13899 + t->chunkUsed = 1;
13900 + t->objectId = pt->t.objectId;
13901 + t->chunkId = pt->t.chunkId;
13902 + t->byteCount = pt->t.byteCount;
13903 + t->chunkDeleted = 0;
13904 + t->serialNumber = 0;
13905 + t->sequenceNumber = pt->t.sequenceNumber;
13906 +
13907 + /* Do extra header info stuff */
13908 +
13909 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
13910 + t->chunkId = 0;
13911 + t->byteCount = 0;
13912 +
13913 + t->extraHeaderInfoAvailable = 1;
13914 + t->extraParentObjectId =
13915 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
13916 + t->extraIsShrinkHeader =
13917 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13918 + t->extraShadows =
13919 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13920 + t->extraObjectType =
13921 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13922 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13923 +
13924 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13925 + t->extraEquivalentObjectId = pt->t.byteCount;
13926 + } else {
13927 + t->extraFileLength = pt->t.byteCount;
13928 + }
13929 + }
13930 + }
13931 +
13932 + yaffs_DumpPackedTags2(pt);
13933 + yaffs_DumpTags2(t);
13934 +
13935 +}
13936 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h
13937 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
13938 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h 2007-06-08 14:07:27.000000000 +0200
13939 @@ -0,0 +1,38 @@
13940 +/*
13941 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13942 + *
13943 + * Copyright (C) 2002-2007 Aleph One Ltd.
13944 + * for Toby Churchill Ltd and Brightstar Engineering
13945 + *
13946 + * Created by Charles Manning <charles@aleph1.co.uk>
13947 + *
13948 + * This program is free software; you can redistribute it and/or modify
13949 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13950 + * published by the Free Software Foundation.
13951 + *
13952 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13953 + */
13954 +
13955 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13956 +
13957 +#ifndef __YAFFS_PACKEDTAGS2_H__
13958 +#define __YAFFS_PACKEDTAGS2_H__
13959 +
13960 +#include "yaffs_guts.h"
13961 +#include "yaffs_ecc.h"
13962 +
13963 +typedef struct {
13964 + unsigned sequenceNumber;
13965 + unsigned objectId;
13966 + unsigned chunkId;
13967 + unsigned byteCount;
13968 +} yaffs_PackedTags2TagsPart;
13969 +
13970 +typedef struct {
13971 + yaffs_PackedTags2TagsPart t;
13972 + yaffs_ECCOther ecc;
13973 +} yaffs_PackedTags2;
13974 +
13975 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
13976 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
13977 +#endif
13978 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c
13979 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
13980 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c 2007-06-08 14:07:27.000000000 +0200
13981 @@ -0,0 +1,160 @@
13982 +/*
13983 + * Copyright (c) 1992, 1993
13984 + * The Regents of the University of California. All rights reserved.
13985 + *
13986 + * Redistribution and use in source and binary forms, with or without
13987 + * modification, are permitted provided that the following conditions
13988 + * are met:
13989 + * 1. Redistributions of source code must retain the above copyright
13990 + * notice, this list of conditions and the following disclaimer.
13991 + * 2. Redistributions in binary form must reproduce the above copyright
13992 + * notice, this list of conditions and the following disclaimer in the
13993 + * documentation and/or other materials provided with the distribution.
13994 + * 3. Neither the name of the University nor the names of its contributors
13995 + * may be used to endorse or promote products derived from this software
13996 + * without specific prior written permission.
13997 + *
13998 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
13999 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14000 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14001 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14002 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14003 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14004 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14005 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14006 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14007 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14008 + * SUCH DAMAGE.
14009 + */
14010 +
14011 +#include "yportenv.h"
14012 +//#include <linux/string.h>
14013 +
14014 +/*
14015 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
14016 + */
14017 +#define swapcode(TYPE, parmi, parmj, n) { \
14018 + long i = (n) / sizeof (TYPE); \
14019 + register TYPE *pi = (TYPE *) (parmi); \
14020 + register TYPE *pj = (TYPE *) (parmj); \
14021 + do { \
14022 + register TYPE t = *pi; \
14023 + *pi++ = *pj; \
14024 + *pj++ = t; \
14025 + } while (--i > 0); \
14026 +}
14027 +
14028 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
14029 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
14030 +
14031 +static __inline void
14032 +swapfunc(char *a, char *b, int n, int swaptype)
14033 +{
14034 + if (swaptype <= 1)
14035 + swapcode(long, a, b, n)
14036 + else
14037 + swapcode(char, a, b, n)
14038 +}
14039 +
14040 +#define swap(a, b) \
14041 + if (swaptype == 0) { \
14042 + long t = *(long *)(a); \
14043 + *(long *)(a) = *(long *)(b); \
14044 + *(long *)(b) = t; \
14045 + } else \
14046 + swapfunc(a, b, es, swaptype)
14047 +
14048 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14049 +
14050 +static __inline char *
14051 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14052 +{
14053 + return cmp(a, b) < 0 ?
14054 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
14055 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
14056 +}
14057 +
14058 +#ifndef min
14059 +#define min(a,b) (((a) < (b)) ? (a) : (b))
14060 +#endif
14061 +
14062 +void
14063 +yaffs_qsort(void *aa, size_t n, size_t es,
14064 + int (*cmp)(const void *, const void *))
14065 +{
14066 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14067 + int d, r, swaptype, swap_cnt;
14068 + register char *a = aa;
14069 +
14070 +loop: SWAPINIT(a, es);
14071 + swap_cnt = 0;
14072 + if (n < 7) {
14073 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14074 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14075 + pl -= es)
14076 + swap(pl, pl - es);
14077 + return;
14078 + }
14079 + pm = (char *)a + (n / 2) * es;
14080 + if (n > 7) {
14081 + pl = (char *)a;
14082 + pn = (char *)a + (n - 1) * es;
14083 + if (n > 40) {
14084 + d = (n / 8) * es;
14085 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14086 + pm = med3(pm - d, pm, pm + d, cmp);
14087 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14088 + }
14089 + pm = med3(pl, pm, pn, cmp);
14090 + }
14091 + swap(a, pm);
14092 + pa = pb = (char *)a + es;
14093 +
14094 + pc = pd = (char *)a + (n - 1) * es;
14095 + for (;;) {
14096 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14097 + if (r == 0) {
14098 + swap_cnt = 1;
14099 + swap(pa, pb);
14100 + pa += es;
14101 + }
14102 + pb += es;
14103 + }
14104 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14105 + if (r == 0) {
14106 + swap_cnt = 1;
14107 + swap(pc, pd);
14108 + pd -= es;
14109 + }
14110 + pc -= es;
14111 + }
14112 + if (pb > pc)
14113 + break;
14114 + swap(pb, pc);
14115 + swap_cnt = 1;
14116 + pb += es;
14117 + pc -= es;
14118 + }
14119 + if (swap_cnt == 0) { /* Switch to insertion sort */
14120 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14121 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14122 + pl -= es)
14123 + swap(pl, pl - es);
14124 + return;
14125 + }
14126 +
14127 + pn = (char *)a + n * es;
14128 + r = min(pa - (char *)a, pb - pa);
14129 + vecswap(a, pb - r, r);
14130 + r = min((long)(pd - pc), (long)(pn - pd - es));
14131 + vecswap(pb, pn - r, r);
14132 + if ((r = pb - pa) > es)
14133 + yaffs_qsort(a, r / es, es, cmp);
14134 + if ((r = pd - pc) > es) {
14135 + /* Iterate rather than recurse to save stack space */
14136 + a = pn - r;
14137 + n = r / es;
14138 + goto loop;
14139 + }
14140 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14141 +}
14142 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h
14143 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
14144 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h 2007-06-08 14:07:27.000000000 +0200
14145 @@ -0,0 +1,23 @@
14146 +/*
14147 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14148 + *
14149 + * Copyright (C) 2002-2007 Aleph One Ltd.
14150 + * for Toby Churchill Ltd and Brightstar Engineering
14151 + *
14152 + * Created by Charles Manning <charles@aleph1.co.uk>
14153 + *
14154 + * This program is free software; you can redistribute it and/or modify
14155 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14156 + * published by the Free Software Foundation.
14157 + *
14158 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14159 + */
14160 +
14161 +
14162 +#ifndef __YAFFS_QSORT_H__
14163 +#define __YAFFS_QSORT_H__
14164 +
14165 +extern void yaffs_qsort (void *const base, size_t total_elems, size_t size,
14166 + int (*cmp)(const void *, const void *));
14167 +
14168 +#endif
14169 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c
14170 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
14171 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c 2007-06-08 14:07:27.000000000 +0200
14172 @@ -0,0 +1,530 @@
14173 +/*
14174 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14175 + *
14176 + * Copyright (C) 2002-2007 Aleph One Ltd.
14177 + * for Toby Churchill Ltd and Brightstar Engineering
14178 + *
14179 + * Created by Charles Manning <charles@aleph1.co.uk>
14180 + *
14181 + * This program is free software; you can redistribute it and/or modify
14182 + * it under the terms of the GNU General Public License version 2 as
14183 + * published by the Free Software Foundation.
14184 + */
14185 +
14186 +#include "yaffs_guts.h"
14187 +#include "yaffs_tagscompat.h"
14188 +#include "yaffs_ecc.h"
14189 +
14190 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
14191 +#ifdef NOTYET
14192 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
14193 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14194 + const __u8 * data,
14195 + const yaffs_Spare * spare);
14196 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14197 + const yaffs_Spare * spare);
14198 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
14199 +#endif
14200 +
14201 +static const char yaffs_countBitsTable[256] = {
14202 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14203 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14204 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14205 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14206 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14207 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14208 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14209 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14210 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14211 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14212 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14213 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14214 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14215 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14216 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14217 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14218 +};
14219 +
14220 +int yaffs_CountBits(__u8 x)
14221 +{
14222 + int retVal;
14223 + retVal = yaffs_countBitsTable[x];
14224 + return retVal;
14225 +}
14226 +
14227 +/********** Tags ECC calculations *********/
14228 +
14229 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
14230 +{
14231 + yaffs_ECCCalculate(data, spare->ecc1);
14232 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14233 +}
14234 +
14235 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
14236 +{
14237 + /* Calculate an ecc */
14238 +
14239 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14240 + unsigned i, j;
14241 + unsigned ecc = 0;
14242 + unsigned bit = 0;
14243 +
14244 + tags->ecc = 0;
14245 +
14246 + for (i = 0; i < 8; i++) {
14247 + for (j = 1; j & 0xff; j <<= 1) {
14248 + bit++;
14249 + if (b[i] & j) {
14250 + ecc ^= bit;
14251 + }
14252 + }
14253 + }
14254 +
14255 + tags->ecc = ecc;
14256 +
14257 +}
14258 +
14259 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
14260 +{
14261 + unsigned ecc = tags->ecc;
14262 +
14263 + yaffs_CalcTagsECC(tags);
14264 +
14265 + ecc ^= tags->ecc;
14266 +
14267 + if (ecc && ecc <= 64) {
14268 + /* TODO: Handle the failure better. Retire? */
14269 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14270 +
14271 + ecc--;
14272 +
14273 + b[ecc / 8] ^= (1 << (ecc & 7));
14274 +
14275 + /* Now recvalc the ecc */
14276 + yaffs_CalcTagsECC(tags);
14277 +
14278 + return 1; /* recovered error */
14279 + } else if (ecc) {
14280 + /* Wierd ecc failure value */
14281 + /* TODO Need to do somethiong here */
14282 + return -1; /* unrecovered error */
14283 + }
14284 +
14285 + return 0;
14286 +}
14287 +
14288 +/********** Tags **********/
14289 +
14290 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
14291 + yaffs_Tags * tagsPtr)
14292 +{
14293 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14294 +
14295 + yaffs_CalcTagsECC(tagsPtr);
14296 +
14297 + sparePtr->tagByte0 = tu->asBytes[0];
14298 + sparePtr->tagByte1 = tu->asBytes[1];
14299 + sparePtr->tagByte2 = tu->asBytes[2];
14300 + sparePtr->tagByte3 = tu->asBytes[3];
14301 + sparePtr->tagByte4 = tu->asBytes[4];
14302 + sparePtr->tagByte5 = tu->asBytes[5];
14303 + sparePtr->tagByte6 = tu->asBytes[6];
14304 + sparePtr->tagByte7 = tu->asBytes[7];
14305 +}
14306 +
14307 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
14308 + yaffs_Tags * tagsPtr)
14309 +{
14310 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14311 + int result;
14312 +
14313 + tu->asBytes[0] = sparePtr->tagByte0;
14314 + tu->asBytes[1] = sparePtr->tagByte1;
14315 + tu->asBytes[2] = sparePtr->tagByte2;
14316 + tu->asBytes[3] = sparePtr->tagByte3;
14317 + tu->asBytes[4] = sparePtr->tagByte4;
14318 + tu->asBytes[5] = sparePtr->tagByte5;
14319 + tu->asBytes[6] = sparePtr->tagByte6;
14320 + tu->asBytes[7] = sparePtr->tagByte7;
14321 +
14322 + result = yaffs_CheckECCOnTags(tagsPtr);
14323 + if (result > 0) {
14324 + dev->tagsEccFixed++;
14325 + } else if (result < 0) {
14326 + dev->tagsEccUnfixed++;
14327 + }
14328 +}
14329 +
14330 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
14331 +{
14332 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14333 +}
14334 +
14335 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14336 + int chunkInNAND, const __u8 * data,
14337 + yaffs_Spare * spare)
14338 +{
14339 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14340 + T(YAFFS_TRACE_ERROR,
14341 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14342 + chunkInNAND));
14343 + return YAFFS_FAIL;
14344 + }
14345 +
14346 + dev->nPageWrites++;
14347 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14348 +}
14349 +
14350 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14351 + int chunkInNAND,
14352 + __u8 * data,
14353 + yaffs_Spare * spare,
14354 + yaffs_ECCResult * eccResult,
14355 + int doErrorCorrection)
14356 +{
14357 + int retVal;
14358 + yaffs_Spare localSpare;
14359 +
14360 + dev->nPageReads++;
14361 +
14362 + if (!spare && data) {
14363 + /* If we don't have a real spare, then we use a local one. */
14364 + /* Need this for the calculation of the ecc */
14365 + spare = &localSpare;
14366 + }
14367 +
14368 + if (!dev->useNANDECC) {
14369 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14370 + if (data && doErrorCorrection) {
14371 + /* Do ECC correction */
14372 + /* Todo handle any errors */
14373 + int eccResult1, eccResult2;
14374 + __u8 calcEcc[3];
14375 +
14376 + yaffs_ECCCalculate(data, calcEcc);
14377 + eccResult1 =
14378 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14379 + yaffs_ECCCalculate(&data[256], calcEcc);
14380 + eccResult2 =
14381 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14382 +
14383 + if (eccResult1 > 0) {
14384 + T(YAFFS_TRACE_ERROR,
14385 + (TSTR
14386 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14387 + TENDSTR), chunkInNAND));
14388 + dev->eccFixed++;
14389 + } else if (eccResult1 < 0) {
14390 + T(YAFFS_TRACE_ERROR,
14391 + (TSTR
14392 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14393 + TENDSTR), chunkInNAND));
14394 + dev->eccUnfixed++;
14395 + }
14396 +
14397 + if (eccResult2 > 0) {
14398 + T(YAFFS_TRACE_ERROR,
14399 + (TSTR
14400 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14401 + TENDSTR), chunkInNAND));
14402 + dev->eccFixed++;
14403 + } else if (eccResult2 < 0) {
14404 + T(YAFFS_TRACE_ERROR,
14405 + (TSTR
14406 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14407 + TENDSTR), chunkInNAND));
14408 + dev->eccUnfixed++;
14409 + }
14410 +
14411 + if (eccResult1 || eccResult2) {
14412 + /* We had a data problem on this page */
14413 + yaffs_HandleReadDataError(dev, chunkInNAND);
14414 + }
14415 +
14416 + if (eccResult1 < 0 || eccResult2 < 0)
14417 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14418 + else if (eccResult1 > 0 || eccResult2 > 0)
14419 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14420 + else
14421 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14422 + }
14423 + } else {
14424 + /* Must allocate enough memory for spare+2*sizeof(int) */
14425 + /* for ecc results from device. */
14426 + struct yaffs_NANDSpare nspare;
14427 + retVal =
14428 + dev->readChunkFromNAND(dev, chunkInNAND, data,
14429 + (yaffs_Spare *) & nspare);
14430 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14431 + if (data && doErrorCorrection) {
14432 + if (nspare.eccres1 > 0) {
14433 + T(YAFFS_TRACE_ERROR,
14434 + (TSTR
14435 + ("**>>mtd ecc error fix performed on chunk %d:0"
14436 + TENDSTR), chunkInNAND));
14437 + } else if (nspare.eccres1 < 0) {
14438 + T(YAFFS_TRACE_ERROR,
14439 + (TSTR
14440 + ("**>>mtd ecc error unfixed on chunk %d:0"
14441 + TENDSTR), chunkInNAND));
14442 + }
14443 +
14444 + if (nspare.eccres2 > 0) {
14445 + T(YAFFS_TRACE_ERROR,
14446 + (TSTR
14447 + ("**>>mtd ecc error fix performed on chunk %d:1"
14448 + TENDSTR), chunkInNAND));
14449 + } else if (nspare.eccres2 < 0) {
14450 + T(YAFFS_TRACE_ERROR,
14451 + (TSTR
14452 + ("**>>mtd ecc error unfixed on chunk %d:1"
14453 + TENDSTR), chunkInNAND));
14454 + }
14455 +
14456 + if (nspare.eccres1 || nspare.eccres2) {
14457 + /* We had a data problem on this page */
14458 + yaffs_HandleReadDataError(dev, chunkInNAND);
14459 + }
14460 +
14461 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14462 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14463 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14464 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14465 + else
14466 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14467 +
14468 + }
14469 + }
14470 + return retVal;
14471 +}
14472 +
14473 +#ifdef NOTYET
14474 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14475 + int chunkInNAND)
14476 +{
14477 +
14478 + static int init = 0;
14479 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14480 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14481 + /* Might as well always allocate the larger size for */
14482 + /* dev->useNANDECC == true; */
14483 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14484 +
14485 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14486 +
14487 + if (!init) {
14488 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14489 + init = 1;
14490 + }
14491 +
14492 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14493 + return YAFFS_FAIL;
14494 + if (memcmp(cmpbuf, spare, 16))
14495 + return YAFFS_FAIL;
14496 +
14497 + return YAFFS_OK;
14498 +
14499 +}
14500 +#endif
14501 +
14502 +/*
14503 + * Functions for robustisizing
14504 + */
14505 +
14506 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
14507 +{
14508 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14509 +
14510 + /* Mark the block for retirement */
14511 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14512 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14513 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14514 +
14515 + /* TODO:
14516 + * Just do a garbage collection on the affected block
14517 + * then retire the block
14518 + * NB recursion
14519 + */
14520 +}
14521 +
14522 +#ifdef NOTYET
14523 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
14524 +{
14525 +}
14526 +
14527 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14528 + const __u8 * data,
14529 + const yaffs_Spare * spare)
14530 +{
14531 +}
14532 +
14533 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14534 + const yaffs_Spare * spare)
14535 +{
14536 +}
14537 +
14538 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
14539 +{
14540 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14541 +
14542 + /* Mark the block for retirement */
14543 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14544 + /* Delete the chunk */
14545 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14546 +}
14547 +
14548 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
14549 + const yaffs_Spare * s0, const yaffs_Spare * s1)
14550 +{
14551 +
14552 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14553 + s0->tagByte0 != s1->tagByte0 ||
14554 + s0->tagByte1 != s1->tagByte1 ||
14555 + s0->tagByte2 != s1->tagByte2 ||
14556 + s0->tagByte3 != s1->tagByte3 ||
14557 + s0->tagByte4 != s1->tagByte4 ||
14558 + s0->tagByte5 != s1->tagByte5 ||
14559 + s0->tagByte6 != s1->tagByte6 ||
14560 + s0->tagByte7 != s1->tagByte7 ||
14561 + s0->ecc1[0] != s1->ecc1[0] ||
14562 + s0->ecc1[1] != s1->ecc1[1] ||
14563 + s0->ecc1[2] != s1->ecc1[2] ||
14564 + s0->ecc2[0] != s1->ecc2[0] ||
14565 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14566 + return 0;
14567 + }
14568 +
14569 + return 1;
14570 +}
14571 +#endif /* NOTYET */
14572 +
14573 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14574 + int chunkInNAND,
14575 + const __u8 * data,
14576 + const yaffs_ExtendedTags *
14577 + eTags)
14578 +{
14579 + yaffs_Spare spare;
14580 + yaffs_Tags tags;
14581 +
14582 + yaffs_SpareInitialise(&spare);
14583 +
14584 + if (eTags->chunkDeleted) {
14585 + spare.pageStatus = 0;
14586 + } else {
14587 + tags.objectId = eTags->objectId;
14588 + tags.chunkId = eTags->chunkId;
14589 + tags.byteCount = eTags->byteCount;
14590 + tags.serialNumber = eTags->serialNumber;
14591 +
14592 + if (!dev->useNANDECC && data) {
14593 + yaffs_CalcECC(data, &spare);
14594 + }
14595 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14596 +
14597 + }
14598 +
14599 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14600 +}
14601 +
14602 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14603 + int chunkInNAND,
14604 + __u8 * data,
14605 + yaffs_ExtendedTags * eTags)
14606 +{
14607 +
14608 + yaffs_Spare spare;
14609 + yaffs_Tags tags;
14610 + yaffs_ECCResult eccResult;
14611 +
14612 + static yaffs_Spare spareFF;
14613 + static int init;
14614 +
14615 + if (!init) {
14616 + memset(&spareFF, 0xFF, sizeof(spareFF));
14617 + init = 1;
14618 + }
14619 +
14620 + if (yaffs_ReadChunkFromNAND
14621 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14622 + /* eTags may be NULL */
14623 + if (eTags) {
14624 +
14625 + int deleted =
14626 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14627 +
14628 + eTags->chunkDeleted = deleted;
14629 + eTags->eccResult = eccResult;
14630 + eTags->blockBad = 0; /* We're reading it */
14631 + /* therefore it is not a bad block */
14632 + eTags->chunkUsed =
14633 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14634 + 0) ? 1 : 0;
14635 +
14636 + if (eTags->chunkUsed) {
14637 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14638 +
14639 + eTags->objectId = tags.objectId;
14640 + eTags->chunkId = tags.chunkId;
14641 + eTags->byteCount = tags.byteCount;
14642 + eTags->serialNumber = tags.serialNumber;
14643 + }
14644 + }
14645 +
14646 + return YAFFS_OK;
14647 + } else {
14648 + return YAFFS_FAIL;
14649 + }
14650 +}
14651 +
14652 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14653 + int blockInNAND)
14654 +{
14655 +
14656 + yaffs_Spare spare;
14657 +
14658 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14659 +
14660 + spare.blockStatus = 'Y';
14661 +
14662 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14663 + &spare);
14664 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14665 + NULL, &spare);
14666 +
14667 + return YAFFS_OK;
14668 +
14669 +}
14670 +
14671 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14672 + int blockNo, yaffs_BlockState *
14673 + state,
14674 + int *sequenceNumber)
14675 +{
14676 +
14677 + yaffs_Spare spare0, spare1;
14678 + static yaffs_Spare spareFF;
14679 + static int init;
14680 + yaffs_ECCResult dummy;
14681 +
14682 + if (!init) {
14683 + memset(&spareFF, 0xFF, sizeof(spareFF));
14684 + init = 1;
14685 + }
14686 +
14687 + *sequenceNumber = 0;
14688 +
14689 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14690 + &spare0, &dummy, 1);
14691 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14692 + &spare1, &dummy, 1);
14693 +
14694 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14695 + *state = YAFFS_BLOCK_STATE_DEAD;
14696 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14697 + *state = YAFFS_BLOCK_STATE_EMPTY;
14698 + else
14699 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14700 +
14701 + return YAFFS_OK;
14702 +}
14703 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h
14704 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
14705 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h 2007-06-08 14:07:27.000000000 +0200
14706 @@ -0,0 +1,40 @@
14707 +/*
14708 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14709 + *
14710 + * Copyright (C) 2002-2007 Aleph One Ltd.
14711 + * for Toby Churchill Ltd and Brightstar Engineering
14712 + *
14713 + * Created by Charles Manning <charles@aleph1.co.uk>
14714 + *
14715 + * This program is free software; you can redistribute it and/or modify
14716 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14717 + * published by the Free Software Foundation.
14718 + *
14719 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14720 + */
14721 +
14722 +#ifndef __YAFFS_TAGSCOMPAT_H__
14723 +#define __YAFFS_TAGSCOMPAT_H__
14724 +
14725 +#include "yaffs_guts.h"
14726 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14727 + int chunkInNAND,
14728 + const __u8 * data,
14729 + const yaffs_ExtendedTags *
14730 + tags);
14731 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14732 + int chunkInNAND,
14733 + __u8 * data,
14734 + yaffs_ExtendedTags *
14735 + tags);
14736 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14737 + int blockNo);
14738 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14739 + int blockNo, yaffs_BlockState *
14740 + state, int *sequenceNumber);
14741 +
14742 +void yaffs_CalcTagsECC(yaffs_Tags * tags);
14743 +int yaffs_CheckECCOnTags(yaffs_Tags * tags);
14744 +int yaffs_CountBits(__u8 byte);
14745 +
14746 +#endif
14747 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c
14748 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
14749 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c 2007-06-08 14:07:27.000000000 +0200
14750 @@ -0,0 +1,28 @@
14751 +/*
14752 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14753 + *
14754 + * Copyright (C) 2002-2007 Aleph One Ltd.
14755 + * for Toby Churchill Ltd and Brightstar Engineering
14756 + *
14757 + * Created by Charles Manning <charles@aleph1.co.uk>
14758 + *
14759 + * This program is free software; you can redistribute it and/or modify
14760 + * it under the terms of the GNU General Public License version 2 as
14761 + * published by the Free Software Foundation.
14762 + */
14763 +
14764 +#include "yaffs_tagsvalidity.h"
14765 +
14766 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
14767 +{
14768 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14769 + tags->validMarker0 = 0xAAAAAAAA;
14770 + tags->validMarker1 = 0x55555555;
14771 +}
14772 +
14773 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
14774 +{
14775 + return (tags->validMarker0 == 0xAAAAAAAA &&
14776 + tags->validMarker1 == 0x55555555);
14777 +
14778 +}
14779 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h
14780 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
14781 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h 2007-06-08 14:07:27.000000000 +0200
14782 @@ -0,0 +1,24 @@
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 Lesser General Public License version 2.1 as
14793 + * published by the Free Software Foundation.
14794 + *
14795 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14796 + */
14797 +
14798 +
14799 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14800 +#define __YAFFS_TAGS_VALIDITY_H__
14801 +
14802 +#include "yaffs_guts.h"
14803 +
14804 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
14805 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
14806 +#endif
14807 diff -urN linux-2.6.21.1/fs/yaffs2/yaffsinterface.h linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h
14808 --- linux-2.6.21.1/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
14809 +++ linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h 2007-06-08 14:07:26.000000000 +0200
14810 @@ -0,0 +1,21 @@
14811 +/*
14812 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14813 + *
14814 + * Copyright (C) 2002-2007 Aleph One Ltd.
14815 + * for Toby Churchill Ltd and Brightstar Engineering
14816 + *
14817 + * Created by Charles Manning <charles@aleph1.co.uk>
14818 + *
14819 + * This program is free software; you can redistribute it and/or modify
14820 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14821 + * published by the Free Software Foundation.
14822 + *
14823 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14824 + */
14825 +
14826 +#ifndef __YAFFSINTERFACE_H__
14827 +#define __YAFFSINTERFACE_H__
14828 +
14829 +int yaffs_Initialise(unsigned nBlocks);
14830 +
14831 +#endif
14832 diff -urN linux-2.6.21.1/fs/yaffs2/yportenv.h linux-2.6.21.1.new/fs/yaffs2/yportenv.h
14833 --- linux-2.6.21.1/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
14834 +++ linux-2.6.21.1.new/fs/yaffs2/yportenv.h 2007-06-08 14:07:27.000000000 +0200
14835 @@ -0,0 +1,186 @@
14836 +/*
14837 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14838 + *
14839 + * Copyright (C) 2002-2007 Aleph One Ltd.
14840 + * for Toby Churchill Ltd and Brightstar Engineering
14841 + *
14842 + * Created by Charles Manning <charles@aleph1.co.uk>
14843 + *
14844 + * This program is free software; you can redistribute it and/or modify
14845 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14846 + * published by the Free Software Foundation.
14847 + *
14848 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14849 + */
14850 +
14851 +
14852 +#ifndef __YPORTENV_H__
14853 +#define __YPORTENV_H__
14854 +
14855 +#if defined CONFIG_YAFFS_WINCE
14856 +
14857 +#include "ywinceenv.h"
14858 +
14859 +#elif defined __KERNEL__
14860 +
14861 +#include "moduleconfig.h"
14862 +
14863 +/* Linux kernel */
14864 +#include <linux/version.h>
14865 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
14866 +#include <linux/config.h>
14867 +#endif
14868 +#include <linux/kernel.h>
14869 +#include <linux/mm.h>
14870 +#include <linux/string.h>
14871 +#include <linux/slab.h>
14872 +#include <linux/vmalloc.h>
14873 +
14874 +#define YCHAR char
14875 +#define YUCHAR unsigned char
14876 +#define _Y(x) x
14877 +#define yaffs_strcpy(a,b) strcpy(a,b)
14878 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14879 +#define yaffs_strncmp(a,b,c) strncmp(a,b,c)
14880 +#define yaffs_strlen(s) strlen(s)
14881 +#define yaffs_sprintf sprintf
14882 +#define yaffs_toupper(a) toupper(a)
14883 +
14884 +#define Y_INLINE inline
14885 +
14886 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14887 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14888 +
14889 +/* #define YPRINTF(x) printk x */
14890 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
14891 +#define YFREE(x) kfree(x)
14892 +#define YMALLOC_ALT(x) vmalloc(x)
14893 +#define YFREE_ALT(x) vfree(x)
14894 +#define YMALLOC_DMA(x) YMALLOC(x)
14895 +
14896 +// KR - added for use in scan so processes aren't blocked indefinitely.
14897 +#define YYIELD() schedule()
14898 +
14899 +#define YAFFS_ROOT_MODE 0666
14900 +#define YAFFS_LOSTNFOUND_MODE 0666
14901 +
14902 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
14903 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14904 +#define Y_TIME_CONVERT(x) (x).tv_sec
14905 +#else
14906 +#define Y_CURRENT_TIME CURRENT_TIME
14907 +#define Y_TIME_CONVERT(x) (x)
14908 +#endif
14909 +
14910 +#define yaffs_SumCompare(x,y) ((x) == (y))
14911 +#define yaffs_strcmp(a,b) strcmp(a,b)
14912 +
14913 +#define TENDSTR "\n"
14914 +#define TSTR(x) KERN_WARNING x
14915 +#define TOUT(p) printk p
14916 +
14917 +#define yaffs_trace(mask, fmt, args...) \
14918 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14919 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14920 + } while (0)
14921 +
14922 +#define compile_time_assertion(assertion) \
14923 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14924 +
14925 +#elif defined CONFIG_YAFFS_DIRECT
14926 +
14927 +/* Direct interface */
14928 +#include "ydirectenv.h"
14929 +
14930 +#elif defined CONFIG_YAFFS_UTIL
14931 +
14932 +/* Stuff for YAFFS utilities */
14933 +
14934 +#include "stdlib.h"
14935 +#include "stdio.h"
14936 +#include "string.h"
14937 +
14938 +#include "devextras.h"
14939 +
14940 +#define YMALLOC(x) malloc(x)
14941 +#define YFREE(x) free(x)
14942 +#define YMALLOC_ALT(x) malloc(x)
14943 +#define YFREE_ALT(x) free(x)
14944 +
14945 +#define YCHAR char
14946 +#define YUCHAR unsigned char
14947 +#define _Y(x) x
14948 +#define yaffs_strcpy(a,b) strcpy(a,b)
14949 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14950 +#define yaffs_strlen(s) strlen(s)
14951 +#define yaffs_sprintf sprintf
14952 +#define yaffs_toupper(a) toupper(a)
14953 +
14954 +#define Y_INLINE inline
14955 +
14956 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
14957 +/* #define YALERT(s) YINFO(s) */
14958 +
14959 +#define TENDSTR "\n"
14960 +#define TSTR(x) x
14961 +#define TOUT(p) printf p
14962 +
14963 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14964 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14965 +/* #define YPRINTF(x) printf x */
14966 +
14967 +#define YAFFS_ROOT_MODE 0666
14968 +#define YAFFS_LOSTNFOUND_MODE 0666
14969 +
14970 +#define yaffs_SumCompare(x,y) ((x) == (y))
14971 +#define yaffs_strcmp(a,b) strcmp(a,b)
14972 +
14973 +#else
14974 +/* Should have specified a configuration type */
14975 +#error Unknown configuration
14976 +
14977 +#endif
14978 +
14979 +/* see yaffs_fs.c */
14980 +extern unsigned int yaffs_traceMask;
14981 +extern unsigned int yaffs_wr_attempts;
14982 +
14983 +/*
14984 + * Tracing flags.
14985 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
14986 + */
14987 +
14988 +#define YAFFS_TRACE_OS 0x00000002
14989 +#define YAFFS_TRACE_ALLOCATE 0x00000004
14990 +#define YAFFS_TRACE_SCAN 0x00000008
14991 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
14992 +#define YAFFS_TRACE_ERASE 0x00000020
14993 +#define YAFFS_TRACE_GC 0x00000040
14994 +#define YAFFS_TRACE_WRITE 0x00000080
14995 +#define YAFFS_TRACE_TRACING 0x00000100
14996 +#define YAFFS_TRACE_DELETION 0x00000200
14997 +#define YAFFS_TRACE_BUFFERS 0x00000400
14998 +#define YAFFS_TRACE_NANDACCESS 0x00000800
14999 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
15000 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
15001 +#define YAFFS_TRACE_MTD 0x00004000
15002 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
15003 +
15004 +#define YAFFS_TRACE_VERIFY 0x00010000
15005 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
15006 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
15007 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
15008 +
15009 +
15010 +#define YAFFS_TRACE_ERROR 0x40000000
15011 +#define YAFFS_TRACE_BUG 0x80000000
15012 +#define YAFFS_TRACE_ALWAYS 0xF0000000
15013 +
15014 +
15015 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p);} while(0)
15016 +
15017 +#ifndef CONFIG_YAFFS_WINCE
15018 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))
15019 +#endif
15020 +
15021 +#endif
This page took 0.708731 seconds and 5 git commands to generate.