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 g_warning("Serialization for inode type %d not implemented!\n",
71 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
73 struct serialized_inode *raw = (struct serialized_inode *)buf;
75 if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
78 BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
79 fs, GUINT32_FROM_LE(raw->type));
81 inode->mode = GUINT32_FROM_LE(raw->mode);
82 inode->uid = GUINT32_FROM_LE(raw->uid);
83 inode->gid = GUINT32_FROM_LE(raw->gid);
84 inode->nlink = GUINT32_FROM_LE(raw->nlink);
85 inode->change_count = GUINT64_FROM_LE(raw->change_count);
86 inode->atime = GINT64_FROM_LE(raw->atime);
87 inode->ctime = GINT64_FROM_LE(raw->ctime);
88 inode->mtime = GINT64_FROM_LE(raw->mtime);
89 inode->ntime = GINT64_FROM_LE(raw->ntime);
91 buf += sizeof(struct serialized_inode);
93 switch (inode->type) {
95 g_array_set_size(inode->blocks,
96 (inode->size + BLUESKY_BLOCK_SIZE - 1)
97 / BLUESKY_BLOCK_SIZE);
98 for (int i = 0; i < inode->blocks->len; i++) {
99 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
100 b->type = BLUESKY_BLOCK_REF;
101 b->ref = g_strdup(buf);
102 buf += strlen(b->ref) + 1;
106 g_warning("Deserialization for inode type %d not implemented!\n",