[PATCH] avoid unnnecessary mtd read when read can be satisfied by write buffer

Kevin Vigor kevin at realmsys.com
Wed May 31 17:50:28 EDT 2006


The following small patch modifies wbuf.c::jffs2_flash_read to avoid a 
call to the mtd read layer in the case where all data returned by the 
read would be replaced by data from the write buffer anyway. This 
condition occurs relatively infrequently, but the cost of the test is 
low and the benefit of avoiding the mtd read is high.

Signed-off-by: Kevin Vigor <kevin at realmsys.com>


diff --git a/fs/jffs2/wbuf.c b/fs/jffs2/wbuf.c
index a7f153f..01ef266 100644
--- a/fs/jffs2/wbuf.c
+++ b/fs/jffs2/wbuf.c
@@ -907,6 +907,20 @@ int jffs2_flash_read(struct jffs2_sb_inf

    /* Read flash */
    down_read(&c->wbuf_sem);
+
+    /* Check if the read can be completely satisfied from the write buffer;
+     * if so, we can avoid the mtd call entirely.
+     */
+    if (c->wbuf_pagesize &&
+        (SECTOR_ADDR(ofs) == SECTOR_ADDR(c->wbuf_ofs)) &&
+        (ofs >= c->wbuf_ofs) &&
+        (ofs + len) <= (c->wbuf_ofs + c->wbuf_len)) {
+        memcpy(buf, c->wbuf + (ofs - c->wbuf_ofs), len);
+        *retlen = len;
+        ret = 0;
+        goto exit;
+    }
+
    ret = c->mtd->read(c->mtd, ofs, len, retlen, buf);

    if ( (ret == -EBADMSG || ret == -EUCLEAN) && (*retlen == len) ) {






More information about the linux-mtd mailing list