Implement UBIFS rootfs support
[openadk.git] / target / linux / patches / 3.12.69 / yaffs2.patch
blob306814439686e91cdff995b16819e73b59429f37
1 diff -Nur linux-3.12.20.orig/fs/Kconfig linux-3.12.20/fs/Kconfig
2 --- linux-3.12.20.orig/fs/Kconfig 2014-05-15 09:56:19.000000000 +0200
3 +++ linux-3.12.20/fs/Kconfig 2014-05-17 11:28:49.000000000 +0200
4 @@ -193,6 +193,7 @@
5 source "fs/befs/Kconfig"
6 source "fs/bfs/Kconfig"
7 source "fs/efs/Kconfig"
8 +source "fs/yaffs2/Kconfig"
9 source "fs/jffs2/Kconfig"
10 # UBIFS File system configuration
11 source "fs/ubifs/Kconfig"
12 diff -Nur linux-3.12.20.orig/fs/Makefile linux-3.12.20/fs/Makefile
13 --- linux-3.12.20.orig/fs/Makefile 2014-05-15 09:56:19.000000000 +0200
14 +++ linux-3.12.20/fs/Makefile 2014-05-17 11:28:49.000000000 +0200
15 @@ -126,3 +126,4 @@
16 obj-$(CONFIG_CEPH_FS) += ceph/
17 obj-$(CONFIG_PSTORE) += pstore/
18 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
19 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
20 diff -Nur linux-3.12.20.orig/fs/yaffs2/Kconfig linux-3.12.20/fs/yaffs2/Kconfig
21 --- linux-3.12.20.orig/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
22 +++ linux-3.12.20/fs/yaffs2/Kconfig 2014-05-17 11:28:49.000000000 +0200
23 @@ -0,0 +1,171 @@
25 +# yaffs file system configurations
28 +config YAFFS_FS
29 + tristate "yaffs2 file system support"
30 + default n
31 + depends on MTD_BLOCK
32 + select YAFFS_YAFFS1
33 + select YAFFS_YAFFS2
34 + help
35 + yaffs2, or Yet Another Flash File System, is a file system
36 + optimised for NAND Flash chips.
38 + To compile the yaffs2 file system support as a module, choose M
39 + here: the module will be called yaffs2.
41 + If unsure, say N.
43 + Further information on yaffs2 is available at
44 + <http://www.aleph1.co.uk/yaffs/>.
46 +config YAFFS_YAFFS1
47 + bool "512 byte / page devices"
48 + depends on YAFFS_FS
49 + default y
50 + help
51 + Enable yaffs1 support -- yaffs for 512 byte / page devices
53 + Not needed for 2K-page devices.
55 + If unsure, say Y.
57 +config YAFFS_9BYTE_TAGS
58 + bool "Use older-style on-NAND data format with pageStatus byte"
59 + depends on YAFFS_YAFFS1
60 + default n
61 + help
63 + Older-style on-NAND data format has a "pageStatus" byte to record
64 + chunk/page state. This byte is zero when the page is discarded.
65 + Choose this option if you have existing on-NAND data using this
66 + format that you need to continue to support. New data written
67 + also uses the older-style format. Note: Use of this option
68 + generally requires that MTD's oob layout be adjusted to use the
69 + older-style format. See notes on tags formats and MTD versions
70 + in yaffs_mtdif1.c.
72 + If unsure, say N.
74 +config YAFFS_DOES_ECC
75 + bool "Lets yaffs do its own ECC"
76 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
77 + default n
78 + help
79 + This enables yaffs to use its own ECC functions instead of using
80 + the ones from the generic MTD-NAND driver.
82 + If unsure, say N.
84 +config YAFFS_ECC_WRONG_ORDER
85 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
86 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
87 + default n
88 + help
89 + This makes yaffs_ecc.c use the same ecc byte order as Steven
90 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
91 + order as SmartMedia.
93 + If unsure, say N.
95 +config YAFFS_YAFFS2
96 + bool "2048 byte (or larger) / page devices"
97 + depends on YAFFS_FS
98 + default y
99 + help
100 + Enable yaffs2 support -- yaffs for >= 2K bytes per page devices
102 + If unsure, say Y.
104 +config YAFFS_AUTO_YAFFS2
105 + bool "Autoselect yaffs2 format"
106 + depends on YAFFS_YAFFS2
107 + default y
108 + help
109 + Without this, you need to explicitely use yaffs2 as the file
110 + system type. With this, you can say "yaffs" and yaffs or yaffs2
111 + will be used depending on the device page size (yaffs on
112 + 512-byte page devices, yaffs2 on 2K page devices).
114 + If unsure, say Y.
116 +config YAFFS_DISABLE_TAGS_ECC
117 + bool "Disable yaffs from doing ECC on tags by default"
118 + depends on YAFFS_FS && YAFFS_YAFFS2
119 + default n
120 + help
121 + This defaults yaffs to using its own ECC calculations on tags instead of
122 + just relying on the MTD.
123 + This behavior can also be overridden with tags_ecc_on and
124 + tags_ecc_off mount options.
126 + If unsure, say N.
128 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
129 + bool "Force chunk erase check"
130 + depends on YAFFS_FS
131 + default n
132 + help
133 + Normally yaffs only checks chunks before writing until an erased
134 + chunk is found. This helps to detect any partially written
135 + chunks that might have happened due to power loss.
137 + Enabling this forces on the test that chunks are erased in flash
138 + before writing to them. This takes more time but is potentially
139 + a bit more secure.
141 + Suggest setting Y during development and ironing out driver
142 + issues etc. Suggest setting to N if you want faster writing.
144 + If unsure, say Y.
146 +config YAFFS_EMPTY_LOST_AND_FOUND
147 + bool "Empty lost and found on boot"
148 + depends on YAFFS_FS
149 + default n
150 + help
151 + If this is enabled then the contents of lost and found is
152 + automatically dumped at mount.
154 + If unsure, say N.
156 +config YAFFS_DISABLE_BLOCK_REFRESHING
157 + bool "Disable yaffs2 block refreshing"
158 + depends on YAFFS_FS
159 + default n
160 + help
161 + If this is set, then block refreshing is disabled.
162 + Block refreshing infrequently refreshes the oldest block in
163 + a yaffs2 file system. This mechanism helps to refresh flash to
164 + mitigate against data loss. This is particularly useful for MLC.
166 + If unsure, say N.
168 +config YAFFS_DISABLE_BACKGROUND
169 + bool "Disable yaffs2 background processing"
170 + depends on YAFFS_FS
171 + default n
172 + help
173 + If this is set, then background processing is disabled.
174 + Background processing makes many foreground activities faster.
176 + If unsure, say N.
178 +config YAFFS_DISABLE_BAD_BLOCK_MARKING
179 + bool "Disable yaffs2 bad block marking"
180 + depends on YAFFS_FS
181 + default n
182 + help
183 + Useful during early flash bring up to prevent problems causing
184 + lots of bad block marking.
186 + If unsure, say N.
188 +config YAFFS_XATTR
189 + bool "Enable yaffs2 xattr support"
190 + depends on YAFFS_FS
191 + default y
192 + help
193 + If this is set then yaffs2 will provide xattr support.
194 + If unsure, say Y.
195 diff -Nur linux-3.12.20.orig/fs/yaffs2/Makefile linux-3.12.20/fs/yaffs2/Makefile
196 --- linux-3.12.20.orig/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
197 +++ linux-3.12.20/fs/yaffs2/Makefile 2014-05-17 11:28:49.000000000 +0200
198 @@ -0,0 +1,18 @@
200 +# Makefile for the linux YAFFS filesystem routines.
203 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
205 +yaffs-y := yaffs_ecc.o yaffs_vfs.o yaffs_guts.o yaffs_checkptrw.o
206 +yaffs-y += yaffs_packedtags1.o yaffs_packedtags2.o yaffs_nand.o
207 +yaffs-y += yaffs_tagscompat.o yaffs_tagsmarshall.o
208 +yaffs-y += yaffs_mtdif.o
209 +yaffs-y += yaffs_nameval.o yaffs_attribs.o
210 +yaffs-y += yaffs_allocator.o
211 +yaffs-y += yaffs_yaffs1.o
212 +yaffs-y += yaffs_yaffs2.o
213 +yaffs-y += yaffs_bitmap.o
214 +yaffs-y += yaffs_summary.o
215 +yaffs-y += yaffs_verify.o
217 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_allocator.c linux-3.12.20/fs/yaffs2/yaffs_allocator.c
218 --- linux-3.12.20.orig/fs/yaffs2/yaffs_allocator.c 1970-01-01 01:00:00.000000000 +0100
219 +++ linux-3.12.20/fs/yaffs2/yaffs_allocator.c 2014-05-17 11:28:49.000000000 +0200
220 @@ -0,0 +1,357 @@
222 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
224 + * Copyright (C) 2002-2011 Aleph One Ltd.
225 + * for Toby Churchill Ltd and Brightstar Engineering
227 + * Created by Charles Manning <charles@aleph1.co.uk>
229 + * This program is free software; you can redistribute it and/or modify
230 + * it under the terms of the GNU General Public License version 2 as
231 + * published by the Free Software Foundation.
232 + */
234 +#include "yaffs_allocator.h"
235 +#include "yaffs_guts.h"
236 +#include "yaffs_trace.h"
237 +#include "yportenv.h"
240 + * Each entry in yaffs_tnode_list and yaffs_obj_list hold blocks
241 + * of approx 100 objects that are themn allocated singly.
242 + * This is basically a simplified slab allocator.
244 + * We don't use the Linux slab allocator because slab does not allow
245 + * us to dump all the objects in one hit when we do a umount and tear
246 + * down all the tnodes and objects. slab requires that we first free
247 + * the individual objects.
249 + * Once yaffs has been mainlined I shall try to motivate for a change
250 + * to slab to provide the extra features we need here.
251 + */
253 +struct yaffs_tnode_list {
254 + struct yaffs_tnode_list *next;
255 + struct yaffs_tnode *tnodes;
258 +struct yaffs_obj_list {
259 + struct yaffs_obj_list *next;
260 + struct yaffs_obj *objects;
263 +struct yaffs_allocator {
264 + int n_tnodes_created;
265 + struct yaffs_tnode *free_tnodes;
266 + int n_free_tnodes;
267 + struct yaffs_tnode_list *alloc_tnode_list;
269 + int n_obj_created;
270 + struct list_head free_objs;
271 + int n_free_objects;
273 + struct yaffs_obj_list *allocated_obj_list;
276 +static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
278 + struct yaffs_allocator *allocator =
279 + (struct yaffs_allocator *)dev->allocator;
280 + struct yaffs_tnode_list *tmp;
282 + if (!allocator) {
283 + BUG();
284 + return;
287 + while (allocator->alloc_tnode_list) {
288 + tmp = allocator->alloc_tnode_list->next;
290 + kfree(allocator->alloc_tnode_list->tnodes);
291 + kfree(allocator->alloc_tnode_list);
292 + allocator->alloc_tnode_list = tmp;
295 + allocator->free_tnodes = NULL;
296 + allocator->n_free_tnodes = 0;
297 + allocator->n_tnodes_created = 0;
300 +static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
302 + struct yaffs_allocator *allocator = dev->allocator;
304 + if (!allocator) {
305 + BUG();
306 + return;
309 + allocator->alloc_tnode_list = NULL;
310 + allocator->free_tnodes = NULL;
311 + allocator->n_free_tnodes = 0;
312 + allocator->n_tnodes_created = 0;
315 +static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
317 + struct yaffs_allocator *allocator =
318 + (struct yaffs_allocator *)dev->allocator;
319 + int i;
320 + struct yaffs_tnode *new_tnodes;
321 + u8 *mem;
322 + struct yaffs_tnode *curr;
323 + struct yaffs_tnode *next;
324 + struct yaffs_tnode_list *tnl;
326 + if (!allocator) {
327 + BUG();
328 + return YAFFS_FAIL;
331 + if (n_tnodes < 1)
332 + return YAFFS_OK;
334 + /* make these things */
335 + new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
336 + mem = (u8 *) new_tnodes;
338 + if (!new_tnodes) {
339 + yaffs_trace(YAFFS_TRACE_ERROR,
340 + "yaffs: Could not allocate Tnodes");
341 + return YAFFS_FAIL;
344 + /* New hookup for wide tnodes */
345 + for (i = 0; i < n_tnodes - 1; i++) {
346 + curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
347 + next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
348 + curr->internal[0] = next;
351 + curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
352 + curr->internal[0] = allocator->free_tnodes;
353 + allocator->free_tnodes = (struct yaffs_tnode *)mem;
355 + allocator->n_free_tnodes += n_tnodes;
356 + allocator->n_tnodes_created += n_tnodes;
358 + /* Now add this bunch of tnodes to a list for freeing up.
359 + * NB If we can't add this to the management list it isn't fatal
360 + * but it just means we can't free this bunch of tnodes later.
361 + */
362 + tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
363 + if (!tnl) {
364 + yaffs_trace(YAFFS_TRACE_ERROR,
365 + "Could not add tnodes to management list");
366 + return YAFFS_FAIL;
367 + } else {
368 + tnl->tnodes = new_tnodes;
369 + tnl->next = allocator->alloc_tnode_list;
370 + allocator->alloc_tnode_list = tnl;
373 + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Tnodes added");
375 + return YAFFS_OK;
378 +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
380 + struct yaffs_allocator *allocator =
381 + (struct yaffs_allocator *)dev->allocator;
382 + struct yaffs_tnode *tn = NULL;
384 + if (!allocator) {
385 + BUG();
386 + return NULL;
389 + /* If there are none left make more */
390 + if (!allocator->free_tnodes)
391 + yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
393 + if (allocator->free_tnodes) {
394 + tn = allocator->free_tnodes;
395 + allocator->free_tnodes = allocator->free_tnodes->internal[0];
396 + allocator->n_free_tnodes--;
399 + return tn;
402 +/* FreeTnode frees up a tnode and puts it back on the free list */
403 +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
405 + struct yaffs_allocator *allocator = dev->allocator;
407 + if (!allocator) {
408 + BUG();
409 + return;
412 + if (tn) {
413 + tn->internal[0] = allocator->free_tnodes;
414 + allocator->free_tnodes = tn;
415 + allocator->n_free_tnodes++;
417 + dev->checkpoint_blocks_required = 0; /* force recalculation */
420 +/*--------------- yaffs_obj alloaction ------------------------
422 + * Free yaffs_objs are stored in a list using obj->siblings.
423 + * The blocks of allocated objects are stored in a linked list.
424 + */
426 +static void yaffs_init_raw_objs(struct yaffs_dev *dev)
428 + struct yaffs_allocator *allocator = dev->allocator;
430 + if (!allocator) {
431 + BUG();
432 + return;
435 + allocator->allocated_obj_list = NULL;
436 + INIT_LIST_HEAD(&allocator->free_objs);
437 + allocator->n_free_objects = 0;
440 +static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
442 + struct yaffs_allocator *allocator = dev->allocator;
443 + struct yaffs_obj_list *tmp;
445 + if (!allocator) {
446 + BUG();
447 + return;
450 + while (allocator->allocated_obj_list) {
451 + tmp = allocator->allocated_obj_list->next;
452 + kfree(allocator->allocated_obj_list->objects);
453 + kfree(allocator->allocated_obj_list);
454 + allocator->allocated_obj_list = tmp;
457 + INIT_LIST_HEAD(&allocator->free_objs);
458 + allocator->n_free_objects = 0;
459 + allocator->n_obj_created = 0;
462 +static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
464 + struct yaffs_allocator *allocator = dev->allocator;
465 + int i;
466 + struct yaffs_obj *new_objs;
467 + struct yaffs_obj_list *list;
469 + if (!allocator) {
470 + BUG();
471 + return YAFFS_FAIL;
474 + if (n_obj < 1)
475 + return YAFFS_OK;
477 + /* make these things */
478 + new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
479 + list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
481 + if (!new_objs || !list) {
482 + kfree(new_objs);
483 + new_objs = NULL;
484 + kfree(list);
485 + list = NULL;
486 + yaffs_trace(YAFFS_TRACE_ALLOCATE,
487 + "Could not allocate more objects");
488 + return YAFFS_FAIL;
491 + /* Hook them into the free list */
492 + for (i = 0; i < n_obj; i++)
493 + list_add(&new_objs[i].siblings, &allocator->free_objs);
495 + allocator->n_free_objects += n_obj;
496 + allocator->n_obj_created += n_obj;
498 + /* Now add this bunch of Objects to a list for freeing up. */
500 + list->objects = new_objs;
501 + list->next = allocator->allocated_obj_list;
502 + allocator->allocated_obj_list = list;
504 + return YAFFS_OK;
507 +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
509 + struct yaffs_obj *obj = NULL;
510 + struct list_head *lh;
511 + struct yaffs_allocator *allocator = dev->allocator;
513 + if (!allocator) {
514 + BUG();
515 + return obj;
518 + /* If there are none left make more */
519 + if (list_empty(&allocator->free_objs))
520 + yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
522 + if (!list_empty(&allocator->free_objs)) {
523 + lh = allocator->free_objs.next;
524 + obj = list_entry(lh, struct yaffs_obj, siblings);
525 + list_del_init(lh);
526 + allocator->n_free_objects--;
529 + return obj;
532 +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
535 + struct yaffs_allocator *allocator = dev->allocator;
537 + if (!allocator) {
538 + BUG();
539 + return;
542 + /* Link into the free list. */
543 + list_add(&obj->siblings, &allocator->free_objs);
544 + allocator->n_free_objects++;
547 +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
550 + if (!dev->allocator) {
551 + BUG();
552 + return;
555 + yaffs_deinit_raw_tnodes(dev);
556 + yaffs_deinit_raw_objs(dev);
557 + kfree(dev->allocator);
558 + dev->allocator = NULL;
561 +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
563 + struct yaffs_allocator *allocator;
565 + if (dev->allocator) {
566 + BUG();
567 + return;
570 + allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
571 + if (allocator) {
572 + dev->allocator = allocator;
573 + yaffs_init_raw_tnodes(dev);
574 + yaffs_init_raw_objs(dev);
578 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_allocator.h linux-3.12.20/fs/yaffs2/yaffs_allocator.h
579 --- linux-3.12.20.orig/fs/yaffs2/yaffs_allocator.h 1970-01-01 01:00:00.000000000 +0100
580 +++ linux-3.12.20/fs/yaffs2/yaffs_allocator.h 2014-05-17 11:28:49.000000000 +0200
581 @@ -0,0 +1,30 @@
583 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
585 + * Copyright (C) 2002-2011 Aleph One Ltd.
586 + * for Toby Churchill Ltd and Brightstar Engineering
588 + * Created by Charles Manning <charles@aleph1.co.uk>
590 + * This program is free software; you can redistribute it and/or modify
591 + * it under the terms of the GNU Lesser General Public License version 2.1 as
592 + * published by the Free Software Foundation.
594 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
595 + */
597 +#ifndef __YAFFS_ALLOCATOR_H__
598 +#define __YAFFS_ALLOCATOR_H__
600 +#include "yaffs_guts.h"
602 +void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev);
603 +void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev);
605 +struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev);
606 +void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn);
608 +struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev);
609 +void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj);
611 +#endif
612 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_attribs.c linux-3.12.20/fs/yaffs2/yaffs_attribs.c
613 --- linux-3.12.20.orig/fs/yaffs2/yaffs_attribs.c 1970-01-01 01:00:00.000000000 +0100
614 +++ linux-3.12.20/fs/yaffs2/yaffs_attribs.c 2014-05-17 11:28:49.000000000 +0200
615 @@ -0,0 +1,166 @@
617 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
619 + * Copyright (C) 2002-2011 Aleph One Ltd.
620 + * for Toby Churchill Ltd and Brightstar Engineering
622 + * Created by Charles Manning <charles@aleph1.co.uk>
624 + * This program is free software; you can redistribute it and/or modify
625 + * it under the terms of the GNU General Public License version 2 as
626 + * published by the Free Software Foundation.
627 + */
629 +#include "yaffs_guts.h"
630 +#include "yaffs_attribs.h"
632 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
633 +static inline uid_t ia_uid_read(const struct iattr *iattr)
635 + return from_kuid(&init_user_ns, iattr->ia_uid);
638 +static inline gid_t ia_gid_read(const struct iattr *iattr)
640 + return from_kgid(&init_user_ns, iattr->ia_gid);
643 +static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
645 + iattr->ia_uid = make_kuid(&init_user_ns, uid);
648 +static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
650 + iattr->ia_gid = make_kgid(&init_user_ns, gid);
652 +#else
653 +static inline uid_t ia_uid_read(const struct iattr *iattr)
655 + return iattr->ia_uid;
658 +static inline gid_t ia_gid_read(const struct iattr *inode)
660 + return iattr->ia_gid;
663 +static inline void ia_uid_write(struct iattr *iattr, uid_t uid)
665 + iattr->ia_uid = uid;
668 +static inline void ia_gid_write(struct iattr *iattr, gid_t gid)
670 + iattr->ia_gid = gid;
672 +#endif
674 +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh)
676 + obj->yst_uid = oh->yst_uid;
677 + obj->yst_gid = oh->yst_gid;
678 + obj->yst_atime = oh->yst_atime;
679 + obj->yst_mtime = oh->yst_mtime;
680 + obj->yst_ctime = oh->yst_ctime;
681 + obj->yst_rdev = oh->yst_rdev;
684 +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj)
686 + oh->yst_uid = obj->yst_uid;
687 + oh->yst_gid = obj->yst_gid;
688 + oh->yst_atime = obj->yst_atime;
689 + oh->yst_mtime = obj->yst_mtime;
690 + oh->yst_ctime = obj->yst_ctime;
691 + oh->yst_rdev = obj->yst_rdev;
695 +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c)
697 + obj->yst_mtime = Y_CURRENT_TIME;
698 + if (do_a)
699 + obj->yst_atime = obj->yst_mtime;
700 + if (do_c)
701 + obj->yst_ctime = obj->yst_mtime;
704 +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev)
706 + yaffs_load_current_time(obj, 1, 1);
707 + obj->yst_rdev = rdev;
708 + obj->yst_uid = uid;
709 + obj->yst_gid = gid;
712 +static loff_t yaffs_get_file_size(struct yaffs_obj *obj)
714 + YCHAR *alias = NULL;
715 + obj = yaffs_get_equivalent_obj(obj);
717 + switch (obj->variant_type) {
718 + case YAFFS_OBJECT_TYPE_FILE:
719 + return obj->variant.file_variant.file_size;
720 + case YAFFS_OBJECT_TYPE_SYMLINK:
721 + alias = obj->variant.symlink_variant.alias;
722 + if (!alias)
723 + return 0;
724 + return strnlen(alias, YAFFS_MAX_ALIAS_LENGTH);
725 + default:
726 + return 0;
730 +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr)
732 + unsigned int valid = attr->ia_valid;
734 + if (valid & ATTR_MODE)
735 + obj->yst_mode = attr->ia_mode;
736 + if (valid & ATTR_UID)
737 + obj->yst_uid = ia_uid_read(attr);
738 + if (valid & ATTR_GID)
739 + obj->yst_gid = ia_gid_read(attr);
741 + if (valid & ATTR_ATIME)
742 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
743 + if (valid & ATTR_CTIME)
744 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
745 + if (valid & ATTR_MTIME)
746 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
748 + if (valid & ATTR_SIZE)
749 + yaffs_resize_file(obj, attr->ia_size);
751 + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
753 + return YAFFS_OK;
757 +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr)
759 + unsigned int valid = 0;
761 + attr->ia_mode = obj->yst_mode;
762 + valid |= ATTR_MODE;
763 + ia_uid_write(attr, obj->yst_uid);
764 + valid |= ATTR_UID;
765 + ia_gid_write(attr, obj->yst_gid);
766 + valid |= ATTR_GID;
768 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
769 + valid |= ATTR_ATIME;
770 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
771 + valid |= ATTR_CTIME;
772 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
773 + valid |= ATTR_MTIME;
775 + attr->ia_size = yaffs_get_file_size(obj);
776 + valid |= ATTR_SIZE;
778 + attr->ia_valid = valid;
780 + return YAFFS_OK;
782 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_attribs.h linux-3.12.20/fs/yaffs2/yaffs_attribs.h
783 --- linux-3.12.20.orig/fs/yaffs2/yaffs_attribs.h 1970-01-01 01:00:00.000000000 +0100
784 +++ linux-3.12.20/fs/yaffs2/yaffs_attribs.h 2014-05-17 11:28:49.000000000 +0200
785 @@ -0,0 +1,28 @@
787 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
789 + * Copyright (C) 2002-2011 Aleph One Ltd.
790 + * for Toby Churchill Ltd and Brightstar Engineering
792 + * Created by Charles Manning <charles@aleph1.co.uk>
794 + * This program is free software; you can redistribute it and/or modify
795 + * it under the terms of the GNU Lesser General Public License version 2.1 as
796 + * published by the Free Software Foundation.
798 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
799 + */
801 +#ifndef __YAFFS_ATTRIBS_H__
802 +#define __YAFFS_ATTRIBS_H__
804 +#include "yaffs_guts.h"
806 +void yaffs_load_attribs(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh);
807 +void yaffs_load_attribs_oh(struct yaffs_obj_hdr *oh, struct yaffs_obj *obj);
808 +void yaffs_attribs_init(struct yaffs_obj *obj, u32 gid, u32 uid, u32 rdev);
809 +void yaffs_load_current_time(struct yaffs_obj *obj, int do_a, int do_c);
810 +int yaffs_set_attribs(struct yaffs_obj *obj, struct iattr *attr);
811 +int yaffs_get_attribs(struct yaffs_obj *obj, struct iattr *attr);
813 +#endif
814 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_bitmap.c linux-3.12.20/fs/yaffs2/yaffs_bitmap.c
815 --- linux-3.12.20.orig/fs/yaffs2/yaffs_bitmap.c 1970-01-01 01:00:00.000000000 +0100
816 +++ linux-3.12.20/fs/yaffs2/yaffs_bitmap.c 2014-05-17 11:28:49.000000000 +0200
817 @@ -0,0 +1,97 @@
819 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
821 + * Copyright (C) 2002-2011 Aleph One Ltd.
822 + * for Toby Churchill Ltd and Brightstar Engineering
824 + * Created by Charles Manning <charles@aleph1.co.uk>
826 + * This program is free software; you can redistribute it and/or modify
827 + * it under the terms of the GNU General Public License version 2 as
828 + * published by the Free Software Foundation.
829 + */
831 +#include "yaffs_bitmap.h"
832 +#include "yaffs_trace.h"
834 + * Chunk bitmap manipulations
835 + */
837 +static inline u8 *yaffs_block_bits(struct yaffs_dev *dev, int blk)
839 + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
840 + yaffs_trace(YAFFS_TRACE_ERROR,
841 + "BlockBits block %d is not valid",
842 + blk);
843 + BUG();
845 + return dev->chunk_bits +
846 + (dev->chunk_bit_stride * (blk - dev->internal_start_block));
849 +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk)
851 + if (blk < dev->internal_start_block || blk > dev->internal_end_block ||
852 + chunk < 0 || chunk >= dev->param.chunks_per_block) {
853 + yaffs_trace(YAFFS_TRACE_ERROR,
854 + "Chunk Id (%d:%d) invalid",
855 + blk, chunk);
856 + BUG();
860 +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk)
862 + u8 *blk_bits = yaffs_block_bits(dev, blk);
864 + memset(blk_bits, 0, dev->chunk_bit_stride);
867 +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
869 + u8 *blk_bits = yaffs_block_bits(dev, blk);
871 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
872 + blk_bits[chunk / 8] &= ~(1 << (chunk & 7));
875 +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
877 + u8 *blk_bits = yaffs_block_bits(dev, blk);
879 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
880 + blk_bits[chunk / 8] |= (1 << (chunk & 7));
883 +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk)
885 + u8 *blk_bits = yaffs_block_bits(dev, blk);
887 + yaffs_verify_chunk_bit_id(dev, blk, chunk);
888 + return (blk_bits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
891 +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk)
893 + u8 *blk_bits = yaffs_block_bits(dev, blk);
894 + int i;
896 + for (i = 0; i < dev->chunk_bit_stride; i++) {
897 + if (*blk_bits)
898 + return 1;
899 + blk_bits++;
901 + return 0;
904 +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk)
906 + u8 *blk_bits = yaffs_block_bits(dev, blk);
907 + int i;
908 + int n = 0;
910 + for (i = 0; i < dev->chunk_bit_stride; i++, blk_bits++)
911 + n += hweight8(*blk_bits);
913 + return n;
915 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_bitmap.h linux-3.12.20/fs/yaffs2/yaffs_bitmap.h
916 --- linux-3.12.20.orig/fs/yaffs2/yaffs_bitmap.h 1970-01-01 01:00:00.000000000 +0100
917 +++ linux-3.12.20/fs/yaffs2/yaffs_bitmap.h 2014-05-17 11:28:49.000000000 +0200
918 @@ -0,0 +1,33 @@
920 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
922 + * Copyright (C) 2002-2011 Aleph One Ltd.
923 + * for Toby Churchill Ltd and Brightstar Engineering
925 + * Created by Charles Manning <charles@aleph1.co.uk>
927 + * This program is free software; you can redistribute it and/or modify
928 + * it under the terms of the GNU Lesser General Public License version 2.1 as
929 + * published by the Free Software Foundation.
931 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
932 + */
935 + * Chunk bitmap manipulations
936 + */
938 +#ifndef __YAFFS_BITMAP_H__
939 +#define __YAFFS_BITMAP_H__
941 +#include "yaffs_guts.h"
943 +void yaffs_verify_chunk_bit_id(struct yaffs_dev *dev, int blk, int chunk);
944 +void yaffs_clear_chunk_bits(struct yaffs_dev *dev, int blk);
945 +void yaffs_clear_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
946 +void yaffs_set_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
947 +int yaffs_check_chunk_bit(struct yaffs_dev *dev, int blk, int chunk);
948 +int yaffs_still_some_chunks(struct yaffs_dev *dev, int blk);
949 +int yaffs_count_chunk_bits(struct yaffs_dev *dev, int blk);
951 +#endif
952 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_checkptrw.c linux-3.12.20/fs/yaffs2/yaffs_checkptrw.c
953 --- linux-3.12.20.orig/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
954 +++ linux-3.12.20/fs/yaffs2/yaffs_checkptrw.c 2014-05-17 11:28:49.000000000 +0200
955 @@ -0,0 +1,474 @@
957 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
959 + * Copyright (C) 2002-2011 Aleph One Ltd.
960 + * for Toby Churchill Ltd and Brightstar Engineering
962 + * Created by Charles Manning <charles@aleph1.co.uk>
964 + * This program is free software; you can redistribute it and/or modify
965 + * it under the terms of the GNU General Public License version 2 as
966 + * published by the Free Software Foundation.
967 + */
969 +#include "yaffs_checkptrw.h"
970 +#include "yaffs_getblockinfo.h"
972 +struct yaffs_checkpt_chunk_hdr {
973 + int version;
974 + int seq;
975 + u32 sum;
976 + u32 xor;
977 +} ;
980 +static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
982 + return chunk - dev->chunk_offset;
985 +static int apply_block_offset(struct yaffs_dev *dev, int block)
987 + return block - dev->block_offset;
990 +static void yaffs2_checkpt_init_chunk_hdr(struct yaffs_dev *dev)
992 + struct yaffs_checkpt_chunk_hdr hdr;
994 + hdr.version = YAFFS_CHECKPOINT_VERSION;
995 + hdr.seq = dev->checkpt_page_seq;
996 + hdr.sum = dev->checkpt_sum;
997 + hdr.xor = dev->checkpt_xor;
999 + dev->checkpt_byte_offs = sizeof(hdr);
1001 + memcpy(dev->checkpt_buffer, &hdr, sizeof(hdr));
1004 +static int yaffs2_checkpt_check_chunk_hdr(struct yaffs_dev *dev)
1006 + struct yaffs_checkpt_chunk_hdr hdr;
1008 + memcpy(&hdr, dev->checkpt_buffer, sizeof(hdr));
1010 + dev->checkpt_byte_offs = sizeof(hdr);
1012 + return hdr.version == YAFFS_CHECKPOINT_VERSION &&
1013 + hdr.seq == dev->checkpt_page_seq &&
1014 + hdr.sum == dev->checkpt_sum &&
1015 + hdr.xor == dev->checkpt_xor;
1018 +static int yaffs2_checkpt_space_ok(struct yaffs_dev *dev)
1020 + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
1022 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1023 + "checkpt blocks_avail = %d", blocks_avail);
1025 + return (blocks_avail <= 0) ? 0 : 1;
1028 +static int yaffs_checkpt_erase(struct yaffs_dev *dev)
1030 + int i;
1032 + if (!dev->drv.drv_erase_fn)
1033 + return 0;
1034 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1035 + "checking blocks %d to %d",
1036 + dev->internal_start_block, dev->internal_end_block);
1038 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
1039 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
1040 + int offset_i = apply_block_offset(dev, i);
1041 + int result;
1043 + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
1044 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1045 + "erasing checkpt block %d", i);
1047 + dev->n_erasures++;
1049 + result = dev->drv.drv_erase_fn(dev, offset_i);
1050 + if(result) {
1051 + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
1052 + dev->n_erased_blocks++;
1053 + dev->n_free_chunks +=
1054 + dev->param.chunks_per_block;
1055 + } else {
1056 + dev->drv.drv_mark_bad_fn(dev, offset_i);
1057 + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
1062 + dev->blocks_in_checkpt = 0;
1064 + return 1;
1067 +static void yaffs2_checkpt_find_erased_block(struct yaffs_dev *dev)
1069 + int i;
1070 + int blocks_avail = dev->n_erased_blocks - dev->param.n_reserved_blocks;
1072 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1073 + "allocating checkpt block: erased %d reserved %d avail %d next %d ",
1074 + dev->n_erased_blocks, dev->param.n_reserved_blocks,
1075 + blocks_avail, dev->checkpt_next_block);
1077 + if (dev->checkpt_next_block >= 0 &&
1078 + dev->checkpt_next_block <= dev->internal_end_block &&
1079 + blocks_avail > 0) {
1081 + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
1082 + i++) {
1083 + struct yaffs_block_info *bi;
1085 + bi = yaffs_get_block_info(dev, i);
1086 + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
1087 + dev->checkpt_next_block = i + 1;
1088 + dev->checkpt_cur_block = i;
1089 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1090 + "allocating checkpt block %d", i);
1091 + return;
1095 + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "out of checkpt blocks");
1097 + dev->checkpt_next_block = -1;
1098 + dev->checkpt_cur_block = -1;
1101 +static void yaffs2_checkpt_find_block(struct yaffs_dev *dev)
1103 + int i;
1104 + struct yaffs_ext_tags tags;
1106 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1107 + "find next checkpt block: start: blocks %d next %d",
1108 + dev->blocks_in_checkpt, dev->checkpt_next_block);
1110 + if (dev->blocks_in_checkpt < dev->checkpt_max_blocks)
1111 + for (i = dev->checkpt_next_block; i <= dev->internal_end_block;
1112 + i++) {
1113 + int chunk = i * dev->param.chunks_per_block;
1114 + enum yaffs_block_state state;
1115 + u32 seq;
1117 + dev->tagger.read_chunk_tags_fn(dev,
1118 + apply_chunk_offset(dev, chunk),
1119 + NULL, &tags);
1120 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1121 + "find next checkpt block: search: block %d state %d oid %d seq %d eccr %d",
1122 + i, (int) state,
1123 + tags.obj_id, tags.seq_number,
1124 + tags.ecc_result);
1126 + if (tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA)
1127 + continue;
1129 + dev->tagger.query_block_fn(dev,
1130 + apply_block_offset(dev, i),
1131 + &state, &seq);
1132 + if (state == YAFFS_BLOCK_STATE_DEAD)
1133 + continue;
1135 + /* Right kind of block */
1136 + dev->checkpt_next_block = tags.obj_id;
1137 + dev->checkpt_cur_block = i;
1138 + dev->checkpt_block_list[dev->blocks_in_checkpt] = i;
1139 + dev->blocks_in_checkpt++;
1140 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1141 + "found checkpt block %d", i);
1142 + return;
1145 + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "found no more checkpt blocks");
1147 + dev->checkpt_next_block = -1;
1148 + dev->checkpt_cur_block = -1;
1151 +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing)
1153 + int i;
1155 + dev->checkpt_open_write = writing;
1157 + /* Got the functions we need? */
1158 + if (!dev->tagger.write_chunk_tags_fn ||
1159 + !dev->tagger.read_chunk_tags_fn ||
1160 + !dev->drv.drv_erase_fn ||
1161 + !dev->drv.drv_mark_bad_fn)
1162 + return 0;
1164 + if (writing && !yaffs2_checkpt_space_ok(dev))
1165 + return 0;
1167 + if (!dev->checkpt_buffer)
1168 + dev->checkpt_buffer =
1169 + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
1170 + if (!dev->checkpt_buffer)
1171 + return 0;
1173 + dev->checkpt_page_seq = 0;
1174 + dev->checkpt_byte_count = 0;
1175 + dev->checkpt_sum = 0;
1176 + dev->checkpt_xor = 0;
1177 + dev->checkpt_cur_block = -1;
1178 + dev->checkpt_cur_chunk = -1;
1179 + dev->checkpt_next_block = dev->internal_start_block;
1181 + if (writing) {
1182 + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
1183 + yaffs2_checkpt_init_chunk_hdr(dev);
1184 + return yaffs_checkpt_erase(dev);
1187 + /* Opening for a read */
1188 + /* Set to a value that will kick off a read */
1189 + dev->checkpt_byte_offs = dev->data_bytes_per_chunk;
1190 + /* A checkpoint block list of 1 checkpoint block per 16 block is
1191 + * (hopefully) going to be way more than we need */
1192 + dev->blocks_in_checkpt = 0;
1193 + dev->checkpt_max_blocks =
1194 + (dev->internal_end_block - dev->internal_start_block) / 16 + 2;
1195 + dev->checkpt_block_list =
1196 + kmalloc(sizeof(int) * dev->checkpt_max_blocks, GFP_NOFS);
1198 + if (!dev->checkpt_block_list)
1199 + return 0;
1201 + for (i = 0; i < dev->checkpt_max_blocks; i++)
1202 + dev->checkpt_block_list[i] = -1;
1204 + return 1;
1207 +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum)
1209 + u32 composite_sum;
1211 + composite_sum = (dev->checkpt_sum << 8) | (dev->checkpt_xor & 0xff);
1212 + *sum = composite_sum;
1213 + return 1;
1216 +static int yaffs2_checkpt_flush_buffer(struct yaffs_dev *dev)
1218 + int chunk;
1219 + int offset_chunk;
1220 + struct yaffs_ext_tags tags;
1222 + if (dev->checkpt_cur_block < 0) {
1223 + yaffs2_checkpt_find_erased_block(dev);
1224 + dev->checkpt_cur_chunk = 0;
1227 + if (dev->checkpt_cur_block < 0)
1228 + return 0;
1230 + tags.is_deleted = 0;
1231 + tags.obj_id = dev->checkpt_next_block; /* Hint to next place to look */
1232 + tags.chunk_id = dev->checkpt_page_seq + 1;
1233 + tags.seq_number = YAFFS_SEQUENCE_CHECKPOINT_DATA;
1234 + tags.n_bytes = dev->data_bytes_per_chunk;
1235 + if (dev->checkpt_cur_chunk == 0) {
1236 + /* First chunk we write for the block? Set block state to
1237 + checkpoint */
1238 + struct yaffs_block_info *bi =
1239 + yaffs_get_block_info(dev, dev->checkpt_cur_block);
1240 + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1241 + dev->blocks_in_checkpt++;
1244 + chunk =
1245 + dev->checkpt_cur_block * dev->param.chunks_per_block +
1246 + dev->checkpt_cur_chunk;
1248 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1249 + "checkpoint wite buffer nand %d(%d:%d) objid %d chId %d",
1250 + chunk, dev->checkpt_cur_block, dev->checkpt_cur_chunk,
1251 + tags.obj_id, tags.chunk_id);
1253 + offset_chunk = apply_chunk_offset(dev, chunk);
1255 + dev->n_page_writes++;
1257 + dev->tagger.write_chunk_tags_fn(dev, offset_chunk,
1258 + dev->checkpt_buffer, &tags);
1259 + dev->checkpt_page_seq++;
1260 + dev->checkpt_cur_chunk++;
1261 + if (dev->checkpt_cur_chunk >= dev->param.chunks_per_block) {
1262 + dev->checkpt_cur_chunk = 0;
1263 + dev->checkpt_cur_block = -1;
1265 + memset(dev->checkpt_buffer, 0, dev->data_bytes_per_chunk);
1267 + yaffs2_checkpt_init_chunk_hdr(dev);
1270 + return 1;
1273 +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes)
1275 + int i = 0;
1276 + int ok = 1;
1277 + u8 *data_bytes = (u8 *) data;
1279 + if (!dev->checkpt_buffer)
1280 + return 0;
1282 + if (!dev->checkpt_open_write)
1283 + return -1;
1285 + while (i < n_bytes && ok) {
1286 + dev->checkpt_buffer[dev->checkpt_byte_offs] = *data_bytes;
1287 + dev->checkpt_sum += *data_bytes;
1288 + dev->checkpt_xor ^= *data_bytes;
1290 + dev->checkpt_byte_offs++;
1291 + i++;
1292 + data_bytes++;
1293 + dev->checkpt_byte_count++;
1295 + if (dev->checkpt_byte_offs < 0 ||
1296 + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk)
1297 + ok = yaffs2_checkpt_flush_buffer(dev);
1300 + return i;
1303 +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes)
1305 + int i = 0;
1306 + int ok = 1;
1307 + struct yaffs_ext_tags tags;
1308 + int chunk;
1309 + int offset_chunk;
1310 + u8 *data_bytes = (u8 *) data;
1312 + if (!dev->checkpt_buffer)
1313 + return 0;
1315 + if (dev->checkpt_open_write)
1316 + return -1;
1318 + while (i < n_bytes && ok) {
1320 + if (dev->checkpt_byte_offs < 0 ||
1321 + dev->checkpt_byte_offs >= dev->data_bytes_per_chunk) {
1323 + if (dev->checkpt_cur_block < 0) {
1324 + yaffs2_checkpt_find_block(dev);
1325 + dev->checkpt_cur_chunk = 0;
1328 + if (dev->checkpt_cur_block < 0) {
1329 + ok = 0;
1330 + break;
1333 + chunk = dev->checkpt_cur_block *
1334 + dev->param.chunks_per_block +
1335 + dev->checkpt_cur_chunk;
1337 + offset_chunk = apply_chunk_offset(dev, chunk);
1338 + dev->n_page_reads++;
1340 + /* read in the next chunk */
1341 + dev->tagger.read_chunk_tags_fn(dev,
1342 + offset_chunk,
1343 + dev->checkpt_buffer,
1344 + &tags);
1346 + if (tags.chunk_id != (dev->checkpt_page_seq + 1) ||
1347 + tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
1348 + tags.seq_number != YAFFS_SEQUENCE_CHECKPOINT_DATA) {
1349 + ok = 0;
1350 + break;
1352 + if(!yaffs2_checkpt_check_chunk_hdr(dev)) {
1353 + ok = 0;
1354 + break;
1357 + dev->checkpt_page_seq++;
1358 + dev->checkpt_cur_chunk++;
1360 + if (dev->checkpt_cur_chunk >=
1361 + dev->param.chunks_per_block)
1362 + dev->checkpt_cur_block = -1;
1366 + *data_bytes = dev->checkpt_buffer[dev->checkpt_byte_offs];
1367 + dev->checkpt_sum += *data_bytes;
1368 + dev->checkpt_xor ^= *data_bytes;
1369 + dev->checkpt_byte_offs++;
1370 + i++;
1371 + data_bytes++;
1372 + dev->checkpt_byte_count++;
1375 + return i;
1378 +int yaffs_checkpt_close(struct yaffs_dev *dev)
1380 + int i;
1382 + if (dev->checkpt_open_write) {
1383 + if (dev->checkpt_byte_offs !=
1384 + sizeof(sizeof(struct yaffs_checkpt_chunk_hdr)))
1385 + yaffs2_checkpt_flush_buffer(dev);
1386 + } else if (dev->checkpt_block_list) {
1387 + for (i = 0;
1388 + i < dev->blocks_in_checkpt &&
1389 + dev->checkpt_block_list[i] >= 0; i++) {
1390 + int blk = dev->checkpt_block_list[i];
1391 + struct yaffs_block_info *bi = NULL;
1393 + if (dev->internal_start_block <= blk &&
1394 + blk <= dev->internal_end_block)
1395 + bi = yaffs_get_block_info(dev, blk);
1396 + if (bi && bi->block_state == YAFFS_BLOCK_STATE_EMPTY)
1397 + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
1399 + kfree(dev->checkpt_block_list);
1400 + dev->checkpt_block_list = NULL;
1403 + dev->n_free_chunks -=
1404 + dev->blocks_in_checkpt * dev->param.chunks_per_block;
1405 + dev->n_erased_blocks -= dev->blocks_in_checkpt;
1407 + yaffs_trace(YAFFS_TRACE_CHECKPOINT, "checkpoint byte count %d",
1408 + dev->checkpt_byte_count);
1410 + if (dev->checkpt_buffer) {
1411 + /* free the buffer */
1412 + kfree(dev->checkpt_buffer);
1413 + dev->checkpt_buffer = NULL;
1414 + return 1;
1415 + } else {
1416 + return 0;
1420 +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev)
1422 + /* Erase the checkpoint data */
1424 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
1425 + "checkpoint invalidate of %d blocks",
1426 + dev->blocks_in_checkpt);
1428 + return yaffs_checkpt_erase(dev);
1430 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_checkptrw.h linux-3.12.20/fs/yaffs2/yaffs_checkptrw.h
1431 --- linux-3.12.20.orig/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
1432 +++ linux-3.12.20/fs/yaffs2/yaffs_checkptrw.h 2014-05-17 11:28:49.000000000 +0200
1433 @@ -0,0 +1,33 @@
1435 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1437 + * Copyright (C) 2002-2011 Aleph One Ltd.
1438 + * for Toby Churchill Ltd and Brightstar Engineering
1440 + * Created by Charles Manning <charles@aleph1.co.uk>
1442 + * This program is free software; you can redistribute it and/or modify
1443 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1444 + * published by the Free Software Foundation.
1446 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1447 + */
1449 +#ifndef __YAFFS_CHECKPTRW_H__
1450 +#define __YAFFS_CHECKPTRW_H__
1452 +#include "yaffs_guts.h"
1454 +int yaffs2_checkpt_open(struct yaffs_dev *dev, int writing);
1456 +int yaffs2_checkpt_wr(struct yaffs_dev *dev, const void *data, int n_bytes);
1458 +int yaffs2_checkpt_rd(struct yaffs_dev *dev, void *data, int n_bytes);
1460 +int yaffs2_get_checkpt_sum(struct yaffs_dev *dev, u32 * sum);
1462 +int yaffs_checkpt_close(struct yaffs_dev *dev);
1464 +int yaffs2_checkpt_invalidate_stream(struct yaffs_dev *dev);
1466 +#endif
1467 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_ecc.c linux-3.12.20/fs/yaffs2/yaffs_ecc.c
1468 --- linux-3.12.20.orig/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
1469 +++ linux-3.12.20/fs/yaffs2/yaffs_ecc.c 2014-05-17 11:28:49.000000000 +0200
1470 @@ -0,0 +1,281 @@
1472 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1474 + * Copyright (C) 2002-2011 Aleph One Ltd.
1475 + * for Toby Churchill Ltd and Brightstar Engineering
1477 + * Created by Charles Manning <charles@aleph1.co.uk>
1479 + * This program is free software; you can redistribute it and/or modify
1480 + * it under the terms of the GNU General Public License version 2 as
1481 + * published by the Free Software Foundation.
1482 + */
1485 + * This code implements the ECC algorithm used in SmartMedia.
1487 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1488 + * The two unused bit are set to 1.
1489 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two
1490 + * such ECC blocks are used on a 512-byte NAND page.
1492 + */
1494 +#include "yportenv.h"
1496 +#include "yaffs_ecc.h"
1498 +/* Table generated by gen-ecc.c
1499 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1500 + * for each byte of data. These are instead provided in a table in bits7..2.
1501 + * Bit 0 of each entry indicates whether the entry has an odd or even parity,
1502 + * and therefore this bytes influence on the line parity.
1503 + */
1505 +static const unsigned char column_parity_table[] = {
1506 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1507 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1508 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1509 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1510 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1511 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1512 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1513 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1514 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1515 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1516 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1517 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1518 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1519 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1520 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1521 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1522 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1523 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1524 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1525 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1526 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1527 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1528 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1529 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1530 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1531 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1532 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1533 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1534 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1535 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1536 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1537 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1541 +/* Calculate the ECC for a 256-byte block of data */
1542 +void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc)
1544 + unsigned int i;
1545 + unsigned char col_parity = 0;
1546 + unsigned char line_parity = 0;
1547 + unsigned char line_parity_prime = 0;
1548 + unsigned char t;
1549 + unsigned char b;
1551 + for (i = 0; i < 256; i++) {
1552 + b = column_parity_table[*data++];
1553 + col_parity ^= b;
1555 + if (b & 0x01) { /* odd number of bits in the byte */
1556 + line_parity ^= i;
1557 + line_parity_prime ^= ~i;
1561 + ecc[2] = (~col_parity) | 0x03;
1563 + t = 0;
1564 + if (line_parity & 0x80)
1565 + t |= 0x80;
1566 + if (line_parity_prime & 0x80)
1567 + t |= 0x40;
1568 + if (line_parity & 0x40)
1569 + t |= 0x20;
1570 + if (line_parity_prime & 0x40)
1571 + t |= 0x10;
1572 + if (line_parity & 0x20)
1573 + t |= 0x08;
1574 + if (line_parity_prime & 0x20)
1575 + t |= 0x04;
1576 + if (line_parity & 0x10)
1577 + t |= 0x02;
1578 + if (line_parity_prime & 0x10)
1579 + t |= 0x01;
1580 + ecc[1] = ~t;
1582 + t = 0;
1583 + if (line_parity & 0x08)
1584 + t |= 0x80;
1585 + if (line_parity_prime & 0x08)
1586 + t |= 0x40;
1587 + if (line_parity & 0x04)
1588 + t |= 0x20;
1589 + if (line_parity_prime & 0x04)
1590 + t |= 0x10;
1591 + if (line_parity & 0x02)
1592 + t |= 0x08;
1593 + if (line_parity_prime & 0x02)
1594 + t |= 0x04;
1595 + if (line_parity & 0x01)
1596 + t |= 0x02;
1597 + if (line_parity_prime & 0x01)
1598 + t |= 0x01;
1599 + ecc[0] = ~t;
1603 +/* Correct the ECC on a 256 byte block of data */
1605 +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
1606 + const unsigned char *test_ecc)
1608 + unsigned char d0, d1, d2; /* deltas */
1610 + d0 = read_ecc[0] ^ test_ecc[0];
1611 + d1 = read_ecc[1] ^ test_ecc[1];
1612 + d2 = read_ecc[2] ^ test_ecc[2];
1614 + if ((d0 | d1 | d2) == 0)
1615 + return 0; /* no error */
1617 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1618 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1619 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1620 + /* Single bit (recoverable) error in data */
1622 + unsigned byte;
1623 + unsigned bit;
1625 + bit = byte = 0;
1627 + if (d1 & 0x80)
1628 + byte |= 0x80;
1629 + if (d1 & 0x20)
1630 + byte |= 0x40;
1631 + if (d1 & 0x08)
1632 + byte |= 0x20;
1633 + if (d1 & 0x02)
1634 + byte |= 0x10;
1635 + if (d0 & 0x80)
1636 + byte |= 0x08;
1637 + if (d0 & 0x20)
1638 + byte |= 0x04;
1639 + if (d0 & 0x08)
1640 + byte |= 0x02;
1641 + if (d0 & 0x02)
1642 + byte |= 0x01;
1644 + if (d2 & 0x80)
1645 + bit |= 0x04;
1646 + if (d2 & 0x20)
1647 + bit |= 0x02;
1648 + if (d2 & 0x08)
1649 + bit |= 0x01;
1651 + data[byte] ^= (1 << bit);
1653 + return 1; /* Corrected the error */
1656 + if ((hweight8(d0) + hweight8(d1) + hweight8(d2)) == 1) {
1657 + /* Reccoverable error in ecc */
1659 + read_ecc[0] = test_ecc[0];
1660 + read_ecc[1] = test_ecc[1];
1661 + read_ecc[2] = test_ecc[2];
1663 + return 1; /* Corrected the error */
1666 + /* Unrecoverable error */
1668 + return -1;
1673 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1674 + */
1675 +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
1676 + struct yaffs_ecc_other *ecc_other)
1678 + unsigned int i;
1679 + unsigned char col_parity = 0;
1680 + unsigned line_parity = 0;
1681 + unsigned line_parity_prime = 0;
1682 + unsigned char b;
1684 + for (i = 0; i < n_bytes; i++) {
1685 + b = column_parity_table[*data++];
1686 + col_parity ^= b;
1688 + if (b & 0x01) {
1689 + /* odd number of bits in the byte */
1690 + line_parity ^= i;
1691 + line_parity_prime ^= ~i;
1696 + ecc_other->col_parity = (col_parity >> 2) & 0x3f;
1697 + ecc_other->line_parity = line_parity;
1698 + ecc_other->line_parity_prime = line_parity_prime;
1701 +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
1702 + struct yaffs_ecc_other *read_ecc,
1703 + const struct yaffs_ecc_other *test_ecc)
1705 + unsigned char delta_col; /* column parity delta */
1706 + unsigned delta_line; /* line parity delta */
1707 + unsigned delta_line_prime; /* line parity delta */
1708 + unsigned bit;
1710 + delta_col = read_ecc->col_parity ^ test_ecc->col_parity;
1711 + delta_line = read_ecc->line_parity ^ test_ecc->line_parity;
1712 + delta_line_prime =
1713 + read_ecc->line_parity_prime ^ test_ecc->line_parity_prime;
1715 + if ((delta_col | delta_line | delta_line_prime) == 0)
1716 + return 0; /* no error */
1718 + if (delta_line == ~delta_line_prime &&
1719 + (((delta_col ^ (delta_col >> 1)) & 0x15) == 0x15)) {
1720 + /* Single bit (recoverable) error in data */
1722 + bit = 0;
1724 + if (delta_col & 0x20)
1725 + bit |= 0x04;
1726 + if (delta_col & 0x08)
1727 + bit |= 0x02;
1728 + if (delta_col & 0x02)
1729 + bit |= 0x01;
1731 + if (delta_line >= n_bytes)
1732 + return -1;
1734 + data[delta_line] ^= (1 << bit);
1736 + return 1; /* corrected */
1739 + if ((hweight32(delta_line) +
1740 + hweight32(delta_line_prime) +
1741 + hweight8(delta_col)) == 1) {
1742 + /* Reccoverable error in ecc */
1744 + *read_ecc = *test_ecc;
1745 + return 1; /* corrected */
1748 + /* Unrecoverable error */
1750 + return -1;
1752 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_ecc.h linux-3.12.20/fs/yaffs2/yaffs_ecc.h
1753 --- linux-3.12.20.orig/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1754 +++ linux-3.12.20/fs/yaffs2/yaffs_ecc.h 2014-05-17 11:28:49.000000000 +0200
1755 @@ -0,0 +1,44 @@
1757 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1759 + * Copyright (C) 2002-2011 Aleph One Ltd.
1760 + * for Toby Churchill Ltd and Brightstar Engineering
1762 + * Created by Charles Manning <charles@aleph1.co.uk>
1764 + * This program is free software; you can redistribute it and/or modify
1765 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1766 + * published by the Free Software Foundation.
1768 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1769 + */
1772 + * This code implements the ECC algorithm used in SmartMedia.
1774 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1775 + * The two unused bit are set to 1.
1776 + * The ECC can correct single bit errors in a 256-byte page of data.
1777 + * Thus, two such ECC blocks are used on a 512-byte NAND page.
1779 + */
1781 +#ifndef __YAFFS_ECC_H__
1782 +#define __YAFFS_ECC_H__
1784 +struct yaffs_ecc_other {
1785 + unsigned char col_parity;
1786 + unsigned line_parity;
1787 + unsigned line_parity_prime;
1790 +void yaffs_ecc_calc(const unsigned char *data, unsigned char *ecc);
1791 +int yaffs_ecc_correct(unsigned char *data, unsigned char *read_ecc,
1792 + const unsigned char *test_ecc);
1794 +void yaffs_ecc_calc_other(const unsigned char *data, unsigned n_bytes,
1795 + struct yaffs_ecc_other *ecc);
1796 +int yaffs_ecc_correct_other(unsigned char *data, unsigned n_bytes,
1797 + struct yaffs_ecc_other *read_ecc,
1798 + const struct yaffs_ecc_other *test_ecc);
1799 +#endif
1800 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_getblockinfo.h linux-3.12.20/fs/yaffs2/yaffs_getblockinfo.h
1801 --- linux-3.12.20.orig/fs/yaffs2/yaffs_getblockinfo.h 1970-01-01 01:00:00.000000000 +0100
1802 +++ linux-3.12.20/fs/yaffs2/yaffs_getblockinfo.h 2014-05-17 11:28:49.000000000 +0200
1803 @@ -0,0 +1,35 @@
1805 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1807 + * Copyright (C) 2002-2011 Aleph One Ltd.
1808 + * for Toby Churchill Ltd and Brightstar Engineering
1810 + * Created by Charles Manning <charles@aleph1.co.uk>
1812 + * This program is free software; you can redistribute it and/or modify
1813 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1814 + * published by the Free Software Foundation.
1816 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1817 + */
1819 +#ifndef __YAFFS_GETBLOCKINFO_H__
1820 +#define __YAFFS_GETBLOCKINFO_H__
1822 +#include "yaffs_guts.h"
1823 +#include "yaffs_trace.h"
1825 +/* Function to manipulate block info */
1826 +static inline struct yaffs_block_info *yaffs_get_block_info(struct yaffs_dev
1827 + *dev, int blk)
1829 + if (blk < dev->internal_start_block || blk > dev->internal_end_block) {
1830 + yaffs_trace(YAFFS_TRACE_ERROR,
1831 + "**>> yaffs: get_block_info block %d is not valid",
1832 + blk);
1833 + BUG();
1835 + return &dev->block_info[blk - dev->internal_start_block];
1838 +#endif
1839 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_guts.c linux-3.12.20/fs/yaffs2/yaffs_guts.c
1840 --- linux-3.12.20.orig/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
1841 +++ linux-3.12.20/fs/yaffs2/yaffs_guts.c 2014-05-17 11:28:49.000000000 +0200
1842 @@ -0,0 +1,5146 @@
1844 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1846 + * Copyright (C) 2002-2011 Aleph One Ltd.
1847 + * for Toby Churchill Ltd and Brightstar Engineering
1849 + * Created by Charles Manning <charles@aleph1.co.uk>
1851 + * This program is free software; you can redistribute it and/or modify
1852 + * it under the terms of the GNU General Public License version 2 as
1853 + * published by the Free Software Foundation.
1854 + */
1856 +#include "yportenv.h"
1857 +#include "yaffs_trace.h"
1859 +#include "yaffs_guts.h"
1860 +#include "yaffs_getblockinfo.h"
1861 +#include "yaffs_tagscompat.h"
1862 +#include "yaffs_tagsmarshall.h"
1863 +#include "yaffs_nand.h"
1864 +#include "yaffs_yaffs1.h"
1865 +#include "yaffs_yaffs2.h"
1866 +#include "yaffs_bitmap.h"
1867 +#include "yaffs_verify.h"
1868 +#include "yaffs_nand.h"
1869 +#include "yaffs_packedtags2.h"
1870 +#include "yaffs_nameval.h"
1871 +#include "yaffs_allocator.h"
1872 +#include "yaffs_attribs.h"
1873 +#include "yaffs_summary.h"
1875 +/* Note YAFFS_GC_GOOD_ENOUGH must be <= YAFFS_GC_PASSIVE_THRESHOLD */
1876 +#define YAFFS_GC_GOOD_ENOUGH 2
1877 +#define YAFFS_GC_PASSIVE_THRESHOLD 4
1879 +#include "yaffs_ecc.h"
1881 +/* Forward declarations */
1883 +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
1884 + const u8 *buffer, int n_bytes, int use_reserve);
1886 +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
1887 + int buffer_size);
1889 +/* Function to calculate chunk and offset */
1891 +void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
1892 + int *chunk_out, u32 *offset_out)
1894 + int chunk;
1895 + u32 offset;
1897 + chunk = (u32) (addr >> dev->chunk_shift);
1899 + if (dev->chunk_div == 1) {
1900 + /* easy power of 2 case */
1901 + offset = (u32) (addr & dev->chunk_mask);
1902 + } else {
1903 + /* Non power-of-2 case */
1905 + loff_t chunk_base;
1907 + chunk /= dev->chunk_div;
1909 + chunk_base = ((loff_t) chunk) * dev->data_bytes_per_chunk;
1910 + offset = (u32) (addr - chunk_base);
1913 + *chunk_out = chunk;
1914 + *offset_out = offset;
1917 +/* Function to return the number of shifts for a power of 2 greater than or
1918 + * equal to the given number
1919 + * Note we don't try to cater for all possible numbers and this does not have to
1920 + * be hellishly efficient.
1921 + */
1923 +static inline u32 calc_shifts_ceiling(u32 x)
1925 + int extra_bits;
1926 + int shifts;
1928 + shifts = extra_bits = 0;
1930 + while (x > 1) {
1931 + if (x & 1)
1932 + extra_bits++;
1933 + x >>= 1;
1934 + shifts++;
1937 + if (extra_bits)
1938 + shifts++;
1940 + return shifts;
1943 +/* Function to return the number of shifts to get a 1 in bit 0
1944 + */
1946 +static inline u32 calc_shifts(u32 x)
1948 + u32 shifts;
1950 + shifts = 0;
1952 + if (!x)
1953 + return 0;
1955 + while (!(x & 1)) {
1956 + x >>= 1;
1957 + shifts++;
1960 + return shifts;
1964 + * Temporary buffer manipulations.
1965 + */
1967 +static int yaffs_init_tmp_buffers(struct yaffs_dev *dev)
1969 + int i;
1970 + u8 *buf = (u8 *) 1;
1972 + memset(dev->temp_buffer, 0, sizeof(dev->temp_buffer));
1974 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
1975 + dev->temp_buffer[i].in_use = 0;
1976 + buf = kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
1977 + dev->temp_buffer[i].buffer = buf;
1980 + return buf ? YAFFS_OK : YAFFS_FAIL;
1983 +u8 *yaffs_get_temp_buffer(struct yaffs_dev * dev)
1985 + int i;
1987 + dev->temp_in_use++;
1988 + if (dev->temp_in_use > dev->max_temp)
1989 + dev->max_temp = dev->temp_in_use;
1991 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
1992 + if (dev->temp_buffer[i].in_use == 0) {
1993 + dev->temp_buffer[i].in_use = 1;
1994 + return dev->temp_buffer[i].buffer;
1998 + yaffs_trace(YAFFS_TRACE_BUFFERS, "Out of temp buffers");
1999 + /*
2000 + * If we got here then we have to allocate an unmanaged one
2001 + * This is not good.
2002 + */
2004 + dev->unmanaged_buffer_allocs++;
2005 + return kmalloc(dev->data_bytes_per_chunk, GFP_NOFS);
2009 +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer)
2011 + int i;
2013 + dev->temp_in_use--;
2015 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
2016 + if (dev->temp_buffer[i].buffer == buffer) {
2017 + dev->temp_buffer[i].in_use = 0;
2018 + return;
2022 + if (buffer) {
2023 + /* assume it is an unmanaged one. */
2024 + yaffs_trace(YAFFS_TRACE_BUFFERS,
2025 + "Releasing unmanaged temp buffer");
2026 + kfree(buffer);
2027 + dev->unmanaged_buffer_deallocs++;
2033 + * Functions for robustisizing TODO
2035 + */
2037 +static void yaffs_handle_chunk_wr_ok(struct yaffs_dev *dev, int nand_chunk,
2038 + const u8 *data,
2039 + const struct yaffs_ext_tags *tags)
2041 + (void) dev;
2042 + (void) nand_chunk;
2043 + (void) data;
2044 + (void) tags;
2047 +static void yaffs_handle_chunk_update(struct yaffs_dev *dev, int nand_chunk,
2048 + const struct yaffs_ext_tags *tags)
2050 + (void) dev;
2051 + (void) nand_chunk;
2052 + (void) tags;
2055 +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
2056 + struct yaffs_block_info *bi)
2058 + if (!bi->gc_prioritise) {
2059 + bi->gc_prioritise = 1;
2060 + dev->has_pending_prioritised_gc = 1;
2061 + bi->chunk_error_strikes++;
2063 + if (bi->chunk_error_strikes > 3) {
2064 + bi->needs_retiring = 1; /* Too many stikes, so retire */
2065 + yaffs_trace(YAFFS_TRACE_ALWAYS,
2066 + "yaffs: Block struck out");
2072 +static void yaffs_handle_chunk_wr_error(struct yaffs_dev *dev, int nand_chunk,
2073 + int erased_ok)
2075 + int flash_block = nand_chunk / dev->param.chunks_per_block;
2076 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
2078 + yaffs_handle_chunk_error(dev, bi);
2080 + if (erased_ok) {
2081 + /* Was an actual write failure,
2082 + * so mark the block for retirement.*/
2083 + bi->needs_retiring = 1;
2084 + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
2085 + "**>> Block %d needs retiring", flash_block);
2088 + /* Delete the chunk */
2089 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2090 + yaffs_skip_rest_of_block(dev);
2094 + * Verification code
2095 + */
2098 + * Simple hash function. Needs to have a reasonable spread
2099 + */
2101 +static inline int yaffs_hash_fn(int n)
2103 + if (n < 0)
2104 + n = -n;
2105 + return n % YAFFS_NOBJECT_BUCKETS;
2109 + * Access functions to useful fake objects.
2110 + * Note that root might have a presence in NAND if permissions are set.
2111 + */
2113 +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev)
2115 + return dev->root_dir;
2118 +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev)
2120 + return dev->lost_n_found;
2124 + * Erased NAND checking functions
2125 + */
2127 +int yaffs_check_ff(u8 *buffer, int n_bytes)
2129 + /* Horrible, slow implementation */
2130 + while (n_bytes--) {
2131 + if (*buffer != 0xff)
2132 + return 0;
2133 + buffer++;
2135 + return 1;
2138 +static int yaffs_check_chunk_erased(struct yaffs_dev *dev, int nand_chunk)
2140 + int retval = YAFFS_OK;
2141 + u8 *data = yaffs_get_temp_buffer(dev);
2142 + struct yaffs_ext_tags tags;
2143 + int result;
2145 + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, data, &tags);
2147 + if (tags.ecc_result > YAFFS_ECC_RESULT_NO_ERROR)
2148 + retval = YAFFS_FAIL;
2150 + if (!yaffs_check_ff(data, dev->data_bytes_per_chunk) ||
2151 + tags.chunk_used) {
2152 + yaffs_trace(YAFFS_TRACE_NANDACCESS,
2153 + "Chunk %d not erased", nand_chunk);
2154 + retval = YAFFS_FAIL;
2157 + yaffs_release_temp_buffer(dev, data);
2159 + return retval;
2163 +static int yaffs_verify_chunk_written(struct yaffs_dev *dev,
2164 + int nand_chunk,
2165 + const u8 *data,
2166 + struct yaffs_ext_tags *tags)
2168 + int retval = YAFFS_OK;
2169 + struct yaffs_ext_tags temp_tags;
2170 + u8 *buffer = yaffs_get_temp_buffer(dev);
2171 + int result;
2173 + result = yaffs_rd_chunk_tags_nand(dev, nand_chunk, buffer, &temp_tags);
2174 + if (memcmp(buffer, data, dev->data_bytes_per_chunk) ||
2175 + temp_tags.obj_id != tags->obj_id ||
2176 + temp_tags.chunk_id != tags->chunk_id ||
2177 + temp_tags.n_bytes != tags->n_bytes)
2178 + retval = YAFFS_FAIL;
2180 + yaffs_release_temp_buffer(dev, buffer);
2182 + return retval;
2186 +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks)
2188 + int reserved_chunks;
2189 + int reserved_blocks = dev->param.n_reserved_blocks;
2190 + int checkpt_blocks;
2192 + checkpt_blocks = yaffs_calc_checkpt_blocks_required(dev);
2194 + reserved_chunks =
2195 + (reserved_blocks + checkpt_blocks) * dev->param.chunks_per_block;
2197 + return (dev->n_free_chunks > (reserved_chunks + n_chunks));
2200 +static int yaffs_find_alloc_block(struct yaffs_dev *dev)
2202 + int i;
2203 + struct yaffs_block_info *bi;
2205 + if (dev->n_erased_blocks < 1) {
2206 + /* Hoosterman we've got a problem.
2207 + * Can't get space to gc
2208 + */
2209 + yaffs_trace(YAFFS_TRACE_ERROR,
2210 + "yaffs tragedy: no more erased blocks");
2212 + return -1;
2215 + /* Find an empty block. */
2217 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
2218 + dev->alloc_block_finder++;
2219 + if (dev->alloc_block_finder < dev->internal_start_block
2220 + || dev->alloc_block_finder > dev->internal_end_block) {
2221 + dev->alloc_block_finder = dev->internal_start_block;
2224 + bi = yaffs_get_block_info(dev, dev->alloc_block_finder);
2226 + if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
2227 + bi->block_state = YAFFS_BLOCK_STATE_ALLOCATING;
2228 + dev->seq_number++;
2229 + bi->seq_number = dev->seq_number;
2230 + dev->n_erased_blocks--;
2231 + yaffs_trace(YAFFS_TRACE_ALLOCATE,
2232 + "Allocated block %d, seq %d, %d left" ,
2233 + dev->alloc_block_finder, dev->seq_number,
2234 + dev->n_erased_blocks);
2235 + return dev->alloc_block_finder;
2239 + yaffs_trace(YAFFS_TRACE_ALWAYS,
2240 + "yaffs tragedy: no more erased blocks, but there should have been %d",
2241 + dev->n_erased_blocks);
2243 + return -1;
2246 +static int yaffs_alloc_chunk(struct yaffs_dev *dev, int use_reserver,
2247 + struct yaffs_block_info **block_ptr)
2249 + int ret_val;
2250 + struct yaffs_block_info *bi;
2252 + if (dev->alloc_block < 0) {
2253 + /* Get next block to allocate off */
2254 + dev->alloc_block = yaffs_find_alloc_block(dev);
2255 + dev->alloc_page = 0;
2258 + if (!use_reserver && !yaffs_check_alloc_available(dev, 1)) {
2259 + /* No space unless we're allowed to use the reserve. */
2260 + return -1;
2263 + if (dev->n_erased_blocks < dev->param.n_reserved_blocks
2264 + && dev->alloc_page == 0)
2265 + yaffs_trace(YAFFS_TRACE_ALLOCATE, "Allocating reserve");
2267 + /* Next page please.... */
2268 + if (dev->alloc_block >= 0) {
2269 + bi = yaffs_get_block_info(dev, dev->alloc_block);
2271 + ret_val = (dev->alloc_block * dev->param.chunks_per_block) +
2272 + dev->alloc_page;
2273 + bi->pages_in_use++;
2274 + yaffs_set_chunk_bit(dev, dev->alloc_block, dev->alloc_page);
2276 + dev->alloc_page++;
2278 + dev->n_free_chunks--;
2280 + /* If the block is full set the state to full */
2281 + if (dev->alloc_page >= dev->param.chunks_per_block) {
2282 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
2283 + dev->alloc_block = -1;
2286 + if (block_ptr)
2287 + *block_ptr = bi;
2289 + return ret_val;
2292 + yaffs_trace(YAFFS_TRACE_ERROR,
2293 + "!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!");
2295 + return -1;
2298 +static int yaffs_get_erased_chunks(struct yaffs_dev *dev)
2300 + int n;
2302 + n = dev->n_erased_blocks * dev->param.chunks_per_block;
2304 + if (dev->alloc_block > 0)
2305 + n += (dev->param.chunks_per_block - dev->alloc_page);
2307 + return n;
2312 + * yaffs_skip_rest_of_block() skips over the rest of the allocation block
2313 + * if we don't want to write to it.
2314 + */
2315 +void yaffs_skip_rest_of_block(struct yaffs_dev *dev)
2317 + struct yaffs_block_info *bi;
2319 + if (dev->alloc_block > 0) {
2320 + bi = yaffs_get_block_info(dev, dev->alloc_block);
2321 + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
2322 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
2323 + dev->alloc_block = -1;
2328 +static int yaffs_write_new_chunk(struct yaffs_dev *dev,
2329 + const u8 *data,
2330 + struct yaffs_ext_tags *tags, int use_reserver)
2332 + int attempts = 0;
2333 + int write_ok = 0;
2334 + int chunk;
2336 + yaffs2_checkpt_invalidate(dev);
2338 + do {
2339 + struct yaffs_block_info *bi = 0;
2340 + int erased_ok = 0;
2342 + chunk = yaffs_alloc_chunk(dev, use_reserver, &bi);
2343 + if (chunk < 0) {
2344 + /* no space */
2345 + break;
2348 + /* First check this chunk is erased, if it needs
2349 + * checking. The checking policy (unless forced
2350 + * always on) is as follows:
2352 + * Check the first page we try to write in a block.
2353 + * If the check passes then we don't need to check any
2354 + * more. If the check fails, we check again...
2355 + * If the block has been erased, we don't need to check.
2357 + * However, if the block has been prioritised for gc,
2358 + * then we think there might be something odd about
2359 + * this block and stop using it.
2361 + * Rationale: We should only ever see chunks that have
2362 + * not been erased if there was a partially written
2363 + * chunk due to power loss. This checking policy should
2364 + * catch that case with very few checks and thus save a
2365 + * lot of checks that are most likely not needed.
2367 + * Mods to the above
2368 + * If an erase check fails or the write fails we skip the
2369 + * rest of the block.
2370 + */
2372 + /* let's give it a try */
2373 + attempts++;
2375 + if (dev->param.always_check_erased)
2376 + bi->skip_erased_check = 0;
2378 + if (!bi->skip_erased_check) {
2379 + erased_ok = yaffs_check_chunk_erased(dev, chunk);
2380 + if (erased_ok != YAFFS_OK) {
2381 + yaffs_trace(YAFFS_TRACE_ERROR,
2382 + "**>> yaffs chunk %d was not erased",
2383 + chunk);
2385 + /* If not erased, delete this one,
2386 + * skip rest of block and
2387 + * try another chunk */
2388 + yaffs_chunk_del(dev, chunk, 1, __LINE__);
2389 + yaffs_skip_rest_of_block(dev);
2390 + continue;
2394 + write_ok = yaffs_wr_chunk_tags_nand(dev, chunk, data, tags);
2396 + if (!bi->skip_erased_check)
2397 + write_ok =
2398 + yaffs_verify_chunk_written(dev, chunk, data, tags);
2400 + if (write_ok != YAFFS_OK) {
2401 + /* Clean up aborted write, skip to next block and
2402 + * try another chunk */
2403 + yaffs_handle_chunk_wr_error(dev, chunk, erased_ok);
2404 + continue;
2407 + bi->skip_erased_check = 1;
2409 + /* Copy the data into the robustification buffer */
2410 + yaffs_handle_chunk_wr_ok(dev, chunk, data, tags);
2412 + } while (write_ok != YAFFS_OK &&
2413 + (yaffs_wr_attempts <= 0 || attempts <= yaffs_wr_attempts));
2415 + if (!write_ok)
2416 + chunk = -1;
2418 + if (attempts > 1) {
2419 + yaffs_trace(YAFFS_TRACE_ERROR,
2420 + "**>> yaffs write required %d attempts",
2421 + attempts);
2422 + dev->n_retried_writes += (attempts - 1);
2425 + return chunk;
2429 + * Block retiring for handling a broken block.
2430 + */
2432 +static void yaffs_retire_block(struct yaffs_dev *dev, int flash_block)
2434 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, flash_block);
2436 + yaffs2_checkpt_invalidate(dev);
2438 + yaffs2_clear_oldest_dirty_seq(dev, bi);
2440 + if (yaffs_mark_bad(dev, flash_block) != YAFFS_OK) {
2441 + if (yaffs_erase_block(dev, flash_block) != YAFFS_OK) {
2442 + yaffs_trace(YAFFS_TRACE_ALWAYS,
2443 + "yaffs: Failed to mark bad and erase block %d",
2444 + flash_block);
2445 + } else {
2446 + struct yaffs_ext_tags tags;
2447 + int chunk_id =
2448 + flash_block * dev->param.chunks_per_block;
2450 + u8 *buffer = yaffs_get_temp_buffer(dev);
2452 + memset(buffer, 0xff, dev->data_bytes_per_chunk);
2453 + memset(&tags, 0, sizeof(tags));
2454 + tags.seq_number = YAFFS_SEQUENCE_BAD_BLOCK;
2455 + if (dev->tagger.write_chunk_tags_fn(dev, chunk_id -
2456 + dev->chunk_offset,
2457 + buffer,
2458 + &tags) != YAFFS_OK)
2459 + yaffs_trace(YAFFS_TRACE_ALWAYS,
2460 + "yaffs: Failed to write bad block marker to block %d",
2461 + flash_block);
2463 + yaffs_release_temp_buffer(dev, buffer);
2467 + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
2468 + bi->gc_prioritise = 0;
2469 + bi->needs_retiring = 0;
2471 + dev->n_retired_blocks++;
2474 +/*---------------- Name handling functions ------------*/
2476 +static u16 yaffs_calc_name_sum(const YCHAR *name)
2478 + u16 sum = 0;
2479 + u16 i = 1;
2481 + if (!name)
2482 + return 0;
2484 + while ((*name) && i < (YAFFS_MAX_NAME_LENGTH / 2)) {
2486 + /* 0x1f mask is case insensitive */
2487 + sum += ((*name) & 0x1f) * i;
2488 + i++;
2489 + name++;
2491 + return sum;
2495 +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR * name)
2497 + memset(obj->short_name, 0, sizeof(obj->short_name));
2499 + if (name && !name[0]) {
2500 + yaffs_fix_null_name(obj, obj->short_name,
2501 + YAFFS_SHORT_NAME_LENGTH);
2502 + name = obj->short_name;
2503 + } else if (name &&
2504 + strnlen(name, YAFFS_SHORT_NAME_LENGTH + 1) <=
2505 + YAFFS_SHORT_NAME_LENGTH) {
2506 + strcpy(obj->short_name, name);
2509 + obj->sum = yaffs_calc_name_sum(name);
2512 +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
2513 + const struct yaffs_obj_hdr *oh)
2515 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
2516 + YCHAR tmp_name[YAFFS_MAX_NAME_LENGTH + 1];
2517 + memset(tmp_name, 0, sizeof(tmp_name));
2518 + yaffs_load_name_from_oh(obj->my_dev, tmp_name, oh->name,
2519 + YAFFS_MAX_NAME_LENGTH + 1);
2520 + yaffs_set_obj_name(obj, tmp_name);
2521 +#else
2522 + yaffs_set_obj_name(obj, oh->name);
2523 +#endif
2526 +loff_t yaffs_max_file_size(struct yaffs_dev *dev)
2528 + if(sizeof(loff_t) < 8)
2529 + return YAFFS_MAX_FILE_SIZE_32;
2530 + else
2531 + return ((loff_t) YAFFS_MAX_CHUNK_ID) * dev->data_bytes_per_chunk;
2534 +/*-------------------- TNODES -------------------
2536 + * List of spare tnodes
2537 + * The list is hooked together using the first pointer
2538 + * in the tnode.
2539 + */
2541 +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev)
2543 + struct yaffs_tnode *tn = yaffs_alloc_raw_tnode(dev);
2545 + if (tn) {
2546 + memset(tn, 0, dev->tnode_size);
2547 + dev->n_tnodes++;
2550 + dev->checkpoint_blocks_required = 0; /* force recalculation */
2552 + return tn;
2555 +/* FreeTnode frees up a tnode and puts it back on the free list */
2556 +static void yaffs_free_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
2558 + yaffs_free_raw_tnode(dev, tn);
2559 + dev->n_tnodes--;
2560 + dev->checkpoint_blocks_required = 0; /* force recalculation */
2563 +static void yaffs_deinit_tnodes_and_objs(struct yaffs_dev *dev)
2565 + yaffs_deinit_raw_tnodes_and_objs(dev);
2566 + dev->n_obj = 0;
2567 + dev->n_tnodes = 0;
2570 +static void yaffs_load_tnode_0(struct yaffs_dev *dev, struct yaffs_tnode *tn,
2571 + unsigned pos, unsigned val)
2573 + u32 *map = (u32 *) tn;
2574 + u32 bit_in_map;
2575 + u32 bit_in_word;
2576 + u32 word_in_map;
2577 + u32 mask;
2579 + pos &= YAFFS_TNODES_LEVEL0_MASK;
2580 + val >>= dev->chunk_grp_bits;
2582 + bit_in_map = pos * dev->tnode_width;
2583 + word_in_map = bit_in_map / 32;
2584 + bit_in_word = bit_in_map & (32 - 1);
2586 + mask = dev->tnode_mask << bit_in_word;
2588 + map[word_in_map] &= ~mask;
2589 + map[word_in_map] |= (mask & (val << bit_in_word));
2591 + if (dev->tnode_width > (32 - bit_in_word)) {
2592 + bit_in_word = (32 - bit_in_word);
2593 + word_in_map++;
2594 + mask =
2595 + dev->tnode_mask >> bit_in_word;
2596 + map[word_in_map] &= ~mask;
2597 + map[word_in_map] |= (mask & (val >> bit_in_word));
2601 +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
2602 + unsigned pos)
2604 + u32 *map = (u32 *) tn;
2605 + u32 bit_in_map;
2606 + u32 bit_in_word;
2607 + u32 word_in_map;
2608 + u32 val;
2610 + pos &= YAFFS_TNODES_LEVEL0_MASK;
2612 + bit_in_map = pos * dev->tnode_width;
2613 + word_in_map = bit_in_map / 32;
2614 + bit_in_word = bit_in_map & (32 - 1);
2616 + val = map[word_in_map] >> bit_in_word;
2618 + if (dev->tnode_width > (32 - bit_in_word)) {
2619 + bit_in_word = (32 - bit_in_word);
2620 + word_in_map++;
2621 + val |= (map[word_in_map] << bit_in_word);
2624 + val &= dev->tnode_mask;
2625 + val <<= dev->chunk_grp_bits;
2627 + return val;
2630 +/* ------------------- End of individual tnode manipulation -----------------*/
2632 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
2633 + * The look up tree is represented by the top tnode and the number of top_level
2634 + * in the tree. 0 means only the level 0 tnode is in the tree.
2635 + */
2637 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
2638 +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
2639 + struct yaffs_file_var *file_struct,
2640 + u32 chunk_id)
2642 + struct yaffs_tnode *tn = file_struct->top;
2643 + u32 i;
2644 + int required_depth;
2645 + int level = file_struct->top_level;
2647 + (void) dev;
2649 + /* Check sane level and chunk Id */
2650 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL)
2651 + return NULL;
2653 + if (chunk_id > YAFFS_MAX_CHUNK_ID)
2654 + return NULL;
2656 + /* First check we're tall enough (ie enough top_level) */
2658 + i = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
2659 + required_depth = 0;
2660 + while (i) {
2661 + i >>= YAFFS_TNODES_INTERNAL_BITS;
2662 + required_depth++;
2665 + if (required_depth > file_struct->top_level)
2666 + return NULL; /* Not tall enough, so we can't find it */
2668 + /* Traverse down to level 0 */
2669 + while (level > 0 && tn) {
2670 + tn = tn->internal[(chunk_id >>
2671 + (YAFFS_TNODES_LEVEL0_BITS +
2672 + (level - 1) *
2673 + YAFFS_TNODES_INTERNAL_BITS)) &
2674 + YAFFS_TNODES_INTERNAL_MASK];
2675 + level--;
2678 + return tn;
2681 +/* add_find_tnode_0 finds the level 0 tnode if it exists,
2682 + * otherwise first expands the tree.
2683 + * This happens in two steps:
2684 + * 1. If the tree isn't tall enough, then make it taller.
2685 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
2687 + * Used when modifying the tree.
2689 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the
2690 + * specified tn will be plugged into the ttree.
2691 + */
2693 +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
2694 + struct yaffs_file_var *file_struct,
2695 + u32 chunk_id,
2696 + struct yaffs_tnode *passed_tn)
2698 + int required_depth;
2699 + int i;
2700 + int l;
2701 + struct yaffs_tnode *tn;
2702 + u32 x;
2704 + /* Check sane level and page Id */
2705 + if (file_struct->top_level < 0 ||
2706 + file_struct->top_level > YAFFS_TNODES_MAX_LEVEL)
2707 + return NULL;
2709 + if (chunk_id > YAFFS_MAX_CHUNK_ID)
2710 + return NULL;
2712 + /* First check we're tall enough (ie enough top_level) */
2714 + x = chunk_id >> YAFFS_TNODES_LEVEL0_BITS;
2715 + required_depth = 0;
2716 + while (x) {
2717 + x >>= YAFFS_TNODES_INTERNAL_BITS;
2718 + required_depth++;
2721 + if (required_depth > file_struct->top_level) {
2722 + /* Not tall enough, gotta make the tree taller */
2723 + for (i = file_struct->top_level; i < required_depth; i++) {
2725 + tn = yaffs_get_tnode(dev);
2727 + if (tn) {
2728 + tn->internal[0] = file_struct->top;
2729 + file_struct->top = tn;
2730 + file_struct->top_level++;
2731 + } else {
2732 + yaffs_trace(YAFFS_TRACE_ERROR,
2733 + "yaffs: no more tnodes");
2734 + return NULL;
2739 + /* Traverse down to level 0, adding anything we need */
2741 + l = file_struct->top_level;
2742 + tn = file_struct->top;
2744 + if (l > 0) {
2745 + while (l > 0 && tn) {
2746 + x = (chunk_id >>
2747 + (YAFFS_TNODES_LEVEL0_BITS +
2748 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
2749 + YAFFS_TNODES_INTERNAL_MASK;
2751 + if ((l > 1) && !tn->internal[x]) {
2752 + /* Add missing non-level-zero tnode */
2753 + tn->internal[x] = yaffs_get_tnode(dev);
2754 + if (!tn->internal[x])
2755 + return NULL;
2756 + } else if (l == 1) {
2757 + /* Looking from level 1 at level 0 */
2758 + if (passed_tn) {
2759 + /* If we already have one, release it */
2760 + if (tn->internal[x])
2761 + yaffs_free_tnode(dev,
2762 + tn->internal[x]);
2763 + tn->internal[x] = passed_tn;
2765 + } else if (!tn->internal[x]) {
2766 + /* Don't have one, none passed in */
2767 + tn->internal[x] = yaffs_get_tnode(dev);
2768 + if (!tn->internal[x])
2769 + return NULL;
2773 + tn = tn->internal[x];
2774 + l--;
2776 + } else {
2777 + /* top is level 0 */
2778 + if (passed_tn) {
2779 + memcpy(tn, passed_tn,
2780 + (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8);
2781 + yaffs_free_tnode(dev, passed_tn);
2785 + return tn;
2788 +static int yaffs_tags_match(const struct yaffs_ext_tags *tags, int obj_id,
2789 + int chunk_obj)
2791 + return (tags->chunk_id == chunk_obj &&
2792 + tags->obj_id == obj_id &&
2793 + !tags->is_deleted) ? 1 : 0;
2797 +static int yaffs_find_chunk_in_group(struct yaffs_dev *dev, int the_chunk,
2798 + struct yaffs_ext_tags *tags, int obj_id,
2799 + int inode_chunk)
2801 + int j;
2803 + for (j = 0; the_chunk && j < dev->chunk_grp_size; j++) {
2804 + if (yaffs_check_chunk_bit
2805 + (dev, the_chunk / dev->param.chunks_per_block,
2806 + the_chunk % dev->param.chunks_per_block)) {
2808 + if (dev->chunk_grp_size == 1)
2809 + return the_chunk;
2810 + else {
2811 + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
2812 + tags);
2813 + if (yaffs_tags_match(tags,
2814 + obj_id, inode_chunk)) {
2815 + /* found it; */
2816 + return the_chunk;
2820 + the_chunk++;
2822 + return -1;
2825 +int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2826 + struct yaffs_ext_tags *tags)
2828 + /*Get the Tnode, then get the level 0 offset chunk offset */
2829 + struct yaffs_tnode *tn;
2830 + int the_chunk = -1;
2831 + struct yaffs_ext_tags local_tags;
2832 + int ret_val = -1;
2833 + struct yaffs_dev *dev = in->my_dev;
2835 + if (!tags) {
2836 + /* Passed a NULL, so use our own tags space */
2837 + tags = &local_tags;
2840 + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2842 + if (!tn)
2843 + return ret_val;
2845 + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2847 + ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2848 + inode_chunk);
2849 + return ret_val;
2852 +static int yaffs_find_del_file_chunk(struct yaffs_obj *in, int inode_chunk,
2853 + struct yaffs_ext_tags *tags)
2855 + /* Get the Tnode, then get the level 0 offset chunk offset */
2856 + struct yaffs_tnode *tn;
2857 + int the_chunk = -1;
2858 + struct yaffs_ext_tags local_tags;
2859 + struct yaffs_dev *dev = in->my_dev;
2860 + int ret_val = -1;
2862 + if (!tags) {
2863 + /* Passed a NULL, so use our own tags space */
2864 + tags = &local_tags;
2867 + tn = yaffs_find_tnode_0(dev, &in->variant.file_variant, inode_chunk);
2869 + if (!tn)
2870 + return ret_val;
2872 + the_chunk = yaffs_get_group_base(dev, tn, inode_chunk);
2874 + ret_val = yaffs_find_chunk_in_group(dev, the_chunk, tags, in->obj_id,
2875 + inode_chunk);
2877 + /* Delete the entry in the filestructure (if found) */
2878 + if (ret_val != -1)
2879 + yaffs_load_tnode_0(dev, tn, inode_chunk, 0);
2881 + return ret_val;
2884 +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
2885 + int nand_chunk, int in_scan)
2887 + /* NB in_scan is zero unless scanning.
2888 + * For forward scanning, in_scan is > 0;
2889 + * for backward scanning in_scan is < 0
2891 + * nand_chunk = 0 is a dummy insert to make sure the tnodes are there.
2892 + */
2894 + struct yaffs_tnode *tn;
2895 + struct yaffs_dev *dev = in->my_dev;
2896 + int existing_cunk;
2897 + struct yaffs_ext_tags existing_tags;
2898 + struct yaffs_ext_tags new_tags;
2899 + unsigned existing_serial, new_serial;
2901 + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE) {
2902 + /* Just ignore an attempt at putting a chunk into a non-file
2903 + * during scanning.
2904 + * If it is not during Scanning then something went wrong!
2905 + */
2906 + if (!in_scan) {
2907 + yaffs_trace(YAFFS_TRACE_ERROR,
2908 + "yaffs tragedy:attempt to put data chunk into a non-file"
2909 + );
2910 + BUG();
2913 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
2914 + return YAFFS_OK;
2917 + tn = yaffs_add_find_tnode_0(dev,
2918 + &in->variant.file_variant,
2919 + inode_chunk, NULL);
2920 + if (!tn)
2921 + return YAFFS_FAIL;
2923 + if (!nand_chunk)
2924 + /* Dummy insert, bail now */
2925 + return YAFFS_OK;
2927 + existing_cunk = yaffs_get_group_base(dev, tn, inode_chunk);
2929 + if (in_scan != 0) {
2930 + /* If we're scanning then we need to test for duplicates
2931 + * NB This does not need to be efficient since it should only
2932 + * happen when the power fails during a write, then only one
2933 + * chunk should ever be affected.
2935 + * Correction for YAFFS2: This could happen quite a lot and we
2936 + * need to think about efficiency! TODO
2937 + * Update: For backward scanning we don't need to re-read tags
2938 + * so this is quite cheap.
2939 + */
2941 + if (existing_cunk > 0) {
2942 + /* NB Right now existing chunk will not be real
2943 + * chunk_id if the chunk group size > 1
2944 + * thus we have to do a FindChunkInFile to get the
2945 + * real chunk id.
2947 + * We have a duplicate now we need to decide which
2948 + * one to use:
2950 + * Backwards scanning YAFFS2: The old one is what
2951 + * we use, dump the new one.
2952 + * YAFFS1: Get both sets of tags and compare serial
2953 + * numbers.
2954 + */
2956 + if (in_scan > 0) {
2957 + /* Only do this for forward scanning */
2958 + yaffs_rd_chunk_tags_nand(dev,
2959 + nand_chunk,
2960 + NULL, &new_tags);
2962 + /* Do a proper find */
2963 + existing_cunk =
2964 + yaffs_find_chunk_in_file(in, inode_chunk,
2965 + &existing_tags);
2968 + if (existing_cunk <= 0) {
2969 + /*Hoosterman - how did this happen? */
2971 + yaffs_trace(YAFFS_TRACE_ERROR,
2972 + "yaffs tragedy: existing chunk < 0 in scan"
2973 + );
2977 + /* NB The deleted flags should be false, otherwise
2978 + * the chunks will not be loaded during a scan
2979 + */
2981 + if (in_scan > 0) {
2982 + new_serial = new_tags.serial_number;
2983 + existing_serial = existing_tags.serial_number;
2986 + if ((in_scan > 0) &&
2987 + (existing_cunk <= 0 ||
2988 + ((existing_serial + 1) & 3) == new_serial)) {
2989 + /* Forward scanning.
2990 + * Use new
2991 + * Delete the old one and drop through to
2992 + * update the tnode
2993 + */
2994 + yaffs_chunk_del(dev, existing_cunk, 1,
2995 + __LINE__);
2996 + } else {
2997 + /* Backward scanning or we want to use the
2998 + * existing one
2999 + * Delete the new one and return early so that
3000 + * the tnode isn't changed
3001 + */
3002 + yaffs_chunk_del(dev, nand_chunk, 1, __LINE__);
3003 + return YAFFS_OK;
3009 + if (existing_cunk == 0)
3010 + in->n_data_chunks++;
3012 + yaffs_load_tnode_0(dev, tn, inode_chunk, nand_chunk);
3014 + return YAFFS_OK;
3017 +static void yaffs_soft_del_chunk(struct yaffs_dev *dev, int chunk)
3019 + struct yaffs_block_info *the_block;
3020 + unsigned block_no;
3022 + yaffs_trace(YAFFS_TRACE_DELETION, "soft delete chunk %d", chunk);
3024 + block_no = chunk / dev->param.chunks_per_block;
3025 + the_block = yaffs_get_block_info(dev, block_no);
3026 + if (the_block) {
3027 + the_block->soft_del_pages++;
3028 + dev->n_free_chunks++;
3029 + yaffs2_update_oldest_dirty_seq(dev, block_no, the_block);
3033 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all
3034 + * the chunks in the file.
3035 + * All soft deleting does is increment the block's softdelete count and pulls
3036 + * the chunk out of the tnode.
3037 + * Thus, essentially this is the same as DeleteWorker except that the chunks
3038 + * are soft deleted.
3039 + */
3041 +static int yaffs_soft_del_worker(struct yaffs_obj *in, struct yaffs_tnode *tn,
3042 + u32 level, int chunk_offset)
3044 + int i;
3045 + int the_chunk;
3046 + int all_done = 1;
3047 + struct yaffs_dev *dev = in->my_dev;
3049 + if (!tn)
3050 + return 1;
3052 + if (level > 0) {
3053 + for (i = YAFFS_NTNODES_INTERNAL - 1;
3054 + all_done && i >= 0;
3055 + i--) {
3056 + if (tn->internal[i]) {
3057 + all_done =
3058 + yaffs_soft_del_worker(in,
3059 + tn->internal[i],
3060 + level - 1,
3061 + (chunk_offset <<
3062 + YAFFS_TNODES_INTERNAL_BITS)
3063 + + i);
3064 + if (all_done) {
3065 + yaffs_free_tnode(dev,
3066 + tn->internal[i]);
3067 + tn->internal[i] = NULL;
3068 + } else {
3069 + /* Can this happen? */
3073 + return (all_done) ? 1 : 0;
3076 + /* level 0 */
3077 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
3078 + the_chunk = yaffs_get_group_base(dev, tn, i);
3079 + if (the_chunk) {
3080 + yaffs_soft_del_chunk(dev, the_chunk);
3081 + yaffs_load_tnode_0(dev, tn, i, 0);
3084 + return 1;
3087 +static void yaffs_remove_obj_from_dir(struct yaffs_obj *obj)
3089 + struct yaffs_dev *dev = obj->my_dev;
3090 + struct yaffs_obj *parent;
3092 + yaffs_verify_obj_in_dir(obj);
3093 + parent = obj->parent;
3095 + yaffs_verify_dir(parent);
3097 + if (dev && dev->param.remove_obj_fn)
3098 + dev->param.remove_obj_fn(obj);
3100 + list_del_init(&obj->siblings);
3101 + obj->parent = NULL;
3103 + yaffs_verify_dir(parent);
3106 +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj)
3108 + if (!directory) {
3109 + yaffs_trace(YAFFS_TRACE_ALWAYS,
3110 + "tragedy: Trying to add an object to a null pointer directory"
3111 + );
3112 + BUG();
3113 + return;
3115 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3116 + yaffs_trace(YAFFS_TRACE_ALWAYS,
3117 + "tragedy: Trying to add an object to a non-directory"
3118 + );
3119 + BUG();
3122 + if (obj->siblings.prev == NULL) {
3123 + /* Not initialised */
3124 + BUG();
3127 + yaffs_verify_dir(directory);
3129 + yaffs_remove_obj_from_dir(obj);
3131 + /* Now add it */
3132 + list_add(&obj->siblings, &directory->variant.dir_variant.children);
3133 + obj->parent = directory;
3135 + if (directory == obj->my_dev->unlinked_dir
3136 + || directory == obj->my_dev->del_dir) {
3137 + obj->unlinked = 1;
3138 + obj->my_dev->n_unlinked_files++;
3139 + obj->rename_allowed = 0;
3142 + yaffs_verify_dir(directory);
3143 + yaffs_verify_obj_in_dir(obj);
3146 +static int yaffs_change_obj_name(struct yaffs_obj *obj,
3147 + struct yaffs_obj *new_dir,
3148 + const YCHAR *new_name, int force, int shadows)
3150 + int unlink_op;
3151 + int del_op;
3152 + struct yaffs_obj *existing_target;
3154 + if (new_dir == NULL)
3155 + new_dir = obj->parent; /* use the old directory */
3157 + if (new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
3158 + yaffs_trace(YAFFS_TRACE_ALWAYS,
3159 + "tragedy: yaffs_change_obj_name: new_dir is not a directory"
3160 + );
3161 + BUG();
3164 + unlink_op = (new_dir == obj->my_dev->unlinked_dir);
3165 + del_op = (new_dir == obj->my_dev->del_dir);
3167 + existing_target = yaffs_find_by_name(new_dir, new_name);
3169 + /* If the object is a file going into the unlinked directory,
3170 + * then it is OK to just stuff it in since duplicate names are OK.
3171 + * else only proceed if the new name does not exist and we're putting
3172 + * it into a directory.
3173 + */
3174 + if (!(unlink_op || del_op || force ||
3175 + shadows > 0 || !existing_target) ||
3176 + new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3177 + return YAFFS_FAIL;
3179 + yaffs_set_obj_name(obj, new_name);
3180 + obj->dirty = 1;
3181 + yaffs_add_obj_to_dir(new_dir, obj);
3183 + if (unlink_op)
3184 + obj->unlinked = 1;
3186 + /* If it is a deletion then we mark it as a shrink for gc */
3187 + if (yaffs_update_oh(obj, new_name, 0, del_op, shadows, NULL) >= 0)
3188 + return YAFFS_OK;
3190 + return YAFFS_FAIL;
3193 +/*------------------------ Short Operations Cache ------------------------------
3194 + * In many situations where there is no high level buffering a lot of
3195 + * reads might be short sequential reads, and a lot of writes may be short
3196 + * sequential writes. eg. scanning/writing a jpeg file.
3197 + * In these cases, a short read/write cache can provide a huge perfomance
3198 + * benefit with dumb-as-a-rock code.
3199 + * In Linux, the page cache provides read buffering and the short op cache
3200 + * provides write buffering.
3202 + * There are a small number (~10) of cache chunks per device so that we don't
3203 + * need a very intelligent search.
3204 + */
3206 +static int yaffs_obj_cache_dirty(struct yaffs_obj *obj)
3208 + struct yaffs_dev *dev = obj->my_dev;
3209 + int i;
3210 + struct yaffs_cache *cache;
3211 + int n_caches = obj->my_dev->param.n_caches;
3213 + for (i = 0; i < n_caches; i++) {
3214 + cache = &dev->cache[i];
3215 + if (cache->object == obj && cache->dirty)
3216 + return 1;
3219 + return 0;
3222 +static void yaffs_flush_file_cache(struct yaffs_obj *obj)
3224 + struct yaffs_dev *dev = obj->my_dev;
3225 + int lowest = -99; /* Stop compiler whining. */
3226 + int i;
3227 + struct yaffs_cache *cache;
3228 + int chunk_written = 0;
3229 + int n_caches = obj->my_dev->param.n_caches;
3231 + if (n_caches < 1)
3232 + return;
3233 + do {
3234 + cache = NULL;
3236 + /* Find the lowest dirty chunk for this object */
3237 + for (i = 0; i < n_caches; i++) {
3238 + if (dev->cache[i].object == obj &&
3239 + dev->cache[i].dirty) {
3240 + if (!cache ||
3241 + dev->cache[i].chunk_id < lowest) {
3242 + cache = &dev->cache[i];
3243 + lowest = cache->chunk_id;
3248 + if (cache && !cache->locked) {
3249 + /* Write it out and free it up */
3250 + chunk_written =
3251 + yaffs_wr_data_obj(cache->object,
3252 + cache->chunk_id,
3253 + cache->data,
3254 + cache->n_bytes, 1);
3255 + cache->dirty = 0;
3256 + cache->object = NULL;
3258 + } while (cache && chunk_written > 0);
3260 + if (cache)
3261 + /* Hoosterman, disk full while writing cache out. */
3262 + yaffs_trace(YAFFS_TRACE_ERROR,
3263 + "yaffs tragedy: no space during cache write");
3266 +/*yaffs_flush_whole_cache(dev)
3269 + */
3271 +void yaffs_flush_whole_cache(struct yaffs_dev *dev)
3273 + struct yaffs_obj *obj;
3274 + int n_caches = dev->param.n_caches;
3275 + int i;
3277 + /* Find a dirty object in the cache and flush it...
3278 + * until there are no further dirty objects.
3279 + */
3280 + do {
3281 + obj = NULL;
3282 + for (i = 0; i < n_caches && !obj; i++) {
3283 + if (dev->cache[i].object && dev->cache[i].dirty)
3284 + obj = dev->cache[i].object;
3286 + if (obj)
3287 + yaffs_flush_file_cache(obj);
3288 + } while (obj);
3292 +/* Grab us a cache chunk for use.
3293 + * First look for an empty one.
3294 + * Then look for the least recently used non-dirty one.
3295 + * Then look for the least recently used dirty one...., flush and look again.
3296 + */
3297 +static struct yaffs_cache *yaffs_grab_chunk_worker(struct yaffs_dev *dev)
3299 + int i;
3301 + if (dev->param.n_caches > 0) {
3302 + for (i = 0; i < dev->param.n_caches; i++) {
3303 + if (!dev->cache[i].object)
3304 + return &dev->cache[i];
3307 + return NULL;
3310 +static struct yaffs_cache *yaffs_grab_chunk_cache(struct yaffs_dev *dev)
3312 + struct yaffs_cache *cache;
3313 + struct yaffs_obj *the_obj;
3314 + int usage;
3315 + int i;
3316 + int pushout;
3318 + if (dev->param.n_caches < 1)
3319 + return NULL;
3321 + /* Try find a non-dirty one... */
3323 + cache = yaffs_grab_chunk_worker(dev);
3325 + if (!cache) {
3326 + /* They were all dirty, find the LRU object and flush
3327 + * its cache, then find again.
3328 + * NB what's here is not very accurate,
3329 + * we actually flush the object with the LRU chunk.
3330 + */
3332 + /* With locking we can't assume we can use entry zero,
3333 + * Set the_obj to a valid pointer for Coverity. */
3334 + the_obj = dev->cache[0].object;
3335 + usage = -1;
3336 + cache = NULL;
3337 + pushout = -1;
3339 + for (i = 0; i < dev->param.n_caches; i++) {
3340 + if (dev->cache[i].object &&
3341 + !dev->cache[i].locked &&
3342 + (dev->cache[i].last_use < usage ||
3343 + !cache)) {
3344 + usage = dev->cache[i].last_use;
3345 + the_obj = dev->cache[i].object;
3346 + cache = &dev->cache[i];
3347 + pushout = i;
3351 + if (!cache || cache->dirty) {
3352 + /* Flush and try again */
3353 + yaffs_flush_file_cache(the_obj);
3354 + cache = yaffs_grab_chunk_worker(dev);
3357 + return cache;
3360 +/* Find a cached chunk */
3361 +static struct yaffs_cache *yaffs_find_chunk_cache(const struct yaffs_obj *obj,
3362 + int chunk_id)
3364 + struct yaffs_dev *dev = obj->my_dev;
3365 + int i;
3367 + if (dev->param.n_caches < 1)
3368 + return NULL;
3370 + for (i = 0; i < dev->param.n_caches; i++) {
3371 + if (dev->cache[i].object == obj &&
3372 + dev->cache[i].chunk_id == chunk_id) {
3373 + dev->cache_hits++;
3375 + return &dev->cache[i];
3378 + return NULL;
3381 +/* Mark the chunk for the least recently used algorithym */
3382 +static void yaffs_use_cache(struct yaffs_dev *dev, struct yaffs_cache *cache,
3383 + int is_write)
3385 + int i;
3387 + if (dev->param.n_caches < 1)
3388 + return;
3390 + if (dev->cache_last_use < 0 ||
3391 + dev->cache_last_use > 100000000) {
3392 + /* Reset the cache usages */
3393 + for (i = 1; i < dev->param.n_caches; i++)
3394 + dev->cache[i].last_use = 0;
3396 + dev->cache_last_use = 0;
3398 + dev->cache_last_use++;
3399 + cache->last_use = dev->cache_last_use;
3401 + if (is_write)
3402 + cache->dirty = 1;
3405 +/* Invalidate a single cache page.
3406 + * Do this when a whole page gets written,
3407 + * ie the short cache for this page is no longer valid.
3408 + */
3409 +static void yaffs_invalidate_chunk_cache(struct yaffs_obj *object, int chunk_id)
3411 + struct yaffs_cache *cache;
3413 + if (object->my_dev->param.n_caches > 0) {
3414 + cache = yaffs_find_chunk_cache(object, chunk_id);
3416 + if (cache)
3417 + cache->object = NULL;
3421 +/* Invalidate all the cache pages associated with this object
3422 + * Do this whenever ther file is deleted or resized.
3423 + */
3424 +static void yaffs_invalidate_whole_cache(struct yaffs_obj *in)
3426 + int i;
3427 + struct yaffs_dev *dev = in->my_dev;
3429 + if (dev->param.n_caches > 0) {
3430 + /* Invalidate it. */
3431 + for (i = 0; i < dev->param.n_caches; i++) {
3432 + if (dev->cache[i].object == in)
3433 + dev->cache[i].object = NULL;
3438 +static void yaffs_unhash_obj(struct yaffs_obj *obj)
3440 + int bucket;
3441 + struct yaffs_dev *dev = obj->my_dev;
3443 + /* If it is still linked into the bucket list, free from the list */
3444 + if (!list_empty(&obj->hash_link)) {
3445 + list_del_init(&obj->hash_link);
3446 + bucket = yaffs_hash_fn(obj->obj_id);
3447 + dev->obj_bucket[bucket].count--;
3451 +/* FreeObject frees up a Object and puts it back on the free list */
3452 +static void yaffs_free_obj(struct yaffs_obj *obj)
3454 + struct yaffs_dev *dev;
3456 + if (!obj) {
3457 + BUG();
3458 + return;
3460 + dev = obj->my_dev;
3461 + yaffs_trace(YAFFS_TRACE_OS, "FreeObject %p inode %p",
3462 + obj, obj->my_inode);
3463 + if (obj->parent)
3464 + BUG();
3465 + if (!list_empty(&obj->siblings))
3466 + BUG();
3468 + if (obj->my_inode) {
3469 + /* We're still hooked up to a cached inode.
3470 + * Don't delete now, but mark for later deletion
3471 + */
3472 + obj->defered_free = 1;
3473 + return;
3476 + yaffs_unhash_obj(obj);
3478 + yaffs_free_raw_obj(dev, obj);
3479 + dev->n_obj--;
3480 + dev->checkpoint_blocks_required = 0; /* force recalculation */
3483 +void yaffs_handle_defered_free(struct yaffs_obj *obj)
3485 + if (obj->defered_free)
3486 + yaffs_free_obj(obj);
3489 +static int yaffs_generic_obj_del(struct yaffs_obj *in)
3491 + /* Iinvalidate the file's data in the cache, without flushing. */
3492 + yaffs_invalidate_whole_cache(in);
3494 + if (in->my_dev->param.is_yaffs2 && in->parent != in->my_dev->del_dir) {
3495 + /* Move to unlinked directory so we have a deletion record */
3496 + yaffs_change_obj_name(in, in->my_dev->del_dir, _Y("deleted"), 0,
3497 + 0);
3500 + yaffs_remove_obj_from_dir(in);
3501 + yaffs_chunk_del(in->my_dev, in->hdr_chunk, 1, __LINE__);
3502 + in->hdr_chunk = 0;
3504 + yaffs_free_obj(in);
3505 + return YAFFS_OK;
3509 +static void yaffs_soft_del_file(struct yaffs_obj *obj)
3511 + if (!obj->deleted ||
3512 + obj->variant_type != YAFFS_OBJECT_TYPE_FILE ||
3513 + obj->soft_del)
3514 + return;
3516 + if (obj->n_data_chunks <= 0) {
3517 + /* Empty file with no duplicate object headers,
3518 + * just delete it immediately */
3519 + yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
3520 + obj->variant.file_variant.top = NULL;
3521 + yaffs_trace(YAFFS_TRACE_TRACING,
3522 + "yaffs: Deleting empty file %d",
3523 + obj->obj_id);
3524 + yaffs_generic_obj_del(obj);
3525 + } else {
3526 + yaffs_soft_del_worker(obj,
3527 + obj->variant.file_variant.top,
3528 + obj->variant.
3529 + file_variant.top_level, 0);
3530 + obj->soft_del = 1;
3534 +/* Pruning removes any part of the file structure tree that is beyond the
3535 + * bounds of the file (ie that does not point to chunks).
3537 + * A file should only get pruned when its size is reduced.
3539 + * Before pruning, the chunks must be pulled from the tree and the
3540 + * level 0 tnode entries must be zeroed out.
3541 + * Could also use this for file deletion, but that's probably better handled
3542 + * by a special case.
3544 + * This function is recursive. For levels > 0 the function is called again on
3545 + * any sub-tree. For level == 0 we just check if the sub-tree has data.
3546 + * If there is no data in a subtree then it is pruned.
3547 + */
3549 +static struct yaffs_tnode *yaffs_prune_worker(struct yaffs_dev *dev,
3550 + struct yaffs_tnode *tn, u32 level,
3551 + int del0)
3553 + int i;
3554 + int has_data;
3556 + if (!tn)
3557 + return tn;
3559 + has_data = 0;
3561 + if (level > 0) {
3562 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
3563 + if (tn->internal[i]) {
3564 + tn->internal[i] =
3565 + yaffs_prune_worker(dev,
3566 + tn->internal[i],
3567 + level - 1,
3568 + (i == 0) ? del0 : 1);
3571 + if (tn->internal[i])
3572 + has_data++;
3574 + } else {
3575 + int tnode_size_u32 = dev->tnode_size / sizeof(u32);
3576 + u32 *map = (u32 *) tn;
3578 + for (i = 0; !has_data && i < tnode_size_u32; i++) {
3579 + if (map[i])
3580 + has_data++;
3584 + if (has_data == 0 && del0) {
3585 + /* Free and return NULL */
3586 + yaffs_free_tnode(dev, tn);
3587 + tn = NULL;
3589 + return tn;
3592 +static int yaffs_prune_tree(struct yaffs_dev *dev,
3593 + struct yaffs_file_var *file_struct)
3595 + int i;
3596 + int has_data;
3597 + int done = 0;
3598 + struct yaffs_tnode *tn;
3600 + if (file_struct->top_level < 1)
3601 + return YAFFS_OK;
3603 + file_struct->top =
3604 + yaffs_prune_worker(dev, file_struct->top, file_struct->top_level, 0);
3606 + /* Now we have a tree with all the non-zero branches NULL but
3607 + * the height is the same as it was.
3608 + * Let's see if we can trim internal tnodes to shorten the tree.
3609 + * We can do this if only the 0th element in the tnode is in use
3610 + * (ie all the non-zero are NULL)
3611 + */
3613 + while (file_struct->top_level && !done) {
3614 + tn = file_struct->top;
3616 + has_data = 0;
3617 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
3618 + if (tn->internal[i])
3619 + has_data++;
3622 + if (!has_data) {
3623 + file_struct->top = tn->internal[0];
3624 + file_struct->top_level--;
3625 + yaffs_free_tnode(dev, tn);
3626 + } else {
3627 + done = 1;
3631 + return YAFFS_OK;
3634 +/*-------------------- End of File Structure functions.-------------------*/
3636 +/* alloc_empty_obj gets us a clean Object.*/
3637 +static struct yaffs_obj *yaffs_alloc_empty_obj(struct yaffs_dev *dev)
3639 + struct yaffs_obj *obj = yaffs_alloc_raw_obj(dev);
3641 + if (!obj)
3642 + return obj;
3644 + dev->n_obj++;
3646 + /* Now sweeten it up... */
3648 + memset(obj, 0, sizeof(struct yaffs_obj));
3649 + obj->being_created = 1;
3651 + obj->my_dev = dev;
3652 + obj->hdr_chunk = 0;
3653 + obj->variant_type = YAFFS_OBJECT_TYPE_UNKNOWN;
3654 + INIT_LIST_HEAD(&(obj->hard_links));
3655 + INIT_LIST_HEAD(&(obj->hash_link));
3656 + INIT_LIST_HEAD(&obj->siblings);
3658 + /* Now make the directory sane */
3659 + if (dev->root_dir) {
3660 + obj->parent = dev->root_dir;
3661 + list_add(&(obj->siblings),
3662 + &dev->root_dir->variant.dir_variant.children);
3665 + /* Add it to the lost and found directory.
3666 + * NB Can't put root or lost-n-found in lost-n-found so
3667 + * check if lost-n-found exists first
3668 + */
3669 + if (dev->lost_n_found)
3670 + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
3672 + obj->being_created = 0;
3674 + dev->checkpoint_blocks_required = 0; /* force recalculation */
3676 + return obj;
3679 +static int yaffs_find_nice_bucket(struct yaffs_dev *dev)
3681 + int i;
3682 + int l = 999;
3683 + int lowest = 999999;
3685 + /* Search for the shortest list or one that
3686 + * isn't too long.
3687 + */
3689 + for (i = 0; i < 10 && lowest > 4; i++) {
3690 + dev->bucket_finder++;
3691 + dev->bucket_finder %= YAFFS_NOBJECT_BUCKETS;
3692 + if (dev->obj_bucket[dev->bucket_finder].count < lowest) {
3693 + lowest = dev->obj_bucket[dev->bucket_finder].count;
3694 + l = dev->bucket_finder;
3698 + return l;
3701 +static int yaffs_new_obj_id(struct yaffs_dev *dev)
3703 + int bucket = yaffs_find_nice_bucket(dev);
3704 + int found = 0;
3705 + struct list_head *i;
3706 + u32 n = (u32) bucket;
3708 + /* Now find an object value that has not already been taken
3709 + * by scanning the list.
3710 + */
3712 + while (!found) {
3713 + found = 1;
3714 + n += YAFFS_NOBJECT_BUCKETS;
3715 + if (1 || dev->obj_bucket[bucket].count > 0) {
3716 + list_for_each(i, &dev->obj_bucket[bucket].list) {
3717 + /* If there is already one in the list */
3718 + if (i && list_entry(i, struct yaffs_obj,
3719 + hash_link)->obj_id == n) {
3720 + found = 0;
3725 + return n;
3728 +static void yaffs_hash_obj(struct yaffs_obj *in)
3730 + int bucket = yaffs_hash_fn(in->obj_id);
3731 + struct yaffs_dev *dev = in->my_dev;
3733 + list_add(&in->hash_link, &dev->obj_bucket[bucket].list);
3734 + dev->obj_bucket[bucket].count++;
3737 +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number)
3739 + int bucket = yaffs_hash_fn(number);
3740 + struct list_head *i;
3741 + struct yaffs_obj *in;
3743 + list_for_each(i, &dev->obj_bucket[bucket].list) {
3744 + /* Look if it is in the list */
3745 + in = list_entry(i, struct yaffs_obj, hash_link);
3746 + if (in->obj_id == number) {
3747 + /* Don't show if it is defered free */
3748 + if (in->defered_free)
3749 + return NULL;
3750 + return in;
3754 + return NULL;
3757 +static struct yaffs_obj *yaffs_new_obj(struct yaffs_dev *dev, int number,
3758 + enum yaffs_obj_type type)
3760 + struct yaffs_obj *the_obj = NULL;
3761 + struct yaffs_tnode *tn = NULL;
3763 + if (number < 0)
3764 + number = yaffs_new_obj_id(dev);
3766 + if (type == YAFFS_OBJECT_TYPE_FILE) {
3767 + tn = yaffs_get_tnode(dev);
3768 + if (!tn)
3769 + return NULL;
3772 + the_obj = yaffs_alloc_empty_obj(dev);
3773 + if (!the_obj) {
3774 + if (tn)
3775 + yaffs_free_tnode(dev, tn);
3776 + return NULL;
3779 + the_obj->fake = 0;
3780 + the_obj->rename_allowed = 1;
3781 + the_obj->unlink_allowed = 1;
3782 + the_obj->obj_id = number;
3783 + yaffs_hash_obj(the_obj);
3784 + the_obj->variant_type = type;
3785 + yaffs_load_current_time(the_obj, 1, 1);
3787 + switch (type) {
3788 + case YAFFS_OBJECT_TYPE_FILE:
3789 + the_obj->variant.file_variant.file_size = 0;
3790 + the_obj->variant.file_variant.scanned_size = 0;
3791 + the_obj->variant.file_variant.shrink_size =
3792 + yaffs_max_file_size(dev);
3793 + the_obj->variant.file_variant.top_level = 0;
3794 + the_obj->variant.file_variant.top = tn;
3795 + break;
3796 + case YAFFS_OBJECT_TYPE_DIRECTORY:
3797 + INIT_LIST_HEAD(&the_obj->variant.dir_variant.children);
3798 + INIT_LIST_HEAD(&the_obj->variant.dir_variant.dirty);
3799 + break;
3800 + case YAFFS_OBJECT_TYPE_SYMLINK:
3801 + case YAFFS_OBJECT_TYPE_HARDLINK:
3802 + case YAFFS_OBJECT_TYPE_SPECIAL:
3803 + /* No action required */
3804 + break;
3805 + case YAFFS_OBJECT_TYPE_UNKNOWN:
3806 + /* todo this should not happen */
3807 + break;
3809 + return the_obj;
3812 +static struct yaffs_obj *yaffs_create_fake_dir(struct yaffs_dev *dev,
3813 + int number, u32 mode)
3816 + struct yaffs_obj *obj =
3817 + yaffs_new_obj(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
3819 + if (!obj)
3820 + return NULL;
3822 + obj->fake = 1; /* it is fake so it might not use NAND */
3823 + obj->rename_allowed = 0;
3824 + obj->unlink_allowed = 0;
3825 + obj->deleted = 0;
3826 + obj->unlinked = 0;
3827 + obj->yst_mode = mode;
3828 + obj->my_dev = dev;
3829 + obj->hdr_chunk = 0; /* Not a valid chunk. */
3830 + return obj;
3835 +static void yaffs_init_tnodes_and_objs(struct yaffs_dev *dev)
3837 + int i;
3839 + dev->n_obj = 0;
3840 + dev->n_tnodes = 0;
3841 + yaffs_init_raw_tnodes_and_objs(dev);
3843 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
3844 + INIT_LIST_HEAD(&dev->obj_bucket[i].list);
3845 + dev->obj_bucket[i].count = 0;
3849 +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
3850 + int number,
3851 + enum yaffs_obj_type type)
3853 + struct yaffs_obj *the_obj = NULL;
3855 + if (number > 0)
3856 + the_obj = yaffs_find_by_number(dev, number);
3858 + if (!the_obj)
3859 + the_obj = yaffs_new_obj(dev, number, type);
3861 + return the_obj;
3865 +YCHAR *yaffs_clone_str(const YCHAR *str)
3867 + YCHAR *new_str = NULL;
3868 + int len;
3870 + if (!str)
3871 + str = _Y("");
3873 + len = strnlen(str, YAFFS_MAX_ALIAS_LENGTH);
3874 + new_str = kmalloc((len + 1) * sizeof(YCHAR), GFP_NOFS);
3875 + if (new_str) {
3876 + strncpy(new_str, str, len);
3877 + new_str[len] = 0;
3879 + return new_str;
3883 + *yaffs_update_parent() handles fixing a directories mtime and ctime when a new
3884 + * link (ie. name) is created or deleted in the directory.
3886 + * ie.
3887 + * create dir/a : update dir's mtime/ctime
3888 + * rm dir/a: update dir's mtime/ctime
3889 + * modify dir/a: don't update dir's mtimme/ctime
3891 + * This can be handled immediately or defered. Defering helps reduce the number
3892 + * of updates when many files in a directory are changed within a brief period.
3894 + * If the directory updating is defered then yaffs_update_dirty_dirs must be
3895 + * called periodically.
3896 + */
3898 +static void yaffs_update_parent(struct yaffs_obj *obj)
3900 + struct yaffs_dev *dev;
3902 + if (!obj)
3903 + return;
3904 + dev = obj->my_dev;
3905 + obj->dirty = 1;
3906 + yaffs_load_current_time(obj, 0, 1);
3907 + if (dev->param.defered_dir_update) {
3908 + struct list_head *link = &obj->variant.dir_variant.dirty;
3910 + if (list_empty(link)) {
3911 + list_add(link, &dev->dirty_dirs);
3912 + yaffs_trace(YAFFS_TRACE_BACKGROUND,
3913 + "Added object %d to dirty directories",
3914 + obj->obj_id);
3917 + } else {
3918 + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
3922 +void yaffs_update_dirty_dirs(struct yaffs_dev *dev)
3924 + struct list_head *link;
3925 + struct yaffs_obj *obj;
3926 + struct yaffs_dir_var *d_s;
3927 + union yaffs_obj_var *o_v;
3929 + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update dirty directories");
3931 + while (!list_empty(&dev->dirty_dirs)) {
3932 + link = dev->dirty_dirs.next;
3933 + list_del_init(link);
3935 + d_s = list_entry(link, struct yaffs_dir_var, dirty);
3936 + o_v = list_entry(d_s, union yaffs_obj_var, dir_variant);
3937 + obj = list_entry(o_v, struct yaffs_obj, variant);
3939 + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Update directory %d",
3940 + obj->obj_id);
3942 + if (obj->dirty)
3943 + yaffs_update_oh(obj, NULL, 0, 0, 0, NULL);
3948 + * Mknod (create) a new object.
3949 + * equiv_obj only has meaning for a hard link;
3950 + * alias_str only has meaning for a symlink.
3951 + * rdev only has meaning for devices (a subset of special objects)
3952 + */
3954 +static struct yaffs_obj *yaffs_create_obj(enum yaffs_obj_type type,
3955 + struct yaffs_obj *parent,
3956 + const YCHAR *name,
3957 + u32 mode,
3958 + u32 uid,
3959 + u32 gid,
3960 + struct yaffs_obj *equiv_obj,
3961 + const YCHAR *alias_str, u32 rdev)
3963 + struct yaffs_obj *in;
3964 + YCHAR *str = NULL;
3965 + struct yaffs_dev *dev = parent->my_dev;
3967 + /* Check if the entry exists.
3968 + * If it does then fail the call since we don't want a dup. */
3969 + if (yaffs_find_by_name(parent, name))
3970 + return NULL;
3972 + if (type == YAFFS_OBJECT_TYPE_SYMLINK) {
3973 + str = yaffs_clone_str(alias_str);
3974 + if (!str)
3975 + return NULL;
3978 + in = yaffs_new_obj(dev, -1, type);
3980 + if (!in) {
3981 + kfree(str);
3982 + return NULL;
3985 + in->hdr_chunk = 0;
3986 + in->valid = 1;
3987 + in->variant_type = type;
3989 + in->yst_mode = mode;
3991 + yaffs_attribs_init(in, gid, uid, rdev);
3993 + in->n_data_chunks = 0;
3995 + yaffs_set_obj_name(in, name);
3996 + in->dirty = 1;
3998 + yaffs_add_obj_to_dir(parent, in);
4000 + in->my_dev = parent->my_dev;
4002 + switch (type) {
4003 + case YAFFS_OBJECT_TYPE_SYMLINK:
4004 + in->variant.symlink_variant.alias = str;
4005 + break;
4006 + case YAFFS_OBJECT_TYPE_HARDLINK:
4007 + in->variant.hardlink_variant.equiv_obj = equiv_obj;
4008 + in->variant.hardlink_variant.equiv_id = equiv_obj->obj_id;
4009 + list_add(&in->hard_links, &equiv_obj->hard_links);
4010 + break;
4011 + case YAFFS_OBJECT_TYPE_FILE:
4012 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4013 + case YAFFS_OBJECT_TYPE_SPECIAL:
4014 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4015 + /* do nothing */
4016 + break;
4019 + if (yaffs_update_oh(in, name, 0, 0, 0, NULL) < 0) {
4020 + /* Could not create the object header, fail */
4021 + yaffs_del_obj(in);
4022 + in = NULL;
4025 + if (in)
4026 + yaffs_update_parent(parent);
4028 + return in;
4031 +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
4032 + const YCHAR *name, u32 mode, u32 uid,
4033 + u32 gid)
4035 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
4036 + uid, gid, NULL, NULL, 0);
4039 +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
4040 + u32 mode, u32 uid, u32 gid)
4042 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
4043 + mode, uid, gid, NULL, NULL, 0);
4046 +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
4047 + const YCHAR *name, u32 mode, u32 uid,
4048 + u32 gid, u32 rdev)
4050 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
4051 + uid, gid, NULL, NULL, rdev);
4054 +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
4055 + const YCHAR *name, u32 mode, u32 uid,
4056 + u32 gid, const YCHAR *alias)
4058 + return yaffs_create_obj(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
4059 + uid, gid, NULL, alias, 0);
4062 +/* yaffs_link_obj returns the object id of the equivalent object.*/
4063 +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR * name,
4064 + struct yaffs_obj *equiv_obj)
4066 + /* Get the real object in case we were fed a hard link obj */
4067 + equiv_obj = yaffs_get_equivalent_obj(equiv_obj);
4069 + if (yaffs_create_obj(YAFFS_OBJECT_TYPE_HARDLINK,
4070 + parent, name, 0, 0, 0,
4071 + equiv_obj, NULL, 0))
4072 + return equiv_obj;
4074 + return NULL;
4080 +/*---------------------- Block Management and Page Allocation -------------*/
4082 +static void yaffs_deinit_blocks(struct yaffs_dev *dev)
4084 + if (dev->block_info_alt && dev->block_info)
4085 + vfree(dev->block_info);
4086 + else
4087 + kfree(dev->block_info);
4089 + dev->block_info_alt = 0;
4091 + dev->block_info = NULL;
4093 + if (dev->chunk_bits_alt && dev->chunk_bits)
4094 + vfree(dev->chunk_bits);
4095 + else
4096 + kfree(dev->chunk_bits);
4097 + dev->chunk_bits_alt = 0;
4098 + dev->chunk_bits = NULL;
4101 +static int yaffs_init_blocks(struct yaffs_dev *dev)
4103 + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
4105 + dev->block_info = NULL;
4106 + dev->chunk_bits = NULL;
4107 + dev->alloc_block = -1; /* force it to get a new one */
4109 + /* If the first allocation strategy fails, thry the alternate one */
4110 + dev->block_info =
4111 + kmalloc(n_blocks * sizeof(struct yaffs_block_info), GFP_NOFS);
4112 + if (!dev->block_info) {
4113 + dev->block_info =
4114 + vmalloc(n_blocks * sizeof(struct yaffs_block_info));
4115 + dev->block_info_alt = 1;
4116 + } else {
4117 + dev->block_info_alt = 0;
4120 + if (!dev->block_info)
4121 + goto alloc_error;
4123 + /* Set up dynamic blockinfo stuff. Round up bytes. */
4124 + dev->chunk_bit_stride = (dev->param.chunks_per_block + 7) / 8;
4125 + dev->chunk_bits =
4126 + kmalloc(dev->chunk_bit_stride * n_blocks, GFP_NOFS);
4127 + if (!dev->chunk_bits) {
4128 + dev->chunk_bits =
4129 + vmalloc(dev->chunk_bit_stride * n_blocks);
4130 + dev->chunk_bits_alt = 1;
4131 + } else {
4132 + dev->chunk_bits_alt = 0;
4134 + if (!dev->chunk_bits)
4135 + goto alloc_error;
4138 + memset(dev->block_info, 0, n_blocks * sizeof(struct yaffs_block_info));
4139 + memset(dev->chunk_bits, 0, dev->chunk_bit_stride * n_blocks);
4140 + return YAFFS_OK;
4142 +alloc_error:
4143 + yaffs_deinit_blocks(dev);
4144 + return YAFFS_FAIL;
4148 +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no)
4150 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block_no);
4151 + int erased_ok = 0;
4152 + int i;
4154 + /* If the block is still healthy erase it and mark as clean.
4155 + * If the block has had a data failure, then retire it.
4156 + */
4158 + yaffs_trace(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
4159 + "yaffs_block_became_dirty block %d state %d %s",
4160 + block_no, bi->block_state,
4161 + (bi->needs_retiring) ? "needs retiring" : "");
4163 + yaffs2_clear_oldest_dirty_seq(dev, bi);
4165 + bi->block_state = YAFFS_BLOCK_STATE_DIRTY;
4167 + /* If this is the block being garbage collected then stop gc'ing */
4168 + if (block_no == dev->gc_block)
4169 + dev->gc_block = 0;
4171 + /* If this block is currently the best candidate for gc
4172 + * then drop as a candidate */
4173 + if (block_no == dev->gc_dirtiest) {
4174 + dev->gc_dirtiest = 0;
4175 + dev->gc_pages_in_use = 0;
4178 + if (!bi->needs_retiring) {
4179 + yaffs2_checkpt_invalidate(dev);
4180 + erased_ok = yaffs_erase_block(dev, block_no);
4181 + if (!erased_ok) {
4182 + dev->n_erase_failures++;
4183 + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4184 + "**>> Erasure failed %d", block_no);
4188 + /* Verify erasure if needed */
4189 + if (erased_ok &&
4190 + ((yaffs_trace_mask & YAFFS_TRACE_ERASE) ||
4191 + !yaffs_skip_verification(dev))) {
4192 + for (i = 0; i < dev->param.chunks_per_block; i++) {
4193 + if (!yaffs_check_chunk_erased(dev,
4194 + block_no * dev->param.chunks_per_block + i)) {
4195 + yaffs_trace(YAFFS_TRACE_ERROR,
4196 + ">>Block %d erasure supposedly OK, but chunk %d not erased",
4197 + block_no, i);
4202 + if (!erased_ok) {
4203 + /* We lost a block of free space */
4204 + dev->n_free_chunks -= dev->param.chunks_per_block;
4205 + yaffs_retire_block(dev, block_no);
4206 + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4207 + "**>> Block %d retired", block_no);
4208 + return;
4211 + /* Clean it up... */
4212 + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
4213 + bi->seq_number = 0;
4214 + dev->n_erased_blocks++;
4215 + bi->pages_in_use = 0;
4216 + bi->soft_del_pages = 0;
4217 + bi->has_shrink_hdr = 0;
4218 + bi->skip_erased_check = 1; /* Clean, so no need to check */
4219 + bi->gc_prioritise = 0;
4220 + bi->has_summary = 0;
4222 + yaffs_clear_chunk_bits(dev, block_no);
4224 + yaffs_trace(YAFFS_TRACE_ERASE, "Erased block %d", block_no);
4227 +static inline int yaffs_gc_process_chunk(struct yaffs_dev *dev,
4228 + struct yaffs_block_info *bi,
4229 + int old_chunk, u8 *buffer)
4231 + int new_chunk;
4232 + int mark_flash = 1;
4233 + struct yaffs_ext_tags tags;
4234 + struct yaffs_obj *object;
4235 + int matching_chunk;
4236 + int ret_val = YAFFS_OK;
4238 + memset(&tags, 0, sizeof(tags));
4239 + yaffs_rd_chunk_tags_nand(dev, old_chunk,
4240 + buffer, &tags);
4241 + object = yaffs_find_by_number(dev, tags.obj_id);
4243 + yaffs_trace(YAFFS_TRACE_GC_DETAIL,
4244 + "Collecting chunk in block %d, %d %d %d ",
4245 + dev->gc_chunk, tags.obj_id,
4246 + tags.chunk_id, tags.n_bytes);
4248 + if (object && !yaffs_skip_verification(dev)) {
4249 + if (tags.chunk_id == 0)
4250 + matching_chunk =
4251 + object->hdr_chunk;
4252 + else if (object->soft_del)
4253 + /* Defeat the test */
4254 + matching_chunk = old_chunk;
4255 + else
4256 + matching_chunk =
4257 + yaffs_find_chunk_in_file
4258 + (object, tags.chunk_id,
4259 + NULL);
4261 + if (old_chunk != matching_chunk)
4262 + yaffs_trace(YAFFS_TRACE_ERROR,
4263 + "gc: page in gc mismatch: %d %d %d %d",
4264 + old_chunk,
4265 + matching_chunk,
4266 + tags.obj_id,
4267 + tags.chunk_id);
4270 + if (!object) {
4271 + yaffs_trace(YAFFS_TRACE_ERROR,
4272 + "page %d in gc has no object: %d %d %d ",
4273 + old_chunk,
4274 + tags.obj_id, tags.chunk_id,
4275 + tags.n_bytes);
4278 + if (object &&
4279 + object->deleted &&
4280 + object->soft_del && tags.chunk_id != 0) {
4281 + /* Data chunk in a soft deleted file,
4282 + * throw it away.
4283 + * It's a soft deleted data chunk,
4284 + * No need to copy this, just forget
4285 + * about it and fix up the object.
4286 + */
4288 + /* Free chunks already includes
4289 + * softdeleted chunks, how ever this
4290 + * chunk is going to soon be really
4291 + * deleted which will increment free
4292 + * chunks. We have to decrement free
4293 + * chunks so this works out properly.
4294 + */
4295 + dev->n_free_chunks--;
4296 + bi->soft_del_pages--;
4298 + object->n_data_chunks--;
4299 + if (object->n_data_chunks <= 0) {
4300 + /* remeber to clean up obj */
4301 + dev->gc_cleanup_list[dev->n_clean_ups] = tags.obj_id;
4302 + dev->n_clean_ups++;
4304 + mark_flash = 0;
4305 + } else if (object) {
4306 + /* It's either a data chunk in a live
4307 + * file or an ObjectHeader, so we're
4308 + * interested in it.
4309 + * NB Need to keep the ObjectHeaders of
4310 + * deleted files until the whole file
4311 + * has been deleted off
4312 + */
4313 + tags.serial_number++;
4314 + dev->n_gc_copies++;
4316 + if (tags.chunk_id == 0) {
4317 + /* It is an object Id,
4318 + * We need to nuke the
4319 + * shrinkheader flags since its
4320 + * work is done.
4321 + * Also need to clean up
4322 + * shadowing.
4323 + */
4324 + struct yaffs_obj_hdr *oh;
4325 + oh = (struct yaffs_obj_hdr *) buffer;
4327 + oh->is_shrink = 0;
4328 + tags.extra_is_shrink = 0;
4329 + oh->shadows_obj = 0;
4330 + oh->inband_shadowed_obj_id = 0;
4331 + tags.extra_shadows = 0;
4333 + /* Update file size */
4334 + if (object->variant_type == YAFFS_OBJECT_TYPE_FILE) {
4335 + yaffs_oh_size_load(oh,
4336 + object->variant.file_variant.file_size);
4337 + tags.extra_file_size =
4338 + object->variant.file_variant.file_size;
4341 + yaffs_verify_oh(object, oh, &tags, 1);
4342 + new_chunk =
4343 + yaffs_write_new_chunk(dev, (u8 *) oh, &tags, 1);
4344 + } else {
4345 + new_chunk =
4346 + yaffs_write_new_chunk(dev, buffer, &tags, 1);
4349 + if (new_chunk < 0) {
4350 + ret_val = YAFFS_FAIL;
4351 + } else {
4353 + /* Now fix up the Tnodes etc. */
4355 + if (tags.chunk_id == 0) {
4356 + /* It's a header */
4357 + object->hdr_chunk = new_chunk;
4358 + object->serial = tags.serial_number;
4359 + } else {
4360 + /* It's a data chunk */
4361 + yaffs_put_chunk_in_file(object, tags.chunk_id,
4362 + new_chunk, 0);
4366 + if (ret_val == YAFFS_OK)
4367 + yaffs_chunk_del(dev, old_chunk, mark_flash, __LINE__);
4368 + return ret_val;
4371 +static int yaffs_gc_block(struct yaffs_dev *dev, int block, int whole_block)
4373 + int old_chunk;
4374 + int ret_val = YAFFS_OK;
4375 + int i;
4376 + int is_checkpt_block;
4377 + int max_copies;
4378 + int chunks_before = yaffs_get_erased_chunks(dev);
4379 + int chunks_after;
4380 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, block);
4382 + is_checkpt_block = (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT);
4384 + yaffs_trace(YAFFS_TRACE_TRACING,
4385 + "Collecting block %d, in use %d, shrink %d, whole_block %d",
4386 + block, bi->pages_in_use, bi->has_shrink_hdr,
4387 + whole_block);
4389 + /*yaffs_verify_free_chunks(dev); */
4391 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL)
4392 + bi->block_state = YAFFS_BLOCK_STATE_COLLECTING;
4394 + bi->has_shrink_hdr = 0; /* clear the flag so that the block can erase */
4396 + dev->gc_disable = 1;
4398 + yaffs_summary_gc(dev, block);
4400 + if (is_checkpt_block || !yaffs_still_some_chunks(dev, block)) {
4401 + yaffs_trace(YAFFS_TRACE_TRACING,
4402 + "Collecting block %d that has no chunks in use",
4403 + block);
4404 + yaffs_block_became_dirty(dev, block);
4405 + } else {
4407 + u8 *buffer = yaffs_get_temp_buffer(dev);
4409 + yaffs_verify_blk(dev, bi, block);
4411 + max_copies = (whole_block) ? dev->param.chunks_per_block : 5;
4412 + old_chunk = block * dev->param.chunks_per_block + dev->gc_chunk;
4414 + for (/* init already done */ ;
4415 + ret_val == YAFFS_OK &&
4416 + dev->gc_chunk < dev->param.chunks_per_block &&
4417 + (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) &&
4418 + max_copies > 0;
4419 + dev->gc_chunk++, old_chunk++) {
4420 + if (yaffs_check_chunk_bit(dev, block, dev->gc_chunk)) {
4421 + /* Page is in use and might need to be copied */
4422 + max_copies--;
4423 + ret_val = yaffs_gc_process_chunk(dev, bi,
4424 + old_chunk, buffer);
4427 + yaffs_release_temp_buffer(dev, buffer);
4430 + yaffs_verify_collected_blk(dev, bi, block);
4432 + if (bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
4433 + /*
4434 + * The gc did not complete. Set block state back to FULL
4435 + * because checkpointing does not restore gc.
4436 + */
4437 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
4438 + } else {
4439 + /* The gc completed. */
4440 + /* Do any required cleanups */
4441 + for (i = 0; i < dev->n_clean_ups; i++) {
4442 + /* Time to delete the file too */
4443 + struct yaffs_obj *object =
4444 + yaffs_find_by_number(dev, dev->gc_cleanup_list[i]);
4445 + if (object) {
4446 + yaffs_free_tnode(dev,
4447 + object->variant.file_variant.top);
4448 + object->variant.file_variant.top = NULL;
4449 + yaffs_trace(YAFFS_TRACE_GC,
4450 + "yaffs: About to finally delete object %d",
4451 + object->obj_id);
4452 + yaffs_generic_obj_del(object);
4453 + object->my_dev->n_deleted_files--;
4457 + chunks_after = yaffs_get_erased_chunks(dev);
4458 + if (chunks_before >= chunks_after)
4459 + yaffs_trace(YAFFS_TRACE_GC,
4460 + "gc did not increase free chunks before %d after %d",
4461 + chunks_before, chunks_after);
4462 + dev->gc_block = 0;
4463 + dev->gc_chunk = 0;
4464 + dev->n_clean_ups = 0;
4467 + dev->gc_disable = 0;
4469 + return ret_val;
4473 + * find_gc_block() selects the dirtiest block (or close enough)
4474 + * for garbage collection.
4475 + */
4477 +static unsigned yaffs_find_gc_block(struct yaffs_dev *dev,
4478 + int aggressive, int background)
4480 + int i;
4481 + int iterations;
4482 + unsigned selected = 0;
4483 + int prioritised = 0;
4484 + int prioritised_exist = 0;
4485 + struct yaffs_block_info *bi;
4486 + int threshold;
4488 + /* First let's see if we need to grab a prioritised block */
4489 + if (dev->has_pending_prioritised_gc && !aggressive) {
4490 + dev->gc_dirtiest = 0;
4491 + bi = dev->block_info;
4492 + for (i = dev->internal_start_block;
4493 + i <= dev->internal_end_block && !selected; i++) {
4495 + if (bi->gc_prioritise) {
4496 + prioritised_exist = 1;
4497 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
4498 + yaffs_block_ok_for_gc(dev, bi)) {
4499 + selected = i;
4500 + prioritised = 1;
4503 + bi++;
4506 + /*
4507 + * If there is a prioritised block and none was selected then
4508 + * this happened because there is at least one old dirty block
4509 + * gumming up the works. Let's gc the oldest dirty block.
4510 + */
4512 + if (prioritised_exist &&
4513 + !selected && dev->oldest_dirty_block > 0)
4514 + selected = dev->oldest_dirty_block;
4516 + if (!prioritised_exist) /* None found, so we can clear this */
4517 + dev->has_pending_prioritised_gc = 0;
4520 + /* If we're doing aggressive GC then we are happy to take a less-dirty
4521 + * block, and search harder.
4522 + * else (leasurely gc), then we only bother to do this if the
4523 + * block has only a few pages in use.
4524 + */
4526 + if (!selected) {
4527 + int pages_used;
4528 + int n_blocks =
4529 + dev->internal_end_block - dev->internal_start_block + 1;
4530 + if (aggressive) {
4531 + threshold = dev->param.chunks_per_block;
4532 + iterations = n_blocks;
4533 + } else {
4534 + int max_threshold;
4536 + if (background)
4537 + max_threshold = dev->param.chunks_per_block / 2;
4538 + else
4539 + max_threshold = dev->param.chunks_per_block / 8;
4541 + if (max_threshold < YAFFS_GC_PASSIVE_THRESHOLD)
4542 + max_threshold = YAFFS_GC_PASSIVE_THRESHOLD;
4544 + threshold = background ? (dev->gc_not_done + 2) * 2 : 0;
4545 + if (threshold < YAFFS_GC_PASSIVE_THRESHOLD)
4546 + threshold = YAFFS_GC_PASSIVE_THRESHOLD;
4547 + if (threshold > max_threshold)
4548 + threshold = max_threshold;
4550 + iterations = n_blocks / 16 + 1;
4551 + if (iterations > 100)
4552 + iterations = 100;
4555 + for (i = 0;
4556 + i < iterations &&
4557 + (dev->gc_dirtiest < 1 ||
4558 + dev->gc_pages_in_use > YAFFS_GC_GOOD_ENOUGH);
4559 + i++) {
4560 + dev->gc_block_finder++;
4561 + if (dev->gc_block_finder < dev->internal_start_block ||
4562 + dev->gc_block_finder > dev->internal_end_block)
4563 + dev->gc_block_finder =
4564 + dev->internal_start_block;
4566 + bi = yaffs_get_block_info(dev, dev->gc_block_finder);
4568 + pages_used = bi->pages_in_use - bi->soft_del_pages;
4570 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL &&
4571 + pages_used < dev->param.chunks_per_block &&
4572 + (dev->gc_dirtiest < 1 ||
4573 + pages_used < dev->gc_pages_in_use) &&
4574 + yaffs_block_ok_for_gc(dev, bi)) {
4575 + dev->gc_dirtiest = dev->gc_block_finder;
4576 + dev->gc_pages_in_use = pages_used;
4580 + if (dev->gc_dirtiest > 0 && dev->gc_pages_in_use <= threshold)
4581 + selected = dev->gc_dirtiest;
4584 + /*
4585 + * If nothing has been selected for a while, try the oldest dirty
4586 + * because that's gumming up the works.
4587 + */
4589 + if (!selected && dev->param.is_yaffs2 &&
4590 + dev->gc_not_done >= (background ? 10 : 20)) {
4591 + yaffs2_find_oldest_dirty_seq(dev);
4592 + if (dev->oldest_dirty_block > 0) {
4593 + selected = dev->oldest_dirty_block;
4594 + dev->gc_dirtiest = selected;
4595 + dev->oldest_dirty_gc_count++;
4596 + bi = yaffs_get_block_info(dev, selected);
4597 + dev->gc_pages_in_use =
4598 + bi->pages_in_use - bi->soft_del_pages;
4599 + } else {
4600 + dev->gc_not_done = 0;
4604 + if (selected) {
4605 + yaffs_trace(YAFFS_TRACE_GC,
4606 + "GC Selected block %d with %d free, prioritised:%d",
4607 + selected,
4608 + dev->param.chunks_per_block - dev->gc_pages_in_use,
4609 + prioritised);
4611 + dev->n_gc_blocks++;
4612 + if (background)
4613 + dev->bg_gcs++;
4615 + dev->gc_dirtiest = 0;
4616 + dev->gc_pages_in_use = 0;
4617 + dev->gc_not_done = 0;
4618 + if (dev->refresh_skip > 0)
4619 + dev->refresh_skip--;
4620 + } else {
4621 + dev->gc_not_done++;
4622 + yaffs_trace(YAFFS_TRACE_GC,
4623 + "GC none: finder %d skip %d threshold %d dirtiest %d using %d oldest %d%s",
4624 + dev->gc_block_finder, dev->gc_not_done, threshold,
4625 + dev->gc_dirtiest, dev->gc_pages_in_use,
4626 + dev->oldest_dirty_block, background ? " bg" : "");
4629 + return selected;
4632 +/* New garbage collector
4633 + * If we're very low on erased blocks then we do aggressive garbage collection
4634 + * otherwise we do "leasurely" garbage collection.
4635 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
4636 + * Passive gc only inspects smaller areas and only accepts more dirty blocks.
4638 + * The idea is to help clear out space in a more spread-out manner.
4639 + * Dunno if it really does anything useful.
4640 + */
4641 +static int yaffs_check_gc(struct yaffs_dev *dev, int background)
4643 + int aggressive = 0;
4644 + int gc_ok = YAFFS_OK;
4645 + int max_tries = 0;
4646 + int min_erased;
4647 + int erased_chunks;
4648 + int checkpt_block_adjust;
4650 + if (dev->param.gc_control_fn &&
4651 + (dev->param.gc_control_fn(dev) & 1) == 0)
4652 + return YAFFS_OK;
4654 + if (dev->gc_disable)
4655 + /* Bail out so we don't get recursive gc */
4656 + return YAFFS_OK;
4658 + /* This loop should pass the first time.
4659 + * Only loops here if the collection does not increase space.
4660 + */
4662 + do {
4663 + max_tries++;
4665 + checkpt_block_adjust = yaffs_calc_checkpt_blocks_required(dev);
4667 + min_erased =
4668 + dev->param.n_reserved_blocks + checkpt_block_adjust + 1;
4669 + erased_chunks =
4670 + dev->n_erased_blocks * dev->param.chunks_per_block;
4672 + /* If we need a block soon then do aggressive gc. */
4673 + if (dev->n_erased_blocks < min_erased)
4674 + aggressive = 1;
4675 + else {
4676 + if (!background
4677 + && erased_chunks > (dev->n_free_chunks / 4))
4678 + break;
4680 + if (dev->gc_skip > 20)
4681 + dev->gc_skip = 20;
4682 + if (erased_chunks < dev->n_free_chunks / 2 ||
4683 + dev->gc_skip < 1 || background)
4684 + aggressive = 0;
4685 + else {
4686 + dev->gc_skip--;
4687 + break;
4691 + dev->gc_skip = 5;
4693 + /* If we don't already have a block being gc'd then see if we
4694 + * should start another */
4696 + if (dev->gc_block < 1 && !aggressive) {
4697 + dev->gc_block = yaffs2_find_refresh_block(dev);
4698 + dev->gc_chunk = 0;
4699 + dev->n_clean_ups = 0;
4701 + if (dev->gc_block < 1) {
4702 + dev->gc_block =
4703 + yaffs_find_gc_block(dev, aggressive, background);
4704 + dev->gc_chunk = 0;
4705 + dev->n_clean_ups = 0;
4708 + if (dev->gc_block > 0) {
4709 + dev->all_gcs++;
4710 + if (!aggressive)
4711 + dev->passive_gc_count++;
4713 + yaffs_trace(YAFFS_TRACE_GC,
4714 + "yaffs: GC n_erased_blocks %d aggressive %d",
4715 + dev->n_erased_blocks, aggressive);
4717 + gc_ok = yaffs_gc_block(dev, dev->gc_block, aggressive);
4720 + if (dev->n_erased_blocks < (dev->param.n_reserved_blocks) &&
4721 + dev->gc_block > 0) {
4722 + yaffs_trace(YAFFS_TRACE_GC,
4723 + "yaffs: GC !!!no reclaim!!! n_erased_blocks %d after try %d block %d",
4724 + dev->n_erased_blocks, max_tries,
4725 + dev->gc_block);
4727 + } while ((dev->n_erased_blocks < dev->param.n_reserved_blocks) &&
4728 + (dev->gc_block > 0) && (max_tries < 2));
4730 + return aggressive ? gc_ok : YAFFS_OK;
4734 + * yaffs_bg_gc()
4735 + * Garbage collects. Intended to be called from a background thread.
4736 + * Returns non-zero if at least half the free chunks are erased.
4737 + */
4738 +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency)
4740 + int erased_chunks = dev->n_erased_blocks * dev->param.chunks_per_block;
4742 + yaffs_trace(YAFFS_TRACE_BACKGROUND, "Background gc %u", urgency);
4744 + yaffs_check_gc(dev, 1);
4745 + return erased_chunks > dev->n_free_chunks / 2;
4748 +/*-------------------- Data file manipulation -----------------*/
4750 +static int yaffs_rd_data_obj(struct yaffs_obj *in, int inode_chunk, u8 * buffer)
4752 + int nand_chunk = yaffs_find_chunk_in_file(in, inode_chunk, NULL);
4754 + if (nand_chunk >= 0)
4755 + return yaffs_rd_chunk_tags_nand(in->my_dev, nand_chunk,
4756 + buffer, NULL);
4757 + else {
4758 + yaffs_trace(YAFFS_TRACE_NANDACCESS,
4759 + "Chunk %d not found zero instead",
4760 + nand_chunk);
4761 + /* get sane (zero) data if you read a hole */
4762 + memset(buffer, 0, in->my_dev->data_bytes_per_chunk);
4763 + return 0;
4768 +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
4769 + int lyn)
4771 + int block;
4772 + int page;
4773 + struct yaffs_ext_tags tags;
4774 + struct yaffs_block_info *bi;
4776 + if (chunk_id <= 0)
4777 + return;
4779 + dev->n_deletions++;
4780 + block = chunk_id / dev->param.chunks_per_block;
4781 + page = chunk_id % dev->param.chunks_per_block;
4783 + if (!yaffs_check_chunk_bit(dev, block, page))
4784 + yaffs_trace(YAFFS_TRACE_VERIFY,
4785 + "Deleting invalid chunk %d", chunk_id);
4787 + bi = yaffs_get_block_info(dev, block);
4789 + yaffs2_update_oldest_dirty_seq(dev, block, bi);
4791 + yaffs_trace(YAFFS_TRACE_DELETION,
4792 + "line %d delete of chunk %d",
4793 + lyn, chunk_id);
4795 + if (!dev->param.is_yaffs2 && mark_flash &&
4796 + bi->block_state != YAFFS_BLOCK_STATE_COLLECTING) {
4798 + memset(&tags, 0, sizeof(tags));
4799 + tags.is_deleted = 1;
4800 + yaffs_wr_chunk_tags_nand(dev, chunk_id, NULL, &tags);
4801 + yaffs_handle_chunk_update(dev, chunk_id, &tags);
4802 + } else {
4803 + dev->n_unmarked_deletions++;
4806 + /* Pull out of the management area.
4807 + * If the whole block became dirty, this will kick off an erasure.
4808 + */
4809 + if (bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING ||
4810 + bi->block_state == YAFFS_BLOCK_STATE_FULL ||
4811 + bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
4812 + bi->block_state == YAFFS_BLOCK_STATE_COLLECTING) {
4813 + dev->n_free_chunks++;
4814 + yaffs_clear_chunk_bit(dev, block, page);
4815 + bi->pages_in_use--;
4817 + if (bi->pages_in_use == 0 &&
4818 + !bi->has_shrink_hdr &&
4819 + bi->block_state != YAFFS_BLOCK_STATE_ALLOCATING &&
4820 + bi->block_state != YAFFS_BLOCK_STATE_NEEDS_SCAN) {
4821 + yaffs_block_became_dirty(dev, block);
4826 +static int yaffs_wr_data_obj(struct yaffs_obj *in, int inode_chunk,
4827 + const u8 *buffer, int n_bytes, int use_reserve)
4829 + /* Find old chunk Need to do this to get serial number
4830 + * Write new one and patch into tree.
4831 + * Invalidate old tags.
4832 + */
4834 + int prev_chunk_id;
4835 + struct yaffs_ext_tags prev_tags;
4836 + int new_chunk_id;
4837 + struct yaffs_ext_tags new_tags;
4838 + struct yaffs_dev *dev = in->my_dev;
4840 + yaffs_check_gc(dev, 0);
4842 + /* Get the previous chunk at this location in the file if it exists.
4843 + * If it does not exist then put a zero into the tree. This creates
4844 + * the tnode now, rather than later when it is harder to clean up.
4845 + */
4846 + prev_chunk_id = yaffs_find_chunk_in_file(in, inode_chunk, &prev_tags);
4847 + if (prev_chunk_id < 1 &&
4848 + !yaffs_put_chunk_in_file(in, inode_chunk, 0, 0))
4849 + return 0;
4851 + /* Set up new tags */
4852 + memset(&new_tags, 0, sizeof(new_tags));
4854 + new_tags.chunk_id = inode_chunk;
4855 + new_tags.obj_id = in->obj_id;
4856 + new_tags.serial_number =
4857 + (prev_chunk_id > 0) ? prev_tags.serial_number + 1 : 1;
4858 + new_tags.n_bytes = n_bytes;
4860 + if (n_bytes < 1 || n_bytes > dev->param.total_bytes_per_chunk) {
4861 + yaffs_trace(YAFFS_TRACE_ERROR,
4862 + "Writing %d bytes to chunk!!!!!!!!!",
4863 + n_bytes);
4864 + BUG();
4867 + new_chunk_id =
4868 + yaffs_write_new_chunk(dev, buffer, &new_tags, use_reserve);
4870 + if (new_chunk_id > 0) {
4871 + yaffs_put_chunk_in_file(in, inode_chunk, new_chunk_id, 0);
4873 + if (prev_chunk_id > 0)
4874 + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
4876 + yaffs_verify_file_sane(in);
4878 + return new_chunk_id;
4884 +static int yaffs_do_xattrib_mod(struct yaffs_obj *obj, int set,
4885 + const YCHAR *name, const void *value, int size,
4886 + int flags)
4888 + struct yaffs_xattr_mod xmod;
4889 + int result;
4891 + xmod.set = set;
4892 + xmod.name = name;
4893 + xmod.data = value;
4894 + xmod.size = size;
4895 + xmod.flags = flags;
4896 + xmod.result = -ENOSPC;
4898 + result = yaffs_update_oh(obj, NULL, 0, 0, 0, &xmod);
4900 + if (result > 0)
4901 + return xmod.result;
4902 + else
4903 + return -ENOSPC;
4906 +static int yaffs_apply_xattrib_mod(struct yaffs_obj *obj, char *buffer,
4907 + struct yaffs_xattr_mod *xmod)
4909 + int retval = 0;
4910 + int x_offs = sizeof(struct yaffs_obj_hdr);
4911 + struct yaffs_dev *dev = obj->my_dev;
4912 + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4913 + char *x_buffer = buffer + x_offs;
4915 + if (xmod->set)
4916 + retval =
4917 + nval_set(x_buffer, x_size, xmod->name, xmod->data,
4918 + xmod->size, xmod->flags);
4919 + else
4920 + retval = nval_del(x_buffer, x_size, xmod->name);
4922 + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4923 + obj->xattr_known = 1;
4924 + xmod->result = retval;
4926 + return retval;
4929 +static int yaffs_do_xattrib_fetch(struct yaffs_obj *obj, const YCHAR *name,
4930 + void *value, int size)
4932 + char *buffer = NULL;
4933 + int result;
4934 + struct yaffs_ext_tags tags;
4935 + struct yaffs_dev *dev = obj->my_dev;
4936 + int x_offs = sizeof(struct yaffs_obj_hdr);
4937 + int x_size = dev->data_bytes_per_chunk - sizeof(struct yaffs_obj_hdr);
4938 + char *x_buffer;
4939 + int retval = 0;
4941 + if (obj->hdr_chunk < 1)
4942 + return -ENODATA;
4944 + /* If we know that the object has no xattribs then don't do all the
4945 + * reading and parsing.
4946 + */
4947 + if (obj->xattr_known && !obj->has_xattr) {
4948 + if (name)
4949 + return -ENODATA;
4950 + else
4951 + return 0;
4954 + buffer = (char *)yaffs_get_temp_buffer(dev);
4955 + if (!buffer)
4956 + return -ENOMEM;
4958 + result =
4959 + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, (u8 *) buffer, &tags);
4961 + if (result != YAFFS_OK)
4962 + retval = -ENOENT;
4963 + else {
4964 + x_buffer = buffer + x_offs;
4966 + if (!obj->xattr_known) {
4967 + obj->has_xattr = nval_hasvalues(x_buffer, x_size);
4968 + obj->xattr_known = 1;
4971 + if (name)
4972 + retval = nval_get(x_buffer, x_size, name, value, size);
4973 + else
4974 + retval = nval_list(x_buffer, x_size, value, size);
4976 + yaffs_release_temp_buffer(dev, (u8 *) buffer);
4977 + return retval;
4980 +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR * name,
4981 + const void *value, int size, int flags)
4983 + return yaffs_do_xattrib_mod(obj, 1, name, value, size, flags);
4986 +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR * name)
4988 + return yaffs_do_xattrib_mod(obj, 0, name, NULL, 0, 0);
4991 +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR * name, void *value,
4992 + int size)
4994 + return yaffs_do_xattrib_fetch(obj, name, value, size);
4997 +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size)
4999 + return yaffs_do_xattrib_fetch(obj, NULL, buffer, size);
5002 +static void yaffs_check_obj_details_loaded(struct yaffs_obj *in)
5004 + u8 *buf;
5005 + struct yaffs_obj_hdr *oh;
5006 + struct yaffs_dev *dev;
5007 + struct yaffs_ext_tags tags;
5008 + int result;
5009 + int alloc_failed = 0;
5011 + if (!in || !in->lazy_loaded || in->hdr_chunk < 1)
5012 + return;
5014 + dev = in->my_dev;
5015 + in->lazy_loaded = 0;
5016 + buf = yaffs_get_temp_buffer(dev);
5018 + result = yaffs_rd_chunk_tags_nand(dev, in->hdr_chunk, buf, &tags);
5019 + oh = (struct yaffs_obj_hdr *)buf;
5021 + in->yst_mode = oh->yst_mode;
5022 + yaffs_load_attribs(in, oh);
5023 + yaffs_set_obj_name_from_oh(in, oh);
5025 + if (in->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
5026 + in->variant.symlink_variant.alias =
5027 + yaffs_clone_str(oh->alias);
5028 + if (!in->variant.symlink_variant.alias)
5029 + alloc_failed = 1; /* Not returned */
5031 + yaffs_release_temp_buffer(dev, buf);
5034 +static void yaffs_load_name_from_oh(struct yaffs_dev *dev, YCHAR *name,
5035 + const YCHAR *oh_name, int buff_size)
5037 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
5038 + if (dev->param.auto_unicode) {
5039 + if (*oh_name) {
5040 + /* It is an ASCII name, do an ASCII to
5041 + * unicode conversion */
5042 + const char *ascii_oh_name = (const char *)oh_name;
5043 + int n = buff_size - 1;
5044 + while (n > 0 && *ascii_oh_name) {
5045 + *name = *ascii_oh_name;
5046 + name++;
5047 + ascii_oh_name++;
5048 + n--;
5050 + } else {
5051 + strncpy(name, oh_name + 1, buff_size - 1);
5053 + } else {
5054 +#else
5055 + (void) dev;
5057 +#endif
5058 + strncpy(name, oh_name, buff_size - 1);
5062 +static void yaffs_load_oh_from_name(struct yaffs_dev *dev, YCHAR *oh_name,
5063 + const YCHAR *name)
5065 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
5067 + int is_ascii;
5068 + YCHAR *w;
5070 + if (dev->param.auto_unicode) {
5072 + is_ascii = 1;
5073 + w = name;
5075 + /* Figure out if the name will fit in ascii character set */
5076 + while (is_ascii && *w) {
5077 + if ((*w) & 0xff00)
5078 + is_ascii = 0;
5079 + w++;
5082 + if (is_ascii) {
5083 + /* It is an ASCII name, so convert unicode to ascii */
5084 + char *ascii_oh_name = (char *)oh_name;
5085 + int n = YAFFS_MAX_NAME_LENGTH - 1;
5086 + while (n > 0 && *name) {
5087 + *ascii_oh_name = *name;
5088 + name++;
5089 + ascii_oh_name++;
5090 + n--;
5092 + } else {
5093 + /* Unicode name, so save starting at the second YCHAR */
5094 + *oh_name = 0;
5095 + strncpy(oh_name + 1, name, YAFFS_MAX_NAME_LENGTH - 2);
5097 + } else {
5098 +#else
5099 + dev = dev;
5101 +#endif
5102 + strncpy(oh_name, name, YAFFS_MAX_NAME_LENGTH - 1);
5106 +/* UpdateObjectHeader updates the header on NAND for an object.
5107 + * If name is not NULL, then that new name is used.
5108 + */
5109 +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name, int force,
5110 + int is_shrink, int shadows, struct yaffs_xattr_mod *xmod)
5113 + struct yaffs_block_info *bi;
5114 + struct yaffs_dev *dev = in->my_dev;
5115 + int prev_chunk_id;
5116 + int ret_val = 0;
5117 + int result = 0;
5118 + int new_chunk_id;
5119 + struct yaffs_ext_tags new_tags;
5120 + struct yaffs_ext_tags old_tags;
5121 + const YCHAR *alias = NULL;
5122 + u8 *buffer = NULL;
5123 + YCHAR old_name[YAFFS_MAX_NAME_LENGTH + 1];
5124 + struct yaffs_obj_hdr *oh = NULL;
5125 + loff_t file_size = 0;
5127 + strcpy(old_name, _Y("silly old name"));
5129 + if (in->fake && in != dev->root_dir && !force && !xmod)
5130 + return ret_val;
5132 + yaffs_check_gc(dev, 0);
5133 + yaffs_check_obj_details_loaded(in);
5135 + buffer = yaffs_get_temp_buffer(in->my_dev);
5136 + oh = (struct yaffs_obj_hdr *)buffer;
5138 + prev_chunk_id = in->hdr_chunk;
5140 + if (prev_chunk_id > 0) {
5141 + result = yaffs_rd_chunk_tags_nand(dev, prev_chunk_id,
5142 + buffer, &old_tags);
5144 + yaffs_verify_oh(in, oh, &old_tags, 0);
5145 + memcpy(old_name, oh->name, sizeof(oh->name));
5146 + memset(buffer, 0xff, sizeof(struct yaffs_obj_hdr));
5147 + } else {
5148 + memset(buffer, 0xff, dev->data_bytes_per_chunk);
5151 + oh->type = in->variant_type;
5152 + oh->yst_mode = in->yst_mode;
5153 + oh->shadows_obj = oh->inband_shadowed_obj_id = shadows;
5155 + yaffs_load_attribs_oh(oh, in);
5157 + if (in->parent)
5158 + oh->parent_obj_id = in->parent->obj_id;
5159 + else
5160 + oh->parent_obj_id = 0;
5162 + if (name && *name) {
5163 + memset(oh->name, 0, sizeof(oh->name));
5164 + yaffs_load_oh_from_name(dev, oh->name, name);
5165 + } else if (prev_chunk_id > 0) {
5166 + memcpy(oh->name, old_name, sizeof(oh->name));
5167 + } else {
5168 + memset(oh->name, 0, sizeof(oh->name));
5171 + oh->is_shrink = is_shrink;
5173 + switch (in->variant_type) {
5174 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5175 + /* Should not happen */
5176 + break;
5177 + case YAFFS_OBJECT_TYPE_FILE:
5178 + if (oh->parent_obj_id != YAFFS_OBJECTID_DELETED &&
5179 + oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED)
5180 + file_size = in->variant.file_variant.file_size;
5181 + yaffs_oh_size_load(oh, file_size);
5182 + break;
5183 + case YAFFS_OBJECT_TYPE_HARDLINK:
5184 + oh->equiv_id = in->variant.hardlink_variant.equiv_id;
5185 + break;
5186 + case YAFFS_OBJECT_TYPE_SPECIAL:
5187 + /* Do nothing */
5188 + break;
5189 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5190 + /* Do nothing */
5191 + break;
5192 + case YAFFS_OBJECT_TYPE_SYMLINK:
5193 + alias = in->variant.symlink_variant.alias;
5194 + if (!alias)
5195 + alias = _Y("no alias");
5196 + strncpy(oh->alias, alias, YAFFS_MAX_ALIAS_LENGTH);
5197 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
5198 + break;
5201 + /* process any xattrib modifications */
5202 + if (xmod)
5203 + yaffs_apply_xattrib_mod(in, (char *)buffer, xmod);
5205 + /* Tags */
5206 + memset(&new_tags, 0, sizeof(new_tags));
5207 + in->serial++;
5208 + new_tags.chunk_id = 0;
5209 + new_tags.obj_id = in->obj_id;
5210 + new_tags.serial_number = in->serial;
5212 + /* Add extra info for file header */
5213 + new_tags.extra_available = 1;
5214 + new_tags.extra_parent_id = oh->parent_obj_id;
5215 + new_tags.extra_file_size = file_size;
5216 + new_tags.extra_is_shrink = oh->is_shrink;
5217 + new_tags.extra_equiv_id = oh->equiv_id;
5218 + new_tags.extra_shadows = (oh->shadows_obj > 0) ? 1 : 0;
5219 + new_tags.extra_obj_type = in->variant_type;
5220 + yaffs_verify_oh(in, oh, &new_tags, 1);
5222 + /* Create new chunk in NAND */
5223 + new_chunk_id =
5224 + yaffs_write_new_chunk(dev, buffer, &new_tags,
5225 + (prev_chunk_id > 0) ? 1 : 0);
5227 + if (buffer)
5228 + yaffs_release_temp_buffer(dev, buffer);
5230 + if (new_chunk_id < 0)
5231 + return new_chunk_id;
5233 + in->hdr_chunk = new_chunk_id;
5235 + if (prev_chunk_id > 0)
5236 + yaffs_chunk_del(dev, prev_chunk_id, 1, __LINE__);
5238 + if (!yaffs_obj_cache_dirty(in))
5239 + in->dirty = 0;
5241 + /* If this was a shrink, then mark the block
5242 + * that the chunk lives on */
5243 + if (is_shrink) {
5244 + bi = yaffs_get_block_info(in->my_dev,
5245 + new_chunk_id /
5246 + in->my_dev->param.chunks_per_block);
5247 + bi->has_shrink_hdr = 1;
5251 + return new_chunk_id;
5254 +/*--------------------- File read/write ------------------------
5255 + * Read and write have very similar structures.
5256 + * In general the read/write has three parts to it
5257 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
5258 + * Some complete chunks
5259 + * An incomplete chunk to end off with
5261 + * Curve-balls: the first chunk might also be the last chunk.
5262 + */
5264 +int yaffs_file_rd(struct yaffs_obj *in, u8 * buffer, loff_t offset, int n_bytes)
5266 + int chunk;
5267 + u32 start;
5268 + int n_copy;
5269 + int n = n_bytes;
5270 + int n_done = 0;
5271 + struct yaffs_cache *cache;
5272 + struct yaffs_dev *dev;
5274 + dev = in->my_dev;
5276 + while (n > 0) {
5277 + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
5278 + chunk++;
5280 + /* OK now check for the curveball where the start and end are in
5281 + * the same chunk.
5282 + */
5283 + if ((start + n) < dev->data_bytes_per_chunk)
5284 + n_copy = n;
5285 + else
5286 + n_copy = dev->data_bytes_per_chunk - start;
5288 + cache = yaffs_find_chunk_cache(in, chunk);
5290 + /* If the chunk is already in the cache or it is less than
5291 + * a whole chunk or we're using inband tags then use the cache
5292 + * (if there is caching) else bypass the cache.
5293 + */
5294 + if (cache || n_copy != dev->data_bytes_per_chunk ||
5295 + dev->param.inband_tags) {
5296 + if (dev->param.n_caches > 0) {
5298 + /* If we can't find the data in the cache,
5299 + * then load it up. */
5301 + if (!cache) {
5302 + cache =
5303 + yaffs_grab_chunk_cache(in->my_dev);
5304 + cache->object = in;
5305 + cache->chunk_id = chunk;
5306 + cache->dirty = 0;
5307 + cache->locked = 0;
5308 + yaffs_rd_data_obj(in, chunk,
5309 + cache->data);
5310 + cache->n_bytes = 0;
5313 + yaffs_use_cache(dev, cache, 0);
5315 + cache->locked = 1;
5317 + memcpy(buffer, &cache->data[start], n_copy);
5319 + cache->locked = 0;
5320 + } else {
5321 + /* Read into the local buffer then copy.. */
5323 + u8 *local_buffer =
5324 + yaffs_get_temp_buffer(dev);
5325 + yaffs_rd_data_obj(in, chunk, local_buffer);
5327 + memcpy(buffer, &local_buffer[start], n_copy);
5329 + yaffs_release_temp_buffer(dev, local_buffer);
5331 + } else {
5332 + /* A full chunk. Read directly into the buffer. */
5333 + yaffs_rd_data_obj(in, chunk, buffer);
5335 + n -= n_copy;
5336 + offset += n_copy;
5337 + buffer += n_copy;
5338 + n_done += n_copy;
5340 + return n_done;
5343 +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
5344 + int n_bytes, int write_through)
5347 + int chunk;
5348 + u32 start;
5349 + int n_copy;
5350 + int n = n_bytes;
5351 + int n_done = 0;
5352 + int n_writeback;
5353 + loff_t start_write = offset;
5354 + int chunk_written = 0;
5355 + u32 n_bytes_read;
5356 + loff_t chunk_start;
5357 + struct yaffs_dev *dev;
5359 + dev = in->my_dev;
5361 + while (n > 0 && chunk_written >= 0) {
5362 + yaffs_addr_to_chunk(dev, offset, &chunk, &start);
5364 + if (((loff_t)chunk) *
5365 + dev->data_bytes_per_chunk + start != offset ||
5366 + start >= dev->data_bytes_per_chunk) {
5367 + yaffs_trace(YAFFS_TRACE_ERROR,
5368 + "AddrToChunk of offset %lld gives chunk %d start %d",
5369 + offset, chunk, start);
5371 + chunk++; /* File pos to chunk in file offset */
5373 + /* OK now check for the curveball where the start and end are in
5374 + * the same chunk.
5375 + */
5377 + if ((start + n) < dev->data_bytes_per_chunk) {
5378 + n_copy = n;
5380 + /* Now calculate how many bytes to write back....
5381 + * If we're overwriting and not writing to then end of
5382 + * file then we need to write back as much as was there
5383 + * before.
5384 + */
5386 + chunk_start = (((loff_t)(chunk - 1)) *
5387 + dev->data_bytes_per_chunk);
5389 + if (chunk_start > in->variant.file_variant.file_size)
5390 + n_bytes_read = 0; /* Past end of file */
5391 + else
5392 + n_bytes_read =
5393 + in->variant.file_variant.file_size -
5394 + chunk_start;
5396 + if (n_bytes_read > dev->data_bytes_per_chunk)
5397 + n_bytes_read = dev->data_bytes_per_chunk;
5399 + n_writeback =
5400 + (n_bytes_read >
5401 + (start + n)) ? n_bytes_read : (start + n);
5403 + if (n_writeback < 0 ||
5404 + n_writeback > dev->data_bytes_per_chunk)
5405 + BUG();
5407 + } else {
5408 + n_copy = dev->data_bytes_per_chunk - start;
5409 + n_writeback = dev->data_bytes_per_chunk;
5412 + if (n_copy != dev->data_bytes_per_chunk ||
5413 + !dev->param.cache_bypass_aligned ||
5414 + dev->param.inband_tags) {
5415 + /* An incomplete start or end chunk (or maybe both
5416 + * start and end chunk), or we're using inband tags,
5417 + * or we're forcing writes through the cache,
5418 + * so we want to use the cache buffers.
5419 + */
5420 + if (dev->param.n_caches > 0) {
5421 + struct yaffs_cache *cache;
5423 + /* If we can't find the data in the cache, then
5424 + * load the cache */
5425 + cache = yaffs_find_chunk_cache(in, chunk);
5427 + if (!cache &&
5428 + yaffs_check_alloc_available(dev, 1)) {
5429 + cache = yaffs_grab_chunk_cache(dev);
5430 + cache->object = in;
5431 + cache->chunk_id = chunk;
5432 + cache->dirty = 0;
5433 + cache->locked = 0;
5434 + yaffs_rd_data_obj(in, chunk,
5435 + cache->data);
5436 + } else if (cache &&
5437 + !cache->dirty &&
5438 + !yaffs_check_alloc_available(dev,
5439 + 1)) {
5440 + /* Drop the cache if it was a read cache
5441 + * item and no space check has been made
5442 + * for it.
5443 + */
5444 + cache = NULL;
5447 + if (cache) {
5448 + yaffs_use_cache(dev, cache, 1);
5449 + cache->locked = 1;
5451 + memcpy(&cache->data[start], buffer,
5452 + n_copy);
5454 + cache->locked = 0;
5455 + cache->n_bytes = n_writeback;
5457 + if (write_through) {
5458 + chunk_written =
5459 + yaffs_wr_data_obj
5460 + (cache->object,
5461 + cache->chunk_id,
5462 + cache->data,
5463 + cache->n_bytes, 1);
5464 + cache->dirty = 0;
5466 + } else {
5467 + chunk_written = -1; /* fail write */
5469 + } else {
5470 + /* An incomplete start or end chunk (or maybe
5471 + * both start and end chunk). Read into the
5472 + * local buffer then copy over and write back.
5473 + */
5475 + u8 *local_buffer = yaffs_get_temp_buffer(dev);
5477 + yaffs_rd_data_obj(in, chunk, local_buffer);
5478 + memcpy(&local_buffer[start], buffer, n_copy);
5480 + chunk_written =
5481 + yaffs_wr_data_obj(in, chunk,
5482 + local_buffer,
5483 + n_writeback, 0);
5485 + yaffs_release_temp_buffer(dev, local_buffer);
5487 + } else {
5488 + /* A full chunk. Write directly from the buffer. */
5490 + chunk_written =
5491 + yaffs_wr_data_obj(in, chunk, buffer,
5492 + dev->data_bytes_per_chunk, 0);
5494 + /* Since we've overwritten the cached data,
5495 + * we better invalidate it. */
5496 + yaffs_invalidate_chunk_cache(in, chunk);
5499 + if (chunk_written >= 0) {
5500 + n -= n_copy;
5501 + offset += n_copy;
5502 + buffer += n_copy;
5503 + n_done += n_copy;
5507 + /* Update file object */
5509 + if ((start_write + n_done) > in->variant.file_variant.file_size)
5510 + in->variant.file_variant.file_size = (start_write + n_done);
5512 + in->dirty = 1;
5513 + return n_done;
5516 +int yaffs_wr_file(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
5517 + int n_bytes, int write_through)
5519 + yaffs2_handle_hole(in, offset);
5520 + return yaffs_do_file_wr(in, buffer, offset, n_bytes, write_through);
5523 +/* ---------------------- File resizing stuff ------------------ */
5525 +static void yaffs_prune_chunks(struct yaffs_obj *in, loff_t new_size)
5528 + struct yaffs_dev *dev = in->my_dev;
5529 + loff_t old_size = in->variant.file_variant.file_size;
5530 + int i;
5531 + int chunk_id;
5532 + u32 dummy;
5533 + int last_del;
5534 + int start_del;
5536 + if (old_size > 0)
5537 + yaffs_addr_to_chunk(dev, old_size - 1, &last_del, &dummy);
5538 + else
5539 + last_del = 0;
5541 + yaffs_addr_to_chunk(dev, new_size + dev->data_bytes_per_chunk - 1,
5542 + &start_del, &dummy);
5543 + last_del++;
5544 + start_del++;
5546 + /* Delete backwards so that we don't end up with holes if
5547 + * power is lost part-way through the operation.
5548 + */
5549 + for (i = last_del; i >= start_del; i--) {
5550 + /* NB this could be optimised somewhat,
5551 + * eg. could retrieve the tags and write them without
5552 + * using yaffs_chunk_del
5553 + */
5555 + chunk_id = yaffs_find_del_file_chunk(in, i, NULL);
5557 + if (chunk_id < 1)
5558 + continue;
5560 + if (chunk_id <
5561 + (dev->internal_start_block * dev->param.chunks_per_block) ||
5562 + chunk_id >=
5563 + ((dev->internal_end_block + 1) *
5564 + dev->param.chunks_per_block)) {
5565 + yaffs_trace(YAFFS_TRACE_ALWAYS,
5566 + "Found daft chunk_id %d for %d",
5567 + chunk_id, i);
5568 + } else {
5569 + in->n_data_chunks--;
5570 + yaffs_chunk_del(dev, chunk_id, 1, __LINE__);
5575 +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size)
5577 + int new_full;
5578 + u32 new_partial;
5579 + struct yaffs_dev *dev = obj->my_dev;
5581 + yaffs_addr_to_chunk(dev, new_size, &new_full, &new_partial);
5583 + yaffs_prune_chunks(obj, new_size);
5585 + if (new_partial != 0) {
5586 + int last_chunk = 1 + new_full;
5587 + u8 *local_buffer = yaffs_get_temp_buffer(dev);
5589 + /* Rewrite the last chunk with its new size and zero pad */
5590 + yaffs_rd_data_obj(obj, last_chunk, local_buffer);
5591 + memset(local_buffer + new_partial, 0,
5592 + dev->data_bytes_per_chunk - new_partial);
5594 + yaffs_wr_data_obj(obj, last_chunk, local_buffer,
5595 + new_partial, 1);
5597 + yaffs_release_temp_buffer(dev, local_buffer);
5600 + obj->variant.file_variant.file_size = new_size;
5602 + yaffs_prune_tree(dev, &obj->variant.file_variant);
5605 +int yaffs_resize_file(struct yaffs_obj *in, loff_t new_size)
5607 + struct yaffs_dev *dev = in->my_dev;
5608 + loff_t old_size = in->variant.file_variant.file_size;
5610 + yaffs_flush_file_cache(in);
5611 + yaffs_invalidate_whole_cache(in);
5613 + yaffs_check_gc(dev, 0);
5615 + if (in->variant_type != YAFFS_OBJECT_TYPE_FILE)
5616 + return YAFFS_FAIL;
5618 + if (new_size == old_size)
5619 + return YAFFS_OK;
5621 + if (new_size > old_size) {
5622 + yaffs2_handle_hole(in, new_size);
5623 + in->variant.file_variant.file_size = new_size;
5624 + } else {
5625 + /* new_size < old_size */
5626 + yaffs_resize_file_down(in, new_size);
5629 + /* Write a new object header to reflect the resize.
5630 + * show we've shrunk the file, if need be
5631 + * Do this only if the file is not in the deleted directories
5632 + * and is not shadowed.
5633 + */
5634 + if (in->parent &&
5635 + !in->is_shadowed &&
5636 + in->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
5637 + in->parent->obj_id != YAFFS_OBJECTID_DELETED)
5638 + yaffs_update_oh(in, NULL, 0, 0, 0, NULL);
5640 + return YAFFS_OK;
5643 +int yaffs_flush_file(struct yaffs_obj *in, int update_time, int data_sync)
5645 + if (!in->dirty)
5646 + return YAFFS_OK;
5648 + yaffs_flush_file_cache(in);
5650 + if (data_sync)
5651 + return YAFFS_OK;
5653 + if (update_time)
5654 + yaffs_load_current_time(in, 0, 0);
5656 + return (yaffs_update_oh(in, NULL, 0, 0, 0, NULL) >= 0) ?
5657 + YAFFS_OK : YAFFS_FAIL;
5661 +/* yaffs_del_file deletes the whole file data
5662 + * and the inode associated with the file.
5663 + * It does not delete the links associated with the file.
5664 + */
5665 +static int yaffs_unlink_file_if_needed(struct yaffs_obj *in)
5667 + int ret_val;
5668 + int del_now = 0;
5669 + struct yaffs_dev *dev = in->my_dev;
5671 + if (!in->my_inode)
5672 + del_now = 1;
5674 + if (del_now) {
5675 + ret_val =
5676 + yaffs_change_obj_name(in, in->my_dev->del_dir,
5677 + _Y("deleted"), 0, 0);
5678 + yaffs_trace(YAFFS_TRACE_TRACING,
5679 + "yaffs: immediate deletion of file %d",
5680 + in->obj_id);
5681 + in->deleted = 1;
5682 + in->my_dev->n_deleted_files++;
5683 + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
5684 + yaffs_resize_file(in, 0);
5685 + yaffs_soft_del_file(in);
5686 + } else {
5687 + ret_val =
5688 + yaffs_change_obj_name(in, in->my_dev->unlinked_dir,
5689 + _Y("unlinked"), 0, 0);
5691 + return ret_val;
5694 +static int yaffs_del_file(struct yaffs_obj *in)
5696 + int ret_val = YAFFS_OK;
5697 + int deleted; /* Need to cache value on stack if in is freed */
5698 + struct yaffs_dev *dev = in->my_dev;
5700 + if (dev->param.disable_soft_del || dev->param.is_yaffs2)
5701 + yaffs_resize_file(in, 0);
5703 + if (in->n_data_chunks > 0) {
5704 + /* Use soft deletion if there is data in the file.
5705 + * That won't be the case if it has been resized to zero.
5706 + */
5707 + if (!in->unlinked)
5708 + ret_val = yaffs_unlink_file_if_needed(in);
5710 + deleted = in->deleted;
5712 + if (ret_val == YAFFS_OK && in->unlinked && !in->deleted) {
5713 + in->deleted = 1;
5714 + deleted = 1;
5715 + in->my_dev->n_deleted_files++;
5716 + yaffs_soft_del_file(in);
5718 + return deleted ? YAFFS_OK : YAFFS_FAIL;
5719 + } else {
5720 + /* The file has no data chunks so we toss it immediately */
5721 + yaffs_free_tnode(in->my_dev, in->variant.file_variant.top);
5722 + in->variant.file_variant.top = NULL;
5723 + yaffs_generic_obj_del(in);
5725 + return YAFFS_OK;
5729 +int yaffs_is_non_empty_dir(struct yaffs_obj *obj)
5731 + return (obj &&
5732 + obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) &&
5733 + !(list_empty(&obj->variant.dir_variant.children));
5736 +static int yaffs_del_dir(struct yaffs_obj *obj)
5738 + /* First check that the directory is empty. */
5739 + if (yaffs_is_non_empty_dir(obj))
5740 + return YAFFS_FAIL;
5742 + return yaffs_generic_obj_del(obj);
5745 +static int yaffs_del_symlink(struct yaffs_obj *in)
5747 + kfree(in->variant.symlink_variant.alias);
5748 + in->variant.symlink_variant.alias = NULL;
5750 + return yaffs_generic_obj_del(in);
5753 +static int yaffs_del_link(struct yaffs_obj *in)
5755 + /* remove this hardlink from the list associated with the equivalent
5756 + * object
5757 + */
5758 + list_del_init(&in->hard_links);
5759 + return yaffs_generic_obj_del(in);
5762 +int yaffs_del_obj(struct yaffs_obj *obj)
5764 + int ret_val = -1;
5766 + switch (obj->variant_type) {
5767 + case YAFFS_OBJECT_TYPE_FILE:
5768 + ret_val = yaffs_del_file(obj);
5769 + break;
5770 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5771 + if (!list_empty(&obj->variant.dir_variant.dirty)) {
5772 + yaffs_trace(YAFFS_TRACE_BACKGROUND,
5773 + "Remove object %d from dirty directories",
5774 + obj->obj_id);
5775 + list_del_init(&obj->variant.dir_variant.dirty);
5777 + return yaffs_del_dir(obj);
5778 + break;
5779 + case YAFFS_OBJECT_TYPE_SYMLINK:
5780 + ret_val = yaffs_del_symlink(obj);
5781 + break;
5782 + case YAFFS_OBJECT_TYPE_HARDLINK:
5783 + ret_val = yaffs_del_link(obj);
5784 + break;
5785 + case YAFFS_OBJECT_TYPE_SPECIAL:
5786 + ret_val = yaffs_generic_obj_del(obj);
5787 + break;
5788 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5789 + ret_val = 0;
5790 + break; /* should not happen. */
5792 + return ret_val;
5796 +static void yaffs_empty_dir_to_dir(struct yaffs_obj *from_dir,
5797 + struct yaffs_obj *to_dir)
5799 + struct yaffs_obj *obj;
5800 + struct list_head *lh;
5801 + struct list_head *n;
5803 + list_for_each_safe(lh, n, &from_dir->variant.dir_variant.children) {
5804 + obj = list_entry(lh, struct yaffs_obj, siblings);
5805 + yaffs_add_obj_to_dir(to_dir, obj);
5809 +struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
5810 + enum yaffs_obj_type type)
5812 + /* Tear down the old variant */
5813 + switch (obj->variant_type) {
5814 + case YAFFS_OBJECT_TYPE_FILE:
5815 + /* Nuke file data */
5816 + yaffs_resize_file(obj, 0);
5817 + yaffs_free_tnode(obj->my_dev, obj->variant.file_variant.top);
5818 + obj->variant.file_variant.top = NULL;
5819 + break;
5820 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5821 + /* Put the children in lost and found. */
5822 + yaffs_empty_dir_to_dir(obj, obj->my_dev->lost_n_found);
5823 + if (!list_empty(&obj->variant.dir_variant.dirty))
5824 + list_del_init(&obj->variant.dir_variant.dirty);
5825 + break;
5826 + case YAFFS_OBJECT_TYPE_SYMLINK:
5827 + /* Nuke symplink data */
5828 + kfree(obj->variant.symlink_variant.alias);
5829 + obj->variant.symlink_variant.alias = NULL;
5830 + break;
5831 + case YAFFS_OBJECT_TYPE_HARDLINK:
5832 + list_del_init(&obj->hard_links);
5833 + break;
5834 + default:
5835 + break;
5838 + memset(&obj->variant, 0, sizeof(obj->variant));
5840 + /*Set up new variant if the memset is not enough. */
5841 + switch (type) {
5842 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5843 + INIT_LIST_HEAD(&obj->variant.dir_variant.children);
5844 + INIT_LIST_HEAD(&obj->variant.dir_variant.dirty);
5845 + break;
5846 + case YAFFS_OBJECT_TYPE_FILE:
5847 + case YAFFS_OBJECT_TYPE_SYMLINK:
5848 + case YAFFS_OBJECT_TYPE_HARDLINK:
5849 + default:
5850 + break;
5853 + obj->variant_type = type;
5855 + return obj;
5859 +static int yaffs_unlink_worker(struct yaffs_obj *obj)
5861 + int del_now = 0;
5863 + if (!obj)
5864 + return YAFFS_FAIL;
5866 + if (!obj->my_inode)
5867 + del_now = 1;
5869 + yaffs_update_parent(obj->parent);
5871 + if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
5872 + return yaffs_del_link(obj);
5873 + } else if (!list_empty(&obj->hard_links)) {
5874 + /* Curve ball: We're unlinking an object that has a hardlink.
5876 + * This problem arises because we are not strictly following
5877 + * The Linux link/inode model.
5879 + * We can't really delete the object.
5880 + * Instead, we do the following:
5881 + * - Select a hardlink.
5882 + * - Unhook it from the hard links
5883 + * - Move it from its parent directory so that the rename works.
5884 + * - Rename the object to the hardlink's name.
5885 + * - Delete the hardlink
5886 + */
5888 + struct yaffs_obj *hl;
5889 + struct yaffs_obj *parent;
5890 + int ret_val;
5891 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
5893 + hl = list_entry(obj->hard_links.next, struct yaffs_obj,
5894 + hard_links);
5896 + yaffs_get_obj_name(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
5897 + parent = hl->parent;
5899 + list_del_init(&hl->hard_links);
5901 + yaffs_add_obj_to_dir(obj->my_dev->unlinked_dir, hl);
5903 + ret_val = yaffs_change_obj_name(obj, parent, name, 0, 0);
5905 + if (ret_val == YAFFS_OK)
5906 + ret_val = yaffs_generic_obj_del(hl);
5908 + return ret_val;
5910 + } else if (del_now) {
5911 + switch (obj->variant_type) {
5912 + case YAFFS_OBJECT_TYPE_FILE:
5913 + return yaffs_del_file(obj);
5914 + break;
5915 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5916 + list_del_init(&obj->variant.dir_variant.dirty);
5917 + return yaffs_del_dir(obj);
5918 + break;
5919 + case YAFFS_OBJECT_TYPE_SYMLINK:
5920 + return yaffs_del_symlink(obj);
5921 + break;
5922 + case YAFFS_OBJECT_TYPE_SPECIAL:
5923 + return yaffs_generic_obj_del(obj);
5924 + break;
5925 + case YAFFS_OBJECT_TYPE_HARDLINK:
5926 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5927 + default:
5928 + return YAFFS_FAIL;
5930 + } else if (yaffs_is_non_empty_dir(obj)) {
5931 + return YAFFS_FAIL;
5932 + } else {
5933 + return yaffs_change_obj_name(obj, obj->my_dev->unlinked_dir,
5934 + _Y("unlinked"), 0, 0);
5938 +static int yaffs_unlink_obj(struct yaffs_obj *obj)
5940 + if (obj && obj->unlink_allowed)
5941 + return yaffs_unlink_worker(obj);
5943 + return YAFFS_FAIL;
5946 +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR *name)
5948 + struct yaffs_obj *obj;
5950 + obj = yaffs_find_by_name(dir, name);
5951 + return yaffs_unlink_obj(obj);
5954 +/* Note:
5955 + * If old_name is NULL then we take old_dir as the object to be renamed.
5956 + */
5957 +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR *old_name,
5958 + struct yaffs_obj *new_dir, const YCHAR *new_name)
5960 + struct yaffs_obj *obj = NULL;
5961 + struct yaffs_obj *existing_target = NULL;
5962 + int force = 0;
5963 + int result;
5964 + struct yaffs_dev *dev;
5966 + if (!old_dir || old_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
5967 + BUG();
5968 + return YAFFS_FAIL;
5970 + if (!new_dir || new_dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
5971 + BUG();
5972 + return YAFFS_FAIL;
5975 + dev = old_dir->my_dev;
5977 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
5978 + /* Special case for case insemsitive systems.
5979 + * While look-up is case insensitive, the name isn't.
5980 + * Therefore we might want to change x.txt to X.txt
5981 + */
5982 + if (old_dir == new_dir &&
5983 + old_name && new_name &&
5984 + strcmp(old_name, new_name) == 0)
5985 + force = 1;
5986 +#endif
5988 + if (strnlen(new_name, YAFFS_MAX_NAME_LENGTH + 1) >
5989 + YAFFS_MAX_NAME_LENGTH)
5990 + /* ENAMETOOLONG */
5991 + return YAFFS_FAIL;
5993 + if (old_name)
5994 + obj = yaffs_find_by_name(old_dir, old_name);
5995 + else{
5996 + obj = old_dir;
5997 + old_dir = obj->parent;
6000 + if (obj && obj->rename_allowed) {
6001 + /* Now handle an existing target, if there is one */
6002 + existing_target = yaffs_find_by_name(new_dir, new_name);
6003 + if (yaffs_is_non_empty_dir(existing_target)) {
6004 + return YAFFS_FAIL; /* ENOTEMPTY */
6005 + } else if (existing_target && existing_target != obj) {
6006 + /* Nuke the target first, using shadowing,
6007 + * but only if it isn't the same object.
6009 + * Note we must disable gc here otherwise it can mess
6010 + * up the shadowing.
6012 + */
6013 + dev->gc_disable = 1;
6014 + yaffs_change_obj_name(obj, new_dir, new_name, force,
6015 + existing_target->obj_id);
6016 + existing_target->is_shadowed = 1;
6017 + yaffs_unlink_obj(existing_target);
6018 + dev->gc_disable = 0;
6021 + result = yaffs_change_obj_name(obj, new_dir, new_name, 1, 0);
6023 + yaffs_update_parent(old_dir);
6024 + if (new_dir != old_dir)
6025 + yaffs_update_parent(new_dir);
6027 + return result;
6029 + return YAFFS_FAIL;
6032 +/*----------------------- Initialisation Scanning ---------------------- */
6034 +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
6035 + int backward_scanning)
6037 + struct yaffs_obj *obj;
6039 + if (backward_scanning) {
6040 + /* Handle YAFFS2 case (backward scanning)
6041 + * If the shadowed object exists then ignore.
6042 + */
6043 + obj = yaffs_find_by_number(dev, obj_id);
6044 + if (obj)
6045 + return;
6048 + /* Let's create it (if it does not exist) assuming it is a file so that
6049 + * it can do shrinking etc.
6050 + * We put it in unlinked dir to be cleaned up after the scanning
6051 + */
6052 + obj =
6053 + yaffs_find_or_create_by_number(dev, obj_id, YAFFS_OBJECT_TYPE_FILE);
6054 + if (!obj)
6055 + return;
6056 + obj->is_shadowed = 1;
6057 + yaffs_add_obj_to_dir(dev->unlinked_dir, obj);
6058 + obj->variant.file_variant.shrink_size = 0;
6059 + obj->valid = 1; /* So that we don't read any other info. */
6062 +void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list)
6064 + struct list_head *lh;
6065 + struct list_head *save;
6066 + struct yaffs_obj *hl;
6067 + struct yaffs_obj *in;
6069 + list_for_each_safe(lh, save, hard_list) {
6070 + hl = list_entry(lh, struct yaffs_obj, hard_links);
6071 + in = yaffs_find_by_number(dev,
6072 + hl->variant.hardlink_variant.equiv_id);
6074 + if (in) {
6075 + /* Add the hardlink pointers */
6076 + hl->variant.hardlink_variant.equiv_obj = in;
6077 + list_add(&hl->hard_links, &in->hard_links);
6078 + } else {
6079 + /* Todo Need to report/handle this better.
6080 + * Got a problem... hardlink to a non-existant object
6081 + */
6082 + hl->variant.hardlink_variant.equiv_obj = NULL;
6083 + INIT_LIST_HEAD(&hl->hard_links);
6088 +static void yaffs_strip_deleted_objs(struct yaffs_dev *dev)
6090 + /*
6091 + * Sort out state of unlinked and deleted objects after scanning.
6092 + */
6093 + struct list_head *i;
6094 + struct list_head *n;
6095 + struct yaffs_obj *l;
6097 + if (dev->read_only)
6098 + return;
6100 + /* Soft delete all the unlinked files */
6101 + list_for_each_safe(i, n,
6102 + &dev->unlinked_dir->variant.dir_variant.children) {
6103 + l = list_entry(i, struct yaffs_obj, siblings);
6104 + yaffs_del_obj(l);
6107 + list_for_each_safe(i, n, &dev->del_dir->variant.dir_variant.children) {
6108 + l = list_entry(i, struct yaffs_obj, siblings);
6109 + yaffs_del_obj(l);
6114 + * This code iterates through all the objects making sure that they are rooted.
6115 + * Any unrooted objects are re-rooted in lost+found.
6116 + * An object needs to be in one of:
6117 + * - Directly under deleted, unlinked
6118 + * - Directly or indirectly under root.
6120 + * Note:
6121 + * This code assumes that we don't ever change the current relationships
6122 + * between directories:
6123 + * root_dir->parent == unlinked_dir->parent == del_dir->parent == NULL
6124 + * lost-n-found->parent == root_dir
6126 + * This fixes the problem where directories might have inadvertently been
6127 + * deleted leaving the object "hanging" without being rooted in the
6128 + * directory tree.
6129 + */
6131 +static int yaffs_has_null_parent(struct yaffs_dev *dev, struct yaffs_obj *obj)
6133 + return (obj == dev->del_dir ||
6134 + obj == dev->unlinked_dir || obj == dev->root_dir);
6137 +static void yaffs_fix_hanging_objs(struct yaffs_dev *dev)
6139 + struct yaffs_obj *obj;
6140 + struct yaffs_obj *parent;
6141 + int i;
6142 + struct list_head *lh;
6143 + struct list_head *n;
6144 + int depth_limit;
6145 + int hanging;
6147 + if (dev->read_only)
6148 + return;
6150 + /* Iterate through the objects in each hash entry,
6151 + * looking at each object.
6152 + * Make sure it is rooted.
6153 + */
6155 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
6156 + list_for_each_safe(lh, n, &dev->obj_bucket[i].list) {
6157 + obj = list_entry(lh, struct yaffs_obj, hash_link);
6158 + parent = obj->parent;
6160 + if (yaffs_has_null_parent(dev, obj)) {
6161 + /* These directories are not hanging */
6162 + hanging = 0;
6163 + } else if (!parent ||
6164 + parent->variant_type !=
6165 + YAFFS_OBJECT_TYPE_DIRECTORY) {
6166 + hanging = 1;
6167 + } else if (yaffs_has_null_parent(dev, parent)) {
6168 + hanging = 0;
6169 + } else {
6170 + /*
6171 + * Need to follow the parent chain to
6172 + * see if it is hanging.
6173 + */
6174 + hanging = 0;
6175 + depth_limit = 100;
6177 + while (parent != dev->root_dir &&
6178 + parent->parent &&
6179 + parent->parent->variant_type ==
6180 + YAFFS_OBJECT_TYPE_DIRECTORY &&
6181 + depth_limit > 0) {
6182 + parent = parent->parent;
6183 + depth_limit--;
6185 + if (parent != dev->root_dir)
6186 + hanging = 1;
6188 + if (hanging) {
6189 + yaffs_trace(YAFFS_TRACE_SCAN,
6190 + "Hanging object %d moved to lost and found",
6191 + obj->obj_id);
6192 + yaffs_add_obj_to_dir(dev->lost_n_found, obj);
6199 + * Delete directory contents for cleaning up lost and found.
6200 + */
6201 +static void yaffs_del_dir_contents(struct yaffs_obj *dir)
6203 + struct yaffs_obj *obj;
6204 + struct list_head *lh;
6205 + struct list_head *n;
6207 + if (dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
6208 + BUG();
6210 + list_for_each_safe(lh, n, &dir->variant.dir_variant.children) {
6211 + obj = list_entry(lh, struct yaffs_obj, siblings);
6212 + if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
6213 + yaffs_del_dir_contents(obj);
6214 + yaffs_trace(YAFFS_TRACE_SCAN,
6215 + "Deleting lost_found object %d",
6216 + obj->obj_id);
6217 + yaffs_unlink_obj(obj);
6221 +static void yaffs_empty_l_n_f(struct yaffs_dev *dev)
6223 + yaffs_del_dir_contents(dev->lost_n_found);
6227 +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *directory,
6228 + const YCHAR *name)
6230 + int sum;
6231 + struct list_head *i;
6232 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
6233 + struct yaffs_obj *l;
6235 + if (!name)
6236 + return NULL;
6238 + if (!directory) {
6239 + yaffs_trace(YAFFS_TRACE_ALWAYS,
6240 + "tragedy: yaffs_find_by_name: null pointer directory"
6241 + );
6242 + BUG();
6243 + return NULL;
6245 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
6246 + yaffs_trace(YAFFS_TRACE_ALWAYS,
6247 + "tragedy: yaffs_find_by_name: non-directory"
6248 + );
6249 + BUG();
6252 + sum = yaffs_calc_name_sum(name);
6254 + list_for_each(i, &directory->variant.dir_variant.children) {
6255 + l = list_entry(i, struct yaffs_obj, siblings);
6257 + if (l->parent != directory)
6258 + BUG();
6260 + yaffs_check_obj_details_loaded(l);
6262 + /* Special case for lost-n-found */
6263 + if (l->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
6264 + if (!strcmp(name, YAFFS_LOSTNFOUND_NAME))
6265 + return l;
6266 + } else if (l->sum == sum || l->hdr_chunk <= 0) {
6267 + /* LostnFound chunk called Objxxx
6268 + * Do a real check
6269 + */
6270 + yaffs_get_obj_name(l, buffer,
6271 + YAFFS_MAX_NAME_LENGTH + 1);
6272 + if (!strncmp(name, buffer, YAFFS_MAX_NAME_LENGTH))
6273 + return l;
6276 + return NULL;
6279 +/* GetEquivalentObject dereferences any hard links to get to the
6280 + * actual object.
6281 + */
6283 +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj)
6285 + if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK) {
6286 + obj = obj->variant.hardlink_variant.equiv_obj;
6287 + yaffs_check_obj_details_loaded(obj);
6289 + return obj;
6293 + * A note or two on object names.
6294 + * * If the object name is missing, we then make one up in the form objnnn
6296 + * * ASCII names are stored in the object header's name field from byte zero
6297 + * * Unicode names are historically stored starting from byte zero.
6299 + * Then there are automatic Unicode names...
6300 + * The purpose of these is to save names in a way that can be read as
6301 + * ASCII or Unicode names as appropriate, thus allowing a Unicode and ASCII
6302 + * system to share files.
6304 + * These automatic unicode are stored slightly differently...
6305 + * - If the name can fit in the ASCII character space then they are saved as
6306 + * ascii names as per above.
6307 + * - If the name needs Unicode then the name is saved in Unicode
6308 + * starting at oh->name[1].
6310 + */
6311 +static void yaffs_fix_null_name(struct yaffs_obj *obj, YCHAR *name,
6312 + int buffer_size)
6314 + /* Create an object name if we could not find one. */
6315 + if (strnlen(name, YAFFS_MAX_NAME_LENGTH) == 0) {
6316 + YCHAR local_name[20];
6317 + YCHAR num_string[20];
6318 + YCHAR *x = &num_string[19];
6319 + unsigned v = obj->obj_id;
6320 + num_string[19] = 0;
6321 + while (v > 0) {
6322 + x--;
6323 + *x = '0' + (v % 10);
6324 + v /= 10;
6326 + /* make up a name */
6327 + strcpy(local_name, YAFFS_LOSTNFOUND_PREFIX);
6328 + strcat(local_name, x);
6329 + strncpy(name, local_name, buffer_size - 1);
6333 +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR *name, int buffer_size)
6335 + memset(name, 0, buffer_size * sizeof(YCHAR));
6336 + yaffs_check_obj_details_loaded(obj);
6337 + if (obj->obj_id == YAFFS_OBJECTID_LOSTNFOUND) {
6338 + strncpy(name, YAFFS_LOSTNFOUND_NAME, buffer_size - 1);
6339 + } else if (obj->short_name[0]) {
6340 + strcpy(name, obj->short_name);
6341 + } else if (obj->hdr_chunk > 0) {
6342 + int result;
6343 + u8 *buffer = yaffs_get_temp_buffer(obj->my_dev);
6345 + struct yaffs_obj_hdr *oh = (struct yaffs_obj_hdr *)buffer;
6347 + memset(buffer, 0, obj->my_dev->data_bytes_per_chunk);
6349 + if (obj->hdr_chunk > 0) {
6350 + result = yaffs_rd_chunk_tags_nand(obj->my_dev,
6351 + obj->hdr_chunk,
6352 + buffer, NULL);
6354 + yaffs_load_name_from_oh(obj->my_dev, name, oh->name,
6355 + buffer_size);
6357 + yaffs_release_temp_buffer(obj->my_dev, buffer);
6360 + yaffs_fix_null_name(obj, name, buffer_size);
6362 + return strnlen(name, YAFFS_MAX_NAME_LENGTH);
6365 +loff_t yaffs_get_obj_length(struct yaffs_obj *obj)
6367 + /* Dereference any hard linking */
6368 + obj = yaffs_get_equivalent_obj(obj);
6370 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
6371 + return obj->variant.file_variant.file_size;
6372 + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
6373 + if (!obj->variant.symlink_variant.alias)
6374 + return 0;
6375 + return strnlen(obj->variant.symlink_variant.alias,
6376 + YAFFS_MAX_ALIAS_LENGTH);
6377 + } else {
6378 + /* Only a directory should drop through to here */
6379 + return obj->my_dev->data_bytes_per_chunk;
6383 +int yaffs_get_obj_link_count(struct yaffs_obj *obj)
6385 + int count = 0;
6386 + struct list_head *i;
6388 + if (!obj->unlinked)
6389 + count++; /* the object itself */
6391 + list_for_each(i, &obj->hard_links)
6392 + count++; /* add the hard links; */
6394 + return count;
6397 +int yaffs_get_obj_inode(struct yaffs_obj *obj)
6399 + obj = yaffs_get_equivalent_obj(obj);
6401 + return obj->obj_id;
6404 +unsigned yaffs_get_obj_type(struct yaffs_obj *obj)
6406 + obj = yaffs_get_equivalent_obj(obj);
6408 + switch (obj->variant_type) {
6409 + case YAFFS_OBJECT_TYPE_FILE:
6410 + return DT_REG;
6411 + break;
6412 + case YAFFS_OBJECT_TYPE_DIRECTORY:
6413 + return DT_DIR;
6414 + break;
6415 + case YAFFS_OBJECT_TYPE_SYMLINK:
6416 + return DT_LNK;
6417 + break;
6418 + case YAFFS_OBJECT_TYPE_HARDLINK:
6419 + return DT_REG;
6420 + break;
6421 + case YAFFS_OBJECT_TYPE_SPECIAL:
6422 + if (S_ISFIFO(obj->yst_mode))
6423 + return DT_FIFO;
6424 + if (S_ISCHR(obj->yst_mode))
6425 + return DT_CHR;
6426 + if (S_ISBLK(obj->yst_mode))
6427 + return DT_BLK;
6428 + if (S_ISSOCK(obj->yst_mode))
6429 + return DT_SOCK;
6430 + return DT_REG;
6431 + break;
6432 + default:
6433 + return DT_REG;
6434 + break;
6438 +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj)
6440 + obj = yaffs_get_equivalent_obj(obj);
6441 + if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
6442 + return yaffs_clone_str(obj->variant.symlink_variant.alias);
6443 + else
6444 + return yaffs_clone_str(_Y(""));
6447 +/*--------------------------- Initialisation code -------------------------- */
6449 +static int yaffs_check_dev_fns(struct yaffs_dev *dev)
6451 + struct yaffs_driver *drv = &dev->drv;
6452 + struct yaffs_tags_handler *tagger = &dev->tagger;
6454 + /* Common functions, gotta have */
6455 + if (!drv->drv_read_chunk_fn ||
6456 + !drv->drv_write_chunk_fn ||
6457 + !drv->drv_erase_fn)
6458 + return 0;
6460 + if (dev->param.is_yaffs2 &&
6461 + (!drv->drv_mark_bad_fn || !drv->drv_check_bad_fn))
6462 + return 0;
6464 + /* Install the default tags marshalling functions if needed. */
6465 + yaffs_tags_compat_install(dev);
6466 + yaffs_tags_marshall_install(dev);
6468 + /* Check we now have the marshalling functions required. */
6469 + if (!tagger->write_chunk_tags_fn ||
6470 + !tagger->read_chunk_tags_fn ||
6471 + !tagger->query_block_fn ||
6472 + !tagger->mark_bad_fn)
6473 + return 0;
6475 + return 1;
6478 +static int yaffs_create_initial_dir(struct yaffs_dev *dev)
6480 + /* Initialise the unlinked, deleted, root and lost+found directories */
6481 + dev->lost_n_found = dev->root_dir = NULL;
6482 + dev->unlinked_dir = dev->del_dir = NULL;
6483 + dev->unlinked_dir =
6484 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
6485 + dev->del_dir =
6486 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
6487 + dev->root_dir =
6488 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_ROOT,
6489 + YAFFS_ROOT_MODE | S_IFDIR);
6490 + dev->lost_n_found =
6491 + yaffs_create_fake_dir(dev, YAFFS_OBJECTID_LOSTNFOUND,
6492 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
6494 + if (dev->lost_n_found && dev->root_dir && dev->unlinked_dir
6495 + && dev->del_dir) {
6496 + yaffs_add_obj_to_dir(dev->root_dir, dev->lost_n_found);
6497 + return YAFFS_OK;
6499 + return YAFFS_FAIL;
6502 +/* Low level init.
6503 + * Typically only used by yaffs_guts_initialise, but also used by the
6504 + * Low level yaffs driver tests.
6505 + */
6507 +int yaffs_guts_ll_init(struct yaffs_dev *dev)
6511 + yaffs_trace(YAFFS_TRACE_TRACING, "yaffs: yaffs_ll_init()");
6513 + if (!dev) {
6514 + yaffs_trace(YAFFS_TRACE_ALWAYS,
6515 + "yaffs: Need a device"
6516 + );
6517 + return YAFFS_FAIL;
6520 + if (dev->ll_init)
6521 + return YAFFS_OK;
6523 + dev->internal_start_block = dev->param.start_block;
6524 + dev->internal_end_block = dev->param.end_block;
6525 + dev->block_offset = 0;
6526 + dev->chunk_offset = 0;
6527 + dev->n_free_chunks = 0;
6529 + dev->gc_block = 0;
6531 + if (dev->param.start_block == 0) {
6532 + dev->internal_start_block = dev->param.start_block + 1;
6533 + dev->internal_end_block = dev->param.end_block + 1;
6534 + dev->block_offset = 1;
6535 + dev->chunk_offset = dev->param.chunks_per_block;
6538 + /* Check geometry parameters. */
6540 + if ((!dev->param.inband_tags && dev->param.is_yaffs2 &&
6541 + dev->param.total_bytes_per_chunk < 1024) ||
6542 + (!dev->param.is_yaffs2 &&
6543 + dev->param.total_bytes_per_chunk < 512) ||
6544 + (dev->param.inband_tags && !dev->param.is_yaffs2) ||
6545 + dev->param.chunks_per_block < 2 ||
6546 + dev->param.n_reserved_blocks < 2 ||
6547 + dev->internal_start_block <= 0 ||
6548 + dev->internal_end_block <= 0 ||
6549 + dev->internal_end_block <=
6550 + (dev->internal_start_block + dev->param.n_reserved_blocks + 2)
6551 + ) {
6552 + /* otherwise it is too small */
6553 + yaffs_trace(YAFFS_TRACE_ALWAYS,
6554 + "NAND geometry problems: chunk size %d, type is yaffs%s, inband_tags %d ",
6555 + dev->param.total_bytes_per_chunk,
6556 + dev->param.is_yaffs2 ? "2" : "",
6557 + dev->param.inband_tags);
6558 + return YAFFS_FAIL;
6561 + /* Sort out space for inband tags, if required */
6562 + if (dev->param.inband_tags)
6563 + dev->data_bytes_per_chunk =
6564 + dev->param.total_bytes_per_chunk -
6565 + sizeof(struct yaffs_packed_tags2_tags_only);
6566 + else
6567 + dev->data_bytes_per_chunk = dev->param.total_bytes_per_chunk;
6569 + /* Got the right mix of functions? */
6570 + if (!yaffs_check_dev_fns(dev)) {
6571 + /* Function missing */
6572 + yaffs_trace(YAFFS_TRACE_ALWAYS,
6573 + "device function(s) missing or wrong");
6575 + return YAFFS_FAIL;
6578 + if (yaffs_init_nand(dev) != YAFFS_OK) {
6579 + yaffs_trace(YAFFS_TRACE_ALWAYS, "InitialiseNAND failed");
6580 + return YAFFS_FAIL;
6583 + return YAFFS_OK;
6587 +int yaffs_guts_format_dev(struct yaffs_dev *dev)
6589 + int i;
6590 + enum yaffs_block_state state;
6591 + u32 dummy;
6593 + if(yaffs_guts_ll_init(dev) != YAFFS_OK)
6594 + return YAFFS_FAIL;
6596 + if(dev->is_mounted)
6597 + return YAFFS_FAIL;
6599 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
6600 + yaffs_query_init_block_state(dev, i, &state, &dummy);
6601 + if (state != YAFFS_BLOCK_STATE_DEAD)
6602 + yaffs_erase_block(dev, i);
6605 + return YAFFS_OK;
6609 +int yaffs_guts_initialise(struct yaffs_dev *dev)
6611 + int init_failed = 0;
6612 + unsigned x;
6613 + int bits;
6615 + if(yaffs_guts_ll_init(dev) != YAFFS_OK)
6616 + return YAFFS_FAIL;
6618 + if (dev->is_mounted) {
6619 + yaffs_trace(YAFFS_TRACE_ALWAYS, "device already mounted");
6620 + return YAFFS_FAIL;
6623 + dev->is_mounted = 1;
6625 + /* OK now calculate a few things for the device */
6627 + /*
6628 + * Calculate all the chunk size manipulation numbers:
6629 + */
6630 + x = dev->data_bytes_per_chunk;
6631 + /* We always use dev->chunk_shift and dev->chunk_div */
6632 + dev->chunk_shift = calc_shifts(x);
6633 + x >>= dev->chunk_shift;
6634 + dev->chunk_div = x;
6635 + /* We only use chunk mask if chunk_div is 1 */
6636 + dev->chunk_mask = (1 << dev->chunk_shift) - 1;
6638 + /*
6639 + * Calculate chunk_grp_bits.
6640 + * We need to find the next power of 2 > than internal_end_block
6641 + */
6643 + x = dev->param.chunks_per_block * (dev->internal_end_block + 1);
6645 + bits = calc_shifts_ceiling(x);
6647 + /* Set up tnode width if wide tnodes are enabled. */
6648 + if (!dev->param.wide_tnodes_disabled) {
6649 + /* bits must be even so that we end up with 32-bit words */
6650 + if (bits & 1)
6651 + bits++;
6652 + if (bits < 16)
6653 + dev->tnode_width = 16;
6654 + else
6655 + dev->tnode_width = bits;
6656 + } else {
6657 + dev->tnode_width = 16;
6660 + dev->tnode_mask = (1 << dev->tnode_width) - 1;
6662 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
6663 + * so if the bitwidth of the
6664 + * chunk range we're using is greater than 16 we need
6665 + * to figure out chunk shift and chunk_grp_size
6666 + */
6668 + if (bits <= dev->tnode_width)
6669 + dev->chunk_grp_bits = 0;
6670 + else
6671 + dev->chunk_grp_bits = bits - dev->tnode_width;
6673 + dev->tnode_size = (dev->tnode_width * YAFFS_NTNODES_LEVEL0) / 8;
6674 + if (dev->tnode_size < sizeof(struct yaffs_tnode))
6675 + dev->tnode_size = sizeof(struct yaffs_tnode);
6677 + dev->chunk_grp_size = 1 << dev->chunk_grp_bits;
6679 + if (dev->param.chunks_per_block < dev->chunk_grp_size) {
6680 + /* We have a problem because the soft delete won't work if
6681 + * the chunk group size > chunks per block.
6682 + * This can be remedied by using larger "virtual blocks".
6683 + */
6684 + yaffs_trace(YAFFS_TRACE_ALWAYS, "chunk group too large");
6686 + return YAFFS_FAIL;
6689 + /* Finished verifying the device, continue with initialisation */
6691 + /* More device initialisation */
6692 + dev->all_gcs = 0;
6693 + dev->passive_gc_count = 0;
6694 + dev->oldest_dirty_gc_count = 0;
6695 + dev->bg_gcs = 0;
6696 + dev->gc_block_finder = 0;
6697 + dev->buffered_block = -1;
6698 + dev->doing_buffered_block_rewrite = 0;
6699 + dev->n_deleted_files = 0;
6700 + dev->n_bg_deletions = 0;
6701 + dev->n_unlinked_files = 0;
6702 + dev->n_ecc_fixed = 0;
6703 + dev->n_ecc_unfixed = 0;
6704 + dev->n_tags_ecc_fixed = 0;
6705 + dev->n_tags_ecc_unfixed = 0;
6706 + dev->n_erase_failures = 0;
6707 + dev->n_erased_blocks = 0;
6708 + dev->gc_disable = 0;
6709 + dev->has_pending_prioritised_gc = 1;
6710 + /* Assume the worst for now, will get fixed on first GC */
6711 + INIT_LIST_HEAD(&dev->dirty_dirs);
6712 + dev->oldest_dirty_seq = 0;
6713 + dev->oldest_dirty_block = 0;
6715 + /* Initialise temporary buffers and caches. */
6716 + if (!yaffs_init_tmp_buffers(dev))
6717 + init_failed = 1;
6719 + dev->cache = NULL;
6720 + dev->gc_cleanup_list = NULL;
6722 + if (!init_failed && dev->param.n_caches > 0) {
6723 + int i;
6724 + void *buf;
6725 + int cache_bytes =
6726 + dev->param.n_caches * sizeof(struct yaffs_cache);
6728 + if (dev->param.n_caches > YAFFS_MAX_SHORT_OP_CACHES)
6729 + dev->param.n_caches = YAFFS_MAX_SHORT_OP_CACHES;
6731 + dev->cache = kmalloc(cache_bytes, GFP_NOFS);
6733 + buf = (u8 *) dev->cache;
6735 + if (dev->cache)
6736 + memset(dev->cache, 0, cache_bytes);
6738 + for (i = 0; i < dev->param.n_caches && buf; i++) {
6739 + dev->cache[i].object = NULL;
6740 + dev->cache[i].last_use = 0;
6741 + dev->cache[i].dirty = 0;
6742 + dev->cache[i].data = buf =
6743 + kmalloc(dev->param.total_bytes_per_chunk, GFP_NOFS);
6745 + if (!buf)
6746 + init_failed = 1;
6748 + dev->cache_last_use = 0;
6751 + dev->cache_hits = 0;
6753 + if (!init_failed) {
6754 + dev->gc_cleanup_list =
6755 + kmalloc(dev->param.chunks_per_block * sizeof(u32),
6756 + GFP_NOFS);
6757 + if (!dev->gc_cleanup_list)
6758 + init_failed = 1;
6761 + if (dev->param.is_yaffs2)
6762 + dev->param.use_header_file_size = 1;
6764 + if (!init_failed && !yaffs_init_blocks(dev))
6765 + init_failed = 1;
6767 + yaffs_init_tnodes_and_objs(dev);
6769 + if (!init_failed && !yaffs_create_initial_dir(dev))
6770 + init_failed = 1;
6772 + if (!init_failed && dev->param.is_yaffs2 &&
6773 + !dev->param.disable_summary &&
6774 + !yaffs_summary_init(dev))
6775 + init_failed = 1;
6777 + if (!init_failed) {
6778 + /* Now scan the flash. */
6779 + if (dev->param.is_yaffs2) {
6780 + if (yaffs2_checkpt_restore(dev)) {
6781 + yaffs_check_obj_details_loaded(dev->root_dir);
6782 + yaffs_trace(YAFFS_TRACE_CHECKPOINT |
6783 + YAFFS_TRACE_MOUNT,
6784 + "yaffs: restored from checkpoint"
6785 + );
6786 + } else {
6788 + /* Clean up the mess caused by an aborted
6789 + * checkpoint load then scan backwards.
6790 + */
6791 + yaffs_deinit_blocks(dev);
6793 + yaffs_deinit_tnodes_and_objs(dev);
6795 + dev->n_erased_blocks = 0;
6796 + dev->n_free_chunks = 0;
6797 + dev->alloc_block = -1;
6798 + dev->alloc_page = -1;
6799 + dev->n_deleted_files = 0;
6800 + dev->n_unlinked_files = 0;
6801 + dev->n_bg_deletions = 0;
6803 + if (!init_failed && !yaffs_init_blocks(dev))
6804 + init_failed = 1;
6806 + yaffs_init_tnodes_and_objs(dev);
6808 + if (!init_failed
6809 + && !yaffs_create_initial_dir(dev))
6810 + init_failed = 1;
6812 + if (!init_failed && !yaffs2_scan_backwards(dev))
6813 + init_failed = 1;
6815 + } else if (!yaffs1_scan(dev)) {
6816 + init_failed = 1;
6819 + yaffs_strip_deleted_objs(dev);
6820 + yaffs_fix_hanging_objs(dev);
6821 + if (dev->param.empty_lost_n_found)
6822 + yaffs_empty_l_n_f(dev);
6825 + if (init_failed) {
6826 + /* Clean up the mess */
6827 + yaffs_trace(YAFFS_TRACE_TRACING,
6828 + "yaffs: yaffs_guts_initialise() aborted.");
6830 + yaffs_deinitialise(dev);
6831 + return YAFFS_FAIL;
6834 + /* Zero out stats */
6835 + dev->n_page_reads = 0;
6836 + dev->n_page_writes = 0;
6837 + dev->n_erasures = 0;
6838 + dev->n_gc_copies = 0;
6839 + dev->n_retried_writes = 0;
6841 + dev->n_retired_blocks = 0;
6843 + yaffs_verify_free_chunks(dev);
6844 + yaffs_verify_blocks(dev);
6846 + /* Clean up any aborted checkpoint data */
6847 + if (!dev->is_checkpointed && dev->blocks_in_checkpt > 0)
6848 + yaffs2_checkpt_invalidate(dev);
6850 + yaffs_trace(YAFFS_TRACE_TRACING,
6851 + "yaffs: yaffs_guts_initialise() done.");
6852 + return YAFFS_OK;
6855 +void yaffs_deinitialise(struct yaffs_dev *dev)
6857 + if (dev->is_mounted) {
6858 + int i;
6860 + yaffs_deinit_blocks(dev);
6861 + yaffs_deinit_tnodes_and_objs(dev);
6862 + yaffs_summary_deinit(dev);
6864 + if (dev->param.n_caches > 0 && dev->cache) {
6866 + for (i = 0; i < dev->param.n_caches; i++) {
6867 + kfree(dev->cache[i].data);
6868 + dev->cache[i].data = NULL;
6871 + kfree(dev->cache);
6872 + dev->cache = NULL;
6875 + kfree(dev->gc_cleanup_list);
6877 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++)
6878 + kfree(dev->temp_buffer[i].buffer);
6880 + dev->is_mounted = 0;
6882 + yaffs_deinit_nand(dev);
6886 +int yaffs_count_free_chunks(struct yaffs_dev *dev)
6888 + int n_free = 0;
6889 + int b;
6890 + struct yaffs_block_info *blk;
6892 + blk = dev->block_info;
6893 + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
6894 + switch (blk->block_state) {
6895 + case YAFFS_BLOCK_STATE_EMPTY:
6896 + case YAFFS_BLOCK_STATE_ALLOCATING:
6897 + case YAFFS_BLOCK_STATE_COLLECTING:
6898 + case YAFFS_BLOCK_STATE_FULL:
6899 + n_free +=
6900 + (dev->param.chunks_per_block - blk->pages_in_use +
6901 + blk->soft_del_pages);
6902 + break;
6903 + default:
6904 + break;
6906 + blk++;
6908 + return n_free;
6911 +int yaffs_get_n_free_chunks(struct yaffs_dev *dev)
6913 + /* This is what we report to the outside world */
6914 + int n_free;
6915 + int n_dirty_caches;
6916 + int blocks_for_checkpt;
6917 + int i;
6919 + n_free = dev->n_free_chunks;
6920 + n_free += dev->n_deleted_files;
6922 + /* Now count and subtract the number of dirty chunks in the cache. */
6924 + for (n_dirty_caches = 0, i = 0; i < dev->param.n_caches; i++) {
6925 + if (dev->cache[i].dirty)
6926 + n_dirty_caches++;
6929 + n_free -= n_dirty_caches;
6931 + n_free -=
6932 + ((dev->param.n_reserved_blocks + 1) * dev->param.chunks_per_block);
6934 + /* Now figure checkpoint space and report that... */
6935 + blocks_for_checkpt = yaffs_calc_checkpt_blocks_required(dev);
6937 + n_free -= (blocks_for_checkpt * dev->param.chunks_per_block);
6939 + if (n_free < 0)
6940 + n_free = 0;
6942 + return n_free;
6948 + * Marshalling functions to get loff_t file sizes into and out of
6949 + * object headers.
6950 + */
6951 +void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize)
6953 + oh->file_size_low = (fsize & 0xFFFFFFFF);
6954 + oh->file_size_high = ((fsize >> 32) & 0xFFFFFFFF);
6957 +loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh)
6959 + loff_t retval;
6961 + if (sizeof(loff_t) >= 8 && ~(oh->file_size_high))
6962 + retval = (((loff_t) oh->file_size_high) << 32) |
6963 + (((loff_t) oh->file_size_low) & 0xFFFFFFFF);
6964 + else
6965 + retval = (loff_t) oh->file_size_low;
6967 + return retval;
6971 +void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10])
6973 + int i;
6974 + struct yaffs_block_info *bi;
6975 + int s;
6977 + for(i = 0; i < 10; i++)
6978 + bs[i] = 0;
6980 + for(i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
6981 + bi = yaffs_get_block_info(dev, i);
6982 + s = bi->block_state;
6983 + if(s > YAFFS_BLOCK_STATE_DEAD || s < YAFFS_BLOCK_STATE_UNKNOWN)
6984 + bs[0]++;
6985 + else
6986 + bs[s]++;
6989 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_guts.h linux-3.12.20/fs/yaffs2/yaffs_guts.h
6990 --- linux-3.12.20.orig/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
6991 +++ linux-3.12.20/fs/yaffs2/yaffs_guts.h 2014-05-17 11:28:49.000000000 +0200
6992 @@ -0,0 +1,1007 @@
6994 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
6996 + * Copyright (C) 2002-2011 Aleph One Ltd.
6997 + * for Toby Churchill Ltd and Brightstar Engineering
6999 + * Created by Charles Manning <charles@aleph1.co.uk>
7001 + * This program is free software; you can redistribute it and/or modify
7002 + * it under the terms of the GNU Lesser General Public License version 2.1 as
7003 + * published by the Free Software Foundation.
7005 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
7006 + */
7008 +#ifndef __YAFFS_GUTS_H__
7009 +#define __YAFFS_GUTS_H__
7011 +#include "yportenv.h"
7013 +#define YAFFS_OK 1
7014 +#define YAFFS_FAIL 0
7016 +/* Give us a Y=0x59,
7017 + * Give us an A=0x41,
7018 + * Give us an FF=0xff
7019 + * Give us an S=0x53
7020 + * And what have we got...
7021 + */
7022 +#define YAFFS_MAGIC 0x5941ff53
7025 + * Tnodes form a tree with the tnodes in "levels"
7026 + * Levels greater than 0 hold 8 slots which point to other tnodes.
7027 + * Those at level 0 hold 16 slots which point to chunks in NAND.
7029 + * A maximum level of 8 thust supports files of size up to:
7031 + * 2^(3*MAX_LEVEL+4)
7033 + * Thus a max level of 8 supports files with up to 2^^28 chunks which gives
7034 + * a maximum file size of around 512Gbytees with 2k chunks.
7035 + */
7036 +#define YAFFS_NTNODES_LEVEL0 16
7037 +#define YAFFS_TNODES_LEVEL0_BITS 4
7038 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
7040 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
7041 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
7042 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
7043 +#define YAFFS_TNODES_MAX_LEVEL 8
7044 +#define YAFFS_TNODES_MAX_BITS (YAFFS_TNODES_LEVEL0_BITS + \
7045 + YAFFS_TNODES_INTERNAL_BITS * \
7046 + YAFFS_TNODES_MAX_LEVEL)
7047 +#define YAFFS_MAX_CHUNK_ID ((1 << YAFFS_TNODES_MAX_BITS) - 1)
7049 +#define YAFFS_MAX_FILE_SIZE_32 0x7fffffff
7051 +/* Constants for YAFFS1 mode */
7052 +#define YAFFS_BYTES_PER_SPARE 16
7053 +#define YAFFS_BYTES_PER_CHUNK 512
7054 +#define YAFFS_CHUNK_SIZE_SHIFT 9
7055 +#define YAFFS_CHUNKS_PER_BLOCK 32
7056 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
7058 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
7059 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
7063 +#define YAFFS_ALLOCATION_NOBJECTS 100
7064 +#define YAFFS_ALLOCATION_NTNODES 100
7065 +#define YAFFS_ALLOCATION_NLINKS 100
7067 +#define YAFFS_NOBJECT_BUCKETS 256
7069 +#define YAFFS_OBJECT_SPACE 0x40000
7070 +#define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE - 1)
7072 +/* Binary data version stamps */
7073 +#define YAFFS_SUMMARY_VERSION 1
7074 +#define YAFFS_CHECKPOINT_VERSION 7
7076 +#ifdef CONFIG_YAFFS_UNICODE
7077 +#define YAFFS_MAX_NAME_LENGTH 127
7078 +#define YAFFS_MAX_ALIAS_LENGTH 79
7079 +#else
7080 +#define YAFFS_MAX_NAME_LENGTH 255
7081 +#define YAFFS_MAX_ALIAS_LENGTH 159
7082 +#endif
7084 +#define YAFFS_SHORT_NAME_LENGTH 15
7086 +/* Some special object ids for pseudo objects */
7087 +#define YAFFS_OBJECTID_ROOT 1
7088 +#define YAFFS_OBJECTID_LOSTNFOUND 2
7089 +#define YAFFS_OBJECTID_UNLINKED 3
7090 +#define YAFFS_OBJECTID_DELETED 4
7092 +/* Fake object Id for summary data */
7093 +#define YAFFS_OBJECTID_SUMMARY 0x10
7095 +/* Pseudo object ids for checkpointing */
7096 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
7097 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
7099 +#define YAFFS_MAX_SHORT_OP_CACHES 20
7101 +#define YAFFS_N_TEMP_BUFFERS 6
7103 +/* We limit the number attempts at sucessfully saving a chunk of data.
7104 + * Small-page devices have 32 pages per block; large-page devices have 64.
7105 + * Default to something in the order of 5 to 10 blocks worth of chunks.
7106 + */
7107 +#define YAFFS_WR_ATTEMPTS (5*64)
7109 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
7110 + * The range is limited slightly to help distinguish bad numbers from good.
7111 + * This also allows us to perhaps in the future use special numbers for
7112 + * special purposes.
7113 + * EFFFFF00 allows the allocation of 8 blocks/second (~1Mbytes) for 15 years,
7114 + * and is a larger number than the lifetime of a 2GB device.
7115 + */
7116 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
7117 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xefffff00
7119 +/* Special sequence number for bad block that failed to be marked bad */
7120 +#define YAFFS_SEQUENCE_BAD_BLOCK 0xffff0000
7122 +/* ChunkCache is used for short read/write operations.*/
7123 +struct yaffs_cache {
7124 + struct yaffs_obj *object;
7125 + int chunk_id;
7126 + int last_use;
7127 + int dirty;
7128 + int n_bytes; /* Only valid if the cache is dirty */
7129 + int locked; /* Can't push out or flush while locked. */
7130 + u8 *data;
7133 +/* yaffs1 tags structures in RAM
7134 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary
7135 + * otherwise the structure size will get blown out.
7136 + */
7138 +struct yaffs_tags {
7139 + u32 chunk_id:20;
7140 + u32 serial_number:2;
7141 + u32 n_bytes_lsb:10;
7142 + u32 obj_id:18;
7143 + u32 ecc:12;
7144 + u32 n_bytes_msb:2;
7147 +union yaffs_tags_union {
7148 + struct yaffs_tags as_tags;
7149 + u8 as_bytes[8];
7153 +/* Stuff used for extended tags in YAFFS2 */
7155 +enum yaffs_ecc_result {
7156 + YAFFS_ECC_RESULT_UNKNOWN,
7157 + YAFFS_ECC_RESULT_NO_ERROR,
7158 + YAFFS_ECC_RESULT_FIXED,
7159 + YAFFS_ECC_RESULT_UNFIXED
7162 +enum yaffs_obj_type {
7163 + YAFFS_OBJECT_TYPE_UNKNOWN,
7164 + YAFFS_OBJECT_TYPE_FILE,
7165 + YAFFS_OBJECT_TYPE_SYMLINK,
7166 + YAFFS_OBJECT_TYPE_DIRECTORY,
7167 + YAFFS_OBJECT_TYPE_HARDLINK,
7168 + YAFFS_OBJECT_TYPE_SPECIAL
7171 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
7173 +struct yaffs_ext_tags {
7174 + unsigned chunk_used; /* Status of the chunk: used or unused */
7175 + unsigned obj_id; /* If 0 this is not used */
7176 + unsigned chunk_id; /* If 0 this is a header, else a data chunk */
7177 + unsigned n_bytes; /* Only valid for data chunks */
7179 + /* The following stuff only has meaning when we read */
7180 + enum yaffs_ecc_result ecc_result;
7181 + unsigned block_bad;
7183 + /* YAFFS 1 stuff */
7184 + unsigned is_deleted; /* The chunk is marked deleted */
7185 + unsigned serial_number; /* Yaffs1 2-bit serial number */
7187 + /* YAFFS2 stuff */
7188 + unsigned seq_number; /* The sequence number of this block */
7190 + /* Extra info if this is an object header (YAFFS2 only) */
7192 + unsigned extra_available; /* Extra info available if not zero */
7193 + unsigned extra_parent_id; /* The parent object */
7194 + unsigned extra_is_shrink; /* Is it a shrink header? */
7195 + unsigned extra_shadows; /* Does this shadow another object? */
7197 + enum yaffs_obj_type extra_obj_type; /* What object type? */
7199 + loff_t extra_file_size; /* Length if it is a file */
7200 + unsigned extra_equiv_id; /* Equivalent object for a hard link */
7203 +/* Spare structure for YAFFS1 */
7204 +struct yaffs_spare {
7205 + u8 tb0;
7206 + u8 tb1;
7207 + u8 tb2;
7208 + u8 tb3;
7209 + u8 page_status; /* set to 0 to delete the chunk */
7210 + u8 block_status;
7211 + u8 tb4;
7212 + u8 tb5;
7213 + u8 ecc1[3];
7214 + u8 tb6;
7215 + u8 tb7;
7216 + u8 ecc2[3];
7219 +/*Special structure for passing through to mtd */
7220 +struct yaffs_nand_spare {
7221 + struct yaffs_spare spare;
7222 + int eccres1;
7223 + int eccres2;
7226 +/* Block data in RAM */
7228 +enum yaffs_block_state {
7229 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
7231 + YAFFS_BLOCK_STATE_SCANNING,
7232 + /* Being scanned */
7234 + YAFFS_BLOCK_STATE_NEEDS_SCAN,
7235 + /* The block might have something on it (ie it is allocating or full,
7236 + * perhaps empty) but it needs to be scanned to determine its true
7237 + * state.
7238 + * This state is only valid during scanning.
7239 + * NB We tolerate empty because the pre-scanner might be incapable of
7240 + * deciding
7241 + * However, if this state is returned on a YAFFS2 device,
7242 + * then we expect a sequence number
7243 + */
7245 + YAFFS_BLOCK_STATE_EMPTY,
7246 + /* This block is empty */
7248 + YAFFS_BLOCK_STATE_ALLOCATING,
7249 + /* This block is partially allocated.
7250 + * At least one page holds valid data.
7251 + * This is the one currently being used for page
7252 + * allocation. Should never be more than one of these.
7253 + * If a block is only partially allocated at mount it is treated as
7254 + * full.
7255 + */
7257 + YAFFS_BLOCK_STATE_FULL,
7258 + /* All the pages in this block have been allocated.
7259 + * If a block was only partially allocated when mounted we treat
7260 + * it as fully allocated.
7261 + */
7263 + YAFFS_BLOCK_STATE_DIRTY,
7264 + /* The block was full and now all chunks have been deleted.
7265 + * Erase me, reuse me.
7266 + */
7268 + YAFFS_BLOCK_STATE_CHECKPOINT,
7269 + /* This block is assigned to holding checkpoint data. */
7271 + YAFFS_BLOCK_STATE_COLLECTING,
7272 + /* This block is being garbage collected */
7274 + YAFFS_BLOCK_STATE_DEAD
7275 + /* This block has failed and is not in use */
7278 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
7280 +struct yaffs_block_info {
7282 + s32 soft_del_pages:10; /* number of soft deleted pages */
7283 + s32 pages_in_use:10; /* number of pages in use */
7284 + u32 block_state:4; /* One of the above block states. */
7285 + /* NB use unsigned because enum is sometimes
7286 + * an int */
7287 + u32 needs_retiring:1; /* Data has failed on this block, */
7288 + /*need to get valid data off and retire*/
7289 + u32 skip_erased_check:1;/* Skip the erased check on this block */
7290 + u32 gc_prioritise:1; /* An ECC check or blank check has failed.
7291 + Block should be prioritised for GC */
7292 + u32 chunk_error_strikes:3; /* How many times we've had ecc etc
7293 + failures on this block and tried to reuse it */
7294 + u32 has_summary:1; /* The block has a summary */
7296 + u32 has_shrink_hdr:1; /* This block has at least one shrink header */
7297 + u32 seq_number; /* block sequence number for yaffs2 */
7301 +/* -------------------------- Object structure -------------------------------*/
7302 +/* This is the object structure as stored on NAND */
7304 +struct yaffs_obj_hdr {
7305 + enum yaffs_obj_type type;
7307 + /* Apply to everything */
7308 + int parent_obj_id;
7309 + u16 sum_no_longer_used; /* checksum of name. No longer used */
7310 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
7312 + /* The following apply to all object types except for hard links */
7313 + u32 yst_mode; /* protection */
7315 + u32 yst_uid;
7316 + u32 yst_gid;
7317 + u32 yst_atime;
7318 + u32 yst_mtime;
7319 + u32 yst_ctime;
7321 + /* File size applies to files only */
7322 + u32 file_size_low;
7324 + /* Equivalent object id applies to hard links only. */
7325 + int equiv_id;
7327 + /* Alias is for symlinks only. */
7328 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
7330 + u32 yst_rdev; /* stuff for block and char devices (major/min) */
7332 + u32 win_ctime[2];
7333 + u32 win_atime[2];
7334 + u32 win_mtime[2];
7336 + u32 inband_shadowed_obj_id;
7337 + u32 inband_is_shrink;
7339 + u32 file_size_high;
7340 + u32 reserved[1];
7341 + int shadows_obj; /* This object header shadows the
7342 + specified object if > 0 */
7344 + /* is_shrink applies to object headers written when wemake a hole. */
7345 + u32 is_shrink;
7349 +/*--------------------------- Tnode -------------------------- */
7351 +struct yaffs_tnode {
7352 + struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
7355 +/*------------------------ Object -----------------------------*/
7356 +/* An object can be one of:
7357 + * - a directory (no data, has children links
7358 + * - a regular file (data.... not prunes :->).
7359 + * - a symlink [symbolic link] (the alias).
7360 + * - a hard link
7361 + */
7363 +struct yaffs_file_var {
7364 + loff_t file_size;
7365 + loff_t scanned_size;
7366 + loff_t shrink_size;
7367 + int top_level;
7368 + struct yaffs_tnode *top;
7371 +struct yaffs_dir_var {
7372 + struct list_head children; /* list of child links */
7373 + struct list_head dirty; /* Entry for list of dirty directories */
7376 +struct yaffs_symlink_var {
7377 + YCHAR *alias;
7380 +struct yaffs_hardlink_var {
7381 + struct yaffs_obj *equiv_obj;
7382 + u32 equiv_id;
7385 +union yaffs_obj_var {
7386 + struct yaffs_file_var file_variant;
7387 + struct yaffs_dir_var dir_variant;
7388 + struct yaffs_symlink_var symlink_variant;
7389 + struct yaffs_hardlink_var hardlink_variant;
7392 +struct yaffs_obj {
7393 + u8 deleted:1; /* This should only apply to unlinked files. */
7394 + u8 soft_del:1; /* it has also been soft deleted */
7395 + u8 unlinked:1; /* An unlinked file.*/
7396 + u8 fake:1; /* A fake object has no presence on NAND. */
7397 + u8 rename_allowed:1; /* Some objects cannot be renamed. */
7398 + u8 unlink_allowed:1;
7399 + u8 dirty:1; /* the object needs to be written to flash */
7400 + u8 valid:1; /* When the file system is being loaded up, this
7401 + * object might be created before the data
7402 + * is available
7403 + * ie. file data chunks encountered before
7404 + * the header.
7405 + */
7406 + u8 lazy_loaded:1; /* This object has been lazy loaded and
7407 + * is missing some detail */
7409 + u8 defered_free:1; /* Object is removed from NAND, but is
7410 + * still in the inode cache.
7411 + * Free of object is defered.
7412 + * until the inode is released.
7413 + */
7414 + u8 being_created:1; /* This object is still being created
7415 + * so skip some verification checks. */
7416 + u8 is_shadowed:1; /* This object is shadowed on the way
7417 + * to being renamed. */
7419 + u8 xattr_known:1; /* We know if this has object has xattribs
7420 + * or not. */
7421 + u8 has_xattr:1; /* This object has xattribs.
7422 + * Only valid if xattr_known. */
7424 + u8 serial; /* serial number of chunk in NAND.*/
7425 + u16 sum; /* sum of the name to speed searching */
7427 + struct yaffs_dev *my_dev; /* The device I'm on */
7429 + struct list_head hash_link; /* list of objects in hash bucket */
7431 + struct list_head hard_links; /* hard linked object chain*/
7433 + /* directory structure stuff */
7434 + /* also used for linking up the free list */
7435 + struct yaffs_obj *parent;
7436 + struct list_head siblings;
7438 + /* Where's my object header in NAND? */
7439 + int hdr_chunk;
7441 + int n_data_chunks; /* Number of data chunks for this file. */
7443 + u32 obj_id; /* the object id value */
7445 + u32 yst_mode;
7447 + YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
7449 +#ifdef CONFIG_YAFFS_WINCE
7450 + u32 win_ctime[2];
7451 + u32 win_mtime[2];
7452 + u32 win_atime[2];
7453 +#else
7454 + u32 yst_uid;
7455 + u32 yst_gid;
7456 + u32 yst_atime;
7457 + u32 yst_mtime;
7458 + u32 yst_ctime;
7459 +#endif
7461 + u32 yst_rdev;
7463 + void *my_inode;
7465 + enum yaffs_obj_type variant_type;
7467 + union yaffs_obj_var variant;
7471 +struct yaffs_obj_bucket {
7472 + struct list_head list;
7473 + int count;
7476 +/* yaffs_checkpt_obj holds the definition of an object as dumped
7477 + * by checkpointing.
7478 + */
7480 +struct yaffs_checkpt_obj {
7481 + int struct_type;
7482 + u32 obj_id;
7483 + u32 parent_id;
7484 + int hdr_chunk;
7485 + enum yaffs_obj_type variant_type:3;
7486 + u8 deleted:1;
7487 + u8 soft_del:1;
7488 + u8 unlinked:1;
7489 + u8 fake:1;
7490 + u8 rename_allowed:1;
7491 + u8 unlink_allowed:1;
7492 + u8 serial;
7493 + int n_data_chunks;
7494 + loff_t size_or_equiv_obj;
7497 +/*--------------------- Temporary buffers ----------------
7499 + * These are chunk-sized working buffers. Each device has a few.
7500 + */
7502 +struct yaffs_buffer {
7503 + u8 *buffer;
7504 + int in_use;
7507 +/*----------------- Device ---------------------------------*/
7509 +struct yaffs_param {
7510 + const YCHAR *name;
7512 + /*
7513 + * Entry parameters set up way early. Yaffs sets up the rest.
7514 + * The structure should be zeroed out before use so that unused
7515 + * and default values are zero.
7516 + */
7518 + int inband_tags; /* Use unband tags */
7519 + u32 total_bytes_per_chunk; /* Should be >= 512, does not need to
7520 + be a power of 2 */
7521 + int chunks_per_block; /* does not need to be a power of 2 */
7522 + int spare_bytes_per_chunk; /* spare area size */
7523 + int start_block; /* Start block we're allowed to use */
7524 + int end_block; /* End block we're allowed to use */
7525 + int n_reserved_blocks; /* Tuneable so that we can reduce
7526 + * reserved blocks on NOR and RAM. */
7528 + int n_caches; /* If <= 0, then short op caching is disabled,
7529 + * else the number of short op caches.
7530 + */
7531 + int cache_bypass_aligned; /* If non-zero then bypass the cache for
7532 + * aligned writes.
7533 + */
7535 + int use_nand_ecc; /* Flag to decide whether or not to use
7536 + * NAND driver ECC on data (yaffs1) */
7537 + int tags_9bytes; /* Use 9 byte tags */
7538 + int no_tags_ecc; /* Flag to decide whether or not to do ECC
7539 + * on packed tags (yaffs2) */
7541 + int is_yaffs2; /* Use yaffs2 mode on this device */
7543 + int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
7545 + int refresh_period; /* How often to check for a block refresh */
7547 + /* Checkpoint control. Can be set before or after initialisation */
7548 + u8 skip_checkpt_rd;
7549 + u8 skip_checkpt_wr;
7551 + int enable_xattr; /* Enable xattribs */
7553 + int max_objects; /*
7554 + * Set to limit the number of objects created.
7555 + * 0 = no limit.
7556 + */
7558 + /* The remove_obj_fn function must be supplied by OS flavours that
7559 + * need it.
7560 + * yaffs direct uses it to implement the faster readdir.
7561 + * Linux uses it to protect the directory during unlocking.
7562 + */
7563 + void (*remove_obj_fn) (struct yaffs_obj *obj);
7565 + /* Callback to mark the superblock dirty */
7566 + void (*sb_dirty_fn) (struct yaffs_dev *dev);
7568 + /* Callback to control garbage collection. */
7569 + unsigned (*gc_control_fn) (struct yaffs_dev *dev);
7571 + /* Debug control flags. Don't use unless you know what you're doing */
7572 + int use_header_file_size; /* Flag to determine if we should use
7573 + * file sizes from the header */
7574 + int disable_lazy_load; /* Disable lazy loading on this device */
7575 + int wide_tnodes_disabled; /* Set to disable wide tnodes */
7576 + int disable_soft_del; /* yaffs 1 only: Set to disable the use of
7577 + * softdeletion. */
7579 + int defered_dir_update; /* Set to defer directory updates */
7581 +#ifdef CONFIG_YAFFS_AUTO_UNICODE
7582 + int auto_unicode;
7583 +#endif
7584 + int always_check_erased; /* Force chunk erased check always on */
7586 + int disable_summary;
7587 + int disable_bad_block_marking;
7591 +struct yaffs_driver {
7592 + int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
7593 + const u8 *data, int data_len,
7594 + const u8 *oob, int oob_len);
7595 + int (*drv_read_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
7596 + u8 *data, int data_len,
7597 + u8 *oob, int oob_len,
7598 + enum yaffs_ecc_result *ecc_result);
7599 + int (*drv_erase_fn) (struct yaffs_dev *dev, int block_no);
7600 + int (*drv_mark_bad_fn) (struct yaffs_dev *dev, int block_no);
7601 + int (*drv_check_bad_fn) (struct yaffs_dev *dev, int block_no);
7602 + int (*drv_initialise_fn) (struct yaffs_dev *dev);
7603 + int (*drv_deinitialise_fn) (struct yaffs_dev *dev);
7606 +struct yaffs_tags_handler {
7607 + int (*write_chunk_tags_fn) (struct yaffs_dev *dev,
7608 + int nand_chunk, const u8 *data,
7609 + const struct yaffs_ext_tags *tags);
7610 + int (*read_chunk_tags_fn) (struct yaffs_dev *dev,
7611 + int nand_chunk, u8 *data,
7612 + struct yaffs_ext_tags *tags);
7614 + int (*query_block_fn) (struct yaffs_dev *dev, int block_no,
7615 + enum yaffs_block_state *state,
7616 + u32 *seq_number);
7617 + int (*mark_bad_fn) (struct yaffs_dev *dev, int block_no);
7620 +struct yaffs_dev {
7621 + struct yaffs_param param;
7622 + struct yaffs_driver drv;
7623 + struct yaffs_tags_handler tagger;
7625 + /* Context storage. Holds extra OS specific data for this device */
7627 + void *os_context;
7628 + void *driver_context;
7630 + struct list_head dev_list;
7632 + int ll_init;
7633 + /* Runtime parameters. Set up by YAFFS. */
7634 + int data_bytes_per_chunk;
7636 + /* Non-wide tnode stuff */
7637 + u16 chunk_grp_bits; /* Number of bits that need to be resolved if
7638 + * the tnodes are not wide enough.
7639 + */
7640 + u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
7642 + /* Stuff to support wide tnodes */
7643 + u32 tnode_width;
7644 + u32 tnode_mask;
7645 + u32 tnode_size;
7647 + /* Stuff for figuring out file offset to chunk conversions */
7648 + u32 chunk_shift; /* Shift value */
7649 + u32 chunk_div; /* Divisor after shifting: 1 for 2^n sizes */
7650 + u32 chunk_mask; /* Mask to use for power-of-2 case */
7652 + int is_mounted;
7653 + int read_only;
7654 + int is_checkpointed;
7656 + /* Stuff to support block offsetting to support start block zero */
7657 + int internal_start_block;
7658 + int internal_end_block;
7659 + int block_offset;
7660 + int chunk_offset;
7662 + /* Runtime checkpointing stuff */
7663 + int checkpt_page_seq; /* running sequence number of checkpt pages */
7664 + int checkpt_byte_count;
7665 + int checkpt_byte_offs;
7666 + u8 *checkpt_buffer;
7667 + int checkpt_open_write;
7668 + int blocks_in_checkpt;
7669 + int checkpt_cur_chunk;
7670 + int checkpt_cur_block;
7671 + int checkpt_next_block;
7672 + int *checkpt_block_list;
7673 + int checkpt_max_blocks;
7674 + u32 checkpt_sum;
7675 + u32 checkpt_xor;
7677 + int checkpoint_blocks_required; /* Number of blocks needed to store
7678 + * current checkpoint set */
7680 + /* Block Info */
7681 + struct yaffs_block_info *block_info;
7682 + u8 *chunk_bits; /* bitmap of chunks in use */
7683 + u8 block_info_alt:1; /* allocated using alternative alloc */
7684 + u8 chunk_bits_alt:1; /* allocated using alternative alloc */
7685 + int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
7686 + * Must be consistent with chunks_per_block.
7687 + */
7689 + int n_erased_blocks;
7690 + int alloc_block; /* Current block being allocated off */
7691 + u32 alloc_page;
7692 + int alloc_block_finder; /* Used to search for next allocation block */
7694 + /* Object and Tnode memory management */
7695 + void *allocator;
7696 + int n_obj;
7697 + int n_tnodes;
7699 + int n_hardlinks;
7701 + struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
7702 + u32 bucket_finder;
7704 + int n_free_chunks;
7706 + /* Garbage collection control */
7707 + u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
7708 + u32 n_clean_ups;
7710 + unsigned has_pending_prioritised_gc; /* We think this device might
7711 + have pending prioritised gcs */
7712 + unsigned gc_disable;
7713 + unsigned gc_block_finder;
7714 + unsigned gc_dirtiest;
7715 + unsigned gc_pages_in_use;
7716 + unsigned gc_not_done;
7717 + unsigned gc_block;
7718 + unsigned gc_chunk;
7719 + unsigned gc_skip;
7720 + struct yaffs_summary_tags *gc_sum_tags;
7722 + /* Special directories */
7723 + struct yaffs_obj *root_dir;
7724 + struct yaffs_obj *lost_n_found;
7726 + int buffered_block; /* Which block is buffered here? */
7727 + int doing_buffered_block_rewrite;
7729 + struct yaffs_cache *cache;
7730 + int cache_last_use;
7732 + /* Stuff for background deletion and unlinked files. */
7733 + struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted
7734 + files live. */
7735 + struct yaffs_obj *del_dir; /* Directory where deleted objects are
7736 + sent to disappear. */
7737 + struct yaffs_obj *unlinked_deletion; /* Current file being
7738 + background deleted. */
7739 + int n_deleted_files; /* Count of files awaiting deletion; */
7740 + int n_unlinked_files; /* Count of unlinked files. */
7741 + int n_bg_deletions; /* Count of background deletions. */
7743 + /* Temporary buffer management */
7744 + struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
7745 + int max_temp;
7746 + int temp_in_use;
7747 + int unmanaged_buffer_allocs;
7748 + int unmanaged_buffer_deallocs;
7750 + /* yaffs2 runtime stuff */
7751 + unsigned seq_number; /* Sequence number of currently
7752 + allocating block */
7753 + unsigned oldest_dirty_seq;
7754 + unsigned oldest_dirty_block;
7756 + /* Block refreshing */
7757 + int refresh_skip; /* A skip down counter.
7758 + * Refresh happens when this gets to zero. */
7760 + /* Dirty directory handling */
7761 + struct list_head dirty_dirs; /* List of dirty directories */
7763 + /* Summary */
7764 + int chunks_per_summary;
7765 + struct yaffs_summary_tags *sum_tags;
7767 + /* Statistics */
7768 + u32 n_page_writes;
7769 + u32 n_page_reads;
7770 + u32 n_erasures;
7771 + u32 n_bad_queries;
7772 + u32 n_bad_markings;
7773 + u32 n_erase_failures;
7774 + u32 n_gc_copies;
7775 + u32 all_gcs;
7776 + u32 passive_gc_count;
7777 + u32 oldest_dirty_gc_count;
7778 + u32 n_gc_blocks;
7779 + u32 bg_gcs;
7780 + u32 n_retried_writes;
7781 + u32 n_retired_blocks;
7782 + u32 n_ecc_fixed;
7783 + u32 n_ecc_unfixed;
7784 + u32 n_tags_ecc_fixed;
7785 + u32 n_tags_ecc_unfixed;
7786 + u32 n_deletions;
7787 + u32 n_unmarked_deletions;
7788 + u32 refresh_count;
7789 + u32 cache_hits;
7790 + u32 tags_used;
7791 + u32 summary_used;
7795 +/* The CheckpointDevice structure holds the device information that changes
7796 + *at runtime and must be preserved over unmount/mount cycles.
7797 + */
7798 +struct yaffs_checkpt_dev {
7799 + int struct_type;
7800 + int n_erased_blocks;
7801 + int alloc_block; /* Current block being allocated off */
7802 + u32 alloc_page;
7803 + int n_free_chunks;
7805 + int n_deleted_files; /* Count of files awaiting deletion; */
7806 + int n_unlinked_files; /* Count of unlinked files. */
7807 + int n_bg_deletions; /* Count of background deletions. */
7809 + /* yaffs2 runtime stuff */
7810 + unsigned seq_number; /* Sequence number of currently
7811 + * allocating block */
7815 +struct yaffs_checkpt_validity {
7816 + int struct_type;
7817 + u32 magic;
7818 + u32 version;
7819 + u32 head;
7822 +struct yaffs_shadow_fixer {
7823 + int obj_id;
7824 + int shadowed_id;
7825 + struct yaffs_shadow_fixer *next;
7828 +/* Structure for doing xattr modifications */
7829 +struct yaffs_xattr_mod {
7830 + int set; /* If 0 then this is a deletion */
7831 + const YCHAR *name;
7832 + const void *data;
7833 + int size;
7834 + int flags;
7835 + int result;
7838 +/*----------------------- YAFFS Functions -----------------------*/
7840 +int yaffs_guts_initialise(struct yaffs_dev *dev);
7841 +void yaffs_deinitialise(struct yaffs_dev *dev);
7843 +int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
7845 +int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
7846 + struct yaffs_obj *new_dir, const YCHAR * new_name);
7848 +int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
7849 +int yaffs_del_obj(struct yaffs_obj *obj);
7850 +struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
7851 + enum yaffs_obj_type type);
7854 +int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
7855 +loff_t yaffs_get_obj_length(struct yaffs_obj *obj);
7856 +int yaffs_get_obj_inode(struct yaffs_obj *obj);
7857 +unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
7858 +int yaffs_get_obj_link_count(struct yaffs_obj *obj);
7860 +/* File operations */
7861 +int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
7862 + int n_bytes);
7863 +int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
7864 + int n_bytes, int write_trhrough);
7865 +int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
7867 +struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
7868 + const YCHAR *name, u32 mode, u32 uid,
7869 + u32 gid);
7871 +int yaffs_flush_file(struct yaffs_obj *obj, int update_time, int data_sync);
7873 +/* Flushing and checkpointing */
7874 +void yaffs_flush_whole_cache(struct yaffs_dev *dev);
7876 +int yaffs_checkpoint_save(struct yaffs_dev *dev);
7877 +int yaffs_checkpoint_restore(struct yaffs_dev *dev);
7879 +/* Directory operations */
7880 +struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
7881 + u32 mode, u32 uid, u32 gid);
7882 +struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
7883 + const YCHAR *name);
7884 +struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
7886 +/* Link operations */
7887 +struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR *name,
7888 + struct yaffs_obj *equiv_obj);
7890 +struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
7892 +/* Symlink operations */
7893 +struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
7894 + const YCHAR *name, u32 mode, u32 uid,
7895 + u32 gid, const YCHAR *alias);
7896 +YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
7898 +/* Special inodes (fifos, sockets and devices) */
7899 +struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
7900 + const YCHAR *name, u32 mode, u32 uid,
7901 + u32 gid, u32 rdev);
7903 +int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR *name,
7904 + const void *value, int size, int flags);
7905 +int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR *name, void *value,
7906 + int size);
7907 +int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
7908 +int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR *name);
7910 +/* Special directories */
7911 +struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
7912 +struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
7914 +void yaffs_handle_defered_free(struct yaffs_obj *obj);
7916 +void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
7918 +int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
7920 +/* Debug dump */
7921 +int yaffs_dump_obj(struct yaffs_obj *obj);
7923 +void yaffs_guts_test(struct yaffs_dev *dev);
7924 +int yaffs_guts_ll_init(struct yaffs_dev *dev);
7927 +/* A few useful functions to be used within the core files*/
7928 +void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
7929 + int lyn);
7930 +int yaffs_check_ff(u8 *buffer, int n_bytes);
7931 +void yaffs_handle_chunk_error(struct yaffs_dev *dev,
7932 + struct yaffs_block_info *bi);
7934 +u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev);
7935 +void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer);
7937 +struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
7938 + int number,
7939 + enum yaffs_obj_type type);
7940 +int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
7941 + int nand_chunk, int in_scan);
7942 +void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR *name);
7943 +void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
7944 + const struct yaffs_obj_hdr *oh);
7945 +void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
7946 +YCHAR *yaffs_clone_str(const YCHAR *str);
7947 +void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list);
7948 +void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
7949 +int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name,
7950 + int force, int is_shrink, int shadows,
7951 + struct yaffs_xattr_mod *xop);
7952 +void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
7953 + int backward_scanning);
7954 +int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
7955 +struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
7956 +struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
7957 + struct yaffs_file_var *file_struct,
7958 + u32 chunk_id,
7959 + struct yaffs_tnode *passed_tn);
7961 +int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
7962 + int n_bytes, int write_trhrough);
7963 +void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
7964 +void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
7966 +int yaffs_count_free_chunks(struct yaffs_dev *dev);
7968 +struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
7969 + struct yaffs_file_var *file_struct,
7970 + u32 chunk_id);
7972 +u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
7973 + unsigned pos);
7975 +int yaffs_is_non_empty_dir(struct yaffs_obj *obj);
7977 +int yaffs_guts_format_dev(struct yaffs_dev *dev);
7979 +void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
7980 + int *chunk_out, u32 *offset_out);
7982 + * Marshalling functions to get loff_t file sizes into aand out of
7983 + * object headers.
7984 + */
7985 +void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize);
7986 +loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh);
7987 +loff_t yaffs_max_file_size(struct yaffs_dev *dev);
7990 + * Debug function to count number of blocks in each state
7991 + * NB Needs to be called with correct number of integers
7992 + */
7994 +void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10]);
7996 +int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
7997 + struct yaffs_ext_tags *tags);
7999 +#endif
8000 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_linux.h linux-3.12.20/fs/yaffs2/yaffs_linux.h
8001 --- linux-3.12.20.orig/fs/yaffs2/yaffs_linux.h 1970-01-01 01:00:00.000000000 +0100
8002 +++ linux-3.12.20/fs/yaffs2/yaffs_linux.h 2014-05-17 11:28:49.000000000 +0200
8003 @@ -0,0 +1,48 @@
8005 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
8007 + * Copyright (C) 2002-2011 Aleph One Ltd.
8008 + * for Toby Churchill Ltd and Brightstar Engineering
8010 + * Created by Charles Manning <charles@aleph1.co.uk>
8012 + * This program is free software; you can redistribute it and/or modify
8013 + * it under the terms of the GNU Lesser General Public License version 2.1 as
8014 + * published by the Free Software Foundation.
8016 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
8017 + */
8019 +#ifndef __YAFFS_LINUX_H__
8020 +#define __YAFFS_LINUX_H__
8022 +#include "yportenv.h"
8024 +struct yaffs_linux_context {
8025 + struct list_head context_list; /* List of these we have mounted */
8026 + struct yaffs_dev *dev;
8027 + struct super_block *super;
8028 + struct task_struct *bg_thread; /* Background thread for this device */
8029 + int bg_running;
8030 + struct mutex gross_lock; /* Gross locking mutex*/
8031 + u8 *spare_buffer; /* For mtdif2 use. Don't know the buffer size
8032 + * at compile time so we have to allocate it.
8033 + */
8034 + struct list_head search_contexts;
8035 + struct task_struct *readdir_process;
8036 + unsigned mount_id;
8037 + int dirty;
8040 +#define yaffs_dev_to_lc(dev) ((struct yaffs_linux_context *)((dev)->os_context))
8041 +#define yaffs_dev_to_mtd(dev) ((struct mtd_info *)((dev)->driver_context))
8043 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
8044 +#define WRITE_SIZE_STR "writesize"
8045 +#define WRITE_SIZE(mtd) ((mtd)->writesize)
8046 +#else
8047 +#define WRITE_SIZE_STR "oobblock"
8048 +#define WRITE_SIZE(mtd) ((mtd)->oobblock)
8049 +#endif
8051 +#endif
8052 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_mtdif.c linux-3.12.20/fs/yaffs2/yaffs_mtdif.c
8053 --- linux-3.12.20.orig/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
8054 +++ linux-3.12.20/fs/yaffs2/yaffs_mtdif.c 2014-05-17 11:28:49.000000000 +0200
8055 @@ -0,0 +1,309 @@
8057 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
8059 + * Copyright (C) 2002-2011 Aleph One Ltd.
8060 + * for Toby Churchill Ltd and Brightstar Engineering
8062 + * Created by Charles Manning <charles@aleph1.co.uk>
8064 + * This program is free software; you can redistribute it and/or modify
8065 + * it under the terms of the GNU General Public License version 2 as
8066 + * published by the Free Software Foundation.
8067 + */
8069 +#include "yportenv.h"
8071 +#include "yaffs_mtdif.h"
8073 +#include "linux/mtd/mtd.h"
8074 +#include "linux/types.h"
8075 +#include "linux/time.h"
8076 +#include "linux/major.h"
8077 +#include "linux/mtd/nand.h"
8078 +#include "linux/kernel.h"
8079 +#include "linux/version.h"
8080 +#include "linux/types.h"
8082 +#include "yaffs_trace.h"
8083 +#include "yaffs_guts.h"
8084 +#include "yaffs_linux.h"
8087 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
8088 +#define MTD_OPS_AUTO_OOB MTD_OOB_AUTO
8089 +#endif
8092 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
8093 +#define mtd_erase(m, ei) (m)->erase(m, ei)
8094 +#define mtd_write_oob(m, addr, pops) (m)->write_oob(m, addr, pops)
8095 +#define mtd_read_oob(m, addr, pops) (m)->read_oob(m, addr, pops)
8096 +#define mtd_block_isbad(m, offs) (m)->block_isbad(m, offs)
8097 +#define mtd_block_markbad(m, offs) (m)->block_markbad(m, offs)
8098 +#endif
8102 +int nandmtd_erase_block(struct yaffs_dev *dev, int block_no)
8104 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8105 + u32 addr =
8106 + ((loff_t) block_no) * dev->param.total_bytes_per_chunk *
8107 + dev->param.chunks_per_block;
8108 + struct erase_info ei;
8109 + int retval = 0;
8111 + ei.mtd = mtd;
8112 + ei.addr = addr;
8113 + ei.len = dev->param.total_bytes_per_chunk * dev->param.chunks_per_block;
8114 + ei.time = 1000;
8115 + ei.retries = 2;
8116 + ei.callback = NULL;
8117 + ei.priv = (u_long) dev;
8119 + retval = mtd_erase(mtd, &ei);
8121 + if (retval == 0)
8122 + return YAFFS_OK;
8124 + return YAFFS_FAIL;
8128 +static int yaffs_mtd_write(struct yaffs_dev *dev, int nand_chunk,
8129 + const u8 *data, int data_len,
8130 + const u8 *oob, int oob_len)
8132 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8133 + loff_t addr;
8134 + struct mtd_oob_ops ops;
8135 + int retval;
8137 + yaffs_trace(YAFFS_TRACE_MTD,
8138 + "yaffs_mtd_write(%p, %d, %p, %d, %p, %d)\n",
8139 + dev, nand_chunk, data, data_len, oob, oob_len);
8141 + if (!data || !data_len) {
8142 + data = NULL;
8143 + data_len = 0;
8146 + if (!oob || !oob_len) {
8147 + oob = NULL;
8148 + oob_len = 0;
8151 + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
8152 + memset(&ops, 0, sizeof(ops));
8153 + ops.mode = MTD_OPS_AUTO_OOB;
8154 + ops.len = (data) ? data_len : 0;
8155 + ops.ooblen = oob_len;
8156 + ops.datbuf = (u8 *)data;
8157 + ops.oobbuf = (u8 *)oob;
8159 + retval = mtd_write_oob(mtd, addr, &ops);
8160 + if (retval) {
8161 + yaffs_trace(YAFFS_TRACE_MTD,
8162 + "write_oob failed, chunk %d, mtd error %d",
8163 + nand_chunk, retval);
8165 + return retval ? YAFFS_FAIL : YAFFS_OK;
8168 +static int yaffs_mtd_read(struct yaffs_dev *dev, int nand_chunk,
8169 + u8 *data, int data_len,
8170 + u8 *oob, int oob_len,
8171 + enum yaffs_ecc_result *ecc_result)
8173 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8174 + loff_t addr;
8175 + struct mtd_oob_ops ops;
8176 + int retval;
8178 + addr = ((loff_t) nand_chunk) * dev->param.total_bytes_per_chunk;
8179 + memset(&ops, 0, sizeof(ops));
8180 + ops.mode = MTD_OPS_AUTO_OOB;
8181 + ops.len = (data) ? data_len : 0;
8182 + ops.ooblen = oob_len;
8183 + ops.datbuf = data;
8184 + ops.oobbuf = oob;
8186 +#if (MTD_VERSION_CODE < MTD_VERSION(2, 6, 20))
8187 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
8188 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
8189 + */
8190 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
8191 +#endif
8192 + /* Read page and oob using MTD.
8193 + * Check status and determine ECC result.
8194 + */
8195 + retval = mtd_read_oob(mtd, addr, &ops);
8196 + if (retval)
8197 + yaffs_trace(YAFFS_TRACE_MTD,
8198 + "read_oob failed, chunk %d, mtd error %d",
8199 + nand_chunk, retval);
8201 + switch (retval) {
8202 + case 0:
8203 + /* no error */
8204 + if(ecc_result)
8205 + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
8206 + break;
8208 + case -EUCLEAN:
8209 + /* MTD's ECC fixed the data */
8210 + if(ecc_result)
8211 + *ecc_result = YAFFS_ECC_RESULT_FIXED;
8212 + dev->n_ecc_fixed++;
8213 + break;
8215 + case -EBADMSG:
8216 + default:
8217 + /* MTD's ECC could not fix the data */
8218 + dev->n_ecc_unfixed++;
8219 + if(ecc_result)
8220 + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
8221 + return YAFFS_FAIL;
8224 + return YAFFS_OK;
8227 +static int yaffs_mtd_erase(struct yaffs_dev *dev, int block_no)
8229 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8231 + loff_t addr;
8232 + struct erase_info ei;
8233 + int retval = 0;
8234 + u32 block_size;
8236 + block_size = dev->param.total_bytes_per_chunk *
8237 + dev->param.chunks_per_block;
8238 + addr = ((loff_t) block_no) * block_size;
8240 + ei.mtd = mtd;
8241 + ei.addr = addr;
8242 + ei.len = block_size;
8243 + ei.time = 1000;
8244 + ei.retries = 2;
8245 + ei.callback = NULL;
8246 + ei.priv = (u_long) dev;
8248 + retval = mtd_erase(mtd, &ei);
8250 + if (retval == 0)
8251 + return YAFFS_OK;
8253 + return YAFFS_FAIL;
8256 +static int yaffs_mtd_mark_bad(struct yaffs_dev *dev, int block_no)
8258 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8259 + int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
8260 + int retval;
8262 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", block_no);
8264 + retval = mtd_block_markbad(mtd, (loff_t) blocksize * block_no);
8265 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
8268 +static int yaffs_mtd_check_bad(struct yaffs_dev *dev, int block_no)
8270 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
8271 + int blocksize = dev->param.chunks_per_block * dev->param.total_bytes_per_chunk;
8272 + int retval;
8274 + yaffs_trace(YAFFS_TRACE_MTD, "checking block %d bad", block_no);
8276 + retval = mtd_block_isbad(mtd, (loff_t) blocksize * block_no);
8277 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
8280 +static int yaffs_mtd_initialise(struct yaffs_dev *dev)
8282 + return YAFFS_OK;
8285 +static int yaffs_mtd_deinitialise(struct yaffs_dev *dev)
8287 + return YAFFS_OK;
8291 +void yaffs_mtd_drv_install(struct yaffs_dev *dev)
8293 + struct yaffs_driver *drv = &dev->drv;
8295 + drv->drv_write_chunk_fn = yaffs_mtd_write;
8296 + drv->drv_read_chunk_fn = yaffs_mtd_read;
8297 + drv->drv_erase_fn = yaffs_mtd_erase;
8298 + drv->drv_mark_bad_fn = yaffs_mtd_mark_bad;
8299 + drv->drv_check_bad_fn = yaffs_mtd_check_bad;
8300 + drv->drv_initialise_fn = yaffs_mtd_initialise;
8301 + drv->drv_deinitialise_fn = yaffs_mtd_deinitialise;
8305 +struct mtd_info * yaffs_get_mtd_device(dev_t sdev)
8307 + struct mtd_info *mtd;
8309 + mtd = yaffs_get_mtd_device(sdev);
8311 + /* Check it's an mtd device..... */
8312 + if (MAJOR(sdev) != MTD_BLOCK_MAJOR)
8313 + return NULL; /* This isn't an mtd device */
8315 + /* Check it's NAND */
8316 + if (mtd->type != MTD_NANDFLASH) {
8317 + yaffs_trace(YAFFS_TRACE_ALWAYS,
8318 + "yaffs: MTD device is not NAND it's type %d",
8319 + mtd->type);
8320 + return NULL;
8323 + yaffs_trace(YAFFS_TRACE_OS, " %s %d", WRITE_SIZE_STR, WRITE_SIZE(mtd));
8324 + yaffs_trace(YAFFS_TRACE_OS, " oobsize %d", mtd->oobsize);
8325 + yaffs_trace(YAFFS_TRACE_OS, " erasesize %d", mtd->erasesize);
8326 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
8327 + yaffs_trace(YAFFS_TRACE_OS, " size %u", mtd->size);
8328 +#else
8329 + yaffs_trace(YAFFS_TRACE_OS, " size %lld", mtd->size);
8330 +#endif
8332 + return mtd;
8335 +int yaffs_verify_mtd(struct mtd_info *mtd, int yaffs_version, int inband_tags)
8337 + if (yaffs_version == 2) {
8338 + if ((WRITE_SIZE(mtd) < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
8339 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) &&
8340 + !inband_tags) {
8341 + yaffs_trace(YAFFS_TRACE_ALWAYS,
8342 + "MTD device does not have the right page sizes"
8343 + );
8344 + return -1;
8346 + } else {
8347 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
8348 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
8349 + yaffs_trace(YAFFS_TRACE_ALWAYS,
8350 + "MTD device does not support have the right page sizes"
8351 + );
8352 + return -1;
8356 + return 0;
8360 +void yaffs_put_mtd_device(struct mtd_info *mtd)
8362 + if(mtd)
8363 + put_mtd_device(mtd);
8365 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_mtdif.h linux-3.12.20/fs/yaffs2/yaffs_mtdif.h
8366 --- linux-3.12.20.orig/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
8367 +++ linux-3.12.20/fs/yaffs2/yaffs_mtdif.h 2014-05-17 11:28:49.000000000 +0200
8368 @@ -0,0 +1,25 @@
8370 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
8372 + * Copyright (C) 2002-2011 Aleph One Ltd.
8373 + * for Toby Churchill Ltd and Brightstar Engineering
8375 + * Created by Charles Manning <charles@aleph1.co.uk>
8377 + * This program is free software; you can redistribute it and/or modify
8378 + * it under the terms of the GNU Lesser General Public License version 2.1 as
8379 + * published by the Free Software Foundation.
8381 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
8382 + */
8384 +#ifndef __YAFFS_MTDIF_H__
8385 +#define __YAFFS_MTDIF_H__
8387 +#include "yaffs_guts.h"
8389 +void yaffs_mtd_drv_install(struct yaffs_dev *dev);
8390 +struct mtd_info * yaffs_get_mtd_device(dev_t sdev);
8391 +void yaffs_put_mtd_device(struct mtd_info *mtd);
8392 +int yaffs_verify_mtd(struct mtd_info *mtd, int yaffs_version, int inband_tags);
8393 +#endif
8394 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_nameval.c linux-3.12.20/fs/yaffs2/yaffs_nameval.c
8395 --- linux-3.12.20.orig/fs/yaffs2/yaffs_nameval.c 1970-01-01 01:00:00.000000000 +0100
8396 +++ linux-3.12.20/fs/yaffs2/yaffs_nameval.c 2014-05-17 11:28:49.000000000 +0200
8397 @@ -0,0 +1,208 @@
8399 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
8401 + * Copyright (C) 2002-2011 Aleph One Ltd.
8402 + * for Toby Churchill Ltd and Brightstar Engineering
8404 + * Created by Charles Manning <charles@aleph1.co.uk>
8406 + * This program is free software; you can redistribute it and/or modify
8407 + * it under the terms of the GNU General Public License version 2 as
8408 + * published by the Free Software Foundation.
8409 + */
8412 + * This simple implementation of a name-value store assumes a small number of
8413 +* values and fits into a small finite buffer.
8415 + * Each attribute is stored as a record:
8416 + * sizeof(int) bytes record size.
8417 + * strnlen+1 bytes name null terminated.
8418 + * nbytes value.
8419 + * ----------
8420 + * total size stored in record size
8422 + * This code has not been tested with unicode yet.
8423 + */
8425 +#include "yaffs_nameval.h"
8427 +#include "yportenv.h"
8429 +static int nval_find(const char *xb, int xb_size, const YCHAR *name,
8430 + int *exist_size)
8432 + int pos = 0;
8433 + int size;
8435 + memcpy(&size, xb, sizeof(int));
8436 + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
8437 + if (!strncmp((YCHAR *) (xb + pos + sizeof(int)),
8438 + name, size)) {
8439 + if (exist_size)
8440 + *exist_size = size;
8441 + return pos;
8443 + pos += size;
8444 + if (pos < xb_size - sizeof(int))
8445 + memcpy(&size, xb + pos, sizeof(int));
8446 + else
8447 + size = 0;
8449 + if (exist_size)
8450 + *exist_size = 0;
8451 + return -ENODATA;
8454 +static int nval_used(const char *xb, int xb_size)
8456 + int pos = 0;
8457 + int size;
8459 + memcpy(&size, xb + pos, sizeof(int));
8460 + while (size > 0 && (size < xb_size) && (pos + size < xb_size)) {
8461 + pos += size;
8462 + if (pos < xb_size - sizeof(int))
8463 + memcpy(&size, xb + pos, sizeof(int));
8464 + else
8465 + size = 0;
8467 + return pos;
8470 +int nval_del(char *xb, int xb_size, const YCHAR *name)
8472 + int pos = nval_find(xb, xb_size, name, NULL);
8473 + int size;
8475 + if (pos < 0 || pos >= xb_size)
8476 + return -ENODATA;
8478 + /* Find size, shift rest over this record,
8479 + * then zero out the rest of buffer */
8480 + memcpy(&size, xb + pos, sizeof(int));
8481 + memcpy(xb + pos, xb + pos + size, xb_size - (pos + size));
8482 + memset(xb + (xb_size - size), 0, size);
8483 + return 0;
8486 +int nval_set(char *xb, int xb_size, const YCHAR *name, const char *buf,
8487 + int bsize, int flags)
8489 + int pos;
8490 + int namelen = strnlen(name, xb_size);
8491 + int reclen;
8492 + int size_exist = 0;
8493 + int space;
8494 + int start;
8496 + pos = nval_find(xb, xb_size, name, &size_exist);
8498 + if (flags & XATTR_CREATE && pos >= 0)
8499 + return -EEXIST;
8500 + if (flags & XATTR_REPLACE && pos < 0)
8501 + return -ENODATA;
8503 + start = nval_used(xb, xb_size);
8504 + space = xb_size - start + size_exist;
8506 + reclen = (sizeof(int) + namelen + 1 + bsize);
8508 + if (reclen > space)
8509 + return -ENOSPC;
8511 + if (pos >= 0) {
8512 + nval_del(xb, xb_size, name);
8513 + start = nval_used(xb, xb_size);
8516 + pos = start;
8518 + memcpy(xb + pos, &reclen, sizeof(int));
8519 + pos += sizeof(int);
8520 + strncpy((YCHAR *) (xb + pos), name, reclen);
8521 + pos += (namelen + 1);
8522 + memcpy(xb + pos, buf, bsize);
8523 + return 0;
8526 +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
8527 + int bsize)
8529 + int pos = nval_find(xb, xb_size, name, NULL);
8530 + int size;
8532 + if (pos >= 0 && pos < xb_size) {
8534 + memcpy(&size, xb + pos, sizeof(int));
8535 + pos += sizeof(int); /* advance past record length */
8536 + size -= sizeof(int);
8538 + /* Advance over name string */
8539 + while (xb[pos] && size > 0 && pos < xb_size) {
8540 + pos++;
8541 + size--;
8543 + /*Advance over NUL */
8544 + pos++;
8545 + size--;
8547 + /* If bsize is zero then this is a size query.
8548 + * Return the size, but don't copy.
8549 + */
8550 + if (!bsize)
8551 + return size;
8553 + if (size <= bsize) {
8554 + memcpy(buf, xb + pos, size);
8555 + return size;
8558 + if (pos >= 0)
8559 + return -ERANGE;
8561 + return -ENODATA;
8564 +int nval_list(const char *xb, int xb_size, char *buf, int bsize)
8566 + int pos = 0;
8567 + int size;
8568 + int name_len;
8569 + int ncopied = 0;
8570 + int filled = 0;
8572 + memcpy(&size, xb + pos, sizeof(int));
8573 + while (size > sizeof(int) &&
8574 + size <= xb_size &&
8575 + (pos + size) < xb_size &&
8576 + !filled) {
8577 + pos += sizeof(int);
8578 + size -= sizeof(int);
8579 + name_len = strnlen((YCHAR *) (xb + pos), size);
8580 + if (ncopied + name_len + 1 < bsize) {
8581 + memcpy(buf, xb + pos, name_len * sizeof(YCHAR));
8582 + buf += name_len;
8583 + *buf = '\0';
8584 + buf++;
8585 + if (sizeof(YCHAR) > 1) {
8586 + *buf = '\0';
8587 + buf++;
8589 + ncopied += (name_len + 1);
8590 + } else {
8591 + filled = 1;
8593 + pos += size;
8594 + if (pos < xb_size - sizeof(int))
8595 + memcpy(&size, xb + pos, sizeof(int));
8596 + else
8597 + size = 0;
8599 + return ncopied;
8602 +int nval_hasvalues(const char *xb, int xb_size)
8604 + return nval_used(xb, xb_size) > 0;
8606 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_nameval.h linux-3.12.20/fs/yaffs2/yaffs_nameval.h
8607 --- linux-3.12.20.orig/fs/yaffs2/yaffs_nameval.h 1970-01-01 01:00:00.000000000 +0100
8608 +++ linux-3.12.20/fs/yaffs2/yaffs_nameval.h 2014-05-17 11:28:49.000000000 +0200
8609 @@ -0,0 +1,28 @@
8611 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
8613 + * Copyright (C) 2002-2011 Aleph One Ltd.
8614 + * for Toby Churchill Ltd and Brightstar Engineering
8616 + * Created by Charles Manning <charles@aleph1.co.uk>
8618 + * This program is free software; you can redistribute it and/or modify
8619 + * it under the terms of the GNU Lesser General Public License version 2.1 as
8620 + * published by the Free Software Foundation.
8622 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
8623 + */
8625 +#ifndef __NAMEVAL_H__
8626 +#define __NAMEVAL_H__
8628 +#include "yportenv.h"
8630 +int nval_del(char *xb, int xb_size, const YCHAR * name);
8631 +int nval_set(char *xb, int xb_size, const YCHAR * name, const char *buf,
8632 + int bsize, int flags);
8633 +int nval_get(const char *xb, int xb_size, const YCHAR * name, char *buf,
8634 + int bsize);
8635 +int nval_list(const char *xb, int xb_size, char *buf, int bsize);
8636 +int nval_hasvalues(const char *xb, int xb_size);
8637 +#endif
8638 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_nand.c linux-3.12.20/fs/yaffs2/yaffs_nand.c
8639 --- linux-3.12.20.orig/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
8640 +++ linux-3.12.20/fs/yaffs2/yaffs_nand.c 2014-05-17 11:28:49.000000000 +0200
8641 @@ -0,0 +1,122 @@
8643 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
8645 + * Copyright (C) 2002-2011 Aleph One Ltd.
8646 + * for Toby Churchill Ltd and Brightstar Engineering
8648 + * Created by Charles Manning <charles@aleph1.co.uk>
8650 + * This program is free software; you can redistribute it and/or modify
8651 + * it under the terms of the GNU General Public License version 2 as
8652 + * published by the Free Software Foundation.
8653 + */
8655 +#include "yaffs_nand.h"
8656 +#include "yaffs_tagscompat.h"
8658 +#include "yaffs_getblockinfo.h"
8659 +#include "yaffs_summary.h"
8661 +static int apply_chunk_offset(struct yaffs_dev *dev, int chunk)
8663 + return chunk - dev->chunk_offset;
8666 +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
8667 + u8 *buffer, struct yaffs_ext_tags *tags)
8669 + int result;
8670 + struct yaffs_ext_tags local_tags;
8671 + int flash_chunk = apply_chunk_offset(dev, nand_chunk);
8673 + dev->n_page_reads++;
8675 + /* If there are no tags provided use local tags. */
8676 + if (!tags)
8677 + tags = &local_tags;
8679 + result = dev->tagger.read_chunk_tags_fn(dev, flash_chunk, buffer, tags);
8680 + if (tags && tags->ecc_result > YAFFS_ECC_RESULT_NO_ERROR) {
8682 + struct yaffs_block_info *bi;
8683 + bi = yaffs_get_block_info(dev,
8684 + nand_chunk /
8685 + dev->param.chunks_per_block);
8686 + yaffs_handle_chunk_error(dev, bi);
8688 + return result;
8691 +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
8692 + int nand_chunk,
8693 + const u8 *buffer, struct yaffs_ext_tags *tags)
8695 + int result;
8696 + int flash_chunk = apply_chunk_offset(dev, nand_chunk);
8698 + dev->n_page_writes++;
8700 + if (!tags) {
8701 + yaffs_trace(YAFFS_TRACE_ERROR, "Writing with no tags");
8702 + BUG();
8703 + return YAFFS_FAIL;
8706 + tags->seq_number = dev->seq_number;
8707 + tags->chunk_used = 1;
8708 + yaffs_trace(YAFFS_TRACE_WRITE,
8709 + "Writing chunk %d tags %d %d",
8710 + nand_chunk, tags->obj_id, tags->chunk_id);
8712 + result = dev->tagger.write_chunk_tags_fn(dev, flash_chunk,
8713 + buffer, tags);
8715 + yaffs_summary_add(dev, tags, nand_chunk);
8717 + return result;
8720 +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no)
8722 + block_no -= dev->block_offset;
8723 + dev->n_bad_markings++;
8725 + if (dev->param.disable_bad_block_marking)
8726 + return YAFFS_OK;
8728 + return dev->tagger.mark_bad_fn(dev, block_no);
8732 +int yaffs_query_init_block_state(struct yaffs_dev *dev,
8733 + int block_no,
8734 + enum yaffs_block_state *state,
8735 + u32 *seq_number)
8737 + block_no -= dev->block_offset;
8738 + return dev->tagger.query_block_fn(dev, block_no, state, seq_number);
8741 +int yaffs_erase_block(struct yaffs_dev *dev, int block_no)
8743 + int result;
8745 + block_no -= dev->block_offset;
8746 + dev->n_erasures++;
8747 + result = dev->drv.drv_erase_fn(dev, block_no);
8748 + return result;
8751 +int yaffs_init_nand(struct yaffs_dev *dev)
8753 + if (dev->drv.drv_initialise_fn)
8754 + return dev->drv.drv_initialise_fn(dev);
8755 + return YAFFS_OK;
8758 +int yaffs_deinit_nand(struct yaffs_dev *dev)
8760 + if (dev->drv.drv_deinitialise_fn)
8761 + return dev->drv.drv_deinitialise_fn(dev);
8762 + return YAFFS_OK;
8764 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_nand.h linux-3.12.20/fs/yaffs2/yaffs_nand.h
8765 --- linux-3.12.20.orig/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
8766 +++ linux-3.12.20/fs/yaffs2/yaffs_nand.h 2014-05-17 11:28:49.000000000 +0200
8767 @@ -0,0 +1,39 @@
8769 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
8771 + * Copyright (C) 2002-2011 Aleph One Ltd.
8772 + * for Toby Churchill Ltd and Brightstar Engineering
8774 + * Created by Charles Manning <charles@aleph1.co.uk>
8776 + * This program is free software; you can redistribute it and/or modify
8777 + * it under the terms of the GNU Lesser General Public License version 2.1 as
8778 + * published by the Free Software Foundation.
8780 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
8781 + */
8783 +#ifndef __YAFFS_NAND_H__
8784 +#define __YAFFS_NAND_H__
8785 +#include "yaffs_guts.h"
8787 +int yaffs_rd_chunk_tags_nand(struct yaffs_dev *dev, int nand_chunk,
8788 + u8 *buffer, struct yaffs_ext_tags *tags);
8790 +int yaffs_wr_chunk_tags_nand(struct yaffs_dev *dev,
8791 + int nand_chunk,
8792 + const u8 *buffer, struct yaffs_ext_tags *tags);
8794 +int yaffs_mark_bad(struct yaffs_dev *dev, int block_no);
8796 +int yaffs_query_init_block_state(struct yaffs_dev *dev,
8797 + int block_no,
8798 + enum yaffs_block_state *state,
8799 + unsigned *seq_number);
8801 +int yaffs_erase_block(struct yaffs_dev *dev, int flash_block);
8803 +int yaffs_init_nand(struct yaffs_dev *dev);
8804 +int yaffs_deinit_nand(struct yaffs_dev *dev);
8806 +#endif
8807 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags1.c linux-3.12.20/fs/yaffs2/yaffs_packedtags1.c
8808 --- linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
8809 +++ linux-3.12.20/fs/yaffs2/yaffs_packedtags1.c 2014-05-17 11:28:49.000000000 +0200
8810 @@ -0,0 +1,56 @@
8812 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
8814 + * Copyright (C) 2002-2011 Aleph One Ltd.
8815 + * for Toby Churchill Ltd and Brightstar Engineering
8817 + * Created by Charles Manning <charles@aleph1.co.uk>
8819 + * This program is free software; you can redistribute it and/or modify
8820 + * it under the terms of the GNU General Public License version 2 as
8821 + * published by the Free Software Foundation.
8822 + */
8824 +#include "yaffs_packedtags1.h"
8825 +#include "yportenv.h"
8827 +static const u8 all_ff[20] = {
8828 + 0xff, 0xff, 0xff, 0xff,
8829 + 0xff, 0xff, 0xff, 0xff,
8830 + 0xff, 0xff, 0xff, 0xff,
8831 + 0xff, 0xff, 0xff, 0xff,
8832 + 0xff, 0xff, 0xff, 0xff
8835 +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
8836 + const struct yaffs_ext_tags *t)
8838 + pt->chunk_id = t->chunk_id;
8839 + pt->serial_number = t->serial_number;
8840 + pt->n_bytes = t->n_bytes;
8841 + pt->obj_id = t->obj_id;
8842 + pt->ecc = 0;
8843 + pt->deleted = (t->is_deleted) ? 0 : 1;
8844 + pt->unused_stuff = 0;
8845 + pt->should_be_ff = 0xffffffff;
8848 +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
8849 + const struct yaffs_packed_tags1 *pt)
8852 + if (memcmp(all_ff, pt, sizeof(struct yaffs_packed_tags1))) {
8853 + t->block_bad = 0;
8854 + if (pt->should_be_ff != 0xffffffff)
8855 + t->block_bad = 1;
8856 + t->chunk_used = 1;
8857 + t->obj_id = pt->obj_id;
8858 + t->chunk_id = pt->chunk_id;
8859 + t->n_bytes = pt->n_bytes;
8860 + t->ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
8861 + t->is_deleted = (pt->deleted) ? 0 : 1;
8862 + t->serial_number = pt->serial_number;
8863 + } else {
8864 + memset(t, 0, sizeof(struct yaffs_ext_tags));
8867 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags1.h linux-3.12.20/fs/yaffs2/yaffs_packedtags1.h
8868 --- linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
8869 +++ linux-3.12.20/fs/yaffs2/yaffs_packedtags1.h 2014-05-17 11:28:49.000000000 +0200
8870 @@ -0,0 +1,39 @@
8872 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
8874 + * Copyright (C) 2002-2011 Aleph One Ltd.
8875 + * for Toby Churchill Ltd and Brightstar Engineering
8877 + * Created by Charles Manning <charles@aleph1.co.uk>
8879 + * This program is free software; you can redistribute it and/or modify
8880 + * it under the terms of the GNU Lesser General Public License version 2.1 as
8881 + * published by the Free Software Foundation.
8883 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
8884 + */
8886 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
8888 +#ifndef __YAFFS_PACKEDTAGS1_H__
8889 +#define __YAFFS_PACKEDTAGS1_H__
8891 +#include "yaffs_guts.h"
8893 +struct yaffs_packed_tags1 {
8894 + u32 chunk_id:20;
8895 + u32 serial_number:2;
8896 + u32 n_bytes:10;
8897 + u32 obj_id:18;
8898 + u32 ecc:12;
8899 + u32 deleted:1;
8900 + u32 unused_stuff:1;
8901 + unsigned should_be_ff;
8905 +void yaffs_pack_tags1(struct yaffs_packed_tags1 *pt,
8906 + const struct yaffs_ext_tags *t);
8907 +void yaffs_unpack_tags1(struct yaffs_ext_tags *t,
8908 + const struct yaffs_packed_tags1 *pt);
8909 +#endif
8910 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags2.c linux-3.12.20/fs/yaffs2/yaffs_packedtags2.c
8911 --- linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
8912 +++ linux-3.12.20/fs/yaffs2/yaffs_packedtags2.c 2014-05-17 11:28:49.000000000 +0200
8913 @@ -0,0 +1,197 @@
8915 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
8917 + * Copyright (C) 2002-2011 Aleph One Ltd.
8918 + * for Toby Churchill Ltd and Brightstar Engineering
8920 + * Created by Charles Manning <charles@aleph1.co.uk>
8922 + * This program is free software; you can redistribute it and/or modify
8923 + * it under the terms of the GNU General Public License version 2 as
8924 + * published by the Free Software Foundation.
8925 + */
8927 +#include "yaffs_packedtags2.h"
8928 +#include "yportenv.h"
8929 +#include "yaffs_trace.h"
8931 +/* This code packs a set of extended tags into a binary structure for
8932 + * NAND storage
8933 + */
8935 +/* Some of the information is "extra" struff which can be packed in to
8936 + * speed scanning
8937 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
8938 + */
8940 +/* Extra flags applied to chunk_id */
8942 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
8943 +#define EXTRA_SHRINK_FLAG 0x40000000
8944 +#define EXTRA_SHADOWS_FLAG 0x20000000
8945 +#define EXTRA_SPARE_FLAGS 0x10000000
8947 +#define ALL_EXTRA_FLAGS 0xf0000000
8949 +/* Also, the top 4 bits of the object Id are set to the object type. */
8950 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
8951 +#define EXTRA_OBJECT_TYPE_MASK ((0x0f) << EXTRA_OBJECT_TYPE_SHIFT)
8953 +static void yaffs_dump_packed_tags2_tags_only(
8954 + const struct yaffs_packed_tags2_tags_only *ptt)
8956 + yaffs_trace(YAFFS_TRACE_MTD,
8957 + "packed tags obj %d chunk %d byte %d seq %d",
8958 + ptt->obj_id, ptt->chunk_id, ptt->n_bytes, ptt->seq_number);
8961 +static void yaffs_dump_packed_tags2(const struct yaffs_packed_tags2 *pt)
8963 + yaffs_dump_packed_tags2_tags_only(&pt->t);
8966 +static void yaffs_dump_tags2(const struct yaffs_ext_tags *t)
8968 + yaffs_trace(YAFFS_TRACE_MTD,
8969 + "ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte %d del %d ser %d seq %d",
8970 + t->ecc_result, t->block_bad, t->chunk_used, t->obj_id,
8971 + t->chunk_id, t->n_bytes, t->is_deleted, t->serial_number,
8972 + t->seq_number);
8976 +static int yaffs_check_tags_extra_packable(const struct yaffs_ext_tags *t)
8978 + if (t->chunk_id != 0 || !t->extra_available)
8979 + return 0;
8981 + /* Check if the file size is too long to store */
8982 + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE &&
8983 + (t->extra_file_size >> 31) != 0)
8984 + return 0;
8985 + return 1;
8988 +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *ptt,
8989 + const struct yaffs_ext_tags *t)
8991 + ptt->chunk_id = t->chunk_id;
8992 + ptt->seq_number = t->seq_number;
8993 + ptt->n_bytes = t->n_bytes;
8994 + ptt->obj_id = t->obj_id;
8996 + /* Only store extra tags for object headers.
8997 + * If it is a file then only store if the file size is short\
8998 + * enough to fit.
8999 + */
9000 + if (yaffs_check_tags_extra_packable(t)) {
9001 + /* Store the extra header info instead */
9002 + /* We save the parent object in the chunk_id */
9003 + ptt->chunk_id = EXTRA_HEADER_INFO_FLAG | t->extra_parent_id;
9004 + if (t->extra_is_shrink)
9005 + ptt->chunk_id |= EXTRA_SHRINK_FLAG;
9006 + if (t->extra_shadows)
9007 + ptt->chunk_id |= EXTRA_SHADOWS_FLAG;
9009 + ptt->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
9010 + ptt->obj_id |= (t->extra_obj_type << EXTRA_OBJECT_TYPE_SHIFT);
9012 + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
9013 + ptt->n_bytes = t->extra_equiv_id;
9014 + else if (t->extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
9015 + ptt->n_bytes = (unsigned) t->extra_file_size;
9016 + else
9017 + ptt->n_bytes = 0;
9020 + yaffs_dump_packed_tags2_tags_only(ptt);
9021 + yaffs_dump_tags2(t);
9024 +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
9025 + const struct yaffs_ext_tags *t, int tags_ecc)
9027 + yaffs_pack_tags2_tags_only(&pt->t, t);
9029 + if (tags_ecc)
9030 + yaffs_ecc_calc_other((unsigned char *)&pt->t,
9031 + sizeof(struct yaffs_packed_tags2_tags_only),
9032 + &pt->ecc);
9035 +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
9036 + struct yaffs_packed_tags2_tags_only *ptt)
9038 + memset(t, 0, sizeof(struct yaffs_ext_tags));
9040 + if (ptt->seq_number == 0xffffffff)
9041 + return;
9043 + t->block_bad = 0;
9044 + t->chunk_used = 1;
9045 + t->obj_id = ptt->obj_id;
9046 + t->chunk_id = ptt->chunk_id;
9047 + t->n_bytes = ptt->n_bytes;
9048 + t->is_deleted = 0;
9049 + t->serial_number = 0;
9050 + t->seq_number = ptt->seq_number;
9052 + /* Do extra header info stuff */
9053 + if (ptt->chunk_id & EXTRA_HEADER_INFO_FLAG) {
9054 + t->chunk_id = 0;
9055 + t->n_bytes = 0;
9057 + t->extra_available = 1;
9058 + t->extra_parent_id = ptt->chunk_id & (~(ALL_EXTRA_FLAGS));
9059 + t->extra_is_shrink = ptt->chunk_id & EXTRA_SHRINK_FLAG ? 1 : 0;
9060 + t->extra_shadows = ptt->chunk_id & EXTRA_SHADOWS_FLAG ? 1 : 0;
9061 + t->extra_obj_type = ptt->obj_id >> EXTRA_OBJECT_TYPE_SHIFT;
9062 + t->obj_id &= ~EXTRA_OBJECT_TYPE_MASK;
9064 + if (t->extra_obj_type == YAFFS_OBJECT_TYPE_HARDLINK)
9065 + t->extra_equiv_id = ptt->n_bytes;
9066 + else
9067 + t->extra_file_size = ptt->n_bytes;
9069 + yaffs_dump_packed_tags2_tags_only(ptt);
9070 + yaffs_dump_tags2(t);
9073 +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
9074 + int tags_ecc)
9076 + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
9078 + if (pt->t.seq_number != 0xffffffff && tags_ecc) {
9079 + /* Chunk is in use and we need to do ECC */
9081 + struct yaffs_ecc_other ecc;
9082 + int result;
9083 + yaffs_ecc_calc_other((unsigned char *)&pt->t,
9084 + sizeof(struct yaffs_packed_tags2_tags_only),
9085 + &ecc);
9086 + result =
9087 + yaffs_ecc_correct_other((unsigned char *)&pt->t,
9088 + sizeof(struct yaffs_packed_tags2_tags_only),
9089 + &pt->ecc, &ecc);
9090 + switch (result) {
9091 + case 0:
9092 + ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
9093 + break;
9094 + case 1:
9095 + ecc_result = YAFFS_ECC_RESULT_FIXED;
9096 + break;
9097 + case -1:
9098 + ecc_result = YAFFS_ECC_RESULT_UNFIXED;
9099 + break;
9100 + default:
9101 + ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
9104 + yaffs_unpack_tags2_tags_only(t, &pt->t);
9106 + t->ecc_result = ecc_result;
9108 + yaffs_dump_packed_tags2(pt);
9109 + yaffs_dump_tags2(t);
9111 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags2.h linux-3.12.20/fs/yaffs2/yaffs_packedtags2.h
9112 --- linux-3.12.20.orig/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
9113 +++ linux-3.12.20/fs/yaffs2/yaffs_packedtags2.h 2014-05-17 11:28:49.000000000 +0200
9114 @@ -0,0 +1,47 @@
9116 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
9118 + * Copyright (C) 2002-2011 Aleph One Ltd.
9119 + * for Toby Churchill Ltd and Brightstar Engineering
9121 + * Created by Charles Manning <charles@aleph1.co.uk>
9123 + * This program is free software; you can redistribute it and/or modify
9124 + * it under the terms of the GNU Lesser General Public License version 2.1 as
9125 + * published by the Free Software Foundation.
9127 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
9128 + */
9130 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
9132 +#ifndef __YAFFS_PACKEDTAGS2_H__
9133 +#define __YAFFS_PACKEDTAGS2_H__
9135 +#include "yaffs_guts.h"
9136 +#include "yaffs_ecc.h"
9138 +struct yaffs_packed_tags2_tags_only {
9139 + unsigned seq_number;
9140 + unsigned obj_id;
9141 + unsigned chunk_id;
9142 + unsigned n_bytes;
9145 +struct yaffs_packed_tags2 {
9146 + struct yaffs_packed_tags2_tags_only t;
9147 + struct yaffs_ecc_other ecc;
9150 +/* Full packed tags with ECC, used for oob tags */
9151 +void yaffs_pack_tags2(struct yaffs_packed_tags2 *pt,
9152 + const struct yaffs_ext_tags *t, int tags_ecc);
9153 +void yaffs_unpack_tags2(struct yaffs_ext_tags *t, struct yaffs_packed_tags2 *pt,
9154 + int tags_ecc);
9156 +/* Only the tags part (no ECC for use with inband tags */
9157 +void yaffs_pack_tags2_tags_only(struct yaffs_packed_tags2_tags_only *pt,
9158 + const struct yaffs_ext_tags *t);
9159 +void yaffs_unpack_tags2_tags_only(struct yaffs_ext_tags *t,
9160 + struct yaffs_packed_tags2_tags_only *pt);
9161 +#endif
9162 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_summary.c linux-3.12.20/fs/yaffs2/yaffs_summary.c
9163 --- linux-3.12.20.orig/fs/yaffs2/yaffs_summary.c 1970-01-01 01:00:00.000000000 +0100
9164 +++ linux-3.12.20/fs/yaffs2/yaffs_summary.c 2014-05-17 11:28:49.000000000 +0200
9165 @@ -0,0 +1,312 @@
9167 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
9169 + * Copyright (C) 2002-2011 Aleph One Ltd.
9170 + * for Toby Churchill Ltd and Brightstar Engineering
9172 + * Created by Charles Manning <charles@aleph1.co.uk>
9174 + * This program is free software; you can redistribute it and/or modify
9175 + * it under the terms of the GNU General Public License version 2 as
9176 + * published by the Free Software Foundation.
9177 + */
9179 +/* Summaries write the useful part of the tags for the chunks in a block into an
9180 + * an array which is written to the last n chunks of the block.
9181 + * Reading the summaries gives all the tags for the block in one read. Much
9182 + * faster.
9184 + * Chunks holding summaries are marked with tags making it look like
9185 + * they are part of a fake file.
9187 + * The summary could also be used during gc.
9189 + */
9191 +#include "yaffs_summary.h"
9192 +#include "yaffs_packedtags2.h"
9193 +#include "yaffs_nand.h"
9194 +#include "yaffs_getblockinfo.h"
9195 +#include "yaffs_bitmap.h"
9198 + * The summary is built up in an array of summary tags.
9199 + * This gets written to the last one or two (maybe more) chunks in a block.
9200 + * A summary header is written as the first part of each chunk of summary data.
9201 + * The summary header must match or the summary is rejected.
9202 + */
9204 +/* Summary tags don't need the sequence number because that is redundant. */
9205 +struct yaffs_summary_tags {
9206 + unsigned obj_id;
9207 + unsigned chunk_id;
9208 + unsigned n_bytes;
9211 +/* Summary header */
9212 +struct yaffs_summary_header {
9213 + unsigned version; /* Must match current version */
9214 + unsigned block; /* Must be this block */
9215 + unsigned seq; /* Must be this sequence number */
9216 + unsigned sum; /* Just add up all the bytes in the tags */
9220 +static void yaffs_summary_clear(struct yaffs_dev *dev)
9222 + if (!dev->sum_tags)
9223 + return;
9224 + memset(dev->sum_tags, 0, dev->chunks_per_summary *
9225 + sizeof(struct yaffs_summary_tags));
9229 +void yaffs_summary_deinit(struct yaffs_dev *dev)
9231 + kfree(dev->sum_tags);
9232 + dev->sum_tags = NULL;
9233 + kfree(dev->gc_sum_tags);
9234 + dev->gc_sum_tags = NULL;
9235 + dev->chunks_per_summary = 0;
9238 +int yaffs_summary_init(struct yaffs_dev *dev)
9240 + int sum_bytes;
9241 + int chunks_used; /* Number of chunks used by summary */
9242 + int sum_tags_bytes;
9244 + sum_bytes = dev->param.chunks_per_block *
9245 + sizeof(struct yaffs_summary_tags);
9247 + chunks_used = (sum_bytes + dev->data_bytes_per_chunk - 1)/
9248 + (dev->data_bytes_per_chunk -
9249 + sizeof(struct yaffs_summary_header));
9251 + dev->chunks_per_summary = dev->param.chunks_per_block - chunks_used;
9252 + sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
9253 + dev->chunks_per_summary;
9254 + dev->sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
9255 + dev->gc_sum_tags = kmalloc(sum_tags_bytes, GFP_NOFS);
9256 + if (!dev->sum_tags || !dev->gc_sum_tags) {
9257 + yaffs_summary_deinit(dev);
9258 + return YAFFS_FAIL;
9261 + yaffs_summary_clear(dev);
9263 + return YAFFS_OK;
9266 +static unsigned yaffs_summary_sum(struct yaffs_dev *dev)
9268 + u8 *sum_buffer = (u8 *)dev->sum_tags;
9269 + int i;
9270 + unsigned sum = 0;
9272 + i = sizeof(struct yaffs_summary_tags) *
9273 + dev->chunks_per_summary;
9274 + while (i > 0) {
9275 + sum += *sum_buffer;
9276 + sum_buffer++;
9277 + i--;
9280 + return sum;
9283 +static int yaffs_summary_write(struct yaffs_dev *dev, int blk)
9285 + struct yaffs_ext_tags tags;
9286 + u8 *buffer;
9287 + u8 *sum_buffer = (u8 *)dev->sum_tags;
9288 + int n_bytes;
9289 + int chunk_in_nand;
9290 + int chunk_in_block;
9291 + int result;
9292 + int this_tx;
9293 + struct yaffs_summary_header hdr;
9294 + int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
9295 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
9297 + buffer = yaffs_get_temp_buffer(dev);
9298 + n_bytes = sizeof(struct yaffs_summary_tags) *
9299 + dev->chunks_per_summary;
9300 + memset(&tags, 0, sizeof(struct yaffs_ext_tags));
9301 + tags.obj_id = YAFFS_OBJECTID_SUMMARY;
9302 + tags.chunk_id = 1;
9303 + chunk_in_block = dev->chunks_per_summary;
9304 + chunk_in_nand = dev->alloc_block * dev->param.chunks_per_block +
9305 + dev->chunks_per_summary;
9306 + hdr.version = YAFFS_SUMMARY_VERSION;
9307 + hdr.block = blk;
9308 + hdr.seq = bi->seq_number;
9309 + hdr.sum = yaffs_summary_sum(dev);
9311 + do {
9312 + this_tx = n_bytes;
9313 + if (this_tx > sum_bytes_per_chunk)
9314 + this_tx = sum_bytes_per_chunk;
9315 + memcpy(buffer, &hdr, sizeof(hdr));
9316 + memcpy(buffer + sizeof(hdr), sum_buffer, this_tx);
9317 + tags.n_bytes = this_tx + sizeof(hdr);
9318 + result = yaffs_wr_chunk_tags_nand(dev, chunk_in_nand,
9319 + buffer, &tags);
9321 + if (result != YAFFS_OK)
9322 + break;
9323 + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
9324 + bi->pages_in_use++;
9325 + dev->n_free_chunks--;
9327 + n_bytes -= this_tx;
9328 + sum_buffer += this_tx;
9329 + chunk_in_nand++;
9330 + chunk_in_block++;
9331 + tags.chunk_id++;
9332 + } while (result == YAFFS_OK && n_bytes > 0);
9333 + yaffs_release_temp_buffer(dev, buffer);
9336 + if (result == YAFFS_OK)
9337 + bi->has_summary = 1;
9340 + return result;
9343 +int yaffs_summary_read(struct yaffs_dev *dev,
9344 + struct yaffs_summary_tags *st,
9345 + int blk)
9347 + struct yaffs_ext_tags tags;
9348 + u8 *buffer;
9349 + u8 *sum_buffer = (u8 *)st;
9350 + int n_bytes;
9351 + int chunk_id;
9352 + int chunk_in_nand;
9353 + int chunk_in_block;
9354 + int result;
9355 + int this_tx;
9356 + struct yaffs_summary_header hdr;
9357 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
9358 + int sum_bytes_per_chunk = dev->data_bytes_per_chunk - sizeof(hdr);
9359 + int sum_tags_bytes;
9361 + sum_tags_bytes = sizeof(struct yaffs_summary_tags) *
9362 + dev->chunks_per_summary;
9363 + buffer = yaffs_get_temp_buffer(dev);
9364 + n_bytes = sizeof(struct yaffs_summary_tags) * dev->chunks_per_summary;
9365 + chunk_in_block = dev->chunks_per_summary;
9366 + chunk_in_nand = blk * dev->param.chunks_per_block +
9367 + dev->chunks_per_summary;
9368 + chunk_id = 1;
9369 + do {
9370 + this_tx = n_bytes;
9371 + if (this_tx > sum_bytes_per_chunk)
9372 + this_tx = sum_bytes_per_chunk;
9373 + result = yaffs_rd_chunk_tags_nand(dev, chunk_in_nand,
9374 + buffer, &tags);
9376 + if (tags.chunk_id != chunk_id ||
9377 + tags.obj_id != YAFFS_OBJECTID_SUMMARY ||
9378 + tags.chunk_used == 0 ||
9379 + tags.ecc_result > YAFFS_ECC_RESULT_FIXED ||
9380 + tags.n_bytes != (this_tx + sizeof(hdr)))
9381 + result = YAFFS_FAIL;
9382 + if (result != YAFFS_OK)
9383 + break;
9385 + if (st == dev->sum_tags) {
9386 + /* If we're scanning then update the block info */
9387 + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
9388 + bi->pages_in_use++;
9390 + memcpy(&hdr, buffer, sizeof(hdr));
9391 + memcpy(sum_buffer, buffer + sizeof(hdr), this_tx);
9392 + n_bytes -= this_tx;
9393 + sum_buffer += this_tx;
9394 + chunk_in_nand++;
9395 + chunk_in_block++;
9396 + chunk_id++;
9397 + } while (result == YAFFS_OK && n_bytes > 0);
9398 + yaffs_release_temp_buffer(dev, buffer);
9400 + if (result == YAFFS_OK) {
9401 + /* Verify header */
9402 + if (hdr.version != YAFFS_SUMMARY_VERSION ||
9403 + hdr.seq != bi->seq_number ||
9404 + hdr.sum != yaffs_summary_sum(dev))
9405 + result = YAFFS_FAIL;
9408 + if (st == dev->sum_tags && result == YAFFS_OK)
9409 + bi->has_summary = 1;
9411 + return result;
9414 +int yaffs_summary_add(struct yaffs_dev *dev,
9415 + struct yaffs_ext_tags *tags,
9416 + int chunk_in_nand)
9418 + struct yaffs_packed_tags2_tags_only tags_only;
9419 + struct yaffs_summary_tags *sum_tags;
9420 + int block_in_nand = chunk_in_nand / dev->param.chunks_per_block;
9421 + int chunk_in_block = chunk_in_nand % dev->param.chunks_per_block;
9423 + if (!dev->sum_tags)
9424 + return YAFFS_OK;
9426 + if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
9427 + yaffs_pack_tags2_tags_only(&tags_only, tags);
9428 + sum_tags = &dev->sum_tags[chunk_in_block];
9429 + sum_tags->chunk_id = tags_only.chunk_id;
9430 + sum_tags->n_bytes = tags_only.n_bytes;
9431 + sum_tags->obj_id = tags_only.obj_id;
9433 + if (chunk_in_block == dev->chunks_per_summary - 1) {
9434 + /* Time to write out the summary */
9435 + yaffs_summary_write(dev, block_in_nand);
9436 + yaffs_summary_clear(dev);
9437 + yaffs_skip_rest_of_block(dev);
9440 + return YAFFS_OK;
9443 +int yaffs_summary_fetch(struct yaffs_dev *dev,
9444 + struct yaffs_ext_tags *tags,
9445 + int chunk_in_block)
9447 + struct yaffs_packed_tags2_tags_only tags_only;
9448 + struct yaffs_summary_tags *sum_tags;
9449 + if (chunk_in_block >= 0 && chunk_in_block < dev->chunks_per_summary) {
9450 + sum_tags = &dev->sum_tags[chunk_in_block];
9451 + tags_only.chunk_id = sum_tags->chunk_id;
9452 + tags_only.n_bytes = sum_tags->n_bytes;
9453 + tags_only.obj_id = sum_tags->obj_id;
9454 + yaffs_unpack_tags2_tags_only(tags, &tags_only);
9455 + return YAFFS_OK;
9457 + return YAFFS_FAIL;
9460 +void yaffs_summary_gc(struct yaffs_dev *dev, int blk)
9462 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, blk);
9463 + int i;
9465 + if (!bi->has_summary)
9466 + return;
9468 + for (i = dev->chunks_per_summary;
9469 + i < dev->param.chunks_per_block;
9470 + i++) {
9471 + if (yaffs_check_chunk_bit(dev, blk, i)) {
9472 + yaffs_clear_chunk_bit(dev, blk, i);
9473 + bi->pages_in_use--;
9474 + dev->n_free_chunks++;
9478 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_summary.h linux-3.12.20/fs/yaffs2/yaffs_summary.h
9479 --- linux-3.12.20.orig/fs/yaffs2/yaffs_summary.h 1970-01-01 01:00:00.000000000 +0100
9480 +++ linux-3.12.20/fs/yaffs2/yaffs_summary.h 2014-05-17 11:28:49.000000000 +0200
9481 @@ -0,0 +1,37 @@
9483 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
9485 + * Copyright (C) 2002-2011 Aleph One Ltd.
9486 + * for Toby Churchill Ltd and Brightstar Engineering
9488 + * Created by Charles Manning <charles@aleph1.co.uk>
9490 + * This program is free software; you can redistribute it and/or modify
9491 + * it under the terms of the GNU Lesser General Public License version 2.1 as
9492 + * published by the Free Software Foundation.
9494 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
9495 + */
9497 +#ifndef __YAFFS_SUMMARY_H__
9498 +#define __YAFFS_SUMMARY_H__
9500 +#include "yaffs_packedtags2.h"
9503 +int yaffs_summary_init(struct yaffs_dev *dev);
9504 +void yaffs_summary_deinit(struct yaffs_dev *dev);
9506 +int yaffs_summary_add(struct yaffs_dev *dev,
9507 + struct yaffs_ext_tags *tags,
9508 + int chunk_in_block);
9509 +int yaffs_summary_fetch(struct yaffs_dev *dev,
9510 + struct yaffs_ext_tags *tags,
9511 + int chunk_in_block);
9512 +int yaffs_summary_read(struct yaffs_dev *dev,
9513 + struct yaffs_summary_tags *st,
9514 + int blk);
9515 +void yaffs_summary_gc(struct yaffs_dev *dev, int blk);
9518 +#endif
9519 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_tagscompat.c linux-3.12.20/fs/yaffs2/yaffs_tagscompat.c
9520 --- linux-3.12.20.orig/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
9521 +++ linux-3.12.20/fs/yaffs2/yaffs_tagscompat.c 2014-05-17 11:28:49.000000000 +0200
9522 @@ -0,0 +1,381 @@
9524 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
9526 + * Copyright (C) 2002-2011 Aleph One Ltd.
9527 + * for Toby Churchill Ltd and Brightstar Engineering
9529 + * Created by Charles Manning <charles@aleph1.co.uk>
9531 + * This program is free software; you can redistribute it and/or modify
9532 + * it under the terms of the GNU General Public License version 2 as
9533 + * published by the Free Software Foundation.
9534 + */
9536 +#include "yaffs_guts.h"
9537 +#include "yaffs_tagscompat.h"
9538 +#include "yaffs_ecc.h"
9539 +#include "yaffs_getblockinfo.h"
9540 +#include "yaffs_trace.h"
9542 +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk);
9545 +/********** Tags ECC calculations *********/
9548 +void yaffs_calc_tags_ecc(struct yaffs_tags *tags)
9550 + /* Calculate an ecc */
9551 + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
9552 + unsigned i, j;
9553 + unsigned ecc = 0;
9554 + unsigned bit = 0;
9556 + tags->ecc = 0;
9558 + for (i = 0; i < 8; i++) {
9559 + for (j = 1; j & 0xff; j <<= 1) {
9560 + bit++;
9561 + if (b[i] & j)
9562 + ecc ^= bit;
9565 + tags->ecc = ecc;
9568 +int yaffs_check_tags_ecc(struct yaffs_tags *tags)
9570 + unsigned ecc = tags->ecc;
9572 + yaffs_calc_tags_ecc(tags);
9574 + ecc ^= tags->ecc;
9576 + if (ecc && ecc <= 64) {
9577 + /* TODO: Handle the failure better. Retire? */
9578 + unsigned char *b = ((union yaffs_tags_union *)tags)->as_bytes;
9580 + ecc--;
9582 + b[ecc / 8] ^= (1 << (ecc & 7));
9584 + /* Now recvalc the ecc */
9585 + yaffs_calc_tags_ecc(tags);
9587 + return 1; /* recovered error */
9588 + } else if (ecc) {
9589 + /* Wierd ecc failure value */
9590 + /* TODO Need to do somethiong here */
9591 + return -1; /* unrecovered error */
9593 + return 0;
9596 +/********** Tags **********/
9598 +static void yaffs_load_tags_to_spare(struct yaffs_spare *spare_ptr,
9599 + struct yaffs_tags *tags_ptr)
9601 + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
9603 + yaffs_calc_tags_ecc(tags_ptr);
9605 + spare_ptr->tb0 = tu->as_bytes[0];
9606 + spare_ptr->tb1 = tu->as_bytes[1];
9607 + spare_ptr->tb2 = tu->as_bytes[2];
9608 + spare_ptr->tb3 = tu->as_bytes[3];
9609 + spare_ptr->tb4 = tu->as_bytes[4];
9610 + spare_ptr->tb5 = tu->as_bytes[5];
9611 + spare_ptr->tb6 = tu->as_bytes[6];
9612 + spare_ptr->tb7 = tu->as_bytes[7];
9615 +static void yaffs_get_tags_from_spare(struct yaffs_dev *dev,
9616 + struct yaffs_spare *spare_ptr,
9617 + struct yaffs_tags *tags_ptr)
9619 + union yaffs_tags_union *tu = (union yaffs_tags_union *)tags_ptr;
9620 + int result;
9622 + tu->as_bytes[0] = spare_ptr->tb0;
9623 + tu->as_bytes[1] = spare_ptr->tb1;
9624 + tu->as_bytes[2] = spare_ptr->tb2;
9625 + tu->as_bytes[3] = spare_ptr->tb3;
9626 + tu->as_bytes[4] = spare_ptr->tb4;
9627 + tu->as_bytes[5] = spare_ptr->tb5;
9628 + tu->as_bytes[6] = spare_ptr->tb6;
9629 + tu->as_bytes[7] = spare_ptr->tb7;
9631 + result = yaffs_check_tags_ecc(tags_ptr);
9632 + if (result > 0)
9633 + dev->n_tags_ecc_fixed++;
9634 + else if (result < 0)
9635 + dev->n_tags_ecc_unfixed++;
9638 +static void yaffs_spare_init(struct yaffs_spare *spare)
9640 + memset(spare, 0xff, sizeof(struct yaffs_spare));
9643 +static int yaffs_wr_nand(struct yaffs_dev *dev,
9644 + int nand_chunk, const u8 *data,
9645 + struct yaffs_spare *spare)
9647 + int data_size = dev->data_bytes_per_chunk;
9649 + return dev->drv.drv_write_chunk_fn(dev, nand_chunk,
9650 + data, data_size,
9651 + (u8 *) spare, sizeof(*spare));
9654 +static int yaffs_rd_chunk_nand(struct yaffs_dev *dev,
9655 + int nand_chunk,
9656 + u8 *data,
9657 + struct yaffs_spare *spare,
9658 + enum yaffs_ecc_result *ecc_result,
9659 + int correct_errors)
9661 + int ret_val;
9662 + struct yaffs_spare local_spare;
9663 + int data_size;
9664 + int spare_size;
9665 + int ecc_result1, ecc_result2;
9666 + u8 calc_ecc[3];
9668 + if (!spare) {
9669 + /* If we don't have a real spare, then we use a local one. */
9670 + /* Need this for the calculation of the ecc */
9671 + spare = &local_spare;
9673 + data_size = dev->data_bytes_per_chunk;
9674 + spare_size = sizeof(struct yaffs_spare);
9676 + if (dev->param.use_nand_ecc)
9677 + return dev->drv.drv_read_chunk_fn(dev, nand_chunk,
9678 + data, data_size,
9679 + (u8 *) spare, spare_size,
9680 + ecc_result);
9683 + /* Handle the ECC at this level. */
9685 + ret_val = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
9686 + data, data_size,
9687 + (u8 *)spare, spare_size,
9688 + NULL);
9689 + if (!data || !correct_errors)
9690 + return ret_val;
9692 + /* Do ECC correction if needed. */
9693 + yaffs_ecc_calc(data, calc_ecc);
9694 + ecc_result1 = yaffs_ecc_correct(data, spare->ecc1, calc_ecc);
9695 + yaffs_ecc_calc(&data[256], calc_ecc);
9696 + ecc_result2 = yaffs_ecc_correct(&data[256], spare->ecc2, calc_ecc);
9698 + if (ecc_result1 > 0) {
9699 + yaffs_trace(YAFFS_TRACE_ERROR,
9700 + "**>>yaffs ecc error fix performed on chunk %d:0",
9701 + nand_chunk);
9702 + dev->n_ecc_fixed++;
9703 + } else if (ecc_result1 < 0) {
9704 + yaffs_trace(YAFFS_TRACE_ERROR,
9705 + "**>>yaffs ecc error unfixed on chunk %d:0",
9706 + nand_chunk);
9707 + dev->n_ecc_unfixed++;
9710 + if (ecc_result2 > 0) {
9711 + yaffs_trace(YAFFS_TRACE_ERROR,
9712 + "**>>yaffs ecc error fix performed on chunk %d:1",
9713 + nand_chunk);
9714 + dev->n_ecc_fixed++;
9715 + } else if (ecc_result2 < 0) {
9716 + yaffs_trace(YAFFS_TRACE_ERROR,
9717 + "**>>yaffs ecc error unfixed on chunk %d:1",
9718 + nand_chunk);
9719 + dev->n_ecc_unfixed++;
9722 + if (ecc_result1 || ecc_result2) {
9723 + /* We had a data problem on this page */
9724 + yaffs_handle_rd_data_error(dev, nand_chunk);
9727 + if (ecc_result1 < 0 || ecc_result2 < 0)
9728 + *ecc_result = YAFFS_ECC_RESULT_UNFIXED;
9729 + else if (ecc_result1 > 0 || ecc_result2 > 0)
9730 + *ecc_result = YAFFS_ECC_RESULT_FIXED;
9731 + else
9732 + *ecc_result = YAFFS_ECC_RESULT_NO_ERROR;
9734 + return ret_val;
9738 + * Functions for robustisizing
9739 + */
9741 +static void yaffs_handle_rd_data_error(struct yaffs_dev *dev, int nand_chunk)
9743 + int flash_block = nand_chunk / dev->param.chunks_per_block;
9745 + /* Mark the block for retirement */
9746 + yaffs_get_block_info(dev, flash_block + dev->block_offset)->
9747 + needs_retiring = 1;
9748 + yaffs_trace(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
9749 + "**>>Block %d marked for retirement",
9750 + flash_block);
9752 + /* TODO:
9753 + * Just do a garbage collection on the affected block
9754 + * then retire the block
9755 + * NB recursion
9756 + */
9759 +static int yaffs_tags_compat_wr(struct yaffs_dev *dev,
9760 + int nand_chunk,
9761 + const u8 *data, const struct yaffs_ext_tags *ext_tags)
9763 + struct yaffs_spare spare;
9764 + struct yaffs_tags tags;
9766 + yaffs_spare_init(&spare);
9768 + if (ext_tags->is_deleted)
9769 + spare.page_status = 0;
9770 + else {
9771 + tags.obj_id = ext_tags->obj_id;
9772 + tags.chunk_id = ext_tags->chunk_id;
9774 + tags.n_bytes_lsb = ext_tags->n_bytes & (1024 - 1);
9776 + if (dev->data_bytes_per_chunk >= 1024)
9777 + tags.n_bytes_msb = (ext_tags->n_bytes >> 10) & 3;
9778 + else
9779 + tags.n_bytes_msb = 3;
9781 + tags.serial_number = ext_tags->serial_number;
9783 + if (!dev->param.use_nand_ecc && data) {
9784 + yaffs_ecc_calc(data, spare.ecc1);
9785 + yaffs_ecc_calc(&data[256], spare.ecc2);
9788 + yaffs_load_tags_to_spare(&spare, &tags);
9790 + return yaffs_wr_nand(dev, nand_chunk, data, &spare);
9793 +static int yaffs_tags_compat_rd(struct yaffs_dev *dev,
9794 + int nand_chunk,
9795 + u8 *data, struct yaffs_ext_tags *ext_tags)
9797 + struct yaffs_spare spare;
9798 + struct yaffs_tags tags;
9799 + enum yaffs_ecc_result ecc_result = YAFFS_ECC_RESULT_UNKNOWN;
9800 + static struct yaffs_spare spare_ff;
9801 + static int init;
9802 + int deleted;
9804 + if (!init) {
9805 + memset(&spare_ff, 0xff, sizeof(spare_ff));
9806 + init = 1;
9809 + if (!yaffs_rd_chunk_nand(dev, nand_chunk,
9810 + data, &spare, &ecc_result, 1))
9811 + return YAFFS_FAIL;
9813 + /* ext_tags may be NULL */
9814 + if (!ext_tags)
9815 + return YAFFS_OK;
9817 + deleted = (hweight8(spare.page_status) < 7) ? 1 : 0;
9819 + ext_tags->is_deleted = deleted;
9820 + ext_tags->ecc_result = ecc_result;
9821 + ext_tags->block_bad = 0; /* We're reading it */
9822 + /* therefore it is not a bad block */
9823 + ext_tags->chunk_used =
9824 + memcmp(&spare_ff, &spare, sizeof(spare_ff)) ? 1 : 0;
9826 + if (ext_tags->chunk_used) {
9827 + yaffs_get_tags_from_spare(dev, &spare, &tags);
9828 + ext_tags->obj_id = tags.obj_id;
9829 + ext_tags->chunk_id = tags.chunk_id;
9830 + ext_tags->n_bytes = tags.n_bytes_lsb;
9832 + if (dev->data_bytes_per_chunk >= 1024)
9833 + ext_tags->n_bytes |=
9834 + (((unsigned)tags.n_bytes_msb) << 10);
9836 + ext_tags->serial_number = tags.serial_number;
9839 + return YAFFS_OK;
9842 +static int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int flash_block)
9844 + struct yaffs_spare spare;
9846 + memset(&spare, 0xff, sizeof(struct yaffs_spare));
9848 + spare.block_status = 'Y';
9850 + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block, NULL,
9851 + &spare);
9852 + yaffs_wr_nand(dev, flash_block * dev->param.chunks_per_block + 1,
9853 + NULL, &spare);
9855 + return YAFFS_OK;
9858 +static int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
9859 + int block_no,
9860 + enum yaffs_block_state *state,
9861 + u32 *seq_number)
9863 + struct yaffs_spare spare0, spare1;
9864 + static struct yaffs_spare spare_ff;
9865 + static int init;
9866 + enum yaffs_ecc_result dummy;
9868 + if (!init) {
9869 + memset(&spare_ff, 0xff, sizeof(spare_ff));
9870 + init = 1;
9873 + *seq_number = 0;
9875 + /* Look for bad block markers in the first two chunks */
9876 + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block,
9877 + NULL, &spare0, &dummy, 0);
9878 + yaffs_rd_chunk_nand(dev, block_no * dev->param.chunks_per_block + 1,
9879 + NULL, &spare1, &dummy, 0);
9881 + if (hweight8(spare0.block_status & spare1.block_status) < 7)
9882 + *state = YAFFS_BLOCK_STATE_DEAD;
9883 + else if (memcmp(&spare_ff, &spare0, sizeof(spare_ff)) == 0)
9884 + *state = YAFFS_BLOCK_STATE_EMPTY;
9885 + else
9886 + *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
9888 + return YAFFS_OK;
9891 +void yaffs_tags_compat_install(struct yaffs_dev *dev)
9893 + if(dev->param.is_yaffs2)
9894 + return;
9895 + if(!dev->tagger.write_chunk_tags_fn)
9896 + dev->tagger.write_chunk_tags_fn = yaffs_tags_compat_wr;
9897 + if(!dev->tagger.read_chunk_tags_fn)
9898 + dev->tagger.read_chunk_tags_fn = yaffs_tags_compat_rd;
9899 + if(!dev->tagger.query_block_fn)
9900 + dev->tagger.query_block_fn = yaffs_tags_compat_query_block;
9901 + if(!dev->tagger.mark_bad_fn)
9902 + dev->tagger.mark_bad_fn = yaffs_tags_compat_mark_bad;
9904 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_tagscompat.h linux-3.12.20/fs/yaffs2/yaffs_tagscompat.h
9905 --- linux-3.12.20.orig/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
9906 +++ linux-3.12.20/fs/yaffs2/yaffs_tagscompat.h 2014-05-17 11:28:49.000000000 +0200
9907 @@ -0,0 +1,44 @@
9909 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
9911 + * Copyright (C) 2002-2011 Aleph One Ltd.
9912 + * for Toby Churchill Ltd and Brightstar Engineering
9914 + * Created by Charles Manning <charles@aleph1.co.uk>
9916 + * This program is free software; you can redistribute it and/or modify
9917 + * it under the terms of the GNU Lesser General Public License version 2.1 as
9918 + * published by the Free Software Foundation.
9920 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
9921 + */
9923 +#ifndef __YAFFS_TAGSCOMPAT_H__
9924 +#define __YAFFS_TAGSCOMPAT_H__
9927 +#include "yaffs_guts.h"
9929 +#if 0
9932 +int yaffs_tags_compat_wr(struct yaffs_dev *dev,
9933 + int nand_chunk,
9934 + const u8 *data, const struct yaffs_ext_tags *tags);
9935 +int yaffs_tags_compat_rd(struct yaffs_dev *dev,
9936 + int nand_chunk,
9937 + u8 *data, struct yaffs_ext_tags *tags);
9938 +int yaffs_tags_compat_mark_bad(struct yaffs_dev *dev, int block_no);
9939 +int yaffs_tags_compat_query_block(struct yaffs_dev *dev,
9940 + int block_no,
9941 + enum yaffs_block_state *state,
9942 + u32 *seq_number);
9944 +#endif
9947 +void yaffs_tags_compat_install(struct yaffs_dev *dev);
9948 +void yaffs_calc_tags_ecc(struct yaffs_tags *tags);
9949 +int yaffs_check_tags_ecc(struct yaffs_tags *tags);
9951 +#endif
9952 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_tagsmarshall.c linux-3.12.20/fs/yaffs2/yaffs_tagsmarshall.c
9953 --- linux-3.12.20.orig/fs/yaffs2/yaffs_tagsmarshall.c 1970-01-01 01:00:00.000000000 +0100
9954 +++ linux-3.12.20/fs/yaffs2/yaffs_tagsmarshall.c 2014-05-17 11:28:49.000000000 +0200
9955 @@ -0,0 +1,199 @@
9957 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
9959 + * Copyright (C) 2002-2011 Aleph One Ltd.
9960 + * for Toby Churchill Ltd and Brightstar Engineering
9962 + * Created by Charles Manning <charles@aleph1.co.uk>
9964 + * This program is free software; you can redistribute it and/or modify
9965 + * it under the terms of the GNU General Public License version 2 as
9966 + * published by the Free Software Foundation.
9967 + */
9969 +#include "yaffs_guts.h"
9970 +#include "yaffs_trace.h"
9971 +#include "yaffs_packedtags2.h"
9973 +static int yaffs_tags_marshall_write(struct yaffs_dev *dev,
9974 + int nand_chunk, const u8 *data,
9975 + const struct yaffs_ext_tags *tags)
9977 + struct yaffs_packed_tags2 pt;
9978 + int retval;
9980 + int packed_tags_size =
9981 + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
9982 + void *packed_tags_ptr =
9983 + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
9985 + yaffs_trace(YAFFS_TRACE_MTD,
9986 + "yaffs_tags_marshall_write chunk %d data %p tags %p",
9987 + nand_chunk, data, tags);
9989 + /* For yaffs2 writing there must be both data and tags.
9990 + * If we're using inband tags, then the tags are stuffed into
9991 + * the end of the data buffer.
9992 + */
9993 + if (!data || !tags)
9994 + BUG();
9995 + else if (dev->param.inband_tags) {
9996 + struct yaffs_packed_tags2_tags_only *pt2tp;
9997 + pt2tp =
9998 + (struct yaffs_packed_tags2_tags_only *)(data +
9999 + dev->
10000 + data_bytes_per_chunk);
10001 + yaffs_pack_tags2_tags_only(pt2tp, tags);
10002 + } else {
10003 + yaffs_pack_tags2(&pt, tags, !dev->param.no_tags_ecc);
10006 + retval = dev->drv.drv_write_chunk_fn(dev, nand_chunk,
10007 + data, dev->param.total_bytes_per_chunk,
10008 + (dev->param.inband_tags) ? NULL : packed_tags_ptr,
10009 + (dev->param.inband_tags) ? 0 : packed_tags_size);
10011 + return retval;
10014 +static int yaffs_tags_marshall_read(struct yaffs_dev *dev,
10015 + int nand_chunk, u8 *data,
10016 + struct yaffs_ext_tags *tags)
10018 + int retval = 0;
10019 + int local_data = 0;
10020 + u8 spare_buffer[100];
10021 + enum yaffs_ecc_result ecc_result;
10023 + struct yaffs_packed_tags2 pt;
10025 + int packed_tags_size =
10026 + dev->param.no_tags_ecc ? sizeof(pt.t) : sizeof(pt);
10027 + void *packed_tags_ptr =
10028 + dev->param.no_tags_ecc ? (void *)&pt.t : (void *)&pt;
10030 + yaffs_trace(YAFFS_TRACE_MTD,
10031 + "yaffs_tags_marshall_read chunk %d data %p tags %p",
10032 + nand_chunk, data, tags);
10034 + if (dev->param.inband_tags) {
10035 + if (!data) {
10036 + local_data = 1;
10037 + data = yaffs_get_temp_buffer(dev);
10041 + if (dev->param.inband_tags || (data && !tags))
10042 + retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
10043 + data, dev->param.total_bytes_per_chunk,
10044 + NULL, 0,
10045 + &ecc_result);
10046 + else if (tags)
10047 + retval = dev->drv.drv_read_chunk_fn(dev, nand_chunk,
10048 + data, dev->param.total_bytes_per_chunk,
10049 + spare_buffer, packed_tags_size,
10050 + &ecc_result);
10051 + else
10052 + BUG();
10055 + if (dev->param.inband_tags) {
10056 + if (tags) {
10057 + struct yaffs_packed_tags2_tags_only *pt2tp;
10058 + pt2tp =
10059 + (struct yaffs_packed_tags2_tags_only *)
10060 + &data[dev->data_bytes_per_chunk];
10061 + yaffs_unpack_tags2_tags_only(tags, pt2tp);
10063 + } else if (tags) {
10064 + memcpy(packed_tags_ptr, spare_buffer, packed_tags_size);
10065 + yaffs_unpack_tags2(tags, &pt, !dev->param.no_tags_ecc);
10068 + if (local_data)
10069 + yaffs_release_temp_buffer(dev, data);
10071 + if (tags && ecc_result == YAFFS_ECC_RESULT_UNFIXED) {
10072 + tags->ecc_result = YAFFS_ECC_RESULT_UNFIXED;
10073 + dev->n_ecc_unfixed++;
10076 + if (tags && ecc_result == -YAFFS_ECC_RESULT_FIXED) {
10077 + if (tags->ecc_result <= YAFFS_ECC_RESULT_NO_ERROR)
10078 + tags->ecc_result = YAFFS_ECC_RESULT_FIXED;
10079 + dev->n_ecc_fixed++;
10082 + if (ecc_result < YAFFS_ECC_RESULT_UNFIXED)
10083 + return YAFFS_OK;
10084 + else
10085 + return YAFFS_FAIL;
10088 +static int yaffs_tags_marshall_query_block(struct yaffs_dev *dev, int block_no,
10089 + enum yaffs_block_state *state,
10090 + u32 *seq_number)
10092 + int retval;
10094 + yaffs_trace(YAFFS_TRACE_MTD, "yaffs_tags_marshall_query_block %d",
10095 + block_no);
10097 + retval = dev->drv.drv_check_bad_fn(dev, block_no);
10099 + if (retval== YAFFS_FAIL) {
10100 + yaffs_trace(YAFFS_TRACE_MTD, "block is bad");
10102 + *state = YAFFS_BLOCK_STATE_DEAD;
10103 + *seq_number = 0;
10104 + } else {
10105 + struct yaffs_ext_tags t;
10107 + yaffs_tags_marshall_read(dev,
10108 + block_no * dev->param.chunks_per_block,
10109 + NULL, &t);
10111 + if (t.chunk_used) {
10112 + *seq_number = t.seq_number;
10113 + *state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
10114 + } else {
10115 + *seq_number = 0;
10116 + *state = YAFFS_BLOCK_STATE_EMPTY;
10120 + yaffs_trace(YAFFS_TRACE_MTD,
10121 + "block query returns seq %d state %d",
10122 + *seq_number, *state);
10124 + if (retval == 0)
10125 + return YAFFS_OK;
10126 + else
10127 + return YAFFS_FAIL;
10130 +static int yaffs_tags_marshall_mark_bad(struct yaffs_dev *dev, int block_no)
10132 + return dev->drv.drv_mark_bad_fn(dev, block_no);
10137 +void yaffs_tags_marshall_install(struct yaffs_dev *dev)
10139 + if (!dev->param.is_yaffs2)
10140 + return;
10142 + if (!dev->tagger.write_chunk_tags_fn)
10143 + dev->tagger.write_chunk_tags_fn = yaffs_tags_marshall_write;
10145 + if (!dev->tagger.read_chunk_tags_fn)
10146 + dev->tagger.read_chunk_tags_fn = yaffs_tags_marshall_read;
10148 + if (!dev->tagger.query_block_fn)
10149 + dev->tagger.query_block_fn = yaffs_tags_marshall_query_block;
10151 + if (!dev->tagger.mark_bad_fn)
10152 + dev->tagger.mark_bad_fn = yaffs_tags_marshall_mark_bad;
10155 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_tagsmarshall.h linux-3.12.20/fs/yaffs2/yaffs_tagsmarshall.h
10156 --- linux-3.12.20.orig/fs/yaffs2/yaffs_tagsmarshall.h 1970-01-01 01:00:00.000000000 +0100
10157 +++ linux-3.12.20/fs/yaffs2/yaffs_tagsmarshall.h 2014-05-17 11:28:49.000000000 +0200
10158 @@ -0,0 +1,22 @@
10160 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
10162 + * Copyright (C) 2002-2011 Aleph One Ltd.
10163 + * for Toby Churchill Ltd and Brightstar Engineering
10165 + * Created by Charles Manning <charles@aleph1.co.uk>
10167 + * This program is free software; you can redistribute it and/or modify
10168 + * it under the terms of the GNU Lesser General Public License version 2.1 as
10169 + * published by the Free Software Foundation.
10171 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
10172 + */
10174 +#ifndef __YAFFS_TAGSMARSHALL_H__
10175 +#define __YAFFS_TAGSMARSHALL_H__
10177 +#include "yaffs_guts.h"
10178 +void yaffs_tags_marshall_install(struct yaffs_dev *dev);
10180 +#endif
10181 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_trace.h linux-3.12.20/fs/yaffs2/yaffs_trace.h
10182 --- linux-3.12.20.orig/fs/yaffs2/yaffs_trace.h 1970-01-01 01:00:00.000000000 +0100
10183 +++ linux-3.12.20/fs/yaffs2/yaffs_trace.h 2014-05-17 11:28:49.000000000 +0200
10184 @@ -0,0 +1,57 @@
10186 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
10188 + * Copyright (C) 2002-2011 Aleph One Ltd.
10189 + * for Toby Churchill Ltd and Brightstar Engineering
10191 + * Created by Charles Manning <charles@aleph1.co.uk>
10193 + * This program is free software; you can redistribute it and/or modify
10194 + * it under the terms of the GNU Lesser General Public License version 2.1 as
10195 + * published by the Free Software Foundation.
10197 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
10198 + */
10200 +#ifndef __YTRACE_H__
10201 +#define __YTRACE_H__
10203 +extern unsigned int yaffs_trace_mask;
10204 +extern unsigned int yaffs_wr_attempts;
10207 + * Tracing flags.
10208 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
10209 + */
10211 +#define YAFFS_TRACE_OS 0x00000002
10212 +#define YAFFS_TRACE_ALLOCATE 0x00000004
10213 +#define YAFFS_TRACE_SCAN 0x00000008
10214 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
10215 +#define YAFFS_TRACE_ERASE 0x00000020
10216 +#define YAFFS_TRACE_GC 0x00000040
10217 +#define YAFFS_TRACE_WRITE 0x00000080
10218 +#define YAFFS_TRACE_TRACING 0x00000100
10219 +#define YAFFS_TRACE_DELETION 0x00000200
10220 +#define YAFFS_TRACE_BUFFERS 0x00000400
10221 +#define YAFFS_TRACE_NANDACCESS 0x00000800
10222 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
10223 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
10224 +#define YAFFS_TRACE_MTD 0x00004000
10225 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
10227 +#define YAFFS_TRACE_VERIFY 0x00010000
10228 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
10229 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
10230 +#define YAFFS_TRACE_VERIFY_ALL 0x000f0000
10232 +#define YAFFS_TRACE_SYNC 0x00100000
10233 +#define YAFFS_TRACE_BACKGROUND 0x00200000
10234 +#define YAFFS_TRACE_LOCK 0x00400000
10235 +#define YAFFS_TRACE_MOUNT 0x00800000
10237 +#define YAFFS_TRACE_ERROR 0x40000000
10238 +#define YAFFS_TRACE_BUG 0x80000000
10239 +#define YAFFS_TRACE_ALWAYS 0xf0000000
10241 +#endif
10242 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_verify.c linux-3.12.20/fs/yaffs2/yaffs_verify.c
10243 --- linux-3.12.20.orig/fs/yaffs2/yaffs_verify.c 1970-01-01 01:00:00.000000000 +0100
10244 +++ linux-3.12.20/fs/yaffs2/yaffs_verify.c 2014-05-17 11:28:49.000000000 +0200
10245 @@ -0,0 +1,529 @@
10247 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
10249 + * Copyright (C) 2002-2011 Aleph One Ltd.
10250 + * for Toby Churchill Ltd and Brightstar Engineering
10252 + * Created by Charles Manning <charles@aleph1.co.uk>
10254 + * This program is free software; you can redistribute it and/or modify
10255 + * it under the terms of the GNU General Public License version 2 as
10256 + * published by the Free Software Foundation.
10257 + */
10259 +#include "yaffs_verify.h"
10260 +#include "yaffs_trace.h"
10261 +#include "yaffs_bitmap.h"
10262 +#include "yaffs_getblockinfo.h"
10263 +#include "yaffs_nand.h"
10265 +int yaffs_skip_verification(struct yaffs_dev *dev)
10267 + (void) dev;
10268 + return !(yaffs_trace_mask &
10269 + (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
10272 +static int yaffs_skip_full_verification(struct yaffs_dev *dev)
10274 + (void) dev;
10275 + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_FULL));
10278 +static int yaffs_skip_nand_verification(struct yaffs_dev *dev)
10280 + (void) dev;
10281 + return !(yaffs_trace_mask & (YAFFS_TRACE_VERIFY_NAND));
10284 +static const char * const block_state_name[] = {
10285 + "Unknown",
10286 + "Needs scan",
10287 + "Scanning",
10288 + "Empty",
10289 + "Allocating",
10290 + "Full",
10291 + "Dirty",
10292 + "Checkpoint",
10293 + "Collecting",
10294 + "Dead"
10297 +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi, int n)
10299 + int actually_used;
10300 + int in_use;
10302 + if (yaffs_skip_verification(dev))
10303 + return;
10305 + /* Report illegal runtime states */
10306 + if (bi->block_state >= YAFFS_NUMBER_OF_BLOCK_STATES)
10307 + yaffs_trace(YAFFS_TRACE_VERIFY,
10308 + "Block %d has undefined state %d",
10309 + n, bi->block_state);
10311 + switch (bi->block_state) {
10312 + case YAFFS_BLOCK_STATE_UNKNOWN:
10313 + case YAFFS_BLOCK_STATE_SCANNING:
10314 + case YAFFS_BLOCK_STATE_NEEDS_SCAN:
10315 + yaffs_trace(YAFFS_TRACE_VERIFY,
10316 + "Block %d has bad run-state %s",
10317 + n, block_state_name[bi->block_state]);
10320 + /* Check pages in use and soft deletions are legal */
10322 + actually_used = bi->pages_in_use - bi->soft_del_pages;
10324 + if (bi->pages_in_use < 0 ||
10325 + bi->pages_in_use > dev->param.chunks_per_block ||
10326 + bi->soft_del_pages < 0 ||
10327 + bi->soft_del_pages > dev->param.chunks_per_block ||
10328 + actually_used < 0 || actually_used > dev->param.chunks_per_block)
10329 + yaffs_trace(YAFFS_TRACE_VERIFY,
10330 + "Block %d has illegal values pages_in_used %d soft_del_pages %d",
10331 + n, bi->pages_in_use, bi->soft_del_pages);
10333 + /* Check chunk bitmap legal */
10334 + in_use = yaffs_count_chunk_bits(dev, n);
10335 + if (in_use != bi->pages_in_use)
10336 + yaffs_trace(YAFFS_TRACE_VERIFY,
10337 + "Block %d has inconsistent values pages_in_use %d counted chunk bits %d",
10338 + n, bi->pages_in_use, in_use);
10341 +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
10342 + struct yaffs_block_info *bi, int n)
10344 + yaffs_verify_blk(dev, bi, n);
10346 + /* After collection the block should be in the erased state */
10348 + if (bi->block_state != YAFFS_BLOCK_STATE_COLLECTING &&
10349 + bi->block_state != YAFFS_BLOCK_STATE_EMPTY) {
10350 + yaffs_trace(YAFFS_TRACE_ERROR,
10351 + "Block %d is in state %d after gc, should be erased",
10352 + n, bi->block_state);
10356 +void yaffs_verify_blocks(struct yaffs_dev *dev)
10358 + int i;
10359 + int state_count[YAFFS_NUMBER_OF_BLOCK_STATES];
10360 + int illegal_states = 0;
10362 + if (yaffs_skip_verification(dev))
10363 + return;
10365 + memset(state_count, 0, sizeof(state_count));
10367 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
10368 + struct yaffs_block_info *bi = yaffs_get_block_info(dev, i);
10369 + yaffs_verify_blk(dev, bi, i);
10371 + if (bi->block_state < YAFFS_NUMBER_OF_BLOCK_STATES)
10372 + state_count[bi->block_state]++;
10373 + else
10374 + illegal_states++;
10377 + yaffs_trace(YAFFS_TRACE_VERIFY, "Block summary");
10379 + yaffs_trace(YAFFS_TRACE_VERIFY,
10380 + "%d blocks have illegal states",
10381 + illegal_states);
10382 + if (state_count[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
10383 + yaffs_trace(YAFFS_TRACE_VERIFY,
10384 + "Too many allocating blocks");
10386 + for (i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
10387 + yaffs_trace(YAFFS_TRACE_VERIFY,
10388 + "%s %d blocks",
10389 + block_state_name[i], state_count[i]);
10391 + if (dev->blocks_in_checkpt != state_count[YAFFS_BLOCK_STATE_CHECKPOINT])
10392 + yaffs_trace(YAFFS_TRACE_VERIFY,
10393 + "Checkpoint block count wrong dev %d count %d",
10394 + dev->blocks_in_checkpt,
10395 + state_count[YAFFS_BLOCK_STATE_CHECKPOINT]);
10397 + if (dev->n_erased_blocks != state_count[YAFFS_BLOCK_STATE_EMPTY])
10398 + yaffs_trace(YAFFS_TRACE_VERIFY,
10399 + "Erased block count wrong dev %d count %d",
10400 + dev->n_erased_blocks,
10401 + state_count[YAFFS_BLOCK_STATE_EMPTY]);
10403 + if (state_count[YAFFS_BLOCK_STATE_COLLECTING] > 1)
10404 + yaffs_trace(YAFFS_TRACE_VERIFY,
10405 + "Too many collecting blocks %d (max is 1)",
10406 + state_count[YAFFS_BLOCK_STATE_COLLECTING]);
10410 + * Verify the object header. oh must be valid, but obj and tags may be NULL in
10411 + * which case those tests will not be performed.
10412 + */
10413 +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
10414 + struct yaffs_ext_tags *tags, int parent_check)
10416 + if (obj && yaffs_skip_verification(obj->my_dev))
10417 + return;
10419 + if (!(tags && obj && oh)) {
10420 + yaffs_trace(YAFFS_TRACE_VERIFY,
10421 + "Verifying object header tags %p obj %p oh %p",
10422 + tags, obj, oh);
10423 + return;
10426 + if (oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
10427 + oh->type > YAFFS_OBJECT_TYPE_MAX)
10428 + yaffs_trace(YAFFS_TRACE_VERIFY,
10429 + "Obj %d header type is illegal value 0x%x",
10430 + tags->obj_id, oh->type);
10432 + if (tags->obj_id != obj->obj_id)
10433 + yaffs_trace(YAFFS_TRACE_VERIFY,
10434 + "Obj %d header mismatch obj_id %d",
10435 + tags->obj_id, obj->obj_id);
10437 + /*
10438 + * Check that the object's parent ids match if parent_check requested.
10440 + * Tests do not apply to the root object.
10441 + */
10443 + if (parent_check && tags->obj_id > 1 && !obj->parent)
10444 + yaffs_trace(YAFFS_TRACE_VERIFY,
10445 + "Obj %d header mismatch parent_id %d obj->parent is NULL",
10446 + tags->obj_id, oh->parent_obj_id);
10448 + if (parent_check && obj->parent &&
10449 + oh->parent_obj_id != obj->parent->obj_id &&
10450 + (oh->parent_obj_id != YAFFS_OBJECTID_UNLINKED ||
10451 + obj->parent->obj_id != YAFFS_OBJECTID_DELETED))
10452 + yaffs_trace(YAFFS_TRACE_VERIFY,
10453 + "Obj %d header mismatch parent_id %d parent_obj_id %d",
10454 + tags->obj_id, oh->parent_obj_id,
10455 + obj->parent->obj_id);
10457 + if (tags->obj_id > 1 && oh->name[0] == 0) /* Null name */
10458 + yaffs_trace(YAFFS_TRACE_VERIFY,
10459 + "Obj %d header name is NULL",
10460 + obj->obj_id);
10462 + if (tags->obj_id > 1 && ((u8) (oh->name[0])) == 0xff) /* Junk name */
10463 + yaffs_trace(YAFFS_TRACE_VERIFY,
10464 + "Obj %d header name is 0xff",
10465 + obj->obj_id);
10468 +void yaffs_verify_file(struct yaffs_obj *obj)
10470 + u32 x;
10471 + int required_depth;
10472 + int actual_depth;
10473 + int last_chunk;
10474 + u32 offset_in_chunk;
10475 + u32 the_chunk;
10477 + u32 i;
10478 + struct yaffs_dev *dev;
10479 + struct yaffs_ext_tags tags;
10480 + struct yaffs_tnode *tn;
10481 + u32 obj_id;
10483 + if (!obj)
10484 + return;
10486 + if (yaffs_skip_verification(obj->my_dev))
10487 + return;
10489 + dev = obj->my_dev;
10490 + obj_id = obj->obj_id;
10493 + /* Check file size is consistent with tnode depth */
10494 + yaffs_addr_to_chunk(dev, obj->variant.file_variant.file_size,
10495 + &last_chunk, &offset_in_chunk);
10496 + last_chunk++;
10497 + x = last_chunk >> YAFFS_TNODES_LEVEL0_BITS;
10498 + required_depth = 0;
10499 + while (x > 0) {
10500 + x >>= YAFFS_TNODES_INTERNAL_BITS;
10501 + required_depth++;
10504 + actual_depth = obj->variant.file_variant.top_level;
10506 + /* Check that the chunks in the tnode tree are all correct.
10507 + * We do this by scanning through the tnode tree and
10508 + * checking the tags for every chunk match.
10509 + */
10511 + if (yaffs_skip_nand_verification(dev))
10512 + return;
10514 + for (i = 1; i <= last_chunk; i++) {
10515 + tn = yaffs_find_tnode_0(dev, &obj->variant.file_variant, i);
10517 + if (!tn)
10518 + continue;
10520 + the_chunk = yaffs_get_group_base(dev, tn, i);
10521 + if (the_chunk > 0) {
10522 + yaffs_rd_chunk_tags_nand(dev, the_chunk, NULL,
10523 + &tags);
10524 + if (tags.obj_id != obj_id || tags.chunk_id != i)
10525 + yaffs_trace(YAFFS_TRACE_VERIFY,
10526 + "Object %d chunk_id %d NAND mismatch chunk %d tags (%d:%d)",
10527 + obj_id, i, the_chunk,
10528 + tags.obj_id, tags.chunk_id);
10533 +void yaffs_verify_link(struct yaffs_obj *obj)
10535 + if (obj && yaffs_skip_verification(obj->my_dev))
10536 + return;
10538 + /* Verify sane equivalent object */
10541 +void yaffs_verify_symlink(struct yaffs_obj *obj)
10543 + if (obj && yaffs_skip_verification(obj->my_dev))
10544 + return;
10546 + /* Verify symlink string */
10549 +void yaffs_verify_special(struct yaffs_obj *obj)
10551 + if (obj && yaffs_skip_verification(obj->my_dev))
10552 + return;
10555 +void yaffs_verify_obj(struct yaffs_obj *obj)
10557 + struct yaffs_dev *dev;
10558 + u32 chunk_min;
10559 + u32 chunk_max;
10560 + u32 chunk_id_ok;
10561 + u32 chunk_in_range;
10562 + u32 chunk_wrongly_deleted;
10563 + u32 chunk_valid;
10565 + if (!obj)
10566 + return;
10568 + if (obj->being_created)
10569 + return;
10571 + dev = obj->my_dev;
10573 + if (yaffs_skip_verification(dev))
10574 + return;
10576 + /* Check sane object header chunk */
10578 + chunk_min = dev->internal_start_block * dev->param.chunks_per_block;
10579 + chunk_max =
10580 + (dev->internal_end_block + 1) * dev->param.chunks_per_block - 1;
10582 + chunk_in_range = (((unsigned)(obj->hdr_chunk)) >= chunk_min &&
10583 + ((unsigned)(obj->hdr_chunk)) <= chunk_max);
10584 + chunk_id_ok = chunk_in_range || (obj->hdr_chunk == 0);
10585 + chunk_valid = chunk_in_range &&
10586 + yaffs_check_chunk_bit(dev,
10587 + obj->hdr_chunk / dev->param.chunks_per_block,
10588 + obj->hdr_chunk % dev->param.chunks_per_block);
10589 + chunk_wrongly_deleted = chunk_in_range && !chunk_valid;
10591 + if (!obj->fake && (!chunk_id_ok || chunk_wrongly_deleted))
10592 + yaffs_trace(YAFFS_TRACE_VERIFY,
10593 + "Obj %d has chunk_id %d %s %s",
10594 + obj->obj_id, obj->hdr_chunk,
10595 + chunk_id_ok ? "" : ",out of range",
10596 + chunk_wrongly_deleted ? ",marked as deleted" : "");
10598 + if (chunk_valid && !yaffs_skip_nand_verification(dev)) {
10599 + struct yaffs_ext_tags tags;
10600 + struct yaffs_obj_hdr *oh;
10601 + u8 *buffer = yaffs_get_temp_buffer(dev);
10603 + oh = (struct yaffs_obj_hdr *)buffer;
10605 + yaffs_rd_chunk_tags_nand(dev, obj->hdr_chunk, buffer, &tags);
10607 + yaffs_verify_oh(obj, oh, &tags, 1);
10609 + yaffs_release_temp_buffer(dev, buffer);
10612 + /* Verify it has a parent */
10613 + if (obj && !obj->fake && (!obj->parent || obj->parent->my_dev != dev)) {
10614 + yaffs_trace(YAFFS_TRACE_VERIFY,
10615 + "Obj %d has parent pointer %p which does not look like an object",
10616 + obj->obj_id, obj->parent);
10619 + /* Verify parent is a directory */
10620 + if (obj->parent &&
10621 + obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
10622 + yaffs_trace(YAFFS_TRACE_VERIFY,
10623 + "Obj %d's parent is not a directory (type %d)",
10624 + obj->obj_id, obj->parent->variant_type);
10627 + switch (obj->variant_type) {
10628 + case YAFFS_OBJECT_TYPE_FILE:
10629 + yaffs_verify_file(obj);
10630 + break;
10631 + case YAFFS_OBJECT_TYPE_SYMLINK:
10632 + yaffs_verify_symlink(obj);
10633 + break;
10634 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10635 + yaffs_verify_dir(obj);
10636 + break;
10637 + case YAFFS_OBJECT_TYPE_HARDLINK:
10638 + yaffs_verify_link(obj);
10639 + break;
10640 + case YAFFS_OBJECT_TYPE_SPECIAL:
10641 + yaffs_verify_special(obj);
10642 + break;
10643 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10644 + default:
10645 + yaffs_trace(YAFFS_TRACE_VERIFY,
10646 + "Obj %d has illegaltype %d",
10647 + obj->obj_id, obj->variant_type);
10648 + break;
10652 +void yaffs_verify_objects(struct yaffs_dev *dev)
10654 + struct yaffs_obj *obj;
10655 + int i;
10656 + struct list_head *lh;
10658 + if (yaffs_skip_verification(dev))
10659 + return;
10661 + /* Iterate through the objects in each hash entry */
10663 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
10664 + list_for_each(lh, &dev->obj_bucket[i].list) {
10665 + obj = list_entry(lh, struct yaffs_obj, hash_link);
10666 + yaffs_verify_obj(obj);
10671 +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj)
10673 + struct list_head *lh;
10674 + struct yaffs_obj *list_obj;
10675 + int count = 0;
10677 + if (!obj) {
10678 + yaffs_trace(YAFFS_TRACE_ALWAYS, "No object to verify");
10679 + BUG();
10680 + return;
10683 + if (yaffs_skip_verification(obj->my_dev))
10684 + return;
10686 + if (!obj->parent) {
10687 + yaffs_trace(YAFFS_TRACE_ALWAYS, "Object does not have parent");
10688 + BUG();
10689 + return;
10692 + if (obj->parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
10693 + yaffs_trace(YAFFS_TRACE_ALWAYS, "Parent is not directory");
10694 + BUG();
10697 + /* Iterate through the objects in each hash entry */
10699 + list_for_each(lh, &obj->parent->variant.dir_variant.children) {
10700 + list_obj = list_entry(lh, struct yaffs_obj, siblings);
10701 + yaffs_verify_obj(list_obj);
10702 + if (obj == list_obj)
10703 + count++;
10706 + if (count != 1) {
10707 + yaffs_trace(YAFFS_TRACE_ALWAYS,
10708 + "Object in directory %d times",
10709 + count);
10710 + BUG();
10714 +void yaffs_verify_dir(struct yaffs_obj *directory)
10716 + struct list_head *lh;
10717 + struct yaffs_obj *list_obj;
10719 + if (!directory) {
10720 + BUG();
10721 + return;
10724 + if (yaffs_skip_full_verification(directory->my_dev))
10725 + return;
10727 + if (directory->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
10728 + yaffs_trace(YAFFS_TRACE_ALWAYS,
10729 + "Directory has wrong type: %d",
10730 + directory->variant_type);
10731 + BUG();
10734 + /* Iterate through the objects in each hash entry */
10736 + list_for_each(lh, &directory->variant.dir_variant.children) {
10737 + list_obj = list_entry(lh, struct yaffs_obj, siblings);
10738 + if (list_obj->parent != directory) {
10739 + yaffs_trace(YAFFS_TRACE_ALWAYS,
10740 + "Object in directory list has wrong parent %p",
10741 + list_obj->parent);
10742 + BUG();
10744 + yaffs_verify_obj_in_dir(list_obj);
10748 +static int yaffs_free_verification_failures;
10750 +void yaffs_verify_free_chunks(struct yaffs_dev *dev)
10752 + int counted;
10753 + int difference;
10755 + if (yaffs_skip_verification(dev))
10756 + return;
10758 + counted = yaffs_count_free_chunks(dev);
10760 + difference = dev->n_free_chunks - counted;
10762 + if (difference) {
10763 + yaffs_trace(YAFFS_TRACE_ALWAYS,
10764 + "Freechunks verification failure %d %d %d",
10765 + dev->n_free_chunks, counted, difference);
10766 + yaffs_free_verification_failures++;
10770 +int yaffs_verify_file_sane(struct yaffs_obj *in)
10772 + (void) in;
10773 + return YAFFS_OK;
10775 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_verify.h linux-3.12.20/fs/yaffs2/yaffs_verify.h
10776 --- linux-3.12.20.orig/fs/yaffs2/yaffs_verify.h 1970-01-01 01:00:00.000000000 +0100
10777 +++ linux-3.12.20/fs/yaffs2/yaffs_verify.h 2014-05-17 11:28:49.000000000 +0200
10778 @@ -0,0 +1,43 @@
10780 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
10782 + * Copyright (C) 2002-2011 Aleph One Ltd.
10783 + * for Toby Churchill Ltd and Brightstar Engineering
10785 + * Created by Charles Manning <charles@aleph1.co.uk>
10787 + * This program is free software; you can redistribute it and/or modify
10788 + * it under the terms of the GNU Lesser General Public License version 2.1 as
10789 + * published by the Free Software Foundation.
10791 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
10792 + */
10794 +#ifndef __YAFFS_VERIFY_H__
10795 +#define __YAFFS_VERIFY_H__
10797 +#include "yaffs_guts.h"
10799 +void yaffs_verify_blk(struct yaffs_dev *dev, struct yaffs_block_info *bi,
10800 + int n);
10801 +void yaffs_verify_collected_blk(struct yaffs_dev *dev,
10802 + struct yaffs_block_info *bi, int n);
10803 +void yaffs_verify_blocks(struct yaffs_dev *dev);
10805 +void yaffs_verify_oh(struct yaffs_obj *obj, struct yaffs_obj_hdr *oh,
10806 + struct yaffs_ext_tags *tags, int parent_check);
10807 +void yaffs_verify_file(struct yaffs_obj *obj);
10808 +void yaffs_verify_link(struct yaffs_obj *obj);
10809 +void yaffs_verify_symlink(struct yaffs_obj *obj);
10810 +void yaffs_verify_special(struct yaffs_obj *obj);
10811 +void yaffs_verify_obj(struct yaffs_obj *obj);
10812 +void yaffs_verify_objects(struct yaffs_dev *dev);
10813 +void yaffs_verify_obj_in_dir(struct yaffs_obj *obj);
10814 +void yaffs_verify_dir(struct yaffs_obj *directory);
10815 +void yaffs_verify_free_chunks(struct yaffs_dev *dev);
10817 +int yaffs_verify_file_sane(struct yaffs_obj *obj);
10819 +int yaffs_skip_verification(struct yaffs_dev *dev);
10821 +#endif
10822 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_vfs.c linux-3.12.20/fs/yaffs2/yaffs_vfs.c
10823 --- linux-3.12.20.orig/fs/yaffs2/yaffs_vfs.c 1970-01-01 01:00:00.000000000 +0100
10824 +++ linux-3.12.20/fs/yaffs2/yaffs_vfs.c 2014-05-17 11:28:49.000000000 +0200
10825 @@ -0,0 +1,3600 @@
10827 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
10829 + * Copyright (C) 2002-2011 Aleph One Ltd.
10830 + * for Toby Churchill Ltd and Brightstar Engineering
10832 + * Created by Charles Manning <charles@aleph1.co.uk>
10833 + * Acknowledgements:
10834 + * Luc van OostenRyck for numerous patches.
10835 + * Nick Bane for numerous patches.
10836 + * Nick Bane for 2.5/2.6 integration.
10837 + * Andras Toth for mknod rdev issue.
10838 + * Michael Fischer for finding the problem with inode inconsistency.
10839 + * Some code bodily lifted from JFFS
10841 + * This program is free software; you can redistribute it and/or modify
10842 + * it under the terms of the GNU General Public License version 2 as
10843 + * published by the Free Software Foundation.
10844 + */
10848 + * This is the file system front-end to YAFFS that hooks it up to
10849 + * the VFS.
10851 + * Special notes:
10852 + * >> 2.4: sb->u.generic_sbp points to the struct yaffs_dev associated with
10853 + * this superblock
10854 + * >> 2.6: sb->s_fs_info points to the struct yaffs_dev associated with this
10855 + * superblock
10856 + * >> inode->u.generic_ip points to the associated struct yaffs_obj.
10857 + */
10860 + * There are two variants of the VFS glue code. This variant should compile
10861 + * for any version of Linux.
10862 + */
10863 +#include <linux/version.h>
10865 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 10))
10866 +#define YAFFS_COMPILE_BACKGROUND
10867 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23))
10868 +#define YAFFS_COMPILE_FREEZER
10869 +#endif
10870 +#endif
10872 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
10873 +#define YAFFS_COMPILE_EXPORTFS
10874 +#endif
10876 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
10877 +#define YAFFS_USE_SETATTR_COPY
10878 +#define YAFFS_USE_TRUNCATE_SETSIZE
10879 +#endif
10880 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35))
10881 +#define YAFFS_HAS_EVICT_INODE
10882 +#endif
10884 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
10885 +#define YAFFS_NEW_FOLLOW_LINK 1
10886 +#else
10887 +#define YAFFS_NEW_FOLLOW_LINK 0
10888 +#endif
10890 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
10891 +#define YAFFS_HAS_WRITE_SUPER
10892 +#endif
10895 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
10896 +#include <linux/config.h>
10897 +#endif
10899 +#include <linux/kernel.h>
10900 +#include <linux/module.h>
10901 +#include <linux/slab.h>
10902 +#include <linux/init.h>
10903 +#include <linux/fs.h>
10904 +#include <linux/proc_fs.h>
10905 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 39))
10906 +#include <linux/smp_lock.h>
10907 +#endif
10908 +#include <linux/pagemap.h>
10909 +#include <linux/mtd/mtd.h>
10910 +#include <linux/interrupt.h>
10911 +#include <linux/string.h>
10912 +#include <linux/ctype.h>
10914 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
10915 +#include <linux/namei.h>
10916 +#endif
10918 +#ifdef YAFFS_COMPILE_EXPORTFS
10919 +#include <linux/exportfs.h>
10920 +#endif
10922 +#ifdef YAFFS_COMPILE_BACKGROUND
10923 +#include <linux/kthread.h>
10924 +#include <linux/delay.h>
10925 +#endif
10926 +#ifdef YAFFS_COMPILE_FREEZER
10927 +#include <linux/freezer.h>
10928 +#endif
10930 +#include <asm/div64.h>
10932 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
10934 +#include <linux/statfs.h>
10936 +#define UnlockPage(p) unlock_page(p)
10937 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
10939 +/* FIXME: use sb->s_id instead ? */
10940 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
10942 +#else
10944 +#include <linux/locks.h>
10945 +#define BDEVNAME_SIZE 0
10946 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
10948 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0))
10949 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
10950 +#define __user
10951 +#endif
10953 +#endif
10955 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
10956 +#define YPROC_ROOT (&proc_root)
10957 +#else
10958 +#define YPROC_ROOT NULL
10959 +#endif
10961 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26))
10962 +#define Y_INIT_TIMER(a) init_timer(a)
10963 +#else
10964 +#define Y_INIT_TIMER(a) init_timer_on_stack(a)
10965 +#endif
10967 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 27))
10968 +#define YAFFS_USE_WRITE_BEGIN_END 1
10969 +#else
10970 +#define YAFFS_USE_WRITE_BEGIN_END 0
10971 +#endif
10973 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 6, 0))
10974 +#define YAFFS_SUPER_HAS_DIRTY
10975 +#endif
10978 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0))
10979 +#define set_nlink(inode, count) do { (inode)->i_nlink = (count); } while(0)
10980 +#endif
10982 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 28))
10983 +static uint32_t YCALCBLOCKS(uint64_t partition_size, uint32_t block_size)
10985 + uint64_t result = partition_size;
10986 + do_div(result, block_size);
10987 + return (uint32_t) result;
10989 +#else
10990 +#define YCALCBLOCKS(s, b) ((s)/(b))
10991 +#endif
10993 +#include <linux/uaccess.h>
10994 +#include <linux/mtd/mtd.h>
10996 +#include "yportenv.h"
10997 +#include "yaffs_trace.h"
10998 +#include "yaffs_guts.h"
10999 +#include "yaffs_attribs.h"
11001 +#include "yaffs_linux.h"
11003 +#include "yaffs_mtdif.h"
11004 +#include "yaffs_packedtags2.h"
11005 +#include "yaffs_getblockinfo.h"
11007 +unsigned int yaffs_trace_mask =
11008 + YAFFS_TRACE_BAD_BLOCKS |
11009 + YAFFS_TRACE_ALWAYS |
11010 + 0;
11012 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
11013 +unsigned int yaffs_auto_checkpoint = 1;
11014 +unsigned int yaffs_gc_control = 1;
11015 +unsigned int yaffs_bg_enable = 1;
11016 +unsigned int yaffs_auto_select = 1;
11017 +/* Module Parameters */
11018 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
11019 +module_param(yaffs_trace_mask, uint, 0644);
11020 +module_param(yaffs_wr_attempts, uint, 0644);
11021 +module_param(yaffs_auto_checkpoint, uint, 0644);
11022 +module_param(yaffs_gc_control, uint, 0644);
11023 +module_param(yaffs_bg_enable, uint, 0644);
11024 +#else
11025 +MODULE_PARM(yaffs_trace_mask, "i");
11026 +MODULE_PARM(yaffs_wr_attempts, "i");
11027 +MODULE_PARM(yaffs_auto_checkpoint, "i");
11028 +MODULE_PARM(yaffs_gc_control, "i");
11029 +#endif
11031 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
11032 +/* use iget and read_inode */
11033 +#define Y_IGET(sb, inum) iget((sb), (inum))
11035 +#else
11036 +/* Call local equivalent */
11037 +#define YAFFS_USE_OWN_IGET
11038 +#define Y_IGET(sb, inum) yaffs_iget((sb), (inum))
11040 +#endif
11042 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
11043 +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->i_private)
11044 +#else
11045 +#define yaffs_inode_to_obj_lv(iptr) ((iptr)->u.generic_ip)
11046 +#endif
11048 +#define yaffs_inode_to_obj(iptr) \
11049 + ((struct yaffs_obj *)(yaffs_inode_to_obj_lv(iptr)))
11050 +#define yaffs_dentry_to_obj(dptr) yaffs_inode_to_obj((dptr)->d_inode)
11052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
11053 +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->s_fs_info)
11054 +#else
11055 +#define yaffs_super_to_dev(sb) ((struct yaffs_dev *)sb->u.generic_sbp)
11056 +#endif
11058 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
11059 +#define Y_CLEAR_INODE(i) clear_inode(i)
11060 +#else
11061 +#define Y_CLEAR_INODE(i) end_writeback(i)
11062 +#endif
11065 +#define update_dir_time(dir) do {\
11066 + (dir)->i_ctime = (dir)->i_mtime = CURRENT_TIME; \
11067 + } while (0)
11069 +static void yaffs_fill_inode_from_obj(struct inode *inode,
11070 + struct yaffs_obj *obj);
11073 +static void yaffs_gross_lock(struct yaffs_dev *dev)
11075 + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locking %p", current);
11076 + mutex_lock(&(yaffs_dev_to_lc(dev)->gross_lock));
11077 + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs locked %p", current);
11080 +static void yaffs_gross_unlock(struct yaffs_dev *dev)
11082 + yaffs_trace(YAFFS_TRACE_LOCK, "yaffs unlocking %p", current);
11083 + mutex_unlock(&(yaffs_dev_to_lc(dev)->gross_lock));
11087 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
11089 + /* Lifted from jffs2 */
11091 + struct yaffs_obj *obj;
11092 + unsigned char *pg_buf;
11093 + int ret;
11094 + loff_t pos = ((loff_t) pg->index) << PAGE_CACHE_SHIFT;
11095 + struct yaffs_dev *dev;
11097 + yaffs_trace(YAFFS_TRACE_OS,
11098 + "yaffs_readpage_nolock at %lld, size %08x",
11099 + (long long)pos,
11100 + (unsigned)PAGE_CACHE_SIZE);
11102 + obj = yaffs_dentry_to_obj(f->f_dentry);
11104 + dev = obj->my_dev;
11106 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
11107 + BUG_ON(!PageLocked(pg));
11108 +#else
11109 + if (!PageLocked(pg))
11110 + PAGE_BUG(pg);
11111 +#endif
11113 + pg_buf = kmap(pg);
11114 + /* FIXME: Can kmap fail? */
11116 + yaffs_gross_lock(dev);
11118 + ret = yaffs_file_rd(obj, pg_buf, pos, PAGE_CACHE_SIZE);
11120 + yaffs_gross_unlock(dev);
11122 + if (ret >= 0)
11123 + ret = 0;
11125 + if (ret) {
11126 + ClearPageUptodate(pg);
11127 + SetPageError(pg);
11128 + } else {
11129 + SetPageUptodate(pg);
11130 + ClearPageError(pg);
11133 + flush_dcache_page(pg);
11134 + kunmap(pg);
11136 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage_nolock done");
11137 + return ret;
11140 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
11142 + int ret = yaffs_readpage_nolock(f, pg);
11143 + UnlockPage(pg);
11144 + return ret;
11147 +static int yaffs_readpage(struct file *f, struct page *pg)
11149 + int ret;
11151 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage");
11152 + ret = yaffs_readpage_unlock(f, pg);
11153 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_readpage done");
11154 + return ret;
11157 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0))
11158 +#define YCRED_FSUID() from_kuid(&init_user_ns, current_fsuid())
11159 +#define YCRED_FSGID() from_kgid(&init_user_ns, current_fsgid())
11160 +#else
11161 +#define YCRED_FSUID() YCRED(current)->fsuid
11162 +#define YCRED_FSGID() YCRED(current)->fsgid
11164 +static inline uid_t i_uid_read(const struct inode *inode)
11166 + return inode->i_uid;
11169 +static inline gid_t i_gid_read(const struct inode *inode)
11171 + return inode->i_gid;
11174 +static inline void i_uid_write(struct inode *inode, uid_t uid)
11176 + inode->i_uid = uid;
11179 +static inline void i_gid_write(struct inode *inode, gid_t gid)
11181 + inode->i_gid = gid;
11183 +#endif
11185 +static void yaffs_set_super_dirty_val(struct yaffs_dev *dev, int val)
11187 + struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
11189 + if (lc)
11190 + lc->dirty = val;
11192 +# ifdef YAFFS_SUPER_HAS_DIRTY
11194 + struct super_block *sb = lc->super;
11196 + if (sb)
11197 + sb->s_dirt = val;
11199 +#endif
11203 +static void yaffs_set_super_dirty(struct yaffs_dev *dev)
11205 + yaffs_set_super_dirty_val(dev, 1);
11208 +static void yaffs_clear_super_dirty(struct yaffs_dev *dev)
11210 + yaffs_set_super_dirty_val(dev, 0);
11213 +static int yaffs_check_super_dirty(struct yaffs_dev *dev)
11215 + struct yaffs_linux_context *lc = yaffs_dev_to_lc(dev);
11217 + if (lc && lc->dirty)
11218 + return 1;
11220 +# ifdef YAFFS_SUPER_HAS_DIRTY
11222 + struct super_block *sb = lc->super;
11224 + if (sb && sb->s_dirt)
11225 + return 1;
11227 +#endif
11228 + return 0;
11232 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
11233 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
11234 +#else
11235 +static int yaffs_writepage(struct page *page)
11236 +#endif
11238 + struct yaffs_dev *dev;
11239 + struct address_space *mapping = page->mapping;
11240 + struct inode *inode;
11241 + unsigned long end_index;
11242 + char *buffer;
11243 + struct yaffs_obj *obj;
11244 + int n_written = 0;
11245 + unsigned n_bytes;
11246 + loff_t i_size;
11248 + if (!mapping)
11249 + BUG();
11250 + inode = mapping->host;
11251 + if (!inode)
11252 + BUG();
11253 + i_size = i_size_read(inode);
11255 + end_index = i_size >> PAGE_CACHE_SHIFT;
11257 + if (page->index < end_index)
11258 + n_bytes = PAGE_CACHE_SIZE;
11259 + else {
11260 + n_bytes = i_size & (PAGE_CACHE_SIZE - 1);
11262 + if (page->index > end_index || !n_bytes) {
11263 + yaffs_trace(YAFFS_TRACE_OS,
11264 + "yaffs_writepage at %lld, inode size = %lld!!",
11265 + ((loff_t)page->index) << PAGE_CACHE_SHIFT,
11266 + inode->i_size);
11267 + yaffs_trace(YAFFS_TRACE_OS,
11268 + " -> don't care!!");
11270 + zero_user_segment(page, 0, PAGE_CACHE_SIZE);
11271 + set_page_writeback(page);
11272 + unlock_page(page);
11273 + end_page_writeback(page);
11274 + return 0;
11278 + if (n_bytes != PAGE_CACHE_SIZE)
11279 + zero_user_segment(page, n_bytes, PAGE_CACHE_SIZE);
11281 + get_page(page);
11283 + buffer = kmap(page);
11285 + obj = yaffs_inode_to_obj(inode);
11286 + dev = obj->my_dev;
11287 + yaffs_gross_lock(dev);
11289 + yaffs_trace(YAFFS_TRACE_OS,
11290 + "yaffs_writepage at %lld, size %08x",
11291 + ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes);
11292 + yaffs_trace(YAFFS_TRACE_OS,
11293 + "writepag0: obj = %lld, ino = %lld",
11294 + obj->variant.file_variant.file_size, inode->i_size);
11296 + n_written = yaffs_wr_file(obj, buffer,
11297 + ((loff_t)page->index) << PAGE_CACHE_SHIFT, n_bytes, 0);
11299 + yaffs_set_super_dirty(dev);
11301 + yaffs_trace(YAFFS_TRACE_OS,
11302 + "writepag1: obj = %lld, ino = %lld",
11303 + obj->variant.file_variant.file_size, inode->i_size);
11305 + yaffs_gross_unlock(dev);
11307 + kunmap(page);
11308 + set_page_writeback(page);
11309 + unlock_page(page);
11310 + end_page_writeback(page);
11311 + put_page(page);
11313 + return (n_written == n_bytes) ? 0 : -ENOSPC;
11316 +/* Space holding and freeing is done to ensure we have space available for write_begin/end */
11317 +/* For now we just assume few parallel writes and check against a small number. */
11318 +/* Todo: need to do this with a counter to handle parallel reads better */
11320 +static ssize_t yaffs_hold_space(struct file *f)
11322 + struct yaffs_obj *obj;
11323 + struct yaffs_dev *dev;
11325 + int n_free_chunks;
11327 + obj = yaffs_dentry_to_obj(f->f_dentry);
11329 + dev = obj->my_dev;
11331 + yaffs_gross_lock(dev);
11333 + n_free_chunks = yaffs_get_n_free_chunks(dev);
11335 + yaffs_gross_unlock(dev);
11337 + return (n_free_chunks > 20) ? 1 : 0;
11340 +static void yaffs_release_space(struct file *f)
11342 + struct yaffs_obj *obj;
11343 + struct yaffs_dev *dev;
11345 + obj = yaffs_dentry_to_obj(f->f_dentry);
11347 + dev = obj->my_dev;
11349 + yaffs_gross_lock(dev);
11351 + yaffs_gross_unlock(dev);
11354 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
11355 +static int yaffs_write_begin(struct file *filp, struct address_space *mapping,
11356 + loff_t pos, unsigned len, unsigned flags,
11357 + struct page **pagep, void **fsdata)
11359 + struct page *pg = NULL;
11360 + pgoff_t index = pos >> PAGE_CACHE_SHIFT;
11362 + int ret = 0;
11363 + int space_held = 0;
11365 + /* Get a page */
11366 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
11367 + pg = grab_cache_page_write_begin(mapping, index, flags);
11368 +#else
11369 + pg = __grab_cache_page(mapping, index);
11370 +#endif
11372 + *pagep = pg;
11373 + if (!pg) {
11374 + ret = -ENOMEM;
11375 + goto out;
11377 + yaffs_trace(YAFFS_TRACE_OS,
11378 + "start yaffs_write_begin index %d(%x) uptodate %d",
11379 + (int)index, (int)index, Page_Uptodate(pg) ? 1 : 0);
11381 + /* Get fs space */
11382 + space_held = yaffs_hold_space(filp);
11384 + if (!space_held) {
11385 + ret = -ENOSPC;
11386 + goto out;
11389 + /* Update page if required */
11391 + if (!Page_Uptodate(pg))
11392 + ret = yaffs_readpage_nolock(filp, pg);
11394 + if (ret)
11395 + goto out;
11397 + /* Happy path return */
11398 + yaffs_trace(YAFFS_TRACE_OS, "end yaffs_write_begin - ok");
11400 + return 0;
11402 +out:
11403 + yaffs_trace(YAFFS_TRACE_OS,
11404 + "end yaffs_write_begin fail returning %d", ret);
11405 + if (space_held)
11406 + yaffs_release_space(filp);
11407 + if (pg) {
11408 + unlock_page(pg);
11409 + page_cache_release(pg);
11411 + return ret;
11414 +#else
11416 +static int yaffs_prepare_write(struct file *f, struct page *pg,
11417 + unsigned offset, unsigned to)
11419 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_prepair_write");
11421 + if (!Page_Uptodate(pg))
11422 + return yaffs_readpage_nolock(f, pg);
11423 + return 0;
11425 +#endif
11428 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
11429 + loff_t * pos)
11431 + struct yaffs_obj *obj;
11432 + int n_written;
11433 + loff_t ipos;
11434 + struct inode *inode;
11435 + struct yaffs_dev *dev;
11437 + obj = yaffs_dentry_to_obj(f->f_dentry);
11439 + if (!obj) {
11440 + yaffs_trace(YAFFS_TRACE_OS,
11441 + "yaffs_file_write: hey obj is null!");
11442 + return -EINVAL;
11445 + dev = obj->my_dev;
11447 + yaffs_gross_lock(dev);
11449 + inode = f->f_dentry->d_inode;
11451 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND)
11452 + ipos = inode->i_size;
11453 + else
11454 + ipos = *pos;
11456 + yaffs_trace(YAFFS_TRACE_OS,
11457 + "yaffs_file_write about to write writing %u(%x) bytes to object %d at %lld",
11458 + (unsigned)n, (unsigned)n, obj->obj_id, ipos);
11460 + n_written = yaffs_wr_file(obj, buf, ipos, n, 0);
11462 + yaffs_set_super_dirty(dev);
11464 + yaffs_trace(YAFFS_TRACE_OS,
11465 + "yaffs_file_write: %d(%x) bytes written",
11466 + (unsigned)n, (unsigned)n);
11468 + if (n_written > 0) {
11469 + ipos += n_written;
11470 + *pos = ipos;
11471 + if (ipos > inode->i_size) {
11472 + inode->i_size = ipos;
11473 + inode->i_blocks = (ipos + 511) >> 9;
11475 + yaffs_trace(YAFFS_TRACE_OS,
11476 + "yaffs_file_write size updated to %lld bytes, %d blocks",
11477 + ipos, (int)(inode->i_blocks));
11481 + yaffs_gross_unlock(dev);
11482 + return (n_written == 0) && (n > 0) ? -ENOSPC : n_written;
11486 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
11487 +static int yaffs_write_end(struct file *filp, struct address_space *mapping,
11488 + loff_t pos, unsigned len, unsigned copied,
11489 + struct page *pg, void *fsdadata)
11491 + int ret = 0;
11492 + void *addr, *kva;
11493 + uint32_t offset_into_page = pos & (PAGE_CACHE_SIZE - 1);
11495 + kva = kmap(pg);
11496 + addr = kva + offset_into_page;
11498 + yaffs_trace(YAFFS_TRACE_OS,
11499 + "yaffs_write_end addr %p pos %lld n_bytes %d",
11500 + addr, pos, copied);
11502 + ret = yaffs_file_write(filp, addr, copied, &pos);
11504 + if (ret != copied) {
11505 + yaffs_trace(YAFFS_TRACE_OS,
11506 + "yaffs_write_end not same size ret %d copied %d",
11507 + ret, copied);
11508 + SetPageError(pg);
11511 + kunmap(pg);
11513 + yaffs_release_space(filp);
11514 + unlock_page(pg);
11515 + page_cache_release(pg);
11516 + return ret;
11518 +#else
11520 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
11521 + unsigned to)
11523 + void *addr, *kva;
11525 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
11526 + int n_bytes = to - offset;
11527 + int n_written;
11529 + kva = kmap(pg);
11530 + addr = kva + offset;
11532 + yaffs_trace(YAFFS_TRACE_OS,
11533 + "yaffs_commit_write addr %p pos %lld n_bytes %d",
11534 + addr, pos, n_bytes);
11536 + n_written = yaffs_file_write(f, addr, n_bytes, &pos);
11538 + if (n_written != n_bytes) {
11539 + yaffs_trace(YAFFS_TRACE_OS,
11540 + "yaffs_commit_write not same size n_written %d n_bytes %d",
11541 + n_written, n_bytes);
11542 + SetPageError(pg);
11544 + kunmap(pg);
11546 + yaffs_trace(YAFFS_TRACE_OS,
11547 + "yaffs_commit_write returning %d",
11548 + n_written == n_bytes ? 0 : n_written);
11550 + return n_written == n_bytes ? 0 : n_written;
11552 +#endif
11554 +static struct address_space_operations yaffs_file_address_operations = {
11555 + .readpage = yaffs_readpage,
11556 + .writepage = yaffs_writepage,
11557 +#if (YAFFS_USE_WRITE_BEGIN_END > 0)
11558 + .write_begin = yaffs_write_begin,
11559 + .write_end = yaffs_write_end,
11560 +#else
11561 + .prepare_write = yaffs_prepare_write,
11562 + .commit_write = yaffs_commit_write,
11563 +#endif
11567 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
11568 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
11569 +#else
11570 +static int yaffs_file_flush(struct file *file)
11571 +#endif
11573 + struct yaffs_obj *obj = yaffs_dentry_to_obj(file->f_dentry);
11575 + struct yaffs_dev *dev = obj->my_dev;
11577 + yaffs_trace(YAFFS_TRACE_OS,
11578 + "yaffs_file_flush object %d (%s)",
11579 + obj->obj_id,
11580 + obj->dirty ? "dirty" : "clean");
11582 + yaffs_gross_lock(dev);
11584 + yaffs_flush_file(obj, 1, 0);
11586 + yaffs_gross_unlock(dev);
11588 + return 0;
11592 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
11593 +static int yaffs_sync_object(struct file *file, loff_t start, loff_t end, int datasync)
11594 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
11595 +static int yaffs_sync_object(struct file *file, int datasync)
11596 +#else
11597 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
11598 + int datasync)
11599 +#endif
11601 + struct yaffs_obj *obj;
11602 + struct yaffs_dev *dev;
11603 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 34))
11604 + struct dentry *dentry = file->f_path.dentry;
11605 +#endif
11607 + obj = yaffs_dentry_to_obj(dentry);
11609 + dev = obj->my_dev;
11611 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
11612 + "yaffs_sync_object");
11613 + yaffs_gross_lock(dev);
11614 + yaffs_flush_file(obj, 1, datasync);
11615 + yaffs_gross_unlock(dev);
11616 + return 0;
11620 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 22))
11621 +static const struct file_operations yaffs_file_operations = {
11622 + .read = do_sync_read,
11623 + .write = do_sync_write,
11624 + .aio_read = generic_file_aio_read,
11625 + .aio_write = generic_file_aio_write,
11626 + .mmap = generic_file_mmap,
11627 + .flush = yaffs_file_flush,
11628 + .fsync = yaffs_sync_object,
11629 + .splice_read = generic_file_splice_read,
11630 + .splice_write = generic_file_splice_write,
11631 + .llseek = generic_file_llseek,
11634 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18))
11636 +static const struct file_operations yaffs_file_operations = {
11637 + .read = do_sync_read,
11638 + .write = do_sync_write,
11639 + .aio_read = generic_file_aio_read,
11640 + .aio_write = generic_file_aio_write,
11641 + .mmap = generic_file_mmap,
11642 + .flush = yaffs_file_flush,
11643 + .fsync = yaffs_sync_object,
11644 + .sendfile = generic_file_sendfile,
11647 +#else
11649 +static const struct file_operations yaffs_file_operations = {
11650 + .read = generic_file_read,
11651 + .write = generic_file_write,
11652 + .mmap = generic_file_mmap,
11653 + .flush = yaffs_file_flush,
11654 + .fsync = yaffs_sync_object,
11655 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
11656 + .sendfile = generic_file_sendfile,
11657 +#endif
11659 +#endif
11664 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25))
11665 +static void zero_user_segment(struct page *page, unsigned start, unsigned end)
11667 + void *kaddr = kmap_atomic(page, KM_USER0);
11668 + memset(kaddr + start, 0, end - start);
11669 + kunmap_atomic(kaddr, KM_USER0);
11670 + flush_dcache_page(page);
11672 +#endif
11675 +static int yaffs_vfs_setsize(struct inode *inode, loff_t newsize)
11677 +#ifdef YAFFS_USE_TRUNCATE_SETSIZE
11678 + truncate_setsize(inode, newsize);
11679 + return 0;
11680 +#else
11681 + truncate_inode_pages(&inode->i_data, newsize);
11682 + return 0;
11683 +#endif
11688 +static int yaffs_vfs_setattr(struct inode *inode, struct iattr *attr)
11690 +#ifdef YAFFS_USE_SETATTR_COPY
11691 + setattr_copy(inode, attr);
11692 + return 0;
11693 +#else
11694 + return inode_setattr(inode, attr);
11695 +#endif
11699 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
11701 + struct inode *inode = dentry->d_inode;
11702 + int error = 0;
11703 + struct yaffs_dev *dev;
11705 + yaffs_trace(YAFFS_TRACE_OS,
11706 + "yaffs_setattr of object %d",
11707 + yaffs_inode_to_obj(inode)->obj_id);
11708 +#if 0
11709 + /* Fail if a requested resize >= 2GB */
11710 + if (attr->ia_valid & ATTR_SIZE && (attr->ia_size >> 31))
11711 + error = -EINVAL;
11712 +#endif
11714 + if (error == 0)
11715 + error = inode_change_ok(inode, attr);
11716 + if (error == 0) {
11717 + int result;
11718 + if (!error) {
11719 + error = yaffs_vfs_setattr(inode, attr);
11720 + yaffs_trace(YAFFS_TRACE_OS, "inode_setattr called");
11721 + if (attr->ia_valid & ATTR_SIZE) {
11722 + yaffs_vfs_setsize(inode, attr->ia_size);
11723 + inode->i_blocks = (inode->i_size + 511) >> 9;
11726 + dev = yaffs_inode_to_obj(inode)->my_dev;
11727 + if (attr->ia_valid & ATTR_SIZE) {
11728 + yaffs_trace(YAFFS_TRACE_OS,
11729 + "resize to %d(%x)",
11730 + (int)(attr->ia_size),
11731 + (int)(attr->ia_size));
11733 + yaffs_gross_lock(dev);
11734 + result = yaffs_set_attribs(yaffs_inode_to_obj(inode), attr);
11735 + if (result == YAFFS_OK) {
11736 + error = 0;
11737 + } else {
11738 + error = -EPERM;
11740 + yaffs_gross_unlock(dev);
11744 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setattr done returning %d", error);
11746 + return error;
11749 +static int yaffs_setxattr(struct dentry *dentry, const char *name,
11750 + const void *value, size_t size, int flags)
11752 + struct inode *inode = dentry->d_inode;
11753 + int error = 0;
11754 + struct yaffs_dev *dev;
11755 + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
11757 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr of object %d", obj->obj_id);
11759 + if (error == 0) {
11760 + int result;
11761 + dev = obj->my_dev;
11762 + yaffs_gross_lock(dev);
11763 + result = yaffs_set_xattrib(obj, name, value, size, flags);
11764 + if (result == YAFFS_OK)
11765 + error = 0;
11766 + else if (result < 0)
11767 + error = result;
11768 + yaffs_gross_unlock(dev);
11771 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_setxattr done returning %d", error);
11773 + return error;
11776 +static ssize_t yaffs_getxattr(struct dentry * dentry, const char *name,
11777 + void *buff, size_t size)
11779 + struct inode *inode = dentry->d_inode;
11780 + int error = 0;
11781 + struct yaffs_dev *dev;
11782 + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
11784 + yaffs_trace(YAFFS_TRACE_OS,
11785 + "yaffs_getxattr \"%s\" from object %d",
11786 + name, obj->obj_id);
11788 + if (error == 0) {
11789 + dev = obj->my_dev;
11790 + yaffs_gross_lock(dev);
11791 + error = yaffs_get_xattrib(obj, name, buff, size);
11792 + yaffs_gross_unlock(dev);
11795 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_getxattr done returning %d", error);
11797 + return error;
11800 +static int yaffs_removexattr(struct dentry *dentry, const char *name)
11802 + struct inode *inode = dentry->d_inode;
11803 + int error = 0;
11804 + struct yaffs_dev *dev;
11805 + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
11807 + yaffs_trace(YAFFS_TRACE_OS,
11808 + "yaffs_removexattr of object %d", obj->obj_id);
11810 + if (error == 0) {
11811 + int result;
11812 + dev = obj->my_dev;
11813 + yaffs_gross_lock(dev);
11814 + result = yaffs_remove_xattrib(obj, name);
11815 + if (result == YAFFS_OK)
11816 + error = 0;
11817 + else if (result < 0)
11818 + error = result;
11819 + yaffs_gross_unlock(dev);
11822 + yaffs_trace(YAFFS_TRACE_OS,
11823 + "yaffs_removexattr done returning %d", error);
11825 + return error;
11828 +static ssize_t yaffs_listxattr(struct dentry * dentry, char *buff, size_t size)
11830 + struct inode *inode = dentry->d_inode;
11831 + int error = 0;
11832 + struct yaffs_dev *dev;
11833 + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
11835 + yaffs_trace(YAFFS_TRACE_OS,
11836 + "yaffs_listxattr of object %d", obj->obj_id);
11838 + if (error == 0) {
11839 + dev = obj->my_dev;
11840 + yaffs_gross_lock(dev);
11841 + error = yaffs_list_xattrib(obj, buff, size);
11842 + yaffs_gross_unlock(dev);
11845 + yaffs_trace(YAFFS_TRACE_OS,
11846 + "yaffs_listxattr done returning %d", error);
11848 + return error;
11852 +static const struct inode_operations yaffs_file_inode_operations = {
11853 + .setattr = yaffs_setattr,
11854 + .setxattr = yaffs_setxattr,
11855 + .getxattr = yaffs_getxattr,
11856 + .listxattr = yaffs_listxattr,
11857 + .removexattr = yaffs_removexattr,
11861 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
11862 + int buflen)
11864 + unsigned char *alias;
11865 + int ret;
11867 + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
11869 + yaffs_gross_lock(dev);
11871 + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
11873 + yaffs_gross_unlock(dev);
11875 + if (!alias)
11876 + return -ENOMEM;
11878 + ret = vfs_readlink(dentry, buffer, buflen, alias);
11879 + kfree(alias);
11880 + return ret;
11883 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
11884 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
11886 + void *ret;
11887 +#else
11888 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
11890 + int ret
11891 +#endif
11892 + unsigned char *alias;
11893 + int ret_int = 0;
11894 + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
11896 + yaffs_gross_lock(dev);
11898 + alias = yaffs_get_symlink_alias(yaffs_dentry_to_obj(dentry));
11899 + yaffs_gross_unlock(dev);
11901 + if (!alias) {
11902 + ret_int = -ENOMEM;
11903 + goto out;
11905 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
11906 + nd_set_link(nd, alias);
11907 + ret = alias;
11908 +out:
11909 + if (ret_int)
11910 + ret = ERR_PTR(ret_int);
11911 + return ret;
11912 +#else
11913 + ret = vfs_follow_link(nd, alias);
11914 + kfree(alias);
11915 +out:
11916 + if (ret_int)
11917 + ret = ret_int;
11918 + return ret;
11919 +#endif
11923 +#ifdef YAFFS_HAS_PUT_INODE
11925 +/* For now put inode is just for debugging
11926 + * Put inode is called when the inode **structure** is put.
11927 + */
11928 +static void yaffs_put_inode(struct inode *inode)
11930 + yaffs_trace(YAFFS_TRACE_OS,
11931 + "yaffs_put_inode: ino %d, count %d"),
11932 + (int)inode->i_ino, atomic_read(&inode->i_count);
11935 +#endif
11937 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
11938 +void yaffs_put_link(struct dentry *dentry, struct nameidata *nd, void *alias)
11940 + kfree(alias);
11942 +#endif
11944 +static const struct inode_operations yaffs_symlink_inode_operations = {
11945 + .readlink = yaffs_readlink,
11946 + .follow_link = yaffs_follow_link,
11947 +#if (YAFFS_NEW_FOLLOW_LINK == 1)
11948 + .put_link = yaffs_put_link,
11949 +#endif
11950 + .setattr = yaffs_setattr,
11951 + .setxattr = yaffs_setxattr,
11952 + .getxattr = yaffs_getxattr,
11953 + .listxattr = yaffs_listxattr,
11954 + .removexattr = yaffs_removexattr,
11957 +#ifdef YAFFS_USE_OWN_IGET
11959 +static struct inode *yaffs_iget(struct super_block *sb, unsigned long ino)
11961 + struct inode *inode;
11962 + struct yaffs_obj *obj;
11963 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
11965 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_iget for %lu", ino);
11967 + inode = iget_locked(sb, ino);
11968 + if (!inode)
11969 + return ERR_PTR(-ENOMEM);
11970 + if (!(inode->i_state & I_NEW))
11971 + return inode;
11973 + /* NB This is called as a side effect of other functions, but
11974 + * we had to release the lock to prevent deadlocks, so
11975 + * need to lock again.
11976 + */
11978 + yaffs_gross_lock(dev);
11980 + obj = yaffs_find_by_number(dev, inode->i_ino);
11982 + yaffs_fill_inode_from_obj(inode, obj);
11984 + yaffs_gross_unlock(dev);
11986 + unlock_new_inode(inode);
11987 + return inode;
11990 +#else
11992 +static void yaffs_read_inode(struct inode *inode)
11994 + /* NB This is called as a side effect of other functions, but
11995 + * we had to release the lock to prevent deadlocks, so
11996 + * need to lock again.
11997 + */
11999 + struct yaffs_obj *obj;
12000 + struct yaffs_dev *dev = yaffs_super_to_dev(inode->i_sb);
12002 + yaffs_trace(YAFFS_TRACE_OS,
12003 + "yaffs_read_inode for %d", (int)inode->i_ino);
12005 + if (current != yaffs_dev_to_lc(dev)->readdir_process)
12006 + yaffs_gross_lock(dev);
12008 + obj = yaffs_find_by_number(dev, inode->i_ino);
12010 + yaffs_fill_inode_from_obj(inode, obj);
12012 + if (current != yaffs_dev_to_lc(dev)->readdir_process)
12013 + yaffs_gross_unlock(dev);
12016 +#endif
12020 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
12021 + struct yaffs_obj *obj)
12023 + struct inode *inode;
12025 + if (!sb) {
12026 + yaffs_trace(YAFFS_TRACE_OS,
12027 + "yaffs_get_inode for NULL super_block!!");
12028 + return NULL;
12032 + if (!obj) {
12033 + yaffs_trace(YAFFS_TRACE_OS,
12034 + "yaffs_get_inode for NULL object!!");
12035 + return NULL;
12039 + yaffs_trace(YAFFS_TRACE_OS,
12040 + "yaffs_get_inode for object %d", obj->obj_id);
12042 + inode = Y_IGET(sb, obj->obj_id);
12043 + if (IS_ERR(inode))
12044 + return NULL;
12046 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
12047 + /* iget also increments the inode's i_count */
12048 + /* NB You can't be holding gross_lock or deadlock will happen! */
12050 + return inode;
12055 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
12056 +#define YCRED(x) x
12057 +#else
12058 +#define YCRED(x) (x->cred)
12059 +#endif
12061 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
12062 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
12063 + dev_t rdev)
12064 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12065 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
12066 + dev_t rdev)
12067 +#else
12068 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
12069 + int rdev)
12070 +#endif
12072 + struct inode *inode;
12074 + struct yaffs_obj *obj = NULL;
12075 + struct yaffs_dev *dev;
12077 + struct yaffs_obj *parent = yaffs_inode_to_obj(dir);
12079 + int error = -ENOSPC;
12080 + uid_t uid = YCRED_FSUID();
12081 + gid_t gid =
12082 + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
12084 + if ((dir->i_mode & S_ISGID) && S_ISDIR(mode))
12085 + mode |= S_ISGID;
12087 + if (parent) {
12088 + yaffs_trace(YAFFS_TRACE_OS,
12089 + "yaffs_mknod: parent object %d type %d",
12090 + parent->obj_id, parent->variant_type);
12091 + } else {
12092 + yaffs_trace(YAFFS_TRACE_OS,
12093 + "yaffs_mknod: could not get parent object");
12094 + return -EPERM;
12097 + yaffs_trace(YAFFS_TRACE_OS,
12098 + "yaffs_mknod: making oject for %s, mode %x dev %x",
12099 + dentry->d_name.name, mode, rdev);
12101 + dev = parent->my_dev;
12103 + yaffs_gross_lock(dev);
12105 + switch (mode & S_IFMT) {
12106 + default:
12107 + /* Special (socket, fifo, device...) */
12108 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making special");
12109 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12110 + obj =
12111 + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
12112 + gid, old_encode_dev(rdev));
12113 +#else
12114 + obj =
12115 + yaffs_create_special(parent, dentry->d_name.name, mode, uid,
12116 + gid, rdev);
12117 +#endif
12118 + break;
12119 + case S_IFREG: /* file */
12120 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making file");
12121 + obj = yaffs_create_file(parent, dentry->d_name.name, mode, uid,
12122 + gid);
12123 + break;
12124 + case S_IFDIR: /* directory */
12125 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making directory");
12126 + obj = yaffs_create_dir(parent, dentry->d_name.name, mode,
12127 + uid, gid);
12128 + break;
12129 + case S_IFLNK: /* symlink */
12130 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod: making symlink");
12131 + obj = NULL; /* Do we ever get here? */
12132 + break;
12135 + /* Can not call yaffs_get_inode() with gross lock held */
12136 + yaffs_gross_unlock(dev);
12138 + if (obj) {
12139 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
12140 + d_instantiate(dentry, inode);
12141 + update_dir_time(dir);
12142 + yaffs_trace(YAFFS_TRACE_OS,
12143 + "yaffs_mknod created object %d count = %d",
12144 + obj->obj_id, atomic_read(&inode->i_count));
12145 + error = 0;
12146 + yaffs_fill_inode_from_obj(dir, parent);
12147 + } else {
12148 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mknod failed making object");
12149 + error = -ENOMEM;
12152 + return error;
12155 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
12156 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
12157 +#else
12158 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
12159 +#endif
12161 + int ret_val;
12162 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_mkdir");
12163 + ret_val = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
12164 + return ret_val;
12168 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
12169 +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
12170 + bool dummy)
12171 +#elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
12172 +static int yaffs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
12173 + struct nameidata *n)
12174 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12175 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
12176 + struct nameidata *n)
12177 +#else
12178 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
12179 +#endif
12181 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_create");
12182 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
12185 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 6, 0))
12186 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
12187 + unsigned int dummy)
12188 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12189 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
12190 + struct nameidata *n)
12191 +#else
12192 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
12193 +#endif
12195 + struct yaffs_obj *obj;
12196 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
12198 + struct yaffs_dev *dev = yaffs_inode_to_obj(dir)->my_dev;
12200 + if (current != yaffs_dev_to_lc(dev)->readdir_process)
12201 + yaffs_gross_lock(dev);
12203 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup for %d:%s",
12204 + yaffs_inode_to_obj(dir)->obj_id, dentry->d_name.name);
12206 + obj = yaffs_find_by_name(yaffs_inode_to_obj(dir), dentry->d_name.name);
12208 + obj = yaffs_get_equivalent_obj(obj); /* in case it was a hardlink */
12210 + /* Can't hold gross lock when calling yaffs_get_inode() */
12211 + if (current != yaffs_dev_to_lc(dev)->readdir_process)
12212 + yaffs_gross_unlock(dev);
12214 + if (obj) {
12215 + yaffs_trace(YAFFS_TRACE_OS,
12216 + "yaffs_lookup found %d", obj->obj_id);
12218 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
12219 + } else {
12220 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_lookup not found");
12224 +/* added NCB for 2.5/6 compatability - forces add even if inode is
12225 + * NULL which creates dentry hash */
12226 + d_add(dentry, inode);
12228 + return NULL;
12232 + * Create a link...
12233 + */
12234 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
12235 + struct dentry *dentry)
12237 + struct inode *inode = old_dentry->d_inode;
12238 + struct yaffs_obj *obj = NULL;
12239 + struct yaffs_obj *link = NULL;
12240 + struct yaffs_dev *dev;
12242 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_link");
12244 + obj = yaffs_inode_to_obj(inode);
12245 + dev = obj->my_dev;
12247 + yaffs_gross_lock(dev);
12249 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
12250 + link =
12251 + yaffs_link_obj(yaffs_inode_to_obj(dir), dentry->d_name.name,
12252 + obj);
12254 + if (link) {
12255 + set_nlink(old_dentry->d_inode, yaffs_get_obj_link_count(obj));
12256 + d_instantiate(dentry, old_dentry->d_inode);
12257 + atomic_inc(&old_dentry->d_inode->i_count);
12258 + yaffs_trace(YAFFS_TRACE_OS,
12259 + "yaffs_link link count %d i_count %d",
12260 + old_dentry->d_inode->i_nlink,
12261 + atomic_read(&old_dentry->d_inode->i_count));
12264 + yaffs_gross_unlock(dev);
12266 + if (link) {
12267 + update_dir_time(dir);
12268 + return 0;
12271 + return -EPERM;
12274 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
12275 + const char *symname)
12277 + struct yaffs_obj *obj;
12278 + struct yaffs_dev *dev;
12279 + uid_t uid = YCRED_FSUID();
12280 + gid_t gid =
12281 + (dir->i_mode & S_ISGID) ? i_gid_read(dir) : YCRED_FSGID();
12283 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_symlink");
12285 + if (strnlen(dentry->d_name.name, YAFFS_MAX_NAME_LENGTH + 1) >
12286 + YAFFS_MAX_NAME_LENGTH)
12287 + return -ENAMETOOLONG;
12289 + if (strnlen(symname, YAFFS_MAX_ALIAS_LENGTH + 1) >
12290 + YAFFS_MAX_ALIAS_LENGTH)
12291 + return -ENAMETOOLONG;
12293 + dev = yaffs_inode_to_obj(dir)->my_dev;
12294 + yaffs_gross_lock(dev);
12295 + obj = yaffs_create_symlink(yaffs_inode_to_obj(dir), dentry->d_name.name,
12296 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
12297 + yaffs_gross_unlock(dev);
12299 + if (obj) {
12300 + struct inode *inode;
12302 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
12303 + d_instantiate(dentry, inode);
12304 + update_dir_time(dir);
12305 + yaffs_trace(YAFFS_TRACE_OS, "symlink created OK");
12306 + return 0;
12307 + } else {
12308 + yaffs_trace(YAFFS_TRACE_OS, "symlink not created");
12311 + return -ENOMEM;
12315 + * The VFS layer already does all the dentry stuff for rename.
12317 + * NB: POSIX says you can rename an object over an old object of the same name
12318 + */
12319 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
12320 + struct inode *new_dir, struct dentry *new_dentry)
12322 + struct yaffs_dev *dev;
12323 + int ret_val = YAFFS_FAIL;
12324 + struct yaffs_obj *target;
12326 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_rename");
12327 + dev = yaffs_inode_to_obj(old_dir)->my_dev;
12329 + yaffs_gross_lock(dev);
12331 + /* Check if the target is an existing directory that is not empty. */
12332 + target = yaffs_find_by_name(yaffs_inode_to_obj(new_dir),
12333 + new_dentry->d_name.name);
12335 + if (target && target->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
12336 + !list_empty(&target->variant.dir_variant.children)) {
12338 + yaffs_trace(YAFFS_TRACE_OS, "target is non-empty dir");
12340 + ret_val = YAFFS_FAIL;
12341 + } else {
12342 + /* Now does unlinking internally using shadowing mechanism */
12343 + yaffs_trace(YAFFS_TRACE_OS, "calling yaffs_rename_obj");
12345 + ret_val = yaffs_rename_obj(yaffs_inode_to_obj(old_dir),
12346 + old_dentry->d_name.name,
12347 + yaffs_inode_to_obj(new_dir),
12348 + new_dentry->d_name.name);
12350 + yaffs_gross_unlock(dev);
12352 + if (ret_val == YAFFS_OK) {
12353 + if (target)
12354 + inode_dec_link_count(new_dentry->d_inode);
12356 + update_dir_time(old_dir);
12357 + if (old_dir != new_dir)
12358 + update_dir_time(new_dir);
12359 + return 0;
12360 + } else {
12361 + return -ENOTEMPTY;
12368 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
12370 + int ret_val;
12372 + struct yaffs_dev *dev;
12373 + struct yaffs_obj *obj;
12375 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_unlink %d:%s",
12376 + (int)(dir->i_ino), dentry->d_name.name);
12377 + obj = yaffs_inode_to_obj(dir);
12378 + dev = obj->my_dev;
12380 + yaffs_gross_lock(dev);
12382 + ret_val = yaffs_unlinker(obj, dentry->d_name.name);
12384 + if (ret_val == YAFFS_OK) {
12385 + inode_dec_link_count(dentry->d_inode);
12386 + dir->i_version++;
12387 + yaffs_gross_unlock(dev);
12388 + update_dir_time(dir);
12389 + return 0;
12391 + yaffs_gross_unlock(dev);
12392 + return -ENOTEMPTY;
12397 +static const struct inode_operations yaffs_dir_inode_operations = {
12398 + .create = yaffs_create,
12399 + .lookup = yaffs_lookup,
12400 + .link = yaffs_link,
12401 + .unlink = yaffs_unlink,
12402 + .symlink = yaffs_symlink,
12403 + .mkdir = yaffs_mkdir,
12404 + .rmdir = yaffs_unlink,
12405 + .mknod = yaffs_mknod,
12406 + .rename = yaffs_rename,
12407 + .setattr = yaffs_setattr,
12408 + .setxattr = yaffs_setxattr,
12409 + .getxattr = yaffs_getxattr,
12410 + .listxattr = yaffs_listxattr,
12411 + .removexattr = yaffs_removexattr,
12414 +/*-----------------------------------------------------------------*/
12415 +/* Directory search context allows us to unlock access to yaffs during
12416 + * filldir without causing problems with the directory being modified.
12417 + * This is similar to the tried and tested mechanism used in yaffs direct.
12419 + * A search context iterates along a doubly linked list of siblings in the
12420 + * directory. If the iterating object is deleted then this would corrupt
12421 + * the list iteration, likely causing a crash. The search context avoids
12422 + * this by using the remove_obj_fn to move the search context to the
12423 + * next object before the object is deleted.
12425 + * Many readdirs (and thus seach conexts) may be alive simulateously so
12426 + * each struct yaffs_dev has a list of these.
12428 + * A seach context lives for the duration of a readdir.
12430 + * All these functions must be called while yaffs is locked.
12431 + */
12433 +struct yaffs_search_context {
12434 + struct yaffs_dev *dev;
12435 + struct yaffs_obj *dir_obj;
12436 + struct yaffs_obj *next_return;
12437 + struct list_head others;
12441 + * yaffs_new_search() creates a new search context, initialises it and
12442 + * adds it to the device's search context list.
12444 + * Called at start of readdir.
12445 + */
12446 +static struct yaffs_search_context *yaffs_new_search(struct yaffs_obj *dir)
12448 + struct yaffs_dev *dev = dir->my_dev;
12449 + struct yaffs_search_context *sc =
12450 + kmalloc(sizeof(struct yaffs_search_context), GFP_NOFS);
12451 + if (sc) {
12452 + sc->dir_obj = dir;
12453 + sc->dev = dev;
12454 + if (list_empty(&sc->dir_obj->variant.dir_variant.children))
12455 + sc->next_return = NULL;
12456 + else
12457 + sc->next_return =
12458 + list_entry(dir->variant.dir_variant.children.next,
12459 + struct yaffs_obj, siblings);
12460 + INIT_LIST_HEAD(&sc->others);
12461 + list_add(&sc->others, &(yaffs_dev_to_lc(dev)->search_contexts));
12463 + return sc;
12467 + * yaffs_search_end() disposes of a search context and cleans up.
12468 + */
12469 +static void yaffs_search_end(struct yaffs_search_context *sc)
12471 + if (sc) {
12472 + list_del(&sc->others);
12473 + kfree(sc);
12478 + * yaffs_search_advance() moves a search context to the next object.
12479 + * Called when the search iterates or when an object removal causes
12480 + * the search context to be moved to the next object.
12481 + */
12482 +static void yaffs_search_advance(struct yaffs_search_context *sc)
12484 + if (!sc)
12485 + return;
12487 + if (sc->next_return == NULL ||
12488 + list_empty(&sc->dir_obj->variant.dir_variant.children))
12489 + sc->next_return = NULL;
12490 + else {
12491 + struct list_head *next = sc->next_return->siblings.next;
12493 + if (next == &sc->dir_obj->variant.dir_variant.children)
12494 + sc->next_return = NULL; /* end of list */
12495 + else
12496 + sc->next_return =
12497 + list_entry(next, struct yaffs_obj, siblings);
12502 + * yaffs_remove_obj_callback() is called when an object is unlinked.
12503 + * We check open search contexts and advance any which are currently
12504 + * on the object being iterated.
12505 + */
12506 +static void yaffs_remove_obj_callback(struct yaffs_obj *obj)
12509 + struct list_head *i;
12510 + struct yaffs_search_context *sc;
12511 + struct list_head *search_contexts =
12512 + &(yaffs_dev_to_lc(obj->my_dev)->search_contexts);
12514 + /* Iterate through the directory search contexts.
12515 + * If any are currently on the object being removed, then advance
12516 + * the search context to the next object to prevent a hanging pointer.
12517 + */
12518 + list_for_each(i, search_contexts) {
12519 + sc = list_entry(i, struct yaffs_search_context, others);
12520 + if (sc->next_return == obj)
12521 + yaffs_search_advance(sc);
12527 +/*-----------------------------------------------------------------*/
12529 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
12530 +static int yaffs_readdir(struct file *file, struct dir_context *ctx)
12532 + struct yaffs_obj *obj;
12533 + struct yaffs_dev *dev;
12534 + struct yaffs_search_context *sc;
12535 + struct inode *inode = file->f_dentry->d_inode;
12536 + unsigned long offset, curoffs;
12537 + struct yaffs_obj *l;
12538 + int ret_val = 0;
12540 + char name[YAFFS_MAX_NAME_LENGTH + 1];
12542 + obj = yaffs_dentry_to_obj(file->f_dentry);
12543 + dev = obj->my_dev;
12545 + yaffs_gross_lock(dev);
12547 + yaffs_dev_to_lc(dev)->readdir_process = current;
12549 + offset = ctx->pos;
12551 + sc = yaffs_new_search(obj);
12552 + if (!sc) {
12553 + ret_val = -ENOMEM;
12554 + goto out;
12557 + yaffs_trace(YAFFS_TRACE_OS,
12558 + "yaffs_readdir: starting at %d", (int)offset);
12560 + if (offset == 0) {
12561 + yaffs_trace(YAFFS_TRACE_OS,
12562 + "yaffs_readdir: entry . ino %d",
12563 + (int)inode->i_ino);
12564 + yaffs_gross_unlock(dev);
12565 + if (!dir_emit_dot(file, ctx)) {
12566 + yaffs_gross_lock(dev);
12567 + goto out;
12569 + yaffs_gross_lock(dev);
12570 + offset++;
12571 + ctx->pos++;
12573 + if (offset == 1) {
12574 + yaffs_trace(YAFFS_TRACE_OS,
12575 + "yaffs_readdir: entry .. ino %d",
12576 + (int)file->f_dentry->d_parent->d_inode->i_ino);
12577 + yaffs_gross_unlock(dev);
12578 + if (!dir_emit_dotdot(file, ctx)) {
12579 + yaffs_gross_lock(dev);
12580 + goto out;
12582 + yaffs_gross_lock(dev);
12583 + offset++;
12584 + ctx->pos++;
12587 + curoffs = 1;
12589 + /* If the directory has changed since the open or last call to
12590 + readdir, rewind to after the 2 canned entries. */
12591 + if (file->f_version != inode->i_version) {
12592 + offset = 2;
12593 + ctx->pos = offset;
12594 + file->f_version = inode->i_version;
12597 + while (sc->next_return) {
12598 + curoffs++;
12599 + l = sc->next_return;
12600 + if (curoffs >= offset) {
12601 + int this_inode = yaffs_get_obj_inode(l);
12602 + int this_type = yaffs_get_obj_type(l);
12604 + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
12605 + yaffs_trace(YAFFS_TRACE_OS,
12606 + "yaffs_readdir: %s inode %d",
12607 + name, yaffs_get_obj_inode(l));
12609 + yaffs_gross_unlock(dev);
12611 + if (!dir_emit(ctx, name, strlen(name),
12612 + this_inode, this_type) < 0) {
12613 + yaffs_gross_lock(dev);
12614 + goto out;
12617 + yaffs_gross_lock(dev);
12619 + offset++;
12620 + ctx->pos++;
12622 + yaffs_search_advance(sc);
12625 +out:
12626 + yaffs_search_end(sc);
12627 + yaffs_dev_to_lc(dev)->readdir_process = NULL;
12628 + yaffs_gross_unlock(dev);
12630 + return ret_val;
12632 +#else
12633 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
12635 + struct yaffs_obj *obj;
12636 + struct yaffs_dev *dev;
12637 + struct yaffs_search_context *sc;
12638 + struct inode *inode = f->f_dentry->d_inode;
12639 + unsigned long offset, curoffs;
12640 + struct yaffs_obj *l;
12641 + int ret_val = 0;
12643 + char name[YAFFS_MAX_NAME_LENGTH + 1];
12645 + obj = yaffs_dentry_to_obj(f->f_dentry);
12646 + dev = obj->my_dev;
12648 + yaffs_gross_lock(dev);
12650 + yaffs_dev_to_lc(dev)->readdir_process = current;
12652 + offset = f->f_pos;
12654 + sc = yaffs_new_search(obj);
12655 + if (!sc) {
12656 + ret_val = -ENOMEM;
12657 + goto out;
12660 + yaffs_trace(YAFFS_TRACE_OS,
12661 + "yaffs_readdir: starting at %d", (int)offset);
12663 + if (offset == 0) {
12664 + yaffs_trace(YAFFS_TRACE_OS,
12665 + "yaffs_readdir: entry . ino %d",
12666 + (int)inode->i_ino);
12667 + yaffs_gross_unlock(dev);
12668 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR) < 0) {
12669 + yaffs_gross_lock(dev);
12670 + goto out;
12672 + yaffs_gross_lock(dev);
12673 + offset++;
12674 + f->f_pos++;
12676 + if (offset == 1) {
12677 + yaffs_trace(YAFFS_TRACE_OS,
12678 + "yaffs_readdir: entry .. ino %d",
12679 + (int)f->f_dentry->d_parent->d_inode->i_ino);
12680 + yaffs_gross_unlock(dev);
12681 + if (filldir(dirent, "..", 2, offset,
12682 + f->f_dentry->d_parent->d_inode->i_ino,
12683 + DT_DIR) < 0) {
12684 + yaffs_gross_lock(dev);
12685 + goto out;
12687 + yaffs_gross_lock(dev);
12688 + offset++;
12689 + f->f_pos++;
12692 + curoffs = 1;
12694 + /* If the directory has changed since the open or last call to
12695 + readdir, rewind to after the 2 canned entries. */
12696 + if (f->f_version != inode->i_version) {
12697 + offset = 2;
12698 + f->f_pos = offset;
12699 + f->f_version = inode->i_version;
12702 + while (sc->next_return) {
12703 + curoffs++;
12704 + l = sc->next_return;
12705 + if (curoffs >= offset) {
12706 + int this_inode = yaffs_get_obj_inode(l);
12707 + int this_type = yaffs_get_obj_type(l);
12709 + yaffs_get_obj_name(l, name, YAFFS_MAX_NAME_LENGTH + 1);
12710 + yaffs_trace(YAFFS_TRACE_OS,
12711 + "yaffs_readdir: %s inode %d",
12712 + name, yaffs_get_obj_inode(l));
12714 + yaffs_gross_unlock(dev);
12716 + if (filldir(dirent,
12717 + name,
12718 + strlen(name),
12719 + offset, this_inode, this_type) < 0) {
12720 + yaffs_gross_lock(dev);
12721 + goto out;
12724 + yaffs_gross_lock(dev);
12726 + offset++;
12727 + f->f_pos++;
12729 + yaffs_search_advance(sc);
12732 +out:
12733 + yaffs_search_end(sc);
12734 + yaffs_dev_to_lc(dev)->readdir_process = NULL;
12735 + yaffs_gross_unlock(dev);
12737 + return ret_val;
12739 +#endif
12741 +static const struct file_operations yaffs_dir_operations = {
12742 + .read = generic_read_dir,
12743 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 12, 0))
12744 + .iterate = yaffs_readdir,
12745 +#else
12746 + .readdir = yaffs_readdir,
12747 +#endif
12748 + .fsync = yaffs_sync_object,
12749 + .llseek = generic_file_llseek,
12752 +static void yaffs_fill_inode_from_obj(struct inode *inode,
12753 + struct yaffs_obj *obj)
12755 + if (inode && obj) {
12757 + /* Check mode against the variant type and attempt to repair if broken. */
12758 + u32 mode = obj->yst_mode;
12759 + switch (obj->variant_type) {
12760 + case YAFFS_OBJECT_TYPE_FILE:
12761 + if (!S_ISREG(mode)) {
12762 + obj->yst_mode &= ~S_IFMT;
12763 + obj->yst_mode |= S_IFREG;
12766 + break;
12767 + case YAFFS_OBJECT_TYPE_SYMLINK:
12768 + if (!S_ISLNK(mode)) {
12769 + obj->yst_mode &= ~S_IFMT;
12770 + obj->yst_mode |= S_IFLNK;
12773 + break;
12774 + case YAFFS_OBJECT_TYPE_DIRECTORY:
12775 + if (!S_ISDIR(mode)) {
12776 + obj->yst_mode &= ~S_IFMT;
12777 + obj->yst_mode |= S_IFDIR;
12780 + break;
12781 + case YAFFS_OBJECT_TYPE_UNKNOWN:
12782 + case YAFFS_OBJECT_TYPE_HARDLINK:
12783 + case YAFFS_OBJECT_TYPE_SPECIAL:
12784 + default:
12785 + /* TODO? */
12786 + break;
12789 + inode->i_flags |= S_NOATIME;
12791 + inode->i_ino = obj->obj_id;
12792 + inode->i_mode = obj->yst_mode;
12793 + i_uid_write(inode, obj->yst_uid);
12794 + i_gid_write(inode, obj->yst_gid);
12795 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
12796 + inode->i_blksize = inode->i_sb->s_blocksize;
12797 +#endif
12798 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12800 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
12801 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
12802 + inode->i_atime.tv_nsec = 0;
12803 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
12804 + inode->i_mtime.tv_nsec = 0;
12805 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
12806 + inode->i_ctime.tv_nsec = 0;
12807 +#else
12808 + inode->i_rdev = obj->yst_rdev;
12809 + inode->i_atime = obj->yst_atime;
12810 + inode->i_mtime = obj->yst_mtime;
12811 + inode->i_ctime = obj->yst_ctime;
12812 +#endif
12813 + inode->i_size = yaffs_get_obj_length(obj);
12814 + inode->i_blocks = (inode->i_size + 511) >> 9;
12816 + set_nlink(inode, yaffs_get_obj_link_count(obj));
12818 + yaffs_trace(YAFFS_TRACE_OS,
12819 + "yaffs_fill_inode mode %x uid %d gid %d size %lld count %d",
12820 + inode->i_mode, i_uid_read(inode), i_gid_read(inode),
12821 + inode->i_size, atomic_read(&inode->i_count));
12823 + switch (obj->yst_mode & S_IFMT) {
12824 + default: /* fifo, device or socket */
12825 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
12826 + init_special_inode(inode, obj->yst_mode,
12827 + old_decode_dev(obj->yst_rdev));
12828 +#else
12829 + init_special_inode(inode, obj->yst_mode,
12830 + (dev_t) (obj->yst_rdev));
12831 +#endif
12832 + break;
12833 + case S_IFREG: /* file */
12834 + inode->i_op = &yaffs_file_inode_operations;
12835 + inode->i_fop = &yaffs_file_operations;
12836 + inode->i_mapping->a_ops =
12837 + &yaffs_file_address_operations;
12838 + break;
12839 + case S_IFDIR: /* directory */
12840 + inode->i_op = &yaffs_dir_inode_operations;
12841 + inode->i_fop = &yaffs_dir_operations;
12842 + break;
12843 + case S_IFLNK: /* symlink */
12844 + inode->i_op = &yaffs_symlink_inode_operations;
12845 + break;
12848 + yaffs_inode_to_obj_lv(inode) = obj;
12850 + obj->my_inode = inode;
12852 + } else {
12853 + yaffs_trace(YAFFS_TRACE_OS,
12854 + "yaffs_fill_inode invalid parameters");
12862 + * yaffs background thread functions .
12863 + * yaffs_bg_thread_fn() the thread function
12864 + * yaffs_bg_start() launches the background thread.
12865 + * yaffs_bg_stop() cleans up the background thread.
12867 + * NB:
12868 + * The thread should only run after the yaffs is initialised
12869 + * The thread should be stopped before yaffs is unmounted.
12870 + * The thread should not do any writing while the fs is in read only.
12871 + */
12873 +static unsigned yaffs_bg_gc_urgency(struct yaffs_dev *dev)
12875 + unsigned erased_chunks =
12876 + dev->n_erased_blocks * dev->param.chunks_per_block;
12877 + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
12878 + unsigned scattered = 0; /* Free chunks not in an erased block */
12880 + if (erased_chunks < dev->n_free_chunks)
12881 + scattered = (dev->n_free_chunks - erased_chunks);
12883 + if (!context->bg_running)
12884 + return 0;
12885 + else if (scattered < (dev->param.chunks_per_block * 2))
12886 + return 0;
12887 + else if (erased_chunks > dev->n_free_chunks / 2)
12888 + return 0;
12889 + else if (erased_chunks > dev->n_free_chunks / 4)
12890 + return 1;
12891 + else
12892 + return 2;
12895 +#ifdef YAFFS_COMPILE_BACKGROUND
12897 +void yaffs_background_waker(unsigned long data)
12899 + wake_up_process((struct task_struct *)data);
12902 +static int yaffs_bg_thread_fn(void *data)
12904 + struct yaffs_dev *dev = (struct yaffs_dev *)data;
12905 + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
12906 + unsigned long now = jiffies;
12907 + unsigned long next_dir_update = now;
12908 + unsigned long next_gc = now;
12909 + unsigned long expires;
12910 + unsigned int urgency;
12912 + int gc_result;
12913 + struct timer_list timer;
12915 + yaffs_trace(YAFFS_TRACE_BACKGROUND,
12916 + "yaffs_background starting for dev %p", (void *)dev);
12918 +#ifdef YAFFS_COMPILE_FREEZER
12919 + set_freezable();
12920 +#endif
12921 + while (context->bg_running) {
12922 + yaffs_trace(YAFFS_TRACE_BACKGROUND, "yaffs_background");
12924 + if (kthread_should_stop())
12925 + break;
12927 +#ifdef YAFFS_COMPILE_FREEZER
12928 + if (try_to_freeze())
12929 + continue;
12930 +#endif
12931 + yaffs_gross_lock(dev);
12933 + now = jiffies;
12935 + if (time_after(now, next_dir_update) && yaffs_bg_enable) {
12936 + yaffs_update_dirty_dirs(dev);
12937 + next_dir_update = now + HZ;
12940 + if (time_after(now, next_gc) && yaffs_bg_enable) {
12941 + if (!dev->is_checkpointed) {
12942 + urgency = yaffs_bg_gc_urgency(dev);
12943 + gc_result = yaffs_bg_gc(dev, urgency);
12944 + if (urgency > 1)
12945 + next_gc = now + HZ / 20 + 1;
12946 + else if (urgency > 0)
12947 + next_gc = now + HZ / 10 + 1;
12948 + else
12949 + next_gc = now + HZ * 2;
12950 + } else {
12951 + /*
12952 + * gc not running so set to next_dir_update
12953 + * to cut down on wake ups
12954 + */
12955 + next_gc = next_dir_update;
12958 + yaffs_gross_unlock(dev);
12959 +#if 1
12960 + expires = next_dir_update;
12961 + if (time_before(next_gc, expires))
12962 + expires = next_gc;
12963 + if (time_before(expires, now))
12964 + expires = now + HZ;
12966 + Y_INIT_TIMER(&timer);
12967 + timer.expires = expires + 1;
12968 + timer.data = (unsigned long)current;
12969 + timer.function = yaffs_background_waker;
12971 + set_current_state(TASK_INTERRUPTIBLE);
12972 + add_timer(&timer);
12973 + schedule();
12974 + del_timer_sync(&timer);
12975 +#else
12976 + msleep(10);
12977 +#endif
12980 + return 0;
12983 +static int yaffs_bg_start(struct yaffs_dev *dev)
12985 + int retval = 0;
12986 + struct yaffs_linux_context *context = yaffs_dev_to_lc(dev);
12988 + if (dev->read_only)
12989 + return -1;
12991 + context->bg_running = 1;
12993 + context->bg_thread = kthread_run(yaffs_bg_thread_fn,
12994 + (void *)dev, "yaffs-bg-%d",
12995 + context->mount_id);
12997 + if (IS_ERR(context->bg_thread)) {
12998 + retval = PTR_ERR(context->bg_thread);
12999 + context->bg_thread = NULL;
13000 + context->bg_running = 0;
13002 + return retval;
13005 +static void yaffs_bg_stop(struct yaffs_dev *dev)
13007 + struct yaffs_linux_context *ctxt = yaffs_dev_to_lc(dev);
13009 + ctxt->bg_running = 0;
13011 + if (ctxt->bg_thread) {
13012 + kthread_stop(ctxt->bg_thread);
13013 + ctxt->bg_thread = NULL;
13016 +#else
13017 +static int yaffs_bg_thread_fn(void *data)
13019 + return 0;
13022 +static int yaffs_bg_start(struct yaffs_dev *dev)
13024 + return 0;
13027 +static void yaffs_bg_stop(struct yaffs_dev *dev)
13030 +#endif
13033 +static void yaffs_flush_inodes(struct super_block *sb)
13035 + struct inode *iptr;
13036 + struct yaffs_obj *obj;
13038 + list_for_each_entry(iptr, &sb->s_inodes, i_sb_list) {
13039 + obj = yaffs_inode_to_obj(iptr);
13040 + if (obj) {
13041 + yaffs_trace(YAFFS_TRACE_OS,
13042 + "flushing obj %d",
13043 + obj->obj_id);
13044 + yaffs_flush_file(obj, 1, 0);
13049 +static void yaffs_flush_super(struct super_block *sb, int do_checkpoint)
13051 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
13052 + if (!dev)
13053 + return;
13055 + yaffs_flush_inodes(sb);
13056 + yaffs_update_dirty_dirs(dev);
13057 + yaffs_flush_whole_cache(dev);
13058 + if (do_checkpoint)
13059 + yaffs_checkpoint_save(dev);
13062 +static LIST_HEAD(yaffs_context_list);
13063 +struct mutex yaffs_context_lock;
13065 +static void yaffs_put_super(struct super_block *sb)
13067 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
13068 + struct mtd_info *mtd = yaffs_dev_to_mtd(dev);
13070 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
13071 + "yaffs_put_super");
13073 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
13074 + "Shutting down yaffs background thread");
13075 + yaffs_bg_stop(dev);
13076 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_BACKGROUND,
13077 + "yaffs background thread shut down");
13079 + yaffs_gross_lock(dev);
13081 + yaffs_flush_super(sb, 1);
13083 + yaffs_deinitialise(dev);
13085 + yaffs_gross_unlock(dev);
13087 + mutex_lock(&yaffs_context_lock);
13088 + list_del_init(&(yaffs_dev_to_lc(dev)->context_list));
13089 + mutex_unlock(&yaffs_context_lock);
13091 + if (yaffs_dev_to_lc(dev)->spare_buffer) {
13092 + kfree(yaffs_dev_to_lc(dev)->spare_buffer);
13093 + yaffs_dev_to_lc(dev)->spare_buffer = NULL;
13096 + kfree(dev);
13098 + yaffs_put_mtd_device(mtd);
13100 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_ALWAYS,
13101 + "yaffs_put_super done");
13105 +static unsigned yaffs_gc_control_callback(struct yaffs_dev *dev)
13107 + return yaffs_gc_control;
13111 +#ifdef YAFFS_COMPILE_EXPORTFS
13113 +static struct inode *yaffs2_nfs_get_inode(struct super_block *sb, uint64_t ino,
13114 + uint32_t generation)
13116 + return Y_IGET(sb, ino);
13119 +static struct dentry *yaffs2_fh_to_dentry(struct super_block *sb,
13120 + struct fid *fid, int fh_len,
13121 + int fh_type)
13123 + return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
13124 + yaffs2_nfs_get_inode);
13127 +static struct dentry *yaffs2_fh_to_parent(struct super_block *sb,
13128 + struct fid *fid, int fh_len,
13129 + int fh_type)
13131 + return generic_fh_to_parent(sb, fid, fh_len, fh_type,
13132 + yaffs2_nfs_get_inode);
13135 +struct dentry *yaffs2_get_parent(struct dentry *dentry)
13138 + struct super_block *sb = dentry->d_inode->i_sb;
13139 + struct dentry *parent = ERR_PTR(-ENOENT);
13140 + struct inode *inode;
13141 + unsigned long parent_ino;
13142 + struct yaffs_obj *d_obj;
13143 + struct yaffs_obj *parent_obj;
13145 + d_obj = yaffs_inode_to_obj(dentry->d_inode);
13147 + if (d_obj) {
13148 + parent_obj = d_obj->parent;
13149 + if (parent_obj) {
13150 + parent_ino = yaffs_get_obj_inode(parent_obj);
13151 + inode = Y_IGET(sb, parent_ino);
13153 + if (IS_ERR(inode)) {
13154 + parent = ERR_CAST(inode);
13155 + } else {
13156 + parent = d_obtain_alias(inode);
13157 + if (!IS_ERR(parent)) {
13158 + parent = ERR_PTR(-ENOMEM);
13159 + iput(inode);
13165 + return parent;
13168 +/* Just declare a zero structure as a NULL value implies
13169 + * using the default functions of exportfs.
13170 + */
13172 +static struct export_operations yaffs_export_ops = {
13173 + .fh_to_dentry = yaffs2_fh_to_dentry,
13174 + .fh_to_parent = yaffs2_fh_to_parent,
13175 + .get_parent = yaffs2_get_parent,
13178 +#endif
13180 +static void yaffs_unstitch_obj(struct inode *inode, struct yaffs_obj *obj)
13182 + /* Clear the association between the inode and
13183 + * the struct yaffs_obj.
13184 + */
13185 + obj->my_inode = NULL;
13186 + yaffs_inode_to_obj_lv(inode) = NULL;
13188 + /* If the object freeing was deferred, then the real
13189 + * free happens now.
13190 + * This should fix the inode inconsistency problem.
13191 + */
13192 + yaffs_handle_defered_free(obj);
13195 +#ifdef YAFFS_HAS_EVICT_INODE
13196 +/* yaffs_evict_inode combines into one operation what was previously done in
13197 + * yaffs_clear_inode() and yaffs_delete_inode()
13199 + */
13200 +static void yaffs_evict_inode(struct inode *inode)
13202 + struct yaffs_obj *obj;
13203 + struct yaffs_dev *dev;
13204 + int deleteme = 0;
13206 + obj = yaffs_inode_to_obj(inode);
13208 + yaffs_trace(YAFFS_TRACE_OS,
13209 + "yaffs_evict_inode: ino %d, count %d %s",
13210 + (int)inode->i_ino, atomic_read(&inode->i_count),
13211 + obj ? "object exists" : "null object");
13213 + if (!inode->i_nlink && !is_bad_inode(inode))
13214 + deleteme = 1;
13215 + truncate_inode_pages(&inode->i_data, 0);
13216 + Y_CLEAR_INODE(inode);
13218 + if (deleteme && obj) {
13219 + dev = obj->my_dev;
13220 + yaffs_gross_lock(dev);
13221 + yaffs_del_obj(obj);
13222 + yaffs_gross_unlock(dev);
13224 + if (obj) {
13225 + dev = obj->my_dev;
13226 + yaffs_gross_lock(dev);
13227 + yaffs_unstitch_obj(inode, obj);
13228 + yaffs_gross_unlock(dev);
13231 +#else
13233 +/* clear is called to tell the fs to release any per-inode data it holds.
13234 + * The object might still exist on disk and is just being thrown out of the cache
13235 + * or else the object has actually been deleted and we're being called via
13236 + * the chain
13237 + * yaffs_delete_inode() -> clear_inode()->yaffs_clear_inode()
13238 + */
13240 +static void yaffs_clear_inode(struct inode *inode)
13242 + struct yaffs_obj *obj;
13243 + struct yaffs_dev *dev;
13245 + obj = yaffs_inode_to_obj(inode);
13247 + yaffs_trace(YAFFS_TRACE_OS,
13248 + "yaffs_clear_inode: ino %d, count %d %s",
13249 + (int)inode->i_ino, atomic_read(&inode->i_count),
13250 + obj ? "object exists" : "null object");
13252 + if (obj) {
13253 + dev = obj->my_dev;
13254 + yaffs_gross_lock(dev);
13255 + yaffs_unstitch_obj(inode, obj);
13256 + yaffs_gross_unlock(dev);
13261 +/* delete is called when the link count is zero and the inode
13262 + * is put (ie. nobody wants to know about it anymore, time to
13263 + * delete the file).
13264 + * NB Must call clear_inode()
13265 + */
13266 +static void yaffs_delete_inode(struct inode *inode)
13268 + struct yaffs_obj *obj = yaffs_inode_to_obj(inode);
13269 + struct yaffs_dev *dev;
13271 + yaffs_trace(YAFFS_TRACE_OS,
13272 + "yaffs_delete_inode: ino %d, count %d %s",
13273 + (int)inode->i_ino, atomic_read(&inode->i_count),
13274 + obj ? "object exists" : "null object");
13276 + if (obj) {
13277 + dev = obj->my_dev;
13278 + yaffs_gross_lock(dev);
13279 + yaffs_del_obj(obj);
13280 + yaffs_gross_unlock(dev);
13282 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 13))
13283 + truncate_inode_pages(&inode->i_data, 0);
13284 +#endif
13285 + clear_inode(inode);
13287 +#endif
13292 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13293 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
13295 + struct yaffs_dev *dev = yaffs_dentry_to_obj(dentry)->my_dev;
13296 + struct super_block *sb = dentry->d_sb;
13297 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
13298 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
13300 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
13301 +#else
13302 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
13304 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
13305 +#endif
13307 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_statfs");
13309 + yaffs_gross_lock(dev);
13311 + buf->f_type = YAFFS_MAGIC;
13312 + buf->f_bsize = sb->s_blocksize;
13313 + buf->f_namelen = 255;
13315 + if (dev->data_bytes_per_chunk & (dev->data_bytes_per_chunk - 1)) {
13316 + /* Do this if chunk size is not a power of 2 */
13318 + uint64_t bytes_in_dev;
13319 + uint64_t bytes_free;
13321 + bytes_in_dev =
13322 + ((uint64_t)
13323 + ((dev->param.end_block - dev->param.start_block +
13324 + 1))) * ((uint64_t) (dev->param.chunks_per_block *
13325 + dev->data_bytes_per_chunk));
13327 + do_div(bytes_in_dev, sb->s_blocksize); /* bytes_in_dev becomes the number of blocks */
13328 + buf->f_blocks = bytes_in_dev;
13330 + bytes_free = ((uint64_t) (yaffs_get_n_free_chunks(dev))) *
13331 + ((uint64_t) (dev->data_bytes_per_chunk));
13333 + do_div(bytes_free, sb->s_blocksize);
13335 + buf->f_bfree = bytes_free;
13337 + } else if (sb->s_blocksize > dev->data_bytes_per_chunk) {
13339 + buf->f_blocks =
13340 + (dev->param.end_block - dev->param.start_block + 1) *
13341 + dev->param.chunks_per_block /
13342 + (sb->s_blocksize / dev->data_bytes_per_chunk);
13343 + buf->f_bfree =
13344 + yaffs_get_n_free_chunks(dev) /
13345 + (sb->s_blocksize / dev->data_bytes_per_chunk);
13346 + } else {
13347 + buf->f_blocks =
13348 + (dev->param.end_block - dev->param.start_block + 1) *
13349 + dev->param.chunks_per_block *
13350 + (dev->data_bytes_per_chunk / sb->s_blocksize);
13352 + buf->f_bfree =
13353 + yaffs_get_n_free_chunks(dev) *
13354 + (dev->data_bytes_per_chunk / sb->s_blocksize);
13357 + buf->f_files = 0;
13358 + buf->f_ffree = 0;
13359 + buf->f_bavail = buf->f_bfree;
13361 + yaffs_gross_unlock(dev);
13362 + return 0;
13367 +static int yaffs_do_sync_fs(struct super_block *sb, int request_checkpoint)
13370 + struct yaffs_dev *dev = yaffs_super_to_dev(sb);
13371 + unsigned int oneshot_checkpoint = (yaffs_auto_checkpoint & 4);
13372 + unsigned gc_urgent = yaffs_bg_gc_urgency(dev);
13373 + int do_checkpoint;
13374 + int dirty = yaffs_check_super_dirty(dev);
13376 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
13377 + "yaffs_do_sync_fs: gc-urgency %d %s %s%s",
13378 + gc_urgent,
13379 + dirty ? "dirty" : "clean",
13380 + request_checkpoint ? "checkpoint requested" : "no checkpoint",
13381 + oneshot_checkpoint ? " one-shot" : "");
13383 + yaffs_gross_lock(dev);
13384 + do_checkpoint = ((request_checkpoint && !gc_urgent) ||
13385 + oneshot_checkpoint) && !dev->is_checkpointed;
13387 + if (dirty || do_checkpoint) {
13388 + yaffs_flush_super(sb, !dev->is_checkpointed && do_checkpoint);
13389 + yaffs_clear_super_dirty(dev);
13390 + if (oneshot_checkpoint)
13391 + yaffs_auto_checkpoint &= ~4;
13393 + yaffs_gross_unlock(dev);
13395 + return 0;
13399 +#ifdef YAFFS_HAS_WRITE_SUPER
13400 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13401 +static void yaffs_write_super(struct super_block *sb)
13402 +#else
13403 +static int yaffs_write_super(struct super_block *sb)
13404 +#endif
13406 + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 2);
13408 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC | YAFFS_TRACE_BACKGROUND,
13409 + "yaffs_write_super %s",
13410 + request_checkpoint ? " checkpt" : "");
13412 + yaffs_do_sync_fs(sb, request_checkpoint);
13414 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
13415 + return 0;
13416 +#endif
13418 +#endif
13420 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13421 +static int yaffs_sync_fs(struct super_block *sb, int wait)
13422 +#else
13423 +static int yaffs_sync_fs(struct super_block *sb)
13424 +#endif
13426 + unsigned request_checkpoint = (yaffs_auto_checkpoint >= 1);
13428 + yaffs_trace(YAFFS_TRACE_OS | YAFFS_TRACE_SYNC,
13429 + "yaffs_sync_fs%s", request_checkpoint ? " checkpt" : "");
13431 + yaffs_do_sync_fs(sb, request_checkpoint);
13433 + return 0;
13438 +static const struct super_operations yaffs_super_ops = {
13439 + .statfs = yaffs_statfs,
13441 +#ifndef YAFFS_USE_OWN_IGET
13442 + .read_inode = yaffs_read_inode,
13443 +#endif
13444 +#ifdef YAFFS_HAS_PUT_INODE
13445 + .put_inode = yaffs_put_inode,
13446 +#endif
13447 + .put_super = yaffs_put_super,
13448 +#ifdef YAFFS_HAS_EVICT_INODE
13449 + .evict_inode = yaffs_evict_inode,
13450 +#else
13451 + .delete_inode = yaffs_delete_inode,
13452 + .clear_inode = yaffs_clear_inode,
13453 +#endif
13454 + .sync_fs = yaffs_sync_fs,
13455 +#ifdef YAFFS_HAS_WRITE_SUPER
13456 + .write_super = yaffs_write_super,
13457 +#endif
13460 +struct yaffs_options {
13461 + int inband_tags;
13462 + int skip_checkpoint_read;
13463 + int skip_checkpoint_write;
13464 + int no_cache;
13465 + int tags_ecc_on;
13466 + int tags_ecc_overridden;
13467 + int lazy_loading_enabled;
13468 + int lazy_loading_overridden;
13469 + int empty_lost_and_found;
13470 + int empty_lost_and_found_overridden;
13471 + int disable_summary;
13474 +#define MAX_OPT_LEN 30
13475 +static int yaffs_parse_options(struct yaffs_options *options,
13476 + const char *options_str)
13478 + char cur_opt[MAX_OPT_LEN + 1];
13479 + int p;
13480 + int error = 0;
13482 + /* Parse through the options which is a comma seperated list */
13484 + while (options_str && *options_str && !error) {
13485 + memset(cur_opt, 0, MAX_OPT_LEN + 1);
13486 + p = 0;
13488 + while (*options_str == ',')
13489 + options_str++;
13491 + while (*options_str && *options_str != ',') {
13492 + if (p < MAX_OPT_LEN) {
13493 + cur_opt[p] = *options_str;
13494 + p++;
13496 + options_str++;
13499 + if (!strcmp(cur_opt, "inband-tags")) {
13500 + options->inband_tags = 1;
13501 + } else if (!strcmp(cur_opt, "tags-ecc-off")) {
13502 + options->tags_ecc_on = 0;
13503 + options->tags_ecc_overridden = 1;
13504 + } else if (!strcmp(cur_opt, "tags-ecc-on")) {
13505 + options->tags_ecc_on = 1;
13506 + options->tags_ecc_overridden = 1;
13507 + } else if (!strcmp(cur_opt, "lazy-loading-off")) {
13508 + options->lazy_loading_enabled = 0;
13509 + options->lazy_loading_overridden = 1;
13510 + } else if (!strcmp(cur_opt, "lazy-loading-on")) {
13511 + options->lazy_loading_enabled = 1;
13512 + options->lazy_loading_overridden = 1;
13513 + } else if (!strcmp(cur_opt, "disable-summary")) {
13514 + options->disable_summary = 1;
13515 + } else if (!strcmp(cur_opt, "empty-lost-and-found-off")) {
13516 + options->empty_lost_and_found = 0;
13517 + options->empty_lost_and_found_overridden = 1;
13518 + } else if (!strcmp(cur_opt, "empty-lost-and-found-on")) {
13519 + options->empty_lost_and_found = 1;
13520 + options->empty_lost_and_found_overridden = 1;
13521 + } else if (!strcmp(cur_opt, "no-cache")) {
13522 + options->no_cache = 1;
13523 + } else if (!strcmp(cur_opt, "no-checkpoint-read")) {
13524 + options->skip_checkpoint_read = 1;
13525 + } else if (!strcmp(cur_opt, "no-checkpoint-write")) {
13526 + options->skip_checkpoint_write = 1;
13527 + } else if (!strcmp(cur_opt, "no-checkpoint")) {
13528 + options->skip_checkpoint_read = 1;
13529 + options->skip_checkpoint_write = 1;
13530 + } else {
13531 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",
13532 + cur_opt);
13533 + error = 1;
13537 + return error;
13541 +static struct dentry *yaffs_make_root(struct inode *inode)
13543 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 4, 0))
13544 + struct dentry *root = d_alloc_root(inode);
13546 + if (!root)
13547 + iput(inode);
13549 + return root;
13550 +#else
13551 + return d_make_root(inode);
13552 +#endif
13558 +static struct super_block *yaffs_internal_read_super(int yaffs_version,
13559 + struct super_block *sb,
13560 + void *data, int silent)
13562 + int n_blocks;
13563 + struct inode *inode = NULL;
13564 + struct dentry *root;
13565 + struct yaffs_dev *dev = 0;
13566 + char devname_buf[BDEVNAME_SIZE + 1];
13567 + struct mtd_info *mtd;
13568 + int err;
13569 + char *data_str = (char *)data;
13570 + struct yaffs_linux_context *context = NULL;
13571 + struct yaffs_param *param;
13573 + int read_only = 0;
13574 + int inband_tags = 0;
13576 + struct yaffs_options options;
13578 + unsigned mount_id;
13579 + int found;
13580 + struct yaffs_linux_context *context_iterator;
13581 + struct list_head *l;
13583 + if (!sb) {
13584 + printk(KERN_INFO "yaffs: sb is NULL\n");
13585 + return NULL;
13588 + sb->s_magic = YAFFS_MAGIC;
13589 + sb->s_op = &yaffs_super_ops;
13590 + sb->s_flags |= MS_NOATIME;
13592 + read_only = ((sb->s_flags & MS_RDONLY) != 0);
13594 +#ifdef YAFFS_COMPILE_EXPORTFS
13595 + sb->s_export_op = &yaffs_export_ops;
13596 +#endif
13598 + if (!sb->s_dev)
13599 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
13600 + else if (!yaffs_devname(sb, devname_buf))
13601 + printk(KERN_INFO "yaffs: devname is NULL\n");
13602 + else
13603 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\" %s\n",
13604 + sb->s_dev,
13605 + yaffs_devname(sb, devname_buf), read_only ? "ro" : "rw");
13607 + if (!data_str)
13608 + data_str = "";
13610 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n", data_str);
13612 + memset(&options, 0, sizeof(options));
13614 + if (yaffs_parse_options(&options, data_str)) {
13615 + /* Option parsing failed */
13616 + return NULL;
13619 + sb->s_blocksize = PAGE_CACHE_SIZE;
13620 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
13622 + yaffs_trace(YAFFS_TRACE_OS,
13623 + "yaffs_read_super: Using yaffs%d", yaffs_version);
13624 + yaffs_trace(YAFFS_TRACE_OS,
13625 + "yaffs_read_super: block size %d", (int)(sb->s_blocksize));
13627 + yaffs_trace(YAFFS_TRACE_ALWAYS,
13628 + "yaffs: Attempting MTD mount of %u.%u,\"%s\"",
13629 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
13630 + yaffs_devname(sb, devname_buf));
13632 + /* Get the device */
13633 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
13634 + if (IS_ERR(mtd)) {
13635 + yaffs_trace(YAFFS_TRACE_ALWAYS,
13636 + "yaffs: MTD device %u either not valid or unavailable",
13637 + MINOR(sb->s_dev));
13638 + return NULL;
13641 + if (yaffs_auto_select && yaffs_version == 1 && WRITE_SIZE(mtd) >= 2048) {
13642 + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs2");
13643 + yaffs_version = 2;
13646 + /* Added NCB 26/5/2006 for completeness */
13647 + if (yaffs_version == 2 && !options.inband_tags
13648 + && WRITE_SIZE(mtd) == 512) {
13649 + yaffs_trace(YAFFS_TRACE_ALWAYS, "auto selecting yaffs1");
13650 + yaffs_version = 1;
13653 + if (mtd->oobavail < sizeof(struct yaffs_packed_tags2) ||
13654 + options.inband_tags)
13655 + inband_tags = 1;
13657 + if(yaffs_verify_mtd(mtd, yaffs_version, inband_tags) < 0)
13658 + return NULL;
13660 + /* OK, so if we got here, we have an MTD that's NAND and looks
13661 + * like it has the right capabilities
13662 + * Set the struct yaffs_dev up for mtd
13663 + */
13665 + if (!read_only && !(mtd->flags & MTD_WRITEABLE)) {
13666 + read_only = 1;
13667 + printk(KERN_INFO
13668 + "yaffs: mtd is read only, setting superblock read only\n"
13669 + );
13670 + sb->s_flags |= MS_RDONLY;
13673 + dev = kmalloc(sizeof(struct yaffs_dev), GFP_KERNEL);
13674 + context = kmalloc(sizeof(struct yaffs_linux_context), GFP_KERNEL);
13676 + if (!dev || !context) {
13677 + kfree(dev);
13678 + kfree(context);
13679 + dev = NULL;
13680 + context = NULL;
13682 + /* Deep shit could not allocate device structure */
13683 + yaffs_trace(YAFFS_TRACE_ALWAYS,
13684 + "yaffs_read_super: Failed trying to allocate struct yaffs_dev."
13685 + );
13686 + return NULL;
13688 + memset(dev, 0, sizeof(struct yaffs_dev));
13689 + param = &(dev->param);
13691 + memset(context, 0, sizeof(struct yaffs_linux_context));
13692 + dev->os_context = context;
13693 + INIT_LIST_HEAD(&(context->context_list));
13694 + context->dev = dev;
13695 + context->super = sb;
13697 + dev->read_only = read_only;
13699 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
13700 + sb->s_fs_info = dev;
13701 +#else
13702 + sb->u.generic_sbp = dev;
13703 +#endif
13706 + dev->driver_context = mtd;
13707 + param->name = mtd->name;
13709 + /* Set up the memory size parameters.... */
13712 + param->n_reserved_blocks = 5;
13713 + param->n_caches = (options.no_cache) ? 0 : 10;
13714 + param->inband_tags = inband_tags;
13716 + param->enable_xattr = 1;
13717 + if (options.lazy_loading_overridden)
13718 + param->disable_lazy_load = !options.lazy_loading_enabled;
13720 + param->defered_dir_update = 1;
13722 + if (options.tags_ecc_overridden)
13723 + param->no_tags_ecc = !options.tags_ecc_on;
13725 + param->empty_lost_n_found = 1;
13726 + param->refresh_period = 500;
13727 + param->disable_summary = options.disable_summary;
13730 +#ifdef CONFIG_YAFFS_DISABLE_BAD_BLOCK_MARKING
13731 + param->disable_bad_block_marking = 1;
13732 +#endif
13733 + if (options.empty_lost_and_found_overridden)
13734 + param->empty_lost_n_found = options.empty_lost_and_found;
13736 + /* ... and the functions. */
13737 + if (yaffs_version == 2) {
13738 + param->is_yaffs2 = 1;
13739 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13740 + param->total_bytes_per_chunk = mtd->writesize;
13741 + param->chunks_per_block = mtd->erasesize / mtd->writesize;
13742 +#else
13743 + param->total_bytes_per_chunk = mtd->oobblock;
13744 + param->chunks_per_block = mtd->erasesize / mtd->oobblock;
13745 +#endif
13746 + n_blocks = YCALCBLOCKS(mtd->size, mtd->erasesize);
13748 + param->start_block = 0;
13749 + param->end_block = n_blocks - 1;
13750 + } else {
13751 + param->is_yaffs2 = 0;
13752 + n_blocks = YCALCBLOCKS(mtd->size,
13753 + YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
13755 + param->chunks_per_block = YAFFS_CHUNKS_PER_BLOCK;
13756 + param->total_bytes_per_chunk = YAFFS_BYTES_PER_CHUNK;
13759 + param->start_block = 0;
13760 + param->end_block = n_blocks - 1;
13762 + yaffs_mtd_drv_install(dev);
13764 + param->sb_dirty_fn = yaffs_set_super_dirty;
13765 + param->gc_control_fn = yaffs_gc_control_callback;
13767 + yaffs_dev_to_lc(dev)->super = sb;
13769 + param->use_nand_ecc = 1;
13771 + param->skip_checkpt_rd = options.skip_checkpoint_read;
13772 + param->skip_checkpt_wr = options.skip_checkpoint_write;
13774 + mutex_lock(&yaffs_context_lock);
13775 + /* Get a mount id */
13776 + found = 0;
13777 + for (mount_id = 0; !found; mount_id++) {
13778 + found = 1;
13779 + list_for_each(l, &yaffs_context_list) {
13780 + context_iterator =
13781 + list_entry(l, struct yaffs_linux_context,
13782 + context_list);
13783 + if (context_iterator->mount_id == mount_id)
13784 + found = 0;
13787 + context->mount_id = mount_id;
13789 + list_add_tail(&(yaffs_dev_to_lc(dev)->context_list),
13790 + &yaffs_context_list);
13791 + mutex_unlock(&yaffs_context_lock);
13793 + /* Directory search handling... */
13794 + INIT_LIST_HEAD(&(yaffs_dev_to_lc(dev)->search_contexts));
13795 + param->remove_obj_fn = yaffs_remove_obj_callback;
13797 + mutex_init(&(yaffs_dev_to_lc(dev)->gross_lock));
13799 + yaffs_gross_lock(dev);
13801 + err = yaffs_guts_initialise(dev);
13803 + yaffs_trace(YAFFS_TRACE_OS,
13804 + "yaffs_read_super: guts initialised %s",
13805 + (err == YAFFS_OK) ? "OK" : "FAILED");
13807 + if (err == YAFFS_OK)
13808 + yaffs_bg_start(dev);
13810 + if (!context->bg_thread)
13811 + param->defered_dir_update = 0;
13813 + sb->s_maxbytes = yaffs_max_file_size(dev);
13815 + /* Release lock before yaffs_get_inode() */
13816 + yaffs_gross_unlock(dev);
13818 + /* Create root inode */
13819 + if (err == YAFFS_OK)
13820 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0, yaffs_root(dev));
13822 + if (!inode)
13823 + return NULL;
13825 + inode->i_op = &yaffs_dir_inode_operations;
13826 + inode->i_fop = &yaffs_dir_operations;
13828 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: got root inode");
13830 + root = yaffs_make_root(inode);
13832 + if (!root)
13833 + return NULL;
13835 + sb->s_root = root;
13836 + if(!dev->is_checkpointed)
13837 + yaffs_set_super_dirty(dev);
13839 + yaffs_trace(YAFFS_TRACE_ALWAYS,
13840 + "yaffs_read_super: is_checkpointed %d",
13841 + dev->is_checkpointed);
13843 + yaffs_trace(YAFFS_TRACE_OS, "yaffs_read_super: done");
13844 + return sb;
13847 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
13848 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
13849 + int silent)
13851 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
13854 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
13855 +static struct dentry *yaffs_mount(struct file_system_type *fs_type, int flags,
13856 + const char *dev_name, void *data)
13858 + return mount_bdev(fs_type, flags, dev_name, data, yaffs_internal_read_super_mtd);
13860 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13861 +static int yaffs_read_super(struct file_system_type *fs,
13862 + int flags, const char *dev_name,
13863 + void *data, struct vfsmount *mnt)
13866 + return get_sb_bdev(fs, flags, dev_name, data,
13867 + yaffs_internal_read_super_mtd, mnt);
13869 +#else
13870 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
13871 + int flags, const char *dev_name,
13872 + void *data)
13875 + return get_sb_bdev(fs, flags, dev_name, data,
13876 + yaffs_internal_read_super_mtd);
13878 +#endif
13880 +static struct file_system_type yaffs_fs_type = {
13881 + .owner = THIS_MODULE,
13882 + .name = "yaffs",
13883 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
13884 + .mount = yaffs_mount,
13885 +#else
13886 + .get_sb = yaffs_read_super,
13887 +#endif
13888 + .kill_sb = kill_block_super,
13889 + .fs_flags = FS_REQUIRES_DEV,
13891 +#else
13892 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
13893 + int silent)
13895 + return yaffs_internal_read_super(1, sb, data, silent);
13898 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
13899 + FS_REQUIRES_DEV);
13900 +#endif
13903 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
13904 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
13905 + int silent)
13907 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
13910 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
13911 +static struct dentry *yaffs2_mount(struct file_system_type *fs_type, int flags,
13912 + const char *dev_name, void *data)
13914 + return mount_bdev(fs_type, flags, dev_name, data, yaffs2_internal_read_super_mtd);
13916 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17))
13917 +static int yaffs2_read_super(struct file_system_type *fs,
13918 + int flags, const char *dev_name, void *data,
13919 + struct vfsmount *mnt)
13921 + return get_sb_bdev(fs, flags, dev_name, data,
13922 + yaffs2_internal_read_super_mtd, mnt);
13924 +#else
13925 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
13926 + int flags, const char *dev_name,
13927 + void *data)
13930 + return get_sb_bdev(fs, flags, dev_name, data,
13931 + yaffs2_internal_read_super_mtd);
13933 +#endif
13935 +static struct file_system_type yaffs2_fs_type = {
13936 + .owner = THIS_MODULE,
13937 + .name = "yaffs2",
13938 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 39))
13939 + .mount = yaffs2_mount,
13940 +#else
13941 + .get_sb = yaffs2_read_super,
13942 +#endif
13943 + .kill_sb = kill_block_super,
13944 + .fs_flags = FS_REQUIRES_DEV,
13946 +#else
13947 +static struct super_block *yaffs2_read_super(struct super_block *sb,
13948 + void *data, int silent)
13950 + return yaffs_internal_read_super(2, sb, data, silent);
13953 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
13954 + FS_REQUIRES_DEV);
13955 +#endif
13958 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
13959 +static struct proc_dir_entry *my_proc_entry;
13961 +static char *yaffs_dump_dev_part0(char *buf, struct yaffs_dev *dev)
13963 + struct yaffs_param *param = &dev->param;
13964 + int bs[10];
13966 + yaffs_count_blocks_by_state(dev,bs);
13968 + buf += sprintf(buf, "start_block.......... %d\n", param->start_block);
13969 + buf += sprintf(buf, "end_block............ %d\n", param->end_block);
13970 + buf += sprintf(buf, "total_bytes_per_chunk %d\n",
13971 + param->total_bytes_per_chunk);
13972 + buf += sprintf(buf, "use_nand_ecc......... %d\n", param->use_nand_ecc);
13973 + buf += sprintf(buf, "no_tags_ecc.......... %d\n", param->no_tags_ecc);
13974 + buf += sprintf(buf, "is_yaffs2............ %d\n", param->is_yaffs2);
13975 + buf += sprintf(buf, "inband_tags.......... %d\n", param->inband_tags);
13976 + buf += sprintf(buf, "empty_lost_n_found... %d\n",
13977 + param->empty_lost_n_found);
13978 + buf += sprintf(buf, "disable_lazy_load.... %d\n",
13979 + param->disable_lazy_load);
13980 + buf += sprintf(buf, "disable_bad_block_mrk %d\n",
13981 + param->disable_bad_block_marking);
13982 + buf += sprintf(buf, "refresh_period....... %d\n",
13983 + param->refresh_period);
13984 + buf += sprintf(buf, "n_caches............. %d\n", param->n_caches);
13985 + buf += sprintf(buf, "n_reserved_blocks.... %d\n",
13986 + param->n_reserved_blocks);
13987 + buf += sprintf(buf, "always_check_erased.. %d\n",
13988 + param->always_check_erased);
13989 + buf += sprintf(buf, "\n");
13990 + buf += sprintf(buf, "block count by state\n");
13991 + buf += sprintf(buf, "0:%d 1:%d 2:%d 3:%d 4:%d\n",
13992 + bs[0], bs[1], bs[2], bs[3], bs[4]);
13993 + buf += sprintf(buf, "5:%d 6:%d 7:%d 8:%d 9:%d\n",
13994 + bs[5], bs[6], bs[7], bs[8], bs[9]);
13996 + return buf;
13999 +static char *yaffs_dump_dev_part1(char *buf, struct yaffs_dev *dev)
14001 + buf += sprintf(buf, "max file size....... %lld\n",
14002 + (long long) yaffs_max_file_size(dev));
14003 + buf += sprintf(buf, "data_bytes_per_chunk. %d\n",
14004 + dev->data_bytes_per_chunk);
14005 + buf += sprintf(buf, "chunk_grp_bits....... %d\n", dev->chunk_grp_bits);
14006 + buf += sprintf(buf, "chunk_grp_size....... %d\n", dev->chunk_grp_size);
14007 + buf += sprintf(buf, "n_erased_blocks...... %d\n", dev->n_erased_blocks);
14008 + buf += sprintf(buf, "blocks_in_checkpt.... %d\n",
14009 + dev->blocks_in_checkpt);
14010 + buf += sprintf(buf, "\n");
14011 + buf += sprintf(buf, "n_tnodes............. %d\n", dev->n_tnodes);
14012 + buf += sprintf(buf, "n_obj................ %d\n", dev->n_obj);
14013 + buf += sprintf(buf, "n_free_chunks........ %d\n", dev->n_free_chunks);
14014 + buf += sprintf(buf, "\n");
14015 + buf += sprintf(buf, "n_page_writes........ %u\n", dev->n_page_writes);
14016 + buf += sprintf(buf, "n_page_reads......... %u\n", dev->n_page_reads);
14017 + buf += sprintf(buf, "n_erasures........... %u\n", dev->n_erasures);
14018 + buf += sprintf(buf, "n_gc_copies.......... %u\n", dev->n_gc_copies);
14019 + buf += sprintf(buf, "all_gcs.............. %u\n", dev->all_gcs);
14020 + buf += sprintf(buf, "passive_gc_count..... %u\n",
14021 + dev->passive_gc_count);
14022 + buf += sprintf(buf, "oldest_dirty_gc_count %u\n",
14023 + dev->oldest_dirty_gc_count);
14024 + buf += sprintf(buf, "n_gc_blocks.......... %u\n", dev->n_gc_blocks);
14025 + buf += sprintf(buf, "bg_gcs............... %u\n", dev->bg_gcs);
14026 + buf += sprintf(buf, "n_retried_writes..... %u\n",
14027 + dev->n_retried_writes);
14028 + buf += sprintf(buf, "n_retired_blocks..... %u\n",
14029 + dev->n_retired_blocks);
14030 + buf += sprintf(buf, "n_ecc_fixed.......... %u\n", dev->n_ecc_fixed);
14031 + buf += sprintf(buf, "n_ecc_unfixed........ %u\n", dev->n_ecc_unfixed);
14032 + buf += sprintf(buf, "n_tags_ecc_fixed..... %u\n",
14033 + dev->n_tags_ecc_fixed);
14034 + buf += sprintf(buf, "n_tags_ecc_unfixed... %u\n",
14035 + dev->n_tags_ecc_unfixed);
14036 + buf += sprintf(buf, "cache_hits........... %u\n", dev->cache_hits);
14037 + buf += sprintf(buf, "n_deleted_files...... %u\n", dev->n_deleted_files);
14038 + buf += sprintf(buf, "n_unlinked_files..... %u\n",
14039 + dev->n_unlinked_files);
14040 + buf += sprintf(buf, "refresh_count........ %u\n", dev->refresh_count);
14041 + buf += sprintf(buf, "n_bg_deletions....... %u\n", dev->n_bg_deletions);
14042 + buf += sprintf(buf, "tags_used............ %u\n", dev->tags_used);
14043 + buf += sprintf(buf, "summary_used......... %u\n", dev->summary_used);
14045 + return buf;
14048 +static int yaffs_proc_read(char *page,
14049 + char **start,
14050 + off_t offset, int count, int *eof, void *data)
14052 + struct list_head *item;
14053 + char *buf = page;
14054 + int step = offset;
14055 + int n = 0;
14057 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
14058 + * We use 'offset' (*ppos) to indicate where we are in dev_list.
14059 + * This also assumes the user has posted a read buffer large
14060 + * enough to hold the complete output; but that's life in /proc.
14061 + */
14063 + *(int *)start = 1;
14065 + /* Print header first */
14066 + if (step == 0)
14067 + buf +=
14068 + sprintf(buf,
14069 + "Multi-version YAFFS built:" __DATE__ " " __TIME__
14070 + "\n");
14071 + else if (step == 1)
14072 + buf += sprintf(buf, "\n");
14073 + else {
14074 + step -= 2;
14076 + mutex_lock(&yaffs_context_lock);
14078 + /* Locate and print the Nth entry. Order N-squared but N is small. */
14079 + list_for_each(item, &yaffs_context_list) {
14080 + struct yaffs_linux_context *dc =
14081 + list_entry(item, struct yaffs_linux_context,
14082 + context_list);
14083 + struct yaffs_dev *dev = dc->dev;
14085 + if (n < (step & ~1)) {
14086 + n += 2;
14087 + continue;
14089 + if ((step & 1) == 0) {
14090 + buf +=
14091 + sprintf(buf, "\nDevice %d \"%s\"\n", n,
14092 + dev->param.name);
14093 + buf = yaffs_dump_dev_part0(buf, dev);
14094 + } else {
14095 + buf = yaffs_dump_dev_part1(buf, dev);
14098 + break;
14100 + mutex_unlock(&yaffs_context_lock);
14103 + return buf - page < count ? buf - page : count;
14106 +/**
14107 + * Set the verbosity of the warnings and error messages.
14109 + * Note that the names can only be a..z or _ with the current code.
14110 + */
14112 +static struct {
14113 + char *mask_name;
14114 + unsigned mask_bitfield;
14115 +} mask_flags[] = {
14116 + {"allocate", YAFFS_TRACE_ALLOCATE},
14117 + {"always", YAFFS_TRACE_ALWAYS},
14118 + {"background", YAFFS_TRACE_BACKGROUND},
14119 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
14120 + {"buffers", YAFFS_TRACE_BUFFERS},
14121 + {"bug", YAFFS_TRACE_BUG},
14122 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
14123 + {"deletion", YAFFS_TRACE_DELETION},
14124 + {"erase", YAFFS_TRACE_ERASE},
14125 + {"error", YAFFS_TRACE_ERROR},
14126 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
14127 + {"gc", YAFFS_TRACE_GC},
14128 + {"lock", YAFFS_TRACE_LOCK},
14129 + {"mtd", YAFFS_TRACE_MTD},
14130 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
14131 + {"os", YAFFS_TRACE_OS},
14132 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
14133 + {"scan", YAFFS_TRACE_SCAN},
14134 + {"mount", YAFFS_TRACE_MOUNT},
14135 + {"tracing", YAFFS_TRACE_TRACING},
14136 + {"sync", YAFFS_TRACE_SYNC},
14137 + {"write", YAFFS_TRACE_WRITE},
14138 + {"verify", YAFFS_TRACE_VERIFY},
14139 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
14140 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
14141 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
14142 + {"all", 0xffffffff},
14143 + {"none", 0},
14144 + {NULL, 0},
14147 +#define MAX_MASK_NAME_LENGTH 40
14148 +static int yaffs_proc_write_trace_options(struct file *file, const char *buf,
14149 + unsigned long count, void *data)
14151 + unsigned rg = 0, mask_bitfield;
14152 + char *end;
14153 + char *mask_name;
14154 + const char *x;
14155 + char substring[MAX_MASK_NAME_LENGTH + 1];
14156 + int i;
14157 + int done = 0;
14158 + int add, len = 0;
14159 + int pos = 0;
14161 + rg = yaffs_trace_mask;
14163 + while (!done && (pos < count)) {
14164 + done = 1;
14165 + while ((pos < count) && isspace(buf[pos]))
14166 + pos++;
14168 + switch (buf[pos]) {
14169 + case '+':
14170 + case '-':
14171 + case '=':
14172 + add = buf[pos];
14173 + pos++;
14174 + break;
14176 + default:
14177 + add = ' ';
14178 + break;
14180 + mask_name = NULL;
14182 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
14184 + if (end > buf + pos) {
14185 + mask_name = "numeral";
14186 + len = end - (buf + pos);
14187 + pos += len;
14188 + done = 0;
14189 + } else {
14190 + for (x = buf + pos, i = 0;
14191 + (*x == '_' || (*x >= 'a' && *x <= 'z')) &&
14192 + i < MAX_MASK_NAME_LENGTH; x++, i++, pos++)
14193 + substring[i] = *x;
14194 + substring[i] = '\0';
14196 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
14197 + if (strcmp(substring, mask_flags[i].mask_name)
14198 + == 0) {
14199 + mask_name = mask_flags[i].mask_name;
14200 + mask_bitfield =
14201 + mask_flags[i].mask_bitfield;
14202 + done = 0;
14203 + break;
14208 + if (mask_name != NULL) {
14209 + done = 0;
14210 + switch (add) {
14211 + case '-':
14212 + rg &= ~mask_bitfield;
14213 + break;
14214 + case '+':
14215 + rg |= mask_bitfield;
14216 + break;
14217 + case '=':
14218 + rg = mask_bitfield;
14219 + break;
14220 + default:
14221 + rg |= mask_bitfield;
14222 + break;
14227 + yaffs_trace_mask = rg | YAFFS_TRACE_ALWAYS;
14229 + printk(KERN_DEBUG "new trace = 0x%08X\n", yaffs_trace_mask);
14231 + if (rg & YAFFS_TRACE_ALWAYS) {
14232 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
14233 + char flag;
14234 + flag = ((rg & mask_flags[i].mask_bitfield) ==
14235 + mask_flags[i].mask_bitfield) ? '+' : '-';
14236 + printk(KERN_DEBUG "%c%s\n", flag,
14237 + mask_flags[i].mask_name);
14241 + return count;
14244 +/* Debug strings are of the form:
14245 + * .bnnn print info on block n
14246 + * .cobjn,chunkn print nand chunk id for objn:chunkn
14247 + */
14249 +static int yaffs_proc_debug_write(struct file *file, const char *buf,
14250 + unsigned long count, void *data)
14253 + char str[100];
14254 + char *p0;
14255 + char *p1;
14256 + long p1_val;
14257 + long p0_val;
14258 + char cmd;
14259 + struct list_head *item;
14261 + memset(str, 0, sizeof(str));
14262 + memcpy(str, buf, min(count, sizeof(str) -1));
14264 + cmd = str[1];
14266 + p0 = str + 2;
14268 + p1 = p0;
14270 + while (*p1 && *p1 != ',') {
14271 + p1++;
14273 + *p1 = '\0';
14274 + p1++;
14276 + p0_val = simple_strtol(p0, NULL, 0);
14277 + p1_val = simple_strtol(p1, NULL, 0);
14280 + mutex_lock(&yaffs_context_lock);
14282 + /* Locate and print the Nth entry. Order N-squared but N is small. */
14283 + list_for_each(item, &yaffs_context_list) {
14284 + struct yaffs_linux_context *dc =
14285 + list_entry(item, struct yaffs_linux_context,
14286 + context_list);
14287 + struct yaffs_dev *dev = dc->dev;
14289 + if (cmd == 'b') {
14290 + struct yaffs_block_info *bi;
14292 + bi = yaffs_get_block_info(dev,p0_val);
14294 + if(bi) {
14295 + printk("Block %d: state %d, retire %d, use %d, seq %d\n",
14296 + (int)p0_val, bi->block_state,
14297 + bi->needs_retiring, bi->pages_in_use,
14298 + bi->seq_number);
14300 + } else if (cmd == 'c') {
14301 + struct yaffs_obj *obj;
14302 + int nand_chunk;
14304 + obj = yaffs_find_by_number(dev, p0_val);
14305 + if (!obj)
14306 + printk("No obj %d\n", (int)p0_val);
14307 + else {
14308 + if(p1_val == 0)
14309 + nand_chunk = obj->hdr_chunk;
14310 + else
14311 + nand_chunk =
14312 + yaffs_find_chunk_in_file(obj,
14313 + p1_val, NULL);
14314 + printk("Nand chunk for %d:%d is %d\n",
14315 + (int)p0_val, (int)p1_val, nand_chunk);
14320 + mutex_unlock(&yaffs_context_lock);
14322 + return count;
14325 +static int yaffs_proc_write(struct file *file, const char *buf,
14326 + unsigned long count, void *data)
14328 + if (buf[0] == '.')
14329 + return yaffs_proc_debug_write(file, buf, count, data);
14330 + return yaffs_proc_write_trace_options(file, buf, count, data);
14332 +#endif /* (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)) */
14334 +/* Stuff to handle installation of file systems */
14335 +struct file_system_to_install {
14336 + struct file_system_type *fst;
14337 + int installed;
14340 +static struct file_system_to_install fs_to_install[] = {
14341 + {&yaffs_fs_type, 0},
14342 + {&yaffs2_fs_type, 0},
14343 + {NULL, 0}
14346 +static int __init init_yaffs_fs(void)
14348 + int error = 0;
14349 + struct file_system_to_install *fsinst;
14351 + yaffs_trace(YAFFS_TRACE_ALWAYS,
14352 + "yaffs built " __DATE__ " " __TIME__ " Installing.");
14354 + mutex_init(&yaffs_context_lock);
14356 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
14357 + /* Install the proc_fs entries */
14358 + my_proc_entry = create_proc_entry("yaffs",
14359 + S_IRUGO | S_IFREG, YPROC_ROOT);
14361 + if (my_proc_entry) {
14362 + my_proc_entry->write_proc = yaffs_proc_write;
14363 + my_proc_entry->read_proc = yaffs_proc_read;
14364 + my_proc_entry->data = NULL;
14365 + } else {
14366 + return -ENOMEM;
14368 +#endif
14370 + /* Now add the file system entries */
14372 + fsinst = fs_to_install;
14374 + while (fsinst->fst && !error) {
14375 + error = register_filesystem(fsinst->fst);
14376 + if (!error)
14377 + fsinst->installed = 1;
14378 + fsinst++;
14381 + /* Any errors? uninstall */
14382 + if (error) {
14383 + fsinst = fs_to_install;
14385 + while (fsinst->fst) {
14386 + if (fsinst->installed) {
14387 + unregister_filesystem(fsinst->fst);
14388 + fsinst->installed = 0;
14390 + fsinst++;
14394 + return error;
14397 +static void __exit exit_yaffs_fs(void)
14400 + struct file_system_to_install *fsinst;
14402 + yaffs_trace(YAFFS_TRACE_ALWAYS,
14403 + "yaffs built " __DATE__ " " __TIME__ " removing.");
14405 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0))
14406 + remove_proc_entry("yaffs", YPROC_ROOT);
14407 +#endif
14409 + fsinst = fs_to_install;
14411 + while (fsinst->fst) {
14412 + if (fsinst->installed) {
14413 + unregister_filesystem(fsinst->fst);
14414 + fsinst->installed = 0;
14416 + fsinst++;
14420 +module_init(init_yaffs_fs)
14421 + module_exit(exit_yaffs_fs)
14423 + MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
14424 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2011");
14425 +MODULE_LICENSE("GPL");
14426 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs1.c linux-3.12.20/fs/yaffs2/yaffs_yaffs1.c
14427 --- linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs1.c 1970-01-01 01:00:00.000000000 +0100
14428 +++ linux-3.12.20/fs/yaffs2/yaffs_yaffs1.c 2014-05-17 11:28:49.000000000 +0200
14429 @@ -0,0 +1,422 @@
14431 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14433 + * Copyright (C) 2002-2011 Aleph One Ltd.
14434 + * for Toby Churchill Ltd and Brightstar Engineering
14436 + * Created by Charles Manning <charles@aleph1.co.uk>
14438 + * This program is free software; you can redistribute it and/or modify
14439 + * it under the terms of the GNU General Public License version 2 as
14440 + * published by the Free Software Foundation.
14441 + */
14443 +#include "yaffs_yaffs1.h"
14444 +#include "yportenv.h"
14445 +#include "yaffs_trace.h"
14446 +#include "yaffs_bitmap.h"
14447 +#include "yaffs_getblockinfo.h"
14448 +#include "yaffs_nand.h"
14449 +#include "yaffs_attribs.h"
14451 +int yaffs1_scan(struct yaffs_dev *dev)
14453 + struct yaffs_ext_tags tags;
14454 + int blk;
14455 + int result;
14456 + int chunk;
14457 + int c;
14458 + int deleted;
14459 + enum yaffs_block_state state;
14460 + LIST_HEAD(hard_list);
14461 + struct yaffs_block_info *bi;
14462 + u32 seq_number;
14463 + struct yaffs_obj_hdr *oh;
14464 + struct yaffs_obj *in;
14465 + struct yaffs_obj *parent;
14466 + int alloc_failed = 0;
14467 + struct yaffs_shadow_fixer *shadow_fixers = NULL;
14468 + u8 *chunk_data;
14470 + yaffs_trace(YAFFS_TRACE_SCAN,
14471 + "yaffs1_scan starts intstartblk %d intendblk %d...",
14472 + dev->internal_start_block, dev->internal_end_block);
14474 + chunk_data = yaffs_get_temp_buffer(dev);
14476 + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
14478 + /* Scan all the blocks to determine their state */
14479 + bi = dev->block_info;
14480 + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
14481 + blk++) {
14482 + yaffs_clear_chunk_bits(dev, blk);
14483 + bi->pages_in_use = 0;
14484 + bi->soft_del_pages = 0;
14486 + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
14488 + bi->block_state = state;
14489 + bi->seq_number = seq_number;
14491 + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
14492 + bi->block_state = state = YAFFS_BLOCK_STATE_DEAD;
14494 + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
14495 + "Block scanning block %d state %d seq %d",
14496 + blk, state, seq_number);
14498 + if (state == YAFFS_BLOCK_STATE_DEAD) {
14499 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
14500 + "block %d is bad", blk);
14501 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
14502 + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
14503 + dev->n_erased_blocks++;
14504 + dev->n_free_chunks += dev->param.chunks_per_block;
14506 + bi++;
14509 + /* For each block.... */
14510 + for (blk = dev->internal_start_block;
14511 + !alloc_failed && blk <= dev->internal_end_block; blk++) {
14513 + cond_resched();
14515 + bi = yaffs_get_block_info(dev, blk);
14516 + state = bi->block_state;
14518 + deleted = 0;
14520 + /* For each chunk in each block that needs scanning.... */
14521 + for (c = 0;
14522 + !alloc_failed && c < dev->param.chunks_per_block &&
14523 + state == YAFFS_BLOCK_STATE_NEEDS_SCAN; c++) {
14524 + /* Read the tags and decide what to do */
14525 + chunk = blk * dev->param.chunks_per_block + c;
14527 + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL,
14528 + &tags);
14530 + /* Let's have a good look at this chunk... */
14532 + if (tags.ecc_result == YAFFS_ECC_RESULT_UNFIXED ||
14533 + tags.is_deleted) {
14534 + /* YAFFS1 only...
14535 + * A deleted chunk
14536 + */
14537 + deleted++;
14538 + dev->n_free_chunks++;
14539 + } else if (!tags.chunk_used) {
14540 + /* An unassigned chunk in the block
14541 + * This means that either the block is empty or
14542 + * this is the one being allocated from
14543 + */
14545 + if (c == 0) {
14546 + /* We're looking at the first chunk in
14547 + *the block so the block is unused */
14548 + state = YAFFS_BLOCK_STATE_EMPTY;
14549 + dev->n_erased_blocks++;
14550 + } else {
14551 + /* this is the block being allocated */
14552 + yaffs_trace(YAFFS_TRACE_SCAN,
14553 + " Allocating from %d %d",
14554 + blk, c);
14555 + state = YAFFS_BLOCK_STATE_ALLOCATING;
14556 + dev->alloc_block = blk;
14557 + dev->alloc_page = c;
14558 + dev->alloc_block_finder = blk;
14562 + dev->n_free_chunks +=
14563 + (dev->param.chunks_per_block - c);
14564 + } else if (tags.chunk_id > 0) {
14565 + /* chunk_id > 0 so it is a data chunk... */
14566 + unsigned int endpos;
14568 + yaffs_set_chunk_bit(dev, blk, c);
14569 + bi->pages_in_use++;
14571 + in = yaffs_find_or_create_by_number(dev,
14572 + tags.obj_id,
14573 + YAFFS_OBJECT_TYPE_FILE);
14574 + /* PutChunkIntoFile checks for a clash
14575 + * (two data chunks with the same chunk_id).
14576 + */
14578 + if (!in)
14579 + alloc_failed = 1;
14581 + if (in) {
14582 + if (!yaffs_put_chunk_in_file
14583 + (in, tags.chunk_id, chunk, 1))
14584 + alloc_failed = 1;
14587 + endpos =
14588 + (tags.chunk_id - 1) *
14589 + dev->data_bytes_per_chunk +
14590 + tags.n_bytes;
14591 + if (in &&
14592 + in->variant_type ==
14593 + YAFFS_OBJECT_TYPE_FILE &&
14594 + in->variant.file_variant.scanned_size <
14595 + endpos) {
14596 + in->variant.file_variant.scanned_size =
14597 + endpos;
14598 + if (!dev->param.use_header_file_size) {
14599 + in->variant.
14600 + file_variant.file_size =
14601 + in->variant.
14602 + file_variant.scanned_size;
14606 + } else {
14607 + /* chunk_id == 0, so it is an ObjectHeader.
14608 + * Make the object
14609 + */
14610 + yaffs_set_chunk_bit(dev, blk, c);
14611 + bi->pages_in_use++;
14613 + result = yaffs_rd_chunk_tags_nand(dev, chunk,
14614 + chunk_data,
14615 + NULL);
14617 + oh = (struct yaffs_obj_hdr *)chunk_data;
14619 + in = yaffs_find_by_number(dev, tags.obj_id);
14620 + if (in && in->variant_type != oh->type) {
14621 + /* This should not happen, but somehow
14622 + * Wev'e ended up with an obj_id that
14623 + * has been reused but not yet deleted,
14624 + * and worse still it has changed type.
14625 + * Delete the old object.
14626 + */
14628 + yaffs_del_obj(in);
14629 + in = NULL;
14632 + in = yaffs_find_or_create_by_number(dev,
14633 + tags.obj_id,
14634 + oh->type);
14636 + if (!in)
14637 + alloc_failed = 1;
14639 + if (in && oh->shadows_obj > 0) {
14641 + struct yaffs_shadow_fixer *fixer;
14642 + fixer =
14643 + kmalloc(sizeof
14644 + (struct yaffs_shadow_fixer),
14645 + GFP_NOFS);
14646 + if (fixer) {
14647 + fixer->next = shadow_fixers;
14648 + shadow_fixers = fixer;
14649 + fixer->obj_id = tags.obj_id;
14650 + fixer->shadowed_id =
14651 + oh->shadows_obj;
14652 + yaffs_trace(YAFFS_TRACE_SCAN,
14653 + " Shadow fixer: %d shadows %d",
14654 + fixer->obj_id,
14655 + fixer->shadowed_id);
14661 + if (in && in->valid) {
14662 + /* We have already filled this one.
14663 + * We have a duplicate and need to
14664 + * resolve it. */
14666 + unsigned existing_serial = in->serial;
14667 + unsigned new_serial =
14668 + tags.serial_number;
14670 + if (((existing_serial + 1) & 3) ==
14671 + new_serial) {
14672 + /* Use new one - destroy the
14673 + * exisiting one */
14674 + yaffs_chunk_del(dev,
14675 + in->hdr_chunk,
14676 + 1, __LINE__);
14677 + in->valid = 0;
14678 + } else {
14679 + /* Use existing - destroy
14680 + * this one. */
14681 + yaffs_chunk_del(dev, chunk, 1,
14682 + __LINE__);
14686 + if (in && !in->valid &&
14687 + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
14688 + tags.obj_id ==
14689 + YAFFS_OBJECTID_LOSTNFOUND)) {
14690 + /* We only load some info, don't fiddle
14691 + * with directory structure */
14692 + in->valid = 1;
14693 + in->variant_type = oh->type;
14695 + in->yst_mode = oh->yst_mode;
14696 + yaffs_load_attribs(in, oh);
14697 + in->hdr_chunk = chunk;
14698 + in->serial = tags.serial_number;
14700 + } else if (in && !in->valid) {
14701 + /* we need to load this info */
14703 + in->valid = 1;
14704 + in->variant_type = oh->type;
14706 + in->yst_mode = oh->yst_mode;
14707 + yaffs_load_attribs(in, oh);
14708 + in->hdr_chunk = chunk;
14709 + in->serial = tags.serial_number;
14711 + yaffs_set_obj_name_from_oh(in, oh);
14712 + in->dirty = 0;
14714 + /* directory stuff...
14715 + * hook up to parent
14716 + */
14718 + parent =
14719 + yaffs_find_or_create_by_number
14720 + (dev, oh->parent_obj_id,
14721 + YAFFS_OBJECT_TYPE_DIRECTORY);
14722 + if (!parent)
14723 + alloc_failed = 1;
14724 + if (parent && parent->variant_type ==
14725 + YAFFS_OBJECT_TYPE_UNKNOWN) {
14726 + /* Set up as a directory */
14727 + parent->variant_type =
14728 + YAFFS_OBJECT_TYPE_DIRECTORY;
14729 + INIT_LIST_HEAD(&parent->
14730 + variant.dir_variant.
14731 + children);
14732 + } else if (!parent ||
14733 + parent->variant_type !=
14734 + YAFFS_OBJECT_TYPE_DIRECTORY) {
14735 + /* Hoosterman, a problem....
14736 + * We're trying to use a
14737 + * non-directory as a directory
14738 + */
14740 + yaffs_trace(YAFFS_TRACE_ERROR,
14741 + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
14742 + );
14743 + parent = dev->lost_n_found;
14746 + yaffs_add_obj_to_dir(parent, in);
14748 + switch (in->variant_type) {
14749 + case YAFFS_OBJECT_TYPE_UNKNOWN:
14750 + /* Todo got a problem */
14751 + break;
14752 + case YAFFS_OBJECT_TYPE_FILE:
14753 + if (dev->param.
14754 + use_header_file_size)
14755 + in->variant.
14756 + file_variant.file_size
14757 + = yaffs_oh_to_size(oh);
14758 + break;
14759 + case YAFFS_OBJECT_TYPE_HARDLINK:
14760 + in->variant.
14761 + hardlink_variant.equiv_id =
14762 + oh->equiv_id;
14763 + list_add(&in->hard_links,
14764 + &hard_list);
14765 + break;
14766 + case YAFFS_OBJECT_TYPE_DIRECTORY:
14767 + /* Do nothing */
14768 + break;
14769 + case YAFFS_OBJECT_TYPE_SPECIAL:
14770 + /* Do nothing */
14771 + break;
14772 + case YAFFS_OBJECT_TYPE_SYMLINK:
14773 + in->variant.symlink_variant.
14774 + alias =
14775 + yaffs_clone_str(oh->alias);
14776 + if (!in->variant.
14777 + symlink_variant.alias)
14778 + alloc_failed = 1;
14779 + break;
14785 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
14786 + /* If we got this far while scanning,
14787 + * then the block is fully allocated. */
14788 + state = YAFFS_BLOCK_STATE_FULL;
14791 + if (state == YAFFS_BLOCK_STATE_ALLOCATING) {
14792 + /* If the block was partially allocated then
14793 + * treat it as fully allocated. */
14794 + state = YAFFS_BLOCK_STATE_FULL;
14795 + dev->alloc_block = -1;
14798 + bi->block_state = state;
14800 + /* Now let's see if it was dirty */
14801 + if (bi->pages_in_use == 0 &&
14802 + !bi->has_shrink_hdr &&
14803 + bi->block_state == YAFFS_BLOCK_STATE_FULL)
14804 + yaffs_block_became_dirty(dev, blk);
14807 + /* Ok, we've done all the scanning.
14808 + * Fix up the hard link chains.
14809 + * We should now have scanned all the objects, now it's time to add
14810 + * these hardlinks.
14811 + */
14813 + yaffs_link_fixup(dev, &hard_list);
14815 + /*
14816 + * Fix up any shadowed objects.
14817 + * There should not be more than one of these.
14818 + */
14820 + struct yaffs_shadow_fixer *fixer;
14821 + struct yaffs_obj *obj;
14823 + while (shadow_fixers) {
14824 + fixer = shadow_fixers;
14825 + shadow_fixers = fixer->next;
14826 + /* Complete the rename transaction by deleting the
14827 + * shadowed object then setting the object header
14828 + to unshadowed.
14829 + */
14830 + obj = yaffs_find_by_number(dev, fixer->shadowed_id);
14831 + if (obj)
14832 + yaffs_del_obj(obj);
14834 + obj = yaffs_find_by_number(dev, fixer->obj_id);
14836 + if (obj)
14837 + yaffs_update_oh(obj, NULL, 1, 0, 0, NULL);
14839 + kfree(fixer);
14843 + yaffs_release_temp_buffer(dev, chunk_data);
14845 + if (alloc_failed)
14846 + return YAFFS_FAIL;
14848 + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs1_scan ends");
14850 + return YAFFS_OK;
14852 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs1.h linux-3.12.20/fs/yaffs2/yaffs_yaffs1.h
14853 --- linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs1.h 1970-01-01 01:00:00.000000000 +0100
14854 +++ linux-3.12.20/fs/yaffs2/yaffs_yaffs1.h 2014-05-17 11:28:49.000000000 +0200
14855 @@ -0,0 +1,22 @@
14857 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14859 + * Copyright (C) 2002-2011 Aleph One Ltd.
14860 + * for Toby Churchill Ltd and Brightstar Engineering
14862 + * Created by Charles Manning <charles@aleph1.co.uk>
14864 + * This program is free software; you can redistribute it and/or modify
14865 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14866 + * published by the Free Software Foundation.
14868 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14869 + */
14871 +#ifndef __YAFFS_YAFFS1_H__
14872 +#define __YAFFS_YAFFS1_H__
14874 +#include "yaffs_guts.h"
14875 +int yaffs1_scan(struct yaffs_dev *dev);
14877 +#endif
14878 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs2.c linux-3.12.20/fs/yaffs2/yaffs_yaffs2.c
14879 --- linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs2.c 1970-01-01 01:00:00.000000000 +0100
14880 +++ linux-3.12.20/fs/yaffs2/yaffs_yaffs2.c 2014-05-17 11:28:49.000000000 +0200
14881 @@ -0,0 +1,1534 @@
14883 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14885 + * Copyright (C) 2002-2011 Aleph One Ltd.
14886 + * for Toby Churchill Ltd and Brightstar Engineering
14888 + * Created by Charles Manning <charles@aleph1.co.uk>
14890 + * This program is free software; you can redistribute it and/or modify
14891 + * it under the terms of the GNU General Public License version 2 as
14892 + * published by the Free Software Foundation.
14893 + */
14895 +#include "yaffs_guts.h"
14896 +#include "yaffs_trace.h"
14897 +#include "yaffs_yaffs2.h"
14898 +#include "yaffs_checkptrw.h"
14899 +#include "yaffs_bitmap.h"
14900 +#include "yaffs_nand.h"
14901 +#include "yaffs_getblockinfo.h"
14902 +#include "yaffs_verify.h"
14903 +#include "yaffs_attribs.h"
14904 +#include "yaffs_summary.h"
14907 + * Checkpoints are really no benefit on very small partitions.
14909 + * To save space on small partitions don't bother with checkpoints unless
14910 + * the partition is at least this big.
14911 + */
14912 +#define YAFFS_CHECKPOINT_MIN_BLOCKS 60
14913 +#define YAFFS_SMALL_HOLE_THRESHOLD 4
14916 + * Oldest Dirty Sequence Number handling.
14917 + */
14919 +/* yaffs_calc_oldest_dirty_seq()
14920 + * yaffs2_find_oldest_dirty_seq()
14921 + * Calculate the oldest dirty sequence number if we don't know it.
14922 + */
14923 +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
14925 + int i;
14926 + unsigned seq;
14927 + unsigned block_no = 0;
14928 + struct yaffs_block_info *b;
14930 + if (!dev->param.is_yaffs2)
14931 + return;
14933 + /* Find the oldest dirty sequence number. */
14934 + seq = dev->seq_number + 1;
14935 + b = dev->block_info;
14936 + for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
14937 + if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
14938 + (b->pages_in_use - b->soft_del_pages) <
14939 + dev->param.chunks_per_block &&
14940 + b->seq_number < seq) {
14941 + seq = b->seq_number;
14942 + block_no = i;
14944 + b++;
14947 + if (block_no) {
14948 + dev->oldest_dirty_seq = seq;
14949 + dev->oldest_dirty_block = block_no;
14953 +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
14955 + if (!dev->param.is_yaffs2)
14956 + return;
14958 + if (!dev->oldest_dirty_seq)
14959 + yaffs_calc_oldest_dirty_seq(dev);
14963 + * yaffs_clear_oldest_dirty_seq()
14964 + * Called when a block is erased or marked bad. (ie. when its seq_number
14965 + * becomes invalid). If the value matches the oldest then we clear
14966 + * dev->oldest_dirty_seq to force its recomputation.
14967 + */
14968 +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
14969 + struct yaffs_block_info *bi)
14972 + if (!dev->param.is_yaffs2)
14973 + return;
14975 + if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
14976 + dev->oldest_dirty_seq = 0;
14977 + dev->oldest_dirty_block = 0;
14982 + * yaffs2_update_oldest_dirty_seq()
14983 + * Update the oldest dirty sequence number whenever we dirty a block.
14984 + * Only do this if the oldest_dirty_seq is actually being tracked.
14985 + */
14986 +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
14987 + struct yaffs_block_info *bi)
14989 + if (!dev->param.is_yaffs2)
14990 + return;
14992 + if (dev->oldest_dirty_seq) {
14993 + if (dev->oldest_dirty_seq > bi->seq_number) {
14994 + dev->oldest_dirty_seq = bi->seq_number;
14995 + dev->oldest_dirty_block = block_no;
15000 +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
15003 + if (!dev->param.is_yaffs2)
15004 + return 1; /* disqualification only applies to yaffs2. */
15006 + if (!bi->has_shrink_hdr)
15007 + return 1; /* can gc */
15009 + yaffs2_find_oldest_dirty_seq(dev);
15011 + /* Can't do gc of this block if there are any blocks older than this
15012 + * one that have discarded pages.
15013 + */
15014 + return (bi->seq_number <= dev->oldest_dirty_seq);
15018 + * yaffs2_find_refresh_block()
15019 + * periodically finds the oldest full block by sequence number for refreshing.
15020 + * Only for yaffs2.
15021 + */
15022 +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
15024 + u32 b;
15025 + u32 oldest = 0;
15026 + u32 oldest_seq = 0;
15027 + struct yaffs_block_info *bi;
15029 + if (!dev->param.is_yaffs2)
15030 + return oldest;
15032 + /*
15033 + * If refresh period < 10 then refreshing is disabled.
15034 + */
15035 + if (dev->param.refresh_period < 10)
15036 + return oldest;
15038 + /*
15039 + * Fix broken values.
15040 + */
15041 + if (dev->refresh_skip > dev->param.refresh_period)
15042 + dev->refresh_skip = dev->param.refresh_period;
15044 + if (dev->refresh_skip > 0)
15045 + return oldest;
15047 + /*
15048 + * Refresh skip is now zero.
15049 + * We'll do a refresh this time around....
15050 + * Update the refresh skip and find the oldest block.
15051 + */
15052 + dev->refresh_skip = dev->param.refresh_period;
15053 + dev->refresh_count++;
15054 + bi = dev->block_info;
15055 + for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
15057 + if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
15059 + if (oldest < 1 || bi->seq_number < oldest_seq) {
15060 + oldest = b;
15061 + oldest_seq = bi->seq_number;
15064 + bi++;
15067 + if (oldest > 0) {
15068 + yaffs_trace(YAFFS_TRACE_GC,
15069 + "GC refresh count %d selected block %d with seq_number %d",
15070 + dev->refresh_count, oldest, oldest_seq);
15073 + return oldest;
15076 +int yaffs2_checkpt_required(struct yaffs_dev *dev)
15078 + int nblocks;
15080 + if (!dev->param.is_yaffs2)
15081 + return 0;
15083 + nblocks = dev->internal_end_block - dev->internal_start_block + 1;
15085 + return !dev->param.skip_checkpt_wr &&
15086 + !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
15089 +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
15091 + int retval;
15092 + int n_bytes = 0;
15093 + int n_blocks;
15094 + int dev_blocks;
15096 + if (!dev->param.is_yaffs2)
15097 + return 0;
15099 + if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
15100 + /* Not a valid value so recalculate */
15101 + dev_blocks = dev->param.end_block - dev->param.start_block + 1;
15102 + n_bytes += sizeof(struct yaffs_checkpt_validity);
15103 + n_bytes += sizeof(struct yaffs_checkpt_dev);
15104 + n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
15105 + n_bytes += dev_blocks * dev->chunk_bit_stride;
15106 + n_bytes +=
15107 + (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
15108 + dev->n_obj;
15109 + n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
15110 + n_bytes += sizeof(struct yaffs_checkpt_validity);
15111 + n_bytes += sizeof(u32); /* checksum */
15113 + /* Round up and add 2 blocks to allow for some bad blocks,
15114 + * so add 3 */
15116 + n_blocks =
15117 + (n_bytes /
15118 + (dev->data_bytes_per_chunk *
15119 + dev->param.chunks_per_block)) + 3;
15121 + dev->checkpoint_blocks_required = n_blocks;
15124 + retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
15125 + if (retval < 0)
15126 + retval = 0;
15127 + return retval;
15130 +/*--------------------- Checkpointing --------------------*/
15132 +static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
15134 + struct yaffs_checkpt_validity cp;
15136 + memset(&cp, 0, sizeof(cp));
15138 + cp.struct_type = sizeof(cp);
15139 + cp.magic = YAFFS_MAGIC;
15140 + cp.version = YAFFS_CHECKPOINT_VERSION;
15141 + cp.head = (head) ? 1 : 0;
15143 + return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
15146 +static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
15148 + struct yaffs_checkpt_validity cp;
15149 + int ok;
15151 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
15153 + if (ok)
15154 + ok = (cp.struct_type == sizeof(cp)) &&
15155 + (cp.magic == YAFFS_MAGIC) &&
15156 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
15157 + (cp.head == ((head) ? 1 : 0));
15158 + return ok ? 1 : 0;
15161 +static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
15162 + struct yaffs_dev *dev)
15164 + cp->n_erased_blocks = dev->n_erased_blocks;
15165 + cp->alloc_block = dev->alloc_block;
15166 + cp->alloc_page = dev->alloc_page;
15167 + cp->n_free_chunks = dev->n_free_chunks;
15169 + cp->n_deleted_files = dev->n_deleted_files;
15170 + cp->n_unlinked_files = dev->n_unlinked_files;
15171 + cp->n_bg_deletions = dev->n_bg_deletions;
15172 + cp->seq_number = dev->seq_number;
15176 +static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
15177 + struct yaffs_checkpt_dev *cp)
15179 + dev->n_erased_blocks = cp->n_erased_blocks;
15180 + dev->alloc_block = cp->alloc_block;
15181 + dev->alloc_page = cp->alloc_page;
15182 + dev->n_free_chunks = cp->n_free_chunks;
15184 + dev->n_deleted_files = cp->n_deleted_files;
15185 + dev->n_unlinked_files = cp->n_unlinked_files;
15186 + dev->n_bg_deletions = cp->n_bg_deletions;
15187 + dev->seq_number = cp->seq_number;
15190 +static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
15192 + struct yaffs_checkpt_dev cp;
15193 + u32 n_bytes;
15194 + u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
15195 + int ok;
15197 + /* Write device runtime values */
15198 + yaffs2_dev_to_checkpt_dev(&cp, dev);
15199 + cp.struct_type = sizeof(cp);
15201 + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
15202 + if (!ok)
15203 + return 0;
15205 + /* Write block info */
15206 + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
15207 + ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
15208 + if (!ok)
15209 + return 0;
15211 + /* Write chunk bits */
15212 + n_bytes = n_blocks * dev->chunk_bit_stride;
15213 + ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
15215 + return ok ? 1 : 0;
15218 +static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
15220 + struct yaffs_checkpt_dev cp;
15221 + u32 n_bytes;
15222 + u32 n_blocks =
15223 + (dev->internal_end_block - dev->internal_start_block + 1);
15224 + int ok;
15226 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
15227 + if (!ok)
15228 + return 0;
15230 + if (cp.struct_type != sizeof(cp))
15231 + return 0;
15233 + yaffs_checkpt_dev_to_dev(dev, &cp);
15235 + n_bytes = n_blocks * sizeof(struct yaffs_block_info);
15237 + ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
15239 + if (!ok)
15240 + return 0;
15242 + n_bytes = n_blocks * dev->chunk_bit_stride;
15244 + ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
15246 + return ok ? 1 : 0;
15249 +static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
15250 + struct yaffs_obj *obj)
15252 + cp->obj_id = obj->obj_id;
15253 + cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
15254 + cp->hdr_chunk = obj->hdr_chunk;
15255 + cp->variant_type = obj->variant_type;
15256 + cp->deleted = obj->deleted;
15257 + cp->soft_del = obj->soft_del;
15258 + cp->unlinked = obj->unlinked;
15259 + cp->fake = obj->fake;
15260 + cp->rename_allowed = obj->rename_allowed;
15261 + cp->unlink_allowed = obj->unlink_allowed;
15262 + cp->serial = obj->serial;
15263 + cp->n_data_chunks = obj->n_data_chunks;
15265 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
15266 + cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
15267 + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
15268 + cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
15271 +static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
15272 + struct yaffs_checkpt_obj *cp)
15274 + struct yaffs_obj *parent;
15276 + if (obj->variant_type != cp->variant_type) {
15277 + yaffs_trace(YAFFS_TRACE_ERROR,
15278 + "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
15279 + cp->obj_id, cp->variant_type, cp->hdr_chunk,
15280 + obj->variant_type);
15281 + return 0;
15284 + obj->obj_id = cp->obj_id;
15286 + if (cp->parent_id)
15287 + parent = yaffs_find_or_create_by_number(obj->my_dev,
15288 + cp->parent_id,
15289 + YAFFS_OBJECT_TYPE_DIRECTORY);
15290 + else
15291 + parent = NULL;
15293 + if (parent) {
15294 + if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
15295 + yaffs_trace(YAFFS_TRACE_ALWAYS,
15296 + "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
15297 + cp->obj_id, cp->parent_id,
15298 + cp->variant_type, cp->hdr_chunk,
15299 + parent->variant_type);
15300 + return 0;
15302 + yaffs_add_obj_to_dir(parent, obj);
15305 + obj->hdr_chunk = cp->hdr_chunk;
15306 + obj->variant_type = cp->variant_type;
15307 + obj->deleted = cp->deleted;
15308 + obj->soft_del = cp->soft_del;
15309 + obj->unlinked = cp->unlinked;
15310 + obj->fake = cp->fake;
15311 + obj->rename_allowed = cp->rename_allowed;
15312 + obj->unlink_allowed = cp->unlink_allowed;
15313 + obj->serial = cp->serial;
15314 + obj->n_data_chunks = cp->n_data_chunks;
15316 + if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
15317 + obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
15318 + else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
15319 + obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
15321 + if (obj->hdr_chunk > 0)
15322 + obj->lazy_loaded = 1;
15323 + return 1;
15326 +static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
15327 + struct yaffs_tnode *tn, u32 level,
15328 + int chunk_offset)
15330 + int i;
15331 + struct yaffs_dev *dev = in->my_dev;
15332 + int ok = 1;
15333 + u32 base_offset;
15335 + if (!tn)
15336 + return 1;
15338 + if (level > 0) {
15339 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
15340 + if (!tn->internal[i])
15341 + continue;
15342 + ok = yaffs2_checkpt_tnode_worker(in,
15343 + tn->internal[i],
15344 + level - 1,
15345 + (chunk_offset <<
15346 + YAFFS_TNODES_INTERNAL_BITS) + i);
15348 + return ok;
15351 + /* Level 0 tnode */
15352 + base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
15353 + ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
15354 + sizeof(base_offset));
15355 + if (ok)
15356 + ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
15357 + dev->tnode_size);
15359 + return ok;
15362 +static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
15364 + u32 end_marker = ~0;
15365 + int ok = 1;
15367 + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
15368 + return ok;
15370 + ok = yaffs2_checkpt_tnode_worker(obj,
15371 + obj->variant.file_variant.top,
15372 + obj->variant.file_variant.
15373 + top_level, 0);
15374 + if (ok)
15375 + ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
15376 + sizeof(end_marker)) == sizeof(end_marker));
15378 + return ok ? 1 : 0;
15381 +static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
15383 + u32 base_chunk;
15384 + int ok = 1;
15385 + struct yaffs_dev *dev = obj->my_dev;
15386 + struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
15387 + struct yaffs_tnode *tn;
15388 + int nread = 0;
15390 + ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
15391 + sizeof(base_chunk));
15393 + while (ok && (~base_chunk)) {
15394 + nread++;
15395 + /* Read level 0 tnode */
15397 + tn = yaffs_get_tnode(dev);
15398 + if (tn)
15399 + ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
15400 + dev->tnode_size);
15401 + else
15402 + ok = 0;
15404 + if (tn && ok)
15405 + ok = yaffs_add_find_tnode_0(dev,
15406 + file_stuct_ptr,
15407 + base_chunk, tn) ? 1 : 0;
15409 + if (ok)
15410 + ok = (yaffs2_checkpt_rd
15411 + (dev, &base_chunk,
15412 + sizeof(base_chunk)) == sizeof(base_chunk));
15415 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15416 + "Checkpoint read tnodes %d records, last %d. ok %d",
15417 + nread, base_chunk, ok);
15419 + return ok ? 1 : 0;
15422 +static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
15424 + struct yaffs_obj *obj;
15425 + struct yaffs_checkpt_obj cp;
15426 + int i;
15427 + int ok = 1;
15428 + struct list_head *lh;
15430 + /* Iterate through the objects in each hash entry,
15431 + * dumping them to the checkpointing stream.
15432 + */
15434 + for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
15435 + list_for_each(lh, &dev->obj_bucket[i].list) {
15436 + obj = list_entry(lh, struct yaffs_obj, hash_link);
15437 + if (!obj->defered_free) {
15438 + yaffs2_obj_checkpt_obj(&cp, obj);
15439 + cp.struct_type = sizeof(cp);
15441 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15442 + "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
15443 + cp.obj_id, cp.parent_id,
15444 + cp.variant_type, cp.hdr_chunk, obj);
15446 + ok = (yaffs2_checkpt_wr(dev, &cp,
15447 + sizeof(cp)) == sizeof(cp));
15449 + if (ok &&
15450 + obj->variant_type ==
15451 + YAFFS_OBJECT_TYPE_FILE)
15452 + ok = yaffs2_wr_checkpt_tnodes(obj);
15457 + /* Dump end of list */
15458 + memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
15459 + cp.struct_type = sizeof(cp);
15461 + if (ok)
15462 + ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
15464 + return ok ? 1 : 0;
15467 +static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
15469 + struct yaffs_obj *obj;
15470 + struct yaffs_checkpt_obj cp;
15471 + int ok = 1;
15472 + int done = 0;
15473 + LIST_HEAD(hard_list);
15476 + while (ok && !done) {
15477 + ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
15478 + if (cp.struct_type != sizeof(cp)) {
15479 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15480 + "struct size %d instead of %d ok %d",
15481 + cp.struct_type, (int)sizeof(cp), ok);
15482 + ok = 0;
15485 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15486 + "Checkpoint read object %d parent %d type %d chunk %d ",
15487 + cp.obj_id, cp.parent_id, cp.variant_type,
15488 + cp.hdr_chunk);
15490 + if (ok && cp.obj_id == ~0) {
15491 + done = 1;
15492 + } else if (ok) {
15493 + obj =
15494 + yaffs_find_or_create_by_number(dev, cp.obj_id,
15495 + cp.variant_type);
15496 + if (obj) {
15497 + ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
15498 + if (!ok)
15499 + break;
15500 + if (obj->variant_type ==
15501 + YAFFS_OBJECT_TYPE_FILE) {
15502 + ok = yaffs2_rd_checkpt_tnodes(obj);
15503 + } else if (obj->variant_type ==
15504 + YAFFS_OBJECT_TYPE_HARDLINK) {
15505 + list_add(&obj->hard_links, &hard_list);
15507 + } else {
15508 + ok = 0;
15513 + if (ok)
15514 + yaffs_link_fixup(dev, &hard_list);
15516 + return ok ? 1 : 0;
15519 +static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
15521 + u32 checkpt_sum;
15522 + int ok;
15524 + yaffs2_get_checkpt_sum(dev, &checkpt_sum);
15526 + ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
15527 + sizeof(checkpt_sum));
15529 + if (!ok)
15530 + return 0;
15532 + return 1;
15535 +static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
15537 + u32 checkpt_sum0;
15538 + u32 checkpt_sum1;
15539 + int ok;
15541 + yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
15543 + ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
15544 + sizeof(checkpt_sum1));
15546 + if (!ok)
15547 + return 0;
15549 + if (checkpt_sum0 != checkpt_sum1)
15550 + return 0;
15552 + return 1;
15555 +static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
15557 + int ok = 1;
15559 + if (!yaffs2_checkpt_required(dev)) {
15560 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15561 + "skipping checkpoint write");
15562 + ok = 0;
15565 + if (ok)
15566 + ok = yaffs2_checkpt_open(dev, 1);
15568 + if (ok) {
15569 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15570 + "write checkpoint validity");
15571 + ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
15573 + if (ok) {
15574 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15575 + "write checkpoint device");
15576 + ok = yaffs2_wr_checkpt_dev(dev);
15578 + if (ok) {
15579 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15580 + "write checkpoint objects");
15581 + ok = yaffs2_wr_checkpt_objs(dev);
15583 + if (ok) {
15584 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15585 + "write checkpoint validity");
15586 + ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
15589 + if (ok)
15590 + ok = yaffs2_wr_checkpt_sum(dev);
15592 + if (!yaffs_checkpt_close(dev))
15593 + ok = 0;
15595 + if (ok)
15596 + dev->is_checkpointed = 1;
15597 + else
15598 + dev->is_checkpointed = 0;
15600 + return dev->is_checkpointed;
15603 +static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
15605 + int ok = 1;
15607 + if (!dev->param.is_yaffs2)
15608 + ok = 0;
15610 + if (ok && dev->param.skip_checkpt_rd) {
15611 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15612 + "skipping checkpoint read");
15613 + ok = 0;
15616 + if (ok)
15617 + ok = yaffs2_checkpt_open(dev, 0); /* open for read */
15619 + if (ok) {
15620 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15621 + "read checkpoint validity");
15622 + ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
15624 + if (ok) {
15625 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15626 + "read checkpoint device");
15627 + ok = yaffs2_rd_checkpt_dev(dev);
15629 + if (ok) {
15630 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15631 + "read checkpoint objects");
15632 + ok = yaffs2_rd_checkpt_objs(dev);
15634 + if (ok) {
15635 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15636 + "read checkpoint validity");
15637 + ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
15640 + if (ok) {
15641 + ok = yaffs2_rd_checkpt_sum(dev);
15642 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15643 + "read checkpoint checksum %d", ok);
15646 + if (!yaffs_checkpt_close(dev))
15647 + ok = 0;
15649 + if (ok)
15650 + dev->is_checkpointed = 1;
15651 + else
15652 + dev->is_checkpointed = 0;
15654 + return ok ? 1 : 0;
15657 +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
15659 + if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
15660 + dev->is_checkpointed = 0;
15661 + yaffs2_checkpt_invalidate_stream(dev);
15663 + if (dev->param.sb_dirty_fn)
15664 + dev->param.sb_dirty_fn(dev);
15667 +int yaffs_checkpoint_save(struct yaffs_dev *dev)
15669 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15670 + "save entry: is_checkpointed %d",
15671 + dev->is_checkpointed);
15673 + yaffs_verify_objects(dev);
15674 + yaffs_verify_blocks(dev);
15675 + yaffs_verify_free_chunks(dev);
15677 + if (!dev->is_checkpointed) {
15678 + yaffs2_checkpt_invalidate(dev);
15679 + yaffs2_wr_checkpt_data(dev);
15682 + yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
15683 + "save exit: is_checkpointed %d",
15684 + dev->is_checkpointed);
15686 + return dev->is_checkpointed;
15689 +int yaffs2_checkpt_restore(struct yaffs_dev *dev)
15691 + int retval;
15693 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15694 + "restore entry: is_checkpointed %d",
15695 + dev->is_checkpointed);
15697 + retval = yaffs2_rd_checkpt_data(dev);
15699 + if (dev->is_checkpointed) {
15700 + yaffs_verify_objects(dev);
15701 + yaffs_verify_blocks(dev);
15702 + yaffs_verify_free_chunks(dev);
15705 + yaffs_trace(YAFFS_TRACE_CHECKPOINT,
15706 + "restore exit: is_checkpointed %d",
15707 + dev->is_checkpointed);
15709 + return retval;
15712 +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
15714 + /* if new_size > old_file_size.
15715 + * We're going to be writing a hole.
15716 + * If the hole is small then write zeros otherwise write a start
15717 + * of hole marker.
15718 + */
15719 + loff_t old_file_size;
15720 + loff_t increase;
15721 + int small_hole;
15722 + int result = YAFFS_OK;
15723 + struct yaffs_dev *dev = NULL;
15724 + u8 *local_buffer = NULL;
15725 + int small_increase_ok = 0;
15727 + if (!obj)
15728 + return YAFFS_FAIL;
15730 + if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
15731 + return YAFFS_FAIL;
15733 + dev = obj->my_dev;
15735 + /* Bail out if not yaffs2 mode */
15736 + if (!dev->param.is_yaffs2)
15737 + return YAFFS_OK;
15739 + old_file_size = obj->variant.file_variant.file_size;
15741 + if (new_size <= old_file_size)
15742 + return YAFFS_OK;
15744 + increase = new_size - old_file_size;
15746 + if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
15747 + yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
15748 + small_hole = 1;
15749 + else
15750 + small_hole = 0;
15752 + if (small_hole)
15753 + local_buffer = yaffs_get_temp_buffer(dev);
15755 + if (local_buffer) {
15756 + /* fill hole with zero bytes */
15757 + loff_t pos = old_file_size;
15758 + int this_write;
15759 + int written;
15760 + memset(local_buffer, 0, dev->data_bytes_per_chunk);
15761 + small_increase_ok = 1;
15763 + while (increase > 0 && small_increase_ok) {
15764 + this_write = increase;
15765 + if (this_write > dev->data_bytes_per_chunk)
15766 + this_write = dev->data_bytes_per_chunk;
15767 + written =
15768 + yaffs_do_file_wr(obj, local_buffer, pos, this_write,
15769 + 0);
15770 + if (written == this_write) {
15771 + pos += this_write;
15772 + increase -= this_write;
15773 + } else {
15774 + small_increase_ok = 0;
15778 + yaffs_release_temp_buffer(dev, local_buffer);
15780 + /* If out of space then reverse any chunks we've added */
15781 + if (!small_increase_ok)
15782 + yaffs_resize_file_down(obj, old_file_size);
15785 + if (!small_increase_ok &&
15786 + obj->parent &&
15787 + obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
15788 + obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
15789 + /* Write a hole start header with the old file size */
15790 + yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
15793 + return result;
15796 +struct yaffs_block_index {
15797 + int seq;
15798 + int block;
15801 +static int yaffs2_ybicmp(const void *a, const void *b)
15803 + int aseq = ((struct yaffs_block_index *)a)->seq;
15804 + int bseq = ((struct yaffs_block_index *)b)->seq;
15805 + int ablock = ((struct yaffs_block_index *)a)->block;
15806 + int bblock = ((struct yaffs_block_index *)b)->block;
15808 + if (aseq == bseq)
15809 + return ablock - bblock;
15811 + return aseq - bseq;
15814 +static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
15815 + struct yaffs_block_info *bi,
15816 + int blk, int chunk_in_block,
15817 + int *found_chunks,
15818 + u8 *chunk_data,
15819 + struct list_head *hard_list,
15820 + int summary_available)
15822 + struct yaffs_obj_hdr *oh;
15823 + struct yaffs_obj *in;
15824 + struct yaffs_obj *parent;
15825 + int equiv_id;
15826 + loff_t file_size;
15827 + int is_shrink;
15828 + int is_unlinked;
15829 + struct yaffs_ext_tags tags;
15830 + int result;
15831 + int alloc_failed = 0;
15832 + int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
15833 + struct yaffs_file_var *file_var;
15834 + struct yaffs_hardlink_var *hl_var;
15835 + struct yaffs_symlink_var *sl_var;
15837 + if (summary_available) {
15838 + result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
15839 + tags.seq_number = bi->seq_number;
15842 + if (!summary_available || tags.obj_id == 0) {
15843 + result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
15844 + dev->tags_used++;
15845 + } else {
15846 + dev->summary_used++;
15849 + /* Let's have a good look at this chunk... */
15851 + if (!tags.chunk_used) {
15852 + /* An unassigned chunk in the block.
15853 + * If there are used chunks after this one, then
15854 + * it is a chunk that was skipped due to failing
15855 + * the erased check. Just skip it so that it can
15856 + * be deleted.
15857 + * But, more typically, We get here when this is
15858 + * an unallocated chunk and his means that
15859 + * either the block is empty or this is the one
15860 + * being allocated from
15861 + */
15863 + if (*found_chunks) {
15864 + /* This is a chunk that was skipped due
15865 + * to failing the erased check */
15866 + } else if (chunk_in_block == 0) {
15867 + /* We're looking at the first chunk in
15868 + * the block so the block is unused */
15869 + bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
15870 + dev->n_erased_blocks++;
15871 + } else {
15872 + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
15873 + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
15874 + if (dev->seq_number == bi->seq_number) {
15875 + /* Allocating from this block*/
15876 + yaffs_trace(YAFFS_TRACE_SCAN,
15877 + " Allocating from %d %d",
15878 + blk, chunk_in_block);
15880 + bi->block_state =
15881 + YAFFS_BLOCK_STATE_ALLOCATING;
15882 + dev->alloc_block = blk;
15883 + dev->alloc_page = chunk_in_block;
15884 + dev->alloc_block_finder = blk;
15885 + } else {
15886 + /* This is a partially written block
15887 + * that is not the current
15888 + * allocation block.
15889 + */
15890 + yaffs_trace(YAFFS_TRACE_SCAN,
15891 + "Partially written block %d detected. gc will fix this.",
15892 + blk);
15897 + dev->n_free_chunks++;
15899 + } else if (tags.ecc_result ==
15900 + YAFFS_ECC_RESULT_UNFIXED) {
15901 + yaffs_trace(YAFFS_TRACE_SCAN,
15902 + " Unfixed ECC in chunk(%d:%d), chunk ignored",
15903 + blk, chunk_in_block);
15904 + dev->n_free_chunks++;
15905 + } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
15906 + tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
15907 + tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
15908 + (tags.chunk_id > 0 &&
15909 + tags.n_bytes > dev->data_bytes_per_chunk) ||
15910 + tags.seq_number != bi->seq_number) {
15911 + yaffs_trace(YAFFS_TRACE_SCAN,
15912 + "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
15913 + blk, chunk_in_block, tags.obj_id,
15914 + tags.chunk_id, tags.n_bytes);
15915 + dev->n_free_chunks++;
15916 + } else if (tags.chunk_id > 0) {
15917 + /* chunk_id > 0 so it is a data chunk... */
15918 + loff_t endpos;
15919 + loff_t chunk_base = (tags.chunk_id - 1) *
15920 + dev->data_bytes_per_chunk;
15922 + *found_chunks = 1;
15924 + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
15925 + bi->pages_in_use++;
15927 + in = yaffs_find_or_create_by_number(dev,
15928 + tags.obj_id,
15929 + YAFFS_OBJECT_TYPE_FILE);
15930 + if (!in)
15931 + /* Out of memory */
15932 + alloc_failed = 1;
15934 + if (in &&
15935 + in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
15936 + chunk_base < in->variant.file_variant.shrink_size) {
15937 + /* This has not been invalidated by
15938 + * a resize */
15939 + if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
15940 + chunk, -1))
15941 + alloc_failed = 1;
15943 + /* File size is calculated by looking at
15944 + * the data chunks if we have not
15945 + * seen an object header yet.
15946 + * Stop this practice once we find an
15947 + * object header.
15948 + */
15949 + endpos = chunk_base + tags.n_bytes;
15951 + if (!in->valid &&
15952 + in->variant.file_variant.scanned_size < endpos) {
15953 + in->variant.file_variant.
15954 + scanned_size = endpos;
15955 + in->variant.file_variant.
15956 + file_size = endpos;
15958 + } else if (in) {
15959 + /* This chunk has been invalidated by a
15960 + * resize, or a past file deletion
15961 + * so delete the chunk*/
15962 + yaffs_chunk_del(dev, chunk, 1, __LINE__);
15964 + } else {
15965 + /* chunk_id == 0, so it is an ObjectHeader.
15966 + * Thus, we read in the object header and make
15967 + * the object
15968 + */
15969 + *found_chunks = 1;
15971 + yaffs_set_chunk_bit(dev, blk, chunk_in_block);
15972 + bi->pages_in_use++;
15974 + oh = NULL;
15975 + in = NULL;
15977 + if (tags.extra_available) {
15978 + in = yaffs_find_or_create_by_number(dev,
15979 + tags.obj_id,
15980 + tags.extra_obj_type);
15981 + if (!in)
15982 + alloc_failed = 1;
15985 + if (!in ||
15986 + (!in->valid && dev->param.disable_lazy_load) ||
15987 + tags.extra_shadows ||
15988 + (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
15989 + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
15991 + /* If we don't have valid info then we
15992 + * need to read the chunk
15993 + * TODO In future we can probably defer
15994 + * reading the chunk and living with
15995 + * invalid data until needed.
15996 + */
15998 + result = yaffs_rd_chunk_tags_nand(dev,
15999 + chunk,
16000 + chunk_data,
16001 + NULL);
16003 + oh = (struct yaffs_obj_hdr *)chunk_data;
16005 + if (dev->param.inband_tags) {
16006 + /* Fix up the header if they got
16007 + * corrupted by inband tags */
16008 + oh->shadows_obj =
16009 + oh->inband_shadowed_obj_id;
16010 + oh->is_shrink =
16011 + oh->inband_is_shrink;
16014 + if (!in) {
16015 + in = yaffs_find_or_create_by_number(dev,
16016 + tags.obj_id, oh->type);
16017 + if (!in)
16018 + alloc_failed = 1;
16022 + if (!in) {
16023 + /* TODO Hoosterman we have a problem! */
16024 + yaffs_trace(YAFFS_TRACE_ERROR,
16025 + "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
16026 + tags.obj_id, chunk);
16027 + return YAFFS_FAIL;
16030 + if (in->valid) {
16031 + /* We have already filled this one.
16032 + * We have a duplicate that will be
16033 + * discarded, but we first have to suck
16034 + * out resize info if it is a file.
16035 + */
16036 + if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
16037 + ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
16038 + (tags.extra_available &&
16039 + tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
16040 + )) {
16041 + loff_t this_size = (oh) ?
16042 + yaffs_oh_to_size(oh) :
16043 + tags.extra_file_size;
16044 + u32 parent_obj_id = (oh) ?
16045 + oh->parent_obj_id :
16046 + tags.extra_parent_id;
16048 + is_shrink = (oh) ?
16049 + oh->is_shrink :
16050 + tags.extra_is_shrink;
16052 + /* If it is deleted (unlinked
16053 + * at start also means deleted)
16054 + * we treat the file size as
16055 + * being zeroed at this point.
16056 + */
16057 + if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
16058 + parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
16059 + this_size = 0;
16060 + is_shrink = 1;
16063 + if (is_shrink &&
16064 + in->variant.file_variant.shrink_size >
16065 + this_size)
16066 + in->variant.file_variant.shrink_size =
16067 + this_size;
16069 + if (is_shrink)
16070 + bi->has_shrink_hdr = 1;
16072 + /* Use existing - destroy this one. */
16073 + yaffs_chunk_del(dev, chunk, 1, __LINE__);
16076 + if (!in->valid && in->variant_type !=
16077 + (oh ? oh->type : tags.extra_obj_type)) {
16078 + yaffs_trace(YAFFS_TRACE_ERROR,
16079 + "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
16080 + oh ? oh->type : tags.extra_obj_type,
16081 + in->variant_type, tags.obj_id,
16082 + chunk);
16083 + in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
16086 + if (!in->valid &&
16087 + (tags.obj_id == YAFFS_OBJECTID_ROOT ||
16088 + tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
16089 + /* We only load some info, don't fiddle
16090 + * with directory structure */
16091 + in->valid = 1;
16093 + if (oh) {
16094 + in->yst_mode = oh->yst_mode;
16095 + yaffs_load_attribs(in, oh);
16096 + in->lazy_loaded = 0;
16097 + } else {
16098 + in->lazy_loaded = 1;
16100 + in->hdr_chunk = chunk;
16102 + } else if (!in->valid) {
16103 + /* we need to load this info */
16104 + in->valid = 1;
16105 + in->hdr_chunk = chunk;
16106 + if (oh) {
16107 + in->variant_type = oh->type;
16108 + in->yst_mode = oh->yst_mode;
16109 + yaffs_load_attribs(in, oh);
16111 + if (oh->shadows_obj > 0)
16112 + yaffs_handle_shadowed_obj(dev,
16113 + oh->shadows_obj, 1);
16115 + yaffs_set_obj_name_from_oh(in, oh);
16116 + parent = yaffs_find_or_create_by_number(dev,
16117 + oh->parent_obj_id,
16118 + YAFFS_OBJECT_TYPE_DIRECTORY);
16119 + file_size = yaffs_oh_to_size(oh);
16120 + is_shrink = oh->is_shrink;
16121 + equiv_id = oh->equiv_id;
16122 + } else {
16123 + in->variant_type = tags.extra_obj_type;
16124 + parent = yaffs_find_or_create_by_number(dev,
16125 + tags.extra_parent_id,
16126 + YAFFS_OBJECT_TYPE_DIRECTORY);
16127 + file_size = tags.extra_file_size;
16128 + is_shrink = tags.extra_is_shrink;
16129 + equiv_id = tags.extra_equiv_id;
16130 + in->lazy_loaded = 1;
16132 + in->dirty = 0;
16134 + if (!parent)
16135 + alloc_failed = 1;
16137 + /* directory stuff...
16138 + * hook up to parent
16139 + */
16141 + if (parent &&
16142 + parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
16143 + /* Set up as a directory */
16144 + parent->variant_type =
16145 + YAFFS_OBJECT_TYPE_DIRECTORY;
16146 + INIT_LIST_HEAD(&parent->
16147 + variant.dir_variant.children);
16148 + } else if (!parent ||
16149 + parent->variant_type !=
16150 + YAFFS_OBJECT_TYPE_DIRECTORY) {
16151 + /* Hoosterman, another problem....
16152 + * Trying to use a non-directory as a directory
16153 + */
16155 + yaffs_trace(YAFFS_TRACE_ERROR,
16156 + "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
16157 + );
16158 + parent = dev->lost_n_found;
16160 + yaffs_add_obj_to_dir(parent, in);
16162 + is_unlinked = (parent == dev->del_dir) ||
16163 + (parent == dev->unlinked_dir);
16165 + if (is_shrink)
16166 + /* Mark the block */
16167 + bi->has_shrink_hdr = 1;
16169 + /* Note re hardlinks.
16170 + * Since we might scan a hardlink before its equivalent
16171 + * object is scanned we put them all in a list.
16172 + * After scanning is complete, we should have all the
16173 + * objects, so we run through this list and fix up all
16174 + * the chains.
16175 + */
16177 + switch (in->variant_type) {
16178 + case YAFFS_OBJECT_TYPE_UNKNOWN:
16179 + /* Todo got a problem */
16180 + break;
16181 + case YAFFS_OBJECT_TYPE_FILE:
16182 + file_var = &in->variant.file_variant;
16183 + if (file_var->scanned_size < file_size) {
16184 + /* This covers the case where the file
16185 + * size is greater than the data held.
16186 + * This will happen if the file is
16187 + * resized to be larger than its
16188 + * current data extents.
16189 + */
16190 + file_var->file_size = file_size;
16191 + file_var->scanned_size = file_size;
16194 + if (file_var->shrink_size > file_size)
16195 + file_var->shrink_size = file_size;
16197 + break;
16198 + case YAFFS_OBJECT_TYPE_HARDLINK:
16199 + hl_var = &in->variant.hardlink_variant;
16200 + if (!is_unlinked) {
16201 + hl_var->equiv_id = equiv_id;
16202 + list_add(&in->hard_links, hard_list);
16204 + break;
16205 + case YAFFS_OBJECT_TYPE_DIRECTORY:
16206 + /* Do nothing */
16207 + break;
16208 + case YAFFS_OBJECT_TYPE_SPECIAL:
16209 + /* Do nothing */
16210 + break;
16211 + case YAFFS_OBJECT_TYPE_SYMLINK:
16212 + sl_var = &in->variant.symlink_variant;
16213 + if (oh) {
16214 + sl_var->alias =
16215 + yaffs_clone_str(oh->alias);
16216 + if (!sl_var->alias)
16217 + alloc_failed = 1;
16219 + break;
16223 + return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
16226 +int yaffs2_scan_backwards(struct yaffs_dev *dev)
16228 + int blk;
16229 + int block_iter;
16230 + int start_iter;
16231 + int end_iter;
16232 + int n_to_scan = 0;
16233 + enum yaffs_block_state state;
16234 + int c;
16235 + int deleted;
16236 + LIST_HEAD(hard_list);
16237 + struct yaffs_block_info *bi;
16238 + u32 seq_number;
16239 + int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
16240 + u8 *chunk_data;
16241 + int found_chunks;
16242 + int alloc_failed = 0;
16243 + struct yaffs_block_index *block_index = NULL;
16244 + int alt_block_index = 0;
16245 + int summary_available;
16247 + yaffs_trace(YAFFS_TRACE_SCAN,
16248 + "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
16249 + dev->internal_start_block, dev->internal_end_block);
16251 + dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
16253 + block_index =
16254 + kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
16256 + if (!block_index) {
16257 + block_index =
16258 + vmalloc(n_blocks * sizeof(struct yaffs_block_index));
16259 + alt_block_index = 1;
16262 + if (!block_index) {
16263 + yaffs_trace(YAFFS_TRACE_SCAN,
16264 + "yaffs2_scan_backwards() could not allocate block index!"
16265 + );
16266 + return YAFFS_FAIL;
16269 + dev->blocks_in_checkpt = 0;
16271 + chunk_data = yaffs_get_temp_buffer(dev);
16273 + /* Scan all the blocks to determine their state */
16274 + bi = dev->block_info;
16275 + for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
16276 + blk++) {
16277 + yaffs_clear_chunk_bits(dev, blk);
16278 + bi->pages_in_use = 0;
16279 + bi->soft_del_pages = 0;
16281 + yaffs_query_init_block_state(dev, blk, &state, &seq_number);
16283 + bi->block_state = state;
16284 + bi->seq_number = seq_number;
16286 + if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
16287 + bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
16288 + if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
16289 + bi->block_state = YAFFS_BLOCK_STATE_DEAD;
16291 + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
16292 + "Block scanning block %d state %d seq %d",
16293 + blk, bi->block_state, seq_number);
16295 + if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
16296 + dev->blocks_in_checkpt++;
16298 + } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
16299 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
16300 + "block %d is bad", blk);
16301 + } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
16302 + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
16303 + dev->n_erased_blocks++;
16304 + dev->n_free_chunks += dev->param.chunks_per_block;
16305 + } else if (bi->block_state ==
16306 + YAFFS_BLOCK_STATE_NEEDS_SCAN) {
16307 + /* Determine the highest sequence number */
16308 + if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
16309 + seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
16310 + block_index[n_to_scan].seq = seq_number;
16311 + block_index[n_to_scan].block = blk;
16312 + n_to_scan++;
16313 + if (seq_number >= dev->seq_number)
16314 + dev->seq_number = seq_number;
16315 + } else {
16316 + /* TODO: Nasty sequence number! */
16317 + yaffs_trace(YAFFS_TRACE_SCAN,
16318 + "Block scanning block %d has bad sequence number %d",
16319 + blk, seq_number);
16322 + bi++;
16325 + yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
16327 + cond_resched();
16329 + /* Sort the blocks by sequence number */
16330 + sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
16331 + yaffs2_ybicmp, NULL);
16333 + cond_resched();
16335 + yaffs_trace(YAFFS_TRACE_SCAN, "...done");
16337 + /* Now scan the blocks looking at the data. */
16338 + start_iter = 0;
16339 + end_iter = n_to_scan - 1;
16340 + yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
16342 + /* For each block.... backwards */
16343 + for (block_iter = end_iter;
16344 + !alloc_failed && block_iter >= start_iter;
16345 + block_iter--) {
16346 + /* Cooperative multitasking! This loop can run for so
16347 + long that watchdog timers expire. */
16348 + cond_resched();
16350 + /* get the block to scan in the correct order */
16351 + blk = block_index[block_iter].block;
16352 + bi = yaffs_get_block_info(dev, blk);
16353 + deleted = 0;
16355 + summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
16357 + /* For each chunk in each block that needs scanning.... */
16358 + found_chunks = 0;
16359 + if (summary_available)
16360 + c = dev->chunks_per_summary - 1;
16361 + else
16362 + c = dev->param.chunks_per_block - 1;
16364 + for (/* c is already initialised */;
16365 + !alloc_failed && c >= 0 &&
16366 + (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
16367 + bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
16368 + c--) {
16369 + /* Scan backwards...
16370 + * Read the tags and decide what to do
16371 + */
16372 + if (yaffs2_scan_chunk(dev, bi, blk, c,
16373 + &found_chunks, chunk_data,
16374 + &hard_list, summary_available) ==
16375 + YAFFS_FAIL)
16376 + alloc_failed = 1;
16379 + if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
16380 + /* If we got this far while scanning, then the block
16381 + * is fully allocated. */
16382 + bi->block_state = YAFFS_BLOCK_STATE_FULL;
16385 + /* Now let's see if it was dirty */
16386 + if (bi->pages_in_use == 0 &&
16387 + !bi->has_shrink_hdr &&
16388 + bi->block_state == YAFFS_BLOCK_STATE_FULL) {
16389 + yaffs_block_became_dirty(dev, blk);
16393 + yaffs_skip_rest_of_block(dev);
16395 + if (alt_block_index)
16396 + vfree(block_index);
16397 + else
16398 + kfree(block_index);
16400 + /* Ok, we've done all the scanning.
16401 + * Fix up the hard link chains.
16402 + * We have scanned all the objects, now it's time to add these
16403 + * hardlinks.
16404 + */
16405 + yaffs_link_fixup(dev, &hard_list);
16407 + yaffs_release_temp_buffer(dev, chunk_data);
16409 + if (alloc_failed)
16410 + return YAFFS_FAIL;
16412 + yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
16414 + return YAFFS_OK;
16416 diff -Nur linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs2.h linux-3.12.20/fs/yaffs2/yaffs_yaffs2.h
16417 --- linux-3.12.20.orig/fs/yaffs2/yaffs_yaffs2.h 1970-01-01 01:00:00.000000000 +0100
16418 +++ linux-3.12.20/fs/yaffs2/yaffs_yaffs2.h 2014-05-17 11:28:49.000000000 +0200
16419 @@ -0,0 +1,39 @@
16421 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
16423 + * Copyright (C) 2002-2011 Aleph One Ltd.
16424 + * for Toby Churchill Ltd and Brightstar Engineering
16426 + * Created by Charles Manning <charles@aleph1.co.uk>
16428 + * This program is free software; you can redistribute it and/or modify
16429 + * it under the terms of the GNU Lesser General Public License version 2.1 as
16430 + * published by the Free Software Foundation.
16432 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
16433 + */
16435 +#ifndef __YAFFS_YAFFS2_H__
16436 +#define __YAFFS_YAFFS2_H__
16438 +#include "yaffs_guts.h"
16440 +void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev);
16441 +void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev);
16442 +void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
16443 + struct yaffs_block_info *bi);
16444 +void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
16445 + struct yaffs_block_info *bi);
16446 +int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi);
16447 +u32 yaffs2_find_refresh_block(struct yaffs_dev *dev);
16448 +int yaffs2_checkpt_required(struct yaffs_dev *dev);
16449 +int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev);
16451 +void yaffs2_checkpt_invalidate(struct yaffs_dev *dev);
16452 +int yaffs2_checkpt_save(struct yaffs_dev *dev);
16453 +int yaffs2_checkpt_restore(struct yaffs_dev *dev);
16455 +int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size);
16456 +int yaffs2_scan_backwards(struct yaffs_dev *dev);
16458 +#endif
16459 diff -Nur linux-3.12.20.orig/fs/yaffs2/yportenv.h linux-3.12.20/fs/yaffs2/yportenv.h
16460 --- linux-3.12.20.orig/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
16461 +++ linux-3.12.20/fs/yaffs2/yportenv.h 2014-05-17 11:28:49.000000000 +0200
16462 @@ -0,0 +1,85 @@
16464 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
16466 + * Copyright (C) 2002-2011 Aleph One Ltd.
16467 + * for Toby Churchill Ltd and Brightstar Engineering
16469 + * Created by Charles Manning <charles@aleph1.co.uk>
16471 + * This program is free software; you can redistribute it and/or modify
16472 + * it under the terms of the GNU Lesser General Public License version 2.1 as
16473 + * published by the Free Software Foundation.
16475 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
16476 + */
16478 +#ifndef __YPORTENV_H__
16479 +#define __YPORTENV_H__
16482 + * Define the MTD version in terms of Linux Kernel versions
16483 + * This allows yaffs to be used independantly of the kernel
16484 + * as well as with it.
16485 + */
16487 +#define MTD_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + (c))
16489 +#ifdef YAFFS_OUT_OF_TREE
16490 +#include "moduleconfig.h"
16491 +#endif
16493 +#include <linux/version.h>
16494 +#define MTD_VERSION_CODE LINUX_VERSION_CODE
16496 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19))
16497 +#include <linux/config.h>
16498 +#endif
16499 +#include <linux/version.h>
16500 +#include <linux/kernel.h>
16501 +#include <linux/mm.h>
16502 +#include <linux/sched.h>
16503 +#include <linux/string.h>
16504 +#include <linux/slab.h>
16505 +#include <linux/vmalloc.h>
16506 +#include <linux/xattr.h>
16507 +#include <linux/list.h>
16508 +#include <linux/types.h>
16509 +#include <linux/fs.h>
16510 +#include <linux/stat.h>
16511 +#include <linux/sort.h>
16512 +#include <linux/bitops.h>
16514 +/* These type wrappings are used to support Unicode names in WinCE. */
16515 +#define YCHAR char
16516 +#define YUCHAR unsigned char
16517 +#define _Y(x) x
16519 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
16520 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
16523 +#define YAFFS_ROOT_MODE 0755
16524 +#define YAFFS_LOSTNFOUND_MODE 0700
16526 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 5, 0))
16527 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
16528 +#define Y_TIME_CONVERT(x) (x).tv_sec
16529 +#else
16530 +#define Y_CURRENT_TIME CURRENT_TIME
16531 +#define Y_TIME_CONVERT(x) (x)
16532 +#endif
16534 +#define compile_time_assertion(assertion) \
16535 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
16538 +#define yaffs_printf(msk, fmt, ...) \
16539 + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__)
16541 +#define yaffs_trace(msk, fmt, ...) do { \
16542 + if (yaffs_trace_mask & (msk)) \
16543 + printk(KERN_DEBUG "yaffs: " fmt "\n", ##__VA_ARGS__); \
16544 +} while (0)
16547 +#endif