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>
14 #include "bluesky-private.h"
16 /* Core filesystem: handling of regular files and caching of file data. */
18 /* Mark a given block dirty and make sure that data is faulted in so that it
19 * can be written to. */
20 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
22 g_return_if_fail(i < inode->blocks->len);
23 BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
26 if (i < inode->blocks->len - 1) {
27 block_len = BLUESKY_BLOCK_SIZE;
29 block_len = inode->size - i * BLUESKY_BLOCK_SIZE;
32 switch (block->type) {
33 case BLUESKY_BLOCK_ZERO:
34 g_print("Allocating zero block of size %zd\n", block_len);
35 block->data = bluesky_string_new(g_malloc0(block_len), block_len);
37 case BLUESKY_BLOCK_REF:
38 bluesky_block_fetch(inode->fs, block, NULL);
39 g_assert(block->type == BLUESKY_BLOCK_CACHED);
41 case BLUESKY_BLOCK_CACHED:
42 case BLUESKY_BLOCK_DIRTY:
43 block->data = bluesky_string_dup(block->data);
47 block->type = BLUESKY_BLOCK_DIRTY;
50 /* Set the size of a file. This will truncate or extend the file as needed.
51 * Newly-allocated bytes are zeroed. */
52 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
54 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
56 if (size == inode->size)
59 g_print("Truncating file to %"PRIi64" bytes\n", size);
61 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
63 if (blocks > inode->blocks->len) {
64 /* Need to add new blocks to the end of a file. New block structures
65 * are automatically zeroed, which initializes them to be pointers to
66 * zero blocks so we don't need to do any more work. If the
67 * previously-last block in the file is smaller than
68 * BLUESKY_BLOCK_SIZE, extend it to full size. */
69 if (inode->blocks->len > 0) {
70 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
71 inode->blocks->len - 1);
73 if (b->type != BLUESKY_BLOCK_ZERO
74 && (b->type == BLUESKY_BLOCK_REF
75 || b->data->len < BLUESKY_BLOCK_SIZE)) {
76 bluesky_block_touch(inode, inode->blocks->len - 1);
77 gsize old_size = b->data->len;
78 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
79 memset(&b->data->data[old_size], 0,
80 BLUESKY_BLOCK_SIZE - old_size);
84 g_array_set_size(inode->blocks, blocks);
85 } else if (blocks < inode->blocks->len) {
86 /* Delete blocks from a file. Must reclaim memory. */
87 for (guint i = inode->blocks->len; i < blocks; i++) {
88 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
90 bluesky_string_unref(b->data);
92 g_array_set_size(inode->blocks, blocks);
95 /* Ensure the new last block of the file is properly sized. If the block
96 * is extended, newly-added bytes must be zeroed. */
98 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
101 if (b->type != BLUESKY_BLOCK_ZERO) {
102 bluesky_block_touch(inode, blocks - 1);
103 gsize old_size = b->data->len;
104 gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
106 bluesky_string_resize(b->data, new_size);
108 if (new_size > old_size) {
109 memset(&b->data->data[old_size], 0, new_size - old_size);
115 bluesky_inode_update_ctime(inode, 1);
118 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
119 const char *data, gint len)
121 g_print("Write %d bytes at offset %"PRIi64"\n", len, offset);
123 g_return_if_fail(inode->type == BLUESKY_REGULAR);
124 g_return_if_fail(offset < inode->size);
125 g_return_if_fail(len <= inode->size - offset);
131 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
132 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
133 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
135 bluesky_block_touch(inode, block_num);
136 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
138 memcpy(&b->data->data[block_offset], data, bytes);
145 bluesky_inode_update_ctime(inode, 1);
148 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
151 g_print("Read %d bytes at offset %"PRIi64"\n", len, offset);
153 if (len == 0 && offset <= inode->size)
156 g_return_if_fail(inode->type == BLUESKY_REGULAR);
157 g_return_if_fail(offset < inode->size);
158 g_return_if_fail(len <= inode->size - offset);
160 /* Start fetches on any data blocks that we will need for this read. */
161 BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
162 barrier->op = STORE_OP_BARRIER;
163 uint64_t start_block, end_block;
164 start_block = offset / BLUESKY_BLOCK_SIZE;
165 end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
166 g_print("Start prefetch on blocks %"PRIi64" .. %"PRIi64"\n",
167 start_block, end_block);
168 for (uint64_t i = start_block; i <= end_block; i++) {
169 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
171 if (b->type == BLUESKY_BLOCK_REF)
172 bluesky_block_fetch(inode->fs, b, barrier);
174 bluesky_store_async_submit(barrier);
175 bluesky_store_async_wait(barrier);
176 bluesky_store_async_unref(barrier);
177 g_print("Prefetch complete.\n");
180 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
181 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
182 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
184 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
187 case BLUESKY_BLOCK_ZERO:
188 memset(buf, 0, bytes);
190 case BLUESKY_BLOCK_REF:
191 bluesky_block_fetch(inode->fs, b, NULL);
193 case BLUESKY_BLOCK_CACHED:
194 case BLUESKY_BLOCK_DIRTY:
195 memcpy(buf, &b->data->data[block_offset], bytes);
205 /* Read the given block from cloud-backed storage if the data is not already
207 static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
209 BlueSkyBlock *block = (BlueSkyBlock *)data;
211 bluesky_string_unref(block->data);
212 block->data = async->data;
213 bluesky_string_ref(block->data);
214 block->type = BLUESKY_BLOCK_CACHED;
217 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
218 BlueSkyStoreAsync *barrier)
220 if (block->type != BLUESKY_BLOCK_REF)
223 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
224 async->op = STORE_OP_GET;
225 async->key = g_strdup(block->ref);
226 bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
227 bluesky_store_async_submit(async);
230 bluesky_store_add_barrier(barrier, async);
232 bluesky_store_async_wait(async);
234 bluesky_store_async_unref(async);
237 /* Write the given block to cloud-backed storage and mark it clean. */
238 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
239 BlueSkyStoreAsync *barrier)
241 if (block->type != BLUESKY_BLOCK_DIRTY)
244 BlueSkyRCStr *data = block->data;
246 GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
247 g_checksum_update(csum, (const guchar *)data->data, data->len);
248 gchar *name = g_strdup(g_checksum_get_string(csum));
250 /* Store the file data asynchronously, and don't bother waiting for a
252 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
253 async->op = STORE_OP_PUT;
254 async->key = g_strdup(name);
255 bluesky_string_ref(data);
257 bluesky_store_async_submit(async);
259 bluesky_store_add_barrier(barrier, async);
260 bluesky_store_async_unref(async);
265 block->type = BLUESKY_BLOCK_CACHED;
267 g_checksum_free(csum);
270 /* Flush all blocks in a file to stable storage. */
271 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
273 g_return_if_fail(inode->type == BLUESKY_REGULAR);
275 for (int i = 0; i < inode->blocks->len; i++) {
276 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
277 bluesky_block_flush(inode->fs, b, barrier);
281 /* Drop clean data blocks for a file from cache. */
282 void bluesky_file_drop_cached(BlueSkyInode *inode)
284 g_return_if_fail(inode->type == BLUESKY_REGULAR);
286 for (int i = 0; i < inode->blocks->len; i++) {
287 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
288 if (b->type == BLUESKY_BLOCK_CACHED) {
289 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
290 "Dropping block %d of inode %"PRIu64" from cache",
293 bluesky_string_unref(b->data);
295 b->type = BLUESKY_BLOCK_REF;