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