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 /* Serialization of in-memory filesystem data structures to bytestrings which
16 * can be written to persistent storage. All data is stored in little-endian
19 /* Magic signature for serialized inodes. */
21 #define INODE_MAGIC 0xa6832100943d71e5ULL
23 struct serialized_inode {
24 uint64_t signature; /* INODE_MAGIC */
30 uint64_t change_count;
35 } __attribute__((packed));
37 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
39 struct serialized_inode buf;
41 buf.signature = GUINT64_TO_LE(INODE_MAGIC);
42 buf.type = GUINT32_TO_LE(inode->type);
43 buf.mode = GUINT32_TO_LE(inode->mode);
44 buf.uid = GUINT32_TO_LE(inode->uid);
45 buf.gid = GUINT32_TO_LE(inode->gid);
46 buf.nlink = GUINT32_TO_LE(inode->nlink);
47 buf.inum = GUINT64_TO_LE(inode->inum);
48 buf.change_count = GUINT64_TO_LE(inode->change_count);
49 buf.atime = GINT64_TO_LE(inode->atime);
50 buf.ctime = GINT64_TO_LE(inode->ctime);
51 buf.mtime = GINT64_TO_LE(inode->mtime);
52 buf.ntime = GINT64_TO_LE(inode->ntime);
54 g_string_append_len(out, (gchar *)&buf, sizeof(buf));
56 switch (inode->type) {
58 for (int i = 0; i < inode->blocks->len; i++) {
59 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
61 g_string_append(out, b->ref);
62 g_string_append_c(out, '\0');
66 case BLUESKY_DIRECTORY:
70 GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
72 while (!g_sequence_iter_is_end(i)) {
73 BlueSkyDirent *d = g_sequence_get(i);
75 seq = GUINT32_TO_LE(d->cookie);
76 inum = GUINT64_TO_LE(d->inum);
77 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
78 g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
79 g_string_append(out, d->name);
80 g_string_append_c(out, '\0');
82 i = g_sequence_iter_next(i);
85 seq = GUINT32_TO_LE(0);
86 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
91 g_warning("Serialization for inode type %d not implemented!\n",
96 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
98 struct serialized_inode *raw = (struct serialized_inode *)buf;
100 if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
103 BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
104 fs, GUINT32_FROM_LE(raw->type));
106 inode->mode = GUINT32_FROM_LE(raw->mode);
107 inode->uid = GUINT32_FROM_LE(raw->uid);
108 inode->gid = GUINT32_FROM_LE(raw->gid);
109 inode->nlink = GUINT32_FROM_LE(raw->nlink);
110 inode->change_count = GUINT64_FROM_LE(raw->change_count);
111 inode->atime = GINT64_FROM_LE(raw->atime);
112 inode->ctime = GINT64_FROM_LE(raw->ctime);
113 inode->mtime = GINT64_FROM_LE(raw->mtime);
114 inode->ntime = GINT64_FROM_LE(raw->ntime);
116 buf += sizeof(struct serialized_inode);
118 g_print("Deserializing inode %lld...\n", (long long)inode->inum);
120 /* TODO: Bounds checking */
121 switch (inode->type) {
122 case BLUESKY_REGULAR:
123 g_array_set_size(inode->blocks,
124 (inode->size + BLUESKY_BLOCK_SIZE - 1)
125 / BLUESKY_BLOCK_SIZE);
126 for (int i = 0; i < inode->blocks->len; i++) {
127 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
128 b->type = BLUESKY_BLOCK_REF;
129 b->ref = g_strdup(buf);
130 buf += strlen(b->ref) + 1;
134 case BLUESKY_DIRECTORY:
136 struct serialized_dirent {
140 } __attribute__((packed));
142 struct serialized_dirent *d = (struct serialized_dirent *)buf;
143 while (GUINT32_FROM_LE(d->seq) != 0) {
144 BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
145 dirent->cookie = GUINT64_FROM_LE(d->seq);
146 dirent->inum = GUINT64_FROM_LE(d->inum);
147 dirent->name = g_strdup(d->name);
149 g_sequence_insert_sorted(inode->dirents, dirent,
150 bluesky_dirent_compare, NULL);
151 g_hash_table_insert(inode->dirhash, dirent->name, dirent);
153 g_print(" dirent[%08x]: %s -> %lld\n",
154 dirent->cookie, dirent->name, dirent->inum);
156 buf = strchr(d->name, '\0') + 1;
157 d = (struct serialized_dirent *)buf;
162 g_warning("Deserialization for inode type %d not implemented!\n",