Fix memory leak in bluesky_block_fetch.
[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     /* Start fetches on any data blocks that we will need for this read. */
161     BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
162     barrier->op = STORE_OP_BARRIER;
163     uint64_t start_block, end_block;
164     start_block = offset / BLUESKY_BLOCK_SIZE;
165     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
166     g_print("Start prefetch on blocks %"PRIi64" .. %"PRIi64"\n",
167             start_block, end_block);
168     for (uint64_t i = start_block; i <= end_block; i++) {
169         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
170                                          i);
171         if (b->type == BLUESKY_BLOCK_REF)
172             bluesky_block_fetch(inode->fs, b, barrier);
173     }
174     bluesky_store_async_submit(barrier);
175     bluesky_store_async_wait(barrier);
176     bluesky_store_async_unref(barrier);
177     g_print("Prefetch complete.\n");
178
179     while (len > 0) {
180         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
181         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
182         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
183
184         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
185                                          block_num);
186         switch (b->type) {
187         case BLUESKY_BLOCK_ZERO:
188             memset(buf, 0, bytes);
189             break;
190         case BLUESKY_BLOCK_REF:
191             bluesky_block_fetch(inode->fs, b, NULL);
192             /* Fall through */
193         case BLUESKY_BLOCK_CACHED:
194         case BLUESKY_BLOCK_DIRTY:
195             memcpy(buf, &b->data->data[block_offset], bytes);
196             break;
197         }
198
199         offset += bytes;
200         buf += bytes;
201         len -= bytes;
202     }
203 }
204
205 /* Read the given block from cloud-backed storage if the data is not already
206  * cached. */
207 static void block_fetch_completion(BlueSkyStoreAsync *async, gpointer data)
208 {
209     BlueSkyBlock *block = (BlueSkyBlock *)data;
210
211     bluesky_string_unref(block->data);
212     block->data = async->data;
213     bluesky_string_ref(block->data);
214     block->type = BLUESKY_BLOCK_CACHED;
215 }
216
217 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block,
218                          BlueSkyStoreAsync *barrier)
219 {
220     if (block->type != BLUESKY_BLOCK_REF)
221         return;
222
223     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
224     async->op = STORE_OP_GET;
225     async->key = g_strdup(block->ref);
226     bluesky_store_async_add_notifier(async, (GFunc)block_fetch_completion, block);
227     bluesky_store_async_submit(async);
228
229     if (barrier != NULL)
230         bluesky_store_add_barrier(barrier, async);
231     else
232         bluesky_store_async_wait(async);
233
234     bluesky_store_async_unref(async);
235 }
236
237 /* Write the given block to cloud-backed storage and mark it clean. */
238 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
239                          BlueSkyStoreAsync *barrier)
240 {
241     if (block->type != BLUESKY_BLOCK_DIRTY)
242         return;
243
244     BlueSkyRCStr *data = block->data;
245
246     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
247     g_checksum_update(csum, (const guchar *)data->data, data->len);
248     gchar *name = g_strdup(g_checksum_get_string(csum));
249
250     /* Store the file data asynchronously, and don't bother waiting for a
251      * response. */
252     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
253     async->op = STORE_OP_PUT;
254     async->key = g_strdup(name);
255     bluesky_string_ref(data);
256     async->data = data;
257     bluesky_store_async_submit(async);
258     if (barrier != NULL)
259         bluesky_store_add_barrier(barrier, async);
260     bluesky_store_async_unref(async);
261
262     g_free(block->ref);
263     block->ref = name;
264
265     block->type = BLUESKY_BLOCK_CACHED;
266
267     g_checksum_free(csum);
268 }
269
270 /* Flush all blocks in a file to stable storage. */
271 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
272 {
273     g_return_if_fail(inode->type == BLUESKY_REGULAR);
274
275     for (int i = 0; i < inode->blocks->len; i++) {
276         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
277         bluesky_block_flush(inode->fs, b, barrier);
278     }
279 }
280
281 /* Drop clean data blocks for a file from cache. */
282 void bluesky_file_drop_cached(BlueSkyInode *inode)
283 {
284     g_return_if_fail(inode->type == BLUESKY_REGULAR);
285
286     for (int i = 0; i < inode->blocks->len; i++) {
287         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
288         if (b->type == BLUESKY_BLOCK_CACHED) {
289             g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
290                   "Dropping block %d of inode %"PRIu64" from cache",
291                   i, inode->inum);
292
293             bluesky_string_unref(b->data);
294             b->data = NULL;
295             b->type = BLUESKY_BLOCK_REF;
296         }
297     }
298 }