Inodes can now be dropped from the cache too, not just file blocks.
authorMichael Vrable <mvrable@cs.ucsd.edu>
Wed, 17 Mar 2010 22:01:07 +0000 (15:01 -0700)
committerMichael Vrable <mvrable@cs.ucsd.edu>
Wed, 17 Mar 2010 22:01:07 +0000 (15:01 -0700)
bluesky/cache.c
bluesky/inode.c

index e9d95cf..481390f 100644 (file)
@@ -54,8 +54,23 @@ static void flushd_inode(gpointer value, gpointer user_data)
 
     if (inode->change_count == inode->change_commit) {
         uint64_t delay = bluesky_get_current_time() - inode->access_time;
-        if (delay >= CACHE_CLEAN_DELAY)
+        if (delay >= CACHE_CLEAN_DELAY) {
             drop_caches(inode);
+
+            /* If the only references are the one we hold and the one in the
+             * filesystem inum->inode hash table...  The check for a refcount
+             * of 2 shouldn't be necessary so it should be okay if there is a
+             * race on the check. */
+            if (inode->refcount == 2) {
+                g_log("bluesky/flushd", G_LOG_LEVEL_DEBUG,
+                      "Trying to drop inode %"PRIu64" from cache", inode->inum);
+                g_mutex_lock(fs->lock);
+                if (g_hash_table_remove(fs->inodes, &inode->inum))
+                    bluesky_inode_unref(inode);
+                g_mutex_unlock(fs->lock);
+            }
+        }
+
         g_mutex_unlock(inode->lock);
         bluesky_inode_unref(inode);
         return;
index 55781ca..3e92e4f 100644 (file)
@@ -120,8 +120,47 @@ void bluesky_inode_ref(BlueSkyInode *inode)
 void bluesky_inode_unref(BlueSkyInode *inode)
 {
     if (g_atomic_int_dec_and_test(&inode->refcount)) {
-        g_error("Reference count for inode %"PRIu64" dropped to zero!\n",
+        g_print("Reference count for inode %"PRIu64" dropped to zero.\n",
                 inode->inum);
+
+        /* Sanity check: Is the inode clean? */
+        if (inode->change_commit < inode->change_count) {
+            g_warning("Dropping inode which is not clean (commit %"PRIi64" < change %"PRIi64")\n", inode->change_commit, inode->change_count);
+        }
+
+        /* Free file type specific data.  It should be an error for there to be
+         * dirty data to commit when the reference count has reaches zero. */
+        switch (inode->type) {
+        case BLUESKY_REGULAR:
+            for (int i = 0; i < inode->blocks->len; i++) {
+                BlueSkyBlock *b = &g_array_index(inode->blocks,
+                                                 BlueSkyBlock, i);
+                if (b->type == BLUESKY_BLOCK_DIRTY) {
+                    g_error("Deleting an inode with dirty file data!");
+                }
+                g_free(b->ref);
+                bluesky_string_unref(b->data);
+            }
+            g_array_unref(inode->blocks);
+            break;
+
+        case BLUESKY_DIRECTORY:
+            g_hash_table_destroy(inode->dirhash);
+            g_hash_table_destroy(inode->dirhash_folded);
+            g_sequence_free(inode->dirents);
+            break;
+
+        case BLUESKY_SYMLINK:
+            g_free(inode->symlink_contents);
+            break;
+
+        default:
+            break;
+        }
+
+        g_mutex_free(inode->lock);
+
+        g_free(inode);
     }
 }
 
@@ -288,6 +327,8 @@ static void complete_inode_fetch(BlueSkyStoreAsync *async, BlueSkyInode *inode)
         bluesky_inode_unref(inode);
     }
 
+    inode->access_time = bluesky_get_current_time();
+
     g_mutex_unlock(inode->lock);
     bluesky_inode_unref(inode);
 }