A Total Overhaul on the Lunaix's Virtual Memory Model (#26)
[lunaix-os.git] / lunaix-os / kernel / fs / pcache.c
index 03f7502072d9acab6c98fd688b0ecb4e708bb1e2..3e77cf50a56cb756596032ca569e0187beff4e0b 100644 (file)
@@ -1,7 +1,6 @@
 #include <klibc/string.h>
 #include <lunaix/ds/btrie.h>
 #include <lunaix/fs.h>
-#include <lunaix/mm/page.h>
 #include <lunaix/mm/pmm.h>
 #include <lunaix/mm/valloc.h>
 #include <lunaix/mm/vmm.h>
 
 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;
+}
+
+static void
+pcache_free_page(void* va)
+{
+    ptr_t pa = vmm_del_mapping(VMS_SELF, (ptr_t)va);
+    pmm_free_page(pa);
+}
+
+static void*
+pcache_alloc_page()
+{
+    int i = 0;
+    ptr_t pp = pmm_alloc_page(0), va = 0;
+
+    if (!pp) {
+        return NULL;
+    }
+
+    if (!(va = (ptr_t)vmap(pp, PAGE_SIZE, KERNEL_DATA))) {
+        pmm_free_page(pp);
+        return NULL;
+    }
+
+    return (void*)va;
+}
+
 void
 pcache_init(struct pcache* pcache)
 {
-    btrie_init(&pcache->tree, PG_SIZE_BITS);
+    btrie_init(&pcache->tree, PAGE_SHIFT);
     llist_init_head(&pcache->dirty);
     llist_init_head(&pcache->pages);
-    pcache_zone = lru_new_zone();
+
+    pcache_zone = lru_new_zone(__pcache_try_evict);
 }
 
 void
 pcache_release_page(struct pcache* pcache, struct pcache_pg* page)
 {
-    vfree(page->pg);
+    pcache_free_page(page->pg);
 
     llist_delete(&page->pg_list);
 
+    btrie_remove(&pcache->tree, page->fpos);
+
     vfree(page);
 
     pcache->n_pages--;
 }
 
-void
-pcache_evict(struct pcache* pcache)
-{
-    struct pcache_pg* page =
-      container_of(lru_evict_one(pcache_zone), struct pcache_pg, lru);
-
-    if (!page)
-        return;
-
-    pcache_invalidate(pcache, page);
-}
-
 struct pcache_pg*
-pcache_new_page(struct pcache* pcache, uint32_t index)
+pcache_new_page(struct pcache* pcache, u32_t index)
 {
     struct pcache_pg* ppg = vzalloc(sizeof(struct pcache_pg));
-    void* pg = valloc(PG_SIZE);
+    void* pg = pcache_alloc_page();
 
     if (!ppg || !pg) {
-        pcache_evict(pcache);
+        lru_evict_one(pcache_zone);
         if (!ppg && !(ppg = vzalloc(sizeof(struct pcache_pg)))) {
             return NULL;
         }
 
-        if (!pg && !(pg = valloc(PG_SIZE))) {
+        if (!pg && !(pg = pcache_alloc_page())) {
             return NULL;
         }
     }
 
     ppg->pg = pg;
+    ppg->holder = pcache;
 
     llist_append(&pcache->pages, &ppg->pg_list);
     btrie_set(&pcache->tree, index, ppg);
@@ -79,17 +103,18 @@ 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,
+                u32_t index,
+                u32_t* offset,
                 struct pcache_pg** page)
 {
     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;
     }
@@ -100,62 +125,87 @@ pcache_get_page(struct pcache* pcache,
 }
 
 int
-pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
+pcache_write(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
 {
-    uint32_t pg_off, buf_off = 0;
+    int errno = 0;
+    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);
-        if (!pg) {
-            return ENOMEM;
+    while (buf_off < len && errno >= 0) {
+        u32_t wr_bytes = MIN(PAGE_SIZE - pg_off, len - buf_off);
+
+        int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
+
+        if (new_page) {
+            // Filling up the page
+            errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
+
+            if (errno < 0) {
+                break;
+            }
+            if (errno < (int)PAGE_SIZE) {
+                // EOF
+                len = MIN(len, buf_off + errno);
+            }
+        } else if (!pg) {
+            errno = inode->default_fops->write(inode, data, wr_bytes, fpos);
+            continue;
         }
 
-        uint32_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);
 
+        pg->len = pg_off + wr_bytes;
         buf_off += wr_bytes;
         fpos += wr_bytes;
     }
 
-    return buf_off;
+    return errno < 0 ? errno : (int)buf_off;
 }
 
 int
-pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
+pcache_read(struct v_inode* inode, void* data, u32_t len, u32_t fpos)
 {
-    uint32_t pg_off, buf_off = 0, new_pg = 0;
+    u32_t pg_off, buf_off = 0, new_pg = 0;
     int errno = 0;
     struct pcache* pcache = inode->pg_cache;
     struct pcache_pg* pg;
 
     while (buf_off < len) {
-        if (pcache_get_page(pcache, fpos, &pg_off, &pg)) {
+        int new_page = pcache_get_page(pcache, fpos, &pg_off, &pg);
+        if (new_page) {
+            // Filling up the page
+            errno = inode->default_fops->read_page(inode, pg->pg, pg->fpos);
 
-            if (!pg) {
-                return ENOMEM;
+            if (errno < 0) {
+                break;
             }
-
-            // Filling up the page
-            errno = inode->default_fops.read(inode, pg->pg, PG_SIZE, pg->fpos);
-            if (errno >= 0 && errno < PG_SIZE) {
+            if (errno < (int)PAGE_SIZE) {
                 // EOF
-                len = buf_off + errno;
-            } else if (errno < 0) {
-                break;
+                len = MIN(len, buf_off + errno);
             }
+
+            pg->len = errno;
+        } else if (!pg) {
+            errno = inode->default_fops->read(
+              inode, (data + buf_off), len - buf_off, pg->fpos);
+            buf_off = len;
+            break;
         }
-        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;
         fpos += rd_bytes;
     }
 
-    return errno < 0 ? errno : buf_off;
+    return errno < 0 ? errno : (int)buf_off;
 }
 
 void
@@ -164,7 +214,7 @@ pcache_release(struct pcache* pcache)
     struct pcache_pg *pos, *n;
     llist_for_each(pos, n, &pcache->pages, pg_list)
     {
-        lru_remove(&pos->lru);
+        lru_remove(pcache_zone, &pos->lru);
         vfree(pos);
     }
 
@@ -175,10 +225,10 @@ int
 pcache_commit(struct v_inode* inode, struct pcache_pg* page)
 {
     if (!(page->flags & PCACHE_DIRTY)) {
-        return;
+        return 0;
     }
 
-    int errno = inode->default_fops.write(inode, page->pg, PG_SIZE, page->fpos);
+    int errno = inode->default_fops->write_page(inode, page->pg, page->fpos);
 
     if (!errno) {
         page->flags &= ~PCACHE_DIRTY;
@@ -192,6 +242,10 @@ pcache_commit(struct v_inode* inode, struct pcache_pg* page)
 void
 pcache_commit_all(struct v_inode* inode)
 {
+    if (!inode->pg_cache) {
+        return;
+    }
+
     struct pcache* cache = inode->pg_cache;
     struct pcache_pg *pos, *n;