Import 2.4.0-test3pre5
[davej-history.git] / fs / jffs / intrep.c
blob039aaa04e854223f5acc3fb0d1a909b2856a1acc
1 /*
2 * JFFS -- Journaling Flash File System, Linux implementation.
4 * Copyright (C) 1999, 2000 Axis Communications, Inc.
6 * Created by Finn Hakansson <finn@axis.com>.
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.
13 * $Id: intrep.c,v 1.15 2000/06/27 15:33:43 dwmw2 Exp $
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
20 /* This file contains the code for the internal structure of the
21 Journaling Flash File System, JFFS. */
24 * Todo list:
26 * memcpy_to_flash() and memcpy_from_flash()-functions.
28 * Implementation of hard links.
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.
34 * A better checksum algorithm.
36 * Consider endianness stuff. ntohl() etc.
38 * Are we handling the atime, mtime, ctime members of the inode right?
40 * Remove some duplicated code. Take a look at jffs_write_node() and
41 * jffs_rewrite_data() for instance.
43 * Implement more meaning of the nlink member in various data structures.
44 * nlink could be used in conjunction with hard links for instance.
46 * Fix the rename stuff. (I.e. if we have two files `a' and `b' and we
47 * do a `mv b a'.) Half of this is already implemented.
49 * Better memory management. Allocate data structures in larger chunks
50 * if possible.
52 * If too much meta data is stored, a garbage collect should be issued.
53 * We have experienced problems with too much meta data with for instance
54 * log files.
56 * Improve the calls to jffs_ioctl(). We would like to retrieve more
57 * information to be able to debug (or to supervise) JFFS during run-time.
60 #define __NO_VERSION__
61 #include <linux/config.h>
62 #include <linux/types.h>
63 #include <linux/malloc.h>
64 #include <linux/jffs.h>
65 #include <linux/fs.h>
66 #include <linux/stat.h>
67 #include <linux/pagemap.h>
68 #include <linux/locks.h>
69 #include <asm/semaphore.h>
70 #include <asm/byteorder.h>
71 #include <linux/version.h>
73 #include "intrep.h"
74 #include "jffs_fm.h"
76 #if LINUX_VERSION_CODE < 0x20300
77 #define set_current_state(x) do{current->state = x;} while (0)
78 #endif
80 #if defined(CONFIG_JFFS_FS_VERBOSE) && CONFIG_JFFS_FS_VERBOSE
81 #define D(x) x
82 #else
83 #define D(x)
84 #endif
85 #define D1(x) D(x)
86 #define D2(x)
87 #define D3(x)
88 #define ASSERT(x) x
90 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
91 long no_jffs_file = 0;
92 long no_jffs_node = 0;
93 long no_jffs_control = 0;
94 long no_jffs_raw_inode = 0;
95 long no_jffs_node_ref = 0;
96 long no_jffs_fm = 0;
97 long no_jffs_fmcontrol = 0;
98 long no_hash = 0;
99 long no_name = 0;
100 #endif
102 static int jffs_scan_flash(struct jffs_control *c);
103 static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
104 static __u8 flash_read_u8(struct mtd_info *mtd, loff_t from);
106 #if 1
107 #define _U 01
108 #define _L 02
109 #define _N 04
110 #define _S 010
111 #define _P 020
112 #define _C 040
113 #define _X 0100
114 #define _B 0200
116 const unsigned char jffs_ctype_[1 + 256] = {
118 _C, _C, _C, _C, _C, _C, _C, _C,
119 _C, _C|_S, _C|_S, _C|_S, _C|_S, _C|_S, _C, _C,
120 _C, _C, _C, _C, _C, _C, _C, _C,
121 _C, _C, _C, _C, _C, _C, _C, _C,
122 _S|_B, _P, _P, _P, _P, _P, _P, _P,
123 _P, _P, _P, _P, _P, _P, _P, _P,
124 _N, _N, _N, _N, _N, _N, _N, _N,
125 _N, _N, _P, _P, _P, _P, _P, _P,
126 _P, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U,
127 _U, _U, _U, _U, _U, _U, _U, _U,
128 _U, _U, _U, _U, _U, _U, _U, _U,
129 _U, _U, _U, _P, _P, _P, _P, _P,
130 _P, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L,
131 _L, _L, _L, _L, _L, _L, _L, _L,
132 _L, _L, _L, _L, _L, _L, _L, _L,
133 _L, _L, _L, _P, _P, _P, _P, _C
136 #define jffs_isalpha(c) ((jffs_ctype_+1)[(int)c]&(_U|_L))
137 #define jffs_isupper(c) ((jffs_ctype_+1)[(int)c]&_U)
138 #define jffs_islower(c) ((jffs_ctype_+1)[(int)c]&_L)
139 #define jffs_isdigit(c) ((jffs_ctype_+1)[(int)c]&_N)
140 #define jffs_isxdigit(c) ((jffs_ctype_+1)[(int)c]&(_X|_N))
141 #define jffs_isspace(c) ((jffs_ctype_+1)[(int)c]&_S)
142 #define jffs_ispunct(c) ((jffs_ctype_+1)[(int)c]&_P)
143 #define jffs_isalnum(c) ((jffs_ctype_+1)[(int)c]&(_U|_L|_N))
144 #define jffs_isprint(c) ((jffs_ctype_+1)[(int)c]&(_P|_U|_L|_N|_B))
145 #define jffs_isgraph(c) ((jffs_ctype_+1)[(int)c]&(_P|_U|_L|_N))
146 #define jffs_iscntrl(c) ((jffs_ctype_+1)[(int)c]&_C)
148 void
149 jffs_hexdump(struct mtd_info *mtd, loff_t pos, int size)
151 char line[16];
152 int j = 0;
154 while (size > 0) {
155 int i;
157 printk("%ld:", (long) pos);
158 for (j = 0; j < 16; j++) {
159 line[j] = flash_read_u8(mtd, pos++);
161 for (i = 0; i < j; i++) {
162 if (!(i & 1)) {
163 printk(" %.2x", line[i] & 0xff);
165 else {
166 printk("%.2x", line[i] & 0xff);
170 /* Print empty space */
171 for (; i < 16; i++) {
172 if (!(i & 1)) {
173 printk(" ");
175 else {
176 printk(" ");
179 printk(" ");
181 for (i = 0; i < j; i++) {
182 if (jffs_isgraph(line[i])) {
183 printk("%c", line[i]);
185 else {
186 printk(".");
189 printk("\n");
190 size -= 16;
193 #endif
195 #define flash_safe_acquire(arg)
196 #define flash_safe_release(arg)
198 static int
199 flash_safe_read(struct mtd_info *mtd, loff_t from,
200 u_char *buf, size_t count)
202 size_t retlen;
204 MTD_READ(mtd, from, count, &retlen, buf);
205 if (retlen != count) {
206 printk("Didn't read all bytes in flash_safe_read()\n");
208 return retlen;
211 static __u32
212 flash_read_u32(struct mtd_info *mtd, loff_t from)
214 size_t retlen;
215 __u32 ret;
217 MTD_READ(mtd, from, 4, &retlen, (unsigned char *)&ret);
218 if (retlen != 4) {
219 printk("Didn't read all bytes in flash_read_u32()\n");
220 return 0;
223 return ret;
226 static __u8
227 flash_read_u8(struct mtd_info *mtd, loff_t from)
229 size_t retlen;
230 __u8 ret;
232 MTD_READ(mtd, from, 1, &retlen, &ret);
233 if (retlen != 1) {
234 printk("Didn't read all bytes in flash_read_u32()\n");
235 return 0;
238 return ret;
242 static int
243 flash_safe_write(struct mtd_info *mtd, loff_t to,
244 const u_char *buf, size_t count)
246 size_t retlen;
248 MTD_WRITE(mtd, to, count, &retlen, buf);
249 if (retlen != count) {
250 printk("Didn't write all bytes in flash_safe_write()\n");
252 return retlen;
255 static int
256 flash_memset(struct mtd_info *mtd, loff_t to,
257 const u_char c, size_t size)
259 static unsigned char pattern[16];
260 int i;
262 /* fill up pattern */
264 for(i = 0; i < 16; i++)
265 pattern[i] = c;
267 /* write as many 16-byte chunks as we can */
269 while(size >= 16) {
270 flash_safe_write(mtd, to, pattern, 16);
271 size -= 16;
272 to += 16;
275 /* and the rest */
277 if(size)
278 flash_safe_write(mtd, to, pattern, size);
280 return size;
283 static void intrep_erase_callback(struct erase_info *done)
285 wait_queue_head_t *wait_q;
287 wait_q = (wait_queue_head_t *)done->priv;
289 wake_up(wait_q);
292 static int
293 flash_erase_region(struct mtd_info *mtd, loff_t start,
294 size_t size)
296 struct erase_info *erase;
297 DECLARE_WAITQUEUE(wait, current);
298 wait_queue_head_t wait_q;
300 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
301 if (!erase)
302 return -ENOMEM;
304 init_waitqueue_head(&wait_q);
306 erase->mtd = mtd;
307 erase->callback = intrep_erase_callback;
308 erase->addr = start;
309 erase->len = size;
310 erase->priv = (u_long)&wait_q;
312 set_current_state(TASK_INTERRUPTIBLE);
313 add_wait_queue(&wait_q, &wait);
315 if (MTD_ERASE(mtd, erase) < 0) {
316 set_current_state(TASK_RUNNING);
317 remove_wait_queue(&wait_q, &wait);
318 kfree(erase);
320 printk(KERN_WARNING "flash: erase of region [0x%ld, 0x%ld] totally failed\n",
321 (long)start, (long)start + size);
323 return -1;
326 schedule(); /* Wait for flash to finish. */
327 /* FIXME: We could have been interrupted here. We don't deal with it */
328 remove_wait_queue(&wait_q, &wait);
330 kfree(erase);
332 return 0;
335 inline int
336 jffs_min(int a, int b)
338 return (a < b ? a : b);
342 inline int
343 jffs_max(int a, int b)
345 return (a > b ? a : b);
349 /* This routine calculates checksums in JFFS. */
350 __u32
351 jffs_checksum(const void *data, int size)
353 __u32 sum = 0;
354 __u8 *ptr = (__u8 *)data;
355 while (size-- > 0) {
356 sum += *ptr++;
358 D3(printk(", result: 0x%08x\n", sum));
359 return sum;
362 __u32
363 jffs_checksum_flash(struct mtd_info *mtd, loff_t start, int size)
365 __u32 sum = 0;
366 loff_t ptr = start;
367 while (size-- > 0) {
368 sum += flash_read_u8(mtd, ptr++);
370 D3(printk("checksum result: 0x%08x\n", sum));
371 return sum;
374 /* Create and initialize a new struct jffs_file. */
375 static struct jffs_file *
376 jffs_create_file(struct jffs_control *c,
377 const struct jffs_raw_inode *raw_inode)
379 struct jffs_file *f;
381 if (!(f = (struct jffs_file *)kmalloc(sizeof(struct jffs_file),
382 GFP_KERNEL))) {
383 D(printk("jffs_create_file(): Failed!\n"));
384 return 0;
386 DJM(no_jffs_file++);
387 memset(f, 0, sizeof(struct jffs_file));
388 f->ino = raw_inode->ino;
389 f->pino = raw_inode->pino;
390 f->nlink = raw_inode->nlink;
391 f->deleted = raw_inode->deleted;
392 f->c = c;
394 return f;
398 /* Build a control block for the file system. */
399 static struct jffs_control *
400 jffs_create_control(kdev_t dev)
402 struct jffs_control *c;
403 register int s = sizeof(struct jffs_control);
404 int i;
405 D(char *t = 0);
407 D2(printk("jffs_create_control()\n"));
409 if (!(c = (struct jffs_control *)kmalloc(s, GFP_KERNEL))) {
410 goto fail_control;
412 DJM(no_jffs_control++);
413 c->root = 0;
414 c->hash_len = JFFS_HASH_SIZE;
415 s = sizeof(struct list_head) * c->hash_len;
416 if (!(c->hash = (struct list_head *)kmalloc(s, GFP_KERNEL))) {
417 goto fail_hash;
419 DJM(no_hash++);
420 for (i=0;i<c->hash_len;i++)
421 INIT_LIST_HEAD(&c->hash[i]);
422 if (!(c->fmc = jffs_build_begin(c, dev))) {
423 goto fail_fminit;
425 c->next_ino = JFFS_MIN_INO + 1;
426 return c;
428 fail_fminit:
429 D(t = "c->fmc");
430 fail_hash:
431 kfree(c);
432 DJM(no_jffs_control--);
433 D(t = t ? t : "c->hash");
434 fail_control:
435 D(t = t ? t : "control");
436 D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
437 return (struct jffs_control *)0;
441 /* Clean up all data structures associated with the file system. */
442 void
443 jffs_cleanup_control(struct jffs_control *c)
445 D2(printk("jffs_cleanup_control()\n"));
447 if (!c) {
448 D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
449 return;
452 /* Free all files and nodes. */
453 if (c->hash) {
454 jffs_foreach_file(c, jffs_free_node_list);
455 kfree(c->hash);
456 DJM(no_hash--);
458 jffs_cleanup_fmcontrol(c->fmc);
459 kfree(c);
460 DJM(no_jffs_control--);
461 D3(printk("jffs_cleanup_control(): Leaving...\n"));
465 /* This function adds a virtual root node to the in-RAM representation.
466 Called by jffs_build_fs(). */
467 static int
468 jffs_add_virtual_root(struct jffs_control *c)
470 struct jffs_file *root;
471 struct jffs_node *node;
473 D2(printk("jffs_add_virtual_root(): "
474 "Creating a virtual root directory.\n"));
476 if (!(root = (struct jffs_file *)kmalloc(sizeof(struct jffs_file),
477 GFP_KERNEL))) {
478 return -ENOMEM;
480 DJM(no_jffs_file++);
481 if (!(node = (struct jffs_node *)kmalloc(sizeof(struct jffs_node),
482 GFP_KERNEL))) {
483 kfree(root);
484 DJM(no_jffs_file--);
485 return -ENOMEM;
487 DJM(no_jffs_node++);
488 memset(node, 0, sizeof(struct jffs_node));
489 node->ino = JFFS_MIN_INO;
490 memset(root, 0, sizeof(struct jffs_file));
491 root->ino = JFFS_MIN_INO;
492 root->mode = S_IFDIR | S_IRWXU | S_IRGRP
493 | S_IXGRP | S_IROTH | S_IXOTH;
494 root->atime = root->mtime = root->ctime = CURRENT_TIME;
495 root->nlink = 1;
496 root->c = c;
497 root->version_head = root->version_tail = node;
498 jffs_insert_file_into_hash(root);
499 return 0;
503 /* This is where the file system is built and initialized. */
505 jffs_build_fs(struct super_block *sb)
507 struct jffs_control *c;
508 int err = 0;
510 D2(printk("jffs_build_fs()\n"));
512 if (!(c = jffs_create_control(sb->s_dev))) {
513 return -ENOMEM;
515 c->building_fs = 1;
516 c->sb = sb;
517 if ((err = jffs_scan_flash(c)) < 0) {
518 goto jffs_build_fs_fail;
521 /* Add a virtual root node if no one exists. */
522 if (!jffs_find_file(c, JFFS_MIN_INO)) {
523 if ((err = jffs_add_virtual_root(c)) < 0) {
524 goto jffs_build_fs_fail;
528 /* Remove deleted nodes. */
529 if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
530 printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
531 goto jffs_build_fs_fail;
533 /* Remove redundant nodes. (We are not interested in the
534 return value in this case.) */
535 jffs_foreach_file(c, jffs_remove_redundant_nodes);
536 /* Try to build a tree from all the nodes. */
537 if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
538 printk("JFFS: Failed to build tree.\n");
539 goto jffs_build_fs_fail;
541 /* Compute the sizes of all files in the filesystem. Adjust if
542 necessary. */
543 if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
544 printk("JFFS: Failed to build file system.\n");
545 goto jffs_build_fs_fail;
547 sb->u.generic_sbp = (void *)c;
548 c->building_fs = 0;
550 D1(jffs_print_hash_table(c));
551 D1(jffs_print_tree(c->root, 0));
553 return 0;
555 jffs_build_fs_fail:
556 jffs_cleanup_control(c);
557 return err;
558 } /* jffs_build_fs() */
561 /* Scan the whole flash memory in order to find all nodes in the
562 file systems. */
563 static int
564 jffs_scan_flash(struct jffs_control *c)
566 char name[JFFS_MAX_NAME_LEN + 2];
567 struct jffs_raw_inode raw_inode;
568 struct jffs_node *node = 0;
569 struct jffs_fmcontrol *fmc = c->fmc;
570 __u32 checksum;
571 __u8 tmp_accurate;
572 __u16 tmp_chksum;
573 loff_t pos = fmc->flash_start;
574 loff_t start;
575 loff_t end = fmc->flash_start + fmc->flash_size;
577 D1(printk("jffs_scan_flash(): start pos = 0x%ld, end = 0x%ld\n",
578 (long)pos, (long)end));
580 flash_safe_acquire(fmc->mtd);
582 /* Start the scan. */
583 while (pos < end) {
585 /* Remember the position from where we started this scan. */
586 start = pos;
588 switch (flash_read_u32(fmc->mtd, pos)) {
589 case JFFS_EMPTY_BITMASK:
590 /* We have found 0xff on this block. We have to
591 scan the rest of the block to be sure it is
592 filled with 0xff. */
593 D1(printk("jffs_scan_flash(): 0xff at pos 0x%ld.\n",
594 (long)pos));
595 for (; pos < end
596 && JFFS_EMPTY_BITMASK == flash_read_u32(fmc->mtd, pos);
597 pos += 4);
598 D1(printk("jffs_scan_flash(): 0xff ended at "
599 "pos 0x%ld.\n", (long)pos));
600 continue;
602 case JFFS_DIRTY_BITMASK:
603 /* We have found 0x00 on this block. We have to
604 scan as far as possible to find out how much
605 is dirty. */
606 D1(printk("jffs_scan_flash(): 0x00 at pos 0x%ld.\n",
607 (long)pos));
608 for (; pos < end
609 && JFFS_DIRTY_BITMASK == flash_read_u32(fmc->mtd, pos);
610 pos += 4);
611 D1(printk("jffs_scan_flash(): 0x00 ended at "
612 "pos 0x%ld.\n", (long)pos));
613 jffs_fmalloced(fmc, (__u32) start,
614 (__u32) (pos - start), 0);
615 continue;
617 case JFFS_MAGIC_BITMASK:
618 /* We have probably found a new raw inode. */
619 break;
621 default:
622 bad_inode:
623 /* We're f*cked. This is not solved yet. We have
624 to scan for the magic pattern. */
625 D1(printk("*************** Dirty flash memory or bad inode: "
626 "hexdump(pos = 0x%ld, len = 128):\n",
627 (long)pos));
628 D1(jffs_hexdump(fmc->mtd, pos, 128));
629 for (pos += 4; pos < end; pos += 4) {
630 switch (flash_read_u32(fmc->mtd, pos)) {
631 case JFFS_MAGIC_BITMASK:
632 jffs_fmalloced(fmc, (__u32) start,
633 (__u32) (pos - start),
635 goto cont_scan;
636 default:
637 break;
640 cont_scan:
641 continue;
644 /* We have found the beginning of an inode. Create a
645 node for it. */
646 if (!node) {
647 if (!(node = (struct jffs_node *)
648 kmalloc(sizeof(struct jffs_node),
649 GFP_KERNEL))) {
650 flash_safe_release(fmc->mtd);
651 return -ENOMEM;
653 DJM(no_jffs_node++);
656 /* Read the next raw inode. */
658 flash_safe_read(fmc->mtd, pos, (u_char *) &raw_inode, sizeof(struct jffs_raw_inode));
660 /* When we compute the checksum for the inode, we never
661 count the 'accurate' or the 'checksum' fields. */
662 tmp_accurate = raw_inode.accurate;
663 tmp_chksum = raw_inode.chksum;
664 raw_inode.accurate = 0;
665 raw_inode.chksum = 0;
666 checksum = jffs_checksum(&raw_inode,
667 sizeof(struct jffs_raw_inode));
668 raw_inode.accurate = tmp_accurate;
669 raw_inode.chksum = tmp_chksum;
671 D3(printk("*** We have found this raw inode at pos 0x%ld "
672 "on the flash:\n", (long)pos));
673 D3(jffs_print_raw_inode(&raw_inode));
675 if (checksum != raw_inode.chksum) {
676 D1(printk("jffs_scan_flash(): Bad checksum: "
677 "checksum = %u, "
678 "raw_inode.chksum = %u\n",
679 checksum, raw_inode.chksum));
680 pos += sizeof(struct jffs_raw_inode);
681 jffs_fmalloced(fmc, (__u32) start,
682 (__u32) (pos - start), 0);
683 /* Reuse this unused struct jffs_node. */
684 continue;
687 /* Check the raw inode read so far. Start with the
688 maximum length of the filename. */
689 if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
690 goto bad_inode;
692 /* The node's data segment should not exceed a
693 certain length. */
694 if (raw_inode.dsize > fmc->max_chunk_size) {
695 goto bad_inode;
698 pos += sizeof(struct jffs_raw_inode);
700 /* This shouldn't be necessary because a node that
701 violates the flash boundaries shouldn't be written
702 in the first place. */
703 if (pos >= end) {
704 goto check_node;
707 /* Read the name. */
708 *name = 0;
709 if (raw_inode.nsize) {
710 flash_safe_read(fmc->mtd, pos, name, raw_inode.nsize);
711 name[raw_inode.nsize] = '\0';
712 pos += raw_inode.nsize
713 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
714 D3(printk("name == \"%s\"\n", name));
715 checksum = jffs_checksum(name, raw_inode.nsize);
716 if (checksum != raw_inode.nchksum) {
717 D1(printk("jffs_scan_flash(): Bad checksum: "
718 "checksum = %u, "
719 "raw_inode.nchksum = %u\n",
720 checksum, raw_inode.nchksum));
721 jffs_fmalloced(fmc, (__u32) start,
722 (__u32) (pos - start), 0);
723 /* Reuse this unused struct jffs_node. */
724 continue;
726 if (pos >= end) {
727 goto check_node;
731 /* Read the data in order to be sure it matches the
732 checksum. */
733 checksum = jffs_checksum_flash(fmc->mtd, pos, raw_inode.dsize);
734 pos += raw_inode.dsize + JFFS_GET_PAD_BYTES(raw_inode.dsize);
736 if (checksum != raw_inode.dchksum) {
737 D1(printk("jffs_scan_flash(): Bad checksum: "
738 "checksum = %u, "
739 "raw_inode.dchksum = %u\n",
740 checksum, raw_inode.dchksum));
741 jffs_fmalloced(fmc, (__u32) start,
742 (__u32) (pos - start), 0);
743 /* Reuse this unused struct jffs_node. */
744 continue;
747 check_node:
749 /* Remember the highest inode number in the whole file
750 system. This information will be used when assigning
751 new files new inode numbers. */
752 if (c->next_ino <= raw_inode.ino) {
753 c->next_ino = raw_inode.ino + 1;
756 if (raw_inode.accurate) {
757 int err;
758 node->data_offset = raw_inode.offset;
759 node->data_size = raw_inode.dsize;
760 node->removed_size = raw_inode.rsize;
761 /* Compute the offset to the actual data in the
762 on-flash node. */
763 node->fm_offset
764 = sizeof(struct jffs_raw_inode)
765 + raw_inode.nsize
766 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
767 node->fm = jffs_fmalloced(fmc, (__u32) start,
768 (__u32) (pos - start),
769 node);
770 if (!node->fm) {
771 D(printk("jffs_scan_flash(): !node->fm\n"));
772 kfree(node);
773 DJM(no_jffs_node--);
774 flash_safe_release(fmc->mtd);
775 return -ENOMEM;
777 if ((err = jffs_insert_node(c, 0, &raw_inode,
778 name, node)) < 0) {
779 printk("JFFS: Failed to handle raw inode. "
780 "(err = %d)\n", err);
781 break;
783 D3(jffs_print_node(node));
784 node = 0; /* Don't free the node! */
786 else {
787 jffs_fmalloced(fmc, (__u32) start,
788 (__u32) (pos - start), 0);
789 D3(printk("jffs_scan_flash(): Just found an obsolete "
790 "raw_inode. Continuing the scan...\n"));
791 /* Reuse this unused struct jffs_node. */
795 if (node) {
796 kfree(node);
797 DJM(no_jffs_node--);
799 jffs_build_end(fmc);
800 D3(printk("jffs_scan_flash(): Leaving...\n"));
801 flash_safe_release(fmc->mtd);
802 return 0;
803 } /* jffs_scan_flash() */
806 /* Insert any kind of node into the file system. Take care of data
807 insertions and deletions. Also remove redundant information. The
808 memory allocated for the `name' is regarded as "given away" in the
809 caller's perspective. */
811 jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
812 const struct jffs_raw_inode *raw_inode,
813 const char *name, struct jffs_node *node)
815 int update_name = 0;
816 int insert_into_tree = 0;
818 D2(printk("jffs_insert_node(): ino = %u, version = %u, name = \"%s\"\n",
819 raw_inode->ino, raw_inode->version,
820 ((name && *name) ? name : "")));
822 /* If there doesn't exist an associated jffs_file, then
823 create, initialize and insert one into the file system. */
824 if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
825 if (!(f = jffs_create_file(c, raw_inode))) {
826 return -ENOMEM;
828 jffs_insert_file_into_hash(f);
829 insert_into_tree = 1;
832 node->ino = raw_inode->ino;
833 node->version = raw_inode->version;
834 node->data_size = raw_inode->dsize;
835 node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
836 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
837 node->name_size = raw_inode->nsize;
839 /* Now insert the node at the correct position into the file's
840 version list. */
841 if (!f->version_head) {
842 /* This is the first node. */
843 f->version_head = node;
844 f->version_tail = node;
845 node->version_prev = 0;
846 node->version_next = 0;
847 f->highest_version = node->version;
848 update_name = 1;
849 f->mode = raw_inode->mode;
850 f->uid = raw_inode->uid;
851 f->gid = raw_inode->gid;
852 f->atime = raw_inode->atime;
853 f->mtime = raw_inode->mtime;
854 f->ctime = raw_inode->ctime;
855 f->deleted = raw_inode->deleted;
857 else if ((f->highest_version < node->version)
858 || (node->version == 0)) {
859 /* Insert at the end of the list. I.e. this node is the
860 oldest one so far. */
861 node->version_prev = f->version_tail;
862 node->version_next = 0;
863 f->version_tail->version_next = node;
864 f->version_tail = node;
865 f->highest_version = node->version;
866 update_name = 1;
867 f->pino = raw_inode->pino;
868 f->mode = raw_inode->mode;
869 f->uid = raw_inode->uid;
870 f->gid = raw_inode->gid;
871 f->atime = raw_inode->atime;
872 f->mtime = raw_inode->mtime;
873 f->ctime = raw_inode->ctime;
874 f->deleted = raw_inode->deleted;
876 else if (f->version_head->version > node->version) {
877 /* Insert at the bottom of the list. */
878 node->version_prev = 0;
879 node->version_next = f->version_head;
880 f->version_head->version_prev = node;
881 f->version_head = node;
882 if (!f->name) {
883 update_name = 1;
885 if (raw_inode->deleted) {
886 f->deleted = raw_inode->deleted;
889 else {
890 struct jffs_node *n;
891 int newer_name = 0;
892 /* Search for the insertion position starting from
893 the tail (newest node). */
894 for (n = f->version_tail; n; n = n->version_prev) {
895 if (n->version < node->version) {
896 node->version_prev = n;
897 node->version_next = n->version_next;
898 node->version_next->version_prev = node;
899 n->version_next = node;
900 if (!newer_name) {
901 update_name = 1;
903 break;
905 if (n->name_size) {
906 newer_name = 1;
911 /* Perhaps update the name. */
912 if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
913 if (f->name) {
914 kfree(f->name);
915 DJM(no_name--);
917 if (!(f->name = (char *) kmalloc(raw_inode->nsize + 1,
918 GFP_KERNEL))) {
919 return -ENOMEM;
921 DJM(no_name++);
922 memcpy(f->name, name, raw_inode->nsize);
923 f->name[raw_inode->nsize] = '\0';
924 f->nsize = raw_inode->nsize;
925 D3(printk("jffs_insert_node(): Updated the name of "
926 "the file to \"%s\".\n", name));
929 if (!c->building_fs) {
930 D3(printk("jffs_insert_node(): ---------------------------"
931 "------------------------------------------- 1\n"));
932 if (insert_into_tree) {
933 jffs_insert_file_into_tree(f);
935 if (f->deleted) {
936 /* Mark all versions of the node as obsolete. */
937 jffs_possibly_delete_file(f);
939 else {
940 if (node->data_size || node->removed_size) {
941 jffs_update_file(f, node);
943 jffs_remove_redundant_nodes(f);
945 #ifdef USE_GC
946 if (!c->fmc->no_call_gc) {
947 jffs_garbage_collect(c);
949 #endif
950 D3(printk("jffs_insert_node(): ---------------------------"
951 "------------------------------------------- 2\n"));
954 return 0;
955 } /* jffs_insert_node() */
958 /* Unlink a jffs_node from the version list it is in. */
959 static inline void
960 jffs_unlink_node_from_version_list(struct jffs_file *f,
961 struct jffs_node *node)
963 if (node->version_prev) {
964 node->version_prev->version_next = node->version_next;
965 } else {
966 f->version_head = node->version_next;
968 if (node->version_next) {
969 node->version_next->version_prev = node->version_prev;
970 } else {
971 f->version_tail = node->version_prev;
976 /* Unlink a jffs_node from the range list it is in. */
977 static inline void
978 jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
980 if (node->range_prev) {
981 node->range_prev->range_next = node->range_next;
983 else {
984 f->range_head = node->range_next;
986 if (node->range_next) {
987 node->range_next->range_prev = node->range_prev;
989 else {
990 f->range_tail = node->range_prev;
995 /* Function used by jffs_remove_redundant_nodes() below. This function
996 classifies what kind of information a node adds to a file. */
997 static inline __u8
998 jffs_classify_node(struct jffs_node *node)
1000 __u8 mod_type = JFFS_MODIFY_INODE;
1002 if (node->name_size) {
1003 mod_type |= JFFS_MODIFY_NAME;
1005 if (node->data_size || node->removed_size) {
1006 mod_type |= JFFS_MODIFY_DATA;
1008 return mod_type;
1012 /* Remove redundant nodes from a file. Mark the on-flash memory
1013 as dirty. */
1015 jffs_remove_redundant_nodes(struct jffs_file *f)
1017 struct jffs_node *newest_node;
1018 struct jffs_node *cur;
1019 struct jffs_node *prev;
1020 __u8 newest_type;
1021 __u8 mod_type;
1022 __u8 node_with_name_later = 0;
1024 if (!(newest_node = f->version_tail)) {
1025 return 0;
1028 /* What does the `newest_node' modify? */
1029 newest_type = jffs_classify_node(newest_node);
1030 node_with_name_later = newest_type & JFFS_MODIFY_NAME;
1032 D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1033 "newest_type: %u\n", f->ino, (f->name ? f->name : ""),
1034 newest_type));
1036 /* Traverse the file's nodes and determine which of them that are
1037 superfluous. Yeah, this might look very complex at first
1038 glance but it is actually very simple. */
1039 for (cur = newest_node->version_prev; cur; cur = prev) {
1040 prev = cur->version_prev;
1041 mod_type = jffs_classify_node(cur);
1042 if ((mod_type <= JFFS_MODIFY_INODE)
1043 || ((newest_type & JFFS_MODIFY_NAME)
1044 && (mod_type
1045 <= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
1046 || (cur->data_size == 0 && cur->removed_size
1047 && !cur->version_prev && node_with_name_later)) {
1048 /* Yes, this node is redundant. Remove it. */
1049 D2(printk("jffs_remove_redundant_nodes(): "
1050 "Removing node: ino: %u, version: %u, "
1051 "mod_type: %u\n", cur->ino, cur->version,
1052 mod_type));
1053 jffs_unlink_node_from_version_list(f, cur);
1054 jffs_fmfree(f->c->fmc, cur->fm, cur);
1055 kfree(cur);
1056 DJM(no_jffs_node--);
1058 else {
1059 node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
1063 return 0;
1067 /* Insert a file into the hash table. */
1069 jffs_insert_file_into_hash(struct jffs_file *f)
1071 int i = f->ino % f->c->hash_len;
1073 D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
1075 list_add(&f->hash, &f->c->hash[i]);
1076 return 0;
1080 /* Insert a file into the file system tree. */
1082 jffs_insert_file_into_tree(struct jffs_file *f)
1084 struct jffs_file *parent;
1086 D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1087 (f->name ? f->name : "")));
1089 if (!(parent = jffs_find_file(f->c, f->pino))) {
1090 if (f->pino == 0) {
1091 f->c->root = f;
1092 f->parent = 0;
1093 f->sibling_prev = 0;
1094 f->sibling_next = 0;
1095 return 0;
1097 else {
1098 D1(printk("jffs_insert_file_into_tree(): Found "
1099 "inode with no parent and pino == %u\n",
1100 f->pino));
1101 return -1;
1104 f->parent = parent;
1105 f->sibling_next = parent->children;
1106 if (f->sibling_next) {
1107 f->sibling_next->sibling_prev = f;
1109 f->sibling_prev = 0;
1110 parent->children = f;
1111 return 0;
1115 /* Remove a file from the hash table. */
1117 jffs_unlink_file_from_hash(struct jffs_file *f)
1119 D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1120 "ino %u\n", f, f->ino));
1122 list_del(&f->hash);
1123 return 0;
1127 /* Just remove the file from the parent's children. Don't free
1128 any memory. */
1130 jffs_unlink_file_from_tree(struct jffs_file *f)
1132 D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
1133 "\"%s\"\n", f->ino, f->pino, (f->name ? f->name : "")));
1135 if (f->sibling_prev) {
1136 f->sibling_prev->sibling_next = f->sibling_next;
1138 else if (f->parent) {
1139 D3(printk("f->parent=%p\n", f->parent));
1140 f->parent->children = f->sibling_next;
1142 if (f->sibling_next) {
1143 f->sibling_next->sibling_prev = f->sibling_prev;
1145 return 0;
1149 /* Find a file with its inode number. */
1150 struct jffs_file *
1151 jffs_find_file(struct jffs_control *c, __u32 ino)
1153 struct jffs_file *f;
1154 int i = ino % c->hash_len;
1155 struct list_head *tmp;
1157 D3(printk("jffs_find_file(): ino: %u\n", ino));
1159 for (tmp = c->hash[i].next; tmp != &c->hash[i]; tmp = tmp->next) {
1160 f = list_entry(tmp, struct jffs_file, hash);
1161 if (ino != f->ino)
1162 continue;
1163 D3(printk("jffs_find_file(): Found file with ino "
1164 "%u. (name: \"%s\")\n",
1165 ino, (f->name ? f->name : ""));
1167 return f;
1169 D3(printk("jffs_find_file(): Didn't find file "
1170 "with ino %u.\n", ino);
1172 return NULL;
1176 /* Find a file in a directory. We are comparing the names. */
1177 struct jffs_file *
1178 jffs_find_child(struct jffs_file *dir, const char *name, int len)
1180 struct jffs_file *f;
1182 D3(printk("jffs_find_child()\n"));
1184 for (f = dir->children; f; f = f->sibling_next) {
1185 if (f->name
1186 && !strncmp(f->name, name, len)
1187 && f->name[len] == '\0') {
1188 break;
1192 D3(if (f) {
1193 printk("jffs_find_child(): Found \"%s\".\n", f->name);
1195 else {
1196 char *copy = (char *) kmalloc(len + 1, GFP_KERNEL);
1197 if (copy) {
1198 memcpy(copy, name, len);
1199 copy[len] = '\0';
1201 printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1202 (copy ? copy : ""));
1203 if (copy) {
1204 kfree(copy);
1208 return f;
1212 /* Write a raw inode that takes up a certain amount of space in the flash
1213 memory. At the end of the flash device, there is often space that is
1214 impossible to use. At these times we want to mark this space as not
1215 used. In the cases when the amount of space is greater or equal than
1216 a struct jffs_raw_inode, we write a "dummy node" that takes up this
1217 space. The space after the raw inode, if it exists, is left as it is.
1218 Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1219 we can compute the checksum of it; we don't have to manipulate it any
1220 further.
1222 If the space left on the device is less than the size of a struct
1223 jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1224 No raw inode is written this time. */
1225 static int
1226 jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1228 struct jffs_fmcontrol *fmc = c->fmc;
1229 int err;
1231 D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1232 "dirty_fm->size = %u\n",
1233 dirty_fm->offset, dirty_fm->size));
1235 if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
1236 struct jffs_raw_inode raw_inode;
1237 memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1238 raw_inode.magic = JFFS_MAGIC_BITMASK;
1239 raw_inode.dsize = dirty_fm->size
1240 - sizeof(struct jffs_raw_inode);
1241 raw_inode.dchksum = raw_inode.dsize * 0xff;
1242 raw_inode.chksum
1243 = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
1245 if ((err = flash_safe_write(fmc->mtd,
1246 dirty_fm->offset,
1247 (u_char *)&raw_inode,
1248 sizeof(struct jffs_raw_inode)))
1249 < 0) {
1250 printk(KERN_ERR "JFFS: jffs_write_dummy_node: "
1251 "flash_safe_write failed!\n");
1252 return err;
1255 else {
1256 flash_safe_acquire(fmc->mtd);
1257 flash_memset(fmc->mtd, dirty_fm->offset, 0, dirty_fm->size);
1258 flash_safe_release(fmc->mtd);
1261 D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1262 return 0;
1265 /* Write a raw inode, possibly its name and possibly some data. */
1267 jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1268 struct jffs_raw_inode *raw_inode,
1269 const char *name, const unsigned char *data)
1271 struct jffs_fmcontrol *fmc = c->fmc;
1272 struct jffs_fm *fm;
1273 __u32 pos;
1274 int err;
1275 __u32 total_name_size = raw_inode->nsize
1276 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1277 __u32 total_data_size = raw_inode->dsize
1278 + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1279 __u32 total_size = sizeof(struct jffs_raw_inode)
1280 + total_name_size + total_data_size;
1282 /* Fire the retrorockets and shoot the fruiton torpedoes, sir! */
1284 ASSERT(if (!node) {
1285 printk("jffs_write_node(): node == NULL\n");
1286 return -EINVAL;
1288 ASSERT(if (raw_inode && raw_inode->nsize && !name) {
1289 printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1290 raw_inode->nsize);
1291 return -EINVAL;
1294 D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1295 "version = %u, total_size = %u\n",
1296 (name ? name : ""), raw_inode->ino,
1297 raw_inode->version, total_size));
1299 /* First try to allocate some flash memory. */
1300 if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
1301 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1302 "failed!\n", fmc, total_size));
1303 return err;
1305 else if (!fm->nodes) {
1306 /* The jffs_fm struct that we got is not good enough.
1307 Make that space dirty. */
1308 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
1309 D(printk("jffs_write_node(): "
1310 "jffs_write_dummy_node(): Failed!\n"));
1311 kfree(fm);
1312 DJM(no_jffs_fm--);
1313 return err;
1315 /* Get a new one. */
1316 if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
1317 D(printk("jffs_write_node(): Second "
1318 "jffs_fmalloc(0x%p, %u) failed!\n",
1319 fmc, total_size));
1320 return err;
1323 node->fm = fm;
1325 ASSERT(if (fm->nodes == 0) {
1326 printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1329 pos = node->fm->offset;
1331 /* Compute the checksum for the data and name chunks. */
1332 raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
1333 raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
1335 /* The checksum is calculated without the chksum and accurate
1336 fields so set them to zero first. */
1337 raw_inode->accurate = 0;
1338 raw_inode->chksum = 0;
1339 raw_inode->chksum = jffs_checksum(raw_inode,
1340 sizeof(struct jffs_raw_inode));
1341 raw_inode->accurate = 0xff;
1343 D3(printk("jffs_write_node(): About to write this raw inode to the "
1344 "flash at pos 0x%ld:\n", (long)pos));
1345 D3(jffs_print_raw_inode(raw_inode));
1347 /* Step 1: Write the raw jffs inode to the flash. */
1348 if ((err = flash_safe_write(fmc->mtd, pos,
1349 (u_char *)raw_inode,
1350 sizeof(struct jffs_raw_inode))) < 0) {
1351 jffs_fmfree_partly(fmc, fm,
1352 total_name_size + total_data_size);
1353 printk(KERN_ERR "JFFS: jffs_write_node: Failed to write "
1354 "raw_inode.\n");
1355 return err;
1357 pos += sizeof(struct jffs_raw_inode);
1359 /* Step 2: Write the name, if there is any. */
1360 if (raw_inode->nsize) {
1361 if ((err = flash_safe_write(fmc->mtd, pos,
1362 (u_char *)name,
1363 raw_inode->nsize)) < 0) {
1364 jffs_fmfree_partly(fmc, fm, total_data_size);
1365 printk(KERN_ERR "JFFS: jffs_write_node: Failed to "
1366 "write the name.\n");
1367 return err;
1369 pos += total_name_size;
1372 /* Step 3: Append the actual data, if any. */
1373 if (raw_inode->dsize) {
1374 if ((err = flash_safe_write(fmc->mtd, pos, data,
1375 raw_inode->dsize)) < 0) {
1376 jffs_fmfree_partly(fmc, fm, 0);
1377 printk(KERN_ERR "JFFS: jffs_write_node: Failed to "
1378 "write the data.\n");
1379 return err;
1383 D3(printk("jffs_write_node(): Leaving...\n"));
1384 return raw_inode->dsize;
1385 } /* jffs_write_node() */
1388 /* Read data from the node and write it to the buffer. 'node_offset'
1389 is how much we have read from this particular node before and which
1390 shouldn't be read again. 'max_size' is how much space there is in
1391 the buffer. */
1392 static int
1393 jffs_get_node_data(struct jffs_file *f, struct jffs_node *node, char *buf,
1394 __u32 node_offset, __u32 max_size, kdev_t dev)
1396 struct jffs_fmcontrol *fmc = f->c->fmc;
1397 __u32 pos = node->fm->offset + node->fm_offset + node_offset;
1398 __u32 avail = node->data_size - node_offset;
1399 __u32 r;
1401 D2(printk(" jffs_get_node_data(): file: \"%s\", ino: %u, "
1402 "version: %u, node_offset: %u\n",
1403 f->name, node->ino, node->version, node_offset));
1405 r = jffs_min(avail, max_size);
1406 flash_safe_read(fmc->mtd, pos, buf, r);
1408 D3(printk(" jffs_get_node_data(): Read %u byte%s.\n",
1409 r, (r == 1 ? "" : "s")));
1411 return r;
1415 /* Read data from the file's nodes. Write the data to the buffer
1416 'buf'. 'read_offset' tells how much data we should skip. */
1418 jffs_read_data(struct jffs_file *f, char *buf, __u32 read_offset, __u32 size)
1420 struct jffs_node *node;
1421 __u32 read_data = 0; /* Total amount of read data. */
1422 __u32 node_offset = 0;
1423 __u32 pos = 0; /* Number of bytes traversed. */
1425 D1(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
1426 "size = %u\n",
1427 (f->name ? f->name : ""), read_offset, size));
1429 if (read_offset >= f->size) {
1430 D(printk(" f->size: %d\n", f->size));
1431 return 0;
1434 /* First find the node to read data from. */
1435 node = f->range_head;
1436 while (pos <= read_offset) {
1437 node_offset = read_offset - pos;
1438 if (node_offset >= node->data_size) {
1439 pos += node->data_size;
1440 node = node->range_next;
1442 else {
1443 break;
1447 /* "Cats are living proof that not everything in nature
1448 has to be useful."
1449 - Garrison Keilor ('97) */
1451 /* Fill the buffer. */
1452 while (node && (read_data < size)) {
1453 int r;
1454 if (!node->fm) {
1455 /* This node does not refer to real data. */
1456 r = jffs_min(size - read_data,
1457 node->data_size - node_offset);
1458 memset(&buf[read_data], 0, r);
1460 else if ((r = jffs_get_node_data(f, node, &buf[read_data],
1461 node_offset,
1462 size - read_data,
1463 f->c->sb->s_dev)) < 0) {
1464 return r;
1466 read_data += r;
1467 node_offset = 0;
1468 node = node->range_next;
1470 D3(printk(" jffs_read_data(): Read %u bytes.\n", read_data));
1471 return read_data;
1475 /* Used for traversing all nodes in the hash table. */
1477 jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
1479 int pos;
1480 int r;
1481 int result = 0;
1483 for (pos = 0; pos < c->hash_len; pos++) {
1484 struct list_head *p, *next;
1485 for (p = c->hash[pos].next; p != &c->hash[pos]; p = next) {
1486 /* We need a reference to the next file in the
1487 list because `func' might remove the current
1488 file `f'. */
1489 next = p->next;
1490 r = func(list_entry(p, struct jffs_file, hash));
1491 if (r < 0)
1492 return r;
1493 result += r;
1497 return result;
1501 /* Free all memory associated with a file. */
1503 jffs_free_node_list(struct jffs_file *f)
1505 struct jffs_node *node;
1506 struct jffs_node *p;
1508 D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
1509 f->ino, (f->name ? f->name : "")));
1510 node = f->version_head;
1511 while (node) {
1512 p = node;
1513 node = node->version_next;
1514 kfree(p);
1515 DJM(no_jffs_node--);
1517 return 0;
1521 /* See if a file is deleted. If so, mark that file's nodes as obsolete. */
1523 jffs_possibly_delete_file(struct jffs_file *f)
1525 struct jffs_node *n;
1527 D3(printk("jffs_possibly_delete_file(): ino: %u\n",
1528 f->ino));
1530 ASSERT(if (!f) {
1531 printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
1532 return -1;
1535 if (f->deleted) {
1536 /* First try to remove all older versions. */
1537 for (n = f->version_head; n; n = n->version_next) {
1538 if (!n->fm) {
1539 continue;
1541 if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
1542 break;
1545 /* Unlink the file from the filesystem. */
1546 jffs_unlink_file_from_tree(f);
1547 jffs_unlink_file_from_hash(f);
1548 jffs_free_node_list(f);
1549 if (f->name) {
1550 kfree(f->name);
1551 DJM(no_name--);
1553 kfree(f);
1554 DJM(no_jffs_file--);
1556 return 0;
1560 /* Used in conjunction with jffs_foreach_file() to count the number
1561 of files in the file system. */
1563 jffs_file_count(struct jffs_file *f)
1565 return 1;
1569 /* Build up a file's range list from scratch by going through the
1570 version list. */
1572 jffs_build_file(struct jffs_file *f)
1574 struct jffs_node *n;
1576 D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
1577 f->ino, (f->name ? f->name : "")));
1579 for (n = f->version_head; n; n = n->version_next) {
1580 jffs_update_file(f, n);
1582 return 0;
1586 /* Remove an amount of data from a file. If this amount of data is
1587 zero, that could mean that a node should be split in two parts.
1588 We remove or change the appropriate nodes in the lists.
1590 Starting offset of area to be removed is node->data_offset,
1591 and the length of the area is in node->removed_size. */
1592 static void
1593 jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
1595 struct jffs_node *n;
1596 __u32 offset = node->data_offset;
1597 __u32 remove_size = node->removed_size;
1599 D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
1600 offset, remove_size));
1602 if (remove_size == 0
1603 && f->range_tail
1604 && f->range_tail->data_offset + f->range_tail->data_size
1605 == offset) {
1606 /* A simple append; nothing to remove or no node to split. */
1607 return;
1610 /* Find the node where we should begin the removal. */
1611 for (n = f->range_head; n; n = n->range_next) {
1612 if (n->data_offset + n->data_size > offset) {
1613 break;
1616 if (!n) {
1617 /* If there's no data in the file there's no data to
1618 remove either. */
1619 return;
1622 if (n->data_offset > offset) {
1623 /* XXX: Not implemented yet. */
1624 printk(KERN_WARNING "JFFS: An unexpected situation "
1625 "occurred in jffs_delete_data.\n");
1627 else if (n->data_offset < offset) {
1628 /* See if the node has to be split into two parts. */
1629 if (n->data_offset + n->data_size < offset + remove_size) {
1630 /* Do the split. */
1631 struct jffs_node *new_node;
1632 D3(printk("jffs_delete_data(): Split node with "
1633 "version number %u.\n", n->version));
1635 if (!(new_node = (struct jffs_node *)
1636 kmalloc(sizeof(struct jffs_node),
1637 GFP_KERNEL))) {
1638 D(printk("jffs_delete_data(): -ENOMEM\n"));
1639 return;
1641 DJM(no_jffs_node++);
1643 new_node->ino = n->ino;
1644 new_node->version = n->version;
1645 new_node->data_offset = offset;
1646 new_node->data_size = n->data_size
1647 - (remove_size
1648 + (offset - n->data_offset));
1649 new_node->fm_offset = n->fm_offset + n->data_size
1650 + remove_size;
1651 new_node->name_size = n->name_size;
1652 new_node->fm = n->fm;
1653 new_node->version_prev = n;
1654 new_node->version_next = n->version_next;
1655 if (new_node->version_next) {
1656 new_node->version_next->version_prev
1657 = new_node;
1659 else {
1660 f->version_tail = new_node;
1662 n->version_next = new_node;
1663 new_node->range_prev = n;
1664 new_node->range_next = n->range_next;
1665 if (new_node->range_next) {
1666 new_node->range_next->range_prev = new_node;
1668 else {
1669 f->range_tail = new_node;
1671 /* A very interesting can of worms. */
1672 n->range_next = new_node;
1673 n->data_size = offset - n->data_offset;
1674 jffs_add_node(new_node);
1675 n = new_node->range_next;
1676 remove_size = 0;
1678 else {
1679 /* No. No need to split the node. Just remove
1680 the end of the node. */
1681 int r = jffs_min(n->data_offset + n->data_size
1682 - offset, remove_size);
1683 n->data_size -= r;
1684 remove_size -= r;
1685 n = n->range_next;
1689 /* Remove as many nodes as necessary. */
1690 while (n && remove_size) {
1691 if (n->data_size <= remove_size) {
1692 struct jffs_node *p = n;
1693 remove_size -= n->data_size;
1694 n = n->range_next;
1695 D3(printk("jffs_delete_data(): Removing node: "
1696 "ino: %u, version: %u\n",
1697 p->ino, p->version));
1698 if (p->fm) {
1699 jffs_fmfree(f->c->fmc, p->fm, p);
1701 jffs_unlink_node_from_range_list(f, p);
1702 jffs_unlink_node_from_version_list(f, p);
1703 kfree(p);
1704 DJM(no_jffs_node--);
1706 else {
1707 n->data_size -= remove_size;
1708 n->fm_offset += remove_size;
1709 n->data_offset -= (node->removed_size - remove_size);
1710 n = n->range_next;
1711 break;
1715 /* Adjust the following nodes' information about offsets etc. */
1716 while (n && node->removed_size) {
1717 n->data_offset -= node->removed_size;
1718 n = n->range_next;
1721 f->size -= node->removed_size;
1722 D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
1723 } /* jffs_delete_data() */
1726 /* Insert some data into a file. Prior to the call to this function,
1727 jffs_delete_data() should be called. */
1728 static void
1729 jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
1731 D3(printk("jffs_insert_data(): node->data_offset = %u, "
1732 "node->data_size = %u, f->size = %u\n",
1733 node->data_offset, node->data_size, f->size));
1735 /* Find the position where we should insert data. */
1737 if (node->data_offset == f->size) {
1738 /* A simple append. This is the most common operation. */
1739 node->range_next = 0;
1740 node->range_prev = f->range_tail;
1741 if (node->range_prev) {
1742 node->range_prev->range_next = node;
1744 f->range_tail = node;
1745 f->size += node->data_size;
1746 if (!f->range_head) {
1747 f->range_head = node;
1750 else if (node->data_offset < f->size) {
1751 /* Trying to insert data into the middle of the file. This
1752 means no problem because jffs_delete_data() has already
1753 prepared the range list for us. */
1754 struct jffs_node *n;
1756 /* Find the correct place for the insertion and then insert
1757 the node. */
1758 for (n = f->range_head; n; n = n->range_next) {
1759 D1(printk("Cool stuff's happening!\n"));
1761 if (n->data_offset == node->data_offset) {
1762 node->range_prev = n->range_prev;
1763 if (node->range_prev) {
1764 node->range_prev->range_next = node;
1766 else {
1767 f->range_head = node;
1769 node->range_next = n;
1770 n->range_prev = node;
1771 break;
1773 ASSERT(else if (n->data_offset + n->data_size >
1774 node->data_offset) {
1775 printk(KERN_ERR "jffs_insert_data(): "
1776 "Couldn't find a place to insert "
1777 "the data!\n");
1778 return;
1782 /* Adjust later nodes' offsets etc. */
1783 n = node->range_next;
1784 while (n) {
1785 n->data_offset += node->data_size;
1786 n = n->range_next;
1788 f->size += node->data_size;
1790 else if (node->data_offset > f->size) {
1791 /* Not implemented yet. */
1792 #if 0
1793 /* Below is some example code for future use if we decide
1794 to implement it. */
1795 /* This is code that isn't supported by VFS. So there aren't
1796 really any reasons to implement it yet. */
1797 if (!f->range_head) {
1798 if (node->data_offset > f->size) {
1799 if (!(nn = jffs_alloc_node())) {
1800 D(printk("jffs_insert_data(): "
1801 "Allocation failed.\n"));
1802 return;
1804 nn->version = JFFS_MAGIC_BITMASK;
1805 nn->data_offset = 0;
1806 nn->data_size = node->data_offset;
1807 nn->removed_size = 0;
1808 nn->fm_offset = 0;
1809 nn->name_size = 0;
1810 nn->fm = 0; /* This is a virtual data holder. */
1811 nn->version_prev = 0;
1812 nn->version_next = 0;
1813 nn->range_prev = 0;
1814 nn->range_next = 0;
1815 nh->range_head = nn;
1816 nh->range_tail = nn;
1819 #endif
1822 D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
1826 /* A new node (with data) has been added to the file and now the range
1827 list has to be modified. */
1828 static int
1829 jffs_update_file(struct jffs_file *f, struct jffs_node *node)
1831 D3(printk("jffs_update_file(): ino: %u, version: %u\n",
1832 f->ino, node->version));
1834 if (node->data_size == 0) {
1835 if (node->removed_size == 0) {
1836 /* data_offset == X */
1837 /* data_size == 0 */
1838 /* remove_size == 0 */
1840 else {
1841 /* data_offset == X */
1842 /* data_size == 0 */
1843 /* remove_size != 0 */
1844 jffs_delete_data(f, node);
1847 else {
1848 /* data_offset == X */
1849 /* data_size != 0 */
1850 /* remove_size == Y */
1851 jffs_delete_data(f, node);
1852 jffs_insert_data(f, node);
1854 return 0;
1858 /* Print the contents of a node. */
1859 void
1860 jffs_print_node(struct jffs_node *n)
1862 D(printk("jffs_node: 0x%p\n", n));
1863 D(printk("{\n"));
1864 D(printk(" 0x%08x, /* version */\n", n->version));
1865 D(printk(" 0x%08x, /* data_offset */\n", n->data_offset));
1866 D(printk(" 0x%08x, /* data_size */\n", n->data_size));
1867 D(printk(" 0x%08x, /* removed_size */\n", n->removed_size));
1868 D(printk(" 0x%08x, /* fm_offset */\n", n->fm_offset));
1869 D(printk(" 0x%02x, /* name_size */\n", n->name_size));
1870 D(printk(" 0x%p, /* fm, fm->offset: %u */\n",
1871 n->fm, n->fm->offset));
1872 D(printk(" 0x%p, /* version_prev */\n", n->version_prev));
1873 D(printk(" 0x%p, /* version_next */\n", n->version_next));
1874 D(printk(" 0x%p, /* range_prev */\n", n->range_prev));
1875 D(printk(" 0x%p, /* range_next */\n", n->range_next));
1876 D(printk("}\n"));
1880 /* Print the contents of a raw inode. */
1881 void
1882 jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
1884 D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
1885 D(printk("{\n"));
1886 D(printk(" 0x%08x, /* magic */\n", raw_inode->magic));
1887 D(printk(" 0x%08x, /* ino */\n", raw_inode->ino));
1888 D(printk(" 0x%08x, /* pino */\n", raw_inode->pino));
1889 D(printk(" 0x%08x, /* version */\n", raw_inode->version));
1890 D(printk(" 0x%08x, /* mode */\n", raw_inode->mode));
1891 D(printk(" 0x%04x, /* uid */\n", raw_inode->uid));
1892 D(printk(" 0x%04x, /* gid */\n", raw_inode->gid));
1893 D(printk(" 0x%08x, /* atime */\n", raw_inode->atime));
1894 D(printk(" 0x%08x, /* mtime */\n", raw_inode->mtime));
1895 D(printk(" 0x%08x, /* ctime */\n", raw_inode->ctime));
1896 D(printk(" 0x%08x, /* offset */\n", raw_inode->offset));
1897 D(printk(" 0x%08x, /* dsize */\n", raw_inode->dsize));
1898 D(printk(" 0x%08x, /* rsize */\n", raw_inode->rsize));
1899 D(printk(" 0x%02x, /* nsize */\n", raw_inode->nsize));
1900 D(printk(" 0x%02x, /* nlink */\n", raw_inode->nlink));
1901 D(printk(" 0x%02x, /* spare */\n",
1902 raw_inode->spare));
1903 D(printk(" %u, /* rename */\n",
1904 raw_inode->rename));
1905 D(printk(" %u, /* deleted */\n",
1906 raw_inode->deleted));
1907 D(printk(" 0x%02x, /* accurate */\n",
1908 raw_inode->accurate));
1909 D(printk(" 0x%08x, /* dchksum */\n", raw_inode->dchksum));
1910 D(printk(" 0x%04x, /* nchksum */\n", raw_inode->nchksum));
1911 D(printk(" 0x%04x, /* chksum */\n", raw_inode->chksum));
1912 D(printk("}\n"));
1916 /* Print the contents of a file. */
1918 jffs_print_file(struct jffs_file *f)
1920 D(int i);
1921 D(printk("jffs_file: 0x%p\n", f));
1922 D(printk("{\n"));
1923 D(printk(" 0x%08x, /* ino */\n", f->ino));
1924 D(printk(" 0x%08x, /* pino */\n", f->pino));
1925 D(printk(" 0x%08x, /* mode */\n", f->mode));
1926 D(printk(" 0x%04x, /* uid */\n", f->uid));
1927 D(printk(" 0x%04x, /* gid */\n", f->gid));
1928 D(printk(" 0x%08x, /* atime */\n", f->atime));
1929 D(printk(" 0x%08x, /* mtime */\n", f->mtime));
1930 D(printk(" 0x%08x, /* ctime */\n", f->ctime));
1931 D(printk(" 0x%02x, /* nsize */\n", f->nsize));
1932 D(printk(" 0x%02x, /* nlink */\n", f->nlink));
1933 D(printk(" 0x%02x, /* deleted */\n", f->deleted));
1934 D(printk(" \"%s\", ", (f->name ? f->name : "")));
1935 D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
1936 printk(" ");
1938 D(printk("/* name */\n"));
1939 D(printk(" 0x%08x, /* size */\n", f->size));
1940 D(printk(" 0x%08x, /* highest_version */\n",
1941 f->highest_version));
1942 D(printk(" 0x%p, /* c */\n", f->c));
1943 D(printk(" 0x%p, /* parent */\n", f->parent));
1944 D(printk(" 0x%p, /* children */\n", f->children));
1945 D(printk(" 0x%p, /* sibling_prev */\n", f->sibling_prev));
1946 D(printk(" 0x%p, /* sibling_next */\n", f->sibling_next));
1947 D(printk(" 0x%p, /* hash_prev */\n", f->hash.prev));
1948 D(printk(" 0x%p, /* hash_next */\n", f->hash.next));
1949 D(printk(" 0x%p, /* range_head */\n", f->range_head));
1950 D(printk(" 0x%p, /* range_tail */\n", f->range_tail));
1951 D(printk(" 0x%p, /* version_head */\n", f->version_head));
1952 D(printk(" 0x%p, /* version_tail */\n", f->version_tail));
1953 D(printk("}\n"));
1954 return 0;
1958 void
1959 jffs_print_hash_table(struct jffs_control *c)
1961 int i;
1963 printk("JFFS: Dumping the file system's hash table...\n");
1964 for (i = 0; i < c->hash_len; i++) {
1965 struct list_head *p;
1966 for (p = c->hash[i].next; p != &c->hash[i]; p = p->next) {
1967 struct jffs_file *f=list_entry(p,struct jffs_file,hash);
1968 printk("*** c->hash[%u]: \"%s\" "
1969 "(ino: %u, pino: %u)\n",
1970 i, (f->name ? f->name : ""),
1971 f->ino, f->pino);
1977 void
1978 jffs_print_tree(struct jffs_file *first_file, int indent)
1980 struct jffs_file *f;
1981 char *space;
1983 if (!first_file) {
1984 return;
1987 if (!(space = (char *) kmalloc(indent + 1, GFP_KERNEL))) {
1988 printk("jffs_print_tree(): Out of memory!\n");
1989 return;
1992 memset(space, ' ', indent);
1993 space[indent] = '\0';
1995 for (f = first_file; f; f = f->sibling_next) {
1996 printk("%s%s (ino: %u, highest_version: %u, size: %u)\n",
1997 space, (f->name ? f->name : "/"),
1998 f->ino, f->highest_version, f->size);
1999 if (S_ISDIR(f->mode)) {
2000 jffs_print_tree(f->children, indent + 2);
2004 kfree(space);
2008 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2009 void
2010 jffs_print_memory_allocation_statistics(void)
2012 static long printout = 0;
2013 printk("________ Memory printout #%ld ________\n", ++printout);
2014 printk("no_jffs_file = %ld\n", no_jffs_file);
2015 printk("no_jffs_node = %ld\n", no_jffs_node);
2016 printk("no_jffs_control = %ld\n", no_jffs_control);
2017 printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
2018 printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
2019 printk("no_jffs_fm = %ld\n", no_jffs_fm);
2020 printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
2021 printk("no_hash = %ld\n", no_hash);
2022 printk("no_name = %ld\n", no_name);
2023 printk("\n");
2025 #endif
2028 /* Rewrite `size' bytes, and begin at `node'. */
2030 jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, int size)
2032 struct jffs_control *c = f->c;
2033 struct jffs_fmcontrol *fmc = c->fmc;
2034 struct jffs_raw_inode raw_inode;
2035 struct jffs_node *new_node;
2036 struct jffs_fm *fm;
2037 __u32 pos;
2038 __u32 pos_dchksum;
2039 __u32 total_name_size;
2040 __u32 total_data_size;
2041 __u32 total_size;
2042 int err;
2044 D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2045 f->ino, (f->name ? f->name : ""), size));
2047 /* Create and initialize the new node. */
2048 if (!(new_node = (struct jffs_node *)
2049 kmalloc(sizeof(struct jffs_node), GFP_KERNEL))) {
2050 D(printk("jffs_rewrite_data(): "
2051 "Failed to allocate node.\n"));
2052 return -ENOMEM;
2054 DJM(no_jffs_node++);
2055 new_node->data_offset = node->data_offset;
2056 new_node->data_size = size;
2057 new_node->removed_size = size;
2058 total_name_size = f->nsize + JFFS_GET_PAD_BYTES(f->nsize);
2059 total_data_size = size + JFFS_GET_PAD_BYTES(size);
2060 total_size = sizeof(struct jffs_raw_inode)
2061 + total_name_size + total_data_size;
2062 new_node->fm_offset = sizeof(struct jffs_raw_inode)
2063 + total_name_size;
2065 if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
2066 D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2067 kfree(new_node);
2068 DJM(no_jffs_node--);
2069 return err;
2071 else if (!fm->nodes) {
2072 /* The jffs_fm struct that we got is not good enough. */
2073 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
2074 D(printk("jffs_rewrite_data(): "
2075 "jffs_write_dummy_node() Failed!\n"));
2076 kfree(fm);
2077 DJM(no_jffs_fm--);
2078 return err;
2080 /* Get a new one. */
2081 if ((err = jffs_fmalloc(fmc, total_size, node, &fm)) < 0) {
2082 D(printk("jffs_rewrite_data(): Second "
2083 "jffs_fmalloc(0x%p, %u) failed!\n",
2084 fmc, total_size));
2085 return err;
2088 new_node->fm = fm;
2090 ASSERT(if (new_node->fm->nodes == 0) {
2091 printk(KERN_ERR "jffs_rewrite_data(): "
2092 "new_node->fm->nodes == 0\n");
2095 /* Initialize the raw inode. */
2096 raw_inode.magic = JFFS_MAGIC_BITMASK;
2097 raw_inode.ino = f->ino;
2098 raw_inode.pino = f->pino;
2099 raw_inode.version = f->highest_version + 1;
2100 raw_inode.mode = f->mode;
2101 raw_inode.uid = f->uid;
2102 raw_inode.gid = f->gid;
2103 raw_inode.atime = f->atime;
2104 raw_inode.mtime = f->mtime;
2105 raw_inode.ctime = f->ctime;
2106 raw_inode.offset = node->data_offset;
2107 raw_inode.dsize = size;
2108 raw_inode.rsize = size;
2109 raw_inode.nsize = f->nsize;
2110 raw_inode.nlink = f->nlink;
2111 raw_inode.spare = 0;
2112 raw_inode.rename = 0;
2113 raw_inode.deleted = 0;
2114 raw_inode.accurate = 0xff;
2115 raw_inode.dchksum = 0;
2116 raw_inode.nchksum = 0;
2118 pos = new_node->fm->offset;
2119 pos_dchksum = pos +JFFS_RAW_INODE_DCHKSUM_OFFSET;
2121 D3(printk("jffs_rewrite_data(): Writing this raw inode "
2122 "to pos 0x%ul.\n", pos));
2123 D3(jffs_print_raw_inode(&raw_inode));
2125 if ((err = flash_safe_write(fmc->mtd, pos,
2126 (u_char *) &raw_inode,
2127 sizeof(struct jffs_raw_inode)
2128 - sizeof(__u32)
2129 - sizeof(__u16) - sizeof(__u16))) < 0) {
2130 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2131 "rewrite. (raw inode)\n");
2132 jffs_fmfree_partly(fmc, fm,
2133 total_name_size + total_data_size);
2134 return err;
2136 pos += sizeof(struct jffs_raw_inode);
2138 /* Write the name to the flash memory. */
2139 if (f->nsize) {
2140 D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2141 "pos 0x%ul.\n", f->name, (long)pos));
2142 if ((err = flash_safe_write(fmc->mtd, pos,
2143 (u_char *)f->name,
2144 f->nsize)) < 0) {
2145 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write "
2146 "error during rewrite. (name)\n");
2147 jffs_fmfree_partly(fmc, fm, total_data_size);
2148 return err;
2150 pos += total_name_size;
2151 raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
2154 /* Write the data. */
2155 if (size) {
2156 int r;
2157 unsigned char *page;
2158 __u32 offset = node->data_offset;
2160 if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
2161 jffs_fmfree_partly(fmc, fm, 0);
2162 return -1;
2165 while (size) {
2166 __u32 s = jffs_min(size, PAGE_SIZE);
2167 if ((r = jffs_read_data(f, (char *)page,
2168 offset, s)) < s) {
2169 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2170 "jffs_read_data() "
2171 "failed! (r = %d)\n", r);
2172 jffs_fmfree_partly(fmc, fm, 0);
2173 return -1;
2175 if ((err = flash_safe_write(fmc->mtd,
2176 pos, page, r)) < 0) {
2177 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2178 "Write error during rewrite. "
2179 "(data)\n");
2180 free_page((unsigned long)page);
2181 jffs_fmfree_partly(fmc, fm, 0);
2182 return err;
2184 pos += r;
2185 size -= r;
2186 offset += r;
2187 raw_inode.dchksum += jffs_checksum(page, r);
2190 free_page((unsigned long)page);
2193 raw_inode.accurate = 0;
2194 raw_inode.chksum = jffs_checksum(&raw_inode,
2195 sizeof(struct jffs_raw_inode)
2196 - sizeof(__u16));
2198 /* Add the checksum. */
2199 if ((err
2200 = flash_safe_write(fmc->mtd, pos_dchksum,
2201 &((u_char *)
2202 &raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
2203 sizeof(__u32) + sizeof(__u16)
2204 + sizeof(__u16))) < 0) {
2205 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2206 "rewrite. (checksum)\n");
2207 jffs_fmfree_partly(fmc, fm, 0);
2208 return err;
2211 /* Now make the file system aware of the newly written node. */
2212 jffs_insert_node(c, f, &raw_inode, f->name, new_node);
2214 D3(printk("jffs_rewrite_data(): Leaving...\n"));
2215 return 0;
2216 } /* jffs_rewrite_data() */
2219 /* jffs_garbage_collect_next implements one step in the garbage collect
2220 process and is often called multiple times at each occasion of a
2221 garbage collect. */
2223 jffs_garbage_collect_next(struct jffs_control *c)
2225 struct jffs_fmcontrol *fmc = c->fmc;
2226 struct jffs_node *node;
2227 struct jffs_file *f;
2228 int size;
2229 int data_size;
2230 int total_name_size;
2231 int free_size = fmc->flash_size - (fmc->used_size + fmc->dirty_size);
2232 __u32 free_chunk_size1 = jffs_free_size1(fmc);
2233 D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
2235 /* Get the oldest node in the flash. */
2236 node = jffs_get_oldest_node(fmc);
2237 ASSERT(if (!node) {
2238 printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
2239 "No oldest node found!\n");
2240 return -1;
2243 /* Find its corresponding file too. */
2244 f = jffs_find_file(c, node->ino);
2245 ASSERT(if (!f) {
2246 printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
2247 "No file to garbage collect! "
2248 "(ino = 0x%08x)\n", node->ino);
2249 return -1;
2252 D1(printk("jffs_garbage_collect_next(): \"%s\", "
2253 "ino: %u, version: %u\n",
2254 (f->name ? f->name : ""), node->ino, node->version));
2256 /* Compute how much we want to rewrite at the moment. */
2257 data_size = f->size - node->data_offset;
2258 total_name_size = f->nsize + JFFS_GET_PAD_BYTES(f->nsize);
2259 size = sizeof(struct jffs_raw_inode) + total_name_size
2260 + data_size + JFFS_GET_PAD_BYTES(data_size);
2262 D2(printk(" total_name_size: %u\n", total_name_size));
2263 D2(printk(" data_size: %u\n", data_size));
2264 D2(printk(" size: %u\n", size));
2265 D2(printk(" f->nsize: %u\n", f->nsize));
2266 D2(printk(" f->size: %u\n", f->size));
2267 D2(printk(" free_chunk_size1: %u\n", free_chunk_size1));
2268 D2(printk(" free_chunk_size2: %u\n", free_chunk_size2));
2270 if (size > fmc->max_chunk_size) {
2271 size = fmc->max_chunk_size;
2272 data_size = size - sizeof(struct jffs_raw_inode)
2273 - total_name_size;
2275 if (size > free_chunk_size1) {
2277 if (free_chunk_size1 <
2278 (sizeof(struct jffs_raw_inode) + f->nsize + BLOCK_SIZE)) {
2279 /* The space left is too small to be of any
2280 use really. */
2281 struct jffs_fm *dirty_fm
2282 = jffs_fmalloced(fmc,
2283 fmc->tail->offset + fmc->tail->size,
2284 free_chunk_size1, NULL);
2285 if (!dirty_fm) {
2286 printk(KERN_ERR "JFFS: "
2287 "jffs_garbage_collect_next: "
2288 "Failed to allocate `dirty' "
2289 "flash memory!\n");
2290 return -1;
2292 jffs_write_dummy_node(c, dirty_fm);
2293 goto jffs_garbage_collect_next_end;
2296 size = free_chunk_size1;
2297 data_size = size - sizeof(struct jffs_raw_inode)
2298 - total_name_size;
2301 D2(printk(" size: %u (again)\n", size));
2303 if (free_size - size < fmc->sector_size) {
2304 /* Just rewrite that node (or even less). */
2305 jffs_rewrite_data(f, node,
2306 jffs_min(node->data_size, data_size));
2308 else {
2309 size -= (sizeof(struct jffs_raw_inode) + f->nsize);
2310 jffs_rewrite_data(f, node, data_size);
2313 jffs_garbage_collect_next_end:
2314 D3(printk("jffs_garbage_collect_next: Leaving...\n"));
2315 return 0;
2316 } /* jffs_garbage_collect_next */
2319 /* If an obsolete node is partly going to be erased due to garbage
2320 collection, the part that isn't going to be erased must be filled
2321 with zeroes so that the scan of the flash will work smoothly next
2322 time.
2323 There are two phases in this procedure: First, the clearing of
2324 the name and data parts of the node. Second, possibly also clearing
2325 a part of the raw inode as well. If the box is power cycled during
2326 the first phase, only the checksum of this node-to-be-cleared-at-
2327 the-end will be wrong. If the box is power cycled during, or after,
2328 the clearing of the raw inode, the information like the length of
2329 the name and data parts are zeroed. The next time the box is
2330 powered up, the scanning algorithm manages this faulty data too
2331 because:
2333 - The checksum is invalid and thus the raw inode must be discarded
2334 in any case.
2335 - If the lengths of the data part or the name part are zeroed, the
2336 scanning just continues after the raw inode. But after the inode
2337 the scanning procedure just finds zeroes which is the same as
2338 dirt.
2340 So, in the end, this could never fail. :-) Even if it does fail,
2341 the scanning algorithm should manage that too. */
2343 static int
2344 jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
2346 struct jffs_fm *fm;
2347 struct jffs_fmcontrol *fmc = c->fmc;
2348 __u32 zero_offset;
2349 __u32 zero_size;
2350 __u32 zero_offset_data;
2351 __u32 zero_size_data;
2352 __u32 cutting_raw_inode = 0;
2354 if (!(fm = jffs_cut_node(fmc, erase_size))) {
2355 D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
2356 return 0;
2359 /* Where and how much shall we clear? */
2360 zero_offset = fmc->head->offset + erase_size;
2361 zero_size = fm->offset + fm->size - zero_offset;
2363 /* Do we have to clear the raw_inode explicitly? */
2364 if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
2365 cutting_raw_inode = sizeof(struct jffs_raw_inode)
2366 - (fm->size - zero_size);
2369 /* First, clear the name and data fields. */
2370 zero_offset_data = zero_offset + cutting_raw_inode;
2371 zero_size_data = zero_size - cutting_raw_inode;
2372 flash_safe_acquire(fmc->mtd);
2373 flash_memset(fmc->mtd, zero_offset_data, 0, zero_size_data);
2374 flash_safe_release(fmc->mtd);
2376 /* Should we clear a part of the raw inode? */
2377 if (cutting_raw_inode) {
2378 /* I guess it is ok to clear the raw inode in this order. */
2379 flash_safe_acquire(fmc->mtd);
2380 flash_memset(fmc->mtd, zero_offset, 0,
2381 cutting_raw_inode);
2382 flash_safe_release(fmc->mtd);
2385 return 0;
2386 } /* jffs_clear_end_of_node() */
2388 /* Try to erase as much as possible of the dirt in the flash memory. */
2389 long
2390 jffs_try_to_erase(struct jffs_control *c)
2392 struct jffs_fmcontrol *fmc = c->fmc;
2393 long erase_size;
2394 int err;
2395 __u32 offset;
2397 D3(printk("jffs_try_to_erase()\n"));
2399 erase_size = jffs_erasable_size(fmc);
2401 D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
2403 if (erase_size == 0) {
2404 return 0;
2406 else if (erase_size < 0) {
2407 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
2408 "jffs_erasable_size returned %ld.\n", erase_size);
2409 return erase_size;
2412 if ((err = jffs_clear_end_of_node(c, erase_size)) < 0) {
2413 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
2414 "Clearing of node failed.\n");
2415 return err;
2418 offset = fmc->head->offset - fmc->flash_start;
2420 /* Now, let's try to do the erase. */
2421 if ((err = flash_erase_region(fmc->mtd,
2422 offset, erase_size)) < 0) {
2423 printk(KERN_ERR "JFFS: Erase of flash failed. "
2424 "offset = %u, erase_size = %ld\n",
2425 offset, erase_size);
2426 /* XXX: Here we should allocate this area as dirty
2427 with jffs_fmalloced or something similar. Now
2428 we just report the error. */
2429 return err;
2432 #if 0
2433 /* Check if the erased sectors really got erased. */
2435 __u32 pos;
2436 __u32 end;
2438 pos = (__u32)flash_get_direct_pointer(c->sb->s_dev, offset);
2439 end = pos + erase_size;
2441 D2(printk("JFFS: Checking erased sector(s)...\n"));
2443 flash_safe_acquire(fmc->mtd);
2445 for (; pos < end; pos += 4) {
2446 if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
2447 printk("JFFS: Erase failed! pos = 0x%ld\n",
2448 (long)pos);
2449 jffs_hexdump(fmc->mtd, pos,
2450 jffs_min(256, end - pos));
2451 err = -1;
2452 break;
2456 flash_safe_release(fmc->mtd);
2458 if (!err) {
2459 D2(printk("JFFS: Erase succeeded.\n"));
2461 else {
2462 /* XXX: Here we should allocate the memory
2463 with jffs_fmalloced() in order to prevent
2464 JFFS from using this area accidentally. */
2465 return err;
2468 #endif
2470 /* Update the flash memory data structures. */
2471 jffs_sync_erase(fmc, erase_size);
2473 return erase_size;
2477 /* There are different criteria that should trigger a garbage collect:
2479 1. There is too much dirt in the memory.
2480 2. The free space is becoming small.
2481 3. There are many versions of a node.
2483 The garbage collect should always be done in a manner that guarantees
2484 that future garbage collects cannot be locked. E.g. Rewritten chunks
2485 should not be too large (span more than one sector in the flash memory
2486 for exemple). Of course there is a limit on how intelligent this garbage
2487 collection can be. */
2489 jffs_garbage_collect(struct jffs_control *c)
2491 struct jffs_fmcontrol *fmc = c->fmc;
2492 long erased_total = 0;
2493 long erased;
2494 int result = 0;
2495 D1(int i = 1);
2497 D2(printk("***jffs_garbage_collect(): fmc->dirty_size = %u\n",
2498 fmc->dirty_size));
2499 D2(jffs_print_fmcontrol(fmc));
2501 c->fmc->no_call_gc = 1;
2503 /* While there is too much dirt left and it is possible
2504 to garbage collect, do so. */
2506 while (fmc->dirty_size >= fmc->sector_size) {
2508 D1(printk("***jffs_garbage_collect(): round #%u, "
2509 "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
2510 D2(jffs_print_fmcontrol(fmc));
2512 /* At least one sector should be able to free now. */
2513 if ((erased = jffs_try_to_erase(c)) < 0) {
2514 printk(KERN_WARNING "JFFS: Error in "
2515 "garbage collector.\n");
2516 result = erased;
2517 goto gc_end;
2519 else if (erased == 0) {
2520 __u32 free_size = fmc->flash_size
2521 - (fmc->used_size
2522 + fmc->dirty_size);
2524 if (free_size > 0) {
2525 /* Let's dare to make a garbage collect. */
2526 if ((result = jffs_garbage_collect_next(c))
2527 < 0) {
2528 printk(KERN_ERR "JFFS: Something "
2529 "has gone seriously wrong "
2530 "with a garbage collect.\n");
2531 goto gc_end;
2534 else {
2535 /* What should we do here? */
2536 D(printk(" jffs_garbage_collect(): "
2537 "erased: %ld, free_size: %u\n",
2538 erased, free_size));
2539 result = -1;
2540 goto gc_end;
2544 D1(printk(" jffs_garbage_collect(): erased: %ld\n", erased));
2545 erased_total += erased;
2546 DJM(jffs_print_memory_allocation_statistics());
2550 gc_end:
2551 c->fmc->no_call_gc = 0;
2553 D3(printk(" jffs_garbage_collect(): Leaving...\n"));
2554 D1(if (erased_total) {
2555 printk("erased_total = %ld\n", erased_total);
2556 jffs_print_fmcontrol(fmc);
2558 return result;