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>
15 /* Core filesystem: handling of regular files and caching of file data. */
17 /* Mark a given block dirty and make sure that data is faulted in so that it
18 * can be written to. */
19 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
21 g_return_if_fail(i < inode->blocks->len);
22 BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
24 switch (block->type) {
25 case BLUESKY_BLOCK_ZERO:
26 block->data = bluesky_string_new(g_malloc0(BLUESKY_BLOCK_SIZE),
29 case BLUESKY_BLOCK_REF:
30 bluesky_block_fetch(inode->fs, block);
31 g_assert(block->type == BLUESKY_BLOCK_CACHED);
33 case BLUESKY_BLOCK_CACHED:
34 case BLUESKY_BLOCK_DIRTY:
35 block->data = bluesky_string_dup(block->data);
39 block->type = BLUESKY_BLOCK_DIRTY;
42 /* Set the size of a file. This will truncate or extend the file as needed.
43 * Newly-allocated bytes are zeroed. */
44 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
46 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
48 if (size == inode->size)
51 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
53 if (blocks > inode->blocks->len) {
54 /* Need to add new blocks to the end of a file. New block structures
55 * are automatically zeroed, which initializes them to be pointers to
56 * zero blocks so we don't need to do any more work. */
57 g_array_set_size(inode->blocks, blocks);
58 } else if (blocks < inode->blocks->len) {
59 /* Delete blocks from a file. Must reclaim memory. */
60 for (guint i = inode->blocks->len; i < blocks; i++) {
61 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
63 bluesky_string_unref(b->data);
65 g_array_set_size(inode->blocks, blocks);
68 /* If the file size is being decreased, ensure that any trailing data in
69 * the last block is zeroed. */
70 if (size < inode->size) {
71 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
73 if (b->type != BLUESKY_BLOCK_ZERO) {
74 bluesky_block_touch(inode, blocks - 1);
75 int end_offset = size % BLUESKY_BLOCK_SIZE;
77 memset(&b->data->data[end_offset], 0,
78 BLUESKY_BLOCK_SIZE - end_offset);
84 bluesky_inode_update_ctime(inode, 1);
87 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
88 const char *data, gint len)
90 g_return_if_fail(inode->type == BLUESKY_REGULAR);
91 g_return_if_fail(offset < inode->size);
92 g_return_if_fail(len <= inode->size - offset);
98 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
99 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
100 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
102 bluesky_block_touch(inode, block_num);
103 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
105 memcpy(&b->data->data[block_offset], data, bytes);
106 bluesky_block_flush(inode->fs, b);
113 bluesky_inode_update_ctime(inode, 1);
114 bluesky_inode_flush(inode->fs, inode);
117 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
120 g_return_if_fail(inode->type == BLUESKY_REGULAR);
121 g_return_if_fail(offset < inode->size);
122 g_return_if_fail(len <= inode->size - offset);
125 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
126 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
127 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
129 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
132 case BLUESKY_BLOCK_ZERO:
133 memset(buf, 0, bytes);
135 case BLUESKY_BLOCK_REF:
136 bluesky_block_fetch(inode->fs, b);
138 case BLUESKY_BLOCK_CACHED:
139 case BLUESKY_BLOCK_DIRTY:
140 memcpy(buf, &b->data->data[block_offset], bytes);
150 /* Read the given block from cloud-backed storage if the data is not already
152 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
154 if (block->type != BLUESKY_BLOCK_REF)
157 g_print("Fetching block from %s\n", block->ref);
158 BlueSkyRCStr *string = bluesky_store_get(fs->store, block->ref);
160 bluesky_string_unref(block->data);
161 block->data = bluesky_crypt_decrypt(string, fs->encryption_key);
162 block->type = BLUESKY_BLOCK_CACHED;
163 bluesky_string_unref(string);
166 /* Write the given block to cloud-backed storage and mark it clean. */
167 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
169 if (block->type != BLUESKY_BLOCK_DIRTY)
172 BlueSkyRCStr *data = block->data;
173 data = bluesky_crypt_encrypt(data, fs->encryption_key);
175 GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
176 g_checksum_update(csum, data->data, data->len);
177 gchar *name = g_strdup(g_checksum_get_string(csum));
179 g_print("Flushing block as %s\n", name);
180 bluesky_store_put(fs->store, name, data);
184 /* block->type = BLUESKY_BLOCK_CACHED; */
185 bluesky_string_unref(block->data);
187 block->type = BLUESKY_BLOCK_REF;
189 g_checksum_free(csum);
190 bluesky_string_unref(data);