Improve the reading back of objects committed to the journal.
[bluesky.git] / bluesky / file.c
1 /* Blue Sky: File Systems in the Cloud
2  *
3  * Copyright (C) 2009  The Regents of the University of California
4  * Written by Michael Vrable <mvrable@cs.ucsd.edu>
5  *
6  * TODO: Licensing
7  */
8
9 #include <stdint.h>
10 #include <glib.h>
11 #include <string.h>
12 #include <inttypes.h>
13
14 #include "bluesky-private.h"
15
16 /* Core filesystem: handling of regular files and caching of file data. */
17
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)
21 {
22     g_return_if_fail(i < inode->blocks->len);
23     BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
24
25     gsize block_len;
26     if (i < inode->blocks->len - 1) {
27         block_len = BLUESKY_BLOCK_SIZE;
28     } else {
29         block_len = inode->size - i * BLUESKY_BLOCK_SIZE;
30     }
31
32     switch (block->type) {
33     case BLUESKY_BLOCK_ZERO:
34         block->data = bluesky_string_new(g_malloc0(block_len), block_len);
35         break;
36     case BLUESKY_BLOCK_REF:
37         bluesky_block_fetch(inode, block, NULL);
38         g_assert(block->type == BLUESKY_BLOCK_CACHED);
39         /* Fall through */
40     case BLUESKY_BLOCK_CACHED:
41     case BLUESKY_BLOCK_DIRTY:
42         block->data = bluesky_string_dup(block->data);
43         break;
44     }
45
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);
51
52     block->type = BLUESKY_BLOCK_DIRTY;
53     if (block->cloudref != NULL)
54         bluesky_cloudlog_unref(block->cloudref);
55     block->cloudref = NULL;
56 }
57
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)
61 {
62     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
63
64     if (size == inode->size)
65         return;
66
67     if (bluesky_verbose) {
68         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
69               "Truncating file to %"PRIi64" bytes", size);
70     }
71
72     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
73
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);
83
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);
92             }
93         }
94
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);
107         }
108         g_array_set_size(inode->blocks, blocks);
109     }
110
111     /* Ensure the new last block of the file is properly sized.  If the block
112      * is extended, newly-added bytes must be zeroed. */
113     if (blocks > 0) {
114         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
115                                          blocks - 1);
116
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;
121
122             bluesky_string_resize(b->data, new_size);
123
124             if (new_size > old_size) {
125                 memset(&b->data->data[old_size], 0, new_size - old_size);
126             }
127         }
128     }
129
130     inode->size = size;
131     bluesky_inode_update_ctime(inode, 1);
132 }
133
134 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
135                         const char *data, gint len)
136 {
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);
140
141     if (len == 0)
142         return;
143
144     while (len > 0) {
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);
148
149         bluesky_block_touch(inode, block_num);
150         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
151                                          block_num);
152         memcpy(&b->data->data[block_offset], data, bytes);
153
154         offset += bytes;
155         data += bytes;
156         len -= bytes;
157     }
158
159     bluesky_inode_update_ctime(inode, 1);
160 }
161
162 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
163                        char *buf, gint len)
164 {
165     if (len == 0 && offset <= inode->size)
166         return;
167
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);
171
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);
182     }
183     for (uint64_t i = start_block; i <= end_block; i++) {
184         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
185                                          i);
186         if (b->type == BLUESKY_BLOCK_REF)
187             bluesky_block_fetch(inode, b, barrier);
188     }
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.");
194     }
195
196     while (len > 0) {
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);
200
201         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
202                                          block_num);
203         switch (b->type) {
204         case BLUESKY_BLOCK_ZERO:
205             memset(buf, 0, bytes);
206             break;
207         case BLUESKY_BLOCK_REF:
208             bluesky_block_fetch(inode, b, NULL);
209             /* Fall through */
210         case BLUESKY_BLOCK_CACHED:
211         case BLUESKY_BLOCK_DIRTY:
212             memcpy(buf, &b->data->data[block_offset], bytes);
213             break;
214         }
215
216         offset += bytes;
217         buf += bytes;
218         len -= bytes;
219     }
220 }
221
222 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
223                          BlueSkyStoreAsync *barrier)
224 {
225     if (block->type != BLUESKY_BLOCK_REF)
226         return;
227
228     g_mutex_lock(block->cloudref->lock);
229     bluesky_cloudlog_fetch(block->cloudref);
230     block->data = block->cloudref->data;
231     bluesky_string_ref(block->data);
232     g_mutex_unlock(block->cloudref->lock);
233     block->type = BLUESKY_BLOCK_CACHED;
234     g_atomic_int_add(&inode->fs->cache_total, 1);
235 }
236
237 /* Write the given block to cloud-backed storage and mark it clean. */
238 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
239                          GList **log_items)
240 {
241     BlueSkyFS *fs = inode->fs;
242
243     if (block->type != BLUESKY_BLOCK_DIRTY)
244         return;
245
246     if (block->cloudref != NULL)
247         bluesky_cloudlog_unref(block->cloudref);
248
249     BlueSkyRCStr *data = block->data;
250
251     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
252     cloudlog->type = LOGTYPE_DATA;
253     cloudlog->inum = inode->inum;
254     cloudlog->data = data;
255     bluesky_string_ref(data);
256     bluesky_cloudlog_sync(cloudlog);
257     *log_items = g_list_prepend(*log_items, cloudlog);
258     bluesky_cloudlog_insert(cloudlog);
259
260     block->cloudref = cloudlog;
261     bluesky_cloudlog_ref(cloudlog);
262
263     block->type = BLUESKY_BLOCK_CACHED;
264     g_atomic_int_add(&fs->cache_dirty, -1);
265 }
266
267 /* Flush all blocks in a file to stable storage. */
268 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
269 {
270     g_return_if_fail(inode->type == BLUESKY_REGULAR);
271
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);
275     }
276 }
277
278 /* Drop clean data blocks for a file from cache. */
279 void bluesky_file_drop_cached(BlueSkyInode *inode)
280 {
281     g_return_if_fail(inode->type == BLUESKY_REGULAR);
282
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_CACHED) {
286             if (bluesky_verbose) {
287                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
288                       "Dropping block %d of inode %"PRIu64" from cache",
289                       i, inode->inum);
290                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
291                       "  (reference count was %d)", b->data->refcount);
292             }
293
294             bluesky_string_unref(b->data);
295             b->data = NULL;
296             b->type = BLUESKY_BLOCK_REF;
297             g_atomic_int_add(&inode->fs->cache_total, -1);
298             g_mutex_lock(b->cloudref->lock);
299             bluesky_string_unref(b->cloudref->data);
300             b->cloudref->data = NULL;
301             g_mutex_unlock(b->cloudref->lock);
302         }
303     }
304 }