2 * JFFS -- Journaling Flash File System, Linux implementation.
4 * Copyright (C) 1999, 2000 Axis Communications AB.
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: jffs_fm.c,v 1.27 2001/09/20 12:29:47 dwmw2 Exp $
15 * Ported to Linux 2.3.x and MTD:
16 * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
19 #define __NO_VERSION__
20 #include <linux/slab.h>
21 #include <linux/blkdev.h>
22 #include <linux/jffs.h>
25 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
26 static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
29 extern kmem_cache_t *fm_cache;
30 extern kmem_cache_t *node_cache;
32 /* This function creates a new shiny flash memory control structure. */
33 struct jffs_fmcontrol *
34 jffs_build_begin(struct jffs_control *c, kdev_t dev)
36 struct jffs_fmcontrol *fmc;
39 D3(printk("jffs_build_begin()\n"));
40 fmc = (struct jffs_fmcontrol *)kmalloc(sizeof(struct jffs_fmcontrol),
43 D(printk("jffs_build_begin(): Allocation of "
44 "struct jffs_fmcontrol failed!\n"));
45 return (struct jffs_fmcontrol *)0;
47 DJM(no_jffs_fmcontrol++);
49 mtd = get_mtd_device(NULL, MINOR(dev));
53 DJM(no_jffs_fmcontrol--);
57 /* Retrieve the size of the flash memory. */
58 fmc->flash_size = mtd->size;
59 D3(printk(" fmc->flash_size = %d bytes\n", fmc->flash_size));
63 fmc->free_size = mtd->size;
64 fmc->sector_size = mtd->erasesize;
65 fmc->max_chunk_size = fmc->sector_size >> 1;
68 + 1 x max_chunk_size, for when a nodes overlaps the end of a sector
69 + 1 x max_chunk_size again, which ought to be enough to handle
70 the case where a rename causes a name to grow, and GC has
71 to write out larger nodes than the ones it's obsoleting.
72 We should fix it so it doesn't have to write the name
74 + another 2 sectors because people keep getting GC stuck and
75 we don't know why. This scares me - I want formal proof
76 of correctness of whatever number we put here. dwmw2.
78 fmc->min_free_size = fmc->sector_size << 2;
85 init_MUTEX(&fmc->biglock);
90 /* When the flash memory scan has completed, this function should be called
91 before use of the control structure. */
93 jffs_build_end(struct jffs_fmcontrol *fmc)
95 D3(printk("jffs_build_end()\n"));
98 fmc->head = fmc->head_extra;
99 fmc->tail = fmc->tail_extra;
101 else if (fmc->head_extra) {
102 fmc->tail_extra->next = fmc->head;
103 fmc->head->prev = fmc->tail_extra;
104 fmc->head = fmc->head_extra;
106 fmc->head_extra = 0; /* These two instructions should be omitted. */
108 D3(jffs_print_fmcontrol(fmc));
112 /* Call this function when the file system is unmounted. This function
113 frees all memory used by this module. */
115 jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
119 struct jffs_fm *next = fmc->head;
121 while ((cur = next)) {
125 put_mtd_device(fmc->mtd);
127 DJM(no_jffs_fmcontrol--);
132 /* This function returns the size of the first chunk of free space on the
133 flash memory. This function will return something nonzero if the flash
134 memory contains any free space. */
136 jffs_free_size1(struct jffs_fmcontrol *fmc)
140 __u32 end = fmc->flash_size;
143 /* There is nothing on the flash. */
144 return fmc->flash_size;
147 /* Compute the beginning and ending of the contents of the flash. */
148 head = fmc->head->offset;
149 tail = fmc->tail->offset + fmc->tail->size;
153 ASSERT(else if (tail > end) {
154 printk(KERN_WARNING "jffs_free_size1(): tail > end\n");
166 /* This function will return something nonzero in case there are two free
167 areas on the flash. Like this:
169 +----------------+------------------+----------------+
170 | FREE 1 | USED / DIRTY | FREE 2 |
171 +----------------+------------------+----------------+
173 fmc->tail ------------------------^
175 The value returned, will be the size of the first empty area on the
176 flash, in this case marked "FREE 1". */
178 jffs_free_size2(struct jffs_fmcontrol *fmc)
181 __u32 head = fmc->head->offset;
182 __u32 tail = fmc->tail->offset + fmc->tail->size;
183 if (tail == fmc->flash_size) {
195 /* Allocate a chunk of flash memory. If there is enough space on the
196 device, a reference to the associated node is stored in the jffs_fm
199 jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
200 struct jffs_fm **result)
203 __u32 free_chunk_size1;
204 __u32 free_chunk_size2;
206 D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
207 "node = 0x%p\n", fmc, size, node));
211 if (!(fm = jffs_alloc_fm())) {
212 D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
216 free_chunk_size1 = jffs_free_size1(fmc);
217 free_chunk_size2 = jffs_free_size2(fmc);
218 if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
219 printk(KERN_WARNING "Free size accounting screwed\n");
220 printk(KERN_WARNING "free_chunk_size1 == 0x%x, free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n", free_chunk_size1, free_chunk_size2, fmc->free_size);
223 D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
224 "free_chunk_size2 = %u\n",
225 free_chunk_size1, free_chunk_size2));
227 if (size <= free_chunk_size1) {
228 if (!(fm->nodes = (struct jffs_node_ref *)
229 kmalloc(sizeof(struct jffs_node_ref),
231 D(printk("jffs_fmalloc(): kmalloc() failed! "
236 DJM(no_jffs_node_ref++);
237 fm->nodes->node = node;
240 fm->offset = fmc->tail->offset + fmc->tail->size;
241 if (fm->offset == fmc->flash_size) {
244 ASSERT(else if (fm->offset > fmc->flash_size) {
245 printk(KERN_WARNING "jffs_fmalloc(): "
246 "offset > flash_end\n");
251 /* There don't have to be files in the file
256 fmc->free_size -= size;
257 fmc->used_size += size;
259 else if (size > free_chunk_size2) {
260 printk(KERN_WARNING "JFFS: Tried to allocate a too "
261 "large flash memory chunk. (size = %u)\n", size);
266 fm->offset = fmc->tail->offset + fmc->tail->size;
267 fm->size = free_chunk_size1;
269 fmc->free_size -= fm->size;
270 fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a
271 bug that caused infinite garbage collection.
272 It previously set fmc->dirty_size to size (which is the
273 size of the requested chunk).
284 fm->prev = fmc->tail;
285 fmc->tail->next = fm;
289 D3(jffs_print_fmcontrol(fmc));
290 D3(jffs_print_fm(fm));
296 /* The on-flash space is not needed anymore by the passed node. Remove
297 the reference to the node from the node list. If the data chunk in
298 the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
299 then mark that chunk as dirty. */
301 jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *node)
303 struct jffs_node_ref *ref;
304 struct jffs_node_ref *prev;
307 D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
308 node->ino, node->version));
310 ASSERT(if (!fmc || !fm || !fm->nodes) {
311 printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
313 fmc, fm, (fm ? fm->nodes : 0));
317 /* Find the reference to the node that is going to be removed
319 for (ref = fm->nodes, prev = 0; ref; ref = ref->next) {
320 if (ref->node == node) {
322 prev->next = ref->next;
325 fm->nodes = ref->next;
328 DJM(no_jffs_node_ref--);
335 /* If the data chunk in the flash memory isn't used anymore
336 just mark it as obsolete. */
338 /* No node uses this chunk so let's remove it. */
339 fmc->used_size -= fm->size;
340 fmc->dirty_size += fm->size;
341 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
342 if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
343 D1(printk("jffs_fmfree(): Failed to mark an on-flash "
344 "node obsolete!\n"));
351 printk(KERN_WARNING "***jffs_fmfree(): "
352 "Didn't delete any node reference!\n");
359 /* This allocation function is used during the initialization of
362 jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
363 struct jffs_node *node)
367 D3(printk("jffs_fmalloced()\n"));
369 if (!(fm = jffs_alloc_fm())) {
370 D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
371 fmc, offset, size, node));
380 /* `node' exists and it should be associated with the
381 jffs_fm structure `fm'. */
382 if (!(fm->nodes = (struct jffs_node_ref *)
383 kmalloc(sizeof(struct jffs_node_ref),
385 D(printk("jffs_fmalloced(): !fm->nodes\n"));
389 DJM(no_jffs_node_ref++);
390 fm->nodes->node = node;
392 fmc->used_size += size;
393 fmc->free_size -= size;
396 /* If there is no node, then this is just a chunk of dirt. */
397 fmc->dirty_size += size;
398 fmc->free_size -= size;
401 if (fmc->head_extra) {
402 fm->prev = fmc->tail_extra;
403 fmc->tail_extra->next = fm;
404 fmc->tail_extra = fm;
406 else if (!fmc->head) {
410 else if (fmc->tail->offset + fmc->tail->size < offset) {
411 fmc->head_extra = fm;
412 fmc->tail_extra = fm;
415 fm->prev = fmc->tail;
416 fmc->tail->next = fm;
419 D3(jffs_print_fmcontrol(fmc));
420 D3(jffs_print_fm(fm));
425 /* Add a new node to an already existing jffs_fm struct. */
427 jffs_add_node(struct jffs_node *node)
429 struct jffs_node_ref *ref;
431 D3(printk("jffs_add_node(): ino = %u\n", node->ino));
433 ref = (struct jffs_node_ref *)kmalloc(sizeof(struct jffs_node_ref),
438 DJM(no_jffs_node_ref++);
440 ref->next = node->fm->nodes;
441 node->fm->nodes = ref;
446 /* Free a part of some allocated space. */
448 jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
450 D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
451 "fm->nodes->node->ino = %u, size = %u\n",
452 fm, (fm ? fm->nodes : 0),
453 (!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));
457 DJM(no_jffs_node_ref--);
460 fmc->used_size -= fm->size;
461 if (fm == fmc->tail) {
463 fmc->free_size += size;
465 fmc->dirty_size += fm->size;
469 /* Find the jffs_fm struct that contains the end of the data chunk that
470 begins at the logical beginning of the flash memory and spans `size'
471 bytes. If we want to erase a sector of the flash memory, we use this
472 function to find where the sector limit cuts a chunk of data. */
474 jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
484 printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
495 else if (pos > size) {
508 /* Move the head of the fmc structures and delete the obsolete parts. */
510 jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
516 printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
520 fmc->dirty_size -= erased_size;
521 fmc->free_size += erased_size;
523 for (fm = fmc->head; fm && (erased_size > 0);) {
524 if (erased_size >= fm->size) {
525 erased_size -= fm->size;
533 fm->size -= erased_size;
534 fm->offset += erased_size;
541 /* Return the oldest used node in the flash memory. */
543 jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
546 struct jffs_node_ref *nref;
547 struct jffs_node *node = 0;
550 printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
554 for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
560 /* The oldest node is the last one in the reference list. This list
561 shouldn't be too long; just one or perhaps two elements. */
562 for (nref = fm->nodes; nref; nref = nref->next) {
566 D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
567 (node ? node->ino : 0), (node ? node->version : 0)));
573 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
575 /* Mark an on-flash node as obsolete.
577 Note that this is just an optimization that isn't necessary for the
578 filesystem to work. */
581 jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
583 /* The `accurate_pos' holds the position of the accurate byte
584 in the jffs_raw_inode structure that we are going to mark
586 __u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
587 unsigned char zero = 0x00;
590 D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
592 printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
596 /* Write 0x00 to the raw inode's accurate member. Don't care
597 about the return value. */
598 MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero);
602 #endif /* JFFS_MARK_OBSOLETE */
604 /* check if it's possible to erase the wanted range, and if not, return
605 * the range that IS erasable, or a negative error code.
608 jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
612 /* assume that sector size for a partition is constant even
613 * if it spans more than one chip (you usually put the same
614 * type of chips in a system)
617 ssize = mtd->erasesize;
619 if (offset % ssize) {
620 printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize);
621 /* The offset is not sector size aligned. */
624 else if (offset > mtd->size) {
625 printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
628 else if (offset + size > mtd->size) {
629 printk(KERN_WARNING "jffs_flash_erasable_size() given length which runs off the end of device (ofs %x + len %x = %x, > %x)\n", offset,size, offset+size, mtd->size);
633 return (size / ssize) * ssize;
637 /* How much dirty flash memory is possible to erase at the moment? */
639 jffs_erasable_size(struct jffs_fmcontrol *fmc)
646 printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
651 /* The flash memory is totally empty. No nodes. No dirt.
656 /* Calculate how much space that is dirty. */
657 for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
658 if (size && fm->offset == 0) {
659 /* We have reached the beginning of the flash. */
665 /* Someone's signature contained this:
666 There's a fine line between fishing and just standing on
667 the shore like an idiot... */
668 ret = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, size);
670 ASSERT(if (ret < 0) {
671 printk("jffs_erasable_size: flash_erasable_size() "
672 "returned something less than zero (%ld).\n", ret);
673 printk("jffs_erasable_size: offset = 0x%08x\n",
677 /* If there is dirt on the flash (which is the reason to why
678 this function was called in the first place) but no space is
679 possible to erase right now, the initial part of the list of
680 jffs_fm structs, that hold place for dirty space, could perhaps
681 be shortened. The list's initial "dirty" elements are merged
682 into just one large dirty jffs_fm struct. This operation must
683 only be performed if nothing is possible to erase. Otherwise,
684 jffs_clear_end_of_node() won't work as expected. */
686 struct jffs_fm *head = fmc->head;
688 /* While there are two dirty nodes beside each other.*/
689 while (head->nodes == 0
691 && head->next->nodes == 0) {
693 head->size += del->size;
694 head->next = del->next;
696 del->next->prev = head;
702 return (ret >= 0 ? ret : 0);
705 struct jffs_fm *jffs_alloc_fm(void)
709 fm = kmem_cache_alloc(fm_cache,GFP_KERNEL);
710 DJM(if (fm) no_jffs_fm++;);
715 void jffs_free_fm(struct jffs_fm *n)
717 kmem_cache_free(fm_cache,n);
723 struct jffs_node *jffs_alloc_node(void)
727 n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL);
733 void jffs_free_node(struct jffs_node *n)
735 kmem_cache_free(node_cache,n);
740 int jffs_get_node_inuse(void)
746 jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
748 D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
750 D(printk(" %u, /* flash_size */\n", fmc->flash_size));
751 D(printk(" %u, /* used_size */\n", fmc->used_size));
752 D(printk(" %u, /* dirty_size */\n", fmc->dirty_size));
753 D(printk(" %u, /* free_size */\n", fmc->free_size));
754 D(printk(" %u, /* sector_size */\n", fmc->sector_size));
755 D(printk(" %u, /* min_free_size */\n", fmc->min_free_size));
756 D(printk(" %u, /* max_chunk_size */\n", fmc->max_chunk_size));
757 D(printk(" 0x%p, /* mtd */\n", fmc->mtd));
758 D(printk(" 0x%p, /* head */ "
759 "(head->offset = 0x%08x)\n",
760 fmc->head, (fmc->head ? fmc->head->offset : 0)));
761 D(printk(" 0x%p, /* tail */ "
762 "(tail->offset + tail->size = 0x%08x)\n",
764 (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
765 D(printk(" 0x%p, /* head_extra */\n", fmc->head_extra));
766 D(printk(" 0x%p, /* tail_extra */\n", fmc->tail_extra));
771 jffs_print_fm(struct jffs_fm *fm)
773 D(printk("struct jffs_fm: 0x%p\n", fm));
775 D(printk(" 0x%08x, /* offset */\n", fm->offset));
776 D(printk(" %u, /* size */\n", fm->size));
777 D(printk(" 0x%p, /* prev */\n", fm->prev));
778 D(printk(" 0x%p, /* next */\n", fm->next));
779 D(printk(" 0x%p, /* nodes */\n", fm->nodes));
784 jffs_print_node_ref(struct jffs_node_ref *ref)
786 D(printk("struct jffs_node_ref: 0x%p\n", ref));
788 D(printk(" 0x%p, /* node */\n", ref->node));
789 D(printk(" 0x%p, /* next */\n", ref->next));