[JFFS2] Extend jffs2_link_node_ref() to link into per-inode list too.

Let's avoid the potential for forgetting to set ref->next_in_ino, by doing
it within jffs2_link_node_ref() instead.

This highlights the ugliness of what we're currently doing with
xattr_datum and xattr_ref structures -- we should find a nicer way of
dealing with that.

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
diff --git a/fs/jffs2/erase.c b/fs/jffs2/erase.c
index f677d69..0fc19a2 100644
--- a/fs/jffs2/erase.c
+++ b/fs/jffs2/erase.c
@@ -410,10 +410,9 @@
 		/* Everything else got zeroed before the erase */
 		jeb->free_size = c->sector_size;
 
-		marker_ref->next_in_ino = NULL;
 		marker_ref->flash_offset = jeb->offset | REF_NORMAL;
 
-		jffs2_link_node_ref(c, jeb, marker_ref, c->cleanmarker_size);
+		jffs2_link_node_ref(c, jeb, marker_ref, c->cleanmarker_size, NULL);
 	}
 
 	spin_lock(&c->erase_completion_lock);
diff --git a/fs/jffs2/gc.c b/fs/jffs2/gc.c
index 4773ba2..153755b 100644
--- a/fs/jffs2/gc.c
+++ b/fs/jffs2/gc.c
@@ -634,11 +634,8 @@
 		printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %zd\n",
                        rawlen, phys_ofs, ret, retlen);
 		if (retlen) {
-                        /* Doesn't belong to any inode */
-			nraw->next_in_ino = NULL;
-
 			nraw->flash_offset |= REF_OBSOLETE;
-			jffs2_add_physical_node_ref(c, nraw, rawlen);
+			jffs2_add_physical_node_ref(c, nraw, rawlen, NULL);
 			jffs2_mark_node_obsolete(c, nraw);
 		} else {
 			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", nraw->flash_offset);
@@ -678,18 +675,8 @@
 		goto out_node;
 	}
 	nraw->flash_offset |= REF_PRISTINE;
-	jffs2_add_physical_node_ref(c, nraw, rawlen);
+	jffs2_add_physical_node_ref(c, nraw, rawlen, ic);
 
-	if (ic) {
-		/* Link into per-inode list. This is safe because of the ic
-		   state being INO_STATE_GC. Note that if we're doing this
-		   for an inode which is in-core, the 'nraw' pointer is then
-		   going to be fetched from ic->nodes by our caller. */
-		spin_lock(&c->erase_completion_lock);
-		nraw->next_in_ino = ic->nodes;
-		ic->nodes = nraw;
-		spin_unlock(&c->erase_completion_lock);
-	}
 	jffs2_mark_node_obsolete(c, raw);
 	D1(printk(KERN_DEBUG "WHEEE! GC REF_PRISTINE node at 0x%08x succeeded\n", ref_offset(raw)));
 
diff --git a/fs/jffs2/nodelist.c b/fs/jffs2/nodelist.c
index 7d563f9..d25d491 100644
--- a/fs/jffs2/nodelist.c
+++ b/fs/jffs2/nodelist.c
@@ -1048,7 +1048,8 @@
 }
 
 void jffs2_link_node_ref(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
-			 struct jffs2_raw_node_ref *ref, uint32_t len)
+			 struct jffs2_raw_node_ref *ref, uint32_t len,
+			 struct jffs2_inode_cache *ic)
 {
 	if (!jeb->first_node)
 		jeb->first_node = ref;
@@ -1065,6 +1066,13 @@
 	}
 	jeb->last_node = ref;
 
