int jffs2_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
uint32_t *len, int prio, uint32_t sumsize)
{ int ret = -EAGAIN; int blocksneeded = c->resv_blocks_write; /* align it */
minsize = PAD(minsize);
/* *Checkifthefreespaceisgreaterthensizeofthereservedpool. *Ifnot,onlyallowroottoproceedwithwriting.
*/ if (prio != ALLOC_DELETION && !jffs2_rp_can_write(c)) {
ret = -ENOSPC; goto out;
}
/* this needs a little more thought (true <tglx> :)) */ while(ret == -EAGAIN) { while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
uint32_t dirty, avail;
/* calculate real dirty size *dirty_sizecontainsblocksonerase_pending_list *thoseblocksarecountedinc->nr_erasing_blocks. *Ifoneblockisactuallyerased,itisnotlongercountedasdirty_space *butitiscountedinc->nr_erasing_blocks,soweadditandsubtractit *withc->nr_erasing_blocks*c->sector_sizeagain. *Blocksonerasable_listarecountedasdirty_size,butnotinc->nr_erasing_blocks *Thishelpsustoforcegcandpickeventuallyacleanblocktospreadtheload. *Weaddunchecked_sizehere,aswehopefullywillfindsomespacetouse. *Thiswillaffectthesumonlyonce,asgcfirstfinisheschecking *ofnodes.
*/
dirty = c->dirty_size + c->erasing_size - c->nr_erasing_blocks * c->sector_size + c->unchecked_size; if (dirty < c->nospc_dirty_size) { if (prio == ALLOC_DELETION && c->nr_free_blocks + c->nr_erasing_blocks >= c->resv_blocks_deletion) {
jffs2_dbg(1, "%s(): Low on dirty space to GC, but it's a deletion. Allowing...\n",
__func__); break;
}
jffs2_dbg(1, "dirty size 0x%08x + unchecked_size 0x%08x < nospc_dirty_size 0x%08x, returning -ENOSPC\n",
dirty, c->unchecked_size,
c->sector_size);
ret = jffs2_do_reserve_space(c, minsize, len, sumsize); if (ret) {
jffs2_dbg(1, "%s(): ret is %d\n", __func__, ret);
}
}
out:
spin_unlock(&c->erase_completion_lock); if (!ret)
ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1); if (ret)
mutex_unlock(&c->alloc_sem); return ret;
}
int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize,
uint32_t *len, uint32_t sumsize)
{ int ret;
minsize = PAD(minsize);
while (true) {
spin_lock(&c->erase_completion_lock);
ret = jffs2_do_reserve_space(c, minsize, len, sumsize); if (ret) {
jffs2_dbg(1, "%s(): looping, ret is %d\n",
__func__, ret);
}
spin_unlock(&c->erase_completion_lock);
if (ret == -EAGAIN)
cond_resched(); else break;
} if (!ret)
ret = jffs2_prealloc_raw_node_refs(c, c->nextblock, 1);
return ret;
}
/* Classify nextblock (clean, dirty of verydirty) and force to select an other one */
if (c->nextblock == NULL) {
jffs2_dbg(1, "%s(): Erase block at 0x%08x has already been placed in a list\n",
__func__, jeb->offset); return;
} /* Check, if we have a dirty block now, or if it was dirty already */ if (ISDIRTY (jeb->wasted_size + jeb->dirty_size)) {
c->dirty_size += jeb->wasted_size;
c->wasted_size -= jeb->wasted_size;
jeb->dirty_size += jeb->wasted_size;
jeb->wasted_size = 0; if (VERYDIRTY(c, jeb->dirty_size)) {
jffs2_dbg(1, "Adding full erase block at 0x%08x to very_dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
jeb->offset, jeb->free_size, jeb->dirty_size,
jeb->used_size);
list_add_tail(&jeb->list, &c->very_dirty_list);
} else {
jffs2_dbg(1, "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
jeb->offset, jeb->free_size, jeb->dirty_size,
jeb->used_size);
list_add_tail(&jeb->list, &c->dirty_list);
}
} else {
jffs2_dbg(1, "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
jeb->offset, jeb->free_size, jeb->dirty_size,
jeb->used_size);
list_add_tail(&jeb->list, &c->clean_list);
}
c->nextblock = NULL;
if (!c->nr_erasing_blocks &&
!list_empty(&c->erasable_pending_wbuf_list)) {
jffs2_dbg(1, "%s(): Flushing write buffer\n",
__func__); /* c->nextblock is NULL, no update to c->nextblock allowed */
spin_unlock(&c->erase_completion_lock);
jffs2_flush_wbuf_pad(c);
spin_lock(&c->erase_completion_lock); /* Have another go. It'll be on the erasable_list now */ return -EAGAIN;
}
if (!c->nr_erasing_blocks) { /* Ouch. We're in GC, or we wouldn't have got here.
And there's no space left. At all. */
pr_crit("Argh. No free space left for GC. nr_erasing_blocks is %d. nr_free_blocks is %d. (erasableempty: %s, erasingempty: %s, erasependingempty: %s)\n",
c->nr_erasing_blocks, c->nr_free_blocks,
str_yes_no(list_empty(&c->erasable_list)),
str_yes_no(list_empty(&c->erasing_list)),
str_yes_no(list_empty(&c->erase_pending_list))); return -ENOSPC;
}
spin_unlock(&c->erase_completion_lock); /* Don't wait for it; just erase one right now */
jffs2_erase_pending_blocks(c, 1);
spin_lock(&c->erase_completion_lock);
/* An erase may have failed, decreasing the amountoffreespaceavailable.Sowemust
restart from the beginning */ return -EAGAIN;
}
#ifdef CONFIG_JFFS2_FS_WRITEBUFFER /* adjust write buffer offset, else we get a non contiguous write bug */ if (!(c->wbuf_ofs % c->sector_size) && !c->wbuf_len)
c->wbuf_ofs = 0xffffffff; #endif
jffs2_dbg(1, "%s(): new nextblock = 0x%08x\n",
__func__, c->nextblock->offset);
return0;
}
/* Called with alloc sem _and_ erase_completion_lock */ staticint jffs2_do_reserve_space(struct jffs2_sb_info *c, uint32_t minsize,
uint32_t *len, uint32_t sumsize)
{ struct jffs2_eraseblock *jeb = c->nextblock;
uint32_t reserved_size; /* for summary information at the end of the jeb */ int ret;
restart:
reserved_size = 0;
if (jffs2_sum_active() && (sumsize != JFFS2_SUMMARY_NOSUM_SIZE)) { /* NOSUM_SIZE means not to generate summary */
/* Is there enough space for writing out the current node, or we have to
write out summary information now, close this jeb and select new nextblock? */ if (jeb && (PAD(minsize) + PAD(c->summary->sum_size + sumsize +
JFFS2_SUMMARY_FRAME_SIZE) > jeb->free_size)) {
/* Has summary been disabled for this jeb? */ if (jffs2_sum_is_disabled(c->summary)) {
sumsize = JFFS2_SUMMARY_NOSUM_SIZE; goto restart;
}
/* Writing out the collected summary information */
dbg_summary("generating summary for 0x%08x.\n", jeb->offset);
ret = jffs2_sum_write_sumnode(c);
if (ret) return ret;
if (jffs2_sum_is_disabled(c->summary)) { /* jffs2_write_sumnode() couldn't write out the summary information diablingsummaryforthisjebandfreethecollectedinformation
*/
sumsize = JFFS2_SUMMARY_NOSUM_SIZE; goto restart;
}
jffs2_close_nextblock(c, jeb);
jeb = NULL; /* keep always valid value in reserved_size */
reserved_size = PAD(sumsize + c->summary->sum_size + JFFS2_SUMMARY_FRAME_SIZE);
}
} else { if (jeb && minsize > jeb->free_size) {
uint32_t waste;
/* Skip the end of this block and file it as having some dirty space */ /* If there's a pending write to it, flush now */
if (jffs2_wbuf_dirty(c)) {
spin_unlock(&c->erase_completion_lock);
jffs2_dbg(1, "%s(): Flushing write buffer\n",
__func__);
jffs2_flush_wbuf_pad(c);
spin_lock(&c->erase_completion_lock);
jeb = c->nextblock; goto restart;
}
spin_unlock(&c->erase_completion_lock);
ret = jffs2_prealloc_raw_node_refs(c, jeb, 1);
/* Just lock it again and continue. Nothing much can change because weholdc->alloc_semanyway.Infact,it'snotentirelyclearwhy weholdc->erase_completion_lockinthemajorityofthisfunction...
but that's a question for another (more caffeine-rich) day. */
spin_lock(&c->erase_completion_lock);
if (ret) return ret;
waste = jeb->free_size;
jffs2_link_node_ref(c, jeb,
(jeb->offset + c->sector_size - waste) | REF_OBSOLETE,
waste, NULL); /* FIXME: that made it count as dirty. Convert to wasted */
jeb->dirty_size -= waste;
c->dirty_size -= waste;
jeb->wasted_size += waste;
c->wasted_size += waste;
jffs2_close_nextblock(c, jeb);
jeb = NULL;
}
}
if (!jeb) {
ret = jffs2_find_nextblock(c); if (ret) return ret;
jeb = c->nextblock;
if (jeb->free_size != c->sector_size - c->cleanmarker_size) {
pr_warn("Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n",
jeb->offset, jeb->free_size); goto restart;
}
} /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
enough space */
*len = jeb->free_size - reserved_size;
if (c->cleanmarker_size && jeb->used_size == c->cleanmarker_size &&
!jeb->first_node->next_in_ino) { /* Only node in it beforehand was a CLEANMARKER node (we think). Somarkitobsoletenowthatthere'sgoingtobeanothernode intheblock.Thiswillreduceused_sizetozerobutWe've alreadysetc->nextblocksothatjffs2_mark_node_obsolete() won'ttrytorefileittothedirty_list.
*/
spin_unlock(&c->erase_completion_lock);
jffs2_mark_node_obsolete(c, jeb->first_node);
spin_lock(&c->erase_completion_lock);
}
jffs2_dbg(1, "%s(): Node at 0x%x(%d), size 0x%x\n",
__func__, ofs & ~3, ofs & 3, len); #if1 /* Allow non-obsolete nodes only to be added at the end of c->nextblock, ifc->nextblockisset.Notethatwbuf.cwillfileobsoletenodes
even after refiling c->nextblock */ if ((c->nextblock || ((ofs & 3) != REF_OBSOLETE))
&& (jeb != c->nextblock || (ofs & ~3) != jeb->offset + (c->sector_size - jeb->free_size))) {
pr_warn("argh. node added in wrong place at 0x%08x(%d)\n",
ofs & ~3, ofs & 3); if (c->nextblock)
pr_warn("nextblock 0x%08x", c->nextblock->offset); else
pr_warn("No nextblock");
pr_cont(", expected at %08x\n",
jeb->offset + (c->sector_size - jeb->free_size)); return ERR_PTR(-EINVAL);
} #endif
spin_lock(&c->erase_completion_lock);
new = jffs2_link_node_ref(c, jeb, ofs, len, ic);
if (!jeb->free_size && !jeb->dirty_size && !ISDIRTY(jeb->wasted_size)) { /* If it lives on the dirty_list, jffs2_reserve_space will put it there */
jffs2_dbg(1, "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
jeb->offset, jeb->free_size, jeb->dirty_size,
jeb->used_size); if (jffs2_wbuf_dirty(c)) { /* Flush the last write in the block if it's outstanding */
spin_unlock(&c->erase_completion_lock);
jffs2_flush_wbuf_pad(c);
spin_lock(&c->erase_completion_lock);
}
if(unlikely(!ref)) {
pr_notice("EEEEEK. jffs2_mark_node_obsolete called with NULL node\n"); return;
} if (ref_obsolete(ref)) {
jffs2_dbg(1, "%s(): called with already obsolete node at 0x%08x\n",
__func__, ref_offset(ref)); return;
}
blocknr = ref->flash_offset / c->sector_size; if (blocknr >= c->nr_blocks) {
pr_notice("raw node at 0x%08x is off the end of device!\n",
ref->flash_offset);
BUG();
}
jeb = &c->blocks[blocknr];
if (jffs2_can_mark_obsolete(c) && !jffs2_is_readonly(c) &&
!(c->flags & (JFFS2_SB_FLAG_SCANNING | JFFS2_SB_FLAG_BUILDING))) { /* Hm. This may confuse static lock analysis. If any of the above threeconditionsisfalse,we'regoingtoreturnfromthis functionwithoutactuallyobliteratinganynodesorfreeing anyjffs2_raw_node_refs.Sowedon'tneedtostoperasesfrom happening,orprotectagainstpeopleholdinganobsolete
jffs2_raw_node_ref without the erase_completion_lock. */
mutex_lock(&c->erase_free_sem);
}
spin_lock(&c->erase_completion_lock);
freed_len = ref_totlen(c, jeb, ref);
if (ref_flags(ref) == REF_UNCHECKED) {
D1(if (unlikely(jeb->unchecked_size < freed_len)) {
pr_notice("raw unchecked node of size 0x%08x freed from erase block %d at 0x%08x, but unchecked_size was already 0x%08x\n",
freed_len, blocknr,
ref->flash_offset, jeb->used_size);
BUG();
})
jffs2_dbg(1, "Obsoleting previously unchecked node at 0x%08x of len %x\n",
ref_offset(ref), freed_len);
jeb->unchecked_size -= freed_len;
c->unchecked_size -= freed_len;
} else {
D1(if (unlikely(jeb->used_size < freed_len)) {
pr_notice("raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
freed_len, blocknr,
ref->flash_offset, jeb->used_size);
BUG();
})
jffs2_dbg(1, "Obsoleting node at 0x%08x of len %#x: ",
ref_offset(ref), freed_len);
jeb->used_size -= freed_len;
c->used_size -= freed_len;
}
// Take care, that wasted size is taken into concern if ((jeb->dirty_size || ISDIRTY(jeb->wasted_size + freed_len)) && jeb != c->nextblock) {
jffs2_dbg(1, "Dirtying\n");
addedsize = freed_len;
jeb->dirty_size += freed_len;
c->dirty_size += freed_len;
/* Convert wasted space to dirty, if not a bad block */ if (jeb->wasted_size) { if (on_list(&jeb->list, &c->bad_used_list)) {
jffs2_dbg(1, "Leaving block at %08x on the bad_used_list\n",
jeb->offset);
addedsize = 0; /* To fool the refiling code later */
} else {
jffs2_dbg(1, "Converting %d bytes of wasted space to dirty in block at %08x\n",
jeb->wasted_size, jeb->offset);
addedsize += jeb->wasted_size;
jeb->dirty_size += jeb->wasted_size;
c->dirty_size += jeb->wasted_size;
c->wasted_size -= jeb->wasted_size;
jeb->wasted_size = 0;
}
}
} else {
jffs2_dbg(1, "Wasting\n");
addedsize = 0;
jeb->wasted_size += freed_len;
c->wasted_size += freed_len;
}
ref->flash_offset = ref_offset(ref) | REF_OBSOLETE;
if (c->flags & JFFS2_SB_FLAG_SCANNING) { /* Flash scanning is in progress. Don't muck about with the block listsbecausethey'renotreadyyet,anddon'tactually obliteratenodesthatlookobsolete.Iftheyweren't markedobsoleteontheflashatthetimethey_became_
obsolete, there was probably a reason for that. */
spin_unlock(&c->erase_completion_lock); /* We didn't lock the erase_free_sem */ return;
}
if (jeb == c->nextblock) {
jffs2_dbg(2, "Not moving nextblock 0x%08x to dirty/erase_pending list\n",
jeb->offset);
} elseif (!jeb->used_size && !jeb->unchecked_size) { if (jeb == c->gcblock) {
jffs2_dbg(1, "gcblock at 0x%08x completely dirtied. Clearing gcblock...\n",
jeb->offset);
c->gcblock = NULL;
} else {
jffs2_dbg(1, "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n",
jeb->offset);
list_del(&jeb->list);
} if (jffs2_wbuf_dirty(c)) {
jffs2_dbg(1, "...and adding to erasable_pending_wbuf_list\n");
list_add_tail(&jeb->list, &c->erasable_pending_wbuf_list);
} else { if (jiffies & 127) { /* Most of the time, we just erase it immediately. Otherwise we
spend ages scanning it on mount, etc. */
jffs2_dbg(1, "...and adding to erase_pending_list\n");
list_add_tail(&jeb->list, &c->erase_pending_list);
c->nr_erasing_blocks++;
jffs2_garbage_collect_trigger(c);
} else { /* Sometimes, however, we leave it elsewhere so it doesn't get
immediately reused, and we spread the load a bit. */
jffs2_dbg(1, "...and adding to erasable_list\n");
list_add_tail(&jeb->list, &c->erasable_list);
}
}
jffs2_dbg(1, "Done OK\n");
} elseif (jeb == c->gcblock) {
jffs2_dbg(2, "Not moving gcblock 0x%08x to dirty_list\n",
jeb->offset);
} elseif (ISDIRTY(jeb->dirty_size) && !ISDIRTY(jeb->dirty_size - addedsize)) {
jffs2_dbg(1, "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n",
jeb->offset);
list_del(&jeb->list);
jffs2_dbg(1, "...and adding to dirty_list\n");
list_add_tail(&jeb->list, &c->dirty_list);
} elseif (VERYDIRTY(c, jeb->dirty_size) &&
!VERYDIRTY(c, jeb->dirty_size - addedsize)) {
jffs2_dbg(1, "Eraseblock at 0x%08x is now very dirty. Removing from dirty list...\n",
jeb->offset);
list_del(&jeb->list);
jffs2_dbg(1, "...and adding to very_dirty_list\n");
list_add_tail(&jeb->list, &c->very_dirty_list);
} else {
jffs2_dbg(1, "Eraseblock at 0x%08x not moved anywhere. (free 0x%08x, dirty 0x%08x, used 0x%08x)\n",
jeb->offset, jeb->free_size, jeb->dirty_size,
jeb->used_size);
}
spin_unlock(&c->erase_completion_lock);
if (!jffs2_can_mark_obsolete(c) || jffs2_is_readonly(c) ||
(c->flags & JFFS2_SB_FLAG_BUILDING)) { /* We didn't lock the erase_free_sem */ return;
}
/* The erase_free_sem is locked, and has been since before we marked the node obsolete andpotentiallyputitseraseblockontotheerase_pending_list.Thus,weknowthat theblockhasn't_already_beenerased,andthat'ref'itselfhasn'tbeenfreedyet
by jffs2_free_jeb_node_refs() in erase.c. Which is nice. */
jffs2_dbg(1, "obliterating obsoleted node at 0x%08x\n",
ref_offset(ref));
ret = jffs2_flash_read(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n); if (ret) {
pr_warn("Read error reading from obsoleted node at 0x%08x: %d\n",
ref_offset(ref), ret); goto out_erase_sem;
} if (retlen != sizeof(n)) {
pr_warn("Short read from obsoleted node at 0x%08x: %zd\n",
ref_offset(ref), retlen); goto out_erase_sem;
} if (PAD(je32_to_cpu(n.totlen)) != PAD(freed_len)) {
pr_warn("Node totlen on flash (0x%08x) != totlen from node ref (0x%08x)\n",
je32_to_cpu(n.totlen), freed_len); goto out_erase_sem;
} if (!(je16_to_cpu(n.nodetype) & JFFS2_NODE_ACCURATE)) {
jffs2_dbg(1, "Node at 0x%08x was already marked obsolete (nodetype 0x%04x)\n",
ref_offset(ref), je16_to_cpu(n.nodetype)); goto out_erase_sem;
} /* XXX FIXME: This is ugly now */
n.nodetype = cpu_to_je16(je16_to_cpu(n.nodetype) & ~JFFS2_NODE_ACCURATE);
ret = jffs2_flash_write(c, ref_offset(ref), sizeof(n), &retlen, (char *)&n); if (ret) {
pr_warn("Write error in obliterating obsoleted node at 0x%08x: %d\n",
ref_offset(ref), ret); goto out_erase_sem;
} if (retlen != sizeof(n)) {
pr_warn("Short write in obliterating obsoleted node at 0x%08x: %zd\n",
ref_offset(ref), retlen); goto out_erase_sem;
}
/* Nodes which have been marked obsolete no longer need to be associatedwithanyinode.Removethemfromtheper-inodelist.
Notewecan'tdothisforNANDatthemomentbecauseweneed obsoletedirentnodestostayonthelists,becauseofthe horridnessinjffs2_garbage_collect_deletion_dirent().Also becausewedeletetheinocache,andonNANDweneedthatto stayarounduntilallthenodesareactuallyerased,inorder tostopusfromgivingthesameinodenumbertoanothernewly
created inode. */ if (ref->next_in_ino) { struct jffs2_inode_cache *ic; struct jffs2_raw_node_ref **p;
spin_lock(&c->erase_completion_lock);
ic = jffs2_raw_ref_to_ic(ref); for (p = &ic->nodes; (*p) != ref; p = &((*p)->next_in_ino))
;
if (c->nr_free_blocks + c->nr_erasing_blocks < c->resv_blocks_gctrigger &&
(dirty > c->nospc_dirty_size))
ret = 1;
list_for_each_entry(jeb, &c->very_dirty_list, list) {
nr_very_dirty++; if (nr_very_dirty == c->vdirty_blocks_gctrigger) {
ret = 1; /* In debug mode, actually go through and count them all */
D1(continue); break;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.