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 #include "bluesky-private.h"
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
20 /* Magic signature and structure of serialized superblocks. */
22 #define SUPERBLOCK_MAGIC 0x65ca91e91b124234ULL
24 struct serialized_superblock {
25 uint64_t signature; /* SUPERBLOCK_MAGIC */
27 } __attribute__((packed));
29 /* Magic signature for serialized inodes. */
31 #define INODE_MAGIC 0xa6832100943d71e6ULL
33 struct serialized_inode {
34 uint64_t signature; /* INODE_MAGIC */
40 uint64_t change_count;
45 } __attribute__((packed));
47 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs)
49 struct serialized_superblock buf;
51 buf.signature = GUINT64_TO_LE(SUPERBLOCK_MAGIC);
52 buf.next_inum = GUINT64_TO_LE(fs->next_inum);
54 g_string_append_len(out, (gchar *)&buf, sizeof(buf));
57 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf)
59 struct serialized_superblock *raw = (struct serialized_superblock *)buf;
61 if (GUINT64_FROM_LE(raw->signature) != SUPERBLOCK_MAGIC)
64 BlueSkyFS *fs = bluesky_new_fs("deserialized");
65 fs->next_inum = GUINT64_FROM_LE(raw->next_inum);
70 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
72 struct serialized_inode buf;
74 buf.signature = GUINT64_TO_LE(INODE_MAGIC);
75 buf.type = GUINT32_TO_LE(inode->type);
76 buf.mode = GUINT32_TO_LE(inode->mode);
77 buf.uid = GUINT32_TO_LE(inode->uid);
78 buf.gid = GUINT32_TO_LE(inode->gid);
79 buf.nlink = GUINT32_TO_LE(inode->nlink);
80 buf.inum = GUINT64_TO_LE(inode->inum);
81 buf.change_count = GUINT64_TO_LE(inode->change_count);
82 buf.atime = GINT64_TO_LE(inode->atime);
83 buf.ctime = GINT64_TO_LE(inode->ctime);
84 buf.mtime = GINT64_TO_LE(inode->mtime);
85 buf.ntime = GINT64_TO_LE(inode->ntime);
87 g_string_append_len(out, (gchar *)&buf, sizeof(buf));
89 switch (inode->type) {
92 uint64_t size = GUINT64_TO_LE(inode->size);
93 g_string_append_len(out, (gchar *)&size, sizeof(uint64_t));
94 for (int i = 0; i < inode->blocks->len; i++) {
95 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
97 memset(&id, 0, sizeof(id));
98 if (b->cloudref != NULL)
100 g_string_append_len(out, (const char *)&id, sizeof(id));
105 case BLUESKY_DIRECTORY:
109 GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
111 while (!g_sequence_iter_is_end(i)) {
112 BlueSkyDirent *d = g_sequence_get(i);
114 seq = GUINT32_TO_LE(d->cookie);
115 inum = GUINT64_TO_LE(d->inum);
116 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
117 g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
118 g_string_append(out, d->name);
119 g_string_append_c(out, '\0');
121 i = g_sequence_iter_next(i);
124 seq = GUINT32_TO_LE(0);
125 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
130 case BLUESKY_SYMLINK:
132 g_string_append(out, inode->symlink_contents);
133 g_string_append_c(out, '\0');
138 g_warning("Serialization for inode type %d not implemented!\n",
143 /* Deserialize an inode into an in-memory representation. Returns a boolean
144 * indicating whether the deserialization was successful. */
145 gboolean bluesky_deserialize_inode(BlueSkyInode *inode, const gchar *buf)
147 if (bluesky_verbose) {
148 g_log("bluesky/serialize", G_LOG_LEVEL_DEBUG,
149 "Deserializing inode %lld...", (long long)inode->inum);
152 struct serialized_inode *raw = (struct serialized_inode *)buf;
154 if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
157 if (inode->inum != GUINT64_FROM_LE(raw->inum))
160 bluesky_init_inode(inode, GUINT32_FROM_LE(raw->type));
162 inode->mode = GUINT32_FROM_LE(raw->mode);
163 inode->uid = GUINT32_FROM_LE(raw->uid);
164 inode->gid = GUINT32_FROM_LE(raw->gid);
165 inode->nlink = GUINT32_FROM_LE(raw->nlink);
166 inode->change_count = GUINT64_FROM_LE(raw->change_count);
167 inode->change_commit = inode->change_count;
168 inode->atime = GINT64_FROM_LE(raw->atime);
169 inode->ctime = GINT64_FROM_LE(raw->ctime);
170 inode->mtime = GINT64_FROM_LE(raw->mtime);
171 inode->ntime = GINT64_FROM_LE(raw->ntime);
173 buf += sizeof(struct serialized_inode);
175 /* TODO: Bounds checking */
176 switch (inode->type) {
177 case BLUESKY_REGULAR:
178 inode->size = GINT64_FROM_LE(*(uint64_t *)buf);
179 buf += sizeof(uint64_t);
180 g_array_set_size(inode->blocks,
181 (inode->size + BLUESKY_BLOCK_SIZE - 1)
182 / BLUESKY_BLOCK_SIZE);
185 for (int i = 0; i < inode->blocks->len; i++) {
186 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
187 b->type = BLUESKY_BLOCK_REF;
188 b->ref = g_strdup(buf);
189 buf += strlen(b->ref) + 1;
194 case BLUESKY_DIRECTORY:
196 struct serialized_dirent {
200 } __attribute__((packed));
202 struct serialized_dirent *d = (struct serialized_dirent *)buf;
203 while (GUINT32_FROM_LE(d->seq) != 0) {
204 BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
205 dirent->cookie = GUINT64_FROM_LE(d->seq);
206 dirent->inum = GUINT64_FROM_LE(d->inum);
207 dirent->name = g_strdup(d->name);
208 dirent->name_folded = bluesky_lowercase(d->name);
210 g_sequence_insert_sorted(inode->dirents, dirent,
211 bluesky_dirent_compare, NULL);
212 g_hash_table_insert(inode->dirhash, dirent->name, dirent);
213 g_hash_table_insert(inode->dirhash_folded, dirent->name_folded,
216 buf = strchr(d->name, '\0') + 1;
217 d = (struct serialized_dirent *)buf;
222 case BLUESKY_SYMLINK:
224 inode->symlink_contents = g_strdup(buf);
229 g_warning("Deserialization for inode type %d not implemented!\n",