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->dirty = bluesky_string_new(g_malloc0(block_len), block_len);
36 case BLUESKY_BLOCK_REF:
37 // FIXME: locking on the cloudlog?
38 bluesky_block_fetch(inode, block, NULL);
39 bluesky_string_ref(block->ref->data);
40 block->dirty = bluesky_string_dup(block->ref->data);
42 case BLUESKY_BLOCK_DIRTY:
43 block->dirty = bluesky_string_dup(block->dirty);
47 if (block->type != BLUESKY_BLOCK_REF && 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 bluesky_cloudlog_unref(block->ref);
57 /* Set the size of a file. This will truncate or extend the file as needed.
58 * 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->dirty->len < BLUESKY_BLOCK_SIZE)) {
87 bluesky_block_touch(inode, inode->blocks->len - 1);
88 gsize old_size = b->dirty->len;
89 bluesky_string_resize(b->dirty, BLUESKY_BLOCK_SIZE);
90 memset(&b->dirty->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_REF
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->dirty);
106 bluesky_cloudlog_unref(b->ref);
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->dirty->len;
120 gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
122 bluesky_string_resize(b->dirty, new_size);
124 if (new_size > old_size) {
125 memset(&b->dirty->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);
144 // TODO: Optimization: If we are entirely overwriting a block we don't need
145 // to fetch it frm storage first.
147 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
148 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
149 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
151 bluesky_block_touch(inode, block_num);
152 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
154 memcpy(&b->dirty->data[block_offset], data, bytes);
161 bluesky_inode_update_ctime(inode, 1);
164 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
167 if (len == 0 && offset <= inode->size)
170 g_return_if_fail(inode->type == BLUESKY_REGULAR);
171 g_return_if_fail(offset < inode->size);
172 g_return_if_fail(len <= inode->size - offset);
175 /* Start fetches on any data blocks that we will need for this read. */
176 BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
177 barrier->op = STORE_OP_BARRIER;
178 uint64_t start_block, end_block;
179 start_block = offset / BLUESKY_BLOCK_SIZE;
180 end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
181 if (bluesky_verbose) {
182 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
183 "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
184 start_block, end_block);
186 for (uint64_t i = start_block; i <= end_block; i++) {
187 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
189 if (b->type == BLUESKY_BLOCK_REF)
190 bluesky_block_fetch(inode, b, barrier);
192 bluesky_store_async_submit(barrier);
193 bluesky_store_async_wait(barrier);
194 bluesky_store_async_unref(barrier);
195 if (bluesky_verbose) {
196 g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
201 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
202 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
203 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
205 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
207 if (b->type == BLUESKY_BLOCK_ZERO) {
208 memset(buf, 0, bytes);
210 BlueSkyRCStr *data = NULL;
211 if (b->type == BLUESKY_BLOCK_REF) {
212 bluesky_block_fetch(inode, b, NULL);
214 } else if (b->type == BLUESKY_BLOCK_DIRTY) {
217 memcpy(buf, &data->data[block_offset], bytes);
226 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
227 BlueSkyStoreAsync *barrier)
229 if (block->type != BLUESKY_BLOCK_REF)
232 g_mutex_lock(block->ref->lock);
233 bluesky_cloudlog_fetch(block->ref);
234 g_mutex_unlock(block->ref->lock);
235 block->type = BLUESKY_BLOCK_REF;
236 g_atomic_int_add(&inode->fs->cache_total, 1); //FIXME
239 /* Write the given block to cloud-backed storage and mark it clean. */
240 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
243 BlueSkyFS *fs = inode->fs;
245 if (block->type != BLUESKY_BLOCK_DIRTY)
248 g_assert(block->ref == NULL);
250 BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
251 cloudlog->type = LOGTYPE_DATA;
252 cloudlog->inum = inode->inum;
253 cloudlog->data = block->dirty; // String ownership is transferred
254 bluesky_cloudlog_sync(cloudlog);
255 bluesky_cloudlog_ref(cloudlog); // Reference for log_items list
256 *log_items = g_list_prepend(*log_items, cloudlog);
257 bluesky_cloudlog_insert(cloudlog);
259 block->ref = cloudlog; // Uses initial reference from _new()
261 block->type = BLUESKY_BLOCK_REF;
263 g_atomic_int_add(&fs->cache_dirty, -1);
266 /* Flush all blocks in a file to stable storage. */
267 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
269 g_return_if_fail(inode->type == BLUESKY_REGULAR);
271 for (int i = 0; i < inode->blocks->len; i++) {
272 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
273 bluesky_block_flush(inode, b, log_items);
277 /* Drop clean data blocks for a file from cache. */
278 void bluesky_file_drop_cached(BlueSkyInode *inode)
280 g_return_if_fail(inode->type == BLUESKY_REGULAR);
282 for (int i = 0; i < inode->blocks->len; i++) {
283 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
284 if (b->type == BLUESKY_BLOCK_REF) {
285 g_mutex_lock(b->ref->lock);
286 if (b->ref->data != NULL
287 && g_atomic_int_get(&b->ref->data_lock_count) == 0
288 && (b->ref->location_flags != 0))
290 bluesky_string_unref(b->ref->data);
293 g_mutex_unlock(b->ref->lock);
294 g_atomic_int_add(&inode->fs->cache_total, -1);