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_DIRTY)
48 g_atomic_int_add(&inode->fs->cache_dirty, 1);
50 block->type = BLUESKY_BLOCK_DIRTY;
51 bluesky_cloudlog_unref(block->ref);
55 /* Set the size of a file. This will truncate or extend the file as needed.
56 * Newly-allocated bytes are zeroed. */
58 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
60 g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
62 if (size == inode->size)
65 if (bluesky_verbose) {
66 g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
67 "Truncating file to %"PRIi64" bytes", size);
70 uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
72 if (blocks > inode->blocks->len) {
73 /* Need to add new blocks to the end of a file. New block structures
74 * are automatically zeroed, which initializes them to be pointers to
75 * zero blocks so we don't need to do any more work. If the
76 * previously-last block in the file is smaller than
77 * BLUESKY_BLOCK_SIZE, extend it to full size. */
78 if (inode->blocks->len > 0) {
79 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
80 inode->blocks->len - 1);
82 if (b->type != BLUESKY_BLOCK_ZERO
83 && (b->type == BLUESKY_BLOCK_REF
84 || b->dirty->len < BLUESKY_BLOCK_SIZE)) {
85 bluesky_block_touch(inode, inode->blocks->len - 1);
86 gsize old_size = b->dirty->len;
87 bluesky_string_resize(b->dirty, BLUESKY_BLOCK_SIZE);
88 memset(&b->dirty->data[old_size], 0,
89 BLUESKY_BLOCK_SIZE - old_size);
93 g_array_set_size(inode->blocks, blocks);
94 } else if (blocks < inode->blocks->len) {
95 /* Delete blocks from a file. Must reclaim memory. */
96 for (guint i = blocks; i < inode->blocks->len; i++) {
97 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
98 if (b->type == BLUESKY_BLOCK_DIRTY)
99 g_atomic_int_add(&inode->fs->cache_dirty, -1);
100 bluesky_string_unref(b->dirty);
101 bluesky_cloudlog_unref(b->ref);
103 g_array_set_size(inode->blocks, blocks);
106 /* Ensure the new last block of the file is properly sized. If the block
107 * is extended, newly-added bytes must be zeroed. */
109 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
112 if (b->type != BLUESKY_BLOCK_ZERO) {
113 bluesky_block_touch(inode, blocks - 1);
114 gsize old_size = b->dirty->len;
115 gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
117 bluesky_string_resize(b->dirty, new_size);
119 if (new_size > old_size) {
120 memset(&b->dirty->data[old_size], 0, new_size - old_size);
126 bluesky_inode_update_ctime(inode, 1);
129 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
130 const char *data, gint len)
132 g_return_if_fail(inode->type == BLUESKY_REGULAR);
133 g_return_if_fail(offset < inode->size);
134 g_return_if_fail(len <= inode->size - offset);
139 // TODO: Optimization: If we are entirely overwriting a block we don't need
140 // to fetch it frm storage first.
142 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
143 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
144 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
146 bluesky_block_touch(inode, block_num);
147 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
149 memcpy(&b->dirty->data[block_offset], data, bytes);
156 bluesky_inode_update_ctime(inode, 1);
159 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
162 if (len == 0 && offset <= inode->size)
165 g_return_if_fail(inode->type == BLUESKY_REGULAR);
166 g_return_if_fail(offset < inode->size);
167 g_return_if_fail(len <= inode->size - offset);
169 BlueSkyProfile *profile = bluesky_profile_get();
171 bluesky_profile_add_event(profile,
172 g_strdup_printf("Start file read prefetch"));
173 uint64_t start_block, end_block;
174 start_block = offset / BLUESKY_BLOCK_SIZE;
175 end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
176 for (uint64_t i = start_block; i <= end_block; i++) {
177 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
179 if (b->type == BLUESKY_BLOCK_REF)
180 bluesky_cloudlog_prefetch(b->ref);
183 bluesky_profile_add_event(profile,
184 g_strdup_printf("End file read prefetch"));
187 uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
188 gint block_offset = offset % BLUESKY_BLOCK_SIZE;
189 gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
191 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
193 if (b->type == BLUESKY_BLOCK_ZERO) {
194 memset(buf, 0, bytes);
196 BlueSkyRCStr *data = NULL;
197 if (b->type == BLUESKY_BLOCK_REF) {
198 bluesky_block_fetch(inode, b, NULL);
200 } else if (b->type == BLUESKY_BLOCK_DIRTY) {
203 memcpy(buf, &data->data[block_offset], bytes);
211 bluesky_profile_add_event(profile,
212 g_strdup_printf("BlueSky read complete"));
215 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
216 BlueSkyStoreAsync *barrier)
218 if (block->type != BLUESKY_BLOCK_REF)
221 g_mutex_lock(block->ref->lock);
222 bluesky_cloudlog_fetch(block->ref);
223 g_mutex_unlock(block->ref->lock);
224 block->type = BLUESKY_BLOCK_REF;
227 /* Write the given block to cloud-backed storage and mark it clean. */
228 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
231 BlueSkyFS *fs = inode->fs;
233 if (block->type != BLUESKY_BLOCK_DIRTY)
236 g_assert(block->ref == NULL);
238 BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs, NULL);
239 cloudlog->type = LOGTYPE_DATA;
240 cloudlog->inum = inode->inum;
241 cloudlog->data = block->dirty; // String ownership is transferred
242 bluesky_cloudlog_stats_update(cloudlog, 1);
243 bluesky_cloudlog_sync(cloudlog);
244 bluesky_cloudlog_ref(cloudlog); // Reference for log_items list
245 *log_items = g_list_prepend(*log_items, cloudlog);
246 bluesky_cloudlog_insert(cloudlog);
248 block->ref = cloudlog; // Uses initial reference from _new()
250 block->type = BLUESKY_BLOCK_REF;
252 g_atomic_int_add(&fs->cache_dirty, -1);
255 /* Flush all blocks in a file to stable storage. */
256 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
258 g_return_if_fail(inode->type == BLUESKY_REGULAR);
260 for (int i = 0; i < inode->blocks->len; i++) {
261 BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
262 bluesky_block_flush(inode, b, log_items);
266 /* Drop clean data blocks for a file from cache. */
267 void bluesky_file_drop_cached(BlueSkyInode *inode)
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 if (b->type == BLUESKY_BLOCK_REF) {
274 g_mutex_lock(b->ref->lock);
275 if (b->ref->data != NULL
276 && g_atomic_int_get(&b->ref->data_lock_count) == 0
277 && (b->ref->location_flags != 0))
279 bluesky_cloudlog_stats_update(b->ref, -1);
280 bluesky_string_unref(b->ref->data);
282 bluesky_cloudlog_stats_update(b->ref, 1);
284 g_mutex_unlock(b->ref->lock);