[PATCH] AT91RM9200 Ethernet: Add netpoll / netconsole support
[sfrench/cifs-2.6.git] / fs / jffs / jffs_fm.c
1 /*
2  * JFFS -- Journaling Flash File System, Linux implementation.
3  *
4  * Copyright (C) 1999, 2000  Axis Communications AB.
5  *
6  * Created by Finn Hakansson <finn@axis.com>.
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * $Id: jffs_fm.c,v 1.27 2001/09/20 12:29:47 dwmw2 Exp $
14  *
15  * Ported to Linux 2.3.x and MTD:
16  * Copyright (C) 2000  Alexander Larsson (alex@cendio.se), Cendio Systems AB
17  *
18  */
19 #include <linux/slab.h>
20 #include <linux/blkdev.h>
21 #include <linux/jffs.h>
22 #include "jffs_fm.h"
23 #include "intrep.h"
24
25 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
26 static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
27 #endif
28
29 static struct jffs_fm *jffs_alloc_fm(void);
30 static void jffs_free_fm(struct jffs_fm *n);
31
32 extern kmem_cache_t     *fm_cache;
33 extern kmem_cache_t     *node_cache;
34
35 #if CONFIG_JFFS_FS_VERBOSE > 0
36 void
37 jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
38 {
39         D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
40         D(printk("{\n"));
41         D(printk("        %u, /* flash_size  */\n", fmc->flash_size));
42         D(printk("        %u, /* used_size  */\n", fmc->used_size));
43         D(printk("        %u, /* dirty_size  */\n", fmc->dirty_size));
44         D(printk("        %u, /* free_size  */\n", fmc->free_size));
45         D(printk("        %u, /* sector_size  */\n", fmc->sector_size));
46         D(printk("        %u, /* min_free_size  */\n", fmc->min_free_size));
47         D(printk("        %u, /* max_chunk_size  */\n", fmc->max_chunk_size));
48         D(printk("        0x%p, /* mtd  */\n", fmc->mtd));
49         D(printk("        0x%p, /* head  */    "
50                  "(head->offset = 0x%08x)\n",
51                  fmc->head, (fmc->head ? fmc->head->offset : 0)));
52         D(printk("        0x%p, /* tail  */    "
53                  "(tail->offset + tail->size = 0x%08x)\n",
54                  fmc->tail,
55                  (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
56         D(printk("        0x%p, /* head_extra  */\n", fmc->head_extra));
57         D(printk("        0x%p, /* tail_extra  */\n", fmc->tail_extra));
58         D(printk("}\n"));
59 }
60 #endif  /*  CONFIG_JFFS_FS_VERBOSE > 0  */
61
62 #if CONFIG_JFFS_FS_VERBOSE > 2
63 static void
64 jffs_print_fm(struct jffs_fm *fm)
65 {
66         D(printk("struct jffs_fm: 0x%p\n", fm));
67         D(printk("{\n"));
68         D(printk("       0x%08x, /* offset  */\n", fm->offset));
69         D(printk("       %u, /* size  */\n", fm->size));
70         D(printk("       0x%p, /* prev  */\n", fm->prev));
71         D(printk("       0x%p, /* next  */\n", fm->next));
72         D(printk("       0x%p, /* nodes  */\n", fm->nodes));
73         D(printk("}\n"));
74 }
75 #endif  /*  CONFIG_JFFS_FS_VERBOSE > 2  */
76
77 #if 0
78 void
79 jffs_print_node_ref(struct jffs_node_ref *ref)
80 {
81         D(printk("struct jffs_node_ref: 0x%p\n", ref));
82         D(printk("{\n"));
83         D(printk("       0x%p, /* node  */\n", ref->node));
84         D(printk("       0x%p, /* next  */\n", ref->next));
85         D(printk("}\n"));
86 }
87 #endif  /*  0  */
88
89 /* This function creates a new shiny flash memory control structure.  */
90 struct jffs_fmcontrol *
91 jffs_build_begin(struct jffs_control *c, int unit)
92 {
93         struct jffs_fmcontrol *fmc;
94         struct mtd_info *mtd;
95         
96         D3(printk("jffs_build_begin()\n"));
97         fmc = kmalloc(sizeof(*fmc), GFP_KERNEL);
98         if (!fmc) {
99                 D(printk("jffs_build_begin(): Allocation of "
100                          "struct jffs_fmcontrol failed!\n"));
101                 return (struct jffs_fmcontrol *)0;
102         }
103         DJM(no_jffs_fmcontrol++);
104
105         mtd = get_mtd_device(NULL, unit);
106
107         if (!mtd) {
108                 kfree(fmc);
109                 DJM(no_jffs_fmcontrol--);
110                 return NULL;
111         }
112         
113         /* Retrieve the size of the flash memory.  */
114         fmc->flash_size = mtd->size;
115         D3(printk("  fmc->flash_size = %d bytes\n", fmc->flash_size));
116
117         fmc->used_size = 0;
118         fmc->dirty_size = 0;
119         fmc->free_size = mtd->size;
120         fmc->sector_size = mtd->erasesize;
121         fmc->max_chunk_size = fmc->sector_size >> 1;
122         /* min_free_size:
123            1 sector, obviously.
124            + 1 x max_chunk_size, for when a nodes overlaps the end of a sector
125            + 1 x max_chunk_size again, which ought to be enough to handle 
126                    the case where a rename causes a name to grow, and GC has
127                    to write out larger nodes than the ones it's obsoleting.
128                    We should fix it so it doesn't have to write the name
129                    _every_ time. Later.
130            + another 2 sectors because people keep getting GC stuck and
131                    we don't know why. This scares me - I want formal proof
132                    of correctness of whatever number we put here. dwmw2.
133         */
134         fmc->min_free_size = fmc->sector_size << 2;
135         fmc->mtd = mtd;
136         fmc->c = c;
137         fmc->head = NULL;
138         fmc->tail = NULL;
139         fmc->head_extra = NULL;
140         fmc->tail_extra = NULL;
141         mutex_init(&fmc->biglock);
142         return fmc;
143 }
144
145
146 /* When the flash memory scan has completed, this function should be called
147    before use of the control structure.  */
148 void
149 jffs_build_end(struct jffs_fmcontrol *fmc)
150 {
151         D3(printk("jffs_build_end()\n"));
152
153         if (!fmc->head) {
154                 fmc->head = fmc->head_extra;
155                 fmc->tail = fmc->tail_extra;
156         }
157         else if (fmc->head_extra) {
158                 fmc->tail_extra->next = fmc->head;
159                 fmc->head->prev = fmc->tail_extra;
160                 fmc->head = fmc->head_extra;
161         }
162         fmc->head_extra = NULL; /* These two instructions should be omitted.  */
163         fmc->tail_extra = NULL;
164         D3(jffs_print_fmcontrol(fmc));
165 }
166
167
168 /* Call this function when the file system is unmounted.  This function
169    frees all memory used by this module.  */
170 void
171 jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
172 {
173         if (fmc) {
174                 struct jffs_fm *next = fmc->head;
175                 while (next) {
176                         struct jffs_fm *cur = next;
177                         next = next->next;
178                         jffs_free_fm(cur);
179                 }
180                 put_mtd_device(fmc->mtd);
181                 kfree(fmc);
182                 DJM(no_jffs_fmcontrol--);
183         }
184 }
185
186
187 /* This function returns the size of the first chunk of free space on the
188    flash memory.  This function will return something nonzero if the flash
189    memory contains any free space.  */
190 __u32
191 jffs_free_size1(struct jffs_fmcontrol *fmc)
192 {
193         __u32 head;
194         __u32 tail;
195         __u32 end = fmc->flash_size;
196
197         if (!fmc->head) {
198                 /* There is nothing on the flash.  */
199                 return fmc->flash_size;
200         }
201
202         /* Compute the beginning and ending of the contents of the flash.  */
203         head = fmc->head->offset;
204         tail = fmc->tail->offset + fmc->tail->size;
205         if (tail == end) {
206                 tail = 0;
207         }
208         ASSERT(else if (tail > end) {
209                 printk(KERN_WARNING "jffs_free_size1(): tail > end\n");
210                 tail = 0;
211         });
212
213         if (head <= tail) {
214                 return end - tail;
215         }
216         else {
217                 return head - tail;
218         }
219 }
220
221 /* This function will return something nonzero in case there are two free
222    areas on the flash.  Like this:
223
224      +----------------+------------------+----------------+
225      |     FREE 1     |   USED / DIRTY   |     FREE 2     |
226      +----------------+------------------+----------------+
227        fmc->head -----^
228        fmc->tail ------------------------^
229
230    The value returned, will be the size of the first empty area on the
231    flash, in this case marked "FREE 1".  */
232 __u32
233 jffs_free_size2(struct jffs_fmcontrol *fmc)
234 {
235         if (fmc->head) {
236                 __u32 head = fmc->head->offset;
237                 __u32 tail = fmc->tail->offset + fmc->tail->size;
238                 if (tail == fmc->flash_size) {
239                         tail = 0;
240                 }
241
242                 if (tail >= head) {
243                         return head;
244                 }
245         }
246         return 0;
247 }
248
249
250 /* Allocate a chunk of flash memory.  If there is enough space on the
251    device, a reference to the associated node is stored in the jffs_fm
252    struct.  */
253 int
254 jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
255              struct jffs_fm **result)
256 {
257         struct jffs_fm *fm;
258         __u32 free_chunk_size1;
259         __u32 free_chunk_size2;
260
261         D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
262                   "node = 0x%p\n", fmc, size, node));
263
264         *result = NULL;
265
266         if (!(fm = jffs_alloc_fm())) {
267                 D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
268                 return -ENOMEM;
269         }
270
271         free_chunk_size1 = jffs_free_size1(fmc);
272         free_chunk_size2 = jffs_free_size2(fmc);
273         if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
274                 printk(KERN_WARNING "Free size accounting screwed\n");
275                 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);
276         }
277
278         D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
279                   "free_chunk_size2 = %u\n",
280                   free_chunk_size1, free_chunk_size2));
281
282         if (size <= free_chunk_size1) {
283                 if (!(fm->nodes = (struct jffs_node_ref *)
284                                   kmalloc(sizeof(struct jffs_node_ref),
285                                           GFP_KERNEL))) {
286                         D(printk("jffs_fmalloc(): kmalloc() failed! "
287                                  "(node_ref)\n"));
288                         jffs_free_fm(fm);
289                         return -ENOMEM;
290                 }
291                 DJM(no_jffs_node_ref++);
292                 fm->nodes->node = node;
293                 fm->nodes->next = NULL;
294                 if (fmc->tail) {
295                         fm->offset = fmc->tail->offset + fmc->tail->size;
296                         if (fm->offset == fmc->flash_size) {
297                                 fm->offset = 0;
298                         }
299                         ASSERT(else if (fm->offset > fmc->flash_size) {
300                                 printk(KERN_WARNING "jffs_fmalloc(): "
301                                        "offset > flash_end\n");
302                                 fm->offset = 0;
303                         });
304                 }
305                 else {
306                         /* There don't have to be files in the file
307                            system yet.  */
308                         fm->offset = 0;
309                 }
310                 fm->size = size;
311                 fmc->free_size -= size;
312                 fmc->used_size += size;
313         }
314         else if (size > free_chunk_size2) {
315                 printk(KERN_WARNING "JFFS: Tried to allocate a too "
316                        "large flash memory chunk. (size = %u)\n", size);
317                 jffs_free_fm(fm);
318                 return -ENOSPC;
319         }
320         else {
321                 fm->offset = fmc->tail->offset + fmc->tail->size;
322                 fm->size = free_chunk_size1;
323                 fm->nodes = NULL;
324                 fmc->free_size -= fm->size;
325                 fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a 
326                                                 bug that caused infinite garbage collection.
327                                                 It previously set fmc->dirty_size to size (which is the
328                                                 size of the requested chunk).
329                                              */
330         }
331
332         fm->next = NULL;
333         if (!fmc->head) {
334                 fm->prev = NULL;
335                 fmc->head = fm;
336                 fmc->tail = fm;
337         }
338         else {
339                 fm->prev = fmc->tail;
340                 fmc->tail->next = fm;
341                 fmc->tail = fm;
342         }
343
344         D3(jffs_print_fmcontrol(fmc));
345         D3(jffs_print_fm(fm));
346         *result = fm;
347         return 0;
348 }
349
350
351 /* The on-flash space is not needed anymore by the passed node.  Remove
352    the reference to the node from the node list.  If the data chunk in
353    the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
354    then mark that chunk as dirty.  */
355 int
356 jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *node)
357 {
358         struct jffs_node_ref *ref;
359         struct jffs_node_ref *prev;
360         ASSERT(int del = 0);
361
362         D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
363                  node->ino, node->version));
364
365         ASSERT(if (!fmc || !fm || !fm->nodes) {
366                 printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
367                        "fm->nodes: 0x%p\n",
368                        fmc, fm, (fm ? fm->nodes : NULL));
369                 return -1;
370         });
371
372         /* Find the reference to the node that is going to be removed
373            and remove it.  */
374         for (ref = fm->nodes, prev = NULL; ref; ref = ref->next) {
375                 if (ref->node == node) {
376                         if (prev) {
377                                 prev->next = ref->next;
378                         }
379                         else {
380                                 fm->nodes = ref->next;
381                         }
382                         kfree(ref);
383                         DJM(no_jffs_node_ref--);
384                         ASSERT(del = 1);
385                         break;
386                 }
387                 prev = ref;
388         }
389
390         /* If the data chunk in the flash memory isn't used anymore
391            just mark it as obsolete.  */
392         if (!fm->nodes) {
393                 /* No node uses this chunk so let's remove it.  */
394                 fmc->used_size -= fm->size;
395                 fmc->dirty_size += fm->size;
396 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
397                 if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
398                         D1(printk("jffs_fmfree(): Failed to mark an on-flash "
399                                   "node obsolete!\n"));
400                         return -1;
401                 }
402 #endif
403         }
404
405         ASSERT(if (!del) {
406                 printk(KERN_WARNING "***jffs_fmfree(): "
407                        "Didn't delete any node reference!\n");
408         });
409
410         return 0;
411 }
412
413
414 /* This allocation function is used during the initialization of
415    the file system.  */
416 struct jffs_fm *
417 jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
418                struct jffs_node *node)
419 {
420         struct jffs_fm *fm;
421
422         D3(printk("jffs_fmalloced()\n"));
423
424         if (!(fm = jffs_alloc_fm())) {
425                 D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
426                          fmc, offset, size, node));
427                 return NULL;
428         }
429         fm->offset = offset;
430         fm->size = size;
431         fm->prev = NULL;
432         fm->next = NULL;
433         fm->nodes = NULL;
434         if (node) {
435                 /* `node' exists and it should be associated with the
436                     jffs_fm structure `fm'.  */
437                 if (!(fm->nodes = (struct jffs_node_ref *)
438                                   kmalloc(sizeof(struct jffs_node_ref),
439                                           GFP_KERNEL))) {
440                         D(printk("jffs_fmalloced(): !fm->nodes\n"));
441                         jffs_free_fm(fm);
442                         return NULL;
443                 }
444                 DJM(no_jffs_node_ref++);
445                 fm->nodes->node = node;
446                 fm->nodes->next = NULL;
447                 fmc->used_size += size;
448                 fmc->free_size -= size;
449         }
450         else {
451                 /* If there is no node, then this is just a chunk of dirt.  */
452                 fmc->dirty_size += size;
453                 fmc->free_size -= size;
454         }
455
456         if (fmc->head_extra) {
457                 fm->prev = fmc->tail_extra;
458                 fmc->tail_extra->next = fm;
459                 fmc->tail_extra = fm;
460         }
461         else if (!fmc->head) {
462                 fmc->head = fm;
463                 fmc->tail = fm;
464         }
465         else if (fmc->tail->offset + fmc->tail->size < offset) {
466                 fmc->head_extra = fm;
467                 fmc->tail_extra = fm;
468         }
469         else {
470                 fm->prev = fmc->tail;
471                 fmc->tail->next = fm;
472                 fmc->tail = fm;
473         }
474         D3(jffs_print_fmcontrol(fmc));
475         D3(jffs_print_fm(fm));
476         return fm;
477 }
478
479
480 /* Add a new node to an already existing jffs_fm struct.  */
481 int
482 jffs_add_node(struct jffs_node *node)
483 {
484         struct jffs_node_ref *ref;
485
486         D3(printk("jffs_add_node(): ino = %u\n", node->ino));
487
488         ref = kmalloc(sizeof(*ref), GFP_KERNEL);
489         if (!ref)
490                 return -ENOMEM;
491
492         DJM(no_jffs_node_ref++);
493         ref->node = node;
494         ref->next = node->fm->nodes;
495         node->fm->nodes = ref;
496         return 0;
497 }
498
499
500 /* Free a part of some allocated space.  */
501 void
502 jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
503 {
504         D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
505                   "fm->nodes->node->ino = %u, size = %u\n",
506                   fm, (fm ? fm->nodes : 0),
507                   (!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));
508
509         if (fm->nodes) {
510                 kfree(fm->nodes);
511                 DJM(no_jffs_node_ref--);
512                 fm->nodes = NULL;
513         }
514         fmc->used_size -= fm->size;
515         if (fm == fmc->tail) {
516                 fm->size -= size;
517                 fmc->free_size += size;
518         }
519         fmc->dirty_size += fm->size;
520 }
521
522
523 /* Find the jffs_fm struct that contains the end of the data chunk that
524    begins at the logical beginning of the flash memory and spans `size'
525    bytes.  If we want to erase a sector of the flash memory, we use this
526    function to find where the sector limit cuts a chunk of data.  */
527 struct jffs_fm *
528 jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
529 {
530         struct jffs_fm *fm;
531         __u32 pos = 0;
532
533         if (size == 0) {
534                 return NULL;
535         }
536
537         ASSERT(if (!fmc) {
538                 printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
539                 return NULL;
540         });
541
542         fm = fmc->head;
543
544         while (fm) {
545                 pos += fm->size;
546                 if (pos < size) {
547                         fm = fm->next;
548                 }
549                 else if (pos > size) {
550                         break;
551                 }
552                 else {
553                         fm = NULL;
554                         break;
555                 }
556         }
557
558         return fm;
559 }
560
561
562 /* Move the head of the fmc structures and delete the obsolete parts.  */
563 void
564 jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
565 {
566         struct jffs_fm *fm;
567         struct jffs_fm *del;
568
569         ASSERT(if (!fmc) {
570                 printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
571                 return;
572         });
573
574         fmc->dirty_size -= erased_size;
575         fmc->free_size += erased_size;
576
577         for (fm = fmc->head; fm && (erased_size > 0);) {
578                 if (erased_size >= fm->size) {
579                         erased_size -= fm->size;
580                         del = fm;
581                         fm = fm->next;
582                         fm->prev = NULL;
583                         fmc->head = fm;
584                         jffs_free_fm(del);
585                 }
586                 else {
587                         fm->size -= erased_size;
588                         fm->offset += erased_size;
589                         break;
590                 }
591         }
592 }
593
594
595 /* Return the oldest used node in the flash memory.  */
596 struct jffs_node *
597 jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
598 {
599         struct jffs_fm *fm;
600         struct jffs_node_ref *nref;
601         struct jffs_node *node = NULL;
602
603         ASSERT(if (!fmc) {
604                 printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
605                 return NULL;
606         });
607
608         for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
609
610         if (!fm) {
611                 return NULL;
612         }
613
614         /* The oldest node is the last one in the reference list.  This list
615            shouldn't be too long; just one or perhaps two elements.  */
616         for (nref = fm->nodes; nref; nref = nref->next) {
617                 node = nref->node;
618         }
619
620         D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
621                   (node ? node->ino : 0), (node ? node->version : 0)));
622
623         return node;
624 }
625
626
627 #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
628
629 /* Mark an on-flash node as obsolete.
630
631    Note that this is just an optimization that isn't necessary for the
632    filesystem to work.  */
633
634 static int
635 jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
636 {
637         /* The `accurate_pos' holds the position of the accurate byte
638            in the jffs_raw_inode structure that we are going to mark
639            as obsolete.  */
640         __u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
641         unsigned char zero = 0x00;
642         size_t len;
643
644         D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
645         ASSERT(if (!fmc) {
646                 printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
647                 return -1;
648         });
649
650         /* Write 0x00 to the raw inode's accurate member.  Don't care
651            about the return value.  */
652         MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero);
653         return 0;
654 }
655
656 #endif /* JFFS_MARK_OBSOLETE  */
657
658 /* check if it's possible to erase the wanted range, and if not, return
659  * the range that IS erasable, or a negative error code.
660  */
661 static long
662 jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
663 {
664          u_long ssize;
665
666         /* assume that sector size for a partition is constant even
667          * if it spans more than one chip (you usually put the same
668          * type of chips in a system)
669          */
670
671         ssize = mtd->erasesize;
672
673         if (offset % ssize) {
674                 printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize);
675                 /* The offset is not sector size aligned.  */
676                 return -1;
677         }
678         else if (offset > mtd->size) {
679                 printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
680                 return -2;
681         }
682         else if (offset + size > mtd->size) {
683                 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);
684                 return -3;
685         }
686
687         return (size / ssize) * ssize;
688 }
689
690
691 /* How much dirty flash memory is possible to erase at the moment?  */
692 long
693 jffs_erasable_size(struct jffs_fmcontrol *fmc)
694 {
695         struct jffs_fm *fm;
696         __u32 size = 0;
697         long ret;
698
699         ASSERT(if (!fmc) {
700                 printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
701                 return -1;
702         });
703
704         if (!fmc->head) {
705                 /* The flash memory is totally empty. No nodes. No dirt.
706                    Just return.  */
707                 return 0;
708         }
709
710         /* Calculate how much space that is dirty.  */
711         for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
712                 if (size && fm->offset == 0) {
713                         /* We have reached the beginning of the flash.  */
714                         break;
715                 }
716                 size += fm->size;
717         }
718
719         /* Someone's signature contained this:
720            There's a fine line between fishing and just standing on
721            the shore like an idiot...  */
722         ret = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, size);
723
724         ASSERT(if (ret < 0) {
725                 printk("jffs_erasable_size: flash_erasable_size() "
726                        "returned something less than zero (%ld).\n", ret);
727                 printk("jffs_erasable_size: offset = 0x%08x\n",
728                        fmc->head->offset);
729         });
730
731         /* If there is dirt on the flash (which is the reason to why
732            this function was called in the first place) but no space is
733            possible to erase right now, the initial part of the list of
734            jffs_fm structs, that hold place for dirty space, could perhaps
735            be shortened.  The list's initial "dirty" elements are merged
736            into just one large dirty jffs_fm struct.  This operation must
737            only be performed if nothing is possible to erase.  Otherwise,
738            jffs_clear_end_of_node() won't work as expected.  */
739         if (ret == 0) {
740                 struct jffs_fm *head = fmc->head;
741                 struct jffs_fm *del;
742                 /* While there are two dirty nodes beside each other.*/
743                 while (head->nodes == 0
744                        && head->next
745                        && head->next->nodes == 0) {
746                         del = head->next;
747                         head->size += del->size;
748                         head->next = del->next;
749                         if (del->next) {
750                                 del->next->prev = head;
751                         }
752                         jffs_free_fm(del);
753                 }
754         }
755
756         return (ret >= 0 ? ret : 0);
757 }
758
759 static struct jffs_fm *jffs_alloc_fm(void)
760 {
761         struct jffs_fm *fm;
762
763         fm = kmem_cache_alloc(fm_cache,GFP_KERNEL);
764         DJM(if (fm) no_jffs_fm++;);
765         
766         return fm;
767 }
768
769 static void jffs_free_fm(struct jffs_fm *n)
770 {
771         kmem_cache_free(fm_cache,n);
772         DJM(no_jffs_fm--);
773 }
774
775
776
777 struct jffs_node *jffs_alloc_node(void)
778 {
779         struct jffs_node *n;
780
781         n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL);
782         if(n != NULL)
783                 no_jffs_node++;
784         return n;
785 }
786
787 void jffs_free_node(struct jffs_node *n)
788 {
789         kmem_cache_free(node_cache,n);
790         no_jffs_node--;
791 }
792
793
794 int jffs_get_node_inuse(void)
795 {
796         return no_jffs_node;
797 }