git://scm.lunaixsky.com
/
lunaix-os.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
refactor: separate syscall interfaces from kernel space, into posix compliant structure.
[lunaix-os.git]
/
lunaix-os
/
kernel
/
fs
/
pcache.c
diff --git
a/lunaix-os/kernel/fs/pcache.c
b/lunaix-os/kernel/fs/pcache.c
index 3db75718592896c96b0968c73c70b84dd2760240..0f30891669108f1a13d9eb544ce1b4910e1b0f02 100644
(file)
--- a/
lunaix-os/kernel/fs/pcache.c
+++ b/
lunaix-os/kernel/fs/pcache.c
@@
-9,19
+9,29
@@
#define PCACHE_DIRTY 0x1
#define PCACHE_DIRTY 0x1
+static struct lru_zone* pcache_zone;
+
+static int
+__pcache_try_evict(struct lru_node* obj)
+{
+ struct pcache_pg* page = container_of(obj, struct pcache_pg, lru);
+ pcache_invalidate(page->holder, page);
+ return 1;
+}
+
void
pcache_init(struct pcache* pcache)
{
btrie_init(&pcache->tree, PG_SIZE_BITS);
llist_init_head(&pcache->dirty);
llist_init_head(&pcache->pages);
void
pcache_init(struct pcache* pcache)
{
btrie_init(&pcache->tree, PG_SIZE_BITS);
llist_init_head(&pcache->dirty);
llist_init_head(&pcache->pages);
+ pcache_zone = lru_new_zone(__pcache_try_evict);
}
void
pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
{
}
void
pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
{
- pmm_free_page(KERNEL_PID, page->pg);
- vmm_del_mapping(PD_REFERENCED, page->pg);
+ vfree(page->pg);
llist_delete(&page->pg_list);
llist_delete(&page->pg_list);
@@
-31,12
+41,24
@@
pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
}
struct pcache_pg*
}
struct pcache_pg*
-pcache_new_page(struct pcache* pcache, u
int
32_t index)
+pcache_new_page(struct pcache* pcache, u32_t index)
{
{
- void* pg = pmm_alloc_page(KERNEL_PID, 0);
- void* pg_v = vmm_vmap(pg, PG_SIZE, PG_PREM_URW);
struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
- ppg->pg = pg_v;
+ void* pg = valloc(PG_SIZE);
+
+ if (!ppg || !pg) {
+ lru_evict_one(pcache_zone);
+ if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
+ return NULL;
+ }
+
+ if (!pg && !(pg = valloc(PG_SIZE))) {
+ return NULL;
+ }
+ }
+
+ ppg->pg = pg;
+ ppg->holder = pcache;
llist_append(&pcache->pages, &ppg->pg_list);
btrie_set(&pcache->tree, index, ppg);
llist_append(&pcache->pages, &ppg->pg_list);
btrie_set(&pcache->tree, index, ppg);
@@
-54,39
+76,46
@@
pcache_set_dirty(struct pcache* pcache, struct pcache_pg* pg)
}
}
}
}
-struct pcache_pg*
+int
pcache_get_page(struct pcache* pcache,
pcache_get_page(struct pcache* pcache,
- u
int
32_t index,
- u
int
32_t* offset,
+ u32_t index,
+ u32_t* offset,
struct pcache_pg** page)
{
struct pcache_pg* pg = btrie_get(&pcache->tree, index);
int is_new = 0;
struct pcache_pg** page)
{
struct pcache_pg* pg = btrie_get(&pcache->tree, index);
int is_new = 0;
-
*offset = index &
((1 << pcache->tree.truncated) - 1);
- if (!pg) {
- pg = pcache_new_page(pcache, index);
- pg->fpos = index
- *offset
;
+
u32_t mask =
((1 << pcache->tree.truncated) - 1);
+ *offset = index & mask;
+ if (!pg && (pg = pcache_new_page(pcache, index))) {
+ pg->fpos = index
& ~mask
;
pcache->n_pages++;
is_new = 1;
}
pcache->n_pages++;
is_new = 1;
}
+ if (pg)
+ lru_use_one(pcache_zone, &pg->lru);
*page = pg;
return is_new;
}
int
*page = pg;
return is_new;
}
int
-pcache_write(struct v_
file* file, void* data, uint32_t len, uint
32_t fpos)
+pcache_write(struct v_
inode* inode, void* data, u32_t len, u
32_t fpos)
{
{
- u
int
32_t pg_off, buf_off = 0;
- struct pcache* pcache =
file->
inode->pg_cache;
+ u32_t pg_off, buf_off = 0;
+ struct pcache* pcache = inode->pg_cache;
struct pcache_pg* pg;
while (buf_off < len) {
pcache_get_page(pcache, fpos, &pg_off, &pg);
struct pcache_pg* pg;
while (buf_off < len) {
pcache_get_page(pcache, fpos, &pg_off, &pg);
- uint32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
+ if (!pg) {
+ return ENOMEM;
+ }
+
+ u32_t wr_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
pcache_set_dirty(pcache, pg);
memcpy(pg->pg + pg_off, (data + buf_off), wr_bytes);
pcache_set_dirty(pcache, pg);
+ pg->len = pg_off + wr_bytes;
buf_off += wr_bytes;
fpos += wr_bytes;
}
buf_off += wr_bytes;
fpos += wr_bytes;
}
@@
-95,26
+124,36
@@
pcache_write(struct v_file* file, void* data, uint32_t len, uint32_t fpos)
}
int
}
int
-pcache_read(struct v_
file* file, void* data, uint32_t len, uint
32_t fpos)
+pcache_read(struct v_
inode* inode, void* data, u32_t len, u
32_t fpos)
{
{
- u
int
32_t pg_off, buf_off = 0, new_pg = 0;
+ u32_t pg_off, buf_off = 0, new_pg = 0;
int errno = 0;
int errno = 0;
- struct pcache* pcache =
file->
inode->pg_cache;
+ struct pcache* pcache = inode->pg_cache;
struct pcache_pg* pg;
struct pcache_pg* pg;
- struct v_inode* inode = file->inode;
while (buf_off < len) {
if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
while (buf_off < len) {
if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
+
+ if (!pg) {
+ return ENOMEM;
+ }
+
// Filling up the page
// Filling up the page
- errno = inode->default_fops.read(file, 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
if (errno >= 0 && errno < PG_SIZE) {
// EOF
- len =
buf_off + errno
;
+ len =
MIN(len, buf_off + errno)
;
} else if (errno < 0) {
break;
}
} else if (errno < 0) {
break;
}
+ pg->len = errno;
}
}
- uint32_t rd_bytes = MIN(PG_SIZE - pg_off, len - buf_off);
+ u32_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;
memcpy((data + buf_off), pg->pg + pg_off, rd_bytes);
buf_off += rd_bytes;
@@
-130,6
+169,7
@@
pcache_release(struct pcache* pcache)
struct pcache_pg *pos, *n;
llist_for_each(pos, n, &pcache->pages, pg_list)
{
struct pcache_pg *pos, *n;
llist_for_each(pos, n, &pcache->pages, pg_list)
{
+ lru_remove(pcache_zone, &pos->lru);
vfree(pos);
}
vfree(pos);
}
@@
-137,38
+177,43
@@
pcache_release(struct pcache* pcache)
}
int
}
int
-pcache_commit(struct v_
file* fil
e, struct pcache_pg* page)
+pcache_commit(struct v_
inode* inod
e, struct pcache_pg* page)
{
if (!(page->flags & PCACHE_DIRTY)) {
return;
}
{
if (!(page->flags & PCACHE_DIRTY)) {
return;
}
- struct v_inode* inode = file->inode;
-
int errno = inode->default_fops.write(fil
e, page->pg, PG_SIZE, page->fpos);
+ int errno =
+
inode->default_fops->write_page(inod
e, page->pg, PG_SIZE, page->fpos);
if (!errno) {
page->flags &= ~PCACHE_DIRTY;
llist_delete(&page->dirty_list);
if (!errno) {
page->flags &= ~PCACHE_DIRTY;
llist_delete(&page->dirty_list);
-
file->
inode->pg_cache->n_dirty--;
+ inode->pg_cache->n_dirty--;
}
return errno;
}
void
}
return errno;
}
void
-pcache_commit_all(struct v_
file* fil
e)
+pcache_commit_all(struct v_
inode* inod
e)
{
{
- struct pcache* cache = file->inode->pg_cache;
+ if (!inode->pg_cache) {
+ return;
+ }
+
+ struct pcache* cache = inode->pg_cache;
struct pcache_pg *pos, *n;
struct pcache_pg *pos, *n;
+
llist_for_each(pos, n, &cache->dirty, dirty_list)
{
llist_for_each(pos, n, &cache->dirty, dirty_list)
{
- pcache_commit(
fil
e, pos);
+ pcache_commit(
inod
e, pos);
}
}
void
}
}
void
-pcache_invalidate(struct
v_file* fil
e, struct pcache_pg* page)
+pcache_invalidate(struct
pcache* pcach
e, struct pcache_pg* page)
{
{
- pcache_commit(
file
, page);
- pcache_release_page(
&file->inode->pg_
cache, page);
+ pcache_commit(
pcache->master
, page);
+ pcache_release_page(
p
cache, page);
}
\ No newline at end of file
}
\ No newline at end of file