Try to clean up the locking for LRU lists.
[bluesky.git] / bluesky / file.c
index 822c2c8..8be3ea1 100644 (file)
@@ -44,6 +44,12 @@ void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
         break;
     }
 
+    if (block->type != BLUESKY_BLOCK_CACHED
+            && block->type != BLUESKY_BLOCK_DIRTY)
+        g_atomic_int_add(&inode->fs->cache_total, 1);
+    if (block->type != BLUESKY_BLOCK_DIRTY)
+        g_atomic_int_add(&inode->fs->cache_dirty, 1);
+
     block->type = BLUESKY_BLOCK_DIRTY;
 }
 
@@ -87,6 +93,11 @@ void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
         for (guint i = inode->blocks->len; i < blocks; i++) {
             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
             g_free(b->ref);
+            if (b->type == BLUESKY_BLOCK_CACHED
+                    || b->type == BLUESKY_BLOCK_DIRTY)
+                g_atomic_int_add(&inode->fs->cache_total, -1);
+            if (b->type == BLUESKY_BLOCK_DIRTY)
+                g_atomic_int_add(&inode->fs->cache_dirty, -1);
             bluesky_string_unref(b->data);
         }
         g_array_set_size(inode->blocks, blocks);
@@ -157,6 +168,25 @@ void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
     g_return_if_fail(offset < inode->size);
     g_return_if_fail(len <= inode->size - offset);
 
+    /* Start fetches on any data blocks that we will need for this read. */
+    BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
+    barrier->op = STORE_OP_BARRIER;
+    uint64_t start_block, end_block;
+    start_block = offset / BLUESKY_BLOCK_SIZE;
+    end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
+    g_print("Start prefetch on blocks %"PRIi64" .. %"PRIi64"\n",
+            start_block, end_block);
+    for (uint64_t i = start_block; i <= end_block; i++) {
+        BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
+                                         i);
+        if (b->type == BLUESKY_BLOCK_REF)
+            bluesky_block_fetch(inode->fs, b, barrier);
+    }
+    bluesky_store_async_submit(barrier);
+    bluesky_store_async_wait(barrier);
+    bluesky_store_async_unref(barrier);
+    g_print("Prefetch complete.\n");
+
     while (len > 0) {
         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
@@ -211,6 +241,9 @@ void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
         bluesky_store_add_barrier(barrier, async);
     else
         bluesky_store_async_wait(async);
+
+    bluesky_store_async_unref(async);
+    g_atomic_int_add(&fs->cache_total, 1);
 }
 
 /* Write the given block to cloud-backed storage and mark it clean. */
@@ -242,6 +275,7 @@ void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
     block->ref = name;
 
     block->type = BLUESKY_BLOCK_CACHED;
+    g_atomic_int_add(&fs->cache_dirty, -1);
 
     g_checksum_free(csum);
 }
@@ -269,9 +303,12 @@ void bluesky_file_drop_cached(BlueSkyInode *inode)
                   "Dropping block %d of inode %"PRIu64" from cache",
                   i, inode->inum);
 
+            g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
+                  "  (reference count was %d)", b->data->refcount);
             bluesky_string_unref(b->data);
             b->data = NULL;
             b->type = BLUESKY_BLOCK_REF;
+            g_atomic_int_add(&inode->fs->cache_total, -1);
         }
     }
 }