Preliminary symlink/readlink support.
[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 <inttypes.h>
11 #include <glib.h>
12 #include <string.h>
13
14 #include "bluesky-private.h"
15
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
18  * format. */
19
20 /* Magic signature and structure of serialized superblocks. */
21
22 #define SUPERBLOCK_MAGIC 0x65ca91e91b124234ULL
23
24 struct serialized_superblock {
25     uint64_t signature;         /* SUPERBLOCK_MAGIC */
26     uint64_t next_inum;
27 } __attribute__((packed));
28
29 /* Magic signature for serialized inodes. */
30
31 #define INODE_MAGIC 0xa6832100943d71e6ULL
32
33 struct serialized_inode {
34     uint64_t signature;         /* INODE_MAGIC */
35     int32_t type;
36     uint32_t mode;
37     uint32_t uid, gid;
38     uint32_t nlink;
39     uint64_t inum;
40     uint64_t change_count;
41     int64_t atime;
42     int64_t ctime;
43     int64_t mtime;
44     int64_t ntime;
45 } __attribute__((packed));
46
47 void bluesky_serialize_superblock(GString *out, BlueSkyFS *fs)
48 {
49     struct serialized_superblock buf;
50
51     buf.signature = GUINT64_TO_LE(SUPERBLOCK_MAGIC);
52     buf.next_inum = GUINT64_TO_LE(fs->next_inum);
53
54     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
55 }
56
57 BlueSkyFS *bluesky_deserialize_superblock(const gchar *buf)
58 {
59     struct serialized_superblock *raw = (struct serialized_superblock *)buf;
60
61     if (GUINT64_FROM_LE(raw->signature) != SUPERBLOCK_MAGIC)
62         return NULL;
63
64     BlueSkyFS *fs = bluesky_new_fs("deserialized");
65     fs->next_inum = GUINT64_FROM_LE(raw->next_inum);
66
67     return fs;
68 }
69
70 void bluesky_serialize_inode(GString *out, BlueSkyInode *inode)
71 {
72     struct serialized_inode buf;
73
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);
86
87     g_string_append_len(out, (gchar *)&buf, sizeof(buf));
88
89     switch (inode->type) {
90     case BLUESKY_REGULAR:
91     {
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);
96             if (b->ref != NULL)
97                 g_string_append(out, b->ref);
98             g_string_append_c(out, '\0');
99         }
100         break;
101     }
102
103     case BLUESKY_DIRECTORY:
104     {
105         uint32_t seq;
106         uint64_t inum;
107         GSequenceIter *i = g_sequence_get_begin_iter(inode->dirents);
108
109         while (!g_sequence_iter_is_end(i)) {
110             BlueSkyDirent *d = g_sequence_get(i);
111
112             seq = GUINT32_TO_LE(d->cookie);
113             inum = GUINT64_TO_LE(d->inum);
114             g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
115             g_string_append_len(out, (gchar *)&inum, sizeof(uint64_t));
116             g_string_append(out, d->name);
117             g_string_append_c(out, '\0');
118
119             i = g_sequence_iter_next(i);
120         }
121
122         seq = GUINT32_TO_LE(0);
123         g_string_append_len(out, (gchar *)&seq, sizeof(uint32_t));
124
125         break;
126     }
127
128     case BLUESKY_SYMLINK:
129     {
130         g_string_append(out, inode->symlink_contents);
131         g_string_append_c(out, '\0');
132     }
133
134     default:
135         g_warning("Serialization for inode type %d not implemented!\n",
136                   inode->type);
137     }
138 }
139
140 BlueSkyInode *bluesky_deserialize_inode(BlueSkyFS *fs, const gchar *buf)
141 {
142     struct serialized_inode *raw = (struct serialized_inode *)buf;
143
144     if (GUINT64_FROM_LE(raw->signature) != INODE_MAGIC)
145         return NULL;
146
147     BlueSkyInode *inode = bluesky_new_inode(GUINT64_FROM_LE(raw->inum),
148                                             fs, GUINT32_FROM_LE(raw->type));
149
150     inode->mode = GUINT32_FROM_LE(raw->mode);
151     inode->uid = GUINT32_FROM_LE(raw->uid);
152     inode->gid = GUINT32_FROM_LE(raw->gid);
153     inode->nlink = GUINT32_FROM_LE(raw->nlink);
154     inode->change_count = GUINT64_FROM_LE(raw->change_count);
155     inode->atime = GINT64_FROM_LE(raw->atime);
156     inode->ctime = GINT64_FROM_LE(raw->ctime);
157     inode->mtime = GINT64_FROM_LE(raw->mtime);
158     inode->ntime = GINT64_FROM_LE(raw->ntime);
159
160     buf += sizeof(struct serialized_inode);
161
162     g_print("Deserializing inode %lld...\n", (long long)inode->inum);
163
164     /* TODO: Bounds checking */
165     switch (inode->type) {
166     case BLUESKY_REGULAR:
167         inode->size = GINT64_FROM_LE(*(uint64_t *)buf);
168         buf += sizeof(uint64_t);
169         g_array_set_size(inode->blocks,
170                          (inode->size + BLUESKY_BLOCK_SIZE - 1)
171                           / BLUESKY_BLOCK_SIZE);
172         for (int i = 0; i < inode->blocks->len; i++) {
173             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
174             b->type = BLUESKY_BLOCK_REF;
175             b->ref = g_strdup(buf);
176             buf += strlen(b->ref) + 1;
177         }
178         break;
179
180     case BLUESKY_DIRECTORY:
181     {
182         struct serialized_dirent {
183             uint32_t seq;
184             uint64_t inum;
185             gchar name[0];
186         } __attribute__((packed));
187
188         struct serialized_dirent *d = (struct serialized_dirent *)buf;
189         while (GUINT32_FROM_LE(d->seq) != 0) {
190             BlueSkyDirent *dirent = g_new(BlueSkyDirent, 1);
191             dirent->cookie = GUINT64_FROM_LE(d->seq);
192             dirent->inum = GUINT64_FROM_LE(d->inum);
193             dirent->name = g_strdup(d->name);
194             dirent->name_folded = bluesky_lowercase(d->name);
195
196             g_sequence_insert_sorted(inode->dirents, dirent,
197                                      bluesky_dirent_compare, NULL);
198             g_hash_table_insert(inode->dirhash, dirent->name, dirent);
199             g_hash_table_insert(inode->dirhash_folded, dirent->name_folded,
200                                 dirent);
201
202             g_print("  dirent[%08x]: %s -> %"PRIu64"\n",
203                     dirent->cookie, dirent->name, dirent->inum);
204
205             buf = strchr(d->name, '\0') + 1;
206             d = (struct serialized_dirent *)buf;
207         }
208         break;
209     }
210
211     case BLUESKY_SYMLINK:
212     {
213         inode->symlink_contents = g_strdup(buf);
214     }
215
216     default:
217         g_warning("Deserialization for inode type %d not implemented!\n",
218                   inode->type);
219     }
220
221     return inode;
222 }