95afc0a9cf7916da1352c64244fc2b501ed8deb2
[bluesky.git] / inode.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <glib.h>
11
12 #include "bluesky.h"
13
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. */
18
19 /* Return the current time, in microseconds since the epoch. */
20 int64_t bluesky_get_current_time()
21 {
22     GTimeVal t;
23     g_get_current_time(&t);
24     return (int64_t)t.tv_sec * 1000000 + t.tv_usec;
25 }
26
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)
34 {
35     uint64_t inum = *(const uint64_t *)key;
36     return (guint)inum;
37 }
38
39 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
40 {
41     uint64_t i1 = *(const uint64_t *)a;
42     uint64_t i2 = *(const uint64_t *)b;
43     return i1 == i2;
44 }
45
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)
49 {
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;
56
57     return fs;
58 }
59
60 /* Allocate a fresh inode number which has not been used before within a
61  * filesystem. */
62 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
63 {
64     uint64_t inum;
65
66     g_mutex_lock(fs->lock);
67     inum = fs->next_inum;
68     fs->next_inum++;
69     g_mutex_unlock(fs->lock);
70
71     return inum;
72 }
73
74 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
75 {
76     BlueSkyInode *i = g_new0(BlueSkyInode, 1);
77
78     i->lock = g_mutex_new();
79     i->type = type;
80     i->inum = inum;
81
82     switch (type) {
83     case BLUESKY_REGULAR:
84         i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
85         break;
86     case BLUESKY_DIRECTORY:
87         i->dirents = g_sequence_new(bluesky_dirent_destroy);
88         break;
89     case BLUESKY_BLOCK:
90     case BLUESKY_CHARACTER:
91     case BLUESKY_SYMLINK:
92     case BLUESKY_SOCKET:
93     case BLUESKY_FIFO:
94         break;
95     }
96
97     return i;
98 }
99
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)
104 {
105     BlueSkyInode *inode = NULL;
106
107     g_mutex_lock(fs->lock);
108     inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
109     g_mutex_unlock(fs->lock);
110
111     return inode;
112 }
113
114 /* Insert an inode into the filesystem inode cache. */
115 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
116 {
117     g_mutex_lock(fs->lock);
118     g_hash_table_insert(fs->inodes, &inode->inum, inode);
119     g_mutex_unlock(fs->lock);
120 }
121
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)
125 {
126     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
127
128     if (size == inode->size)
129         return;
130
131     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_MAX_FILE_SIZE;
132
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);
142             g_free(b->ref);
143             g_free(b->data);
144         }
145         g_array_set_size(inode->blocks, blocks);
146     }
147
148     /* TODO: Zero out partial blocks if needed? */
149
150     inode->size = size;
151     inode->change_count++;
152 }