⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jffs_fm.cpp

📁 JFFS的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:

		while ((cur = next)) {
			next = next->next;
			free(cur);
			DJM(no_jffs_fm--);
		}
		free(fmc);
		DJM(no_jffs_fmcontrol--);
	}
}


/* This function creates a new shiny flash memory control structure.  */
struct jffs_fmcontrol *jffs_build_begin(struct jffs_control *c, kdev_t dev)
{
	struct jffs_fmcontrol *fmc;

	D3(printk("jffs_build_begin()\n"));
	fmc = (struct jffs_fmcontrol *)malloc(sizeof(struct jffs_fmcontrol));
	if (!fmc) {
		D(printk("jffs_build_begin(): Allocation of "
			 "struct jffs_fmcontrol failed!\n"));
		return (struct jffs_fmcontrol *)0;
	}
	DJM(no_jffs_fmcontrol++);

	/* Retrieve the size of the flash memory. !!!!! */
	
	fmc->flash_start = JFFS_ADDR_START;
	fmc->flash_size = JFFS_ADDR_END - JFFS_ADDR_START;

	D3(printk("  fmc->flash_start = 0x%08x\n", fmc->flash_start));
	D3(printk("  fmc->flash_size = %d bytes\n", fmc->flash_size));

	fmc->used_size = 0;
	fmc->dirty_size = 0;
	fmc->sector_size = 65536;
	fmc->max_chunk_size = fmc->sector_size >> 3; //改为8k
	//fmc->min_free_size = (fmc->sector_size << 1) - fmc->max_chunk_size;
	fmc->min_free_size = (fmc->sector_size << 0) + fmc->max_chunk_size;
	fmc->flash_part = flash_getpart(dev);
	fmc->no_call_gc = 0;
	fmc->c = c;
	fmc->head = 0;
	fmc->tail = 0;
	fmc->head_extra = 0;
	fmc->tail_extra = 0;
	return fmc;
}

/* When the flash memory scan has completed, this function should be called
   before use of the control structure.  */
void jffs_build_end(struct jffs_fmcontrol *fmc)
{
	D3(printk("jffs_build_end()\n"));

	if (!fmc->head) {
		fmc->head = fmc->head_extra;
		fmc->tail = fmc->tail_extra;
	}
	else if (fmc->head_extra) {
		fmc->tail_extra->next = fmc->head;
		fmc->head->prev = fmc->tail_extra;
		fmc->head = fmc->head_extra;
	}
	fmc->head_extra = 0; /* These two instructions should be omitted.  */
	fmc->tail_extra = 0;
	D3(jffs_print_fmcontrol(fmc));
}

#if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE

/* Mark an on-flash node as obsolete.

   Note that this is just an optimization that isn't necessary for the
   filesystem to work.  */

static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
{
	/* The `accurate_pos' holds the position of the accurate byte
	   in the jffs_raw_inode structure that we are going to mark
	   as obsolete.  */
	__u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
	unsigned char zero = 0x00;

	D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
	ASSERT(if (!fmc) {
		printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
		return -1;
	});

	/* Write 0x00 to the raw inode's accurate member.  Don't care
	   about the return value.  */
	flash_safe_write(fmc->flash_part, (unsigned char *) accurate_pos,
			 &zero, 1);
	return 0;
}

#endif /* JFFS_MARK_OBSOLETE  */


/* Add a new node to an already existing jffs_fm struct.  */
int
jffs_add_node(struct jffs_node *node)
{
	struct jffs_node_ref *ref;
	struct jffs_fm *fm = node->fm;
	int s = sizeof(struct jffs_node_ref);

	D3(printk("jffs_add_node(): ino = %u\n", node->ino));

	if (!(ref = (struct jffs_node_ref *)malloc(s))) {
		D(printk("jffs_add_node(): ref == 0\n"));
		return -ENOMEM;
	}
	DJM(no_jffs_node_ref++);
	ref->node = node;
	ref->next = fm->nodes;
	fm->nodes = ref;
	return 0;
}


/* Free a part of some allocated space.  */
void
jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
{
	D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
		  "fm->nodes->node->ino = %u, size = %u\n",
		  fm, (fm ? fm->nodes : 0),
		  (!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));

	if (fm->nodes) {
		free(fm->nodes);
		DJM(no_jffs_node_ref--);
		fm->nodes = 0;
	}
	fmc->used_size -= fm->size;
	if (fm == fmc->tail) {
		fm->size -= size;
	}
	fmc->dirty_size += fm->size;
}




