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 void bluesky_init(void);
22 gchar *bluesky_lowercase(const gchar *s);
24 /* Reference-counted blocks of memory, used for passing data in and out of
25 * storage backends and in other places. */
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);
37 /* Cryptographic operations. */
38 #define CRYPTO_BLOCK_SIZE 16 /* 128-bit AES */
39 #define CRYPTO_KEY_SIZE 16
41 void bluesky_crypt_init();
42 void bluesky_crypt_hash_key(const char *keystr, uint8_t *out);
43 void bluesky_crypt_random_bytes(guchar *buf, gint len);
44 BlueSkyRCStr *bluesky_crypt_encrypt(BlueSkyRCStr *in, const uint8_t *key);
45 BlueSkyRCStr *bluesky_crypt_decrypt(BlueSkyRCStr *in, const uint8_t *key);
47 /* Storage interface. This presents a key-value store abstraction, and can
48 * have multiple implementations: in-memory, on-disk, in-cloud. */
50 /* Create a new store instance and return a handle to it. */
53 /* Clean up any resources used by this store. */
54 void (*destroy)(gpointer store);
56 /* Fetch an item with the given name, or return NULL if not found. */
57 BlueSkyRCStr * (*get)(gpointer store, const gchar *key);
59 /* Store an item to the given key name. */
60 void (*put)(gpointer store, const gchar *key, BlueSkyRCStr *val);
61 } BlueSkyStoreImplementation;
63 void bluesky_store_register(const BlueSkyStoreImplementation *impl,
67 typedef struct _BlueSkyStore BlueSkyStore;
69 void bluesky_store_init();
70 BlueSkyStore *bluesky_store_new(const gchar *type);
71 void bluesky_store_free(BlueSkyStore *store);
72 BlueSkyRCStr *bluesky_store_get(BlueSkyStore *store, const gchar *key);
73 void bluesky_store_put(BlueSkyStore *store,
74 const gchar *key, BlueSkyRCStr *val);
76 /* File types. The numeric values are chosen to match with those used in
80 BLUESKY_DIRECTORY = 2,
82 BLUESKY_CHARACTER = 4,
88 /* Filesystem state. Each filesystem which is exported is represented by a
89 * single bluesky_fs structure in memory. */
93 gchar *name; /* Descriptive name for the filesystem */
94 GHashTable *inodes; /* Cached inodes */
95 uint64_t next_inum; /* Next available inode for allocation */
100 /* Inode number of the root directory. */
101 #define BLUESKY_ROOT_INUM 1
103 /* Timestamp, measured in microseconds since the Unix epoch. */
104 typedef int64_t bluesky_time;
106 /* High-resolution timer, measured in nanoseconds. */
107 typedef int64_t bluesky_time_hires;
108 bluesky_time_hires bluesky_now_hires();
110 /* In-memory representation of an inode within a Blue Sky server. This
111 * corresponds roughly with information that is committed to persistent
112 * storage. Locking/refcounting rules:
113 * - To access or modify any data fields, the lock must be held. This
114 * includes file blocks.
115 * - One reference is held by the BlueSkyFS inode hash table. If that is the
116 * only reference (and the inode is unlocked), the inode is subject to
117 * dropping from the cache.
118 * - Any pending operations should hold extra references to the inode as
119 * appropriate to keep it available until the operation completes.
127 BlueSkyFileType type;
132 /* Rather than track an inode number and generation number, we will simply
133 * never re-use a fileid after a file is deleted. 64 bits should be enough
134 * that we don't exhaust the identifier space. */
137 /* change_count is increased with every operation which modifies the inode,
138 * and can be used to determine if cached data is still valid.
139 * change_commit is the value of change_count when the inode was last
140 * committed to stable storage. */
141 uint64_t change_count, change_commit;
143 int64_t atime; /* Microseconds since the Unix epoch */
146 int64_t ntime; /* "new time": time object was created */
148 /* File-specific fields */
152 /* Directory-specific fields */
153 GSequence *dirents; /* List of entries for READDIR */
154 GHashTable *dirhash; /* Hash table by name for LOOKUP */
155 GHashTable *dirhash_folded; /* As above, but case-folded */
156 uint64_t parent_inum; /* inode for ".."; 0 if the root directory */
159 /* A directory entry. The name is UTF-8 and is a freshly-allocated string.
160 * Each directory entry is listed in two indices: dirents is indexed by cookie
161 * and dirhash by name. The cookie is a randomly-assigned 32-bit value, unique
162 * within the directory, that remains unchanged until the entry is deleted. It
163 * is used to provide a stable key for restarting a READDIR call. */
166 gchar *name_folded; /* Name, folded for case-insensitive lookup */
171 /* File data is divided into fixed-size blocks (except the last block which may
172 * be short?). These blocks are backed by storage in a key/value store, but
173 * may also be dirty if modifications have been made in-memory that have not
175 #define BLUESKY_BLOCK_SIZE 32768ULL
176 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
178 BLUESKY_BLOCK_ZERO = 0, /* Data is all zeroes, not explicitly stored */
179 BLUESKY_BLOCK_REF = 1, /* Reference to key/value store, not cached */
180 BLUESKY_BLOCK_CACHED = 2, /* Data is cached in memory, clean */
181 BLUESKY_BLOCK_DIRTY = 3, /* Data needs to be committed to store */
185 BlueSkyBlockType type;
186 gchar *ref; /* Name of data block in the backing store */
187 BlueSkyRCStr *data; /* Pointer to data in memory if cached */
190 BlueSkyFS *bluesky_init_fs(gchar *name, BlueSkyStore *store);
191 void bluesky_superblock_flush(BlueSkyFS *fs);
193 int64_t bluesky_get_current_time();
194 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
195 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
196 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFS *fs, BlueSkyFileType type);
198 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
199 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
201 void bluesky_dirent_destroy(gpointer dirent);
202 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
203 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name);
204 gboolean bluesky_directory_insert(BlueSkyInode *dir, const gchar *name,
206 void bluesky_directory_dump(BlueSkyInode *dir);
208 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
209 void bluesky_block_fetch(BlueSkyFS *fs, BlueSkyBlock *block);
210 void bluesky_block_flush(BlueSkyFS *fs, BlueSkyBlock *block);
211 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
212 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
213 const char *data, gint len);
214 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
215 char *buf, gint len);
217 void bluesky_inode_flush(BlueSkyFS *fs, BlueSkyInode *inode);
218 void bluesky_inode_fetch(BlueSkyFS *fs, uint64_t inum);
220 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,