xref: /openbmc/linux/fs/jffs2/readinode.c (revision 3877f0b6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * JFFS2 -- Journalling Flash File System, Version 2.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2001-2003 Red Hat, Inc.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Created by David Woodhouse <dwmw2@infradead.org>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * For licensing information, see the file 'LICENCE' in this directory.
91da177e4SLinus Torvalds  *
10182ec4eeSThomas Gleixner  * $Id: readinode.c,v 1.143 2005/11/07 11:14:41 gleixner Exp $
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <linux/kernel.h>
15737b7661SAndrew Lunn #include <linux/sched.h>
161da177e4SLinus Torvalds #include <linux/slab.h>
171da177e4SLinus Torvalds #include <linux/fs.h>
181da177e4SLinus Torvalds #include <linux/crc32.h>
191da177e4SLinus Torvalds #include <linux/pagemap.h>
201da177e4SLinus Torvalds #include <linux/mtd/mtd.h>
211da177e4SLinus Torvalds #include <linux/compiler.h>
221da177e4SLinus Torvalds #include "nodelist.h"
231da177e4SLinus Torvalds 
24f97117d1SArtem B. Bityutskiy /*
25f97117d1SArtem B. Bityutskiy  * Put a new tmp_dnode_info into the temporaty RB-tree, keeping the list in
26f97117d1SArtem B. Bityutskiy  * order of increasing version.
27f97117d1SArtem B. Bityutskiy  */
28f97117d1SArtem B. Bityutskiy static void jffs2_add_tn_to_tree(struct jffs2_tmp_dnode_info *tn, struct rb_root *list)
291da177e4SLinus Torvalds {
30f97117d1SArtem B. Bityutskiy 	struct rb_node **p = &list->rb_node;
31f97117d1SArtem B. Bityutskiy 	struct rb_node * parent = NULL;
32f97117d1SArtem B. Bityutskiy 	struct jffs2_tmp_dnode_info *this;
331da177e4SLinus Torvalds 
34f97117d1SArtem B. Bityutskiy 	while (*p) {
35f97117d1SArtem B. Bityutskiy 		parent = *p;
36f97117d1SArtem B. Bityutskiy 		this = rb_entry(parent, struct jffs2_tmp_dnode_info, rb);
371da177e4SLinus Torvalds 
38f97117d1SArtem B. Bityutskiy 		/* There may actually be a collision here, but it doesn't
39f97117d1SArtem B. Bityutskiy 		   actually matter. As long as the two nodes with the same
40f97117d1SArtem B. Bityutskiy 		   version are together, it's all fine. */
411e0da3cbSArtem B. Bityutskiy 		if (tn->version > this->version)
42f97117d1SArtem B. Bityutskiy 			p = &(*p)->rb_left;
43f97117d1SArtem B. Bityutskiy 		else
44f97117d1SArtem B. Bityutskiy 			p = &(*p)->rb_right;
45f97117d1SArtem B. Bityutskiy 	}
461da177e4SLinus Torvalds 
47f97117d1SArtem B. Bityutskiy 	rb_link_node(&tn->rb, parent, p);
48f97117d1SArtem B. Bityutskiy 	rb_insert_color(&tn->rb, list);
49f97117d1SArtem B. Bityutskiy }
50f97117d1SArtem B. Bityutskiy 
51f97117d1SArtem B. Bityutskiy static void jffs2_free_tmp_dnode_info_list(struct rb_root *list)
52f97117d1SArtem B. Bityutskiy {
53f97117d1SArtem B. Bityutskiy 	struct rb_node *this;
54f97117d1SArtem B. Bityutskiy 	struct jffs2_tmp_dnode_info *tn;
55f97117d1SArtem B. Bityutskiy 
56f97117d1SArtem B. Bityutskiy 	this = list->rb_node;
57f97117d1SArtem B. Bityutskiy 
58f97117d1SArtem B. Bityutskiy 	/* Now at bottom of tree */
59f97117d1SArtem B. Bityutskiy 	while (this) {
60f97117d1SArtem B. Bityutskiy 		if (this->rb_left)
61f97117d1SArtem B. Bityutskiy 			this = this->rb_left;
62f97117d1SArtem B. Bityutskiy 		else if (this->rb_right)
63f97117d1SArtem B. Bityutskiy 			this = this->rb_right;
64f97117d1SArtem B. Bityutskiy 		else {
65f97117d1SArtem B. Bityutskiy 			tn = rb_entry(this, struct jffs2_tmp_dnode_info, rb);
66f97117d1SArtem B. Bityutskiy 			jffs2_free_full_dnode(tn->fn);
67f97117d1SArtem B. Bityutskiy 			jffs2_free_tmp_dnode_info(tn);
68f97117d1SArtem B. Bityutskiy 
69f97117d1SArtem B. Bityutskiy 			this = this->rb_parent;
70f97117d1SArtem B. Bityutskiy 			if (!this)
711da177e4SLinus Torvalds 				break;
721da177e4SLinus Torvalds 
73f97117d1SArtem B. Bityutskiy 			if (this->rb_left == &tn->rb)
74f97117d1SArtem B. Bityutskiy 				this->rb_left = NULL;
75f97117d1SArtem B. Bityutskiy 			else if (this->rb_right == &tn->rb)
76f97117d1SArtem B. Bityutskiy 				this->rb_right = NULL;
77f97117d1SArtem B. Bityutskiy 			else BUG();
78f97117d1SArtem B. Bityutskiy 		}
79f97117d1SArtem B. Bityutskiy 	}
80f97117d1SArtem B. Bityutskiy 	list->rb_node = NULL;
81f97117d1SArtem B. Bityutskiy }
821da177e4SLinus Torvalds 
83f97117d1SArtem B. Bityutskiy static void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
84f97117d1SArtem B. Bityutskiy {
85f97117d1SArtem B. Bityutskiy 	struct jffs2_full_dirent *next;
86f97117d1SArtem B. Bityutskiy 
87f97117d1SArtem B. Bityutskiy 	while (fd) {
88f97117d1SArtem B. Bityutskiy 		next = fd->next;
89f97117d1SArtem B. Bityutskiy 		jffs2_free_full_dirent(fd);
90f97117d1SArtem B. Bityutskiy 		fd = next;
91f97117d1SArtem B. Bityutskiy 	}
92f97117d1SArtem B. Bityutskiy }
93f97117d1SArtem B. Bityutskiy 
94f97117d1SArtem B. Bityutskiy /* Returns first valid node after 'ref'. May return 'ref' */
95f97117d1SArtem B. Bityutskiy static struct jffs2_raw_node_ref *jffs2_first_valid_node(struct jffs2_raw_node_ref *ref)
96f97117d1SArtem B. Bityutskiy {
97f97117d1SArtem B. Bityutskiy 	while (ref && ref->next_in_ino) {
98f97117d1SArtem B. Bityutskiy 		if (!ref_obsolete(ref))
99f97117d1SArtem B. Bityutskiy 			return ref;
100733802d9SArtem B. Bityutskiy 		dbg_noderef("node at 0x%08x is obsoleted. Ignoring.\n", ref_offset(ref));
101f97117d1SArtem B. Bityutskiy 		ref = ref->next_in_ino;
102f97117d1SArtem B. Bityutskiy 	}
103f97117d1SArtem B. Bityutskiy 	return NULL;
104f97117d1SArtem B. Bityutskiy }
105f97117d1SArtem B. Bityutskiy 
106f97117d1SArtem B. Bityutskiy /*
107f97117d1SArtem B. Bityutskiy  * Helper function for jffs2_get_inode_nodes().
108f97117d1SArtem B. Bityutskiy  * It is called every time an directory entry node is found.
109f97117d1SArtem B. Bityutskiy  *
110f97117d1SArtem B. Bityutskiy  * Returns: 0 on succes;
111f97117d1SArtem B. Bityutskiy  * 	    1 if the node should be marked obsolete;
112f97117d1SArtem B. Bityutskiy  * 	    negative error code on failure.
113f97117d1SArtem B. Bityutskiy  */
1141e0da3cbSArtem B. Bityutskiy static inline int read_direntry(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
1150ef675d4SAtsushi Nemoto 				struct jffs2_raw_dirent *rd, size_t read, struct jffs2_full_dirent **fdp,
1161e0da3cbSArtem B. Bityutskiy 				uint32_t *latest_mctime, uint32_t *mctime_ver)
117f97117d1SArtem B. Bityutskiy {
118f97117d1SArtem B. Bityutskiy 	struct jffs2_full_dirent *fd;
119f97117d1SArtem B. Bityutskiy 
120f97117d1SArtem B. Bityutskiy 	/* The direntry nodes are checked during the flash scanning */
121f97117d1SArtem B. Bityutskiy 	BUG_ON(ref_flags(ref) == REF_UNCHECKED);
122f97117d1SArtem B. Bityutskiy 	/* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
123f97117d1SArtem B. Bityutskiy 	BUG_ON(ref_obsolete(ref));
124f97117d1SArtem B. Bityutskiy 
125f97117d1SArtem B. Bityutskiy 	/* Sanity check */
126f97117d1SArtem B. Bityutskiy 	if (unlikely(PAD((rd->nsize + sizeof(*rd))) != PAD(je32_to_cpu(rd->totlen)))) {
127e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("illegal nsize in node at %#08x: nsize %#02x, totlen %#04x\n",
128f97117d1SArtem B. Bityutskiy 		       ref_offset(ref), rd->nsize, je32_to_cpu(rd->totlen));
129f97117d1SArtem B. Bityutskiy 		return 1;
130f97117d1SArtem B. Bityutskiy 	}
131f97117d1SArtem B. Bityutskiy 
132f97117d1SArtem B. Bityutskiy 	fd = jffs2_alloc_full_dirent(rd->nsize + 1);
133f97117d1SArtem B. Bityutskiy 	if (unlikely(!fd))
134f97117d1SArtem B. Bityutskiy 		return -ENOMEM;
135f97117d1SArtem B. Bityutskiy 
136f97117d1SArtem B. Bityutskiy 	fd->raw = ref;
137f97117d1SArtem B. Bityutskiy 	fd->version = je32_to_cpu(rd->version);
138f97117d1SArtem B. Bityutskiy 	fd->ino = je32_to_cpu(rd->ino);
139f97117d1SArtem B. Bityutskiy 	fd->type = rd->type;
140f97117d1SArtem B. Bityutskiy 
141f97117d1SArtem B. Bityutskiy 	/* Pick out the mctime of the latest dirent */
1423a69e0cdSArtem B. Bityutskiy 	if(fd->version > *mctime_ver && je32_to_cpu(rd->mctime)) {
143f97117d1SArtem B. Bityutskiy 		*mctime_ver = fd->version;
144f97117d1SArtem B. Bityutskiy 		*latest_mctime = je32_to_cpu(rd->mctime);
145f97117d1SArtem B. Bityutskiy 	}
146f97117d1SArtem B. Bityutskiy 
147f97117d1SArtem B. Bityutskiy 	/*
148f97117d1SArtem B. Bityutskiy 	 * Copy as much of the name as possible from the raw
149f97117d1SArtem B. Bityutskiy 	 * dirent we've already read from the flash.
150f97117d1SArtem B. Bityutskiy 	 */
151f97117d1SArtem B. Bityutskiy 	if (read > sizeof(*rd))
152f97117d1SArtem B. Bityutskiy 		memcpy(&fd->name[0], &rd->name[0],
153f97117d1SArtem B. Bityutskiy 		       min_t(uint32_t, rd->nsize, (read - sizeof(*rd)) ));
154f97117d1SArtem B. Bityutskiy 
155f97117d1SArtem B. Bityutskiy 	/* Do we need to copy any more of the name directly from the flash? */
156f97117d1SArtem B. Bityutskiy 	if (rd->nsize + sizeof(*rd) > read) {
157f97117d1SArtem B. Bityutskiy 		/* FIXME: point() */
158f97117d1SArtem B. Bityutskiy 		int err;
159f97117d1SArtem B. Bityutskiy 		int already = read - sizeof(*rd);
160f97117d1SArtem B. Bityutskiy 
161f97117d1SArtem B. Bityutskiy 		err = jffs2_flash_read(c, (ref_offset(ref)) + read,
162f97117d1SArtem B. Bityutskiy 				rd->nsize - already, &read, &fd->name[already]);
163f97117d1SArtem B. Bityutskiy 		if (unlikely(read != rd->nsize - already) && likely(!err))
164f97117d1SArtem B. Bityutskiy 			return -EIO;
165f97117d1SArtem B. Bityutskiy 
166f97117d1SArtem B. Bityutskiy 		if (unlikely(err)) {
167e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("read remainder of name: error %d\n", err);
168f97117d1SArtem B. Bityutskiy 			jffs2_free_full_dirent(fd);
169f97117d1SArtem B. Bityutskiy 			return -EIO;
170f97117d1SArtem B. Bityutskiy 		}
171f97117d1SArtem B. Bityutskiy 	}
172f97117d1SArtem B. Bityutskiy 
173f97117d1SArtem B. Bityutskiy 	fd->nhash = full_name_hash(fd->name, rd->nsize);
174f97117d1SArtem B. Bityutskiy 	fd->next = NULL;
175f97117d1SArtem B. Bityutskiy 	fd->name[rd->nsize] = '\0';
176f97117d1SArtem B. Bityutskiy 
177f97117d1SArtem B. Bityutskiy 	/*
178f97117d1SArtem B. Bityutskiy 	 * Wheee. We now have a complete jffs2_full_dirent structure, with
179f97117d1SArtem B. Bityutskiy 	 * the name in it and everything. Link it into the list
180f97117d1SArtem B. Bityutskiy 	 */
181f97117d1SArtem B. Bityutskiy 	jffs2_add_fd_to_list(c, fd, fdp);
182f97117d1SArtem B. Bityutskiy 
183f97117d1SArtem B. Bityutskiy 	return 0;
184f97117d1SArtem B. Bityutskiy }
185f97117d1SArtem B. Bityutskiy 
186f97117d1SArtem B. Bityutskiy /*
187f97117d1SArtem B. Bityutskiy  * Helper function for jffs2_get_inode_nodes().
188f97117d1SArtem B. Bityutskiy  * It is called every time an inode node is found.
189f97117d1SArtem B. Bityutskiy  *
190f97117d1SArtem B. Bityutskiy  * Returns: 0 on succes;
191f97117d1SArtem B. Bityutskiy  * 	    1 if the node should be marked obsolete;
192f97117d1SArtem B. Bityutskiy  * 	    negative error code on failure.
193f97117d1SArtem B. Bityutskiy  */
1941e0da3cbSArtem B. Bityutskiy static inline int read_dnode(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
1951e0da3cbSArtem B. Bityutskiy 			     struct jffs2_raw_inode *rd, struct rb_root *tnp, int rdlen,
1961e0da3cbSArtem B. Bityutskiy 			     uint32_t *latest_mctime, uint32_t *mctime_ver)
197f97117d1SArtem B. Bityutskiy {
198f97117d1SArtem B. Bityutskiy 	struct jffs2_tmp_dnode_info *tn;
1991e0da3cbSArtem B. Bityutskiy 	uint32_t len, csize;
2001e0da3cbSArtem B. Bityutskiy 	int ret = 1;
201f97117d1SArtem B. Bityutskiy 
202f97117d1SArtem B. Bityutskiy 	/* Obsoleted. This cannot happen, surely? dwmw2 20020308 */
203f97117d1SArtem B. Bityutskiy 	BUG_ON(ref_obsolete(ref));
204f97117d1SArtem B. Bityutskiy 
2051e0da3cbSArtem B. Bityutskiy 	tn = jffs2_alloc_tmp_dnode_info();
2061e0da3cbSArtem B. Bityutskiy 	if (!tn) {
207fb6a82c9SRandy Dunlap 		JFFS2_ERROR("failed to allocate tn (%zu bytes).\n", sizeof(*tn));
2081e0da3cbSArtem B. Bityutskiy 		return -ENOMEM;
2091e0da3cbSArtem B. Bityutskiy 	}
2101e0da3cbSArtem B. Bityutskiy 
2111e0da3cbSArtem B. Bityutskiy 	tn->partial_crc = 0;
2121e0da3cbSArtem B. Bityutskiy 	csize = je32_to_cpu(rd->csize);
2131e0da3cbSArtem B. Bityutskiy 
214f97117d1SArtem B. Bityutskiy 	/* If we've never checked the CRCs on this node, check them now */
215f97117d1SArtem B. Bityutskiy 	if (ref_flags(ref) == REF_UNCHECKED) {
2161e0da3cbSArtem B. Bityutskiy 		uint32_t crc;
217f97117d1SArtem B. Bityutskiy 
218f97117d1SArtem B. Bityutskiy 		crc = crc32(0, rd, sizeof(*rd) - 8);
219f97117d1SArtem B. Bityutskiy 		if (unlikely(crc != je32_to_cpu(rd->node_crc))) {
220e0d60137SArtem B. Bityutskiy 			JFFS2_NOTICE("header CRC failed on node at %#08x: read %#08x, calculated %#08x\n",
221f97117d1SArtem B. Bityutskiy 					ref_offset(ref), je32_to_cpu(rd->node_crc), crc);
2221e0da3cbSArtem B. Bityutskiy 			goto free_out;
223f97117d1SArtem B. Bityutskiy 		}
224f97117d1SArtem B. Bityutskiy 
225f97117d1SArtem B. Bityutskiy 		/* Sanity checks */
226f97117d1SArtem B. Bityutskiy 		if (unlikely(je32_to_cpu(rd->offset) > je32_to_cpu(rd->isize)) ||
227f97117d1SArtem B. Bityutskiy 		    unlikely(PAD(je32_to_cpu(rd->csize) + sizeof(*rd)) != PAD(je32_to_cpu(rd->totlen)))) {
228e0d60137SArtem B. Bityutskiy 				JFFS2_WARNING("inode node header CRC is corrupted at %#08x\n", ref_offset(ref));
229737b7661SAndrew Lunn 				jffs2_dbg_dump_node(c, ref_offset(ref));
2301e0da3cbSArtem B. Bityutskiy 			goto free_out;
231f97117d1SArtem B. Bityutskiy 		}
232f97117d1SArtem B. Bityutskiy 
2331e0da3cbSArtem B. Bityutskiy 		if (jffs2_is_writebuffered(c) && csize != 0) {
2341e0da3cbSArtem B. Bityutskiy 			/* At this point we are supposed to check the data CRC
2351e0da3cbSArtem B. Bityutskiy 			 * of our unchecked node. But thus far, we do not
2361e0da3cbSArtem B. Bityutskiy 			 * know whether the node is valid or obsolete. To
2371e0da3cbSArtem B. Bityutskiy 			 * figure this out, we need to walk all the nodes of
2381e0da3cbSArtem B. Bityutskiy 			 * the inode and build the inode fragtree. We don't
2391e0da3cbSArtem B. Bityutskiy 			 * want to spend time checking data of nodes which may
2401e0da3cbSArtem B. Bityutskiy 			 * later be found to be obsolete. So we put off the full
2411e0da3cbSArtem B. Bityutskiy 			 * data CRC checking until we have read all the inode
2421e0da3cbSArtem B. Bityutskiy 			 * nodes and have started building the fragtree.
2431e0da3cbSArtem B. Bityutskiy 			 *
2441e0da3cbSArtem B. Bityutskiy 			 * The fragtree is being built starting with nodes
2451e0da3cbSArtem B. Bityutskiy 			 * having the highest version number, so we'll be able
2461e0da3cbSArtem B. Bityutskiy 			 * to detect whether a node is valid (i.e., it is not
2471e0da3cbSArtem B. Bityutskiy 			 * overlapped by a node with higher version) or not.
2481e0da3cbSArtem B. Bityutskiy 			 * And we'll be able to check only those nodes, which
2491e0da3cbSArtem B. Bityutskiy 			 * are not obsolete.
2501e0da3cbSArtem B. Bityutskiy 			 *
2511e0da3cbSArtem B. Bityutskiy 			 * Of course, this optimization only makes sense in case
2521e0da3cbSArtem B. Bityutskiy 			 * of NAND flashes (or other flashes whith
2531e0da3cbSArtem B. Bityutskiy 			 * !jffs2_can_mark_obsolete()), since on NOR flashes
2541e0da3cbSArtem B. Bityutskiy 			 * nodes are marked obsolete physically.
2551e0da3cbSArtem B. Bityutskiy 			 *
2561e0da3cbSArtem B. Bityutskiy 			 * Since NAND flashes (or other flashes with
2571e0da3cbSArtem B. Bityutskiy 			 * jffs2_is_writebuffered(c)) are anyway read by
2581e0da3cbSArtem B. Bityutskiy 			 * fractions of c->wbuf_pagesize, and we have just read
2591e0da3cbSArtem B. Bityutskiy 			 * the node header, it is likely that the starting part
2601e0da3cbSArtem B. Bityutskiy 			 * of the node data is also read when we read the
2611e0da3cbSArtem B. Bityutskiy 			 * header. So we don't mind to check the CRC of the
2621e0da3cbSArtem B. Bityutskiy 			 * starting part of the data of the node now, and check
2631e0da3cbSArtem B. Bityutskiy 			 * the second part later (in jffs2_check_node_data()).
2641e0da3cbSArtem B. Bityutskiy 			 * Of course, we will not need to re-read and re-check
2651e0da3cbSArtem B. Bityutskiy 			 * the NAND page which we have just read. This is why we
2661e0da3cbSArtem B. Bityutskiy 			 * read the whole NAND page at jffs2_get_inode_nodes(),
2671e0da3cbSArtem B. Bityutskiy 			 * while we needed only the node header.
2681e0da3cbSArtem B. Bityutskiy 			 */
2691e0da3cbSArtem B. Bityutskiy 			unsigned char *buf;
270f97117d1SArtem B. Bityutskiy 
2711e0da3cbSArtem B. Bityutskiy 			/* 'buf' will point to the start of data */
2721e0da3cbSArtem B. Bityutskiy 			buf = (unsigned char *)rd + sizeof(*rd);
2731e0da3cbSArtem B. Bityutskiy 			/* len will be the read data length */
2741e0da3cbSArtem B. Bityutskiy 			len = min_t(uint32_t, rdlen - sizeof(*rd), csize);
275280562b2SArtem B. Bityutskiy 			tn->partial_crc = crc32(0, buf, len);
276f97117d1SArtem B. Bityutskiy 
277733802d9SArtem B. Bityutskiy 			dbg_readinode("Calculates CRC (%#08x) for %d bytes, csize %d\n", tn->partial_crc, len, csize);
278f97117d1SArtem B. Bityutskiy 
2791e0da3cbSArtem B. Bityutskiy 			/* If we actually calculated the whole data CRC
2801e0da3cbSArtem B. Bityutskiy 			 * and it is wrong, drop the node. */
2813c091337SArtem B. Bityutskiy 			if (len >= csize && unlikely(tn->partial_crc != je32_to_cpu(rd->data_crc))) {
28239243508SArtem B. Bityutskiy 				JFFS2_NOTICE("wrong data CRC in data node at 0x%08x: read %#08x, calculated %#08x.\n",
28339243508SArtem B. Bityutskiy 					ref_offset(ref), tn->partial_crc, je32_to_cpu(rd->data_crc));
2841e0da3cbSArtem B. Bityutskiy 				goto free_out;
28539243508SArtem B. Bityutskiy 			}
286f97117d1SArtem B. Bityutskiy 
2871e0da3cbSArtem B. Bityutskiy 		} else if (csize == 0) {
2881e0da3cbSArtem B. Bityutskiy 			/*
2891e0da3cbSArtem B. Bityutskiy 			 * We checked the header CRC. If the node has no data, adjust
2901e0da3cbSArtem B. Bityutskiy 			 * the space accounting now. For other nodes this will be done
2911e0da3cbSArtem B. Bityutskiy 			 * later either when the node is marked obsolete or when its
2921e0da3cbSArtem B. Bityutskiy 			 * data is checked.
2931e0da3cbSArtem B. Bityutskiy 			 */
2941e0da3cbSArtem B. Bityutskiy 			struct jffs2_eraseblock *jeb;
2951e0da3cbSArtem B. Bityutskiy 
296733802d9SArtem B. Bityutskiy 			dbg_readinode("the node has no data.\n");
297f97117d1SArtem B. Bityutskiy 			jeb = &c->blocks[ref->flash_offset / c->sector_size];
298f97117d1SArtem B. Bityutskiy 			len = ref_totlen(c, jeb, ref);
299f97117d1SArtem B. Bityutskiy 
300f97117d1SArtem B. Bityutskiy 			spin_lock(&c->erase_completion_lock);
301f97117d1SArtem B. Bityutskiy 			jeb->used_size += len;
302f97117d1SArtem B. Bityutskiy 			jeb->unchecked_size -= len;
303f97117d1SArtem B. Bityutskiy 			c->used_size += len;
304f97117d1SArtem B. Bityutskiy 			c->unchecked_size -= len;
305f97117d1SArtem B. Bityutskiy 			ref->flash_offset = ref_offset(ref) | REF_NORMAL;
306f97117d1SArtem B. Bityutskiy 			spin_unlock(&c->erase_completion_lock);
307f97117d1SArtem B. Bityutskiy 		}
308f97117d1SArtem B. Bityutskiy 	}
309f97117d1SArtem B. Bityutskiy 
310f97117d1SArtem B. Bityutskiy 	tn->fn = jffs2_alloc_full_dnode();
311f97117d1SArtem B. Bityutskiy 	if (!tn->fn) {
312e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("alloc fn failed\n");
3131e0da3cbSArtem B. Bityutskiy 		ret = -ENOMEM;
3141e0da3cbSArtem B. Bityutskiy 		goto free_out;
315f97117d1SArtem B. Bityutskiy 	}
316f97117d1SArtem B. Bityutskiy 
317f97117d1SArtem B. Bityutskiy 	tn->version = je32_to_cpu(rd->version);
318f97117d1SArtem B. Bityutskiy 	tn->fn->ofs = je32_to_cpu(rd->offset);
3191e0da3cbSArtem B. Bityutskiy 	tn->data_crc = je32_to_cpu(rd->data_crc);
3201e0da3cbSArtem B. Bityutskiy 	tn->csize = csize;
321f97117d1SArtem B. Bityutskiy 	tn->fn->raw = ref;
322f97117d1SArtem B. Bityutskiy 
323f97117d1SArtem B. Bityutskiy 	/* There was a bug where we wrote hole nodes out with
324f97117d1SArtem B. Bityutskiy 	   csize/dsize swapped. Deal with it */
3251e0da3cbSArtem B. Bityutskiy 	if (rd->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(rd->dsize) && csize)
3261e0da3cbSArtem B. Bityutskiy 		tn->fn->size = csize;
327f97117d1SArtem B. Bityutskiy 	else // normal case...
328f97117d1SArtem B. Bityutskiy 		tn->fn->size = je32_to_cpu(rd->dsize);
329f97117d1SArtem B. Bityutskiy 
330733802d9SArtem B. Bityutskiy 	dbg_readinode("dnode @%08x: ver %u, offset %#04x, dsize %#04x, csize %#04x\n",
331280562b2SArtem B. Bityutskiy 		  ref_offset(ref), je32_to_cpu(rd->version), je32_to_cpu(rd->offset), je32_to_cpu(rd->dsize), csize);
332f97117d1SArtem B. Bityutskiy 
333f97117d1SArtem B. Bityutskiy 	jffs2_add_tn_to_tree(tn, tnp);
334f97117d1SArtem B. Bityutskiy 
335f97117d1SArtem B. Bityutskiy 	return 0;
3361e0da3cbSArtem B. Bityutskiy 
3371e0da3cbSArtem B. Bityutskiy free_out:
3381e0da3cbSArtem B. Bityutskiy 	jffs2_free_tmp_dnode_info(tn);
3391e0da3cbSArtem B. Bityutskiy 	return ret;
340f97117d1SArtem B. Bityutskiy }
341f97117d1SArtem B. Bityutskiy 
342f97117d1SArtem B. Bityutskiy /*
343f97117d1SArtem B. Bityutskiy  * Helper function for jffs2_get_inode_nodes().
344f97117d1SArtem B. Bityutskiy  * It is called every time an unknown node is found.
345f97117d1SArtem B. Bityutskiy  *
3463877f0b6SDavid Woodhouse  * Returns: 0 on success;
347f97117d1SArtem B. Bityutskiy  * 	    1 if the node should be marked obsolete;
348f97117d1SArtem B. Bityutskiy  * 	    negative error code on failure.
349f97117d1SArtem B. Bityutskiy  */
3501e0da3cbSArtem B. Bityutskiy static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, struct jffs2_unknown_node *un)
351f97117d1SArtem B. Bityutskiy {
352f97117d1SArtem B. Bityutskiy 	/* We don't mark unknown nodes as REF_UNCHECKED */
353f97117d1SArtem B. Bityutskiy 	BUG_ON(ref_flags(ref) == REF_UNCHECKED);
354f97117d1SArtem B. Bityutskiy 
355f97117d1SArtem B. Bityutskiy 	un->nodetype = cpu_to_je16(JFFS2_NODE_ACCURATE | je16_to_cpu(un->nodetype));
356f97117d1SArtem B. Bityutskiy 
357f97117d1SArtem B. Bityutskiy 	switch(je16_to_cpu(un->nodetype) & JFFS2_COMPAT_MASK) {
358f97117d1SArtem B. Bityutskiy 
359f97117d1SArtem B. Bityutskiy 	case JFFS2_FEATURE_INCOMPAT:
360e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("unknown INCOMPAT nodetype %#04X at %#08x\n",
361f97117d1SArtem B. Bityutskiy 			    je16_to_cpu(un->nodetype), ref_offset(ref));
362f97117d1SArtem B. Bityutskiy 		/* EEP */
363f97117d1SArtem B. Bityutskiy 		BUG();
364f97117d1SArtem B. Bityutskiy 		break;
365f97117d1SArtem B. Bityutskiy 
366f97117d1SArtem B. Bityutskiy 	case JFFS2_FEATURE_ROCOMPAT:
367e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("unknown ROCOMPAT nodetype %#04X at %#08x\n",
368f97117d1SArtem B. Bityutskiy 			    je16_to_cpu(un->nodetype), ref_offset(ref));
369f97117d1SArtem B. Bityutskiy 		BUG_ON(!(c->flags & JFFS2_SB_FLAG_RO));
370f97117d1SArtem B. Bityutskiy 		break;
371f97117d1SArtem B. Bityutskiy 
372f97117d1SArtem B. Bityutskiy 	case JFFS2_FEATURE_RWCOMPAT_COPY:
373e0d60137SArtem B. Bityutskiy 		JFFS2_NOTICE("unknown RWCOMPAT_COPY nodetype %#04X at %#08x\n",
374f97117d1SArtem B. Bityutskiy 			     je16_to_cpu(un->nodetype), ref_offset(ref));
375f97117d1SArtem B. Bityutskiy 		break;
376f97117d1SArtem B. Bityutskiy 
377f97117d1SArtem B. Bityutskiy 	case JFFS2_FEATURE_RWCOMPAT_DELETE:
378e0d60137SArtem B. Bityutskiy 		JFFS2_NOTICE("unknown RWCOMPAT_DELETE nodetype %#04X at %#08x\n",
379f97117d1SArtem B. Bityutskiy 			     je16_to_cpu(un->nodetype), ref_offset(ref));
380f97117d1SArtem B. Bityutskiy 		return 1;
381f97117d1SArtem B. Bityutskiy 	}
382f97117d1SArtem B. Bityutskiy 
383f97117d1SArtem B. Bityutskiy 	return 0;
384f97117d1SArtem B. Bityutskiy }
385f97117d1SArtem B. Bityutskiy 
3861e0da3cbSArtem B. Bityutskiy /*
3871e0da3cbSArtem B. Bityutskiy  * Helper function for jffs2_get_inode_nodes().
3881e0da3cbSArtem B. Bityutskiy  * The function detects whether more data should be read and reads it if yes.
3891e0da3cbSArtem B. Bityutskiy  *
3901e0da3cbSArtem B. Bityutskiy  * Returns: 0 on succes;
3911e0da3cbSArtem B. Bityutskiy  * 	    negative error code on failure.
3921e0da3cbSArtem B. Bityutskiy  */
3931e0da3cbSArtem B. Bityutskiy static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
3941e0da3cbSArtem B. Bityutskiy 		     int right_size, int *rdlen, unsigned char *buf, unsigned char *bufstart)
3951e0da3cbSArtem B. Bityutskiy {
3961e0da3cbSArtem B. Bityutskiy 	int right_len, err, len;
3971e0da3cbSArtem B. Bityutskiy 	size_t retlen;
3981e0da3cbSArtem B. Bityutskiy 	uint32_t offs;
3991e0da3cbSArtem B. Bityutskiy 
4001e0da3cbSArtem B. Bityutskiy 	if (jffs2_is_writebuffered(c)) {
4011e0da3cbSArtem B. Bityutskiy 		right_len = c->wbuf_pagesize - (bufstart - buf);
4021e0da3cbSArtem B. Bityutskiy 		if (right_size + (int)(bufstart - buf) > c->wbuf_pagesize)
4031e0da3cbSArtem B. Bityutskiy 			right_len += c->wbuf_pagesize;
4041e0da3cbSArtem B. Bityutskiy 	} else
4051e0da3cbSArtem B. Bityutskiy 		right_len = right_size;
4061e0da3cbSArtem B. Bityutskiy 
4071e0da3cbSArtem B. Bityutskiy 	if (*rdlen == right_len)
4081e0da3cbSArtem B. Bityutskiy 		return 0;
4091e0da3cbSArtem B. Bityutskiy 
4101e0da3cbSArtem B. Bityutskiy 	/* We need to read more data */
4111e0da3cbSArtem B. Bityutskiy 	offs = ref_offset(ref) + *rdlen;
4121e0da3cbSArtem B. Bityutskiy 	if (jffs2_is_writebuffered(c)) {
4131e0da3cbSArtem B. Bityutskiy 		bufstart = buf + c->wbuf_pagesize;
4141e0da3cbSArtem B. Bityutskiy 		len = c->wbuf_pagesize;
4151e0da3cbSArtem B. Bityutskiy 	} else {
4161e0da3cbSArtem B. Bityutskiy 		bufstart = buf + *rdlen;
4171e0da3cbSArtem B. Bityutskiy 		len = right_size - *rdlen;
4181e0da3cbSArtem B. Bityutskiy 	}
4191e0da3cbSArtem B. Bityutskiy 
420733802d9SArtem B. Bityutskiy 	dbg_readinode("read more %d bytes\n", len);
4211e0da3cbSArtem B. Bityutskiy 
4221e0da3cbSArtem B. Bityutskiy 	err = jffs2_flash_read(c, offs, len, &retlen, bufstart);
4231e0da3cbSArtem B. Bityutskiy 	if (err) {
4241e0da3cbSArtem B. Bityutskiy 		JFFS2_ERROR("can not read %d bytes from 0x%08x, "
4251e0da3cbSArtem B. Bityutskiy 			"error code: %d.\n", len, offs, err);
4261e0da3cbSArtem B. Bityutskiy 		return err;
4271e0da3cbSArtem B. Bityutskiy 	}
4281e0da3cbSArtem B. Bityutskiy 
4291e0da3cbSArtem B. Bityutskiy 	if (retlen < len) {
430fb6a82c9SRandy Dunlap 		JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
4311e0da3cbSArtem B. Bityutskiy 				offs, retlen, len);
4321e0da3cbSArtem B. Bityutskiy 		return -EIO;
4331e0da3cbSArtem B. Bityutskiy 	}
4341e0da3cbSArtem B. Bityutskiy 
4351e0da3cbSArtem B. Bityutskiy 	*rdlen = right_len;
4361e0da3cbSArtem B. Bityutskiy 
4371e0da3cbSArtem B. Bityutskiy 	return 0;
4381e0da3cbSArtem B. Bityutskiy }
4391e0da3cbSArtem B. Bityutskiy 
440f97117d1SArtem B. Bityutskiy /* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
441f97117d1SArtem B. Bityutskiy    with this ino, returning the former in order of version */
442f97117d1SArtem B. Bityutskiy static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
443f97117d1SArtem B. Bityutskiy 				 struct rb_root *tnp, struct jffs2_full_dirent **fdp,
444f97117d1SArtem B. Bityutskiy 				 uint32_t *highest_version, uint32_t *latest_mctime,
445f97117d1SArtem B. Bityutskiy 				 uint32_t *mctime_ver)
446f97117d1SArtem B. Bityutskiy {
447f97117d1SArtem B. Bityutskiy 	struct jffs2_raw_node_ref *ref, *valid_ref;
448f97117d1SArtem B. Bityutskiy 	struct rb_root ret_tn = RB_ROOT;
449f97117d1SArtem B. Bityutskiy 	struct jffs2_full_dirent *ret_fd = NULL;
4501e0da3cbSArtem B. Bityutskiy 	unsigned char *buf = NULL;
4511e0da3cbSArtem B. Bityutskiy 	union jffs2_node_union *node;
452f97117d1SArtem B. Bityutskiy 	size_t retlen;
4531e0da3cbSArtem B. Bityutskiy 	int len, err;
454f97117d1SArtem B. Bityutskiy 
455f97117d1SArtem B. Bityutskiy 	*mctime_ver = 0;
456f97117d1SArtem B. Bityutskiy 
457733802d9SArtem B. Bityutskiy 	dbg_readinode("ino #%u\n", f->inocache->ino);
458f97117d1SArtem B. Bityutskiy 
4591e0da3cbSArtem B. Bityutskiy 	if (jffs2_is_writebuffered(c)) {
4601e0da3cbSArtem B. Bityutskiy 		/*
4611e0da3cbSArtem B. Bityutskiy 		 * If we have the write buffer, we assume the minimal I/O unit
4621e0da3cbSArtem B. Bityutskiy 		 * is c->wbuf_pagesize. We implement some optimizations which in
4631e0da3cbSArtem B. Bityutskiy 		 * this case and we need a temporary buffer of size =
4641e0da3cbSArtem B. Bityutskiy 		 * 2*c->wbuf_pagesize bytes (see comments in read_dnode()).
4651e0da3cbSArtem B. Bityutskiy 		 * Basically, we want to read not only the node header, but the
4661e0da3cbSArtem B. Bityutskiy 		 * whole wbuf (NAND page in case of NAND) or 2, if the node
4671e0da3cbSArtem B. Bityutskiy 		 * header overlaps the border between the 2 wbufs.
4681e0da3cbSArtem B. Bityutskiy 		 */
4691e0da3cbSArtem B. Bityutskiy 		len = 2*c->wbuf_pagesize;
4701e0da3cbSArtem B. Bityutskiy 	} else {
4711e0da3cbSArtem B. Bityutskiy 		/*
4721e0da3cbSArtem B. Bityutskiy 		 * When there is no write buffer, the size of the temporary
4731e0da3cbSArtem B. Bityutskiy 		 * buffer is the size of the larges node header.
4741e0da3cbSArtem B. Bityutskiy 		 */
4751e0da3cbSArtem B. Bityutskiy 		len = sizeof(union jffs2_node_union);
4761e0da3cbSArtem B. Bityutskiy 	}
4771e0da3cbSArtem B. Bityutskiy 
4781e0da3cbSArtem B. Bityutskiy 	/* FIXME: in case of NOR and available ->point() this
4791e0da3cbSArtem B. Bityutskiy 	 * needs to be fixed. */
4801e0da3cbSArtem B. Bityutskiy 	buf = kmalloc(len, GFP_KERNEL);
4811e0da3cbSArtem B. Bityutskiy 	if (!buf)
4821e0da3cbSArtem B. Bityutskiy 		return -ENOMEM;
4831e0da3cbSArtem B. Bityutskiy 
484f97117d1SArtem B. Bityutskiy 	spin_lock(&c->erase_completion_lock);
485f97117d1SArtem B. Bityutskiy 	valid_ref = jffs2_first_valid_node(f->inocache->nodes);
4861e0da3cbSArtem B. Bityutskiy 	if (!valid_ref && f->inocache->ino != 1)
4871e0da3cbSArtem B. Bityutskiy 		JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
488f97117d1SArtem B. Bityutskiy 	while (valid_ref) {
4891e0da3cbSArtem B. Bityutskiy 		unsigned char *bufstart;
4901e0da3cbSArtem B. Bityutskiy 
491f97117d1SArtem B. Bityutskiy 		/* We can hold a pointer to a non-obsolete node without the spinlock,
492f97117d1SArtem B. Bityutskiy 		   but _obsolete_ nodes may disappear at any time, if the block
493f97117d1SArtem B. Bityutskiy 		   they're in gets erased. So if we mark 'ref' obsolete while we're
494f97117d1SArtem B. Bityutskiy 		   not holding the lock, it can go away immediately. For that reason,
495f97117d1SArtem B. Bityutskiy 		   we find the next valid node first, before processing 'ref'.
496f97117d1SArtem B. Bityutskiy 		*/
497f97117d1SArtem B. Bityutskiy 		ref = valid_ref;
498f97117d1SArtem B. Bityutskiy 		valid_ref = jffs2_first_valid_node(ref->next_in_ino);
499f97117d1SArtem B. Bityutskiy 		spin_unlock(&c->erase_completion_lock);
500f97117d1SArtem B. Bityutskiy 
501f97117d1SArtem B. Bityutskiy 		cond_resched();
502f97117d1SArtem B. Bityutskiy 
5031e0da3cbSArtem B. Bityutskiy 		/*
5041e0da3cbSArtem B. Bityutskiy 		 * At this point we don't know the type of the node we're going
5051e0da3cbSArtem B. Bityutskiy 		 * to read, so we do not know the size of its header. In order
5061e0da3cbSArtem B. Bityutskiy 		 * to minimize the amount of flash IO we assume the node has
5071e0da3cbSArtem B. Bityutskiy 		 * size = JFFS2_MIN_NODE_HEADER.
5081e0da3cbSArtem B. Bityutskiy 		 */
5091e0da3cbSArtem B. Bityutskiy 		if (jffs2_is_writebuffered(c)) {
5101e0da3cbSArtem B. Bityutskiy 			/*
5111e0da3cbSArtem B. Bityutskiy 			 * We treat 'buf' as 2 adjacent wbufs. We want to
5121e0da3cbSArtem B. Bityutskiy 			 * adjust bufstart such as it points to the
5131e0da3cbSArtem B. Bityutskiy 			 * beginning of the node within this wbuf.
5141e0da3cbSArtem B. Bityutskiy 			 */
5151e0da3cbSArtem B. Bityutskiy 			bufstart = buf + (ref_offset(ref) % c->wbuf_pagesize);
5161e0da3cbSArtem B. Bityutskiy 			/* We will read either one wbuf or 2 wbufs. */
5171e0da3cbSArtem B. Bityutskiy 			len = c->wbuf_pagesize - (bufstart - buf);
51839243508SArtem B. Bityutskiy 			if (JFFS2_MIN_NODE_HEADER + (int)(bufstart - buf) > c->wbuf_pagesize) {
51939243508SArtem B. Bityutskiy 				/* The header spans the border of the first wbuf */
5201e0da3cbSArtem B. Bityutskiy 				len += c->wbuf_pagesize;
5211e0da3cbSArtem B. Bityutskiy 			}
5221e0da3cbSArtem B. Bityutskiy 		} else {
5231e0da3cbSArtem B. Bityutskiy 			bufstart = buf;
5241e0da3cbSArtem B. Bityutskiy 			len = JFFS2_MIN_NODE_HEADER;
5251e0da3cbSArtem B. Bityutskiy 		}
5261e0da3cbSArtem B. Bityutskiy 
527733802d9SArtem B. Bityutskiy 		dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
5281e0da3cbSArtem B. Bityutskiy 
529f97117d1SArtem B. Bityutskiy 		/* FIXME: point() */
5301e0da3cbSArtem B. Bityutskiy 		err = jffs2_flash_read(c, ref_offset(ref), len,
5311e0da3cbSArtem B. Bityutskiy 				       &retlen, bufstart);
532f97117d1SArtem B. Bityutskiy 		if (err) {
5331e0da3cbSArtem B. Bityutskiy 			JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err);
534f97117d1SArtem B. Bityutskiy 			goto free_out;
535f97117d1SArtem B. Bityutskiy 		}
536f97117d1SArtem B. Bityutskiy 
5371e0da3cbSArtem B. Bityutskiy 		if (retlen < len) {
538fb6a82c9SRandy Dunlap 			JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", ref_offset(ref), retlen, len);
539f97117d1SArtem B. Bityutskiy 			err = -EIO;
540f97117d1SArtem B. Bityutskiy 			goto free_out;
541f97117d1SArtem B. Bityutskiy 		}
542f97117d1SArtem B. Bityutskiy 
5431e0da3cbSArtem B. Bityutskiy 		node = (union jffs2_node_union *)bufstart;
5441e0da3cbSArtem B. Bityutskiy 
5453877f0b6SDavid Woodhouse 		/* No need to mask in the valid bit; it shouldn't be invalid */
5463877f0b6SDavid Woodhouse 		if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
5473877f0b6SDavid Woodhouse 			JFFS2_NOTICE("Node header CRC failed at %#08x. {%04x,%04x,%08x,%08x}\n",
5483877f0b6SDavid Woodhouse 				     ref_offset(ref), je16_to_cpu(node->u.magic),
5493877f0b6SDavid Woodhouse 				     je16_to_cpu(node->u.nodetype),
5503877f0b6SDavid Woodhouse 				     je32_to_cpu(node->u.totlen),
5513877f0b6SDavid Woodhouse 				     je32_to_cpu(node->u.hdr_crc));
5523877f0b6SDavid Woodhouse 			jffs2_dbg_dump_node(c, ref_offset(ref));
5533877f0b6SDavid Woodhouse 			jffs2_mark_node_obsolete(c, ref);
5543877f0b6SDavid Woodhouse 			goto cont;
5553877f0b6SDavid Woodhouse 		}
5563877f0b6SDavid Woodhouse 
5571e0da3cbSArtem B. Bityutskiy 		switch (je16_to_cpu(node->u.nodetype)) {
5581e0da3cbSArtem B. Bityutskiy 
5591e0da3cbSArtem B. Bityutskiy 		case JFFS2_NODETYPE_DIRENT:
5601e0da3cbSArtem B. Bityutskiy 
5611e0da3cbSArtem B. Bityutskiy 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) {
5621e0da3cbSArtem B. Bityutskiy 				err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf, bufstart);
5631e0da3cbSArtem B. Bityutskiy 				if (unlikely(err))
5641e0da3cbSArtem B. Bityutskiy 					goto free_out;
5651e0da3cbSArtem B. Bityutskiy 			}
5661e0da3cbSArtem B. Bityutskiy 
5671e0da3cbSArtem B. Bityutskiy 			err = read_direntry(c, ref, &node->d, retlen, &ret_fd, latest_mctime, mctime_ver);
568f97117d1SArtem B. Bityutskiy 			if (err == 1) {
569f97117d1SArtem B. Bityutskiy 				jffs2_mark_node_obsolete(c, ref);
570f97117d1SArtem B. Bityutskiy 				break;
571f97117d1SArtem B. Bityutskiy 			} else if (unlikely(err))
572f97117d1SArtem B. Bityutskiy 				goto free_out;
573f97117d1SArtem B. Bityutskiy 
5741e0da3cbSArtem B. Bityutskiy 			if (je32_to_cpu(node->d.version) > *highest_version)
5751e0da3cbSArtem B. Bityutskiy 				*highest_version = je32_to_cpu(node->d.version);
576f97117d1SArtem B. Bityutskiy 
577f97117d1SArtem B. Bityutskiy 			break;
578f97117d1SArtem B. Bityutskiy 
579f97117d1SArtem B. Bityutskiy 		case JFFS2_NODETYPE_INODE:
580f97117d1SArtem B. Bityutskiy 
5811e0da3cbSArtem B. Bityutskiy 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) {
5821e0da3cbSArtem B. Bityutskiy 				err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf, bufstart);
5831e0da3cbSArtem B. Bityutskiy 				if (unlikely(err))
584f97117d1SArtem B. Bityutskiy 					goto free_out;
585f97117d1SArtem B. Bityutskiy 			}
586f97117d1SArtem B. Bityutskiy 
5871e0da3cbSArtem B. Bityutskiy 			err = read_dnode(c, ref, &node->i, &ret_tn, len, latest_mctime, mctime_ver);
588f97117d1SArtem B. Bityutskiy 			if (err == 1) {
589f97117d1SArtem B. Bityutskiy 				jffs2_mark_node_obsolete(c, ref);
590f97117d1SArtem B. Bityutskiy 				break;
591f97117d1SArtem B. Bityutskiy 			} else if (unlikely(err))
592f97117d1SArtem B. Bityutskiy 				goto free_out;
593f97117d1SArtem B. Bityutskiy 
5941e0da3cbSArtem B. Bityutskiy 			if (je32_to_cpu(node->i.version) > *highest_version)
5951e0da3cbSArtem B. Bityutskiy 				*highest_version = je32_to_cpu(node->i.version);
596f97117d1SArtem B. Bityutskiy 
5971da177e4SLinus Torvalds 			break;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 		default:
6001e0da3cbSArtem B. Bityutskiy 			if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) {
6011e0da3cbSArtem B. Bityutskiy 				err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf, bufstart);
6021e0da3cbSArtem B. Bityutskiy 				if (unlikely(err))
6031e0da3cbSArtem B. Bityutskiy 					goto free_out;
6041da177e4SLinus Torvalds 			}
6051da177e4SLinus Torvalds 
6061e0da3cbSArtem B. Bityutskiy 			err = read_unknown(c, ref, &node->u);
607f97117d1SArtem B. Bityutskiy 			if (err == 1) {
608f97117d1SArtem B. Bityutskiy 				jffs2_mark_node_obsolete(c, ref);
609f97117d1SArtem B. Bityutskiy 				break;
610f97117d1SArtem B. Bityutskiy 			} else if (unlikely(err))
611f97117d1SArtem B. Bityutskiy 				goto free_out;
612f97117d1SArtem B. Bityutskiy 
6131da177e4SLinus Torvalds 		}
6143877f0b6SDavid Woodhouse 	cont:
615f97117d1SArtem B. Bityutskiy 		spin_lock(&c->erase_completion_lock);
6161da177e4SLinus Torvalds 	}
6171e0da3cbSArtem B. Bityutskiy 
618f97117d1SArtem B. Bityutskiy 	spin_unlock(&c->erase_completion_lock);
619f97117d1SArtem B. Bityutskiy 	*tnp = ret_tn;
620f97117d1SArtem B. Bityutskiy 	*fdp = ret_fd;
6211e0da3cbSArtem B. Bityutskiy 	kfree(buf);
622f97117d1SArtem B. Bityutskiy 
623733802d9SArtem B. Bityutskiy 	dbg_readinode("nodes of inode #%u were read, the highest version is %u, latest_mctime %u, mctime_ver %u.\n",
6241e0da3cbSArtem B. Bityutskiy 			f->inocache->ino, *highest_version, *latest_mctime, *mctime_ver);
625f97117d1SArtem B. Bityutskiy 	return 0;
626f97117d1SArtem B. Bityutskiy 
627f97117d1SArtem B. Bityutskiy  free_out:
628f97117d1SArtem B. Bityutskiy 	jffs2_free_tmp_dnode_info_list(&ret_tn);
629f97117d1SArtem B. Bityutskiy 	jffs2_free_full_dirent_list(ret_fd);
6301e0da3cbSArtem B. Bityutskiy 	kfree(buf);
631f97117d1SArtem B. Bityutskiy 	return err;
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds static int jffs2_do_read_inode_internal(struct jffs2_sb_info *c,
6351da177e4SLinus Torvalds 					struct jffs2_inode_info *f,
6361da177e4SLinus Torvalds 					struct jffs2_raw_inode *latest_node)
6371da177e4SLinus Torvalds {
6381e0da3cbSArtem B. Bityutskiy 	struct jffs2_tmp_dnode_info *tn;
6399dee7503SDavid Woodhouse 	struct rb_root tn_list;
6409dee7503SDavid Woodhouse 	struct rb_node *rb, *repl_rb;
6411da177e4SLinus Torvalds 	struct jffs2_full_dirent *fd_list;
6421e0da3cbSArtem B. Bityutskiy 	struct jffs2_full_dnode *fn, *first_fn = NULL;
6431da177e4SLinus Torvalds 	uint32_t crc;
6441da177e4SLinus Torvalds 	uint32_t latest_mctime, mctime_ver;
6451da177e4SLinus Torvalds 	size_t retlen;
6461da177e4SLinus Torvalds 	int ret;
6471da177e4SLinus Torvalds 
648733802d9SArtem B. Bityutskiy 	dbg_readinode("ino #%u nlink is %d\n", f->inocache->ino, f->inocache->nlink);
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 	/* Grab all nodes relevant to this ino */
6511da177e4SLinus Torvalds 	ret = jffs2_get_inode_nodes(c, f, &tn_list, &fd_list, &f->highest_version, &latest_mctime, &mctime_ver);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (ret) {
654e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("cannot read nodes for ino %u, returned error is %d\n", f->inocache->ino, ret);
6551da177e4SLinus Torvalds 		if (f->inocache->state == INO_STATE_READING)
6561da177e4SLinus Torvalds 			jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
6571da177e4SLinus Torvalds 		return ret;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 	f->dents = fd_list;
6601da177e4SLinus Torvalds 
6619dee7503SDavid Woodhouse 	rb = rb_first(&tn_list);
6621da177e4SLinus Torvalds 
6639dee7503SDavid Woodhouse 	while (rb) {
6641e0da3cbSArtem B. Bityutskiy 		cond_resched();
6659dee7503SDavid Woodhouse 		tn = rb_entry(rb, struct jffs2_tmp_dnode_info, rb);
6661da177e4SLinus Torvalds 		fn = tn->fn;
6671e0da3cbSArtem B. Bityutskiy 		ret = 1;
668733802d9SArtem B. Bityutskiy 		dbg_readinode("consider node ver %u, phys offset "
6691e0da3cbSArtem B. Bityutskiy 			"%#08x(%d), range %u-%u.\n", tn->version,
6701e0da3cbSArtem B. Bityutskiy 			ref_offset(fn->raw), ref_flags(fn->raw),
6711e0da3cbSArtem B. Bityutskiy 			fn->ofs, fn->ofs + fn->size);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 		if (fn->size) {
6741e0da3cbSArtem B. Bityutskiy 			ret = jffs2_add_older_frag_to_fragtree(c, f, tn);
6751e0da3cbSArtem B. Bityutskiy 			/* TODO: the error code isn't checked, check it */
6761e0da3cbSArtem B. Bityutskiy 			jffs2_dbg_fragtree_paranoia_check_nolock(f);
6771e0da3cbSArtem B. Bityutskiy 			BUG_ON(ret < 0);
6781e0da3cbSArtem B. Bityutskiy 			if (!first_fn && ret == 0)
6791e0da3cbSArtem B. Bityutskiy 				first_fn = fn;
6801e0da3cbSArtem B. Bityutskiy 		} else if (!first_fn) {
6811e0da3cbSArtem B. Bityutskiy 			first_fn = fn;
6821da177e4SLinus Torvalds 			f->metadata = fn;
6831e0da3cbSArtem B. Bityutskiy 			ret = 0; /* Prevent freeing the metadata update node */
6841e0da3cbSArtem B. Bityutskiy 		} else
6851e0da3cbSArtem B. Bityutskiy 			jffs2_mark_node_obsolete(c, fn->raw);
6861e0da3cbSArtem B. Bityutskiy 
6879dee7503SDavid Woodhouse 		BUG_ON(rb->rb_left);
6889dee7503SDavid Woodhouse 		if (rb->rb_parent && rb->rb_parent->rb_left == rb) {
6899dee7503SDavid Woodhouse 			/* We were then left-hand child of our parent. We need
6901e0da3cbSArtem B. Bityutskiy 			 * to move our own right-hand child into our place. */
6919dee7503SDavid Woodhouse 			repl_rb = rb->rb_right;
6929dee7503SDavid Woodhouse 			if (repl_rb)
6939dee7503SDavid Woodhouse 				repl_rb->rb_parent = rb->rb_parent;
6949dee7503SDavid Woodhouse 		} else
6959dee7503SDavid Woodhouse 			repl_rb = NULL;
6969dee7503SDavid Woodhouse 
6979dee7503SDavid Woodhouse 		rb = rb_next(rb);
6989dee7503SDavid Woodhouse 
6999dee7503SDavid Woodhouse 		/* Remove the spent tn from the tree; don't bother rebalancing
7001e0da3cbSArtem B. Bityutskiy 		 * but put our right-hand child in our own place. */
7019dee7503SDavid Woodhouse 		if (tn->rb.rb_parent) {
7029dee7503SDavid Woodhouse 			if (tn->rb.rb_parent->rb_left == &tn->rb)
7039dee7503SDavid Woodhouse 				tn->rb.rb_parent->rb_left = repl_rb;
7049dee7503SDavid Woodhouse 			else if (tn->rb.rb_parent->rb_right == &tn->rb)
7059dee7503SDavid Woodhouse 				tn->rb.rb_parent->rb_right = repl_rb;
7069dee7503SDavid Woodhouse 			else BUG();
7079dee7503SDavid Woodhouse 		} else if (tn->rb.rb_right)
7089dee7503SDavid Woodhouse 			tn->rb.rb_right->rb_parent = NULL;
7099dee7503SDavid Woodhouse 
7101da177e4SLinus Torvalds 		jffs2_free_tmp_dnode_info(tn);
7111e0da3cbSArtem B. Bityutskiy 		if (ret) {
712733802d9SArtem B. Bityutskiy 			dbg_readinode("delete dnode %u-%u.\n",
7131e0da3cbSArtem B. Bityutskiy 				fn->ofs, fn->ofs + fn->size);
7141e0da3cbSArtem B. Bityutskiy 			jffs2_free_full_dnode(fn);
7151e0da3cbSArtem B. Bityutskiy 		}
7161da177e4SLinus Torvalds 	}
717e0c8e42fSArtem B. Bityutskiy 	jffs2_dbg_fragtree_paranoia_check_nolock(f);
7181da177e4SLinus Torvalds 
7191e0da3cbSArtem B. Bityutskiy 	BUG_ON(first_fn && ref_obsolete(first_fn->raw));
7201e0da3cbSArtem B. Bityutskiy 
7211e0da3cbSArtem B. Bityutskiy 	fn = first_fn;
7221e0da3cbSArtem B. Bityutskiy 	if (unlikely(!first_fn)) {
7231da177e4SLinus Torvalds 		/* No data nodes for this inode. */
7241da177e4SLinus Torvalds 		if (f->inocache->ino != 1) {
725e0d60137SArtem B. Bityutskiy 			JFFS2_WARNING("no data nodes found for ino #%u\n", f->inocache->ino);
7261da177e4SLinus Torvalds 			if (!fd_list) {
7271da177e4SLinus Torvalds 				if (f->inocache->state == INO_STATE_READING)
7281da177e4SLinus Torvalds 					jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
7291da177e4SLinus Torvalds 				return -EIO;
7301da177e4SLinus Torvalds 			}
731e0d60137SArtem B. Bityutskiy 			JFFS2_NOTICE("but it has children so we fake some modes for it\n");
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 		latest_node->mode = cpu_to_jemode(S_IFDIR|S_IRUGO|S_IWUSR|S_IXUGO);
7341da177e4SLinus Torvalds 		latest_node->version = cpu_to_je32(0);
7351da177e4SLinus Torvalds 		latest_node->atime = latest_node->ctime = latest_node->mtime = cpu_to_je32(0);
7361da177e4SLinus Torvalds 		latest_node->isize = cpu_to_je32(0);
7371da177e4SLinus Torvalds 		latest_node->gid = cpu_to_je16(0);
7381da177e4SLinus Torvalds 		latest_node->uid = cpu_to_je16(0);
7391da177e4SLinus Torvalds 		if (f->inocache->state == INO_STATE_READING)
7401da177e4SLinus Torvalds 			jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
7411da177e4SLinus Torvalds 		return 0;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	ret = jffs2_flash_read(c, ref_offset(fn->raw), sizeof(*latest_node), &retlen, (void *)latest_node);
7451da177e4SLinus Torvalds 	if (ret || retlen != sizeof(*latest_node)) {
746e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("failed to read from flash: error %d, %zd of %zd bytes read\n",
7471da177e4SLinus Torvalds 			ret, retlen, sizeof(*latest_node));
7481da177e4SLinus Torvalds 		/* FIXME: If this fails, there seems to be a memory leak. Find it. */
7491da177e4SLinus Torvalds 		up(&f->sem);
7501da177e4SLinus Torvalds 		jffs2_do_clear_inode(c, f);
7511da177e4SLinus Torvalds 		return ret?ret:-EIO;
7521da177e4SLinus Torvalds 	}
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	crc = crc32(0, latest_node, sizeof(*latest_node)-8);
7551da177e4SLinus Torvalds 	if (crc != je32_to_cpu(latest_node->node_crc)) {
756e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("CRC failed for read_inode of inode %u at physical location 0x%x\n",
757e0d60137SArtem B. Bityutskiy 			f->inocache->ino, ref_offset(fn->raw));
7581da177e4SLinus Torvalds 		up(&f->sem);
7591da177e4SLinus Torvalds 		jffs2_do_clear_inode(c, f);
7601da177e4SLinus Torvalds 		return -EIO;
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	switch(jemode_to_cpu(latest_node->mode) & S_IFMT) {
7641da177e4SLinus Torvalds 	case S_IFDIR:
7651da177e4SLinus Torvalds 		if (mctime_ver > je32_to_cpu(latest_node->version)) {
7661da177e4SLinus Torvalds 			/* The times in the latest_node are actually older than
7671da177e4SLinus Torvalds 			   mctime in the latest dirent. Cheat. */
7681da177e4SLinus Torvalds 			latest_node->ctime = latest_node->mtime = cpu_to_je32(latest_mctime);
7691da177e4SLinus Torvalds 		}
7701da177e4SLinus Torvalds 		break;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	case S_IFREG:
7741da177e4SLinus Torvalds 		/* If it was a regular file, truncate it to the latest node's isize */
775f302cd02SArtem B. Bityutskiy 		jffs2_truncate_fragtree(c, &f->fragtree, je32_to_cpu(latest_node->isize));
7761da177e4SLinus Torvalds 		break;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	case S_IFLNK:
7791da177e4SLinus Torvalds 		/* Hack to work around broken isize in old symlink code.
7801da177e4SLinus Torvalds 		   Remove this when dwmw2 comes to his senses and stops
7811da177e4SLinus Torvalds 		   symlinks from being an entirely gratuitous special
7821da177e4SLinus Torvalds 		   case. */
7831da177e4SLinus Torvalds 		if (!je32_to_cpu(latest_node->isize))
7841da177e4SLinus Torvalds 			latest_node->isize = latest_node->dsize;
78532f1a95dSArtem B. Bityuckiy 
78632f1a95dSArtem B. Bityuckiy 		if (f->inocache->state != INO_STATE_CHECKING) {
78732f1a95dSArtem B. Bityuckiy 			/* Symlink's inode data is the target path. Read it and
7882b79adccSArtem B. Bityutskiy 			 * keep in RAM to facilitate quick follow symlink
7892b79adccSArtem B. Bityutskiy 			 * operation. */
7902b79adccSArtem B. Bityutskiy 			f->target = kmalloc(je32_to_cpu(latest_node->csize) + 1, GFP_KERNEL);
7912b79adccSArtem B. Bityutskiy 			if (!f->target) {
792e0d60137SArtem B. Bityutskiy 				JFFS2_ERROR("can't allocate %d bytes of memory for the symlink target path cache\n", je32_to_cpu(latest_node->csize));
79332f1a95dSArtem B. Bityuckiy 				up(&f->sem);
79432f1a95dSArtem B. Bityuckiy 				jffs2_do_clear_inode(c, f);
79532f1a95dSArtem B. Bityuckiy 				return -ENOMEM;
79632f1a95dSArtem B. Bityuckiy 			}
79732f1a95dSArtem B. Bityuckiy 
79832f1a95dSArtem B. Bityuckiy 			ret = jffs2_flash_read(c, ref_offset(fn->raw) + sizeof(*latest_node),
7992b79adccSArtem B. Bityutskiy 						je32_to_cpu(latest_node->csize), &retlen, (char *)f->target);
80032f1a95dSArtem B. Bityuckiy 
80132f1a95dSArtem B. Bityuckiy 			if (ret  || retlen != je32_to_cpu(latest_node->csize)) {
80232f1a95dSArtem B. Bityuckiy 				if (retlen != je32_to_cpu(latest_node->csize))
80332f1a95dSArtem B. Bityuckiy 					ret = -EIO;
8042b79adccSArtem B. Bityutskiy 				kfree(f->target);
8052b79adccSArtem B. Bityutskiy 				f->target = NULL;
80632f1a95dSArtem B. Bityuckiy 				up(&f->sem);
80732f1a95dSArtem B. Bityuckiy 				jffs2_do_clear_inode(c, f);
80832f1a95dSArtem B. Bityuckiy 				return -ret;
80932f1a95dSArtem B. Bityuckiy 			}
81032f1a95dSArtem B. Bityuckiy 
8112b79adccSArtem B. Bityutskiy 			f->target[je32_to_cpu(latest_node->csize)] = '\0';
812733802d9SArtem B. Bityutskiy 			dbg_readinode("symlink's target '%s' cached\n", f->target);
81332f1a95dSArtem B. Bityuckiy 		}
81432f1a95dSArtem B. Bityuckiy 
8151da177e4SLinus Torvalds 		/* fall through... */
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	case S_IFBLK:
8181da177e4SLinus Torvalds 	case S_IFCHR:
8191da177e4SLinus Torvalds 		/* Certain inode types should have only one data node, and it's
8201da177e4SLinus Torvalds 		   kept as the metadata node */
8211da177e4SLinus Torvalds 		if (f->metadata) {
822e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("Argh. Special inode #%u with mode 0%o had metadata node\n",
8231da177e4SLinus Torvalds 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
8241da177e4SLinus Torvalds 			up(&f->sem);
8251da177e4SLinus Torvalds 			jffs2_do_clear_inode(c, f);
8261da177e4SLinus Torvalds 			return -EIO;
8271da177e4SLinus Torvalds 		}
8281da177e4SLinus Torvalds 		if (!frag_first(&f->fragtree)) {
829e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("Argh. Special inode #%u with mode 0%o has no fragments\n",
8301da177e4SLinus Torvalds 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
8311da177e4SLinus Torvalds 			up(&f->sem);
8321da177e4SLinus Torvalds 			jffs2_do_clear_inode(c, f);
8331da177e4SLinus Torvalds 			return -EIO;
8341da177e4SLinus Torvalds 		}
8351da177e4SLinus Torvalds 		/* ASSERT: f->fraglist != NULL */
8361da177e4SLinus Torvalds 		if (frag_next(frag_first(&f->fragtree))) {
837e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("Argh. Special inode #%u with mode 0x%x had more than one node\n",
8381da177e4SLinus Torvalds 			       f->inocache->ino, jemode_to_cpu(latest_node->mode));
8391da177e4SLinus Torvalds 			/* FIXME: Deal with it - check crc32, check for duplicate node, check times and discard the older one */
8401da177e4SLinus Torvalds 			up(&f->sem);
8411da177e4SLinus Torvalds 			jffs2_do_clear_inode(c, f);
8421da177e4SLinus Torvalds 			return -EIO;
8431da177e4SLinus Torvalds 		}
8441da177e4SLinus Torvalds 		/* OK. We're happy */
8451da177e4SLinus Torvalds 		f->metadata = frag_first(&f->fragtree)->node;
8461da177e4SLinus Torvalds 		jffs2_free_node_frag(frag_first(&f->fragtree));
8471da177e4SLinus Torvalds 		f->fragtree = RB_ROOT;
8481da177e4SLinus Torvalds 		break;
8491da177e4SLinus Torvalds 	}
8501da177e4SLinus Torvalds 	if (f->inocache->state == INO_STATE_READING)
8511da177e4SLinus Torvalds 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_PRESENT);
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds 	return 0;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
856f97117d1SArtem B. Bityutskiy /* Scan the list of all nodes present for this ino, build map of versions, etc. */
857f97117d1SArtem B. Bityutskiy int jffs2_do_read_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
858f97117d1SArtem B. Bityutskiy 			uint32_t ino, struct jffs2_raw_inode *latest_node)
859f97117d1SArtem B. Bityutskiy {
860733802d9SArtem B. Bityutskiy 	dbg_readinode("read inode #%u\n", ino);
861f97117d1SArtem B. Bityutskiy 
862f97117d1SArtem B. Bityutskiy  retry_inocache:
863f97117d1SArtem B. Bityutskiy 	spin_lock(&c->inocache_lock);
864f97117d1SArtem B. Bityutskiy 	f->inocache = jffs2_get_ino_cache(c, ino);
865f97117d1SArtem B. Bityutskiy 
866f97117d1SArtem B. Bityutskiy 	if (f->inocache) {
867f97117d1SArtem B. Bityutskiy 		/* Check its state. We may need to wait before we can use it */
868f97117d1SArtem B. Bityutskiy 		switch(f->inocache->state) {
869f97117d1SArtem B. Bityutskiy 		case INO_STATE_UNCHECKED:
870f97117d1SArtem B. Bityutskiy 		case INO_STATE_CHECKEDABSENT:
871f97117d1SArtem B. Bityutskiy 			f->inocache->state = INO_STATE_READING;
872f97117d1SArtem B. Bityutskiy 			break;
873f97117d1SArtem B. Bityutskiy 
874f97117d1SArtem B. Bityutskiy 		case INO_STATE_CHECKING:
875f97117d1SArtem B. Bityutskiy 		case INO_STATE_GC:
876f97117d1SArtem B. Bityutskiy 			/* If it's in either of these states, we need
877f97117d1SArtem B. Bityutskiy 			   to wait for whoever's got it to finish and
878f97117d1SArtem B. Bityutskiy 			   put it back. */
879733802d9SArtem B. Bityutskiy 			dbg_readinode("waiting for ino #%u in state %d\n", ino, f->inocache->state);
880f97117d1SArtem B. Bityutskiy 			sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
881f97117d1SArtem B. Bityutskiy 			goto retry_inocache;
882f97117d1SArtem B. Bityutskiy 
883f97117d1SArtem B. Bityutskiy 		case INO_STATE_READING:
884f97117d1SArtem B. Bityutskiy 		case INO_STATE_PRESENT:
885f97117d1SArtem B. Bityutskiy 			/* Eep. This should never happen. It can
886f97117d1SArtem B. Bityutskiy 			happen if Linux calls read_inode() again
887f97117d1SArtem B. Bityutskiy 			before clear_inode() has finished though. */
888e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("Eep. Trying to read_inode #%u when it's already in state %d!\n", ino, f->inocache->state);
889f97117d1SArtem B. Bityutskiy 			/* Fail. That's probably better than allowing it to succeed */
890f97117d1SArtem B. Bityutskiy 			f->inocache = NULL;
891f97117d1SArtem B. Bityutskiy 			break;
892f97117d1SArtem B. Bityutskiy 
893f97117d1SArtem B. Bityutskiy 		default:
894f97117d1SArtem B. Bityutskiy 			BUG();
895f97117d1SArtem B. Bityutskiy 		}
896f97117d1SArtem B. Bityutskiy 	}
897f97117d1SArtem B. Bityutskiy 	spin_unlock(&c->inocache_lock);
898f97117d1SArtem B. Bityutskiy 
899f97117d1SArtem B. Bityutskiy 	if (!f->inocache && ino == 1) {
900f97117d1SArtem B. Bityutskiy 		/* Special case - no root inode on medium */
901f97117d1SArtem B. Bityutskiy 		f->inocache = jffs2_alloc_inode_cache();
902f97117d1SArtem B. Bityutskiy 		if (!f->inocache) {
903e0d60137SArtem B. Bityutskiy 			JFFS2_ERROR("cannot allocate inocache for root inode\n");
904f97117d1SArtem B. Bityutskiy 			return -ENOMEM;
905f97117d1SArtem B. Bityutskiy 		}
906733802d9SArtem B. Bityutskiy 		dbg_readinode("creating inocache for root inode\n");
907f97117d1SArtem B. Bityutskiy 		memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
908f97117d1SArtem B. Bityutskiy 		f->inocache->ino = f->inocache->nlink = 1;
909f97117d1SArtem B. Bityutskiy 		f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
910f97117d1SArtem B. Bityutskiy 		f->inocache->state = INO_STATE_READING;
911f97117d1SArtem B. Bityutskiy 		jffs2_add_ino_cache(c, f->inocache);
912f97117d1SArtem B. Bityutskiy 	}
913f97117d1SArtem B. Bityutskiy 	if (!f->inocache) {
914e0d60137SArtem B. Bityutskiy 		JFFS2_ERROR("requestied to read an nonexistent ino %u\n", ino);
915f97117d1SArtem B. Bityutskiy 		return -ENOENT;
916f97117d1SArtem B. Bityutskiy 	}
917f97117d1SArtem B. Bityutskiy 
918f97117d1SArtem B. Bityutskiy 	return jffs2_do_read_inode_internal(c, f, latest_node);
919f97117d1SArtem B. Bityutskiy }
920f97117d1SArtem B. Bityutskiy 
921f97117d1SArtem B. Bityutskiy int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
922f97117d1SArtem B. Bityutskiy {
923f97117d1SArtem B. Bityutskiy 	struct jffs2_raw_inode n;
924f97117d1SArtem B. Bityutskiy 	struct jffs2_inode_info *f = kmalloc(sizeof(*f), GFP_KERNEL);
925f97117d1SArtem B. Bityutskiy 	int ret;
926f97117d1SArtem B. Bityutskiy 
927f97117d1SArtem B. Bityutskiy 	if (!f)
928f97117d1SArtem B. Bityutskiy 		return -ENOMEM;
929f97117d1SArtem B. Bityutskiy 
930f97117d1SArtem B. Bityutskiy 	memset(f, 0, sizeof(*f));
931f97117d1SArtem B. Bityutskiy 	init_MUTEX_LOCKED(&f->sem);
932f97117d1SArtem B. Bityutskiy 	f->inocache = ic;
933f97117d1SArtem B. Bityutskiy 
934f97117d1SArtem B. Bityutskiy 	ret = jffs2_do_read_inode_internal(c, f, &n);
935f97117d1SArtem B. Bityutskiy 	if (!ret) {
936f97117d1SArtem B. Bityutskiy 		up(&f->sem);
937f97117d1SArtem B. Bityutskiy 		jffs2_do_clear_inode(c, f);
938f97117d1SArtem B. Bityutskiy 	}
939f97117d1SArtem B. Bityutskiy 	kfree (f);
940f97117d1SArtem B. Bityutskiy 	return ret;
941f97117d1SArtem B. Bityutskiy }
942f97117d1SArtem B. Bityutskiy 
9431da177e4SLinus Torvalds void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	struct jffs2_full_dirent *fd, *fds;
9461da177e4SLinus Torvalds 	int deleted;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	down(&f->sem);
9491da177e4SLinus Torvalds 	deleted = f->inocache && !f->inocache->nlink;
9501da177e4SLinus Torvalds 
95167e345d1SDavid Woodhouse 	if (f->inocache && f->inocache->state != INO_STATE_CHECKING)
95267e345d1SDavid Woodhouse 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_CLEARING);
95367e345d1SDavid Woodhouse 
9541da177e4SLinus Torvalds 	if (f->metadata) {
9551da177e4SLinus Torvalds 		if (deleted)
9561da177e4SLinus Torvalds 			jffs2_mark_node_obsolete(c, f->metadata->raw);
9571da177e4SLinus Torvalds 		jffs2_free_full_dnode(f->metadata);
9581da177e4SLinus Torvalds 	}
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	jffs2_kill_fragtree(&f->fragtree, deleted?c:NULL);
9611da177e4SLinus Torvalds 
9622b79adccSArtem B. Bityutskiy 	if (f->target) {
9632b79adccSArtem B. Bityutskiy 		kfree(f->target);
9642b79adccSArtem B. Bityutskiy 		f->target = NULL;
96532f1a95dSArtem B. Bityuckiy 	}
9661da177e4SLinus Torvalds 
9672b79adccSArtem B. Bityutskiy 	fds = f->dents;
9681da177e4SLinus Torvalds 	while(fds) {
9691da177e4SLinus Torvalds 		fd = fds;
9701da177e4SLinus Torvalds 		fds = fd->next;
9711da177e4SLinus Torvalds 		jffs2_free_full_dirent(fd);
9721da177e4SLinus Torvalds 	}
9731da177e4SLinus Torvalds 
97467e345d1SDavid Woodhouse 	if (f->inocache && f->inocache->state != INO_STATE_CHECKING) {
9751da177e4SLinus Torvalds 		jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT);
97667e345d1SDavid Woodhouse 		if (f->inocache->nodes == (void *)f->inocache)
97767e345d1SDavid Woodhouse 			jffs2_del_ino_cache(c, f->inocache);
97867e345d1SDavid Woodhouse 	}
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	up(&f->sem);
9811da177e4SLinus Torvalds }
982