📄 jffs_fm.cpp
字号:
#include <stdlib.h>
//#include "../../comm/cmd.h" livefall
#include <io.h>
#include "../jffs/jffstypes.h"
#include "../jffs/fs.h"
#include "../jffs/errno.h"
#include "../jffs/jffs_fm.h"
#include "../jffs/j_flash.h"
#if defined(CONFIG_JFFS_FS_VERBOSE) && CONFIG_JFFS_FS_VERBOSE
#define D(x) x
#else
#define D(x)
#endif
#define D1(x)
#define D2(x)
#define D3(x)
#define ASSERT(x)
#define printk printf
#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
#endif
__u32 jffs_free_size1(struct jffs_fmcontrol *fmc);
__u32 jffs_free_size2(struct jffs_fmcontrol *fmc);
/* This allocation function is used during the initialization of
the file system. */
struct jffs_fm *jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
struct jffs_node *node)
{
struct jffs_fm *fm;
D3(printk("jffs_fmalloced()\n"));
if (!(fm = (struct jffs_fm *)malloc(sizeof(struct jffs_fm)))) {
D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
fmc, offset, size, node));
return 0;
}
DJM(no_jffs_fm++);
fm->offset = offset;
fm->size = size;
fm->prev = 0;
fm->next = 0;
fm->nodes = 0;
if (node) {
/* `node' exists and it should be associated with the
jffs_fm structure `fm'. */
if (!(fm->nodes = (struct jffs_node_ref *)
malloc(sizeof(struct jffs_node_ref)))) {
D(printk("jffs_fmalloced(): !fm->nodes\n"));
free(fm);
DJM(no_jffs_fm--);
return 0;
}
DJM(no_jffs_node_ref++);
fm->nodes->node = node;
fm->nodes->next = 0;
fmc->used_size += size;
}
else {
/* If there is no node, then this is just a chunk of dirt. */
fmc->dirty_size += size;
}
if (fmc->head_extra) {
fm->prev = fmc->tail_extra;
fmc->tail_extra->next = fm;
fmc->tail_extra = fm;
}
else if (!fmc->head) {
fmc->head = fm;
fmc->tail = fm;
}
else if (fmc->tail->offset + fmc->tail->size < offset) {
fmc->head_extra = fm;
fmc->tail_extra = fm;
}
else {
fm->prev = fmc->tail;
fmc->tail->next = fm;
fmc->tail = fm;
}
D3(jffs_print_fmcontrol(fmc));
D3(jffs_print_fm(fm));
return fm;
}
/* Allocate a chunk of flash memory. If there is enough space on the
device, a reference to the associated node is stored in the jffs_fm
struct. */
int
jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
struct jffs_fm **result)
{
struct jffs_fm *fm;
__u32 free_chunk_size1;
__u32 free_chunk_size2;
D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
"node = 0x%p\n", fmc, size, node));
*result = 0;
if (!(fm = (struct jffs_fm*)malloc(sizeof(struct jffs_fm)))) {
D(printk("jffs_fmalloc(): fm==0\n"));
return -ENOMEM;
}
DJM(no_jffs_fm++);
free_chunk_size1 = jffs_free_size1(fmc);
free_chunk_size2 = jffs_free_size2(fmc);
D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
"free_chunk_size2 = %u\n",
free_chunk_size1, free_chunk_size2));
if (size <= free_chunk_size1) {
if (!(fm->nodes = (struct jffs_node_ref *)
malloc(sizeof(struct jffs_node_ref)))) {
D(printk("jffs_fmalloc(): fm->nodes==0\n"));
free(fm);
DJM(no_jffs_fm--);
return -ENOMEM;
}
DJM(no_jffs_node_ref++);
fm->nodes->node = node;
fm->nodes->next = 0;
if (fmc->tail) {
fm->offset = fmc->tail->offset + fmc->tail->size;
if (fm->offset
== fmc->flash_start + fmc->flash_size) {
fm->offset = fmc->flash_start;
}
ASSERT(else if (fm->offset
> fmc->flash_start
+ fmc->flash_size) {
printk(KERN_WARNING "jffs_fmalloc(): "
"offset > flash_end\n");
fm->offset = fmc->flash_start;
});
}
else {
/* There don't have to be files in the file
system yet. */
fm->offset = fmc->flash_start;
}
fm->size = size;
fmc->used_size += size;
}
else if (size > free_chunk_size2) {
/*printk(KERN_WARNING "JFFS: Tried to allocate a too "
"large flash memory chunk. (size = %u)\n", size);*/
free(fm);
DJM(no_jffs_fm--);
return -ENOSPC;
}
else {
fm->offset = fmc->tail->offset + fmc->tail->size;
fm->size = free_chunk_size1;
fm->nodes = 0;
fmc->dirty_size += fm->size;
}
fm->next = 0;
if (!fmc->head) {
fm->prev = 0;
fmc->head = fm;
fmc->tail = fm;
}
else {
fm->prev = fmc->tail;
fmc->tail->next = fm;
fmc->tail = fm;
}
D3(jffs_print_fmcontrol(fmc));
D3(jffs_print_fm(fm));
*result = fm;
return 0;
}
/* This function returns the size of the first chunk of free space on the
flash memory. This function will return something nonzero if the flash
memory contains any free space. */
__u32 jffs_free_size1(struct jffs_fmcontrol *fmc)
{
__u32 head;
__u32 tail;
__u32 end = fmc->flash_start + fmc->flash_size;
if (!fmc->head) {
/* There is nothing on the flash. */
return fmc->flash_size;
}
/* Compute the beginning and ending of the contents of the flash. */
head = fmc->head->offset;
tail = fmc->tail->offset + fmc->tail->size;
if (tail == end) {
tail = fmc->flash_start;
}
ASSERT(else if (tail > end) {
/*printk(KERN_WARNING "jffs_free_size1(): tail > end\n");*/
tail = fmc->flash_start;
});
if (head <= tail) {
return end - tail;
}
else {
return head - tail;
}
}
/* This function will return something nonzero in case there are two free
areas on the flash. Like this:
+----------------+------------------+----------------+
| FREE 1 | USED / DIRTY | FREE 2 |
+----------------+------------------+----------------+
fmc->head -----^
fmc->tail ------------------------^
The value returned, will be the size of the first empty area on the
flash, in this case marked "FREE 1". */
__u32 jffs_free_size2(struct jffs_fmcontrol *fmc)
{
if (fmc->head) {
__u32 head = fmc->head->offset;
__u32 tail = fmc->tail->offset + fmc->tail->size;
if (tail == fmc->flash_start + fmc->flash_size) {
tail = fmc->flash_start;
}
if (tail >= head) {
return head - fmc->flash_start;
}
}
return 0;
}
/* The on-flash space is not needed anymore by the passed node. Remove
the reference to the node from the node list. If the data chunk in
the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
then mark that chunk as dirty. */
int jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm,
struct jffs_node *node)
{
struct jffs_node_ref *ref;
struct jffs_node_ref *prev;
ASSERT(int del = 0);
D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
node->ino, node->version));
ASSERT(if (!fmc || !fm || !fm->nodes) {
printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
"fm->nodes: 0x%p\n",
fmc, fm, (fm ? fm->nodes : 0));
return -1;
});
/* Find the reference to the node that is going to be removed
and remove it. */
for (ref = fm->nodes, prev = 0; ref; ref = ref->next) {
if (ref->node == node) {
if (prev) {
prev->next = ref->next;
}
else {
fm->nodes = ref->next;
}
free(ref);
DJM(no_jffs_node_ref--);
ASSERT(del = 1);
break;
}
prev = ref;
}
/* If the data chunk in the flash memory isn't used anymore
just mark it as obsolete. */
if (!fm->nodes) {
/* No node uses this chunk so let's remove it. */
fmc->used_size -= fm->size;
fmc->dirty_size += fm->size;
#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE /*???*/
if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
D1(printk("jffs_fmfree(): Failed to mark an on-flash "
"node obsolete!\n"));
return -1;
}
#endif
fmc->c->sb->s_dirt = 1;
}
ASSERT(if (!del) {
printk(KERN_WARNING "***jffs_fmfree(): "
"Didn't delete any node reference!\n");
});
return 0;
}
/* Call this function when the file system is unmounted. This function
frees all memory used by this module. */
void jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
{
if (fmc) {
struct jffs_fm *cur;
struct jffs_fm *next = fmc->head;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -