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