eaa9258442187e32099b3e6fa4431ace48fa298b
[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 <glib.h>
11 #include <string.h>
12
13 #include "bluesky.h"
14
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
17  * format. */
18
19 /* Magic signature for serialized inodes. */
20
21 #define INODE_MAGIC 0xa6832100943d71e6ULL
22
23 struct serialized_inode {
24     uint64_t signature;         /* INODE_MAGIC */
25     int32_t type;
26     uint32_t mode;
27     uint32_t uid, gid;
28     uint32_t nlink;
29     uint64_t inum;
30     uint64_t change_count;
31     int64_t atime;
32     int64_t ctime;
33     int64_t mtime;
34     int64_t ntime;
35 } __attribute__((packed));
36
37 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
38 {
39     struct serialized_inode buf;
40
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);
53
54     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
55
56     switch (inode->type) {
57     case BLUESKY_REGULAR:
58     {
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);
63             if (b->ref != NULL)
64                 g_string_append(out, b->ref);
65             g_string_append_c(out, '\0');
66         }
67         break;
68     }
69
70     case BLUESKY_DIRECTORY:
71     {
72         uint32_t seq;
73         uint64_t inum;
74         GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
75
76         while (!g_sequence_iter_is_end(i)) {
77             BlueSkyDirent *d = g_sequence_get(i);
78
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');
85
86             i = g_sequence_iter_next(i);
87         }
88
89         seq = GUINT32_TO_LE(0);
90         g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
91
92         break;
93     }
94     default:
95         g_warning("Serialization for inode type %d not implemented!\n",
96                   inode->type);
97     }
98 }
99
100 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
101 {
102     struct serialized_inode *raw = (struct serialized_inode *)buf;
103
104     if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
105         return NULL;
106
107     BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
108                                             fs, GUINT32_FROM_LE(raw->type));
109
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);
119
120     buf += sizeof(struct serialized_inode);
121
122     g_print("Deserializing inode %lld...\n", (long long)inode->inum);
123
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;
137         }
138         break;
139
140     case BLUESKY_DIRECTORY:
141     {
142         struct serialized_dirent {
143             uint32_t seq;
144             uint64_t inum;
145             gchar name[0];
146         } __attribute__((packed));
147
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);
154
155             g_sequence_insert_sorted(inode->dirents, dirent,
156                                      bluesky_dirent_compare, NULL);
157             g_hash_table_insert(inode->dirhash, dirent->name, dirent);
158
159             g_print("  dirent[%08x]: %s -> %lld\n",
160                     dirent->cookie, dirent->name, dirent->inum);
161
162             buf = strchr(d->name, '\0') + 1;
163             d = (struct serialized_dirent *)buf;
164         }
165         break;
166     }
167     default:
168         g_warning("Deserialization for inode type %d not implemented!\n",
169                   inode->type);
170     }
171
172     return inode;
173 }