Fix up reference counting for cloud log items.
[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->data = bluesky_string_new(g_malloc0(block_len), block_len);
35         break;
36     case BLUESKY_BLOCK_REF:
37         bluesky_block_fetch(inode, block, NULL);
38         g_assert(block->type == BLUESKY_BLOCK_CACHED);
39         /* Fall through */
40     case BLUESKY_BLOCK_CACHED:
41     case BLUESKY_BLOCK_DIRTY:
42         block->data = bluesky_string_dup(block->data);
43         break;
44     }
45
46     if (block->type != BLUESKY_BLOCK_CACHED
47             && 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->cloudref);
54     block->cloudref = 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 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
60 {
61     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
62
63     if (size == inode->size)
64         return;
65
66     if (bluesky_verbose) {
67         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
68               "Truncating file to %"PRIi64" bytes", size);
69     }
70
71     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
72
73     if (blocks > inode->blocks->len) {
74         /* Need to add new blocks to the end of a file.  New block structures
75          * are automatically zeroed, which initializes them to be pointers to
76          * zero blocks so we don't need to do any more work.  If the
77          * previously-last block in the file is smaller than
78          * BLUESKY_BLOCK_SIZE, extend it to full size. */
79         if (inode->blocks->len > 0) {
80             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
81                                              inode->blocks->len - 1);
82
83             if (b->type != BLUESKY_BLOCK_ZERO
84                     && (b->type == BLUESKY_BLOCK_REF
85                         || b->data->len < BLUESKY_BLOCK_SIZE)) {
86                 bluesky_block_touch(inode, inode->blocks->len - 1);
87                 gsize old_size = b->data->len;
88                 bluesky_string_resize(b->data, BLUESKY_BLOCK_SIZE);
89                 memset(&b->data->data[old_size], 0,
90                        BLUESKY_BLOCK_SIZE - old_size);
91             }
92         }
93
94         g_array_set_size(inode->blocks, blocks);
95     } else if (blocks < inode->blocks->len) {
96         /* Delete blocks from a file.  Must reclaim memory. */
97         for (guint i = inode->blocks->len; i < blocks; i++) {
98             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
99             if (b->type == BLUESKY_BLOCK_CACHED
100                     || b->type == BLUESKY_BLOCK_DIRTY)
101                 g_atomic_int_add(&inode->fs->cache_total, -1);
102             if (b->type == BLUESKY_BLOCK_DIRTY)
103                 g_atomic_int_add(&inode->fs->cache_dirty, -1);
104             bluesky_string_unref(b->data);
105             bluesky_cloudlog_unref(b->cloudref);
106         }
107         g_array_set_size(inode->blocks, blocks);
108     }
109
110     /* Ensure the new last block of the file is properly sized.  If the block
111      * is extended, newly-added bytes must be zeroed. */
112     if (blocks > 0) {
113         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
114                                          blocks - 1);
115
116         if (b->type != BLUESKY_BLOCK_ZERO) {
117             bluesky_block_touch(inode, blocks - 1);
118             gsize old_size = b->data->len;
119             gsize new_size = size - (blocks - 1) * BLUESKY_BLOCK_SIZE;
120
121             bluesky_string_resize(b->data, new_size);
122
123             if (new_size > old_size) {
124                 memset(&b->data->data[old_size], 0, new_size - old_size);
125             }
126         }
127     }
128
129     inode->size = size;
130     bluesky_inode_update_ctime(inode, 1);
131 }
132
133 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
134                         const char *data, gint len)
135 {
136     g_return_if_fail(inode->type == BLUESKY_REGULAR);
137     g_return_if_fail(offset < inode->size);
138     g_return_if_fail(len <= inode->size - offset);
139
140     if (len == 0)
141         return;
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         bluesky_block_touch(inode, block_num);
149         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
150                                          block_num);
151         memcpy(&b->data->data[block_offset], data, bytes);
152
153         offset += bytes;
154         data += bytes;
155         len -= bytes;
156     }
157
158     bluesky_inode_update_ctime(inode, 1);
159 }
160
161 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
162                        char *buf, gint len)
163 {
164     if (len == 0 && offset <= inode->size)
165         return;
166
167     g_return_if_fail(inode->type == BLUESKY_REGULAR);
168     g_return_if_fail(offset < inode->size);
169     g_return_if_fail(len <= inode->size - offset);
170
171     /* Start fetches on any data blocks that we will need for this read. */
172     BlueSkyStoreAsync *barrier = bluesky_store_async_new(inode->fs->store);
173     barrier->op = STORE_OP_BARRIER;
174     uint64_t start_block, end_block;
175     start_block = offset / BLUESKY_BLOCK_SIZE;
176     end_block = (offset + len - 1) / BLUESKY_BLOCK_SIZE;
177     if (bluesky_verbose) {
178         g_log("bluesky/file", G_LOG_LEVEL_DEBUG,
179               "Start prefetch on blocks %"PRIi64" .. %"PRIi64,
180               start_block, end_block);
181     }
182     for (uint64_t i = start_block; i <= end_block; i++) {
183         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
184                                          i);
185         if (b->type == BLUESKY_BLOCK_REF)
186             bluesky_block_fetch(inode, b, barrier);
187     }
188     bluesky_store_async_submit(barrier);
189     bluesky_store_async_wait(barrier);
190     bluesky_store_async_unref(barrier);
191     if (bluesky_verbose) {
192         g_log("bluesky/file", G_LOG_LEVEL_DEBUG, "Prefetch complete.");
193     }
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         switch (b->type) {
203         case BLUESKY_BLOCK_ZERO:
204             memset(buf, 0, bytes);
205             break;
206         case BLUESKY_BLOCK_REF:
207             bluesky_block_fetch(inode, b, NULL);
208             /* Fall through */
209         case BLUESKY_BLOCK_CACHED:
210         case BLUESKY_BLOCK_DIRTY:
211             memcpy(buf, &b->data->data[block_offset], bytes);
212             break;
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->cloudref->lock);
228     bluesky_cloudlog_fetch(block->cloudref);
229     block->data = block->cloudref->data;
230     bluesky_string_ref(block->data);
231     g_mutex_unlock(block->cloudref->lock);
232     block->type = BLUESKY_BLOCK_CACHED;
233     g_atomic_int_add(&inode->fs->cache_total, 1);
234 }
235
236 /* Write the given block to cloud-backed storage and mark it clean. */
237 void bluesky_block_flush(BlueSkyInode *inode, BlueSkyBlock *block,
238                          GList **log_items)
239 {
240     BlueSkyFS *fs = inode->fs;
241
242     if (block->type != BLUESKY_BLOCK_DIRTY)
243         return;
244
245     bluesky_cloudlog_unref(block->cloudref);
246
247     BlueSkyRCStr *data = block->data;
248
249     BlueSkyCloudLog *cloudlog = bluesky_cloudlog_new(fs);
250     cloudlog->type = LOGTYPE_DATA;
251     cloudlog->inum = inode->inum;
252     cloudlog->data = data;
253     bluesky_string_ref(data);
254     bluesky_cloudlog_sync(cloudlog);
255     *log_items = g_list_prepend(*log_items, cloudlog);
256     bluesky_cloudlog_insert(cloudlog);
257
258     block->cloudref = cloudlog;
259
260     block->type = BLUESKY_BLOCK_CACHED;
261     g_atomic_int_add(&fs->cache_dirty, -1);
262 }
263
264 /* Flush all blocks in a file to stable storage. */
265 void bluesky_file_flush(BlueSkyInode *inode, GList **log_items)
266 {
267     g_return_if_fail(inode->type == BLUESKY_REGULAR);
268
269     for (int i = 0; i < inode->blocks->len; i++) {
270         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
271         bluesky_block_flush(inode, b, log_items);
272     }
273 }
274
275 /* Drop clean data blocks for a file from cache. */
276 void bluesky_file_drop_cached(BlueSkyInode *inode)
277 {
278     g_return_if_fail(inode->type == BLUESKY_REGULAR);
279
280     for (int i = 0; i < inode->blocks->len; i++) {
281         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
282         if (b->type == BLUESKY_BLOCK_CACHED) {
283             if (bluesky_verbose) {
284                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
285                       "Dropping block %d of inode %"PRIu64" from cache",
286                       i, inode->inum);
287                 g_log("bluesky/cache", G_LOG_LEVEL_DEBUG,
288                       "  (reference count was %d)", b->data->refcount);
289             }
290
291             bluesky_string_unref(b->data);
292             b->data = NULL;
293             b->type = BLUESKY_BLOCK_REF;
294             g_atomic_int_add(&inode->fs->cache_total, -1);
295             g_mutex_lock(b->cloudref->lock);
296             if (b->cloudref->location_flags & CLOUDLOG_JOURNAL) {
297                 bluesky_string_unref(b->cloudref->data);
298                 b->cloudref->data = NULL;
299             }
300             g_mutex_unlock(b->cloudref->lock);
301         }
302     }
303 }