Partial work on parallel data fetches from S3 for large reads.
[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         g_print("Allocating zero block of size %zd\n", block_len);
35         block->data = bluesky_string_new(g_malloc0(block_len), block_len);
36         break;
37     case BLUESKY_BLOCK_REF:
38         bluesky_block_fetch(inode->fs, block, NULL);
39         g_assert(block->type == BLUESKY_BLOCK_CACHED);
40         /* Fall through */
41     case BLUESKY_BLOCK_CACHED:
42     case BLUESKY_BLOCK_DIRTY:
43         block->data = bluesky_string_dup(block->data);
44         break;
45     }
46
47     block->type = BLUESKY_BLOCK_DIRTY;
48 }
49
50 /* Set the size of a file.  This will truncate or extend the file as needed.
51  * Newly-allocated bytes are zeroed. */
52 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
53 {
54     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
55
56     if (size == inode->size)
57         return;
58
59     g_print("Truncating file to %"PRIi64" bytes\n", size);
60
61     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
62
63     if (blocks > inode->blocks->len) {
64         /* Need to add new blocks to the end of a file.  New block structures
65          * are automatically zeroed, which initializes them to be pointers to
66          * zero blocks so we don't need to do any more work.  If the
67          * previously-last block in the file is smaller than
68          * BLUESKY_BLOCK_SIZE, extend it to full size. */
69         if (inode->blocks->len > 0) {
70             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
71                                              inode->blocks->len - 1);
72
73             if (b->type != BLUESKY_BLOCK_ZERO
74                     && (b->type == BLUESKY_BLOCK_REF
75                         || b->data->len < BLUESKY_BLOCK_SIZE)) {
76                 bluesky_block_touch(inode, inode->blocks->len - 1);
77                 gsize old_size = b->data->len;
78                 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
79                 memset(&b->data->data[old_size], 0,
80                        BLUESKY_BLOCK_SIZE - old_size);
81             }
82         }
83
84         g_array_set_size(inode->blocks, blocks);
85     } else if (blocks < inode->blocks->len) {
86         /* Delete blocks from a file.  Must reclaim memory. */
87         for (guint i = inode->blocks->len; i < blocks; i++) {
88             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
89             g_free(b->ref);
90             bluesky_string_unref(b->data);
91         }
92         g_array_set_size(inode->blocks, blocks);
93     }
94
95     /* Ensure the new last block of the file is properly sized.  If the block
96      * is extended, newly-added bytes must be zeroed. */
97     if (blocks > 0) {
98         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
99                                          blocks - 1);
100
101         if (b->type != BLUESKY_BLOCK_ZERO) {
102             bluesky_block_touch(inode, blocks - 1);
103             gsize old_size = b->data->len;
104             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
105
106             bluesky_string_resize(b->data, new_size);
107
108             if (new_size > old_size) {
109                 memset(&b->data->data[old_size], 0, new_size - old_size);
110             }
111         }
112     }
113
114     inode->size = size;
115     bluesky_inode_update_ctime(inode, 1);
116 }
117
118 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
119                         const char *data, gint len)
120 {
121     g_print("Write %d bytes at offset %"PRIi64"\n", len, offset);
122
123     g_return_if_fail(inode->type == BLUESKY_REGULAR);
124     g_return_if_fail(offset < inode->size);
125     g_return_if_fail(len <= inode->size - offset);
126
127     if (len == 0)
128         return;
129
130     while (len > 0) {
131         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
132         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
133         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
134
135         bluesky_block_touch(inode, block_num);
136         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
137                                          block_num);
138         memcpy(&b->data->data[block_offset], data, bytes);
139
140         offset += bytes;
141         data += bytes;
142         len -= bytes;
143     }
144
145     bluesky_inode_update_ctime(inode, 1);
146 }
147
148 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
149                        char *buf, gint len)
150 {
151     g_print("Read %d bytes at offset %"PRIi64"\n", len, offset);
152
153     if (len == 0 && offset <= inode->size)
154         return;
155
156     g_return_if_fail(inode->type == BLUESKY_REGULAR);
157     g_return_if_fail(offset < inode->size);
158     g_return_if_fail(len <= inode->size - offset);
159
160     while (len > 0) {
161         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
162         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
163         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
164
165         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
166                                          block_num);
167         switch (b->type) {
168         case BLUESKY_BLOCK_ZERO:
169             memset(buf, 0, bytes);
170             break;
171         case BLUESKY_BLOCK_REF:
172             bluesky_block_fetch(inode->fs, b, NULL);
173             /* Fall through */
174         case BLUESKY_BLOCK_CACHED:
175         case BLUESKY_BLOCK_DIRTY:
176             memcpy(buf, &b->data->data[block_offset], bytes);
177             break;
178         }
179
180         offset += bytes;
181         buf += bytes;
182         len -= bytes;
183     }
184 }
185
186 /* Read the given block from cloud-backed storage if the data is not already
187  * cached. */
188 static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
189 {
190     BlueSkyBlock *block = (BlueSkyBlock *)data;
191
192     bluesky_string_unref(block->data);
193     block->data = async->data;
194     bluesky_string_ref(block->data);
195     block->type = BLUESKY_BLOCK_CACHED;
196 }
197
198 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
199                          BlueSkyStoreAsync *barrier)
200 {
201     if (block->type != BLUESKY_BLOCK_REF)
202         return;
203
204     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
205     async->op = STORE_OP_GET;
206     async->key = g_strdup(block->ref);
207     bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
208     bluesky_store_async_submit(async);
209
210     if (barrier != NULL)
211         bluesky_store_add_barrier(barrier, async);
212     else
213         bluesky_store_async_wait(async);
214 }
215
216 /* Write the given block to cloud-backed storage and mark it clean. */
217 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
218                          BlueSkyStoreAsync *barrier)
219 {
220     if (block->type != BLUESKY_BLOCK_DIRTY)
221         return;
222
223     BlueSkyRCStr *data = block->data;
224
225     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
226     g_checksum_update(csum, (const guchar *)data->data, data->len);
227     gchar *name = g_strdup(g_checksum_get_string(csum));
228
229     /* Store the file data asynchronously, and don't bother waiting for a
230      * response. */
231     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
232     async->op = STORE_OP_PUT;
233     async->key = g_strdup(name);
234     bluesky_string_ref(data);
235     async->data = data;
236     bluesky_store_async_submit(async);
237     if (barrier != NULL)
238         bluesky_store_add_barrier(barrier, async);
239     bluesky_store_async_unref(async);
240
241     g_free(block->ref);
242     block->ref = name;
243
244     block->type = BLUESKY_BLOCK_CACHED;
245
246     g_checksum_free(csum);
247 }
248
249 /* Flush all blocks in a file to stable storage. */
250 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
251 {
252     g_return_if_fail(inode->type == BLUESKY_REGULAR);
253
254     for (int i = 0; i < inode->blocks->len; i++) {
255         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
256         bluesky_block_flush(inode->fs, b, barrier);
257     }
258 }
259
260 /* Drop clean data blocks for a file from cache. */
261 void bluesky_file_drop_cached(BlueSkyInode *inode)
262 {
263     g_return_if_fail(inode->type == BLUESKY_REGULAR);
264
265     for (int i = 0; i < inode->blocks->len; i++) {
266         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
267         if (b->type == BLUESKY_BLOCK_CACHED) {
268             g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
269                   "Dropping block %d of inode %"PRIu64" from cache",
270                   i, inode->inum);
271
272             bluesky_string_unref(b->data);
273             b->data = NULL;
274             b->type = BLUESKY_BLOCK_REF;
275         }
276     }
277 }