Fix the S3Store get operation to handle arbitrarily-sized objects.
[bluesky.git] / bluesky / bluesky.h
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 #ifndef _BLUESKY_H
10 #define _BLUESKY_H
11
12 #include <stdint.h>
13 #include <inttypes.h>
14 #include <glib.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 /* BlueSky status and error codes.  Various frontends should translate these to
21  * the appropriate error code for whatever protocol they implement. */
22 typedef enum {
23     BSTATUS_OK = 0,             /* No error */
24     BSTATUS_IOERR,              /* I/O error of some form */
25     BSTATUS_NOENT,              /* File does not exist */
26 } BlueSkyStatus;
27
28 void bluesky_init(void);
29
30 gchar *bluesky_lowercase(const gchar *s);
31
32 /* Reference-counted blocks of memory, used for passing data in and out of
33  * storage backends and in other places. */
34 typedef struct {
35     gint refcount;
36     gchar *data;
37     gsize len;
38 } BlueSkyRCStr;
39
40 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
41 BlueSkyRCStr *bluesky_string_new_from_string(GString *s);
42 void bluesky_string_ref(BlueSkyRCStr *string);
43 void bluesky_string_unref(BlueSkyRCStr *string);
44 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
45 void bluesky_string_resize(BlueSkyRCStr *string, gsize len);
46
47 /* Cryptographic operations. */
48 #define CRYPTO_BLOCK_SIZE 16        /* 128-bit AES */
49 #define CRYPTO_KEY_SIZE   16
50
51 void bluesky_crypt_init();
52 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
53 void bluesky_crypt_random_bytes(guchar *buf, gint len);
54 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
55 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
56
57 /* Storage interface.  This presents a key-value store abstraction, and can
58  * have multiple implementations: in-memory, on-disk, in-cloud. */
59 typedef struct {
60     /* Create a new store instance and return a handle to it. */
61     gpointer (*create)();
62
63     /* Clean up any resources used by this store. */
64     void (*destroy)(gpointer store);
65
66     /* Fetch an item with the given name, or return NULL if not found. */
67     BlueSkyRCStr * (*get)(gpointer store, const gchar *key);
68
69     /* Store an item to the given key name. */
70     void (*put)(gpointer store, const gchar *key, BlueSkyRCStr *val);
71 } BlueSkyStoreImplementation;
72
73 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
74                             const gchar *name);
75
76 struct _BlueSkyStore;
77 typedef struct _BlueSkyStore BlueSkyStore;
78
79 void bluesky_store_init();
80 BlueSkyStore *bluesky_store_new(const gchar *type);
81 void bluesky_store_free(BlueSkyStore *store);
82 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
83 void bluesky_store_put(BlueSkyStore *store,
84                        const gchar *key, BlueSkyRCStr *val);
85
86 /* File types.  The numeric values are chosen to match with those used in
87  * NFSv3. */
88 typedef enum {
89     BLUESKY_REGULAR = 1,
90     BLUESKY_DIRECTORY = 2,
91     BLUESKY_BLOCK = 3,
92     BLUESKY_CHARACTER = 4,
93     BLUESKY_SYMLINK = 5,
94     BLUESKY_SOCKET = 6,
95     BLUESKY_FIFO = 7,
96 } BlueSkyFileType;
97
98 /* Filesystem state.  Each filesystem which is exported is represented by a
99  * single bluesky_fs structure in memory. */
100 typedef struct {
101     GMutex *lock;
102
103     gchar *name;                /* Descriptive name for the filesystem */
104     GHashTable *inodes;         /* Cached inodes */
105     uint64_t next_inum;         /* Next available inode for allocation */
106
107     BlueSkyStore *store;
108 } BlueSkyFS;
109
110 /* Inode number of the root directory. */
111 #define BLUESKY_ROOT_INUM 1
112
113 /* Timestamp, measured in microseconds since the Unix epoch. */
114 typedef int64_t bluesky_time;
115
116 /* High-resolution timer, measured in nanoseconds. */
117 typedef int64_t bluesky_time_hires;
118 bluesky_time_hires bluesky_now_hires();
119
120 /* In-memory representation of an inode within a Blue Sky server.  This
121  * corresponds roughly with information that is committed to persistent
122  * storage.  Locking/refcounting rules:
123  *   - To access or modify any data fields, the lock must be held.  This
124  *     includes file blocks.
125  *   - One reference is held by the BlueSkyFS inode hash table.  If that is the
126  *     only reference (and the inode is unlocked), the inode is subject to
127  *     dropping from the cache.
128  *   - Any pending operations should hold extra references to the inode as
129  *     appropriate to keep it available until the operation completes.
130  * */
131 typedef struct {
132     GMutex *lock;
133     gint refcount;
134
135     BlueSkyFS *fs;
136
137     BlueSkyFileType type;
138     uint32_t mode;
139     uint32_t uid, gid;
140     uint32_t nlink;
141
142     /* Rather than track an inode number and generation number, we will simply
143      * never re-use a fileid after a file is deleted.  64 bits should be enough
144      * that we don't exhaust the identifier space. */
145     uint64_t inum;
146
147     /* change_count is increased with every operation which modifies the inode,
148      * and can be used to determine if cached data is still valid.
149      * change_commit is the value of change_count when the inode was last
150      * committed to stable storage. */
151     uint64_t change_count, change_commit;
152
153     int64_t atime;              /* Microseconds since the Unix epoch */
154     int64_t ctime;
155     int64_t mtime;
156     int64_t ntime;              /* "new time": time object was created */
157
158     /* File-specific fields */
159     uint64_t size;
160     GArray *blocks;
161
162     /* Directory-specific fields */
163     GSequence *dirents;         /* List of entries for READDIR */
164     GHashTable *dirhash;        /* Hash table by name for LOOKUP */
165     GHashTable *dirhash_folded; /* As above, but case-folded */
166     uint64_t parent_inum;       /* inode for ".."; 0 if the root directory */
167 } BlueSkyInode;
168
169 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
170  * Each directory entry is listed in two indices: dirents is indexed by cookie
171  * and dirhash by name.  The cookie is a randomly-assigned 32-bit value, unique
172  * within the directory, that remains unchanged until the entry is deleted.  It
173  * is used to provide a stable key for restarting a READDIR call. */
174 typedef struct {
175     gchar *name;
176     gchar *name_folded;         /* Name, folded for case-insensitive lookup */
177     uint32_t cookie;
178     uint64_t inum;
179 } BlueSkyDirent;
180
181 /* File data is divided into fixed-size blocks (except the last block which may
182  * be short?).  These blocks are backed by storage in a key/value store, but
183  * may also be dirty if modifications have been made in-memory that have not
184  * been committed. */
185 #define BLUESKY_BLOCK_SIZE 32768ULL
186 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
187 typedef enum {
188     BLUESKY_BLOCK_ZERO = 0,     /* Data is all zeroes, not explicitly stored */
189     BLUESKY_BLOCK_REF = 1,      /* Reference to key/value store, not cached */
190     BLUESKY_BLOCK_CACHED = 2,   /* Data is cached in memory, clean */
191     BLUESKY_BLOCK_DIRTY = 3,    /* Data needs to be committed to store */
192 } BlueSkyBlockType;
193
194 typedef struct {
195     BlueSkyBlockType type;
196     gchar *ref;                 /* Name of data block in the backing store */
197     BlueSkyRCStr *data;         /* Pointer to data in memory if cached */
198 } BlueSkyBlock;
199
200 BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store);
201 void bluesky_superblock_flush(BlueSkyFS *fs);
202
203 int64_t bluesky_get_current_time();
204 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
205 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
206 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
207
208 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
209 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
210
211 void bluesky_dirent_destroy(gpointer dirent);
212 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
213 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name);
214 BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie);
215 gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
216                                   uint64_t inum);
217 void bluesky_directory_dump(BlueSkyInode *dir);
218
219 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
220 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
221 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block);
222 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
223 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
224                         const char *data, gint len);
225 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
226                        char *buf, gint len);
227
228 void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
229 void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
230
231 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
232                             gpointer unused);
233
234 #ifdef __cplusplus
235 }
236 #endif
237
238 #endif