Disable most debugging print messages; should help with performance.
[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->fs, 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 }
54
55 /* Set the size of a file.  This will truncate or extend the file as needed.
56  * Newly-allocated bytes are zeroed. */
57 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
58 {
59     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
60
61     if (size == inode->size)
62         return;
63
64     if (bluesky_verbose) {
65         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
66               "Truncating file to %"PRIi64" bytes", size);
67     }
68
69     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
70
71     if (blocks > inode->blocks->len) {
72         /* Need to add new blocks to the end of a file.  New block structures
73          * are automatically zeroed, which initializes them to be pointers to
74          * zero blocks so we don't need to do any more work.  If the
75          * previously-last block in the file is smaller than
76          * BLUESKY_BLOCK_SIZE, extend it to full size. */
77         if (inode->blocks->len > 0) {
78             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
79                                              inode->blocks->len - 1);
80
81             if (b->type != BLUESKY_BLOCK_ZERO
82                     && (b->type == BLUESKY_BLOCK_REF
83                         || b->data->len < BLUESKY_BLOCK_SIZE)) {
84                 bluesky_block_touch(inode, inode->blocks->len - 1);
85                 gsize old_size = b->data->len;
86                 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
87                 memset(&b->data->data[old_size], 0,
88                        BLUESKY_BLOCK_SIZE - old_size);
89             }
90         }
91
92         g_array_set_size(inode->blocks, blocks);
93     } else if (blocks < inode->blocks->len) {
94         /* Delete blocks from a file.  Must reclaim memory. */
95         for (guint i = inode->blocks->len; i < blocks; i++) {
96             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
97             g_free(b->ref);
98             if (b->type == BLUESKY_BLOCK_CACHED
99                     || b->type == BLUESKY_BLOCK_DIRTY)
100                 g_atomic_int_add(&inode->fs->cache_total, -1);
101             if (b->type == BLUESKY_BLOCK_DIRTY)
102                 g_atomic_int_add(&inode->fs->cache_dirty, -1);
103             bluesky_string_unref(b->data);
104         }
105         g_array_set_size(inode->blocks, blocks);
106     }
107
108     /* Ensure the new last block of the file is properly sized.  If the block
109      * is extended, newly-added bytes must be zeroed. */
110     if (blocks > 0) {
111         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
112                                          blocks - 1);
113
114         if (b->type != BLUESKY_BLOCK_ZERO) {
115             bluesky_block_touch(inode, blocks - 1);
116             gsize old_size = b->data->len;
117             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
118
119             bluesky_string_resize(b->data, new_size);
120
121             if (new_size > old_size) {
122                 memset(&b->data->data[old_size], 0, new_size - old_size);
123             }
124         }
125     }
126
127     inode->size = size;
128     bluesky_inode_update_ctime(inode, 1);
129 }
130
131 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
132                         const char *data, gint len)
133 {
134     g_return_if_fail(inode->type == BLUESKY_REGULAR);
135     g_return_if_fail(offset < inode->size);
136     g_return_if_fail(len <= inode->size - offset);
137
138     if (len == 0)
139         return;
140
141     while (len > 0) {
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);
145
146         bluesky_block_touch(inode, block_num);
147         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
148                                          block_num);
149         memcpy(&b->data->data[block_offset], data, bytes);
150
151         offset += bytes;
152         data += bytes;
153         len -= bytes;
154     }
155
156     bluesky_inode_update_ctime(inode, 1);
157 }
158
159 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
160                        char *buf, gint len)
161 {
162     if (len == 0 && offset <= inode->size)
163         return;
164
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);
168
169     /* Start fetches on any data blocks that we will need for this read. */
170     BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
171     barrier->op = STORE_OP_BARRIER;
172     uint64_t start_block, end_block;
173     start_block = offset / BLUESKY_BLOCK_SIZE;
174     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
175     if (bluesky_verbose) {
176         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
177               "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
178               start_block, end_block);
179     }
180     for (uint64_t i = start_block; i <= end_block; i++) {
181         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
182                                          i);
183         if (b->type == BLUESKY_BLOCK_REF)
184             bluesky_block_fetch(inode->fs, b, barrier);
185     }
186     bluesky_store_async_submit(barrier);
187     bluesky_store_async_wait(barrier);
188     bluesky_store_async_unref(barrier);
189     if (bluesky_verbose) {
190         g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
191     }
192
193     while (len > 0) {
194         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
195         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
196         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
197
198         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
199                                          block_num);
200         switch (b->type) {
201         case BLUESKY_BLOCK_ZERO:
202             memset(buf, 0, bytes);
203             break;
204         case BLUESKY_BLOCK_REF:
205             bluesky_block_fetch(inode->fs, b, NULL);
206             /* Fall through */
207         case BLUESKY_BLOCK_CACHED:
208         case BLUESKY_BLOCK_DIRTY:
209             memcpy(buf, &b->data->data[block_offset], bytes);
210             break;
211         }
212
213         offset += bytes;
214         buf += bytes;
215         len -= bytes;
216     }
217 }
218
219 /* Read the given block from cloud-backed storage if the data is not already
220  * cached. */
221 static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
222 {
223     BlueSkyBlock *block = (BlueSkyBlock *)data;
224
225     bluesky_string_unref(block->data);
226     block->data = async->data;
227     bluesky_string_ref(block->data);
228     block->type = BLUESKY_BLOCK_CACHED;
229 }
230
231 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
232                          BlueSkyStoreAsync *barrier)
233 {
234     if (block->type != BLUESKY_BLOCK_REF)
235         return;
236
237     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
238     async->op = STORE_OP_GET;
239     async->key = g_strdup(block->ref);
240     bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
241     bluesky_store_async_submit(async);
242
243     if (barrier != NULL)
244         bluesky_store_add_barrier(barrier, async);
245     else
246         bluesky_store_async_wait(async);
247
248     bluesky_store_async_unref(async);
249     g_atomic_int_add(&fs->cache_total, 1);
250 }
251
252 /* Write the given block to cloud-backed storage and mark it clean. */
253 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
254                          BlueSkyStoreAsync *barrier)
255 {
256     if (block->type != BLUESKY_BLOCK_DIRTY)
257         return;
258
259     BlueSkyRCStr *data = block->data;
260
261     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
262     g_checksum_update(csum, (const guchar *)data->data, data->len);
263     gchar *name = g_strdup(g_checksum_get_string(csum));
264
265     /* Store the file data asynchronously, and don't bother waiting for a
266      * response. */
267     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
268     async->op = STORE_OP_PUT;
269     async->key = g_strdup(name);
270     bluesky_string_ref(data);
271     async->data = data;
272     bluesky_store_async_submit(async);
273     if (barrier != NULL)
274         bluesky_store_add_barrier(barrier, async);
275     bluesky_store_async_unref(async);
276
277     g_free(block->ref);
278     block->ref = name;
279
280     block->type = BLUESKY_BLOCK_CACHED;
281     g_atomic_int_add(&fs->cache_dirty, -1);
282
283     g_checksum_free(csum);
284 }
285
286 /* Flush all blocks in a file to stable storage. */
287 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
288 {
289     g_return_if_fail(inode->type == BLUESKY_REGULAR);
290
291     for (int i = 0; i < inode->blocks->len; i++) {
292         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
293         bluesky_block_flush(inode->fs, b, barrier);
294     }
295 }
296
297 /* Drop clean data blocks for a file from cache. */
298 void bluesky_file_drop_cached(BlueSkyInode *inode)
299 {
300     g_return_if_fail(inode->type == BLUESKY_REGULAR);
301
302     for (int i = 0; i < inode->blocks->len; i++) {
303         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
304         if (b->type == BLUESKY_BLOCK_CACHED) {
305             if (bluesky_verbose) {
306                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
307                       "Dropping block %d of inode %"PRIu64" from cache",
308                       i, inode->inum);
309                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
310                       "  (reference count was %d)", b->data->refcount);
311             }
312
313             bluesky_string_unref(b->data);
314             b->data = NULL;
315             b->type = BLUESKY_BLOCK_REF;
316             g_atomic_int_add(&inode->fs->cache_total, -1);
317         }
318     }
319 }