+	if (ic) {
+		ref->next_in_ino = ic->nodes;
+		ic->nodes = ref;
+	} else {
+		ref->next_in_ino = NULL;
+	}
+
 	switch(ref_flags(ref)) {
 	case REF_UNCHECKED:
 		c->unchecked_size += len;
@@ -1120,12 +1128,11 @@
 
 		ref->flash_offset = jeb->offset + c->sector_size - jeb->free_size;
 		ref->flash_offset |= REF_OBSOLETE;
-		ref->next_in_ino = 0;
 #ifdef TEST_TOTLEN
 		ref->__totlen = size;
 #endif
 
-		jffs2_link_node_ref(c, jeb, ref, size);
+		jffs2_link_node_ref(c, jeb, ref, size, NULL);
 	}
 
 	return 0;
diff --git a/fs/jffs2/nodelist.h b/fs/jffs2/nodelist.h
index 80d1fda..ee5aedc 100644
--- a/fs/jffs2/nodelist.h
+++ b/fs/jffs2/nodelist.h
@@ -307,7 +307,8 @@
 void jffs2_truncate_fragtree (struct jffs2_sb_info *c, struct rb_root *list, uint32_t size);
 int jffs2_add_older_frag_to_fragtree(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_tmp_dnode_info *tn);
 void jffs2_link_node_ref(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
-			 struct jffs2_raw_node_ref *ref, uint32_t len);
+			 struct jffs2_raw_node_ref *ref, uint32_t len,
+			 struct jffs2_inode_cache *ic);
 extern uint32_t __jffs2_ref_totlen(struct jffs2_sb_info *c,
 				   struct jffs2_eraseblock *jeb,
 				   struct jffs2_raw_node_ref *ref);
@@ -318,7 +319,10 @@
 			uint32_t *len, int prio, uint32_t sumsize);
 int jffs2_reserve_space_gc(struct jffs2_sb_info *c, uint32_t minsize, uint32_t *ofs,
 			uint32_t *len, uint32_t sumsize);
-int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, uint32_t len);
+int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, 
+				struct jffs2_raw_node_ref *new,
+				uint32_t len,
+				struct jffs2_inode_cache *ic);
 void jffs2_complete_reservation(struct jffs2_sb_info *c);
 void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw);
 
diff --git a/fs/jffs2/nodemgmt.c b/fs/jffs2/nodemgmt.c
index 9a0f312..e10e58e 100644
--- a/fs/jffs2/nodemgmt.c
+++ b/fs/jffs2/nodemgmt.c
@@ -381,7 +381,8 @@
  *	Must be called with the alloc_sem held.
  */
 
-int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, uint32_t len)
+int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new,
+				uint32_t len, struct jffs2_inode_cache *ic)
 {
 	struct jffs2_eraseblock *jeb;
 
@@ -403,7 +404,7 @@
 #endif
 	spin_lock(&c->erase_completion_lock);
 
-	jffs2_link_node_ref(c, jeb, new, len);
+	jffs2_link_node_ref(c, jeb, new, 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 */
diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
index cffafec..6fce703 100644
--- a/fs/jffs2/scan.c
+++ b/fs/jffs2/scan.c
@@ -361,9 +361,9 @@
 	xd->node = raw;
 
 	raw->flash_offset = ofs | REF_PRISTINE;
-	raw->next_in_ino = (void *)xd;
 
-	jffs2_link_node_ref(c, jeb, raw, totlen);
+	jffs2_link_node_ref(c, jeb, raw, totlen, NULL);
+	/* FIXME */ raw->next_in_ino = (void *)xd;
 
 	if (jffs2_sum_active())
 		jffs2_sum_add_xattr_mem(s, rx, ofs - jeb->offset);
@@ -425,9 +425,9 @@
 	c->xref_temp = ref;
 
 	raw->flash_offset = ofs | REF_PRISTINE;
-	raw->next_in_ino = (void *)ref;
 
-	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(rr->totlen)));
+	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(rr->totlen)), NULL);
+	/* FIXME */ raw->next_in_ino = (void *)ref;
 
 	if (jffs2_sum_active())
 		jffs2_sum_add_xref_mem(s, rr, ofs - jeb->offset);
@@ -844,10 +844,9 @@
 					printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
 					return -ENOMEM;
 				}
