X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/bf870a1dde437a48ae40d092a14e164c861ea102..40941f94f7c8522f65f9ebc425a300d40412bc16:/lunaix-os/kernel/fs/pcache.c diff --git a/lunaix-os/kernel/fs/pcache.c b/lunaix-os/kernel/fs/pcache.c index 8ef8fc8..a6c4c31 100644 --- a/lunaix-os/kernel/fs/pcache.c +++ b/lunaix-os/kernel/fs/pcache.c @@ -76,7 +76,7 @@ pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg) } } -struct pcache_pg* +int pcache_get_page(struct pcache* pcache, uint32_t index, uint32_t* offset, @@ -84,9 +84,10 @@ pcache_get_page(struct pcache* pcache, { struct pcache_pg* pg = btrie_get(&pcache->tree, index); int is_new = 0; - *offset = index & ((1 << pcache->tree.truncated) - 1); + u32_t mask = ((1 << pcache->tree.truncated) - 1); + *offset = index & mask; if (!pg && (pg = pcache_new_page(pcache, index))) { - pg->fpos = index - *offset; + pg->fpos = index & ~mask; pcache->n_pages++; is_new = 1; } @@ -114,6 +115,7 @@ pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos) pcache_set_dirty(pcache, pg); + pg->len = pg_off + wr_bytes; buf_off += wr_bytes; fpos += wr_bytes; } @@ -137,15 +139,21 @@ pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos) } // Filling up the page - errno = inode->default_fops.read(inode, pg->pg, PG_SIZE, pg->fpos); + errno = + inode->default_fops->read_page(inode, pg->pg, PG_SIZE, pg->fpos); if (errno >= 0 && errno < PG_SIZE) { // EOF - len = buf_off + errno; + len = MIN(len, buf_off + errno); } else if (errno < 0) { break; } + pg->len = errno; } - uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off); + uint32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off); + + if (!rd_bytes) + break; + memcpy((data + buf_off), pg->pg + pg_off, rd_bytes); buf_off += rd_bytes; @@ -175,7 +183,8 @@ pcache_commit(struct v_inode* inode, struct pcache_pg* page) return; } - int errno = inode->default_fops.write(inode, page->pg, PG_SIZE, page->fpos); + int errno = + inode->default_fops->write_page(inode, page->pg, PG_SIZE, page->fpos); if (!errno) { page->flags &= ~PCACHE_DIRTY;