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