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