xref: /OK3568_Linux_fs/kernel/fs/jffs2/readinode.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * JFFS2 -- Journalling Flash File System, Version 2.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright © 2001-2007 Red Hat, Inc.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Created by David Woodhouse <dwmw2@infradead.org>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * For licensing information, see the file 'LICENCE' in this directory.
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/sched.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/fs.h>
18*4882a593Smuzhiyun #include <linux/crc32.h>
19*4882a593Smuzhiyun #include <linux/pagemap.h>
20*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
21*4882a593Smuzhiyun #include <linux/compiler.h>
22*4882a593Smuzhiyun #include "nodelist.h"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Check the data CRC of the node.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * Returns: 0 if the data CRC is correct;
28*4882a593Smuzhiyun  * 	    1 - if incorrect;
29*4882a593Smuzhiyun  *	    error code if an error occurred.
30*4882a593Smuzhiyun  */
check_node_data(struct jffs2_sb_info * c,struct jffs2_tmp_dnode_info * tn)31*4882a593Smuzhiyun static int check_node_data(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	struct jffs2_raw_node_ref *ref = tn->fn->raw;
34*4882a593Smuzhiyun 	int err = 0, pointed = 0;
35*4882a593Smuzhiyun 	struct jffs2_eraseblock *jeb;
36*4882a593Smuzhiyun 	unsigned char *buffer;
37*4882a593Smuzhiyun 	uint32_t crc, ofs, len;
38*4882a593Smuzhiyun 	size_t retlen;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	BUG_ON(tn->csize == 0);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	/* Calculate how many bytes were already checked */
43*4882a593Smuzhiyun 	ofs = ref_offset(ref) + sizeof(struct jffs2_raw_inode);
44*4882a593Smuzhiyun 	len = tn->csize;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	if (jffs2_is_writebuffered(c)) {
47*4882a593Smuzhiyun 		int adj = ofs % c->wbuf_pagesize;
48*4882a593Smuzhiyun 		if (likely(adj))
49*4882a593Smuzhiyun 			adj = c->wbuf_pagesize - adj;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 		if (adj >= tn->csize) {
52*4882a593Smuzhiyun 			dbg_readinode("no need to check node at %#08x, data length %u, data starts at %#08x - it has already been checked.\n",
53*4882a593Smuzhiyun 				      ref_offset(ref), tn->csize, ofs);
54*4882a593Smuzhiyun 			goto adj_acc;
55*4882a593Smuzhiyun 		}
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 		ofs += adj;
58*4882a593Smuzhiyun 		len -= adj;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	dbg_readinode("check node at %#08x, data length %u, partial CRC %#08x, correct CRC %#08x, data starts at %#08x, start checking from %#08x - %u bytes.\n",
62*4882a593Smuzhiyun 		ref_offset(ref), tn->csize, tn->partial_crc, tn->data_crc, ofs - len, ofs, len);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun #ifndef __ECOS
65*4882a593Smuzhiyun 	/* TODO: instead, incapsulate point() stuff to jffs2_flash_read(),
66*4882a593Smuzhiyun 	 * adding and jffs2_flash_read_end() interface. */
67*4882a593Smuzhiyun 	err = mtd_point(c->mtd, ofs, len, &retlen, (void **)&buffer, NULL);
68*4882a593Smuzhiyun 	if (!err && retlen < len) {
69*4882a593Smuzhiyun 		JFFS2_WARNING("MTD point returned len too short: %zu instead of %u.\n", retlen, tn->csize);
70*4882a593Smuzhiyun 		mtd_unpoint(c->mtd, ofs, retlen);
71*4882a593Smuzhiyun 	} else if (err) {
72*4882a593Smuzhiyun 		if (err != -EOPNOTSUPP)
73*4882a593Smuzhiyun 			JFFS2_WARNING("MTD point failed: error code %d.\n", err);
74*4882a593Smuzhiyun 	} else
75*4882a593Smuzhiyun 		pointed = 1; /* succefully pointed to device */
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	if (!pointed) {
79*4882a593Smuzhiyun 		buffer = kmalloc(len, GFP_KERNEL);
80*4882a593Smuzhiyun 		if (unlikely(!buffer))
81*4882a593Smuzhiyun 			return -ENOMEM;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		/* TODO: this is very frequent pattern, make it a separate
84*4882a593Smuzhiyun 		 * routine */
85*4882a593Smuzhiyun 		err = jffs2_flash_read(c, ofs, len, &retlen, buffer);
86*4882a593Smuzhiyun 		if (err) {
87*4882a593Smuzhiyun 			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ofs, err);
88*4882a593Smuzhiyun 			goto free_out;
89*4882a593Smuzhiyun 		}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 		if (retlen != len) {
92*4882a593Smuzhiyun 			JFFS2_ERROR("short read at %#08x: %zd instead of %d.\n", ofs, retlen, len);
93*4882a593Smuzhiyun 			err = -EIO;
94*4882a593Smuzhiyun 			goto free_out;
95*4882a593Smuzhiyun 		}
96*4882a593Smuzhiyun 	}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	/* Continue calculating CRC */
99*4882a593Smuzhiyun 	crc = crc32(tn->partial_crc, buffer, len);
100*4882a593Smuzhiyun 	if(!pointed)
101*4882a593Smuzhiyun 		kfree(buffer);
102*4882a593Smuzhiyun #ifndef __ECOS
103*4882a593Smuzhiyun 	else
104*4882a593Smuzhiyun 		mtd_unpoint(c->mtd, ofs, len);
105*4882a593Smuzhiyun #endif
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	if (crc != tn->data_crc) {
108*4882a593Smuzhiyun 		JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
109*4882a593Smuzhiyun 			     ref_offset(ref), tn->data_crc, crc);
110*4882a593Smuzhiyun 		return 1;
111*4882a593Smuzhiyun 	}
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun adj_acc:
114*4882a593Smuzhiyun 	jeb = &c->blocks[ref->flash_offset / c->sector_size];
115*4882a593Smuzhiyun 	len = ref_totlen(c, jeb, ref);
116*4882a593Smuzhiyun 	/* If it should be REF_NORMAL, it'll get marked as such when
117*4882a593Smuzhiyun 	   we build the fragtree, shortly. No need to worry about GC
118*4882a593Smuzhiyun 	   moving it while it's marked REF_PRISTINE -- GC won't happen
119*4882a593Smuzhiyun 	   till we've finished checking every inode anyway. */
120*4882a593Smuzhiyun 	ref->flash_offset |= REF_PRISTINE;
121*4882a593Smuzhiyun 	/*
122*4882a593Smuzhiyun 	 * Mark the node as having been checked and fix the
123*4882a593Smuzhiyun 	 * accounting accordingly.
124*4882a593Smuzhiyun 	 */
125*4882a593Smuzhiyun 	spin_lock(&c->erase_completion_lock);
126*4882a593Smuzhiyun 	jeb->used_size += len;
127*4882a593Smuzhiyun 	jeb->unchecked_size -= len;
128*4882a593Smuzhiyun 	c->used_size += len;
129*4882a593Smuzhiyun 	c->unchecked_size -= len;
130*4882a593Smuzhiyun 	jffs2_dbg_acct_paranoia_check_nolock(c, jeb);
131*4882a593Smuzhiyun 	spin_unlock(&c->erase_completion_lock);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	return 0;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun free_out:
136*4882a593Smuzhiyun 	if(!pointed)
137*4882a593Smuzhiyun 		kfree(buffer);
138*4882a593Smuzhiyun #ifndef __ECOS
139*4882a593Smuzhiyun 	else
140*4882a593Smuzhiyun 		mtd_unpoint(c->mtd, ofs, len);
141*4882a593Smuzhiyun #endif
142*4882a593Smuzhiyun 	return err;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun  * Helper function for jffs2_add_older_frag_to_fragtree().
147*4882a593Smuzhiyun  *
148*4882a593Smuzhiyun  * Checks the node if we are in the checking stage.
149*4882a593Smuzhiyun  */
check_tn_node(struct jffs2_sb_info * c,struct jffs2_tmp_dnode_info * tn)150*4882a593Smuzhiyun static int check_tn_node(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	int ret;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	BUG_ON(ref_obsolete(tn->fn->raw));
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	/* We only check the data CRC of unchecked nodes */
157*4882a593Smuzhiyun 	if (ref_flags(tn->fn->raw) != REF_UNCHECKED)
158*4882a593Smuzhiyun 		return 0;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	dbg_readinode("check node %#04x-%#04x, phys offs %#08x\n",
161*4882a593Smuzhiyun 		      tn->fn->ofs, tn->fn->ofs + tn->fn->size, ref_offset(tn->fn->raw));
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	ret = check_node_data(c, tn);
164*4882a593Smuzhiyun 	if (unlikely(ret < 0)) {
165*4882a593Smuzhiyun 		JFFS2_ERROR("check_node_data() returned error: %d.\n",
166*4882a593Smuzhiyun 			ret);
167*4882a593Smuzhiyun 	} else if (unlikely(ret > 0)) {
168*4882a593Smuzhiyun 		dbg_readinode("CRC error, mark it obsolete.\n");
169*4882a593Smuzhiyun 		jffs2_mark_node_obsolete(c, tn->fn->raw);
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	return ret;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
jffs2_lookup_tn(struct rb_root * tn_root,uint32_t offset)175*4882a593Smuzhiyun static struct jffs2_tmp_dnode_info *jffs2_lookup_tn(struct rb_root *tn_root, uint32_t offset)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct rb_node *next;
178*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *tn = NULL;
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 	dbg_readinode("root %p, offset %d\n", tn_root, offset);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	next = tn_root->rb_node;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	while (next) {
185*4882a593Smuzhiyun 		tn = rb_entry(next, struct jffs2_tmp_dnode_info, rb);
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 		if (tn->fn->ofs < offset)
188*4882a593Smuzhiyun 			next = tn->rb.rb_right;
189*4882a593Smuzhiyun 		else if (tn->fn->ofs >= offset)
190*4882a593Smuzhiyun 			next = tn->rb.rb_left;
191*4882a593Smuzhiyun 		else
192*4882a593Smuzhiyun 			break;
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	return tn;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 
jffs2_kill_tn(struct jffs2_sb_info * c,struct jffs2_tmp_dnode_info * tn)199*4882a593Smuzhiyun static void jffs2_kill_tn(struct jffs2_sb_info *c, struct jffs2_tmp_dnode_info *tn)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	jffs2_mark_node_obsolete(c, tn->fn->raw);
202*4882a593Smuzhiyun 	jffs2_free_full_dnode(tn->fn);
203*4882a593Smuzhiyun 	jffs2_free_tmp_dnode_info(tn);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun  * This function is used when we read an inode. Data nodes arrive in
207*4882a593Smuzhiyun  * arbitrary order -- they may be older or newer than the nodes which
208*4882a593Smuzhiyun  * are already in the tree. Where overlaps occur, the older node can
209*4882a593Smuzhiyun  * be discarded as long as the newer passes the CRC check. We don't
210*4882a593Smuzhiyun  * bother to keep track of holes in this rbtree, and neither do we deal
211*4882a593Smuzhiyun  * with frags -- we can have multiple entries starting at the same
212*4882a593Smuzhiyun  * offset, and the one with the smallest length will come first in the
213*4882a593Smuzhiyun  * ordering.
214*4882a593Smuzhiyun  *
215*4882a593Smuzhiyun  * Returns 0 if the node was handled (including marking it obsolete)
216*4882a593Smuzhiyun  *	 < 0 an if error occurred
217*4882a593Smuzhiyun  */
jffs2_add_tn_to_tree(struct jffs2_sb_info * c,struct jffs2_readinode_info * rii,struct jffs2_tmp_dnode_info * tn)218*4882a593Smuzhiyun static int jffs2_add_tn_to_tree(struct jffs2_sb_info *c,
219*4882a593Smuzhiyun 				struct jffs2_readinode_info *rii,
220*4882a593Smuzhiyun 				struct jffs2_tmp_dnode_info *tn)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	uint32_t fn_end = tn->fn->ofs + tn->fn->size;
223*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *this, *ptn;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	dbg_readinode("insert fragment %#04x-%#04x, ver %u at %08x\n", tn->fn->ofs, fn_end, tn->version, ref_offset(tn->fn->raw));
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	/* If a node has zero dsize, we only have to keep it if it might be the
228*4882a593Smuzhiyun 	   node with highest version -- i.e. the one which will end up as f->metadata.
229*4882a593Smuzhiyun 	   Note that such nodes won't be REF_UNCHECKED since there are no data to
230*4882a593Smuzhiyun 	   check anyway. */
231*4882a593Smuzhiyun 	if (!tn->fn->size) {
232*4882a593Smuzhiyun 		if (rii->mdata_tn) {
233*4882a593Smuzhiyun 			if (rii->mdata_tn->version < tn->version) {
234*4882a593Smuzhiyun 				/* We had a candidate mdata node already */
235*4882a593Smuzhiyun 				dbg_readinode("kill old mdata with ver %d\n", rii->mdata_tn->version);
236*4882a593Smuzhiyun 				jffs2_kill_tn(c, rii->mdata_tn);
237*4882a593Smuzhiyun 			} else {
238*4882a593Smuzhiyun 				dbg_readinode("kill new mdata with ver %d (older than existing %d\n",
239*4882a593Smuzhiyun 					      tn->version, rii->mdata_tn->version);
240*4882a593Smuzhiyun 				jffs2_kill_tn(c, tn);
241*4882a593Smuzhiyun 				return 0;
242*4882a593Smuzhiyun 			}
243*4882a593Smuzhiyun 		}
244*4882a593Smuzhiyun 		rii->mdata_tn = tn;
245*4882a593Smuzhiyun 		dbg_readinode("keep new mdata with ver %d\n", tn->version);
246*4882a593Smuzhiyun 		return 0;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* Find the earliest node which _may_ be relevant to this one */
250*4882a593Smuzhiyun 	this = jffs2_lookup_tn(&rii->tn_root, tn->fn->ofs);
251*4882a593Smuzhiyun 	if (this) {
252*4882a593Smuzhiyun 		/* If the node is coincident with another at a lower address,
253*4882a593Smuzhiyun 		   back up until the other node is found. It may be relevant */
254*4882a593Smuzhiyun 		while (this->overlapped) {
255*4882a593Smuzhiyun 			ptn = tn_prev(this);
256*4882a593Smuzhiyun 			if (!ptn) {
257*4882a593Smuzhiyun 				/*
258*4882a593Smuzhiyun 				 * We killed a node which set the overlapped
259*4882a593Smuzhiyun 				 * flags during the scan. Fix it up.
260*4882a593Smuzhiyun 				 */
261*4882a593Smuzhiyun 				this->overlapped = 0;
262*4882a593Smuzhiyun 				break;
263*4882a593Smuzhiyun 			}
264*4882a593Smuzhiyun 			this = ptn;
265*4882a593Smuzhiyun 		}
266*4882a593Smuzhiyun 		dbg_readinode("'this' found %#04x-%#04x (%s)\n", this->fn->ofs, this->fn->ofs + this->fn->size, this->fn ? "data" : "hole");
267*4882a593Smuzhiyun 	}
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	while (this) {
270*4882a593Smuzhiyun 		if (this->fn->ofs > fn_end)
271*4882a593Smuzhiyun 			break;
272*4882a593Smuzhiyun 		dbg_readinode("Ponder this ver %d, 0x%x-0x%x\n",
273*4882a593Smuzhiyun 			      this->version, this->fn->ofs, this->fn->size);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 		if (this->version == tn->version) {
276*4882a593Smuzhiyun 			/* Version number collision means REF_PRISTINE GC. Accept either of them
277*4882a593Smuzhiyun 			   as long as the CRC is correct. Check the one we have already...  */
278*4882a593Smuzhiyun 			if (!check_tn_node(c, this)) {
279*4882a593Smuzhiyun 				/* The one we already had was OK. Keep it and throw away the new one */
280*4882a593Smuzhiyun 				dbg_readinode("Like old node. Throw away new\n");
281*4882a593Smuzhiyun 				jffs2_kill_tn(c, tn);
282*4882a593Smuzhiyun 				return 0;
283*4882a593Smuzhiyun 			} else {
284*4882a593Smuzhiyun 				/* Who cares if the new one is good; keep it for now anyway. */
285*4882a593Smuzhiyun 				dbg_readinode("Like new node. Throw away old\n");
286*4882a593Smuzhiyun 				rb_replace_node(&this->rb, &tn->rb, &rii->tn_root);
287*4882a593Smuzhiyun 				jffs2_kill_tn(c, this);
288*4882a593Smuzhiyun 				/* Same overlapping from in front and behind */
289*4882a593Smuzhiyun 				return 0;
290*4882a593Smuzhiyun 			}
291*4882a593Smuzhiyun 		}
292*4882a593Smuzhiyun 		if (this->version < tn->version &&
293*4882a593Smuzhiyun 		    this->fn->ofs >= tn->fn->ofs &&
294*4882a593Smuzhiyun 		    this->fn->ofs + this->fn->size <= fn_end) {
295*4882a593Smuzhiyun 			/* New node entirely overlaps 'this' */
296*4882a593Smuzhiyun 			if (check_tn_node(c, tn)) {
297*4882a593Smuzhiyun 				dbg_readinode("new node bad CRC\n");
298*4882a593Smuzhiyun 				jffs2_kill_tn(c, tn);
299*4882a593Smuzhiyun 				return 0;
300*4882a593Smuzhiyun 			}
301*4882a593Smuzhiyun 			/* ... and is good. Kill 'this' and any subsequent nodes which are also overlapped */
302*4882a593Smuzhiyun 			while (this && this->fn->ofs + this->fn->size <= fn_end) {
303*4882a593Smuzhiyun 				struct jffs2_tmp_dnode_info *next = tn_next(this);
304*4882a593Smuzhiyun 				if (this->version < tn->version) {
305*4882a593Smuzhiyun 					tn_erase(this, &rii->tn_root);
306*4882a593Smuzhiyun 					dbg_readinode("Kill overlapped ver %d, 0x%x-0x%x\n",
307*4882a593Smuzhiyun 						      this->version, this->fn->ofs,
308*4882a593Smuzhiyun 						      this->fn->ofs+this->fn->size);
309*4882a593Smuzhiyun 					jffs2_kill_tn(c, this);
310*4882a593Smuzhiyun 				}
311*4882a593Smuzhiyun 				this = next;
312*4882a593Smuzhiyun 			}
313*4882a593Smuzhiyun 			dbg_readinode("Done killing overlapped nodes\n");
314*4882a593Smuzhiyun 			continue;
315*4882a593Smuzhiyun 		}
316*4882a593Smuzhiyun 		if (this->version > tn->version &&
317*4882a593Smuzhiyun 		    this->fn->ofs <= tn->fn->ofs &&
318*4882a593Smuzhiyun 		    this->fn->ofs+this->fn->size >= fn_end) {
319*4882a593Smuzhiyun 			/* New node entirely overlapped by 'this' */
320*4882a593Smuzhiyun 			if (!check_tn_node(c, this)) {
321*4882a593Smuzhiyun 				dbg_readinode("Good CRC on old node. Kill new\n");
322*4882a593Smuzhiyun 				jffs2_kill_tn(c, tn);
323*4882a593Smuzhiyun 				return 0;
324*4882a593Smuzhiyun 			}
325*4882a593Smuzhiyun 			/* ... but 'this' was bad. Replace it... */
326*4882a593Smuzhiyun 			dbg_readinode("Bad CRC on old overlapping node. Kill it\n");
327*4882a593Smuzhiyun 			tn_erase(this, &rii->tn_root);
328*4882a593Smuzhiyun 			jffs2_kill_tn(c, this);
329*4882a593Smuzhiyun 			break;
330*4882a593Smuzhiyun 		}
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 		this = tn_next(this);
333*4882a593Smuzhiyun 	}
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	/* We neither completely obsoleted nor were completely
336*4882a593Smuzhiyun 	   obsoleted by an earlier node. Insert into the tree */
337*4882a593Smuzhiyun 	{
338*4882a593Smuzhiyun 		struct rb_node *parent;
339*4882a593Smuzhiyun 		struct rb_node **link = &rii->tn_root.rb_node;
340*4882a593Smuzhiyun 		struct jffs2_tmp_dnode_info *insert_point = NULL;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		while (*link) {
343*4882a593Smuzhiyun 			parent = *link;
344*4882a593Smuzhiyun 			insert_point = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
345*4882a593Smuzhiyun 			if (tn->fn->ofs > insert_point->fn->ofs)
346*4882a593Smuzhiyun 				link = &insert_point->rb.rb_right;
347*4882a593Smuzhiyun 			else if (tn->fn->ofs < insert_point->fn->ofs ||
348*4882a593Smuzhiyun 				 tn->fn->size < insert_point->fn->size)
349*4882a593Smuzhiyun 				link = &insert_point->rb.rb_left;
350*4882a593Smuzhiyun 			else
351*4882a593Smuzhiyun 				link = &insert_point->rb.rb_right;
352*4882a593Smuzhiyun 		}
353*4882a593Smuzhiyun 		rb_link_node(&tn->rb, &insert_point->rb, link);
354*4882a593Smuzhiyun 		rb_insert_color(&tn->rb, &rii->tn_root);
355*4882a593Smuzhiyun 	}
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	/* If there's anything behind that overlaps us, note it */
358*4882a593Smuzhiyun 	this = tn_prev(tn);
359*4882a593Smuzhiyun 	if (this) {
360*4882a593Smuzhiyun 		while (1) {
361*4882a593Smuzhiyun 			if (this->fn->ofs + this->fn->size > tn->fn->ofs) {
362*4882a593Smuzhiyun 				dbg_readinode("Node is overlapped by %p (v %d, 0x%x-0x%x)\n",
363*4882a593Smuzhiyun 					      this, this->version, this->fn->ofs,
364*4882a593Smuzhiyun 					      this->fn->ofs+this->fn->size);
365*4882a593Smuzhiyun 				tn->overlapped = 1;
366*4882a593Smuzhiyun 				break;
367*4882a593Smuzhiyun 			}
368*4882a593Smuzhiyun 			if (!this->overlapped)
369*4882a593Smuzhiyun 				break;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 			ptn = tn_prev(this);
372*4882a593Smuzhiyun 			if (!ptn) {
373*4882a593Smuzhiyun 				/*
374*4882a593Smuzhiyun 				 * We killed a node which set the overlapped
375*4882a593Smuzhiyun 				 * flags during the scan. Fix it up.
376*4882a593Smuzhiyun 				 */
377*4882a593Smuzhiyun 				this->overlapped = 0;
378*4882a593Smuzhiyun 				break;
379*4882a593Smuzhiyun 			}
380*4882a593Smuzhiyun 			this = ptn;
381*4882a593Smuzhiyun 		}
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/* If the new node overlaps anything ahead, note it */
385*4882a593Smuzhiyun 	this = tn_next(tn);
386*4882a593Smuzhiyun 	while (this && this->fn->ofs < fn_end) {
387*4882a593Smuzhiyun 		this->overlapped = 1;
388*4882a593Smuzhiyun 		dbg_readinode("Node ver %d, 0x%x-0x%x is overlapped\n",
389*4882a593Smuzhiyun 			      this->version, this->fn->ofs,
390*4882a593Smuzhiyun 			      this->fn->ofs+this->fn->size);
391*4882a593Smuzhiyun 		this = tn_next(this);
392*4882a593Smuzhiyun 	}
393*4882a593Smuzhiyun 	return 0;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun /* Trivial function to remove the last node in the tree. Which by definition
397*4882a593Smuzhiyun    has no right-hand child — so can be removed just by making its left-hand
398*4882a593Smuzhiyun    child (if any) take its place under its parent. Since this is only done
399*4882a593Smuzhiyun    when we're consuming the whole tree, there's no need to use rb_erase()
400*4882a593Smuzhiyun    and let it worry about adjusting colours and balancing the tree. That
401*4882a593Smuzhiyun    would just be a waste of time. */
eat_last(struct rb_root * root,struct rb_node * node)402*4882a593Smuzhiyun static void eat_last(struct rb_root *root, struct rb_node *node)
403*4882a593Smuzhiyun {
404*4882a593Smuzhiyun 	struct rb_node *parent = rb_parent(node);
405*4882a593Smuzhiyun 	struct rb_node **link;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* LAST! */
408*4882a593Smuzhiyun 	BUG_ON(node->rb_right);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	if (!parent)
411*4882a593Smuzhiyun 		link = &root->rb_node;
412*4882a593Smuzhiyun 	else if (node == parent->rb_left)
413*4882a593Smuzhiyun 		link = &parent->rb_left;
414*4882a593Smuzhiyun 	else
415*4882a593Smuzhiyun 		link = &parent->rb_right;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	*link = node->rb_left;
418*4882a593Smuzhiyun 	if (node->rb_left)
419*4882a593Smuzhiyun 		node->rb_left->__rb_parent_color = node->__rb_parent_color;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun /* We put the version tree in reverse order, so we can use the same eat_last()
423*4882a593Smuzhiyun    function that we use to consume the tmpnode tree (tn_root). */
ver_insert(struct rb_root * ver_root,struct jffs2_tmp_dnode_info * tn)424*4882a593Smuzhiyun static void ver_insert(struct rb_root *ver_root, struct jffs2_tmp_dnode_info *tn)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	struct rb_node **link = &ver_root->rb_node;
427*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
428*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *this_tn;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	while (*link) {
431*4882a593Smuzhiyun 		parent = *link;
432*4882a593Smuzhiyun 		this_tn = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 		if (tn->version > this_tn->version)
435*4882a593Smuzhiyun 			link = &parent->rb_left;
436*4882a593Smuzhiyun 		else
437*4882a593Smuzhiyun 			link = &parent->rb_right;
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 	dbg_readinode("Link new node at %p (root is %p)\n", link, ver_root);
440*4882a593Smuzhiyun 	rb_link_node(&tn->rb, parent, link);
441*4882a593Smuzhiyun 	rb_insert_color(&tn->rb, ver_root);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun /* Build final, normal fragtree from tn tree. It doesn't matter which order
445*4882a593Smuzhiyun    we add nodes to the real fragtree, as long as they don't overlap. And
446*4882a593Smuzhiyun    having thrown away the majority of overlapped nodes as we went, there
447*4882a593Smuzhiyun    really shouldn't be many sets of nodes which do overlap. If we start at
448*4882a593Smuzhiyun    the end, we can use the overlap markers -- we can just eat nodes which
449*4882a593Smuzhiyun    aren't overlapped, and when we encounter nodes which _do_ overlap we
450*4882a593Smuzhiyun    sort them all into a temporary tree in version order before replaying them. */
jffs2_build_inode_fragtree(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_readinode_info * rii)451*4882a593Smuzhiyun static int jffs2_build_inode_fragtree(struct jffs2_sb_info *c,
452*4882a593Smuzhiyun 				      struct jffs2_inode_info *f,
453*4882a593Smuzhiyun 				      struct jffs2_readinode_info *rii)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *pen, *last, *this;
456*4882a593Smuzhiyun 	struct rb_root ver_root = RB_ROOT;
457*4882a593Smuzhiyun 	uint32_t high_ver = 0;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	if (rii->mdata_tn) {
460*4882a593Smuzhiyun 		dbg_readinode("potential mdata is ver %d at %p\n", rii->mdata_tn->version, rii->mdata_tn);
461*4882a593Smuzhiyun 		high_ver = rii->mdata_tn->version;
462*4882a593Smuzhiyun 		rii->latest_ref = rii->mdata_tn->fn->raw;
463*4882a593Smuzhiyun 	}
464*4882a593Smuzhiyun #ifdef JFFS2_DBG_READINODE_MESSAGES
465*4882a593Smuzhiyun 	this = tn_last(&rii->tn_root);
466*4882a593Smuzhiyun 	while (this) {
467*4882a593Smuzhiyun 		dbg_readinode("tn %p ver %d range 0x%x-0x%x ov %d\n", this, this->version, this->fn->ofs,
468*4882a593Smuzhiyun 			      this->fn->ofs+this->fn->size, this->overlapped);
469*4882a593Smuzhiyun 		this = tn_prev(this);
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun #endif
472*4882a593Smuzhiyun 	pen = tn_last(&rii->tn_root);
473*4882a593Smuzhiyun 	while ((last = pen)) {
474*4882a593Smuzhiyun 		pen = tn_prev(last);
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 		eat_last(&rii->tn_root, &last->rb);
477*4882a593Smuzhiyun 		ver_insert(&ver_root, last);
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 		if (unlikely(last->overlapped)) {
480*4882a593Smuzhiyun 			if (pen)
481*4882a593Smuzhiyun 				continue;
482*4882a593Smuzhiyun 			/*
483*4882a593Smuzhiyun 			 * We killed a node which set the overlapped
484*4882a593Smuzhiyun 			 * flags during the scan. Fix it up.
485*4882a593Smuzhiyun 			 */
486*4882a593Smuzhiyun 			last->overlapped = 0;
487*4882a593Smuzhiyun 		}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 		/* Now we have a bunch of nodes in reverse version
490*4882a593Smuzhiyun 		   order, in the tree at ver_root. Most of the time,
491*4882a593Smuzhiyun 		   there'll actually be only one node in the 'tree',
492*4882a593Smuzhiyun 		   in fact. */
493*4882a593Smuzhiyun 		this = tn_last(&ver_root);
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		while (this) {
496*4882a593Smuzhiyun 			struct jffs2_tmp_dnode_info *vers_next;
497*4882a593Smuzhiyun 			int ret;
498*4882a593Smuzhiyun 			vers_next = tn_prev(this);
499*4882a593Smuzhiyun 			eat_last(&ver_root, &this->rb);
500*4882a593Smuzhiyun 			if (check_tn_node(c, this)) {
501*4882a593Smuzhiyun 				dbg_readinode("node ver %d, 0x%x-0x%x failed CRC\n",
502*4882a593Smuzhiyun 					     this->version, this->fn->ofs,
503*4882a593Smuzhiyun 					     this->fn->ofs+this->fn->size);
504*4882a593Smuzhiyun 				jffs2_kill_tn(c, this);
505*4882a593Smuzhiyun 			} else {
506*4882a593Smuzhiyun 				if (this->version > high_ver) {
507*4882a593Smuzhiyun 					/* Note that this is different from the other
508*4882a593Smuzhiyun 					   highest_version, because this one is only
509*4882a593Smuzhiyun 					   counting _valid_ nodes which could give the
510*4882a593Smuzhiyun 					   latest inode metadata */
511*4882a593Smuzhiyun 					high_ver = this->version;
512*4882a593Smuzhiyun 					rii->latest_ref = this->fn->raw;
513*4882a593Smuzhiyun 				}
514*4882a593Smuzhiyun 				dbg_readinode("Add %p (v %d, 0x%x-0x%x, ov %d) to fragtree\n",
515*4882a593Smuzhiyun 					     this, this->version, this->fn->ofs,
516*4882a593Smuzhiyun 					     this->fn->ofs+this->fn->size, this->overlapped);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 				ret = jffs2_add_full_dnode_to_inode(c, f, this->fn);
519*4882a593Smuzhiyun 				if (ret) {
520*4882a593Smuzhiyun 					/* Free the nodes in vers_root; let the caller
521*4882a593Smuzhiyun 					   deal with the rest */
522*4882a593Smuzhiyun 					JFFS2_ERROR("Add node to tree failed %d\n", ret);
523*4882a593Smuzhiyun 					while (1) {
524*4882a593Smuzhiyun 						vers_next = tn_prev(this);
525*4882a593Smuzhiyun 						if (check_tn_node(c, this))
526*4882a593Smuzhiyun 							jffs2_mark_node_obsolete(c, this->fn->raw);
527*4882a593Smuzhiyun 						jffs2_free_full_dnode(this->fn);
528*4882a593Smuzhiyun 						jffs2_free_tmp_dnode_info(this);
529*4882a593Smuzhiyun 						this = vers_next;
530*4882a593Smuzhiyun 						if (!this)
531*4882a593Smuzhiyun 							break;
532*4882a593Smuzhiyun 						eat_last(&ver_root, &vers_next->rb);
533*4882a593Smuzhiyun 					}
534*4882a593Smuzhiyun 					return ret;
535*4882a593Smuzhiyun 				}
536*4882a593Smuzhiyun 				jffs2_free_tmp_dnode_info(this);
537*4882a593Smuzhiyun 			}
538*4882a593Smuzhiyun 			this = vers_next;
539*4882a593Smuzhiyun 		}
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 	return 0;
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun 
jffs2_free_tmp_dnode_info_list(struct rb_root * list)544*4882a593Smuzhiyun static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *tn, *next;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	rbtree_postorder_for_each_entry_safe(tn, next, list, rb) {
549*4882a593Smuzhiyun 			jffs2_free_full_dnode(tn->fn);
550*4882a593Smuzhiyun 			jffs2_free_tmp_dnode_info(tn);
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	*list = RB_ROOT;
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
jffs2_free_full_dirent_list(struct jffs2_full_dirent * fd)556*4882a593Smuzhiyun static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
557*4882a593Smuzhiyun {
558*4882a593Smuzhiyun 	struct jffs2_full_dirent *next;
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun 	while (fd) {
561*4882a593Smuzhiyun 		next = fd->next;
562*4882a593Smuzhiyun 		jffs2_free_full_dirent(fd);
563*4882a593Smuzhiyun 		fd = next;
564*4882a593Smuzhiyun 	}
565*4882a593Smuzhiyun }
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun /* Returns first valid node after 'ref'. May return 'ref' */
jffs2_first_valid_node(struct jffs2_raw_node_ref * ref)568*4882a593Smuzhiyun static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	while (ref && ref->next_in_ino) {
571*4882a593Smuzhiyun 		if (!ref_obsolete(ref))
572*4882a593Smuzhiyun 			return ref;
573*4882a593Smuzhiyun 		dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
574*4882a593Smuzhiyun 		ref = ref->next_in_ino;
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 	return NULL;
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun /*
580*4882a593Smuzhiyun  * Helper function for jffs2_get_inode_nodes().
581*4882a593Smuzhiyun  * It is called every time an directory entry node is found.
582*4882a593Smuzhiyun  *
583*4882a593Smuzhiyun  * Returns: 0 on success;
584*4882a593Smuzhiyun  * 	    negative error code on failure.
585*4882a593Smuzhiyun  */
read_direntry(struct jffs2_sb_info * c,struct jffs2_raw_node_ref * ref,struct jffs2_raw_dirent * rd,size_t read,struct jffs2_readinode_info * rii)586*4882a593Smuzhiyun static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
587*4882a593Smuzhiyun 				struct jffs2_raw_dirent *rd, size_t read,
588*4882a593Smuzhiyun 				struct jffs2_readinode_info *rii)
589*4882a593Smuzhiyun {
590*4882a593Smuzhiyun 	struct jffs2_full_dirent *fd;
591*4882a593Smuzhiyun 	uint32_t crc;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	/* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
594*4882a593Smuzhiyun 	BUG_ON(ref_obsolete(ref));
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	crc = crc32(0, rd, sizeof(*rd) - 8);
597*4882a593Smuzhiyun 	if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
598*4882a593Smuzhiyun 		JFFS2_NOTICE("header CRC failed on dirent node at %#08x: read %#08x, calculated %#08x\n",
599*4882a593Smuzhiyun 			     ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
600*4882a593Smuzhiyun 		jffs2_mark_node_obsolete(c, ref);
601*4882a593Smuzhiyun 		return 0;
602*4882a593Smuzhiyun 	}
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	/* If we've never checked the CRCs on this node, check them now */
605*4882a593Smuzhiyun 	if (ref_flags(ref) == REF_UNCHECKED) {
606*4882a593Smuzhiyun 		struct jffs2_eraseblock *jeb;
607*4882a593Smuzhiyun 		int len;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 		/* Sanity check */
610*4882a593Smuzhiyun 		if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
611*4882a593Smuzhiyun 			JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
612*4882a593Smuzhiyun 				    ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
613*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, ref);
614*4882a593Smuzhiyun 			return 0;
615*4882a593Smuzhiyun 		}
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 		jeb = &c->blocks[ref->flash_offset / c->sector_size];
618*4882a593Smuzhiyun 		len = ref_totlen(c, jeb, ref);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 		spin_lock(&c->erase_completion_lock);
621*4882a593Smuzhiyun 		jeb->used_size += len;
622*4882a593Smuzhiyun 		jeb->unchecked_size -= len;
623*4882a593Smuzhiyun 		c->used_size += len;
624*4882a593Smuzhiyun 		c->unchecked_size -= len;
625*4882a593Smuzhiyun 		ref->flash_offset = ref_offset(ref) | dirent_node_state(rd);
626*4882a593Smuzhiyun 		spin_unlock(&c->erase_completion_lock);
627*4882a593Smuzhiyun 	}
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	fd = jffs2_alloc_full_dirent(rd->nsize + 1);
630*4882a593Smuzhiyun 	if (unlikely(!fd))
631*4882a593Smuzhiyun 		return -ENOMEM;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	fd->raw = ref;
634*4882a593Smuzhiyun 	fd->version = je32_to_cpu(rd->version);
635*4882a593Smuzhiyun 	fd->ino = je32_to_cpu(rd->ino);
636*4882a593Smuzhiyun 	fd->type = rd->type;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	if (fd->version > rii->highest_version)
639*4882a593Smuzhiyun 		rii->highest_version = fd->version;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/* Pick out the mctime of the latest dirent */
642*4882a593Smuzhiyun 	if(fd->version > rii->mctime_ver && je32_to_cpu(rd->mctime)) {
643*4882a593Smuzhiyun 		rii->mctime_ver = fd->version;
644*4882a593Smuzhiyun 		rii->latest_mctime = je32_to_cpu(rd->mctime);
645*4882a593Smuzhiyun 	}
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	/*
648*4882a593Smuzhiyun 	 * Copy as much of the name as possible from the raw
649*4882a593Smuzhiyun 	 * dirent we've already read from the flash.
650*4882a593Smuzhiyun 	 */
651*4882a593Smuzhiyun 	if (read > sizeof(*rd))
652*4882a593Smuzhiyun 		memcpy(&fd->name[0], &rd->name[0],
653*4882a593Smuzhiyun 		       min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	/* Do we need to copy any more of the name directly from the flash? */
656*4882a593Smuzhiyun 	if (rd->nsize + sizeof(*rd) > read) {
657*4882a593Smuzhiyun 		/* FIXME: point() */
658*4882a593Smuzhiyun 		int err;
659*4882a593Smuzhiyun 		int already = read - sizeof(*rd);
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 		err = jffs2_flash_read(c, (ref_offset(ref)) + read,
662*4882a593Smuzhiyun 				rd->nsize - already, &read, &fd->name[already]);
663*4882a593Smuzhiyun 		if (unlikely(read != rd->nsize - already) && likely(!err)) {
664*4882a593Smuzhiyun 			jffs2_free_full_dirent(fd);
665*4882a593Smuzhiyun 			JFFS2_ERROR("short read: wanted %d bytes, got %zd\n",
666*4882a593Smuzhiyun 				    rd->nsize - already, read);
667*4882a593Smuzhiyun 			return -EIO;
668*4882a593Smuzhiyun 		}
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 		if (unlikely(err)) {
671*4882a593Smuzhiyun 			JFFS2_ERROR("read remainder of name: error %d\n", err);
672*4882a593Smuzhiyun 			jffs2_free_full_dirent(fd);
673*4882a593Smuzhiyun 			return -EIO;
674*4882a593Smuzhiyun 		}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun #ifdef CONFIG_JFFS2_SUMMARY
677*4882a593Smuzhiyun 		/*
678*4882a593Smuzhiyun 		 * we use CONFIG_JFFS2_SUMMARY because without it, we
679*4882a593Smuzhiyun 		 * have checked it while mounting
680*4882a593Smuzhiyun 		 */
681*4882a593Smuzhiyun 		crc = crc32(0, fd->name, rd->nsize);
682*4882a593Smuzhiyun 		if (unlikely(crc != je32_to_cpu(rd->name_crc))) {
683*4882a593Smuzhiyun 			JFFS2_NOTICE("name CRC failed on dirent node at"
684*4882a593Smuzhiyun 			   "%#08x: read %#08x,calculated %#08x\n",
685*4882a593Smuzhiyun 			   ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
686*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, ref);
687*4882a593Smuzhiyun 			jffs2_free_full_dirent(fd);
688*4882a593Smuzhiyun 			return 0;
689*4882a593Smuzhiyun 		}
690*4882a593Smuzhiyun #endif
691*4882a593Smuzhiyun 	}
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	fd->nhash = full_name_hash(NULL, fd->name, rd->nsize);
694*4882a593Smuzhiyun 	fd->next = NULL;
695*4882a593Smuzhiyun 	fd->name[rd->nsize] = '\0';
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/*
698*4882a593Smuzhiyun 	 * Wheee. We now have a complete jffs2_full_dirent structure, with
699*4882a593Smuzhiyun 	 * the name in it and everything. Link it into the list
700*4882a593Smuzhiyun 	 */
701*4882a593Smuzhiyun 	jffs2_add_fd_to_list(c, fd, &rii->fds);
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	return 0;
704*4882a593Smuzhiyun }
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun  * Helper function for jffs2_get_inode_nodes().
708*4882a593Smuzhiyun  * It is called every time an inode node is found.
709*4882a593Smuzhiyun  *
710*4882a593Smuzhiyun  * Returns: 0 on success (possibly after marking a bad node obsolete);
711*4882a593Smuzhiyun  * 	    negative error code on failure.
712*4882a593Smuzhiyun  */
read_dnode(struct jffs2_sb_info * c,struct jffs2_raw_node_ref * ref,struct jffs2_raw_inode * rd,int rdlen,struct jffs2_readinode_info * rii)713*4882a593Smuzhiyun static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
714*4882a593Smuzhiyun 			     struct jffs2_raw_inode *rd, int rdlen,
715*4882a593Smuzhiyun 			     struct jffs2_readinode_info *rii)
716*4882a593Smuzhiyun {
717*4882a593Smuzhiyun 	struct jffs2_tmp_dnode_info *tn;
718*4882a593Smuzhiyun 	uint32_t len, csize;
719*4882a593Smuzhiyun 	int ret = 0;
720*4882a593Smuzhiyun 	uint32_t crc;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
723*4882a593Smuzhiyun 	BUG_ON(ref_obsolete(ref));
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	crc = crc32(0, rd, sizeof(*rd) - 8);
726*4882a593Smuzhiyun 	if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
727*4882a593Smuzhiyun 		JFFS2_NOTICE("node CRC failed on dnode at %#08x: read %#08x, calculated %#08x\n",
728*4882a593Smuzhiyun 			     ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
729*4882a593Smuzhiyun 		jffs2_mark_node_obsolete(c, ref);
730*4882a593Smuzhiyun 		return 0;
731*4882a593Smuzhiyun 	}
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun 	tn = jffs2_alloc_tmp_dnode_info();
734*4882a593Smuzhiyun 	if (!tn) {
735*4882a593Smuzhiyun 		JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
736*4882a593Smuzhiyun 		return -ENOMEM;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	tn->partial_crc = 0;
740*4882a593Smuzhiyun 	csize = je32_to_cpu(rd->csize);
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 	/* If we've never checked the CRCs on this node, check them now */
743*4882a593Smuzhiyun 	if (ref_flags(ref) == REF_UNCHECKED) {
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		/* Sanity checks */
746*4882a593Smuzhiyun 		if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
747*4882a593Smuzhiyun 		    unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
748*4882a593Smuzhiyun 			JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
749*4882a593Smuzhiyun 			jffs2_dbg_dump_node(c, ref_offset(ref));
750*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, ref);
751*4882a593Smuzhiyun 			goto free_out;
752*4882a593Smuzhiyun 		}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		if (jffs2_is_writebuffered(c) && csize != 0) {
755*4882a593Smuzhiyun 			/* At this point we are supposed to check the data CRC
756*4882a593Smuzhiyun 			 * of our unchecked node. But thus far, we do not
757*4882a593Smuzhiyun 			 * know whether the node is valid or obsolete. To
758*4882a593Smuzhiyun 			 * figure this out, we need to walk all the nodes of
759*4882a593Smuzhiyun 			 * the inode and build the inode fragtree. We don't
760*4882a593Smuzhiyun 			 * want to spend time checking data of nodes which may
761*4882a593Smuzhiyun 			 * later be found to be obsolete. So we put off the full
762*4882a593Smuzhiyun 			 * data CRC checking until we have read all the inode
763*4882a593Smuzhiyun 			 * nodes and have started building the fragtree.
764*4882a593Smuzhiyun 			 *
765*4882a593Smuzhiyun 			 * The fragtree is being built starting with nodes
766*4882a593Smuzhiyun 			 * having the highest version number, so we'll be able
767*4882a593Smuzhiyun 			 * to detect whether a node is valid (i.e., it is not
768*4882a593Smuzhiyun 			 * overlapped by a node with higher version) or not.
769*4882a593Smuzhiyun 			 * And we'll be able to check only those nodes, which
770*4882a593Smuzhiyun 			 * are not obsolete.
771*4882a593Smuzhiyun 			 *
772*4882a593Smuzhiyun 			 * Of course, this optimization only makes sense in case
773*4882a593Smuzhiyun 			 * of NAND flashes (or other flashes with
774*4882a593Smuzhiyun 			 * !jffs2_can_mark_obsolete()), since on NOR flashes
775*4882a593Smuzhiyun 			 * nodes are marked obsolete physically.
776*4882a593Smuzhiyun 			 *
777*4882a593Smuzhiyun 			 * Since NAND flashes (or other flashes with
778*4882a593Smuzhiyun 			 * jffs2_is_writebuffered(c)) are anyway read by
779*4882a593Smuzhiyun 			 * fractions of c->wbuf_pagesize, and we have just read
780*4882a593Smuzhiyun 			 * the node header, it is likely that the starting part
781*4882a593Smuzhiyun 			 * of the node data is also read when we read the
782*4882a593Smuzhiyun 			 * header. So we don't mind to check the CRC of the
783*4882a593Smuzhiyun 			 * starting part of the data of the node now, and check
784*4882a593Smuzhiyun 			 * the second part later (in jffs2_check_node_data()).
785*4882a593Smuzhiyun 			 * Of course, we will not need to re-read and re-check
786*4882a593Smuzhiyun 			 * the NAND page which we have just read. This is why we
787*4882a593Smuzhiyun 			 * read the whole NAND page at jffs2_get_inode_nodes(),
788*4882a593Smuzhiyun 			 * while we needed only the node header.
789*4882a593Smuzhiyun 			 */
790*4882a593Smuzhiyun 			unsigned char *buf;
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 			/* 'buf' will point to the start of data */
793*4882a593Smuzhiyun 			buf = (unsigned char *)rd + sizeof(*rd);
794*4882a593Smuzhiyun 			/* len will be the read data length */
795*4882a593Smuzhiyun 			len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
796*4882a593Smuzhiyun 			tn->partial_crc = crc32(0, buf, len);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 			dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 			/* If we actually calculated the whole data CRC
801*4882a593Smuzhiyun 			 * and it is wrong, drop the node. */
802*4882a593Smuzhiyun 			if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
803*4882a593Smuzhiyun 				JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
804*4882a593Smuzhiyun 					ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
805*4882a593Smuzhiyun 				jffs2_mark_node_obsolete(c, ref);
806*4882a593Smuzhiyun 				goto free_out;
807*4882a593Smuzhiyun 			}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 		} else if (csize == 0) {
810*4882a593Smuzhiyun 			/*
811*4882a593Smuzhiyun 			 * We checked the header CRC. If the node has no data, adjust
812*4882a593Smuzhiyun 			 * the space accounting now. For other nodes this will be done
813*4882a593Smuzhiyun 			 * later either when the node is marked obsolete or when its
814*4882a593Smuzhiyun 			 * data is checked.
815*4882a593Smuzhiyun 			 */
816*4882a593Smuzhiyun 			struct jffs2_eraseblock *jeb;
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 			dbg_readinode("the node has no data.\n");
819*4882a593Smuzhiyun 			jeb = &c->blocks[ref->flash_offset / c->sector_size];
820*4882a593Smuzhiyun 			len = ref_totlen(c, jeb, ref);
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 			spin_lock(&c->erase_completion_lock);
823*4882a593Smuzhiyun 			jeb->used_size += len;
824*4882a593Smuzhiyun 			jeb->unchecked_size -= len;
825*4882a593Smuzhiyun 			c->used_size += len;
826*4882a593Smuzhiyun 			c->unchecked_size -= len;
827*4882a593Smuzhiyun 			ref->flash_offset = ref_offset(ref) | REF_NORMAL;
828*4882a593Smuzhiyun 			spin_unlock(&c->erase_completion_lock);
829*4882a593Smuzhiyun 		}
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	tn->fn = jffs2_alloc_full_dnode();
833*4882a593Smuzhiyun 	if (!tn->fn) {
834*4882a593Smuzhiyun 		JFFS2_ERROR("alloc fn failed\n");
835*4882a593Smuzhiyun 		ret = -ENOMEM;
836*4882a593Smuzhiyun 		goto free_out;
837*4882a593Smuzhiyun 	}
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	tn->version = je32_to_cpu(rd->version);
840*4882a593Smuzhiyun 	tn->fn->ofs = je32_to_cpu(rd->offset);
841*4882a593Smuzhiyun 	tn->data_crc = je32_to_cpu(rd->data_crc);
842*4882a593Smuzhiyun 	tn->csize = csize;
843*4882a593Smuzhiyun 	tn->fn->raw = ref;
844*4882a593Smuzhiyun 	tn->overlapped = 0;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	if (tn->version > rii->highest_version)
847*4882a593Smuzhiyun 		rii->highest_version = tn->version;
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	/* There was a bug where we wrote hole nodes out with
850*4882a593Smuzhiyun 	   csize/dsize swapped. Deal with it */
851*4882a593Smuzhiyun 	if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
852*4882a593Smuzhiyun 		tn->fn->size = csize;
853*4882a593Smuzhiyun 	else // normal case...
854*4882a593Smuzhiyun 		tn->fn->size = je32_to_cpu(rd->dsize);
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	dbg_readinode2("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
857*4882a593Smuzhiyun 		       ref_offset(ref), je32_to_cpu(rd->version),
858*4882a593Smuzhiyun 		       je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	ret = jffs2_add_tn_to_tree(c, rii, tn);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	if (ret) {
863*4882a593Smuzhiyun 		jffs2_free_full_dnode(tn->fn);
864*4882a593Smuzhiyun 	free_out:
865*4882a593Smuzhiyun 		jffs2_free_tmp_dnode_info(tn);
866*4882a593Smuzhiyun 		return ret;
867*4882a593Smuzhiyun 	}
868*4882a593Smuzhiyun #ifdef JFFS2_DBG_READINODE2_MESSAGES
869*4882a593Smuzhiyun 	dbg_readinode2("After adding ver %d:\n", je32_to_cpu(rd->version));
870*4882a593Smuzhiyun 	tn = tn_first(&rii->tn_root);
871*4882a593Smuzhiyun 	while (tn) {
872*4882a593Smuzhiyun 		dbg_readinode2("%p: v %d r 0x%x-0x%x ov %d\n",
873*4882a593Smuzhiyun 			       tn, tn->version, tn->fn->ofs,
874*4882a593Smuzhiyun 			       tn->fn->ofs+tn->fn->size, tn->overlapped);
875*4882a593Smuzhiyun 		tn = tn_next(tn);
876*4882a593Smuzhiyun 	}
877*4882a593Smuzhiyun #endif
878*4882a593Smuzhiyun 	return 0;
879*4882a593Smuzhiyun }
880*4882a593Smuzhiyun 
881*4882a593Smuzhiyun /*
882*4882a593Smuzhiyun  * Helper function for jffs2_get_inode_nodes().
883*4882a593Smuzhiyun  * It is called every time an unknown node is found.
884*4882a593Smuzhiyun  *
885*4882a593Smuzhiyun  * Returns: 0 on success;
886*4882a593Smuzhiyun  * 	    negative error code on failure.
887*4882a593Smuzhiyun  */
read_unknown(struct jffs2_sb_info * c,struct jffs2_raw_node_ref * ref,struct jffs2_unknown_node * un)888*4882a593Smuzhiyun static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
889*4882a593Smuzhiyun {
890*4882a593Smuzhiyun 	/* We don't mark unknown nodes as REF_UNCHECKED */
891*4882a593Smuzhiyun 	if (ref_flags(ref) == REF_UNCHECKED) {
892*4882a593Smuzhiyun 		JFFS2_ERROR("REF_UNCHECKED but unknown node at %#08x\n",
893*4882a593Smuzhiyun 			    ref_offset(ref));
894*4882a593Smuzhiyun 		JFFS2_ERROR("Node is {%04x,%04x,%08x,%08x}. Please report this error.\n",
895*4882a593Smuzhiyun 			    je16_to_cpu(un->magic), je16_to_cpu(un->nodetype),
896*4882a593Smuzhiyun 			    je32_to_cpu(un->totlen), je32_to_cpu(un->hdr_crc));
897*4882a593Smuzhiyun 		jffs2_mark_node_obsolete(c, ref);
898*4882a593Smuzhiyun 		return 0;
899*4882a593Smuzhiyun 	}
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun 	un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	case JFFS2_FEATURE_INCOMPAT:
906*4882a593Smuzhiyun 		JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
907*4882a593Smuzhiyun 			    je16_to_cpu(un->nodetype), ref_offset(ref));
908*4882a593Smuzhiyun 		/* EEP */
909*4882a593Smuzhiyun 		BUG();
910*4882a593Smuzhiyun 		break;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	case JFFS2_FEATURE_ROCOMPAT:
913*4882a593Smuzhiyun 		JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
914*4882a593Smuzhiyun 			    je16_to_cpu(un->nodetype), ref_offset(ref));
915*4882a593Smuzhiyun 		BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
916*4882a593Smuzhiyun 		break;
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	case JFFS2_FEATURE_RWCOMPAT_COPY:
919*4882a593Smuzhiyun 		JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
920*4882a593Smuzhiyun 			     je16_to_cpu(un->nodetype), ref_offset(ref));
921*4882a593Smuzhiyun 		break;
922*4882a593Smuzhiyun 
923*4882a593Smuzhiyun 	case JFFS2_FEATURE_RWCOMPAT_DELETE:
924*4882a593Smuzhiyun 		JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
925*4882a593Smuzhiyun 			     je16_to_cpu(un->nodetype), ref_offset(ref));
926*4882a593Smuzhiyun 		jffs2_mark_node_obsolete(c, ref);
927*4882a593Smuzhiyun 		return 0;
928*4882a593Smuzhiyun 	}
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	return 0;
931*4882a593Smuzhiyun }
932*4882a593Smuzhiyun 
933*4882a593Smuzhiyun /*
934*4882a593Smuzhiyun  * Helper function for jffs2_get_inode_nodes().
935*4882a593Smuzhiyun  * The function detects whether more data should be read and reads it if yes.
936*4882a593Smuzhiyun  *
937*4882a593Smuzhiyun  * Returns: 0 on success;
938*4882a593Smuzhiyun  * 	    negative error code on failure.
939*4882a593Smuzhiyun  */
read_more(struct jffs2_sb_info * c,struct jffs2_raw_node_ref * ref,int needed_len,int * rdlen,unsigned char * buf)940*4882a593Smuzhiyun static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
941*4882a593Smuzhiyun 		     int needed_len, int *rdlen, unsigned char *buf)
942*4882a593Smuzhiyun {
943*4882a593Smuzhiyun 	int err, to_read = needed_len - *rdlen;
944*4882a593Smuzhiyun 	size_t retlen;
945*4882a593Smuzhiyun 	uint32_t offs;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	if (jffs2_is_writebuffered(c)) {
948*4882a593Smuzhiyun 		int rem = to_read % c->wbuf_pagesize;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 		if (rem)
951*4882a593Smuzhiyun 			to_read += c->wbuf_pagesize - rem;
952*4882a593Smuzhiyun 	}
953*4882a593Smuzhiyun 
954*4882a593Smuzhiyun 	/* We need to read more data */
955*4882a593Smuzhiyun 	offs = ref_offset(ref) + *rdlen;
956*4882a593Smuzhiyun 
957*4882a593Smuzhiyun 	dbg_readinode("read more %d bytes\n", to_read);
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
960*4882a593Smuzhiyun 	if (err) {
961*4882a593Smuzhiyun 		JFFS2_ERROR("can not read %d bytes from 0x%08x, "
962*4882a593Smuzhiyun 			"error code: %d.\n", to_read, offs, err);
963*4882a593Smuzhiyun 		return err;
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	if (retlen < to_read) {
967*4882a593Smuzhiyun 		JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
968*4882a593Smuzhiyun 				offs, retlen, to_read);
969*4882a593Smuzhiyun 		return -EIO;
970*4882a593Smuzhiyun 	}
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	*rdlen += to_read;
973*4882a593Smuzhiyun 	return 0;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
977*4882a593Smuzhiyun    with this ino. Perform a preliminary ordering on data nodes, throwing away
978*4882a593Smuzhiyun    those which are completely obsoleted by newer ones. The naïve approach we
979*4882a593Smuzhiyun    use to take of just returning them _all_ in version order will cause us to
980*4882a593Smuzhiyun    run out of memory in certain degenerate cases. */
jffs2_get_inode_nodes(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_readinode_info * rii)981*4882a593Smuzhiyun static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
982*4882a593Smuzhiyun 				 struct jffs2_readinode_info *rii)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun 	struct jffs2_raw_node_ref *ref, *valid_ref;
985*4882a593Smuzhiyun 	unsigned char *buf = NULL;
986*4882a593Smuzhiyun 	union jffs2_node_union *node;
987*4882a593Smuzhiyun 	size_t retlen;
988*4882a593Smuzhiyun 	int len, err;
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	rii->mctime_ver = 0;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	dbg_readinode("ino #%u\n", f->inocache->ino);
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun 	/* FIXME: in case of NOR and available ->point() this
995*4882a593Smuzhiyun 	 * needs to be fixed. */
996*4882a593Smuzhiyun 	len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
997*4882a593Smuzhiyun 	buf = kmalloc(len, GFP_KERNEL);
998*4882a593Smuzhiyun 	if (!buf)
999*4882a593Smuzhiyun 		return -ENOMEM;
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun 	spin_lock(&c->erase_completion_lock);
1002*4882a593Smuzhiyun 	valid_ref = jffs2_first_valid_node(f->inocache->nodes);
1003*4882a593Smuzhiyun 	if (!valid_ref && f->inocache->ino != 1)
1004*4882a593Smuzhiyun 		JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
1005*4882a593Smuzhiyun 	while (valid_ref) {
1006*4882a593Smuzhiyun 		/* We can hold a pointer to a non-obsolete node without the spinlock,
1007*4882a593Smuzhiyun 		   but _obsolete_ nodes may disappear at any time, if the block
1008*4882a593Smuzhiyun 		   they're in gets erased. So if we mark 'ref' obsolete while we're
1009*4882a593Smuzhiyun 		   not holding the lock, it can go away immediately. For that reason,
1010*4882a593Smuzhiyun 		   we find the next valid node first, before processing 'ref'.
1011*4882a593Smuzhiyun 		*/
1012*4882a593Smuzhiyun 		ref = valid_ref;
1013*4882a593Smuzhiyun 		valid_ref = jffs2_first_valid_node(ref->next_in_ino);
1014*4882a593Smuzhiyun 		spin_unlock(&c->erase_completion_lock);
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 		cond_resched();
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 		/*
1019*4882a593Smuzhiyun 		 * At this point we don't know the type of the node we're going
1020*4882a593Smuzhiyun 		 * to read, so we do not know the size of its header. In order
1021*4882a593Smuzhiyun 		 * to minimize the amount of flash IO we assume the header is
1022*4882a593Smuzhiyun 		 * of size = JFFS2_MIN_NODE_HEADER.
1023*4882a593Smuzhiyun 		 */
1024*4882a593Smuzhiyun 		len = JFFS2_MIN_NODE_HEADER;
1025*4882a593Smuzhiyun 		if (jffs2_is_writebuffered(c)) {
1026*4882a593Smuzhiyun 			int end, rem;
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun 			/*
1029*4882a593Smuzhiyun 			 * We are about to read JFFS2_MIN_NODE_HEADER bytes,
1030*4882a593Smuzhiyun 			 * but this flash has some minimal I/O unit. It is
1031*4882a593Smuzhiyun 			 * possible that we'll need to read more soon, so read
1032*4882a593Smuzhiyun 			 * up to the next min. I/O unit, in order not to
1033*4882a593Smuzhiyun 			 * re-read the same min. I/O unit twice.
1034*4882a593Smuzhiyun 			 */
1035*4882a593Smuzhiyun 			end = ref_offset(ref) + len;
1036*4882a593Smuzhiyun 			rem = end % c->wbuf_pagesize;
1037*4882a593Smuzhiyun 			if (rem)
1038*4882a593Smuzhiyun 				end += c->wbuf_pagesize - rem;
1039*4882a593Smuzhiyun 			len = end - ref_offset(ref);
1040*4882a593Smuzhiyun 		}
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 		dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 		/* FIXME: point() */
1045*4882a593Smuzhiyun 		err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
1046*4882a593Smuzhiyun 		if (err) {
1047*4882a593Smuzhiyun 			JFFS2_ERROR("can not read %d bytes from 0x%08x, error code: %d.\n", len, ref_offset(ref), err);
1048*4882a593Smuzhiyun 			goto free_out;
1049*4882a593Smuzhiyun 		}
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 		if (retlen < len) {
1052*4882a593Smuzhiyun 			JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
1053*4882a593Smuzhiyun 			err = -EIO;
1054*4882a593Smuzhiyun 			goto free_out;
1055*4882a593Smuzhiyun 		}
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun 		node = (union jffs2_node_union *)buf;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 		/* No need to mask in the valid bit; it shouldn't be invalid */
1060*4882a593Smuzhiyun 		if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
1061*4882a593Smuzhiyun 			JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
1062*4882a593Smuzhiyun 				     ref_offset(ref), je16_to_cpu(node->u.magic),
1063*4882a593Smuzhiyun 				     je16_to_cpu(node->u.nodetype),
1064*4882a593Smuzhiyun 				     je32_to_cpu(node->u.totlen),
1065*4882a593Smuzhiyun 				     je32_to_cpu(node->u.hdr_crc));
1066*4882a593Smuzhiyun 			jffs2_dbg_dump_node(c, ref_offset(ref));
1067*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, ref);
1068*4882a593Smuzhiyun 			goto cont;
1069*4882a593Smuzhiyun 		}
1070*4882a593Smuzhiyun 		if (je16_to_cpu(node->u.magic) != JFFS2_MAGIC_BITMASK) {
1071*4882a593Smuzhiyun 			/* Not a JFFS2 node, whinge and move on */
1072*4882a593Smuzhiyun 			JFFS2_NOTICE("Wrong magic bitmask 0x%04x in node header at %#08x.\n",
1073*4882a593Smuzhiyun 				     je16_to_cpu(node->u.magic), ref_offset(ref));
1074*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, ref);
1075*4882a593Smuzhiyun 			goto cont;
1076*4882a593Smuzhiyun 		}
1077*4882a593Smuzhiyun 
1078*4882a593Smuzhiyun 		switch (je16_to_cpu(node->u.nodetype)) {
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun 		case JFFS2_NODETYPE_DIRENT:
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent) &&
1083*4882a593Smuzhiyun 			    len < sizeof(struct jffs2_raw_dirent)) {
1084*4882a593Smuzhiyun 				err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
1085*4882a593Smuzhiyun 				if (unlikely(err))
1086*4882a593Smuzhiyun 					goto free_out;
1087*4882a593Smuzhiyun 			}
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 			err = read_direntry(c, ref, &node->d, retlen, rii);
1090*4882a593Smuzhiyun 			if (unlikely(err))
1091*4882a593Smuzhiyun 				goto free_out;
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 			break;
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 		case JFFS2_NODETYPE_INODE:
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode) &&
1098*4882a593Smuzhiyun 			    len < sizeof(struct jffs2_raw_inode)) {
1099*4882a593Smuzhiyun 				err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
1100*4882a593Smuzhiyun 				if (unlikely(err))
1101*4882a593Smuzhiyun 					goto free_out;
1102*4882a593Smuzhiyun 			}
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 			err = read_dnode(c, ref, &node->i, len, rii);
1105*4882a593Smuzhiyun 			if (unlikely(err))
1106*4882a593Smuzhiyun 				goto free_out;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 			break;
1109*4882a593Smuzhiyun 
1110*4882a593Smuzhiyun 		default:
1111*4882a593Smuzhiyun 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node) &&
1112*4882a593Smuzhiyun 			    len < sizeof(struct jffs2_unknown_node)) {
1113*4882a593Smuzhiyun 				err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
1114*4882a593Smuzhiyun 				if (unlikely(err))
1115*4882a593Smuzhiyun 					goto free_out;
1116*4882a593Smuzhiyun 			}
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 			err = read_unknown(c, ref, &node->u);
1119*4882a593Smuzhiyun 			if (unlikely(err))
1120*4882a593Smuzhiyun 				goto free_out;
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun 		}
1123*4882a593Smuzhiyun 	cont:
1124*4882a593Smuzhiyun 		spin_lock(&c->erase_completion_lock);
1125*4882a593Smuzhiyun 	}
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	spin_unlock(&c->erase_completion_lock);
1128*4882a593Smuzhiyun 	kfree(buf);
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 	f->highest_version = rii->highest_version;
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
1133*4882a593Smuzhiyun 		      f->inocache->ino, rii->highest_version, rii->latest_mctime,
1134*4882a593Smuzhiyun 		      rii->mctime_ver);
1135*4882a593Smuzhiyun 	return 0;
1136*4882a593Smuzhiyun 
1137*4882a593Smuzhiyun  free_out:
1138*4882a593Smuzhiyun 	jffs2_free_tmp_dnode_info_list(&rii->tn_root);
1139*4882a593Smuzhiyun 	jffs2_free_full_dirent_list(rii->fds);
1140*4882a593Smuzhiyun 	rii->fds = NULL;
1141*4882a593Smuzhiyun 	kfree(buf);
1142*4882a593Smuzhiyun 	return err;
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun 
jffs2_do_read_inode_internal(struct jffs2_sb_info * c,struct jffs2_inode_info * f,struct jffs2_raw_inode * latest_node)1145*4882a593Smuzhiyun static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
1146*4882a593Smuzhiyun 					struct jffs2_inode_info *f,
1147*4882a593Smuzhiyun 					struct jffs2_raw_inode *latest_node)
1148*4882a593Smuzhiyun {
1149*4882a593Smuzhiyun 	struct jffs2_readinode_info rii;
1150*4882a593Smuzhiyun 	uint32_t crc, new_size;
1151*4882a593Smuzhiyun 	size_t retlen;
1152*4882a593Smuzhiyun 	int ret;
1153*4882a593Smuzhiyun 
1154*4882a593Smuzhiyun 	dbg_readinode("ino #%u pino/nlink is %d\n", f->inocache->ino,
1155*4882a593Smuzhiyun 		      f->inocache->pino_nlink);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	memset(&rii, 0, sizeof(rii));
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	/* Grab all nodes relevant to this ino */
1160*4882a593Smuzhiyun 	ret = jffs2_get_inode_nodes(c, f, &rii);
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	if (ret) {
1163*4882a593Smuzhiyun 		JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
1164*4882a593Smuzhiyun 		if (f->inocache->state == INO_STATE_READING)
1165*4882a593Smuzhiyun 			jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1166*4882a593Smuzhiyun 		return ret;
1167*4882a593Smuzhiyun 	}
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun 	ret = jffs2_build_inode_fragtree(c, f, &rii);
1170*4882a593Smuzhiyun 	if (ret) {
1171*4882a593Smuzhiyun 		JFFS2_ERROR("Failed to build final fragtree for inode #%u: error %d\n",
1172*4882a593Smuzhiyun 			    f->inocache->ino, ret);
1173*4882a593Smuzhiyun 		if (f->inocache->state == INO_STATE_READING)
1174*4882a593Smuzhiyun 			jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1175*4882a593Smuzhiyun 		jffs2_free_tmp_dnode_info_list(&rii.tn_root);
1176*4882a593Smuzhiyun 		/* FIXME: We could at least crc-check them all */
1177*4882a593Smuzhiyun 		if (rii.mdata_tn) {
1178*4882a593Smuzhiyun 			jffs2_free_full_dnode(rii.mdata_tn->fn);
1179*4882a593Smuzhiyun 			jffs2_free_tmp_dnode_info(rii.mdata_tn);
1180*4882a593Smuzhiyun 			rii.mdata_tn = NULL;
1181*4882a593Smuzhiyun 		}
1182*4882a593Smuzhiyun 		return ret;
1183*4882a593Smuzhiyun 	}
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	if (rii.mdata_tn) {
1186*4882a593Smuzhiyun 		if (rii.mdata_tn->fn->raw == rii.latest_ref) {
1187*4882a593Smuzhiyun 			f->metadata = rii.mdata_tn->fn;
1188*4882a593Smuzhiyun 			jffs2_free_tmp_dnode_info(rii.mdata_tn);
1189*4882a593Smuzhiyun 		} else {
1190*4882a593Smuzhiyun 			jffs2_kill_tn(c, rii.mdata_tn);
1191*4882a593Smuzhiyun 		}
1192*4882a593Smuzhiyun 		rii.mdata_tn = NULL;
1193*4882a593Smuzhiyun 	}
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	f->dents = rii.fds;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	if (unlikely(!rii.latest_ref)) {
1200*4882a593Smuzhiyun 		/* No data nodes for this inode. */
1201*4882a593Smuzhiyun 		if (f->inocache->ino != 1) {
1202*4882a593Smuzhiyun 			JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
1203*4882a593Smuzhiyun 			if (!rii.fds) {
1204*4882a593Smuzhiyun 				if (f->inocache->state == INO_STATE_READING)
1205*4882a593Smuzhiyun 					jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1206*4882a593Smuzhiyun 				return -EIO;
1207*4882a593Smuzhiyun 			}
1208*4882a593Smuzhiyun 			JFFS2_NOTICE("but it has children so we fake some modes for it\n");
1209*4882a593Smuzhiyun 		}
1210*4882a593Smuzhiyun 		latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
1211*4882a593Smuzhiyun 		latest_node->version = cpu_to_je32(0);
1212*4882a593Smuzhiyun 		latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
1213*4882a593Smuzhiyun 		latest_node->isize = cpu_to_je32(0);
1214*4882a593Smuzhiyun 		latest_node->gid = cpu_to_je16(0);
1215*4882a593Smuzhiyun 		latest_node->uid = cpu_to_je16(0);
1216*4882a593Smuzhiyun 		if (f->inocache->state == INO_STATE_READING)
1217*4882a593Smuzhiyun 			jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1218*4882a593Smuzhiyun 		return 0;
1219*4882a593Smuzhiyun 	}
1220*4882a593Smuzhiyun 
1221*4882a593Smuzhiyun 	ret = jffs2_flash_read(c, ref_offset(rii.latest_ref), sizeof(*latest_node), &retlen, (void *)latest_node);
1222*4882a593Smuzhiyun 	if (ret || retlen != sizeof(*latest_node)) {
1223*4882a593Smuzhiyun 		JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
1224*4882a593Smuzhiyun 			ret, retlen, sizeof(*latest_node));
1225*4882a593Smuzhiyun 		/* FIXME: If this fails, there seems to be a memory leak. Find it. */
1226*4882a593Smuzhiyun 		return ret ? ret : -EIO;
1227*4882a593Smuzhiyun 	}
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	crc = crc32(0, latest_node, sizeof(*latest_node)-8);
1230*4882a593Smuzhiyun 	if (crc != je32_to_cpu(latest_node->node_crc)) {
1231*4882a593Smuzhiyun 		JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
1232*4882a593Smuzhiyun 			f->inocache->ino, ref_offset(rii.latest_ref));
1233*4882a593Smuzhiyun 		return -EIO;
1234*4882a593Smuzhiyun 	}
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
1237*4882a593Smuzhiyun 	case S_IFDIR:
1238*4882a593Smuzhiyun 		if (rii.mctime_ver > je32_to_cpu(latest_node->version)) {
1239*4882a593Smuzhiyun 			/* The times in the latest_node are actually older than
1240*4882a593Smuzhiyun 			   mctime in the latest dirent. Cheat. */
1241*4882a593Smuzhiyun 			latest_node->ctime = latest_node->mtime = cpu_to_je32(rii.latest_mctime);
1242*4882a593Smuzhiyun 		}
1243*4882a593Smuzhiyun 		break;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	case S_IFREG:
1247*4882a593Smuzhiyun 		/* If it was a regular file, truncate it to the latest node's isize */
1248*4882a593Smuzhiyun 		new_size = jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
1249*4882a593Smuzhiyun 		if (new_size != je32_to_cpu(latest_node->isize)) {
1250*4882a593Smuzhiyun 			JFFS2_WARNING("Truncating ino #%u to %d bytes failed because it only had %d bytes to start with!\n",
1251*4882a593Smuzhiyun 				      f->inocache->ino, je32_to_cpu(latest_node->isize), new_size);
1252*4882a593Smuzhiyun 			latest_node->isize = cpu_to_je32(new_size);
1253*4882a593Smuzhiyun 		}
1254*4882a593Smuzhiyun 		break;
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	case S_IFLNK:
1257*4882a593Smuzhiyun 		/* Hack to work around broken isize in old symlink code.
1258*4882a593Smuzhiyun 		   Remove this when dwmw2 comes to his senses and stops
1259*4882a593Smuzhiyun 		   symlinks from being an entirely gratuitous special
1260*4882a593Smuzhiyun 		   case. */
1261*4882a593Smuzhiyun 		if (!je32_to_cpu(latest_node->isize))
1262*4882a593Smuzhiyun 			latest_node->isize = latest_node->dsize;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 		if (f->inocache->state != INO_STATE_CHECKING) {
1265*4882a593Smuzhiyun 			/* Symlink's inode data is the target path. Read it and
1266*4882a593Smuzhiyun 			 * keep in RAM to facilitate quick follow symlink
1267*4882a593Smuzhiyun 			 * operation. */
1268*4882a593Smuzhiyun 			uint32_t csize = je32_to_cpu(latest_node->csize);
1269*4882a593Smuzhiyun 			if (csize > JFFS2_MAX_NAME_LEN)
1270*4882a593Smuzhiyun 				return -ENAMETOOLONG;
1271*4882a593Smuzhiyun 			f->target = kmalloc(csize + 1, GFP_KERNEL);
1272*4882a593Smuzhiyun 			if (!f->target) {
1273*4882a593Smuzhiyun 				JFFS2_ERROR("can't allocate %u bytes of memory for the symlink target path cache\n", csize);
1274*4882a593Smuzhiyun 				return -ENOMEM;
1275*4882a593Smuzhiyun 			}
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun 			ret = jffs2_flash_read(c, ref_offset(rii.latest_ref) + sizeof(*latest_node),
1278*4882a593Smuzhiyun 					       csize, &retlen, (char *)f->target);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 			if (ret || retlen != csize) {
1281*4882a593Smuzhiyun 				if (retlen != csize)
1282*4882a593Smuzhiyun 					ret = -EIO;
1283*4882a593Smuzhiyun 				kfree(f->target);
1284*4882a593Smuzhiyun 				f->target = NULL;
1285*4882a593Smuzhiyun 				return ret;
1286*4882a593Smuzhiyun 			}
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 			f->target[csize] = '\0';
1289*4882a593Smuzhiyun 			dbg_readinode("symlink's target '%s' cached\n", f->target);
1290*4882a593Smuzhiyun 		}
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun 		fallthrough;
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	case S_IFBLK:
1295*4882a593Smuzhiyun 	case S_IFCHR:
1296*4882a593Smuzhiyun 		/* Certain inode types should have only one data node, and it's
1297*4882a593Smuzhiyun 		   kept as the metadata node */
1298*4882a593Smuzhiyun 		if (f->metadata) {
1299*4882a593Smuzhiyun 			JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
1300*4882a593Smuzhiyun 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
1301*4882a593Smuzhiyun 			return -EIO;
1302*4882a593Smuzhiyun 		}
1303*4882a593Smuzhiyun 		if (!frag_first(&f->fragtree)) {
1304*4882a593Smuzhiyun 			JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
1305*4882a593Smuzhiyun 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
1306*4882a593Smuzhiyun 			return -EIO;
1307*4882a593Smuzhiyun 		}
1308*4882a593Smuzhiyun 		/* ASSERT: f->fraglist != NULL */
1309*4882a593Smuzhiyun 		if (frag_next(frag_first(&f->fragtree))) {
1310*4882a593Smuzhiyun 			JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
1311*4882a593Smuzhiyun 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
1312*4882a593Smuzhiyun 			/* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
1313*4882a593Smuzhiyun 			return -EIO;
1314*4882a593Smuzhiyun 		}
1315*4882a593Smuzhiyun 		/* OK. We're happy */
1316*4882a593Smuzhiyun 		f->metadata = frag_first(&f->fragtree)->node;
1317*4882a593Smuzhiyun 		jffs2_free_node_frag(frag_first(&f->fragtree));
1318*4882a593Smuzhiyun 		f->fragtree = RB_ROOT;
1319*4882a593Smuzhiyun 		break;
1320*4882a593Smuzhiyun 	}
1321*4882a593Smuzhiyun 	if (f->inocache->state == INO_STATE_READING)
1322*4882a593Smuzhiyun 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	return 0;
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun /* Scan the list of all nodes present for this ino, build map of versions, etc. */
jffs2_do_read_inode(struct jffs2_sb_info * c,struct jffs2_inode_info * f,uint32_t ino,struct jffs2_raw_inode * latest_node)1328*4882a593Smuzhiyun int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
1329*4882a593Smuzhiyun 			uint32_t ino, struct jffs2_raw_inode *latest_node)
1330*4882a593Smuzhiyun {
1331*4882a593Smuzhiyun 	dbg_readinode("read inode #%u\n", ino);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun  retry_inocache:
1334*4882a593Smuzhiyun 	spin_lock(&c->inocache_lock);
1335*4882a593Smuzhiyun 	f->inocache = jffs2_get_ino_cache(c, ino);
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	if (f->inocache) {
1338*4882a593Smuzhiyun 		/* Check its state. We may need to wait before we can use it */
1339*4882a593Smuzhiyun 		switch(f->inocache->state) {
1340*4882a593Smuzhiyun 		case INO_STATE_UNCHECKED:
1341*4882a593Smuzhiyun 		case INO_STATE_CHECKEDABSENT:
1342*4882a593Smuzhiyun 			f->inocache->state = INO_STATE_READING;
1343*4882a593Smuzhiyun 			break;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 		case INO_STATE_CHECKING:
1346*4882a593Smuzhiyun 		case INO_STATE_GC:
1347*4882a593Smuzhiyun 			/* If it's in either of these states, we need
1348*4882a593Smuzhiyun 			   to wait for whoever's got it to finish and
1349*4882a593Smuzhiyun 			   put it back. */
1350*4882a593Smuzhiyun 			dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
1351*4882a593Smuzhiyun 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
1352*4882a593Smuzhiyun 			goto retry_inocache;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 		case INO_STATE_READING:
1355*4882a593Smuzhiyun 		case INO_STATE_PRESENT:
1356*4882a593Smuzhiyun 			/* Eep. This should never happen. It can
1357*4882a593Smuzhiyun 			happen if Linux calls read_inode() again
1358*4882a593Smuzhiyun 			before clear_inode() has finished though. */
1359*4882a593Smuzhiyun 			JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
1360*4882a593Smuzhiyun 			/* Fail. That's probably better than allowing it to succeed */
1361*4882a593Smuzhiyun 			f->inocache = NULL;
1362*4882a593Smuzhiyun 			break;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 		default:
1365*4882a593Smuzhiyun 			BUG();
1366*4882a593Smuzhiyun 		}
1367*4882a593Smuzhiyun 	}
1368*4882a593Smuzhiyun 	spin_unlock(&c->inocache_lock);
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	if (!f->inocache && ino == 1) {
1371*4882a593Smuzhiyun 		/* Special case - no root inode on medium */
1372*4882a593Smuzhiyun 		f->inocache = jffs2_alloc_inode_cache();
1373*4882a593Smuzhiyun 		if (!f->inocache) {
1374*4882a593Smuzhiyun 			JFFS2_ERROR("cannot allocate inocache for root inode\n");
1375*4882a593Smuzhiyun 			return -ENOMEM;
1376*4882a593Smuzhiyun 		}
1377*4882a593Smuzhiyun 		dbg_readinode("creating inocache for root inode\n");
1378*4882a593Smuzhiyun 		memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
1379*4882a593Smuzhiyun 		f->inocache->ino = f->inocache->pino_nlink = 1;
1380*4882a593Smuzhiyun 		f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
1381*4882a593Smuzhiyun 		f->inocache->state = INO_STATE_READING;
1382*4882a593Smuzhiyun 		jffs2_add_ino_cache(c, f->inocache);
1383*4882a593Smuzhiyun 	}
1384*4882a593Smuzhiyun 	if (!f->inocache) {
1385*4882a593Smuzhiyun 		JFFS2_ERROR("requested to read a nonexistent ino %u\n", ino);
1386*4882a593Smuzhiyun 		return -ENOENT;
1387*4882a593Smuzhiyun 	}
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	return jffs2_do_read_inode_internal(c, f, latest_node);
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
jffs2_do_crccheck_inode(struct jffs2_sb_info * c,struct jffs2_inode_cache * ic)1392*4882a593Smuzhiyun int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
1393*4882a593Smuzhiyun {
1394*4882a593Smuzhiyun 	struct jffs2_raw_inode n;
1395*4882a593Smuzhiyun 	struct jffs2_inode_info *f = kzalloc(sizeof(*f), GFP_KERNEL);
1396*4882a593Smuzhiyun 	int ret;
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	if (!f)
1399*4882a593Smuzhiyun 		return -ENOMEM;
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	mutex_init(&f->sem);
1402*4882a593Smuzhiyun 	mutex_lock(&f->sem);
1403*4882a593Smuzhiyun 	f->inocache = ic;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	ret = jffs2_do_read_inode_internal(c, f, &n);
1406*4882a593Smuzhiyun 	mutex_unlock(&f->sem);
1407*4882a593Smuzhiyun 	jffs2_do_clear_inode(c, f);
1408*4882a593Smuzhiyun 	jffs2_xattr_do_crccheck_inode(c, ic);
1409*4882a593Smuzhiyun 	kfree (f);
1410*4882a593Smuzhiyun 	return ret;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun 
jffs2_do_clear_inode(struct jffs2_sb_info * c,struct jffs2_inode_info * f)1413*4882a593Smuzhiyun void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun 	struct jffs2_full_dirent *fd, *fds;
1416*4882a593Smuzhiyun 	int deleted;
1417*4882a593Smuzhiyun 
1418*4882a593Smuzhiyun 	jffs2_xattr_delete_inode(c, f->inocache);
1419*4882a593Smuzhiyun 	mutex_lock(&f->sem);
1420*4882a593Smuzhiyun 	deleted = f->inocache && !f->inocache->pino_nlink;
1421*4882a593Smuzhiyun 
1422*4882a593Smuzhiyun 	if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
1423*4882a593Smuzhiyun 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun 	if (f->metadata) {
1426*4882a593Smuzhiyun 		if (deleted)
1427*4882a593Smuzhiyun 			jffs2_mark_node_obsolete(c, f->metadata->raw);
1428*4882a593Smuzhiyun 		jffs2_free_full_dnode(f->metadata);
1429*4882a593Smuzhiyun 	}
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	fds = f->dents;
1434*4882a593Smuzhiyun 	while(fds) {
1435*4882a593Smuzhiyun 		fd = fds;
1436*4882a593Smuzhiyun 		fds = fd->next;
1437*4882a593Smuzhiyun 		jffs2_free_full_dirent(fd);
1438*4882a593Smuzhiyun 	}
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
1441*4882a593Smuzhiyun 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
1442*4882a593Smuzhiyun 		if (f->inocache->nodes == (void *)f->inocache)
1443*4882a593Smuzhiyun 			jffs2_del_ino_cache(c, f->inocache);
1444*4882a593Smuzhiyun 	}
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun 	mutex_unlock(&f->sem);
1447*4882a593Smuzhiyun }
1448