/* Find the jffs_fm struct that contains the end of the data chunk that
   begins at the logical beginning of the flash memory and spans `size'
   bytes.  If we want to erase a sector of the flash memory, we use this
   function to find where the sector limit cuts a chunk of data.  */
struct jffs_fm *jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
{
	struct jffs_fm *fm;
	__u32 pos = 0;

	if (size == 0) {
		return 0;
	}

	ASSERT(if (!fmc) {
		printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
		return 0;
	});

	fm = fmc->head;

	while (fm) {
		pos += fm->size;
		if (pos < size) {
			fm = fm->next;
		}
		else if (pos > size) {
			break;
		}
		else {
			fm = 0;
			break;
		}
	}

	return fm;
}



/* Move the head of the fmc structures and delete the obsolete parts.  */
void jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
{
	struct jffs_fm *fm;
	struct jffs_fm *del;

	ASSERT(if (!fmc) {
		printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
		return;
	});

	fmc->dirty_size -= erased_size;

	for (fm = fmc->head; fm && (erased_size > 0);) {
		if (erased_size >= fm->size) {
			erased_size -= fm->size;
			del = fm;
			fm = fm->next;
			fm->prev = 0;
			fmc->head = fm;
			kfree(del);
			DJM(no_jffs_fm--);
		}
		else {
			fm->size -= erased_size;
			fm->offset += erased_size;
			break;
		}
	}
}



/* Return the oldest used node in the flash memory.  */
struct jffs_node *jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
{
	struct jffs_fm *fm;
	struct jffs_node_ref *nref;
	struct jffs_node *node = 0;

	ASSERT(if (!fmc) {
		printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
		return 0;
	});

	for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);

	if (!fm) {
		return 0;
	}

	/* The oldest node is the last one in the reference list.  This list
	   shouldn't be too long; just one or perhaps two elements.  */
	for (nref = fm->nodes; nref; nref = nref->next) {
		node = nref->node;
	}

	D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
		  (node ? node->ino : 0), (node ? node->version : 0)));

	return node;
}



/* How much dirty flash memory is possible to erase at the moment?  */
long jffs_erasable_size(struct jffs_fmcontrol *fmc)
{
	struct jffs_fm *fm;
	__u32 size = 0;
	long ret;

	ASSERT(if (!fmc) {
		printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
		return -1;
	});

	if (!fmc->head) {
		/* The flash memory is totally empty. No nodes. No dirt.
		   Just return.  */
		return 0;
	}

	/* Calculate how much space that is dirty.  */
	for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
		if (size && fm->offset == fmc->flash_start) {
			/* We have reached the beginning of the flash.  */
			break;
		}
		size += fm->size;
	}

	/* Someone's signature contained this:
	   There's a fine line between fishing and just standing on
	   the shore like an idiot...  */
	ret = flash_erasable_size(fmc->flash_part,
				  fmc->head->offset - fmc->flash_start, size);

	ASSERT(if (ret < 0) {
		printk("jffs_erasable_size: flash_erasable_size() "
		       "returned something less than zero (%ld).\n", ret);
		printk("jffs_erasable_size: offset = 0x%08x\n",
		       fmc->head->offset - fmc->flash_start);
	});

	/* If there is dirt on the flash (which is the reason to why
	   this function was called in the first place) but no space is
	   possible to erase right now, the initial part of the list of
	   jffs_fm structs, that hold place for dirty space, could perhaps
	   be shortened.  The list's initial "dirty" elements are merged
	   into just one large dirty jffs_fm struct.  This operation must
	   only be performed if nothing is possible to erase.  Otherwise,
	   jffs_clear_end_of_node() won't work as expected.  */
	if (ret == 0) {
		struct jffs_fm *head = fmc->head;
		struct jffs_fm *del;
		/* While there are two dirty nodes beside each other.*/
		while (head->nodes == 0
		       && head->next
		       && head->next->nodes == 0) {
			del = head->next;
			head->size += del->size;
			head->next = del->next;
			if (del->next) {
				del->next->prev = head;
			}
			kfree(del);
			DJM(no_jffs_fm--);
		}
	}

	return (ret >= 0 ? ret : 0);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -