1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
20 /* BlueSky status and error codes. Various frontends should translate these to
21 * the appropriate error code for whatever protocol they implement. */
23 BSTATUS_OK = 0, /* No error */
24 BSTATUS_IOERR, /* I/O error of some form */
25 BSTATUS_NOENT, /* File does not exist */
28 void bluesky_init(void);
30 gchar *bluesky_lowercase(const gchar *s);
32 /* Reference-counted blocks of memory, used for passing data in and out of
33 * storage backends and in other places. */
40 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
41 BlueSkyRCStr *bluesky_string_new_from_gstring(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);
47 /* Cryptographic operations. */
48 #define CRYPTO_BLOCK_SIZE 16 /* 128-bit AES */
49 #define CRYPTO_KEY_SIZE 16
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);
57 /* Storage interface. This presents a key-value store abstraction, and can
58 * have multiple implementations: in-memory, on-disk, in-cloud. */
60 typedef struct _BlueSkyStore BlueSkyStore;
62 void bluesky_store_init();
63 BlueSkyStore *bluesky_store_new(const gchar *type);
64 void bluesky_store_free(BlueSkyStore *store);
65 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
66 void bluesky_store_put(BlueSkyStore *store,
67 const gchar *key, BlueSkyRCStr *val);
69 /* File types. The numeric values are chosen to match with those used in
73 BLUESKY_DIRECTORY = 2,
75 BLUESKY_CHARACTER = 4,
81 /* Filesystem state. Each filesystem which is exported is represented by a
82 * single bluesky_fs structure in memory. */
86 gchar *name; /* Descriptive name for the filesystem */
87 GHashTable *inodes; /* Cached inodes */
88 uint64_t next_inum; /* Next available inode for allocation */
93 /* Inode number of the root directory. */
94 #define BLUESKY_ROOT_INUM 1
96 /* Timestamp, measured in microseconds since the Unix epoch. */
97 typedef int64_t bluesky_time;
99 /* High-resolution timer, measured in nanoseconds. */
100 typedef int64_t bluesky_time_hires;
101 bluesky_time_hires bluesky_now_hires();
103 /* In-memory representation of an inode within a Blue Sky server. This
104 * corresponds roughly with information that is committed to persistent
105 * storage. Locking/refcounting rules:
106 * - To access or modify any data fields, the lock must be held. This
107 * includes file blocks.
108 * - One reference is held by the BlueSkyFS inode hash table. If that is the
109 * only reference (and the inode is unlocked), the inode is subject to
110 * dropping from the cache.
111 * - Any pending operations should hold extra references to the inode as
112 * appropriate to keep it available until the operation completes.
120 BlueSkyFileType type;
125 /* Rather than track an inode number and generation number, we will simply
126 * never re-use a fileid after a file is deleted. 64 bits should be enough
127 * that we don't exhaust the identifier space. */
130 /* change_count is increased with every operation which modifies the inode,
131 * and can be used to determine if cached data is still valid.
132 * change_commit is the value of change_count when the inode was last
133 * committed to stable storage. */
134 uint64_t change_count, change_commit;
136 int64_t atime; /* Microseconds since the Unix epoch */
139 int64_t ntime; /* "new time": time object was created */
141 /* File-specific fields */
145 /* Directory-specific fields */
146 GSequence *dirents; /* List of entries for READDIR */
147 GHashTable *dirhash; /* Hash table by name for LOOKUP */
148 GHashTable *dirhash_folded; /* As above, but case-folded */
149 uint64_t parent_inum; /* inode for ".."; 0 if the root directory */
152 /* A directory entry. The name is UTF-8 and is a freshly-allocated string.
153 * Each directory entry is listed in two indices: dirents is indexed by cookie
154 * and dirhash by name. The cookie is a randomly-assigned 32-bit value, unique
155 * within the directory, that remains unchanged until the entry is deleted. It
156 * is used to provide a stable key for restarting a READDIR call. */
159 gchar *name_folded; /* Name, folded for case-insensitive lookup */
164 /* File data is divided into fixed-size blocks (except the last block which may
165 * be short?). These blocks are backed by storage in a key/value store, but
166 * may also be dirty if modifications have been made in-memory that have not
168 #define BLUESKY_BLOCK_SIZE 32768ULL
169 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
171 BLUESKY_BLOCK_ZERO = 0, /* Data is all zeroes, not explicitly stored */
172 BLUESKY_BLOCK_REF = 1, /* Reference to key/value store, not cached */
173 BLUESKY_BLOCK_CACHED = 2, /* Data is cached in memory, clean */
174 BLUESKY_BLOCK_DIRTY = 3, /* Data needs to be committed to store */
178 BlueSkyBlockType type;
179 gchar *ref; /* Name of data block in the backing store */
180 BlueSkyRCStr *data; /* Pointer to data in memory if cached */
183 BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store);
184 void bluesky_superblock_flush(BlueSkyFS *fs);
186 int64_t bluesky_get_current_time();
187 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
188 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
189 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
191 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
192 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
194 void bluesky_dirent_destroy(gpointer dirent);
195 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
196 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name);
197 BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie);
198 gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
200 void bluesky_directory_dump(BlueSkyInode *dir);
202 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
203 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
204 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block);
205 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
206 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
207 const char *data, gint len);
208 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
209 char *buf, gint len);
211 void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
212 void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
214 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,