Merge branch 'master' into gfs2
[pandora-kernel.git] / fs / jffs / intrep.c
1 /*
2  * JFFS -- Journaling Flash File System, Linux implementation.
3  *
4  * Copyright (C) 1999, 2000  Axis Communications, Inc.
5  *
6  * Created by Finn Hakansson <finn@axis.com>.
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * $Id: intrep.c,v 1.102 2001/09/23 23:28:36 dwmw2 Exp $
14  *
15  * Ported to Linux 2.3.x and MTD:
16  * Copyright (C) 2000  Alexander Larsson (alex@cendio.se), Cendio Systems AB
17  *
18  */
19
20 /* This file contains the code for the internal structure of the
21    Journaling Flash File System, JFFS.  */
22
23 /*
24  * Todo list:
25  *
26  * memcpy_to_flash() and memcpy_from_flash() functions.
27  *
28  * Implementation of hard links.
29  *
30  * Organize the source code in a better way. Against the VFS we could
31  * have jffs_ext.c, and against the block device jffs_int.c.
32  * A better file-internal organization too.
33  *
34  * A better checksum algorithm.
35  *
36  * Consider endianness stuff. ntohl() etc.
37  *
38  * Are we handling the atime, mtime, ctime members of the inode right?
39  *
40  * Remove some duplicated code. Take a look at jffs_write_node() and
41  * jffs_rewrite_data() for instance.
42  *
43  * Implement more meaning of the nlink member in various data structures.
44  * nlink could be used in conjunction with hard links for instance.
45  *
46  * Better memory management. Allocate data structures in larger chunks
47  * if possible.
48  *
49  * If too much meta data is stored, a garbage collect should be issued.
50  * We have experienced problems with too much meta data with for instance
51  * log files.
52  *
53  * Improve the calls to jffs_ioctl(). We would like to retrieve more
54  * information to be able to debug (or to supervise) JFFS during run-time.
55  *
56  */
57
58 #include <linux/types.h>
59 #include <linux/slab.h>
60 #include <linux/jffs.h>
61 #include <linux/fs.h>
62 #include <linux/stat.h>
63 #include <linux/pagemap.h>
64 #include <linux/mutex.h>
65 #include <asm/byteorder.h>
66 #include <linux/smp_lock.h>
67 #include <linux/time.h>
68 #include <linux/ctype.h>
69
70 #include "intrep.h"
71 #include "jffs_fm.h"
72
73 long no_jffs_node = 0;
74 static long no_jffs_file = 0;
75 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
76 long no_jffs_control = 0;
77 long no_jffs_raw_inode = 0;
78 long no_jffs_node_ref = 0;
79 long no_jffs_fm = 0;
80 long no_jffs_fmcontrol = 0;
81 long no_hash = 0;
82 long no_name = 0;
83 #endif
84
85 static int jffs_scan_flash(struct jffs_control *c);
86 static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
87 static int jffs_build_file(struct jffs_file *f);
88 static int jffs_free_file(struct jffs_file *f);
89 static int jffs_free_node_list(struct jffs_file *f);
90 static int jffs_garbage_collect_now(struct jffs_control *c);
91 static int jffs_insert_file_into_hash(struct jffs_file *f);
92 static int jffs_remove_redundant_nodes(struct jffs_file *f);
93
94 /* Is there enough space on the flash?  */
95 static inline int JFFS_ENOUGH_SPACE(struct jffs_control *c, __u32 space)
96 {
97         struct jffs_fmcontrol *fmc = c->fmc;
98
99         while (1) {
100                 if ((fmc->flash_size - (fmc->used_size + fmc->dirty_size))
101                         >= fmc->min_free_size + space) {
102                         return 1;
103                 }
104                 if (fmc->dirty_size < fmc->sector_size)
105                         return 0;
106
107                 if (jffs_garbage_collect_now(c)) {
108                   D1(printk("JFFS_ENOUGH_SPACE: jffs_garbage_collect_now() failed.\n"));
109                   return 0;
110                 }
111         }
112 }
113
114 #if CONFIG_JFFS_FS_VERBOSE > 0
115 static __u8
116 flash_read_u8(struct mtd_info *mtd, loff_t from)
117 {
118         size_t retlen;
119         __u8 ret;
120         int res;
121
122         res = MTD_READ(mtd, from, 1, &retlen, &ret);
123         if (retlen != 1) {
124                 printk("Didn't read a byte in flash_read_u8(). Returned %d\n", res);
125                 return 0;
126         }
127
128         return ret;
129 }
130
131 static void
132 jffs_hexdump(struct mtd_info *mtd, loff_t pos, int size)
133 {
134         char line[16];
135         int j = 0;
136
137         while (size > 0) {
138                 int i;
139
140                 printk("%ld:", (long) pos);
141                 for (j = 0; j < 16; j++) {
142                         line[j] = flash_read_u8(mtd, pos++);
143                 }
144                 for (i = 0; i < j; i++) {
145                         if (!(i & 1)) {
146                                 printk(" %.2x", line[i] & 0xff);
147                         }
148                         else {
149                                 printk("%.2x", line[i] & 0xff);
150                         }
151                 }
152
153                 /* Print empty space */
154                 for (; i < 16; i++) {
155                         if (!(i & 1)) {
156                                 printk("   ");
157                         }
158                         else {
159                                 printk("  ");
160                         }
161                 }
162                 printk("  ");
163
164                 for (i = 0; i < j; i++) {
165                         if (isgraph(line[i])) {
166                                 printk("%c", line[i]);
167                         }
168                         else {
169                                 printk(".");
170                         }
171                 }
172                 printk("\n");
173                 size -= 16;
174         }
175 }
176
177 /* Print the contents of a node.  */
178 static void
179 jffs_print_node(struct jffs_node *n)
180 {
181         D(printk("jffs_node: 0x%p\n", n));
182         D(printk("{\n"));
183         D(printk("        0x%08x, /* version  */\n", n->version));
184         D(printk("        0x%08x, /* data_offset  */\n", n->data_offset));
185         D(printk("        0x%08x, /* data_size  */\n", n->data_size));
186         D(printk("        0x%08x, /* removed_size  */\n", n->removed_size));
187         D(printk("        0x%08x, /* fm_offset  */\n", n->fm_offset));
188         D(printk("        0x%02x,       /* name_size  */\n", n->name_size));
189         D(printk("        0x%p, /* fm,  fm->offset: %u  */\n",
190                  n->fm, (n->fm ? n->fm->offset : 0)));
191         D(printk("        0x%p, /* version_prev  */\n", n->version_prev));
192         D(printk("        0x%p, /* version_next  */\n", n->version_next));
193         D(printk("        0x%p, /* range_prev  */\n", n->range_prev));
194         D(printk("        0x%p, /* range_next  */\n", n->range_next));
195         D(printk("}\n"));
196 }
197
198 #endif
199
200 /* Print the contents of a raw inode.  */
201 static void
202 jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
203 {
204         D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
205         D(printk("{\n"));
206         D(printk("        0x%08x, /* magic  */\n", raw_inode->magic));
207         D(printk("        0x%08x, /* ino  */\n", raw_inode->ino));
208         D(printk("        0x%08x, /* pino  */\n", raw_inode->pino));
209         D(printk("        0x%08x, /* version  */\n", raw_inode->version));
210         D(printk("        0x%08x, /* mode  */\n", raw_inode->mode));
211         D(printk("        0x%04x,     /* uid  */\n", raw_inode->uid));
212         D(printk("        0x%04x,     /* gid  */\n", raw_inode->gid));
213         D(printk("        0x%08x, /* atime  */\n", raw_inode->atime));
214         D(printk("        0x%08x, /* mtime  */\n", raw_inode->mtime));
215         D(printk("        0x%08x, /* ctime  */\n", raw_inode->ctime));
216         D(printk("        0x%08x, /* offset  */\n", raw_inode->offset));
217         D(printk("        0x%08x, /* dsize  */\n", raw_inode->dsize));
218         D(printk("        0x%08x, /* rsize  */\n", raw_inode->rsize));
219         D(printk("        0x%02x,       /* nsize  */\n", raw_inode->nsize));
220         D(printk("        0x%02x,       /* nlink  */\n", raw_inode->nlink));
221         D(printk("        0x%02x,       /* spare  */\n",
222                  raw_inode->spare));
223         D(printk("        %u,          /* rename  */\n",
224                  raw_inode->rename));
225         D(printk("        %u,          /* deleted  */\n",
226                  raw_inode->deleted));
227         D(printk("        0x%02x,       /* accurate  */\n",
228                  raw_inode->accurate));
229         D(printk("        0x%08x, /* dchksum  */\n", raw_inode->dchksum));
230         D(printk("        0x%04x,     /* nchksum  */\n", raw_inode->nchksum));
231         D(printk("        0x%04x,     /* chksum  */\n", raw_inode->chksum));
232         D(printk("}\n"));
233 }
234
235 #define flash_safe_acquire(arg)
236 #define flash_safe_release(arg)
237
238
239 static int
240 flash_safe_read(struct mtd_info *mtd, loff_t from,
241                 u_char *buf, size_t count)
242 {
243         size_t retlen;
244         int res;
245
246         D3(printk(KERN_NOTICE "flash_safe_read(%p, %08x, %p, %08x)\n",
247                   mtd, (unsigned int) from, buf, count));
248
249         res = mtd->read(mtd, from, count, &retlen, buf);
250         if (retlen != count) {
251                 panic("Didn't read all bytes in flash_safe_read(). Returned %d\n", res);
252         }
253         return res?res:retlen;
254 }
255
256
257 static __u32
258 flash_read_u32(struct mtd_info *mtd, loff_t from)
259 {
260         size_t retlen;
261         __u32 ret;
262         int res;
263
264         res = mtd->read(mtd, from, 4, &retlen, (unsigned char *)&ret);
265         if (retlen != 4) {
266                 printk("Didn't read all bytes in flash_read_u32(). Returned %d\n", res);
267                 return 0;
268         }
269
270         return ret;
271 }
272
273
274 static int
275 flash_safe_write(struct mtd_info *mtd, loff_t to,
276                  const u_char *buf, size_t count)
277 {
278         size_t retlen;
279         int res;
280
281         D3(printk(KERN_NOTICE "flash_safe_write(%p, %08x, %p, %08x)\n",
282                   mtd, (unsigned int) to, buf, count));
283
284         res = mtd->write(mtd, to, count, &retlen, buf);
285         if (retlen != count) {
286                 printk("Didn't write all bytes in flash_safe_write(). Returned %d\n", res);
287         }
288         return res?res:retlen;
289 }
290
291
292 static int
293 flash_safe_writev(struct mtd_info *mtd, const struct kvec *vecs,
294                         unsigned long iovec_cnt, loff_t to)
295 {
296         size_t retlen, retlen_a;
297         int i;
298         int res;
299
300         D3(printk(KERN_NOTICE "flash_safe_writev(%p, %08x, %p)\n",
301                   mtd, (unsigned int) to, vecs));
302
303         if (mtd->writev) {
304                 res = mtd->writev(mtd, vecs, iovec_cnt, to, &retlen);
305                 return res ? res : retlen;
306         }
307         /* Not implemented writev. Repeatedly use write - on the not so
308            unreasonable assumption that the mtd driver doesn't care how
309            many write cycles we use. */
310         res=0;
311         retlen=0;
312
313         for (i=0; !res && i<iovec_cnt; i++) {
314                 res = mtd->write(mtd, to, vecs[i].iov_len, &retlen_a,
315                                  vecs[i].iov_base);
316                 if (retlen_a != vecs[i].iov_len) {
317                         printk("Didn't write all bytes in flash_safe_writev(). Returned %d\n", res);
318                         if (i != iovec_cnt-1)
319                                 return -EIO;
320                 }
321                 /* If res is non-zero, retlen_a is undefined, but we don't
322                    care because in that case it's not going to be 
323                    returned anyway.
324                 */
325                 to += retlen_a;
326                 retlen += retlen_a;
327         }
328         return res?res:retlen;
329 }
330
331
332 static int
333 flash_memset(struct mtd_info *mtd, loff_t to,
334              const u_char c, size_t size)
335 {
336         static unsigned char pattern[64];
337         int i;
338
339         /* fill up pattern */
340
341         for(i = 0; i < 64; i++)
342                 pattern[i] = c;
343
344         /* write as many 64-byte chunks as we can */
345
346         while (size >= 64) {
347                 flash_safe_write(mtd, to, pattern, 64);
348                 size -= 64;
349                 to += 64;
350         }
351
352         /* and the rest */
353
354         if(size)
355                 flash_safe_write(mtd, to, pattern, size);
356
357         return size;
358 }
359
360
361 static void
362 intrep_erase_callback(struct erase_info *done)
363 {
364         wait_queue_head_t *wait_q;
365
366         wait_q = (wait_queue_head_t *)done->priv;
367
368         wake_up(wait_q);
369 }
370
371
372 static int
373 flash_erase_region(struct mtd_info *mtd, loff_t start,
374                    size_t size)
375 {
376         struct erase_info *erase;
377         DECLARE_WAITQUEUE(wait, current);
378         wait_queue_head_t wait_q;
379
380         erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
381         if (!erase)
382                 return -ENOMEM;
383
384         init_waitqueue_head(&wait_q);
385
386         erase->mtd = mtd;
387         erase->callback = intrep_erase_callback;
388         erase->addr = start;
389         erase->len = size;
390         erase->priv = (u_long)&wait_q;
391
392         /* FIXME: Use TASK_INTERRUPTIBLE and deal with being interrupted */
393         set_current_state(TASK_UNINTERRUPTIBLE);
394         add_wait_queue(&wait_q, &wait);
395
396         if (mtd->erase(mtd, erase) < 0) {
397                 set_current_state(TASK_RUNNING);
398                 remove_wait_queue(&wait_q, &wait);
399                 kfree(erase);
400
401                 printk(KERN_WARNING "flash: erase of region [0x%lx, 0x%lx] "
402                        "totally failed\n", (long)start, (long)start + size);
403
404                 return -1;
405         }
406
407         schedule(); /* Wait for flash to finish. */
408         remove_wait_queue(&wait_q, &wait);
409
410         kfree(erase);
411
412         return 0;
413 }
414
415 /* This routine calculates checksums in JFFS.  */
416 static __u32
417 jffs_checksum(const void *data, int size)
418 {
419         __u32 sum = 0;
420         __u8 *ptr = (__u8 *)data;
421         while (size-- > 0) {
422                 sum += *ptr++;
423         }
424         D3(printk(", result: 0x%08x\n", sum));
425         return sum;
426 }
427
428
429 static int
430 jffs_checksum_flash(struct mtd_info *mtd, loff_t start, int size, __u32 *result)
431 {
432         __u32 sum = 0;
433         loff_t ptr = start;
434         __u8 *read_buf;
435         int i, length;
436
437         /* Allocate read buffer */
438         read_buf = (__u8 *) kmalloc (sizeof(__u8) * 4096, GFP_KERNEL);
439         if (!read_buf) {
440                 printk(KERN_NOTICE "kmalloc failed in jffs_checksum_flash()\n");
441                 return -ENOMEM;
442         }
443         /* Loop until checksum done */
444         while (size) {
445                 /* Get amount of data to read */
446                 if (size < 4096)
447                         length = size;
448                 else
449                         length = 4096;
450
451                 /* Perform flash read */
452                 D3(printk(KERN_NOTICE "jffs_checksum_flash\n"));
453                 flash_safe_read(mtd, ptr, &read_buf[0], length);
454
455                 /* Compute checksum */
456                 for (i=0; i < length ; i++)
457                         sum += read_buf[i];
458
459                 /* Update pointer and size */
460                 size -= length;
461                 ptr += length;
462         }
463
464         /* Free read buffer */
465         kfree(read_buf);
466
467         /* Return result */
468         D3(printk("checksum result: 0x%08x\n", sum));
469         *result = sum;
470         return 0;
471 }
472
473 static __inline__ void jffs_fm_write_lock(struct jffs_fmcontrol *fmc)
474 {
475   //    down(&fmc->wlock);
476 }
477
478 static __inline__ void jffs_fm_write_unlock(struct jffs_fmcontrol *fmc)
479 {
480   //    up(&fmc->wlock);
481 }
482
483
484 /* Create and initialize a new struct jffs_file.  */
485 static struct jffs_file *
486 jffs_create_file(struct jffs_control *c,
487                  const struct jffs_raw_inode *raw_inode)
488 {
489         struct jffs_file *f;
490
491         if (!(f = kzalloc(sizeof(*f), GFP_KERNEL))) {
492                 D(printk("jffs_create_file(): Failed!\n"));
493                 return NULL;
494         }
495         no_jffs_file++;
496         f->ino = raw_inode->ino;
497         f->pino = raw_inode->pino;
498         f->nlink = raw_inode->nlink;
499         f->deleted = raw_inode->deleted;
500         f->c = c;
501
502         return f;
503 }
504
505
506 /* Build a control block for the file system.  */
507 static struct jffs_control *
508 jffs_create_control(struct super_block *sb)
509 {
510         struct jffs_control *c;
511         register int s = sizeof(struct jffs_control);
512         int i;
513         D(char *t = 0);
514
515         D2(printk("jffs_create_control()\n"));
516
517         if (!(c = kmalloc(s, GFP_KERNEL))) {
518                 goto fail_control;
519         }
520         DJM(no_jffs_control++);
521         c->root = NULL;
522         c->gc_task = NULL;
523         c->hash_len = JFFS_HASH_SIZE;
524         s = sizeof(struct list_head) * c->hash_len;
525         if (!(c->hash = kmalloc(s, GFP_KERNEL))) {
526                 goto fail_hash;
527         }
528         DJM(no_hash++);
529         for (i = 0; i < c->hash_len; i++)
530                 INIT_LIST_HEAD(&c->hash[i]);
531         if (!(c->fmc = jffs_build_begin(c, MINOR(sb->s_dev)))) {
532                 goto fail_fminit;
533         }
534         c->next_ino = JFFS_MIN_INO + 1;
535         c->delete_list = (struct jffs_delete_list *) 0;
536         return c;
537
538 fail_fminit:
539         D(t = "c->fmc");
540 fail_hash:
541         kfree(c);
542         DJM(no_jffs_control--);
543         D(t = t ? t : "c->hash");
544 fail_control:
545         D(t = t ? t : "control");
546         D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
547         return (struct jffs_control *)0;
548 }
549
550
551 /* Clean up all data structures associated with the file system.  */
552 void
553 jffs_cleanup_control(struct jffs_control *c)
554 {
555         D2(printk("jffs_cleanup_control()\n"));
556
557         if (!c) {
558                 D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
559                 return;
560         }
561
562         while (c->delete_list) {
563                 struct jffs_delete_list *delete_list_element;
564                 delete_list_element = c->delete_list;
565                 c->delete_list = c->delete_list->next;
566                 kfree(delete_list_element);
567         }
568
569         /* Free all files and nodes.  */
570         if (c->hash) {
571                 jffs_foreach_file(c, jffs_free_node_list);
572                 jffs_foreach_file(c, jffs_free_file);
573                 kfree(c->hash);
574                 DJM(no_hash--);
575         }
576         jffs_cleanup_fmcontrol(c->fmc);
577         kfree(c);
578         DJM(no_jffs_control--);
579         D3(printk("jffs_cleanup_control(): Leaving...\n"));
580 }
581
582
583 /* This function adds a virtual root node to the in-RAM representation.
584    Called by jffs_build_fs().  */
585 static int
586 jffs_add_virtual_root(struct jffs_control *c)
587 {
588         struct jffs_file *root;
589         struct jffs_node *node;
590
591         D2(printk("jffs_add_virtual_root(): "
592                   "Creating a virtual root directory.\n"));
593
594         if (!(root = kmalloc(sizeof(struct jffs_file), GFP_KERNEL))) {
595                 return -ENOMEM;
596         }
597         no_jffs_file++;
598         if (!(node = jffs_alloc_node())) {
599                 kfree(root);
600                 no_jffs_file--;
601                 return -ENOMEM;
602         }
603         DJM(no_jffs_node++);
604         memset(node, 0, sizeof(struct jffs_node));
605         node->ino = JFFS_MIN_INO;
606         memset(root, 0, sizeof(struct jffs_file));
607         root->ino = JFFS_MIN_INO;
608         root->mode = S_IFDIR | S_IRWXU | S_IRGRP
609                      | S_IXGRP | S_IROTH | S_IXOTH;
610         root->atime = root->mtime = root->ctime = get_seconds();
611         root->nlink = 1;
612         root->c = c;
613         root->version_head = root->version_tail = node;
614         jffs_insert_file_into_hash(root);
615         return 0;
616 }
617
618
619 /* This is where the file system is built and initialized.  */
620 int
621 jffs_build_fs(struct super_block *sb)
622 {
623         struct jffs_control *c;
624         int err = 0;
625
626         D2(printk("jffs_build_fs()\n"));
627
628         if (!(c = jffs_create_control(sb))) {
629                 return -ENOMEM;
630         }
631         c->building_fs = 1;
632         c->sb = sb;
633         if ((err = jffs_scan_flash(c)) < 0) {
634                 if(err == -EAGAIN){
635                         /* scan_flash() wants us to try once more. A flipping 
636                            bits sector was detect in the middle of the scan flash.
637                            Clean up old allocated memory before going in.
638                         */
639                         D1(printk("jffs_build_fs: Cleaning up all control structures,"
640                                   " reallocating them and trying mount again.\n"));
641                         jffs_cleanup_control(c);
642                         if (!(c = jffs_create_control(sb))) {
643                                 return -ENOMEM;
644                         }
645                         c->building_fs = 1;
646                         c->sb = sb;
647
648                         if ((err = jffs_scan_flash(c)) < 0) {
649                                 goto jffs_build_fs_fail;
650                         }                       
651                 }else{
652                         goto jffs_build_fs_fail;
653                 }
654         }
655
656         /* Add a virtual root node if no one exists.  */
657         if (!jffs_find_file(c, JFFS_MIN_INO)) {
658                 if ((err = jffs_add_virtual_root(c)) < 0) {
659                         goto jffs_build_fs_fail;
660                 }
661         }
662
663         while (c->delete_list) {
664                 struct jffs_file *f;
665                 struct jffs_delete_list *delete_list_element;
666
667                 if ((f = jffs_find_file(c, c->delete_list->ino))) {
668                         f->deleted = 1;
669                 }
670                 delete_list_element = c->delete_list;
671                 c->delete_list = c->delete_list->next;
672                 kfree(delete_list_element);
673         }
674
675         /* Remove deleted nodes.  */
676         if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
677                 printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
678                 goto jffs_build_fs_fail;
679         }
680         /* Remove redundant nodes.  (We are not interested in the
681            return value in this case.)  */
682         jffs_foreach_file(c, jffs_remove_redundant_nodes);
683         /* Try to build a tree from all the nodes.  */
684         if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
685                 printk("JFFS: Failed to build tree.\n");
686                 goto jffs_build_fs_fail;
687         }
688         /* Compute the sizes of all files in the filesystem.  Adjust if
689            necessary.  */
690         if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
691                 printk("JFFS: Failed to build file system.\n");
692                 goto jffs_build_fs_fail;
693         }
694         sb->s_fs_info = (void *)c;
695         c->building_fs = 0;
696
697         D1(jffs_print_hash_table(c));
698         D1(jffs_print_tree(c->root, 0));
699
700         return 0;
701
702 jffs_build_fs_fail:
703         jffs_cleanup_control(c);
704         return err;
705 } /* jffs_build_fs()  */
706
707
708 /*
709   This checks for sectors that were being erased in their previous 
710   lifetimes and for some reason or the other (power fail etc.), 
711   the erase cycles never completed.
712   As the flash array would have reverted back to read status, 
713   these sectors are detected by the symptom of the "flipping bits",
714   i.e. bits being read back differently from the same location in
715   flash if read multiple times.
716   The only solution to this is to re-erase the entire
717   sector.
718   Unfortunately detecting "flipping bits" is not a simple exercise
719   as a bit may be read back at 1 or 0 depending on the alignment 
720   of the stars in the universe.
721   The level of confidence is in direct proportion to the number of 
722   scans done. By power fail testing I (Vipin) have been able to 
723   proove that reading twice is not enough.
724   Maybe 4 times? Change NUM_REREADS to a higher number if you want
725   a (even) higher degree of confidence in your mount process. 
726   A higher number would of course slow down your mount.
727 */
728 static int check_partly_erased_sectors(struct jffs_fmcontrol *fmc){
729
730 #define NUM_REREADS             4 /* see note above */
731 #define READ_AHEAD_BYTES        4096 /* must be a multiple of 4, 
732                                         usually set to kernel page size */
733
734         __u8 *read_buf1;
735         __u8 *read_buf2;
736
737         int err = 0;
738         int retlen;
739         int i;
740         int cnt;
741         __u32 offset;
742         loff_t pos = 0;
743         loff_t end = fmc->flash_size;
744
745
746         /* Allocate read buffers */
747         read_buf1 = (__u8 *) kmalloc (sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
748         if (!read_buf1)
749                 return -ENOMEM;
750
751         read_buf2 = (__u8 *) kmalloc (sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
752         if (!read_buf2) {
753                 kfree(read_buf1);
754                 return -ENOMEM;
755         }
756
757  CHECK_NEXT:
758         while(pos < end){
759                 
760                 D1(printk("check_partly_erased_sector():checking sector which contains"
761                           " offset 0x%x for flipping bits..\n", (__u32)pos));
762                 
763                 retlen = flash_safe_read(fmc->mtd, pos,
764                                          &read_buf1[0], READ_AHEAD_BYTES);
765                 retlen &= ~3;
766                 
767                 for(cnt = 0; cnt < NUM_REREADS; cnt++){
768                         (void)flash_safe_read(fmc->mtd, pos,
769                                               &read_buf2[0], READ_AHEAD_BYTES);
770                         
771                         for (i=0 ; i < retlen ; i+=4) {
772                                 /* buffers MUST match, double word for word! */
773                                 if(*((__u32 *) &read_buf1[i]) !=
774                                    *((__u32 *) &read_buf2[i])
775                                    ){
776                                         /* flipping bits detected, time to erase sector */
777                                         /* This will help us log some statistics etc. */
778                                         D1(printk("Flipping bits detected in re-read round:%i of %i\n",
779                                                cnt, NUM_REREADS));
780                                         D1(printk("check_partly_erased_sectors:flipping bits detected"
781                                                   " @offset:0x%x(0x%x!=0x%x)\n",
782                                                   (__u32)pos+i, *((__u32 *) &read_buf1[i]), 
783                                                   *((__u32 *) &read_buf2[i])));
784                                         
785                                         /* calculate start of present sector */
786                                         offset = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
787                                         
788                                         D1(printk("check_partly_erased_sector():erasing sector starting 0x%x.\n",
789                                                   offset));
790                                         
791                                         if (flash_erase_region(fmc->mtd,
792                                                                offset, fmc->sector_size) < 0) {
793                                                 printk(KERN_ERR "JFFS: Erase of flash failed. "
794                                                        "offset = %u, erase_size = %d\n",
795                                                        offset , fmc->sector_size);
796                                                 
797                                                 err = -EIO;
798                                                 goto returnBack;
799
800                                         }else{
801                                                 D1(printk("JFFS: Erase of flash sector @0x%x successful.\n",
802                                                        offset));
803                                                 /* skip ahead to the next sector */
804                                                 pos = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
805                                                 pos += fmc->sector_size;
806                                                 goto CHECK_NEXT;
807                                         }
808                                 }
809                         }
810                 }
811                 pos += READ_AHEAD_BYTES;
812         }
813
814  returnBack:
815         kfree(read_buf1);
816         kfree(read_buf2);
817
818         D2(printk("check_partly_erased_sector():Done checking all sectors till offset 0x%x for flipping bits.\n",
819                   (__u32)pos));
820
821         return err;
822
823 }/* end check_partly_erased_sectors() */
824
825
826
827 /* Scan the whole flash memory in order to find all nodes in the
828    file systems.  */
829 static int
830 jffs_scan_flash(struct jffs_control *c)
831 {
832         char name[JFFS_MAX_NAME_LEN + 2];
833         struct jffs_raw_inode raw_inode;
834         struct jffs_node *node = NULL;
835         struct jffs_fmcontrol *fmc = c->fmc;
836         __u32 checksum;
837         __u8 tmp_accurate;
838         __u16 tmp_chksum;
839         __u32 deleted_file;
840         loff_t pos = 0;
841         loff_t start;
842         loff_t test_start;
843         loff_t end = fmc->flash_size;
844         __u8 *read_buf;
845         int i, len, retlen;
846         __u32 offset;
847
848         __u32 free_chunk_size1;
849         __u32 free_chunk_size2;
850
851         
852 #define NUMFREEALLOWED     2        /* 2 chunks of at least erase size space allowed */
853         int num_free_space = 0;       /* Flag err if more than TWO
854                                        free blocks found. This is NOT allowed
855                                        by the current jffs design.
856                                     */
857         int num_free_spc_not_accp = 0; /* For debugging purposed keep count 
858                                         of how much free space was rejected and
859                                         marked dirty
860                                      */
861
862         D1(printk("jffs_scan_flash(): start pos = 0x%lx, end = 0x%lx\n",
863                   (long)pos, (long)end));
864
865         flash_safe_acquire(fmc->mtd);
866
867         /*
868           check and make sure that any sector does not suffer
869           from the "partly erased, bit flipping syndrome" (TM Vipin :)
870           If so, offending sectors will be erased.
871         */
872         if(check_partly_erased_sectors(fmc) < 0){
873
874                 flash_safe_release(fmc->mtd);
875                 return -EIO; /* bad, bad, bad error. Cannot continue.*/
876         }
877
878         /* Allocate read buffer */
879         read_buf = (__u8 *) kmalloc (sizeof(__u8) * 4096, GFP_KERNEL);
880         if (!read_buf) {
881                 flash_safe_release(fmc->mtd);
882                 return -ENOMEM;
883         }
884                               
885         /* Start the scan.  */
886         while (pos < end) {
887                 deleted_file = 0;
888
889                 /* Remember the position from where we started this scan.  */
890                 start = pos;
891
892                 switch (flash_read_u32(fmc->mtd, pos)) {
893                 case JFFS_EMPTY_BITMASK:
894                         /* We have found 0xffffffff at this position.  We have to
895                            scan the rest of the flash till the end or till
896                            something else than 0xffffffff is found.
897                            Keep going till we do not find JFFS_EMPTY_BITMASK 
898                            anymore */
899
900                         D1(printk("jffs_scan_flash(): 0xffffffff at pos 0x%lx.\n",
901                                   (long)pos));
902
903                         while(pos < end){
904
905                               len = end - pos < 4096 ? end - pos : 4096;
906                               
907                               retlen = flash_safe_read(fmc->mtd, pos,
908                                                  &read_buf[0], len);
909
910                               retlen &= ~3;
911                               
912                               for (i=0 ; i < retlen ; i+=4, pos += 4) {
913                                       if(*((__u32 *) &read_buf[i]) !=
914                                          JFFS_EMPTY_BITMASK)
915                                         break;
916                               }
917                               if (i == retlen)
918                                     continue;
919                               else
920                                     break;
921                         }
922
923                         D1(printk("jffs_scan_flash():0xffffffff ended at pos 0x%lx.\n",
924                                   (long)pos));
925                         
926                         /* If some free space ends in the middle of a sector,
927                            treat it as dirty rather than clean.
928                            This is to handle the case where one thread 
929                            allocated space for a node, but didn't get to
930                            actually _write_ it before power was lost, leaving
931                            a gap in the log. Shifting all node writes into
932                            a single kernel thread will fix the original problem.
933                         */
934                         if ((__u32) pos % fmc->sector_size) {
935                                 /* If there was free space in previous 
936                                    sectors, don't mark that dirty too - 
937                                    only from the beginning of this sector
938                                    (or from start) 
939                                 */
940
941                                 test_start = pos & ~(fmc->sector_size-1); /* end of last sector */
942
943                                 if (start < test_start) {
944
945                                         /* free space started in the previous sector! */
946
947                                         if((num_free_space < NUMFREEALLOWED) && 
948                                            ((unsigned int)(test_start - start) >= fmc->sector_size)){
949
950                                                 /*
951                                                   Count it in if we are still under NUMFREEALLOWED *and* it is 
952                                                   at least 1 erase sector in length. This will keep us from 
953                                                   picking any little ole' space as "free".
954                                                 */
955                                           
956                                                 D1(printk("Reducing end of free space to 0x%x from 0x%x\n",
957                                                           (unsigned int)test_start, (unsigned int)pos));
958
959                                                 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
960                                                           (unsigned int) start,
961                                                           (unsigned int)(test_start - start)));
962
963                                                 /* below, space from "start" to "pos" will be marked dirty. */
964                                                 start = test_start; 
965                                                 
966                                                 /* Being in here means that we have found at least an entire 
967                                                    erase sector size of free space ending on a sector boundary.
968                                                    Keep track of free spaces accepted.
969                                                 */
970                                                 num_free_space++;
971                                         }else{
972                                                 num_free_spc_not_accp++;
973                                                 D1(printk("Free space (#%i) found but *Not* accepted: Starting"
974                                                           " 0x%x for 0x%x bytes\n",
975                                                           num_free_spc_not_accp, (unsigned int)start, 
976                                                           (unsigned int)((unsigned int)(pos & ~(fmc->sector_size-1)) - (unsigned int)start)));
977                                                 
978                                         }
979                                         
980                                 }
981                                 if((((__u32)(pos - start)) != 0)){
982
983                                         D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
984                                                   (unsigned int) start, (unsigned int) (pos - start)));
985                                         jffs_fmalloced(fmc, (__u32) start,
986                                                        (__u32) (pos - start), NULL);
987                                 }else{
988                                         /* "Flipping bits" detected. This means that our scan for them
989                                            did not catch this offset. See check_partly_erased_sectors() for
990                                            more info.
991                                         */
992                                         
993                                         D1(printk("jffs_scan_flash():wants to allocate dirty flash "
994                                                   "space for 0 bytes.\n"));
995                                         D1(printk("jffs_scan_flash(): Flipping bits! We will free "
996                                                   "all allocated memory, erase this sector and remount\n"));
997
998                                         /* calculate start of present sector */
999                                         offset = (((__u32)pos)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
1000                                         
1001                                         D1(printk("jffs_scan_flash():erasing sector starting 0x%x.\n",
1002                                                   offset));
1003                                         
1004                                         if (flash_erase_region(fmc->mtd,
1005                                                                offset, fmc->sector_size) < 0) {
1006                                                 printk(KERN_ERR "JFFS: Erase of flash failed. "
1007                                                        "offset = %u, erase_size = %d\n",
1008                                                        offset , fmc->sector_size);
1009
1010                                                 flash_safe_release(fmc->mtd);
1011                                                 kfree(read_buf);
1012                                                 return -1; /* bad, bad, bad! */
1013
1014                                         }
1015                                         flash_safe_release(fmc->mtd);
1016                                         kfree(read_buf);
1017
1018                                         return -EAGAIN; /* erased offending sector. Try mount one more time please. */
1019                                 }
1020                         }else{
1021                                 /* Being in here means that we have found free space that ends on an erase sector
1022                                    boundary.
1023                                    Count it in if we are still under NUMFREEALLOWED *and* it is at least 1 erase 
1024                                    sector in length. This will keep us from picking any little ole' space as "free".
1025                                  */
1026                                  if((num_free_space < NUMFREEALLOWED) && 
1027                                     ((unsigned int)(pos - start) >= fmc->sector_size)){
1028                                            /* We really don't do anything to mark space as free, except *not* 
1029                                               mark it dirty and just advance the "pos" location pointer. 
1030                                               It will automatically be picked up as free space.
1031                                             */ 
1032                                            num_free_space++;
1033                                            D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
1034                                                      (unsigned int) start, (unsigned int) (pos - start)));
1035                                  }else{
1036                                          num_free_spc_not_accp++;
1037                                          D1(printk("Free space (#%i) found but *Not* accepted: Starting "
1038                                                    "0x%x for 0x%x bytes\n", num_free_spc_not_accp, 
1039                                                    (unsigned int) start, 
1040                                                    (unsigned int) (pos - start)));
1041                                          
1042                                          /* Mark this space as dirty. We already have our free space. */
1043                                          D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
1044                                                    (unsigned int) start, (unsigned int) (pos - start)));
1045                                          jffs_fmalloced(fmc, (__u32) start,
1046                                                         (__u32) (pos - start), NULL);                                      
1047                                  }
1048                                  
1049                         }
1050                         if(num_free_space > NUMFREEALLOWED){
1051                                  printk(KERN_WARNING "jffs_scan_flash(): Found free space "
1052                                         "number %i. Only %i free space is allowed.\n",
1053                                         num_free_space, NUMFREEALLOWED);                              
1054                         }
1055                         continue;
1056
1057                 case JFFS_DIRTY_BITMASK:
1058                         /* We have found 0x00000000 at this position.  Scan as far
1059                            as possible to find out how much is dirty.  */
1060                         D1(printk("jffs_scan_flash(): 0x00000000 at pos 0x%lx.\n",
1061                                   (long)pos));
1062                         for (; pos < end
1063                                && JFFS_DIRTY_BITMASK == flash_read_u32(fmc->mtd, pos);
1064                              pos += 4);
1065                         D1(printk("jffs_scan_flash(): 0x00 ended at "
1066                                   "pos 0x%lx.\n", (long)pos));
1067                         jffs_fmalloced(fmc, (__u32) start,
1068                                        (__u32) (pos - start), NULL);
1069                         continue;
1070
1071                 case JFFS_MAGIC_BITMASK:
1072                         /* We have probably found a new raw inode.  */
1073                         break;
1074
1075                 default:
1076                 bad_inode:
1077                         /* We're f*cked.  This is not solved yet.  We have
1078                            to scan for the magic pattern.  */
1079                         D1(printk("*************** Dirty flash memory or "
1080                                   "bad inode: "
1081                                   "hexdump(pos = 0x%lx, len = 128):\n",
1082                                   (long)pos));
1083                         D1(jffs_hexdump(fmc->mtd, pos, 128));
1084
1085                         for (pos += 4; pos < end; pos += 4) {
1086                                 switch (flash_read_u32(fmc->mtd, pos)) {
1087                                 case JFFS_MAGIC_BITMASK:
1088                                 case JFFS_EMPTY_BITMASK:
1089                                         /* handle these in the main switch() loop */
1090                                         goto cont_scan;
1091
1092                                 default:
1093                                         break;
1094                                 }
1095                         }
1096
1097                         cont_scan:
1098                         /* First, mark as dirty the region
1099                            which really does contain crap. */
1100                         jffs_fmalloced(fmc, (__u32) start,
1101                                        (__u32) (pos - start),
1102                                        NULL);
1103                         
1104                         continue;
1105                 }/* switch */
1106
1107                 /* We have found the beginning of an inode.  Create a
1108                    node for it unless there already is one available.  */
1109                 if (!node) {
1110                         if (!(node = jffs_alloc_node())) {
1111                                 /* Free read buffer */
1112                                 kfree(read_buf);
1113
1114                                 /* Release the flash device */
1115                                 flash_safe_release(fmc->mtd);
1116         
1117                                 return -ENOMEM;
1118                         }
1119                         DJM(no_jffs_node++);
1120                 }
1121
1122                 /* Read the next raw inode.  */
1123
1124                 flash_safe_read(fmc->mtd, pos, (u_char *) &raw_inode,
1125                                 sizeof(struct jffs_raw_inode));
1126
1127                 /* When we compute the checksum for the inode, we never
1128                    count the 'accurate' or the 'checksum' fields.  */
1129                 tmp_accurate = raw_inode.accurate;
1130                 tmp_chksum = raw_inode.chksum;
1131                 raw_inode.accurate = 0;
1132                 raw_inode.chksum = 0;
1133                 checksum = jffs_checksum(&raw_inode,
1134                                          sizeof(struct jffs_raw_inode));
1135                 raw_inode.accurate = tmp_accurate;
1136                 raw_inode.chksum = tmp_chksum;
1137
1138                 D3(printk("*** We have found this raw inode at pos 0x%lx "
1139                           "on the flash:\n", (long)pos));
1140                 D3(jffs_print_raw_inode(&raw_inode));
1141
1142                 if (checksum != raw_inode.chksum) {
1143                         D1(printk("jffs_scan_flash(): Bad checksum: "
1144                                   "checksum = %u, "
1145                                   "raw_inode.chksum = %u\n",
1146                                   checksum, raw_inode.chksum));
1147                         pos += sizeof(struct jffs_raw_inode);
1148                         jffs_fmalloced(fmc, (__u32) start,
1149                                        (__u32) (pos - start), NULL);
1150                         /* Reuse this unused struct jffs_node.  */
1151                         continue;
1152                 }
1153
1154                 /* Check the raw inode read so far.  Start with the
1155                    maximum length of the filename.  */
1156                 if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
1157                         printk(KERN_WARNING "jffs_scan_flash: Found a "
1158                                "JFFS node with name too large\n");
1159                         goto bad_inode;
1160                 }
1161
1162                 if (raw_inode.rename && raw_inode.dsize != sizeof(__u32)) {
1163                         printk(KERN_WARNING "jffs_scan_flash: Found a "
1164                                "rename node with dsize %u.\n",
1165                                raw_inode.dsize);
1166                         jffs_print_raw_inode(&raw_inode);
1167                         goto bad_inode;
1168                 }
1169
1170                 /* The node's data segment should not exceed a
1171                    certain length.  */
1172                 if (raw_inode.dsize > fmc->max_chunk_size) {
1173                         printk(KERN_WARNING "jffs_scan_flash: Found a "
1174                                "JFFS node with dsize (0x%x) > max_chunk_size (0x%x)\n",
1175                                raw_inode.dsize, fmc->max_chunk_size);
1176                         goto bad_inode;
1177                 }
1178
1179                 pos += sizeof(struct jffs_raw_inode);
1180
1181                 /* This shouldn't be necessary because a node that
1182                    violates the flash boundaries shouldn't be written
1183                    in the first place. */
1184                 if (pos >= end) {
1185                         goto check_node;
1186                 }
1187
1188                 /* Read the name.  */
1189                 *name = 0;
1190                 if (raw_inode.nsize) {
1191                         flash_safe_read(fmc->mtd, pos, name, raw_inode.nsize);
1192                         name[raw_inode.nsize] = '\0';
1193                         pos += raw_inode.nsize
1194                                + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1195                         D3(printk("name == \"%s\"\n", name));
1196                         checksum = jffs_checksum(name, raw_inode.nsize);
1197                         if (checksum != raw_inode.nchksum) {
1198                                 D1(printk("jffs_scan_flash(): Bad checksum: "
1199                                           "checksum = %u, "
1200                                           "raw_inode.nchksum = %u\n",
1201                                           checksum, raw_inode.nchksum));
1202                                 jffs_fmalloced(fmc, (__u32) start,
1203                                                (__u32) (pos - start), NULL);
1204                                 /* Reuse this unused struct jffs_node.  */
1205                                 continue;
1206                         }
1207                         if (pos >= end) {
1208                                 goto check_node;
1209                         }
1210                 }
1211
1212                 /* Read the data, if it exists, in order to be sure it
1213                    matches the checksum.  */
1214                 if (raw_inode.dsize) {
1215                         if (raw_inode.rename) {
1216                                 deleted_file = flash_read_u32(fmc->mtd, pos);
1217                         }
1218                         if (jffs_checksum_flash(fmc->mtd, pos, raw_inode.dsize, &checksum)) {
1219                                 printk("jffs_checksum_flash() failed to calculate a checksum\n");
1220                                 jffs_fmalloced(fmc, (__u32) start,
1221                                                (__u32) (pos - start), NULL);
1222                                 /* Reuse this unused struct jffs_node.  */
1223                                 continue;
1224                         }                               
1225                         pos += raw_inode.dsize
1226                                + JFFS_GET_PAD_BYTES(raw_inode.dsize);
1227
1228                         if (checksum != raw_inode.dchksum) {
1229                                 D1(printk("jffs_scan_flash(): Bad checksum: "
1230                                           "checksum = %u, "
1231                                           "raw_inode.dchksum = %u\n",
1232                                           checksum, raw_inode.dchksum));
1233                                 jffs_fmalloced(fmc, (__u32) start,
1234                                                (__u32) (pos - start), NULL);
1235                                 /* Reuse this unused struct jffs_node.  */
1236                                 continue;
1237                         }
1238                 }
1239
1240                 check_node:
1241
1242                 /* Remember the highest inode number in the whole file
1243                    system.  This information will be used when assigning
1244                    new files new inode numbers.  */
1245                 if (c->next_ino <= raw_inode.ino) {
1246                         c->next_ino = raw_inode.ino + 1;
1247                 }
1248
1249                 if (raw_inode.accurate) {
1250                         int err;
1251                         node->data_offset = raw_inode.offset;
1252                         node->data_size = raw_inode.dsize;
1253                         node->removed_size = raw_inode.rsize;
1254                         /* Compute the offset to the actual data in the
1255                            on-flash node.  */
1256                         node->fm_offset
1257                         = sizeof(struct jffs_raw_inode)
1258                           + raw_inode.nsize
1259                           + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1260                         node->fm = jffs_fmalloced(fmc, (__u32) start,
1261                                                   (__u32) (pos - start),
1262                                                   node);
1263                         if (!node->fm) {
1264                                 D(printk("jffs_scan_flash(): !node->fm\n"));
1265                                 jffs_free_node(node);
1266                                 DJM(no_jffs_node--);
1267
1268                                 /* Free read buffer */
1269                                 kfree(read_buf);
1270
1271                                 /* Release the flash device */
1272                                 flash_safe_release(fmc->mtd);
1273
1274                                 return -ENOMEM;
1275                         }
1276                         if ((err = jffs_insert_node(c, NULL, &raw_inode,
1277                                                     name, node)) < 0) {
1278                                 printk("JFFS: Failed to handle raw inode. "
1279                                        "(err = %d)\n", err);
1280                                 break;
1281                         }
1282                         if (raw_inode.rename) {
1283                                 struct jffs_delete_list *dl
1284                                 = (struct jffs_delete_list *)
1285                                   kmalloc(sizeof(struct jffs_delete_list),
1286                                           GFP_KERNEL);
1287                                 if (!dl) {
1288                                         D(printk("jffs_scan_flash: !dl\n"));
1289                                         jffs_free_node(node);
1290                                         DJM(no_jffs_node--);
1291
1292                                         /* Release the flash device */
1293                                         flash_safe_release(fmc->flash_part);
1294
1295                                         /* Free read buffer */
1296                                         kfree(read_buf);
1297
1298                                         return -ENOMEM;
1299                                 }
1300                                 dl->ino = deleted_file;
1301                                 dl->next = c->delete_list;
1302                                 c->delete_list = dl;
1303                                 node->data_size = 0;
1304                         }
1305                         D3(jffs_print_node(node));
1306                         node = NULL; /* Don't free the node!  */
1307                 }
1308                 else {
1309                         jffs_fmalloced(fmc, (__u32) start,
1310                                        (__u32) (pos - start), NULL);
1311                         D3(printk("jffs_scan_flash(): Just found an obsolete "
1312                                   "raw_inode. Continuing the scan...\n"));
1313                         /* Reuse this unused struct jffs_node.  */
1314                 }
1315         }
1316
1317         if (node) {
1318                 jffs_free_node(node);
1319                 DJM(no_jffs_node--);
1320         }
1321         jffs_build_end(fmc);
1322
1323         /* Free read buffer */
1324         kfree(read_buf);
1325
1326         if(!num_free_space){
1327                 printk(KERN_WARNING "jffs_scan_flash(): Did not find even a single "
1328                        "chunk of free space. This is BAD!\n");
1329         }
1330
1331         /* Return happy */
1332         D3(printk("jffs_scan_flash(): Leaving...\n"));
1333         flash_safe_release(fmc->mtd);
1334
1335         /* This is to trap the "free size accounting screwed error. */
1336         free_chunk_size1 = jffs_free_size1(fmc);
1337         free_chunk_size2 = jffs_free_size2(fmc);
1338
1339         if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
1340
1341                 printk(KERN_WARNING "jffs_scan_falsh():Free size accounting screwed\n");
1342                 printk(KERN_WARNING "jfffs_scan_flash():free_chunk_size1 == 0x%x, "
1343                        "free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n", 
1344                        free_chunk_size1, free_chunk_size2, fmc->free_size);
1345
1346                 return -1; /* Do NOT mount f/s so that we can inspect what happened.
1347                               Mounting this  screwed up f/s will screw us up anyway.
1348                             */
1349         }       
1350
1351         return 0; /* as far as we are concerned, we are happy! */
1352 } /* jffs_scan_flash()  */
1353
1354
1355 /* Insert any kind of node into the file system.  Take care of data
1356    insertions and deletions.  Also remove redundant information. The
1357    memory allocated for the `name' is regarded as "given away" in the
1358    caller's perspective.  */
1359 int
1360 jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
1361                  const struct jffs_raw_inode *raw_inode,
1362                  const char *name, struct jffs_node *node)
1363 {
1364         int update_name = 0;
1365         int insert_into_tree = 0;
1366
1367         D2(printk("jffs_insert_node(): ino = %u, version = %u, "
1368                   "name = \"%s\", deleted = %d\n",
1369                   raw_inode->ino, raw_inode->version,
1370                   ((name && *name) ? name : ""), raw_inode->deleted));
1371
1372         /* If there doesn't exist an associated jffs_file, then
1373            create, initialize and insert one into the file system.  */
1374         if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
1375                 if (!(f = jffs_create_file(c, raw_inode))) {
1376                         return -ENOMEM;
1377                 }
1378                 jffs_insert_file_into_hash(f);
1379                 insert_into_tree = 1;
1380         }
1381         node->ino = raw_inode->ino;
1382         node->version = raw_inode->version;
1383         node->data_size = raw_inode->dsize;
1384         node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
1385                           + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1386         node->name_size = raw_inode->nsize;
1387
1388         /* Now insert the node at the correct position into the file's
1389            version list.  */
1390         if (!f->version_head) {
1391                 /* This is the first node.  */
1392                 f->version_head = node;
1393                 f->version_tail = node;
1394                 node->version_prev = NULL;
1395                 node->version_next = NULL;
1396                 f->highest_version = node->version;
1397                 update_name = 1;
1398                 f->mode = raw_inode->mode;
1399                 f->uid = raw_inode->uid;
1400                 f->gid = raw_inode->gid;
1401                 f->atime = raw_inode->atime;
1402                 f->mtime = raw_inode->mtime;
1403                 f->ctime = raw_inode->ctime;
1404         }
1405         else if ((f->highest_version < node->version)
1406                  || (node->version == 0)) {
1407                 /* Insert at the end of the list.  I.e. this node is the
1408                    newest one so far.  */
1409                 node->version_prev = f->version_tail;
1410                 node->version_next = NULL;
1411                 f->version_tail->version_next = node;
1412                 f->version_tail = node;
1413                 f->highest_version = node->version;
1414                 update_name = 1;
1415                 f->pino = raw_inode->pino;
1416                 f->mode = raw_inode->mode;
1417                 f->uid = raw_inode->uid;
1418                 f->gid = raw_inode->gid;
1419                 f->atime = raw_inode->atime;
1420                 f->mtime = raw_inode->mtime;
1421                 f->ctime = raw_inode->ctime;
1422         }
1423         else if (f->version_head->version > node->version) {
1424                 /* Insert at the bottom of the list.  */
1425                 node->version_prev = NULL;
1426                 node->version_next = f->version_head;
1427                 f->version_head->version_prev = node;
1428                 f->version_head = node;
1429                 if (!f->name) {
1430                         update_name = 1;
1431                 }
1432         }
1433         else {
1434                 struct jffs_node *n;
1435                 int newer_name = 0;
1436                 /* Search for the insertion position starting from
1437                    the tail (newest node).  */
1438                 for (n = f->version_tail; n; n = n->version_prev) {
1439                         if (n->version < node->version) {
1440                                 node->version_prev = n;
1441                                 node->version_next = n->version_next;
1442                                 node->version_next->version_prev = node;
1443                                 n->version_next = node;
1444                                 if (!newer_name) {
1445                                         update_name = 1;
1446                                 }
1447                                 break;
1448                         }
1449                         if (n->name_size) {
1450                                 newer_name = 1;
1451                         }
1452                 }
1453         }
1454
1455         /* Deletion is irreversible. If any 'deleted' node is ever
1456            written, the file is deleted */
1457         if (raw_inode->deleted)
1458                 f->deleted = raw_inode->deleted;
1459
1460         /* Perhaps update the name.  */
1461         if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
1462                 if (f->name) {
1463                         kfree(f->name);
1464                         DJM(no_name--);
1465                 }
1466                 if (!(f->name = (char *) kmalloc(raw_inode->nsize + 1,
1467                                                  GFP_KERNEL))) {
1468                         return -ENOMEM;
1469                 }
1470                 DJM(no_name++);
1471                 memcpy(f->name, name, raw_inode->nsize);
1472                 f->name[raw_inode->nsize] = '\0';
1473                 f->nsize = raw_inode->nsize;
1474                 D3(printk("jffs_insert_node(): Updated the name of "
1475                           "the file to \"%s\".\n", name));
1476         }
1477
1478         if (!c->building_fs) {
1479                 D3(printk("jffs_insert_node(): ---------------------------"
1480                           "------------------------------------------- 1\n"));
1481                 if (insert_into_tree) {
1482                         jffs_insert_file_into_tree(f);
1483                 }
1484                 /* Once upon a time, we would call jffs_possibly_delete_file()
1485                    here. That causes an oops if someone's still got the file
1486                    open, so now we only do it in jffs_delete_inode()
1487                    -- dwmw2
1488                 */
1489                 if (node->data_size || node->removed_size) {
1490                         jffs_update_file(f, node);
1491                 }
1492                 jffs_remove_redundant_nodes(f);
1493
1494                 jffs_garbage_collect_trigger(c);
1495
1496                 D3(printk("jffs_insert_node(): ---------------------------"
1497                           "------------------------------------------- 2\n"));
1498         }
1499
1500         return 0;
1501 } /* jffs_insert_node()  */
1502
1503
1504 /* Unlink a jffs_node from the version list it is in.  */
1505 static inline void
1506 jffs_unlink_node_from_version_list(struct jffs_file *f,
1507                                    struct jffs_node *node)
1508 {
1509         if (node->version_prev) {
1510                 node->version_prev->version_next = node->version_next;
1511         } else {
1512                 f->version_head = node->version_next;
1513         }
1514         if (node->version_next) {
1515                 node->version_next->version_prev = node->version_prev;
1516         } else {
1517                 f->version_tail = node->version_prev;
1518         }
1519 }
1520
1521
1522 /* Unlink a jffs_node from the range list it is in.  */
1523 static inline void
1524 jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
1525 {
1526         if (node->range_prev) {
1527                 node->range_prev->range_next = node->range_next;
1528         }
1529         else {
1530                 f->range_head = node->range_next;
1531         }
1532         if (node->range_next) {
1533                 node->range_next->range_prev = node->range_prev;
1534         }
1535         else {
1536                 f->range_tail = node->range_prev;
1537         }
1538 }
1539
1540
1541 /* Function used by jffs_remove_redundant_nodes() below.  This function
1542    classifies what kind of information a node adds to a file.  */
1543 static inline __u8
1544 jffs_classify_node(struct jffs_node *node)
1545 {
1546         __u8 mod_type = JFFS_MODIFY_INODE;
1547
1548         if (node->name_size) {
1549                 mod_type |= JFFS_MODIFY_NAME;
1550         }
1551         if (node->data_size || node->removed_size) {
1552                 mod_type |= JFFS_MODIFY_DATA;
1553         }
1554         return mod_type;
1555 }
1556
1557
1558 /* Remove redundant nodes from a file.  Mark the on-flash memory
1559    as dirty.  */
1560 static int
1561 jffs_remove_redundant_nodes(struct jffs_file *f)
1562 {
1563         struct jffs_node *newest_node;
1564         struct jffs_node *cur;
1565         struct jffs_node *prev;
1566         __u8 newest_type;
1567         __u8 mod_type;
1568         __u8 node_with_name_later = 0;
1569
1570         if (!(newest_node = f->version_tail)) {
1571                 return 0;
1572         }
1573
1574         /* What does the `newest_node' modify?  */
1575         newest_type = jffs_classify_node(newest_node);
1576         node_with_name_later = newest_type & JFFS_MODIFY_NAME;
1577
1578         D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1579                   "newest_type: %u\n", f->ino, (f->name ? f->name : ""),
1580                   newest_type));
1581
1582         /* Traverse the file's nodes and determine which of them that are
1583            superfluous.  Yeah, this might look very complex at first
1584            glance but it is actually very simple.  */
1585         for (cur = newest_node->version_prev; cur; cur = prev) {
1586                 prev = cur->version_prev;
1587                 mod_type = jffs_classify_node(cur);
1588                 if ((mod_type <= JFFS_MODIFY_INODE)
1589                     || ((newest_type & JFFS_MODIFY_NAME)
1590                         && (mod_type
1591                             <= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
1592                     || (cur->data_size == 0 && cur->removed_size
1593                         && !cur->version_prev && node_with_name_later)) {
1594                         /* Yes, this node is redundant. Remove it.  */
1595                         D2(printk("jffs_remove_redundant_nodes(): "
1596                                   "Removing node: ino: %u, version: %u, "
1597                                   "mod_type: %u\n", cur->ino, cur->version,
1598                                   mod_type));
1599                         jffs_unlink_node_from_version_list(f, cur);
1600                         jffs_fmfree(f->c->fmc, cur->fm, cur);
1601                         jffs_free_node(cur);
1602                         DJM(no_jffs_node--);
1603                 }
1604                 else {
1605                         node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
1606                 }
1607         }
1608
1609         return 0;
1610 }
1611
1612
1613 /* Insert a file into the hash table.  */
1614 static int
1615 jffs_insert_file_into_hash(struct jffs_file *f)
1616 {
1617         int i = f->ino % f->c->hash_len;
1618
1619         D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
1620
1621         list_add(&f->hash, &f->c->hash[i]);
1622         return 0;
1623 }
1624
1625
1626 /* Insert a file into the file system tree.  */
1627 int
1628 jffs_insert_file_into_tree(struct jffs_file *f)
1629 {
1630         struct jffs_file *parent;
1631
1632         D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1633                   (f->name ? f->name : "")));
1634
1635         if (!(parent = jffs_find_file(f->c, f->pino))) {
1636                 if (f->pino == 0) {
1637                         f->c->root = f;
1638                         f->parent = NULL;
1639                         f->sibling_prev = NULL;
1640                         f->sibling_next = NULL;
1641                         return 0;
1642                 }
1643                 else {
1644                         D1(printk("jffs_insert_file_into_tree(): Found "
1645                                   "inode with no parent and pino == %u\n",
1646                                   f->pino));
1647                         return -1;
1648                 }
1649         }
1650         f->parent = parent;
1651         f->sibling_next = parent->children;
1652         if (f->sibling_next) {
1653                 f->sibling_next->sibling_prev = f;
1654         }
1655         f->sibling_prev = NULL;
1656         parent->children = f;
1657         return 0;
1658 }
1659
1660
1661 /* Remove a file from the hash table.  */
1662 static int
1663 jffs_unlink_file_from_hash(struct jffs_file *f)
1664 {
1665         D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1666                   "ino %u\n", f, f->ino));
1667
1668         list_del(&f->hash);
1669         return 0;
1670 }
1671
1672
1673 /* Just remove the file from the parent's children.  Don't free
1674    any memory.  */
1675 int
1676 jffs_unlink_file_from_tree(struct jffs_file *f)
1677 {
1678         D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
1679                   "\"%s\"\n", f->ino, f->pino, (f->name ? f->name : "")));
1680
1681         if (f->sibling_prev) {
1682                 f->sibling_prev->sibling_next = f->sibling_next;
1683         }
1684         else if (f->parent) {
1685                 D3(printk("f->parent=%p\n", f->parent));
1686                 f->parent->children = f->sibling_next;
1687         }
1688         if (f->sibling_next) {
1689                 f->sibling_next->sibling_prev = f->sibling_prev;
1690         }
1691         return 0;
1692 }
1693
1694
1695 /* Find a file with its inode number.  */
1696 struct jffs_file *
1697 jffs_find_file(struct jffs_control *c, __u32 ino)
1698 {
1699         struct jffs_file *f;
1700         int i = ino % c->hash_len;
1701
1702         D3(printk("jffs_find_file(): ino: %u\n", ino));
1703
1704         list_for_each_entry(f, &c->hash[i], hash) {
1705                 if (ino != f->ino)
1706                         continue;
1707                 D3(printk("jffs_find_file(): Found file with ino "
1708                                "%u. (name: \"%s\")\n",
1709                                ino, (f->name ? f->name : ""));
1710                 );
1711                 return f;
1712         }
1713         D3(printk("jffs_find_file(): Didn't find file "
1714                          "with ino %u.\n", ino);
1715         );
1716         return NULL;
1717 }
1718
1719
1720 /* Find a file in a directory.  We are comparing the names.  */
1721 struct jffs_file *
1722 jffs_find_child(struct jffs_file *dir, const char *name, int len)
1723 {
1724         struct jffs_file *f;
1725
1726         D3(printk("jffs_find_child()\n"));
1727
1728         for (f = dir->children; f; f = f->sibling_next) {
1729                 if (!f->deleted && f->name
1730                     && !strncmp(f->name, name, len)
1731                     && f->name[len] == '\0') {
1732                         break;
1733                 }
1734         }
1735
1736         D3(if (f) {
1737                 printk("jffs_find_child(): Found \"%s\".\n", f->name);
1738         }
1739         else {
1740                 char *copy = (char *) kmalloc(len + 1, GFP_KERNEL);
1741                 if (copy) {
1742                         memcpy(copy, name, len);
1743                         copy[len] = '\0';
1744                 }
1745                 printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1746                        (copy ? copy : ""));
1747                 kfree(copy);
1748         });
1749
1750         return f;
1751 }
1752
1753
1754 /* Write a raw inode that takes up a certain amount of space in the flash
1755    memory.  At the end of the flash device, there is often space that is
1756    impossible to use.  At these times we want to mark this space as not
1757    used.  In the cases when the amount of space is greater or equal than
1758    a struct jffs_raw_inode, we write a "dummy node" that takes up this
1759    space.  The space after the raw inode, if it exists, is left as it is.
1760    Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1761    we can compute the checksum of it; we don't have to manipulate it any
1762    further.
1763
1764    If the space left on the device is less than the size of a struct
1765    jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1766    No raw inode is written this time.  */
1767 static int
1768 jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1769 {
1770         struct jffs_fmcontrol *fmc = c->fmc;
1771         int err;
1772
1773         D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1774                   "dirty_fm->size = %u\n",
1775                   dirty_fm->offset, dirty_fm->size));
1776
1777         if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
1778                 struct jffs_raw_inode raw_inode;
1779                 memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1780                 raw_inode.magic = JFFS_MAGIC_BITMASK;
1781                 raw_inode.dsize = dirty_fm->size
1782                                   - sizeof(struct jffs_raw_inode);
1783                 raw_inode.dchksum = raw_inode.dsize * 0xff;
1784                 raw_inode.chksum
1785                 = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
1786
1787                 if ((err = flash_safe_write(fmc->mtd,
1788                                             dirty_fm->offset,
1789                                             (u_char *)&raw_inode,
1790                                             sizeof(struct jffs_raw_inode)))
1791                     < 0) {
1792                         printk(KERN_ERR "JFFS: jffs_write_dummy_node: "
1793                                "flash_safe_write failed!\n");
1794                         return err;
1795                 }
1796         }
1797         else {
1798                 flash_safe_acquire(fmc->mtd);
1799                 flash_memset(fmc->mtd, dirty_fm->offset, 0, dirty_fm->size);
1800                 flash_safe_release(fmc->mtd);
1801         }
1802
1803         D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1804         return 0;
1805 }
1806
1807
1808 /* Write a raw inode, possibly its name and possibly some data.  */
1809 int
1810 jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1811                 struct jffs_raw_inode *raw_inode,
1812                 const char *name, const unsigned char *data,
1813                 int recoverable,
1814                 struct jffs_file *f)
1815 {
1816         struct jffs_fmcontrol *fmc = c->fmc;
1817         struct jffs_fm *fm;
1818         struct kvec node_iovec[4];
1819         unsigned long iovec_cnt;
1820
1821         __u32 pos;
1822         int err;
1823         __u32 slack = 0;
1824
1825         __u32 total_name_size = raw_inode->nsize
1826                                 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1827         __u32 total_data_size = raw_inode->dsize
1828                                 + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1829         __u32 total_size = sizeof(struct jffs_raw_inode)
1830                            + total_name_size + total_data_size;
1831         
1832         /* If this node isn't something that will eventually let
1833            GC free even more space, then don't allow it unless
1834            there's at least max_chunk_size space still available
1835         */
1836         if (!recoverable)
1837                 slack = fmc->max_chunk_size;
1838                 
1839
1840         /* Fire the retrorockets and shoot the fruiton torpedoes, sir!  */
1841
1842         ASSERT(if (!node) {
1843                 printk("jffs_write_node(): node == NULL\n");
1844                 return -EINVAL;
1845         });
1846         ASSERT(if (raw_inode && raw_inode->nsize && !name) {
1847                 printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1848                        raw_inode->nsize);
1849                 return -EINVAL;
1850         });
1851
1852         D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1853                   "total_size = %u\n",
1854                   (name ? name : ""), raw_inode->ino,
1855                   total_size));
1856
1857         jffs_fm_write_lock(fmc);
1858
1859 retry:
1860         fm = NULL;
1861         err = 0;
1862         while (!fm) {
1863
1864                 /* Deadlocks suck. */
1865                 while(fmc->free_size < fmc->min_free_size + total_size + slack) {
1866                         jffs_fm_write_unlock(fmc);
1867                         if (!JFFS_ENOUGH_SPACE(c, total_size + slack))
1868                                 return -ENOSPC;
1869                         jffs_fm_write_lock(fmc);
1870                 }
1871
1872                 /* First try to allocate some flash memory.  */
1873                 err = jffs_fmalloc(fmc, total_size, node, &fm);
1874                 
1875                 if (err == -ENOSPC) {
1876                         /* Just out of space. GC and try again */
1877                         if (fmc->dirty_size < fmc->sector_size) {
1878                                 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1879                                          "failed, no dirty space to GC\n", fmc,
1880                                          total_size));
1881                                 return err;
1882                         }
1883                         
1884                         D1(printk(KERN_INFO "jffs_write_node(): Calling jffs_garbage_collect_now()\n"));
1885                         jffs_fm_write_unlock(fmc);
1886                         if ((err = jffs_garbage_collect_now(c))) {
1887                                 D(printk("jffs_write_node(): jffs_garbage_collect_now() failed\n"));
1888                                 return err;
1889                         }
1890                         jffs_fm_write_lock(fmc);
1891                         continue;
1892                 } 
1893
1894                 if (err < 0) {
1895                         jffs_fm_write_unlock(fmc);
1896
1897                         D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1898                                  "failed!\n", fmc, total_size));
1899                         return err;
1900                 }
1901
1902                 if (!fm->nodes) {
1903                         /* The jffs_fm struct that we got is not good enough.
1904                            Make that space dirty and try again  */
1905                         if ((err = jffs_write_dummy_node(c, fm)) < 0) {
1906                                 kfree(fm);
1907                                 DJM(no_jffs_fm--);
1908                                 jffs_fm_write_unlock(fmc);
1909                                 D(printk("jffs_write_node(): "
1910                                          "jffs_write_dummy_node(): Failed!\n"));
1911                                 return err;
1912                         }
1913                         fm = NULL;
1914                 }
1915         } /* while(!fm) */
1916         node->fm = fm;
1917
1918         ASSERT(if (fm->nodes == 0) {
1919                 printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1920         });
1921
1922         pos = node->fm->offset;
1923
1924         /* Increment the version number here. We can't let the caller
1925            set it beforehand, because we might have had to do GC on a node
1926            of this file - and we'd end up reusing version numbers.
1927         */
1928         if (f) {
1929                 raw_inode->version = f->highest_version + 1;
1930                 D1(printk (KERN_NOTICE "jffs_write_node(): setting version of %s to %d\n", f->name, raw_inode->version));
1931
1932                 /* if the file was deleted, set the deleted bit in the raw inode */
1933                 if (f->deleted)
1934                         raw_inode->deleted = 1;
1935         }
1936
1937         /* Compute the checksum for the data and name chunks.  */
1938         raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
1939         raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
1940
1941         /* The checksum is calculated without the chksum and accurate
1942            fields so set them to zero first.  */
1943         raw_inode->accurate = 0;
1944         raw_inode->chksum = 0;
1945         raw_inode->chksum = jffs_checksum(raw_inode,
1946                                           sizeof(struct jffs_raw_inode));
1947         raw_inode->accurate = 0xff;
1948
1949         D3(printk("jffs_write_node(): About to write this raw inode to the "
1950                   "flash at pos 0x%lx:\n", (long)pos));
1951         D3(jffs_print_raw_inode(raw_inode));
1952
1953         /* The actual raw JFFS node */
1954         node_iovec[0].iov_base = (void *) raw_inode;
1955         node_iovec[0].iov_len = (size_t) sizeof(struct jffs_raw_inode);
1956         iovec_cnt = 1;
1957
1958         /* Get name and size if there is one */
1959         if (raw_inode->nsize) {
1960                 node_iovec[iovec_cnt].iov_base = (void *) name;
1961                 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->nsize;
1962                 iovec_cnt++;
1963
1964                 if (JFFS_GET_PAD_BYTES(raw_inode->nsize)) {
1965                         static unsigned char allff[3]={255,255,255};
1966                         /* Add some extra padding if necessary */
1967                         node_iovec[iovec_cnt].iov_base = allff;
1968                         node_iovec[iovec_cnt].iov_len =
1969                                 JFFS_GET_PAD_BYTES(raw_inode->nsize);
1970                         iovec_cnt++;
1971                 }
1972         }
1973
1974         /* Get data and size if there is any */
1975         if (raw_inode->dsize) {
1976                 node_iovec[iovec_cnt].iov_base = (void *) data;
1977                 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->dsize;
1978                 iovec_cnt++;
1979                 /* No need to pad this because we're not actually putting
1980                    anything after it.
1981                 */
1982         }
1983
1984         if ((err = flash_safe_writev(fmc->mtd, node_iovec, iovec_cnt,
1985                                     pos)) < 0) {
1986                 jffs_fmfree_partly(fmc, fm, 0);
1987                 jffs_fm_write_unlock(fmc);
1988                 printk(KERN_ERR "JFFS: jffs_write_node: Failed to write, "
1989                        "requested %i, wrote %i\n", total_size, err);
1990                 goto retry;
1991         }
1992         if (raw_inode->deleted)
1993                 f->deleted = 1;
1994
1995         jffs_fm_write_unlock(fmc);
1996         D3(printk("jffs_write_node(): Leaving...\n"));
1997         return raw_inode->dsize;
1998 } /* jffs_write_node()  */
1999
2000
2001 /* Read data from the node and write it to the buffer.  'node_offset'
2002    is how much we have read from this particular node before and which
2003    shouldn't be read again.  'max_size' is how much space there is in
2004    the buffer.  */
2005 static int
2006 jffs_get_node_data(struct jffs_file *f, struct jffs_node *node, 
2007                    unsigned char *buf,__u32 node_offset, __u32 max_size)
2008 {
2009         struct jffs_fmcontrol *fmc = f->c->fmc;
2010         __u32 pos = node->fm->offset + node->fm_offset + node_offset;
2011         __u32 avail = node->data_size - node_offset;
2012         __u32 r;
2013
2014         D2(printk("  jffs_get_node_data(): file: \"%s\", ino: %u, "
2015                   "version: %u, node_offset: %u\n",
2016                   f->name, node->ino, node->version, node_offset));
2017
2018         r = min(avail, max_size);
2019         D3(printk(KERN_NOTICE "jffs_get_node_data\n"));
2020         flash_safe_read(fmc->mtd, pos, buf, r);
2021
2022         D3(printk("  jffs_get_node_data(): Read %u byte%s.\n",
2023                   r, (r == 1 ? "" : "s")));
2024
2025         return r;
2026 }
2027
2028
2029 /* Read data from the file's nodes.  Write the data to the buffer
2030    'buf'.  'read_offset' tells how much data we should skip.  */
2031 int
2032 jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset,
2033                __u32 size)
2034 {
2035         struct jffs_node *node;
2036         __u32 read_data = 0; /* Total amount of read data.  */
2037         __u32 node_offset = 0;
2038         __u32 pos = 0; /* Number of bytes traversed.  */
2039
2040         D2(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
2041                   "size = %u\n",
2042                   (f->name ? f->name : ""), read_offset, size));
2043
2044         if (read_offset >= f->size) {
2045                 D(printk("  f->size: %d\n", f->size));
2046                 return 0;
2047         }
2048
2049         /* First find the node to read data from.  */
2050         node = f->range_head;
2051         while (pos <= read_offset) {
2052                 node_offset = read_offset - pos;
2053                 if (node_offset >= node->data_size) {
2054                         pos += node->data_size;
2055                         node = node->range_next;
2056                 }
2057                 else {
2058                         break;
2059                 }
2060         }
2061
2062         /* "Cats are living proof that not everything in nature
2063            has to be useful."
2064            - Garrison Keilor ('97)  */
2065
2066         /* Fill the buffer.  */
2067         while (node && (read_data < size)) {
2068                 int r;
2069                 if (!node->fm) {
2070                         /* This node does not refer to real data.  */
2071                         r = min(size - read_data,
2072                                      node->data_size - node_offset);
2073                         memset(&buf[read_data], 0, r);
2074                 }
2075                 else if ((r = jffs_get_node_data(f, node, &buf[read_data],
2076                                                  node_offset,
2077                                                  size - read_data)) < 0) {
2078                         return r;
2079                 }
2080                 read_data += r;
2081                 node_offset = 0;
2082                 node = node->range_next;
2083         }
2084         D3(printk("  jffs_read_data(): Read %u bytes.\n", read_data));
2085         return read_data;
2086 }
2087
2088
2089 /* Used for traversing all nodes in the hash table.  */
2090 int
2091 jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
2092 {
2093         int pos;
2094         int r;
2095         int result = 0;
2096
2097         for (pos = 0; pos < c->hash_len; pos++) {
2098                 struct jffs_file *f, *next;
2099
2100                 /* We must do _safe, because 'func' might remove the
2101                    current file 'f' from the list.  */
2102                 list_for_each_entry_safe(f, next, &c->hash[pos], hash) {
2103                         r = func(f);
2104                         if (r < 0)
2105                                 return r;
2106                         result += r;
2107                 }
2108         }
2109
2110         return result;
2111 }
2112
2113
2114 /* Free all nodes associated with a file.  */
2115 static int
2116 jffs_free_node_list(struct jffs_file *f)
2117 {
2118         struct jffs_node *node;
2119         struct jffs_node *p;
2120
2121         D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
2122                   f->ino, (f->name ? f->name : "")));
2123         node = f->version_head;
2124         while (node) {
2125                 p = node;
2126                 node = node->version_next;
2127                 jffs_free_node(p);
2128                 DJM(no_jffs_node--);
2129         }
2130         return 0;
2131 }
2132
2133
2134 /* Free a file and its name.  */
2135 static int
2136 jffs_free_file(struct jffs_file *f)
2137 {
2138         D3(printk("jffs_free_file: f #%u, \"%s\"\n",
2139                   f->ino, (f->name ? f->name : "")));
2140
2141         if (f->name) {
2142                 kfree(f->name);
2143                 DJM(no_name--);
2144         }
2145         kfree(f);
2146         no_jffs_file--;
2147         return 0;
2148 }
2149
2150 static long
2151 jffs_get_file_count(void)
2152 {
2153         return no_jffs_file;
2154 }
2155
2156 /* See if a file is deleted. If so, mark that file's nodes as obsolete.  */
2157 int
2158 jffs_possibly_delete_file(struct jffs_file *f)
2159 {
2160         struct jffs_node *n;
2161
2162         D3(printk("jffs_possibly_delete_file(): ino: %u\n",
2163                   f->ino));
2164
2165         ASSERT(if (!f) {
2166                 printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
2167                 return -1;
2168         });
2169
2170         if (f->deleted) {
2171                 /* First try to remove all older versions.  Commence with
2172                    the oldest node.  */
2173                 for (n = f->version_head; n; n = n->version_next) {
2174                         if (!n->fm) {
2175                                 continue;
2176                         }
2177                         if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
2178                                 break;
2179                         }
2180                 }
2181                 /* Unlink the file from the filesystem.  */
2182                 if (!f->c->building_fs) {
2183                         jffs_unlink_file_from_tree(f);
2184                 }
2185                 jffs_unlink_file_from_hash(f);
2186                 jffs_free_node_list(f);
2187                 jffs_free_file(f);
2188         }
2189         return 0;
2190 }
2191
2192
2193 /* Used in conjunction with jffs_foreach_file() to count the number
2194    of files in the file system.  */
2195 int
2196 jffs_file_count(struct jffs_file *f)
2197 {
2198         return 1;
2199 }
2200
2201
2202 /* Build up a file's range list from scratch by going through the
2203    version list.  */
2204 static int
2205 jffs_build_file(struct jffs_file *f)
2206 {
2207         struct jffs_node *n;
2208
2209         D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
2210                   f->ino, (f->name ? f->name : "")));
2211
2212         for (n = f->version_head; n; n = n->version_next) {
2213                 jffs_update_file(f, n);
2214         }
2215         return 0;
2216 }
2217
2218
2219 /* Remove an amount of data from a file. If this amount of data is
2220    zero, that could mean that a node should be split in two parts.
2221    We remove or change the appropriate nodes in the lists.
2222
2223    Starting offset of area to be removed is node->data_offset,
2224    and the length of the area is in node->removed_size.   */
2225 static int
2226 jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
2227 {
2228         struct jffs_node *n;
2229         __u32 offset = node->data_offset;
2230         __u32 remove_size = node->removed_size;
2231
2232         D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
2233                   offset, remove_size));
2234
2235         if (remove_size == 0
2236             && f->range_tail
2237             && f->range_tail->data_offset + f->range_tail->data_size
2238                == offset) {
2239                 /* A simple append; nothing to remove or no node to split.  */
2240                 return 0;
2241         }
2242
2243         /* Find the node where we should begin the removal.  */
2244         for (n = f->range_head; n; n = n->range_next) {
2245                 if (n->data_offset + n->data_size > offset) {
2246                         break;
2247                 }
2248         }
2249         if (!n) {
2250                 /* If there's no data in the file there's no data to
2251                    remove either.  */
2252                 return 0;
2253         }
2254
2255         if (n->data_offset > offset) {
2256                 /* XXX: Not implemented yet.  */
2257                 printk(KERN_WARNING "JFFS: An unexpected situation "
2258                        "occurred in jffs_delete_data.\n");
2259         }
2260         else if (n->data_offset < offset) {
2261                 /* See if the node has to be split into two parts.  */
2262                 if (n->data_offset + n->data_size > offset + remove_size) {
2263                         /* Do the split.  */
2264                         struct jffs_node *new_node;
2265                         D3(printk("jffs_delete_data(): Split node with "
2266                                   "version number %u.\n", n->version));
2267
2268                         if (!(new_node = jffs_alloc_node())) {
2269                                 D(printk("jffs_delete_data(): -ENOMEM\n"));
2270                                 return -ENOMEM;
2271                         }
2272                         DJM(no_jffs_node++);
2273
2274                         new_node->ino = n->ino;
2275                         new_node->version = n->version;
2276                         new_node->data_offset = offset;
2277                         new_node->data_size = n->data_size - (remove_size + (offset - n->data_offset));
2278                         new_node->fm_offset = n->fm_offset + (remove_size + (offset - n->data_offset));
2279                         new_node->name_size = n->name_size;
2280                         new_node->fm = n->fm;
2281                         new_node->version_prev = n;
2282                         new_node->version_next = n->version_next;
2283                         if (new_node->version_next) {
2284                                 new_node->version_next->version_prev
2285                                 = new_node;
2286                         }
2287                         else {
2288                                 f->version_tail = new_node;
2289                         }
2290                         n->version_next = new_node;
2291                         new_node->range_prev = n;
2292                         new_node->range_next = n->range_next;
2293                         if (new_node->range_next) {
2294                                 new_node->range_next->range_prev = new_node;
2295                         }
2296                         else {
2297                                 f->range_tail = new_node;
2298                         }
2299                         /* A very interesting can of worms.  */
2300                         n->range_next = new_node;
2301                         n->data_size = offset - n->data_offset;
2302                         if (new_node->fm)
2303                                 jffs_add_node(new_node);
2304                         else {
2305                                 D1(printk(KERN_WARNING "jffs_delete_data(): Splitting an empty node (file hold).\n!"));
2306                                 D1(printk(KERN_WARNING "FIXME: Did dwmw2 do the right thing here?\n"));
2307                         }
2308                         n = new_node->range_next;
2309                         remove_size = 0;
2310                 }
2311                 else {
2312                         /* No.  No need to split the node.  Just remove
2313                            the end of the node.  */
2314                         int r = min(n->data_offset + n->data_size
2315                                          - offset, remove_size);
2316                         n->data_size -= r;
2317                         remove_size -= r;
2318                         n = n->range_next;
2319                 }
2320         }
2321
2322         /* Remove as many nodes as necessary.  */
2323         while (n && remove_size) {
2324                 if (n->data_size <= remove_size) {
2325                         struct jffs_node *p = n;
2326                         remove_size -= n->data_size;
2327                         n = n->range_next;
2328                         D3(printk("jffs_delete_data(): Removing node: "
2329                                   "ino: %u, version: %u%s\n",
2330                                   p->ino, p->version,
2331                                   (p->fm ? "" : " (virtual)")));
2332                         if (p->fm) {
2333                                 jffs_fmfree(f->c->fmc, p->fm, p);
2334                         }
2335                         jffs_unlink_node_from_range_list(f, p);
2336                         jffs_unlink_node_from_version_list(f, p);
2337                         jffs_free_node(p);
2338                         DJM(no_jffs_node--);
2339                 }
2340                 else {
2341                         n->data_size -= remove_size;
2342                         n->fm_offset += remove_size;
2343                         n->data_offset -= (node->removed_size - remove_size);
2344                         n = n->range_next;
2345                         break;
2346                 }
2347         }
2348
2349         /* Adjust the following nodes' information about offsets etc.  */
2350         while (n && node->removed_size) {
2351                 n->data_offset -= node->removed_size;
2352                 n = n->range_next;
2353         }
2354
2355         if (node->removed_size > (f->size - node->data_offset)) {
2356                 /* It's possible that the removed_size is in fact
2357                  * greater than the amount of data we actually thought
2358                  * were present in the first place - some of the nodes 
2359                  * which this node originally obsoleted may already have
2360                  * been deleted from the flash by subsequent garbage 
2361                  * collection.
2362                  *
2363                  * If this is the case, don't let f->size go negative.
2364                  * Bad things would happen :)
2365                  */
2366                 f->size = node->data_offset;
2367         } else {
2368                 f->size -= node->removed_size;
2369         }
2370         D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
2371         return 0;
2372 } /* jffs_delete_data()  */
2373
2374
2375 /* Insert some data into a file.  Prior to the call to this function,
2376    jffs_delete_data should be called.  */
2377 static int
2378 jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
2379 {
2380         D3(printk("jffs_insert_data(): node->data_offset = %u, "
2381                   "node->data_size = %u, f->size = %u\n",
2382                   node->data_offset, node->data_size, f->size));
2383
2384         /* Find the position where we should insert data.  */
2385         retry:
2386         if (node->data_offset == f->size) {
2387                 /* A simple append.  This is the most common operation.  */
2388                 node->range_next = NULL;
2389                 node->range_prev = f->range_tail;
2390                 if (node->range_prev) {
2391                         node->range_prev->range_next = node;
2392                 }
2393                 f->range_tail = node;
2394                 f->size += node->data_size;
2395                 if (!f->range_head) {
2396                         f->range_head = node;
2397                 }
2398         }
2399         else if (node->data_offset < f->size) {
2400                 /* Trying to insert data into the middle of the file.  This
2401                    means no problem because jffs_delete_data() has already
2402                    prepared the range list for us.  */
2403                 struct jffs_node *n;
2404
2405                 /* Find the correct place for the insertion and then insert
2406                    the node.  */
2407                 for (n = f->range_head; n; n = n->range_next) {
2408                         D2(printk("Cool stuff's happening!\n"));
2409
2410                         if (n->data_offset == node->data_offset) {
2411                                 node->range_prev = n->range_prev;
2412                                 if (node->range_prev) {
2413                                         node->range_prev->range_next = node;
2414                                 }
2415                                 else {
2416                                         f->range_head = node;
2417                                 }
2418                                 node->range_next = n;
2419                                 n->range_prev = node;
2420                                 break;
2421                         }
2422                         ASSERT(else if (n->data_offset + n->data_size >
2423                                         node->data_offset) {
2424                                 printk(KERN_ERR "jffs_insert_data(): "
2425                                        "Couldn't find a place to insert "
2426                                        "the data!\n");
2427                                 return -1;
2428                         });
2429                 }
2430
2431                 /* Adjust later nodes' offsets etc.  */
2432                 n = node->range_next;
2433                 while (n) {
2434                         n->data_offset += node->data_size;
2435                         n = n->range_next;
2436                 }
2437                 f->size += node->data_size;
2438         }
2439         else if (node->data_offset > f->size) {
2440                 /* Okay.  This is tricky.  This means that we want to insert
2441                    data at a place that is beyond the limits of the file as
2442                    it is constructed right now.  This is actually a common
2443                    event that for instance could occur during the mounting
2444                    of the file system if a large file have been truncated,
2445                    rewritten and then only partially garbage collected.  */
2446
2447                 struct jffs_node *n;
2448
2449                 /* We need a place holder for the data that is missing in
2450                    front of this insertion.  This "virtual node" will not
2451                    be associated with any space on the flash device.  */
2452                 struct jffs_node *virtual_node;
2453                 if (!(virtual_node = jffs_alloc_node())) {
2454                         return -ENOMEM;
2455                 }
2456
2457                 D(printk("jffs_insert_data: Inserting a virtual node.\n"));
2458                 D(printk("  node->data_offset = %u\n", node->data_offset));
2459                 D(printk("  f->size = %u\n", f->size));
2460
2461                 virtual_node->ino = node->ino;
2462                 virtual_node->version = node->version;
2463                 virtual_node->removed_size = 0;
2464                 virtual_node->fm_offset = 0;
2465                 virtual_node->name_size = 0;
2466                 virtual_node->fm = NULL; /* This is a virtual data holder.  */
2467                 virtual_node->version_prev = NULL;
2468                 virtual_node->version_next = NULL;
2469                 virtual_node->range_next = NULL;
2470
2471                 /* Are there any data at all in the file yet?  */
2472                 if (f->range_head) {
2473                         virtual_node->data_offset
2474                         = f->range_tail->data_offset
2475                           + f->range_tail->data_size;
2476                         virtual_node->data_size
2477                         = node->data_offset - virtual_node->data_offset;
2478                         virtual_node->range_prev = f->range_tail;
2479                         f->range_tail->range_next = virtual_node;
2480                 }
2481                 else {
2482                         virtual_node->data_offset = 0;
2483                         virtual_node->data_size = node->data_offset;
2484                         virtual_node->range_prev = NULL;
2485                         f->range_head = virtual_node;
2486                 }
2487
2488                 f->range_tail = virtual_node;
2489                 f->size += virtual_node->data_size;
2490
2491                 /* Insert this virtual node in the version list as well.  */
2492                 for (n = f->version_head; n ; n = n->version_next) {
2493                         if (n->version == virtual_node->version) {
2494                                 virtual_node->version_prev = n->version_prev;
2495                                 n->version_prev = virtual_node;
2496                                 if (virtual_node->version_prev) {
2497                                         virtual_node->version_prev
2498                                         ->version_next = virtual_node;
2499                                 }
2500                                 else {
2501                                         f->version_head = virtual_node;
2502                                 }
2503                                 virtual_node->version_next = n;
2504                                 break;
2505                         }
2506                 }
2507
2508                 D(jffs_print_node(virtual_node));
2509
2510                 /* Make a new try to insert the node.  */
2511                 goto retry;
2512         }
2513
2514         D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
2515         return 0;
2516 }
2517
2518
2519 /* A new node (with data) has been added to the file and now the range
2520    list has to be modified.  */
2521 static int
2522 jffs_update_file(struct jffs_file *f, struct jffs_node *node)
2523 {
2524         int err;
2525
2526         D3(printk("jffs_update_file(): ino: %u, version: %u\n",
2527                   f->ino, node->version));
2528
2529         if (node->data_size == 0) {
2530                 if (node->removed_size == 0) {
2531                         /* data_offset == X  */
2532                         /* data_size == 0  */
2533                         /* remove_size == 0  */
2534                 }
2535                 else {
2536                         /* data_offset == X  */
2537                         /* data_size == 0  */
2538                         /* remove_size != 0  */
2539                         if ((err = jffs_delete_data(f, node)) < 0) {
2540                                 return err;
2541                         }
2542                 }
2543         }
2544         else {
2545                 /* data_offset == X  */
2546                 /* data_size != 0  */
2547                 /* remove_size == Y  */
2548                 if ((err = jffs_delete_data(f, node)) < 0) {
2549                         return err;
2550                 }
2551                 if ((err = jffs_insert_data(f, node)) < 0) {
2552                         return err;
2553                 }
2554         }
2555         return 0;
2556 }
2557
2558 /* Print the contents of a file.  */
2559 #if 0
2560 int
2561 jffs_print_file(struct jffs_file *f)
2562 {
2563         D(int i);
2564         D(printk("jffs_file: 0x%p\n", f));
2565         D(printk("{\n"));
2566         D(printk("        0x%08x, /* ino  */\n", f->ino));
2567         D(printk("        0x%08x, /* pino  */\n", f->pino));
2568         D(printk("        0x%08x, /* mode  */\n", f->mode));
2569         D(printk("        0x%04x,     /* uid  */\n", f->uid));
2570         D(printk("        0x%04x,     /* gid  */\n", f->gid));
2571         D(printk("        0x%08x, /* atime  */\n", f->atime));
2572         D(printk("        0x%08x, /* mtime  */\n", f->mtime));
2573         D(printk("        0x%08x, /* ctime  */\n", f->ctime));
2574         D(printk("        0x%02x,       /* nsize  */\n", f->nsize));
2575         D(printk("        0x%02x,       /* nlink  */\n", f->nlink));
2576         D(printk("        0x%02x,       /* deleted  */\n", f->deleted));
2577         D(printk("        \"%s\", ", (f->name ? f->name : "")));
2578         D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
2579                 printk(" ");
2580         });
2581         D(printk("/* name  */\n"));
2582         D(printk("        0x%08x, /* size  */\n", f->size));
2583         D(printk("        0x%08x, /* highest_version  */\n",
2584                  f->highest_version));
2585         D(printk("        0x%p, /* c  */\n", f->c));
2586         D(printk("        0x%p, /* parent  */\n", f->parent));
2587         D(printk("        0x%p, /* children  */\n", f->children));
2588         D(printk("        0x%p, /* sibling_prev  */\n", f->sibling_prev));
2589         D(printk("        0x%p, /* sibling_next  */\n", f->sibling_next));
2590         D(printk("        0x%p, /* hash_prev  */\n", f->hash.prev));
2591         D(printk("        0x%p, /* hash_next  */\n", f->hash.next));
2592         D(printk("        0x%p, /* range_head  */\n", f->range_head));
2593         D(printk("        0x%p, /* range_tail  */\n", f->range_tail));
2594         D(printk("        0x%p, /* version_head  */\n", f->version_head));
2595         D(printk("        0x%p, /* version_tail  */\n", f->version_tail));
2596         D(printk("}\n"));
2597         return 0;
2598 }
2599 #endif  /*  0  */
2600
2601 void
2602 jffs_print_hash_table(struct jffs_control *c)
2603 {
2604         int i;
2605
2606         printk("JFFS: Dumping the file system's hash table...\n");
2607         for (i = 0; i < c->hash_len; i++) {
2608                 struct jffs_file *f;
2609                 list_for_each_entry(f, &c->hash[i], hash) {
2610                         printk("*** c->hash[%u]: \"%s\" "
2611                                "(ino: %u, pino: %u)\n",
2612                                i, (f->name ? f->name : ""),
2613                                f->ino, f->pino);
2614                 }
2615         }
2616 }
2617
2618
2619 void
2620 jffs_print_tree(struct jffs_file *first_file, int indent)
2621 {
2622         struct jffs_file *f;
2623         char *space;
2624         int dir;
2625
2626         if (!first_file) {
2627                 return;
2628         }
2629
2630         if (!(space = (char *) kmalloc(indent + 1, GFP_KERNEL))) {
2631                 printk("jffs_print_tree(): Out of memory!\n");
2632                 return;
2633         }
2634
2635         memset(space, ' ', indent);
2636         space[indent] = '\0';
2637
2638         for (f = first_file; f; f = f->sibling_next) {
2639                 dir = S_ISDIR(f->mode);
2640                 printk("%s%s%s (ino: %u, highest_version: %u, size: %u)\n",
2641                        space, (f->name ? f->name : ""), (dir ? "/" : ""),
2642                        f->ino, f->highest_version, f->size);
2643                 if (dir) {
2644                         jffs_print_tree(f->children, indent + 2);
2645                 }
2646         }
2647
2648         kfree(space);
2649 }
2650
2651
2652 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2653 void
2654 jffs_print_memory_allocation_statistics(void)
2655 {
2656         static long printout;
2657         printk("________ Memory printout #%ld ________\n", ++printout);
2658         printk("no_jffs_file = %ld\n", no_jffs_file);
2659         printk("no_jffs_node = %ld\n", no_jffs_node);
2660         printk("no_jffs_control = %ld\n", no_jffs_control);
2661         printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
2662         printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
2663         printk("no_jffs_fm = %ld\n", no_jffs_fm);
2664         printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
2665         printk("no_hash = %ld\n", no_hash);
2666         printk("no_name = %ld\n", no_name);
2667         printk("\n");
2668 }
2669 #endif
2670
2671
2672 /* Rewrite `size' bytes, and begin at `node'.  */
2673 static int
2674 jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, __u32 size)
2675 {
2676         struct jffs_control *c = f->c;
2677         struct jffs_fmcontrol *fmc = c->fmc;
2678         struct jffs_raw_inode raw_inode;
2679         struct jffs_node *new_node;
2680         struct jffs_fm *fm;
2681         __u32 pos;
2682         __u32 pos_dchksum;
2683         __u32 total_name_size;
2684         __u32 total_data_size;
2685         __u32 total_size;
2686         int err;
2687
2688         D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2689                   f->ino, (f->name ? f->name : "(null)"), size));
2690
2691         /* Create and initialize the new node.  */
2692         if (!(new_node = jffs_alloc_node())) {
2693                 D(printk("jffs_rewrite_data(): "
2694                          "Failed to allocate node.\n"));
2695                 return -ENOMEM;
2696         }
2697         DJM(no_jffs_node++);
2698         new_node->data_offset = node->data_offset;
2699         new_node->removed_size = size;
2700         total_name_size = JFFS_PAD(f->nsize);
2701         total_data_size = JFFS_PAD(size);
2702         total_size = sizeof(struct jffs_raw_inode)
2703                      + total_name_size + total_data_size;
2704         new_node->fm_offset = sizeof(struct jffs_raw_inode)
2705                               + total_name_size;
2706
2707 retry:
2708         jffs_fm_write_lock(fmc);
2709         err = 0;
2710
2711         if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
2712                 DJM(no_jffs_node--);
2713                 jffs_fm_write_unlock(fmc);
2714                 D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2715                 jffs_free_node(new_node);
2716                 return err;
2717         }
2718         else if (!fm->nodes) {
2719                 /* The jffs_fm struct that we got is not big enough.  */
2720                 /* This should never happen, because we deal with this case
2721                    in jffs_garbage_collect_next().*/
2722                 printk(KERN_WARNING "jffs_rewrite_data(): Allocated node is too small (%d bytes of %d)\n", fm->size, total_size);
2723                 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
2724                         D(printk("jffs_rewrite_data(): "
2725                                  "jffs_write_dummy_node() Failed!\n"));
2726                 } else {
2727                         err = -ENOSPC;
2728                 }
2729                 DJM(no_jffs_fm--);
2730                 jffs_fm_write_unlock(fmc);
2731                 kfree(fm);
2732                 
2733                 return err;
2734         }
2735         new_node->fm = fm;
2736
2737         /* Initialize the raw inode.  */
2738         raw_inode.magic = JFFS_MAGIC_BITMASK;
2739         raw_inode.ino = f->ino;
2740         raw_inode.pino = f->pino;
2741         raw_inode.version = f->highest_version + 1;
2742         raw_inode.mode = f->mode;
2743         raw_inode.uid = f->uid;
2744         raw_inode.gid = f->gid;
2745         raw_inode.atime = f->atime;
2746         raw_inode.mtime = f->mtime;
2747         raw_inode.ctime = f->ctime;
2748         raw_inode.offset = node->data_offset;
2749         raw_inode.dsize = size;
2750         raw_inode.rsize = size;
2751         raw_inode.nsize = f->nsize;
2752         raw_inode.nlink = f->nlink;
2753         raw_inode.spare = 0;
2754         raw_inode.rename = 0;
2755         raw_inode.deleted = f->deleted;
2756         raw_inode.accurate = 0xff;
2757         raw_inode.dchksum = 0;
2758         raw_inode.nchksum = 0;
2759
2760         pos = new_node->fm->offset;
2761         pos_dchksum = pos +JFFS_RAW_INODE_DCHKSUM_OFFSET;
2762
2763         D3(printk("jffs_rewrite_data(): Writing this raw inode "
2764                   "to pos 0x%ul.\n", pos));
2765         D3(jffs_print_raw_inode(&raw_inode));
2766
2767         if ((err = flash_safe_write(fmc->mtd, pos,
2768                                     (u_char *) &raw_inode,
2769                                     sizeof(struct jffs_raw_inode)
2770                                     - sizeof(__u32)
2771                                     - sizeof(__u16) - sizeof(__u16))) < 0) {
2772                 jffs_fmfree_partly(fmc, fm,
2773                                    total_name_size + total_data_size);
2774                 jffs_fm_write_unlock(fmc);
2775                 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2776                         "rewrite. (raw inode)\n");
2777                 printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2778                         "rewrite. (raw inode)\n");
2779                 goto retry;
2780         }
2781         pos += sizeof(struct jffs_raw_inode);
2782
2783         /* Write the name to the flash memory.  */
2784         if (f->nsize) {
2785                 D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2786                           "pos 0x%ul.\n", f->name, (unsigned int) pos));
2787                 if ((err = flash_safe_write(fmc->mtd, pos,
2788                                             (u_char *)f->name,
2789                                             f->nsize)) < 0) {
2790                         jffs_fmfree_partly(fmc, fm, total_data_size);
2791                         jffs_fm_write_unlock(fmc);
2792                         printk(KERN_ERR "JFFS: jffs_rewrite_data: Write "
2793                                 "error during rewrite. (name)\n");
2794                         printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2795                                 "rewrite. (name)\n");
2796                         goto retry;
2797                 }
2798                 pos += total_name_size;
2799                 raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
2800         }
2801
2802         /* Write the data.  */
2803         if (size) {
2804                 int r;
2805                 unsigned char *page;
2806                 __u32 offset = node->data_offset;
2807
2808                 if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
2809                         jffs_fmfree_partly(fmc, fm, 0);
2810                         return -1;
2811                 }
2812
2813                 while (size) {
2814                         __u32 s = min(size, (__u32)PAGE_SIZE);
2815                         if ((r = jffs_read_data(f, (char *)page,
2816                                                 offset, s)) < s) {
2817                                 free_page((unsigned long)page);
2818                                 jffs_fmfree_partly(fmc, fm, 0);
2819                                 jffs_fm_write_unlock(fmc);
2820                                 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2821                                          "jffs_read_data() "
2822                                          "failed! (r = %d)\n", r);
2823                                 return -1;
2824                         }
2825                         if ((err = flash_safe_write(fmc->mtd,
2826                                                     pos, page, r)) < 0) {
2827                                 free_page((unsigned long)page);
2828                                 jffs_fmfree_partly(fmc, fm, 0);
2829                                 jffs_fm_write_unlock(fmc);
2830                                 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2831                                        "Write error during rewrite. "
2832                                        "(data)\n");
2833                                 goto retry;
2834                         }
2835                         pos += r;
2836                         size -= r;
2837                         offset += r;
2838                         raw_inode.dchksum += jffs_checksum(page, r);
2839                 }
2840
2841                 free_page((unsigned long)page);
2842         }
2843
2844         raw_inode.accurate = 0;
2845         raw_inode.chksum = jffs_checksum(&raw_inode,
2846                                          sizeof(struct jffs_raw_inode)
2847                                          - sizeof(__u16));
2848
2849         /* Add the checksum.  */
2850         if ((err
2851              = flash_safe_write(fmc->mtd, pos_dchksum,
2852                                 &((u_char *)
2853                                 &raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
2854                                 sizeof(__u32) + sizeof(__u16)
2855                                 + sizeof(__u16))) < 0) {
2856                 jffs_fmfree_partly(fmc, fm, 0);
2857                 jffs_fm_write_unlock(fmc);
2858                 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2859                        "rewrite. (checksum)\n");
2860                 goto retry;
2861         }
2862
2863         /* Now make the file system aware of the newly written node.  */
2864         jffs_insert_node(c, f, &raw_inode, f->name, new_node);
2865         jffs_fm_write_unlock(fmc);
2866
2867         D3(printk("jffs_rewrite_data(): Leaving...\n"));
2868         return 0;
2869 } /* jffs_rewrite_data()  */
2870
2871
2872 /* jffs_garbage_collect_next implements one step in the garbage collect
2873    process and is often called multiple times at each occasion of a
2874    garbage collect.  */
2875
2876 static int
2877 jffs_garbage_collect_next(struct jffs_control *c)
2878 {
2879         struct jffs_fmcontrol *fmc = c->fmc;
2880         struct jffs_node *node;
2881         struct jffs_file *f;
2882         int err = 0;
2883         __u32 size;
2884         __u32 data_size;
2885         __u32 total_name_size;
2886         __u32 extra_available;
2887         __u32 space_needed;
2888         __u32 free_chunk_size1 = jffs_free_size1(fmc);
2889         D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
2890
2891         /* Get the oldest node in the flash.  */
2892         node = jffs_get_oldest_node(fmc);
2893         ASSERT(if (!node) {
2894                 printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
2895                        "No oldest node found!\n");
2896                 err = -1;
2897                 goto jffs_garbage_collect_next_end;
2898                 
2899
2900         });
2901
2902         /* Find its corresponding file too.  */
2903         f = jffs_find_file(c, node->ino);
2904
2905         if (!f) {
2906           printk (KERN_ERR "JFFS: jffs_garbage_collect_next: "
2907                   "No file to garbage collect! "
2908                   "(ino = 0x%08x)\n", node->ino);
2909           /* FIXME: Free the offending node and recover. */
2910           err = -1;
2911           goto jffs_garbage_collect_next_end;
2912         }
2913
2914         /* We always write out the name. Theoretically, we don't need
2915            to, but for now it's easier - because otherwise we'd have
2916            to keep track of how many times the current name exists on
2917            the flash and make sure it never reaches zero.
2918
2919            The current approach means that would be possible to cause
2920            the GC to end up eating its tail by writing lots of nodes
2921            with no name for it to garbage-collect. Hence the change in
2922            inode.c to write names with _every_ node.
2923
2924            It sucks, but it _should_ work.
2925         */
2926         total_name_size = JFFS_PAD(f->nsize);
2927
2928         D1(printk("jffs_garbage_collect_next(): \"%s\", "
2929                   "ino: %u, version: %u, location 0x%x, dsize %u\n",
2930                   (f->name ? f->name : ""), node->ino, node->version, 
2931                   node->fm->offset, node->data_size));
2932
2933         /* Compute how many data it's possible to rewrite at the moment.  */
2934         data_size = f->size - node->data_offset;
2935
2936         /* And from that, the total size of the chunk we want to write */
2937         size = sizeof(struct jffs_raw_inode) + total_name_size
2938                + data_size + JFFS_GET_PAD_BYTES(data_size);
2939
2940         /* If that's more than max_chunk_size, reduce it accordingly */
2941         if (size > fmc->max_chunk_size) {
2942                 size = fmc->max_chunk_size;
2943                 data_size = size - sizeof(struct jffs_raw_inode)
2944                             - total_name_size;
2945         }
2946
2947         /* If we're asking to take up more space than free_chunk_size1
2948            but we _could_ fit in it, shrink accordingly.
2949         */
2950         if (size > free_chunk_size1) {
2951
2952                 if (free_chunk_size1 <
2953                     (sizeof(struct jffs_raw_inode) + total_name_size + BLOCK_SIZE)){
2954                         /* The space left is too small to be of any
2955                            use really.  */
2956                         struct jffs_fm *dirty_fm
2957                         = jffs_fmalloced(fmc,
2958                                          fmc->tail->offset + fmc->tail->size,
2959                                          free_chunk_size1, NULL);
2960                         if (!dirty_fm) {
2961                                 printk(KERN_ERR "JFFS: "
2962                                        "jffs_garbage_collect_next: "
2963                                        "Failed to allocate `dirty' "
2964                                        "flash memory!\n");
2965                                 err = -1;
2966                                 goto jffs_garbage_collect_next_end;
2967                         }
2968                         D1(printk("Dirtying end of flash - too small\n"));
2969                         jffs_write_dummy_node(c, dirty_fm);
2970                         err = 0;
2971                         goto jffs_garbage_collect_next_end;
2972                 }
2973                 D1(printk("Reducing size of new node from %d to %d to avoid "
2974                           " exceeding free_chunk_size1\n",
2975                           size, free_chunk_size1));
2976
2977                 size = free_chunk_size1;
2978                 data_size = size - sizeof(struct jffs_raw_inode)
2979                             - total_name_size;
2980         }
2981
2982
2983         /* Calculate the amount of space needed to hold the nodes
2984            which are remaining in the tail */
2985         space_needed = fmc->min_free_size - (node->fm->offset % fmc->sector_size);
2986
2987         /* From that, calculate how much 'extra' space we can use to
2988            increase the size of the node we're writing from the size
2989            of the node we're obsoleting
2990         */
2991         if (space_needed > fmc->free_size) {
2992                 /* If we've gone below min_free_size for some reason,
2993                    don't fuck up. This is why we have 
2994                    min_free_size > sector_size. Whinge about it though,
2995                    just so I can convince myself my maths is right.
2996                 */
2997                 D1(printk(KERN_WARNING "jffs_garbage_collect_next(): "
2998                           "space_needed %d exceeded free_size %d\n",
2999                           space_needed, fmc->free_size));
3000                 extra_available = 0;
3001         } else {
3002                 extra_available = fmc->free_size - space_needed;
3003         }
3004
3005         /* Check that we don't use up any more 'extra' space than
3006            what's available */
3007         if (size > JFFS_PAD(node->data_size) + total_name_size + 
3008             sizeof(struct jffs_raw_inode) + extra_available) {
3009                 D1(printk("Reducing size of new node from %d to %ld to avoid "
3010                        "catching our tail\n", size, 
3011                           (long) (JFFS_PAD(node->data_size) + JFFS_PAD(node->name_size) + 
3012                           sizeof(struct jffs_raw_inode) + extra_available)));
3013                 D1(printk("space_needed = %d, extra_available = %d\n", 
3014                           space_needed, extra_available));
3015
3016                 size = JFFS_PAD(node->data_size) + total_name_size + 
3017                   sizeof(struct jffs_raw_inode) + extra_available;
3018                 data_size = size - sizeof(struct jffs_raw_inode)
3019                         - total_name_size;
3020         };
3021
3022         D2(printk("  total_name_size: %u\n", total_name_size));
3023         D2(printk("  data_size: %u\n", data_size));
3024         D2(printk("  size: %u\n", size));
3025         D2(printk("  f->nsize: %u\n", f->nsize));
3026         D2(printk("  f->size: %u\n", f->size));
3027         D2(printk("  node->data_offset: %u\n", node->data_offset));
3028         D2(printk("  free_chunk_size1: %u\n", free_chunk_size1));
3029         D2(printk("  free_chunk_size2: %u\n", free_chunk_size2));
3030         D2(printk("  node->fm->offset: 0x%08x\n", node->fm->offset));
3031
3032         if ((err = jffs_rewrite_data(f, node, data_size))) {
3033                 printk(KERN_WARNING "jffs_rewrite_data() failed: %d\n", err);
3034                 return err;
3035         }
3036           
3037 jffs_garbage_collect_next_end:
3038         D3(printk("jffs_garbage_collect_next: Leaving...\n"));
3039         return err;
3040 } /* jffs_garbage_collect_next */
3041
3042
3043 /* If an obsolete node is partly going to be erased due to garbage
3044    collection, the part that isn't going to be erased must be filled
3045    with zeroes so that the scan of the flash will work smoothly next
3046    time.  (The data in the file could for instance be a JFFS image
3047    which could cause enormous confusion during a scan of the flash
3048    device if we didn't do this.)
3049      There are two phases in this procedure: First, the clearing of
3050    the name and data parts of the node. Second, possibly also clearing
3051    a part of the raw inode as well.  If the box is power cycled during
3052    the first phase, only the checksum of this node-to-be-cleared-at-
3053    the-end will be wrong.  If the box is power cycled during, or after,
3054    the clearing of the raw inode, the information like the length of
3055    the name and data parts are zeroed.  The next time the box is
3056    powered up, the scanning algorithm manages this faulty data too
3057    because:
3058
3059    - The checksum is invalid and thus the raw inode must be discarded
3060      in any case.
3061    - If the lengths of the data part or the name part are zeroed, the
3062      scanning just continues after the raw inode.  But after the inode
3063      the scanning procedure just finds zeroes which is the same as
3064      dirt.
3065
3066    So, in the end, this could never fail. :-)  Even if it does fail,
3067    the scanning algorithm should manage that too.  */
3068
3069 static int
3070 jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
3071 {
3072         struct jffs_fm *fm;
3073         struct jffs_fmcontrol *fmc = c->fmc;
3074         __u32 zero_offset;
3075         __u32 zero_size;
3076         __u32 zero_offset_data;
3077         __u32 zero_size_data;
3078         __u32 cutting_raw_inode = 0;
3079
3080         if (!(fm = jffs_cut_node(fmc, erase_size))) {
3081                 D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3082                 return 0;
3083         }
3084
3085         /* Where and how much shall we clear?  */
3086         zero_offset = fmc->head->offset + erase_size;
3087         zero_size = fm->offset + fm->size - zero_offset;
3088
3089         /* Do we have to clear the raw_inode explicitly?  */
3090         if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
3091                 cutting_raw_inode = sizeof(struct jffs_raw_inode)
3092                                     - (fm->size - zero_size);
3093         }
3094
3095         /* First, clear the name and data fields.  */
3096         zero_offset_data = zero_offset + cutting_raw_inode;
3097         zero_size_data = zero_size - cutting_raw_inode;
3098         flash_safe_acquire(fmc->mtd);
3099         flash_memset(fmc->mtd, zero_offset_data, 0, zero_size_data);
3100         flash_safe_release(fmc->mtd);
3101
3102         /* Should we clear a part of the raw inode?  */
3103         if (cutting_raw_inode) {
3104                 /* I guess it is ok to clear the raw inode in this order.  */
3105                 flash_safe_acquire(fmc->mtd);
3106                 flash_memset(fmc->mtd, zero_offset, 0,
3107                              cutting_raw_inode);
3108                 flash_safe_release(fmc->mtd);
3109         }
3110
3111         return 0;
3112 } /* jffs_clear_end_of_node()  */
3113
3114 /* Try to erase as much as possible of the dirt in the flash memory.  */
3115 static long
3116 jffs_try_to_erase(struct jffs_control *c)
3117 {
3118         struct jffs_fmcontrol *fmc = c->fmc;
3119         long erase_size;
3120         int err;
3121         __u32 offset;
3122
3123         D3(printk("jffs_try_to_erase()\n"));
3124
3125         erase_size = jffs_erasable_size(fmc);
3126
3127         D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
3128
3129         if (erase_size == 0) {
3130                 return 0;
3131         }
3132         else if (erase_size < 0) {
3133                 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3134                        "jffs_erasable_size returned %ld.\n", erase_size);
3135                 return erase_size;
3136         }
3137
3138         if ((err = jffs_clear_end_of_node(c, erase_size)) < 0) {
3139                 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3140                        "Clearing of node failed.\n");
3141                 return err;
3142         }
3143
3144         offset = fmc->head->offset;
3145
3146         /* Now, let's try to do the erase.  */
3147         if ((err = flash_erase_region(fmc->mtd,
3148                                       offset, erase_size)) < 0) {
3149                 printk(KERN_ERR "JFFS: Erase of flash failed. "
3150                        "offset = %u, erase_size = %ld\n",
3151                        offset, erase_size);
3152                 /* XXX: Here we should allocate this area as dirty
3153                    with jffs_fmalloced or something similar.  Now
3154                    we just report the error.  */
3155                 return err;
3156         }
3157
3158 #if 0
3159         /* Check if the erased sectors really got erased.  */
3160         {
3161                 __u32 pos;
3162                 __u32 end;
3163
3164                 pos = (__u32)flash_get_direct_pointer(to_kdev_t(c->sb->s_dev), offset);
3165                 end = pos + erase_size;
3166
3167                 D2(printk("JFFS: Checking erased sector(s)...\n"));
3168
3169                 flash_safe_acquire(fmc->mtd);
3170
3171                 for (; pos < end; pos += 4) {
3172                         if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
3173                                 printk("JFFS: Erase failed! pos = 0x%lx\n",
3174                                        (long)pos);
3175                                 jffs_hexdump(fmc->mtd, pos,
3176                                              jffs_min(256, end - pos));
3177                                 err = -1;
3178                                 break;
3179                         }
3180                 }
3181
3182                 flash_safe_release(fmc->mtd);
3183
3184                 if (!err) {
3185                         D2(printk("JFFS: Erase succeeded.\n"));
3186                 }
3187                 else {
3188                         /* XXX: Here we should allocate the memory
3189                            with jffs_fmalloced() in order to prevent
3190                            JFFS from using this area accidentally.  */
3191                         return err;
3192                 }
3193         }
3194 #endif
3195
3196         /* Update the flash memory data structures.  */
3197         jffs_sync_erase(fmc, erase_size);
3198
3199         return erase_size;
3200 }
3201
3202
3203 /* There are different criteria that should trigger a garbage collect:
3204
3205    1. There is too much dirt in the memory.
3206    2. The free space is becoming small.
3207    3. There are many versions of a node.
3208
3209    The garbage collect should always be done in a manner that guarantees
3210    that future garbage collects cannot be locked.  E.g. Rewritten chunks
3211    should not be too large (span more than one sector in the flash memory
3212    for exemple).  Of course there is a limit on how intelligent this garbage
3213    collection can be.  */
3214
3215
3216 static int
3217 jffs_garbage_collect_now(struct jffs_control *c)
3218 {
3219         struct jffs_fmcontrol *fmc = c->fmc;
3220         long erased = 0;
3221         int result = 0;
3222         D1(int i = 1);
3223         D2(printk("***jffs_garbage_collect_now(): fmc->dirty_size = %u, fmc->free_size = 0x%x\n, fcs1=0x%x, fcs2=0x%x",
3224                   fmc->dirty_size, fmc->free_size, jffs_free_size1(fmc), jffs_free_size2(fmc)));
3225         D2(jffs_print_fmcontrol(fmc));
3226
3227         //      down(&fmc->gclock);
3228
3229         /* If it is possible to garbage collect, do so.  */
3230         
3231         while (erased == 0) {
3232                 D1(printk("***jffs_garbage_collect_now(): round #%u, "
3233                           "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3234                 D2(jffs_print_fmcontrol(fmc));
3235
3236                 if ((erased = jffs_try_to_erase(c)) < 0) {
3237                         printk(KERN_WARNING "JFFS: Error in "
3238                                "garbage collector.\n");
3239                         result = erased;
3240                         goto gc_end;
3241                 }
3242                 if (erased)
3243                         break;
3244                 
3245                 if (fmc->free_size == 0) {
3246                         /* Argh */
3247                         printk(KERN_ERR "jffs_garbage_collect_now(): free_size == 0. This is BAD.\n");
3248                         result = -ENOSPC;
3249                         break;
3250                 }
3251
3252                 if (fmc->dirty_size < fmc->sector_size) {
3253                         /* Actually, we _may_ have been able to free some, 
3254                          * if there are many overlapping nodes which aren't
3255                          * actually marked dirty because they still have
3256                          * some valid data in each.
3257                          */
3258                         result = -ENOSPC;
3259                         break;
3260                 }
3261
3262                 /* Let's dare to make a garbage collect.  */
3263                 if ((result = jffs_garbage_collect_next(c)) < 0) {
3264                         printk(KERN_ERR "JFFS: Something "
3265                                "has gone seriously wrong "
3266                                "with a garbage collect.\n");
3267                         goto gc_end;
3268                 }
3269
3270                 D1(printk("   jffs_garbage_collect_now(): erased: %ld\n", erased));
3271                 DJM(jffs_print_memory_allocation_statistics());
3272         }
3273         
3274 gc_end:
3275         //      up(&fmc->gclock);
3276
3277         D3(printk("   jffs_garbage_collect_now(): Leaving...\n"));
3278         D1(if (erased) {
3279                 printk("jffs_g_c_now(): erased = %ld\n", erased);
3280                 jffs_print_fmcontrol(fmc);
3281         });
3282
3283         if (!erased && !result)
3284                 return -ENOSPC;
3285
3286         return result;
3287 } /* jffs_garbage_collect_now() */
3288
3289
3290 /* Determine if it is reasonable to start garbage collection.
3291    We start a gc pass if either:
3292    - The number of free bytes < MIN_FREE_BYTES && at least one
3293      block is dirty, OR
3294    - The number of dirty bytes > MAX_DIRTY_BYTES
3295 */
3296 static inline int thread_should_wake (struct jffs_control *c)
3297 {
3298         D1(printk (KERN_NOTICE "thread_should_wake(): free=%d, dirty=%d, blocksize=%d.\n",
3299                    c->fmc->free_size, c->fmc->dirty_size, c->fmc->sector_size));
3300
3301         /* If there's not enough dirty space to free a block, there's no point. */
3302         if (c->fmc->dirty_size < c->fmc->sector_size) {
3303                 D2(printk(KERN_NOTICE "thread_should_wake(): Not waking. Insufficient dirty space\n"));
3304                 return 0;
3305         }
3306 #if 1
3307         /* If there is too much RAM used by the various structures, GC */
3308         if (jffs_get_node_inuse() > (c->fmc->used_size/c->fmc->max_chunk_size * 5 + jffs_get_file_count() * 2 + 50)) {
3309                 /* FIXME: Provide proof that this test can be satisfied. We
3310                    don't want a filesystem doing endless GC just because this
3311                    condition cannot ever be false.
3312                 */
3313                 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to number of nodes\n"));
3314                 return 1;
3315         }
3316 #endif
3317         /* If there are fewer free bytes than the threshold, GC */
3318         if (c->fmc->free_size < c->gc_minfree_threshold) {
3319                 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to insufficent free space\n"));
3320                 return 1;
3321         }
3322         /* If there are more dirty bytes than the threshold, GC */
3323         if (c->fmc->dirty_size > c->gc_maxdirty_threshold) {
3324                 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to excessive dirty space\n"));
3325                 return 1;
3326         }       
3327         /* FIXME: What about the "There are many versions of a node" condition? */
3328
3329         return 0;
3330 }
3331
3332
3333 void jffs_garbage_collect_trigger(struct jffs_control *c)
3334 {
3335         /* NOTE: We rely on the fact that we have the BKL here.
3336          * Otherwise, the gc_task could go away between the check
3337          * and the wake_up_process()
3338          */
3339         if (c->gc_task && thread_should_wake(c))
3340                 send_sig(SIGHUP, c->gc_task, 1);
3341 }
3342   
3343
3344 /* Kernel threads  take (void *) as arguments.   Thus we pass
3345    the jffs_control data as a (void *) and then cast it. */
3346 int
3347 jffs_garbage_collect_thread(void *ptr)
3348 {
3349         struct jffs_control *c = (struct jffs_control *) ptr;
3350         struct jffs_fmcontrol *fmc = c->fmc;
3351         long erased;
3352         int result = 0;
3353         D1(int i = 1);
3354
3355         daemonize("jffs_gcd");
3356
3357         c->gc_task = current;
3358
3359         lock_kernel();
3360         init_completion(&c->gc_thread_comp); /* barrier */ 
3361         spin_lock_irq(&current->sighand->siglock);
3362         siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
3363         recalc_sigpending();
3364         spin_unlock_irq(&current->sighand->siglock);
3365
3366         D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): Starting infinite loop.\n"));
3367
3368         for (;;) {
3369
3370                 /* See if we need to start gc.  If we don't, go to sleep.
3371                    
3372                    Current implementation is a BAD THING(tm).  If we try 
3373                    to unmount the FS, the unmount operation will sleep waiting
3374                    for this thread to exit.  We need to arrange to send it a
3375                    sig before the umount process sleeps.
3376                 */
3377
3378                 if (!thread_should_wake(c))
3379                         set_current_state (TASK_INTERRUPTIBLE);
3380                 
3381                 schedule(); /* Yes, we do this even if we want to go
3382                                        on immediately - we're a low priority 
3383                                        background task. */
3384
3385                 /* Put_super will send a SIGKILL and then wait on the sem. 
3386                  */
3387                 while (signal_pending(current)) {
3388                         siginfo_t info;
3389                         unsigned long signr = 0;
3390
3391                         if (try_to_freeze())
3392                                 continue;
3393
3394                         spin_lock_irq(&current->sighand->siglock);
3395                         signr = dequeue_signal(current, &current->blocked, &info);
3396                         spin_unlock_irq(&current->sighand->siglock);
3397
3398                         switch(signr) {
3399                         case SIGSTOP:
3400                                 D1(printk("jffs_garbage_collect_thread(): SIGSTOP received.\n"));
3401                                 set_current_state(TASK_STOPPED);
3402                                 schedule();
3403                                 break;
3404
3405                         case SIGKILL:
3406                                 D1(printk("jffs_garbage_collect_thread(): SIGKILL received.\n"));
3407                                 c->gc_task = NULL;
3408                                 complete_and_exit(&c->gc_thread_comp, 0);
3409                         }
3410                 }
3411
3412
3413                 D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): collecting.\n"));
3414
3415                 D3(printk (KERN_NOTICE "g_c_thread(): down biglock\n"));
3416                 mutex_lock(&fmc->biglock);
3417                 
3418                 D1(printk("***jffs_garbage_collect_thread(): round #%u, "
3419                           "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3420                 D2(jffs_print_fmcontrol(fmc));
3421
3422                 if ((erased = jffs_try_to_erase(c)) < 0) {
3423                         printk(KERN_WARNING "JFFS: Error in "
3424                                "garbage collector: %ld.\n", erased);
3425                 }
3426
3427                 if (erased)
3428                         goto gc_end;
3429
3430                 if (fmc->free_size == 0) {
3431                         /* Argh. Might as well commit suicide. */
3432                         printk(KERN_ERR "jffs_garbage_collect_thread(): free_size == 0. This is BAD.\n");
3433                         send_sig(SIGQUIT, c->gc_task, 1);
3434                         // panic()
3435                         goto gc_end;
3436                 }
3437                 
3438                 /* Let's dare to make a garbage collect.  */
3439                 if ((result = jffs_garbage_collect_next(c)) < 0) {
3440                         printk(KERN_ERR "JFFS: Something "
3441                                "has gone seriously wrong "
3442                                "with a garbage collect: %d\n", result);
3443                 }
3444                 
3445         gc_end:
3446                 D3(printk (KERN_NOTICE "g_c_thread(): up biglock\n"));
3447                 mutex_unlock(&fmc->biglock);
3448         } /* for (;;) */
3449 } /* jffs_garbage_collect_thread() */