Rework caching of data blocks to eliminate double-caching.
[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_CACHED
48             && block->type != BLUESKY_BLOCK_DIRTY)
49         g_atomic_int_add(&inode->fs->cache_total, 1);   //FIXME */
50     if (block->type != BLUESKY_BLOCK_DIRTY)
51         g_atomic_int_add(&inode->fs->cache_dirty, 1);
52
53     block->type = BLUESKY_BLOCK_DIRTY;
54     bluesky_cloudlog_unref(block->ref);
55     block->ref = NULL;
56 }
57
58 /* Set the size of a file.  This will truncate or extend the file as needed.
59  * Newly-allocated bytes are zeroed. */
60 // FIXME
61 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
62 {
63     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
64
65     if (size == inode->size)
66         return;
67
68     if (bluesky_verbose) {
69         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
70               "Truncating file to %"PRIi64" bytes", size);
71     }
72
73     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
74
75     if (blocks > inode->blocks->len) {
76         /* Need to add new blocks to the end of a file.  New block structures
77          * are automatically zeroed, which initializes them to be pointers to
78          * zero blocks so we don't need to do any more work.  If the
79          * previously-last block in the file is smaller than
80          * BLUESKY_BLOCK_SIZE, extend it to full size. */
81         if (inode->blocks->len > 0) {
82             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
83                                              inode->blocks->len - 1);
84
85             if (b->type != BLUESKY_BLOCK_ZERO
86                     && (b->type == BLUESKY_BLOCK_REF
87                         || b->dirty->len < BLUESKY_BLOCK_SIZE)) {
88                 bluesky_block_touch(inode, inode->blocks->len - 1);
89                 gsize old_size = b->dirty->len;
90                 bluesky_string_resize(b->dirty, BLUESKY_BLOCK_SIZE);
91                 memset(&b->dirty->data[old_size], 0,
92                        BLUESKY_BLOCK_SIZE - old_size);
93             }
94         }
95
96         g_array_set_size(inode->blocks, blocks);
97     } else if (blocks < inode->blocks->len) {
98         /* Delete blocks from a file.  Must reclaim memory. */
99         for (guint i = inode->blocks->len; i < blocks; i++) {
100             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
101             /* if (b->type == BLUESKY_BLOCK_CACHED
102                     || b->type == BLUESKY_BLOCK_DIRTY)
103                 g_atomic_int_add(&inode->fs->cache_total, -1); FIXME */
104             if (b->type == BLUESKY_BLOCK_DIRTY)
105                 g_atomic_int_add(&inode->fs->cache_dirty, -1);
106             bluesky_string_unref(b->dirty);
107             bluesky_cloudlog_unref(b->ref);
108         }
109         g_array_set_size(inode->blocks, blocks);
110     }
111
112     /* Ensure the new last block of the file is properly sized.  If the block
113      * is extended, newly-added bytes must be zeroed. */
114     if (blocks > 0) {
115         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
116                                          blocks - 1);
117
118         if (b->type != BLUESKY_BLOCK_ZERO) {
119             bluesky_block_touch(inode, blocks - 1);
120             gsize old_size = b->dirty->len;
121             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
122
123             bluesky_string_resize(b->dirty, new_size);
124
125             if (new_size > old_size) {
126                 memset(&b->dirty->data[old_size], 0, new_size - old_size);
127             }
128         }
129     }
130
131     inode->size = size;
132     bluesky_inode_update_ctime(inode, 1);
133 }
134
135 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
136                         const char *data, gint len)
137 {
138     g_return_if_fail(inode->type == BLUESKY_REGULAR);
139     g_return_if_fail(offset < inode->size);
140     g_return_if_fail(len <= inode->size - offset);
141
142     if (len == 0)
143         return;
144
145     // TODO: Optimization: If we are entirely overwriting a block we don't need
146     // to fetch it frm storage first.
147     while (len > 0) {
148         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
149         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
150         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
151
152         bluesky_block_touch(inode, block_num);
153         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
154                                          block_num);
155         memcpy(&b->dirty->data[block_offset], data, bytes);
156
157         offset += bytes;
158         data += bytes;
159         len -= bytes;
160     }
161
162     bluesky_inode_update_ctime(inode, 1);
163 }
164
165 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
166                        char *buf, gint len)
167 {
168     if (len == 0 && offset <= inode->size)
169         return;
170
171     g_return_if_fail(inode->type == BLUESKY_REGULAR);
172     g_return_if_fail(offset < inode->size);
173     g_return_if_fail(len <= inode->size - offset);
174
175 #if 0
176     /* Start fetches on any data blocks that we will need for this read. */
177     BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
178     barrier->op = STORE_OP_BARRIER;
179     uint64_t start_block, end_block;
180     start_block = offset / BLUESKY_BLOCK_SIZE;
181     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
182     if (bluesky_verbose) {
183         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
184               "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
185               start_block, end_block);
186     }
187     for (uint64_t i = start_block; i <= end_block; i++) {
188         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
189                                          i);
190         if (b->type == BLUESKY_BLOCK_REF)
191             bluesky_block_fetch(inode, b, barrier);
192     }
193     bluesky_store_async_submit(barrier);
194     bluesky_store_async_wait(barrier);
195     bluesky_store_async_unref(barrier);
196     if (bluesky_verbose) {
197         g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
198     }
199 #endif
200
201     while (len > 0) {
202         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
203         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
204         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
205
206         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
207                                          block_num);
208         if (b->type == BLUESKY_BLOCK_ZERO) {
209             memset(buf, 0, bytes);
210         } else {
211             BlueSkyRCStr *data = NULL;
212             if (b->type == BLUESKY_BLOCK_REF) {
213                 bluesky_block_fetch(inode, b, NULL);
214                 data = b->ref->data;
215             } else if (b->type == BLUESKY_BLOCK_DIRTY) {
216                 data = b->dirty;
217             }
218             memcpy(buf, &data->data[block_offset], bytes);
219         }
220
221         offset += bytes;
222         buf += bytes;
223         len -= bytes;
224     }
225 }
226
227 void bluesky_block_fetch(BlueSkyInode *inode, BlueSkyBlock *block,
228                          BlueSkyStoreAsync *barrier)
229 {
230     if (block->type != BLUESKY_BLOCK_REF)
231         return;
232
233     g_mutex_lock(block->ref->lock);
234     bluesky_cloudlog_fetch(block->ref);
235     g_mutex_unlock(block->ref->lock);
236     block->type = BLUESKY_BLOCK_REF;
237     g_atomic_int_add(&inode->fs->cache_total, 1);  //FIXME
238 }
239
240 /* Write the given block to cloud-backed storage and mark it clean. */
241 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
242                          GList **log_items)
243 {
244     BlueSkyFS *fs = inode->fs;
245
246     if (block->type != BLUESKY_BLOCK_DIRTY)
247         return;
248
249     g_assert(block->ref == NULL);
250
251     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
252     cloudlog->type = LOGTYPE_DATA;
253     cloudlog->inum = inode->inum;
254     cloudlog->data = block->dirty;      // String ownership is transferred
255     bluesky_cloudlog_sync(cloudlog);
256     bluesky_cloudlog_ref(cloudlog);     // Reference for log_items list
257     *log_items = g_list_prepend(*log_items, cloudlog);
258     bluesky_cloudlog_insert(cloudlog);
259
260     block->ref = cloudlog;              // Uses initial reference from _new()
261
262     block->type = BLUESKY_BLOCK_REF;
263     block->dirty = NULL;
264     g_atomic_int_add(&fs->cache_dirty, -1);
265 }
266
267 /* Flush all blocks in a file to stable storage. */
268 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
269 {
270     g_return_if_fail(inode->type == BLUESKY_REGULAR);
271
272     for (int i = 0; i < inode->blocks->len; i++) {
273         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
274         bluesky_block_flush(inode, b, log_items);
275     }
276 }
277
278 /* Drop clean data blocks for a file from cache. */
279 void bluesky_file_drop_cached(BlueSkyInode *inode)
280 {
281     g_return_if_fail(inode->type == BLUESKY_REGULAR);
282
283     for (int i = 0; i < inode->blocks->len; i++) {
284         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
285         if (b->type == BLUESKY_BLOCK_REF) {
286             g_mutex_lock(b->ref->lock);
287             if (b->ref->data != NULL
288                 && g_atomic_int_get(&b->ref->data_lock_count) == 0
289                 && (b->ref->location_flags != 0))
290             {
291                 bluesky_string_unref(b->ref->data);
292                 b->ref->data = NULL;
293             }
294             g_mutex_unlock(b->ref->lock);
295             g_atomic_int_add(&inode->fs->cache_total, -1);
296         }
297     }
298 }