Merge branch 'master' of git+ssh://openadk.org/git/openadk
[openadk.git] / target / linux / patches / 2.6.30.1 / yaffs2.patch
bloba19ab9c8447b4850ede6f69e39086db16341b825
1 diff -Nur linux-2.6.30.orig/fs/Kconfig linux-2.6.30/fs/Kconfig
2 --- linux-2.6.30.orig/fs/Kconfig 2009-06-10 05:05:27.000000000 +0200
3 +++ linux-2.6.30/fs/Kconfig 2009-06-11 09:21:04.000000000 +0200
4 @@ -162,6 +162,10 @@
5 source "fs/befs/Kconfig"
6 source "fs/bfs/Kconfig"
7 source "fs/efs/Kconfig"
9 +# Patched by YAFFS
10 +source "fs/yaffs2/Kconfig"
12 source "fs/jffs2/Kconfig"
13 # UBIFS File system configuration
14 source "fs/ubifs/Kconfig"
15 diff -Nur linux-2.6.30.orig/fs/Makefile linux-2.6.30/fs/Makefile
16 --- linux-2.6.30.orig/fs/Makefile 2009-06-10 05:05:27.000000000 +0200
17 +++ linux-2.6.30/fs/Makefile 2009-06-11 09:21:31.000000000 +0200
18 @@ -124,3 +124,4 @@
19 obj-$(CONFIG_BTRFS_FS) += btrfs/
20 obj-$(CONFIG_GFS2_FS) += gfs2/
21 obj-$(CONFIG_EXOFS_FS) += exofs/
22 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
23 diff -Nur linux-2.6.30.orig/fs/Makefile.pre.yaffs linux-2.6.30/fs/Makefile.pre.yaffs
24 --- linux-2.6.30.orig/fs/Makefile.pre.yaffs 1970-01-01 01:00:00.000000000 +0100
25 +++ linux-2.6.30/fs/Makefile.pre.yaffs 2009-06-11 09:21:04.000000000 +0200
26 @@ -0,0 +1,126 @@
28 +# Makefile for the Linux filesystems.
30 +# 14 Sep 2000, Christoph Hellwig <hch@infradead.org>
31 +# Rewritten to use lists instead of if-statements.
32 +#
34 +obj-y := open.o read_write.o file_table.o super.o \
35 + char_dev.o stat.o exec.o pipe.o namei.o fcntl.o \
36 + ioctl.o readdir.o select.o fifo.o dcache.o inode.o \
37 + attr.o bad_inode.o file.o filesystems.o namespace.o \
38 + seq_file.o xattr.o libfs.o fs-writeback.o \
39 + pnode.o drop_caches.o splice.o sync.o utimes.o \
40 + stack.o
42 +ifeq ($(CONFIG_BLOCK),y)
43 +obj-y += buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
44 +else
45 +obj-y += no-block.o
46 +endif
48 +obj-$(CONFIG_BLK_DEV_INTEGRITY) += bio-integrity.o
49 +obj-y += notify/
50 +obj-$(CONFIG_EPOLL) += eventpoll.o
51 +obj-$(CONFIG_ANON_INODES) += anon_inodes.o
52 +obj-$(CONFIG_SIGNALFD) += signalfd.o
53 +obj-$(CONFIG_TIMERFD) += timerfd.o
54 +obj-$(CONFIG_EVENTFD) += eventfd.o
55 +obj-$(CONFIG_AIO) += aio.o
56 +obj-$(CONFIG_FILE_LOCKING) += locks.o
57 +obj-$(CONFIG_COMPAT) += compat.o compat_ioctl.o
59 +nfsd-$(CONFIG_NFSD) := nfsctl.o
60 +obj-y += $(nfsd-y) $(nfsd-m)
62 +obj-$(CONFIG_BINFMT_AOUT) += binfmt_aout.o
63 +obj-$(CONFIG_BINFMT_EM86) += binfmt_em86.o
64 +obj-$(CONFIG_BINFMT_MISC) += binfmt_misc.o
66 +# binfmt_script is always there
67 +obj-y += binfmt_script.o
69 +obj-$(CONFIG_BINFMT_ELF) += binfmt_elf.o
70 +obj-$(CONFIG_COMPAT_BINFMT_ELF) += compat_binfmt_elf.o
71 +obj-$(CONFIG_BINFMT_ELF_FDPIC) += binfmt_elf_fdpic.o
72 +obj-$(CONFIG_BINFMT_SOM) += binfmt_som.o
73 +obj-$(CONFIG_BINFMT_FLAT) += binfmt_flat.o
75 +obj-$(CONFIG_FS_MBCACHE) += mbcache.o
76 +obj-$(CONFIG_FS_POSIX_ACL) += posix_acl.o xattr_acl.o
77 +obj-$(CONFIG_NFS_COMMON) += nfs_common/
78 +obj-$(CONFIG_GENERIC_ACL) += generic_acl.o
80 +obj-$(CONFIG_QUOTA) += dquot.o
81 +obj-$(CONFIG_QFMT_V1) += quota_v1.o
82 +obj-$(CONFIG_QFMT_V2) += quota_v2.o
83 +obj-$(CONFIG_QUOTA_TREE) += quota_tree.o
84 +obj-$(CONFIG_QUOTACTL) += quota.o
86 +obj-$(CONFIG_PROC_FS) += proc/
87 +obj-y += partitions/
88 +obj-$(CONFIG_SYSFS) += sysfs/
89 +obj-$(CONFIG_CONFIGFS_FS) += configfs/
90 +obj-y += devpts/
92 +obj-$(CONFIG_PROFILING) += dcookies.o
93 +obj-$(CONFIG_DLM) += dlm/
95 +# Do not add any filesystems before this line
96 +obj-$(CONFIG_REISERFS_FS) += reiserfs/
97 +obj-$(CONFIG_EXT3_FS) += ext3/ # Before ext2 so root fs can be ext3
98 +obj-$(CONFIG_EXT2_FS) += ext2/
99 +# We place ext4 after ext2 so plain ext2 root fs's are mounted using ext2
100 +# unless explicitly requested by rootfstype
101 +obj-$(CONFIG_EXT4_FS) += ext4/
102 +obj-$(CONFIG_JBD) += jbd/
103 +obj-$(CONFIG_JBD2) += jbd2/
104 +obj-$(CONFIG_CRAMFS) += cramfs/
105 +obj-$(CONFIG_SQUASHFS) += squashfs/
106 +obj-y += ramfs/
107 +obj-$(CONFIG_HUGETLBFS) += hugetlbfs/
108 +obj-$(CONFIG_CODA_FS) += coda/
109 +obj-$(CONFIG_MINIX_FS) += minix/
110 +obj-$(CONFIG_FAT_FS) += fat/
111 +obj-$(CONFIG_BFS_FS) += bfs/
112 +obj-$(CONFIG_ISO9660_FS) += isofs/
113 +obj-$(CONFIG_HFSPLUS_FS) += hfsplus/ # Before hfs to find wrapped HFS+
114 +obj-$(CONFIG_HFS_FS) += hfs/
115 +obj-$(CONFIG_ECRYPT_FS) += ecryptfs/
116 +obj-$(CONFIG_VXFS_FS) += freevxfs/
117 +obj-$(CONFIG_NFS_FS) += nfs/
118 +obj-$(CONFIG_EXPORTFS) += exportfs/
119 +obj-$(CONFIG_NFSD) += nfsd/
120 +obj-$(CONFIG_LOCKD) += lockd/
121 +obj-$(CONFIG_NLS) += nls/
122 +obj-$(CONFIG_SYSV_FS) += sysv/
123 +obj-$(CONFIG_SMB_FS) += smbfs/
124 +obj-$(CONFIG_CIFS) += cifs/
125 +obj-$(CONFIG_NCP_FS) += ncpfs/
126 +obj-$(CONFIG_HPFS_FS) += hpfs/
127 +obj-$(CONFIG_NTFS_FS) += ntfs/
128 +obj-$(CONFIG_UFS_FS) += ufs/
129 +obj-$(CONFIG_EFS_FS) += efs/
130 +obj-$(CONFIG_JFFS2_FS) += jffs2/
131 +obj-$(CONFIG_UBIFS_FS) += ubifs/
132 +obj-$(CONFIG_AFFS_FS) += affs/
133 +obj-$(CONFIG_ROMFS_FS) += romfs/
134 +obj-$(CONFIG_QNX4FS_FS) += qnx4/
135 +obj-$(CONFIG_AUTOFS_FS) += autofs/
136 +obj-$(CONFIG_AUTOFS4_FS) += autofs4/
137 +obj-$(CONFIG_ADFS_FS) += adfs/
138 +obj-$(CONFIG_FUSE_FS) += fuse/
139 +obj-$(CONFIG_UDF_FS) += udf/
140 +obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
141 +obj-$(CONFIG_OMFS_FS) += omfs/
142 +obj-$(CONFIG_JFS_FS) += jfs/
143 +obj-$(CONFIG_XFS_FS) += xfs/
144 +obj-$(CONFIG_9P_FS) += 9p/
145 +obj-$(CONFIG_AFS_FS) += afs/
146 +obj-$(CONFIG_BEFS_FS) += befs/
147 +obj-$(CONFIG_HOSTFS) += hostfs/
148 +obj-$(CONFIG_HPPFS) += hppfs/
149 +obj-$(CONFIG_DEBUG_FS) += debugfs/
150 +obj-$(CONFIG_OCFS2_FS) += ocfs2/
151 +obj-$(CONFIG_BTRFS_FS) += btrfs/
152 +obj-$(CONFIG_GFS2_FS) += gfs2/
153 diff -Nur linux-2.6.30.orig/fs/yaffs2/devextras.h linux-2.6.30/fs/yaffs2/devextras.h
154 --- linux-2.6.30.orig/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
155 +++ linux-2.6.30/fs/yaffs2/devextras.h 2009-06-11 09:21:04.000000000 +0200
156 @@ -0,0 +1,196 @@
158 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
160 + * Copyright (C) 2002-2007 Aleph One Ltd.
161 + * for Toby Churchill Ltd and Brightstar Engineering
163 + * Created by Charles Manning <charles@aleph1.co.uk>
165 + * This program is free software; you can redistribute it and/or modify
166 + * it under the terms of the GNU Lesser General Public License version 2.1 as
167 + * published by the Free Software Foundation.
169 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
170 + */
173 + * This file is just holds extra declarations of macros that would normally
174 + * be providesd in the Linux kernel. These macros have been written from
175 + * scratch but are functionally equivalent to the Linux ones.
177 + */
179 +#ifndef __EXTRAS_H__
180 +#define __EXTRAS_H__
183 +#if !(defined __KERNEL__)
185 +/* Definition of types */
186 +typedef unsigned char __u8;
187 +typedef unsigned short __u16;
188 +typedef unsigned __u32;
190 +#endif
193 + * This is a simple doubly linked list implementation that matches the
194 + * way the Linux kernel doubly linked list implementation works.
195 + */
197 +struct ylist_head {
198 + struct ylist_head *next; /* next in chain */
199 + struct ylist_head *prev; /* previous in chain */
203 +/* Initialise a static list */
204 +#define YLIST_HEAD(name) \
205 +struct ylist_head name = { &(name), &(name)}
209 +/* Initialise a list head to an empty list */
210 +#define YINIT_LIST_HEAD(p) \
211 +do { \
212 + (p)->next = (p);\
213 + (p)->prev = (p); \
214 +} while (0)
217 +/* Add an element to a list */
218 +static __inline__ void ylist_add(struct ylist_head *newEntry,
219 + struct ylist_head *list)
221 + struct ylist_head *listNext = list->next;
223 + list->next = newEntry;
224 + newEntry->prev = list;
225 + newEntry->next = listNext;
226 + listNext->prev = newEntry;
230 +static __inline__ void ylist_add_tail(struct ylist_head *newEntry,
231 + struct ylist_head *list)
233 + struct ylist_head *listPrev = list->prev;
235 + list->prev = newEntry;
236 + newEntry->next = list;
237 + newEntry->prev = listPrev;
238 + listPrev->next = newEntry;
243 +/* Take an element out of its current list, with or without
244 + * reinitialising the links.of the entry*/
245 +static __inline__ void ylist_del(struct ylist_head *entry)
247 + struct ylist_head *listNext = entry->next;
248 + struct ylist_head *listPrev = entry->prev;
250 + listNext->prev = listPrev;
251 + listPrev->next = listNext;
255 +static __inline__ void ylist_del_init(struct ylist_head *entry)
257 + ylist_del(entry);
258 + entry->next = entry->prev = entry;
262 +/* Test if the list is empty */
263 +static __inline__ int ylist_empty(struct ylist_head *entry)
265 + return (entry->next == entry);
269 +/* ylist_entry takes a pointer to a list entry and offsets it to that
270 + * we can find a pointer to the object it is embedded in.
271 + */
274 +#define ylist_entry(entry, type, member) \
275 + ((type *)((char *)(entry)-(unsigned long)(&((type *)NULL)->member)))
278 +/* ylist_for_each and list_for_each_safe iterate over lists.
279 + * ylist_for_each_safe uses temporary storage to make the list delete safe
280 + */
282 +#define ylist_for_each(itervar, list) \
283 + for (itervar = (list)->next; itervar != (list); itervar = itervar->next)
285 +#define ylist_for_each_safe(itervar, saveVar, list) \
286 + for (itervar = (list)->next, saveVar = (list)->next->next; \
287 + itervar != (list); itervar = saveVar, saveVar = saveVar->next)
290 +#if !(defined __KERNEL__)
293 +#ifndef WIN32
294 +#include <sys/stat.h>
295 +#endif
298 +#ifdef CONFIG_YAFFS_PROVIDE_DEFS
299 +/* File types */
302 +#define DT_UNKNOWN 0
303 +#define DT_FIFO 1
304 +#define DT_CHR 2
305 +#define DT_DIR 4
306 +#define DT_BLK 6
307 +#define DT_REG 8
308 +#define DT_LNK 10
309 +#define DT_SOCK 12
310 +#define DT_WHT 14
313 +#ifndef WIN32
314 +#include <sys/stat.h>
315 +#endif
318 + * Attribute flags. These should be or-ed together to figure out what
319 + * has been changed!
320 + */
321 +#define ATTR_MODE 1
322 +#define ATTR_UID 2
323 +#define ATTR_GID 4
324 +#define ATTR_SIZE 8
325 +#define ATTR_ATIME 16
326 +#define ATTR_MTIME 32
327 +#define ATTR_CTIME 64
329 +struct iattr {
330 + unsigned int ia_valid;
331 + unsigned ia_mode;
332 + unsigned ia_uid;
333 + unsigned ia_gid;
334 + unsigned ia_size;
335 + unsigned ia_atime;
336 + unsigned ia_mtime;
337 + unsigned ia_ctime;
338 + unsigned int ia_attr_flags;
341 +#endif
343 +#else
345 +#include <linux/types.h>
346 +#include <linux/fs.h>
347 +#include <linux/stat.h>
349 +#endif
352 +#endif
353 diff -Nur linux-2.6.30.orig/fs/yaffs2/Kconfig linux-2.6.30/fs/yaffs2/Kconfig
354 --- linux-2.6.30.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
355 +++ linux-2.6.30/fs/yaffs2/Kconfig 2009-06-11 09:21:04.000000000 +0200
356 @@ -0,0 +1,156 @@
358 +# YAFFS file system configurations
361 +config YAFFS_FS
362 + tristate "YAFFS2 file system support"
363 + default n
364 + depends on MTD_BLOCK
365 + select YAFFS_YAFFS1
366 + select YAFFS_YAFFS2
367 + help
368 + YAFFS2, or Yet Another Flash Filing System, is a filing system
369 + optimised for NAND Flash chips.
371 + To compile the YAFFS2 file system support as a module, choose M
372 + here: the module will be called yaffs2.
374 + If unsure, say N.
376 + Further information on YAFFS2 is available at
377 + <http://www.aleph1.co.uk/yaffs/>.
379 +config YAFFS_YAFFS1
380 + bool "512 byte / page devices"
381 + depends on YAFFS_FS
382 + default y
383 + help
384 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
386 + Not needed for 2K-page devices.
388 + If unsure, say Y.
390 +config YAFFS_9BYTE_TAGS
391 + bool "Use older-style on-NAND data format with pageStatus byte"
392 + depends on YAFFS_YAFFS1
393 + default n
394 + help
396 + Older-style on-NAND data format has a "pageStatus" byte to record
397 + chunk/page state. This byte is zero when the page is discarded.
398 + Choose this option if you have existing on-NAND data using this
399 + format that you need to continue to support. New data written
400 + also uses the older-style format. Note: Use of this option
401 + generally requires that MTD's oob layout be adjusted to use the
402 + older-style format. See notes on tags formats and MTD versions
403 + in yaffs_mtdif1.c.
405 + If unsure, say N.
407 +config YAFFS_DOES_ECC
408 + bool "Lets Yaffs do its own ECC"
409 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
410 + default n
411 + help
412 + This enables Yaffs to use its own ECC functions instead of using
413 + the ones from the generic MTD-NAND driver.
415 + If unsure, say N.
417 +config YAFFS_ECC_WRONG_ORDER
418 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
419 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
420 + default n
421 + help
422 + This makes yaffs_ecc.c use the same ecc byte order as Steven
423 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
424 + order as SmartMedia.
426 + If unsure, say N.
428 +config YAFFS_YAFFS2
429 + bool "2048 byte (or larger) / page devices"
430 + depends on YAFFS_FS
431 + default y
432 + help
433 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
435 + If unsure, say Y.
437 +config YAFFS_AUTO_YAFFS2
438 + bool "Autoselect yaffs2 format"
439 + depends on YAFFS_YAFFS2
440 + default y
441 + help
442 + Without this, you need to explicitely use yaffs2 as the file
443 + system type. With this, you can say "yaffs" and yaffs or yaffs2
444 + will be used depending on the device page size (yaffs on
445 + 512-byte page devices, yaffs2 on 2K page devices).
447 + If unsure, say Y.
449 +config YAFFS_DISABLE_LAZY_LOAD
450 + bool "Disable lazy loading"
451 + depends on YAFFS_YAFFS2
452 + default n
453 + help
454 + "Lazy loading" defers loading file details until they are
455 + required. This saves mount time, but makes the first look-up
456 + a bit longer.
458 + Lazy loading will only happen if enabled by this option being 'n'
459 + and if the appropriate tags are available, else yaffs2 will
460 + automatically fall back to immediate loading and do the right
461 + thing.
463 + Lazy laoding will be required by checkpointing.
465 + Setting this to 'y' will disable lazy loading.
467 + If unsure, say N.
470 +config YAFFS_DISABLE_WIDE_TNODES
471 + bool "Turn off wide tnodes"
472 + depends on YAFFS_FS
473 + default n
474 + help
475 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
476 + page devices and >=128MB for 2k page devices. They use slightly
477 + more RAM but are faster since they eliminate chunk group
478 + searching.
480 + Setting this to 'y' will force tnode width to 16 bits and save
481 + memory but make large arrays slower.
483 + If unsure, say N.
485 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
486 + bool "Force chunk erase check"
487 + depends on YAFFS_FS
488 + default n
489 + help
490 + Normally YAFFS only checks chunks before writing until an erased
491 + chunk is found. This helps to detect any partially written
492 + chunks that might have happened due to power loss.
494 + Enabling this forces on the test that chunks are erased in flash
495 + before writing to them. This takes more time but is potentially
496 + a bit more secure.
498 + Suggest setting Y during development and ironing out driver
499 + issues etc. Suggest setting to N if you want faster writing.
501 + If unsure, say Y.
503 +config YAFFS_SHORT_NAMES_IN_RAM
504 + bool "Cache short names in RAM"
505 + depends on YAFFS_FS
506 + default y
507 + help
508 + If this config is set, then short names are stored with the
509 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
510 + but makes look-ups faster.
512 + If unsure, say Y.
513 diff -Nur linux-2.6.30.orig/fs/yaffs2/Makefile linux-2.6.30/fs/yaffs2/Makefile
514 --- linux-2.6.30.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
515 +++ linux-2.6.30/fs/yaffs2/Makefile 2009-06-11 09:21:04.000000000 +0200
516 @@ -0,0 +1,10 @@
518 +# Makefile for the linux YAFFS filesystem routines.
521 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
523 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
524 +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
525 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
526 +yaffs-y += yaffs_mtdif.o yaffs_mtdif1.o yaffs_mtdif2.o
527 diff -Nur linux-2.6.30.orig/fs/yaffs2/moduleconfig.h linux-2.6.30/fs/yaffs2/moduleconfig.h
528 --- linux-2.6.30.orig/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
529 +++ linux-2.6.30/fs/yaffs2/moduleconfig.h 2009-06-11 09:21:04.000000000 +0200
530 @@ -0,0 +1,65 @@
532 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
534 + * Copyright (C) 2002-2007 Aleph One Ltd.
535 + * for Toby Churchill Ltd and Brightstar Engineering
537 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
539 + * This program is free software; you can redistribute it and/or modify
540 + * it under the terms of the GNU Lesser General Public License version 2.1 as
541 + * published by the Free Software Foundation.
543 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
544 + */
546 +#ifndef __YAFFS_CONFIG_H__
547 +#define __YAFFS_CONFIG_H__
549 +#ifdef YAFFS_OUT_OF_TREE
551 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
552 +#define CONFIG_YAFFS_FS
553 +#define CONFIG_YAFFS_YAFFS1
554 +#define CONFIG_YAFFS_YAFFS2
556 +/* These options are independent of each other. Select those that matter. */
558 +/* Default: Not selected */
559 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
560 +/* #define CONFIG_YAFFS_DOES_ECC */
562 +/* Default: Not selected */
563 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
564 +/* CONFIG_YAFFS_DOES_ECC is set */
565 +/* #define CONFIG_YAFFS_ECC_WRONG_ORDER */
567 +/* Default: Selected */
568 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
569 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
571 +/* Default: Selected */
572 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
573 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
575 +/* Default: 10 */
576 +/* Meaning: set the count of blocks to reserve for checkpointing */
577 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
580 +Older-style on-NAND data format has a "pageStatus" byte to record
581 +chunk/page state. This byte is zeroed when the page is discarded.
582 +Choose this option if you have existing on-NAND data in this format
583 +that you need to continue to support. New data written also uses the
584 +older-style format.
585 +Note: Use of this option generally requires that MTD's oob layout be
586 +adjusted to use the older-style format. See notes on tags formats and
587 +MTD versions in yaffs_mtdif1.c.
589 +/* Default: Not selected */
590 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
591 +/* #define CONFIG_YAFFS_9BYTE_TAGS */
593 +#endif /* YAFFS_OUT_OF_TREE */
595 +#endif /* __YAFFS_CONFIG_H__ */
596 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_checkptrw.c linux-2.6.30/fs/yaffs2/yaffs_checkptrw.c
597 --- linux-2.6.30.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
598 +++ linux-2.6.30/fs/yaffs2/yaffs_checkptrw.c 2009-06-11 09:21:04.000000000 +0200
599 @@ -0,0 +1,394 @@
601 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
603 + * Copyright (C) 2002-2007 Aleph One Ltd.
604 + * for Toby Churchill Ltd and Brightstar Engineering
606 + * Created by Charles Manning <charles@aleph1.co.uk>
608 + * This program is free software; you can redistribute it and/or modify
609 + * it under the terms of the GNU General Public License version 2 as
610 + * published by the Free Software Foundation.
611 + */
613 +const char *yaffs_checkptrw_c_version =
614 + "$Id: yaffs_checkptrw.c,v 1.18 2009-03-06 17:20:49 wookey Exp $";
617 +#include "yaffs_checkptrw.h"
618 +#include "yaffs_getblockinfo.h"
620 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
622 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
624 + T(YAFFS_TRACE_CHECKPOINT,
625 + (TSTR("checkpt blocks available = %d" TENDSTR),
626 + blocksAvailable));
628 + return (blocksAvailable <= 0) ? 0 : 1;
632 +static int yaffs_CheckpointErase(yaffs_Device *dev)
634 + int i;
636 + if (!dev->eraseBlockInNAND)
637 + return 0;
638 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checking blocks %d to %d"TENDSTR),
639 + dev->internalStartBlock, dev->internalEndBlock));
641 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
642 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
643 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
644 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("erasing checkpt block %d"TENDSTR), i));
645 + if (dev->eraseBlockInNAND(dev, i - dev->blockOffset /* realign */)) {
646 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
647 + dev->nErasedBlocks++;
648 + dev->nFreeChunks += dev->nChunksPerBlock;
649 + } else {
650 + dev->markNANDBlockBad(dev, i);
651 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
656 + dev->blocksInCheckpoint = 0;
658 + return 1;
662 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
664 + int i;
665 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
666 + T(YAFFS_TRACE_CHECKPOINT,
667 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
668 + dev->nErasedBlocks, dev->nReservedBlocks, blocksAvailable, dev->checkpointNextBlock));
670 + if (dev->checkpointNextBlock >= 0 &&
671 + dev->checkpointNextBlock <= dev->internalEndBlock &&
672 + blocksAvailable > 0) {
674 + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
675 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
676 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
677 + dev->checkpointNextBlock = i + 1;
678 + dev->checkpointCurrentBlock = i;
679 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("allocating checkpt block %d"TENDSTR), i));
680 + return;
684 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("out of checkpt blocks"TENDSTR)));
686 + dev->checkpointNextBlock = -1;
687 + dev->checkpointCurrentBlock = -1;
690 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
692 + int i;
693 + yaffs_ExtendedTags tags;
695 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
696 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
698 + if (dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
699 + for (i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++) {
700 + int chunk = i * dev->nChunksPerBlock;
701 + int realignedChunk = chunk - dev->chunkOffset;
703 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
704 + NULL, &tags);
705 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
706 + i, tags.objectId, tags.sequenceNumber, tags.eccResult));
708 + if (tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA) {
709 + /* Right kind of block */
710 + dev->checkpointNextBlock = tags.objectId;
711 + dev->checkpointCurrentBlock = i;
712 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
713 + dev->blocksInCheckpoint++;
714 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found checkpt block %d"TENDSTR), i));
715 + return;
719 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("found no more checkpt blocks"TENDSTR)));
721 + dev->checkpointNextBlock = -1;
722 + dev->checkpointCurrentBlock = -1;
726 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
729 + /* Got the functions we need? */
730 + if (!dev->writeChunkWithTagsToNAND ||
731 + !dev->readChunkWithTagsFromNAND ||
732 + !dev->eraseBlockInNAND ||
733 + !dev->markNANDBlockBad)
734 + return 0;
736 + if (forWriting && !yaffs_CheckpointSpaceOk(dev))
737 + return 0;
739 + if (!dev->checkpointBuffer)
740 + dev->checkpointBuffer = YMALLOC_DMA(dev->totalBytesPerChunk);
741 + if (!dev->checkpointBuffer)
742 + return 0;
745 + dev->checkpointPageSequence = 0;
747 + dev->checkpointOpenForWrite = forWriting;
749 + dev->checkpointByteCount = 0;
750 + dev->checkpointSum = 0;
751 + dev->checkpointXor = 0;
752 + dev->checkpointCurrentBlock = -1;
753 + dev->checkpointCurrentChunk = -1;
754 + dev->checkpointNextBlock = dev->internalStartBlock;
756 + /* Erase all the blocks in the checkpoint area */
757 + if (forWriting) {
758 + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
759 + dev->checkpointByteOffset = 0;
760 + return yaffs_CheckpointErase(dev);
761 + } else {
762 + int i;
763 + /* Set to a value that will kick off a read */
764 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
765 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
766 + * going to be way more than we need */
767 + dev->blocksInCheckpoint = 0;
768 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
769 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
770 + for (i = 0; i < dev->checkpointMaxBlocks; i++)
771 + dev->checkpointBlockList[i] = -1;
774 + return 1;
777 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
779 + __u32 compositeSum;
780 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
781 + *sum = compositeSum;
782 + return 1;
785 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
787 + int chunk;
788 + int realignedChunk;
790 + yaffs_ExtendedTags tags;
792 + if (dev->checkpointCurrentBlock < 0) {
793 + yaffs_CheckpointFindNextErasedBlock(dev);
794 + dev->checkpointCurrentChunk = 0;
797 + if (dev->checkpointCurrentBlock < 0)
798 + return 0;
800 + tags.chunkDeleted = 0;
801 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
802 + tags.chunkId = dev->checkpointPageSequence + 1;
803 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
804 + tags.byteCount = dev->nDataBytesPerChunk;
805 + if (dev->checkpointCurrentChunk == 0) {
806 + /* First chunk we write for the block? Set block state to
807 + checkpoint */
808 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointCurrentBlock);
809 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
810 + dev->blocksInCheckpoint++;
813 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
816 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
817 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk, tags.objectId, tags.chunkId));
819 + realignedChunk = chunk - dev->chunkOffset;
821 + dev->writeChunkWithTagsToNAND(dev, realignedChunk,
822 + dev->checkpointBuffer, &tags);
823 + dev->checkpointByteOffset = 0;
824 + dev->checkpointPageSequence++;
825 + dev->checkpointCurrentChunk++;
826 + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock) {
827 + dev->checkpointCurrentChunk = 0;
828 + dev->checkpointCurrentBlock = -1;
830 + memset(dev->checkpointBuffer, 0, dev->nDataBytesPerChunk);
832 + return 1;
836 +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes)
838 + int i = 0;
839 + int ok = 1;
842 + __u8 * dataBytes = (__u8 *)data;
846 + if (!dev->checkpointBuffer)
847 + return 0;
849 + if (!dev->checkpointOpenForWrite)
850 + return -1;
852 + while (i < nBytes && ok) {
853 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes;
854 + dev->checkpointSum += *dataBytes;
855 + dev->checkpointXor ^= *dataBytes;
857 + dev->checkpointByteOffset++;
858 + i++;
859 + dataBytes++;
860 + dev->checkpointByteCount++;
863 + if (dev->checkpointByteOffset < 0 ||
864 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
865 + ok = yaffs_CheckpointFlushBuffer(dev);
868 + return i;
871 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
873 + int i = 0;
874 + int ok = 1;
875 + yaffs_ExtendedTags tags;
878 + int chunk;
879 + int realignedChunk;
881 + __u8 *dataBytes = (__u8 *)data;
883 + if (!dev->checkpointBuffer)
884 + return 0;
886 + if (dev->checkpointOpenForWrite)
887 + return -1;
889 + while (i < nBytes && ok) {
892 + if (dev->checkpointByteOffset < 0 ||
893 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
895 + if (dev->checkpointCurrentBlock < 0) {
896 + yaffs_CheckpointFindNextCheckpointBlock(dev);
897 + dev->checkpointCurrentChunk = 0;
900 + if (dev->checkpointCurrentBlock < 0)
901 + ok = 0;
902 + else {
903 + chunk = dev->checkpointCurrentBlock *
904 + dev->nChunksPerBlock +
905 + dev->checkpointCurrentChunk;
907 + realignedChunk = chunk - dev->chunkOffset;
909 + /* read in the next chunk */
910 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
911 + dev->readChunkWithTagsFromNAND(dev,
912 + realignedChunk,
913 + dev->checkpointBuffer,
914 + &tags);
916 + if (tags.chunkId != (dev->checkpointPageSequence + 1) ||
917 + tags.eccResult > YAFFS_ECC_RESULT_FIXED ||
918 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
919 + ok = 0;
921 + dev->checkpointByteOffset = 0;
922 + dev->checkpointPageSequence++;
923 + dev->checkpointCurrentChunk++;
925 + if (dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
926 + dev->checkpointCurrentBlock = -1;
930 + if (ok) {
931 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
932 + dev->checkpointSum += *dataBytes;
933 + dev->checkpointXor ^= *dataBytes;
934 + dev->checkpointByteOffset++;
935 + i++;
936 + dataBytes++;
937 + dev->checkpointByteCount++;
941 + return i;
944 +int yaffs_CheckpointClose(yaffs_Device *dev)
947 + if (dev->checkpointOpenForWrite) {
948 + if (dev->checkpointByteOffset != 0)
949 + yaffs_CheckpointFlushBuffer(dev);
950 + } else {
951 + int i;
952 + for (i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++) {
953 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, dev->checkpointBlockList[i]);
954 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
955 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
956 + else {
957 + /* Todo this looks odd... */
960 + YFREE(dev->checkpointBlockList);
961 + dev->checkpointBlockList = NULL;
964 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
965 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
968 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint byte count %d" TENDSTR),
969 + dev->checkpointByteCount));
971 + if (dev->checkpointBuffer) {
972 + /* free the buffer */
973 + YFREE(dev->checkpointBuffer);
974 + dev->checkpointBuffer = NULL;
975 + return 1;
976 + } else
977 + return 0;
980 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
982 + /* Erase the first checksum block */
984 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("checkpoint invalidate"TENDSTR)));
986 + if (!yaffs_CheckpointSpaceOk(dev))
987 + return 0;
989 + return yaffs_CheckpointErase(dev);
994 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_checkptrw.h linux-2.6.30/fs/yaffs2/yaffs_checkptrw.h
995 --- linux-2.6.30.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
996 +++ linux-2.6.30/fs/yaffs2/yaffs_checkptrw.h 2009-06-11 09:21:04.000000000 +0200
997 @@ -0,0 +1,35 @@
999 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1001 + * Copyright (C) 2002-2007 Aleph One Ltd.
1002 + * for Toby Churchill Ltd and Brightstar Engineering
1004 + * Created by Charles Manning <charles@aleph1.co.uk>
1006 + * This program is free software; you can redistribute it and/or modify
1007 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1008 + * published by the Free Software Foundation.
1010 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1011 + */
1013 +#ifndef __YAFFS_CHECKPTRW_H__
1014 +#define __YAFFS_CHECKPTRW_H__
1016 +#include "yaffs_guts.h"
1018 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
1020 +int yaffs_CheckpointWrite(yaffs_Device *dev, const void *data, int nBytes);
1022 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes);
1024 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
1026 +int yaffs_CheckpointClose(yaffs_Device *dev);
1028 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
1031 +#endif
1033 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_ecc.c linux-2.6.30/fs/yaffs2/yaffs_ecc.c
1034 --- linux-2.6.30.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
1035 +++ linux-2.6.30/fs/yaffs2/yaffs_ecc.c 2009-06-11 09:21:04.000000000 +0200
1036 @@ -0,0 +1,326 @@
1038 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1040 + * Copyright (C) 2002-2007 Aleph One Ltd.
1041 + * for Toby Churchill Ltd and Brightstar Engineering
1043 + * Created by Charles Manning <charles@aleph1.co.uk>
1045 + * This program is free software; you can redistribute it and/or modify
1046 + * it under the terms of the GNU General Public License version 2 as
1047 + * published by the Free Software Foundation.
1048 + */
1051 + * This code implements the ECC algorithm used in SmartMedia.
1053 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1054 + * The two unused bit are set to 1.
1055 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1056 + * blocks are used on a 512-byte NAND page.
1058 + */
1060 +/* Table generated by gen-ecc.c
1061 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1062 + * for each byte of data. These are instead provided in a table in bits7..2.
1063 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1064 + * this bytes influence on the line parity.
1065 + */
1067 +const char *yaffs_ecc_c_version =
1068 + "$Id: yaffs_ecc.c,v 1.11 2009-03-06 17:20:50 wookey Exp $";
1070 +#include "yportenv.h"
1072 +#include "yaffs_ecc.h"
1074 +static const unsigned char column_parity_table[] = {
1075 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1076 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1077 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1078 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1079 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1080 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1081 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1082 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1083 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1084 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1085 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1086 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1087 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1088 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1089 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1090 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1091 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1092 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1093 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1094 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1095 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1096 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1097 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1098 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1099 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1100 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1101 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1102 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1103 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1104 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1105 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1106 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1109 +/* Count the bits in an unsigned char or a U32 */
1111 +static int yaffs_CountBits(unsigned char x)
1113 + int r = 0;
1114 + while (x) {
1115 + if (x & 1)
1116 + r++;
1117 + x >>= 1;
1119 + return r;
1122 +static int yaffs_CountBits32(unsigned x)
1124 + int r = 0;
1125 + while (x) {
1126 + if (x & 1)
1127 + r++;
1128 + x >>= 1;
1130 + return r;
1133 +/* Calculate the ECC for a 256-byte block of data */
1134 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1136 + unsigned int i;
1138 + unsigned char col_parity = 0;
1139 + unsigned char line_parity = 0;
1140 + unsigned char line_parity_prime = 0;
1141 + unsigned char t;
1142 + unsigned char b;
1144 + for (i = 0; i < 256; i++) {
1145 + b = column_parity_table[*data++];
1146 + col_parity ^= b;
1148 + if (b & 0x01) { /* odd number of bits in the byte */
1149 + line_parity ^= i;
1150 + line_parity_prime ^= ~i;
1154 + ecc[2] = (~col_parity) | 0x03;
1156 + t = 0;
1157 + if (line_parity & 0x80)
1158 + t |= 0x80;
1159 + if (line_parity_prime & 0x80)
1160 + t |= 0x40;
1161 + if (line_parity & 0x40)
1162 + t |= 0x20;
1163 + if (line_parity_prime & 0x40)
1164 + t |= 0x10;
1165 + if (line_parity & 0x20)
1166 + t |= 0x08;
1167 + if (line_parity_prime & 0x20)
1168 + t |= 0x04;
1169 + if (line_parity & 0x10)
1170 + t |= 0x02;
1171 + if (line_parity_prime & 0x10)
1172 + t |= 0x01;
1173 + ecc[1] = ~t;
1175 + t = 0;
1176 + if (line_parity & 0x08)
1177 + t |= 0x80;
1178 + if (line_parity_prime & 0x08)
1179 + t |= 0x40;
1180 + if (line_parity & 0x04)
1181 + t |= 0x20;
1182 + if (line_parity_prime & 0x04)
1183 + t |= 0x10;
1184 + if (line_parity & 0x02)
1185 + t |= 0x08;
1186 + if (line_parity_prime & 0x02)
1187 + t |= 0x04;
1188 + if (line_parity & 0x01)
1189 + t |= 0x02;
1190 + if (line_parity_prime & 0x01)
1191 + t |= 0x01;
1192 + ecc[0] = ~t;
1194 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1195 + /* Swap the bytes into the wrong order */
1196 + t = ecc[0];
1197 + ecc[0] = ecc[1];
1198 + ecc[1] = t;
1199 +#endif
1203 +/* Correct the ECC on a 256 byte block of data */
1205 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1206 + const unsigned char *test_ecc)
1208 + unsigned char d0, d1, d2; /* deltas */
1210 + d0 = read_ecc[0] ^ test_ecc[0];
1211 + d1 = read_ecc[1] ^ test_ecc[1];
1212 + d2 = read_ecc[2] ^ test_ecc[2];
1214 + if ((d0 | d1 | d2) == 0)
1215 + return 0; /* no error */
1217 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1218 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1219 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1220 + /* Single bit (recoverable) error in data */
1222 + unsigned byte;
1223 + unsigned bit;
1225 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1226 + /* swap the bytes to correct for the wrong order */
1227 + unsigned char t;
1229 + t = d0;
1230 + d0 = d1;
1231 + d1 = t;
1232 +#endif
1234 + bit = byte = 0;
1236 + if (d1 & 0x80)
1237 + byte |= 0x80;
1238 + if (d1 & 0x20)
1239 + byte |= 0x40;
1240 + if (d1 & 0x08)
1241 + byte |= 0x20;
1242 + if (d1 & 0x02)
1243 + byte |= 0x10;
1244 + if (d0 & 0x80)
1245 + byte |= 0x08;
1246 + if (d0 & 0x20)
1247 + byte |= 0x04;
1248 + if (d0 & 0x08)
1249 + byte |= 0x02;
1250 + if (d0 & 0x02)
1251 + byte |= 0x01;
1253 + if (d2 & 0x80)
1254 + bit |= 0x04;
1255 + if (d2 & 0x20)
1256 + bit |= 0x02;
1257 + if (d2 & 0x08)
1258 + bit |= 0x01;
1260 + data[byte] ^= (1 << bit);
1262 + return 1; /* Corrected the error */
1265 + if ((yaffs_CountBits(d0) +
1266 + yaffs_CountBits(d1) +
1267 + yaffs_CountBits(d2)) == 1) {
1268 + /* Reccoverable error in ecc */
1270 + read_ecc[0] = test_ecc[0];
1271 + read_ecc[1] = test_ecc[1];
1272 + read_ecc[2] = test_ecc[2];
1274 + return 1; /* Corrected the error */
1277 + /* Unrecoverable error */
1279 + return -1;
1285 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1286 + */
1287 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1288 + yaffs_ECCOther *eccOther)
1290 + unsigned int i;
1292 + unsigned char col_parity = 0;
1293 + unsigned line_parity = 0;
1294 + unsigned line_parity_prime = 0;
1295 + unsigned char b;
1297 + for (i = 0; i < nBytes; i++) {
1298 + b = column_parity_table[*data++];
1299 + col_parity ^= b;
1301 + if (b & 0x01) {
1302 + /* odd number of bits in the byte */
1303 + line_parity ^= i;
1304 + line_parity_prime ^= ~i;
1309 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1310 + eccOther->lineParity = line_parity;
1311 + eccOther->lineParityPrime = line_parity_prime;
1314 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1315 + yaffs_ECCOther *read_ecc,
1316 + const yaffs_ECCOther *test_ecc)
1318 + unsigned char cDelta; /* column parity delta */
1319 + unsigned lDelta; /* line parity delta */
1320 + unsigned lDeltaPrime; /* line parity delta */
1321 + unsigned bit;
1323 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1324 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1325 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1327 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1328 + return 0; /* no error */
1330 + if (lDelta == ~lDeltaPrime &&
1331 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15)) {
1332 + /* Single bit (recoverable) error in data */
1334 + bit = 0;
1336 + if (cDelta & 0x20)
1337 + bit |= 0x04;
1338 + if (cDelta & 0x08)
1339 + bit |= 0x02;
1340 + if (cDelta & 0x02)
1341 + bit |= 0x01;
1343 + if (lDelta >= nBytes)
1344 + return -1;
1346 + data[lDelta] ^= (1 << bit);
1348 + return 1; /* corrected */
1351 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1352 + yaffs_CountBits(cDelta)) == 1) {
1353 + /* Reccoverable error in ecc */
1355 + *read_ecc = *test_ecc;
1356 + return 1; /* corrected */
1359 + /* Unrecoverable error */
1361 + return -1;
1363 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_ecc.h linux-2.6.30/fs/yaffs2/yaffs_ecc.h
1364 --- linux-2.6.30.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1365 +++ linux-2.6.30/fs/yaffs2/yaffs_ecc.h 2009-06-11 09:21:04.000000000 +0200
1366 @@ -0,0 +1,44 @@
1368 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1370 + * Copyright (C) 2002-2007 Aleph One Ltd.
1371 + * for Toby Churchill Ltd and Brightstar Engineering
1373 + * Created by Charles Manning <charles@aleph1.co.uk>
1375 + * This program is free software; you can redistribute it and/or modify
1376 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1377 + * published by the Free Software Foundation.
1379 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1380 + */
1383 + * This code implements the ECC algorithm used in SmartMedia.
1385 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1386 + * The two unused bit are set to 1.
1387 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1388 + * blocks are used on a 512-byte NAND page.
1390 + */
1392 +#ifndef __YAFFS_ECC_H__
1393 +#define __YAFFS_ECC_H__
1395 +typedef struct {
1396 + unsigned char colParity;
1397 + unsigned lineParity;
1398 + unsigned lineParityPrime;
1399 +} yaffs_ECCOther;
1401 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1402 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1403 + const unsigned char *test_ecc);
1405 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1406 + yaffs_ECCOther *ecc);
1407 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1408 + yaffs_ECCOther *read_ecc,
1409 + const yaffs_ECCOther *test_ecc);
1410 +#endif
1411 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_fs.c linux-2.6.30/fs/yaffs2/yaffs_fs.c
1412 --- linux-2.6.30.orig/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1413 +++ linux-2.6.30/fs/yaffs2/yaffs_fs.c 2009-06-11 09:21:04.000000000 +0200
1414 @@ -0,0 +1,2529 @@
1416 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1418 + * Copyright (C) 2002-2009 Aleph One Ltd.
1419 + * for Toby Churchill Ltd and Brightstar Engineering
1421 + * Created by Charles Manning <charles@aleph1.co.uk>
1422 + * Acknowledgements:
1423 + * Luc van OostenRyck for numerous patches.
1424 + * Nick Bane for numerous patches.
1425 + * Nick Bane for 2.5/2.6 integration.
1426 + * Andras Toth for mknod rdev issue.
1427 + * Michael Fischer for finding the problem with inode inconsistency.
1428 + * Some code bodily lifted from JFFS
1430 + * This program is free software; you can redistribute it and/or modify
1431 + * it under the terms of the GNU General Public License version 2 as
1432 + * published by the Free Software Foundation.
1433 + */
1437 + * This is the file system front-end to YAFFS that hooks it up to
1438 + * the VFS.
1440 + * Special notes:
1441 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1442 + * this superblock
1443 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1444 + * superblock
1445 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1446 + */
1448 +const char *yaffs_fs_c_version =
1449 + "$Id: yaffs_fs.c,v 1.79 2009-03-17 01:12:00 wookey Exp $";
1450 +extern const char *yaffs_guts_c_version;
1452 +#include <linux/version.h>
1453 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
1454 +#include <linux/config.h>
1455 +#endif
1456 +#include <linux/kernel.h>
1457 +#include <linux/module.h>
1458 +#include <linux/slab.h>
1459 +#include <linux/init.h>
1460 +#include <linux/fs.h>
1461 +#include <linux/proc_fs.h>
1462 +#include <linux/smp_lock.h>
1463 +#include <linux/pagemap.h>
1464 +#include <linux/mtd/mtd.h>
1465 +#include <linux/interrupt.h>
1466 +#include <linux/string.h>
1467 +#include <linux/ctype.h>
1469 +#include "asm/div64.h"
1471 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1473 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1474 +#include <linux/statfs.h>
1475 +#define UnlockPage(p) unlock_page(p)
1476 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1478 +/* FIXME: use sb->s_id instead ? */
1479 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1481 +#else
1483 +#include <linux/locks.h>
1484 +#define BDEVNAME_SIZE 0
1485 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1487 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
1488 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1489 +#define __user
1490 +#endif
1492 +#endif
1494 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
1495 +#define YPROC_ROOT (&proc_root)
1496 +#else
1497 +#define YPROC_ROOT NULL
1498 +#endif
1500 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1501 +#define WRITE_SIZE_STR "writesize"
1502 +#define WRITE_SIZE(mtd) ((mtd)->writesize)
1503 +#else
1504 +#define WRITE_SIZE_STR "oobblock"
1505 +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
1506 +#endif
1508 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
1509 +#define YAFFS_USE_WRITE_BEGIN_END 1
1510 +#else
1511 +#define YAFFS_USE_WRITE_BEGIN_END 0
1512 +#endif
1514 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
1515 +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
1517 + uint64_t result = partition_size;
1518 + do_div(result, block_size);
1519 + return (uint32_t)result;
1521 +#else
1522 +#define YCALCBLOCKS(s, b) ((s)/(b))
1523 +#endif
1525 +#include <linux/uaccess.h>
1527 +#include "yportenv.h"
1528 +#include "yaffs_guts.h"
1530 +#include <linux/mtd/mtd.h>
1531 +#include "yaffs_mtdif.h"
1532 +#include "yaffs_mtdif1.h"
1533 +#include "yaffs_mtdif2.h"
1535 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1536 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1537 +unsigned int yaffs_auto_checkpoint = 1;
1539 +/* Module Parameters */
1540 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1541 +module_param(yaffs_traceMask, uint, 0644);
1542 +module_param(yaffs_wr_attempts, uint, 0644);
1543 +module_param(yaffs_auto_checkpoint, uint, 0644);
1544 +#else
1545 +MODULE_PARM(yaffs_traceMask, "i");
1546 +MODULE_PARM(yaffs_wr_attempts, "i");
1547 +MODULE_PARM(yaffs_auto_checkpoint, "i");
1548 +#endif
1550 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
1551 +/* use iget and read_inode */
1552 +#define Y_IGET(sb, inum) iget((sb), (inum))
1553 +static void yaffs_read_inode(struct inode *inode);
1555 +#else
1556 +/* Call local equivalent */
1557 +#define YAFFS_USE_OWN_IGET
1558 +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
1560 +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino);
1561 +#endif
1563 +/*#define T(x) printk x */
1565 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
1566 +#define yaffs_InodeToObjectLV(iptr) ((iptr)->i_private)
1567 +#else
1568 +#define yaffs_InodeToObjectLV(iptr) ((iptr)->u.generic_ip)
1569 +#endif
1571 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1572 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1574 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1575 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1576 +#else
1577 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1578 +#endif
1580 +static void yaffs_put_super(struct super_block *sb);
1582 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1583 + loff_t *pos);
1584 +static ssize_t yaffs_hold_space(struct file *f);
1585 +static void yaffs_release_space(struct file *f);
1587 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1588 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1589 +#else
1590 +static int yaffs_file_flush(struct file *file);
1591 +#endif
1593 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1594 + int datasync);
1596 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1598 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1599 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1600 + struct nameidata *n);
1601 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1602 + struct nameidata *n);
1603 +#else
1604 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1605 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1606 +#endif
1607 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1608 + struct dentry *dentry);
1609 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1610 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1611 + const char *symname);
1612 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1614 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1615 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1616 + dev_t dev);
1617 +#else
1618 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1619 + int dev);
1620 +#endif
1621 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1622 + struct inode *new_dir, struct dentry *new_dentry);
1623 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1625 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1626 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1627 +static void yaffs_write_super(struct super_block *sb);
1628 +#else
1629 +static int yaffs_sync_fs(struct super_block *sb);
1630 +static int yaffs_write_super(struct super_block *sb);
1631 +#endif
1633 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1634 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1635 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1636 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1637 +#else
1638 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1639 +#endif
1641 +#ifdef YAFFS_HAS_PUT_INODE
1642 +static void yaffs_put_inode(struct inode *inode);
1643 +#endif
1645 +static void yaffs_delete_inode(struct inode *);
1646 +static void yaffs_clear_inode(struct inode *);
1648 +static int yaffs_readpage(struct file *file, struct page *page);
1649 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1650 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1651 +#else
1652 +static int yaffs_writepage(struct page *page);
1653 +#endif
1656 +#if (YAFFS_USE_WRITE_BEGIN_END != 0)
1657 +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
1658 + loff_t pos, unsigned len, unsigned flags,
1659 + struct page **pagep, void **fsdata);
1660 +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
1661 + loff_t pos, unsigned len, unsigned copied,
1662 + struct page *pg, void *fsdadata);
1663 +#else
1664 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1665 + unsigned offset, unsigned to);
1666 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1667 + unsigned to);
1669 +#endif
1671 +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
1672 + int buflen);
1673 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
1674 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1675 +#else
1676 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1677 +#endif
1679 +static struct address_space_operations yaffs_file_address_operations = {
1680 + .readpage = yaffs_readpage,
1681 + .writepage = yaffs_writepage,
1682 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
1683 + .write_begin = yaffs_write_begin,
1684 + .write_end = yaffs_write_end,
1685 +#else
1686 + .prepare_write = yaffs_prepare_write,
1687 + .commit_write = yaffs_commit_write,
1688 +#endif
1691 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
1692 +static const struct file_operations yaffs_file_operations = {
1693 + .read = do_sync_read,
1694 + .write = do_sync_write,
1695 + .aio_read = generic_file_aio_read,
1696 + .aio_write = generic_file_aio_write,
1697 + .mmap = generic_file_mmap,
1698 + .flush = yaffs_file_flush,
1699 + .fsync = yaffs_sync_object,
1700 + .splice_read = generic_file_splice_read,
1701 + .splice_write = generic_file_splice_write,
1702 + .llseek = generic_file_llseek,
1705 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
1707 +static const struct file_operations yaffs_file_operations = {
1708 + .read = do_sync_read,
1709 + .write = do_sync_write,
1710 + .aio_read = generic_file_aio_read,
1711 + .aio_write = generic_file_aio_write,
1712 + .mmap = generic_file_mmap,
1713 + .flush = yaffs_file_flush,
1714 + .fsync = yaffs_sync_object,
1715 + .sendfile = generic_file_sendfile,
1718 +#else
1720 +static const struct file_operations yaffs_file_operations = {
1721 + .read = generic_file_read,
1722 + .write = generic_file_write,
1723 + .mmap = generic_file_mmap,
1724 + .flush = yaffs_file_flush,
1725 + .fsync = yaffs_sync_object,
1726 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1727 + .sendfile = generic_file_sendfile,
1728 +#endif
1730 +#endif
1732 +static const struct inode_operations yaffs_file_inode_operations = {
1733 + .setattr = yaffs_setattr,
1736 +static const struct inode_operations yaffs_symlink_inode_operations = {
1737 + .readlink = yaffs_readlink,
1738 + .follow_link = yaffs_follow_link,
1739 + .setattr = yaffs_setattr,
1742 +static const struct inode_operations yaffs_dir_inode_operations = {
1743 + .create = yaffs_create,
1744 + .lookup = yaffs_lookup,
1745 + .link = yaffs_link,
1746 + .unlink = yaffs_unlink,
1747 + .symlink = yaffs_symlink,
1748 + .mkdir = yaffs_mkdir,
1749 + .rmdir = yaffs_unlink,
1750 + .mknod = yaffs_mknod,
1751 + .rename = yaffs_rename,
1752 + .setattr = yaffs_setattr,
1755 +static const struct file_operations yaffs_dir_operations = {
1756 + .read = generic_read_dir,
1757 + .readdir = yaffs_readdir,
1758 + .fsync = yaffs_sync_object,
1761 +static const struct super_operations yaffs_super_ops = {
1762 + .statfs = yaffs_statfs,
1764 +#ifndef YAFFS_USE_OWN_IGET
1765 + .read_inode = yaffs_read_inode,
1766 +#endif
1767 +#ifdef YAFFS_HAS_PUT_INODE
1768 + .put_inode = yaffs_put_inode,
1769 +#endif
1770 + .put_super = yaffs_put_super,
1771 + .delete_inode = yaffs_delete_inode,
1772 + .clear_inode = yaffs_clear_inode,
1773 + .sync_fs = yaffs_sync_fs,
1774 + .write_super = yaffs_write_super,
1777 +static void yaffs_GrossLock(yaffs_Device *dev)
1779 + T(YAFFS_TRACE_OS, ("yaffs locking %p\n", current));
1780 + down(&dev->grossLock);
1781 + T(YAFFS_TRACE_OS, ("yaffs locked %p\n", current));
1784 +static void yaffs_GrossUnlock(yaffs_Device *dev)
1786 + T(YAFFS_TRACE_OS, ("yaffs unlocking %p\n", current));
1787 + up(&dev->grossLock);
1790 +static int yaffs_readlink(struct dentry *dentry, char __user *buffer,
1791 + int buflen)
1793 + unsigned char *alias;
1794 + int ret;
1796 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1798 + yaffs_GrossLock(dev);
1800 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1802 + yaffs_GrossUnlock(dev);
1804 + if (!alias)
1805 + return -ENOMEM;
1807 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1808 + kfree(alias);
1809 + return ret;
1812 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
1813 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1814 +#else
1815 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1816 +#endif
1818 + unsigned char *alias;
1819 + int ret;
1820 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1822 + yaffs_GrossLock(dev);
1824 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1826 + yaffs_GrossUnlock(dev);
1828 + if (!alias) {
1829 + ret = -ENOMEM;
1830 + goto out;
1833 + ret = vfs_follow_link(nd, alias);
1834 + kfree(alias);
1835 +out:
1836 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
1837 + return ERR_PTR(ret);
1838 +#else
1839 + return ret;
1840 +#endif
1843 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1844 + yaffs_Object *obj);
1847 + * Lookup is used to find objects in the fs
1848 + */
1849 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
1851 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1852 + struct nameidata *n)
1853 +#else
1854 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1855 +#endif
1857 + yaffs_Object *obj;
1858 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1860 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1862 + yaffs_GrossLock(dev);
1864 + T(YAFFS_TRACE_OS,
1865 + ("yaffs_lookup for %d:%s\n",
1866 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1868 + obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1869 + dentry->d_name.name);
1871 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1873 + /* Can't hold gross lock when calling yaffs_get_inode() */
1874 + yaffs_GrossUnlock(dev);
1876 + if (obj) {
1877 + T(YAFFS_TRACE_OS,
1878 + ("yaffs_lookup found %d\n", obj->objectId));
1880 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1882 + if (inode) {
1883 + T(YAFFS_TRACE_OS,
1884 + ("yaffs_loookup dentry \n"));
1885 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1886 + * d_add even if NULL inode */
1887 +#if 0
1888 + /*dget(dentry); // try to solve directory bug */
1889 + d_add(dentry, inode);
1891 + /* return dentry; */
1892 + return NULL;
1893 +#endif
1896 + } else {
1897 + T(YAFFS_TRACE_OS, ("yaffs_lookup not found\n"));
1901 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1902 + * NULL which creates dentry hash */
1903 + d_add(dentry, inode);
1905 + return NULL;
1909 +#ifdef YAFFS_HAS_PUT_INODE
1911 +/* For now put inode is just for debugging
1912 + * Put inode is called when the inode **structure** is put.
1913 + */
1914 +static void yaffs_put_inode(struct inode *inode)
1916 + T(YAFFS_TRACE_OS,
1917 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1918 + atomic_read(&inode->i_count)));
1921 +#endif
1923 +/* clear is called to tell the fs to release any per-inode data it holds */
1924 +static void yaffs_clear_inode(struct inode *inode)
1926 + yaffs_Object *obj;
1927 + yaffs_Device *dev;
1929 + obj = yaffs_InodeToObject(inode);
1931 + T(YAFFS_TRACE_OS,
1932 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1933 + atomic_read(&inode->i_count),
1934 + obj ? "object exists" : "null object"));
1936 + if (obj) {
1937 + dev = obj->myDev;
1938 + yaffs_GrossLock(dev);
1940 + /* Clear the association between the inode and
1941 + * the yaffs_Object.
1942 + */
1943 + obj->myInode = NULL;
1944 + yaffs_InodeToObjectLV(inode) = NULL;
1946 + /* If the object freeing was deferred, then the real
1947 + * free happens now.
1948 + * This should fix the inode inconsistency problem.
1949 + */
1951 + yaffs_HandleDeferedFree(obj);
1953 + yaffs_GrossUnlock(dev);
1958 +/* delete is called when the link count is zero and the inode
1959 + * is put (ie. nobody wants to know about it anymore, time to
1960 + * delete the file).
1961 + * NB Must call clear_inode()
1962 + */
1963 +static void yaffs_delete_inode(struct inode *inode)
1965 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1966 + yaffs_Device *dev;
1968 + T(YAFFS_TRACE_OS,
1969 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1970 + atomic_read(&inode->i_count),
1971 + obj ? "object exists" : "null object"));
1973 + if (obj) {
1974 + dev = obj->myDev;
1975 + yaffs_GrossLock(dev);
1976 + yaffs_DeleteObject(obj);
1977 + yaffs_GrossUnlock(dev);
1979 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
1980 + truncate_inode_pages(&inode->i_data, 0);
1981 +#endif
1982 + clear_inode(inode);
1985 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
1986 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1987 +#else
1988 +static int yaffs_file_flush(struct file *file)
1989 +#endif
1991 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1993 + yaffs_Device *dev = obj->myDev;
1995 + T(YAFFS_TRACE_OS,
1996 + ("yaffs_file_flush object %d (%s)\n", obj->objectId,
1997 + obj->dirty ? "dirty" : "clean"));
1999 + yaffs_GrossLock(dev);
2001 + yaffs_FlushFile(obj, 1);
2003 + yaffs_GrossUnlock(dev);
2005 + return 0;
2008 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
2010 + /* Lifted from jffs2 */
2012 + yaffs_Object *obj;
2013 + unsigned char *pg_buf;
2014 + int ret;
2016 + yaffs_Device *dev;
2018 + T(YAFFS_TRACE_OS, ("yaffs_readpage at %08x, size %08x\n",
2019 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
2020 + (unsigned)PAGE_CACHE_SIZE));
2022 + obj = yaffs_DentryToObject(f->f_dentry);
2024 + dev = obj->myDev;
2026 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2027 + BUG_ON(!PageLocked(pg));
2028 +#else
2029 + if (!PageLocked(pg))
2030 + PAGE_BUG(pg);
2031 +#endif
2033 + pg_buf = kmap(pg);
2034 + /* FIXME: Can kmap fail? */
2036 + yaffs_GrossLock(dev);
2038 + ret = yaffs_ReadDataFromFile(obj, pg_buf,
2039 + pg->index << PAGE_CACHE_SHIFT,
2040 + PAGE_CACHE_SIZE);
2042 + yaffs_GrossUnlock(dev);
2044 + if (ret >= 0)
2045 + ret = 0;
2047 + if (ret) {
2048 + ClearPageUptodate(pg);
2049 + SetPageError(pg);
2050 + } else {
2051 + SetPageUptodate(pg);
2052 + ClearPageError(pg);
2055 + flush_dcache_page(pg);
2056 + kunmap(pg);
2058 + T(YAFFS_TRACE_OS, ("yaffs_readpage done\n"));
2059 + return ret;
2062 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
2064 + int ret = yaffs_readpage_nolock(f, pg);
2065 + UnlockPage(pg);
2066 + return ret;
2069 +static int yaffs_readpage(struct file *f, struct page *pg)
2071 + return yaffs_readpage_unlock(f, pg);
2074 +/* writepage inspired by/stolen from smbfs */
2076 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2077 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
2078 +#else
2079 +static int yaffs_writepage(struct page *page)
2080 +#endif
2082 + struct address_space *mapping = page->mapping;
2083 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
2084 + struct inode *inode;
2085 + unsigned long end_index;
2086 + char *buffer;
2087 + yaffs_Object *obj;
2088 + int nWritten = 0;
2089 + unsigned nBytes;
2091 + if (!mapping)
2092 + BUG();
2093 + inode = mapping->host;
2094 + if (!inode)
2095 + BUG();
2097 + if (offset > inode->i_size) {
2098 + T(YAFFS_TRACE_OS,
2099 + ("yaffs_writepage at %08x, inode size = %08x!!!\n",
2100 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
2101 + (unsigned)inode->i_size));
2102 + T(YAFFS_TRACE_OS,
2103 + (" -> don't care!!\n"));
2104 + unlock_page(page);
2105 + return 0;
2108 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
2110 + /* easy case */
2111 + if (page->index < end_index)
2112 + nBytes = PAGE_CACHE_SIZE;
2113 + else
2114 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
2116 + get_page(page);
2118 + buffer = kmap(page);
2120 + obj = yaffs_InodeToObject(inode);
2121 + yaffs_GrossLock(obj->myDev);
2123 + T(YAFFS_TRACE_OS,
2124 + ("yaffs_writepage at %08x, size %08x\n",
2125 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2126 + T(YAFFS_TRACE_OS,
2127 + ("writepag0: obj = %05x, ino = %05x\n",
2128 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2130 + nWritten = yaffs_WriteDataToFile(obj, buffer,
2131 + page->index << PAGE_CACHE_SHIFT, nBytes, 0);
2133 + T(YAFFS_TRACE_OS,
2134 + ("writepag1: obj = %05x, ino = %05x\n",
2135 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2137 + yaffs_GrossUnlock(obj->myDev);
2139 + kunmap(page);
2140 + SetPageUptodate(page);
2141 + UnlockPage(page);
2142 + put_page(page);
2144 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2148 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
2149 +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
2150 + loff_t pos, unsigned len, unsigned flags,
2151 + struct page **pagep, void **fsdata)
2153 + struct page *pg = NULL;
2154 + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2155 + uint32_t offset = pos & (PAGE_CACHE_SIZE - 1);
2156 + uint32_t to = offset + len;
2158 + int ret = 0;
2159 + int space_held = 0;
2161 + T(YAFFS_TRACE_OS, ("start yaffs_write_begin\n"));
2162 + /* Get a page */
2163 +#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28)
2164 + pg = grab_cache_page_write_begin(mapping, index, flags);
2165 +#else
2166 + pg = __grab_cache_page(mapping, index);
2167 +#endif
2169 + *pagep = pg;
2170 + if (!pg) {
2171 + ret = -ENOMEM;
2172 + goto out;
2174 + /* Get fs space */
2175 + space_held = yaffs_hold_space(filp);
2177 + if (!space_held) {
2178 + ret = -ENOSPC;
2179 + goto out;
2182 + /* Update page if required */
2184 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2185 + ret = yaffs_readpage_nolock(filp, pg);
2187 + if (ret)
2188 + goto out;
2190 + /* Happy path return */
2191 + T(YAFFS_TRACE_OS, ("end yaffs_write_begin - ok\n"));
2193 + return 0;
2195 +out:
2196 + T(YAFFS_TRACE_OS, ("end yaffs_write_begin fail returning %d\n", ret));
2197 + if (space_held)
2198 + yaffs_release_space(filp);
2199 + if (pg) {
2200 + unlock_page(pg);
2201 + page_cache_release(pg);
2203 + return ret;
2206 +#else
2208 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2209 + unsigned offset, unsigned to)
2211 + T(YAFFS_TRACE_OS, ("yaffs_prepair_write\n"));
2213 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2214 + return yaffs_readpage_nolock(f, pg);
2215 + return 0;
2217 +#endif
2219 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
2220 +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
2221 + loff_t pos, unsigned len, unsigned copied,
2222 + struct page *pg, void *fsdadata)
2224 + int ret = 0;
2225 + void *addr, *kva;
2226 + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
2228 + kva = kmap(pg);
2229 + addr = kva + offset_into_page;
2231 + T(YAFFS_TRACE_OS,
2232 + ("yaffs_write_end addr %x pos %x nBytes %d\n",
2233 + (unsigned) addr,
2234 + (int)pos, copied));
2236 + ret = yaffs_file_write(filp, addr, copied, &pos);
2238 + if (ret != copied) {
2239 + T(YAFFS_TRACE_OS,
2240 + ("yaffs_write_end not same size ret %d copied %d\n",
2241 + ret, copied));
2242 + SetPageError(pg);
2243 + ClearPageUptodate(pg);
2244 + } else {
2245 + SetPageUptodate(pg);
2248 + kunmap(pg);
2250 + yaffs_release_space(filp);
2251 + unlock_page(pg);
2252 + page_cache_release(pg);
2253 + return ret;
2255 +#else
2257 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2258 + unsigned to)
2260 + void *addr, *kva;
2262 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2263 + int nBytes = to - offset;
2264 + int nWritten;
2266 + unsigned spos = pos;
2267 + unsigned saddr;
2269 + kva = kmap(pg);
2270 + addr = kva + offset;
2272 + saddr = (unsigned) addr;
2274 + T(YAFFS_TRACE_OS,
2275 + ("yaffs_commit_write addr %x pos %x nBytes %d\n",
2276 + saddr, spos, nBytes));
2278 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2280 + if (nWritten != nBytes) {
2281 + T(YAFFS_TRACE_OS,
2282 + ("yaffs_commit_write not same size nWritten %d nBytes %d\n",
2283 + nWritten, nBytes));
2284 + SetPageError(pg);
2285 + ClearPageUptodate(pg);
2286 + } else {
2287 + SetPageUptodate(pg);
2290 + kunmap(pg);
2292 + T(YAFFS_TRACE_OS,
2293 + ("yaffs_commit_write returning %d\n",
2294 + nWritten == nBytes ? 0 : nWritten));
2296 + return nWritten == nBytes ? 0 : nWritten;
2298 +#endif
2301 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj)
2303 + if (inode && obj) {
2306 + /* Check mode against the variant type and attempt to repair if broken. */
2307 + __u32 mode = obj->yst_mode;
2308 + switch (obj->variantType) {
2309 + case YAFFS_OBJECT_TYPE_FILE:
2310 + if (!S_ISREG(mode)) {
2311 + obj->yst_mode &= ~S_IFMT;
2312 + obj->yst_mode |= S_IFREG;
2315 + break;
2316 + case YAFFS_OBJECT_TYPE_SYMLINK:
2317 + if (!S_ISLNK(mode)) {
2318 + obj->yst_mode &= ~S_IFMT;
2319 + obj->yst_mode |= S_IFLNK;
2322 + break;
2323 + case YAFFS_OBJECT_TYPE_DIRECTORY:
2324 + if (!S_ISDIR(mode)) {
2325 + obj->yst_mode &= ~S_IFMT;
2326 + obj->yst_mode |= S_IFDIR;
2329 + break;
2330 + case YAFFS_OBJECT_TYPE_UNKNOWN:
2331 + case YAFFS_OBJECT_TYPE_HARDLINK:
2332 + case YAFFS_OBJECT_TYPE_SPECIAL:
2333 + default:
2334 + /* TODO? */
2335 + break;
2338 + inode->i_flags |= S_NOATIME;
2340 + inode->i_ino = obj->objectId;
2341 + inode->i_mode = obj->yst_mode;
2342 + inode->i_uid = obj->yst_uid;
2343 + inode->i_gid = obj->yst_gid;
2344 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
2345 + inode->i_blksize = inode->i_sb->s_blocksize;
2346 +#endif
2347 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2349 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2350 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2351 + inode->i_atime.tv_nsec = 0;
2352 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2353 + inode->i_mtime.tv_nsec = 0;
2354 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2355 + inode->i_ctime.tv_nsec = 0;
2356 +#else
2357 + inode->i_rdev = obj->yst_rdev;
2358 + inode->i_atime = obj->yst_atime;
2359 + inode->i_mtime = obj->yst_mtime;
2360 + inode->i_ctime = obj->yst_ctime;
2361 +#endif
2362 + inode->i_size = yaffs_GetObjectFileLength(obj);
2363 + inode->i_blocks = (inode->i_size + 511) >> 9;
2365 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2367 + T(YAFFS_TRACE_OS,
2368 + ("yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2369 + inode->i_mode, inode->i_uid, inode->i_gid,
2370 + (int)inode->i_size, atomic_read(&inode->i_count)));
2372 + switch (obj->yst_mode & S_IFMT) {
2373 + default: /* fifo, device or socket */
2374 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2375 + init_special_inode(inode, obj->yst_mode,
2376 + old_decode_dev(obj->yst_rdev));
2377 +#else
2378 + init_special_inode(inode, obj->yst_mode,
2379 + (dev_t) (obj->yst_rdev));
2380 +#endif
2381 + break;
2382 + case S_IFREG: /* file */
2383 + inode->i_op = &yaffs_file_inode_operations;
2384 + inode->i_fop = &yaffs_file_operations;
2385 + inode->i_mapping->a_ops =
2386 + &yaffs_file_address_operations;
2387 + break;
2388 + case S_IFDIR: /* directory */
2389 + inode->i_op = &yaffs_dir_inode_operations;
2390 + inode->i_fop = &yaffs_dir_operations;
2391 + break;
2392 + case S_IFLNK: /* symlink */
2393 + inode->i_op = &yaffs_symlink_inode_operations;
2394 + break;
2397 + yaffs_InodeToObjectLV(inode) = obj;
2399 + obj->myInode = inode;
2401 + } else {
2402 + T(YAFFS_TRACE_OS,
2403 + ("yaffs_FileInode invalid parameters\n"));
2408 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2409 + yaffs_Object *obj)
2411 + struct inode *inode;
2413 + if (!sb) {
2414 + T(YAFFS_TRACE_OS,
2415 + ("yaffs_get_inode for NULL super_block!!\n"));
2416 + return NULL;
2420 + if (!obj) {
2421 + T(YAFFS_TRACE_OS,
2422 + ("yaffs_get_inode for NULL object!!\n"));
2423 + return NULL;
2427 + T(YAFFS_TRACE_OS,
2428 + ("yaffs_get_inode for object %d\n", obj->objectId));
2430 + inode = Y_IGET(sb, obj->objectId);
2431 + if (IS_ERR(inode))
2432 + return NULL;
2434 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2435 + /* iget also increments the inode's i_count */
2436 + /* NB You can't be holding grossLock or deadlock will happen! */
2438 + return inode;
2441 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2442 + loff_t *pos)
2444 + yaffs_Object *obj;
2445 + int nWritten, ipos;
2446 + struct inode *inode;
2447 + yaffs_Device *dev;
2449 + obj = yaffs_DentryToObject(f->f_dentry);
2451 + dev = obj->myDev;
2453 + yaffs_GrossLock(dev);
2455 + inode = f->f_dentry->d_inode;
2457 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
2458 + ipos = inode->i_size;
2459 + else
2460 + ipos = *pos;
2462 + if (!obj)
2463 + T(YAFFS_TRACE_OS,
2464 + ("yaffs_file_write: hey obj is null!\n"));
2465 + else
2466 + T(YAFFS_TRACE_OS,
2467 + ("yaffs_file_write about to write writing %zu bytes"
2468 + "to object %d at %d\n",
2469 + n, obj->objectId, ipos));
2471 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2473 + T(YAFFS_TRACE_OS,
2474 + ("yaffs_file_write writing %zu bytes, %d written at %d\n",
2475 + n, nWritten, ipos));
2477 + if (nWritten > 0) {
2478 + ipos += nWritten;
2479 + *pos = ipos;
2480 + if (ipos > inode->i_size) {
2481 + inode->i_size = ipos;
2482 + inode->i_blocks = (ipos + 511) >> 9;
2484 + T(YAFFS_TRACE_OS,
2485 + ("yaffs_file_write size updated to %d bytes, "
2486 + "%d blocks\n",
2487 + ipos, (int)(inode->i_blocks)));
2491 + yaffs_GrossUnlock(dev);
2492 + return nWritten == 0 ? -ENOSPC : nWritten;
2495 +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
2496 +/* For now we just assume few parallel writes and check against a small number. */
2497 +/* Todo: need to do this with a counter to handle parallel reads better */
2499 +static ssize_t yaffs_hold_space(struct file *f)
2501 + yaffs_Object *obj;
2502 + yaffs_Device *dev;
2504 + int nFreeChunks;
2507 + obj = yaffs_DentryToObject(f->f_dentry);
2509 + dev = obj->myDev;
2511 + yaffs_GrossLock(dev);
2513 + nFreeChunks = yaffs_GetNumberOfFreeChunks(dev);
2515 + yaffs_GrossUnlock(dev);
2517 + return (nFreeChunks > 20) ? 1 : 0;
2520 +static void yaffs_release_space(struct file *f)
2522 + yaffs_Object *obj;
2523 + yaffs_Device *dev;
2526 + obj = yaffs_DentryToObject(f->f_dentry);
2528 + dev = obj->myDev;
2530 + yaffs_GrossLock(dev);
2533 + yaffs_GrossUnlock(dev);
2536 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2538 + yaffs_Object *obj;
2539 + yaffs_Device *dev;
2540 + struct inode *inode = f->f_dentry->d_inode;
2541 + unsigned long offset, curoffs;
2542 + struct ylist_head *i;
2543 + yaffs_Object *l;
2545 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2547 + obj = yaffs_DentryToObject(f->f_dentry);
2548 + dev = obj->myDev;
2550 + yaffs_GrossLock(dev);
2552 + offset = f->f_pos;
2554 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2556 + if (offset == 0) {
2557 + T(YAFFS_TRACE_OS,
2558 + ("yaffs_readdir: entry . ino %d \n",
2559 + (int)inode->i_ino));
2560 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0)
2561 + goto out;
2562 + offset++;
2563 + f->f_pos++;
2565 + if (offset == 1) {
2566 + T(YAFFS_TRACE_OS,
2567 + ("yaffs_readdir: entry .. ino %d \n",
2568 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2569 + if (filldir(dirent, "..", 2, offset,
2570 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
2571 + goto out;
2572 + offset++;
2573 + f->f_pos++;
2576 + curoffs = 1;
2578 + /* If the directory has changed since the open or last call to
2579 + readdir, rewind to after the 2 canned entries. */
2581 + if (f->f_version != inode->i_version) {
2582 + offset = 2;
2583 + f->f_pos = offset;
2584 + f->f_version = inode->i_version;
2587 + ylist_for_each(i, &obj->variant.directoryVariant.children) {
2588 + curoffs++;
2589 + if (curoffs >= offset) {
2590 + l = ylist_entry(i, yaffs_Object, siblings);
2592 + yaffs_GetObjectName(l, name,
2593 + YAFFS_MAX_NAME_LENGTH + 1);
2594 + T(YAFFS_TRACE_OS,
2595 + ("yaffs_readdir: %s inode %d\n", name,
2596 + yaffs_GetObjectInode(l)));
2598 + if (filldir(dirent,
2599 + name,
2600 + strlen(name),
2601 + offset,
2602 + yaffs_GetObjectInode(l),
2603 + yaffs_GetObjectType(l)) < 0)
2604 + goto up_and_out;
2606 + offset++;
2607 + f->f_pos++;
2611 +up_and_out:
2612 +out:
2613 + yaffs_GrossUnlock(dev);
2615 + return 0;
2619 + * File creation. Allocate an inode, and we're done..
2620 + */
2622 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
2623 +#define YCRED(x) x
2624 +#else
2625 +#define YCRED(x) (x->cred)
2626 +#endif
2628 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2629 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2630 + dev_t rdev)
2631 +#else
2632 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2633 + int rdev)
2634 +#endif
2636 + struct inode *inode;
2638 + yaffs_Object *obj = NULL;
2639 + yaffs_Device *dev;
2641 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2643 + int error = -ENOSPC;
2644 + uid_t uid = YCRED(current)->fsuid;
2645 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
2647 + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2648 + mode |= S_ISGID;
2650 + if (parent) {
2651 + T(YAFFS_TRACE_OS,
2652 + ("yaffs_mknod: parent object %d type %d\n",
2653 + parent->objectId, parent->variantType));
2654 + } else {
2655 + T(YAFFS_TRACE_OS,
2656 + ("yaffs_mknod: could not get parent object\n"));
2657 + return -EPERM;
2660 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2661 + "mode %x dev %x\n",
2662 + dentry->d_name.name, mode, rdev));
2664 + dev = parent->myDev;
2666 + yaffs_GrossLock(dev);
2668 + switch (mode & S_IFMT) {
2669 + default:
2670 + /* Special (socket, fifo, device...) */
2671 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making special\n"));
2672 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2673 + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2674 + gid, old_encode_dev(rdev));
2675 +#else
2676 + obj = yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2677 + gid, rdev);
2678 +#endif
2679 + break;
2680 + case S_IFREG: /* file */
2681 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making file\n"));
2682 + obj = yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2683 + gid);
2684 + break;
2685 + case S_IFDIR: /* directory */
2686 + T(YAFFS_TRACE_OS,
2687 + ("yaffs_mknod: making directory\n"));
2688 + obj = yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2689 + uid, gid);
2690 + break;
2691 + case S_IFLNK: /* symlink */
2692 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making symlink\n"));
2693 + obj = NULL; /* Do we ever get here? */
2694 + break;
2697 + /* Can not call yaffs_get_inode() with gross lock held */
2698 + yaffs_GrossUnlock(dev);
2700 + if (obj) {
2701 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2702 + d_instantiate(dentry, inode);
2703 + T(YAFFS_TRACE_OS,
2704 + ("yaffs_mknod created object %d count = %d\n",
2705 + obj->objectId, atomic_read(&inode->i_count)));
2706 + error = 0;
2707 + } else {
2708 + T(YAFFS_TRACE_OS,
2709 + ("yaffs_mknod failed making object\n"));
2710 + error = -ENOMEM;
2713 + return error;
2716 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2718 + int retVal;
2719 + T(YAFFS_TRACE_OS, ("yaffs_mkdir\n"));
2720 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2721 + return retVal;
2724 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2725 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2726 + struct nameidata *n)
2727 +#else
2728 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2729 +#endif
2731 + T(YAFFS_TRACE_OS, ("yaffs_create\n"));
2732 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2735 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2737 + int retVal;
2739 + yaffs_Device *dev;
2741 + T(YAFFS_TRACE_OS,
2742 + ("yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2743 + dentry->d_name.name));
2745 + dev = yaffs_InodeToObject(dir)->myDev;
2747 + yaffs_GrossLock(dev);
2749 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2751 + if (retVal == YAFFS_OK) {
2752 + dentry->d_inode->i_nlink--;
2753 + dir->i_version++;
2754 + yaffs_GrossUnlock(dev);
2755 + mark_inode_dirty(dentry->d_inode);
2756 + return 0;
2758 + yaffs_GrossUnlock(dev);
2759 + return -ENOTEMPTY;
2763 + * Create a link...
2764 + */
2765 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2766 + struct dentry *dentry)
2768 + struct inode *inode = old_dentry->d_inode;
2769 + yaffs_Object *obj = NULL;
2770 + yaffs_Object *link = NULL;
2771 + yaffs_Device *dev;
2773 + T(YAFFS_TRACE_OS, ("yaffs_link\n"));
2775 + obj = yaffs_InodeToObject(inode);
2776 + dev = obj->myDev;
2778 + yaffs_GrossLock(dev);
2780 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2781 + link = yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2782 + obj);
2784 + if (link) {
2785 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2786 + d_instantiate(dentry, old_dentry->d_inode);
2787 + atomic_inc(&old_dentry->d_inode->i_count);
2788 + T(YAFFS_TRACE_OS,
2789 + ("yaffs_link link count %d i_count %d\n",
2790 + old_dentry->d_inode->i_nlink,
2791 + atomic_read(&old_dentry->d_inode->i_count)));
2794 + yaffs_GrossUnlock(dev);
2796 + if (link)
2797 + return 0;
2799 + return -EPERM;
2802 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2803 + const char *symname)
2805 + yaffs_Object *obj;
2806 + yaffs_Device *dev;
2807 + uid_t uid = YCRED(current)->fsuid;
2808 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : YCRED(current)->fsgid;
2810 + T(YAFFS_TRACE_OS, ("yaffs_symlink\n"));
2812 + dev = yaffs_InodeToObject(dir)->myDev;
2813 + yaffs_GrossLock(dev);
2814 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2815 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2816 + yaffs_GrossUnlock(dev);
2818 + if (obj) {
2819 + struct inode *inode;
2821 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2822 + d_instantiate(dentry, inode);
2823 + T(YAFFS_TRACE_OS, ("symlink created OK\n"));
2824 + return 0;
2825 + } else {
2826 + T(YAFFS_TRACE_OS, ("symlink not created\n"));
2829 + return -ENOMEM;
2832 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2833 + int datasync)
2836 + yaffs_Object *obj;
2837 + yaffs_Device *dev;
2839 + obj = yaffs_DentryToObject(dentry);
2841 + dev = obj->myDev;
2843 + T(YAFFS_TRACE_OS, ("yaffs_sync_object\n"));
2844 + yaffs_GrossLock(dev);
2845 + yaffs_FlushFile(obj, 1);
2846 + yaffs_GrossUnlock(dev);
2847 + return 0;
2851 + * The VFS layer already does all the dentry stuff for rename.
2853 + * NB: POSIX says you can rename an object over an old object of the same name
2854 + */
2855 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2856 + struct inode *new_dir, struct dentry *new_dentry)
2858 + yaffs_Device *dev;
2859 + int retVal = YAFFS_FAIL;
2860 + yaffs_Object *target;
2862 + T(YAFFS_TRACE_OS, ("yaffs_rename\n"));
2863 + dev = yaffs_InodeToObject(old_dir)->myDev;
2865 + yaffs_GrossLock(dev);
2867 + /* Check if the target is an existing directory that is not empty. */
2868 + target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2869 + new_dentry->d_name.name);
2873 + if (target && target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2874 + !ylist_empty(&target->variant.directoryVariant.children)) {
2876 + T(YAFFS_TRACE_OS, ("target is non-empty dir\n"));
2878 + retVal = YAFFS_FAIL;
2879 + } else {
2880 + /* Now does unlinking internally using shadowing mechanism */
2881 + T(YAFFS_TRACE_OS, ("calling yaffs_RenameObject\n"));
2883 + retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2884 + old_dentry->d_name.name,
2885 + yaffs_InodeToObject(new_dir),
2886 + new_dentry->d_name.name);
2888 + yaffs_GrossUnlock(dev);
2890 + if (retVal == YAFFS_OK) {
2891 + if (target) {
2892 + new_dentry->d_inode->i_nlink--;
2893 + mark_inode_dirty(new_dentry->d_inode);
2896 + return 0;
2897 + } else {
2898 + return -ENOTEMPTY;
2902 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2904 + struct inode *inode = dentry->d_inode;
2905 + int error;
2906 + yaffs_Device *dev;
2908 + T(YAFFS_TRACE_OS,
2909 + ("yaffs_setattr of object %d\n",
2910 + yaffs_InodeToObject(inode)->objectId));
2912 + error = inode_change_ok(inode, attr);
2913 + if (error == 0) {
2914 + dev = yaffs_InodeToObject(inode)->myDev;
2915 + yaffs_GrossLock(dev);
2916 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2917 + YAFFS_OK) {
2918 + error = 0;
2919 + } else {
2920 + error = -EPERM;
2922 + yaffs_GrossUnlock(dev);
2923 + if (!error)
2924 + error = inode_setattr(inode, attr);
2926 + return error;
2929 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
2930 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2932 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2933 + struct super_block *sb = dentry->d_sb;
2934 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
2935 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2937 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2938 +#else
2939 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2941 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2942 +#endif
2944 + T(YAFFS_TRACE_OS, ("yaffs_statfs\n"));
2946 + yaffs_GrossLock(dev);
2948 + buf->f_type = YAFFS_MAGIC;
2949 + buf->f_bsize = sb->s_blocksize;
2950 + buf->f_namelen = 255;
2952 + if (dev->nDataBytesPerChunk & (dev->nDataBytesPerChunk - 1)) {
2953 + /* Do this if chunk size is not a power of 2 */
2955 + uint64_t bytesInDev;
2956 + uint64_t bytesFree;
2958 + bytesInDev = ((uint64_t)((dev->endBlock - dev->startBlock + 1))) *
2959 + ((uint64_t)(dev->nChunksPerBlock * dev->nDataBytesPerChunk));
2961 + do_div(bytesInDev, sb->s_blocksize); /* bytesInDev becomes the number of blocks */
2962 + buf->f_blocks = bytesInDev;
2964 + bytesFree = ((uint64_t)(yaffs_GetNumberOfFreeChunks(dev))) *
2965 + ((uint64_t)(dev->nDataBytesPerChunk));
2967 + do_div(bytesFree, sb->s_blocksize);
2969 + buf->f_bfree = bytesFree;
2971 + } else if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2973 + buf->f_blocks =
2974 + (dev->endBlock - dev->startBlock + 1) *
2975 + dev->nChunksPerBlock /
2976 + (sb->s_blocksize / dev->nDataBytesPerChunk);
2977 + buf->f_bfree =
2978 + yaffs_GetNumberOfFreeChunks(dev) /
2979 + (sb->s_blocksize / dev->nDataBytesPerChunk);
2980 + } else {
2981 + buf->f_blocks =
2982 + (dev->endBlock - dev->startBlock + 1) *
2983 + dev->nChunksPerBlock *
2984 + (dev->nDataBytesPerChunk / sb->s_blocksize);
2986 + buf->f_bfree =
2987 + yaffs_GetNumberOfFreeChunks(dev) *
2988 + (dev->nDataBytesPerChunk / sb->s_blocksize);
2991 + buf->f_files = 0;
2992 + buf->f_ffree = 0;
2993 + buf->f_bavail = buf->f_bfree;
2995 + yaffs_GrossUnlock(dev);
2996 + return 0;
3000 +static int yaffs_do_sync_fs(struct super_block *sb)
3003 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
3004 + T(YAFFS_TRACE_OS, ("yaffs_do_sync_fs\n"));
3006 + if (sb->s_dirt) {
3007 + yaffs_GrossLock(dev);
3009 + if (dev) {
3010 + yaffs_FlushEntireDeviceCache(dev);
3011 + yaffs_CheckpointSave(dev);
3014 + yaffs_GrossUnlock(dev);
3016 + sb->s_dirt = 0;
3018 + return 0;
3022 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3023 +static void yaffs_write_super(struct super_block *sb)
3024 +#else
3025 +static int yaffs_write_super(struct super_block *sb)
3026 +#endif
3029 + T(YAFFS_TRACE_OS, ("yaffs_write_super\n"));
3030 + if (yaffs_auto_checkpoint >= 2)
3031 + yaffs_do_sync_fs(sb);
3032 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
3033 + return 0;
3034 +#endif
3038 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3039 +static int yaffs_sync_fs(struct super_block *sb, int wait)
3040 +#else
3041 +static int yaffs_sync_fs(struct super_block *sb)
3042 +#endif
3044 + T(YAFFS_TRACE_OS, ("yaffs_sync_fs\n"));
3046 + if (yaffs_auto_checkpoint >= 1)
3047 + yaffs_do_sync_fs(sb);
3049 + return 0;
3052 +#ifdef YAFFS_USE_OWN_IGET
3054 +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
3056 + struct inode *inode;
3057 + yaffs_Object *obj;
3058 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
3060 + T(YAFFS_TRACE_OS,
3061 + ("yaffs_iget for %lu\n", ino));
3063 + inode = iget_locked(sb, ino);
3064 + if (!inode)
3065 + return ERR_PTR(-ENOMEM);
3066 + if (!(inode->i_state & I_NEW))
3067 + return inode;
3069 + /* NB This is called as a side effect of other functions, but
3070 + * we had to release the lock to prevent deadlocks, so
3071 + * need to lock again.
3072 + */
3074 + yaffs_GrossLock(dev);
3076 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
3078 + yaffs_FillInodeFromObject(inode, obj);
3080 + yaffs_GrossUnlock(dev);
3082 + unlock_new_inode(inode);
3083 + return inode;
3086 +#else
3088 +static void yaffs_read_inode(struct inode *inode)
3090 + /* NB This is called as a side effect of other functions, but
3091 + * we had to release the lock to prevent deadlocks, so
3092 + * need to lock again.
3093 + */
3095 + yaffs_Object *obj;
3096 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
3098 + T(YAFFS_TRACE_OS,
3099 + ("yaffs_read_inode for %d\n", (int)inode->i_ino));
3101 + yaffs_GrossLock(dev);
3103 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
3105 + yaffs_FillInodeFromObject(inode, obj);
3107 + yaffs_GrossUnlock(dev);
3110 +#endif
3112 +static YLIST_HEAD(yaffs_dev_list);
3114 +#if 0 /* not used */
3115 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
3117 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
3119 + if (*flags & MS_RDONLY) {
3120 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
3122 + T(YAFFS_TRACE_OS,
3123 + ("yaffs_remount_fs: %s: RO\n", dev->name));
3125 + yaffs_GrossLock(dev);
3127 + yaffs_FlushEntireDeviceCache(dev);
3129 + yaffs_CheckpointSave(dev);
3131 + if (mtd->sync)
3132 + mtd->sync(mtd);
3134 + yaffs_GrossUnlock(dev);
3135 + } else {
3136 + T(YAFFS_TRACE_OS,
3137 + ("yaffs_remount_fs: %s: RW\n", dev->name));
3140 + return 0;
3142 +#endif
3144 +static void yaffs_put_super(struct super_block *sb)
3146 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
3148 + T(YAFFS_TRACE_OS, ("yaffs_put_super\n"));
3150 + yaffs_GrossLock(dev);
3152 + yaffs_FlushEntireDeviceCache(dev);
3154 + yaffs_CheckpointSave(dev);
3156 + if (dev->putSuperFunc)
3157 + dev->putSuperFunc(sb);
3159 + yaffs_Deinitialise(dev);
3161 + yaffs_GrossUnlock(dev);
3163 + /* we assume this is protected by lock_kernel() in mount/umount */
3164 + ylist_del(&dev->devList);
3166 + if (dev->spareBuffer) {
3167 + YFREE(dev->spareBuffer);
3168 + dev->spareBuffer = NULL;
3171 + kfree(dev);
3175 +static void yaffs_MTDPutSuper(struct super_block *sb)
3177 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
3179 + if (mtd->sync)
3180 + mtd->sync(mtd);
3182 + put_mtd_device(mtd);
3186 +static void yaffs_MarkSuperBlockDirty(void *vsb)
3188 + struct super_block *sb = (struct super_block *)vsb;
3190 + T(YAFFS_TRACE_OS, ("yaffs_MarkSuperBlockDirty() sb = %p\n", sb));
3191 + if (sb)
3192 + sb->s_dirt = 1;
3195 +typedef struct {
3196 + int inband_tags;
3197 + int skip_checkpoint_read;
3198 + int skip_checkpoint_write;
3199 + int no_cache;
3200 +} yaffs_options;
3202 +#define MAX_OPT_LEN 20
3203 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
3205 + char cur_opt[MAX_OPT_LEN + 1];
3206 + int p;
3207 + int error = 0;
3209 + /* Parse through the options which is a comma seperated list */
3211 + while (options_str && *options_str && !error) {
3212 + memset(cur_opt, 0, MAX_OPT_LEN + 1);
3213 + p = 0;
3215 + while (*options_str && *options_str != ',') {
3216 + if (p < MAX_OPT_LEN) {
3217 + cur_opt[p] = *options_str;
3218 + p++;
3220 + options_str++;
3223 + if (!strcmp(cur_opt, "inband-tags"))
3224 + options->inband_tags = 1;
3225 + else if (!strcmp(cur_opt, "no-cache"))
3226 + options->no_cache = 1;
3227 + else if (!strcmp(cur_opt, "no-checkpoint-read"))
3228 + options->skip_checkpoint_read = 1;
3229 + else if (!strcmp(cur_opt, "no-checkpoint-write"))
3230 + options->skip_checkpoint_write = 1;
3231 + else if (!strcmp(cur_opt, "no-checkpoint")) {
3232 + options->skip_checkpoint_read = 1;
3233 + options->skip_checkpoint_write = 1;
3234 + } else {
3235 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
3236 + cur_opt);
3237 + error = 1;
3241 + return error;
3244 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
3245 + struct super_block *sb,
3246 + void *data, int silent)
3248 + int nBlocks;
3249 + struct inode *inode = NULL;
3250 + struct dentry *root;
3251 + yaffs_Device *dev = 0;
3252 + char devname_buf[BDEVNAME_SIZE + 1];
3253 + struct mtd_info *mtd;
3254 + int err;
3255 + char *data_str = (char *)data;
3257 + yaffs_options options;
3259 + sb->s_magic = YAFFS_MAGIC;
3260 + sb->s_op = &yaffs_super_ops;
3261 + sb->s_flags |= MS_NOATIME;
3263 + if (!sb)
3264 + printk(KERN_INFO "yaffs: sb is NULL\n");
3265 + else if (!sb->s_dev)
3266 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
3267 + else if (!yaffs_devname(sb, devname_buf))
3268 + printk(KERN_INFO "yaffs: devname is NULL\n");
3269 + else
3270 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
3271 + sb->s_dev,
3272 + yaffs_devname(sb, devname_buf));
3274 + if (!data_str)
3275 + data_str = "";
3277 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
3279 + memset(&options, 0, sizeof(options));
3281 + if (yaffs_parse_options(&options, data_str)) {
3282 + /* Option parsing failed */
3283 + return NULL;
3287 + sb->s_blocksize = PAGE_CACHE_SIZE;
3288 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3289 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
3290 + T(YAFFS_TRACE_OS,
3291 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
3293 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
3294 + T(YAFFS_TRACE_OS,
3295 + ("yaffs: Write verification disabled. All guarantees "
3296 + "null and void\n"));
3297 +#endif
3299 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3300 + "\"%s\"\n",
3301 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
3302 + yaffs_devname(sb, devname_buf)));
3304 + /* Check it's an mtd device..... */
3305 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR)
3306 + return NULL; /* This isn't an mtd device */
3308 + /* Get the device */
3309 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3310 + if (!mtd) {
3311 + T(YAFFS_TRACE_ALWAYS,
3312 + ("yaffs: MTD device #%u doesn't appear to exist\n",
3313 + MINOR(sb->s_dev)));
3314 + return NULL;
3316 + /* Check it's NAND */
3317 + if (mtd->type != MTD_NANDFLASH) {
3318 + T(YAFFS_TRACE_ALWAYS,
3319 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3320 + return NULL;
3323 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3324 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3325 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3326 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3327 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3328 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3329 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3330 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3331 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3332 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3333 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
3334 + T(YAFFS_TRACE_OS, (" size %u\n", mtd->size));
3335 +#else
3336 + T(YAFFS_TRACE_OS, (" size %lld\n", mtd->size));
3337 +#endif
3339 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3341 + if (yaffsVersion == 1 && WRITE_SIZE(mtd) >= 2048) {
3342 + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs2\n"));
3343 + yaffsVersion = 2;
3346 + /* Added NCB 26/5/2006 for completeness */
3347 + if (yaffsVersion == 2 && !options.inband_tags && WRITE_SIZE(mtd) == 512) {
3348 + T(YAFFS_TRACE_ALWAYS, ("yaffs: auto selecting yaffs1\n"));
3349 + yaffsVersion = 1;
3352 +#endif
3354 + if (yaffsVersion == 2) {
3355 + /* Check for version 2 style functions */
3356 + if (!mtd->erase ||
3357 + !mtd->block_isbad ||
3358 + !mtd->block_markbad ||
3359 + !mtd->read ||
3360 + !mtd->write ||
3361 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3362 + !mtd->read_oob || !mtd->write_oob) {
3363 +#else
3364 + !mtd->write_ecc ||
3365 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3366 +#endif
3367 + T(YAFFS_TRACE_ALWAYS,
3368 + ("yaffs: MTD device does not support required "
3369 + "functions\n"));;
3370 + return NULL;
3373 + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3374 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
3375 + !options.inband_tags) {
3376 + T(YAFFS_TRACE_ALWAYS,
3377 + ("yaffs: MTD device does not have the "
3378 + "right page sizes\n"));
3379 + return NULL;
3381 + } else {
3382 + /* Check for V1 style functions */
3383 + if (!mtd->erase ||
3384 + !mtd->read ||
3385 + !mtd->write ||
3386 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3387 + !mtd->read_oob || !mtd->write_oob) {
3388 +#else
3389 + !mtd->write_ecc ||
3390 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3391 +#endif
3392 + T(YAFFS_TRACE_ALWAYS,
3393 + ("yaffs: MTD device does not support required "
3394 + "functions\n"));;
3395 + return NULL;
3398 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3399 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3400 + T(YAFFS_TRACE_ALWAYS,
3401 + ("yaffs: MTD device does not support have the "
3402 + "right page sizes\n"));
3403 + return NULL;
3407 + /* OK, so if we got here, we have an MTD that's NAND and looks
3408 + * like it has the right capabilities
3409 + * Set the yaffs_Device up for mtd
3410 + */
3412 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3413 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3414 +#else
3415 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3416 +#endif
3417 + if (!dev) {
3418 + /* Deep shit could not allocate device structure */
3419 + T(YAFFS_TRACE_ALWAYS,
3420 + ("yaffs_read_super: Failed trying to allocate "
3421 + "yaffs_Device. \n"));
3422 + return NULL;
3425 + memset(dev, 0, sizeof(yaffs_Device));
3426 + dev->genericDevice = mtd;
3427 + dev->name = mtd->name;
3429 + /* Set up the memory size parameters.... */
3431 + nBlocks = YCALCBLOCKS(mtd->size, (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK));
3433 + dev->startBlock = 0;
3434 + dev->endBlock = nBlocks - 1;
3435 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3436 + dev->totalBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3437 + dev->nReservedBlocks = 5;
3438 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3439 + dev->inbandTags = options.inband_tags;
3441 + /* ... and the functions. */
3442 + if (yaffsVersion == 2) {
3443 + dev->writeChunkWithTagsToNAND =
3444 + nandmtd2_WriteChunkWithTagsToNAND;
3445 + dev->readChunkWithTagsFromNAND =
3446 + nandmtd2_ReadChunkWithTagsFromNAND;
3447 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3448 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3449 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3450 + dev->isYaffs2 = 1;
3451 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3452 + dev->totalBytesPerChunk = mtd->writesize;
3453 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3454 +#else
3455 + dev->totalBytesPerChunk = mtd->oobblock;
3456 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3457 +#endif
3458 + nBlocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
3460 + dev->startBlock = 0;
3461 + dev->endBlock = nBlocks - 1;
3462 + } else {
3463 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3464 + /* use the MTD interface in yaffs_mtdif1.c */
3465 + dev->writeChunkWithTagsToNAND =
3466 + nandmtd1_WriteChunkWithTagsToNAND;
3467 + dev->readChunkWithTagsFromNAND =
3468 + nandmtd1_ReadChunkWithTagsFromNAND;
3469 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3470 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3471 +#else
3472 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3473 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3474 +#endif
3475 + dev->isYaffs2 = 0;
3477 + /* ... and common functions */
3478 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3479 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3481 + dev->putSuperFunc = yaffs_MTDPutSuper;
3483 + dev->superBlock = (void *)sb;
3484 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3487 +#ifndef CONFIG_YAFFS_DOES_ECC
3488 + dev->useNANDECC = 1;
3489 +#endif
3491 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3492 + dev->wideTnodesDisabled = 1;
3493 +#endif
3495 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3496 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3498 + /* we assume this is protected by lock_kernel() in mount/umount */
3499 + ylist_add_tail(&dev->devList, &yaffs_dev_list);
3501 + init_MUTEX(&dev->grossLock);
3503 + yaffs_GrossLock(dev);
3505 + err = yaffs_GutsInitialise(dev);
3507 + T(YAFFS_TRACE_OS,
3508 + ("yaffs_read_super: guts initialised %s\n",
3509 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3511 + /* Release lock before yaffs_get_inode() */
3512 + yaffs_GrossUnlock(dev);
3514 + /* Create root inode */
3515 + if (err == YAFFS_OK)
3516 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3517 + yaffs_Root(dev));
3519 + if (!inode)
3520 + return NULL;
3522 + inode->i_op = &yaffs_dir_inode_operations;
3523 + inode->i_fop = &yaffs_dir_operations;
3525 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3527 + root = d_alloc_root(inode);
3529 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3531 + if (!root) {
3532 + iput(inode);
3533 + return NULL;
3535 + sb->s_root = root;
3536 + sb->s_dirt = !dev->isCheckpointed;
3537 + T(YAFFS_TRACE_ALWAYS,
3538 + ("yaffs_read_super: isCheckpointed %d\n", dev->isCheckpointed));
3540 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3541 + return sb;
3545 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3546 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3547 + int silent)
3549 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3552 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3553 +static int yaffs_read_super(struct file_system_type *fs,
3554 + int flags, const char *dev_name,
3555 + void *data, struct vfsmount *mnt)
3558 + return get_sb_bdev(fs, flags, dev_name, data,
3559 + yaffs_internal_read_super_mtd, mnt);
3561 +#else
3562 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3563 + int flags, const char *dev_name,
3564 + void *data)
3567 + return get_sb_bdev(fs, flags, dev_name, data,
3568 + yaffs_internal_read_super_mtd);
3570 +#endif
3572 +static struct file_system_type yaffs_fs_type = {
3573 + .owner = THIS_MODULE,
3574 + .name = "yaffs",
3575 + .get_sb = yaffs_read_super,
3576 + .kill_sb = kill_block_super,
3577 + .fs_flags = FS_REQUIRES_DEV,
3579 +#else
3580 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3581 + int silent)
3583 + return yaffs_internal_read_super(1, sb, data, silent);
3586 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3587 + FS_REQUIRES_DEV);
3588 +#endif
3591 +#ifdef CONFIG_YAFFS_YAFFS2
3593 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
3594 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3595 + int silent)
3597 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3600 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
3601 +static int yaffs2_read_super(struct file_system_type *fs,
3602 + int flags, const char *dev_name, void *data,
3603 + struct vfsmount *mnt)
3605 + return get_sb_bdev(fs, flags, dev_name, data,
3606 + yaffs2_internal_read_super_mtd, mnt);
3608 +#else
3609 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3610 + int flags, const char *dev_name,
3611 + void *data)
3614 + return get_sb_bdev(fs, flags, dev_name, data,
3615 + yaffs2_internal_read_super_mtd);
3617 +#endif
3619 +static struct file_system_type yaffs2_fs_type = {
3620 + .owner = THIS_MODULE,
3621 + .name = "yaffs2",
3622 + .get_sb = yaffs2_read_super,
3623 + .kill_sb = kill_block_super,
3624 + .fs_flags = FS_REQUIRES_DEV,
3626 +#else
3627 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3628 + void *data, int silent)
3630 + return yaffs_internal_read_super(2, sb, data, silent);
3633 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3634 + FS_REQUIRES_DEV);
3635 +#endif
3637 +#endif /* CONFIG_YAFFS_YAFFS2 */
3639 +static struct proc_dir_entry *my_proc_entry;
3641 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3643 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3644 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3645 + buf += sprintf(buf, "totalBytesPerChunk. %d\n", dev->totalBytesPerChunk);
3646 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3647 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3648 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3649 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3650 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3651 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3652 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3653 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3654 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3655 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3656 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3657 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3658 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3659 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3660 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3661 + buf += sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3662 + buf += sprintf(buf, "passiveGCs......... %d\n",
3663 + dev->passiveGarbageCollections);
3664 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3665 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3666 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3667 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3668 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3669 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3670 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3671 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3672 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3673 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3674 + buf +=
3675 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3676 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3677 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3678 + buf += sprintf(buf, "inbandTags......... %d\n", dev->inbandTags);
3680 + return buf;
3683 +static int yaffs_proc_read(char *page,
3684 + char **start,
3685 + off_t offset, int count, int *eof, void *data)
3687 + struct ylist_head *item;
3688 + char *buf = page;
3689 + int step = offset;
3690 + int n = 0;
3692 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3693 + * We use 'offset' (*ppos) to indicate where we are in devList.
3694 + * This also assumes the user has posted a read buffer large
3695 + * enough to hold the complete output; but that's life in /proc.
3696 + */
3698 + *(int *)start = 1;
3700 + /* Print header first */
3701 + if (step == 0) {
3702 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3703 + "\n%s\n%s\n", yaffs_fs_c_version,
3704 + yaffs_guts_c_version);
3707 + /* hold lock_kernel while traversing yaffs_dev_list */
3708 + lock_kernel();
3710 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3711 + ylist_for_each(item, &yaffs_dev_list) {
3712 + yaffs_Device *dev = ylist_entry(item, yaffs_Device, devList);
3713 + if (n < step) {
3714 + n++;
3715 + continue;
3717 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3718 + buf = yaffs_dump_dev(buf, dev);
3719 + break;
3721 + unlock_kernel();
3723 + return buf - page < count ? buf - page : count;
3726 +/**
3727 + * Set the verbosity of the warnings and error messages.
3729 + * Note that the names can only be a..z or _ with the current code.
3730 + */
3732 +static struct {
3733 + char *mask_name;
3734 + unsigned mask_bitfield;
3735 +} mask_flags[] = {
3736 + {"allocate", YAFFS_TRACE_ALLOCATE},
3737 + {"always", YAFFS_TRACE_ALWAYS},
3738 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3739 + {"buffers", YAFFS_TRACE_BUFFERS},
3740 + {"bug", YAFFS_TRACE_BUG},
3741 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3742 + {"deletion", YAFFS_TRACE_DELETION},
3743 + {"erase", YAFFS_TRACE_ERASE},
3744 + {"error", YAFFS_TRACE_ERROR},
3745 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3746 + {"gc", YAFFS_TRACE_GC},
3747 + {"mtd", YAFFS_TRACE_MTD},
3748 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3749 + {"os", YAFFS_TRACE_OS},
3750 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3751 + {"scan", YAFFS_TRACE_SCAN},
3752 + {"tracing", YAFFS_TRACE_TRACING},
3754 + {"verify", YAFFS_TRACE_VERIFY},
3755 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3756 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3757 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3759 + {"write", YAFFS_TRACE_WRITE},
3760 + {"all", 0xffffffff},
3761 + {"none", 0},
3762 + {NULL, 0},
3765 +#define MAX_MASK_NAME_LENGTH 40
3766 +static int yaffs_proc_write(struct file *file, const char *buf,
3767 + unsigned long count, void *data)
3769 + unsigned rg = 0, mask_bitfield;
3770 + char *end;
3771 + char *mask_name;
3772 + const char *x;
3773 + char substring[MAX_MASK_NAME_LENGTH + 1];
3774 + int i;
3775 + int done = 0;
3776 + int add, len = 0;
3777 + int pos = 0;
3779 + rg = yaffs_traceMask;
3781 + while (!done && (pos < count)) {
3782 + done = 1;
3783 + while ((pos < count) && isspace(buf[pos]))
3784 + pos++;
3786 + switch (buf[pos]) {
3787 + case '+':
3788 + case '-':
3789 + case '=':
3790 + add = buf[pos];
3791 + pos++;
3792 + break;
3794 + default:
3795 + add = ' ';
3796 + break;
3798 + mask_name = NULL;
3800 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3802 + if (end > buf + pos) {
3803 + mask_name = "numeral";
3804 + len = end - (buf + pos);
3805 + pos += len;
3806 + done = 0;
3807 + } else {
3808 + for (x = buf + pos, i = 0;
3809 + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
3810 + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3811 + substring[i] = *x;
3812 + substring[i] = '\0';
3814 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3815 + if (strcmp(substring, mask_flags[i].mask_name) == 0) {
3816 + mask_name = mask_flags[i].mask_name;
3817 + mask_bitfield = mask_flags[i].mask_bitfield;
3818 + done = 0;
3819 + break;
3824 + if (mask_name != NULL) {
3825 + done = 0;
3826 + switch (add) {
3827 + case '-':
3828 + rg &= ~mask_bitfield;
3829 + break;
3830 + case '+':
3831 + rg |= mask_bitfield;
3832 + break;
3833 + case '=':
3834 + rg = mask_bitfield;
3835 + break;
3836 + default:
3837 + rg |= mask_bitfield;
3838 + break;
3843 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3845 + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_traceMask);
3847 + if (rg & YAFFS_TRACE_ALWAYS) {
3848 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3849 + char flag;
3850 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3851 + printk(KERN_DEBUG "%c%s\n", flag, mask_flags[i].mask_name);
3855 + return count;
3858 +/* Stuff to handle installation of file systems */
3859 +struct file_system_to_install {
3860 + struct file_system_type *fst;
3861 + int installed;
3864 +static struct file_system_to_install fs_to_install[] = {
3865 + {&yaffs_fs_type, 0},
3866 + {&yaffs2_fs_type, 0},
3867 + {NULL, 0}
3870 +static int __init init_yaffs_fs(void)
3872 + int error = 0;
3873 + struct file_system_to_install *fsinst;
3875 + T(YAFFS_TRACE_ALWAYS,
3876 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3878 + /* Install the proc_fs entry */
3879 + my_proc_entry = create_proc_entry("yaffs",
3880 + S_IRUGO | S_IFREG,
3881 + YPROC_ROOT);
3883 + if (my_proc_entry) {
3884 + my_proc_entry->write_proc = yaffs_proc_write;
3885 + my_proc_entry->read_proc = yaffs_proc_read;
3886 + my_proc_entry->data = NULL;
3887 + } else
3888 + return -ENOMEM;
3890 + /* Now add the file system entries */
3892 + fsinst = fs_to_install;
3894 + while (fsinst->fst && !error) {
3895 + error = register_filesystem(fsinst->fst);
3896 + if (!error)
3897 + fsinst->installed = 1;
3898 + fsinst++;
3901 + /* Any errors? uninstall */
3902 + if (error) {
3903 + fsinst = fs_to_install;
3905 + while (fsinst->fst) {
3906 + if (fsinst->installed) {
3907 + unregister_filesystem(fsinst->fst);
3908 + fsinst->installed = 0;
3910 + fsinst++;
3914 + return error;
3917 +static void __exit exit_yaffs_fs(void)
3920 + struct file_system_to_install *fsinst;
3922 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3923 + " removing. \n"));
3925 + remove_proc_entry("yaffs", YPROC_ROOT);
3927 + fsinst = fs_to_install;
3929 + while (fsinst->fst) {
3930 + if (fsinst->installed) {
3931 + unregister_filesystem(fsinst->fst);
3932 + fsinst->installed = 0;
3934 + fsinst++;
3938 +module_init(init_yaffs_fs)
3939 +module_exit(exit_yaffs_fs)
3941 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3942 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3943 +MODULE_LICENSE("GPL");
3944 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_getblockinfo.h linux-2.6.30/fs/yaffs2/yaffs_getblockinfo.h
3945 --- linux-2.6.30.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
3946 +++ linux-2.6.30/fs/yaffs2/yaffs_getblockinfo.h 2009-06-11 09:21:04.000000000 +0200
3947 @@ -0,0 +1,34 @@
3949 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
3951 + * Copyright (C) 2002-2007 Aleph One Ltd.
3952 + * for Toby Churchill Ltd and Brightstar Engineering
3954 + * Created by Charles Manning <charles@aleph1.co.uk>
3956 + * This program is free software; you can redistribute it and/or modify
3957 + * it under the terms of the GNU Lesser General Public License version 2.1 as
3958 + * published by the Free Software Foundation.
3960 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
3961 + */
3963 +#ifndef __YAFFS_GETBLOCKINFO_H__
3964 +#define __YAFFS_GETBLOCKINFO_H__
3966 +#include "yaffs_guts.h"
3968 +/* Function to manipulate block info */
3969 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
3971 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3972 + T(YAFFS_TRACE_ERROR,
3973 + (TSTR
3974 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
3975 + blk));
3976 + YBUG();
3978 + return &dev->blockInfo[blk - dev->internalStartBlock];
3981 +#endif
3982 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_guts.c linux-2.6.30/fs/yaffs2/yaffs_guts.c
3983 --- linux-2.6.30.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3984 +++ linux-2.6.30/fs/yaffs2/yaffs_guts.c 2009-06-11 09:21:04.000000000 +0200
3985 @@ -0,0 +1,7552 @@
3987 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3989 + * Copyright (C) 2002-2007 Aleph One Ltd.
3990 + * for Toby Churchill Ltd and Brightstar Engineering
3992 + * Created by Charles Manning <charles@aleph1.co.uk>
3994 + * This program is free software; you can redistribute it and/or modify
3995 + * it under the terms of the GNU General Public License version 2 as
3996 + * published by the Free Software Foundation.
3997 + */
3999 +const char *yaffs_guts_c_version =
4000 + "$Id: yaffs_guts.c,v 1.82 2009-03-09 04:24:17 charles Exp $";
4002 +#include "yportenv.h"
4004 +#include "yaffsinterface.h"
4005 +#include "yaffs_guts.h"
4006 +#include "yaffs_tagsvalidity.h"
4007 +#include "yaffs_getblockinfo.h"
4009 +#include "yaffs_tagscompat.h"
4010 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
4011 +#include "yaffs_qsort.h"
4012 +#endif
4013 +#include "yaffs_nand.h"
4015 +#include "yaffs_checkptrw.h"
4017 +#include "yaffs_nand.h"
4018 +#include "yaffs_packedtags2.h"
4021 +#define YAFFS_PASSIVE_GC_CHUNKS 2
4023 +#include "yaffs_ecc.h"
4026 +/* Robustification (if it ever comes about...) */
4027 +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND);
4028 +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
4029 + int erasedOk);
4030 +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
4031 + const __u8 *data,
4032 + const yaffs_ExtendedTags *tags);
4033 +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
4034 + const yaffs_ExtendedTags *tags);
4036 +/* Other local prototypes */
4037 +static int yaffs_UnlinkObject(yaffs_Object *obj);
4038 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
4040 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
4042 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev,
4043 + const __u8 *buffer,
4044 + yaffs_ExtendedTags *tags,
4045 + int useReserve);
4046 +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
4047 + int chunkInNAND, int inScan);
4049 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
4050 + yaffs_ObjectType type);
4051 +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
4052 + yaffs_Object *obj);
4053 +static int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name,
4054 + int force, int isShrink, int shadows);
4055 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj);
4056 +static int yaffs_CheckStructures(void);
4057 +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
4058 + int chunkOffset, int *limit);
4059 +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in);
4061 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev, int blockNo);
4064 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4065 + int chunkInNAND);
4067 +static int yaffs_UnlinkWorker(yaffs_Object *obj);
4069 +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
4070 + int chunkInObject);
4072 +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
4073 + yaffs_BlockInfo **blockUsedPtr);
4075 +static void yaffs_VerifyFreeChunks(yaffs_Device *dev);
4077 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
4079 +static void yaffs_VerifyDirectory(yaffs_Object *directory);
4080 +#ifdef YAFFS_PARANOID
4081 +static int yaffs_CheckFileSanity(yaffs_Object *in);
4082 +#else
4083 +#define yaffs_CheckFileSanity(in)
4084 +#endif
4086 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in);
4087 +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId);
4089 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
4091 +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
4092 + yaffs_ExtendedTags *tags);
4094 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
4095 + unsigned pos);
4096 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
4097 + yaffs_FileStructure *fStruct,
4098 + __u32 chunkId);
4101 +/* Function to calculate chunk and offset */
4103 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, int *chunkOut,
4104 + __u32 *offsetOut)
4106 + int chunk;
4107 + __u32 offset;
4109 + chunk = (__u32)(addr >> dev->chunkShift);
4111 + if (dev->chunkDiv == 1) {
4112 + /* easy power of 2 case */
4113 + offset = (__u32)(addr & dev->chunkMask);
4114 + } else {
4115 + /* Non power-of-2 case */
4117 + loff_t chunkBase;
4119 + chunk /= dev->chunkDiv;
4121 + chunkBase = ((loff_t)chunk) * dev->nDataBytesPerChunk;
4122 + offset = (__u32)(addr - chunkBase);
4125 + *chunkOut = chunk;
4126 + *offsetOut = offset;
4129 +/* Function to return the number of shifts for a power of 2 greater than or
4130 + * equal to the given number
4131 + * Note we don't try to cater for all possible numbers and this does not have to
4132 + * be hellishly efficient.
4133 + */
4135 +static __u32 ShiftsGE(__u32 x)
4137 + int extraBits;
4138 + int nShifts;
4140 + nShifts = extraBits = 0;
4142 + while (x > 1) {
4143 + if (x & 1)
4144 + extraBits++;
4145 + x >>= 1;
4146 + nShifts++;
4149 + if (extraBits)
4150 + nShifts++;
4152 + return nShifts;
4155 +/* Function to return the number of shifts to get a 1 in bit 0
4156 + */
4158 +static __u32 Shifts(__u32 x)
4160 + int nShifts;
4162 + nShifts = 0;
4164 + if (!x)
4165 + return 0;
4167 + while (!(x&1)) {
4168 + x >>= 1;
4169 + nShifts++;
4172 + return nShifts;
4178 + * Temporary buffer manipulations.
4179 + */
4181 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
4183 + int i;
4184 + __u8 *buf = (__u8 *)1;
4186 + memset(dev->tempBuffer, 0, sizeof(dev->tempBuffer));
4188 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
4189 + dev->tempBuffer[i].line = 0; /* not in use */
4190 + dev->tempBuffer[i].buffer = buf =
4191 + YMALLOC_DMA(dev->totalBytesPerChunk);
4194 + return buf ? YAFFS_OK : YAFFS_FAIL;
4197 +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo)
4199 + int i, j;
4201 + dev->tempInUse++;
4202 + if (dev->tempInUse > dev->maxTemp)
4203 + dev->maxTemp = dev->tempInUse;
4205 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4206 + if (dev->tempBuffer[i].line == 0) {
4207 + dev->tempBuffer[i].line = lineNo;
4208 + if ((i + 1) > dev->maxTemp) {
4209 + dev->maxTemp = i + 1;
4210 + for (j = 0; j <= i; j++)
4211 + dev->tempBuffer[j].maxLine =
4212 + dev->tempBuffer[j].line;
4215 + return dev->tempBuffer[i].buffer;
4219 + T(YAFFS_TRACE_BUFFERS,
4220 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
4221 + lineNo));
4222 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
4223 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
4225 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
4227 + /*
4228 + * If we got here then we have to allocate an unmanaged one
4229 + * This is not good.
4230 + */
4232 + dev->unmanagedTempAllocations++;
4233 + return YMALLOC(dev->nDataBytesPerChunk);
4237 +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer,
4238 + int lineNo)
4240 + int i;
4242 + dev->tempInUse--;
4244 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4245 + if (dev->tempBuffer[i].buffer == buffer) {
4246 + dev->tempBuffer[i].line = 0;
4247 + return;
4251 + if (buffer) {
4252 + /* assume it is an unmanaged one. */
4253 + T(YAFFS_TRACE_BUFFERS,
4254 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
4255 + lineNo));
4256 + YFREE(buffer);
4257 + dev->unmanagedTempDeallocations++;
4263 + * Determine if we have a managed buffer.
4264 + */
4265 +int yaffs_IsManagedTempBuffer(yaffs_Device *dev, const __u8 *buffer)
4267 + int i;
4269 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
4270 + if (dev->tempBuffer[i].buffer == buffer)
4271 + return 1;
4274 + for (i = 0; i < dev->nShortOpCaches; i++) {
4275 + if (dev->srCache[i].data == buffer)
4276 + return 1;
4279 + if (buffer == dev->checkpointBuffer)
4280 + return 1;
4282 + T(YAFFS_TRACE_ALWAYS,
4283 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
4284 + return 0;
4290 + * Chunk bitmap manipulations
4291 + */
4293 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk)
4295 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
4296 + T(YAFFS_TRACE_ERROR,
4297 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
4298 + blk));
4299 + YBUG();
4301 + return dev->chunkBits +
4302 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
4305 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
4307 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
4308 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
4309 + T(YAFFS_TRACE_ERROR,
4310 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),
4311 + blk, chunk));
4312 + YBUG();
4316 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device *dev, int blk)
4318 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4320 + memset(blkBits, 0, dev->chunkBitmapStride);
4323 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device *dev, int blk, int chunk)
4325 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4327 + yaffs_VerifyChunkBitId(dev, blk, chunk);
4329 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
4332 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device *dev, int blk, int chunk)
4334 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4336 + yaffs_VerifyChunkBitId(dev, blk, chunk);
4338 + blkBits[chunk / 8] |= (1 << (chunk & 7));
4341 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device *dev, int blk, int chunk)
4343 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4344 + yaffs_VerifyChunkBitId(dev, blk, chunk);
4346 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4349 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device *dev, int blk)
4351 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4352 + int i;
4353 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4354 + if (*blkBits)
4355 + return 1;
4356 + blkBits++;
4358 + return 0;
4361 +static int yaffs_CountChunkBits(yaffs_Device *dev, int blk)
4363 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4364 + int i;
4365 + int n = 0;
4366 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4367 + __u8 x = *blkBits;
4368 + while (x) {
4369 + if (x & 1)
4370 + n++;
4371 + x >>= 1;
4374 + blkBits++;
4376 + return n;
4380 + * Verification code
4381 + */
4383 +static int yaffs_SkipVerification(yaffs_Device *dev)
4385 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4388 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4390 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4393 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4395 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4398 +static const char *blockStateName[] = {
4399 +"Unknown",
4400 +"Needs scanning",
4401 +"Scanning",
4402 +"Empty",
4403 +"Allocating",
4404 +"Full",
4405 +"Dirty",
4406 +"Checkpoint",
4407 +"Collecting",
4408 +"Dead"
4411 +static void yaffs_VerifyBlock(yaffs_Device *dev, yaffs_BlockInfo *bi, int n)
4413 + int actuallyUsed;
4414 + int inUse;
4416 + if (yaffs_SkipVerification(dev))
4417 + return;
4419 + /* Report illegal runtime states */
4420 + if (bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4421 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has undefined state %d"TENDSTR), n, bi->blockState));
4423 + switch (bi->blockState) {
4424 + case YAFFS_BLOCK_STATE_UNKNOWN:
4425 + case YAFFS_BLOCK_STATE_SCANNING:
4426 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4427 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has bad run-state %s"TENDSTR),
4428 + n, blockStateName[bi->blockState]));
4431 + /* Check pages in use and soft deletions are legal */
4433 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4435 + if (bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4436 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4437 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4438 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4439 + n, bi->pagesInUse, bi->softDeletions));
4442 + /* Check chunk bitmap legal */
4443 + inUse = yaffs_CountChunkBits(dev, n);
4444 + if (inUse != bi->pagesInUse)
4445 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4446 + n, bi->pagesInUse, inUse));
4448 + /* Check that the sequence number is valid.
4449 + * Ten million is legal, but is very unlikely
4450 + */
4451 + if (dev->isYaffs2 &&
4452 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4453 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000))
4454 + T(YAFFS_TRACE_VERIFY, (TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4455 + n, bi->sequenceNumber));
4458 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev, yaffs_BlockInfo *bi,
4459 + int n)
4461 + yaffs_VerifyBlock(dev, bi, n);
4463 + /* After collection the block should be in the erased state */
4464 + /* This will need to change if we do partial gc */
4466 + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING &&
4467 + bi->blockState != YAFFS_BLOCK_STATE_EMPTY) {
4468 + T(YAFFS_TRACE_ERROR, (TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4469 + n, bi->blockState));
4473 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4475 + int i;
4476 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4477 + int nIllegalBlockStates = 0;
4479 + if (yaffs_SkipVerification(dev))
4480 + return;
4482 + memset(nBlocksPerState, 0, sizeof(nBlocksPerState));
4484 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
4485 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, i);
4486 + yaffs_VerifyBlock(dev, bi, i);
4488 + if (bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4489 + nBlocksPerState[bi->blockState]++;
4490 + else
4491 + nIllegalBlockStates++;
4494 + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
4495 + T(YAFFS_TRACE_VERIFY, (TSTR("Block summary"TENDSTR)));
4497 + T(YAFFS_TRACE_VERIFY, (TSTR("%d blocks have illegal states"TENDSTR), nIllegalBlockStates));
4498 + if (nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4499 + T(YAFFS_TRACE_VERIFY, (TSTR("Too many allocating blocks"TENDSTR)));
4501 + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4502 + T(YAFFS_TRACE_VERIFY,
4503 + (TSTR("%s %d blocks"TENDSTR),
4504 + blockStateName[i], nBlocksPerState[i]));
4506 + if (dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4507 + T(YAFFS_TRACE_VERIFY,
4508 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4509 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4511 + if (dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4512 + T(YAFFS_TRACE_VERIFY,
4513 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4514 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4516 + if (nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4517 + T(YAFFS_TRACE_VERIFY,
4518 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4519 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4521 + T(YAFFS_TRACE_VERIFY, (TSTR(""TENDSTR)));
4526 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4527 + * case those tests will not be performed.
4528 + */
4529 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4531 + if (obj && yaffs_SkipVerification(obj->myDev))
4532 + return;
4534 + if (!(tags && obj && oh)) {
4535 + T(YAFFS_TRACE_VERIFY,
4536 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4537 + (__u32)tags, (__u32)obj, (__u32)oh));
4538 + return;
4541 + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4542 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4543 + T(YAFFS_TRACE_VERIFY,
4544 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4545 + tags->objectId, oh->type));
4547 + if (tags->objectId != obj->objectId)
4548 + T(YAFFS_TRACE_VERIFY,
4549 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4550 + tags->objectId, obj->objectId));
4553 + /*
4554 + * Check that the object's parent ids match if parentCheck requested.
4556 + * Tests do not apply to the root object.
4557 + */
4559 + if (parentCheck && tags->objectId > 1 && !obj->parent)
4560 + T(YAFFS_TRACE_VERIFY,
4561 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4562 + tags->objectId, oh->parentObjectId));
4564 + if (parentCheck && obj->parent &&
4565 + oh->parentObjectId != obj->parent->objectId &&
4566 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4567 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4568 + T(YAFFS_TRACE_VERIFY,
4569 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4570 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4572 + if (tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4573 + T(YAFFS_TRACE_VERIFY,
4574 + (TSTR("Obj %d header name is NULL"TENDSTR),
4575 + obj->objectId));
4577 + if (tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4578 + T(YAFFS_TRACE_VERIFY,
4579 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4580 + obj->objectId));
4585 +static int yaffs_VerifyTnodeWorker(yaffs_Object *obj, yaffs_Tnode *tn,
4586 + __u32 level, int chunkOffset)
4588 + int i;
4589 + yaffs_Device *dev = obj->myDev;
4590 + int ok = 1;
4592 + if (tn) {
4593 + if (level > 0) {
4595 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
4596 + if (tn->internal[i]) {
4597 + ok = yaffs_VerifyTnodeWorker(obj,
4598 + tn->internal[i],
4599 + level - 1,
4600 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4603 + } else if (level == 0) {
4604 + yaffs_ExtendedTags tags;
4605 + __u32 objectId = obj->objectId;
4607 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4609 + for (i = 0; i < YAFFS_NTNODES_LEVEL0; i++) {
4610 + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
4612 + if (theChunk > 0) {
4613 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4614 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
4615 + if (tags.objectId != objectId || tags.chunkId != chunkOffset) {
4616 + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4617 + objectId, chunkOffset, theChunk,
4618 + tags.objectId, tags.chunkId));
4621 + chunkOffset++;
4626 + return ok;
4631 +static void yaffs_VerifyFile(yaffs_Object *obj)
4633 + int requiredTallness;
4634 + int actualTallness;
4635 + __u32 lastChunk;
4636 + __u32 x;
4637 + __u32 i;
4638 + yaffs_Device *dev;
4639 + yaffs_ExtendedTags tags;
4640 + yaffs_Tnode *tn;
4641 + __u32 objectId;
4643 + if (!obj)
4644 + return;
4646 + if (yaffs_SkipVerification(obj->myDev))
4647 + return;
4649 + dev = obj->myDev;
4650 + objectId = obj->objectId;
4652 + /* Check file size is consistent with tnode depth */
4653 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4654 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4655 + requiredTallness = 0;
4656 + while (x > 0) {
4657 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4658 + requiredTallness++;
4661 + actualTallness = obj->variant.fileVariant.topLevel;
4663 + if (requiredTallness > actualTallness)
4664 + T(YAFFS_TRACE_VERIFY,
4665 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4666 + obj->objectId, actualTallness, requiredTallness));
4669 + /* Check that the chunks in the tnode tree are all correct.
4670 + * We do this by scanning through the tnode tree and
4671 + * checking the tags for every chunk match.
4672 + */
4674 + if (yaffs_SkipNANDVerification(dev))
4675 + return;
4677 + for (i = 1; i <= lastChunk; i++) {
4678 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant, i);
4680 + if (tn) {
4681 + __u32 theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
4682 + if (theChunk > 0) {
4683 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4684 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL, &tags);
4685 + if (tags.objectId != objectId || tags.chunkId != i) {
4686 + T(~0, (TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4687 + objectId, i, theChunk,
4688 + tags.objectId, tags.chunkId));
4696 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4698 + if (obj && yaffs_SkipVerification(obj->myDev))
4699 + return;
4701 + /* Verify sane equivalent object */
4704 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4706 + if (obj && yaffs_SkipVerification(obj->myDev))
4707 + return;
4709 + /* Verify symlink string */
4712 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4714 + if (obj && yaffs_SkipVerification(obj->myDev))
4715 + return;
4718 +static void yaffs_VerifyObject(yaffs_Object *obj)
4720 + yaffs_Device *dev;
4722 + __u32 chunkMin;
4723 + __u32 chunkMax;
4725 + __u32 chunkIdOk;
4726 + __u32 chunkInRange;
4727 + __u32 chunkShouldNotBeDeleted;
4728 + __u32 chunkValid;
4730 + if (!obj)
4731 + return;
4733 + if (obj->beingCreated)
4734 + return;
4736 + dev = obj->myDev;
4738 + if (yaffs_SkipVerification(dev))
4739 + return;
4741 + /* Check sane object header chunk */
4743 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4744 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4746 + chunkInRange = (((unsigned)(obj->hdrChunk)) >= chunkMin && ((unsigned)(obj->hdrChunk)) <= chunkMax);
4747 + chunkIdOk = chunkInRange || obj->hdrChunk == 0;
4748 + chunkValid = chunkInRange &&
4749 + yaffs_CheckChunkBit(dev,
4750 + obj->hdrChunk / dev->nChunksPerBlock,
4751 + obj->hdrChunk % dev->nChunksPerBlock);
4752 + chunkShouldNotBeDeleted = chunkInRange && !chunkValid;
4754 + if (!obj->fake &&
4755 + (!chunkIdOk || chunkShouldNotBeDeleted)) {
4756 + T(YAFFS_TRACE_VERIFY,
4757 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4758 + obj->objectId, obj->hdrChunk,
4759 + chunkIdOk ? "" : ",out of range",
4760 + chunkShouldNotBeDeleted ? ",marked as deleted" : ""));
4763 + if (chunkValid && !yaffs_SkipNANDVerification(dev)) {
4764 + yaffs_ExtendedTags tags;
4765 + yaffs_ObjectHeader *oh;
4766 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
4768 + oh = (yaffs_ObjectHeader *)buffer;
4770 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->hdrChunk, buffer,
4771 + &tags);
4773 + yaffs_VerifyObjectHeader(obj, oh, &tags, 1);
4775 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
4778 + /* Verify it has a parent */
4779 + if (obj && !obj->fake &&
4780 + (!obj->parent || obj->parent->myDev != dev)) {
4781 + T(YAFFS_TRACE_VERIFY,
4782 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4783 + obj->objectId, obj->parent));
4786 + /* Verify parent is a directory */
4787 + if (obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
4788 + T(YAFFS_TRACE_VERIFY,
4789 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4790 + obj->objectId, obj->parent->variantType));
4793 + switch (obj->variantType) {
4794 + case YAFFS_OBJECT_TYPE_FILE:
4795 + yaffs_VerifyFile(obj);
4796 + break;
4797 + case YAFFS_OBJECT_TYPE_SYMLINK:
4798 + yaffs_VerifySymlink(obj);
4799 + break;
4800 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4801 + yaffs_VerifyDirectory(obj);
4802 + break;
4803 + case YAFFS_OBJECT_TYPE_HARDLINK:
4804 + yaffs_VerifyHardLink(obj);
4805 + break;
4806 + case YAFFS_OBJECT_TYPE_SPECIAL:
4807 + yaffs_VerifySpecial(obj);
4808 + break;
4809 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4810 + default:
4811 + T(YAFFS_TRACE_VERIFY,
4812 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4813 + obj->objectId, obj->variantType));
4814 + break;
4818 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4820 + yaffs_Object *obj;
4821 + int i;
4822 + struct ylist_head *lh;
4824 + if (yaffs_SkipVerification(dev))
4825 + return;
4827 + /* Iterate through the objects in each hash entry */
4829 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
4830 + ylist_for_each(lh, &dev->objectBucket[i].list) {
4831 + if (lh) {
4832 + obj = ylist_entry(lh, yaffs_Object, hashLink);
4833 + yaffs_VerifyObject(obj);
4841 + * Simple hash function. Needs to have a reasonable spread
4842 + */
4844 +static Y_INLINE int yaffs_HashFunction(int n)
4846 + n = abs(n);
4847 + return n % YAFFS_NOBJECT_BUCKETS;
4851 + * Access functions to useful fake objects.
4852 + * Note that root might have a presence in NAND if permissions are set.
4853 + */
4855 +yaffs_Object *yaffs_Root(yaffs_Device *dev)
4857 + return dev->rootDir;
4860 +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev)
4862 + return dev->lostNFoundDir;
4867 + * Erased NAND checking functions
4868 + */
4870 +int yaffs_CheckFF(__u8 *buffer, int nBytes)
4872 + /* Horrible, slow implementation */
4873 + while (nBytes--) {
4874 + if (*buffer != 0xFF)
4875 + return 0;
4876 + buffer++;
4878 + return 1;
4881 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4882 + int chunkInNAND)
4884 + int retval = YAFFS_OK;
4885 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4886 + yaffs_ExtendedTags tags;
4887 + int result;
4889 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4891 + if (tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4892 + retval = YAFFS_FAIL;
4894 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4895 + T(YAFFS_TRACE_NANDACCESS,
4896 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4897 + retval = YAFFS_FAIL;
4900 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4902 + return retval;
4906 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4907 + const __u8 *data,
4908 + yaffs_ExtendedTags *tags,
4909 + int useReserve)
4911 + int attempts = 0;
4912 + int writeOk = 0;
4913 + int chunk;
4915 + yaffs_InvalidateCheckpoint(dev);
4917 + do {
4918 + yaffs_BlockInfo *bi = 0;
4919 + int erasedOk = 0;
4921 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4922 + if (chunk < 0) {
4923 + /* no space */
4924 + break;
4927 + /* First check this chunk is erased, if it needs
4928 + * checking. The checking policy (unless forced
4929 + * always on) is as follows:
4931 + * Check the first page we try to write in a block.
4932 + * If the check passes then we don't need to check any
4933 + * more. If the check fails, we check again...
4934 + * If the block has been erased, we don't need to check.
4936 + * However, if the block has been prioritised for gc,
4937 + * then we think there might be something odd about
4938 + * this block and stop using it.
4940 + * Rationale: We should only ever see chunks that have
4941 + * not been erased if there was a partially written
4942 + * chunk due to power loss. This checking policy should
4943 + * catch that case with very few checks and thus save a
4944 + * lot of checks that are most likely not needed.
4945 + */
4946 + if (bi->gcPrioritise) {
4947 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4948 + /* try another chunk */
4949 + continue;
4952 + /* let's give it a try */
4953 + attempts++;
4955 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4956 + bi->skipErasedCheck = 0;
4957 +#endif
4958 + if (!bi->skipErasedCheck) {
4959 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4960 + if (erasedOk != YAFFS_OK) {
4961 + T(YAFFS_TRACE_ERROR,
4962 + (TSTR("**>> yaffs chunk %d was not erased"
4963 + TENDSTR), chunk));
4965 + /* try another chunk */
4966 + continue;
4968 + bi->skipErasedCheck = 1;
4971 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4972 + data, tags);
4973 + if (writeOk != YAFFS_OK) {
4974 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4975 + /* try another chunk */
4976 + continue;
4979 + /* Copy the data into the robustification buffer */
4980 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4982 + } while (writeOk != YAFFS_OK &&
4983 + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
4985 + if (!writeOk)
4986 + chunk = -1;
4988 + if (attempts > 1) {
4989 + T(YAFFS_TRACE_ERROR,
4990 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4991 + attempts));
4993 + dev->nRetriedWrites += (attempts - 1);
4996 + return chunk;
5000 + * Block retiring for handling a broken block.
5001 + */
5003 +static void yaffs_RetireBlock(yaffs_Device *dev, int blockInNAND)
5005 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
5007 + yaffs_InvalidateCheckpoint(dev);
5009 + if (yaffs_MarkBlockBad(dev, blockInNAND) != YAFFS_OK) {
5010 + if (yaffs_EraseBlockInNAND(dev, blockInNAND) != YAFFS_OK) {
5011 + T(YAFFS_TRACE_ALWAYS, (TSTR(
5012 + "yaffs: Failed to mark bad and erase block %d"
5013 + TENDSTR), blockInNAND));
5014 + } else {
5015 + yaffs_ExtendedTags tags;
5016 + int chunkId = blockInNAND * dev->nChunksPerBlock;
5018 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
5020 + memset(buffer, 0xff, dev->nDataBytesPerChunk);
5021 + yaffs_InitialiseTags(&tags);
5022 + tags.sequenceNumber = YAFFS_SEQUENCE_BAD_BLOCK;
5023 + if (dev->writeChunkWithTagsToNAND(dev, chunkId -
5024 + dev->chunkOffset, buffer, &tags) != YAFFS_OK)
5025 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Failed to "
5026 + TCONT("write bad block marker to block %d")
5027 + TENDSTR), blockInNAND));
5029 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
5033 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
5034 + bi->gcPrioritise = 0;
5035 + bi->needsRetiring = 0;
5037 + dev->nRetiredBlocks++;
5041 + * Functions for robustisizing TODO
5043 + */
5045 +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
5046 + const __u8 *data,
5047 + const yaffs_ExtendedTags *tags)
5051 +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
5052 + const yaffs_ExtendedTags *tags)
5056 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
5058 + if (!bi->gcPrioritise) {
5059 + bi->gcPrioritise = 1;
5060 + dev->hasPendingPrioritisedGCs = 1;
5061 + bi->chunkErrorStrikes++;
5063 + if (bi->chunkErrorStrikes > 3) {
5064 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
5065 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
5071 +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND,
5072 + int erasedOk)
5074 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
5075 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
5077 + yaffs_HandleChunkError(dev, bi);
5079 + if (erasedOk) {
5080 + /* Was an actual write failure, so mark the block for retirement */
5081 + bi->needsRetiring = 1;
5082 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
5083 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
5086 + /* Delete the chunk */
5087 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
5091 +/*---------------- Name handling functions ------------*/
5093 +static __u16 yaffs_CalcNameSum(const YCHAR *name)
5095 + __u16 sum = 0;
5096 + __u16 i = 1;
5098 + const YUCHAR *bname = (const YUCHAR *) name;
5099 + if (bname) {
5100 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
5102 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
5103 + sum += yaffs_toupper(*bname) * i;
5104 +#else
5105 + sum += (*bname) * i;
5106 +#endif
5107 + i++;
5108 + bname++;
5111 + return sum;
5114 +static void yaffs_SetObjectName(yaffs_Object *obj, const YCHAR *name)
5116 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
5117 + memset(obj->shortName, 0, sizeof(YCHAR) * (YAFFS_SHORT_NAME_LENGTH+1));
5118 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH)
5119 + yaffs_strcpy(obj->shortName, name);
5120 + else
5121 + obj->shortName[0] = _Y('\0');
5122 +#endif
5123 + obj->sum = yaffs_CalcNameSum(name);
5126 +/*-------------------- TNODES -------------------
5128 + * List of spare tnodes
5129 + * The list is hooked together using the first pointer
5130 + * in the tnode.
5131 + */
5133 +/* yaffs_CreateTnodes creates a bunch more tnodes and
5134 + * adds them to the tnode free list.
5135 + * Don't use this function directly
5136 + */
5138 +static int yaffs_CreateTnodes(yaffs_Device *dev, int nTnodes)
5140 + int i;
5141 + int tnodeSize;
5142 + yaffs_Tnode *newTnodes;
5143 + __u8 *mem;
5144 + yaffs_Tnode *curr;
5145 + yaffs_Tnode *next;
5146 + yaffs_TnodeList *tnl;
5148 + if (nTnodes < 1)
5149 + return YAFFS_OK;
5151 + /* Calculate the tnode size in bytes for variable width tnode support.
5152 + * Must be a multiple of 32-bits */
5153 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
5155 + if (tnodeSize < sizeof(yaffs_Tnode))
5156 + tnodeSize = sizeof(yaffs_Tnode);
5158 + /* make these things */
5160 + newTnodes = YMALLOC(nTnodes * tnodeSize);
5161 + mem = (__u8 *)newTnodes;
5163 + if (!newTnodes) {
5164 + T(YAFFS_TRACE_ERROR,
5165 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
5166 + return YAFFS_FAIL;
5169 + /* Hook them into the free list */
5170 +#if 0
5171 + for (i = 0; i < nTnodes - 1; i++) {
5172 + newTnodes[i].internal[0] = &newTnodes[i + 1];
5173 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
5174 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
5175 +#endif
5178 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
5179 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
5180 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
5181 +#endif
5182 + dev->freeTnodes = newTnodes;
5183 +#else
5184 + /* New hookup for wide tnodes */
5185 + for (i = 0; i < nTnodes - 1; i++) {
5186 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
5187 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
5188 + curr->internal[0] = next;
5191 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
5192 + curr->internal[0] = dev->freeTnodes;
5193 + dev->freeTnodes = (yaffs_Tnode *)mem;
5195 +#endif
5198 + dev->nFreeTnodes += nTnodes;
5199 + dev->nTnodesCreated += nTnodes;
5201 + /* Now add this bunch of tnodes to a list for freeing up.
5202 + * NB If we can't add this to the management list it isn't fatal
5203 + * but it just means we can't free this bunch of tnodes later.
5204 + */
5206 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
5207 + if (!tnl) {
5208 + T(YAFFS_TRACE_ERROR,
5209 + (TSTR
5210 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
5211 + return YAFFS_FAIL;
5212 + } else {
5213 + tnl->tnodes = newTnodes;
5214 + tnl->next = dev->allocatedTnodeList;
5215 + dev->allocatedTnodeList = tnl;
5218 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
5220 + return YAFFS_OK;
5223 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
5225 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device *dev)
5227 + yaffs_Tnode *tn = NULL;
5229 + /* If there are none left make more */
5230 + if (!dev->freeTnodes)
5231 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
5233 + if (dev->freeTnodes) {
5234 + tn = dev->freeTnodes;
5235 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
5236 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
5237 + /* Hoosterman, this thing looks like it isn't in the list */
5238 + T(YAFFS_TRACE_ALWAYS,
5239 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
5241 +#endif
5242 + dev->freeTnodes = dev->freeTnodes->internal[0];
5243 + dev->nFreeTnodes--;
5246 + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
5248 + return tn;
5251 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev)
5253 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
5254 + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
5256 + if (tnodeSize < sizeof(yaffs_Tnode))
5257 + tnodeSize = sizeof(yaffs_Tnode);
5259 + if (tn)
5260 + memset(tn, 0, tnodeSize);
5262 + return tn;
5265 +/* FreeTnode frees up a tnode and puts it back on the free list */
5266 +static void yaffs_FreeTnode(yaffs_Device *dev, yaffs_Tnode *tn)
5268 + if (tn) {
5269 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
5270 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
5271 + /* Hoosterman, this thing looks like it is already in the list */
5272 + T(YAFFS_TRACE_ALWAYS,
5273 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
5275 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
5276 +#endif
5277 + tn->internal[0] = dev->freeTnodes;
5278 + dev->freeTnodes = tn;
5279 + dev->nFreeTnodes++;
5281 + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
5284 +static void yaffs_DeinitialiseTnodes(yaffs_Device *dev)
5286 + /* Free the list of allocated tnodes */
5287 + yaffs_TnodeList *tmp;
5289 + while (dev->allocatedTnodeList) {
5290 + tmp = dev->allocatedTnodeList->next;
5292 + YFREE(dev->allocatedTnodeList->tnodes);
5293 + YFREE(dev->allocatedTnodeList);
5294 + dev->allocatedTnodeList = tmp;
5298 + dev->freeTnodes = NULL;
5299 + dev->nFreeTnodes = 0;
5302 +static void yaffs_InitialiseTnodes(yaffs_Device *dev)
5304 + dev->allocatedTnodeList = NULL;
5305 + dev->freeTnodes = NULL;
5306 + dev->nFreeTnodes = 0;
5307 + dev->nTnodesCreated = 0;
5311 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos,
5312 + unsigned val)
5314 + __u32 *map = (__u32 *)tn;
5315 + __u32 bitInMap;
5316 + __u32 bitInWord;
5317 + __u32 wordInMap;
5318 + __u32 mask;
5320 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5321 + val >>= dev->chunkGroupBits;
5323 + bitInMap = pos * dev->tnodeWidth;
5324 + wordInMap = bitInMap / 32;
5325 + bitInWord = bitInMap & (32 - 1);
5327 + mask = dev->tnodeMask << bitInWord;
5329 + map[wordInMap] &= ~mask;
5330 + map[wordInMap] |= (mask & (val << bitInWord));
5332 + if (dev->tnodeWidth > (32 - bitInWord)) {
5333 + bitInWord = (32 - bitInWord);
5334 + wordInMap++;;
5335 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
5336 + map[wordInMap] &= ~mask;
5337 + map[wordInMap] |= (mask & (val >> bitInWord));
5341 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn,
5342 + unsigned pos)
5344 + __u32 *map = (__u32 *)tn;
5345 + __u32 bitInMap;
5346 + __u32 bitInWord;
5347 + __u32 wordInMap;
5348 + __u32 val;
5350 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5352 + bitInMap = pos * dev->tnodeWidth;
5353 + wordInMap = bitInMap / 32;
5354 + bitInWord = bitInMap & (32 - 1);
5356 + val = map[wordInMap] >> bitInWord;
5358 + if (dev->tnodeWidth > (32 - bitInWord)) {
5359 + bitInWord = (32 - bitInWord);
5360 + wordInMap++;;
5361 + val |= (map[wordInMap] << bitInWord);
5364 + val &= dev->tnodeMask;
5365 + val <<= dev->chunkGroupBits;
5367 + return val;
5370 +/* ------------------- End of individual tnode manipulation -----------------*/
5372 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5373 + * The look up tree is represented by the top tnode and the number of topLevel
5374 + * in the tree. 0 means only the level 0 tnode is in the tree.
5375 + */
5377 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5378 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,
5379 + yaffs_FileStructure *fStruct,
5380 + __u32 chunkId)
5382 + yaffs_Tnode *tn = fStruct->top;
5383 + __u32 i;
5384 + int requiredTallness;
5385 + int level = fStruct->topLevel;
5387 + /* Check sane level and chunk Id */
5388 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
5389 + return NULL;
5391 + if (chunkId > YAFFS_MAX_CHUNK_ID)
5392 + return NULL;
5394 + /* First check we're tall enough (ie enough topLevel) */
5396 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5397 + requiredTallness = 0;
5398 + while (i) {
5399 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5400 + requiredTallness++;
5403 + if (requiredTallness > fStruct->topLevel)
5404 + return NULL; /* Not tall enough, so we can't find it */
5406 + /* Traverse down to level 0 */
5407 + while (level > 0 && tn) {
5408 + tn = tn->internal[(chunkId >>
5409 + (YAFFS_TNODES_LEVEL0_BITS +
5410 + (level - 1) *
5411 + YAFFS_TNODES_INTERNAL_BITS)) &
5412 + YAFFS_TNODES_INTERNAL_MASK];
5413 + level--;
5416 + return tn;
5419 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5420 + * This happens in two steps:
5421 + * 1. If the tree isn't tall enough, then make it taller.
5422 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5424 + * Used when modifying the tree.
5426 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5427 + * be plugged into the ttree.
5428 + */
5430 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev,
5431 + yaffs_FileStructure *fStruct,
5432 + __u32 chunkId,
5433 + yaffs_Tnode *passedTn)
5435 + int requiredTallness;
5436 + int i;
5437 + int l;
5438 + yaffs_Tnode *tn;
5440 + __u32 x;
5443 + /* Check sane level and page Id */
5444 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL)
5445 + return NULL;
5447 + if (chunkId > YAFFS_MAX_CHUNK_ID)
5448 + return NULL;
5450 + /* First check we're tall enough (ie enough topLevel) */
5452 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5453 + requiredTallness = 0;
5454 + while (x) {
5455 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5456 + requiredTallness++;
5460 + if (requiredTallness > fStruct->topLevel) {
5461 + /* Not tall enough, gotta make the tree taller */
5462 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5464 + tn = yaffs_GetTnode(dev);
5466 + if (tn) {
5467 + tn->internal[0] = fStruct->top;
5468 + fStruct->top = tn;
5469 + } else {
5470 + T(YAFFS_TRACE_ERROR,
5471 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5475 + fStruct->topLevel = requiredTallness;
5478 + /* Traverse down to level 0, adding anything we need */
5480 + l = fStruct->topLevel;
5481 + tn = fStruct->top;
5483 + if (l > 0) {
5484 + while (l > 0 && tn) {
5485 + x = (chunkId >>
5486 + (YAFFS_TNODES_LEVEL0_BITS +
5487 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5488 + YAFFS_TNODES_INTERNAL_MASK;
5491 + if ((l > 1) && !tn->internal[x]) {
5492 + /* Add missing non-level-zero tnode */
5493 + tn->internal[x] = yaffs_GetTnode(dev);
5495 + } else if (l == 1) {
5496 + /* Looking from level 1 at level 0 */
5497 + if (passedTn) {
5498 + /* If we already have one, then release it.*/
5499 + if (tn->internal[x])
5500 + yaffs_FreeTnode(dev, tn->internal[x]);
5501 + tn->internal[x] = passedTn;
5503 + } else if (!tn->internal[x]) {
5504 + /* Don't have one, none passed in */
5505 + tn->internal[x] = yaffs_GetTnode(dev);
5509 + tn = tn->internal[x];
5510 + l--;
5512 + } else {
5513 + /* top is level 0 */
5514 + if (passedTn) {
5515 + memcpy(tn, passedTn, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5516 + yaffs_FreeTnode(dev, passedTn);
5520 + return tn;
5523 +static int yaffs_FindChunkInGroup(yaffs_Device *dev, int theChunk,
5524 + yaffs_ExtendedTags *tags, int objectId,
5525 + int chunkInInode)
5527 + int j;
5529 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5530 + if (yaffs_CheckChunkBit(dev, theChunk / dev->nChunksPerBlock,
5531 + theChunk % dev->nChunksPerBlock)) {
5532 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5533 + tags);
5534 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5535 + /* found it; */
5536 + return theChunk;
5539 + theChunk++;
5541 + return -1;
5545 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5546 + * chunks and tnodes in the file
5547 + * Returns 1 if the tree was deleted.
5548 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5549 + */
5551 +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level,
5552 + int chunkOffset, int *limit)
5554 + int i;
5555 + int chunkInInode;
5556 + int theChunk;
5557 + yaffs_ExtendedTags tags;
5558 + int foundChunk;
5559 + yaffs_Device *dev = in->myDev;
5561 + int allDone = 1;
5563 + if (tn) {
5564 + if (level > 0) {
5565 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5566 + i--) {
5567 + if (tn->internal[i]) {
5568 + if (limit && (*limit) < 0) {
5569 + allDone = 0;
5570 + } else {
5571 + allDone =
5572 + yaffs_DeleteWorker(in,
5573 + tn->
5574 + internal
5575 + [i],
5576 + level -
5577 + 1,
5578 + (chunkOffset
5579 + <<
5580 + YAFFS_TNODES_INTERNAL_BITS)
5581 + + i,
5582 + limit);
5584 + if (allDone) {
5585 + yaffs_FreeTnode(dev,
5586 + tn->
5587 + internal[i]);
5588 + tn->internal[i] = NULL;
5592 + return (allDone) ? 1 : 0;
5593 + } else if (level == 0) {
5594 + int hitLimit = 0;
5596 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5597 + i--) {
5598 + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
5599 + if (theChunk) {
5601 + chunkInInode = (chunkOffset <<
5602 + YAFFS_TNODES_LEVEL0_BITS) + i;
5604 + foundChunk =
5605 + yaffs_FindChunkInGroup(dev,
5606 + theChunk,
5607 + &tags,
5608 + in->objectId,
5609 + chunkInInode);
5611 + if (foundChunk > 0) {
5612 + yaffs_DeleteChunk(dev,
5613 + foundChunk, 1,
5614 + __LINE__);
5615 + in->nDataChunks--;
5616 + if (limit) {
5617 + *limit = *limit - 1;
5618 + if (*limit <= 0)
5619 + hitLimit = 1;
5624 + yaffs_PutLevel0Tnode(dev, tn, i, 0);
5628 + return (i < 0) ? 1 : 0;
5634 + return 1;
5638 +static void yaffs_SoftDeleteChunk(yaffs_Device *dev, int chunk)
5640 + yaffs_BlockInfo *theBlock;
5642 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5644 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5645 + if (theBlock) {
5646 + theBlock->softDeletions++;
5647 + dev->nFreeChunks++;
5651 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5652 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5653 + * of the tnode.
5654 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5655 + */
5657 +static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn,
5658 + __u32 level, int chunkOffset)
5660 + int i;
5661 + int theChunk;
5662 + int allDone = 1;
5663 + yaffs_Device *dev = in->myDev;
5665 + if (tn) {
5666 + if (level > 0) {
5668 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5669 + i--) {
5670 + if (tn->internal[i]) {
5671 + allDone =
5672 + yaffs_SoftDeleteWorker(in,
5673 + tn->
5674 + internal[i],
5675 + level - 1,
5676 + (chunkOffset
5677 + <<
5678 + YAFFS_TNODES_INTERNAL_BITS)
5679 + + i);
5680 + if (allDone) {
5681 + yaffs_FreeTnode(dev,
5682 + tn->
5683 + internal[i]);
5684 + tn->internal[i] = NULL;
5685 + } else {
5686 + /* Hoosterman... how could this happen? */
5690 + return (allDone) ? 1 : 0;
5691 + } else if (level == 0) {
5693 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5694 + theChunk = yaffs_GetChunkGroupBase(dev, tn, i);
5695 + if (theChunk) {
5696 + /* Note this does not find the real chunk, only the chunk group.
5697 + * We make an assumption that a chunk group is not larger than
5698 + * a block.
5699 + */
5700 + yaffs_SoftDeleteChunk(dev, theChunk);
5701 + yaffs_PutLevel0Tnode(dev, tn, i, 0);
5705 + return 1;
5711 + return 1;
5715 +static void yaffs_SoftDeleteFile(yaffs_Object *obj)
5717 + if (obj->deleted &&
5718 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5719 + if (obj->nDataChunks <= 0) {
5720 + /* Empty file with no duplicate object headers, just delete it immediately */
5721 + yaffs_FreeTnode(obj->myDev,
5722 + obj->variant.fileVariant.top);
5723 + obj->variant.fileVariant.top = NULL;
5724 + T(YAFFS_TRACE_TRACING,
5725 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5726 + obj->objectId));
5727 + yaffs_DoGenericObjectDeletion(obj);
5728 + } else {
5729 + yaffs_SoftDeleteWorker(obj,
5730 + obj->variant.fileVariant.top,
5731 + obj->variant.fileVariant.
5732 + topLevel, 0);
5733 + obj->softDeleted = 1;
5738 +/* Pruning removes any part of the file structure tree that is beyond the
5739 + * bounds of the file (ie that does not point to chunks).
5741 + * A file should only get pruned when its size is reduced.
5743 + * Before pruning, the chunks must be pulled from the tree and the
5744 + * level 0 tnode entries must be zeroed out.
5745 + * Could also use this for file deletion, but that's probably better handled
5746 + * by a special case.
5747 + */
5749 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn,
5750 + __u32 level, int del0)
5752 + int i;
5753 + int hasData;
5755 + if (tn) {
5756 + hasData = 0;
5758 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5759 + if (tn->internal[i] && level > 0) {
5760 + tn->internal[i] =
5761 + yaffs_PruneWorker(dev, tn->internal[i],
5762 + level - 1,
5763 + (i == 0) ? del0 : 1);
5766 + if (tn->internal[i])
5767 + hasData++;
5770 + if (hasData == 0 && del0) {
5771 + /* Free and return NULL */
5773 + yaffs_FreeTnode(dev, tn);
5774 + tn = NULL;
5779 + return tn;
5783 +static int yaffs_PruneFileStructure(yaffs_Device *dev,
5784 + yaffs_FileStructure *fStruct)
5786 + int i;
5787 + int hasData;
5788 + int done = 0;
5789 + yaffs_Tnode *tn;
5791 + if (fStruct->topLevel > 0) {
5792 + fStruct->top =
5793 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5795 + /* Now we have a tree with all the non-zero branches NULL but the height
5796 + * is the same as it was.
5797 + * Let's see if we can trim internal tnodes to shorten the tree.
5798 + * We can do this if only the 0th element in the tnode is in use
5799 + * (ie all the non-zero are NULL)
5800 + */
5802 + while (fStruct->topLevel && !done) {
5803 + tn = fStruct->top;
5805 + hasData = 0;
5806 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5807 + if (tn->internal[i])
5808 + hasData++;
5811 + if (!hasData) {
5812 + fStruct->top = tn->internal[0];
5813 + fStruct->topLevel--;
5814 + yaffs_FreeTnode(dev, tn);
5815 + } else {
5816 + done = 1;
5821 + return YAFFS_OK;
5824 +/*-------------------- End of File Structure functions.-------------------*/
5826 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5827 + * adds them to the object free list.
5828 + */
5829 +static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects)
5831 + int i;
5832 + yaffs_Object *newObjects;
5833 + yaffs_ObjectList *list;
5835 + if (nObjects < 1)
5836 + return YAFFS_OK;
5838 + /* make these things */
5839 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5840 + list = YMALLOC(sizeof(yaffs_ObjectList));
5842 + if (!newObjects || !list) {
5843 + if (newObjects)
5844 + YFREE(newObjects);
5845 + if (list)
5846 + YFREE(list);
5847 + T(YAFFS_TRACE_ALLOCATE,
5848 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5849 + return YAFFS_FAIL;
5852 + /* Hook them into the free list */
5853 + for (i = 0; i < nObjects - 1; i++) {
5854 + newObjects[i].siblings.next =
5855 + (struct ylist_head *)(&newObjects[i + 1]);
5858 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5859 + dev->freeObjects = newObjects;
5860 + dev->nFreeObjects += nObjects;
5861 + dev->nObjectsCreated += nObjects;
5863 + /* Now add this bunch of Objects to a list for freeing up. */
5865 + list->objects = newObjects;
5866 + list->next = dev->allocatedObjectList;
5867 + dev->allocatedObjectList = list;
5869 + return YAFFS_OK;
5873 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5874 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev)
5876 + yaffs_Object *tn = NULL;
5878 +#ifdef VALGRIND_TEST
5879 + tn = YMALLOC(sizeof(yaffs_Object));
5880 +#else
5881 + /* If there are none left make more */
5882 + if (!dev->freeObjects)
5883 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5885 + if (dev->freeObjects) {
5886 + tn = dev->freeObjects;
5887 + dev->freeObjects =
5888 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5889 + dev->nFreeObjects--;
5891 +#endif
5892 + if (tn) {
5893 + /* Now sweeten it up... */
5895 + memset(tn, 0, sizeof(yaffs_Object));
5896 + tn->beingCreated = 1;
5898 + tn->myDev = dev;
5899 + tn->hdrChunk = 0;
5900 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5901 + YINIT_LIST_HEAD(&(tn->hardLinks));
5902 + YINIT_LIST_HEAD(&(tn->hashLink));
5903 + YINIT_LIST_HEAD(&tn->siblings);
5906 + /* Now make the directory sane */
5907 + if (dev->rootDir) {
5908 + tn->parent = dev->rootDir;
5909 + ylist_add(&(tn->siblings), &dev->rootDir->variant.directoryVariant.children);
5912 + /* Add it to the lost and found directory.
5913 + * NB Can't put root or lostNFound in lostNFound so
5914 + * check if lostNFound exists first
5915 + */
5916 + if (dev->lostNFoundDir)
5917 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5919 + tn->beingCreated = 0;
5922 + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
5924 + return tn;
5927 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev, int number,
5928 + __u32 mode)
5931 + yaffs_Object *obj =
5932 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5933 + if (obj) {
5934 + obj->fake = 1; /* it is fake so it might have no NAND presence... */
5935 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5936 + obj->unlinkAllowed = 0; /* ... or unlink it */
5937 + obj->deleted = 0;
5938 + obj->unlinked = 0;
5939 + obj->yst_mode = mode;
5940 + obj->myDev = dev;
5941 + obj->hdrChunk = 0; /* Not a valid chunk. */
5944 + return obj;
5948 +static void yaffs_UnhashObject(yaffs_Object *tn)
5950 + int bucket;
5951 + yaffs_Device *dev = tn->myDev;
5953 + /* If it is still linked into the bucket list, free from the list */
5954 + if (!ylist_empty(&tn->hashLink)) {
5955 + ylist_del_init(&tn->hashLink);
5956 + bucket = yaffs_HashFunction(tn->objectId);
5957 + dev->objectBucket[bucket].count--;
5961 +/* FreeObject frees up a Object and puts it back on the free list */
5962 +static void yaffs_FreeObject(yaffs_Object *tn)
5964 + yaffs_Device *dev = tn->myDev;
5966 +#ifdef __KERNEL__
5967 + T(YAFFS_TRACE_OS, (TSTR("FreeObject %p inode %p"TENDSTR), tn, tn->myInode));
5968 +#endif
5970 + if (tn->parent)
5971 + YBUG();
5972 + if (!ylist_empty(&tn->siblings))
5973 + YBUG();
5976 +#ifdef __KERNEL__
5977 + if (tn->myInode) {
5978 + /* We're still hooked up to a cached inode.
5979 + * Don't delete now, but mark for later deletion
5980 + */
5981 + tn->deferedFree = 1;
5982 + return;
5984 +#endif
5986 + yaffs_UnhashObject(tn);
5988 +#ifdef VALGRIND_TEST
5989 + YFREE(tn);
5990 +#else
5991 + /* Link into the free list. */
5992 + tn->siblings.next = (struct ylist_head *)(dev->freeObjects);
5993 + dev->freeObjects = tn;
5994 + dev->nFreeObjects++;
5995 +#endif
5996 + dev->nCheckpointBlocksRequired = 0; /* force recalculation*/
5999 +#ifdef __KERNEL__
6001 +void yaffs_HandleDeferedFree(yaffs_Object *obj)
6003 + if (obj->deferedFree)
6004 + yaffs_FreeObject(obj);
6007 +#endif
6009 +static void yaffs_DeinitialiseObjects(yaffs_Device *dev)
6011 + /* Free the list of allocated Objects */
6013 + yaffs_ObjectList *tmp;
6015 + while (dev->allocatedObjectList) {
6016 + tmp = dev->allocatedObjectList->next;
6017 + YFREE(dev->allocatedObjectList->objects);
6018 + YFREE(dev->allocatedObjectList);
6020 + dev->allocatedObjectList = tmp;
6023 + dev->freeObjects = NULL;
6024 + dev->nFreeObjects = 0;
6027 +static void yaffs_InitialiseObjects(yaffs_Device *dev)
6029 + int i;
6031 + dev->allocatedObjectList = NULL;
6032 + dev->freeObjects = NULL;
6033 + dev->nFreeObjects = 0;
6035 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
6036 + YINIT_LIST_HEAD(&dev->objectBucket[i].list);
6037 + dev->objectBucket[i].count = 0;
6041 +static int yaffs_FindNiceObjectBucket(yaffs_Device *dev)
6043 + static int x;
6044 + int i;
6045 + int l = 999;
6046 + int lowest = 999999;
6048 + /* First let's see if we can find one that's empty. */
6050 + for (i = 0; i < 10 && lowest > 0; i++) {
6051 + x++;
6052 + x %= YAFFS_NOBJECT_BUCKETS;
6053 + if (dev->objectBucket[x].count < lowest) {
6054 + lowest = dev->objectBucket[x].count;
6055 + l = x;
6060 + /* If we didn't find an empty list, then try
6061 + * looking a bit further for a short one
6062 + */
6064 + for (i = 0; i < 10 && lowest > 3; i++) {
6065 + x++;
6066 + x %= YAFFS_NOBJECT_BUCKETS;
6067 + if (dev->objectBucket[x].count < lowest) {
6068 + lowest = dev->objectBucket[x].count;
6069 + l = x;
6074 + return l;
6077 +static int yaffs_CreateNewObjectNumber(yaffs_Device *dev)
6079 + int bucket = yaffs_FindNiceObjectBucket(dev);
6081 + /* Now find an object value that has not already been taken
6082 + * by scanning the list.
6083 + */
6085 + int found = 0;
6086 + struct ylist_head *i;
6088 + __u32 n = (__u32) bucket;
6090 + /* yaffs_CheckObjectHashSanity(); */
6092 + while (!found) {
6093 + found = 1;
6094 + n += YAFFS_NOBJECT_BUCKETS;
6095 + if (1 || dev->objectBucket[bucket].count > 0) {
6096 + ylist_for_each(i, &dev->objectBucket[bucket].list) {
6097 + /* If there is already one in the list */
6098 + if (i && ylist_entry(i, yaffs_Object,
6099 + hashLink)->objectId == n) {
6100 + found = 0;
6106 + return n;
6109 +static void yaffs_HashObject(yaffs_Object *in)
6111 + int bucket = yaffs_HashFunction(in->objectId);
6112 + yaffs_Device *dev = in->myDev;
6114 + ylist_add(&in->hashLink, &dev->objectBucket[bucket].list);
6115 + dev->objectBucket[bucket].count++;
6118 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number)
6120 + int bucket = yaffs_HashFunction(number);
6121 + struct ylist_head *i;
6122 + yaffs_Object *in;
6124 + ylist_for_each(i, &dev->objectBucket[bucket].list) {
6125 + /* Look if it is in the list */
6126 + if (i) {
6127 + in = ylist_entry(i, yaffs_Object, hashLink);
6128 + if (in->objectId == number) {
6129 +#ifdef __KERNEL__
6130 + /* Don't tell the VFS about this one if it is defered free */
6131 + if (in->deferedFree)
6132 + return NULL;
6133 +#endif
6135 + return in;
6140 + return NULL;
6143 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev, int number,
6144 + yaffs_ObjectType type)
6146 + yaffs_Object *theObject;
6147 + yaffs_Tnode *tn = NULL;
6149 + if (number < 0)
6150 + number = yaffs_CreateNewObjectNumber(dev);
6152 + theObject = yaffs_AllocateEmptyObject(dev);
6153 + if (!theObject)
6154 + return NULL;
6156 + if (type == YAFFS_OBJECT_TYPE_FILE) {
6157 + tn = yaffs_GetTnode(dev);
6158 + if (!tn) {
6159 + yaffs_FreeObject(theObject);
6160 + return NULL;
6164 + if (theObject) {
6165 + theObject->fake = 0;
6166 + theObject->renameAllowed = 1;
6167 + theObject->unlinkAllowed = 1;
6168 + theObject->objectId = number;
6169 + yaffs_HashObject(theObject);
6170 + theObject->variantType = type;
6171 +#ifdef CONFIG_YAFFS_WINCE
6172 + yfsd_WinFileTimeNow(theObject->win_atime);
6173 + theObject->win_ctime[0] = theObject->win_mtime[0] =
6174 + theObject->win_atime[0];
6175 + theObject->win_ctime[1] = theObject->win_mtime[1] =
6176 + theObject->win_atime[1];
6178 +#else
6180 + theObject->yst_atime = theObject->yst_mtime =
6181 + theObject->yst_ctime = Y_CURRENT_TIME;
6182 +#endif
6183 + switch (type) {
6184 + case YAFFS_OBJECT_TYPE_FILE:
6185 + theObject->variant.fileVariant.fileSize = 0;
6186 + theObject->variant.fileVariant.scannedFileSize = 0;
6187 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
6188 + theObject->variant.fileVariant.topLevel = 0;
6189 + theObject->variant.fileVariant.top = tn;
6190 + break;
6191 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6192 + YINIT_LIST_HEAD(&theObject->variant.directoryVariant.
6193 + children);
6194 + break;
6195 + case YAFFS_OBJECT_TYPE_SYMLINK:
6196 + case YAFFS_OBJECT_TYPE_HARDLINK:
6197 + case YAFFS_OBJECT_TYPE_SPECIAL:
6198 + /* No action required */
6199 + break;
6200 + case YAFFS_OBJECT_TYPE_UNKNOWN:
6201 + /* todo this should not happen */
6202 + break;
6206 + return theObject;
6209 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev,
6210 + int number,
6211 + yaffs_ObjectType type)
6213 + yaffs_Object *theObject = NULL;
6215 + if (number > 0)
6216 + theObject = yaffs_FindObjectByNumber(dev, number);
6218 + if (!theObject)
6219 + theObject = yaffs_CreateNewObject(dev, number, type);
6221 + return theObject;
6226 +static YCHAR *yaffs_CloneString(const YCHAR *str)
6228 + YCHAR *newStr = NULL;
6230 + if (str && *str) {
6231 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
6232 + if (newStr)
6233 + yaffs_strcpy(newStr, str);
6236 + return newStr;
6241 + * Mknod (create) a new object.
6242 + * equivalentObject only has meaning for a hard link;
6243 + * aliasString only has meaning for a sumlink.
6244 + * rdev only has meaning for devices (a subset of special objects)
6245 + */
6247 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
6248 + yaffs_Object *parent,
6249 + const YCHAR *name,
6250 + __u32 mode,
6251 + __u32 uid,
6252 + __u32 gid,
6253 + yaffs_Object *equivalentObject,
6254 + const YCHAR *aliasString, __u32 rdev)
6256 + yaffs_Object *in;
6257 + YCHAR *str = NULL;
6259 + yaffs_Device *dev = parent->myDev;
6261 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
6262 + if (yaffs_FindObjectByName(parent, name))
6263 + return NULL;
6265 + in = yaffs_CreateNewObject(dev, -1, type);
6267 + if (!in)
6268 + return YAFFS_FAIL;
6270 + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
6271 + str = yaffs_CloneString(aliasString);
6272 + if (!str) {
6273 + yaffs_FreeObject(in);
6274 + return NULL;
6280 + if (in) {
6281 + in->hdrChunk = 0;
6282 + in->valid = 1;
6283 + in->variantType = type;
6285 + in->yst_mode = mode;
6287 +#ifdef CONFIG_YAFFS_WINCE
6288 + yfsd_WinFileTimeNow(in->win_atime);
6289 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
6290 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
6292 +#else
6293 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
6295 + in->yst_rdev = rdev;
6296 + in->yst_uid = uid;
6297 + in->yst_gid = gid;
6298 +#endif
6299 + in->nDataChunks = 0;
6301 + yaffs_SetObjectName(in, name);
6302 + in->dirty = 1;
6304 + yaffs_AddObjectToDirectory(parent, in);
6306 + in->myDev = parent->myDev;
6308 + switch (type) {
6309 + case YAFFS_OBJECT_TYPE_SYMLINK:
6310 + in->variant.symLinkVariant.alias = str;
6311 + break;
6312 + case YAFFS_OBJECT_TYPE_HARDLINK:
6313 + in->variant.hardLinkVariant.equivalentObject =
6314 + equivalentObject;
6315 + in->variant.hardLinkVariant.equivalentObjectId =
6316 + equivalentObject->objectId;
6317 + ylist_add(&in->hardLinks, &equivalentObject->hardLinks);
6318 + break;
6319 + case YAFFS_OBJECT_TYPE_FILE:
6320 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6321 + case YAFFS_OBJECT_TYPE_SPECIAL:
6322 + case YAFFS_OBJECT_TYPE_UNKNOWN:
6323 + /* do nothing */
6324 + break;
6327 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
6328 + /* Could not create the object header, fail the creation */
6329 + yaffs_DeleteObject(in);
6330 + in = NULL;
6335 + return in;
6338 +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
6339 + __u32 mode, __u32 uid, __u32 gid)
6341 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
6342 + uid, gid, NULL, NULL, 0);
6345 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
6346 + __u32 mode, __u32 uid, __u32 gid)
6348 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
6349 + mode, uid, gid, NULL, NULL, 0);
6352 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
6353 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
6355 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
6356 + uid, gid, NULL, NULL, rdev);
6359 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
6360 + __u32 mode, __u32 uid, __u32 gid,
6361 + const YCHAR *alias)
6363 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
6364 + uid, gid, NULL, alias, 0);
6367 +/* yaffs_Link returns the object id of the equivalent object.*/
6368 +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
6369 + yaffs_Object *equivalentObject)
6371 + /* Get the real object in case we were fed a hard link as an equivalent object */
6372 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6374 + if (yaffs_MknodObject
6375 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6376 + equivalentObject, NULL, 0)) {
6377 + return equivalentObject;
6378 + } else {
6379 + return NULL;
6384 +static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir,
6385 + const YCHAR *newName, int force, int shadows)
6387 + int unlinkOp;
6388 + int deleteOp;
6390 + yaffs_Object *existingTarget;
6392 + if (newDir == NULL)
6393 + newDir = obj->parent; /* use the old directory */
6395 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6396 + T(YAFFS_TRACE_ALWAYS,
6397 + (TSTR
6398 + ("tragedy: yaffs_ChangeObjectName: newDir is not a directory"
6399 + TENDSTR)));
6400 + YBUG();
6403 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6404 + if (obj->myDev->isYaffs2)
6405 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6406 + else
6407 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6408 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6410 + deleteOp = (newDir == obj->myDev->deletedDir);
6412 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6414 + /* If the object is a file going into the unlinked directory,
6415 + * then it is OK to just stuff it in since duplicate names are allowed.
6416 + * else only proceed if the new name does not exist and if we're putting
6417 + * it into a directory.
6418 + */
6419 + if ((unlinkOp ||
6420 + deleteOp ||
6421 + force ||
6422 + (shadows > 0) ||
6423 + !existingTarget) &&
6424 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6425 + yaffs_SetObjectName(obj, newName);
6426 + obj->dirty = 1;
6428 + yaffs_AddObjectToDirectory(newDir, obj);
6430 + if (unlinkOp)
6431 + obj->unlinked = 1;
6433 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6434 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows) >= 0)
6435 + return YAFFS_OK;
6438 + return YAFFS_FAIL;
6441 +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
6442 + yaffs_Object *newDir, const YCHAR *newName)
6444 + yaffs_Object *obj = NULL;
6445 + yaffs_Object *existingTarget = NULL;
6446 + int force = 0;
6449 + if (!oldDir || oldDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
6450 + YBUG();
6451 + if (!newDir || newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
6452 + YBUG();
6454 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6455 + /* Special case for case insemsitive systems (eg. WinCE).
6456 + * While look-up is case insensitive, the name isn't.
6457 + * Therefore we might want to change x.txt to X.txt
6458 + */
6459 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0)
6460 + force = 1;
6461 +#endif
6463 + else if (yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6464 + /* ENAMETOOLONG */
6465 + return YAFFS_FAIL;
6467 + obj = yaffs_FindObjectByName(oldDir, oldName);
6469 + if (obj && obj->renameAllowed) {
6471 + /* Now do the handling for an existing target, if there is one */
6473 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6474 + if (existingTarget &&
6475 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6476 + !ylist_empty(&existingTarget->variant.directoryVariant.children)) {
6477 + /* There is a target that is a non-empty directory, so we fail */
6478 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6479 + } else if (existingTarget && existingTarget != obj) {
6480 + /* Nuke the target first, using shadowing,
6481 + * but only if it isn't the same object
6482 + */
6483 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6484 + existingTarget->objectId);
6485 + yaffs_UnlinkObject(existingTarget);
6488 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6490 + return YAFFS_FAIL;
6493 +/*------------------------- Block Management and Page Allocation ----------------*/
6495 +static int yaffs_InitialiseBlocks(yaffs_Device *dev)
6497 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6499 + dev->blockInfo = NULL;
6500 + dev->chunkBits = NULL;
6502 + dev->allocationBlock = -1; /* force it to get a new one */
6504 + /* If the first allocation strategy fails, thry the alternate one */
6505 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6506 + if (!dev->blockInfo) {
6507 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6508 + dev->blockInfoAlt = 1;
6509 + } else
6510 + dev->blockInfoAlt = 0;
6512 + if (dev->blockInfo) {
6513 + /* Set up dynamic blockinfo stuff. */
6514 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6515 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6516 + if (!dev->chunkBits) {
6517 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6518 + dev->chunkBitsAlt = 1;
6519 + } else
6520 + dev->chunkBitsAlt = 0;
6523 + if (dev->blockInfo && dev->chunkBits) {
6524 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6525 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6526 + return YAFFS_OK;
6529 + return YAFFS_FAIL;
6532 +static void yaffs_DeinitialiseBlocks(yaffs_Device *dev)
6534 + if (dev->blockInfoAlt && dev->blockInfo)
6535 + YFREE_ALT(dev->blockInfo);
6536 + else if (dev->blockInfo)
6537 + YFREE(dev->blockInfo);
6539 + dev->blockInfoAlt = 0;
6541 + dev->blockInfo = NULL;
6543 + if (dev->chunkBitsAlt && dev->chunkBits)
6544 + YFREE_ALT(dev->chunkBits);
6545 + else if (dev->chunkBits)
6546 + YFREE(dev->chunkBits);
6547 + dev->chunkBitsAlt = 0;
6548 + dev->chunkBits = NULL;
6551 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device *dev,
6552 + yaffs_BlockInfo *bi)
6554 + int i;
6555 + __u32 seq;
6556 + yaffs_BlockInfo *b;
6558 + if (!dev->isYaffs2)
6559 + return 1; /* disqualification only applies to yaffs2. */
6561 + if (!bi->hasShrinkHeader)
6562 + return 1; /* can gc */
6564 + /* Find the oldest dirty sequence number if we don't know it and save it
6565 + * so we don't have to keep recomputing it.
6566 + */
6567 + if (!dev->oldestDirtySequence) {
6568 + seq = dev->sequenceNumber;
6570 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6571 + i++) {
6572 + b = yaffs_GetBlockInfo(dev, i);
6573 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6574 + (b->pagesInUse - b->softDeletions) <
6575 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6576 + seq = b->sequenceNumber;
6579 + dev->oldestDirtySequence = seq;
6582 + /* Can't do gc of this block if there are any blocks older than this one that have
6583 + * discarded pages.
6584 + */
6585 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6588 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6589 + * for garbage collection.
6590 + */
6592 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device *dev,
6593 + int aggressive)
6595 + int b = dev->currentDirtyChecker;
6597 + int i;
6598 + int iterations;
6599 + int dirtiest = -1;
6600 + int pagesInUse = 0;
6601 + int prioritised = 0;
6602 + yaffs_BlockInfo *bi;
6603 + int pendingPrioritisedExist = 0;
6605 + /* First let's see if we need to grab a prioritised block */
6606 + if (dev->hasPendingPrioritisedGCs) {
6607 + for (i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++) {
6609 + bi = yaffs_GetBlockInfo(dev, i);
6610 + /* yaffs_VerifyBlock(dev,bi,i); */
6612 + if (bi->gcPrioritise) {
6613 + pendingPrioritisedExist = 1;
6614 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6615 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6616 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6617 + dirtiest = i;
6618 + prioritised = 1;
6619 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6624 + if (!pendingPrioritisedExist) /* None found, so we can clear this */
6625 + dev->hasPendingPrioritisedGCs = 0;
6628 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6629 + * search harder.
6630 + * else (we're doing a leasurely gc), then we only bother to do this if the
6631 + * block has only a few pages in use.
6632 + */
6634 + dev->nonAggressiveSkip--;
6636 + if (!aggressive && (dev->nonAggressiveSkip > 0))
6637 + return -1;
6639 + if (!prioritised)
6640 + pagesInUse =
6641 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6643 + if (aggressive)
6644 + iterations =
6645 + dev->internalEndBlock - dev->internalStartBlock + 1;
6646 + else {
6647 + iterations =
6648 + dev->internalEndBlock - dev->internalStartBlock + 1;
6649 + iterations = iterations / 16;
6650 + if (iterations > 200)
6651 + iterations = 200;
6654 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6655 + b++;
6656 + if (b < dev->internalStartBlock || b > dev->internalEndBlock)
6657 + b = dev->internalStartBlock;
6659 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6660 + T(YAFFS_TRACE_ERROR,
6661 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6662 + YBUG();
6665 + bi = yaffs_GetBlockInfo(dev, b);
6667 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6668 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6669 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6670 + dirtiest = b;
6671 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6675 + dev->currentDirtyChecker = b;
6677 + if (dirtiest > 0) {
6678 + T(YAFFS_TRACE_GC,
6679 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6680 + dev->nChunksPerBlock - pagesInUse, prioritised));
6683 + dev->oldestDirtySequence = 0;
6685 + if (dirtiest > 0)
6686 + dev->nonAggressiveSkip = 4;
6688 + return dirtiest;
6691 +static void yaffs_BlockBecameDirty(yaffs_Device *dev, int blockNo)
6693 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6695 + int erasedOk = 0;
6697 + /* If the block is still healthy erase it and mark as clean.
6698 + * If the block has had a data failure, then retire it.
6699 + */
6701 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6702 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6703 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6705 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6707 + if (!bi->needsRetiring) {
6708 + yaffs_InvalidateCheckpoint(dev);
6709 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6710 + if (!erasedOk) {
6711 + dev->nErasureFailures++;
6712 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6713 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6717 + if (erasedOk &&
6718 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6719 + int i;
6720 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6721 + if (!yaffs_CheckChunkErased
6722 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6723 + T(YAFFS_TRACE_ERROR,
6724 + (TSTR
6725 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6726 + TENDSTR), blockNo, i));
6731 + if (erasedOk) {
6732 + /* Clean it up... */
6733 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6734 + dev->nErasedBlocks++;
6735 + bi->pagesInUse = 0;
6736 + bi->softDeletions = 0;
6737 + bi->hasShrinkHeader = 0;
6738 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6739 + bi->gcPrioritise = 0;
6740 + yaffs_ClearChunkBits(dev, blockNo);
6742 + T(YAFFS_TRACE_ERASE,
6743 + (TSTR("Erased block %d" TENDSTR), blockNo));
6744 + } else {
6745 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6747 + yaffs_RetireBlock(dev, blockNo);
6748 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6749 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6753 +static int yaffs_FindBlockForAllocation(yaffs_Device *dev)
6755 + int i;
6757 + yaffs_BlockInfo *bi;
6759 + if (dev->nErasedBlocks < 1) {
6760 + /* Hoosterman we've got a problem.
6761 + * Can't get space to gc
6762 + */
6763 + T(YAFFS_TRACE_ERROR,
6764 + (TSTR("yaffs tragedy: no more erased blocks" TENDSTR)));
6766 + return -1;
6769 + /* Find an empty block. */
6771 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6772 + dev->allocationBlockFinder++;
6773 + if (dev->allocationBlockFinder < dev->internalStartBlock
6774 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6775 + dev->allocationBlockFinder = dev->internalStartBlock;
6778 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6780 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6781 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6782 + dev->sequenceNumber++;
6783 + bi->sequenceNumber = dev->sequenceNumber;
6784 + dev->nErasedBlocks--;
6785 + T(YAFFS_TRACE_ALLOCATE,
6786 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6787 + dev->allocationBlockFinder, dev->sequenceNumber,
6788 + dev->nErasedBlocks));
6789 + return dev->allocationBlockFinder;
6793 + T(YAFFS_TRACE_ALWAYS,
6794 + (TSTR
6795 + ("yaffs tragedy: no more erased blocks, but there should have been %d"
6796 + TENDSTR), dev->nErasedBlocks));
6798 + return -1;
6803 +static int yaffs_CalcCheckpointBlocksRequired(yaffs_Device *dev)
6805 + if (!dev->nCheckpointBlocksRequired &&
6806 + dev->isYaffs2) {
6807 + /* Not a valid value so recalculate */
6808 + int nBytes = 0;
6809 + int nBlocks;
6810 + int devBlocks = (dev->endBlock - dev->startBlock + 1);
6811 + int tnodeSize;
6813 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
6815 + if (tnodeSize < sizeof(yaffs_Tnode))
6816 + tnodeSize = sizeof(yaffs_Tnode);
6818 + nBytes += sizeof(yaffs_CheckpointValidity);
6819 + nBytes += sizeof(yaffs_CheckpointDevice);
6820 + nBytes += devBlocks * sizeof(yaffs_BlockInfo);
6821 + nBytes += devBlocks * dev->chunkBitmapStride;
6822 + nBytes += (sizeof(yaffs_CheckpointObject) + sizeof(__u32)) * (dev->nObjectsCreated - dev->nFreeObjects);
6823 + nBytes += (tnodeSize + sizeof(__u32)) * (dev->nTnodesCreated - dev->nFreeTnodes);
6824 + nBytes += sizeof(yaffs_CheckpointValidity);
6825 + nBytes += sizeof(__u32); /* checksum*/
6827 + /* Round up and add 2 blocks to allow for some bad blocks, so add 3 */
6829 + nBlocks = (nBytes/(dev->nDataBytesPerChunk * dev->nChunksPerBlock)) + 3;
6831 + dev->nCheckpointBlocksRequired = nBlocks;
6834 + return dev->nCheckpointBlocksRequired;
6838 + * Check if there's space to allocate...
6839 + * Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6840 + */
6841 +static int yaffs_CheckSpaceForAllocation(yaffs_Device *dev)
6843 + int reservedChunks;
6844 + int reservedBlocks = dev->nReservedBlocks;
6845 + int checkpointBlocks;
6847 + if (dev->isYaffs2) {
6848 + checkpointBlocks = yaffs_CalcCheckpointBlocksRequired(dev) -
6849 + dev->blocksInCheckpoint;
6850 + if (checkpointBlocks < 0)
6851 + checkpointBlocks = 0;
6852 + } else {
6853 + checkpointBlocks = 0;
6856 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6858 + return (dev->nFreeChunks > reservedChunks);
6861 +static int yaffs_AllocateChunk(yaffs_Device *dev, int useReserve,
6862 + yaffs_BlockInfo **blockUsedPtr)
6864 + int retVal;
6865 + yaffs_BlockInfo *bi;
6867 + if (dev->allocationBlock < 0) {
6868 + /* Get next block to allocate off */
6869 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6870 + dev->allocationPage = 0;
6873 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6874 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6875 + return -1;
6878 + if (dev->nErasedBlocks < dev->nReservedBlocks
6879 + && dev->allocationPage == 0) {
6880 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6883 + /* Next page please.... */
6884 + if (dev->allocationBlock >= 0) {
6885 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6887 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6888 + dev->allocationPage;
6889 + bi->pagesInUse++;
6890 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6891 + dev->allocationPage);
6893 + dev->allocationPage++;
6895 + dev->nFreeChunks--;
6897 + /* If the block is full set the state to full */
6898 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6899 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6900 + dev->allocationBlock = -1;
6903 + if (blockUsedPtr)
6904 + *blockUsedPtr = bi;
6906 + return retVal;
6909 + T(YAFFS_TRACE_ERROR,
6910 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6912 + return -1;
6915 +static int yaffs_GetErasedChunks(yaffs_Device *dev)
6917 + int n;
6919 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6921 + if (dev->allocationBlock > 0)
6922 + n += (dev->nChunksPerBlock - dev->allocationPage);
6924 + return n;
6928 +static int yaffs_GarbageCollectBlock(yaffs_Device *dev, int block,
6929 + int wholeBlock)
6931 + int oldChunk;
6932 + int newChunk;
6933 + int markNAND;
6934 + int retVal = YAFFS_OK;
6935 + int cleanups = 0;
6936 + int i;
6937 + int isCheckpointBlock;
6938 + int matchingChunk;
6939 + int maxCopies;
6941 + int chunksBefore = yaffs_GetErasedChunks(dev);
6942 + int chunksAfter;
6944 + yaffs_ExtendedTags tags;
6946 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6948 + yaffs_Object *object;
6950 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6952 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6954 + T(YAFFS_TRACE_TRACING,
6955 + (TSTR("Collecting block %d, in use %d, shrink %d, wholeBlock %d" TENDSTR),
6956 + block,
6957 + bi->pagesInUse,
6958 + bi->hasShrinkHeader,
6959 + wholeBlock));
6961 + /*yaffs_VerifyFreeChunks(dev); */
6963 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6965 + /* Take off the number of soft deleted entries because
6966 + * they're going to get really deleted during GC.
6967 + */
6968 + dev->nFreeChunks -= bi->softDeletions;
6970 + dev->isDoingGC = 1;
6972 + if (isCheckpointBlock ||
6973 + !yaffs_StillSomeChunkBits(dev, block)) {
6974 + T(YAFFS_TRACE_TRACING,
6975 + (TSTR
6976 + ("Collecting block %d that has no chunks in use" TENDSTR),
6977 + block));
6978 + yaffs_BlockBecameDirty(dev, block);
6979 + } else {
6981 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6983 + yaffs_VerifyBlock(dev, bi, block);
6985 + maxCopies = (wholeBlock) ? dev->nChunksPerBlock : 10;
6986 + oldChunk = block * dev->nChunksPerBlock + dev->gcChunk;
6988 + for (/* init already done */;
6989 + retVal == YAFFS_OK &&
6990 + dev->gcChunk < dev->nChunksPerBlock &&
6991 + (bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) &&
6992 + maxCopies > 0;
6993 + dev->gcChunk++, oldChunk++) {
6994 + if (yaffs_CheckChunkBit(dev, block, dev->gcChunk)) {
6996 + /* This page is in use and might need to be copied off */
6998 + maxCopies--;
7000 + markNAND = 1;
7002 + yaffs_InitialiseTags(&tags);
7004 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
7005 + buffer, &tags);
7007 + object =
7008 + yaffs_FindObjectByNumber(dev,
7009 + tags.objectId);
7011 + T(YAFFS_TRACE_GC_DETAIL,
7012 + (TSTR
7013 + ("Collecting chunk in block %d, %d %d %d " TENDSTR),
7014 + dev->gcChunk, tags.objectId, tags.chunkId,
7015 + tags.byteCount));
7017 + if (object && !yaffs_SkipVerification(dev)) {
7018 + if (tags.chunkId == 0)
7019 + matchingChunk = object->hdrChunk;
7020 + else if (object->softDeleted)
7021 + matchingChunk = oldChunk; /* Defeat the test */
7022 + else
7023 + matchingChunk = yaffs_FindChunkInFile(object, tags.chunkId, NULL);
7025 + if (oldChunk != matchingChunk)
7026 + T(YAFFS_TRACE_ERROR,
7027 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
7028 + oldChunk, matchingChunk, tags.objectId, tags.chunkId));
7032 + if (!object) {
7033 + T(YAFFS_TRACE_ERROR,
7034 + (TSTR
7035 + ("page %d in gc has no object: %d %d %d "
7036 + TENDSTR), oldChunk,
7037 + tags.objectId, tags.chunkId, tags.byteCount));
7040 + if (object &&
7041 + object->deleted &&
7042 + object->softDeleted &&
7043 + tags.chunkId != 0) {
7044 + /* Data chunk in a soft deleted file, throw it away
7045 + * It's a soft deleted data chunk,
7046 + * No need to copy this, just forget about it and
7047 + * fix up the object.
7048 + */
7050 + object->nDataChunks--;
7052 + if (object->nDataChunks <= 0) {
7053 + /* remeber to clean up the object */
7054 + dev->gcCleanupList[cleanups] =
7055 + tags.objectId;
7056 + cleanups++;
7058 + markNAND = 0;
7059 + } else if (0) {
7060 + /* Todo object && object->deleted && object->nDataChunks == 0 */
7061 + /* Deleted object header with no data chunks.
7062 + * Can be discarded and the file deleted.
7063 + */
7064 + object->hdrChunk = 0;
7065 + yaffs_FreeTnode(object->myDev,
7066 + object->variant.
7067 + fileVariant.top);
7068 + object->variant.fileVariant.top = NULL;
7069 + yaffs_DoGenericObjectDeletion(object);
7071 + } else if (object) {
7072 + /* It's either a data chunk in a live file or
7073 + * an ObjectHeader, so we're interested in it.
7074 + * NB Need to keep the ObjectHeaders of deleted files
7075 + * until the whole file has been deleted off
7076 + */
7077 + tags.serialNumber++;
7079 + dev->nGCCopies++;
7081 + if (tags.chunkId == 0) {
7082 + /* It is an object Id,
7083 + * We need to nuke the shrinkheader flags first
7084 + * We no longer want the shrinkHeader flag since its work is done
7085 + * and if it is left in place it will mess up scanning.
7086 + */
7088 + yaffs_ObjectHeader *oh;
7089 + oh = (yaffs_ObjectHeader *)buffer;
7090 + oh->isShrink = 0;
7091 + tags.extraIsShrinkHeader = 0;
7093 + yaffs_VerifyObjectHeader(object, oh, &tags, 1);
7096 + newChunk =
7097 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
7099 + if (newChunk < 0) {
7100 + retVal = YAFFS_FAIL;
7101 + } else {
7103 + /* Ok, now fix up the Tnodes etc. */
7105 + if (tags.chunkId == 0) {
7106 + /* It's a header */
7107 + object->hdrChunk = newChunk;
7108 + object->serial = tags.serialNumber;
7109 + } else {
7110 + /* It's a data chunk */
7111 + yaffs_PutChunkIntoFile
7112 + (object,
7113 + tags.chunkId,
7114 + newChunk, 0);
7119 + if (retVal == YAFFS_OK)
7120 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
7125 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7128 + /* Do any required cleanups */
7129 + for (i = 0; i < cleanups; i++) {
7130 + /* Time to delete the file too */
7131 + object =
7132 + yaffs_FindObjectByNumber(dev,
7133 + dev->gcCleanupList[i]);
7134 + if (object) {
7135 + yaffs_FreeTnode(dev,
7136 + object->variant.fileVariant.
7137 + top);
7138 + object->variant.fileVariant.top = NULL;
7139 + T(YAFFS_TRACE_GC,
7140 + (TSTR
7141 + ("yaffs: About to finally delete object %d"
7142 + TENDSTR), object->objectId));
7143 + yaffs_DoGenericObjectDeletion(object);
7144 + object->myDev->nDeletedFiles--;
7151 + yaffs_VerifyCollectedBlock(dev, bi, block);
7153 + chunksAfter = yaffs_GetErasedChunks(dev);
7154 + if (chunksBefore >= chunksAfter) {
7155 + T(YAFFS_TRACE_GC,
7156 + (TSTR
7157 + ("gc did not increase free chunks before %d after %d"
7158 + TENDSTR), chunksBefore, chunksAfter));
7161 + /* If the gc completed then clear the current gcBlock so that we find another. */
7162 + if (bi->blockState != YAFFS_BLOCK_STATE_COLLECTING) {
7163 + dev->gcBlock = -1;
7164 + dev->gcChunk = 0;
7167 + dev->isDoingGC = 0;
7169 + return retVal;
7172 +/* New garbage collector
7173 + * If we're very low on erased blocks then we do aggressive garbage collection
7174 + * otherwise we do "leasurely" garbage collection.
7175 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
7176 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
7178 + * The idea is to help clear out space in a more spread-out manner.
7179 + * Dunno if it really does anything useful.
7180 + */
7181 +static int yaffs_CheckGarbageCollection(yaffs_Device *dev)
7183 + int block;
7184 + int aggressive;
7185 + int gcOk = YAFFS_OK;
7186 + int maxTries = 0;
7188 + int checkpointBlockAdjust;
7190 + if (dev->isDoingGC) {
7191 + /* Bail out so we don't get recursive gc */
7192 + return YAFFS_OK;
7195 + /* This loop should pass the first time.
7196 + * We'll only see looping here if the erase of the collected block fails.
7197 + */
7199 + do {
7200 + maxTries++;
7202 + checkpointBlockAdjust = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
7203 + if (checkpointBlockAdjust < 0)
7204 + checkpointBlockAdjust = 0;
7206 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
7207 + /* We need a block soon...*/
7208 + aggressive = 1;
7209 + } else {
7210 + /* We're in no hurry */
7211 + aggressive = 0;
7214 + if (dev->gcBlock <= 0) {
7215 + dev->gcBlock = yaffs_FindBlockForGarbageCollection(dev, aggressive);
7216 + dev->gcChunk = 0;
7219 + block = dev->gcBlock;
7221 + if (block > 0) {
7222 + dev->garbageCollections++;
7223 + if (!aggressive)
7224 + dev->passiveGarbageCollections++;
7226 + T(YAFFS_TRACE_GC,
7227 + (TSTR
7228 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
7229 + dev->nErasedBlocks, aggressive));
7231 + gcOk = yaffs_GarbageCollectBlock(dev, block, aggressive);
7234 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
7235 + T(YAFFS_TRACE_GC,
7236 + (TSTR
7237 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
7238 + TENDSTR), dev->nErasedBlocks, maxTries, block));
7240 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) &&
7241 + (block > 0) &&
7242 + (maxTries < 2));
7244 + return aggressive ? gcOk : YAFFS_OK;
7247 +/*------------------------- TAGS --------------------------------*/
7249 +static int yaffs_TagsMatch(const yaffs_ExtendedTags *tags, int objectId,
7250 + int chunkInObject)
7252 + return (tags->chunkId == chunkInObject &&
7253 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
7258 +/*-------------------- Data file manipulation -----------------*/
7260 +static int yaffs_FindChunkInFile(yaffs_Object *in, int chunkInInode,
7261 + yaffs_ExtendedTags *tags)
7263 + /*Get the Tnode, then get the level 0 offset chunk offset */
7264 + yaffs_Tnode *tn;
7265 + int theChunk = -1;
7266 + yaffs_ExtendedTags localTags;
7267 + int retVal = -1;
7269 + yaffs_Device *dev = in->myDev;
7271 + if (!tags) {
7272 + /* Passed a NULL, so use our own tags space */
7273 + tags = &localTags;
7276 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
7278 + if (tn) {
7279 + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
7281 + retVal =
7282 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
7283 + chunkInInode);
7285 + return retVal;
7288 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in, int chunkInInode,
7289 + yaffs_ExtendedTags *tags)
7291 + /* Get the Tnode, then get the level 0 offset chunk offset */
7292 + yaffs_Tnode *tn;
7293 + int theChunk = -1;
7294 + yaffs_ExtendedTags localTags;
7296 + yaffs_Device *dev = in->myDev;
7297 + int retVal = -1;
7299 + if (!tags) {
7300 + /* Passed a NULL, so use our own tags space */
7301 + tags = &localTags;
7304 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
7306 + if (tn) {
7308 + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
7310 + retVal =
7311 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
7312 + chunkInInode);
7314 + /* Delete the entry in the filestructure (if found) */
7315 + if (retVal != -1)
7316 + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, 0);
7319 + return retVal;
7322 +#ifdef YAFFS_PARANOID
7324 +static int yaffs_CheckFileSanity(yaffs_Object *in)
7326 + int chunk;
7327 + int nChunks;
7328 + int fSize;
7329 + int failed = 0;
7330 + int objId;
7331 + yaffs_Tnode *tn;
7332 + yaffs_Tags localTags;
7333 + yaffs_Tags *tags = &localTags;
7334 + int theChunk;
7335 + int chunkDeleted;
7337 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
7338 + return YAFFS_FAIL;
7340 + objId = in->objectId;
7341 + fSize = in->variant.fileVariant.fileSize;
7342 + nChunks =
7343 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
7345 + for (chunk = 1; chunk <= nChunks; chunk++) {
7346 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
7347 + chunk);
7349 + if (tn) {
7351 + theChunk = yaffs_GetChunkGroupBase(dev, tn, chunk);
7353 + if (yaffs_CheckChunkBits
7354 + (dev, theChunk / dev->nChunksPerBlock,
7355 + theChunk % dev->nChunksPerBlock)) {
7357 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
7358 + tags,
7359 + &chunkDeleted);
7360 + if (yaffs_TagsMatch
7361 + (tags, in->objectId, chunk, chunkDeleted)) {
7362 + /* found it; */
7365 + } else {
7367 + failed = 1;
7370 + } else {
7371 + /* T(("No level 0 found for %d\n", chunk)); */
7375 + return failed ? YAFFS_FAIL : YAFFS_OK;
7378 +#endif
7380 +static int yaffs_PutChunkIntoFile(yaffs_Object *in, int chunkInInode,
7381 + int chunkInNAND, int inScan)
7383 + /* NB inScan is zero unless scanning.
7384 + * For forward scanning, inScan is > 0;
7385 + * for backward scanning inScan is < 0
7386 + */
7388 + yaffs_Tnode *tn;
7389 + yaffs_Device *dev = in->myDev;
7390 + int existingChunk;
7391 + yaffs_ExtendedTags existingTags;
7392 + yaffs_ExtendedTags newTags;
7393 + unsigned existingSerial, newSerial;
7395 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7396 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
7397 + * If it is not during Scanning then something went wrong!
7398 + */
7399 + if (!inScan) {
7400 + T(YAFFS_TRACE_ERROR,
7401 + (TSTR
7402 + ("yaffs tragedy:attempt to put data chunk into a non-file"
7403 + TENDSTR)));
7404 + YBUG();
7407 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7408 + return YAFFS_OK;
7411 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7412 + &in->variant.fileVariant,
7413 + chunkInInode,
7414 + NULL);
7415 + if (!tn)
7416 + return YAFFS_FAIL;
7418 + existingChunk = yaffs_GetChunkGroupBase(dev, tn, chunkInInode);
7420 + if (inScan != 0) {
7421 + /* If we're scanning then we need to test for duplicates
7422 + * NB This does not need to be efficient since it should only ever
7423 + * happen when the power fails during a write, then only one
7424 + * chunk should ever be affected.
7426 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7427 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7428 + */
7430 + if (existingChunk > 0) {
7431 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7432 + * thus we have to do a FindChunkInFile to get the real chunk id.
7434 + * We have a duplicate now we need to decide which one to use:
7436 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7437 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7438 + * YAFFS1: Get both sets of tags and compare serial numbers.
7439 + */
7441 + if (inScan > 0) {
7442 + /* Only do this for forward scanning */
7443 + yaffs_ReadChunkWithTagsFromNAND(dev,
7444 + chunkInNAND,
7445 + NULL, &newTags);
7447 + /* Do a proper find */
7448 + existingChunk =
7449 + yaffs_FindChunkInFile(in, chunkInInode,
7450 + &existingTags);
7453 + if (existingChunk <= 0) {
7454 + /*Hoosterman - how did this happen? */
7456 + T(YAFFS_TRACE_ERROR,
7457 + (TSTR
7458 + ("yaffs tragedy: existing chunk < 0 in scan"
7459 + TENDSTR)));
7463 + /* NB The deleted flags should be false, otherwise the chunks will
7464 + * not be loaded during a scan
7465 + */
7467 + if (inScan > 0) {
7468 + newSerial = newTags.serialNumber;
7469 + existingSerial = existingTags.serialNumber;
7472 + if ((inScan > 0) &&
7473 + (in->myDev->isYaffs2 ||
7474 + existingChunk <= 0 ||
7475 + ((existingSerial + 1) & 3) == newSerial)) {
7476 + /* Forward scanning.
7477 + * Use new
7478 + * Delete the old one and drop through to update the tnode
7479 + */
7480 + yaffs_DeleteChunk(dev, existingChunk, 1,
7481 + __LINE__);
7482 + } else {
7483 + /* Backward scanning or we want to use the existing one
7484 + * Use existing.
7485 + * Delete the new one and return early so that the tnode isn't changed
7486 + */
7487 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7488 + __LINE__);
7489 + return YAFFS_OK;
7495 + if (existingChunk == 0)
7496 + in->nDataChunks++;
7498 + yaffs_PutLevel0Tnode(dev, tn, chunkInInode, chunkInNAND);
7500 + return YAFFS_OK;
7503 +static int yaffs_ReadChunkDataFromObject(yaffs_Object *in, int chunkInInode,
7504 + __u8 *buffer)
7506 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7508 + if (chunkInNAND >= 0)
7509 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7510 + buffer, NULL);
7511 + else {
7512 + T(YAFFS_TRACE_NANDACCESS,
7513 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7514 + chunkInNAND));
7515 + /* get sane (zero) data if you read a hole */
7516 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7517 + return 0;
7522 +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn)
7524 + int block;
7525 + int page;
7526 + yaffs_ExtendedTags tags;
7527 + yaffs_BlockInfo *bi;
7529 + if (chunkId <= 0)
7530 + return;
7532 + dev->nDeletions++;
7533 + block = chunkId / dev->nChunksPerBlock;
7534 + page = chunkId % dev->nChunksPerBlock;
7537 + if (!yaffs_CheckChunkBit(dev, block, page))
7538 + T(YAFFS_TRACE_VERIFY,
7539 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7540 + chunkId));
7542 + bi = yaffs_GetBlockInfo(dev, block);
7544 + T(YAFFS_TRACE_DELETION,
7545 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7547 + if (markNAND &&
7548 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7550 + yaffs_InitialiseTags(&tags);
7552 + tags.chunkDeleted = 1;
7554 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7555 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7556 + } else {
7557 + dev->nUnmarkedDeletions++;
7560 + /* Pull out of the management area.
7561 + * If the whole block became dirty, this will kick off an erasure.
7562 + */
7563 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7564 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7565 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7566 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7567 + dev->nFreeChunks++;
7569 + yaffs_ClearChunkBit(dev, block, page);
7571 + bi->pagesInUse--;
7573 + if (bi->pagesInUse == 0 &&
7574 + !bi->hasShrinkHeader &&
7575 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7576 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7577 + yaffs_BlockBecameDirty(dev, block);
7584 +static int yaffs_WriteChunkDataToObject(yaffs_Object *in, int chunkInInode,
7585 + const __u8 *buffer, int nBytes,
7586 + int useReserve)
7588 + /* Find old chunk Need to do this to get serial number
7589 + * Write new one and patch into tree.
7590 + * Invalidate old tags.
7591 + */
7593 + int prevChunkId;
7594 + yaffs_ExtendedTags prevTags;
7596 + int newChunkId;
7597 + yaffs_ExtendedTags newTags;
7599 + yaffs_Device *dev = in->myDev;
7601 + yaffs_CheckGarbageCollection(dev);
7603 + /* Get the previous chunk at this location in the file if it exists */
7604 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7606 + /* Set up new tags */
7607 + yaffs_InitialiseTags(&newTags);
7609 + newTags.chunkId = chunkInInode;
7610 + newTags.objectId = in->objectId;
7611 + newTags.serialNumber =
7612 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7613 + newTags.byteCount = nBytes;
7615 + if (nBytes < 1 || nBytes > dev->totalBytesPerChunk) {
7616 + T(YAFFS_TRACE_ERROR,
7617 + (TSTR("Writing %d bytes to chunk!!!!!!!!!" TENDSTR), nBytes));
7618 + YBUG();
7621 + newChunkId =
7622 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7623 + useReserve);
7625 + if (newChunkId >= 0) {
7626 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7628 + if (prevChunkId >= 0)
7629 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7631 + yaffs_CheckFileSanity(in);
7633 + return newChunkId;
7637 +/* UpdateObjectHeader updates the header on NAND for an object.
7638 + * If name is not NULL, then that new name is used.
7639 + */
7640 +int yaffs_UpdateObjectHeader(yaffs_Object *in, const YCHAR *name, int force,
7641 + int isShrink, int shadows)
7644 + yaffs_BlockInfo *bi;
7646 + yaffs_Device *dev = in->myDev;
7648 + int prevChunkId;
7649 + int retVal = 0;
7650 + int result = 0;
7652 + int newChunkId;
7653 + yaffs_ExtendedTags newTags;
7654 + yaffs_ExtendedTags oldTags;
7656 + __u8 *buffer = NULL;
7657 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7659 + yaffs_ObjectHeader *oh = NULL;
7661 + yaffs_strcpy(oldName, _Y("silly old name"));
7664 + if (!in->fake ||
7665 + in == dev->rootDir || /* The rootDir should also be saved */
7666 + force) {
7668 + yaffs_CheckGarbageCollection(dev);
7669 + yaffs_CheckObjectDetailsLoaded(in);
7671 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7672 + oh = (yaffs_ObjectHeader *) buffer;
7674 + prevChunkId = in->hdrChunk;
7676 + if (prevChunkId > 0) {
7677 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7678 + buffer, &oldTags);
7680 + yaffs_VerifyObjectHeader(in, oh, &oldTags, 0);
7682 + memcpy(oldName, oh->name, sizeof(oh->name));
7685 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7687 + oh->type = in->variantType;
7688 + oh->yst_mode = in->yst_mode;
7689 + oh->shadowsObject = oh->inbandShadowsObject = shadows;
7691 +#ifdef CONFIG_YAFFS_WINCE
7692 + oh->win_atime[0] = in->win_atime[0];
7693 + oh->win_ctime[0] = in->win_ctime[0];
7694 + oh->win_mtime[0] = in->win_mtime[0];
7695 + oh->win_atime[1] = in->win_atime[1];
7696 + oh->win_ctime[1] = in->win_ctime[1];
7697 + oh->win_mtime[1] = in->win_mtime[1];
7698 +#else
7699 + oh->yst_uid = in->yst_uid;
7700 + oh->yst_gid = in->yst_gid;
7701 + oh->yst_atime = in->yst_atime;
7702 + oh->yst_mtime = in->yst_mtime;
7703 + oh->yst_ctime = in->yst_ctime;
7704 + oh->yst_rdev = in->yst_rdev;
7705 +#endif
7706 + if (in->parent)
7707 + oh->parentObjectId = in->parent->objectId;
7708 + else
7709 + oh->parentObjectId = 0;
7711 + if (name && *name) {
7712 + memset(oh->name, 0, sizeof(oh->name));
7713 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7714 + } else if (prevChunkId >= 0)
7715 + memcpy(oh->name, oldName, sizeof(oh->name));
7716 + else
7717 + memset(oh->name, 0, sizeof(oh->name));
7719 + oh->isShrink = isShrink;
7721 + switch (in->variantType) {
7722 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7723 + /* Should not happen */
7724 + break;
7725 + case YAFFS_OBJECT_TYPE_FILE:
7726 + oh->fileSize =
7727 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7728 + || oh->parentObjectId ==
7729 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7730 + fileVariant.fileSize;
7731 + break;
7732 + case YAFFS_OBJECT_TYPE_HARDLINK:
7733 + oh->equivalentObjectId =
7734 + in->variant.hardLinkVariant.equivalentObjectId;
7735 + break;
7736 + case YAFFS_OBJECT_TYPE_SPECIAL:
7737 + /* Do nothing */
7738 + break;
7739 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7740 + /* Do nothing */
7741 + break;
7742 + case YAFFS_OBJECT_TYPE_SYMLINK:
7743 + yaffs_strncpy(oh->alias,
7744 + in->variant.symLinkVariant.alias,
7745 + YAFFS_MAX_ALIAS_LENGTH);
7746 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7747 + break;
7750 + /* Tags */
7751 + yaffs_InitialiseTags(&newTags);
7752 + in->serial++;
7753 + newTags.chunkId = 0;
7754 + newTags.objectId = in->objectId;
7755 + newTags.serialNumber = in->serial;
7757 + /* Add extra info for file header */
7759 + newTags.extraHeaderInfoAvailable = 1;
7760 + newTags.extraParentObjectId = oh->parentObjectId;
7761 + newTags.extraFileLength = oh->fileSize;
7762 + newTags.extraIsShrinkHeader = oh->isShrink;
7763 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7764 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7765 + newTags.extraObjectType = in->variantType;
7767 + yaffs_VerifyObjectHeader(in, oh, &newTags, 1);
7769 + /* Create new chunk in NAND */
7770 + newChunkId =
7771 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7772 + (prevChunkId >= 0) ? 1 : 0);
7774 + if (newChunkId >= 0) {
7776 + in->hdrChunk = newChunkId;
7778 + if (prevChunkId >= 0) {
7779 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7780 + __LINE__);
7783 + if (!yaffs_ObjectHasCachedWriteData(in))
7784 + in->dirty = 0;
7786 + /* If this was a shrink, then mark the block that the chunk lives on */
7787 + if (isShrink) {
7788 + bi = yaffs_GetBlockInfo(in->myDev,
7789 + newChunkId / in->myDev->nChunksPerBlock);
7790 + bi->hasShrinkHeader = 1;
7795 + retVal = newChunkId;
7799 + if (buffer)
7800 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7802 + return retVal;
7805 +/*------------------------ Short Operations Cache ----------------------------------------
7806 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7807 + * reads might be short sequential reads, and a lot of writes may be short
7808 + * sequential writes. eg. scanning/writing a jpeg file.
7809 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7810 + * with dumb-as-a-rock code.
7811 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7812 + * buffering.
7814 + * There are a limited number (~10) of cache chunks per device so that we don't
7815 + * need a very intelligent search.
7816 + */
7818 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7820 + yaffs_Device *dev = obj->myDev;
7821 + int i;
7822 + yaffs_ChunkCache *cache;
7823 + int nCaches = obj->myDev->nShortOpCaches;
7825 + for (i = 0; i < nCaches; i++) {
7826 + cache = &dev->srCache[i];
7827 + if (cache->object == obj &&
7828 + cache->dirty)
7829 + return 1;
7832 + return 0;
7836 +static void yaffs_FlushFilesChunkCache(yaffs_Object *obj)
7838 + yaffs_Device *dev = obj->myDev;
7839 + int lowest = -99; /* Stop compiler whining. */
7840 + int i;
7841 + yaffs_ChunkCache *cache;
7842 + int chunkWritten = 0;
7843 + int nCaches = obj->myDev->nShortOpCaches;
7845 + if (nCaches > 0) {
7846 + do {
7847 + cache = NULL;
7849 + /* Find the dirty cache for this object with the lowest chunk id. */
7850 + for (i = 0; i < nCaches; i++) {
7851 + if (dev->srCache[i].object == obj &&
7852 + dev->srCache[i].dirty) {
7853 + if (!cache
7854 + || dev->srCache[i].chunkId <
7855 + lowest) {
7856 + cache = &dev->srCache[i];
7857 + lowest = cache->chunkId;
7862 + if (cache && !cache->locked) {
7863 + /* Write it out and free it up */
7865 + chunkWritten =
7866 + yaffs_WriteChunkDataToObject(cache->object,
7867 + cache->chunkId,
7868 + cache->data,
7869 + cache->nBytes,
7870 + 1);
7871 + cache->dirty = 0;
7872 + cache->object = NULL;
7875 + } while (cache && chunkWritten > 0);
7877 + if (cache) {
7878 + /* Hoosterman, disk full while writing cache out. */
7879 + T(YAFFS_TRACE_ERROR,
7880 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7887 +/*yaffs_FlushEntireDeviceCache(dev)
7890 + */
7892 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7894 + yaffs_Object *obj;
7895 + int nCaches = dev->nShortOpCaches;
7896 + int i;
7898 + /* Find a dirty object in the cache and flush it...
7899 + * until there are no further dirty objects.
7900 + */
7901 + do {
7902 + obj = NULL;
7903 + for (i = 0; i < nCaches && !obj; i++) {
7904 + if (dev->srCache[i].object &&
7905 + dev->srCache[i].dirty)
7906 + obj = dev->srCache[i].object;
7909 + if (obj)
7910 + yaffs_FlushFilesChunkCache(obj);
7912 + } while (obj);
7917 +/* Grab us a cache chunk for use.
7918 + * First look for an empty one.
7919 + * Then look for the least recently used non-dirty one.
7920 + * Then look for the least recently used dirty one...., flush and look again.
7921 + */
7922 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev)
7924 + int i;
7926 + if (dev->nShortOpCaches > 0) {
7927 + for (i = 0; i < dev->nShortOpCaches; i++) {
7928 + if (!dev->srCache[i].object)
7929 + return &dev->srCache[i];
7933 + return NULL;
7936 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev)
7938 + yaffs_ChunkCache *cache;
7939 + yaffs_Object *theObj;
7940 + int usage;
7941 + int i;
7942 + int pushout;
7944 + if (dev->nShortOpCaches > 0) {
7945 + /* Try find a non-dirty one... */
7947 + cache = yaffs_GrabChunkCacheWorker(dev);
7949 + if (!cache) {
7950 + /* They were all dirty, find the last recently used object and flush
7951 + * its cache, then find again.
7952 + * NB what's here is not very accurate, we actually flush the object
7953 + * the last recently used page.
7954 + */
7956 + /* With locking we can't assume we can use entry zero */
7958 + theObj = NULL;
7959 + usage = -1;
7960 + cache = NULL;
7961 + pushout = -1;
7963 + for (i = 0; i < dev->nShortOpCaches; i++) {
7964 + if (dev->srCache[i].object &&
7965 + !dev->srCache[i].locked &&
7966 + (dev->srCache[i].lastUse < usage || !cache)) {
7967 + usage = dev->srCache[i].lastUse;
7968 + theObj = dev->srCache[i].object;
7969 + cache = &dev->srCache[i];
7970 + pushout = i;
7974 + if (!cache || cache->dirty) {
7975 + /* Flush and try again */
7976 + yaffs_FlushFilesChunkCache(theObj);
7977 + cache = yaffs_GrabChunkCacheWorker(dev);
7981 + return cache;
7982 + } else
7983 + return NULL;
7987 +/* Find a cached chunk */
7988 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj,
7989 + int chunkId)
7991 + yaffs_Device *dev = obj->myDev;
7992 + int i;
7993 + if (dev->nShortOpCaches > 0) {
7994 + for (i = 0; i < dev->nShortOpCaches; i++) {
7995 + if (dev->srCache[i].object == obj &&
7996 + dev->srCache[i].chunkId == chunkId) {
7997 + dev->cacheHits++;
7999 + return &dev->srCache[i];
8003 + return NULL;
8006 +/* Mark the chunk for the least recently used algorithym */
8007 +static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache,
8008 + int isAWrite)
8011 + if (dev->nShortOpCaches > 0) {
8012 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
8013 + /* Reset the cache usages */
8014 + int i;
8015 + for (i = 1; i < dev->nShortOpCaches; i++)
8016 + dev->srCache[i].lastUse = 0;
8018 + dev->srLastUse = 0;
8021 + dev->srLastUse++;
8023 + cache->lastUse = dev->srLastUse;
8025 + if (isAWrite)
8026 + cache->dirty = 1;
8030 +/* Invalidate a single cache page.
8031 + * Do this when a whole page gets written,
8032 + * ie the short cache for this page is no longer valid.
8033 + */
8034 +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId)
8036 + if (object->myDev->nShortOpCaches > 0) {
8037 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
8039 + if (cache)
8040 + cache->object = NULL;
8044 +/* Invalidate all the cache pages associated with this object
8045 + * Do this whenever ther file is deleted or resized.
8046 + */
8047 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in)
8049 + int i;
8050 + yaffs_Device *dev = in->myDev;
8052 + if (dev->nShortOpCaches > 0) {
8053 + /* Invalidate it. */
8054 + for (i = 0; i < dev->nShortOpCaches; i++) {
8055 + if (dev->srCache[i].object == in)
8056 + dev->srCache[i].object = NULL;
8061 +/*--------------------- Checkpointing --------------------*/
8064 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev, int head)
8066 + yaffs_CheckpointValidity cp;
8068 + memset(&cp, 0, sizeof(cp));
8070 + cp.structType = sizeof(cp);
8071 + cp.magic = YAFFS_MAGIC;
8072 + cp.version = YAFFS_CHECKPOINT_VERSION;
8073 + cp.head = (head) ? 1 : 0;
8075 + return (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp)) ?
8076 + 1 : 0;
8079 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
8081 + yaffs_CheckpointValidity cp;
8082 + int ok;
8084 + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
8086 + if (ok)
8087 + ok = (cp.structType == sizeof(cp)) &&
8088 + (cp.magic == YAFFS_MAGIC) &&
8089 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
8090 + (cp.head == ((head) ? 1 : 0));
8091 + return ok ? 1 : 0;
8094 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
8095 + yaffs_Device *dev)
8097 + cp->nErasedBlocks = dev->nErasedBlocks;
8098 + cp->allocationBlock = dev->allocationBlock;
8099 + cp->allocationPage = dev->allocationPage;
8100 + cp->nFreeChunks = dev->nFreeChunks;
8102 + cp->nDeletedFiles = dev->nDeletedFiles;
8103 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
8104 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
8105 + cp->sequenceNumber = dev->sequenceNumber;
8106 + cp->oldestDirtySequence = dev->oldestDirtySequence;
8110 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
8111 + yaffs_CheckpointDevice *cp)
8113 + dev->nErasedBlocks = cp->nErasedBlocks;
8114 + dev->allocationBlock = cp->allocationBlock;
8115 + dev->allocationPage = cp->allocationPage;
8116 + dev->nFreeChunks = cp->nFreeChunks;
8118 + dev->nDeletedFiles = cp->nDeletedFiles;
8119 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
8120 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
8121 + dev->sequenceNumber = cp->sequenceNumber;
8122 + dev->oldestDirtySequence = cp->oldestDirtySequence;
8126 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
8128 + yaffs_CheckpointDevice cp;
8129 + __u32 nBytes;
8130 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
8132 + int ok;
8134 + /* Write device runtime values*/
8135 + yaffs_DeviceToCheckpointDevice(&cp, dev);
8136 + cp.structType = sizeof(cp);
8138 + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
8140 + /* Write block info */
8141 + if (ok) {
8142 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
8143 + ok = (yaffs_CheckpointWrite(dev, dev->blockInfo, nBytes) == nBytes);
8146 + /* Write chunk bits */
8147 + if (ok) {
8148 + nBytes = nBlocks * dev->chunkBitmapStride;
8149 + ok = (yaffs_CheckpointWrite(dev, dev->chunkBits, nBytes) == nBytes);
8151 + return ok ? 1 : 0;
8155 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
8157 + yaffs_CheckpointDevice cp;
8158 + __u32 nBytes;
8159 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
8161 + int ok;
8163 + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
8164 + if (!ok)
8165 + return 0;
8167 + if (cp.structType != sizeof(cp))
8168 + return 0;
8171 + yaffs_CheckpointDeviceToDevice(dev, &cp);
8173 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
8175 + ok = (yaffs_CheckpointRead(dev, dev->blockInfo, nBytes) == nBytes);
8177 + if (!ok)
8178 + return 0;
8179 + nBytes = nBlocks * dev->chunkBitmapStride;
8181 + ok = (yaffs_CheckpointRead(dev, dev->chunkBits, nBytes) == nBytes);
8183 + return ok ? 1 : 0;
8186 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
8187 + yaffs_Object *obj)
8190 + cp->objectId = obj->objectId;
8191 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
8192 + cp->hdrChunk = obj->hdrChunk;
8193 + cp->variantType = obj->variantType;
8194 + cp->deleted = obj->deleted;
8195 + cp->softDeleted = obj->softDeleted;
8196 + cp->unlinked = obj->unlinked;
8197 + cp->fake = obj->fake;
8198 + cp->renameAllowed = obj->renameAllowed;
8199 + cp->unlinkAllowed = obj->unlinkAllowed;
8200 + cp->serial = obj->serial;
8201 + cp->nDataChunks = obj->nDataChunks;
8203 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
8204 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
8205 + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
8206 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
8209 +static int yaffs_CheckpointObjectToObject(yaffs_Object *obj, yaffs_CheckpointObject *cp)
8212 + yaffs_Object *parent;
8214 + if (obj->variantType != cp->variantType) {
8215 + T(YAFFS_TRACE_ERROR, (TSTR("Checkpoint read object %d type %d "
8216 + TCONT("chunk %d does not match existing object type %d")
8217 + TENDSTR), cp->objectId, cp->variantType, cp->hdrChunk,
8218 + obj->variantType));
8219 + return 0;
8222 + obj->objectId = cp->objectId;
8224 + if (cp->parentId)
8225 + parent = yaffs_FindOrCreateObjectByNumber(
8226 + obj->myDev,
8227 + cp->parentId,
8228 + YAFFS_OBJECT_TYPE_DIRECTORY);
8229 + else
8230 + parent = NULL;
8232 + if (parent) {
8233 + if (parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
8234 + T(YAFFS_TRACE_ALWAYS, (TSTR("Checkpoint read object %d parent %d type %d"
8235 + TCONT(" chunk %d Parent type, %d, not directory")
8236 + TENDSTR),
8237 + cp->objectId, cp->parentId, cp->variantType,
8238 + cp->hdrChunk, parent->variantType));
8239 + return 0;
8241 + yaffs_AddObjectToDirectory(parent, obj);
8244 + obj->hdrChunk = cp->hdrChunk;
8245 + obj->variantType = cp->variantType;
8246 + obj->deleted = cp->deleted;
8247 + obj->softDeleted = cp->softDeleted;
8248 + obj->unlinked = cp->unlinked;
8249 + obj->fake = cp->fake;
8250 + obj->renameAllowed = cp->renameAllowed;
8251 + obj->unlinkAllowed = cp->unlinkAllowed;
8252 + obj->serial = cp->serial;
8253 + obj->nDataChunks = cp->nDataChunks;
8255 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
8256 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
8257 + else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
8258 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
8260 + if (obj->hdrChunk > 0)
8261 + obj->lazyLoaded = 1;
8262 + return 1;
8267 +static int yaffs_CheckpointTnodeWorker(yaffs_Object *in, yaffs_Tnode *tn,
8268 + __u32 level, int chunkOffset)
8270 + int i;
8271 + yaffs_Device *dev = in->myDev;
8272 + int ok = 1;
8273 + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
8275 + if (tnodeSize < sizeof(yaffs_Tnode))
8276 + tnodeSize = sizeof(yaffs_Tnode);
8279 + if (tn) {
8280 + if (level > 0) {
8282 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
8283 + if (tn->internal[i]) {
8284 + ok = yaffs_CheckpointTnodeWorker(in,
8285 + tn->internal[i],
8286 + level - 1,
8287 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
8290 + } else if (level == 0) {
8291 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
8292 + ok = (yaffs_CheckpointWrite(dev, &baseOffset, sizeof(baseOffset)) == sizeof(baseOffset));
8293 + if (ok)
8294 + ok = (yaffs_CheckpointWrite(dev, tn, tnodeSize) == tnodeSize);
8298 + return ok;
8302 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
8304 + __u32 endMarker = ~0;
8305 + int ok = 1;
8307 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8308 + ok = yaffs_CheckpointTnodeWorker(obj,
8309 + obj->variant.fileVariant.top,
8310 + obj->variant.fileVariant.topLevel,
8311 + 0);
8312 + if (ok)
8313 + ok = (yaffs_CheckpointWrite(obj->myDev, &endMarker, sizeof(endMarker)) ==
8314 + sizeof(endMarker));
8317 + return ok ? 1 : 0;
8320 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
8322 + __u32 baseChunk;
8323 + int ok = 1;
8324 + yaffs_Device *dev = obj->myDev;
8325 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
8326 + yaffs_Tnode *tn;
8327 + int nread = 0;
8328 + int tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
8330 + if (tnodeSize < sizeof(yaffs_Tnode))
8331 + tnodeSize = sizeof(yaffs_Tnode);
8333 + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
8335 + while (ok && (~baseChunk)) {
8336 + nread++;
8337 + /* Read level 0 tnode */
8340 + tn = yaffs_GetTnodeRaw(dev);
8341 + if (tn)
8342 + ok = (yaffs_CheckpointRead(dev, tn, tnodeSize) == tnodeSize);
8343 + else
8344 + ok = 0;
8346 + if (tn && ok)
8347 + ok = yaffs_AddOrFindLevel0Tnode(dev,
8348 + fileStructPtr,
8349 + baseChunk,
8350 + tn) ? 1 : 0;
8352 + if (ok)
8353 + ok = (yaffs_CheckpointRead(dev, &baseChunk, sizeof(baseChunk)) == sizeof(baseChunk));
8357 + T(YAFFS_TRACE_CHECKPOINT, (
8358 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
8359 + nread, baseChunk, ok));
8361 + return ok ? 1 : 0;
8365 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
8367 + yaffs_Object *obj;
8368 + yaffs_CheckpointObject cp;
8369 + int i;
8370 + int ok = 1;
8371 + struct ylist_head *lh;
8374 + /* Iterate through the objects in each hash entry,
8375 + * dumping them to the checkpointing stream.
8376 + */
8378 + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
8379 + ylist_for_each(lh, &dev->objectBucket[i].list) {
8380 + if (lh) {
8381 + obj = ylist_entry(lh, yaffs_Object, hashLink);
8382 + if (!obj->deferedFree) {
8383 + yaffs_ObjectToCheckpointObject(&cp, obj);
8384 + cp.structType = sizeof(cp);
8386 + T(YAFFS_TRACE_CHECKPOINT, (
8387 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
8388 + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk, (unsigned) obj));
8390 + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
8392 + if (ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE)
8393 + ok = yaffs_WriteCheckpointTnodes(obj);
8399 + /* Dump end of list */
8400 + memset(&cp, 0xFF, sizeof(yaffs_CheckpointObject));
8401 + cp.structType = sizeof(cp);
8403 + if (ok)
8404 + ok = (yaffs_CheckpointWrite(dev, &cp, sizeof(cp)) == sizeof(cp));
8406 + return ok ? 1 : 0;
8409 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8411 + yaffs_Object *obj;
8412 + yaffs_CheckpointObject cp;
8413 + int ok = 1;
8414 + int done = 0;
8415 + yaffs_Object *hardList = NULL;
8417 + while (ok && !done) {
8418 + ok = (yaffs_CheckpointRead(dev, &cp, sizeof(cp)) == sizeof(cp));
8419 + if (cp.structType != sizeof(cp)) {
8420 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("struct size %d instead of %d ok %d"TENDSTR),
8421 + cp.structType, sizeof(cp), ok));
8422 + ok = 0;
8425 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8426 + cp.objectId, cp.parentId, cp.variantType, cp.hdrChunk));
8428 + if (ok && cp.objectId == ~0)
8429 + done = 1;
8430 + else if (ok) {
8431 + obj = yaffs_FindOrCreateObjectByNumber(dev, cp.objectId, cp.variantType);
8432 + if (obj) {
8433 + ok = yaffs_CheckpointObjectToObject(obj, &cp);
8434 + if (!ok)
8435 + break;
8436 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8437 + ok = yaffs_ReadCheckpointTnodes(obj);
8438 + } else if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8439 + obj->hardLinks.next =
8440 + (struct ylist_head *) hardList;
8441 + hardList = obj;
8443 + } else
8444 + ok = 0;
8448 + if (ok)
8449 + yaffs_HardlinkFixup(dev, hardList);
8451 + return ok ? 1 : 0;
8454 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8456 + __u32 checkpointSum;
8457 + int ok;
8459 + yaffs_GetCheckpointSum(dev, &checkpointSum);
8461 + ok = (yaffs_CheckpointWrite(dev, &checkpointSum, sizeof(checkpointSum)) == sizeof(checkpointSum));
8463 + if (!ok)
8464 + return 0;
8466 + return 1;
8469 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8471 + __u32 checkpointSum0;
8472 + __u32 checkpointSum1;
8473 + int ok;
8475 + yaffs_GetCheckpointSum(dev, &checkpointSum0);
8477 + ok = (yaffs_CheckpointRead(dev, &checkpointSum1, sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8479 + if (!ok)
8480 + return 0;
8482 + if (checkpointSum0 != checkpointSum1)
8483 + return 0;
8485 + return 1;
8489 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8491 + int ok = 1;
8493 + if (dev->skipCheckpointWrite || !dev->isYaffs2) {
8494 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint write" TENDSTR)));
8495 + ok = 0;
8498 + if (ok)
8499 + ok = yaffs_CheckpointOpen(dev, 1);
8501 + if (ok) {
8502 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
8503 + ok = yaffs_WriteCheckpointValidityMarker(dev, 1);
8505 + if (ok) {
8506 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint device" TENDSTR)));
8507 + ok = yaffs_WriteCheckpointDevice(dev);
8509 + if (ok) {
8510 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint objects" TENDSTR)));
8511 + ok = yaffs_WriteCheckpointObjects(dev);
8513 + if (ok) {
8514 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("write checkpoint validity" TENDSTR)));
8515 + ok = yaffs_WriteCheckpointValidityMarker(dev, 0);
8518 + if (ok)
8519 + ok = yaffs_WriteCheckpointSum(dev);
8521 + if (!yaffs_CheckpointClose(dev))
8522 + ok = 0;
8524 + if (ok)
8525 + dev->isCheckpointed = 1;
8526 + else
8527 + dev->isCheckpointed = 0;
8529 + return dev->isCheckpointed;
8532 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8534 + int ok = 1;
8536 + if (dev->skipCheckpointRead || !dev->isYaffs2) {
8537 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("skipping checkpoint read" TENDSTR)));
8538 + ok = 0;
8541 + if (ok)
8542 + ok = yaffs_CheckpointOpen(dev, 0); /* open for read */
8544 + if (ok) {
8545 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
8546 + ok = yaffs_ReadCheckpointValidityMarker(dev, 1);
8548 + if (ok) {
8549 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint device" TENDSTR)));
8550 + ok = yaffs_ReadCheckpointDevice(dev);
8552 + if (ok) {
8553 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint objects" TENDSTR)));
8554 + ok = yaffs_ReadCheckpointObjects(dev);
8556 + if (ok) {
8557 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint validity" TENDSTR)));
8558 + ok = yaffs_ReadCheckpointValidityMarker(dev, 0);
8561 + if (ok) {
8562 + ok = yaffs_ReadCheckpointSum(dev);
8563 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("read checkpoint checksum %d" TENDSTR), ok));
8566 + if (!yaffs_CheckpointClose(dev))
8567 + ok = 0;
8569 + if (ok)
8570 + dev->isCheckpointed = 1;
8571 + else
8572 + dev->isCheckpointed = 0;
8574 + return ok ? 1 : 0;
8578 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8580 + if (dev->isCheckpointed ||
8581 + dev->blocksInCheckpoint > 0) {
8582 + dev->isCheckpointed = 0;
8583 + yaffs_CheckpointInvalidateStream(dev);
8584 + if (dev->superBlock && dev->markSuperBlockDirty)
8585 + dev->markSuperBlockDirty(dev->superBlock);
8590 +int yaffs_CheckpointSave(yaffs_Device *dev)
8593 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("save entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
8595 + yaffs_VerifyObjects(dev);
8596 + yaffs_VerifyBlocks(dev);
8597 + yaffs_VerifyFreeChunks(dev);
8599 + if (!dev->isCheckpointed) {
8600 + yaffs_InvalidateCheckpoint(dev);
8601 + yaffs_WriteCheckpointData(dev);
8604 + T(YAFFS_TRACE_ALWAYS, (TSTR("save exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
8606 + return dev->isCheckpointed;
8609 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8611 + int retval;
8612 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore entry: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
8614 + retval = yaffs_ReadCheckpointData(dev);
8616 + if (dev->isCheckpointed) {
8617 + yaffs_VerifyObjects(dev);
8618 + yaffs_VerifyBlocks(dev);
8619 + yaffs_VerifyFreeChunks(dev);
8622 + T(YAFFS_TRACE_CHECKPOINT, (TSTR("restore exit: isCheckpointed %d"TENDSTR), dev->isCheckpointed));
8624 + return retval;
8627 +/*--------------------- File read/write ------------------------
8628 + * Read and write have very similar structures.
8629 + * In general the read/write has three parts to it
8630 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8631 + * Some complete chunks
8632 + * An incomplete chunk to end off with
8634 + * Curve-balls: the first chunk might also be the last chunk.
8635 + */
8637 +int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 *buffer, loff_t offset,
8638 + int nBytes)
8641 + int chunk;
8642 + __u32 start;
8643 + int nToCopy;
8644 + int n = nBytes;
8645 + int nDone = 0;
8646 + yaffs_ChunkCache *cache;
8648 + yaffs_Device *dev;
8650 + dev = in->myDev;
8652 + while (n > 0) {
8653 + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
8654 + /* start = offset % dev->nDataBytesPerChunk; */
8655 + yaffs_AddrToChunk(dev, offset, &chunk, &start);
8656 + chunk++;
8658 + /* OK now check for the curveball where the start and end are in
8659 + * the same chunk.
8660 + */
8661 + if ((start + n) < dev->nDataBytesPerChunk)
8662 + nToCopy = n;
8663 + else
8664 + nToCopy = dev->nDataBytesPerChunk - start;
8666 + cache = yaffs_FindChunkCache(in, chunk);
8668 + /* If the chunk is already in the cache or it is less than a whole chunk
8669 + * or we're using inband tags then use the cache (if there is caching)
8670 + * else bypass the cache.
8671 + */
8672 + if (cache || nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
8673 + if (dev->nShortOpCaches > 0) {
8675 + /* If we can't find the data in the cache, then load it up. */
8677 + if (!cache) {
8678 + cache = yaffs_GrabChunkCache(in->myDev);
8679 + cache->object = in;
8680 + cache->chunkId = chunk;
8681 + cache->dirty = 0;
8682 + cache->locked = 0;
8683 + yaffs_ReadChunkDataFromObject(in, chunk,
8684 + cache->
8685 + data);
8686 + cache->nBytes = 0;
8689 + yaffs_UseChunkCache(dev, cache, 0);
8691 + cache->locked = 1;
8694 + memcpy(buffer, &cache->data[start], nToCopy);
8696 + cache->locked = 0;
8697 + } else {
8698 + /* Read into the local buffer then copy..*/
8700 + __u8 *localBuffer =
8701 + yaffs_GetTempBuffer(dev, __LINE__);
8702 + yaffs_ReadChunkDataFromObject(in, chunk,
8703 + localBuffer);
8705 + memcpy(buffer, &localBuffer[start], nToCopy);
8708 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8709 + __LINE__);
8712 + } else {
8714 + /* A full chunk. Read directly into the supplied buffer. */
8715 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8719 + n -= nToCopy;
8720 + offset += nToCopy;
8721 + buffer += nToCopy;
8722 + nDone += nToCopy;
8726 + return nDone;
8729 +int yaffs_WriteDataToFile(yaffs_Object *in, const __u8 *buffer, loff_t offset,
8730 + int nBytes, int writeThrough)
8733 + int chunk;
8734 + __u32 start;
8735 + int nToCopy;
8736 + int n = nBytes;
8737 + int nDone = 0;
8738 + int nToWriteBack;
8739 + int startOfWrite = offset;
8740 + int chunkWritten = 0;
8741 + __u32 nBytesRead;
8742 + __u32 chunkStart;
8744 + yaffs_Device *dev;
8746 + dev = in->myDev;
8748 + while (n > 0 && chunkWritten >= 0) {
8749 + /* chunk = offset / dev->nDataBytesPerChunk + 1; */
8750 + /* start = offset % dev->nDataBytesPerChunk; */
8751 + yaffs_AddrToChunk(dev, offset, &chunk, &start);
8753 + if (chunk * dev->nDataBytesPerChunk + start != offset ||
8754 + start >= dev->nDataBytesPerChunk) {
8755 + T(YAFFS_TRACE_ERROR, (
8756 + TSTR("AddrToChunk of offset %d gives chunk %d start %d"
8757 + TENDSTR),
8758 + (int)offset, chunk, start));
8760 + chunk++;
8762 + /* OK now check for the curveball where the start and end are in
8763 + * the same chunk.
8764 + */
8766 + if ((start + n) < dev->nDataBytesPerChunk) {
8767 + nToCopy = n;
8769 + /* Now folks, to calculate how many bytes to write back....
8770 + * If we're overwriting and not writing to then end of file then
8771 + * we need to write back as much as was there before.
8772 + */
8774 + chunkStart = ((chunk - 1) * dev->nDataBytesPerChunk);
8776 + if (chunkStart > in->variant.fileVariant.fileSize)
8777 + nBytesRead = 0; /* Past end of file */
8778 + else
8779 + nBytesRead = in->variant.fileVariant.fileSize - chunkStart;
8781 + if (nBytesRead > dev->nDataBytesPerChunk)
8782 + nBytesRead = dev->nDataBytesPerChunk;
8784 + nToWriteBack =
8785 + (nBytesRead >
8786 + (start + n)) ? nBytesRead : (start + n);
8788 + if (nToWriteBack < 0 || nToWriteBack > dev->nDataBytesPerChunk)
8789 + YBUG();
8791 + } else {
8792 + nToCopy = dev->nDataBytesPerChunk - start;
8793 + nToWriteBack = dev->nDataBytesPerChunk;
8796 + if (nToCopy != dev->nDataBytesPerChunk || dev->inbandTags) {
8797 + /* An incomplete start or end chunk (or maybe both start and end chunk),
8798 + * or we're using inband tags, so we want to use the cache buffers.
8799 + */
8800 + if (dev->nShortOpCaches > 0) {
8801 + yaffs_ChunkCache *cache;
8802 + /* If we can't find the data in the cache, then load the cache */
8803 + cache = yaffs_FindChunkCache(in, chunk);
8805 + if (!cache
8806 + && yaffs_CheckSpaceForAllocation(in->
8807 + myDev)) {
8808 + cache = yaffs_GrabChunkCache(in->myDev);
8809 + cache->object = in;
8810 + cache->chunkId = chunk;
8811 + cache->dirty = 0;
8812 + cache->locked = 0;
8813 + yaffs_ReadChunkDataFromObject(in, chunk,
8814 + cache->
8815 + data);
8816 + } else if (cache &&
8817 + !cache->dirty &&
8818 + !yaffs_CheckSpaceForAllocation(in->myDev)) {
8819 + /* Drop the cache if it was a read cache item and
8820 + * no space check has been made for it.
8821 + */
8822 + cache = NULL;
8825 + if (cache) {
8826 + yaffs_UseChunkCache(dev, cache, 1);
8827 + cache->locked = 1;
8830 + memcpy(&cache->data[start], buffer,
8831 + nToCopy);
8834 + cache->locked = 0;
8835 + cache->nBytes = nToWriteBack;
8837 + if (writeThrough) {
8838 + chunkWritten =
8839 + yaffs_WriteChunkDataToObject
8840 + (cache->object,
8841 + cache->chunkId,
8842 + cache->data, cache->nBytes,
8843 + 1);
8844 + cache->dirty = 0;
8847 + } else {
8848 + chunkWritten = -1; /* fail the write */
8850 + } else {
8851 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8852 + * Read into the local buffer then copy, then copy over and write back.
8853 + */
8855 + __u8 *localBuffer =
8856 + yaffs_GetTempBuffer(dev, __LINE__);
8858 + yaffs_ReadChunkDataFromObject(in, chunk,
8859 + localBuffer);
8863 + memcpy(&localBuffer[start], buffer, nToCopy);
8865 + chunkWritten =
8866 + yaffs_WriteChunkDataToObject(in, chunk,
8867 + localBuffer,
8868 + nToWriteBack,
8869 + 0);
8871 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8872 + __LINE__);
8876 + } else {
8877 + /* A full chunk. Write directly from the supplied buffer. */
8881 + chunkWritten =
8882 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8883 + dev->nDataBytesPerChunk,
8884 + 0);
8886 + /* Since we've overwritten the cached data, we better invalidate it. */
8887 + yaffs_InvalidateChunkCache(in, chunk);
8890 + if (chunkWritten >= 0) {
8891 + n -= nToCopy;
8892 + offset += nToCopy;
8893 + buffer += nToCopy;
8894 + nDone += nToCopy;
8899 + /* Update file object */
8901 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize)
8902 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8904 + in->dirty = 1;
8906 + return nDone;
8910 +/* ---------------------- File resizing stuff ------------------ */
8912 +static void yaffs_PruneResizedChunks(yaffs_Object *in, int newSize)
8915 + yaffs_Device *dev = in->myDev;
8916 + int oldFileSize = in->variant.fileVariant.fileSize;
8918 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8920 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8921 + dev->nDataBytesPerChunk;
8922 + int i;
8923 + int chunkId;
8925 + /* Delete backwards so that we don't end up with holes if
8926 + * power is lost part-way through the operation.
8927 + */
8928 + for (i = lastDel; i >= startDel; i--) {
8929 + /* NB this could be optimised somewhat,
8930 + * eg. could retrieve the tags and write them without
8931 + * using yaffs_DeleteChunk
8932 + */
8934 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8935 + if (chunkId > 0) {
8936 + if (chunkId <
8937 + (dev->internalStartBlock * dev->nChunksPerBlock)
8938 + || chunkId >=
8939 + ((dev->internalEndBlock +
8940 + 1) * dev->nChunksPerBlock)) {
8941 + T(YAFFS_TRACE_ALWAYS,
8942 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8943 + chunkId, i));
8944 + } else {
8945 + in->nDataChunks--;
8946 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8953 +int yaffs_ResizeFile(yaffs_Object *in, loff_t newSize)
8956 + int oldFileSize = in->variant.fileVariant.fileSize;
8957 + __u32 newSizeOfPartialChunk;
8958 + int newFullChunks;
8960 + yaffs_Device *dev = in->myDev;
8962 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8964 + yaffs_FlushFilesChunkCache(in);
8965 + yaffs_InvalidateWholeChunkCache(in);
8967 + yaffs_CheckGarbageCollection(dev);
8969 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE)
8970 + return YAFFS_FAIL;
8972 + if (newSize == oldFileSize)
8973 + return YAFFS_OK;
8975 + if (newSize < oldFileSize) {
8977 + yaffs_PruneResizedChunks(in, newSize);
8979 + if (newSizeOfPartialChunk != 0) {
8980 + int lastChunk = 1 + newFullChunks;
8982 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8984 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8985 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8986 + localBuffer);
8988 + memset(localBuffer + newSizeOfPartialChunk, 0,
8989 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8991 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8992 + newSizeOfPartialChunk, 1);
8994 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8997 + in->variant.fileVariant.fileSize = newSize;
8999 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
9000 + } else {
9001 + /* newsSize > oldFileSize */
9002 + in->variant.fileVariant.fileSize = newSize;
9006 + /* Write a new object header.
9007 + * show we've shrunk the file, if need be
9008 + * Do this only if the file is not in the deleted directories.
9009 + */
9010 + if (in->parent &&
9011 + in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
9012 + in->parent->objectId != YAFFS_OBJECTID_DELETED)
9013 + yaffs_UpdateObjectHeader(in, NULL, 0,
9014 + (newSize < oldFileSize) ? 1 : 0, 0);
9016 + return YAFFS_OK;
9019 +loff_t yaffs_GetFileSize(yaffs_Object *obj)
9021 + obj = yaffs_GetEquivalentObject(obj);
9023 + switch (obj->variantType) {
9024 + case YAFFS_OBJECT_TYPE_FILE:
9025 + return obj->variant.fileVariant.fileSize;
9026 + case YAFFS_OBJECT_TYPE_SYMLINK:
9027 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
9028 + default:
9029 + return 0;
9035 +int yaffs_FlushFile(yaffs_Object *in, int updateTime)
9037 + int retVal;
9038 + if (in->dirty) {
9039 + yaffs_FlushFilesChunkCache(in);
9040 + if (updateTime) {
9041 +#ifdef CONFIG_YAFFS_WINCE
9042 + yfsd_WinFileTimeNow(in->win_mtime);
9043 +#else
9045 + in->yst_mtime = Y_CURRENT_TIME;
9047 +#endif
9050 + retVal = (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
9051 + 0) ? YAFFS_OK : YAFFS_FAIL;
9052 + } else {
9053 + retVal = YAFFS_OK;
9056 + return retVal;
9060 +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in)
9063 + /* First off, invalidate the file's data in the cache, without flushing. */
9064 + yaffs_InvalidateWholeChunkCache(in);
9066 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
9067 + /* Move to the unlinked directory so we have a record that it was deleted. */
9068 + yaffs_ChangeObjectName(in, in->myDev->deletedDir, _Y("deleted"), 0, 0);
9072 + yaffs_RemoveObjectFromDirectory(in);
9073 + yaffs_DeleteChunk(in->myDev, in->hdrChunk, 1, __LINE__);
9074 + in->hdrChunk = 0;
9076 + yaffs_FreeObject(in);
9077 + return YAFFS_OK;
9081 +/* yaffs_DeleteFile deletes the whole file data
9082 + * and the inode associated with the file.
9083 + * It does not delete the links associated with the file.
9084 + */
9085 +static int yaffs_UnlinkFileIfNeeded(yaffs_Object *in)
9088 + int retVal;
9089 + int immediateDeletion = 0;
9091 +#ifdef __KERNEL__
9092 + if (!in->myInode)
9093 + immediateDeletion = 1;
9094 +#else
9095 + if (in->inUse <= 0)
9096 + immediateDeletion = 1;
9097 +#endif
9099 + if (immediateDeletion) {
9100 + retVal =
9101 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
9102 + _Y("deleted"), 0, 0);
9103 + T(YAFFS_TRACE_TRACING,
9104 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
9105 + in->objectId));
9106 + in->deleted = 1;
9107 + in->myDev->nDeletedFiles++;
9108 + if (1 || in->myDev->isYaffs2)
9109 + yaffs_ResizeFile(in, 0);
9110 + yaffs_SoftDeleteFile(in);
9111 + } else {
9112 + retVal =
9113 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
9114 + _Y("unlinked"), 0, 0);
9118 + return retVal;
9121 +int yaffs_DeleteFile(yaffs_Object *in)
9123 + int retVal = YAFFS_OK;
9124 + int deleted = in->deleted;
9126 + yaffs_ResizeFile(in, 0);
9128 + if (in->nDataChunks > 0) {
9129 + /* Use soft deletion if there is data in the file.
9130 + * That won't be the case if it has been resized to zero.
9131 + */
9132 + if (!in->unlinked)
9133 + retVal = yaffs_UnlinkFileIfNeeded(in);
9135 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
9136 + in->deleted = 1;
9137 + deleted = 1;
9138 + in->myDev->nDeletedFiles++;
9139 + yaffs_SoftDeleteFile(in);
9141 + return deleted ? YAFFS_OK : YAFFS_FAIL;
9142 + } else {
9143 + /* The file has no data chunks so we toss it immediately */
9144 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
9145 + in->variant.fileVariant.top = NULL;
9146 + yaffs_DoGenericObjectDeletion(in);
9148 + return YAFFS_OK;
9152 +static int yaffs_DeleteDirectory(yaffs_Object *in)
9154 + /* First check that the directory is empty. */
9155 + if (ylist_empty(&in->variant.directoryVariant.children))
9156 + return yaffs_DoGenericObjectDeletion(in);
9158 + return YAFFS_FAIL;
9162 +static int yaffs_DeleteSymLink(yaffs_Object *in)
9164 + YFREE(in->variant.symLinkVariant.alias);
9166 + return yaffs_DoGenericObjectDeletion(in);
9169 +static int yaffs_DeleteHardLink(yaffs_Object *in)
9171 + /* remove this hardlink from the list assocaited with the equivalent
9172 + * object
9173 + */
9174 + ylist_del_init(&in->hardLinks);
9175 + return yaffs_DoGenericObjectDeletion(in);
9178 +int yaffs_DeleteObject(yaffs_Object *obj)
9180 +int retVal = -1;
9181 + switch (obj->variantType) {
9182 + case YAFFS_OBJECT_TYPE_FILE:
9183 + retVal = yaffs_DeleteFile(obj);
9184 + break;
9185 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9186 + return yaffs_DeleteDirectory(obj);
9187 + break;
9188 + case YAFFS_OBJECT_TYPE_SYMLINK:
9189 + retVal = yaffs_DeleteSymLink(obj);
9190 + break;
9191 + case YAFFS_OBJECT_TYPE_HARDLINK:
9192 + retVal = yaffs_DeleteHardLink(obj);
9193 + break;
9194 + case YAFFS_OBJECT_TYPE_SPECIAL:
9195 + retVal = yaffs_DoGenericObjectDeletion(obj);
9196 + break;
9197 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9198 + retVal = 0;
9199 + break; /* should not happen. */
9202 + return retVal;
9205 +static int yaffs_UnlinkWorker(yaffs_Object *obj)
9208 + int immediateDeletion = 0;
9210 +#ifdef __KERNEL__
9211 + if (!obj->myInode)
9212 + immediateDeletion = 1;
9213 +#else
9214 + if (obj->inUse <= 0)
9215 + immediateDeletion = 1;
9216 +#endif
9218 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
9219 + return yaffs_DeleteHardLink(obj);
9220 + } else if (!ylist_empty(&obj->hardLinks)) {
9221 + /* Curve ball: We're unlinking an object that has a hardlink.
9223 + * This problem arises because we are not strictly following
9224 + * The Linux link/inode model.
9226 + * We can't really delete the object.
9227 + * Instead, we do the following:
9228 + * - Select a hardlink.
9229 + * - Unhook it from the hard links
9230 + * - Unhook it from its parent directory (so that the rename can work)
9231 + * - Rename the object to the hardlink's name.
9232 + * - Delete the hardlink
9233 + */
9235 + yaffs_Object *hl;
9236 + int retVal;
9237 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
9239 + hl = ylist_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
9241 + ylist_del_init(&hl->hardLinks);
9242 + ylist_del_init(&hl->siblings);
9244 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
9246 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
9248 + if (retVal == YAFFS_OK)
9249 + retVal = yaffs_DoGenericObjectDeletion(hl);
9251 + return retVal;
9253 + } else if (immediateDeletion) {
9254 + switch (obj->variantType) {
9255 + case YAFFS_OBJECT_TYPE_FILE:
9256 + return yaffs_DeleteFile(obj);
9257 + break;
9258 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9259 + return yaffs_DeleteDirectory(obj);
9260 + break;
9261 + case YAFFS_OBJECT_TYPE_SYMLINK:
9262 + return yaffs_DeleteSymLink(obj);
9263 + break;
9264 + case YAFFS_OBJECT_TYPE_SPECIAL:
9265 + return yaffs_DoGenericObjectDeletion(obj);
9266 + break;
9267 + case YAFFS_OBJECT_TYPE_HARDLINK:
9268 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9269 + default:
9270 + return YAFFS_FAIL;
9272 + } else
9273 + return yaffs_ChangeObjectName(obj, obj->myDev->unlinkedDir,
9274 + _Y("unlinked"), 0, 0);
9278 +static int yaffs_UnlinkObject(yaffs_Object *obj)
9281 + if (obj && obj->unlinkAllowed)
9282 + return yaffs_UnlinkWorker(obj);
9284 + return YAFFS_FAIL;
9287 +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name)
9289 + yaffs_Object *obj;
9291 + obj = yaffs_FindObjectByName(dir, name);
9292 + return yaffs_UnlinkObject(obj);
9295 +/*----------------------- Initialisation Scanning ---------------------- */
9297 +static void yaffs_HandleShadowedObject(yaffs_Device *dev, int objId,
9298 + int backwardScanning)
9300 + yaffs_Object *obj;
9302 + if (!backwardScanning) {
9303 + /* Handle YAFFS1 forward scanning case
9304 + * For YAFFS1 we always do the deletion
9305 + */
9307 + } else {
9308 + /* Handle YAFFS2 case (backward scanning)
9309 + * If the shadowed object exists then ignore.
9310 + */
9311 + if (yaffs_FindObjectByNumber(dev, objId))
9312 + return;
9315 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
9316 + * We put it in unlinked dir to be cleaned up after the scanning
9317 + */
9318 + obj =
9319 + yaffs_FindOrCreateObjectByNumber(dev, objId,
9320 + YAFFS_OBJECT_TYPE_FILE);
9321 + if (!obj)
9322 + return;
9323 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
9324 + obj->variant.fileVariant.shrinkSize = 0;
9325 + obj->valid = 1; /* So that we don't read any other info for this file */
9329 +typedef struct {
9330 + int seq;
9331 + int block;
9332 +} yaffs_BlockIndex;
9335 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
9337 + yaffs_Object *hl;
9338 + yaffs_Object *in;
9340 + while (hardList) {
9341 + hl = hardList;
9342 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
9344 + in = yaffs_FindObjectByNumber(dev,
9345 + hl->variant.hardLinkVariant.
9346 + equivalentObjectId);
9348 + if (in) {
9349 + /* Add the hardlink pointers */
9350 + hl->variant.hardLinkVariant.equivalentObject = in;
9351 + ylist_add(&hl->hardLinks, &in->hardLinks);
9352 + } else {
9353 + /* Todo Need to report/handle this better.
9354 + * Got a problem... hardlink to a non-existant object
9355 + */
9356 + hl->variant.hardLinkVariant.equivalentObject = NULL;
9357 + YINIT_LIST_HEAD(&hl->hardLinks);
9367 +static int ybicmp(const void *a, const void *b)
9369 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
9370 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
9371 + register int ablock = ((yaffs_BlockIndex *)a)->block;
9372 + register int bblock = ((yaffs_BlockIndex *)b)->block;
9373 + if (aseq == bseq)
9374 + return ablock - bblock;
9375 + else
9376 + return aseq - bseq;
9380 +struct yaffs_ShadowFixerStruct {
9381 + int objectId;
9382 + int shadowedId;
9383 + struct yaffs_ShadowFixerStruct *next;
9387 +static void yaffs_StripDeletedObjects(yaffs_Device *dev)
9389 + /*
9390 + * Sort out state of unlinked and deleted objects after scanning.
9391 + */
9392 + struct ylist_head *i;
9393 + struct ylist_head *n;
9394 + yaffs_Object *l;
9396 + /* Soft delete all the unlinked files */
9397 + ylist_for_each_safe(i, n,
9398 + &dev->unlinkedDir->variant.directoryVariant.children) {
9399 + if (i) {
9400 + l = ylist_entry(i, yaffs_Object, siblings);
9401 + yaffs_DeleteObject(l);
9405 + ylist_for_each_safe(i, n,
9406 + &dev->deletedDir->variant.directoryVariant.children) {
9407 + if (i) {
9408 + l = ylist_entry(i, yaffs_Object, siblings);
9409 + yaffs_DeleteObject(l);
9415 +static int yaffs_Scan(yaffs_Device *dev)
9417 + yaffs_ExtendedTags tags;
9418 + int blk;
9419 + int blockIterator;
9420 + int startIterator;
9421 + int endIterator;
9422 + int result;
9424 + int chunk;
9425 + int c;
9426 + int deleted;
9427 + yaffs_BlockState state;
9428 + yaffs_Object *hardList = NULL;
9429 + yaffs_BlockInfo *bi;
9430 + __u32 sequenceNumber;
9431 + yaffs_ObjectHeader *oh;
9432 + yaffs_Object *in;
9433 + yaffs_Object *parent;
9435 + int alloc_failed = 0;
9437 + struct yaffs_ShadowFixerStruct *shadowFixerList = NULL;
9440 + __u8 *chunkData;
9444 + T(YAFFS_TRACE_SCAN,
9445 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9446 + dev->internalStartBlock, dev->internalEndBlock));
9448 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9450 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9452 + /* Scan all the blocks to determine their state */
9453 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9454 + bi = yaffs_GetBlockInfo(dev, blk);
9455 + yaffs_ClearChunkBits(dev, blk);
9456 + bi->pagesInUse = 0;
9457 + bi->softDeletions = 0;
9459 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9461 + bi->blockState = state;
9462 + bi->sequenceNumber = sequenceNumber;
9464 + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
9465 + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
9467 + T(YAFFS_TRACE_SCAN_DEBUG,
9468 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9469 + state, sequenceNumber));
9471 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9472 + T(YAFFS_TRACE_BAD_BLOCKS,
9473 + (TSTR("block %d is bad" TENDSTR), blk));
9474 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9475 + T(YAFFS_TRACE_SCAN_DEBUG,
9476 + (TSTR("Block empty " TENDSTR)));
9477 + dev->nErasedBlocks++;
9478 + dev->nFreeChunks += dev->nChunksPerBlock;
9482 + startIterator = dev->internalStartBlock;
9483 + endIterator = dev->internalEndBlock;
9485 + /* For each block.... */
9486 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9487 + blockIterator++) {
9489 + YYIELD();
9491 + YYIELD();
9493 + blk = blockIterator;
9495 + bi = yaffs_GetBlockInfo(dev, blk);
9496 + state = bi->blockState;
9498 + deleted = 0;
9500 + /* For each chunk in each block that needs scanning....*/
9501 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9502 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9503 + /* Read the tags and decide what to do */
9504 + chunk = blk * dev->nChunksPerBlock + c;
9506 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9507 + &tags);
9509 + /* Let's have a good look at this chunk... */
9511 + if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED || tags.chunkDeleted) {
9512 + /* YAFFS1 only...
9513 + * A deleted chunk
9514 + */
9515 + deleted++;
9516 + dev->nFreeChunks++;
9517 + /*T((" %d %d deleted\n",blk,c)); */
9518 + } else if (!tags.chunkUsed) {
9519 + /* An unassigned chunk in the block
9520 + * This means that either the block is empty or
9521 + * this is the one being allocated from
9522 + */
9524 + if (c == 0) {
9525 + /* We're looking at the first chunk in the block so the block is unused */
9526 + state = YAFFS_BLOCK_STATE_EMPTY;
9527 + dev->nErasedBlocks++;
9528 + } else {
9529 + /* this is the block being allocated from */
9530 + T(YAFFS_TRACE_SCAN,
9531 + (TSTR
9532 + (" Allocating from %d %d" TENDSTR),
9533 + blk, c));
9534 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9535 + dev->allocationBlock = blk;
9536 + dev->allocationPage = c;
9537 + dev->allocationBlockFinder = blk;
9538 + /* Set it to here to encourage the allocator to go forth from here. */
9542 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9543 + } else if (tags.chunkId > 0) {
9544 + /* chunkId > 0 so it is a data chunk... */
9545 + unsigned int endpos;
9547 + yaffs_SetChunkBit(dev, blk, c);
9548 + bi->pagesInUse++;
9550 + in = yaffs_FindOrCreateObjectByNumber(dev,
9551 + tags.
9552 + objectId,
9553 + YAFFS_OBJECT_TYPE_FILE);
9554 + /* PutChunkIntoFile checks for a clash (two data chunks with
9555 + * the same chunkId).
9556 + */
9558 + if (!in)
9559 + alloc_failed = 1;
9561 + if (in) {
9562 + if (!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk, 1))
9563 + alloc_failed = 1;
9566 + endpos =
9567 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9568 + tags.byteCount;
9569 + if (in &&
9570 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9571 + && in->variant.fileVariant.scannedFileSize <
9572 + endpos) {
9573 + in->variant.fileVariant.
9574 + scannedFileSize = endpos;
9575 + if (!dev->useHeaderFileSize) {
9576 + in->variant.fileVariant.
9577 + fileSize =
9578 + in->variant.fileVariant.
9579 + scannedFileSize;
9583 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9584 + } else {
9585 + /* chunkId == 0, so it is an ObjectHeader.
9586 + * Thus, we read in the object header and make the object
9587 + */
9588 + yaffs_SetChunkBit(dev, blk, c);
9589 + bi->pagesInUse++;
9591 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9592 + chunkData,
9593 + NULL);
9595 + oh = (yaffs_ObjectHeader *) chunkData;
9597 + in = yaffs_FindObjectByNumber(dev,
9598 + tags.objectId);
9599 + if (in && in->variantType != oh->type) {
9600 + /* This should not happen, but somehow
9601 + * Wev'e ended up with an objectId that has been reused but not yet
9602 + * deleted, and worse still it has changed type. Delete the old object.
9603 + */
9605 + yaffs_DeleteObject(in);
9607 + in = 0;
9610 + in = yaffs_FindOrCreateObjectByNumber(dev,
9611 + tags.
9612 + objectId,
9613 + oh->type);
9615 + if (!in)
9616 + alloc_failed = 1;
9618 + if (in && oh->shadowsObject > 0) {
9620 + struct yaffs_ShadowFixerStruct *fixer;
9621 + fixer = YMALLOC(sizeof(struct yaffs_ShadowFixerStruct));
9622 + if (fixer) {
9623 + fixer->next = shadowFixerList;
9624 + shadowFixerList = fixer;
9625 + fixer->objectId = tags.objectId;
9626 + fixer->shadowedId = oh->shadowsObject;
9631 + if (in && in->valid) {
9632 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9634 + unsigned existingSerial = in->serial;
9635 + unsigned newSerial = tags.serialNumber;
9637 + if (((existingSerial + 1) & 3) == newSerial) {
9638 + /* Use new one - destroy the exisiting one */
9639 + yaffs_DeleteChunk(dev,
9640 + in->hdrChunk,
9641 + 1, __LINE__);
9642 + in->valid = 0;
9643 + } else {
9644 + /* Use existing - destroy this one. */
9645 + yaffs_DeleteChunk(dev, chunk, 1,
9646 + __LINE__);
9650 + if (in && !in->valid &&
9651 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9652 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9653 + /* We only load some info, don't fiddle with directory structure */
9654 + in->valid = 1;
9655 + in->variantType = oh->type;
9657 + in->yst_mode = oh->yst_mode;
9658 +#ifdef CONFIG_YAFFS_WINCE
9659 + in->win_atime[0] = oh->win_atime[0];
9660 + in->win_ctime[0] = oh->win_ctime[0];
9661 + in->win_mtime[0] = oh->win_mtime[0];
9662 + in->win_atime[1] = oh->win_atime[1];
9663 + in->win_ctime[1] = oh->win_ctime[1];
9664 + in->win_mtime[1] = oh->win_mtime[1];
9665 +#else
9666 + in->yst_uid = oh->yst_uid;
9667 + in->yst_gid = oh->yst_gid;
9668 + in->yst_atime = oh->yst_atime;
9669 + in->yst_mtime = oh->yst_mtime;
9670 + in->yst_ctime = oh->yst_ctime;
9671 + in->yst_rdev = oh->yst_rdev;
9672 +#endif
9673 + in->hdrChunk = chunk;
9674 + in->serial = tags.serialNumber;
9676 + } else if (in && !in->valid) {
9677 + /* we need to load this info */
9679 + in->valid = 1;
9680 + in->variantType = oh->type;
9682 + in->yst_mode = oh->yst_mode;
9683 +#ifdef CONFIG_YAFFS_WINCE
9684 + in->win_atime[0] = oh->win_atime[0];
9685 + in->win_ctime[0] = oh->win_ctime[0];
9686 + in->win_mtime[0] = oh->win_mtime[0];
9687 + in->win_atime[1] = oh->win_atime[1];
9688 + in->win_ctime[1] = oh->win_ctime[1];
9689 + in->win_mtime[1] = oh->win_mtime[1];
9690 +#else
9691 + in->yst_uid = oh->yst_uid;
9692 + in->yst_gid = oh->yst_gid;
9693 + in->yst_atime = oh->yst_atime;
9694 + in->yst_mtime = oh->yst_mtime;
9695 + in->yst_ctime = oh->yst_ctime;
9696 + in->yst_rdev = oh->yst_rdev;
9697 +#endif
9698 + in->hdrChunk = chunk;
9699 + in->serial = tags.serialNumber;
9701 + yaffs_SetObjectName(in, oh->name);
9702 + in->dirty = 0;
9704 + /* directory stuff...
9705 + * hook up to parent
9706 + */
9708 + parent =
9709 + yaffs_FindOrCreateObjectByNumber
9710 + (dev, oh->parentObjectId,
9711 + YAFFS_OBJECT_TYPE_DIRECTORY);
9712 + if (!parent)
9713 + alloc_failed = 1;
9714 + if (parent && parent->variantType ==
9715 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9716 + /* Set up as a directory */
9717 + parent->variantType =
9718 + YAFFS_OBJECT_TYPE_DIRECTORY;
9719 + YINIT_LIST_HEAD(&parent->variant.
9720 + directoryVariant.
9721 + children);
9722 + } else if (!parent || parent->variantType !=
9723 + YAFFS_OBJECT_TYPE_DIRECTORY) {
9724 + /* Hoosterman, another problem....
9725 + * We're trying to use a non-directory as a directory
9726 + */
9728 + T(YAFFS_TRACE_ERROR,
9729 + (TSTR
9730 + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
9731 + TENDSTR)));
9732 + parent = dev->lostNFoundDir;
9735 + yaffs_AddObjectToDirectory(parent, in);
9737 + if (0 && (parent == dev->deletedDir ||
9738 + parent == dev->unlinkedDir)) {
9739 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9740 + dev->nDeletedFiles++;
9742 + /* Note re hardlinks.
9743 + * Since we might scan a hardlink before its equivalent object is scanned
9744 + * we put them all in a list.
9745 + * After scanning is complete, we should have all the objects, so we run through this
9746 + * list and fix up all the chains.
9747 + */
9749 + switch (in->variantType) {
9750 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9751 + /* Todo got a problem */
9752 + break;
9753 + case YAFFS_OBJECT_TYPE_FILE:
9754 + if (dev->useHeaderFileSize)
9756 + in->variant.fileVariant.
9757 + fileSize =
9758 + oh->fileSize;
9760 + break;
9761 + case YAFFS_OBJECT_TYPE_HARDLINK:
9762 + in->variant.hardLinkVariant.
9763 + equivalentObjectId =
9764 + oh->equivalentObjectId;
9765 + in->hardLinks.next =
9766 + (struct ylist_head *)
9767 + hardList;
9768 + hardList = in;
9769 + break;
9770 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9771 + /* Do nothing */
9772 + break;
9773 + case YAFFS_OBJECT_TYPE_SPECIAL:
9774 + /* Do nothing */
9775 + break;
9776 + case YAFFS_OBJECT_TYPE_SYMLINK:
9777 + in->variant.symLinkVariant.alias =
9778 + yaffs_CloneString(oh->alias);
9779 + if (!in->variant.symLinkVariant.alias)
9780 + alloc_failed = 1;
9781 + break;
9785 + if (parent == dev->deletedDir) {
9786 + yaffs_DestroyObject(in);
9787 + bi->hasShrinkHeader = 1;
9794 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9795 + /* If we got this far while scanning, then the block is fully allocated.*/
9796 + state = YAFFS_BLOCK_STATE_FULL;
9799 + bi->blockState = state;
9801 + /* Now let's see if it was dirty */
9802 + if (bi->pagesInUse == 0 &&
9803 + !bi->hasShrinkHeader &&
9804 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9805 + yaffs_BlockBecameDirty(dev, blk);
9811 + /* Ok, we've done all the scanning.
9812 + * Fix up the hard link chains.
9813 + * We should now have scanned all the objects, now it's time to add these
9814 + * hardlinks.
9815 + */
9817 + yaffs_HardlinkFixup(dev, hardList);
9819 + /* Fix up any shadowed objects */
9821 + struct yaffs_ShadowFixerStruct *fixer;
9822 + yaffs_Object *obj;
9824 + while (shadowFixerList) {
9825 + fixer = shadowFixerList;
9826 + shadowFixerList = fixer->next;
9827 + /* Complete the rename transaction by deleting the shadowed object
9828 + * then setting the object header to unshadowed.
9829 + */
9830 + obj = yaffs_FindObjectByNumber(dev, fixer->shadowedId);
9831 + if (obj)
9832 + yaffs_DeleteObject(obj);
9834 + obj = yaffs_FindObjectByNumber(dev, fixer->objectId);
9836 + if (obj)
9837 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
9839 + YFREE(fixer);
9843 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9845 + if (alloc_failed)
9846 + return YAFFS_FAIL;
9848 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9851 + return YAFFS_OK;
9854 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9856 + __u8 *chunkData;
9857 + yaffs_ObjectHeader *oh;
9858 + yaffs_Device *dev;
9859 + yaffs_ExtendedTags tags;
9860 + int result;
9861 + int alloc_failed = 0;
9863 + if (!in)
9864 + return;
9866 + dev = in->myDev;
9868 +#if 0
9869 + T(YAFFS_TRACE_SCAN, (TSTR("details for object %d %s loaded" TENDSTR),
9870 + in->objectId,
9871 + in->lazyLoaded ? "not yet" : "already"));
9872 +#endif
9874 + if (in->lazyLoaded && in->hdrChunk > 0) {
9875 + in->lazyLoaded = 0;
9876 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9878 + result = yaffs_ReadChunkWithTagsFromNAND(dev, in->hdrChunk, chunkData, &tags);
9879 + oh = (yaffs_ObjectHeader *) chunkData;
9881 + in->yst_mode = oh->yst_mode;
9882 +#ifdef CONFIG_YAFFS_WINCE
9883 + in->win_atime[0] = oh->win_atime[0];
9884 + in->win_ctime[0] = oh->win_ctime[0];
9885 + in->win_mtime[0] = oh->win_mtime[0];
9886 + in->win_atime[1] = oh->win_atime[1];
9887 + in->win_ctime[1] = oh->win_ctime[1];
9888 + in->win_mtime[1] = oh->win_mtime[1];
9889 +#else
9890 + in->yst_uid = oh->yst_uid;
9891 + in->yst_gid = oh->yst_gid;
9892 + in->yst_atime = oh->yst_atime;
9893 + in->yst_mtime = oh->yst_mtime;
9894 + in->yst_ctime = oh->yst_ctime;
9895 + in->yst_rdev = oh->yst_rdev;
9897 +#endif
9898 + yaffs_SetObjectName(in, oh->name);
9900 + if (in->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
9901 + in->variant.symLinkVariant.alias =
9902 + yaffs_CloneString(oh->alias);
9903 + if (!in->variant.symLinkVariant.alias)
9904 + alloc_failed = 1; /* Not returned to caller */
9907 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9911 +static int yaffs_ScanBackwards(yaffs_Device *dev)
9913 + yaffs_ExtendedTags tags;
9914 + int blk;
9915 + int blockIterator;
9916 + int startIterator;
9917 + int endIterator;
9918 + int nBlocksToScan = 0;
9920 + int chunk;
9921 + int result;
9922 + int c;
9923 + int deleted;
9924 + yaffs_BlockState state;
9925 + yaffs_Object *hardList = NULL;
9926 + yaffs_BlockInfo *bi;
9927 + __u32 sequenceNumber;
9928 + yaffs_ObjectHeader *oh;
9929 + yaffs_Object *in;
9930 + yaffs_Object *parent;
9931 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9932 + int itsUnlinked;
9933 + __u8 *chunkData;
9935 + int fileSize;
9936 + int isShrink;
9937 + int foundChunksInBlock;
9938 + int equivalentObjectId;
9939 + int alloc_failed = 0;
9942 + yaffs_BlockIndex *blockIndex = NULL;
9943 + int altBlockIndex = 0;
9945 + if (!dev->isYaffs2) {
9946 + T(YAFFS_TRACE_SCAN,
9947 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9948 + return YAFFS_FAIL;
9951 + T(YAFFS_TRACE_SCAN,
9952 + (TSTR
9953 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9954 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9957 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9959 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9961 + if (!blockIndex) {
9962 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9963 + altBlockIndex = 1;
9966 + if (!blockIndex) {
9967 + T(YAFFS_TRACE_SCAN,
9968 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9969 + return YAFFS_FAIL;
9972 + dev->blocksInCheckpoint = 0;
9974 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9976 + /* Scan all the blocks to determine their state */
9977 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9978 + bi = yaffs_GetBlockInfo(dev, blk);
9979 + yaffs_ClearChunkBits(dev, blk);
9980 + bi->pagesInUse = 0;
9981 + bi->softDeletions = 0;
9983 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9985 + bi->blockState = state;
9986 + bi->sequenceNumber = sequenceNumber;
9988 + if (bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9989 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9990 + if (bi->sequenceNumber == YAFFS_SEQUENCE_BAD_BLOCK)
9991 + bi->blockState = state = YAFFS_BLOCK_STATE_DEAD;
9993 + T(YAFFS_TRACE_SCAN_DEBUG,
9994 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9995 + state, sequenceNumber));
9998 + if (state == YAFFS_BLOCK_STATE_CHECKPOINT) {
9999 + dev->blocksInCheckpoint++;
10001 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
10002 + T(YAFFS_TRACE_BAD_BLOCKS,
10003 + (TSTR("block %d is bad" TENDSTR), blk));
10004 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
10005 + T(YAFFS_TRACE_SCAN_DEBUG,
10006 + (TSTR("Block empty " TENDSTR)));
10007 + dev->nErasedBlocks++;
10008 + dev->nFreeChunks += dev->nChunksPerBlock;
10009 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10011 + /* Determine the highest sequence number */
10012 + if (sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
10013 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
10015 + blockIndex[nBlocksToScan].seq = sequenceNumber;
10016 + blockIndex[nBlocksToScan].block = blk;
10018 + nBlocksToScan++;
10020 + if (sequenceNumber >= dev->sequenceNumber)
10021 + dev->sequenceNumber = sequenceNumber;
10022 + } else {
10023 + /* TODO: Nasty sequence number! */
10024 + T(YAFFS_TRACE_SCAN,
10025 + (TSTR
10026 + ("Block scanning block %d has bad sequence number %d"
10027 + TENDSTR), blk, sequenceNumber));
10033 + T(YAFFS_TRACE_SCAN,
10034 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
10038 + YYIELD();
10040 + /* Sort the blocks */
10041 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
10043 + /* Use qsort now. */
10044 + yaffs_qsort(blockIndex, nBlocksToScan, sizeof(yaffs_BlockIndex), ybicmp);
10046 +#else
10048 + /* Dungy old bubble sort... */
10050 + yaffs_BlockIndex temp;
10051 + int i;
10052 + int j;
10054 + for (i = 0; i < nBlocksToScan; i++)
10055 + for (j = i + 1; j < nBlocksToScan; j++)
10056 + if (blockIndex[i].seq > blockIndex[j].seq) {
10057 + temp = blockIndex[j];
10058 + blockIndex[j] = blockIndex[i];
10059 + blockIndex[i] = temp;
10062 +#endif
10064 + YYIELD();
10066 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
10068 + /* Now scan the blocks looking at the data. */
10069 + startIterator = 0;
10070 + endIterator = nBlocksToScan - 1;
10071 + T(YAFFS_TRACE_SCAN_DEBUG,
10072 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
10074 + /* For each block.... backwards */
10075 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
10076 + blockIterator--) {
10077 + /* Cooperative multitasking! This loop can run for so
10078 + long that watchdog timers expire. */
10079 + YYIELD();
10081 + /* get the block to scan in the correct order */
10082 + blk = blockIndex[blockIterator].block;
10084 + bi = yaffs_GetBlockInfo(dev, blk);
10087 + state = bi->blockState;
10089 + deleted = 0;
10091 + /* For each chunk in each block that needs scanning.... */
10092 + foundChunksInBlock = 0;
10093 + for (c = dev->nChunksPerBlock - 1;
10094 + !alloc_failed && c >= 0 &&
10095 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
10096 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
10097 + /* Scan backwards...
10098 + * Read the tags and decide what to do
10099 + */
10101 + chunk = blk * dev->nChunksPerBlock + c;
10103 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
10104 + &tags);
10106 + /* Let's have a good look at this chunk... */
10108 + if (!tags.chunkUsed) {
10109 + /* An unassigned chunk in the block.
10110 + * If there are used chunks after this one, then
10111 + * it is a chunk that was skipped due to failing the erased
10112 + * check. Just skip it so that it can be deleted.
10113 + * But, more typically, We get here when this is an unallocated
10114 + * chunk and his means that either the block is empty or
10115 + * this is the one being allocated from
10116 + */
10118 + if (foundChunksInBlock) {
10119 + /* This is a chunk that was skipped due to failing the erased check */
10120 + } else if (c == 0) {
10121 + /* We're looking at the first chunk in the block so the block is unused */
10122 + state = YAFFS_BLOCK_STATE_EMPTY;
10123 + dev->nErasedBlocks++;
10124 + } else {
10125 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
10126 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
10127 + if (dev->sequenceNumber == bi->sequenceNumber) {
10128 + /* this is the block being allocated from */
10130 + T(YAFFS_TRACE_SCAN,
10131 + (TSTR
10132 + (" Allocating from %d %d"
10133 + TENDSTR), blk, c));
10135 + state = YAFFS_BLOCK_STATE_ALLOCATING;
10136 + dev->allocationBlock = blk;
10137 + dev->allocationPage = c;
10138 + dev->allocationBlockFinder = blk;
10139 + } else {
10140 + /* This is a partially written block that is not
10141 + * the current allocation block. This block must have
10142 + * had a write failure, so set up for retirement.
10143 + */
10145 + /* bi->needsRetiring = 1; ??? TODO */
10146 + bi->gcPrioritise = 1;
10148 + T(YAFFS_TRACE_ALWAYS,
10149 + (TSTR("Partially written block %d detected" TENDSTR),
10150 + blk));
10155 + dev->nFreeChunks++;
10157 + } else if (tags.eccResult == YAFFS_ECC_RESULT_UNFIXED) {
10158 + T(YAFFS_TRACE_SCAN,
10159 + (TSTR(" Unfixed ECC in chunk(%d:%d), chunk ignored"TENDSTR),
10160 + blk, c));
10162 + dev->nFreeChunks++;
10164 + } else if (tags.chunkId > 0) {
10165 + /* chunkId > 0 so it is a data chunk... */
10166 + unsigned int endpos;
10167 + __u32 chunkBase =
10168 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
10170 + foundChunksInBlock = 1;
10173 + yaffs_SetChunkBit(dev, blk, c);
10174 + bi->pagesInUse++;
10176 + in = yaffs_FindOrCreateObjectByNumber(dev,
10177 + tags.
10178 + objectId,
10179 + YAFFS_OBJECT_TYPE_FILE);
10180 + if (!in) {
10181 + /* Out of memory */
10182 + alloc_failed = 1;
10185 + if (in &&
10186 + in->variantType == YAFFS_OBJECT_TYPE_FILE
10187 + && chunkBase <
10188 + in->variant.fileVariant.shrinkSize) {
10189 + /* This has not been invalidated by a resize */
10190 + if (!yaffs_PutChunkIntoFile(in, tags.chunkId,
10191 + chunk, -1)) {
10192 + alloc_failed = 1;
10195 + /* File size is calculated by looking at the data chunks if we have not
10196 + * seen an object header yet. Stop this practice once we find an object header.
10197 + */
10198 + endpos =
10199 + (tags.chunkId -
10200 + 1) * dev->nDataBytesPerChunk +
10201 + tags.byteCount;
10203 + if (!in->valid && /* have not got an object header yet */
10204 + in->variant.fileVariant.
10205 + scannedFileSize < endpos) {
10206 + in->variant.fileVariant.
10207 + scannedFileSize = endpos;
10208 + in->variant.fileVariant.
10209 + fileSize =
10210 + in->variant.fileVariant.
10211 + scannedFileSize;
10214 + } else if (in) {
10215 + /* This chunk has been invalidated by a resize, so delete */
10216 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
10219 + } else {
10220 + /* chunkId == 0, so it is an ObjectHeader.
10221 + * Thus, we read in the object header and make the object
10222 + */
10223 + foundChunksInBlock = 1;
10225 + yaffs_SetChunkBit(dev, blk, c);
10226 + bi->pagesInUse++;
10228 + oh = NULL;
10229 + in = NULL;
10231 + if (tags.extraHeaderInfoAvailable) {
10232 + in = yaffs_FindOrCreateObjectByNumber
10233 + (dev, tags.objectId,
10234 + tags.extraObjectType);
10235 + if (!in)
10236 + alloc_failed = 1;
10239 + if (!in ||
10240 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
10241 + !in->valid ||
10242 +#endif
10243 + tags.extraShadows ||
10244 + (!in->valid &&
10245 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
10246 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))) {
10248 + /* If we don't have valid info then we need to read the chunk
10249 + * TODO In future we can probably defer reading the chunk and
10250 + * living with invalid data until needed.
10251 + */
10253 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
10254 + chunk,
10255 + chunkData,
10256 + NULL);
10258 + oh = (yaffs_ObjectHeader *) chunkData;
10260 + if (dev->inbandTags) {
10261 + /* Fix up the header if they got corrupted by inband tags */
10262 + oh->shadowsObject = oh->inbandShadowsObject;
10263 + oh->isShrink = oh->inbandIsShrink;
10266 + if (!in) {
10267 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
10268 + if (!in)
10269 + alloc_failed = 1;
10274 + if (!in) {
10275 + /* TODO Hoosterman we have a problem! */
10276 + T(YAFFS_TRACE_ERROR,
10277 + (TSTR
10278 + ("yaffs tragedy: Could not make object for object %d at chunk %d during scan"
10279 + TENDSTR), tags.objectId, chunk));
10280 + continue;
10283 + if (in->valid) {
10284 + /* We have already filled this one.
10285 + * We have a duplicate that will be discarded, but
10286 + * we first have to suck out resize info if it is a file.
10287 + */
10289 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
10290 + ((oh &&
10291 + oh->type == YAFFS_OBJECT_TYPE_FILE) ||
10292 + (tags.extraHeaderInfoAvailable &&
10293 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))) {
10294 + __u32 thisSize =
10295 + (oh) ? oh->fileSize : tags.
10296 + extraFileLength;
10297 + __u32 parentObjectId =
10298 + (oh) ? oh->
10299 + parentObjectId : tags.
10300 + extraParentObjectId;
10303 + isShrink =
10304 + (oh) ? oh->isShrink : tags.
10305 + extraIsShrinkHeader;
10307 + /* If it is deleted (unlinked at start also means deleted)
10308 + * we treat the file size as being zeroed at this point.
10309 + */
10310 + if (parentObjectId ==
10311 + YAFFS_OBJECTID_DELETED
10312 + || parentObjectId ==
10313 + YAFFS_OBJECTID_UNLINKED) {
10314 + thisSize = 0;
10315 + isShrink = 1;
10318 + if (isShrink &&
10319 + in->variant.fileVariant.
10320 + shrinkSize > thisSize) {
10321 + in->variant.fileVariant.
10322 + shrinkSize =
10323 + thisSize;
10326 + if (isShrink)
10327 + bi->hasShrinkHeader = 1;
10330 + /* Use existing - destroy this one. */
10331 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
10335 + if (!in->valid && in->variantType !=
10336 + (oh ? oh->type : tags.extraObjectType))
10337 + T(YAFFS_TRACE_ERROR, (
10338 + TSTR("yaffs tragedy: Bad object type, "
10339 + TCONT("%d != %d, for object %d at chunk ")
10340 + TCONT("%d during scan")
10341 + TENDSTR), oh ?
10342 + oh->type : tags.extraObjectType,
10343 + in->variantType, tags.objectId,
10344 + chunk));
10346 + if (!in->valid &&
10347 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
10348 + tags.objectId ==
10349 + YAFFS_OBJECTID_LOSTNFOUND)) {
10350 + /* We only load some info, don't fiddle with directory structure */
10351 + in->valid = 1;
10353 + if (oh) {
10354 + in->variantType = oh->type;
10356 + in->yst_mode = oh->yst_mode;
10357 +#ifdef CONFIG_YAFFS_WINCE
10358 + in->win_atime[0] = oh->win_atime[0];
10359 + in->win_ctime[0] = oh->win_ctime[0];
10360 + in->win_mtime[0] = oh->win_mtime[0];
10361 + in->win_atime[1] = oh->win_atime[1];
10362 + in->win_ctime[1] = oh->win_ctime[1];
10363 + in->win_mtime[1] = oh->win_mtime[1];
10364 +#else
10365 + in->yst_uid = oh->yst_uid;
10366 + in->yst_gid = oh->yst_gid;
10367 + in->yst_atime = oh->yst_atime;
10368 + in->yst_mtime = oh->yst_mtime;
10369 + in->yst_ctime = oh->yst_ctime;
10370 + in->yst_rdev = oh->yst_rdev;
10372 +#endif
10373 + } else {
10374 + in->variantType = tags.extraObjectType;
10375 + in->lazyLoaded = 1;
10378 + in->hdrChunk = chunk;
10380 + } else if (!in->valid) {
10381 + /* we need to load this info */
10383 + in->valid = 1;
10384 + in->hdrChunk = chunk;
10386 + if (oh) {
10387 + in->variantType = oh->type;
10389 + in->yst_mode = oh->yst_mode;
10390 +#ifdef CONFIG_YAFFS_WINCE
10391 + in->win_atime[0] = oh->win_atime[0];
10392 + in->win_ctime[0] = oh->win_ctime[0];
10393 + in->win_mtime[0] = oh->win_mtime[0];
10394 + in->win_atime[1] = oh->win_atime[1];
10395 + in->win_ctime[1] = oh->win_ctime[1];
10396 + in->win_mtime[1] = oh->win_mtime[1];
10397 +#else
10398 + in->yst_uid = oh->yst_uid;
10399 + in->yst_gid = oh->yst_gid;
10400 + in->yst_atime = oh->yst_atime;
10401 + in->yst_mtime = oh->yst_mtime;
10402 + in->yst_ctime = oh->yst_ctime;
10403 + in->yst_rdev = oh->yst_rdev;
10404 +#endif
10406 + if (oh->shadowsObject > 0)
10407 + yaffs_HandleShadowedObject(dev,
10408 + oh->
10409 + shadowsObject,
10410 + 1);
10413 + yaffs_SetObjectName(in, oh->name);
10414 + parent =
10415 + yaffs_FindOrCreateObjectByNumber
10416 + (dev, oh->parentObjectId,
10417 + YAFFS_OBJECT_TYPE_DIRECTORY);
10419 + fileSize = oh->fileSize;
10420 + isShrink = oh->isShrink;
10421 + equivalentObjectId = oh->equivalentObjectId;
10423 + } else {
10424 + in->variantType = tags.extraObjectType;
10425 + parent =
10426 + yaffs_FindOrCreateObjectByNumber
10427 + (dev, tags.extraParentObjectId,
10428 + YAFFS_OBJECT_TYPE_DIRECTORY);
10429 + fileSize = tags.extraFileLength;
10430 + isShrink = tags.extraIsShrinkHeader;
10431 + equivalentObjectId = tags.extraEquivalentObjectId;
10432 + in->lazyLoaded = 1;
10435 + in->dirty = 0;
10437 + if (!parent)
10438 + alloc_failed = 1;
10440 + /* directory stuff...
10441 + * hook up to parent
10442 + */
10444 + if (parent && parent->variantType ==
10445 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10446 + /* Set up as a directory */
10447 + parent->variantType =
10448 + YAFFS_OBJECT_TYPE_DIRECTORY;
10449 + YINIT_LIST_HEAD(&parent->variant.
10450 + directoryVariant.
10451 + children);
10452 + } else if (!parent || parent->variantType !=
10453 + YAFFS_OBJECT_TYPE_DIRECTORY) {
10454 + /* Hoosterman, another problem....
10455 + * We're trying to use a non-directory as a directory
10456 + */
10458 + T(YAFFS_TRACE_ERROR,
10459 + (TSTR
10460 + ("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
10461 + TENDSTR)));
10462 + parent = dev->lostNFoundDir;
10465 + yaffs_AddObjectToDirectory(parent, in);
10467 + itsUnlinked = (parent == dev->deletedDir) ||
10468 + (parent == dev->unlinkedDir);
10470 + if (isShrink) {
10471 + /* Mark the block as having a shrinkHeader */
10472 + bi->hasShrinkHeader = 1;
10475 + /* Note re hardlinks.
10476 + * Since we might scan a hardlink before its equivalent object is scanned
10477 + * we put them all in a list.
10478 + * After scanning is complete, we should have all the objects, so we run
10479 + * through this list and fix up all the chains.
10480 + */
10482 + switch (in->variantType) {
10483 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10484 + /* Todo got a problem */
10485 + break;
10486 + case YAFFS_OBJECT_TYPE_FILE:
10488 + if (in->variant.fileVariant.
10489 + scannedFileSize < fileSize) {
10490 + /* This covers the case where the file size is greater
10491 + * than where the data is
10492 + * This will happen if the file is resized to be larger
10493 + * than its current data extents.
10494 + */
10495 + in->variant.fileVariant.fileSize = fileSize;
10496 + in->variant.fileVariant.scannedFileSize =
10497 + in->variant.fileVariant.fileSize;
10500 + if (isShrink &&
10501 + in->variant.fileVariant.shrinkSize > fileSize) {
10502 + in->variant.fileVariant.shrinkSize = fileSize;
10505 + break;
10506 + case YAFFS_OBJECT_TYPE_HARDLINK:
10507 + if (!itsUnlinked) {
10508 + in->variant.hardLinkVariant.equivalentObjectId =
10509 + equivalentObjectId;
10510 + in->hardLinks.next =
10511 + (struct ylist_head *) hardList;
10512 + hardList = in;
10514 + break;
10515 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10516 + /* Do nothing */
10517 + break;
10518 + case YAFFS_OBJECT_TYPE_SPECIAL:
10519 + /* Do nothing */
10520 + break;
10521 + case YAFFS_OBJECT_TYPE_SYMLINK:
10522 + if (oh) {
10523 + in->variant.symLinkVariant.alias =
10524 + yaffs_CloneString(oh->alias);
10525 + if (!in->variant.symLinkVariant.alias)
10526 + alloc_failed = 1;
10528 + break;
10535 + } /* End of scanning for each chunk */
10537 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10538 + /* If we got this far while scanning, then the block is fully allocated. */
10539 + state = YAFFS_BLOCK_STATE_FULL;
10542 + bi->blockState = state;
10544 + /* Now let's see if it was dirty */
10545 + if (bi->pagesInUse == 0 &&
10546 + !bi->hasShrinkHeader &&
10547 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10548 + yaffs_BlockBecameDirty(dev, blk);
10553 + if (altBlockIndex)
10554 + YFREE_ALT(blockIndex);
10555 + else
10556 + YFREE(blockIndex);
10558 + /* Ok, we've done all the scanning.
10559 + * Fix up the hard link chains.
10560 + * We should now have scanned all the objects, now it's time to add these
10561 + * hardlinks.
10562 + */
10563 + yaffs_HardlinkFixup(dev, hardList);
10566 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10568 + if (alloc_failed)
10569 + return YAFFS_FAIL;
10571 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10573 + return YAFFS_OK;
10576 +/*------------------------------ Directory Functions ----------------------------- */
10578 +static void yaffs_VerifyObjectInDirectory(yaffs_Object *obj)
10580 + struct ylist_head *lh;
10581 + yaffs_Object *listObj;
10583 + int count = 0;
10585 + if (!obj) {
10586 + T(YAFFS_TRACE_ALWAYS, (TSTR("No object to verify" TENDSTR)));
10587 + YBUG();
10588 + return;
10591 + if (yaffs_SkipVerification(obj->myDev))
10592 + return;
10594 + if (!obj->parent) {
10595 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object does not have parent" TENDSTR)));
10596 + YBUG();
10597 + return;
10600 + if (obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10601 + T(YAFFS_TRACE_ALWAYS, (TSTR("Parent is not directory" TENDSTR)));
10602 + YBUG();
10605 + /* Iterate through the objects in each hash entry */
10607 + ylist_for_each(lh, &obj->parent->variant.directoryVariant.children) {
10608 + if (lh) {
10609 + listObj = ylist_entry(lh, yaffs_Object, siblings);
10610 + yaffs_VerifyObject(listObj);
10611 + if (obj == listObj)
10612 + count++;
10616 + if (count != 1) {
10617 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory %d times" TENDSTR), count));
10618 + YBUG();
10622 +static void yaffs_VerifyDirectory(yaffs_Object *directory)
10624 + struct ylist_head *lh;
10625 + yaffs_Object *listObj;
10627 + if (!directory) {
10628 + YBUG();
10629 + return;
10632 + if (yaffs_SkipFullVerification(directory->myDev))
10633 + return;
10635 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10636 + T(YAFFS_TRACE_ALWAYS, (TSTR("Directory has wrong type: %d" TENDSTR), directory->variantType));
10637 + YBUG();
10640 + /* Iterate through the objects in each hash entry */
10642 + ylist_for_each(lh, &directory->variant.directoryVariant.children) {
10643 + if (lh) {
10644 + listObj = ylist_entry(lh, yaffs_Object, siblings);
10645 + if (listObj->parent != directory) {
10646 + T(YAFFS_TRACE_ALWAYS, (TSTR("Object in directory list has wrong parent %p" TENDSTR), listObj->parent));
10647 + YBUG();
10649 + yaffs_VerifyObjectInDirectory(listObj);
10655 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj)
10657 + yaffs_Device *dev = obj->myDev;
10658 + yaffs_Object *parent;
10660 + yaffs_VerifyObjectInDirectory(obj);
10661 + parent = obj->parent;
10663 + yaffs_VerifyDirectory(parent);
10665 + if (dev && dev->removeObjectCallback)
10666 + dev->removeObjectCallback(obj);
10669 + ylist_del_init(&obj->siblings);
10670 + obj->parent = NULL;
10672 + yaffs_VerifyDirectory(parent);
10676 +static void yaffs_AddObjectToDirectory(yaffs_Object *directory,
10677 + yaffs_Object *obj)
10679 + if (!directory) {
10680 + T(YAFFS_TRACE_ALWAYS,
10681 + (TSTR
10682 + ("tragedy: Trying to add an object to a null pointer directory"
10683 + TENDSTR)));
10684 + YBUG();
10685 + return;
10687 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10688 + T(YAFFS_TRACE_ALWAYS,
10689 + (TSTR
10690 + ("tragedy: Trying to add an object to a non-directory"
10691 + TENDSTR)));
10692 + YBUG();
10695 + if (obj->siblings.prev == NULL) {
10696 + /* Not initialised */
10697 + YBUG();
10701 + yaffs_VerifyDirectory(directory);
10703 + yaffs_RemoveObjectFromDirectory(obj);
10706 + /* Now add it */
10707 + ylist_add(&obj->siblings, &directory->variant.directoryVariant.children);
10708 + obj->parent = directory;
10710 + if (directory == obj->myDev->unlinkedDir
10711 + || directory == obj->myDev->deletedDir) {
10712 + obj->unlinked = 1;
10713 + obj->myDev->nUnlinkedFiles++;
10714 + obj->renameAllowed = 0;
10717 + yaffs_VerifyDirectory(directory);
10718 + yaffs_VerifyObjectInDirectory(obj);
10721 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,
10722 + const YCHAR *name)
10724 + int sum;
10726 + struct ylist_head *i;
10727 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10729 + yaffs_Object *l;
10731 + if (!name)
10732 + return NULL;
10734 + if (!directory) {
10735 + T(YAFFS_TRACE_ALWAYS,
10736 + (TSTR
10737 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10738 + TENDSTR)));
10739 + YBUG();
10740 + return NULL;
10742 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10743 + T(YAFFS_TRACE_ALWAYS,
10744 + (TSTR
10745 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10746 + YBUG();
10749 + sum = yaffs_CalcNameSum(name);
10751 + ylist_for_each(i, &directory->variant.directoryVariant.children) {
10752 + if (i) {
10753 + l = ylist_entry(i, yaffs_Object, siblings);
10755 + if (l->parent != directory)
10756 + YBUG();
10758 + yaffs_CheckObjectDetailsLoaded(l);
10760 + /* Special case for lost-n-found */
10761 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10762 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0)
10763 + return l;
10764 + } else if (yaffs_SumCompare(l->sum, sum) || l->hdrChunk <= 0) {
10765 + /* LostnFound chunk called Objxxx
10766 + * Do a real check
10767 + */
10768 + yaffs_GetObjectName(l, buffer,
10769 + YAFFS_MAX_NAME_LENGTH);
10770 + if (yaffs_strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH) == 0)
10771 + return l;
10776 + return NULL;
10780 +#if 0
10781 +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
10782 + int (*fn) (yaffs_Object *))
10784 + struct ylist_head *i;
10785 + yaffs_Object *l;
10787 + if (!theDir) {
10788 + T(YAFFS_TRACE_ALWAYS,
10789 + (TSTR
10790 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10791 + TENDSTR)));
10792 + YBUG();
10793 + return YAFFS_FAIL;
10795 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10796 + T(YAFFS_TRACE_ALWAYS,
10797 + (TSTR
10798 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10799 + YBUG();
10800 + return YAFFS_FAIL;
10803 + ylist_for_each(i, &theDir->variant.directoryVariant.children) {
10804 + if (i) {
10805 + l = ylist_entry(i, yaffs_Object, siblings);
10806 + if (l && !fn(l))
10807 + return YAFFS_FAIL;
10811 + return YAFFS_OK;
10814 +#endif
10816 +/* GetEquivalentObject dereferences any hard links to get to the
10817 + * actual object.
10818 + */
10820 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj)
10822 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10823 + /* We want the object id of the equivalent object, not this one */
10824 + obj = obj->variant.hardLinkVariant.equivalentObject;
10825 + yaffs_CheckObjectDetailsLoaded(obj);
10827 + return obj;
10830 +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize)
10832 + memset(name, 0, buffSize * sizeof(YCHAR));
10834 + yaffs_CheckObjectDetailsLoaded(obj);
10836 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10837 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10838 + } else if (obj->hdrChunk <= 0) {
10839 + YCHAR locName[20];
10840 + YCHAR numString[20];
10841 + YCHAR *x = &numString[19];
10842 + unsigned v = obj->objectId;
10843 + numString[19] = 0;
10844 + while (v > 0) {
10845 + x--;
10846 + *x = '0' + (v % 10);
10847 + v /= 10;
10849 + /* make up a name */
10850 + yaffs_strcpy(locName, YAFFS_LOSTNFOUND_PREFIX);
10851 + yaffs_strcat(locName, x);
10852 + yaffs_strncpy(name, locName, buffSize - 1);
10855 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10856 + else if (obj->shortName[0])
10857 + yaffs_strcpy(name, obj->shortName);
10858 +#endif
10859 + else {
10860 + int result;
10861 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10863 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10865 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10867 + if (obj->hdrChunk > 0) {
10868 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10869 + obj->hdrChunk, buffer,
10870 + NULL);
10872 + yaffs_strncpy(name, oh->name, buffSize - 1);
10874 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10877 + return yaffs_strlen(name);
10880 +int yaffs_GetObjectFileLength(yaffs_Object *obj)
10882 + /* Dereference any hard linking */
10883 + obj = yaffs_GetEquivalentObject(obj);
10885 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE)
10886 + return obj->variant.fileVariant.fileSize;
10887 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
10888 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10889 + else {
10890 + /* Only a directory should drop through to here */
10891 + return obj->myDev->nDataBytesPerChunk;
10895 +int yaffs_GetObjectLinkCount(yaffs_Object *obj)
10897 + int count = 0;
10898 + struct ylist_head *i;
10900 + if (!obj->unlinked)
10901 + count++; /* the object itself */
10903 + ylist_for_each(i, &obj->hardLinks)
10904 + count++; /* add the hard links; */
10906 + return count;
10909 +int yaffs_GetObjectInode(yaffs_Object *obj)
10911 + obj = yaffs_GetEquivalentObject(obj);
10913 + return obj->objectId;
10916 +unsigned yaffs_GetObjectType(yaffs_Object *obj)
10918 + obj = yaffs_GetEquivalentObject(obj);
10920 + switch (obj->variantType) {
10921 + case YAFFS_OBJECT_TYPE_FILE:
10922 + return DT_REG;
10923 + break;
10924 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10925 + return DT_DIR;
10926 + break;
10927 + case YAFFS_OBJECT_TYPE_SYMLINK:
10928 + return DT_LNK;
10929 + break;
10930 + case YAFFS_OBJECT_TYPE_HARDLINK:
10931 + return DT_REG;
10932 + break;
10933 + case YAFFS_OBJECT_TYPE_SPECIAL:
10934 + if (S_ISFIFO(obj->yst_mode))
10935 + return DT_FIFO;
10936 + if (S_ISCHR(obj->yst_mode))
10937 + return DT_CHR;
10938 + if (S_ISBLK(obj->yst_mode))
10939 + return DT_BLK;
10940 + if (S_ISSOCK(obj->yst_mode))
10941 + return DT_SOCK;
10942 + default:
10943 + return DT_REG;
10944 + break;
10948 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj)
10950 + obj = yaffs_GetEquivalentObject(obj);
10951 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
10952 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10953 + else
10954 + return yaffs_CloneString(_Y(""));
10957 +#ifndef CONFIG_YAFFS_WINCE
10959 +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr)
10961 + unsigned int valid = attr->ia_valid;
10963 + if (valid & ATTR_MODE)
10964 + obj->yst_mode = attr->ia_mode;
10965 + if (valid & ATTR_UID)
10966 + obj->yst_uid = attr->ia_uid;
10967 + if (valid & ATTR_GID)
10968 + obj->yst_gid = attr->ia_gid;
10970 + if (valid & ATTR_ATIME)
10971 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10972 + if (valid & ATTR_CTIME)
10973 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10974 + if (valid & ATTR_MTIME)
10975 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10977 + if (valid & ATTR_SIZE)
10978 + yaffs_ResizeFile(obj, attr->ia_size);
10980 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10982 + return YAFFS_OK;
10985 +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr)
10987 + unsigned int valid = 0;
10989 + attr->ia_mode = obj->yst_mode;
10990 + valid |= ATTR_MODE;
10991 + attr->ia_uid = obj->yst_uid;
10992 + valid |= ATTR_UID;
10993 + attr->ia_gid = obj->yst_gid;
10994 + valid |= ATTR_GID;
10996 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10997 + valid |= ATTR_ATIME;
10998 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10999 + valid |= ATTR_CTIME;
11000 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
11001 + valid |= ATTR_MTIME;
11003 + attr->ia_size = yaffs_GetFileSize(obj);
11004 + valid |= ATTR_SIZE;
11006 + attr->ia_valid = valid;
11008 + return YAFFS_OK;
11011 +#endif
11013 +#if 0
11014 +int yaffs_DumpObject(yaffs_Object *obj)
11016 + YCHAR name[257];
11018 + yaffs_GetObjectName(obj, name, 256);
11020 + T(YAFFS_TRACE_ALWAYS,
11021 + (TSTR
11022 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
11023 + " chunk %d type %d size %d\n"
11024 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
11025 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->hdrChunk,
11026 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
11028 + return YAFFS_OK;
11030 +#endif
11032 +/*---------------------------- Initialisation code -------------------------------------- */
11034 +static int yaffs_CheckDevFunctions(const yaffs_Device *dev)
11037 + /* Common functions, gotta have */
11038 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
11039 + return 0;
11041 +#ifdef CONFIG_YAFFS_YAFFS2
11043 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
11044 + if (dev->writeChunkWithTagsToNAND &&
11045 + dev->readChunkWithTagsFromNAND &&
11046 + !dev->writeChunkToNAND &&
11047 + !dev->readChunkFromNAND &&
11048 + dev->markNANDBlockBad && dev->queryNANDBlock)
11049 + return 1;
11050 +#endif
11052 + /* Can use the "spare" style interface for yaffs1 */
11053 + if (!dev->isYaffs2 &&
11054 + !dev->writeChunkWithTagsToNAND &&
11055 + !dev->readChunkWithTagsFromNAND &&
11056 + dev->writeChunkToNAND &&
11057 + dev->readChunkFromNAND &&
11058 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
11059 + return 1;
11061 + return 0; /* bad */
11065 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
11067 + /* Initialise the unlinked, deleted, root and lost and found directories */
11069 + dev->lostNFoundDir = dev->rootDir = NULL;
11070 + dev->unlinkedDir = dev->deletedDir = NULL;
11072 + dev->unlinkedDir =
11073 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
11075 + dev->deletedDir =
11076 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
11078 + dev->rootDir =
11079 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
11080 + YAFFS_ROOT_MODE | S_IFDIR);
11081 + dev->lostNFoundDir =
11082 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
11083 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
11085 + if (dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir) {
11086 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
11087 + return YAFFS_OK;
11090 + return YAFFS_FAIL;
11093 +int yaffs_GutsInitialise(yaffs_Device *dev)
11095 + int init_failed = 0;
11096 + unsigned x;
11097 + int bits;
11099 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
11101 + /* Check stuff that must be set */
11103 + if (!dev) {
11104 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
11105 + return YAFFS_FAIL;
11108 + dev->internalStartBlock = dev->startBlock;
11109 + dev->internalEndBlock = dev->endBlock;
11110 + dev->blockOffset = 0;
11111 + dev->chunkOffset = 0;
11112 + dev->nFreeChunks = 0;
11114 + dev->gcBlock = -1;
11116 + if (dev->startBlock == 0) {
11117 + dev->internalStartBlock = dev->startBlock + 1;
11118 + dev->internalEndBlock = dev->endBlock + 1;
11119 + dev->blockOffset = 1;
11120 + dev->chunkOffset = dev->nChunksPerBlock;
11123 + /* Check geometry parameters. */
11125 + if ((!dev->inbandTags && dev->isYaffs2 && dev->totalBytesPerChunk < 1024) ||
11126 + (!dev->isYaffs2 && dev->totalBytesPerChunk < 512) ||
11127 + (dev->inbandTags && !dev->isYaffs2) ||
11128 + dev->nChunksPerBlock < 2 ||
11129 + dev->nReservedBlocks < 2 ||
11130 + dev->internalStartBlock <= 0 ||
11131 + dev->internalEndBlock <= 0 ||
11132 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2)) { /* otherwise it is too small */
11133 + T(YAFFS_TRACE_ALWAYS,
11134 + (TSTR
11135 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s, inbandTags %d "
11136 + TENDSTR), dev->totalBytesPerChunk, dev->isYaffs2 ? "2" : "", dev->inbandTags));
11137 + return YAFFS_FAIL;
11140 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
11141 + T(YAFFS_TRACE_ALWAYS,
11142 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
11143 + return YAFFS_FAIL;
11146 + /* Sort out space for inband tags, if required */
11147 + if (dev->inbandTags)
11148 + dev->nDataBytesPerChunk = dev->totalBytesPerChunk - sizeof(yaffs_PackedTags2TagsPart);
11149 + else
11150 + dev->nDataBytesPerChunk = dev->totalBytesPerChunk;
11152 + /* Got the right mix of functions? */
11153 + if (!yaffs_CheckDevFunctions(dev)) {
11154 + /* Function missing */
11155 + T(YAFFS_TRACE_ALWAYS,
11156 + (TSTR
11157 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
11159 + return YAFFS_FAIL;
11162 + /* This is really a compilation check. */
11163 + if (!yaffs_CheckStructures()) {
11164 + T(YAFFS_TRACE_ALWAYS,
11165 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
11166 + return YAFFS_FAIL;
11169 + if (dev->isMounted) {
11170 + T(YAFFS_TRACE_ALWAYS,
11171 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
11172 + return YAFFS_FAIL;
11175 + /* Finished with most checks. One or two more checks happen later on too. */
11177 + dev->isMounted = 1;
11179 + /* OK now calculate a few things for the device */
11181 + /*
11182 + * Calculate all the chunk size manipulation numbers:
11183 + */
11184 + x = dev->nDataBytesPerChunk;
11185 + /* We always use dev->chunkShift and dev->chunkDiv */
11186 + dev->chunkShift = Shifts(x);
11187 + x >>= dev->chunkShift;
11188 + dev->chunkDiv = x;
11189 + /* We only use chunk mask if chunkDiv is 1 */
11190 + dev->chunkMask = (1<<dev->chunkShift) - 1;
11192 + /*
11193 + * Calculate chunkGroupBits.
11194 + * We need to find the next power of 2 > than internalEndBlock
11195 + */
11197 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
11199 + bits = ShiftsGE(x);
11201 + /* Set up tnode width if wide tnodes are enabled. */
11202 + if (!dev->wideTnodesDisabled) {
11203 + /* bits must be even so that we end up with 32-bit words */
11204 + if (bits & 1)
11205 + bits++;
11206 + if (bits < 16)
11207 + dev->tnodeWidth = 16;
11208 + else
11209 + dev->tnodeWidth = bits;
11210 + } else
11211 + dev->tnodeWidth = 16;
11213 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
11215 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
11216 + * so if the bitwidth of the
11217 + * chunk range we're using is greater than 16 we need
11218 + * to figure out chunk shift and chunkGroupSize
11219 + */
11221 + if (bits <= dev->tnodeWidth)
11222 + dev->chunkGroupBits = 0;
11223 + else
11224 + dev->chunkGroupBits = bits - dev->tnodeWidth;
11227 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
11229 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
11230 + /* We have a problem because the soft delete won't work if
11231 + * the chunk group size > chunks per block.
11232 + * This can be remedied by using larger "virtual blocks".
11233 + */
11234 + T(YAFFS_TRACE_ALWAYS,
11235 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
11237 + return YAFFS_FAIL;
11240 + /* OK, we've finished verifying the device, lets continue with initialisation */
11242 + /* More device initialisation */
11243 + dev->garbageCollections = 0;
11244 + dev->passiveGarbageCollections = 0;
11245 + dev->currentDirtyChecker = 0;
11246 + dev->bufferedBlock = -1;
11247 + dev->doingBufferedBlockRewrite = 0;
11248 + dev->nDeletedFiles = 0;
11249 + dev->nBackgroundDeletions = 0;
11250 + dev->nUnlinkedFiles = 0;
11251 + dev->eccFixed = 0;
11252 + dev->eccUnfixed = 0;
11253 + dev->tagsEccFixed = 0;
11254 + dev->tagsEccUnfixed = 0;
11255 + dev->nErasureFailures = 0;
11256 + dev->nErasedBlocks = 0;
11257 + dev->isDoingGC = 0;
11258 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
11260 + /* Initialise temporary buffers and caches. */
11261 + if (!yaffs_InitialiseTempBuffers(dev))
11262 + init_failed = 1;
11264 + dev->srCache = NULL;
11265 + dev->gcCleanupList = NULL;
11268 + if (!init_failed &&
11269 + dev->nShortOpCaches > 0) {
11270 + int i;
11271 + void *buf;
11272 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
11274 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES)
11275 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
11277 + dev->srCache = YMALLOC(srCacheBytes);
11279 + buf = (__u8 *) dev->srCache;
11281 + if (dev->srCache)
11282 + memset(dev->srCache, 0, srCacheBytes);
11284 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
11285 + dev->srCache[i].object = NULL;
11286 + dev->srCache[i].lastUse = 0;
11287 + dev->srCache[i].dirty = 0;
11288 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->totalBytesPerChunk);
11290 + if (!buf)
11291 + init_failed = 1;
11293 + dev->srLastUse = 0;
11296 + dev->cacheHits = 0;
11298 + if (!init_failed) {
11299 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
11300 + if (!dev->gcCleanupList)
11301 + init_failed = 1;
11304 + if (dev->isYaffs2)
11305 + dev->useHeaderFileSize = 1;
11307 + if (!init_failed && !yaffs_InitialiseBlocks(dev))
11308 + init_failed = 1;
11310 + yaffs_InitialiseTnodes(dev);
11311 + yaffs_InitialiseObjects(dev);
11313 + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
11314 + init_failed = 1;
11317 + if (!init_failed) {
11318 + /* Now scan the flash. */
11319 + if (dev->isYaffs2) {
11320 + if (yaffs_CheckpointRestore(dev)) {
11321 + yaffs_CheckObjectDetailsLoaded(dev->rootDir);
11322 + T(YAFFS_TRACE_ALWAYS,
11323 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
11324 + } else {
11326 + /* Clean up the mess caused by an aborted checkpoint load
11327 + * and scan backwards.
11328 + */
11329 + yaffs_DeinitialiseBlocks(dev);
11330 + yaffs_DeinitialiseTnodes(dev);
11331 + yaffs_DeinitialiseObjects(dev);
11334 + dev->nErasedBlocks = 0;
11335 + dev->nFreeChunks = 0;
11336 + dev->allocationBlock = -1;
11337 + dev->allocationPage = -1;
11338 + dev->nDeletedFiles = 0;
11339 + dev->nUnlinkedFiles = 0;
11340 + dev->nBackgroundDeletions = 0;
11341 + dev->oldestDirtySequence = 0;
11343 + if (!init_failed && !yaffs_InitialiseBlocks(dev))
11344 + init_failed = 1;
11346 + yaffs_InitialiseTnodes(dev);
11347 + yaffs_InitialiseObjects(dev);
11349 + if (!init_failed && !yaffs_CreateInitialDirectories(dev))
11350 + init_failed = 1;
11352 + if (!init_failed && !yaffs_ScanBackwards(dev))
11353 + init_failed = 1;
11355 + } else if (!yaffs_Scan(dev))
11356 + init_failed = 1;
11358 + yaffs_StripDeletedObjects(dev);
11361 + if (init_failed) {
11362 + /* Clean up the mess */
11363 + T(YAFFS_TRACE_TRACING,
11364 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
11366 + yaffs_Deinitialise(dev);
11367 + return YAFFS_FAIL;
11370 + /* Zero out stats */
11371 + dev->nPageReads = 0;
11372 + dev->nPageWrites = 0;
11373 + dev->nBlockErasures = 0;
11374 + dev->nGCCopies = 0;
11375 + dev->nRetriedWrites = 0;
11377 + dev->nRetiredBlocks = 0;
11379 + yaffs_VerifyFreeChunks(dev);
11380 + yaffs_VerifyBlocks(dev);
11383 + T(YAFFS_TRACE_TRACING,
11384 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
11385 + return YAFFS_OK;
11389 +void yaffs_Deinitialise(yaffs_Device *dev)
11391 + if (dev->isMounted) {
11392 + int i;
11394 + yaffs_DeinitialiseBlocks(dev);
11395 + yaffs_DeinitialiseTnodes(dev);
11396 + yaffs_DeinitialiseObjects(dev);
11397 + if (dev->nShortOpCaches > 0 &&
11398 + dev->srCache) {
11400 + for (i = 0; i < dev->nShortOpCaches; i++) {
11401 + if (dev->srCache[i].data)
11402 + YFREE(dev->srCache[i].data);
11403 + dev->srCache[i].data = NULL;
11406 + YFREE(dev->srCache);
11407 + dev->srCache = NULL;
11410 + YFREE(dev->gcCleanupList);
11412 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
11413 + YFREE(dev->tempBuffer[i].buffer);
11415 + dev->isMounted = 0;
11417 + if (dev->deinitialiseNAND)
11418 + dev->deinitialiseNAND(dev);
11422 +static int yaffs_CountFreeChunks(yaffs_Device *dev)
11424 + int nFree;
11425 + int b;
11427 + yaffs_BlockInfo *blk;
11429 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11430 + b++) {
11431 + blk = yaffs_GetBlockInfo(dev, b);
11433 + switch (blk->blockState) {
11434 + case YAFFS_BLOCK_STATE_EMPTY:
11435 + case YAFFS_BLOCK_STATE_ALLOCATING:
11436 + case YAFFS_BLOCK_STATE_COLLECTING:
11437 + case YAFFS_BLOCK_STATE_FULL:
11438 + nFree +=
11439 + (dev->nChunksPerBlock - blk->pagesInUse +
11440 + blk->softDeletions);
11441 + break;
11442 + default:
11443 + break;
11447 + return nFree;
11450 +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev)
11452 + /* This is what we report to the outside world */
11454 + int nFree;
11455 + int nDirtyCacheChunks;
11456 + int blocksForCheckpoint;
11457 + int i;
11459 +#if 1
11460 + nFree = dev->nFreeChunks;
11461 +#else
11462 + nFree = yaffs_CountFreeChunks(dev);
11463 +#endif
11465 + nFree += dev->nDeletedFiles;
11467 + /* Now count the number of dirty chunks in the cache and subtract those */
11469 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11470 + if (dev->srCache[i].dirty)
11471 + nDirtyCacheChunks++;
11474 + nFree -= nDirtyCacheChunks;
11476 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11478 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11479 + blocksForCheckpoint = yaffs_CalcCheckpointBlocksRequired(dev) - dev->blocksInCheckpoint;
11480 + if (blocksForCheckpoint < 0)
11481 + blocksForCheckpoint = 0;
11483 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11485 + if (nFree < 0)
11486 + nFree = 0;
11488 + return nFree;
11492 +static int yaffs_freeVerificationFailures;
11494 +static void yaffs_VerifyFreeChunks(yaffs_Device *dev)
11496 + int counted;
11497 + int difference;
11499 + if (yaffs_SkipVerification(dev))
11500 + return;
11502 + counted = yaffs_CountFreeChunks(dev);
11504 + difference = dev->nFreeChunks - counted;
11506 + if (difference) {
11507 + T(YAFFS_TRACE_ALWAYS,
11508 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11509 + dev->nFreeChunks, counted, difference));
11510 + yaffs_freeVerificationFailures++;
11514 +/*---------------------------------------- YAFFS test code ----------------------*/
11516 +#define yaffs_CheckStruct(structure, syze, name) \
11517 + do { \
11518 + if (sizeof(structure) != syze) { \
11519 + T(YAFFS_TRACE_ALWAYS, (TSTR("%s should be %d but is %d\n" TENDSTR),\
11520 + name, syze, sizeof(structure))); \
11521 + return YAFFS_FAIL; \
11522 + } \
11523 + } while (0)
11525 +static int yaffs_CheckStructures(void)
11527 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags"); */
11528 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion"); */
11529 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare"); */
11530 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11531 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode");
11532 +#endif
11533 +#ifndef CONFIG_YAFFS_WINCE
11534 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader");
11535 +#endif
11536 + return YAFFS_OK;
11538 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_guts.h linux-2.6.30/fs/yaffs2/yaffs_guts.h
11539 --- linux-2.6.30.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
11540 +++ linux-2.6.30/fs/yaffs2/yaffs_guts.h 2009-06-11 09:21:04.000000000 +0200
11541 @@ -0,0 +1,904 @@
11543 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11545 + * Copyright (C) 2002-2007 Aleph One Ltd.
11546 + * for Toby Churchill Ltd and Brightstar Engineering
11548 + * Created by Charles Manning <charles@aleph1.co.uk>
11550 + * This program is free software; you can redistribute it and/or modify
11551 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11552 + * published by the Free Software Foundation.
11554 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11555 + */
11557 +#ifndef __YAFFS_GUTS_H__
11558 +#define __YAFFS_GUTS_H__
11560 +#include "devextras.h"
11561 +#include "yportenv.h"
11563 +#define YAFFS_OK 1
11564 +#define YAFFS_FAIL 0
11566 +/* Give us a Y=0x59,
11567 + * Give us an A=0x41,
11568 + * Give us an FF=0xFF
11569 + * Give us an S=0x53
11570 + * And what have we got...
11571 + */
11572 +#define YAFFS_MAGIC 0x5941FF53
11574 +#define YAFFS_NTNODES_LEVEL0 16
11575 +#define YAFFS_TNODES_LEVEL0_BITS 4
11576 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11578 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11579 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11580 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11581 +#define YAFFS_TNODES_MAX_LEVEL 6
11583 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11584 +#define YAFFS_BYTES_PER_SPARE 16
11585 +#define YAFFS_BYTES_PER_CHUNK 512
11586 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11587 +#define YAFFS_CHUNKS_PER_BLOCK 32
11588 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11589 +#endif
11591 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11592 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11594 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11596 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11598 +#define YAFFS_ALLOCATION_NOBJECTS 100
11599 +#define YAFFS_ALLOCATION_NTNODES 100
11600 +#define YAFFS_ALLOCATION_NLINKS 100
11602 +#define YAFFS_NOBJECT_BUCKETS 256
11605 +#define YAFFS_OBJECT_SPACE 0x40000
11607 +#define YAFFS_CHECKPOINT_VERSION 3
11609 +#ifdef CONFIG_YAFFS_UNICODE
11610 +#define YAFFS_MAX_NAME_LENGTH 127
11611 +#define YAFFS_MAX_ALIAS_LENGTH 79
11612 +#else
11613 +#define YAFFS_MAX_NAME_LENGTH 255
11614 +#define YAFFS_MAX_ALIAS_LENGTH 159
11615 +#endif
11617 +#define YAFFS_SHORT_NAME_LENGTH 15
11619 +/* Some special object ids for pseudo objects */
11620 +#define YAFFS_OBJECTID_ROOT 1
11621 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11622 +#define YAFFS_OBJECTID_UNLINKED 3
11623 +#define YAFFS_OBJECTID_DELETED 4
11625 +/* Sseudo object ids for checkpointing */
11626 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11627 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11628 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11630 +/* */
11632 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11634 +#define YAFFS_N_TEMP_BUFFERS 6
11636 +/* We limit the number attempts at sucessfully saving a chunk of data.
11637 + * Small-page devices have 32 pages per block; large-page devices have 64.
11638 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11639 + */
11640 +#define YAFFS_WR_ATTEMPTS (5*64)
11642 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11643 + * The range is limited slightly to help distinguish bad numbers from good.
11644 + * This also allows us to perhaps in the future use special numbers for
11645 + * special purposes.
11646 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11647 + * and is a larger number than the lifetime of a 2GB device.
11648 + */
11649 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11650 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11652 +/* Special sequence number for bad block that failed to be marked bad */
11653 +#define YAFFS_SEQUENCE_BAD_BLOCK 0xFFFF0000
11655 +/* ChunkCache is used for short read/write operations.*/
11656 +typedef struct {
11657 + struct yaffs_ObjectStruct *object;
11658 + int chunkId;
11659 + int lastUse;
11660 + int dirty;
11661 + int nBytes; /* Only valid if the cache is dirty */
11662 + int locked; /* Can't push out or flush while locked. */
11663 +#ifdef CONFIG_YAFFS_YAFFS2
11664 + __u8 *data;
11665 +#else
11666 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11667 +#endif
11668 +} yaffs_ChunkCache;
11672 +/* Tags structures in RAM
11673 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11674 + * the structure size will get blown out.
11675 + */
11677 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11678 +typedef struct {
11679 + unsigned chunkId:20;
11680 + unsigned serialNumber:2;
11681 + unsigned byteCountLSB:10;
11682 + unsigned objectId:18;
11683 + unsigned ecc:12;
11684 + unsigned byteCountMSB:2;
11685 +} yaffs_Tags;
11687 +typedef union {
11688 + yaffs_Tags asTags;
11689 + __u8 asBytes[8];
11690 +} yaffs_TagsUnion;
11692 +#endif
11694 +/* Stuff used for extended tags in YAFFS2 */
11696 +typedef enum {
11697 + YAFFS_ECC_RESULT_UNKNOWN,
11698 + YAFFS_ECC_RESULT_NO_ERROR,
11699 + YAFFS_ECC_RESULT_FIXED,
11700 + YAFFS_ECC_RESULT_UNFIXED
11701 +} yaffs_ECCResult;
11703 +typedef enum {
11704 + YAFFS_OBJECT_TYPE_UNKNOWN,
11705 + YAFFS_OBJECT_TYPE_FILE,
11706 + YAFFS_OBJECT_TYPE_SYMLINK,
11707 + YAFFS_OBJECT_TYPE_DIRECTORY,
11708 + YAFFS_OBJECT_TYPE_HARDLINK,
11709 + YAFFS_OBJECT_TYPE_SPECIAL
11710 +} yaffs_ObjectType;
11712 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11714 +typedef struct {
11716 + unsigned validMarker0;
11717 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11718 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11719 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11720 + unsigned byteCount; /* Only valid for data chunks */
11722 + /* The following stuff only has meaning when we read */
11723 + yaffs_ECCResult eccResult;
11724 + unsigned blockBad;
11726 + /* YAFFS 1 stuff */
11727 + unsigned chunkDeleted; /* The chunk is marked deleted */
11728 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11730 + /* YAFFS2 stuff */
11731 + unsigned sequenceNumber; /* The sequence number of this block */
11733 + /* Extra info if this is an object header (YAFFS2 only) */
11735 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11736 + unsigned extraParentObjectId; /* The parent object */
11737 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11738 + unsigned extraShadows; /* Does this shadow another object? */
11740 + yaffs_ObjectType extraObjectType; /* What object type? */
11742 + unsigned extraFileLength; /* Length if it is a file */
11743 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11745 + unsigned validMarker1;
11747 +} yaffs_ExtendedTags;
11749 +/* Spare structure for YAFFS1 */
11750 +typedef struct {
11751 + __u8 tagByte0;
11752 + __u8 tagByte1;
11753 + __u8 tagByte2;
11754 + __u8 tagByte3;
11755 + __u8 pageStatus; /* set to 0 to delete the chunk */
11756 + __u8 blockStatus;
11757 + __u8 tagByte4;
11758 + __u8 tagByte5;
11759 + __u8 ecc1[3];
11760 + __u8 tagByte6;
11761 + __u8 tagByte7;
11762 + __u8 ecc2[3];
11763 +} yaffs_Spare;
11765 +/*Special structure for passing through to mtd */
11766 +struct yaffs_NANDSpare {
11767 + yaffs_Spare spare;
11768 + int eccres1;
11769 + int eccres2;
11772 +/* Block data in RAM */
11774 +typedef enum {
11775 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11777 + YAFFS_BLOCK_STATE_SCANNING,
11778 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11779 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11780 + * but it needs to be scanned to determine its true state.
11781 + * This state is only valid during yaffs_Scan.
11782 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11783 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11784 + */
11786 + YAFFS_BLOCK_STATE_EMPTY,
11787 + /* This block is empty */
11789 + YAFFS_BLOCK_STATE_ALLOCATING,
11790 + /* This block is partially allocated.
11791 + * At least one page holds valid data.
11792 + * This is the one currently being used for page
11793 + * allocation. Should never be more than one of these
11794 + */
11796 + YAFFS_BLOCK_STATE_FULL,
11797 + /* All the pages in this block have been allocated.
11798 + */
11800 + YAFFS_BLOCK_STATE_DIRTY,
11801 + /* All pages have been allocated and deleted.
11802 + * Erase me, reuse me.
11803 + */
11805 + YAFFS_BLOCK_STATE_CHECKPOINT,
11806 + /* This block is assigned to holding checkpoint data.
11807 + */
11809 + YAFFS_BLOCK_STATE_COLLECTING,
11810 + /* This block is being garbage collected */
11812 + YAFFS_BLOCK_STATE_DEAD
11813 + /* This block has failed and is not in use */
11814 +} yaffs_BlockState;
11816 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11819 +typedef struct {
11821 + int softDeletions:10; /* number of soft deleted pages */
11822 + int pagesInUse:10; /* number of pages in use */
11823 + unsigned blockState:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
11824 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11825 + /* and retire the block. */
11826 + __u32 skipErasedCheck:1; /* If this is set we can skip the erased check on this block */
11827 + __u32 gcPrioritise:1; /* An ECC check or blank check has failed on this block.
11828 + It should be prioritised for GC */
11829 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11831 +#ifdef CONFIG_YAFFS_YAFFS2
11832 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11833 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11834 +#endif
11836 +} yaffs_BlockInfo;
11838 +/* -------------------------- Object structure -------------------------------*/
11839 +/* This is the object structure as stored on NAND */
11841 +typedef struct {
11842 + yaffs_ObjectType type;
11844 + /* Apply to everything */
11845 + int parentObjectId;
11846 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11847 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11849 + /* The following apply to directories, files, symlinks - not hard links */
11850 + __u32 yst_mode; /* protection */
11852 +#ifdef CONFIG_YAFFS_WINCE
11853 + __u32 notForWinCE[5];
11854 +#else
11855 + __u32 yst_uid;
11856 + __u32 yst_gid;
11857 + __u32 yst_atime;
11858 + __u32 yst_mtime;
11859 + __u32 yst_ctime;
11860 +#endif
11862 + /* File size applies to files only */
11863 + int fileSize;
11865 + /* Equivalent object id applies to hard links only. */
11866 + int equivalentObjectId;
11868 + /* Alias is for symlinks only. */
11869 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11871 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11873 +#ifdef CONFIG_YAFFS_WINCE
11874 + __u32 win_ctime[2];
11875 + __u32 win_atime[2];
11876 + __u32 win_mtime[2];
11877 +#else
11878 + __u32 roomToGrow[6];
11880 +#endif
11881 + __u32 inbandShadowsObject;
11882 + __u32 inbandIsShrink;
11884 + __u32 reservedSpace[2];
11885 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11887 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11888 + __u32 isShrink;
11890 +} yaffs_ObjectHeader;
11892 +/*--------------------------- Tnode -------------------------- */
11894 +union yaffs_Tnode_union {
11895 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11896 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11897 +#else
11898 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11899 +#endif
11900 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11904 +typedef union yaffs_Tnode_union yaffs_Tnode;
11906 +struct yaffs_TnodeList_struct {
11907 + struct yaffs_TnodeList_struct *next;
11908 + yaffs_Tnode *tnodes;
11911 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11913 +/*------------------------ Object -----------------------------*/
11914 +/* An object can be one of:
11915 + * - a directory (no data, has children links
11916 + * - a regular file (data.... not prunes :->).
11917 + * - a symlink [symbolic link] (the alias).
11918 + * - a hard link
11919 + */
11921 +typedef struct {
11922 + __u32 fileSize;
11923 + __u32 scannedFileSize;
11924 + __u32 shrinkSize;
11925 + int topLevel;
11926 + yaffs_Tnode *top;
11927 +} yaffs_FileStructure;
11929 +typedef struct {
11930 + struct ylist_head children; /* list of child links */
11931 +} yaffs_DirectoryStructure;
11933 +typedef struct {
11934 + YCHAR *alias;
11935 +} yaffs_SymLinkStructure;
11937 +typedef struct {
11938 + struct yaffs_ObjectStruct *equivalentObject;
11939 + __u32 equivalentObjectId;
11940 +} yaffs_HardLinkStructure;
11942 +typedef union {
11943 + yaffs_FileStructure fileVariant;
11944 + yaffs_DirectoryStructure directoryVariant;
11945 + yaffs_SymLinkStructure symLinkVariant;
11946 + yaffs_HardLinkStructure hardLinkVariant;
11947 +} yaffs_ObjectVariant;
11949 +struct yaffs_ObjectStruct {
11950 + __u8 deleted:1; /* This should only apply to unlinked files. */
11951 + __u8 softDeleted:1; /* it has also been soft deleted */
11952 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11953 + __u8 fake:1; /* A fake object has no presence on NAND. */
11954 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11955 + __u8 unlinkAllowed:1;
11956 + __u8 dirty:1; /* the object needs to be written to flash */
11957 + __u8 valid:1; /* When the file system is being loaded up, this
11958 + * object might be created before the data
11959 + * is available (ie. file data records appear before the header).
11960 + */
11961 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11963 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11964 + * still in the inode cache. Free of object is defered.
11965 + * until the inode is released.
11966 + */
11967 + __u8 beingCreated:1; /* This object is still being created so skip some checks. */
11969 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11970 + __u16 sum; /* sum of the name to speed searching */
11972 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11974 + struct ylist_head hashLink; /* list of objects in this hash bucket */
11976 + struct ylist_head hardLinks; /* all the equivalent hard linked objects */
11978 + /* directory structure stuff */
11979 + /* also used for linking up the free list */
11980 + struct yaffs_ObjectStruct *parent;
11981 + struct ylist_head siblings;
11983 + /* Where's my object header in NAND? */
11984 + int hdrChunk;
11986 + int nDataChunks; /* Number of data chunks attached to the file. */
11988 + __u32 objectId; /* the object id value */
11990 + __u32 yst_mode;
11992 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11993 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11994 +#endif
11996 +#ifndef __KERNEL__
11997 + __u32 inUse;
11998 +#endif
12000 +#ifdef CONFIG_YAFFS_WINCE
12001 + __u32 win_ctime[2];
12002 + __u32 win_mtime[2];
12003 + __u32 win_atime[2];
12004 +#else
12005 + __u32 yst_uid;
12006 + __u32 yst_gid;
12007 + __u32 yst_atime;
12008 + __u32 yst_mtime;
12009 + __u32 yst_ctime;
12010 +#endif
12012 + __u32 yst_rdev;
12014 +#ifdef __KERNEL__
12015 + struct inode *myInode;
12017 +#endif
12019 + yaffs_ObjectType variantType;
12021 + yaffs_ObjectVariant variant;
12025 +typedef struct yaffs_ObjectStruct yaffs_Object;
12027 +struct yaffs_ObjectList_struct {
12028 + yaffs_Object *objects;
12029 + struct yaffs_ObjectList_struct *next;
12032 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
12034 +typedef struct {
12035 + struct ylist_head list;
12036 + int count;
12037 +} yaffs_ObjectBucket;
12040 +/* yaffs_CheckpointObject holds the definition of an object as dumped
12041 + * by checkpointing.
12042 + */
12044 +typedef struct {
12045 + int structType;
12046 + __u32 objectId;
12047 + __u32 parentId;
12048 + int hdrChunk;
12049 + yaffs_ObjectType variantType:3;
12050 + __u8 deleted:1;
12051 + __u8 softDeleted:1;
12052 + __u8 unlinked:1;
12053 + __u8 fake:1;
12054 + __u8 renameAllowed:1;
12055 + __u8 unlinkAllowed:1;
12056 + __u8 serial;
12058 + int nDataChunks;
12059 + __u32 fileSizeOrEquivalentObjectId;
12060 +} yaffs_CheckpointObject;
12062 +/*--------------------- Temporary buffers ----------------
12064 + * These are chunk-sized working buffers. Each device has a few
12065 + */
12067 +typedef struct {
12068 + __u8 *buffer;
12069 + int line; /* track from whence this buffer was allocated */
12070 + int maxLine;
12071 +} yaffs_TempBuffer;
12073 +/*----------------- Device ---------------------------------*/
12075 +struct yaffs_DeviceStruct {
12076 + struct ylist_head devList;
12077 + const char *name;
12079 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
12080 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
12081 + int nChunksPerBlock; /* does not need to be a power of 2 */
12082 + int spareBytesPerChunk; /* spare area size */
12083 + int startBlock; /* Start block we're allowed to use */
12084 + int endBlock; /* End block we're allowed to use */
12085 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
12086 + /* reserved blocks on NOR and RAM. */
12089 + /* Stuff used by the shared space checkpointing mechanism */
12090 + /* If this value is zero, then this mechanism is disabled */
12092 +/* int nCheckpointReservedBlocks; */ /* Blocks to reserve for checkpoint data */
12095 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
12096 + * the number of short op caches (don't use too many)
12097 + */
12099 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
12101 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
12103 + void *genericDevice; /* Pointer to device context
12104 + * On an mtd this holds the mtd pointer.
12105 + */
12106 + void *superBlock;
12108 + /* NAND access functions (Must be set before calling YAFFS)*/
12110 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct *dev,
12111 + int chunkInNAND, const __u8 *data,
12112 + const yaffs_Spare *spare);
12113 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct *dev,
12114 + int chunkInNAND, __u8 *data,
12115 + yaffs_Spare *spare);
12116 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct *dev,
12117 + int blockInNAND);
12118 + int (*initialiseNAND) (struct yaffs_DeviceStruct *dev);
12119 + int (*deinitialiseNAND) (struct yaffs_DeviceStruct *dev);
12121 +#ifdef CONFIG_YAFFS_YAFFS2
12122 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct *dev,
12123 + int chunkInNAND, const __u8 *data,
12124 + const yaffs_ExtendedTags *tags);
12125 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct *dev,
12126 + int chunkInNAND, __u8 *data,
12127 + yaffs_ExtendedTags *tags);
12128 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct *dev, int blockNo);
12129 + int (*queryNANDBlock) (struct yaffs_DeviceStruct *dev, int blockNo,
12130 + yaffs_BlockState *state, __u32 *sequenceNumber);
12131 +#endif
12133 + int isYaffs2;
12135 + /* The removeObjectCallback function must be supplied by OS flavours that
12136 + * need it. The Linux kernel does not use this, but yaffs direct does use
12137 + * it to implement the faster readdir
12138 + */
12139 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
12141 + /* Callback to mark the superblock dirsty */
12142 + void (*markSuperBlockDirty)(void *superblock);
12144 + int wideTnodesDisabled; /* Set to disable wide tnodes */
12146 + YCHAR *pathDividers; /* String of legal path dividers */
12149 + /* End of stuff that must be set before initialisation. */
12151 + /* Checkpoint control. Can be set before or after initialisation */
12152 + __u8 skipCheckpointRead;
12153 + __u8 skipCheckpointWrite;
12155 + /* Runtime parameters. Set up by YAFFS. */
12157 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
12158 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
12160 + /* Stuff to support wide tnodes */
12161 + __u32 tnodeWidth;
12162 + __u32 tnodeMask;
12164 + /* Stuff for figuring out file offset to chunk conversions */
12165 + __u32 chunkShift; /* Shift value */
12166 + __u32 chunkDiv; /* Divisor after shifting: 1 for power-of-2 sizes */
12167 + __u32 chunkMask; /* Mask to use for power-of-2 case */
12169 + /* Stuff to handle inband tags */
12170 + int inbandTags;
12171 + __u32 totalBytesPerChunk;
12173 +#ifdef __KERNEL__
12175 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
12176 + struct semaphore grossLock; /* Gross locking semaphore */
12177 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
12178 + * at compile time so we have to allocate it.
12179 + */
12180 + void (*putSuperFunc) (struct super_block *sb);
12181 +#endif
12183 + int isMounted;
12185 + int isCheckpointed;
12188 + /* Stuff to support block offsetting to support start block zero */
12189 + int internalStartBlock;
12190 + int internalEndBlock;
12191 + int blockOffset;
12192 + int chunkOffset;
12195 + /* Runtime checkpointing stuff */
12196 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
12197 + int checkpointByteCount;
12198 + int checkpointByteOffset;
12199 + __u8 *checkpointBuffer;
12200 + int checkpointOpenForWrite;
12201 + int blocksInCheckpoint;
12202 + int checkpointCurrentChunk;
12203 + int checkpointCurrentBlock;
12204 + int checkpointNextBlock;
12205 + int *checkpointBlockList;
12206 + int checkpointMaxBlocks;
12207 + __u32 checkpointSum;
12208 + __u32 checkpointXor;
12210 + int nCheckpointBlocksRequired; /* Number of blocks needed to store current checkpoint set */
12212 + /* Block Info */
12213 + yaffs_BlockInfo *blockInfo;
12214 + __u8 *chunkBits; /* bitmap of chunks in use */
12215 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
12216 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
12217 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
12218 + * Must be consistent with nChunksPerBlock.
12219 + */
12221 + int nErasedBlocks;
12222 + int allocationBlock; /* Current block being allocated off */
12223 + __u32 allocationPage;
12224 + int allocationBlockFinder; /* Used to search for next allocation block */
12226 + /* Runtime state */
12227 + int nTnodesCreated;
12228 + yaffs_Tnode *freeTnodes;
12229 + int nFreeTnodes;
12230 + yaffs_TnodeList *allocatedTnodeList;
12232 + int isDoingGC;
12233 + int gcBlock;
12234 + int gcChunk;
12236 + int nObjectsCreated;
12237 + yaffs_Object *freeObjects;
12238 + int nFreeObjects;
12240 + int nHardLinks;
12242 + yaffs_ObjectList *allocatedObjectList;
12244 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
12246 + int nFreeChunks;
12248 + int currentDirtyChecker; /* Used to find current dirtiest block */
12250 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
12251 + int nonAggressiveSkip; /* GC state/mode */
12253 + /* Statistcs */
12254 + int nPageWrites;
12255 + int nPageReads;
12256 + int nBlockErasures;
12257 + int nErasureFailures;
12258 + int nGCCopies;
12259 + int garbageCollections;
12260 + int passiveGarbageCollections;
12261 + int nRetriedWrites;
12262 + int nRetiredBlocks;
12263 + int eccFixed;
12264 + int eccUnfixed;
12265 + int tagsEccFixed;
12266 + int tagsEccUnfixed;
12267 + int nDeletions;
12268 + int nUnmarkedDeletions;
12270 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
12272 + /* Special directories */
12273 + yaffs_Object *rootDir;
12274 + yaffs_Object *lostNFoundDir;
12276 + /* Buffer areas for storing data to recover from write failures TODO
12277 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
12278 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
12279 + */
12281 + int bufferedBlock; /* Which block is buffered here? */
12282 + int doingBufferedBlockRewrite;
12284 + yaffs_ChunkCache *srCache;
12285 + int srLastUse;
12287 + int cacheHits;
12289 + /* Stuff for background deletion and unlinked files.*/
12290 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
12291 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
12292 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
12293 + int nDeletedFiles; /* Count of files awaiting deletion;*/
12294 + int nUnlinkedFiles; /* Count of unlinked files. */
12295 + int nBackgroundDeletions; /* Count of background deletions. */
12298 + /* Temporary buffer management */
12299 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
12300 + int maxTemp;
12301 + int tempInUse;
12302 + int unmanagedTempAllocations;
12303 + int unmanagedTempDeallocations;
12305 + /* yaffs2 runtime stuff */
12306 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
12307 + unsigned oldestDirtySequence;
12311 +typedef struct yaffs_DeviceStruct yaffs_Device;
12313 +/* The static layout of block usage etc is stored in the super block header */
12314 +typedef struct {
12315 + int StructType;
12316 + int version;
12317 + int checkpointStartBlock;
12318 + int checkpointEndBlock;
12319 + int startBlock;
12320 + int endBlock;
12321 + int rfu[100];
12322 +} yaffs_SuperBlockHeader;
12324 +/* The CheckpointDevice structure holds the device information that changes at runtime and
12325 + * must be preserved over unmount/mount cycles.
12326 + */
12327 +typedef struct {
12328 + int structType;
12329 + int nErasedBlocks;
12330 + int allocationBlock; /* Current block being allocated off */
12331 + __u32 allocationPage;
12332 + int nFreeChunks;
12334 + int nDeletedFiles; /* Count of files awaiting deletion;*/
12335 + int nUnlinkedFiles; /* Count of unlinked files. */
12336 + int nBackgroundDeletions; /* Count of background deletions. */
12338 + /* yaffs2 runtime stuff */
12339 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
12340 + unsigned oldestDirtySequence;
12342 +} yaffs_CheckpointDevice;
12345 +typedef struct {
12346 + int structType;
12347 + __u32 magic;
12348 + __u32 version;
12349 + __u32 head;
12350 +} yaffs_CheckpointValidity;
12353 +/*----------------------- YAFFS Functions -----------------------*/
12355 +int yaffs_GutsInitialise(yaffs_Device *dev);
12356 +void yaffs_Deinitialise(yaffs_Device *dev);
12358 +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
12360 +int yaffs_RenameObject(yaffs_Object *oldDir, const YCHAR *oldName,
12361 + yaffs_Object *newDir, const YCHAR *newName);
12363 +int yaffs_Unlink(yaffs_Object *dir, const YCHAR *name);
12364 +int yaffs_DeleteObject(yaffs_Object *obj);
12366 +int yaffs_GetObjectName(yaffs_Object *obj, YCHAR *name, int buffSize);
12367 +int yaffs_GetObjectFileLength(yaffs_Object *obj);
12368 +int yaffs_GetObjectInode(yaffs_Object *obj);
12369 +unsigned yaffs_GetObjectType(yaffs_Object *obj);
12370 +int yaffs_GetObjectLinkCount(yaffs_Object *obj);
12372 +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
12373 +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
12375 +/* File operations */
12376 +int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, loff_t offset,
12377 + int nBytes);
12378 +int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, loff_t offset,
12379 + int nBytes, int writeThrough);
12380 +int yaffs_ResizeFile(yaffs_Object *obj, loff_t newSize);
12382 +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent, const YCHAR *name,
12383 + __u32 mode, __u32 uid, __u32 gid);
12384 +int yaffs_FlushFile(yaffs_Object *obj, int updateTime);
12386 +/* Flushing and checkpointing */
12387 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
12389 +int yaffs_CheckpointSave(yaffs_Device *dev);
12390 +int yaffs_CheckpointRestore(yaffs_Device *dev);
12392 +/* Directory operations */
12393 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent, const YCHAR *name,
12394 + __u32 mode, __u32 uid, __u32 gid);
12395 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir, const YCHAR *name);
12396 +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,
12397 + int (*fn) (yaffs_Object *));
12399 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev, __u32 number);
12401 +/* Link operations */
12402 +yaffs_Object *yaffs_Link(yaffs_Object *parent, const YCHAR *name,
12403 + yaffs_Object *equivalentObject);
12405 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
12407 +/* Symlink operations */
12408 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const YCHAR *name,
12409 + __u32 mode, __u32 uid, __u32 gid,
12410 + const YCHAR *alias);
12411 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object *obj);
12413 +/* Special inodes (fifos, sockets and devices) */
12414 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent, const YCHAR *name,
12415 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
12417 +/* Special directories */
12418 +yaffs_Object *yaffs_Root(yaffs_Device *dev);
12419 +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
12421 +#ifdef CONFIG_YAFFS_WINCE
12422 +/* CONFIG_YAFFS_WINCE special stuff */
12423 +void yfsd_WinFileTimeNow(__u32 target[2]);
12424 +#endif
12426 +#ifdef __KERNEL__
12428 +void yaffs_HandleDeferedFree(yaffs_Object *obj);
12429 +#endif
12431 +/* Debug dump */
12432 +int yaffs_DumpObject(yaffs_Object *obj);
12434 +void yaffs_GutsTest(yaffs_Device *dev);
12436 +/* A few useful functions */
12437 +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
12438 +void yaffs_DeleteChunk(yaffs_Device *dev, int chunkId, int markNAND, int lyn);
12439 +int yaffs_CheckFF(__u8 *buffer, int nBytes);
12440 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12442 +__u8 *yaffs_GetTempBuffer(yaffs_Device *dev, int lineNo);
12443 +void yaffs_ReleaseTempBuffer(yaffs_Device *dev, __u8 *buffer, int lineNo);
12445 +#endif
12446 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffsinterface.h linux-2.6.30/fs/yaffs2/yaffsinterface.h
12447 --- linux-2.6.30.orig/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
12448 +++ linux-2.6.30/fs/yaffs2/yaffsinterface.h 2009-06-11 09:21:04.000000000 +0200
12449 @@ -0,0 +1,21 @@
12451 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12453 + * Copyright (C) 2002-2007 Aleph One Ltd.
12454 + * for Toby Churchill Ltd and Brightstar Engineering
12456 + * Created by Charles Manning <charles@aleph1.co.uk>
12458 + * This program is free software; you can redistribute it and/or modify
12459 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12460 + * published by the Free Software Foundation.
12462 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12463 + */
12465 +#ifndef __YAFFSINTERFACE_H__
12466 +#define __YAFFSINTERFACE_H__
12468 +int yaffs_Initialise(unsigned nBlocks);
12470 +#endif
12471 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif1.c linux-2.6.30/fs/yaffs2/yaffs_mtdif1.c
12472 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
12473 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif1.c 2009-06-11 09:21:04.000000000 +0200
12474 @@ -0,0 +1,365 @@
12476 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12477 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12479 + * Copyright (C) 2002 Aleph One Ltd.
12480 + * for Toby Churchill Ltd and Brightstar Engineering
12482 + * This program is free software; you can redistribute it and/or modify
12483 + * it under the terms of the GNU General Public License version 2 as
12484 + * published by the Free Software Foundation.
12485 + */
12488 + * This module provides the interface between yaffs_nand.c and the
12489 + * MTD API. This version is used when the MTD interface supports the
12490 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12491 + * and we have small-page NAND device.
12493 + * These functions are invoked via function pointers in yaffs_nand.c.
12494 + * This replaces functionality provided by functions in yaffs_mtdif.c
12495 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12496 + * called in yaffs_mtdif.c when the function pointers are NULL.
12497 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12498 + */
12500 +#include "yportenv.h"
12501 +#include "yaffs_guts.h"
12502 +#include "yaffs_packedtags1.h"
12503 +#include "yaffs_tagscompat.h" /* for yaffs_CalcTagsECC */
12505 +#include "linux/kernel.h"
12506 +#include "linux/version.h"
12507 +#include "linux/types.h"
12508 +#include "linux/mtd/mtd.h"
12510 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12511 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
12513 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.10 2009-03-09 07:41:10 charles Exp $";
12515 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12516 +# define YTAG1_SIZE 8
12517 +#else
12518 +# define YTAG1_SIZE 9
12519 +#endif
12521 +#if 0
12522 +/* Use the following nand_ecclayout with MTD when using
12523 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12524 + * If you have existing Yaffs images and the byte order differs from this,
12525 + * adjust 'oobfree' to match your existing Yaffs data.
12527 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12528 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12529 + * the 9th byte.
12531 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12532 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12533 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12534 + * byte and B is the small-page bad-block indicator byte.
12535 + */
12536 +static struct nand_ecclayout nand_oob_16 = {
12537 + .eccbytes = 6,
12538 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12539 + .oobavail = 9,
12540 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12542 +#endif
12544 +/* Write a chunk (page) of data to NAND.
12546 + * Caller always provides ExtendedTags data which are converted to a more
12547 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12548 + * contents of the tags meta-data; used to valid the tags when read.
12550 + * - Pack ExtendedTags to PackedTags1 form
12551 + * - Compute mini-ECC for PackedTags1
12552 + * - Write data and packed tags to NAND.
12554 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12555 + * a full sequence number (as found in the larger PackedTags2 form) it is
12556 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12557 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12558 + * to be written just once. When Yaffs performs this operation, this
12559 + * function is called with a NULL data pointer -- calling MTD write_oob
12560 + * without data is valid usage (2.6.17).
12562 + * Any underlying MTD error results in YAFFS_FAIL.
12563 + * Returns YAFFS_OK or YAFFS_FAIL.
12564 + */
12565 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12566 + int chunkInNAND, const __u8 *data, const yaffs_ExtendedTags *etags)
12568 + struct mtd_info *mtd = dev->genericDevice;
12569 + int chunkBytes = dev->nDataBytesPerChunk;
12570 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12571 + struct mtd_oob_ops ops;
12572 + yaffs_PackedTags1 pt1;
12573 + int retval;
12575 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12576 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12577 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12579 + dev->nPageWrites++;
12581 + yaffs_PackTags1(&pt1, etags);
12582 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12584 + /* When deleting a chunk, the upper layer provides only skeletal
12585 + * etags, one with chunkDeleted set. However, we need to update the
12586 + * tags, not erase them completely. So we use the NAND write property
12587 + * that only zeroed-bits stick and set tag bytes to all-ones and
12588 + * zero just the (not) deleted bit.
12589 + */
12590 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12591 + if (etags->chunkDeleted) {
12592 + memset(&pt1, 0xff, 8);
12593 + /* clear delete status bit to indicate deleted */
12594 + pt1.deleted = 0;
12596 +#else
12597 + ((__u8 *)&pt1)[8] = 0xff;
12598 + if (etags->chunkDeleted) {
12599 + memset(&pt1, 0xff, 8);
12600 + /* zero pageStatus byte to indicate deleted */
12601 + ((__u8 *)&pt1)[8] = 0;
12603 +#endif
12605 + memset(&ops, 0, sizeof(ops));
12606 + ops.mode = MTD_OOB_AUTO;
12607 + ops.len = (data) ? chunkBytes : 0;
12608 + ops.ooblen = YTAG1_SIZE;
12609 + ops.datbuf = (__u8 *)data;
12610 + ops.oobbuf = (__u8 *)&pt1;
12612 + retval = mtd->write_oob(mtd, addr, &ops);
12613 + if (retval) {
12614 + yaffs_trace(YAFFS_TRACE_MTD,
12615 + "write_oob failed, chunk %d, mtd error %d\n",
12616 + chunkInNAND, retval);
12618 + return retval ? YAFFS_FAIL : YAFFS_OK;
12621 +/* Return with empty ExtendedTags but add eccResult.
12622 + */
12623 +static int rettags(yaffs_ExtendedTags *etags, int eccResult, int retval)
12625 + if (etags) {
12626 + memset(etags, 0, sizeof(*etags));
12627 + etags->eccResult = eccResult;
12629 + return retval;
12632 +/* Read a chunk (page) from NAND.
12634 + * Caller expects ExtendedTags data to be usable even on error; that is,
12635 + * all members except eccResult and blockBad are zeroed.
12637 + * - Check ECC results for data (if applicable)
12638 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12639 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12640 + * - Convert PackedTags1 to ExtendedTags
12641 + * - Update eccResult and blockBad members to refect state.
12643 + * Returns YAFFS_OK or YAFFS_FAIL.
12644 + */
12645 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12646 + int chunkInNAND, __u8 *data, yaffs_ExtendedTags *etags)
12648 + struct mtd_info *mtd = dev->genericDevice;
12649 + int chunkBytes = dev->nDataBytesPerChunk;
12650 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12651 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12652 + struct mtd_oob_ops ops;
12653 + yaffs_PackedTags1 pt1;
12654 + int retval;
12655 + int deleted;
12657 + dev->nPageReads++;
12659 + memset(&ops, 0, sizeof(ops));
12660 + ops.mode = MTD_OOB_AUTO;
12661 + ops.len = (data) ? chunkBytes : 0;
12662 + ops.ooblen = YTAG1_SIZE;
12663 + ops.datbuf = data;
12664 + ops.oobbuf = (__u8 *)&pt1;
12666 +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
12667 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12668 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12669 + */
12670 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12671 +#endif
12672 + /* Read page and oob using MTD.
12673 + * Check status and determine ECC result.
12674 + */
12675 + retval = mtd->read_oob(mtd, addr, &ops);
12676 + if (retval) {
12677 + yaffs_trace(YAFFS_TRACE_MTD,
12678 + "read_oob failed, chunk %d, mtd error %d\n",
12679 + chunkInNAND, retval);
12682 + switch (retval) {
12683 + case 0:
12684 + /* no error */
12685 + break;
12687 + case -EUCLEAN:
12688 + /* MTD's ECC fixed the data */
12689 + eccres = YAFFS_ECC_RESULT_FIXED;
12690 + dev->eccFixed++;
12691 + break;
12693 + case -EBADMSG:
12694 + /* MTD's ECC could not fix the data */
12695 + dev->eccUnfixed++;
12696 + /* fall into... */
12697 + default:
12698 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12699 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12700 + return YAFFS_FAIL;
12703 + /* Check for a blank/erased chunk.
12704 + */
12705 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12706 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12707 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12710 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12711 + /* Read deleted status (bit) then return it to it's non-deleted
12712 + * state before performing tags mini-ECC check. pt1.deleted is
12713 + * inverted.
12714 + */
12715 + deleted = !pt1.deleted;
12716 + pt1.deleted = 1;
12717 +#else
12718 + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12719 +#endif
12721 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12722 + */
12723 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12724 + switch (retval) {
12725 + case 0:
12726 + /* no tags error, use MTD result */
12727 + break;
12728 + case 1:
12729 + /* recovered tags-ECC error */
12730 + dev->tagsEccFixed++;
12731 + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
12732 + eccres = YAFFS_ECC_RESULT_FIXED;
12733 + break;
12734 + default:
12735 + /* unrecovered tags-ECC error */
12736 + dev->tagsEccUnfixed++;
12737 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12740 + /* Unpack the tags to extended form and set ECC result.
12741 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12742 + */
12743 + pt1.shouldBeFF = 0xFFFFFFFF;
12744 + yaffs_UnpackTags1(etags, &pt1);
12745 + etags->eccResult = eccres;
12747 + /* Set deleted state */
12748 + etags->chunkDeleted = deleted;
12749 + return YAFFS_OK;
12752 +/* Mark a block bad.
12754 + * This is a persistant state.
12755 + * Use of this function should be rare.
12757 + * Returns YAFFS_OK or YAFFS_FAIL.
12758 + */
12759 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12761 + struct mtd_info *mtd = dev->genericDevice;
12762 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12763 + int retval;
12765 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad\n", blockNo);
12767 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12768 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12771 +/* Check any MTD prerequists.
12773 + * Returns YAFFS_OK or YAFFS_FAIL.
12774 + */
12775 +static int nandmtd1_TestPrerequists(struct mtd_info *mtd)
12777 + /* 2.6.18 has mtd->ecclayout->oobavail */
12778 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12779 + int oobavail = mtd->ecclayout->oobavail;
12781 + if (oobavail < YTAG1_SIZE) {
12782 + yaffs_trace(YAFFS_TRACE_ERROR,
12783 + "mtd device has only %d bytes for tags, need %d\n",
12784 + oobavail, YTAG1_SIZE);
12785 + return YAFFS_FAIL;
12787 + return YAFFS_OK;
12790 +/* Query for the current state of a specific block.
12792 + * Examine the tags of the first chunk of the block and return the state:
12793 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12794 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12795 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12797 + * Always returns YAFFS_OK.
12798 + */
12799 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12800 + yaffs_BlockState *pState, __u32 *pSequenceNumber)
12802 + struct mtd_info *mtd = dev->genericDevice;
12803 + int chunkNo = blockNo * dev->nChunksPerBlock;
12804 + loff_t addr = (loff_t)chunkNo * dev->nDataBytesPerChunk;
12805 + yaffs_ExtendedTags etags;
12806 + int state = YAFFS_BLOCK_STATE_DEAD;
12807 + int seqnum = 0;
12808 + int retval;
12810 + /* We don't yet have a good place to test for MTD config prerequists.
12811 + * Do it here as we are called during the initial scan.
12812 + */
12813 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK)
12814 + return YAFFS_FAIL;
12816 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12817 + etags.blockBad = (mtd->block_isbad)(mtd, addr);
12818 + if (etags.blockBad) {
12819 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12820 + "block %d is marked bad\n", blockNo);
12821 + state = YAFFS_BLOCK_STATE_DEAD;
12822 + } else if (etags.eccResult != YAFFS_ECC_RESULT_NO_ERROR) {
12823 + /* bad tags, need to look more closely */
12824 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12825 + } else if (etags.chunkUsed) {
12826 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12827 + seqnum = etags.sequenceNumber;
12828 + } else {
12829 + state = YAFFS_BLOCK_STATE_EMPTY;
12832 + *pState = state;
12833 + *pSequenceNumber = seqnum;
12835 + /* query always succeeds */
12836 + return YAFFS_OK;
12839 +#endif /*MTD_VERSION*/
12840 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif1.h linux-2.6.30/fs/yaffs2/yaffs_mtdif1.h
12841 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
12842 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif1.h 2009-06-11 09:21:04.000000000 +0200
12843 @@ -0,0 +1,28 @@
12845 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
12847 + * Copyright (C) 2002-2007 Aleph One Ltd.
12848 + * for Toby Churchill Ltd and Brightstar Engineering
12850 + * This program is free software; you can redistribute it and/or modify
12851 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12852 + * published by the Free Software Foundation.
12854 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12855 + */
12857 +#ifndef __YAFFS_MTDIF1_H__
12858 +#define __YAFFS_MTDIF1_H__
12860 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
12861 + const __u8 *data, const yaffs_ExtendedTags *tags);
12863 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
12864 + __u8 *data, yaffs_ExtendedTags *tags);
12866 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
12868 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12869 + yaffs_BlockState *state, __u32 *sequenceNumber);
12871 +#endif
12872 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif2.c linux-2.6.30/fs/yaffs2/yaffs_mtdif2.c
12873 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
12874 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif2.c 2009-06-11 09:21:04.000000000 +0200
12875 @@ -0,0 +1,246 @@
12877 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12879 + * Copyright (C) 2002-2007 Aleph One Ltd.
12880 + * for Toby Churchill Ltd and Brightstar Engineering
12882 + * Created by Charles Manning <charles@aleph1.co.uk>
12884 + * This program is free software; you can redistribute it and/or modify
12885 + * it under the terms of the GNU General Public License version 2 as
12886 + * published by the Free Software Foundation.
12887 + */
12889 +/* mtd interface for YAFFS2 */
12891 +const char *yaffs_mtdif2_c_version =
12892 + "$Id: yaffs_mtdif2.c,v 1.23 2009-03-06 17:20:53 wookey Exp $";
12894 +#include "yportenv.h"
12897 +#include "yaffs_mtdif2.h"
12899 +#include "linux/mtd/mtd.h"
12900 +#include "linux/types.h"
12901 +#include "linux/time.h"
12903 +#include "yaffs_packedtags2.h"
12905 +/* NB For use with inband tags....
12906 + * We assume that the data buffer is of size totalBytersPerChunk so that we can also
12907 + * use it to load the tags.
12908 + */
12909 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
12910 + const __u8 *data,
12911 + const yaffs_ExtendedTags *tags)
12913 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12914 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
12915 + struct mtd_oob_ops ops;
12916 +#else
12917 + size_t dummy;
12918 +#endif
12919 + int retval = 0;
12921 + loff_t addr;
12923 + yaffs_PackedTags2 pt;
12925 + T(YAFFS_TRACE_MTD,
12926 + (TSTR
12927 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
12928 + TENDSTR), chunkInNAND, data, tags));
12931 + addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
12933 + /* For yaffs2 writing there must be both data and tags.
12934 + * If we're using inband tags, then the tags are stuffed into
12935 + * the end of the data buffer.
12936 + */
12937 + if (!data || !tags)
12938 + BUG();
12939 + else if (dev->inbandTags) {
12940 + yaffs_PackedTags2TagsPart *pt2tp;
12941 + pt2tp = (yaffs_PackedTags2TagsPart *)(data + dev->nDataBytesPerChunk);
12942 + yaffs_PackTags2TagsPart(pt2tp, tags);
12943 + } else
12944 + yaffs_PackTags2(&pt, tags);
12946 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
12947 + ops.mode = MTD_OOB_AUTO;
12948 + ops.ooblen = (dev->inbandTags) ? 0 : sizeof(pt);
12949 + ops.len = dev->totalBytesPerChunk;
12950 + ops.ooboffs = 0;
12951 + ops.datbuf = (__u8 *)data;
12952 + ops.oobbuf = (dev->inbandTags) ? NULL : (void *)&pt;
12953 + retval = mtd->write_oob(mtd, addr, &ops);
12955 +#else
12956 + if (!dev->inbandTags) {
12957 + retval =
12958 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12959 + &dummy, data, (__u8 *) &pt, NULL);
12960 + } else {
12961 + retval =
12962 + mtd->write(mtd, addr, dev->totalBytesPerChunk, &dummy,
12963 + data);
12965 +#endif
12967 + if (retval == 0)
12968 + return YAFFS_OK;
12969 + else
12970 + return YAFFS_FAIL;
12973 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
12974 + __u8 *data, yaffs_ExtendedTags *tags)
12976 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12977 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
12978 + struct mtd_oob_ops ops;
12979 +#endif
12980 + size_t dummy;
12981 + int retval = 0;
12982 + int localData = 0;
12984 + loff_t addr = ((loff_t) chunkInNAND) * dev->totalBytesPerChunk;
12986 + yaffs_PackedTags2 pt;
12988 + T(YAFFS_TRACE_MTD,
12989 + (TSTR
12990 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
12991 + TENDSTR), chunkInNAND, data, tags));
12993 + if (dev->inbandTags) {
12995 + if (!data) {
12996 + localData = 1;
12997 + data = yaffs_GetTempBuffer(dev, __LINE__);
13004 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13005 + if (dev->inbandTags || (data && !tags))
13006 + retval = mtd->read(mtd, addr, dev->totalBytesPerChunk,
13007 + &dummy, data);
13008 + else if (tags) {
13009 + ops.mode = MTD_OOB_AUTO;
13010 + ops.ooblen = sizeof(pt);
13011 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13012 + ops.ooboffs = 0;
13013 + ops.datbuf = data;
13014 + ops.oobbuf = dev->spareBuffer;
13015 + retval = mtd->read_oob(mtd, addr, &ops);
13017 +#else
13018 + if (!dev->inbandTags && data && tags) {
13020 + retval = mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13021 + &dummy, data, dev->spareBuffer,
13022 + NULL);
13023 + } else {
13024 + if (data)
13025 + retval =
13026 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13027 + data);
13028 + if (!dev->inbandTags && tags)
13029 + retval =
13030 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13031 + dev->spareBuffer);
13033 +#endif
13036 + if (dev->inbandTags) {
13037 + if (tags) {
13038 + yaffs_PackedTags2TagsPart *pt2tp;
13039 + pt2tp = (yaffs_PackedTags2TagsPart *)&data[dev->nDataBytesPerChunk];
13040 + yaffs_UnpackTags2TagsPart(tags, pt2tp);
13042 + } else {
13043 + if (tags) {
13044 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13045 + yaffs_UnpackTags2(tags, &pt);
13049 + if (localData)
13050 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
13052 + if (tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13053 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13054 + if (retval == 0)
13055 + return YAFFS_OK;
13056 + else
13057 + return YAFFS_FAIL;
13060 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13062 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13063 + int retval;
13064 + T(YAFFS_TRACE_MTD,
13065 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13067 + retval =
13068 + mtd->block_markbad(mtd,
13069 + blockNo * dev->nChunksPerBlock *
13070 + dev->totalBytesPerChunk);
13072 + if (retval == 0)
13073 + return YAFFS_OK;
13074 + else
13075 + return YAFFS_FAIL;
13079 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13080 + yaffs_BlockState *state, __u32 *sequenceNumber)
13082 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13083 + int retval;
13085 + T(YAFFS_TRACE_MTD,
13086 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13087 + retval =
13088 + mtd->block_isbad(mtd,
13089 + blockNo * dev->nChunksPerBlock *
13090 + dev->totalBytesPerChunk);
13092 + if (retval) {
13093 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13095 + *state = YAFFS_BLOCK_STATE_DEAD;
13096 + *sequenceNumber = 0;
13097 + } else {
13098 + yaffs_ExtendedTags t;
13099 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13100 + blockNo *
13101 + dev->nChunksPerBlock, NULL,
13102 + &t);
13104 + if (t.chunkUsed) {
13105 + *sequenceNumber = t.sequenceNumber;
13106 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13107 + } else {
13108 + *sequenceNumber = 0;
13109 + *state = YAFFS_BLOCK_STATE_EMPTY;
13112 + T(YAFFS_TRACE_MTD,
13113 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13114 + *state));
13116 + if (retval == 0)
13117 + return YAFFS_OK;
13118 + else
13119 + return YAFFS_FAIL;
13122 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif2.h linux-2.6.30/fs/yaffs2/yaffs_mtdif2.h
13123 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
13124 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif2.h 2009-06-11 09:21:04.000000000 +0200
13125 @@ -0,0 +1,29 @@
13127 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13129 + * Copyright (C) 2002-2007 Aleph One Ltd.
13130 + * for Toby Churchill Ltd and Brightstar Engineering
13132 + * Created by Charles Manning <charles@aleph1.co.uk>
13134 + * This program is free software; you can redistribute it and/or modify
13135 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13136 + * published by the Free Software Foundation.
13138 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13139 + */
13141 +#ifndef __YAFFS_MTDIF2_H__
13142 +#define __YAFFS_MTDIF2_H__
13144 +#include "yaffs_guts.h"
13145 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device *dev, int chunkInNAND,
13146 + const __u8 *data,
13147 + const yaffs_ExtendedTags *tags);
13148 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
13149 + __u8 *data, yaffs_ExtendedTags *tags);
13150 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13151 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13152 + yaffs_BlockState *state, __u32 *sequenceNumber);
13154 +#endif
13155 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif.c linux-2.6.30/fs/yaffs2/yaffs_mtdif.c
13156 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
13157 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif.c 2009-06-11 09:21:04.000000000 +0200
13158 @@ -0,0 +1,241 @@
13160 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13162 + * Copyright (C) 2002-2007 Aleph One Ltd.
13163 + * for Toby Churchill Ltd and Brightstar Engineering
13165 + * Created by Charles Manning <charles@aleph1.co.uk>
13167 + * This program is free software; you can redistribute it and/or modify
13168 + * it under the terms of the GNU General Public License version 2 as
13169 + * published by the Free Software Foundation.
13170 + */
13172 +const char *yaffs_mtdif_c_version =
13173 + "$Id: yaffs_mtdif.c,v 1.22 2009-03-06 17:20:51 wookey Exp $";
13175 +#include "yportenv.h"
13178 +#include "yaffs_mtdif.h"
13180 +#include "linux/mtd/mtd.h"
13181 +#include "linux/types.h"
13182 +#include "linux/time.h"
13183 +#include "linux/mtd/nand.h"
13185 +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
13186 +static struct nand_oobinfo yaffs_oobinfo = {
13187 + .useecc = 1,
13188 + .eccbytes = 6,
13189 + .eccpos = {8, 9, 10, 13, 14, 15}
13192 +static struct nand_oobinfo yaffs_noeccinfo = {
13193 + .useecc = 0,
13195 +#endif
13197 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
13198 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
13200 + oob[0] = spare->tagByte0;
13201 + oob[1] = spare->tagByte1;
13202 + oob[2] = spare->tagByte2;
13203 + oob[3] = spare->tagByte3;
13204 + oob[4] = spare->tagByte4;
13205 + oob[5] = spare->tagByte5 & 0x3f;
13206 + oob[5] |= spare->blockStatus == 'Y' ? 0 : 0x80;
13207 + oob[5] |= spare->pageStatus == 0 ? 0 : 0x40;
13208 + oob[6] = spare->tagByte6;
13209 + oob[7] = spare->tagByte7;
13212 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
13214 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
13215 + spare->tagByte0 = oob[0];
13216 + spare->tagByte1 = oob[1];
13217 + spare->tagByte2 = oob[2];
13218 + spare->tagByte3 = oob[3];
13219 + spare->tagByte4 = oob[4];
13220 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
13221 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
13222 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
13223 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
13224 + spare->tagByte6 = oob[6];
13225 + spare->tagByte7 = oob[7];
13226 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
13228 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
13230 +#endif
13232 +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
13233 + const __u8 *data, const yaffs_Spare *spare)
13235 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13236 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
13237 + struct mtd_oob_ops ops;
13238 +#endif
13239 + size_t dummy;
13240 + int retval = 0;
13242 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13243 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
13244 + __u8 spareAsBytes[8]; /* OOB */
13246 + if (data && !spare)
13247 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
13248 + &dummy, data);
13249 + else if (spare) {
13250 + if (dev->useNANDECC) {
13251 + translate_spare2oob(spare, spareAsBytes);
13252 + ops.mode = MTD_OOB_AUTO;
13253 + ops.ooblen = 8; /* temp hack */
13254 + } else {
13255 + ops.mode = MTD_OOB_RAW;
13256 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
13258 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
13259 + ops.datbuf = (u8 *)data;
13260 + ops.ooboffs = 0;
13261 + ops.oobbuf = spareAsBytes;
13262 + retval = mtd->write_oob(mtd, addr, &ops);
13264 +#else
13265 + __u8 *spareAsBytes = (__u8 *) spare;
13267 + if (data && spare) {
13268 + if (dev->useNANDECC)
13269 + retval =
13270 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13271 + &dummy, data, spareAsBytes,
13272 + &yaffs_oobinfo);
13273 + else
13274 + retval =
13275 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13276 + &dummy, data, spareAsBytes,
13277 + &yaffs_noeccinfo);
13278 + } else {
13279 + if (data)
13280 + retval =
13281 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13282 + data);
13283 + if (spare)
13284 + retval =
13285 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
13286 + &dummy, spareAsBytes);
13288 +#endif
13290 + if (retval == 0)
13291 + return YAFFS_OK;
13292 + else
13293 + return YAFFS_FAIL;
13296 +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
13297 + yaffs_Spare *spare)
13299 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13300 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
13301 + struct mtd_oob_ops ops;
13302 +#endif
13303 + size_t dummy;
13304 + int retval = 0;
13306 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13307 +#if (MTD_VERSION_CODE > MTD_VERSION(2, 6, 17))
13308 + __u8 spareAsBytes[8]; /* OOB */
13310 + if (data && !spare)
13311 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13312 + &dummy, data);
13313 + else if (spare) {
13314 + if (dev->useNANDECC) {
13315 + ops.mode = MTD_OOB_AUTO;
13316 + ops.ooblen = 8; /* temp hack */
13317 + } else {
13318 + ops.mode = MTD_OOB_RAW;
13319 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
13321 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
13322 + ops.datbuf = data;
13323 + ops.ooboffs = 0;
13324 + ops.oobbuf = spareAsBytes;
13325 + retval = mtd->read_oob(mtd, addr, &ops);
13326 + if (dev->useNANDECC)
13327 + translate_oob2spare(spare, spareAsBytes);
13329 +#else
13330 + __u8 *spareAsBytes = (__u8 *) spare;
13332 + if (data && spare) {
13333 + if (dev->useNANDECC) {
13334 + /* Careful, this call adds 2 ints */
13335 + /* to the end of the spare data. Calling function */
13336 + /* should allocate enough memory for spare, */
13337 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
13338 + retval =
13339 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13340 + &dummy, data, spareAsBytes,
13341 + &yaffs_oobinfo);
13342 + } else {
13343 + retval =
13344 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13345 + &dummy, data, spareAsBytes,
13346 + &yaffs_noeccinfo);
13348 + } else {
13349 + if (data)
13350 + retval =
13351 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13352 + data);
13353 + if (spare)
13354 + retval =
13355 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
13356 + &dummy, spareAsBytes);
13358 +#endif
13360 + if (retval == 0)
13361 + return YAFFS_OK;
13362 + else
13363 + return YAFFS_FAIL;
13366 +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber)
13368 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13369 + __u32 addr =
13370 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
13371 + * dev->nChunksPerBlock;
13372 + struct erase_info ei;
13373 + int retval = 0;
13375 + ei.mtd = mtd;
13376 + ei.addr = addr;
13377 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
13378 + ei.time = 1000;
13379 + ei.retries = 2;
13380 + ei.callback = NULL;
13381 + ei.priv = (u_long) dev;
13383 + /* Todo finish off the ei if required */
13385 + sema_init(&dev->sem, 0);
13387 + retval = mtd->erase(mtd, &ei);
13389 + if (retval == 0)
13390 + return YAFFS_OK;
13391 + else
13392 + return YAFFS_FAIL;
13395 +int nandmtd_InitialiseNAND(yaffs_Device *dev)
13397 + return YAFFS_OK;
13400 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif.h linux-2.6.30/fs/yaffs2/yaffs_mtdif.h
13401 --- linux-2.6.30.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
13402 +++ linux-2.6.30/fs/yaffs2/yaffs_mtdif.h 2009-06-11 09:21:04.000000000 +0200
13403 @@ -0,0 +1,32 @@
13405 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13407 + * Copyright (C) 2002-2007 Aleph One Ltd.
13408 + * for Toby Churchill Ltd and Brightstar Engineering
13410 + * Created by Charles Manning <charles@aleph1.co.uk>
13412 + * This program is free software; you can redistribute it and/or modify
13413 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13414 + * published by the Free Software Foundation.
13416 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13417 + */
13419 +#ifndef __YAFFS_MTDIF_H__
13420 +#define __YAFFS_MTDIF_H__
13422 +#include "yaffs_guts.h"
13424 +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 18))
13425 +extern struct nand_oobinfo yaffs_oobinfo;
13426 +extern struct nand_oobinfo yaffs_noeccinfo;
13427 +#endif
13429 +int nandmtd_WriteChunkToNAND(yaffs_Device *dev, int chunkInNAND,
13430 + const __u8 *data, const yaffs_Spare *spare);
13431 +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev, int chunkInNAND, __u8 *data,
13432 + yaffs_Spare *spare);
13433 +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber);
13434 +int nandmtd_InitialiseNAND(yaffs_Device *dev);
13435 +#endif
13436 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_nand.c linux-2.6.30/fs/yaffs2/yaffs_nand.c
13437 --- linux-2.6.30.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
13438 +++ linux-2.6.30/fs/yaffs2/yaffs_nand.c 2009-06-11 09:21:04.000000000 +0200
13439 @@ -0,0 +1,135 @@
13441 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13443 + * Copyright (C) 2002-2007 Aleph One Ltd.
13444 + * for Toby Churchill Ltd and Brightstar Engineering
13446 + * Created by Charles Manning <charles@aleph1.co.uk>
13448 + * This program is free software; you can redistribute it and/or modify
13449 + * it under the terms of the GNU General Public License version 2 as
13450 + * published by the Free Software Foundation.
13451 + */
13453 +const char *yaffs_nand_c_version =
13454 + "$Id: yaffs_nand.c,v 1.10 2009-03-06 17:20:54 wookey Exp $";
13456 +#include "yaffs_nand.h"
13457 +#include "yaffs_tagscompat.h"
13458 +#include "yaffs_tagsvalidity.h"
13460 +#include "yaffs_getblockinfo.h"
13462 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
13463 + __u8 *buffer,
13464 + yaffs_ExtendedTags *tags)
13466 + int result;
13467 + yaffs_ExtendedTags localTags;
13469 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13471 + /* If there are no tags provided, use local tags to get prioritised gc working */
13472 + if (!tags)
13473 + tags = &localTags;
13475 + if (dev->readChunkWithTagsFromNAND)
13476 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13477 + tags);
13478 + else
13479 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13480 + realignedChunkInNAND,
13481 + buffer,
13482 + tags);
13483 + if (tags &&
13484 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR) {
13486 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13487 + yaffs_HandleChunkError(dev, bi);
13490 + return result;
13493 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
13494 + int chunkInNAND,
13495 + const __u8 *buffer,
13496 + yaffs_ExtendedTags *tags)
13498 + chunkInNAND -= dev->chunkOffset;
13501 + if (tags) {
13502 + tags->sequenceNumber = dev->sequenceNumber;
13503 + tags->chunkUsed = 1;
13504 + if (!yaffs_ValidateTags(tags)) {
13505 + T(YAFFS_TRACE_ERROR,
13506 + (TSTR("Writing uninitialised tags" TENDSTR)));
13507 + YBUG();
13509 + T(YAFFS_TRACE_WRITE,
13510 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13511 + tags->objectId, tags->chunkId));
13512 + } else {
13513 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13514 + YBUG();
13517 + if (dev->writeChunkWithTagsToNAND)
13518 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13519 + tags);
13520 + else
13521 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13522 + chunkInNAND,
13523 + buffer,
13524 + tags);
13527 +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo)
13529 + blockNo -= dev->blockOffset;
13532 + if (dev->markNANDBlockBad)
13533 + return dev->markNANDBlockBad(dev, blockNo);
13534 + else
13535 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13538 +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
13539 + int blockNo,
13540 + yaffs_BlockState *state,
13541 + __u32 *sequenceNumber)
13543 + blockNo -= dev->blockOffset;
13545 + if (dev->queryNANDBlock)
13546 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13547 + else
13548 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13549 + state,
13550 + sequenceNumber);
13554 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13555 + int blockInNAND)
13557 + int result;
13559 + blockInNAND -= dev->blockOffset;
13562 + dev->nBlockErasures++;
13563 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13565 + return result;
13568 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13570 + return dev->initialiseNAND(dev);
13575 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.30/fs/yaffs2/yaffs_nandemul2k.h
13576 --- linux-2.6.30.orig/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
13577 +++ linux-2.6.30/fs/yaffs2/yaffs_nandemul2k.h 2009-06-11 09:21:04.000000000 +0200
13578 @@ -0,0 +1,39 @@
13580 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13582 + * Copyright (C) 2002-2007 Aleph One Ltd.
13583 + * for Toby Churchill Ltd and Brightstar Engineering
13585 + * Created by Charles Manning <charles@aleph1.co.uk>
13587 + * This program is free software; you can redistribute it and/or modify
13588 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13589 + * published by the Free Software Foundation.
13591 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13592 + */
13594 +/* Interface to emulated NAND functions (2k page size) */
13596 +#ifndef __YAFFS_NANDEMUL2K_H__
13597 +#define __YAFFS_NANDEMUL2K_H__
13599 +#include "yaffs_guts.h"
13601 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13602 + int chunkInNAND, const __u8 *data,
13603 + const yaffs_ExtendedTags *tags);
13604 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13605 + int chunkInNAND, __u8 *data,
13606 + yaffs_ExtendedTags *tags);
13607 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13608 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13609 + yaffs_BlockState *state, __u32 *sequenceNumber);
13610 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13611 + int blockInNAND);
13612 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13613 +int nandemul2k_GetBytesPerChunk(void);
13614 +int nandemul2k_GetChunksPerBlock(void);
13615 +int nandemul2k_GetNumberOfBlocks(void);
13617 +#endif
13618 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_nand.h linux-2.6.30/fs/yaffs2/yaffs_nand.h
13619 --- linux-2.6.30.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
13620 +++ linux-2.6.30/fs/yaffs2/yaffs_nand.h 2009-06-11 09:21:04.000000000 +0200
13621 @@ -0,0 +1,44 @@
13623 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13625 + * Copyright (C) 2002-2007 Aleph One Ltd.
13626 + * for Toby Churchill Ltd and Brightstar Engineering
13628 + * Created by Charles Manning <charles@aleph1.co.uk>
13630 + * This program is free software; you can redistribute it and/or modify
13631 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13632 + * published by the Free Software Foundation.
13634 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13635 + */
13637 +#ifndef __YAFFS_NAND_H__
13638 +#define __YAFFS_NAND_H__
13639 +#include "yaffs_guts.h"
13643 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device *dev, int chunkInNAND,
13644 + __u8 *buffer,
13645 + yaffs_ExtendedTags *tags);
13647 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,
13648 + int chunkInNAND,
13649 + const __u8 *buffer,
13650 + yaffs_ExtendedTags *tags);
13652 +int yaffs_MarkBlockBad(yaffs_Device *dev, int blockNo);
13654 +int yaffs_QueryInitialBlockState(yaffs_Device *dev,
13655 + int blockNo,
13656 + yaffs_BlockState *state,
13657 + unsigned *sequenceNumber);
13659 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13660 + int blockInNAND);
13662 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13664 +#endif
13666 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags1.c linux-2.6.30/fs/yaffs2/yaffs_packedtags1.c
13667 --- linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
13668 +++ linux-2.6.30/fs/yaffs2/yaffs_packedtags1.c 2009-06-11 09:21:04.000000000 +0200
13669 @@ -0,0 +1,50 @@
13671 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13673 + * Copyright (C) 2002-2007 Aleph One Ltd.
13674 + * for Toby Churchill Ltd and Brightstar Engineering
13676 + * Created by Charles Manning <charles@aleph1.co.uk>
13678 + * This program is free software; you can redistribute it and/or modify
13679 + * it under the terms of the GNU General Public License version 2 as
13680 + * published by the Free Software Foundation.
13681 + */
13683 +#include "yaffs_packedtags1.h"
13684 +#include "yportenv.h"
13686 +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t)
13688 + pt->chunkId = t->chunkId;
13689 + pt->serialNumber = t->serialNumber;
13690 + pt->byteCount = t->byteCount;
13691 + pt->objectId = t->objectId;
13692 + pt->ecc = 0;
13693 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13694 + pt->unusedStuff = 0;
13695 + pt->shouldBeFF = 0xFFFFFFFF;
13699 +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt)
13701 + static const __u8 allFF[] =
13702 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13703 +0xff };
13705 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13706 + t->blockBad = 0;
13707 + if (pt->shouldBeFF != 0xFFFFFFFF)
13708 + t->blockBad = 1;
13709 + t->chunkUsed = 1;
13710 + t->objectId = pt->objectId;
13711 + t->chunkId = pt->chunkId;
13712 + t->byteCount = pt->byteCount;
13713 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13714 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13715 + t->serialNumber = pt->serialNumber;
13716 + } else {
13717 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13720 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags1.h linux-2.6.30/fs/yaffs2/yaffs_packedtags1.h
13721 --- linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
13722 +++ linux-2.6.30/fs/yaffs2/yaffs_packedtags1.h 2009-06-11 09:21:04.000000000 +0200
13723 @@ -0,0 +1,37 @@
13725 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13727 + * Copyright (C) 2002-2007 Aleph One Ltd.
13728 + * for Toby Churchill Ltd and Brightstar Engineering
13730 + * Created by Charles Manning <charles@aleph1.co.uk>
13732 + * This program is free software; you can redistribute it and/or modify
13733 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13734 + * published by the Free Software Foundation.
13736 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13737 + */
13739 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13741 +#ifndef __YAFFS_PACKEDTAGS1_H__
13742 +#define __YAFFS_PACKEDTAGS1_H__
13744 +#include "yaffs_guts.h"
13746 +typedef struct {
13747 + unsigned chunkId:20;
13748 + unsigned serialNumber:2;
13749 + unsigned byteCount:10;
13750 + unsigned objectId:18;
13751 + unsigned ecc:12;
13752 + unsigned deleted:1;
13753 + unsigned unusedStuff:1;
13754 + unsigned shouldBeFF;
13756 +} yaffs_PackedTags1;
13758 +void yaffs_PackTags1(yaffs_PackedTags1 *pt, const yaffs_ExtendedTags *t);
13759 +void yaffs_UnpackTags1(yaffs_ExtendedTags *t, const yaffs_PackedTags1 *pt);
13760 +#endif
13761 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags2.c linux-2.6.30/fs/yaffs2/yaffs_packedtags2.c
13762 --- linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
13763 +++ linux-2.6.30/fs/yaffs2/yaffs_packedtags2.c 2009-06-11 09:21:04.000000000 +0200
13764 @@ -0,0 +1,206 @@
13766 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13768 + * Copyright (C) 2002-2007 Aleph One Ltd.
13769 + * for Toby Churchill Ltd and Brightstar Engineering
13771 + * Created by Charles Manning <charles@aleph1.co.uk>
13773 + * This program is free software; you can redistribute it and/or modify
13774 + * it under the terms of the GNU General Public License version 2 as
13775 + * published by the Free Software Foundation.
13776 + */
13778 +#include "yaffs_packedtags2.h"
13779 +#include "yportenv.h"
13780 +#include "yaffs_tagsvalidity.h"
13782 +/* This code packs a set of extended tags into a binary structure for
13783 + * NAND storage
13784 + */
13786 +/* Some of the information is "extra" struff which can be packed in to
13787 + * speed scanning
13788 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13789 + */
13791 +/* Extra flags applied to chunkId */
13793 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13794 +#define EXTRA_SHRINK_FLAG 0x40000000
13795 +#define EXTRA_SHADOWS_FLAG 0x20000000
13796 +#define EXTRA_SPARE_FLAGS 0x10000000
13798 +#define ALL_EXTRA_FLAGS 0xF0000000
13800 +/* Also, the top 4 bits of the object Id are set to the object type. */
13801 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13802 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13805 +static void yaffs_DumpPackedTags2TagsPart(const yaffs_PackedTags2TagsPart *ptt)
13807 + T(YAFFS_TRACE_MTD,
13808 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13809 + ptt->objectId, ptt->chunkId, ptt->byteCount,
13810 + ptt->sequenceNumber));
13812 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 *pt)
13814 + yaffs_DumpPackedTags2TagsPart(&pt->t);
13817 +static void yaffs_DumpTags2(const yaffs_ExtendedTags *t)
13819 + T(YAFFS_TRACE_MTD,
13820 + (TSTR
13821 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d"
13822 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13823 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13824 + t->sequenceNumber));
13828 +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *ptt,
13829 + const yaffs_ExtendedTags *t)
13831 + ptt->chunkId = t->chunkId;
13832 + ptt->sequenceNumber = t->sequenceNumber;
13833 + ptt->byteCount = t->byteCount;
13834 + ptt->objectId = t->objectId;
13836 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13837 + /* Store the extra header info instead */
13838 + /* We save the parent object in the chunkId */
13839 + ptt->chunkId = EXTRA_HEADER_INFO_FLAG
13840 + | t->extraParentObjectId;
13841 + if (t->extraIsShrinkHeader)
13842 + ptt->chunkId |= EXTRA_SHRINK_FLAG;
13843 + if (t->extraShadows)
13844 + ptt->chunkId |= EXTRA_SHADOWS_FLAG;
13846 + ptt->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13847 + ptt->objectId |=
13848 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13850 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
13851 + ptt->byteCount = t->extraEquivalentObjectId;
13852 + else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE)
13853 + ptt->byteCount = t->extraFileLength;
13854 + else
13855 + ptt->byteCount = 0;
13858 + yaffs_DumpPackedTags2TagsPart(ptt);
13859 + yaffs_DumpTags2(t);
13863 +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t)
13865 + yaffs_PackTags2TagsPart(&pt->t, t);
13867 +#ifndef YAFFS_IGNORE_TAGS_ECC
13869 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13870 + sizeof(yaffs_PackedTags2TagsPart),
13871 + &pt->ecc);
13873 +#endif
13877 +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t,
13878 + yaffs_PackedTags2TagsPart *ptt)
13881 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13883 + yaffs_InitialiseTags(t);
13885 + if (ptt->sequenceNumber != 0xFFFFFFFF) {
13886 + t->blockBad = 0;
13887 + t->chunkUsed = 1;
13888 + t->objectId = ptt->objectId;
13889 + t->chunkId = ptt->chunkId;
13890 + t->byteCount = ptt->byteCount;
13891 + t->chunkDeleted = 0;
13892 + t->serialNumber = 0;
13893 + t->sequenceNumber = ptt->sequenceNumber;
13895 + /* Do extra header info stuff */
13897 + if (ptt->chunkId & EXTRA_HEADER_INFO_FLAG) {
13898 + t->chunkId = 0;
13899 + t->byteCount = 0;
13901 + t->extraHeaderInfoAvailable = 1;
13902 + t->extraParentObjectId =
13903 + ptt->chunkId & (~(ALL_EXTRA_FLAGS));
13904 + t->extraIsShrinkHeader =
13905 + (ptt->chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13906 + t->extraShadows =
13907 + (ptt->chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13908 + t->extraObjectType =
13909 + ptt->objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13910 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13912 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK)
13913 + t->extraEquivalentObjectId = ptt->byteCount;
13914 + else
13915 + t->extraFileLength = ptt->byteCount;
13919 + yaffs_DumpPackedTags2TagsPart(ptt);
13920 + yaffs_DumpTags2(t);
13925 +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt)
13928 + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13930 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13931 + /* Page is in use */
13932 +#ifndef YAFFS_IGNORE_TAGS_ECC
13934 + yaffs_ECCOther ecc;
13935 + int result;
13936 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13937 + sizeof
13938 + (yaffs_PackedTags2TagsPart),
13939 + &ecc);
13940 + result =
13941 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13942 + sizeof
13943 + (yaffs_PackedTags2TagsPart),
13944 + &pt->ecc, &ecc);
13945 + switch (result) {
13946 + case 0:
13947 + eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13948 + break;
13949 + case 1:
13950 + eccResult = YAFFS_ECC_RESULT_FIXED;
13951 + break;
13952 + case -1:
13953 + eccResult = YAFFS_ECC_RESULT_UNFIXED;
13954 + break;
13955 + default:
13956 + eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13959 +#endif
13962 + yaffs_UnpackTags2TagsPart(t, &pt->t);
13964 + t->eccResult = eccResult;
13966 + yaffs_DumpPackedTags2(pt);
13967 + yaffs_DumpTags2(t);
13971 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags2.h linux-2.6.30/fs/yaffs2/yaffs_packedtags2.h
13972 --- linux-2.6.30.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
13973 +++ linux-2.6.30/fs/yaffs2/yaffs_packedtags2.h 2009-06-11 09:21:04.000000000 +0200
13974 @@ -0,0 +1,43 @@
13976 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13978 + * Copyright (C) 2002-2007 Aleph One Ltd.
13979 + * for Toby Churchill Ltd and Brightstar Engineering
13981 + * Created by Charles Manning <charles@aleph1.co.uk>
13983 + * This program is free software; you can redistribute it and/or modify
13984 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13985 + * published by the Free Software Foundation.
13987 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13988 + */
13990 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13992 +#ifndef __YAFFS_PACKEDTAGS2_H__
13993 +#define __YAFFS_PACKEDTAGS2_H__
13995 +#include "yaffs_guts.h"
13996 +#include "yaffs_ecc.h"
13998 +typedef struct {
13999 + unsigned sequenceNumber;
14000 + unsigned objectId;
14001 + unsigned chunkId;
14002 + unsigned byteCount;
14003 +} yaffs_PackedTags2TagsPart;
14005 +typedef struct {
14006 + yaffs_PackedTags2TagsPart t;
14007 + yaffs_ECCOther ecc;
14008 +} yaffs_PackedTags2;
14010 +/* Full packed tags with ECC, used for oob tags */
14011 +void yaffs_PackTags2(yaffs_PackedTags2 *pt, const yaffs_ExtendedTags *t);
14012 +void yaffs_UnpackTags2(yaffs_ExtendedTags *t, yaffs_PackedTags2 *pt);
14014 +/* Only the tags part (no ECC for use with inband tags */
14015 +void yaffs_PackTags2TagsPart(yaffs_PackedTags2TagsPart *pt, const yaffs_ExtendedTags *t);
14016 +void yaffs_UnpackTags2TagsPart(yaffs_ExtendedTags *t, yaffs_PackedTags2TagsPart *pt);
14017 +#endif
14018 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_qsort.c linux-2.6.30/fs/yaffs2/yaffs_qsort.c
14019 --- linux-2.6.30.orig/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
14020 +++ linux-2.6.30/fs/yaffs2/yaffs_qsort.c 2009-06-11 09:21:04.000000000 +0200
14021 @@ -0,0 +1,163 @@
14023 + * Copyright (c) 1992, 1993
14024 + * The Regents of the University of California. All rights reserved.
14026 + * Redistribution and use in source and binary forms, with or without
14027 + * modification, are permitted provided that the following conditions
14028 + * are met:
14029 + * 1. Redistributions of source code must retain the above copyright
14030 + * notice, this list of conditions and the following disclaimer.
14031 + * 2. Redistributions in binary form must reproduce the above copyright
14032 + * notice, this list of conditions and the following disclaimer in the
14033 + * documentation and/or other materials provided with the distribution.
14034 + * 3. Neither the name of the University nor the names of its contributors
14035 + * may be used to endorse or promote products derived from this software
14036 + * without specific prior written permission.
14038 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14039 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14040 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14041 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14042 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14043 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14044 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14045 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14046 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14047 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14048 + * SUCH DAMAGE.
14049 + */
14051 +#include "yportenv.h"
14052 +/* #include <linux/string.h> */
14055 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
14056 + */
14057 +#define swapcode(TYPE, parmi, parmj, n) do { \
14058 + long i = (n) / sizeof (TYPE); \
14059 + register TYPE *pi = (TYPE *) (parmi); \
14060 + register TYPE *pj = (TYPE *) (parmj); \
14061 + do { \
14062 + register TYPE t = *pi; \
14063 + *pi++ = *pj; \
14064 + *pj++ = t; \
14065 + } while (--i > 0); \
14066 +} while (0)
14068 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
14069 + es % sizeof(long) ? 2 : es == sizeof(long) ? 0 : 1;
14071 +static __inline void
14072 +swapfunc(char *a, char *b, int n, int swaptype)
14074 + if (swaptype <= 1)
14075 + swapcode(long, a, b, n);
14076 + else
14077 + swapcode(char, a, b, n);
14080 +#define yswap(a, b) do { \
14081 + if (swaptype == 0) { \
14082 + long t = *(long *)(a); \
14083 + *(long *)(a) = *(long *)(b); \
14084 + *(long *)(b) = t; \
14085 + } else \
14086 + swapfunc(a, b, es, swaptype); \
14087 +} while (0)
14089 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14091 +static __inline char *
14092 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14094 + return cmp(a, b) < 0 ?
14095 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a))
14096 + : (cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c));
14099 +#ifndef min
14100 +#define min(a, b) (((a) < (b)) ? (a) : (b))
14101 +#endif
14103 +void
14104 +yaffs_qsort(void *aa, size_t n, size_t es,
14105 + int (*cmp)(const void *, const void *))
14107 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14108 + int d, r, swaptype, swap_cnt;
14109 + register char *a = aa;
14111 +loop: SWAPINIT(a, es);
14112 + swap_cnt = 0;
14113 + if (n < 7) {
14114 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14115 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14116 + pl -= es)
14117 + yswap(pl, pl - es);
14118 + return;
14120 + pm = (char *)a + (n / 2) * es;
14121 + if (n > 7) {
14122 + pl = (char *)a;
14123 + pn = (char *)a + (n - 1) * es;
14124 + if (n > 40) {
14125 + d = (n / 8) * es;
14126 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14127 + pm = med3(pm - d, pm, pm + d, cmp);
14128 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14130 + pm = med3(pl, pm, pn, cmp);
14132 + yswap(a, pm);
14133 + pa = pb = (char *)a + es;
14135 + pc = pd = (char *)a + (n - 1) * es;
14136 + for (;;) {
14137 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14138 + if (r == 0) {
14139 + swap_cnt = 1;
14140 + yswap(pa, pb);
14141 + pa += es;
14143 + pb += es;
14145 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14146 + if (r == 0) {
14147 + swap_cnt = 1;
14148 + yswap(pc, pd);
14149 + pd -= es;
14151 + pc -= es;
14153 + if (pb > pc)
14154 + break;
14155 + yswap(pb, pc);
14156 + swap_cnt = 1;
14157 + pb += es;
14158 + pc -= es;
14160 + if (swap_cnt == 0) { /* Switch to insertion sort */
14161 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14162 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14163 + pl -= es)
14164 + yswap(pl, pl - es);
14165 + return;
14168 + pn = (char *)a + n * es;
14169 + r = min(pa - (char *)a, pb - pa);
14170 + vecswap(a, pb - r, r);
14171 + r = min((long)(pd - pc), (long)(pn - pd - es));
14172 + vecswap(pb, pn - r, r);
14173 + r = pb - pa;
14174 + if (r > es)
14175 + yaffs_qsort(a, r / es, es, cmp);
14176 + r = pd - pc;
14177 + if (r > es) {
14178 + /* Iterate rather than recurse to save stack space */
14179 + a = pn - r;
14180 + n = r / es;
14181 + goto loop;
14183 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14185 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_qsort.h linux-2.6.30/fs/yaffs2/yaffs_qsort.h
14186 --- linux-2.6.30.orig/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
14187 +++ linux-2.6.30/fs/yaffs2/yaffs_qsort.h 2009-06-11 09:21:04.000000000 +0200
14188 @@ -0,0 +1,23 @@
14190 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14192 + * Copyright (C) 2002-2007 Aleph One Ltd.
14193 + * for Toby Churchill Ltd and Brightstar Engineering
14195 + * Created by Charles Manning <charles@aleph1.co.uk>
14197 + * This program is free software; you can redistribute it and/or modify
14198 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14199 + * published by the Free Software Foundation.
14201 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14202 + */
14205 +#ifndef __YAFFS_QSORT_H__
14206 +#define __YAFFS_QSORT_H__
14208 +extern void yaffs_qsort(void *const base, size_t total_elems, size_t size,
14209 + int (*cmp)(const void *, const void *));
14211 +#endif
14212 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_tagscompat.c linux-2.6.30/fs/yaffs2/yaffs_tagscompat.c
14213 --- linux-2.6.30.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
14214 +++ linux-2.6.30/fs/yaffs2/yaffs_tagscompat.c 2009-06-11 09:21:04.000000000 +0200
14215 @@ -0,0 +1,541 @@
14217 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14219 + * Copyright (C) 2002-2007 Aleph One Ltd.
14220 + * for Toby Churchill Ltd and Brightstar Engineering
14222 + * Created by Charles Manning <charles@aleph1.co.uk>
14224 + * This program is free software; you can redistribute it and/or modify
14225 + * it under the terms of the GNU General Public License version 2 as
14226 + * published by the Free Software Foundation.
14227 + */
14229 +#include "yaffs_guts.h"
14230 +#include "yaffs_tagscompat.h"
14231 +#include "yaffs_ecc.h"
14232 +#include "yaffs_getblockinfo.h"
14234 +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND);
14235 +#ifdef NOTYET
14236 +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND);
14237 +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
14238 + const __u8 *data,
14239 + const yaffs_Spare *spare);
14240 +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
14241 + const yaffs_Spare *spare);
14242 +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND);
14243 +#endif
14245 +static const char yaffs_countBitsTable[256] = {
14246 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14247 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14248 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14249 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14250 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14251 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14252 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14253 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14254 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14255 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14256 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14257 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14258 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14259 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14260 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14261 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14264 +int yaffs_CountBits(__u8 x)
14266 + int retVal;
14267 + retVal = yaffs_countBitsTable[x];
14268 + return retVal;
14271 +/********** Tags ECC calculations *********/
14273 +void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare)
14275 + yaffs_ECCCalculate(data, spare->ecc1);
14276 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14279 +void yaffs_CalcTagsECC(yaffs_Tags *tags)
14281 + /* Calculate an ecc */
14283 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14284 + unsigned i, j;
14285 + unsigned ecc = 0;
14286 + unsigned bit = 0;
14288 + tags->ecc = 0;
14290 + for (i = 0; i < 8; i++) {
14291 + for (j = 1; j & 0xff; j <<= 1) {
14292 + bit++;
14293 + if (b[i] & j)
14294 + ecc ^= bit;
14298 + tags->ecc = ecc;
14302 +int yaffs_CheckECCOnTags(yaffs_Tags *tags)
14304 + unsigned ecc = tags->ecc;
14306 + yaffs_CalcTagsECC(tags);
14308 + ecc ^= tags->ecc;
14310 + if (ecc && ecc <= 64) {
14311 + /* TODO: Handle the failure better. Retire? */
14312 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14314 + ecc--;
14316 + b[ecc / 8] ^= (1 << (ecc & 7));
14318 + /* Now recvalc the ecc */
14319 + yaffs_CalcTagsECC(tags);
14321 + return 1; /* recovered error */
14322 + } else if (ecc) {
14323 + /* Wierd ecc failure value */
14324 + /* TODO Need to do somethiong here */
14325 + return -1; /* unrecovered error */
14328 + return 0;
14331 +/********** Tags **********/
14333 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr,
14334 + yaffs_Tags *tagsPtr)
14336 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14338 + yaffs_CalcTagsECC(tagsPtr);
14340 + sparePtr->tagByte0 = tu->asBytes[0];
14341 + sparePtr->tagByte1 = tu->asBytes[1];
14342 + sparePtr->tagByte2 = tu->asBytes[2];
14343 + sparePtr->tagByte3 = tu->asBytes[3];
14344 + sparePtr->tagByte4 = tu->asBytes[4];
14345 + sparePtr->tagByte5 = tu->asBytes[5];
14346 + sparePtr->tagByte6 = tu->asBytes[6];
14347 + sparePtr->tagByte7 = tu->asBytes[7];
14350 +static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,
14351 + yaffs_Tags *tagsPtr)
14353 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14354 + int result;
14356 + tu->asBytes[0] = sparePtr->tagByte0;
14357 + tu->asBytes[1] = sparePtr->tagByte1;
14358 + tu->asBytes[2] = sparePtr->tagByte2;
14359 + tu->asBytes[3] = sparePtr->tagByte3;
14360 + tu->asBytes[4] = sparePtr->tagByte4;
14361 + tu->asBytes[5] = sparePtr->tagByte5;
14362 + tu->asBytes[6] = sparePtr->tagByte6;
14363 + tu->asBytes[7] = sparePtr->tagByte7;
14365 + result = yaffs_CheckECCOnTags(tagsPtr);
14366 + if (result > 0)
14367 + dev->tagsEccFixed++;
14368 + else if (result < 0)
14369 + dev->tagsEccUnfixed++;
14372 +static void yaffs_SpareInitialise(yaffs_Spare *spare)
14374 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14377 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14378 + int chunkInNAND, const __u8 *data,
14379 + yaffs_Spare *spare)
14381 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14382 + T(YAFFS_TRACE_ERROR,
14383 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14384 + chunkInNAND));
14385 + return YAFFS_FAIL;
14388 + dev->nPageWrites++;
14389 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14392 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14393 + int chunkInNAND,
14394 + __u8 *data,
14395 + yaffs_Spare *spare,
14396 + yaffs_ECCResult *eccResult,
14397 + int doErrorCorrection)
14399 + int retVal;
14400 + yaffs_Spare localSpare;
14402 + dev->nPageReads++;
14404 + if (!spare && data) {
14405 + /* If we don't have a real spare, then we use a local one. */
14406 + /* Need this for the calculation of the ecc */
14407 + spare = &localSpare;
14410 + if (!dev->useNANDECC) {
14411 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14412 + if (data && doErrorCorrection) {
14413 + /* Do ECC correction */
14414 + /* Todo handle any errors */
14415 + int eccResult1, eccResult2;
14416 + __u8 calcEcc[3];
14418 + yaffs_ECCCalculate(data, calcEcc);
14419 + eccResult1 =
14420 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14421 + yaffs_ECCCalculate(&data[256], calcEcc);
14422 + eccResult2 =
14423 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14425 + if (eccResult1 > 0) {
14426 + T(YAFFS_TRACE_ERROR,
14427 + (TSTR
14428 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14429 + TENDSTR), chunkInNAND));
14430 + dev->eccFixed++;
14431 + } else if (eccResult1 < 0) {
14432 + T(YAFFS_TRACE_ERROR,
14433 + (TSTR
14434 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14435 + TENDSTR), chunkInNAND));
14436 + dev->eccUnfixed++;
14439 + if (eccResult2 > 0) {
14440 + T(YAFFS_TRACE_ERROR,
14441 + (TSTR
14442 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14443 + TENDSTR), chunkInNAND));
14444 + dev->eccFixed++;
14445 + } else if (eccResult2 < 0) {
14446 + T(YAFFS_TRACE_ERROR,
14447 + (TSTR
14448 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14449 + TENDSTR), chunkInNAND));
14450 + dev->eccUnfixed++;
14453 + if (eccResult1 || eccResult2) {
14454 + /* We had a data problem on this page */
14455 + yaffs_HandleReadDataError(dev, chunkInNAND);
14458 + if (eccResult1 < 0 || eccResult2 < 0)
14459 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14460 + else if (eccResult1 > 0 || eccResult2 > 0)
14461 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14462 + else
14463 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14465 + } else {
14466 + /* Must allocate enough memory for spare+2*sizeof(int) */
14467 + /* for ecc results from device. */
14468 + struct yaffs_NANDSpare nspare;
14470 + memset(&nspare, 0, sizeof(nspare));
14472 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data,
14473 + (yaffs_Spare *) &nspare);
14474 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14475 + if (data && doErrorCorrection) {
14476 + if (nspare.eccres1 > 0) {
14477 + T(YAFFS_TRACE_ERROR,
14478 + (TSTR
14479 + ("**>>mtd ecc error fix performed on chunk %d:0"
14480 + TENDSTR), chunkInNAND));
14481 + } else if (nspare.eccres1 < 0) {
14482 + T(YAFFS_TRACE_ERROR,
14483 + (TSTR
14484 + ("**>>mtd ecc error unfixed on chunk %d:0"
14485 + TENDSTR), chunkInNAND));
14488 + if (nspare.eccres2 > 0) {
14489 + T(YAFFS_TRACE_ERROR,
14490 + (TSTR
14491 + ("**>>mtd ecc error fix performed on chunk %d:1"
14492 + TENDSTR), chunkInNAND));
14493 + } else if (nspare.eccres2 < 0) {
14494 + T(YAFFS_TRACE_ERROR,
14495 + (TSTR
14496 + ("**>>mtd ecc error unfixed on chunk %d:1"
14497 + TENDSTR), chunkInNAND));
14500 + if (nspare.eccres1 || nspare.eccres2) {
14501 + /* We had a data problem on this page */
14502 + yaffs_HandleReadDataError(dev, chunkInNAND);
14505 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14506 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14507 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14508 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14509 + else
14510 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14514 + return retVal;
14517 +#ifdef NOTYET
14518 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14519 + int chunkInNAND)
14521 + static int init;
14522 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14523 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14524 + /* Might as well always allocate the larger size for */
14525 + /* dev->useNANDECC == true; */
14526 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14528 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14530 + if (!init) {
14531 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14532 + init = 1;
14535 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14536 + return YAFFS_FAIL;
14537 + if (memcmp(cmpbuf, spare, 16))
14538 + return YAFFS_FAIL;
14540 + return YAFFS_OK;
14543 +#endif
14546 + * Functions for robustisizing
14547 + */
14549 +static void yaffs_HandleReadDataError(yaffs_Device *dev, int chunkInNAND)
14551 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14553 + /* Mark the block for retirement */
14554 + yaffs_GetBlockInfo(dev, blockInNAND + dev->blockOffset)->needsRetiring = 1;
14555 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14556 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14558 + /* TODO:
14559 + * Just do a garbage collection on the affected block
14560 + * then retire the block
14561 + * NB recursion
14562 + */
14565 +#ifdef NOTYET
14566 +static void yaffs_CheckWrittenBlock(yaffs_Device *dev, int chunkInNAND)
14570 +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev, int chunkInNAND,
14571 + const __u8 *data,
14572 + const yaffs_Spare *spare)
14576 +static void yaffs_HandleUpdateChunk(yaffs_Device *dev, int chunkInNAND,
14577 + const yaffs_Spare *spare)
14581 +static void yaffs_HandleWriteChunkError(yaffs_Device *dev, int chunkInNAND)
14583 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14585 + /* Mark the block for retirement */
14586 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14587 + /* Delete the chunk */
14588 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14591 +static int yaffs_VerifyCompare(const __u8 *d0, const __u8 *d1,
14592 + const yaffs_Spare *s0, const yaffs_Spare *s1)
14595 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14596 + s0->tagByte0 != s1->tagByte0 ||
14597 + s0->tagByte1 != s1->tagByte1 ||
14598 + s0->tagByte2 != s1->tagByte2 ||
14599 + s0->tagByte3 != s1->tagByte3 ||
14600 + s0->tagByte4 != s1->tagByte4 ||
14601 + s0->tagByte5 != s1->tagByte5 ||
14602 + s0->tagByte6 != s1->tagByte6 ||
14603 + s0->tagByte7 != s1->tagByte7 ||
14604 + s0->ecc1[0] != s1->ecc1[0] ||
14605 + s0->ecc1[1] != s1->ecc1[1] ||
14606 + s0->ecc1[2] != s1->ecc1[2] ||
14607 + s0->ecc2[0] != s1->ecc2[0] ||
14608 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14609 + return 0;
14612 + return 1;
14614 +#endif /* NOTYET */
14616 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
14617 + int chunkInNAND,
14618 + const __u8 *data,
14619 + const yaffs_ExtendedTags *eTags)
14621 + yaffs_Spare spare;
14622 + yaffs_Tags tags;
14624 + yaffs_SpareInitialise(&spare);
14626 + if (eTags->chunkDeleted)
14627 + spare.pageStatus = 0;
14628 + else {
14629 + tags.objectId = eTags->objectId;
14630 + tags.chunkId = eTags->chunkId;
14632 + tags.byteCountLSB = eTags->byteCount & 0x3ff;
14634 + if (dev->nDataBytesPerChunk >= 1024)
14635 + tags.byteCountMSB = (eTags->byteCount >> 10) & 3;
14636 + else
14637 + tags.byteCountMSB = 3;
14640 + tags.serialNumber = eTags->serialNumber;
14642 + if (!dev->useNANDECC && data)
14643 + yaffs_CalcECC(data, &spare);
14645 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14649 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14652 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
14653 + int chunkInNAND,
14654 + __u8 *data,
14655 + yaffs_ExtendedTags *eTags)
14658 + yaffs_Spare spare;
14659 + yaffs_Tags tags;
14660 + yaffs_ECCResult eccResult = YAFFS_ECC_RESULT_UNKNOWN;
14662 + static yaffs_Spare spareFF;
14663 + static int init;
14665 + if (!init) {
14666 + memset(&spareFF, 0xFF, sizeof(spareFF));
14667 + init = 1;
14670 + if (yaffs_ReadChunkFromNAND
14671 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14672 + /* eTags may be NULL */
14673 + if (eTags) {
14675 + int deleted =
14676 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14678 + eTags->chunkDeleted = deleted;
14679 + eTags->eccResult = eccResult;
14680 + eTags->blockBad = 0; /* We're reading it */
14681 + /* therefore it is not a bad block */
14682 + eTags->chunkUsed =
14683 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14684 + 0) ? 1 : 0;
14686 + if (eTags->chunkUsed) {
14687 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14689 + eTags->objectId = tags.objectId;
14690 + eTags->chunkId = tags.chunkId;
14691 + eTags->byteCount = tags.byteCountLSB;
14693 + if (dev->nDataBytesPerChunk >= 1024)
14694 + eTags->byteCount |= (((unsigned) tags.byteCountMSB) << 10);
14696 + eTags->serialNumber = tags.serialNumber;
14700 + return YAFFS_OK;
14701 + } else {
14702 + return YAFFS_FAIL;
14706 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14707 + int blockInNAND)
14710 + yaffs_Spare spare;
14712 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14714 + spare.blockStatus = 'Y';
14716 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14717 + &spare);
14718 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14719 + NULL, &spare);
14721 + return YAFFS_OK;
14725 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14726 + int blockNo,
14727 + yaffs_BlockState *state,
14728 + __u32 *sequenceNumber)
14731 + yaffs_Spare spare0, spare1;
14732 + static yaffs_Spare spareFF;
14733 + static int init;
14734 + yaffs_ECCResult dummy;
14736 + if (!init) {
14737 + memset(&spareFF, 0xFF, sizeof(spareFF));
14738 + init = 1;
14741 + *sequenceNumber = 0;
14743 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14744 + &spare0, &dummy, 1);
14745 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14746 + &spare1, &dummy, 1);
14748 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14749 + *state = YAFFS_BLOCK_STATE_DEAD;
14750 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14751 + *state = YAFFS_BLOCK_STATE_EMPTY;
14752 + else
14753 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14755 + return YAFFS_OK;
14757 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_tagscompat.h linux-2.6.30/fs/yaffs2/yaffs_tagscompat.h
14758 --- linux-2.6.30.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
14759 +++ linux-2.6.30/fs/yaffs2/yaffs_tagscompat.h 2009-06-11 09:21:04.000000000 +0200
14760 @@ -0,0 +1,39 @@
14762 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14764 + * Copyright (C) 2002-2007 Aleph One Ltd.
14765 + * for Toby Churchill Ltd and Brightstar Engineering
14767 + * Created by Charles Manning <charles@aleph1.co.uk>
14769 + * This program is free software; you can redistribute it and/or modify
14770 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14771 + * published by the Free Software Foundation.
14773 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14774 + */
14776 +#ifndef __YAFFS_TAGSCOMPAT_H__
14777 +#define __YAFFS_TAGSCOMPAT_H__
14779 +#include "yaffs_guts.h"
14780 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device *dev,
14781 + int chunkInNAND,
14782 + const __u8 *data,
14783 + const yaffs_ExtendedTags *tags);
14784 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device *dev,
14785 + int chunkInNAND,
14786 + __u8 *data,
14787 + yaffs_ExtendedTags *tags);
14788 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14789 + int blockNo);
14790 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14791 + int blockNo,
14792 + yaffs_BlockState *state,
14793 + __u32 *sequenceNumber);
14795 +void yaffs_CalcTagsECC(yaffs_Tags *tags);
14796 +int yaffs_CheckECCOnTags(yaffs_Tags *tags);
14797 +int yaffs_CountBits(__u8 byte);
14799 +#endif
14800 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.30/fs/yaffs2/yaffs_tagsvalidity.c
14801 --- linux-2.6.30.orig/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
14802 +++ linux-2.6.30/fs/yaffs2/yaffs_tagsvalidity.c 2009-06-11 09:21:04.000000000 +0200
14803 @@ -0,0 +1,28 @@
14805 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14807 + * Copyright (C) 2002-2007 Aleph One Ltd.
14808 + * for Toby Churchill Ltd and Brightstar Engineering
14810 + * Created by Charles Manning <charles@aleph1.co.uk>
14812 + * This program is free software; you can redistribute it and/or modify
14813 + * it under the terms of the GNU General Public License version 2 as
14814 + * published by the Free Software Foundation.
14815 + */
14817 +#include "yaffs_tagsvalidity.h"
14819 +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags)
14821 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14822 + tags->validMarker0 = 0xAAAAAAAA;
14823 + tags->validMarker1 = 0x55555555;
14826 +int yaffs_ValidateTags(yaffs_ExtendedTags *tags)
14828 + return (tags->validMarker0 == 0xAAAAAAAA &&
14829 + tags->validMarker1 == 0x55555555);
14832 diff -Nur linux-2.6.30.orig/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.30/fs/yaffs2/yaffs_tagsvalidity.h
14833 --- linux-2.6.30.orig/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
14834 +++ linux-2.6.30/fs/yaffs2/yaffs_tagsvalidity.h 2009-06-11 09:21:04.000000000 +0200
14835 @@ -0,0 +1,24 @@
14837 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14839 + * Copyright (C) 2002-2007 Aleph One Ltd.
14840 + * for Toby Churchill Ltd and Brightstar Engineering
14842 + * Created by Charles Manning <charles@aleph1.co.uk>
14844 + * This program is free software; you can redistribute it and/or modify
14845 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14846 + * published by the Free Software Foundation.
14848 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14849 + */
14852 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14853 +#define __YAFFS_TAGS_VALIDITY_H__
14855 +#include "yaffs_guts.h"
14857 +void yaffs_InitialiseTags(yaffs_ExtendedTags *tags);
14858 +int yaffs_ValidateTags(yaffs_ExtendedTags *tags);
14859 +#endif
14860 diff -Nur linux-2.6.30.orig/fs/yaffs2/yportenv.h linux-2.6.30/fs/yaffs2/yportenv.h
14861 --- linux-2.6.30.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
14862 +++ linux-2.6.30/fs/yaffs2/yportenv.h 2009-06-11 09:21:04.000000000 +0200
14863 @@ -0,0 +1,203 @@
14865 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14867 + * Copyright (C) 2002-2007 Aleph One Ltd.
14868 + * for Toby Churchill Ltd and Brightstar Engineering
14870 + * Created by Charles Manning <charles@aleph1.co.uk>
14872 + * This program is free software; you can redistribute it and/or modify
14873 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14874 + * published by the Free Software Foundation.
14876 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14877 + */
14880 +#ifndef __YPORTENV_H__
14881 +#define __YPORTENV_H__
14884 + * Define the MTD version in terms of Linux Kernel versions
14885 + * This allows yaffs to be used independantly of the kernel
14886 + * as well as with it.
14887 + */
14889 +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
14891 +#if defined CONFIG_YAFFS_WINCE
14893 +#include "ywinceenv.h"
14895 +#elif defined __KERNEL__
14897 +#include "moduleconfig.h"
14899 +/* Linux kernel */
14901 +#include <linux/version.h>
14902 +#define MTD_VERSION_CODE LINUX_VERSION_CODE
14904 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
14905 +#include <linux/config.h>
14906 +#endif
14907 +#include <linux/kernel.h>
14908 +#include <linux/mm.h>
14909 +#include <linux/sched.h>
14910 +#include <linux/string.h>
14911 +#include <linux/slab.h>
14912 +#include <linux/vmalloc.h>
14914 +#define YCHAR char
14915 +#define YUCHAR unsigned char
14916 +#define _Y(x) x
14917 +#define yaffs_strcat(a, b) strcat(a, b)
14918 +#define yaffs_strcpy(a, b) strcpy(a, b)
14919 +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
14920 +#define yaffs_strncmp(a, b, c) strncmp(a, b, c)
14921 +#define yaffs_strlen(s) strlen(s)
14922 +#define yaffs_sprintf sprintf
14923 +#define yaffs_toupper(a) toupper(a)
14925 +#define Y_INLINE inline
14927 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14928 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14930 +/* #define YPRINTF(x) printk x */
14931 +#define YMALLOC(x) kmalloc(x, GFP_NOFS)
14932 +#define YFREE(x) kfree(x)
14933 +#define YMALLOC_ALT(x) vmalloc(x)
14934 +#define YFREE_ALT(x) vfree(x)
14935 +#define YMALLOC_DMA(x) YMALLOC(x)
14937 +/* KR - added for use in scan so processes aren't blocked indefinitely. */
14938 +#define YYIELD() schedule()
14940 +#define YAFFS_ROOT_MODE 0666
14941 +#define YAFFS_LOSTNFOUND_MODE 0666
14943 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
14944 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14945 +#define Y_TIME_CONVERT(x) (x).tv_sec
14946 +#else
14947 +#define Y_CURRENT_TIME CURRENT_TIME
14948 +#define Y_TIME_CONVERT(x) (x)
14949 +#endif
14951 +#define yaffs_SumCompare(x, y) ((x) == (y))
14952 +#define yaffs_strcmp(a, b) strcmp(a, b)
14954 +#define TENDSTR "\n"
14955 +#define TSTR(x) KERN_WARNING x
14956 +#define TCONT(x) x
14957 +#define TOUT(p) printk p
14959 +#define yaffs_trace(mask, fmt, args...) \
14960 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14961 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14962 + } while (0)
14964 +#define compile_time_assertion(assertion) \
14965 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14967 +#elif defined CONFIG_YAFFS_DIRECT
14969 +#define MTD_VERSION_CODE MTD_VERSION(2, 6, 22)
14971 +/* Direct interface */
14972 +#include "ydirectenv.h"
14974 +#elif defined CONFIG_YAFFS_UTIL
14976 +/* Stuff for YAFFS utilities */
14978 +#include "stdlib.h"
14979 +#include "stdio.h"
14980 +#include "string.h"
14982 +#include "devextras.h"
14984 +#define YMALLOC(x) malloc(x)
14985 +#define YFREE(x) free(x)
14986 +#define YMALLOC_ALT(x) malloc(x)
14987 +#define YFREE_ALT(x) free(x)
14989 +#define YCHAR char
14990 +#define YUCHAR unsigned char
14991 +#define _Y(x) x
14992 +#define yaffs_strcat(a, b) strcat(a, b)
14993 +#define yaffs_strcpy(a, b) strcpy(a, b)
14994 +#define yaffs_strncpy(a, b, c) strncpy(a, b, c)
14995 +#define yaffs_strlen(s) strlen(s)
14996 +#define yaffs_sprintf sprintf
14997 +#define yaffs_toupper(a) toupper(a)
14999 +#define Y_INLINE inline
15001 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
15002 +/* #define YALERT(s) YINFO(s) */
15004 +#define TENDSTR "\n"
15005 +#define TSTR(x) x
15006 +#define TOUT(p) printf p
15008 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
15009 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
15010 +/* #define YPRINTF(x) printf x */
15012 +#define YAFFS_ROOT_MODE 0666
15013 +#define YAFFS_LOSTNFOUND_MODE 0666
15015 +#define yaffs_SumCompare(x, y) ((x) == (y))
15016 +#define yaffs_strcmp(a, b) strcmp(a, b)
15018 +#else
15019 +/* Should have specified a configuration type */
15020 +#error Unknown configuration
15022 +#endif
15024 +/* see yaffs_fs.c */
15025 +extern unsigned int yaffs_traceMask;
15026 +extern unsigned int yaffs_wr_attempts;
15029 + * Tracing flags.
15030 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
15031 + */
15033 +#define YAFFS_TRACE_OS 0x00000002
15034 +#define YAFFS_TRACE_ALLOCATE 0x00000004
15035 +#define YAFFS_TRACE_SCAN 0x00000008
15036 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
15037 +#define YAFFS_TRACE_ERASE 0x00000020
15038 +#define YAFFS_TRACE_GC 0x00000040
15039 +#define YAFFS_TRACE_WRITE 0x00000080
15040 +#define YAFFS_TRACE_TRACING 0x00000100
15041 +#define YAFFS_TRACE_DELETION 0x00000200
15042 +#define YAFFS_TRACE_BUFFERS 0x00000400
15043 +#define YAFFS_TRACE_NANDACCESS 0x00000800
15044 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
15045 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
15046 +#define YAFFS_TRACE_MTD 0x00004000
15047 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
15049 +#define YAFFS_TRACE_VERIFY 0x00010000
15050 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
15051 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
15052 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
15055 +#define YAFFS_TRACE_ERROR 0x40000000
15056 +#define YAFFS_TRACE_BUG 0x80000000
15057 +#define YAFFS_TRACE_ALWAYS 0xF0000000
15060 +#define T(mask, p) do { if ((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p); } while (0)
15062 +#ifndef YBUG
15063 +#define YBUG() do {T(YAFFS_TRACE_BUG, (TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR), __LINE__)); } while (0)
15064 +#endif
15066 +#endif