Split handling of file data out of inode.c.
[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
13 #include "bluesky.h"
14
15 /* Core filesystem: handling of regular files and caching of file data. */
16
17 /* Mark a given block dirty and make sure that data is faulted in so that it
18  * can be written to. */
19 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i)
20 {
21     g_return_if_fail(i < inode->blocks->len);
22     BlueSkyBlock *block = &g_array_index(inode->blocks, BlueSkyBlock, i);
23
24     switch (block->type) {
25     case BLUESKY_BLOCK_ZERO:
26         block->data = bluesky_string_new(g_malloc0(BLUESKY_BLOCK_SIZE),
27                                          BLUESKY_BLOCK_SIZE);
28         break;
29     case BLUESKY_BLOCK_REF:
30         bluesky_block_fetch(inode->fs, block);
31         g_assert(block->type == BLUESKY_BLOCK_CACHED);
32         /* Fall through */
33     case BLUESKY_BLOCK_CACHED:
34     case BLUESKY_BLOCK_DIRTY:
35         block->data = bluesky_string_dup(block->data);
36         break;
37     }
38
39     block->type = BLUESKY_BLOCK_DIRTY;
40 }
41
42 /* Set the size of a file.  This will truncate or extend the file as needed.
43  * Newly-allocated bytes are zeroed. */
44 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
45 {
46     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
47
48     if (size == inode->size)
49         return;
50
51     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_BLOCK_SIZE;
52
53     if (blocks > inode->blocks->len) {
54         /* Need to add new blocks to the end of a file.  New block structures
55          * are automatically zeroed, which initializes them to be pointers to
56          * zero blocks so we don't need to do any more work. */
57         g_array_set_size(inode->blocks, blocks);
58     } else if (blocks < inode->blocks->len) {
59         /* Delete blocks from a file.  Must reclaim memory. */
60         for (guint i = inode->blocks->len; i < blocks; i++) {
61             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
62             g_free(b->ref);
63             bluesky_string_unref(b->data);
64         }
65         g_array_set_size(inode->blocks, blocks);
66     }
67
68     /* If the file size is being decreased, ensure that any trailing data in
69      * the last block is zeroed. */
70     if (size < inode->size) {
71         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
72                                          blocks - 1);
73         if (b->type != BLUESKY_BLOCK_ZERO) {
74             bluesky_block_touch(inode, blocks - 1);
75             int end_offset = size % BLUESKY_BLOCK_SIZE;
76             if (end_offset > 0) {
77                 memset(&b->data->data[end_offset], 0,
78                        BLUESKY_BLOCK_SIZE - end_offset);
79             }
80         }
81     }
82
83     inode->size = size;
84     bluesky_inode_update_ctime(inode, 1);
85 }
86
87 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
88                         const char *data, gint len)
89 {
90     g_return_if_fail(inode->type == BLUESKY_REGULAR);
91     g_return_if_fail(offset < inode->size);
92     g_return_if_fail(len <= inode->size - offset);
93
94     if (len == 0)
95         return;
96
97     while (len > 0) {
98         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
99         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
100         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
101
102         bluesky_block_touch(inode, block_num);
103         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
104                                          block_num);
105         memcpy(&b->data->data[block_offset], data, bytes);
106         bluesky_block_flush(inode->fs, b);
107
108         offset += bytes;
109         data += bytes;
110         len -= bytes;
111     }
112
113     bluesky_inode_update_ctime(inode, 1);
114 }
115
116 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
117                        char *buf, gint len)
118 {
119     g_return_if_fail(inode->type == BLUESKY_REGULAR);
120     g_return_if_fail(offset < inode->size);
121     g_return_if_fail(len <= inode->size - offset);
122
123     while (len > 0) {
124         uint64_t block_num = offset / BLUESKY_BLOCK_SIZE;
125         gint block_offset = offset % BLUESKY_BLOCK_SIZE;
126         gint bytes = MIN(BLUESKY_BLOCK_SIZE - block_offset, len);
127
128         BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock,
129                                          block_num);
130         switch (b->type) {
131         case BLUESKY_BLOCK_ZERO:
132             memset(buf, 0, bytes);
133             break;
134         case BLUESKY_BLOCK_REF:
135             bluesky_block_fetch(inode->fs, b);
136             /* Fall through */
137         case BLUESKY_BLOCK_CACHED:
138         case BLUESKY_BLOCK_DIRTY:
139             memcpy(buf, &b->data->data[block_offset], bytes);
140             break;
141         }
142
143         offset += bytes;
144         buf += bytes;
145         len -= bytes;
146     }
147 }
148
149 /* Read the given block from cloud-backed storage if the data is not already
150  * cached. */
151 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block)
152 {
153     if (block->type != BLUESKY_BLOCK_REF)
154         return;
155
156     g_print("Fetching block from %s\n", block->ref);
157     BlueSkyRCStr *string = s3store_get(fs->store, block->ref);
158
159     bluesky_string_unref(block->data);
160     block->data = bluesky_crypt_decrypt(string, fs->encryption_key);
161     block->type = BLUESKY_BLOCK_CACHED;
162     bluesky_string_unref(string);
163 }
164
165 /* Write the given block to cloud-backed storage and mark it clean. */
166 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block)
167 {
168     if (block->type != BLUESKY_BLOCK_DIRTY)
169         return;
170
171     BlueSkyRCStr *data = block->data;
172     data = bluesky_crypt_encrypt(data, fs->encryption_key);
173
174     GChecksum *csum = g_checksum_new(G_CHECKSUM_SHA256);
175     g_checksum_update(csum, data->data, data->len);
176     gchar *name = g_strdup(g_checksum_get_string(csum));
177
178     g_print("Flushing block as %s\n", name);
179     s3store_put(fs->store, name, data);
180     g_free(block->ref);
181     block->ref = name;
182
183     /* block->type = BLUESKY_BLOCK_CACHED; */
184     bluesky_string_unref(block->data);
185     block->data = NULL;
186     block->type = BLUESKY_BLOCK_REF;
187
188     g_checksum_free(csum);
189     bluesky_string_unref(data);
190 }