📄 jffs_fm.c
字号:
printk(KERN_WARNING "***jffs_fmfree(): " "Didn't delete any node reference!\n"); }); return 0;}/* 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 = jffs_alloc_fm())) { D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n", fmc, offset, size, node)); return 0; } 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 *) kmalloc(sizeof(struct jffs_node_ref), GFP_KERNEL))) { D(printk("jffs_fmalloced(): !fm->nodes\n")); jffs_free_fm(fm); return 0; } DJM(no_jffs_node_ref++); fm->nodes->node = node; fm->nodes->next = 0; fmc->used_size += size; fmc->free_size -= size; } else { /* If there is no node, then this is just a chunk of dirt. */ fmc->dirty_size += size; fmc->free_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;}/* Add a new node to an already existing jffs_fm struct. */intjffs_add_node(struct jffs_node *node){ struct jffs_node_ref *ref; D3(printk("jffs_add_node(): ino = %u\n", node->ino)); ref = (struct jffs_node_ref *)kmalloc(sizeof(struct jffs_node_ref), GFP_KERNEL); if (!ref) return -ENOMEM; DJM(no_jffs_node_ref++); ref->node = node; ref->next = node->fm->nodes; node->fm->nodes = ref; return 0;}/* Free a part of some allocated space. */voidjffs_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) { kfree(fm->nodes); DJM(no_jffs_node_ref--); fm->nodes = 0; } fmc->used_size -= fm->size; if (fm == fmc->tail) { fm->size -= size; fmc->free_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. */voidjffs_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; fmc->free_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; jffs_free_fm(del); } 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;}#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 intjffs_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; size_t len; 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. */ MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero); return 0;}#endif /* JFFS_MARK_OBSOLETE *//* check if it's possible to erase the wanted range, and if not, return * the range that IS erasable, or a negative error code. */longjffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size){ u_long ssize; /* assume that sector size for a partition is constant even * if it spans more than one chip (you usually put the same * type of chips in a system) */ ssize = mtd->erasesize; if (offset % ssize) { printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize); /* The offset is not sector size aligned. */ return -1; } else if (offset > mtd->size) { printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size); return -2; } else if (offset + size > mtd->size) { 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); return -3; } return (size / ssize) * ssize;}/* How much dirty flash memory is possible to erase at the moment? */longjffs_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 == 0) { /* 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 = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, 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); }); /* 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; } jffs_free_fm(del); } } return (ret >= 0 ? ret : 0);}struct jffs_fm *jffs_alloc_fm(void){ struct jffs_fm *fm; fm = kmem_cache_alloc(fm_cache,GFP_KERNEL); DJM(if (fm) no_jffs_fm++;); return fm;}void jffs_free_fm(struct jffs_fm *n){ kmem_cache_free(fm_cache,n); DJM(no_jffs_fm--);}struct jffs_node *jffs_alloc_node(void){ struct jffs_node *n; n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL); if(n != NULL) no_jffs_node++; return n;}void jffs_free_node(struct jffs_node *n){ kmem_cache_free(node_cache,n); no_jffs_node--;}int jffs_get_node_inuse(void){ return no_jffs_node;}voidjffs_print_fmcontrol(struct jffs_fmcontrol *fmc){ D(printk("struct jffs_fmcontrol: 0x%p\n", fmc)); D(printk("{\n")); D(printk(" %u, /* flash_size */\n", fmc->flash_size)); D(printk(" %u, /* used_size */\n", fmc->used_size)); D(printk(" %u, /* dirty_size */\n", fmc->dirty_size)); D(printk(" %u, /* free_size */\n", fmc->free_size)); D(printk(" %u, /* sector_size */\n", fmc->sector_size)); D(printk(" %u, /* min_free_size */\n", fmc->min_free_size)); D(printk(" %u, /* max_chunk_size */\n", fmc->max_chunk_size)); D(printk(" 0x%p, /* mtd */\n", fmc->mtd)); D(printk(" 0x%p, /* head */ " "(head->offset = 0x%08x)\n", fmc->head, (fmc->head ? fmc->head->offset : 0))); D(printk(" 0x%p, /* tail */ " "(tail->offset + tail->size = 0x%08x)\n", fmc->tail, (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0))); D(printk(" 0x%p, /* head_extra */\n", fmc->head_extra)); D(printk(" 0x%p, /* tail_extra */\n", fmc->tail_extra)); D(printk("}\n"));}voidjffs_print_fm(struct jffs_fm *fm){ D(printk("struct jffs_fm: 0x%p\n", fm)); D(printk("{\n")); D(printk(" 0x%08x, /* offset */\n", fm->offset)); D(printk(" %u, /* size */\n", fm->size)); D(printk(" 0x%p, /* prev */\n", fm->prev)); D(printk(" 0x%p, /* next */\n", fm->next)); D(printk(" 0x%p, /* nodes */\n", fm->nodes)); D(printk("}\n"));}voidjffs_print_node_ref(struct jffs_node_ref *ref){ D(printk("struct jffs_node_ref: 0x%p\n", ref)); D(printk("{\n")); D(printk(" 0x%p, /* node */\n", ref->node)); D(printk(" 0x%p, /* next */\n", ref->next)); D(printk("}\n"));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -