3 @@ -35,7 +35,6 @@ source "fs/gfs2/Kconfig"
4 source "fs/ocfs2/Kconfig"
5 source "fs/btrfs/Kconfig"
6 source "fs/nilfs2/Kconfig"
7 -source "fs/yaffs2/Kconfig"
11 @@ -176,6 +175,10 @@ source "fs/hfsplus/Kconfig"
12 source "fs/befs/Kconfig"
13 source "fs/bfs/Kconfig"
14 source "fs/efs/Kconfig"
17 +source "fs/yaffs2/Kconfig"
19 source "fs/jffs2/Kconfig"
20 # UBIFS File system configuration
21 source "fs/ubifs/Kconfig"
24 @@ -122,5 +122,6 @@ obj-$(CONFIG_BTRFS_FS) += btrfs/
25 obj-$(CONFIG_GFS2_FS) += gfs2/
26 obj-$(CONFIG_EXOFS_FS) += exofs/
27 obj-$(CONFIG_CEPH_FS) += ceph/
28 -obj-$(CONFIG_YAFFS_FS) += yaffs2/
31 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
32 --- a/fs/yaffs2/devextras.h
33 +++ b/fs/yaffs2/devextras.h
36 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
38 - * Copyright (C) 2002-2007 Aleph One Ltd.
39 + * Copyright (C) 2002-2010 Aleph One Ltd.
40 * for Toby Churchill Ltd and Brightstar Engineering
42 * Created by Charles Manning <charles@aleph1.co.uk>
47 +#include "yportenv.h"
49 #if !(defined __KERNEL__)
51 /* Definition of types */
52 @@ -33,103 +35,6 @@ typedef unsigned __u32;
57 - * This is a simple doubly linked list implementation that matches the
58 - * way the Linux kernel doubly linked list implementation works.
62 - struct ylist_head *next; /* next in chain */
63 - struct ylist_head *prev; /* previous in chain */
67 -/* Initialise a static list */
68 -#define YLIST_HEAD(name) \
69 -struct ylist_head name = { &(name), &(name)}
73 -/* Initialise a list head to an empty list */
74 -#define YINIT_LIST_HEAD(p) \
81 -/* Add an element to a list */
82 -static __inline__ void ylist_add(struct ylist_head *newEntry,
83 - struct ylist_head *list)
85 - struct ylist_head *listNext = list->next;
87 - list->next = newEntry;
88 - newEntry->prev = list;
89 - newEntry->next = listNext;
90 - listNext->prev = newEntry;
94 -static __inline__ void ylist_add_tail(struct ylist_head *newEntry,
95 - struct ylist_head *list)
97 - struct ylist_head *listPrev = list->prev;
99 - list->prev = newEntry;
100 - newEntry->next = list;
101 - newEntry->prev = listPrev;
102 - listPrev->next = newEntry;
107 -/* Take an element out of its current list, with or without
108 - * reinitialising the links.of the entry*/
109 -static __inline__ void ylist_del(struct ylist_head *entry)
111 - struct ylist_head *listNext = entry->next;
112 - struct ylist_head *listPrev = entry->prev;
114 - listNext->prev = listPrev;
115 - listPrev->next = listNext;
119 -static __inline__ void ylist_del_init(struct ylist_head *entry)
122 - entry->next = entry->prev = entry;
126 -/* Test if the list is empty */
127 -static __inline__ int ylist_empty(struct ylist_head *entry)
129 - return (entry->next == entry);
133 -/* ylist_entry takes a pointer to a list entry and offsets it to that
134 - * we can find a pointer to the object it is embedded in.
138 -#define ylist_entry(entry, type, member) \
139 - ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
142 -/* ylist_for_each and list_for_each_safe iterate over lists.
143 - * ylist_for_each_safe uses temporary storage to make the list delete safe
146 -#define ylist_for_each(itervar, list) \
147 - for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
149 -#define ylist_for_each_safe(itervar, saveVar, list) \
150 - for (itervar = (list)->next, saveVar = (list)->next->next; \
151 - itervar != (list); itervar = saveVar, saveVar = saveVar->next)
154 #if !(defined __KERNEL__)
156 --- a/fs/yaffs2/Kconfig
157 +++ b/fs/yaffs2/Kconfig
158 @@ -90,23 +90,15 @@ config YAFFS_AUTO_YAFFS2
162 -config YAFFS_DISABLE_LAZY_LOAD
163 - bool "Disable lazy loading"
164 - depends on YAFFS_YAFFS2
165 +config YAFFS_DISABLE_TAGS_ECC
166 + bool "Disable YAFFS from doing ECC on tags by default"
167 + depends on YAFFS_FS && YAFFS_YAFFS2
170 - "Lazy loading" defers loading file details until they are
171 - required. This saves mount time, but makes the first look-up
174 - Lazy loading will only happen if enabled by this option being 'n'
175 - and if the appropriate tags are available, else yaffs2 will
176 - automatically fall back to immediate loading and do the right
179 - Lazy laoding will be required by checkpointing.
181 - Setting this to 'y' will disable lazy loading.
182 + This defaults Yaffs to using its own ECC calculations on tags instead of
183 + just relying on the MTD.
184 + This behavior can also be overridden with tags_ecc_on and
185 + tags_ecc_off mount options.
189 @@ -154,3 +146,45 @@ config YAFFS_SHORT_NAMES_IN_RAM
190 but makes look-ups faster.
194 +config YAFFS_EMPTY_LOST_AND_FOUND
195 + bool "Empty lost and found on boot"
196 + depends on YAFFS_FS
199 + If this is enabled then the contents of lost and found is
200 + automatically dumped at mount.
204 +config YAFFS_DISABLE_BLOCK_REFRESHING
205 + bool "Disable yaffs2 block refreshing"
206 + depends on YAFFS_FS
209 + If this is set, then block refreshing is disabled.
210 + Block refreshing infrequently refreshes the oldest block in
211 + a yaffs2 file system. This mechanism helps to refresh flash to
212 + mitigate against data loss. This is particularly useful for MLC.
216 +config YAFFS_DISABLE_BACKGROUND
217 + bool "Disable yaffs2 background processing"
218 + depends on YAFFS_FS
221 + If this is set, then background processing is disabled.
222 + Background processing makes many foreground activities faster.
227 + bool "Enable yaffs2 xattr support"
228 + depends on YAFFS_FS
231 + If this is set then yaffs2 will provide xattr support.
235 --- a/fs/yaffs2/Makefile
236 +++ b/fs/yaffs2/Makefile
239 obj-$(CONFIG_YAFFS_FS) += yaffs.o
241 -yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
242 -yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
243 +yaffs-y := yaffs_ecc.o yaffs_vfs_glue.o yaffs_guts.o yaffs_checkptrw.o
244 +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
245 yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
246 yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
247 +yaffs-y += yaffs_nameval.o
248 +yaffs-y += yaffs_allocator.o
249 +yaffs-y += yaffs_yaffs1.o
250 +yaffs-y += yaffs_yaffs2.o
251 +yaffs-y += yaffs_bitmap.o
252 +yaffs-y += yaffs_verify.o
254 --- a/fs/yaffs2/moduleconfig.h
255 +++ b/fs/yaffs2/moduleconfig.h
258 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
260 - * Copyright (C) 2002-2007 Aleph One Ltd.
261 + * Copyright (C) 2002-2010 Aleph One Ltd.
262 * for Toby Churchill Ltd and Brightstar Engineering
264 * Created by Martin Fouts <Martin.Fouts@palmsource.com>
266 /* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
267 /* #define CONFIG_YAFFS_DOES_ECC */
269 +/* Default: Selected */
270 +/* Meaning: Yaffs does its own ECC on tags for packed tags rather than use mtd */
271 +#define CONFIG_YAFFS_DOES_TAGS_ECC
273 /* Default: Not selected */
274 /* Meaning: ECC byte order is 'wrong'. Only meaningful if */
275 /* CONFIG_YAFFS_DOES_ECC is set */
276 /* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
278 -/* Default: Selected */
279 -/* Meaning: Disables testing whether chunks are erased before writing to them*/
280 -#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
281 +/* Default: Not selected */
282 +/* Meaning: Always test whether chunks are erased before writing to them.
283 + Use during mtd debugging and init. */
284 +/* #define CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED */
286 +/* Default: Not Selected */
287 +/* Meaning: At mount automatically empty all files from lost and found. */
288 +/* This is done to fix an old problem where rmdir was not checking for an */
289 +/* empty directory. This can also be achieved with a mount option. */
290 +#define CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
292 /* Default: Selected */
293 /* Meaning: Cache short names, taking more RAM, but faster look-ups */
294 #define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
297 -/* Meaning: set the count of blocks to reserve for checkpointing */
298 -#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
299 +/* Default: Unselected */
300 +/* Meaning: Select to disable block refreshing. */
301 +/* Block Refreshing periodically rewrites the oldest block. */
302 +/* #define CONFIG_DISABLE_BLOCK_REFRESHING */
304 +/* Default: Unselected */
305 +/* Meaning: Select to disable background processing */
306 +/* #define CONFIG_DISABLE_BACKGROUND */
309 +/* Default: Selected */
310 +/* Meaning: Enable XATTR support */
311 +#define CONFIG_YAFFS_XATTR
314 -Older-style on-NAND data format has a "pageStatus" byte to record
315 +Older-style on-NAND data format has a "page_status" byte to record
316 chunk/page state. This byte is zeroed when the page is discarded.
317 Choose this option if you have existing on-NAND data in this format
318 that you need to continue to support. New data written also uses the
319 @@ -57,7 +78,7 @@ adjusted to use the older-style format.
320 MTD versions in yaffs_mtdif1.c.
322 /* Default: Not selected */
323 -/* Meaning: Use older-style on-NAND data format with pageStatus byte */
324 +/* Meaning: Use older-style on-NAND data format with page_status byte */
325 /* #define CONFIG_YAFFS_9BYTE_TAGS */
327 #endif /* YAFFS_OUT_OF_TREE */
329 +++ b/fs/yaffs2/yaffs_allocator.c
332 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
334 + * Copyright (C) 2002-2010 Aleph One Ltd.
335 + * for Toby Churchill Ltd and Brightstar Engineering
337 + * Created by Charles Manning <charles@aleph1.co.uk>
339 + * This program is free software; you can redistribute it and/or modify
340 + * it under the terms of the GNU Lesser General Public License version 2.1 as
341 + * published by the Free Software Foundation.
343 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
347 +#include "yaffs_allocator.h"
348 +#include "yaffs_guts.h"
349 +#include "yaffs_trace.h"
350 +#include "yportenv.h"
352 +#ifdef CONFIG_YAFFS_YMALLOC_ALLOCATOR
354 +void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
359 +void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
364 +yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
366 + return (yaffs_tnode_t *)YMALLOC(dev->tnode_size);
369 +void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
375 +void yaffs_init_raw_objs(yaffs_dev_t *dev)
380 +void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
385 +yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
388 + return (yaffs_obj_t *) YMALLOC(sizeof(yaffs_obj_t));
392 +void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
401 +struct yaffs_tnode_list {
402 + struct yaffs_tnode_list *next;
403 + yaffs_tnode_t *tnodes;
406 +typedef struct yaffs_tnode_list yaffs_tnodelist_t;
408 +struct yaffs_obj_tList_struct {
409 + yaffs_obj_t *objects;
410 + struct yaffs_obj_tList_struct *next;
413 +typedef struct yaffs_obj_tList_struct yaffs_obj_tList;
416 +struct yaffs_AllocatorStruct {
417 + int n_tnodesCreated;
418 + yaffs_tnode_t *freeTnodes;
420 + yaffs_tnodelist_t *allocatedTnodeList;
423 + yaffs_obj_t *freeObjects;
426 + yaffs_obj_tList *allocatedObjectList;
429 +typedef struct yaffs_AllocatorStruct yaffs_Allocator;
432 +static void yaffs_deinit_raw_tnodes(yaffs_dev_t *dev)
435 + yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
437 + yaffs_tnodelist_t *tmp;
444 + while (allocator->allocatedTnodeList) {
445 + tmp = allocator->allocatedTnodeList->next;
447 + YFREE(allocator->allocatedTnodeList->tnodes);
448 + YFREE(allocator->allocatedTnodeList);
449 + allocator->allocatedTnodeList = tmp;
453 + allocator->freeTnodes = NULL;
454 + allocator->nFreeTnodes = 0;
455 + allocator->n_tnodesCreated = 0;
458 +static void yaffs_init_raw_tnodes(yaffs_dev_t *dev)
460 + yaffs_Allocator *allocator = dev->allocator;
463 + allocator->allocatedTnodeList = NULL;
464 + allocator->freeTnodes = NULL;
465 + allocator->nFreeTnodes = 0;
466 + allocator->n_tnodesCreated = 0;
471 +static int yaffs_create_tnodes(yaffs_dev_t *dev, int n_tnodes)
473 + yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
475 + yaffs_tnode_t *newTnodes;
477 + yaffs_tnode_t *curr;
478 + yaffs_tnode_t *next;
479 + yaffs_tnodelist_t *tnl;
490 + /* make these things */
492 + newTnodes = YMALLOC(n_tnodes * dev->tnode_size);
493 + mem = (__u8 *)newTnodes;
496 + T(YAFFS_TRACE_ERROR,
497 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
501 + /* New hookup for wide tnodes */
502 + for (i = 0; i < n_tnodes - 1; i++) {
503 + curr = (yaffs_tnode_t *) &mem[i * dev->tnode_size];
504 + next = (yaffs_tnode_t *) &mem[(i+1) * dev->tnode_size];
505 + curr->internal[0] = next;
508 + curr = (yaffs_tnode_t *) &mem[(n_tnodes - 1) * dev->tnode_size];
509 + curr->internal[0] = allocator->freeTnodes;
510 + allocator->freeTnodes = (yaffs_tnode_t *)mem;
512 + allocator->nFreeTnodes += n_tnodes;
513 + allocator->n_tnodesCreated += n_tnodes;
515 + /* Now add this bunch of tnodes to a list for freeing up.
516 + * NB If we can't add this to the management list it isn't fatal
517 + * but it just means we can't free this bunch of tnodes later.
520 + tnl = YMALLOC(sizeof(yaffs_tnodelist_t));
522 + T(YAFFS_TRACE_ERROR,
524 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
527 + tnl->tnodes = newTnodes;
528 + tnl->next = allocator->allocatedTnodeList;
529 + allocator->allocatedTnodeList = tnl;
532 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
538 +yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
540 + yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
541 + yaffs_tnode_t *tn = NULL;
548 + /* If there are none left make more */
549 + if (!allocator->freeTnodes)
550 + yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
552 + if (allocator->freeTnodes) {
553 + tn = allocator->freeTnodes;
554 + allocator->freeTnodes = allocator->freeTnodes->internal[0];
555 + allocator->nFreeTnodes--;
561 +/* FreeTnode frees up a tnode and puts it back on the free list */
562 +void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
564 + yaffs_Allocator *allocator = dev->allocator;
572 + tn->internal[0] = allocator->freeTnodes;
573 + allocator->freeTnodes = tn;
574 + allocator->nFreeTnodes++;
576 + dev->checkpoint_blocks_required = 0; /* force recalculation*/
581 +static void yaffs_init_raw_objs(yaffs_dev_t *dev)
583 + yaffs_Allocator *allocator = dev->allocator;
586 + allocator->allocatedObjectList = NULL;
587 + allocator->freeObjects = NULL;
588 + allocator->nFreeObjects = 0;
593 +static void yaffs_deinit_raw_objs(yaffs_dev_t *dev)
595 + yaffs_Allocator *allocator = dev->allocator;
596 + yaffs_obj_tList *tmp;
603 + while (allocator->allocatedObjectList) {
604 + tmp = allocator->allocatedObjectList->next;
605 + YFREE(allocator->allocatedObjectList->objects);
606 + YFREE(allocator->allocatedObjectList);
608 + allocator->allocatedObjectList = tmp;
611 + allocator->freeObjects = NULL;
612 + allocator->nFreeObjects = 0;
613 + allocator->n_objCreated = 0;
617 +static int yaffs_create_free_objs(yaffs_dev_t *dev, int n_obj)
619 + yaffs_Allocator *allocator = dev->allocator;
622 + yaffs_obj_t *newObjects;
623 + yaffs_obj_tList *list;
633 + /* make these things */
634 + newObjects = YMALLOC(n_obj * sizeof(yaffs_obj_t));
635 + list = YMALLOC(sizeof(yaffs_obj_tList));
637 + if (!newObjects || !list) {
646 + T(YAFFS_TRACE_ALLOCATE,
647 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
651 + /* Hook them into the free list */
652 + for (i = 0; i < n_obj - 1; i++) {
653 + newObjects[i].siblings.next =
654 + (struct ylist_head *)(&newObjects[i + 1]);
657 + newObjects[n_obj - 1].siblings.next = (void *)allocator->freeObjects;
658 + allocator->freeObjects = newObjects;
659 + allocator->nFreeObjects += n_obj;
660 + allocator->n_objCreated += n_obj;
662 + /* Now add this bunch of Objects to a list for freeing up. */
664 + list->objects = newObjects;
665 + list->next = allocator->allocatedObjectList;
666 + allocator->allocatedObjectList = list;
671 +yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
673 + yaffs_obj_t *obj = NULL;
674 + yaffs_Allocator *allocator = dev->allocator;
681 + /* If there are none left make more */
682 + if (!allocator->freeObjects)
683 + yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
685 + if (allocator->freeObjects) {
686 + obj = allocator->freeObjects;
687 + allocator->freeObjects =
688 + (yaffs_obj_t *) (allocator->freeObjects->siblings.next);
689 + allocator->nFreeObjects--;
696 +void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
699 + yaffs_Allocator *allocator = dev->allocator;
704 + /* Link into the free list. */
705 + obj->siblings.next = (struct ylist_head *)(allocator->freeObjects);
706 + allocator->freeObjects = obj;
707 + allocator->nFreeObjects++;
711 +void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
713 + if(dev->allocator){
714 + yaffs_deinit_raw_tnodes(dev);
715 + yaffs_deinit_raw_objs(dev);
717 + YFREE(dev->allocator);
718 + dev->allocator=NULL;
723 +void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
725 + yaffs_Allocator *allocator;
727 + if(!dev->allocator){
728 + allocator = YMALLOC(sizeof(yaffs_Allocator));
730 + dev->allocator = allocator;
731 + yaffs_init_raw_tnodes(dev);
732 + yaffs_init_raw_objs(dev);
741 +++ b/fs/yaffs2/yaffs_allocator.h
744 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
746 + * Copyright (C) 2002-2010 Aleph One Ltd.
747 + * for Toby Churchill Ltd and Brightstar Engineering
749 + * Created by Charles Manning <charles@aleph1.co.uk>
751 + * This program is free software; you can redistribute it and/or modify
752 + * it under the terms of the GNU Lesser General Public License version 2.1 as
753 + * published by the Free Software Foundation.
755 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
758 +#ifndef __YAFFS_ALLOCATOR_H__
759 +#define __YAFFS_ALLOCATOR_H__
761 +#include "yaffs_guts.h"
763 +void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev);
764 +void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev);
766 +yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev);
767 +void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn);
769 +yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev);
770 +void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj);
774 +++ b/fs/yaffs2/yaffs_bitmap.c
777 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
779 + * Copyright (C) 2002-2010 Aleph One Ltd.
780 + * for Toby Churchill Ltd and Brightstar Engineering
782 + * Created by Charles Manning <charles@aleph1.co.uk>
784 + * This program is free software; you can redistribute it and/or modify
785 + * it under the terms of the GNU General Public License version 2 as
786 + * published by the Free Software Foundation.
789 +#include "yaffs_bitmap.h"
790 +#include "yaffs_trace.h"
792 + * Chunk bitmap manipulations
795 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_dev_t *dev, int blk)
797 + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
798 + T(YAFFS_TRACE_ERROR,
799 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
803 + return dev->chunk_bits +
804 + (dev->chunk_bit_stride * (blk - dev->internal_start_block));
807 +void yaffs_verify_chunk_bit_id(yaffs_dev_t *dev, int blk, int chunk)
809 + if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
810 + chunk < 0 || chunk >= dev->param.chunks_per_block) {
811 + T(YAFFS_TRACE_ERROR,
812 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
818 +void yaffs_clear_chunk_bits(yaffs_dev_t *dev, int blk)
820 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
822 + memset(blkBits, 0, dev->chunk_bit_stride);
825 +void yaffs_clear_chunk_bit(yaffs_dev_t *dev, int blk, int chunk)
827 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
829 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
831 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
834 +void yaffs_set_chunk_bit(yaffs_dev_t *dev, int blk, int chunk)
836 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
838 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
840 + blkBits[chunk / 8] |= (1 << (chunk & 7));
843 +int yaffs_check_chunk_bit(yaffs_dev_t *dev, int blk, int chunk)
845 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
846 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
848 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
851 +int yaffs_still_some_chunks(yaffs_dev_t *dev, int blk)
853 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
855 + for (i = 0; i < dev->chunk_bit_stride; i++) {
863 +int yaffs_count_chunk_bits(yaffs_dev_t *dev, int blk)
865 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
868 + for (i = 0; i < dev->chunk_bit_stride; i++) {
882 +++ b/fs/yaffs2/yaffs_bitmap.h
885 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
887 + * Copyright (C) 2002-2010 Aleph One Ltd.
888 + * for Toby Churchill Ltd and Brightstar Engineering
890 + * Created by Charles Manning <charles@aleph1.co.uk>
892 + * This program is free software; you can redistribute it and/or modify
893 + * it under the terms of the GNU General Public License version 2 as
894 + * published by the Free Software Foundation.
898 + * Chunk bitmap manipulations
901 +#ifndef __YAFFS_BITMAP_H__
902 +#define __YAFFS_BITMAP_H__
904 +#include "yaffs_guts.h"
906 +void yaffs_verify_chunk_bit_id(yaffs_dev_t *dev, int blk, int chunk);
907 +void yaffs_clear_chunk_bits(yaffs_dev_t *dev, int blk);
908 +void yaffs_clear_chunk_bit(yaffs_dev_t *dev, int blk, int chunk);
909 +void yaffs_set_chunk_bit(yaffs_dev_t *dev, int blk, int chunk);
910 +int yaffs_check_chunk_bit(yaffs_dev_t *dev, int blk, int chunk);
911 +int yaffs_still_some_chunks(yaffs_dev_t *dev, int blk);
912 +int yaffs_count_chunk_bits(yaffs_dev_t *dev, int blk);
915 --- a/fs/yaffs2/yaffs_checkptrw.c
916 +++ b/fs/yaffs2/yaffs_checkptrw.c
919 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
921 - * Copyright (C) 2002-2007 Aleph One Ltd.
922 + * Copyright (C) 2002-2010 Aleph One Ltd.
923 * for Toby Churchill Ltd and Brightstar Engineering
925 * Created by Charles Manning <charles@aleph1.co.uk>
927 * published by the Free Software Foundation.
930 -const char *yaffs_checkptrw_c_version =
931 - "$Id: yaffs_checkptrw.c,v 1.18 2009-03-06 17:20:49 wookey Exp $";
934 #include "yaffs_checkptrw.h"
935 #include "yaffs_getblockinfo.h"
937 -static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
938 +static int yaffs2_checkpt_space_ok(yaffs_dev_t *dev)
940 - int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
941 + int blocksAvailable = dev->n_erased_blocks - dev->param.n_reserved_blocks;
943 T(YAFFS_TRACE_CHECKPOINT,
944 (TSTR("checkpt blocks available = %d" TENDSTR),
945 @@ -30,53 +26,56 @@ static int yaffs_CheckpointSpaceOk(yaffs
949 -static int yaffs_CheckpointErase(yaffs_Device *dev)
950 +static int yaffs_checkpt_erase(yaffs_dev_t *dev)
954 - if (!dev->eraseBlockInNAND)
955 + if (!dev->param.erase_fn)
957 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
958 - dev->internalStartBlock, dev->internalEndBlock));
959 + dev->internal_start_block, dev->internal_end_block));
961 - for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
962 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
963 - if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
964 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
965 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, i);
966 + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
967 T(YAFFS_TRACE_CHECKPOINT, (TSTR("erasing checkpt block %d"TENDSTR), i));
968 - if (dev->eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
969 - bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
970 - dev->nErasedBlocks++;
971 - dev->nFreeChunks += dev->nChunksPerBlock;
975 + if (dev->param.erase_fn(dev, i - dev->block_offset /* realign */)) {
976 + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
977 + dev->n_erased_blocks++;
978 + dev->n_free_chunks += dev->param.chunks_per_block;
980 - dev->markNANDBlockBad(dev, i);
981 - bi->blockState = YAFFS_BLOCK_STATE_DEAD;
982 + dev->param.bad_block_fn(dev, i);
983 + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
988 - dev->blocksInCheckpoint = 0;
989 + dev->blocks_in_checkpt = 0;
995 -static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
996 +static void yaffs2_checkpt_find_erased_block(yaffs_dev_t *dev)
999 - int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
1000 + int blocksAvailable = dev->n_erased_blocks - dev->param.n_reserved_blocks;
1001 T(YAFFS_TRACE_CHECKPOINT,
1002 (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
1003 - dev->nErasedBlocks, dev->nReservedBlocks, blocksAvailable, dev->checkpointNextBlock));
1004 + dev->n_erased_blocks, dev->param.n_reserved_blocks, blocksAvailable, dev->checkpt_next_block));
1006 - if (dev->checkpointNextBlock >= 0 &&
1007 - dev->checkpointNextBlock <= dev->internalEndBlock &&
1008 + if (dev->checkpt_next_block >= 0 &&
1009 + dev->checkpt_next_block <= dev->internal_end_block &&
1010 blocksAvailable > 0) {
1012 - for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
1013 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
1014 - if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
1015 - dev->checkpointNextBlock = i + 1;
1016 - dev->checkpointCurrentBlock = i;
1017 + for (i = dev->checkpt_next_block; i <= dev->internal_end_block; i++) {
1018 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, i);
1019 + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1020 + dev->checkpt_next_block = i + 1;
1021 + dev->checkpt_cur_block = i;
1022 T(YAFFS_TRACE_CHECKPOINT, (TSTR("allocating checkpt block %d"TENDSTR), i));
1025 @@ -84,34 +83,34 @@ static void yaffs_CheckpointFindNextEras
1027 T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
1029 - dev->checkpointNextBlock = -1;
1030 - dev->checkpointCurrentBlock = -1;
1031 + dev->checkpt_next_block = -1;
1032 + dev->checkpt_cur_block = -1;
1035 -static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
1036 +static void yaffs2_checkpt_find_block(yaffs_dev_t *dev)
1039 - yaffs_ExtendedTags tags;
1040 + yaffs_ext_tags tags;
1042 T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
1043 - dev->blocksInCheckpoint, dev->checkpointNextBlock));
1044 + dev->blocks_in_checkpt, dev->checkpt_next_block));
1046 - if (dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
1047 - for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
1048 - int chunk = i * dev->nChunksPerBlock;
1049 - int realignedChunk = chunk - dev->chunkOffset;
1050 + if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
1051 + for (i = dev->checkpt_next_block; i <= dev->internal_end_block; i++) {
1052 + int chunk = i * dev->param.chunks_per_block;
1053 + int realignedChunk = chunk - dev->chunk_offset;
1055 - dev->readChunkWithTagsFromNAND(dev, realignedChunk,
1056 + dev->param.read_chunk_tags_fn(dev, realignedChunk,
1058 T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
1059 - i, tags.objectId, tags.sequenceNumber, tags.eccResult));
1060 + i, tags.obj_id, tags.seq_number, tags.ecc_result));
1062 - if (tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
1063 + if (tags.seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
1064 /* Right kind of block */
1065 - dev->checkpointNextBlock = tags.objectId;
1066 - dev->checkpointCurrentBlock = i;
1067 - dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
1068 - dev->blocksInCheckpoint++;
1069 + dev->checkpt_next_block = tags.obj_id;
1070 + dev->checkpt_cur_block = i;
1071 + dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
1072 + dev->blocks_in_checkpt++;
1073 T(YAFFS_TRACE_CHECKPOINT, (TSTR("found checkpt block %d"TENDSTR), i));
1076 @@ -119,122 +118,127 @@ static void yaffs_CheckpointFindNextChec
1078 T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
1080 - dev->checkpointNextBlock = -1;
1081 - dev->checkpointCurrentBlock = -1;
1082 + dev->checkpt_next_block = -1;
1083 + dev->checkpt_cur_block = -1;
1087 -int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
1088 +int yaffs2_checkpt_open(yaffs_dev_t *dev, int forWriting)
1092 + dev->checkpt_open_write = forWriting;
1094 /* Got the functions we need? */
1095 - if (!dev->writeChunkWithTagsToNAND ||
1096 - !dev->readChunkWithTagsFromNAND ||
1097 - !dev->eraseBlockInNAND ||
1098 - !dev->markNANDBlockBad)
1099 + if (!dev->param.write_chunk_tags_fn ||
1100 + !dev->param.read_chunk_tags_fn ||
1101 + !dev->param.erase_fn ||
1102 + !dev->param.bad_block_fn)
1105 - if (forWriting && !yaffs_CheckpointSpaceOk(dev))
1106 + if (forWriting && !yaffs2_checkpt_space_ok(dev))
1109 - if (!dev->checkpointBuffer)
1110 - dev->checkpointBuffer = YMALLOC_DMA(dev->totalBytesPerChunk);
1111 - if (!dev->checkpointBuffer)
1112 + if (!dev->checkpt_buffer)
1113 + dev->checkpt_buffer = YMALLOC_DMA(dev->param.total_bytes_per_chunk);
1114 + if (!dev->checkpt_buffer)
1118 - dev->checkpointPageSequence = 0;
1120 - dev->checkpointOpenForWrite = forWriting;
1122 - dev->checkpointByteCount = 0;
1123 - dev->checkpointSum = 0;
1124 - dev->checkpointXor = 0;
1125 - dev->checkpointCurrentBlock = -1;
1126 - dev->checkpointCurrentChunk = -1;
1127 - dev->checkpointNextBlock = dev->internalStartBlock;
1128 + dev->checkpt_page_seq = 0;
1129 + dev->checkpt_byte_count = 0;
1130 + dev->checkpt_sum = 0;
1131 + dev->checkpt_xor = 0;
1132 + dev->checkpt_cur_block = -1;
1133 + dev->checkpt_cur_chunk = -1;
1134 + dev->checkpt_next_block = dev->internal_start_block;
1136 /* Erase all the blocks in the checkpoint area */
1138 - memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
1139 - dev->checkpointByteOffset = 0;
1140 - return yaffs_CheckpointErase(dev);
1141 + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
1142 + dev->checkpt_byte_offs = 0;
1143 + return yaffs_checkpt_erase(dev);
1146 /* Set to a value that will kick off a read */
1147 - dev->checkpointByteOffset = dev->nDataBytesPerChunk;
1148 + dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
1149 /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
1150 * going to be way more than we need */
1151 - dev->blocksInCheckpoint = 0;
1152 - dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
1153 - dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
1154 - for (i = 0; i < dev->checkpointMaxBlocks; i++)
1155 - dev->checkpointBlockList[i] = -1;
1156 + dev->blocks_in_checkpt = 0;
1157 + dev->checkpt_max_blocks = (dev->internal_end_block - dev->internal_start_block)/16 + 2;
1158 + dev->checkpt_block_list = YMALLOC(sizeof(int) * dev->checkpt_max_blocks);
1159 + if(!dev->checkpt_block_list)
1162 + for (i = 0; i < dev->checkpt_max_blocks; i++)
1163 + dev->checkpt_block_list[i] = -1;
1169 -int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
1170 +int yaffs2_get_checkpt_sum(yaffs_dev_t *dev, __u32 *sum)
1173 - compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
1174 + compositeSum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xFF);
1175 *sum = compositeSum;
1179 -static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
1180 +static int yaffs2_checkpt_flush_buffer(yaffs_dev_t *dev)
1185 - yaffs_ExtendedTags tags;
1186 + yaffs_ext_tags tags;
1188 - if (dev->checkpointCurrentBlock < 0) {
1189 - yaffs_CheckpointFindNextErasedBlock(dev);
1190 - dev->checkpointCurrentChunk = 0;
1191 + if (dev->checkpt_cur_block < 0) {
1192 + yaffs2_checkpt_find_erased_block(dev);
1193 + dev->checkpt_cur_chunk = 0;
1196 - if (dev->checkpointCurrentBlock < 0)
1197 + if (dev->checkpt_cur_block < 0)
1200 - tags.chunkDeleted = 0;
1201 - tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
1202 - tags.chunkId = dev->checkpointPageSequence + 1;
1203 - tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
1204 - tags.byteCount = dev->nDataBytesPerChunk;
1205 - if (dev->checkpointCurrentChunk == 0) {
1206 + tags.is_deleted = 0;
1207 + tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
1208 + tags.chunk_id = dev->checkpt_page_seq + 1;
1209 + tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
1210 + tags.n_bytes = dev->data_bytes_per_chunk;
1211 + if (dev->checkpt_cur_chunk == 0) {
1212 /* First chunk we write for the block? Set block state to
1214 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
1215 - bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
1216 - dev->blocksInCheckpoint++;
1217 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, dev->checkpt_cur_block);
1218 + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1219 + dev->blocks_in_checkpt++;
1222 - chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
1223 + chunk = dev->checkpt_cur_block * dev->param.chunks_per_block + dev->checkpt_cur_chunk;
1226 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
1227 - chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk, tags.objectId, tags.chunkId));
1228 + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk, tags.obj_id, tags.chunk_id));
1230 - realignedChunk = chunk - dev->chunkOffset;
1231 + realignedChunk = chunk - dev->chunk_offset;
1233 - dev->writeChunkWithTagsToNAND(dev, realignedChunk,
1234 - dev->checkpointBuffer, &tags);
1235 - dev->checkpointByteOffset = 0;
1236 - dev->checkpointPageSequence++;
1237 - dev->checkpointCurrentChunk++;
1238 - if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock) {
1239 - dev->checkpointCurrentChunk = 0;
1240 - dev->checkpointCurrentBlock = -1;
1241 + dev->n_page_writes++;
1243 + dev->param.write_chunk_tags_fn(dev, realignedChunk,
1244 + dev->checkpt_buffer, &tags);
1245 + dev->checkpt_byte_offs = 0;
1246 + dev->checkpt_page_seq++;
1247 + dev->checkpt_cur_chunk++;
1248 + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
1249 + dev->checkpt_cur_chunk = 0;
1250 + dev->checkpt_cur_block = -1;
1252 - memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
1253 + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
1259 -int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
1260 +int yaffs2_checkpt_wr(yaffs_dev_t *dev, const void *data, int n_bytes)
1264 @@ -244,36 +248,36 @@ int yaffs_CheckpointWrite(yaffs_Device *
1268 - if (!dev->checkpointBuffer)
1269 + if (!dev->checkpt_buffer)
1272 - if (!dev->checkpointOpenForWrite)
1273 + if (!dev->checkpt_open_write)
1276 - while (i < nBytes && ok) {
1277 - dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
1278 - dev->checkpointSum += *dataBytes;
1279 - dev->checkpointXor ^= *dataBytes;
1280 + while (i < n_bytes && ok) {
1281 + dev->checkpt_buffer[dev->checkpt_byte_offs] = *dataBytes;
1282 + dev->checkpt_sum += *dataBytes;
1283 + dev->checkpt_xor ^= *dataBytes;
1285 - dev->checkpointByteOffset++;
1286 + dev->checkpt_byte_offs++;
1289 - dev->checkpointByteCount++;
1290 + dev->checkpt_byte_count++;
1293 - if (dev->checkpointByteOffset < 0 ||
1294 - dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
1295 - ok = yaffs_CheckpointFlushBuffer(dev);
1296 + if (dev->checkpt_byte_offs < 0 ||
1297 + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
1298 + ok = yaffs2_checkpt_flush_buffer(dev);
1304 -int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
1305 +int yaffs2_checkpt_rd(yaffs_dev_t *dev, void *data, int n_bytes)
1309 - yaffs_ExtendedTags tags;
1310 + yaffs_ext_tags tags;
1314 @@ -281,113 +285,116 @@ int yaffs_CheckpointRead(yaffs_Device *d
1316 __u8 *dataBytes = (__u8 *)data;
1318 - if (!dev->checkpointBuffer)
1319 + if (!dev->checkpt_buffer)
1322 - if (dev->checkpointOpenForWrite)
1323 + if (dev->checkpt_open_write)
1326 - while (i < nBytes && ok) {
1327 + while (i < n_bytes && ok) {
1330 - if (dev->checkpointByteOffset < 0 ||
1331 - dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
1332 + if (dev->checkpt_byte_offs < 0 ||
1333 + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
1335 - if (dev->checkpointCurrentBlock < 0) {
1336 - yaffs_CheckpointFindNextCheckpointBlock(dev);
1337 - dev->checkpointCurrentChunk = 0;
1338 + if (dev->checkpt_cur_block < 0) {
1339 + yaffs2_checkpt_find_block(dev);
1340 + dev->checkpt_cur_chunk = 0;
1343 - if (dev->checkpointCurrentBlock < 0)
1344 + if (dev->checkpt_cur_block < 0)
1347 - chunk = dev->checkpointCurrentBlock *
1348 - dev->nChunksPerBlock +
1349 - dev->checkpointCurrentChunk;
1351 - realignedChunk = chunk - dev->chunkOffset;
1352 + chunk = dev->checkpt_cur_block *
1353 + dev->param.chunks_per_block +
1354 + dev->checkpt_cur_chunk;
1356 + realignedChunk = chunk - dev->chunk_offset;
1358 + dev->n_page_reads++;
1360 /* read in the next chunk */
1361 /* printf("read checkpoint page %d\n",dev->checkpointPage); */
1362 - dev->readChunkWithTagsFromNAND(dev,
1363 + dev->param.read_chunk_tags_fn(dev,
1365 - dev->checkpointBuffer,
1366 + dev->checkpt_buffer,
1369 - if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
1370 - tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
1371 - tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
1372 + if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
1373 + tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
1374 + tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
1377 - dev->checkpointByteOffset = 0;
1378 - dev->checkpointPageSequence++;
1379 - dev->checkpointCurrentChunk++;
1380 + dev->checkpt_byte_offs = 0;
1381 + dev->checkpt_page_seq++;
1382 + dev->checkpt_cur_chunk++;
1384 - if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
1385 - dev->checkpointCurrentBlock = -1;
1386 + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block)
1387 + dev->checkpt_cur_block = -1;
1392 - *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
1393 - dev->checkpointSum += *dataBytes;
1394 - dev->checkpointXor ^= *dataBytes;
1395 - dev->checkpointByteOffset++;
1396 + *dataBytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
1397 + dev->checkpt_sum += *dataBytes;
1398 + dev->checkpt_xor ^= *dataBytes;
1399 + dev->checkpt_byte_offs++;
1402 - dev->checkpointByteCount++;
1403 + dev->checkpt_byte_count++;
1410 -int yaffs_CheckpointClose(yaffs_Device *dev)
1411 +int yaffs_checkpt_close(yaffs_dev_t *dev)
1414 - if (dev->checkpointOpenForWrite) {
1415 - if (dev->checkpointByteOffset != 0)
1416 - yaffs_CheckpointFlushBuffer(dev);
1418 + if (dev->checkpt_open_write) {
1419 + if (dev->checkpt_byte_offs != 0)
1420 + yaffs2_checkpt_flush_buffer(dev);
1421 + } else if(dev->checkpt_block_list){
1423 - for (i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++) {
1424 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointBlockList[i]);
1425 - if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
1426 - bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
1427 + for (i = 0; i < dev->blocks_in_checkpt && dev->checkpt_block_list[i] >= 0; i++) {
1428 + int blk = dev->checkpt_block_list[i];
1429 + yaffs_block_info_t *bi = NULL;
1430 + if( dev->internal_start_block <= blk && blk <= dev->internal_end_block)
1431 + bi = yaffs_get_block_info(dev, blk);
1432 + if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
1433 + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1435 /* Todo this looks odd... */
1438 - YFREE(dev->checkpointBlockList);
1439 - dev->checkpointBlockList = NULL;
1440 + YFREE(dev->checkpt_block_list);
1441 + dev->checkpt_block_list = NULL;
1444 - dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
1445 - dev->nErasedBlocks -= dev->blocksInCheckpoint;
1446 + dev->n_free_chunks -= dev->blocks_in_checkpt * dev->param.chunks_per_block;
1447 + dev->n_erased_blocks -= dev->blocks_in_checkpt;
1450 T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
1451 - dev->checkpointByteCount));
1452 + dev->checkpt_byte_count));
1454 - if (dev->checkpointBuffer) {
1455 + if (dev->checkpt_buffer) {
1456 /* free the buffer */
1457 - YFREE(dev->checkpointBuffer);
1458 - dev->checkpointBuffer = NULL;
1459 + YFREE(dev->checkpt_buffer);
1460 + dev->checkpt_buffer = NULL;
1466 -int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
1467 +int yaffs2_checkpt_invalidate_stream(yaffs_dev_t *dev)
1469 - /* Erase the first checksum block */
1471 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate"TENDSTR)));
1472 + /* Erase the checkpoint data */
1474 - if (!yaffs_CheckpointSpaceOk(dev))
1476 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate of %d blocks"TENDSTR),
1477 + dev->blocks_in_checkpt));
1479 - return yaffs_CheckpointErase(dev);
1480 + return yaffs_checkpt_erase(dev);
1484 --- a/fs/yaffs2/yaffs_checkptrw.h
1485 +++ b/fs/yaffs2/yaffs_checkptrw.h
1488 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1490 - * Copyright (C) 2002-2007 Aleph One Ltd.
1491 + * Copyright (C) 2002-2010 Aleph One Ltd.
1492 * for Toby Churchill Ltd and Brightstar Engineering
1494 * Created by Charles Manning <charles@aleph1.co.uk>
1497 #include "yaffs_guts.h"
1499 -int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
1500 +int yaffs2_checkpt_open(yaffs_dev_t *dev, int forWriting);
1502 -int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes);
1503 +int yaffs2_checkpt_wr(yaffs_dev_t *dev, const void *data, int n_bytes);
1505 -int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes);
1506 +int yaffs2_checkpt_rd(yaffs_dev_t *dev, void *data, int n_bytes);
1508 -int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
1509 +int yaffs2_get_checkpt_sum(yaffs_dev_t *dev, __u32 *sum);
1511 -int yaffs_CheckpointClose(yaffs_Device *dev);
1512 +int yaffs_checkpt_close(yaffs_dev_t *dev);
1514 -int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
1515 +int yaffs2_checkpt_invalidate_stream(yaffs_dev_t *dev);
1520 --- a/fs/yaffs2/yaffs_ecc.c
1521 +++ b/fs/yaffs2/yaffs_ecc.c
1524 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1526 - * Copyright (C) 2002-2007 Aleph One Ltd.
1527 + * Copyright (C) 2002-2010 Aleph One Ltd.
1528 * for Toby Churchill Ltd and Brightstar Engineering
1530 * Created by Charles Manning <charles@aleph1.co.uk>
1532 * this bytes influence on the line parity.
1535 -const char *yaffs_ecc_c_version =
1536 - "$Id: yaffs_ecc.c,v 1.11 2009-03-06 17:20:50 wookey Exp $";
1538 #include "yportenv.h"
1540 #include "yaffs_ecc.h"
1541 @@ -72,7 +69,7 @@ static const unsigned char column_parity
1543 /* Count the bits in an unsigned char or a U32 */
1545 -static int yaffs_CountBits(unsigned char x)
1546 +static int yaffs_count_bits(unsigned char x)
1550 @@ -83,7 +80,7 @@ static int yaffs_CountBits(unsigned char
1554 -static int yaffs_CountBits32(unsigned x)
1555 +static int yaffs_count_bits32(unsigned x)
1559 @@ -95,7 +92,7 @@ static int yaffs_CountBits32(unsigned x)
1562 /* Calculate the ECC for a 256-byte block of data */
1563 -void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1564 +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc)
1568 @@ -166,7 +163,7 @@ void yaffs_ECCCalculate(const unsigned c
1570 /* Correct the ECC on a 256 byte block of data */
1572 -int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1573 +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
1574 const unsigned char *test_ecc)
1576 unsigned char d0, d1, d2; /* deltas */
1577 @@ -226,9 +223,9 @@ int yaffs_ECCCorrect(unsigned char *data
1578 return 1; /* Corrected the error */
1581 - if ((yaffs_CountBits(d0) +
1582 - yaffs_CountBits(d1) +
1583 - yaffs_CountBits(d2)) == 1) {
1584 + if ((yaffs_count_bits(d0) +
1585 + yaffs_count_bits(d1) +
1586 + yaffs_count_bits(d2)) == 1) {
1587 /* Reccoverable error in ecc */
1589 read_ecc[0] = test_ecc[0];
1590 @@ -248,7 +245,7 @@ int yaffs_ECCCorrect(unsigned char *data
1592 * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1594 -void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1595 +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
1596 yaffs_ECCOther *eccOther)
1599 @@ -258,7 +255,7 @@ void yaffs_ECCCalculateOther(const unsig
1600 unsigned line_parity_prime = 0;
1603 - for (i = 0; i < nBytes; i++) {
1604 + for (i = 0; i < n_bytes; i++) {
1605 b = column_parity_table[*data++];
1608 @@ -275,7 +272,7 @@ void yaffs_ECCCalculateOther(const unsig
1609 eccOther->lineParityPrime = line_parity_prime;
1612 -int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1613 +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
1614 yaffs_ECCOther *read_ecc,
1615 const yaffs_ECCOther *test_ecc)
1617 @@ -304,7 +301,7 @@ int yaffs_ECCCorrectOther(unsigned char
1621 - if (lDelta >= nBytes)
1622 + if (lDelta >= n_bytes)
1625 data[lDelta] ^= (1 << bit);
1626 @@ -312,8 +309,8 @@ int yaffs_ECCCorrectOther(unsigned char
1627 return 1; /* corrected */
1630 - if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1631 - yaffs_CountBits(cDelta)) == 1) {
1632 + if ((yaffs_count_bits32(lDelta) + yaffs_count_bits32(lDeltaPrime) +
1633 + yaffs_count_bits(cDelta)) == 1) {
1634 /* Reccoverable error in ecc */
1636 *read_ecc = *test_ecc;
1637 --- a/fs/yaffs2/yaffs_ecc.h
1638 +++ b/fs/yaffs2/yaffs_ecc.h
1641 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1643 - * Copyright (C) 2002-2007 Aleph One Ltd.
1644 + * Copyright (C) 2002-2010 Aleph One Ltd.
1645 * for Toby Churchill Ltd and Brightstar Engineering
1647 * Created by Charles Manning <charles@aleph1.co.uk>
1648 @@ -32,13 +32,13 @@ typedef struct {
1649 unsigned lineParityPrime;
1652 -void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1653 -int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1654 +void yaffs_ecc_cacl(const unsigned char *data, unsigned char *ecc);
1655 +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
1656 const unsigned char *test_ecc);
1658 -void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1659 +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
1660 yaffs_ECCOther *ecc);
1661 -int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1662 +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
1663 yaffs_ECCOther *read_ecc,
1664 const yaffs_ECCOther *test_ecc);
1666 --- a/fs/yaffs2/yaffs_fs.c
1670 - * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1672 - * Copyright (C) 2002-2009 Aleph One Ltd.
1673 - * for Toby Churchill Ltd and Brightstar Engineering
1675 - * Created by Charles Manning <charles@aleph1.co.uk>
1676 - * Acknowledgements:
1677 - * Luc van OostenRyck for numerous patches.
1678 - * Nick Bane for numerous patches.
1679 - * Nick Bane for 2.5/2.6 integration.
1680 - * Andras Toth for mknod rdev issue.
1681 - * Michael Fischer for finding the problem with inode inconsistency.
1682 - * Some code bodily lifted from JFFS
1684 - * This program is free software; you can redistribute it and/or modify
1685 - * it under the terms of the GNU General Public License version 2 as
1686 - * published by the Free Software Foundation.
1691 - * This is the file system front-end to YAFFS that hooks it up to
1695 - * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1697 - * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1699 - * >> inode->u.generic_ip points to the associated yaffs_Object.
1702 -const char *yaffs_fs_c_version =
1703 - "$Id: yaffs_fs.c,v 1.79 2009-03-17 01:12:00 wookey Exp $";
1704 -extern const char *yaffs_guts_c_version;
1706 -#include <linux/version.h>
1707 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1708 -#include <linux/config.h>
1710 -#include <linux/kernel.h>
1711 -#include <linux/module.h>
1712 -#include <linux/slab.h>
1713 -#include <linux/init.h>
1714 -#include <linux/fs.h>
1715 -#include <linux/proc_fs.h>
1716 -#include <linux/smp_lock.h>
1717 -#include <linux/pagemap.h>
1718 -#include <linux/mtd/mtd.h>
1719 -#include <linux/interrupt.h>
1720 -#include <linux/string.h>
1721 -#include <linux/ctype.h>
1723 -#include "asm/div64.h"
1725 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1727 -#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1728 -#include <linux/statfs.h>
1729 -#define UnlockPage(p) unlock_page(p)
1730 -#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1732 -/* FIXME: use sb->s_id instead ? */
1733 -#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1737 -#include <linux/locks.h>
1738 -#define BDEVNAME_SIZE 0
1739 -#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1741 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
1742 -/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1748 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
1749 -#define YPROC_ROOT (&proc_root)
1751 -#define YPROC_ROOT NULL
1754 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1755 -#define WRITE_SIZE_STR "writesize"
1756 -#define WRITE_SIZE(mtd) ((mtd)->writesize)
1758 -#define WRITE_SIZE_STR "oobblock"
1759 -#define WRITE_SIZE(mtd) ((mtd)->oobblock)
1762 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
1763 -#define YAFFS_USE_WRITE_BEGIN_END 1
1765 -#define YAFFS_USE_WRITE_BEGIN_END 0
1768 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
1769 -static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
1771 - uint64_t result = partition_size;
1772 - do_div(result, block_size);
1773 - return (uint32_t)result;
1776 -#define YCALCBLOCKS(s, b) ((s)/(b))
1779 -#include <linux/uaccess.h>
1781 -#include "yportenv.h"
1782 -#include "yaffs_guts.h"
1784 -#include <linux/mtd/mtd.h>
1785 -#include "yaffs_mtdif.h"
1786 -#include "yaffs_mtdif1.h"
1787 -#include "yaffs_mtdif2.h"
1789 -unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1790 -unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1791 -unsigned int yaffs_auto_checkpoint = 1;
1793 -/* Module Parameters */
1794 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1795 -module_param(yaffs_traceMask, uint, 0644);
1796 -module_param(yaffs_wr_attempts, uint, 0644);
1797 -module_param(yaffs_auto_checkpoint, uint, 0644);
1799 -MODULE_PARM(yaffs_traceMask, "i");
1800 -MODULE_PARM(yaffs_wr_attempts, "i");
1801 -MODULE_PARM(yaffs_auto_checkpoint, "i");
1804 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
1805 -/* use iget and read_inode */
1806 -#define Y_IGET(sb, inum) iget((sb), (inum))
1807 -static void yaffs_read_inode(struct inode *inode);
1810 -/* Call local equivalent */
1811 -#define YAFFS_USE_OWN_IGET
1812 -#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
1814 -static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
1817 -/*#define T(x) printk x */
1819 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
1820 -#define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
1822 -#define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
1825 -#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1826 -#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1828 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1829 -#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1831 -#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1834 -static void yaffs_put_super(struct super_block *sb);
1836 -static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1838 -static ssize_t yaffs_hold_space(struct file *f);
1839 -static void yaffs_release_space(struct file *f);
1841 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1842 -static int yaffs_file_flush(struct file *file, fl_owner_t id);
1844 -static int yaffs_file_flush(struct file *file);
1847 -static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1850 -static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1852 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1853 -static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1854 - struct nameidata *n);
1855 -static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1856 - struct nameidata *n);
1858 -static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1859 -static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1861 -static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1862 - struct dentry *dentry);
1863 -static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1864 -static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1865 - const char *symname);
1866 -static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1868 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1869 -static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1872 -static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1875 -static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1876 - struct inode *new_dir, struct dentry *new_dentry);
1877 -static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1879 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1880 -static int yaffs_sync_fs(struct super_block *sb, int wait);
1881 -static void yaffs_write_super(struct super_block *sb);
1883 -static int yaffs_sync_fs(struct super_block *sb);
1884 -static int yaffs_write_super(struct super_block *sb);
1887 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1888 -static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1889 -#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1890 -static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1892 -static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1895 -#ifdef YAFFS_HAS_PUT_INODE
1896 -static void yaffs_put_inode(struct inode *inode);
1899 -static void yaffs_delete_inode(struct inode *);
1900 -static void yaffs_clear_inode(struct inode *);
1902 -static int yaffs_readpage(struct file *file, struct page *page);
1903 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1904 -static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1906 -static int yaffs_writepage(struct page *page);
1910 -#if (YAFFS_USE_WRITE_BEGIN_END != 0)
1911 -static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1912 - loff_t pos, unsigned len, unsigned flags,
1913 - struct page **pagep, void **fsdata);
1914 -static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1915 - loff_t pos, unsigned len, unsigned copied,
1916 - struct page *pg, void *fsdadata);
1918 -static int yaffs_prepare_write(struct file *f, struct page *pg,
1919 - unsigned offset, unsigned to);
1920 -static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1925 -static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
1927 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
1928 -static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1930 -static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1933 -static struct address_space_operations yaffs_file_address_operations = {
1934 - .readpage = yaffs_readpage,
1935 - .writepage = yaffs_writepage,
1936 -#if (YAFFS_USE_WRITE_BEGIN_END > 0)
1937 - .write_begin = yaffs_write_begin,
1938 - .write_end = yaffs_write_end,
1940 - .prepare_write = yaffs_prepare_write,
1941 - .commit_write = yaffs_commit_write,
1945 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
1946 -static const struct file_operations yaffs_file_operations = {
1947 - .read = do_sync_read,
1948 - .write = do_sync_write,
1949 - .aio_read = generic_file_aio_read,
1950 - .aio_write = generic_file_aio_write,
1951 - .mmap = generic_file_mmap,
1952 - .flush = yaffs_file_flush,
1953 - .fsync = yaffs_sync_object,
1954 - .splice_read = generic_file_splice_read,
1955 - .splice_write = generic_file_splice_write,
1956 - .llseek = generic_file_llseek,
1959 -#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
1961 -static const struct file_operations yaffs_file_operations = {
1962 - .read = do_sync_read,
1963 - .write = do_sync_write,
1964 - .aio_read = generic_file_aio_read,
1965 - .aio_write = generic_file_aio_write,
1966 - .mmap = generic_file_mmap,
1967 - .flush = yaffs_file_flush,
1968 - .fsync = yaffs_sync_object,
1969 - .sendfile = generic_file_sendfile,
1974 -static const struct file_operations yaffs_file_operations = {
1975 - .read = generic_file_read,
1976 - .write = generic_file_write,
1977 - .mmap = generic_file_mmap,
1978 - .flush = yaffs_file_flush,
1979 - .fsync = yaffs_sync_object,
1980 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1981 - .sendfile = generic_file_sendfile,
1986 -static const struct inode_operations yaffs_file_inode_operations = {
1987 - .setattr = yaffs_setattr,
1990 -static const struct inode_operations yaffs_symlink_inode_operations = {
1991 - .readlink = yaffs_readlink,
1992 - .follow_link = yaffs_follow_link,
1993 - .setattr = yaffs_setattr,
1996 -static const struct inode_operations yaffs_dir_inode_operations = {
1997 - .create = yaffs_create,
1998 - .lookup = yaffs_lookup,
1999 - .link = yaffs_link,
2000 - .unlink = yaffs_unlink,
2001 - .symlink = yaffs_symlink,
2002 - .mkdir = yaffs_mkdir,
2003 - .rmdir = yaffs_unlink,
2004 - .mknod = yaffs_mknod,
2005 - .rename = yaffs_rename,
2006 - .setattr = yaffs_setattr,
2009 -static const struct file_operations yaffs_dir_operations = {
2010 - .read = generic_read_dir,
2011 - .readdir = yaffs_readdir,
2012 - .fsync = yaffs_sync_object,
2015 -static const struct super_operations yaffs_super_ops = {
2016 - .statfs = yaffs_statfs,
2018 -#ifndef YAFFS_USE_OWN_IGET
2019 - .read_inode = yaffs_read_inode,
2021 -#ifdef YAFFS_HAS_PUT_INODE
2022 - .put_inode = yaffs_put_inode,
2024 - .put_super = yaffs_put_super,
2025 - .delete_inode = yaffs_delete_inode,
2026 - .clear_inode = yaffs_clear_inode,
2027 - .sync_fs = yaffs_sync_fs,
2028 - .write_super = yaffs_write_super,
2031 -static void yaffs_GrossLock(yaffs_Device *dev)
2033 - T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
2034 - down(&dev->grossLock);
2035 - T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
2038 -static void yaffs_GrossUnlock(yaffs_Device *dev)
2040 - T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
2041 - up(&dev->grossLock);
2044 -static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
2047 - unsigned char *alias;
2050 - yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2052 - yaffs_GrossLock(dev);
2054 - alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
2056 - yaffs_GrossUnlock(dev);
2061 - ret = vfs_readlink(dentry, buffer, buflen, alias);
2066 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2067 -static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
2069 -static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
2072 - unsigned char *alias;
2074 - yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2076 - yaffs_GrossLock(dev);
2078 - alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
2080 - yaffs_GrossUnlock(dev);
2087 - ret = vfs_follow_link(nd, alias);
2090 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2091 - return ERR_PTR(ret);
2097 -struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2098 - yaffs_Object *obj);
2101 - * Lookup is used to find objects in the fs
2103 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2105 -static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
2106 - struct nameidata *n)
2108 -static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
2111 - yaffs_Object *obj;
2112 - struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
2114 - yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
2116 - yaffs_GrossLock(dev);
2119 - ("yaffs_lookup for %d:%s\n",
2120 - yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
2122 - obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
2123 - dentry->d_name.name);
2125 - obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
2127 - /* Can't hold gross lock when calling yaffs_get_inode() */
2128 - yaffs_GrossUnlock(dev);
2132 - ("yaffs_lookup found %d\n", obj->objectId));
2134 - inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2138 - ("yaffs_loookup dentry \n"));
2139 -/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
2140 - * d_add even if NULL inode */
2142 - /*dget(dentry); // try to solve directory bug */
2143 - d_add(dentry, inode);
2145 - /* return dentry; */
2151 - T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
2155 -/* added NCB for 2.5/6 compatability - forces add even if inode is
2156 - * NULL which creates dentry hash */
2157 - d_add(dentry, inode);
2163 -#ifdef YAFFS_HAS_PUT_INODE
2165 -/* For now put inode is just for debugging
2166 - * Put inode is called when the inode **structure** is put.
2168 -static void yaffs_put_inode(struct inode *inode)
2171 - ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
2172 - atomic_read(&inode->i_count)));
2177 -/* clear is called to tell the fs to release any per-inode data it holds */
2178 -static void yaffs_clear_inode(struct inode *inode)
2180 - yaffs_Object *obj;
2181 - yaffs_Device *dev;
2183 - obj = yaffs_InodeToObject(inode);
2186 - ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
2187 - atomic_read(&inode->i_count),
2188 - obj ? "object exists" : "null object"));
2192 - yaffs_GrossLock(dev);
2194 - /* Clear the association between the inode and
2195 - * the yaffs_Object.
2197 - obj->myInode = NULL;
2198 - yaffs_InodeToObjectLV(inode) = NULL;
2200 - /* If the object freeing was deferred, then the real
2201 - * free happens now.
2202 - * This should fix the inode inconsistency problem.
2205 - yaffs_HandleDeferedFree(obj);
2207 - yaffs_GrossUnlock(dev);
2212 -/* delete is called when the link count is zero and the inode
2213 - * is put (ie. nobody wants to know about it anymore, time to
2214 - * delete the file).
2215 - * NB Must call clear_inode()
2217 -static void yaffs_delete_inode(struct inode *inode)
2219 - yaffs_Object *obj = yaffs_InodeToObject(inode);
2220 - yaffs_Device *dev;
2223 - ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
2224 - atomic_read(&inode->i_count),
2225 - obj ? "object exists" : "null object"));
2229 - yaffs_GrossLock(dev);
2230 - yaffs_DeleteObject(obj);
2231 - yaffs_GrossUnlock(dev);
2233 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
2234 - truncate_inode_pages(&inode->i_data, 0);
2236 - clear_inode(inode);
2239 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2240 -static int yaffs_file_flush(struct file *file, fl_owner_t id)
2242 -static int yaffs_file_flush(struct file *file)
2245 - yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
2247 - yaffs_Device *dev = obj->myDev;
2250 - ("yaffs_file_flush object %d (%s)\n", obj->objectId,
2251 - obj->dirty ? "dirty" : "clean"));
2253 - yaffs_GrossLock(dev);
2255 - yaffs_FlushFile(obj, 1);
2257 - yaffs_GrossUnlock(dev);
2262 -static int yaffs_readpage_nolock(struct file *f, struct page *pg)
2264 - /* Lifted from jffs2 */
2266 - yaffs_Object *obj;
2267 - unsigned char *pg_buf;
2270 - yaffs_Device *dev;
2272 - T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
2273 - (unsigned)(pg->index << PAGE_CACHE_SHIFT),
2274 - (unsigned)PAGE_CACHE_SIZE));
2276 - obj = yaffs_DentryToObject(f->f_dentry);
2280 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2281 - BUG_ON(!PageLocked(pg));
2283 - if (!PageLocked(pg))
2287 - pg_buf = kmap(pg);
2288 - /* FIXME: Can kmap fail? */
2290 - yaffs_GrossLock(dev);
2292 - ret = yaffs_ReadDataFromFile(obj, pg_buf,
2293 - pg->index << PAGE_CACHE_SHIFT,
2296 - yaffs_GrossUnlock(dev);
2302 - ClearPageUptodate(pg);
2305 - SetPageUptodate(pg);
2306 - ClearPageError(pg);
2309 - flush_dcache_page(pg);
2312 - T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
2316 -static int yaffs_readpage_unlock(struct file *f, struct page *pg)
2318 - int ret = yaffs_readpage_nolock(f, pg);
2323 -static int yaffs_readpage(struct file *f, struct page *pg)
2325 - return yaffs_readpage_unlock(f, pg);
2328 -/* writepage inspired by/stolen from smbfs */
2330 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2331 -static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
2333 -static int yaffs_writepage(struct page *page)
2336 - struct address_space *mapping = page->mapping;
2337 - loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
2338 - struct inode *inode;
2339 - unsigned long end_index;
2341 - yaffs_Object *obj;
2347 - inode = mapping->host;
2351 - if (offset > inode->i_size) {
2353 - ("yaffs_writepage at %08x, inode size = %08x!!!\n",
2354 - (unsigned)(page->index << PAGE_CACHE_SHIFT),
2355 - (unsigned)inode->i_size));
2357 - (" -> don't care!!\n"));
2358 - unlock_page(page);
2362 - end_index = inode->i_size >> PAGE_CACHE_SHIFT;
2365 - if (page->index < end_index)
2366 - nBytes = PAGE_CACHE_SIZE;
2368 - nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
2372 - buffer = kmap(page);
2374 - obj = yaffs_InodeToObject(inode);
2375 - yaffs_GrossLock(obj->myDev);
2378 - ("yaffs_writepage at %08x, size %08x\n",
2379 - (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2381 - ("writepag0: obj = %05x, ino = %05x\n",
2382 - (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2384 - nWritten = yaffs_WriteDataToFile(obj, buffer,
2385 - page->index << PAGE_CACHE_SHIFT, nBytes, 0);
2388 - ("writepag1: obj = %05x, ino = %05x\n",
2389 - (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2391 - yaffs_GrossUnlock(obj->myDev);
2394 - SetPageUptodate(page);
2398 - return (nWritten == nBytes) ? 0 : -ENOSPC;
2402 -#if (YAFFS_USE_WRITE_BEGIN_END > 0)
2403 -static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
2404 - loff_t pos, unsigned len, unsigned flags,
2405 - struct page **pagep, void **fsdata)
2407 - struct page *pg = NULL;
2408 - pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2409 - uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
2410 - uint32_t to = offset + len;
2413 - int space_held = 0;
2415 - T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
2417 -#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28)
2418 - pg = grab_cache_page_write_begin(mapping, index, flags);
2420 - pg = __grab_cache_page(mapping, index);
2428 - /* Get fs space */
2429 - space_held = yaffs_hold_space(filp);
2431 - if (!space_held) {
2436 - /* Update page if required */
2438 - if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2439 - ret = yaffs_readpage_nolock(filp, pg);
2444 - /* Happy path return */
2445 - T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
2450 - T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
2452 - yaffs_release_space(filp);
2455 - page_cache_release(pg);
2462 -static int yaffs_prepare_write(struct file *f, struct page *pg,
2463 - unsigned offset, unsigned to)
2465 - T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
2467 - if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2468 - return yaffs_readpage_nolock(f, pg);
2473 -#if (YAFFS_USE_WRITE_BEGIN_END > 0)
2474 -static int yaffs_write_end(struct file *filp, struct address_space *mapping,
2475 - loff_t pos, unsigned len, unsigned copied,
2476 - struct page *pg, void *fsdadata)
2480 - uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
2483 - addr = kva + offset_into_page;
2486 - ("yaffs_write_end addr %x pos %x nBytes %d\n",
2488 - (int)pos, copied));
2490 - ret = yaffs_file_write(filp, addr, copied, &pos);
2492 - if (ret != copied) {
2494 - ("yaffs_write_end not same size ret %d copied %d\n",
2497 - ClearPageUptodate(pg);
2499 - SetPageUptodate(pg);
2504 - yaffs_release_space(filp);
2506 - page_cache_release(pg);
2511 -static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2516 - loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2517 - int nBytes = to - offset;
2520 - unsigned spos = pos;
2524 - addr = kva + offset;
2526 - saddr = (unsigned) addr;
2529 - ("yaffs_commit_write addr %x pos %x nBytes %d\n",
2530 - saddr, spos, nBytes));
2532 - nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2534 - if (nWritten != nBytes) {
2536 - ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
2537 - nWritten, nBytes));
2539 - ClearPageUptodate(pg);
2541 - SetPageUptodate(pg);
2547 - ("yaffs_commit_write returning %d\n",
2548 - nWritten == nBytes ? 0 : nWritten));
2550 - return nWritten == nBytes ? 0 : nWritten;
2555 -static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
2557 - if (inode && obj) {
2560 - /* Check mode against the variant type and attempt to repair if broken. */
2561 - __u32 mode = obj->yst_mode;
2562 - switch (obj->variantType) {
2563 - case YAFFS_OBJECT_TYPE_FILE:
2564 - if (!S_ISREG(mode)) {
2565 - obj->yst_mode &= ~S_IFMT;
2566 - obj->yst_mode |= S_IFREG;
2570 - case YAFFS_OBJECT_TYPE_SYMLINK:
2571 - if (!S_ISLNK(mode)) {
2572 - obj->yst_mode &= ~S_IFMT;
2573 - obj->yst_mode |= S_IFLNK;
2577 - case YAFFS_OBJECT_TYPE_DIRECTORY:
2578 - if (!S_ISDIR(mode)) {
2579 - obj->yst_mode &= ~S_IFMT;
2580 - obj->yst_mode |= S_IFDIR;
2584 - case YAFFS_OBJECT_TYPE_UNKNOWN:
2585 - case YAFFS_OBJECT_TYPE_HARDLINK:
2586 - case YAFFS_OBJECT_TYPE_SPECIAL:
2592 - inode->i_flags |= S_NOATIME;
2594 - inode->i_ino = obj->objectId;
2595 - inode->i_mode = obj->yst_mode;
2596 - inode->i_uid = obj->yst_uid;
2597 - inode->i_gid = obj->yst_gid;
2598 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2599 - inode->i_blksize = inode->i_sb->s_blocksize;
2601 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2603 - inode->i_rdev = old_decode_dev(obj->yst_rdev);
2604 - inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2605 - inode->i_atime.tv_nsec = 0;
2606 - inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2607 - inode->i_mtime.tv_nsec = 0;
2608 - inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2609 - inode->i_ctime.tv_nsec = 0;
2611 - inode->i_rdev = obj->yst_rdev;
2612 - inode->i_atime = obj->yst_atime;
2613 - inode->i_mtime = obj->yst_mtime;
2614 - inode->i_ctime = obj->yst_ctime;
2616 - inode->i_size = yaffs_GetObjectFileLength(obj);
2617 - inode->i_blocks = (inode->i_size + 511) >> 9;
2619 - inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2622 - ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2623 - inode->i_mode, inode->i_uid, inode->i_gid,
2624 - (int)inode->i_size, atomic_read(&inode->i_count)));
2626 - switch (obj->yst_mode & S_IFMT) {
2627 - default: /* fifo, device or socket */
2628 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2629 - init_special_inode(inode, obj->yst_mode,
2630 - old_decode_dev(obj->yst_rdev));
2632 - init_special_inode(inode, obj->yst_mode,
2633 - (dev_t) (obj->yst_rdev));
2636 - case S_IFREG: /* file */
2637 - inode->i_op = &yaffs_file_inode_operations;
2638 - inode->i_fop = &yaffs_file_operations;
2639 - inode->i_mapping->a_ops =
2640 - &yaffs_file_address_operations;
2642 - case S_IFDIR: /* directory */
2643 - inode->i_op = &yaffs_dir_inode_operations;
2644 - inode->i_fop = &yaffs_dir_operations;
2646 - case S_IFLNK: /* symlink */
2647 - inode->i_op = &yaffs_symlink_inode_operations;
2651 - yaffs_InodeToObjectLV(inode) = obj;
2653 - obj->myInode = inode;
2657 - ("yaffs_FileInode invalid parameters\n"));
2662 -struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2663 - yaffs_Object *obj)
2665 - struct inode *inode;
2669 - ("yaffs_get_inode for NULL super_block!!\n"));
2676 - ("yaffs_get_inode for NULL object!!\n"));
2682 - ("yaffs_get_inode for object %d\n", obj->objectId));
2684 - inode = Y_IGET(sb, obj->objectId);
2685 - if (IS_ERR(inode))
2688 - /* NB Side effect: iget calls back to yaffs_read_inode(). */
2689 - /* iget also increments the inode's i_count */
2690 - /* NB You can't be holding grossLock or deadlock will happen! */
2695 -static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2698 - yaffs_Object *obj;
2699 - int nWritten, ipos;
2700 - struct inode *inode;
2701 - yaffs_Device *dev;
2703 - obj = yaffs_DentryToObject(f->f_dentry);
2707 - yaffs_GrossLock(dev);
2709 - inode = f->f_dentry->d_inode;
2711 - if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
2712 - ipos = inode->i_size;
2718 - ("yaffs_file_write: hey obj is null!\n"));
2721 - ("yaffs_file_write about to write writing %zu bytes"
2722 - "to object %d at %d\n",
2723 - n, obj->objectId, ipos));
2725 - nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2728 - ("yaffs_file_write writing %zu bytes, %d written at %d\n",
2729 - n, nWritten, ipos));
2731 - if (nWritten > 0) {
2734 - if (ipos > inode->i_size) {
2735 - inode->i_size = ipos;
2736 - inode->i_blocks = (ipos + 511) >> 9;
2739 - ("yaffs_file_write size updated to %d bytes, "
2741 - ipos, (int)(inode->i_blocks)));
2745 - yaffs_GrossUnlock(dev);
2746 - return nWritten == 0 ? -ENOSPC : nWritten;
2749 -/* Space holding and freeing is done to ensure we have space available for write_begin/end */
2750 -/* For now we just assume few parallel writes and check against a small number. */
2751 -/* Todo: need to do this with a counter to handle parallel reads better */
2753 -static ssize_t yaffs_hold_space(struct file *f)
2755 - yaffs_Object *obj;
2756 - yaffs_Device *dev;
2761 - obj = yaffs_DentryToObject(f->f_dentry);
2765 - yaffs_GrossLock(dev);
2767 - nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
2769 - yaffs_GrossUnlock(dev);
2771 - return (nFreeChunks > 20) ? 1 : 0;
2774 -static void yaffs_release_space(struct file *f)
2776 - yaffs_Object *obj;
2777 - yaffs_Device *dev;
2780 - obj = yaffs_DentryToObject(f->f_dentry);
2784 - yaffs_GrossLock(dev);
2787 - yaffs_GrossUnlock(dev);
2790 -static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2792 - yaffs_Object *obj;
2793 - yaffs_Device *dev;
2794 - struct inode *inode = f->f_dentry->d_inode;
2795 - unsigned long offset, curoffs;
2796 - struct ylist_head *i;
2799 - char name[YAFFS_MAX_NAME_LENGTH + 1];
2801 - obj = yaffs_DentryToObject(f->f_dentry);
2804 - yaffs_GrossLock(dev);
2806 - offset = f->f_pos;
2808 - T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2810 - if (offset == 0) {
2812 - ("yaffs_readdir: entry . ino %d \n",
2813 - (int)inode->i_ino));
2814 - if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
2819 - if (offset == 1) {
2821 - ("yaffs_readdir: entry .. ino %d \n",
2822 - (int)f->f_dentry->d_parent->d_inode->i_ino));
2823 - if (filldir(dirent, "..", 2, offset,
2824 - f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
2832 - /* If the directory has changed since the open or last call to
2833 - readdir, rewind to after the 2 canned entries. */
2835 - if (f->f_version != inode->i_version) {
2837 - f->f_pos = offset;
2838 - f->f_version = inode->i_version;
2841 - ylist_for_each(i, &obj->variant.directoryVariant.children) {
2843 - if (curoffs >= offset) {
2844 - l = ylist_entry(i, yaffs_Object, siblings);
2846 - yaffs_GetObjectName(l, name,
2847 - YAFFS_MAX_NAME_LENGTH + 1);
2849 - ("yaffs_readdir: %s inode %d\n", name,
2850 - yaffs_GetObjectInode(l)));
2852 - if (filldir(dirent,
2856 - yaffs_GetObjectInode(l),
2857 - yaffs_GetObjectType(l)) < 0)
2867 - yaffs_GrossUnlock(dev);
2873 - * File creation. Allocate an inode, and we're done..
2876 -#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2879 -#define YCRED(x) (x->cred)
2882 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2883 -static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2886 -static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2890 - struct inode *inode;
2892 - yaffs_Object *obj = NULL;
2893 - yaffs_Device *dev;
2895 - yaffs_Object *parent = yaffs_InodeToObject(dir);
2897 - int error = -ENOSPC;
2898 - uid_t uid = YCRED(current)->fsuid;
2899 - gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
2901 - if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2906 - ("yaffs_mknod: parent object %d type %d\n",
2907 - parent->objectId, parent->variantType));
2910 - ("yaffs_mknod: could not get parent object\n"));
2914 - T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2915 - "mode %x dev %x\n",
2916 - dentry->d_name.name, mode, rdev));
2918 - dev = parent->myDev;
2920 - yaffs_GrossLock(dev);
2922 - switch (mode & S_IFMT) {
2924 - /* Special (socket, fifo, device...) */
2925 - T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
2926 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2927 - obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2928 - gid, old_encode_dev(rdev));
2930 - obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2934 - case S_IFREG: /* file */
2935 - T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
2936 - obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2939 - case S_IFDIR: /* directory */
2941 - ("yaffs_mknod: making directory\n"));
2942 - obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2945 - case S_IFLNK: /* symlink */
2946 - T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
2947 - obj = NULL; /* Do we ever get here? */
2951 - /* Can not call yaffs_get_inode() with gross lock held */
2952 - yaffs_GrossUnlock(dev);
2955 - inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2956 - d_instantiate(dentry, inode);
2958 - ("yaffs_mknod created object %d count = %d\n",
2959 - obj->objectId, atomic_read(&inode->i_count)));
2963 - ("yaffs_mknod failed making object\n"));
2970 -static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2973 - T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
2974 - retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2978 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2979 -static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2980 - struct nameidata *n)
2982 -static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2985 - T(YAFFS_TRACE_OS, ("yaffs_create\n"));
2986 - return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2989 -static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2993 - yaffs_Device *dev;
2996 - ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2997 - dentry->d_name.name));
2999 - dev = yaffs_InodeToObject(dir)->myDev;
3001 - yaffs_GrossLock(dev);
3003 - retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
3005 - if (retVal == YAFFS_OK) {
3006 - dentry->d_inode->i_nlink--;
3008 - yaffs_GrossUnlock(dev);
3009 - mark_inode_dirty(dentry->d_inode);
3012 - yaffs_GrossUnlock(dev);
3013 - return -ENOTEMPTY;
3017 - * Create a link...
3019 -static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
3020 - struct dentry *dentry)
3022 - struct inode *inode = old_dentry->d_inode;
3023 - yaffs_Object *obj = NULL;
3024 - yaffs_Object *link = NULL;
3025 - yaffs_Device *dev;
3027 - T(YAFFS_TRACE_OS, ("yaffs_link\n"));
3029 - obj = yaffs_InodeToObject(inode);
3032 - yaffs_GrossLock(dev);
3034 - if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
3035 - link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
3039 - old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
3040 - d_instantiate(dentry, old_dentry->d_inode);
3041 - atomic_inc(&old_dentry->d_inode->i_count);
3043 - ("yaffs_link link count %d i_count %d\n",
3044 - old_dentry->d_inode->i_nlink,
3045 - atomic_read(&old_dentry->d_inode->i_count)));
3048 - yaffs_GrossUnlock(dev);
3056 -static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
3057 - const char *symname)
3059 - yaffs_Object *obj;
3060 - yaffs_Device *dev;
3061 - uid_t uid = YCRED(current)->fsuid;
3062 - gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
3064 - T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
3066 - dev = yaffs_InodeToObject(dir)->myDev;
3067 - yaffs_GrossLock(dev);
3068 - obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
3069 - S_IFLNK | S_IRWXUGO, uid, gid, symname);
3070 - yaffs_GrossUnlock(dev);
3073 - struct inode *inode;
3075 - inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
3076 - d_instantiate(dentry, inode);
3077 - T(YAFFS_TRACE_OS, ("symlink created OK\n"));
3080 - T(YAFFS_TRACE_OS, ("symlink not created\n"));
3086 -static int yaffs_sync_object(struct file *file, struct dentry *dentry,
3090 - yaffs_Object *obj;
3091 - yaffs_Device *dev;
3093 - obj = yaffs_DentryToObject(dentry);
3097 - T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
3098 - yaffs_GrossLock(dev);
3099 - yaffs_FlushFile(obj, 1);
3100 - yaffs_GrossUnlock(dev);
3105 - * The VFS layer already does all the dentry stuff for rename.
3107 - * NB: POSIX says you can rename an object over an old object of the same name
3109 -static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
3110 - struct inode *new_dir, struct dentry *new_dentry)
3112 - yaffs_Device *dev;
3113 - int retVal = YAFFS_FAIL;
3114 - yaffs_Object *target;
3116 - T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
3117 - dev = yaffs_InodeToObject(old_dir)->myDev;
3119 - yaffs_GrossLock(dev);
3121 - /* Check if the target is an existing directory that is not empty. */
3122 - target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
3123 - new_dentry->d_name.name);
3127 - if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
3128 - !ylist_empty(&target->variant.directoryVariant.children)) {
3130 - T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
3132 - retVal = YAFFS_FAIL;
3134 - /* Now does unlinking internally using shadowing mechanism */
3135 - T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
3137 - retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
3138 - old_dentry->d_name.name,
3139 - yaffs_InodeToObject(new_dir),
3140 - new_dentry->d_name.name);
3142 - yaffs_GrossUnlock(dev);
3144 - if (retVal == YAFFS_OK) {
3146 - new_dentry->d_inode->i_nlink--;
3147 - mark_inode_dirty(new_dentry->d_inode);
3152 - return -ENOTEMPTY;
3156 -static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
3158 - struct inode *inode = dentry->d_inode;
3160 - yaffs_Device *dev;
3163 - ("yaffs_setattr of object %d\n",
3164 - yaffs_InodeToObject(inode)->objectId));
3166 - error = inode_change_ok(inode, attr);
3168 - dev = yaffs_InodeToObject(inode)->myDev;
3169 - yaffs_GrossLock(dev);
3170 - if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
3176 - yaffs_GrossUnlock(dev);
3178 - error = inode_setattr(inode, attr);
3183 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3184 -static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
3186 - yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
3187 - struct super_block *sb = dentry->d_sb;
3188 -#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3189 -static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
3191 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3193 -static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
3195 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3198 - T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
3200 - yaffs_GrossLock(dev);
3202 - buf->f_type = YAFFS_MAGIC;
3203 - buf->f_bsize = sb->s_blocksize;
3204 - buf->f_namelen = 255;
3206 - if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
3207 - /* Do this if chunk size is not a power of 2 */
3209 - uint64_t bytesInDev;
3210 - uint64_t bytesFree;
3212 - bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
3213 - ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
3215 - do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
3216 - buf->f_blocks = bytesInDev;
3218 - bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
3219 - ((uint64_t)(dev->nDataBytesPerChunk));
3221 - do_div(bytesFree, sb->s_blocksize);
3223 - buf->f_bfree = bytesFree;
3225 - } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
3228 - (dev->endBlock - dev->startBlock + 1) *
3229 - dev->nChunksPerBlock /
3230 - (sb->s_blocksize / dev->nDataBytesPerChunk);
3232 - yaffs_GetNumberOfFreeChunks(dev) /
3233 - (sb->s_blocksize / dev->nDataBytesPerChunk);
3236 - (dev->endBlock - dev->startBlock + 1) *
3237 - dev->nChunksPerBlock *
3238 - (dev->nDataBytesPerChunk / sb->s_blocksize);
3241 - yaffs_GetNumberOfFreeChunks(dev) *
3242 - (dev->nDataBytesPerChunk / sb->s_blocksize);
3247 - buf->f_bavail = buf->f_bfree;
3249 - yaffs_GrossUnlock(dev);
3254 -static int yaffs_do_sync_fs(struct super_block *sb)
3257 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3258 - T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
3261 - yaffs_GrossLock(dev);
3264 - yaffs_FlushEntireDeviceCache(dev);
3265 - yaffs_CheckpointSave(dev);
3268 - yaffs_GrossUnlock(dev);
3276 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3277 -static void yaffs_write_super(struct super_block *sb)
3279 -static int yaffs_write_super(struct super_block *sb)
3283 - T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
3284 - if (yaffs_auto_checkpoint >= 2)
3285 - yaffs_do_sync_fs(sb);
3286 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
3292 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3293 -static int yaffs_sync_fs(struct super_block *sb, int wait)
3295 -static int yaffs_sync_fs(struct super_block *sb)
3298 - T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
3300 - if (yaffs_auto_checkpoint >= 1)
3301 - yaffs_do_sync_fs(sb);
3306 -#ifdef YAFFS_USE_OWN_IGET
3308 -static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
3310 - struct inode *inode;
3311 - yaffs_Object *obj;
3312 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3315 - ("yaffs_iget for %lu\n", ino));
3317 - inode = iget_locked(sb, ino);
3319 - return ERR_PTR(-ENOMEM);
3320 - if (!(inode->i_state & I_NEW))
3323 - /* NB This is called as a side effect of other functions, but
3324 - * we had to release the lock to prevent deadlocks, so
3325 - * need to lock again.
3328 - yaffs_GrossLock(dev);
3330 - obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
3332 - yaffs_FillInodeFromObject(inode, obj);
3334 - yaffs_GrossUnlock(dev);
3336 - unlock_new_inode(inode);
3342 -static void yaffs_read_inode(struct inode *inode)
3344 - /* NB This is called as a side effect of other functions, but
3345 - * we had to release the lock to prevent deadlocks, so
3346 - * need to lock again.
3349 - yaffs_Object *obj;
3350 - yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
3353 - ("yaffs_read_inode for %d\n", (int)inode->i_ino));
3355 - yaffs_GrossLock(dev);
3357 - obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
3359 - yaffs_FillInodeFromObject(inode, obj);
3361 - yaffs_GrossUnlock(dev);
3366 -static YLIST_HEAD(yaffs_dev_list);
3368 -#if 0 /* not used */
3369 -static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
3371 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3373 - if (*flags & MS_RDONLY) {
3374 - struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
3377 - ("yaffs_remount_fs: %s: RO\n", dev->name));
3379 - yaffs_GrossLock(dev);
3381 - yaffs_FlushEntireDeviceCache(dev);
3383 - yaffs_CheckpointSave(dev);
3388 - yaffs_GrossUnlock(dev);
3391 - ("yaffs_remount_fs: %s: RW\n", dev->name));
3398 -static void yaffs_put_super(struct super_block *sb)
3400 - yaffs_Device *dev = yaffs_SuperToDevice(sb);
3402 - T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
3404 - yaffs_GrossLock(dev);
3406 - yaffs_FlushEntireDeviceCache(dev);
3408 - yaffs_CheckpointSave(dev);
3410 - if (dev->putSuperFunc)
3411 - dev->putSuperFunc(sb);
3413 - yaffs_Deinitialise(dev);
3415 - yaffs_GrossUnlock(dev);
3417 - /* we assume this is protected by lock_kernel() in mount/umount */
3418 - ylist_del(&dev->devList);
3420 - if (dev->spareBuffer) {
3421 - YFREE(dev->spareBuffer);
3422 - dev->spareBuffer = NULL;
3429 -static void yaffs_MTDPutSuper(struct super_block *sb)
3431 - struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
3436 - put_mtd_device(mtd);
3440 -static void yaffs_MarkSuperBlockDirty(void *vsb)
3442 - struct super_block *sb = (struct super_block *)vsb;
3444 - T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
3451 - int skip_checkpoint_read;
3452 - int skip_checkpoint_write;
3456 -#define MAX_OPT_LEN 20
3457 -static int yaffs_parse_options(yaffs_options *options, const char *options_str)
3459 - char cur_opt[MAX_OPT_LEN + 1];
3463 - /* Parse through the options which is a comma seperated list */
3465 - while (options_str && *options_str && !error) {
3466 - memset(cur_opt, 0, MAX_OPT_LEN + 1);
3469 - while (*options_str && *options_str != ',') {
3470 - if (p < MAX_OPT_LEN) {
3471 - cur_opt[p] = *options_str;
3477 - if (!strcmp(cur_opt, "inband-tags"))
3478 - options->inband_tags = 1;
3479 - else if (!strcmp(cur_opt, "no-cache"))
3480 - options->no_cache = 1;
3481 - else if (!strcmp(cur_opt, "no-checkpoint-read"))
3482 - options->skip_checkpoint_read = 1;
3483 - else if (!strcmp(cur_opt, "no-checkpoint-write"))
3484 - options->skip_checkpoint_write = 1;
3485 - else if (!strcmp(cur_opt, "no-checkpoint")) {
3486 - options->skip_checkpoint_read = 1;
3487 - options->skip_checkpoint_write = 1;
3489 - printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
3498 -static struct super_block *yaffs_internal_read_super(int yaffsVersion,
3499 - struct super_block *sb,
3500 - void *data, int silent)
3503 - struct inode *inode = NULL;
3504 - struct dentry *root;
3505 - yaffs_Device *dev = 0;
3506 - char devname_buf[BDEVNAME_SIZE + 1];
3507 - struct mtd_info *mtd;
3509 - char *data_str = (char *)data;
3511 - yaffs_options options;
3513 - sb->s_magic = YAFFS_MAGIC;
3514 - sb->s_op = &yaffs_super_ops;
3515 - sb->s_flags |= MS_NOATIME;
3518 - printk(KERN_INFO "yaffs: sb is NULL\n");
3519 - else if (!sb->s_dev)
3520 - printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
3521 - else if (!yaffs_devname(sb, devname_buf))
3522 - printk(KERN_INFO "yaffs: devname is NULL\n");
3524 - printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
3526 - yaffs_devname(sb, devname_buf));
3531 - printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
3533 - memset(&options, 0, sizeof(options));
3535 - if (yaffs_parse_options(&options, data_str)) {
3536 - /* Option parsing failed */
3541 - sb->s_blocksize = PAGE_CACHE_SIZE;
3542 - sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3543 - T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
3545 - ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
3547 -#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
3549 - ("yaffs: Write verification disabled. All guarantees "
3550 - "null and void\n"));
3553 - T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3555 - MAJOR(sb->s_dev), MINOR(sb->s_dev),
3556 - yaffs_devname(sb, devname_buf)));
3558 - /* Check it's an mtd device..... */
3559 - if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
3560 - return NULL; /* This isn't an mtd device */
3562 - /* Get the device */
3563 - mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3565 - T(YAFFS_TRACE_ALWAYS,
3566 - ("yaffs: MTD device #%u doesn't appear to exist\n",
3567 - MINOR(sb->s_dev)));
3570 - /* Check it's NAND */
3571 - if (mtd->type != MTD_NANDFLASH) {
3572 - T(YAFFS_TRACE_ALWAYS,
3573 - ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3577 - T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3578 - T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3579 - T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3580 - T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3581 - T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3582 - T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3583 - T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3584 - T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3585 - T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3586 - T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3587 -#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
3588 - T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
3590 - T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
3593 -#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3595 - if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
3596 - T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
3600 - /* Added NCB 26/5/2006 for completeness */
3601 - if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
3602 - T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
3608 - if (yaffsVersion == 2) {
3609 - /* Check for version 2 style functions */
3610 - if (!mtd->erase ||
3611 - !mtd->block_isbad ||
3612 - !mtd->block_markbad ||
3615 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3616 - !mtd->read_oob || !mtd->write_oob) {
3618 - !mtd->write_ecc ||
3619 - !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3621 - T(YAFFS_TRACE_ALWAYS,
3622 - ("yaffs: MTD device does not support required "
3627 - if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3628 - mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
3629 - !options.inband_tags) {
3630 - T(YAFFS_TRACE_ALWAYS,
3631 - ("yaffs: MTD device does not have the "
3632 - "right page sizes\n"));
3636 - /* Check for V1 style functions */
3637 - if (!mtd->erase ||
3640 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3641 - !mtd->read_oob || !mtd->write_oob) {
3643 - !mtd->write_ecc ||
3644 - !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3646 - T(YAFFS_TRACE_ALWAYS,
3647 - ("yaffs: MTD device does not support required "
3652 - if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3653 - mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3654 - T(YAFFS_TRACE_ALWAYS,
3655 - ("yaffs: MTD device does not support have the "
3656 - "right page sizes\n"));
3661 - /* OK, so if we got here, we have an MTD that's NAND and looks
3662 - * like it has the right capabilities
3663 - * Set the yaffs_Device up for mtd
3666 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3667 - sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3669 - sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3672 - /* Deep shit could not allocate device structure */
3673 - T(YAFFS_TRACE_ALWAYS,
3674 - ("yaffs_read_super: Failed trying to allocate "
3675 - "yaffs_Device. \n"));
3679 - memset(dev, 0, sizeof(yaffs_Device));
3680 - dev->genericDevice = mtd;
3681 - dev->name = mtd->name;
3683 - /* Set up the memory size parameters.... */
3685 - nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
3687 - dev->startBlock = 0;
3688 - dev->endBlock = nBlocks - 1;
3689 - dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3690 - dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3691 - dev->nReservedBlocks = 5;
3692 - dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3693 - dev->inbandTags = options.inband_tags;
3695 - /* ... and the functions. */
3696 - if (yaffsVersion == 2) {
3697 - dev->writeChunkWithTagsToNAND =
3698 - nandmtd2_WriteChunkWithTagsToNAND;
3699 - dev->readChunkWithTagsFromNAND =
3700 - nandmtd2_ReadChunkWithTagsFromNAND;
3701 - dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3702 - dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3703 - dev->spareBuffer = YMALLOC(mtd->oobsize);
3704 - dev->isYaffs2 = 1;
3705 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3706 - dev->totalBytesPerChunk = mtd->writesize;
3707 - dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3709 - dev->totalBytesPerChunk = mtd->oobblock;
3710 - dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3712 - nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3714 - dev->startBlock = 0;
3715 - dev->endBlock = nBlocks - 1;
3717 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3718 - /* use the MTD interface in yaffs_mtdif1.c */
3719 - dev->writeChunkWithTagsToNAND =
3720 - nandmtd1_WriteChunkWithTagsToNAND;
3721 - dev->readChunkWithTagsFromNAND =
3722 - nandmtd1_ReadChunkWithTagsFromNAND;
3723 - dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3724 - dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3726 - dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3727 - dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3729 - dev->isYaffs2 = 0;
3731 - /* ... and common functions */
3732 - dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3733 - dev->initialiseNAND = nandmtd_InitialiseNAND;
3735 - dev->putSuperFunc = yaffs_MTDPutSuper;
3737 - dev->superBlock = (void *)sb;
3738 - dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3741 -#ifndef CONFIG_YAFFS_DOES_ECC
3742 - dev->useNANDECC = 1;
3745 -#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3746 - dev->wideTnodesDisabled = 1;
3749 - dev->skipCheckpointRead = options.skip_checkpoint_read;
3750 - dev->skipCheckpointWrite = options.skip_checkpoint_write;
3752 - /* we assume this is protected by lock_kernel() in mount/umount */
3753 - ylist_add_tail(&dev->devList, &yaffs_dev_list);
3755 - init_MUTEX(&dev->grossLock);
3757 - yaffs_GrossLock(dev);
3759 - err = yaffs_GutsInitialise(dev);
3762 - ("yaffs_read_super: guts initialised %s\n",
3763 - (err == YAFFS_OK) ? "OK" : "FAILED"));
3765 - /* Release lock before yaffs_get_inode() */
3766 - yaffs_GrossUnlock(dev);
3768 - /* Create root inode */
3769 - if (err == YAFFS_OK)
3770 - inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3776 - inode->i_op = &yaffs_dir_inode_operations;
3777 - inode->i_fop = &yaffs_dir_operations;
3779 - T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3781 - root = d_alloc_root(inode);
3783 - T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3789 - sb->s_root = root;
3790 - sb->s_dirt = !dev->isCheckpointed;
3791 - T(YAFFS_TRACE_ALWAYS,
3792 - ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
3794 - T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3799 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3800 -static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3803 - return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3806 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3807 -static int yaffs_read_super(struct file_system_type *fs,
3808 - int flags, const char *dev_name,
3809 - void *data, struct vfsmount *mnt)
3812 - return get_sb_bdev(fs, flags, dev_name, data,
3813 - yaffs_internal_read_super_mtd, mnt);
3816 -static struct super_block *yaffs_read_super(struct file_system_type *fs,
3817 - int flags, const char *dev_name,
3821 - return get_sb_bdev(fs, flags, dev_name, data,
3822 - yaffs_internal_read_super_mtd);
3826 -static struct file_system_type yaffs_fs_type = {
3827 - .owner = THIS_MODULE,
3829 - .get_sb = yaffs_read_super,
3830 - .kill_sb = kill_block_super,
3831 - .fs_flags = FS_REQUIRES_DEV,
3834 -static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3837 - return yaffs_internal_read_super(1, sb, data, silent);
3840 -static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3845 -#ifdef CONFIG_YAFFS_YAFFS2
3847 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3848 -static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3851 - return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3854 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3855 -static int yaffs2_read_super(struct file_system_type *fs,
3856 - int flags, const char *dev_name, void *data,
3857 - struct vfsmount *mnt)
3859 - return get_sb_bdev(fs, flags, dev_name, data,
3860 - yaffs2_internal_read_super_mtd, mnt);
3863 -static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3864 - int flags, const char *dev_name,
3868 - return get_sb_bdev(fs, flags, dev_name, data,
3869 - yaffs2_internal_read_super_mtd);
3873 -static struct file_system_type yaffs2_fs_type = {
3874 - .owner = THIS_MODULE,
3876 - .get_sb = yaffs2_read_super,
3877 - .kill_sb = kill_block_super,
3878 - .fs_flags = FS_REQUIRES_DEV,
3881 -static struct super_block *yaffs2_read_super(struct super_block *sb,
3882 - void *data, int silent)
3884 - return yaffs_internal_read_super(2, sb, data, silent);
3887 -static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3891 -#endif /* CONFIG_YAFFS_YAFFS2 */
3893 -static struct proc_dir_entry *my_proc_entry;
3895 -static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3897 - buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3898 - buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3899 - buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
3900 - buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3901 - buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3902 - buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3903 - buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3904 - buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3905 - buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3906 - buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3907 - buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3908 - buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3909 - buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3910 - buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3911 - buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3912 - buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3913 - buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3914 - buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3915 - buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3916 - buf += sprintf(buf, "passiveGCs......... %d\n",
3917 - dev->passiveGarbageCollections);
3918 - buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3919 - buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3920 - buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3921 - buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3922 - buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3923 - buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3924 - buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3925 - buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3926 - buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3927 - buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3929 - sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3930 - buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3931 - buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3932 - buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
3937 -static int yaffs_proc_read(char *page,
3939 - off_t offset, int count, int *eof, void *data)
3941 - struct ylist_head *item;
3943 - int step = offset;
3946 - /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3947 - * We use 'offset' (*ppos) to indicate where we are in devList.
3948 - * This also assumes the user has posted a read buffer large
3949 - * enough to hold the complete output; but that's life in /proc.
3952 - *(int *)start = 1;
3954 - /* Print header first */
3956 - buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3957 - "\n%s\n%s\n", yaffs_fs_c_version,
3958 - yaffs_guts_c_version);
3961 - /* hold lock_kernel while traversing yaffs_dev_list */
3964 - /* Locate and print the Nth entry. Order N-squared but N is small. */
3965 - ylist_for_each(item, &yaffs_dev_list) {
3966 - yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
3971 - buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3972 - buf = yaffs_dump_dev(buf, dev);
3977 - return buf - page < count ? buf - page : count;
3981 - * Set the verbosity of the warnings and error messages.
3983 - * Note that the names can only be a..z or _ with the current code.
3988 - unsigned mask_bitfield;
3990 - {"allocate", YAFFS_TRACE_ALLOCATE},
3991 - {"always", YAFFS_TRACE_ALWAYS},
3992 - {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3993 - {"buffers", YAFFS_TRACE_BUFFERS},
3994 - {"bug", YAFFS_TRACE_BUG},
3995 - {"checkpt", YAFFS_TRACE_CHECKPOINT},
3996 - {"deletion", YAFFS_TRACE_DELETION},
3997 - {"erase", YAFFS_TRACE_ERASE},
3998 - {"error", YAFFS_TRACE_ERROR},
3999 - {"gc_detail", YAFFS_TRACE_GC_DETAIL},
4000 - {"gc", YAFFS_TRACE_GC},
4001 - {"mtd", YAFFS_TRACE_MTD},
4002 - {"nandaccess", YAFFS_TRACE_NANDACCESS},
4003 - {"os", YAFFS_TRACE_OS},
4004 - {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
4005 - {"scan", YAFFS_TRACE_SCAN},
4006 - {"tracing", YAFFS_TRACE_TRACING},
4008 - {"verify", YAFFS_TRACE_VERIFY},
4009 - {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
4010 - {"verify_full", YAFFS_TRACE_VERIFY_FULL},
4011 - {"verify_all", YAFFS_TRACE_VERIFY_ALL},
4013 - {"write", YAFFS_TRACE_WRITE},
4014 - {"all", 0xffffffff},
4019 -#define MAX_MASK_NAME_LENGTH 40
4020 -static int yaffs_proc_write(struct file *file, const char *buf,
4021 - unsigned long count, void *data)
4023 - unsigned rg = 0, mask_bitfield;
4027 - char substring[MAX_MASK_NAME_LENGTH + 1];
4033 - rg = yaffs_traceMask;
4035 - while (!done && (pos < count)) {
4037 - while ((pos < count) && isspace(buf[pos]))
4040 - switch (buf[pos]) {
4054 - mask_bitfield = simple_strtoul(buf + pos, &end, 0);
4056 - if (end > buf + pos) {
4057 - mask_name = "numeral";
4058 - len = end - (buf + pos);
4062 - for (x = buf + pos, i = 0;
4063 - (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
4064 - i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
4065 - substring[i] = *x;
4066 - substring[i] = '\0';
4068 - for (i = 0; mask_flags[i].mask_name != NULL; i++) {
4069 - if (strcmp(substring, mask_flags[i].mask_name) == 0) {
4070 - mask_name = mask_flags[i].mask_name;
4071 - mask_bitfield = mask_flags[i].mask_bitfield;
4078 - if (mask_name != NULL) {
4082 - rg &= ~mask_bitfield;
4085 - rg |= mask_bitfield;
4088 - rg = mask_bitfield;
4091 - rg |= mask_bitfield;
4097 - yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
4099 - printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
4101 - if (rg & YAFFS_TRACE_ALWAYS) {
4102 - for (i = 0; mask_flags[i].mask_name != NULL; i++) {
4104 - flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
4105 - printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
4112 -/* Stuff to handle installation of file systems */
4113 -struct file_system_to_install {
4114 - struct file_system_type *fst;
4118 -static struct file_system_to_install fs_to_install[] = {
4119 - {&yaffs_fs_type, 0},
4120 - {&yaffs2_fs_type, 0},
4124 -static int __init init_yaffs_fs(void)
4127 - struct file_system_to_install *fsinst;
4129 - T(YAFFS_TRACE_ALWAYS,
4130 - ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
4132 - /* Install the proc_fs entry */
4133 - my_proc_entry = create_proc_entry("yaffs",
4134 - S_IRUGO | S_IFREG,
4137 - if (my_proc_entry) {
4138 - my_proc_entry->write_proc = yaffs_proc_write;
4139 - my_proc_entry->read_proc = yaffs_proc_read;
4140 - my_proc_entry->data = NULL;
4144 - /* Now add the file system entries */
4146 - fsinst = fs_to_install;
4148 - while (fsinst->fst && !error) {
4149 - error = register_filesystem(fsinst->fst);
4151 - fsinst->installed = 1;
4155 - /* Any errors? uninstall */
4157 - fsinst = fs_to_install;
4159 - while (fsinst->fst) {
4160 - if (fsinst->installed) {
4161 - unregister_filesystem(fsinst->fst);
4162 - fsinst->installed = 0;
4171 -static void __exit exit_yaffs_fs(void)
4174 - struct file_system_to_install *fsinst;
4176 - T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
4177 - " removing. \n"));
4179 - remove_proc_entry("yaffs", YPROC_ROOT);
4181 - fsinst = fs_to_install;
4183 - while (fsinst->fst) {
4184 - if (fsinst->installed) {
4185 - unregister_filesystem(fsinst->fst);
4186 - fsinst->installed = 0;
4192 -module_init(init_yaffs_fs)
4193 -module_exit(exit_yaffs_fs)
4195 -MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
4196 -MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
4197 -MODULE_LICENSE("GPL");
4198 --- a/fs/yaffs2/yaffs_getblockinfo.h
4199 +++ b/fs/yaffs2/yaffs_getblockinfo.h
4202 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
4204 - * Copyright (C) 2002-2007 Aleph One Ltd.
4205 + * Copyright (C) 2002-2010 Aleph One Ltd.
4206 * for Toby Churchill Ltd and Brightstar Engineering
4208 * Created by Charles Manning <charles@aleph1.co.uk>
4210 #define __YAFFS_GETBLOCKINFO_H__
4212 #include "yaffs_guts.h"
4213 +#include "yaffs_trace.h"
4215 /* Function to manipulate block info */
4216 -static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
4217 +static Y_INLINE yaffs_block_info_t *yaffs_get_block_info(yaffs_dev_t * dev, int blk)
4219 - if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
4220 + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
4221 T(YAFFS_TRACE_ERROR,
4223 ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
4227 - return &dev->blockInfo[blk - dev->internalStartBlock];
4228 + return &dev->block_info[blk - dev->internal_start_block];
4232 --- a/fs/yaffs2/yaffs_guts.c
4233 +++ b/fs/yaffs2/yaffs_guts.c
4236 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4238 - * Copyright (C) 2002-2007 Aleph One Ltd.
4239 + * Copyright (C) 2002-2010 Aleph One Ltd.
4240 * for Toby Churchill Ltd and Brightstar Engineering
4242 * Created by Charles Manning <charles@aleph1.co.uk>
4244 * it under the terms of the GNU General Public License version 2 as
4245 * published by the Free Software Foundation.
4248 -const char *yaffs_guts_c_version =
4249 - "$Id: yaffs_guts.c,v 1.82 2009-03-09 04:24:17 charles Exp $";
4251 #include "yportenv.h"
4252 +#include "yaffs_trace.h"
4254 #include "yaffsinterface.h"
4255 #include "yaffs_guts.h"
4256 @@ -22,118 +19,109 @@ const char *yaffs_guts_c_version =
4257 #include "yaffs_getblockinfo.h"
4259 #include "yaffs_tagscompat.h"
4260 -#ifndef CONFIG_YAFFS_USE_OWN_SORT
4261 -#include "yaffs_qsort.h"
4264 #include "yaffs_nand.h"
4266 -#include "yaffs_checkptrw.h"
4267 +#include "yaffs_yaffs1.h"
4268 +#include "yaffs_yaffs2.h"
4269 +#include "yaffs_bitmap.h"
4270 +#include "yaffs_verify.h"
4272 #include "yaffs_nand.h"
4273 #include "yaffs_packedtags2.h"
4275 +#include "yaffs_nameval.h"
4276 +#include "yaffs_allocator.h"
4278 -#define YAFFS_PASSIVE_GC_CHUNKS 2
4279 +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
4280 +#define YAFFS_GC_GOOD_ENOUGH 2
4281 +#define YAFFS_GC_PASSIVE_THRESHOLD 4
4283 #include "yaffs_ecc.h"
4287 /* Robustification (if it ever comes about...) */
4288 -static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
4289 -static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
4290 +static void yaffs_retire_block(yaffs_dev_t *dev, int flash_block);
4291 +static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
4293 -static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
4294 +static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
4296 - const yaffs_ExtendedTags *tags);
4297 -static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
4298 - const yaffs_ExtendedTags *tags);
4299 + const yaffs_ext_tags *tags);
4300 +static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
4301 + const yaffs_ext_tags *tags);
4303 /* Other local prototypes */
4304 -static int yaffs_UnlinkObject(yaffs_Object *obj);
4305 -static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
4307 -static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
4308 +static void yaffs_update_parent(yaffs_obj_t *obj);
4309 +static int yaffs_unlink_obj(yaffs_obj_t *obj);
4310 +static int yaffs_obj_cache_dirty(yaffs_obj_t *obj);
4312 -static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
4313 +static int yaffs_write_new_chunk(yaffs_dev_t *dev,
4315 - yaffs_ExtendedTags *tags,
4316 + yaffs_ext_tags *tags,
4318 -static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
4319 - int chunkInNAND, int inScan);
4321 -static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
4322 - yaffs_ObjectType type);
4323 -static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
4324 - yaffs_Object *obj);
4325 -static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
4326 - int force, int isShrink, int shadows);
4327 -static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
4328 -static int yaffs_CheckStructures(void);
4329 -static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
4330 - int chunkOffset, int *limit);
4331 -static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
4333 -static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
4335 +static yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
4336 + yaffs_obj_type type);
4338 -static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4341 -static int yaffs_UnlinkWorker(yaffs_Object *obj);
4342 +static int yaffs_apply_xattrib_mod(yaffs_obj_t *obj, char *buffer, yaffs_xattr_mod *xmod);
4344 -static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
4345 - int chunkInObject);
4346 +static void yaffs_remove_obj_from_dir(yaffs_obj_t *obj);
4347 +static int yaffs_check_structures(void);
4348 +static int yaffs_generic_obj_del(yaffs_obj_t *in);
4350 +static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
4353 -static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
4354 - yaffs_BlockInfo **blockUsedPtr);
4355 +static int yaffs_unlink_worker(yaffs_obj_t *obj);
4357 -static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
4358 +static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
4359 + int chunkInObject);
4361 -static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
4362 +static int yaffs_alloc_chunk(yaffs_dev_t *dev, int useReserve,
4363 + yaffs_block_info_t **blockUsedPtr);
4365 -static void yaffs_VerifyDirectory(yaffs_Object *directory);
4366 -#ifdef YAFFS_PARANOID
4367 -static int yaffs_CheckFileSanity(yaffs_Object *in);
4369 -#define yaffs_CheckFileSanity(in)
4371 +static void yaffs_check_obj_details_loaded(yaffs_obj_t *in);
4373 +static void yaffs_invalidate_whole_cache(yaffs_obj_t *in);
4374 +static void yaffs_invalidate_chunk_cache(yaffs_obj_t *object, int chunk_id);
4376 -static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
4377 -static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
4378 +static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
4379 + yaffs_ext_tags *tags);
4381 -static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
4382 +static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
4385 + yaffs_ext_tags *tags);
4387 -static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
4388 - yaffs_ExtendedTags *tags);
4390 -static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
4392 -static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
4393 - yaffs_FileStructure *fStruct,
4395 +static void yaffs_load_name_from_oh(yaffs_dev_t *dev,YCHAR *name, const YCHAR *ohName, int bufferSize);
4396 +static void yaffs_load_oh_from_name(yaffs_dev_t *dev,YCHAR *ohName, const YCHAR *name);
4399 /* Function to calculate chunk and offset */
4401 -static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
4402 +static void yaffs_addr_to_chunk(yaffs_dev_t *dev, loff_t addr, int *chunkOut,
4408 - chunk = (__u32)(addr >> dev->chunkShift);
4409 + chunk = (__u32)(addr >> dev->chunk_shift);
4411 - if (dev->chunkDiv == 1) {
4412 + if (dev->chunk_div == 1) {
4413 /* easy power of 2 case */
4414 - offset = (__u32)(addr & dev->chunkMask);
4415 + offset = (__u32)(addr & dev->chunk_mask);
4417 /* Non power-of-2 case */
4421 - chunk /= dev->chunkDiv;
4422 + chunk /= dev->chunk_div;
4424 - chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
4425 + chunkBase = ((loff_t)chunk) * dev->data_bytes_per_chunk;
4426 offset = (__u32)(addr - chunkBase);
4429 @@ -172,7 +160,7 @@ static __u32 ShiftsGE(__u32 x)
4431 static __u32 Shifts(__u32 x)
4438 @@ -193,49 +181,49 @@ static __u32 Shifts(__u32 x)
4439 * Temporary buffer manipulations.
4442 -static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
4443 +static int yaffs_init_tmp_buffers(yaffs_dev_t *dev)
4446 __u8 *buf = (__u8 *)1;
4448 - memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
4449 + memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
4451 for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
4452 - dev->tempBuffer[i].line = 0; /* not in use */
4453 - dev->tempBuffer[i].buffer = buf =
4454 - YMALLOC_DMA(dev->totalBytesPerChunk);
4455 + dev->temp_buffer[i].line = 0; /* not in use */
4456 + dev->temp_buffer[i].buffer = buf =
4457 + YMALLOC_DMA(dev->param.total_bytes_per_chunk);
4460 return buf ? YAFFS_OK : YAFFS_FAIL;
4463 -__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
4464 +__u8 *yaffs_get_temp_buffer(yaffs_dev_t *dev, int line_no)
4469 - if (dev->tempInUse > dev->maxTemp)
4470 - dev->maxTemp = dev->tempInUse;
4471 + dev->temp_in_use++;
4472 + if (dev->temp_in_use > dev->max_temp)
4473 + dev->max_temp = dev->temp_in_use;
4475 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4476 - if (dev->tempBuffer[i].line == 0) {
4477 - dev->tempBuffer[i].line = lineNo;
4478 - if ((i + 1) > dev->maxTemp) {
4479 - dev->maxTemp = i + 1;
4480 + if (dev->temp_buffer[i].line == 0) {
4481 + dev->temp_buffer[i].line = line_no;
4482 + if ((i + 1) > dev->max_temp) {
4483 + dev->max_temp = i + 1;
4484 for (j = 0; j <= i; j++)
4485 - dev->tempBuffer[j].maxLine =
4486 - dev->tempBuffer[j].line;
4487 + dev->temp_buffer[j].max_line =
4488 + dev->temp_buffer[j].line;
4491 - return dev->tempBuffer[i].buffer;
4492 + return dev->temp_buffer[i].buffer;
4496 T(YAFFS_TRACE_BUFFERS,
4497 (TSTR("Out of temp buffers at line %d, other held by lines:"),
4500 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4501 - T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
4502 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->temp_buffer[i].line));
4504 T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
4506 @@ -244,21 +232,21 @@ __u8 *yaffs_GetTempBuffer(yaffs_Device *
4510 - dev->unmanagedTempAllocations++;
4511 - return YMALLOC(dev->nDataBytesPerChunk);
4512 + dev->unmanaged_buffer_allocs++;
4513 + return YMALLOC(dev->data_bytes_per_chunk);
4517 -void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
4519 +void yaffs_release_temp_buffer(yaffs_dev_t *dev, __u8 *buffer,
4525 + dev->temp_in_use--;
4527 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4528 - if (dev->tempBuffer[i].buffer == buffer) {
4529 - dev->tempBuffer[i].line = 0;
4530 + if (dev->temp_buffer[i].buffer == buffer) {
4531 + dev->temp_buffer[i].line = 0;
4535 @@ -267,9 +255,9 @@ void yaffs_ReleaseTempBuffer(yaffs_Devic
4536 /* assume it is an unmanaged one. */
4537 T(YAFFS_TRACE_BUFFERS,
4538 (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
4542 - dev->unmanagedTempDeallocations++;
4543 + dev->unmanaged_buffer_deallocs++;
4547 @@ -277,21 +265,21 @@ void yaffs_ReleaseTempBuffer(yaffs_Devic
4549 * Determine if we have a managed buffer.
4551 -int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
4552 +int yaffs_is_managed_tmp_buffer(yaffs_dev_t *dev, const __u8 *buffer)
4556 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4557 - if (dev->tempBuffer[i].buffer == buffer)
4558 + if (dev->temp_buffer[i].buffer == buffer)
4562 - for (i = 0; i < dev->nShortOpCaches; i++) {
4563 - if (dev->srCache[i].data == buffer)
4564 + for (i = 0; i < dev->param.n_caches; i++) {
4565 + if (dev->cache[i].data == buffer)
4569 - if (buffer == dev->checkpointBuffer)
4570 + if (buffer == dev->checkpt_buffer)
4573 T(YAFFS_TRACE_ALWAYS,
4574 @@ -299,6397 +287,4205 @@ int yaffs_IsManagedTempBuffer(yaffs_Devi
4581 - * Chunk bitmap manipulations
4582 + * Verification code
4585 -static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
4587 - if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
4588 - T(YAFFS_TRACE_ERROR,
4589 - (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
4593 - return dev->chunkBits +
4594 - (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
4597 -static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
4599 - if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
4600 - chunk < 0 || chunk >= dev->nChunksPerBlock) {
4601 - T(YAFFS_TRACE_ERROR,
4602 - (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
4608 -static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
4610 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4612 - memset(blkBits, 0, dev->chunkBitmapStride);
4615 + * Simple hash function. Needs to have a reasonable spread
4618 -static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
4619 +static Y_INLINE int yaffs_hash_fn(int n)
4621 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4623 - yaffs_VerifyChunkBitId(dev, blk, chunk);
4625 - blkBits[chunk / 8] &= ~(1 << (chunk & 7));
4627 + return n % YAFFS_NOBJECT_BUCKETS;
4630 -static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
4632 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4634 - yaffs_VerifyChunkBitId(dev, blk, chunk);
4636 - blkBits[chunk / 8] |= (1 << (chunk & 7));
4639 + * Access functions to useful fake objects.
4640 + * Note that root might have a presence in NAND if permissions are set.
4643 -static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
4644 +yaffs_obj_t *yaffs_root(yaffs_dev_t *dev)
4646 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4647 - yaffs_VerifyChunkBitId(dev, blk, chunk);
4649 - return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4650 + return dev->root_dir;
4653 -static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
4654 +yaffs_obj_t *yaffs_lost_n_found(yaffs_dev_t *dev)
4656 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4658 - for (i = 0; i < dev->chunkBitmapStride; i++) {
4664 + return dev->lost_n_found;
4667 -static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
4669 - __u8 *blkBits = yaffs_BlockBits(dev, blk);
4672 - for (i = 0; i < dev->chunkBitmapStride; i++) {
4673 - __u8 x = *blkBits;
4686 - * Verification code
4687 + * Erased NAND checking functions
4690 -static int yaffs_SkipVerification(yaffs_Device *dev)
4692 - return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4695 -static int yaffs_SkipFullVerification(yaffs_Device *dev)
4697 - return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4700 -static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4701 +int yaffs_check_ff(__u8 *buffer, int n_bytes)
4703 - return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4704 + /* Horrible, slow implementation */
4705 + while (n_bytes--) {
4706 + if (*buffer != 0xFF)
4713 -static const char *blockStateName[] = {
4726 -static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
4727 +static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
4732 + int retval = YAFFS_OK;
4733 + __u8 *data = yaffs_get_temp_buffer(dev, __LINE__);
4734 + yaffs_ext_tags tags;
4737 - if (yaffs_SkipVerification(dev))
4739 + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
4741 - /* Report illegal runtime states */
4742 - if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4743 - T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
4744 + if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
4745 + retval = YAFFS_FAIL;
4747 - switch (bi->blockState) {
4748 - case YAFFS_BLOCK_STATE_UNKNOWN:
4749 - case YAFFS_BLOCK_STATE_SCANNING:
4750 - case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4751 - T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
4752 - n, blockStateName[bi->blockState]));
4753 + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) || tags.chunk_used) {
4754 + T(YAFFS_TRACE_NANDACCESS,
4755 + (TSTR("Chunk %d not erased" TENDSTR), nand_chunk));
4756 + retval = YAFFS_FAIL;
4759 - /* Check pages in use and soft deletions are legal */
4761 - actuallyUsed = bi->pagesInUse - bi->softDeletions;
4763 - if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4764 - bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4765 - actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4766 - T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4767 - n, bi->pagesInUse, bi->softDeletions));
4769 + yaffs_release_temp_buffer(dev, data, __LINE__);
4771 - /* Check chunk bitmap legal */
4772 - inUse = yaffs_CountChunkBits(dev, n);
4773 - if (inUse != bi->pagesInUse)
4774 - T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4775 - n, bi->pagesInUse, inUse));
4778 - /* Check that the sequence number is valid.
4779 - * Ten million is legal, but is very unlikely
4781 - if (dev->isYaffs2 &&
4782 - (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4783 - (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
4784 - T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4785 - n, bi->sequenceNumber));
4788 -static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
4791 +static int yaffs_verify_chunk_written(yaffs_dev_t *dev,
4794 + yaffs_ext_tags *tags)
4796 - yaffs_VerifyBlock(dev, bi, n);
4797 + int retval = YAFFS_OK;
4798 + yaffs_ext_tags tempTags;
4799 + __u8 *buffer = yaffs_get_temp_buffer(dev,__LINE__);
4802 + result = yaffs_rd_chunk_tags_nand(dev,nand_chunk,buffer,&tempTags);
4803 + if(memcmp(buffer,data,dev->data_bytes_per_chunk) ||
4804 + tempTags.obj_id != tags->obj_id ||
4805 + tempTags.chunk_id != tags->chunk_id ||
4806 + tempTags.n_bytes != tags->n_bytes)
4807 + retval = YAFFS_FAIL;
4809 - /* After collection the block should be in the erased state */
4810 - /* This will need to change if we do partial gc */
4811 + yaffs_release_temp_buffer(dev, buffer, __LINE__);
4813 - if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
4814 - bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
4815 - T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4816 - n, bi->blockState));
4821 -static void yaffs_VerifyBlocks(yaffs_Device *dev)
4822 +static int yaffs_write_new_chunk(struct yaffs_dev_s *dev,
4824 + yaffs_ext_tags *tags,
4828 - int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4829 - int nIllegalBlockStates = 0;
4831 - if (yaffs_SkipVerification(dev))
4834 - memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
4836 - for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
4837 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
4838 - yaffs_VerifyBlock(dev, bi, i);
4840 - if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4841 - nBlocksPerState[bi->blockState]++;
4843 - nIllegalBlockStates++;
4849 - T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
4850 - T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
4851 + yaffs2_checkpt_invalidate(dev);
4853 - T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
4854 - if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4855 - T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
4857 + yaffs_block_info_t *bi = 0;
4860 - for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4861 - T(YAFFS_TRACE_VERIFY,
4862 - (TSTR("%s %d blocks"TENDSTR),
4863 - blockStateName[i], nBlocksPerState[i]));
4864 + chunk = yaffs_alloc_chunk(dev, useReserve, &bi);
4870 - if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4871 - T(YAFFS_TRACE_VERIFY,
4872 - (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4873 - dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4874 + /* First check this chunk is erased, if it needs
4875 + * checking. The checking policy (unless forced
4876 + * always on) is as follows:
4878 + * Check the first page we try to write in a block.
4879 + * If the check passes then we don't need to check any
4880 + * more. If the check fails, we check again...
4881 + * If the block has been erased, we don't need to check.
4883 + * However, if the block has been prioritised for gc,
4884 + * then we think there might be something odd about
4885 + * this block and stop using it.
4887 + * Rationale: We should only ever see chunks that have
4888 + * not been erased if there was a partially written
4889 + * chunk due to power loss. This checking policy should
4890 + * catch that case with very few checks and thus save a
4891 + * lot of checks that are most likely not needed.
4893 + * Mods to the above
4894 + * If an erase check fails or the write fails we skip the
4895 + * rest of the block.
4898 - if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4899 - T(YAFFS_TRACE_VERIFY,
4900 - (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4901 - dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4902 + /* let's give it a try */
4905 - if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4906 - T(YAFFS_TRACE_VERIFY,
4907 - (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4908 - nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4909 + if(dev->param.always_check_erased)
4910 + bi->skip_erased_check = 0;
4912 - T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
4913 + if (!bi->skip_erased_check) {
4914 + erasedOk = yaffs_check_chunk_erased(dev, chunk);
4915 + if (erasedOk != YAFFS_OK) {
4916 + T(YAFFS_TRACE_ERROR,
4917 + (TSTR("**>> yaffs chunk %d was not erased"
4918 + TENDSTR), chunk));
4921 + /* If not erased, delete this one,
4922 + * skip rest of block and
4923 + * try another chunk */
4924 + yaffs_chunk_del(dev,chunk,1,__LINE__);
4925 + yaffs_skip_rest_of_block(dev);
4931 - * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4932 - * case those tests will not be performed.
4934 -static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4936 - if (obj && yaffs_SkipVerification(obj->myDev))
4938 + writeOk = yaffs_wr_chunk_tags_nand(dev, chunk,
4941 - if (!(tags && obj && oh)) {
4942 - T(YAFFS_TRACE_VERIFY,
4943 - (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4944 - (__u32)tags, (__u32)obj, (__u32)oh));
4947 + if(!bi->skip_erased_check)
4948 + writeOk = yaffs_verify_chunk_written(dev, chunk, data, tags);
4950 - if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4951 - oh->type > YAFFS_OBJECT_TYPE_MAX)
4952 - T(YAFFS_TRACE_VERIFY,
4953 - (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4954 - tags->objectId, oh->type));
4955 + if (writeOk != YAFFS_OK) {
4956 + /* Clean up aborted write, skip to next block and
4957 + * try another chunk */
4958 + yaffs_handle_chunk_wr_error(dev, chunk, erasedOk);
4962 - if (tags->objectId != obj->objectId)
4963 - T(YAFFS_TRACE_VERIFY,
4964 - (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4965 - tags->objectId, obj->objectId));
4966 + bi->skip_erased_check = 1;
4968 + /* Copy the data into the robustification buffer */
4969 + yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
4972 - * Check that the object's parent ids match if parentCheck requested.
4974 - * Tests do not apply to the root object.
4976 + } while (writeOk != YAFFS_OK &&
4977 + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
4979 - if (parentCheck && tags->objectId > 1 && !obj->parent)
4980 - T(YAFFS_TRACE_VERIFY,
4981 - (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4982 - tags->objectId, oh->parentObjectId));
4986 - if (parentCheck && obj->parent &&
4987 - oh->parentObjectId != obj->parent->objectId &&
4988 - (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4989 - obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4990 - T(YAFFS_TRACE_VERIFY,
4991 - (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4992 - tags->objectId, oh->parentObjectId, obj->parent->objectId));
4993 + if (attempts > 1) {
4994 + T(YAFFS_TRACE_ERROR,
4995 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4998 - if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4999 - T(YAFFS_TRACE_VERIFY,
5000 - (TSTR("Obj %d header name is NULL"TENDSTR),
5002 + dev->n_retired_writes += (attempts - 1);
5005 - if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
5006 - T(YAFFS_TRACE_VERIFY,
5007 - (TSTR("Obj %d header name is 0xFF"TENDSTR),
5015 + * Block retiring for handling a broken block.
5018 -static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
5019 - __u32 level, int chunkOffset)
5020 +static void yaffs_retire_block(yaffs_dev_t *dev, int flash_block)
5023 - yaffs_Device *dev = obj->myDev;
5025 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
5029 + yaffs2_checkpt_invalidate(dev);
5031 + yaffs2_clear_oldest_dirty_seq(dev,bi);
5033 - for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
5034 - if (tn->internal[i]) {
5035 - ok = yaffs_VerifyTnodeWorker(obj,
5038 - (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
5041 - } else if (level == 0) {
5042 - yaffs_ExtendedTags tags;
5043 - __u32 objectId = obj->objectId;
5044 + if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
5045 + if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
5046 + T(YAFFS_TRACE_ALWAYS, (TSTR(
5047 + "yaffs: Failed to mark bad and erase block %d"
5048 + TENDSTR), flash_block));
5050 + yaffs_ext_tags tags;
5051 + int chunk_id = flash_block * dev->param.chunks_per_block;
5053 - chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
5054 + __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
5056 - for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
5057 - __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
5058 + memset(buffer, 0xff, dev->data_bytes_per_chunk);
5059 + yaffs_init_tags(&tags);
5060 + tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
5061 + if (dev->param.write_chunk_tags_fn(dev, chunk_id -
5062 + dev->chunk_offset, buffer, &tags) != YAFFS_OK)
5063 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
5064 + TCONT("write bad block marker to block %d")
5065 + TENDSTR), flash_block));
5067 - if (theChunk > 0) {
5068 - /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
5069 - yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
5070 - if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
5071 - T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
5072 - objectId, chunkOffset, theChunk,
5073 - tags.objectId, tags.chunkId));
5078 + yaffs_release_temp_buffer(dev, buffer, __LINE__);
5083 + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
5084 + bi->gc_prioritise = 0;
5085 + bi->needs_retiring = 0;
5087 + dev->n_retired_blocks++;
5091 + * Functions for robustisizing TODO
5095 -static void yaffs_VerifyFile(yaffs_Object *obj)
5096 +static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
5098 + const yaffs_ext_tags *tags)
5100 - int requiredTallness;
5101 - int actualTallness;
5105 - yaffs_Device *dev;
5106 - yaffs_ExtendedTags tags;
5110 + nand_chunk=nand_chunk;
5117 +static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
5118 + const yaffs_ext_tags *tags)
5121 + nand_chunk=nand_chunk;
5125 - if (yaffs_SkipVerification(obj->myDev))
5127 +void yaffs_handle_chunk_error(yaffs_dev_t *dev, yaffs_block_info_t *bi)
5129 + if (!bi->gc_prioritise) {
5130 + bi->gc_prioritise = 1;
5131 + dev->has_pending_prioritised_gc = 1;
5132 + bi->chunk_error_strikes++;
5135 - objectId = obj->objectId;
5136 + if (bi->chunk_error_strikes > 3) {
5137 + bi->needs_retiring = 1; /* Too many stikes, so retire this */
5138 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
5140 - /* Check file size is consistent with tnode depth */
5141 - lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
5142 - x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
5143 - requiredTallness = 0;
5145 - x >>= YAFFS_TNODES_INTERNAL_BITS;
5146 - requiredTallness++;
5151 - actualTallness = obj->variant.fileVariant.topLevel;
5152 +static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk,
5155 + int flash_block = nand_chunk / dev->param.chunks_per_block;
5156 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, flash_block);
5158 - if (requiredTallness > actualTallness)
5159 - T(YAFFS_TRACE_VERIFY,
5160 - (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
5161 - obj->objectId, actualTallness, requiredTallness));
5162 + yaffs_handle_chunk_error(dev, bi);
5165 + /* Was an actual write failure, so mark the block for retirement */
5166 + bi->needs_retiring = 1;
5167 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5168 + (TSTR("**>> Block %d needs retiring" TENDSTR), flash_block));
5171 - /* Check that the chunks in the tnode tree are all correct.
5172 - * We do this by scanning through the tnode tree and
5173 - * checking the tags for every chunk match.
5175 + /* Delete the chunk */
5176 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
5177 + yaffs_skip_rest_of_block(dev);
5180 - if (yaffs_SkipNANDVerification(dev))
5183 - for (i = 1; i <= lastChunk; i++) {
5184 - tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
5185 +/*---------------- Name handling functions ------------*/
5188 - __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
5189 - if (theChunk > 0) {
5190 - /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
5191 - yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
5192 - if (tags.objectId != objectId || tags.chunkId != i) {
5193 - T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
5194 - objectId, i, theChunk,
5195 - tags.objectId, tags.chunkId));
5198 +static __u16 yaffs_calc_name_sum(const YCHAR *name)
5203 + const YUCHAR *bname = (const YUCHAR *) name;
5205 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
5207 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
5208 + sum += yaffs_toupper(*bname) * i;
5210 + sum += (*bname) * i;
5220 -static void yaffs_VerifyHardLink(yaffs_Object *obj)
5221 +void yaffs_set_obj_name(yaffs_obj_t *obj, const YCHAR *name)
5223 - if (obj && yaffs_SkipVerification(obj->myDev))
5226 - /* Verify sane equivalent object */
5227 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5228 + memset(obj->short_name, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
5229 + if (name && yaffs_strnlen(name,YAFFS_SHORT_NAME_LENGTH+1) <= YAFFS_SHORT_NAME_LENGTH)
5230 + yaffs_strcpy(obj->short_name, name);
5232 + obj->short_name[0] = _Y('\0');
5234 + obj->sum = yaffs_calc_name_sum(name);
5237 -static void yaffs_VerifySymlink(yaffs_Object *obj)
5238 +void yaffs_set_obj_name_from_oh(yaffs_obj_t *obj, const yaffs_obj_header *oh)
5240 - if (obj && yaffs_SkipVerification(obj->myDev))
5243 - /* Verify symlink string */
5244 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
5245 + YCHAR tmpName[YAFFS_MAX_NAME_LENGTH+1];
5246 + memset(tmpName,0,sizeof(tmpName));
5247 + yaffs_load_name_from_oh(obj->my_dev,tmpName,oh->name,YAFFS_MAX_NAME_LENGTH+1);
5248 + yaffs_set_obj_name(obj,tmpName);
5250 + yaffs_set_obj_name(obj,oh->name);
5254 -static void yaffs_VerifySpecial(yaffs_Object *obj)
5256 - if (obj && yaffs_SkipVerification(obj->myDev))
5260 -static void yaffs_VerifyObject(yaffs_Object *obj)
5262 - yaffs_Device *dev;
5268 - __u32 chunkInRange;
5269 - __u32 chunkShouldNotBeDeleted;
5275 - if (obj->beingCreated)
5277 +/*-------------------- TNODES -------------------
5280 + * List of spare tnodes
5281 + * The list is hooked together using the first pointer
5285 - if (yaffs_SkipVerification(dev))
5288 - /* Check sane object header chunk */
5289 +yaffs_tnode_t *yaffs_get_tnode(yaffs_dev_t *dev)
5291 + yaffs_tnode_t *tn = yaffs_alloc_raw_tnode(dev);
5293 + memset(tn, 0, dev->tnode_size);
5297 - chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
5298 - chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
5299 + dev->checkpoint_blocks_required = 0; /* force recalculation*/
5301 - chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
5302 - chunkIdOk = chunkInRange || obj->hdrChunk == 0;
5303 - chunkValid = chunkInRange &&
5304 - yaffs_CheckChunkBit(dev,
5305 - obj->hdrChunk / dev->nChunksPerBlock,
5306 - obj->hdrChunk % dev->nChunksPerBlock);
5307 - chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
5312 - (!chunkIdOk || chunkShouldNotBeDeleted)) {
5313 - T(YAFFS_TRACE_VERIFY,
5314 - (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
5315 - obj->objectId, obj->hdrChunk,
5316 - chunkIdOk ? "" : ",out of range",
5317 - chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
5319 +/* FreeTnode frees up a tnode and puts it back on the free list */
5320 +static void yaffs_free_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
5322 + yaffs_free_raw_tnode(dev,tn);
5324 + dev->checkpoint_blocks_required = 0; /* force recalculation*/
5327 - if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
5328 - yaffs_ExtendedTags tags;
5329 - yaffs_ObjectHeader *oh;
5330 - __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
5331 +static void yaffs_deinit_tnodes_and_objs(yaffs_dev_t *dev)
5333 + yaffs_deinit_raw_tnodes_and_objs(dev);
5335 + dev->n_tnodes = 0;
5338 - oh = (yaffs_ObjectHeader *)buffer;
5340 - yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
5342 +void yaffs_load_tnode_0(yaffs_dev_t *dev, yaffs_tnode_t *tn, unsigned pos,
5345 + __u32 *map = (__u32 *)tn;
5351 - yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
5352 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5353 + val >>= dev->chunk_grp_bits;
5355 - yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
5357 + bitInMap = pos * dev->tnode_width;
5358 + wordInMap = bitInMap / 32;
5359 + bitInWord = bitInMap & (32 - 1);
5361 - /* Verify it has a parent */
5362 - if (obj && !obj->fake &&
5363 - (!obj->parent || obj->parent->myDev != dev)) {
5364 - T(YAFFS_TRACE_VERIFY,
5365 - (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
5366 - obj->objectId, obj->parent));
5368 + mask = dev->tnode_mask << bitInWord;
5370 - /* Verify parent is a directory */
5371 - if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
5372 - T(YAFFS_TRACE_VERIFY,
5373 - (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
5374 - obj->objectId, obj->parent->variantType));
5376 + map[wordInMap] &= ~mask;
5377 + map[wordInMap] |= (mask & (val << bitInWord));
5379 - switch (obj->variantType) {
5380 - case YAFFS_OBJECT_TYPE_FILE:
5381 - yaffs_VerifyFile(obj);
5383 - case YAFFS_OBJECT_TYPE_SYMLINK:
5384 - yaffs_VerifySymlink(obj);
5386 - case YAFFS_OBJECT_TYPE_DIRECTORY:
5387 - yaffs_VerifyDirectory(obj);
5389 - case YAFFS_OBJECT_TYPE_HARDLINK:
5390 - yaffs_VerifyHardLink(obj);
5392 - case YAFFS_OBJECT_TYPE_SPECIAL:
5393 - yaffs_VerifySpecial(obj);
5395 - case YAFFS_OBJECT_TYPE_UNKNOWN:
5397 - T(YAFFS_TRACE_VERIFY,
5398 - (TSTR("Obj %d has illegaltype %d"TENDSTR),
5399 - obj->objectId, obj->variantType));
5401 + if (dev->tnode_width > (32 - bitInWord)) {
5402 + bitInWord = (32 - bitInWord);
5404 + mask = dev->tnode_mask >> (/*dev->tnode_width -*/ bitInWord);
5405 + map[wordInMap] &= ~mask;
5406 + map[wordInMap] |= (mask & (val >> bitInWord));
5410 -static void yaffs_VerifyObjects(yaffs_Device *dev)
5411 +__u32 yaffs_get_group_base(yaffs_dev_t *dev, yaffs_tnode_t *tn,
5414 - yaffs_Object *obj;
5416 - struct ylist_head *lh;
5417 + __u32 *map = (__u32 *)tn;
5423 - if (yaffs_SkipVerification(dev))
5425 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5427 - /* Iterate through the objects in each hash entry */
5428 + bitInMap = pos * dev->tnode_width;
5429 + wordInMap = bitInMap / 32;
5430 + bitInWord = bitInMap & (32 - 1);
5432 - for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5433 - ylist_for_each(lh, &dev->objectBucket[i].list) {
5435 - obj = ylist_entry(lh, yaffs_Object, hashLink);
5436 - yaffs_VerifyObject(obj);
5441 + val = map[wordInMap] >> bitInWord;
5443 + if (dev->tnode_width > (32 - bitInWord)) {
5444 + bitInWord = (32 - bitInWord);
5446 + val |= (map[wordInMap] << bitInWord);
5450 - * Simple hash function. Needs to have a reasonable spread
5452 + val &= dev->tnode_mask;
5453 + val <<= dev->chunk_grp_bits;
5455 -static Y_INLINE int yaffs_HashFunction(int n)
5458 - return n % YAFFS_NOBJECT_BUCKETS;
5463 - * Access functions to useful fake objects.
5464 - * Note that root might have a presence in NAND if permissions are set.
5465 +/* ------------------- End of individual tnode manipulation -----------------*/
5467 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5468 + * The look up tree is represented by the top tnode and the number of top_level
5469 + * in the tree. 0 means only the level 0 tnode is in the tree.
5472 -yaffs_Object *yaffs_Root(yaffs_Device *dev)
5473 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5474 +yaffs_tnode_t *yaffs_find_tnode_0(yaffs_dev_t *dev,
5475 + yaffs_file_s *file_struct,
5478 - return dev->rootDir;
5480 + yaffs_tnode_t *tn = file_struct->top;
5482 + int requiredTallness;
5483 + int level = file_struct->top_level;
5485 -yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
5487 - return dev->lostNFoundDir;
5491 + /* Check sane level and chunk Id */
5492 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
5496 - * Erased NAND checking functions
5498 + if (chunk_id > YAFFS_MAX_CHUNK_ID)
5501 -int yaffs_CheckFF(__u8 *buffer, int nBytes)
5503 - /* Horrible, slow implementation */
5504 - while (nBytes--) {
5505 - if (*buffer != 0xFF)
5508 + /* First check we're tall enough (ie enough top_level) */
5510 + i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
5511 + requiredTallness = 0;
5513 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5514 + requiredTallness++;
5519 -static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
5522 - int retval = YAFFS_OK;
5523 - __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
5524 - yaffs_ExtendedTags tags;
5526 + if (requiredTallness > file_struct->top_level)
5527 + return NULL; /* Not tall enough, so we can't find it */
5529 + /* Traverse down to level 0 */
5530 + while (level > 0 && tn) {
5531 + tn = tn->internal[(chunk_id >>
5532 + (YAFFS_TNODES_LEVEL0_BITS +
5534 + YAFFS_TNODES_INTERNAL_BITS)) &
5535 + YAFFS_TNODES_INTERNAL_MASK];
5539 - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
5543 - if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
5544 - retval = YAFFS_FAIL;
5545 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5546 + * This happens in two steps:
5547 + * 1. If the tree isn't tall enough, then make it taller.
5548 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5550 + * Used when modifying the tree.
5552 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5553 + * be plugged into the ttree.
5556 - if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
5557 - T(YAFFS_TRACE_NANDACCESS,
5558 - (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
5559 - retval = YAFFS_FAIL;
5561 +yaffs_tnode_t *yaffs_add_find_tnode_0(yaffs_dev_t *dev,
5562 + yaffs_file_s *file_struct,
5564 + yaffs_tnode_t *passed_tn)
5566 + int requiredTallness;
5569 + yaffs_tnode_t *tn;
5571 - yaffs_ReleaseTempBuffer(dev, data, __LINE__);
5577 + /* Check sane level and page Id */
5578 + if (file_struct->top_level < 0 || file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
5581 -static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
5583 - yaffs_ExtendedTags *tags,
5589 + if (chunk_id > YAFFS_MAX_CHUNK_ID)
5592 - yaffs_InvalidateCheckpoint(dev);
5593 + /* First check we're tall enough (ie enough top_level) */
5596 - yaffs_BlockInfo *bi = 0;
5598 + x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
5599 + requiredTallness = 0;
5601 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5602 + requiredTallness++;
5605 - chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
5611 - /* First check this chunk is erased, if it needs
5612 - * checking. The checking policy (unless forced
5613 - * always on) is as follows:
5615 - * Check the first page we try to write in a block.
5616 - * If the check passes then we don't need to check any
5617 - * more. If the check fails, we check again...
5618 - * If the block has been erased, we don't need to check.
5620 - * However, if the block has been prioritised for gc,
5621 - * then we think there might be something odd about
5622 - * this block and stop using it.
5624 - * Rationale: We should only ever see chunks that have
5625 - * not been erased if there was a partially written
5626 - * chunk due to power loss. This checking policy should
5627 - * catch that case with very few checks and thus save a
5628 - * lot of checks that are most likely not needed.
5630 - if (bi->gcPrioritise) {
5631 - yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
5632 - /* try another chunk */
5635 + if (requiredTallness > file_struct->top_level) {
5636 + /* Not tall enough, gotta make the tree taller */
5637 + for (i = file_struct->top_level; i < requiredTallness; i++) {
5639 - /* let's give it a try */
5641 + tn = yaffs_get_tnode(dev);
5643 -#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
5644 - bi->skipErasedCheck = 0;
5646 - if (!bi->skipErasedCheck) {
5647 - erasedOk = yaffs_CheckChunkErased(dev, chunk);
5648 - if (erasedOk != YAFFS_OK) {
5650 + tn->internal[0] = file_struct->top;
5651 + file_struct->top = tn;
5652 + file_struct->top_level++;
5654 T(YAFFS_TRACE_ERROR,
5655 - (TSTR("**>> yaffs chunk %d was not erased"
5656 - TENDSTR), chunk));
5658 - /* try another chunk */
5660 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5663 - bi->skipErasedCheck = 1;
5667 - writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
5669 - if (writeOk != YAFFS_OK) {
5670 - yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
5671 - /* try another chunk */
5674 + /* Traverse down to level 0, adding anything we need */
5676 - /* Copy the data into the robustification buffer */
5677 - yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
5678 + l = file_struct->top_level;
5679 + tn = file_struct->top;
5681 - } while (writeOk != YAFFS_OK &&
5682 - (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
5684 + while (l > 0 && tn) {
5686 + (YAFFS_TNODES_LEVEL0_BITS +
5687 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5688 + YAFFS_TNODES_INTERNAL_MASK;
5693 - if (attempts > 1) {
5694 - T(YAFFS_TRACE_ERROR,
5695 - (TSTR("**>> yaffs write required %d attempts" TENDSTR),
5697 + if ((l > 1) && !tn->internal[x]) {
5698 + /* Add missing non-level-zero tnode */
5699 + tn->internal[x] = yaffs_get_tnode(dev);
5700 + if(!tn->internal[x])
5702 + } else if (l == 1) {
5703 + /* Looking from level 1 at level 0 */
5705 + /* If we already have one, then release it.*/
5706 + if (tn->internal[x])
5707 + yaffs_free_tnode(dev, tn->internal[x]);
5708 + tn->internal[x] = passed_tn;
5710 + } else if (!tn->internal[x]) {
5711 + /* Don't have one, none passed in */
5712 + tn->internal[x] = yaffs_get_tnode(dev);
5713 + if(!tn->internal[x])
5718 - dev->nRetriedWrites += (attempts - 1);
5719 + tn = tn->internal[x];
5723 + /* top is level 0 */
5725 + memcpy(tn, passed_tn, (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8);
5726 + yaffs_free_tnode(dev, passed_tn);
5735 - * Block retiring for handling a broken block.
5738 -static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
5739 +static int yaffs_find_chunk_in_group(yaffs_dev_t *dev, int theChunk,
5740 + yaffs_ext_tags *tags, int obj_id,
5743 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
5746 - yaffs_InvalidateCheckpoint(dev);
5747 + for (j = 0; theChunk && j < dev->chunk_grp_size; j++) {
5748 + if (yaffs_check_chunk_bit(dev, theChunk / dev->param.chunks_per_block,
5749 + theChunk % dev->param.chunks_per_block)) {
5751 + if(dev->chunk_grp_size == 1)
5754 + yaffs_rd_chunk_tags_nand(dev, theChunk, NULL,
5756 + if (yaffs_tags_match(tags, obj_id, inode_chunk)) {
5767 - if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
5768 - if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
5769 - T(YAFFS_TRACE_ALWAYS, (TSTR(
5770 - "yaffs: Failed to mark bad and erase block %d"
5771 - TENDSTR), blockInNAND));
5773 - yaffs_ExtendedTags tags;
5774 - int chunkId = blockInNAND * dev->nChunksPerBlock;
5776 +/* Experimental code not being used yet. Might speed up file deletion */
5777 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5778 + * chunks and tnodes in the file.
5779 + * Returns 1 if the tree was deleted.
5780 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5783 - __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
5784 +static int yaffs_del_worker(yaffs_obj_t *in, yaffs_tnode_t *tn, __u32 level,
5785 + int chunk_offset, int *limit)
5790 + yaffs_ext_tags tags;
5792 + yaffs_dev_t *dev = in->my_dev;
5794 - memset(buffer, 0xff, dev->nDataBytesPerChunk);
5795 - yaffs_InitialiseTags(&tags);
5796 - tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
5797 - if (dev->writeChunkWithTagsToNAND(dev, chunkId -
5798 - dev->chunkOffset, buffer, &tags) != YAFFS_OK)
5799 - T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
5800 - TCONT("write bad block marker to block %d")
5801 - TENDSTR), blockInNAND));
5804 - yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
5809 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5811 + if (tn->internal[i]) {
5812 + if (limit && (*limit) < 0) {
5816 + yaffs_del_worker(in,
5824 + YAFFS_TNODES_INTERNAL_BITS)
5829 + yaffs_free_tnode(dev,
5832 + tn->internal[i] = NULL;
5836 + return (allDone) ? 1 : 0;
5837 + } else if (level == 0) {
5840 - bi->blockState = YAFFS_BLOCK_STATE_DEAD;
5841 - bi->gcPrioritise = 0;
5842 - bi->needsRetiring = 0;
5843 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5845 + theChunk = yaffs_get_group_base(dev, tn, i);
5848 - dev->nRetiredBlocks++;
5850 + inode_chunk = (chunk_offset <<
5851 + YAFFS_TNODES_LEVEL0_BITS) + i;
5854 - * Functions for robustisizing TODO
5858 + yaffs_find_chunk_in_group(dev,
5864 -static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
5866 - const yaffs_ExtendedTags *tags)
5869 + if (foundChunk > 0) {
5870 + yaffs_chunk_del(dev,
5873 + in->n_data_chunks--;
5875 + *limit = *limit - 1;
5880 -static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
5881 - const yaffs_ExtendedTags *tags)
5886 -void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
5888 - if (!bi->gcPrioritise) {
5889 - bi->gcPrioritise = 1;
5890 - dev->hasPendingPrioritisedGCs = 1;
5891 - bi->chunkErrorStrikes++;
5892 + yaffs_load_tnode_0(dev, tn, i, 0);
5895 - if (bi->chunkErrorStrikes > 3) {
5896 - bi->needsRetiring = 1; /* Too many stikes, so retire this */
5897 - T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
5899 + return (i < 0) ? 1 : 0;
5909 -static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
5913 +static void yaffs_soft_del_chunk(yaffs_dev_t *dev, int chunk)
5915 - int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
5916 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
5917 + yaffs_block_info_t *theBlock;
5918 + unsigned block_no;
5920 - yaffs_HandleChunkError(dev, bi);
5921 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5924 - /* Was an actual write failure, so mark the block for retirement */
5925 - bi->needsRetiring = 1;
5926 - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5927 - (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
5928 + block_no = chunk / dev->param.chunks_per_block;
5929 + theBlock = yaffs_get_block_info(dev, block_no);
5931 + theBlock->soft_del_pages++;
5932 + dev->n_free_chunks++;
5933 + yaffs2_update_oldest_dirty_seq(dev, block_no, theBlock);
5936 - /* Delete the chunk */
5937 - yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
5940 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5941 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5943 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5946 -/*---------------- Name handling functions ------------*/
5948 -static __u16 yaffs_CalcNameSum(const YCHAR *name)
5949 +static int yaffs_soft_del_worker(yaffs_obj_t *in, yaffs_tnode_t *tn,
5950 + __u32 level, int chunk_offset)
5957 + yaffs_dev_t *dev = in->my_dev;
5959 - const YUCHAR *bname = (const YUCHAR *) name;
5961 - while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
5965 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5967 + if (tn->internal[i]) {
5969 + yaffs_soft_del_worker(in,
5975 + YAFFS_TNODES_INTERNAL_BITS)
5978 + yaffs_free_tnode(dev,
5981 + tn->internal[i] = NULL;
5983 + /* Hoosterman... how could this happen? */
5987 + return (allDone) ? 1 : 0;
5988 + } else if (level == 0) {
5990 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5991 + theChunk = yaffs_get_group_base(dev, tn, i);
5993 + /* Note this does not find the real chunk, only the chunk group.
5994 + * We make an assumption that a chunk group is not larger than
5997 + yaffs_soft_del_chunk(dev, theChunk);
5998 + yaffs_load_tnode_0(dev, tn, i, 0);
6004 -#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6005 - sum += yaffs_toupper(*bname) * i;
6007 - sum += (*bname) * i;
6020 -static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
6021 +static void yaffs_soft_del_file(yaffs_obj_t *obj)
6023 -#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
6024 - memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
6025 - if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
6026 - yaffs_strcpy(obj->shortName, name);
6028 - obj->shortName[0] = _Y('\0');
6030 - obj->sum = yaffs_CalcNameSum(name);
6031 + if (obj->deleted &&
6032 + obj->variant_type == YAFFS_OBJECT_TYPE_FILE && !obj->soft_del) {
6033 + if (obj->n_data_chunks <= 0) {
6034 + /* Empty file with no duplicate object headers, just delete it immediately */
6035 + yaffs_free_tnode(obj->my_dev,
6036 + obj->variant.file_variant.top);
6037 + obj->variant.file_variant.top = NULL;
6038 + T(YAFFS_TRACE_TRACING,
6039 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
6041 + yaffs_generic_obj_del(obj);
6043 + yaffs_soft_del_worker(obj,
6044 + obj->variant.file_variant.top,
6045 + obj->variant.file_variant.
6047 + obj->soft_del = 1;
6052 -/*-------------------- TNODES -------------------
6054 - * List of spare tnodes
6055 - * The list is hooked together using the first pointer
6059 -/* yaffs_CreateTnodes creates a bunch more tnodes and
6060 - * adds them to the tnode free list.
6061 - * Don't use this function directly
6062 +/* Pruning removes any part of the file structure tree that is beyond the
6063 + * bounds of the file (ie that does not point to chunks).
6065 + * A file should only get pruned when its size is reduced.
6067 + * Before pruning, the chunks must be pulled from the tree and the
6068 + * level 0 tnode entries must be zeroed out.
6069 + * Could also use this for file deletion, but that's probably better handled
6070 + * by a special case.
6072 + * This function is recursive. For levels > 0 the function is called again on
6073 + * any sub-tree. For level == 0 we just check if the sub-tree has data.
6074 + * If there is no data in a subtree then it is pruned.
6077 -static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
6078 +static yaffs_tnode_t *yaffs_prune_worker(yaffs_dev_t *dev, yaffs_tnode_t *tn,
6079 + __u32 level, int del0)
6083 - yaffs_Tnode *newTnodes;
6085 - yaffs_Tnode *curr;
6086 - yaffs_Tnode *next;
6087 - yaffs_TnodeList *tnl;
6095 - /* Calculate the tnode size in bytes for variable width tnode support.
6096 - * Must be a multiple of 32-bits */
6097 - tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
6099 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
6100 + if (tn->internal[i]) {
6102 + yaffs_prune_worker(dev, tn->internal[i],
6104 + (i == 0) ? del0 : 1);
6107 - if (tnodeSize < sizeof(yaffs_Tnode))
6108 - tnodeSize = sizeof(yaffs_Tnode);
6109 + if (tn->internal[i])
6113 + int tnode_size_u32 = dev->tnode_size/sizeof(__u32);
6114 + __u32 *map = (__u32 *)tn;
6116 - /* make these things */
6117 + for(i = 0; !hasData && i < tnode_size_u32; i++){
6123 - newTnodes = YMALLOC(nTnodes * tnodeSize);
6124 - mem = (__u8 *)newTnodes;
6125 + if (hasData == 0 && del0) {
6126 + /* Free and return NULL */
6129 - T(YAFFS_TRACE_ERROR,
6130 - (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
6131 - return YAFFS_FAIL;
6133 + yaffs_free_tnode(dev, tn);
6137 - /* Hook them into the free list */
6139 - for (i = 0; i < nTnodes - 1; i++) {
6140 - newTnodes[i].internal[0] = &newTnodes[i + 1];
6141 -#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
6142 - newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
6146 - newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
6147 -#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
6148 - newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
6150 - dev->freeTnodes = newTnodes;
6152 - /* New hookup for wide tnodes */
6153 - for (i = 0; i < nTnodes - 1; i++) {
6154 - curr = (yaffs_Tnode *) &mem[i * tnodeSize];
6155 - next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
6156 - curr->internal[0] = next;
6160 - curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
6161 - curr->internal[0] = dev->freeTnodes;
6162 - dev->freeTnodes = (yaffs_Tnode *)mem;
6166 +static int yaffs_prune_tree(yaffs_dev_t *dev,
6167 + yaffs_file_s *file_struct)
6172 + yaffs_tnode_t *tn;
6174 + if (file_struct->top_level > 0) {
6175 + file_struct->top =
6176 + yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
6178 + /* Now we have a tree with all the non-zero branches NULL but the height
6179 + * is the same as it was.
6180 + * Let's see if we can trim internal tnodes to shorten the tree.
6181 + * We can do this if only the 0th element in the tnode is in use
6182 + * (ie all the non-zero are NULL)
6185 - dev->nFreeTnodes += nTnodes;
6186 - dev->nTnodesCreated += nTnodes;
6187 + while (file_struct->top_level && !done) {
6188 + tn = file_struct->top;
6190 - /* Now add this bunch of tnodes to a list for freeing up.
6191 - * NB If we can't add this to the management list it isn't fatal
6192 - * but it just means we can't free this bunch of tnodes later.
6195 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
6196 + if (tn->internal[i])
6200 - tnl = YMALLOC(sizeof(yaffs_TnodeList));
6202 - T(YAFFS_TRACE_ERROR,
6204 - ("yaffs: Could not add tnodes to management list" TENDSTR)));
6205 - return YAFFS_FAIL;
6207 - tnl->tnodes = newTnodes;
6208 - tnl->next = dev->allocatedTnodeList;
6209 - dev->allocatedTnodeList = tnl;
6211 + file_struct->top = tn->internal[0];
6212 + file_struct->top_level--;
6213 + yaffs_free_tnode(dev, tn);
6220 - T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
6225 -/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
6226 +/*-------------------- End of File Structure functions.-------------------*/
6229 -static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
6230 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
6231 +static yaffs_obj_t *yaffs_alloc_empty_obj(yaffs_dev_t *dev)
6233 - yaffs_Tnode *tn = NULL;
6234 + yaffs_obj_t *obj = yaffs_alloc_raw_obj(dev);
6236 - /* If there are none left make more */
6237 - if (!dev->freeTnodes)
6238 - yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
6240 - if (dev->freeTnodes) {
6241 - tn = dev->freeTnodes;
6242 -#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
6243 - if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
6244 - /* Hoosterman, this thing looks like it isn't in the list */
6245 - T(YAFFS_TRACE_ALWAYS,
6246 - (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
6249 - dev->freeTnodes = dev->freeTnodes->internal[0];
6250 - dev->nFreeTnodes--;
6255 - dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
6256 + /* Now sweeten it up... */
6260 + memset(obj, 0, sizeof(yaffs_obj_t));
6261 + obj->being_created = 1;
6263 -static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
6265 - yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
6266 - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
6267 + obj->my_dev = dev;
6268 + obj->hdr_chunk = 0;
6269 + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
6270 + YINIT_LIST_HEAD(&(obj->hard_links));
6271 + YINIT_LIST_HEAD(&(obj->hash_link));
6272 + YINIT_LIST_HEAD(&obj->siblings);
6274 - if (tnodeSize < sizeof(yaffs_Tnode))
6275 - tnodeSize = sizeof(yaffs_Tnode);
6278 - memset(tn, 0, tnodeSize);
6279 + /* Now make the directory sane */
6280 + if (dev->root_dir) {
6281 + obj->parent = dev->root_dir;
6282 + ylist_add(&(obj->siblings), &dev->root_dir->variant.dir_variant.children);
6286 + /* Add it to the lost and found directory.
6287 + * NB Can't put root or lostNFound in lostNFound so
6288 + * check if lostNFound exists first
6290 + if (dev->lost_n_found)
6291 + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
6293 + obj->being_created = 0;
6296 + dev->checkpoint_blocks_required = 0; /* force recalculation*/
6301 -/* FreeTnode frees up a tnode and puts it back on the free list */
6302 -static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
6303 +static yaffs_obj_t *yaffs_create_fake_dir(yaffs_dev_t *dev, int number,
6307 -#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
6308 - if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
6309 - /* Hoosterman, this thing looks like it is already in the list */
6310 - T(YAFFS_TRACE_ALWAYS,
6311 - (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
6313 - tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
6315 - tn->internal[0] = dev->freeTnodes;
6316 - dev->freeTnodes = tn;
6317 - dev->nFreeTnodes++;
6319 + yaffs_obj_t *obj =
6320 + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
6322 + obj->fake = 1; /* it is fake so it might have no NAND presence... */
6323 + obj->rename_allowed = 0; /* ... and we're not allowed to rename it... */
6324 + obj->unlink_allowed = 0; /* ... or unlink it */
6326 + obj->unlinked = 0;
6327 + obj->yst_mode = mode;
6328 + obj->my_dev = dev;
6329 + obj->hdr_chunk = 0; /* Not a valid chunk. */
6336 +static void yaffs_unhash_obj(yaffs_obj_t *obj)
6339 + yaffs_dev_t *dev = obj->my_dev;
6341 + /* If it is still linked into the bucket list, free from the list */
6342 + if (!ylist_empty(&obj->hash_link)) {
6343 + ylist_del_init(&obj->hash_link);
6344 + bucket = yaffs_hash_fn(obj->obj_id);
6345 + dev->obj_bucket[bucket].count--;
6347 - dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
6350 -static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
6351 +/* FreeObject frees up a Object and puts it back on the free list */
6352 +static void yaffs_free_obj(yaffs_obj_t *obj)
6354 - /* Free the list of allocated tnodes */
6355 - yaffs_TnodeList *tmp;
6356 + yaffs_dev_t *dev = obj->my_dev;
6358 + T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), obj, obj->my_inode));
6360 - while (dev->allocatedTnodeList) {
6361 - tmp = dev->allocatedTnodeList->next;
6366 + if (!ylist_empty(&obj->siblings))
6369 - YFREE(dev->allocatedTnodeList->tnodes);
6370 - YFREE(dev->allocatedTnodeList);
6371 - dev->allocatedTnodeList = tmp;
6373 + if (obj->my_inode) {
6374 + /* We're still hooked up to a cached inode.
6375 + * Don't delete now, but mark for later deletion
6377 + obj->defered_free = 1;
6381 - dev->freeTnodes = NULL;
6382 - dev->nFreeTnodes = 0;
6384 + yaffs_unhash_obj(obj);
6386 -static void yaffs_InitialiseTnodes(yaffs_Device *dev)
6388 - dev->allocatedTnodeList = NULL;
6389 - dev->freeTnodes = NULL;
6390 - dev->nFreeTnodes = 0;
6391 - dev->nTnodesCreated = 0;
6392 + yaffs_free_raw_obj(dev,obj);
6394 + dev->checkpoint_blocks_required = 0; /* force recalculation*/
6398 -void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
6400 +void yaffs_handle_defered_free(yaffs_obj_t *obj)
6402 - __u32 *map = (__u32 *)tn;
6408 - pos &= YAFFS_TNODES_LEVEL0_MASK;
6409 - val >>= dev->chunkGroupBits;
6410 + if (obj->defered_free)
6411 + yaffs_free_obj(obj);
6414 - bitInMap = pos * dev->tnodeWidth;
6415 - wordInMap = bitInMap / 32;
6416 - bitInWord = bitInMap & (32 - 1);
6417 +static void yaffs_init_tnodes_and_objs(yaffs_dev_t *dev)
6421 - mask = dev->tnodeMask << bitInWord;
6423 + dev->n_tnodes = 0;
6425 - map[wordInMap] &= ~mask;
6426 - map[wordInMap] |= (mask & (val << bitInWord));
6427 + yaffs_init_raw_tnodes_and_objs(dev);
6429 - if (dev->tnodeWidth > (32 - bitInWord)) {
6430 - bitInWord = (32 - bitInWord);
6432 - mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
6433 - map[wordInMap] &= ~mask;
6434 - map[wordInMap] |= (mask & (val >> bitInWord));
6435 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
6436 + YINIT_LIST_HEAD(&dev->obj_bucket[i].list);
6437 + dev->obj_bucket[i].count = 0;
6441 -static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
6443 +static int yaffs_find_nice_bucket(yaffs_dev_t *dev)
6445 - __u32 *map = (__u32 *)tn;
6452 + int lowest = 999999;
6454 - pos &= YAFFS_TNODES_LEVEL0_MASK;
6456 - bitInMap = pos * dev->tnodeWidth;
6457 - wordInMap = bitInMap / 32;
6458 - bitInWord = bitInMap & (32 - 1);
6459 + /* Search for the shortest list or one that
6463 - val = map[wordInMap] >> bitInWord;
6464 + for (i = 0; i < 10 && lowest > 4; i++) {
6465 + dev->bucket_finder++;
6466 + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
6467 + if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
6468 + lowest = dev->obj_bucket[dev->bucket_finder].count;
6469 + l = dev->bucket_finder;
6472 - if (dev->tnodeWidth > (32 - bitInWord)) {
6473 - bitInWord = (32 - bitInWord);
6475 - val |= (map[wordInMap] << bitInWord);
6478 - val &= dev->tnodeMask;
6479 - val <<= dev->chunkGroupBits;
6485 -/* ------------------- End of individual tnode manipulation -----------------*/
6487 -/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
6488 - * The look up tree is represented by the top tnode and the number of topLevel
6489 - * in the tree. 0 means only the level 0 tnode is in the tree.
6492 -/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
6493 -static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
6494 - yaffs_FileStructure *fStruct,
6496 +static int yaffs_new_obj_id(yaffs_dev_t *dev)
6498 - yaffs_Tnode *tn = fStruct->top;
6500 - int requiredTallness;
6501 - int level = fStruct->topLevel;
6503 - /* Check sane level and chunk Id */
6504 - if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
6506 + int bucket = yaffs_find_nice_bucket(dev);
6508 - if (chunkId > YAFFS_MAX_CHUNK_ID)
6510 + /* Now find an object value that has not already been taken
6511 + * by scanning the list.
6514 - /* First check we're tall enough (ie enough topLevel) */
6516 + struct ylist_head *i;
6518 - i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
6519 - requiredTallness = 0;
6521 - i >>= YAFFS_TNODES_INTERNAL_BITS;
6522 - requiredTallness++;
6524 + __u32 n = (__u32) bucket;
6526 - if (requiredTallness > fStruct->topLevel)
6527 - return NULL; /* Not tall enough, so we can't find it */
6528 + /* yaffs_check_obj_hash_sane(); */
6530 - /* Traverse down to level 0 */
6531 - while (level > 0 && tn) {
6532 - tn = tn->internal[(chunkId >>
6533 - (YAFFS_TNODES_LEVEL0_BITS +
6535 - YAFFS_TNODES_INTERNAL_BITS)) &
6536 - YAFFS_TNODES_INTERNAL_MASK];
6540 + n += YAFFS_NOBJECT_BUCKETS;
6541 + if (1 || dev->obj_bucket[bucket].count > 0) {
6542 + ylist_for_each(i, &dev->obj_bucket[bucket].list) {
6543 + /* If there is already one in the list */
6544 + if (i && ylist_entry(i, yaffs_obj_t,
6545 + hash_link)->obj_id == n) {
6556 -/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
6557 - * This happens in two steps:
6558 - * 1. If the tree isn't tall enough, then make it taller.
6559 - * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
6561 - * Used when modifying the tree.
6563 - * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
6564 - * be plugged into the ttree.
6567 -static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
6568 - yaffs_FileStructure *fStruct,
6570 - yaffs_Tnode *passedTn)
6571 +static void yaffs_hash_obj(yaffs_obj_t *in)
6573 - int requiredTallness;
6579 + int bucket = yaffs_hash_fn(in->obj_id);
6580 + yaffs_dev_t *dev = in->my_dev;
6582 + ylist_add(&in->hash_link, &dev->obj_bucket[bucket].list);
6583 + dev->obj_bucket[bucket].count++;
6586 - /* Check sane level and page Id */
6587 - if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
6589 +yaffs_obj_t *yaffs_find_by_number(yaffs_dev_t *dev, __u32 number)
6591 + int bucket = yaffs_hash_fn(number);
6592 + struct ylist_head *i;
6595 - if (chunkId > YAFFS_MAX_CHUNK_ID)
6597 + ylist_for_each(i, &dev->obj_bucket[bucket].list) {
6598 + /* Look if it is in the list */
6600 + in = ylist_entry(i, yaffs_obj_t, hash_link);
6601 + if (in->obj_id == number) {
6603 - /* First check we're tall enough (ie enough topLevel) */
6604 + /* Don't tell the VFS about this one if it is defered free */
6605 + if (in->defered_free)
6608 - x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
6609 - requiredTallness = 0;
6611 - x >>= YAFFS_TNODES_INTERNAL_BITS;
6612 - requiredTallness++;
6621 - if (requiredTallness > fStruct->topLevel) {
6622 - /* Not tall enough, gotta make the tree taller */
6623 - for (i = fStruct->topLevel; i < requiredTallness; i++) {
6624 +yaffs_obj_t *yaffs_new_obj(yaffs_dev_t *dev, int number,
6625 + yaffs_obj_type type)
6627 + yaffs_obj_t *theObject=NULL;
6628 + yaffs_tnode_t *tn = NULL;
6630 - tn = yaffs_GetTnode(dev);
6632 + number = yaffs_new_obj_id(dev);
6635 - tn->internal[0] = fStruct->top;
6636 - fStruct->top = tn;
6638 - T(YAFFS_TRACE_ERROR,
6639 - (TSTR("yaffs: no more tnodes" TENDSTR)));
6642 + if (type == YAFFS_OBJECT_TYPE_FILE) {
6643 + tn = yaffs_get_tnode(dev);
6648 - fStruct->topLevel = requiredTallness;
6649 + theObject = yaffs_alloc_empty_obj(dev);
6652 + yaffs_free_tnode(dev,tn);
6656 - /* Traverse down to level 0, adding anything we need */
6658 - l = fStruct->topLevel;
6659 - tn = fStruct->top;
6661 + theObject->fake = 0;
6662 + theObject->rename_allowed = 1;
6663 + theObject->unlink_allowed = 1;
6664 + theObject->obj_id = number;
6665 + yaffs_hash_obj(theObject);
6666 + theObject->variant_type = type;
6667 +#ifdef CONFIG_YAFFS_WINCE
6668 + yfsd_win_file_time_now(theObject->win_atime);
6669 + theObject->win_ctime[0] = theObject->win_mtime[0] =
6670 + theObject->win_atime[0];
6671 + theObject->win_ctime[1] = theObject->win_mtime[1] =
6672 + theObject->win_atime[1];
6677 - while (l > 0 && tn) {
6679 - (YAFFS_TNODES_LEVEL0_BITS +
6680 - (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
6681 - YAFFS_TNODES_INTERNAL_MASK;
6682 + theObject->yst_atime = theObject->yst_mtime =
6683 + theObject->yst_ctime = Y_CURRENT_TIME;
6686 + case YAFFS_OBJECT_TYPE_FILE:
6687 + theObject->variant.file_variant.file_size = 0;
6688 + theObject->variant.file_variant.scanned_size = 0;
6689 + theObject->variant.file_variant.shrink_size = 0xFFFFFFFF; /* max __u32 */
6690 + theObject->variant.file_variant.top_level = 0;
6691 + theObject->variant.file_variant.top = tn;
6693 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6694 + YINIT_LIST_HEAD(&theObject->variant.dir_variant.
6696 + YINIT_LIST_HEAD(&theObject->variant.dir_variant.
6699 + case YAFFS_OBJECT_TYPE_SYMLINK:
6700 + case YAFFS_OBJECT_TYPE_HARDLINK:
6701 + case YAFFS_OBJECT_TYPE_SPECIAL:
6702 + /* No action required */
6704 + case YAFFS_OBJECT_TYPE_UNKNOWN:
6705 + /* todo this should not happen */
6713 - if ((l > 1) && !tn->internal[x]) {
6714 - /* Add missing non-level-zero tnode */
6715 - tn->internal[x] = yaffs_GetTnode(dev);
6716 +yaffs_obj_t *yaffs_find_or_create_by_number(yaffs_dev_t *dev,
6718 + yaffs_obj_type type)
6720 + yaffs_obj_t *theObject = NULL;
6722 - } else if (l == 1) {
6723 - /* Looking from level 1 at level 0 */
6725 - /* If we already have one, then release it.*/
6726 - if (tn->internal[x])
6727 - yaffs_FreeTnode(dev, tn->internal[x]);
6728 - tn->internal[x] = passedTn;
6730 + theObject = yaffs_find_by_number(dev, number);
6732 - } else if (!tn->internal[x]) {
6733 - /* Don't have one, none passed in */
6734 - tn->internal[x] = yaffs_GetTnode(dev);
6738 + theObject = yaffs_new_obj(dev, number, type);
6740 - tn = tn->internal[x];
6744 - /* top is level 0 */
6746 - memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
6747 - yaffs_FreeTnode(dev, passedTn);
6755 -static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
6756 - yaffs_ExtendedTags *tags, int objectId,
6759 +YCHAR *yaffs_clone_str(const YCHAR *str)
6762 + YCHAR *newStr = NULL;
6765 - for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
6766 - if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock,
6767 - theChunk % dev->nChunksPerBlock)) {
6768 - yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
6770 - if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
6779 + len = yaffs_strnlen(str,YAFFS_MAX_ALIAS_LENGTH);
6780 + newStr = YMALLOC((len + 1) * sizeof(YCHAR));
6782 + yaffs_strncpy(newStr, str,len);
6791 -/* DeleteWorker scans backwards through the tnode tree and deletes all the
6792 - * chunks and tnodes in the file
6793 - * Returns 1 if the tree was deleted.
6794 - * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
6796 + * Mknod (create) a new object.
6797 + * equiv_obj only has meaning for a hard link;
6798 + * aliasString only has meaning for a symlink.
6799 + * rdev only has meaning for devices (a subset of special objects)
6802 -static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
6803 - int chunkOffset, int *limit)
6804 +static yaffs_obj_t *yaffs_create_obj(yaffs_obj_type type,
6805 + yaffs_obj_t *parent,
6806 + const YCHAR *name,
6810 + yaffs_obj_t *equiv_obj,
6811 + const YCHAR *aliasString, __u32 rdev)
6816 - yaffs_ExtendedTags tags;
6818 - yaffs_Device *dev = in->myDev;
6824 - for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
6826 - if (tn->internal[i]) {
6827 - if (limit && (*limit) < 0) {
6831 - yaffs_DeleteWorker(in,
6839 - YAFFS_TNODES_INTERNAL_BITS)
6844 - yaffs_FreeTnode(dev,
6847 - tn->internal[i] = NULL;
6851 - return (allDone) ? 1 : 0;
6852 - } else if (level == 0) {
6855 - for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
6857 - theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
6860 - chunkInInode = (chunkOffset <<
6861 - YAFFS_TNODES_LEVEL0_BITS) + i;
6864 - yaffs_FindChunkInGroup(dev,
6870 - if (foundChunk > 0) {
6871 - yaffs_DeleteChunk(dev,
6874 - in->nDataChunks--;
6876 - *limit = *limit - 1;
6883 - yaffs_PutLevel0Tnode(dev, tn, i, 0);
6887 - return (i < 0) ? 1 : 0;
6897 -static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
6899 - yaffs_BlockInfo *theBlock;
6901 - T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
6903 - theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
6905 - theBlock->softDeletions++;
6906 - dev->nFreeChunks++;
6910 -/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
6911 - * All soft deleting does is increment the block's softdelete count and pulls the chunk out
6913 - * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
6916 -static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
6917 - __u32 level, int chunkOffset)
6922 - yaffs_Device *dev = in->myDev;
6927 - for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
6929 - if (tn->internal[i]) {
6931 - yaffs_SoftDeleteWorker(in,
6937 - YAFFS_TNODES_INTERNAL_BITS)
6940 - yaffs_FreeTnode(dev,
6943 - tn->internal[i] = NULL;
6945 - /* Hoosterman... how could this happen? */
6949 - return (allDone) ? 1 : 0;
6950 - } else if (level == 0) {
6952 - for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
6953 - theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
6955 - /* Note this does not find the real chunk, only the chunk group.
6956 - * We make an assumption that a chunk group is not larger than
6959 - yaffs_SoftDeleteChunk(dev, theChunk);
6960 - yaffs_PutLevel0Tnode(dev, tn, i, 0);
6974 -static void yaffs_SoftDeleteFile(yaffs_Object *obj)
6976 - if (obj->deleted &&
6977 - obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
6978 - if (obj->nDataChunks <= 0) {
6979 - /* Empty file with no duplicate object headers, just delete it immediately */
6980 - yaffs_FreeTnode(obj->myDev,
6981 - obj->variant.fileVariant.top);
6982 - obj->variant.fileVariant.top = NULL;
6983 - T(YAFFS_TRACE_TRACING,
6984 - (TSTR("yaffs: Deleting empty file %d" TENDSTR),
6986 - yaffs_DoGenericObjectDeletion(obj);
6988 - yaffs_SoftDeleteWorker(obj,
6989 - obj->variant.fileVariant.top,
6990 - obj->variant.fileVariant.
6992 - obj->softDeleted = 1;
6997 -/* Pruning removes any part of the file structure tree that is beyond the
6998 - * bounds of the file (ie that does not point to chunks).
7000 - * A file should only get pruned when its size is reduced.
7002 - * Before pruning, the chunks must be pulled from the tree and the
7003 - * level 0 tnode entries must be zeroed out.
7004 - * Could also use this for file deletion, but that's probably better handled
7005 - * by a special case.
7008 -static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
7009 - __u32 level, int del0)
7017 - for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
7018 - if (tn->internal[i] && level > 0) {
7020 - yaffs_PruneWorker(dev, tn->internal[i],
7022 - (i == 0) ? del0 : 1);
7025 - if (tn->internal[i])
7029 - if (hasData == 0 && del0) {
7030 - /* Free and return NULL */
7032 - yaffs_FreeTnode(dev, tn);
7042 -static int yaffs_PruneFileStructure(yaffs_Device *dev,
7043 - yaffs_FileStructure *fStruct)
7050 - if (fStruct->topLevel > 0) {
7052 - yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
7054 - /* Now we have a tree with all the non-zero branches NULL but the height
7055 - * is the same as it was.
7056 - * Let's see if we can trim internal tnodes to shorten the tree.
7057 - * We can do this if only the 0th element in the tnode is in use
7058 - * (ie all the non-zero are NULL)
7061 - while (fStruct->topLevel && !done) {
7062 - tn = fStruct->top;
7065 - for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
7066 - if (tn->internal[i])
7071 - fStruct->top = tn->internal[0];
7072 - fStruct->topLevel--;
7073 - yaffs_FreeTnode(dev, tn);
7083 -/*-------------------- End of File Structure functions.-------------------*/
7085 -/* yaffs_CreateFreeObjects creates a bunch more objects and
7086 - * adds them to the object free list.
7088 -static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
7091 - yaffs_Object *newObjects;
7092 - yaffs_ObjectList *list;
7097 - /* make these things */
7098 - newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
7099 - list = YMALLOC(sizeof(yaffs_ObjectList));
7101 - if (!newObjects || !list) {
7103 - YFREE(newObjects);
7106 - T(YAFFS_TRACE_ALLOCATE,
7107 - (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
7108 - return YAFFS_FAIL;
7111 - /* Hook them into the free list */
7112 - for (i = 0; i < nObjects - 1; i++) {
7113 - newObjects[i].siblings.next =
7114 - (struct ylist_head *)(&newObjects[i + 1]);
7117 - newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
7118 - dev->freeObjects = newObjects;
7119 - dev->nFreeObjects += nObjects;
7120 - dev->nObjectsCreated += nObjects;
7122 - /* Now add this bunch of Objects to a list for freeing up. */
7124 - list->objects = newObjects;
7125 - list->next = dev->allocatedObjectList;
7126 - dev->allocatedObjectList = list;
7132 -/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
7133 -static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
7135 - yaffs_Object *tn = NULL;
7137 -#ifdef VALGRIND_TEST
7138 - tn = YMALLOC(sizeof(yaffs_Object));
7140 - /* If there are none left make more */
7141 - if (!dev->freeObjects)
7142 - yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
7144 - if (dev->freeObjects) {
7145 - tn = dev->freeObjects;
7146 - dev->freeObjects =
7147 - (yaffs_Object *) (dev->freeObjects->siblings.next);
7148 - dev->nFreeObjects--;
7152 - /* Now sweeten it up... */
7154 - memset(tn, 0, sizeof(yaffs_Object));
7155 - tn->beingCreated = 1;
7159 - tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
7160 - YINIT_LIST_HEAD(&(tn->hardLinks));
7161 - YINIT_LIST_HEAD(&(tn->hashLink));
7162 - YINIT_LIST_HEAD(&tn->siblings);
7165 - /* Now make the directory sane */
7166 - if (dev->rootDir) {
7167 - tn->parent = dev->rootDir;
7168 - ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
7171 - /* Add it to the lost and found directory.
7172 - * NB Can't put root or lostNFound in lostNFound so
7173 - * check if lostNFound exists first
7175 - if (dev->lostNFoundDir)
7176 - yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
7178 - tn->beingCreated = 0;
7181 - dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
7186 -static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
7190 - yaffs_Object *obj =
7191 - yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
7193 - obj->fake = 1; /* it is fake so it might have no NAND presence... */
7194 - obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
7195 - obj->unlinkAllowed = 0; /* ... or unlink it */
7197 - obj->unlinked = 0;
7198 - obj->yst_mode = mode;
7200 - obj->hdrChunk = 0; /* Not a valid chunk. */
7207 -static void yaffs_UnhashObject(yaffs_Object *tn)
7210 - yaffs_Device *dev = tn->myDev;
7212 - /* If it is still linked into the bucket list, free from the list */
7213 - if (!ylist_empty(&tn->hashLink)) {
7214 - ylist_del_init(&tn->hashLink);
7215 - bucket = yaffs_HashFunction(tn->objectId);
7216 - dev->objectBucket[bucket].count--;
7220 -/* FreeObject frees up a Object and puts it back on the free list */
7221 -static void yaffs_FreeObject(yaffs_Object *tn)
7223 - yaffs_Device *dev = tn->myDev;
7226 - T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
7231 - if (!ylist_empty(&tn->siblings))
7236 - if (tn->myInode) {
7237 - /* We're still hooked up to a cached inode.
7238 - * Don't delete now, but mark for later deletion
7240 - tn->deferedFree = 1;
7245 - yaffs_UnhashObject(tn);
7247 -#ifdef VALGRIND_TEST
7250 - /* Link into the free list. */
7251 - tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
7252 - dev->freeObjects = tn;
7253 - dev->nFreeObjects++;
7255 - dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
7260 -void yaffs_HandleDeferedFree(yaffs_Object *obj)
7262 - if (obj->deferedFree)
7263 - yaffs_FreeObject(obj);
7268 -static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
7270 - /* Free the list of allocated Objects */
7272 - yaffs_ObjectList *tmp;
7274 - while (dev->allocatedObjectList) {
7275 - tmp = dev->allocatedObjectList->next;
7276 - YFREE(dev->allocatedObjectList->objects);
7277 - YFREE(dev->allocatedObjectList);
7279 - dev->allocatedObjectList = tmp;
7282 - dev->freeObjects = NULL;
7283 - dev->nFreeObjects = 0;
7286 -static void yaffs_InitialiseObjects(yaffs_Device *dev)
7290 - dev->allocatedObjectList = NULL;
7291 - dev->freeObjects = NULL;
7292 - dev->nFreeObjects = 0;
7294 - for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
7295 - YINIT_LIST_HEAD(&dev->objectBucket[i].list);
7296 - dev->objectBucket[i].count = 0;
7300 -static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
7305 - int lowest = 999999;
7307 - /* First let's see if we can find one that's empty. */
7309 - for (i = 0; i < 10 && lowest > 0; i++) {
7311 - x %= YAFFS_NOBJECT_BUCKETS;
7312 - if (dev->objectBucket[x].count < lowest) {
7313 - lowest = dev->objectBucket[x].count;
7319 - /* If we didn't find an empty list, then try
7320 - * looking a bit further for a short one
7323 - for (i = 0; i < 10 && lowest > 3; i++) {
7325 - x %= YAFFS_NOBJECT_BUCKETS;
7326 - if (dev->objectBucket[x].count < lowest) {
7327 - lowest = dev->objectBucket[x].count;
7336 -static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
7338 - int bucket = yaffs_FindNiceObjectBucket(dev);
7340 - /* Now find an object value that has not already been taken
7341 - * by scanning the list.
7345 - struct ylist_head *i;
7347 - __u32 n = (__u32) bucket;
7349 - /* yaffs_CheckObjectHashSanity(); */
7353 - n += YAFFS_NOBJECT_BUCKETS;
7354 - if (1 || dev->objectBucket[bucket].count > 0) {
7355 - ylist_for_each(i, &dev->objectBucket[bucket].list) {
7356 - /* If there is already one in the list */
7357 - if (i && ylist_entry(i, yaffs_Object,
7358 - hashLink)->objectId == n) {
7368 -static void yaffs_HashObject(yaffs_Object *in)
7370 - int bucket = yaffs_HashFunction(in->objectId);
7371 - yaffs_Device *dev = in->myDev;
7373 - ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
7374 - dev->objectBucket[bucket].count++;
7377 -yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
7379 - int bucket = yaffs_HashFunction(number);
7380 - struct ylist_head *i;
7383 - ylist_for_each(i, &dev->objectBucket[bucket].list) {
7384 - /* Look if it is in the list */
7386 - in = ylist_entry(i, yaffs_Object, hashLink);
7387 - if (in->objectId == number) {
7389 - /* Don't tell the VFS about this one if it is defered free */
7390 - if (in->deferedFree)
7402 -yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
7403 - yaffs_ObjectType type)
7405 - yaffs_Object *theObject;
7406 - yaffs_Tnode *tn = NULL;
7409 - number = yaffs_CreateNewObjectNumber(dev);
7411 - theObject = yaffs_AllocateEmptyObject(dev);
7415 - if (type == YAFFS_OBJECT_TYPE_FILE) {
7416 - tn = yaffs_GetTnode(dev);
7418 - yaffs_FreeObject(theObject);
7424 - theObject->fake = 0;
7425 - theObject->renameAllowed = 1;
7426 - theObject->unlinkAllowed = 1;
7427 - theObject->objectId = number;
7428 - yaffs_HashObject(theObject);
7429 - theObject->variantType = type;
7430 -#ifdef CONFIG_YAFFS_WINCE
7431 - yfsd_WinFileTimeNow(theObject->win_atime);
7432 - theObject->win_ctime[0] = theObject->win_mtime[0] =
7433 - theObject->win_atime[0];
7434 - theObject->win_ctime[1] = theObject->win_mtime[1] =
7435 - theObject->win_atime[1];
7439 - theObject->yst_atime = theObject->yst_mtime =
7440 - theObject->yst_ctime = Y_CURRENT_TIME;
7443 - case YAFFS_OBJECT_TYPE_FILE:
7444 - theObject->variant.fileVariant.fileSize = 0;
7445 - theObject->variant.fileVariant.scannedFileSize = 0;
7446 - theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
7447 - theObject->variant.fileVariant.topLevel = 0;
7448 - theObject->variant.fileVariant.top = tn;
7450 - case YAFFS_OBJECT_TYPE_DIRECTORY:
7451 - YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
7454 - case YAFFS_OBJECT_TYPE_SYMLINK:
7455 - case YAFFS_OBJECT_TYPE_HARDLINK:
7456 - case YAFFS_OBJECT_TYPE_SPECIAL:
7457 - /* No action required */
7459 - case YAFFS_OBJECT_TYPE_UNKNOWN:
7460 - /* todo this should not happen */
7468 -static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
7470 - yaffs_ObjectType type)
7472 - yaffs_Object *theObject = NULL;
7475 - theObject = yaffs_FindObjectByNumber(dev, number);
7478 - theObject = yaffs_CreateNewObject(dev, number, type);
7485 -static YCHAR *yaffs_CloneString(const YCHAR *str)
7487 - YCHAR *newStr = NULL;
7489 - if (str && *str) {
7490 - newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
7492 - yaffs_strcpy(newStr, str);
7500 - * Mknod (create) a new object.
7501 - * equivalentObject only has meaning for a hard link;
7502 - * aliasString only has meaning for a sumlink.
7503 - * rdev only has meaning for devices (a subset of special objects)
7506 -static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
7507 - yaffs_Object *parent,
7508 - const YCHAR *name,
7512 - yaffs_Object *equivalentObject,
7513 - const YCHAR *aliasString, __u32 rdev)
7516 - YCHAR *str = NULL;
7518 - yaffs_Device *dev = parent->myDev;
7520 - /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
7521 - if (yaffs_FindObjectByName(parent, name))
7524 - in = yaffs_CreateNewObject(dev, -1, type);
7527 - return YAFFS_FAIL;
7529 - if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
7530 - str = yaffs_CloneString(aliasString);
7532 - yaffs_FreeObject(in);
7542 - in->variantType = type;
7544 - in->yst_mode = mode;
7546 -#ifdef CONFIG_YAFFS_WINCE
7547 - yfsd_WinFileTimeNow(in->win_atime);
7548 - in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
7549 - in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
7552 - in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
7554 - in->yst_rdev = rdev;
7555 - in->yst_uid = uid;
7556 - in->yst_gid = gid;
7558 - in->nDataChunks = 0;
7560 - yaffs_SetObjectName(in, name);
7563 - yaffs_AddObjectToDirectory(parent, in);
7565 - in->myDev = parent->myDev;
7568 - case YAFFS_OBJECT_TYPE_SYMLINK:
7569 - in->variant.symLinkVariant.alias = str;
7571 - case YAFFS_OBJECT_TYPE_HARDLINK:
7572 - in->variant.hardLinkVariant.equivalentObject =
7574 - in->variant.hardLinkVariant.equivalentObjectId =
7575 - equivalentObject->objectId;
7576 - ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
7578 - case YAFFS_OBJECT_TYPE_FILE:
7579 - case YAFFS_OBJECT_TYPE_DIRECTORY:
7580 - case YAFFS_OBJECT_TYPE_SPECIAL:
7581 - case YAFFS_OBJECT_TYPE_UNKNOWN:
7586 - if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
7587 - /* Could not create the object header, fail the creation */
7588 - yaffs_DeleteObject(in);
7597 -yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
7598 - __u32 mode, __u32 uid, __u32 gid)
7600 - return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
7601 - uid, gid, NULL, NULL, 0);
7604 -yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
7605 - __u32 mode, __u32 uid, __u32 gid)
7607 - return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
7608 - mode, uid, gid, NULL, NULL, 0);
7611 -yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
7612 - __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
7614 - return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
7615 - uid, gid, NULL, NULL, rdev);
7618 -yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
7619 - __u32 mode, __u32 uid, __u32 gid,
7620 - const YCHAR *alias)
7622 - return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
7623 - uid, gid, NULL, alias, 0);
7626 -/* yaffs_Link returns the object id of the equivalent object.*/
7627 -yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
7628 - yaffs_Object *equivalentObject)
7630 - /* Get the real object in case we were fed a hard link as an equivalent object */
7631 - equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
7633 - if (yaffs_MknodObject
7634 - (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
7635 - equivalentObject, NULL, 0)) {
7636 - return equivalentObject;
7643 -static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
7644 - const YCHAR *newName, int force, int shadows)
7649 - yaffs_Object *existingTarget;
7651 - if (newDir == NULL)
7652 - newDir = obj->parent; /* use the old directory */
7654 - if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
7655 - T(YAFFS_TRACE_ALWAYS,
7657 - ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
7662 - /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
7663 - if (obj->myDev->isYaffs2)
7664 - unlinkOp = (newDir == obj->myDev->unlinkedDir);
7666 - unlinkOp = (newDir == obj->myDev->unlinkedDir
7667 - && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
7669 - deleteOp = (newDir == obj->myDev->deletedDir);
7671 - existingTarget = yaffs_FindObjectByName(newDir, newName);
7673 - /* If the object is a file going into the unlinked directory,
7674 - * then it is OK to just stuff it in since duplicate names are allowed.
7675 - * else only proceed if the new name does not exist and if we're putting
7676 - * it into a directory.
7682 - !existingTarget) &&
7683 - newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
7684 - yaffs_SetObjectName(obj, newName);
7687 - yaffs_AddObjectToDirectory(newDir, obj);
7690 - obj->unlinked = 1;
7692 - /* If it is a deletion then we mark it as a shrink for gc purposes. */
7693 - if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
7697 - return YAFFS_FAIL;
7700 -int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
7701 - yaffs_Object *newDir, const YCHAR *newName)
7703 - yaffs_Object *obj = NULL;
7704 - yaffs_Object *existingTarget = NULL;
7708 - if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
7710 - if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
7713 -#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
7714 - /* Special case for case insemsitive systems (eg. WinCE).
7715 - * While look-up is case insensitive, the name isn't.
7716 - * Therefore we might want to change x.txt to X.txt
7718 - if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
7722 - else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
7723 - /* ENAMETOOLONG */
7724 - return YAFFS_FAIL;
7726 - obj = yaffs_FindObjectByName(oldDir, oldName);
7728 - if (obj && obj->renameAllowed) {
7730 - /* Now do the handling for an existing target, if there is one */
7732 - existingTarget = yaffs_FindObjectByName(newDir, newName);
7733 - if (existingTarget &&
7734 - existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
7735 - !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
7736 - /* There is a target that is a non-empty directory, so we fail */
7737 - return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
7738 - } else if (existingTarget && existingTarget != obj) {
7739 - /* Nuke the target first, using shadowing,
7740 - * but only if it isn't the same object
7742 - yaffs_ChangeObjectName(obj, newDir, newName, force,
7743 - existingTarget->objectId);
7744 - yaffs_UnlinkObject(existingTarget);
7747 - return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
7749 - return YAFFS_FAIL;
7752 -/*------------------------- Block Management and Page Allocation ----------------*/
7754 -static int yaffs_InitialiseBlocks(yaffs_Device *dev)
7756 - int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
7758 - dev->blockInfo = NULL;
7759 - dev->chunkBits = NULL;
7761 - dev->allocationBlock = -1; /* force it to get a new one */
7763 - /* If the first allocation strategy fails, thry the alternate one */
7764 - dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
7765 - if (!dev->blockInfo) {
7766 - dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
7767 - dev->blockInfoAlt = 1;
7769 - dev->blockInfoAlt = 0;
7771 - if (dev->blockInfo) {
7772 - /* Set up dynamic blockinfo stuff. */
7773 - dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
7774 - dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
7775 - if (!dev->chunkBits) {
7776 - dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
7777 - dev->chunkBitsAlt = 1;
7779 - dev->chunkBitsAlt = 0;
7782 - if (dev->blockInfo && dev->chunkBits) {
7783 - memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
7784 - memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
7788 - return YAFFS_FAIL;
7791 -static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
7793 - if (dev->blockInfoAlt && dev->blockInfo)
7794 - YFREE_ALT(dev->blockInfo);
7795 - else if (dev->blockInfo)
7796 - YFREE(dev->blockInfo);
7798 - dev->blockInfoAlt = 0;
7800 - dev->blockInfo = NULL;
7802 - if (dev->chunkBitsAlt && dev->chunkBits)
7803 - YFREE_ALT(dev->chunkBits);
7804 - else if (dev->chunkBits)
7805 - YFREE(dev->chunkBits);
7806 - dev->chunkBitsAlt = 0;
7807 - dev->chunkBits = NULL;
7810 -static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
7811 - yaffs_BlockInfo *bi)
7815 - yaffs_BlockInfo *b;
7817 - if (!dev->isYaffs2)
7818 - return 1; /* disqualification only applies to yaffs2. */
7820 - if (!bi->hasShrinkHeader)
7821 - return 1; /* can gc */
7823 - /* Find the oldest dirty sequence number if we don't know it and save it
7824 - * so we don't have to keep recomputing it.
7826 - if (!dev->oldestDirtySequence) {
7827 - seq = dev->sequenceNumber;
7829 - for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
7831 - b = yaffs_GetBlockInfo(dev, i);
7832 - if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
7833 - (b->pagesInUse - b->softDeletions) <
7834 - dev->nChunksPerBlock && b->sequenceNumber < seq) {
7835 - seq = b->sequenceNumber;
7838 - dev->oldestDirtySequence = seq;
7841 - /* Can't do gc of this block if there are any blocks older than this one that have
7842 - * discarded pages.
7844 - return (bi->sequenceNumber <= dev->oldestDirtySequence);
7847 -/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
7848 - * for garbage collection.
7851 -static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
7854 - int b = dev->currentDirtyChecker;
7858 - int dirtiest = -1;
7859 - int pagesInUse = 0;
7860 - int prioritised = 0;
7861 - yaffs_BlockInfo *bi;
7862 - int pendingPrioritisedExist = 0;
7864 - /* First let's see if we need to grab a prioritised block */
7865 - if (dev->hasPendingPrioritisedGCs) {
7866 - for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
7868 - bi = yaffs_GetBlockInfo(dev, i);
7869 - /* yaffs_VerifyBlock(dev,bi,i); */
7871 - if (bi->gcPrioritise) {
7872 - pendingPrioritisedExist = 1;
7873 - if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
7874 - yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
7875 - pagesInUse = (bi->pagesInUse - bi->softDeletions);
7878 - aggressive = 1; /* Fool the non-aggressive skip logiv below */
7883 - if (!pendingPrioritisedExist) /* None found, so we can clear this */
7884 - dev->hasPendingPrioritisedGCs = 0;
7887 - /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
7889 - * else (we're doing a leasurely gc), then we only bother to do this if the
7890 - * block has only a few pages in use.
7893 - dev->nonAggressiveSkip--;
7895 - if (!aggressive && (dev->nonAggressiveSkip > 0))
7900 - (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
7904 - dev->internalEndBlock - dev->internalStartBlock + 1;
7907 - dev->internalEndBlock - dev->internalStartBlock + 1;
7908 - iterations = iterations / 16;
7909 - if (iterations > 200)
7913 - for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
7915 - if (b < dev->internalStartBlock || b > dev->internalEndBlock)
7916 - b = dev->internalStartBlock;
7918 - if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
7919 - T(YAFFS_TRACE_ERROR,
7920 - (TSTR("**>> Block %d is not valid" TENDSTR), b));
7924 - bi = yaffs_GetBlockInfo(dev, b);
7926 - if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
7927 - (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
7928 - yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
7930 - pagesInUse = (bi->pagesInUse - bi->softDeletions);
7934 - dev->currentDirtyChecker = b;
7936 - if (dirtiest > 0) {
7938 - (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
7939 - dev->nChunksPerBlock - pagesInUse, prioritised));
7942 - dev->oldestDirtySequence = 0;
7945 - dev->nonAggressiveSkip = 4;
7950 -static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
7952 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
7956 - /* If the block is still healthy erase it and mark as clean.
7957 - * If the block has had a data failure, then retire it.
7960 - T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
7961 - (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
7962 - blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
7964 - bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
7966 - if (!bi->needsRetiring) {
7967 - yaffs_InvalidateCheckpoint(dev);
7968 - erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
7970 - dev->nErasureFailures++;
7971 - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
7972 - (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
7977 - ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
7979 - for (i = 0; i < dev->nChunksPerBlock; i++) {
7980 - if (!yaffs_CheckChunkErased
7981 - (dev, blockNo * dev->nChunksPerBlock + i)) {
7982 - T(YAFFS_TRACE_ERROR,
7984 - (">>Block %d erasure supposedly OK, but chunk %d not erased"
7985 - TENDSTR), blockNo, i));
7991 - /* Clean it up... */
7992 - bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
7993 - dev->nErasedBlocks++;
7994 - bi->pagesInUse = 0;
7995 - bi->softDeletions = 0;
7996 - bi->hasShrinkHeader = 0;
7997 - bi->skipErasedCheck = 1; /* This is clean, so no need to check */
7998 - bi->gcPrioritise = 0;
7999 - yaffs_ClearChunkBits(dev, blockNo);
8001 - T(YAFFS_TRACE_ERASE,
8002 - (TSTR("Erased block %d" TENDSTR), blockNo));
8004 - dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
8006 - yaffs_RetireBlock(dev, blockNo);
8007 - T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
8008 - (TSTR("**>> Block %d retired" TENDSTR), blockNo));
8012 -static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
8016 - yaffs_BlockInfo *bi;
8018 - if (dev->nErasedBlocks < 1) {
8019 - /* Hoosterman we've got a problem.
8020 - * Can't get space to gc
8022 - T(YAFFS_TRACE_ERROR,
8023 - (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
8028 - /* Find an empty block. */
8030 - for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
8031 - dev->allocationBlockFinder++;
8032 - if (dev->allocationBlockFinder < dev->internalStartBlock
8033 - || dev->allocationBlockFinder > dev->internalEndBlock) {
8034 - dev->allocationBlockFinder = dev->internalStartBlock;
8037 - bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
8039 - if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
8040 - bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
8041 - dev->sequenceNumber++;
8042 - bi->sequenceNumber = dev->sequenceNumber;
8043 - dev->nErasedBlocks--;
8044 - T(YAFFS_TRACE_ALLOCATE,
8045 - (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
8046 - dev->allocationBlockFinder, dev->sequenceNumber,
8047 - dev->nErasedBlocks));
8048 - return dev->allocationBlockFinder;
8052 - T(YAFFS_TRACE_ALWAYS,
8054 - ("yaffs tragedy: no more erased blocks, but there should have been %d"
8055 - TENDSTR), dev->nErasedBlocks));
8062 -static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
8064 - if (!dev->nCheckpointBlocksRequired &&
8066 - /* Not a valid value so recalculate */
8069 - int devBlocks = (dev->endBlock - dev->startBlock + 1);
8072 - tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
8074 - if (tnodeSize < sizeof(yaffs_Tnode))
8075 - tnodeSize = sizeof(yaffs_Tnode);
8077 - nBytes += sizeof(yaffs_CheckpointValidity);
8078 - nBytes += sizeof(yaffs_CheckpointDevice);
8079 - nBytes += devBlocks * sizeof(yaffs_BlockInfo);
8080 - nBytes += devBlocks * dev->chunkBitmapStride;
8081 - nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
8082 - nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
8083 - nBytes += sizeof(yaffs_CheckpointValidity);
8084 - nBytes += sizeof(__u32); /* checksum*/
8086 - /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
8088 - nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->nChunksPerBlock)) + 3;
8090 - dev->nCheckpointBlocksRequired = nBlocks;
8093 - return dev->nCheckpointBlocksRequired;
8097 - * Check if there's space to allocate...
8098 - * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
8100 -static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
8102 - int reservedChunks;
8103 - int reservedBlocks = dev->nReservedBlocks;
8104 - int checkpointBlocks;
8106 - if (dev->isYaffs2) {
8107 - checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
8108 - dev->blocksInCheckpoint;
8109 - if (checkpointBlocks < 0)
8110 - checkpointBlocks = 0;
8112 - checkpointBlocks = 0;
8115 - reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
8117 - return (dev->nFreeChunks > reservedChunks);
8120 -static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
8121 - yaffs_BlockInfo **blockUsedPtr)
8124 - yaffs_BlockInfo *bi;
8126 - if (dev->allocationBlock < 0) {
8127 - /* Get next block to allocate off */
8128 - dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
8129 - dev->allocationPage = 0;
8132 - if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
8133 - /* Not enough space to allocate unless we're allowed to use the reserve. */
8137 - if (dev->nErasedBlocks < dev->nReservedBlocks
8138 - && dev->allocationPage == 0) {
8139 - T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
8142 - /* Next page please.... */
8143 - if (dev->allocationBlock >= 0) {
8144 - bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
8146 - retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
8147 - dev->allocationPage;
8149 - yaffs_SetChunkBit(dev, dev->allocationBlock,
8150 - dev->allocationPage);
8152 - dev->allocationPage++;
8154 - dev->nFreeChunks--;
8156 - /* If the block is full set the state to full */
8157 - if (dev->allocationPage >= dev->nChunksPerBlock) {
8158 - bi->blockState = YAFFS_BLOCK_STATE_FULL;
8159 - dev->allocationBlock = -1;
8163 - *blockUsedPtr = bi;
8168 - T(YAFFS_TRACE_ERROR,
8169 - (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
8174 -static int yaffs_GetErasedChunks(yaffs_Device *dev)
8178 - n = dev->nErasedBlocks * dev->nChunksPerBlock;
8180 - if (dev->allocationBlock > 0)
8181 - n += (dev->nChunksPerBlock - dev->allocationPage);
8187 -static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
8193 - int retVal = YAFFS_OK;
8196 - int isCheckpointBlock;
8197 - int matchingChunk;
8200 - int chunksBefore = yaffs_GetErasedChunks(dev);
8203 - yaffs_ExtendedTags tags;
8205 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
8207 - yaffs_Object *object;
8209 - isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
8211 - bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
8213 - T(YAFFS_TRACE_TRACING,
8214 - (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
8217 - bi->hasShrinkHeader,
8220 - /*yaffs_VerifyFreeChunks(dev); */
8222 - bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
8224 - /* Take off the number of soft deleted entries because
8225 - * they're going to get really deleted during GC.
8227 - dev->nFreeChunks -= bi->softDeletions;
8229 - dev->isDoingGC = 1;
8231 - if (isCheckpointBlock ||
8232 - !yaffs_StillSomeChunkBits(dev, block)) {
8233 - T(YAFFS_TRACE_TRACING,
8235 - ("Collecting block %d that has no chunks in use" TENDSTR),
8237 - yaffs_BlockBecameDirty(dev, block);
8240 - __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
8242 - yaffs_VerifyBlock(dev, bi, block);
8244 - maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10;
8245 - oldChunk = block * dev->nChunksPerBlock + dev->gcChunk;
8247 - for (/* init already done */;
8248 - retVal == YAFFS_OK &&
8249 - dev->gcChunk < dev->nChunksPerBlock &&
8250 - (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
8252 - dev->gcChunk++, oldChunk++) {
8253 - if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
8255 - /* This page is in use and might need to be copied off */
8261 - yaffs_InitialiseTags(&tags);
8263 - yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
8267 - yaffs_FindObjectByNumber(dev,
8270 - T(YAFFS_TRACE_GC_DETAIL,
8272 - ("Collecting chunk in block %d, %d %d %d " TENDSTR),
8273 - dev->gcChunk, tags.objectId, tags.chunkId,
8276 - if (object && !yaffs_SkipVerification(dev)) {
8277 - if (tags.chunkId == 0)
8278 - matchingChunk = object->hdrChunk;
8279 - else if (object->softDeleted)
8280 - matchingChunk = oldChunk; /* Defeat the test */
8282 - matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
8284 - if (oldChunk != matchingChunk)
8285 - T(YAFFS_TRACE_ERROR,
8286 - (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
8287 - oldChunk, matchingChunk, tags.objectId, tags.chunkId));
8292 - T(YAFFS_TRACE_ERROR,
8294 - ("page %d in gc has no object: %d %d %d "
8295 - TENDSTR), oldChunk,
8296 - tags.objectId, tags.chunkId, tags.byteCount));
8300 - object->deleted &&
8301 - object->softDeleted &&
8302 - tags.chunkId != 0) {
8303 - /* Data chunk in a soft deleted file, throw it away
8304 - * It's a soft deleted data chunk,
8305 - * No need to copy this, just forget about it and
8306 - * fix up the object.
8309 - object->nDataChunks--;
8311 - if (object->nDataChunks <= 0) {
8312 - /* remeber to clean up the object */
8313 - dev->gcCleanupList[cleanups] =
8319 - /* Todo object && object->deleted && object->nDataChunks == 0 */
8320 - /* Deleted object header with no data chunks.
8321 - * Can be discarded and the file deleted.
8323 - object->hdrChunk = 0;
8324 - yaffs_FreeTnode(object->myDev,
8327 - object->variant.fileVariant.top = NULL;
8328 - yaffs_DoGenericObjectDeletion(object);
8330 - } else if (object) {
8331 - /* It's either a data chunk in a live file or
8332 - * an ObjectHeader, so we're interested in it.
8333 - * NB Need to keep the ObjectHeaders of deleted files
8334 - * until the whole file has been deleted off
8336 - tags.serialNumber++;
8340 - if (tags.chunkId == 0) {
8341 - /* It is an object Id,
8342 - * We need to nuke the shrinkheader flags first
8343 - * We no longer want the shrinkHeader flag since its work is done
8344 - * and if it is left in place it will mess up scanning.
8347 - yaffs_ObjectHeader *oh;
8348 - oh = (yaffs_ObjectHeader *)buffer;
8350 - tags.extraIsShrinkHeader = 0;
8352 - yaffs_VerifyObjectHeader(object, oh, &tags, 1);
8356 - yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
8358 - if (newChunk < 0) {
8359 - retVal = YAFFS_FAIL;
8362 - /* Ok, now fix up the Tnodes etc. */
8364 - if (tags.chunkId == 0) {
8365 - /* It's a header */
8366 - object->hdrChunk = newChunk;
8367 - object->serial = tags.serialNumber;
8369 - /* It's a data chunk */
8370 - yaffs_PutChunkIntoFile
8378 - if (retVal == YAFFS_OK)
8379 - yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
8384 - yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
8387 - /* Do any required cleanups */
8388 - for (i = 0; i < cleanups; i++) {
8389 - /* Time to delete the file too */
8391 - yaffs_FindObjectByNumber(dev,
8392 - dev->gcCleanupList[i]);
8394 - yaffs_FreeTnode(dev,
8395 - object->variant.fileVariant.
8397 - object->variant.fileVariant.top = NULL;
8400 - ("yaffs: About to finally delete object %d"
8401 - TENDSTR), object->objectId));
8402 - yaffs_DoGenericObjectDeletion(object);
8403 - object->myDev->nDeletedFiles--;
8410 - yaffs_VerifyCollectedBlock(dev, bi, block);
8412 - chunksAfter = yaffs_GetErasedChunks(dev);
8413 - if (chunksBefore >= chunksAfter) {
8416 - ("gc did not increase free chunks before %d after %d"
8417 - TENDSTR), chunksBefore, chunksAfter));
8420 - /* If the gc completed then clear the current gcBlock so that we find another. */
8421 - if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
8422 - dev->gcBlock = -1;
8426 - dev->isDoingGC = 0;
8431 -/* New garbage collector
8432 - * If we're very low on erased blocks then we do aggressive garbage collection
8433 - * otherwise we do "leasurely" garbage collection.
8434 - * Aggressive gc looks further (whole array) and will accept less dirty blocks.
8435 - * Passive gc only inspects smaller areas and will only accept more dirty blocks.
8437 - * The idea is to help clear out space in a more spread-out manner.
8438 - * Dunno if it really does anything useful.
8440 -static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
8444 - int gcOk = YAFFS_OK;
8447 - int checkpointBlockAdjust;
8449 - if (dev->isDoingGC) {
8450 - /* Bail out so we don't get recursive gc */
8454 - /* This loop should pass the first time.
8455 - * We'll only see looping here if the erase of the collected block fails.
8461 - checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
8462 - if (checkpointBlockAdjust < 0)
8463 - checkpointBlockAdjust = 0;
8465 - if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
8466 - /* We need a block soon...*/
8469 - /* We're in no hurry */
8473 - if (dev->gcBlock <= 0) {
8474 - dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive);
8478 - block = dev->gcBlock;
8481 - dev->garbageCollections++;
8483 - dev->passiveGarbageCollections++;
8487 - ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
8488 - dev->nErasedBlocks, aggressive));
8490 - gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive);
8493 - if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
8496 - ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
8497 - TENDSTR), dev->nErasedBlocks, maxTries, block));
8499 - } while ((dev->nErasedBlocks < dev->nReservedBlocks) &&
8503 - return aggressive ? gcOk : YAFFS_OK;
8506 -/*------------------------- TAGS --------------------------------*/
8508 -static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
8509 - int chunkInObject)
8511 - return (tags->chunkId == chunkInObject &&
8512 - tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
8517 -/*-------------------- Data file manipulation -----------------*/
8519 -static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
8520 - yaffs_ExtendedTags *tags)
8522 - /*Get the Tnode, then get the level 0 offset chunk offset */
8524 - int theChunk = -1;
8525 - yaffs_ExtendedTags localTags;
8528 - yaffs_Device *dev = in->myDev;
8531 - /* Passed a NULL, so use our own tags space */
8532 - tags = &localTags;
8535 - tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
8538 - theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
8541 - yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
8547 -static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
8548 - yaffs_ExtendedTags *tags)
8550 - /* Get the Tnode, then get the level 0 offset chunk offset */
8552 - int theChunk = -1;
8553 - yaffs_ExtendedTags localTags;
8555 - yaffs_Device *dev = in->myDev;
8559 - /* Passed a NULL, so use our own tags space */
8560 - tags = &localTags;
8563 - tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
8567 - theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
8570 - yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
8573 - /* Delete the entry in the filestructure (if found) */
8575 - yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
8581 -#ifdef YAFFS_PARANOID
8583 -static int yaffs_CheckFileSanity(yaffs_Object *in)
8591 - yaffs_Tags localTags;
8592 - yaffs_Tags *tags = &localTags;
8596 - if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
8597 - return YAFFS_FAIL;
8599 - objId = in->objectId;
8600 - fSize = in->variant.fileVariant.fileSize;
8602 - (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
8604 - for (chunk = 1; chunk <= nChunks; chunk++) {
8605 - tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
8610 - theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
8612 - if (yaffs_CheckChunkBits
8613 - (dev, theChunk / dev->nChunksPerBlock,
8614 - theChunk % dev->nChunksPerBlock)) {
8616 - yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
8619 - if (yaffs_TagsMatch
8620 - (tags, in->objectId, chunk, chunkDeleted)) {
8630 - /* T(("No level 0 found for %d\n", chunk)); */
8634 - return failed ? YAFFS_FAIL : YAFFS_OK;
8639 -static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
8640 - int chunkInNAND, int inScan)
8642 - /* NB inScan is zero unless scanning.
8643 - * For forward scanning, inScan is > 0;
8644 - * for backward scanning inScan is < 0
8648 - yaffs_Device *dev = in->myDev;
8649 - int existingChunk;
8650 - yaffs_ExtendedTags existingTags;
8651 - yaffs_ExtendedTags newTags;
8652 - unsigned existingSerial, newSerial;
8654 - if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8655 - /* Just ignore an attempt at putting a chunk into a non-file during scanning
8656 - * If it is not during Scanning then something went wrong!
8659 - T(YAFFS_TRACE_ERROR,
8661 - ("yaffs tragedy:attempt to put data chunk into a non-file"
8666 - yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
8670 - tn = yaffs_AddOrFindLevel0Tnode(dev,
8671 - &in->variant.fileVariant,
8675 - return YAFFS_FAIL;
8677 - existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
8679 - if (inScan != 0) {
8680 - /* If we're scanning then we need to test for duplicates
8681 - * NB This does not need to be efficient since it should only ever
8682 - * happen when the power fails during a write, then only one
8683 - * chunk should ever be affected.
8685 - * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
8686 - * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
8689 - if (existingChunk > 0) {
8690 - /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
8691 - * thus we have to do a FindChunkInFile to get the real chunk id.
8693 - * We have a duplicate now we need to decide which one to use:
8695 - * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
8696 - * Forward scanning YAFFS2: The new one is what we use, dump the old one.
8697 - * YAFFS1: Get both sets of tags and compare serial numbers.
8701 - /* Only do this for forward scanning */
8702 - yaffs_ReadChunkWithTagsFromNAND(dev,
8706 - /* Do a proper find */
8708 - yaffs_FindChunkInFile(in, chunkInInode,
8712 - if (existingChunk <= 0) {
8713 - /*Hoosterman - how did this happen? */
8715 - T(YAFFS_TRACE_ERROR,
8717 - ("yaffs tragedy: existing chunk < 0 in scan"
8722 - /* NB The deleted flags should be false, otherwise the chunks will
8723 - * not be loaded during a scan
8727 - newSerial = newTags.serialNumber;
8728 - existingSerial = existingTags.serialNumber;
8731 - if ((inScan > 0) &&
8732 - (in->myDev->isYaffs2 ||
8733 - existingChunk <= 0 ||
8734 - ((existingSerial + 1) & 3) == newSerial)) {
8735 - /* Forward scanning.
8737 - * Delete the old one and drop through to update the tnode
8739 - yaffs_DeleteChunk(dev, existingChunk, 1,
8742 - /* Backward scanning or we want to use the existing one
8744 - * Delete the new one and return early so that the tnode isn't changed
8746 - yaffs_DeleteChunk(dev, chunkInNAND, 1,
8754 - if (existingChunk == 0)
8755 - in->nDataChunks++;
8757 - yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
8762 -static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
8765 - int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
8767 - if (chunkInNAND >= 0)
8768 - return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
8771 - T(YAFFS_TRACE_NANDACCESS,
8772 - (TSTR("Chunk %d not found zero instead" TENDSTR),
8774 - /* get sane (zero) data if you read a hole */
8775 - memset(buffer, 0, in->myDev->nDataBytesPerChunk);
8781 -void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
8785 - yaffs_ExtendedTags tags;
8786 - yaffs_BlockInfo *bi;
8791 - dev->nDeletions++;
8792 - block = chunkId / dev->nChunksPerBlock;
8793 - page = chunkId % dev->nChunksPerBlock;
8796 - if (!yaffs_CheckChunkBit(dev, block, page))
8797 - T(YAFFS_TRACE_VERIFY,
8798 - (TSTR("Deleting invalid chunk %d"TENDSTR),
8801 - bi = yaffs_GetBlockInfo(dev, block);
8803 - T(YAFFS_TRACE_DELETION,
8804 - (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
8807 - bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
8809 - yaffs_InitialiseTags(&tags);
8811 - tags.chunkDeleted = 1;
8813 - yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
8814 - yaffs_HandleUpdateChunk(dev, chunkId, &tags);
8816 - dev->nUnmarkedDeletions++;
8819 - /* Pull out of the management area.
8820 - * If the whole block became dirty, this will kick off an erasure.
8822 - if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
8823 - bi->blockState == YAFFS_BLOCK_STATE_FULL ||
8824 - bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
8825 - bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
8826 - dev->nFreeChunks++;
8828 - yaffs_ClearChunkBit(dev, block, page);
8832 - if (bi->pagesInUse == 0 &&
8833 - !bi->hasShrinkHeader &&
8834 - bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
8835 - bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
8836 - yaffs_BlockBecameDirty(dev, block);
8843 -static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
8844 - const __u8 *buffer, int nBytes,
8847 - /* Find old chunk Need to do this to get serial number
8848 - * Write new one and patch into tree.
8849 - * Invalidate old tags.
8853 - yaffs_ExtendedTags prevTags;
8856 - yaffs_ExtendedTags newTags;
8858 - yaffs_Device *dev = in->myDev;
8860 - yaffs_CheckGarbageCollection(dev);
8862 - /* Get the previous chunk at this location in the file if it exists */
8863 - prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
8865 - /* Set up new tags */
8866 - yaffs_InitialiseTags(&newTags);
8868 - newTags.chunkId = chunkInInode;
8869 - newTags.objectId = in->objectId;
8870 - newTags.serialNumber =
8871 - (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
8872 - newTags.byteCount = nBytes;
8874 - if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) {
8875 - T(YAFFS_TRACE_ERROR,
8876 - (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
8881 - yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
8884 - if (newChunkId >= 0) {
8885 - yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
8887 - if (prevChunkId >= 0)
8888 - yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
8890 - yaffs_CheckFileSanity(in);
8892 - return newChunkId;
8896 -/* UpdateObjectHeader updates the header on NAND for an object.
8897 - * If name is not NULL, then that new name is used.
8899 -int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
8900 - int isShrink, int shadows)
8903 - yaffs_BlockInfo *bi;
8905 - yaffs_Device *dev = in->myDev;
8912 - yaffs_ExtendedTags newTags;
8913 - yaffs_ExtendedTags oldTags;
8915 - __u8 *buffer = NULL;
8916 - YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
8918 - yaffs_ObjectHeader *oh = NULL;
8920 + YCHAR *str = NULL;
8922 - yaffs_strcpy(oldName, _Y("silly old name"));
8923 + yaffs_dev_t *dev = parent->my_dev;
8925 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
8926 + if (yaffs_find_by_name(parent, name))
8930 - in == dev->rootDir || /* The rootDir should also be saved */
8932 + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
8933 + str = yaffs_clone_str(aliasString);
8938 - yaffs_CheckGarbageCollection(dev);
8939 - yaffs_CheckObjectDetailsLoaded(in);
8940 + in = yaffs_new_obj(dev, -1, type);
8942 - buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
8943 - oh = (yaffs_ObjectHeader *) buffer;
8950 - prevChunkId = in->hdrChunk;
8952 - if (prevChunkId > 0) {
8953 - result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
8954 - buffer, &oldTags);
8956 - yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
8958 - memcpy(oldName, oh->name, sizeof(oh->name));
8961 - memset(buffer, 0xFF, dev->nDataBytesPerChunk);
8963 + in->hdr_chunk = 0;
8965 + in->variant_type = type;
8967 - oh->type = in->variantType;
8968 - oh->yst_mode = in->yst_mode;
8969 - oh->shadowsObject = oh->inbandShadowsObject = shadows;
8970 + in->yst_mode = mode;
8972 #ifdef CONFIG_YAFFS_WINCE
8973 - oh->win_atime[0] = in->win_atime[0];
8974 - oh->win_ctime[0] = in->win_ctime[0];
8975 - oh->win_mtime[0] = in->win_mtime[0];
8976 - oh->win_atime[1] = in->win_atime[1];
8977 - oh->win_ctime[1] = in->win_ctime[1];
8978 - oh->win_mtime[1] = in->win_mtime[1];
8979 + yfsd_win_file_time_now(in->win_atime);
8980 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
8981 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
8984 - oh->yst_uid = in->yst_uid;
8985 - oh->yst_gid = in->yst_gid;
8986 - oh->yst_atime = in->yst_atime;
8987 - oh->yst_mtime = in->yst_mtime;
8988 - oh->yst_ctime = in->yst_ctime;
8989 - oh->yst_rdev = in->yst_rdev;
8990 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
8992 + in->yst_rdev = rdev;
8993 + in->yst_uid = uid;
8994 + in->yst_gid = gid;
8997 - oh->parentObjectId = in->parent->objectId;
8999 - oh->parentObjectId = 0;
9000 + in->n_data_chunks = 0;
9002 - if (name && *name) {
9003 - memset(oh->name, 0, sizeof(oh->name));
9004 - yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
9005 - } else if (prevChunkId >= 0)
9006 - memcpy(oh->name, oldName, sizeof(oh->name));
9008 - memset(oh->name, 0, sizeof(oh->name));
9009 + yaffs_set_obj_name(in, name);
9012 - oh->isShrink = isShrink;
9013 + yaffs_add_obj_to_dir(parent, in);
9015 - switch (in->variantType) {
9016 - case YAFFS_OBJECT_TYPE_UNKNOWN:
9017 - /* Should not happen */
9019 - case YAFFS_OBJECT_TYPE_FILE:
9021 - (oh->parentObjectId == YAFFS_OBJECTID_DELETED
9022 - || oh->parentObjectId ==
9023 - YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
9024 - fileVariant.fileSize;
9025 + in->my_dev = parent->my_dev;
9028 + case YAFFS_OBJECT_TYPE_SYMLINK:
9029 + in->variant.symlink_variant.alias = str;
9031 case YAFFS_OBJECT_TYPE_HARDLINK:
9032 - oh->equivalentObjectId =
9033 - in->variant.hardLinkVariant.equivalentObjectId;
9035 - case YAFFS_OBJECT_TYPE_SPECIAL:
9037 + in->variant.hardlink_variant.equiv_obj =
9039 + in->variant.hardlink_variant.equiv_id =
9040 + equiv_obj->obj_id;
9041 + ylist_add(&in->hard_links, &equiv_obj->hard_links);
9043 + case YAFFS_OBJECT_TYPE_FILE:
9044 case YAFFS_OBJECT_TYPE_DIRECTORY:
9047 - case YAFFS_OBJECT_TYPE_SYMLINK:
9048 - yaffs_strncpy(oh->alias,
9049 - in->variant.symLinkVariant.alias,
9050 - YAFFS_MAX_ALIAS_LENGTH);
9051 - oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
9052 + case YAFFS_OBJECT_TYPE_SPECIAL:
9053 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9059 - yaffs_InitialiseTags(&newTags);
9061 - newTags.chunkId = 0;
9062 - newTags.objectId = in->objectId;
9063 - newTags.serialNumber = in->serial;
9065 - /* Add extra info for file header */
9067 - newTags.extraHeaderInfoAvailable = 1;
9068 - newTags.extraParentObjectId = oh->parentObjectId;
9069 - newTags.extraFileLength = oh->fileSize;
9070 - newTags.extraIsShrinkHeader = oh->isShrink;
9071 - newTags.extraEquivalentObjectId = oh->equivalentObjectId;
9072 - newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
9073 - newTags.extraObjectType = in->variantType;
9075 - yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
9077 - /* Create new chunk in NAND */
9079 - yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
9080 - (prevChunkId >= 0) ? 1 : 0);
9082 - if (newChunkId >= 0) {
9084 - in->hdrChunk = newChunkId;
9086 - if (prevChunkId >= 0) {
9087 - yaffs_DeleteChunk(dev, prevChunkId, 1,
9091 - if (!yaffs_ObjectHasCachedWriteData(in))
9094 - /* If this was a shrink, then mark the block that the chunk lives on */
9096 - bi = yaffs_GetBlockInfo(in->myDev,
9097 - newChunkId / in->myDev->nChunksPerBlock);
9098 - bi->hasShrinkHeader = 1;
9101 + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
9102 + /* Could not create the object header, fail the creation */
9103 + yaffs_del_obj(in);
9107 - retVal = newChunkId;
9109 + yaffs_update_parent(parent);
9113 - yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
9119 -/*------------------------ Short Operations Cache ----------------------------------------
9120 - * In many situations where there is no high level buffering (eg WinCE) a lot of
9121 - * reads might be short sequential reads, and a lot of writes may be short
9122 - * sequential writes. eg. scanning/writing a jpeg file.
9123 - * In these cases, a short read/write cache can provide a huge perfomance benefit
9124 - * with dumb-as-a-rock code.
9125 - * In Linux, the page cache provides read buffering aand the short op cache provides write
9128 - * There are a limited number (~10) of cache chunks per device so that we don't
9129 - * need a very intelligent search.
9132 -static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
9133 +yaffs_obj_t *yaffs_create_file(yaffs_obj_t *parent, const YCHAR *name,
9134 + __u32 mode, __u32 uid, __u32 gid)
9136 - yaffs_Device *dev = obj->myDev;
9138 - yaffs_ChunkCache *cache;
9139 - int nCaches = obj->myDev->nShortOpCaches;
9141 - for (i = 0; i < nCaches; i++) {
9142 - cache = &dev->srCache[i];
9143 - if (cache->object == obj &&
9147 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
9148 + uid, gid, NULL, NULL, 0);
9152 +yaffs_obj_t *yaffs_create_dir(yaffs_obj_t *parent, const YCHAR *name,
9153 + __u32 mode, __u32 uid, __u32 gid)
9155 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
9156 + mode, uid, gid, NULL, NULL, 0);
9159 +yaffs_obj_t *yaffs_create_special(yaffs_obj_t *parent, const YCHAR *name,
9160 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
9162 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
9163 + uid, gid, NULL, NULL, rdev);
9166 -static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
9167 +yaffs_obj_t *yaffs_create_symlink(yaffs_obj_t *parent, const YCHAR *name,
9168 + __u32 mode, __u32 uid, __u32 gid,
9169 + const YCHAR *alias)
9171 - yaffs_Device *dev = obj->myDev;
9172 - int lowest = -99; /* Stop compiler whining. */
9174 - yaffs_ChunkCache *cache;
9175 - int chunkWritten = 0;
9176 - int nCaches = obj->myDev->nShortOpCaches;
9177 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
9178 + uid, gid, NULL, alias, 0);
9181 - if (nCaches > 0) {
9184 +/* yaffs_link_obj returns the object id of the equivalent object.*/
9185 +yaffs_obj_t *yaffs_link_obj(yaffs_obj_t *parent, const YCHAR *name,
9186 + yaffs_obj_t *equiv_obj)
9188 + /* Get the real object in case we were fed a hard link as an equivalent object */
9189 + equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
9191 - /* Find the dirty cache for this object with the lowest chunk id. */
9192 - for (i = 0; i < nCaches; i++) {
9193 - if (dev->srCache[i].object == obj &&
9194 - dev->srCache[i].dirty) {
9196 - || dev->srCache[i].chunkId <
9198 - cache = &dev->srCache[i];
9199 - lowest = cache->chunkId;
9203 + if (yaffs_create_obj
9204 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
9205 + equiv_obj, NULL, 0)) {
9211 - if (cache && !cache->locked) {
9212 - /* Write it out and free it up */
9216 - yaffs_WriteChunkDataToObject(cache->object,
9222 - cache->object = NULL;
9224 +static int yaffs_change_obj_name(yaffs_obj_t *obj, yaffs_obj_t *new_dir,
9225 + const YCHAR *new_name, int force, int shadows)
9230 - } while (cache && chunkWritten > 0);
9231 + yaffs_obj_t *existingTarget;
9234 - /* Hoosterman, disk full while writing cache out. */
9235 - T(YAFFS_TRACE_ERROR,
9236 - (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
9237 + if (new_dir == NULL)
9238 + new_dir = obj->parent; /* use the old directory */
9241 + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
9242 + T(YAFFS_TRACE_ALWAYS,
9244 + ("tragedy: yaffs_change_obj_name: new_dir is not a directory"
9250 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
9251 + if (obj->my_dev->param.is_yaffs2)
9252 + unlinkOp = (new_dir == obj->my_dev->unlinked_dir);
9254 + unlinkOp = (new_dir == obj->my_dev->unlinked_dir
9255 + && obj->variant_type == YAFFS_OBJECT_TYPE_FILE);
9257 -/*yaffs_FlushEntireDeviceCache(dev)
9261 + deleteOp = (new_dir == obj->my_dev->del_dir);
9263 -void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
9265 - yaffs_Object *obj;
9266 - int nCaches = dev->nShortOpCaches;
9268 + existingTarget = yaffs_find_by_name(new_dir, new_name);
9270 - /* Find a dirty object in the cache and flush it...
9271 - * until there are no further dirty objects.
9272 + /* If the object is a file going into the unlinked directory,
9273 + * then it is OK to just stuff it in since duplicate names are allowed.
9274 + * else only proceed if the new name does not exist and if we're putting
9275 + * it into a directory.
9279 - for (i = 0; i < nCaches && !obj; i++) {
9280 - if (dev->srCache[i].object &&
9281 - dev->srCache[i].dirty)
9282 - obj = dev->srCache[i].object;
9286 - yaffs_FlushFilesChunkCache(obj);
9295 + !existingTarget) &&
9296 + new_dir->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
9297 + yaffs_set_obj_name(obj, new_name);
9300 + yaffs_add_obj_to_dir(new_dir, obj);
9302 -/* Grab us a cache chunk for use.
9303 - * First look for an empty one.
9304 - * Then look for the least recently used non-dirty one.
9305 - * Then look for the least recently used dirty one...., flush and look again.
9307 -static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
9311 + obj->unlinked = 1;
9313 - if (dev->nShortOpCaches > 0) {
9314 - for (i = 0; i < dev->nShortOpCaches; i++) {
9315 - if (!dev->srCache[i].object)
9316 - return &dev->srCache[i];
9318 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
9319 + if (yaffs_update_oh(obj, new_name, 0, deleteOp, shadows, NULL) >= 0)
9324 + return YAFFS_FAIL;
9327 -static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
9328 +int yaffs_rename_obj(yaffs_obj_t *old_dir, const YCHAR *old_name,
9329 + yaffs_obj_t *new_dir, const YCHAR *new_name)
9331 - yaffs_ChunkCache *cache;
9332 - yaffs_Object *theObj;
9337 - if (dev->nShortOpCaches > 0) {
9338 - /* Try find a non-dirty one... */
9340 - cache = yaffs_GrabChunkCacheWorker(dev);
9341 + yaffs_obj_t *obj = NULL;
9342 + yaffs_obj_t *existingTarget = NULL;
9348 - /* They were all dirty, find the last recently used object and flush
9349 - * its cache, then find again.
9350 - * NB what's here is not very accurate, we actually flush the object
9351 - * the last recently used page.
9354 - /* With locking we can't assume we can use entry zero */
9355 + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
9357 + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
9364 + dev = old_dir->my_dev;
9366 - for (i = 0; i < dev->nShortOpCaches; i++) {
9367 - if (dev->srCache[i].object &&
9368 - !dev->srCache[i].locked &&
9369 - (dev->srCache[i].lastUse < usage || !cache)) {
9370 - usage = dev->srCache[i].lastUse;
9371 - theObj = dev->srCache[i].object;
9372 - cache = &dev->srCache[i];
9376 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
9377 + /* Special case for case insemsitive systems (eg. WinCE).
9378 + * While look-up is case insensitive, the name isn't.
9379 + * Therefore we might want to change x.txt to X.txt
9381 + if (old_dir == new_dir && yaffs_strcmp(old_name, new_name) == 0)
9385 - if (!cache || cache->dirty) {
9386 - /* Flush and try again */
9387 - yaffs_FlushFilesChunkCache(theObj);
9388 - cache = yaffs_GrabChunkCacheWorker(dev);
9390 + if(yaffs_strnlen(new_name,YAFFS_MAX_NAME_LENGTH+1) > YAFFS_MAX_NAME_LENGTH)
9391 + /* ENAMETOOLONG */
9392 + return YAFFS_FAIL;
9398 + obj = yaffs_find_by_name(old_dir, old_name);
9401 + if (obj && obj->rename_allowed) {
9403 -/* Find a cached chunk */
9404 -static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
9407 - yaffs_Device *dev = obj->myDev;
9409 - if (dev->nShortOpCaches > 0) {
9410 - for (i = 0; i < dev->nShortOpCaches; i++) {
9411 - if (dev->srCache[i].object == obj &&
9412 - dev->srCache[i].chunkId == chunkId) {
9414 + /* Now do the handling for an existing target, if there is one */
9416 - return &dev->srCache[i];
9418 + existingTarget = yaffs_find_by_name(new_dir, new_name);
9419 + if (existingTarget &&
9420 + existingTarget->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
9421 + !ylist_empty(&existingTarget->variant.dir_variant.children)) {
9422 + /* There is a target that is a non-empty directory, so we fail */
9423 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
9424 + } else if (existingTarget && existingTarget != obj) {
9425 + /* Nuke the target first, using shadowing,
9426 + * but only if it isn't the same object.
9428 + * Note we must disable gc otherwise it can mess up the shadowing.
9431 + dev->gc_disable=1;
9432 + yaffs_change_obj_name(obj, new_dir, new_name, force,
9433 + existingTarget->obj_id);
9434 + existingTarget->is_shadowed = 1;
9435 + yaffs_unlink_obj(existingTarget);
9436 + dev->gc_disable=0;
9439 + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
9441 + yaffs_update_parent(old_dir);
9442 + if(new_dir != old_dir)
9443 + yaffs_update_parent(new_dir);
9448 + return YAFFS_FAIL;
9451 -/* Mark the chunk for the least recently used algorithym */
9452 -static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
9454 +/*------------------------- Block Management and Page Allocation ----------------*/
9456 +static int yaffs_init_blocks(yaffs_dev_t *dev)
9458 + int nBlocks = dev->internal_end_block - dev->internal_start_block + 1;
9460 - if (dev->nShortOpCaches > 0) {
9461 - if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
9462 - /* Reset the cache usages */
9464 - for (i = 1; i < dev->nShortOpCaches; i++)
9465 - dev->srCache[i].lastUse = 0;
9466 + dev->block_info = NULL;
9467 + dev->chunk_bits = NULL;
9469 - dev->srLastUse = 0;
9471 + dev->alloc_block = -1; /* force it to get a new one */
9474 + /* If the first allocation strategy fails, thry the alternate one */
9475 + dev->block_info = YMALLOC(nBlocks * sizeof(yaffs_block_info_t));
9476 + if (!dev->block_info) {
9477 + dev->block_info = YMALLOC_ALT(nBlocks * sizeof(yaffs_block_info_t));
9478 + dev->block_info_alt = 1;
9480 + dev->block_info_alt = 0;
9482 - cache->lastUse = dev->srLastUse;
9483 + if (dev->block_info) {
9484 + /* Set up dynamic blockinfo stuff. */
9485 + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8; /* round up bytes */
9486 + dev->chunk_bits = YMALLOC(dev->chunk_bit_stride * nBlocks);
9487 + if (!dev->chunk_bits) {
9488 + dev->chunk_bits = YMALLOC_ALT(dev->chunk_bit_stride * nBlocks);
9489 + dev->chunk_bits_alt = 1;
9491 + dev->chunk_bits_alt = 0;
9496 + if (dev->block_info && dev->chunk_bits) {
9497 + memset(dev->block_info, 0, nBlocks * sizeof(yaffs_block_info_t));
9498 + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * nBlocks);
9502 + return YAFFS_FAIL;
9505 -/* Invalidate a single cache page.
9506 - * Do this when a whole page gets written,
9507 - * ie the short cache for this page is no longer valid.
9509 -static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
9510 +static void yaffs_deinit_blocks(yaffs_dev_t *dev)
9512 - if (object->myDev->nShortOpCaches > 0) {
9513 - yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
9514 + if (dev->block_info_alt && dev->block_info)
9515 + YFREE_ALT(dev->block_info);
9516 + else if (dev->block_info)
9517 + YFREE(dev->block_info);
9520 - cache->object = NULL;
9523 + dev->block_info_alt = 0;
9525 -/* Invalidate all the cache pages associated with this object
9526 - * Do this whenever ther file is deleted or resized.
9528 -static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
9531 - yaffs_Device *dev = in->myDev;
9532 + dev->block_info = NULL;
9534 - if (dev->nShortOpCaches > 0) {
9535 - /* Invalidate it. */
9536 - for (i = 0; i < dev->nShortOpCaches; i++) {
9537 - if (dev->srCache[i].object == in)
9538 - dev->srCache[i].object = NULL;
9541 + if (dev->chunk_bits_alt && dev->chunk_bits)
9542 + YFREE_ALT(dev->chunk_bits);
9543 + else if (dev->chunk_bits)
9544 + YFREE(dev->chunk_bits);
9545 + dev->chunk_bits_alt = 0;
9546 + dev->chunk_bits = NULL;
9549 -/*--------------------- Checkpointing --------------------*/
9550 +void yaffs_block_became_dirty(yaffs_dev_t *dev, int block_no)
9552 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, block_no);
9556 -static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
9558 - yaffs_CheckpointValidity cp;
9559 + /* If the block is still healthy erase it and mark as clean.
9560 + * If the block has had a data failure, then retire it.
9563 - memset(&cp, 0, sizeof(cp));
9564 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
9565 + (TSTR("yaffs_block_became_dirty block %d state %d %s"TENDSTR),
9566 + block_no, bi->block_state, (bi->needs_retiring) ? "needs retiring" : ""));
9568 - cp.structType = sizeof(cp);
9569 - cp.magic = YAFFS_MAGIC;
9570 - cp.version = YAFFS_CHECKPOINT_VERSION;
9571 - cp.head = (head) ? 1 : 0;
9572 + yaffs2_clear_oldest_dirty_seq(dev,bi);
9574 - return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
9577 + bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
9579 -static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
9581 - yaffs_CheckpointValidity cp;
9583 + /* If this is the block being garbage collected then stop gc'ing this block */
9584 + if(block_no == dev->gc_block)
9585 + dev->gc_block = 0;
9587 - ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
9588 + /* If this block is currently the best candidate for gc then drop as a candidate */
9589 + if(block_no == dev->gc_dirtiest){
9590 + dev->gc_dirtiest = 0;
9591 + dev->gc_pages_in_use = 0;
9595 - ok = (cp.structType == sizeof(cp)) &&
9596 - (cp.magic == YAFFS_MAGIC) &&
9597 - (cp.version == YAFFS_CHECKPOINT_VERSION) &&
9598 - (cp.head == ((head) ? 1 : 0));
9599 - return ok ? 1 : 0;
9601 + if (!bi->needs_retiring) {
9602 + yaffs2_checkpt_invalidate(dev);
9603 + erasedOk = yaffs_erase_block(dev, block_no);
9605 + dev->n_erase_failures++;
9606 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
9607 + (TSTR("**>> Erasure failed %d" TENDSTR), block_no));
9611 -static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
9612 - yaffs_Device *dev)
9614 - cp->nErasedBlocks = dev->nErasedBlocks;
9615 - cp->allocationBlock = dev->allocationBlock;
9616 - cp->allocationPage = dev->allocationPage;
9617 - cp->nFreeChunks = dev->nFreeChunks;
9619 + ((yaffs_trace_mask & YAFFS_TRACE_ERASE) || !yaffs_skip_verification(dev))) {
9621 + for (i = 0; i < dev->param.chunks_per_block; i++) {
9622 + if (!yaffs_check_chunk_erased
9623 + (dev, block_no * dev->param.chunks_per_block + i)) {
9624 + T(YAFFS_TRACE_ERROR,
9626 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
9627 + TENDSTR), block_no, i));
9633 + /* Clean it up... */
9634 + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
9635 + bi->seq_number = 0;
9636 + dev->n_erased_blocks++;
9637 + bi->pages_in_use = 0;
9638 + bi->soft_del_pages = 0;
9639 + bi->has_shrink_hdr = 0;
9640 + bi->skip_erased_check = 1; /* This is clean, so no need to check */
9641 + bi->gc_prioritise = 0;
9642 + yaffs_clear_chunk_bits(dev, block_no);
9644 - cp->nDeletedFiles = dev->nDeletedFiles;
9645 - cp->nUnlinkedFiles = dev->nUnlinkedFiles;
9646 - cp->nBackgroundDeletions = dev->nBackgroundDeletions;
9647 - cp->sequenceNumber = dev->sequenceNumber;
9648 - cp->oldestDirtySequence = dev->oldestDirtySequence;
9649 + T(YAFFS_TRACE_ERASE,
9650 + (TSTR("Erased block %d" TENDSTR), block_no));
9652 + dev->n_free_chunks -= dev->param.chunks_per_block; /* We lost a block of free space */
9654 + yaffs_retire_block(dev, block_no);
9655 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
9656 + (TSTR("**>> Block %d retired" TENDSTR), block_no));
9660 -static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
9661 - yaffs_CheckpointDevice *cp)
9662 +static int yaffs_find_alloc_block(yaffs_dev_t *dev)
9664 - dev->nErasedBlocks = cp->nErasedBlocks;
9665 - dev->allocationBlock = cp->allocationBlock;
9666 - dev->allocationPage = cp->allocationPage;
9667 - dev->nFreeChunks = cp->nFreeChunks;
9669 - dev->nDeletedFiles = cp->nDeletedFiles;
9670 - dev->nUnlinkedFiles = cp->nUnlinkedFiles;
9671 - dev->nBackgroundDeletions = cp->nBackgroundDeletions;
9672 - dev->sequenceNumber = cp->sequenceNumber;
9673 - dev->oldestDirtySequence = cp->oldestDirtySequence;
9677 + yaffs_block_info_t *bi;
9679 -static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
9681 - yaffs_CheckpointDevice cp;
9683 - __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
9684 + if (dev->n_erased_blocks < 1) {
9685 + /* Hoosterman we've got a problem.
9686 + * Can't get space to gc
9688 + T(YAFFS_TRACE_ERROR,
9689 + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
9695 - /* Write device runtime values*/
9696 - yaffs_DeviceToCheckpointDevice(&cp, dev);
9697 - cp.structType = sizeof(cp);
9698 + /* Find an empty block. */
9700 - ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
9701 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
9702 + dev->alloc_block_finder++;
9703 + if (dev->alloc_block_finder < dev->internal_start_block
9704 + || dev->alloc_block_finder > dev->internal_end_block) {
9705 + dev->alloc_block_finder = dev->internal_start_block;
9708 - /* Write block info */
9710 - nBytes = nBlocks * sizeof(yaffs_BlockInfo);
9711 - ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
9712 + bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
9714 + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
9715 + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
9716 + dev->seq_number++;
9717 + bi->seq_number = dev->seq_number;
9718 + dev->n_erased_blocks--;
9719 + T(YAFFS_TRACE_ALLOCATE,
9720 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
9721 + dev->alloc_block_finder, dev->seq_number,
9722 + dev->n_erased_blocks));
9723 + return dev->alloc_block_finder;
9727 - /* Write chunk bits */
9729 - nBytes = nBlocks * dev->chunkBitmapStride;
9730 - ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
9732 - return ok ? 1 : 0;
9733 + T(YAFFS_TRACE_ALWAYS,
9735 + ("yaffs tragedy: no more erased blocks, but there should have been %d"
9736 + TENDSTR), dev->n_erased_blocks));
9741 -static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
9744 + * Check if there's space to allocate...
9745 + * Thinks.... do we need top make this ths same as yaffs_get_free_chunks()?
9747 +int yaffs_check_alloc_available(yaffs_dev_t *dev, int n_chunks)
9749 - yaffs_CheckpointDevice cp;
9751 - __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
9752 + int reservedChunks;
9753 + int reservedBlocks = dev->param.n_reserved_blocks;
9754 + int checkpointBlocks;
9757 + checkpointBlocks = yaffs_calc_checkpt_blocks_required(dev);
9759 - ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
9762 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->param.chunks_per_block);
9764 - if (cp.structType != sizeof(cp))
9766 + return (dev->n_free_chunks > (reservedChunks + n_chunks));
9769 +static int yaffs_alloc_chunk(yaffs_dev_t *dev, int useReserve,
9770 + yaffs_block_info_t **blockUsedPtr)
9773 + yaffs_block_info_t *bi;
9775 + if (dev->alloc_block < 0) {
9776 + /* Get next block to allocate off */
9777 + dev->alloc_block = yaffs_find_alloc_block(dev);
9778 + dev->alloc_page = 0;
9781 + if (!useReserve && !yaffs_check_alloc_available(dev, 1)) {
9782 + /* Not enough space to allocate unless we're allowed to use the reserve. */
9786 - yaffs_CheckpointDeviceToDevice(dev, &cp);
9787 + if (dev->n_erased_blocks < dev->param.n_reserved_blocks
9788 + && dev->alloc_page == 0) {
9789 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
9792 - nBytes = nBlocks * sizeof(yaffs_BlockInfo);
9793 + /* Next page please.... */
9794 + if (dev->alloc_block >= 0) {
9795 + bi = yaffs_get_block_info(dev, dev->alloc_block);
9797 - ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
9798 + retVal = (dev->alloc_block * dev->param.chunks_per_block) +
9800 + bi->pages_in_use++;
9801 + yaffs_set_chunk_bit(dev, dev->alloc_block,
9806 - nBytes = nBlocks * dev->chunkBitmapStride;
9807 + dev->alloc_page++;
9809 - ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
9810 + dev->n_free_chunks--;
9812 - return ok ? 1 : 0;
9814 + /* If the block is full set the state to full */
9815 + if (dev->alloc_page >= dev->param.chunks_per_block) {
9816 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
9817 + dev->alloc_block = -1;
9820 -static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
9821 - yaffs_Object *obj)
9824 + *blockUsedPtr = bi;
9829 - cp->objectId = obj->objectId;
9830 - cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
9831 - cp->hdrChunk = obj->hdrChunk;
9832 - cp->variantType = obj->variantType;
9833 - cp->deleted = obj->deleted;
9834 - cp->softDeleted = obj->softDeleted;
9835 - cp->unlinked = obj->unlinked;
9836 - cp->fake = obj->fake;
9837 - cp->renameAllowed = obj->renameAllowed;
9838 - cp->unlinkAllowed = obj->unlinkAllowed;
9839 - cp->serial = obj->serial;
9840 - cp->nDataChunks = obj->nDataChunks;
9841 + T(YAFFS_TRACE_ERROR,
9842 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
9844 - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
9845 - cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
9846 - else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
9847 - cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
9851 -static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
9852 +static int yaffs_get_erased_chunks(yaffs_dev_t *dev)
9856 - yaffs_Object *parent;
9857 + n = dev->n_erased_blocks * dev->param.chunks_per_block;
9859 - if (obj->variantType != cp->variantType) {
9860 - T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
9861 - TCONT("chunk %d does not match existing object type %d")
9862 - TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
9863 - obj->variantType));
9866 + if (dev->alloc_block > 0)
9867 + n += (dev->param.chunks_per_block - dev->alloc_page);
9869 - obj->objectId = cp->objectId;
9873 - parent = yaffs_FindOrCreateObjectByNumber(
9876 - YAFFS_OBJECT_TYPE_DIRECTORY);
9882 - if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
9883 - T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
9884 - TCONT(" chunk %d Parent type, %d, not directory")
9886 - cp->objectId, cp->parentId, cp->variantType,
9887 - cp->hdrChunk, parent->variantType));
9890 + * yaffs_skip_rest_of_block() skips over the rest of the allocation block
9891 + * if we don't want to write to it.
9893 +void yaffs_skip_rest_of_block(yaffs_dev_t *dev)
9895 + if(dev->alloc_block > 0){
9896 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, dev->alloc_block);
9897 + if(bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING){
9898 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
9899 + dev->alloc_block = -1;
9901 - yaffs_AddObjectToDirectory(parent, obj);
9904 - obj->hdrChunk = cp->hdrChunk;
9905 - obj->variantType = cp->variantType;
9906 - obj->deleted = cp->deleted;
9907 - obj->softDeleted = cp->softDeleted;
9908 - obj->unlinked = cp->unlinked;
9909 - obj->fake = cp->fake;
9910 - obj->renameAllowed = cp->renameAllowed;
9911 - obj->unlinkAllowed = cp->unlinkAllowed;
9912 - obj->serial = cp->serial;
9913 - obj->nDataChunks = cp->nDataChunks;
9915 - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
9916 - obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
9917 - else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
9918 - obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
9920 - if (obj->hdrChunk > 0)
9921 - obj->lazyLoaded = 1;
9927 -static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
9928 - __u32 level, int chunkOffset)
9929 +static int yaffs_gc_block(yaffs_dev_t *dev, int block,
9935 + int retVal = YAFFS_OK;
9937 - yaffs_Device *dev = in->myDev;
9939 - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
9941 - if (tnodeSize < sizeof(yaffs_Tnode))
9942 - tnodeSize = sizeof(yaffs_Tnode);
9943 + int isCheckpointBlock;
9944 + int matchingChunk;
9947 + int chunksBefore = yaffs_get_erased_chunks(dev);
9952 + yaffs_ext_tags tags;
9954 - for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
9955 - if (tn->internal[i]) {
9956 - ok = yaffs_CheckpointTnodeWorker(in,
9959 - (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
9962 - } else if (level == 0) {
9963 - __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
9964 - ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
9966 - ok = (yaffs_CheckpointWrite(dev, tn, tnodeSize) == tnodeSize);
9969 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, block);
9972 + yaffs_obj_t *object;
9975 + isCheckpointBlock = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
9977 -static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
9979 - __u32 endMarker = ~0;
9982 - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
9983 - ok = yaffs_CheckpointTnodeWorker(obj,
9984 - obj->variant.fileVariant.top,
9985 - obj->variant.fileVariant.topLevel,
9988 - ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
9989 - sizeof(endMarker));
9991 + T(YAFFS_TRACE_TRACING,
9992 + (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
9995 + bi->has_shrink_hdr,
9998 - return ok ? 1 : 0;
10000 + /*yaffs_verify_free_chunks(dev); */
10002 -static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
10006 - yaffs_Device *dev = obj->myDev;
10007 - yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
10010 - int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
10011 + if(bi->block_state == YAFFS_BLOCK_STATE_FULL)
10012 + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
10014 + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
10016 - if (tnodeSize < sizeof(yaffs_Tnode))
10017 - tnodeSize = sizeof(yaffs_Tnode);
10018 + dev->gc_disable = 1;
10020 - ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
10021 + if (isCheckpointBlock ||
10022 + !yaffs_still_some_chunks(dev, block)) {
10023 + T(YAFFS_TRACE_TRACING,
10025 + ("Collecting block %d that has no chunks in use" TENDSTR),
10027 + yaffs_block_became_dirty(dev, block);
10030 - while (ok && (~baseChunk)) {
10032 - /* Read level 0 tnode */
10033 + __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
10035 + yaffs_verify_blk(dev, bi, block);
10037 - tn = yaffs_GetTnodeRaw(dev);
10039 - ok = (yaffs_CheckpointRead(dev, tn, tnodeSize) == tnodeSize);
10042 + maxCopies = (wholeBlock) ? dev->param.chunks_per_block : 5;
10043 + oldChunk = block * dev->param.chunks_per_block + dev->gc_chunk;
10046 - ok = yaffs_AddOrFindLevel0Tnode(dev,
10050 + for (/* init already done */;
10051 + retVal == YAFFS_OK &&
10052 + dev->gc_chunk < dev->param.chunks_per_block &&
10053 + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
10055 + dev->gc_chunk++, oldChunk++) {
10056 + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
10059 - ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
10060 + /* This page is in use and might need to be copied off */
10065 - T(YAFFS_TRACE_CHECKPOINT, (
10066 - TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
10067 - nread, baseChunk, ok));
10070 - return ok ? 1 : 0;
10072 + yaffs_init_tags(&tags);
10074 + yaffs_rd_chunk_tags_nand(dev, oldChunk,
10077 -static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
10079 - yaffs_Object *obj;
10080 - yaffs_CheckpointObject cp;
10083 - struct ylist_head *lh;
10085 + yaffs_find_by_number(dev,
10088 + T(YAFFS_TRACE_GC_DETAIL,
10090 + ("Collecting chunk in block %d, %d %d %d " TENDSTR),
10091 + dev->gc_chunk, tags.obj_id, tags.chunk_id,
10094 - /* Iterate through the objects in each hash entry,
10095 - * dumping them to the checkpointing stream.
10097 + if (object && !yaffs_skip_verification(dev)) {
10098 + if (tags.chunk_id == 0)
10099 + matchingChunk = object->hdr_chunk;
10100 + else if (object->soft_del)
10101 + matchingChunk = oldChunk; /* Defeat the test */
10103 + matchingChunk = yaffs_find_chunk_in_file(object, tags.chunk_id, NULL);
10105 - for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
10106 - ylist_for_each(lh, &dev->objectBucket[i].list) {
10108 - obj = ylist_entry(lh, yaffs_Object, hashLink);
10109 - if (!obj->deferedFree) {
10110 - yaffs_ObjectToCheckpointObject(&cp, obj);
10111 - cp.structType = sizeof(cp);
10113 - T(YAFFS_TRACE_CHECKPOINT, (
10114 - TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
10115 - cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, (unsigned) obj));
10116 + if (oldChunk != matchingChunk)
10117 + T(YAFFS_TRACE_ERROR,
10118 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
10119 + oldChunk, matchingChunk, tags.obj_id, tags.chunk_id));
10121 - ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
10124 - if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
10125 - ok = yaffs_WriteCheckpointTnodes(obj);
10127 + T(YAFFS_TRACE_ERROR,
10129 + ("page %d in gc has no object: %d %d %d "
10130 + TENDSTR), oldChunk,
10131 + tags.obj_id, tags.chunk_id, tags.n_bytes));
10137 - /* Dump end of list */
10138 - memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
10139 - cp.structType = sizeof(cp);
10141 + object->deleted &&
10142 + object->soft_del &&
10143 + tags.chunk_id != 0) {
10144 + /* Data chunk in a soft deleted file, throw it away
10145 + * It's a soft deleted data chunk,
10146 + * No need to copy this, just forget about it and
10147 + * fix up the object.
10150 + /* Free chunks already includes softdeleted chunks.
10151 + * How ever this chunk is going to soon be really deleted
10152 + * which will increment free chunks.
10153 + * We have to decrement free chunks so this works out properly.
10155 + dev->n_free_chunks--;
10156 + bi->soft_del_pages--;
10158 + object->n_data_chunks--;
10161 - ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
10162 + if (object->n_data_chunks <= 0) {
10163 + /* remeber to clean up the object */
10164 + dev->gc_cleanup_list[dev->n_clean_ups] =
10166 + dev->n_clean_ups++;
10170 + /* Todo object && object->deleted && object->n_data_chunks == 0 */
10171 + /* Deleted object header with no data chunks.
10172 + * Can be discarded and the file deleted.
10174 + object->hdr_chunk = 0;
10175 + yaffs_free_tnode(object->my_dev,
10177 + file_variant.top);
10178 + object->variant.file_variant.top = NULL;
10179 + yaffs_generic_obj_del(object);
10181 - return ok ? 1 : 0;
10183 + } else if (object) {
10184 + /* It's either a data chunk in a live file or
10185 + * an ObjectHeader, so we're interested in it.
10186 + * NB Need to keep the ObjectHeaders of deleted files
10187 + * until the whole file has been deleted off
10189 + tags.serial_number++;
10191 -static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
10193 - yaffs_Object *obj;
10194 - yaffs_CheckpointObject cp;
10197 - yaffs_Object *hardList = NULL;
10198 + dev->n_gc_copies++;
10200 - while (ok && !done) {
10201 - ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
10202 - if (cp.structType != sizeof(cp)) {
10203 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
10204 - cp.structType, sizeof(cp), ok));
10208 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
10209 - cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
10211 - if (ok && cp.objectId == ~0)
10214 - obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
10216 - ok = yaffs_CheckpointObjectToObject(obj, &cp);
10219 - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10220 - ok = yaffs_ReadCheckpointTnodes(obj);
10221 - } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10222 - obj->hardLinks.next =
10223 - (struct ylist_head *) hardList;
10230 + if (tags.chunk_id == 0) {
10231 + /* It is an object Id,
10232 + * We need to nuke the shrinkheader flags first
10233 + * Also need to clean up shadowing.
10234 + * We no longer want the shrinkHeader flag since its work is done
10235 + * and if it is left in place it will mess up scanning.
10239 - yaffs_HardlinkFixup(dev, hardList);
10240 + yaffs_obj_header *oh;
10241 + oh = (yaffs_obj_header *)buffer;
10243 - return ok ? 1 : 0;
10245 + oh->is_shrink = 0;
10246 + tags.extra_is_shrink = 0;
10248 -static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
10250 - __u32 checkpointSum;
10252 + oh->shadows_obj = 0;
10253 + oh->inband_shadowed_obj_id = 0;
10254 + tags.extra_shadows = 0;
10256 + /* Update file size */
10257 + if(object->variant_type == YAFFS_OBJECT_TYPE_FILE){
10258 + oh->file_size = object->variant.file_variant.file_size;
10259 + tags.extra_length = oh->file_size;
10262 + yaffs_verify_oh(object, oh, &tags, 1);
10264 + yaffs_write_new_chunk(dev,(__u8 *) oh, &tags, 1);
10267 + yaffs_write_new_chunk(dev, buffer, &tags, 1);
10269 + if (newChunk < 0) {
10270 + retVal = YAFFS_FAIL;
10273 - yaffs_GetCheckpointSum(dev, &checkpointSum);
10274 + /* Ok, now fix up the Tnodes etc. */
10276 - ok = (yaffs_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
10277 + if (tags.chunk_id == 0) {
10278 + /* It's a header */
10279 + object->hdr_chunk = newChunk;
10280 + object->serial = tags.serial_number;
10282 + /* It's a data chunk */
10284 + ok = yaffs_put_chunk_in_file
10294 + if (retVal == YAFFS_OK)
10295 + yaffs_chunk_del(dev, oldChunk, mark_flash, __LINE__);
10302 -static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
10304 - __u32 checkpointSum0;
10305 - __u32 checkpointSum1;
10307 + yaffs_release_temp_buffer(dev, buffer, __LINE__);
10309 - yaffs_GetCheckpointSum(dev, &checkpointSum0);
10311 - ok = (yaffs_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
10317 - if (checkpointSum0 != checkpointSum1)
10319 + yaffs_verify_collected_blk(dev, bi, block);
10325 -static int yaffs_WriteCheckpointData(yaffs_Device *dev)
10328 + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
10330 + * The gc did not complete. Set block state back to FULL
10331 + * because checkpointing does not restore gc.
10333 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
10335 + /* The gc completed. */
10336 + /* Do any required cleanups */
10337 + for (i = 0; i < dev->n_clean_ups; i++) {
10338 + /* Time to delete the file too */
10340 + yaffs_find_by_number(dev,
10341 + dev->gc_cleanup_list[i]);
10343 + yaffs_free_tnode(dev,
10344 + object->variant.file_variant.
10346 + object->variant.file_variant.top = NULL;
10347 + T(YAFFS_TRACE_GC,
10349 + ("yaffs: About to finally delete object %d"
10350 + TENDSTR), object->obj_id));
10351 + yaffs_generic_obj_del(object);
10352 + object->my_dev->n_deleted_files--;
10355 - if (dev->skipCheckpointWrite || !dev->isYaffs2) {
10356 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
10362 - ok = yaffs_CheckpointOpen(dev, 1);
10365 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
10366 - ok = yaffs_WriteCheckpointValidityMarker(dev, 1);
10369 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
10370 - ok = yaffs_WriteCheckpointDevice(dev);
10373 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
10374 - ok = yaffs_WriteCheckpointObjects(dev);
10377 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
10378 - ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
10379 + chunksAfter = yaffs_get_erased_chunks(dev);
10380 + if (chunksBefore >= chunksAfter) {
10381 + T(YAFFS_TRACE_GC,
10383 + ("gc did not increase free chunks before %d after %d"
10384 + TENDSTR), chunksBefore, chunksAfter));
10386 + dev->gc_block = 0;
10387 + dev->gc_chunk = 0;
10388 + dev->n_clean_ups = 0;
10392 - ok = yaffs_WriteCheckpointSum(dev);
10394 - if (!yaffs_CheckpointClose(dev))
10398 - dev->isCheckpointed = 1;
10400 - dev->isCheckpointed = 0;
10401 + dev->gc_disable = 0;
10403 - return dev->isCheckpointed;
10407 -static int yaffs_ReadCheckpointData(yaffs_Device *dev)
10409 + * FindBlockForgarbageCollection is used to select the dirtiest block (or close enough)
10410 + * for garbage collection.
10413 +static unsigned yaffs_find_gc_block(yaffs_dev_t *dev,
10420 + unsigned selected = 0;
10421 + int prioritised = 0;
10422 + int prioritisedExists = 0;
10423 + yaffs_block_info_t *bi;
10426 - if (dev->skipCheckpointRead || !dev->isYaffs2) {
10427 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
10430 + /* First let's see if we need to grab a prioritised block */
10431 + if (dev->has_pending_prioritised_gc && !aggressive) {
10432 + dev->gc_dirtiest = 0;
10433 + bi = dev->block_info;
10434 + for (i = dev->internal_start_block;
10435 + i <= dev->internal_end_block && !selected;
10438 + if (bi->gc_prioritise) {
10439 + prioritisedExists = 1;
10440 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
10441 + yaffs_block_ok_for_gc(dev, bi)) {
10450 - ok = yaffs_CheckpointOpen(dev, 0); /* open for read */
10452 + * If there is a prioritised block and none was selected then
10453 + * this happened because there is at least one old dirty block gumming
10454 + * up the works. Let's gc the oldest dirty block.
10458 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
10459 - ok = yaffs_ReadCheckpointValidityMarker(dev, 1);
10462 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
10463 - ok = yaffs_ReadCheckpointDevice(dev);
10466 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
10467 - ok = yaffs_ReadCheckpointObjects(dev);
10470 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
10471 - ok = yaffs_ReadCheckpointValidityMarker(dev, 0);
10473 + if(prioritisedExists &&
10475 + dev->oldest_dirty_block > 0)
10476 + selected = dev->oldest_dirty_block;
10479 - ok = yaffs_ReadCheckpointSum(dev);
10480 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
10481 + if (!prioritisedExists) /* None found, so we can clear this */
10482 + dev->has_pending_prioritised_gc = 0;
10485 - if (!yaffs_CheckpointClose(dev))
10489 - dev->isCheckpointed = 1;
10491 - dev->isCheckpointed = 0;
10493 - return ok ? 1 : 0;
10494 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
10496 + * else (we're doing a leasurely gc), then we only bother to do this if the
10497 + * block has only a few pages in use.
10503 + int nBlocks = dev->internal_end_block - dev->internal_start_block + 1;
10505 + threshold = dev->param.chunks_per_block;
10506 + iterations = nBlocks;
10508 + int maxThreshold;
10510 -static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
10512 - if (dev->isCheckpointed ||
10513 - dev->blocksInCheckpoint > 0) {
10514 - dev->isCheckpointed = 0;
10515 - yaffs_CheckpointInvalidateStream(dev);
10516 - if (dev->superBlock && dev->markSuperBlockDirty)
10517 - dev->markSuperBlockDirty(dev->superBlock);
10521 + maxThreshold = dev->param.chunks_per_block/2;
10523 + maxThreshold = dev->param.chunks_per_block/8;
10525 + if(maxThreshold < YAFFS_GC_PASSIVE_THRESHOLD)
10526 + maxThreshold = YAFFS_GC_PASSIVE_THRESHOLD;
10528 -int yaffs_CheckpointSave(yaffs_Device *dev)
10530 + threshold = background ?
10531 + (dev->gc_not_done + 2) * 2 : 0;
10532 + if(threshold <YAFFS_GC_PASSIVE_THRESHOLD)
10533 + threshold = YAFFS_GC_PASSIVE_THRESHOLD;
10534 + if(threshold > maxThreshold)
10535 + threshold = maxThreshold;
10537 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
10538 + iterations = nBlocks / 16 + 1;
10539 + if (iterations > 100)
10540 + iterations = 100;
10543 - yaffs_VerifyObjects(dev);
10544 - yaffs_VerifyBlocks(dev);
10545 - yaffs_VerifyFreeChunks(dev);
10547 + i < iterations &&
10548 + (dev->gc_dirtiest < 1 ||
10549 + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
10551 + dev->gc_block_finder++;
10552 + if (dev->gc_block_finder < dev->internal_start_block ||
10553 + dev->gc_block_finder > dev->internal_end_block)
10554 + dev->gc_block_finder = dev->internal_start_block;
10556 - if (!dev->isCheckpointed) {
10557 - yaffs_InvalidateCheckpoint(dev);
10558 - yaffs_WriteCheckpointData(dev);
10560 + bi = yaffs_get_block_info(dev, dev->gc_block_finder);
10562 - T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
10563 + pagesUsed = bi->pages_in_use - bi->soft_del_pages;
10565 - return dev->isCheckpointed;
10567 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
10568 + pagesUsed < dev->param.chunks_per_block &&
10569 + (dev->gc_dirtiest < 1 || pagesUsed < dev->gc_pages_in_use) &&
10570 + yaffs_block_ok_for_gc(dev, bi)) {
10571 + dev->gc_dirtiest = dev->gc_block_finder;
10572 + dev->gc_pages_in_use = pagesUsed;
10576 -int yaffs_CheckpointRestore(yaffs_Device *dev)
10579 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
10580 + if(dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
10581 + selected = dev->gc_dirtiest;
10584 - retval = yaffs_ReadCheckpointData(dev);
10586 + * If nothing has been selected for a while, try selecting the oldest dirty
10587 + * because that's gumming up the works.
10590 - if (dev->isCheckpointed) {
10591 - yaffs_VerifyObjects(dev);
10592 - yaffs_VerifyBlocks(dev);
10593 - yaffs_VerifyFreeChunks(dev);
10594 + if(!selected && dev->param.is_yaffs2 &&
10595 + dev->gc_not_done >= ( background ? 10 : 20)){
10596 + yaffs2_find_oldest_dirty_seq(dev);
10597 + if(dev->oldest_dirty_block > 0) {
10598 + selected = dev->oldest_dirty_block;
10599 + dev->gc_dirtiest = selected;
10600 + dev->oldest_dirty_gc_count++;
10601 + bi = yaffs_get_block_info(dev, selected);
10602 + dev->gc_pages_in_use = bi->pages_in_use - bi->soft_del_pages;
10604 + dev->gc_not_done = 0;
10607 - T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
10609 + T(YAFFS_TRACE_GC,
10610 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR),
10612 + dev->param.chunks_per_block - dev->gc_pages_in_use,
10615 + dev->n_gc_blocks++;
10619 + dev->gc_dirtiest = 0;
10620 + dev->gc_pages_in_use = 0;
10621 + dev->gc_not_done = 0;
10622 + if(dev->refresh_skip > 0)
10623 + dev->refresh_skip--;
10625 + dev->gc_not_done++;
10626 + T(YAFFS_TRACE_GC,
10627 + (TSTR("GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s" TENDSTR),
10628 + dev->gc_block_finder, dev->gc_not_done,
10630 + dev->gc_dirtiest, dev->gc_pages_in_use,
10631 + dev->oldest_dirty_block,
10632 + background ? " bg" : ""));
10639 -/*--------------------- File read/write ------------------------
10640 - * Read and write have very similar structures.
10641 - * In general the read/write has three parts to it
10642 - * An incomplete chunk to start with (if the read/write is not chunk-aligned)
10643 - * Some complete chunks
10644 - * An incomplete chunk to end off with
10645 +/* New garbage collector
10646 + * If we're very low on erased blocks then we do aggressive garbage collection
10647 + * otherwise we do "leasurely" garbage collection.
10648 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
10649 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
10651 - * Curve-balls: the first chunk might also be the last chunk.
10652 + * The idea is to help clear out space in a more spread-out manner.
10653 + * Dunno if it really does anything useful.
10656 -int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
10658 +static int yaffs_check_gc(yaffs_dev_t *dev, int background)
10660 + int aggressive = 0;
10661 + int gcOk = YAFFS_OK;
10662 + int maxTries = 0;
10664 + int erasedChunks;
10665 + int checkpointBlockAdjust;
10672 - yaffs_ChunkCache *cache;
10674 - yaffs_Device *dev;
10679 - /* chunk = offset / dev->nDataBytesPerChunk + 1; */
10680 - /* start = offset % dev->nDataBytesPerChunk; */
10681 - yaffs_AddrToChunk(dev, offset, &chunk, &start);
10684 - /* OK now check for the curveball where the start and end are in
10685 - * the same chunk.
10687 - if ((start + n) < dev->nDataBytesPerChunk)
10690 - nToCopy = dev->nDataBytesPerChunk - start;
10692 - cache = yaffs_FindChunkCache(in, chunk);
10694 - /* If the chunk is already in the cache or it is less than a whole chunk
10695 - * or we're using inband tags then use the cache (if there is caching)
10696 - * else bypass the cache.
10698 - if (cache || nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
10699 - if (dev->nShortOpCaches > 0) {
10701 - /* If we can't find the data in the cache, then load it up. */
10702 + if(dev->param.gc_control &&
10703 + (dev->param.gc_control(dev) & 1) == 0)
10707 - cache = yaffs_GrabChunkCache(in->myDev);
10708 - cache->object = in;
10709 - cache->chunkId = chunk;
10710 - cache->dirty = 0;
10711 - cache->locked = 0;
10712 - yaffs_ReadChunkDataFromObject(in, chunk,
10715 - cache->nBytes = 0;
10717 + if (dev->gc_disable) {
10718 + /* Bail out so we don't get recursive gc */
10722 - yaffs_UseChunkCache(dev, cache, 0);
10723 + /* This loop should pass the first time.
10724 + * We'll only see looping here if the collection does not increase space.
10727 - cache->locked = 1;
10731 + checkpointBlockAdjust = yaffs_calc_checkpt_blocks_required(dev);
10733 - memcpy(buffer, &cache->data[start], nToCopy);
10734 + minErased = dev->param.n_reserved_blocks + checkpointBlockAdjust + 1;
10735 + erasedChunks = dev->n_erased_blocks * dev->param.chunks_per_block;
10737 - cache->locked = 0;
10739 - /* Read into the local buffer then copy..*/
10740 + /* If we need a block soon then do aggressive gc.*/
10741 + if (dev->n_erased_blocks < minErased)
10744 + if(!background && erasedChunks > (dev->n_free_chunks / 4))
10747 - __u8 *localBuffer =
10748 - yaffs_GetTempBuffer(dev, __LINE__);
10749 - yaffs_ReadChunkDataFromObject(in, chunk,
10751 + if(dev->gc_skip > 20)
10752 + dev->gc_skip = 20;
10753 + if(erasedChunks < dev->n_free_chunks/2 ||
10754 + dev->gc_skip < 1 ||
10763 - memcpy(buffer, &localBuffer[start], nToCopy);
10764 + dev->gc_skip = 5;
10766 + /* If we don't already have a block being gc'd then see if we should start another */
10768 - yaffs_ReleaseTempBuffer(dev, localBuffer,
10771 + if (dev->gc_block < 1 && !aggressive) {
10772 + dev->gc_block = yaffs2_find_refresh_block(dev);
10773 + dev->gc_chunk = 0;
10774 + dev->n_clean_ups=0;
10776 + if (dev->gc_block < 1) {
10777 + dev->gc_block = yaffs_find_gc_block(dev, aggressive, background);
10778 + dev->gc_chunk = 0;
10779 + dev->n_clean_ups=0;
10783 + if (dev->gc_block > 0) {
10786 + dev->passive_gc_count++;
10788 - /* A full chunk. Read directly into the supplied buffer. */
10789 - yaffs_ReadChunkDataFromObject(in, chunk, buffer);
10790 + T(YAFFS_TRACE_GC,
10792 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
10793 + dev->n_erased_blocks, aggressive));
10795 + gcOk = yaffs_gc_block(dev, dev->gc_block, aggressive);
10799 - offset += nToCopy;
10800 - buffer += nToCopy;
10801 - nDone += nToCopy;
10804 + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) && dev->gc_block > 0) {
10805 + T(YAFFS_TRACE_GC,
10807 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
10808 + TENDSTR), dev->n_erased_blocks, maxTries, dev->gc_block));
10810 + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
10811 + (dev->gc_block > 0) &&
10815 + return aggressive ? gcOk : YAFFS_OK;
10818 -int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
10819 - int nBytes, int writeThrough)
10822 + * Garbage collects. Intended to be called from a background thread.
10823 + * Returns non-zero if at least half the free chunks are erased.
10825 +int yaffs_bg_gc(yaffs_dev_t *dev, unsigned urgency)
10827 + int erasedChunks = dev->n_erased_blocks * dev->param.chunks_per_block;
10834 - int nToWriteBack;
10835 - int startOfWrite = offset;
10836 - int chunkWritten = 0;
10837 - __u32 nBytesRead;
10838 - __u32 chunkStart;
10839 + T(YAFFS_TRACE_BACKGROUND, (TSTR("Background gc %u" TENDSTR),urgency));
10841 - yaffs_Device *dev;
10842 + yaffs_check_gc(dev, 1);
10843 + return erasedChunks > dev->n_free_chunks/2;
10847 +/*------------------------- TAGS --------------------------------*/
10849 - while (n > 0 && chunkWritten >= 0) {
10850 - /* chunk = offset / dev->nDataBytesPerChunk + 1; */
10851 - /* start = offset % dev->nDataBytesPerChunk; */
10852 - yaffs_AddrToChunk(dev, offset, &chunk, &start);
10853 +static int yaffs_tags_match(const yaffs_ext_tags *tags, int obj_id,
10854 + int chunkInObject)
10856 + return (tags->chunk_id == chunkInObject &&
10857 + tags->obj_id == obj_id && !tags->is_deleted) ? 1 : 0;
10859 - if (chunk * dev->nDataBytesPerChunk + start != offset ||
10860 - start >= dev->nDataBytesPerChunk) {
10861 - T(YAFFS_TRACE_ERROR, (
10862 - TSTR("AddrToChunk of offset %d gives chunk %d start %d"
10864 - (int)offset, chunk, start));
10869 - /* OK now check for the curveball where the start and end are in
10870 - * the same chunk.
10873 - if ((start + n) < dev->nDataBytesPerChunk) {
10875 +/*-------------------- Data file manipulation -----------------*/
10877 - /* Now folks, to calculate how many bytes to write back....
10878 - * If we're overwriting and not writing to then end of file then
10879 - * we need to write back as much as was there before.
10881 +static int yaffs_find_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
10882 + yaffs_ext_tags *tags)
10884 + /*Get the Tnode, then get the level 0 offset chunk offset */
10885 + yaffs_tnode_t *tn;
10886 + int theChunk = -1;
10887 + yaffs_ext_tags localTags;
10890 - chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
10891 + yaffs_dev_t *dev = in->my_dev;
10893 - if (chunkStart > in->variant.fileVariant.fileSize)
10894 - nBytesRead = 0; /* Past end of file */
10896 - nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
10898 + /* Passed a NULL, so use our own tags space */
10899 + tags = &localTags;
10902 - if (nBytesRead > dev->nDataBytesPerChunk)
10903 - nBytesRead = dev->nDataBytesPerChunk;
10904 + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
10908 - (start + n)) ? nBytesRead : (start + n);
10910 + theChunk = yaffs_get_group_base(dev, tn, inode_chunk);
10912 - if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
10915 + yaffs_find_chunk_in_group(dev, theChunk, tags, in->obj_id,
10922 - nToCopy = dev->nDataBytesPerChunk - start;
10923 - nToWriteBack = dev->nDataBytesPerChunk;
10925 +static int yaffs_find_del_file_chunk(yaffs_obj_t *in, int inode_chunk,
10926 + yaffs_ext_tags *tags)
10928 + /* Get the Tnode, then get the level 0 offset chunk offset */
10929 + yaffs_tnode_t *tn;
10930 + int theChunk = -1;
10931 + yaffs_ext_tags localTags;
10933 - if (nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
10934 - /* An incomplete start or end chunk (or maybe both start and end chunk),
10935 - * or we're using inband tags, so we want to use the cache buffers.
10937 - if (dev->nShortOpCaches > 0) {
10938 - yaffs_ChunkCache *cache;
10939 - /* If we can't find the data in the cache, then load the cache */
10940 - cache = yaffs_FindChunkCache(in, chunk);
10941 + yaffs_dev_t *dev = in->my_dev;
10945 - && yaffs_CheckSpaceForAllocation(in->
10947 - cache = yaffs_GrabChunkCache(in->myDev);
10948 - cache->object = in;
10949 - cache->chunkId = chunk;
10950 - cache->dirty = 0;
10951 - cache->locked = 0;
10952 - yaffs_ReadChunkDataFromObject(in, chunk,
10955 - } else if (cache &&
10957 - !yaffs_CheckSpaceForAllocation(in->myDev)) {
10958 - /* Drop the cache if it was a read cache item and
10959 - * no space check has been made for it.
10964 + /* Passed a NULL, so use our own tags space */
10965 + tags = &localTags;
10969 - yaffs_UseChunkCache(dev, cache, 1);
10970 - cache->locked = 1;
10971 + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
10975 - memcpy(&cache->data[start], buffer,
10977 + theChunk = yaffs_get_group_base(dev, tn, inode_chunk);
10980 + yaffs_find_chunk_in_group(dev, theChunk, tags, in->obj_id,
10983 - cache->locked = 0;
10984 - cache->nBytes = nToWriteBack;
10985 + /* Delete the entry in the filestructure (if found) */
10986 + if (retVal != -1)
10987 + yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
10990 - if (writeThrough) {
10992 - yaffs_WriteChunkDataToObject
10995 - cache->data, cache->nBytes,
10997 - cache->dirty = 0;
11003 - chunkWritten = -1; /* fail the write */
11006 - /* An incomplete start or end chunk (or maybe both start and end chunk)
11007 - * Read into the local buffer then copy, then copy over and write back.
11010 - __u8 *localBuffer =
11011 - yaffs_GetTempBuffer(dev, __LINE__);
11012 +int yaffs_put_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
11013 + int nand_chunk, int in_scan)
11015 + /* NB in_scan is zero unless scanning.
11016 + * For forward scanning, in_scan is > 0;
11017 + * for backward scanning in_scan is < 0
11019 + * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
11022 - yaffs_ReadChunkDataFromObject(in, chunk,
11024 + yaffs_tnode_t *tn;
11025 + yaffs_dev_t *dev = in->my_dev;
11026 + int existingChunk;
11027 + yaffs_ext_tags existingTags;
11028 + yaffs_ext_tags newTags;
11029 + unsigned existingSerial, newSerial;
11031 + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
11032 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
11033 + * If it is not during Scanning then something went wrong!
11036 + T(YAFFS_TRACE_ERROR,
11038 + ("yaffs tragedy:attempt to put data chunk into a non-file"
11043 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
11047 - memcpy(&localBuffer[start], buffer, nToCopy);
11048 + tn = yaffs_add_find_tnode_0(dev,
11049 + &in->variant.file_variant,
11053 + return YAFFS_FAIL;
11056 + /* Dummy insert, bail now */
11060 - yaffs_WriteChunkDataToObject(in, chunk,
11064 + existingChunk = yaffs_get_group_base(dev, tn, inode_chunk);
11066 - yaffs_ReleaseTempBuffer(dev, localBuffer,
11068 + if (in_scan != 0) {
11069 + /* If we're scanning then we need to test for duplicates
11070 + * NB This does not need to be efficient since it should only ever
11071 + * happen when the power fails during a write, then only one
11072 + * chunk should ever be affected.
11074 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
11075 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
11078 + if (existingChunk > 0) {
11079 + /* NB Right now existing chunk will not be real chunk_id if the chunk group size > 1
11080 + * thus we have to do a FindChunkInFile to get the real chunk id.
11082 + * We have a duplicate now we need to decide which one to use:
11084 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
11085 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
11086 + * YAFFS1: Get both sets of tags and compare serial numbers.
11089 + if (in_scan > 0) {
11090 + /* Only do this for forward scanning */
11091 + yaffs_rd_chunk_tags_nand(dev,
11095 + /* Do a proper find */
11097 + yaffs_find_chunk_in_file(in, inode_chunk,
11102 - /* A full chunk. Write directly from the supplied buffer. */
11103 + if (existingChunk <= 0) {
11104 + /*Hoosterman - how did this happen? */
11106 + T(YAFFS_TRACE_ERROR,
11108 + ("yaffs tragedy: existing chunk < 0 in scan"
11114 - yaffs_WriteChunkDataToObject(in, chunk, buffer,
11115 - dev->nDataBytesPerChunk,
11117 + /* NB The deleted flags should be false, otherwise the chunks will
11118 + * not be loaded during a scan
11121 - /* Since we've overwritten the cached data, we better invalidate it. */
11122 - yaffs_InvalidateChunkCache(in, chunk);
11124 + if (in_scan > 0) {
11125 + newSerial = newTags.serial_number;
11126 + existingSerial = existingTags.serial_number;
11129 - if (chunkWritten >= 0) {
11131 - offset += nToCopy;
11132 - buffer += nToCopy;
11133 - nDone += nToCopy;
11134 + if ((in_scan > 0) &&
11135 + (existingChunk <= 0 ||
11136 + ((existingSerial + 1) & 3) == newSerial)) {
11137 + /* Forward scanning.
11139 + * Delete the old one and drop through to update the tnode
11141 + yaffs_chunk_del(dev, existingChunk, 1,
11144 + /* Backward scanning or we want to use the existing one
11146 + * Delete the new one and return early so that the tnode isn't changed
11148 + yaffs_chunk_del(dev, nand_chunk, 1,
11156 - /* Update file object */
11158 - if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
11159 - in->variant.fileVariant.fileSize = (startOfWrite + nDone);
11160 + if (existingChunk == 0)
11161 + in->n_data_chunks++;
11164 + yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
11170 +static int yaffs_rd_data_obj(yaffs_obj_t *in, int inode_chunk,
11173 + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
11175 -/* ---------------------- File resizing stuff ------------------ */
11176 + if (nand_chunk >= 0)
11177 + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
11180 + T(YAFFS_TRACE_NANDACCESS,
11181 + (TSTR("Chunk %d not found zero instead" TENDSTR),
11183 + /* get sane (zero) data if you read a hole */
11184 + memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
11190 -static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
11191 +void yaffs_chunk_del(yaffs_dev_t *dev, int chunk_id, int mark_flash, int lyn)
11195 + yaffs_ext_tags tags;
11196 + yaffs_block_info_t *bi;
11198 - yaffs_Device *dev = in->myDev;
11199 - int oldFileSize = in->variant.fileVariant.fileSize;
11200 + if (chunk_id <= 0)
11203 - int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
11204 + dev->n_deletions++;
11205 + block = chunk_id / dev->param.chunks_per_block;
11206 + page = chunk_id % dev->param.chunks_per_block;
11208 - int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
11209 - dev->nDataBytesPerChunk;
11213 - /* Delete backwards so that we don't end up with holes if
11214 - * power is lost part-way through the operation.
11215 + if (!yaffs_check_chunk_bit(dev, block, page))
11216 + T(YAFFS_TRACE_VERIFY,
11217 + (TSTR("Deleting invalid chunk %d"TENDSTR),
11220 + bi = yaffs_get_block_info(dev, block);
11222 + yaffs2_update_oldest_dirty_seq(dev, block, bi);
11224 + T(YAFFS_TRACE_DELETION,
11225 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunk_id));
11227 + if (!dev->param.is_yaffs2 && mark_flash &&
11228 + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
11230 + yaffs_init_tags(&tags);
11232 + tags.is_deleted = 1;
11234 + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
11235 + yaffs_handle_chunk_update(dev, chunk_id, &tags);
11237 + dev->n_unmarked_deletions++;
11240 + /* Pull out of the management area.
11241 + * If the whole block became dirty, this will kick off an erasure.
11243 - for (i = lastDel; i >= startDel; i--) {
11244 - /* NB this could be optimised somewhat,
11245 - * eg. could retrieve the tags and write them without
11246 - * using yaffs_DeleteChunk
11248 + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
11249 + bi->block_state == YAFFS_BLOCK_STATE_FULL ||
11250 + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
11251 + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
11252 + dev->n_free_chunks++;
11254 - chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
11255 - if (chunkId > 0) {
11257 - (dev->internalStartBlock * dev->nChunksPerBlock)
11259 - ((dev->internalEndBlock +
11260 - 1) * dev->nChunksPerBlock)) {
11261 - T(YAFFS_TRACE_ALWAYS,
11262 - (TSTR("Found daft chunkId %d for %d" TENDSTR),
11265 - in->nDataChunks--;
11266 - yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
11268 + yaffs_clear_chunk_bit(dev, block, page);
11270 + bi->pages_in_use--;
11272 + if (bi->pages_in_use == 0 &&
11273 + !bi->has_shrink_hdr &&
11274 + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
11275 + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
11276 + yaffs_block_became_dirty(dev, block);
11283 -int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
11284 +static int yaffs_wr_data_obj(yaffs_obj_t *in, int inode_chunk,
11285 + const __u8 *buffer, int n_bytes,
11288 + /* Find old chunk Need to do this to get serial number
11289 + * Write new one and patch into tree.
11290 + * Invalidate old tags.
11293 - int oldFileSize = in->variant.fileVariant.fileSize;
11294 - __u32 newSizeOfPartialChunk;
11295 - int newFullChunks;
11297 + yaffs_ext_tags prevTags;
11299 - yaffs_Device *dev = in->myDev;
11301 + yaffs_ext_tags newTags;
11303 - yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
11304 + yaffs_dev_t *dev = in->my_dev;
11306 - yaffs_FlushFilesChunkCache(in);
11307 - yaffs_InvalidateWholeChunkCache(in);
11308 + yaffs_check_gc(dev,0);
11310 - yaffs_CheckGarbageCollection(dev);
11311 + /* Get the previous chunk at this location in the file if it exists.
11312 + * If it does not exist then put a zero into the tree. This creates
11313 + * the tnode now, rather than later when it is harder to clean up.
11315 + prevChunkId = yaffs_find_chunk_in_file(in, inode_chunk, &prevTags);
11316 + if(prevChunkId < 1 &&
11317 + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
11320 - if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
11321 - return YAFFS_FAIL;
11322 + /* Set up new tags */
11323 + yaffs_init_tags(&newTags);
11325 - if (newSize == oldFileSize)
11327 + newTags.chunk_id = inode_chunk;
11328 + newTags.obj_id = in->obj_id;
11329 + newTags.serial_number =
11330 + (prevChunkId > 0) ? prevTags.serial_number + 1 : 1;
11331 + newTags.n_bytes = n_bytes;
11333 - if (newSize < oldFileSize) {
11334 + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
11335 + T(YAFFS_TRACE_ERROR,
11336 + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), n_bytes));
11342 + yaffs_write_new_chunk(dev, buffer, &newTags,
11345 - yaffs_PruneResizedChunks(in, newSize);
11346 + if (newChunkId > 0) {
11347 + yaffs_put_chunk_in_file(in, inode_chunk, newChunkId, 0);
11349 - if (newSizeOfPartialChunk != 0) {
11350 - int lastChunk = 1 + newFullChunks;
11351 + if (prevChunkId > 0)
11352 + yaffs_chunk_del(dev, prevChunkId, 1, __LINE__);
11354 - __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
11355 + yaffs_verify_file_sane(in);
11357 + return newChunkId;
11359 - /* Got to read and rewrite the last chunk with its new size and zero pad */
11360 - yaffs_ReadChunkDataFromObject(in, lastChunk,
11364 - memset(localBuffer + newSizeOfPartialChunk, 0,
11365 - dev->nDataBytesPerChunk - newSizeOfPartialChunk);
11366 +/* UpdateObjectHeader updates the header on NAND for an object.
11367 + * If name is not NULL, then that new name is used.
11369 +int yaffs_update_oh(yaffs_obj_t *in, const YCHAR *name, int force,
11370 + int is_shrink, int shadows, yaffs_xattr_mod *xmod)
11373 - yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
11374 - newSizeOfPartialChunk, 1);
11375 + yaffs_block_info_t *bi;
11377 - yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
11379 + yaffs_dev_t *dev = in->my_dev;
11381 - in->variant.fileVariant.fileSize = newSize;
11386 - yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
11388 - /* newsSize > oldFileSize */
11389 - in->variant.fileVariant.fileSize = newSize;
11392 + yaffs_ext_tags newTags;
11393 + yaffs_ext_tags oldTags;
11394 + const YCHAR *alias = NULL;
11396 + __u8 *buffer = NULL;
11397 + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
11399 - /* Write a new object header.
11400 - * show we've shrunk the file, if need be
11401 - * Do this only if the file is not in the deleted directories.
11403 - if (in->parent &&
11404 - in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
11405 - in->parent->objectId != YAFFS_OBJECTID_DELETED)
11406 - yaffs_UpdateObjectHeader(in, NULL, 0,
11407 - (newSize < oldFileSize) ? 1 : 0, 0);
11408 + yaffs_obj_header *oh = NULL;
11412 + yaffs_strcpy(old_name, _Y("silly old name"));
11414 -loff_t yaffs_GetFileSize(yaffs_Object *obj)
11416 - obj = yaffs_GetEquivalentObject(obj);
11418 - switch (obj->variantType) {
11419 - case YAFFS_OBJECT_TYPE_FILE:
11420 - return obj->variant.fileVariant.fileSize;
11421 - case YAFFS_OBJECT_TYPE_SYMLINK:
11422 - return yaffs_strlen(obj->variant.symLinkVariant.alias);
11428 + in == dev->root_dir || /* The root_dir should also be saved */
11431 + yaffs_check_gc(dev,0);
11432 + yaffs_check_obj_details_loaded(in);
11434 + buffer = yaffs_get_temp_buffer(in->my_dev, __LINE__);
11435 + oh = (yaffs_obj_header *) buffer;
11437 + prevChunkId = in->hdr_chunk;
11439 + if (prevChunkId > 0) {
11440 + result = yaffs_rd_chunk_tags_nand(dev, prevChunkId,
11441 + buffer, &oldTags);
11443 + yaffs_verify_oh(in, oh, &oldTags, 0);
11445 + memcpy(old_name, oh->name, sizeof(oh->name));
11446 + memset(buffer, 0xFF, sizeof(yaffs_obj_header));
11448 + memset(buffer, 0xFF, dev->data_bytes_per_chunk);
11450 + oh->type = in->variant_type;
11451 + oh->yst_mode = in->yst_mode;
11452 + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
11454 -int yaffs_FlushFile(yaffs_Object *in, int updateTime)
11458 - yaffs_FlushFilesChunkCache(in);
11459 - if (updateTime) {
11460 #ifdef CONFIG_YAFFS_WINCE
11461 - yfsd_WinFileTimeNow(in->win_mtime);
11462 + oh->win_atime[0] = in->win_atime[0];
11463 + oh->win_ctime[0] = in->win_ctime[0];
11464 + oh->win_mtime[0] = in->win_mtime[0];
11465 + oh->win_atime[1] = in->win_atime[1];
11466 + oh->win_ctime[1] = in->win_ctime[1];
11467 + oh->win_mtime[1] = in->win_mtime[1];
11469 + oh->yst_uid = in->yst_uid;
11470 + oh->yst_gid = in->yst_gid;
11471 + oh->yst_atime = in->yst_atime;
11472 + oh->yst_mtime = in->yst_mtime;
11473 + oh->yst_ctime = in->yst_ctime;
11474 + oh->yst_rdev = in->yst_rdev;
11477 + oh->parent_obj_id = in->parent->obj_id;
11479 + oh->parent_obj_id = 0;
11481 + if (name && *name) {
11482 + memset(oh->name, 0, sizeof(oh->name));
11483 + yaffs_load_oh_from_name(dev,oh->name,name);
11484 + } else if (prevChunkId > 0)
11485 + memcpy(oh->name, old_name, sizeof(oh->name));
11487 + memset(oh->name, 0, sizeof(oh->name));
11489 - in->yst_mtime = Y_CURRENT_TIME;
11490 + oh->is_shrink = is_shrink;
11493 + switch (in->variant_type) {
11494 + case YAFFS_OBJECT_TYPE_UNKNOWN:
11495 + /* Should not happen */
11497 + case YAFFS_OBJECT_TYPE_FILE:
11499 + (oh->parent_obj_id == YAFFS_OBJECTID_DELETED
11500 + || oh->parent_obj_id ==
11501 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
11502 + file_variant.file_size;
11504 + case YAFFS_OBJECT_TYPE_HARDLINK:
11506 + in->variant.hardlink_variant.equiv_id;
11508 + case YAFFS_OBJECT_TYPE_SPECIAL:
11511 + case YAFFS_OBJECT_TYPE_DIRECTORY:
11514 + case YAFFS_OBJECT_TYPE_SYMLINK:
11515 + alias = in->variant.symlink_variant.alias;
11517 + alias = _Y("no alias");
11518 + yaffs_strncpy(oh->alias,
11520 + YAFFS_MAX_ALIAS_LENGTH);
11521 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
11525 - retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
11526 - 0) ? YAFFS_OK : YAFFS_FAIL;
11528 - retVal = YAFFS_OK;
11530 + /* process any xattrib modifications */
11532 + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
11538 + yaffs_init_tags(&newTags);
11540 + newTags.chunk_id = 0;
11541 + newTags.obj_id = in->obj_id;
11542 + newTags.serial_number = in->serial;
11544 -static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
11546 + /* Add extra info for file header */
11548 - /* First off, invalidate the file's data in the cache, without flushing. */
11549 - yaffs_InvalidateWholeChunkCache(in);
11550 + newTags.extra_available = 1;
11551 + newTags.extra_parent_id = oh->parent_obj_id;
11552 + newTags.extra_length = oh->file_size;
11553 + newTags.extra_is_shrink = oh->is_shrink;
11554 + newTags.extra_equiv_id = oh->equiv_id;
11555 + newTags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
11556 + newTags.extra_obj_type = in->variant_type;
11558 - if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
11559 - /* Move to the unlinked directory so we have a record that it was deleted. */
11560 - yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
11561 + yaffs_verify_oh(in, oh, &newTags, 1);
11564 + /* Create new chunk in NAND */
11566 + yaffs_write_new_chunk(dev, buffer, &newTags,
11567 + (prevChunkId > 0) ? 1 : 0);
11569 - yaffs_RemoveObjectFromDirectory(in);
11570 - yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
11571 - in->hdrChunk = 0;
11572 + if (newChunkId >= 0) {
11574 - yaffs_FreeObject(in);
11576 + in->hdr_chunk = newChunkId;
11579 + if (prevChunkId > 0) {
11580 + yaffs_chunk_del(dev, prevChunkId, 1,
11584 -/* yaffs_DeleteFile deletes the whole file data
11585 - * and the inode associated with the file.
11586 - * It does not delete the links associated with the file.
11588 -static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
11590 + if (!yaffs_obj_cache_dirty(in))
11594 - int immediateDeletion = 0;
11595 + /* If this was a shrink, then mark the block that the chunk lives on */
11597 + bi = yaffs_get_block_info(in->my_dev,
11598 + newChunkId / in->my_dev->param.chunks_per_block);
11599 + bi->has_shrink_hdr = 1;
11603 - if (!in->myInode)
11604 - immediateDeletion = 1;
11606 - if (in->inUse <= 0)
11607 - immediateDeletion = 1;
11611 + retVal = newChunkId;
11613 - if (immediateDeletion) {
11615 - yaffs_ChangeObjectName(in, in->myDev->deletedDir,
11616 - _Y("deleted"), 0, 0);
11617 - T(YAFFS_TRACE_TRACING,
11618 - (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
11621 - in->myDev->nDeletedFiles++;
11622 - if (1 || in->myDev->isYaffs2)
11623 - yaffs_ResizeFile(in, 0);
11624 - yaffs_SoftDeleteFile(in);
11627 - yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
11628 - _Y("unlinked"), 0, 0);
11632 + yaffs_release_temp_buffer(dev, buffer, __LINE__);
11637 -int yaffs_DeleteFile(yaffs_Object *in)
11639 - int retVal = YAFFS_OK;
11640 - int deleted = in->deleted;
11642 - yaffs_ResizeFile(in, 0);
11644 - if (in->nDataChunks > 0) {
11645 - /* Use soft deletion if there is data in the file.
11646 - * That won't be the case if it has been resized to zero.
11648 - if (!in->unlinked)
11649 - retVal = yaffs_UnlinkFileIfNeeded(in);
11651 - if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
11654 - in->myDev->nDeletedFiles++;
11655 - yaffs_SoftDeleteFile(in);
11657 - return deleted ? YAFFS_OK : YAFFS_FAIL;
11659 - /* The file has no data chunks so we toss it immediately */
11660 - yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
11661 - in->variant.fileVariant.top = NULL;
11662 - yaffs_DoGenericObjectDeletion(in);
11667 +/*------------------------ Short Operations Cache ----------------------------------------
11668 + * In many situations where there is no high level buffering (eg WinCE) a lot of
11669 + * reads might be short sequential reads, and a lot of writes may be short
11670 + * sequential writes. eg. scanning/writing a jpeg file.
11671 + * In these cases, a short read/write cache can provide a huge perfomance benefit
11672 + * with dumb-as-a-rock code.
11673 + * In Linux, the page cache provides read buffering aand the short op cache provides write
11676 + * There are a limited number (~10) of cache chunks per device so that we don't
11677 + * need a very intelligent search.
11680 -static int yaffs_DeleteDirectory(yaffs_Object *in)
11681 +static int yaffs_obj_cache_dirty(yaffs_obj_t *obj)
11683 - /* First check that the directory is empty. */
11684 - if (ylist_empty(&in->variant.directoryVariant.children))
11685 - return yaffs_DoGenericObjectDeletion(in);
11686 + yaffs_dev_t *dev = obj->my_dev;
11688 + yaffs_cache_t *cache;
11689 + int nCaches = obj->my_dev->param.n_caches;
11691 - return YAFFS_FAIL;
11692 + for (i = 0; i < nCaches; i++) {
11693 + cache = &dev->cache[i];
11694 + if (cache->object == obj &&
11702 -static int yaffs_DeleteSymLink(yaffs_Object *in)
11704 - YFREE(in->variant.symLinkVariant.alias);
11706 - return yaffs_DoGenericObjectDeletion(in);
11709 -static int yaffs_DeleteHardLink(yaffs_Object *in)
11710 +static void yaffs_flush_file_cache(yaffs_obj_t *obj)
11712 - /* remove this hardlink from the list assocaited with the equivalent
11715 - ylist_del_init(&in->hardLinks);
11716 - return yaffs_DoGenericObjectDeletion(in);
11718 + yaffs_dev_t *dev = obj->my_dev;
11719 + int lowest = -99; /* Stop compiler whining. */
11721 + yaffs_cache_t *cache;
11722 + int chunkWritten = 0;
11723 + int nCaches = obj->my_dev->param.n_caches;
11725 -int yaffs_DeleteObject(yaffs_Object *obj)
11728 - switch (obj->variantType) {
11729 - case YAFFS_OBJECT_TYPE_FILE:
11730 - retVal = yaffs_DeleteFile(obj);
11732 - case YAFFS_OBJECT_TYPE_DIRECTORY:
11733 - return yaffs_DeleteDirectory(obj);
11735 - case YAFFS_OBJECT_TYPE_SYMLINK:
11736 - retVal = yaffs_DeleteSymLink(obj);
11738 - case YAFFS_OBJECT_TYPE_HARDLINK:
11739 - retVal = yaffs_DeleteHardLink(obj);
11741 - case YAFFS_OBJECT_TYPE_SPECIAL:
11742 - retVal = yaffs_DoGenericObjectDeletion(obj);
11744 - case YAFFS_OBJECT_TYPE_UNKNOWN:
11746 - break; /* should not happen. */
11748 + if (nCaches > 0) {
11754 + /* Find the dirty cache for this object with the lowest chunk id. */
11755 + for (i = 0; i < nCaches; i++) {
11756 + if (dev->cache[i].object == obj &&
11757 + dev->cache[i].dirty) {
11759 + || dev->cache[i].chunk_id <
11761 + cache = &dev->cache[i];
11762 + lowest = cache->chunk_id;
11767 -static int yaffs_UnlinkWorker(yaffs_Object *obj)
11769 + if (cache && !cache->locked) {
11770 + /* Write it out and free it up */
11772 - int immediateDeletion = 0;
11774 + yaffs_wr_data_obj(cache->object,
11779 + cache->dirty = 0;
11780 + cache->object = NULL;
11784 - if (!obj->myInode)
11785 - immediateDeletion = 1;
11787 - if (obj->inUse <= 0)
11788 - immediateDeletion = 1;
11790 + } while (cache && chunkWritten > 0);
11792 - if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
11793 - return yaffs_DeleteHardLink(obj);
11794 - } else if (!ylist_empty(&obj->hardLinks)) {
11795 - /* Curve ball: We're unlinking an object that has a hardlink.
11797 - * This problem arises because we are not strictly following
11798 - * The Linux link/inode model.
11800 - * We can't really delete the object.
11801 - * Instead, we do the following:
11802 - * - Select a hardlink.
11803 - * - Unhook it from the hard links
11804 - * - Unhook it from its parent directory (so that the rename can work)
11805 - * - Rename the object to the hardlink's name.
11806 - * - Delete the hardlink
11809 + /* Hoosterman, disk full while writing cache out. */
11810 + T(YAFFS_TRACE_ERROR,
11811 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
11813 - yaffs_Object *hl;
11815 - YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11819 - hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
11822 - ylist_del_init(&hl->hardLinks);
11823 - ylist_del_init(&hl->siblings);
11824 +/*yaffs_flush_whole_cache(dev)
11829 - yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
11830 +void yaffs_flush_whole_cache(yaffs_dev_t *dev)
11832 + yaffs_obj_t *obj;
11833 + int nCaches = dev->param.n_caches;
11836 - retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
11837 + /* Find a dirty object in the cache and flush it...
11838 + * until there are no further dirty objects.
11842 + for (i = 0; i < nCaches && !obj; i++) {
11843 + if (dev->cache[i].object &&
11844 + dev->cache[i].dirty)
11845 + obj = dev->cache[i].object;
11847 - if (retVal == YAFFS_OK)
11848 - retVal = yaffs_DoGenericObjectDeletion(hl);
11851 + yaffs_flush_file_cache(obj);
11856 - } else if (immediateDeletion) {
11857 - switch (obj->variantType) {
11858 - case YAFFS_OBJECT_TYPE_FILE:
11859 - return yaffs_DeleteFile(obj);
11861 - case YAFFS_OBJECT_TYPE_DIRECTORY:
11862 - return yaffs_DeleteDirectory(obj);
11864 - case YAFFS_OBJECT_TYPE_SYMLINK:
11865 - return yaffs_DeleteSymLink(obj);
11867 - case YAFFS_OBJECT_TYPE_SPECIAL:
11868 - return yaffs_DoGenericObjectDeletion(obj);
11870 - case YAFFS_OBJECT_TYPE_HARDLINK:
11871 - case YAFFS_OBJECT_TYPE_UNKNOWN:
11873 - return YAFFS_FAIL;
11876 - return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
11877 - _Y("unlinked"), 0, 0);
11881 -static int yaffs_UnlinkObject(yaffs_Object *obj)
11882 +/* Grab us a cache chunk for use.
11883 + * First look for an empty one.
11884 + * Then look for the least recently used non-dirty one.
11885 + * Then look for the least recently used dirty one...., flush and look again.
11887 +static yaffs_cache_t *yaffs_grab_chunk_worker(yaffs_dev_t *dev)
11891 - if (obj && obj->unlinkAllowed)
11892 - return yaffs_UnlinkWorker(obj);
11894 - return YAFFS_FAIL;
11897 -int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
11899 - yaffs_Object *obj;
11900 + if (dev->param.n_caches > 0) {
11901 + for (i = 0; i < dev->param.n_caches; i++) {
11902 + if (!dev->cache[i].object)
11903 + return &dev->cache[i];
11907 - obj = yaffs_FindObjectByName(dir, name);
11908 - return yaffs_UnlinkObject(obj);
11912 -/*----------------------- Initialisation Scanning ---------------------- */
11914 -static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
11915 - int backwardScanning)
11916 +static yaffs_cache_t *yaffs_grab_chunk_cache(yaffs_dev_t *dev)
11918 - yaffs_Object *obj;
11919 + yaffs_cache_t *cache;
11920 + yaffs_obj_t *theObj;
11925 - if (!backwardScanning) {
11926 - /* Handle YAFFS1 forward scanning case
11927 - * For YAFFS1 we always do the deletion
11929 + if (dev->param.n_caches > 0) {
11930 + /* Try find a non-dirty one... */
11933 - /* Handle YAFFS2 case (backward scanning)
11934 - * If the shadowed object exists then ignore.
11936 - if (yaffs_FindObjectByNumber(dev, objId))
11939 + cache = yaffs_grab_chunk_worker(dev);
11941 - /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
11942 - * We put it in unlinked dir to be cleaned up after the scanning
11945 - yaffs_FindOrCreateObjectByNumber(dev, objId,
11946 - YAFFS_OBJECT_TYPE_FILE);
11949 - yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
11950 - obj->variant.fileVariant.shrinkSize = 0;
11951 - obj->valid = 1; /* So that we don't read any other info for this file */
11953 + /* They were all dirty, find the last recently used object and flush
11954 + * its cache, then find again.
11955 + * NB what's here is not very accurate, we actually flush the object
11956 + * the last recently used page.
11960 + /* With locking we can't assume we can use entry zero */
11965 -} yaffs_BlockIndex;
11971 + for (i = 0; i < dev->param.n_caches; i++) {
11972 + if (dev->cache[i].object &&
11973 + !dev->cache[i].locked &&
11974 + (dev->cache[i].last_use < usage || !cache)) {
11975 + usage = dev->cache[i].last_use;
11976 + theObj = dev->cache[i].object;
11977 + cache = &dev->cache[i];
11982 -static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
11984 - yaffs_Object *hl;
11985 - yaffs_Object *in;
11986 + if (!cache || cache->dirty) {
11987 + /* Flush and try again */
11988 + yaffs_flush_file_cache(theObj);
11989 + cache = yaffs_grab_chunk_worker(dev);
11992 - while (hardList) {
11994 - hardList = (yaffs_Object *) (hardList->hardLinks.next);
12000 - in = yaffs_FindObjectByNumber(dev,
12001 - hl->variant.hardLinkVariant.
12002 - equivalentObjectId);
12006 - /* Add the hardlink pointers */
12007 - hl->variant.hardLinkVariant.equivalentObject = in;
12008 - ylist_add(&hl->hardLinks, &in->hardLinks);
12010 - /* Todo Need to report/handle this better.
12011 - * Got a problem... hardlink to a non-existant object
12013 - hl->variant.hardLinkVariant.equivalentObject = NULL;
12014 - YINIT_LIST_HEAD(&hl->hardLinks);
12015 +/* Find a cached chunk */
12016 +static yaffs_cache_t *yaffs_find_chunk_cache(const yaffs_obj_t *obj,
12019 + yaffs_dev_t *dev = obj->my_dev;
12021 + if (dev->param.n_caches > 0) {
12022 + for (i = 0; i < dev->param.n_caches; i++) {
12023 + if (dev->cache[i].object == obj &&
12024 + dev->cache[i].chunk_id == chunk_id) {
12025 + dev->cache_hits++;
12027 + return &dev->cache[i];
12034 +/* Mark the chunk for the least recently used algorithym */
12035 +static void yaffs_use_cache(yaffs_dev_t *dev, yaffs_cache_t *cache,
12039 + if (dev->param.n_caches > 0) {
12040 + if (dev->cache_last_use < 0 || dev->cache_last_use > 100000000) {
12041 + /* Reset the cache usages */
12043 + for (i = 1; i < dev->param.n_caches; i++)
12044 + dev->cache[i].last_use = 0;
12046 + dev->cache_last_use = 0;
12049 + dev->cache_last_use++;
12051 + cache->last_use = dev->cache_last_use;
12053 -static int ybicmp(const void *a, const void *b)
12055 - register int aseq = ((yaffs_BlockIndex *)a)->seq;
12056 - register int bseq = ((yaffs_BlockIndex *)b)->seq;
12057 - register int ablock = ((yaffs_BlockIndex *)a)->block;
12058 - register int bblock = ((yaffs_BlockIndex *)b)->block;
12059 - if (aseq == bseq)
12060 - return ablock - bblock;
12062 - return aseq - bseq;
12064 + cache->dirty = 1;
12068 +/* Invalidate a single cache page.
12069 + * Do this when a whole page gets written,
12070 + * ie the short cache for this page is no longer valid.
12072 +static void yaffs_invalidate_chunk_cache(yaffs_obj_t *object, int chunk_id)
12074 + if (object->my_dev->param.n_caches > 0) {
12075 + yaffs_cache_t *cache = yaffs_find_chunk_cache(object, chunk_id);
12077 -struct yaffs_ShadowFixerStruct {
12080 - struct yaffs_ShadowFixerStruct *next;
12084 + cache->object = NULL;
12088 -static void yaffs_StripDeletedObjects(yaffs_Device *dev)
12089 +/* Invalidate all the cache pages associated with this object
12090 + * Do this whenever ther file is deleted or resized.
12092 +static void yaffs_invalidate_whole_cache(yaffs_obj_t *in)
12095 - * Sort out state of unlinked and deleted objects after scanning.
12097 - struct ylist_head *i;
12098 - struct ylist_head *n;
12101 + yaffs_dev_t *dev = in->my_dev;
12103 - /* Soft delete all the unlinked files */
12104 - ylist_for_each_safe(i, n,
12105 - &dev->unlinkedDir->variant.directoryVariant.children) {
12107 - l = ylist_entry(i, yaffs_Object, siblings);
12108 - yaffs_DeleteObject(l);
12109 + if (dev->param.n_caches > 0) {
12110 + /* Invalidate it. */
12111 + for (i = 0; i < dev->param.n_caches; i++) {
12112 + if (dev->cache[i].object == in)
12113 + dev->cache[i].object = NULL;
12118 - ylist_for_each_safe(i, n,
12119 - &dev->deletedDir->variant.directoryVariant.children) {
12121 - l = ylist_entry(i, yaffs_Object, siblings);
12122 - yaffs_DeleteObject(l);
12127 +/*--------------------- File read/write ------------------------
12128 + * Read and write have very similar structures.
12129 + * In general the read/write has three parts to it
12130 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
12131 + * Some complete chunks
12132 + * An incomplete chunk to end off with
12134 + * Curve-balls: the first chunk might also be the last chunk.
12137 -static int yaffs_Scan(yaffs_Device *dev)
12138 +int yaffs_file_rd(yaffs_obj_t *in, __u8 *buffer, loff_t offset,
12141 - yaffs_ExtendedTags tags;
12143 - int blockIterator;
12144 - int startIterator;
12151 - yaffs_BlockState state;
12152 - yaffs_Object *hardList = NULL;
12153 - yaffs_BlockInfo *bi;
12154 - __u32 sequenceNumber;
12155 - yaffs_ObjectHeader *oh;
12156 - yaffs_Object *in;
12157 - yaffs_Object *parent;
12162 + yaffs_cache_t *cache;
12164 - int alloc_failed = 0;
12165 + yaffs_dev_t *dev;
12167 - struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
12168 + dev = in->my_dev;
12171 + /* chunk = offset / dev->data_bytes_per_chunk + 1; */
12172 + /* start = offset % dev->data_bytes_per_chunk; */
12173 + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
12177 + /* OK now check for the curveball where the start and end are in
12178 + * the same chunk.
12180 + if ((start + n) < dev->data_bytes_per_chunk)
12183 + nToCopy = dev->data_bytes_per_chunk - start;
12185 + cache = yaffs_find_chunk_cache(in, chunk);
12187 + /* If the chunk is already in the cache or it is less than a whole chunk
12188 + * or we're using inband tags then use the cache (if there is caching)
12189 + * else bypass the cache.
12191 + if (cache || nToCopy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
12192 + if (dev->param.n_caches > 0) {
12194 - T(YAFFS_TRACE_SCAN,
12195 - (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
12196 - dev->internalStartBlock, dev->internalEndBlock));
12197 + /* If we can't find the data in the cache, then load it up. */
12199 - chunkData = yaffs_GetTempBuffer(dev, __LINE__);
12201 + cache = yaffs_grab_chunk_cache(in->my_dev);
12202 + cache->object = in;
12203 + cache->chunk_id = chunk;
12204 + cache->dirty = 0;
12205 + cache->locked = 0;
12206 + yaffs_rd_data_obj(in, chunk,
12209 + cache->n_bytes = 0;
12212 - dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
12213 + yaffs_use_cache(dev, cache, 0);
12215 - /* Scan all the blocks to determine their state */
12216 - for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
12217 - bi = yaffs_GetBlockInfo(dev, blk);
12218 - yaffs_ClearChunkBits(dev, blk);
12219 - bi->pagesInUse = 0;
12220 - bi->softDeletions = 0;
12221 + cache->locked = 1;
12223 - yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
12225 - bi->blockState = state;
12226 - bi->sequenceNumber = sequenceNumber;
12227 + memcpy(buffer, &cache->data[start], nToCopy);
12229 - if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
12230 - bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
12231 + cache->locked = 0;
12233 + /* Read into the local buffer then copy..*/
12235 - T(YAFFS_TRACE_SCAN_DEBUG,
12236 - (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
12237 - state, sequenceNumber));
12238 + __u8 *localBuffer =
12239 + yaffs_get_temp_buffer(dev, __LINE__);
12240 + yaffs_rd_data_obj(in, chunk,
12243 - if (state == YAFFS_BLOCK_STATE_DEAD) {
12244 - T(YAFFS_TRACE_BAD_BLOCKS,
12245 - (TSTR("block %d is bad" TENDSTR), blk));
12246 - } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
12247 - T(YAFFS_TRACE_SCAN_DEBUG,
12248 - (TSTR("Block empty " TENDSTR)));
12249 - dev->nErasedBlocks++;
12250 - dev->nFreeChunks += dev->nChunksPerBlock;
12253 + memcpy(buffer, &localBuffer[start], nToCopy);
12255 - startIterator = dev->internalStartBlock;
12256 - endIterator = dev->internalEndBlock;
12258 - /* For each block.... */
12259 - for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
12260 - blockIterator++) {
12261 + yaffs_release_temp_buffer(dev, localBuffer,
12269 + /* A full chunk. Read directly into the supplied buffer. */
12270 + yaffs_rd_data_obj(in, chunk, buffer);
12272 - blk = blockIterator;
12275 - bi = yaffs_GetBlockInfo(dev, blk);
12276 - state = bi->blockState;
12278 + offset += nToCopy;
12279 + buffer += nToCopy;
12280 + nDone += nToCopy;
12285 - /* For each chunk in each block that needs scanning....*/
12286 - for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
12287 - state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
12288 - /* Read the tags and decide what to do */
12289 - chunk = blk * dev->nChunksPerBlock + c;
12293 - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
12295 +int yaffs_do_file_wr(yaffs_obj_t *in, const __u8 *buffer, loff_t offset,
12296 + int n_bytes, int write_trhrough)
12299 - /* Let's have a good look at this chunk... */
12305 + int nToWriteBack;
12306 + int startOfWrite = offset;
12307 + int chunkWritten = 0;
12308 + __u32 n_bytesRead;
12309 + __u32 chunkStart;
12311 - if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
12312 - /* YAFFS1 only...
12313 - * A deleted chunk
12316 - dev->nFreeChunks++;
12317 - /*T((" %d %d deleted\n",blk,c)); */
12318 - } else if (!tags.chunkUsed) {
12319 - /* An unassigned chunk in the block
12320 - * This means that either the block is empty or
12321 - * this is the one being allocated from
12323 + yaffs_dev_t *dev;
12326 - /* We're looking at the first chunk in the block so the block is unused */
12327 - state = YAFFS_BLOCK_STATE_EMPTY;
12328 - dev->nErasedBlocks++;
12330 - /* this is the block being allocated from */
12331 - T(YAFFS_TRACE_SCAN,
12333 - (" Allocating from %d %d" TENDSTR),
12335 - state = YAFFS_BLOCK_STATE_ALLOCATING;
12336 - dev->allocationBlock = blk;
12337 - dev->allocationPage = c;
12338 - dev->allocationBlockFinder = blk;
12339 - /* Set it to here to encourage the allocator to go forth from here. */
12340 + dev = in->my_dev;
12343 + while (n > 0 && chunkWritten >= 0) {
12344 + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
12346 - dev->nFreeChunks += (dev->nChunksPerBlock - c);
12347 - } else if (tags.chunkId > 0) {
12348 - /* chunkId > 0 so it is a data chunk... */
12349 - unsigned int endpos;
12351 - yaffs_SetChunkBit(dev, blk, c);
12352 - bi->pagesInUse++;
12354 - in = yaffs_FindOrCreateObjectByNumber(dev,
12357 - YAFFS_OBJECT_TYPE_FILE);
12358 - /* PutChunkIntoFile checks for a clash (two data chunks with
12359 - * the same chunkId).
12361 + if (chunk * dev->data_bytes_per_chunk + start != offset ||
12362 + start >= dev->data_bytes_per_chunk) {
12363 + T(YAFFS_TRACE_ERROR, (
12364 + TSTR("AddrToChunk of offset %d gives chunk %d start %d"
12366 + (int)offset, chunk, start));
12368 + chunk++; /* File pos to chunk in file offset */
12371 - alloc_failed = 1;
12372 + /* OK now check for the curveball where the start and end are in
12373 + * the same chunk.
12377 - if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
12378 - alloc_failed = 1;
12380 + if ((start + n) < dev->data_bytes_per_chunk) {
12384 - (tags.chunkId - 1) * dev->nDataBytesPerChunk +
12387 - in->variantType == YAFFS_OBJECT_TYPE_FILE
12388 - && in->variant.fileVariant.scannedFileSize <
12390 - in->variant.fileVariant.
12391 - scannedFileSize = endpos;
12392 - if (!dev->useHeaderFileSize) {
12393 - in->variant.fileVariant.
12395 - in->variant.fileVariant.
12398 + /* Now folks, to calculate how many bytes to write back....
12399 + * If we're overwriting and not writing to then end of file then
12400 + * we need to write back as much as was there before.
12404 - /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
12406 - /* chunkId == 0, so it is an ObjectHeader.
12407 - * Thus, we read in the object header and make the object
12409 - yaffs_SetChunkBit(dev, blk, c);
12410 - bi->pagesInUse++;
12411 + chunkStart = ((chunk - 1) * dev->data_bytes_per_chunk);
12413 - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
12417 - oh = (yaffs_ObjectHeader *) chunkData;
12419 - in = yaffs_FindObjectByNumber(dev,
12421 - if (in && in->variantType != oh->type) {
12422 - /* This should not happen, but somehow
12423 - * Wev'e ended up with an objectId that has been reused but not yet
12424 - * deleted, and worse still it has changed type. Delete the old object.
12426 + if (chunkStart > in->variant.file_variant.file_size)
12427 + n_bytesRead = 0; /* Past end of file */
12429 + n_bytesRead = in->variant.file_variant.file_size - chunkStart;
12431 - yaffs_DeleteObject(in);
12432 + if (n_bytesRead > dev->data_bytes_per_chunk)
12433 + n_bytesRead = dev->data_bytes_per_chunk;
12439 + (start + n)) ? n_bytesRead : (start + n);
12441 - in = yaffs_FindOrCreateObjectByNumber(dev,
12447 - alloc_failed = 1;
12449 - if (in && oh->shadowsObject > 0) {
12451 - struct yaffs_ShadowFixerStruct *fixer;
12452 - fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
12454 - fixer->next = shadowFixerList;
12455 - shadowFixerList = fixer;
12456 - fixer->objectId = tags.objectId;
12457 - fixer->shadowedId = oh->shadowsObject;
12459 + if (nToWriteBack < 0 || nToWriteBack > dev->data_bytes_per_chunk)
12463 + nToCopy = dev->data_bytes_per_chunk - start;
12464 + nToWriteBack = dev->data_bytes_per_chunk;
12467 + if (nToCopy != dev->data_bytes_per_chunk || dev->param.inband_tags) {
12468 + /* An incomplete start or end chunk (or maybe both start and end chunk),
12469 + * or we're using inband tags, so we want to use the cache buffers.
12471 + if (dev->param.n_caches > 0) {
12472 + yaffs_cache_t *cache;
12473 + /* If we can't find the data in the cache, then load the cache */
12474 + cache = yaffs_find_chunk_cache(in, chunk);
12477 + && yaffs_check_alloc_available(dev, 1)) {
12478 + cache = yaffs_grab_chunk_cache(dev);
12479 + cache->object = in;
12480 + cache->chunk_id = chunk;
12481 + cache->dirty = 0;
12482 + cache->locked = 0;
12483 + yaffs_rd_data_obj(in, chunk,
12485 + } else if (cache &&
12487 + !yaffs_check_alloc_available(dev, 1)) {
12488 + /* Drop the cache if it was a read cache item and
12489 + * no space check has been made for it.
12494 - if (in && in->valid) {
12495 - /* We have already filled this one. We have a duplicate and need to resolve it. */
12497 + yaffs_use_cache(dev, cache, 1);
12498 + cache->locked = 1;
12500 - unsigned existingSerial = in->serial;
12501 - unsigned newSerial = tags.serialNumber;
12503 - if (((existingSerial + 1) & 3) == newSerial) {
12504 - /* Use new one - destroy the exisiting one */
12505 - yaffs_DeleteChunk(dev,
12510 - /* Use existing - destroy this one. */
12511 - yaffs_DeleteChunk(dev, chunk, 1,
12513 + memcpy(&cache->data[start], buffer,
12517 + cache->locked = 0;
12518 + cache->n_bytes = nToWriteBack;
12520 + if (write_trhrough) {
12522 + yaffs_wr_data_obj
12525 + cache->data, cache->n_bytes,
12527 + cache->dirty = 0;
12531 + chunkWritten = -1; /* fail the write */
12534 + /* An incomplete start or end chunk (or maybe both start and end chunk)
12535 + * Read into the local buffer then copy, then copy over and write back.
12538 - if (in && !in->valid &&
12539 - (tags.objectId == YAFFS_OBJECTID_ROOT ||
12540 - tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
12541 - /* We only load some info, don't fiddle with directory structure */
12543 - in->variantType = oh->type;
12544 + __u8 *localBuffer =
12545 + yaffs_get_temp_buffer(dev, __LINE__);
12547 - in->yst_mode = oh->yst_mode;
12548 -#ifdef CONFIG_YAFFS_WINCE
12549 - in->win_atime[0] = oh->win_atime[0];
12550 - in->win_ctime[0] = oh->win_ctime[0];
12551 - in->win_mtime[0] = oh->win_mtime[0];
12552 - in->win_atime[1] = oh->win_atime[1];
12553 - in->win_ctime[1] = oh->win_ctime[1];
12554 - in->win_mtime[1] = oh->win_mtime[1];
12556 - in->yst_uid = oh->yst_uid;
12557 - in->yst_gid = oh->yst_gid;
12558 - in->yst_atime = oh->yst_atime;
12559 - in->yst_mtime = oh->yst_mtime;
12560 - in->yst_ctime = oh->yst_ctime;
12561 - in->yst_rdev = oh->yst_rdev;
12563 - in->hdrChunk = chunk;
12564 - in->serial = tags.serialNumber;
12565 + yaffs_rd_data_obj(in, chunk,
12568 - } else if (in && !in->valid) {
12569 - /* we need to load this info */
12572 - in->variantType = oh->type;
12574 - in->yst_mode = oh->yst_mode;
12575 -#ifdef CONFIG_YAFFS_WINCE
12576 - in->win_atime[0] = oh->win_atime[0];
12577 - in->win_ctime[0] = oh->win_ctime[0];
12578 - in->win_mtime[0] = oh->win_mtime[0];
12579 - in->win_atime[1] = oh->win_atime[1];
12580 - in->win_ctime[1] = oh->win_ctime[1];
12581 - in->win_mtime[1] = oh->win_mtime[1];
12583 - in->yst_uid = oh->yst_uid;
12584 - in->yst_gid = oh->yst_gid;
12585 - in->yst_atime = oh->yst_atime;
12586 - in->yst_mtime = oh->yst_mtime;
12587 - in->yst_ctime = oh->yst_ctime;
12588 - in->yst_rdev = oh->yst_rdev;
12590 - in->hdrChunk = chunk;
12591 - in->serial = tags.serialNumber;
12592 + memcpy(&localBuffer[start], buffer, nToCopy);
12594 - yaffs_SetObjectName(in, oh->name);
12597 + yaffs_wr_data_obj(in, chunk,
12602 - /* directory stuff...
12603 - * hook up to parent
12605 + yaffs_release_temp_buffer(dev, localBuffer,
12609 - yaffs_FindOrCreateObjectByNumber
12610 - (dev, oh->parentObjectId,
12611 - YAFFS_OBJECT_TYPE_DIRECTORY);
12613 - alloc_failed = 1;
12614 - if (parent && parent->variantType ==
12615 - YAFFS_OBJECT_TYPE_UNKNOWN) {
12616 - /* Set up as a directory */
12617 - parent->variantType =
12618 - YAFFS_OBJECT_TYPE_DIRECTORY;
12619 - YINIT_LIST_HEAD(&parent->variant.
12620 - directoryVariant.
12622 - } else if (!parent || parent->variantType !=
12623 - YAFFS_OBJECT_TYPE_DIRECTORY) {
12624 - /* Hoosterman, another problem....
12625 - * We're trying to use a non-directory as a directory
12629 - T(YAFFS_TRACE_ERROR,
12631 - ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
12633 - parent = dev->lostNFoundDir;
12636 + /* A full chunk. Write directly from the supplied buffer. */
12638 - yaffs_AddObjectToDirectory(parent, in);
12640 - if (0 && (parent == dev->deletedDir ||
12641 - parent == dev->unlinkedDir)) {
12642 - in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
12643 - dev->nDeletedFiles++;
12645 - /* Note re hardlinks.
12646 - * Since we might scan a hardlink before its equivalent object is scanned
12647 - * we put them all in a list.
12648 - * After scanning is complete, we should have all the objects, so we run through this
12649 - * list and fix up all the chains.
12652 - switch (in->variantType) {
12653 - case YAFFS_OBJECT_TYPE_UNKNOWN:
12654 - /* Todo got a problem */
12656 - case YAFFS_OBJECT_TYPE_FILE:
12657 - if (dev->useHeaderFileSize)
12659 - in->variant.fileVariant.
12664 - case YAFFS_OBJECT_TYPE_HARDLINK:
12665 - in->variant.hardLinkVariant.
12666 - equivalentObjectId =
12667 - oh->equivalentObjectId;
12668 - in->hardLinks.next =
12669 - (struct ylist_head *)
12673 - case YAFFS_OBJECT_TYPE_DIRECTORY:
12676 - case YAFFS_OBJECT_TYPE_SPECIAL:
12679 - case YAFFS_OBJECT_TYPE_SYMLINK:
12680 - in->variant.symLinkVariant.alias =
12681 - yaffs_CloneString(oh->alias);
12682 - if (!in->variant.symLinkVariant.alias)
12683 - alloc_failed = 1;
12687 + yaffs_wr_data_obj(in, chunk, buffer,
12688 + dev->data_bytes_per_chunk,
12692 - if (parent == dev->deletedDir) {
12693 - yaffs_DestroyObject(in);
12694 - bi->hasShrinkHeader = 1;
12699 + /* Since we've overwritten the cached data, we better invalidate it. */
12700 + yaffs_invalidate_chunk_cache(in, chunk);
12703 - if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
12704 - /* If we got this far while scanning, then the block is fully allocated.*/
12705 - state = YAFFS_BLOCK_STATE_FULL;
12706 + if (chunkWritten >= 0) {
12708 + offset += nToCopy;
12709 + buffer += nToCopy;
12710 + nDone += nToCopy;
12713 - bi->blockState = state;
12716 - /* Now let's see if it was dirty */
12717 - if (bi->pagesInUse == 0 &&
12718 - !bi->hasShrinkHeader &&
12719 - bi->blockState == YAFFS_BLOCK_STATE_FULL) {
12720 - yaffs_BlockBecameDirty(dev, blk);
12722 + /* Update file object */
12725 + if ((startOfWrite + nDone) > in->variant.file_variant.file_size)
12726 + in->variant.file_variant.file_size = (startOfWrite + nDone);
12730 - /* Ok, we've done all the scanning.
12731 - * Fix up the hard link chains.
12732 - * We should now have scanned all the objects, now it's time to add these
12738 - yaffs_HardlinkFixup(dev, hardList);
12739 +int yaffs_wr_file(yaffs_obj_t *in, const __u8 *buffer, loff_t offset,
12740 + int n_bytes, int write_trhrough)
12742 + yaffs2_handle_hole(in,offset);
12743 + return yaffs_do_file_wr(in,buffer,offset,n_bytes,write_trhrough);
12746 - /* Fix up any shadowed objects */
12748 - struct yaffs_ShadowFixerStruct *fixer;
12749 - yaffs_Object *obj;
12751 - while (shadowFixerList) {
12752 - fixer = shadowFixerList;
12753 - shadowFixerList = fixer->next;
12754 - /* Complete the rename transaction by deleting the shadowed object
12755 - * then setting the object header to unshadowed.
12757 - obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
12759 - yaffs_DeleteObject(obj);
12761 - obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
12764 - yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
12765 +/* ---------------------- File resizing stuff ------------------ */
12770 +static void yaffs_prune_chunks(yaffs_obj_t *in, int new_size)
12773 - yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
12774 + yaffs_dev_t *dev = in->my_dev;
12775 + int oldFileSize = in->variant.file_variant.file_size;
12777 - if (alloc_failed)
12778 - return YAFFS_FAIL;
12779 + int lastDel = 1 + (oldFileSize - 1) / dev->data_bytes_per_chunk;
12781 + int startDel = 1 + (new_size + dev->data_bytes_per_chunk - 1) /
12782 + dev->data_bytes_per_chunk;
12786 - T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
12787 + /* Delete backwards so that we don't end up with holes if
12788 + * power is lost part-way through the operation.
12790 + for (i = lastDel; i >= startDel; i--) {
12791 + /* NB this could be optimised somewhat,
12792 + * eg. could retrieve the tags and write them without
12793 + * using yaffs_chunk_del
12796 + chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
12797 + if (chunk_id > 0) {
12799 + (dev->internal_start_block * dev->param.chunks_per_block)
12801 + ((dev->internal_end_block +
12802 + 1) * dev->param.chunks_per_block)) {
12803 + T(YAFFS_TRACE_ALWAYS,
12804 + (TSTR("Found daft chunk_id %d for %d" TENDSTR),
12807 + in->n_data_chunks--;
12808 + yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
12816 -static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
12819 - yaffs_ObjectHeader *oh;
12820 - yaffs_Device *dev;
12821 - yaffs_ExtendedTags tags;
12823 - int alloc_failed = 0;
12827 +void yaffs_resize_file_down( yaffs_obj_t *obj, loff_t new_size)
12829 + int newFullChunks;
12830 + __u32 new_sizeOfPartialChunk;
12831 + yaffs_dev_t *dev = obj->my_dev;
12834 + yaffs_addr_to_chunk(dev, new_size, &newFullChunks, &new_sizeOfPartialChunk);
12837 - T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
12839 - in->lazyLoaded ? "not yet" : "already"));
12841 + yaffs_prune_chunks(obj, new_size);
12843 - if (in->lazyLoaded && in->hdrChunk > 0) {
12844 - in->lazyLoaded = 0;
12845 - chunkData = yaffs_GetTempBuffer(dev, __LINE__);
12846 + if (new_sizeOfPartialChunk != 0) {
12847 + int lastChunk = 1 + newFullChunks;
12848 + __u8 *localBuffer = yaffs_get_temp_buffer(dev, __LINE__);
12850 - result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
12851 - oh = (yaffs_ObjectHeader *) chunkData;
12852 + /* Got to read and rewrite the last chunk with its new size and zero pad */
12853 + yaffs_rd_data_obj(obj, lastChunk, localBuffer);
12854 + memset(localBuffer + new_sizeOfPartialChunk, 0,
12855 + dev->data_bytes_per_chunk - new_sizeOfPartialChunk);
12857 - in->yst_mode = oh->yst_mode;
12858 -#ifdef CONFIG_YAFFS_WINCE
12859 - in->win_atime[0] = oh->win_atime[0];
12860 - in->win_ctime[0] = oh->win_ctime[0];
12861 - in->win_mtime[0] = oh->win_mtime[0];
12862 - in->win_atime[1] = oh->win_atime[1];
12863 - in->win_ctime[1] = oh->win_ctime[1];
12864 - in->win_mtime[1] = oh->win_mtime[1];
12866 - in->yst_uid = oh->yst_uid;
12867 - in->yst_gid = oh->yst_gid;
12868 - in->yst_atime = oh->yst_atime;
12869 - in->yst_mtime = oh->yst_mtime;
12870 - in->yst_ctime = oh->yst_ctime;
12871 - in->yst_rdev = oh->yst_rdev;
12872 + yaffs_wr_data_obj(obj, lastChunk, localBuffer,
12873 + new_sizeOfPartialChunk, 1);
12876 - yaffs_SetObjectName(in, oh->name);
12877 + yaffs_release_temp_buffer(dev, localBuffer, __LINE__);
12880 - if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
12881 - in->variant.symLinkVariant.alias =
12882 - yaffs_CloneString(oh->alias);
12883 - if (!in->variant.symLinkVariant.alias)
12884 - alloc_failed = 1; /* Not returned to caller */
12886 + obj->variant.file_variant.file_size = new_size;
12888 - yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
12890 + yaffs_prune_tree(dev, &obj->variant.file_variant);
12893 -static int yaffs_ScanBackwards(yaffs_Device *dev)
12895 - yaffs_ExtendedTags tags;
12897 - int blockIterator;
12898 - int startIterator;
12900 - int nBlocksToScan = 0;
12906 - yaffs_BlockState state;
12907 - yaffs_Object *hardList = NULL;
12908 - yaffs_BlockInfo *bi;
12909 - __u32 sequenceNumber;
12910 - yaffs_ObjectHeader *oh;
12911 - yaffs_Object *in;
12912 - yaffs_Object *parent;
12913 - int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
12919 - int foundChunksInBlock;
12920 - int equivalentObjectId;
12921 - int alloc_failed = 0;
12922 +int yaffs_resize_file(yaffs_obj_t *in, loff_t new_size)
12924 + yaffs_dev_t *dev = in->my_dev;
12925 + int oldFileSize = in->variant.file_variant.file_size;
12927 + yaffs_flush_file_cache(in);
12928 + yaffs_invalidate_whole_cache(in);
12930 - yaffs_BlockIndex *blockIndex = NULL;
12931 - int altBlockIndex = 0;
12932 + yaffs_check_gc(dev,0);
12934 - if (!dev->isYaffs2) {
12935 - T(YAFFS_TRACE_SCAN,
12936 - (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
12937 + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
12941 - T(YAFFS_TRACE_SCAN,
12943 - ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
12944 - TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
12945 + if (new_size == oldFileSize)
12948 + if(new_size > oldFileSize){
12949 + yaffs2_handle_hole(in,new_size);
12950 + in->variant.file_variant.file_size = new_size;
12952 + /* new_size < oldFileSize */
12953 + yaffs_resize_file_down(in, new_size);
12956 + /* Write a new object header to reflect the resize.
12957 + * show we've shrunk the file, if need be
12958 + * Do this only if the file is not in the deleted directories
12959 + * and is not shadowed.
12961 + if (in->parent &&
12962 + !in->is_shadowed &&
12963 + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
12964 + in->parent->obj_id != YAFFS_OBJECTID_DELETED)
12965 + yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
12967 - dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
12969 - blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
12973 - if (!blockIndex) {
12974 - blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
12975 - altBlockIndex = 1;
12977 +loff_t yaffs_get_file_size(yaffs_obj_t *obj)
12979 + YCHAR *alias = NULL;
12980 + obj = yaffs_get_equivalent_obj(obj);
12982 - if (!blockIndex) {
12983 - T(YAFFS_TRACE_SCAN,
12984 - (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
12985 - return YAFFS_FAIL;
12986 + switch (obj->variant_type) {
12987 + case YAFFS_OBJECT_TYPE_FILE:
12988 + return obj->variant.file_variant.file_size;
12989 + case YAFFS_OBJECT_TYPE_SYMLINK:
12990 + alias = obj->variant.symlink_variant.alias;
12993 + return yaffs_strnlen(alias,YAFFS_MAX_ALIAS_LENGTH);
12999 - dev->blocksInCheckpoint = 0;
13001 - chunkData = yaffs_GetTempBuffer(dev, __LINE__);
13003 - /* Scan all the blocks to determine their state */
13004 - for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
13005 - bi = yaffs_GetBlockInfo(dev, blk);
13006 - yaffs_ClearChunkBits(dev, blk);
13007 - bi->pagesInUse = 0;
13008 - bi->softDeletions = 0;
13010 - yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
13012 - bi->blockState = state;
13013 - bi->sequenceNumber = sequenceNumber;
13015 - if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
13016 - bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
13017 - if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
13018 - bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
13019 +int yaffs_flush_file(yaffs_obj_t *in, int update_time, int data_sync)
13023 + yaffs_flush_file_cache(in);
13024 + if(data_sync) /* Only sync data */
13027 + if (update_time) {
13028 +#ifdef CONFIG_YAFFS_WINCE
13029 + yfsd_win_file_time_now(in->win_mtime);
13032 - T(YAFFS_TRACE_SCAN_DEBUG,
13033 - (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
13034 - state, sequenceNumber));
13035 + in->yst_mtime = Y_CURRENT_TIME;
13040 - if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
13041 - dev->blocksInCheckpoint++;
13042 + retVal = (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >=
13043 + 0) ? YAFFS_OK : YAFFS_FAIL;
13046 + retVal = YAFFS_OK;
13049 - } else if (state == YAFFS_BLOCK_STATE_DEAD) {
13050 - T(YAFFS_TRACE_BAD_BLOCKS,
13051 - (TSTR("block %d is bad" TENDSTR), blk));
13052 - } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
13053 - T(YAFFS_TRACE_SCAN_DEBUG,
13054 - (TSTR("Block empty " TENDSTR)));
13055 - dev->nErasedBlocks++;
13056 - dev->nFreeChunks += dev->nChunksPerBlock;
13057 - } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
13060 - /* Determine the highest sequence number */
13061 - if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
13062 - sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
13065 - blockIndex[nBlocksToScan].seq = sequenceNumber;
13066 - blockIndex[nBlocksToScan].block = blk;
13067 +static int yaffs_generic_obj_del(yaffs_obj_t *in)
13071 + /* First off, invalidate the file's data in the cache, without flushing. */
13072 + yaffs_invalidate_whole_cache(in);
13074 - if (sequenceNumber >= dev->sequenceNumber)
13075 - dev->sequenceNumber = sequenceNumber;
13077 - /* TODO: Nasty sequence number! */
13078 - T(YAFFS_TRACE_SCAN,
13080 - ("Block scanning block %d has bad sequence number %d"
13081 - TENDSTR), blk, sequenceNumber));
13082 + if (in->my_dev->param.is_yaffs2 && (in->parent != in->my_dev->del_dir)) {
13083 + /* Move to the unlinked directory so we have a record that it was deleted. */
13084 + yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0, 0);
13090 - T(YAFFS_TRACE_SCAN,
13091 - (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
13092 + yaffs_remove_obj_from_dir(in);
13093 + yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
13094 + in->hdr_chunk = 0;
13096 + yaffs_free_obj(in);
13102 +/* yaffs_del_file deletes the whole file data
13103 + * and the inode associated with the file.
13104 + * It does not delete the links associated with the file.
13106 +static int yaffs_unlink_file_if_needed(yaffs_obj_t *in)
13109 - /* Sort the blocks */
13110 -#ifndef CONFIG_YAFFS_USE_OWN_SORT
13112 - /* Use qsort now. */
13113 - yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
13117 - /* Dungy old bubble sort... */
13119 + int immediateDeletion = 0;
13120 + yaffs_dev_t *dev = in->my_dev;
13122 - yaffs_BlockIndex temp;
13125 + if (!in->my_inode)
13126 + immediateDeletion = 1;
13128 - for (i = 0; i < nBlocksToScan; i++)
13129 - for (j = i + 1; j < nBlocksToScan; j++)
13130 - if (blockIndex[i].seq > blockIndex[j].seq) {
13131 - temp = blockIndex[j];
13132 - blockIndex[j] = blockIndex[i];
13133 - blockIndex[i] = temp;
13135 + if (immediateDeletion) {
13137 + yaffs_change_obj_name(in, in->my_dev->del_dir,
13138 + _Y("deleted"), 0, 0);
13139 + T(YAFFS_TRACE_TRACING,
13140 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
13143 + in->my_dev->n_deleted_files++;
13144 + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
13145 + yaffs_resize_file(in, 0);
13146 + yaffs_soft_del_file(in);
13149 + yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
13150 + _Y("unlinked"), 0, 0);
13156 - T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
13160 - /* Now scan the blocks looking at the data. */
13161 - startIterator = 0;
13162 - endIterator = nBlocksToScan - 1;
13163 - T(YAFFS_TRACE_SCAN_DEBUG,
13164 - (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
13165 +int yaffs_del_file(yaffs_obj_t *in)
13167 + int retVal = YAFFS_OK;
13168 + int deleted; /* Need to cache value on stack if in is freed */
13169 + yaffs_dev_t *dev = in->my_dev;
13171 - /* For each block.... backwards */
13172 - for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
13173 - blockIterator--) {
13174 - /* Cooperative multitasking! This loop can run for so
13175 - long that watchdog timers expire. */
13177 + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
13178 + yaffs_resize_file(in, 0);
13180 - /* get the block to scan in the correct order */
13181 - blk = blockIndex[blockIterator].block;
13182 + if (in->n_data_chunks > 0) {
13183 + /* Use soft deletion if there is data in the file.
13184 + * That won't be the case if it has been resized to zero.
13186 + if (!in->unlinked)
13187 + retVal = yaffs_unlink_file_if_needed(in);
13189 - bi = yaffs_GetBlockInfo(dev, blk);
13190 + deleted = in->deleted;
13192 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
13195 + in->my_dev->n_deleted_files++;
13196 + yaffs_soft_del_file(in);
13198 + return deleted ? YAFFS_OK : YAFFS_FAIL;
13200 + /* The file has no data chunks so we toss it immediately */
13201 + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
13202 + in->variant.file_variant.top = NULL;
13203 + yaffs_generic_obj_del(in);
13205 - state = bi->blockState;
13211 +static int yaffs_is_non_empty_dir(yaffs_obj_t *obj)
13213 + return (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
13214 + !(ylist_empty(&obj->variant.dir_variant.children));
13217 - /* For each chunk in each block that needs scanning.... */
13218 - foundChunksInBlock = 0;
13219 - for (c = dev->nChunksPerBlock - 1;
13220 - !alloc_failed && c >= 0 &&
13221 - (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
13222 - state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
13223 - /* Scan backwards...
13224 - * Read the tags and decide what to do
13226 +static int yaffs_del_dir(yaffs_obj_t *obj)
13228 + /* First check that the directory is empty. */
13229 + if (yaffs_is_non_empty_dir(obj))
13230 + return YAFFS_FAIL;
13232 - chunk = blk * dev->nChunksPerBlock + c;
13233 + return yaffs_generic_obj_del(obj);
13236 - result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
13238 +static int yaffs_del_symlink(yaffs_obj_t *in)
13240 + if(in->variant.symlink_variant.alias)
13241 + YFREE(in->variant.symlink_variant.alias);
13242 + in->variant.symlink_variant.alias=NULL;
13244 - /* Let's have a good look at this chunk... */
13245 + return yaffs_generic_obj_del(in);
13248 - if (!tags.chunkUsed) {
13249 - /* An unassigned chunk in the block.
13250 - * If there are used chunks after this one, then
13251 - * it is a chunk that was skipped due to failing the erased
13252 - * check. Just skip it so that it can be deleted.
13253 - * But, more typically, We get here when this is an unallocated
13254 - * chunk and his means that either the block is empty or
13255 - * this is the one being allocated from
13257 +static int yaffs_del_link(yaffs_obj_t *in)
13259 + /* remove this hardlink from the list assocaited with the equivalent
13262 + ylist_del_init(&in->hard_links);
13263 + return yaffs_generic_obj_del(in);
13266 - if (foundChunksInBlock) {
13267 - /* This is a chunk that was skipped due to failing the erased check */
13268 - } else if (c == 0) {
13269 - /* We're looking at the first chunk in the block so the block is unused */
13270 - state = YAFFS_BLOCK_STATE_EMPTY;
13271 - dev->nErasedBlocks++;
13273 - if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
13274 - state == YAFFS_BLOCK_STATE_ALLOCATING) {
13275 - if (dev->sequenceNumber == bi->sequenceNumber) {
13276 - /* this is the block being allocated from */
13278 - T(YAFFS_TRACE_SCAN,
13280 - (" Allocating from %d %d"
13281 - TENDSTR), blk, c));
13283 - state = YAFFS_BLOCK_STATE_ALLOCATING;
13284 - dev->allocationBlock = blk;
13285 - dev->allocationPage = c;
13286 - dev->allocationBlockFinder = blk;
13288 - /* This is a partially written block that is not
13289 - * the current allocation block. This block must have
13290 - * had a write failure, so set up for retirement.
13293 - /* bi->needsRetiring = 1; ??? TODO */
13294 - bi->gcPrioritise = 1;
13296 - T(YAFFS_TRACE_ALWAYS,
13297 - (TSTR("Partially written block %d detected" TENDSTR),
13302 +int yaffs_del_obj(yaffs_obj_t *obj)
13305 + switch (obj->variant_type) {
13306 + case YAFFS_OBJECT_TYPE_FILE:
13307 + retVal = yaffs_del_file(obj);
13309 + case YAFFS_OBJECT_TYPE_DIRECTORY:
13310 + if(!ylist_empty(&obj->variant.dir_variant.dirty)){
13311 + T(YAFFS_TRACE_BACKGROUND, (TSTR("Remove object %d from dirty directories" TENDSTR),obj->obj_id));
13312 + ylist_del_init(&obj->variant.dir_variant.dirty);
13314 + return yaffs_del_dir(obj);
13316 + case YAFFS_OBJECT_TYPE_SYMLINK:
13317 + retVal = yaffs_del_symlink(obj);
13319 + case YAFFS_OBJECT_TYPE_HARDLINK:
13320 + retVal = yaffs_del_link(obj);
13322 + case YAFFS_OBJECT_TYPE_SPECIAL:
13323 + retVal = yaffs_generic_obj_del(obj);
13325 + case YAFFS_OBJECT_TYPE_UNKNOWN:
13327 + break; /* should not happen. */
13330 - dev->nFreeChunks++;
13334 - } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
13335 - T(YAFFS_TRACE_SCAN,
13336 - (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
13339 - dev->nFreeChunks++;
13341 - } else if (tags.chunkId > 0) {
13342 - /* chunkId > 0 so it is a data chunk... */
13343 - unsigned int endpos;
13344 - __u32 chunkBase =
13345 - (tags.chunkId - 1) * dev->nDataBytesPerChunk;
13347 - foundChunksInBlock = 1;
13350 - yaffs_SetChunkBit(dev, blk, c);
13351 - bi->pagesInUse++;
13353 - in = yaffs_FindOrCreateObjectByNumber(dev,
13356 - YAFFS_OBJECT_TYPE_FILE);
13358 - /* Out of memory */
13359 - alloc_failed = 1;
13361 +static int yaffs_unlink_worker(yaffs_obj_t *obj)
13365 - in->variantType == YAFFS_OBJECT_TYPE_FILE
13367 - in->variant.fileVariant.shrinkSize) {
13368 - /* This has not been invalidated by a resize */
13369 - if (!yaffs_PutChunkIntoFile(in, tags.chunkId,
13371 - alloc_failed = 1;
13373 + int immediateDeletion = 0;
13375 - /* File size is calculated by looking at the data chunks if we have not
13376 - * seen an object header yet. Stop this practice once we find an object header.
13380 - 1) * dev->nDataBytesPerChunk +
13383 - if (!in->valid && /* have not got an object header yet */
13384 - in->variant.fileVariant.
13385 - scannedFileSize < endpos) {
13386 - in->variant.fileVariant.
13387 - scannedFileSize = endpos;
13388 - in->variant.fileVariant.
13390 - in->variant.fileVariant.
13393 + if (!obj->my_inode)
13394 + immediateDeletion = 1;
13397 - /* This chunk has been invalidated by a resize, so delete */
13398 - yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
13400 + yaffs_update_parent(obj->parent);
13404 - /* chunkId == 0, so it is an ObjectHeader.
13405 - * Thus, we read in the object header and make the object
13407 - foundChunksInBlock = 1;
13408 + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
13409 + return yaffs_del_link(obj);
13410 + } else if (!ylist_empty(&obj->hard_links)) {
13411 + /* Curve ball: We're unlinking an object that has a hardlink.
13413 + * This problem arises because we are not strictly following
13414 + * The Linux link/inode model.
13416 + * We can't really delete the object.
13417 + * Instead, we do the following:
13418 + * - Select a hardlink.
13419 + * - Unhook it from the hard links
13420 + * - Move it from its parent directory (so that the rename can work)
13421 + * - Rename the object to the hardlink's name.
13422 + * - Delete the hardlink
13425 - yaffs_SetChunkBit(dev, blk, c);
13426 - bi->pagesInUse++;
13428 + yaffs_obj_t *parent;
13430 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
13434 + hl = ylist_entry(obj->hard_links.next, yaffs_obj_t, hard_links);
13436 - if (tags.extraHeaderInfoAvailable) {
13437 - in = yaffs_FindOrCreateObjectByNumber
13438 - (dev, tags.objectId,
13439 - tags.extraObjectType);
13441 - alloc_failed = 1;
13443 + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
13444 + parent = hl->parent;
13447 -#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
13450 - tags.extraShadows ||
13452 - (tags.objectId == YAFFS_OBJECTID_ROOT ||
13453 - tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
13455 - /* If we don't have valid info then we need to read the chunk
13456 - * TODO In future we can probably defer reading the chunk and
13457 - * living with invalid data until needed.
13459 + ylist_del_init(&hl->hard_links);
13461 - result = yaffs_ReadChunkWithTagsFromNAND(dev,
13466 - oh = (yaffs_ObjectHeader *) chunkData;
13468 - if (dev->inbandTags) {
13469 - /* Fix up the header if they got corrupted by inband tags */
13470 - oh->shadowsObject = oh->inbandShadowsObject;
13471 - oh->isShrink = oh->inbandIsShrink;
13473 + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
13476 - in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
13478 - alloc_failed = 1;
13480 + retVal = yaffs_change_obj_name(obj,parent, name, 0, 0);
13483 + if (retVal == YAFFS_OK)
13484 + retVal = yaffs_generic_obj_del(hl);
13487 - /* TODO Hoosterman we have a problem! */
13488 - T(YAFFS_TRACE_ERROR,
13490 - ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
13491 - TENDSTR), tags.objectId, chunk));
13497 - /* We have already filled this one.
13498 - * We have a duplicate that will be discarded, but
13499 - * we first have to suck out resize info if it is a file.
13501 + } else if (immediateDeletion) {
13502 + switch (obj->variant_type) {
13503 + case YAFFS_OBJECT_TYPE_FILE:
13504 + return yaffs_del_file(obj);
13506 + case YAFFS_OBJECT_TYPE_DIRECTORY:
13507 + ylist_del_init(&obj->variant.dir_variant.dirty);
13508 + return yaffs_del_dir(obj);
13510 + case YAFFS_OBJECT_TYPE_SYMLINK:
13511 + return yaffs_del_symlink(obj);
13513 + case YAFFS_OBJECT_TYPE_SPECIAL:
13514 + return yaffs_generic_obj_del(obj);
13516 + case YAFFS_OBJECT_TYPE_HARDLINK:
13517 + case YAFFS_OBJECT_TYPE_UNKNOWN:
13519 + return YAFFS_FAIL;
13521 + } else if(yaffs_is_non_empty_dir(obj))
13522 + return YAFFS_FAIL;
13524 + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
13525 + _Y("unlinked"), 0, 0);
13528 - if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
13530 - oh->type == YAFFS_OBJECT_TYPE_FILE) ||
13531 - (tags.extraHeaderInfoAvailable &&
13532 - tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
13534 - (oh) ? oh->fileSize : tags.
13536 - __u32 parentObjectId =
13538 - parentObjectId : tags.
13539 - extraParentObjectId;
13543 - (oh) ? oh->isShrink : tags.
13544 - extraIsShrinkHeader;
13546 - /* If it is deleted (unlinked at start also means deleted)
13547 - * we treat the file size as being zeroed at this point.
13549 - if (parentObjectId ==
13550 - YAFFS_OBJECTID_DELETED
13551 - || parentObjectId ==
13552 - YAFFS_OBJECTID_UNLINKED) {
13556 +static int yaffs_unlink_obj(yaffs_obj_t *obj)
13560 - in->variant.fileVariant.
13561 - shrinkSize > thisSize) {
13562 - in->variant.fileVariant.
13566 + if (obj && obj->unlink_allowed)
13567 + return yaffs_unlink_worker(obj);
13570 - bi->hasShrinkHeader = 1;
13571 + return YAFFS_FAIL;
13574 - /* Use existing - destroy this one. */
13575 - yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
13577 +int yaffs_unlinker(yaffs_obj_t *dir, const YCHAR *name)
13579 + yaffs_obj_t *obj;
13582 + obj = yaffs_find_by_name(dir, name);
13583 + return yaffs_unlink_obj(obj);
13586 - if (!in->valid && in->variantType !=
13587 - (oh ? oh->type : tags.extraObjectType))
13588 - T(YAFFS_TRACE_ERROR, (
13589 - TSTR("yaffs tragedy: Bad object type, "
13590 - TCONT("%d != %d, for object %d at chunk ")
13591 - TCONT("%d during scan")
13593 - oh->type : tags.extraObjectType,
13594 - in->variantType, tags.objectId,
13597 - if (!in->valid &&
13598 - (tags.objectId == YAFFS_OBJECTID_ROOT ||
13600 - YAFFS_OBJECTID_LOSTNFOUND)) {
13601 - /* We only load some info, don't fiddle with directory structure */
13603 +/*----------------------- Initialisation Scanning ---------------------- */
13606 - in->variantType = oh->type;
13607 +void yaffs_handle_shadowed_obj(yaffs_dev_t *dev, int obj_id,
13608 + int backward_scanning)
13610 + yaffs_obj_t *obj;
13612 - in->yst_mode = oh->yst_mode;
13613 -#ifdef CONFIG_YAFFS_WINCE
13614 - in->win_atime[0] = oh->win_atime[0];
13615 - in->win_ctime[0] = oh->win_ctime[0];
13616 - in->win_mtime[0] = oh->win_mtime[0];
13617 - in->win_atime[1] = oh->win_atime[1];
13618 - in->win_ctime[1] = oh->win_ctime[1];
13619 - in->win_mtime[1] = oh->win_mtime[1];
13621 - in->yst_uid = oh->yst_uid;
13622 - in->yst_gid = oh->yst_gid;
13623 - in->yst_atime = oh->yst_atime;
13624 - in->yst_mtime = oh->yst_mtime;
13625 - in->yst_ctime = oh->yst_ctime;
13626 - in->yst_rdev = oh->yst_rdev;
13627 + if (!backward_scanning) {
13628 + /* Handle YAFFS1 forward scanning case
13629 + * For YAFFS1 we always do the deletion
13634 - in->variantType = tags.extraObjectType;
13635 - in->lazyLoaded = 1;
13638 + /* Handle YAFFS2 case (backward scanning)
13639 + * If the shadowed object exists then ignore.
13641 + obj = yaffs_find_by_number(dev, obj_id);
13646 - in->hdrChunk = chunk;
13647 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
13648 + * We put it in unlinked dir to be cleaned up after the scanning
13651 + yaffs_find_or_create_by_number(dev, obj_id,
13652 + YAFFS_OBJECT_TYPE_FILE);
13655 + obj->is_shadowed = 1;
13656 + yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
13657 + obj->variant.file_variant.shrink_size = 0;
13658 + obj->valid = 1; /* So that we don't read any other info for this file */
13660 - } else if (!in->valid) {
13661 - /* we need to load this info */
13665 - in->hdrChunk = chunk;
13668 - in->variantType = oh->type;
13669 +void yaffs_link_fixup(yaffs_dev_t *dev, yaffs_obj_t *hard_list)
13674 - in->yst_mode = oh->yst_mode;
13675 -#ifdef CONFIG_YAFFS_WINCE
13676 - in->win_atime[0] = oh->win_atime[0];
13677 - in->win_ctime[0] = oh->win_ctime[0];
13678 - in->win_mtime[0] = oh->win_mtime[0];
13679 - in->win_atime[1] = oh->win_atime[1];
13680 - in->win_ctime[1] = oh->win_ctime[1];
13681 - in->win_mtime[1] = oh->win_mtime[1];
13683 - in->yst_uid = oh->yst_uid;
13684 - in->yst_gid = oh->yst_gid;
13685 - in->yst_atime = oh->yst_atime;
13686 - in->yst_mtime = oh->yst_mtime;
13687 - in->yst_ctime = oh->yst_ctime;
13688 - in->yst_rdev = oh->yst_rdev;
13690 + while (hard_list) {
13692 + hard_list = (yaffs_obj_t *) (hard_list->hard_links.next);
13694 + in = yaffs_find_by_number(dev,
13695 + hl->variant.hardlink_variant.
13698 - if (oh->shadowsObject > 0)
13699 - yaffs_HandleShadowedObject(dev,
13705 - yaffs_SetObjectName(in, oh->name);
13707 - yaffs_FindOrCreateObjectByNumber
13708 - (dev, oh->parentObjectId,
13709 - YAFFS_OBJECT_TYPE_DIRECTORY);
13711 - fileSize = oh->fileSize;
13712 - isShrink = oh->isShrink;
13713 - equivalentObjectId = oh->equivalentObjectId;
13715 + /* Add the hardlink pointers */
13716 + hl->variant.hardlink_variant.equiv_obj = in;
13717 + ylist_add(&hl->hard_links, &in->hard_links);
13719 + /* Todo Need to report/handle this better.
13720 + * Got a problem... hardlink to a non-existant object
13722 + hl->variant.hardlink_variant.equiv_obj = NULL;
13723 + YINIT_LIST_HEAD(&hl->hard_links);
13726 - in->variantType = tags.extraObjectType;
13728 - yaffs_FindOrCreateObjectByNumber
13729 - (dev, tags.extraParentObjectId,
13730 - YAFFS_OBJECT_TYPE_DIRECTORY);
13731 - fileSize = tags.extraFileLength;
13732 - isShrink = tags.extraIsShrinkHeader;
13733 - equivalentObjectId = tags.extraEquivalentObjectId;
13734 - in->lazyLoaded = 1;
13743 - alloc_failed = 1;
13744 +static void yaffs_strip_deleted_objs(yaffs_dev_t *dev)
13747 + * Sort out state of unlinked and deleted objects after scanning.
13749 + struct ylist_head *i;
13750 + struct ylist_head *n;
13753 - /* directory stuff...
13754 - * hook up to parent
13756 + if (dev->read_only)
13759 - if (parent && parent->variantType ==
13760 - YAFFS_OBJECT_TYPE_UNKNOWN) {
13761 - /* Set up as a directory */
13762 - parent->variantType =
13763 - YAFFS_OBJECT_TYPE_DIRECTORY;
13764 - YINIT_LIST_HEAD(&parent->variant.
13765 - directoryVariant.
13767 - } else if (!parent || parent->variantType !=
13768 - YAFFS_OBJECT_TYPE_DIRECTORY) {
13769 - /* Hoosterman, another problem....
13770 - * We're trying to use a non-directory as a directory
13772 + /* Soft delete all the unlinked files */
13773 + ylist_for_each_safe(i, n,
13774 + &dev->unlinked_dir->variant.dir_variant.children) {
13776 + l = ylist_entry(i, yaffs_obj_t, siblings);
13777 + yaffs_del_obj(l);
13781 - T(YAFFS_TRACE_ERROR,
13783 - ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
13785 - parent = dev->lostNFoundDir;
13787 + ylist_for_each_safe(i, n,
13788 + &dev->del_dir->variant.dir_variant.children) {
13790 + l = ylist_entry(i, yaffs_obj_t, siblings);
13791 + yaffs_del_obj(l);
13795 - yaffs_AddObjectToDirectory(parent, in);
13798 - itsUnlinked = (parent == dev->deletedDir) ||
13799 - (parent == dev->unlinkedDir);
13801 + * This code iterates through all the objects making sure that they are rooted.
13802 + * Any unrooted objects are re-rooted in lost+found.
13803 + * An object needs to be in one of:
13804 + * - Directly under deleted, unlinked
13805 + * - Directly or indirectly under root.
13808 + * This code assumes that we don't ever change the current relationships between
13810 + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
13811 + * lostNfound->parent == root_dir
13813 + * This fixes the problem where directories might have inadvertently been deleted
13814 + * leaving the object "hanging" without being rooted in the directory tree.
13817 +static int yaffs_has_null_parent(yaffs_dev_t *dev, yaffs_obj_t *obj)
13819 + return (obj == dev->del_dir ||
13820 + obj == dev->unlinked_dir||
13821 + obj == dev->root_dir);
13825 - /* Mark the block as having a shrinkHeader */
13826 - bi->hasShrinkHeader = 1;
13828 +static void yaffs_fix_hanging_objs(yaffs_dev_t *dev)
13830 + yaffs_obj_t *obj;
13831 + yaffs_obj_t *parent;
13833 + struct ylist_head *lh;
13834 + struct ylist_head *n;
13838 - /* Note re hardlinks.
13839 - * Since we might scan a hardlink before its equivalent object is scanned
13840 - * we put them all in a list.
13841 - * After scanning is complete, we should have all the objects, so we run
13842 - * through this list and fix up all the chains.
13844 + if (dev->read_only)
13847 - switch (in->variantType) {
13848 - case YAFFS_OBJECT_TYPE_UNKNOWN:
13849 - /* Todo got a problem */
13851 - case YAFFS_OBJECT_TYPE_FILE:
13853 - if (in->variant.fileVariant.
13854 - scannedFileSize < fileSize) {
13855 - /* This covers the case where the file size is greater
13856 - * than where the data is
13857 - * This will happen if the file is resized to be larger
13858 - * than its current data extents.
13860 - in->variant.fileVariant.fileSize = fileSize;
13861 - in->variant.fileVariant.scannedFileSize =
13862 - in->variant.fileVariant.fileSize;
13864 + /* Iterate through the objects in each hash entry,
13865 + * looking at each object.
13866 + * Make sure it is rooted.
13870 - in->variant.fileVariant.shrinkSize > fileSize) {
13871 - in->variant.fileVariant.shrinkSize = fileSize;
13873 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
13874 + ylist_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
13876 + obj = ylist_entry(lh, yaffs_obj_t, hash_link);
13877 + parent= obj->parent;
13879 + if(yaffs_has_null_parent(dev,obj)){
13880 + /* These directories are not hanging */
13883 + else if(!parent || parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
13885 + else if(yaffs_has_null_parent(dev,parent))
13889 + * Need to follow the parent chain to see if it is hanging.
13895 - case YAFFS_OBJECT_TYPE_HARDLINK:
13896 - if (!itsUnlinked) {
13897 - in->variant.hardLinkVariant.equivalentObjectId =
13898 - equivalentObjectId;
13899 - in->hardLinks.next =
13900 - (struct ylist_head *) hardList;
13904 - case YAFFS_OBJECT_TYPE_DIRECTORY:
13907 - case YAFFS_OBJECT_TYPE_SPECIAL:
13910 - case YAFFS_OBJECT_TYPE_SYMLINK:
13912 - in->variant.symLinkVariant.alias =
13913 - yaffs_CloneString(oh->alias);
13914 - if (!in->variant.symLinkVariant.alias)
13915 - alloc_failed = 1;
13918 + while(parent != dev->root_dir &&
13919 + parent->parent &&
13920 + parent->parent->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
13922 + parent = parent->parent;
13926 + if(parent != dev->root_dir)
13930 + T(YAFFS_TRACE_SCAN,
13931 + (TSTR("Hanging object %d moved to lost and found" TENDSTR),
13933 + yaffs_add_obj_to_dir(dev->lost_n_found,obj);
13941 - } /* End of scanning for each chunk */
13943 - if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
13944 - /* If we got this far while scanning, then the block is fully allocated. */
13945 - state = YAFFS_BLOCK_STATE_FULL;
13948 + * Delete directory contents for cleaning up lost and found.
13950 +static void yaffs_del_dir_contents(yaffs_obj_t *dir)
13952 + yaffs_obj_t *obj;
13953 + struct ylist_head *lh;
13954 + struct ylist_head *n;
13956 - bi->blockState = state;
13957 + if(dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
13960 + ylist_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
13962 + obj = ylist_entry(lh, yaffs_obj_t, siblings);
13963 + if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
13964 + yaffs_del_dir_contents(obj);
13966 + T(YAFFS_TRACE_SCAN,
13967 + (TSTR("Deleting lost_found object %d" TENDSTR),
13970 - /* Now let's see if it was dirty */
13971 - if (bi->pagesInUse == 0 &&
13972 - !bi->hasShrinkHeader &&
13973 - bi->blockState == YAFFS_BLOCK_STATE_FULL) {
13974 - yaffs_BlockBecameDirty(dev, blk);
13975 + /* Need to use UnlinkObject since Delete would not handle
13976 + * hardlinked objects correctly.
13978 + yaffs_unlink_obj(obj);
13985 - if (altBlockIndex)
13986 - YFREE_ALT(blockIndex);
13988 - YFREE(blockIndex);
13989 +static void yaffs_empty_l_n_f(yaffs_dev_t *dev)
13991 + yaffs_del_dir_contents(dev->lost_n_found);
13994 - /* Ok, we've done all the scanning.
13995 - * Fix up the hard link chains.
13996 - * We should now have scanned all the objects, now it's time to add these
13999 - yaffs_HardlinkFixup(dev, hardList);
14000 +static void yaffs_check_obj_details_loaded(yaffs_obj_t *in)
14003 + yaffs_obj_header *oh;
14004 + yaffs_dev_t *dev;
14005 + yaffs_ext_tags tags;
14007 + int alloc_failed = 0;
14012 - yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
14013 + dev = in->my_dev;
14015 - if (alloc_failed)
14016 - return YAFFS_FAIL;
14018 + T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
14020 + in->lazy_loaded ? "not yet" : "already"));
14023 - T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
14024 + if (in->lazy_loaded && in->hdr_chunk > 0) {
14025 + in->lazy_loaded = 0;
14026 + chunkData = yaffs_get_temp_buffer(dev, __LINE__);
14030 + result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, chunkData, &tags);
14031 + oh = (yaffs_obj_header *) chunkData;
14033 -/*------------------------------ Directory Functions ----------------------------- */
14034 + in->yst_mode = oh->yst_mode;
14035 +#ifdef CONFIG_YAFFS_WINCE
14036 + in->win_atime[0] = oh->win_atime[0];
14037 + in->win_ctime[0] = oh->win_ctime[0];
14038 + in->win_mtime[0] = oh->win_mtime[0];
14039 + in->win_atime[1] = oh->win_atime[1];
14040 + in->win_ctime[1] = oh->win_ctime[1];
14041 + in->win_mtime[1] = oh->win_mtime[1];
14043 + in->yst_uid = oh->yst_uid;
14044 + in->yst_gid = oh->yst_gid;
14045 + in->yst_atime = oh->yst_atime;
14046 + in->yst_mtime = oh->yst_mtime;
14047 + in->yst_ctime = oh->yst_ctime;
14048 + in->yst_rdev = oh->yst_rdev;
14050 -static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
14052 - struct ylist_head *lh;
14053 - yaffs_Object *listObj;
14055 + yaffs_set_obj_name_from_oh(in, oh);
14058 + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
14059 + in->variant.symlink_variant.alias =
14060 + yaffs_clone_str(oh->alias);
14061 + if (!in->variant.symlink_variant.alias)
14062 + alloc_failed = 1; /* Not returned to caller */
14066 - T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
14069 + yaffs_release_temp_buffer(dev, chunkData, __LINE__);
14073 - if (yaffs_SkipVerification(obj->myDev))
14075 +/*------------------------------ Directory Functions ----------------------------- */
14077 - if (!obj->parent) {
14078 - T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
14081 + *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
14082 + * link (ie. name) is created or deleted in the directory.
14085 + * create dir/a : update dir's mtime/ctime
14086 + * rm dir/a: update dir's mtime/ctime
14087 + * modify dir/a: don't update dir's mtimme/ctime
14089 + * This can be handled immediately or defered. Defering helps reduce the number
14090 + * of updates when many files in a directory are changed within a brief period.
14092 + * If the directory updating is defered then yaffs_update_dirty_dirs must be
14093 + * called periodically.
14096 +static void yaffs_update_parent(yaffs_obj_t *obj)
14098 + yaffs_dev_t *dev;
14103 - if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
14104 - T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
14108 - /* Iterate through the objects in each hash entry */
14109 +#ifndef CONFIG_YAFFS_WINCE
14111 - ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
14113 - listObj = ylist_entry(lh, yaffs_Object, siblings);
14114 - yaffs_VerifyObject(listObj);
14115 - if (obj == listObj)
14117 + dev = obj->my_dev;
14119 + obj->yst_mtime = obj->yst_ctime = Y_CURRENT_TIME;
14120 + if(dev->param.defered_dir_update){
14121 + struct ylist_head *link = &obj->variant.dir_variant.dirty;
14123 + if(ylist_empty(link)){
14124 + ylist_add(link,&dev->dirty_dirs);
14125 + T(YAFFS_TRACE_BACKGROUND, (TSTR("Added object %d to dirty directories" TENDSTR),obj->obj_id));
14129 - if (count != 1) {
14130 - T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
14134 + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
14138 -static void yaffs_VerifyDirectory(yaffs_Object *directory)
14139 +void yaffs_update_dirty_dirs(yaffs_dev_t *dev)
14141 - struct ylist_head *lh;
14142 - yaffs_Object *listObj;
14144 - if (!directory) {
14148 + struct ylist_head *link;
14149 + yaffs_obj_t *obj;
14151 + yaffs_obj_variant *oV;
14153 - if (yaffs_SkipFullVerification(directory->myDev))
14155 + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update dirty directories" TENDSTR)));
14157 - if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
14158 - T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
14161 + while(!ylist_empty(&dev->dirty_dirs)){
14162 + link = dev->dirty_dirs.next;
14163 + ylist_del_init(link);
14165 + dS=ylist_entry(link,yaffs_dir_s,dirty);
14166 + oV = ylist_entry(dS,yaffs_obj_variant,dir_variant);
14167 + obj = ylist_entry(oV,yaffs_obj_t,variant);
14169 - /* Iterate through the objects in each hash entry */
14170 + T(YAFFS_TRACE_BACKGROUND, (TSTR("Update directory %d" TENDSTR), obj->obj_id));
14172 - ylist_for_each(lh, &directory->variant.directoryVariant.children) {
14174 - listObj = ylist_entry(lh, yaffs_Object, siblings);
14175 - if (listObj->parent != directory) {
14176 - T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
14179 - yaffs_VerifyObjectInDirectory(listObj);
14182 + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
14187 -static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
14188 +static void yaffs_remove_obj_from_dir(yaffs_obj_t *obj)
14190 - yaffs_Device *dev = obj->myDev;
14191 - yaffs_Object *parent;
14192 + yaffs_dev_t *dev = obj->my_dev;
14193 + yaffs_obj_t *parent;
14195 - yaffs_VerifyObjectInDirectory(obj);
14196 + yaffs_verify_obj_in_dir(obj);
14197 parent = obj->parent;
14199 - yaffs_VerifyDirectory(parent);
14200 + yaffs_verify_dir(parent);
14202 - if (dev && dev->removeObjectCallback)
14203 - dev->removeObjectCallback(obj);
14204 + if (dev && dev->param.remove_obj_fn)
14205 + dev->param.remove_obj_fn(obj);
14208 ylist_del_init(&obj->siblings);
14209 obj->parent = NULL;
14211 - yaffs_VerifyDirectory(parent);
14213 + yaffs_verify_dir(parent);
14217 -static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
14218 - yaffs_Object *obj)
14219 +void yaffs_add_obj_to_dir(yaffs_obj_t *directory,
14220 + yaffs_obj_t *obj)
14223 T(YAFFS_TRACE_ALWAYS,
14224 @@ -6699,7 +4495,7 @@ static void yaffs_AddObjectToDirectory(y
14228 - if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
14229 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
14230 T(YAFFS_TRACE_ALWAYS,
14232 ("tragedy: Trying to add an object to a non-directory"
14233 @@ -6713,27 +4509,27 @@ static void yaffs_AddObjectToDirectory(y
14237 - yaffs_VerifyDirectory(directory);
14238 + yaffs_verify_dir(directory);
14240 - yaffs_RemoveObjectFromDirectory(obj);
14241 + yaffs_remove_obj_from_dir(obj);
14245 - ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
14246 + ylist_add(&obj->siblings, &directory->variant.dir_variant.children);
14247 obj->parent = directory;
14249 - if (directory == obj->myDev->unlinkedDir
14250 - || directory == obj->myDev->deletedDir) {
14251 + if (directory == obj->my_dev->unlinked_dir
14252 + || directory == obj->my_dev->del_dir) {
14254 - obj->myDev->nUnlinkedFiles++;
14255 - obj->renameAllowed = 0;
14256 + obj->my_dev->n_unlinked_files++;
14257 + obj->rename_allowed = 0;
14260 - yaffs_VerifyDirectory(directory);
14261 - yaffs_VerifyObjectInDirectory(obj);
14262 + yaffs_verify_dir(directory);
14263 + yaffs_verify_obj_in_dir(obj);
14266 -yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
14267 +yaffs_obj_t *yaffs_find_by_name(yaffs_obj_t *directory,
14271 @@ -6741,7 +4537,7 @@ yaffs_Object *yaffs_FindObjectByName(yaf
14272 struct ylist_head *i;
14273 YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
14280 @@ -6749,39 +4545,39 @@ yaffs_Object *yaffs_FindObjectByName(yaf
14282 T(YAFFS_TRACE_ALWAYS,
14284 - ("tragedy: yaffs_FindObjectByName: null pointer directory"
14285 + ("tragedy: yaffs_find_by_name: null pointer directory"
14290 - if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
14291 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
14292 T(YAFFS_TRACE_ALWAYS,
14294 - ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
14295 + ("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
14299 - sum = yaffs_CalcNameSum(name);
14300 + sum = yaffs_calc_name_sum(name);
14302 - ylist_for_each(i, &directory->variant.directoryVariant.children) {
14303 + ylist_for_each(i, &directory->variant.dir_variant.children) {
14305 - l = ylist_entry(i, yaffs_Object, siblings);
14306 + l = ylist_entry(i, yaffs_obj_t, siblings);
14308 if (l->parent != directory)
14311 - yaffs_CheckObjectDetailsLoaded(l);
14312 + yaffs_check_obj_details_loaded(l);
14314 /* Special case for lost-n-found */
14315 - if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
14316 + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
14317 if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
14319 - } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
14320 + } else if (yaffs_sum_cmp(l->sum, sum) || l->hdr_chunk <= 0) {
14321 /* LostnFound chunk called Objxxx
14324 - yaffs_GetObjectName(l, buffer,
14325 - YAFFS_MAX_NAME_LENGTH);
14326 + yaffs_get_obj_name(l, buffer,
14327 + YAFFS_MAX_NAME_LENGTH + 1);
14328 if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
14331 @@ -6793,31 +4589,31 @@ yaffs_Object *yaffs_FindObjectByName(yaf
14335 -int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
14336 - int (*fn) (yaffs_Object *))
14337 +int yaffs_ApplyToDirectoryChildren(yaffs_obj_t *the_dir,
14338 + int (*fn) (yaffs_obj_t *))
14340 struct ylist_head *i;
14346 T(YAFFS_TRACE_ALWAYS,
14348 - ("tragedy: yaffs_FindObjectByName: null pointer directory"
14349 + ("tragedy: yaffs_find_by_name: null pointer directory"
14354 - if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
14355 + if (the_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
14356 T(YAFFS_TRACE_ALWAYS,
14358 - ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
14359 + ("tragedy: yaffs_find_by_name: non-directory" TENDSTR)));
14364 - ylist_for_each(i, &theDir->variant.directoryVariant.children) {
14365 + ylist_for_each(i, &the_dir->variant.dir_variant.children) {
14367 - l = ylist_entry(i, yaffs_Object, siblings);
14368 + l = ylist_entry(i, yaffs_obj_t, siblings);
14372 @@ -6832,82 +4628,175 @@ int yaffs_ApplyToDirectoryChildren(yaffs
14376 -yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
14377 +yaffs_obj_t *yaffs_get_equivalent_obj(yaffs_obj_t *obj)
14379 - if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
14380 + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
14381 /* We want the object id of the equivalent object, not this one */
14382 - obj = obj->variant.hardLinkVariant.equivalentObject;
14383 - yaffs_CheckObjectDetailsLoaded(obj);
14384 + obj = obj->variant.hardlink_variant.equiv_obj;
14385 + yaffs_check_obj_details_loaded(obj);
14390 -int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
14392 - memset(name, 0, buffSize * sizeof(YCHAR));
14394 - yaffs_CheckObjectDetailsLoaded(obj);
14396 + * A note or two on object names.
14397 + * * If the object name is missing, we then make one up in the form objnnn
14399 + * * ASCII names are stored in the object header's name field from byte zero
14400 + * * Unicode names are historically stored starting from byte zero.
14402 + * Then there are automatic Unicode names...
14403 + * The purpose of these is to save names in a way that can be read as
14404 + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
14405 + * system to share files.
14407 + * These automatic unicode are stored slightly differently...
14408 + * - If the name can fit in the ASCII character space then they are saved as
14409 + * ascii names as per above.
14410 + * - If the name needs Unicode then the name is saved in Unicode
14411 + * starting at oh->name[1].
14413 - if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
14414 - yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
14415 - } else if (obj->hdrChunk <= 0) {
14417 +static void yaffs_fix_null_name(yaffs_obj_t * obj,YCHAR * name, int buffer_size)
14419 + /* Create an object name if we could not find one. */
14420 + if(yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH) == 0){
14422 YCHAR numString[20];
14423 YCHAR *x = &numString[19];
14424 - unsigned v = obj->objectId;
14425 + unsigned v = obj->obj_id;
14430 *x = '0' + (v % 10);
14433 /* make up a name */
14434 yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
14435 - yaffs_strcat(locName, x);
14436 - yaffs_strncpy(name, locName, buffSize - 1);
14437 + yaffs_strcat(locName,x);
14438 + yaffs_strncpy(name, locName, buffer_size - 1);
14442 +static void yaffs_load_name_from_oh(yaffs_dev_t *dev,YCHAR *name, const YCHAR *ohName, int bufferSize)
14444 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
14445 + if(dev->param.auto_unicode){
14447 + /* It is an ASCII name, so do an ASCII to unicode conversion */
14448 + const char *asciiOhName = (const char *)ohName;
14449 + int n = bufferSize - 1;
14450 + while(n > 0 && *asciiOhName){
14451 + *name = *asciiOhName;
14457 + yaffs_strncpy(name,ohName+1, bufferSize -1);
14460 + yaffs_strncpy(name, ohName, bufferSize - 1);
14464 +static void yaffs_load_oh_from_name(yaffs_dev_t *dev, YCHAR *ohName, const YCHAR *name)
14466 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
14471 + if(dev->param.auto_unicode){
14476 + /* Figure out if the name will fit in ascii character set */
14477 + while(isAscii && *w){
14478 + if((*w) & 0xff00)
14484 + /* It is an ASCII name, so do a unicode to ascii conversion */
14485 + char *asciiOhName = (char *)ohName;
14486 + int n = YAFFS_MAX_NAME_LENGTH - 1;
14487 + while(n > 0 && *name){
14488 + *asciiOhName= *name;
14494 + /* It is a unicode name, so save starting at the second YCHAR */
14496 + yaffs_strncpy(ohName+1,name, YAFFS_MAX_NAME_LENGTH -2);
14501 + yaffs_strncpy(ohName,name, YAFFS_MAX_NAME_LENGTH - 1);
14505 +int yaffs_get_obj_name(yaffs_obj_t * obj, YCHAR * name, int buffer_size)
14507 + memset(name, 0, buffer_size * sizeof(YCHAR));
14509 + yaffs_check_obj_details_loaded(obj);
14511 + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
14512 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
14514 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
14515 - else if (obj->shortName[0])
14516 - yaffs_strcpy(name, obj->shortName);
14517 + else if (obj->short_name[0]) {
14518 + yaffs_strcpy(name, obj->short_name);
14522 + else if(obj->hdr_chunk > 0) {
14524 - __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
14525 + __u8 *buffer = yaffs_get_temp_buffer(obj->my_dev, __LINE__);
14527 - yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
14528 + yaffs_obj_header *oh = (yaffs_obj_header *) buffer;
14530 - memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
14531 + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
14533 - if (obj->hdrChunk > 0) {
14534 - result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
14535 - obj->hdrChunk, buffer,
14536 + if (obj->hdr_chunk > 0) {
14537 + result = yaffs_rd_chunk_tags_nand(obj->my_dev,
14538 + obj->hdr_chunk, buffer,
14541 - yaffs_strncpy(name, oh->name, buffSize - 1);
14542 + yaffs_load_name_from_oh(obj->my_dev,name,oh->name,buffer_size);
14544 - yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
14545 + yaffs_release_temp_buffer(obj->my_dev, buffer, __LINE__);
14548 - return yaffs_strlen(name);
14549 + yaffs_fix_null_name(obj,name,buffer_size);
14551 + return yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH);
14554 -int yaffs_GetObjectFileLength(yaffs_Object *obj)
14556 +int yaffs_get_obj_length(yaffs_obj_t *obj)
14558 /* Dereference any hard linking */
14559 - obj = yaffs_GetEquivalentObject(obj);
14560 + obj = yaffs_get_equivalent_obj(obj);
14562 - if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
14563 - return obj->variant.fileVariant.fileSize;
14564 - if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
14565 - return yaffs_strlen(obj->variant.symLinkVariant.alias);
14567 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
14568 + return obj->variant.file_variant.file_size;
14569 + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
14570 + if(!obj->variant.symlink_variant.alias)
14572 + return yaffs_strnlen(obj->variant.symlink_variant.alias,YAFFS_MAX_ALIAS_LENGTH);
14574 /* Only a directory should drop through to here */
14575 - return obj->myDev->nDataBytesPerChunk;
14576 + return obj->my_dev->data_bytes_per_chunk;
14580 -int yaffs_GetObjectLinkCount(yaffs_Object *obj)
14581 +int yaffs_get_obj_link_count(yaffs_obj_t *obj)
14584 struct ylist_head *i;
14585 @@ -6915,24 +4804,24 @@ int yaffs_GetObjectLinkCount(yaffs_Objec
14586 if (!obj->unlinked)
14587 count++; /* the object itself */
14589 - ylist_for_each(i, &obj->hardLinks)
14590 + ylist_for_each(i, &obj->hard_links)
14591 count++; /* add the hard links; */
14596 -int yaffs_GetObjectInode(yaffs_Object *obj)
14597 +int yaffs_get_obj_inode(yaffs_obj_t *obj)
14599 - obj = yaffs_GetEquivalentObject(obj);
14600 + obj = yaffs_get_equivalent_obj(obj);
14602 - return obj->objectId;
14603 + return obj->obj_id;
14606 -unsigned yaffs_GetObjectType(yaffs_Object *obj)
14607 +unsigned yaffs_get_obj_type(yaffs_obj_t *obj)
14609 - obj = yaffs_GetEquivalentObject(obj);
14610 + obj = yaffs_get_equivalent_obj(obj);
14612 - switch (obj->variantType) {
14613 + switch (obj->variant_type) {
14614 case YAFFS_OBJECT_TYPE_FILE:
14617 @@ -6960,18 +4849,18 @@ unsigned yaffs_GetObjectType(yaffs_Objec
14621 -YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
14622 +YCHAR *yaffs_get_symlink_alias(yaffs_obj_t *obj)
14624 - obj = yaffs_GetEquivalentObject(obj);
14625 - if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
14626 - return yaffs_CloneString(obj->variant.symLinkVariant.alias);
14627 + obj = yaffs_get_equivalent_obj(obj);
14628 + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
14629 + return yaffs_clone_str(obj->variant.symlink_variant.alias);
14631 - return yaffs_CloneString(_Y(""));
14632 + return yaffs_clone_str(_Y(""));
14635 #ifndef CONFIG_YAFFS_WINCE
14637 -int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
14638 +int yaffs_set_attribs(yaffs_obj_t *obj, struct iattr *attr)
14640 unsigned int valid = attr->ia_valid;
14642 @@ -6990,14 +4879,14 @@ int yaffs_SetAttributes(yaffs_Object *ob
14643 obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
14645 if (valid & ATTR_SIZE)
14646 - yaffs_ResizeFile(obj, attr->ia_size);
14647 + yaffs_resize_file(obj, attr->ia_size);
14649 - yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
14650 + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
14655 -int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
14656 +int yaffs_get_attribs(yaffs_obj_t *obj, struct iattr *attr)
14658 unsigned int valid = 0;
14660 @@ -7015,7 +4904,7 @@ int yaffs_GetAttributes(yaffs_Object *ob
14661 Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
14662 valid |= ATTR_MTIME;
14664 - attr->ia_size = yaffs_GetFileSize(obj);
14665 + attr->ia_size = yaffs_get_file_size(obj);
14666 valid |= ATTR_SIZE;
14668 attr->ia_valid = valid;
14669 @@ -7025,20 +4914,137 @@ int yaffs_GetAttributes(yaffs_Object *ob
14674 +static int yaffs_do_xattrib_mod(yaffs_obj_t *obj, int set, const YCHAR *name, const void *value, int size, int flags)
14676 + yaffs_xattr_mod xmod;
14681 + xmod.name = name;
14682 + xmod.data = value;
14683 + xmod.size = size;
14684 + xmod.flags = flags;
14685 + xmod.result = -ENOSPC;
14687 + result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
14690 + return xmod.result;
14695 +static int yaffs_apply_xattrib_mod(yaffs_obj_t *obj, char *buffer, yaffs_xattr_mod *xmod)
14698 + int x_offs = sizeof(yaffs_obj_header);
14699 + yaffs_dev_t *dev = obj->my_dev;
14700 + int x_size = dev->data_bytes_per_chunk - sizeof(yaffs_obj_header);
14702 + char * x_buffer = buffer + x_offs;
14705 + retval = nval_set(x_buffer, x_size, xmod->name, xmod->data, xmod->size, xmod->flags);
14707 + retval = nval_del(x_buffer, x_size, xmod->name);
14709 + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
14710 + obj->xattr_known = 1;
14712 + xmod->result = retval;
14717 +static int yaffs_do_xattrib_fetch(yaffs_obj_t *obj, const YCHAR *name, void *value, int size)
14719 + char *buffer = NULL;
14721 + yaffs_ext_tags tags;
14722 + yaffs_dev_t *dev = obj->my_dev;
14723 + int x_offs = sizeof(yaffs_obj_header);
14724 + int x_size = dev->data_bytes_per_chunk - sizeof(yaffs_obj_header);
14730 + if(obj->hdr_chunk < 1)
14733 + /* If we know that the object has no xattribs then don't do all the
14734 + * reading and parsing.
14736 + if(obj->xattr_known && !obj->has_xattr){
14743 + buffer = (char *) yaffs_get_temp_buffer(dev, __LINE__);
14747 + result = yaffs_rd_chunk_tags_nand(dev,obj->hdr_chunk, (__u8 *)buffer, &tags);
14749 + if(result != YAFFS_OK)
14750 + retval = -ENOENT;
14752 + x_buffer = buffer + x_offs;
14754 + if (!obj->xattr_known){
14755 + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
14756 + obj->xattr_known = 1;
14760 + retval = nval_get(x_buffer, x_size, name, value, size);
14762 + retval = nval_list(x_buffer, x_size, value,size);
14764 + yaffs_release_temp_buffer(dev,(__u8 *)buffer,__LINE__);
14768 +int yaffs_set_xattrib(yaffs_obj_t *obj, const YCHAR *name, const void * value, int size, int flags)
14770 + return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
14773 +int yaffs_remove_xattrib(yaffs_obj_t *obj, const YCHAR *name)
14775 + return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
14778 +int yaffs_get_xattrib(yaffs_obj_t *obj, const YCHAR *name, void *value, int size)
14780 + return yaffs_do_xattrib_fetch(obj, name, value, size);
14783 +int yaffs_list_xattrib(yaffs_obj_t *obj, char *buffer, int size)
14785 + return yaffs_do_xattrib_fetch(obj, NULL, buffer,size);
14791 -int yaffs_DumpObject(yaffs_Object *obj)
14792 +int yaffs_dump_obj(yaffs_obj_t *obj)
14796 - yaffs_GetObjectName(obj, name, 256);
14797 + yaffs_get_obj_name(obj, name, YAFFS_MAX_NAME_LENGTH + 1);
14799 T(YAFFS_TRACE_ALWAYS,
14801 ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
14802 " chunk %d type %d size %d\n"
14803 - TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
14804 - obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
14805 - yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
14806 + TENDSTR), obj->obj_id, yaffs_get_obj_inode(obj), name,
14807 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdr_chunk,
14808 + yaffs_get_obj_type(obj), yaffs_get_obj_length(obj)));
14812 @@ -7046,72 +5052,74 @@ int yaffs_DumpObject(yaffs_Object *obj)
14814 /*---------------------------- Initialisation code -------------------------------------- */
14816 -static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
14817 +static int yaffs_cehck_dev_fns(const yaffs_dev_t *dev)
14820 /* Common functions, gotta have */
14821 - if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
14822 + if (!dev->param.erase_fn || !dev->param.initialise_flash_fn)
14825 #ifdef CONFIG_YAFFS_YAFFS2
14827 /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
14828 - if (dev->writeChunkWithTagsToNAND &&
14829 - dev->readChunkWithTagsFromNAND &&
14830 - !dev->writeChunkToNAND &&
14831 - !dev->readChunkFromNAND &&
14832 - dev->markNANDBlockBad && dev->queryNANDBlock)
14833 + if (dev->param.write_chunk_tags_fn &&
14834 + dev->param.read_chunk_tags_fn &&
14835 + !dev->param.write_chunk_fn &&
14836 + !dev->param.read_chunk_fn &&
14837 + dev->param.bad_block_fn &&
14838 + dev->param.query_block_fn)
14842 /* Can use the "spare" style interface for yaffs1 */
14843 - if (!dev->isYaffs2 &&
14844 - !dev->writeChunkWithTagsToNAND &&
14845 - !dev->readChunkWithTagsFromNAND &&
14846 - dev->writeChunkToNAND &&
14847 - dev->readChunkFromNAND &&
14848 - !dev->markNANDBlockBad && !dev->queryNANDBlock)
14849 + if (!dev->param.is_yaffs2 &&
14850 + !dev->param.write_chunk_tags_fn &&
14851 + !dev->param.read_chunk_tags_fn &&
14852 + dev->param.write_chunk_fn &&
14853 + dev->param.read_chunk_fn &&
14854 + !dev->param.bad_block_fn &&
14855 + !dev->param.query_block_fn)
14858 - return 0; /* bad */
14859 + return 0; /* bad */
14863 -static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
14864 +static int yaffs_create_initial_dir(yaffs_dev_t *dev)
14866 /* Initialise the unlinked, deleted, root and lost and found directories */
14868 - dev->lostNFoundDir = dev->rootDir = NULL;
14869 - dev->unlinkedDir = dev->deletedDir = NULL;
14870 + dev->lost_n_found = dev->root_dir = NULL;
14871 + dev->unlinked_dir = dev->del_dir = NULL;
14873 - dev->unlinkedDir =
14874 - yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
14875 + dev->unlinked_dir =
14876 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
14878 - dev->deletedDir =
14879 - yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
14881 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
14884 - yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
14886 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
14887 YAFFS_ROOT_MODE | S_IFDIR);
14888 - dev->lostNFoundDir =
14889 - yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
14890 + dev->lost_n_found =
14891 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
14892 YAFFS_LOSTNFOUND_MODE | S_IFDIR);
14894 - if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
14895 - yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
14896 + if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir && dev->del_dir) {
14897 + yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
14904 -int yaffs_GutsInitialise(yaffs_Device *dev)
14905 +int yaffs_guts_initialise(yaffs_dev_t *dev)
14907 int init_failed = 0;
14911 - T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
14912 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_guts_initialise()" TENDSTR)));
14914 /* Check stuff that must be set */
14916 @@ -7120,52 +5128,52 @@ int yaffs_GutsInitialise(yaffs_Device *d
14920 - dev->internalStartBlock = dev->startBlock;
14921 - dev->internalEndBlock = dev->endBlock;
14922 - dev->blockOffset = 0;
14923 - dev->chunkOffset = 0;
14924 - dev->nFreeChunks = 0;
14926 - dev->gcBlock = -1;
14928 - if (dev->startBlock == 0) {
14929 - dev->internalStartBlock = dev->startBlock + 1;
14930 - dev->internalEndBlock = dev->endBlock + 1;
14931 - dev->blockOffset = 1;
14932 - dev->chunkOffset = dev->nChunksPerBlock;
14933 + dev->internal_start_block = dev->param.start_block;
14934 + dev->internal_end_block = dev->param.end_block;
14935 + dev->block_offset = 0;
14936 + dev->chunk_offset = 0;
14937 + dev->n_free_chunks = 0;
14939 + dev->gc_block = 0;
14941 + if (dev->param.start_block == 0) {
14942 + dev->internal_start_block = dev->param.start_block + 1;
14943 + dev->internal_end_block = dev->param.end_block + 1;
14944 + dev->block_offset = 1;
14945 + dev->chunk_offset = dev->param.chunks_per_block;
14948 /* Check geometry parameters. */
14950 - if ((!dev->inbandTags && dev->isYaffs2 && dev->totalBytesPerChunk < 1024) ||
14951 - (!dev->isYaffs2 && dev->totalBytesPerChunk < 512) ||
14952 - (dev->inbandTags && !dev->isYaffs2) ||
14953 - dev->nChunksPerBlock < 2 ||
14954 - dev->nReservedBlocks < 2 ||
14955 - dev->internalStartBlock <= 0 ||
14956 - dev->internalEndBlock <= 0 ||
14957 - dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2)) { /* otherwise it is too small */
14958 + if ((!dev->param.inband_tags && dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 1024) ||
14959 + (!dev->param.is_yaffs2 && dev->param.total_bytes_per_chunk < 512) ||
14960 + (dev->param.inband_tags && !dev->param.is_yaffs2) ||
14961 + dev->param.chunks_per_block < 2 ||
14962 + dev->param.n_reserved_blocks < 2 ||
14963 + dev->internal_start_block <= 0 ||
14964 + dev->internal_end_block <= 0 ||
14965 + dev->internal_end_block <= (dev->internal_start_block + dev->param.n_reserved_blocks + 2)) { /* otherwise it is too small */
14966 T(YAFFS_TRACE_ALWAYS,
14968 - ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
14969 - TENDSTR), dev->totalBytesPerChunk, dev->isYaffs2 ? "2" : "", dev->inbandTags));
14970 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d "
14971 + TENDSTR), dev->param.total_bytes_per_chunk, dev->param.is_yaffs2 ? "2" : "", dev->param.inband_tags));
14975 - if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
14976 + if (yaffs_init_nand(dev) != YAFFS_OK) {
14977 T(YAFFS_TRACE_ALWAYS,
14978 (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
14982 /* Sort out space for inband tags, if required */
14983 - if (dev->inbandTags)
14984 - dev->nDataBytesPerChunk = dev->totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
14985 + if (dev->param.inband_tags)
14986 + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk - sizeof(yaffs_PackedTags2TagsPart);
14988 - dev->nDataBytesPerChunk = dev->totalBytesPerChunk;
14989 + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
14991 /* Got the right mix of functions? */
14992 - if (!yaffs_CheckDevFunctions(dev)) {
14993 + if (!yaffs_cehck_dev_fns(dev)) {
14994 /* Function missing */
14995 T(YAFFS_TRACE_ALWAYS,
14997 @@ -7175,13 +5183,13 @@ int yaffs_GutsInitialise(yaffs_Device *d
15000 /* This is really a compilation check. */
15001 - if (!yaffs_CheckStructures()) {
15002 + if (!yaffs_check_structures()) {
15003 T(YAFFS_TRACE_ALWAYS,
15004 - (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
15005 + (TSTR("yaffs_check_structures failed\n" TENDSTR)));
15009 - if (dev->isMounted) {
15010 + if (dev->is_mounted) {
15011 T(YAFFS_TRACE_ALWAYS,
15012 (TSTR("yaffs: device already mounted\n" TENDSTR)));
15014 @@ -7189,59 +5197,62 @@ int yaffs_GutsInitialise(yaffs_Device *d
15016 /* Finished with most checks. One or two more checks happen later on too. */
15018 - dev->isMounted = 1;
15019 + dev->is_mounted = 1;
15021 /* OK now calculate a few things for the device */
15024 * Calculate all the chunk size manipulation numbers:
15026 - x = dev->nDataBytesPerChunk;
15027 - /* We always use dev->chunkShift and dev->chunkDiv */
15028 - dev->chunkShift = Shifts(x);
15029 - x >>= dev->chunkShift;
15030 - dev->chunkDiv = x;
15031 - /* We only use chunk mask if chunkDiv is 1 */
15032 - dev->chunkMask = (1<<dev->chunkShift) - 1;
15033 + x = dev->data_bytes_per_chunk;
15034 + /* We always use dev->chunk_shift and dev->chunk_div */
15035 + dev->chunk_shift = Shifts(x);
15036 + x >>= dev->chunk_shift;
15037 + dev->chunk_div = x;
15038 + /* We only use chunk mask if chunk_div is 1 */
15039 + dev->chunk_mask = (1<<dev->chunk_shift) - 1;
15042 - * Calculate chunkGroupBits.
15043 - * We need to find the next power of 2 > than internalEndBlock
15044 + * Calculate chunk_grp_bits.
15045 + * We need to find the next power of 2 > than internal_end_block
15048 - x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
15049 + x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
15051 bits = ShiftsGE(x);
15053 /* Set up tnode width if wide tnodes are enabled. */
15054 - if (!dev->wideTnodesDisabled) {
15055 + if (!dev->param.wide_tnodes_disabled) {
15056 /* bits must be even so that we end up with 32-bit words */
15060 - dev->tnodeWidth = 16;
15061 + dev->tnode_width = 16;
15063 - dev->tnodeWidth = bits;
15064 + dev->tnode_width = bits;
15066 - dev->tnodeWidth = 16;
15067 + dev->tnode_width = 16;
15069 - dev->tnodeMask = (1<<dev->tnodeWidth)-1;
15070 + dev->tnode_mask = (1<<dev->tnode_width)-1;
15072 /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
15073 * so if the bitwidth of the
15074 * chunk range we're using is greater than 16 we need
15075 - * to figure out chunk shift and chunkGroupSize
15076 + * to figure out chunk shift and chunk_grp_size
15079 - if (bits <= dev->tnodeWidth)
15080 - dev->chunkGroupBits = 0;
15081 + if (bits <= dev->tnode_width)
15082 + dev->chunk_grp_bits = 0;
15084 - dev->chunkGroupBits = bits - dev->tnodeWidth;
15085 + dev->chunk_grp_bits = bits - dev->tnode_width;
15087 + dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0)/8;
15088 + if(dev->tnode_size < sizeof(yaffs_tnode_t))
15089 + dev->tnode_size = sizeof(yaffs_tnode_t);
15091 - dev->chunkGroupSize = 1 << dev->chunkGroupBits;
15092 + dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
15094 - if (dev->nChunksPerBlock < dev->chunkGroupSize) {
15095 + if (dev->param.chunks_per_block < dev->chunk_grp_size) {
15096 /* We have a problem because the soft delete won't work if
15097 * the chunk group size > chunks per block.
15098 * This can be remedied by using larger "virtual blocks".
15099 @@ -7255,85 +5266,89 @@ int yaffs_GutsInitialise(yaffs_Device *d
15100 /* OK, we've finished verifying the device, lets continue with initialisation */
15102 /* More device initialisation */
15103 - dev->garbageCollections = 0;
15104 - dev->passiveGarbageCollections = 0;
15105 - dev->currentDirtyChecker = 0;
15106 - dev->bufferedBlock = -1;
15107 - dev->doingBufferedBlockRewrite = 0;
15108 - dev->nDeletedFiles = 0;
15109 - dev->nBackgroundDeletions = 0;
15110 - dev->nUnlinkedFiles = 0;
15111 - dev->eccFixed = 0;
15112 - dev->eccUnfixed = 0;
15113 - dev->tagsEccFixed = 0;
15114 - dev->tagsEccUnfixed = 0;
15115 - dev->nErasureFailures = 0;
15116 - dev->nErasedBlocks = 0;
15117 - dev->isDoingGC = 0;
15118 - dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
15119 + dev->all_gcs = 0;
15120 + dev->passive_gc_count = 0;
15121 + dev->oldest_dirty_gc_count = 0;
15123 + dev->gc_block_finder = 0;
15124 + dev->buffered_block = -1;
15125 + dev->doing_buffered_block_rewrite = 0;
15126 + dev->n_deleted_files = 0;
15127 + dev->n_bg_deletions = 0;
15128 + dev->n_unlinked_files = 0;
15129 + dev->n_ecc_fixed = 0;
15130 + dev->n_ecc_unfixed = 0;
15131 + dev->n_tags_ecc_fixed = 0;
15132 + dev->n_tags_ecc_unfixed = 0;
15133 + dev->n_erase_failures = 0;
15134 + dev->n_erased_blocks = 0;
15135 + dev->gc_disable= 0;
15136 + dev->has_pending_prioritised_gc = 1; /* Assume the worst for now, will get fixed on first GC */
15137 + YINIT_LIST_HEAD(&dev->dirty_dirs);
15138 + dev->oldest_dirty_seq = 0;
15139 + dev->oldest_dirty_block = 0;
15141 /* Initialise temporary buffers and caches. */
15142 - if (!yaffs_InitialiseTempBuffers(dev))
15143 + if (!yaffs_init_tmp_buffers(dev))
15146 - dev->srCache = NULL;
15147 - dev->gcCleanupList = NULL;
15148 + dev->cache = NULL;
15149 + dev->gc_cleanup_list = NULL;
15152 if (!init_failed &&
15153 - dev->nShortOpCaches > 0) {
15154 + dev->param.n_caches > 0) {
15157 - int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
15158 + int cacheBytes = dev->param.n_caches * sizeof(yaffs_cache_t);
15160 - if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
15161 - dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
15162 + if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
15163 + dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
15165 - dev->srCache = YMALLOC(srCacheBytes);
15166 + dev->cache = YMALLOC(cacheBytes);
15168 - buf = (__u8 *) dev->srCache;
15169 + buf = (__u8 *) dev->cache;
15171 - if (dev->srCache)
15172 - memset(dev->srCache, 0, srCacheBytes);
15174 + memset(dev->cache, 0, cacheBytes);
15176 - for (i = 0; i < dev->nShortOpCaches && buf; i++) {
15177 - dev->srCache[i].object = NULL;
15178 - dev->srCache[i].lastUse = 0;
15179 - dev->srCache[i].dirty = 0;
15180 - dev->srCache[i].data = buf = YMALLOC_DMA(dev->totalBytesPerChunk);
15181 + for (i = 0; i < dev->param.n_caches && buf; i++) {
15182 + dev->cache[i].object = NULL;
15183 + dev->cache[i].last_use = 0;
15184 + dev->cache[i].dirty = 0;
15185 + dev->cache[i].data = buf = YMALLOC_DMA(dev->param.total_bytes_per_chunk);
15190 - dev->srLastUse = 0;
15191 + dev->cache_last_use = 0;
15194 - dev->cacheHits = 0;
15195 + dev->cache_hits = 0;
15197 if (!init_failed) {
15198 - dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
15199 - if (!dev->gcCleanupList)
15200 + dev->gc_cleanup_list = YMALLOC(dev->param.chunks_per_block * sizeof(__u32));
15201 + if (!dev->gc_cleanup_list)
15205 - if (dev->isYaffs2)
15206 - dev->useHeaderFileSize = 1;
15207 + if (dev->param.is_yaffs2)
15208 + dev->param.use_header_file_size = 1;
15210 - if (!init_failed && !yaffs_InitialiseBlocks(dev))
15211 + if (!init_failed && !yaffs_init_blocks(dev))
15214 - yaffs_InitialiseTnodes(dev);
15215 - yaffs_InitialiseObjects(dev);
15216 + yaffs_init_tnodes_and_objs(dev);
15218 - if (!init_failed && !yaffs_CreateInitialDirectories(dev))
15219 + if (!init_failed && !yaffs_create_initial_dir(dev))
15223 if (!init_failed) {
15224 /* Now scan the flash. */
15225 - if (dev->isYaffs2) {
15226 - if (yaffs_CheckpointRestore(dev)) {
15227 - yaffs_CheckObjectDetailsLoaded(dev->rootDir);
15228 + if (dev->param.is_yaffs2) {
15229 + if (yaffs2_checkpt_restore(dev)) {
15230 + yaffs_check_obj_details_loaded(dev->root_dir);
15231 T(YAFFS_TRACE_ALWAYS,
15232 (TSTR("yaffs: restored from checkpoint" TENDSTR)));
15234 @@ -7341,128 +5356,129 @@ int yaffs_GutsInitialise(yaffs_Device *d
15235 /* Clean up the mess caused by an aborted checkpoint load
15236 * and scan backwards.
15238 - yaffs_DeinitialiseBlocks(dev);
15239 - yaffs_DeinitialiseTnodes(dev);
15240 - yaffs_DeinitialiseObjects(dev);
15241 + yaffs_deinit_blocks(dev);
15243 + yaffs_deinit_tnodes_and_objs(dev);
15245 - dev->nErasedBlocks = 0;
15246 - dev->nFreeChunks = 0;
15247 - dev->allocationBlock = -1;
15248 - dev->allocationPage = -1;
15249 - dev->nDeletedFiles = 0;
15250 - dev->nUnlinkedFiles = 0;
15251 - dev->nBackgroundDeletions = 0;
15252 - dev->oldestDirtySequence = 0;
15253 + dev->n_erased_blocks = 0;
15254 + dev->n_free_chunks = 0;
15255 + dev->alloc_block = -1;
15256 + dev->alloc_page = -1;
15257 + dev->n_deleted_files = 0;
15258 + dev->n_unlinked_files = 0;
15259 + dev->n_bg_deletions = 0;
15261 - if (!init_failed && !yaffs_InitialiseBlocks(dev))
15262 + if (!init_failed && !yaffs_init_blocks(dev))
15265 - yaffs_InitialiseTnodes(dev);
15266 - yaffs_InitialiseObjects(dev);
15267 + yaffs_init_tnodes_and_objs(dev);
15269 - if (!init_failed && !yaffs_CreateInitialDirectories(dev))
15270 + if (!init_failed && !yaffs_create_initial_dir(dev))
15273 - if (!init_failed && !yaffs_ScanBackwards(dev))
15274 + if (!init_failed && !yaffs2_scan_backwards(dev))
15277 - } else if (!yaffs_Scan(dev))
15278 + } else if (!yaffs1_scan(dev))
15281 - yaffs_StripDeletedObjects(dev);
15282 + yaffs_strip_deleted_objs(dev);
15283 + yaffs_fix_hanging_objs(dev);
15284 + if(dev->param.empty_lost_n_found)
15285 + yaffs_empty_l_n_f(dev);
15289 /* Clean up the mess */
15290 T(YAFFS_TRACE_TRACING,
15291 - (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
15292 + (TSTR("yaffs: yaffs_guts_initialise() aborted.\n" TENDSTR)));
15294 - yaffs_Deinitialise(dev);
15295 + yaffs_deinitialise(dev);
15299 /* Zero out stats */
15300 - dev->nPageReads = 0;
15301 - dev->nPageWrites = 0;
15302 - dev->nBlockErasures = 0;
15303 - dev->nGCCopies = 0;
15304 - dev->nRetriedWrites = 0;
15306 - dev->nRetiredBlocks = 0;
15308 - yaffs_VerifyFreeChunks(dev);
15309 - yaffs_VerifyBlocks(dev);
15311 + dev->n_page_reads = 0;
15312 + dev->n_page_writes = 0;
15313 + dev->n_erasures = 0;
15314 + dev->n_gc_copies = 0;
15315 + dev->n_retired_writes = 0;
15317 + dev->n_retired_blocks = 0;
15319 + yaffs_verify_free_chunks(dev);
15320 + yaffs_verify_blocks(dev);
15322 + /* Clean up any aborted checkpoint data */
15323 + if(!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
15324 + yaffs2_checkpt_invalidate(dev);
15326 T(YAFFS_TRACE_TRACING,
15327 - (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
15328 + (TSTR("yaffs: yaffs_guts_initialise() done.\n" TENDSTR)));
15333 -void yaffs_Deinitialise(yaffs_Device *dev)
15334 +void yaffs_deinitialise(yaffs_dev_t *dev)
15336 - if (dev->isMounted) {
15337 + if (dev->is_mounted) {
15340 - yaffs_DeinitialiseBlocks(dev);
15341 - yaffs_DeinitialiseTnodes(dev);
15342 - yaffs_DeinitialiseObjects(dev);
15343 - if (dev->nShortOpCaches > 0 &&
15345 + yaffs_deinit_blocks(dev);
15346 + yaffs_deinit_tnodes_and_objs(dev);
15347 + if (dev->param.n_caches > 0 &&
15350 - for (i = 0; i < dev->nShortOpCaches; i++) {
15351 - if (dev->srCache[i].data)
15352 - YFREE(dev->srCache[i].data);
15353 - dev->srCache[i].data = NULL;
15354 + for (i = 0; i < dev->param.n_caches; i++) {
15355 + if (dev->cache[i].data)
15356 + YFREE(dev->cache[i].data);
15357 + dev->cache[i].data = NULL;
15360 - YFREE(dev->srCache);
15361 - dev->srCache = NULL;
15362 + YFREE(dev->cache);
15363 + dev->cache = NULL;
15366 - YFREE(dev->gcCleanupList);
15367 + YFREE(dev->gc_cleanup_list);
15369 for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
15370 - YFREE(dev->tempBuffer[i].buffer);
15371 + YFREE(dev->temp_buffer[i].buffer);
15373 - dev->isMounted = 0;
15374 + dev->is_mounted = 0;
15376 - if (dev->deinitialiseNAND)
15377 - dev->deinitialiseNAND(dev);
15378 + if (dev->param.deinitialise_flash_fn)
15379 + dev->param.deinitialise_flash_fn(dev);
15383 -static int yaffs_CountFreeChunks(yaffs_Device *dev)
15384 +int yaffs_count_free_chunks(yaffs_dev_t *dev)
15390 - yaffs_BlockInfo *blk;
15392 - for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
15394 - blk = yaffs_GetBlockInfo(dev, b);
15395 + yaffs_block_info_t *blk;
15397 - switch (blk->blockState) {
15398 + blk = dev->block_info;
15399 + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
15400 + switch (blk->block_state) {
15401 case YAFFS_BLOCK_STATE_EMPTY:
15402 case YAFFS_BLOCK_STATE_ALLOCATING:
15403 case YAFFS_BLOCK_STATE_COLLECTING:
15404 case YAFFS_BLOCK_STATE_FULL:
15406 - (dev->nChunksPerBlock - blk->pagesInUse +
15407 - blk->softDeletions);
15408 + (dev->param.chunks_per_block - blk->pages_in_use +
15409 + blk->soft_del_pages);
15420 -int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
15421 +int yaffs_get_n_free_chunks(yaffs_dev_t *dev)
15423 /* This is what we report to the outside world */
15425 @@ -7472,30 +5488,28 @@ int yaffs_GetNumberOfFreeChunks(yaffs_De
15429 - nFree = dev->nFreeChunks;
15430 + nFree = dev->n_free_chunks;
15432 - nFree = yaffs_CountFreeChunks(dev);
15433 + nFree = yaffs_count_free_chunks(dev);
15436 - nFree += dev->nDeletedFiles;
15437 + nFree += dev->n_deleted_files;
15439 /* Now count the number of dirty chunks in the cache and subtract those */
15441 - for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
15442 - if (dev->srCache[i].dirty)
15443 + for (nDirtyCacheChunks = 0, i = 0; i < dev->param.n_caches; i++) {
15444 + if (dev->cache[i].dirty)
15445 nDirtyCacheChunks++;
15448 nFree -= nDirtyCacheChunks;
15450 - nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
15451 + nFree -= ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
15453 /* Now we figure out how much to reserve for the checkpoint and report that... */
15454 - blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
15455 - if (blocksForCheckpoint < 0)
15456 - blocksForCheckpoint = 0;
15457 + blocksForCheckpoint = yaffs_calc_checkpt_blocks_required(dev);
15459 - nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
15460 + nFree -= (blocksForCheckpoint * dev->param.chunks_per_block);
15464 @@ -7504,49 +5518,27 @@ int yaffs_GetNumberOfFreeChunks(yaffs_De
15468 -static int yaffs_freeVerificationFailures;
15470 -static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
15475 - if (yaffs_SkipVerification(dev))
15478 - counted = yaffs_CountFreeChunks(dev);
15480 - difference = dev->nFreeChunks - counted;
15482 - if (difference) {
15483 - T(YAFFS_TRACE_ALWAYS,
15484 - (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
15485 - dev->nFreeChunks, counted, difference));
15486 - yaffs_freeVerificationFailures++;
15490 /*---------------------------------------- YAFFS test code ----------------------*/
15492 -#define yaffs_CheckStruct(structure, syze, name) \
15493 +#define yaffs_check_struct(structure, syze, name) \
15495 if (sizeof(structure) != syze) { \
15496 T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
15497 - name, syze, sizeof(structure))); \
15498 + name, syze, (int) sizeof(structure))); \
15499 return YAFFS_FAIL; \
15503 -static int yaffs_CheckStructures(void)
15504 +static int yaffs_check_structures(void)
15506 -/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
15507 -/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
15508 -/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
15509 -#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
15510 - yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode");
15512 +/* yaffs_check_struct(yaffs_tags_t,8,"yaffs_tags_t"); */
15513 +/* yaffs_check_struct(yaffs_tags_union_t,8,"yaffs_tags_union_t"); */
15514 +/* yaffs_check_struct(yaffs_spare,16,"yaffs_spare"); */
15515 +/* yaffs_check_struct(yaffs_tnode_t, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_tnode_t"); */
15517 #ifndef CONFIG_YAFFS_WINCE
15518 - yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");
15519 + yaffs_check_struct(yaffs_obj_header, 512, "yaffs_obj_header");
15523 --- a/fs/yaffs2/yaffs_guts.h
15524 +++ b/fs/yaffs2/yaffs_guts.h
15527 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
15529 - * Copyright (C) 2002-2007 Aleph One Ltd.
15530 + * Copyright (C) 2002-2010 Aleph One Ltd.
15531 * for Toby Churchill Ltd and Brightstar Engineering
15533 * Created by Charles Manning <charles@aleph1.co.uk>
15535 #ifndef __YAFFS_GUTS_H__
15536 #define __YAFFS_GUTS_H__
15538 -#include "devextras.h"
15539 #include "yportenv.h"
15540 +#include "devextras.h"
15541 +#include "yaffs_list.h"
15544 #define YAFFS_FAIL 0
15547 #define YAFFS_MAX_CHUNK_ID 0x000FFFFF
15549 -#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
15551 #define YAFFS_ALLOCATION_NOBJECTS 100
15552 #define YAFFS_ALLOCATION_NTNODES 100
15556 #define YAFFS_OBJECT_SPACE 0x40000
15557 +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE -1)
15559 -#define YAFFS_CHECKPOINT_VERSION 3
15560 +#define YAFFS_CHECKPOINT_VERSION 4
15562 #ifdef CONFIG_YAFFS_UNICODE
15563 #define YAFFS_MAX_NAME_LENGTH 127
15564 @@ -81,12 +82,11 @@
15565 #define YAFFS_OBJECTID_UNLINKED 3
15566 #define YAFFS_OBJECTID_DELETED 4
15568 -/* Sseudo object ids for checkpointing */
15569 +/* Pseudo object ids for checkpointing */
15570 #define YAFFS_OBJECTID_SB_HEADER 0x10
15571 #define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
15572 #define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
15576 #define YAFFS_MAX_SHORT_OP_CACHES 20
15578 @@ -113,18 +113,14 @@
15580 /* ChunkCache is used for short read/write operations.*/
15582 - struct yaffs_ObjectStruct *object;
15585 + struct yaffs_obj_s *object;
15589 - int nBytes; /* Only valid if the cache is dirty */
15590 + int n_bytes; /* Only valid if the cache is dirty */
15591 int locked; /* Can't push out or flush while locked. */
15592 -#ifdef CONFIG_YAFFS_YAFFS2
15595 - __u8 data[YAFFS_BYTES_PER_CHUNK];
15597 -} yaffs_ChunkCache;
15602 @@ -135,18 +131,18 @@ typedef struct {
15604 #ifndef CONFIG_YAFFS_NO_YAFFS1
15606 - unsigned chunkId:20;
15607 - unsigned serialNumber:2;
15608 - unsigned byteCountLSB:10;
15609 - unsigned objectId:18;
15610 + unsigned chunk_id:20;
15611 + unsigned serial_number:2;
15612 + unsigned n_bytes_lsb:10;
15613 + unsigned obj_id:18;
15615 - unsigned byteCountMSB:2;
15617 + unsigned n_bytes_msb:2;
15621 - yaffs_Tags asTags;
15623 -} yaffs_TagsUnion;
15624 + yaffs_tags_t as_tags;
15625 + __u8 as_bytes[8];
15626 +} yaffs_tags_union_t;
15630 @@ -157,7 +153,7 @@ typedef enum {
15631 YAFFS_ECC_RESULT_NO_ERROR,
15632 YAFFS_ECC_RESULT_FIXED,
15633 YAFFS_ECC_RESULT_UNFIXED
15634 -} yaffs_ECCResult;
15635 +} yaffs_ecc_result;
15638 YAFFS_OBJECT_TYPE_UNKNOWN,
15639 @@ -166,64 +162,64 @@ typedef enum {
15640 YAFFS_OBJECT_TYPE_DIRECTORY,
15641 YAFFS_OBJECT_TYPE_HARDLINK,
15642 YAFFS_OBJECT_TYPE_SPECIAL
15643 -} yaffs_ObjectType;
15646 #define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
15650 - unsigned validMarker0;
15651 - unsigned chunkUsed; /* Status of the chunk: used or unused */
15652 - unsigned objectId; /* If 0 then this is not part of an object (unused) */
15653 - unsigned chunkId; /* If 0 then this is a header, else a data chunk */
15654 - unsigned byteCount; /* Only valid for data chunks */
15655 + unsigned validity1;
15656 + unsigned chunk_used; /* Status of the chunk: used or unused */
15657 + unsigned obj_id; /* If 0 then this is not part of an object (unused) */
15658 + unsigned chunk_id; /* If 0 then this is a header, else a data chunk */
15659 + unsigned n_bytes; /* Only valid for data chunks */
15661 /* The following stuff only has meaning when we read */
15662 - yaffs_ECCResult eccResult;
15663 - unsigned blockBad;
15664 + yaffs_ecc_result ecc_result;
15665 + unsigned block_bad;
15667 /* YAFFS 1 stuff */
15668 - unsigned chunkDeleted; /* The chunk is marked deleted */
15669 - unsigned serialNumber; /* Yaffs1 2-bit serial number */
15670 + unsigned is_deleted; /* The chunk is marked deleted */
15671 + unsigned serial_number; /* Yaffs1 2-bit serial number */
15674 - unsigned sequenceNumber; /* The sequence number of this block */
15675 + unsigned seq_number; /* The sequence number of this block */
15677 /* Extra info if this is an object header (YAFFS2 only) */
15679 - unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
15680 - unsigned extraParentObjectId; /* The parent object */
15681 - unsigned extraIsShrinkHeader; /* Is it a shrink header? */
15682 - unsigned extraShadows; /* Does this shadow another object? */
15683 + unsigned extra_available; /* There is extra info available if this is not zero */
15684 + unsigned extra_parent_id; /* The parent object */
15685 + unsigned extra_is_shrink; /* Is it a shrink header? */
15686 + unsigned extra_shadows; /* Does this shadow another object? */
15688 - yaffs_ObjectType extraObjectType; /* What object type? */
15689 + yaffs_obj_type extra_obj_type; /* What object type? */
15691 - unsigned extraFileLength; /* Length if it is a file */
15692 - unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
15693 + unsigned extra_length; /* Length if it is a file */
15694 + unsigned extra_equiv_id; /* Equivalent object Id if it is a hard link */
15696 - unsigned validMarker1;
15697 + unsigned validty1;
15699 -} yaffs_ExtendedTags;
15702 /* Spare structure for YAFFS1 */
15708 - __u8 pageStatus; /* set to 0 to delete the chunk */
15709 - __u8 blockStatus;
15716 + __u8 page_status; /* set to 0 to delete the chunk */
15717 + __u8 block_status;
15729 /*Special structure for passing through to mtd */
15730 -struct yaffs_NANDSpare {
15731 - yaffs_Spare spare;
15732 +struct yaffs_nand_spare {
15733 + yaffs_spare spare;
15737 @@ -234,6 +230,8 @@ typedef enum {
15738 YAFFS_BLOCK_STATE_UNKNOWN = 0,
15740 YAFFS_BLOCK_STATE_SCANNING,
15741 + /* Being scanned */
15743 YAFFS_BLOCK_STATE_NEEDS_SCANNING,
15744 /* The block might have something on it (ie it is allocating or full, perhaps empty)
15745 * but it needs to be scanned to determine its true state.
15746 @@ -249,67 +247,69 @@ typedef enum {
15747 /* This block is partially allocated.
15748 * At least one page holds valid data.
15749 * This is the one currently being used for page
15750 - * allocation. Should never be more than one of these
15751 + * allocation. Should never be more than one of these.
15752 + * If a block is only partially allocated at mount it is treated as full.
15755 YAFFS_BLOCK_STATE_FULL,
15756 /* All the pages in this block have been allocated.
15757 + * If a block was only partially allocated when mounted we treat
15758 + * it as fully allocated.
15761 YAFFS_BLOCK_STATE_DIRTY,
15762 - /* All pages have been allocated and deleted.
15763 + /* The block was full and now all chunks have been deleted.
15764 * Erase me, reuse me.
15767 YAFFS_BLOCK_STATE_CHECKPOINT,
15768 - /* This block is assigned to holding checkpoint data.
15770 + /* This block is assigned to holding checkpoint data. */
15772 YAFFS_BLOCK_STATE_COLLECTING,
15773 /* This block is being garbage collected */
15775 YAFFS_BLOCK_STATE_DEAD
15776 /* This block has failed and is not in use */
15777 -} yaffs_BlockState;
15778 +} yaffs_block_state_t;
15780 #define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
15785 - int softDeletions:10; /* number of soft deleted pages */
15786 - int pagesInUse:10; /* number of pages in use */
15787 - unsigned blockState:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
15788 - __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
15789 + int soft_del_pages:10; /* number of soft deleted pages */
15790 + int pages_in_use:10; /* number of pages in use */
15791 + unsigned block_state:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
15792 + __u32 needs_retiring:1; /* Data has failed on this block, need to get valid data off */
15793 /* and retire the block. */
15794 - __u32 skipErasedCheck:1; /* If this is set we can skip the erased check on this block */
15795 - __u32 gcPrioritise:1; /* An ECC check or blank check has failed on this block.
15796 + __u32 skip_erased_check:1; /* If this is set we can skip the erased check on this block */
15797 + __u32 gc_prioritise:1; /* An ECC check or blank check has failed on this block.
15798 It should be prioritised for GC */
15799 - __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
15800 + __u32 chunk_error_strikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
15802 #ifdef CONFIG_YAFFS_YAFFS2
15803 - __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
15804 - __u32 sequenceNumber; /* block sequence number for yaffs2 */
15805 + __u32 has_shrink_hdr:1; /* This block has at least one shrink object header */
15806 + __u32 seq_number; /* block sequence number for yaffs2 */
15809 -} yaffs_BlockInfo;
15810 +} yaffs_block_info_t;
15812 /* -------------------------- Object structure -------------------------------*/
15813 /* This is the object structure as stored on NAND */
15816 - yaffs_ObjectType type;
15817 + yaffs_obj_type type;
15819 /* Apply to everything */
15820 - int parentObjectId;
15821 - __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
15822 + int parent_obj_id;
15823 + __u16 sum_no_longer_used; /* checksum of name. No longer used */
15824 YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
15826 /* The following apply to directories, files, symlinks - not hard links */
15827 __u32 yst_mode; /* protection */
15829 #ifdef CONFIG_YAFFS_WINCE
15830 - __u32 notForWinCE[5];
15831 + __u32 not_for_wince[5];
15835 @@ -319,10 +319,10 @@ typedef struct {
15838 /* File size applies to files only */
15842 /* Equivalent object id applies to hard links only. */
15843 - int equivalentObjectId;
15846 /* Alias is for symlinks only. */
15847 YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
15848 @@ -334,40 +334,29 @@ typedef struct {
15849 __u32 win_atime[2];
15850 __u32 win_mtime[2];
15852 - __u32 roomToGrow[6];
15853 + __u32 room_to_grow[6];
15856 - __u32 inbandShadowsObject;
15857 - __u32 inbandIsShrink;
15858 + __u32 inband_shadowed_obj_id;
15859 + __u32 inband_is_shrink;
15861 - __u32 reservedSpace[2];
15862 - int shadowsObject; /* This object header shadows the specified object if > 0 */
15863 + __u32 reserved[2];
15864 + int shadows_obj; /* This object header shadows the specified object if > 0 */
15866 - /* isShrink applies to object headers written when we shrink the file (ie resize) */
15868 + /* is_shrink applies to object headers written when we shrink the file (ie resize) */
15871 -} yaffs_ObjectHeader;
15872 +} yaffs_obj_header;
15874 /*--------------------------- Tnode -------------------------- */
15876 -union yaffs_Tnode_union {
15877 -#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
15878 - union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
15880 - union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
15882 -/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
15883 +union yaffs_tnode_union {
15884 + union yaffs_tnode_union *internal[YAFFS_NTNODES_INTERNAL];
15888 -typedef union yaffs_Tnode_union yaffs_Tnode;
15889 +typedef union yaffs_tnode_union yaffs_tnode_t;
15891 -struct yaffs_TnodeList_struct {
15892 - struct yaffs_TnodeList_struct *next;
15893 - yaffs_Tnode *tnodes;
15896 -typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
15898 /*------------------------ Object -----------------------------*/
15899 /* An object can be one of:
15900 @@ -378,82 +367,85 @@ typedef struct yaffs_TnodeList_struct ya
15905 - __u32 scannedFileSize;
15906 - __u32 shrinkSize;
15908 - yaffs_Tnode *top;
15909 -} yaffs_FileStructure;
15911 + __u32 scanned_size;
15912 + __u32 shrink_size;
15914 + yaffs_tnode_t *top;
15918 struct ylist_head children; /* list of child links */
15919 -} yaffs_DirectoryStructure;
15920 + struct ylist_head dirty; /* Entry for list of dirty directories */
15925 -} yaffs_SymLinkStructure;
15926 +} yaffs_symlink_t;
15929 - struct yaffs_ObjectStruct *equivalentObject;
15930 - __u32 equivalentObjectId;
15931 -} yaffs_HardLinkStructure;
15932 + struct yaffs_obj_s *equiv_obj;
15934 +} yaffs_hard_link_s;
15937 - yaffs_FileStructure fileVariant;
15938 - yaffs_DirectoryStructure directoryVariant;
15939 - yaffs_SymLinkStructure symLinkVariant;
15940 - yaffs_HardLinkStructure hardLinkVariant;
15941 -} yaffs_ObjectVariant;
15942 + yaffs_file_s file_variant;
15943 + yaffs_dir_s dir_variant;
15944 + yaffs_symlink_t symlink_variant;
15945 + yaffs_hard_link_s hardlink_variant;
15946 +} yaffs_obj_variant;
15950 -struct yaffs_ObjectStruct {
15951 +struct yaffs_obj_s {
15952 __u8 deleted:1; /* This should only apply to unlinked files. */
15953 - __u8 softDeleted:1; /* it has also been soft deleted */
15954 + __u8 soft_del:1; /* it has also been soft deleted */
15955 __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
15956 __u8 fake:1; /* A fake object has no presence on NAND. */
15957 - __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
15958 - __u8 unlinkAllowed:1;
15959 + __u8 rename_allowed:1; /* Some objects are not allowed to be renamed. */
15960 + __u8 unlink_allowed:1;
15961 __u8 dirty:1; /* the object needs to be written to flash */
15962 __u8 valid:1; /* When the file system is being loaded up, this
15963 * object might be created before the data
15964 * is available (ie. file data records appear before the header).
15966 - __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
15967 + __u8 lazy_loaded:1; /* This object has been lazy loaded and is missing some detail */
15969 - __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
15970 + __u8 defered_free:1; /* For Linux kernel. Object is removed from NAND, but is
15971 * still in the inode cache. Free of object is defered.
15972 * until the inode is released.
15974 - __u8 beingCreated:1; /* This object is still being created so skip some checks. */
15975 + __u8 being_created:1; /* This object is still being created so skip some checks. */
15976 + __u8 is_shadowed:1; /* This object is shadowed on the way to being renamed. */
15978 + __u8 xattr_known:1; /* We know if this has object has xattribs or not. */
15979 + __u8 has_xattr:1; /* This object has xattribs. Valid if xattr_known. */
15981 __u8 serial; /* serial number of chunk in NAND. Cached here */
15982 __u16 sum; /* sum of the name to speed searching */
15984 - struct yaffs_DeviceStruct *myDev; /* The device I'm on */
15985 + struct yaffs_dev_s *my_dev; /* The device I'm on */
15987 - struct ylist_head hashLink; /* list of objects in this hash bucket */
15988 + struct ylist_head hash_link; /* list of objects in this hash bucket */
15990 - struct ylist_head hardLinks; /* all the equivalent hard linked objects */
15991 + struct ylist_head hard_links; /* all the equivalent hard linked objects */
15993 /* directory structure stuff */
15994 /* also used for linking up the free list */
15995 - struct yaffs_ObjectStruct *parent;
15996 + struct yaffs_obj_s *parent;
15997 struct ylist_head siblings;
15999 /* Where's my object header in NAND? */
16003 - int nDataChunks; /* Number of data chunks attached to the file. */
16004 + int n_data_chunks; /* Number of data chunks attached to the file. */
16006 - __u32 objectId; /* the object id value */
16007 + __u32 obj_id; /* the object id value */
16011 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
16012 - YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
16015 -#ifndef __KERNEL__
16017 + YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
16020 #ifdef CONFIG_YAFFS_WINCE
16021 @@ -470,53 +462,43 @@ struct yaffs_ObjectStruct {
16026 - struct inode *myInode;
16030 + yaffs_obj_type variant_type;
16032 - yaffs_ObjectType variantType;
16033 + yaffs_obj_variant variant;
16035 - yaffs_ObjectVariant variant;
16039 -typedef struct yaffs_ObjectStruct yaffs_Object;
16041 -struct yaffs_ObjectList_struct {
16042 - yaffs_Object *objects;
16043 - struct yaffs_ObjectList_struct *next;
16046 -typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
16047 +typedef struct yaffs_obj_s yaffs_obj_t;
16050 struct ylist_head list;
16052 -} yaffs_ObjectBucket;
16053 +} yaffs_obj_bucket;
16056 -/* yaffs_CheckpointObject holds the definition of an object as dumped
16057 +/* yaffs_checkpt_obj_t holds the definition of an object as dumped
16058 * by checkpointing.
16066 - yaffs_ObjectType variantType:3;
16071 + yaffs_obj_type variant_type:3;
16073 - __u8 softDeleted:1;
16077 - __u8 renameAllowed:1;
16078 - __u8 unlinkAllowed:1;
16079 + __u8 rename_allowed:1;
16080 + __u8 unlink_allowed:1;
16084 - __u32 fileSizeOrEquivalentObjectId;
16085 -} yaffs_CheckpointObject;
16086 + int n_data_chunks;
16087 + __u32 size_or_equiv_obj;
16088 +} yaffs_checkpt_obj_t;
16090 /*--------------------- Temporary buffers ----------------
16092 @@ -526,379 +508,462 @@ typedef struct {
16095 int line; /* track from whence this buffer was allocated */
16097 -} yaffs_TempBuffer;
16101 /*----------------- Device ---------------------------------*/
16103 -struct yaffs_DeviceStruct {
16104 - struct ylist_head devList;
16105 - const char *name;
16107 - /* Entry parameters set up way early. Yaffs sets up the rest.*/
16108 - int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
16109 - int nChunksPerBlock; /* does not need to be a power of 2 */
16110 - int spareBytesPerChunk; /* spare area size */
16111 - int startBlock; /* Start block we're allowed to use */
16112 - int endBlock; /* End block we're allowed to use */
16113 - int nReservedBlocks; /* We want this tuneable so that we can reduce */
16114 - /* reserved blocks on NOR and RAM. */
16117 - /* Stuff used by the shared space checkpointing mechanism */
16118 - /* If this value is zero, then this mechanism is disabled */
16119 +struct yaffs_param_s {
16120 + const YCHAR *name;
16122 -/* int nCheckpointReservedBlocks; */ /* Blocks to reserve for checkpoint data */
16124 + * Entry parameters set up way early. Yaffs sets up the rest.
16125 + * The structure should be zeroed out before use so that unused
16126 + * and defualt values are zero.
16129 + int inband_tags; /* Use unband tags */
16130 + __u32 total_bytes_per_chunk; /* Should be >= 512, does not need to be a power of 2 */
16131 + int chunks_per_block; /* does not need to be a power of 2 */
16132 + int spare_bytes_per_chunk; /* spare area size */
16133 + int start_block; /* Start block we're allowed to use */
16134 + int end_block; /* End block we're allowed to use */
16135 + int n_reserved_blocks; /* We want this tuneable so that we can reduce */
16136 + /* reserved blocks on NOR and RAM. */
16139 - int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
16140 - * the number of short op caches (don't use too many)
16141 + int n_caches; /* If <= 0, then short op caching is disabled, else
16142 + * the number of short op caches (don't use too many).
16143 + * 10 to 20 is a good bet.
16145 + int use_nand_ecc; /* Flag to decide whether or not to use NANDECC on data (yaffs1) */
16146 + int no_tags_ecc; /* Flag to decide whether or not to do ECC on packed tags (yaffs2) */
16148 - int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
16149 + int is_yaffs2; /* Use yaffs2 mode on this device */
16151 - int useNANDECC; /* Flag to decide whether or not to use NANDECC */
16152 + int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
16154 - void *genericDevice; /* Pointer to device context
16155 - * On an mtd this holds the mtd pointer.
16157 - void *superBlock;
16158 + int refresh_period; /* How often we should check to do a block refresh */
16160 + /* Checkpoint control. Can be set before or after initialisation */
16161 + __u8 skip_checkpt_rd;
16162 + __u8 skip_checkpt_wr;
16164 + int enable_xattr; /* Enable xattribs */
16166 /* NAND access functions (Must be set before calling YAFFS)*/
16168 - int (*writeChunkToNAND) (struct yaffs_DeviceStruct *dev,
16169 - int chunkInNAND, const __u8 *data,
16170 - const yaffs_Spare *spare);
16171 - int (*readChunkFromNAND) (struct yaffs_DeviceStruct *dev,
16172 - int chunkInNAND, __u8 *data,
16173 - yaffs_Spare *spare);
16174 - int (*eraseBlockInNAND) (struct yaffs_DeviceStruct *dev,
16175 - int blockInNAND);
16176 - int (*initialiseNAND) (struct yaffs_DeviceStruct *dev);
16177 - int (*deinitialiseNAND) (struct yaffs_DeviceStruct *dev);
16178 + int (*write_chunk_fn) (struct yaffs_dev_s *dev,
16179 + int nand_chunk, const __u8 *data,
16180 + const yaffs_spare *spare);
16181 + int (*read_chunk_fn) (struct yaffs_dev_s *dev,
16182 + int nand_chunk, __u8 *data,
16183 + yaffs_spare *spare);
16184 + int (*erase_fn) (struct yaffs_dev_s *dev,
16185 + int flash_block);
16186 + int (*initialise_flash_fn) (struct yaffs_dev_s *dev);
16187 + int (*deinitialise_flash_fn) (struct yaffs_dev_s *dev);
16189 #ifdef CONFIG_YAFFS_YAFFS2
16190 - int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct *dev,
16191 - int chunkInNAND, const __u8 *data,
16192 - const yaffs_ExtendedTags *tags);
16193 - int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct *dev,
16194 - int chunkInNAND, __u8 *data,
16195 - yaffs_ExtendedTags *tags);
16196 - int (*markNANDBlockBad) (struct yaffs_DeviceStruct *dev, int blockNo);
16197 - int (*queryNANDBlock) (struct yaffs_DeviceStruct *dev, int blockNo,
16198 - yaffs_BlockState *state, __u32 *sequenceNumber);
16203 - /* The removeObjectCallback function must be supplied by OS flavours that
16204 - * need it. The Linux kernel does not use this, but yaffs direct does use
16205 - * it to implement the faster readdir
16206 + int (*write_chunk_tags_fn) (struct yaffs_dev_s *dev,
16207 + int nand_chunk, const __u8 *data,
16208 + const yaffs_ext_tags *tags);
16209 + int (*read_chunk_tags_fn) (struct yaffs_dev_s *dev,
16210 + int nand_chunk, __u8 *data,
16211 + yaffs_ext_tags *tags);
16212 + int (*bad_block_fn) (struct yaffs_dev_s *dev, int block_no);
16213 + int (*query_block_fn) (struct yaffs_dev_s *dev, int block_no,
16214 + yaffs_block_state_t *state, __u32 *seq_number);
16217 + /* The remove_obj_fn function must be supplied by OS flavours that
16219 + * yaffs direct uses it to implement the faster readdir.
16220 + * Linux uses it to protect the directory during unlocking.
16222 - void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
16223 + void (*remove_obj_fn)(struct yaffs_obj_s *obj);
16225 - /* Callback to mark the superblock dirsty */
16226 - void (*markSuperBlockDirty)(void *superblock);
16227 + /* Callback to mark the superblock dirty */
16228 + void (*sb_dirty_fn)(struct yaffs_dev_s *dev);
16230 + /* Callback to control garbage collection. */
16231 + unsigned (*gc_control)(struct yaffs_dev_s *dev);
16233 + /* Debug control flags. Don't use unless you know what you're doing */
16234 + int use_header_file_size; /* Flag to determine if we should use file sizes from the header */
16235 + int disable_lazy_load; /* Disable lazy loading on this device */
16236 + int wide_tnodes_disabled; /* Set to disable wide tnodes */
16237 + int disable_soft_del; /* yaffs 1 only: Set to disable the use of softdeletion. */
16239 + int defered_dir_update; /* Set to defer directory updates */
16241 - int wideTnodesDisabled; /* Set to disable wide tnodes */
16242 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
16243 + int auto_unicode;
16245 + int always_check_erased; /* Force chunk erased check always on */
16248 - YCHAR *pathDividers; /* String of legal path dividers */
16249 +typedef struct yaffs_param_s yaffs_param_t;
16251 +struct yaffs_dev_s {
16252 + struct yaffs_param_s param;
16254 - /* End of stuff that must be set before initialisation. */
16255 + /* Context storage. Holds extra OS specific data for this device */
16257 - /* Checkpoint control. Can be set before or after initialisation */
16258 - __u8 skipCheckpointRead;
16259 - __u8 skipCheckpointWrite;
16260 + void *os_context;
16261 + void *driver_context;
16263 + struct ylist_head dev_list;
16265 /* Runtime parameters. Set up by YAFFS. */
16266 + int data_bytes_per_chunk;
16268 - __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
16269 - __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
16270 + /* Non-wide tnode stuff */
16271 + __u16 chunk_grp_bits; /* Number of bits that need to be resolved if
16272 + * the tnodes are not wide enough.
16274 + __u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
16276 /* Stuff to support wide tnodes */
16277 - __u32 tnodeWidth;
16279 + __u32 tnode_width;
16280 + __u32 tnode_mask;
16281 + __u32 tnode_size;
16283 /* Stuff for figuring out file offset to chunk conversions */
16284 - __u32 chunkShift; /* Shift value */
16285 - __u32 chunkDiv; /* Divisor after shifting: 1 for power-of-2 sizes */
16286 - __u32 chunkMask; /* Mask to use for power-of-2 case */
16288 - /* Stuff to handle inband tags */
16290 - __u32 totalBytesPerChunk;
16294 - struct semaphore sem; /* Semaphore for waiting on erasure.*/
16295 - struct semaphore grossLock; /* Gross locking semaphore */
16296 - __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
16297 - * at compile time so we have to allocate it.
16299 - void (*putSuperFunc) (struct super_block *sb);
16301 + __u32 chunk_shift; /* Shift value */
16302 + __u32 chunk_div; /* Divisor after shifting: 1 for power-of-2 sizes */
16303 + __u32 chunk_mask; /* Mask to use for power-of-2 case */
16307 - int isCheckpointed;
16311 + int is_checkpointed;
16314 /* Stuff to support block offsetting to support start block zero */
16315 - int internalStartBlock;
16316 - int internalEndBlock;
16319 + int internal_start_block;
16320 + int internal_end_block;
16321 + int block_offset;
16322 + int chunk_offset;
16325 /* Runtime checkpointing stuff */
16326 - int checkpointPageSequence; /* running sequence number of checkpoint pages */
16327 - int checkpointByteCount;
16328 - int checkpointByteOffset;
16329 - __u8 *checkpointBuffer;
16330 - int checkpointOpenForWrite;
16331 - int blocksInCheckpoint;
16332 - int checkpointCurrentChunk;
16333 - int checkpointCurrentBlock;
16334 - int checkpointNextBlock;
16335 - int *checkpointBlockList;
16336 - int checkpointMaxBlocks;
16337 - __u32 checkpointSum;
16338 - __u32 checkpointXor;
16339 + int checkpt_page_seq; /* running sequence number of checkpoint pages */
16340 + int checkpt_byte_count;
16341 + int checkpt_byte_offs;
16342 + __u8 *checkpt_buffer;
16343 + int checkpt_open_write;
16344 + int blocks_in_checkpt;
16345 + int checkpt_cur_chunk;
16346 + int checkpt_cur_block;
16347 + int checkpt_next_block;
16348 + int *checkpt_block_list;
16349 + int checkpt_max_blocks;
16350 + __u32 checkpt_sum;
16351 + __u32 checkpt_xor;
16353 - int nCheckpointBlocksRequired; /* Number of blocks needed to store current checkpoint set */
16354 + int checkpoint_blocks_required; /* Number of blocks needed to store current checkpoint set */
16357 - yaffs_BlockInfo *blockInfo;
16358 - __u8 *chunkBits; /* bitmap of chunks in use */
16359 - unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
16360 - unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
16361 - int chunkBitmapStride; /* Number of bytes of chunkBits per block.
16362 - * Must be consistent with nChunksPerBlock.
16363 + yaffs_block_info_t *block_info;
16364 + __u8 *chunk_bits; /* bitmap of chunks in use */
16365 + unsigned block_info_alt:1; /* was allocated using alternative strategy */
16366 + unsigned chunk_bits_alt:1; /* was allocated using alternative strategy */
16367 + int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
16368 + * Must be consistent with chunks_per_block.
16371 - int nErasedBlocks;
16372 - int allocationBlock; /* Current block being allocated off */
16373 - __u32 allocationPage;
16374 - int allocationBlockFinder; /* Used to search for next allocation block */
16376 - /* Runtime state */
16377 - int nTnodesCreated;
16378 - yaffs_Tnode *freeTnodes;
16380 - yaffs_TnodeList *allocatedTnodeList;
16386 - int nObjectsCreated;
16387 - yaffs_Object *freeObjects;
16388 - int nFreeObjects;
16392 - yaffs_ObjectList *allocatedObjectList;
16394 - yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
16398 - int currentDirtyChecker; /* Used to find current dirtiest block */
16400 - __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
16401 - int nonAggressiveSkip; /* GC state/mode */
16406 - int nBlockErasures;
16407 - int nErasureFailures;
16409 - int garbageCollections;
16410 - int passiveGarbageCollections;
16411 - int nRetriedWrites;
16412 - int nRetiredBlocks;
16415 - int tagsEccFixed;
16416 - int tagsEccUnfixed;
16418 - int nUnmarkedDeletions;
16420 - int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
16421 + int n_erased_blocks;
16422 + int alloc_block; /* Current block being allocated off */
16423 + __u32 alloc_page;
16424 + int alloc_block_finder; /* Used to search for next allocation block */
16426 + /* Object and Tnode memory management */
16433 + yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
16434 + __u32 bucket_finder;
16436 + int n_free_chunks;
16438 + /* Garbage collection control */
16439 + __u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
16440 + __u32 n_clean_ups;
16442 + unsigned has_pending_prioritised_gc; /* We think this device might have pending prioritised gcs */
16443 + unsigned gc_disable;
16444 + unsigned gc_block_finder;
16445 + unsigned gc_dirtiest;
16446 + unsigned gc_pages_in_use;
16447 + unsigned gc_not_done;
16448 + unsigned gc_block;
16449 + unsigned gc_chunk;
16450 + unsigned gc_skip;
16452 /* Special directories */
16453 - yaffs_Object *rootDir;
16454 - yaffs_Object *lostNFoundDir;
16455 + yaffs_obj_t *root_dir;
16456 + yaffs_obj_t *lost_n_found;
16458 /* Buffer areas for storing data to recover from write failures TODO
16459 - * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
16460 - * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
16461 + * __u8 buffered_data[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
16462 + * yaffs_spare buffered_spare[YAFFS_CHUNKS_PER_BLOCK];
16465 - int bufferedBlock; /* Which block is buffered here? */
16466 - int doingBufferedBlockRewrite;
16468 - yaffs_ChunkCache *srCache;
16470 + int buffered_block; /* Which block is buffered here? */
16471 + int doing_buffered_block_rewrite;
16474 + yaffs_cache_t *cache;
16475 + int cache_last_use;
16477 /* Stuff for background deletion and unlinked files.*/
16478 - yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
16479 - yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
16480 - yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
16481 - int nDeletedFiles; /* Count of files awaiting deletion;*/
16482 - int nUnlinkedFiles; /* Count of unlinked files. */
16483 - int nBackgroundDeletions; /* Count of background deletions. */
16485 + yaffs_obj_t *unlinked_dir; /* Directory where unlinked and deleted files live. */
16486 + yaffs_obj_t *del_dir; /* Directory where deleted objects are sent to disappear. */
16487 + yaffs_obj_t *unlinked_deletion; /* Current file being background deleted.*/
16488 + int n_deleted_files; /* Count of files awaiting deletion;*/
16489 + int n_unlinked_files; /* Count of unlinked files. */
16490 + int n_bg_deletions; /* Count of background deletions. */
16492 /* Temporary buffer management */
16493 - yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
16496 - int unmanagedTempAllocations;
16497 - int unmanagedTempDeallocations;
16498 + yaffs_buffer_t temp_buffer[YAFFS_N_TEMP_BUFFERS];
16501 + int unmanaged_buffer_allocs;
16502 + int unmanaged_buffer_deallocs;
16504 /* yaffs2 runtime stuff */
16505 - unsigned sequenceNumber; /* Sequence number of currently allocating block */
16506 - unsigned oldestDirtySequence;
16507 + unsigned seq_number; /* Sequence number of currently allocating block */
16508 + unsigned oldest_dirty_seq;
16509 + unsigned oldest_dirty_block;
16511 + /* Block refreshing */
16512 + int refresh_skip; /* A skip down counter. Refresh happens when this gets to zero. */
16514 + /* Dirty directory handling */
16515 + struct ylist_head dirty_dirs; /* List of dirty directories */
16519 + __u32 n_page_writes;
16520 + __u32 n_page_reads;
16521 + __u32 n_erasures;
16522 + __u32 n_erase_failures;
16523 + __u32 n_gc_copies;
16525 + __u32 passive_gc_count;
16526 + __u32 oldest_dirty_gc_count;
16527 + __u32 n_gc_blocks;
16529 + __u32 n_retired_writes;
16530 + __u32 n_retired_blocks;
16531 + __u32 n_ecc_fixed;
16532 + __u32 n_ecc_unfixed;
16533 + __u32 n_tags_ecc_fixed;
16534 + __u32 n_tags_ecc_unfixed;
16535 + __u32 n_deletions;
16536 + __u32 n_unmarked_deletions;
16537 + __u32 refresh_count;
16538 + __u32 cache_hits;
16542 -typedef struct yaffs_DeviceStruct yaffs_Device;
16543 +typedef struct yaffs_dev_s yaffs_dev_t;
16545 /* The static layout of block usage etc is stored in the super block header */
16549 - int checkpointStartBlock;
16550 - int checkpointEndBlock;
16553 + int checkpt_start_block;
16554 + int checkpt_end_block;
16558 -} yaffs_SuperBlockHeader;
16559 +} yaffs_sb_header;
16561 /* The CheckpointDevice structure holds the device information that changes at runtime and
16562 * must be preserved over unmount/mount cycles.
16566 - int nErasedBlocks;
16567 - int allocationBlock; /* Current block being allocated off */
16568 - __u32 allocationPage;
16571 - int nDeletedFiles; /* Count of files awaiting deletion;*/
16572 - int nUnlinkedFiles; /* Count of unlinked files. */
16573 - int nBackgroundDeletions; /* Count of background deletions. */
16575 + int n_erased_blocks;
16576 + int alloc_block; /* Current block being allocated off */
16577 + __u32 alloc_page;
16578 + int n_free_chunks;
16580 + int n_deleted_files; /* Count of files awaiting deletion;*/
16581 + int n_unlinked_files; /* Count of unlinked files. */
16582 + int n_bg_deletions; /* Count of background deletions. */
16584 /* yaffs2 runtime stuff */
16585 - unsigned sequenceNumber; /* Sequence number of currently allocating block */
16586 - unsigned oldestDirtySequence;
16587 + unsigned seq_number; /* Sequence number of currently allocating block */
16589 -} yaffs_CheckpointDevice;
16590 +} yaffs_checkpt_dev_t;
16599 -} yaffs_CheckpointValidity;
16600 +} yaffs_checkpt_validty_t;
16603 +struct yaffs_shadow_fixer_s {
16606 + struct yaffs_shadow_fixer_s *next;
16609 +/* Structure for doing xattr modifications */
16611 + int set; /* If 0 then this is a deletion */
16612 + const YCHAR *name;
16613 + const void *data;
16620 /*----------------------- YAFFS Functions -----------------------*/
16622 -int yaffs_GutsInitialise(yaffs_Device *dev);
16623 -void yaffs_Deinitialise(yaffs_Device *dev);
16624 +int yaffs_guts_initialise(yaffs_dev_t *dev);
16625 +void yaffs_deinitialise(yaffs_dev_t *dev);
16627 -int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
16628 +int yaffs_get_n_free_chunks(yaffs_dev_t *dev);
16630 -int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
16631 - yaffs_Object *newDir, const YCHAR *newName);
16632 +int yaffs_rename_obj(yaffs_obj_t *old_dir, const YCHAR *old_name,
16633 + yaffs_obj_t *new_dir, const YCHAR *new_name);
16635 -int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name);
16636 -int yaffs_DeleteObject(yaffs_Object *obj);
16637 +int yaffs_unlinker(yaffs_obj_t *dir, const YCHAR *name);
16638 +int yaffs_del_obj(yaffs_obj_t *obj);
16640 -int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize);
16641 -int yaffs_GetObjectFileLength(yaffs_Object *obj);
16642 -int yaffs_GetObjectInode(yaffs_Object *obj);
16643 -unsigned yaffs_GetObjectType(yaffs_Object *obj);
16644 -int yaffs_GetObjectLinkCount(yaffs_Object *obj);
16645 +int yaffs_get_obj_name(yaffs_obj_t *obj, YCHAR *name, int buffer_size);
16646 +int yaffs_get_obj_length(yaffs_obj_t *obj);
16647 +int yaffs_get_obj_inode(yaffs_obj_t *obj);
16648 +unsigned yaffs_get_obj_type(yaffs_obj_t *obj);
16649 +int yaffs_get_obj_link_count(yaffs_obj_t *obj);
16651 -int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
16652 -int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
16653 +int yaffs_set_attribs(yaffs_obj_t *obj, struct iattr *attr);
16654 +int yaffs_get_attribs(yaffs_obj_t *obj, struct iattr *attr);
16656 /* File operations */
16657 -int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, loff_t offset,
16659 -int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, loff_t offset,
16660 - int nBytes, int writeThrough);
16661 -int yaffs_ResizeFile(yaffs_Object *obj, loff_t newSize);
16662 +int yaffs_file_rd(yaffs_obj_t *obj, __u8 *buffer, loff_t offset,
16664 +int yaffs_wr_file(yaffs_obj_t *obj, const __u8 *buffer, loff_t offset,
16665 + int n_bytes, int write_trhrough);
16666 +int yaffs_resize_file(yaffs_obj_t *obj, loff_t new_size);
16668 -yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
16669 +yaffs_obj_t *yaffs_create_file(yaffs_obj_t *parent, const YCHAR *name,
16670 __u32 mode, __u32 uid, __u32 gid);
16671 -int yaffs_FlushFile(yaffs_Object *obj, int updateTime);
16673 +int yaffs_flush_file(yaffs_obj_t *obj, int update_time, int data_sync);
16675 /* Flushing and checkpointing */
16676 -void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
16677 +void yaffs_flush_whole_cache(yaffs_dev_t *dev);
16679 -int yaffs_CheckpointSave(yaffs_Device *dev);
16680 -int yaffs_CheckpointRestore(yaffs_Device *dev);
16681 +int yaffs_checkpoint_save(yaffs_dev_t *dev);
16682 +int yaffs_checkpoint_restore(yaffs_dev_t *dev);
16684 /* Directory operations */
16685 -yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
16686 +yaffs_obj_t *yaffs_create_dir(yaffs_obj_t *parent, const YCHAR *name,
16687 __u32 mode, __u32 uid, __u32 gid);
16688 -yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir, const YCHAR *name);
16689 -int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
16690 - int (*fn) (yaffs_Object *));
16691 +yaffs_obj_t *yaffs_find_by_name(yaffs_obj_t *the_dir, const YCHAR *name);
16692 +int yaffs_ApplyToDirectoryChildren(yaffs_obj_t *the_dir,
16693 + int (*fn) (yaffs_obj_t *));
16695 -yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number);
16696 +yaffs_obj_t *yaffs_find_by_number(yaffs_dev_t *dev, __u32 number);
16698 /* Link operations */
16699 -yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
16700 - yaffs_Object *equivalentObject);
16701 +yaffs_obj_t *yaffs_link_obj(yaffs_obj_t *parent, const YCHAR *name,
16702 + yaffs_obj_t *equiv_obj);
16704 -yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
16705 +yaffs_obj_t *yaffs_get_equivalent_obj(yaffs_obj_t *obj);
16707 /* Symlink operations */
16708 -yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
16709 +yaffs_obj_t *yaffs_create_symlink(yaffs_obj_t *parent, const YCHAR *name,
16710 __u32 mode, __u32 uid, __u32 gid,
16711 const YCHAR *alias);
16712 -YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj);
16713 +YCHAR *yaffs_get_symlink_alias(yaffs_obj_t *obj);
16715 /* Special inodes (fifos, sockets and devices) */
16716 -yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
16717 +yaffs_obj_t *yaffs_create_special(yaffs_obj_t *parent, const YCHAR *name,
16718 __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
16721 +int yaffs_set_xattrib(yaffs_obj_t *obj, const YCHAR *name, const void * value, int size, int flags);
16722 +int yaffs_get_xattrib(yaffs_obj_t *obj, const YCHAR *name, void *value, int size);
16723 +int yaffs_list_xattrib(yaffs_obj_t *obj, char *buffer, int size);
16724 +int yaffs_remove_xattrib(yaffs_obj_t *obj, const YCHAR *name);
16726 /* Special directories */
16727 -yaffs_Object *yaffs_Root(yaffs_Device *dev);
16728 -yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
16729 +yaffs_obj_t *yaffs_root(yaffs_dev_t *dev);
16730 +yaffs_obj_t *yaffs_lost_n_found(yaffs_dev_t *dev);
16732 #ifdef CONFIG_YAFFS_WINCE
16733 /* CONFIG_YAFFS_WINCE special stuff */
16734 -void yfsd_WinFileTimeNow(__u32 target[2]);
16735 +void yfsd_win_file_time_now(__u32 target[2]);
16739 +void yaffs_handle_defered_free(yaffs_obj_t *obj);
16741 -void yaffs_HandleDeferedFree(yaffs_Object *obj);
16743 +void yaffs_update_dirty_dirs(yaffs_dev_t *dev);
16745 +int yaffs_bg_gc(yaffs_dev_t *dev, unsigned urgency);
16748 -int yaffs_DumpObject(yaffs_Object *obj);
16749 +int yaffs_dump_obj(yaffs_obj_t *obj);
16751 -void yaffs_GutsTest(yaffs_Device *dev);
16752 +void yaffs_guts_test(yaffs_dev_t *dev);
16754 -/* A few useful functions */
16755 -void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
16756 -void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn);
16757 -int yaffs_CheckFF(__u8 *buffer, int nBytes);
16758 -void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
16759 +/* A few useful functions to be used within the core files*/
16760 +void yaffs_chunk_del(yaffs_dev_t *dev, int chunk_id, int mark_flash, int lyn);
16761 +int yaffs_check_ff(__u8 *buffer, int n_bytes);
16762 +void yaffs_handle_chunk_error(yaffs_dev_t *dev, yaffs_block_info_t *bi);
16764 +__u8 *yaffs_get_temp_buffer(yaffs_dev_t *dev, int line_no);
16765 +void yaffs_release_temp_buffer(yaffs_dev_t *dev, __u8 *buffer, int line_no);
16767 +yaffs_obj_t *yaffs_find_or_create_by_number(yaffs_dev_t *dev,
16769 + yaffs_obj_type type);
16770 +int yaffs_put_chunk_in_file(yaffs_obj_t *in, int inode_chunk,
16771 + int nand_chunk, int in_scan);
16772 +void yaffs_set_obj_name(yaffs_obj_t *obj, const YCHAR *name);
16773 +void yaffs_set_obj_name_from_oh(yaffs_obj_t *obj, const yaffs_obj_header *oh);
16774 +void yaffs_add_obj_to_dir(yaffs_obj_t *directory,
16775 + yaffs_obj_t *obj);
16776 +YCHAR *yaffs_clone_str(const YCHAR *str);
16777 +void yaffs_link_fixup(yaffs_dev_t *dev, yaffs_obj_t *hard_list);
16778 +void yaffs_block_became_dirty(yaffs_dev_t *dev, int block_no);
16779 +int yaffs_update_oh(yaffs_obj_t *in, const YCHAR *name,
16780 + int force, int is_shrink, int shadows,
16781 + yaffs_xattr_mod *xop);
16782 +void yaffs_handle_shadowed_obj(yaffs_dev_t *dev, int obj_id,
16783 + int backward_scanning);
16784 +int yaffs_check_alloc_available(yaffs_dev_t *dev, int n_chunks);
16785 +yaffs_tnode_t *yaffs_get_tnode(yaffs_dev_t *dev);
16786 +yaffs_tnode_t *yaffs_add_find_tnode_0(yaffs_dev_t *dev,
16787 + yaffs_file_s *file_struct,
16789 + yaffs_tnode_t *passed_tn);
16791 +int yaffs_do_file_wr(yaffs_obj_t *in, const __u8 *buffer, loff_t offset,
16792 + int n_bytes, int write_trhrough);
16793 +void yaffs_resize_file_down( yaffs_obj_t *obj, loff_t new_size);
16794 +void yaffs_skip_rest_of_block(yaffs_dev_t *dev);
16796 +int yaffs_count_free_chunks(yaffs_dev_t *dev);
16798 +yaffs_tnode_t *yaffs_find_tnode_0(yaffs_dev_t *dev,
16799 + yaffs_file_s *file_struct,
16802 -__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo);
16803 -void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
16804 +__u32 yaffs_get_group_base(yaffs_dev_t *dev, yaffs_tnode_t *tn, unsigned pos);
16807 --- a/fs/yaffs2/yaffsinterface.h
16808 +++ b/fs/yaffs2/yaffsinterface.h
16811 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
16813 - * Copyright (C) 2002-2007 Aleph One Ltd.
16814 + * Copyright (C) 2002-2010 Aleph One Ltd.
16815 * for Toby Churchill Ltd and Brightstar Engineering
16817 * Created by Charles Manning <charles@aleph1.co.uk>
16819 #ifndef __YAFFSINTERFACE_H__
16820 #define __YAFFSINTERFACE_H__
16822 -int yaffs_Initialise(unsigned nBlocks);
16823 +int yaffs_initialise(unsigned nBlocks);
16827 +++ b/fs/yaffs2/yaffs_linux_allocator.c
16830 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
16832 + * Copyright (C) 2002-2010 Aleph One Ltd.
16833 + * for Toby Churchill Ltd and Brightstar Engineering
16835 + * Created by Charles Manning <charles@aleph1.co.uk>
16837 + * This program is free software; you can redistribute it and/or modify
16838 + * it under the terms of the GNU Lesser General Public License version 2.1 as
16839 + * published by the Free Software Foundation.
16841 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
16843 + * Note: Tis code is currently unused. Being checked in in case it becomes useful.
16847 +#include "yaffs_allocator.h"
16848 +#include "yaffs_guts.h"
16849 +#include "yaffs_trace.h"
16850 +#include "yportenv.h"
16851 +#include "yaffs_linux.h"
16853 + * Start out with the same allocator as yaffs direct.
16854 + * Todo: Change to Linux slab allocator.
16859 +#define NAMELEN 20
16860 +struct yaffs_AllocatorStruct {
16861 + char tnode_name[NAMELEN+1];
16862 + char object_name[NAMELEN+1];
16863 + struct kmem_cache *tnode_cache;
16864 + struct kmem_cache *object_cache;
16867 +typedef struct yaffs_AllocatorStruct yaffs_Allocator;
16871 +void yaffs_deinit_raw_tnodes_and_objs(yaffs_dev_t *dev)
16873 + yaffs_Allocator *allocator = (yaffs_Allocator *)dev->allocator;
16875 + T(YAFFS_TRACE_ALLOCATE,(TSTR("Deinitialising yaffs allocator\n")));
16878 + if(allocator->tnode_cache){
16879 + kmem_cache_destroy(allocator->tnode_cache);
16880 + allocator->tnode_cache = NULL;
16882 + T(YAFFS_TRACE_ALWAYS,
16883 + (TSTR("NULL tnode cache\n")));
16887 + if(allocator->object_cache){
16888 + kmem_cache_destroy(allocator->object_cache);
16889 + allocator->object_cache = NULL;
16891 + T(YAFFS_TRACE_ALWAYS,
16892 + (TSTR("NULL object cache\n")));
16896 + YFREE(allocator);
16899 + T(YAFFS_TRACE_ALWAYS,
16900 + (TSTR("Deinitialising NULL allocator\n")));
16903 + dev->allocator = NULL;
16907 +static void fake_ctor0(void *data){data = data;}
16908 +static void fake_ctor1(void *data){data = data;}
16909 +static void fake_ctor2(void *data){data = data;}
16910 +static void fake_ctor3(void *data){data = data;}
16911 +static void fake_ctor4(void *data){data = data;}
16912 +static void fake_ctor5(void *data){data = data;}
16913 +static void fake_ctor6(void *data){data = data;}
16914 +static void fake_ctor7(void *data){data = data;}
16915 +static void fake_ctor8(void *data){data = data;}
16916 +static void fake_ctor9(void *data){data = data;}
16918 +static void (*fake_ctor_list[10]) (void *) = {
16931 +void yaffs_init_raw_tnodes_and_objs(yaffs_dev_t *dev)
16933 + yaffs_Allocator *allocator;
16934 + unsigned mount_id = yaffs_dev_to_lc(dev)->mount_id;
16936 + T(YAFFS_TRACE_ALLOCATE,(TSTR("Initialising yaffs allocator\n")));
16938 + if(dev->allocator)
16940 + else if(mount_id >= 10){
16941 + T(YAFFS_TRACE_ALWAYS,(TSTR("Bad mount_id %u\n"),mount_id));
16943 + allocator = YMALLOC(sizeof(yaffs_Allocator));
16944 + memset(allocator,0,sizeof(yaffs_Allocator));
16945 + dev->allocator = allocator;
16947 + if(!dev->allocator){
16948 + T(YAFFS_TRACE_ALWAYS,
16949 + (TSTR("yaffs allocator creation failed\n")));
16955 + sprintf(allocator->tnode_name,"yaffs_t_%u",mount_id);
16956 + sprintf(allocator->object_name,"yaffs_o_%u",mount_id);
16958 + allocator->tnode_cache =
16959 + kmem_cache_create(allocator->tnode_name,
16962 + fake_ctor_list[mount_id]);
16963 + if(allocator->tnode_cache)
16964 + T(YAFFS_TRACE_ALLOCATE,
16965 + (TSTR("tnode cache \"%s\" %p\n"),
16966 + allocator->tnode_name,allocator->tnode_cache));
16968 + T(YAFFS_TRACE_ALWAYS,
16969 + (TSTR("yaffs cache creation failed\n")));
16974 + allocator->object_cache =
16975 + kmem_cache_create(allocator->object_name,
16976 + sizeof(yaffs_obj_t),
16978 + fake_ctor_list[mount_id]);
16980 + if(allocator->object_cache)
16981 + T(YAFFS_TRACE_ALLOCATE,
16982 + (TSTR("object cache \"%s\" %p\n"),
16983 + allocator->object_name,allocator->object_cache));
16986 + T(YAFFS_TRACE_ALWAYS,
16987 + (TSTR("yaffs cache creation failed\n")));
16994 +yaffs_tnode_t *yaffs_alloc_raw_tnode(yaffs_dev_t *dev)
16996 + yaffs_Allocator *allocator = dev->allocator;
16997 + if(!allocator || !allocator->tnode_cache){
17001 + return kmem_cache_alloc(allocator->tnode_cache, GFP_NOFS);
17004 +void yaffs_free_raw_tnode(yaffs_dev_t *dev, yaffs_tnode_t *tn)
17006 + yaffs_Allocator *allocator = dev->allocator;
17007 + kmem_cache_free(allocator->tnode_cache,tn);
17010 +yaffs_obj_t *yaffs_alloc_raw_obj(yaffs_dev_t *dev)
17012 + yaffs_Allocator *allocator = dev->allocator;
17017 + if(!allocator->object_cache){
17021 + return kmem_cache_alloc(allocator->object_cache, GFP_NOFS);
17024 +void yaffs_free_raw_obj(yaffs_dev_t *dev, yaffs_obj_t *obj)
17026 + yaffs_Allocator *allocator = dev->allocator;
17027 + kmem_cache_free(allocator->object_cache,obj);
17030 +++ b/fs/yaffs2/yaffs_linux.h
17033 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
17035 + * Copyright (C) 2002-2010 Aleph One Ltd.
17036 + * for Toby Churchill Ltd and Brightstar Engineering
17038 + * Created by Charles Manning <charles@aleph1.co.uk>
17040 + * This program is free software; you can redistribute it and/or modify
17041 + * it under the terms of the GNU Lesser General Public License version 2.1 as
17042 + * published by the Free Software Foundation.
17044 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
17047 +#ifndef __YAFFS_LINUX_H__
17048 +#define __YAFFS_LINUX_H__
17050 +#include "devextras.h"
17051 +#include "yportenv.h"
17053 +struct yaffs_LinuxContext {
17054 + struct ylist_head contextList; /* List of these we have mounted */
17055 + struct yaffs_dev_s *dev;
17056 + struct super_block * superBlock;
17057 + struct task_struct *bgThread; /* Background thread for this device */
17059 + struct semaphore grossLock; /* Gross locking semaphore */
17060 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
17061 + * at compile time so we have to allocate it.
17063 + struct ylist_head searchContexts;
17064 + void (*putSuperFunc)(struct super_block *sb);
17066 + struct task_struct *readdirProcess;
17067 + unsigned mount_id;
17070 +#define yaffs_dev_to_lc(dev) ((struct yaffs_LinuxContext *)((dev)->os_context))
17071 +#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
17076 +++ b/fs/yaffs2/yaffs_list.h
17079 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
17081 + * Copyright (C) 2002-2010 Aleph One Ltd.
17082 + * for Toby Churchill Ltd and Brightstar Engineering
17084 + * Created by Charles Manning <charles@aleph1.co.uk>
17086 + * This program is free software; you can redistribute it and/or modify
17087 + * it under the terms of the GNU Lesser General Public License version 2.1 as
17088 + * published by the Free Software Foundation.
17090 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
17094 + * This file is just holds extra declarations of macros that would normally
17095 + * be providesd in the Linux kernel. These macros have been written from
17096 + * scratch but are functionally equivalent to the Linux ones.
17100 +#ifndef __YAFFS_LIST_H__
17101 +#define __YAFFS_LIST_H__
17104 +#include "yportenv.h"
17107 + * This is a simple doubly linked list implementation that matches the
17108 + * way the Linux kernel doubly linked list implementation works.
17111 +struct ylist_head {
17112 + struct ylist_head *next; /* next in chain */
17113 + struct ylist_head *prev; /* previous in chain */
17117 +/* Initialise a static list */
17118 +#define YLIST_HEAD(name) \
17119 +struct ylist_head name = { &(name), &(name)}
17123 +/* Initialise a list head to an empty list */
17124 +#define YINIT_LIST_HEAD(p) \
17126 + (p)->next = (p);\
17127 + (p)->prev = (p); \
17131 +/* Add an element to a list */
17132 +static Y_INLINE void ylist_add(struct ylist_head *newEntry,
17133 + struct ylist_head *list)
17135 + struct ylist_head *listNext = list->next;
17137 + list->next = newEntry;
17138 + newEntry->prev = list;
17139 + newEntry->next = listNext;
17140 + listNext->prev = newEntry;
17144 +static Y_INLINE void ylist_add_tail(struct ylist_head *newEntry,
17145 + struct ylist_head *list)
17147 + struct ylist_head *listPrev = list->prev;
17149 + list->prev = newEntry;
17150 + newEntry->next = list;
17151 + newEntry->prev = listPrev;
17152 + listPrev->next = newEntry;
17157 +/* Take an element out of its current list, with or without
17158 + * reinitialising the links.of the entry*/
17159 +static Y_INLINE void ylist_del(struct ylist_head *entry)
17161 + struct ylist_head *listNext = entry->next;
17162 + struct ylist_head *listPrev = entry->prev;
17164 + listNext->prev = listPrev;
17165 + listPrev->next = listNext;
17169 +static Y_INLINE void ylist_del_init(struct ylist_head *entry)
17171 + ylist_del(entry);
17172 + entry->next = entry->prev = entry;
17176 +/* Test if the list is empty */
17177 +static Y_INLINE int ylist_empty(struct ylist_head *entry)
17179 + return (entry->next == entry);
17183 +/* ylist_entry takes a pointer to a list entry and offsets it to that
17184 + * we can find a pointer to the object it is embedded in.
17188 +#define ylist_entry(entry, type, member) \
17189 + ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
17192 +/* ylist_for_each and list_for_each_safe iterate over lists.
17193 + * ylist_for_each_safe uses temporary storage to make the list delete safe
17196 +#define ylist_for_each(itervar, list) \
17197 + for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
17199 +#define ylist_for_each_safe(itervar, saveVar, list) \
17200 + for (itervar = (list)->next, saveVar = (list)->next->next; \
17201 + itervar != (list); itervar = saveVar, saveVar = saveVar->next)
17205 --- a/fs/yaffs2/yaffs_mtdif1.c
17206 +++ b/fs/yaffs2/yaffs_mtdif1.c
17208 * YAFFS: Yet another FFS. A NAND-flash specific file system.
17209 * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
17211 - * Copyright (C) 2002 Aleph One Ltd.
17212 + * Copyright (C) 2002-2010 Aleph One Ltd.
17213 * for Toby Churchill Ltd and Brightstar Engineering
17215 * This program is free software; you can redistribute it and/or modify
17216 @@ -18,15 +18,17 @@
17218 * These functions are invoked via function pointers in yaffs_nand.c.
17219 * This replaces functionality provided by functions in yaffs_mtdif.c
17220 - * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
17221 + * and the yaffs_tags_tCompatability functions in yaffs_tagscompat.c that are
17222 * called in yaffs_mtdif.c when the function pointers are NULL.
17223 - * We assume the MTD layer is performing ECC (useNANDECC is true).
17224 + * We assume the MTD layer is performing ECC (use_nand_ecc is true).
17227 #include "yportenv.h"
17228 +#include "yaffs_trace.h"
17229 #include "yaffs_guts.h"
17230 #include "yaffs_packedtags1.h"
17231 -#include "yaffs_tagscompat.h" /* for yaffs_CalcTagsECC */
17232 +#include "yaffs_tagscompat.h" /* for yaffs_calc_tags_ecc */
17233 +#include "yaffs_linux.h"
17235 #include "linux/kernel.h"
17236 #include "linux/version.h"
17238 /* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
17239 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
17241 -const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.10 2009-03-09 07:41:10 charles Exp $";
17243 #ifndef CONFIG_YAFFS_9BYTE_TAGS
17244 # define YTAG1_SIZE 8
17246 @@ -51,12 +51,12 @@ const char *yaffs_mtdif1_c_version = "$I
17247 * adjust 'oobfree' to match your existing Yaffs data.
17249 * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
17250 - * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
17251 + * page_status byte (at NAND spare offset 4) scattered/gathered from/to
17254 * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
17255 - * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
17256 - * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
17257 + * We have/need PackedTags1 plus page_status: T0,T1,T2,T3,T4,T5,T6,T7,P
17258 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the page_status
17259 * byte and B is the small-page bad-block indicator byte.
17261 static struct nand_ecclayout nand_oob_16 = {
17262 @@ -88,42 +88,40 @@ static struct nand_ecclayout nand_oob_16
17263 * Any underlying MTD error results in YAFFS_FAIL.
17264 * Returns YAFFS_OK or YAFFS_FAIL.
17266 -int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
17267 - int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
17268 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_dev_t *dev,
17269 + int nand_chunk, const __u8 *data, const yaffs_ext_tags *etags)
17271 - struct mtd_info *mtd = dev->genericDevice;
17272 - int chunkBytes = dev->nDataBytesPerChunk;
17273 - loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
17274 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
17275 + int chunkBytes = dev->data_bytes_per_chunk;
17276 + loff_t addr = ((loff_t)nand_chunk) * chunkBytes;
17277 struct mtd_oob_ops ops;
17278 yaffs_PackedTags1 pt1;
17281 - /* we assume that PackedTags1 and yaffs_Tags are compatible */
17282 + /* we assume that PackedTags1 and yaffs_tags_t are compatible */
17283 compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
17284 - compile_time_assertion(sizeof(yaffs_Tags) == 8);
17286 - dev->nPageWrites++;
17287 + compile_time_assertion(sizeof(yaffs_tags_t) == 8);
17289 yaffs_PackTags1(&pt1, etags);
17290 - yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
17291 + yaffs_calc_tags_ecc((yaffs_tags_t *)&pt1);
17293 /* When deleting a chunk, the upper layer provides only skeletal
17294 - * etags, one with chunkDeleted set. However, we need to update the
17295 + * etags, one with is_deleted set. However, we need to update the
17296 * tags, not erase them completely. So we use the NAND write property
17297 * that only zeroed-bits stick and set tag bytes to all-ones and
17298 * zero just the (not) deleted bit.
17300 #ifndef CONFIG_YAFFS_9BYTE_TAGS
17301 - if (etags->chunkDeleted) {
17302 + if (etags->is_deleted) {
17303 memset(&pt1, 0xff, 8);
17304 /* clear delete status bit to indicate deleted */
17308 ((__u8 *)&pt1)[8] = 0xff;
17309 - if (etags->chunkDeleted) {
17310 + if (etags->is_deleted) {
17311 memset(&pt1, 0xff, 8);
17312 - /* zero pageStatus byte to indicate deleted */
17313 + /* zero page_status byte to indicate deleted */
17314 ((__u8 *)&pt1)[8] = 0;
17317 @@ -137,20 +135,20 @@ int nandmtd1_WriteChunkWithTagsToNAND(ya
17319 retval = mtd->write_oob(mtd, addr, &ops);
17321 - yaffs_trace(YAFFS_TRACE_MTD,
17322 - "write_oob failed, chunk %d, mtd error %d\n",
17323 - chunkInNAND, retval);
17324 + T(YAFFS_TRACE_MTD,
17325 + (TSTR("write_oob failed, chunk %d, mtd error %d"TENDSTR),
17326 + nand_chunk, retval));
17328 return retval ? YAFFS_FAIL : YAFFS_OK;
17331 -/* Return with empty ExtendedTags but add eccResult.
17332 +/* Return with empty ExtendedTags but add ecc_result.
17334 -static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
17335 +static int rettags(yaffs_ext_tags *etags, int ecc_result, int retval)
17338 memset(etags, 0, sizeof(*etags));
17339 - etags->eccResult = eccResult;
17340 + etags->ecc_result = ecc_result;
17344 @@ -158,30 +156,28 @@ static int rettags(yaffs_ExtendedTags *e
17345 /* Read a chunk (page) from NAND.
17347 * Caller expects ExtendedTags data to be usable even on error; that is,
17348 - * all members except eccResult and blockBad are zeroed.
17349 + * all members except ecc_result and block_bad are zeroed.
17351 * - Check ECC results for data (if applicable)
17352 * - Check for blank/erased block (return empty ExtendedTags if blank)
17353 * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
17354 * - Convert PackedTags1 to ExtendedTags
17355 - * - Update eccResult and blockBad members to refect state.
17356 + * - Update ecc_result and block_bad members to refect state.
17358 * Returns YAFFS_OK or YAFFS_FAIL.
17360 -int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
17361 - int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
17362 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev,
17363 + int nand_chunk, __u8 *data, yaffs_ext_tags *etags)
17365 - struct mtd_info *mtd = dev->genericDevice;
17366 - int chunkBytes = dev->nDataBytesPerChunk;
17367 - loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
17368 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
17369 + int chunkBytes = dev->data_bytes_per_chunk;
17370 + loff_t addr = ((loff_t)nand_chunk) * chunkBytes;
17371 int eccres = YAFFS_ECC_RESULT_NO_ERROR;
17372 struct mtd_oob_ops ops;
17373 yaffs_PackedTags1 pt1;
17377 - dev->nPageReads++;
17379 memset(&ops, 0, sizeof(ops));
17380 ops.mode = MTD_OOB_AUTO;
17381 ops.len = (data) ? chunkBytes : 0;
17382 @@ -200,9 +196,9 @@ int nandmtd1_ReadChunkWithTagsFromNAND(y
17384 retval = mtd->read_oob(mtd, addr, &ops);
17386 - yaffs_trace(YAFFS_TRACE_MTD,
17387 - "read_oob failed, chunk %d, mtd error %d\n",
17388 - chunkInNAND, retval);
17389 + T(YAFFS_TRACE_MTD,
17390 + (TSTR("read_oob failed, chunk %d, mtd error %d"TENDSTR),
17391 + nand_chunk, retval));
17395 @@ -213,23 +209,23 @@ int nandmtd1_ReadChunkWithTagsFromNAND(y
17397 /* MTD's ECC fixed the data */
17398 eccres = YAFFS_ECC_RESULT_FIXED;
17400 + dev->n_ecc_fixed++;
17404 /* MTD's ECC could not fix the data */
17405 - dev->eccUnfixed++;
17406 + dev->n_ecc_unfixed++;
17409 rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
17410 - etags->blockBad = (mtd->block_isbad)(mtd, addr);
17411 + etags->block_bad = (mtd->block_isbad)(mtd, addr);
17415 /* Check for a blank/erased chunk.
17417 - if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
17418 - /* when blank, upper layers want eccResult to be <= NO_ERROR */
17419 + if (yaffs_check_ff((__u8 *)&pt1, 8)) {
17420 + /* when blank, upper layers want ecc_result to be <= NO_ERROR */
17421 return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
17424 @@ -241,37 +237,37 @@ int nandmtd1_ReadChunkWithTagsFromNAND(y
17425 deleted = !pt1.deleted;
17428 - deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
17429 + deleted = (yaffs_count_bits(((__u8 *)&pt1)[8]) < 7);
17432 /* Check the packed tags mini-ECC and correct if necessary/possible.
17434 - retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
17435 + retval = yaffs_check_tags_ecc((yaffs_tags_t *)&pt1);
17438 /* no tags error, use MTD result */
17441 /* recovered tags-ECC error */
17442 - dev->tagsEccFixed++;
17443 + dev->n_tags_ecc_fixed++;
17444 if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
17445 eccres = YAFFS_ECC_RESULT_FIXED;
17448 /* unrecovered tags-ECC error */
17449 - dev->tagsEccUnfixed++;
17450 + dev->n_tags_ecc_unfixed++;
17451 return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
17454 /* Unpack the tags to extended form and set ECC result.
17455 - * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
17456 + * [set shouldBeFF just to keep yaffs_unpack_tags1 happy]
17458 pt1.shouldBeFF = 0xFFFFFFFF;
17459 - yaffs_UnpackTags1(etags, &pt1);
17460 - etags->eccResult = eccres;
17461 + yaffs_unpack_tags1(etags, &pt1);
17462 + etags->ecc_result = eccres;
17464 /* Set deleted state */
17465 - etags->chunkDeleted = deleted;
17466 + etags->is_deleted = deleted;
17470 @@ -282,15 +278,15 @@ int nandmtd1_ReadChunkWithTagsFromNAND(y
17472 * Returns YAFFS_OK or YAFFS_FAIL.
17474 -int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
17475 +int nandmtd1_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no)
17477 - struct mtd_info *mtd = dev->genericDevice;
17478 - int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
17479 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
17480 + int blocksize = dev->param.chunks_per_block * dev->data_bytes_per_chunk;
17483 - yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad\n", blockNo);
17484 + T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("marking block %d bad"TENDSTR), block_no));
17486 - retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
17487 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * block_no);
17488 return (retval) ? YAFFS_FAIL : YAFFS_OK;
17491 @@ -305,9 +301,9 @@ static int nandmtd1_TestPrerequists(stru
17492 int oobavail = mtd->ecclayout->oobavail;
17494 if (oobavail < YTAG1_SIZE) {
17495 - yaffs_trace(YAFFS_TRACE_ERROR,
17496 - "mtd device has only %d bytes for tags, need %d\n",
17497 - oobavail, YTAG1_SIZE);
17498 + T(YAFFS_TRACE_ERROR,
17499 + (TSTR("mtd device has only %d bytes for tags, need %d"TENDSTR),
17500 + oobavail, YTAG1_SIZE));
17504 @@ -322,13 +318,13 @@ static int nandmtd1_TestPrerequists(stru
17506 * Always returns YAFFS_OK.
17508 -int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
17509 - yaffs_BlockState *pState, __u32 *pSequenceNumber)
17510 +int nandmtd1_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
17511 + yaffs_block_state_t *pState, __u32 *pSequenceNumber)
17513 - struct mtd_info *mtd = dev->genericDevice;
17514 - int chunkNo = blockNo * dev->nChunksPerBlock;
17515 - loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
17516 - yaffs_ExtendedTags etags;
17517 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
17518 + int chunkNo = block_no * dev->param.chunks_per_block;
17519 + loff_t addr = (loff_t)chunkNo * dev->data_bytes_per_chunk;
17520 + yaffs_ext_tags etags;
17521 int state = YAFFS_BLOCK_STATE_DEAD;
17524 @@ -340,17 +336,17 @@ int nandmtd1_QueryNANDBlock(struct yaffs
17527 retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
17528 - etags.blockBad = (mtd->block_isbad)(mtd, addr);
17529 - if (etags.blockBad) {
17530 - yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
17531 - "block %d is marked bad\n", blockNo);
17532 + etags.block_bad = (mtd->block_isbad)(mtd, addr);
17533 + if (etags.block_bad) {
17534 + T(YAFFS_TRACE_BAD_BLOCKS,
17535 + (TSTR("block %d is marked bad"TENDSTR), block_no));
17536 state = YAFFS_BLOCK_STATE_DEAD;
17537 - } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
17538 + } else if (etags.ecc_result != YAFFS_ECC_RESULT_NO_ERROR) {
17539 /* bad tags, need to look more closely */
17540 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
17541 - } else if (etags.chunkUsed) {
17542 + } else if (etags.chunk_used) {
17543 state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
17544 - seqnum = etags.sequenceNumber;
17545 + seqnum = etags.seq_number;
17547 state = YAFFS_BLOCK_STATE_EMPTY;
17549 --- a/fs/yaffs2/yaffs_mtdif1-compat.c
17552 -From ian@brightstareng.com Fri May 18 15:06:49 2007
17553 -From ian@brightstareng.com Fri May 18 15:08:21 2007
17554 -Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
17555 - by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
17556 - (envelope-from <ian@brightstareng.com>)
17557 - id 1Hp380-00011e-T6
17558 - for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
17559 -Received: from localhost (localhost.localdomain [127.0.0.1])
17560 - by zebra.brightstareng.com (Postfix) with ESMTP
17561 - id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
17562 -Received: from zebra.brightstareng.com ([127.0.0.1])
17563 - by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
17564 - id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
17565 -Received: from pippin (unknown [192.168.1.25])
17566 - by zebra.brightstareng.com (Postfix) with ESMTP
17567 - id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
17568 -From: Ian McDonnell <ian@brightstareng.com>
17569 -To: David Goodenough <david.goodenough@linkchoose.co.uk>
17570 -Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
17571 -Date: Fri, 18 May 2007 10:06:49 -0400
17572 -User-Agent: KMail/1.9.1
17573 -References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
17574 -In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
17575 -Cc: Andrea Conti <alyf@alyf.net>,
17576 - Charles Manning <manningc2@actrix.gen.nz>
17578 -Content-Type: Multipart/Mixed;
17579 - boundary="Boundary-00=_5LbTGmt62YoutxM"
17580 -Message-Id: <200705181006.49860.ian@brightstareng.com>
17581 -X-Virus-Scanned: by amavisd-new at brightstareng.com
17584 -X-KMail-EncryptionState:
17585 -X-KMail-SignatureState:
17588 ---Boundary-00=_5LbTGmt62YoutxM
17589 -Content-Type: text/plain;
17590 - charset="iso-8859-15"
17591 -Content-Transfer-Encoding: 7bit
17592 -Content-Disposition: inline
17596 -On Friday 18 May 2007 08:34, you wrote:
17597 -> Yea team. With this fix in place (I put it in the wrong place
17598 -> at first) I can now mount and ls the Yaffs partition without
17599 -> an error messages!
17603 -Attached is a newer yaffs_mtdif1.c with a bandaid to help the
17604 -2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
17605 -See the LINUX_VERSION_CODE conditional in
17606 -nandmtd1_ReadChunkWithTagsFromNAND.
17610 ---Boundary-00=_5LbTGmt62YoutxM
17611 -Content-Type: text/x-csrc;
17612 - charset="iso-8859-15";
17613 - name="yaffs_mtdif1.c"
17614 -Content-Transfer-Encoding: 7bit
17615 -Content-Disposition: attachment;
17616 - filename="yaffs_mtdif1.c"
17619 - * YAFFS: Yet another FFS. A NAND-flash specific file system.
17620 - * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
17622 - * Copyright (C) 2002 Aleph One Ltd.
17623 - * for Toby Churchill Ltd and Brightstar Engineering
17625 - * This program is free software; you can redistribute it and/or modify
17626 - * it under the terms of the GNU General Public License version 2 as
17627 - * published by the Free Software Foundation.
17631 - * This module provides the interface between yaffs_nand.c and the
17632 - * MTD API. This version is used when the MTD interface supports the
17633 - * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
17634 - * and we have small-page NAND device.
17636 - * These functions are invoked via function pointers in yaffs_nand.c.
17637 - * This replaces functionality provided by functions in yaffs_mtdif.c
17638 - * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
17639 - * called in yaffs_mtdif.c when the function pointers are NULL.
17640 - * We assume the MTD layer is performing ECC (useNANDECC is true).
17643 -#include "yportenv.h"
17644 -#include "yaffs_guts.h"
17645 -#include "yaffs_packedtags1.h"
17646 -#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
17648 -#include "linux/kernel.h"
17649 -#include "linux/version.h"
17650 -#include "linux/types.h"
17651 -#include "linux/mtd/mtd.h"
17653 -/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
17654 -#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
17656 -const char *yaffs_mtdif1_c_version = "$Id$";
17658 -#ifndef CONFIG_YAFFS_9BYTE_TAGS
17659 -# define YTAG1_SIZE 8
17661 -# define YTAG1_SIZE 9
17665 -/* Use the following nand_ecclayout with MTD when using
17666 - * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
17667 - * If you have existing Yaffs images and the byte order differs from this,
17668 - * adjust 'oobfree' to match your existing Yaffs data.
17670 - * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
17671 - * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
17674 - * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
17675 - * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
17676 - * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
17677 - * byte and B is the small-page bad-block indicator byte.
17679 -static struct nand_ecclayout nand_oob_16 = {
17681 - .eccpos = { 8, 9, 10, 13, 14, 15 },
17683 - .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
17687 -/* Write a chunk (page) of data to NAND.
17689 - * Caller always provides ExtendedTags data which are converted to a more
17690 - * compact (packed) form for storage in NAND. A mini-ECC runs over the
17691 - * contents of the tags meta-data; used to valid the tags when read.
17693 - * - Pack ExtendedTags to PackedTags1 form
17694 - * - Compute mini-ECC for PackedTags1
17695 - * - Write data and packed tags to NAND.
17697 - * Note: Due to the use of the PackedTags1 meta-data which does not include
17698 - * a full sequence number (as found in the larger PackedTags2 form) it is
17699 - * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
17700 - * discarded and dirty. This is not ideal: newer NAND parts are supposed
17701 - * to be written just once. When Yaffs performs this operation, this
17702 - * function is called with a NULL data pointer -- calling MTD write_oob
17703 - * without data is valid usage (2.6.17).
17705 - * Any underlying MTD error results in YAFFS_FAIL.
17706 - * Returns YAFFS_OK or YAFFS_FAIL.
17708 -int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
17709 - int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
17711 - struct mtd_info * mtd = dev->genericDevice;
17712 - int chunkBytes = dev->nDataBytesPerChunk;
17713 - loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
17714 - struct mtd_oob_ops ops;
17715 - yaffs_PackedTags1 pt1;
17718 - /* we assume that PackedTags1 and yaffs_Tags are compatible */
17719 - compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
17720 - compile_time_assertion(sizeof(yaffs_Tags) == 8);
17722 - yaffs_PackTags1(&pt1, etags);
17723 - yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
17725 - /* When deleting a chunk, the upper layer provides only skeletal
17726 - * etags, one with chunkDeleted set. However, we need to update the
17727 - * tags, not erase them completely. So we use the NAND write property
17728 - * that only zeroed-bits stick and set tag bytes to all-ones and
17729 - * zero just the (not) deleted bit.
17731 -#ifndef CONFIG_YAFFS_9BYTE_TAGS
17732 - if (etags->chunkDeleted) {
17733 - memset(&pt1, 0xff, 8);
17734 - /* clear delete status bit to indicate deleted */
17738 - ((__u8 *)&pt1)[8] = 0xff;
17739 - if (etags->chunkDeleted) {
17740 - memset(&pt1, 0xff, 8);
17741 - /* zero pageStatus byte to indicate deleted */
17742 - ((__u8 *)&pt1)[8] = 0;
17746 - memset(&ops, 0, sizeof(ops));
17747 - ops.mode = MTD_OOB_AUTO;
17748 - ops.len = (data) ? chunkBytes : 0;
17749 - ops.ooblen = YTAG1_SIZE;
17750 - ops.datbuf = (__u8 *)data;
17751 - ops.oobbuf = (__u8 *)&pt1;
17753 - retval = mtd->write_oob(mtd, addr, &ops);
17755 - yaffs_trace(YAFFS_TRACE_MTD,
17756 - "write_oob failed, chunk %d, mtd error %d\n",
17757 - chunkInNAND, retval);
17759 - return retval ? YAFFS_FAIL : YAFFS_OK;
17762 -/* Return with empty ExtendedTags but add eccResult.
17764 -static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
17767 - memset(etags, 0, sizeof(*etags));
17768 - etags->eccResult = eccResult;
17773 -/* Read a chunk (page) from NAND.
17775 - * Caller expects ExtendedTags data to be usable even on error; that is,
17776 - * all members except eccResult and blockBad are zeroed.
17778 - * - Check ECC results for data (if applicable)
17779 - * - Check for blank/erased block (return empty ExtendedTags if blank)
17780 - * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
17781 - * - Convert PackedTags1 to ExtendedTags
17782 - * - Update eccResult and blockBad members to refect state.
17784 - * Returns YAFFS_OK or YAFFS_FAIL.
17786 -int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
17787 - int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
17789 - struct mtd_info * mtd = dev->genericDevice;
17790 - int chunkBytes = dev->nDataBytesPerChunk;
17791 - loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
17792 - int eccres = YAFFS_ECC_RESULT_NO_ERROR;
17793 - struct mtd_oob_ops ops;
17794 - yaffs_PackedTags1 pt1;
17798 - memset(&ops, 0, sizeof(ops));
17799 - ops.mode = MTD_OOB_AUTO;
17800 - ops.len = (data) ? chunkBytes : 0;
17801 - ops.ooblen = YTAG1_SIZE;
17802 - ops.datbuf = data;
17803 - ops.oobbuf = (__u8 *)&pt1;
17805 -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
17806 - /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
17807 - * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
17809 - ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
17811 - /* Read page and oob using MTD.
17812 - * Check status and determine ECC result.
17814 - retval = mtd->read_oob(mtd, addr, &ops);
17816 - yaffs_trace(YAFFS_TRACE_MTD,
17817 - "read_oob failed, chunk %d, mtd error %d\n",
17818 - chunkInNAND, retval);
17821 - switch (retval) {
17827 - /* MTD's ECC fixed the data */
17828 - eccres = YAFFS_ECC_RESULT_FIXED;
17833 - /* MTD's ECC could not fix the data */
17834 - dev->eccUnfixed++;
17835 - /* fall into... */
17837 - rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
17838 - etags->blockBad = (mtd->block_isbad)(mtd, addr);
17839 - return YAFFS_FAIL;
17842 - /* Check for a blank/erased chunk.
17844 - if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
17845 - /* when blank, upper layers want eccResult to be <= NO_ERROR */
17846 - return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
17849 -#ifndef CONFIG_YAFFS_9BYTE_TAGS
17850 - /* Read deleted status (bit) then return it to it's non-deleted
17851 - * state before performing tags mini-ECC check. pt1.deleted is
17854 - deleted = !pt1.deleted;
17857 - (void) deleted; /* not used */
17860 - /* Check the packed tags mini-ECC and correct if necessary/possible.
17862 - retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
17863 - switch (retval) {
17865 - /* no tags error, use MTD result */
17868 - /* recovered tags-ECC error */
17869 - dev->tagsEccFixed++;
17870 - eccres = YAFFS_ECC_RESULT_FIXED;
17873 - /* unrecovered tags-ECC error */
17874 - dev->tagsEccUnfixed++;
17875 - return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
17878 - /* Unpack the tags to extended form and set ECC result.
17879 - * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
17881 - pt1.shouldBeFF = 0xFFFFFFFF;
17882 - yaffs_UnpackTags1(etags, &pt1);
17883 - etags->eccResult = eccres;
17885 - /* Set deleted state.
17887 -#ifndef CONFIG_YAFFS_9BYTE_TAGS
17888 - etags->chunkDeleted = deleted;
17890 - etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
17895 -/* Mark a block bad.
17897 - * This is a persistant state.
17898 - * Use of this function should be rare.
17900 - * Returns YAFFS_OK or YAFFS_FAIL.
17902 -int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
17904 - struct mtd_info * mtd = dev->genericDevice;
17905 - int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
17908 - yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
17910 - retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
17911 - return (retval) ? YAFFS_FAIL : YAFFS_OK;
17914 -/* Check any MTD prerequists.
17916 - * Returns YAFFS_OK or YAFFS_FAIL.
17918 -static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
17920 - /* 2.6.18 has mtd->ecclayout->oobavail */
17921 - /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
17922 - int oobavail = mtd->ecclayout->oobavail;
17924 - if (oobavail < YTAG1_SIZE) {
17925 - yaffs_trace(YAFFS_TRACE_ERROR,
17926 - "mtd device has only %d bytes for tags, need %d",
17927 - oobavail, YTAG1_SIZE);
17928 - return YAFFS_FAIL;
17933 -/* Query for the current state of a specific block.
17935 - * Examine the tags of the first chunk of the block and return the state:
17936 - * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
17937 - * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
17938 - * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
17940 - * Always returns YAFFS_OK.
17942 -int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
17943 - yaffs_BlockState * pState, int *pSequenceNumber)
17945 - struct mtd_info * mtd = dev->genericDevice;
17946 - int chunkNo = blockNo * dev->nChunksPerBlock;
17947 - yaffs_ExtendedTags etags;
17948 - int state = YAFFS_BLOCK_STATE_DEAD;
17952 - /* We don't yet have a good place to test for MTD config prerequists.
17953 - * Do it here as we are called during the initial scan.
17955 - if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
17956 - return YAFFS_FAIL;
17959 - retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
17960 - if (etags.blockBad) {
17961 - yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
17962 - "block %d is marked bad", blockNo);
17963 - state = YAFFS_BLOCK_STATE_DEAD;
17965 - else if (etags.chunkUsed) {
17966 - state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
17967 - seqnum = etags.sequenceNumber;
17970 - state = YAFFS_BLOCK_STATE_EMPTY;
17974 - *pSequenceNumber = seqnum;
17976 - /* query always succeeds */
17980 -#endif /*KERNEL_VERSION*/
17982 ---Boundary-00=_5LbTGmt62YoutxM--
17986 --- a/fs/yaffs2/yaffs_mtdif1.h
17987 +++ b/fs/yaffs2/yaffs_mtdif1.h
17990 * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
17992 - * Copyright (C) 2002-2007 Aleph One Ltd.
17993 + * Copyright (C) 2002-2010 Aleph One Ltd.
17994 * for Toby Churchill Ltd and Brightstar Engineering
17996 * This program is free software; you can redistribute it and/or modify
17997 @@ -14,15 +14,15 @@
17998 #ifndef __YAFFS_MTDIF1_H__
17999 #define __YAFFS_MTDIF1_H__
18001 -int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
18002 - const __u8 *data, const yaffs_ExtendedTags *tags);
18003 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk,
18004 + const __u8 *data, const yaffs_ext_tags *tags);
18006 -int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
18007 - __u8 *data, yaffs_ExtendedTags *tags);
18008 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
18009 + __u8 *data, yaffs_ext_tags *tags);
18011 -int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
18012 +int nandmtd1_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no);
18014 -int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
18015 - yaffs_BlockState *state, __u32 *sequenceNumber);
18016 +int nandmtd1_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
18017 + yaffs_block_state_t *state, __u32 *seq_number);
18020 --- a/fs/yaffs2/yaffs_mtdif2.c
18021 +++ b/fs/yaffs2/yaffs_mtdif2.c
18024 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
18026 - * Copyright (C) 2002-2007 Aleph One Ltd.
18027 + * Copyright (C) 2002-2010 Aleph One Ltd.
18028 * for Toby Churchill Ltd and Brightstar Engineering
18030 * Created by Charles Manning <charles@aleph1.co.uk>
18033 /* mtd interface for YAFFS2 */
18035 -const char *yaffs_mtdif2_c_version =
18036 - "$Id: yaffs_mtdif2.c,v 1.23 2009-03-06 17:20:53 wookey Exp $";
18038 #include "yportenv.h"
18040 +#include "yaffs_trace.h"
18042 #include "yaffs_mtdif2.h"
18044 @@ -27,15 +24,17 @@ const char *yaffs_mtdif2_c_version =
18046 #include "yaffs_packedtags2.h"
18048 +#include "yaffs_linux.h"
18050 /* NB For use with inband tags....
18051 * We assume that the data buffer is of size totalBytersPerChunk so that we can also
18052 * use it to load the tags.
18054 -int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
18055 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk,
18057 - const yaffs_ExtendedTags *tags)
18058 + const yaffs_ext_tags *tags)
18060 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18061 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
18062 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18063 struct mtd_oob_ops ops;
18065 @@ -47,13 +46,16 @@ int nandmtd2_WriteChunkWithTagsToNAND(ya
18067 yaffs_PackedTags2 pt;
18069 + int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
18070 + void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t : (void *)&pt;
18074 ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
18075 - TENDSTR), chunkInNAND, data, tags));
18076 + TENDSTR), nand_chunk, data, tags));
18079 - addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
18080 + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
18082 /* For yaffs2 writing there must be both data and tags.
18083 * If we're using inband tags, then the tags are stuffed into
18084 @@ -61,30 +63,30 @@ int nandmtd2_WriteChunkWithTagsToNAND(ya
18086 if (!data || !tags)
18088 - else if (dev->inbandTags) {
18089 + else if (dev->param.inband_tags) {
18090 yaffs_PackedTags2TagsPart *pt2tp;
18091 - pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
18092 + pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->data_bytes_per_chunk);
18093 yaffs_PackTags2TagsPart(pt2tp, tags);
18095 - yaffs_PackTags2(&pt, tags);
18096 + yaffs_PackTags2(&pt, tags, !dev->param.no_tags_ecc);
18098 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
18099 ops.mode = MTD_OOB_AUTO;
18100 - ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
18101 - ops.len = dev->totalBytesPerChunk;
18102 + ops.ooblen = (dev->param.inband_tags) ? 0 : packed_tags_size;
18103 + ops.len = dev->param.total_bytes_per_chunk;
18105 ops.datbuf = (__u8 *)data;
18106 - ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
18107 + ops.oobbuf = (dev->param.inband_tags) ? NULL : packed_tags_ptr;
18108 retval = mtd->write_oob(mtd, addr, &ops);
18111 - if (!dev->inbandTags) {
18112 + if (!dev->param.inband_tags) {
18114 - mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
18115 - &dummy, data, (__u8 *) &pt, NULL);
18116 + mtd->write_ecc(mtd, addr, dev->data_bytes_per_chunk,
18117 + &dummy, data, (__u8 *) packed_tags_ptr, NULL);
18120 - mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
18121 + mtd->write(mtd, addr, dev->param.total_bytes_per_chunk, &dummy,
18125 @@ -95,10 +97,10 @@ int nandmtd2_WriteChunkWithTagsToNAND(ya
18129 -int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
18130 - __u8 *data, yaffs_ExtendedTags *tags)
18131 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
18132 + __u8 *data, yaffs_ext_tags *tags)
18134 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18135 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
18136 #if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18137 struct mtd_oob_ops ops;
18139 @@ -106,20 +108,23 @@ int nandmtd2_ReadChunkWithTagsFromNAND(y
18143 - loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
18144 + loff_t addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
18146 yaffs_PackedTags2 pt;
18148 + int packed_tags_size = dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
18149 + void * packed_tags_ptr = dev->param.no_tags_ecc ? (void *) &pt.t: (void *)&pt;
18153 ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
18154 - TENDSTR), chunkInNAND, data, tags));
18155 + TENDSTR), nand_chunk, data, tags));
18157 - if (dev->inbandTags) {
18158 + if (dev->param.inband_tags) {
18162 - data = yaffs_GetTempBuffer(dev, __LINE__);
18163 + data = yaffs_get_temp_buffer(dev, __LINE__);
18167 @@ -127,30 +132,30 @@ int nandmtd2_ReadChunkWithTagsFromNAND(y
18170 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
18171 - if (dev->inbandTags || (data && !tags))
18172 - retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
18173 + if (dev->param.inband_tags || (data && !tags))
18174 + retval = mtd->read(mtd, addr, dev->param.total_bytes_per_chunk,
18177 ops.mode = MTD_OOB_AUTO;
18178 - ops.ooblen = sizeof(pt);
18179 - ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
18180 + ops.ooblen = packed_tags_size;
18181 + ops.len = data ? dev->data_bytes_per_chunk : packed_tags_size;
18184 - ops.oobbuf = dev->spareBuffer;
18185 + ops.oobbuf = yaffs_dev_to_lc(dev)->spareBuffer;
18186 retval = mtd->read_oob(mtd, addr, &ops);
18189 - if (!dev->inbandTags && data && tags) {
18190 + if (!dev->param.inband_tags && data && tags) {
18192 - retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
18193 + retval = mtd->read_ecc(mtd, addr, dev->data_bytes_per_chunk,
18194 &dummy, data, dev->spareBuffer,
18199 - mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
18200 + mtd->read(mtd, addr, dev->data_bytes_per_chunk, &dummy,
18202 - if (!dev->inbandTags && tags)
18203 + if (!dev->param.inband_tags && tags)
18205 mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
18207 @@ -158,41 +163,47 @@ int nandmtd2_ReadChunkWithTagsFromNAND(y
18211 - if (dev->inbandTags) {
18212 + if (dev->param.inband_tags) {
18214 yaffs_PackedTags2TagsPart *pt2tp;
18215 - pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
18216 - yaffs_UnpackTags2TagsPart(tags, pt2tp);
18217 + pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->data_bytes_per_chunk];
18218 + yaffs_unpack_tags2tags_part(tags, pt2tp);
18222 - memcpy(&pt, dev->spareBuffer, sizeof(pt));
18223 - yaffs_UnpackTags2(tags, &pt);
18224 + memcpy(packed_tags_ptr, yaffs_dev_to_lc(dev)->spareBuffer, packed_tags_size);
18225 + yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
18230 - yaffs_ReleaseTempBuffer(dev, data, __LINE__);
18231 + yaffs_release_temp_buffer(dev, data, __LINE__);
18233 - if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
18234 - tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
18235 + if (tags && retval == -EBADMSG && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
18236 + tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
18237 + dev->n_ecc_unfixed++;
18239 + if(tags && retval == -EUCLEAN && tags->ecc_result == YAFFS_ECC_RESULT_NO_ERROR) {
18240 + tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
18241 + dev->n_ecc_fixed++;
18249 -int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
18250 +int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no)
18252 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18253 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
18256 - (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
18257 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), block_no));
18260 mtd->block_markbad(mtd,
18261 - blockNo * dev->nChunksPerBlock *
18262 - dev->totalBytesPerChunk);
18263 + block_no * dev->param.chunks_per_block *
18264 + dev->param.total_bytes_per_chunk);
18268 @@ -201,41 +212,41 @@ int nandmtd2_MarkNANDBlockBad(struct yaf
18272 -int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
18273 - yaffs_BlockState *state, __u32 *sequenceNumber)
18274 +int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
18275 + yaffs_block_state_t *state, __u32 *seq_number)
18277 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18278 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
18282 - (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
18283 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), block_no));
18285 mtd->block_isbad(mtd,
18286 - blockNo * dev->nChunksPerBlock *
18287 - dev->totalBytesPerChunk);
18288 + block_no * dev->param.chunks_per_block *
18289 + dev->param.total_bytes_per_chunk);
18292 T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
18294 *state = YAFFS_BLOCK_STATE_DEAD;
18295 - *sequenceNumber = 0;
18298 - yaffs_ExtendedTags t;
18299 + yaffs_ext_tags t;
18300 nandmtd2_ReadChunkWithTagsFromNAND(dev,
18302 - dev->nChunksPerBlock, NULL,
18304 + dev->param.chunks_per_block, NULL,
18307 - if (t.chunkUsed) {
18308 - *sequenceNumber = t.sequenceNumber;
18309 + if (t.chunk_used) {
18310 + *seq_number = t.seq_number;
18311 *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
18313 - *sequenceNumber = 0;
18315 *state = YAFFS_BLOCK_STATE_EMPTY;
18319 - (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
18320 + (TSTR("block is bad seq %d state %d" TENDSTR), *seq_number,
18324 --- a/fs/yaffs2/yaffs_mtdif2.h
18325 +++ b/fs/yaffs2/yaffs_mtdif2.h
18328 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
18330 - * Copyright (C) 2002-2007 Aleph One Ltd.
18331 + * Copyright (C) 2002-2010 Aleph One Ltd.
18332 * for Toby Churchill Ltd and Brightstar Engineering
18334 * Created by Charles Manning <charles@aleph1.co.uk>
18335 @@ -17,13 +17,13 @@
18336 #define __YAFFS_MTDIF2_H__
18338 #include "yaffs_guts.h"
18339 -int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
18340 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_dev_t *dev, int nand_chunk,
18342 - const yaffs_ExtendedTags *tags);
18343 -int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
18344 - __u8 *data, yaffs_ExtendedTags *tags);
18345 -int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
18346 -int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
18347 - yaffs_BlockState *state, __u32 *sequenceNumber);
18348 + const yaffs_ext_tags *tags);
18349 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_dev_t *dev, int nand_chunk,
18350 + __u8 *data, yaffs_ext_tags *tags);
18351 +int nandmtd2_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no);
18352 +int nandmtd2_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
18353 + yaffs_block_state_t *state, __u32 *seq_number);
18356 --- a/fs/yaffs2/yaffs_mtdif.c
18357 +++ b/fs/yaffs2/yaffs_mtdif.c
18360 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
18362 - * Copyright (C) 2002-2007 Aleph One Ltd.
18363 + * Copyright (C) 2002-2010 Aleph One Ltd.
18364 * for Toby Churchill Ltd and Brightstar Engineering
18366 * Created by Charles Manning <charles@aleph1.co.uk>
18368 * published by the Free Software Foundation.
18371 -const char *yaffs_mtdif_c_version =
18372 - "$Id: yaffs_mtdif.c,v 1.22 2009-03-06 17:20:51 wookey Exp $";
18374 #include "yportenv.h"
18377 @@ -24,208 +21,26 @@ const char *yaffs_mtdif_c_version =
18378 #include "linux/time.h"
18379 #include "linux/mtd/nand.h"
18381 -#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
18382 -static struct nand_oobinfo yaffs_oobinfo = {
18385 - .eccpos = {8, 9, 10, 13, 14, 15}
18388 -static struct nand_oobinfo yaffs_noeccinfo = {
18393 -#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18394 -static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
18396 - oob[0] = spare->tagByte0;
18397 - oob[1] = spare->tagByte1;
18398 - oob[2] = spare->tagByte2;
18399 - oob[3] = spare->tagByte3;
18400 - oob[4] = spare->tagByte4;
18401 - oob[5] = spare->tagByte5 & 0x3f;
18402 - oob[5] |= spare->blockStatus == 'Y' ? 0 : 0x80;
18403 - oob[5] |= spare->pageStatus == 0 ? 0 : 0x40;
18404 - oob[6] = spare->tagByte6;
18405 - oob[7] = spare->tagByte7;
18408 -static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
18410 - struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
18411 - spare->tagByte0 = oob[0];
18412 - spare->tagByte1 = oob[1];
18413 - spare->tagByte2 = oob[2];
18414 - spare->tagByte3 = oob[3];
18415 - spare->tagByte4 = oob[4];
18416 - spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
18417 - spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
18418 - spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
18419 - spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
18420 - spare->tagByte6 = oob[6];
18421 - spare->tagByte7 = oob[7];
18422 - spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
18424 - nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
18428 -int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
18429 - const __u8 *data, const yaffs_Spare *spare)
18431 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18432 -#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18433 - struct mtd_oob_ops ops;
18438 - loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
18439 -#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18440 - __u8 spareAsBytes[8]; /* OOB */
18442 - if (data && !spare)
18443 - retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
18445 - else if (spare) {
18446 - if (dev->useNANDECC) {
18447 - translate_spare2oob(spare, spareAsBytes);
18448 - ops.mode = MTD_OOB_AUTO;
18449 - ops.ooblen = 8; /* temp hack */
18451 - ops.mode = MTD_OOB_RAW;
18452 - ops.ooblen = YAFFS_BYTES_PER_SPARE;
18454 - ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
18455 - ops.datbuf = (u8 *)data;
18457 - ops.oobbuf = spareAsBytes;
18458 - retval = mtd->write_oob(mtd, addr, &ops);
18461 - __u8 *spareAsBytes = (__u8 *) spare;
18463 - if (data && spare) {
18464 - if (dev->useNANDECC)
18466 - mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
18467 - &dummy, data, spareAsBytes,
18471 - mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
18472 - &dummy, data, spareAsBytes,
18473 - &yaffs_noeccinfo);
18477 - mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
18481 - mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
18482 - &dummy, spareAsBytes);
18489 - return YAFFS_FAIL;
18492 -int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
18493 - yaffs_Spare *spare)
18495 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18496 -#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18497 - struct mtd_oob_ops ops;
18501 +#include "yaffs_linux.h"
18503 - loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
18504 -#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
18505 - __u8 spareAsBytes[8]; /* OOB */
18507 - if (data && !spare)
18508 - retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
18510 - else if (spare) {
18511 - if (dev->useNANDECC) {
18512 - ops.mode = MTD_OOB_AUTO;
18513 - ops.ooblen = 8; /* temp hack */
18515 - ops.mode = MTD_OOB_RAW;
18516 - ops.ooblen = YAFFS_BYTES_PER_SPARE;
18518 - ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
18519 - ops.datbuf = data;
18521 - ops.oobbuf = spareAsBytes;
18522 - retval = mtd->read_oob(mtd, addr, &ops);
18523 - if (dev->useNANDECC)
18524 - translate_oob2spare(spare, spareAsBytes);
18527 - __u8 *spareAsBytes = (__u8 *) spare;
18529 - if (data && spare) {
18530 - if (dev->useNANDECC) {
18531 - /* Careful, this call adds 2 ints */
18532 - /* to the end of the spare data. Calling function */
18533 - /* should allocate enough memory for spare, */
18534 - /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
18536 - mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
18537 - &dummy, data, spareAsBytes,
18541 - mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
18542 - &dummy, data, spareAsBytes,
18543 - &yaffs_noeccinfo);
18548 - mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
18552 - mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
18553 - &dummy, spareAsBytes);
18560 - return YAFFS_FAIL;
18563 -int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
18564 +int nandmtd_EraseBlockInNAND(yaffs_dev_t *dev, int blockNumber)
18566 - struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
18567 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
18569 - ((loff_t) blockNumber) * dev->nDataBytesPerChunk
18570 - * dev->nChunksPerBlock;
18571 + ((loff_t) blockNumber) * dev->param.total_bytes_per_chunk
18572 + * dev->param.chunks_per_block;
18573 struct erase_info ei;
18579 - ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
18580 + ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
18583 ei.callback = NULL;
18584 ei.priv = (u_long) dev;
18586 - /* Todo finish off the ei if required */
18588 - sema_init(&dev->sem, 0);
18590 retval = mtd->erase(mtd, &ei);
18593 @@ -234,7 +49,7 @@ int nandmtd_EraseBlockInNAND(yaffs_Devic
18597 -int nandmtd_InitialiseNAND(yaffs_Device *dev)
18598 +int nandmtd_InitialiseNAND(yaffs_dev_t *dev)
18602 --- a/fs/yaffs2/yaffs_mtdif.h
18603 +++ b/fs/yaffs2/yaffs_mtdif.h
18606 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
18608 - * Copyright (C) 2002-2007 Aleph One Ltd.
18609 + * Copyright (C) 2002-2010 Aleph One Ltd.
18610 * for Toby Churchill Ltd and Brightstar Engineering
18612 * Created by Charles Manning <charles@aleph1.co.uk>
18614 extern struct nand_oobinfo yaffs_oobinfo;
18615 extern struct nand_oobinfo yaffs_noeccinfo;
18618 -int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
18619 - const __u8 *data, const yaffs_Spare *spare);
18620 -int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
18621 - yaffs_Spare *spare);
18622 -int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber);
18623 -int nandmtd_InitialiseNAND(yaffs_Device *dev);
18624 +int nandmtd_EraseBlockInNAND(yaffs_dev_t *dev, int blockNumber);
18625 +int nandmtd_InitialiseNAND(yaffs_dev_t *dev);
18628 +++ b/fs/yaffs2/yaffs_nameval.c
18631 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
18633 + * Copyright (C) 2002-2010 Aleph One Ltd.
18634 + * for Toby Churchill Ltd and Brightstar Engineering
18636 + * Created by Charles Manning <charles@aleph1.co.uk>
18638 + * This program is free software; you can redistribute it and/or modify
18639 + * it under the terms of the GNU General Public License version 2 as
18640 + * published by the Free Software Foundation.
18644 + * This simple implementation of a name-value store assumes a small number of values and fits
18645 + * into a small finite buffer.
18647 + * Each attribute is stored as a record:
18648 + * sizeof(int) bytes record size.
18649 + * strnlen+1 bytes name null terminated.
18652 + * total size stored in record size
18654 + * This code has not been tested with unicode yet.
18658 +#include "yaffs_nameval.h"
18660 +#include "yportenv.h"
18662 +static int nval_find(const char *xb, int xb_size, const YCHAR *name,
18668 + memcpy(&size,xb,sizeof(int));
18669 + while(size > 0 && (size < xb_size) && (pos + size < xb_size)){
18670 + if(yaffs_strncmp((YCHAR *)(xb+pos+sizeof(int)),name,size) == 0){
18672 + *exist_size = size;
18676 + if(pos < xb_size -sizeof(int))
18677 + memcpy(&size,xb + pos,sizeof(int));
18686 +static int nval_used(const char *xb, int xb_size)
18691 + memcpy(&size,xb + pos,sizeof(int));
18692 + while(size > 0 && (size < xb_size) && (pos + size < xb_size)){
18694 + if(pos < xb_size -sizeof(int))
18695 + memcpy(&size,xb + pos,sizeof(int));
18702 +int nval_del(char *xb, int xb_size, const YCHAR *name)
18704 + int pos = nval_find(xb, xb_size, name, NULL);
18707 + if(pos >= 0 && pos < xb_size){
18708 + /* Find size, shift rest over this record, then zero out the rest of buffer */
18709 + memcpy(&size,xb+pos,sizeof(int));
18710 + memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
18711 + memset(xb + (xb_size - size),0,size);
18717 +int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf, int bsize, int flags)
18720 + int namelen = yaffs_strnlen(name,xb_size);
18722 + int size_exist = 0;
18726 + pos = nval_find(xb,xb_size,name, &size_exist);
18728 + if(flags & XATTR_CREATE && pos >= 0)
18730 + if(flags & XATTR_REPLACE && pos < 0)
18733 + start = nval_used(xb,xb_size);
18734 + space = xb_size - start + size_exist;
18736 + reclen = (sizeof(int) + namelen + 1 + bsize);
18738 + if(reclen > space)
18742 + nval_del(xb,xb_size,name);
18743 + start = nval_used(xb, xb_size);
18748 + memcpy(xb + pos,&reclen,sizeof(int));
18749 + pos +=sizeof(int);
18750 + yaffs_strncpy((YCHAR *)(xb + pos), name, reclen);
18751 + pos+= (namelen+1);
18752 + memcpy(xb + pos,buf,bsize);
18756 +int nval_get(const char *xb, int xb_size, const YCHAR *name, char *buf, int bsize)
18758 + int pos = nval_find(xb,xb_size,name,NULL);
18761 + if(pos >= 0 && pos< xb_size){
18763 + memcpy(&size,xb +pos,sizeof(int));
18764 + pos+=sizeof(int); /* advance past record length */
18765 + size -= sizeof(int);
18767 + /* Advance over name string */
18768 + while(xb[pos] && size > 0 && pos < xb_size){
18772 + /*Advance over NUL */
18776 + if(size <= bsize){
18777 + memcpy(buf,xb + pos,size);
18788 +int nval_list(const char *xb, int xb_size, char *buf, int bsize)
18796 + memcpy(&size,xb + pos,sizeof(int));
18797 + while(size > sizeof(int) && size <= xb_size && (pos + size) < xb_size && !filled){
18798 + pos+= sizeof(int);
18799 + size-=sizeof(int);
18800 + name_len = yaffs_strnlen((YCHAR *)(xb + pos), size);
18801 + if(ncopied + name_len + 1 < bsize){
18802 + memcpy(buf,xb+pos,name_len * sizeof(YCHAR));
18806 + if(sizeof(YCHAR) > 1){
18810 + ncopied += (name_len+1);
18814 + if(pos < xb_size -sizeof(int))
18815 + memcpy(&size,xb + pos,sizeof(int));
18823 +int nval_hasvalues(const char *xb, int xb_size)
18825 + return nval_used(xb, xb_size) > 0;
18828 +++ b/fs/yaffs2/yaffs_nameval.h
18831 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
18833 + * Copyright (C) 2002-2010 Aleph One Ltd.
18834 + * for Toby Churchill Ltd and Brightstar Engineering
18836 + * Created by Charles Manning <charles@aleph1.co.uk>
18838 + * This program is free software; you can redistribute it and/or modify
18839 + * it under the terms of the GNU Lesser General Public License version 2.1 as
18840 + * published by the Free Software Foundation.
18842 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
18844 +#ifndef __NAMEVAL_H__
18845 +#define __NAMEVAL_H__
18847 +#include "yportenv.h"
18849 +int nval_del(char *xb, int xb_size, const YCHAR *name);
18850 +int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf, int bsize, int flags);
18851 +int nval_get(const char *xb, int xb_size, const YCHAR *name, char *buf, int bsize);
18852 +int nval_list(const char *xb, int xb_size, char *buf, int bsize);
18853 +int nval_hasvalues(const char *xb, int xb_size);
18855 --- a/fs/yaffs2/yaffs_nand.c
18856 +++ b/fs/yaffs2/yaffs_nand.c
18859 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
18861 - * Copyright (C) 2002-2007 Aleph One Ltd.
18862 + * Copyright (C) 2002-2010 Aleph One Ltd.
18863 * for Toby Churchill Ltd and Brightstar Engineering
18865 * Created by Charles Manning <charles@aleph1.co.uk>
18866 @@ -11,124 +11,129 @@
18867 * published by the Free Software Foundation.
18870 -const char *yaffs_nand_c_version =
18871 - "$Id: yaffs_nand.c,v 1.10 2009-03-06 17:20:54 wookey Exp $";
18873 #include "yaffs_nand.h"
18874 #include "yaffs_tagscompat.h"
18875 #include "yaffs_tagsvalidity.h"
18877 #include "yaffs_getblockinfo.h"
18879 -int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
18880 +int yaffs_rd_chunk_tags_nand(yaffs_dev_t *dev, int nand_chunk,
18882 - yaffs_ExtendedTags *tags)
18883 + yaffs_ext_tags *tags)
18886 - yaffs_ExtendedTags localTags;
18887 + yaffs_ext_tags localTags;
18889 + int realignedChunkInNAND = nand_chunk - dev->chunk_offset;
18891 - int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
18892 + dev->n_page_reads++;
18894 /* If there are no tags provided, use local tags to get prioritised gc working */
18898 - if (dev->readChunkWithTagsFromNAND)
18899 - result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
18900 + if (dev->param.read_chunk_tags_fn)
18901 + result = dev->param.read_chunk_tags_fn(dev, realignedChunkInNAND, buffer,
18904 - result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
18905 + result = yaffs_tags_compat_rd(dev,
18906 realignedChunkInNAND,
18910 - tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR) {
18911 + tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
18913 - yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
18914 - yaffs_HandleChunkError(dev, bi);
18915 + yaffs_block_info_t *bi;
18916 + bi = yaffs_get_block_info(dev, nand_chunk/dev->param.chunks_per_block);
18917 + yaffs_handle_chunk_error(dev, bi);
18923 -int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
18925 +int yaffs_wr_chunk_tags_nand(yaffs_dev_t *dev,
18927 const __u8 *buffer,
18928 - yaffs_ExtendedTags *tags)
18929 + yaffs_ext_tags *tags)
18931 - chunkInNAND -= dev->chunkOffset;
18933 + dev->n_page_writes++;
18935 + nand_chunk -= dev->chunk_offset;
18939 - tags->sequenceNumber = dev->sequenceNumber;
18940 - tags->chunkUsed = 1;
18941 - if (!yaffs_ValidateTags(tags)) {
18942 + tags->seq_number = dev->seq_number;
18943 + tags->chunk_used = 1;
18944 + if (!yaffs_validate_tags(tags)) {
18945 T(YAFFS_TRACE_ERROR,
18946 (TSTR("Writing uninitialised tags" TENDSTR)));
18949 T(YAFFS_TRACE_WRITE,
18950 - (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
18951 - tags->objectId, tags->chunkId));
18952 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), nand_chunk,
18953 + tags->obj_id, tags->chunk_id));
18955 T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
18959 - if (dev->writeChunkWithTagsToNAND)
18960 - return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
18961 + if (dev->param.write_chunk_tags_fn)
18962 + return dev->param.write_chunk_tags_fn(dev, nand_chunk, buffer,
18965 - return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
18967 + return yaffs_tags_compat_wr(dev,
18973 -int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
18974 +int yaffs_mark_bad(yaffs_dev_t *dev, int block_no)
18976 - blockNo -= dev->blockOffset;
18977 + block_no -= dev->block_offset;
18981 - if (dev->markNANDBlockBad)
18982 - return dev->markNANDBlockBad(dev, blockNo);
18983 + if (dev->param.bad_block_fn)
18984 + return dev->param.bad_block_fn(dev, block_no);
18986 - return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
18987 + return yaffs_tags_compat_mark_bad(dev, block_no);
18990 -int yaffs_QueryInitialBlockState(yaffs_Device *dev,
18992 - yaffs_BlockState *state,
18993 - __u32 *sequenceNumber)
18994 +int yaffs_query_init_block_state(yaffs_dev_t *dev,
18996 + yaffs_block_state_t *state,
18997 + __u32 *seq_number)
18999 - blockNo -= dev->blockOffset;
19000 + block_no -= dev->block_offset;
19002 - if (dev->queryNANDBlock)
19003 - return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
19004 + if (dev->param.query_block_fn)
19005 + return dev->param.query_block_fn(dev, block_no, state, seq_number);
19007 - return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
19008 + return yaffs_tags_compat_query_block(dev, block_no,
19015 -int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
19017 +int yaffs_erase_block(struct yaffs_dev_s *dev,
19022 - blockInNAND -= dev->blockOffset;
19023 + flash_block -= dev->block_offset;
19025 + dev->n_erasures++;
19027 - dev->nBlockErasures++;
19028 - result = dev->eraseBlockInNAND(dev, blockInNAND);
19029 + result = dev->param.erase_fn(dev, flash_block);
19034 -int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
19035 +int yaffs_init_nand(struct yaffs_dev_s *dev)
19037 - return dev->initialiseNAND(dev);
19038 + if(dev->param.initialise_flash_fn)
19039 + return dev->param.initialise_flash_fn(dev);
19044 --- a/fs/yaffs2/yaffs_nandemul2k.h
19045 +++ b/fs/yaffs2/yaffs_nandemul2k.h
19048 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
19050 - * Copyright (C) 2002-2007 Aleph One Ltd.
19051 + * Copyright (C) 2002-2010 Aleph One Ltd.
19052 * for Toby Churchill Ltd and Brightstar Engineering
19054 * Created by Charles Manning <charles@aleph1.co.uk>
19055 @@ -20,18 +20,18 @@
19057 #include "yaffs_guts.h"
19059 -int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
19060 - int chunkInNAND, const __u8 *data,
19061 - const yaffs_ExtendedTags *tags);
19062 -int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
19063 - int chunkInNAND, __u8 *data,
19064 - yaffs_ExtendedTags *tags);
19065 -int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
19066 -int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
19067 - yaffs_BlockState *state, __u32 *sequenceNumber);
19068 -int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
19069 - int blockInNAND);
19070 -int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
19071 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_dev_s *dev,
19072 + int nand_chunk, const __u8 *data,
19073 + const yaffs_ext_tags *tags);
19074 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_dev_s *dev,
19075 + int nand_chunk, __u8 *data,
19076 + yaffs_ext_tags *tags);
19077 +int nandemul2k_MarkNANDBlockBad(struct yaffs_dev_s *dev, int block_no);
19078 +int nandemul2k_QueryNANDBlock(struct yaffs_dev_s *dev, int block_no,
19079 + yaffs_block_state_t *state, __u32 *seq_number);
19080 +int nandemul2k_EraseBlockInNAND(struct yaffs_dev_s *dev,
19081 + int flash_block);
19082 +int nandemul2k_InitialiseNAND(struct yaffs_dev_s *dev);
19083 int nandemul2k_GetBytesPerChunk(void);
19084 int nandemul2k_GetChunksPerBlock(void);
19085 int nandemul2k_GetNumberOfBlocks(void);
19086 --- a/fs/yaffs2/yaffs_nand.h
19087 +++ b/fs/yaffs2/yaffs_nand.h
19090 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
19092 - * Copyright (C) 2002-2007 Aleph One Ltd.
19093 + * Copyright (C) 2002-2010 Aleph One Ltd.
19094 * for Toby Churchill Ltd and Brightstar Engineering
19096 * Created by Charles Manning <charles@aleph1.co.uk>
19097 @@ -19,26 +19,26 @@
19101 -int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
19102 +int yaffs_rd_chunk_tags_nand(yaffs_dev_t *dev, int nand_chunk,
19104 - yaffs_ExtendedTags *tags);
19105 + yaffs_ext_tags *tags);
19107 -int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
19109 +int yaffs_wr_chunk_tags_nand(yaffs_dev_t *dev,
19111 const __u8 *buffer,
19112 - yaffs_ExtendedTags *tags);
19113 + yaffs_ext_tags *tags);
19115 -int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
19116 +int yaffs_mark_bad(yaffs_dev_t *dev, int block_no);
19118 -int yaffs_QueryInitialBlockState(yaffs_Device *dev,
19120 - yaffs_BlockState *state,
19121 - unsigned *sequenceNumber);
19122 +int yaffs_query_init_block_state(yaffs_dev_t *dev,
19124 + yaffs_block_state_t *state,
19125 + unsigned *seq_number);
19127 -int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
19128 - int blockInNAND);
19129 +int yaffs_erase_block(struct yaffs_dev_s *dev,
19130 + int flash_block);
19132 -int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
19133 +int yaffs_init_nand(struct yaffs_dev_s *dev);
19137 --- a/fs/yaffs2/yaffs_packedtags1.c
19138 +++ b/fs/yaffs2/yaffs_packedtags1.c
19141 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
19143 - * Copyright (C) 2002-2007 Aleph One Ltd.
19144 + * Copyright (C) 2002-2010 Aleph One Ltd.
19145 * for Toby Churchill Ltd and Brightstar Engineering
19147 * Created by Charles Manning <charles@aleph1.co.uk>
19148 @@ -14,37 +14,37 @@
19149 #include "yaffs_packedtags1.h"
19150 #include "yportenv.h"
19152 -void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t)
19153 +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ext_tags *t)
19155 - pt->chunkId = t->chunkId;
19156 - pt->serialNumber = t->serialNumber;
19157 - pt->byteCount = t->byteCount;
19158 - pt->objectId = t->objectId;
19159 + pt->chunk_id = t->chunk_id;
19160 + pt->serial_number = t->serial_number;
19161 + pt->n_bytes = t->n_bytes;
19162 + pt->obj_id = t->obj_id;
19164 - pt->deleted = (t->chunkDeleted) ? 0 : 1;
19165 + pt->deleted = (t->is_deleted) ? 0 : 1;
19166 pt->unusedStuff = 0;
19167 pt->shouldBeFF = 0xFFFFFFFF;
19171 -void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt)
19172 +void yaffs_unpack_tags1(yaffs_ext_tags *t, const yaffs_PackedTags1 *pt)
19174 static const __u8 allFF[] =
19175 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
19178 if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
19180 + t->block_bad = 0;
19181 if (pt->shouldBeFF != 0xFFFFFFFF)
19183 - t->chunkUsed = 1;
19184 - t->objectId = pt->objectId;
19185 - t->chunkId = pt->chunkId;
19186 - t->byteCount = pt->byteCount;
19187 - t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
19188 - t->chunkDeleted = (pt->deleted) ? 0 : 1;
19189 - t->serialNumber = pt->serialNumber;
19190 + t->block_bad = 1;
19191 + t->chunk_used = 1;
19192 + t->obj_id = pt->obj_id;
19193 + t->chunk_id = pt->chunk_id;
19194 + t->n_bytes = pt->n_bytes;
19195 + t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
19196 + t->is_deleted = (pt->deleted) ? 0 : 1;
19197 + t->serial_number = pt->serial_number;
19199 - memset(t, 0, sizeof(yaffs_ExtendedTags));
19200 + memset(t, 0, sizeof(yaffs_ext_tags));
19203 --- a/fs/yaffs2/yaffs_packedtags1.h
19204 +++ b/fs/yaffs2/yaffs_packedtags1.h
19207 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
19209 - * Copyright (C) 2002-2007 Aleph One Ltd.
19210 + * Copyright (C) 2002-2010 Aleph One Ltd.
19211 * for Toby Churchill Ltd and Brightstar Engineering
19213 * Created by Charles Manning <charles@aleph1.co.uk>
19214 @@ -21,10 +21,10 @@
19215 #include "yaffs_guts.h"
19218 - unsigned chunkId:20;
19219 - unsigned serialNumber:2;
19220 - unsigned byteCount:10;
19221 - unsigned objectId:18;
19222 + unsigned chunk_id:20;
19223 + unsigned serial_number:2;
19224 + unsigned n_bytes:10;
19225 + unsigned obj_id:18;
19227 unsigned deleted:1;
19228 unsigned unusedStuff:1;
19229 @@ -32,6 +32,6 @@ typedef struct {
19231 } yaffs_PackedTags1;
19233 -void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t);
19234 -void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt);
19235 +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ext_tags *t);
19236 +void yaffs_unpack_tags1(yaffs_ext_tags *t, const yaffs_PackedTags1 *pt);
19238 --- a/fs/yaffs2/yaffs_packedtags2.c
19239 +++ b/fs/yaffs2/yaffs_packedtags2.c
19242 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
19244 - * Copyright (C) 2002-2007 Aleph One Ltd.
19245 + * Copyright (C) 2002-2010 Aleph One Ltd.
19246 * for Toby Churchill Ltd and Brightstar Engineering
19248 * Created by Charles Manning <charles@aleph1.co.uk>
19251 #include "yaffs_packedtags2.h"
19252 #include "yportenv.h"
19253 +#include "yaffs_trace.h"
19254 #include "yaffs_tagsvalidity.h"
19256 /* This code packs a set of extended tags into a binary structure for
19258 * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
19261 -/* Extra flags applied to chunkId */
19262 +/* Extra flags applied to chunk_id */
19264 #define EXTRA_HEADER_INFO_FLAG 0x80000000
19265 #define EXTRA_SHRINK_FLAG 0x40000000
19266 @@ -42,53 +43,53 @@ static void yaffs_DumpPackedTags2TagsPar
19269 (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
19270 - ptt->objectId, ptt->chunkId, ptt->byteCount,
19271 - ptt->sequenceNumber));
19272 + ptt->obj_id, ptt->chunk_id, ptt->n_bytes,
19273 + ptt->seq_number));
19275 static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
19277 yaffs_DumpPackedTags2TagsPart(&pt->t);
19280 -static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
19281 +static void yaffs_DumpTags2(const yaffs_ext_tags *t)
19285 ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
19286 - TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
19287 - t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
19288 - t->sequenceNumber));
19289 + TENDSTR), t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
19290 + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
19295 void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
19296 - const yaffs_ExtendedTags *t)
19297 + const yaffs_ext_tags *t)
19299 - ptt->chunkId = t->chunkId;
19300 - ptt->sequenceNumber = t->sequenceNumber;
19301 - ptt->byteCount = t->byteCount;
19302 - ptt->objectId = t->objectId;
19303 + ptt->chunk_id = t->chunk_id;
19304 + ptt->seq_number = t->seq_number;
19305 + ptt->n_bytes = t->n_bytes;
19306 + ptt->obj_id = t->obj_id;
19308 - if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
19309 + if (t->chunk_id == 0 && t->extra_available) {
19310 /* Store the extra header info instead */
19311 - /* We save the parent object in the chunkId */
19312 - ptt->chunkId = EXTRA_HEADER_INFO_FLAG
19313 - | t->extraParentObjectId;
19314 - if (t->extraIsShrinkHeader)
19315 - ptt->chunkId |= EXTRA_SHRINK_FLAG;
19316 - if (t->extraShadows)
19317 - ptt->chunkId |= EXTRA_SHADOWS_FLAG;
19319 - ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
19321 - (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
19323 - if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
19324 - ptt->byteCount = t->extraEquivalentObjectId;
19325 - else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
19326 - ptt->byteCount = t->extraFileLength;
19327 + /* We save the parent object in the chunk_id */
19328 + ptt->chunk_id = EXTRA_HEADER_INFO_FLAG
19329 + | t->extra_parent_id;
19330 + if (t->extra_is_shrink)
19331 + ptt->chunk_id |= EXTRA_SHRINK_FLAG;
19332 + if (t->extra_shadows)
19333 + ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
19335 + ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
19337 + (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
19339 + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
19340 + ptt->n_bytes = t->extra_equiv_id;
19341 + else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
19342 + ptt->n_bytes = t->extra_length;
19344 - ptt->byteCount = 0;
19345 + ptt->n_bytes = 0;
19348 yaffs_DumpPackedTags2TagsPart(ptt);
19349 @@ -96,59 +97,56 @@ void yaffs_PackTags2TagsPart(yaffs_Packe
19353 -void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
19354 +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ext_tags *t, int tagsECC)
19356 yaffs_PackTags2TagsPart(&pt->t, t);
19358 -#ifndef YAFFS_IGNORE_TAGS_ECC
19360 - yaffs_ECCCalculateOther((unsigned char *)&pt->t,
19362 + yaffs_ecc_calc_other((unsigned char *)&pt->t,
19363 sizeof(yaffs_PackedTags2TagsPart),
19370 -void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
19371 +void yaffs_unpack_tags2tags_part(yaffs_ext_tags *t,
19372 yaffs_PackedTags2TagsPart *ptt)
19375 - memset(t, 0, sizeof(yaffs_ExtendedTags));
19376 + memset(t, 0, sizeof(yaffs_ext_tags));
19378 - yaffs_InitialiseTags(t);
19379 + yaffs_init_tags(t);
19381 - if (ptt->sequenceNumber != 0xFFFFFFFF) {
19383 - t->chunkUsed = 1;
19384 - t->objectId = ptt->objectId;
19385 - t->chunkId = ptt->chunkId;
19386 - t->byteCount = ptt->byteCount;
19387 - t->chunkDeleted = 0;
19388 - t->serialNumber = 0;
19389 - t->sequenceNumber = ptt->sequenceNumber;
19390 + if (ptt->seq_number != 0xFFFFFFFF) {
19391 + t->block_bad = 0;
19392 + t->chunk_used = 1;
19393 + t->obj_id = ptt->obj_id;
19394 + t->chunk_id = ptt->chunk_id;
19395 + t->n_bytes = ptt->n_bytes;
19396 + t->is_deleted = 0;
19397 + t->serial_number = 0;
19398 + t->seq_number = ptt->seq_number;
19400 /* Do extra header info stuff */
19402 - if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
19404 - t->byteCount = 0;
19406 - t->extraHeaderInfoAvailable = 1;
19407 - t->extraParentObjectId =
19408 - ptt->chunkId & (~(ALL_EXTRA_FLAGS));
19409 - t->extraIsShrinkHeader =
19410 - (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
19411 - t->extraShadows =
19412 - (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
19413 - t->extraObjectType =
19414 - ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
19415 - t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
19416 + if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
19420 + t->extra_available = 1;
19421 + t->extra_parent_id =
19422 + ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
19423 + t->extra_is_shrink =
19424 + (ptt->chunk_id & EXTRA_SHRINK_FLAG) ? 1 : 0;
19425 + t->extra_shadows =
19426 + (ptt->chunk_id & EXTRA_SHADOWS_FLAG) ? 1 : 0;
19427 + t->extra_obj_type =
19428 + ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
19429 + t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
19431 - if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
19432 - t->extraEquivalentObjectId = ptt->byteCount;
19433 + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
19434 + t->extra_equiv_id = ptt->n_bytes;
19436 - t->extraFileLength = ptt->byteCount;
19437 + t->extra_length = ptt->n_bytes;
19441 @@ -158,49 +156,43 @@ void yaffs_UnpackTags2TagsPart(yaffs_Ext
19445 -void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
19446 +void yaffs_unpack_tags2(yaffs_ext_tags *t, yaffs_PackedTags2 *pt, int tagsECC)
19449 - yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
19450 + yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
19452 - if (pt->t.sequenceNumber != 0xFFFFFFFF) {
19453 - /* Page is in use */
19454 -#ifndef YAFFS_IGNORE_TAGS_ECC
19456 - yaffs_ECCOther ecc;
19458 - yaffs_ECCCalculateOther((unsigned char *)&pt->t,
19460 - (yaffs_PackedTags2TagsPart),
19463 - yaffs_ECCCorrectOther((unsigned char *)&pt->t,
19465 - (yaffs_PackedTags2TagsPart),
19467 - switch (result) {
19468 + if (pt->t.seq_number != 0xFFFFFFFF &&
19470 + /* Chunk is in use and we need to do ECC */
19472 + yaffs_ECCOther ecc;
19474 + yaffs_ecc_calc_other((unsigned char *)&pt->t,
19475 + sizeof(yaffs_PackedTags2TagsPart),
19477 + result = yaffs_ecc_correct_other((unsigned char *)&pt->t,
19478 + sizeof(yaffs_PackedTags2TagsPart),
19480 + switch (result) {
19482 - eccResult = YAFFS_ECC_RESULT_NO_ERROR;
19483 + ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
19486 - eccResult = YAFFS_ECC_RESULT_FIXED;
19487 + ecc_result = YAFFS_ECC_RESULT_FIXED;
19490 - eccResult = YAFFS_ECC_RESULT_UNFIXED;
19491 + ecc_result = YAFFS_ECC_RESULT_UNFIXED;
19494 - eccResult = YAFFS_ECC_RESULT_UNKNOWN;
19496 + ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
19501 - yaffs_UnpackTags2TagsPart(t, &pt->t);
19502 + yaffs_unpack_tags2tags_part(t, &pt->t);
19504 - t->eccResult = eccResult;
19505 + t->ecc_result = ecc_result;
19507 yaffs_DumpPackedTags2(pt);
19508 yaffs_DumpTags2(t);
19512 --- a/fs/yaffs2/yaffs_packedtags2.h
19513 +++ b/fs/yaffs2/yaffs_packedtags2.h
19516 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
19518 - * Copyright (C) 2002-2007 Aleph One Ltd.
19519 + * Copyright (C) 2002-2010 Aleph One Ltd.
19520 * for Toby Churchill Ltd and Brightstar Engineering
19522 * Created by Charles Manning <charles@aleph1.co.uk>
19523 @@ -22,10 +22,10 @@
19524 #include "yaffs_ecc.h"
19527 - unsigned sequenceNumber;
19528 - unsigned objectId;
19529 - unsigned chunkId;
19530 - unsigned byteCount;
19531 + unsigned seq_number;
19533 + unsigned chunk_id;
19534 + unsigned n_bytes;
19535 } yaffs_PackedTags2TagsPart;
19538 @@ -34,10 +34,10 @@ typedef struct {
19539 } yaffs_PackedTags2;
19541 /* Full packed tags with ECC, used for oob tags */
19542 -void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t);
19543 -void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt);
19544 +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ext_tags *t, int tagsECC);
19545 +void yaffs_unpack_tags2(yaffs_ext_tags *t, yaffs_PackedTags2 *pt, int tagsECC);
19547 /* Only the tags part (no ECC for use with inband tags */
19548 -void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *pt, const yaffs_ExtendedTags *t);
19549 -void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t, yaffs_PackedTags2TagsPart *pt);
19550 +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *pt, const yaffs_ext_tags *t);
19551 +void yaffs_unpack_tags2tags_part(yaffs_ext_tags *t, yaffs_PackedTags2TagsPart *pt);
19553 --- a/fs/yaffs2/yaffs_qsort.h
19554 +++ b/fs/yaffs2/yaffs_qsort.h
19557 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
19559 - * Copyright (C) 2002-2007 Aleph One Ltd.
19560 + * Copyright (C) 2002-2010 Aleph One Ltd.
19561 * for Toby Churchill Ltd and Brightstar Engineering
19563 * Created by Charles Manning <charles@aleph1.co.uk>
19565 #ifndef __YAFFS_QSORT_H__
19566 #define __YAFFS_QSORT_H__
19569 +#include <linux/sort.h>
19571 +extern void yaffs_qsort(void *const base, size_t total_elems, size_t size,
19572 + int (*cmp)(const void *, const void *)){
19573 + sort(base, total_elems, size, cmp, NULL);
19578 extern void yaffs_qsort(void *const base, size_t total_elems, size_t size,
19579 int (*cmp)(const void *, const void *));
19583 --- a/fs/yaffs2/yaffs_tagscompat.c
19584 +++ b/fs/yaffs2/yaffs_tagscompat.c
19587 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
19589 - * Copyright (C) 2002-2007 Aleph One Ltd.
19590 + * Copyright (C) 2002-2010 Aleph One Ltd.
19591 * for Toby Churchill Ltd and Brightstar Engineering
19593 * Created by Charles Manning <charles@aleph1.co.uk>
19594 @@ -15,19 +15,20 @@
19595 #include "yaffs_tagscompat.h"
19596 #include "yaffs_ecc.h"
19597 #include "yaffs_getblockinfo.h"
19598 +#include "yaffs_trace.h"
19600 -static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
19601 +static void yaffs_handle_rd_data_error(yaffs_dev_t *dev, int nand_chunk);
19603 -static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
19604 -static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
19605 +static void yaffs_check_written_block(yaffs_dev_t *dev, int nand_chunk);
19606 +static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
19608 - const yaffs_Spare *spare);
19609 -static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
19610 - const yaffs_Spare *spare);
19611 -static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
19612 + const yaffs_spare *spare);
19613 +static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
19614 + const yaffs_spare *spare);
19615 +static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk);
19618 -static const char yaffs_countBitsTable[256] = {
19619 +static const char yaffs_count_bits_table[256] = {
19620 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
19621 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
19622 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
19623 @@ -46,26 +47,26 @@ static const char yaffs_countBitsTable[2
19624 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
19627 -int yaffs_CountBits(__u8 x)
19628 +int yaffs_count_bits(__u8 x)
19631 - retVal = yaffs_countBitsTable[x];
19632 + retVal = yaffs_count_bits_table[x];
19636 /********** Tags ECC calculations *********/
19638 -void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
19639 +void yaffs_calc_ecc(const __u8 *data, yaffs_spare *spare)
19641 - yaffs_ECCCalculate(data, spare->ecc1);
19642 - yaffs_ECCCalculate(&data[256], spare->ecc2);
19643 + yaffs_ecc_cacl(data, spare->ecc1);
19644 + yaffs_ecc_cacl(&data[256], spare->ecc2);
19647 -void yaffs_CalcTagsECC(yaffs_Tags *tags)
19648 +void yaffs_calc_tags_ecc(yaffs_tags_t *tags)
19650 /* Calculate an ecc */
19652 - unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
19653 + unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
19657 @@ -84,24 +85,24 @@ void yaffs_CalcTagsECC(yaffs_Tags *tags)
19661 -int yaffs_CheckECCOnTags(yaffs_Tags *tags)
19662 +int yaffs_check_tags_ecc(yaffs_tags_t *tags)
19664 unsigned ecc = tags->ecc;
19666 - yaffs_CalcTagsECC(tags);
19667 + yaffs_calc_tags_ecc(tags);
19671 if (ecc && ecc <= 64) {
19672 /* TODO: Handle the failure better. Retire? */
19673 - unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
19674 + unsigned char *b = ((yaffs_tags_union_t *) tags)->as_bytes;
19678 b[ecc / 8] ^= (1 << (ecc & 7));
19680 /* Now recvalc the ecc */
19681 - yaffs_CalcTagsECC(tags);
19682 + yaffs_calc_tags_ecc(tags);
19684 return 1; /* recovered error */
19686 @@ -115,76 +116,73 @@ int yaffs_CheckECCOnTags(yaffs_Tags *tag
19688 /********** Tags **********/
19690 -static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
19691 - yaffs_Tags *tagsPtr)
19692 +static void yaffs_load_tags_to_spare(yaffs_spare *sparePtr,
19693 + yaffs_tags_t *tagsPtr)
19695 - yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
19696 + yaffs_tags_union_t *tu = (yaffs_tags_union_t *) tagsPtr;
19698 - yaffs_CalcTagsECC(tagsPtr);
19699 + yaffs_calc_tags_ecc(tagsPtr);
19701 - sparePtr->tagByte0 = tu->asBytes[0];
19702 - sparePtr->tagByte1 = tu->asBytes[1];
19703 - sparePtr->tagByte2 = tu->asBytes[2];
19704 - sparePtr->tagByte3 = tu->asBytes[3];
19705 - sparePtr->tagByte4 = tu->asBytes[4];
19706 - sparePtr->tagByte5 = tu->asBytes[5];
19707 - sparePtr->tagByte6 = tu->asBytes[6];
19708 - sparePtr->tagByte7 = tu->asBytes[7];
19709 + sparePtr->tb0 = tu->as_bytes[0];
19710 + sparePtr->tb1 = tu->as_bytes[1];
19711 + sparePtr->tb2 = tu->as_bytes[2];
19712 + sparePtr->tb3 = tu->as_bytes[3];
19713 + sparePtr->tb4 = tu->as_bytes[4];
19714 + sparePtr->tb5 = tu->as_bytes[5];
19715 + sparePtr->tb6 = tu->as_bytes[6];
19716 + sparePtr->tb7 = tu->as_bytes[7];
19719 -static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
19720 - yaffs_Tags *tagsPtr)
19721 +static void yaffs_get_tags_from_spare(yaffs_dev_t *dev, yaffs_spare *sparePtr,
19722 + yaffs_tags_t *tagsPtr)
19724 - yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
19725 + yaffs_tags_union_t *tu = (yaffs_tags_union_t *) tagsPtr;
19728 - tu->asBytes[0] = sparePtr->tagByte0;
19729 - tu->asBytes[1] = sparePtr->tagByte1;
19730 - tu->asBytes[2] = sparePtr->tagByte2;
19731 - tu->asBytes[3] = sparePtr->tagByte3;
19732 - tu->asBytes[4] = sparePtr->tagByte4;
19733 - tu->asBytes[5] = sparePtr->tagByte5;
19734 - tu->asBytes[6] = sparePtr->tagByte6;
19735 - tu->asBytes[7] = sparePtr->tagByte7;
19736 + tu->as_bytes[0] = sparePtr->tb0;
19737 + tu->as_bytes[1] = sparePtr->tb1;
19738 + tu->as_bytes[2] = sparePtr->tb2;
19739 + tu->as_bytes[3] = sparePtr->tb3;
19740 + tu->as_bytes[4] = sparePtr->tb4;
19741 + tu->as_bytes[5] = sparePtr->tb5;
19742 + tu->as_bytes[6] = sparePtr->tb6;
19743 + tu->as_bytes[7] = sparePtr->tb7;
19745 - result = yaffs_CheckECCOnTags(tagsPtr);
19746 + result = yaffs_check_tags_ecc(tagsPtr);
19748 - dev->tagsEccFixed++;
19749 + dev->n_tags_ecc_fixed++;
19750 else if (result < 0)
19751 - dev->tagsEccUnfixed++;
19752 + dev->n_tags_ecc_unfixed++;
19755 -static void yaffs_SpareInitialise(yaffs_Spare *spare)
19756 +static void yaffs_spare_init(yaffs_spare *spare)
19758 - memset(spare, 0xFF, sizeof(yaffs_Spare));
19759 + memset(spare, 0xFF, sizeof(yaffs_spare));
19762 -static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
19763 - int chunkInNAND, const __u8 *data,
19764 - yaffs_Spare *spare)
19765 +static int yaffs_wr_nand(struct yaffs_dev_s *dev,
19766 + int nand_chunk, const __u8 *data,
19767 + yaffs_spare *spare)
19769 - if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
19770 + if (nand_chunk < dev->param.start_block * dev->param.chunks_per_block) {
19771 T(YAFFS_TRACE_ERROR,
19772 (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
19778 - dev->nPageWrites++;
19779 - return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
19780 + return dev->param.write_chunk_fn(dev, nand_chunk, data, spare);
19783 -static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
19785 +static int yaffs_rd_chunk_nand(struct yaffs_dev_s *dev,
19788 - yaffs_Spare *spare,
19789 - yaffs_ECCResult *eccResult,
19790 + yaffs_spare *spare,
19791 + yaffs_ecc_result *ecc_result,
19792 int doErrorCorrection)
19795 - yaffs_Spare localSpare;
19797 - dev->nPageReads++;
19798 + yaffs_spare localSpare;
19800 if (!spare && data) {
19801 /* If we don't have a real spare, then we use a local one. */
19802 @@ -192,107 +190,107 @@ static int yaffs_ReadChunkFromNAND(struc
19803 spare = &localSpare;
19806 - if (!dev->useNANDECC) {
19807 - retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
19808 + if (!dev->param.use_nand_ecc) {
19809 + retVal = dev->param.read_chunk_fn(dev, nand_chunk, data, spare);
19810 if (data && doErrorCorrection) {
19811 /* Do ECC correction */
19812 /* Todo handle any errors */
19813 - int eccResult1, eccResult2;
19814 + int ecc_result1, ecc_result2;
19817 - yaffs_ECCCalculate(data, calcEcc);
19819 - yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
19820 - yaffs_ECCCalculate(&data[256], calcEcc);
19822 - yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
19823 + yaffs_ecc_cacl(data, calcEcc);
19825 + yaffs_ecc_correct(data, spare->ecc1, calcEcc);
19826 + yaffs_ecc_cacl(&data[256], calcEcc);
19828 + yaffs_ecc_correct(&data[256], spare->ecc2, calcEcc);
19830 - if (eccResult1 > 0) {
19831 + if (ecc_result1 > 0) {
19832 T(YAFFS_TRACE_ERROR,
19834 ("**>>yaffs ecc error fix performed on chunk %d:0"
19835 - TENDSTR), chunkInNAND));
19837 - } else if (eccResult1 < 0) {
19838 + TENDSTR), nand_chunk));
19839 + dev->n_ecc_fixed++;
19840 + } else if (ecc_result1 < 0) {
19841 T(YAFFS_TRACE_ERROR,
19843 ("**>>yaffs ecc error unfixed on chunk %d:0"
19844 - TENDSTR), chunkInNAND));
19845 - dev->eccUnfixed++;
19846 + TENDSTR), nand_chunk));
19847 + dev->n_ecc_unfixed++;
19850 - if (eccResult2 > 0) {
19851 + if (ecc_result2 > 0) {
19852 T(YAFFS_TRACE_ERROR,
19854 ("**>>yaffs ecc error fix performed on chunk %d:1"
19855 - TENDSTR), chunkInNAND));
19857 - } else if (eccResult2 < 0) {
19858 + TENDSTR), nand_chunk));
19859 + dev->n_ecc_fixed++;
19860 + } else if (ecc_result2 < 0) {
19861 T(YAFFS_TRACE_ERROR,
19863 ("**>>yaffs ecc error unfixed on chunk %d:1"
19864 - TENDSTR), chunkInNAND));
19865 - dev->eccUnfixed++;
19866 + TENDSTR), nand_chunk));
19867 + dev->n_ecc_unfixed++;
19870 - if (eccResult1 || eccResult2) {
19871 + if (ecc_result1 || ecc_result2) {
19872 /* We had a data problem on this page */
19873 - yaffs_HandleReadDataError(dev, chunkInNAND);
19874 + yaffs_handle_rd_data_error(dev, nand_chunk);
19877 - if (eccResult1 < 0 || eccResult2 < 0)
19878 - *eccResult = YAFFS_ECC_RESULT_UNFIXED;
19879 - else if (eccResult1 > 0 || eccResult2 > 0)
19880 - *eccResult = YAFFS_ECC_RESULT_FIXED;
19881 + if (ecc_result1 < 0 || ecc_result2 < 0)
19882 + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
19883 + else if (ecc_result1 > 0 || ecc_result2 > 0)
19884 + *ecc_result = YAFFS_ECC_RESULT_FIXED;
19886 - *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
19887 + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
19890 /* Must allocate enough memory for spare+2*sizeof(int) */
19891 /* for ecc results from device. */
19892 - struct yaffs_NANDSpare nspare;
19893 + struct yaffs_nand_spare nspare;
19895 memset(&nspare, 0, sizeof(nspare));
19897 - retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
19898 - (yaffs_Spare *) &nspare);
19899 - memcpy(spare, &nspare, sizeof(yaffs_Spare));
19900 + retVal = dev->param.read_chunk_fn(dev, nand_chunk, data,
19901 + (yaffs_spare *) &nspare);
19902 + memcpy(spare, &nspare, sizeof(yaffs_spare));
19903 if (data && doErrorCorrection) {
19904 if (nspare.eccres1 > 0) {
19905 T(YAFFS_TRACE_ERROR,
19907 ("**>>mtd ecc error fix performed on chunk %d:0"
19908 - TENDSTR), chunkInNAND));
19909 + TENDSTR), nand_chunk));
19910 } else if (nspare.eccres1 < 0) {
19911 T(YAFFS_TRACE_ERROR,
19913 ("**>>mtd ecc error unfixed on chunk %d:0"
19914 - TENDSTR), chunkInNAND));
19915 + TENDSTR), nand_chunk));
19918 if (nspare.eccres2 > 0) {
19919 T(YAFFS_TRACE_ERROR,
19921 ("**>>mtd ecc error fix performed on chunk %d:1"
19922 - TENDSTR), chunkInNAND));
19923 + TENDSTR), nand_chunk));
19924 } else if (nspare.eccres2 < 0) {
19925 T(YAFFS_TRACE_ERROR,
19927 ("**>>mtd ecc error unfixed on chunk %d:1"
19928 - TENDSTR), chunkInNAND));
19929 + TENDSTR), nand_chunk));
19932 if (nspare.eccres1 || nspare.eccres2) {
19933 /* We had a data problem on this page */
19934 - yaffs_HandleReadDataError(dev, chunkInNAND);
19935 + yaffs_handle_rd_data_error(dev, nand_chunk);
19938 if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
19939 - *eccResult = YAFFS_ECC_RESULT_UNFIXED;
19940 + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
19941 else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
19942 - *eccResult = YAFFS_ECC_RESULT_FIXED;
19943 + *ecc_result = YAFFS_ECC_RESULT_FIXED;
19945 - *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
19946 + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
19950 @@ -300,17 +298,17 @@ static int yaffs_ReadChunkFromNAND(struc
19954 -static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
19956 +static int yaffs_check_chunk_erased(struct yaffs_dev_s *dev,
19960 static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
19961 static __u8 data[YAFFS_BYTES_PER_CHUNK];
19962 /* Might as well always allocate the larger size for */
19963 - /* dev->useNANDECC == true; */
19964 - static __u8 spare[sizeof(struct yaffs_NANDSpare)];
19965 + /* dev->param.use_nand_ecc == true; */
19966 + static __u8 spare[sizeof(struct yaffs_nand_spare)];
19968 - dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
19969 + dev->param.read_chunk_fn(dev, nand_chunk, data, (yaffs_spare *) spare);
19972 memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
19973 @@ -331,14 +329,14 @@ static int yaffs_CheckChunkErased(struct
19974 * Functions for robustisizing
19977 -static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
19978 +static void yaffs_handle_rd_data_error(yaffs_dev_t *dev, int nand_chunk)
19980 - int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
19981 + int flash_block = nand_chunk / dev->param.chunks_per_block;
19983 /* Mark the block for retirement */
19984 - yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
19985 + yaffs_get_block_info(dev, flash_block + dev->block_offset)->needs_retiring = 1;
19986 T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
19987 - (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
19988 + (TSTR("**>>Block %d marked for retirement" TENDSTR), flash_block));
19991 * Just do a garbage collection on the affected block
19992 @@ -348,44 +346,44 @@ static void yaffs_HandleReadDataError(ya
19996 -static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
19997 +static void yaffs_check_written_block(yaffs_dev_t *dev, int nand_chunk)
20001 -static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
20002 +static void yaffs_handle_chunk_wr_ok(yaffs_dev_t *dev, int nand_chunk,
20004 - const yaffs_Spare *spare)
20005 + const yaffs_spare *spare)
20009 -static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
20010 - const yaffs_Spare *spare)
20011 +static void yaffs_handle_chunk_update(yaffs_dev_t *dev, int nand_chunk,
20012 + const yaffs_spare *spare)
20016 -static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
20017 +static void yaffs_handle_chunk_wr_error(yaffs_dev_t *dev, int nand_chunk)
20019 - int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
20020 + int flash_block = nand_chunk / dev->param.chunks_per_block;
20022 /* Mark the block for retirement */
20023 - yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
20024 + yaffs_get_block_info(dev, flash_block)->needs_retiring = 1;
20025 /* Delete the chunk */
20026 - yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
20027 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
20030 -static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
20031 - const yaffs_Spare *s0, const yaffs_Spare *s1)
20032 +static int yaffs_verify_cmp(const __u8 *d0, const __u8 *d1,
20033 + const yaffs_spare *s0, const yaffs_spare *s1)
20036 if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
20037 - s0->tagByte0 != s1->tagByte0 ||
20038 - s0->tagByte1 != s1->tagByte1 ||
20039 - s0->tagByte2 != s1->tagByte2 ||
20040 - s0->tagByte3 != s1->tagByte3 ||
20041 - s0->tagByte4 != s1->tagByte4 ||
20042 - s0->tagByte5 != s1->tagByte5 ||
20043 - s0->tagByte6 != s1->tagByte6 ||
20044 - s0->tagByte7 != s1->tagByte7 ||
20045 + s0->tb0 != s1->tb0 ||
20046 + s0->tb1 != s1->tb1 ||
20047 + s0->tb2 != s1->tb2 ||
20048 + s0->tb3 != s1->tb3 ||
20049 + s0->tb4 != s1->tb4 ||
20050 + s0->tb5 != s1->tb5 ||
20051 + s0->tb6 != s1->tb6 ||
20052 + s0->tb7 != s1->tb7 ||
20053 s0->ecc1[0] != s1->ecc1[0] ||
20054 s0->ecc1[1] != s1->ecc1[1] ||
20055 s0->ecc1[2] != s1->ecc1[2] ||
20056 @@ -398,53 +396,53 @@ static int yaffs_VerifyCompare(const __u
20058 #endif /* NOTYET */
20060 -int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
20062 +int yaffs_tags_compat_wr(yaffs_dev_t *dev,
20065 - const yaffs_ExtendedTags *eTags)
20066 + const yaffs_ext_tags *eTags)
20068 - yaffs_Spare spare;
20070 + yaffs_spare spare;
20071 + yaffs_tags_t tags;
20073 - yaffs_SpareInitialise(&spare);
20074 + yaffs_spare_init(&spare);
20076 - if (eTags->chunkDeleted)
20077 - spare.pageStatus = 0;
20078 + if (eTags->is_deleted)
20079 + spare.page_status = 0;
20081 - tags.objectId = eTags->objectId;
20082 - tags.chunkId = eTags->chunkId;
20083 + tags.obj_id = eTags->obj_id;
20084 + tags.chunk_id = eTags->chunk_id;
20086 - tags.byteCountLSB = eTags->byteCount & 0x3ff;
20087 + tags.n_bytes_lsb = eTags->n_bytes & 0x3ff;
20089 - if (dev->nDataBytesPerChunk >= 1024)
20090 - tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
20091 + if (dev->data_bytes_per_chunk >= 1024)
20092 + tags.n_bytes_msb = (eTags->n_bytes >> 10) & 3;
20094 - tags.byteCountMSB = 3;
20095 + tags.n_bytes_msb = 3;
20098 - tags.serialNumber = eTags->serialNumber;
20099 + tags.serial_number = eTags->serial_number;
20101 - if (!dev->useNANDECC && data)
20102 - yaffs_CalcECC(data, &spare);
20103 + if (!dev->param.use_nand_ecc && data)
20104 + yaffs_calc_ecc(data, &spare);
20106 - yaffs_LoadTagsIntoSpare(&spare, &tags);
20107 + yaffs_load_tags_to_spare(&spare, &tags);
20111 - return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
20112 + return yaffs_wr_nand(dev, nand_chunk, data, &spare);
20115 -int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
20117 +int yaffs_tags_compat_rd(yaffs_dev_t *dev,
20120 - yaffs_ExtendedTags *eTags)
20121 + yaffs_ext_tags *eTags)
20124 - yaffs_Spare spare;
20126 - yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
20127 + yaffs_spare spare;
20128 + yaffs_tags_t tags;
20129 + yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
20131 - static yaffs_Spare spareFF;
20132 + static yaffs_spare spareFF;
20136 @@ -452,33 +450,33 @@ int yaffs_TagsCompatabilityReadChunkWith
20140 - if (yaffs_ReadChunkFromNAND
20141 - (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
20142 + if (yaffs_rd_chunk_nand
20143 + (dev, nand_chunk, data, &spare, &ecc_result, 1)) {
20144 /* eTags may be NULL */
20148 - (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
20149 + (yaffs_count_bits(spare.page_status) < 7) ? 1 : 0;
20151 - eTags->chunkDeleted = deleted;
20152 - eTags->eccResult = eccResult;
20153 - eTags->blockBad = 0; /* We're reading it */
20154 + eTags->is_deleted = deleted;
20155 + eTags->ecc_result = ecc_result;
20156 + eTags->block_bad = 0; /* We're reading it */
20157 /* therefore it is not a bad block */
20158 - eTags->chunkUsed =
20159 + eTags->chunk_used =
20160 (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
20163 - if (eTags->chunkUsed) {
20164 - yaffs_GetTagsFromSpare(dev, &spare, &tags);
20165 + if (eTags->chunk_used) {
20166 + yaffs_get_tags_from_spare(dev, &spare, &tags);
20168 - eTags->objectId = tags.objectId;
20169 - eTags->chunkId = tags.chunkId;
20170 - eTags->byteCount = tags.byteCountLSB;
20171 + eTags->obj_id = tags.obj_id;
20172 + eTags->chunk_id = tags.chunk_id;
20173 + eTags->n_bytes = tags.n_bytes_lsb;
20175 - if (dev->nDataBytesPerChunk >= 1024)
20176 - eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
20177 + if (dev->data_bytes_per_chunk >= 1024)
20178 + eTags->n_bytes |= (((unsigned) tags.n_bytes_msb) << 10);
20180 - eTags->serialNumber = tags.serialNumber;
20181 + eTags->serial_number = tags.serial_number;
20185 @@ -488,49 +486,49 @@ int yaffs_TagsCompatabilityReadChunkWith
20189 -int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
20191 +int yaffs_tags_compat_mark_bad(struct yaffs_dev_s *dev,
20195 - yaffs_Spare spare;
20196 + yaffs_spare spare;
20198 - memset(&spare, 0xff, sizeof(yaffs_Spare));
20199 + memset(&spare, 0xff, sizeof(yaffs_spare));
20201 - spare.blockStatus = 'Y';
20202 + spare.block_status = 'Y';
20204 - yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
20205 + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
20207 - yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
20208 + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
20215 -int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
20217 - yaffs_BlockState *state,
20218 - __u32 *sequenceNumber)
20219 +int yaffs_tags_compat_query_block(struct yaffs_dev_s *dev,
20221 + yaffs_block_state_t *state,
20222 + __u32 *seq_number)
20225 - yaffs_Spare spare0, spare1;
20226 - static yaffs_Spare spareFF;
20227 + yaffs_spare spare0, spare1;
20228 + static yaffs_spare spareFF;
20230 - yaffs_ECCResult dummy;
20231 + yaffs_ecc_result dummy;
20234 memset(&spareFF, 0xFF, sizeof(spareFF));
20238 - *sequenceNumber = 0;
20241 - yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
20242 + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block, NULL,
20243 &spare0, &dummy, 1);
20244 - yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
20245 + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1, NULL,
20246 &spare1, &dummy, 1);
20248 - if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
20249 + if (yaffs_count_bits(spare0.block_status & spare1.block_status) < 7)
20250 *state = YAFFS_BLOCK_STATE_DEAD;
20251 else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
20252 *state = YAFFS_BLOCK_STATE_EMPTY;
20253 --- a/fs/yaffs2/yaffs_tagscompat.h
20254 +++ b/fs/yaffs2/yaffs_tagscompat.h
20257 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
20259 - * Copyright (C) 2002-2007 Aleph One Ltd.
20260 + * Copyright (C) 2002-2010 Aleph One Ltd.
20261 * for Toby Churchill Ltd and Brightstar Engineering
20263 * Created by Charles Manning <charles@aleph1.co.uk>
20264 @@ -17,23 +17,23 @@
20265 #define __YAFFS_TAGSCOMPAT_H__
20267 #include "yaffs_guts.h"
20268 -int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
20270 +int yaffs_tags_compat_wr(yaffs_dev_t *dev,
20273 - const yaffs_ExtendedTags *tags);
20274 -int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
20276 + const yaffs_ext_tags *tags);
20277 +int yaffs_tags_compat_rd(yaffs_dev_t *dev,
20280 - yaffs_ExtendedTags *tags);
20281 -int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
20283 -int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
20285 - yaffs_BlockState *state,
20286 - __u32 *sequenceNumber);
20287 + yaffs_ext_tags *tags);
20288 +int yaffs_tags_compat_mark_bad(struct yaffs_dev_s *dev,
20290 +int yaffs_tags_compat_query_block(struct yaffs_dev_s *dev,
20292 + yaffs_block_state_t *state,
20293 + __u32 *seq_number);
20295 -void yaffs_CalcTagsECC(yaffs_Tags *tags);
20296 -int yaffs_CheckECCOnTags(yaffs_Tags *tags);
20297 -int yaffs_CountBits(__u8 byte);
20298 +void yaffs_calc_tags_ecc(yaffs_tags_t *tags);
20299 +int yaffs_check_tags_ecc(yaffs_tags_t *tags);
20300 +int yaffs_count_bits(__u8 byte);
20303 --- a/fs/yaffs2/yaffs_tagsvalidity.c
20304 +++ b/fs/yaffs2/yaffs_tagsvalidity.c
20307 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
20309 - * Copyright (C) 2002-2007 Aleph One Ltd.
20310 + * Copyright (C) 2002-2010 Aleph One Ltd.
20311 * for Toby Churchill Ltd and Brightstar Engineering
20313 * Created by Charles Manning <charles@aleph1.co.uk>
20314 @@ -13,16 +13,16 @@
20316 #include "yaffs_tagsvalidity.h"
20318 -void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
20319 +void yaffs_init_tags(yaffs_ext_tags *tags)
20321 - memset(tags, 0, sizeof(yaffs_ExtendedTags));
20322 - tags->validMarker0 = 0xAAAAAAAA;
20323 - tags->validMarker1 = 0x55555555;
20324 + memset(tags, 0, sizeof(yaffs_ext_tags));
20325 + tags->validity1 = 0xAAAAAAAA;
20326 + tags->validty1 = 0x55555555;
20329 -int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
20330 +int yaffs_validate_tags(yaffs_ext_tags *tags)
20332 - return (tags->validMarker0 == 0xAAAAAAAA &&
20333 - tags->validMarker1 == 0x55555555);
20334 + return (tags->validity1 == 0xAAAAAAAA &&
20335 + tags->validty1 == 0x55555555);
20338 --- a/fs/yaffs2/yaffs_tagsvalidity.h
20339 +++ b/fs/yaffs2/yaffs_tagsvalidity.h
20342 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
20344 - * Copyright (C) 2002-2007 Aleph One Ltd.
20345 + * Copyright (C) 2002-2010 Aleph One Ltd.
20346 * for Toby Churchill Ltd and Brightstar Engineering
20348 * Created by Charles Manning <charles@aleph1.co.uk>
20351 #include "yaffs_guts.h"
20353 -void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
20354 -int yaffs_ValidateTags(yaffs_ExtendedTags *tags);
20355 +void yaffs_init_tags(yaffs_ext_tags *tags);
20356 +int yaffs_validate_tags(yaffs_ext_tags *tags);
20359 +++ b/fs/yaffs2/yaffs_trace.h
20362 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
20364 + * Copyright (C) 2002-2010 Aleph One Ltd.
20365 + * for Toby Churchill Ltd and Brightstar Engineering
20367 + * Created by Charles Manning <charles@aleph1.co.uk>
20369 + * This program is free software; you can redistribute it and/or modify
20370 + * it under the terms of the GNU Lesser General Public License version 2.1 as
20371 + * published by the Free Software Foundation.
20373 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
20377 +#ifndef __YTRACE_H__
20378 +#define __YTRACE_H__
20380 +extern unsigned int yaffs_trace_mask;
20381 +extern unsigned int yaffs_wr_attempts;
20385 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
20388 +#define YAFFS_TRACE_OS 0x00000002
20389 +#define YAFFS_TRACE_ALLOCATE 0x00000004
20390 +#define YAFFS_TRACE_SCAN 0x00000008
20391 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
20392 +#define YAFFS_TRACE_ERASE 0x00000020
20393 +#define YAFFS_TRACE_GC 0x00000040
20394 +#define YAFFS_TRACE_WRITE 0x00000080
20395 +#define YAFFS_TRACE_TRACING 0x00000100
20396 +#define YAFFS_TRACE_DELETION 0x00000200
20397 +#define YAFFS_TRACE_BUFFERS 0x00000400
20398 +#define YAFFS_TRACE_NANDACCESS 0x00000800
20399 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
20400 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
20401 +#define YAFFS_TRACE_MTD 0x00004000
20402 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
20404 +#define YAFFS_TRACE_VERIFY 0x00010000
20405 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
20406 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
20407 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
20409 +#define YAFFS_TRACE_SYNC 0x00100000
20410 +#define YAFFS_TRACE_BACKGROUND 0x00200000
20411 +#define YAFFS_TRACE_LOCK 0x00400000
20413 +#define YAFFS_TRACE_ERROR 0x40000000
20414 +#define YAFFS_TRACE_BUG 0x80000000
20415 +#define YAFFS_TRACE_ALWAYS 0xF0000000
20418 +#define T(mask, p) do { if ((mask) & (yaffs_trace_mask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
20422 +++ b/fs/yaffs2/yaffs_verify.c
20425 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
20427 + * Copyright (C) 2002-2010 Aleph One Ltd.
20428 + * for Toby Churchill Ltd and Brightstar Engineering
20430 + * Created by Charles Manning <charles@aleph1.co.uk>
20432 + * This program is free software; you can redistribute it and/or modify
20433 + * it under the terms of the GNU General Public License version 2 as
20434 + * published by the Free Software Foundation.
20438 +#include "yaffs_verify.h"
20439 +#include "yaffs_trace.h"
20440 +#include "yaffs_bitmap.h"
20441 +#include "yaffs_getblockinfo.h"
20442 +#include "yaffs_nand.h"
20444 +int yaffs_skip_verification(yaffs_dev_t *dev)
20447 + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
20450 +static int yaffs_skip_full_verification(yaffs_dev_t *dev)
20453 + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
20456 +static int yaffs_skip_nand_verification(yaffs_dev_t *dev)
20459 + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
20463 +static const char *block_stateName[] = {
20477 +void yaffs_verify_blk(yaffs_dev_t *dev, yaffs_block_info_t *bi, int n)
20479 + int actuallyUsed;
20482 + if (yaffs_skip_verification(dev))
20485 + /* Report illegal runtime states */
20486 + if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
20487 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->block_state));
20489 + switch (bi->block_state) {
20490 + case YAFFS_BLOCK_STATE_UNKNOWN:
20491 + case YAFFS_BLOCK_STATE_SCANNING:
20492 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
20493 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
20494 + n, block_stateName[bi->block_state]));
20497 + /* Check pages in use and soft deletions are legal */
20499 + actuallyUsed = bi->pages_in_use - bi->soft_del_pages;
20501 + if (bi->pages_in_use < 0 || bi->pages_in_use > dev->param.chunks_per_block ||
20502 + bi->soft_del_pages < 0 || bi->soft_del_pages > dev->param.chunks_per_block ||
20503 + actuallyUsed < 0 || actuallyUsed > dev->param.chunks_per_block)
20504 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pages_in_used %d soft_del_pages %d"TENDSTR),
20505 + n, bi->pages_in_use, bi->soft_del_pages));
20508 + /* Check chunk bitmap legal */
20509 + inUse = yaffs_count_chunk_bits(dev, n);
20510 + if (inUse != bi->pages_in_use)
20511 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pages_in_use %d counted chunk bits %d"TENDSTR),
20512 + n, bi->pages_in_use, inUse));
20518 +void yaffs_verify_collected_blk(yaffs_dev_t *dev, yaffs_block_info_t *bi, int n)
20520 + yaffs_verify_blk(dev, bi, n);
20522 + /* After collection the block should be in the erased state */
20524 + if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
20525 + bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
20526 + T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
20527 + n, bi->block_state));
20531 +void yaffs_verify_blocks(yaffs_dev_t *dev)
20534 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
20535 + int nIllegalBlockStates = 0;
20537 + if (yaffs_skip_verification(dev))
20540 + memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
20542 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
20543 + yaffs_block_info_t *bi = yaffs_get_block_info(dev, i);
20544 + yaffs_verify_blk(dev, bi, i);
20546 + if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
20547 + nBlocksPerState[bi->block_state]++;
20549 + nIllegalBlockStates++;
20552 + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
20553 + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
20555 + T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
20556 + if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
20557 + T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
20559 + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
20560 + T(YAFFS_TRACE_VERIFY,
20561 + (TSTR("%s %d blocks"TENDSTR),
20562 + block_stateName[i], nBlocksPerState[i]));
20564 + if (dev->blocks_in_checkpt != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
20565 + T(YAFFS_TRACE_VERIFY,
20566 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
20567 + dev->blocks_in_checkpt, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
20569 + if (dev->n_erased_blocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
20570 + T(YAFFS_TRACE_VERIFY,
20571 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
20572 + dev->n_erased_blocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
20574 + if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
20575 + T(YAFFS_TRACE_VERIFY,
20576 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
20577 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
20579 + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
20584 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
20585 + * case those tests will not be performed.
20587 +void yaffs_verify_oh(yaffs_obj_t *obj, yaffs_obj_header *oh, yaffs_ext_tags *tags, int parentCheck)
20589 + if (obj && yaffs_skip_verification(obj->my_dev))
20592 + if (!(tags && obj && oh)) {
20593 + T(YAFFS_TRACE_VERIFY,
20594 + (TSTR("Verifying object header tags %p obj %p oh %p"TENDSTR),
20599 + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
20600 + oh->type > YAFFS_OBJECT_TYPE_MAX)
20601 + T(YAFFS_TRACE_VERIFY,
20602 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
20603 + tags->obj_id, oh->type));
20605 + if (tags->obj_id != obj->obj_id)
20606 + T(YAFFS_TRACE_VERIFY,
20607 + (TSTR("Obj %d header mismatch obj_id %d"TENDSTR),
20608 + tags->obj_id, obj->obj_id));
20612 + * Check that the object's parent ids match if parentCheck requested.
20614 + * Tests do not apply to the root object.
20617 + if (parentCheck && tags->obj_id > 1 && !obj->parent)
20618 + T(YAFFS_TRACE_VERIFY,
20619 + (TSTR("Obj %d header mismatch parent_id %d obj->parent is NULL"TENDSTR),
20620 + tags->obj_id, oh->parent_obj_id));
20622 + if (parentCheck && obj->parent &&
20623 + oh->parent_obj_id != obj->parent->obj_id &&
20624 + (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
20625 + obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
20626 + T(YAFFS_TRACE_VERIFY,
20627 + (TSTR("Obj %d header mismatch parent_id %d parent_obj_id %d"TENDSTR),
20628 + tags->obj_id, oh->parent_obj_id, obj->parent->obj_id));
20630 + if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
20631 + T(YAFFS_TRACE_VERIFY,
20632 + (TSTR("Obj %d header name is NULL"TENDSTR),
20635 + if (tags->obj_id > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
20636 + T(YAFFS_TRACE_VERIFY,
20637 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
20643 +/* Not being used, but don't want to throw away yet */
20644 +int yaffs_verify_tnode_worker(yaffs_obj_t *obj, yaffs_tnode_t *tn,
20645 + __u32 level, int chunk_offset)
20648 + yaffs_dev_t *dev = obj->my_dev;
20654 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
20655 + if (tn->internal[i]) {
20656 + ok = yaffs_verify_tnode_worker(obj,
20659 + (chunk_offset<<YAFFS_TNODES_INTERNAL_BITS) + i);
20662 + } else if (level == 0) {
20663 + yaffs_ext_tags tags;
20664 + __u32 obj_id = obj->obj_id;
20666 + chunk_offset <<= YAFFS_TNODES_LEVEL0_BITS;
20668 + for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
20669 + __u32 theChunk = yaffs_get_group_base(dev, tn, i);
20671 + if (theChunk > 0) {
20672 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.obj_id,tags.chunk_id,theChunk)); */
20673 + yaffs_rd_chunk_tags_nand(dev, theChunk, NULL, &tags);
20674 + if (tags.obj_id != obj_id || tags.chunk_id != chunk_offset) {
20675 + T(~0, (TSTR("Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
20676 + obj_id, chunk_offset, theChunk,
20677 + tags.obj_id, tags.chunk_id));
20691 +void yaffs_verify_file(yaffs_obj_t *obj)
20693 + int requiredTallness;
20694 + int actualTallness;
20698 + yaffs_dev_t *dev;
20699 + yaffs_ext_tags tags;
20700 + yaffs_tnode_t *tn;
20706 + if (yaffs_skip_verification(obj->my_dev))
20709 + dev = obj->my_dev;
20710 + obj_id = obj->obj_id;
20712 + /* Check file size is consistent with tnode depth */
20713 + lastChunk = obj->variant.file_variant.file_size / dev->data_bytes_per_chunk + 1;
20714 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
20715 + requiredTallness = 0;
20717 + x >>= YAFFS_TNODES_INTERNAL_BITS;
20718 + requiredTallness++;
20721 + actualTallness = obj->variant.file_variant.top_level;
20723 + /* Check that the chunks in the tnode tree are all correct.
20724 + * We do this by scanning through the tnode tree and
20725 + * checking the tags for every chunk match.
20728 + if (yaffs_skip_nand_verification(dev))
20731 + for (i = 1; i <= lastChunk; i++) {
20732 + tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
20735 + __u32 theChunk = yaffs_get_group_base(dev, tn, i);
20736 + if (theChunk > 0) {
20737 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),obj_id,i,theChunk)); */
20738 + yaffs_rd_chunk_tags_nand(dev, theChunk, NULL, &tags);
20739 + if (tags.obj_id != obj_id || tags.chunk_id != i) {
20740 + T(~0, (TSTR("Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
20741 + obj_id, i, theChunk,
20742 + tags.obj_id, tags.chunk_id));
20750 +void yaffs_verify_link(yaffs_obj_t *obj)
20752 + if (obj && yaffs_skip_verification(obj->my_dev))
20755 + /* Verify sane equivalent object */
20758 +void yaffs_verify_symlink(yaffs_obj_t *obj)
20760 + if (obj && yaffs_skip_verification(obj->my_dev))
20763 + /* Verify symlink string */
20766 +void yaffs_verify_special(yaffs_obj_t *obj)
20768 + if (obj && yaffs_skip_verification(obj->my_dev))
20772 +void yaffs_verify_obj(yaffs_obj_t *obj)
20774 + yaffs_dev_t *dev;
20779 + __u32 chunk_idOk;
20780 + __u32 chunkInRange;
20781 + __u32 chunkShouldNotBeDeleted;
20782 + __u32 chunkValid;
20787 + if (obj->being_created)
20790 + dev = obj->my_dev;
20792 + if (yaffs_skip_verification(dev))
20795 + /* Check sane object header chunk */
20797 + chunkMin = dev->internal_start_block * dev->param.chunks_per_block;
20798 + chunkMax = (dev->internal_end_block+1) * dev->param.chunks_per_block - 1;
20800 + chunkInRange = (((unsigned)(obj->hdr_chunk)) >= chunkMin && ((unsigned)(obj->hdr_chunk)) <= chunkMax);
20801 + chunk_idOk = chunkInRange || (obj->hdr_chunk == 0);
20802 + chunkValid = chunkInRange &&
20803 + yaffs_check_chunk_bit(dev,
20804 + obj->hdr_chunk / dev->param.chunks_per_block,
20805 + obj->hdr_chunk % dev->param.chunks_per_block);
20806 + chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
20808 + if (!obj->fake &&
20809 + (!chunk_idOk || chunkShouldNotBeDeleted)) {
20810 + T(YAFFS_TRACE_VERIFY,
20811 + (TSTR("Obj %d has chunk_id %d %s %s"TENDSTR),
20812 + obj->obj_id, obj->hdr_chunk,
20813 + chunk_idOk ? "" : ",out of range",
20814 + chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
20817 + if (chunkValid && !yaffs_skip_nand_verification(dev)) {
20818 + yaffs_ext_tags tags;
20819 + yaffs_obj_header *oh;
20820 + __u8 *buffer = yaffs_get_temp_buffer(dev, __LINE__);
20822 + oh = (yaffs_obj_header *)buffer;
20824 + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer,
20827 + yaffs_verify_oh(obj, oh, &tags, 1);
20829 + yaffs_release_temp_buffer(dev, buffer, __LINE__);
20832 + /* Verify it has a parent */
20833 + if (obj && !obj->fake &&
20834 + (!obj->parent || obj->parent->my_dev != dev)) {
20835 + T(YAFFS_TRACE_VERIFY,
20836 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
20837 + obj->obj_id, obj->parent));
20840 + /* Verify parent is a directory */
20841 + if (obj->parent && obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
20842 + T(YAFFS_TRACE_VERIFY,
20843 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
20844 + obj->obj_id, obj->parent->variant_type));
20847 + switch (obj->variant_type) {
20848 + case YAFFS_OBJECT_TYPE_FILE:
20849 + yaffs_verify_file(obj);
20851 + case YAFFS_OBJECT_TYPE_SYMLINK:
20852 + yaffs_verify_symlink(obj);
20854 + case YAFFS_OBJECT_TYPE_DIRECTORY:
20855 + yaffs_verify_dir(obj);
20857 + case YAFFS_OBJECT_TYPE_HARDLINK:
20858 + yaffs_verify_link(obj);
20860 + case YAFFS_OBJECT_TYPE_SPECIAL:
20861 + yaffs_verify_special(obj);
20863 + case YAFFS_OBJECT_TYPE_UNKNOWN:
20865 + T(YAFFS_TRACE_VERIFY,
20866 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
20867 + obj->obj_id, obj->variant_type));
20872 +void yaffs_verify_objects(yaffs_dev_t *dev)
20874 + yaffs_obj_t *obj;
20876 + struct ylist_head *lh;
20878 + if (yaffs_skip_verification(dev))
20881 + /* Iterate through the objects in each hash entry */
20883 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
20884 + ylist_for_each(lh, &dev->obj_bucket[i].list) {
20886 + obj = ylist_entry(lh, yaffs_obj_t, hash_link);
20887 + yaffs_verify_obj(obj);
20894 +void yaffs_verify_obj_in_dir(yaffs_obj_t *obj)
20896 + struct ylist_head *lh;
20897 + yaffs_obj_t *listObj;
20902 + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
20907 + if (yaffs_skip_verification(obj->my_dev))
20910 + if (!obj->parent) {
20911 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
20916 + if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
20917 + T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
20921 + /* Iterate through the objects in each hash entry */
20923 + ylist_for_each(lh, &obj->parent->variant.dir_variant.children) {
20925 + listObj = ylist_entry(lh, yaffs_obj_t, siblings);
20926 + yaffs_verify_obj(listObj);
20927 + if (obj == listObj)
20932 + if (count != 1) {
20933 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
20938 +void yaffs_verify_dir(yaffs_obj_t *directory)
20940 + struct ylist_head *lh;
20941 + yaffs_obj_t *listObj;
20943 + if (!directory) {
20948 + if (yaffs_skip_full_verification(directory->my_dev))
20951 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
20952 + T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variant_type));
20956 + /* Iterate through the objects in each hash entry */
20958 + ylist_for_each(lh, &directory->variant.dir_variant.children) {
20960 + listObj = ylist_entry(lh, yaffs_obj_t, siblings);
20961 + if (listObj->parent != directory) {
20962 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
20965 + yaffs_verify_obj_in_dir(listObj);
20970 +static int yaffs_free_verification_failures;
20972 +void yaffs_verify_free_chunks(yaffs_dev_t *dev)
20977 + if (yaffs_skip_verification(dev))
20980 + counted = yaffs_count_free_chunks(dev);
20982 + difference = dev->n_free_chunks - counted;
20984 + if (difference) {
20985 + T(YAFFS_TRACE_ALWAYS,
20986 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
20987 + dev->n_free_chunks, counted, difference));
20988 + yaffs_free_verification_failures++;
20992 +int yaffs_verify_file_sane(yaffs_obj_t *in)
21000 + yaffs_tnode_t *tn;
21001 + yaffs_tags_t localTags;
21002 + yaffs_tags_t *tags = &localTags;
21006 + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
21007 + return YAFFS_FAIL;
21009 + obj_id = in->obj_id;
21010 + fSize = in->variant.file_variant.file_size;
21012 + (fSize + in->my_dev->data_bytes_per_chunk - 1) / in->my_dev->data_bytes_per_chunk;
21014 + for (chunk = 1; chunk <= n_chunks; chunk++) {
21015 + tn = yaffs_find_tnode_0(in->my_dev, &in->variant.file_variant,
21020 + theChunk = yaffs_get_group_base(dev, tn, chunk);
21022 + if (yaffs_check_chunk_bits
21023 + (dev, theChunk / dev->param.chunks_per_block,
21024 + theChunk % dev->param.chunks_per_block)) {
21026 + yaffs_rd_chunk_tags_nand(in->my_dev, theChunk,
21029 + if (yaffs_tags_match
21030 + (tags, in->obj_id, chunk, is_deleted)) {
21040 + /* T(("No level 0 found for %d\n", chunk)); */
21044 + return failed ? YAFFS_FAIL : YAFFS_OK;
21051 +++ b/fs/yaffs2/yaffs_verify.h
21054 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
21056 + * Copyright (C) 2002-2010 Aleph One Ltd.
21057 + * for Toby Churchill Ltd and Brightstar Engineering
21059 + * Created by Charles Manning <charles@aleph1.co.uk>
21061 + * This program is free software; you can redistribute it and/or modify
21062 + * it under the terms of the GNU General Public License version 2 as
21063 + * published by the Free Software Foundation.
21066 +#ifndef __YAFFS_VERIFY_H__
21067 +#define __YAFFS_VERIFY_H__
21069 +#include "yaffs_guts.h"
21071 +void yaffs_verify_blk(yaffs_dev_t *dev, yaffs_block_info_t *bi, int n);
21072 +void yaffs_verify_collected_blk(yaffs_dev_t *dev, yaffs_block_info_t *bi, int n);
21073 +void yaffs_verify_blocks(yaffs_dev_t *dev);
21075 +void yaffs_verify_oh(yaffs_obj_t *obj, yaffs_obj_header *oh, yaffs_ext_tags *tags, int parentCheck);
21076 +void yaffs_verify_file(yaffs_obj_t *obj);
21077 +void yaffs_verify_link(yaffs_obj_t *obj);
21078 +void yaffs_verify_symlink(yaffs_obj_t *obj);
21079 +void yaffs_verify_special(yaffs_obj_t *obj);
21080 +void yaffs_verify_obj(yaffs_obj_t *obj);
21081 +void yaffs_verify_objects(yaffs_dev_t *dev);
21082 +void yaffs_verify_obj_in_dir(yaffs_obj_t *obj);
21083 +void yaffs_verify_dir(yaffs_obj_t *directory);
21084 +void yaffs_verify_free_chunks(yaffs_dev_t *dev);
21086 +int yaffs_verify_file_sane(yaffs_obj_t *obj);
21088 +int yaffs_skip_verification(yaffs_dev_t *dev);
21093 +++ b/fs/yaffs2/yaffs_vfs_glue.c
21096 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
21098 + * Copyright (C) 2002-2010 Aleph One Ltd.
21099 + * for Toby Churchill Ltd and Brightstar Engineering
21101 + * Created by Charles Manning <charles@aleph1.co.uk>
21102 + * Acknowledgements:
21103 + * Luc van OostenRyck for numerous patches.
21104 + * Nick Bane for numerous patches.
21105 + * Nick Bane for 2.5/2.6 integration.
21106 + * Andras Toth for mknod rdev issue.
21107 + * Michael Fischer for finding the problem with inode inconsistency.
21108 + * Some code bodily lifted from JFFS
21110 + * This program is free software; you can redistribute it and/or modify
21111 + * it under the terms of the GNU General Public License version 2 as
21112 + * published by the Free Software Foundation.
21117 + * This is the file system front-end to YAFFS that hooks it up to
21121 + * >> 2.4: sb->u.generic_sbp points to the yaffs_dev_t associated with
21122 + * this superblock
21123 + * >> 2.6: sb->s_fs_info points to the yaffs_dev_t associated with this
21125 + * >> inode->u.generic_ip points to the associated yaffs_obj_t.
21129 + * There are two variants of the VFS glue code. This variant should compile
21130 + * for any version of Linux.
21132 +#include <linux/version.h>
21134 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
21135 +#define YAFFS_COMPILE_BACKGROUND
21136 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6, 23))
21137 +#define YAFFS_COMPILE_FREEZER
21141 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28))
21142 +#define YAFFS_COMPILE_EXPORTFS
21145 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
21146 +#define YAFFS_USE_SETATTR_COPY
21147 +#define YAFFS_USE_TRUNCATE_SETSIZE
21149 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,35))
21150 +#define YAFFS_HAS_EVICT_INODE
21153 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
21154 +#define YAFFS_NEW_FOLLOW_LINK 1
21156 +#define YAFFS_NEW_FOLLOW_LINK 0
21159 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
21160 +#include <linux/config.h>
21163 +#include <linux/kernel.h>
21164 +#include <linux/module.h>
21165 +#include <linux/slab.h>
21166 +#include <linux/init.h>
21167 +#include <linux/fs.h>
21168 +#include <linux/proc_fs.h>
21169 +#include <linux/smp_lock.h>
21170 +#include <linux/pagemap.h>
21171 +#include <linux/mtd/mtd.h>
21172 +#include <linux/interrupt.h>
21173 +#include <linux/string.h>
21174 +#include <linux/ctype.h>
21176 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21177 +#include <linux/namei.h>
21180 +#ifdef YAFFS_COMPILE_EXPORTFS
21181 +#include <linux/exportfs.h>
21184 +#ifdef YAFFS_COMPILE_BACKGROUND
21185 +#include <linux/kthread.h>
21186 +#include <linux/delay.h>
21188 +#ifdef YAFFS_COMPILE_FREEZER
21189 +#include <linux/freezer.h>
21192 +#include <asm/div64.h>
21194 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21196 +#include <linux/statfs.h>
21198 +#define UnlockPage(p) unlock_page(p)
21199 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
21201 +/* FIXME: use sb->s_id instead ? */
21202 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
21206 +#include <linux/locks.h>
21207 +#define BDEVNAME_SIZE 0
21208 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
21210 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
21211 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
21217 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
21218 +#define YPROC_ROOT (&proc_root)
21220 +#define YPROC_ROOT NULL
21223 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26))
21224 +#define Y_INIT_TIMER(a) init_timer(a)
21226 +#define Y_INIT_TIMER(a) init_timer_on_stack(a)
21229 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
21230 +#define WRITE_SIZE_STR "writesize"
21231 +#define WRITE_SIZE(mtd) ((mtd)->writesize)
21233 +#define WRITE_SIZE_STR "oobblock"
21234 +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
21237 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
21238 +#define YAFFS_USE_WRITE_BEGIN_END 1
21240 +#define YAFFS_USE_WRITE_BEGIN_END 0
21243 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
21244 +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
21246 + uint64_t result = partition_size;
21247 + do_div(result, block_size);
21248 + return (uint32_t)result;
21251 +#define YCALCBLOCKS(s, b) ((s)/(b))
21254 +#include <linux/uaccess.h>
21255 +#include <linux/mtd/mtd.h>
21257 +#include "yportenv.h"
21258 +#include "yaffs_trace.h"
21259 +#include "yaffs_guts.h"
21261 +#include "yaffs_linux.h"
21263 +#include "yaffs_mtdif.h"
21264 +#include "yaffs_mtdif1.h"
21265 +#include "yaffs_mtdif2.h"
21267 +unsigned int yaffs_trace_mask = YAFFS_TRACE_BAD_BLOCKS | YAFFS_TRACE_ALWAYS;
21268 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
21269 +unsigned int yaffs_auto_checkpoint = 1;
21270 +unsigned int yaffs_gc_control = 1;
21271 +unsigned int yaffs_bg_enable = 1;
21273 +/* Module Parameters */
21274 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21275 +module_param(yaffs_trace_mask, uint, 0644);
21276 +module_param(yaffs_wr_attempts, uint, 0644);
21277 +module_param(yaffs_auto_checkpoint, uint, 0644);
21278 +module_param(yaffs_gc_control, uint, 0644);
21279 +module_param(yaffs_bg_enable, uint, 0644);
21281 +MODULE_PARM(yaffs_trace_mask, "i");
21282 +MODULE_PARM(yaffs_wr_attempts, "i");
21283 +MODULE_PARM(yaffs_auto_checkpoint, "i");
21284 +MODULE_PARM(yaffs_gc_control, "i");
21287 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
21288 +/* use iget and read_inode */
21289 +#define Y_IGET(sb, inum) iget((sb), (inum))
21290 +static void yaffs_read_inode(struct inode *inode);
21293 +/* Call local equivalent */
21294 +#define YAFFS_USE_OWN_IGET
21295 +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
21297 +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
21300 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
21301 +#define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
21303 +#define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
21306 +#define yaffs_InodeToObject(iptr) ((yaffs_obj_t *)(yaffs_InodeToObjectLV(iptr)))
21307 +#define yaffs_dentry_to_obj(dptr) yaffs_InodeToObject((dptr)->d_inode)
21309 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21310 +#define yaffs_SuperToDevice(sb) ((yaffs_dev_t *)sb->s_fs_info)
21312 +#define yaffs_SuperToDevice(sb) ((yaffs_dev_t *)sb->u.generic_sbp)
21316 +#define update_dir_time(dir) do {\
21317 + (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
21320 +static void yaffs_put_super(struct super_block *sb);
21322 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
21324 +static ssize_t yaffs_hold_space(struct file *f);
21325 +static void yaffs_release_space(struct file *f);
21327 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
21328 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
21330 +static int yaffs_file_flush(struct file *file);
21333 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
21334 +static int yaffs_sync_object(struct file *file, int datasync);
21336 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
21340 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
21342 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21343 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
21344 + struct nameidata *n);
21345 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
21346 + struct nameidata *n);
21348 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
21349 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
21351 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
21352 + struct dentry *dentry);
21353 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
21354 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
21355 + const char *symname);
21356 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
21358 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21359 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
21362 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
21365 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
21366 + struct inode *new_dir, struct dentry *new_dentry);
21367 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
21369 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
21370 +static int yaffs_sync_fs(struct super_block *sb, int wait);
21371 +static void yaffs_write_super(struct super_block *sb);
21373 +static int yaffs_sync_fs(struct super_block *sb);
21374 +static int yaffs_write_super(struct super_block *sb);
21377 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
21378 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
21379 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21380 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
21382 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
21385 +#ifdef YAFFS_HAS_PUT_INODE
21386 +static void yaffs_put_inode(struct inode *inode);
21389 +#ifdef YAFFS_HAS_EVICT_INODE
21390 +static void yaffs_evict_inode(struct inode *);
21392 +static void yaffs_delete_inode(struct inode *);
21393 +static void yaffs_clear_inode(struct inode *);
21396 +static int yaffs_readpage(struct file *file, struct page *page);
21397 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21398 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
21400 +static int yaffs_writepage(struct page *page);
21403 +#ifdef CONFIG_YAFFS_XATTR
21404 +int yaffs_setxattr(struct dentry *dentry, const char *name,
21405 + const void *value, size_t size, int flags);
21406 +ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
21408 +int yaffs_removexattr(struct dentry *dentry, const char *name);
21409 +ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size);
21413 +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
21414 +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
21415 + loff_t pos, unsigned len, unsigned flags,
21416 + struct page **pagep, void **fsdata);
21417 +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
21418 + loff_t pos, unsigned len, unsigned copied,
21419 + struct page *pg, void *fsdadata);
21421 +static int yaffs_prepare_write(struct file *f, struct page *pg,
21422 + unsigned offset, unsigned to);
21423 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
21428 +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
21430 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21431 +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias);
21432 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
21434 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
21437 +static void yaffs_touch_super(yaffs_dev_t *dev);
21439 +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin);
21441 +static int yaffs_vfs_setattr(struct inode *, struct iattr *);
21444 +static struct address_space_operations yaffs_file_address_operations = {
21445 + .readpage = yaffs_readpage,
21446 + .writepage = yaffs_writepage,
21447 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
21448 + .write_begin = yaffs_write_begin,
21449 + .write_end = yaffs_write_end,
21451 + .prepare_write = yaffs_prepare_write,
21452 + .commit_write = yaffs_commit_write,
21457 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
21458 +static const struct file_operations yaffs_file_operations = {
21459 + .read = do_sync_read,
21460 + .write = do_sync_write,
21461 + .aio_read = generic_file_aio_read,
21462 + .aio_write = generic_file_aio_write,
21463 + .mmap = generic_file_mmap,
21464 + .flush = yaffs_file_flush,
21465 + .fsync = yaffs_sync_object,
21466 + .splice_read = generic_file_splice_read,
21467 + .splice_write = generic_file_splice_write,
21468 + .llseek = generic_file_llseek,
21471 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
21473 +static const struct file_operations yaffs_file_operations = {
21474 + .read = do_sync_read,
21475 + .write = do_sync_write,
21476 + .aio_read = generic_file_aio_read,
21477 + .aio_write = generic_file_aio_write,
21478 + .mmap = generic_file_mmap,
21479 + .flush = yaffs_file_flush,
21480 + .fsync = yaffs_sync_object,
21481 + .sendfile = generic_file_sendfile,
21486 +static const struct file_operations yaffs_file_operations = {
21487 + .read = generic_file_read,
21488 + .write = generic_file_write,
21489 + .mmap = generic_file_mmap,
21490 + .flush = yaffs_file_flush,
21491 + .fsync = yaffs_sync_object,
21492 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21493 + .sendfile = generic_file_sendfile,
21498 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
21499 +static void zero_user_segment(struct page *page, unsigned start, unsigned end)
21501 + void * kaddr = kmap_atomic(page, KM_USER0);
21502 + memset(kaddr + start, 0, end - start);
21503 + kunmap_atomic(kaddr, KM_USER0);
21504 + flush_dcache_page(page);
21509 +static const struct inode_operations yaffs_file_inode_operations = {
21510 + .setattr = yaffs_setattr,
21511 +#ifdef CONFIG_YAFFS_XATTR
21512 + .setxattr = yaffs_setxattr,
21513 + .getxattr = yaffs_getxattr,
21514 + .listxattr = yaffs_listxattr,
21515 + .removexattr = yaffs_removexattr,
21519 +static const struct inode_operations yaffs_symlink_inode_operations = {
21520 + .readlink = yaffs_readlink,
21521 + .follow_link = yaffs_follow_link,
21522 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21523 + .put_link = yaffs_put_link,
21525 + .setattr = yaffs_setattr,
21526 +#ifdef CONFIG_YAFFS_XATTR
21527 + .setxattr = yaffs_setxattr,
21528 + .getxattr = yaffs_getxattr,
21529 + .listxattr = yaffs_listxattr,
21530 + .removexattr = yaffs_removexattr,
21534 +static const struct inode_operations yaffs_dir_inode_operations = {
21535 + .create = yaffs_create,
21536 + .lookup = yaffs_lookup,
21537 + .link = yaffs_link,
21538 + .unlink = yaffs_unlink,
21539 + .symlink = yaffs_symlink,
21540 + .mkdir = yaffs_mkdir,
21541 + .rmdir = yaffs_unlink,
21542 + .mknod = yaffs_mknod,
21543 + .rename = yaffs_rename,
21544 + .setattr = yaffs_setattr,
21545 +#ifdef CONFIG_YAFFS_XATTR
21546 + .setxattr = yaffs_setxattr,
21547 + .getxattr = yaffs_getxattr,
21548 + .listxattr = yaffs_listxattr,
21549 + .removexattr = yaffs_removexattr,
21553 +static const struct file_operations yaffs_dir_operations = {
21554 + .read = generic_read_dir,
21555 + .readdir = yaffs_readdir,
21556 + .fsync = yaffs_sync_object,
21557 + .llseek = yaffs_dir_llseek,
21560 +static const struct super_operations yaffs_super_ops = {
21561 + .statfs = yaffs_statfs,
21563 +#ifndef YAFFS_USE_OWN_IGET
21564 + .read_inode = yaffs_read_inode,
21566 +#ifdef YAFFS_HAS_PUT_INODE
21567 + .put_inode = yaffs_put_inode,
21569 + .put_super = yaffs_put_super,
21570 +#ifdef YAFFS_HAS_EVICT_INODE
21571 + .evict_inode = yaffs_evict_inode,
21573 + .delete_inode = yaffs_delete_inode,
21574 + .clear_inode = yaffs_clear_inode,
21576 + .sync_fs = yaffs_sync_fs,
21577 + .write_super = yaffs_write_super,
21581 +static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
21583 +#ifdef YAFFS_USE_SETATTR_COPY
21584 + setattr_copy(inode,attr);
21587 + return inode_setattr(inode, attr);
21592 +static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
21594 +#ifdef YAFFS_USE_TRUNCATE_SETSIZE
21595 + truncate_setsize(inode,newsize);
21598 + truncate_inode_pages(&inode->i_data,newsize);
21604 +static unsigned yaffs_gc_control_callback(yaffs_dev_t *dev)
21606 + return yaffs_gc_control;
21609 +static void yaffs_gross_lock(yaffs_dev_t *dev)
21611 + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locking %p\n"), current));
21612 + down(&(yaffs_dev_to_lc(dev)->grossLock));
21613 + T(YAFFS_TRACE_LOCK, (TSTR("yaffs locked %p\n"), current));
21616 +static void yaffs_gross_unlock(yaffs_dev_t *dev)
21618 + T(YAFFS_TRACE_LOCK, (TSTR("yaffs unlocking %p\n"), current));
21619 + up(&(yaffs_dev_to_lc(dev)->grossLock));
21622 +#ifdef YAFFS_COMPILE_EXPORTFS
21624 +static struct inode *
21625 +yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino, uint32_t generation)
21627 + return Y_IGET(sb, ino);
21630 +static struct dentry *
21631 +yaffs2_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
21633 + return generic_fh_to_dentry(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode) ;
21636 +static struct dentry *
21637 + yaffs2_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, int fh_type)
21639 + return generic_fh_to_parent(sb, fid, fh_len, fh_type, yaffs2_nfs_get_inode);
21642 +struct dentry *yaffs2_get_parent(struct dentry *dentry)
21645 + struct super_block *sb = dentry->d_inode->i_sb;
21646 + struct dentry *parent = ERR_PTR(-ENOENT);
21647 + struct inode *inode;
21648 + unsigned long parent_ino;
21649 + yaffs_obj_t *d_obj;
21650 + yaffs_obj_t *parent_obj;
21652 + d_obj = yaffs_InodeToObject(dentry->d_inode);
21655 + parent_obj = d_obj->parent;
21656 + if (parent_obj) {
21657 + parent_ino = yaffs_get_obj_inode(parent_obj);
21658 + inode = Y_IGET(sb, parent_ino);
21660 + if (IS_ERR(inode)) {
21661 + parent = ERR_CAST(inode);
21663 + parent = d_obtain_alias(inode);
21664 + if (!IS_ERR(parent)) {
21665 + parent = ERR_PTR(-ENOMEM);
21675 +/* Just declare a zero structure as a NULL value implies
21676 + * using the default functions of exportfs.
21679 +static struct export_operations yaffs_export_ops =
21681 + .fh_to_dentry = yaffs2_fh_to_dentry,
21682 + .fh_to_parent = yaffs2_fh_to_parent,
21683 + .get_parent = yaffs2_get_parent,
21688 +/*-----------------------------------------------------------------*/
21689 +/* Directory search context allows us to unlock access to yaffs during
21690 + * filldir without causing problems with the directory being modified.
21691 + * This is similar to the tried and tested mechanism used in yaffs direct.
21693 + * A search context iterates along a doubly linked list of siblings in the
21694 + * directory. If the iterating object is deleted then this would corrupt
21695 + * the list iteration, likely causing a crash. The search context avoids
21696 + * this by using the remove_obj_fn to move the search context to the
21697 + * next object before the object is deleted.
21699 + * Many readdirs (and thus seach conexts) may be alive simulateously so
21700 + * each yaffs_dev_t has a list of these.
21702 + * A seach context lives for the duration of a readdir.
21704 + * All these functions must be called while yaffs is locked.
21707 +struct yaffs_SearchContext {
21708 + yaffs_dev_t *dev;
21709 + yaffs_obj_t *dirObj;
21710 + yaffs_obj_t *nextReturn;
21711 + struct ylist_head others;
21715 + * yaffs_NewSearch() creates a new search context, initialises it and
21716 + * adds it to the device's search context list.
21718 + * Called at start of readdir.
21720 +static struct yaffs_SearchContext * yaffs_NewSearch(yaffs_obj_t *dir)
21722 + yaffs_dev_t *dev = dir->my_dev;
21723 + struct yaffs_SearchContext *sc = YMALLOC(sizeof(struct yaffs_SearchContext));
21725 + sc->dirObj = dir;
21727 + if( ylist_empty(&sc->dirObj->variant.dir_variant.children))
21728 + sc->nextReturn = NULL;
21730 + sc->nextReturn = ylist_entry(
21731 + dir->variant.dir_variant.children.next,
21732 + yaffs_obj_t,siblings);
21733 + YINIT_LIST_HEAD(&sc->others);
21734 + ylist_add(&sc->others,&(yaffs_dev_to_lc(dev)->searchContexts));
21740 + * yaffs_search_end() disposes of a search context and cleans up.
21742 +static void yaffs_search_end(struct yaffs_SearchContext * sc)
21745 + ylist_del(&sc->others);
21751 + * yaffs_search_advance() moves a search context to the next object.
21752 + * Called when the search iterates or when an object removal causes
21753 + * the search context to be moved to the next object.
21755 +static void yaffs_search_advance(struct yaffs_SearchContext *sc)
21760 + if( sc->nextReturn == NULL ||
21761 + ylist_empty(&sc->dirObj->variant.dir_variant.children))
21762 + sc->nextReturn = NULL;
21764 + struct ylist_head *next = sc->nextReturn->siblings.next;
21766 + if( next == &sc->dirObj->variant.dir_variant.children)
21767 + sc->nextReturn = NULL; /* end of list */
21769 + sc->nextReturn = ylist_entry(next,yaffs_obj_t,siblings);
21774 + * yaffs_remove_obj_callback() is called when an object is unlinked.
21775 + * We check open search contexts and advance any which are currently
21776 + * on the object being iterated.
21778 +static void yaffs_remove_obj_callback(yaffs_obj_t *obj)
21781 + struct ylist_head *i;
21782 + struct yaffs_SearchContext *sc;
21783 + struct ylist_head *search_contexts = &(yaffs_dev_to_lc(obj->my_dev)->searchContexts);
21786 + /* Iterate through the directory search contexts.
21787 + * If any are currently on the object being removed, then advance
21788 + * the search context to the next object to prevent a hanging pointer.
21790 + ylist_for_each(i, search_contexts) {
21792 + sc = ylist_entry(i, struct yaffs_SearchContext,others);
21793 + if(sc->nextReturn == obj)
21794 + yaffs_search_advance(sc);
21801 +/*-----------------------------------------------------------------*/
21803 +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
21806 + unsigned char *alias;
21809 + yaffs_dev_t *dev = yaffs_dentry_to_obj(dentry)->my_dev;
21811 + yaffs_gross_lock(dev);
21813 + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
21815 + yaffs_gross_unlock(dev);
21820 + ret = vfs_readlink(dentry, buffer, buflen, alias);
21825 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21826 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
21828 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
21831 + unsigned char *alias;
21833 + yaffs_dev_t *dev = yaffs_dentry_to_obj(dentry)->my_dev;
21835 + yaffs_gross_lock(dev);
21837 + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
21838 + yaffs_gross_unlock(dev);
21845 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21846 + nd_set_link(nd, alias);
21847 + ret = (int)alias;
21849 + return ERR_PTR(ret);
21851 + ret = vfs_follow_link(nd, alias);
21858 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
21859 +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias) {
21864 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
21865 + yaffs_obj_t *obj);
21868 + * Lookup is used to find objects in the fs
21870 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
21872 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
21873 + struct nameidata *n)
21875 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
21878 + yaffs_obj_t *obj;
21879 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
21881 + yaffs_dev_t *dev = yaffs_InodeToObject(dir)->my_dev;
21883 + if(current != yaffs_dev_to_lc(dev)->readdirProcess)
21884 + yaffs_gross_lock(dev);
21886 + T(YAFFS_TRACE_OS,
21887 + (TSTR("yaffs_lookup for %d:%s\n"),
21888 + yaffs_InodeToObject(dir)->obj_id, dentry->d_name.name));
21890 + obj = yaffs_find_by_name(yaffs_InodeToObject(dir),
21891 + dentry->d_name.name);
21893 + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
21895 + /* Can't hold gross lock when calling yaffs_get_inode() */
21896 + if(current != yaffs_dev_to_lc(dev)->readdirProcess)
21897 + yaffs_gross_unlock(dev);
21900 + T(YAFFS_TRACE_OS,
21901 + (TSTR("yaffs_lookup found %d\n"), obj->obj_id));
21903 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
21906 + T(YAFFS_TRACE_OS,
21907 + (TSTR("yaffs_loookup dentry \n")));
21908 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
21909 + * d_add even if NULL inode */
21911 + /*dget(dentry); // try to solve directory bug */
21912 + d_add(dentry, inode);
21914 + /* return dentry; */
21920 + T(YAFFS_TRACE_OS,(TSTR("yaffs_lookup not found\n")));
21924 +/* added NCB for 2.5/6 compatability - forces add even if inode is
21925 + * NULL which creates dentry hash */
21926 + d_add(dentry, inode);
21932 +#ifdef YAFFS_HAS_PUT_INODE
21934 +/* For now put inode is just for debugging
21935 + * Put inode is called when the inode **structure** is put.
21937 +static void yaffs_put_inode(struct inode *inode)
21939 + T(YAFFS_TRACE_OS,
21940 + (TSTR("yaffs_put_inode: ino %d, count %d\n"), (int)inode->i_ino,
21941 + atomic_read(&inode->i_count)));
21947 +static void yaffs_unstitch_obj(struct inode *inode, yaffs_obj_t *obj)
21949 + /* Clear the association between the inode and
21950 + * the yaffs_obj_t.
21952 + obj->my_inode = NULL;
21953 + yaffs_InodeToObjectLV(inode) = NULL;
21955 + /* If the object freeing was deferred, then the real
21956 + * free happens now.
21957 + * This should fix the inode inconsistency problem.
21959 + yaffs_handle_defered_free(obj);
21962 +#ifdef YAFFS_HAS_EVICT_INODE
21963 +/* yaffs_evict_inode combines into one operation what was previously done in
21964 + * yaffs_clear_inode() and yaffs_delete_inode()
21967 +static void yaffs_evict_inode( struct inode *inode)
21969 + yaffs_obj_t *obj;
21970 + yaffs_dev_t *dev;
21971 + int deleteme = 0;
21973 + obj = yaffs_InodeToObject(inode);
21975 + T(YAFFS_TRACE_OS,
21976 + (TSTR("yaffs_evict_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
21977 + atomic_read(&inode->i_count),
21978 + obj ? "object exists" : "null object"));
21980 + if (!inode->i_nlink && !is_bad_inode(inode))
21982 + truncate_inode_pages(&inode->i_data,0);
21983 + end_writeback(inode);
21985 + if(deleteme && obj){
21986 + dev = obj->my_dev;
21987 + yaffs_gross_lock(dev);
21988 + yaffs_del_obj(obj);
21989 + yaffs_gross_unlock(dev);
21992 + dev = obj->my_dev;
21993 + yaffs_gross_lock(dev);
21994 + yaffs_unstitch_obj(inode,obj);
21995 + yaffs_gross_unlock(dev);
22002 +/* clear is called to tell the fs to release any per-inode data it holds.
22003 + * The object might still exist on disk and is just being thrown out of the cache
22004 + * or else the object has actually been deleted and we're being called via
22006 + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
22009 +static void yaffs_clear_inode(struct inode *inode)
22011 + yaffs_obj_t *obj;
22012 + yaffs_dev_t *dev;
22014 + obj = yaffs_InodeToObject(inode);
22016 + T(YAFFS_TRACE_OS,
22017 + (TSTR("yaffs_clear_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
22018 + atomic_read(&inode->i_count),
22019 + obj ? "object exists" : "null object"));
22022 + dev = obj->my_dev;
22023 + yaffs_gross_lock(dev);
22024 + yaffs_unstitch_obj(inode,obj);
22025 + yaffs_gross_unlock(dev);
22030 +/* delete is called when the link count is zero and the inode
22031 + * is put (ie. nobody wants to know about it anymore, time to
22032 + * delete the file).
22033 + * NB Must call clear_inode()
22035 +static void yaffs_delete_inode(struct inode *inode)
22037 + yaffs_obj_t *obj = yaffs_InodeToObject(inode);
22038 + yaffs_dev_t *dev;
22040 + T(YAFFS_TRACE_OS,
22041 + (TSTR("yaffs_delete_inode: ino %d, count %d %s\n"), (int)inode->i_ino,
22042 + atomic_read(&inode->i_count),
22043 + obj ? "object exists" : "null object"));
22046 + dev = obj->my_dev;
22047 + yaffs_gross_lock(dev);
22048 + yaffs_del_obj(obj);
22049 + yaffs_gross_unlock(dev);
22051 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
22052 + truncate_inode_pages(&inode->i_data, 0);
22054 + clear_inode(inode);
22059 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
22060 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
22062 +static int yaffs_file_flush(struct file *file)
22065 + yaffs_obj_t *obj = yaffs_dentry_to_obj(file->f_dentry);
22067 + yaffs_dev_t *dev = obj->my_dev;
22069 + T(YAFFS_TRACE_OS,
22070 + (TSTR("yaffs_file_flush object %d (%s)\n"), obj->obj_id,
22071 + obj->dirty ? "dirty" : "clean"));
22073 + yaffs_gross_lock(dev);
22075 + yaffs_flush_file(obj, 1, 0);
22077 + yaffs_gross_unlock(dev);
22082 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
22084 + /* Lifted from jffs2 */
22086 + yaffs_obj_t *obj;
22087 + unsigned char *pg_buf;
22090 + yaffs_dev_t *dev;
22092 + T(YAFFS_TRACE_OS,
22093 + (TSTR("yaffs_readpage_nolock at %08x, size %08x\n"),
22094 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
22095 + (unsigned)PAGE_CACHE_SIZE));
22097 + obj = yaffs_dentry_to_obj(f->f_dentry);
22099 + dev = obj->my_dev;
22101 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22102 + BUG_ON(!PageLocked(pg));
22104 + if (!PageLocked(pg))
22108 + pg_buf = kmap(pg);
22109 + /* FIXME: Can kmap fail? */
22111 + yaffs_gross_lock(dev);
22113 + ret = yaffs_file_rd(obj, pg_buf,
22114 + pg->index << PAGE_CACHE_SHIFT,
22115 + PAGE_CACHE_SIZE);
22117 + yaffs_gross_unlock(dev);
22123 + ClearPageUptodate(pg);
22124 + SetPageError(pg);
22126 + SetPageUptodate(pg);
22127 + ClearPageError(pg);
22130 + flush_dcache_page(pg);
22133 + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage_nolock done\n")));
22137 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
22139 + int ret = yaffs_readpage_nolock(f, pg);
22144 +static int yaffs_readpage(struct file *f, struct page *pg)
22148 + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage\n")));
22149 + ret=yaffs_readpage_unlock(f, pg);
22150 + T(YAFFS_TRACE_OS, (TSTR("yaffs_readpage done\n")));
22154 +/* writepage inspired by/stolen from smbfs */
22156 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22157 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
22159 +static int yaffs_writepage(struct page *page)
22162 + yaffs_dev_t *dev;
22163 + struct address_space *mapping = page->mapping;
22164 + struct inode *inode;
22165 + unsigned long end_index;
22167 + yaffs_obj_t *obj;
22168 + int nWritten = 0;
22169 + unsigned n_bytes;
22174 + inode = mapping->host;
22177 + i_size = i_size_read(inode);
22179 + end_index = i_size >> PAGE_CACHE_SHIFT;
22181 + if(page->index < end_index)
22182 + n_bytes = PAGE_CACHE_SIZE;
22184 + n_bytes = i_size & (PAGE_CACHE_SIZE -1);
22186 + if (page->index > end_index || !n_bytes) {
22187 + T(YAFFS_TRACE_OS,
22188 + (TSTR("yaffs_writepage at %08x, inode size = %08x!!!\n"),
22189 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
22190 + (unsigned)inode->i_size));
22191 + T(YAFFS_TRACE_OS,
22192 + (TSTR(" -> don't care!!\n")));
22194 + zero_user_segment(page,0,PAGE_CACHE_SIZE);
22195 + set_page_writeback(page);
22196 + unlock_page(page);
22197 + end_page_writeback(page);
22202 + if(n_bytes != PAGE_CACHE_SIZE)
22203 + zero_user_segment(page,n_bytes,PAGE_CACHE_SIZE);
22207 + buffer = kmap(page);
22209 + obj = yaffs_InodeToObject(inode);
22210 + dev = obj->my_dev;
22211 + yaffs_gross_lock(dev);
22213 + T(YAFFS_TRACE_OS,
22214 + (TSTR("yaffs_writepage at %08x, size %08x\n"),
22215 + (unsigned)(page->index << PAGE_CACHE_SHIFT), n_bytes));
22216 + T(YAFFS_TRACE_OS,
22217 + (TSTR("writepag0: obj = %05x, ino = %05x\n"),
22218 + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
22220 + nWritten = yaffs_wr_file(obj, buffer,
22221 + page->index << PAGE_CACHE_SHIFT, n_bytes, 0);
22223 + yaffs_touch_super(dev);
22225 + T(YAFFS_TRACE_OS,
22226 + (TSTR("writepag1: obj = %05x, ino = %05x\n"),
22227 + (int)obj->variant.file_variant.file_size, (int)inode->i_size));
22229 + yaffs_gross_unlock(dev);
22232 + set_page_writeback(page);
22233 + unlock_page(page);
22234 + end_page_writeback(page);
22237 + return (nWritten == n_bytes) ? 0 : -ENOSPC;
22241 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
22242 +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
22243 + loff_t pos, unsigned len, unsigned flags,
22244 + struct page **pagep, void **fsdata)
22246 + struct page *pg = NULL;
22247 + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
22250 + int space_held = 0;
22253 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
22254 + pg = grab_cache_page_write_begin(mapping, index, flags);
22256 + pg = __grab_cache_page(mapping, index);
22264 + T(YAFFS_TRACE_OS,
22265 + (TSTR("start yaffs_write_begin index %d(%x) uptodate %d\n"),
22266 + (int)index,(int)index,Page_Uptodate(pg) ? 1 : 0));
22268 + /* Get fs space */
22269 + space_held = yaffs_hold_space(filp);
22271 + if (!space_held) {
22276 + /* Update page if required */
22278 + if (!Page_Uptodate(pg))
22279 + ret = yaffs_readpage_nolock(filp, pg);
22284 + /* Happy path return */
22285 + T(YAFFS_TRACE_OS, (TSTR("end yaffs_write_begin - ok\n")));
22290 + T(YAFFS_TRACE_OS,
22291 + (TSTR("end yaffs_write_begin fail returning %d\n"), ret));
22293 + yaffs_release_space(filp);
22296 + page_cache_release(pg);
22303 +static int yaffs_prepare_write(struct file *f, struct page *pg,
22304 + unsigned offset, unsigned to)
22306 + T(YAFFS_TRACE_OS, (TSTR("yaffs_prepair_write\n")));
22308 + if (!Page_Uptodate(pg))
22309 + return yaffs_readpage_nolock(f, pg);
22314 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
22315 +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
22316 + loff_t pos, unsigned len, unsigned copied,
22317 + struct page *pg, void *fsdadata)
22320 + void *addr, *kva;
22321 + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
22324 + addr = kva + offset_into_page;
22326 + T(YAFFS_TRACE_OS,
22327 + ("yaffs_write_end addr %p pos %x n_bytes %d\n",
22328 + addr,(unsigned)pos, copied));
22330 + ret = yaffs_file_write(filp, addr, copied, &pos);
22332 + if (ret != copied) {
22333 + T(YAFFS_TRACE_OS,
22334 + (TSTR("yaffs_write_end not same size ret %d copied %d\n"),
22336 + SetPageError(pg);
22343 + yaffs_release_space(filp);
22345 + page_cache_release(pg);
22350 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
22353 + void *addr, *kva;
22355 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
22356 + int n_bytes = to - offset;
22359 + unsigned spos = pos;
22363 + addr = kva + offset;
22365 + saddr = (unsigned) addr;
22367 + T(YAFFS_TRACE_OS,
22368 + (TSTR("yaffs_commit_write addr %x pos %x n_bytes %d\n"),
22369 + saddr, spos, n_bytes));
22371 + nWritten = yaffs_file_write(f, addr, n_bytes, &pos);
22373 + if (nWritten != n_bytes) {
22374 + T(YAFFS_TRACE_OS,
22375 + (TSTR("yaffs_commit_write not same size nWritten %d n_bytes %d\n"),
22376 + nWritten, n_bytes));
22377 + SetPageError(pg);
22384 + T(YAFFS_TRACE_OS,
22385 + (TSTR("yaffs_commit_write returning %d\n"),
22386 + nWritten == n_bytes ? 0 : nWritten));
22388 + return nWritten == n_bytes ? 0 : nWritten;
22393 +static void yaffs_fill_inode_from_obj(struct inode *inode, yaffs_obj_t *obj)
22395 + if (inode && obj) {
22398 + /* Check mode against the variant type and attempt to repair if broken. */
22399 + __u32 mode = obj->yst_mode;
22400 + switch (obj->variant_type) {
22401 + case YAFFS_OBJECT_TYPE_FILE:
22402 + if (!S_ISREG(mode)) {
22403 + obj->yst_mode &= ~S_IFMT;
22404 + obj->yst_mode |= S_IFREG;
22408 + case YAFFS_OBJECT_TYPE_SYMLINK:
22409 + if (!S_ISLNK(mode)) {
22410 + obj->yst_mode &= ~S_IFMT;
22411 + obj->yst_mode |= S_IFLNK;
22415 + case YAFFS_OBJECT_TYPE_DIRECTORY:
22416 + if (!S_ISDIR(mode)) {
22417 + obj->yst_mode &= ~S_IFMT;
22418 + obj->yst_mode |= S_IFDIR;
22422 + case YAFFS_OBJECT_TYPE_UNKNOWN:
22423 + case YAFFS_OBJECT_TYPE_HARDLINK:
22424 + case YAFFS_OBJECT_TYPE_SPECIAL:
22430 + inode->i_flags |= S_NOATIME;
22432 + inode->i_ino = obj->obj_id;
22433 + inode->i_mode = obj->yst_mode;
22434 + inode->i_uid = obj->yst_uid;
22435 + inode->i_gid = obj->yst_gid;
22436 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
22437 + inode->i_blksize = inode->i_sb->s_blocksize;
22439 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22441 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
22442 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
22443 + inode->i_atime.tv_nsec = 0;
22444 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
22445 + inode->i_mtime.tv_nsec = 0;
22446 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
22447 + inode->i_ctime.tv_nsec = 0;
22449 + inode->i_rdev = obj->yst_rdev;
22450 + inode->i_atime = obj->yst_atime;
22451 + inode->i_mtime = obj->yst_mtime;
22452 + inode->i_ctime = obj->yst_ctime;
22454 + inode->i_size = yaffs_get_obj_length(obj);
22455 + inode->i_blocks = (inode->i_size + 511) >> 9;
22457 + inode->i_nlink = yaffs_get_obj_link_count(obj);
22459 + T(YAFFS_TRACE_OS,
22460 + (TSTR("yaffs_fill_inode mode %x uid %d gid %d size %d count %d\n"),
22461 + inode->i_mode, inode->i_uid, inode->i_gid,
22462 + (int)inode->i_size, atomic_read(&inode->i_count)));
22464 + switch (obj->yst_mode & S_IFMT) {
22465 + default: /* fifo, device or socket */
22466 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22467 + init_special_inode(inode, obj->yst_mode,
22468 + old_decode_dev(obj->yst_rdev));
22470 + init_special_inode(inode, obj->yst_mode,
22471 + (dev_t) (obj->yst_rdev));
22474 + case S_IFREG: /* file */
22475 + inode->i_op = &yaffs_file_inode_operations;
22476 + inode->i_fop = &yaffs_file_operations;
22477 + inode->i_mapping->a_ops =
22478 + &yaffs_file_address_operations;
22480 + case S_IFDIR: /* directory */
22481 + inode->i_op = &yaffs_dir_inode_operations;
22482 + inode->i_fop = &yaffs_dir_operations;
22484 + case S_IFLNK: /* symlink */
22485 + inode->i_op = &yaffs_symlink_inode_operations;
22489 + yaffs_InodeToObjectLV(inode) = obj;
22491 + obj->my_inode = inode;
22494 + T(YAFFS_TRACE_OS,
22495 + (TSTR("yaffs_FileInode invalid parameters\n")));
22500 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
22501 + yaffs_obj_t *obj)
22503 + struct inode *inode;
22506 + T(YAFFS_TRACE_OS,
22507 + (TSTR("yaffs_get_inode for NULL super_block!!\n")));
22513 + T(YAFFS_TRACE_OS,
22514 + (TSTR("yaffs_get_inode for NULL object!!\n")));
22519 + T(YAFFS_TRACE_OS,
22520 + (TSTR("yaffs_get_inode for object %d\n"), obj->obj_id));
22522 + inode = Y_IGET(sb, obj->obj_id);
22523 + if (IS_ERR(inode))
22526 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
22527 + /* iget also increments the inode's i_count */
22528 + /* NB You can't be holding grossLock or deadlock will happen! */
22533 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
22536 + yaffs_obj_t *obj;
22537 + int nWritten, ipos;
22538 + struct inode *inode;
22539 + yaffs_dev_t *dev;
22541 + obj = yaffs_dentry_to_obj(f->f_dentry);
22543 + dev = obj->my_dev;
22545 + yaffs_gross_lock(dev);
22547 + inode = f->f_dentry->d_inode;
22549 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
22550 + ipos = inode->i_size;
22555 + T(YAFFS_TRACE_OS,
22556 + (TSTR("yaffs_file_write: hey obj is null!\n")));
22558 + T(YAFFS_TRACE_OS,
22559 + (TSTR("yaffs_file_write about to write writing %u(%x) bytes"
22560 + "to object %d at %d(%x)\n"),
22561 + (unsigned) n, (unsigned) n, obj->obj_id, ipos,ipos));
22563 + nWritten = yaffs_wr_file(obj, buf, ipos, n, 0);
22565 + yaffs_touch_super(dev);
22567 + T(YAFFS_TRACE_OS,
22568 + (TSTR("yaffs_file_write: %d(%x) bytes written\n"),
22569 + (unsigned )n,(unsigned)n));
22571 + if (nWritten > 0) {
22572 + ipos += nWritten;
22574 + if (ipos > inode->i_size) {
22575 + inode->i_size = ipos;
22576 + inode->i_blocks = (ipos + 511) >> 9;
22578 + T(YAFFS_TRACE_OS,
22579 + (TSTR("yaffs_file_write size updated to %d bytes, "
22581 + ipos, (int)(inode->i_blocks)));
22585 + yaffs_gross_unlock(dev);
22586 + return (nWritten == 0) && (n > 0) ? -ENOSPC : nWritten;
22589 +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
22590 +/* For now we just assume few parallel writes and check against a small number. */
22591 +/* Todo: need to do this with a counter to handle parallel reads better */
22593 +static ssize_t yaffs_hold_space(struct file *f)
22595 + yaffs_obj_t *obj;
22596 + yaffs_dev_t *dev;
22598 + int n_free_chunks;
22601 + obj = yaffs_dentry_to_obj(f->f_dentry);
22603 + dev = obj->my_dev;
22605 + yaffs_gross_lock(dev);
22607 + n_free_chunks = yaffs_get_n_free_chunks(dev);
22609 + yaffs_gross_unlock(dev);
22611 + return (n_free_chunks > 20) ? 1 : 0;
22614 +static void yaffs_release_space(struct file *f)
22616 + yaffs_obj_t *obj;
22617 + yaffs_dev_t *dev;
22620 + obj = yaffs_dentry_to_obj(f->f_dentry);
22622 + dev = obj->my_dev;
22624 + yaffs_gross_lock(dev);
22627 + yaffs_gross_unlock(dev);
22631 +static loff_t yaffs_dir_llseek(struct file *file, loff_t offset, int origin)
22633 + long long retval;
22639 + offset += i_size_read(file->f_path.dentry->d_inode);
22642 + offset += file->f_pos;
22644 + retval = -EINVAL;
22646 + if (offset >= 0){
22647 + if (offset != file->f_pos)
22648 + file->f_pos = offset;
22657 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
22659 + yaffs_obj_t *obj;
22660 + yaffs_dev_t *dev;
22661 + struct yaffs_SearchContext *sc;
22662 + struct inode *inode = f->f_dentry->d_inode;
22663 + unsigned long offset, curoffs;
22667 + char name[YAFFS_MAX_NAME_LENGTH + 1];
22669 + obj = yaffs_dentry_to_obj(f->f_dentry);
22670 + dev = obj->my_dev;
22672 + yaffs_gross_lock(dev);
22674 + yaffs_dev_to_lc(dev)->readdirProcess = current;
22676 + offset = f->f_pos;
22678 + sc = yaffs_NewSearch(obj);
22680 + retVal = -ENOMEM;
22684 + T(YAFFS_TRACE_OS, (TSTR("yaffs_readdir: starting at %d\n"), (int)offset));
22686 + if (offset == 0) {
22687 + T(YAFFS_TRACE_OS,
22688 + (TSTR("yaffs_readdir: entry . ino %d \n"),
22689 + (int)inode->i_ino));
22690 + yaffs_gross_unlock(dev);
22691 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0){
22692 + yaffs_gross_lock(dev);
22695 + yaffs_gross_lock(dev);
22699 + if (offset == 1) {
22700 + T(YAFFS_TRACE_OS,
22701 + (TSTR("yaffs_readdir: entry .. ino %d \n"),
22702 + (int)f->f_dentry->d_parent->d_inode->i_ino));
22703 + yaffs_gross_unlock(dev);
22704 + if (filldir(dirent, "..", 2, offset,
22705 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0){
22706 + yaffs_gross_lock(dev);
22709 + yaffs_gross_lock(dev);
22716 + /* If the directory has changed since the open or last call to
22717 + readdir, rewind to after the 2 canned entries. */
22718 + if (f->f_version != inode->i_version) {
22720 + f->f_pos = offset;
22721 + f->f_version = inode->i_version;
22724 + while(sc->nextReturn){
22726 + l = sc->nextReturn;
22727 + if (curoffs >= offset) {
22728 + int this_inode = yaffs_get_obj_inode(l);
22729 + int this_type = yaffs_get_obj_type(l);
22731 + yaffs_get_obj_name(l, name,
22732 + YAFFS_MAX_NAME_LENGTH + 1);
22733 + T(YAFFS_TRACE_OS,
22734 + (TSTR("yaffs_readdir: %s inode %d\n"),
22735 + name, yaffs_get_obj_inode(l)));
22737 + yaffs_gross_unlock(dev);
22739 + if (filldir(dirent,
22745 + yaffs_gross_lock(dev);
22749 + yaffs_gross_lock(dev);
22754 + yaffs_search_advance(sc);
22758 + yaffs_search_end(sc);
22759 + yaffs_dev_to_lc(dev)->readdirProcess = NULL;
22760 + yaffs_gross_unlock(dev);
22768 + * File creation. Allocate an inode, and we're done..
22771 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
22772 +#define YCRED(x) x
22774 +#define YCRED(x) (x->cred)
22777 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22778 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
22781 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
22785 + struct inode *inode;
22787 + yaffs_obj_t *obj = NULL;
22788 + yaffs_dev_t *dev;
22790 + yaffs_obj_t *parent = yaffs_InodeToObject(dir);
22792 + int error = -ENOSPC;
22793 + uid_t uid = YCRED(current)->fsuid;
22794 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
22796 + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
22800 + T(YAFFS_TRACE_OS,
22801 + (TSTR("yaffs_mknod: parent object %d type %d\n"),
22802 + parent->obj_id, parent->variant_type));
22804 + T(YAFFS_TRACE_OS,
22805 + (TSTR("yaffs_mknod: could not get parent object\n")));
22809 + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making oject for %s, "
22810 + "mode %x dev %x\n"),
22811 + dentry->d_name.name, mode, rdev));
22813 + dev = parent->my_dev;
22815 + yaffs_gross_lock(dev);
22817 + switch (mode & S_IFMT) {
22819 + /* Special (socket, fifo, device...) */
22820 + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making special\n")));
22821 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22822 + obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
22823 + gid, old_encode_dev(rdev));
22825 + obj = yaffs_create_special(parent, dentry->d_name.name, mode, uid,
22829 + case S_IFREG: /* file */
22830 + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making file\n")));
22831 + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
22834 + case S_IFDIR: /* directory */
22835 + T(YAFFS_TRACE_OS,
22836 + (TSTR("yaffs_mknod: making directory\n")));
22837 + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
22840 + case S_IFLNK: /* symlink */
22841 + T(YAFFS_TRACE_OS, (TSTR("yaffs_mknod: making symlink\n")));
22842 + obj = NULL; /* Do we ever get here? */
22846 + /* Can not call yaffs_get_inode() with gross lock held */
22847 + yaffs_gross_unlock(dev);
22850 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
22851 + d_instantiate(dentry, inode);
22852 + update_dir_time(dir);
22853 + T(YAFFS_TRACE_OS,
22854 + (TSTR("yaffs_mknod created object %d count = %d\n"),
22855 + obj->obj_id, atomic_read(&inode->i_count)));
22857 + yaffs_fill_inode_from_obj(dir,parent);
22859 + T(YAFFS_TRACE_OS,
22860 + (TSTR("yaffs_mknod failed making object\n")));
22867 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
22870 + T(YAFFS_TRACE_OS, (TSTR("yaffs_mkdir\n")));
22871 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
22875 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
22876 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
22877 + struct nameidata *n)
22879 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
22882 + T(YAFFS_TRACE_OS,(TSTR("yaffs_create\n")));
22883 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
22886 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
22890 + yaffs_dev_t *dev;
22891 + yaffs_obj_t *obj;
22893 + T(YAFFS_TRACE_OS,
22894 + (TSTR("yaffs_unlink %d:%s\n"),
22895 + (int)(dir->i_ino),
22896 + dentry->d_name.name));
22897 + obj = yaffs_InodeToObject(dir);
22898 + dev = obj->my_dev;
22900 + yaffs_gross_lock(dev);
22902 + retVal = yaffs_unlinker(obj, dentry->d_name.name);
22904 + if (retVal == YAFFS_OK) {
22905 + dentry->d_inode->i_nlink--;
22906 + dir->i_version++;
22907 + yaffs_gross_unlock(dev);
22908 + mark_inode_dirty(dentry->d_inode);
22909 + update_dir_time(dir);
22912 + yaffs_gross_unlock(dev);
22913 + return -ENOTEMPTY;
22917 + * Create a link...
22919 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
22920 + struct dentry *dentry)
22922 + struct inode *inode = old_dentry->d_inode;
22923 + yaffs_obj_t *obj = NULL;
22924 + yaffs_obj_t *link = NULL;
22925 + yaffs_dev_t *dev;
22927 + T(YAFFS_TRACE_OS, (TSTR("yaffs_link\n")));
22929 + obj = yaffs_InodeToObject(inode);
22930 + dev = obj->my_dev;
22932 + yaffs_gross_lock(dev);
22934 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
22935 + link = yaffs_link_obj(yaffs_InodeToObject(dir), dentry->d_name.name,
22939 + old_dentry->d_inode->i_nlink = yaffs_get_obj_link_count(obj);
22940 + d_instantiate(dentry, old_dentry->d_inode);
22941 + atomic_inc(&old_dentry->d_inode->i_count);
22942 + T(YAFFS_TRACE_OS,
22943 + (TSTR("yaffs_link link count %d i_count %d\n"),
22944 + old_dentry->d_inode->i_nlink,
22945 + atomic_read(&old_dentry->d_inode->i_count)));
22948 + yaffs_gross_unlock(dev);
22951 + update_dir_time(dir);
22958 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
22959 + const char *symname)
22961 + yaffs_obj_t *obj;
22962 + yaffs_dev_t *dev;
22963 + uid_t uid = YCRED(current)->fsuid;
22964 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
22966 + T(YAFFS_TRACE_OS, (TSTR("yaffs_symlink\n")));
22968 + dev = yaffs_InodeToObject(dir)->my_dev;
22969 + yaffs_gross_lock(dev);
22970 + obj = yaffs_create_symlink(yaffs_InodeToObject(dir), dentry->d_name.name,
22971 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
22972 + yaffs_gross_unlock(dev);
22975 + struct inode *inode;
22977 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
22978 + d_instantiate(dentry, inode);
22979 + update_dir_time(dir);
22980 + T(YAFFS_TRACE_OS, (TSTR("symlink created OK\n")));
22983 + T(YAFFS_TRACE_OS, (TSTR("symlink not created\n")));
22989 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
22990 +static int yaffs_sync_object(struct file *file, int datasync)
22992 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
22997 + yaffs_obj_t *obj;
22998 + yaffs_dev_t *dev;
22999 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
23000 + struct dentry *dentry = file->f_path.dentry;
23003 + obj = yaffs_dentry_to_obj(dentry);
23005 + dev = obj->my_dev;
23007 + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
23008 + (TSTR("yaffs_sync_object\n")));
23009 + yaffs_gross_lock(dev);
23010 + yaffs_flush_file(obj, 1, datasync);
23011 + yaffs_gross_unlock(dev);
23016 + * The VFS layer already does all the dentry stuff for rename.
23018 + * NB: POSIX says you can rename an object over an old object of the same name
23020 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
23021 + struct inode *new_dir, struct dentry *new_dentry)
23023 + yaffs_dev_t *dev;
23024 + int retVal = YAFFS_FAIL;
23025 + yaffs_obj_t *target;
23027 + T(YAFFS_TRACE_OS, (TSTR("yaffs_rename\n")));
23028 + dev = yaffs_InodeToObject(old_dir)->my_dev;
23030 + yaffs_gross_lock(dev);
23032 + /* Check if the target is an existing directory that is not empty. */
23033 + target = yaffs_find_by_name(yaffs_InodeToObject(new_dir),
23034 + new_dentry->d_name.name);
23038 + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
23039 + !ylist_empty(&target->variant.dir_variant.children)) {
23041 + T(YAFFS_TRACE_OS, (TSTR("target is non-empty dir\n")));
23043 + retVal = YAFFS_FAIL;
23045 + /* Now does unlinking internally using shadowing mechanism */
23046 + T(YAFFS_TRACE_OS, (TSTR("calling yaffs_rename_obj\n")));
23048 + retVal = yaffs_rename_obj(yaffs_InodeToObject(old_dir),
23049 + old_dentry->d_name.name,
23050 + yaffs_InodeToObject(new_dir),
23051 + new_dentry->d_name.name);
23053 + yaffs_gross_unlock(dev);
23055 + if (retVal == YAFFS_OK) {
23057 + new_dentry->d_inode->i_nlink--;
23058 + mark_inode_dirty(new_dentry->d_inode);
23061 + update_dir_time(old_dir);
23062 + if(old_dir != new_dir)
23063 + update_dir_time(new_dir);
23066 + return -ENOTEMPTY;
23070 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
23072 + struct inode *inode = dentry->d_inode;
23074 + yaffs_dev_t *dev;
23076 + T(YAFFS_TRACE_OS,
23077 + (TSTR("yaffs_setattr of object %d\n"),
23078 + yaffs_InodeToObject(inode)->obj_id));
23080 + /* Fail if a requested resize >= 2GB */
23081 + if (attr->ia_valid & ATTR_SIZE &&
23082 + (attr->ia_size >> 31))
23086 + error = inode_change_ok(inode, attr);
23087 + if (error == 0) {
23090 + error = yaffs_vfs_setattr(inode, attr);
23091 + T(YAFFS_TRACE_OS,(TSTR("inode_setattr called\n")));
23092 + if (attr->ia_valid & ATTR_SIZE){
23093 + yaffs_vfs_setsize(inode,attr->ia_size);
23094 + inode->i_blocks = (inode->i_size + 511) >> 9;
23097 + dev = yaffs_InodeToObject(inode)->my_dev;
23098 + if (attr->ia_valid & ATTR_SIZE){
23099 + T(YAFFS_TRACE_OS,(TSTR("resize to %d(%x)\n"),
23100 + (int)(attr->ia_size),(int)(attr->ia_size)));
23102 + yaffs_gross_lock(dev);
23103 + result = yaffs_set_attribs(yaffs_InodeToObject(inode), attr);
23104 + if(result == YAFFS_OK) {
23109 + yaffs_gross_unlock(dev);
23113 + T(YAFFS_TRACE_OS,
23114 + (TSTR("yaffs_setattr done returning %d\n"),error));
23119 +#ifdef CONFIG_YAFFS_XATTR
23120 +int yaffs_setxattr(struct dentry *dentry, const char *name,
23121 + const void *value, size_t size, int flags)
23123 + struct inode *inode = dentry->d_inode;
23125 + yaffs_dev_t *dev;
23126 + yaffs_obj_t *obj = yaffs_InodeToObject(inode);
23128 + T(YAFFS_TRACE_OS,
23129 + (TSTR("yaffs_setxattr of object %d\n"),
23133 + if (error == 0) {
23135 + dev = obj->my_dev;
23136 + yaffs_gross_lock(dev);
23137 + result = yaffs_set_xattrib(obj, name, value, size, flags);
23138 + if(result == YAFFS_OK)
23140 + else if(result < 0)
23142 + yaffs_gross_unlock(dev);
23145 + T(YAFFS_TRACE_OS,
23146 + (TSTR("yaffs_setxattr done returning %d\n"),error));
23152 +ssize_t yaffs_getxattr(struct dentry *dentry, const char *name, void *buff,
23155 + struct inode *inode = dentry->d_inode;
23157 + yaffs_dev_t *dev;
23158 + yaffs_obj_t *obj = yaffs_InodeToObject(inode);
23160 + T(YAFFS_TRACE_OS,
23161 + (TSTR("yaffs_getxattr \"%s\" from object %d\n"),
23162 + name, obj->obj_id));
23164 + if (error == 0) {
23165 + dev = obj->my_dev;
23166 + yaffs_gross_lock(dev);
23167 + error = yaffs_get_xattrib(obj, name, buff, size);
23168 + yaffs_gross_unlock(dev);
23171 + T(YAFFS_TRACE_OS,
23172 + (TSTR("yaffs_getxattr done returning %d\n"),error));
23177 +int yaffs_removexattr(struct dentry *dentry, const char *name)
23179 + struct inode *inode = dentry->d_inode;
23181 + yaffs_dev_t *dev;
23182 + yaffs_obj_t *obj = yaffs_InodeToObject(inode);
23184 + T(YAFFS_TRACE_OS,
23185 + (TSTR("yaffs_removexattr of object %d\n"),
23189 + if (error == 0) {
23191 + dev = obj->my_dev;
23192 + yaffs_gross_lock(dev);
23193 + result = yaffs_remove_xattrib(obj, name);
23194 + if(result == YAFFS_OK)
23196 + else if(result < 0)
23198 + yaffs_gross_unlock(dev);
23201 + T(YAFFS_TRACE_OS,
23202 + (TSTR("yaffs_removexattr done returning %d\n"),error));
23207 +ssize_t yaffs_listxattr(struct dentry *dentry, char *buff, size_t size)
23209 + struct inode *inode = dentry->d_inode;
23211 + yaffs_dev_t *dev;
23212 + yaffs_obj_t *obj = yaffs_InodeToObject(inode);
23214 + T(YAFFS_TRACE_OS,
23215 + (TSTR("yaffs_listxattr of object %d\n"),
23219 + if (error == 0) {
23220 + dev = obj->my_dev;
23221 + yaffs_gross_lock(dev);
23222 + error = yaffs_list_xattrib(obj, buff, size);
23223 + yaffs_gross_unlock(dev);
23226 + T(YAFFS_TRACE_OS,
23227 + (TSTR("yaffs_listxattr done returning %d\n"),error));
23235 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
23236 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
23238 + yaffs_dev_t *dev = yaffs_dentry_to_obj(dentry)->my_dev;
23239 + struct super_block *sb = dentry->d_sb;
23240 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
23241 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
23243 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23245 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
23247 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23250 + T(YAFFS_TRACE_OS, (TSTR("yaffs_statfs\n")));
23252 + yaffs_gross_lock(dev);
23254 + buf->f_type = YAFFS_MAGIC;
23255 + buf->f_bsize = sb->s_blocksize;
23256 + buf->f_namelen = 255;
23258 + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
23259 + /* Do this if chunk size is not a power of 2 */
23261 + uint64_t bytesInDev;
23262 + uint64_t bytesFree;
23264 + bytesInDev = ((uint64_t)((dev->param.end_block - dev->param.start_block + 1))) *
23265 + ((uint64_t)(dev->param.chunks_per_block * dev->data_bytes_per_chunk));
23267 + do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
23268 + buf->f_blocks = bytesInDev;
23270 + bytesFree = ((uint64_t)(yaffs_get_n_free_chunks(dev))) *
23271 + ((uint64_t)(dev->data_bytes_per_chunk));
23273 + do_div(bytesFree, sb->s_blocksize);
23275 + buf->f_bfree = bytesFree;
23277 + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
23280 + (dev->param.end_block - dev->param.start_block + 1) *
23281 + dev->param.chunks_per_block /
23282 + (sb->s_blocksize / dev->data_bytes_per_chunk);
23284 + yaffs_get_n_free_chunks(dev) /
23285 + (sb->s_blocksize / dev->data_bytes_per_chunk);
23288 + (dev->param.end_block - dev->param.start_block + 1) *
23289 + dev->param.chunks_per_block *
23290 + (dev->data_bytes_per_chunk / sb->s_blocksize);
23293 + yaffs_get_n_free_chunks(dev) *
23294 + (dev->data_bytes_per_chunk / sb->s_blocksize);
23297 + buf->f_files = 0;
23298 + buf->f_ffree = 0;
23299 + buf->f_bavail = buf->f_bfree;
23301 + yaffs_gross_unlock(dev);
23307 +static void yaffs_flush_inodes(struct super_block *sb)
23309 + struct inode *iptr;
23310 + yaffs_obj_t *obj;
23312 + list_for_each_entry(iptr,&sb->s_inodes, i_sb_list){
23313 + obj = yaffs_InodeToObject(iptr);
23315 + T(YAFFS_TRACE_OS, (TSTR("flushing obj %d\n"),
23317 + yaffs_flush_file(obj,1,0);
23323 +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
23325 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23329 + yaffs_flush_inodes(sb);
23330 + yaffs_update_dirty_dirs(dev);
23331 + yaffs_flush_whole_cache(dev);
23332 + if(do_checkpoint)
23333 + yaffs_checkpoint_save(dev);
23337 +static unsigned yaffs_bg_gc_urgency(yaffs_dev_t *dev)
23339 + unsigned erasedChunks = dev->n_erased_blocks * dev->param.chunks_per_block;
23340 + struct yaffs_LinuxContext *context = yaffs_dev_to_lc(dev);
23341 + unsigned scatteredFree = 0; /* Free chunks not in an erased block */
23343 + if(erasedChunks < dev->n_free_chunks)
23344 + scatteredFree = (dev->n_free_chunks - erasedChunks);
23346 + if(!context->bgRunning)
23348 + else if(scatteredFree < (dev->param.chunks_per_block * 2))
23350 + else if(erasedChunks > dev->n_free_chunks/2)
23352 + else if(erasedChunks > dev->n_free_chunks/4)
23358 +static int yaffs_do_sync_fs(struct super_block *sb,
23359 + int request_checkpoint)
23362 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23363 + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
23364 + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
23365 + int do_checkpoint;
23367 + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
23368 + (TSTR("yaffs_do_sync_fs: gc-urgency %d %s %s%s\n"),
23370 + sb->s_dirt ? "dirty" : "clean",
23371 + request_checkpoint ? "checkpoint requested" : "no checkpoint",
23372 + oneshot_checkpoint ? " one-shot" : "" ));
23374 + yaffs_gross_lock(dev);
23375 + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
23376 + oneshot_checkpoint) &&
23377 + !dev->is_checkpointed;
23379 + if (sb->s_dirt || do_checkpoint) {
23380 + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
23382 + if(oneshot_checkpoint)
23383 + yaffs_auto_checkpoint &= ~4;
23385 + yaffs_gross_unlock(dev);
23391 + * yaffs background thread functions .
23392 + * yaffs_bg_thread_fn() the thread function
23393 + * yaffs_bg_start() launches the background thread.
23394 + * yaffs_bg_stop() cleans up the background thread.
23397 + * The thread should only run after the yaffs is initialised
23398 + * The thread should be stopped before yaffs is unmounted.
23399 + * The thread should not do any writing while the fs is in read only.
23402 +#ifdef YAFFS_COMPILE_BACKGROUND
23404 +void yaffs_background_waker(unsigned long data)
23406 + wake_up_process((struct task_struct *)data);
23409 +static int yaffs_bg_thread_fn(void *data)
23411 + yaffs_dev_t *dev = (yaffs_dev_t *)data;
23412 + struct yaffs_LinuxContext *context = yaffs_dev_to_lc(dev);
23413 + unsigned long now = jiffies;
23414 + unsigned long next_dir_update = now;
23415 + unsigned long next_gc = now;
23416 + unsigned long expires;
23417 + unsigned int urgency;
23420 + struct timer_list timer;
23422 + T(YAFFS_TRACE_BACKGROUND,
23423 + (TSTR("yaffs_background starting for dev %p\n"),
23426 +#ifdef YAFFS_COMPILE_FREEZER
23429 + while(context->bgRunning){
23430 + T(YAFFS_TRACE_BACKGROUND,
23431 + (TSTR("yaffs_background\n")));
23433 + if(kthread_should_stop())
23436 +#ifdef YAFFS_COMPILE_FREEZER
23437 + if(try_to_freeze())
23440 + yaffs_gross_lock(dev);
23444 + if(time_after(now, next_dir_update) && yaffs_bg_enable){
23445 + yaffs_update_dirty_dirs(dev);
23446 + next_dir_update = now + HZ;
23449 + if(time_after(now,next_gc) && yaffs_bg_enable){
23450 + if(!dev->is_checkpointed){
23451 + urgency = yaffs_bg_gc_urgency(dev);
23452 + gcResult = yaffs_bg_gc(dev, urgency);
23454 + next_gc = now + HZ/20+1;
23455 + else if(urgency > 0)
23456 + next_gc = now + HZ/10+1;
23458 + next_gc = now + HZ * 2;
23460 + * gc not running so set to next_dir_update
23461 + * to cut down on wake ups
23463 + next_gc = next_dir_update;
23465 + yaffs_gross_unlock(dev);
23467 + expires = next_dir_update;
23468 + if (time_before(next_gc,expires))
23469 + expires = next_gc;
23470 + if(time_before(expires,now))
23471 + expires = now + HZ;
23473 + Y_INIT_TIMER(&timer);
23474 + timer.expires = expires+1;
23475 + timer.data = (unsigned long) current;
23476 + timer.function = yaffs_background_waker;
23478 + set_current_state(TASK_INTERRUPTIBLE);
23479 + add_timer(&timer);
23481 + del_timer_sync(&timer);
23490 +static int yaffs_bg_start(yaffs_dev_t *dev)
23493 + struct yaffs_LinuxContext *context = yaffs_dev_to_lc(dev);
23495 + if(dev->read_only)
23498 + context->bgRunning = 1;
23500 + context->bgThread = kthread_run(yaffs_bg_thread_fn,
23501 + (void *)dev,"yaffs-bg-%d",context->mount_id);
23503 + if(IS_ERR(context->bgThread)){
23504 + retval = PTR_ERR(context->bgThread);
23505 + context->bgThread = NULL;
23506 + context->bgRunning = 0;
23511 +static void yaffs_bg_stop(yaffs_dev_t *dev)
23513 + struct yaffs_LinuxContext *ctxt = yaffs_dev_to_lc(dev);
23515 + ctxt->bgRunning = 0;
23517 + if( ctxt->bgThread){
23518 + kthread_stop(ctxt->bgThread);
23519 + ctxt->bgThread = NULL;
23523 +static int yaffs_bg_thread_fn(void *data)
23528 +static int yaffs_bg_start(yaffs_dev_t *dev)
23533 +static void yaffs_bg_stop(yaffs_dev_t *dev)
23539 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
23540 +static void yaffs_write_super(struct super_block *sb)
23542 +static int yaffs_write_super(struct super_block *sb)
23545 + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
23547 + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
23548 + (TSTR("yaffs_write_super%s\n"),
23549 + request_checkpoint ? " checkpt" : ""));
23551 + yaffs_do_sync_fs(sb, request_checkpoint);
23553 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
23559 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
23560 +static int yaffs_sync_fs(struct super_block *sb, int wait)
23562 +static int yaffs_sync_fs(struct super_block *sb)
23565 + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
23567 + T(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
23568 + (TSTR("yaffs_sync_fs%s\n"),
23569 + request_checkpoint ? " checkpt" : ""));
23571 + yaffs_do_sync_fs(sb, request_checkpoint);
23576 +#ifdef YAFFS_USE_OWN_IGET
23578 +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
23580 + struct inode *inode;
23581 + yaffs_obj_t *obj;
23582 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23584 + T(YAFFS_TRACE_OS,
23585 + (TSTR("yaffs_iget for %lu\n"), ino));
23587 + inode = iget_locked(sb, ino);
23589 + return ERR_PTR(-ENOMEM);
23590 + if (!(inode->i_state & I_NEW))
23593 + /* NB This is called as a side effect of other functions, but
23594 + * we had to release the lock to prevent deadlocks, so
23595 + * need to lock again.
23598 + yaffs_gross_lock(dev);
23600 + obj = yaffs_find_by_number(dev, inode->i_ino);
23602 + yaffs_fill_inode_from_obj(inode, obj);
23604 + yaffs_gross_unlock(dev);
23606 + unlock_new_inode(inode);
23612 +static void yaffs_read_inode(struct inode *inode)
23614 + /* NB This is called as a side effect of other functions, but
23615 + * we had to release the lock to prevent deadlocks, so
23616 + * need to lock again.
23619 + yaffs_obj_t *obj;
23620 + yaffs_dev_t *dev = yaffs_SuperToDevice(inode->i_sb);
23622 + T(YAFFS_TRACE_OS,
23623 + (TSTR("yaffs_read_inode for %d\n"), (int)inode->i_ino));
23625 + if(current != yaffs_dev_to_lc(dev)->readdirProcess)
23626 + yaffs_gross_lock(dev);
23628 + obj = yaffs_find_by_number(dev, inode->i_ino);
23630 + yaffs_fill_inode_from_obj(inode, obj);
23632 + if(current != yaffs_dev_to_lc(dev)->readdirProcess)
23633 + yaffs_gross_unlock(dev);
23638 +static YLIST_HEAD(yaffs_context_list);
23639 +struct semaphore yaffs_context_lock;
23641 +static void yaffs_put_super(struct super_block *sb)
23643 + yaffs_dev_t *dev = yaffs_SuperToDevice(sb);
23645 + T(YAFFS_TRACE_OS, (TSTR("yaffs_put_super\n")));
23647 + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
23648 + (TSTR("Shutting down yaffs background thread\n")));
23649 + yaffs_bg_stop(dev);
23650 + T(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
23651 + (TSTR("yaffs background thread shut down\n")));
23653 + yaffs_gross_lock(dev);
23655 + yaffs_flush_super(sb,1);
23657 + if (yaffs_dev_to_lc(dev)->putSuperFunc)
23658 + yaffs_dev_to_lc(dev)->putSuperFunc(sb);
23661 + yaffs_deinitialise(dev);
23663 + yaffs_gross_unlock(dev);
23665 + down(&yaffs_context_lock);
23666 + ylist_del_init(&(yaffs_dev_to_lc(dev)->contextList));
23667 + up(&yaffs_context_lock);
23669 + if (yaffs_dev_to_lc(dev)->spareBuffer) {
23670 + YFREE(yaffs_dev_to_lc(dev)->spareBuffer);
23671 + yaffs_dev_to_lc(dev)->spareBuffer = NULL;
23678 +static void yaffs_MTDPutSuper(struct super_block *sb)
23680 + struct mtd_info *mtd = yaffs_dev_to_mtd(yaffs_SuperToDevice(sb));
23685 + put_mtd_device(mtd);
23689 +static void yaffs_touch_super(yaffs_dev_t *dev)
23691 + struct super_block *sb = yaffs_dev_to_lc(dev)->superBlock;
23693 + T(YAFFS_TRACE_OS, (TSTR("yaffs_touch_super() sb = %p\n"), sb));
23700 + int skip_checkpoint_read;
23701 + int skip_checkpoint_write;
23704 + int tags_ecc_overridden;
23705 + int lazy_loading_enabled;
23706 + int lazy_loading_overridden;
23707 + int empty_lost_and_found;
23708 + int empty_lost_and_found_overridden;
23711 +#define MAX_OPT_LEN 30
23712 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
23714 + char cur_opt[MAX_OPT_LEN + 1];
23718 + /* Parse through the options which is a comma seperated list */
23720 + while (options_str && *options_str && !error) {
23721 + memset(cur_opt, 0, MAX_OPT_LEN + 1);
23724 + while(*options_str == ',')
23727 + while (*options_str && *options_str != ',') {
23728 + if (p < MAX_OPT_LEN) {
23729 + cur_opt[p] = *options_str;
23735 + if (!strcmp(cur_opt, "inband-tags"))
23736 + options->inband_tags = 1;
23737 + else if (!strcmp(cur_opt, "tags-ecc-off")){
23738 + options->tags_ecc_on = 0;
23739 + options->tags_ecc_overridden=1;
23740 + } else if (!strcmp(cur_opt, "tags-ecc-on")){
23741 + options->tags_ecc_on = 1;
23742 + options->tags_ecc_overridden = 1;
23743 + } else if (!strcmp(cur_opt, "lazy-loading-off")){
23744 + options->lazy_loading_enabled = 0;
23745 + options->lazy_loading_overridden=1;
23746 + } else if (!strcmp(cur_opt, "lazy-loading-on")){
23747 + options->lazy_loading_enabled = 1;
23748 + options->lazy_loading_overridden = 1;
23749 + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")){
23750 + options->empty_lost_and_found = 0;
23751 + options->empty_lost_and_found_overridden=1;
23752 + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")){
23753 + options->empty_lost_and_found = 1;
23754 + options->empty_lost_and_found_overridden=1;
23755 + } else if (!strcmp(cur_opt, "no-cache"))
23756 + options->no_cache = 1;
23757 + else if (!strcmp(cur_opt, "no-checkpoint-read"))
23758 + options->skip_checkpoint_read = 1;
23759 + else if (!strcmp(cur_opt, "no-checkpoint-write"))
23760 + options->skip_checkpoint_write = 1;
23761 + else if (!strcmp(cur_opt, "no-checkpoint")) {
23762 + options->skip_checkpoint_read = 1;
23763 + options->skip_checkpoint_write = 1;
23765 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
23774 +static struct super_block *yaffs_internal_read_super(int yaffs_version,
23775 + struct super_block *sb,
23776 + void *data, int silent)
23779 + struct inode *inode = NULL;
23780 + struct dentry *root;
23781 + yaffs_dev_t *dev = 0;
23782 + char devname_buf[BDEVNAME_SIZE + 1];
23783 + struct mtd_info *mtd;
23785 + char *data_str = (char *)data;
23786 + struct yaffs_LinuxContext *context = NULL;
23787 + yaffs_param_t *param;
23789 + int read_only = 0;
23791 + yaffs_options options;
23793 + unsigned mount_id;
23795 + struct yaffs_LinuxContext *context_iterator;
23796 + struct ylist_head *l;
23798 + sb->s_magic = YAFFS_MAGIC;
23799 + sb->s_op = &yaffs_super_ops;
23800 + sb->s_flags |= MS_NOATIME;
23802 + read_only =((sb->s_flags & MS_RDONLY) != 0);
23805 +#ifdef YAFFS_COMPILE_EXPORTFS
23806 + sb->s_export_op = &yaffs_export_ops;
23810 + printk(KERN_INFO "yaffs: sb is NULL\n");
23811 + else if (!sb->s_dev)
23812 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
23813 + else if (!yaffs_devname(sb, devname_buf))
23814 + printk(KERN_INFO "yaffs: devname is NULL\n");
23816 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
23818 + yaffs_devname(sb, devname_buf),
23819 + read_only ? "ro" : "rw");
23824 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
23826 + memset(&options, 0, sizeof(options));
23828 + if (yaffs_parse_options(&options, data_str)) {
23829 + /* Option parsing failed */
23834 + sb->s_blocksize = PAGE_CACHE_SIZE;
23835 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
23837 + T(YAFFS_TRACE_OS,
23838 + (TSTR("yaffs_read_super: Using yaffs%d\n"), yaffs_version));
23839 + T(YAFFS_TRACE_OS,
23840 + (TSTR("yaffs_read_super: block size %d\n"),
23841 + (int)(sb->s_blocksize)));
23843 + T(YAFFS_TRACE_ALWAYS,
23844 + (TSTR("yaffs: Attempting MTD mount of %u.%u,\"%s\"\n"),
23845 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
23846 + yaffs_devname(sb, devname_buf)));
23848 + /* Check it's an mtd device..... */
23849 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
23850 + return NULL; /* This isn't an mtd device */
23852 + /* Get the device */
23853 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
23855 + T(YAFFS_TRACE_ALWAYS,
23856 + (TSTR("yaffs: MTD device #%u doesn't appear to exist\n"),
23857 + MINOR(sb->s_dev)));
23860 + /* Check it's NAND */
23861 + if (mtd->type != MTD_NANDFLASH) {
23862 + T(YAFFS_TRACE_ALWAYS,
23863 + (TSTR("yaffs: MTD device is not NAND it's type %d\n"),
23868 + T(YAFFS_TRACE_OS, (TSTR(" erase %p\n"), mtd->erase));
23869 + T(YAFFS_TRACE_OS, (TSTR(" read %p\n"), mtd->read));
23870 + T(YAFFS_TRACE_OS, (TSTR(" write %p\n"), mtd->write));
23871 + T(YAFFS_TRACE_OS, (TSTR(" readoob %p\n"), mtd->read_oob));
23872 + T(YAFFS_TRACE_OS, (TSTR(" writeoob %p\n"), mtd->write_oob));
23873 + T(YAFFS_TRACE_OS, (TSTR(" block_isbad %p\n"), mtd->block_isbad));
23874 + T(YAFFS_TRACE_OS, (TSTR(" block_markbad %p\n"), mtd->block_markbad));
23875 + T(YAFFS_TRACE_OS, (TSTR(" %s %d\n"), WRITE_SIZE_STR, WRITE_SIZE(mtd)));
23876 + T(YAFFS_TRACE_OS, (TSTR(" oobsize %d\n"), mtd->oobsize));
23877 + T(YAFFS_TRACE_OS, (TSTR(" erasesize %d\n"), mtd->erasesize));
23878 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
23879 + T(YAFFS_TRACE_OS, (TSTR(" size %u\n"), mtd->size));
23881 + T(YAFFS_TRACE_OS, (TSTR(" size %lld\n"), mtd->size));
23884 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
23886 + if (yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
23887 + T(YAFFS_TRACE_ALWAYS,
23888 + (TSTR("yaffs: auto selecting yaffs2\n")));
23889 + yaffs_version = 2;
23892 + /* Added NCB 26/5/2006 for completeness */
23893 + if (yaffs_version == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
23894 + T(YAFFS_TRACE_ALWAYS,
23895 + (TSTR("yaffs: auto selecting yaffs1\n")));
23896 + yaffs_version = 1;
23901 + if (yaffs_version == 2) {
23902 + /* Check for version 2 style functions */
23903 + if (!mtd->erase ||
23904 + !mtd->block_isbad ||
23905 + !mtd->block_markbad ||
23908 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
23909 + !mtd->read_oob || !mtd->write_oob) {
23911 + !mtd->write_ecc ||
23912 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
23914 + T(YAFFS_TRACE_ALWAYS,
23915 + (TSTR("yaffs: MTD device does not support required "
23916 + "functions\n")));
23920 + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
23921 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
23922 + !options.inband_tags) {
23923 + T(YAFFS_TRACE_ALWAYS,
23924 + (TSTR("yaffs: MTD device does not have the "
23925 + "right page sizes\n")));
23929 + /* Check for V1 style functions */
23930 + if (!mtd->erase ||
23933 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
23934 + !mtd->read_oob || !mtd->write_oob) {
23936 + !mtd->write_ecc ||
23937 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
23939 + T(YAFFS_TRACE_ALWAYS,
23940 + (TSTR("yaffs: MTD device does not support required "
23941 + "functions\n")));
23945 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
23946 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
23947 + T(YAFFS_TRACE_ALWAYS,
23948 + (TSTR("yaffs: MTD device does not support have the "
23949 + "right page sizes\n")));
23954 + /* OK, so if we got here, we have an MTD that's NAND and looks
23955 + * like it has the right capabilities
23956 + * Set the yaffs_dev_t up for mtd
23959 + if (!read_only && !(mtd->flags & MTD_WRITEABLE)){
23961 + printk(KERN_INFO "yaffs: mtd is read only, setting superblock read only");
23962 + sb->s_flags |= MS_RDONLY;
23965 + dev = kmalloc(sizeof(yaffs_dev_t), GFP_KERNEL);
23966 + context = kmalloc(sizeof(struct yaffs_LinuxContext),GFP_KERNEL);
23968 + if(!dev || !context ){
23978 + /* Deep shit could not allocate device structure */
23979 + T(YAFFS_TRACE_ALWAYS,
23980 + (TSTR("yaffs_read_super: Failed trying to allocate "
23981 + "yaffs_dev_t. \n")));
23984 + memset(dev, 0, sizeof(yaffs_dev_t));
23985 + param = &(dev->param);
23987 + memset(context,0,sizeof(struct yaffs_LinuxContext));
23988 + dev->os_context = context;
23989 + YINIT_LIST_HEAD(&(context->contextList));
23990 + context->dev = dev;
23991 + context->superBlock = sb;
23993 + dev->read_only = read_only;
23995 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
23996 + sb->s_fs_info = dev;
23998 + sb->u.generic_sbp = dev;
24001 + dev->driver_context = mtd;
24002 + param->name = mtd->name;
24004 + /* Set up the memory size parameters.... */
24006 + nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
24008 + param->start_block = 0;
24009 + param->end_block = nBlocks - 1;
24010 + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
24011 + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
24012 + param->n_reserved_blocks = 5;
24013 + param->n_caches = (options.no_cache) ? 0 : 10;
24014 + param->inband_tags = options.inband_tags;
24016 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
24017 + param->disable_lazy_load = 1;
24019 +#ifdef CONFIG_YAFFS_XATTR
24020 + param->enable_xattr = 1;
24022 + if(options.lazy_loading_overridden)
24023 + param->disable_lazy_load = !options.lazy_loading_enabled;
24025 +#ifdef CONFIG_YAFFS_DISABLE_TAGS_ECC
24026 + param->no_tags_ecc = 1;
24029 +#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND
24031 + param->defered_dir_update = 1;
24034 + if(options.tags_ecc_overridden)
24035 + param->no_tags_ecc = !options.tags_ecc_on;
24037 +#ifdef CONFIG_YAFFS_EMPTY_LOST_AND_FOUND
24038 + param->empty_lost_n_found = 1;
24041 +#ifdef CONFIG_YAFFS_DISABLE_BLOCK_REFRESHING
24042 + param->refresh_period = 0;
24044 + param->refresh_period = 500;
24047 +#ifdef CONFIG_YAFFS__ALWAYS_CHECK_CHUNK_ERASED
24048 + param->always_check_erased = 1;
24051 + if(options.empty_lost_and_found_overridden)
24052 + param->empty_lost_n_found = options.empty_lost_and_found;
24054 + /* ... and the functions. */
24055 + if (yaffs_version == 2) {
24056 + param->write_chunk_tags_fn =
24057 + nandmtd2_WriteChunkWithTagsToNAND;
24058 + param->read_chunk_tags_fn =
24059 + nandmtd2_ReadChunkWithTagsFromNAND;
24060 + param->bad_block_fn = nandmtd2_MarkNANDBlockBad;
24061 + param->query_block_fn = nandmtd2_QueryNANDBlock;
24062 + yaffs_dev_to_lc(dev)->spareBuffer = YMALLOC(mtd->oobsize);
24063 + param->is_yaffs2 = 1;
24064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
24065 + param->total_bytes_per_chunk = mtd->writesize;
24066 + param->chunks_per_block = mtd->erasesize / mtd->writesize;
24068 + param->total_bytes_per_chunk = mtd->oobblock;
24069 + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
24071 + nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
24073 + param->start_block = 0;
24074 + param->end_block = nBlocks - 1;
24076 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
24077 + /* use the MTD interface in yaffs_mtdif1.c */
24078 + param->write_chunk_tags_fn =
24079 + nandmtd1_WriteChunkWithTagsToNAND;
24080 + param->read_chunk_tags_fn =
24081 + nandmtd1_ReadChunkWithTagsFromNAND;
24082 + param->bad_block_fn = nandmtd1_MarkNANDBlockBad;
24083 + param->query_block_fn = nandmtd1_QueryNANDBlock;
24085 + param->write_chunk_fn = nandmtd_WriteChunkToNAND;
24086 + param->read_chunk_fn = nandmtd_ReadChunkFromNAND;
24088 + param->is_yaffs2 = 0;
24090 + /* ... and common functions */
24091 + param->erase_fn = nandmtd_EraseBlockInNAND;
24092 + param->initialise_flash_fn = nandmtd_InitialiseNAND;
24094 + yaffs_dev_to_lc(dev)->putSuperFunc = yaffs_MTDPutSuper;
24096 + param->sb_dirty_fn = yaffs_touch_super;
24097 + param->gc_control = yaffs_gc_control_callback;
24099 + yaffs_dev_to_lc(dev)->superBlock= sb;
24102 +#ifndef CONFIG_YAFFS_DOES_ECC
24103 + param->use_nand_ecc = 1;
24106 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
24107 + param->wide_tnodes_disabled = 1;
24110 + param->skip_checkpt_rd = options.skip_checkpoint_read;
24111 + param->skip_checkpt_wr = options.skip_checkpoint_write;
24113 + down(&yaffs_context_lock);
24114 + /* Get a mount id */
24116 + for(mount_id=0; ! found; mount_id++){
24118 + ylist_for_each(l,&yaffs_context_list){
24119 + context_iterator = ylist_entry(l,struct yaffs_LinuxContext,contextList);
24120 + if(context_iterator->mount_id == mount_id)
24124 + context->mount_id = mount_id;
24126 + ylist_add_tail(&(yaffs_dev_to_lc(dev)->contextList), &yaffs_context_list);
24127 + up(&yaffs_context_lock);
24129 + /* Directory search handling...*/
24130 + YINIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->searchContexts));
24131 + param->remove_obj_fn = yaffs_remove_obj_callback;
24133 + init_MUTEX(&(yaffs_dev_to_lc(dev)->grossLock));
24135 + yaffs_gross_lock(dev);
24137 + err = yaffs_guts_initialise(dev);
24139 + T(YAFFS_TRACE_OS,
24140 + (TSTR("yaffs_read_super: guts initialised %s\n"),
24141 + (err == YAFFS_OK) ? "OK" : "FAILED"));
24143 + if(err == YAFFS_OK)
24144 + yaffs_bg_start(dev);
24146 + if(!context->bgThread)
24147 + param->defered_dir_update = 0;
24150 + /* Release lock before yaffs_get_inode() */
24151 + yaffs_gross_unlock(dev);
24153 + /* Create root inode */
24154 + if (err == YAFFS_OK)
24155 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
24156 + yaffs_root(dev));
24161 + inode->i_op = &yaffs_dir_inode_operations;
24162 + inode->i_fop = &yaffs_dir_operations;
24164 + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: got root inode\n")));
24166 + root = d_alloc_root(inode);
24168 + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: d_alloc_root done\n")));
24174 + sb->s_root = root;
24175 + sb->s_dirt = !dev->is_checkpointed;
24176 + T(YAFFS_TRACE_ALWAYS,
24177 + (TSTR("yaffs_read_super: is_checkpointed %d\n"),
24178 + dev->is_checkpointed));
24180 + T(YAFFS_TRACE_OS, (TSTR("yaffs_read_super: done\n")));
24185 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
24186 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
24189 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
24192 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
24193 +static int yaffs_read_super(struct file_system_type *fs,
24194 + int flags, const char *dev_name,
24195 + void *data, struct vfsmount *mnt)
24198 + return get_sb_bdev(fs, flags, dev_name, data,
24199 + yaffs_internal_read_super_mtd, mnt);
24202 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
24203 + int flags, const char *dev_name,
24207 + return get_sb_bdev(fs, flags, dev_name, data,
24208 + yaffs_internal_read_super_mtd);
24212 +static struct file_system_type yaffs_fs_type = {
24213 + .owner = THIS_MODULE,
24215 + .get_sb = yaffs_read_super,
24216 + .kill_sb = kill_block_super,
24217 + .fs_flags = FS_REQUIRES_DEV,
24220 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
24223 + return yaffs_internal_read_super(1, sb, data, silent);
24226 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
24227 + FS_REQUIRES_DEV);
24231 +#ifdef CONFIG_YAFFS_YAFFS2
24233 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
24234 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
24237 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
24240 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
24241 +static int yaffs2_read_super(struct file_system_type *fs,
24242 + int flags, const char *dev_name, void *data,
24243 + struct vfsmount *mnt)
24245 + return get_sb_bdev(fs, flags, dev_name, data,
24246 + yaffs2_internal_read_super_mtd, mnt);
24249 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
24250 + int flags, const char *dev_name,
24254 + return get_sb_bdev(fs, flags, dev_name, data,
24255 + yaffs2_internal_read_super_mtd);
24259 +static struct file_system_type yaffs2_fs_type = {
24260 + .owner = THIS_MODULE,
24261 + .name = "yaffs2",
24262 + .get_sb = yaffs2_read_super,
24263 + .kill_sb = kill_block_super,
24264 + .fs_flags = FS_REQUIRES_DEV,
24267 +static struct super_block *yaffs2_read_super(struct super_block *sb,
24268 + void *data, int silent)
24270 + return yaffs_internal_read_super(2, sb, data, silent);
24273 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
24274 + FS_REQUIRES_DEV);
24277 +#endif /* CONFIG_YAFFS_YAFFS2 */
24279 +static struct proc_dir_entry *my_proc_entry;
24280 +static struct proc_dir_entry *debug_proc_entry;
24282 +static char *yaffs_dump_dev_part0(char *buf, yaffs_dev_t * dev)
24284 + buf += sprintf(buf, "start_block.......... %d\n", dev->param.start_block);
24285 + buf += sprintf(buf, "end_block............ %d\n", dev->param.end_block);
24286 + buf += sprintf(buf, "total_bytes_per_chunk %d\n", dev->param.total_bytes_per_chunk);
24287 + buf += sprintf(buf, "use_nand_ecc......... %d\n", dev->param.use_nand_ecc);
24288 + buf += sprintf(buf, "no_tags_ecc.......... %d\n", dev->param.no_tags_ecc);
24289 + buf += sprintf(buf, "is_yaffs2............ %d\n", dev->param.is_yaffs2);
24290 + buf += sprintf(buf, "inband_tags.......... %d\n", dev->param.inband_tags);
24291 + buf += sprintf(buf, "empty_lost_n_found... %d\n", dev->param.empty_lost_n_found);
24292 + buf += sprintf(buf, "disable_lazy_load.... %d\n", dev->param.disable_lazy_load);
24293 + buf += sprintf(buf, "refresh_period....... %d\n", dev->param.refresh_period);
24294 + buf += sprintf(buf, "n_caches............. %d\n", dev->param.n_caches);
24295 + buf += sprintf(buf, "n_reserved_blocks.... %d\n", dev->param.n_reserved_blocks);
24296 + buf += sprintf(buf, "always_check_erased.. %d\n", dev->param.always_check_erased);
24298 + buf += sprintf(buf, "\n");
24304 +static char *yaffs_dump_dev_part1(char *buf, yaffs_dev_t * dev)
24306 + buf += sprintf(buf, "data_bytes_per_chunk. %d\n", dev->data_bytes_per_chunk);
24307 + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
24308 + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
24309 + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
24310 + buf += sprintf(buf, "blocks_in_checkpt.... %d\n", dev->blocks_in_checkpt);
24311 + buf += sprintf(buf, "\n");
24312 + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
24313 + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
24314 + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
24315 + buf += sprintf(buf, "\n");
24316 + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
24317 + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
24318 + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
24319 + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
24320 + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
24321 + buf += sprintf(buf, "passive_gc_count..... %u\n", dev->passive_gc_count);
24322 + buf += sprintf(buf, "oldest_dirty_gc_count %u\n", dev->oldest_dirty_gc_count);
24323 + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
24324 + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
24325 + buf += sprintf(buf, "n_retired_writes..... %u\n", dev->n_retired_writes);
24326 + buf += sprintf(buf, "nRetireBlocks........ %u\n", dev->n_retired_blocks);
24327 + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
24328 + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
24329 + buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n", dev->n_tags_ecc_fixed);
24330 + buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n", dev->n_tags_ecc_unfixed);
24331 + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
24332 + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
24333 + buf += sprintf(buf, "n_unlinked_files..... %u\n", dev->n_unlinked_files);
24334 + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
24335 + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
24340 +static int yaffs_proc_read(char *page,
24342 + off_t offset, int count, int *eof, void *data)
24344 + struct ylist_head *item;
24345 + char *buf = page;
24346 + int step = offset;
24349 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
24350 + * We use 'offset' (*ppos) to indicate where we are in dev_list.
24351 + * This also assumes the user has posted a read buffer large
24352 + * enough to hold the complete output; but that's life in /proc.
24355 + *(int *)start = 1;
24357 + /* Print header first */
24359 + buf += sprintf(buf, "Multi-version YAFFS built:" __DATE__ " " __TIME__"\n");
24360 + else if (step == 1)
24361 + buf += sprintf(buf,"\n");
24365 + down(&yaffs_context_lock);
24367 + /* Locate and print the Nth entry. Order N-squared but N is small. */
24368 + ylist_for_each(item, &yaffs_context_list) {
24369 + struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
24370 + yaffs_dev_t *dev = dc->dev;
24372 + if (n < (step & ~1)) {
24376 + if((step & 1)==0){
24377 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->param.name);
24378 + buf = yaffs_dump_dev_part0(buf, dev);
24380 + buf = yaffs_dump_dev_part1(buf, dev);
24384 + up(&yaffs_context_lock);
24387 + return buf - page < count ? buf - page : count;
24390 +static int yaffs_stats_proc_read(char *page,
24392 + off_t offset, int count, int *eof, void *data)
24394 + struct ylist_head *item;
24395 + char *buf = page;
24398 + down(&yaffs_context_lock);
24400 + /* Locate and print the Nth entry. Order N-squared but N is small. */
24401 + ylist_for_each(item, &yaffs_context_list) {
24402 + struct yaffs_LinuxContext *dc = ylist_entry(item, struct yaffs_LinuxContext, contextList);
24403 + yaffs_dev_t *dev = dc->dev;
24405 + int erasedChunks;
24407 + erasedChunks = dev->n_erased_blocks * dev->param.chunks_per_block;
24409 + buf += sprintf(buf,"%d, %d, %d, %u, %u, %u, %u\n",
24410 + n, dev->n_free_chunks, erasedChunks,
24411 + dev->bg_gcs, dev->oldest_dirty_gc_count,
24412 + dev->n_obj, dev->n_tnodes);
24414 + up(&yaffs_context_lock);
24417 + return buf - page < count ? buf - page : count;
24421 + * Set the verbosity of the warnings and error messages.
24423 + * Note that the names can only be a..z or _ with the current code.
24428 + unsigned mask_bitfield;
24429 +} mask_flags[] = {
24430 + {"allocate", YAFFS_TRACE_ALLOCATE},
24431 + {"always", YAFFS_TRACE_ALWAYS},
24432 + {"background", YAFFS_TRACE_BACKGROUND},
24433 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
24434 + {"buffers", YAFFS_TRACE_BUFFERS},
24435 + {"bug", YAFFS_TRACE_BUG},
24436 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
24437 + {"deletion", YAFFS_TRACE_DELETION},
24438 + {"erase", YAFFS_TRACE_ERASE},
24439 + {"error", YAFFS_TRACE_ERROR},
24440 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
24441 + {"gc", YAFFS_TRACE_GC},
24442 + {"lock", YAFFS_TRACE_LOCK},
24443 + {"mtd", YAFFS_TRACE_MTD},
24444 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
24445 + {"os", YAFFS_TRACE_OS},
24446 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
24447 + {"scan", YAFFS_TRACE_SCAN},
24448 + {"tracing", YAFFS_TRACE_TRACING},
24449 + {"sync", YAFFS_TRACE_SYNC},
24450 + {"write", YAFFS_TRACE_WRITE},
24452 + {"verify", YAFFS_TRACE_VERIFY},
24453 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
24454 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
24455 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
24457 + {"all", 0xffffffff},
24462 +#define MAX_MASK_NAME_LENGTH 40
24463 +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
24464 + unsigned long count, void *data)
24466 + unsigned rg = 0, mask_bitfield;
24470 + char substring[MAX_MASK_NAME_LENGTH + 1];
24473 + int add, len = 0;
24476 + rg = yaffs_trace_mask;
24478 + while (!done && (pos < count)) {
24480 + while ((pos < count) && isspace(buf[pos]))
24483 + switch (buf[pos]) {
24495 + mask_name = NULL;
24497 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
24499 + if (end > buf + pos) {
24500 + mask_name = "numeral";
24501 + len = end - (buf + pos);
24505 + for (x = buf + pos, i = 0;
24506 + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
24507 + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
24508 + substring[i] = *x;
24509 + substring[i] = '\0';
24511 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
24512 + if (strcmp(substring, mask_flags[i].mask_name) == 0) {
24513 + mask_name = mask_flags[i].mask_name;
24514 + mask_bitfield = mask_flags[i].mask_bitfield;
24521 + if (mask_name != NULL) {
24525 + rg &= ~mask_bitfield;
24528 + rg |= mask_bitfield;
24531 + rg = mask_bitfield;
24534 + rg |= mask_bitfield;
24540 + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
24542 + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
24544 + if (rg & YAFFS_TRACE_ALWAYS) {
24545 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
24547 + flag = ((rg & mask_flags[i].mask_bitfield) ==
24548 + mask_flags[i].mask_bitfield) ? '+' : '-';
24549 + printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
24557 +static int yaffs_proc_write(struct file *file, const char *buf,
24558 + unsigned long count, void *data)
24560 + return yaffs_proc_write_trace_options(file, buf, count, data);
24563 +/* Stuff to handle installation of file systems */
24564 +struct file_system_to_install {
24565 + struct file_system_type *fst;
24569 +static struct file_system_to_install fs_to_install[] = {
24570 + {&yaffs_fs_type, 0},
24571 + {&yaffs2_fs_type, 0},
24575 +static int __init init_yaffs_fs(void)
24578 + struct file_system_to_install *fsinst;
24580 + T(YAFFS_TRACE_ALWAYS,
24581 + (TSTR("yaffs built " __DATE__ " " __TIME__ " Installing. \n")));
24583 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
24584 + T(YAFFS_TRACE_ALWAYS,
24585 + (TSTR(" \n\n\n\nYAFFS-WARNING CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED selected.\n\n\n\n")));
24591 + init_MUTEX(&yaffs_context_lock);
24593 + /* Install the proc_fs entries */
24594 + my_proc_entry = create_proc_entry("yaffs",
24595 + S_IRUGO | S_IFREG,
24598 + if (my_proc_entry) {
24599 + my_proc_entry->write_proc = yaffs_proc_write;
24600 + my_proc_entry->read_proc = yaffs_proc_read;
24601 + my_proc_entry->data = NULL;
24605 + debug_proc_entry = create_proc_entry("yaffs_stats",
24606 + S_IRUGO | S_IFREG,
24609 + if (debug_proc_entry) {
24610 + debug_proc_entry->write_proc = NULL;
24611 + debug_proc_entry->read_proc = yaffs_stats_proc_read;
24612 + debug_proc_entry->data = NULL;
24616 + /* Now add the file system entries */
24618 + fsinst = fs_to_install;
24620 + while (fsinst->fst && !error) {
24621 + error = register_filesystem(fsinst->fst);
24623 + fsinst->installed = 1;
24627 + /* Any errors? uninstall */
24629 + fsinst = fs_to_install;
24631 + while (fsinst->fst) {
24632 + if (fsinst->installed) {
24633 + unregister_filesystem(fsinst->fst);
24634 + fsinst->installed = 0;
24643 +static void __exit exit_yaffs_fs(void)
24646 + struct file_system_to_install *fsinst;
24648 + T(YAFFS_TRACE_ALWAYS,
24649 + (TSTR("yaffs built " __DATE__ " " __TIME__ " removing. \n")));
24651 + remove_proc_entry("yaffs", YPROC_ROOT);
24652 + remove_proc_entry("yaffs_stats", YPROC_ROOT);
24654 + fsinst = fs_to_install;
24656 + while (fsinst->fst) {
24657 + if (fsinst->installed) {
24658 + unregister_filesystem(fsinst->fst);
24659 + fsinst->installed = 0;
24665 +module_init(init_yaffs_fs)
24666 +module_exit(exit_yaffs_fs)
24668 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
24669 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2010");
24670 +MODULE_LICENSE("GPL");
24672 +++ b/fs/yaffs2/yaffs_yaffs1.c
24675 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
24677 + * Copyright (C) 2002-2010 Aleph One Ltd.
24678 + * for Toby Churchill Ltd and Brightstar Engineering
24680 + * Created by Charles Manning <charles@aleph1.co.uk>
24682 + * This program is free software; you can redistribute it and/or modify
24683 + * it under the terms of the GNU General Public License version 2 as
24684 + * published by the Free Software Foundation.
24686 +#include "yaffs_yaffs1.h"
24687 +#include "yportenv.h"
24688 +#include "yaffs_trace.h"
24689 +#include "yaffs_bitmap.h"
24690 +#include "yaffs_getblockinfo.h"
24691 +#include "yaffs_nand.h"
24694 +int yaffs1_scan(yaffs_dev_t *dev)
24696 + yaffs_ext_tags tags;
24698 + int blockIterator;
24699 + int startIterator;
24706 + yaffs_block_state_t state;
24707 + yaffs_obj_t *hard_list = NULL;
24708 + yaffs_block_info_t *bi;
24709 + __u32 seq_number;
24710 + yaffs_obj_header *oh;
24712 + yaffs_obj_t *parent;
24714 + int alloc_failed = 0;
24716 + struct yaffs_shadow_fixer_s *shadowFixerList = NULL;
24723 + T(YAFFS_TRACE_SCAN,
24724 + (TSTR("yaffs1_scan starts intstartblk %d intendblk %d..." TENDSTR),
24725 + dev->internal_start_block, dev->internal_end_block));
24727 + chunkData = yaffs_get_temp_buffer(dev, __LINE__);
24729 + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
24731 + /* Scan all the blocks to determine their state */
24732 + bi = dev->block_info;
24733 + for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
24734 + yaffs_clear_chunk_bits(dev, blk);
24735 + bi->pages_in_use = 0;
24736 + bi->soft_del_pages = 0;
24738 + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
24740 + bi->block_state = state;
24741 + bi->seq_number = seq_number;
24743 + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
24744 + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
24746 + T(YAFFS_TRACE_SCAN_DEBUG,
24747 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
24748 + state, seq_number));
24750 + if (state == YAFFS_BLOCK_STATE_DEAD) {
24751 + T(YAFFS_TRACE_BAD_BLOCKS,
24752 + (TSTR("block %d is bad" TENDSTR), blk));
24753 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
24754 + T(YAFFS_TRACE_SCAN_DEBUG,
24755 + (TSTR("Block empty " TENDSTR)));
24756 + dev->n_erased_blocks++;
24757 + dev->n_free_chunks += dev->param.chunks_per_block;
24762 + startIterator = dev->internal_start_block;
24763 + endIterator = dev->internal_end_block;
24765 + /* For each block.... */
24766 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
24767 + blockIterator++) {
24773 + blk = blockIterator;
24775 + bi = yaffs_get_block_info(dev, blk);
24776 + state = bi->block_state;
24780 + /* For each chunk in each block that needs scanning....*/
24781 + for (c = 0; !alloc_failed && c < dev->param.chunks_per_block &&
24782 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
24783 + /* Read the tags and decide what to do */
24784 + chunk = blk * dev->param.chunks_per_block + c;
24786 + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
24789 + /* Let's have a good look at this chunk... */
24791 + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED || tags.is_deleted) {
24792 + /* YAFFS1 only...
24793 + * A deleted chunk
24796 + dev->n_free_chunks++;
24797 + /*T((" %d %d deleted\n",blk,c)); */
24798 + } else if (!tags.chunk_used) {
24799 + /* An unassigned chunk in the block
24800 + * This means that either the block is empty or
24801 + * this is the one being allocated from
24805 + /* We're looking at the first chunk in the block so the block is unused */
24806 + state = YAFFS_BLOCK_STATE_EMPTY;
24807 + dev->n_erased_blocks++;
24809 + /* this is the block being allocated from */
24810 + T(YAFFS_TRACE_SCAN,
24812 + (" Allocating from %d %d" TENDSTR),
24814 + state = YAFFS_BLOCK_STATE_ALLOCATING;
24815 + dev->alloc_block = blk;
24816 + dev->alloc_page = c;
24817 + dev->alloc_block_finder = blk;
24818 + /* Set block finder here to encourage the allocator to go forth from here. */
24822 + dev->n_free_chunks += (dev->param.chunks_per_block - c);
24823 + } else if (tags.chunk_id > 0) {
24824 + /* chunk_id > 0 so it is a data chunk... */
24825 + unsigned int endpos;
24827 + yaffs_set_chunk_bit(dev, blk, c);
24828 + bi->pages_in_use++;
24830 + in = yaffs_find_or_create_by_number(dev,
24833 + YAFFS_OBJECT_TYPE_FILE);
24834 + /* PutChunkIntoFile checks for a clash (two data chunks with
24835 + * the same chunk_id).
24839 + alloc_failed = 1;
24842 + if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, 1))
24843 + alloc_failed = 1;
24847 + (tags.chunk_id - 1) * dev->data_bytes_per_chunk +
24850 + in->variant_type == YAFFS_OBJECT_TYPE_FILE
24851 + && in->variant.file_variant.scanned_size <
24853 + in->variant.file_variant.
24854 + scanned_size = endpos;
24855 + if (!dev->param.use_header_file_size) {
24856 + in->variant.file_variant.
24858 + in->variant.file_variant.
24863 + /* T((" %d %d data %d %d\n",blk,c,tags.obj_id,tags.chunk_id)); */
24865 + /* chunk_id == 0, so it is an ObjectHeader.
24866 + * Thus, we read in the object header and make the object
24868 + yaffs_set_chunk_bit(dev, blk, c);
24869 + bi->pages_in_use++;
24871 + result = yaffs_rd_chunk_tags_nand(dev, chunk,
24875 + oh = (yaffs_obj_header *) chunkData;
24877 + in = yaffs_find_by_number(dev,
24879 + if (in && in->variant_type != oh->type) {
24880 + /* This should not happen, but somehow
24881 + * Wev'e ended up with an obj_id that has been reused but not yet
24882 + * deleted, and worse still it has changed type. Delete the old object.
24885 + yaffs_del_obj(in);
24890 + in = yaffs_find_or_create_by_number(dev,
24896 + alloc_failed = 1;
24898 + if (in && oh->shadows_obj > 0) {
24900 + struct yaffs_shadow_fixer_s *fixer;
24901 + fixer = YMALLOC(sizeof(struct yaffs_shadow_fixer_s));
24903 + fixer->next = shadowFixerList;
24904 + shadowFixerList = fixer;
24905 + fixer->obj_id = tags.obj_id;
24906 + fixer->shadowed_id = oh->shadows_obj;
24907 + T(YAFFS_TRACE_SCAN,
24909 + (" Shadow fixer: %d shadows %d" TENDSTR),
24910 + fixer->obj_id, fixer->shadowed_id));
24916 + if (in && in->valid) {
24917 + /* We have already filled this one. We have a duplicate and need to resolve it. */
24919 + unsigned existingSerial = in->serial;
24920 + unsigned newSerial = tags.serial_number;
24922 + if (((existingSerial + 1) & 3) == newSerial) {
24923 + /* Use new one - destroy the exisiting one */
24924 + yaffs_chunk_del(dev,
24929 + /* Use existing - destroy this one. */
24930 + yaffs_chunk_del(dev, chunk, 1,
24935 + if (in && !in->valid &&
24936 + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
24937 + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
24938 + /* We only load some info, don't fiddle with directory structure */
24940 + in->variant_type = oh->type;
24942 + in->yst_mode = oh->yst_mode;
24943 +#ifdef CONFIG_YAFFS_WINCE
24944 + in->win_atime[0] = oh->win_atime[0];
24945 + in->win_ctime[0] = oh->win_ctime[0];
24946 + in->win_mtime[0] = oh->win_mtime[0];
24947 + in->win_atime[1] = oh->win_atime[1];
24948 + in->win_ctime[1] = oh->win_ctime[1];
24949 + in->win_mtime[1] = oh->win_mtime[1];
24951 + in->yst_uid = oh->yst_uid;
24952 + in->yst_gid = oh->yst_gid;
24953 + in->yst_atime = oh->yst_atime;
24954 + in->yst_mtime = oh->yst_mtime;
24955 + in->yst_ctime = oh->yst_ctime;
24956 + in->yst_rdev = oh->yst_rdev;
24958 + in->hdr_chunk = chunk;
24959 + in->serial = tags.serial_number;
24961 + } else if (in && !in->valid) {
24962 + /* we need to load this info */
24965 + in->variant_type = oh->type;
24967 + in->yst_mode = oh->yst_mode;
24968 +#ifdef CONFIG_YAFFS_WINCE
24969 + in->win_atime[0] = oh->win_atime[0];
24970 + in->win_ctime[0] = oh->win_ctime[0];
24971 + in->win_mtime[0] = oh->win_mtime[0];
24972 + in->win_atime[1] = oh->win_atime[1];
24973 + in->win_ctime[1] = oh->win_ctime[1];
24974 + in->win_mtime[1] = oh->win_mtime[1];
24976 + in->yst_uid = oh->yst_uid;
24977 + in->yst_gid = oh->yst_gid;
24978 + in->yst_atime = oh->yst_atime;
24979 + in->yst_mtime = oh->yst_mtime;
24980 + in->yst_ctime = oh->yst_ctime;
24981 + in->yst_rdev = oh->yst_rdev;
24983 + in->hdr_chunk = chunk;
24984 + in->serial = tags.serial_number;
24986 + yaffs_set_obj_name_from_oh(in, oh);
24989 + /* directory stuff...
24990 + * hook up to parent
24994 + yaffs_find_or_create_by_number
24995 + (dev, oh->parent_obj_id,
24996 + YAFFS_OBJECT_TYPE_DIRECTORY);
24998 + alloc_failed = 1;
24999 + if (parent && parent->variant_type ==
25000 + YAFFS_OBJECT_TYPE_UNKNOWN) {
25001 + /* Set up as a directory */
25002 + parent->variant_type =
25003 + YAFFS_OBJECT_TYPE_DIRECTORY;
25004 + YINIT_LIST_HEAD(&parent->variant.
25007 + } else if (!parent || parent->variant_type !=
25008 + YAFFS_OBJECT_TYPE_DIRECTORY) {
25009 + /* Hoosterman, another problem....
25010 + * We're trying to use a non-directory as a directory
25013 + T(YAFFS_TRACE_ERROR,
25015 + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
25017 + parent = dev->lost_n_found;
25020 + yaffs_add_obj_to_dir(parent, in);
25022 + if (0 && (parent == dev->del_dir ||
25023 + parent == dev->unlinked_dir)) {
25024 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
25025 + dev->n_deleted_files++;
25027 + /* Note re hardlinks.
25028 + * Since we might scan a hardlink before its equivalent object is scanned
25029 + * we put them all in a list.
25030 + * After scanning is complete, we should have all the objects, so we run through this
25031 + * list and fix up all the chains.
25034 + switch (in->variant_type) {
25035 + case YAFFS_OBJECT_TYPE_UNKNOWN:
25036 + /* Todo got a problem */
25038 + case YAFFS_OBJECT_TYPE_FILE:
25039 + if (dev->param.use_header_file_size)
25041 + in->variant.file_variant.
25046 + case YAFFS_OBJECT_TYPE_HARDLINK:
25047 + in->variant.hardlink_variant.
25050 + in->hard_links.next =
25051 + (struct ylist_head *)
25055 + case YAFFS_OBJECT_TYPE_DIRECTORY:
25058 + case YAFFS_OBJECT_TYPE_SPECIAL:
25061 + case YAFFS_OBJECT_TYPE_SYMLINK:
25062 + in->variant.symlink_variant.alias =
25063 + yaffs_clone_str(oh->alias);
25064 + if (!in->variant.symlink_variant.alias)
25065 + alloc_failed = 1;
25073 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
25074 + /* If we got this far while scanning, then the block is fully allocated.*/
25075 + state = YAFFS_BLOCK_STATE_FULL;
25078 + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
25079 + /* If the block was partially allocated then treat it as fully allocated.*/
25080 + state = YAFFS_BLOCK_STATE_FULL;
25081 + dev->alloc_block = -1;
25084 + bi->block_state = state;
25086 + /* Now let's see if it was dirty */
25087 + if (bi->pages_in_use == 0 &&
25088 + !bi->has_shrink_hdr &&
25089 + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
25090 + yaffs_block_became_dirty(dev, blk);
25096 + /* Ok, we've done all the scanning.
25097 + * Fix up the hard link chains.
25098 + * We should now have scanned all the objects, now it's time to add these
25102 + yaffs_link_fixup(dev, hard_list);
25104 + /* Fix up any shadowed objects */
25106 + struct yaffs_shadow_fixer_s *fixer;
25107 + yaffs_obj_t *obj;
25109 + while (shadowFixerList) {
25110 + fixer = shadowFixerList;
25111 + shadowFixerList = fixer->next;
25112 + /* Complete the rename transaction by deleting the shadowed object
25113 + * then setting the object header to unshadowed.
25115 + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
25117 + yaffs_del_obj(obj);
25119 + obj = yaffs_find_by_number(dev, fixer->obj_id);
25122 + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
25128 + yaffs_release_temp_buffer(dev, chunkData, __LINE__);
25130 + if (alloc_failed)
25131 + return YAFFS_FAIL;
25133 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs1_scan ends" TENDSTR)));
25140 +++ b/fs/yaffs2/yaffs_yaffs1.h
25143 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
25145 + * Copyright (C) 2002-2010 Aleph One Ltd.
25146 + * for Toby Churchill Ltd and Brightstar Engineering
25148 + * Created by Charles Manning <charles@aleph1.co.uk>
25150 + * This program is free software; you can redistribute it and/or modify
25151 + * it under the terms of the GNU Lesser General Public License version 2.1 as
25152 + * published by the Free Software Foundation.
25154 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
25157 +#ifndef __YAFFS_YAFFS1_H__
25158 +#define __YAFFS_YAFFS1_H__
25160 +#include "yaffs_guts.h"
25161 +int yaffs1_scan(yaffs_dev_t *dev);
25165 +++ b/fs/yaffs2/yaffs_yaffs2.c
25168 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
25170 + * Copyright (C) 2002-2010 Aleph One Ltd.
25171 + * for Toby Churchill Ltd and Brightstar Engineering
25173 + * Created by Charles Manning <charles@aleph1.co.uk>
25175 + * This program is free software; you can redistribute it and/or modify
25176 + * it under the terms of the GNU General Public License version 2 as
25177 + * published by the Free Software Foundation.
25181 +#include "yaffs_guts.h"
25182 +#include "yaffs_trace.h"
25183 +#include "yaffs_yaffs2.h"
25184 +#include "yaffs_checkptrw.h"
25185 +#include "yaffs_bitmap.h"
25186 +#include "yaffs_qsort.h"
25187 +#include "yaffs_nand.h"
25188 +#include "yaffs_getblockinfo.h"
25189 +#include "yaffs_verify.h"
25192 + * Checkpoints are really no benefit on very small partitions.
25194 + * To save space on small partitions don't bother with checkpoints unless
25195 + * the partition is at least this big.
25197 +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
25199 +#define YAFFS_SMALL_HOLE_THRESHOLD 4
25203 + * Oldest Dirty Sequence Number handling.
25206 +/* yaffs_calc_oldest_dirty_seq()
25207 + * yaffs2_find_oldest_dirty_seq()
25208 + * Calculate the oldest dirty sequence number if we don't know it.
25210 +void yaffs_calc_oldest_dirty_seq(yaffs_dev_t *dev)
25214 + unsigned block_no = 0;
25215 + yaffs_block_info_t *b;
25217 + if(!dev->param.is_yaffs2)
25220 + /* Find the oldest dirty sequence number. */
25221 + seq = dev->seq_number + 1;
25222 + b = dev->block_info;
25223 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
25224 + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
25225 + (b->pages_in_use - b->soft_del_pages) < dev->param.chunks_per_block &&
25226 + b->seq_number < seq) {
25227 + seq = b->seq_number;
25234 + dev->oldest_dirty_seq = seq;
25235 + dev->oldest_dirty_block = block_no;
25241 +void yaffs2_find_oldest_dirty_seq(yaffs_dev_t *dev)
25243 + if(!dev->param.is_yaffs2)
25246 + if(!dev->oldest_dirty_seq)
25247 + yaffs_calc_oldest_dirty_seq(dev);
25251 + * yaffs_clear_oldest_dirty_seq()
25252 + * Called when a block is erased or marked bad. (ie. when its seq_number
25253 + * becomes invalid). If the value matches the oldest then we clear
25254 + * dev->oldest_dirty_seq to force its recomputation.
25256 +void yaffs2_clear_oldest_dirty_seq(yaffs_dev_t *dev, yaffs_block_info_t *bi)
25259 + if(!dev->param.is_yaffs2)
25262 + if(!bi || bi->seq_number == dev->oldest_dirty_seq){
25263 + dev->oldest_dirty_seq = 0;
25264 + dev->oldest_dirty_block = 0;
25269 + * yaffs2_update_oldest_dirty_seq()
25270 + * Update the oldest dirty sequence number whenever we dirty a block.
25271 + * Only do this if the oldest_dirty_seq is actually being tracked.
25273 +void yaffs2_update_oldest_dirty_seq(yaffs_dev_t *dev, unsigned block_no, yaffs_block_info_t *bi)
25275 + if(!dev->param.is_yaffs2)
25278 + if(dev->oldest_dirty_seq){
25279 + if(dev->oldest_dirty_seq > bi->seq_number){
25280 + dev->oldest_dirty_seq = bi->seq_number;
25281 + dev->oldest_dirty_block = block_no;
25286 +int yaffs_block_ok_for_gc(yaffs_dev_t *dev,
25287 + yaffs_block_info_t *bi)
25290 + if (!dev->param.is_yaffs2)
25291 + return 1; /* disqualification only applies to yaffs2. */
25293 + if (!bi->has_shrink_hdr)
25294 + return 1; /* can gc */
25296 + yaffs2_find_oldest_dirty_seq(dev);
25298 + /* Can't do gc of this block if there are any blocks older than this one that have
25299 + * discarded pages.
25301 + return (bi->seq_number <= dev->oldest_dirty_seq);
25305 + * yaffs2_find_refresh_block()
25306 + * periodically finds the oldest full block by sequence number for refreshing.
25307 + * Only for yaffs2.
25309 +__u32 yaffs2_find_refresh_block(yaffs_dev_t *dev)
25313 + __u32 oldest = 0;
25314 + __u32 oldestSequence = 0;
25316 + yaffs_block_info_t *bi;
25318 + if(!dev->param.is_yaffs2)
25322 + * If refresh period < 10 then refreshing is disabled.
25324 + if(dev->param.refresh_period < 10)
25328 + * Fix broken values.
25330 + if(dev->refresh_skip > dev->param.refresh_period)
25331 + dev->refresh_skip = dev->param.refresh_period;
25333 + if(dev->refresh_skip > 0)
25337 + * Refresh skip is now zero.
25338 + * We'll do a refresh this time around....
25339 + * Update the refresh skip and find the oldest block.
25341 + dev->refresh_skip = dev->param.refresh_period;
25342 + dev->refresh_count++;
25343 + bi = dev->block_info;
25344 + for (b = dev->internal_start_block; b <=dev->internal_end_block; b++){
25346 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL){
25349 + bi->seq_number < oldestSequence){
25351 + oldestSequence = bi->seq_number;
25357 + if (oldest > 0) {
25358 + T(YAFFS_TRACE_GC,
25359 + (TSTR("GC refresh count %d selected block %d with seq_number %d" TENDSTR),
25360 + dev->refresh_count, oldest, oldestSequence));
25366 +int yaffs2_checkpt_required(yaffs_dev_t *dev)
25370 + if(!dev->param.is_yaffs2)
25373 + nblocks = dev->internal_end_block - dev->internal_start_block + 1 ;
25375 + return !dev->param.skip_checkpt_wr &&
25376 + !dev->read_only &&
25377 + (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
25380 +int yaffs_calc_checkpt_blocks_required(yaffs_dev_t *dev)
25384 + if(!dev->param.is_yaffs2)
25387 + if (!dev->checkpoint_blocks_required &&
25388 + yaffs2_checkpt_required(dev)){
25389 + /* Not a valid value so recalculate */
25392 + int devBlocks = (dev->param.end_block - dev->param.start_block + 1);
25394 + n_bytes += sizeof(yaffs_checkpt_validty_t);
25395 + n_bytes += sizeof(yaffs_checkpt_dev_t);
25396 + n_bytes += devBlocks * sizeof(yaffs_block_info_t);
25397 + n_bytes += devBlocks * dev->chunk_bit_stride;
25398 + n_bytes += (sizeof(yaffs_checkpt_obj_t) + sizeof(__u32)) * (dev->n_obj);
25399 + n_bytes += (dev->tnode_size + sizeof(__u32)) * (dev->n_tnodes);
25400 + n_bytes += sizeof(yaffs_checkpt_validty_t);
25401 + n_bytes += sizeof(__u32); /* checksum*/
25403 + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
25405 + nBlocks = (n_bytes/(dev->data_bytes_per_chunk * dev->param.chunks_per_block)) + 3;
25407 + dev->checkpoint_blocks_required = nBlocks;
25410 + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
25416 +/*--------------------- Checkpointing --------------------*/
25419 +static int yaffs2_wr_checkpt_validity_marker(yaffs_dev_t *dev, int head)
25421 + yaffs_checkpt_validty_t cp;
25423 + memset(&cp, 0, sizeof(cp));
25425 + cp.struct_type = sizeof(cp);
25426 + cp.magic = YAFFS_MAGIC;
25427 + cp.version = YAFFS_CHECKPOINT_VERSION;
25428 + cp.head = (head) ? 1 : 0;
25430 + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
25434 +static int yaffs2_rd_checkpt_validty_marker(yaffs_dev_t *dev, int head)
25436 + yaffs_checkpt_validty_t cp;
25439 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
25442 + ok = (cp.struct_type == sizeof(cp)) &&
25443 + (cp.magic == YAFFS_MAGIC) &&
25444 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
25445 + (cp.head == ((head) ? 1 : 0));
25446 + return ok ? 1 : 0;
25449 +static void yaffs2_dev_to_checkpt_dev(yaffs_checkpt_dev_t *cp,
25450 + yaffs_dev_t *dev)
25452 + cp->n_erased_blocks = dev->n_erased_blocks;
25453 + cp->alloc_block = dev->alloc_block;
25454 + cp->alloc_page = dev->alloc_page;
25455 + cp->n_free_chunks = dev->n_free_chunks;
25457 + cp->n_deleted_files = dev->n_deleted_files;
25458 + cp->n_unlinked_files = dev->n_unlinked_files;
25459 + cp->n_bg_deletions = dev->n_bg_deletions;
25460 + cp->seq_number = dev->seq_number;
25464 +static void yaffs_checkpt_dev_to_dev(yaffs_dev_t *dev,
25465 + yaffs_checkpt_dev_t *cp)
25467 + dev->n_erased_blocks = cp->n_erased_blocks;
25468 + dev->alloc_block = cp->alloc_block;
25469 + dev->alloc_page = cp->alloc_page;
25470 + dev->n_free_chunks = cp->n_free_chunks;
25472 + dev->n_deleted_files = cp->n_deleted_files;
25473 + dev->n_unlinked_files = cp->n_unlinked_files;
25474 + dev->n_bg_deletions = cp->n_bg_deletions;
25475 + dev->seq_number = cp->seq_number;
25479 +static int yaffs2_wr_checkpt_dev(yaffs_dev_t *dev)
25481 + yaffs_checkpt_dev_t cp;
25483 + __u32 nBlocks = (dev->internal_end_block - dev->internal_start_block + 1);
25487 + /* Write device runtime values*/
25488 + yaffs2_dev_to_checkpt_dev(&cp, dev);
25489 + cp.struct_type = sizeof(cp);
25491 + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
25493 + /* Write block info */
25495 + n_bytes = nBlocks * sizeof(yaffs_block_info_t);
25496 + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
25499 + /* Write chunk bits */
25501 + n_bytes = nBlocks * dev->chunk_bit_stride;
25502 + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
25504 + return ok ? 1 : 0;
25508 +static int yaffs2_rd_checkpt_dev(yaffs_dev_t *dev)
25510 + yaffs_checkpt_dev_t cp;
25512 + __u32 nBlocks = (dev->internal_end_block - dev->internal_start_block + 1);
25516 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
25520 + if (cp.struct_type != sizeof(cp))
25524 + yaffs_checkpt_dev_to_dev(dev, &cp);
25526 + n_bytes = nBlocks * sizeof(yaffs_block_info_t);
25528 + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
25532 + n_bytes = nBlocks * dev->chunk_bit_stride;
25534 + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
25536 + return ok ? 1 : 0;
25539 +static void yaffs2_obj_checkpt_obj(yaffs_checkpt_obj_t *cp,
25540 + yaffs_obj_t *obj)
25543 + cp->obj_id = obj->obj_id;
25544 + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
25545 + cp->hdr_chunk = obj->hdr_chunk;
25546 + cp->variant_type = obj->variant_type;
25547 + cp->deleted = obj->deleted;
25548 + cp->soft_del = obj->soft_del;
25549 + cp->unlinked = obj->unlinked;
25550 + cp->fake = obj->fake;
25551 + cp->rename_allowed = obj->rename_allowed;
25552 + cp->unlink_allowed = obj->unlink_allowed;
25553 + cp->serial = obj->serial;
25554 + cp->n_data_chunks = obj->n_data_chunks;
25556 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
25557 + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
25558 + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
25559 + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
25562 +static int taffs2_checkpt_obj_to_obj(yaffs_obj_t *obj, yaffs_checkpt_obj_t *cp)
25565 + yaffs_obj_t *parent;
25567 + if (obj->variant_type != cp->variant_type) {
25568 + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
25569 + TCONT("chunk %d does not match existing object type %d")
25570 + TENDSTR), cp->obj_id, cp->variant_type, cp->hdr_chunk,
25571 + obj->variant_type));
25575 + obj->obj_id = cp->obj_id;
25577 + if (cp->parent_id)
25578 + parent = yaffs_find_or_create_by_number(
25581 + YAFFS_OBJECT_TYPE_DIRECTORY);
25586 + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
25587 + T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
25588 + TCONT(" chunk %d Parent type, %d, not directory")
25590 + cp->obj_id, cp->parent_id, cp->variant_type,
25591 + cp->hdr_chunk, parent->variant_type));
25594 + yaffs_add_obj_to_dir(parent, obj);
25597 + obj->hdr_chunk = cp->hdr_chunk;
25598 + obj->variant_type = cp->variant_type;
25599 + obj->deleted = cp->deleted;
25600 + obj->soft_del = cp->soft_del;
25601 + obj->unlinked = cp->unlinked;
25602 + obj->fake = cp->fake;
25603 + obj->rename_allowed = cp->rename_allowed;
25604 + obj->unlink_allowed = cp->unlink_allowed;
25605 + obj->serial = cp->serial;
25606 + obj->n_data_chunks = cp->n_data_chunks;
25608 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
25609 + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
25610 + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
25611 + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
25613 + if (obj->hdr_chunk > 0)
25614 + obj->lazy_loaded = 1;
25620 +static int yaffs2_checkpt_tnode_worker(yaffs_obj_t *in, yaffs_tnode_t *tn,
25621 + __u32 level, int chunk_offset)
25624 + yaffs_dev_t *dev = in->my_dev;
25630 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
25631 + if (tn->internal[i]) {
25632 + ok = yaffs2_checkpt_tnode_worker(in,
25635 + (chunk_offset<<YAFFS_TNODES_INTERNAL_BITS) + i);
25638 + } else if (level == 0) {
25639 + __u32 baseOffset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
25640 + ok = (yaffs2_checkpt_wr(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
25642 + ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) == dev->tnode_size);
25650 +static int yaffs2_wr_checkpt_tnodes(yaffs_obj_t *obj)
25652 + __u32 endMarker = ~0;
25655 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
25656 + ok = yaffs2_checkpt_tnode_worker(obj,
25657 + obj->variant.file_variant.top,
25658 + obj->variant.file_variant.top_level,
25661 + ok = (yaffs2_checkpt_wr(obj->my_dev, &endMarker, sizeof(endMarker)) ==
25662 + sizeof(endMarker));
25665 + return ok ? 1 : 0;
25668 +static int yaffs2_rd_checkpt_tnodes(yaffs_obj_t *obj)
25672 + yaffs_dev_t *dev = obj->my_dev;
25673 + yaffs_file_s *fileStructPtr = &obj->variant.file_variant;
25674 + yaffs_tnode_t *tn;
25677 + ok = (yaffs2_checkpt_rd(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
25679 + while (ok && (~baseChunk)) {
25681 + /* Read level 0 tnode */
25684 + tn = yaffs_get_tnode(dev);
25686 + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) == dev->tnode_size);
25691 + ok = yaffs_add_find_tnode_0(dev,
25697 + ok = (yaffs2_checkpt_rd(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
25701 + T(YAFFS_TRACE_CHECKPOINT, (
25702 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
25703 + nread, baseChunk, ok));
25705 + return ok ? 1 : 0;
25709 +static int yaffs2_wr_checkpt_objs(yaffs_dev_t *dev)
25711 + yaffs_obj_t *obj;
25712 + yaffs_checkpt_obj_t cp;
25715 + struct ylist_head *lh;
25718 + /* Iterate through the objects in each hash entry,
25719 + * dumping them to the checkpointing stream.
25722 + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
25723 + ylist_for_each(lh, &dev->obj_bucket[i].list) {
25725 + obj = ylist_entry(lh, yaffs_obj_t, hash_link);
25726 + if (!obj->defered_free) {
25727 + yaffs2_obj_checkpt_obj(&cp, obj);
25728 + cp.struct_type = sizeof(cp);
25730 + T(YAFFS_TRACE_CHECKPOINT, (
25731 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %p" TENDSTR),
25732 + cp.obj_id, cp.parent_id, cp.variant_type, cp.hdr_chunk, obj));
25734 + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
25736 + if (ok && obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
25737 + ok = yaffs2_wr_checkpt_tnodes(obj);
25743 + /* Dump end of list */
25744 + memset(&cp, 0xFF, sizeof(yaffs_checkpt_obj_t));
25745 + cp.struct_type = sizeof(cp);
25748 + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
25750 + return ok ? 1 : 0;
25753 +static int yaffs2_rd_checkpt_objs(yaffs_dev_t *dev)
25755 + yaffs_obj_t *obj;
25756 + yaffs_checkpt_obj_t cp;
25759 + yaffs_obj_t *hard_list = NULL;
25761 + while (ok && !done) {
25762 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
25763 + if (cp.struct_type != sizeof(cp)) {
25764 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
25765 + cp.struct_type, (int)sizeof(cp), ok));
25769 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
25770 + cp.obj_id, cp.parent_id, cp.variant_type, cp.hdr_chunk));
25772 + if (ok && cp.obj_id == ~0)
25775 + obj = yaffs_find_or_create_by_number(dev, cp.obj_id, cp.variant_type);
25777 + ok = taffs2_checkpt_obj_to_obj(obj, &cp);
25780 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE) {
25781 + ok = yaffs2_rd_checkpt_tnodes(obj);
25782 + } else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
25783 + obj->hard_links.next =
25784 + (struct ylist_head *) hard_list;
25793 + yaffs_link_fixup(dev, hard_list);
25795 + return ok ? 1 : 0;
25798 +static int yaffs2_wr_checkpt_sum(yaffs_dev_t *dev)
25800 + __u32 checkpt_sum;
25803 + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
25805 + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) == sizeof(checkpt_sum));
25813 +static int yaffs2_rd_checkpt_sum(yaffs_dev_t *dev)
25815 + __u32 checkpt_sum0;
25816 + __u32 checkpt_sum1;
25819 + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
25821 + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) == sizeof(checkpt_sum1));
25826 + if (checkpt_sum0 != checkpt_sum1)
25833 +static int yaffs2_wr_checkpt_data(yaffs_dev_t *dev)
25837 + if (!yaffs2_checkpt_required(dev)) {
25838 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
25843 + ok = yaffs2_checkpt_open(dev, 1);
25846 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
25847 + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
25850 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
25851 + ok = yaffs2_wr_checkpt_dev(dev);
25854 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
25855 + ok = yaffs2_wr_checkpt_objs(dev);
25858 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
25859 + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
25863 + ok = yaffs2_wr_checkpt_sum(dev);
25865 + if (!yaffs_checkpt_close(dev))
25869 + dev->is_checkpointed = 1;
25871 + dev->is_checkpointed = 0;
25873 + return dev->is_checkpointed;
25876 +static int yaffs2_rd_checkpt_data(yaffs_dev_t *dev)
25880 + if(!dev->param.is_yaffs2)
25883 + if (ok && dev->param.skip_checkpt_rd) {
25884 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
25889 + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
25892 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
25893 + ok = yaffs2_rd_checkpt_validty_marker(dev, 1);
25896 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
25897 + ok = yaffs2_rd_checkpt_dev(dev);
25900 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
25901 + ok = yaffs2_rd_checkpt_objs(dev);
25904 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
25905 + ok = yaffs2_rd_checkpt_validty_marker(dev, 0);
25909 + ok = yaffs2_rd_checkpt_sum(dev);
25910 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
25913 + if (!yaffs_checkpt_close(dev))
25917 + dev->is_checkpointed = 1;
25919 + dev->is_checkpointed = 0;
25921 + return ok ? 1 : 0;
25925 +void yaffs2_checkpt_invalidate(yaffs_dev_t *dev)
25927 + if (dev->is_checkpointed ||
25928 + dev->blocks_in_checkpt > 0) {
25929 + dev->is_checkpointed = 0;
25930 + yaffs2_checkpt_invalidate_stream(dev);
25932 + if (dev->param.sb_dirty_fn)
25933 + dev->param.sb_dirty_fn(dev);
25937 +int yaffs_checkpoint_save(yaffs_dev_t *dev)
25940 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
25942 + yaffs_verify_objects(dev);
25943 + yaffs_verify_blocks(dev);
25944 + yaffs_verify_free_chunks(dev);
25946 + if (!dev->is_checkpointed) {
25947 + yaffs2_checkpt_invalidate(dev);
25948 + yaffs2_wr_checkpt_data(dev);
25951 + T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
25953 + return dev->is_checkpointed;
25956 +int yaffs2_checkpt_restore(yaffs_dev_t *dev)
25959 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
25961 + retval = yaffs2_rd_checkpt_data(dev);
25963 + if (dev->is_checkpointed) {
25964 + yaffs_verify_objects(dev);
25965 + yaffs_verify_blocks(dev);
25966 + yaffs_verify_free_chunks(dev);
25969 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: is_checkpointed %d"TENDSTR), dev->is_checkpointed));
25974 +int yaffs2_handle_hole(yaffs_obj_t *obj, loff_t new_size)
25976 + /* if newsSize > oldFileSize.
25977 + * We're going to be writing a hole.
25978 + * If the hole is small then write zeros otherwise write a start of hole marker.
25982 + loff_t oldFileSize;
25985 + int result = YAFFS_OK;
25986 + yaffs_dev_t *dev = NULL;
25988 + __u8 *localBuffer = NULL;
25990 + int smallIncreaseOk = 0;
25993 + return YAFFS_FAIL;
25995 + if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
25996 + return YAFFS_FAIL;
25998 + dev = obj->my_dev;
26000 + /* Bail out if not yaffs2 mode */
26001 + if(!dev->param.is_yaffs2)
26004 + oldFileSize = obj->variant.file_variant.file_size;
26006 + if (new_size <= oldFileSize)
26009 + increase = new_size - oldFileSize;
26011 + if(increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
26012 + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
26018 + localBuffer= yaffs_get_temp_buffer(dev, __LINE__);
26021 + /* fill hole with zero bytes */
26022 + int pos = oldFileSize;
26025 + memset(localBuffer,0,dev->data_bytes_per_chunk);
26026 + smallIncreaseOk = 1;
26028 + while(increase > 0 && smallIncreaseOk){
26029 + thisWrite = increase;
26030 + if(thisWrite > dev->data_bytes_per_chunk)
26031 + thisWrite = dev->data_bytes_per_chunk;
26032 + written = yaffs_do_file_wr(obj,localBuffer,pos,thisWrite,0);
26033 + if(written == thisWrite){
26034 + pos += thisWrite;
26035 + increase -= thisWrite;
26037 + smallIncreaseOk = 0;
26040 + yaffs_release_temp_buffer(dev,localBuffer,__LINE__);
26042 + /* If we were out of space then reverse any chunks we've added */
26043 + if(!smallIncreaseOk)
26044 + yaffs_resize_file_down(obj, oldFileSize);
26047 + if (!smallIncreaseOk &&
26049 + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
26050 + obj->parent->obj_id != YAFFS_OBJECTID_DELETED){
26051 + /* Write a hole start header with the old file size */
26052 + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
26063 +} yaffs_BlockIndex;
26066 +static int yaffs2_ybicmp(const void *a, const void *b)
26068 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
26069 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
26070 + register int ablock = ((yaffs_BlockIndex *)a)->block;
26071 + register int bblock = ((yaffs_BlockIndex *)b)->block;
26072 + if (aseq == bseq)
26073 + return ablock - bblock;
26075 + return aseq - bseq;
26078 +int yaffs2_scan_backwards(yaffs_dev_t *dev)
26080 + yaffs_ext_tags tags;
26082 + int blockIterator;
26083 + int startIterator;
26085 + int nBlocksToScan = 0;
26091 + yaffs_block_state_t state;
26092 + yaffs_obj_t *hard_list = NULL;
26093 + yaffs_block_info_t *bi;
26094 + __u32 seq_number;
26095 + yaffs_obj_header *oh;
26097 + yaffs_obj_t *parent;
26098 + int nBlocks = dev->internal_end_block - dev->internal_start_block + 1;
26104 + int foundChunksInBlock;
26106 + int alloc_failed = 0;
26109 + yaffs_BlockIndex *blockIndex = NULL;
26110 + int altBlockIndex = 0;
26112 + T(YAFFS_TRACE_SCAN,
26114 + ("yaffs2_scan_backwards starts intstartblk %d intendblk %d..."
26115 + TENDSTR), dev->internal_start_block, dev->internal_end_block));
26118 + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
26120 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
26122 + if (!blockIndex) {
26123 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
26124 + altBlockIndex = 1;
26127 + if (!blockIndex) {
26128 + T(YAFFS_TRACE_SCAN,
26129 + (TSTR("yaffs2_scan_backwards() could not allocate block index!" TENDSTR)));
26130 + return YAFFS_FAIL;
26133 + dev->blocks_in_checkpt = 0;
26135 + chunkData = yaffs_get_temp_buffer(dev, __LINE__);
26137 + /* Scan all the blocks to determine their state */
26138 + bi = dev->block_info;
26139 + for (blk = dev->internal_start_block; blk <= dev->internal_end_block; blk++) {
26140 + yaffs_clear_chunk_bits(dev, blk);
26141 + bi->pages_in_use = 0;
26142 + bi->soft_del_pages = 0;
26144 + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
26146 + bi->block_state = state;
26147 + bi->seq_number = seq_number;
26149 + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
26150 + bi->block_state = state = YAFFS_BLOCK_STATE_CHECKPOINT;
26151 + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
26152 + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
26154 + T(YAFFS_TRACE_SCAN_DEBUG,
26155 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
26156 + state, seq_number));
26159 + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
26160 + dev->blocks_in_checkpt++;
26162 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
26163 + T(YAFFS_TRACE_BAD_BLOCKS,
26164 + (TSTR("block %d is bad" TENDSTR), blk));
26165 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
26166 + T(YAFFS_TRACE_SCAN_DEBUG,
26167 + (TSTR("Block empty " TENDSTR)));
26168 + dev->n_erased_blocks++;
26169 + dev->n_free_chunks += dev->param.chunks_per_block;
26170 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
26172 + /* Determine the highest sequence number */
26173 + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
26174 + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
26176 + blockIndex[nBlocksToScan].seq = seq_number;
26177 + blockIndex[nBlocksToScan].block = blk;
26181 + if (seq_number >= dev->seq_number)
26182 + dev->seq_number = seq_number;
26184 + /* TODO: Nasty sequence number! */
26185 + T(YAFFS_TRACE_SCAN,
26187 + ("Block scanning block %d has bad sequence number %d"
26188 + TENDSTR), blk, seq_number));
26195 + T(YAFFS_TRACE_SCAN,
26196 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
26202 + /* Sort the blocks by sequence number*/
26203 + yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), yaffs2_ybicmp);
26207 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
26209 + /* Now scan the blocks looking at the data. */
26210 + startIterator = 0;
26211 + endIterator = nBlocksToScan - 1;
26212 + T(YAFFS_TRACE_SCAN_DEBUG,
26213 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
26215 + /* For each block.... backwards */
26216 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
26217 + blockIterator--) {
26218 + /* Cooperative multitasking! This loop can run for so
26219 + long that watchdog timers expire. */
26222 + /* get the block to scan in the correct order */
26223 + blk = blockIndex[blockIterator].block;
26225 + bi = yaffs_get_block_info(dev, blk);
26228 + state = bi->block_state;
26232 + /* For each chunk in each block that needs scanning.... */
26233 + foundChunksInBlock = 0;
26234 + for (c = dev->param.chunks_per_block - 1;
26235 + !alloc_failed && c >= 0 &&
26236 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
26237 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
26238 + /* Scan backwards...
26239 + * Read the tags and decide what to do
26242 + chunk = blk * dev->param.chunks_per_block + c;
26244 + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
26247 + /* Let's have a good look at this chunk... */
26249 + if (!tags.chunk_used) {
26250 + /* An unassigned chunk in the block.
26251 + * If there are used chunks after this one, then
26252 + * it is a chunk that was skipped due to failing the erased
26253 + * check. Just skip it so that it can be deleted.
26254 + * But, more typically, We get here when this is an unallocated
26255 + * chunk and his means that either the block is empty or
26256 + * this is the one being allocated from
26259 + if (foundChunksInBlock) {
26260 + /* This is a chunk that was skipped due to failing the erased check */
26261 + } else if (c == 0) {
26262 + /* We're looking at the first chunk in the block so the block is unused */
26263 + state = YAFFS_BLOCK_STATE_EMPTY;
26264 + dev->n_erased_blocks++;
26266 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
26267 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
26268 + if (dev->seq_number == bi->seq_number) {
26269 + /* this is the block being allocated from */
26271 + T(YAFFS_TRACE_SCAN,
26273 + (" Allocating from %d %d"
26274 + TENDSTR), blk, c));
26276 + state = YAFFS_BLOCK_STATE_ALLOCATING;
26277 + dev->alloc_block = blk;
26278 + dev->alloc_page = c;
26279 + dev->alloc_block_finder = blk;
26281 + /* This is a partially written block that is not
26282 + * the current allocation block.
26285 + T(YAFFS_TRACE_SCAN,
26286 + (TSTR("Partially written block %d detected" TENDSTR),
26292 + dev->n_free_chunks++;
26294 + } else if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
26295 + T(YAFFS_TRACE_SCAN,
26296 + (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
26299 + dev->n_free_chunks++;
26301 + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
26302 + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
26303 + (tags.chunk_id > 0 && tags.n_bytes > dev->data_bytes_per_chunk) ||
26304 + tags.seq_number != bi->seq_number ) {
26305 + T(YAFFS_TRACE_SCAN,
26306 + (TSTR("Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored"TENDSTR),
26307 + blk, c,tags.obj_id, tags.chunk_id, tags.n_bytes));
26309 + dev->n_free_chunks++;
26311 + } else if (tags.chunk_id > 0) {
26312 + /* chunk_id > 0 so it is a data chunk... */
26313 + unsigned int endpos;
26314 + __u32 chunkBase =
26315 + (tags.chunk_id - 1) * dev->data_bytes_per_chunk;
26317 + foundChunksInBlock = 1;
26320 + yaffs_set_chunk_bit(dev, blk, c);
26321 + bi->pages_in_use++;
26323 + in = yaffs_find_or_create_by_number(dev,
26326 + YAFFS_OBJECT_TYPE_FILE);
26328 + /* Out of memory */
26329 + alloc_failed = 1;
26333 + in->variant_type == YAFFS_OBJECT_TYPE_FILE
26334 + && chunkBase < in->variant.file_variant.shrink_size) {
26335 + /* This has not been invalidated by a resize */
26336 + if (!yaffs_put_chunk_in_file(in, tags.chunk_id, chunk, -1)) {
26337 + alloc_failed = 1;
26340 + /* File size is calculated by looking at the data chunks if we have not
26341 + * seen an object header yet. Stop this practice once we find an object header.
26343 + endpos = chunkBase + tags.n_bytes;
26345 + if (!in->valid && /* have not got an object header yet */
26346 + in->variant.file_variant.scanned_size < endpos) {
26347 + in->variant.file_variant.scanned_size = endpos;
26348 + in->variant.file_variant.file_size = endpos;
26352 + /* This chunk has been invalidated by a resize, or a past file deletion
26353 + * so delete the chunk*/
26354 + yaffs_chunk_del(dev, chunk, 1, __LINE__);
26358 + /* chunk_id == 0, so it is an ObjectHeader.
26359 + * Thus, we read in the object header and make the object
26361 + foundChunksInBlock = 1;
26363 + yaffs_set_chunk_bit(dev, blk, c);
26364 + bi->pages_in_use++;
26369 + if (tags.extra_available) {
26370 + in = yaffs_find_or_create_by_number(dev,
26372 + tags.extra_obj_type);
26374 + alloc_failed = 1;
26378 + (!in->valid && dev->param.disable_lazy_load) ||
26379 + tags.extra_shadows ||
26381 + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
26382 + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
26384 + /* If we don't have valid info then we need to read the chunk
26385 + * TODO In future we can probably defer reading the chunk and
26386 + * living with invalid data until needed.
26389 + result = yaffs_rd_chunk_tags_nand(dev,
26394 + oh = (yaffs_obj_header *) chunkData;
26396 + if (dev->param.inband_tags) {
26397 + /* Fix up the header if they got corrupted by inband tags */
26398 + oh->shadows_obj = oh->inband_shadowed_obj_id;
26399 + oh->is_shrink = oh->inband_is_shrink;
26403 + in = yaffs_find_or_create_by_number(dev, tags.obj_id, oh->type);
26405 + alloc_failed = 1;
26411 + /* TODO Hoosterman we have a problem! */
26412 + T(YAFFS_TRACE_ERROR,
26414 + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
26415 + TENDSTR), tags.obj_id, chunk));
26420 + /* We have already filled this one.
26421 + * We have a duplicate that will be discarded, but
26422 + * we first have to suck out resize info if it is a file.
26425 + if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
26427 + oh->type == YAFFS_OBJECT_TYPE_FILE) ||
26428 + (tags.extra_available &&
26429 + tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE))) {
26431 + (oh) ? oh->file_size : tags.
26433 + __u32 parent_obj_id =
26435 + parent_obj_id : tags.
26440 + (oh) ? oh->is_shrink : tags.
26443 + /* If it is deleted (unlinked at start also means deleted)
26444 + * we treat the file size as being zeroed at this point.
26446 + if (parent_obj_id ==
26447 + YAFFS_OBJECTID_DELETED
26448 + || parent_obj_id ==
26449 + YAFFS_OBJECTID_UNLINKED) {
26454 + if (is_shrink && in->variant.file_variant.shrink_size > thisSize)
26455 + in->variant.file_variant.shrink_size = thisSize;
26458 + bi->has_shrink_hdr = 1;
26461 + /* Use existing - destroy this one. */
26462 + yaffs_chunk_del(dev, chunk, 1, __LINE__);
26466 + if (!in->valid && in->variant_type !=
26467 + (oh ? oh->type : tags.extra_obj_type))
26468 + T(YAFFS_TRACE_ERROR, (
26469 + TSTR("yaffs tragedy: Bad object type, "
26470 + TCONT("%d != %d, for object %d at chunk ")
26471 + TCONT("%d during scan")
26473 + oh->type : tags.extra_obj_type,
26474 + in->variant_type, tags.obj_id,
26477 + if (!in->valid &&
26478 + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
26480 + YAFFS_OBJECTID_LOSTNFOUND)) {
26481 + /* We only load some info, don't fiddle with directory structure */
26486 + in->yst_mode = oh->yst_mode;
26487 +#ifdef CONFIG_YAFFS_WINCE
26488 + in->win_atime[0] = oh->win_atime[0];
26489 + in->win_ctime[0] = oh->win_ctime[0];
26490 + in->win_mtime[0] = oh->win_mtime[0];
26491 + in->win_atime[1] = oh->win_atime[1];
26492 + in->win_ctime[1] = oh->win_ctime[1];
26493 + in->win_mtime[1] = oh->win_mtime[1];
26495 + in->yst_uid = oh->yst_uid;
26496 + in->yst_gid = oh->yst_gid;
26497 + in->yst_atime = oh->yst_atime;
26498 + in->yst_mtime = oh->yst_mtime;
26499 + in->yst_ctime = oh->yst_ctime;
26500 + in->yst_rdev = oh->yst_rdev;
26502 + in->lazy_loaded = 0;
26506 + in->lazy_loaded = 1;
26508 + in->hdr_chunk = chunk;
26510 + } else if (!in->valid) {
26511 + /* we need to load this info */
26514 + in->hdr_chunk = chunk;
26517 + in->variant_type = oh->type;
26519 + in->yst_mode = oh->yst_mode;
26520 +#ifdef CONFIG_YAFFS_WINCE
26521 + in->win_atime[0] = oh->win_atime[0];
26522 + in->win_ctime[0] = oh->win_ctime[0];
26523 + in->win_mtime[0] = oh->win_mtime[0];
26524 + in->win_atime[1] = oh->win_atime[1];
26525 + in->win_ctime[1] = oh->win_ctime[1];
26526 + in->win_mtime[1] = oh->win_mtime[1];
26528 + in->yst_uid = oh->yst_uid;
26529 + in->yst_gid = oh->yst_gid;
26530 + in->yst_atime = oh->yst_atime;
26531 + in->yst_mtime = oh->yst_mtime;
26532 + in->yst_ctime = oh->yst_ctime;
26533 + in->yst_rdev = oh->yst_rdev;
26536 + if (oh->shadows_obj > 0)
26537 + yaffs_handle_shadowed_obj(dev,
26544 + yaffs_set_obj_name_from_oh(in, oh);
26546 + yaffs_find_or_create_by_number
26547 + (dev, oh->parent_obj_id,
26548 + YAFFS_OBJECT_TYPE_DIRECTORY);
26550 + file_size = oh->file_size;
26551 + is_shrink = oh->is_shrink;
26552 + equiv_id = oh->equiv_id;
26555 + in->variant_type = tags.extra_obj_type;
26557 + yaffs_find_or_create_by_number
26558 + (dev, tags.extra_parent_id,
26559 + YAFFS_OBJECT_TYPE_DIRECTORY);
26560 + file_size = tags.extra_length;
26561 + is_shrink = tags.extra_is_shrink;
26562 + equiv_id = tags.extra_equiv_id;
26563 + in->lazy_loaded = 1;
26569 + alloc_failed = 1;
26571 + /* directory stuff...
26572 + * hook up to parent
26575 + if (parent && parent->variant_type ==
26576 + YAFFS_OBJECT_TYPE_UNKNOWN) {
26577 + /* Set up as a directory */
26578 + parent->variant_type =
26579 + YAFFS_OBJECT_TYPE_DIRECTORY;
26580 + YINIT_LIST_HEAD(&parent->variant.
26583 + } else if (!parent || parent->variant_type !=
26584 + YAFFS_OBJECT_TYPE_DIRECTORY) {
26585 + /* Hoosterman, another problem....
26586 + * We're trying to use a non-directory as a directory
26589 + T(YAFFS_TRACE_ERROR,
26591 + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
26593 + parent = dev->lost_n_found;
26596 + yaffs_add_obj_to_dir(parent, in);
26598 + itsUnlinked = (parent == dev->del_dir) ||
26599 + (parent == dev->unlinked_dir);
26602 + /* Mark the block as having a shrinkHeader */
26603 + bi->has_shrink_hdr = 1;
26606 + /* Note re hardlinks.
26607 + * Since we might scan a hardlink before its equivalent object is scanned
26608 + * we put them all in a list.
26609 + * After scanning is complete, we should have all the objects, so we run
26610 + * through this list and fix up all the chains.
26613 + switch (in->variant_type) {
26614 + case YAFFS_OBJECT_TYPE_UNKNOWN:
26615 + /* Todo got a problem */
26617 + case YAFFS_OBJECT_TYPE_FILE:
26619 + if (in->variant.file_variant.
26620 + scanned_size < file_size) {
26621 + /* This covers the case where the file size is greater
26622 + * than where the data is
26623 + * This will happen if the file is resized to be larger
26624 + * than its current data extents.
26626 + in->variant.file_variant.file_size = file_size;
26627 + in->variant.file_variant.scanned_size = file_size;
26630 + if (in->variant.file_variant.shrink_size > file_size)
26631 + in->variant.file_variant.shrink_size = file_size;
26635 + case YAFFS_OBJECT_TYPE_HARDLINK:
26636 + if (!itsUnlinked) {
26637 + in->variant.hardlink_variant.equiv_id =
26639 + in->hard_links.next =
26640 + (struct ylist_head *) hard_list;
26644 + case YAFFS_OBJECT_TYPE_DIRECTORY:
26647 + case YAFFS_OBJECT_TYPE_SPECIAL:
26650 + case YAFFS_OBJECT_TYPE_SYMLINK:
26652 + in->variant.symlink_variant.alias =
26653 + yaffs_clone_str(oh->alias);
26654 + if (!in->variant.symlink_variant.alias)
26655 + alloc_failed = 1;
26664 + } /* End of scanning for each chunk */
26666 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
26667 + /* If we got this far while scanning, then the block is fully allocated. */
26668 + state = YAFFS_BLOCK_STATE_FULL;
26672 + bi->block_state = state;
26674 + /* Now let's see if it was dirty */
26675 + if (bi->pages_in_use == 0 &&
26676 + !bi->has_shrink_hdr &&
26677 + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
26678 + yaffs_block_became_dirty(dev, blk);
26683 + yaffs_skip_rest_of_block(dev);
26685 + if (altBlockIndex)
26686 + YFREE_ALT(blockIndex);
26688 + YFREE(blockIndex);
26690 + /* Ok, we've done all the scanning.
26691 + * Fix up the hard link chains.
26692 + * We should now have scanned all the objects, now it's time to add these
26695 + yaffs_link_fixup(dev, hard_list);
26698 + yaffs_release_temp_buffer(dev, chunkData, __LINE__);
26700 + if (alloc_failed)
26701 + return YAFFS_FAIL;
26703 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs2_scan_backwards ends" TENDSTR)));
26708 +++ b/fs/yaffs2/yaffs_yaffs2.h
26711 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
26713 + * Copyright (C) 2002-2010 Aleph One Ltd.
26714 + * for Toby Churchill Ltd and Brightstar Engineering
26716 + * Created by Charles Manning <charles@aleph1.co.uk>
26718 + * This program is free software; you can redistribute it and/or modify
26719 + * it under the terms of the GNU General Public License version 2 as
26720 + * published by the Free Software Foundation.
26723 +#ifndef __YAFFS_YAFFS2_H__
26724 +#define __YAFFS_YAFFS2_H__
26726 +#include "yaffs_guts.h"
26728 +void yaffs_calc_oldest_dirty_seq(yaffs_dev_t *dev);
26729 +void yaffs2_find_oldest_dirty_seq(yaffs_dev_t *dev);
26730 +void yaffs2_clear_oldest_dirty_seq(yaffs_dev_t *dev, yaffs_block_info_t *bi);
26731 +void yaffs2_update_oldest_dirty_seq(yaffs_dev_t *dev, unsigned block_no, yaffs_block_info_t *bi);
26732 +int yaffs_block_ok_for_gc(yaffs_dev_t *dev, yaffs_block_info_t *bi);
26733 +__u32 yaffs2_find_refresh_block(yaffs_dev_t *dev);
26734 +int yaffs2_checkpt_required(yaffs_dev_t *dev);
26735 +int yaffs_calc_checkpt_blocks_required(yaffs_dev_t *dev);
26738 +void yaffs2_checkpt_invalidate(yaffs_dev_t *dev);
26739 +int yaffs2_checkpt_save(yaffs_dev_t *dev);
26740 +int yaffs2_checkpt_restore(yaffs_dev_t *dev);
26742 +int yaffs2_handle_hole(yaffs_obj_t *obj, loff_t new_size);
26743 +int yaffs2_scan_backwards(yaffs_dev_t *dev);
26746 --- a/fs/yaffs2/yportenv.h
26747 +++ b/fs/yaffs2/yportenv.h
26750 * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
26752 - * Copyright (C) 2002-2007 Aleph One Ltd.
26753 + * Copyright (C) 2002-2010 Aleph One Ltd.
26754 * for Toby Churchill Ltd and Brightstar Engineering
26756 * Created by Charles Manning <charles@aleph1.co.uk>
26757 @@ -41,12 +41,14 @@
26758 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
26759 #include <linux/config.h>
26762 #include <linux/kernel.h>
26763 #include <linux/mm.h>
26764 #include <linux/sched.h>
26765 #include <linux/string.h>
26766 #include <linux/slab.h>
26767 #include <linux/vmalloc.h>
26768 +#include <linux/xattr.h>
26771 #define YUCHAR unsigned char
26772 @@ -55,11 +57,11 @@
26773 #define yaffs_strcpy(a, b) strcpy(a, b)
26774 #define yaffs_strncpy(a, b, c) strncpy(a, b, c)
26775 #define yaffs_strncmp(a, b, c) strncmp(a, b, c)
26776 -#define yaffs_strlen(s) strlen(s)
26777 +#define yaffs_strnlen(s,m) strnlen(s,m)
26778 #define yaffs_sprintf sprintf
26779 #define yaffs_toupper(a) toupper(a)
26781 -#define Y_INLINE inline
26782 +#define Y_INLINE __inline__
26784 #define YAFFS_LOSTNFOUND_NAME "lost+found"
26785 #define YAFFS_LOSTNFOUND_PREFIX "obj"
26786 @@ -71,11 +73,11 @@
26787 #define YFREE_ALT(x) vfree(x)
26788 #define YMALLOC_DMA(x) YMALLOC(x)
26790 -/* KR - added for use in scan so processes aren't blocked indefinitely. */
26791 #define YYIELD() schedule()
26792 +#define Y_DUMP_STACK() dump_stack()
26794 -#define YAFFS_ROOT_MODE 0666
26795 -#define YAFFS_LOSTNFOUND_MODE 0666
26796 +#define YAFFS_ROOT_MODE 0755
26797 +#define YAFFS_LOSTNFOUND_MODE 0700
26799 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
26800 #define Y_CURRENT_TIME CURRENT_TIME.tv_sec
26801 @@ -85,19 +87,14 @@
26802 #define Y_TIME_CONVERT(x) (x)
26805 -#define yaffs_SumCompare(x, y) ((x) == (y))
26806 +#define yaffs_sum_cmp(x, y) ((x) == (y))
26807 #define yaffs_strcmp(a, b) strcmp(a, b)
26809 #define TENDSTR "\n"
26810 -#define TSTR(x) KERN_WARNING x
26811 +#define TSTR(x) KERN_DEBUG x
26813 #define TOUT(p) printk p
26815 -#define yaffs_trace(mask, fmt, args...) \
26816 - do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
26817 - printk(KERN_WARNING "yaffs: " fmt, ## args); \
26820 #define compile_time_assertion(assertion) \
26821 ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
26823 @@ -116,7 +113,6 @@
26825 #include "string.h"
26827 -#include "devextras.h"
26829 #define YMALLOC(x) malloc(x)
26830 #define YFREE(x) free(x)
26831 @@ -129,7 +125,7 @@
26832 #define yaffs_strcat(a, b) strcat(a, b)
26833 #define yaffs_strcpy(a, b) strcpy(a, b)
26834 #define yaffs_strncpy(a, b, c) strncpy(a, b, c)
26835 -#define yaffs_strlen(s) strlen(s)
26836 +#define yaffs_strnlen(s,m) strnlen(s,m)
26837 #define yaffs_sprintf sprintf
26838 #define yaffs_toupper(a) toupper(a)
26840 @@ -146,10 +142,10 @@
26841 #define YAFFS_LOSTNFOUND_PREFIX "obj"
26842 /* #define YPRINTF(x) printf x */
26844 -#define YAFFS_ROOT_MODE 0666
26845 -#define YAFFS_LOSTNFOUND_MODE 0666
26846 +#define YAFFS_ROOT_MODE 0755
26847 +#define YAFFS_LOSTNFOUND_MODE 0700
26849 -#define yaffs_SumCompare(x, y) ((x) == (y))
26850 +#define yaffs_sum_cmp(x, y) ((x) == (y))
26851 #define yaffs_strcmp(a, b) strcmp(a, b)
26854 @@ -158,46 +154,180 @@
26858 -/* see yaffs_fs.c */
26859 -extern unsigned int yaffs_traceMask;
26860 -extern unsigned int yaffs_wr_attempts;
26861 +#if defined(CONFIG_YAFFS_DIRECT) || defined(CONFIG_YAFFS_WINCE)
26865 - * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
26867 +#ifdef CONFIG_YAFFSFS_PROVIDE_VALUES
26870 +#define O_RDONLY 00
26874 +#define O_WRONLY 01
26882 +#define O_CREAT 0100
26886 +#define O_EXCL 0200
26890 +#define O_TRUNC 01000
26894 +#define O_APPEND 02000
26898 +#define SEEK_SET 0
26902 +#define SEEK_CUR 1
26906 +#define SEEK_END 2
26946 +#define ENODATA 61
26950 +#define ENOTEMPTY 39
26953 +#ifndef ENAMETOOLONG
26954 +#define ENAMETOOLONG 36
26966 +#define ENOTDIR 20
26977 +#define S_IFMT 0170000
26981 +#define S_IFLNK 0120000
26984 -#define YAFFS_TRACE_OS 0x00000002
26985 -#define YAFFS_TRACE_ALLOCATE 0x00000004
26986 -#define YAFFS_TRACE_SCAN 0x00000008
26987 -#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
26988 -#define YAFFS_TRACE_ERASE 0x00000020
26989 -#define YAFFS_TRACE_GC 0x00000040
26990 -#define YAFFS_TRACE_WRITE 0x00000080
26991 -#define YAFFS_TRACE_TRACING 0x00000100
26992 -#define YAFFS_TRACE_DELETION 0x00000200
26993 -#define YAFFS_TRACE_BUFFERS 0x00000400
26994 -#define YAFFS_TRACE_NANDACCESS 0x00000800
26995 -#define YAFFS_TRACE_GC_DETAIL 0x00001000
26996 -#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
26997 -#define YAFFS_TRACE_MTD 0x00004000
26998 -#define YAFFS_TRACE_CHECKPOINT 0x00008000
27000 -#define YAFFS_TRACE_VERIFY 0x00010000
27001 -#define YAFFS_TRACE_VERIFY_NAND 0x00020000
27002 -#define YAFFS_TRACE_VERIFY_FULL 0x00040000
27003 -#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
27006 -#define YAFFS_TRACE_ERROR 0x40000000
27007 -#define YAFFS_TRACE_BUG 0x80000000
27008 -#define YAFFS_TRACE_ALWAYS 0xF0000000
27010 +#define S_IFDIR 0040000
27014 +#define S_IFREG 0100000
27018 +#define S_IREAD 0000400
27022 +#define S_IWRITE 0000200
27025 -#define T(mask, p) do { if ((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
27027 +#define S_IEXEC 0000100
27030 +#ifndef XATTR_CREATE
27031 +#define XATTR_CREATE 1
27034 +#ifndef XATTR_REPLACE
27035 +#define XATTR_REPLACE 2
27046 +#include <errno.h>
27047 +#include <sys/stat.h>
27048 +#include <fcntl.h>
27053 +#ifndef Y_DUMP_STACK
27054 +#define Y_DUMP_STACK() do { } while (0)
27058 -#define YBUG() do {T(YAFFS_TRACE_BUG, (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR), __LINE__)); } while (0)
27059 +#define YBUG() do {\
27060 + T(YAFFS_TRACE_BUG,\
27061 + (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),\