Add in some support for journal replay.
[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 #if 0
170     /* Start fetches on any data blocks that we will need for this read. */
171     BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
172     barrier->op = STORE_OP_BARRIER;
173     uint64_t start_block, end_block;
174     start_block = offset / BLUESKY_BLOCK_SIZE;
175     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
176     if (bluesky_verbose) {
177         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
178               "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
179               start_block, end_block);
180     }
181     for (uint64_t i = start_block; i <= end_block; i++) {
182         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
183                                          i);
184         if (b->type == BLUESKY_BLOCK_REF)
185             bluesky_block_fetch(inode, b, barrier);
186     }
187     bluesky_store_async_submit(barrier);
188     bluesky_store_async_wait(barrier);
189     bluesky_store_async_unref(barrier);
190     if (bluesky_verbose) {
191         g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
192     }
193 #endif
194
195     while (len > 0) {
196         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
197         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
198         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
199
200         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
201                                          block_num);
202         if (b->type == BLUESKY_BLOCK_ZERO) {
203             memset(buf, 0, bytes);
204         } else {
205             BlueSkyRCStr *data = NULL;
206             if (b->type == BLUESKY_BLOCK_REF) {
207                 bluesky_block_fetch(inode, b, NULL);
208                 data = b->ref->data;
209             } else if (b->type == BLUESKY_BLOCK_DIRTY) {
210                 data = b->dirty;
211             }
212             memcpy(buf, &data->data[block_offset], bytes);
213         }
214
215         offset += bytes;
216         buf += bytes;
217         len -= bytes;
218     }
219 }
220
221 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
222                          BlueSkyStoreAsync *barrier)
223 {
224     if (block->type != BLUESKY_BLOCK_REF)
225         return;
226
227     g_mutex_lock(block->ref->lock);
228     bluesky_cloudlog_fetch(block->ref);
229     g_mutex_unlock(block->ref->lock);
230     block->type = BLUESKY_BLOCK_REF;
231 }
232
233 /* Write the given block to cloud-backed storage and mark it clean. */
234 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
235                          GList **log_items)
236 {
237     BlueSkyFS *fs = inode->fs;
238
239     if (block->type != BLUESKY_BLOCK_DIRTY)
240         return;
241
242     g_assert(block->ref == NULL);
243
244     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs, NULL);
245     cloudlog->type = LOGTYPE_DATA;
246     cloudlog->inum = inode->inum;
247     cloudlog->data = block->dirty;      // String ownership is transferred
248     bluesky_cloudlog_stats_update(cloudlog, 1);
249     bluesky_cloudlog_sync(cloudlog);
250     bluesky_cloudlog_ref(cloudlog);     // Reference for log_items list
251     *log_items = g_list_prepend(*log_items, cloudlog);
252     bluesky_cloudlog_insert(cloudlog);
253
254     block->ref = cloudlog;              // Uses initial reference from _new()
255
256     block->type = BLUESKY_BLOCK_REF;
257     block->dirty = NULL;
258     g_atomic_int_add(&fs->cache_dirty, -1);
259 }
260
261 /* Flush all blocks in a file to stable storage. */
262 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
263 {
264     g_return_if_fail(inode->type == BLUESKY_REGULAR);
265
266     for (int i = 0; i < inode->blocks->len; i++) {
267         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
268         bluesky_block_flush(inode, b, log_items);
269     }
270 }
271
272 /* Drop clean data blocks for a file from cache. */
273 void bluesky_file_drop_cached(BlueSkyInode *inode)
274 {
275     g_return_if_fail(inode->type == BLUESKY_REGULAR);
276
277     for (int i = 0; i < inode->blocks->len; i++) {
278         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
279         if (b->type == BLUESKY_BLOCK_REF) {
280             g_mutex_lock(b->ref->lock);
281             if (b->ref->data != NULL
282                 && g_atomic_int_get(&b->ref->data_lock_count) == 0
283                 && (b->ref->location_flags != 0))
284             {
285                 bluesky_cloudlog_stats_update(b->ref, -1);
286                 bluesky_string_unref(b->ref->data);
287                 b->ref->data = NULL;
288                 bluesky_cloudlog_stats_update(b->ref, 1);
289             }
290             g_mutex_unlock(b->ref->lock);
291         }
292     }
293 }