-				marker_ref->next_in_ino = NULL;
 				marker_ref->flash_offset = ofs | REF_NORMAL;
 
-				jffs2_link_node_ref(c, jeb, marker_ref, c->cleanmarker_size);
+				jffs2_link_node_ref(c, jeb, marker_ref, c->cleanmarker_size, NULL);
 
 				ofs += PAD(c->cleanmarker_size);
 			}
@@ -892,8 +891,7 @@
 				if (!ref)
 					return -ENOMEM;
 				ref->flash_offset = ofs | REF_PRISTINE;
-				ref->next_in_ino = 0;
-				jffs2_link_node_ref(c, jeb, ref, PAD(je32_to_cpu(node->totlen)));
+				jffs2_link_node_ref(c, jeb, ref, PAD(je32_to_cpu(node->totlen)), NULL);
 
 				/* We can't summarise nodes we don't grok */
 				jffs2_sum_disable_collecting(s);
@@ -1004,10 +1002,7 @@
 
 	raw->flash_offset = ofs | REF_UNCHECKED;
 
-	raw->next_in_ino = ic->nodes;
-	ic->nodes = raw;
-	
-	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(ri->totlen)));
+	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(ri->totlen)), ic);
 
 	D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n",
 		  je32_to_cpu(ri->ino), je32_to_cpu(ri->version),
@@ -1082,10 +1077,7 @@
 	}
 
 	raw->flash_offset = ofs | REF_PRISTINE;
-	raw->next_in_ino = ic->nodes;
-	ic->nodes = raw;
-
-	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(rd->totlen)));
+	jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(rd->totlen)), ic);
 
 	fd->raw = raw;
 	fd->next = NULL;
diff --git a/fs/jffs2/summary.c b/fs/jffs2/summary.c
index 1451732..351ba9f 100644
--- a/fs/jffs2/summary.c
+++ b/fs/jffs2/summary.c
@@ -430,10 +430,7 @@
 
 				raw->flash_offset |= REF_UNCHECKED;
 
-				raw->next_in_ino = ic->nodes;
-				ic->nodes = raw;
-
-				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spi->totlen)));
+				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spi->totlen)), ic);
 
 				*pseudo_random += je32_to_cpu(spi->version);
 
@@ -473,10 +470,7 @@
 				}
 
 				raw->flash_offset |= REF_PRISTINE;
-				raw->next_in_ino = ic->nodes;
-				ic->nodes = raw;
-
-				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spd->totlen)));
+				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spd->totlen)), ic);
 
 				fd->raw = raw;
 				fd->next = NULL;
@@ -525,9 +519,9 @@
 				xd->node = raw;
 
 				raw->flash_offset |= REF_UNCHECKED;
-				raw->next_in_ino = (void *)xd;
 
-				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spx->totlen)));
+				jffs2_link_node_ref(c, jeb, raw, PAD(je32_to_cpu(spx->totlen)), NULL);
+				/* FIXME */ raw->next_in_ino = (void *)xd;
 
 				*pseudo_random += je32_to_cpu(spx->xid);
 				sp += JFFS2_SUMMARY_XATTR_SIZE;
@@ -561,9 +555,9 @@
 				c->xref_temp = ref;
 
 				raw->flash_offset |= REF_UNCHECKED;
-				raw->next_in_ino = (void *)ref;
 
-				jffs2_link_node_ref(c, jeb, raw, PAD(sizeof(struct jffs2_raw_xref)));
+				jffs2_link_node_ref(c, jeb, raw, PAD(sizeof(struct jffs2_raw_xref)), NULL);
+				/* FIXME */ raw->next_in_ino = (void *)ref;
 
 				*pseudo_random += raw->flash_offset;
 				sp += JFFS2_SUMMARY_XREF_SIZE;
@@ -664,9 +658,8 @@
 			}
 
 			marker_ref->flash_offset = jeb->offset | REF_NORMAL;
-			marker_ref->next_in_ino = NULL;
 
