Track when cached file data is written out.
[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);
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. */
67         g_array_set_size(inode->blocks, blocks);
68     } else if (blocks < inode->blocks->len) {
69         /* Delete blocks from a file.  Must reclaim memory. */
70         for (guint i = inode->blocks->len; i < blocks; i++) {
71             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
72             g_free(b->ref);
73             bluesky_string_unref(b->data);
74         }
75         g_array_set_size(inode->blocks, blocks);
76     }
77
78     /* Ensure the last block of the file is properly sized.  If the block is
79      * extended, newly-added bytes must be zeroed. */
80     if (blocks > 0) {
81         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
82                                          blocks - 1);
83
84         if (b->type != BLUESKY_BLOCK_ZERO) {
85             bluesky_block_touch(inode, blocks - 1);
86             gsize old_size = b->data->len;
87             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
88
89             bluesky_string_resize(b->data, new_size);
90
91             if (new_size > old_size) {
92                 memset(&b->data->data[old_size], 0, new_size - old_size);
93             }
94         }
95     }
96
97     inode->size = size;
98     bluesky_inode_update_ctime(inode, 1);
99 }
100
101 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
102                         const char *data, gint len)
103 {
104     g_print("Write %d bytes at offset %"PRIi64"\n", len, offset);
105
106     g_return_if_fail(inode->type == BLUESKY_REGULAR);
107     g_return_if_fail(offset < inode->size);
108     g_return_if_fail(len <= inode->size - offset);
109
110     if (len == 0)
111         return;
112
113     while (len > 0) {
114         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
115         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
116         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
117
118         bluesky_block_touch(inode, block_num);
119         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
120                                          block_num);
121         memcpy(&b->data->data[block_offset], data, bytes);
122
123         offset += bytes;
124         data += bytes;
125         len -= bytes;
126     }
127
128     bluesky_inode_update_ctime(inode, 1);
129 }
130
131 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
132                        char *buf, gint len)
133 {
134     g_print("Read %d bytes at offset %"PRIi64"\n", len, offset);
135
136     if (len == 0 && offset <= inode->size)
137         return;
138
139     g_return_if_fail(inode->type == BLUESKY_REGULAR);
140     g_return_if_fail(offset < inode->size);
141     g_return_if_fail(len <= inode->size - offset);
142
143     while (len > 0) {
144         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
145         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
146         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
147
148         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
149                                          block_num);
150         switch (b->type) {
151         case BLUESKY_BLOCK_ZERO:
152             memset(buf, 0, bytes);
153             break;
154         case BLUESKY_BLOCK_REF:
155             bluesky_block_fetch(inode->fs, b);
156             /* Fall through */
157         case BLUESKY_BLOCK_CACHED:
158         case BLUESKY_BLOCK_DIRTY:
159             memcpy(buf, &b->data->data[block_offset], bytes);
160             break;
161         }
162
163         offset += bytes;
164         buf += bytes;
165         len -= bytes;
166     }
167 }
168
169 /* Read the given block from cloud-backed storage if the data is not already
170  * cached. */
171 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
172 {
173     if (block->type != BLUESKY_BLOCK_REF)
174         return;
175
176     BlueSkyRCStr *string = bluesky_store_get(fs->store, block->ref);
177
178     bluesky_string_unref(block->data);
179     block->data = string;
180     block->type = BLUESKY_BLOCK_CACHED;
181 }
182
183 /* Write the given block to cloud-backed storage and mark it clean. */
184 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block,
185                          BlueSkyStoreAsync *barrier)
186 {
187     if (block->type != BLUESKY_BLOCK_DIRTY)
188         return;
189
190     BlueSkyRCStr *data = block->data;
191
192     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
193     g_checksum_update(csum, data->data, data->len);
194     gchar *name = g_strdup(g_checksum_get_string(csum));
195
196     /* Store the file data asynchronously, and don't bother waiting for a
197      * response. */
198     BlueSkyStoreAsync *async = bluesky_store_async_new(fs->store);
199     async->op = STORE_OP_PUT;
200     async->key = g_strdup(name);
201     bluesky_string_ref(data);
202     async->data = data;
203     bluesky_store_async_submit(async);
204     if (barrier != NULL)
205         bluesky_store_add_barrier(barrier, async);
206     bluesky_store_async_unref(async);
207
208     g_free(block->ref);
209     block->ref = name;
210
211     /* block->type = BLUESKY_BLOCK_CACHED; */
212     bluesky_string_unref(block->data);
213     block->data = NULL;
214     block->type = BLUESKY_BLOCK_REF;
215
216     g_checksum_free(csum);
217     //bluesky_string_unref(data);
218 }
219
220 /* Flush all blocks in a file to stable storage. */
221 void bluesky_file_flush(BlueSkyInode *inode, BlueSkyStoreAsync *barrier)
222 {
223     g_return_if_fail(inode->type == BLUESKY_REGULAR);
224
225     for (int i = 0; i < inode->blocks->len; i++) {
226         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
227         bluesky_block_flush(inode->fs, b, barrier);
228     }
229 }