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 (int64_t)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();
84 i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
86 case BLUESKY_DIRECTORY:
87 i->dirents = g_sequence_new(bluesky_dirent_destroy);
90 case BLUESKY_CHARACTER:
100 /* Retrieve an inode from the filesystem. Eventually this will be a cache and
101 * so we might need to go fetch the inode from elsewhere; for now all
102 * filesystem state is stored here. */
103 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
105 BlueSkyInode *inode = NULL;
107 g_mutex_lock(fs->lock);
108 inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
109 g_mutex_unlock(fs->lock);
114 /* Insert an inode into the filesystem inode cache. */
115 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
117 g_mutex_lock(fs->lock);
118 g_hash_table_insert(fs->inodes, &inode->inum, inode);
119 g_mutex_unlock(fs->lock);
122 /* Set the size of a file. This will truncate or extend the file as needed.
123 * Newly-allocated bytes are zeroed. */
124 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
126 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
128 if (size == inode->size)
131 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_MAX_FILE_SIZE;
133 if (blocks > inode->blocks->len) {
134 /* Need to add new blocks to the end of a file. New block structures
135 * are automatically zeroed, which initializes them to be pointers to
136 * zero blocks so we don't need to do any more work. */
137 g_array_set_size(inode->blocks, blocks);
138 } else if (blocks < inode->blocks->len) {
139 /* Delete blocks from a file. Must reclaim memory. */
140 for (guint i = inode->blocks->len; i < blocks; i++) {
141 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
145 g_array_set_size(inode->blocks, blocks);
148 /* TODO: Zero out partial blocks if needed? */
151 inode->change_count++;