11558Srgrimes/*
21558Srgrimes * JFFS2 -- Journalling Flash File System, Version 2.
31558Srgrimes *
41558Srgrimes * Copyright �� 2001-2007 Red Hat, Inc.
51558Srgrimes *
61558Srgrimes * Created by David Woodhouse <dwmw2@infradead.org>
71558Srgrimes *
81558Srgrimes * For licensing information, see the file 'LICENCE' in this directory.
91558Srgrimes *
101558Srgrimes */
111558Srgrimes
121558Srgrimes#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
131558Srgrimes
141558Srgrimes#include <linux/kernel.h>
151558Srgrimes#include <linux/slab.h>
161558Srgrimes#include <linux/crc32.h>
171558Srgrimes#include <linux/pagemap.h>
181558Srgrimes#include <linux/mtd/mtd.h>
191558Srgrimes#include <linux/compiler.h>
201558Srgrimes#include "nodelist.h"
211558Srgrimes#include "compr.h"
221558Srgrimes
231558Srgrimesint jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
241558Srgrimes		     struct jffs2_full_dnode *fd, unsigned char *buf,
251558Srgrimes		     int ofs, int len)
261558Srgrimes{
271558Srgrimes	struct jffs2_raw_inode *ri;
281558Srgrimes	size_t readlen;
291558Srgrimes	uint32_t crc;
301558Srgrimes	unsigned char *decomprbuf = NULL;
311558Srgrimes	unsigned char *readbuf = NULL;
321558Srgrimes	int ret = 0;
331558Srgrimes
341558Srgrimes	ri = jffs2_alloc_raw_inode();
3536997Scharnier	if (!ri)
361558Srgrimes		return -ENOMEM;
371558Srgrimes
381558Srgrimes	ret = jffs2_flash_read(c, ref_offset(fd->raw), sizeof(*ri), &readlen, (char *)ri);
391558Srgrimes	if (ret) {
401558Srgrimes		jffs2_free_raw_inode(ri);
4136997Scharnier		pr_warn("Error reading node from 0x%08x: %d\n",
4223672Speter			ref_offset(fd->raw), ret);
4336997Scharnier		return ret;
4436997Scharnier	}
4550476Speter	if (readlen != sizeof(*ri)) {
461558Srgrimes		jffs2_free_raw_inode(ri);
471558Srgrimes		pr_warn("Short read from 0x%08x: wanted 0x%zx bytes, got 0x%zx\n",
481558Srgrimes			ref_offset(fd->raw), sizeof(*ri), readlen);
4958928Sjoerg		return -EIO;
501558Srgrimes	}
5196478Sphk	crc = crc32(0, ri, sizeof(*ri)-8);
521558Srgrimes
5323672Speter	jffs2_dbg(1, "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n",
541558Srgrimes		  ref_offset(fd->raw), je32_to_cpu(ri->node_crc),
551558Srgrimes		  crc, je32_to_cpu(ri->dsize), je32_to_cpu(ri->csize),
561558Srgrimes		  je32_to_cpu(ri->offset), buf);
571558Srgrimes	if (crc != je32_to_cpu(ri->node_crc)) {
581558Srgrimes		pr_warn("Node CRC %08x != calculated CRC %08x for node at %08x\n",
5923672Speter			je32_to_cpu(ri->node_crc), crc, ref_offset(fd->raw));
601558Srgrimes		ret = -EIO;
611558Srgrimes		goto out_ri;
6299562Siedowse	}
63103949Smike	/* There was a bug where we wrote hole nodes out with csize/dsize
641558Srgrimes	   swapped. Deal with it */
651558Srgrimes	if (ri->compr == JFFS2_COMPR_ZERO && !je32_to_cpu(ri->dsize) &&
661558Srgrimes	    je32_to_cpu(ri->csize)) {
671558Srgrimes		ri->dsize = ri->csize;
6899562Siedowse		ri->csize = cpu_to_je32(0);
691558Srgrimes	}
701558Srgrimes
711558Srgrimes	D1(if(ofs + len > je32_to_cpu(ri->dsize)) {
721558Srgrimes			pr_warn("jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n",
731558Srgrimes				len, ofs, je32_to_cpu(ri->dsize));
741558Srgrimes		ret = -EINVAL;
751558Srgrimes		goto out_ri;
761558Srgrimes	});
7712377Sjoerg
781558Srgrimes
791558Srgrimes	if (ri->compr == JFFS2_COMPR_ZERO) {
8025288Swollman		memset(buf, 0, len);
811558Srgrimes		goto out_ri;
821558Srgrimes	}
831558Srgrimes
841558Srgrimes	/* Cases:
8598542Smckusick	   Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
8698542Smckusick	   Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided
8798542Smckusick	   Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy
8898542Smckusick	   Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
8998542Smckusick	*/
9092837Simp	if (ri->compr == JFFS2_COMPR_NONE && len == je32_to_cpu(ri->dsize)) {
9192837Simp		readbuf = buf;
9292837Simp	} else {
931558Srgrimes		readbuf = kmalloc(je32_to_cpu(ri->csize), GFP_KERNEL);
941558Srgrimes		if (!readbuf) {
9592837Simp			ret = -ENOMEM;
961558Srgrimes			goto out_ri;
9786470Siedowse		}
9886473Siedowse	}
9986473Siedowse	if (ri->compr != JFFS2_COMPR_NONE) {
10098542Smckusick		if (len < je32_to_cpu(ri->dsize)) {
10186473Siedowse			decomprbuf = kmalloc(je32_to_cpu(ri->dsize), GFP_KERNEL);
10286473Siedowse			if (!decomprbuf) {
10398542Smckusick				ret = -ENOMEM;
1041558Srgrimes				goto out_readbuf;
10591540Siedowse			}
1061558Srgrimes		} else {
10785635Sdillon			decomprbuf = buf;
1081558Srgrimes		}
10998542Smckusick	} else {
1101558Srgrimes		decomprbuf = readbuf;
1111558Srgrimes	}
11221409Simp
11321409Simp	jffs2_dbg(2, "Read %d bytes to %p\n", je32_to_cpu(ri->csize),
1141558Srgrimes		  readbuf);
1151558Srgrimes	ret = jffs2_flash_read(c, (ref_offset(fd->raw)) + sizeof(*ri),
1161558Srgrimes			       je32_to_cpu(ri->csize), &readlen, readbuf);
1171558Srgrimes
1181558Srgrimes	if (!ret && readlen != je32_to_cpu(ri->csize))
1191558Srgrimes		ret = -EIO;
12023672Speter	if (ret)
12123672Speter		goto out_decomprbuf;
1221558Srgrimes
12323672Speter	crc = crc32(0, readbuf, je32_to_cpu(ri->csize));
12425288Swollman	if (crc != je32_to_cpu(ri->data_crc)) {
12591540Siedowse		pr_warn("Data CRC %08x != calculated CRC %08x for node at %08x\n",
12625288Swollman			je32_to_cpu(ri->data_crc), crc, ref_offset(fd->raw));
12791540Siedowse		ret = -EIO;
12825288Swollman		goto out_decomprbuf;
12925288Swollman	}
13025288Swollman	jffs2_dbg(2, "Data CRC matches calculated CRC %08x\n", crc);
13123672Speter	if (ri->compr != JFFS2_COMPR_NONE) {
13223672Speter		jffs2_dbg(2, "Decompress %d bytes from %p to %d bytes at %p\n",
13323672Speter			  je32_to_cpu(ri->csize), readbuf,
13423672Speter			  je32_to_cpu(ri->dsize), decomprbuf);
13523672Speter		ret = jffs2_decompress(c, f, ri->compr | (ri->usercompr << 8), readbuf, decomprbuf, je32_to_cpu(ri->csize), je32_to_cpu(ri->dsize));
1361558Srgrimes		if (ret) {
1371558Srgrimes			pr_warn("Error: jffs2_decompress returned %d\n", ret);
13823672Speter			goto out_decomprbuf;
13923672Speter		}
1401558Srgrimes	}
1411558Srgrimes
14223672Speter	if (len < je32_to_cpu(ri->dsize)) {
14323672Speter		memcpy(buf, decomprbuf+ofs, len);
14423672Speter	}
1451558Srgrimes out_decomprbuf:
1461558Srgrimes	if(decomprbuf != buf && decomprbuf != readbuf)
1471558Srgrimes		kfree(decomprbuf);
14823672Speter out_readbuf:
14923672Speter	if(readbuf != buf)
1501558Srgrimes		kfree(readbuf);
1511558Srgrimes out_ri:
1521558Srgrimes	jffs2_free_raw_inode(ri);
1531558Srgrimes
1541558Srgrimes	return ret;
1551558Srgrimes}
15623672Speter
15723672Speterint jffs2_read_inode_range(struct jffs2_sb_info *c, struct jffs2_inode_info *f,
15823672Speter			   unsigned char *buf, uint32_t offset, uint32_t len)
15923672Speter{
16022192Sjoerg	uint32_t end = offset + len;
16122192Sjoerg	struct jffs2_node_frag *frag;
16223672Speter	int ret;
16323672Speter
1641558Srgrimes	jffs2_dbg(1, "%s(): ino #%u, range 0x%08x-0x%08x\n",
1651558Srgrimes		  __func__, f->inocache->ino, offset, offset + len);
16679427Sdillon
16779427Sdillon	frag = jffs2_lookup_node_frag(&f->fragtree, offset);
16879427Sdillon
16979427Sdillon	/* XXX FIXME: Where a single physical node actually shows up in two
17023672Speter	   frags, we read it twice. Don't do that. */
17123672Speter	/* Now we're pointing at the first frag which overlaps our page
1721558Srgrimes	 * (or perhaps is before it, if we've been asked to read off the
1731558Srgrimes	 * end of the file). */
17425288Swollman	while(offset < end) {
17525288Swollman		jffs2_dbg(2, "%s(): offset %d, end %d\n",
17625288Swollman			  __func__, offset, end);
17725288Swollman		if (unlikely(!frag || frag->ofs > offset ||
17825288Swollman			     frag->ofs + frag->size <= offset)) {
17925288Swollman			uint32_t holesize = end - offset;
1801558Srgrimes			if (frag && frag->ofs > offset) {
1811558Srgrimes				jffs2_dbg(1, "Eep. Hole in ino #%u fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n",
1821558Srgrimes					  f->inocache->ino, frag->ofs, offset);
1831558Srgrimes				holesize = min(holesize, frag->ofs - offset);
18423672Speter			}
18523672Speter			jffs2_dbg(1, "Filling non-frag hole from %d-%d\n",
1861558Srgrimes				  offset, offset + holesize);
1871558Srgrimes			memset(buf, 0, holesize);
18891540Siedowse			buf += holesize;
18991540Siedowse			offset += holesize;
19091540Siedowse			continue;
19191540Siedowse		} else if (unlikely(!frag->node)) {
19223672Speter			uint32_t holeend = min(end, frag->ofs + frag->size);
19323672Speter			jffs2_dbg(1, "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n",
19423672Speter				  offset, holeend, frag->ofs,
19523672Speter				  frag->ofs + frag->size);
19623672Speter			memset(buf, 0, holeend - offset);
19737635Sjkoshy			buf += holeend - offset;
19823672Speter			offset = holeend;
19923672Speter			frag = frag_next(frag);
20023672Speter			continue;
20123672Speter		} else {
20223672Speter			uint32_t readlen;
20323672Speter			uint32_t fragofs; /* offset within the frag to start reading */
20423672Speter
20523672Speter			fragofs = offset - frag->ofs;
20623672Speter			readlen = min(frag->size - fragofs, end - offset);
20723672Speter			jffs2_dbg(1, "Reading %d-%d from node at 0x%08x (%d)\n",
20823672Speter				  frag->ofs+fragofs,
20923672Speter				  frag->ofs + fragofs+readlen,
21037635Sjkoshy				  ref_offset(frag->node->raw),
21123672Speter				  ref_flags(frag->node->raw));
2121558Srgrimes			ret = jffs2_read_dnode(c, f, frag->node, buf, fragofs + frag->ofs - frag->node->ofs, readlen);
21323672Speter			jffs2_dbg(2, "node read done\n");
2141558Srgrimes			if (ret) {
21523672Speter				jffs2_dbg(1, "%s(): error %d\n",
21623672Speter					  __func__, ret);
21723672Speter				memset(buf, 0, readlen);
2181558Srgrimes				return ret;
219102231Strhodes			}
22037635Sjkoshy			buf += readlen;
2211558Srgrimes			offset += readlen;
2221558Srgrimes			frag = frag_next(frag);
2231558Srgrimes			jffs2_dbg(2, "node read was OK. Looping\n");
2241558Srgrimes		}
2251558Srgrimes	}
2261558Srgrimes	return 0;
2271558Srgrimes}
2281558Srgrimes
22937635Sjkoshy