1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
14 /* Core filesystem. Different proxies, such as the NFSv3 one, interface to
15 * this, but the core actually tracks the data which is stored. So far we just
16 * implement an in-memory filesystem, but eventually this will be state which
17 * is persisted to the cloud. */
19 /* Return the current time, in microseconds since the epoch. */
20 int64_t bluesky_get_current_time()
23 g_get_current_time(&t);
24 return t.tv_sec * 1000000 + t.tv_usec;
27 /* Unfortunately a glib hash table is only guaranteed to be able to store
28 * 32-bit keys if we use the key directly. If we want 64-bit inode numbers,
29 * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
30 * to it. Rather than allocate the memory for the key, we'll just include a
31 * pointer to the 64-bit inum stored in the inode itself, so that we don't need
32 * to do any more memory management. */
33 static guint bluesky_fs_key_hash_func(gconstpointer key)
35 uint64_t inum = *(const uint64_t *)key;
39 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
41 uint64_t i1 = *(const uint64_t *)a;
42 uint64_t i2 = *(const uint64_t *)b;
46 /* Filesystem-level operations. A filesystem is like a directory tree that we
47 * are willing to export. */
48 BlueSkyFS *bluesky_new_fs(gchar *name)
50 BlueSkyFS *fs = g_new0(BlueSkyFS, 1);
51 fs->lock = g_mutex_new();
52 fs->name = g_strdup(name);
53 fs->inodes = g_hash_table_new(bluesky_fs_key_hash_func,
54 bluesky_fs_key_equal_func);
55 fs->next_inum = BLUESKY_ROOT_INUM + 1;
60 /* Allocate a fresh inode number which has not been used before within a
62 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
66 g_mutex_lock(fs->lock);
69 g_mutex_unlock(fs->lock);
74 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
76 BlueSkyInode *i = g_new0(BlueSkyInode, 1);
78 i->lock = g_mutex_new();
85 case BLUESKY_DIRECTORY:
86 i->dirents = g_sequence_new(bluesky_dirent_destroy);
88 case BLUESKY_CHARACTER:
98 /* Retrieve an inode from the filesystem. Eventually this will be a cache and
99 * so we might need to go fetch the inode from elsewhere; for now all
100 * filesystem state is stored here. */
101 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
103 BlueSkyInode *inode = NULL;
105 g_mutex_lock(fs->lock);
106 inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
107 g_mutex_unlock(fs->lock);
112 /* Insert an inode into the filesystem inode cache. */
113 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
115 g_mutex_lock(fs->lock);
116 g_hash_table_insert(fs->inodes, &inode->inum, inode);
117 g_mutex_unlock(fs->lock);