remove debug junk
[openwrt.git] / target / linux / generic-2.6 / patches / 510-Yaffs.patch
1 diff -urN linux.old/fs/Kconfig linux.dev/fs/Kconfig
2 --- linux.old/fs/Kconfig 2006-11-29 22:57:37.000000000 +0100
3 +++ linux.dev/fs/Kconfig 2006-12-14 04:21:47.000000000 +0100
4 @@ -1202,6 +1202,8 @@
5 To compile the EFS file system support as a module, choose M here: the
6 module will be called efs.
7
8 +source "fs/yaffs2/Kconfig"
9 +
10 config JFFS_FS
11 tristate "Journalling Flash File System (JFFS) support"
12 depends on MTD && BLOCK
13 diff -urN linux.old/fs/Makefile linux.dev/fs/Makefile
14 --- linux.old/fs/Makefile 2006-11-29 22:57:37.000000000 +0100
15 +++ linux.dev/fs/Makefile 2006-12-14 04:21:47.000000000 +0100
16 @@ -114,3 +114,4 @@
17 obj-$(CONFIG_DEBUG_FS) += debugfs/
18 obj-$(CONFIG_OCFS2_FS) += ocfs2/
19 obj-$(CONFIG_GFS2_FS) += gfs2/
20 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
21 diff -urN linux.old/fs/yaffs2/devextras.h linux.dev/fs/yaffs2/devextras.h
22 --- linux.old/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
23 +++ linux.dev/fs/yaffs2/devextras.h 2006-12-14 04:21:47.000000000 +0100
24 @@ -0,0 +1,265 @@
25 +/*
26 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
27 + * devextras.h
28 + *
29 + * Copyright (C) 2002 Aleph One Ltd.
30 + * for Toby Churchill Ltd and Brightstar Engineering
31 + *
32 + * Created by Charles Manning <charles@aleph1.co.uk>
33 + *
34 + * This program is free software; you can redistribute it and/or modify
35 + * it under the terms of the GNU Lesser General Public License version 2.1 as
36 + * published by the Free Software Foundation.
37 + *
38 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
39 + *
40 + * This file is just holds extra declarations used during development.
41 + * Most of these are from kernel includes placed here so we can use them in
42 + * applications.
43 + *
44 + * $Id: devextras.h,v 1.2 2005/08/11 02:37:49 marty Exp $
45 + *
46 + */
47 +
48 +#ifndef __EXTRAS_H__
49 +#define __EXTRAS_H__
50 +
51 +#if defined WIN32
52 +#define __inline__ __inline
53 +#define new newHack
54 +#endif
55 +
56 +#if !(defined __KERNEL__) || (defined WIN32)
57 +
58 +/* User space defines */
59 +
60 +typedef unsigned char __u8;
61 +typedef unsigned short __u16;
62 +typedef unsigned __u32;
63 +
64 +/*
65 + * Simple doubly linked list implementation.
66 + *
67 + * Some of the internal functions ("__xxx") are useful when
68 + * manipulating whole lists rather than single entries, as
69 + * sometimes we already know the next/prev entries and we can
70 + * generate better code by using them directly rather than
71 + * using the generic single-entry routines.
72 + */
73 +
74 +#define prefetch(x) 1
75 +
76 +struct list_head {
77 + struct list_head *next, *prev;
78 +};
79 +
80 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
81 +
82 +#define LIST_HEAD(name) \
83 + struct list_head name = LIST_HEAD_INIT(name)
84 +
85 +#define INIT_LIST_HEAD(ptr) do { \
86 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
87 +} while (0)
88 +
89 +/*
90 + * Insert a new entry between two known consecutive entries.
91 + *
92 + * This is only for internal list manipulation where we know
93 + * the prev/next entries already!
94 + */
95 +static __inline__ void __list_add(struct list_head *new,
96 + struct list_head *prev,
97 + struct list_head *next)
98 +{
99 + next->prev = new;
100 + new->next = next;
101 + new->prev = prev;
102 + prev->next = new;
103 +}
104 +
105 +/**
106 + * list_add - add a new entry
107 + * @new: new entry to be added
108 + * @head: list head to add it after
109 + *
110 + * Insert a new entry after the specified head.
111 + * This is good for implementing stacks.
112 + */
113 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
114 +{
115 + __list_add(new, head, head->next);
116 +}
117 +
118 +/**
119 + * list_add_tail - add a new entry
120 + * @new: new entry to be added
121 + * @head: list head to add it before
122 + *
123 + * Insert a new entry before the specified head.
124 + * This is useful for implementing queues.
125 + */
126 +static __inline__ void list_add_tail(struct list_head *new,
127 + struct list_head *head)
128 +{
129 + __list_add(new, head->prev, head);
130 +}
131 +
132 +/*
133 + * Delete a list entry by making the prev/next entries
134 + * point to each other.
135 + *
136 + * This is only for internal list manipulation where we know
137 + * the prev/next entries already!
138 + */
139 +static __inline__ void __list_del(struct list_head *prev,
140 + struct list_head *next)
141 +{
142 + next->prev = prev;
143 + prev->next = next;
144 +}
145 +
146 +/**
147 + * list_del - deletes entry from list.
148 + * @entry: the element to delete from the list.
149 + * Note: list_empty on entry does not return true after this, the entry is
150 + * in an undefined state.
151 + */
152 +static __inline__ void list_del(struct list_head *entry)
153 +{
154 + __list_del(entry->prev, entry->next);
155 +}
156 +
157 +/**
158 + * list_del_init - deletes entry from list and reinitialize it.
159 + * @entry: the element to delete from the list.
160 + */
161 +static __inline__ void list_del_init(struct list_head *entry)
162 +{
163 + __list_del(entry->prev, entry->next);
164 + INIT_LIST_HEAD(entry);
165 +}
166 +
167 +/**
168 + * list_empty - tests whether a list is empty
169 + * @head: the list to test.
170 + */
171 +static __inline__ int list_empty(struct list_head *head)
172 +{
173 + return head->next == head;
174 +}
175 +
176 +/**
177 + * list_splice - join two lists
178 + * @list: the new list to add.
179 + * @head: the place to add it in the first list.
180 + */
181 +static __inline__ void list_splice(struct list_head *list,
182 + struct list_head *head)
183 +{
184 + struct list_head *first = list->next;
185 +
186 + if (first != list) {
187 + struct list_head *last = list->prev;
188 + struct list_head *at = head->next;
189 +
190 + first->prev = head;
191 + head->next = first;
192 +
193 + last->next = at;
194 + at->prev = last;
195 + }
196 +}
197 +
198 +/**
199 + * list_entry - get the struct for this entry
200 + * @ptr: the &struct list_head pointer.
201 + * @type: the type of the struct this is embedded in.
202 + * @member: the name of the list_struct within the struct.
203 + */
204 +#define list_entry(ptr, type, member) \
205 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
206 +
207 +/**
208 + * list_for_each - iterate over a list
209 + * @pos: the &struct list_head to use as a loop counter.
210 + * @head: the head for your list.
211 + */
212 +#define list_for_each(pos, head) \
213 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
214 + pos = pos->next, prefetch(pos->next))
215 +
216 +/**
217 + * list_for_each_safe - iterate over a list safe against removal
218 + * of list entry
219 + * @pos: the &struct list_head to use as a loop counter.
220 + * @n: another &struct list_head to use as temporary storage
221 + * @head: the head for your list.
222 + */
223 +#define list_for_each_safe(pos, n, head) \
224 + for (pos = (head)->next, n = pos->next; pos != (head); \
225 + pos = n, n = pos->next)
226 +
227 +/*
228 + * File types
229 + */
230 +#define DT_UNKNOWN 0
231 +#define DT_FIFO 1
232 +#define DT_CHR 2
233 +#define DT_DIR 4
234 +#define DT_BLK 6
235 +#define DT_REG 8
236 +#define DT_LNK 10
237 +#define DT_SOCK 12
238 +#define DT_WHT 14
239 +
240 +#ifndef WIN32
241 +#include <sys/stat.h>
242 +#endif
243 +
244 +/*
245 + * Attribute flags. These should be or-ed together to figure out what
246 + * has been changed!
247 + */
248 +#define ATTR_MODE 1
249 +#define ATTR_UID 2
250 +#define ATTR_GID 4
251 +#define ATTR_SIZE 8
252 +#define ATTR_ATIME 16
253 +#define ATTR_MTIME 32
254 +#define ATTR_CTIME 64
255 +#define ATTR_ATIME_SET 128
256 +#define ATTR_MTIME_SET 256
257 +#define ATTR_FORCE 512 /* Not a change, but a change it */
258 +#define ATTR_ATTR_FLAG 1024
259 +
260 +struct iattr {
261 + unsigned int ia_valid;
262 + unsigned ia_mode;
263 + unsigned ia_uid;
264 + unsigned ia_gid;
265 + unsigned ia_size;
266 + unsigned ia_atime;
267 + unsigned ia_mtime;
268 + unsigned ia_ctime;
269 + unsigned int ia_attr_flags;
270 +};
271 +
272 +#define KERN_DEBUG
273 +
274 +#else
275 +
276 +#ifndef WIN32
277 +#include <linux/types.h>
278 +#include <linux/list.h>
279 +#include <linux/fs.h>
280 +#include <linux/stat.h>
281 +#endif
282 +
283 +#endif
284 +
285 +#if defined WIN32
286 +#undef new
287 +#endif
288 +
289 +#endif
290 diff -urN linux.old/fs/yaffs2/Kconfig linux.dev/fs/yaffs2/Kconfig
291 --- linux.old/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
292 +++ linux.dev/fs/yaffs2/Kconfig 2006-12-14 04:21:47.000000000 +0100
293 @@ -0,0 +1,135 @@
294 +#
295 +# YAFFS file system configurations
296 +#
297 +
298 +config YAFFS_FS
299 + tristate "YAFFS2 file system support"
300 + default n
301 + depends on MTD
302 + select YAFFS_YAFFS1
303 + select YAFFS_YAFFS2
304 + help
305 + YAFFS2, or Yet Another Flash Filing System, is a filing system
306 + optimised for NAND Flash chips.
307 +
308 + To compile the YAFFS2 file system support as a module, choose M here:
309 + the module will be called yaffs2.
310 +
311 + If unsure, say N.
312 +
313 + Further information on YAFFS2 is available at
314 + <http://www.aleph1.co.uk/yaffs/>.
315 +
316 +config YAFFS_YAFFS1
317 + bool "512 byte / page devices"
318 + depends on YAFFS_FS
319 + default y
320 + help
321 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
322 +
323 + If unsure, say Y.
324 +
325 +config YAFFS_DOES_ECC
326 + bool "Lets Yaffs do its own ECC"
327 + depends on YAFFS_FS && YAFFS_YAFFS1
328 + default n
329 + help
330 + This enables Yaffs to use its own ECC functions instead of using
331 + the ones from the generic MTD-NAND driver.
332 +
333 + If unsure, say N.
334 +
335 +config YAFFS_ECC_WRONG_ORDER
336 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
337 + depends on YAFFS_FS && YAFFS_DOES_ECC
338 + default n
339 + help
340 + This makes yaffs_ecc.c use the same ecc byte order as
341 + Steven Hill's nand_ecc.c. If not set, then you get the
342 + same ecc byte order as SmartMedia.
343 +
344 + If unsure, say N.
345 +
346 +config YAFFS_YAFFS2
347 + bool "2048 byte (or larger) / page devices"
348 + depends on YAFFS_FS
349 + default y
350 + help
351 + Enable YAFFS2 support -- yaffs for >= 2048 byte / page larger devices
352 +
353 + If unsure, say Y.
354 +
355 +config YAFFS_AUTO_YAFFS2
356 + bool "Autoselect yaffs2 format"
357 + depends on YAFFS_YAFFS2
358 + default y
359 + help
360 + Without this, you need to explicitely use yaffs2 as the file
361 + system type. With this, you can say "yaffs" and yaffs or yaffs2
362 + will be used depending on the device page size.
363 +
364 + If unsure, say Y.
365 +
366 +config YAFFS_DISABLE_LAZY_LOAD
367 + bool "Disable lazy loading"
368 + depends on YAFFS_YAFFS2
369 + default n
370 + help
371 + "Lazy loading" defers loading file details until they are
372 + required. This saves mount time, but makes the first look-up
373 + a bit longer.
374 +
375 + Lazy loading will only happen if enabled by this option being 'n'
376 + and if the appropriate tags are available, else yaffs2 will
377 + automatically fall back to immediate loading and do the right
378 + thing.
379 +
380 + Lazy laoding will be required by checkpointing.
381 +
382 + Setting this to 'y' will disable lazy loading.
383 +
384 + If unsure, say N.
385 +
386 +config YAFFS_DISABLE_WIDE_TNODES
387 + bool "Turn off wide tnodes"
388 + depends on YAFFS_FS
389 + default n
390 + help
391 + Wide tnodes are only used for large NAND arrays (>=32MB for
392 + 512-byte page devices and >=128MB for 2k page devices). They use
393 + slightly more RAM but are faster since they eliminate chunk group
394 + searching.
395 +
396 + Setting this to 'y' will force tnode width to 16 bits and make
397 + large arrays slower.
398 +
399 + If unsure, say N.
400 +
401 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
402 + bool "Force chunk erase check"
403 + depends on YAFFS_FS
404 + default n
405 + help
406 + Normally YAFFS only checks chunks before writing until an erased
407 + chunk is found. This helps to detect any partially written chunks
408 + that might have happened due to power loss.
409 +
410 + Enabling this forces on the test that chunks are erased in flash
411 + before writing to them. This takes more time but is potentially a
412 + bit more secure.
413 +
414 + Suggest setting Y during development and ironing out driver issues
415 + etc. Suggest setting to N if you want faster writing.
416 +
417 + If unsure, say Y.
418 +
419 +config YAFFS_SHORT_NAMES_IN_RAM
420 + bool "Cache short names in RAM"
421 + depends on YAFFS_FS
422 + default y
423 + help
424 + If this config is set, then short names are stored with the
425 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
426 + but makes look-ups faster.
427 +
428 + If unsure, say Y.
429 diff -urN linux.old/fs/yaffs2/Makefile linux.dev/fs/yaffs2/Makefile
430 --- linux.old/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
431 +++ linux.dev/fs/yaffs2/Makefile 2006-12-14 04:21:47.000000000 +0100
432 @@ -0,0 +1,10 @@
433 +#
434 +# Makefile for the linux YAFFS filesystem routines.
435 +#
436 +
437 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
438 +
439 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
440 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
441 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
442 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
443 diff -urN linux.old/fs/yaffs2/moduleconfig.h linux.dev/fs/yaffs2/moduleconfig.h
444 --- linux.old/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
445 +++ linux.dev/fs/yaffs2/moduleconfig.h 2006-12-14 04:21:47.000000000 +0100
446 @@ -0,0 +1,32 @@
447 +#ifndef __YAFFS_CONFIG_H__
448 +#define __YAFFS_CONFIG_H__
449 +
450 +#ifdef YAFFS_OUT_OF_TREE
451 +
452 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
453 +#define CONFIG_YAFFS_FS
454 +#define CONFIG_YAFFS_YAFFS1
455 +#define CONFIG_YAFFS_YAFFS2
456 +
457 +/* These options are independent of each other. Select those that matter. */
458 +
459 +/* Default: Not selected */
460 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
461 +//#define CONFIG_YAFFS_DOES_ECC
462 +
463 +/* Default: Not selected */
464 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
465 +/* CONFIG_YAFFS_DOES_ECC is set */
466 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
467 +
468 +/* Default: Selected */
469 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
470 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
471 +
472 +/* Default: Selected */
473 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
474 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
475 +
476 +#endif /* YAFFS_OUT_OF_TREE */
477 +
478 +#endif /* __YAFFS_CONFIG_H__ */
479 diff -urN linux.old/fs/yaffs2/yaffs_checkptrw.c linux.dev/fs/yaffs2/yaffs_checkptrw.c
480 --- linux.old/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
481 +++ linux.dev/fs/yaffs2/yaffs_checkptrw.c 2006-12-14 04:21:47.000000000 +0100
482 @@ -0,0 +1,384 @@
483 +/* YAFFS: Yet another FFS. A NAND-flash specific file system.
484 + *
485 + * Copyright (C) 2002 Aleph One Ltd.
486 + * for Toby Churchill Ltd and Brightstar Engineering
487 + *
488 + * Created by Charles Manning <charles@aleph1.co.uk>
489 + *
490 + * This program is free software; you can redistribute it and/or modify
491 + * it under the terms of the GNU General Public License version 2 as
492 + * published by the Free Software Foundation.
493 + *
494 + */
495 +
496 +const char *yaffs_checkptrw_c_version =
497 + "$Id: yaffs_checkptrw.c,v 1.11 2006/11/11 23:27:04 charles Exp $";
498 +
499 +
500 +#include "yaffs_checkptrw.h"
501 +
502 +
503 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
504 +{
505 +
506 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
507 +
508 + T(YAFFS_TRACE_CHECKPOINT,
509 + (TSTR("checkpt blocks available = %d" TENDSTR),
510 + blocksAvailable));
511 +
512 +
513 + return (blocksAvailable <= 0) ? 0 : 1;
514 +}
515 +
516 +
517 +
518 +static int yaffs_CheckpointErase(yaffs_Device *dev)
519 +{
520 +
521 + int i;
522 +
523 +
524 + if(!dev->eraseBlockInNAND)
525 + return 0;
526 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
527 + dev->internalStartBlock,dev->internalEndBlock));
528 +
529 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
530 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
531 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
532 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
533 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
534 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
535 + dev->nErasedBlocks++;
536 + dev->nFreeChunks += dev->nChunksPerBlock;
537 + }
538 + else {
539 + dev->markNANDBlockBad(dev,i);
540 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
541 + }
542 + }
543 + }
544 +
545 + dev->blocksInCheckpoint = 0;
546 +
547 + return 1;
548 +}
549 +
550 +
551 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
552 +{
553 + int i;
554 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
555 + T(YAFFS_TRACE_CHECKPOINT,
556 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
557 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
558 +
559 + if(dev->checkpointNextBlock >= 0 &&
560 + dev->checkpointNextBlock <= dev->internalEndBlock &&
561 + blocksAvailable > 0){
562 +
563 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
564 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
565 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
566 + dev->checkpointNextBlock = i + 1;
567 + dev->checkpointCurrentBlock = i;
568 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
569 + return;
570 + }
571 + }
572 + }
573 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
574 +
575 + dev->checkpointNextBlock = -1;
576 + dev->checkpointCurrentBlock = -1;
577 +}
578 +
579 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
580 +{
581 + int i;
582 + yaffs_ExtendedTags tags;
583 +
584 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
585 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
586 +
587 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
588 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
589 + int chunk = i * dev->nChunksPerBlock;
590 + int realignedChunk = chunk - dev->chunkOffset;
591 +
592 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
593 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
594 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
595 +
596 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
597 + /* Right kind of block */
598 + dev->checkpointNextBlock = tags.objectId;
599 + dev->checkpointCurrentBlock = i;
600 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
601 + dev->blocksInCheckpoint++;
602 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
603 + return;
604 + }
605 + }
606 +
607 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
608 +
609 + dev->checkpointNextBlock = -1;
610 + dev->checkpointCurrentBlock = -1;
611 +}
612 +
613 +
614 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
615 +{
616 +
617 + /* Got the functions we need? */
618 + if (!dev->writeChunkWithTagsToNAND ||
619 + !dev->readChunkWithTagsFromNAND ||
620 + !dev->eraseBlockInNAND ||
621 + !dev->markNANDBlockBad)
622 + return 0;
623 +
624 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
625 + return 0;
626 +
627 + if(!dev->checkpointBuffer)
628 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
629 + if(!dev->checkpointBuffer)
630 + return 0;
631 +
632 +
633 + dev->checkpointPageSequence = 0;
634 +
635 + dev->checkpointOpenForWrite = forWriting;
636 +
637 + dev->checkpointByteCount = 0;
638 + dev->checkpointCurrentBlock = -1;
639 + dev->checkpointCurrentChunk = -1;
640 + dev->checkpointNextBlock = dev->internalStartBlock;
641 +
642 + /* Erase all the blocks in the checkpoint area */
643 + if(forWriting){
644 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
645 + dev->checkpointByteOffset = 0;
646 + return yaffs_CheckpointErase(dev);
647 +
648 +
649 + } else {
650 + int i;
651 + /* Set to a value that will kick off a read */
652 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
653 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
654 + * going to be way more than we need */
655 + dev->blocksInCheckpoint = 0;
656 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
657 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
658 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
659 + dev->checkpointBlockList[i] = -1;
660 + }
661 +
662 + return 1;
663 +}
664 +
665 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
666 +{
667 +
668 + int chunk;
669 + int realignedChunk;
670 +
671 + yaffs_ExtendedTags tags;
672 +
673 + if(dev->checkpointCurrentBlock < 0){
674 + yaffs_CheckpointFindNextErasedBlock(dev);
675 + dev->checkpointCurrentChunk = 0;
676 + }
677 +
678 + if(dev->checkpointCurrentBlock < 0)
679 + return 0;
680 +
681 + tags.chunkDeleted = 0;
682 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
683 + tags.chunkId = dev->checkpointPageSequence + 1;
684 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
685 + tags.byteCount = dev->nDataBytesPerChunk;
686 + if(dev->checkpointCurrentChunk == 0){
687 + /* First chunk we write for the block? Set block state to
688 + checkpoint */
689 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
690 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
691 + dev->blocksInCheckpoint++;
692 + }
693 +
694 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
695 +
696 +
697 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
698 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
699 +
700 + realignedChunk = chunk - dev->chunkOffset;
701 +
702 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
703 + dev->checkpointByteOffset = 0;
704 + dev->checkpointPageSequence++;
705 + dev->checkpointCurrentChunk++;
706 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
707 + dev->checkpointCurrentChunk = 0;
708 + dev->checkpointCurrentBlock = -1;
709 + }
710 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
711 +
712 + return 1;
713 +}
714 +
715 +
716 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
717 +{
718 + int i=0;
719 + int ok = 1;
720 +
721 +
722 + __u8 * dataBytes = (__u8 *)data;
723 +
724 +
725 +
726 + if(!dev->checkpointBuffer)
727 + return 0;
728 +
729 + while(i < nBytes && ok) {
730 +
731 +
732 +
733 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
734 + dev->checkpointByteOffset++;
735 + i++;
736 + dataBytes++;
737 + dev->checkpointByteCount++;
738 +
739 +
740 + if(dev->checkpointByteOffset < 0 ||
741 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
742 + ok = yaffs_CheckpointFlushBuffer(dev);
743 +
744 + }
745 +
746 + return i;
747 +}
748 +
749 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
750 +{
751 + int i=0;
752 + int ok = 1;
753 + yaffs_ExtendedTags tags;
754 +
755 +
756 + int chunk;
757 + int realignedChunk;
758 +
759 + __u8 *dataBytes = (__u8 *)data;
760 +
761 + if(!dev->checkpointBuffer)
762 + return 0;
763 +
764 + while(i < nBytes && ok) {
765 +
766 +
767 + if(dev->checkpointByteOffset < 0 ||
768 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
769 +
770 + if(dev->checkpointCurrentBlock < 0){
771 + yaffs_CheckpointFindNextCheckpointBlock(dev);
772 + dev->checkpointCurrentChunk = 0;
773 + }
774 +
775 + if(dev->checkpointCurrentBlock < 0)
776 + ok = 0;
777 + else {
778 +
779 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
780 + dev->checkpointCurrentChunk;
781 +
782 + realignedChunk = chunk - dev->chunkOffset;
783 +
784 + /* read in the next chunk */
785 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
786 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
787 + dev->checkpointBuffer,
788 + &tags);
789 +
790 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
791 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
792 + ok = 0;
793 +
794 + dev->checkpointByteOffset = 0;
795 + dev->checkpointPageSequence++;
796 + dev->checkpointCurrentChunk++;
797 +
798 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
799 + dev->checkpointCurrentBlock = -1;
800 + }
801 + }
802 +
803 + if(ok){
804 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
805 + dev->checkpointByteOffset++;
806 + i++;
807 + dataBytes++;
808 + dev->checkpointByteCount++;
809 + }
810 + }
811 +
812 + return i;
813 +}
814 +
815 +int yaffs_CheckpointClose(yaffs_Device *dev)
816 +{
817 +
818 + if(dev->checkpointOpenForWrite){
819 + if(dev->checkpointByteOffset != 0)
820 + yaffs_CheckpointFlushBuffer(dev);
821 + } else {
822 + int i;
823 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
824 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
825 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
826 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
827 + else {
828 + // Todo this looks odd...
829 + }
830 + }
831 + YFREE(dev->checkpointBlockList);
832 + dev->checkpointBlockList = NULL;
833 + }
834 +
835 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
836 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
837 +
838 +
839 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
840 + dev->checkpointByteCount));
841 +
842 + if(dev->checkpointBuffer){
843 + /* free the buffer */
844 + YFREE(dev->checkpointBuffer);
845 + dev->checkpointBuffer = NULL;
846 + return 1;
847 + }
848 + else
849 + return 0;
850 +
851 +}
852 +
853 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
854 +{
855 + /* Erase the first checksum block */
856 +
857 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
858 +
859 + if(!yaffs_CheckpointSpaceOk(dev))
860 + return 0;
861 +
862 + return yaffs_CheckpointErase(dev);
863 +}
864 +
865 +
866 +
867 diff -urN linux.old/fs/yaffs2/yaffs_checkptrw.h linux.dev/fs/yaffs2/yaffs_checkptrw.h
868 --- linux.old/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
869 +++ linux.dev/fs/yaffs2/yaffs_checkptrw.h 2006-12-14 04:21:47.000000000 +0100
870 @@ -0,0 +1,18 @@
871 +#ifndef __YAFFS_CHECKPTRW_H__
872 +#define __YAFFS_CHECKPTRW_H__
873 +
874 +#include "yaffs_guts.h"
875 +
876 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
877 +
878 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
879 +
880 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
881 +
882 +int yaffs_CheckpointClose(yaffs_Device *dev);
883 +
884 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
885 +
886 +
887 +#endif
888 +
889 diff -urN linux.old/fs/yaffs2/yaffs_ecc.c linux.dev/fs/yaffs2/yaffs_ecc.c
890 --- linux.old/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
891 +++ linux.dev/fs/yaffs2/yaffs_ecc.c 2006-12-14 04:21:47.000000000 +0100
892 @@ -0,0 +1,333 @@
893 +/*
894 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
895 + *
896 + * yaffs_ecc.c: ECC generation/correction algorithms.
897 + *
898 + * Copyright (C) 2002 Aleph One Ltd.
899 + *
900 + * Created by Charles Manning <charles@aleph1.co.uk>
901 + *
902 + *
903 + * This program is free software; you can redistribute it and/or
904 + * modify it under the terms of the GNU Lesser General Public License
905 + * version 2.1 as published by the Free Software Foundation.
906 + */
907 +
908 + /*
909 + * This code implements the ECC algorithm used in SmartMedia.
910 + *
911 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
912 + * The two unused bit are set to 1.
913 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
914 + * blocks are used on a 512-byte NAND page.
915 + *
916 + */
917 +
918 +/* Table generated by gen-ecc.c
919 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
920 + * for each byte of data. These are instead provided in a table in bits7..2.
921 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
922 + * this bytes influence on the line parity.
923 + */
924 +
925 +const char *yaffs_ecc_c_version =
926 + "$Id: yaffs_ecc.c,v 1.7 2006/09/14 22:02:46 charles Exp $";
927 +
928 +#include "yportenv.h"
929 +
930 +#include "yaffs_ecc.h"
931 +
932 +static const unsigned char column_parity_table[] = {
933 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
934 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
935 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
936 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
937 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
938 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
939 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
940 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
941 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
942 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
943 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
944 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
945 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
946 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
947 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
948 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
949 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
950 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
951 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
952 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
953 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
954 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
955 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
956 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
957 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
958 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
959 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
960 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
961 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
962 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
963 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
964 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
965 +};
966 +
967 +/* Count the bits in an unsigned char or a U32 */
968 +
969 +static int yaffs_CountBits(unsigned char x)
970 +{
971 + int r = 0;
972 + while (x) {
973 + if (x & 1)
974 + r++;
975 + x >>= 1;
976 + }
977 + return r;
978 +}
979 +
980 +static int yaffs_CountBits32(unsigned x)
981 +{
982 + int r = 0;
983 + while (x) {
984 + if (x & 1)
985 + r++;
986 + x >>= 1;
987 + }
988 + return r;
989 +}
990 +
991 +/* Calculate the ECC for a 256-byte block of data */
992 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
993 +{
994 + unsigned int i;
995 +
996 + unsigned char col_parity = 0;
997 + unsigned char line_parity = 0;
998 + unsigned char line_parity_prime = 0;
999 + unsigned char t;
1000 + unsigned char b;
1001 +
1002 + for (i = 0; i < 256; i++) {
1003 + b = column_parity_table[*data++];
1004 + col_parity ^= b;
1005 +
1006 + if (b & 0x01) // odd number of bits in the byte
1007 + {
1008 + line_parity ^= i;
1009 + line_parity_prime ^= ~i;
1010 + }
1011 +
1012 + }
1013 +
1014 + ecc[2] = (~col_parity) | 0x03;
1015 +
1016 + t = 0;
1017 + if (line_parity & 0x80)
1018 + t |= 0x80;
1019 + if (line_parity_prime & 0x80)
1020 + t |= 0x40;
1021 + if (line_parity & 0x40)
1022 + t |= 0x20;
1023 + if (line_parity_prime & 0x40)
1024 + t |= 0x10;
1025 + if (line_parity & 0x20)
1026 + t |= 0x08;
1027 + if (line_parity_prime & 0x20)
1028 + t |= 0x04;
1029 + if (line_parity & 0x10)
1030 + t |= 0x02;
1031 + if (line_parity_prime & 0x10)
1032 + t |= 0x01;
1033 + ecc[1] = ~t;
1034 +
1035 + t = 0;
1036 + if (line_parity & 0x08)
1037 + t |= 0x80;
1038 + if (line_parity_prime & 0x08)
1039 + t |= 0x40;
1040 + if (line_parity & 0x04)
1041 + t |= 0x20;
1042 + if (line_parity_prime & 0x04)
1043 + t |= 0x10;
1044 + if (line_parity & 0x02)
1045 + t |= 0x08;
1046 + if (line_parity_prime & 0x02)
1047 + t |= 0x04;
1048 + if (line_parity & 0x01)
1049 + t |= 0x02;
1050 + if (line_parity_prime & 0x01)
1051 + t |= 0x01;
1052 + ecc[0] = ~t;
1053 +
1054 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1055 + // Swap the bytes into the wrong order
1056 + t = ecc[0];
1057 + ecc[0] = ecc[1];
1058 + ecc[1] = t;
1059 +#endif
1060 +}
1061 +
1062 +
1063 +/* Correct the ECC on a 256 byte block of data */
1064 +
1065 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1066 + const unsigned char *test_ecc)
1067 +{
1068 + unsigned char d0, d1, d2; /* deltas */
1069 +
1070 + d0 = read_ecc[0] ^ test_ecc[0];
1071 + d1 = read_ecc[1] ^ test_ecc[1];
1072 + d2 = read_ecc[2] ^ test_ecc[2];
1073 +
1074 + if ((d0 | d1 | d2) == 0)
1075 + return 0; /* no error */
1076 +
1077 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1078 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1079 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1080 + /* Single bit (recoverable) error in data */
1081 +
1082 + unsigned byte;
1083 + unsigned bit;
1084 +
1085 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1086 + // swap the bytes to correct for the wrong order
1087 + unsigned char t;
1088 +
1089 + t = d0;
1090 + d0 = d1;
1091 + d1 = t;
1092 +#endif
1093 +
1094 + bit = byte = 0;
1095 +
1096 + if (d1 & 0x80)
1097 + byte |= 0x80;
1098 + if (d1 & 0x20)
1099 + byte |= 0x40;
1100 + if (d1 & 0x08)
1101 + byte |= 0x20;
1102 + if (d1 & 0x02)
1103 + byte |= 0x10;
1104 + if (d0 & 0x80)
1105 + byte |= 0x08;
1106 + if (d0 & 0x20)
1107 + byte |= 0x04;
1108 + if (d0 & 0x08)
1109 + byte |= 0x02;
1110 + if (d0 & 0x02)
1111 + byte |= 0x01;
1112 +
1113 + if (d2 & 0x80)
1114 + bit |= 0x04;
1115 + if (d2 & 0x20)
1116 + bit |= 0x02;
1117 + if (d2 & 0x08)
1118 + bit |= 0x01;
1119 +
1120 + data[byte] ^= (1 << bit);
1121 +
1122 + return 1; /* Corrected the error */
1123 + }
1124 +
1125 + if ((yaffs_CountBits(d0) +
1126 + yaffs_CountBits(d1) +
1127 + yaffs_CountBits(d2)) == 1) {
1128 + /* Reccoverable error in ecc */
1129 +
1130 + read_ecc[0] = test_ecc[0];
1131 + read_ecc[1] = test_ecc[1];
1132 + read_ecc[2] = test_ecc[2];
1133 +
1134 + return 1; /* Corrected the error */
1135 + }
1136 +
1137 + /* Unrecoverable error */
1138 +
1139 + return -1;
1140 +
1141 +}
1142 +
1143 +
1144 +/*
1145 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1146 + */
1147 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1148 + yaffs_ECCOther * eccOther)
1149 +{
1150 + unsigned int i;
1151 +
1152 + unsigned char col_parity = 0;
1153 + unsigned line_parity = 0;
1154 + unsigned line_parity_prime = 0;
1155 + unsigned char b;
1156 +
1157 + for (i = 0; i < nBytes; i++) {
1158 + b = column_parity_table[*data++];
1159 + col_parity ^= b;
1160 +
1161 + if (b & 0x01) {
1162 + /* odd number of bits in the byte */
1163 + line_parity ^= i;
1164 + line_parity_prime ^= ~i;
1165 + }
1166 +
1167 + }
1168 +
1169 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1170 + eccOther->lineParity = line_parity;
1171 + eccOther->lineParityPrime = line_parity_prime;
1172 +}
1173 +
1174 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1175 + yaffs_ECCOther * read_ecc,
1176 + const yaffs_ECCOther * test_ecc)
1177 +{
1178 + unsigned char cDelta; /* column parity delta */
1179 + unsigned lDelta; /* line parity delta */
1180 + unsigned lDeltaPrime; /* line parity delta */
1181 + unsigned bit;
1182 +
1183 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1184 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1185 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1186 +
1187 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1188 + return 0; /* no error */
1189 +
1190 + if (lDelta == ~lDeltaPrime &&
1191 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1192 + {
1193 + /* Single bit (recoverable) error in data */
1194 +
1195 + bit = 0;
1196 +
1197 + if (cDelta & 0x20)
1198 + bit |= 0x04;
1199 + if (cDelta & 0x08)
1200 + bit |= 0x02;
1201 + if (cDelta & 0x02)
1202 + bit |= 0x01;
1203 +
1204 + if(lDelta >= nBytes)
1205 + return -1;
1206 +
1207 + data[lDelta] ^= (1 << bit);
1208 +
1209 + return 1; /* corrected */
1210 + }
1211 +
1212 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1213 + yaffs_CountBits(cDelta)) == 1) {
1214 + /* Reccoverable error in ecc */
1215 +
1216 + *read_ecc = *test_ecc;
1217 + return 1; /* corrected */
1218 + }
1219 +
1220 + /* Unrecoverable error */
1221 +
1222 + return -1;
1223 +
1224 +}
1225 +
1226 diff -urN linux.old/fs/yaffs2/yaffs_ecc.h linux.dev/fs/yaffs2/yaffs_ecc.h
1227 --- linux.old/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1228 +++ linux.dev/fs/yaffs2/yaffs_ecc.h 2006-12-14 04:21:47.000000000 +0100
1229 @@ -0,0 +1,44 @@
1230 +/*
1231 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
1232 + *
1233 + * yaffs_ecc.c: ECC generation/correction algorithms.
1234 + *
1235 + * Copyright (C) 2002 Aleph One Ltd.
1236 + *
1237 + * Created by Charles Manning <charles@aleph1.co.uk>
1238 + *
1239 + * This program is free software; you can redistribute it and/or modify
1240 + * it under the terms of the GNU General Public License version 2 as
1241 + * published by the Free Software Foundation.
1242 + *
1243 + */
1244 +
1245 + /*
1246 + * This code implements the ECC algorithm used in SmartMedia.
1247 + *
1248 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1249 + * The two unused bit are set to 1.
1250 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1251 + * blocks are used on a 512-byte NAND page.
1252 + *
1253 + */
1254 +
1255 +#ifndef __YAFFS_ECC_H__
1256 +#define __YAFFS_ECC_H__
1257 +
1258 +typedef struct {
1259 + unsigned char colParity;
1260 + unsigned lineParity;
1261 + unsigned lineParityPrime;
1262 +} yaffs_ECCOther;
1263 +
1264 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1265 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1266 + const unsigned char *test_ecc);
1267 +
1268 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1269 + yaffs_ECCOther * ecc);
1270 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1271 + yaffs_ECCOther * read_ecc,
1272 + const yaffs_ECCOther * test_ecc);
1273 +#endif
1274 diff -urN linux.old/fs/yaffs2/yaffs_fs.c linux.dev/fs/yaffs2/yaffs_fs.c
1275 --- linux.old/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1276 +++ linux.dev/fs/yaffs2/yaffs_fs.c 2006-12-14 04:33:02.000000000 +0100
1277 @@ -0,0 +1,2136 @@
1278 +/*
1279 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
1280 + * yaffs_fs.c
1281 + *
1282 + * Copyright (C) 2002 Aleph One Ltd.
1283 + * for Toby Churchill Ltd and Brightstar Engineering
1284 + *
1285 + * Created by Charles Manning <charles@aleph1.co.uk>
1286 + *
1287 + * This program is free software; you can redistribute it and/or modify
1288 + * it under the terms of the GNU General Public License version 2 as
1289 + * published by the Free Software Foundation.
1290 + *
1291 + * This is the file system front-end to YAFFS that hooks it up to
1292 + * the VFS.
1293 + *
1294 + * Special notes:
1295 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1296 + * this superblock
1297 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1298 + * superblock
1299 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1300 + *
1301 + * Acknowledgements:
1302 + * * Luc van OostenRyck for numerous patches.
1303 + * * Nick Bane for numerous patches.
1304 + * * Nick Bane for 2.5/2.6 integration.
1305 + * * Andras Toth for mknod rdev issue.
1306 + * * Michael Fischer for finding the problem with inode inconsistency.
1307 + * * Some code bodily lifted from JFFS2.
1308 + */
1309 +
1310 +const char *yaffs_fs_c_version =
1311 + "$Id: yaffs_fs.c,v 1.54 2006/10/24 18:09:15 charles Exp $";
1312 +extern const char *yaffs_guts_c_version;
1313 +
1314 +#include <linux/autoconf.h>
1315 +#include <linux/kernel.h>
1316 +#include <linux/module.h>
1317 +#include <linux/version.h>
1318 +#include <linux/slab.h>
1319 +#include <linux/init.h>
1320 +#include <linux/list.h>
1321 +#include <linux/fs.h>
1322 +#include <linux/proc_fs.h>
1323 +#include <linux/smp_lock.h>
1324 +#include <linux/pagemap.h>
1325 +#include <linux/mtd/mtd.h>
1326 +#include <linux/interrupt.h>
1327 +#include <linux/string.h>
1328 +#include <linux/ctype.h>
1329 +
1330 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1331 +
1332 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1333 +#include <asm/statfs.h>
1334 +#define UnlockPage(p) unlock_page(p)
1335 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1336 +
1337 +/* FIXME: use sb->s_id instead ? */
1338 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1339 +
1340 +#else
1341 +
1342 +#include <linux/locks.h>
1343 +#define BDEVNAME_SIZE 0
1344 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1345 +
1346 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1347 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1348 +#define __user
1349 +#endif
1350 +
1351 +#endif
1352 +
1353 +#include <asm/uaccess.h>
1354 +
1355 +#include "yportenv.h"
1356 +#include "yaffs_guts.h"
1357 +
1358 +unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS |
1359 + YAFFS_TRACE_BAD_BLOCKS
1360 + /* | 0xFFFFFFFF */;
1361 +
1362 +#include <linux/mtd/mtd.h>
1363 +#include "yaffs_mtdif.h"
1364 +#include "yaffs_mtdif2.h"
1365 +
1366 +/*#define T(x) printk x */
1367 +
1368 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1369 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->i_private))
1370 +#else
1371 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip))
1372 +#endif
1373 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1374 +
1375 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1376 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1377 +#else
1378 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1379 +#endif
1380 +
1381 +static void yaffs_put_super(struct super_block *sb);
1382 +
1383 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1384 + loff_t * pos);
1385 +
1386 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1387 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1388 +#else
1389 +static int yaffs_file_flush(struct file *file);
1390 +#endif
1391 +
1392 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1393 + int datasync);
1394 +
1395 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1396 +
1397 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1398 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1399 + struct nameidata *n);
1400 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1401 + struct nameidata *n);
1402 +#else
1403 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1404 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1405 +#endif
1406 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1407 + struct dentry *dentry);
1408 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1409 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1410 + const char *symname);
1411 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1412 +
1413 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1414 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1415 + dev_t dev);
1416 +#else
1417 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1418 + int dev);
1419 +#endif
1420 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1421 + struct inode *new_dir, struct dentry *new_dentry);
1422 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1423 +
1424 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1425 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1426 +static void yaffs_write_super(struct super_block *sb);
1427 +#else
1428 +static int yaffs_sync_fs(struct super_block *sb);
1429 +static int yaffs_write_super(struct super_block *sb);
1430 +#endif
1431 +
1432 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1433 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1434 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1435 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1436 +#else
1437 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1438 +#endif
1439 +static void yaffs_read_inode(struct inode *inode);
1440 +
1441 +static void yaffs_put_inode(struct inode *inode);
1442 +static void yaffs_delete_inode(struct inode *);
1443 +static void yaffs_clear_inode(struct inode *);
1444 +
1445 +static int yaffs_readpage(struct file *file, struct page *page);
1446 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1447 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1448 +#else
1449 +static int yaffs_writepage(struct page *page);
1450 +#endif
1451 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1452 + unsigned offset, unsigned to);
1453 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1454 + unsigned to);
1455 +
1456 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1457 + int buflen);
1458 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1459 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1460 +#else
1461 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1462 +#endif
1463 +
1464 +static struct address_space_operations yaffs_file_address_operations = {
1465 + .readpage = yaffs_readpage,
1466 + .writepage = yaffs_writepage,
1467 + .prepare_write = yaffs_prepare_write,
1468 + .commit_write = yaffs_commit_write,
1469 +};
1470 +
1471 +static struct file_operations yaffs_file_operations = {
1472 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1473 + .read = do_sync_read,
1474 + .write = do_sync_write,
1475 + .aio_read = generic_file_aio_read,
1476 + .aio_write = generic_file_aio_write,
1477 +#else
1478 + .read = generic_file_read,
1479 + .write = generic_file_write,
1480 +#endif
1481 + .mmap = generic_file_mmap,
1482 + .flush = yaffs_file_flush,
1483 + .fsync = yaffs_sync_object,
1484 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1485 + .sendfile = generic_file_sendfile,
1486 +#endif
1487 +
1488 +};
1489 +
1490 +static struct inode_operations yaffs_file_inode_operations = {
1491 + .setattr = yaffs_setattr,
1492 +};
1493 +
1494 +static struct inode_operations yaffs_symlink_inode_operations = {
1495 + .readlink = yaffs_readlink,
1496 + .follow_link = yaffs_follow_link,
1497 + .setattr = yaffs_setattr,
1498 +};
1499 +
1500 +static struct inode_operations yaffs_dir_inode_operations = {
1501 + .create = yaffs_create,
1502 + .lookup = yaffs_lookup,
1503 + .link = yaffs_link,
1504 + .unlink = yaffs_unlink,
1505 + .symlink = yaffs_symlink,
1506 + .mkdir = yaffs_mkdir,
1507 + .rmdir = yaffs_unlink,
1508 + .mknod = yaffs_mknod,
1509 + .rename = yaffs_rename,
1510 + .setattr = yaffs_setattr,
1511 +};
1512 +
1513 +static struct file_operations yaffs_dir_operations = {
1514 + .read = generic_read_dir,
1515 + .readdir = yaffs_readdir,
1516 + .fsync = yaffs_sync_object,
1517 +};
1518 +
1519 +static struct super_operations yaffs_super_ops = {
1520 + .statfs = yaffs_statfs,
1521 + .read_inode = yaffs_read_inode,
1522 + .put_inode = yaffs_put_inode,
1523 + .put_super = yaffs_put_super,
1524 + .delete_inode = yaffs_delete_inode,
1525 + .clear_inode = yaffs_clear_inode,
1526 + .sync_fs = yaffs_sync_fs,
1527 + .write_super = yaffs_write_super,
1528 +};
1529 +
1530 +static void yaffs_GrossLock(yaffs_Device * dev)
1531 +{
1532 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1533 +
1534 + down(&dev->grossLock);
1535 +}
1536 +
1537 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1538 +{
1539 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1540 + up(&dev->grossLock);
1541 +
1542 +}
1543 +
1544 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1545 + int buflen)
1546 +{
1547 + unsigned char *alias;
1548 + int ret;
1549 +
1550 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1551 +
1552 + yaffs_GrossLock(dev);
1553 +
1554 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1555 +
1556 + yaffs_GrossUnlock(dev);
1557 +
1558 + if (!alias)
1559 + return -ENOMEM;
1560 +
1561 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1562 + kfree(alias);
1563 + return ret;
1564 +}
1565 +
1566 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1567 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1568 +#else
1569 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1570 +#endif
1571 +{
1572 + unsigned char *alias;
1573 + int ret;
1574 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1575 +
1576 + yaffs_GrossLock(dev);
1577 +
1578 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1579 +
1580 + yaffs_GrossUnlock(dev);
1581 +
1582 + if (!alias)
1583 + {
1584 + ret = -ENOMEM;
1585 + goto out;
1586 + }
1587 +
1588 + ret = vfs_follow_link(nd, alias);
1589 + kfree(alias);
1590 +out:
1591 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1592 + return ERR_PTR (ret);
1593 +#else
1594 + return ret;
1595 +#endif
1596 +}
1597 +
1598 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1599 + yaffs_Object * obj);
1600 +
1601 +/*
1602 + * Lookup is used to find objects in the fs
1603 + */
1604 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1605 +
1606 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1607 + struct nameidata *n)
1608 +#else
1609 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1610 +#endif
1611 +{
1612 + yaffs_Object *obj;
1613 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1614 +
1615 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1616 +
1617 + yaffs_GrossLock(dev);
1618 +
1619 + T(YAFFS_TRACE_OS,
1620 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1621 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1622 +
1623 + obj =
1624 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1625 + dentry->d_name.name);
1626 +
1627 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1628 +
1629 + /* Can't hold gross lock when calling yaffs_get_inode() */
1630 + yaffs_GrossUnlock(dev);
1631 +
1632 + if (obj) {
1633 + T(YAFFS_TRACE_OS,
1634 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1635 +
1636 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1637 +
1638 + if (inode) {
1639 + T(YAFFS_TRACE_OS,
1640 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1641 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1642 + * d_add even if NULL inode */
1643 +#if 0
1644 + /*dget(dentry); // try to solve directory bug */
1645 + d_add(dentry, inode);
1646 +
1647 + /* return dentry; */
1648 + return NULL;
1649 +#endif
1650 + }
1651 +
1652 + } else {
1653 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1654 +
1655 + }
1656 +
1657 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1658 + * NULL which creates dentry hash */
1659 + d_add(dentry, inode);
1660 +
1661 + return NULL;
1662 + /* return (ERR_PTR(-EIO)); */
1663 +
1664 +}
1665 +
1666 +/* For now put inode is just for debugging
1667 + * Put inode is called when the inode **structure** is put.
1668 + */
1669 +static void yaffs_put_inode(struct inode *inode)
1670 +{
1671 + T(YAFFS_TRACE_OS,
1672 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1673 + atomic_read(&inode->i_count)));
1674 +
1675 +}
1676 +
1677 +/* clear is called to tell the fs to release any per-inode data it holds */
1678 +static void yaffs_clear_inode(struct inode *inode)
1679 +{
1680 + yaffs_Object *obj;
1681 + yaffs_Device *dev;
1682 +
1683 + obj = yaffs_InodeToObject(inode);
1684 +
1685 + T(YAFFS_TRACE_OS,
1686 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1687 + atomic_read(&inode->i_count),
1688 + obj ? "object exists" : "null object"));
1689 +
1690 + if (obj) {
1691 + dev = obj->myDev;
1692 + yaffs_GrossLock(dev);
1693 +
1694 + /* Clear the association between the inode and
1695 + * the yaffs_Object.
1696 + */
1697 + obj->myInode = NULL;
1698 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1699 + inode->i_private = NULL;
1700 +#else
1701 + inode->u.generic_ip = NULL;
1702 +#endif
1703 +
1704 + /* If the object freeing was deferred, then the real
1705 + * free happens now.
1706 + * This should fix the inode inconsistency problem.
1707 + */
1708 +
1709 + yaffs_HandleDeferedFree(obj);
1710 +
1711 + yaffs_GrossUnlock(dev);
1712 + }
1713 +
1714 +}
1715 +
1716 +/* delete is called when the link count is zero and the inode
1717 + * is put (ie. nobody wants to know about it anymore, time to
1718 + * delete the file).
1719 + * NB Must call clear_inode()
1720 + */
1721 +static void yaffs_delete_inode(struct inode *inode)
1722 +{
1723 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1724 + yaffs_Device *dev;
1725 +
1726 + T(YAFFS_TRACE_OS,
1727 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1728 + atomic_read(&inode->i_count),
1729 + obj ? "object exists" : "null object"));
1730 +
1731 + if (obj) {
1732 + dev = obj->myDev;
1733 + yaffs_GrossLock(dev);
1734 + yaffs_DeleteFile(obj);
1735 + yaffs_GrossUnlock(dev);
1736 + }
1737 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1738 + truncate_inode_pages (&inode->i_data, 0);
1739 +#endif
1740 + clear_inode(inode);
1741 +}
1742 +
1743 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1744 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1745 +#else
1746 +static int yaffs_file_flush(struct file *file)
1747 +#endif
1748 +{
1749 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1750 +
1751 + yaffs_Device *dev = obj->myDev;
1752 +
1753 + T(YAFFS_TRACE_OS,
1754 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1755 + obj->dirty ? "dirty" : "clean"));
1756 +
1757 + yaffs_GrossLock(dev);
1758 +
1759 + yaffs_FlushFile(obj, 1);
1760 +
1761 + yaffs_GrossUnlock(dev);
1762 +
1763 + return 0;
1764 +}
1765 +
1766 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1767 +{
1768 + /* Lifted from jffs2 */
1769 +
1770 + yaffs_Object *obj;
1771 + unsigned char *pg_buf;
1772 + int ret;
1773 +
1774 + yaffs_Device *dev;
1775 +
1776 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1777 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1778 + (unsigned)PAGE_CACHE_SIZE));
1779 +
1780 + obj = yaffs_DentryToObject(f->f_dentry);
1781 +
1782 + dev = obj->myDev;
1783 +
1784 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1785 + BUG_ON(!PageLocked(pg));
1786 +#else
1787 + if (!PageLocked(pg))
1788 + PAGE_BUG(pg);
1789 +#endif
1790 +
1791 + pg_buf = kmap(pg);
1792 + /* FIXME: Can kmap fail? */
1793 +
1794 + yaffs_GrossLock(dev);
1795 +
1796 + ret =
1797 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1798 + PAGE_CACHE_SIZE);
1799 +
1800 + yaffs_GrossUnlock(dev);
1801 +
1802 + if (ret >= 0)
1803 + ret = 0;
1804 +
1805 + if (ret) {
1806 + ClearPageUptodate(pg);
1807 + SetPageError(pg);
1808 + } else {
1809 + SetPageUptodate(pg);
1810 + ClearPageError(pg);
1811 + }
1812 +
1813 + flush_dcache_page(pg);
1814 + kunmap(pg);
1815 +
1816 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1817 + return ret;
1818 +}
1819 +
1820 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1821 +{
1822 + int ret = yaffs_readpage_nolock(f, pg);
1823 + UnlockPage(pg);
1824 + return ret;
1825 +}
1826 +
1827 +static int yaffs_readpage(struct file *f, struct page *pg)
1828 +{
1829 + return yaffs_readpage_unlock(f, pg);
1830 +}
1831 +
1832 +/* writepage inspired by/stolen from smbfs */
1833 +
1834 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1835 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1836 +#else
1837 +static int yaffs_writepage(struct page *page)
1838 +#endif
1839 +{
1840 + struct address_space *mapping = page->mapping;
1841 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1842 + struct inode *inode;
1843 + unsigned long end_index;
1844 + char *buffer;
1845 + yaffs_Object *obj;
1846 + int nWritten = 0;
1847 + unsigned nBytes;
1848 +
1849 + if (!mapping)
1850 + BUG();
1851 + inode = mapping->host;
1852 + if (!inode)
1853 + BUG();
1854 +
1855 + if (offset > inode->i_size) {
1856 + T(YAFFS_TRACE_OS,
1857 + (KERN_DEBUG
1858 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1859 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1860 + (unsigned)inode->i_size));
1861 + T(YAFFS_TRACE_OS,
1862 + (KERN_DEBUG " -> don't care!!\n"));
1863 + unlock_page(page);
1864 + return 0;
1865 + }
1866 +
1867 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1868 +
1869 + /* easy case */
1870 + if (page->index < end_index) {
1871 + nBytes = PAGE_CACHE_SIZE;
1872 + } else {
1873 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1874 + }
1875 +
1876 + get_page(page);
1877 +
1878 + buffer = kmap(page);
1879 +
1880 + obj = yaffs_InodeToObject(inode);
1881 + yaffs_GrossLock(obj->myDev);
1882 +
1883 + T(YAFFS_TRACE_OS,
1884 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
1885 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1886 + T(YAFFS_TRACE_OS,
1887 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
1888 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1889 +
1890 + nWritten =
1891 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
1892 + nBytes, 0);
1893 +
1894 + T(YAFFS_TRACE_OS,
1895 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
1896 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1897 +
1898 + yaffs_GrossUnlock(obj->myDev);
1899 +
1900 + kunmap(page);
1901 + SetPageUptodate(page);
1902 + UnlockPage(page);
1903 + put_page(page);
1904 +
1905 + return (nWritten == nBytes) ? 0 : -ENOSPC;
1906 +}
1907 +
1908 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1909 + unsigned offset, unsigned to)
1910 +{
1911 +
1912 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
1913 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
1914 + return yaffs_readpage_nolock(f, pg);
1915 +
1916 + return 0;
1917 +
1918 +}
1919 +
1920 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1921 + unsigned to)
1922 +{
1923 +
1924 + void *addr = page_address(pg) + offset;
1925 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
1926 + int nBytes = to - offset;
1927 + int nWritten;
1928 +
1929 + unsigned spos = pos;
1930 + unsigned saddr = (unsigned)addr;
1931 +
1932 + T(YAFFS_TRACE_OS,
1933 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
1934 + spos, nBytes));
1935 +
1936 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
1937 +
1938 + if (nWritten != nBytes) {
1939 + T(YAFFS_TRACE_OS,
1940 + (KERN_DEBUG
1941 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
1942 + nWritten, nBytes));
1943 + SetPageError(pg);
1944 + ClearPageUptodate(pg);
1945 + } else {
1946 + SetPageUptodate(pg);
1947 + }
1948 +
1949 + T(YAFFS_TRACE_OS,
1950 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
1951 + nWritten == nBytes ? 0 : nWritten));
1952 +
1953 + return nWritten == nBytes ? 0 : nWritten;
1954 +
1955 +}
1956 +
1957 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
1958 +{
1959 + if (inode && obj) {
1960 +
1961 +
1962 + /* Check mode against the variant type and attempt to repair if broken. */
1963 + __u32 mode = obj->yst_mode;
1964 + switch( obj->variantType ){
1965 + case YAFFS_OBJECT_TYPE_FILE :
1966 + if( ! S_ISREG(mode) ){
1967 + obj->yst_mode &= ~S_IFMT;
1968 + obj->yst_mode |= S_IFREG;
1969 + }
1970 +
1971 + break;
1972 + case YAFFS_OBJECT_TYPE_SYMLINK :
1973 + if( ! S_ISLNK(mode) ){
1974 + obj->yst_mode &= ~S_IFMT;
1975 + obj->yst_mode |= S_IFLNK;
1976 + }
1977 +
1978 + break;
1979 + case YAFFS_OBJECT_TYPE_DIRECTORY :
1980 + if( ! S_ISDIR(mode) ){
1981 + obj->yst_mode &= ~S_IFMT;
1982 + obj->yst_mode |= S_IFDIR;
1983 + }
1984 +
1985 + break;
1986 + case YAFFS_OBJECT_TYPE_UNKNOWN :
1987 + case YAFFS_OBJECT_TYPE_HARDLINK :
1988 + case YAFFS_OBJECT_TYPE_SPECIAL :
1989 + default:
1990 + /* TODO? */
1991 + break;
1992 + }
1993 +
1994 + inode->i_ino = obj->objectId;
1995 + inode->i_mode = obj->yst_mode;
1996 + inode->i_uid = obj->yst_uid;
1997 + inode->i_gid = obj->yst_gid;
1998 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1999 + inode->i_blksize = inode->i_sb->s_blocksize;
2000 +#endif
2001 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2002 +
2003 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2004 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2005 + inode->i_atime.tv_nsec = 0;
2006 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2007 + inode->i_mtime.tv_nsec = 0;
2008 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2009 + inode->i_ctime.tv_nsec = 0;
2010 +#else
2011 + inode->i_rdev = obj->yst_rdev;
2012 + inode->i_atime = obj->yst_atime;
2013 + inode->i_mtime = obj->yst_mtime;
2014 + inode->i_ctime = obj->yst_ctime;
2015 +#endif
2016 + inode->i_size = yaffs_GetObjectFileLength(obj);
2017 + inode->i_blocks = (inode->i_size + 511) >> 9;
2018 +
2019 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2020 +
2021 + T(YAFFS_TRACE_OS,
2022 + (KERN_DEBUG
2023 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2024 + inode->i_mode, inode->i_uid, inode->i_gid,
2025 + (int)inode->i_size, atomic_read(&inode->i_count)));
2026 +
2027 + switch (obj->yst_mode & S_IFMT) {
2028 + default: /* fifo, device or socket */
2029 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2030 + init_special_inode(inode, obj->yst_mode,
2031 + old_decode_dev(obj->yst_rdev));
2032 +#else
2033 + init_special_inode(inode, obj->yst_mode,
2034 + (dev_t) (obj->yst_rdev));
2035 +#endif
2036 + break;
2037 + case S_IFREG: /* file */
2038 + inode->i_op = &yaffs_file_inode_operations;
2039 + inode->i_fop = &yaffs_file_operations;
2040 + inode->i_mapping->a_ops =
2041 + &yaffs_file_address_operations;
2042 + break;
2043 + case S_IFDIR: /* directory */
2044 + inode->i_op = &yaffs_dir_inode_operations;
2045 + inode->i_fop = &yaffs_dir_operations;
2046 + break;
2047 + case S_IFLNK: /* symlink */
2048 + inode->i_op = &yaffs_symlink_inode_operations;
2049 + break;
2050 + }
2051 +
2052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
2053 + inode->i_private = obj;
2054 +#else
2055 + inode->u.generic_ip = obj;
2056 +#endif
2057 + obj->myInode = inode;
2058 +
2059 + } else {
2060 + T(YAFFS_TRACE_OS,
2061 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2062 + }
2063 +
2064 +}
2065 +
2066 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2067 + yaffs_Object * obj)
2068 +{
2069 + struct inode *inode;
2070 +
2071 + if (!sb) {
2072 + T(YAFFS_TRACE_OS,
2073 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2074 + return NULL;
2075 +
2076 + }
2077 +
2078 + if (!obj) {
2079 + T(YAFFS_TRACE_OS,
2080 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2081 + return NULL;
2082 +
2083 + }
2084 +
2085 + T(YAFFS_TRACE_OS,
2086 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2087 +
2088 + inode = iget(sb, obj->objectId);
2089 +
2090 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2091 + /* iget also increments the inode's i_count */
2092 + /* NB You can't be holding grossLock or deadlock will happen! */
2093 +
2094 + return inode;
2095 +}
2096 +
2097 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2098 + loff_t * pos)
2099 +{
2100 + yaffs_Object *obj;
2101 + int nWritten, ipos;
2102 + struct inode *inode;
2103 + yaffs_Device *dev;
2104 +
2105 + obj = yaffs_DentryToObject(f->f_dentry);
2106 +
2107 + dev = obj->myDev;
2108 +
2109 + yaffs_GrossLock(dev);
2110 +
2111 + inode = f->f_dentry->d_inode;
2112 +
2113 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2114 + ipos = inode->i_size;
2115 + } else {
2116 + ipos = *pos;
2117 + }
2118 +
2119 + if (!obj) {
2120 + T(YAFFS_TRACE_OS,
2121 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2122 + } else {
2123 + T(YAFFS_TRACE_OS,
2124 + (KERN_DEBUG
2125 + "yaffs_file_write about to write writing %d bytes"
2126 + "to object %d at %d\n",
2127 + n, obj->objectId, ipos));
2128 + }
2129 +
2130 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2131 +
2132 + T(YAFFS_TRACE_OS,
2133 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2134 + n, nWritten, ipos));
2135 + if (nWritten > 0) {
2136 + ipos += nWritten;
2137 + *pos = ipos;
2138 + if (ipos > inode->i_size) {
2139 + inode->i_size = ipos;
2140 + inode->i_blocks = (ipos + 511) >> 9;
2141 +
2142 + T(YAFFS_TRACE_OS,
2143 + (KERN_DEBUG
2144 + "yaffs_file_write size updated to %d bytes, "
2145 + "%d blocks\n",
2146 + ipos, (int)(inode->i_blocks)));
2147 + }
2148 +
2149 + }
2150 + yaffs_GrossUnlock(dev);
2151 + return nWritten == 0 ? -ENOSPC : nWritten;
2152 +}
2153 +
2154 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2155 +{
2156 + yaffs_Object *obj;
2157 + yaffs_Device *dev;
2158 + struct inode *inode = f->f_dentry->d_inode;
2159 + unsigned long offset, curoffs;
2160 + struct list_head *i;
2161 + yaffs_Object *l;
2162 +
2163 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2164 +
2165 + obj = yaffs_DentryToObject(f->f_dentry);
2166 + dev = obj->myDev;
2167 +
2168 + yaffs_GrossLock(dev);
2169 +
2170 + offset = f->f_pos;
2171 +
2172 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2173 +
2174 + if (offset == 0) {
2175 + T(YAFFS_TRACE_OS,
2176 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2177 + (int)inode->i_ino));
2178 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2179 + < 0) {
2180 + goto out;
2181 + }
2182 + offset++;
2183 + f->f_pos++;
2184 + }
2185 + if (offset == 1) {
2186 + T(YAFFS_TRACE_OS,
2187 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2188 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2189 + if (filldir
2190 + (dirent, "..", 2, offset,
2191 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2192 + goto out;
2193 + }
2194 + offset++;
2195 + f->f_pos++;
2196 + }
2197 +
2198 + curoffs = 1;
2199 +
2200 + /* If the directory has changed since the open or last call to
2201 + readdir, rewind to after the 2 canned entries. */
2202 +
2203 + if (f->f_version != inode->i_version) {
2204 + offset = 2;
2205 + f->f_pos = offset;
2206 + f->f_version = inode->i_version;
2207 + }
2208 +
2209 + list_for_each(i, &obj->variant.directoryVariant.children) {
2210 + curoffs++;
2211 + if (curoffs >= offset) {
2212 + l = list_entry(i, yaffs_Object, siblings);
2213 +
2214 + yaffs_GetObjectName(l, name,
2215 + YAFFS_MAX_NAME_LENGTH + 1);
2216 + T(YAFFS_TRACE_OS,
2217 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2218 + yaffs_GetObjectInode(l)));
2219 +
2220 + if (filldir(dirent,
2221 + name,
2222 + strlen(name),
2223 + offset,
2224 + yaffs_GetObjectInode(l),
2225 + yaffs_GetObjectType(l))
2226 + < 0) {
2227 + goto up_and_out;
2228 + }
2229 +
2230 + offset++;
2231 + f->f_pos++;
2232 + }
2233 + }
2234 +
2235 + up_and_out:
2236 + out:
2237 +
2238 + yaffs_GrossUnlock(dev);
2239 +
2240 + return 0;
2241 +}
2242 +
2243 +/*
2244 + * File creation. Allocate an inode, and we're done..
2245 + */
2246 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2247 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2248 + dev_t rdev)
2249 +#else
2250 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2251 + int rdev)
2252 +#endif
2253 +{
2254 + struct inode *inode;
2255 +
2256 + yaffs_Object *obj = NULL;
2257 + yaffs_Device *dev;
2258 +
2259 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2260 +
2261 + int error = -ENOSPC;
2262 + uid_t uid = current->fsuid;
2263 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2264 +
2265 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2266 + mode |= S_ISGID;
2267 +
2268 + if (parent) {
2269 + T(YAFFS_TRACE_OS,
2270 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2271 + parent->objectId, parent->variantType));
2272 + } else {
2273 + T(YAFFS_TRACE_OS,
2274 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2275 + return -EPERM;
2276 + }
2277 +
2278 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2279 + "mode %x dev %x\n",
2280 + dentry->d_name.name, mode, rdev));
2281 +
2282 + dev = parent->myDev;
2283 +
2284 + yaffs_GrossLock(dev);
2285 +
2286 + switch (mode & S_IFMT) {
2287 + default:
2288 + /* Special (socket, fifo, device...) */
2289 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2290 + "yaffs_mknod: making special\n"));
2291 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2292 + obj =
2293 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2294 + gid, old_encode_dev(rdev));
2295 +#else
2296 + obj =
2297 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2298 + gid, rdev);
2299 +#endif
2300 + break;
2301 + case S_IFREG: /* file */
2302 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2303 + obj =
2304 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2305 + gid);
2306 + break;
2307 + case S_IFDIR: /* directory */
2308 + T(YAFFS_TRACE_OS,
2309 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2310 + obj =
2311 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2312 + uid, gid);
2313 + break;
2314 + case S_IFLNK: /* symlink */
2315 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2316 + obj = NULL; /* Do we ever get here? */
2317 + break;
2318 + }
2319 +
2320 + /* Can not call yaffs_get_inode() with gross lock held */
2321 + yaffs_GrossUnlock(dev);
2322 +
2323 + if (obj) {
2324 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2325 + d_instantiate(dentry, inode);
2326 + T(YAFFS_TRACE_OS,
2327 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2328 + obj->objectId, atomic_read(&inode->i_count)));
2329 + error = 0;
2330 + } else {
2331 + T(YAFFS_TRACE_OS,
2332 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2333 + error = -ENOMEM;
2334 + }
2335 +
2336 + return error;
2337 +}
2338 +
2339 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2340 +{
2341 + int retVal;
2342 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2343 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2344 +#if 0
2345 + /* attempt to fix dir bug - didn't work */
2346 + if (!retVal) {
2347 + dget(dentry);
2348 + }
2349 +#endif
2350 + return retVal;
2351 +}
2352 +
2353 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2354 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2355 + struct nameidata *n)
2356 +#else
2357 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2358 +#endif
2359 +{
2360 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2361 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2362 +}
2363 +
2364 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2365 +{
2366 + int retVal;
2367 +
2368 + yaffs_Device *dev;
2369 +
2370 + T(YAFFS_TRACE_OS,
2371 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2372 + dentry->d_name.name));
2373 +
2374 + dev = yaffs_InodeToObject(dir)->myDev;
2375 +
2376 + yaffs_GrossLock(dev);
2377 +
2378 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2379 +
2380 + if (retVal == YAFFS_OK) {
2381 + dentry->d_inode->i_nlink--;
2382 + dir->i_version++;
2383 + yaffs_GrossUnlock(dev);
2384 + mark_inode_dirty(dentry->d_inode);
2385 + return 0;
2386 + }
2387 + yaffs_GrossUnlock(dev);
2388 + return -ENOTEMPTY;
2389 +}
2390 +
2391 +/*
2392 + * Create a link...
2393 + */
2394 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2395 + struct dentry *dentry)
2396 +{
2397 + struct inode *inode = old_dentry->d_inode;
2398 + yaffs_Object *obj = NULL;
2399 + yaffs_Object *link = NULL;
2400 + yaffs_Device *dev;
2401 +
2402 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2403 +
2404 + obj = yaffs_InodeToObject(inode);
2405 + dev = obj->myDev;
2406 +
2407 + yaffs_GrossLock(dev);
2408 +
2409 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2410 + {
2411 + link =
2412 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2413 + obj);
2414 + }
2415 +
2416 + if (link) {
2417 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2418 + d_instantiate(dentry, old_dentry->d_inode);
2419 + atomic_inc(&old_dentry->d_inode->i_count);
2420 + T(YAFFS_TRACE_OS,
2421 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2422 + old_dentry->d_inode->i_nlink,
2423 + atomic_read(&old_dentry->d_inode->i_count)));
2424 +
2425 + }
2426 +
2427 + yaffs_GrossUnlock(dev);
2428 +
2429 + if (link) {
2430 +
2431 + return 0;
2432 + }
2433 +
2434 + return -EPERM;
2435 +}
2436 +
2437 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2438 + const char *symname)
2439 +{
2440 + yaffs_Object *obj;
2441 + yaffs_Device *dev;
2442 + uid_t uid = current->fsuid;
2443 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2444 +
2445 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2446 +
2447 + dev = yaffs_InodeToObject(dir)->myDev;
2448 + yaffs_GrossLock(dev);
2449 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2450 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2451 + yaffs_GrossUnlock(dev);
2452 +
2453 + if (obj) {
2454 +
2455 + struct inode *inode;
2456 +
2457 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2458 + d_instantiate(dentry, inode);
2459 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2460 + return 0;
2461 + } else {
2462 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2463 +
2464 + }
2465 +
2466 + return -ENOMEM;
2467 +}
2468 +
2469 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2470 + int datasync)
2471 +{
2472 +
2473 + yaffs_Object *obj;
2474 + yaffs_Device *dev;
2475 +
2476 + obj = yaffs_DentryToObject(dentry);
2477 +
2478 + dev = obj->myDev;
2479 +
2480 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2481 + yaffs_GrossLock(dev);
2482 + yaffs_FlushFile(obj, 1);
2483 + yaffs_GrossUnlock(dev);
2484 + return 0;
2485 +}
2486 +
2487 +/*
2488 + * The VFS layer already does all the dentry stuff for rename.
2489 + *
2490 + * NB: POSIX says you can rename an object over an old object of the same name
2491 + */
2492 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2493 + struct inode *new_dir, struct dentry *new_dentry)
2494 +{
2495 + yaffs_Device *dev;
2496 + int retVal = YAFFS_FAIL;
2497 + yaffs_Object *target;
2498 +
2499 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2500 + dev = yaffs_InodeToObject(old_dir)->myDev;
2501 +
2502 + yaffs_GrossLock(dev);
2503 +
2504 + /* Check if the target is an existing directory that is not empty. */
2505 + target =
2506 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2507 + new_dentry->d_name.name);
2508 +
2509 +
2510 +
2511 + if (target &&
2512 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2513 + !list_empty(&target->variant.directoryVariant.children)) {
2514 +
2515 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2516 +
2517 + retVal = YAFFS_FAIL;
2518 + } else {
2519 +
2520 + /* Now does unlinking internally using shadowing mechanism */
2521 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2522 +
2523 + retVal =
2524 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2525 + old_dentry->d_name.name,
2526 + yaffs_InodeToObject(new_dir),
2527 + new_dentry->d_name.name);
2528 +
2529 + }
2530 + yaffs_GrossUnlock(dev);
2531 +
2532 + if (retVal == YAFFS_OK) {
2533 + if(target) {
2534 + new_dentry->d_inode->i_nlink--;
2535 + mark_inode_dirty(new_dentry->d_inode);
2536 + }
2537 +
2538 + return 0;
2539 + } else {
2540 + return -ENOTEMPTY;
2541 + }
2542 +
2543 +}
2544 +
2545 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2546 +{
2547 + struct inode *inode = dentry->d_inode;
2548 + int error;
2549 + yaffs_Device *dev;
2550 +
2551 + T(YAFFS_TRACE_OS,
2552 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2553 + yaffs_InodeToObject(inode)->objectId));
2554 +
2555 + if ((error = inode_change_ok(inode, attr)) == 0) {
2556 +
2557 + dev = yaffs_InodeToObject(inode)->myDev;
2558 + yaffs_GrossLock(dev);
2559 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2560 + YAFFS_OK) {
2561 + error = 0;
2562 + } else {
2563 + error = -EPERM;
2564 + }
2565 + yaffs_GrossUnlock(dev);
2566 + if (!error)
2567 + error = inode_setattr(inode, attr);
2568 + }
2569 + return error;
2570 +}
2571 +
2572 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2573 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2574 +{
2575 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2576 + struct super_block *sb = dentry->d_sb;
2577 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2578 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2579 +{
2580 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2581 +#else
2582 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2583 +{
2584 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2585 +#endif
2586 +
2587 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2588 +
2589 + yaffs_GrossLock(dev);
2590 +
2591 + buf->f_type = YAFFS_MAGIC;
2592 + buf->f_bsize = sb->s_blocksize;
2593 + buf->f_namelen = 255;
2594 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2595 +
2596 + buf->f_blocks =
2597 + (dev->endBlock - dev->startBlock +
2598 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2599 + dev->nDataBytesPerChunk);
2600 + buf->f_bfree =
2601 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2602 + dev->nDataBytesPerChunk);
2603 + } else {
2604 +
2605 + buf->f_blocks =
2606 + (dev->endBlock - dev->startBlock +
2607 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2608 + sb->s_blocksize);
2609 + buf->f_bfree =
2610 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2611 + sb->s_blocksize);
2612 + }
2613 + buf->f_files = 0;
2614 + buf->f_ffree = 0;
2615 + buf->f_bavail = buf->f_bfree;
2616 +
2617 + yaffs_GrossUnlock(dev);
2618 + return 0;
2619 +}
2620 +
2621 +
2622 +
2623 +static int yaffs_do_sync_fs(struct super_block *sb)
2624 +{
2625 +
2626 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2627 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2628 +
2629 + if(sb->s_dirt) {
2630 + yaffs_GrossLock(dev);
2631 +
2632 + if(dev)
2633 + yaffs_CheckpointSave(dev);
2634 +
2635 + yaffs_GrossUnlock(dev);
2636 +
2637 + sb->s_dirt = 0;
2638 + }
2639 + return 0;
2640 +}
2641 +
2642 +
2643 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2644 +static void yaffs_write_super(struct super_block *sb)
2645 +#else
2646 +static int yaffs_write_super(struct super_block *sb)
2647 +#endif
2648 +{
2649 +
2650 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2651 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2652 + return 0; /* yaffs_do_sync_fs(sb);*/
2653 +#endif
2654 +}
2655 +
2656 +
2657 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2658 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2659 +#else
2660 +static int yaffs_sync_fs(struct super_block *sb)
2661 +#endif
2662 +{
2663 +
2664 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2665 +
2666 + return 0; /* yaffs_do_sync_fs(sb);*/
2667 +
2668 +}
2669 +
2670 +
2671 +static void yaffs_read_inode(struct inode *inode)
2672 +{
2673 + /* NB This is called as a side effect of other functions, but
2674 + * we had to release the lock to prevent deadlocks, so
2675 + * need to lock again.
2676 + */
2677 +
2678 + yaffs_Object *obj;
2679 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2680 +
2681 + T(YAFFS_TRACE_OS,
2682 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2683 +
2684 + yaffs_GrossLock(dev);
2685 +
2686 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2687 +
2688 + yaffs_FillInodeFromObject(inode, obj);
2689 +
2690 + yaffs_GrossUnlock(dev);
2691 +}
2692 +
2693 +static LIST_HEAD(yaffs_dev_list);
2694 +
2695 +static void yaffs_put_super(struct super_block *sb)
2696 +{
2697 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2698 +
2699 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2700 +
2701 + yaffs_GrossLock(dev);
2702 +
2703 + yaffs_FlushEntireDeviceCache(dev);
2704 +
2705 + if (dev->putSuperFunc) {
2706 + dev->putSuperFunc(sb);
2707 + }
2708 +
2709 + yaffs_CheckpointSave(dev);
2710 + yaffs_Deinitialise(dev);
2711 +
2712 + yaffs_GrossUnlock(dev);
2713 +
2714 + /* we assume this is protected by lock_kernel() in mount/umount */
2715 + list_del(&dev->devList);
2716 +
2717 + if(dev->spareBuffer){
2718 + YFREE(dev->spareBuffer);
2719 + dev->spareBuffer = NULL;
2720 + }
2721 +
2722 + kfree(dev);
2723 +}
2724 +
2725 +
2726 +static void yaffs_MTDPutSuper(struct super_block *sb)
2727 +{
2728 +
2729 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2730 +
2731 + if (mtd->sync) {
2732 + mtd->sync(mtd);
2733 + }
2734 +
2735 + put_mtd_device(mtd);
2736 +}
2737 +
2738 +
2739 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2740 +{
2741 + struct super_block *sb = (struct super_block *)vsb;
2742 +
2743 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2744 +// if(sb)
2745 +// sb->s_dirt = 1;
2746 +}
2747 +
2748 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2749 + struct super_block *sb,
2750 + void *data, int silent)
2751 +{
2752 + int nBlocks;
2753 + struct inode *inode = NULL;
2754 + struct dentry *root;
2755 + yaffs_Device *dev = 0;
2756 + char devname_buf[BDEVNAME_SIZE + 1];
2757 + struct mtd_info *mtd;
2758 + int err;
2759 +
2760 + sb->s_magic = YAFFS_MAGIC;
2761 + sb->s_op = &yaffs_super_ops;
2762 +
2763 + if (!sb)
2764 + printk(KERN_INFO "yaffs: sb is NULL\n");
2765 + else if (!sb->s_dev)
2766 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2767 + else if (!yaffs_devname(sb, devname_buf))
2768 + printk(KERN_INFO "yaffs: devname is NULL\n");
2769 + else
2770 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2771 + sb->s_dev,
2772 + yaffs_devname(sb, devname_buf));
2773 +
2774 + sb->s_blocksize = PAGE_CACHE_SIZE;
2775 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2776 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2777 + T(YAFFS_TRACE_OS,
2778 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2779 +
2780 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2781 + T(YAFFS_TRACE_OS,
2782 + ("yaffs: Write verification disabled. All guarantees "
2783 + "null and void\n"));
2784 +#endif
2785 +
2786 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2787 + "\"%s\"\n",
2788 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
2789 + yaffs_devname(sb, devname_buf)));
2790 +
2791 + /* Check it's an mtd device..... */
2792 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
2793 + return NULL; /* This isn't an mtd device */
2794 + }
2795 + /* Get the device */
2796 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2797 + if (!mtd) {
2798 + T(YAFFS_TRACE_ALWAYS,
2799 + ("yaffs: MTD device #%u doesn't appear to exist\n",
2800 + MINOR(sb->s_dev)));
2801 + return NULL;
2802 + }
2803 + /* Check it's NAND */
2804 + if (mtd->type != MTD_NANDFLASH) {
2805 + T(YAFFS_TRACE_ALWAYS,
2806 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
2807 + return NULL;
2808 + }
2809 +
2810 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
2811 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
2812 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
2813 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
2814 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
2815 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
2816 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
2817 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2818 + T(YAFFS_TRACE_OS, (" writesize %d\n", mtd->writesize));
2819 +#else
2820 + T(YAFFS_TRACE_OS, (" oobblock %d\n", mtd->oobblock));
2821 +#endif
2822 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
2823 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
2824 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
2825 +
2826 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
2827 +
2828 + if (yaffsVersion == 1 &&
2829 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2830 + mtd->writesize >= 2048) {
2831 +#else
2832 + mtd->oobblock >= 2048) {
2833 +#endif
2834 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
2835 + yaffsVersion = 2;
2836 + }
2837 +
2838 + /* Added NCB 26/5/2006 for completeness */
2839 + if (yaffsVersion == 2 &&
2840 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2841 + mtd->writesize == 512) {
2842 +#else
2843 + mtd->oobblock == 512) {
2844 +#endif
2845 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
2846 + yaffsVersion = 1;
2847 + }
2848 +
2849 +#endif
2850 +
2851 + if (yaffsVersion == 2) {
2852 + /* Check for version 2 style functions */
2853 + if (!mtd->erase ||
2854 + !mtd->block_isbad ||
2855 + !mtd->block_markbad ||
2856 + !mtd->read ||
2857 + !mtd->write ||
2858 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2859 + !mtd->read_oob || !mtd->write_oob) {
2860 +#else
2861 + !mtd->write_ecc ||
2862 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2863 +#endif
2864 + T(YAFFS_TRACE_ALWAYS,
2865 + ("yaffs: MTD device does not support required "
2866 + "functions\n"));;
2867 + return NULL;
2868 + }
2869 +
2870 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2871 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2872 +#else
2873 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
2874 +#endif
2875 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
2876 + T(YAFFS_TRACE_ALWAYS,
2877 + ("yaffs: MTD device does not have the "
2878 + "right page sizes\n"));
2879 + return NULL;
2880 + }
2881 + } else {
2882 + /* Check for V1 style functions */
2883 + if (!mtd->erase ||
2884 + !mtd->read ||
2885 + !mtd->write ||
2886 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2887 + !mtd->read_oob || !mtd->write_oob) {
2888 +#else
2889 + !mtd->write_ecc ||
2890 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
2891 +#endif
2892 + T(YAFFS_TRACE_ALWAYS,
2893 + ("yaffs: MTD device does not support required "
2894 + "functions\n"));;
2895 + return NULL;
2896 + }
2897 +
2898 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2899 + if (mtd->writesize < YAFFS_BYTES_PER_CHUNK ||
2900 +#else
2901 + if (mtd->oobblock < YAFFS_BYTES_PER_CHUNK ||
2902 +#endif
2903 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
2904 + T(YAFFS_TRACE_ALWAYS,
2905 + ("yaffs: MTD device does not support have the "
2906 + "right page sizes\n"));
2907 + return NULL;
2908 + }
2909 + }
2910 +
2911 + /* OK, so if we got here, we have an MTD that's NAND and looks
2912 + * like it has the right capabilities
2913 + * Set the yaffs_Device up for mtd
2914 + */
2915 +
2916 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2917 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2918 +#else
2919 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
2920 +#endif
2921 + if (!dev) {
2922 + /* Deep shit could not allocate device structure */
2923 + T(YAFFS_TRACE_ALWAYS,
2924 + ("yaffs_read_super: Failed trying to allocate "
2925 + "yaffs_Device. \n"));
2926 + return NULL;
2927 + }
2928 +
2929 + memset(dev, 0, sizeof(yaffs_Device));
2930 + dev->genericDevice = mtd;
2931 + dev->name = mtd->name;
2932 +
2933 + /* Set up the memory size parameters.... */
2934 +
2935 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
2936 + dev->startBlock = 0;
2937 + dev->endBlock = nBlocks - 1;
2938 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
2939 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
2940 + dev->nReservedBlocks = 5;
2941 + dev->nShortOpCaches = 10; /* Enable short op caching */
2942 +
2943 + /* ... and the functions. */
2944 + if (yaffsVersion == 2) {
2945 + dev->writeChunkWithTagsToNAND =
2946 + nandmtd2_WriteChunkWithTagsToNAND;
2947 + dev->readChunkWithTagsFromNAND =
2948 + nandmtd2_ReadChunkWithTagsFromNAND;
2949 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
2950 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
2951 + dev->spareBuffer = YMALLOC(mtd->oobsize);
2952 + dev->isYaffs2 = 1;
2953 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2954 + dev->nDataBytesPerChunk = mtd->writesize;
2955 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
2956 +#else
2957 + dev->nDataBytesPerChunk = mtd->oobblock;
2958 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
2959 +#endif
2960 + nBlocks = mtd->size / mtd->erasesize;
2961 +
2962 + dev->nCheckpointReservedBlocks = 0;
2963 + dev->startBlock = 0;
2964 + dev->endBlock = nBlocks - 1;
2965 + } else {
2966 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
2967 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
2968 + dev->isYaffs2 = 0;
2969 + }
2970 + /* ... and common functions */
2971 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
2972 + dev->initialiseNAND = nandmtd_InitialiseNAND;
2973 +
2974 + dev->putSuperFunc = yaffs_MTDPutSuper;
2975 +
2976 + dev->superBlock = (void *)sb;
2977 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
2978 +
2979 +
2980 +#ifndef CONFIG_YAFFS_DOES_ECC
2981 + dev->useNANDECC = 1;
2982 +#endif
2983 +
2984 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
2985 + dev->wideTnodesDisabled = 1;
2986 +#endif
2987 +
2988 + /* we assume this is protected by lock_kernel() in mount/umount */
2989 + list_add_tail(&dev->devList, &yaffs_dev_list);
2990 +
2991 + init_MUTEX(&dev->grossLock);
2992 +
2993 + yaffs_GrossLock(dev);
2994 +
2995 + err = yaffs_GutsInitialise(dev);
2996 +
2997 + T(YAFFS_TRACE_OS,
2998 + ("yaffs_read_super: guts initialised %s\n",
2999 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3000 +
3001 + /* Release lock before yaffs_get_inode() */
3002 + yaffs_GrossUnlock(dev);
3003 +
3004 + /* Create root inode */
3005 + if (err == YAFFS_OK)
3006 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3007 + yaffs_Root(dev));
3008 +
3009 + if (!inode)
3010 + return NULL;
3011 +
3012 + inode->i_op = &yaffs_dir_inode_operations;
3013 + inode->i_fop = &yaffs_dir_operations;
3014 +
3015 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3016 +
3017 + root = d_alloc_root(inode);
3018 +
3019 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3020 +
3021 + if (!root) {
3022 + iput(inode);
3023 + return NULL;
3024 + }
3025 + sb->s_root = root;
3026 +
3027 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3028 + return sb;
3029 +}
3030 +
3031 +
3032 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3033 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3034 + int silent)
3035 +{
3036 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3037 +}
3038 +
3039 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3040 +static int yaffs_read_super(struct file_system_type *fs,
3041 + int flags, const char *dev_name,
3042 + void *data, struct vfsmount *mnt)
3043 +{
3044 +
3045 + return get_sb_bdev(fs, flags, dev_name, data,
3046 + yaffs_internal_read_super_mtd, mnt);
3047 +}
3048 +#else
3049 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3050 + int flags, const char *dev_name,
3051 + void *data)
3052 +{
3053 +
3054 + return get_sb_bdev(fs, flags, dev_name, data,
3055 + yaffs_internal_read_super_mtd);
3056 +}
3057 +#endif
3058 +
3059 +static struct file_system_type yaffs_fs_type = {
3060 + .owner = THIS_MODULE,
3061 + .name = "yaffs",
3062 + .get_sb = yaffs_read_super,
3063 + .kill_sb = kill_block_super,
3064 + .fs_flags = FS_REQUIRES_DEV,
3065 +};
3066 +#else
3067 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3068 + int silent)
3069 +{
3070 + return yaffs_internal_read_super(1, sb, data, silent);
3071 +}
3072 +
3073 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3074 + FS_REQUIRES_DEV);
3075 +#endif
3076 +
3077 +
3078 +#ifdef CONFIG_YAFFS_YAFFS2
3079 +
3080 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3081 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3082 + int silent)
3083 +{
3084 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3085 +}
3086 +
3087 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3088 +static int yaffs2_read_super(struct file_system_type *fs,
3089 + int flags, const char *dev_name, void *data,
3090 + struct vfsmount *mnt)
3091 +{
3092 + return get_sb_bdev(fs, flags, dev_name, data,
3093 + yaffs2_internal_read_super_mtd, mnt);
3094 +}
3095 +#else
3096 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3097 + int flags, const char *dev_name,
3098 + void *data)
3099 +{
3100 +
3101 + return get_sb_bdev(fs, flags, dev_name, data,
3102 + yaffs2_internal_read_super_mtd);
3103 +}
3104 +#endif
3105 +
3106 +static struct file_system_type yaffs2_fs_type = {
3107 + .owner = THIS_MODULE,
3108 + .name = "yaffs2",
3109 + .get_sb = yaffs2_read_super,
3110 + .kill_sb = kill_block_super,
3111 + .fs_flags = FS_REQUIRES_DEV,
3112 +};
3113 +#else
3114 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3115 + void *data, int silent)
3116 +{
3117 + return yaffs_internal_read_super(2, sb, data, silent);
3118 +}
3119 +
3120 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3121 + FS_REQUIRES_DEV);
3122 +#endif
3123 +
3124 +#endif /* CONFIG_YAFFS_YAFFS2 */
3125 +
3126 +static struct proc_dir_entry *my_proc_entry;
3127 +
3128 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3129 +{
3130 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3131 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3132 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3133 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3134 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3135 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3136 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3137 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3138 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3139 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3140 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3141 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3142 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3143 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3144 + buf +=
3145 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3146 + buf +=
3147 + sprintf(buf, "passiveGCs......... %d\n",
3148 + dev->passiveGarbageCollections);
3149 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3150 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3151 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3152 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3153 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3154 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3155 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3156 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3157 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3158 + buf +=
3159 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3160 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3161 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3162 +
3163 + return buf;
3164 +}
3165 +
3166 +static int yaffs_proc_read(char *page,
3167 + char **start,
3168 + off_t offset, int count, int *eof, void *data)
3169 +{
3170 + struct list_head *item;
3171 + char *buf = page;
3172 + int step = offset;
3173 + int n = 0;
3174 +
3175 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3176 + * We use 'offset' (*ppos) to indicate where we are in devList.
3177 + * This also assumes the user has posted a read buffer large
3178 + * enough to hold the complete output; but that's life in /proc.
3179 + */
3180 +
3181 + *(int *)start = 1;
3182 +
3183 + /* Print header first */
3184 + if (step == 0) {
3185 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3186 + "\n%s\n%s\n", yaffs_fs_c_version,
3187 + yaffs_guts_c_version);
3188 + }
3189 +
3190 + /* hold lock_kernel while traversing yaffs_dev_list */
3191 + lock_kernel();
3192 +
3193 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3194 + list_for_each(item, &yaffs_dev_list) {
3195 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3196 + if (n < step) {
3197 + n++;
3198 + continue;
3199 + }
3200 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3201 + buf = yaffs_dump_dev(buf, dev);
3202 + break;
3203 + }
3204 + unlock_kernel();
3205 +
3206 + return buf - page < count ? buf - page : count;
3207 +}
3208 +
3209 +/**
3210 + * Set the verbosity of the warnings and error messages.
3211 + *
3212 + */
3213 +
3214 +static struct {
3215 + char *mask_name;
3216 + unsigned mask_bitfield;
3217 +} mask_flags[] = {
3218 + {"allocate", YAFFS_TRACE_ALLOCATE},
3219 + {"always", YAFFS_TRACE_ALWAYS},
3220 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3221 + {"buffers", YAFFS_TRACE_BUFFERS},
3222 + {"bug", YAFFS_TRACE_BUG},
3223 + {"deletion", YAFFS_TRACE_DELETION},
3224 + {"erase", YAFFS_TRACE_ERASE},
3225 + {"error", YAFFS_TRACE_ERROR},
3226 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3227 + {"gc", YAFFS_TRACE_GC},
3228 + {"mtd", YAFFS_TRACE_MTD},
3229 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3230 + {"os", YAFFS_TRACE_OS},
3231 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3232 + {"scan", YAFFS_TRACE_SCAN},
3233 + {"tracing", YAFFS_TRACE_TRACING},
3234 + {"write", YAFFS_TRACE_WRITE},
3235 + {"all", 0xffffffff},
3236 + {"none", 0},
3237 + {NULL, 0},
3238 +};
3239 +
3240 +static int yaffs_proc_write(struct file *file, const char *buf,
3241 + unsigned long count, void *data)
3242 +{
3243 + unsigned rg = 0, mask_bitfield;
3244 + char *end, *mask_name;
3245 + int i;
3246 + int done = 0;
3247 + int add, len;
3248 + int pos = 0;
3249 +
3250 + rg = yaffs_traceMask;
3251 +
3252 + while (!done && (pos < count)) {
3253 + done = 1;
3254 + while ((pos < count) && isspace(buf[pos])) {
3255 + pos++;
3256 + }
3257 +
3258 + switch (buf[pos]) {
3259 + case '+':
3260 + case '-':
3261 + case '=':
3262 + add = buf[pos];
3263 + pos++;
3264 + break;
3265 +
3266 + default:
3267 + add = ' ';
3268 + break;
3269 + }
3270 + mask_name = NULL;
3271 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3272 + if (end > buf + pos) {
3273 + mask_name = "numeral";
3274 + len = end - (buf + pos);
3275 + done = 0;
3276 + } else {
3277 +
3278 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3279 + len = strlen(mask_flags[i].mask_name);
3280 + if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3281 + mask_name = mask_flags[i].mask_name;
3282 + mask_bitfield = mask_flags[i].mask_bitfield;
3283 + done = 0;
3284 + break;
3285 + }
3286 + }
3287 + }
3288 +
3289 + if (mask_name != NULL) {
3290 + pos += len;
3291 + done = 0;
3292 + switch(add) {
3293 + case '-':
3294 + rg &= ~mask_bitfield;
3295 + break;
3296 + case '+':
3297 + rg |= mask_bitfield;
3298 + break;
3299 + case '=':
3300 + rg = mask_bitfield;
3301 + break;
3302 + default:
3303 + rg |= mask_bitfield;
3304 + break;
3305 + }
3306 + }
3307 + }
3308 +
3309 + yaffs_traceMask = rg;
3310 + if (rg & YAFFS_TRACE_ALWAYS) {
3311 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3312 + char flag;
3313 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3314 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3315 + }
3316 + }
3317 +
3318 + return count;
3319 +}
3320 +
3321 +/* Stuff to handle installation of file systems */
3322 +struct file_system_to_install {
3323 + struct file_system_type *fst;
3324 + int installed;
3325 +};
3326 +
3327 +static struct file_system_to_install fs_to_install[] = {
3328 +//#ifdef CONFIG_YAFFS_YAFFS1
3329 + {&yaffs_fs_type, 0},
3330 +//#endif
3331 +//#ifdef CONFIG_YAFFS_YAFFS2
3332 + {&yaffs2_fs_type, 0},
3333 +//#endif
3334 + {NULL, 0}
3335 +};
3336 +
3337 +static int __init init_yaffs_fs(void)
3338 +{
3339 + int error = 0;
3340 + struct file_system_to_install *fsinst;
3341 +
3342 + T(YAFFS_TRACE_ALWAYS,
3343 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3344 +
3345 + /* Install the proc_fs entry */
3346 + my_proc_entry = create_proc_entry("yaffs",
3347 + S_IRUGO | S_IFREG,
3348 + &proc_root);
3349 +
3350 + if (my_proc_entry) {
3351 + my_proc_entry->write_proc = yaffs_proc_write;
3352 + my_proc_entry->read_proc = yaffs_proc_read;
3353 + my_proc_entry->data = NULL;
3354 + } else {
3355 + return -ENOMEM;
3356 + }
3357 +
3358 + /* Now add the file system entries */
3359 +
3360 + fsinst = fs_to_install;
3361 +
3362 + while (fsinst->fst && !error) {
3363 + error = register_filesystem(fsinst->fst);
3364 + if (!error) {
3365 + fsinst->installed = 1;
3366 + }
3367 + fsinst++;
3368 + }
3369 +
3370 + /* Any errors? uninstall */
3371 + if (error) {
3372 + fsinst = fs_to_install;
3373 +
3374 + while (fsinst->fst) {
3375 + if (fsinst->installed) {
3376 + unregister_filesystem(fsinst->fst);
3377 + fsinst->installed = 0;
3378 + }
3379 + fsinst++;
3380 + }
3381 + }
3382 +
3383 + return error;
3384 +}
3385 +
3386 +static void __exit exit_yaffs_fs(void)
3387 +{
3388 +
3389 + struct file_system_to_install *fsinst;
3390 +
3391 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3392 + " removing. \n"));
3393 +
3394 + remove_proc_entry("yaffs", &proc_root);
3395 +
3396 + fsinst = fs_to_install;
3397 +
3398 + while (fsinst->fst) {
3399 + if (fsinst->installed) {
3400 + unregister_filesystem(fsinst->fst);
3401 + fsinst->installed = 0;
3402 + }
3403 + fsinst++;
3404 + }
3405 +
3406 +}
3407 +
3408 +module_init(init_yaffs_fs)
3409 +module_exit(exit_yaffs_fs)
3410 +
3411 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3412 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3413 +MODULE_LICENSE("GPL");
3414 diff -urN linux.old/fs/yaffs2/yaffs_guts.c linux.dev/fs/yaffs2/yaffs_guts.c
3415 --- linux.old/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3416 +++ linux.dev/fs/yaffs2/yaffs_guts.c 2006-12-14 04:21:47.000000000 +0100
3417 @@ -0,0 +1,6675 @@
3418 +/*
3419 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
3420 + *
3421 + * Copyright (C) 2002 Aleph One Ltd.
3422 + * for Toby Churchill Ltd and Brightstar Engineering
3423 + *
3424 + * Created by Charles Manning <charles@aleph1.co.uk>
3425 + *
3426 + * This program is free software; you can redistribute it and/or modify
3427 + * it under the terms of the GNU General Public License version 2 as
3428 + * published by the Free Software Foundation.
3429 + *
3430 + */
3431 +
3432 +const char *yaffs_guts_c_version =
3433 + "$Id: yaffs_guts.c,v 1.45 2006/11/14 03:07:17 charles Exp $";
3434 +
3435 +#include "yportenv.h"
3436 +
3437 +#include "yaffsinterface.h"
3438 +#include "yaffs_guts.h"
3439 +#include "yaffs_tagsvalidity.h"
3440 +
3441 +#include "yaffs_tagscompat.h"
3442 +#ifndef CONFIG_YAFFS_OWN_SORT
3443 +#include "yaffs_qsort.h"
3444 +#endif
3445 +#include "yaffs_nand.h"
3446 +
3447 +#include "yaffs_checkptrw.h"
3448 +
3449 +#include "yaffs_nand.h"
3450 +#include "yaffs_packedtags2.h"
3451 +
3452 +
3453 +#ifdef CONFIG_YAFFS_WINCE
3454 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3455 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3456 +#endif
3457 +
3458 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3459 +
3460 +#include "yaffs_ecc.h"
3461 +
3462 +
3463 +/* Robustification (if it ever comes about...) */
3464 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3465 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3466 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3467 + const __u8 * data,
3468 + const yaffs_ExtendedTags * tags);
3469 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3470 + const yaffs_ExtendedTags * tags);
3471 +
3472 +/* Other local prototypes */
3473 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3474 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3475 +
3476 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3477 +
3478 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3479 + const __u8 * buffer,
3480 + yaffs_ExtendedTags * tags,
3481 + int useReserve);
3482 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3483 + int chunkInNAND, int inScan);
3484 +
3485 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3486 + yaffs_ObjectType type);
3487 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3488 + yaffs_Object * obj);
3489 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3490 + int force, int isShrink, int shadows);
3491 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3492 +static int yaffs_CheckStructures(void);
3493 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3494 + int chunkOffset, int *limit);
3495 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3496 +
3497 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3498 +
3499 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3500 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3501 + int lineNo);
3502 +
3503 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3504 + int chunkInNAND);
3505 +
3506 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3507 +static void yaffs_DestroyObject(yaffs_Object * obj);
3508 +
3509 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3510 + int chunkInObject);
3511 +
3512 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3513 +
3514 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3515 +
3516 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3517 +
3518 +#ifdef YAFFS_PARANOID
3519 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3520 +#else
3521 +#define yaffs_CheckFileSanity(in)
3522 +#endif
3523 +
3524 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3525 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3526 +
3527 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3528 +
3529 +
3530 +
3531 +/* Function to calculate chunk and offset */
3532 +
3533 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3534 +{
3535 + if(dev->chunkShift){
3536 + /* Easy-peasy power of 2 case */
3537 + *chunk = (__u32)(addr >> dev->chunkShift);
3538 + *offset = (__u32)(addr & dev->chunkMask);
3539 + }
3540 + else if(dev->crumbsPerChunk)
3541 + {
3542 + /* Case where we're using "crumbs" */
3543 + *offset = (__u32)(addr & dev->crumbMask);
3544 + addr >>= dev->crumbShift;
3545 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3546 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3547 + }
3548 + else
3549 + YBUG();
3550 +}
3551 +
3552 +/* Function to return the number of shifts for a power of 2 greater than or equal
3553 + * to the given number
3554 + * Note we don't try to cater for all possible numbers and this does not have to
3555 + * be hellishly efficient.
3556 + */
3557 +
3558 +static __u32 ShiftsGE(__u32 x)
3559 +{
3560 + int extraBits;
3561 + int nShifts;
3562 +
3563 + nShifts = extraBits = 0;
3564 +
3565 + while(x>1){
3566 + if(x & 1) extraBits++;
3567 + x>>=1;
3568 + nShifts++;
3569 + }
3570 +
3571 + if(extraBits)
3572 + nShifts++;
3573 +
3574 + return nShifts;
3575 +}
3576 +
3577 +/* Function to return the number of shifts to get a 1 in bit 0
3578 + */
3579 +
3580 +static __u32 ShiftDiv(__u32 x)
3581 +{
3582 + int nShifts;
3583 +
3584 + nShifts = 0;
3585 +
3586 + if(!x) return 0;
3587 +
3588 + while( !(x&1)){
3589 + x>>=1;
3590 + nShifts++;
3591 + }
3592 +
3593 + return nShifts;
3594 +}
3595 +
3596 +
3597 +
3598 +/*
3599 + * Temporary buffer manipulations.
3600 + */
3601 +
3602 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3603 +{
3604 + int i, j;
3605 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3606 + if (dev->tempBuffer[i].line == 0) {
3607 + dev->tempBuffer[i].line = lineNo;
3608 + if ((i + 1) > dev->maxTemp) {
3609 + dev->maxTemp = i + 1;
3610 + for (j = 0; j <= i; j++)
3611 + dev->tempBuffer[j].maxLine =
3612 + dev->tempBuffer[j].line;
3613 + }
3614 +
3615 + return dev->tempBuffer[i].buffer;
3616 + }
3617 + }
3618 +
3619 + T(YAFFS_TRACE_BUFFERS,
3620 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3621 + lineNo));
3622 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3623 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3624 + }
3625 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3626 +
3627 + /*
3628 + * If we got here then we have to allocate an unmanaged one
3629 + * This is not good.
3630 + */
3631 +
3632 + dev->unmanagedTempAllocations++;
3633 + return YMALLOC(dev->nDataBytesPerChunk);
3634 +
3635 +}
3636 +
3637 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3638 + int lineNo)
3639 +{
3640 + int i;
3641 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3642 + if (dev->tempBuffer[i].buffer == buffer) {
3643 + dev->tempBuffer[i].line = 0;
3644 + return;
3645 + }
3646 + }
3647 +
3648 + if (buffer) {
3649 + /* assume it is an unmanaged one. */
3650 + T(YAFFS_TRACE_BUFFERS,
3651 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3652 + lineNo));
3653 + YFREE(buffer);
3654 + dev->unmanagedTempDeallocations++;
3655 + }
3656 +
3657 +}
3658 +
3659 +/*
3660 + * Determine if we have a managed buffer.
3661 + */
3662 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3663 +{
3664 + int i;
3665 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3666 + if (dev->tempBuffer[i].buffer == buffer)
3667 + return 1;
3668 +
3669 + }
3670 +
3671 + for (i = 0; i < dev->nShortOpCaches; i++) {
3672 + if( dev->srCache[i].data == buffer )
3673 + return 1;
3674 +
3675 + }
3676 +
3677 + if (buffer == dev->checkpointBuffer)
3678 + return 1;
3679 +
3680 + T(YAFFS_TRACE_ALWAYS,
3681 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3682 + return 0;
3683 +}
3684 +
3685 +/*
3686 + * Chunk bitmap manipulations
3687 + */
3688 +
3689 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3690 +{
3691 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3692 + T(YAFFS_TRACE_ERROR,
3693 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3694 + blk));
3695 + YBUG();
3696 + }
3697 + return dev->chunkBits +
3698 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3699 +}
3700 +
3701 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3702 +{
3703 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3704 +
3705 + memset(blkBits, 0, dev->chunkBitmapStride);
3706 +}
3707 +
3708 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3709 +{
3710 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3711 +
3712 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
3713 +}
3714 +
3715 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
3716 +{
3717 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3718 +
3719 + blkBits[chunk / 8] |= (1 << (chunk & 7));
3720 +}
3721 +
3722 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
3723 +{
3724 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3725 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
3726 +}
3727 +
3728 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
3729 +{
3730 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3731 + int i;
3732 + for (i = 0; i < dev->chunkBitmapStride; i++) {
3733 + if (*blkBits)
3734 + return 1;
3735 + blkBits++;
3736 + }
3737 + return 0;
3738 +}
3739 +
3740 +/*
3741 + * Simple hash function. Needs to have a reasonable spread
3742 + */
3743 +
3744 +static Y_INLINE int yaffs_HashFunction(int n)
3745 +{
3746 + n = abs(n);
3747 + return (n % YAFFS_NOBJECT_BUCKETS);
3748 +}
3749 +
3750 +/*
3751 + * Access functions to useful fake objects
3752 + */
3753 +
3754 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
3755 +{
3756 + return dev->rootDir;
3757 +}
3758 +
3759 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
3760 +{
3761 + return dev->lostNFoundDir;
3762 +}
3763 +
3764 +
3765 +/*
3766 + * Erased NAND checking functions
3767 + */
3768 +
3769 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
3770 +{
3771 + /* Horrible, slow implementation */
3772 + while (nBytes--) {
3773 + if (*buffer != 0xFF)
3774 + return 0;
3775 + buffer++;
3776 + }
3777 + return 1;
3778 +}
3779 +
3780 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3781 + int chunkInNAND)
3782 +{
3783 +
3784 + int retval = YAFFS_OK;
3785 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
3786 + yaffs_ExtendedTags tags;
3787 + int result;
3788 +
3789 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
3790 +
3791 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
3792 + retval = YAFFS_FAIL;
3793 +
3794 +
3795 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
3796 + T(YAFFS_TRACE_NANDACCESS,
3797 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
3798 + retval = YAFFS_FAIL;
3799 + }
3800 +
3801 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
3802 +
3803 + return retval;
3804 +
3805 +}
3806 +
3807 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
3808 + const __u8 * data,
3809 + yaffs_ExtendedTags * tags,
3810 + int useReserve)
3811 +{
3812 + int chunk;
3813 +
3814 + int writeOk = 0;
3815 + int erasedOk = 1;
3816 + int attempts = 0;
3817 + yaffs_BlockInfo *bi;
3818 +
3819 + yaffs_InvalidateCheckpoint(dev);
3820 +
3821 + do {
3822 + chunk = yaffs_AllocateChunk(dev, useReserve,&bi);
3823 +
3824 + if (chunk >= 0) {
3825 + /* First check this chunk is erased, if it needs checking.
3826 + * The checking policy (unless forced always on) is as follows:
3827 + * Check the first page we try to write in a block.
3828 + * - If the check passes then we don't need to check any more.
3829 + * - If the check fails, we check again...
3830 + * If the block has been erased, we don't need to check.
3831 + *
3832 + * However, if the block has been prioritised for gc, then
3833 + * we think there might be something odd about this block
3834 + * and stop using it.
3835 + *
3836 + * Rationale:
3837 + * We should only ever see chunks that have not been erased
3838 + * if there was a partially written chunk due to power loss
3839 + * This checking policy should catch that case with very
3840 + * few checks and thus save a lot of checks that are most likely not
3841 + * needed.
3842 + */
3843 +
3844 + if(bi->gcPrioritise){
3845 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
3846 + } else {
3847 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
3848 +
3849 + bi->skipErasedCheck = 0;
3850 +
3851 +#endif
3852 + if(!bi->skipErasedCheck){
3853 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
3854 + if(erasedOk && !bi->gcPrioritise)
3855 + bi->skipErasedCheck = 1;
3856 + }
3857 +
3858 + if (!erasedOk) {
3859 + T(YAFFS_TRACE_ERROR,
3860 + (TSTR
3861 + ("**>> yaffs chunk %d was not erased"
3862 + TENDSTR), chunk));
3863 + } else {
3864 + writeOk =
3865 + yaffs_WriteChunkWithTagsToNAND(dev, chunk,
3866 + data, tags);
3867 + }
3868 +
3869 + attempts++;
3870 +
3871 + if (writeOk) {
3872 + /*
3873 + * Copy the data into the robustification buffer.
3874 + * NB We do this at the end to prevent duplicates in the case of a write error.
3875 + * Todo
3876 + */
3877 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
3878 +
3879 + } else {
3880 + /* The erased check or write failed */
3881 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
3882 + }
3883 + }
3884 + }
3885 +
3886 + } while (chunk >= 0 && !writeOk);
3887 +
3888 + if (attempts > 1) {
3889 + T(YAFFS_TRACE_ERROR,
3890 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
3891 + attempts));
3892 + dev->nRetriedWrites += (attempts - 1);
3893 + }
3894 +
3895 + return chunk;
3896 +}
3897 +
3898 +/*
3899 + * Block retiring for handling a broken block.
3900 + */
3901 +
3902 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
3903 +{
3904 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
3905 +
3906 + yaffs_InvalidateCheckpoint(dev);
3907 +
3908 + yaffs_MarkBlockBad(dev, blockInNAND);
3909 +
3910 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
3911 + bi->gcPrioritise = 0;
3912 + bi->needsRetiring = 0;
3913 +
3914 + dev->nRetiredBlocks++;
3915 +}
3916 +
3917 +/*
3918 + * Functions for robustisizing TODO
3919 + *
3920 + */
3921 +
3922 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3923 + const __u8 * data,
3924 + const yaffs_ExtendedTags * tags)
3925 +{
3926 +}
3927 +
3928 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3929 + const yaffs_ExtendedTags * tags)
3930 +{
3931 +}
3932 +
3933 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
3934 +{
3935 + if(!bi->gcPrioritise){
3936 + bi->gcPrioritise = 1;
3937 + dev->hasPendingPrioritisedGCs = 1;
3938 + bi->chunkErrorStrikes ++;
3939 +
3940 + if(bi->chunkErrorStrikes > 3){
3941 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
3942 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
3943 +
3944 + }
3945 +
3946 + }
3947 +}
3948 +
3949 +static void yaffs_ReportOddballBlocks(yaffs_Device *dev)
3950 +{
3951 + int i;
3952 +
3953 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock && (yaffs_traceMask & YAFFS_TRACE_BAD_BLOCKS); i++){
3954 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
3955 + if(bi->needsRetiring || bi->gcPrioritise)
3956 + T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("yaffs block %d%s%s" TENDSTR),
3957 + i,
3958 + bi->needsRetiring ? " needs retiring" : "",
3959 + bi->gcPrioritise ? " gc prioritised" : ""));
3960 +
3961 + }
3962 +}
3963 +
3964 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
3965 +{
3966 +
3967 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
3968 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
3969 +
3970 + yaffs_HandleChunkError(dev,bi);
3971 +
3972 +
3973 + if(erasedOk ) {
3974 + /* Was an actual write failure, so mark the block for retirement */
3975 + bi->needsRetiring = 1;
3976 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
3977 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
3978 +
3979 +
3980 + }
3981 +
3982 + /* Delete the chunk */
3983 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
3984 +}
3985 +
3986 +
3987 +/*---------------- Name handling functions ------------*/
3988 +
3989 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
3990 +{
3991 + __u16 sum = 0;
3992 + __u16 i = 1;
3993 +
3994 + YUCHAR *bname = (YUCHAR *) name;
3995 + if (bname) {
3996 + while ((*bname) && (i <= YAFFS_MAX_NAME_LENGTH)) {
3997 +
3998 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
3999 + sum += yaffs_toupper(*bname) * i;
4000 +#else
4001 + sum += (*bname) * i;
4002 +#endif
4003 + i++;
4004 + bname++;
4005 + }
4006 + }
4007 + return sum;
4008 +}
4009 +
4010 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4011 +{
4012 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4013 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4014 + yaffs_strcpy(obj->shortName, name);
4015 + } else {
4016 + obj->shortName[0] = _Y('\0');
4017 + }
4018 +#endif
4019 + obj->sum = yaffs_CalcNameSum(name);
4020 +}
4021 +
4022 +/*-------------------- TNODES -------------------
4023 +
4024 + * List of spare tnodes
4025 + * The list is hooked together using the first pointer
4026 + * in the tnode.
4027 + */
4028 +
4029 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4030 + * adds them to the tnode free list.
4031 + * Don't use this function directly
4032 + */
4033 +
4034 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4035 +{
4036 + int i;
4037 + int tnodeSize;
4038 + yaffs_Tnode *newTnodes;
4039 + __u8 *mem;
4040 + yaffs_Tnode *curr;
4041 + yaffs_Tnode *next;
4042 + yaffs_TnodeList *tnl;
4043 +
4044 + if (nTnodes < 1)
4045 + return YAFFS_OK;
4046 +
4047 + /* Calculate the tnode size in bytes for variable width tnode support.
4048 + * Must be a multiple of 32-bits */
4049 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4050 +
4051 + /* make these things */
4052 +
4053 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4054 + mem = (__u8 *)newTnodes;
4055 +
4056 + if (!newTnodes) {
4057 + T(YAFFS_TRACE_ERROR,
4058 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4059 + return YAFFS_FAIL;
4060 + }
4061 +
4062 + /* Hook them into the free list */
4063 +#if 0
4064 + for (i = 0; i < nTnodes - 1; i++) {
4065 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4066 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4067 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4068 +#endif
4069 + }
4070 +
4071 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4072 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4073 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4074 +#endif
4075 + dev->freeTnodes = newTnodes;
4076 +#else
4077 + /* New hookup for wide tnodes */
4078 + for(i = 0; i < nTnodes -1; i++) {
4079 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4080 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4081 + curr->internal[0] = next;
4082 + }
4083 +
4084 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4085 + curr->internal[0] = dev->freeTnodes;
4086 + dev->freeTnodes = (yaffs_Tnode *)mem;
4087 +
4088 +#endif
4089 +
4090 +
4091 + dev->nFreeTnodes += nTnodes;
4092 + dev->nTnodesCreated += nTnodes;
4093 +
4094 + /* Now add this bunch of tnodes to a list for freeing up.
4095 + * NB If we can't add this to the management list it isn't fatal
4096 + * but it just means we can't free this bunch of tnodes later.
4097 + */
4098 +
4099 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4100 + if (!tnl) {
4101 + T(YAFFS_TRACE_ERROR,
4102 + (TSTR
4103 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4104 +
4105 + } else {
4106 + tnl->tnodes = newTnodes;
4107 + tnl->next = dev->allocatedTnodeList;
4108 + dev->allocatedTnodeList = tnl;
4109 + }
4110 +
4111 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4112 +
4113 + return YAFFS_OK;
4114 +}
4115 +
4116 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4117 +
4118 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4119 +{
4120 + yaffs_Tnode *tn = NULL;
4121 +
4122 + /* If there are none left make more */
4123 + if (!dev->freeTnodes) {
4124 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4125 + }
4126 +
4127 + if (dev->freeTnodes) {
4128 + tn = dev->freeTnodes;
4129 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4130 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4131 + /* Hoosterman, this thing looks like it isn't in the list */
4132 + T(YAFFS_TRACE_ALWAYS,
4133 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4134 + }
4135 +#endif
4136 + dev->freeTnodes = dev->freeTnodes->internal[0];
4137 + dev->nFreeTnodes--;
4138 + }
4139 +
4140 + return tn;
4141 +}
4142 +
4143 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4144 +{
4145 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4146 +
4147 + if(tn)
4148 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4149 +
4150 + return tn;
4151 +}
4152 +
4153 +/* FreeTnode frees up a tnode and puts it back on the free list */
4154 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4155 +{
4156 + if (tn) {
4157 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4158 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4159 + /* Hoosterman, this thing looks like it is already in the list */
4160 + T(YAFFS_TRACE_ALWAYS,
4161 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4162 + }
4163 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4164 +#endif
4165 + tn->internal[0] = dev->freeTnodes;
4166 + dev->freeTnodes = tn;
4167 + dev->nFreeTnodes++;
4168 + }
4169 +}
4170 +
4171 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4172 +{
4173 + /* Free the list of allocated tnodes */
4174 + yaffs_TnodeList *tmp;
4175 +
4176 + while (dev->allocatedTnodeList) {
4177 + tmp = dev->allocatedTnodeList->next;
4178 +
4179 + YFREE(dev->allocatedTnodeList->tnodes);
4180 + YFREE(dev->allocatedTnodeList);
4181 + dev->allocatedTnodeList = tmp;
4182 +
4183 + }
4184 +
4185 + dev->freeTnodes = NULL;
4186 + dev->nFreeTnodes = 0;
4187 +}
4188 +
4189 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4190 +{
4191 + dev->allocatedTnodeList = NULL;
4192 + dev->freeTnodes = NULL;
4193 + dev->nFreeTnodes = 0;
4194 + dev->nTnodesCreated = 0;
4195 +
4196 +}
4197 +
4198 +
4199 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4200 +{
4201 + __u32 *map = (__u32 *)tn;
4202 + __u32 bitInMap;
4203 + __u32 bitInWord;
4204 + __u32 wordInMap;
4205 + __u32 mask;
4206 +
4207 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4208 + val >>= dev->chunkGroupBits;
4209 +
4210 + bitInMap = pos * dev->tnodeWidth;
4211 + wordInMap = bitInMap /32;
4212 + bitInWord = bitInMap & (32 -1);
4213 +
4214 + mask = dev->tnodeMask << bitInWord;
4215 +
4216 + map[wordInMap] &= ~mask;
4217 + map[wordInMap] |= (mask & (val << bitInWord));
4218 +
4219 + if(dev->tnodeWidth > (32-bitInWord)) {
4220 + bitInWord = (32 - bitInWord);
4221 + wordInMap++;;
4222 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4223 + map[wordInMap] &= ~mask;
4224 + map[wordInMap] |= (mask & (val >> bitInWord));
4225 + }
4226 +}
4227 +
4228 +__u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
4229 +{
4230 + __u32 *map = (__u32 *)tn;
4231 + __u32 bitInMap;
4232 + __u32 bitInWord;
4233 + __u32 wordInMap;
4234 + __u32 val;
4235 +
4236 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4237 +
4238 + bitInMap = pos * dev->tnodeWidth;
4239 + wordInMap = bitInMap /32;
4240 + bitInWord = bitInMap & (32 -1);
4241 +
4242 + val = map[wordInMap] >> bitInWord;
4243 +
4244 + if(dev->tnodeWidth > (32-bitInWord)) {
4245 + bitInWord = (32 - bitInWord);
4246 + wordInMap++;;
4247 + val |= (map[wordInMap] << bitInWord);
4248 + }
4249 +
4250 + val &= dev->tnodeMask;
4251 + val <<= dev->chunkGroupBits;
4252 +
4253 + return val;
4254 +}
4255 +
4256 +/* ------------------- End of individual tnode manipulation -----------------*/
4257 +
4258 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
4259 + * The look up tree is represented by the top tnode and the number of topLevel
4260 + * in the tree. 0 means only the level 0 tnode is in the tree.
4261 + */
4262 +
4263 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
4264 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
4265 + yaffs_FileStructure * fStruct,
4266 + __u32 chunkId)
4267 +{
4268 +
4269 + yaffs_Tnode *tn = fStruct->top;
4270 + __u32 i;
4271 + int requiredTallness;
4272 + int level = fStruct->topLevel;
4273 +
4274 + /* Check sane level and chunk Id */
4275 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
4276 + return NULL;
4277 + }
4278 +
4279 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
4280 + return NULL;
4281 + }
4282 +
4283 + /* First check we're tall enough (ie enough topLevel) */
4284 +
4285 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
4286 + requiredTallness = 0;
4287 + while (i) {
4288 + i >>= YAFFS_TNODES_INTERNAL_BITS;
4289 + requiredTallness++;
4290 + }
4291 +
4292 + if (requiredTallness > fStruct->topLevel) {
4293 + /* Not tall enough, so we can't find it, return NULL. */
4294 + return NULL;
4295 + }
4296 +
4297 + /* Traverse down to level 0 */
4298 + while (level > 0 && tn) {
4299 + tn = tn->
4300 + internal[(chunkId >>
4301 + ( YAFFS_TNODES_LEVEL0_BITS +
4302 + (level - 1) *
4303 + YAFFS_TNODES_INTERNAL_BITS)
4304 + ) &
4305 + YAFFS_TNODES_INTERNAL_MASK];
4306 + level--;
4307 +
4308 + }
4309 +
4310 + return tn;
4311 +}
4312 +
4313 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
4314 + * This happens in two steps:
4315 + * 1. If the tree isn't tall enough, then make it taller.
4316 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
4317 + *
4318 + * Used when modifying the tree.
4319 + *
4320 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
4321 + * be plugged into the ttree.
4322 + */
4323 +
4324 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
4325 + yaffs_FileStructure * fStruct,
4326 + __u32 chunkId,
4327 + yaffs_Tnode *passedTn)
4328 +{
4329 +
4330 + int requiredTallness;
4331 + int i;
4332 + int l;
4333 + yaffs_Tnode *tn;
4334 +
4335 + __u32 x;
4336 +
4337 +
4338 + /* Check sane level and page Id */
4339 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
4340 + return NULL;
4341 + }
4342 +
4343 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
4344 + return NULL;
4345 + }
4346 +
4347 + /* First check we're tall enough (ie enough topLevel) */
4348 +
4349 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
4350 + requiredTallness = 0;
4351 + while (x) {
4352 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4353 + requiredTallness++;
4354 + }
4355 +
4356 +
4357 + if (requiredTallness > fStruct->topLevel) {
4358 + /* Not tall enough,gotta make the tree taller */
4359 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
4360 +
4361 + tn = yaffs_GetTnode(dev);
4362 +
4363 + if (tn) {
4364 + tn->internal[0] = fStruct->top;
4365 + fStruct->top = tn;
4366 + } else {
4367 + T(YAFFS_TRACE_ERROR,
4368 + (TSTR("yaffs: no more tnodes" TENDSTR)));
4369 + }
4370 + }
4371 +
4372 + fStruct->topLevel = requiredTallness;
4373 + }
4374 +
4375 + /* Traverse down to level 0, adding anything we need */
4376 +
4377 + l = fStruct->topLevel;
4378 + tn = fStruct->top;
4379 +
4380 + if(l > 0) {
4381 + while (l > 0 && tn) {
4382 + x = (chunkId >>
4383 + ( YAFFS_TNODES_LEVEL0_BITS +
4384 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
4385 + YAFFS_TNODES_INTERNAL_MASK;
4386 +
4387 +
4388 + if((l>1) && !tn->internal[x]){
4389 + /* Add missing non-level-zero tnode */
4390 + tn->internal[x] = yaffs_GetTnode(dev);
4391 +
4392 + } else if(l == 1) {
4393 + /* Looking from level 1 at level 0 */
4394 + if (passedTn) {
4395 + /* If we already have one, then release it.*/
4396 + if(tn->internal[x])
4397 + yaffs_FreeTnode(dev,tn->internal[x]);
4398 + tn->internal[x] = passedTn;
4399 +
4400 + } else if(!tn->internal[x]) {
4401 + /* Don't have one, none passed in */
4402 + tn->internal[x] = yaffs_GetTnode(dev);
4403 + }
4404 + }
4405 +
4406 + tn = tn->internal[x];
4407 + l--;
4408 + }
4409 + } else {
4410 + /* top is level 0 */
4411 + if(passedTn) {
4412 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4413 + yaffs_FreeTnode(dev,passedTn);
4414 + }
4415 + }
4416 +
4417 + return tn;
4418 +}
4419 +
4420 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
4421 + yaffs_ExtendedTags * tags, int objectId,
4422 + int chunkInInode)
4423 +{
4424 + int j;
4425 +
4426 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
4427 + if (yaffs_CheckChunkBit
4428 + (dev, theChunk / dev->nChunksPerBlock,
4429 + theChunk % dev->nChunksPerBlock)) {
4430 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
4431 + tags);
4432 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
4433 + /* found it; */
4434 + return theChunk;
4435 +
4436 + }
4437 + }
4438 + theChunk++;
4439 + }
4440 + return -1;
4441 +}
4442 +
4443 +
4444 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
4445 + * chunks and tnodes in the file
4446 + * Returns 1 if the tree was deleted.
4447 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
4448 + */
4449 +
4450 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
4451 + int chunkOffset, int *limit)
4452 +{
4453 + int i;
4454 + int chunkInInode;
4455 + int theChunk;
4456 + yaffs_ExtendedTags tags;
4457 + int foundChunk;
4458 + yaffs_Device *dev = in->myDev;
4459 +
4460 + int allDone = 1;
4461 +
4462 + if (tn) {
4463 + if (level > 0) {
4464 +
4465 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
4466 + i--) {
4467 + if (tn->internal[i]) {
4468 + if (limit && (*limit) < 0) {
4469 + allDone = 0;
4470 + } else {
4471 + allDone =
4472 + yaffs_DeleteWorker(in,
4473 + tn->
4474 + internal
4475 + [i],
4476 + level -
4477 + 1,
4478 + (chunkOffset
4479 + <<
4480 + YAFFS_TNODES_INTERNAL_BITS)
4481 + + i,
4482 + limit);
4483 + }
4484 + if (allDone) {
4485 + yaffs_FreeTnode(dev,
4486 + tn->
4487 + internal[i]);
4488 + tn->internal[i] = NULL;
4489 + }
4490 + }
4491 +
4492 + }
4493 + return (allDone) ? 1 : 0;
4494 + } else if (level == 0) {
4495 + int hitLimit = 0;
4496 +
4497 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
4498 + i--) {
4499 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4500 + if (theChunk) {
4501 +
4502 + chunkInInode =
4503 + (chunkOffset <<
4504 + YAFFS_TNODES_LEVEL0_BITS) + i;
4505 +
4506 + foundChunk =
4507 + yaffs_FindChunkInGroup(dev,
4508 + theChunk,
4509 + &tags,
4510 + in->objectId,
4511 + chunkInInode);
4512 +
4513 + if (foundChunk > 0) {
4514 + yaffs_DeleteChunk(dev,
4515 + foundChunk, 1,
4516 + __LINE__);
4517 + in->nDataChunks--;
4518 + if (limit) {
4519 + *limit = *limit - 1;
4520 + if (*limit <= 0) {
4521 + hitLimit = 1;
4522 + }
4523 + }
4524 +
4525 + }
4526 +
4527 + yaffs_PutLevel0Tnode(dev,tn,i,0);
4528 + }
4529 +
4530 + }
4531 + return (i < 0) ? 1 : 0;
4532 +
4533 + }
4534 +
4535 + }
4536 +
4537 + return 1;
4538 +
4539 +}
4540 +
4541 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
4542 +{
4543 +
4544 + yaffs_BlockInfo *theBlock;
4545 +
4546 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
4547 +
4548 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
4549 + if (theBlock) {
4550 + theBlock->softDeletions++;
4551 + dev->nFreeChunks++;
4552 + }
4553 +}
4554 +
4555 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
4556 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
4557 + * of the tnode.
4558 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
4559 + */
4560 +
4561 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
4562 + __u32 level, int chunkOffset)
4563 +{
4564 + int i;
4565 + int theChunk;
4566 + int allDone = 1;
4567 + yaffs_Device *dev = in->myDev;
4568 +
4569 + if (tn) {
4570 + if (level > 0) {
4571 +
4572 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
4573 + i--) {
4574 + if (tn->internal[i]) {
4575 + allDone =
4576 + yaffs_SoftDeleteWorker(in,
4577 + tn->
4578 + internal[i],
4579 + level - 1,
4580 + (chunkOffset
4581 + <<
4582 + YAFFS_TNODES_INTERNAL_BITS)
4583 + + i);
4584 + if (allDone) {
4585 + yaffs_FreeTnode(dev,
4586 + tn->
4587 + internal[i]);
4588 + tn->internal[i] = NULL;
4589 + } else {
4590 + /* Hoosterman... how could this happen? */
4591 + }
4592 + }
4593 + }
4594 + return (allDone) ? 1 : 0;
4595 + } else if (level == 0) {
4596 +
4597 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
4598 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4599 + if (theChunk) {
4600 + /* Note this does not find the real chunk, only the chunk group.
4601 + * We make an assumption that a chunk group is not larger than
4602 + * a block.
4603 + */
4604 + yaffs_SoftDeleteChunk(dev, theChunk);
4605 + yaffs_PutLevel0Tnode(dev,tn,i,0);
4606 + }
4607 +
4608 + }
4609 + return 1;
4610 +
4611 + }
4612 +
4613 + }
4614 +
4615 + return 1;
4616 +
4617 +}
4618 +
4619 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
4620 +{
4621 + if (obj->deleted &&
4622 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
4623 + if (obj->nDataChunks <= 0) {
4624 + /* Empty file with no duplicate object headers, just delete it immediately */
4625 + yaffs_FreeTnode(obj->myDev,
4626 + obj->variant.fileVariant.top);
4627 + obj->variant.fileVariant.top = NULL;
4628 + T(YAFFS_TRACE_TRACING,
4629 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
4630 + obj->objectId));
4631 + yaffs_DoGenericObjectDeletion(obj);
4632 + } else {
4633 + yaffs_SoftDeleteWorker(obj,
4634 + obj->variant.fileVariant.top,
4635 + obj->variant.fileVariant.
4636 + topLevel, 0);
4637 + obj->softDeleted = 1;
4638 + }
4639 + }
4640 +}
4641 +
4642 +/* Pruning removes any part of the file structure tree that is beyond the
4643 + * bounds of the file (ie that does not point to chunks).
4644 + *
4645 + * A file should only get pruned when its size is reduced.
4646 + *
4647 + * Before pruning, the chunks must be pulled from the tree and the
4648 + * level 0 tnode entries must be zeroed out.
4649 + * Could also use this for file deletion, but that's probably better handled
4650 + * by a special case.
4651 + */
4652 +
4653 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
4654 + __u32 level, int del0)
4655 +{
4656 + int i;
4657 + int hasData;
4658 +
4659 + if (tn) {
4660 + hasData = 0;
4661 +
4662 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
4663 + if (tn->internal[i] && level > 0) {
4664 + tn->internal[i] =
4665 + yaffs_PruneWorker(dev, tn->internal[i],
4666 + level - 1,
4667 + (i == 0) ? del0 : 1);
4668 + }
4669 +
4670 + if (tn->internal[i]) {
4671 + hasData++;
4672 + }
4673 + }
4674 +
4675 + if (hasData == 0 && del0) {
4676 + /* Free and return NULL */
4677 +
4678 + yaffs_FreeTnode(dev, tn);
4679 + tn = NULL;
4680 + }
4681 +
4682 + }
4683 +
4684 + return tn;
4685 +
4686 +}
4687 +
4688 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
4689 + yaffs_FileStructure * fStruct)
4690 +{
4691 + int i;
4692 + int hasData;
4693 + int done = 0;
4694 + yaffs_Tnode *tn;
4695 +
4696 + if (fStruct->topLevel > 0) {
4697 + fStruct->top =
4698 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
4699 +
4700 + /* Now we have a tree with all the non-zero branches NULL but the height
4701 + * is the same as it was.
4702 + * Let's see if we can trim internal tnodes to shorten the tree.
4703 + * We can do this if only the 0th element in the tnode is in use
4704 + * (ie all the non-zero are NULL)
4705 + */
4706 +
4707 + while (fStruct->topLevel && !done) {
4708 + tn = fStruct->top;
4709 +
4710 + hasData = 0;
4711 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
4712 + if (tn->internal[i]) {
4713 + hasData++;
4714 + }
4715 + }
4716 +
4717 + if (!hasData) {
4718 + fStruct->top = tn->internal[0];
4719 + fStruct->topLevel--;
4720 + yaffs_FreeTnode(dev, tn);
4721 + } else {
4722 + done = 1;
4723 + }
4724 + }
4725 + }
4726 +
4727 + return YAFFS_OK;
4728 +}
4729 +
4730 +/*-------------------- End of File Structure functions.-------------------*/
4731 +
4732 +/* yaffs_CreateFreeObjects creates a bunch more objects and
4733 + * adds them to the object free list.
4734 + */
4735 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
4736 +{
4737 + int i;
4738 + yaffs_Object *newObjects;
4739 + yaffs_ObjectList *list;
4740 +
4741 + if (nObjects < 1)
4742 + return YAFFS_OK;
4743 +
4744 + /* make these things */
4745 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
4746 +
4747 + if (!newObjects) {
4748 + T(YAFFS_TRACE_ALLOCATE,
4749 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
4750 + return YAFFS_FAIL;
4751 + }
4752 +
4753 + /* Hook them into the free list */
4754 + for (i = 0; i < nObjects - 1; i++) {
4755 + newObjects[i].siblings.next =
4756 + (struct list_head *)(&newObjects[i + 1]);
4757 + }
4758 +
4759 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
4760 + dev->freeObjects = newObjects;
4761 + dev->nFreeObjects += nObjects;
4762 + dev->nObjectsCreated += nObjects;
4763 +
4764 + /* Now add this bunch of Objects to a list for freeing up. */
4765 +
4766 + list = YMALLOC(sizeof(yaffs_ObjectList));
4767 + if (!list) {
4768 + T(YAFFS_TRACE_ALLOCATE,
4769 + (TSTR("Could not add objects to management list" TENDSTR)));
4770 + } else {
4771 + list->objects = newObjects;
4772 + list->next = dev->allocatedObjectList;
4773 + dev->allocatedObjectList = list;
4774 + }
4775 +
4776 + return YAFFS_OK;
4777 +}
4778 +
4779 +
4780 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
4781 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
4782 +{
4783 + yaffs_Object *tn = NULL;
4784 +
4785 + /* If there are none left make more */
4786 + if (!dev->freeObjects) {
4787 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
4788 + }
4789 +
4790 + if (dev->freeObjects) {
4791 + tn = dev->freeObjects;
4792 + dev->freeObjects =
4793 + (yaffs_Object *) (dev->freeObjects->siblings.next);
4794 + dev->nFreeObjects--;
4795 +
4796 + /* Now sweeten it up... */
4797 +
4798 + memset(tn, 0, sizeof(yaffs_Object));
4799 + tn->myDev = dev;
4800 + tn->chunkId = -1;
4801 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
4802 + INIT_LIST_HEAD(&(tn->hardLinks));
4803 + INIT_LIST_HEAD(&(tn->hashLink));
4804 + INIT_LIST_HEAD(&tn->siblings);
4805 +
4806 + /* Add it to the lost and found directory.
4807 + * NB Can't put root or lostNFound in lostNFound so
4808 + * check if lostNFound exists first
4809 + */
4810 + if (dev->lostNFoundDir) {
4811 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
4812 + }
4813 + }
4814 +
4815 + return tn;
4816 +}
4817 +
4818 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
4819 + __u32 mode)
4820 +{
4821 +
4822 + yaffs_Object *obj =
4823 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
4824 + if (obj) {
4825 + obj->fake = 1; /* it is fake so it has no NAND presence... */
4826 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
4827 + obj->unlinkAllowed = 0; /* ... or unlink it */
4828 + obj->deleted = 0;
4829 + obj->unlinked = 0;
4830 + obj->yst_mode = mode;
4831 + obj->myDev = dev;
4832 + obj->chunkId = 0; /* Not a valid chunk. */
4833 + }
4834 +
4835 + return obj;
4836 +
4837 +}
4838 +
4839 +static void yaffs_UnhashObject(yaffs_Object * tn)
4840 +{
4841 + int bucket;
4842 + yaffs_Device *dev = tn->myDev;
4843 +
4844 + /* If it is still linked into the bucket list, free from the list */
4845 + if (!list_empty(&tn->hashLink)) {
4846 + list_del_init(&tn->hashLink);
4847 + bucket = yaffs_HashFunction(tn->objectId);
4848 + dev->objectBucket[bucket].count--;
4849 + }
4850 +
4851 +}
4852 +
4853 +/* FreeObject frees up a Object and puts it back on the free list */
4854 +static void yaffs_FreeObject(yaffs_Object * tn)
4855 +{
4856 +
4857 + yaffs_Device *dev = tn->myDev;
4858 +
4859 +#ifdef __KERNEL__
4860 + if (tn->myInode) {
4861 + /* We're still hooked up to a cached inode.
4862 + * Don't delete now, but mark for later deletion
4863 + */
4864 + tn->deferedFree = 1;
4865 + return;
4866 + }
4867 +#endif
4868 +
4869 + yaffs_UnhashObject(tn);
4870 +
4871 + /* Link into the free list. */
4872 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
4873 + dev->freeObjects = tn;
4874 + dev->nFreeObjects++;
4875 +}
4876 +
4877 +#ifdef __KERNEL__
4878 +
4879 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
4880 +{
4881 + if (obj->deferedFree) {
4882 + yaffs_FreeObject(obj);
4883 + }
4884 +}
4885 +
4886 +#endif
4887 +
4888 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
4889 +{
4890 + /* Free the list of allocated Objects */
4891 +
4892 + yaffs_ObjectList *tmp;
4893 +
4894 + while (dev->allocatedObjectList) {
4895 + tmp = dev->allocatedObjectList->next;
4896 + YFREE(dev->allocatedObjectList->objects);
4897 + YFREE(dev->allocatedObjectList);
4898 +
4899 + dev->allocatedObjectList = tmp;
4900 + }
4901 +
4902 + dev->freeObjects = NULL;
4903 + dev->nFreeObjects = 0;
4904 +}
4905 +
4906 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
4907 +{
4908 + int i;
4909 +
4910 + dev->allocatedObjectList = NULL;
4911 + dev->freeObjects = NULL;
4912 + dev->nFreeObjects = 0;
4913 +
4914 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4915 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
4916 + dev->objectBucket[i].count = 0;
4917 + }
4918 +
4919 +}
4920 +
4921 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
4922 +{
4923 + static int x = 0;
4924 + int i;
4925 + int l = 999;
4926 + int lowest = 999999;
4927 +
4928 + /* First let's see if we can find one that's empty. */
4929 +
4930 + for (i = 0; i < 10 && lowest > 0; i++) {
4931 + x++;
4932 + x %= YAFFS_NOBJECT_BUCKETS;
4933 + if (dev->objectBucket[x].count < lowest) {
4934 + lowest = dev->objectBucket[x].count;
4935 + l = x;
4936 + }
4937 +
4938 + }
4939 +
4940 + /* If we didn't find an empty list, then try
4941 + * looking a bit further for a short one
4942 + */
4943 +
4944 + for (i = 0; i < 10 && lowest > 3; i++) {
4945 + x++;
4946 + x %= YAFFS_NOBJECT_BUCKETS;
4947 + if (dev->objectBucket[x].count < lowest) {
4948 + lowest = dev->objectBucket[x].count;
4949 + l = x;
4950 + }
4951 +
4952 + }
4953 +
4954 + return l;
4955 +}
4956 +
4957 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
4958 +{
4959 + int bucket = yaffs_FindNiceObjectBucket(dev);
4960 +
4961 + /* Now find an object value that has not already been taken
4962 + * by scanning the list.
4963 + */
4964 +
4965 + int found = 0;
4966 + struct list_head *i;
4967 +
4968 + __u32 n = (__u32) bucket;
4969 +
4970 + /* yaffs_CheckObjectHashSanity(); */
4971 +
4972 + while (!found) {
4973 + found = 1;
4974 + n += YAFFS_NOBJECT_BUCKETS;
4975 + if (1 || dev->objectBucket[bucket].count > 0) {
4976 + list_for_each(i, &dev->objectBucket[bucket].list) {
4977 + /* If there is already one in the list */
4978 + if (i
4979 + && list_entry(i, yaffs_Object,
4980 + hashLink)->objectId == n) {
4981 + found = 0;
4982 + }
4983 + }
4984 + }
4985 + }
4986 +
4987 +
4988 + return n;
4989 +}
4990 +
4991 +static void yaffs_HashObject(yaffs_Object * in)
4992 +{
4993 + int bucket = yaffs_HashFunction(in->objectId);
4994 + yaffs_Device *dev = in->myDev;
4995 +
4996 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
4997 + dev->objectBucket[bucket].count++;
4998 +
4999 +}
5000 +
5001 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5002 +{
5003 + int bucket = yaffs_HashFunction(number);
5004 + struct list_head *i;
5005 + yaffs_Object *in;
5006 +
5007 + list_for_each(i, &dev->objectBucket[bucket].list) {
5008 + /* Look if it is in the list */
5009 + if (i) {
5010 + in = list_entry(i, yaffs_Object, hashLink);
5011 + if (in->objectId == number) {
5012 +#ifdef __KERNEL__
5013 + /* Don't tell the VFS about this one if it is defered free */
5014 + if (in->deferedFree)
5015 + return NULL;
5016 +#endif
5017 +
5018 + return in;
5019 + }
5020 + }
5021 + }
5022 +
5023 + return NULL;
5024 +}
5025 +
5026 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5027 + yaffs_ObjectType type)
5028 +{
5029 +
5030 + yaffs_Object *theObject;
5031 +
5032 + if (number < 0) {
5033 + number = yaffs_CreateNewObjectNumber(dev);
5034 + }
5035 +
5036 + theObject = yaffs_AllocateEmptyObject(dev);
5037 +
5038 + if (theObject) {
5039 + theObject->fake = 0;
5040 + theObject->renameAllowed = 1;
5041 + theObject->unlinkAllowed = 1;
5042 + theObject->objectId = number;
5043 + yaffs_HashObject(theObject);
5044 + theObject->variantType = type;
5045 +#ifdef CONFIG_YAFFS_WINCE
5046 + yfsd_WinFileTimeNow(theObject->win_atime);
5047 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5048 + theObject->win_atime[0];
5049 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5050 + theObject->win_atime[1];
5051 +
5052 +#else
5053 +
5054 + theObject->yst_atime = theObject->yst_mtime =
5055 + theObject->yst_ctime = Y_CURRENT_TIME;
5056 +#endif
5057 + switch (type) {
5058 + case YAFFS_OBJECT_TYPE_FILE:
5059 + theObject->variant.fileVariant.fileSize = 0;
5060 + theObject->variant.fileVariant.scannedFileSize = 0;
5061 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5062 + theObject->variant.fileVariant.topLevel = 0;
5063 + theObject->variant.fileVariant.top =
5064 + yaffs_GetTnode(dev);
5065 + break;
5066 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5067 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5068 + children);
5069 + break;
5070 + case YAFFS_OBJECT_TYPE_SYMLINK:
5071 + case YAFFS_OBJECT_TYPE_HARDLINK:
5072 + case YAFFS_OBJECT_TYPE_SPECIAL:
5073 + /* No action required */
5074 + break;
5075 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5076 + /* todo this should not happen */
5077 + break;
5078 + }
5079 + }
5080 +
5081 + return theObject;
5082 +}
5083 +
5084 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5085 + int number,
5086 + yaffs_ObjectType type)
5087 +{
5088 + yaffs_Object *theObject = NULL;
5089 +
5090 + if (number > 0) {
5091 + theObject = yaffs_FindObjectByNumber(dev, number);
5092 + }
5093 +
5094 + if (!theObject) {
5095 + theObject = yaffs_CreateNewObject(dev, number, type);
5096 + }
5097 +
5098 + return theObject;
5099 +
5100 +}
5101 +
5102 +
5103 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5104 +{
5105 + YCHAR *newStr = NULL;
5106 +
5107 + if (str && *str) {
5108 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5109 + yaffs_strcpy(newStr, str);
5110 + }
5111 +
5112 + return newStr;
5113 +
5114 +}
5115 +
5116 +/*
5117 + * Mknod (create) a new object.
5118 + * equivalentObject only has meaning for a hard link;
5119 + * aliasString only has meaning for a sumlink.
5120 + * rdev only has meaning for devices (a subset of special objects)
5121 + */
5122 +
5123 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5124 + yaffs_Object * parent,
5125 + const YCHAR * name,
5126 + __u32 mode,
5127 + __u32 uid,
5128 + __u32 gid,
5129 + yaffs_Object * equivalentObject,
5130 + const YCHAR * aliasString, __u32 rdev)
5131 +{
5132 + yaffs_Object *in;
5133 +
5134 + yaffs_Device *dev = parent->myDev;
5135 +
5136 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5137 + if (yaffs_FindObjectByName(parent, name)) {
5138 + return NULL;
5139 + }
5140 +
5141 + in = yaffs_CreateNewObject(dev, -1, type);
5142 +
5143 + if (in) {
5144 + in->chunkId = -1;
5145 + in->valid = 1;
5146 + in->variantType = type;
5147 +
5148 + in->yst_mode = mode;
5149 +
5150 +#ifdef CONFIG_YAFFS_WINCE
5151 + yfsd_WinFileTimeNow(in->win_atime);
5152 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5153 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5154 +
5155 +#else
5156 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5157 +
5158 + in->yst_rdev = rdev;
5159 + in->yst_uid = uid;
5160 + in->yst_gid = gid;
5161 +#endif
5162 + in->nDataChunks = 0;
5163 +
5164 + yaffs_SetObjectName(in, name);
5165 + in->dirty = 1;
5166 +
5167 + yaffs_AddObjectToDirectory(parent, in);
5168 +
5169 + in->myDev = parent->myDev;
5170 +
5171 + switch (type) {
5172 + case YAFFS_OBJECT_TYPE_SYMLINK:
5173 + in->variant.symLinkVariant.alias =
5174 + yaffs_CloneString(aliasString);
5175 + break;
5176 + case YAFFS_OBJECT_TYPE_HARDLINK:
5177 + in->variant.hardLinkVariant.equivalentObject =
5178 + equivalentObject;
5179 + in->variant.hardLinkVariant.equivalentObjectId =
5180 + equivalentObject->objectId;
5181 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5182 + break;
5183 + case YAFFS_OBJECT_TYPE_FILE:
5184 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5185 + case YAFFS_OBJECT_TYPE_SPECIAL:
5186 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5187 + /* do nothing */
5188 + break;
5189 + }
5190 +
5191 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5192 + /* Could not create the object header, fail the creation */
5193 + yaffs_DestroyObject(in);
5194 + in = NULL;
5195 + }
5196 +
5197 + }
5198 +
5199 + return in;
5200 +}
5201 +
5202 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5203 + __u32 mode, __u32 uid, __u32 gid)
5204 +{
5205 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
5206 + uid, gid, NULL, NULL, 0);
5207 +}
5208 +
5209 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
5210 + __u32 mode, __u32 uid, __u32 gid)
5211 +{
5212 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
5213 + mode, uid, gid, NULL, NULL, 0);
5214 +}
5215 +
5216 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
5217 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
5218 +{
5219 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
5220 + uid, gid, NULL, NULL, rdev);
5221 +}
5222 +
5223 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
5224 + __u32 mode, __u32 uid, __u32 gid,
5225 + const YCHAR * alias)
5226 +{
5227 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
5228 + uid, gid, NULL, alias, 0);
5229 +}
5230 +
5231 +/* yaffs_Link returns the object id of the equivalent object.*/
5232 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
5233 + yaffs_Object * equivalentObject)
5234 +{
5235 + /* Get the real object in case we were fed a hard link as an equivalent object */
5236 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
5237 +
5238 + if (yaffs_MknodObject
5239 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
5240 + equivalentObject, NULL, 0)) {
5241 + return equivalentObject;
5242 + } else {
5243 + return NULL;
5244 + }
5245 +
5246 +}
5247 +
5248 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
5249 + const YCHAR * newName, int force, int shadows)
5250 +{
5251 + int unlinkOp;
5252 + int deleteOp;
5253 +
5254 + yaffs_Object *existingTarget;
5255 +
5256 + if (newDir == NULL) {
5257 + newDir = obj->parent; /* use the old directory */
5258 + }
5259 +
5260 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5261 + T(YAFFS_TRACE_ALWAYS,
5262 + (TSTR
5263 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
5264 + TENDSTR)));
5265 + YBUG();
5266 + }
5267 +
5268 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
5269 + if (obj->myDev->isYaffs2) {
5270 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
5271 + } else {
5272 + unlinkOp = (newDir == obj->myDev->unlinkedDir
5273 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
5274 + }
5275 +
5276 + deleteOp = (newDir == obj->myDev->deletedDir);
5277 +
5278 + existingTarget = yaffs_FindObjectByName(newDir, newName);
5279 +
5280 + /* If the object is a file going into the unlinked directory,
5281 + * then it is OK to just stuff it in since duplicate names are allowed.
5282 + * else only proceed if the new name does not exist and if we're putting
5283 + * it into a directory.
5284 + */
5285 + if ((unlinkOp ||
5286 + deleteOp ||
5287 + force ||
5288 + (shadows > 0) ||
5289 + !existingTarget) &&
5290 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
5291 + yaffs_SetObjectName(obj, newName);
5292 + obj->dirty = 1;
5293 +
5294 + yaffs_AddObjectToDirectory(newDir, obj);
5295 +
5296 + if (unlinkOp)
5297 + obj->unlinked = 1;
5298 +
5299 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
5300 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
5301 + return YAFFS_OK;
5302 + }
5303 +
5304 + return YAFFS_FAIL;
5305 +}
5306 +
5307 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
5308 + yaffs_Object * newDir, const YCHAR * newName)
5309 +{
5310 + yaffs_Object *obj;
5311 + yaffs_Object *existingTarget;
5312 + int force = 0;
5313 +
5314 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
5315 + /* Special case for case insemsitive systems (eg. WinCE).
5316 + * While look-up is case insensitive, the name isn't.
5317 + * Therefore we might want to change x.txt to X.txt
5318 + */
5319 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
5320 + force = 1;
5321 + }
5322 +#endif
5323 +
5324 + obj = yaffs_FindObjectByName(oldDir, oldName);
5325 + /* Check new name to long. */
5326 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
5327 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
5328 + /* ENAMETOOLONG */
5329 + return YAFFS_FAIL;
5330 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
5331 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
5332 + /* ENAMETOOLONG */
5333 + return YAFFS_FAIL;
5334 +
5335 + if (obj && obj->renameAllowed) {
5336 +
5337 + /* Now do the handling for an existing target, if there is one */
5338 +
5339 + existingTarget = yaffs_FindObjectByName(newDir, newName);
5340 + if (existingTarget &&
5341 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
5342 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
5343 + /* There is a target that is a non-empty directory, so we fail */
5344 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
5345 + } else if (existingTarget && existingTarget != obj) {
5346 + /* Nuke the target first, using shadowing,
5347 + * but only if it isn't the same object
5348 + */
5349 + yaffs_ChangeObjectName(obj, newDir, newName, force,
5350 + existingTarget->objectId);
5351 + yaffs_UnlinkObject(existingTarget);
5352 + }
5353 +
5354 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
5355 + }
5356 + return YAFFS_FAIL;
5357 +}
5358 +
5359 +/*------------------------- Block Management and Page Allocation ----------------*/
5360 +
5361 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
5362 +{
5363 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
5364 +
5365 + dev->allocationBlock = -1; /* force it to get a new one */
5366 +
5367 + /* Todo we're assuming the malloc will pass. */
5368 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
5369 + if(!dev->blockInfo){
5370 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
5371 + dev->blockInfoAlt = 1;
5372 + }
5373 + else
5374 + dev->blockInfoAlt = 0;
5375 +
5376 + /* Set up dynamic blockinfo stuff. */
5377 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
5378 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
5379 + if(!dev->chunkBits){
5380 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
5381 + dev->chunkBitsAlt = 1;
5382 + }
5383 + else
5384 + dev->chunkBitsAlt = 0;
5385 +
5386 + if (dev->blockInfo && dev->chunkBits) {
5387 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
5388 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
5389 + return YAFFS_OK;
5390 + }
5391 +
5392 + return YAFFS_FAIL;
5393 +
5394 +}
5395 +
5396 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
5397 +{
5398 + if(dev->blockInfoAlt)
5399 + YFREE_ALT(dev->blockInfo);
5400 + else
5401 + YFREE(dev->blockInfo);
5402 + dev->blockInfoAlt = 0;
5403 +
5404 + dev->blockInfo = NULL;
5405 +
5406 + if(dev->chunkBitsAlt)
5407 + YFREE_ALT(dev->chunkBits);
5408 + else
5409 + YFREE(dev->chunkBits);
5410 + dev->chunkBitsAlt = 0;
5411 + dev->chunkBits = NULL;
5412 +}
5413 +
5414 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
5415 + yaffs_BlockInfo * bi)
5416 +{
5417 + int i;
5418 + __u32 seq;
5419 + yaffs_BlockInfo *b;
5420 +
5421 + if (!dev->isYaffs2)
5422 + return 1; /* disqualification only applies to yaffs2. */
5423 +
5424 + if (!bi->hasShrinkHeader)
5425 + return 1; /* can gc */
5426 +
5427 + /* Find the oldest dirty sequence number if we don't know it and save it
5428 + * so we don't have to keep recomputing it.
5429 + */
5430 + if (!dev->oldestDirtySequence) {
5431 + seq = dev->sequenceNumber;
5432 +
5433 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
5434 + i++) {
5435 + b = yaffs_GetBlockInfo(dev, i);
5436 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
5437 + (b->pagesInUse - b->softDeletions) <
5438 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
5439 + seq = b->sequenceNumber;
5440 + }
5441 + }
5442 + dev->oldestDirtySequence = seq;
5443 + }
5444 +
5445 + /* Can't do gc of this block if there are any blocks older than this one that have
5446 + * discarded pages.
5447 + */
5448 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
5449 +
5450 +}
5451 +
5452 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
5453 + * for garbage collection.
5454 + */
5455 +
5456 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
5457 + int aggressive)
5458 +{
5459 +
5460 + int b = dev->currentDirtyChecker;
5461 +
5462 + int i;
5463 + int iterations;
5464 + int dirtiest = -1;
5465 + int pagesInUse;
5466 + int prioritised=0;
5467 + yaffs_BlockInfo *bi;
5468 + static int nonAggressiveSkip = 0;
5469 + int pendingPrioritisedExist = 0;
5470 +
5471 + /* First let's see if we need to grab a prioritised block */
5472 + if(dev->hasPendingPrioritisedGCs){
5473 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
5474 +
5475 + bi = yaffs_GetBlockInfo(dev, i);
5476 + if(bi->gcPrioritise) {
5477 + pendingPrioritisedExist = 1;
5478 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
5479 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
5480 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
5481 + dirtiest = i;
5482 + prioritised = 1;
5483 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
5484 + }
5485 + }
5486 + }
5487 +
5488 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
5489 + dev->hasPendingPrioritisedGCs = 0;
5490 + }
5491 +
5492 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
5493 + * search harder.
5494 + * else (we're doing a leasurely gc), then we only bother to do this if the
5495 + * block has only a few pages in use.
5496 + */
5497 +
5498 + nonAggressiveSkip--;
5499 +
5500 + if (!aggressive && (nonAggressiveSkip > 0)) {
5501 + return -1;
5502 + }
5503 +
5504 + if(!prioritised)
5505 + pagesInUse =
5506 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
5507 +
5508 + if (aggressive) {
5509 + iterations =
5510 + dev->internalEndBlock - dev->internalStartBlock + 1;
5511 + } else {
5512 + iterations =
5513 + dev->internalEndBlock - dev->internalStartBlock + 1;
5514 + iterations = iterations / 16;
5515 + if (iterations > 200) {
5516 + iterations = 200;
5517 + }
5518 + }
5519 +
5520 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
5521 + b++;
5522 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
5523 + b = dev->internalStartBlock;
5524 + }
5525 +
5526 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
5527 + T(YAFFS_TRACE_ERROR,
5528 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
5529 + YBUG();
5530 + }
5531 +
5532 + bi = yaffs_GetBlockInfo(dev, b);
5533 +
5534 +#if 0
5535 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
5536 + dirtiest = b;
5537 + pagesInUse = 0;
5538 + }
5539 + else
5540 +#endif
5541 +
5542 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
5543 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
5544 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
5545 + dirtiest = b;
5546 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
5547 + }
5548 + }
5549 +
5550 + dev->currentDirtyChecker = b;
5551 +
5552 + if (dirtiest > 0) {
5553 + T(YAFFS_TRACE_GC,
5554 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
5555 + dev->nChunksPerBlock - pagesInUse,prioritised));
5556 + }
5557 +
5558 + dev->oldestDirtySequence = 0;
5559 +
5560 + if (dirtiest > 0) {
5561 + nonAggressiveSkip = 4;
5562 + }
5563 +
5564 + return dirtiest;
5565 +}
5566 +
5567 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
5568 +{
5569 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
5570 +
5571 + int erasedOk = 0;
5572 +
5573 + /* If the block is still healthy erase it and mark as clean.
5574 + * If the block has had a data failure, then retire it.
5575 + */
5576 +
5577 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
5578 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
5579 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
5580 +
5581 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
5582 +
5583 + if (!bi->needsRetiring) {
5584 + yaffs_InvalidateCheckpoint(dev);
5585 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
5586 + if (!erasedOk) {
5587 + dev->nErasureFailures++;
5588 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5589 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
5590 + }
5591 + }
5592 +
5593 + if (erasedOk && (yaffs_traceMask & YAFFS_TRACE_ERASE)) {
5594 + int i;
5595 + for (i = 0; i < dev->nChunksPerBlock; i++) {
5596 + if (!yaffs_CheckChunkErased
5597 + (dev, blockNo * dev->nChunksPerBlock + i)) {
5598 + T(YAFFS_TRACE_ERROR,
5599 + (TSTR
5600 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
5601 + TENDSTR), blockNo, i));
5602 + }
5603 + }
5604 + }
5605 +
5606 + if (erasedOk) {
5607 + /* Clean it up... */
5608 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
5609 + dev->nErasedBlocks++;
5610 + bi->pagesInUse = 0;
5611 + bi->softDeletions = 0;
5612 + bi->hasShrinkHeader = 0;
5613 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
5614 + bi->gcPrioritise = 0;
5615 + yaffs_ClearChunkBits(dev, blockNo);
5616 +
5617 + T(YAFFS_TRACE_ERASE,
5618 + (TSTR("Erased block %d" TENDSTR), blockNo));
5619 + } else {
5620 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
5621 +
5622 + yaffs_RetireBlock(dev, blockNo);
5623 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5624 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
5625 + }
5626 +}
5627 +
5628 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
5629 +{
5630 + int i;
5631 +
5632 + yaffs_BlockInfo *bi;
5633 +
5634 + if (dev->nErasedBlocks < 1) {
5635 + /* Hoosterman we've got a problem.
5636 + * Can't get space to gc
5637 + */
5638 + T(YAFFS_TRACE_ERROR,
5639 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
5640 +
5641 + return -1;
5642 + }
5643 +
5644 + /* Find an empty block. */
5645 +
5646 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
5647 + dev->allocationBlockFinder++;
5648 + if (dev->allocationBlockFinder < dev->internalStartBlock
5649 + || dev->allocationBlockFinder > dev->internalEndBlock) {
5650 + dev->allocationBlockFinder = dev->internalStartBlock;
5651 + }
5652 +
5653 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
5654 +
5655 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
5656 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
5657 + dev->sequenceNumber++;
5658 + bi->sequenceNumber = dev->sequenceNumber;
5659 + dev->nErasedBlocks--;
5660 + T(YAFFS_TRACE_ALLOCATE,
5661 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
5662 + dev->allocationBlockFinder, dev->sequenceNumber,
5663 + dev->nErasedBlocks));
5664 + return dev->allocationBlockFinder;
5665 + }
5666 + }
5667 +
5668 + T(YAFFS_TRACE_ALWAYS,
5669 + (TSTR
5670 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
5671 + TENDSTR), dev->nErasedBlocks));
5672 +
5673 + return -1;
5674 +}
5675 +
5676 +
5677 +// Check if there's space to allocate...
5678 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
5679 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
5680 +{
5681 + int reservedChunks;
5682 + int reservedBlocks = dev->nReservedBlocks;
5683 + int checkpointBlocks;
5684 +
5685 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
5686 + if(checkpointBlocks < 0)
5687 + checkpointBlocks = 0;
5688 +
5689 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
5690 +
5691 + return (dev->nFreeChunks > reservedChunks);
5692 +}
5693 +
5694 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
5695 +{
5696 + int retVal;
5697 + yaffs_BlockInfo *bi;
5698 +
5699 + if (dev->allocationBlock < 0) {
5700 + /* Get next block to allocate off */
5701 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
5702 + dev->allocationPage = 0;
5703 + }
5704 +
5705 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
5706 + /* Not enough space to allocate unless we're allowed to use the reserve. */
5707 + return -1;
5708 + }
5709 +
5710 + if (dev->nErasedBlocks < dev->nReservedBlocks
5711 + && dev->allocationPage == 0) {
5712 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
5713 + }
5714 +
5715 + /* Next page please.... */
5716 + if (dev->allocationBlock >= 0) {
5717 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
5718 +
5719 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
5720 + dev->allocationPage;
5721 + bi->pagesInUse++;
5722 + yaffs_SetChunkBit(dev, dev->allocationBlock,
5723 + dev->allocationPage);
5724 +
5725 + dev->allocationPage++;
5726 +
5727 + dev->nFreeChunks--;
5728 +
5729 + /* If the block is full set the state to full */
5730 + if (dev->allocationPage >= dev->nChunksPerBlock) {
5731 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
5732 + dev->allocationBlock = -1;
5733 + }
5734 +
5735 + if(blockUsedPtr)
5736 + *blockUsedPtr = bi;
5737 +
5738 + return retVal;
5739 + }
5740 +
5741 + T(YAFFS_TRACE_ERROR,
5742 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
5743 +
5744 + return -1;
5745 +}
5746 +
5747 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
5748 +{
5749 + int n;
5750 +
5751 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
5752 +
5753 + if (dev->allocationBlock > 0) {
5754 + n += (dev->nChunksPerBlock - dev->allocationPage);
5755 + }
5756 +
5757 + return n;
5758 +
5759 +}
5760 +
5761 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
5762 +{
5763 + int oldChunk;
5764 + int newChunk;
5765 + int chunkInBlock;
5766 + int markNAND;
5767 + int retVal = YAFFS_OK;
5768 + int cleanups = 0;
5769 + int i;
5770 + int isCheckpointBlock;
5771 +
5772 + int chunksBefore = yaffs_GetErasedChunks(dev);
5773 + int chunksAfter;
5774 +
5775 + yaffs_ExtendedTags tags;
5776 +
5777 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
5778 +
5779 + yaffs_Object *object;
5780 +
5781 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
5782 +
5783 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
5784 +
5785 + T(YAFFS_TRACE_TRACING,
5786 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
5787 + bi->pagesInUse, bi->hasShrinkHeader));
5788 +
5789 + /*yaffs_VerifyFreeChunks(dev); */
5790 +
5791 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
5792 +
5793 + /* Take off the number of soft deleted entries because
5794 + * they're going to get really deleted during GC.
5795 + */
5796 + dev->nFreeChunks -= bi->softDeletions;
5797 +
5798 + dev->isDoingGC = 1;
5799 +
5800 + if (isCheckpointBlock ||
5801 + !yaffs_StillSomeChunkBits(dev, block)) {
5802 + T(YAFFS_TRACE_TRACING,
5803 + (TSTR
5804 + ("Collecting block %d that has no chunks in use" TENDSTR),
5805 + block));
5806 + yaffs_BlockBecameDirty(dev, block);
5807 + } else {
5808 +
5809 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
5810 +
5811 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
5812 + chunkInBlock < dev->nChunksPerBlock
5813 + && yaffs_StillSomeChunkBits(dev, block);
5814 + chunkInBlock++, oldChunk++) {
5815 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
5816 +
5817 + /* This page is in use and might need to be copied off */
5818 +
5819 + markNAND = 1;
5820 +
5821 + yaffs_InitialiseTags(&tags);
5822 +
5823 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
5824 + buffer, &tags);
5825 +
5826 + object =
5827 + yaffs_FindObjectByNumber(dev,
5828 + tags.objectId);
5829 +
5830 + T(YAFFS_TRACE_GC_DETAIL,
5831 + (TSTR
5832 + ("Collecting page %d, %d %d %d " TENDSTR),
5833 + chunkInBlock, tags.objectId, tags.chunkId,
5834 + tags.byteCount));
5835 +
5836 + if (!object) {
5837 + T(YAFFS_TRACE_ERROR,
5838 + (TSTR
5839 + ("page %d in gc has no object "
5840 + TENDSTR), oldChunk));
5841 + }
5842 +
5843 + if (object && object->deleted
5844 + && tags.chunkId != 0) {
5845 + /* Data chunk in a deleted file, throw it away
5846 + * It's a soft deleted data chunk,
5847 + * No need to copy this, just forget about it and
5848 + * fix up the object.
5849 + */
5850 +
5851 + object->nDataChunks--;
5852 +
5853 + if (object->nDataChunks <= 0) {
5854 + /* remeber to clean up the object */
5855 + dev->gcCleanupList[cleanups] =
5856 + tags.objectId;
5857 + cleanups++;
5858 + }
5859 + markNAND = 0;
5860 + } else if (0
5861 + /* Todo object && object->deleted && object->nDataChunks == 0 */
5862 + ) {
5863 + /* Deleted object header with no data chunks.
5864 + * Can be discarded and the file deleted.
5865 + */
5866 + object->chunkId = 0;
5867 + yaffs_FreeTnode(object->myDev,
5868 + object->variant.
5869 + fileVariant.top);
5870 + object->variant.fileVariant.top = NULL;
5871 + yaffs_DoGenericObjectDeletion(object);
5872 +
5873 + } else if (object) {
5874 + /* It's either a data chunk in a live file or
5875 + * an ObjectHeader, so we're interested in it.
5876 + * NB Need to keep the ObjectHeaders of deleted files
5877 + * until the whole file has been deleted off
5878 + */
5879 + tags.serialNumber++;
5880 +
5881 + dev->nGCCopies++;
5882 +
5883 + if (tags.chunkId == 0) {
5884 + /* It is an object Id,
5885 + * We need to nuke the shrinkheader flags first
5886 + * We no longer want the shrinkHeader flag since its work is done
5887 + * and if it is left in place it will mess up scanning.
5888 + * Also, clear out any shadowing stuff
5889 + */
5890 +
5891 + yaffs_ObjectHeader *oh;
5892 + oh = (yaffs_ObjectHeader *)buffer;
5893 + oh->isShrink = 0;
5894 + oh->shadowsObject = -1;
5895 + tags.extraShadows = 0;
5896 + tags.extraIsShrinkHeader = 0;
5897 + }
5898 +
5899 + newChunk =
5900 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
5901 +
5902 + if (newChunk < 0) {
5903 + retVal = YAFFS_FAIL;
5904 + } else {
5905 +
5906 + /* Ok, now fix up the Tnodes etc. */
5907 +
5908 + if (tags.chunkId == 0) {
5909 + /* It's a header */
5910 + object->chunkId = newChunk;
5911 + object->serial = tags.serialNumber;
5912 + } else {
5913 + /* It's a data chunk */
5914 + yaffs_PutChunkIntoFile
5915 + (object,
5916 + tags.chunkId,
5917 + newChunk, 0);
5918 + }
5919 + }
5920 + }
5921 +
5922 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
5923 +
5924 + }
5925 + }
5926 +
5927 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
5928 +
5929 +
5930 + /* Do any required cleanups */
5931 + for (i = 0; i < cleanups; i++) {
5932 + /* Time to delete the file too */
5933 + object =
5934 + yaffs_FindObjectByNumber(dev,
5935 + dev->gcCleanupList[i]);
5936 + if (object) {
5937 + yaffs_FreeTnode(dev,
5938 + object->variant.fileVariant.
5939 + top);
5940 + object->variant.fileVariant.top = NULL;
5941 + T(YAFFS_TRACE_GC,
5942 + (TSTR
5943 + ("yaffs: About to finally delete object %d"
5944 + TENDSTR), object->objectId));
5945 + yaffs_DoGenericObjectDeletion(object);
5946 + object->myDev->nDeletedFiles--;
5947 + }
5948 +
5949 + }
5950 +
5951 + }
5952 +
5953 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
5954 + T(YAFFS_TRACE_GC,
5955 + (TSTR
5956 + ("gc did not increase free chunks before %d after %d"
5957 + TENDSTR), chunksBefore, chunksAfter));
5958 + }
5959 +
5960 + dev->isDoingGC = 0;
5961 +
5962 + return YAFFS_OK;
5963 +}
5964 +
5965 +/* New garbage collector
5966 + * If we're very low on erased blocks then we do aggressive garbage collection
5967 + * otherwise we do "leasurely" garbage collection.
5968 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
5969 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
5970 + *
5971 + * The idea is to help clear out space in a more spread-out manner.
5972 + * Dunno if it really does anything useful.
5973 + */
5974 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
5975 +{
5976 + int block;
5977 + int aggressive;
5978 + int gcOk = YAFFS_OK;
5979 + int maxTries = 0;
5980 +
5981 + int checkpointBlockAdjust;
5982 +
5983 + if (dev->isDoingGC) {
5984 + /* Bail out so we don't get recursive gc */
5985 + return YAFFS_OK;
5986 + }
5987 +
5988 + /* This loop should pass the first time.
5989 + * We'll only see looping here if the erase of the collected block fails.
5990 + */
5991 +
5992 + do {
5993 + maxTries++;
5994 +
5995 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
5996 + if(checkpointBlockAdjust < 0)
5997 + checkpointBlockAdjust = 0;
5998 +
5999 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust)) {
6000 + /* We need a block soon...*/
6001 + aggressive = 1;
6002 + } else {
6003 + /* We're in no hurry */
6004 + aggressive = 0;
6005 + }
6006 +
6007 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6008 +
6009 + if (block > 0) {
6010 + dev->garbageCollections++;
6011 + if (!aggressive) {
6012 + dev->passiveGarbageCollections++;
6013 + }
6014 +
6015 + T(YAFFS_TRACE_GC,
6016 + (TSTR
6017 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6018 + dev->nErasedBlocks, aggressive));
6019 +
6020 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6021 + }
6022 +
6023 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6024 + T(YAFFS_TRACE_GC,
6025 + (TSTR
6026 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6027 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6028 + }
6029 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6030 + && (maxTries < 2));
6031 +
6032 + return aggressive ? gcOk : YAFFS_OK;
6033 +}
6034 +
6035 +/*------------------------- TAGS --------------------------------*/
6036 +
6037 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6038 + int chunkInObject)
6039 +{
6040 + return (tags->chunkId == chunkInObject &&
6041 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6042 +
6043 +}
6044 +
6045 +
6046 +/*-------------------- Data file manipulation -----------------*/
6047 +
6048 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6049 + yaffs_ExtendedTags * tags)
6050 +{
6051 + /*Get the Tnode, then get the level 0 offset chunk offset */
6052 + yaffs_Tnode *tn;
6053 + int theChunk = -1;
6054 + yaffs_ExtendedTags localTags;
6055 + int retVal = -1;
6056 +
6057 + yaffs_Device *dev = in->myDev;
6058 +
6059 + if (!tags) {
6060 + /* Passed a NULL, so use our own tags space */
6061 + tags = &localTags;
6062 + }
6063 +
6064 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6065 +
6066 + if (tn) {
6067 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6068 +
6069 + retVal =
6070 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6071 + chunkInInode);
6072 + }
6073 + return retVal;
6074 +}
6075 +
6076 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6077 + yaffs_ExtendedTags * tags)
6078 +{
6079 + /* Get the Tnode, then get the level 0 offset chunk offset */
6080 + yaffs_Tnode *tn;
6081 + int theChunk = -1;
6082 + yaffs_ExtendedTags localTags;
6083 +
6084 + yaffs_Device *dev = in->myDev;
6085 + int retVal = -1;
6086 +
6087 + if (!tags) {
6088 + /* Passed a NULL, so use our own tags space */
6089 + tags = &localTags;
6090 + }
6091 +
6092 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6093 +
6094 + if (tn) {
6095 +
6096 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6097 +
6098 + retVal =
6099 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6100 + chunkInInode);
6101 +
6102 + /* Delete the entry in the filestructure (if found) */
6103 + if (retVal != -1) {
6104 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6105 + }
6106 + } else {
6107 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6108 + }
6109 +
6110 + if (retVal == -1) {
6111 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6112 + }
6113 + return retVal;
6114 +}
6115 +
6116 +#ifdef YAFFS_PARANOID
6117 +
6118 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6119 +{
6120 + int chunk;
6121 + int nChunks;
6122 + int fSize;
6123 + int failed = 0;
6124 + int objId;
6125 + yaffs_Tnode *tn;
6126 + yaffs_Tags localTags;
6127 + yaffs_Tags *tags = &localTags;
6128 + int theChunk;
6129 + int chunkDeleted;
6130 +
6131 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6132 + /* T(("Object not a file\n")); */
6133 + return YAFFS_FAIL;
6134 + }
6135 +
6136 + objId = in->objectId;
6137 + fSize = in->variant.fileVariant.fileSize;
6138 + nChunks =
6139 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6140 +
6141 + for (chunk = 1; chunk <= nChunks; chunk++) {
6142 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6143 + chunk);
6144 +
6145 + if (tn) {
6146 +
6147 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6148 +
6149 + if (yaffs_CheckChunkBits
6150 + (dev, theChunk / dev->nChunksPerBlock,
6151 + theChunk % dev->nChunksPerBlock)) {
6152 +
6153 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6154 + tags,
6155 + &chunkDeleted);
6156 + if (yaffs_TagsMatch
6157 + (tags, in->objectId, chunk, chunkDeleted)) {
6158 + /* found it; */
6159 +
6160 + }
6161 + } else {
6162 +
6163 + failed = 1;
6164 + }
6165 +
6166 + } else {
6167 + /* T(("No level 0 found for %d\n", chunk)); */
6168 + }
6169 + }
6170 +
6171 + return failed ? YAFFS_FAIL : YAFFS_OK;
6172 +}
6173 +
6174 +#endif
6175 +
6176 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
6177 + int chunkInNAND, int inScan)
6178 +{
6179 + /* NB inScan is zero unless scanning.
6180 + * For forward scanning, inScan is > 0;
6181 + * for backward scanning inScan is < 0
6182 + */
6183 +
6184 + yaffs_Tnode *tn;
6185 + yaffs_Device *dev = in->myDev;
6186 + int existingChunk;
6187 + yaffs_ExtendedTags existingTags;
6188 + yaffs_ExtendedTags newTags;
6189 + unsigned existingSerial, newSerial;
6190 +
6191 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6192 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
6193 + * If it is not during Scanning then something went wrong!
6194 + */
6195 + if (!inScan) {
6196 + T(YAFFS_TRACE_ERROR,
6197 + (TSTR
6198 + ("yaffs tragedy:attempt to put data chunk into a non-file"
6199 + TENDSTR)));
6200 + YBUG();
6201 + }
6202 +
6203 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
6204 + return YAFFS_OK;
6205 + }
6206 +
6207 + tn = yaffs_AddOrFindLevel0Tnode(dev,
6208 + &in->variant.fileVariant,
6209 + chunkInInode,
6210 + NULL);
6211 + if (!tn) {
6212 + return YAFFS_FAIL;
6213 + }
6214 +
6215 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6216 +
6217 + if (inScan != 0) {
6218 + /* If we're scanning then we need to test for duplicates
6219 + * NB This does not need to be efficient since it should only ever
6220 + * happen when the power fails during a write, then only one
6221 + * chunk should ever be affected.
6222 + *
6223 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
6224 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
6225 + */
6226 +
6227 + if (existingChunk != 0) {
6228 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
6229 + * thus we have to do a FindChunkInFile to get the real chunk id.
6230 + *
6231 + * We have a duplicate now we need to decide which one to use:
6232 + *
6233 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
6234 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
6235 + * YAFFS1: Get both sets of tags and compare serial numbers.
6236 + */
6237 +
6238 + if (inScan > 0) {
6239 + /* Only do this for forward scanning */
6240 + yaffs_ReadChunkWithTagsFromNAND(dev,
6241 + chunkInNAND,
6242 + NULL, &newTags);
6243 +
6244 + /* Do a proper find */
6245 + existingChunk =
6246 + yaffs_FindChunkInFile(in, chunkInInode,
6247 + &existingTags);
6248 + }
6249 +
6250 + if (existingChunk <= 0) {
6251 + /*Hoosterman - how did this happen? */
6252 +
6253 + T(YAFFS_TRACE_ERROR,
6254 + (TSTR
6255 + ("yaffs tragedy: existing chunk < 0 in scan"
6256 + TENDSTR)));
6257 +
6258 + }
6259 +
6260 + /* NB The deleted flags should be false, otherwise the chunks will
6261 + * not be loaded during a scan
6262 + */
6263 +
6264 + newSerial = newTags.serialNumber;
6265 + existingSerial = existingTags.serialNumber;
6266 +
6267 + if ((inScan > 0) &&
6268 + (in->myDev->isYaffs2 ||
6269 + existingChunk <= 0 ||
6270 + ((existingSerial + 1) & 3) == newSerial)) {
6271 + /* Forward scanning.
6272 + * Use new
6273 + * Delete the old one and drop through to update the tnode
6274 + */
6275 + yaffs_DeleteChunk(dev, existingChunk, 1,
6276 + __LINE__);
6277 + } else {
6278 + /* Backward scanning or we want to use the existing one
6279 + * Use existing.
6280 + * Delete the new one and return early so that the tnode isn't changed
6281 + */
6282 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
6283 + __LINE__);
6284 + return YAFFS_OK;
6285 + }
6286 + }
6287 +
6288 + }
6289 +
6290 + if (existingChunk == 0) {
6291 + in->nDataChunks++;
6292 + }
6293 +
6294 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
6295 +
6296 + return YAFFS_OK;
6297 +}
6298 +
6299 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
6300 + __u8 * buffer)
6301 +{
6302 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
6303 +
6304 + if (chunkInNAND >= 0) {
6305 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
6306 + buffer,NULL);
6307 + } else {
6308 + T(YAFFS_TRACE_NANDACCESS,
6309 + (TSTR("Chunk %d not found zero instead" TENDSTR),
6310 + chunkInNAND));
6311 + /* get sane (zero) data if you read a hole */
6312 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
6313 + return 0;
6314 + }
6315 +
6316 +}
6317 +
6318 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
6319 +{
6320 + int block;
6321 + int page;
6322 + yaffs_ExtendedTags tags;
6323 + yaffs_BlockInfo *bi;
6324 +
6325 + if (chunkId <= 0)
6326 + return;
6327 +
6328 + dev->nDeletions++;
6329 + block = chunkId / dev->nChunksPerBlock;
6330 + page = chunkId % dev->nChunksPerBlock;
6331 +
6332 + bi = yaffs_GetBlockInfo(dev, block);
6333 +
6334 + T(YAFFS_TRACE_DELETION,
6335 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
6336 +
6337 + if (markNAND &&
6338 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
6339 +
6340 + yaffs_InitialiseTags(&tags);
6341 +
6342 + tags.chunkDeleted = 1;
6343 +
6344 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
6345 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
6346 + } else {
6347 + dev->nUnmarkedDeletions++;
6348 + }
6349 +
6350 + /* Pull out of the management area.
6351 + * If the whole block became dirty, this will kick off an erasure.
6352 + */
6353 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
6354 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
6355 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
6356 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
6357 + dev->nFreeChunks++;
6358 +
6359 + yaffs_ClearChunkBit(dev, block, page);
6360 +
6361 + bi->pagesInUse--;
6362 +
6363 + if (bi->pagesInUse == 0 &&
6364 + !bi->hasShrinkHeader &&
6365 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
6366 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
6367 + yaffs_BlockBecameDirty(dev, block);
6368 + }
6369 +
6370 + } else {
6371 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
6372 + }
6373 +
6374 +}
6375 +
6376 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
6377 + const __u8 * buffer, int nBytes,
6378 + int useReserve)
6379 +{
6380 + /* Find old chunk Need to do this to get serial number
6381 + * Write new one and patch into tree.
6382 + * Invalidate old tags.
6383 + */
6384 +
6385 + int prevChunkId;
6386 + yaffs_ExtendedTags prevTags;
6387 +
6388 + int newChunkId;
6389 + yaffs_ExtendedTags newTags;
6390 +
6391 + yaffs_Device *dev = in->myDev;
6392 +
6393 + yaffs_CheckGarbageCollection(dev);
6394 +
6395 + /* Get the previous chunk at this location in the file if it exists */
6396 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
6397 +
6398 + /* Set up new tags */
6399 + yaffs_InitialiseTags(&newTags);
6400 +
6401 + newTags.chunkId = chunkInInode;
6402 + newTags.objectId = in->objectId;
6403 + newTags.serialNumber =
6404 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
6405 + newTags.byteCount = nBytes;
6406 +
6407 + newChunkId =
6408 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
6409 + useReserve);
6410 +
6411 + if (newChunkId >= 0) {
6412 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
6413 +
6414 + if (prevChunkId >= 0) {
6415 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
6416 +
6417 + }
6418 +
6419 + yaffs_CheckFileSanity(in);
6420 + }
6421 + return newChunkId;
6422 +
6423 +}
6424 +
6425 +/* UpdateObjectHeader updates the header on NAND for an object.
6426 + * If name is not NULL, then that new name is used.
6427 + */
6428 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
6429 + int isShrink, int shadows)
6430 +{
6431 +
6432 + yaffs_BlockInfo *bi;
6433 +
6434 + yaffs_Device *dev = in->myDev;
6435 +
6436 + int prevChunkId;
6437 + int retVal = 0;
6438 + int result = 0;
6439 +
6440 + int newChunkId;
6441 + yaffs_ExtendedTags newTags;
6442 +
6443 + __u8 *buffer = NULL;
6444 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
6445 +
6446 + yaffs_ObjectHeader *oh = NULL;
6447 +
6448 + if (!in->fake || force) {
6449 +
6450 + yaffs_CheckGarbageCollection(dev);
6451 +
6452 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
6453 + oh = (yaffs_ObjectHeader *) buffer;
6454 +
6455 + prevChunkId = in->chunkId;
6456 +
6457 + if (prevChunkId >= 0) {
6458 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
6459 + buffer, NULL);
6460 + memcpy(oldName, oh->name, sizeof(oh->name));
6461 + }
6462 +
6463 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
6464 +
6465 + oh->type = in->variantType;
6466 + oh->yst_mode = in->yst_mode;
6467 + oh->shadowsObject = shadows;
6468 +
6469 +#ifdef CONFIG_YAFFS_WINCE
6470 + oh->win_atime[0] = in->win_atime[0];
6471 + oh->win_ctime[0] = in->win_ctime[0];
6472 + oh->win_mtime[0] = in->win_mtime[0];
6473 + oh->win_atime[1] = in->win_atime[1];
6474 + oh->win_ctime[1] = in->win_ctime[1];
6475 + oh->win_mtime[1] = in->win_mtime[1];
6476 +#else
6477 + oh->yst_uid = in->yst_uid;
6478 + oh->yst_gid = in->yst_gid;
6479 + oh->yst_atime = in->yst_atime;
6480 + oh->yst_mtime = in->yst_mtime;
6481 + oh->yst_ctime = in->yst_ctime;
6482 + oh->yst_rdev = in->yst_rdev;
6483 +#endif
6484 + if (in->parent) {
6485 + oh->parentObjectId = in->parent->objectId;
6486 + } else {
6487 + oh->parentObjectId = 0;
6488 + }
6489 +
6490 + if (name && *name) {
6491 + memset(oh->name, 0, sizeof(oh->name));
6492 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
6493 + } else if (prevChunkId) {
6494 + memcpy(oh->name, oldName, sizeof(oh->name));
6495 + } else {
6496 + memset(oh->name, 0, sizeof(oh->name));
6497 + }
6498 +
6499 + oh->isShrink = isShrink;
6500 +
6501 + switch (in->variantType) {
6502 + case YAFFS_OBJECT_TYPE_UNKNOWN:
6503 + /* Should not happen */
6504 + break;
6505 + case YAFFS_OBJECT_TYPE_FILE:
6506 + oh->fileSize =
6507 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
6508 + || oh->parentObjectId ==
6509 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
6510 + fileVariant.fileSize;
6511 + break;
6512 + case YAFFS_OBJECT_TYPE_HARDLINK:
6513 + oh->equivalentObjectId =
6514 + in->variant.hardLinkVariant.equivalentObjectId;
6515 + break;
6516 + case YAFFS_OBJECT_TYPE_SPECIAL:
6517 + /* Do nothing */
6518 + break;
6519 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6520 + /* Do nothing */
6521 + break;
6522 + case YAFFS_OBJECT_TYPE_SYMLINK:
6523 + yaffs_strncpy(oh->alias,
6524 + in->variant.symLinkVariant.alias,
6525 + YAFFS_MAX_ALIAS_LENGTH);
6526 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
6527 + break;
6528 + }
6529 +
6530 + /* Tags */
6531 + yaffs_InitialiseTags(&newTags);
6532 + in->serial++;
6533 + newTags.chunkId = 0;
6534 + newTags.objectId = in->objectId;
6535 + newTags.serialNumber = in->serial;
6536 +
6537 + /* Add extra info for file header */
6538 +
6539 + newTags.extraHeaderInfoAvailable = 1;
6540 + newTags.extraParentObjectId = oh->parentObjectId;
6541 + newTags.extraFileLength = oh->fileSize;
6542 + newTags.extraIsShrinkHeader = oh->isShrink;
6543 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
6544 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
6545 + newTags.extraObjectType = in->variantType;
6546 +
6547 + /* Create new chunk in NAND */
6548 + newChunkId =
6549 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
6550 + (prevChunkId >= 0) ? 1 : 0);
6551 +
6552 + if (newChunkId >= 0) {
6553 +
6554 + in->chunkId = newChunkId;
6555 +
6556 + if (prevChunkId >= 0) {
6557 + yaffs_DeleteChunk(dev, prevChunkId, 1,
6558 + __LINE__);
6559 + }
6560 +
6561 + if(!yaffs_ObjectHasCachedWriteData(in))
6562 + in->dirty = 0;
6563 +
6564 + /* If this was a shrink, then mark the block that the chunk lives on */
6565 + if (isShrink) {
6566 + bi = yaffs_GetBlockInfo(in->myDev,
6567 + newChunkId /in->myDev-> nChunksPerBlock);
6568 + bi->hasShrinkHeader = 1;
6569 + }
6570 +
6571 + }
6572 +
6573 + retVal = newChunkId;
6574 +
6575 + }
6576 +
6577 + if (buffer)
6578 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6579 +
6580 + return retVal;
6581 +}
6582 +
6583 +/*------------------------ Short Operations Cache ----------------------------------------
6584 + * In many situations where there is no high level buffering (eg WinCE) a lot of
6585 + * reads might be short sequential reads, and a lot of writes may be short
6586 + * sequential writes. eg. scanning/writing a jpeg file.
6587 + * In these cases, a short read/write cache can provide a huge perfomance benefit
6588 + * with dumb-as-a-rock code.
6589 + * In Linux, the page cache provides read buffering aand the short op cache provides write
6590 + * buffering.
6591 + *
6592 + * There are a limited number (~10) of cache chunks per device so that we don't
6593 + * need a very intelligent search.
6594 + */
6595 +
6596 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
6597 +{
6598 + yaffs_Device *dev = obj->myDev;
6599 + int i;
6600 + yaffs_ChunkCache *cache;
6601 + int nCaches = obj->myDev->nShortOpCaches;
6602 +
6603 + for(i = 0; i < nCaches; i++){
6604 + cache = &dev->srCache[i];
6605 + if (cache->object == obj &&
6606 + cache->dirty)
6607 + return 1;
6608 + }
6609 +
6610 + return 0;
6611 +}
6612 +
6613 +
6614 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
6615 +{
6616 + yaffs_Device *dev = obj->myDev;
6617 + int lowest = -99; /* Stop compiler whining. */
6618 + int i;
6619 + yaffs_ChunkCache *cache;
6620 + int chunkWritten = 0;
6621 + int nCaches = obj->myDev->nShortOpCaches;
6622 +
6623 + if (nCaches > 0) {
6624 + do {
6625 + cache = NULL;
6626 +
6627 + /* Find the dirty cache for this object with the lowest chunk id. */
6628 + for (i = 0; i < nCaches; i++) {
6629 + if (dev->srCache[i].object == obj &&
6630 + dev->srCache[i].dirty) {
6631 + if (!cache
6632 + || dev->srCache[i].chunkId <
6633 + lowest) {
6634 + cache = &dev->srCache[i];
6635 + lowest = cache->chunkId;
6636 + }
6637 + }
6638 + }
6639 +
6640 + if (cache && !cache->locked) {
6641 + /* Write it out and free it up */
6642 +
6643 + chunkWritten =
6644 + yaffs_WriteChunkDataToObject(cache->object,
6645 + cache->chunkId,
6646 + cache->data,
6647 + cache->nBytes,
6648 + 1);
6649 + cache->dirty = 0;
6650 + cache->object = NULL;
6651 + }
6652 +
6653 + } while (cache && chunkWritten > 0);
6654 +
6655 + if (cache) {
6656 + /* Hoosterman, disk full while writing cache out. */
6657 + T(YAFFS_TRACE_ERROR,
6658 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
6659 +
6660 + }
6661 + }
6662 +
6663 +}
6664 +
6665 +/*yaffs_FlushEntireDeviceCache(dev)
6666 + *
6667 + *
6668 + */
6669 +
6670 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
6671 +{
6672 + yaffs_Object *obj;
6673 + int nCaches = dev->nShortOpCaches;
6674 + int i;
6675 +
6676 + /* Find a dirty object in the cache and flush it...
6677 + * until there are no further dirty objects.
6678 + */
6679 + do {
6680 + obj = NULL;
6681 + for( i = 0; i < nCaches && !obj; i++) {
6682 + if (dev->srCache[i].object &&
6683 + dev->srCache[i].dirty)
6684 + obj = dev->srCache[i].object;
6685 +
6686 + }
6687 + if(obj)
6688 + yaffs_FlushFilesChunkCache(obj);
6689 +
6690 + } while(obj);
6691 +
6692 +}
6693 +
6694 +
6695 +/* Grab us a cache chunk for use.
6696 + * First look for an empty one.
6697 + * Then look for the least recently used non-dirty one.
6698 + * Then look for the least recently used dirty one...., flush and look again.
6699 + */
6700 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
6701 +{
6702 + int i;
6703 + int usage;
6704 + int theOne;
6705 +
6706 + if (dev->nShortOpCaches > 0) {
6707 + for (i = 0; i < dev->nShortOpCaches; i++) {
6708 + if (!dev->srCache[i].object)
6709 + return &dev->srCache[i];
6710 + }
6711 +
6712 + return NULL;
6713 +
6714 + theOne = -1;
6715 + usage = 0; /* just to stop the compiler grizzling */
6716 +
6717 + for (i = 0; i < dev->nShortOpCaches; i++) {
6718 + if (!dev->srCache[i].dirty &&
6719 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
6720 + theOne < 0)) {
6721 + usage = dev->srCache[i].lastUse;
6722 + theOne = i;
6723 + }
6724 + }
6725 +
6726 +
6727 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
6728 + } else {
6729 + return NULL;
6730 + }
6731 +
6732 +}
6733 +
6734 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
6735 +{
6736 + yaffs_ChunkCache *cache;
6737 + yaffs_Object *theObj;
6738 + int usage;
6739 + int i;
6740 + int pushout;
6741 +
6742 + if (dev->nShortOpCaches > 0) {
6743 + /* Try find a non-dirty one... */
6744 +
6745 + cache = yaffs_GrabChunkCacheWorker(dev);
6746 +
6747 + if (!cache) {
6748 + /* They were all dirty, find the last recently used object and flush
6749 + * its cache, then find again.
6750 + * NB what's here is not very accurate, we actually flush the object
6751 + * the last recently used page.
6752 + */
6753 +
6754 + /* With locking we can't assume we can use entry zero */
6755 +
6756 + theObj = NULL;
6757 + usage = -1;
6758 + cache = NULL;
6759 + pushout = -1;
6760 +
6761 + for (i = 0; i < dev->nShortOpCaches; i++) {
6762 + if (dev->srCache[i].object &&
6763 + !dev->srCache[i].locked &&
6764 + (dev->srCache[i].lastUse < usage || !cache))
6765 + {
6766 + usage = dev->srCache[i].lastUse;
6767 + theObj = dev->srCache[i].object;
6768 + cache = &dev->srCache[i];
6769 + pushout = i;
6770 + }
6771 + }
6772 +
6773 + if (!cache || cache->dirty) {
6774 + /* Flush and try again */
6775 + yaffs_FlushFilesChunkCache(theObj);
6776 + cache = yaffs_GrabChunkCacheWorker(dev);
6777 + }
6778 +
6779 + }
6780 + return cache;
6781 + } else
6782 + return NULL;
6783 +
6784 +}
6785 +
6786 +/* Find a cached chunk */
6787 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
6788 + int chunkId)
6789 +{
6790 + yaffs_Device *dev = obj->myDev;
6791 + int i;
6792 + if (dev->nShortOpCaches > 0) {
6793 + for (i = 0; i < dev->nShortOpCaches; i++) {
6794 + if (dev->srCache[i].object == obj &&
6795 + dev->srCache[i].chunkId == chunkId) {
6796 + dev->cacheHits++;
6797 +
6798 + return &dev->srCache[i];
6799 + }
6800 + }
6801 + }
6802 + return NULL;
6803 +}
6804 +
6805 +/* Mark the chunk for the least recently used algorithym */
6806 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
6807 + int isAWrite)
6808 +{
6809 +
6810 + if (dev->nShortOpCaches > 0) {
6811 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
6812 + /* Reset the cache usages */
6813 + int i;
6814 + for (i = 1; i < dev->nShortOpCaches; i++) {
6815 + dev->srCache[i].lastUse = 0;
6816 + }
6817 + dev->srLastUse = 0;
6818 + }
6819 +
6820 + dev->srLastUse++;
6821 +
6822 + cache->lastUse = dev->srLastUse;
6823 +
6824 + if (isAWrite) {
6825 + cache->dirty = 1;
6826 + }
6827 + }
6828 +}
6829 +
6830 +/* Invalidate a single cache page.
6831 + * Do this when a whole page gets written,
6832 + * ie the short cache for this page is no longer valid.
6833 + */
6834 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
6835 +{
6836 + if (object->myDev->nShortOpCaches > 0) {
6837 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
6838 +
6839 + if (cache) {
6840 + cache->object = NULL;
6841 + }
6842 + }
6843 +}
6844 +
6845 +/* Invalidate all the cache pages associated with this object
6846 + * Do this whenever ther file is deleted or resized.
6847 + */
6848 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
6849 +{
6850 + int i;
6851 + yaffs_Device *dev = in->myDev;
6852 +
6853 + if (dev->nShortOpCaches > 0) {
6854 + /* Invalidate it. */
6855 + for (i = 0; i < dev->nShortOpCaches; i++) {
6856 + if (dev->srCache[i].object == in) {
6857 + dev->srCache[i].object = NULL;
6858 + }
6859 + }
6860 + }
6861 +}
6862 +
6863 +/*--------------------- Checkpointing --------------------*/
6864 +
6865 +
6866 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
6867 +{
6868 + yaffs_CheckpointValidity cp;
6869 + cp.structType = sizeof(cp);
6870 + cp.magic = YAFFS_MAGIC;
6871 + cp.version = YAFFS_CHECKPOINT_VERSION;
6872 + cp.head = (head) ? 1 : 0;
6873 +
6874 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
6875 + 1 : 0;
6876 +}
6877 +
6878 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
6879 +{
6880 + yaffs_CheckpointValidity cp;
6881 + int ok;
6882 +
6883 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
6884 +
6885 + if(ok)
6886 + ok = (cp.structType == sizeof(cp)) &&
6887 + (cp.magic == YAFFS_MAGIC) &&
6888 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
6889 + (cp.head == ((head) ? 1 : 0));
6890 + return ok ? 1 : 0;
6891 +}
6892 +
6893 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
6894 + yaffs_Device *dev)
6895 +{
6896 + cp->nErasedBlocks = dev->nErasedBlocks;
6897 + cp->allocationBlock = dev->allocationBlock;
6898 + cp->allocationPage = dev->allocationPage;
6899 + cp->nFreeChunks = dev->nFreeChunks;
6900 +
6901 + cp->nDeletedFiles = dev->nDeletedFiles;
6902 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
6903 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
6904 + cp->sequenceNumber = dev->sequenceNumber;
6905 + cp->oldestDirtySequence = dev->oldestDirtySequence;
6906 +
6907 +}
6908 +
6909 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
6910 + yaffs_CheckpointDevice *cp)
6911 +{
6912 + dev->nErasedBlocks = cp->nErasedBlocks;
6913 + dev->allocationBlock = cp->allocationBlock;
6914 + dev->allocationPage = cp->allocationPage;
6915 + dev->nFreeChunks = cp->nFreeChunks;
6916 +
6917 + dev->nDeletedFiles = cp->nDeletedFiles;
6918 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
6919 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
6920 + dev->sequenceNumber = cp->sequenceNumber;
6921 + dev->oldestDirtySequence = cp->oldestDirtySequence;
6922 +}
6923 +
6924 +
6925 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
6926 +{
6927 + yaffs_CheckpointDevice cp;
6928 + __u32 nBytes;
6929 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
6930 +
6931 + int ok;
6932 +
6933 + /* Write device runtime values*/
6934 + yaffs_DeviceToCheckpointDevice(&cp,dev);
6935 + cp.structType = sizeof(cp);
6936 +
6937 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
6938 +
6939 + /* Write block info */
6940 + if(ok) {
6941 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
6942 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
6943 + }
6944 +
6945 + /* Write chunk bits */
6946 + if(ok) {
6947 + nBytes = nBlocks * dev->chunkBitmapStride;
6948 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
6949 + }
6950 + return ok ? 1 : 0;
6951 +
6952 +}
6953 +
6954 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
6955 +{
6956 + yaffs_CheckpointDevice cp;
6957 + __u32 nBytes;
6958 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
6959 +
6960 + int ok;
6961 +
6962 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
6963 + if(!ok)
6964 + return 0;
6965 +
6966 + if(cp.structType != sizeof(cp))
6967 + return 0;
6968 +
6969 +
6970 + yaffs_CheckpointDeviceToDevice(dev,&cp);
6971 +
6972 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
6973 +
6974 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
6975 +
6976 + if(!ok)
6977 + return 0;
6978 + nBytes = nBlocks * dev->chunkBitmapStride;
6979 +
6980 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
6981 +
6982 + return ok ? 1 : 0;
6983 +}
6984 +
6985 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
6986 + yaffs_Object *obj)
6987 +{
6988 +
6989 + cp->objectId = obj->objectId;
6990 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
6991 + cp->chunkId = obj->chunkId;
6992 + cp->variantType = obj->variantType;
6993 + cp->deleted = obj->deleted;
6994 + cp->softDeleted = obj->softDeleted;
6995 + cp->unlinked = obj->unlinked;
6996 + cp->fake = obj->fake;
6997 + cp->renameAllowed = obj->renameAllowed;
6998 + cp->unlinkAllowed = obj->unlinkAllowed;
6999 + cp->serial = obj->serial;
7000 + cp->nDataChunks = obj->nDataChunks;
7001 +
7002 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7003 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7004 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7005 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7006 +}
7007 +
7008 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7009 +{
7010 +
7011 + yaffs_Object *parent;
7012 +
7013 + obj->objectId = cp->objectId;
7014 +
7015 + if(cp->parentId)
7016 + parent = yaffs_FindOrCreateObjectByNumber(
7017 + obj->myDev,
7018 + cp->parentId,
7019 + YAFFS_OBJECT_TYPE_DIRECTORY);
7020 + else
7021 + parent = NULL;
7022 +
7023 + if(parent)
7024 + yaffs_AddObjectToDirectory(parent, obj);
7025 +
7026 + obj->chunkId = cp->chunkId;
7027 + obj->variantType = cp->variantType;
7028 + obj->deleted = cp->deleted;
7029 + obj->softDeleted = cp->softDeleted;
7030 + obj->unlinked = cp->unlinked;
7031 + obj->fake = cp->fake;
7032 + obj->renameAllowed = cp->renameAllowed;
7033 + obj->unlinkAllowed = cp->unlinkAllowed;
7034 + obj->serial = cp->serial;
7035 + obj->nDataChunks = cp->nDataChunks;
7036 +
7037 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7038 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7039 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7040 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7041 +
7042 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7043 + obj->lazyLoaded = 1;
7044 +}
7045 +
7046 +
7047 +
7048 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7049 + __u32 level, int chunkOffset)
7050 +{
7051 + int i;
7052 + yaffs_Device *dev = in->myDev;
7053 + int ok = 1;
7054 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7055 +
7056 + if (tn) {
7057 + if (level > 0) {
7058 +
7059 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7060 + if (tn->internal[i]) {
7061 + ok = yaffs_CheckpointTnodeWorker(in,
7062 + tn->internal[i],
7063 + level - 1,
7064 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7065 + }
7066 + }
7067 + } else if (level == 0) {
7068 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7069 + /* printf("write tnode at %d\n",baseOffset); */
7070 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7071 + if(ok)
7072 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7073 + }
7074 + }
7075 +
7076 + return ok;
7077 +
7078 +}
7079 +
7080 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7081 +{
7082 + __u32 endMarker = ~0;
7083 + int ok = 1;
7084 +
7085 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7086 + ok = yaffs_CheckpointTnodeWorker(obj,
7087 + obj->variant.fileVariant.top,
7088 + obj->variant.fileVariant.topLevel,
7089 + 0);
7090 + if(ok)
7091 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7092 + sizeof(endMarker));
7093 + }
7094 +
7095 + return ok ? 1 : 0;
7096 +}
7097 +
7098 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7099 +{
7100 + __u32 baseChunk;
7101 + int ok = 1;
7102 + yaffs_Device *dev = obj->myDev;
7103 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7104 + yaffs_Tnode *tn;
7105 +
7106 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7107 +
7108 + while(ok && (~baseChunk)){
7109 + /* Read level 0 tnode */
7110 +
7111 + /* printf("read tnode at %d\n",baseChunk); */
7112 + tn = yaffs_GetTnodeRaw(dev);
7113 + if(tn)
7114 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7115 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7116 + else
7117 + ok = 0;
7118 +
7119 + if(tn && ok){
7120 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7121 + fileStructPtr,
7122 + baseChunk,
7123 + tn) ? 1 : 0;
7124 + }
7125 +
7126 + if(ok)
7127 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7128 +
7129 + }
7130 +
7131 + return ok ? 1 : 0;
7132 +}
7133 +
7134 +
7135 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7136 +{
7137 + yaffs_Object *obj;
7138 + yaffs_CheckpointObject cp;
7139 + int i;
7140 + int ok = 1;
7141 + struct list_head *lh;
7142 +
7143 +
7144 + /* Iterate through the objects in each hash entry,
7145 + * dumping them to the checkpointing stream.
7146 + */
7147 +
7148 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
7149 + list_for_each(lh, &dev->objectBucket[i].list) {
7150 + if (lh) {
7151 + obj = list_entry(lh, yaffs_Object, hashLink);
7152 + if (!obj->deferedFree) {
7153 + yaffs_ObjectToCheckpointObject(&cp,obj);
7154 + cp.structType = sizeof(cp);
7155 +
7156 + T(YAFFS_TRACE_CHECKPOINT,(
7157 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7158 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7159 +
7160 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7161 +
7162 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7163 + ok = yaffs_WriteCheckpointTnodes(obj);
7164 + }
7165 + }
7166 + }
7167 + }
7168 + }
7169 +
7170 + /* Dump end of list */
7171 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
7172 + cp.structType = sizeof(cp);
7173 +
7174 + if(ok)
7175 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7176 +
7177 + return ok ? 1 : 0;
7178 +}
7179 +
7180 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
7181 +{
7182 + yaffs_Object *obj;
7183 + yaffs_CheckpointObject cp;
7184 + int ok = 1;
7185 + int done = 0;
7186 + yaffs_Object *hardList = NULL;
7187 +
7188 + while(ok && !done) {
7189 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7190 + if(cp.structType != sizeof(cp)) {
7191 + /* printf("structure parsing failed\n"); */
7192 + ok = 0;
7193 + }
7194 +
7195 + if(ok && cp.objectId == ~0)
7196 + done = 1;
7197 + else if(ok){
7198 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
7199 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7200 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7201 + if(obj) {
7202 + yaffs_CheckpointObjectToObject(obj,&cp);
7203 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
7204 + ok = yaffs_ReadCheckpointTnodes(obj);
7205 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
7206 + obj->hardLinks.next =
7207 + (struct list_head *)
7208 + hardList;
7209 + hardList = obj;
7210 + }
7211 +
7212 + }
7213 + }
7214 + }
7215 +
7216 + if(ok)
7217 + yaffs_HardlinkFixup(dev,hardList);
7218 +
7219 + return ok ? 1 : 0;
7220 +}
7221 +
7222 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
7223 +{
7224 +
7225 + int ok;
7226 +
7227 + ok = yaffs_CheckpointOpen(dev,1);
7228 +
7229 + if(ok)
7230 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
7231 + if(ok)
7232 + ok = yaffs_WriteCheckpointDevice(dev);
7233 + if(ok)
7234 + ok = yaffs_WriteCheckpointObjects(dev);
7235 + if(ok)
7236 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
7237 +
7238 + if(!yaffs_CheckpointClose(dev))
7239 + ok = 0;
7240 +
7241 + if(ok)
7242 + dev->isCheckpointed = 1;
7243 + else
7244 + dev->isCheckpointed = 0;
7245 +
7246 + return dev->isCheckpointed;
7247 +}
7248 +
7249 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
7250 +{
7251 + int ok;
7252 +
7253 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
7254 +
7255 + if(ok)
7256 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
7257 + if(ok)
7258 + ok = yaffs_ReadCheckpointDevice(dev);
7259 + if(ok)
7260 + ok = yaffs_ReadCheckpointObjects(dev);
7261 + if(ok)
7262 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
7263 +
7264 +
7265 +
7266 + if(!yaffs_CheckpointClose(dev))
7267 + ok = 0;
7268 +
7269 + if(ok)
7270 + dev->isCheckpointed = 1;
7271 + else
7272 + dev->isCheckpointed = 0;
7273 +
7274 + return ok ? 1 : 0;
7275 +
7276 +}
7277 +
7278 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
7279 +{
7280 + if(dev->isCheckpointed ||
7281 + dev->blocksInCheckpoint > 0){
7282 + dev->isCheckpointed = 0;
7283 + yaffs_CheckpointInvalidateStream(dev);
7284 + if(dev->superBlock && dev->markSuperBlockDirty)
7285 + dev->markSuperBlockDirty(dev->superBlock);
7286 + }
7287 +}
7288 +
7289 +
7290 +int yaffs_CheckpointSave(yaffs_Device *dev)
7291 +{
7292 + yaffs_ReportOddballBlocks(dev);
7293 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7294 +
7295 + if(!dev->isCheckpointed)
7296 + yaffs_WriteCheckpointData(dev);
7297 +
7298 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7299 +
7300 + return dev->isCheckpointed;
7301 +}
7302 +
7303 +int yaffs_CheckpointRestore(yaffs_Device *dev)
7304 +{
7305 + int retval;
7306 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7307 +
7308 + retval = yaffs_ReadCheckpointData(dev);
7309 +
7310 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
7311 +
7312 + yaffs_ReportOddballBlocks(dev);
7313 +
7314 + return retval;
7315 +}
7316 +
7317 +/*--------------------- File read/write ------------------------
7318 + * Read and write have very similar structures.
7319 + * In general the read/write has three parts to it
7320 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
7321 + * Some complete chunks
7322 + * An incomplete chunk to end off with
7323 + *
7324 + * Curve-balls: the first chunk might also be the last chunk.
7325 + */
7326 +
7327 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
7328 + int nBytes)
7329 +{
7330 +
7331 + int chunk;
7332 + int start;
7333 + int nToCopy;
7334 + int n = nBytes;
7335 + int nDone = 0;
7336 + yaffs_ChunkCache *cache;
7337 +
7338 + yaffs_Device *dev;
7339 +
7340 + dev = in->myDev;
7341 +
7342 + while (n > 0) {
7343 + //chunk = offset / dev->nDataBytesPerChunk + 1;
7344 + //start = offset % dev->nDataBytesPerChunk;
7345 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
7346 + chunk++;
7347 +
7348 + /* OK now check for the curveball where the start and end are in
7349 + * the same chunk.
7350 + */
7351 + if ((start + n) < dev->nDataBytesPerChunk) {
7352 + nToCopy = n;
7353 + } else {
7354 + nToCopy = dev->nDataBytesPerChunk - start;
7355 + }
7356 +
7357 + cache = yaffs_FindChunkCache(in, chunk);
7358 +
7359 + /* If the chunk is already in the cache or it is less than a whole chunk
7360 + * then use the cache (if there is caching)
7361 + * else bypass the cache.
7362 + */
7363 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
7364 + if (dev->nShortOpCaches > 0) {
7365 +
7366 + /* If we can't find the data in the cache, then load it up. */
7367 +
7368 + if (!cache) {
7369 + cache = yaffs_GrabChunkCache(in->myDev);
7370 + cache->object = in;
7371 + cache->chunkId = chunk;
7372 + cache->dirty = 0;
7373 + cache->locked = 0;
7374 + yaffs_ReadChunkDataFromObject(in, chunk,
7375 + cache->
7376 + data);
7377 + cache->nBytes = 0;
7378 + }
7379 +
7380 + yaffs_UseChunkCache(dev, cache, 0);
7381 +
7382 + cache->locked = 1;
7383 +
7384 +#ifdef CONFIG_YAFFS_WINCE
7385 + yfsd_UnlockYAFFS(TRUE);
7386 +#endif
7387 + memcpy(buffer, &cache->data[start], nToCopy);
7388 +
7389 +#ifdef CONFIG_YAFFS_WINCE
7390 + yfsd_LockYAFFS(TRUE);
7391 +#endif
7392 + cache->locked = 0;
7393 + } else {
7394 + /* Read into the local buffer then copy..*/
7395 +
7396 + __u8 *localBuffer =
7397 + yaffs_GetTempBuffer(dev, __LINE__);
7398 + yaffs_ReadChunkDataFromObject(in, chunk,
7399 + localBuffer);
7400 +#ifdef CONFIG_YAFFS_WINCE
7401 + yfsd_UnlockYAFFS(TRUE);
7402 +#endif
7403 + memcpy(buffer, &localBuffer[start], nToCopy);
7404 +
7405 +#ifdef CONFIG_YAFFS_WINCE
7406 + yfsd_LockYAFFS(TRUE);
7407 +#endif
7408 + yaffs_ReleaseTempBuffer(dev, localBuffer,
7409 + __LINE__);
7410 + }
7411 +
7412 + } else {
7413 +#ifdef CONFIG_YAFFS_WINCE
7414 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7415 +
7416 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
7417 + * This is because we otherwise screw up WinCE's memory mapper
7418 + */
7419 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
7420 +
7421 +#ifdef CONFIG_YAFFS_WINCE
7422 + yfsd_UnlockYAFFS(TRUE);
7423 +#endif
7424 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
7425 +
7426 +#ifdef CONFIG_YAFFS_WINCE
7427 + yfsd_LockYAFFS(TRUE);
7428 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7429 +#endif
7430 +
7431 +#else
7432 + /* A full chunk. Read directly into the supplied buffer. */
7433 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
7434 +#endif
7435 + }
7436 +
7437 + n -= nToCopy;
7438 + offset += nToCopy;
7439 + buffer += nToCopy;
7440 + nDone += nToCopy;
7441 +
7442 + }
7443 +
7444 + return nDone;
7445 +}
7446 +
7447 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
7448 + int nBytes, int writeThrough)
7449 +{
7450 +
7451 + int chunk;
7452 + int start;
7453 + int nToCopy;
7454 + int n = nBytes;
7455 + int nDone = 0;
7456 + int nToWriteBack;
7457 + int startOfWrite = offset;
7458 + int chunkWritten = 0;
7459 + int nBytesRead;
7460 +
7461 + yaffs_Device *dev;
7462 +
7463 + dev = in->myDev;
7464 +
7465 + while (n > 0 && chunkWritten >= 0) {
7466 + //chunk = offset / dev->nDataBytesPerChunk + 1;
7467 + //start = offset % dev->nDataBytesPerChunk;
7468 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
7469 + chunk++;
7470 +
7471 + /* OK now check for the curveball where the start and end are in
7472 + * the same chunk.
7473 + */
7474 +
7475 + if ((start + n) < dev->nDataBytesPerChunk) {
7476 + nToCopy = n;
7477 +
7478 + /* Now folks, to calculate how many bytes to write back....
7479 + * If we're overwriting and not writing to then end of file then
7480 + * we need to write back as much as was there before.
7481 + */
7482 +
7483 + nBytesRead =
7484 + in->variant.fileVariant.fileSize -
7485 + ((chunk - 1) * dev->nDataBytesPerChunk);
7486 +
7487 + if (nBytesRead > dev->nDataBytesPerChunk) {
7488 + nBytesRead = dev->nDataBytesPerChunk;
7489 + }
7490 +
7491 + nToWriteBack =
7492 + (nBytesRead >
7493 + (start + n)) ? nBytesRead : (start + n);
7494 +
7495 + } else {
7496 + nToCopy = dev->nDataBytesPerChunk - start;
7497 + nToWriteBack = dev->nDataBytesPerChunk;
7498 + }
7499 +
7500 + if (nToCopy != dev->nDataBytesPerChunk) {
7501 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
7502 + if (dev->nShortOpCaches > 0) {
7503 + yaffs_ChunkCache *cache;
7504 + /* If we can't find the data in the cache, then load the cache */
7505 + cache = yaffs_FindChunkCache(in, chunk);
7506 +
7507 + if (!cache
7508 + && yaffs_CheckSpaceForAllocation(in->
7509 + myDev)) {
7510 + cache = yaffs_GrabChunkCache(in->myDev);
7511 + cache->object = in;
7512 + cache->chunkId = chunk;
7513 + cache->dirty = 0;
7514 + cache->locked = 0;
7515 + yaffs_ReadChunkDataFromObject(in, chunk,
7516 + cache->
7517 + data);
7518 + }
7519 + else if(cache &&
7520 + !cache->dirty &&
7521 + !yaffs_CheckSpaceForAllocation(in->myDev)){
7522 + /* Drop the cache if it was a read cache item and
7523 + * no space check has been made for it.
7524 + */
7525 + cache = NULL;
7526 + }
7527 +
7528 + if (cache) {
7529 + yaffs_UseChunkCache(dev, cache, 1);
7530 + cache->locked = 1;
7531 +#ifdef CONFIG_YAFFS_WINCE
7532 + yfsd_UnlockYAFFS(TRUE);
7533 +#endif
7534 +
7535 + memcpy(&cache->data[start], buffer,
7536 + nToCopy);
7537 +
7538 +#ifdef CONFIG_YAFFS_WINCE
7539 + yfsd_LockYAFFS(TRUE);
7540 +#endif
7541 + cache->locked = 0;
7542 + cache->nBytes = nToWriteBack;
7543 +
7544 + if (writeThrough) {
7545 + chunkWritten =
7546 + yaffs_WriteChunkDataToObject
7547 + (cache->object,
7548 + cache->chunkId,
7549 + cache->data, cache->nBytes,
7550 + 1);
7551 + cache->dirty = 0;
7552 + }
7553 +
7554 + } else {
7555 + chunkWritten = -1; /* fail the write */
7556 + }
7557 + } else {
7558 + /* An incomplete start or end chunk (or maybe both start and end chunk)
7559 + * Read into the local buffer then copy, then copy over and write back.
7560 + */
7561 +
7562 + __u8 *localBuffer =
7563 + yaffs_GetTempBuffer(dev, __LINE__);
7564 +
7565 + yaffs_ReadChunkDataFromObject(in, chunk,
7566 + localBuffer);
7567 +
7568 +#ifdef CONFIG_YAFFS_WINCE
7569 + yfsd_UnlockYAFFS(TRUE);
7570 +#endif
7571 +
7572 + memcpy(&localBuffer[start], buffer, nToCopy);
7573 +
7574 +#ifdef CONFIG_YAFFS_WINCE
7575 + yfsd_LockYAFFS(TRUE);
7576 +#endif
7577 + chunkWritten =
7578 + yaffs_WriteChunkDataToObject(in, chunk,
7579 + localBuffer,
7580 + nToWriteBack,
7581 + 0);
7582 +
7583 + yaffs_ReleaseTempBuffer(dev, localBuffer,
7584 + __LINE__);
7585 +
7586 + }
7587 +
7588 + } else {
7589 +
7590 +#ifdef CONFIG_YAFFS_WINCE
7591 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
7592 + * This is because we otherwise screw up WinCE's memory mapper
7593 + */
7594 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7595 +#ifdef CONFIG_YAFFS_WINCE
7596 + yfsd_UnlockYAFFS(TRUE);
7597 +#endif
7598 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
7599 +#ifdef CONFIG_YAFFS_WINCE
7600 + yfsd_LockYAFFS(TRUE);
7601 +#endif
7602 + chunkWritten =
7603 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
7604 + dev->nDataBytesPerChunk,
7605 + 0);
7606 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7607 +#else
7608 + /* A full chunk. Write directly from the supplied buffer. */
7609 + chunkWritten =
7610 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
7611 + dev->nDataBytesPerChunk,
7612 + 0);
7613 +#endif
7614 + /* Since we've overwritten the cached data, we better invalidate it. */
7615 + yaffs_InvalidateChunkCache(in, chunk);
7616 + }
7617 +
7618 + if (chunkWritten >= 0) {
7619 + n -= nToCopy;
7620 + offset += nToCopy;
7621 + buffer += nToCopy;
7622 + nDone += nToCopy;
7623 + }
7624 +
7625 + }
7626 +
7627 + /* Update file object */
7628 +
7629 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
7630 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
7631 + }
7632 +
7633 + in->dirty = 1;
7634 +
7635 + return nDone;
7636 +}
7637 +
7638 +
7639 +/* ---------------------- File resizing stuff ------------------ */
7640 +
7641 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
7642 +{
7643 +
7644 + yaffs_Device *dev = in->myDev;
7645 + int oldFileSize = in->variant.fileVariant.fileSize;
7646 +
7647 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
7648 +
7649 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
7650 + dev->nDataBytesPerChunk;
7651 + int i;
7652 + int chunkId;
7653 +
7654 + /* Delete backwards so that we don't end up with holes if
7655 + * power is lost part-way through the operation.
7656 + */
7657 + for (i = lastDel; i >= startDel; i--) {
7658 + /* NB this could be optimised somewhat,
7659 + * eg. could retrieve the tags and write them without
7660 + * using yaffs_DeleteChunk
7661 + */
7662 +
7663 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
7664 + if (chunkId > 0) {
7665 + if (chunkId <
7666 + (dev->internalStartBlock * dev->nChunksPerBlock)
7667 + || chunkId >=
7668 + ((dev->internalEndBlock +
7669 + 1) * dev->nChunksPerBlock)) {
7670 + T(YAFFS_TRACE_ALWAYS,
7671 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
7672 + chunkId, i));
7673 + } else {
7674 + in->nDataChunks--;
7675 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
7676 + }
7677 + }
7678 + }
7679 +
7680 +}
7681 +
7682 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
7683 +{
7684 +
7685 + int oldFileSize = in->variant.fileVariant.fileSize;
7686 + int newSizeOfPartialChunk;
7687 + int newFullChunks;
7688 +
7689 + yaffs_Device *dev = in->myDev;
7690 +
7691 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
7692 +
7693 + yaffs_FlushFilesChunkCache(in);
7694 + yaffs_InvalidateWholeChunkCache(in);
7695 +
7696 + yaffs_CheckGarbageCollection(dev);
7697 +
7698 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7699 + return yaffs_GetFileSize(in);
7700 + }
7701 +
7702 + if (newSize == oldFileSize) {
7703 + return oldFileSize;
7704 + }
7705 +
7706 + if (newSize < oldFileSize) {
7707 +
7708 + yaffs_PruneResizedChunks(in, newSize);
7709 +
7710 + if (newSizeOfPartialChunk != 0) {
7711 + int lastChunk = 1 + newFullChunks;
7712 +
7713 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
7714 +
7715 + /* Got to read and rewrite the last chunk with its new size and zero pad */
7716 + yaffs_ReadChunkDataFromObject(in, lastChunk,
7717 + localBuffer);
7718 +
7719 + memset(localBuffer + newSizeOfPartialChunk, 0,
7720 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
7721 +
7722 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
7723 + newSizeOfPartialChunk, 1);
7724 +
7725 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
7726 + }
7727 +
7728 + in->variant.fileVariant.fileSize = newSize;
7729 +
7730 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
7731 + }
7732 + /* Write a new object header.
7733 + * show we've shrunk the file, if need be
7734 + * Do this only if the file is not in the deleted directories.
7735 + */
7736 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
7737 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
7738 + yaffs_UpdateObjectHeader(in, NULL, 0,
7739 + (newSize < oldFileSize) ? 1 : 0, 0);
7740 + }
7741 +
7742 + return newSize;
7743 +}
7744 +
7745 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
7746 +{
7747 + obj = yaffs_GetEquivalentObject(obj);
7748 +
7749 + switch (obj->variantType) {
7750 + case YAFFS_OBJECT_TYPE_FILE:
7751 + return obj->variant.fileVariant.fileSize;
7752 + case YAFFS_OBJECT_TYPE_SYMLINK:
7753 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
7754 + default:
7755 + return 0;
7756 + }
7757 +}
7758 +
7759 +
7760 +
7761 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
7762 +{
7763 + int retVal;
7764 + if (in->dirty) {
7765 + yaffs_FlushFilesChunkCache(in);
7766 + if (updateTime) {
7767 +#ifdef CONFIG_YAFFS_WINCE
7768 + yfsd_WinFileTimeNow(in->win_mtime);
7769 +#else
7770 +
7771 + in->yst_mtime = Y_CURRENT_TIME;
7772 +
7773 +#endif
7774 + }
7775 +
7776 + retVal =
7777 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
7778 + 0) ? YAFFS_OK : YAFFS_FAIL;
7779 + } else {
7780 + retVal = YAFFS_OK;
7781 + }
7782 +
7783 + return retVal;
7784 +
7785 +}
7786 +
7787 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
7788 +{
7789 +
7790 + /* First off, invalidate the file's data in the cache, without flushing. */
7791 + yaffs_InvalidateWholeChunkCache(in);
7792 +
7793 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
7794 + /* Move to the unlinked directory so we have a record that it was deleted. */
7795 + yaffs_ChangeObjectName(in, in->myDev->deletedDir, NULL, 0, 0);
7796 +
7797 + }
7798 +
7799 + yaffs_RemoveObjectFromDirectory(in);
7800 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
7801 + in->chunkId = -1;
7802 +
7803 + yaffs_FreeObject(in);
7804 + return YAFFS_OK;
7805 +
7806 +}
7807 +
7808 +/* yaffs_DeleteFile deletes the whole file data
7809 + * and the inode associated with the file.
7810 + * It does not delete the links associated with the file.
7811 + */
7812 +static int yaffs_UnlinkFile(yaffs_Object * in)
7813 +{
7814 +
7815 + int retVal;
7816 + int immediateDeletion = 0;
7817 +
7818 + if (1) {
7819 +#ifdef __KERNEL__
7820 + if (!in->myInode) {
7821 + immediateDeletion = 1;
7822 +
7823 + }
7824 +#else
7825 + if (in->inUse <= 0) {
7826 + immediateDeletion = 1;
7827 +
7828 + }
7829 +#endif
7830 + if (immediateDeletion) {
7831 + retVal =
7832 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
7833 + NULL, 0, 0);
7834 + T(YAFFS_TRACE_TRACING,
7835 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
7836 + in->objectId));
7837 + in->deleted = 1;
7838 + in->myDev->nDeletedFiles++;
7839 + if (0 && in->myDev->isYaffs2) {
7840 + yaffs_ResizeFile(in, 0);
7841 + }
7842 + yaffs_SoftDeleteFile(in);
7843 + } else {
7844 + retVal =
7845 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
7846 + NULL, 0, 0);
7847 + }
7848 +
7849 + }
7850 + return retVal;
7851 +}
7852 +
7853 +int yaffs_DeleteFile(yaffs_Object * in)
7854 +{
7855 + int retVal = YAFFS_OK;
7856 +
7857 + if (in->nDataChunks > 0) {
7858 + /* Use soft deletion if there is data in the file */
7859 + if (!in->unlinked) {
7860 + retVal = yaffs_UnlinkFile(in);
7861 + }
7862 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
7863 + in->deleted = 1;
7864 + in->myDev->nDeletedFiles++;
7865 + yaffs_SoftDeleteFile(in);
7866 + }
7867 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
7868 + } else {
7869 + /* The file has no data chunks so we toss it immediately */
7870 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
7871 + in->variant.fileVariant.top = NULL;
7872 + yaffs_DoGenericObjectDeletion(in);
7873 +
7874 + return YAFFS_OK;
7875 + }
7876 +}
7877 +
7878 +static int yaffs_DeleteDirectory(yaffs_Object * in)
7879 +{
7880 + /* First check that the directory is empty. */
7881 + if (list_empty(&in->variant.directoryVariant.children)) {
7882 + return yaffs_DoGenericObjectDeletion(in);
7883 + }
7884 +
7885 + return YAFFS_FAIL;
7886 +
7887 +}
7888 +
7889 +static int yaffs_DeleteSymLink(yaffs_Object * in)
7890 +{
7891 + YFREE(in->variant.symLinkVariant.alias);
7892 +
7893 + return yaffs_DoGenericObjectDeletion(in);
7894 +}
7895 +
7896 +static int yaffs_DeleteHardLink(yaffs_Object * in)
7897 +{
7898 + /* remove this hardlink from the list assocaited with the equivalent
7899 + * object
7900 + */
7901 + list_del(&in->hardLinks);
7902 + return yaffs_DoGenericObjectDeletion(in);
7903 +}
7904 +
7905 +static void yaffs_DestroyObject(yaffs_Object * obj)
7906 +{
7907 + switch (obj->variantType) {
7908 + case YAFFS_OBJECT_TYPE_FILE:
7909 + yaffs_DeleteFile(obj);
7910 + break;
7911 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7912 + yaffs_DeleteDirectory(obj);
7913 + break;
7914 + case YAFFS_OBJECT_TYPE_SYMLINK:
7915 + yaffs_DeleteSymLink(obj);
7916 + break;
7917 + case YAFFS_OBJECT_TYPE_HARDLINK:
7918 + yaffs_DeleteHardLink(obj);
7919 + break;
7920 + case YAFFS_OBJECT_TYPE_SPECIAL:
7921 + yaffs_DoGenericObjectDeletion(obj);
7922 + break;
7923 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7924 + break; /* should not happen. */
7925 + }
7926 +}
7927 +
7928 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
7929 +{
7930 +
7931 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
7932 + return yaffs_DeleteHardLink(obj);
7933 + } else if (!list_empty(&obj->hardLinks)) {
7934 + /* Curve ball: We're unlinking an object that has a hardlink.
7935 + *
7936 + * This problem arises because we are not strictly following
7937 + * The Linux link/inode model.
7938 + *
7939 + * We can't really delete the object.
7940 + * Instead, we do the following:
7941 + * - Select a hardlink.
7942 + * - Unhook it from the hard links
7943 + * - Unhook it from its parent directory (so that the rename can work)
7944 + * - Rename the object to the hardlink's name.
7945 + * - Delete the hardlink
7946 + */
7947 +
7948 + yaffs_Object *hl;
7949 + int retVal;
7950 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
7951 +
7952 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
7953 +
7954 + list_del_init(&hl->hardLinks);
7955 + list_del_init(&hl->siblings);
7956 +
7957 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
7958 +
7959 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
7960 +
7961 + if (retVal == YAFFS_OK) {
7962 + retVal = yaffs_DoGenericObjectDeletion(hl);
7963 + }
7964 + return retVal;
7965 +
7966 + } else {
7967 + switch (obj->variantType) {
7968 + case YAFFS_OBJECT_TYPE_FILE:
7969 + return yaffs_UnlinkFile(obj);
7970 + break;
7971 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7972 + return yaffs_DeleteDirectory(obj);
7973 + break;
7974 + case YAFFS_OBJECT_TYPE_SYMLINK:
7975 + return yaffs_DeleteSymLink(obj);
7976 + break;
7977 + case YAFFS_OBJECT_TYPE_SPECIAL:
7978 + return yaffs_DoGenericObjectDeletion(obj);
7979 + break;
7980 + case YAFFS_OBJECT_TYPE_HARDLINK:
7981 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7982 + default:
7983 + return YAFFS_FAIL;
7984 + }
7985 + }
7986 +}
7987 +
7988 +
7989 +static int yaffs_UnlinkObject( yaffs_Object *obj)
7990 +{
7991 +
7992 + if (obj && obj->unlinkAllowed) {
7993 + return yaffs_UnlinkWorker(obj);
7994 + }
7995 +
7996 + return YAFFS_FAIL;
7997 +
7998 +}
7999 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8000 +{
8001 + yaffs_Object *obj;
8002 +
8003 + obj = yaffs_FindObjectByName(dir, name);
8004 + return yaffs_UnlinkObject(obj);
8005 +}
8006 +
8007 +/*----------------------- Initialisation Scanning ---------------------- */
8008 +
8009 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8010 + int backwardScanning)
8011 +{
8012 + yaffs_Object *obj;
8013 +
8014 + if (!backwardScanning) {
8015 + /* Handle YAFFS1 forward scanning case
8016 + * For YAFFS1 we always do the deletion
8017 + */
8018 +
8019 + } else {
8020 + /* Handle YAFFS2 case (backward scanning)
8021 + * If the shadowed object exists then ignore.
8022 + */
8023 + if (yaffs_FindObjectByNumber(dev, objId)) {
8024 + return;
8025 + }
8026 + }
8027 +
8028 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8029 + * We put it in unlinked dir to be cleaned up after the scanning
8030 + */
8031 + obj =
8032 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8033 + YAFFS_OBJECT_TYPE_FILE);
8034 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8035 + obj->variant.fileVariant.shrinkSize = 0;
8036 + obj->valid = 1; /* So that we don't read any other info for this file */
8037 +
8038 +}
8039 +
8040 +typedef struct {
8041 + int seq;
8042 + int block;
8043 +} yaffs_BlockIndex;
8044 +
8045 +
8046 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8047 +{
8048 + yaffs_Object *hl;
8049 + yaffs_Object *in;
8050 +
8051 + while (hardList) {
8052 + hl = hardList;
8053 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8054 +
8055 + in = yaffs_FindObjectByNumber(dev,
8056 + hl->variant.hardLinkVariant.
8057 + equivalentObjectId);
8058 +
8059 + if (in) {
8060 + /* Add the hardlink pointers */
8061 + hl->variant.hardLinkVariant.equivalentObject = in;
8062 + list_add(&hl->hardLinks, &in->hardLinks);
8063 + } else {
8064 + /* Todo Need to report/handle this better.
8065 + * Got a problem... hardlink to a non-existant object
8066 + */
8067 + hl->variant.hardLinkVariant.equivalentObject = NULL;
8068 + INIT_LIST_HEAD(&hl->hardLinks);
8069 +
8070 + }
8071 +
8072 + }
8073 +
8074 +}
8075 +
8076 +
8077 +
8078 +
8079 +
8080 +static int ybicmp(const void *a, const void *b){
8081 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
8082 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
8083 + register int ablock = ((yaffs_BlockIndex *)a)->block;
8084 + register int bblock = ((yaffs_BlockIndex *)b)->block;
8085 + if( aseq == bseq )
8086 + return ablock - bblock;
8087 + else
8088 + return aseq - bseq;
8089 +
8090 +}
8091 +
8092 +static int yaffs_Scan(yaffs_Device * dev)
8093 +{
8094 + yaffs_ExtendedTags tags;
8095 + int blk;
8096 + int blockIterator;
8097 + int startIterator;
8098 + int endIterator;
8099 + int nBlocksToScan = 0;
8100 + int result;
8101 +
8102 + int chunk;
8103 + int c;
8104 + int deleted;
8105 + yaffs_BlockState state;
8106 + yaffs_Object *hardList = NULL;
8107 + yaffs_Object *hl;
8108 + yaffs_BlockInfo *bi;
8109 + int sequenceNumber;
8110 + yaffs_ObjectHeader *oh;
8111 + yaffs_Object *in;
8112 + yaffs_Object *parent;
8113 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
8114 +
8115 + __u8 *chunkData;
8116 +
8117 + yaffs_BlockIndex *blockIndex = NULL;
8118 +
8119 + if (dev->isYaffs2) {
8120 + T(YAFFS_TRACE_SCAN,
8121 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
8122 + return YAFFS_FAIL;
8123 + }
8124 +
8125 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
8126 +
8127 + T(YAFFS_TRACE_SCAN,
8128 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
8129 + dev->internalStartBlock, dev->internalEndBlock));
8130 +
8131 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8132 +
8133 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
8134 +
8135 + if (dev->isYaffs2) {
8136 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
8137 + }
8138 +
8139 + /* Scan all the blocks to determine their state */
8140 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
8141 + bi = yaffs_GetBlockInfo(dev, blk);
8142 + yaffs_ClearChunkBits(dev, blk);
8143 + bi->pagesInUse = 0;
8144 + bi->softDeletions = 0;
8145 +
8146 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
8147 +
8148 + bi->blockState = state;
8149 + bi->sequenceNumber = sequenceNumber;
8150 +
8151 + T(YAFFS_TRACE_SCAN_DEBUG,
8152 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
8153 + state, sequenceNumber));
8154 +
8155 + if (state == YAFFS_BLOCK_STATE_DEAD) {
8156 + T(YAFFS_TRACE_BAD_BLOCKS,
8157 + (TSTR("block %d is bad" TENDSTR), blk));
8158 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
8159 + T(YAFFS_TRACE_SCAN_DEBUG,
8160 + (TSTR("Block empty " TENDSTR)));
8161 + dev->nErasedBlocks++;
8162 + dev->nFreeChunks += dev->nChunksPerBlock;
8163 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8164 +
8165 + /* Determine the highest sequence number */
8166 + if (dev->isYaffs2 &&
8167 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
8168 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
8169 +
8170 + blockIndex[nBlocksToScan].seq = sequenceNumber;
8171 + blockIndex[nBlocksToScan].block = blk;
8172 +
8173 + nBlocksToScan++;
8174 +
8175 + if (sequenceNumber >= dev->sequenceNumber) {
8176 + dev->sequenceNumber = sequenceNumber;
8177 + }
8178 + } else if (dev->isYaffs2) {
8179 + /* TODO: Nasty sequence number! */
8180 + T(YAFFS_TRACE_SCAN,
8181 + (TSTR
8182 + ("Block scanning block %d has bad sequence number %d"
8183 + TENDSTR), blk, sequenceNumber));
8184 +
8185 + }
8186 + }
8187 + }
8188 +
8189 + /* Sort the blocks
8190 + * Dungy old bubble sort for now...
8191 + */
8192 + if (dev->isYaffs2) {
8193 + yaffs_BlockIndex temp;
8194 + int i;
8195 + int j;
8196 +
8197 + for (i = 0; i < nBlocksToScan; i++)
8198 + for (j = i + 1; j < nBlocksToScan; j++)
8199 + if (blockIndex[i].seq > blockIndex[j].seq) {
8200 + temp = blockIndex[j];
8201 + blockIndex[j] = blockIndex[i];
8202 + blockIndex[i] = temp;
8203 + }
8204 + }
8205 +
8206 + /* Now scan the blocks looking at the data. */
8207 + if (dev->isYaffs2) {
8208 + startIterator = 0;
8209 + endIterator = nBlocksToScan - 1;
8210 + T(YAFFS_TRACE_SCAN_DEBUG,
8211 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
8212 + } else {
8213 + startIterator = dev->internalStartBlock;
8214 + endIterator = dev->internalEndBlock;
8215 + }
8216 +
8217 + /* For each block.... */
8218 + for (blockIterator = startIterator; blockIterator <= endIterator;
8219 + blockIterator++) {
8220 +
8221 + if (dev->isYaffs2) {
8222 + /* get the block to scan in the correct order */
8223 + blk = blockIndex[blockIterator].block;
8224 + } else {
8225 + blk = blockIterator;
8226 + }
8227 +
8228 + bi = yaffs_GetBlockInfo(dev, blk);
8229 + state = bi->blockState;
8230 +
8231 + deleted = 0;
8232 +
8233 + /* For each chunk in each block that needs scanning....*/
8234 + for (c = 0; c < dev->nChunksPerBlock &&
8235 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
8236 + /* Read the tags and decide what to do */
8237 + chunk = blk * dev->nChunksPerBlock + c;
8238 +
8239 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
8240 + &tags);
8241 +
8242 + /* Let's have a good look at this chunk... */
8243 +
8244 + if (!dev->isYaffs2 && tags.chunkDeleted) {
8245 + /* YAFFS1 only...
8246 + * A deleted chunk
8247 + */
8248 + deleted++;
8249 + dev->nFreeChunks++;
8250 + /*T((" %d %d deleted\n",blk,c)); */
8251 + } else if (!tags.chunkUsed) {
8252 + /* An unassigned chunk in the block
8253 + * This means that either the block is empty or
8254 + * this is the one being allocated from
8255 + */
8256 +
8257 + if (c == 0) {
8258 + /* We're looking at the first chunk in the block so the block is unused */
8259 + state = YAFFS_BLOCK_STATE_EMPTY;
8260 + dev->nErasedBlocks++;
8261 + } else {
8262 + /* this is the block being allocated from */
8263 + T(YAFFS_TRACE_SCAN,
8264 + (TSTR
8265 + (" Allocating from %d %d" TENDSTR),
8266 + blk, c));
8267 + state = YAFFS_BLOCK_STATE_ALLOCATING;
8268 + dev->allocationBlock = blk;
8269 + dev->allocationPage = c;
8270 + dev->allocationBlockFinder = blk;
8271 + /* Set it to here to encourage the allocator to go forth from here. */
8272 +
8273 + /* Yaffs2 sanity check:
8274 + * This should be the one with the highest sequence number
8275 + */
8276 + if (dev->isYaffs2
8277 + && (dev->sequenceNumber !=
8278 + bi->sequenceNumber)) {
8279 + T(YAFFS_TRACE_ALWAYS,
8280 + (TSTR
8281 + ("yaffs: Allocation block %d was not highest sequence id:"
8282 + " block seq = %d, dev seq = %d"
8283 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
8284 + }
8285 + }
8286 +
8287 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
8288 + } else if (tags.chunkId > 0) {
8289 + /* chunkId > 0 so it is a data chunk... */
8290 + unsigned int endpos;
8291 +
8292 + yaffs_SetChunkBit(dev, blk, c);
8293 + bi->pagesInUse++;
8294 +
8295 + in = yaffs_FindOrCreateObjectByNumber(dev,
8296 + tags.
8297 + objectId,
8298 + YAFFS_OBJECT_TYPE_FILE);
8299 + /* PutChunkIntoFile checks for a clash (two data chunks with
8300 + * the same chunkId).
8301 + */
8302 + yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,
8303 + 1);
8304 + endpos =
8305 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
8306 + tags.byteCount;
8307 + if (in->variantType == YAFFS_OBJECT_TYPE_FILE
8308 + && in->variant.fileVariant.scannedFileSize <
8309 + endpos) {
8310 + in->variant.fileVariant.
8311 + scannedFileSize = endpos;
8312 + if (!dev->useHeaderFileSize) {
8313 + in->variant.fileVariant.
8314 + fileSize =
8315 + in->variant.fileVariant.
8316 + scannedFileSize;
8317 + }
8318 +
8319 + }
8320 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
8321 + } else {
8322 + /* chunkId == 0, so it is an ObjectHeader.
8323 + * Thus, we read in the object header and make the object
8324 + */
8325 + yaffs_SetChunkBit(dev, blk, c);
8326 + bi->pagesInUse++;
8327 +
8328 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
8329 + chunkData,
8330 + NULL);
8331 +
8332 + oh = (yaffs_ObjectHeader *) chunkData;
8333 +
8334 + in = yaffs_FindObjectByNumber(dev,
8335 + tags.objectId);
8336 + if (in && in->variantType != oh->type) {
8337 + /* This should not happen, but somehow
8338 + * Wev'e ended up with an objectId that has been reused but not yet
8339 + * deleted, and worse still it has changed type. Delete the old object.
8340 + */
8341 +
8342 + yaffs_DestroyObject(in);
8343 +
8344 + in = 0;
8345 + }
8346 +
8347 + in = yaffs_FindOrCreateObjectByNumber(dev,
8348 + tags.
8349 + objectId,
8350 + oh->type);
8351 +
8352 + if (oh->shadowsObject > 0) {
8353 + yaffs_HandleShadowedObject(dev,
8354 + oh->
8355 + shadowsObject,
8356 + 0);
8357 + }
8358 +
8359 + if (in->valid) {
8360 + /* We have already filled this one. We have a duplicate and need to resolve it. */
8361 +
8362 + unsigned existingSerial = in->serial;
8363 + unsigned newSerial = tags.serialNumber;
8364 +
8365 + if (dev->isYaffs2 ||
8366 + ((existingSerial + 1) & 3) ==
8367 + newSerial) {
8368 + /* Use new one - destroy the exisiting one */
8369 + yaffs_DeleteChunk(dev,
8370 + in->chunkId,
8371 + 1, __LINE__);
8372 + in->valid = 0;
8373 + } else {
8374 + /* Use existing - destroy this one. */
8375 + yaffs_DeleteChunk(dev, chunk, 1,
8376 + __LINE__);
8377 + }
8378 + }
8379 +
8380 + if (!in->valid &&
8381 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
8382 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
8383 + /* We only load some info, don't fiddle with directory structure */
8384 + in->valid = 1;
8385 + in->variantType = oh->type;
8386 +
8387 + in->yst_mode = oh->yst_mode;
8388 +#ifdef CONFIG_YAFFS_WINCE
8389 + in->win_atime[0] = oh->win_atime[0];
8390 + in->win_ctime[0] = oh->win_ctime[0];
8391 + in->win_mtime[0] = oh->win_mtime[0];
8392 + in->win_atime[1] = oh->win_atime[1];
8393 + in->win_ctime[1] = oh->win_ctime[1];
8394 + in->win_mtime[1] = oh->win_mtime[1];
8395 +#else
8396 + in->yst_uid = oh->yst_uid;
8397 + in->yst_gid = oh->yst_gid;
8398 + in->yst_atime = oh->yst_atime;
8399 + in->yst_mtime = oh->yst_mtime;
8400 + in->yst_ctime = oh->yst_ctime;
8401 + in->yst_rdev = oh->yst_rdev;
8402 +#endif
8403 + in->chunkId = chunk;
8404 +
8405 + } else if (!in->valid) {
8406 + /* we need to load this info */
8407 +
8408 + in->valid = 1;
8409 + in->variantType = oh->type;
8410 +
8411 + in->yst_mode = oh->yst_mode;
8412 +#ifdef CONFIG_YAFFS_WINCE
8413 + in->win_atime[0] = oh->win_atime[0];
8414 + in->win_ctime[0] = oh->win_ctime[0];
8415 + in->win_mtime[0] = oh->win_mtime[0];
8416 + in->win_atime[1] = oh->win_atime[1];
8417 + in->win_ctime[1] = oh->win_ctime[1];
8418 + in->win_mtime[1] = oh->win_mtime[1];
8419 +#else
8420 + in->yst_uid = oh->yst_uid;
8421 + in->yst_gid = oh->yst_gid;
8422 + in->yst_atime = oh->yst_atime;
8423 + in->yst_mtime = oh->yst_mtime;
8424 + in->yst_ctime = oh->yst_ctime;
8425 + in->yst_rdev = oh->yst_rdev;
8426 +#endif
8427 + in->chunkId = chunk;
8428 +
8429 + yaffs_SetObjectName(in, oh->name);
8430 + in->dirty = 0;
8431 +
8432 + /* directory stuff...
8433 + * hook up to parent
8434 + */
8435 +
8436 + parent =
8437 + yaffs_FindOrCreateObjectByNumber
8438 + (dev, oh->parentObjectId,
8439 + YAFFS_OBJECT_TYPE_DIRECTORY);
8440 + if (parent->variantType ==
8441 + YAFFS_OBJECT_TYPE_UNKNOWN) {
8442 + /* Set up as a directory */
8443 + parent->variantType =
8444 + YAFFS_OBJECT_TYPE_DIRECTORY;
8445 + INIT_LIST_HEAD(&parent->variant.
8446 + directoryVariant.
8447 + children);
8448 + } else if (parent->variantType !=
8449 + YAFFS_OBJECT_TYPE_DIRECTORY)
8450 + {
8451 + /* Hoosterman, another problem....
8452 + * We're trying to use a non-directory as a directory
8453 + */
8454 +
8455 + T(YAFFS_TRACE_ERROR,
8456 + (TSTR
8457 + ("yaffs tragedy: attempting to use non-directory as"
8458 + " a directory in scan. Put in lost+found."
8459 + TENDSTR)));
8460 + parent = dev->lostNFoundDir;
8461 + }
8462 +
8463 + yaffs_AddObjectToDirectory(parent, in);
8464 +
8465 + if (0 && (parent == dev->deletedDir ||
8466 + parent == dev->unlinkedDir)) {
8467 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
8468 + dev->nDeletedFiles++;
8469 + }
8470 + /* Note re hardlinks.
8471 + * Since we might scan a hardlink before its equivalent object is scanned
8472 + * we put them all in a list.
8473 + * After scanning is complete, we should have all the objects, so we run through this
8474 + * list and fix up all the chains.
8475 + */
8476 +
8477 + switch (in->variantType) {
8478 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8479 + /* Todo got a problem */
8480 + break;
8481 + case YAFFS_OBJECT_TYPE_FILE:
8482 + if (dev->isYaffs2
8483 + && oh->isShrink) {
8484 + /* Prune back the shrunken chunks */
8485 + yaffs_PruneResizedChunks
8486 + (in, oh->fileSize);
8487 + /* Mark the block as having a shrinkHeader */
8488 + bi->hasShrinkHeader = 1;
8489 + }
8490 +
8491 + if (dev->useHeaderFileSize)
8492 +
8493 + in->variant.fileVariant.
8494 + fileSize =
8495 + oh->fileSize;
8496 +
8497 + break;
8498 + case YAFFS_OBJECT_TYPE_HARDLINK:
8499 + in->variant.hardLinkVariant.
8500 + equivalentObjectId =
8501 + oh->equivalentObjectId;
8502 + in->hardLinks.next =
8503 + (struct list_head *)
8504 + hardList;
8505 + hardList = in;
8506 + break;
8507 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8508 + /* Do nothing */
8509 + break;
8510 + case YAFFS_OBJECT_TYPE_SPECIAL:
8511 + /* Do nothing */
8512 + break;
8513 + case YAFFS_OBJECT_TYPE_SYMLINK:
8514 + in->variant.symLinkVariant.
8515 + alias =
8516 + yaffs_CloneString(oh->alias);
8517 + break;
8518 + }
8519 +
8520 + if (parent == dev->deletedDir) {
8521 + yaffs_DestroyObject(in);
8522 + bi->hasShrinkHeader = 1;
8523 + }
8524 + }
8525 + }
8526 + }
8527 +
8528 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8529 + /* If we got this far while scanning, then the block is fully allocated.*/
8530 + state = YAFFS_BLOCK_STATE_FULL;
8531 + }
8532 +
8533 + bi->blockState = state;
8534 +
8535 + /* Now let's see if it was dirty */
8536 + if (bi->pagesInUse == 0 &&
8537 + !bi->hasShrinkHeader &&
8538 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
8539 + yaffs_BlockBecameDirty(dev, blk);
8540 + }
8541 +
8542 + }
8543 +
8544 + if (blockIndex) {
8545 + YFREE(blockIndex);
8546 + }
8547 +
8548 +
8549 + /* Ok, we've done all the scanning.
8550 + * Fix up the hard link chains.
8551 + * We should now have scanned all the objects, now it's time to add these
8552 + * hardlinks.
8553 + */
8554 +
8555 + yaffs_HardlinkFixup(dev,hardList);
8556 +
8557 + /* Handle the unlinked files. Since they were left in an unlinked state we should
8558 + * just delete them.
8559 + */
8560 + {
8561 + struct list_head *i;
8562 + struct list_head *n;
8563 +
8564 + yaffs_Object *l;
8565 + /* Soft delete all the unlinked files */
8566 + list_for_each_safe(i, n,
8567 + &dev->unlinkedDir->variant.directoryVariant.
8568 + children) {
8569 + if (i) {
8570 + l = list_entry(i, yaffs_Object, siblings);
8571 + yaffs_DestroyObject(l);
8572 + }
8573 + }
8574 + }
8575 +
8576 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
8577 +
8578 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
8579 +
8580 + return YAFFS_OK;
8581 +}
8582 +
8583 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
8584 +{
8585 + __u8 *chunkData;
8586 + yaffs_ObjectHeader *oh;
8587 + yaffs_Device *dev = in->myDev;
8588 + yaffs_ExtendedTags tags;
8589 + int result;
8590 +
8591 +#if 0
8592 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
8593 + in->objectId,
8594 + in->lazyLoaded ? "not yet" : "already"));
8595 +#endif
8596 +
8597 + if(in->lazyLoaded){
8598 + in->lazyLoaded = 0;
8599 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8600 +
8601 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
8602 + oh = (yaffs_ObjectHeader *) chunkData;
8603 +
8604 + in->yst_mode = oh->yst_mode;
8605 +#ifdef CONFIG_YAFFS_WINCE
8606 + in->win_atime[0] = oh->win_atime[0];
8607 + in->win_ctime[0] = oh->win_ctime[0];
8608 + in->win_mtime[0] = oh->win_mtime[0];
8609 + in->win_atime[1] = oh->win_atime[1];
8610 + in->win_ctime[1] = oh->win_ctime[1];
8611 + in->win_mtime[1] = oh->win_mtime[1];
8612 +#else
8613 + in->yst_uid = oh->yst_uid;
8614 + in->yst_gid = oh->yst_gid;
8615 + in->yst_atime = oh->yst_atime;
8616 + in->yst_mtime = oh->yst_mtime;
8617 + in->yst_ctime = oh->yst_ctime;
8618 + in->yst_rdev = oh->yst_rdev;
8619 +
8620 +#endif
8621 + yaffs_SetObjectName(in, oh->name);
8622 +
8623 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
8624 + in->variant.symLinkVariant.alias =
8625 + yaffs_CloneString(oh->alias);
8626 +
8627 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
8628 + }
8629 +}
8630 +
8631 +static int yaffs_ScanBackwards(yaffs_Device * dev)
8632 +{
8633 + yaffs_ExtendedTags tags;
8634 + int blk;
8635 + int blockIterator;
8636 + int startIterator;
8637 + int endIterator;
8638 + int nBlocksToScan = 0;
8639 +
8640 + int chunk;
8641 + int result;
8642 + int c;
8643 + int deleted;
8644 + yaffs_BlockState state;
8645 + yaffs_Object *hardList = NULL;
8646 + yaffs_BlockInfo *bi;
8647 + int sequenceNumber;
8648 + yaffs_ObjectHeader *oh;
8649 + yaffs_Object *in;
8650 + yaffs_Object *parent;
8651 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
8652 + int itsUnlinked;
8653 + __u8 *chunkData;
8654 +
8655 + int fileSize;
8656 + int isShrink;
8657 + int foundChunksInBlock;
8658 + int equivalentObjectId;
8659 +
8660 +
8661 + yaffs_BlockIndex *blockIndex = NULL;
8662 + int altBlockIndex = 0;
8663 +
8664 + if (!dev->isYaffs2) {
8665 + T(YAFFS_TRACE_SCAN,
8666 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
8667 + return YAFFS_FAIL;
8668 + }
8669 +
8670 + T(YAFFS_TRACE_SCAN,
8671 + (TSTR
8672 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
8673 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
8674 +
8675 +
8676 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
8677 +
8678 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
8679 +
8680 + if(!blockIndex) {
8681 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
8682 + altBlockIndex = 1;
8683 + }
8684 +
8685 + if(!blockIndex) {
8686 + T(YAFFS_TRACE_SCAN,
8687 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
8688 + return YAFFS_FAIL;
8689 + }
8690 +
8691 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
8692 +
8693 + /* Scan all the blocks to determine their state */
8694 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
8695 + bi = yaffs_GetBlockInfo(dev, blk);
8696 + yaffs_ClearChunkBits(dev, blk);
8697 + bi->pagesInUse = 0;
8698 + bi->softDeletions = 0;
8699 +
8700 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
8701 +
8702 + bi->blockState = state;
8703 + bi->sequenceNumber = sequenceNumber;
8704 +
8705 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
8706 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
8707 +
8708 + T(YAFFS_TRACE_SCAN_DEBUG,
8709 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
8710 + state, sequenceNumber));
8711 +
8712 +
8713 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
8714 + /* todo .. fix free space ? */
8715 +
8716 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
8717 + T(YAFFS_TRACE_BAD_BLOCKS,
8718 + (TSTR("block %d is bad" TENDSTR), blk));
8719 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
8720 + T(YAFFS_TRACE_SCAN_DEBUG,
8721 + (TSTR("Block empty " TENDSTR)));
8722 + dev->nErasedBlocks++;
8723 + dev->nFreeChunks += dev->nChunksPerBlock;
8724 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8725 +
8726 + /* Determine the highest sequence number */
8727 + if (dev->isYaffs2 &&
8728 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
8729 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
8730 +
8731 + blockIndex[nBlocksToScan].seq = sequenceNumber;
8732 + blockIndex[nBlocksToScan].block = blk;
8733 +
8734 + nBlocksToScan++;
8735 +
8736 + if (sequenceNumber >= dev->sequenceNumber) {
8737 + dev->sequenceNumber = sequenceNumber;
8738 + }
8739 + } else if (dev->isYaffs2) {
8740 + /* TODO: Nasty sequence number! */
8741 + T(YAFFS_TRACE_SCAN,
8742 + (TSTR
8743 + ("Block scanning block %d has bad sequence number %d"
8744 + TENDSTR), blk, sequenceNumber));
8745 +
8746 + }
8747 + }
8748 + }
8749 +
8750 + T(YAFFS_TRACE_SCAN,
8751 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
8752 +
8753 +
8754 +
8755 + YYIELD();
8756 +
8757 + /* Sort the blocks */
8758 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
8759 + {
8760 + /* Use qsort now. */
8761 + qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
8762 + }
8763 +#else
8764 + {
8765 + /* Dungy old bubble sort... */
8766 +
8767 + yaffs_BlockIndex temp;
8768 + int i;
8769 + int j;
8770 +
8771 + for (i = 0; i < nBlocksToScan; i++)
8772 + for (j = i + 1; j < nBlocksToScan; j++)
8773 + if (blockIndex[i].seq > blockIndex[j].seq) {
8774 + temp = blockIndex[j];
8775 + blockIndex[j] = blockIndex[i];
8776 + blockIndex[i] = temp;
8777 + }
8778 + }
8779 +#endif
8780 +
8781 + YYIELD();
8782 +
8783 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
8784 +
8785 + /* Now scan the blocks looking at the data. */
8786 + startIterator = 0;
8787 + endIterator = nBlocksToScan - 1;
8788 + T(YAFFS_TRACE_SCAN_DEBUG,
8789 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
8790 +
8791 + /* For each block.... backwards */
8792 + for (blockIterator = endIterator; blockIterator >= startIterator;
8793 + blockIterator--) {
8794 + /* Cooperative multitasking! This loop can run for so
8795 + long that watchdog timers expire. */
8796 + YYIELD();
8797 +
8798 + /* get the block to scan in the correct order */
8799 + blk = blockIndex[blockIterator].block;
8800 +
8801 + bi = yaffs_GetBlockInfo(dev, blk);
8802 + state = bi->blockState;
8803 +
8804 + deleted = 0;
8805 +
8806 + /* For each chunk in each block that needs scanning.... */
8807 + foundChunksInBlock = 0;
8808 + for (c = dev->nChunksPerBlock - 1; c >= 0 &&
8809 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
8810 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
8811 + /* Scan backwards...
8812 + * Read the tags and decide what to do
8813 + */
8814 + chunk = blk * dev->nChunksPerBlock + c;
8815 +
8816 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
8817 + &tags);
8818 +
8819 + /* Let's have a good look at this chunk... */
8820 +
8821 + if (!tags.chunkUsed) {
8822 + /* An unassigned chunk in the block.
8823 + * If there are used chunks after this one, then
8824 + * it is a chunk that was skipped due to failing the erased
8825 + * check. Just skip it so that it can be deleted.
8826 + * But, more typically, We get here when this is an unallocated
8827 + * chunk and his means that either the block is empty or
8828 + * this is the one being allocated from
8829 + */
8830 +
8831 + if(foundChunksInBlock)
8832 + {
8833 + /* This is a chunk that was skipped due to failing the erased check */
8834 +
8835 + } else if (c == 0) {
8836 + /* We're looking at the first chunk in the block so the block is unused */
8837 + state = YAFFS_BLOCK_STATE_EMPTY;
8838 + dev->nErasedBlocks++;
8839 + } else {
8840 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
8841 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
8842 + if(dev->sequenceNumber == bi->sequenceNumber) {
8843 + /* this is the block being allocated from */
8844 +
8845 + T(YAFFS_TRACE_SCAN,
8846 + (TSTR
8847 + (" Allocating from %d %d"
8848 + TENDSTR), blk, c));
8849 +
8850 + state = YAFFS_BLOCK_STATE_ALLOCATING;
8851 + dev->allocationBlock = blk;
8852 + dev->allocationPage = c;
8853 + dev->allocationBlockFinder = blk;
8854 + }
8855 + else {
8856 + /* This is a partially written block that is not
8857 + * the current allocation block. This block must have
8858 + * had a write failure, so set up for retirement.
8859 + */
8860 +
8861 + bi->needsRetiring = 1;
8862 + bi->gcPrioritise = 1;
8863 +
8864 + T(YAFFS_TRACE_ALWAYS,
8865 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
8866 + blk));
8867 + }
8868 +
8869 + }
8870 +
8871 + }
8872 +
8873 + dev->nFreeChunks++;
8874 +
8875 + } else if (tags.chunkId > 0) {
8876 + /* chunkId > 0 so it is a data chunk... */
8877 + unsigned int endpos;
8878 + __u32 chunkBase =
8879 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
8880 +
8881 + foundChunksInBlock = 1;
8882 +
8883 +
8884 + yaffs_SetChunkBit(dev, blk, c);
8885 + bi->pagesInUse++;
8886 +
8887 + in = yaffs_FindOrCreateObjectByNumber(dev,
8888 + tags.
8889 + objectId,
8890 + YAFFS_OBJECT_TYPE_FILE);
8891 + if (in->variantType == YAFFS_OBJECT_TYPE_FILE
8892 + && chunkBase <
8893 + in->variant.fileVariant.shrinkSize) {
8894 + /* This has not been invalidated by a resize */
8895 + yaffs_PutChunkIntoFile(in, tags.chunkId,
8896 + chunk, -1);
8897 +
8898 + /* File size is calculated by looking at the data chunks if we have not
8899 + * seen an object header yet. Stop this practice once we find an object header.
8900 + */
8901 + endpos =
8902 + (tags.chunkId -
8903 + 1) * dev->nDataBytesPerChunk +
8904 + tags.byteCount;
8905 +
8906 + if (!in->valid && /* have not got an object header yet */
8907 + in->variant.fileVariant.
8908 + scannedFileSize < endpos) {
8909 + in->variant.fileVariant.
8910 + scannedFileSize = endpos;
8911 + in->variant.fileVariant.
8912 + fileSize =
8913 + in->variant.fileVariant.
8914 + scannedFileSize;
8915 + }
8916 +
8917 + } else {
8918 + /* This chunk has been invalidated by a resize, so delete */
8919 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
8920 +
8921 + }
8922 + } else {
8923 + /* chunkId == 0, so it is an ObjectHeader.
8924 + * Thus, we read in the object header and make the object
8925 + */
8926 + foundChunksInBlock = 1;
8927 +
8928 + yaffs_SetChunkBit(dev, blk, c);
8929 + bi->pagesInUse++;
8930 +
8931 + oh = NULL;
8932 + in = NULL;
8933 +
8934 + if (tags.extraHeaderInfoAvailable) {
8935 + in = yaffs_FindOrCreateObjectByNumber
8936 + (dev, tags.objectId,
8937 + tags.extraObjectType);
8938 + }
8939 +
8940 + if (!in ||
8941 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
8942 + !in->valid ||
8943 +#endif
8944 + tags.extraShadows ||
8945 + (!in->valid &&
8946 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
8947 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
8948 + ) {
8949 +
8950 + /* If we don't have valid info then we need to read the chunk
8951 + * TODO In future we can probably defer reading the chunk and
8952 + * living with invalid data until needed.
8953 + */
8954 +
8955 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
8956 + chunk,
8957 + chunkData,
8958 + NULL);
8959 +
8960 + oh = (yaffs_ObjectHeader *) chunkData;
8961 +
8962 + if (!in)
8963 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
8964 +
8965 + }
8966 +
8967 + if (!in) {
8968 + /* TODO Hoosterman we have a problem! */
8969 + T(YAFFS_TRACE_ERROR,
8970 + (TSTR
8971 + ("yaffs tragedy: Could not make object for object %d "
8972 + "at chunk %d during scan"
8973 + TENDSTR), tags.objectId, chunk));
8974 +
8975 + }
8976 +
8977 + if (in->valid) {
8978 + /* We have already filled this one.
8979 + * We have a duplicate that will be discarded, but
8980 + * we first have to suck out resize info if it is a file.
8981 + */
8982 +
8983 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
8984 + ((oh &&
8985 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
8986 + (tags.extraHeaderInfoAvailable &&
8987 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
8988 + ) {
8989 + __u32 thisSize =
8990 + (oh) ? oh->fileSize : tags.
8991 + extraFileLength;
8992 + __u32 parentObjectId =
8993 + (oh) ? oh->
8994 + parentObjectId : tags.
8995 + extraParentObjectId;
8996 + unsigned isShrink =
8997 + (oh) ? oh->isShrink : tags.
8998 + extraIsShrinkHeader;
8999 +
9000 + /* If it is deleted (unlinked at start also means deleted)
9001 + * we treat the file size as being zeroed at this point.
9002 + */
9003 + if (parentObjectId ==
9004 + YAFFS_OBJECTID_DELETED
9005 + || parentObjectId ==
9006 + YAFFS_OBJECTID_UNLINKED) {
9007 + thisSize = 0;
9008 + isShrink = 1;
9009 + }
9010 +
9011 + if (isShrink &&
9012 + in->variant.fileVariant.
9013 + shrinkSize > thisSize) {
9014 + in->variant.fileVariant.
9015 + shrinkSize =
9016 + thisSize;
9017 + }
9018 +
9019 + if (isShrink) {
9020 + bi->hasShrinkHeader = 1;
9021 + }
9022 +
9023 + }
9024 + /* Use existing - destroy this one. */
9025 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9026 +
9027 + }
9028 +
9029 + if (!in->valid &&
9030 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9031 + tags.objectId ==
9032 + YAFFS_OBJECTID_LOSTNFOUND)) {
9033 + /* We only load some info, don't fiddle with directory structure */
9034 + in->valid = 1;
9035 +
9036 + if(oh) {
9037 + in->variantType = oh->type;
9038 +
9039 + in->yst_mode = oh->yst_mode;
9040 +#ifdef CONFIG_YAFFS_WINCE
9041 + in->win_atime[0] = oh->win_atime[0];
9042 + in->win_ctime[0] = oh->win_ctime[0];
9043 + in->win_mtime[0] = oh->win_mtime[0];
9044 + in->win_atime[1] = oh->win_atime[1];
9045 + in->win_ctime[1] = oh->win_ctime[1];
9046 + in->win_mtime[1] = oh->win_mtime[1];
9047 +#else
9048 + in->yst_uid = oh->yst_uid;
9049 + in->yst_gid = oh->yst_gid;
9050 + in->yst_atime = oh->yst_atime;
9051 + in->yst_mtime = oh->yst_mtime;
9052 + in->yst_ctime = oh->yst_ctime;
9053 + in->yst_rdev = oh->yst_rdev;
9054 +
9055 +#endif
9056 + } else {
9057 + in->variantType = tags.extraObjectType;
9058 + in->lazyLoaded = 1;
9059 + }
9060 +
9061 + in->chunkId = chunk;
9062 +
9063 + } else if (!in->valid) {
9064 + /* we need to load this info */
9065 +
9066 + in->valid = 1;
9067 + in->chunkId = chunk;
9068 +
9069 + if(oh) {
9070 + in->variantType = oh->type;
9071 +
9072 + in->yst_mode = oh->yst_mode;
9073 +#ifdef CONFIG_YAFFS_WINCE
9074 + in->win_atime[0] = oh->win_atime[0];
9075 + in->win_ctime[0] = oh->win_ctime[0];
9076 + in->win_mtime[0] = oh->win_mtime[0];
9077 + in->win_atime[1] = oh->win_atime[1];
9078 + in->win_ctime[1] = oh->win_ctime[1];
9079 + in->win_mtime[1] = oh->win_mtime[1];
9080 +#else
9081 + in->yst_uid = oh->yst_uid;
9082 + in->yst_gid = oh->yst_gid;
9083 + in->yst_atime = oh->yst_atime;
9084 + in->yst_mtime = oh->yst_mtime;
9085 + in->yst_ctime = oh->yst_ctime;
9086 + in->yst_rdev = oh->yst_rdev;
9087 +#endif
9088 +
9089 + if (oh->shadowsObject > 0)
9090 + yaffs_HandleShadowedObject(dev,
9091 + oh->
9092 + shadowsObject,
9093 + 1);
9094 +
9095 +
9096 + yaffs_SetObjectName(in, oh->name);
9097 + parent =
9098 + yaffs_FindOrCreateObjectByNumber
9099 + (dev, oh->parentObjectId,
9100 + YAFFS_OBJECT_TYPE_DIRECTORY);
9101 +
9102 + fileSize = oh->fileSize;
9103 + isShrink = oh->isShrink;
9104 + equivalentObjectId = oh->equivalentObjectId;
9105 +
9106 + }
9107 + else {
9108 + in->variantType = tags.extraObjectType;
9109 + parent =
9110 + yaffs_FindOrCreateObjectByNumber
9111 + (dev, tags.extraParentObjectId,
9112 + YAFFS_OBJECT_TYPE_DIRECTORY);
9113 + fileSize = tags.extraFileLength;
9114 + isShrink = tags.extraIsShrinkHeader;
9115 + equivalentObjectId = tags.extraEquivalentObjectId;
9116 + in->lazyLoaded = 1;
9117 +
9118 + }
9119 + in->dirty = 0;
9120 +
9121 + /* directory stuff...
9122 + * hook up to parent
9123 + */
9124 +
9125 + if (parent->variantType ==
9126 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9127 + /* Set up as a directory */
9128 + parent->variantType =
9129 + YAFFS_OBJECT_TYPE_DIRECTORY;
9130 + INIT_LIST_HEAD(&parent->variant.
9131 + directoryVariant.
9132 + children);
9133 + } else if (parent->variantType !=
9134 + YAFFS_OBJECT_TYPE_DIRECTORY)
9135 + {
9136 + /* Hoosterman, another problem....
9137 + * We're trying to use a non-directory as a directory
9138 + */
9139 +
9140 + T(YAFFS_TRACE_ERROR,
9141 + (TSTR
9142 + ("yaffs tragedy: attempting to use non-directory as"
9143 + " a directory in scan. Put in lost+found."
9144 + TENDSTR)));
9145 + parent = dev->lostNFoundDir;
9146 + }
9147 +
9148 + yaffs_AddObjectToDirectory(parent, in);
9149 +
9150 + itsUnlinked = (parent == dev->deletedDir) ||
9151 + (parent == dev->unlinkedDir);
9152 +
9153 + if (isShrink) {
9154 + /* Mark the block as having a shrinkHeader */
9155 + bi->hasShrinkHeader = 1;
9156 + }
9157 +
9158 + /* Note re hardlinks.
9159 + * Since we might scan a hardlink before its equivalent object is scanned
9160 + * we put them all in a list.
9161 + * After scanning is complete, we should have all the objects, so we run
9162 + * through this list and fix up all the chains.
9163 + */
9164 +
9165 + switch (in->variantType) {
9166 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9167 + /* Todo got a problem */
9168 + break;
9169 + case YAFFS_OBJECT_TYPE_FILE:
9170 +
9171 + if (in->variant.fileVariant.
9172 + scannedFileSize < fileSize) {
9173 + /* This covers the case where the file size is greater
9174 + * than where the data is
9175 + * This will happen if the file is resized to be larger
9176 + * than its current data extents.
9177 + */
9178 + in->variant.fileVariant.fileSize = fileSize;
9179 + in->variant.fileVariant.scannedFileSize =
9180 + in->variant.fileVariant.fileSize;
9181 + }
9182 +
9183 + if (isShrink &&
9184 + in->variant.fileVariant.shrinkSize > fileSize) {
9185 + in->variant.fileVariant.shrinkSize = fileSize;
9186 + }
9187 +
9188 + break;
9189 + case YAFFS_OBJECT_TYPE_HARDLINK:
9190 + if(!itsUnlinked) {
9191 + in->variant.hardLinkVariant.equivalentObjectId =
9192 + equivalentObjectId;
9193 + in->hardLinks.next =
9194 + (struct list_head *) hardList;
9195 + hardList = in;
9196 + }
9197 + break;
9198 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9199 + /* Do nothing */
9200 + break;
9201 + case YAFFS_OBJECT_TYPE_SPECIAL:
9202 + /* Do nothing */
9203 + break;
9204 + case YAFFS_OBJECT_TYPE_SYMLINK:
9205 + if(oh)
9206 + in->variant.symLinkVariant.alias =
9207 + yaffs_CloneString(oh->
9208 + alias);
9209 + break;
9210 + }
9211 +
9212 + }
9213 + }
9214 + }
9215 +
9216 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9217 + /* If we got this far while scanning, then the block is fully allocated. */
9218 + state = YAFFS_BLOCK_STATE_FULL;
9219 + }
9220 +
9221 + bi->blockState = state;
9222 +
9223 + /* Now let's see if it was dirty */
9224 + if (bi->pagesInUse == 0 &&
9225 + !bi->hasShrinkHeader &&
9226 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9227 + yaffs_BlockBecameDirty(dev, blk);
9228 + }
9229 +
9230 + }
9231 +
9232 + if (altBlockIndex)
9233 + YFREE_ALT(blockIndex);
9234 + else
9235 + YFREE(blockIndex);
9236 +
9237 + /* Ok, we've done all the scanning.
9238 + * Fix up the hard link chains.
9239 + * We should now have scanned all the objects, now it's time to add these
9240 + * hardlinks.
9241 + */
9242 + yaffs_HardlinkFixup(dev,hardList);
9243 +
9244 +
9245 + /*
9246 + * Sort out state of unlinked and deleted objects.
9247 + */
9248 + {
9249 + struct list_head *i;
9250 + struct list_head *n;
9251 +
9252 + yaffs_Object *l;
9253 +
9254 + /* Soft delete all the unlinked files */
9255 + list_for_each_safe(i, n,
9256 + &dev->unlinkedDir->variant.directoryVariant.
9257 + children) {
9258 + if (i) {
9259 + l = list_entry(i, yaffs_Object, siblings);
9260 + yaffs_DestroyObject(l);
9261 + }
9262 + }
9263 +
9264 + /* Soft delete all the deletedDir files */
9265 + list_for_each_safe(i, n,
9266 + &dev->deletedDir->variant.directoryVariant.
9267 + children) {
9268 + if (i) {
9269 + l = list_entry(i, yaffs_Object, siblings);
9270 + yaffs_DestroyObject(l);
9271 +
9272 + }
9273 + }
9274 + }
9275 +
9276 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9277 +
9278 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
9279 +
9280 + return YAFFS_OK;
9281 +}
9282 +
9283 +/*------------------------------ Directory Functions ----------------------------- */
9284 +
9285 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
9286 +{
9287 + yaffs_Device *dev = obj->myDev;
9288 +
9289 + if(dev && dev->removeObjectCallback)
9290 + dev->removeObjectCallback(obj);
9291 +
9292 + list_del_init(&obj->siblings);
9293 + obj->parent = NULL;
9294 +}
9295 +
9296 +
9297 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
9298 + yaffs_Object * obj)
9299 +{
9300 +
9301 + if (!directory) {
9302 + T(YAFFS_TRACE_ALWAYS,
9303 + (TSTR
9304 + ("tragedy: Trying to add an object to a null pointer directory"
9305 + TENDSTR)));
9306 + YBUG();
9307 + }
9308 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9309 + T(YAFFS_TRACE_ALWAYS,
9310 + (TSTR
9311 + ("tragedy: Trying to add an object to a non-directory"
9312 + TENDSTR)));
9313 + YBUG();
9314 + }
9315 +
9316 + if (obj->siblings.prev == NULL) {
9317 + /* Not initialised */
9318 + INIT_LIST_HEAD(&obj->siblings);
9319 +
9320 + } else if (!list_empty(&obj->siblings)) {
9321 + /* If it is holed up somewhere else, un hook it */
9322 + yaffs_RemoveObjectFromDirectory(obj);
9323 + }
9324 + /* Now add it */
9325 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
9326 + obj->parent = directory;
9327 +
9328 + if (directory == obj->myDev->unlinkedDir
9329 + || directory == obj->myDev->deletedDir) {
9330 + obj->unlinked = 1;
9331 + obj->myDev->nUnlinkedFiles++;
9332 + obj->renameAllowed = 0;
9333 + }
9334 +}
9335 +
9336 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
9337 + const YCHAR * name)
9338 +{
9339 + int sum;
9340 +
9341 + struct list_head *i;
9342 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
9343 +
9344 + yaffs_Object *l;
9345 +
9346 + if (!name) {
9347 + return NULL;
9348 + }
9349 +
9350 + if (!directory) {
9351 + T(YAFFS_TRACE_ALWAYS,
9352 + (TSTR
9353 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
9354 + TENDSTR)));
9355 + YBUG();
9356 + }
9357 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9358 + T(YAFFS_TRACE_ALWAYS,
9359 + (TSTR
9360 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
9361 + YBUG();
9362 + }
9363 +
9364 + sum = yaffs_CalcNameSum(name);
9365 +
9366 + list_for_each(i, &directory->variant.directoryVariant.children) {
9367 + if (i) {
9368 + l = list_entry(i, yaffs_Object, siblings);
9369 +
9370 + yaffs_CheckObjectDetailsLoaded(l);
9371 +
9372 + /* Special case for lost-n-found */
9373 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
9374 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
9375 + return l;
9376 + }
9377 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
9378 + {
9379 + /* LostnFound cunk called Objxxx
9380 + * Do a real check
9381 + */
9382 + yaffs_GetObjectName(l, buffer,
9383 + YAFFS_MAX_NAME_LENGTH);
9384 + if (yaffs_strcmp(name, buffer) == 0) {
9385 + return l;
9386 + }
9387 +
9388 + }
9389 + }
9390 + }
9391 +
9392 + return NULL;
9393 +}
9394 +
9395 +
9396 +#if 0
9397 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
9398 + int (*fn) (yaffs_Object *))
9399 +{
9400 + struct list_head *i;
9401 + yaffs_Object *l;
9402 +
9403 + if (!theDir) {
9404 + T(YAFFS_TRACE_ALWAYS,
9405 + (TSTR
9406 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
9407 + TENDSTR)));
9408 + YBUG();
9409 + }
9410 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9411 + T(YAFFS_TRACE_ALWAYS,
9412 + (TSTR
9413 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
9414 + YBUG();
9415 + }
9416 +
9417 + list_for_each(i, &theDir->variant.directoryVariant.children) {
9418 + if (i) {
9419 + l = list_entry(i, yaffs_Object, siblings);
9420 + if (l && !fn(l)) {
9421 + return YAFFS_FAIL;
9422 + }
9423 + }
9424 + }
9425 +
9426 + return YAFFS_OK;
9427 +
9428 +}
9429 +#endif
9430 +
9431 +/* GetEquivalentObject dereferences any hard links to get to the
9432 + * actual object.
9433 + */
9434 +
9435 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
9436 +{
9437 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
9438 + /* We want the object id of the equivalent object, not this one */
9439 + obj = obj->variant.hardLinkVariant.equivalentObject;
9440 + }
9441 + return obj;
9442 +
9443 +}
9444 +
9445 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
9446 +{
9447 + memset(name, 0, buffSize * sizeof(YCHAR));
9448 +
9449 + yaffs_CheckObjectDetailsLoaded(obj);
9450 +
9451 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
9452 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
9453 + } else if (obj->chunkId <= 0) {
9454 + YCHAR locName[20];
9455 + /* make up a name */
9456 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
9457 + obj->objectId);
9458 + yaffs_strncpy(name, locName, buffSize - 1);
9459 +
9460 + }
9461 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
9462 + else if (obj->shortName[0]) {
9463 + yaffs_strcpy(name, obj->shortName);
9464 + }
9465 +#endif
9466 + else {
9467 + int result;
9468 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
9469 +
9470 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
9471 +
9472 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
9473 +
9474 + if (obj->chunkId >= 0) {
9475 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
9476 + obj->chunkId, buffer,
9477 + NULL);
9478 + }
9479 + yaffs_strncpy(name, oh->name, buffSize - 1);
9480 +
9481 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
9482 + }
9483 +
9484 + return yaffs_strlen(name);
9485 +}
9486 +
9487 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
9488 +{
9489 +
9490 + /* Dereference any hard linking */
9491 + obj = yaffs_GetEquivalentObject(obj);
9492 +
9493 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
9494 + return obj->variant.fileVariant.fileSize;
9495 + }
9496 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
9497 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
9498 + } else {
9499 + /* Only a directory should drop through to here */
9500 + return obj->myDev->nDataBytesPerChunk;
9501 + }
9502 +}
9503 +
9504 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
9505 +{
9506 + int count = 0;
9507 + struct list_head *i;
9508 +
9509 + if (!obj->unlinked) {
9510 + count++; /* the object itself */
9511 + }
9512 + list_for_each(i, &obj->hardLinks) {
9513 + count++; /* add the hard links; */
9514 + }
9515 + return count;
9516 +
9517 +}
9518 +
9519 +int yaffs_GetObjectInode(yaffs_Object * obj)
9520 +{
9521 + obj = yaffs_GetEquivalentObject(obj);
9522 +
9523 + return obj->objectId;
9524 +}
9525 +
9526 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
9527 +{
9528 + obj = yaffs_GetEquivalentObject(obj);
9529 +
9530 + switch (obj->variantType) {
9531 + case YAFFS_OBJECT_TYPE_FILE:
9532 + return DT_REG;
9533 + break;
9534 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9535 + return DT_DIR;
9536 + break;
9537 + case YAFFS_OBJECT_TYPE_SYMLINK:
9538 + return DT_LNK;
9539 + break;
9540 + case YAFFS_OBJECT_TYPE_HARDLINK:
9541 + return DT_REG;
9542 + break;
9543 + case YAFFS_OBJECT_TYPE_SPECIAL:
9544 + if (S_ISFIFO(obj->yst_mode))
9545 + return DT_FIFO;
9546 + if (S_ISCHR(obj->yst_mode))
9547 + return DT_CHR;
9548 + if (S_ISBLK(obj->yst_mode))
9549 + return DT_BLK;
9550 + if (S_ISSOCK(obj->yst_mode))
9551 + return DT_SOCK;
9552 + default:
9553 + return DT_REG;
9554 + break;
9555 + }
9556 +}
9557 +
9558 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
9559 +{
9560 + obj = yaffs_GetEquivalentObject(obj);
9561 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
9562 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
9563 + } else {
9564 + return yaffs_CloneString(_Y(""));
9565 + }
9566 +}
9567 +
9568 +#ifndef CONFIG_YAFFS_WINCE
9569 +
9570 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
9571 +{
9572 + unsigned int valid = attr->ia_valid;
9573 +
9574 + if (valid & ATTR_MODE)
9575 + obj->yst_mode = attr->ia_mode;
9576 + if (valid & ATTR_UID)
9577 + obj->yst_uid = attr->ia_uid;
9578 + if (valid & ATTR_GID)
9579 + obj->yst_gid = attr->ia_gid;
9580 +
9581 + if (valid & ATTR_ATIME)
9582 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
9583 + if (valid & ATTR_CTIME)
9584 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
9585 + if (valid & ATTR_MTIME)
9586 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
9587 +
9588 + if (valid & ATTR_SIZE)
9589 + yaffs_ResizeFile(obj, attr->ia_size);
9590 +
9591 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
9592 +
9593 + return YAFFS_OK;
9594 +
9595 +}
9596 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
9597 +{
9598 + unsigned int valid = 0;
9599 +
9600 + attr->ia_mode = obj->yst_mode;
9601 + valid |= ATTR_MODE;
9602 + attr->ia_uid = obj->yst_uid;
9603 + valid |= ATTR_UID;
9604 + attr->ia_gid = obj->yst_gid;
9605 + valid |= ATTR_GID;
9606 +
9607 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
9608 + valid |= ATTR_ATIME;
9609 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
9610 + valid |= ATTR_CTIME;
9611 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
9612 + valid |= ATTR_MTIME;
9613 +
9614 + attr->ia_size = yaffs_GetFileSize(obj);
9615 + valid |= ATTR_SIZE;
9616 +
9617 + attr->ia_valid = valid;
9618 +
9619 + return YAFFS_OK;
9620 +
9621 +}
9622 +
9623 +#endif
9624 +
9625 +#if 0
9626 +int yaffs_DumpObject(yaffs_Object * obj)
9627 +{
9628 + YCHAR name[257];
9629 +
9630 + yaffs_GetObjectName(obj, name, 256);
9631 +
9632 + T(YAFFS_TRACE_ALWAYS,
9633 + (TSTR
9634 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
9635 + " chunk %d type %d size %d\n"
9636 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
9637 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
9638 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
9639 +
9640 + return YAFFS_OK;
9641 +}
9642 +#endif
9643 +
9644 +/*---------------------------- Initialisation code -------------------------------------- */
9645 +
9646 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
9647 +{
9648 +
9649 + /* Common functions, gotta have */
9650 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
9651 + return 0;
9652 +
9653 +#ifdef CONFIG_YAFFS_YAFFS2
9654 +
9655 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
9656 + if (dev->writeChunkWithTagsToNAND &&
9657 + dev->readChunkWithTagsFromNAND &&
9658 + !dev->writeChunkToNAND &&
9659 + !dev->readChunkFromNAND &&
9660 + dev->markNANDBlockBad && dev->queryNANDBlock)
9661 + return 1;
9662 +#endif
9663 +
9664 + /* Can use the "spare" style interface for yaffs1 */
9665 + if (!dev->isYaffs2 &&
9666 + !dev->writeChunkWithTagsToNAND &&
9667 + !dev->readChunkWithTagsFromNAND &&
9668 + dev->writeChunkToNAND &&
9669 + dev->readChunkFromNAND &&
9670 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
9671 + return 1;
9672 +
9673 + return 0; /* bad */
9674 +}
9675 +
9676 +
9677 +static void yaffs_CreateInitialDirectories(yaffs_Device *dev)
9678 +{
9679 + /* Initialise the unlinked, deleted, root and lost and found directories */
9680 +
9681 + dev->lostNFoundDir = dev->rootDir = NULL;
9682 + dev->unlinkedDir = dev->deletedDir = NULL;
9683 +
9684 + dev->unlinkedDir =
9685 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
9686 + dev->deletedDir =
9687 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
9688 +
9689 + dev->rootDir =
9690 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
9691 + YAFFS_ROOT_MODE | S_IFDIR);
9692 + dev->lostNFoundDir =
9693 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
9694 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
9695 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
9696 +}
9697 +
9698 +int yaffs_GutsInitialise(yaffs_Device * dev)
9699 +{
9700 + unsigned x;
9701 + int bits;
9702 +
9703 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
9704 +
9705 + /* Check stuff that must be set */
9706 +
9707 + if (!dev) {
9708 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
9709 + return YAFFS_FAIL;
9710 + }
9711 +
9712 + dev->internalStartBlock = dev->startBlock;
9713 + dev->internalEndBlock = dev->endBlock;
9714 + dev->blockOffset = 0;
9715 + dev->chunkOffset = 0;
9716 + dev->nFreeChunks = 0;
9717 +
9718 + if (dev->startBlock == 0) {
9719 + dev->internalStartBlock = dev->startBlock + 1;
9720 + dev->internalEndBlock = dev->endBlock + 1;
9721 + dev->blockOffset = 1;
9722 + dev->chunkOffset = dev->nChunksPerBlock;
9723 + }
9724 +
9725 + /* Check geometry parameters. */
9726 +
9727 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
9728 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
9729 + dev->nChunksPerBlock < 2 ||
9730 + dev->nReservedBlocks < 2 ||
9731 + dev->internalStartBlock <= 0 ||
9732 + dev->internalEndBlock <= 0 ||
9733 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
9734 + ) {
9735 + T(YAFFS_TRACE_ALWAYS,
9736 + (TSTR
9737 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
9738 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
9739 + return YAFFS_FAIL;
9740 + }
9741 +
9742 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
9743 + T(YAFFS_TRACE_ALWAYS,
9744 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
9745 + return YAFFS_FAIL;
9746 + }
9747 +
9748 + /* Got the right mix of functions? */
9749 + if (!yaffs_CheckDevFunctions(dev)) {
9750 + /* Function missing */
9751 + T(YAFFS_TRACE_ALWAYS,
9752 + (TSTR
9753 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
9754 +
9755 + return YAFFS_FAIL;
9756 + }
9757 +
9758 + /* This is really a compilation check. */
9759 + if (!yaffs_CheckStructures()) {
9760 + T(YAFFS_TRACE_ALWAYS,
9761 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
9762 + return YAFFS_FAIL;
9763 + }
9764 +
9765 + if (dev->isMounted) {
9766 + T(YAFFS_TRACE_ALWAYS,
9767 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
9768 + return YAFFS_FAIL;
9769 + }
9770 +
9771 + /* Finished with most checks. One or two more checks happen later on too. */
9772 +
9773 + dev->isMounted = 1;
9774 +
9775 +
9776 +
9777 + /* OK now calculate a few things for the device */
9778 +
9779 + /*
9780 + * Calculate all the chunk size manipulation numbers:
9781 + */
9782 + /* Start off assuming it is a power of 2 */
9783 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
9784 + dev->chunkMask = (1<<dev->chunkShift) - 1;
9785 +
9786 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
9787 + /* Yes it is a power of 2, disable crumbs */
9788 + dev->crumbMask = 0;
9789 + dev->crumbShift = 0;
9790 + dev->crumbsPerChunk = 0;
9791 + } else {
9792 + /* Not a power of 2, use crumbs instead */
9793 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
9794 + dev->crumbMask = (1<<dev->crumbShift)-1;
9795 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
9796 + dev->chunkShift = 0;
9797 + dev->chunkMask = 0;
9798 + }
9799 +
9800 +
9801 + /*
9802 + * Calculate chunkGroupBits.
9803 + * We need to find the next power of 2 > than internalEndBlock
9804 + */
9805 +
9806 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
9807 +
9808 + bits = ShiftsGE(x);
9809 +
9810 + /* Set up tnode width if wide tnodes are enabled. */
9811 + if(!dev->wideTnodesDisabled){
9812 + /* bits must be even so that we end up with 32-bit words */
9813 + if(bits & 1)
9814 + bits++;
9815 + if(bits < 16)
9816 + dev->tnodeWidth = 16;
9817 + else
9818 + dev->tnodeWidth = bits;
9819 + }
9820 + else
9821 + dev->tnodeWidth = 16;
9822 +
9823 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
9824 +
9825 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
9826 + * so if the bitwidth of the
9827 + * chunk range we're using is greater than 16 we need
9828 + * to figure out chunk shift and chunkGroupSize
9829 + */
9830 +
9831 + if (bits <= dev->tnodeWidth)
9832 + dev->chunkGroupBits = 0;
9833 + else
9834 + dev->chunkGroupBits = bits - dev->tnodeWidth;
9835 +
9836 +
9837 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
9838 +
9839 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
9840 + /* We have a problem because the soft delete won't work if
9841 + * the chunk group size > chunks per block.
9842 + * This can be remedied by using larger "virtual blocks".
9843 + */
9844 + T(YAFFS_TRACE_ALWAYS,
9845 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
9846 +
9847 + return YAFFS_FAIL;
9848 + }
9849 +
9850 + /* OK, we've finished verifying the device, lets continue with initialisation */
9851 +
9852 + /* More device initialisation */
9853 + dev->garbageCollections = 0;
9854 + dev->passiveGarbageCollections = 0;
9855 + dev->currentDirtyChecker = 0;
9856 + dev->bufferedBlock = -1;
9857 + dev->doingBufferedBlockRewrite = 0;
9858 + dev->nDeletedFiles = 0;
9859 + dev->nBackgroundDeletions = 0;
9860 + dev->nUnlinkedFiles = 0;
9861 + dev->eccFixed = 0;
9862 + dev->eccUnfixed = 0;
9863 + dev->tagsEccFixed = 0;
9864 + dev->tagsEccUnfixed = 0;
9865 + dev->nErasureFailures = 0;
9866 + dev->nErasedBlocks = 0;
9867 + dev->isDoingGC = 0;
9868 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
9869 +
9870 + /* Initialise temporary buffers and caches. */
9871 + {
9872 + int i;
9873 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
9874 + dev->tempBuffer[i].line = 0; /* not in use */
9875 + dev->tempBuffer[i].buffer =
9876 + YMALLOC_DMA(dev->nDataBytesPerChunk);
9877 + }
9878 + }
9879 +
9880 + if (dev->nShortOpCaches > 0) {
9881 + int i;
9882 +
9883 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
9884 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
9885 + }
9886 +
9887 + dev->srCache =
9888 + YMALLOC(dev->nShortOpCaches * sizeof(yaffs_ChunkCache));
9889 +
9890 + for (i = 0; i < dev->nShortOpCaches; i++) {
9891 + dev->srCache[i].object = NULL;
9892 + dev->srCache[i].lastUse = 0;
9893 + dev->srCache[i].dirty = 0;
9894 + dev->srCache[i].data = YMALLOC_DMA(dev->nDataBytesPerChunk);
9895 + }
9896 + dev->srLastUse = 0;
9897 + }
9898 +
9899 + dev->cacheHits = 0;
9900 +
9901 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
9902 +
9903 + if (dev->isYaffs2) {
9904 + dev->useHeaderFileSize = 1;
9905 + }
9906 +
9907 + yaffs_InitialiseBlocks(dev);
9908 + yaffs_InitialiseTnodes(dev);
9909 + yaffs_InitialiseObjects(dev);
9910 +
9911 + yaffs_CreateInitialDirectories(dev);
9912 +
9913 +
9914 + /* Now scan the flash. */
9915 + if (dev->isYaffs2) {
9916 + if(yaffs_CheckpointRestore(dev)) {
9917 + T(YAFFS_TRACE_CHECKPOINT,
9918 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
9919 + } else {
9920 +
9921 + /* Clean up the mess caused by an aborted checkpoint load
9922 + * and scan backwards.
9923 + */
9924 + yaffs_DeinitialiseBlocks(dev);
9925 + yaffs_DeinitialiseTnodes(dev);
9926 + yaffs_DeinitialiseObjects(dev);
9927 + yaffs_InitialiseBlocks(dev);
9928 + yaffs_InitialiseTnodes(dev);
9929 + yaffs_InitialiseObjects(dev);
9930 + yaffs_CreateInitialDirectories(dev);
9931 +
9932 + yaffs_ScanBackwards(dev);
9933 + }
9934 + }else
9935 + yaffs_Scan(dev);
9936 +
9937 + /* Zero out stats */
9938 + dev->nPageReads = 0;
9939 + dev->nPageWrites = 0;
9940 + dev->nBlockErasures = 0;
9941 + dev->nGCCopies = 0;
9942 + dev->nRetriedWrites = 0;
9943 +
9944 + dev->nRetiredBlocks = 0;
9945 +
9946 + yaffs_VerifyFreeChunks(dev);
9947 +
9948 + T(YAFFS_TRACE_TRACING,
9949 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
9950 + return YAFFS_OK;
9951 +
9952 +}
9953 +
9954 +void yaffs_Deinitialise(yaffs_Device * dev)
9955 +{
9956 + if (dev->isMounted) {
9957 + int i;
9958 +
9959 + yaffs_DeinitialiseBlocks(dev);
9960 + yaffs_DeinitialiseTnodes(dev);
9961 + yaffs_DeinitialiseObjects(dev);
9962 + if (dev->nShortOpCaches > 0) {
9963 +
9964 + for (i = 0; i < dev->nShortOpCaches; i++) {
9965 + YFREE(dev->srCache[i].data);
9966 + }
9967 +
9968 + YFREE(dev->srCache);
9969 + }
9970 +
9971 + YFREE(dev->gcCleanupList);
9972 +
9973 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
9974 + YFREE(dev->tempBuffer[i].buffer);
9975 + }
9976 +
9977 + dev->isMounted = 0;
9978 + }
9979 +
9980 +}
9981 +
9982 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
9983 +{
9984 + int nFree;
9985 + int b;
9986 +
9987 + yaffs_BlockInfo *blk;
9988 +
9989 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
9990 + b++) {
9991 + blk = yaffs_GetBlockInfo(dev, b);
9992 +
9993 + switch (blk->blockState) {
9994 + case YAFFS_BLOCK_STATE_EMPTY:
9995 + case YAFFS_BLOCK_STATE_ALLOCATING:
9996 + case YAFFS_BLOCK_STATE_COLLECTING:
9997 + case YAFFS_BLOCK_STATE_FULL:
9998 + nFree +=
9999 + (dev->nChunksPerBlock - blk->pagesInUse +
10000 + blk->softDeletions);
10001 + break;
10002 + default:
10003 + break;
10004 + }
10005 +
10006 + }
10007 +
10008 + return nFree;
10009 +}
10010 +
10011 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
10012 +{
10013 + /* This is what we report to the outside world */
10014 +
10015 + int nFree;
10016 + int nDirtyCacheChunks;
10017 + int blocksForCheckpoint;
10018 +
10019 +#if 1
10020 + nFree = dev->nFreeChunks;
10021 +#else
10022 + nFree = yaffs_CountFreeChunks(dev);
10023 +#endif
10024 +
10025 + nFree += dev->nDeletedFiles;
10026 +
10027 + /* Now count the number of dirty chunks in the cache and subtract those */
10028 +
10029 + {
10030 + int i;
10031 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
10032 + if (dev->srCache[i].dirty)
10033 + nDirtyCacheChunks++;
10034 + }
10035 + }
10036 +
10037 + nFree -= nDirtyCacheChunks;
10038 +
10039 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
10040 +
10041 + /* Now we figure out how much to reserve for the checkpoint and report that... */
10042 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
10043 + if(blocksForCheckpoint < 0)
10044 + blocksForCheckpoint = 0;
10045 +
10046 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
10047 +
10048 + if (nFree < 0)
10049 + nFree = 0;
10050 +
10051 + return nFree;
10052 +
10053 +}
10054 +
10055 +static int yaffs_freeVerificationFailures;
10056 +
10057 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
10058 +{
10059 + int counted = yaffs_CountFreeChunks(dev);
10060 +
10061 + int difference = dev->nFreeChunks - counted;
10062 +
10063 + if (difference) {
10064 + T(YAFFS_TRACE_ALWAYS,
10065 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
10066 + dev->nFreeChunks, counted, difference));
10067 + yaffs_freeVerificationFailures++;
10068 + }
10069 +}
10070 +
10071 +/*---------------------------------------- YAFFS test code ----------------------*/
10072 +
10073 +#define yaffs_CheckStruct(structure,syze, name) \
10074 + if(sizeof(structure) != syze) \
10075 + { \
10076 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
10077 + name,syze,sizeof(structure))); \
10078 + return YAFFS_FAIL; \
10079 + }
10080 +
10081 +static int yaffs_CheckStructures(void)
10082 +{
10083 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
10084 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
10085 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
10086 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
10087 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
10088 +#endif
10089 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
10090 +
10091 + return YAFFS_OK;
10092 +}
10093 diff -urN linux.old/fs/yaffs2/yaffs_guts.h linux.dev/fs/yaffs2/yaffs_guts.h
10094 --- linux.old/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
10095 +++ linux.dev/fs/yaffs2/yaffs_guts.h 2006-12-14 04:21:47.000000000 +0100
10096 @@ -0,0 +1,893 @@
10097 +/*
10098 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
10099 + * yaffs_guts.h: Configuration etc for yaffs_guts
10100 + *
10101 + * Copyright (C) 2002 Aleph One Ltd.
10102 + * for Toby Churchill Ltd and Brightstar Engineering
10103 + *
10104 + * Created by Charles Manning <charles@aleph1.co.uk>
10105 + *
10106 + * This program is free software; you can redistribute it and/or modify
10107 + * it under the terms of the GNU Lesser General Public License version 2.1 as
10108 + * published by the Free Software Foundation.
10109 + *
10110 + *
10111 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
10112 + *
10113 + * $Id: yaffs_guts.h,v 1.25 2006/10/13 08:52:49 charles Exp $
10114 + */
10115 +
10116 +#ifndef __YAFFS_GUTS_H__
10117 +#define __YAFFS_GUTS_H__
10118 +
10119 +#include "devextras.h"
10120 +#include "yportenv.h"
10121 +
10122 +#define YAFFS_OK 1
10123 +#define YAFFS_FAIL 0
10124 +
10125 +/* Give us a Y=0x59,
10126 + * Give us an A=0x41,
10127 + * Give us an FF=0xFF
10128 + * Give us an S=0x53
10129 + * And what have we got...
10130 + */
10131 +#define YAFFS_MAGIC 0x5941FF53
10132 +
10133 +#define YAFFS_NTNODES_LEVEL0 16
10134 +#define YAFFS_TNODES_LEVEL0_BITS 4
10135 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
10136 +
10137 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
10138 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
10139 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
10140 +#define YAFFS_TNODES_MAX_LEVEL 6
10141 +
10142 +#ifndef CONFIG_YAFFS_NO_YAFFS1
10143 +#define YAFFS_BYTES_PER_SPARE 16
10144 +#define YAFFS_BYTES_PER_CHUNK 512
10145 +#define YAFFS_CHUNK_SIZE_SHIFT 9
10146 +#define YAFFS_CHUNKS_PER_BLOCK 32
10147 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
10148 +#endif
10149 +
10150 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
10151 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
10152 +
10153 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
10154 +
10155 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
10156 +
10157 +#define YAFFS_ALLOCATION_NOBJECTS 100
10158 +#define YAFFS_ALLOCATION_NTNODES 100
10159 +#define YAFFS_ALLOCATION_NLINKS 100
10160 +
10161 +#define YAFFS_NOBJECT_BUCKETS 256
10162 +
10163 +
10164 +#define YAFFS_OBJECT_SPACE 0x40000
10165 +
10166 +#define YAFFS_NCHECKPOINT_OBJECTS 5000
10167 +
10168 +#define YAFFS_CHECKPOINT_VERSION 2
10169 +
10170 +#ifdef CONFIG_YAFFS_UNICODE
10171 +#define YAFFS_MAX_NAME_LENGTH 127
10172 +#define YAFFS_MAX_ALIAS_LENGTH 79
10173 +#else
10174 +#define YAFFS_MAX_NAME_LENGTH 255
10175 +#define YAFFS_MAX_ALIAS_LENGTH 159
10176 +#endif
10177 +
10178 +#define YAFFS_SHORT_NAME_LENGTH 15
10179 +
10180 +/* Some special object ids for pseudo objects */
10181 +#define YAFFS_OBJECTID_ROOT 1
10182 +#define YAFFS_OBJECTID_LOSTNFOUND 2
10183 +#define YAFFS_OBJECTID_UNLINKED 3
10184 +#define YAFFS_OBJECTID_DELETED 4
10185 +
10186 +/* Sseudo object ids for checkpointing */
10187 +#define YAFFS_OBJECTID_SB_HEADER 0x10
10188 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
10189 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
10190 +
10191 +/* */
10192 +
10193 +#define YAFFS_MAX_SHORT_OP_CACHES 20
10194 +
10195 +#define YAFFS_N_TEMP_BUFFERS 4
10196 +
10197 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
10198 + * The range is limited slightly to help distinguish bad numbers from good.
10199 + * This also allows us to perhaps in the future use special numbers for
10200 + * special purposes.
10201 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
10202 + * and is a larger number than the lifetime of a 2GB device.
10203 + */
10204 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
10205 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
10206 +
10207 +/* ChunkCache is used for short read/write operations.*/
10208 +typedef struct {
10209 + struct yaffs_ObjectStruct *object;
10210 + int chunkId;
10211 + int lastUse;
10212 + int dirty;
10213 + int nBytes; /* Only valid if the cache is dirty */
10214 + int locked; /* Can't push out or flush while locked. */
10215 +#ifdef CONFIG_YAFFS_YAFFS2
10216 + __u8 *data;
10217 +#else
10218 + __u8 data[YAFFS_BYTES_PER_CHUNK];
10219 +#endif
10220 +} yaffs_ChunkCache;
10221 +
10222 +
10223 +
10224 +/* Tags structures in RAM
10225 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
10226 + * the structure size will get blown out.
10227 + */
10228 +
10229 +#ifndef CONFIG_YAFFS_NO_YAFFS1
10230 +typedef struct {
10231 + unsigned chunkId:20;
10232 + unsigned serialNumber:2;
10233 + unsigned byteCount:10;
10234 + unsigned objectId:18;
10235 + unsigned ecc:12;
10236 + unsigned unusedStuff:2;
10237 +
10238 +} yaffs_Tags;
10239 +
10240 +typedef union {
10241 + yaffs_Tags asTags;
10242 + __u8 asBytes[8];
10243 +} yaffs_TagsUnion;
10244 +
10245 +#endif
10246 +
10247 +/* Stuff used for extended tags in YAFFS2 */
10248 +
10249 +typedef enum {
10250 + YAFFS_ECC_RESULT_UNKNOWN,
10251 + YAFFS_ECC_RESULT_NO_ERROR,
10252 + YAFFS_ECC_RESULT_FIXED,
10253 + YAFFS_ECC_RESULT_UNFIXED
10254 +} yaffs_ECCResult;
10255 +
10256 +typedef enum {
10257 + YAFFS_OBJECT_TYPE_UNKNOWN,
10258 + YAFFS_OBJECT_TYPE_FILE,
10259 + YAFFS_OBJECT_TYPE_SYMLINK,
10260 + YAFFS_OBJECT_TYPE_DIRECTORY,
10261 + YAFFS_OBJECT_TYPE_HARDLINK,
10262 + YAFFS_OBJECT_TYPE_SPECIAL
10263 +} yaffs_ObjectType;
10264 +
10265 +typedef struct {
10266 +
10267 + unsigned validMarker0;
10268 + unsigned chunkUsed; /* Status of the chunk: used or unused */
10269 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
10270 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
10271 + unsigned byteCount; /* Only valid for data chunks */
10272 +
10273 + /* The following stuff only has meaning when we read */
10274 + yaffs_ECCResult eccResult;
10275 + unsigned blockBad;
10276 +
10277 + /* YAFFS 1 stuff */
10278 + unsigned chunkDeleted; /* The chunk is marked deleted */
10279 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
10280 +
10281 + /* YAFFS2 stuff */
10282 + unsigned sequenceNumber; /* The sequence number of this block */
10283 +
10284 + /* Extra info if this is an object header (YAFFS2 only) */
10285 +
10286 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
10287 + unsigned extraParentObjectId; /* The parent object */
10288 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
10289 + unsigned extraShadows; /* Does this shadow another object? */
10290 +
10291 + yaffs_ObjectType extraObjectType; /* What object type? */
10292 +
10293 + unsigned extraFileLength; /* Length if it is a file */
10294 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
10295 +
10296 + unsigned validMarker1;
10297 +
10298 +} yaffs_ExtendedTags;
10299 +
10300 +/* Spare structure for YAFFS1 */
10301 +typedef struct {
10302 + __u8 tagByte0;
10303 + __u8 tagByte1;
10304 + __u8 tagByte2;
10305 + __u8 tagByte3;
10306 + __u8 pageStatus; /* set to 0 to delete the chunk */
10307 + __u8 blockStatus;
10308 + __u8 tagByte4;
10309 + __u8 tagByte5;
10310 + __u8 ecc1[3];
10311 + __u8 tagByte6;
10312 + __u8 tagByte7;
10313 + __u8 ecc2[3];
10314 +} yaffs_Spare;
10315 +
10316 +/*Special structure for passing through to mtd */
10317 +struct yaffs_NANDSpare {
10318 + yaffs_Spare spare;
10319 + int eccres1;
10320 + int eccres2;
10321 +};
10322 +
10323 +/* Block data in RAM */
10324 +
10325 +typedef enum {
10326 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
10327 +
10328 + YAFFS_BLOCK_STATE_SCANNING,
10329 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
10330 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
10331 + * but it needs to be scanned to determine its true state.
10332 + * This state is only valid during yaffs_Scan.
10333 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
10334 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
10335 + */
10336 +
10337 + YAFFS_BLOCK_STATE_EMPTY,
10338 + /* This block is empty */
10339 +
10340 + YAFFS_BLOCK_STATE_ALLOCATING,
10341 + /* This block is partially allocated.
10342 + * At least one page holds valid data.
10343 + * This is the one currently being used for page
10344 + * allocation. Should never be more than one of these
10345 + */
10346 +
10347 + YAFFS_BLOCK_STATE_FULL,
10348 + /* All the pages in this block have been allocated.
10349 + */
10350 +
10351 + YAFFS_BLOCK_STATE_DIRTY,
10352 + /* All pages have been allocated and deleted.
10353 + * Erase me, reuse me.
10354 + */
10355 +
10356 + YAFFS_BLOCK_STATE_CHECKPOINT,
10357 + /* This block is assigned to holding checkpoint data.
10358 + */
10359 +
10360 + YAFFS_BLOCK_STATE_COLLECTING,
10361 + /* This block is being garbage collected */
10362 +
10363 + YAFFS_BLOCK_STATE_DEAD
10364 + /* This block has failed and is not in use */
10365 +} yaffs_BlockState;
10366 +
10367 +typedef struct {
10368 +
10369 + int softDeletions:10; /* number of soft deleted pages */
10370 + int pagesInUse:10; /* number of pages in use */
10371 + yaffs_BlockState blockState:4; /* One of the above block states */
10372 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
10373 + /* and retire the block. */
10374 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
10375 + __u32 gcPrioritise: 1; /* An ECC check or bank check has failed on this block.
10376 + It should be prioritised for GC */
10377 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
10378 +
10379 +#ifdef CONFIG_YAFFS_YAFFS2
10380 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
10381 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
10382 +#endif
10383 +
10384 +} yaffs_BlockInfo;
10385 +
10386 +/* -------------------------- Object structure -------------------------------*/
10387 +/* This is the object structure as stored on NAND */
10388 +
10389 +typedef struct {
10390 + yaffs_ObjectType type;
10391 +
10392 + /* Apply to everything */
10393 + int parentObjectId;
10394 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
10395 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
10396 +
10397 + /* Thes following apply to directories, files, symlinks - not hard links */
10398 + __u32 yst_mode; /* protection */
10399 +
10400 +#ifdef CONFIG_YAFFS_WINCE
10401 + __u32 notForWinCE[5];
10402 +#else
10403 + __u32 yst_uid;
10404 + __u32 yst_gid;
10405 + __u32 yst_atime;
10406 + __u32 yst_mtime;
10407 + __u32 yst_ctime;
10408 +#endif
10409 +
10410 + /* File size applies to files only */
10411 + int fileSize;
10412 +
10413 + /* Equivalent object id applies to hard links only. */
10414 + int equivalentObjectId;
10415 +
10416 + /* Alias is for symlinks only. */
10417 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
10418 +
10419 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
10420 +
10421 +#ifdef CONFIG_YAFFS_WINCE
10422 + __u32 win_ctime[2];
10423 + __u32 win_atime[2];
10424 + __u32 win_mtime[2];
10425 + __u32 roomToGrow[4];
10426 +#else
10427 + __u32 roomToGrow[10];
10428 +#endif
10429 +
10430 + int shadowsObject; /* This object header shadows the specified object if > 0 */
10431 +
10432 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
10433 + __u32 isShrink;
10434 +
10435 +} yaffs_ObjectHeader;
10436 +
10437 +/*--------------------------- Tnode -------------------------- */
10438 +
10439 +union yaffs_Tnode_union {
10440 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
10441 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
10442 +#else
10443 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
10444 +#endif
10445 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
10446 +
10447 +};
10448 +
10449 +typedef union yaffs_Tnode_union yaffs_Tnode;
10450 +
10451 +struct yaffs_TnodeList_struct {
10452 + struct yaffs_TnodeList_struct *next;
10453 + yaffs_Tnode *tnodes;
10454 +};
10455 +
10456 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
10457 +
10458 +/*------------------------ Object -----------------------------*/
10459 +/* An object can be one of:
10460 + * - a directory (no data, has children links
10461 + * - a regular file (data.... not prunes :->).
10462 + * - a symlink [symbolic link] (the alias).
10463 + * - a hard link
10464 + */
10465 +
10466 +typedef struct {
10467 + __u32 fileSize;
10468 + __u32 scannedFileSize;
10469 + __u32 shrinkSize;
10470 + int topLevel;
10471 + yaffs_Tnode *top;
10472 +} yaffs_FileStructure;
10473 +
10474 +typedef struct {
10475 + struct list_head children; /* list of child links */
10476 +} yaffs_DirectoryStructure;
10477 +
10478 +typedef struct {
10479 + YCHAR *alias;
10480 +} yaffs_SymLinkStructure;
10481 +
10482 +typedef struct {
10483 + struct yaffs_ObjectStruct *equivalentObject;
10484 + __u32 equivalentObjectId;
10485 +} yaffs_HardLinkStructure;
10486 +
10487 +typedef union {
10488 + yaffs_FileStructure fileVariant;
10489 + yaffs_DirectoryStructure directoryVariant;
10490 + yaffs_SymLinkStructure symLinkVariant;
10491 + yaffs_HardLinkStructure hardLinkVariant;
10492 +} yaffs_ObjectVariant;
10493 +
10494 +struct yaffs_ObjectStruct {
10495 + __u8 deleted:1; /* This should only apply to unlinked files. */
10496 + __u8 softDeleted:1; /* it has also been soft deleted */
10497 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
10498 + __u8 fake:1; /* A fake object has no presence on NAND. */
10499 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
10500 + __u8 unlinkAllowed:1;
10501 + __u8 dirty:1; /* the object needs to be written to flash */
10502 + __u8 valid:1; /* When the file system is being loaded up, this
10503 + * object might be created before the data
10504 + * is available (ie. file data records appear before the header).
10505 + */
10506 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
10507 +
10508 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
10509 + * still in the inode cache. Free of object is defered.
10510 + * until the inode is released.
10511 + */
10512 +
10513 + __u8 serial; /* serial number of chunk in NAND. Cached here */
10514 + __u16 sum; /* sum of the name to speed searching */
10515 +
10516 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
10517 +
10518 + struct list_head hashLink; /* list of objects in this hash bucket */
10519 +
10520 + struct list_head hardLinks; /* all the equivalent hard linked objects */
10521 +
10522 + /* directory structure stuff */
10523 + /* also used for linking up the free list */
10524 + struct yaffs_ObjectStruct *parent;
10525 + struct list_head siblings;
10526 +
10527 + /* Where's my object header in NAND? */
10528 + int chunkId;
10529 +
10530 + int nDataChunks; /* Number of data chunks attached to the file. */
10531 +
10532 + __u32 objectId; /* the object id value */
10533 +
10534 + __u32 yst_mode;
10535 +
10536 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10537 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
10538 +#endif
10539 +
10540 +#ifndef __KERNEL__
10541 + __u32 inUse;
10542 +#endif
10543 +
10544 +#ifdef CONFIG_YAFFS_WINCE
10545 + __u32 win_ctime[2];
10546 + __u32 win_mtime[2];
10547 + __u32 win_atime[2];
10548 +#else
10549 + __u32 yst_uid;
10550 + __u32 yst_gid;
10551 + __u32 yst_atime;
10552 + __u32 yst_mtime;
10553 + __u32 yst_ctime;
10554 +#endif
10555 +
10556 + __u32 yst_rdev;
10557 +
10558 +#ifdef __KERNEL__
10559 + struct inode *myInode;
10560 +
10561 +#endif
10562 +
10563 + yaffs_ObjectType variantType;
10564 +
10565 + yaffs_ObjectVariant variant;
10566 +
10567 +};
10568 +
10569 +typedef struct yaffs_ObjectStruct yaffs_Object;
10570 +
10571 +struct yaffs_ObjectList_struct {
10572 + yaffs_Object *objects;
10573 + struct yaffs_ObjectList_struct *next;
10574 +};
10575 +
10576 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
10577 +
10578 +typedef struct {
10579 + struct list_head list;
10580 + int count;
10581 +} yaffs_ObjectBucket;
10582 +
10583 +
10584 +/* yaffs_CheckpointObject holds the definition of an object as dumped
10585 + * by checkpointing.
10586 + */
10587 +
10588 +typedef struct {
10589 + int structType;
10590 + __u32 objectId;
10591 + __u32 parentId;
10592 + int chunkId;
10593 +
10594 + yaffs_ObjectType variantType:3;
10595 + __u8 deleted:1;
10596 + __u8 softDeleted:1;
10597 + __u8 unlinked:1;
10598 + __u8 fake:1;
10599 + __u8 renameAllowed:1;
10600 + __u8 unlinkAllowed:1;
10601 + __u8 serial;
10602 +
10603 + int nDataChunks;
10604 + __u32 fileSizeOrEquivalentObjectId;
10605 +
10606 +}yaffs_CheckpointObject;
10607 +
10608 +/*--------------------- Temporary buffers ----------------
10609 + *
10610 + * These are chunk-sized working buffers. Each device has a few
10611 + */
10612 +
10613 +typedef struct {
10614 + __u8 *buffer;
10615 + int line; /* track from whence this buffer was allocated */
10616 + int maxLine;
10617 +} yaffs_TempBuffer;
10618 +
10619 +/*----------------- Device ---------------------------------*/
10620 +
10621 +struct yaffs_DeviceStruct {
10622 + struct list_head devList;
10623 + const char *name;
10624 +
10625 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
10626 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
10627 + int nChunksPerBlock; /* does not need to be a power of 2 */
10628 + int nBytesPerSpare; /* spare area size */
10629 + int startBlock; /* Start block we're allowed to use */
10630 + int endBlock; /* End block we're allowed to use */
10631 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
10632 + /* reserved blocks on NOR and RAM. */
10633 +
10634 + /* Stuff used by the partitioned checkpointing mechanism */
10635 + int checkpointStartBlock;
10636 + int checkpointEndBlock;
10637 +
10638 + /* Stuff used by the shared space checkpointing mechanism */
10639 + /* If this value is zero, then this mechanism is disabled */
10640 +
10641 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
10642 +
10643 +
10644 +
10645 +
10646 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
10647 + * the number of short op caches (don't use too many)
10648 + */
10649 +
10650 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
10651 +
10652 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
10653 +
10654 + void *genericDevice; /* Pointer to device context
10655 + * On an mtd this holds the mtd pointer.
10656 + */
10657 + void *superBlock;
10658 +
10659 + /* NAND access functions (Must be set before calling YAFFS)*/
10660 +
10661 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
10662 + int chunkInNAND, const __u8 * data,
10663 + const yaffs_Spare * spare);
10664 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
10665 + int chunkInNAND, __u8 * data,
10666 + yaffs_Spare * spare);
10667 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
10668 + int blockInNAND);
10669 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
10670 +
10671 +#ifdef CONFIG_YAFFS_YAFFS2
10672 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
10673 + int chunkInNAND, const __u8 * data,
10674 + const yaffs_ExtendedTags * tags);
10675 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
10676 + int chunkInNAND, __u8 * data,
10677 + yaffs_ExtendedTags * tags);
10678 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
10679 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
10680 + yaffs_BlockState * state, int *sequenceNumber);
10681 +#endif
10682 +
10683 + int isYaffs2;
10684 +
10685 + /* The removeObjectCallback function must be supplied by OS flavours that
10686 + * need it. The Linux kernel does not use this, but yaffs direct does use
10687 + * it to implement the faster readdir
10688 + */
10689 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
10690 +
10691 + /* Callback to mark the superblock dirsty */
10692 + void (*markSuperBlockDirty)(void * superblock);
10693 +
10694 + int wideTnodesDisabled; /* Set to disable wide tnodes */
10695 +
10696 +
10697 + /* End of stuff that must be set before initialisation. */
10698 +
10699 + /* Runtime parameters. Set up by YAFFS. */
10700 +
10701 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
10702 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
10703 +
10704 + /* Stuff to support wide tnodes */
10705 + __u32 tnodeWidth;
10706 + __u32 tnodeMask;
10707 +
10708 + /* Stuff to support various file offses to chunk/offset translations */
10709 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
10710 + __u32 crumbMask;
10711 + __u32 crumbShift;
10712 + __u32 crumbsPerChunk;
10713 +
10714 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
10715 + __u32 chunkShift;
10716 + __u32 chunkMask;
10717 +
10718 +
10719 +#ifdef __KERNEL__
10720 +
10721 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
10722 + struct semaphore grossLock; /* Gross locking semaphore */
10723 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
10724 + * at compile time so we have to allocate it.
10725 + */
10726 + void (*putSuperFunc) (struct super_block * sb);
10727 +#endif
10728 +
10729 + int isMounted;
10730 +
10731 + int isCheckpointed;
10732 +
10733 +
10734 + /* Stuff to support block offsetting to support start block zero */
10735 + int internalStartBlock;
10736 + int internalEndBlock;
10737 + int blockOffset;
10738 + int chunkOffset;
10739 +
10740 +
10741 + /* Runtime checkpointing stuff */
10742 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
10743 + int checkpointByteCount;
10744 + int checkpointByteOffset;
10745 + __u8 *checkpointBuffer;
10746 + int checkpointOpenForWrite;
10747 + int blocksInCheckpoint;
10748 + int checkpointCurrentChunk;
10749 + int checkpointCurrentBlock;
10750 + int checkpointNextBlock;
10751 + int *checkpointBlockList;
10752 + int checkpointMaxBlocks;
10753 +
10754 + /* Block Info */
10755 + yaffs_BlockInfo *blockInfo;
10756 + __u8 *chunkBits; /* bitmap of chunks in use */
10757 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
10758 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
10759 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
10760 + * Must be consistent with nChunksPerBlock.
10761 + */
10762 +
10763 + int nErasedBlocks;
10764 + int allocationBlock; /* Current block being allocated off */
10765 + __u32 allocationPage;
10766 + int allocationBlockFinder; /* Used to search for next allocation block */
10767 +
10768 + /* Runtime state */
10769 + int nTnodesCreated;
10770 + yaffs_Tnode *freeTnodes;
10771 + int nFreeTnodes;
10772 + yaffs_TnodeList *allocatedTnodeList;
10773 +
10774 + int isDoingGC;
10775 +
10776 + int nObjectsCreated;
10777 + yaffs_Object *freeObjects;
10778 + int nFreeObjects;
10779 +
10780 + yaffs_ObjectList *allocatedObjectList;
10781 +
10782 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
10783 +
10784 + int nFreeChunks;
10785 +
10786 + int currentDirtyChecker; /* Used to find current dirtiest block */
10787 +
10788 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
10789 +
10790 + /* Statistcs */
10791 + int nPageWrites;
10792 + int nPageReads;
10793 + int nBlockErasures;
10794 + int nErasureFailures;
10795 + int nGCCopies;
10796 + int garbageCollections;
10797 + int passiveGarbageCollections;
10798 + int nRetriedWrites;
10799 + int nRetiredBlocks;
10800 + int eccFixed;
10801 + int eccUnfixed;
10802 + int tagsEccFixed;
10803 + int tagsEccUnfixed;
10804 + int nDeletions;
10805 + int nUnmarkedDeletions;
10806 +
10807 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
10808 +
10809 + /* Special directories */
10810 + yaffs_Object *rootDir;
10811 + yaffs_Object *lostNFoundDir;
10812 +
10813 + /* Buffer areas for storing data to recover from write failures TODO
10814 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
10815 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
10816 + */
10817 +
10818 + int bufferedBlock; /* Which block is buffered here? */
10819 + int doingBufferedBlockRewrite;
10820 +
10821 + yaffs_ChunkCache *srCache;
10822 + int srLastUse;
10823 +
10824 + int cacheHits;
10825 +
10826 + /* Stuff for background deletion and unlinked files.*/
10827 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
10828 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
10829 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
10830 + int nDeletedFiles; /* Count of files awaiting deletion;*/
10831 + int nUnlinkedFiles; /* Count of unlinked files. */
10832 + int nBackgroundDeletions; /* Count of background deletions. */
10833 +
10834 +
10835 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
10836 + int maxTemp;
10837 + int unmanagedTempAllocations;
10838 + int unmanagedTempDeallocations;
10839 +
10840 + /* yaffs2 runtime stuff */
10841 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
10842 + unsigned oldestDirtySequence;
10843 +
10844 +};
10845 +
10846 +typedef struct yaffs_DeviceStruct yaffs_Device;
10847 +
10848 +/* The static layout of bllock usage etc is stored in the super block header */
10849 +typedef struct {
10850 + int StructType;
10851 + int version;
10852 + int checkpointStartBlock;
10853 + int checkpointEndBlock;
10854 + int startBlock;
10855 + int endBlock;
10856 + int rfu[100];
10857 +} yaffs_SuperBlockHeader;
10858 +
10859 +/* The CheckpointDevice structure holds the device information that changes at runtime and
10860 + * must be preserved over unmount/mount cycles.
10861 + */
10862 +typedef struct {
10863 + int structType;
10864 + int nErasedBlocks;
10865 + int allocationBlock; /* Current block being allocated off */
10866 + __u32 allocationPage;
10867 + int nFreeChunks;
10868 +
10869 + int nDeletedFiles; /* Count of files awaiting deletion;*/
10870 + int nUnlinkedFiles; /* Count of unlinked files. */
10871 + int nBackgroundDeletions; /* Count of background deletions. */
10872 +
10873 + /* yaffs2 runtime stuff */
10874 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
10875 + unsigned oldestDirtySequence;
10876 +
10877 +} yaffs_CheckpointDevice;
10878 +
10879 +
10880 +typedef struct {
10881 + int structType;
10882 + __u32 magic;
10883 + __u32 version;
10884 + __u32 head;
10885 +} yaffs_CheckpointValidity;
10886 +
10887 +/* Function to manipulate block info */
10888 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
10889 +{
10890 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
10891 + T(YAFFS_TRACE_ERROR,
10892 + (TSTR
10893 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
10894 + blk));
10895 + YBUG();
10896 + }
10897 + return &dev->blockInfo[blk - dev->internalStartBlock];
10898 +}
10899 +
10900 +/*----------------------- YAFFS Functions -----------------------*/
10901 +
10902 +int yaffs_GutsInitialise(yaffs_Device * dev);
10903 +void yaffs_Deinitialise(yaffs_Device * dev);
10904 +
10905 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
10906 +
10907 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
10908 + yaffs_Object * newDir, const YCHAR * newName);
10909 +
10910 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
10911 +int yaffs_DeleteFile(yaffs_Object * obj);
10912 +
10913 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
10914 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
10915 +int yaffs_GetObjectInode(yaffs_Object * obj);
10916 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
10917 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
10918 +
10919 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
10920 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
10921 +
10922 +/* File operations */
10923 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
10924 + int nBytes);
10925 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
10926 + int nBytes, int writeThrough);
10927 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
10928 +
10929 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
10930 + __u32 mode, __u32 uid, __u32 gid);
10931 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
10932 +
10933 +/* Flushing and checkpointing */
10934 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
10935 +
10936 +int yaffs_CheckpointSave(yaffs_Device *dev);
10937 +int yaffs_CheckpointRestore(yaffs_Device *dev);
10938 +
10939 +/* Directory operations */
10940 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
10941 + __u32 mode, __u32 uid, __u32 gid);
10942 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
10943 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10944 + int (*fn) (yaffs_Object *));
10945 +
10946 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
10947 +
10948 +/* Link operations */
10949 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
10950 + yaffs_Object * equivalentObject);
10951 +
10952 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
10953 +
10954 +/* Symlink operations */
10955 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
10956 + __u32 mode, __u32 uid, __u32 gid,
10957 + const YCHAR * alias);
10958 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
10959 +
10960 +/* Special inodes (fifos, sockets and devices) */
10961 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
10962 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
10963 +
10964 +/* Special directories */
10965 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
10966 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
10967 +
10968 +#ifdef CONFIG_YAFFS_WINCE
10969 +/* CONFIG_YAFFS_WINCE special stuff */
10970 +void yfsd_WinFileTimeNow(__u32 target[2]);
10971 +#endif
10972 +
10973 +#ifdef __KERNEL__
10974 +
10975 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
10976 +#endif
10977 +
10978 +/* Debug dump */
10979 +int yaffs_DumpObject(yaffs_Object * obj);
10980 +
10981 +void yaffs_GutsTest(yaffs_Device * dev);
10982 +
10983 +/* A few useful functions */
10984 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
10985 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
10986 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
10987 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
10988 +
10989 +#endif
10990 diff -urN linux.old/fs/yaffs2/yaffsinterface.h linux.dev/fs/yaffs2/yaffsinterface.h
10991 --- linux.old/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
10992 +++ linux.dev/fs/yaffs2/yaffsinterface.h 2006-12-14 04:21:47.000000000 +0100
10993 @@ -0,0 +1,23 @@
10994 +/*
10995 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
10996 + * yaffsinterface.h: Interface to the guts of yaffs.
10997 + *
10998 + * Copyright (C) 2002 Aleph One Ltd.
10999 + * for Toby Churchill Ltd and Brightstar Engineering
11000 + *
11001 + * Created by Charles Manning <charles@aleph1.co.uk>
11002 + *
11003 + * This program is free software; you can redistribute it and/or modify
11004 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11005 + * published by the Free Software Foundation.
11006 + *
11007 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11008 + *
11009 + */
11010 +
11011 +#ifndef __YAFFSINTERFACE_H__
11012 +#define __YAFFSINTERFACE_H__
11013 +
11014 +int yaffs_Initialise(unsigned nBlocks);
11015 +
11016 +#endif
11017 diff -urN linux.old/fs/yaffs2/yaffs_mtdif2.c linux.dev/fs/yaffs2/yaffs_mtdif2.c
11018 --- linux.old/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
11019 +++ linux.dev/fs/yaffs2/yaffs_mtdif2.c 2006-12-14 04:21:47.000000000 +0100
11020 @@ -0,0 +1,234 @@
11021 +/*
11022 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11023 + * yaffs_mtdif.c NAND mtd wrapper functions.
11024 + *
11025 + * Copyright (C) 2002 Aleph One Ltd.
11026 + * for Toby Churchill Ltd and Brightstar Engineering
11027 + *
11028 + * Created by Charles Manning <charles@aleph1.co.uk>
11029 + *
11030 + * This program is free software; you can redistribute it and/or modify
11031 + * it under the terms of the GNU General Public License version 2 as
11032 + * published by the Free Software Foundation.
11033 + *
11034 + */
11035 +
11036 +/* mtd interface for YAFFS2 */
11037 +
11038 +const char *yaffs_mtdif2_c_version =
11039 + "$Id: yaffs_mtdif2.c,v 1.15 2006/11/08 06:24:34 charles Exp $";
11040 +
11041 +#include "yportenv.h"
11042 +
11043 +
11044 +#include "yaffs_mtdif2.h"
11045 +
11046 +#include "linux/mtd/mtd.h"
11047 +#include "linux/types.h"
11048 +#include "linux/time.h"
11049 +
11050 +#include "yaffs_packedtags2.h"
11051 +
11052 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
11053 + const __u8 * data,
11054 + const yaffs_ExtendedTags * tags)
11055 +{
11056 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11057 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11058 + struct mtd_oob_ops ops;
11059 +#else
11060 + size_t dummy;
11061 +#endif
11062 + int retval = 0;
11063 +
11064 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11065 +
11066 + yaffs_PackedTags2 pt;
11067 +
11068 + T(YAFFS_TRACE_MTD,
11069 + (TSTR
11070 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
11071 + TENDSTR), chunkInNAND, data, tags));
11072 +
11073 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11074 + if (tags)
11075 + yaffs_PackTags2(&pt, tags);
11076 + else
11077 + BUG(); /* both tags and data should always be present */
11078 +
11079 + if (data) {
11080 + ops.mode = MTD_OOB_AUTO;
11081 + ops.ooblen = sizeof(pt);
11082 + ops.len = dev->nDataBytesPerChunk;
11083 + ops.ooboffs = 0;
11084 + ops.datbuf = (__u8 *)data;
11085 + ops.oobbuf = (void *)&pt;
11086 + retval = mtd->write_oob(mtd, addr, &ops);
11087 + } else
11088 + BUG(); /* both tags and data should always be present */
11089 +#else
11090 + if (tags) {
11091 + yaffs_PackTags2(&pt, tags);
11092 + }
11093 +
11094 + if (data && tags) {
11095 + if (dev->useNANDECC)
11096 + retval =
11097 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11098 + &dummy, data, (__u8 *) & pt, NULL);
11099 + else
11100 + retval =
11101 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11102 + &dummy, data, (__u8 *) & pt, NULL);
11103 + } else {
11104 + if (data)
11105 + retval =
11106 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11107 + data);
11108 + if (tags)
11109 + retval =
11110 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
11111 + (__u8 *) & pt);
11112 +
11113 + }
11114 +#endif
11115 +
11116 + if (retval == 0)
11117 + return YAFFS_OK;
11118 + else
11119 + return YAFFS_FAIL;
11120 +}
11121 +
11122 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11123 + __u8 * data, yaffs_ExtendedTags * tags)
11124 +{
11125 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11126 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11127 + struct mtd_oob_ops ops;
11128 +#endif
11129 + size_t dummy;
11130 + int retval = 0;
11131 +
11132 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11133 +
11134 + yaffs_PackedTags2 pt;
11135 +
11136 + T(YAFFS_TRACE_MTD,
11137 + (TSTR
11138 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
11139 + TENDSTR), chunkInNAND, data, tags));
11140 +
11141 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11142 + if (data && !tags)
11143 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
11144 + &dummy, data);
11145 + else if (tags) {
11146 + ops.mode = MTD_OOB_AUTO;
11147 + ops.ooblen = sizeof(pt);
11148 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
11149 + ops.ooboffs = 0;
11150 + ops.datbuf = data;
11151 + ops.oobbuf = dev->spareBuffer;
11152 + retval = mtd->read_oob(mtd, addr, &ops);
11153 + }
11154 +#else
11155 + if (data && tags) {
11156 + if (dev->useNANDECC) {
11157 + retval =
11158 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11159 + &dummy, data, dev->spareBuffer,
11160 + NULL);
11161 + } else {
11162 + retval =
11163 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11164 + &dummy, data, dev->spareBuffer,
11165 + NULL);
11166 + }
11167 + } else {
11168 + if (data)
11169 + retval =
11170 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11171 + data);
11172 + if (tags)
11173 + retval =
11174 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
11175 + dev->spareBuffer);
11176 + }
11177 +#endif
11178 +
11179 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
11180 +
11181 + if (tags)
11182 + yaffs_UnpackTags2(tags, &pt);
11183 +
11184 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
11185 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
11186 +
11187 + if (retval == 0)
11188 + return YAFFS_OK;
11189 + else
11190 + return YAFFS_FAIL;
11191 +}
11192 +
11193 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
11194 +{
11195 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11196 + int retval;
11197 + T(YAFFS_TRACE_MTD,
11198 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
11199 +
11200 + retval =
11201 + mtd->block_markbad(mtd,
11202 + blockNo * dev->nChunksPerBlock *
11203 + dev->nDataBytesPerChunk);
11204 +
11205 + if (retval == 0)
11206 + return YAFFS_OK;
11207 + else
11208 + return YAFFS_FAIL;
11209 +
11210 +}
11211 +
11212 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11213 + yaffs_BlockState * state, int *sequenceNumber)
11214 +{
11215 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11216 + int retval;
11217 +
11218 + T(YAFFS_TRACE_MTD,
11219 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
11220 + retval =
11221 + mtd->block_isbad(mtd,
11222 + blockNo * dev->nChunksPerBlock *
11223 + dev->nDataBytesPerChunk);
11224 +
11225 + if (retval) {
11226 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
11227 +
11228 + *state = YAFFS_BLOCK_STATE_DEAD;
11229 + *sequenceNumber = 0;
11230 + } else {
11231 + yaffs_ExtendedTags t;
11232 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
11233 + blockNo *
11234 + dev->nChunksPerBlock, NULL,
11235 + &t);
11236 +
11237 + if (t.chunkUsed) {
11238 + *sequenceNumber = t.sequenceNumber;
11239 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
11240 + } else {
11241 + *sequenceNumber = 0;
11242 + *state = YAFFS_BLOCK_STATE_EMPTY;
11243 + }
11244 + }
11245 + T(YAFFS_TRACE_MTD,
11246 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
11247 + *state));
11248 +
11249 + if (retval == 0)
11250 + return YAFFS_OK;
11251 + else
11252 + return YAFFS_FAIL;
11253 +}
11254 +
11255 diff -urN linux.old/fs/yaffs2/yaffs_mtdif2.h linux.dev/fs/yaffs2/yaffs_mtdif2.h
11256 --- linux.old/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
11257 +++ linux.dev/fs/yaffs2/yaffs_mtdif2.h 2006-12-14 04:21:47.000000000 +0100
11258 @@ -0,0 +1,29 @@
11259 +/*
11260 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11261 + * yaffs_mtdif.c NAND mtd wrapper functions.
11262 + *
11263 + * Copyright (C) 2002 Aleph One Ltd.
11264 + * for Toby Churchill Ltd and Brightstar Engineering
11265 + *
11266 + * Created by Charles Manning <charles@aleph1.co.uk>
11267 + *
11268 + * This program is free software; you can redistribute it and/or modify
11269 + * it under the terms of the GNU General Public License version 2 as
11270 + * published by the Free Software Foundation.
11271 + *
11272 + */
11273 +
11274 +#ifndef __YAFFS_MTDIF2_H__
11275 +#define __YAFFS_MTDIF2_H__
11276 +
11277 +#include "yaffs_guts.h"
11278 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
11279 + const __u8 * data,
11280 + const yaffs_ExtendedTags * tags);
11281 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11282 + __u8 * data, yaffs_ExtendedTags * tags);
11283 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
11284 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11285 + yaffs_BlockState * state, int *sequenceNumber);
11286 +
11287 +#endif
11288 diff -urN linux.old/fs/yaffs2/yaffs_mtdif.c linux.dev/fs/yaffs2/yaffs_mtdif.c
11289 --- linux.old/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
11290 +++ linux.dev/fs/yaffs2/yaffs_mtdif.c 2006-12-14 04:21:47.000000000 +0100
11291 @@ -0,0 +1,243 @@
11292 +/*
11293 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11294 + * yaffs_mtdif.c NAND mtd wrapper functions.
11295 + *
11296 + * Copyright (C) 2002 Aleph One Ltd.
11297 + * for Toby Churchill Ltd and Brightstar Engineering
11298 + *
11299 + * Created by Charles Manning <charles@aleph1.co.uk>
11300 + *
11301 + * This program is free software; you can redistribute it and/or modify
11302 + * it under the terms of the GNU General Public License version 2 as
11303 + * published by the Free Software Foundation.
11304 + *
11305 + */
11306 +
11307 +const char *yaffs_mtdif_c_version =
11308 + "$Id: yaffs_mtdif.c,v 1.17 2006/11/29 20:21:12 charles Exp $";
11309 +
11310 +#include "yportenv.h"
11311 +
11312 +
11313 +#include "yaffs_mtdif.h"
11314 +
11315 +#include "linux/mtd/mtd.h"
11316 +#include "linux/types.h"
11317 +#include "linux/time.h"
11318 +#include "linux/mtd/nand.h"
11319 +
11320 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
11321 +static struct nand_oobinfo yaffs_oobinfo = {
11322 + .useecc = 1,
11323 + .eccbytes = 6,
11324 + .eccpos = {8, 9, 10, 13, 14, 15}
11325 +};
11326 +
11327 +static struct nand_oobinfo yaffs_noeccinfo = {
11328 + .useecc = 0,
11329 +};
11330 +#endif
11331 +
11332 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11333 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
11334 +{
11335 + oob[0] = spare->tagByte0;
11336 + oob[1] = spare->tagByte1;
11337 + oob[2] = spare->tagByte2;
11338 + oob[3] = spare->tagByte3;
11339 + oob[4] = spare->tagByte4;
11340 + oob[5] = spare->tagByte5 & 0x3f;
11341 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
11342 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
11343 + oob[6] = spare->tagByte6;
11344 + oob[7] = spare->tagByte7;
11345 +}
11346 +
11347 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
11348 +{
11349 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
11350 + spare->tagByte0 = oob[0];
11351 + spare->tagByte1 = oob[1];
11352 + spare->tagByte2 = oob[2];
11353 + spare->tagByte3 = oob[3];
11354 + spare->tagByte4 = oob[4];
11355 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
11356 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
11357 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
11358 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
11359 + spare->tagByte6 = oob[6];
11360 + spare->tagByte7 = oob[7];
11361 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
11362 +
11363 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
11364 +}
11365 +#endif
11366 +
11367 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
11368 + const __u8 * data, const yaffs_Spare * spare)
11369 +{
11370 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11371 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11372 + struct mtd_oob_ops ops;
11373 +#endif
11374 + size_t dummy;
11375 + int retval = 0;
11376 +
11377 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11378 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11379 + __u8 spareAsBytes[8]; /* OOB */
11380 +
11381 + if (data && !spare)
11382 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
11383 + &dummy, data);
11384 + else if (spare) {
11385 + if (dev->useNANDECC) {
11386 + translate_spare2oob(spare, spareAsBytes);
11387 + ops.mode = MTD_OOB_AUTO;
11388 + ops.ooblen = 8; /* temp hack */
11389 + } else {
11390 + ops.mode = MTD_OOB_RAW;
11391 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
11392 + }
11393 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
11394 + ops.datbuf = (u8 *)data;
11395 + ops.ooboffs = 0;
11396 + ops.oobbuf = spareAsBytes;
11397 + retval = mtd->write_oob(mtd, addr, &ops);
11398 + }
11399 +#else
11400 + __u8 *spareAsBytes = (__u8 *) spare;
11401 +
11402 + if (data && spare) {
11403 + if (dev->useNANDECC)
11404 + retval =
11405 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11406 + &dummy, data, spareAsBytes,
11407 + &yaffs_oobinfo);
11408 + else
11409 + retval =
11410 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
11411 + &dummy, data, spareAsBytes,
11412 + &yaffs_noeccinfo);
11413 + } else {
11414 + if (data)
11415 + retval =
11416 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11417 + data);
11418 + if (spare)
11419 + retval =
11420 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
11421 + &dummy, spareAsBytes);
11422 + }
11423 +#endif
11424 +
11425 + if (retval == 0)
11426 + return YAFFS_OK;
11427 + else
11428 + return YAFFS_FAIL;
11429 +}
11430 +
11431 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
11432 + yaffs_Spare * spare)
11433 +{
11434 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11435 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11436 + struct mtd_oob_ops ops;
11437 +#endif
11438 + size_t dummy;
11439 + int retval = 0;
11440 +
11441 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
11442 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
11443 + __u8 spareAsBytes[8]; /* OOB */
11444 +
11445 + if (data && !spare)
11446 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
11447 + &dummy, data);
11448 + else if (spare) {
11449 + if (dev->useNANDECC) {
11450 + ops.mode = MTD_OOB_AUTO;
11451 + ops.ooblen = 8; /* temp hack */
11452 + } else {
11453 + ops.mode = MTD_OOB_RAW;
11454 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
11455 + }
11456 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
11457 + ops.datbuf = data;
11458 + ops.ooboffs = 0;
11459 + ops.oobbuf = spareAsBytes;
11460 + retval = mtd->read_oob(mtd, addr, &ops);
11461 + if (dev->useNANDECC)
11462 + translate_oob2spare(spare, spareAsBytes);
11463 + }
11464 +#else
11465 + __u8 *spareAsBytes = (__u8 *) spare;
11466 +
11467 + if (data && spare) {
11468 + if (dev->useNANDECC) {
11469 + /* Careful, this call adds 2 ints */
11470 + /* to the end of the spare data. Calling function */
11471 + /* should allocate enough memory for spare, */
11472 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
11473 + retval =
11474 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11475 + &dummy, data, spareAsBytes,
11476 + &yaffs_oobinfo);
11477 + } else {
11478 + retval =
11479 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
11480 + &dummy, data, spareAsBytes,
11481 + &yaffs_noeccinfo);
11482 + }
11483 + } else {
11484 + if (data)
11485 + retval =
11486 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
11487 + data);
11488 + if (spare)
11489 + retval =
11490 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
11491 + &dummy, spareAsBytes);
11492 + }
11493 +#endif
11494 +
11495 + if (retval == 0)
11496 + return YAFFS_OK;
11497 + else
11498 + return YAFFS_FAIL;
11499 +}
11500 +
11501 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
11502 +{
11503 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
11504 + __u32 addr =
11505 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
11506 + * dev->nChunksPerBlock;
11507 + struct erase_info ei;
11508 + int retval = 0;
11509 +
11510 + ei.mtd = mtd;
11511 + ei.addr = addr;
11512 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
11513 + ei.time = 1000;
11514 + ei.retries = 2;
11515 + ei.callback = NULL;
11516 + ei.priv = (u_long) dev;
11517 +
11518 + /* Todo finish off the ei if required */
11519 +
11520 + sema_init(&dev->sem, 0);
11521 +
11522 + retval = mtd->erase(mtd, &ei);
11523 +
11524 + if (retval == 0)
11525 + return YAFFS_OK;
11526 + else
11527 + return YAFFS_FAIL;
11528 +}
11529 +
11530 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
11531 +{
11532 + return YAFFS_OK;
11533 +}
11534 +
11535 diff -urN linux.old/fs/yaffs2/yaffs_mtdif.h linux.dev/fs/yaffs2/yaffs_mtdif.h
11536 --- linux.old/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
11537 +++ linux.dev/fs/yaffs2/yaffs_mtdif.h 2006-12-14 04:21:47.000000000 +0100
11538 @@ -0,0 +1,31 @@
11539 +/*
11540 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11541 + * yaffs_mtdif.h NAND mtd interface wrappers
11542 + *
11543 + * Copyright (C) 2002 Aleph One Ltd.
11544 + * for Toby Churchill Ltd and Brightstar Engineering
11545 + *
11546 + * Created by Charles Manning <charles@aleph1.co.uk>
11547 + *
11548 + * This program is free software; you can redistribute it and/or modify
11549 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11550 + * published by the Free Software Foundation.
11551 + *
11552 + *
11553 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11554 + *
11555 + * $Id: yaffs_mtdif.h,v 1.3 2005/08/11 01:07:43 marty Exp $
11556 + */
11557 +
11558 +#ifndef __YAFFS_MTDIF_H__
11559 +#define __YAFFS_MTDIF_H__
11560 +
11561 +#include "yaffs_guts.h"
11562 +
11563 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
11564 + const __u8 * data, const yaffs_Spare * spare);
11565 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
11566 + yaffs_Spare * spare);
11567 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
11568 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
11569 +#endif
11570 diff -urN linux.old/fs/yaffs2/yaffs_nand.c linux.dev/fs/yaffs2/yaffs_nand.c
11571 --- linux.old/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
11572 +++ linux.dev/fs/yaffs2/yaffs_nand.c 2006-12-14 04:21:47.000000000 +0100
11573 @@ -0,0 +1,135 @@
11574 +/*
11575 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11576 + *
11577 + * Copyright (C) 2002 Aleph One Ltd.
11578 + * for Toby Churchill Ltd and Brightstar Engineering
11579 + *
11580 + * Created by Charles Manning <charles@aleph1.co.uk>
11581 + *
11582 + * This program is free software; you can redistribute it and/or modify
11583 + * it under the terms of the GNU General Public License version 2 as
11584 + * published by the Free Software Foundation.
11585 + *
11586 + */
11587 +
11588 +const char *yaffs_nand_c_version =
11589 + "$Id: yaffs_nand.c,v 1.5 2006/11/08 09:52:12 charles Exp $";
11590 +
11591 +#include "yaffs_nand.h"
11592 +#include "yaffs_tagscompat.h"
11593 +#include "yaffs_tagsvalidity.h"
11594 +
11595 +
11596 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11597 + __u8 * buffer,
11598 + yaffs_ExtendedTags * tags)
11599 +{
11600 + int result;
11601 + yaffs_ExtendedTags localTags;
11602 +
11603 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
11604 +
11605 + /* If there are no tags provided, use local tags to get prioritised gc working */
11606 + if(!tags)
11607 + tags = &localTags;
11608 +
11609 + if (dev->readChunkWithTagsFromNAND)
11610 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
11611 + tags);
11612 + else
11613 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
11614 + realignedChunkInNAND,
11615 + buffer,
11616 + tags);
11617 + if(tags &&
11618 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
11619 +
11620 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
11621 + yaffs_HandleChunkError(dev,bi);
11622 + }
11623 +
11624 + return result;
11625 +}
11626 +
11627 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
11628 + int chunkInNAND,
11629 + const __u8 * buffer,
11630 + yaffs_ExtendedTags * tags)
11631 +{
11632 + chunkInNAND -= dev->chunkOffset;
11633 +
11634 +
11635 + if (tags) {
11636 + tags->sequenceNumber = dev->sequenceNumber;
11637 + tags->chunkUsed = 1;
11638 + if (!yaffs_ValidateTags(tags)) {
11639 + T(YAFFS_TRACE_ERROR,
11640 + (TSTR("Writing uninitialised tags" TENDSTR)));
11641 + YBUG();
11642 + }
11643 + T(YAFFS_TRACE_WRITE,
11644 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
11645 + tags->objectId, tags->chunkId));
11646 + } else {
11647 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
11648 + YBUG();
11649 + }
11650 +
11651 + if (dev->writeChunkWithTagsToNAND)
11652 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
11653 + tags);
11654 + else
11655 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
11656 + chunkInNAND,
11657 + buffer,
11658 + tags);
11659 +}
11660 +
11661 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
11662 +{
11663 + blockNo -= dev->blockOffset;
11664 +
11665 +;
11666 + if (dev->markNANDBlockBad)
11667 + return dev->markNANDBlockBad(dev, blockNo);
11668 + else
11669 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
11670 +}
11671 +
11672 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
11673 + int blockNo,
11674 + yaffs_BlockState * state,
11675 + unsigned *sequenceNumber)
11676 +{
11677 + blockNo -= dev->blockOffset;
11678 +
11679 + if (dev->queryNANDBlock)
11680 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
11681 + else
11682 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
11683 + state,
11684 + sequenceNumber);
11685 +}
11686 +
11687 +
11688 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11689 + int blockInNAND)
11690 +{
11691 + int result;
11692 +
11693 + blockInNAND -= dev->blockOffset;
11694 +
11695 +
11696 + dev->nBlockErasures++;
11697 + result = dev->eraseBlockInNAND(dev, blockInNAND);
11698 +
11699 + return result;
11700 +}
11701 +
11702 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
11703 +{
11704 + return dev->initialiseNAND(dev);
11705 +}
11706 +
11707 +
11708 +
11709 diff -urN linux.old/fs/yaffs2/yaffs_nandemul2k.h linux.dev/fs/yaffs2/yaffs_nandemul2k.h
11710 --- linux.old/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
11711 +++ linux.dev/fs/yaffs2/yaffs_nandemul2k.h 2006-12-14 04:21:47.000000000 +0100
11712 @@ -0,0 +1,42 @@
11713 +/*
11714 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11715 + *
11716 + * Copyright (C) 2002 Aleph One Ltd.
11717 + * for Toby Churchill Ltd and Brightstar Engineering
11718 + *
11719 + * Created by Charles Manning <charles@aleph1.co.uk>
11720 + *
11721 + * This program is free software; you can redistribute it and/or modify
11722 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11723 + * published by the Free Software Foundation.
11724 + *
11725 + *
11726 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11727 + *
11728 + * yaffs_nandemul2k.h: Interface to emulated NAND functions (2k page size)
11729 + *
11730 + * $Id: yaffs_nandemul2k.h,v 1.2 2005/08/11 02:37:49 marty Exp $
11731 + */
11732 +
11733 +#ifndef __YAFFS_NANDEMUL2K_H__
11734 +#define __YAFFS_NANDEMUL2K_H__
11735 +
11736 +#include "yaffs_guts.h"
11737 +
11738 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
11739 + int chunkInNAND, const __u8 * data,
11740 + yaffs_ExtendedTags * tags);
11741 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
11742 + int chunkInNAND, __u8 * data,
11743 + yaffs_ExtendedTags * tags);
11744 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
11745 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
11746 + yaffs_BlockState * state, int *sequenceNumber);
11747 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11748 + int blockInNAND);
11749 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
11750 +int nandemul2k_GetBytesPerChunk(void);
11751 +int nandemul2k_GetChunksPerBlock(void);
11752 +int nandemul2k_GetNumberOfBlocks(void);
11753 +
11754 +#endif
11755 diff -urN linux.old/fs/yaffs2/yaffs_nand.h linux.dev/fs/yaffs2/yaffs_nand.h
11756 --- linux.old/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
11757 +++ linux.dev/fs/yaffs2/yaffs_nand.h 2006-12-14 04:21:47.000000000 +0100
11758 @@ -0,0 +1,43 @@
11759 +/*
11760 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11761 + *
11762 + * Copyright (C) 2002 Aleph One Ltd.
11763 + * for Toby Churchill Ltd and Brightstar Engineering
11764 + *
11765 + * Created by Charles Manning <charles@aleph1.co.uk>
11766 + *
11767 + * This program is free software; you can redistribute it and/or modify
11768 + * it under the terms of the GNU General Public License version 2 as
11769 + * published by the Free Software Foundation.
11770 + *
11771 + */
11772 +
11773 +#ifndef __YAFFS_NAND_H__
11774 +#define __YAFFS_NAND_H__
11775 +#include "yaffs_guts.h"
11776 +
11777 +
11778 +
11779 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
11780 + __u8 * buffer,
11781 + yaffs_ExtendedTags * tags);
11782 +
11783 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
11784 + int chunkInNAND,
11785 + const __u8 * buffer,
11786 + yaffs_ExtendedTags * tags);
11787 +
11788 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
11789 +
11790 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
11791 + int blockNo,
11792 + yaffs_BlockState * state,
11793 + unsigned *sequenceNumber);
11794 +
11795 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
11796 + int blockInNAND);
11797 +
11798 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
11799 +
11800 +#endif
11801 +
11802 diff -urN linux.old/fs/yaffs2/yaffs_packedtags1.c linux.dev/fs/yaffs2/yaffs_packedtags1.c
11803 --- linux.old/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
11804 +++ linux.dev/fs/yaffs2/yaffs_packedtags1.c 2006-12-14 04:21:47.000000000 +0100
11805 @@ -0,0 +1,39 @@
11806 +#include "yaffs_packedtags1.h"
11807 +#include "yportenv.h"
11808 +
11809 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
11810 +{
11811 + pt->chunkId = t->chunkId;
11812 + pt->serialNumber = t->serialNumber;
11813 + pt->byteCount = t->byteCount;
11814 + pt->objectId = t->objectId;
11815 + pt->ecc = 0;
11816 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
11817 + pt->unusedStuff = 0;
11818 + pt->shouldBeFF = 0xFFFFFFFF;
11819 +
11820 +}
11821 +
11822 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
11823 +{
11824 + static const __u8 allFF[] =
11825 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
11826 +0xff };
11827 +
11828 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
11829 + t->blockBad = 0;
11830 + if (pt->shouldBeFF != 0xFFFFFFFF) {
11831 + t->blockBad = 1;
11832 + }
11833 + t->chunkUsed = 1;
11834 + t->objectId = pt->objectId;
11835 + t->chunkId = pt->chunkId;
11836 + t->byteCount = pt->byteCount;
11837 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
11838 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
11839 + t->serialNumber = pt->serialNumber;
11840 + } else {
11841 + memset(t, 0, sizeof(yaffs_ExtendedTags));
11842 +
11843 + }
11844 +}
11845 diff -urN linux.old/fs/yaffs2/yaffs_packedtags1.h linux.dev/fs/yaffs2/yaffs_packedtags1.h
11846 --- linux.old/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
11847 +++ linux.dev/fs/yaffs2/yaffs_packedtags1.h 2006-12-14 04:21:47.000000000 +0100
11848 @@ -0,0 +1,22 @@
11849 +// This is used to pack YAFFS1 tags, not YAFFS2 tags.
11850 +
11851 +#ifndef __YAFFS_PACKEDTAGS1_H__
11852 +#define __YAFFS_PACKEDTAGS1_H__
11853 +
11854 +#include "yaffs_guts.h"
11855 +
11856 +typedef struct {
11857 + unsigned chunkId:20;
11858 + unsigned serialNumber:2;
11859 + unsigned byteCount:10;
11860 + unsigned objectId:18;
11861 + unsigned ecc:12;
11862 + unsigned deleted:1;
11863 + unsigned unusedStuff:1;
11864 + unsigned shouldBeFF;
11865 +
11866 +} yaffs_PackedTags1;
11867 +
11868 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
11869 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
11870 +#endif
11871 diff -urN linux.old/fs/yaffs2/yaffs_packedtags2.c linux.dev/fs/yaffs2/yaffs_packedtags2.c
11872 --- linux.old/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
11873 +++ linux.dev/fs/yaffs2/yaffs_packedtags2.c 2006-12-14 04:21:47.000000000 +0100
11874 @@ -0,0 +1,184 @@
11875 +/*
11876 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
11877 + *
11878 + * yaffs_packedtags2.c: Tags packing for YAFFS2
11879 + *
11880 + * Copyright (C) 2002 Aleph One Ltd.
11881 + *
11882 + * Created by Charles Manning <charles@aleph1.co.uk>
11883 + *
11884 + *
11885 + * This program is free software; you can redistribute it and/or
11886 + * modify it under the terms of the GNU Lesser General Public License
11887 + * version 2.1 as published by the Free Software Foundation.
11888 + */
11889 +
11890 +#include "yaffs_packedtags2.h"
11891 +#include "yportenv.h"
11892 +#include "yaffs_tagsvalidity.h"
11893 +
11894 +/* This code packs a set of extended tags into a binary structure for
11895 + * NAND storage
11896 + */
11897 +
11898 +/* Some of the information is "extra" struff which can be packed in to
11899 + * speed scanning
11900 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
11901 + */
11902 +
11903 +/* Extra flags applied to chunkId */
11904 +
11905 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
11906 +#define EXTRA_SHRINK_FLAG 0x40000000
11907 +#define EXTRA_SHADOWS_FLAG 0x20000000
11908 +#define EXTRA_SPARE_FLAGS 0x10000000
11909 +
11910 +#define ALL_EXTRA_FLAGS 0xF0000000
11911 +
11912 +/* Also, the top 4 bits of the object Id are set to the object type. */
11913 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
11914 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
11915 +
11916 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
11917 +{
11918 + T(YAFFS_TRACE_MTD,
11919 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
11920 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
11921 + pt->t.sequenceNumber));
11922 +}
11923 +
11924 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
11925 +{
11926 + T(YAFFS_TRACE_MTD,
11927 + (TSTR
11928 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
11929 + "%d del %d ser %d seq %d"
11930 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
11931 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
11932 + t->sequenceNumber));
11933 +
11934 +}
11935 +
11936 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
11937 +{
11938 + pt->t.chunkId = t->chunkId;
11939 + pt->t.sequenceNumber = t->sequenceNumber;
11940 + pt->t.byteCount = t->byteCount;
11941 + pt->t.objectId = t->objectId;
11942 +
11943 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
11944 + /* Store the extra header info instead */
11945 + /* We save the parent object in the chunkId */
11946 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
11947 + | t->extraParentObjectId;
11948 + if (t->extraIsShrinkHeader) {
11949 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
11950 + }
11951 + if (t->extraShadows) {
11952 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
11953 + }
11954 +
11955 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
11956 + pt->t.objectId |=
11957 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
11958 +
11959 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
11960 + pt->t.byteCount = t->extraEquivalentObjectId;
11961 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
11962 + pt->t.byteCount = t->extraFileLength;
11963 + } else {
11964 + pt->t.byteCount = 0;
11965 + }
11966 + }
11967 +
11968 + yaffs_DumpPackedTags2(pt);
11969 + yaffs_DumpTags2(t);
11970 +
11971 +#ifndef YAFFS_IGNORE_TAGS_ECC
11972 + {
11973 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
11974 + sizeof(yaffs_PackedTags2TagsPart),
11975 + &pt->ecc);
11976 + }
11977 +#endif
11978 +}
11979 +
11980 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
11981 +{
11982 +
11983 + memset(t, 0, sizeof(yaffs_ExtendedTags));
11984 +
11985 + yaffs_InitialiseTags(t);
11986 +
11987 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
11988 + /* Page is in use */
11989 +#ifdef YAFFS_IGNORE_TAGS_ECC
11990 + {
11991 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
11992 + }
11993 +#else
11994 + {
11995 + yaffs_ECCOther ecc;
11996 + int result;
11997 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
11998 + sizeof
11999 + (yaffs_PackedTags2TagsPart),
12000 + &ecc);
12001 + result =
12002 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
12003 + sizeof
12004 + (yaffs_PackedTags2TagsPart),
12005 + &pt->ecc, &ecc);
12006 + switch(result){
12007 + case 0:
12008 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12009 + break;
12010 + case 1:
12011 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
12012 + break;
12013 + case -1:
12014 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
12015 + break;
12016 + default:
12017 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
12018 + }
12019 + }
12020 +#endif
12021 + t->blockBad = 0;
12022 + t->chunkUsed = 1;
12023 + t->objectId = pt->t.objectId;
12024 + t->chunkId = pt->t.chunkId;
12025 + t->byteCount = pt->t.byteCount;
12026 + t->chunkDeleted = 0;
12027 + t->serialNumber = 0;
12028 + t->sequenceNumber = pt->t.sequenceNumber;
12029 +
12030 + /* Do extra header info stuff */
12031 +
12032 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
12033 + t->chunkId = 0;
12034 + t->byteCount = 0;
12035 +
12036 + t->extraHeaderInfoAvailable = 1;
12037 + t->extraParentObjectId =
12038 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
12039 + t->extraIsShrinkHeader =
12040 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
12041 + t->extraShadows =
12042 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
12043 + t->extraObjectType =
12044 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
12045 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
12046 +
12047 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
12048 + t->extraEquivalentObjectId = pt->t.byteCount;
12049 + } else {
12050 + t->extraFileLength = pt->t.byteCount;
12051 + }
12052 + }
12053 + }
12054 +
12055 + yaffs_DumpPackedTags2(pt);
12056 + yaffs_DumpTags2(t);
12057 +
12058 +}
12059 diff -urN linux.old/fs/yaffs2/yaffs_packedtags2.h linux.dev/fs/yaffs2/yaffs_packedtags2.h
12060 --- linux.old/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
12061 +++ linux.dev/fs/yaffs2/yaffs_packedtags2.h 2006-12-14 04:21:47.000000000 +0100
12062 @@ -0,0 +1,23 @@
12063 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
12064 +
12065 +#ifndef __YAFFS_PACKEDTAGS2_H__
12066 +#define __YAFFS_PACKEDTAGS2_H__
12067 +
12068 +#include "yaffs_guts.h"
12069 +#include "yaffs_ecc.h"
12070 +
12071 +typedef struct {
12072 + unsigned sequenceNumber;
12073 + unsigned objectId;
12074 + unsigned chunkId;
12075 + unsigned byteCount;
12076 +} yaffs_PackedTags2TagsPart;
12077 +
12078 +typedef struct {
12079 + yaffs_PackedTags2TagsPart t;
12080 + yaffs_ECCOther ecc;
12081 +} yaffs_PackedTags2;
12082 +
12083 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
12084 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
12085 +#endif
12086 diff -urN linux.old/fs/yaffs2/yaffs_qsort.c linux.dev/fs/yaffs2/yaffs_qsort.c
12087 --- linux.old/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
12088 +++ linux.dev/fs/yaffs2/yaffs_qsort.c 2006-12-14 04:21:47.000000000 +0100
12089 @@ -0,0 +1,156 @@
12090 +/*
12091 + * Copyright (c) 1992, 1993
12092 + * The Regents of the University of California. All rights reserved.
12093 + *
12094 + * Redistribution and use in source and binary forms, with or without
12095 + * modification, are permitted provided that the following conditions
12096 + * are met:
12097 + * 1. Redistributions of source code must retain the above copyright
12098 + * notice, this list of conditions and the following disclaimer.
12099 + * 2. Redistributions in binary form must reproduce the above copyright
12100 + * notice, this list of conditions and the following disclaimer in the
12101 + * documentation and/or other materials provided with the distribution.
12102 + * 3. Neither the name of the University nor the names of its contributors
12103 + * may be used to endorse or promote products derived from this software
12104 + * without specific prior written permission.
12105 + *
12106 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
12107 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12108 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
12109 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
12110 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
12111 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
12112 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
12113 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
12114 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
12115 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
12116 + * SUCH DAMAGE.
12117 + */
12118 +
12119 +#include "yportenv.h"
12120 +//#include <linux/string.h>
12121 +
12122 +/*
12123 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
12124 + */
12125 +#define swapcode(TYPE, parmi, parmj, n) { \
12126 + long i = (n) / sizeof (TYPE); \
12127 + register TYPE *pi = (TYPE *) (parmi); \
12128 + register TYPE *pj = (TYPE *) (parmj); \
12129 + do { \
12130 + register TYPE t = *pi; \
12131 + *pi++ = *pj; \
12132 + *pj++ = t; \
12133 + } while (--i > 0); \
12134 +}
12135 +
12136 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
12137 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
12138 +
12139 +static __inline void
12140 +swapfunc(char *a, char *b, int n, int swaptype)
12141 +{
12142 + if (swaptype <= 1)
12143 + swapcode(long, a, b, n)
12144 + else
12145 + swapcode(char, a, b, n)
12146 +}
12147 +
12148 +#define swap(a, b) \
12149 + if (swaptype == 0) { \
12150 + long t = *(long *)(a); \
12151 + *(long *)(a) = *(long *)(b); \
12152 + *(long *)(b) = t; \
12153 + } else \
12154 + swapfunc(a, b, es, swaptype)
12155 +
12156 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
12157 +
12158 +static __inline char *
12159 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
12160 +{
12161 + return cmp(a, b) < 0 ?
12162 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
12163 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
12164 +}
12165 +
12166 +#define min(a,b) (((a) < (b)) ? (a) : (b))
12167 +void
12168 +qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
12169 +{
12170 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
12171 + int d, r, swaptype, swap_cnt;
12172 + register char *a = aa;
12173 +
12174 +loop: SWAPINIT(a, es);
12175 + swap_cnt = 0;
12176 + if (n < 7) {
12177 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
12178 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
12179 + pl -= es)
12180 + swap(pl, pl - es);
12181 + return;
12182 + }
12183 + pm = (char *)a + (n / 2) * es;
12184 + if (n > 7) {
12185 + pl = (char *)a;
12186 + pn = (char *)a + (n - 1) * es;
12187 + if (n > 40) {
12188 + d = (n / 8) * es;
12189 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
12190 + pm = med3(pm - d, pm, pm + d, cmp);
12191 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
12192 + }
12193 + pm = med3(pl, pm, pn, cmp);
12194 + }
12195 + swap(a, pm);
12196 + pa = pb = (char *)a + es;
12197 +
12198 + pc = pd = (char *)a + (n - 1) * es;
12199 + for (;;) {
12200 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
12201 + if (r == 0) {
12202 + swap_cnt = 1;
12203 + swap(pa, pb);
12204 + pa += es;
12205 + }
12206 + pb += es;
12207 + }
12208 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
12209 + if (r == 0) {
12210 + swap_cnt = 1;
12211 + swap(pc, pd);
12212 + pd -= es;
12213 + }
12214 + pc -= es;
12215 + }
12216 + if (pb > pc)
12217 + break;
12218 + swap(pb, pc);
12219 + swap_cnt = 1;
12220 + pb += es;
12221 + pc -= es;
12222 + }
12223 + if (swap_cnt == 0) { /* Switch to insertion sort */
12224 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
12225 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
12226 + pl -= es)
12227 + swap(pl, pl - es);
12228 + return;
12229 + }
12230 +
12231 + pn = (char *)a + n * es;
12232 + r = min(pa - (char *)a, pb - pa);
12233 + vecswap(a, pb - r, r);
12234 + r = min((long)(pd - pc), (long)(pn - pd - es));
12235 + vecswap(pb, pn - r, r);
12236 + if ((r = pb - pa) > es)
12237 + qsort(a, r / es, es, cmp);
12238 + if ((r = pd - pc) > es) {
12239 + /* Iterate rather than recurse to save stack space */
12240 + a = pn - r;
12241 + n = r / es;
12242 + goto loop;
12243 + }
12244 +/* qsort(pn - r, r / es, es, cmp);*/
12245 +}
12246 diff -urN linux.old/fs/yaffs2/yaffs_qsort.h linux.dev/fs/yaffs2/yaffs_qsort.h
12247 --- linux.old/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
12248 +++ linux.dev/fs/yaffs2/yaffs_qsort.h 2006-12-14 04:21:47.000000000 +0100
12249 @@ -0,0 +1,23 @@
12250 +/*
12251 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12252 + * yaffs_qsort.h: Interface to BSD-licensed qsort routine.
12253 + *
12254 + * Copyright (C) 2002 Aleph One Ltd.
12255 + * for Toby Churchill Ltd and Brightstar Engineering
12256 + *
12257 + * Created by Charles Manning <charles@aleph1.co.uk>
12258 + *
12259 + * This program is free software; you can redistribute it and/or modify
12260 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12261 + * published by the Free Software Foundation.
12262 + *
12263 + * $Id: yaffs_qsort.h,v 1.2 2006/11/07 23:20:09 charles Exp $
12264 + */
12265 +
12266 +#ifndef __YAFFS_QSORT_H__
12267 +#define __YAFFS_QSORT_H__
12268 +
12269 +extern void qsort (void *const base, size_t total_elems, size_t size,
12270 + int (*cmp)(const void *, const void *));
12271 +
12272 +#endif
12273 diff -urN linux.old/fs/yaffs2/yaffs_tagscompat.c linux.dev/fs/yaffs2/yaffs_tagscompat.c
12274 --- linux.old/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
12275 +++ linux.dev/fs/yaffs2/yaffs_tagscompat.c 2006-12-14 04:21:47.000000000 +0100
12276 @@ -0,0 +1,532 @@
12277 +/*
12278 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12279 + * yaffs_tagscompat.h: Tags compatability layer to use YAFFS1 formatted NAND.
12280 + *
12281 + * Copyright (C) 2002 Aleph One Ltd.
12282 + *
12283 + * Created by Charles Manning <charles@aleph1.co.uk>
12284 + *
12285 + * This program is free software; you can redistribute it and/or modify
12286 + * it under the terms of the GNU General Public License version 2 as
12287 + * published by the Free Software Foundation.
12288 + *
12289 + * $Id: yaffs_tagscompat.c,v 1.8 2005/11/29 20:54:32 marty Exp $
12290 + */
12291 +
12292 +#include "yaffs_guts.h"
12293 +#include "yaffs_tagscompat.h"
12294 +#include "yaffs_ecc.h"
12295 +
12296 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
12297 +#ifdef NOTYET
12298 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
12299 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
12300 + const __u8 * data,
12301 + const yaffs_Spare * spare);
12302 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
12303 + const yaffs_Spare * spare);
12304 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
12305 +#endif
12306 +
12307 +static const char yaffs_countBitsTable[256] = {
12308 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
12309 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12310 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12311 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12312 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12313 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12314 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12315 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12316 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
12317 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12318 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12319 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12320 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
12321 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12322 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
12323 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
12324 +};
12325 +
12326 +static int yaffs_CountBits(__u8 x)
12327 +{
12328 + int retVal;
12329 + retVal = yaffs_countBitsTable[x];
12330 + return retVal;
12331 +}
12332 +
12333 +/********** Tags ECC calculations *********/
12334 +
12335 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
12336 +{
12337 + yaffs_ECCCalculate(data, spare->ecc1);
12338 + yaffs_ECCCalculate(&data[256], spare->ecc2);
12339 +}
12340 +
12341 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
12342 +{
12343 + /* Calculate an ecc */
12344 +
12345 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
12346 + unsigned i, j;
12347 + unsigned ecc = 0;
12348 + unsigned bit = 0;
12349 +
12350 + tags->ecc = 0;
12351 +
12352 + for (i = 0; i < 8; i++) {
12353 + for (j = 1; j & 0xff; j <<= 1) {
12354 + bit++;
12355 + if (b[i] & j) {
12356 + ecc ^= bit;
12357 + }
12358 + }
12359 + }
12360 +
12361 + tags->ecc = ecc;
12362 +
12363 +}
12364 +
12365 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
12366 +{
12367 + unsigned ecc = tags->ecc;
12368 +
12369 + yaffs_CalcTagsECC(tags);
12370 +
12371 + ecc ^= tags->ecc;
12372 +
12373 + if (ecc && ecc <= 64) {
12374 + /* TODO: Handle the failure better. Retire? */
12375 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
12376 +
12377 + ecc--;
12378 +
12379 + b[ecc / 8] ^= (1 << (ecc & 7));
12380 +
12381 + /* Now recvalc the ecc */
12382 + yaffs_CalcTagsECC(tags);
12383 +
12384 + return 1; /* recovered error */
12385 + } else if (ecc) {
12386 + /* Wierd ecc failure value */
12387 + /* TODO Need to do somethiong here */
12388 + return -1; /* unrecovered error */
12389 + }
12390 +
12391 + return 0;
12392 +}
12393 +
12394 +/********** Tags **********/
12395 +
12396 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
12397 + yaffs_Tags * tagsPtr)
12398 +{
12399 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
12400 +
12401 + yaffs_CalcTagsECC(tagsPtr);
12402 +
12403 + sparePtr->tagByte0 = tu->asBytes[0];
12404 + sparePtr->tagByte1 = tu->asBytes[1];
12405 + sparePtr->tagByte2 = tu->asBytes[2];
12406 + sparePtr->tagByte3 = tu->asBytes[3];
12407 + sparePtr->tagByte4 = tu->asBytes[4];
12408 + sparePtr->tagByte5 = tu->asBytes[5];
12409 + sparePtr->tagByte6 = tu->asBytes[6];
12410 + sparePtr->tagByte7 = tu->asBytes[7];
12411 +}
12412 +
12413 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
12414 + yaffs_Tags * tagsPtr)
12415 +{
12416 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
12417 + int result;
12418 +
12419 + tu->asBytes[0] = sparePtr->tagByte0;
12420 + tu->asBytes[1] = sparePtr->tagByte1;
12421 + tu->asBytes[2] = sparePtr->tagByte2;
12422 + tu->asBytes[3] = sparePtr->tagByte3;
12423 + tu->asBytes[4] = sparePtr->tagByte4;
12424 + tu->asBytes[5] = sparePtr->tagByte5;
12425 + tu->asBytes[6] = sparePtr->tagByte6;
12426 + tu->asBytes[7] = sparePtr->tagByte7;
12427 +
12428 + result = yaffs_CheckECCOnTags(tagsPtr);
12429 + if (result > 0) {
12430 + dev->tagsEccFixed++;
12431 + } else if (result < 0) {
12432 + dev->tagsEccUnfixed++;
12433 + }
12434 +}
12435 +
12436 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
12437 +{
12438 + memset(spare, 0xFF, sizeof(yaffs_Spare));
12439 +}
12440 +
12441 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
12442 + int chunkInNAND, const __u8 * data,
12443 + yaffs_Spare * spare)
12444 +{
12445 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
12446 + T(YAFFS_TRACE_ERROR,
12447 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
12448 + chunkInNAND));
12449 + return YAFFS_FAIL;
12450 + }
12451 +
12452 + dev->nPageWrites++;
12453 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
12454 +}
12455 +
12456 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
12457 + int chunkInNAND,
12458 + __u8 * data,
12459 + yaffs_Spare * spare,
12460 + yaffs_ECCResult * eccResult,
12461 + int doErrorCorrection)
12462 +{
12463 + int retVal;
12464 + yaffs_Spare localSpare;
12465 +
12466 + dev->nPageReads++;
12467 +
12468 + if (!spare && data) {
12469 + /* If we don't have a real spare, then we use a local one. */
12470 + /* Need this for the calculation of the ecc */
12471 + spare = &localSpare;
12472 + }
12473 +
12474 + if (!dev->useNANDECC) {
12475 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
12476 + if (data && doErrorCorrection) {
12477 + /* Do ECC correction */
12478 + /* Todo handle any errors */
12479 + int eccResult1, eccResult2;
12480 + __u8 calcEcc[3];
12481 +
12482 + yaffs_ECCCalculate(data, calcEcc);
12483 + eccResult1 =
12484 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
12485 + yaffs_ECCCalculate(&data[256], calcEcc);
12486 + eccResult2 =
12487 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
12488 +
12489 + if (eccResult1 > 0) {
12490 + T(YAFFS_TRACE_ERROR,
12491 + (TSTR
12492 + ("**>>yaffs ecc error fix performed on chunk %d:0"
12493 + TENDSTR), chunkInNAND));
12494 + dev->eccFixed++;
12495 + } else if (eccResult1 < 0) {
12496 + T(YAFFS_TRACE_ERROR,
12497 + (TSTR
12498 + ("**>>yaffs ecc error unfixed on chunk %d:0"
12499 + TENDSTR), chunkInNAND));
12500 + dev->eccUnfixed++;
12501 + }
12502 +
12503 + if (eccResult2 > 0) {
12504 + T(YAFFS_TRACE_ERROR,
12505 + (TSTR
12506 + ("**>>yaffs ecc error fix performed on chunk %d:1"
12507 + TENDSTR), chunkInNAND));
12508 + dev->eccFixed++;
12509 + } else if (eccResult2 < 0) {
12510 + T(YAFFS_TRACE_ERROR,
12511 + (TSTR
12512 + ("**>>yaffs ecc error unfixed on chunk %d:1"
12513 + TENDSTR), chunkInNAND));
12514 + dev->eccUnfixed++;
12515 + }
12516 +
12517 + if (eccResult1 || eccResult2) {
12518 + /* We had a data problem on this page */
12519 + yaffs_HandleReadDataError(dev, chunkInNAND);
12520 + }
12521 +
12522 + if (eccResult1 < 0 || eccResult2 < 0)
12523 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
12524 + else if (eccResult1 > 0 || eccResult2 > 0)
12525 + *eccResult = YAFFS_ECC_RESULT_FIXED;
12526 + else
12527 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12528 + }
12529 + } else {
12530 + /* Must allocate enough memory for spare+2*sizeof(int) */
12531 + /* for ecc results from device. */
12532 + struct yaffs_NANDSpare nspare;
12533 + retVal =
12534 + dev->readChunkFromNAND(dev, chunkInNAND, data,
12535 + (yaffs_Spare *) & nspare);
12536 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
12537 + if (data && doErrorCorrection) {
12538 + if (nspare.eccres1 > 0) {
12539 + T(YAFFS_TRACE_ERROR,
12540 + (TSTR
12541 + ("**>>mtd ecc error fix performed on chunk %d:0"
12542 + TENDSTR), chunkInNAND));
12543 + } else if (nspare.eccres1 < 0) {
12544 + T(YAFFS_TRACE_ERROR,
12545 + (TSTR
12546 + ("**>>mtd ecc error unfixed on chunk %d:0"
12547 + TENDSTR), chunkInNAND));
12548 + }
12549 +
12550 + if (nspare.eccres2 > 0) {
12551 + T(YAFFS_TRACE_ERROR,
12552 + (TSTR
12553 + ("**>>mtd ecc error fix performed on chunk %d:1"
12554 + TENDSTR), chunkInNAND));
12555 + } else if (nspare.eccres2 < 0) {
12556 + T(YAFFS_TRACE_ERROR,
12557 + (TSTR
12558 + ("**>>mtd ecc error unfixed on chunk %d:1"
12559 + TENDSTR), chunkInNAND));
12560 + }
12561 +
12562 + if (nspare.eccres1 || nspare.eccres2) {
12563 + /* We had a data problem on this page */
12564 + yaffs_HandleReadDataError(dev, chunkInNAND);
12565 + }
12566 +
12567 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
12568 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
12569 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
12570 + *eccResult = YAFFS_ECC_RESULT_FIXED;
12571 + else
12572 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
12573 +
12574 + }
12575 + }
12576 + return retVal;
12577 +}
12578 +
12579 +#ifdef NOTYET
12580 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
12581 + int chunkInNAND)
12582 +{
12583 +
12584 + static int init = 0;
12585 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
12586 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
12587 + /* Might as well always allocate the larger size for */
12588 + /* dev->useNANDECC == true; */
12589 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
12590 +
12591 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
12592 +
12593 + if (!init) {
12594 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
12595 + init = 1;
12596 + }
12597 +
12598 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
12599 + return YAFFS_FAIL;
12600 + if (memcmp(cmpbuf, spare, 16))
12601 + return YAFFS_FAIL;
12602 +
12603 + return YAFFS_OK;
12604 +
12605 +}
12606 +#endif
12607 +
12608 +/*
12609 + * Functions for robustisizing
12610 + */
12611 +
12612 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
12613 +{
12614 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
12615 +
12616 + /* Mark the block for retirement */
12617 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
12618 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
12619 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
12620 +
12621 + /* TODO:
12622 + * Just do a garbage collection on the affected block
12623 + * then retire the block
12624 + * NB recursion
12625 + */
12626 +}
12627 +
12628 +#ifdef NOTYET
12629 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
12630 +{
12631 +}
12632 +
12633 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
12634 + const __u8 * data,
12635 + const yaffs_Spare * spare)
12636 +{
12637 +}
12638 +
12639 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
12640 + const yaffs_Spare * spare)
12641 +{
12642 +}
12643 +
12644 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
12645 +{
12646 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
12647 +
12648 + /* Mark the block for retirement */
12649 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
12650 + /* Delete the chunk */
12651 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
12652 +}
12653 +
12654 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
12655 + const yaffs_Spare * s0, const yaffs_Spare * s1)
12656 +{
12657 +
12658 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
12659 + s0->tagByte0 != s1->tagByte0 ||
12660 + s0->tagByte1 != s1->tagByte1 ||
12661 + s0->tagByte2 != s1->tagByte2 ||
12662 + s0->tagByte3 != s1->tagByte3 ||
12663 + s0->tagByte4 != s1->tagByte4 ||
12664 + s0->tagByte5 != s1->tagByte5 ||
12665 + s0->tagByte6 != s1->tagByte6 ||
12666 + s0->tagByte7 != s1->tagByte7 ||
12667 + s0->ecc1[0] != s1->ecc1[0] ||
12668 + s0->ecc1[1] != s1->ecc1[1] ||
12669 + s0->ecc1[2] != s1->ecc1[2] ||
12670 + s0->ecc2[0] != s1->ecc2[0] ||
12671 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
12672 + return 0;
12673 + }
12674 +
12675 + return 1;
12676 +}
12677 +#endif /* NOTYET */
12678 +
12679 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
12680 + int chunkInNAND,
12681 + const __u8 * data,
12682 + const yaffs_ExtendedTags *
12683 + eTags)
12684 +{
12685 + yaffs_Spare spare;
12686 + yaffs_Tags tags;
12687 +
12688 + yaffs_SpareInitialise(&spare);
12689 +
12690 + if (eTags->chunkDeleted) {
12691 + spare.pageStatus = 0;
12692 + } else {
12693 + tags.objectId = eTags->objectId;
12694 + tags.chunkId = eTags->chunkId;
12695 + tags.byteCount = eTags->byteCount;
12696 + tags.serialNumber = eTags->serialNumber;
12697 +
12698 + if (!dev->useNANDECC && data) {
12699 + yaffs_CalcECC(data, &spare);
12700 + }
12701 + yaffs_LoadTagsIntoSpare(&spare, &tags);
12702 +
12703 + }
12704 +
12705 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
12706 +}
12707 +
12708 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
12709 + int chunkInNAND,
12710 + __u8 * data,
12711 + yaffs_ExtendedTags * eTags)
12712 +{
12713 +
12714 + yaffs_Spare spare;
12715 + yaffs_Tags tags;
12716 + yaffs_ECCResult eccResult;
12717 +
12718 + static yaffs_Spare spareFF;
12719 + static int init;
12720 +
12721 + if (!init) {
12722 + memset(&spareFF, 0xFF, sizeof(spareFF));
12723 + init = 1;
12724 + }
12725 +
12726 + if (yaffs_ReadChunkFromNAND
12727 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
12728 + /* eTags may be NULL */
12729 + if (eTags) {
12730 +
12731 + int deleted =
12732 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
12733 +
12734 + eTags->chunkDeleted = deleted;
12735 + eTags->eccResult = eccResult;
12736 + eTags->blockBad = 0; /* We're reading it */
12737 + /* therefore it is not a bad block */
12738 + eTags->chunkUsed =
12739 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
12740 + 0) ? 1 : 0;
12741 +
12742 + if (eTags->chunkUsed) {
12743 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
12744 +
12745 + eTags->objectId = tags.objectId;
12746 + eTags->chunkId = tags.chunkId;
12747 + eTags->byteCount = tags.byteCount;
12748 + eTags->serialNumber = tags.serialNumber;
12749 + }
12750 + }
12751 +
12752 + return YAFFS_OK;
12753 + } else {
12754 + return YAFFS_FAIL;
12755 + }
12756 +}
12757 +
12758 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
12759 + int blockInNAND)
12760 +{
12761 +
12762 + yaffs_Spare spare;
12763 +
12764 + memset(&spare, 0xff, sizeof(yaffs_Spare));
12765 +
12766 + spare.blockStatus = 'Y';
12767 +
12768 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
12769 + &spare);
12770 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
12771 + NULL, &spare);
12772 +
12773 + return YAFFS_OK;
12774 +
12775 +}
12776 +
12777 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
12778 + int blockNo, yaffs_BlockState *
12779 + state,
12780 + int *sequenceNumber)
12781 +{
12782 +
12783 + yaffs_Spare spare0, spare1;
12784 + static yaffs_Spare spareFF;
12785 + static int init;
12786 + yaffs_ECCResult dummy;
12787 +
12788 + if (!init) {
12789 + memset(&spareFF, 0xFF, sizeof(spareFF));
12790 + init = 1;
12791 + }
12792 +
12793 + *sequenceNumber = 0;
12794 +
12795 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
12796 + &spare0, &dummy, 1);
12797 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
12798 + &spare1, &dummy, 1);
12799 +
12800 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
12801 + *state = YAFFS_BLOCK_STATE_DEAD;
12802 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
12803 + *state = YAFFS_BLOCK_STATE_EMPTY;
12804 + else
12805 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12806 +
12807 + return YAFFS_OK;
12808 +}
12809 diff -urN linux.old/fs/yaffs2/yaffs_tagscompat.h linux.dev/fs/yaffs2/yaffs_tagscompat.h
12810 --- linux.old/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
12811 +++ linux.dev/fs/yaffs2/yaffs_tagscompat.h 2006-12-14 04:21:47.000000000 +0100
12812 @@ -0,0 +1,40 @@
12813 +/*
12814 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12815 + * yaffs_ramdisk.h: yaffs ram disk component
12816 + *
12817 + * Copyright (C) 2002 Aleph One Ltd.
12818 + *
12819 + * Created by Charles Manning <charles@aleph1.co.uk>
12820 + *
12821 + * This program is free software; you can redistribute it and/or modify
12822 + * it under the terms of the GNU General Public License version 2 as
12823 + * published by the Free Software Foundation.
12824 + *
12825 + * $Id: yaffs_tagscompat.h,v 1.2 2005/08/11 02:33:03 marty Exp $
12826 + */
12827 +
12828 +/* This provides a ram disk under yaffs.
12829 + * NB this is not intended for NAND emulation.
12830 + * Use this with dev->useNANDECC enabled, then ECC overheads are not required.
12831 + */
12832 +#ifndef __YAFFS_TAGSCOMPAT_H__
12833 +#define __YAFFS_TAGSCOMPAT_H__
12834 +
12835 +#include "yaffs_guts.h"
12836 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
12837 + int chunkInNAND,
12838 + const __u8 * data,
12839 + const yaffs_ExtendedTags *
12840 + tags);
12841 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
12842 + int chunkInNAND,
12843 + __u8 * data,
12844 + yaffs_ExtendedTags *
12845 + tags);
12846 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
12847 + int blockNo);
12848 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
12849 + int blockNo, yaffs_BlockState *
12850 + state, int *sequenceNumber);
12851 +
12852 +#endif
12853 diff -urN linux.old/fs/yaffs2/yaffs_tagsvalidity.c linux.dev/fs/yaffs2/yaffs_tagsvalidity.c
12854 --- linux.old/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
12855 +++ linux.dev/fs/yaffs2/yaffs_tagsvalidity.c 2006-12-14 04:21:47.000000000 +0100
12856 @@ -0,0 +1,31 @@
12857 +
12858 +/*
12859 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12860 + *
12861 + * Copyright (C) 2002 Aleph One Ltd.
12862 + * for Toby Churchill Ltd and Brightstar Engineering
12863 + *
12864 + * Created by Charles Manning <charles@aleph1.co.uk>
12865 + *
12866 + * This program is free software; you can redistribute it and/or modify
12867 + * it under the terms of the GNU General Public License version 2 as
12868 + * published by the Free Software Foundation.
12869 + *
12870 + * $Id: yaffs_tagsvalidity.c,v 1.2 2005/08/11 02:33:03 marty Exp $
12871 + */
12872 +
12873 +#include "yaffs_tagsvalidity.h"
12874 +
12875 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
12876 +{
12877 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
12878 + tags->validMarker0 = 0xAAAAAAAA;
12879 + tags->validMarker1 = 0x55555555;
12880 +}
12881 +
12882 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
12883 +{
12884 + return (tags->validMarker0 == 0xAAAAAAAA &&
12885 + tags->validMarker1 == 0x55555555);
12886 +
12887 +}
12888 diff -urN linux.old/fs/yaffs2/yaffs_tagsvalidity.h linux.dev/fs/yaffs2/yaffs_tagsvalidity.h
12889 --- linux.old/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
12890 +++ linux.dev/fs/yaffs2/yaffs_tagsvalidity.h 2006-12-14 04:21:47.000000000 +0100
12891 @@ -0,0 +1,25 @@
12892 +
12893 +/*
12894 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12895 + *
12896 + * Copyright (C) 2002 Aleph One Ltd.
12897 + * for Toby Churchill Ltd and Brightstar Engineering
12898 + *
12899 + * Created by Charles Manning <charles@aleph1.co.uk>
12900 + *
12901 + * This program is free software; you can redistribute it and/or modify
12902 + * it under the terms of the GNU General Public License version 2 as
12903 + * published by the Free Software Foundation.
12904 + *
12905 + * $Id: yaffs_tagsvalidity.h,v 1.2 2005/08/11 02:33:03 marty Exp $
12906 + */
12907 +//yaffs_tagsvalidity.h
12908 +
12909 +#ifndef __YAFFS_TAGS_VALIDITY_H__
12910 +#define __YAFFS_TAGS_VALIDITY_H__
12911 +
12912 +#include "yaffs_guts.h"
12913 +
12914 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12915 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
12916 +#endif
12917 diff -urN linux.old/fs/yaffs2/yportenv.h linux.dev/fs/yaffs2/yportenv.h
12918 --- linux.old/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
12919 +++ linux.dev/fs/yaffs2/yportenv.h 2006-12-14 04:26:06.000000000 +0100
12920 @@ -0,0 +1,165 @@
12921 +/*
12922 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12923 + * yportenv.h: Portable services used by yaffs. This is done to allow
12924 + * simple migration from kernel space into app space for testing.
12925 + *
12926 + * Copyright (C) 2002 Aleph One Ltd.
12927 + * for Toby Churchill Ltd and Brightstar Engineering
12928 + *
12929 + * Created by Charles Manning <charles@aleph1.co.uk>
12930 + *
12931 + * This program is free software; you can redistribute it and/or modify
12932 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12933 + * published by the Free Software Foundation.
12934 + *
12935 + *
12936 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12937 + *
12938 + * $Id: yportenv.h,v 1.11 2006/05/21 09:39:12 charles Exp $
12939 + *
12940 + */
12941 +
12942 +#ifndef __YPORTENV_H__
12943 +#define __YPORTENV_H__
12944 +
12945 +#if defined CONFIG_YAFFS_WINCE
12946 +
12947 +#include "ywinceenv.h"
12948 +
12949 +#elif defined __KERNEL__
12950 +
12951 +#include "moduleconfig.h"
12952 +
12953 +/* Linux kernel */
12954 +#include <linux/autoconf.h>
12955 +#include <linux/kernel.h>
12956 +#include <linux/version.h>
12957 +#include <linux/mm.h>
12958 +#include <linux/string.h>
12959 +#include <linux/slab.h>
12960 +#include <linux/vmalloc.h>
12961 +
12962 +#define YCHAR char
12963 +#define YUCHAR unsigned char
12964 +#define _Y(x) x
12965 +#define yaffs_strcpy(a,b) strcpy(a,b)
12966 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
12967 +#define yaffs_strlen(s) strlen(s)
12968 +#define yaffs_sprintf sprintf
12969 +#define yaffs_toupper(a) toupper(a)
12970 +
12971 +#define Y_INLINE inline
12972 +
12973 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
12974 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
12975 +
12976 +/* #define YPRINTF(x) printk x */
12977 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
12978 +#define YFREE(x) kfree(x)
12979 +#define YMALLOC_ALT(x) vmalloc(x)
12980 +#define YFREE_ALT(x) vfree(x)
12981 +#define YMALLOC_DMA(x) YMALLOC(x)
12982 +
12983 +// KR - added for use in scan so processes aren't blocked indefinitely.
12984 +#define YYIELD() schedule()
12985 +
12986 +#define YAFFS_ROOT_MODE 0666
12987 +#define YAFFS_LOSTNFOUND_MODE 0666
12988 +
12989 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
12990 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
12991 +#define Y_TIME_CONVERT(x) (x).tv_sec
12992 +#else
12993 +#define Y_CURRENT_TIME CURRENT_TIME
12994 +#define Y_TIME_CONVERT(x) (x)
12995 +#endif
12996 +
12997 +#define yaffs_SumCompare(x,y) ((x) == (y))
12998 +#define yaffs_strcmp(a,b) strcmp(a,b)
12999 +
13000 +#define TENDSTR "\n"
13001 +#define TSTR(x) KERN_WARNING x
13002 +#define TOUT(p) printk p
13003 +
13004 +#elif defined CONFIG_YAFFS_DIRECT
13005 +
13006 +/* Direct interface */
13007 +#include "ydirectenv.h"
13008 +
13009 +#elif defined CONFIG_YAFFS_UTIL
13010 +
13011 +/* Stuff for YAFFS utilities */
13012 +
13013 +#include "stdlib.h"
13014 +#include "stdio.h"
13015 +#include "string.h"
13016 +
13017 +#include "devextras.h"
13018 +
13019 +#define YMALLOC(x) malloc(x)
13020 +#define YFREE(x) free(x)
13021 +#define YMALLOC_ALT(x) malloc(x)
13022 +#define YFREE_ALT(x) free(x)
13023 +
13024 +#define YCHAR char
13025 +#define YUCHAR unsigned char
13026 +#define _Y(x) x
13027 +#define yaffs_strcpy(a,b) strcpy(a,b)
13028 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
13029 +#define yaffs_strlen(s) strlen(s)
13030 +#define yaffs_sprintf sprintf
13031 +#define yaffs_toupper(a) toupper(a)
13032 +
13033 +#define Y_INLINE inline
13034 +
13035 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
13036 +/* #define YALERT(s) YINFO(s) */
13037 +
13038 +#define TENDSTR "\n"
13039 +#define TSTR(x) x
13040 +#define TOUT(p) printf p
13041 +
13042 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
13043 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
13044 +/* #define YPRINTF(x) printf x */
13045 +
13046 +#define YAFFS_ROOT_MODE 0666
13047 +#define YAFFS_LOSTNFOUND_MODE 0666
13048 +
13049 +#define yaffs_SumCompare(x,y) ((x) == (y))
13050 +#define yaffs_strcmp(a,b) strcmp(a,b)
13051 +
13052 +#else
13053 +/* Should have specified a configuration type */
13054 +#error Unknown configuration
13055 +
13056 +#endif
13057 +
13058 +extern unsigned yaffs_traceMask;
13059 +
13060 +#define YAFFS_TRACE_ERROR 0x00000001
13061 +#define YAFFS_TRACE_OS 0x00000002
13062 +#define YAFFS_TRACE_ALLOCATE 0x00000004
13063 +#define YAFFS_TRACE_SCAN 0x00000008
13064 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
13065 +#define YAFFS_TRACE_ERASE 0x00000020
13066 +#define YAFFS_TRACE_GC 0x00000040
13067 +#define YAFFS_TRACE_WRITE 0x00000080
13068 +#define YAFFS_TRACE_TRACING 0x00000100
13069 +#define YAFFS_TRACE_DELETION 0x00000200
13070 +#define YAFFS_TRACE_BUFFERS 0x00000400
13071 +#define YAFFS_TRACE_NANDACCESS 0x00000800
13072 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
13073 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
13074 +#define YAFFS_TRACE_MTD 0x00004000
13075 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
13076 +#define YAFFS_TRACE_ALWAYS 0x40000000
13077 +#define YAFFS_TRACE_BUG 0x80000000
13078 +
13079 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ERROR)) TOUT(p);} while(0)
13080 +
13081 +#ifndef CONFIG_YAFFS_WINCE
13082 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))
13083 +#endif
13084 +
13085 +#endif
This page took 0.804189 seconds and 5 git commands to generate.