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 0xa6832100943d71e6ULL
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) {
59 uint64_t size = GUINT64_TO_LE(inode->size);
60 g_string_append_len(out, (gchar *)&size, sizeof(uint64_t));
61 for (int i = 0; i < inode->blocks->len; i++) {
62 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
64 g_string_append(out, b->ref);
65 g_string_append_c(out, '\0');
70 case BLUESKY_DIRECTORY:
74 GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
76 while (!g_sequence_iter_is_end(i)) {
77 BlueSkyDirent *d = g_sequence_get(i);
79 seq = GUINT32_TO_LE(d->cookie);
80 inum = GUINT64_TO_LE(d->inum);
81 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
82 g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
83 g_string_append(out, d->name);
84 g_string_append_c(out, '\0');
86 i = g_sequence_iter_next(i);
89 seq = GUINT32_TO_LE(0);
90 g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
95 g_warning("Serialization for inode type %d not implemented!\n",
100 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
102 struct serialized_inode *raw = (struct serialized_inode *)buf;
104 if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
107 BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
108 fs, GUINT32_FROM_LE(raw->type));
110 inode->mode = GUINT32_FROM_LE(raw->mode);
111 inode->uid = GUINT32_FROM_LE(raw->uid);
112 inode->gid = GUINT32_FROM_LE(raw->gid);
113 inode->nlink = GUINT32_FROM_LE(raw->nlink);
114 inode->change_count = GUINT64_FROM_LE(raw->change_count);
115 inode->atime = GINT64_FROM_LE(raw->atime);
116 inode->ctime = GINT64_FROM_LE(raw->ctime);
117 inode->mtime = GINT64_FROM_LE(raw->mtime);
118 inode->ntime = GINT64_FROM_LE(raw->ntime);
120 buf += sizeof(struct serialized_inode);
122 g_print("Deserializing inode %lld...\n", (long long)inode->inum);
124 /* TODO: Bounds checking */
125 switch (inode->type) {
126 case BLUESKY_REGULAR:
127 inode->size = GINT64_FROM_LE(*(uint64_t *)buf);
128 buf += sizeof(uint64_t);
129 g_array_set_size(inode->blocks,
130 (inode->size + BLUESKY_BLOCK_SIZE - 1)
131 / BLUESKY_BLOCK_SIZE);
132 for (int i = 0; i < inode->blocks->len; i++) {
133 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
134 b->type = BLUESKY_BLOCK_REF;
135 b->ref = g_strdup(buf);
136 buf += strlen(b->ref) + 1;
140 case BLUESKY_DIRECTORY:
142 struct serialized_dirent {
146 } __attribute__((packed));
148 struct serialized_dirent *d = (struct serialized_dirent *)buf;
149 while (GUINT32_FROM_LE(d->seq) != 0) {
150 BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
151 dirent->cookie = GUINT64_FROM_LE(d->seq);
152 dirent->inum = GUINT64_FROM_LE(d->inum);
153 dirent->name = g_strdup(d->name);
155 g_sequence_insert_sorted(inode->dirents, dirent,
156 bluesky_dirent_compare, NULL);
157 g_hash_table_insert(inode->dirhash, dirent->name, dirent);
159 g_print(" dirent[%08x]: %s -> %lld\n",
160 dirent->cookie, dirent->name, dirent->inum);
162 buf = strchr(d->name, '\0') + 1;
163 d = (struct serialized_dirent *)buf;
168 g_warning("Deserialization for inode type %d not implemented!\n",