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_CACHED
48 && block->type != BLUESKY_BLOCK_DIRTY)
49 g_atomic_int_add(&inode->fs->cache_total, 1); //FIXME */
50 if (block->type != BLUESKY_BLOCK_DIRTY)
51 g_atomic_int_add(&inode->fs->cache_dirty, 1);
53 block->type = BLUESKY_BLOCK_DIRTY;
54 bluesky_cloudlog_unref(block->ref);
58 /* Set the size of a file. This will truncate or extend the file as needed.
59 * Newly-allocated bytes are zeroed. */
61 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
63 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
65 if (size == inode->size)
68 if (bluesky_verbose) {
69 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
70 "Truncating file to %"PRIi64" bytes", size);
73 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
75 if (blocks > inode->blocks->len) {
76 /* Need to add new blocks to the end of a file. New block structures
77 * are automatically zeroed, which initializes them to be pointers to
78 * zero blocks so we don't need to do any more work. If the
79 * previously-last block in the file is smaller than
80 * BLUESKY_BLOCK_SIZE, extend it to full size. */
81 if (inode->blocks->len > 0) {
82 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
83 inode->blocks->len - 1);
85 if (b->type != BLUESKY_BLOCK_ZERO
86 && (b->type == BLUESKY_BLOCK_REF
87 || b->dirty->len < BLUESKY_BLOCK_SIZE)) {
88 bluesky_block_touch(inode, inode->blocks->len - 1);
89 gsize old_size = b->dirty->len;
90 bluesky_string_resize(b->dirty, BLUESKY_BLOCK_SIZE);
91 memset(&b->dirty->data[old_size], 0,
92 BLUESKY_BLOCK_SIZE - old_size);
96 g_array_set_size(inode->blocks, blocks);
97 } else if (blocks < inode->blocks->len) {
98 /* Delete blocks from a file. Must reclaim memory. */
99 for (guint i = inode->blocks->len; i < blocks; i++) {
100 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
101 /* if (b->type == BLUESKY_BLOCK_CACHED
102 || b->type == BLUESKY_BLOCK_DIRTY)
103 g_atomic_int_add(&inode->fs->cache_total, -1); FIXME */
104 if (b->type == BLUESKY_BLOCK_DIRTY)
105 g_atomic_int_add(&inode->fs->cache_dirty, -1);
106 bluesky_string_unref(b->dirty);
107 bluesky_cloudlog_unref(b->ref);
109 g_array_set_size(inode->blocks, blocks);
112 /* Ensure the new last block of the file is properly sized. If the block
113 * is extended, newly-added bytes must be zeroed. */
115 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
118 if (b->type != BLUESKY_BLOCK_ZERO) {
119 bluesky_block_touch(inode, blocks - 1);
120 gsize old_size = b->dirty->len;
121 gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
123 bluesky_string_resize(b->dirty, new_size);
125 if (new_size > old_size) {
126 memset(&b->dirty->data[old_size], 0, new_size - old_size);
132 bluesky_inode_update_ctime(inode, 1);
135 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
136 const char *data, gint len)
138 g_return_if_fail(inode->type == BLUESKY_REGULAR);
139 g_return_if_fail(offset < inode->size);
140 g_return_if_fail(len <= inode->size - offset);
145 // TODO: Optimization: If we are entirely overwriting a block we don't need
146 // to fetch it frm storage first.
148 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
149 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
150 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
152 bluesky_block_touch(inode, block_num);
153 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
155 memcpy(&b->dirty->data[block_offset], data, bytes);
162 bluesky_inode_update_ctime(inode, 1);
165 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
168 if (len == 0 && offset <= inode->size)
171 g_return_if_fail(inode->type == BLUESKY_REGULAR);
172 g_return_if_fail(offset < inode->size);
173 g_return_if_fail(len <= inode->size - offset);
176 /* Start fetches on any data blocks that we will need for this read. */
177 BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
178 barrier->op = STORE_OP_BARRIER;
179 uint64_t start_block, end_block;
180 start_block = offset / BLUESKY_BLOCK_SIZE;
181 end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
182 if (bluesky_verbose) {
183 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
184 "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
185 start_block, end_block);
187 for (uint64_t i = start_block; i <= end_block; i++) {
188 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
190 if (b->type == BLUESKY_BLOCK_REF)
191 bluesky_block_fetch(inode, b, barrier);
193 bluesky_store_async_submit(barrier);
194 bluesky_store_async_wait(barrier);
195 bluesky_store_async_unref(barrier);
196 if (bluesky_verbose) {
197 g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
202 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
203 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
204 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
206 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
208 if (b->type == BLUESKY_BLOCK_ZERO) {
209 memset(buf, 0, bytes);
211 BlueSkyRCStr *data = NULL;
212 if (b->type == BLUESKY_BLOCK_REF) {
213 bluesky_block_fetch(inode, b, NULL);
215 } else if (b->type == BLUESKY_BLOCK_DIRTY) {
218 memcpy(buf, &data->data[block_offset], bytes);
227 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
228 BlueSkyStoreAsync *barrier)
230 if (block->type != BLUESKY_BLOCK_REF)
233 g_mutex_lock(block->ref->lock);
234 bluesky_cloudlog_fetch(block->ref);
235 g_mutex_unlock(block->ref->lock);
236 block->type = BLUESKY_BLOCK_REF;
237 g_atomic_int_add(&inode->fs->cache_total, 1); //FIXME
240 /* Write the given block to cloud-backed storage and mark it clean. */
241 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
244 BlueSkyFS *fs = inode->fs;
246 if (block->type != BLUESKY_BLOCK_DIRTY)
249 g_assert(block->ref == NULL);
251 BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
252 cloudlog->type = LOGTYPE_DATA;
253 cloudlog->inum = inode->inum;
254 cloudlog->data = block->dirty; // String ownership is transferred
255 bluesky_cloudlog_sync(cloudlog);
256 bluesky_cloudlog_ref(cloudlog); // Reference for log_items list
257 *log_items = g_list_prepend(*log_items, cloudlog);
258 bluesky_cloudlog_insert(cloudlog);
260 block->ref = cloudlog; // Uses initial reference from _new()
262 block->type = BLUESKY_BLOCK_REF;
264 g_atomic_int_add(&fs->cache_dirty, -1);
267 /* Flush all blocks in a file to stable storage. */
268 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
270 g_return_if_fail(inode->type == BLUESKY_REGULAR);
272 for (int i = 0; i < inode->blocks->len; i++) {
273 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
274 bluesky_block_flush(inode, b, log_items);
278 /* Drop clean data blocks for a file from cache. */
279 void bluesky_file_drop_cached(BlueSkyInode *inode)
281 g_return_if_fail(inode->type == BLUESKY_REGULAR);
283 for (int i = 0; i < inode->blocks->len; i++) {
284 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
285 if (b->type == BLUESKY_BLOCK_REF) {
286 g_mutex_lock(b->ref->lock);
287 if (b->ref->data != NULL
288 && g_atomic_int_get(&b->ref->data_lock_count) == 0
289 && (b->ref->location_flags != 0))
291 bluesky_string_unref(b->ref->data);
294 g_mutex_unlock(b->ref->lock);
295 g_atomic_int_add(&inode->fs->cache_total, -1);