Support to multi-threading and pthread interface (POSIX.1-2008) (#23)
[lunaix-os.git] / lunaix-os / kernel / fs / pcache.c
index a6c4c31d34993b9ba9b8da91a195f51b886ad655..4c6f69cdbe284953e3e011ebcb84a2157802aff3 100644 (file)
@@ -19,32 +19,60 @@ __pcache_try_evict(struct lru_node* obj)
     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, PG_SIZE, PG_PREM_RW, 0))) {
+        pmm_free_page(pp);
+        return NULL;
+    }
+
+    return (void*)va;
+}
+
 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)
 {
-    vfree(page->pg);
+    pcache_free_page(page->pg);
 
     llist_delete(&page->pg_list);
 
+    btrie_remove(&pcache->tree, page->fpos);
+
     vfree(page);
 
     pcache->n_pages--;
 }
 
 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) {
         lru_evict_one(pcache_zone);
@@ -52,7 +80,7 @@ pcache_new_page(struct pcache* pcache, uint32_t index)
             return NULL;
         }
 
-        if (!pg && !(pg = valloc(PG_SIZE))) {
+        if (!pg && !(pg = pcache_alloc_page())) {
             return NULL;
         }
     }
@@ -78,8 +106,8 @@ pcache_set_dirty(struct pcache* pcache, struct pcache_pg* 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);
@@ -98,21 +126,35 @@ 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(PG_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 < PG_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;
@@ -120,36 +162,40 @@ pcache_write(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
         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_page(inode, pg->pg, PG_SIZE, pg->fpos);
-            if (errno >= 0 && errno < PG_SIZE) {
+            if (errno < PG_SIZE) {
                 // EOF
                 len = MIN(len, buf_off + errno);
-            } else if (errno < 0) {
-                break;
             }
+
             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->len - pg_off, len - buf_off);
+
+        u32_t rd_bytes = MIN(pg->len - pg_off, len - buf_off);
 
         if (!rd_bytes)
             break;
@@ -160,7 +206,7 @@ pcache_read(struct v_inode* inode, void* data, uint32_t len, uint32_t fpos)
         fpos += rd_bytes;
     }
 
-    return errno < 0 ? errno : buf_off;
+    return errno < 0 ? errno : (int)buf_off;
 }
 
 void
@@ -180,11 +226,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_page(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;