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.102 2001/09/23 23:28:36 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. */
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 * Better memory management. Allocate data structures in larger chunks
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
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.
58 #include <linux/types.h>
59 #include <linux/slab.h>
60 #include <linux/jffs.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 #include <linux/freezer.h>
74 long no_jffs_node = 0;
75 static long no_jffs_file = 0;
76 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
77 long no_jffs_control = 0;
78 long no_jffs_raw_inode = 0;
79 long no_jffs_node_ref = 0;
81 long no_jffs_fmcontrol = 0;
86 static int jffs_scan_flash(struct jffs_control *c);
87 static int jffs_update_file(struct jffs_file *f, struct jffs_node *node);
88 static int jffs_build_file(struct jffs_file *f);
89 static int jffs_free_file(struct jffs_file *f);
90 static int jffs_free_node_list(struct jffs_file *f);
91 static int jffs_garbage_collect_now(struct jffs_control *c);
92 static int jffs_insert_file_into_hash(struct jffs_file *f);
93 static int jffs_remove_redundant_nodes(struct jffs_file *f);
95 /* Is there enough space on the flash? */
96 static inline int JFFS_ENOUGH_SPACE(struct jffs_control *c, __u32 space)
98 struct jffs_fmcontrol *fmc = c->fmc;
101 if ((fmc->flash_size - (fmc->used_size + fmc->dirty_size))
102 >= fmc->min_free_size + space) {
105 if (fmc->dirty_size < fmc->sector_size)
108 if (jffs_garbage_collect_now(c)) {
109 D1(printk("JFFS_ENOUGH_SPACE: jffs_garbage_collect_now() failed.\n"));
115 #if CONFIG_JFFS_FS_VERBOSE > 0
117 flash_read_u8(struct mtd_info *mtd, loff_t from)
123 res = MTD_READ(mtd, from, 1, &retlen, &ret);
125 printk("Didn't read a byte in flash_read_u8(). Returned %d\n", res);
133 jffs_hexdump(struct mtd_info *mtd, loff_t pos, int size)
141 printk("%ld:", (long) pos);
142 for (j = 0; j < 16; j++) {
143 line[j] = flash_read_u8(mtd, pos++);
145 for (i = 0; i < j; i++) {
147 printk(" %.2x", line[i] & 0xff);
150 printk("%.2x", line[i] & 0xff);
154 /* Print empty space */
155 for (; i < 16; i++) {
165 for (i = 0; i < j; i++) {
166 if (isgraph(line[i])) {
167 printk("%c", line[i]);
178 /* Print the contents of a node. */
180 jffs_print_node(struct jffs_node *n)
182 D(printk("jffs_node: 0x%p\n", n));
184 D(printk(" 0x%08x, /* version */\n", n->version));
185 D(printk(" 0x%08x, /* data_offset */\n", n->data_offset));
186 D(printk(" 0x%08x, /* data_size */\n", n->data_size));
187 D(printk(" 0x%08x, /* removed_size */\n", n->removed_size));
188 D(printk(" 0x%08x, /* fm_offset */\n", n->fm_offset));
189 D(printk(" 0x%02x, /* name_size */\n", n->name_size));
190 D(printk(" 0x%p, /* fm, fm->offset: %u */\n",
191 n->fm, (n->fm ? n->fm->offset : 0)));
192 D(printk(" 0x%p, /* version_prev */\n", n->version_prev));
193 D(printk(" 0x%p, /* version_next */\n", n->version_next));
194 D(printk(" 0x%p, /* range_prev */\n", n->range_prev));
195 D(printk(" 0x%p, /* range_next */\n", n->range_next));
201 /* Print the contents of a raw inode. */
203 jffs_print_raw_inode(struct jffs_raw_inode *raw_inode)
205 D(printk("jffs_raw_inode: inode number: %u\n", raw_inode->ino));
207 D(printk(" 0x%08x, /* magic */\n", raw_inode->magic));
208 D(printk(" 0x%08x, /* ino */\n", raw_inode->ino));
209 D(printk(" 0x%08x, /* pino */\n", raw_inode->pino));
210 D(printk(" 0x%08x, /* version */\n", raw_inode->version));
211 D(printk(" 0x%08x, /* mode */\n", raw_inode->mode));
212 D(printk(" 0x%04x, /* uid */\n", raw_inode->uid));
213 D(printk(" 0x%04x, /* gid */\n", raw_inode->gid));
214 D(printk(" 0x%08x, /* atime */\n", raw_inode->atime));
215 D(printk(" 0x%08x, /* mtime */\n", raw_inode->mtime));
216 D(printk(" 0x%08x, /* ctime */\n", raw_inode->ctime));
217 D(printk(" 0x%08x, /* offset */\n", raw_inode->offset));
218 D(printk(" 0x%08x, /* dsize */\n", raw_inode->dsize));
219 D(printk(" 0x%08x, /* rsize */\n", raw_inode->rsize));
220 D(printk(" 0x%02x, /* nsize */\n", raw_inode->nsize));
221 D(printk(" 0x%02x, /* nlink */\n", raw_inode->nlink));
222 D(printk(" 0x%02x, /* spare */\n",
224 D(printk(" %u, /* rename */\n",
226 D(printk(" %u, /* deleted */\n",
227 raw_inode->deleted));
228 D(printk(" 0x%02x, /* accurate */\n",
229 raw_inode->accurate));
230 D(printk(" 0x%08x, /* dchksum */\n", raw_inode->dchksum));
231 D(printk(" 0x%04x, /* nchksum */\n", raw_inode->nchksum));
232 D(printk(" 0x%04x, /* chksum */\n", raw_inode->chksum));
236 #define flash_safe_acquire(arg)
237 #define flash_safe_release(arg)
241 flash_safe_read(struct mtd_info *mtd, loff_t from,
242 u_char *buf, size_t count)
247 D3(printk(KERN_NOTICE "flash_safe_read(%p, %08x, %p, %08x)\n",
248 mtd, (unsigned int) from, buf, count));
250 res = mtd->read(mtd, from, count, &retlen, buf);
251 if (retlen != count) {
252 panic("Didn't read all bytes in flash_safe_read(). Returned %d\n", res);
254 return res?res:retlen;
259 flash_read_u32(struct mtd_info *mtd, loff_t from)
265 res = mtd->read(mtd, from, 4, &retlen, (unsigned char *)&ret);
267 printk("Didn't read all bytes in flash_read_u32(). Returned %d\n", res);
276 flash_safe_write(struct mtd_info *mtd, loff_t to,
277 const u_char *buf, size_t count)
282 D3(printk(KERN_NOTICE "flash_safe_write(%p, %08x, %p, %08x)\n",
283 mtd, (unsigned int) to, buf, count));
285 res = mtd->write(mtd, to, count, &retlen, buf);
286 if (retlen != count) {
287 printk("Didn't write all bytes in flash_safe_write(). Returned %d\n", res);
289 return res?res:retlen;
294 flash_safe_writev(struct mtd_info *mtd, const struct kvec *vecs,
295 unsigned long iovec_cnt, loff_t to)
297 size_t retlen, retlen_a;
301 D3(printk(KERN_NOTICE "flash_safe_writev(%p, %08x, %p)\n",
302 mtd, (unsigned int) to, vecs));
305 res = mtd->writev(mtd, vecs, iovec_cnt, to, &retlen);
306 return res ? res : retlen;
308 /* Not implemented writev. Repeatedly use write - on the not so
309 unreasonable assumption that the mtd driver doesn't care how
310 many write cycles we use. */
314 for (i=0; !res && i<iovec_cnt; i++) {
315 res = mtd->write(mtd, to, vecs[i].iov_len, &retlen_a,
317 if (retlen_a != vecs[i].iov_len) {
318 printk("Didn't write all bytes in flash_safe_writev(). Returned %d\n", res);
319 if (i != iovec_cnt-1)
322 /* If res is non-zero, retlen_a is undefined, but we don't
323 care because in that case it's not going to be
329 return res?res:retlen;
334 flash_memset(struct mtd_info *mtd, loff_t to,
335 const u_char c, size_t size)
337 static unsigned char pattern[64];
340 /* fill up pattern */
342 for(i = 0; i < 64; i++)
345 /* write as many 64-byte chunks as we can */
348 flash_safe_write(mtd, to, pattern, 64);
356 flash_safe_write(mtd, to, pattern, size);
363 intrep_erase_callback(struct erase_info *done)
365 wait_queue_head_t *wait_q;
367 wait_q = (wait_queue_head_t *)done->priv;
374 flash_erase_region(struct mtd_info *mtd, loff_t start,
377 struct erase_info *erase;
378 DECLARE_WAITQUEUE(wait, current);
379 wait_queue_head_t wait_q;
381 erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
385 init_waitqueue_head(&wait_q);
388 erase->callback = intrep_erase_callback;
391 erase->priv = (u_long)&wait_q;
393 /* FIXME: Use TASK_INTERRUPTIBLE and deal with being interrupted */
394 set_current_state(TASK_UNINTERRUPTIBLE);
395 add_wait_queue(&wait_q, &wait);
397 if (mtd->erase(mtd, erase) < 0) {
398 set_current_state(TASK_RUNNING);
399 remove_wait_queue(&wait_q, &wait);
402 printk(KERN_WARNING "flash: erase of region [0x%lx, 0x%lx] "
403 "totally failed\n", (long)start, (long)start + size);
408 schedule(); /* Wait for flash to finish. */
409 remove_wait_queue(&wait_q, &wait);
416 /* This routine calculates checksums in JFFS. */
418 jffs_checksum(const void *data, int size)
421 __u8 *ptr = (__u8 *)data;
425 D3(printk(", result: 0x%08x\n", sum));
431 jffs_checksum_flash(struct mtd_info *mtd, loff_t start, int size, __u32 *result)
438 /* Allocate read buffer */
439 read_buf = (__u8 *) kmalloc (sizeof(__u8) * 4096, GFP_KERNEL);
441 printk(KERN_NOTICE "kmalloc failed in jffs_checksum_flash()\n");
444 /* Loop until checksum done */
446 /* Get amount of data to read */
452 /* Perform flash read */
453 D3(printk(KERN_NOTICE "jffs_checksum_flash\n"));
454 flash_safe_read(mtd, ptr, &read_buf[0], length);
456 /* Compute checksum */
457 for (i=0; i < length ; i++)
460 /* Update pointer and size */
465 /* Free read buffer */
469 D3(printk("checksum result: 0x%08x\n", sum));
474 static __inline__ void jffs_fm_write_lock(struct jffs_fmcontrol *fmc)
476 // down(&fmc->wlock);
479 static __inline__ void jffs_fm_write_unlock(struct jffs_fmcontrol *fmc)
485 /* Create and initialize a new struct jffs_file. */
486 static struct jffs_file *
487 jffs_create_file(struct jffs_control *c,
488 const struct jffs_raw_inode *raw_inode)
492 if (!(f = kzalloc(sizeof(*f), GFP_KERNEL))) {
493 D(printk("jffs_create_file(): Failed!\n"));
497 f->ino = raw_inode->ino;
498 f->pino = raw_inode->pino;
499 f->nlink = raw_inode->nlink;
500 f->deleted = raw_inode->deleted;
507 /* Build a control block for the file system. */
508 static struct jffs_control *
509 jffs_create_control(struct super_block *sb)
511 struct jffs_control *c;
512 register int s = sizeof(struct jffs_control);
516 D2(printk("jffs_create_control()\n"));
518 if (!(c = kmalloc(s, GFP_KERNEL))) {
521 DJM(no_jffs_control++);
524 c->hash_len = JFFS_HASH_SIZE;
525 s = sizeof(struct list_head) * c->hash_len;
526 if (!(c->hash = kmalloc(s, GFP_KERNEL))) {
530 for (i = 0; i < c->hash_len; i++)
531 INIT_LIST_HEAD(&c->hash[i]);
532 if (!(c->fmc = jffs_build_begin(c, MINOR(sb->s_dev)))) {
535 c->next_ino = JFFS_MIN_INO + 1;
536 c->delete_list = (struct jffs_delete_list *) 0;
543 DJM(no_jffs_control--);
544 D(t = t ? t : "c->hash");
546 D(t = t ? t : "control");
547 D(printk("jffs_create_control(): Allocation failed: (%s)\n", t));
548 return (struct jffs_control *)0;
552 /* Clean up all data structures associated with the file system. */
554 jffs_cleanup_control(struct jffs_control *c)
556 D2(printk("jffs_cleanup_control()\n"));
559 D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
563 while (c->delete_list) {
564 struct jffs_delete_list *delete_list_element;
565 delete_list_element = c->delete_list;
566 c->delete_list = c->delete_list->next;
567 kfree(delete_list_element);
570 /* Free all files and nodes. */
572 jffs_foreach_file(c, jffs_free_node_list);
573 jffs_foreach_file(c, jffs_free_file);
577 jffs_cleanup_fmcontrol(c->fmc);
579 DJM(no_jffs_control--);
580 D3(printk("jffs_cleanup_control(): Leaving...\n"));
584 /* This function adds a virtual root node to the in-RAM representation.
585 Called by jffs_build_fs(). */
587 jffs_add_virtual_root(struct jffs_control *c)
589 struct jffs_file *root;
590 struct jffs_node *node;
592 D2(printk("jffs_add_virtual_root(): "
593 "Creating a virtual root directory.\n"));
595 if (!(root = kmalloc(sizeof(struct jffs_file), GFP_KERNEL))) {
599 if (!(node = jffs_alloc_node())) {
605 memset(node, 0, sizeof(struct jffs_node));
606 node->ino = JFFS_MIN_INO;
607 memset(root, 0, sizeof(struct jffs_file));
608 root->ino = JFFS_MIN_INO;
609 root->mode = S_IFDIR | S_IRWXU | S_IRGRP
610 | S_IXGRP | S_IROTH | S_IXOTH;
611 root->atime = root->mtime = root->ctime = get_seconds();
614 root->version_head = root->version_tail = node;
615 jffs_insert_file_into_hash(root);
620 /* This is where the file system is built and initialized. */
622 jffs_build_fs(struct super_block *sb)
624 struct jffs_control *c;
627 D2(printk("jffs_build_fs()\n"));
629 if (!(c = jffs_create_control(sb))) {
634 if ((err = jffs_scan_flash(c)) < 0) {
636 /* scan_flash() wants us to try once more. A flipping
637 bits sector was detect in the middle of the scan flash.
638 Clean up old allocated memory before going in.
640 D1(printk("jffs_build_fs: Cleaning up all control structures,"
641 " reallocating them and trying mount again.\n"));
642 jffs_cleanup_control(c);
643 if (!(c = jffs_create_control(sb))) {
649 if ((err = jffs_scan_flash(c)) < 0) {
650 goto jffs_build_fs_fail;
653 goto jffs_build_fs_fail;
657 /* Add a virtual root node if no one exists. */
658 if (!jffs_find_file(c, JFFS_MIN_INO)) {
659 if ((err = jffs_add_virtual_root(c)) < 0) {
660 goto jffs_build_fs_fail;
664 while (c->delete_list) {
666 struct jffs_delete_list *delete_list_element;
668 if ((f = jffs_find_file(c, c->delete_list->ino))) {
671 delete_list_element = c->delete_list;
672 c->delete_list = c->delete_list->next;
673 kfree(delete_list_element);
676 /* Remove deleted nodes. */
677 if ((err = jffs_foreach_file(c, jffs_possibly_delete_file)) < 0) {
678 printk(KERN_ERR "JFFS: Failed to remove deleted nodes.\n");
679 goto jffs_build_fs_fail;
681 /* Remove redundant nodes. (We are not interested in the
682 return value in this case.) */
683 jffs_foreach_file(c, jffs_remove_redundant_nodes);
684 /* Try to build a tree from all the nodes. */
685 if ((err = jffs_foreach_file(c, jffs_insert_file_into_tree)) < 0) {
686 printk("JFFS: Failed to build tree.\n");
687 goto jffs_build_fs_fail;
689 /* Compute the sizes of all files in the filesystem. Adjust if
691 if ((err = jffs_foreach_file(c, jffs_build_file)) < 0) {
692 printk("JFFS: Failed to build file system.\n");
693 goto jffs_build_fs_fail;
695 sb->s_fs_info = (void *)c;
698 D1(jffs_print_hash_table(c));
699 D1(jffs_print_tree(c->root, 0));
704 jffs_cleanup_control(c);
706 } /* jffs_build_fs() */
710 This checks for sectors that were being erased in their previous
711 lifetimes and for some reason or the other (power fail etc.),
712 the erase cycles never completed.
713 As the flash array would have reverted back to read status,
714 these sectors are detected by the symptom of the "flipping bits",
715 i.e. bits being read back differently from the same location in
716 flash if read multiple times.
717 The only solution to this is to re-erase the entire
719 Unfortunately detecting "flipping bits" is not a simple exercise
720 as a bit may be read back at 1 or 0 depending on the alignment
721 of the stars in the universe.
722 The level of confidence is in direct proportion to the number of
723 scans done. By power fail testing I (Vipin) have been able to
724 proove that reading twice is not enough.
725 Maybe 4 times? Change NUM_REREADS to a higher number if you want
726 a (even) higher degree of confidence in your mount process.
727 A higher number would of course slow down your mount.
729 static int check_partly_erased_sectors(struct jffs_fmcontrol *fmc){
731 #define NUM_REREADS 4 /* see note above */
732 #define READ_AHEAD_BYTES 4096 /* must be a multiple of 4,
733 usually set to kernel page size */
744 loff_t end = fmc->flash_size;
747 /* Allocate read buffers */
748 read_buf1 = (__u8 *) kmalloc (sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
752 read_buf2 = (__u8 *) kmalloc (sizeof(__u8) * READ_AHEAD_BYTES, GFP_KERNEL);
761 D1(printk("check_partly_erased_sector():checking sector which contains"
762 " offset 0x%x for flipping bits..\n", (__u32)pos));
764 retlen = flash_safe_read(fmc->mtd, pos,
765 &read_buf1[0], READ_AHEAD_BYTES);
768 for(cnt = 0; cnt < NUM_REREADS; cnt++){
769 (void)flash_safe_read(fmc->mtd, pos,
770 &read_buf2[0], READ_AHEAD_BYTES);
772 for (i=0 ; i < retlen ; i+=4) {
773 /* buffers MUST match, double word for word! */
774 if(*((__u32 *) &read_buf1[i]) !=
775 *((__u32 *) &read_buf2[i])
777 /* flipping bits detected, time to erase sector */
778 /* This will help us log some statistics etc. */
779 D1(printk("Flipping bits detected in re-read round:%i of %i\n",
781 D1(printk("check_partly_erased_sectors:flipping bits detected"
782 " @offset:0x%x(0x%x!=0x%x)\n",
783 (__u32)pos+i, *((__u32 *) &read_buf1[i]),
784 *((__u32 *) &read_buf2[i])));
786 /* calculate start of present sector */
787 offset = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
789 D1(printk("check_partly_erased_sector():erasing sector starting 0x%x.\n",
792 if (flash_erase_region(fmc->mtd,
793 offset, fmc->sector_size) < 0) {
794 printk(KERN_ERR "JFFS: Erase of flash failed. "
795 "offset = %u, erase_size = %d\n",
796 offset , fmc->sector_size);
802 D1(printk("JFFS: Erase of flash sector @0x%x successful.\n",
804 /* skip ahead to the next sector */
805 pos = (((__u32)pos+i)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
806 pos += fmc->sector_size;
812 pos += READ_AHEAD_BYTES;
819 D2(printk("check_partly_erased_sector():Done checking all sectors till offset 0x%x for flipping bits.\n",
824 }/* end check_partly_erased_sectors() */
828 /* Scan the whole flash memory in order to find all nodes in the
831 jffs_scan_flash(struct jffs_control *c)
833 char name[JFFS_MAX_NAME_LEN + 2];
834 struct jffs_raw_inode raw_inode;
835 struct jffs_node *node = NULL;
836 struct jffs_fmcontrol *fmc = c->fmc;
844 loff_t end = fmc->flash_size;
849 __u32 free_chunk_size1;
850 __u32 free_chunk_size2;
853 #define NUMFREEALLOWED 2 /* 2 chunks of at least erase size space allowed */
854 int num_free_space = 0; /* Flag err if more than TWO
855 free blocks found. This is NOT allowed
856 by the current jffs design.
858 int num_free_spc_not_accp = 0; /* For debugging purposed keep count
859 of how much free space was rejected and
863 D1(printk("jffs_scan_flash(): start pos = 0x%lx, end = 0x%lx\n",
864 (long)pos, (long)end));
866 flash_safe_acquire(fmc->mtd);
869 check and make sure that any sector does not suffer
870 from the "partly erased, bit flipping syndrome" (TM Vipin :)
871 If so, offending sectors will be erased.
873 if(check_partly_erased_sectors(fmc) < 0){
875 flash_safe_release(fmc->mtd);
876 return -EIO; /* bad, bad, bad error. Cannot continue.*/
879 /* Allocate read buffer */
880 read_buf = (__u8 *) kmalloc (sizeof(__u8) * 4096, GFP_KERNEL);
882 flash_safe_release(fmc->mtd);
886 /* Start the scan. */
890 /* Remember the position from where we started this scan. */
893 switch (flash_read_u32(fmc->mtd, pos)) {
894 case JFFS_EMPTY_BITMASK:
895 /* We have found 0xffffffff at this position. We have to
896 scan the rest of the flash till the end or till
897 something else than 0xffffffff is found.
898 Keep going till we do not find JFFS_EMPTY_BITMASK
901 D1(printk("jffs_scan_flash(): 0xffffffff at pos 0x%lx.\n",
906 len = end - pos < 4096 ? end - pos : 4096;
908 retlen = flash_safe_read(fmc->mtd, pos,
913 for (i=0 ; i < retlen ; i+=4, pos += 4) {
914 if(*((__u32 *) &read_buf[i]) !=
924 D1(printk("jffs_scan_flash():0xffffffff ended at pos 0x%lx.\n",
927 /* If some free space ends in the middle of a sector,
928 treat it as dirty rather than clean.
929 This is to handle the case where one thread
930 allocated space for a node, but didn't get to
931 actually _write_ it before power was lost, leaving
932 a gap in the log. Shifting all node writes into
933 a single kernel thread will fix the original problem.
935 if ((__u32) pos % fmc->sector_size) {
936 /* If there was free space in previous
937 sectors, don't mark that dirty too -
938 only from the beginning of this sector
942 test_start = pos & ~(fmc->sector_size-1); /* end of last sector */
944 if (start < test_start) {
946 /* free space started in the previous sector! */
948 if((num_free_space < NUMFREEALLOWED) &&
949 ((unsigned int)(test_start - start) >= fmc->sector_size)){
952 Count it in if we are still under NUMFREEALLOWED *and* it is
953 at least 1 erase sector in length. This will keep us from
954 picking any little ole' space as "free".
957 D1(printk("Reducing end of free space to 0x%x from 0x%x\n",
958 (unsigned int)test_start, (unsigned int)pos));
960 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
961 (unsigned int) start,
962 (unsigned int)(test_start - start)));
964 /* below, space from "start" to "pos" will be marked dirty. */
967 /* Being in here means that we have found at least an entire
968 erase sector size of free space ending on a sector boundary.
969 Keep track of free spaces accepted.
973 num_free_spc_not_accp++;
974 D1(printk("Free space (#%i) found but *Not* accepted: Starting"
975 " 0x%x for 0x%x bytes\n",
976 num_free_spc_not_accp, (unsigned int)start,
977 (unsigned int)((unsigned int)(pos & ~(fmc->sector_size-1)) - (unsigned int)start)));
982 if((((__u32)(pos - start)) != 0)){
984 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
985 (unsigned int) start, (unsigned int) (pos - start)));
986 jffs_fmalloced(fmc, (__u32) start,
987 (__u32) (pos - start), NULL);
989 /* "Flipping bits" detected. This means that our scan for them
990 did not catch this offset. See check_partly_erased_sectors() for
994 D1(printk("jffs_scan_flash():wants to allocate dirty flash "
995 "space for 0 bytes.\n"));
996 D1(printk("jffs_scan_flash(): Flipping bits! We will free "
997 "all allocated memory, erase this sector and remount\n"));
999 /* calculate start of present sector */
1000 offset = (((__u32)pos)/(__u32)fmc->sector_size) * (__u32)fmc->sector_size;
1002 D1(printk("jffs_scan_flash():erasing sector starting 0x%x.\n",
1005 if (flash_erase_region(fmc->mtd,
1006 offset, fmc->sector_size) < 0) {
1007 printk(KERN_ERR "JFFS: Erase of flash failed. "
1008 "offset = %u, erase_size = %d\n",
1009 offset , fmc->sector_size);
1011 flash_safe_release(fmc->mtd);
1013 return -1; /* bad, bad, bad! */
1016 flash_safe_release(fmc->mtd);
1019 return -EAGAIN; /* erased offending sector. Try mount one more time please. */
1022 /* Being in here means that we have found free space that ends on an erase sector
1024 Count it in if we are still under NUMFREEALLOWED *and* it is at least 1 erase
1025 sector in length. This will keep us from picking any little ole' space as "free".
1027 if((num_free_space < NUMFREEALLOWED) &&
1028 ((unsigned int)(pos - start) >= fmc->sector_size)){
1029 /* We really don't do anything to mark space as free, except *not*
1030 mark it dirty and just advance the "pos" location pointer.
1031 It will automatically be picked up as free space.
1034 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
1035 (unsigned int) start, (unsigned int) (pos - start)));
1037 num_free_spc_not_accp++;
1038 D1(printk("Free space (#%i) found but *Not* accepted: Starting "
1039 "0x%x for 0x%x bytes\n", num_free_spc_not_accp,
1040 (unsigned int) start,
1041 (unsigned int) (pos - start)));
1043 /* Mark this space as dirty. We already have our free space. */
1044 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
1045 (unsigned int) start, (unsigned int) (pos - start)));
1046 jffs_fmalloced(fmc, (__u32) start,
1047 (__u32) (pos - start), NULL);
1051 if(num_free_space > NUMFREEALLOWED){
1052 printk(KERN_WARNING "jffs_scan_flash(): Found free space "
1053 "number %i. Only %i free space is allowed.\n",
1054 num_free_space, NUMFREEALLOWED);
1058 case JFFS_DIRTY_BITMASK:
1059 /* We have found 0x00000000 at this position. Scan as far
1060 as possible to find out how much is dirty. */
1061 D1(printk("jffs_scan_flash(): 0x00000000 at pos 0x%lx.\n",
1064 && JFFS_DIRTY_BITMASK == flash_read_u32(fmc->mtd, pos);
1066 D1(printk("jffs_scan_flash(): 0x00 ended at "
1067 "pos 0x%lx.\n", (long)pos));
1068 jffs_fmalloced(fmc, (__u32) start,
1069 (__u32) (pos - start), NULL);
1072 case JFFS_MAGIC_BITMASK:
1073 /* We have probably found a new raw inode. */
1078 /* We're f*cked. This is not solved yet. We have
1079 to scan for the magic pattern. */
1080 D1(printk("*************** Dirty flash memory or "
1082 "hexdump(pos = 0x%lx, len = 128):\n",
1084 D1(jffs_hexdump(fmc->mtd, pos, 128));
1086 for (pos += 4; pos < end; pos += 4) {
1087 switch (flash_read_u32(fmc->mtd, pos)) {
1088 case JFFS_MAGIC_BITMASK:
1089 case JFFS_EMPTY_BITMASK:
1090 /* handle these in the main switch() loop */
1099 /* First, mark as dirty the region
1100 which really does contain crap. */
1101 jffs_fmalloced(fmc, (__u32) start,
1102 (__u32) (pos - start),
1108 /* We have found the beginning of an inode. Create a
1109 node for it unless there already is one available. */
1111 if (!(node = jffs_alloc_node())) {
1112 /* Free read buffer */
1115 /* Release the flash device */
1116 flash_safe_release(fmc->mtd);
1120 DJM(no_jffs_node++);
1123 /* Read the next raw inode. */
1125 flash_safe_read(fmc->mtd, pos, (u_char *) &raw_inode,
1126 sizeof(struct jffs_raw_inode));
1128 /* When we compute the checksum for the inode, we never
1129 count the 'accurate' or the 'checksum' fields. */
1130 tmp_accurate = raw_inode.accurate;
1131 tmp_chksum = raw_inode.chksum;
1132 raw_inode.accurate = 0;
1133 raw_inode.chksum = 0;
1134 checksum = jffs_checksum(&raw_inode,
1135 sizeof(struct jffs_raw_inode));
1136 raw_inode.accurate = tmp_accurate;
1137 raw_inode.chksum = tmp_chksum;
1139 D3(printk("*** We have found this raw inode at pos 0x%lx "
1140 "on the flash:\n", (long)pos));
1141 D3(jffs_print_raw_inode(&raw_inode));
1143 if (checksum != raw_inode.chksum) {
1144 D1(printk("jffs_scan_flash(): Bad checksum: "
1146 "raw_inode.chksum = %u\n",
1147 checksum, raw_inode.chksum));
1148 pos += sizeof(struct jffs_raw_inode);
1149 jffs_fmalloced(fmc, (__u32) start,
1150 (__u32) (pos - start), NULL);
1151 /* Reuse this unused struct jffs_node. */
1155 /* Check the raw inode read so far. Start with the
1156 maximum length of the filename. */
1157 if (raw_inode.nsize > JFFS_MAX_NAME_LEN) {
1158 printk(KERN_WARNING "jffs_scan_flash: Found a "
1159 "JFFS node with name too large\n");
1163 if (raw_inode.rename && raw_inode.dsize != sizeof(__u32)) {
1164 printk(KERN_WARNING "jffs_scan_flash: Found a "
1165 "rename node with dsize %u.\n",
1167 jffs_print_raw_inode(&raw_inode);
1171 /* The node's data segment should not exceed a
1173 if (raw_inode.dsize > fmc->max_chunk_size) {
1174 printk(KERN_WARNING "jffs_scan_flash: Found a "
1175 "JFFS node with dsize (0x%x) > max_chunk_size (0x%x)\n",
1176 raw_inode.dsize, fmc->max_chunk_size);
1180 pos += sizeof(struct jffs_raw_inode);
1182 /* This shouldn't be necessary because a node that
1183 violates the flash boundaries shouldn't be written
1184 in the first place. */
1189 /* Read the name. */
1191 if (raw_inode.nsize) {
1192 flash_safe_read(fmc->mtd, pos, name, raw_inode.nsize);
1193 name[raw_inode.nsize] = '\0';
1194 pos += raw_inode.nsize
1195 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1196 D3(printk("name == \"%s\"\n", name));
1197 checksum = jffs_checksum(name, raw_inode.nsize);
1198 if (checksum != raw_inode.nchksum) {
1199 D1(printk("jffs_scan_flash(): Bad checksum: "
1201 "raw_inode.nchksum = %u\n",
1202 checksum, raw_inode.nchksum));
1203 jffs_fmalloced(fmc, (__u32) start,
1204 (__u32) (pos - start), NULL);
1205 /* Reuse this unused struct jffs_node. */
1213 /* Read the data, if it exists, in order to be sure it
1214 matches the checksum. */
1215 if (raw_inode.dsize) {
1216 if (raw_inode.rename) {
1217 deleted_file = flash_read_u32(fmc->mtd, pos);
1219 if (jffs_checksum_flash(fmc->mtd, pos, raw_inode.dsize, &checksum)) {
1220 printk("jffs_checksum_flash() failed to calculate a checksum\n");
1221 jffs_fmalloced(fmc, (__u32) start,
1222 (__u32) (pos - start), NULL);
1223 /* Reuse this unused struct jffs_node. */
1226 pos += raw_inode.dsize
1227 + JFFS_GET_PAD_BYTES(raw_inode.dsize);
1229 if (checksum != raw_inode.dchksum) {
1230 D1(printk("jffs_scan_flash(): Bad checksum: "
1232 "raw_inode.dchksum = %u\n",
1233 checksum, raw_inode.dchksum));
1234 jffs_fmalloced(fmc, (__u32) start,
1235 (__u32) (pos - start), NULL);
1236 /* Reuse this unused struct jffs_node. */
1243 /* Remember the highest inode number in the whole file
1244 system. This information will be used when assigning
1245 new files new inode numbers. */
1246 if (c->next_ino <= raw_inode.ino) {
1247 c->next_ino = raw_inode.ino + 1;
1250 if (raw_inode.accurate) {
1252 node->data_offset = raw_inode.offset;
1253 node->data_size = raw_inode.dsize;
1254 node->removed_size = raw_inode.rsize;
1255 /* Compute the offset to the actual data in the
1258 = sizeof(struct jffs_raw_inode)
1260 + JFFS_GET_PAD_BYTES(raw_inode.nsize);
1261 node->fm = jffs_fmalloced(fmc, (__u32) start,
1262 (__u32) (pos - start),
1265 D(printk("jffs_scan_flash(): !node->fm\n"));
1266 jffs_free_node(node);
1267 DJM(no_jffs_node--);
1269 /* Free read buffer */
1272 /* Release the flash device */
1273 flash_safe_release(fmc->mtd);
1277 if ((err = jffs_insert_node(c, NULL, &raw_inode,
1279 printk("JFFS: Failed to handle raw inode. "
1280 "(err = %d)\n", err);
1283 if (raw_inode.rename) {
1284 struct jffs_delete_list *dl
1285 = (struct jffs_delete_list *)
1286 kmalloc(sizeof(struct jffs_delete_list),
1289 D(printk("jffs_scan_flash: !dl\n"));
1290 jffs_free_node(node);
1291 DJM(no_jffs_node--);
1293 /* Release the flash device */
1294 flash_safe_release(fmc->flash_part);
1296 /* Free read buffer */
1301 dl->ino = deleted_file;
1302 dl->next = c->delete_list;
1303 c->delete_list = dl;
1304 node->data_size = 0;
1306 D3(jffs_print_node(node));
1307 node = NULL; /* Don't free the node! */
1310 jffs_fmalloced(fmc, (__u32) start,
1311 (__u32) (pos - start), NULL);
1312 D3(printk("jffs_scan_flash(): Just found an obsolete "
1313 "raw_inode. Continuing the scan...\n"));
1314 /* Reuse this unused struct jffs_node. */
1319 jffs_free_node(node);
1320 DJM(no_jffs_node--);
1322 jffs_build_end(fmc);
1324 /* Free read buffer */
1327 if(!num_free_space){
1328 printk(KERN_WARNING "jffs_scan_flash(): Did not find even a single "
1329 "chunk of free space. This is BAD!\n");
1333 D3(printk("jffs_scan_flash(): Leaving...\n"));
1334 flash_safe_release(fmc->mtd);
1336 /* This is to trap the "free size accounting screwed error. */
1337 free_chunk_size1 = jffs_free_size1(fmc);
1338 free_chunk_size2 = jffs_free_size2(fmc);
1340 if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
1342 printk(KERN_WARNING "jffs_scan_falsh():Free size accounting screwed\n");
1343 printk(KERN_WARNING "jfffs_scan_flash():free_chunk_size1 == 0x%x, "
1344 "free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n",
1345 free_chunk_size1, free_chunk_size2, fmc->free_size);
1347 return -1; /* Do NOT mount f/s so that we can inspect what happened.
1348 Mounting this screwed up f/s will screw us up anyway.
1352 return 0; /* as far as we are concerned, we are happy! */
1353 } /* jffs_scan_flash() */
1356 /* Insert any kind of node into the file system. Take care of data
1357 insertions and deletions. Also remove redundant information. The
1358 memory allocated for the `name' is regarded as "given away" in the
1359 caller's perspective. */
1361 jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
1362 const struct jffs_raw_inode *raw_inode,
1363 const char *name, struct jffs_node *node)
1365 int update_name = 0;
1366 int insert_into_tree = 0;
1368 D2(printk("jffs_insert_node(): ino = %u, version = %u, "
1369 "name = \"%s\", deleted = %d\n",
1370 raw_inode->ino, raw_inode->version,
1371 ((name && *name) ? name : ""), raw_inode->deleted));
1373 /* If there doesn't exist an associated jffs_file, then
1374 create, initialize and insert one into the file system. */
1375 if (!f && !(f = jffs_find_file(c, raw_inode->ino))) {
1376 if (!(f = jffs_create_file(c, raw_inode))) {
1379 jffs_insert_file_into_hash(f);
1380 insert_into_tree = 1;
1382 node->ino = raw_inode->ino;
1383 node->version = raw_inode->version;
1384 node->data_size = raw_inode->dsize;
1385 node->fm_offset = sizeof(struct jffs_raw_inode) + raw_inode->nsize
1386 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1387 node->name_size = raw_inode->nsize;
1389 /* Now insert the node at the correct position into the file's
1391 if (!f->version_head) {
1392 /* This is the first node. */
1393 f->version_head = node;
1394 f->version_tail = node;
1395 node->version_prev = NULL;
1396 node->version_next = NULL;
1397 f->highest_version = node->version;
1399 f->mode = raw_inode->mode;
1400 f->uid = raw_inode->uid;
1401 f->gid = raw_inode->gid;
1402 f->atime = raw_inode->atime;
1403 f->mtime = raw_inode->mtime;
1404 f->ctime = raw_inode->ctime;
1406 else if ((f->highest_version < node->version)
1407 || (node->version == 0)) {
1408 /* Insert at the end of the list. I.e. this node is the
1409 newest one so far. */
1410 node->version_prev = f->version_tail;
1411 node->version_next = NULL;
1412 f->version_tail->version_next = node;
1413 f->version_tail = node;
1414 f->highest_version = node->version;
1416 f->pino = raw_inode->pino;
1417 f->mode = raw_inode->mode;
1418 f->uid = raw_inode->uid;
1419 f->gid = raw_inode->gid;
1420 f->atime = raw_inode->atime;
1421 f->mtime = raw_inode->mtime;
1422 f->ctime = raw_inode->ctime;
1424 else if (f->version_head->version > node->version) {
1425 /* Insert at the bottom of the list. */
1426 node->version_prev = NULL;
1427 node->version_next = f->version_head;
1428 f->version_head->version_prev = node;
1429 f->version_head = node;
1435 struct jffs_node *n;
1437 /* Search for the insertion position starting from
1438 the tail (newest node). */
1439 for (n = f->version_tail; n; n = n->version_prev) {
1440 if (n->version < node->version) {
1441 node->version_prev = n;
1442 node->version_next = n->version_next;
1443 node->version_next->version_prev = node;
1444 n->version_next = node;
1456 /* Deletion is irreversible. If any 'deleted' node is ever
1457 written, the file is deleted */
1458 if (raw_inode->deleted)
1459 f->deleted = raw_inode->deleted;
1461 /* Perhaps update the name. */
1462 if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
1467 if (!(f->name = (char *) kmalloc(raw_inode->nsize + 1,
1472 memcpy(f->name, name, raw_inode->nsize);
1473 f->name[raw_inode->nsize] = '\0';
1474 f->nsize = raw_inode->nsize;
1475 D3(printk("jffs_insert_node(): Updated the name of "
1476 "the file to \"%s\".\n", name));
1479 if (!c->building_fs) {
1480 D3(printk("jffs_insert_node(): ---------------------------"
1481 "------------------------------------------- 1\n"));
1482 if (insert_into_tree) {
1483 jffs_insert_file_into_tree(f);
1485 /* Once upon a time, we would call jffs_possibly_delete_file()
1486 here. That causes an oops if someone's still got the file
1487 open, so now we only do it in jffs_delete_inode()
1490 if (node->data_size || node->removed_size) {
1491 jffs_update_file(f, node);
1493 jffs_remove_redundant_nodes(f);
1495 jffs_garbage_collect_trigger(c);
1497 D3(printk("jffs_insert_node(): ---------------------------"
1498 "------------------------------------------- 2\n"));
1502 } /* jffs_insert_node() */
1505 /* Unlink a jffs_node from the version list it is in. */
1507 jffs_unlink_node_from_version_list(struct jffs_file *f,
1508 struct jffs_node *node)
1510 if (node->version_prev) {
1511 node->version_prev->version_next = node->version_next;
1513 f->version_head = node->version_next;
1515 if (node->version_next) {
1516 node->version_next->version_prev = node->version_prev;
1518 f->version_tail = node->version_prev;
1523 /* Unlink a jffs_node from the range list it is in. */
1525 jffs_unlink_node_from_range_list(struct jffs_file *f, struct jffs_node *node)
1527 if (node->range_prev) {
1528 node->range_prev->range_next = node->range_next;
1531 f->range_head = node->range_next;
1533 if (node->range_next) {
1534 node->range_next->range_prev = node->range_prev;
1537 f->range_tail = node->range_prev;
1542 /* Function used by jffs_remove_redundant_nodes() below. This function
1543 classifies what kind of information a node adds to a file. */
1545 jffs_classify_node(struct jffs_node *node)
1547 __u8 mod_type = JFFS_MODIFY_INODE;
1549 if (node->name_size) {
1550 mod_type |= JFFS_MODIFY_NAME;
1552 if (node->data_size || node->removed_size) {
1553 mod_type |= JFFS_MODIFY_DATA;
1559 /* Remove redundant nodes from a file. Mark the on-flash memory
1562 jffs_remove_redundant_nodes(struct jffs_file *f)
1564 struct jffs_node *newest_node;
1565 struct jffs_node *cur;
1566 struct jffs_node *prev;
1569 __u8 node_with_name_later = 0;
1571 if (!(newest_node = f->version_tail)) {
1575 /* What does the `newest_node' modify? */
1576 newest_type = jffs_classify_node(newest_node);
1577 node_with_name_later = newest_type & JFFS_MODIFY_NAME;
1579 D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1580 "newest_type: %u\n", f->ino, (f->name ? f->name : ""),
1583 /* Traverse the file's nodes and determine which of them that are
1584 superfluous. Yeah, this might look very complex at first
1585 glance but it is actually very simple. */
1586 for (cur = newest_node->version_prev; cur; cur = prev) {
1587 prev = cur->version_prev;
1588 mod_type = jffs_classify_node(cur);
1589 if ((mod_type <= JFFS_MODIFY_INODE)
1590 || ((newest_type & JFFS_MODIFY_NAME)
1592 <= (JFFS_MODIFY_INODE + JFFS_MODIFY_NAME)))
1593 || (cur->data_size == 0 && cur->removed_size
1594 && !cur->version_prev && node_with_name_later)) {
1595 /* Yes, this node is redundant. Remove it. */
1596 D2(printk("jffs_remove_redundant_nodes(): "
1597 "Removing node: ino: %u, version: %u, "
1598 "mod_type: %u\n", cur->ino, cur->version,
1600 jffs_unlink_node_from_version_list(f, cur);
1601 jffs_fmfree(f->c->fmc, cur->fm, cur);
1602 jffs_free_node(cur);
1603 DJM(no_jffs_node--);
1606 node_with_name_later |= (mod_type & JFFS_MODIFY_NAME);
1614 /* Insert a file into the hash table. */
1616 jffs_insert_file_into_hash(struct jffs_file *f)
1618 int i = f->ino % f->c->hash_len;
1620 D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f->ino));
1622 list_add(&f->hash, &f->c->hash[i]);
1627 /* Insert a file into the file system tree. */
1629 jffs_insert_file_into_tree(struct jffs_file *f)
1631 struct jffs_file *parent;
1633 D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1634 (f->name ? f->name : "")));
1636 if (!(parent = jffs_find_file(f->c, f->pino))) {
1640 f->sibling_prev = NULL;
1641 f->sibling_next = NULL;
1645 D1(printk("jffs_insert_file_into_tree(): Found "
1646 "inode with no parent and pino == %u\n",
1652 f->sibling_next = parent->children;
1653 if (f->sibling_next) {
1654 f->sibling_next->sibling_prev = f;
1656 f->sibling_prev = NULL;
1657 parent->children = f;
1662 /* Remove a file from the hash table. */
1664 jffs_unlink_file_from_hash(struct jffs_file *f)
1666 D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1667 "ino %u\n", f, f->ino));
1674 /* Just remove the file from the parent's children. Don't free
1677 jffs_unlink_file_from_tree(struct jffs_file *f)
1679 D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
1680 "\"%s\"\n", f->ino, f->pino, (f->name ? f->name : "")));
1682 if (f->sibling_prev) {
1683 f->sibling_prev->sibling_next = f->sibling_next;
1685 else if (f->parent) {
1686 D3(printk("f->parent=%p\n", f->parent));
1687 f->parent->children = f->sibling_next;
1689 if (f->sibling_next) {
1690 f->sibling_next->sibling_prev = f->sibling_prev;
1696 /* Find a file with its inode number. */
1698 jffs_find_file(struct jffs_control *c, __u32 ino)
1700 struct jffs_file *f;
1701 int i = ino % c->hash_len;
1703 D3(printk("jffs_find_file(): ino: %u\n", ino));
1705 list_for_each_entry(f, &c->hash[i], hash) {
1708 D3(printk("jffs_find_file(): Found file with ino "
1709 "%u. (name: \"%s\")\n",
1710 ino, (f->name ? f->name : ""));
1714 D3(printk("jffs_find_file(): Didn't find file "
1715 "with ino %u.\n", ino);
1721 /* Find a file in a directory. We are comparing the names. */
1723 jffs_find_child(struct jffs_file *dir, const char *name, int len)
1725 struct jffs_file *f;
1727 D3(printk("jffs_find_child()\n"));
1729 for (f = dir->children; f; f = f->sibling_next) {
1730 if (!f->deleted && f->name
1731 && !strncmp(f->name, name, len)
1732 && f->name[len] == '\0') {
1738 printk("jffs_find_child(): Found \"%s\".\n", f->name);
1741 char *copy = (char *) kmalloc(len + 1, GFP_KERNEL);
1743 memcpy(copy, name, len);
1746 printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1747 (copy ? copy : ""));
1755 /* Write a raw inode that takes up a certain amount of space in the flash
1756 memory. At the end of the flash device, there is often space that is
1757 impossible to use. At these times we want to mark this space as not
1758 used. In the cases when the amount of space is greater or equal than
1759 a struct jffs_raw_inode, we write a "dummy node" that takes up this
1760 space. The space after the raw inode, if it exists, is left as it is.
1761 Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1762 we can compute the checksum of it; we don't have to manipulate it any
1765 If the space left on the device is less than the size of a struct
1766 jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1767 No raw inode is written this time. */
1769 jffs_write_dummy_node(struct jffs_control *c, struct jffs_fm *dirty_fm)
1771 struct jffs_fmcontrol *fmc = c->fmc;
1774 D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1775 "dirty_fm->size = %u\n",
1776 dirty_fm->offset, dirty_fm->size));
1778 if (dirty_fm->size >= sizeof(struct jffs_raw_inode)) {
1779 struct jffs_raw_inode raw_inode;
1780 memset(&raw_inode, 0, sizeof(struct jffs_raw_inode));
1781 raw_inode.magic = JFFS_MAGIC_BITMASK;
1782 raw_inode.dsize = dirty_fm->size
1783 - sizeof(struct jffs_raw_inode);
1784 raw_inode.dchksum = raw_inode.dsize * 0xff;
1786 = jffs_checksum(&raw_inode, sizeof(struct jffs_raw_inode));
1788 if ((err = flash_safe_write(fmc->mtd,
1790 (u_char *)&raw_inode,
1791 sizeof(struct jffs_raw_inode)))
1793 printk(KERN_ERR "JFFS: jffs_write_dummy_node: "
1794 "flash_safe_write failed!\n");
1799 flash_safe_acquire(fmc->mtd);
1800 flash_memset(fmc->mtd, dirty_fm->offset, 0, dirty_fm->size);
1801 flash_safe_release(fmc->mtd);
1804 D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1809 /* Write a raw inode, possibly its name and possibly some data. */
1811 jffs_write_node(struct jffs_control *c, struct jffs_node *node,
1812 struct jffs_raw_inode *raw_inode,
1813 const char *name, const unsigned char *data,
1815 struct jffs_file *f)
1817 struct jffs_fmcontrol *fmc = c->fmc;
1819 struct kvec node_iovec[4];
1820 unsigned long iovec_cnt;
1826 __u32 total_name_size = raw_inode->nsize
1827 + JFFS_GET_PAD_BYTES(raw_inode->nsize);
1828 __u32 total_data_size = raw_inode->dsize
1829 + JFFS_GET_PAD_BYTES(raw_inode->dsize);
1830 __u32 total_size = sizeof(struct jffs_raw_inode)
1831 + total_name_size + total_data_size;
1833 /* If this node isn't something that will eventually let
1834 GC free even more space, then don't allow it unless
1835 there's at least max_chunk_size space still available
1838 slack = fmc->max_chunk_size;
1841 /* Fire the retrorockets and shoot the fruiton torpedoes, sir! */
1844 printk("jffs_write_node(): node == NULL\n");
1847 ASSERT(if (raw_inode && raw_inode->nsize && !name) {
1848 printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1853 D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1854 "total_size = %u\n",
1855 (name ? name : ""), raw_inode->ino,
1858 jffs_fm_write_lock(fmc);
1865 /* Deadlocks suck. */
1866 while(fmc->free_size < fmc->min_free_size + total_size + slack) {
1867 jffs_fm_write_unlock(fmc);
1868 if (!JFFS_ENOUGH_SPACE(c, total_size + slack))
1870 jffs_fm_write_lock(fmc);
1873 /* First try to allocate some flash memory. */
1874 err = jffs_fmalloc(fmc, total_size, node, &fm);
1876 if (err == -ENOSPC) {
1877 /* Just out of space. GC and try again */
1878 if (fmc->dirty_size < fmc->sector_size) {
1879 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1880 "failed, no dirty space to GC\n", fmc,
1885 D1(printk(KERN_INFO "jffs_write_node(): Calling jffs_garbage_collect_now()\n"));
1886 jffs_fm_write_unlock(fmc);
1887 if ((err = jffs_garbage_collect_now(c))) {
1888 D(printk("jffs_write_node(): jffs_garbage_collect_now() failed\n"));
1891 jffs_fm_write_lock(fmc);
1896 jffs_fm_write_unlock(fmc);
1898 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1899 "failed!\n", fmc, total_size));
1904 /* The jffs_fm struct that we got is not good enough.
1905 Make that space dirty and try again */
1906 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
1909 jffs_fm_write_unlock(fmc);
1910 D(printk("jffs_write_node(): "
1911 "jffs_write_dummy_node(): Failed!\n"));
1919 ASSERT(if (fm->nodes == 0) {
1920 printk(KERN_ERR "jffs_write_node(): fm->nodes == 0\n");
1923 pos = node->fm->offset;
1925 /* Increment the version number here. We can't let the caller
1926 set it beforehand, because we might have had to do GC on a node
1927 of this file - and we'd end up reusing version numbers.
1930 raw_inode->version = f->highest_version + 1;
1931 D1(printk (KERN_NOTICE "jffs_write_node(): setting version of %s to %d\n", f->name, raw_inode->version));
1933 /* if the file was deleted, set the deleted bit in the raw inode */
1935 raw_inode->deleted = 1;
1938 /* Compute the checksum for the data and name chunks. */
1939 raw_inode->dchksum = jffs_checksum(data, raw_inode->dsize);
1940 raw_inode->nchksum = jffs_checksum(name, raw_inode->nsize);
1942 /* The checksum is calculated without the chksum and accurate
1943 fields so set them to zero first. */
1944 raw_inode->accurate = 0;
1945 raw_inode->chksum = 0;
1946 raw_inode->chksum = jffs_checksum(raw_inode,
1947 sizeof(struct jffs_raw_inode));
1948 raw_inode->accurate = 0xff;
1950 D3(printk("jffs_write_node(): About to write this raw inode to the "
1951 "flash at pos 0x%lx:\n", (long)pos));
1952 D3(jffs_print_raw_inode(raw_inode));
1954 /* The actual raw JFFS node */
1955 node_iovec[0].iov_base = (void *) raw_inode;
1956 node_iovec[0].iov_len = (size_t) sizeof(struct jffs_raw_inode);
1959 /* Get name and size if there is one */
1960 if (raw_inode->nsize) {
1961 node_iovec[iovec_cnt].iov_base = (void *) name;
1962 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->nsize;
1965 if (JFFS_GET_PAD_BYTES(raw_inode->nsize)) {
1966 static unsigned char allff[3]={255,255,255};
1967 /* Add some extra padding if necessary */
1968 node_iovec[iovec_cnt].iov_base = allff;
1969 node_iovec[iovec_cnt].iov_len =
1970 JFFS_GET_PAD_BYTES(raw_inode->nsize);
1975 /* Get data and size if there is any */
1976 if (raw_inode->dsize) {
1977 node_iovec[iovec_cnt].iov_base = (void *) data;
1978 node_iovec[iovec_cnt].iov_len = (size_t) raw_inode->dsize;
1980 /* No need to pad this because we're not actually putting
1985 if ((err = flash_safe_writev(fmc->mtd, node_iovec, iovec_cnt,
1987 jffs_fmfree_partly(fmc, fm, 0);
1988 jffs_fm_write_unlock(fmc);
1989 printk(KERN_ERR "JFFS: jffs_write_node: Failed to write, "
1990 "requested %i, wrote %i\n", total_size, err);
1993 if (raw_inode->deleted)
1996 jffs_fm_write_unlock(fmc);
1997 D3(printk("jffs_write_node(): Leaving...\n"));
1998 return raw_inode->dsize;
1999 } /* jffs_write_node() */
2002 /* Read data from the node and write it to the buffer. 'node_offset'
2003 is how much we have read from this particular node before and which
2004 shouldn't be read again. 'max_size' is how much space there is in
2007 jffs_get_node_data(struct jffs_file *f, struct jffs_node *node,
2008 unsigned char *buf,__u32 node_offset, __u32 max_size)
2010 struct jffs_fmcontrol *fmc = f->c->fmc;
2011 __u32 pos = node->fm->offset + node->fm_offset + node_offset;
2012 __u32 avail = node->data_size - node_offset;
2015 D2(printk(" jffs_get_node_data(): file: \"%s\", ino: %u, "
2016 "version: %u, node_offset: %u\n",
2017 f->name, node->ino, node->version, node_offset));
2019 r = min(avail, max_size);
2020 D3(printk(KERN_NOTICE "jffs_get_node_data\n"));
2021 flash_safe_read(fmc->mtd, pos, buf, r);
2023 D3(printk(" jffs_get_node_data(): Read %u byte%s.\n",
2024 r, (r == 1 ? "" : "s")));
2030 /* Read data from the file's nodes. Write the data to the buffer
2031 'buf'. 'read_offset' tells how much data we should skip. */
2033 jffs_read_data(struct jffs_file *f, unsigned char *buf, __u32 read_offset,
2036 struct jffs_node *node;
2037 __u32 read_data = 0; /* Total amount of read data. */
2038 __u32 node_offset = 0;
2039 __u32 pos = 0; /* Number of bytes traversed. */
2041 D2(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
2043 (f->name ? f->name : ""), read_offset, size));
2045 if (read_offset >= f->size) {
2046 D(printk(" f->size: %d\n", f->size));
2050 /* First find the node to read data from. */
2051 node = f->range_head;
2052 while (pos <= read_offset) {
2053 node_offset = read_offset - pos;
2054 if (node_offset >= node->data_size) {
2055 pos += node->data_size;
2056 node = node->range_next;
2063 /* "Cats are living proof that not everything in nature
2065 - Garrison Keilor ('97) */
2067 /* Fill the buffer. */
2068 while (node && (read_data < size)) {
2071 /* This node does not refer to real data. */
2072 r = min(size - read_data,
2073 node->data_size - node_offset);
2074 memset(&buf[read_data], 0, r);
2076 else if ((r = jffs_get_node_data(f, node, &buf[read_data],
2078 size - read_data)) < 0) {
2083 node = node->range_next;
2085 D3(printk(" jffs_read_data(): Read %u bytes.\n", read_data));
2090 /* Used for traversing all nodes in the hash table. */
2092 jffs_foreach_file(struct jffs_control *c, int (*func)(struct jffs_file *))
2098 for (pos = 0; pos < c->hash_len; pos++) {
2099 struct jffs_file *f, *next;
2101 /* We must do _safe, because 'func' might remove the
2102 current file 'f' from the list. */
2103 list_for_each_entry_safe(f, next, &c->hash[pos], hash) {
2115 /* Free all nodes associated with a file. */
2117 jffs_free_node_list(struct jffs_file *f)
2119 struct jffs_node *node;
2120 struct jffs_node *p;
2122 D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
2123 f->ino, (f->name ? f->name : "")));
2124 node = f->version_head;
2127 node = node->version_next;
2129 DJM(no_jffs_node--);
2135 /* Free a file and its name. */
2137 jffs_free_file(struct jffs_file *f)
2139 D3(printk("jffs_free_file: f #%u, \"%s\"\n",
2140 f->ino, (f->name ? f->name : "")));
2152 jffs_get_file_count(void)
2154 return no_jffs_file;
2157 /* See if a file is deleted. If so, mark that file's nodes as obsolete. */
2159 jffs_possibly_delete_file(struct jffs_file *f)
2161 struct jffs_node *n;
2163 D3(printk("jffs_possibly_delete_file(): ino: %u\n",
2167 printk(KERN_ERR "jffs_possibly_delete_file(): f == NULL\n");
2172 /* First try to remove all older versions. Commence with
2174 for (n = f->version_head; n; n = n->version_next) {
2178 if (jffs_fmfree(f->c->fmc, n->fm, n) < 0) {
2182 /* Unlink the file from the filesystem. */
2183 if (!f->c->building_fs) {
2184 jffs_unlink_file_from_tree(f);
2186 jffs_unlink_file_from_hash(f);
2187 jffs_free_node_list(f);
2194 /* Used in conjunction with jffs_foreach_file() to count the number
2195 of files in the file system. */
2197 jffs_file_count(struct jffs_file *f)
2203 /* Build up a file's range list from scratch by going through the
2206 jffs_build_file(struct jffs_file *f)
2208 struct jffs_node *n;
2210 D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
2211 f->ino, (f->name ? f->name : "")));
2213 for (n = f->version_head; n; n = n->version_next) {
2214 jffs_update_file(f, n);
2220 /* Remove an amount of data from a file. If this amount of data is
2221 zero, that could mean that a node should be split in two parts.
2222 We remove or change the appropriate nodes in the lists.
2224 Starting offset of area to be removed is node->data_offset,
2225 and the length of the area is in node->removed_size. */
2227 jffs_delete_data(struct jffs_file *f, struct jffs_node *node)
2229 struct jffs_node *n;
2230 __u32 offset = node->data_offset;
2231 __u32 remove_size = node->removed_size;
2233 D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
2234 offset, remove_size));
2236 if (remove_size == 0
2238 && f->range_tail->data_offset + f->range_tail->data_size
2240 /* A simple append; nothing to remove or no node to split. */
2244 /* Find the node where we should begin the removal. */
2245 for (n = f->range_head; n; n = n->range_next) {
2246 if (n->data_offset + n->data_size > offset) {
2251 /* If there's no data in the file there's no data to
2256 if (n->data_offset > offset) {
2257 /* XXX: Not implemented yet. */
2258 printk(KERN_WARNING "JFFS: An unexpected situation "
2259 "occurred in jffs_delete_data.\n");
2261 else if (n->data_offset < offset) {
2262 /* See if the node has to be split into two parts. */
2263 if (n->data_offset + n->data_size > offset + remove_size) {
2265 struct jffs_node *new_node;
2266 D3(printk("jffs_delete_data(): Split node with "
2267 "version number %u.\n", n->version));
2269 if (!(new_node = jffs_alloc_node())) {
2270 D(printk("jffs_delete_data(): -ENOMEM\n"));
2273 DJM(no_jffs_node++);
2275 new_node->ino = n->ino;
2276 new_node->version = n->version;
2277 new_node->data_offset = offset;
2278 new_node->data_size = n->data_size - (remove_size + (offset - n->data_offset));
2279 new_node->fm_offset = n->fm_offset + (remove_size + (offset - n->data_offset));
2280 new_node->name_size = n->name_size;
2281 new_node->fm = n->fm;
2282 new_node->version_prev = n;
2283 new_node->version_next = n->version_next;
2284 if (new_node->version_next) {
2285 new_node->version_next->version_prev
2289 f->version_tail = new_node;
2291 n->version_next = new_node;
2292 new_node->range_prev = n;
2293 new_node->range_next = n->range_next;
2294 if (new_node->range_next) {
2295 new_node->range_next->range_prev = new_node;
2298 f->range_tail = new_node;
2300 /* A very interesting can of worms. */
2301 n->range_next = new_node;
2302 n->data_size = offset - n->data_offset;
2304 jffs_add_node(new_node);
2306 D1(printk(KERN_WARNING "jffs_delete_data(): Splitting an empty node (file hold).\n!"));
2307 D1(printk(KERN_WARNING "FIXME: Did dwmw2 do the right thing here?\n"));
2309 n = new_node->range_next;
2313 /* No. No need to split the node. Just remove
2314 the end of the node. */
2315 int r = min(n->data_offset + n->data_size
2316 - offset, remove_size);
2323 /* Remove as many nodes as necessary. */
2324 while (n && remove_size) {
2325 if (n->data_size <= remove_size) {
2326 struct jffs_node *p = n;
2327 remove_size -= n->data_size;
2329 D3(printk("jffs_delete_data(): Removing node: "
2330 "ino: %u, version: %u%s\n",
2332 (p->fm ? "" : " (virtual)")));
2334 jffs_fmfree(f->c->fmc, p->fm, p);
2336 jffs_unlink_node_from_range_list(f, p);
2337 jffs_unlink_node_from_version_list(f, p);
2339 DJM(no_jffs_node--);
2342 n->data_size -= remove_size;
2343 n->fm_offset += remove_size;
2344 n->data_offset -= (node->removed_size - remove_size);
2350 /* Adjust the following nodes' information about offsets etc. */
2351 while (n && node->removed_size) {
2352 n->data_offset -= node->removed_size;
2356 if (node->removed_size > (f->size - node->data_offset)) {
2357 /* It's possible that the removed_size is in fact
2358 * greater than the amount of data we actually thought
2359 * were present in the first place - some of the nodes
2360 * which this node originally obsoleted may already have
2361 * been deleted from the flash by subsequent garbage
2364 * If this is the case, don't let f->size go negative.
2365 * Bad things would happen :)
2367 f->size = node->data_offset;
2369 f->size -= node->removed_size;
2371 D3(printk("jffs_delete_data(): f->size = %d\n", f->size));
2373 } /* jffs_delete_data() */
2376 /* Insert some data into a file. Prior to the call to this function,
2377 jffs_delete_data should be called. */
2379 jffs_insert_data(struct jffs_file *f, struct jffs_node *node)
2381 D3(printk("jffs_insert_data(): node->data_offset = %u, "
2382 "node->data_size = %u, f->size = %u\n",
2383 node->data_offset, node->data_size, f->size));
2385 /* Find the position where we should insert data. */
2387 if (node->data_offset == f->size) {
2388 /* A simple append. This is the most common operation. */
2389 node->range_next = NULL;
2390 node->range_prev = f->range_tail;
2391 if (node->range_prev) {
2392 node->range_prev->range_next = node;
2394 f->range_tail = node;
2395 f->size += node->data_size;
2396 if (!f->range_head) {
2397 f->range_head = node;
2400 else if (node->data_offset < f->size) {
2401 /* Trying to insert data into the middle of the file. This
2402 means no problem because jffs_delete_data() has already
2403 prepared the range list for us. */
2404 struct jffs_node *n;
2406 /* Find the correct place for the insertion and then insert
2408 for (n = f->range_head; n; n = n->range_next) {
2409 D2(printk("Cool stuff's happening!\n"));
2411 if (n->data_offset == node->data_offset) {
2412 node->range_prev = n->range_prev;
2413 if (node->range_prev) {
2414 node->range_prev->range_next = node;
2417 f->range_head = node;
2419 node->range_next = n;
2420 n->range_prev = node;
2423 ASSERT(else if (n->data_offset + n->data_size >
2424 node->data_offset) {
2425 printk(KERN_ERR "jffs_insert_data(): "
2426 "Couldn't find a place to insert "
2432 /* Adjust later nodes' offsets etc. */
2433 n = node->range_next;
2435 n->data_offset += node->data_size;
2438 f->size += node->data_size;
2440 else if (node->data_offset > f->size) {
2441 /* Okay. This is tricky. This means that we want to insert
2442 data at a place that is beyond the limits of the file as
2443 it is constructed right now. This is actually a common
2444 event that for instance could occur during the mounting
2445 of the file system if a large file have been truncated,
2446 rewritten and then only partially garbage collected. */
2448 struct jffs_node *n;
2450 /* We need a place holder for the data that is missing in
2451 front of this insertion. This "virtual node" will not
2452 be associated with any space on the flash device. */
2453 struct jffs_node *virtual_node;
2454 if (!(virtual_node = jffs_alloc_node())) {
2458 D(printk("jffs_insert_data: Inserting a virtual node.\n"));
2459 D(printk(" node->data_offset = %u\n", node->data_offset));
2460 D(printk(" f->size = %u\n", f->size));
2462 virtual_node->ino = node->ino;
2463 virtual_node->version = node->version;
2464 virtual_node->removed_size = 0;
2465 virtual_node->fm_offset = 0;
2466 virtual_node->name_size = 0;
2467 virtual_node->fm = NULL; /* This is a virtual data holder. */
2468 virtual_node->version_prev = NULL;
2469 virtual_node->version_next = NULL;
2470 virtual_node->range_next = NULL;
2472 /* Are there any data at all in the file yet? */
2473 if (f->range_head) {
2474 virtual_node->data_offset
2475 = f->range_tail->data_offset
2476 + f->range_tail->data_size;
2477 virtual_node->data_size
2478 = node->data_offset - virtual_node->data_offset;
2479 virtual_node->range_prev = f->range_tail;
2480 f->range_tail->range_next = virtual_node;
2483 virtual_node->data_offset = 0;
2484 virtual_node->data_size = node->data_offset;
2485 virtual_node->range_prev = NULL;
2486 f->range_head = virtual_node;
2489 f->range_tail = virtual_node;
2490 f->size += virtual_node->data_size;
2492 /* Insert this virtual node in the version list as well. */
2493 for (n = f->version_head; n ; n = n->version_next) {
2494 if (n->version == virtual_node->version) {
2495 virtual_node->version_prev = n->version_prev;
2496 n->version_prev = virtual_node;
2497 if (virtual_node->version_prev) {
2498 virtual_node->version_prev
2499 ->version_next = virtual_node;
2502 f->version_head = virtual_node;
2504 virtual_node->version_next = n;
2509 D(jffs_print_node(virtual_node));
2511 /* Make a new try to insert the node. */
2515 D3(printk("jffs_insert_data(): f->size = %d\n", f->size));
2520 /* A new node (with data) has been added to the file and now the range
2521 list has to be modified. */
2523 jffs_update_file(struct jffs_file *f, struct jffs_node *node)
2527 D3(printk("jffs_update_file(): ino: %u, version: %u\n",
2528 f->ino, node->version));
2530 if (node->data_size == 0) {
2531 if (node->removed_size == 0) {
2532 /* data_offset == X */
2533 /* data_size == 0 */
2534 /* remove_size == 0 */
2537 /* data_offset == X */
2538 /* data_size == 0 */
2539 /* remove_size != 0 */
2540 if ((err = jffs_delete_data(f, node)) < 0) {
2546 /* data_offset == X */
2547 /* data_size != 0 */
2548 /* remove_size == Y */
2549 if ((err = jffs_delete_data(f, node)) < 0) {
2552 if ((err = jffs_insert_data(f, node)) < 0) {
2559 /* Print the contents of a file. */
2562 jffs_print_file(struct jffs_file *f)
2565 D(printk("jffs_file: 0x%p\n", f));
2567 D(printk(" 0x%08x, /* ino */\n", f->ino));
2568 D(printk(" 0x%08x, /* pino */\n", f->pino));
2569 D(printk(" 0x%08x, /* mode */\n", f->mode));
2570 D(printk(" 0x%04x, /* uid */\n", f->uid));
2571 D(printk(" 0x%04x, /* gid */\n", f->gid));
2572 D(printk(" 0x%08x, /* atime */\n", f->atime));
2573 D(printk(" 0x%08x, /* mtime */\n", f->mtime));
2574 D(printk(" 0x%08x, /* ctime */\n", f->ctime));
2575 D(printk(" 0x%02x, /* nsize */\n", f->nsize));
2576 D(printk(" 0x%02x, /* nlink */\n", f->nlink));
2577 D(printk(" 0x%02x, /* deleted */\n", f->deleted));
2578 D(printk(" \"%s\", ", (f->name ? f->name : "")));
2579 D(for (i = strlen(f->name ? f->name : ""); i < 8; ++i) {
2582 D(printk("/* name */\n"));
2583 D(printk(" 0x%08x, /* size */\n", f->size));
2584 D(printk(" 0x%08x, /* highest_version */\n",
2585 f->highest_version));
2586 D(printk(" 0x%p, /* c */\n", f->c));
2587 D(printk(" 0x%p, /* parent */\n", f->parent));
2588 D(printk(" 0x%p, /* children */\n", f->children));
2589 D(printk(" 0x%p, /* sibling_prev */\n", f->sibling_prev));
2590 D(printk(" 0x%p, /* sibling_next */\n", f->sibling_next));
2591 D(printk(" 0x%p, /* hash_prev */\n", f->hash.prev));
2592 D(printk(" 0x%p, /* hash_next */\n", f->hash.next));
2593 D(printk(" 0x%p, /* range_head */\n", f->range_head));
2594 D(printk(" 0x%p, /* range_tail */\n", f->range_tail));
2595 D(printk(" 0x%p, /* version_head */\n", f->version_head));
2596 D(printk(" 0x%p, /* version_tail */\n", f->version_tail));
2603 jffs_print_hash_table(struct jffs_control *c)
2607 printk("JFFS: Dumping the file system's hash table...\n");
2608 for (i = 0; i < c->hash_len; i++) {
2609 struct jffs_file *f;
2610 list_for_each_entry(f, &c->hash[i], hash) {
2611 printk("*** c->hash[%u]: \"%s\" "
2612 "(ino: %u, pino: %u)\n",
2613 i, (f->name ? f->name : ""),
2621 jffs_print_tree(struct jffs_file *first_file, int indent)
2623 struct jffs_file *f;
2631 if (!(space = (char *) kmalloc(indent + 1, GFP_KERNEL))) {
2632 printk("jffs_print_tree(): Out of memory!\n");
2636 memset(space, ' ', indent);
2637 space[indent] = '\0';
2639 for (f = first_file; f; f = f->sibling_next) {
2640 dir = S_ISDIR(f->mode);
2641 printk("%s%s%s (ino: %u, highest_version: %u, size: %u)\n",
2642 space, (f->name ? f->name : ""), (dir ? "/" : ""),
2643 f->ino, f->highest_version, f->size);
2645 jffs_print_tree(f->children, indent + 2);
2653 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2655 jffs_print_memory_allocation_statistics(void)
2657 static long printout;
2658 printk("________ Memory printout #%ld ________\n", ++printout);
2659 printk("no_jffs_file = %ld\n", no_jffs_file);
2660 printk("no_jffs_node = %ld\n", no_jffs_node);
2661 printk("no_jffs_control = %ld\n", no_jffs_control);
2662 printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode);
2663 printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref);
2664 printk("no_jffs_fm = %ld\n", no_jffs_fm);
2665 printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol);
2666 printk("no_hash = %ld\n", no_hash);
2667 printk("no_name = %ld\n", no_name);
2673 /* Rewrite `size' bytes, and begin at `node'. */
2675 jffs_rewrite_data(struct jffs_file *f, struct jffs_node *node, __u32 size)
2677 struct jffs_control *c = f->c;
2678 struct jffs_fmcontrol *fmc = c->fmc;
2679 struct jffs_raw_inode raw_inode;
2680 struct jffs_node *new_node;
2684 __u32 total_name_size;
2685 __u32 total_data_size;
2689 D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2690 f->ino, (f->name ? f->name : "(null)"), size));
2692 /* Create and initialize the new node. */
2693 if (!(new_node = jffs_alloc_node())) {
2694 D(printk("jffs_rewrite_data(): "
2695 "Failed to allocate node.\n"));
2698 DJM(no_jffs_node++);
2699 new_node->data_offset = node->data_offset;
2700 new_node->removed_size = size;
2701 total_name_size = JFFS_PAD(f->nsize);
2702 total_data_size = JFFS_PAD(size);
2703 total_size = sizeof(struct jffs_raw_inode)
2704 + total_name_size + total_data_size;
2705 new_node->fm_offset = sizeof(struct jffs_raw_inode)
2709 jffs_fm_write_lock(fmc);
2712 if ((err = jffs_fmalloc(fmc, total_size, new_node, &fm)) < 0) {
2713 DJM(no_jffs_node--);
2714 jffs_fm_write_unlock(fmc);
2715 D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2716 jffs_free_node(new_node);
2719 else if (!fm->nodes) {
2720 /* The jffs_fm struct that we got is not big enough. */
2721 /* This should never happen, because we deal with this case
2722 in jffs_garbage_collect_next().*/
2723 printk(KERN_WARNING "jffs_rewrite_data(): Allocated node is too small (%d bytes of %d)\n", fm->size, total_size);
2724 if ((err = jffs_write_dummy_node(c, fm)) < 0) {
2725 D(printk("jffs_rewrite_data(): "
2726 "jffs_write_dummy_node() Failed!\n"));
2731 jffs_fm_write_unlock(fmc);
2738 /* Initialize the raw inode. */
2739 raw_inode.magic = JFFS_MAGIC_BITMASK;
2740 raw_inode.ino = f->ino;
2741 raw_inode.pino = f->pino;
2742 raw_inode.version = f->highest_version + 1;
2743 raw_inode.mode = f->mode;
2744 raw_inode.uid = f->uid;
2745 raw_inode.gid = f->gid;
2746 raw_inode.atime = f->atime;
2747 raw_inode.mtime = f->mtime;
2748 raw_inode.ctime = f->ctime;
2749 raw_inode.offset = node->data_offset;
2750 raw_inode.dsize = size;
2751 raw_inode.rsize = size;
2752 raw_inode.nsize = f->nsize;
2753 raw_inode.nlink = f->nlink;
2754 raw_inode.spare = 0;
2755 raw_inode.rename = 0;
2756 raw_inode.deleted = f->deleted;
2757 raw_inode.accurate = 0xff;
2758 raw_inode.dchksum = 0;
2759 raw_inode.nchksum = 0;
2761 pos = new_node->fm->offset;
2762 pos_dchksum = pos +JFFS_RAW_INODE_DCHKSUM_OFFSET;
2764 D3(printk("jffs_rewrite_data(): Writing this raw inode "
2765 "to pos 0x%ul.\n", pos));
2766 D3(jffs_print_raw_inode(&raw_inode));
2768 if ((err = flash_safe_write(fmc->mtd, pos,
2769 (u_char *) &raw_inode,
2770 sizeof(struct jffs_raw_inode)
2772 - sizeof(__u16) - sizeof(__u16))) < 0) {
2773 jffs_fmfree_partly(fmc, fm,
2774 total_name_size + total_data_size);
2775 jffs_fm_write_unlock(fmc);
2776 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2777 "rewrite. (raw inode)\n");
2778 printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2779 "rewrite. (raw inode)\n");
2782 pos += sizeof(struct jffs_raw_inode);
2784 /* Write the name to the flash memory. */
2786 D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2787 "pos 0x%ul.\n", f->name, (unsigned int) pos));
2788 if ((err = flash_safe_write(fmc->mtd, pos,
2791 jffs_fmfree_partly(fmc, fm, total_data_size);
2792 jffs_fm_write_unlock(fmc);
2793 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write "
2794 "error during rewrite. (name)\n");
2795 printk(KERN_ERR "JFFS: jffs_rewrite_data: Now retrying "
2796 "rewrite. (name)\n");
2799 pos += total_name_size;
2800 raw_inode.nchksum = jffs_checksum(f->name, f->nsize);
2803 /* Write the data. */
2806 unsigned char *page;
2807 __u32 offset = node->data_offset;
2809 if (!(page = (unsigned char *)__get_free_page(GFP_KERNEL))) {
2810 jffs_fmfree_partly(fmc, fm, 0);
2815 __u32 s = min(size, (__u32)PAGE_SIZE);
2816 if ((r = jffs_read_data(f, (char *)page,
2818 free_page((unsigned long)page);
2819 jffs_fmfree_partly(fmc, fm, 0);
2820 jffs_fm_write_unlock(fmc);
2821 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2823 "failed! (r = %d)\n", r);
2826 if ((err = flash_safe_write(fmc->mtd,
2827 pos, page, r)) < 0) {
2828 free_page((unsigned long)page);
2829 jffs_fmfree_partly(fmc, fm, 0);
2830 jffs_fm_write_unlock(fmc);
2831 printk(KERN_ERR "JFFS: jffs_rewrite_data: "
2832 "Write error during rewrite. "
2839 raw_inode.dchksum += jffs_checksum(page, r);
2842 free_page((unsigned long)page);
2845 raw_inode.accurate = 0;
2846 raw_inode.chksum = jffs_checksum(&raw_inode,
2847 sizeof(struct jffs_raw_inode)
2850 /* Add the checksum. */
2852 = flash_safe_write(fmc->mtd, pos_dchksum,
2854 &raw_inode)[JFFS_RAW_INODE_DCHKSUM_OFFSET],
2855 sizeof(__u32) + sizeof(__u16)
2856 + sizeof(__u16))) < 0) {
2857 jffs_fmfree_partly(fmc, fm, 0);
2858 jffs_fm_write_unlock(fmc);
2859 printk(KERN_ERR "JFFS: jffs_rewrite_data: Write error during "
2860 "rewrite. (checksum)\n");
2864 /* Now make the file system aware of the newly written node. */
2865 jffs_insert_node(c, f, &raw_inode, f->name, new_node);
2866 jffs_fm_write_unlock(fmc);
2868 D3(printk("jffs_rewrite_data(): Leaving...\n"));
2870 } /* jffs_rewrite_data() */
2873 /* jffs_garbage_collect_next implements one step in the garbage collect
2874 process and is often called multiple times at each occasion of a
2878 jffs_garbage_collect_next(struct jffs_control *c)
2880 struct jffs_fmcontrol *fmc = c->fmc;
2881 struct jffs_node *node;
2882 struct jffs_file *f;
2886 __u32 total_name_size;
2887 __u32 extra_available;
2889 __u32 free_chunk_size1 = jffs_free_size1(fmc);
2890 D2(__u32 free_chunk_size2 = jffs_free_size2(fmc));
2892 /* Get the oldest node in the flash. */
2893 node = jffs_get_oldest_node(fmc);
2895 printk(KERN_ERR "JFFS: jffs_garbage_collect_next: "
2896 "No oldest node found!\n");
2898 goto jffs_garbage_collect_next_end;
2903 /* Find its corresponding file too. */
2904 f = jffs_find_file(c, node->ino);
2907 printk (KERN_ERR "JFFS: jffs_garbage_collect_next: "
2908 "No file to garbage collect! "
2909 "(ino = 0x%08x)\n", node->ino);
2910 /* FIXME: Free the offending node and recover. */
2912 goto jffs_garbage_collect_next_end;
2915 /* We always write out the name. Theoretically, we don't need
2916 to, but for now it's easier - because otherwise we'd have
2917 to keep track of how many times the current name exists on
2918 the flash and make sure it never reaches zero.
2920 The current approach means that would be possible to cause
2921 the GC to end up eating its tail by writing lots of nodes
2922 with no name for it to garbage-collect. Hence the change in
2923 inode.c to write names with _every_ node.
2925 It sucks, but it _should_ work.
2927 total_name_size = JFFS_PAD(f->nsize);
2929 D1(printk("jffs_garbage_collect_next(): \"%s\", "
2930 "ino: %u, version: %u, location 0x%x, dsize %u\n",
2931 (f->name ? f->name : ""), node->ino, node->version,
2932 node->fm->offset, node->data_size));
2934 /* Compute how many data it's possible to rewrite at the moment. */
2935 data_size = f->size - node->data_offset;
2937 /* And from that, the total size of the chunk we want to write */
2938 size = sizeof(struct jffs_raw_inode) + total_name_size
2939 + data_size + JFFS_GET_PAD_BYTES(data_size);
2941 /* If that's more than max_chunk_size, reduce it accordingly */
2942 if (size > fmc->max_chunk_size) {
2943 size = fmc->max_chunk_size;
2944 data_size = size - sizeof(struct jffs_raw_inode)
2948 /* If we're asking to take up more space than free_chunk_size1
2949 but we _could_ fit in it, shrink accordingly.
2951 if (size > free_chunk_size1) {
2953 if (free_chunk_size1 <
2954 (sizeof(struct jffs_raw_inode) + total_name_size + BLOCK_SIZE)){
2955 /* The space left is too small to be of any
2957 struct jffs_fm *dirty_fm
2958 = jffs_fmalloced(fmc,
2959 fmc->tail->offset + fmc->tail->size,
2960 free_chunk_size1, NULL);
2962 printk(KERN_ERR "JFFS: "
2963 "jffs_garbage_collect_next: "
2964 "Failed to allocate `dirty' "
2967 goto jffs_garbage_collect_next_end;
2969 D1(printk("Dirtying end of flash - too small\n"));
2970 jffs_write_dummy_node(c, dirty_fm);
2972 goto jffs_garbage_collect_next_end;
2974 D1(printk("Reducing size of new node from %d to %d to avoid "
2975 " exceeding free_chunk_size1\n",
2976 size, free_chunk_size1));
2978 size = free_chunk_size1;
2979 data_size = size - sizeof(struct jffs_raw_inode)
2984 /* Calculate the amount of space needed to hold the nodes
2985 which are remaining in the tail */
2986 space_needed = fmc->min_free_size - (node->fm->offset % fmc->sector_size);
2988 /* From that, calculate how much 'extra' space we can use to
2989 increase the size of the node we're writing from the size
2990 of the node we're obsoleting
2992 if (space_needed > fmc->free_size) {
2993 /* If we've gone below min_free_size for some reason,
2994 don't fuck up. This is why we have
2995 min_free_size > sector_size. Whinge about it though,
2996 just so I can convince myself my maths is right.
2998 D1(printk(KERN_WARNING "jffs_garbage_collect_next(): "
2999 "space_needed %d exceeded free_size %d\n",
3000 space_needed, fmc->free_size));
3001 extra_available = 0;
3003 extra_available = fmc->free_size - space_needed;
3006 /* Check that we don't use up any more 'extra' space than
3008 if (size > JFFS_PAD(node->data_size) + total_name_size +
3009 sizeof(struct jffs_raw_inode) + extra_available) {
3010 D1(printk("Reducing size of new node from %d to %ld to avoid "
3011 "catching our tail\n", size,
3012 (long) (JFFS_PAD(node->data_size) + JFFS_PAD(node->name_size) +
3013 sizeof(struct jffs_raw_inode) + extra_available)));
3014 D1(printk("space_needed = %d, extra_available = %d\n",
3015 space_needed, extra_available));
3017 size = JFFS_PAD(node->data_size) + total_name_size +
3018 sizeof(struct jffs_raw_inode) + extra_available;
3019 data_size = size - sizeof(struct jffs_raw_inode)
3023 D2(printk(" total_name_size: %u\n", total_name_size));
3024 D2(printk(" data_size: %u\n", data_size));
3025 D2(printk(" size: %u\n", size));
3026 D2(printk(" f->nsize: %u\n", f->nsize));
3027 D2(printk(" f->size: %u\n", f->size));
3028 D2(printk(" node->data_offset: %u\n", node->data_offset));
3029 D2(printk(" free_chunk_size1: %u\n", free_chunk_size1));
3030 D2(printk(" free_chunk_size2: %u\n", free_chunk_size2));
3031 D2(printk(" node->fm->offset: 0x%08x\n", node->fm->offset));
3033 if ((err = jffs_rewrite_data(f, node, data_size))) {
3034 printk(KERN_WARNING "jffs_rewrite_data() failed: %d\n", err);
3038 jffs_garbage_collect_next_end:
3039 D3(printk("jffs_garbage_collect_next: Leaving...\n"));
3041 } /* jffs_garbage_collect_next */
3044 /* If an obsolete node is partly going to be erased due to garbage
3045 collection, the part that isn't going to be erased must be filled
3046 with zeroes so that the scan of the flash will work smoothly next
3047 time. (The data in the file could for instance be a JFFS image
3048 which could cause enormous confusion during a scan of the flash
3049 device if we didn't do this.)
3050 There are two phases in this procedure: First, the clearing of
3051 the name and data parts of the node. Second, possibly also clearing
3052 a part of the raw inode as well. If the box is power cycled during
3053 the first phase, only the checksum of this node-to-be-cleared-at-
3054 the-end will be wrong. If the box is power cycled during, or after,
3055 the clearing of the raw inode, the information like the length of
3056 the name and data parts are zeroed. The next time the box is
3057 powered up, the scanning algorithm manages this faulty data too
3060 - The checksum is invalid and thus the raw inode must be discarded
3062 - If the lengths of the data part or the name part are zeroed, the
3063 scanning just continues after the raw inode. But after the inode
3064 the scanning procedure just finds zeroes which is the same as
3067 So, in the end, this could never fail. :-) Even if it does fail,
3068 the scanning algorithm should manage that too. */
3071 jffs_clear_end_of_node(struct jffs_control *c, __u32 erase_size)
3074 struct jffs_fmcontrol *fmc = c->fmc;
3077 __u32 zero_offset_data;
3078 __u32 zero_size_data;
3079 __u32 cutting_raw_inode = 0;
3081 if (!(fm = jffs_cut_node(fmc, erase_size))) {
3082 D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3086 /* Where and how much shall we clear? */
3087 zero_offset = fmc->head->offset + erase_size;
3088 zero_size = fm->offset + fm->size - zero_offset;
3090 /* Do we have to clear the raw_inode explicitly? */
3091 if (fm->size - zero_size < sizeof(struct jffs_raw_inode)) {
3092 cutting_raw_inode = sizeof(struct jffs_raw_inode)
3093 - (fm->size - zero_size);
3096 /* First, clear the name and data fields. */
3097 zero_offset_data = zero_offset + cutting_raw_inode;
3098 zero_size_data = zero_size - cutting_raw_inode;
3099 flash_safe_acquire(fmc->mtd);
3100 flash_memset(fmc->mtd, zero_offset_data, 0, zero_size_data);
3101 flash_safe_release(fmc->mtd);
3103 /* Should we clear a part of the raw inode? */
3104 if (cutting_raw_inode) {
3105 /* I guess it is ok to clear the raw inode in this order. */
3106 flash_safe_acquire(fmc->mtd);
3107 flash_memset(fmc->mtd, zero_offset, 0,
3109 flash_safe_release(fmc->mtd);
3113 } /* jffs_clear_end_of_node() */
3115 /* Try to erase as much as possible of the dirt in the flash memory. */
3117 jffs_try_to_erase(struct jffs_control *c)
3119 struct jffs_fmcontrol *fmc = c->fmc;
3124 D3(printk("jffs_try_to_erase()\n"));
3126 erase_size = jffs_erasable_size(fmc);
3128 D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size));
3130 if (erase_size == 0) {
3133 else if (erase_size < 0) {
3134 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3135 "jffs_erasable_size returned %ld.\n", erase_size);
3139 if ((err = jffs_clear_end_of_node(c, erase_size)) < 0) {
3140 printk(KERN_ERR "JFFS: jffs_try_to_erase: "
3141 "Clearing of node failed.\n");
3145 offset = fmc->head->offset;
3147 /* Now, let's try to do the erase. */
3148 if ((err = flash_erase_region(fmc->mtd,
3149 offset, erase_size)) < 0) {
3150 printk(KERN_ERR "JFFS: Erase of flash failed. "
3151 "offset = %u, erase_size = %ld\n",
3152 offset, erase_size);
3153 /* XXX: Here we should allocate this area as dirty
3154 with jffs_fmalloced or something similar. Now
3155 we just report the error. */
3160 /* Check if the erased sectors really got erased. */
3165 pos = (__u32)flash_get_direct_pointer(to_kdev_t(c->sb->s_dev), offset);
3166 end = pos + erase_size;
3168 D2(printk("JFFS: Checking erased sector(s)...\n"));
3170 flash_safe_acquire(fmc->mtd);
3172 for (; pos < end; pos += 4) {
3173 if (*(__u32 *)pos != JFFS_EMPTY_BITMASK) {
3174 printk("JFFS: Erase failed! pos = 0x%lx\n",
3176 jffs_hexdump(fmc->mtd, pos,
3177 jffs_min(256, end - pos));
3183 flash_safe_release(fmc->mtd);
3186 D2(printk("JFFS: Erase succeeded.\n"));
3189 /* XXX: Here we should allocate the memory
3190 with jffs_fmalloced() in order to prevent
3191 JFFS from using this area accidentally. */
3197 /* Update the flash memory data structures. */
3198 jffs_sync_erase(fmc, erase_size);
3204 /* There are different criteria that should trigger a garbage collect:
3206 1. There is too much dirt in the memory.
3207 2. The free space is becoming small.
3208 3. There are many versions of a node.
3210 The garbage collect should always be done in a manner that guarantees
3211 that future garbage collects cannot be locked. E.g. Rewritten chunks
3212 should not be too large (span more than one sector in the flash memory
3213 for exemple). Of course there is a limit on how intelligent this garbage
3214 collection can be. */
3218 jffs_garbage_collect_now(struct jffs_control *c)
3220 struct jffs_fmcontrol *fmc = c->fmc;
3224 D2(printk("***jffs_garbage_collect_now(): fmc->dirty_size = %u, fmc->free_size = 0x%x\n, fcs1=0x%x, fcs2=0x%x",
3225 fmc->dirty_size, fmc->free_size, jffs_free_size1(fmc), jffs_free_size2(fmc)));
3226 D2(jffs_print_fmcontrol(fmc));
3228 // down(&fmc->gclock);
3230 /* If it is possible to garbage collect, do so. */
3232 while (erased == 0) {
3233 D1(printk("***jffs_garbage_collect_now(): round #%u, "
3234 "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3235 D2(jffs_print_fmcontrol(fmc));
3237 if ((erased = jffs_try_to_erase(c)) < 0) {
3238 printk(KERN_WARNING "JFFS: Error in "
3239 "garbage collector.\n");
3246 if (fmc->free_size == 0) {
3248 printk(KERN_ERR "jffs_garbage_collect_now(): free_size == 0. This is BAD.\n");
3253 if (fmc->dirty_size < fmc->sector_size) {
3254 /* Actually, we _may_ have been able to free some,
3255 * if there are many overlapping nodes which aren't
3256 * actually marked dirty because they still have
3257 * some valid data in each.
3263 /* Let's dare to make a garbage collect. */
3264 if ((result = jffs_garbage_collect_next(c)) < 0) {
3265 printk(KERN_ERR "JFFS: Something "
3266 "has gone seriously wrong "
3267 "with a garbage collect.\n");
3271 D1(printk(" jffs_garbage_collect_now(): erased: %ld\n", erased));
3272 DJM(jffs_print_memory_allocation_statistics());
3276 // up(&fmc->gclock);
3278 D3(printk(" jffs_garbage_collect_now(): Leaving...\n"));
3280 printk("jffs_g_c_now(): erased = %ld\n", erased);
3281 jffs_print_fmcontrol(fmc);
3284 if (!erased && !result)
3288 } /* jffs_garbage_collect_now() */
3291 /* Determine if it is reasonable to start garbage collection.
3292 We start a gc pass if either:
3293 - The number of free bytes < MIN_FREE_BYTES && at least one
3295 - The number of dirty bytes > MAX_DIRTY_BYTES
3297 static inline int thread_should_wake (struct jffs_control *c)
3299 D1(printk (KERN_NOTICE "thread_should_wake(): free=%d, dirty=%d, blocksize=%d.\n",
3300 c->fmc->free_size, c->fmc->dirty_size, c->fmc->sector_size));
3302 /* If there's not enough dirty space to free a block, there's no point. */
3303 if (c->fmc->dirty_size < c->fmc->sector_size) {
3304 D2(printk(KERN_NOTICE "thread_should_wake(): Not waking. Insufficient dirty space\n"));
3308 /* If there is too much RAM used by the various structures, GC */
3309 if (jffs_get_node_inuse() > (c->fmc->used_size/c->fmc->max_chunk_size * 5 + jffs_get_file_count() * 2 + 50)) {
3310 /* FIXME: Provide proof that this test can be satisfied. We
3311 don't want a filesystem doing endless GC just because this
3312 condition cannot ever be false.
3314 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to number of nodes\n"));
3318 /* If there are fewer free bytes than the threshold, GC */
3319 if (c->fmc->free_size < c->gc_minfree_threshold) {
3320 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to insufficent free space\n"));
3323 /* If there are more dirty bytes than the threshold, GC */
3324 if (c->fmc->dirty_size > c->gc_maxdirty_threshold) {
3325 D2(printk(KERN_NOTICE "thread_should_wake(): Waking due to excessive dirty space\n"));
3328 /* FIXME: What about the "There are many versions of a node" condition? */
3334 void jffs_garbage_collect_trigger(struct jffs_control *c)
3336 /* NOTE: We rely on the fact that we have the BKL here.
3337 * Otherwise, the gc_task could go away between the check
3338 * and the wake_up_process()
3340 if (c->gc_task && thread_should_wake(c))
3341 send_sig(SIGHUP, c->gc_task, 1);
3345 /* Kernel threads take (void *) as arguments. Thus we pass
3346 the jffs_control data as a (void *) and then cast it. */
3348 jffs_garbage_collect_thread(void *ptr)
3350 struct jffs_control *c = (struct jffs_control *) ptr;
3351 struct jffs_fmcontrol *fmc = c->fmc;
3356 daemonize("jffs_gcd");
3358 c->gc_task = current;
3361 init_completion(&c->gc_thread_comp); /* barrier */
3362 spin_lock_irq(¤t->sighand->siglock);
3363 siginitsetinv (¤t->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
3364 recalc_sigpending();
3365 spin_unlock_irq(¤t->sighand->siglock);
3367 D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): Starting infinite loop.\n"));
3371 /* See if we need to start gc. If we don't, go to sleep.
3373 Current implementation is a BAD THING(tm). If we try
3374 to unmount the FS, the unmount operation will sleep waiting
3375 for this thread to exit. We need to arrange to send it a
3376 sig before the umount process sleeps.
3379 if (!thread_should_wake(c))
3380 set_current_state (TASK_INTERRUPTIBLE);
3382 schedule(); /* Yes, we do this even if we want to go
3383 on immediately - we're a low priority
3386 /* Put_super will send a SIGKILL and then wait on the sem.
3388 while (signal_pending(current)) {
3390 unsigned long signr = 0;
3392 if (try_to_freeze())
3395 spin_lock_irq(¤t->sighand->siglock);
3396 signr = dequeue_signal(current, ¤t->blocked, &info);
3397 spin_unlock_irq(¤t->sighand->siglock);
3401 D1(printk("jffs_garbage_collect_thread(): SIGSTOP received.\n"));
3402 set_current_state(TASK_STOPPED);
3407 D1(printk("jffs_garbage_collect_thread(): SIGKILL received.\n"));
3409 complete_and_exit(&c->gc_thread_comp, 0);
3414 D1(printk (KERN_NOTICE "jffs_garbage_collect_thread(): collecting.\n"));
3416 D3(printk (KERN_NOTICE "g_c_thread(): down biglock\n"));
3417 mutex_lock(&fmc->biglock);
3419 D1(printk("***jffs_garbage_collect_thread(): round #%u, "
3420 "fmc->dirty_size = %u\n", i++, fmc->dirty_size));
3421 D2(jffs_print_fmcontrol(fmc));
3423 if ((erased = jffs_try_to_erase(c)) < 0) {
3424 printk(KERN_WARNING "JFFS: Error in "
3425 "garbage collector: %ld.\n", erased);
3431 if (fmc->free_size == 0) {
3432 /* Argh. Might as well commit suicide. */
3433 printk(KERN_ERR "jffs_garbage_collect_thread(): free_size == 0. This is BAD.\n");
3434 send_sig(SIGQUIT, c->gc_task, 1);
3439 /* Let's dare to make a garbage collect. */
3440 if ((result = jffs_garbage_collect_next(c)) < 0) {
3441 printk(KERN_ERR "JFFS: Something "
3442 "has gone seriously wrong "
3443 "with a garbage collect: %d\n", result);
3447 D3(printk (KERN_NOTICE "g_c_thread(): up biglock\n"));
3448 mutex_unlock(&fmc->biglock);
3450 } /* jffs_garbage_collect_thread() */