File read and write operations.
[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 #include <string.h>
12
13 #include "bluesky.h"
14
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. */
19
20 /* Return the current time, in microseconds since the epoch. */
21 int64_t bluesky_get_current_time()
22 {
23     GTimeVal t;
24     g_get_current_time(&t);
25     return (int64_t)t.tv_sec * 1000000 + t.tv_usec;
26 }
27
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)
32 {
33     int64_t now = bluesky_get_current_time();
34     inode->change_count++;
35     inode->ctime = now;
36     if (update_mtime)
37         inode->mtime = now;
38 }
39
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)
47 {
48     uint64_t inum = *(const uint64_t *)key;
49     return (guint)inum;
50 }
51
52 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
53 {
54     uint64_t i1 = *(const uint64_t *)a;
55     uint64_t i2 = *(const uint64_t *)b;
56     return i1 == i2;
57 }
58
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)
62 {
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;
69
70     return fs;
71 }
72
73 /* Allocate a fresh inode number which has not been used before within a
74  * filesystem. */
75 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
76 {
77     uint64_t inum;
78
79     g_mutex_lock(fs->lock);
80     inum = fs->next_inum;
81     fs->next_inum++;
82     g_mutex_unlock(fs->lock);
83
84     return inum;
85 }
86
87 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
88 {
89     BlueSkyInode *i = g_new0(BlueSkyInode, 1);
90
91     i->lock = g_mutex_new();
92     i->type = type;
93     i->inum = inum;
94
95     switch (type) {
96     case BLUESKY_REGULAR:
97         i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
98         break;
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);
102         break;
103     case BLUESKY_BLOCK:
104     case BLUESKY_CHARACTER:
105     case BLUESKY_SYMLINK:
106     case BLUESKY_SOCKET:
107     case BLUESKY_FIFO:
108         break;
109     }
110
111     return i;
112 }
113
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)
118 {
119     BlueSkyInode *inode = NULL;
120
121     g_mutex_lock(fs->lock);
122     inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
123     g_mutex_unlock(fs->lock);
124
125     return inode;
126 }
127
128 /* Insert an inode into the filesystem inode cache. */
129 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
130 {
131     g_mutex_lock(fs->lock);
132     g_hash_table_insert(fs->inodes, &inode->inum, inode);
133     g_mutex_unlock(fs->lock);
134 }
135
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)
139 {
140     g_return_if_fail(i < inode->blocks->len);
141     BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
142
143     switch (block->type) {
144     case BLUESKY_BLOCK_ZERO:
145         block->data = g_malloc0(BLUESKY_BLOCK_SIZE);
146         break;
147     case BLUESKY_BLOCK_REF:
148         /* TODO: Pull in data first */
149         block->data = g_malloc0(BLUESKY_BLOCK_SIZE);
150         break;
151     case BLUESKY_BLOCK_CACHED:
152     case BLUESKY_BLOCK_DIRTY:
153         break;
154     }
155
156     block->type = BLUESKY_BLOCK_DIRTY;
157 }
158
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)
162 {
163     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
164
165     if (size == inode->size)
166         return;
167
168     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
169
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);
179             g_free(b->ref);
180             g_free(b->data);
181         }
182         g_array_set_size(inode->blocks, blocks);
183     }
184
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,
189                                          blocks - 1);
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);
196             }
197         }
198     }
199
200     inode->size = size;
201     bluesky_inode_update_ctime(inode, 1);
202 }
203
204 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
205                         const char *data, gint len)
206 {
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);
210
211     if (len == 0)
212         return;
213
214     while (len > 0) {
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);
218
219         bluesky_block_touch(inode, block_num);
220         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
221                                          block_num);
222         memcpy(&b->data[block_offset], data, bytes);
223
224         offset += bytes;
225         data += bytes;
226         len -= bytes;
227     }
228
229     bluesky_inode_update_ctime(inode, 1);
230 }
231
232 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
233                        char *buf, gint len)
234 {
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);
238
239     while (len > 0) {
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);
243
244         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
245                                          block_num);
246         switch (b->type) {
247         case BLUESKY_BLOCK_ZERO:
248             memset(buf, 0, bytes);
249             break;
250         case BLUESKY_BLOCK_REF:
251             /* TODO: Pull in data first */
252             memset(buf, 0, bytes);
253             break;
254         case BLUESKY_BLOCK_CACHED:
255         case BLUESKY_BLOCK_DIRTY:
256             memcpy(buf, &b->data[block_offset], bytes);
257             break;
258         }
259
260         offset += bytes;
261         buf += bytes;
262         len -= bytes;
263     }
264 }