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/config.h>
59 #include <linux/types.h>
60 #include <linux/slab.h>
61 #include <linux/jffs.h>
63 #include <linux/stat.h>
64 #include <linux/pagemap.h>
65 #include <asm/semaphore.h>
66 #include <asm/byteorder.h>
67 #include <linux/smp_lock.h>
68 #include <linux/time.h>
69 #include <linux/ctype.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
]);
180 #define flash_safe_acquire(arg)
181 #define flash_safe_release(arg)
185 flash_safe_read(struct mtd_info
*mtd
, loff_t from
,
186 u_char
*buf
, size_t count
)
191 D3(printk(KERN_NOTICE
"flash_safe_read(%p, %08x, %p, %08x)\n",
192 mtd
, (unsigned int) from
, buf
, count
));
194 res
= MTD_READ(mtd
, from
, count
, &retlen
, buf
);
195 if (retlen
!= count
) {
196 panic("Didn't read all bytes in flash_safe_read(). Returned %d\n", res
);
198 return res
?res
:retlen
;
203 flash_read_u32(struct mtd_info
*mtd
, loff_t from
)
209 res
= MTD_READ(mtd
, from
, 4, &retlen
, (unsigned char *)&ret
);
211 printk("Didn't read all bytes in flash_read_u32(). Returned %d\n", res
);
220 flash_safe_write(struct mtd_info
*mtd
, loff_t to
,
221 const u_char
*buf
, size_t count
)
226 D3(printk(KERN_NOTICE
"flash_safe_write(%p, %08x, %p, %08x)\n",
227 mtd
, (unsigned int) to
, buf
, count
));
229 res
= MTD_WRITE(mtd
, to
, count
, &retlen
, buf
);
230 if (retlen
!= count
) {
231 printk("Didn't write all bytes in flash_safe_write(). Returned %d\n", res
);
233 return res
?res
:retlen
;
238 flash_safe_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
239 unsigned long iovec_cnt
, loff_t to
)
241 size_t retlen
, retlen_a
;
245 D3(printk(KERN_NOTICE
"flash_safe_writev(%p, %08x, %p)\n",
246 mtd
, (unsigned int) to
, vecs
));
249 res
= MTD_WRITEV(mtd
, vecs
, iovec_cnt
, to
, &retlen
);
250 return res
? res
: retlen
;
252 /* Not implemented writev. Repeatedly use write - on the not so
253 unreasonable assumption that the mtd driver doesn't care how
254 many write cycles we use. */
258 for (i
=0; !res
&& i
<iovec_cnt
; i
++) {
259 res
= MTD_WRITE(mtd
, to
, vecs
[i
].iov_len
, &retlen_a
, vecs
[i
].iov_base
);
260 if (retlen_a
!= vecs
[i
].iov_len
) {
261 printk("Didn't write all bytes in flash_safe_writev(). Returned %d\n", res
);
262 if (i
!= iovec_cnt
-1)
265 /* If res is non-zero, retlen_a is undefined, but we don't
266 care because in that case it's not going to be
272 return res
?res
:retlen
;
277 flash_memset(struct mtd_info
*mtd
, loff_t to
,
278 const u_char c
, size_t size
)
280 static unsigned char pattern
[64];
283 /* fill up pattern */
285 for(i
= 0; i
< 64; i
++)
288 /* write as many 64-byte chunks as we can */
291 flash_safe_write(mtd
, to
, pattern
, 64);
299 flash_safe_write(mtd
, to
, pattern
, size
);
306 intrep_erase_callback(struct erase_info
*done
)
308 wait_queue_head_t
*wait_q
;
310 wait_q
= (wait_queue_head_t
*)done
->priv
;
317 flash_erase_region(struct mtd_info
*mtd
, loff_t start
,
320 struct erase_info
*erase
;
321 DECLARE_WAITQUEUE(wait
, current
);
322 wait_queue_head_t wait_q
;
324 erase
= kmalloc(sizeof(struct erase_info
), GFP_KERNEL
);
328 init_waitqueue_head(&wait_q
);
331 erase
->callback
= intrep_erase_callback
;
334 erase
->priv
= (u_long
)&wait_q
;
336 /* FIXME: Use TASK_INTERRUPTIBLE and deal with being interrupted */
337 set_current_state(TASK_UNINTERRUPTIBLE
);
338 add_wait_queue(&wait_q
, &wait
);
340 if (MTD_ERASE(mtd
, erase
) < 0) {
341 set_current_state(TASK_RUNNING
);
342 remove_wait_queue(&wait_q
, &wait
);
345 printk(KERN_WARNING
"flash: erase of region [0x%lx, 0x%lx] "
346 "totally failed\n", (long)start
, (long)start
+ size
);
351 schedule(); /* Wait for flash to finish. */
352 remove_wait_queue(&wait_q
, &wait
);
359 /* This routine calculates checksums in JFFS. */
361 jffs_checksum(const void *data
, int size
)
364 __u8
*ptr
= (__u8
*)data
;
368 D3(printk(", result: 0x%08x\n", sum
));
374 jffs_checksum_flash(struct mtd_info
*mtd
, loff_t start
, int size
, __u32
*result
)
381 /* Allocate read buffer */
382 read_buf
= (__u8
*) kmalloc (sizeof(__u8
) * 4096, GFP_KERNEL
);
384 printk(KERN_NOTICE
"kmalloc failed in jffs_checksum_flash()\n");
387 /* Loop until checksum done */
389 /* Get amount of data to read */
395 /* Perform flash read */
396 D3(printk(KERN_NOTICE
"jffs_checksum_flash\n"));
397 flash_safe_read(mtd
, ptr
, &read_buf
[0], length
);
399 /* Compute checksum */
400 for (i
=0; i
< length
; i
++)
403 /* Update pointer and size */
408 /* Free read buffer */
412 D3(printk("checksum result: 0x%08x\n", sum
));
417 static __inline__
void jffs_fm_write_lock(struct jffs_fmcontrol
*fmc
)
419 // down(&fmc->wlock);
422 static __inline__
void jffs_fm_write_unlock(struct jffs_fmcontrol
*fmc
)
428 /* Create and initialize a new struct jffs_file. */
429 static struct jffs_file
*
430 jffs_create_file(struct jffs_control
*c
,
431 const struct jffs_raw_inode
*raw_inode
)
435 if (!(f
= (struct jffs_file
*)kmalloc(sizeof(struct jffs_file
),
437 D(printk("jffs_create_file(): Failed!\n"));
441 memset(f
, 0, sizeof(struct jffs_file
));
442 f
->ino
= raw_inode
->ino
;
443 f
->pino
= raw_inode
->pino
;
444 f
->nlink
= raw_inode
->nlink
;
445 f
->deleted
= raw_inode
->deleted
;
452 /* Build a control block for the file system. */
453 static struct jffs_control
*
454 jffs_create_control(struct super_block
*sb
)
456 struct jffs_control
*c
;
457 register int s
= sizeof(struct jffs_control
);
461 D2(printk("jffs_create_control()\n"));
463 if (!(c
= (struct jffs_control
*)kmalloc(s
, GFP_KERNEL
))) {
466 DJM(no_jffs_control
++);
469 c
->hash_len
= JFFS_HASH_SIZE
;
470 s
= sizeof(struct list_head
) * c
->hash_len
;
471 if (!(c
->hash
= (struct list_head
*)kmalloc(s
, GFP_KERNEL
))) {
475 for (i
= 0; i
< c
->hash_len
; i
++)
476 INIT_LIST_HEAD(&c
->hash
[i
]);
477 if (!(c
->fmc
= jffs_build_begin(c
, MINOR(sb
->s_dev
)))) {
480 c
->next_ino
= JFFS_MIN_INO
+ 1;
481 c
->delete_list
= (struct jffs_delete_list
*) 0;
488 DJM(no_jffs_control
--);
489 D(t
= t
? t
: "c->hash");
491 D(t
= t
? t
: "control");
492 D(printk("jffs_create_control(): Allocation failed: (%s)\n", t
));
493 return (struct jffs_control
*)0;
497 /* Clean up all data structures associated with the file system. */
499 jffs_cleanup_control(struct jffs_control
*c
)
501 D2(printk("jffs_cleanup_control()\n"));
504 D(printk("jffs_cleanup_control(): c == NULL !!!\n"));
508 while (c
->delete_list
) {
509 struct jffs_delete_list
*delete_list_element
;
510 delete_list_element
= c
->delete_list
;
511 c
->delete_list
= c
->delete_list
->next
;
512 kfree(delete_list_element
);
515 /* Free all files and nodes. */
517 jffs_foreach_file(c
, jffs_free_node_list
);
518 jffs_foreach_file(c
, jffs_free_file
);
522 jffs_cleanup_fmcontrol(c
->fmc
);
524 DJM(no_jffs_control
--);
525 D3(printk("jffs_cleanup_control(): Leaving...\n"));
529 /* This function adds a virtual root node to the in-RAM representation.
530 Called by jffs_build_fs(). */
532 jffs_add_virtual_root(struct jffs_control
*c
)
534 struct jffs_file
*root
;
535 struct jffs_node
*node
;
537 D2(printk("jffs_add_virtual_root(): "
538 "Creating a virtual root directory.\n"));
540 if (!(root
= (struct jffs_file
*)kmalloc(sizeof(struct jffs_file
),
545 if (!(node
= jffs_alloc_node())) {
551 memset(node
, 0, sizeof(struct jffs_node
));
552 node
->ino
= JFFS_MIN_INO
;
553 memset(root
, 0, sizeof(struct jffs_file
));
554 root
->ino
= JFFS_MIN_INO
;
555 root
->mode
= S_IFDIR
| S_IRWXU
| S_IRGRP
556 | S_IXGRP
| S_IROTH
| S_IXOTH
;
557 root
->atime
= root
->mtime
= root
->ctime
= get_seconds();
560 root
->version_head
= root
->version_tail
= node
;
561 jffs_insert_file_into_hash(root
);
566 /* This is where the file system is built and initialized. */
568 jffs_build_fs(struct super_block
*sb
)
570 struct jffs_control
*c
;
573 D2(printk("jffs_build_fs()\n"));
575 if (!(c
= jffs_create_control(sb
))) {
580 if ((err
= jffs_scan_flash(c
)) < 0) {
582 /* scan_flash() wants us to try once more. A flipping
583 bits sector was detect in the middle of the scan flash.
584 Clean up old allocated memory before going in.
586 D1(printk("jffs_build_fs: Cleaning up all control structures,"
587 " reallocating them and trying mount again.\n"));
588 jffs_cleanup_control(c
);
589 if (!(c
= jffs_create_control(sb
))) {
595 if ((err
= jffs_scan_flash(c
)) < 0) {
596 goto jffs_build_fs_fail
;
599 goto jffs_build_fs_fail
;
603 /* Add a virtual root node if no one exists. */
604 if (!jffs_find_file(c
, JFFS_MIN_INO
)) {
605 if ((err
= jffs_add_virtual_root(c
)) < 0) {
606 goto jffs_build_fs_fail
;
610 while (c
->delete_list
) {
612 struct jffs_delete_list
*delete_list_element
;
614 if ((f
= jffs_find_file(c
, c
->delete_list
->ino
))) {
617 delete_list_element
= c
->delete_list
;
618 c
->delete_list
= c
->delete_list
->next
;
619 kfree(delete_list_element
);
622 /* Remove deleted nodes. */
623 if ((err
= jffs_foreach_file(c
, jffs_possibly_delete_file
)) < 0) {
624 printk(KERN_ERR
"JFFS: Failed to remove deleted nodes.\n");
625 goto jffs_build_fs_fail
;
627 /* Remove redundant nodes. (We are not interested in the
628 return value in this case.) */
629 jffs_foreach_file(c
, jffs_remove_redundant_nodes
);
630 /* Try to build a tree from all the nodes. */
631 if ((err
= jffs_foreach_file(c
, jffs_insert_file_into_tree
)) < 0) {
632 printk("JFFS: Failed to build tree.\n");
633 goto jffs_build_fs_fail
;
635 /* Compute the sizes of all files in the filesystem. Adjust if
637 if ((err
= jffs_foreach_file(c
, jffs_build_file
)) < 0) {
638 printk("JFFS: Failed to build file system.\n");
639 goto jffs_build_fs_fail
;
641 sb
->s_fs_info
= (void *)c
;
644 D1(jffs_print_hash_table(c
));
645 D1(jffs_print_tree(c
->root
, 0));
650 jffs_cleanup_control(c
);
652 } /* jffs_build_fs() */
656 This checks for sectors that were being erased in their previous
657 lifetimes and for some reason or the other (power fail etc.),
658 the erase cycles never completed.
659 As the flash array would have reverted back to read status,
660 these sectors are detected by the symptom of the "flipping bits",
661 i.e. bits being read back differently from the same location in
662 flash if read multiple times.
663 The only solution to this is to re-erase the entire
665 Unfortunately detecting "flipping bits" is not a simple exercise
666 as a bit may be read back at 1 or 0 depending on the alignment
667 of the stars in the universe.
668 The level of confidence is in direct proportion to the number of
669 scans done. By power fail testing I (Vipin) have been able to
670 proove that reading twice is not enough.
671 Maybe 4 times? Change NUM_REREADS to a higher number if you want
672 a (even) higher degree of confidence in your mount process.
673 A higher number would of course slow down your mount.
675 static int check_partly_erased_sectors(struct jffs_fmcontrol
*fmc
){
677 #define NUM_REREADS 4 /* see note above */
678 #define READ_AHEAD_BYTES 4096 /* must be a multiple of 4,
679 usually set to kernel page size */
690 loff_t end
= fmc
->flash_size
;
693 /* Allocate read buffers */
694 read_buf1
= (__u8
*) kmalloc (sizeof(__u8
) * READ_AHEAD_BYTES
, GFP_KERNEL
);
698 read_buf2
= (__u8
*) kmalloc (sizeof(__u8
) * READ_AHEAD_BYTES
, GFP_KERNEL
);
707 D1(printk("check_partly_erased_sector():checking sector which contains"
708 " offset 0x%x for flipping bits..\n", (__u32
)pos
));
710 retlen
= flash_safe_read(fmc
->mtd
, pos
,
711 &read_buf1
[0], READ_AHEAD_BYTES
);
714 for(cnt
= 0; cnt
< NUM_REREADS
; cnt
++){
715 (void)flash_safe_read(fmc
->mtd
, pos
,
716 &read_buf2
[0], READ_AHEAD_BYTES
);
718 for (i
=0 ; i
< retlen
; i
+=4) {
719 /* buffers MUST match, double word for word! */
720 if(*((__u32
*) &read_buf1
[i
]) !=
721 *((__u32
*) &read_buf2
[i
])
723 /* flipping bits detected, time to erase sector */
724 /* This will help us log some statistics etc. */
725 D1(printk("Flipping bits detected in re-read round:%i of %i\n",
727 D1(printk("check_partly_erased_sectors:flipping bits detected"
728 " @offset:0x%x(0x%x!=0x%x)\n",
729 (__u32
)pos
+i
, *((__u32
*) &read_buf1
[i
]),
730 *((__u32
*) &read_buf2
[i
])));
732 /* calculate start of present sector */
733 offset
= (((__u32
)pos
+i
)/(__u32
)fmc
->sector_size
) * (__u32
)fmc
->sector_size
;
735 D1(printk("check_partly_erased_sector():erasing sector starting 0x%x.\n",
738 if (flash_erase_region(fmc
->mtd
,
739 offset
, fmc
->sector_size
) < 0) {
740 printk(KERN_ERR
"JFFS: Erase of flash failed. "
741 "offset = %u, erase_size = %d\n",
742 offset
, fmc
->sector_size
);
748 D1(printk("JFFS: Erase of flash sector @0x%x successful.\n",
750 /* skip ahead to the next sector */
751 pos
= (((__u32
)pos
+i
)/(__u32
)fmc
->sector_size
) * (__u32
)fmc
->sector_size
;
752 pos
+= fmc
->sector_size
;
758 pos
+= READ_AHEAD_BYTES
;
765 D2(printk("check_partly_erased_sector():Done checking all sectors till offset 0x%x for flipping bits.\n",
770 }/* end check_partly_erased_sectors() */
774 /* Scan the whole flash memory in order to find all nodes in the
777 jffs_scan_flash(struct jffs_control
*c
)
779 char name
[JFFS_MAX_NAME_LEN
+ 2];
780 struct jffs_raw_inode raw_inode
;
781 struct jffs_node
*node
= NULL
;
782 struct jffs_fmcontrol
*fmc
= c
->fmc
;
790 loff_t end
= fmc
->flash_size
;
795 __u32 free_chunk_size1
;
796 __u32 free_chunk_size2
;
799 #define NUMFREEALLOWED 2 /* 2 chunks of at least erase size space allowed */
800 int num_free_space
= 0; /* Flag err if more than TWO
801 free blocks found. This is NOT allowed
802 by the current jffs design.
804 int num_free_spc_not_accp
= 0; /* For debugging purposed keep count
805 of how much free space was rejected and
809 D1(printk("jffs_scan_flash(): start pos = 0x%lx, end = 0x%lx\n",
810 (long)pos
, (long)end
));
812 flash_safe_acquire(fmc
->mtd
);
815 check and make sure that any sector does not suffer
816 from the "partly erased, bit flipping syndrome" (TM Vipin :)
817 If so, offending sectors will be erased.
819 if(check_partly_erased_sectors(fmc
) < 0){
821 flash_safe_release(fmc
->mtd
);
822 return -EIO
; /* bad, bad, bad error. Cannot continue.*/
825 /* Allocate read buffer */
826 read_buf
= (__u8
*) kmalloc (sizeof(__u8
) * 4096, GFP_KERNEL
);
828 flash_safe_release(fmc
->mtd
);
832 /* Start the scan. */
836 /* Remember the position from where we started this scan. */
839 switch (flash_read_u32(fmc
->mtd
, pos
)) {
840 case JFFS_EMPTY_BITMASK
:
841 /* We have found 0xffffffff at this position. We have to
842 scan the rest of the flash till the end or till
843 something else than 0xffffffff is found.
844 Keep going till we do not find JFFS_EMPTY_BITMASK
847 D1(printk("jffs_scan_flash(): 0xffffffff at pos 0x%lx.\n",
852 len
= end
- pos
< 4096 ? end
- pos
: 4096;
854 retlen
= flash_safe_read(fmc
->mtd
, pos
,
859 for (i
=0 ; i
< retlen
; i
+=4, pos
+= 4) {
860 if(*((__u32
*) &read_buf
[i
]) !=
870 D1(printk("jffs_scan_flash():0xffffffff ended at pos 0x%lx.\n",
873 /* If some free space ends in the middle of a sector,
874 treat it as dirty rather than clean.
875 This is to handle the case where one thread
876 allocated space for a node, but didn't get to
877 actually _write_ it before power was lost, leaving
878 a gap in the log. Shifting all node writes into
879 a single kernel thread will fix the original problem.
881 if ((__u32
) pos
% fmc
->sector_size
) {
882 /* If there was free space in previous
883 sectors, don't mark that dirty too -
884 only from the beginning of this sector
888 test_start
= pos
& ~(fmc
->sector_size
-1); /* end of last sector */
890 if (start
< test_start
) {
892 /* free space started in the previous sector! */
894 if((num_free_space
< NUMFREEALLOWED
) &&
895 ((unsigned int)(test_start
- start
) >= fmc
->sector_size
)){
898 Count it in if we are still under NUMFREEALLOWED *and* it is
899 at least 1 erase sector in length. This will keep us from
900 picking any little ole' space as "free".
903 D1(printk("Reducing end of free space to 0x%x from 0x%x\n",
904 (unsigned int)test_start
, (unsigned int)pos
));
906 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
907 (unsigned int) start
,
908 (unsigned int)(test_start
- start
)));
910 /* below, space from "start" to "pos" will be marked dirty. */
913 /* Being in here means that we have found at least an entire
914 erase sector size of free space ending on a sector boundary.
915 Keep track of free spaces accepted.
919 num_free_spc_not_accp
++;
920 D1(printk("Free space (#%i) found but *Not* accepted: Starting"
921 " 0x%x for 0x%x bytes\n",
922 num_free_spc_not_accp
, (unsigned int)start
,
923 (unsigned int)((unsigned int)(pos
& ~(fmc
->sector_size
-1)) - (unsigned int)start
)));
928 if((((__u32
)(pos
- start
)) != 0)){
930 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
931 (unsigned int) start
, (unsigned int) (pos
- start
)));
932 jffs_fmalloced(fmc
, (__u32
) start
,
933 (__u32
) (pos
- start
), NULL
);
935 /* "Flipping bits" detected. This means that our scan for them
936 did not catch this offset. See check_partly_erased_sectors() for
940 D1(printk("jffs_scan_flash():wants to allocate dirty flash "
941 "space for 0 bytes.\n"));
942 D1(printk("jffs_scan_flash(): Flipping bits! We will free "
943 "all allocated memory, erase this sector and remount\n"));
945 /* calculate start of present sector */
946 offset
= (((__u32
)pos
)/(__u32
)fmc
->sector_size
) * (__u32
)fmc
->sector_size
;
948 D1(printk("jffs_scan_flash():erasing sector starting 0x%x.\n",
951 if (flash_erase_region(fmc
->mtd
,
952 offset
, fmc
->sector_size
) < 0) {
953 printk(KERN_ERR
"JFFS: Erase of flash failed. "
954 "offset = %u, erase_size = %d\n",
955 offset
, fmc
->sector_size
);
957 flash_safe_release(fmc
->mtd
);
959 return -1; /* bad, bad, bad! */
962 flash_safe_release(fmc
->mtd
);
965 return -EAGAIN
; /* erased offending sector. Try mount one more time please. */
968 /* Being in here means that we have found free space that ends on an erase sector
970 Count it in if we are still under NUMFREEALLOWED *and* it is at least 1 erase
971 sector in length. This will keep us from picking any little ole' space as "free".
973 if((num_free_space
< NUMFREEALLOWED
) &&
974 ((unsigned int)(pos
- start
) >= fmc
->sector_size
)){
975 /* We really don't do anything to mark space as free, except *not*
976 mark it dirty and just advance the "pos" location pointer.
977 It will automatically be picked up as free space.
980 D1(printk("Free space accepted: Starting 0x%x for 0x%x bytes\n",
981 (unsigned int) start
, (unsigned int) (pos
- start
)));
983 num_free_spc_not_accp
++;
984 D1(printk("Free space (#%i) found but *Not* accepted: Starting "
985 "0x%x for 0x%x bytes\n", num_free_spc_not_accp
,
986 (unsigned int) start
,
987 (unsigned int) (pos
- start
)));
989 /* Mark this space as dirty. We already have our free space. */
990 D1(printk("Dirty space: Starting 0x%x for 0x%x bytes\n",
991 (unsigned int) start
, (unsigned int) (pos
- start
)));
992 jffs_fmalloced(fmc
, (__u32
) start
,
993 (__u32
) (pos
- start
), NULL
);
997 if(num_free_space
> NUMFREEALLOWED
){
998 printk(KERN_WARNING
"jffs_scan_flash(): Found free space "
999 "number %i. Only %i free space is allowed.\n",
1000 num_free_space
, NUMFREEALLOWED
);
1004 case JFFS_DIRTY_BITMASK
:
1005 /* We have found 0x00000000 at this position. Scan as far
1006 as possible to find out how much is dirty. */
1007 D1(printk("jffs_scan_flash(): 0x00000000 at pos 0x%lx.\n",
1010 && JFFS_DIRTY_BITMASK
== flash_read_u32(fmc
->mtd
, pos
);
1012 D1(printk("jffs_scan_flash(): 0x00 ended at "
1013 "pos 0x%lx.\n", (long)pos
));
1014 jffs_fmalloced(fmc
, (__u32
) start
,
1015 (__u32
) (pos
- start
), NULL
);
1018 case JFFS_MAGIC_BITMASK
:
1019 /* We have probably found a new raw inode. */
1024 /* We're f*cked. This is not solved yet. We have
1025 to scan for the magic pattern. */
1026 D1(printk("*************** Dirty flash memory or "
1028 "hexdump(pos = 0x%lx, len = 128):\n",
1030 D1(jffs_hexdump(fmc
->mtd
, pos
, 128));
1032 for (pos
+= 4; pos
< end
; pos
+= 4) {
1033 switch (flash_read_u32(fmc
->mtd
, pos
)) {
1034 case JFFS_MAGIC_BITMASK
:
1035 case JFFS_EMPTY_BITMASK
:
1036 /* handle these in the main switch() loop */
1045 /* First, mark as dirty the region
1046 which really does contain crap. */
1047 jffs_fmalloced(fmc
, (__u32
) start
,
1048 (__u32
) (pos
- start
),
1054 /* We have found the beginning of an inode. Create a
1055 node for it unless there already is one available. */
1057 if (!(node
= jffs_alloc_node())) {
1058 /* Free read buffer */
1061 /* Release the flash device */
1062 flash_safe_release(fmc
->mtd
);
1066 DJM(no_jffs_node
++);
1069 /* Read the next raw inode. */
1071 flash_safe_read(fmc
->mtd
, pos
, (u_char
*) &raw_inode
,
1072 sizeof(struct jffs_raw_inode
));
1074 /* When we compute the checksum for the inode, we never
1075 count the 'accurate' or the 'checksum' fields. */
1076 tmp_accurate
= raw_inode
.accurate
;
1077 tmp_chksum
= raw_inode
.chksum
;
1078 raw_inode
.accurate
= 0;
1079 raw_inode
.chksum
= 0;
1080 checksum
= jffs_checksum(&raw_inode
,
1081 sizeof(struct jffs_raw_inode
));
1082 raw_inode
.accurate
= tmp_accurate
;
1083 raw_inode
.chksum
= tmp_chksum
;
1085 D3(printk("*** We have found this raw inode at pos 0x%lx "
1086 "on the flash:\n", (long)pos
));
1087 D3(jffs_print_raw_inode(&raw_inode
));
1089 if (checksum
!= raw_inode
.chksum
) {
1090 D1(printk("jffs_scan_flash(): Bad checksum: "
1092 "raw_inode.chksum = %u\n",
1093 checksum
, raw_inode
.chksum
));
1094 pos
+= sizeof(struct jffs_raw_inode
);
1095 jffs_fmalloced(fmc
, (__u32
) start
,
1096 (__u32
) (pos
- start
), NULL
);
1097 /* Reuse this unused struct jffs_node. */
1101 /* Check the raw inode read so far. Start with the
1102 maximum length of the filename. */
1103 if (raw_inode
.nsize
> JFFS_MAX_NAME_LEN
) {
1104 printk(KERN_WARNING
"jffs_scan_flash: Found a "
1105 "JFFS node with name too large\n");
1109 if (raw_inode
.rename
&& raw_inode
.dsize
!= sizeof(__u32
)) {
1110 printk(KERN_WARNING
"jffs_scan_flash: Found a "
1111 "rename node with dsize %u.\n",
1113 jffs_print_raw_inode(&raw_inode
);
1117 /* The node's data segment should not exceed a
1119 if (raw_inode
.dsize
> fmc
->max_chunk_size
) {
1120 printk(KERN_WARNING
"jffs_scan_flash: Found a "
1121 "JFFS node with dsize (0x%x) > max_chunk_size (0x%x)\n",
1122 raw_inode
.dsize
, fmc
->max_chunk_size
);
1126 pos
+= sizeof(struct jffs_raw_inode
);
1128 /* This shouldn't be necessary because a node that
1129 violates the flash boundaries shouldn't be written
1130 in the first place. */
1135 /* Read the name. */
1137 if (raw_inode
.nsize
) {
1138 flash_safe_read(fmc
->mtd
, pos
, name
, raw_inode
.nsize
);
1139 name
[raw_inode
.nsize
] = '\0';
1140 pos
+= raw_inode
.nsize
1141 + JFFS_GET_PAD_BYTES(raw_inode
.nsize
);
1142 D3(printk("name == \"%s\"\n", name
));
1143 checksum
= jffs_checksum(name
, raw_inode
.nsize
);
1144 if (checksum
!= raw_inode
.nchksum
) {
1145 D1(printk("jffs_scan_flash(): Bad checksum: "
1147 "raw_inode.nchksum = %u\n",
1148 checksum
, raw_inode
.nchksum
));
1149 jffs_fmalloced(fmc
, (__u32
) start
,
1150 (__u32
) (pos
- start
), NULL
);
1151 /* Reuse this unused struct jffs_node. */
1159 /* Read the data, if it exists, in order to be sure it
1160 matches the checksum. */
1161 if (raw_inode
.dsize
) {
1162 if (raw_inode
.rename
) {
1163 deleted_file
= flash_read_u32(fmc
->mtd
, pos
);
1165 if (jffs_checksum_flash(fmc
->mtd
, pos
, raw_inode
.dsize
, &checksum
)) {
1166 printk("jffs_checksum_flash() failed to calculate a checksum\n");
1167 jffs_fmalloced(fmc
, (__u32
) start
,
1168 (__u32
) (pos
- start
), NULL
);
1169 /* Reuse this unused struct jffs_node. */
1172 pos
+= raw_inode
.dsize
1173 + JFFS_GET_PAD_BYTES(raw_inode
.dsize
);
1175 if (checksum
!= raw_inode
.dchksum
) {
1176 D1(printk("jffs_scan_flash(): Bad checksum: "
1178 "raw_inode.dchksum = %u\n",
1179 checksum
, raw_inode
.dchksum
));
1180 jffs_fmalloced(fmc
, (__u32
) start
,
1181 (__u32
) (pos
- start
), NULL
);
1182 /* Reuse this unused struct jffs_node. */
1189 /* Remember the highest inode number in the whole file
1190 system. This information will be used when assigning
1191 new files new inode numbers. */
1192 if (c
->next_ino
<= raw_inode
.ino
) {
1193 c
->next_ino
= raw_inode
.ino
+ 1;
1196 if (raw_inode
.accurate
) {
1198 node
->data_offset
= raw_inode
.offset
;
1199 node
->data_size
= raw_inode
.dsize
;
1200 node
->removed_size
= raw_inode
.rsize
;
1201 /* Compute the offset to the actual data in the
1204 = sizeof(struct jffs_raw_inode
)
1206 + JFFS_GET_PAD_BYTES(raw_inode
.nsize
);
1207 node
->fm
= jffs_fmalloced(fmc
, (__u32
) start
,
1208 (__u32
) (pos
- start
),
1211 D(printk("jffs_scan_flash(): !node->fm\n"));
1212 jffs_free_node(node
);
1213 DJM(no_jffs_node
--);
1215 /* Free read buffer */
1218 /* Release the flash device */
1219 flash_safe_release(fmc
->mtd
);
1223 if ((err
= jffs_insert_node(c
, NULL
, &raw_inode
,
1225 printk("JFFS: Failed to handle raw inode. "
1226 "(err = %d)\n", err
);
1229 if (raw_inode
.rename
) {
1230 struct jffs_delete_list
*dl
1231 = (struct jffs_delete_list
*)
1232 kmalloc(sizeof(struct jffs_delete_list
),
1235 D(printk("jffs_scan_flash: !dl\n"));
1236 jffs_free_node(node
);
1237 DJM(no_jffs_node
--);
1239 /* Release the flash device */
1240 flash_safe_release(fmc
->flash_part
);
1242 /* Free read buffer */
1247 dl
->ino
= deleted_file
;
1248 dl
->next
= c
->delete_list
;
1249 c
->delete_list
= dl
;
1250 node
->data_size
= 0;
1252 D3(jffs_print_node(node
));
1253 node
= NULL
; /* Don't free the node! */
1256 jffs_fmalloced(fmc
, (__u32
) start
,
1257 (__u32
) (pos
- start
), NULL
);
1258 D3(printk("jffs_scan_flash(): Just found an obsolete "
1259 "raw_inode. Continuing the scan...\n"));
1260 /* Reuse this unused struct jffs_node. */
1265 jffs_free_node(node
);
1266 DJM(no_jffs_node
--);
1268 jffs_build_end(fmc
);
1270 /* Free read buffer */
1273 if(!num_free_space
){
1274 printk(KERN_WARNING
"jffs_scan_flash(): Did not find even a single "
1275 "chunk of free space. This is BAD!\n");
1279 D3(printk("jffs_scan_flash(): Leaving...\n"));
1280 flash_safe_release(fmc
->mtd
);
1282 /* This is to trap the "free size accounting screwed error. */
1283 free_chunk_size1
= jffs_free_size1(fmc
);
1284 free_chunk_size2
= jffs_free_size2(fmc
);
1286 if (free_chunk_size1
+ free_chunk_size2
!= fmc
->free_size
) {
1288 printk(KERN_WARNING
"jffs_scan_falsh():Free size accounting screwed\n");
1289 printk(KERN_WARNING
"jfffs_scan_flash():free_chunk_size1 == 0x%x, "
1290 "free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n",
1291 free_chunk_size1
, free_chunk_size2
, fmc
->free_size
);
1293 return -1; /* Do NOT mount f/s so that we can inspect what happened.
1294 Mounting this screwed up f/s will screw us up anyway.
1298 return 0; /* as far as we are concerned, we are happy! */
1299 } /* jffs_scan_flash() */
1302 /* Insert any kind of node into the file system. Take care of data
1303 insertions and deletions. Also remove redundant information. The
1304 memory allocated for the `name' is regarded as "given away" in the
1305 caller's perspective. */
1307 jffs_insert_node(struct jffs_control
*c
, struct jffs_file
*f
,
1308 const struct jffs_raw_inode
*raw_inode
,
1309 const char *name
, struct jffs_node
*node
)
1311 int update_name
= 0;
1312 int insert_into_tree
= 0;
1314 D2(printk("jffs_insert_node(): ino = %u, version = %u, "
1315 "name = \"%s\", deleted = %d\n",
1316 raw_inode
->ino
, raw_inode
->version
,
1317 ((name
&& *name
) ? name
: ""), raw_inode
->deleted
));
1319 /* If there doesn't exist an associated jffs_file, then
1320 create, initialize and insert one into the file system. */
1321 if (!f
&& !(f
= jffs_find_file(c
, raw_inode
->ino
))) {
1322 if (!(f
= jffs_create_file(c
, raw_inode
))) {
1325 jffs_insert_file_into_hash(f
);
1326 insert_into_tree
= 1;
1328 node
->ino
= raw_inode
->ino
;
1329 node
->version
= raw_inode
->version
;
1330 node
->data_size
= raw_inode
->dsize
;
1331 node
->fm_offset
= sizeof(struct jffs_raw_inode
) + raw_inode
->nsize
1332 + JFFS_GET_PAD_BYTES(raw_inode
->nsize
);
1333 node
->name_size
= raw_inode
->nsize
;
1335 /* Now insert the node at the correct position into the file's
1337 if (!f
->version_head
) {
1338 /* This is the first node. */
1339 f
->version_head
= node
;
1340 f
->version_tail
= node
;
1341 node
->version_prev
= NULL
;
1342 node
->version_next
= NULL
;
1343 f
->highest_version
= node
->version
;
1345 f
->mode
= raw_inode
->mode
;
1346 f
->uid
= raw_inode
->uid
;
1347 f
->gid
= raw_inode
->gid
;
1348 f
->atime
= raw_inode
->atime
;
1349 f
->mtime
= raw_inode
->mtime
;
1350 f
->ctime
= raw_inode
->ctime
;
1352 else if ((f
->highest_version
< node
->version
)
1353 || (node
->version
== 0)) {
1354 /* Insert at the end of the list. I.e. this node is the
1355 newest one so far. */
1356 node
->version_prev
= f
->version_tail
;
1357 node
->version_next
= NULL
;
1358 f
->version_tail
->version_next
= node
;
1359 f
->version_tail
= node
;
1360 f
->highest_version
= node
->version
;
1362 f
->pino
= raw_inode
->pino
;
1363 f
->mode
= raw_inode
->mode
;
1364 f
->uid
= raw_inode
->uid
;
1365 f
->gid
= raw_inode
->gid
;
1366 f
->atime
= raw_inode
->atime
;
1367 f
->mtime
= raw_inode
->mtime
;
1368 f
->ctime
= raw_inode
->ctime
;
1370 else if (f
->version_head
->version
> node
->version
) {
1371 /* Insert at the bottom of the list. */
1372 node
->version_prev
= NULL
;
1373 node
->version_next
= f
->version_head
;
1374 f
->version_head
->version_prev
= node
;
1375 f
->version_head
= node
;
1381 struct jffs_node
*n
;
1383 /* Search for the insertion position starting from
1384 the tail (newest node). */
1385 for (n
= f
->version_tail
; n
; n
= n
->version_prev
) {
1386 if (n
->version
< node
->version
) {
1387 node
->version_prev
= n
;
1388 node
->version_next
= n
->version_next
;
1389 node
->version_next
->version_prev
= node
;
1390 n
->version_next
= node
;
1402 /* Deletion is irreversible. If any 'deleted' node is ever
1403 written, the file is deleted */
1404 if (raw_inode
->deleted
)
1405 f
->deleted
= raw_inode
->deleted
;
1407 /* Perhaps update the name. */
1408 if (raw_inode
->nsize
&& update_name
&& name
&& *name
&& (name
!= f
->name
)) {
1413 if (!(f
->name
= (char *) kmalloc(raw_inode
->nsize
+ 1,
1418 memcpy(f
->name
, name
, raw_inode
->nsize
);
1419 f
->name
[raw_inode
->nsize
] = '\0';
1420 f
->nsize
= raw_inode
->nsize
;
1421 D3(printk("jffs_insert_node(): Updated the name of "
1422 "the file to \"%s\".\n", name
));
1425 if (!c
->building_fs
) {
1426 D3(printk("jffs_insert_node(): ---------------------------"
1427 "------------------------------------------- 1\n"));
1428 if (insert_into_tree
) {
1429 jffs_insert_file_into_tree(f
);
1431 /* Once upon a time, we would call jffs_possibly_delete_file()
1432 here. That causes an oops if someone's still got the file
1433 open, so now we only do it in jffs_delete_inode()
1436 if (node
->data_size
|| node
->removed_size
) {
1437 jffs_update_file(f
, node
);
1439 jffs_remove_redundant_nodes(f
);
1441 jffs_garbage_collect_trigger(c
);
1443 D3(printk("jffs_insert_node(): ---------------------------"
1444 "------------------------------------------- 2\n"));
1448 } /* jffs_insert_node() */
1451 /* Unlink a jffs_node from the version list it is in. */
1453 jffs_unlink_node_from_version_list(struct jffs_file
*f
,
1454 struct jffs_node
*node
)
1456 if (node
->version_prev
) {
1457 node
->version_prev
->version_next
= node
->version_next
;
1459 f
->version_head
= node
->version_next
;
1461 if (node
->version_next
) {
1462 node
->version_next
->version_prev
= node
->version_prev
;
1464 f
->version_tail
= node
->version_prev
;
1469 /* Unlink a jffs_node from the range list it is in. */
1471 jffs_unlink_node_from_range_list(struct jffs_file
*f
, struct jffs_node
*node
)
1473 if (node
->range_prev
) {
1474 node
->range_prev
->range_next
= node
->range_next
;
1477 f
->range_head
= node
->range_next
;
1479 if (node
->range_next
) {
1480 node
->range_next
->range_prev
= node
->range_prev
;
1483 f
->range_tail
= node
->range_prev
;
1488 /* Function used by jffs_remove_redundant_nodes() below. This function
1489 classifies what kind of information a node adds to a file. */
1491 jffs_classify_node(struct jffs_node
*node
)
1493 __u8 mod_type
= JFFS_MODIFY_INODE
;
1495 if (node
->name_size
) {
1496 mod_type
|= JFFS_MODIFY_NAME
;
1498 if (node
->data_size
|| node
->removed_size
) {
1499 mod_type
|= JFFS_MODIFY_DATA
;
1505 /* Remove redundant nodes from a file. Mark the on-flash memory
1508 jffs_remove_redundant_nodes(struct jffs_file
*f
)
1510 struct jffs_node
*newest_node
;
1511 struct jffs_node
*cur
;
1512 struct jffs_node
*prev
;
1515 __u8 node_with_name_later
= 0;
1517 if (!(newest_node
= f
->version_tail
)) {
1521 /* What does the `newest_node' modify? */
1522 newest_type
= jffs_classify_node(newest_node
);
1523 node_with_name_later
= newest_type
& JFFS_MODIFY_NAME
;
1525 D3(printk("jffs_remove_redundant_nodes(): ino: %u, name: \"%s\", "
1526 "newest_type: %u\n", f
->ino
, (f
->name
? f
->name
: ""),
1529 /* Traverse the file's nodes and determine which of them that are
1530 superfluous. Yeah, this might look very complex at first
1531 glance but it is actually very simple. */
1532 for (cur
= newest_node
->version_prev
; cur
; cur
= prev
) {
1533 prev
= cur
->version_prev
;
1534 mod_type
= jffs_classify_node(cur
);
1535 if ((mod_type
<= JFFS_MODIFY_INODE
)
1536 || ((newest_type
& JFFS_MODIFY_NAME
)
1538 <= (JFFS_MODIFY_INODE
+ JFFS_MODIFY_NAME
)))
1539 || (cur
->data_size
== 0 && cur
->removed_size
1540 && !cur
->version_prev
&& node_with_name_later
)) {
1541 /* Yes, this node is redundant. Remove it. */
1542 D2(printk("jffs_remove_redundant_nodes(): "
1543 "Removing node: ino: %u, version: %u, "
1544 "mod_type: %u\n", cur
->ino
, cur
->version
,
1546 jffs_unlink_node_from_version_list(f
, cur
);
1547 jffs_fmfree(f
->c
->fmc
, cur
->fm
, cur
);
1548 jffs_free_node(cur
);
1549 DJM(no_jffs_node
--);
1552 node_with_name_later
|= (mod_type
& JFFS_MODIFY_NAME
);
1560 /* Insert a file into the hash table. */
1562 jffs_insert_file_into_hash(struct jffs_file
*f
)
1564 int i
= f
->ino
% f
->c
->hash_len
;
1566 D3(printk("jffs_insert_file_into_hash(): f->ino: %u\n", f
->ino
));
1568 list_add(&f
->hash
, &f
->c
->hash
[i
]);
1573 /* Insert a file into the file system tree. */
1575 jffs_insert_file_into_tree(struct jffs_file
*f
)
1577 struct jffs_file
*parent
;
1579 D3(printk("jffs_insert_file_into_tree(): name: \"%s\"\n",
1580 (f
->name
? f
->name
: "")));
1582 if (!(parent
= jffs_find_file(f
->c
, f
->pino
))) {
1586 f
->sibling_prev
= NULL
;
1587 f
->sibling_next
= NULL
;
1591 D1(printk("jffs_insert_file_into_tree(): Found "
1592 "inode with no parent and pino == %u\n",
1598 f
->sibling_next
= parent
->children
;
1599 if (f
->sibling_next
) {
1600 f
->sibling_next
->sibling_prev
= f
;
1602 f
->sibling_prev
= NULL
;
1603 parent
->children
= f
;
1608 /* Remove a file from the hash table. */
1610 jffs_unlink_file_from_hash(struct jffs_file
*f
)
1612 D3(printk("jffs_unlink_file_from_hash(): f: 0x%p, "
1613 "ino %u\n", f
, f
->ino
));
1620 /* Just remove the file from the parent's children. Don't free
1623 jffs_unlink_file_from_tree(struct jffs_file
*f
)
1625 D3(printk("jffs_unlink_file_from_tree(): ino: %d, pino: %d, name: "
1626 "\"%s\"\n", f
->ino
, f
->pino
, (f
->name
? f
->name
: "")));
1628 if (f
->sibling_prev
) {
1629 f
->sibling_prev
->sibling_next
= f
->sibling_next
;
1631 else if (f
->parent
) {
1632 D3(printk("f->parent=%p\n", f
->parent
));
1633 f
->parent
->children
= f
->sibling_next
;
1635 if (f
->sibling_next
) {
1636 f
->sibling_next
->sibling_prev
= f
->sibling_prev
;
1642 /* Find a file with its inode number. */
1644 jffs_find_file(struct jffs_control
*c
, __u32 ino
)
1646 struct jffs_file
*f
;
1647 int i
= ino
% c
->hash_len
;
1648 struct list_head
*tmp
;
1650 D3(printk("jffs_find_file(): ino: %u\n", ino
));
1652 for (tmp
= c
->hash
[i
].next
; tmp
!= &c
->hash
[i
]; tmp
= tmp
->next
) {
1653 f
= list_entry(tmp
, struct jffs_file
, hash
);
1656 D3(printk("jffs_find_file(): Found file with ino "
1657 "%u. (name: \"%s\")\n",
1658 ino
, (f
->name
? f
->name
: ""));
1662 D3(printk("jffs_find_file(): Didn't find file "
1663 "with ino %u.\n", ino
);
1669 /* Find a file in a directory. We are comparing the names. */
1671 jffs_find_child(struct jffs_file
*dir
, const char *name
, int len
)
1673 struct jffs_file
*f
;
1675 D3(printk("jffs_find_child()\n"));
1677 for (f
= dir
->children
; f
; f
= f
->sibling_next
) {
1678 if (!f
->deleted
&& f
->name
1679 && !strncmp(f
->name
, name
, len
)
1680 && f
->name
[len
] == '\0') {
1686 printk("jffs_find_child(): Found \"%s\".\n", f
->name
);
1689 char *copy
= (char *) kmalloc(len
+ 1, GFP_KERNEL
);
1691 memcpy(copy
, name
, len
);
1694 printk("jffs_find_child(): Didn't find the file \"%s\".\n",
1695 (copy
? copy
: ""));
1705 /* Write a raw inode that takes up a certain amount of space in the flash
1706 memory. At the end of the flash device, there is often space that is
1707 impossible to use. At these times we want to mark this space as not
1708 used. In the cases when the amount of space is greater or equal than
1709 a struct jffs_raw_inode, we write a "dummy node" that takes up this
1710 space. The space after the raw inode, if it exists, is left as it is.
1711 Since this space after the raw inode contains JFFS_EMPTY_BITMASK bytes,
1712 we can compute the checksum of it; we don't have to manipulate it any
1715 If the space left on the device is less than the size of a struct
1716 jffs_raw_inode, this space is filled with JFFS_DIRTY_BITMASK bytes.
1717 No raw inode is written this time. */
1719 jffs_write_dummy_node(struct jffs_control
*c
, struct jffs_fm
*dirty_fm
)
1721 struct jffs_fmcontrol
*fmc
= c
->fmc
;
1724 D1(printk("jffs_write_dummy_node(): dirty_fm->offset = 0x%08x, "
1725 "dirty_fm->size = %u\n",
1726 dirty_fm
->offset
, dirty_fm
->size
));
1728 if (dirty_fm
->size
>= sizeof(struct jffs_raw_inode
)) {
1729 struct jffs_raw_inode raw_inode
;
1730 memset(&raw_inode
, 0, sizeof(struct jffs_raw_inode
));
1731 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
1732 raw_inode
.dsize
= dirty_fm
->size
1733 - sizeof(struct jffs_raw_inode
);
1734 raw_inode
.dchksum
= raw_inode
.dsize
* 0xff;
1736 = jffs_checksum(&raw_inode
, sizeof(struct jffs_raw_inode
));
1738 if ((err
= flash_safe_write(fmc
->mtd
,
1740 (u_char
*)&raw_inode
,
1741 sizeof(struct jffs_raw_inode
)))
1743 printk(KERN_ERR
"JFFS: jffs_write_dummy_node: "
1744 "flash_safe_write failed!\n");
1749 flash_safe_acquire(fmc
->mtd
);
1750 flash_memset(fmc
->mtd
, dirty_fm
->offset
, 0, dirty_fm
->size
);
1751 flash_safe_release(fmc
->mtd
);
1754 D3(printk("jffs_write_dummy_node(): Leaving...\n"));
1759 /* Write a raw inode, possibly its name and possibly some data. */
1761 jffs_write_node(struct jffs_control
*c
, struct jffs_node
*node
,
1762 struct jffs_raw_inode
*raw_inode
,
1763 const char *name
, const unsigned char *data
,
1765 struct jffs_file
*f
)
1767 struct jffs_fmcontrol
*fmc
= c
->fmc
;
1769 struct kvec node_iovec
[4];
1770 unsigned long iovec_cnt
;
1776 __u32 total_name_size
= raw_inode
->nsize
1777 + JFFS_GET_PAD_BYTES(raw_inode
->nsize
);
1778 __u32 total_data_size
= raw_inode
->dsize
1779 + JFFS_GET_PAD_BYTES(raw_inode
->dsize
);
1780 __u32 total_size
= sizeof(struct jffs_raw_inode
)
1781 + total_name_size
+ total_data_size
;
1783 /* If this node isn't something that will eventually let
1784 GC free even more space, then don't allow it unless
1785 there's at least max_chunk_size space still available
1788 slack
= fmc
->max_chunk_size
;
1791 /* Fire the retrorockets and shoot the fruiton torpedoes, sir! */
1794 printk("jffs_write_node(): node == NULL\n");
1797 ASSERT(if (raw_inode
&& raw_inode
->nsize
&& !name
) {
1798 printk("*** jffs_write_node(): nsize = %u but name == NULL\n",
1803 D1(printk("jffs_write_node(): filename = \"%s\", ino = %u, "
1804 "total_size = %u\n",
1805 (name
? name
: ""), raw_inode
->ino
,
1808 jffs_fm_write_lock(fmc
);
1815 /* Deadlocks suck. */
1816 while(fmc
->free_size
< fmc
->min_free_size
+ total_size
+ slack
) {
1817 jffs_fm_write_unlock(fmc
);
1818 if (!JFFS_ENOUGH_SPACE(c
, total_size
+ slack
))
1820 jffs_fm_write_lock(fmc
);
1823 /* First try to allocate some flash memory. */
1824 err
= jffs_fmalloc(fmc
, total_size
, node
, &fm
);
1826 if (err
== -ENOSPC
) {
1827 /* Just out of space. GC and try again */
1828 if (fmc
->dirty_size
< fmc
->sector_size
) {
1829 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1830 "failed, no dirty space to GC\n", fmc
,
1835 D1(printk(KERN_INFO
"jffs_write_node(): Calling jffs_garbage_collect_now()\n"));
1836 jffs_fm_write_unlock(fmc
);
1837 if ((err
= jffs_garbage_collect_now(c
))) {
1838 D(printk("jffs_write_node(): jffs_garbage_collect_now() failed\n"));
1841 jffs_fm_write_lock(fmc
);
1846 jffs_fm_write_unlock(fmc
);
1848 D(printk("jffs_write_node(): jffs_fmalloc(0x%p, %u) "
1849 "failed!\n", fmc
, total_size
));
1854 /* The jffs_fm struct that we got is not good enough.
1855 Make that space dirty and try again */
1856 if ((err
= jffs_write_dummy_node(c
, fm
)) < 0) {
1859 jffs_fm_write_unlock(fmc
);
1860 D(printk("jffs_write_node(): "
1861 "jffs_write_dummy_node(): Failed!\n"));
1869 ASSERT(if (fm
->nodes
== 0) {
1870 printk(KERN_ERR
"jffs_write_node(): fm->nodes == 0\n");
1873 pos
= node
->fm
->offset
;
1875 /* Increment the version number here. We can't let the caller
1876 set it beforehand, because we might have had to do GC on a node
1877 of this file - and we'd end up reusing version numbers.
1880 raw_inode
->version
= f
->highest_version
+ 1;
1881 D1(printk (KERN_NOTICE
"jffs_write_node(): setting version of %s to %d\n", f
->name
, raw_inode
->version
));
1883 /* if the file was deleted, set the deleted bit in the raw inode */
1885 raw_inode
->deleted
= 1;
1888 /* Compute the checksum for the data and name chunks. */
1889 raw_inode
->dchksum
= jffs_checksum(data
, raw_inode
->dsize
);
1890 raw_inode
->nchksum
= jffs_checksum(name
, raw_inode
->nsize
);
1892 /* The checksum is calculated without the chksum and accurate
1893 fields so set them to zero first. */
1894 raw_inode
->accurate
= 0;
1895 raw_inode
->chksum
= 0;
1896 raw_inode
->chksum
= jffs_checksum(raw_inode
,
1897 sizeof(struct jffs_raw_inode
));
1898 raw_inode
->accurate
= 0xff;
1900 D3(printk("jffs_write_node(): About to write this raw inode to the "
1901 "flash at pos 0x%lx:\n", (long)pos
));
1902 D3(jffs_print_raw_inode(raw_inode
));
1904 /* The actual raw JFFS node */
1905 node_iovec
[0].iov_base
= (void *) raw_inode
;
1906 node_iovec
[0].iov_len
= (size_t) sizeof(struct jffs_raw_inode
);
1909 /* Get name and size if there is one */
1910 if (raw_inode
->nsize
) {
1911 node_iovec
[iovec_cnt
].iov_base
= (void *) name
;
1912 node_iovec
[iovec_cnt
].iov_len
= (size_t) raw_inode
->nsize
;
1915 if (JFFS_GET_PAD_BYTES(raw_inode
->nsize
)) {
1916 static char allff
[3]={255,255,255};
1917 /* Add some extra padding if necessary */
1918 node_iovec
[iovec_cnt
].iov_base
= allff
;
1919 node_iovec
[iovec_cnt
].iov_len
=
1920 JFFS_GET_PAD_BYTES(raw_inode
->nsize
);
1925 /* Get data and size if there is any */
1926 if (raw_inode
->dsize
) {
1927 node_iovec
[iovec_cnt
].iov_base
= (void *) data
;
1928 node_iovec
[iovec_cnt
].iov_len
= (size_t) raw_inode
->dsize
;
1930 /* No need to pad this because we're not actually putting
1935 if ((err
= flash_safe_writev(fmc
->mtd
, node_iovec
, iovec_cnt
,
1937 jffs_fmfree_partly(fmc
, fm
, 0);
1938 jffs_fm_write_unlock(fmc
);
1939 printk(KERN_ERR
"JFFS: jffs_write_node: Failed to write, "
1940 "requested %i, wrote %i\n", total_size
, err
);
1943 if (raw_inode
->deleted
)
1946 jffs_fm_write_unlock(fmc
);
1947 D3(printk("jffs_write_node(): Leaving...\n"));
1948 return raw_inode
->dsize
;
1949 } /* jffs_write_node() */
1952 /* Read data from the node and write it to the buffer. 'node_offset'
1953 is how much we have read from this particular node before and which
1954 shouldn't be read again. 'max_size' is how much space there is in
1957 jffs_get_node_data(struct jffs_file
*f
, struct jffs_node
*node
,
1958 unsigned char *buf
,__u32 node_offset
, __u32 max_size
)
1960 struct jffs_fmcontrol
*fmc
= f
->c
->fmc
;
1961 __u32 pos
= node
->fm
->offset
+ node
->fm_offset
+ node_offset
;
1962 __u32 avail
= node
->data_size
- node_offset
;
1965 D2(printk(" jffs_get_node_data(): file: \"%s\", ino: %u, "
1966 "version: %u, node_offset: %u\n",
1967 f
->name
, node
->ino
, node
->version
, node_offset
));
1969 r
= min(avail
, max_size
);
1970 D3(printk(KERN_NOTICE
"jffs_get_node_data\n"));
1971 flash_safe_read(fmc
->mtd
, pos
, buf
, r
);
1973 D3(printk(" jffs_get_node_data(): Read %u byte%s.\n",
1974 r
, (r
== 1 ? "" : "s")));
1980 /* Read data from the file's nodes. Write the data to the buffer
1981 'buf'. 'read_offset' tells how much data we should skip. */
1983 jffs_read_data(struct jffs_file
*f
, unsigned char *buf
, __u32 read_offset
,
1986 struct jffs_node
*node
;
1987 __u32 read_data
= 0; /* Total amount of read data. */
1988 __u32 node_offset
= 0;
1989 __u32 pos
= 0; /* Number of bytes traversed. */
1991 D2(printk("jffs_read_data(): file = \"%s\", read_offset = %d, "
1993 (f
->name
? f
->name
: ""), read_offset
, size
));
1995 if (read_offset
>= f
->size
) {
1996 D(printk(" f->size: %d\n", f
->size
));
2000 /* First find the node to read data from. */
2001 node
= f
->range_head
;
2002 while (pos
<= read_offset
) {
2003 node_offset
= read_offset
- pos
;
2004 if (node_offset
>= node
->data_size
) {
2005 pos
+= node
->data_size
;
2006 node
= node
->range_next
;
2013 /* "Cats are living proof that not everything in nature
2015 - Garrison Keilor ('97) */
2017 /* Fill the buffer. */
2018 while (node
&& (read_data
< size
)) {
2021 /* This node does not refer to real data. */
2022 r
= min(size
- read_data
,
2023 node
->data_size
- node_offset
);
2024 memset(&buf
[read_data
], 0, r
);
2026 else if ((r
= jffs_get_node_data(f
, node
, &buf
[read_data
],
2028 size
- read_data
)) < 0) {
2033 node
= node
->range_next
;
2035 D3(printk(" jffs_read_data(): Read %u bytes.\n", read_data
));
2040 /* Used for traversing all nodes in the hash table. */
2042 jffs_foreach_file(struct jffs_control
*c
, int (*func
)(struct jffs_file
*))
2048 for (pos
= 0; pos
< c
->hash_len
; pos
++) {
2049 struct list_head
*p
, *next
;
2050 for (p
= c
->hash
[pos
].next
; p
!= &c
->hash
[pos
]; p
= next
) {
2051 /* We need a reference to the next file in the
2052 list because `func' might remove the current
2055 r
= func(list_entry(p
, struct jffs_file
, hash
));
2066 /* Free all nodes associated with a file. */
2068 jffs_free_node_list(struct jffs_file
*f
)
2070 struct jffs_node
*node
;
2071 struct jffs_node
*p
;
2073 D3(printk("jffs_free_node_list(): f #%u, \"%s\"\n",
2074 f
->ino
, (f
->name
? f
->name
: "")));
2075 node
= f
->version_head
;
2078 node
= node
->version_next
;
2080 DJM(no_jffs_node
--);
2086 /* Free a file and its name. */
2088 jffs_free_file(struct jffs_file
*f
)
2090 D3(printk("jffs_free_file: f #%u, \"%s\"\n",
2091 f
->ino
, (f
->name
? f
->name
: "")));
2103 jffs_get_file_count(void)
2105 return no_jffs_file
;
2108 /* See if a file is deleted. If so, mark that file's nodes as obsolete. */
2110 jffs_possibly_delete_file(struct jffs_file
*f
)
2112 struct jffs_node
*n
;
2114 D3(printk("jffs_possibly_delete_file(): ino: %u\n",
2118 printk(KERN_ERR
"jffs_possibly_delete_file(): f == NULL\n");
2123 /* First try to remove all older versions. Commence with
2125 for (n
= f
->version_head
; n
; n
= n
->version_next
) {
2129 if (jffs_fmfree(f
->c
->fmc
, n
->fm
, n
) < 0) {
2133 /* Unlink the file from the filesystem. */
2134 if (!f
->c
->building_fs
) {
2135 jffs_unlink_file_from_tree(f
);
2137 jffs_unlink_file_from_hash(f
);
2138 jffs_free_node_list(f
);
2145 /* Used in conjunction with jffs_foreach_file() to count the number
2146 of files in the file system. */
2148 jffs_file_count(struct jffs_file
*f
)
2154 /* Build up a file's range list from scratch by going through the
2157 jffs_build_file(struct jffs_file
*f
)
2159 struct jffs_node
*n
;
2161 D3(printk("jffs_build_file(): ino: %u, name: \"%s\"\n",
2162 f
->ino
, (f
->name
? f
->name
: "")));
2164 for (n
= f
->version_head
; n
; n
= n
->version_next
) {
2165 jffs_update_file(f
, n
);
2171 /* Remove an amount of data from a file. If this amount of data is
2172 zero, that could mean that a node should be split in two parts.
2173 We remove or change the appropriate nodes in the lists.
2175 Starting offset of area to be removed is node->data_offset,
2176 and the length of the area is in node->removed_size. */
2178 jffs_delete_data(struct jffs_file
*f
, struct jffs_node
*node
)
2180 struct jffs_node
*n
;
2181 __u32 offset
= node
->data_offset
;
2182 __u32 remove_size
= node
->removed_size
;
2184 D3(printk("jffs_delete_data(): offset = %u, remove_size = %u\n",
2185 offset
, remove_size
));
2187 if (remove_size
== 0
2189 && f
->range_tail
->data_offset
+ f
->range_tail
->data_size
2191 /* A simple append; nothing to remove or no node to split. */
2195 /* Find the node where we should begin the removal. */
2196 for (n
= f
->range_head
; n
; n
= n
->range_next
) {
2197 if (n
->data_offset
+ n
->data_size
> offset
) {
2202 /* If there's no data in the file there's no data to
2207 if (n
->data_offset
> offset
) {
2208 /* XXX: Not implemented yet. */
2209 printk(KERN_WARNING
"JFFS: An unexpected situation "
2210 "occurred in jffs_delete_data.\n");
2212 else if (n
->data_offset
< offset
) {
2213 /* See if the node has to be split into two parts. */
2214 if (n
->data_offset
+ n
->data_size
> offset
+ remove_size
) {
2216 struct jffs_node
*new_node
;
2217 D3(printk("jffs_delete_data(): Split node with "
2218 "version number %u.\n", n
->version
));
2220 if (!(new_node
= jffs_alloc_node())) {
2221 D(printk("jffs_delete_data(): -ENOMEM\n"));
2224 DJM(no_jffs_node
++);
2226 new_node
->ino
= n
->ino
;
2227 new_node
->version
= n
->version
;
2228 new_node
->data_offset
= offset
;
2229 new_node
->data_size
= n
->data_size
- (remove_size
+ (offset
- n
->data_offset
));
2230 new_node
->fm_offset
= n
->fm_offset
+ (remove_size
+ (offset
- n
->data_offset
));
2231 new_node
->name_size
= n
->name_size
;
2232 new_node
->fm
= n
->fm
;
2233 new_node
->version_prev
= n
;
2234 new_node
->version_next
= n
->version_next
;
2235 if (new_node
->version_next
) {
2236 new_node
->version_next
->version_prev
2240 f
->version_tail
= new_node
;
2242 n
->version_next
= new_node
;
2243 new_node
->range_prev
= n
;
2244 new_node
->range_next
= n
->range_next
;
2245 if (new_node
->range_next
) {
2246 new_node
->range_next
->range_prev
= new_node
;
2249 f
->range_tail
= new_node
;
2251 /* A very interesting can of worms. */
2252 n
->range_next
= new_node
;
2253 n
->data_size
= offset
- n
->data_offset
;
2255 jffs_add_node(new_node
);
2257 D1(printk(KERN_WARNING
"jffs_delete_data(): Splitting an empty node (file hold).\n!"));
2258 D1(printk(KERN_WARNING
"FIXME: Did dwmw2 do the right thing here?\n"));
2260 n
= new_node
->range_next
;
2264 /* No. No need to split the node. Just remove
2265 the end of the node. */
2266 int r
= min(n
->data_offset
+ n
->data_size
2267 - offset
, remove_size
);
2274 /* Remove as many nodes as necessary. */
2275 while (n
&& remove_size
) {
2276 if (n
->data_size
<= remove_size
) {
2277 struct jffs_node
*p
= n
;
2278 remove_size
-= n
->data_size
;
2280 D3(printk("jffs_delete_data(): Removing node: "
2281 "ino: %u, version: %u%s\n",
2283 (p
->fm
? "" : " (virtual)")));
2285 jffs_fmfree(f
->c
->fmc
, p
->fm
, p
);
2287 jffs_unlink_node_from_range_list(f
, p
);
2288 jffs_unlink_node_from_version_list(f
, p
);
2290 DJM(no_jffs_node
--);
2293 n
->data_size
-= remove_size
;
2294 n
->fm_offset
+= remove_size
;
2295 n
->data_offset
-= (node
->removed_size
- remove_size
);
2301 /* Adjust the following nodes' information about offsets etc. */
2302 while (n
&& node
->removed_size
) {
2303 n
->data_offset
-= node
->removed_size
;
2307 if (node
->removed_size
> (f
->size
- node
->data_offset
)) {
2308 /* It's possible that the removed_size is in fact
2309 * greater than the amount of data we actually thought
2310 * were present in the first place - some of the nodes
2311 * which this node originally obsoleted may already have
2312 * been deleted from the flash by subsequent garbage
2315 * If this is the case, don't let f->size go negative.
2316 * Bad things would happen :)
2318 f
->size
= node
->data_offset
;
2320 f
->size
-= node
->removed_size
;
2322 D3(printk("jffs_delete_data(): f->size = %d\n", f
->size
));
2324 } /* jffs_delete_data() */
2327 /* Insert some data into a file. Prior to the call to this function,
2328 jffs_delete_data should be called. */
2330 jffs_insert_data(struct jffs_file
*f
, struct jffs_node
*node
)
2332 D3(printk("jffs_insert_data(): node->data_offset = %u, "
2333 "node->data_size = %u, f->size = %u\n",
2334 node
->data_offset
, node
->data_size
, f
->size
));
2336 /* Find the position where we should insert data. */
2338 if (node
->data_offset
== f
->size
) {
2339 /* A simple append. This is the most common operation. */
2340 node
->range_next
= NULL
;
2341 node
->range_prev
= f
->range_tail
;
2342 if (node
->range_prev
) {
2343 node
->range_prev
->range_next
= node
;
2345 f
->range_tail
= node
;
2346 f
->size
+= node
->data_size
;
2347 if (!f
->range_head
) {
2348 f
->range_head
= node
;
2351 else if (node
->data_offset
< f
->size
) {
2352 /* Trying to insert data into the middle of the file. This
2353 means no problem because jffs_delete_data() has already
2354 prepared the range list for us. */
2355 struct jffs_node
*n
;
2357 /* Find the correct place for the insertion and then insert
2359 for (n
= f
->range_head
; n
; n
= n
->range_next
) {
2360 D2(printk("Cool stuff's happening!\n"));
2362 if (n
->data_offset
== node
->data_offset
) {
2363 node
->range_prev
= n
->range_prev
;
2364 if (node
->range_prev
) {
2365 node
->range_prev
->range_next
= node
;
2368 f
->range_head
= node
;
2370 node
->range_next
= n
;
2371 n
->range_prev
= node
;
2374 ASSERT(else if (n
->data_offset
+ n
->data_size
>
2375 node
->data_offset
) {
2376 printk(KERN_ERR
"jffs_insert_data(): "
2377 "Couldn't find a place to insert "
2383 /* Adjust later nodes' offsets etc. */
2384 n
= node
->range_next
;
2386 n
->data_offset
+= node
->data_size
;
2389 f
->size
+= node
->data_size
;
2391 else if (node
->data_offset
> f
->size
) {
2392 /* Okay. This is tricky. This means that we want to insert
2393 data at a place that is beyond the limits of the file as
2394 it is constructed right now. This is actually a common
2395 event that for instance could occur during the mounting
2396 of the file system if a large file have been truncated,
2397 rewritten and then only partially garbage collected. */
2399 struct jffs_node
*n
;
2401 /* We need a place holder for the data that is missing in
2402 front of this insertion. This "virtual node" will not
2403 be associated with any space on the flash device. */
2404 struct jffs_node
*virtual_node
;
2405 if (!(virtual_node
= jffs_alloc_node())) {
2409 D(printk("jffs_insert_data: Inserting a virtual node.\n"));
2410 D(printk(" node->data_offset = %u\n", node
->data_offset
));
2411 D(printk(" f->size = %u\n", f
->size
));
2413 virtual_node
->ino
= node
->ino
;
2414 virtual_node
->version
= node
->version
;
2415 virtual_node
->removed_size
= 0;
2416 virtual_node
->fm_offset
= 0;
2417 virtual_node
->name_size
= 0;
2418 virtual_node
->fm
= NULL
; /* This is a virtual data holder. */
2419 virtual_node
->version_prev
= NULL
;
2420 virtual_node
->version_next
= NULL
;
2421 virtual_node
->range_next
= NULL
;
2423 /* Are there any data at all in the file yet? */
2424 if (f
->range_head
) {
2425 virtual_node
->data_offset
2426 = f
->range_tail
->data_offset
2427 + f
->range_tail
->data_size
;
2428 virtual_node
->data_size
2429 = node
->data_offset
- virtual_node
->data_offset
;
2430 virtual_node
->range_prev
= f
->range_tail
;
2431 f
->range_tail
->range_next
= virtual_node
;
2434 virtual_node
->data_offset
= 0;
2435 virtual_node
->data_size
= node
->data_offset
;
2436 virtual_node
->range_prev
= NULL
;
2437 f
->range_head
= virtual_node
;
2440 f
->range_tail
= virtual_node
;
2441 f
->size
+= virtual_node
->data_size
;
2443 /* Insert this virtual node in the version list as well. */
2444 for (n
= f
->version_head
; n
; n
= n
->version_next
) {
2445 if (n
->version
== virtual_node
->version
) {
2446 virtual_node
->version_prev
= n
->version_prev
;
2447 n
->version_prev
= virtual_node
;
2448 if (virtual_node
->version_prev
) {
2449 virtual_node
->version_prev
2450 ->version_next
= virtual_node
;
2453 f
->version_head
= virtual_node
;
2455 virtual_node
->version_next
= n
;
2460 D(jffs_print_node(virtual_node
));
2462 /* Make a new try to insert the node. */
2466 D3(printk("jffs_insert_data(): f->size = %d\n", f
->size
));
2471 /* A new node (with data) has been added to the file and now the range
2472 list has to be modified. */
2474 jffs_update_file(struct jffs_file
*f
, struct jffs_node
*node
)
2478 D3(printk("jffs_update_file(): ino: %u, version: %u\n",
2479 f
->ino
, node
->version
));
2481 if (node
->data_size
== 0) {
2482 if (node
->removed_size
== 0) {
2483 /* data_offset == X */
2484 /* data_size == 0 */
2485 /* remove_size == 0 */
2488 /* data_offset == X */
2489 /* data_size == 0 */
2490 /* remove_size != 0 */
2491 if ((err
= jffs_delete_data(f
, node
)) < 0) {
2497 /* data_offset == X */
2498 /* data_size != 0 */
2499 /* remove_size == Y */
2500 if ((err
= jffs_delete_data(f
, node
)) < 0) {
2503 if ((err
= jffs_insert_data(f
, node
)) < 0) {
2510 /* Print the contents of a node. */
2512 jffs_print_node(struct jffs_node
*n
)
2514 D(printk("jffs_node: 0x%p\n", n
));
2516 D(printk(" 0x%08x, /* version */\n", n
->version
));
2517 D(printk(" 0x%08x, /* data_offset */\n", n
->data_offset
));
2518 D(printk(" 0x%08x, /* data_size */\n", n
->data_size
));
2519 D(printk(" 0x%08x, /* removed_size */\n", n
->removed_size
));
2520 D(printk(" 0x%08x, /* fm_offset */\n", n
->fm_offset
));
2521 D(printk(" 0x%02x, /* name_size */\n", n
->name_size
));
2522 D(printk(" 0x%p, /* fm, fm->offset: %u */\n",
2523 n
->fm
, (n
->fm
? n
->fm
->offset
: 0)));
2524 D(printk(" 0x%p, /* version_prev */\n", n
->version_prev
));
2525 D(printk(" 0x%p, /* version_next */\n", n
->version_next
));
2526 D(printk(" 0x%p, /* range_prev */\n", n
->range_prev
));
2527 D(printk(" 0x%p, /* range_next */\n", n
->range_next
));
2532 /* Print the contents of a raw inode. */
2534 jffs_print_raw_inode(struct jffs_raw_inode
*raw_inode
)
2536 D(printk("jffs_raw_inode: inode number: %u\n", raw_inode
->ino
));
2538 D(printk(" 0x%08x, /* magic */\n", raw_inode
->magic
));
2539 D(printk(" 0x%08x, /* ino */\n", raw_inode
->ino
));
2540 D(printk(" 0x%08x, /* pino */\n", raw_inode
->pino
));
2541 D(printk(" 0x%08x, /* version */\n", raw_inode
->version
));
2542 D(printk(" 0x%08x, /* mode */\n", raw_inode
->mode
));
2543 D(printk(" 0x%04x, /* uid */\n", raw_inode
->uid
));
2544 D(printk(" 0x%04x, /* gid */\n", raw_inode
->gid
));
2545 D(printk(" 0x%08x, /* atime */\n", raw_inode
->atime
));
2546 D(printk(" 0x%08x, /* mtime */\n", raw_inode
->mtime
));
2547 D(printk(" 0x%08x, /* ctime */\n", raw_inode
->ctime
));
2548 D(printk(" 0x%08x, /* offset */\n", raw_inode
->offset
));
2549 D(printk(" 0x%08x, /* dsize */\n", raw_inode
->dsize
));
2550 D(printk(" 0x%08x, /* rsize */\n", raw_inode
->rsize
));
2551 D(printk(" 0x%02x, /* nsize */\n", raw_inode
->nsize
));
2552 D(printk(" 0x%02x, /* nlink */\n", raw_inode
->nlink
));
2553 D(printk(" 0x%02x, /* spare */\n",
2555 D(printk(" %u, /* rename */\n",
2556 raw_inode
->rename
));
2557 D(printk(" %u, /* deleted */\n",
2558 raw_inode
->deleted
));
2559 D(printk(" 0x%02x, /* accurate */\n",
2560 raw_inode
->accurate
));
2561 D(printk(" 0x%08x, /* dchksum */\n", raw_inode
->dchksum
));
2562 D(printk(" 0x%04x, /* nchksum */\n", raw_inode
->nchksum
));
2563 D(printk(" 0x%04x, /* chksum */\n", raw_inode
->chksum
));
2568 /* Print the contents of a file. */
2571 jffs_print_file(struct jffs_file
*f
)
2574 D(printk("jffs_file: 0x%p\n", f
));
2576 D(printk(" 0x%08x, /* ino */\n", f
->ino
));
2577 D(printk(" 0x%08x, /* pino */\n", f
->pino
));
2578 D(printk(" 0x%08x, /* mode */\n", f
->mode
));
2579 D(printk(" 0x%04x, /* uid */\n", f
->uid
));
2580 D(printk(" 0x%04x, /* gid */\n", f
->gid
));
2581 D(printk(" 0x%08x, /* atime */\n", f
->atime
));
2582 D(printk(" 0x%08x, /* mtime */\n", f
->mtime
));
2583 D(printk(" 0x%08x, /* ctime */\n", f
->ctime
));
2584 D(printk(" 0x%02x, /* nsize */\n", f
->nsize
));
2585 D(printk(" 0x%02x, /* nlink */\n", f
->nlink
));
2586 D(printk(" 0x%02x, /* deleted */\n", f
->deleted
));
2587 D(printk(" \"%s\", ", (f
->name
? f
->name
: "")));
2588 D(for (i
= strlen(f
->name
? f
->name
: ""); i
< 8; ++i
) {
2591 D(printk("/* name */\n"));
2592 D(printk(" 0x%08x, /* size */\n", f
->size
));
2593 D(printk(" 0x%08x, /* highest_version */\n",
2594 f
->highest_version
));
2595 D(printk(" 0x%p, /* c */\n", f
->c
));
2596 D(printk(" 0x%p, /* parent */\n", f
->parent
));
2597 D(printk(" 0x%p, /* children */\n", f
->children
));
2598 D(printk(" 0x%p, /* sibling_prev */\n", f
->sibling_prev
));
2599 D(printk(" 0x%p, /* sibling_next */\n", f
->sibling_next
));
2600 D(printk(" 0x%p, /* hash_prev */\n", f
->hash
.prev
));
2601 D(printk(" 0x%p, /* hash_next */\n", f
->hash
.next
));
2602 D(printk(" 0x%p, /* range_head */\n", f
->range_head
));
2603 D(printk(" 0x%p, /* range_tail */\n", f
->range_tail
));
2604 D(printk(" 0x%p, /* version_head */\n", f
->version_head
));
2605 D(printk(" 0x%p, /* version_tail */\n", f
->version_tail
));
2612 jffs_print_hash_table(struct jffs_control
*c
)
2616 printk("JFFS: Dumping the file system's hash table...\n");
2617 for (i
= 0; i
< c
->hash_len
; i
++) {
2618 struct list_head
*p
;
2619 for (p
= c
->hash
[i
].next
; p
!= &c
->hash
[i
]; p
= p
->next
) {
2620 struct jffs_file
*f
=list_entry(p
,struct jffs_file
,hash
);
2621 printk("*** c->hash[%u]: \"%s\" "
2622 "(ino: %u, pino: %u)\n",
2623 i
, (f
->name
? f
->name
: ""),
2631 jffs_print_tree(struct jffs_file
*first_file
, int indent
)
2633 struct jffs_file
*f
;
2641 if (!(space
= (char *) kmalloc(indent
+ 1, GFP_KERNEL
))) {
2642 printk("jffs_print_tree(): Out of memory!\n");
2646 memset(space
, ' ', indent
);
2647 space
[indent
] = '\0';
2649 for (f
= first_file
; f
; f
= f
->sibling_next
) {
2650 dir
= S_ISDIR(f
->mode
);
2651 printk("%s%s%s (ino: %u, highest_version: %u, size: %u)\n",
2652 space
, (f
->name
? f
->name
: ""), (dir
? "/" : ""),
2653 f
->ino
, f
->highest_version
, f
->size
);
2655 jffs_print_tree(f
->children
, indent
+ 2);
2663 #if defined(JFFS_MEMORY_DEBUG) && JFFS_MEMORY_DEBUG
2665 jffs_print_memory_allocation_statistics(void)
2667 static long printout
;
2668 printk("________ Memory printout #%ld ________\n", ++printout
);
2669 printk("no_jffs_file = %ld\n", no_jffs_file
);
2670 printk("no_jffs_node = %ld\n", no_jffs_node
);
2671 printk("no_jffs_control = %ld\n", no_jffs_control
);
2672 printk("no_jffs_raw_inode = %ld\n", no_jffs_raw_inode
);
2673 printk("no_jffs_node_ref = %ld\n", no_jffs_node_ref
);
2674 printk("no_jffs_fm = %ld\n", no_jffs_fm
);
2675 printk("no_jffs_fmcontrol = %ld\n", no_jffs_fmcontrol
);
2676 printk("no_hash = %ld\n", no_hash
);
2677 printk("no_name = %ld\n", no_name
);
2683 /* Rewrite `size' bytes, and begin at `node'. */
2685 jffs_rewrite_data(struct jffs_file
*f
, struct jffs_node
*node
, __u32 size
)
2687 struct jffs_control
*c
= f
->c
;
2688 struct jffs_fmcontrol
*fmc
= c
->fmc
;
2689 struct jffs_raw_inode raw_inode
;
2690 struct jffs_node
*new_node
;
2694 __u32 total_name_size
;
2695 __u32 total_data_size
;
2699 D1(printk("***jffs_rewrite_data(): node: %u, name: \"%s\", size: %u\n",
2700 f
->ino
, (f
->name
? f
->name
: "(null)"), size
));
2702 /* Create and initialize the new node. */
2703 if (!(new_node
= jffs_alloc_node())) {
2704 D(printk("jffs_rewrite_data(): "
2705 "Failed to allocate node.\n"));
2708 DJM(no_jffs_node
++);
2709 new_node
->data_offset
= node
->data_offset
;
2710 new_node
->removed_size
= size
;
2711 total_name_size
= JFFS_PAD(f
->nsize
);
2712 total_data_size
= JFFS_PAD(size
);
2713 total_size
= sizeof(struct jffs_raw_inode
)
2714 + total_name_size
+ total_data_size
;
2715 new_node
->fm_offset
= sizeof(struct jffs_raw_inode
)
2719 jffs_fm_write_lock(fmc
);
2722 if ((err
= jffs_fmalloc(fmc
, total_size
, new_node
, &fm
)) < 0) {
2723 DJM(no_jffs_node
--);
2724 jffs_fm_write_unlock(fmc
);
2725 D(printk("jffs_rewrite_data(): Failed to allocate fm.\n"));
2726 jffs_free_node(new_node
);
2729 else if (!fm
->nodes
) {
2730 /* The jffs_fm struct that we got is not big enough. */
2731 /* This should never happen, because we deal with this case
2732 in jffs_garbage_collect_next().*/
2733 printk(KERN_WARNING
"jffs_rewrite_data(): Allocated node is too small (%d bytes of %d)\n", fm
->size
, total_size
);
2734 if ((err
= jffs_write_dummy_node(c
, fm
)) < 0) {
2735 D(printk("jffs_rewrite_data(): "
2736 "jffs_write_dummy_node() Failed!\n"));
2741 jffs_fm_write_unlock(fmc
);
2748 /* Initialize the raw inode. */
2749 raw_inode
.magic
= JFFS_MAGIC_BITMASK
;
2750 raw_inode
.ino
= f
->ino
;
2751 raw_inode
.pino
= f
->pino
;
2752 raw_inode
.version
= f
->highest_version
+ 1;
2753 raw_inode
.mode
= f
->mode
;
2754 raw_inode
.uid
= f
->uid
;
2755 raw_inode
.gid
= f
->gid
;
2756 raw_inode
.atime
= f
->atime
;
2757 raw_inode
.mtime
= f
->mtime
;
2758 raw_inode
.ctime
= f
->ctime
;
2759 raw_inode
.offset
= node
->data_offset
;
2760 raw_inode
.dsize
= size
;
2761 raw_inode
.rsize
= size
;
2762 raw_inode
.nsize
= f
->nsize
;
2763 raw_inode
.nlink
= f
->nlink
;
2764 raw_inode
.spare
= 0;
2765 raw_inode
.rename
= 0;
2766 raw_inode
.deleted
= f
->deleted
;
2767 raw_inode
.accurate
= 0xff;
2768 raw_inode
.dchksum
= 0;
2769 raw_inode
.nchksum
= 0;
2771 pos
= new_node
->fm
->offset
;
2772 pos_dchksum
= pos
+JFFS_RAW_INODE_DCHKSUM_OFFSET
;
2774 D3(printk("jffs_rewrite_data(): Writing this raw inode "
2775 "to pos 0x%ul.\n", pos
));
2776 D3(jffs_print_raw_inode(&raw_inode
));
2778 if ((err
= flash_safe_write(fmc
->mtd
, pos
,
2779 (u_char
*) &raw_inode
,
2780 sizeof(struct jffs_raw_inode
)
2782 - sizeof(__u16
) - sizeof(__u16
))) < 0) {
2783 jffs_fmfree_partly(fmc
, fm
,
2784 total_name_size
+ total_data_size
);
2785 jffs_fm_write_unlock(fmc
);
2786 printk(KERN_ERR
"JFFS: jffs_rewrite_data: Write error during "
2787 "rewrite. (raw inode)\n");
2788 printk(KERN_ERR
"JFFS: jffs_rewrite_data: Now retrying "
2789 "rewrite. (raw inode)\n");
2792 pos
+= sizeof(struct jffs_raw_inode
);
2794 /* Write the name to the flash memory. */
2796 D3(printk("jffs_rewrite_data(): Writing name \"%s\" to "
2797 "pos 0x%ul.\n", f
->name
, (unsigned int) pos
));
2798 if ((err
= flash_safe_write(fmc
->mtd
, pos
,
2801 jffs_fmfree_partly(fmc
, fm
, total_data_size
);
2802 jffs_fm_write_unlock(fmc
);
2803 printk(KERN_ERR
"JFFS: jffs_rewrite_data: Write "
2804 "error during rewrite. (name)\n");
2805 printk(KERN_ERR
"JFFS: jffs_rewrite_data: Now retrying "
2806 "rewrite. (name)\n");
2809 pos
+= total_name_size
;
2810 raw_inode
.nchksum
= jffs_checksum(f
->name
, f
->nsize
);
2813 /* Write the data. */
2816 unsigned char *page
;
2817 __u32 offset
= node
->data_offset
;
2819 if (!(page
= (unsigned char *)__get_free_page(GFP_KERNEL
))) {
2820 jffs_fmfree_partly(fmc
, fm
, 0);
2825 __u32 s
= min(size
, (__u32
)PAGE_SIZE
);
2826 if ((r
= jffs_read_data(f
, (char *)page
,
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: "
2833 "failed! (r = %d)\n", r
);
2836 if ((err
= flash_safe_write(fmc
->mtd
,
2837 pos
, page
, r
)) < 0) {
2838 free_page((unsigned long)page
);
2839 jffs_fmfree_partly(fmc
, fm
, 0);
2840 jffs_fm_write_unlock(fmc
);
2841 printk(KERN_ERR
"JFFS: jffs_rewrite_data: "
2842 "Write error during rewrite. "
2849 raw_inode
.dchksum
+= jffs_checksum(page
, r
);
2852 free_page((unsigned long)page
);
2855 raw_inode
.accurate
= 0;
2856 raw_inode
.chksum
= jffs_checksum(&raw_inode
,
2857 sizeof(struct jffs_raw_inode
)
2860 /* Add the checksum. */
2862 = flash_safe_write(fmc
->mtd
, pos_dchksum
,
2864 &raw_inode
)[JFFS_RAW_INODE_DCHKSUM_OFFSET
],
2865 sizeof(__u32
) + sizeof(__u16
)
2866 + sizeof(__u16
))) < 0) {
2867 jffs_fmfree_partly(fmc
, fm
, 0);
2868 jffs_fm_write_unlock(fmc
);
2869 printk(KERN_ERR
"JFFS: jffs_rewrite_data: Write error during "
2870 "rewrite. (checksum)\n");
2874 /* Now make the file system aware of the newly written node. */
2875 jffs_insert_node(c
, f
, &raw_inode
, f
->name
, new_node
);
2876 jffs_fm_write_unlock(fmc
);
2878 D3(printk("jffs_rewrite_data(): Leaving...\n"));
2880 } /* jffs_rewrite_data() */
2883 /* jffs_garbage_collect_next implements one step in the garbage collect
2884 process and is often called multiple times at each occasion of a
2888 jffs_garbage_collect_next(struct jffs_control
*c
)
2890 struct jffs_fmcontrol
*fmc
= c
->fmc
;
2891 struct jffs_node
*node
;
2892 struct jffs_file
*f
;
2896 __u32 total_name_size
;
2897 __u32 extra_available
;
2899 __u32 free_chunk_size1
= jffs_free_size1(fmc
);
2900 D2(__u32 free_chunk_size2
= jffs_free_size2(fmc
));
2902 /* Get the oldest node in the flash. */
2903 node
= jffs_get_oldest_node(fmc
);
2905 printk(KERN_ERR
"JFFS: jffs_garbage_collect_next: "
2906 "No oldest node found!\n");
2908 goto jffs_garbage_collect_next_end
;
2913 /* Find its corresponding file too. */
2914 f
= jffs_find_file(c
, node
->ino
);
2917 printk (KERN_ERR
"JFFS: jffs_garbage_collect_next: "
2918 "No file to garbage collect! "
2919 "(ino = 0x%08x)\n", node
->ino
);
2920 /* FIXME: Free the offending node and recover. */
2922 goto jffs_garbage_collect_next_end
;
2925 /* We always write out the name. Theoretically, we don't need
2926 to, but for now it's easier - because otherwise we'd have
2927 to keep track of how many times the current name exists on
2928 the flash and make sure it never reaches zero.
2930 The current approach means that would be possible to cause
2931 the GC to end up eating its tail by writing lots of nodes
2932 with no name for it to garbage-collect. Hence the change in
2933 inode.c to write names with _every_ node.
2935 It sucks, but it _should_ work.
2937 total_name_size
= JFFS_PAD(f
->nsize
);
2939 D1(printk("jffs_garbage_collect_next(): \"%s\", "
2940 "ino: %u, version: %u, location 0x%x, dsize %u\n",
2941 (f
->name
? f
->name
: ""), node
->ino
, node
->version
,
2942 node
->fm
->offset
, node
->data_size
));
2944 /* Compute how many data it's possible to rewrite at the moment. */
2945 data_size
= f
->size
- node
->data_offset
;
2947 /* And from that, the total size of the chunk we want to write */
2948 size
= sizeof(struct jffs_raw_inode
) + total_name_size
2949 + data_size
+ JFFS_GET_PAD_BYTES(data_size
);
2951 /* If that's more than max_chunk_size, reduce it accordingly */
2952 if (size
> fmc
->max_chunk_size
) {
2953 size
= fmc
->max_chunk_size
;
2954 data_size
= size
- sizeof(struct jffs_raw_inode
)
2958 /* If we're asking to take up more space than free_chunk_size1
2959 but we _could_ fit in it, shrink accordingly.
2961 if (size
> free_chunk_size1
) {
2963 if (free_chunk_size1
<
2964 (sizeof(struct jffs_raw_inode
) + total_name_size
+ BLOCK_SIZE
)){
2965 /* The space left is too small to be of any
2967 struct jffs_fm
*dirty_fm
2968 = jffs_fmalloced(fmc
,
2969 fmc
->tail
->offset
+ fmc
->tail
->size
,
2970 free_chunk_size1
, NULL
);
2972 printk(KERN_ERR
"JFFS: "
2973 "jffs_garbage_collect_next: "
2974 "Failed to allocate `dirty' "
2977 goto jffs_garbage_collect_next_end
;
2979 D1(printk("Dirtying end of flash - too small\n"));
2980 jffs_write_dummy_node(c
, dirty_fm
);
2982 goto jffs_garbage_collect_next_end
;
2984 D1(printk("Reducing size of new node from %d to %d to avoid "
2985 " exceeding free_chunk_size1\n",
2986 size
, free_chunk_size1
));
2988 size
= free_chunk_size1
;
2989 data_size
= size
- sizeof(struct jffs_raw_inode
)
2994 /* Calculate the amount of space needed to hold the nodes
2995 which are remaining in the tail */
2996 space_needed
= fmc
->min_free_size
- (node
->fm
->offset
% fmc
->sector_size
);
2998 /* From that, calculate how much 'extra' space we can use to
2999 increase the size of the node we're writing from the size
3000 of the node we're obsoleting
3002 if (space_needed
> fmc
->free_size
) {
3003 /* If we've gone below min_free_size for some reason,
3004 don't fuck up. This is why we have
3005 min_free_size > sector_size. Whinge about it though,
3006 just so I can convince myself my maths is right.
3008 D1(printk(KERN_WARNING
"jffs_garbage_collect_next(): "
3009 "space_needed %d exceeded free_size %d\n",
3010 space_needed
, fmc
->free_size
));
3011 extra_available
= 0;
3013 extra_available
= fmc
->free_size
- space_needed
;
3016 /* Check that we don't use up any more 'extra' space than
3018 if (size
> JFFS_PAD(node
->data_size
) + total_name_size
+
3019 sizeof(struct jffs_raw_inode
) + extra_available
) {
3020 D1(printk("Reducing size of new node from %d to %ld to avoid "
3021 "catching our tail\n", size
,
3022 (long) (JFFS_PAD(node
->data_size
) + JFFS_PAD(node
->name_size
) +
3023 sizeof(struct jffs_raw_inode
) + extra_available
)));
3024 D1(printk("space_needed = %d, extra_available = %d\n",
3025 space_needed
, extra_available
));
3027 size
= JFFS_PAD(node
->data_size
) + total_name_size
+
3028 sizeof(struct jffs_raw_inode
) + extra_available
;
3029 data_size
= size
- sizeof(struct jffs_raw_inode
)
3033 D2(printk(" total_name_size: %u\n", total_name_size
));
3034 D2(printk(" data_size: %u\n", data_size
));
3035 D2(printk(" size: %u\n", size
));
3036 D2(printk(" f->nsize: %u\n", f
->nsize
));
3037 D2(printk(" f->size: %u\n", f
->size
));
3038 D2(printk(" node->data_offset: %u\n", node
->data_offset
));
3039 D2(printk(" free_chunk_size1: %u\n", free_chunk_size1
));
3040 D2(printk(" free_chunk_size2: %u\n", free_chunk_size2
));
3041 D2(printk(" node->fm->offset: 0x%08x\n", node
->fm
->offset
));
3043 if ((err
= jffs_rewrite_data(f
, node
, data_size
))) {
3044 printk(KERN_WARNING
"jffs_rewrite_data() failed: %d\n", err
);
3048 jffs_garbage_collect_next_end
:
3049 D3(printk("jffs_garbage_collect_next: Leaving...\n"));
3051 } /* jffs_garbage_collect_next */
3054 /* If an obsolete node is partly going to be erased due to garbage
3055 collection, the part that isn't going to be erased must be filled
3056 with zeroes so that the scan of the flash will work smoothly next
3057 time. (The data in the file could for instance be a JFFS image
3058 which could cause enormous confusion during a scan of the flash
3059 device if we didn't do this.)
3060 There are two phases in this procedure: First, the clearing of
3061 the name and data parts of the node. Second, possibly also clearing
3062 a part of the raw inode as well. If the box is power cycled during
3063 the first phase, only the checksum of this node-to-be-cleared-at-
3064 the-end will be wrong. If the box is power cycled during, or after,
3065 the clearing of the raw inode, the information like the length of
3066 the name and data parts are zeroed. The next time the box is
3067 powered up, the scanning algorithm manages this faulty data too
3070 - The checksum is invalid and thus the raw inode must be discarded
3072 - If the lengths of the data part or the name part are zeroed, the
3073 scanning just continues after the raw inode. But after the inode
3074 the scanning procedure just finds zeroes which is the same as
3077 So, in the end, this could never fail. :-) Even if it does fail,
3078 the scanning algorithm should manage that too. */
3081 jffs_clear_end_of_node(struct jffs_control
*c
, __u32 erase_size
)
3084 struct jffs_fmcontrol
*fmc
= c
->fmc
;
3087 __u32 zero_offset_data
;
3088 __u32 zero_size_data
;
3089 __u32 cutting_raw_inode
= 0;
3091 if (!(fm
= jffs_cut_node(fmc
, erase_size
))) {
3092 D3(printk("jffs_clear_end_of_node(): fm == NULL\n"));
3096 /* Where and how much shall we clear? */
3097 zero_offset
= fmc
->head
->offset
+ erase_size
;
3098 zero_size
= fm
->offset
+ fm
->size
- zero_offset
;
3100 /* Do we have to clear the raw_inode explicitly? */
3101 if (fm
->size
- zero_size
< sizeof(struct jffs_raw_inode
)) {
3102 cutting_raw_inode
= sizeof(struct jffs_raw_inode
)
3103 - (fm
->size
- zero_size
);
3106 /* First, clear the name and data fields. */
3107 zero_offset_data
= zero_offset
+ cutting_raw_inode
;
3108 zero_size_data
= zero_size
- cutting_raw_inode
;
3109 flash_safe_acquire(fmc
->mtd
);
3110 flash_memset(fmc
->mtd
, zero_offset_data
, 0, zero_size_data
);
3111 flash_safe_release(fmc
->mtd
);
3113 /* Should we clear a part of the raw inode? */
3114 if (cutting_raw_inode
) {
3115 /* I guess it is ok to clear the raw inode in this order. */
3116 flash_safe_acquire(fmc
->mtd
);
3117 flash_memset(fmc
->mtd
, zero_offset
, 0,
3119 flash_safe_release(fmc
->mtd
);
3123 } /* jffs_clear_end_of_node() */
3125 /* Try to erase as much as possible of the dirt in the flash memory. */
3127 jffs_try_to_erase(struct jffs_control
*c
)
3129 struct jffs_fmcontrol
*fmc
= c
->fmc
;
3134 D3(printk("jffs_try_to_erase()\n"));
3136 erase_size
= jffs_erasable_size(fmc
);
3138 D2(printk("jffs_try_to_erase(): erase_size = %ld\n", erase_size
));
3140 if (erase_size
== 0) {
3143 else if (erase_size
< 0) {
3144 printk(KERN_ERR
"JFFS: jffs_try_to_erase: "
3145 "jffs_erasable_size returned %ld.\n", erase_size
);
3149 if ((err
= jffs_clear_end_of_node(c
, erase_size
)) < 0) {
3150 printk(KERN_ERR
"JFFS: jffs_try_to_erase: "
3151 "Clearing of node failed.\n");
3155 offset
= fmc
->head
->offset
;
3157 /* Now, let's try to do the erase. */
3158 if ((err
= flash_erase_region(fmc
->mtd
,
3159 offset
, erase_size
)) < 0) {
3160 printk(KERN_ERR
"JFFS: Erase of flash failed. "
3161 "offset = %u, erase_size = %ld\n",
3162 offset
, erase_size
);
3163 /* XXX: Here we should allocate this area as dirty
3164 with jffs_fmalloced or something similar. Now
3165 we just report the error. */
3170 /* Check if the erased sectors really got erased. */
3175 pos
= (__u32
)flash_get_direct_pointer(to_kdev_t(c
->sb
->s_dev
), offset
);
3176 end
= pos
+ erase_size
;
3178 D2(printk("JFFS: Checking erased sector(s)...\n"));
3180 flash_safe_acquire(fmc
->mtd
);
3182 for (; pos
< end
; pos
+= 4) {
3183 if (*(__u32
*)pos
!= JFFS_EMPTY_BITMASK
) {
3184 printk("JFFS: Erase failed! pos = 0x%lx\n",
3186 jffs_hexdump(fmc
->mtd
, pos
,
3187 jffs_min(256, end
- pos
));
3193 flash_safe_release(fmc
->mtd
);
3196 D2(printk("JFFS: Erase succeeded.\n"));
3199 /* XXX: Here we should allocate the memory
3200 with jffs_fmalloced() in order to prevent
3201 JFFS from using this area accidentally. */
3207 /* Update the flash memory data structures. */
3208 jffs_sync_erase(fmc
, erase_size
);
3214 /* There are different criteria that should trigger a garbage collect:
3216 1. There is too much dirt in the memory.
3217 2. The free space is becoming small.
3218 3. There are many versions of a node.
3220 The garbage collect should always be done in a manner that guarantees
3221 that future garbage collects cannot be locked. E.g. Rewritten chunks
3222 should not be too large (span more than one sector in the flash memory
3223 for exemple). Of course there is a limit on how intelligent this garbage
3224 collection can be. */
3228 jffs_garbage_collect_now(struct jffs_control
*c
)
3230 struct jffs_fmcontrol
*fmc
= c
->fmc
;
3234 D2(printk("***jffs_garbage_collect_now(): fmc->dirty_size = %u, fmc->free_size = 0x%x\n, fcs1=0x%x, fcs2=0x%x",
3235 fmc
->dirty_size
, fmc
->free_size
, jffs_free_size1(fmc
), jffs_free_size2(fmc
)));
3236 D2(jffs_print_fmcontrol(fmc
));
3238 // down(&fmc->gclock);
3240 /* If it is possible to garbage collect, do so. */
3242 while (erased
== 0) {
3243 D1(printk("***jffs_garbage_collect_now(): round #%u, "
3244 "fmc->dirty_size = %u\n", i
++, fmc
->dirty_size
));
3245 D2(jffs_print_fmcontrol(fmc
));
3247 if ((erased
= jffs_try_to_erase(c
)) < 0) {
3248 printk(KERN_WARNING
"JFFS: Error in "
3249 "garbage collector.\n");
3256 if (fmc
->free_size
== 0) {
3258 printk(KERN_ERR
"jffs_garbage_collect_now(): free_size == 0. This is BAD.\n");
3263 if (fmc
->dirty_size
< fmc
->sector_size
) {
3264 /* Actually, we _may_ have been able to free some,
3265 * if there are many overlapping nodes which aren't
3266 * actually marked dirty because they still have
3267 * some valid data in each.
3273 /* Let's dare to make a garbage collect. */
3274 if ((result
= jffs_garbage_collect_next(c
)) < 0) {
3275 printk(KERN_ERR
"JFFS: Something "
3276 "has gone seriously wrong "
3277 "with a garbage collect.\n");
3281 D1(printk(" jffs_garbage_collect_now(): erased: %ld\n", erased
));
3282 DJM(jffs_print_memory_allocation_statistics());
3286 // up(&fmc->gclock);
3288 D3(printk(" jffs_garbage_collect_now(): Leaving...\n"));
3290 printk("jffs_g_c_now(): erased = %ld\n", erased
);
3291 jffs_print_fmcontrol(fmc
);
3294 if (!erased
&& !result
)
3298 } /* jffs_garbage_collect_now() */
3301 /* Determine if it is reasonable to start garbage collection.
3302 We start a gc pass if either:
3303 - The number of free bytes < MIN_FREE_BYTES && at least one
3305 - The number of dirty bytes > MAX_DIRTY_BYTES
3307 static inline int thread_should_wake (struct jffs_control
*c
)
3309 D1(printk (KERN_NOTICE
"thread_should_wake(): free=%d, dirty=%d, blocksize=%d.\n",
3310 c
->fmc
->free_size
, c
->fmc
->dirty_size
, c
->fmc
->sector_size
));
3312 /* If there's not enough dirty space to free a block, there's no point. */
3313 if (c
->fmc
->dirty_size
< c
->fmc
->sector_size
) {
3314 D2(printk(KERN_NOTICE
"thread_should_wake(): Not waking. Insufficient dirty space\n"));
3318 /* If there is too much RAM used by the various structures, GC */
3319 if (jffs_get_node_inuse() > (c
->fmc
->used_size
/c
->fmc
->max_chunk_size
* 5 + jffs_get_file_count() * 2 + 50)) {
3320 /* FIXME: Provide proof that this test can be satisfied. We
3321 don't want a filesystem doing endless GC just because this
3322 condition cannot ever be false.
3324 D2(printk(KERN_NOTICE
"thread_should_wake(): Waking due to number of nodes\n"));
3328 /* If there are fewer free bytes than the threshold, GC */
3329 if (c
->fmc
->free_size
< c
->gc_minfree_threshold
) {
3330 D2(printk(KERN_NOTICE
"thread_should_wake(): Waking due to insufficent free space\n"));
3333 /* If there are more dirty bytes than the threshold, GC */
3334 if (c
->fmc
->dirty_size
> c
->gc_maxdirty_threshold
) {
3335 D2(printk(KERN_NOTICE
"thread_should_wake(): Waking due to excessive dirty space\n"));
3338 /* FIXME: What about the "There are many versions of a node" condition? */
3344 void jffs_garbage_collect_trigger(struct jffs_control
*c
)
3346 /* NOTE: We rely on the fact that we have the BKL here.
3347 * Otherwise, the gc_task could go away between the check
3348 * and the wake_up_process()
3350 if (c
->gc_task
&& thread_should_wake(c
))
3351 send_sig(SIGHUP
, c
->gc_task
, 1);
3355 /* Kernel threads take (void *) as arguments. Thus we pass
3356 the jffs_control data as a (void *) and then cast it. */
3358 jffs_garbage_collect_thread(void *ptr
)
3360 struct jffs_control
*c
= (struct jffs_control
*) ptr
;
3361 struct jffs_fmcontrol
*fmc
= c
->fmc
;
3366 daemonize("jffs_gcd");
3368 c
->gc_task
= current
;
3371 init_completion(&c
->gc_thread_comp
); /* barrier */
3372 spin_lock_irq(¤t
->sighand
->siglock
);
3373 siginitsetinv (¤t
->blocked
, sigmask(SIGHUP
) | sigmask(SIGKILL
) | sigmask(SIGSTOP
) | sigmask(SIGCONT
));
3374 recalc_sigpending();
3375 spin_unlock_irq(¤t
->sighand
->siglock
);
3377 D1(printk (KERN_NOTICE
"jffs_garbage_collect_thread(): Starting infinite loop.\n"));
3381 /* See if we need to start gc. If we don't, go to sleep.
3383 Current implementation is a BAD THING(tm). If we try
3384 to unmount the FS, the unmount operation will sleep waiting
3385 for this thread to exit. We need to arrange to send it a
3386 sig before the umount process sleeps.
3389 if (!thread_should_wake(c
))
3390 set_current_state (TASK_INTERRUPTIBLE
);
3392 schedule(); /* Yes, we do this even if we want to go
3393 on immediately - we're a low priority
3396 /* Put_super will send a SIGKILL and then wait on the sem.
3398 while (signal_pending(current
)) {
3400 unsigned long signr
= 0;
3402 spin_lock_irq(¤t
->sighand
->siglock
);
3403 signr
= dequeue_signal(current
, ¤t
->blocked
, &info
);
3404 spin_unlock_irq(¤t
->sighand
->siglock
);
3408 D1(printk("jffs_garbage_collect_thread(): SIGSTOP received.\n"));
3409 set_current_state(TASK_STOPPED
);
3414 D1(printk("jffs_garbage_collect_thread(): SIGKILL received.\n"));
3416 complete_and_exit(&c
->gc_thread_comp
, 0);
3421 D1(printk (KERN_NOTICE
"jffs_garbage_collect_thread(): collecting.\n"));
3423 D3(printk (KERN_NOTICE
"g_c_thread(): down biglock\n"));
3424 down(&fmc
->biglock
);
3426 D1(printk("***jffs_garbage_collect_thread(): round #%u, "
3427 "fmc->dirty_size = %u\n", i
++, fmc
->dirty_size
));
3428 D2(jffs_print_fmcontrol(fmc
));
3430 if ((erased
= jffs_try_to_erase(c
)) < 0) {
3431 printk(KERN_WARNING
"JFFS: Error in "
3432 "garbage collector: %ld.\n", erased
);
3438 if (fmc
->free_size
== 0) {
3439 /* Argh. Might as well commit suicide. */
3440 printk(KERN_ERR
"jffs_garbage_collect_thread(): free_size == 0. This is BAD.\n");
3441 send_sig(SIGQUIT
, c
->gc_task
, 1);
3446 /* Let's dare to make a garbage collect. */
3447 if ((result
= jffs_garbage_collect_next(c
)) < 0) {
3448 printk(KERN_ERR
"JFFS: Something "
3449 "has gone seriously wrong "
3450 "with a garbage collect: %d\n", result
);
3454 D3(printk (KERN_NOTICE
"g_c_thread(): up biglock\n"));
3457 } /* jffs_garbage_collect_thread() */