-			jffs2_link_node_ref(c, jeb, marker_ref, je32_to_cpu(summary->cln_mkr));
+			jffs2_link_node_ref(c, jeb, marker_ref, je32_to_cpu(summary->cln_mkr), NULL);
 		}
 	}
 
@@ -686,10 +679,9 @@
 		return -ENOMEM;
 	}
 
-	cache_ref->next_in_ino = NULL;
 	cache_ref->flash_offset |= REF_NORMAL;
 
-	jffs2_link_node_ref(c, jeb, cache_ref, sumsize);
+	jffs2_link_node_ref(c, jeb, cache_ref, sumsize, NULL);
 
 	if (unlikely(jeb->free_size)) {
 		JFFS2_WARNING("Free size 0x%x bytes in eraseblock @0x%08x with summary?\n",
@@ -849,9 +841,8 @@
 
 			ref->flash_offset = jeb->offset + c->sector_size - jeb->free_size;
 			ref->flash_offset |= REF_OBSOLETE;
-			ref->next_in_ino = 0;
 
-			jffs2_link_node_ref(c, jeb, ref, c->sector_size - jeb->free_size);
+			jffs2_link_node_ref(c, jeb, ref, c->sector_size - jeb->free_size, NULL);
 		}
 
 		c->summary->sum_size = JFFS2_SUMMARY_NOSUM_SIZE;
@@ -909,11 +900,10 @@
 		return -ENOMEM;
 	}
 
-	summary_ref->next_in_ino = NULL;
 	summary_ref->flash_offset = (jeb->offset + c->sector_size - jeb->free_size) | REF_NORMAL;
 
 	spin_lock(&c->erase_completion_lock);
-	jffs2_link_node_ref(c, jeb, summary_ref, infosize);
+	jffs2_link_node_ref(c, jeb, summary_ref, infosize, NULL);
 
 	return 0;
 }
diff --git a/fs/jffs2/wbuf.c b/fs/jffs2/wbuf.c
index 45e3573..62f685f 100644
--- a/fs/jffs2/wbuf.c
+++ b/fs/jffs2/wbuf.c
@@ -312,9 +312,8 @@
 					return;
 
 				raw2->flash_offset = ofs | REF_OBSOLETE;
-				raw2->next_in_ino = NULL;
 
-				jffs2_add_physical_node_ref(c, raw2, ref_totlen(c, jeb, *first_raw));
+				jffs2_add_physical_node_ref(c, raw2, ref_totlen(c, jeb, *first_raw), NULL);
 			}
 			return;
 		}
@@ -507,11 +506,10 @@
 			return -ENOMEM;
 		ref->flash_offset = c->wbuf_ofs + c->wbuf_len;
 		ref->flash_offset |= REF_OBSOLETE;
-		ref->next_in_ino = NULL;
 
 		spin_lock(&c->erase_completion_lock);
 
-		jffs2_link_node_ref(c, jeb, ref, waste);
+		jffs2_link_node_ref(c, jeb, ref, waste, NULL);
 		/* FIXME: that made it count as dirty. Convert to wasted */
 		jeb->dirty_size -= waste;
 		c->dirty_size -= waste;
diff --git a/fs/jffs2/write.c b/fs/jffs2/write.c
index 4462541..319a70f 100644
--- a/fs/jffs2/write.c
+++ b/fs/jffs2/write.c
@@ -122,16 +122,13 @@
 
 		/* Mark the space as dirtied */
 		if (retlen) {
-			/* Doesn't belong to any inode */
-			raw->next_in_ino = NULL;
-
 			/* Don't change raw->size to match retlen. We may have
 			   written the node header already, and only the data will
 			   seem corrupted, in which case the scan would skip over
 			   any node we write before the original intended end of
 			   this node */
 			raw->flash_offset |= REF_OBSOLETE;
-			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen));
+			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen), NULL);
 			jffs2_mark_node_obsolete(c, raw);
 		} else {
 			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
@@ -189,13 +186,7 @@
 	} else {
 		raw->flash_offset |= REF_NORMAL;
 	}
