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 block->data = bluesky_string_new(g_malloc0(block_len), block_len);
36 case BLUESKY_BLOCK_REF:
37 bluesky_block_fetch(inode, block, NULL);
38 g_assert(block->type == BLUESKY_BLOCK_CACHED);
40 case BLUESKY_BLOCK_CACHED:
41 case BLUESKY_BLOCK_DIRTY:
42 block->data = bluesky_string_dup(block->data);
46 if (block->type != BLUESKY_BLOCK_CACHED
47 && block->type != BLUESKY_BLOCK_DIRTY)
48 g_atomic_int_add(&inode->fs->cache_total, 1);
49 if (block->type != BLUESKY_BLOCK_DIRTY)
50 g_atomic_int_add(&inode->fs->cache_dirty, 1);
52 block->type = BLUESKY_BLOCK_DIRTY;
53 if (block->cloudref != NULL)
54 bluesky_cloudlog_unref(block->cloudref);
55 block->cloudref = NULL;
58 /* Set the size of a file. This will truncate or extend the file as needed.
59 * Newly-allocated bytes are zeroed. */
60 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
62 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
64 if (size == inode->size)
67 if (bluesky_verbose) {
68 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
69 "Truncating file to %"PRIi64" bytes", size);
72 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
74 if (blocks > inode->blocks->len) {
75 /* Need to add new blocks to the end of a file. New block structures
76 * are automatically zeroed, which initializes them to be pointers to
77 * zero blocks so we don't need to do any more work. If the
78 * previously-last block in the file is smaller than
79 * BLUESKY_BLOCK_SIZE, extend it to full size. */
80 if (inode->blocks->len > 0) {
81 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
82 inode->blocks->len - 1);
84 if (b->type != BLUESKY_BLOCK_ZERO
85 && (b->type == BLUESKY_BLOCK_REF
86 || b->data->len < BLUESKY_BLOCK_SIZE)) {
87 bluesky_block_touch(inode, inode->blocks->len - 1);
88 gsize old_size = b->data->len;
89 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
90 memset(&b->data->data[old_size], 0,
91 BLUESKY_BLOCK_SIZE - old_size);
95 g_array_set_size(inode->blocks, blocks);
96 } else if (blocks < inode->blocks->len) {
97 /* Delete blocks from a file. Must reclaim memory. */
98 for (guint i = inode->blocks->len; i < blocks; i++) {
99 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
100 if (b->type == BLUESKY_BLOCK_CACHED
101 || b->type == BLUESKY_BLOCK_DIRTY)
102 g_atomic_int_add(&inode->fs->cache_total, -1);
103 if (b->type == BLUESKY_BLOCK_DIRTY)
104 g_atomic_int_add(&inode->fs->cache_dirty, -1);
105 bluesky_string_unref(b->data);
106 bluesky_cloudlog_unref(b->cloudref);
108 g_array_set_size(inode->blocks, blocks);
111 /* Ensure the new last block of the file is properly sized. If the block
112 * is extended, newly-added bytes must be zeroed. */
114 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
117 if (b->type != BLUESKY_BLOCK_ZERO) {
118 bluesky_block_touch(inode, blocks - 1);
119 gsize old_size = b->data->len;
120 gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
122 bluesky_string_resize(b->data, new_size);
124 if (new_size > old_size) {
125 memset(&b->data->data[old_size], 0, new_size - old_size);
131 bluesky_inode_update_ctime(inode, 1);
134 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
135 const char *data, gint len)
137 g_return_if_fail(inode->type == BLUESKY_REGULAR);
138 g_return_if_fail(offset < inode->size);
139 g_return_if_fail(len <= inode->size - offset);
145 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
146 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
147 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
149 bluesky_block_touch(inode, block_num);
150 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
152 memcpy(&b->data->data[block_offset], data, bytes);
159 bluesky_inode_update_ctime(inode, 1);
162 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
165 if (len == 0 && offset <= inode->size)
168 g_return_if_fail(inode->type == BLUESKY_REGULAR);
169 g_return_if_fail(offset < inode->size);
170 g_return_if_fail(len <= inode->size - offset);
172 /* Start fetches on any data blocks that we will need for this read. */
173 BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
174 barrier->op = STORE_OP_BARRIER;
175 uint64_t start_block, end_block;
176 start_block = offset / BLUESKY_BLOCK_SIZE;
177 end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
178 if (bluesky_verbose) {
179 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
180 "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
181 start_block, end_block);
183 for (uint64_t i = start_block; i <= end_block; i++) {
184 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
186 if (b->type == BLUESKY_BLOCK_REF)
187 bluesky_block_fetch(inode, b, barrier);
189 bluesky_store_async_submit(barrier);
190 bluesky_store_async_wait(barrier);
191 bluesky_store_async_unref(barrier);
192 if (bluesky_verbose) {
193 g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
197 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
198 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
199 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
201 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
204 case BLUESKY_BLOCK_ZERO:
205 memset(buf, 0, bytes);
207 case BLUESKY_BLOCK_REF:
208 bluesky_block_fetch(inode, b, NULL);
210 case BLUESKY_BLOCK_CACHED:
211 case BLUESKY_BLOCK_DIRTY:
212 memcpy(buf, &b->data->data[block_offset], bytes);
223 /* Read the given block from cloud-backed storage if the data is not already
225 static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
227 BlueSkyBlock *block = (BlueSkyBlock *)data;
229 bluesky_string_unref(block->data);
230 block->data = async->data;
231 bluesky_string_ref(block->data);
233 if (block->data == NULL) {
234 g_warning("Failed to fetch data block from store!\n");
235 block->data = bluesky_string_new(g_malloc0(BLUESKY_BLOCK_SIZE),
239 block->type = BLUESKY_BLOCK_CACHED;
243 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
244 BlueSkyStoreAsync *barrier)
246 // TODO: Implement this
249 BlueSkyFS *fs = inode->fs;
251 if (block->type != BLUESKY_BLOCK_REF)
254 BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
255 async->op = STORE_OP_GET;
256 async->key = g_strdup(block->ref);
257 bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
258 bluesky_store_async_submit(async);
261 bluesky_store_add_barrier(barrier, async);
263 bluesky_store_async_wait(async);
265 bluesky_store_async_unref(async);
266 g_atomic_int_add(&fs->cache_total, 1);
270 /* Write the given block to cloud-backed storage and mark it clean. */
271 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
274 BlueSkyFS *fs = inode->fs;
276 if (block->type != BLUESKY_BLOCK_DIRTY)
279 if (block->cloudref != NULL)
280 bluesky_cloudlog_unref(block->cloudref);
282 BlueSkyRCStr *data = block->data;
284 BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
285 cloudlog->type = LOGTYPE_DATA;
286 cloudlog->inum = inode->inum;
287 cloudlog->data = data;
288 bluesky_string_ref(data);
289 bluesky_cloudlog_sync(cloudlog);
290 *log_items = g_list_prepend(*log_items, cloudlog);
291 bluesky_cloudlog_insert(cloudlog);
293 block->cloudref = cloudlog;
294 bluesky_cloudlog_ref(cloudlog);
296 block->type = BLUESKY_BLOCK_CACHED;
297 g_atomic_int_add(&fs->cache_dirty, -1);
300 /* Flush all blocks in a file to stable storage. */
301 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
303 g_return_if_fail(inode->type == BLUESKY_REGULAR);
305 for (int i = 0; i < inode->blocks->len; i++) {
306 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
307 bluesky_block_flush(inode, b, log_items);
311 /* Drop clean data blocks for a file from cache. */
312 void bluesky_file_drop_cached(BlueSkyInode *inode)
315 g_return_if_fail(inode->type == BLUESKY_REGULAR);
317 for (int i = 0; i < inode->blocks->len; i++) {
318 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
319 if (b->type == BLUESKY_BLOCK_CACHED) {
320 if (bluesky_verbose) {
321 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
322 "Dropping block %d of inode %"PRIu64" from cache",
324 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
325 " (reference count was %d)", b->data->refcount);
328 bluesky_string_unref(b->data);
330 b->type = BLUESKY_BLOCK_REF;
331 g_atomic_int_add(&inode->fs->cache_total, -1);