1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2010 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
16 #include "bluesky-private.h"
18 /* Proxy component of the file system cleaner. This consists effectively of
19 * code for merging multiple versions of the file system logs: that generated
20 * by us and that generated by the in-cloud cleaner. Other logic, such as for
21 * rewriting log segments where needed and deleting old segments, is handled by
22 * the in-cloud cleaner component. */
24 /* A specialized function for loading an object from the cloud, used just by
25 * the cleaner. When the cleaner runs, it will produce new objects that have
26 * the same object ID as before, differing only in the outgoing link locations
27 * as a result of cleaning. We can't use the normal functions for loading
28 * objects since those can only deal with one version of an object. Instead,
29 * we load the cleaned object with this function, and then merge the state into
30 * the official object as needed.
32 * The bluesky_cleaner_deserialize function is like
33 * bluesky_deserialize_cloudlog, but does a more limited deserialization and
34 * keeps the returned item entirely separate from any in-memory BlueSkyCloudLog
38 BlueSkyCloudLogType type;
40 BlueSkyCloudPointer location;
47 BlueSkyCloudPointer location;
50 static BlueSkyCleanerItem *bluesky_cleaner_deserialize(BlueSkyRCStr *raw)
52 const char *data = raw->data;
53 size_t len = raw->len;
54 const char *data1, *data2, *data3;
55 size_t len1, len2, len3;
58 if (len < sizeof(struct cloudlog_header)
59 || memcmp(data, CLOUDLOG_MAGIC, 4) != 0)
61 g_warning("Deserializing garbage cloud log item from cleaner!");
65 struct cloudlog_header *header = (struct cloudlog_header *)data;
66 len1 = GUINT32_FROM_LE(header->size1);
67 len2 = GUINT32_FROM_LE(header->size2);
68 len3 = GUINT32_FROM_LE(header->size3);
69 data1 = data + sizeof(struct cloudlog_header);
72 g_assert(data3 + len3 - data <= len);
74 BlueSkyCleanerItem *item = g_new0(BlueSkyCleanerItem, 1);
75 item->type = header->type - '0';
76 item->inum = GUINT64_FROM_LE(header->inum);
77 memcpy(&item->id, &header->id, sizeof(BlueSkyCloudID));
79 int link_count = len2 / sizeof(BlueSkyCloudID);
80 g_print("Outgoing links: %d\n", link_count);
81 item->links = g_array_new(FALSE, TRUE, sizeof(BlueSkyCleanerLink));
82 for (int i = 0; i < link_count; i++) {
83 BlueSkyCleanerLink link;
85 g_assert(len2 >= sizeof(link.id));
86 memcpy(&link.id, data2, sizeof(link.id));
87 data2 += sizeof(link.id); len2 -= sizeof(link.id);
89 g_assert(len3 >= sizeof(link.location));
90 memcpy(&link.location, data3, sizeof(link.location));
91 data3 += sizeof(link.location); len3 -= sizeof(link.location);
93 g_array_append_val(item->links, link);
99 static void bluesky_cleaner_item_free(BlueSkyCleanerItem *item)
103 g_array_unref(item->links);
107 /* Check the cleaner's logs to find the a more recent checkpoint record. This
108 * should be called occasionally to see if the cleaner has done any work since
110 static BlueSkyCleanerItem *bluesky_cleaner_find_checkpoint(BlueSkyFS *fs)
112 char *prefix = g_strdup_printf("log-%08d", BLUESKY_CLOUD_DIR_CLEANER);
113 char *last_segment = bluesky_store_lookup_last(fs->store, prefix);
115 if (last_segment == NULL)
118 g_print("Last cloud log segment: %s\n", last_segment);
119 int seq = atoi(last_segment + 13);
120 g_free(last_segment);
122 if (seq <= fs->log_state->latest_cleaner_seq_seen)
125 g_print("New log segment appeared in cleaner directory: %d\n", seq);
127 BlueSkyCacheFile *cachefile;
128 cachefile = bluesky_cachefile_lookup(fs, BLUESKY_CLOUD_DIR_CLEANER, seq,
130 while (!cachefile->complete)
131 g_cond_wait(cachefile->cond, cachefile->lock);
133 g_print("Downloaded latest cleaner segment.\n");
135 int64_t offset = -1, length = 0;
137 const BlueSkyRangesetItem *item;
138 item = bluesky_rangeset_lookup_next(cachefile->items, offset + 1);
141 offset = item->start;
142 length = item->length;
146 bluesky_cachefile_unref(cachefile);
147 g_mutex_unlock(cachefile->lock);
151 g_print("Found a cleaner checkpoint record.\n");
153 BlueSkyRCStr *data = bluesky_cachefile_map_raw(cachefile, offset, length);
154 bluesky_cachefile_unref(cachefile);
155 g_mutex_unlock(cachefile->lock);
157 BlueSkyCleanerItem *checkpoint = bluesky_cleaner_deserialize(data);
158 bluesky_string_unref(data);
163 static BlueSkyCleanerItem *cleaner_load_item(BlueSkyFS *fs,
164 BlueSkyCloudPointer location)
166 g_print("Loading item %d/%d/%d...\n", location.directory, location.sequence, location.offset);
168 BlueSkyCacheFile *cachefile;
169 cachefile = bluesky_cachefile_lookup(fs, location.directory,
170 location.sequence, TRUE);
171 while (!cachefile->complete)
172 g_cond_wait(cachefile->cond, cachefile->lock);
174 /* TODO: Ought to check that we are loading an item which validated? */
175 BlueSkyRCStr *data = bluesky_cachefile_map_raw(cachefile, location.offset,
177 bluesky_cachefile_unref(cachefile);
178 g_mutex_unlock(cachefile->lock);
180 BlueSkyCleanerItem *item = bluesky_cleaner_deserialize(data);
181 bluesky_string_unref(data);
186 /* Does the item at the given cloud location from the cleaner need merging? An
187 * item in the primary log does not need to be merged, as by definition we
188 * already know about it. Similarly, old items in the cleaner's log--those
189 * that we have already seen from a previous merge--do not need to be mergd
191 gboolean needs_merging(BlueSkyFS *fs, BlueSkyCloudPointer location)
193 if (location.directory == BLUESKY_CLOUD_DIR_PRIMARY)
196 if (location.directory == BLUESKY_CLOUD_DIR_CLEANER
197 && location.sequence <= fs->log_state->latest_cleaner_seq_seen)
203 static void merge_inode(BlueSkyFS *fs, BlueSkyCleanerItem *cleaner_inode)
205 /* There are two versions we are concerned with: cleaner_ is for the data
206 * stored in the cleaner's log, and proxy_ is for our most recent version,
207 * which the cleaner might or might not know about. */
208 uint64_t inum = cleaner_inode->inum;
210 g_print("Merging inode %"PRIu64" from cleaner\n", inum);
212 g_mutex_lock(fs->lock);
213 InodeMapEntry *entry = bluesky_inode_map_lookup(fs->inode_map, inum, 0);
215 /* Inode doesn't exist: it was probably deleted so keep it that way. */
216 g_mutex_unlock(fs->lock);
220 BlueSkyCloudLog *proxy_item = entry->item;
221 g_mutex_lock(proxy_item->lock);
222 BlueSkyCloudPointer proxy_location = entry->item->location;
223 BlueSkyCloudID proxy_id = entry->item->id;
224 g_mutex_unlock(proxy_item->lock);
225 g_mutex_unlock(fs->lock);
227 /* If the cleaner and the proxy have the same ID, and if the proxy's
228 * in-memory copy is unmodified, then we can simply use the cleaner's
229 * version of the inode. */
232 /* Merge file data together for a regular file. Iterate over the file
233 * blocks in the proxy's copy of the inode. If the block ID is unchanged
234 * in the cleaner but the location was updated, then update the location in
235 * the cleaner because the block was relocated. Otherwise ignore the
236 * cleaner's version for that block because the proxy's information is more
238 BlueSkyInode *proxy_inode = bluesky_get_inode(fs, inum);
239 g_mutex_lock(proxy_inode->lock);
240 if (proxy_inode->type == BLUESKY_REGULAR) {
241 for (int i = 0; i < proxy_inode->blocks->len; i++) {
242 BlueSkyBlock *b = &g_array_index(proxy_inode->blocks,
244 if (b->type != BLUESKY_BLOCK_REF)
246 if (i >= cleaner_inode->links->len)
248 BlueSkyCleanerLink *cb = &g_array_index(cleaner_inode->links,
249 BlueSkyCleanerLink, i);
250 if (memcmp(&b->ref->id, &cb->id, sizeof(BlueSkyCloudPointer)) != 0)
253 g_print(" Updating block %d pointer\n", i);
254 b->ref->location = cb->location;
257 g_mutex_unlock(proxy_inode->lock);
259 /* Mark the inode as modified so it will get written back to the cloud. We
260 * don't actually need to force a synchronous write to our local journal
261 * since there have been no logical modifications. */
263 g_mutex_lock(inode->fs->lock);
264 bluesky_list_unlink(&inode->fs->unlogged_list, inode->unlogged_list);
265 inode->unlogged_list = bluesky_list_prepend(&inode->fs->unlogged_list, inode);
266 g_mutex_unlock(inode->fs->lock);
270 void bluesky_cleaner_merge(BlueSkyFS *fs)
272 BlueSkyCleanerItem *checkpoint = bluesky_cleaner_find_checkpoint(fs);
274 if (checkpoint == NULL) {
275 g_warning("Unable to load cleaner checkpoint record!");
279 /* Iterate over each of the inode map sections in the checkpoint */
280 for (int i = 0; i < checkpoint->links->len; i++) {
281 BlueSkyCleanerLink *link = &g_array_index(checkpoint->links,
282 BlueSkyCleanerLink, i);
283 if (!needs_merging(fs, link->location))
286 BlueSkyCleanerItem *imap = cleaner_load_item(fs, link->location);
288 g_warning("Unable to load cleaner inode map");
292 /* Iterate over all inodes found in the inode map section */
293 for (int j = 0; j < imap->links->len; j++) {
294 BlueSkyCleanerLink *link = &g_array_index(imap->links,
295 BlueSkyCleanerLink, j);
296 if (!needs_merging(fs, link->location))
298 BlueSkyCleanerItem *inode = cleaner_load_item(fs, link->location);
300 merge_inode(fs, inode);
302 bluesky_cleaner_item_free(inode);
305 bluesky_cleaner_item_free(imap);
308 bluesky_cleaner_item_free(checkpoint);