-	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen));
-
-	/* Link into per-inode list */
-	spin_lock(&c->erase_completion_lock);
-	raw->next_in_ino = f->inocache->nodes;
-	f->inocache->nodes = raw;
-	spin_unlock(&c->erase_completion_lock);
+	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*ri)+datalen), f->inocache);
 
 	D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x(%d) with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n",
 		  flash_ofs, ref_flags(raw), je32_to_cpu(ri->dsize),
@@ -275,9 +266,8 @@
 			       sizeof(*rd)+namelen, flash_ofs, ret, retlen);
 		/* Mark the space as dirtied */
 		if (retlen) {
-			raw->next_in_ino = NULL;
 			raw->flash_offset |= REF_OBSOLETE;
-			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen));
+			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen), NULL);
 			jffs2_mark_node_obsolete(c, raw);
 		} else {
 			printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
@@ -323,12 +313,7 @@
 	}
 	/* Mark the space used */
 	raw->flash_offset |= REF_PRISTINE;
-	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen));
-
-	spin_lock(&c->erase_completion_lock);
-	raw->next_in_ino = f->inocache->nodes;
-	f->inocache->nodes = raw;
-	spin_unlock(&c->erase_completion_lock);
+	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(*rd)+namelen), f->inocache);
 
 	if (retried) {
 		jffs2_dbg_acct_sanity_check(c,NULL);
diff --git a/fs/jffs2/xattr.c b/fs/jffs2/xattr.c
index e16f846..76d1661 100644
--- a/fs/jffs2/xattr.c
+++ b/fs/jffs2/xattr.c
@@ -322,7 +322,6 @@
 	if (!raw)
 		return -ENOMEM;
 	raw->flash_offset = phys_ofs;
-	raw->next_in_ino = (void *)xd;
 
 	/* Setup raw-xattr */
 	rx.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
@@ -345,8 +344,7 @@
 		rc = rc ? rc : -EIO;
 		if (length) {
 			raw->flash_offset |= REF_OBSOLETE;
-			raw->next_in_ino = NULL;
-			jffs2_add_physical_node_ref(c, raw, PAD(totlen));
+			jffs2_add_physical_node_ref(c, raw, PAD(totlen), NULL);
 			jffs2_mark_node_obsolete(c, raw);
 		} else {
 			jffs2_free_raw_node_ref(raw);
@@ -356,7 +354,9 @@
 
 	/* success */
 	raw->flash_offset |= REF_PRISTINE;
-	jffs2_add_physical_node_ref(c, raw, PAD(totlen));
+	jffs2_add_physical_node_ref(c, raw, PAD(totlen), NULL);
+	/* FIXME */ raw->next_in_ino = (void *)xd;
+
 	if (xd->node)
 		delete_xattr_datum_node(c, xd);
 	xd->node = raw;
@@ -566,7 +566,6 @@
 	if (!raw)
 		return -ENOMEM;
 	raw->flash_offset = phys_ofs;
-	raw->next_in_ino = (void *)ref;
 
 	rr.magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
 	rr.nodetype = cpu_to_je16(JFFS2_NODETYPE_XREF);
@@ -584,8 +583,7 @@
 		ret = ret ? ret : -EIO;
 		if (length) {
 			raw->flash_offset |= REF_OBSOLETE;
-			raw->next_in_ino = NULL;
-			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)));
+			jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)), NULL);
 			jffs2_mark_node_obsolete(c, raw);
 		} else {
 			jffs2_free_raw_node_ref(raw);
@@ -594,7 +592,8 @@
 	}
 	raw->flash_offset |= REF_PRISTINE;
 
-	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)));
+	jffs2_add_physical_node_ref(c, raw, PAD(sizeof(rr)), NULL);
+	/* FIXME */ raw->next_in_ino = (void *)ref;
 	if (ref->node)
 		delete_xattr_ref_node(c, ref);
 	ref->node = raw;