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>
15 /* Core filesystem. Different proxies, such as the NFSv3 one, interface to
16 * this, but the core actually tracks the data which is stored. So far we just
17 * implement an in-memory filesystem, but eventually this will be state which
18 * is persisted to the cloud. */
20 /* Return the current time, in microseconds since the epoch. */
21 int64_t bluesky_get_current_time()
24 g_get_current_time(&t);
25 return (int64_t)t.tv_sec * 1000000 + t.tv_usec;
28 /* Update an inode to indicate that a modification was made. This increases
29 * the change counter, updates the ctime to the current time, and optionally
30 * updates the mtime. */
31 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime)
33 int64_t now = bluesky_get_current_time();
34 inode->change_count++;
40 /* Unfortunately a glib hash table is only guaranteed to be able to store
41 * 32-bit keys if we use the key directly. If we want 64-bit inode numbers,
42 * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
43 * to it. Rather than allocate the memory for the key, we'll just include a
44 * pointer to the 64-bit inum stored in the inode itself, so that we don't need
45 * to do any more memory management. */
46 static guint bluesky_fs_key_hash_func(gconstpointer key)
48 uint64_t inum = *(const uint64_t *)key;
52 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
54 uint64_t i1 = *(const uint64_t *)a;
55 uint64_t i2 = *(const uint64_t *)b;
59 /* Filesystem-level operations. A filesystem is like a directory tree that we
60 * are willing to export. */
61 BlueSkyFS *bluesky_new_fs(gchar *name)
63 BlueSkyFS *fs = g_new0(BlueSkyFS, 1);
64 fs->lock = g_mutex_new();
65 fs->name = g_strdup(name);
66 fs->inodes = g_hash_table_new(bluesky_fs_key_hash_func,
67 bluesky_fs_key_equal_func);
68 fs->next_inum = BLUESKY_ROOT_INUM + 1;
73 /* Allocate a fresh inode number which has not been used before within a
75 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
79 g_mutex_lock(fs->lock);
82 g_mutex_unlock(fs->lock);
87 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
89 BlueSkyInode *i = g_new0(BlueSkyInode, 1);
91 i->lock = g_mutex_new();
97 i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
99 case BLUESKY_DIRECTORY:
100 i->dirents = g_sequence_new(bluesky_dirent_destroy);
101 i->dirhash = g_hash_table_new(g_str_hash, g_str_equal);
104 case BLUESKY_CHARACTER:
105 case BLUESKY_SYMLINK:
114 /* Retrieve an inode from the filesystem. Eventually this will be a cache and
115 * so we might need to go fetch the inode from elsewhere; for now all
116 * filesystem state is stored here. */
117 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
119 BlueSkyInode *inode = NULL;
121 g_mutex_lock(fs->lock);
122 inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
123 g_mutex_unlock(fs->lock);
128 /* Insert an inode into the filesystem inode cache. */
129 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
131 g_mutex_lock(fs->lock);
132 g_hash_table_insert(fs->inodes, &inode->inum, inode);
133 g_mutex_unlock(fs->lock);
136 /* Mark a given block dirty and make sure that data is faulted in so that it
137 * can be written to. */
138 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
140 g_return_if_fail(i < inode->blocks->len);
141 BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
143 switch (block->type) {
144 case BLUESKY_BLOCK_ZERO:
145 block->data = g_malloc0(BLUESKY_BLOCK_SIZE);
147 case BLUESKY_BLOCK_REF:
148 /* TODO: Pull in data first */
149 block->data = g_malloc0(BLUESKY_BLOCK_SIZE);
151 case BLUESKY_BLOCK_CACHED:
152 case BLUESKY_BLOCK_DIRTY:
156 block->type = BLUESKY_BLOCK_DIRTY;
159 /* Set the size of a file. This will truncate or extend the file as needed.
160 * Newly-allocated bytes are zeroed. */
161 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
163 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
165 if (size == inode->size)
168 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
170 if (blocks > inode->blocks->len) {
171 /* Need to add new blocks to the end of a file. New block structures
172 * are automatically zeroed, which initializes them to be pointers to
173 * zero blocks so we don't need to do any more work. */
174 g_array_set_size(inode->blocks, blocks);
175 } else if (blocks < inode->blocks->len) {
176 /* Delete blocks from a file. Must reclaim memory. */
177 for (guint i = inode->blocks->len; i < blocks; i++) {
178 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
182 g_array_set_size(inode->blocks, blocks);
185 /* If the file size is being decreased, ensure that any trailing data in
186 * the last block is zeroed. */
187 if (size < inode->size) {
188 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
190 if (b->type != BLUESKY_BLOCK_ZERO) {
191 bluesky_block_touch(inode, blocks - 1);
192 int end_offset = size % BLUESKY_BLOCK_SIZE;
193 if (end_offset > 0) {
194 memset(&b->data[end_offset], 0,
195 BLUESKY_BLOCK_SIZE - end_offset);
201 bluesky_inode_update_ctime(inode, 1);
204 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
205 const char *data, gint len)
207 g_return_if_fail(inode->type == BLUESKY_REGULAR);
208 g_return_if_fail(offset < inode->size);
209 g_return_if_fail(len <= inode->size - offset);
215 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
216 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
217 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
219 bluesky_block_touch(inode, block_num);
220 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
222 memcpy(&b->data[block_offset], data, bytes);
229 bluesky_inode_update_ctime(inode, 1);
232 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
235 g_return_if_fail(inode->type == BLUESKY_REGULAR);
236 g_return_if_fail(offset < inode->size);
237 g_return_if_fail(len <= inode->size - offset);
240 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
241 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
242 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
244 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
247 case BLUESKY_BLOCK_ZERO:
248 memset(buf, 0, bytes);
250 case BLUESKY_BLOCK_REF:
251 /* TODO: Pull in data first */
252 memset(buf, 0, bytes);
254 case BLUESKY_BLOCK_CACHED:
255 case BLUESKY_BLOCK_DIRTY:
256 memcpy(buf, &b->data[block_offset], bytes);