Commit 10731f83 authored by Artem Bityutskiy's avatar Artem Bityutskiy Committed by David Woodhouse

[JFFS2] fix buffer sise calculations in jffs2_get_inode_nodes()

In read inode we have an optimization which prevents one
min. I/O unit (e.g. NAND page) to be read more then once.

Namely, at the beginning we do not know which node type we read,
so we read so we assume we read the directory entry, because it
has the smallest node header. When we read it, we read up to the
next min. I/O unit, just because if later we'll need to read more,
we already have this data.

If it turns out to be that the node is not directory entry, and
we need more data, and we did not read it because it sits in the
next min. I/O unit, we read the whole next (or several next)
min. I/O unit(s). And if it happens to be that we read a data node,
and we've read part of its data, we calculate partial CRC.
So if later we need to check data CRC, we'll only read the rest
of the data from further min. I/O units and continue CRC checking.

This code was a bit messy and buggy. The bug was that it assumed
relatively large min. I/O unit, so that the largest node header
could overlap only one min. I/O unit boundary.

This parch clean-ups the code a bit and fixes this bug.
The patch was not tested on flash with small min. I/O unit, like
NOR-ECC, nut it was tested on NAND with 512 bytes NAND page, so
it at least does not break NAND. It was also tested with mtdram
so it should not break NOR.
Signed-off-by: default avatarArtem Bityutskiy <Artem.Bityutskiy@nokia.com>
Signed-off-by: default avatarDavid Woodhouse <dwmw2@infradead.org>
parent 7f762ab2
...@@ -421,49 +421,38 @@ static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_re ...@@ -421,49 +421,38 @@ static inline int read_unknown(struct jffs2_sb_info *c, struct jffs2_raw_node_re
* negative error code on failure. * negative error code on failure.
*/ */
static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref, static int read_more(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref,
int right_size, int *rdlen, unsigned char *buf, unsigned char *bufstart) int needed_len, int *rdlen, unsigned char *buf)
{ {
int right_len, err, len; int err, to_read = needed_len - *rdlen;
size_t retlen; size_t retlen;
uint32_t offs; uint32_t offs;
if (jffs2_is_writebuffered(c)) { if (jffs2_is_writebuffered(c)) {
right_len = c->wbuf_pagesize - (bufstart - buf); int rem = to_read % c->wbuf_pagesize;
if (right_size + (int)(bufstart - buf) > c->wbuf_pagesize)
right_len += c->wbuf_pagesize;
} else
right_len = right_size;
if (*rdlen == right_len) if (rem)
return 0; to_read += c->wbuf_pagesize - rem;
}
/* We need to read more data */ /* We need to read more data */
offs = ref_offset(ref) + *rdlen; offs = ref_offset(ref) + *rdlen;
if (jffs2_is_writebuffered(c)) {
bufstart = buf + c->wbuf_pagesize;
len = c->wbuf_pagesize;
} else {
bufstart = buf + *rdlen;
len = right_size - *rdlen;
}
dbg_readinode("read more %d bytes\n", len); dbg_readinode("read more %d bytes\n", to_read);
err = jffs2_flash_read(c, offs, len, &retlen, bufstart); err = jffs2_flash_read(c, offs, to_read, &retlen, buf + *rdlen);
if (err) { if (err) {
JFFS2_ERROR("can not read %d bytes from 0x%08x, " JFFS2_ERROR("can not read %d bytes from 0x%08x, "
"error code: %d.\n", len, offs, err); "error code: %d.\n", to_read, offs, err);
return err; return err;
} }
if (retlen < len) { if (retlen < to_read) {
JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n", JFFS2_ERROR("short read at %#08x: %zu instead of %d.\n",
offs, retlen, len); offs, retlen, to_read);
return -EIO; return -EIO;
} }
*rdlen = right_len; *rdlen += to_read;
return 0; return 0;
} }
...@@ -486,27 +475,9 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -486,27 +475,9 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
dbg_readinode("ino #%u\n", f->inocache->ino); dbg_readinode("ino #%u\n", f->inocache->ino);
if (jffs2_is_writebuffered(c)) {
/*
* If we have the write buffer, we assume the minimal I/O unit
* is c->wbuf_pagesize. We implement some optimizations which in
* this case and we need a temporary buffer of size =
* 2*c->wbuf_pagesize bytes (see comments in read_dnode()).
* Basically, we want to read not only the node header, but the
* whole wbuf (NAND page in case of NAND) or 2, if the node
* header overlaps the border between the 2 wbufs.
*/
len = 2*c->wbuf_pagesize;
} else {
/*
* When there is no write buffer, the size of the temporary
* buffer is the size of the larges node header.
*/
len = sizeof(union jffs2_node_union);
}
/* FIXME: in case of NOR and available ->point() this /* FIXME: in case of NOR and available ->point() this
* needs to be fixed. */ * needs to be fixed. */
len = sizeof(union jffs2_node_union) + c->wbuf_pagesize;
buf = kmalloc(len, GFP_KERNEL); buf = kmalloc(len, GFP_KERNEL);
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
...@@ -516,8 +487,6 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -516,8 +487,6 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
if (!valid_ref && f->inocache->ino != 1) if (!valid_ref && f->inocache->ino != 1)
JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino); JFFS2_WARNING("Eep. No valid nodes for ino #%u.\n", f->inocache->ino);
while (valid_ref) { while (valid_ref) {
unsigned char *bufstart;
/* We can hold a pointer to a non-obsolete node without the spinlock, /* We can hold a pointer to a non-obsolete node without the spinlock,
but _obsolete_ nodes may disappear at any time, if the block but _obsolete_ nodes may disappear at any time, if the block
they're in gets erased. So if we mark 'ref' obsolete while we're they're in gets erased. So if we mark 'ref' obsolete while we're
...@@ -533,32 +502,31 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -533,32 +502,31 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
/* /*
* At this point we don't know the type of the node we're going * At this point we don't know the type of the node we're going
* to read, so we do not know the size of its header. In order * to read, so we do not know the size of its header. In order
* to minimize the amount of flash IO we assume the node has * to minimize the amount of flash IO we assume the header is
* size = JFFS2_MIN_NODE_HEADER. * of size = JFFS2_MIN_NODE_HEADER.
*/ */
len = JFFS2_MIN_NODE_HEADER;
if (jffs2_is_writebuffered(c)) { if (jffs2_is_writebuffered(c)) {
int end, rem;
/* /*
* We treat 'buf' as 2 adjacent wbufs. We want to * We are about to read JFFS2_MIN_NODE_HEADER bytes,
* adjust bufstart such as it points to the * but this flash has some minimal I/O unit. It is
* beginning of the node within this wbuf. * possible that we'll need to read more soon, so read
* up to the next min. I/O unit, in order not to
* re-read the same min. I/O unit twice.
*/ */
bufstart = buf + (ref_offset(ref) % c->wbuf_pagesize); end = ref_offset(ref) + len;
/* We will read either one wbuf or 2 wbufs. */ rem = end % c->wbuf_pagesize;
len = c->wbuf_pagesize - (bufstart - buf); if (rem)
if (JFFS2_MIN_NODE_HEADER + (int)(bufstart - buf) > c->wbuf_pagesize) { end += c->wbuf_pagesize - rem;
/* The header spans the border of the first wbuf */ len = end - ref_offset(ref);
len += c->wbuf_pagesize;
}
} else {
bufstart = buf;
len = JFFS2_MIN_NODE_HEADER;
} }
dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref)); dbg_readinode("read %d bytes at %#08x(%d).\n", len, ref_offset(ref), ref_flags(ref));
/* FIXME: point() */ /* FIXME: point() */
err = jffs2_flash_read(c, ref_offset(ref), len, err = jffs2_flash_read(c, ref_offset(ref), len, &retlen, buf);
&retlen, bufstart);
if (err) { if (err) {
JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err); JFFS2_ERROR("can not read %d bytes from 0x%08x, " "error code: %d.\n", len, ref_offset(ref), err);
goto free_out; goto free_out;
...@@ -570,7 +538,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -570,7 +538,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
goto free_out; goto free_out;
} }
node = (union jffs2_node_union *)bufstart; node = (union jffs2_node_union *)buf;
/* No need to mask in the valid bit; it shouldn't be invalid */ /* No need to mask in the valid bit; it shouldn't be invalid */
if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) { if (je32_to_cpu(node->u.hdr_crc) != crc32(0, node, sizeof(node->u)-4)) {
...@@ -596,7 +564,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -596,7 +564,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
case JFFS2_NODETYPE_DIRENT: case JFFS2_NODETYPE_DIRENT:
if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) { if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_dirent)) {
err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf, bufstart); err = read_more(c, ref, sizeof(struct jffs2_raw_dirent), &len, buf);
if (unlikely(err)) if (unlikely(err))
goto free_out; goto free_out;
} }
...@@ -616,7 +584,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -616,7 +584,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
case JFFS2_NODETYPE_INODE: case JFFS2_NODETYPE_INODE:
if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) { if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_raw_inode)) {
err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf, bufstart); err = read_more(c, ref, sizeof(struct jffs2_raw_inode), &len, buf);
if (unlikely(err)) if (unlikely(err))
goto free_out; goto free_out;
} }
...@@ -635,7 +603,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf ...@@ -635,7 +603,7 @@ static int jffs2_get_inode_nodes(struct jffs2_sb_info *c, struct jffs2_inode_inf
default: default:
if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) { if (JFFS2_MIN_NODE_HEADER < sizeof(struct jffs2_unknown_node)) {
err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf, bufstart); err = read_more(c, ref, sizeof(struct jffs2_unknown_node), &len, buf);
if (unlikely(err)) if (unlikely(err))
goto free_out; goto free_out;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment