Update logic for flushing data to cloud.
[bluesky.git] / bluesky / serialize.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 <inttypes.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15
16 /* Serialization of in-memory filesystem data structures to bytestrings which
17  * can be written to persistent storage.  All data is stored in little-endian
18  * format. */
19
20 /* Magic signature and structure of serialized superblocks. */
21
22 #define SUPERBLOCK_MAGIC 0x65ca91e91b124234ULL
23
24 struct serialized_superblock {
25     uint64_t signature;         /* SUPERBLOCK_MAGIC */
26     uint64_t next_inum;
27 } __attribute__((packed));
28
29 /* Magic signature for serialized inodes. */
30
31 #define INODE_MAGIC 0xa6832100943d71e6ULL
32
33 struct serialized_inode {
34     uint64_t signature;         /* INODE_MAGIC */
35     int32_t type;
36     uint32_t mode;
37     uint32_t uid, gid;
38     uint32_t nlink;
39     uint64_t inum;
40     uint64_t change_count;
41     int64_t atime;
42     int64_t ctime;
43     int64_t mtime;
44     int64_t ntime;
45 } __attribute__((packed));
46
47 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs)
48 {
49     struct serialized_superblock buf;
50
51     buf.signature = GUINT64_TO_LE(SUPERBLOCK_MAGIC);
52     buf.next_inum = GUINT64_TO_LE(fs->next_inum);
53
54     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
55 }
56
57 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf)
58 {
59     struct serialized_superblock *raw = (struct serialized_superblock *)buf;
60
61     if (GUINT64_FROM_LE(raw->signature) != SUPERBLOCK_MAGIC)
62         return NULL;
63
64     BlueSkyFS *fs = bluesky_new_fs("deserialized");
65     fs->next_inum = GUINT64_FROM_LE(raw->next_inum);
66
67     return fs;
68 }
69
70 BlueSkyCloudLog *bluesky_serialize_inode(BlueSkyInode *inode)
71 {
72     BlueSkyFS *fs = inode->fs;
73     GString *out = g_string_new("");
74     struct serialized_inode buf;
75
76     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
77     cloudlog->type = LOGTYPE_INODE;
78     cloudlog->inum = inode->inum;
79
80     buf.signature = GUINT64_TO_LE(INODE_MAGIC);
81     buf.type = GUINT32_TO_LE(inode->type);
82     buf.mode = GUINT32_TO_LE(inode->mode);
83     buf.uid = GUINT32_TO_LE(inode->uid);
84     buf.gid = GUINT32_TO_LE(inode->gid);
85     buf.nlink = GUINT32_TO_LE(inode->nlink);
86     buf.inum = GUINT64_TO_LE(inode->inum);
87     buf.change_count = GUINT64_TO_LE(inode->change_count);
88     buf.atime = GINT64_TO_LE(inode->atime);
89     buf.ctime = GINT64_TO_LE(inode->ctime);
90     buf.mtime = GINT64_TO_LE(inode->mtime);
91     buf.ntime = GINT64_TO_LE(inode->ntime);
92
93     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
94
95     switch (inode->type) {
96     case BLUESKY_REGULAR:
97     {
98         uint64_t size = GUINT64_TO_LE(inode->size);
99         g_string_append_len(out, (gchar *)&size, sizeof(uint64_t));
100         for (int i = 0; i < inode->blocks->len; i++) {
101             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
102             BlueSkyCloudLog *ref = (b->type == BLUESKY_BLOCK_REF ? b->ref : NULL);
103             bluesky_cloudlog_ref(ref);
104             g_array_append_val(cloudlog->links, ref);
105         }
106         break;
107     }
108
109     case BLUESKY_DIRECTORY:
110     {
111         uint32_t seq;
112         uint64_t inum;
113         GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
114
115         while (!g_sequence_iter_is_end(i)) {
116             BlueSkyDirent *d = g_sequence_get(i);
117
118             seq = GUINT32_TO_LE(d->cookie);
119             inum = GUINT64_TO_LE(d->inum);
120             g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
121             g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
122             g_string_append(out, d->name);
123             g_string_append_c(out, '\0');
124
125             i = g_sequence_iter_next(i);
126         }
127
128         seq = GUINT32_TO_LE(0);
129         g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
130
131         break;
132     }
133
134     case BLUESKY_SYMLINK:
135     {
136         g_string_append(out, inode->symlink_contents);
137         g_string_append_c(out, '\0');
138         break;
139     }
140
141     default:
142         g_warning("Serialization for inode type %d not implemented!\n",
143                   inode->type);
144     }
145
146     cloudlog->data = bluesky_string_new_from_gstring(out);
147     bluesky_cloudlog_insert(cloudlog);
148     bluesky_cloudlog_stats_update(cloudlog, 1);
149
150     return cloudlog;
151 }
152
153 /* Deserialize an inode into an in-memory representation.  Returns a boolean
154  * indicating whether the deserialization was successful. */
155 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
156 {
157     if (bluesky_verbose) {
158         g_log("bluesky/serialize", G_LOG_LEVEL_DEBUG,
159               "Deserializing inode %lld...", (long long)inode->inum);
160     }
161
162     struct serialized_inode *raw = (struct serialized_inode *)buf;
163
164     if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
165         return FALSE;
166
167     if (inode->inum != GUINT64_FROM_LE(raw->inum))
168         return FALSE;
169
170     bluesky_init_inode(inode, GUINT32_FROM_LE(raw->type));
171
172     inode->mode = GUINT32_FROM_LE(raw->mode);
173     inode->uid = GUINT32_FROM_LE(raw->uid);
174     inode->gid = GUINT32_FROM_LE(raw->gid);
175     inode->nlink = GUINT32_FROM_LE(raw->nlink);
176     inode->change_count = GUINT64_FROM_LE(raw->change_count);
177     inode->change_commit = inode->change_count;
178     inode->atime = GINT64_FROM_LE(raw->atime);
179     inode->ctime = GINT64_FROM_LE(raw->ctime);
180     inode->mtime = GINT64_FROM_LE(raw->mtime);
181     inode->ntime = GINT64_FROM_LE(raw->ntime);
182
183     buf += sizeof(struct serialized_inode);
184
185     /* TODO: Bounds checking */
186     switch (inode->type) {
187     case BLUESKY_REGULAR:
188         inode->size = GINT64_FROM_LE(*(uint64_t *)buf);
189         buf += sizeof(uint64_t);
190         g_array_set_size(inode->blocks,
191                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
192                           / BLUESKY_BLOCK_SIZE);
193         // TODO
194 #if 0
195         for (int i = 0; i < inode->blocks->len; i++) {
196             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
197             b->type = BLUESKY_BLOCK_REF;
198             b->ref = g_strdup(buf);
199             buf += strlen(b->ref) + 1;
200         }
201 #endif
202         break;
203
204     case BLUESKY_DIRECTORY:
205     {
206         struct serialized_dirent {
207             uint32_t seq;
208             uint64_t inum;
209             gchar name[0];
210         } __attribute__((packed));
211
212         struct serialized_dirent *d = (struct serialized_dirent *)buf;
213         while (GUINT32_FROM_LE(d->seq) != 0) {
214             BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
215             dirent->cookie = GUINT64_FROM_LE(d->seq);
216             dirent->inum = GUINT64_FROM_LE(d->inum);
217             dirent->name = g_strdup(d->name);
218             dirent->name_folded = bluesky_lowercase(d->name);
219
220             g_sequence_insert_sorted(inode->dirents, dirent,
221                                      bluesky_dirent_compare, NULL);
222             g_hash_table_insert(inode->dirhash, dirent->name, dirent);
223             g_hash_table_insert(inode->dirhash_folded, dirent->name_folded,
224                                 dirent);
225
226             buf = strchr(d->name, '\0') + 1;
227             d = (struct serialized_dirent *)buf;
228         }
229         break;
230     }
231
232     case BLUESKY_SYMLINK:
233     {
234         inode->symlink_contents = g_strdup(buf);
235         break;
236     }
237
238     default:
239         g_warning("Deserialization for inode type %d not implemented!\n",
240                   inode->type);
241     }
242
243     return TRUE;
244 }