5c4b157febaf6956dd2c55d524184e9691a55c0c
[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->dirty = bluesky_string_new(g_malloc0(block_len), block_len);
35         break;
36     case BLUESKY_BLOCK_REF:
37         // FIXME: locking on the cloudlog?
38         bluesky_block_fetch(inode, block, NULL);
39         bluesky_string_ref(block->ref->data);
40         block->dirty = bluesky_string_dup(block->ref->data);
41         break;
42     case BLUESKY_BLOCK_DIRTY:
43         block->dirty = bluesky_string_dup(block->dirty);
44         break;
45     }
46
47     if (block->type != BLUESKY_BLOCK_DIRTY)
48         g_atomic_int_add(&inode->fs->cache_dirty, 1);
49
50     block->type = BLUESKY_BLOCK_DIRTY;
51     bluesky_cloudlog_unref(block->ref);
52     block->ref = NULL;
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 // FIXME
58 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
59 {
60     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
61
62     if (size == inode->size)
63         return;
64
65     if (bluesky_verbose) {
66         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
67               "Truncating file to %"PRIi64" bytes", size);
68     }
69
70     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
71
72     if (blocks > inode->blocks->len) {
73         /* Need to add new blocks to the end of a file.  New block structures
74          * are automatically zeroed, which initializes them to be pointers to
75          * zero blocks so we don't need to do any more work.  If the
76          * previously-last block in the file is smaller than
77          * BLUESKY_BLOCK_SIZE, extend it to full size. */
78         if (inode->blocks->len > 0) {
79             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
80                                              inode->blocks->len - 1);
81
82             if (b->type != BLUESKY_BLOCK_ZERO
83                     && (b->type == BLUESKY_BLOCK_REF
84                         || b->dirty->len < BLUESKY_BLOCK_SIZE)) {
85                 bluesky_block_touch(inode, inode->blocks->len - 1);
86                 gsize old_size = b->dirty->len;
87                 bluesky_string_resize(b->dirty, BLUESKY_BLOCK_SIZE);
88                 memset(&b->dirty->data[old_size], 0,
89                        BLUESKY_BLOCK_SIZE - old_size);
90             }
91         }
92
93         g_array_set_size(inode->blocks, blocks);
94     } else if (blocks < inode->blocks->len) {
95         /* Delete blocks from a file.  Must reclaim memory. */
96         for (guint i = blocks; i < inode->blocks->len; i++) {
97             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
98             if (b->type == BLUESKY_BLOCK_DIRTY)
99                 g_atomic_int_add(&inode->fs->cache_dirty, -1);
100             bluesky_string_unref(b->dirty);
101             bluesky_cloudlog_unref(b->ref);
102         }
103         g_array_set_size(inode->blocks, blocks);
104     }
105
106     /* Ensure the new last block of the file is properly sized.  If the block
107      * is extended, newly-added bytes must be zeroed. */
108     if (blocks > 0) {
109         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
110                                          blocks - 1);
111
112         if (b->type != BLUESKY_BLOCK_ZERO) {
113             bluesky_block_touch(inode, blocks - 1);
114             gsize old_size = b->dirty->len;
115             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
116
117             bluesky_string_resize(b->dirty, new_size);
118
119             if (new_size > old_size) {
120                 memset(&b->dirty->data[old_size], 0, new_size - old_size);
121             }
122         }
123     }
124
125     inode->size = size;
126     bluesky_inode_update_ctime(inode, 1);
127 }
128
129 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
130                         const char *data, gint len)
131 {
132     g_return_if_fail(inode->type == BLUESKY_REGULAR);
133     g_return_if_fail(offset < inode->size);
134     g_return_if_fail(len <= inode->size - offset);
135
136     if (len == 0)
137         return;
138
139     // TODO: Optimization: If we are entirely overwriting a block we don't need
140     // to fetch it frm storage first.
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->dirty->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     BlueSkyProfile *profile = bluesky_profile_get();
170
171     bluesky_profile_add_event(profile,
172                               g_strdup_printf("Start file read prefetch"));
173     uint64_t start_block, end_block;
174     start_block = offset / BLUESKY_BLOCK_SIZE;
175     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
176     for (uint64_t i = start_block; i <= end_block; i++) {
177         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
178                                          i);
179         if (b->type == BLUESKY_BLOCK_REF)
180             bluesky_cloudlog_prefetch(b->ref);
181     }
182
183     bluesky_profile_add_event(profile,
184                               g_strdup_printf("End file read prefetch"));
185
186     while (len > 0) {
187         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
188         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
189         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
190
191         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
192                                          block_num);
193         if (b->type == BLUESKY_BLOCK_ZERO) {
194             memset(buf, 0, bytes);
195         } else {
196             BlueSkyRCStr *data = NULL;
197             if (b->type == BLUESKY_BLOCK_REF) {
198                 bluesky_block_fetch(inode, b, NULL);
199                 data = b->ref->data;
200             } else if (b->type == BLUESKY_BLOCK_DIRTY) {
201                 data = b->dirty;
202             }
203             memcpy(buf, &data->data[block_offset], bytes);
204         }
205
206         offset += bytes;
207         buf += bytes;
208         len -= bytes;
209     }
210
211     bluesky_profile_add_event(profile,
212                               g_strdup_printf("BlueSky read complete"));
213 }
214
215 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
216                          BlueSkyStoreAsync *barrier)
217 {
218     if (block->type != BLUESKY_BLOCK_REF)
219         return;
220
221     g_mutex_lock(block->ref->lock);
222     bluesky_cloudlog_fetch(block->ref);
223     g_mutex_unlock(block->ref->lock);
224     block->type = BLUESKY_BLOCK_REF;
225 }
226
227 /* Write the given block to cloud-backed storage and mark it clean. */
228 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
229                          GList **log_items)
230 {
231     BlueSkyFS *fs = inode->fs;
232
233     if (block->type != BLUESKY_BLOCK_DIRTY)
234         return;
235
236     g_assert(block->ref == NULL);
237
238     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs, NULL);
239     cloudlog->type = LOGTYPE_DATA;
240     cloudlog->inum = inode->inum;
241     cloudlog->data = block->dirty;      // String ownership is transferred
242     bluesky_cloudlog_stats_update(cloudlog, 1);
243     bluesky_cloudlog_sync(cloudlog);
244     bluesky_cloudlog_ref(cloudlog);     // Reference for log_items list
245     *log_items = g_list_prepend(*log_items, cloudlog);
246     bluesky_cloudlog_insert(cloudlog);
247
248     block->ref = cloudlog;              // Uses initial reference from _new()
249
250     block->type = BLUESKY_BLOCK_REF;
251     block->dirty = NULL;
252     g_atomic_int_add(&fs->cache_dirty, -1);
253 }
254
255 /* Flush all blocks in a file to stable storage. */
256 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
257 {
258     g_return_if_fail(inode->type == BLUESKY_REGULAR);
259
260     for (int i = 0; i < inode->blocks->len; i++) {
261         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
262         bluesky_block_flush(inode, b, log_items);
263     }
264 }
265
266 /* Drop clean data blocks for a file from cache. */
267 void bluesky_file_drop_cached(BlueSkyInode *inode)
268 {
269     g_return_if_fail(inode->type == BLUESKY_REGULAR);
270
271     for (int i = 0; i < inode->blocks->len; i++) {
272         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
273         if (b->type == BLUESKY_BLOCK_REF) {
274             g_mutex_lock(b->ref->lock);
275             if (b->ref->data != NULL
276                 && g_atomic_int_get(&b->ref->data_lock_count) == 0
277                 && (b->ref->location_flags != 0))
278             {
279                 bluesky_cloudlog_stats_update(b->ref, -1);
280                 bluesky_string_unref(b->ref->data);
281                 b->ref->data = NULL;
282                 bluesky_cloudlog_stats_update(b->ref, 1);
283             }
284             g_mutex_unlock(b->ref->lock);
285         }
286     }
287 }