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 /* Update an inode to indicate that a modification was made. This increases
28 * the change counter, updates the ctime to the current time, and optionally
29 * updates the mtime. */
30 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime)
32 int64_t now = bluesky_get_current_time();
33 inode->change_count++;
39 /* Unfortunately a glib hash table is only guaranteed to be able to store
40 * 32-bit keys if we use the key directly. If we want 64-bit inode numbers,
41 * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
42 * to it. Rather than allocate the memory for the key, we'll just include a
43 * pointer to the 64-bit inum stored in the inode itself, so that we don't need
44 * to do any more memory management. */
45 static guint bluesky_fs_key_hash_func(gconstpointer key)
47 uint64_t inum = *(const uint64_t *)key;
51 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
53 uint64_t i1 = *(const uint64_t *)a;
54 uint64_t i2 = *(const uint64_t *)b;
58 /* Filesystem-level operations. A filesystem is like a directory tree that we
59 * are willing to export. */
60 BlueSkyFS *bluesky_new_fs(gchar *name)
62 BlueSkyFS *fs = g_new0(BlueSkyFS, 1);
63 fs->lock = g_mutex_new();
64 fs->name = g_strdup(name);
65 fs->inodes = g_hash_table_new(bluesky_fs_key_hash_func,
66 bluesky_fs_key_equal_func);
67 fs->next_inum = BLUESKY_ROOT_INUM + 1;
72 /* Allocate a fresh inode number which has not been used before within a
74 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
78 g_mutex_lock(fs->lock);
81 g_mutex_unlock(fs->lock);
86 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
88 BlueSkyInode *i = g_new0(BlueSkyInode, 1);
90 i->lock = g_mutex_new();
96 i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
98 case BLUESKY_DIRECTORY:
99 i->dirents = g_sequence_new(bluesky_dirent_destroy);
100 i->dirhash = g_hash_table_new(g_str_hash, g_str_equal);
103 case BLUESKY_CHARACTER:
104 case BLUESKY_SYMLINK:
113 /* Retrieve an inode from the filesystem. Eventually this will be a cache and
114 * so we might need to go fetch the inode from elsewhere; for now all
115 * filesystem state is stored here. */
116 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
118 BlueSkyInode *inode = NULL;
120 g_mutex_lock(fs->lock);
121 inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
122 g_mutex_unlock(fs->lock);
127 /* Insert an inode into the filesystem inode cache. */
128 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
130 g_mutex_lock(fs->lock);
131 g_hash_table_insert(fs->inodes, &inode->inum, inode);
132 g_mutex_unlock(fs->lock);
135 /* Set the size of a file. This will truncate or extend the file as needed.
136 * Newly-allocated bytes are zeroed. */
137 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
139 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
141 if (size == inode->size)
144 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_MAX_FILE_SIZE;
146 if (blocks > inode->blocks->len) {
147 /* Need to add new blocks to the end of a file. New block structures
148 * are automatically zeroed, which initializes them to be pointers to
149 * zero blocks so we don't need to do any more work. */
150 g_array_set_size(inode->blocks, blocks);
151 } else if (blocks < inode->blocks->len) {
152 /* Delete blocks from a file. Must reclaim memory. */
153 for (guint i = inode->blocks->len; i < blocks; i++) {
154 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
158 g_array_set_size(inode->blocks, blocks);
161 /* TODO: Zero out partial blocks if needed? */
164 inode->change_count++;