5571db61edca13bce778d41aed01e70a288a8f77
[bluesky.git] / 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 <glib.h>
14
15 /* Reference-counted blocks of memory, used for passing data in and out of
16  * storage backends and in other places. */
17 typedef struct {
18     gint refcount;
19     gpointer data;
20     gsize len;
21 } BlueSkyRCStr;
22
23 BlueSkyRCStr *bluesky_string_new(gpointer data, gsize len);
24 void bluesky_string_ref(BlueSkyRCStr *string);
25 void bluesky_string_unref(BlueSkyRCStr *string);
26 BlueSkyRCStr *bluesky_string_dup(BlueSkyRCStr *string);
27
28 /* File types.  The numeric values are chosen to match with those used in
29  * NFSv3. */
30 typedef enum {
31     BLUESKY_REGULAR = 1,
32     BLUESKY_DIRECTORY = 2,
33     BLUESKY_BLOCK = 3,
34     BLUESKY_CHARACTER = 4,
35     BLUESKY_SYMLINK = 5,
36     BLUESKY_SOCKET = 6,
37     BLUESKY_FIFO = 7,
38 } BlueSkyFileType;
39
40 /* Filesystem state.  Each filesystem which is exported is represented by a
41  * single bluesky_fs structure in memory. */
42 typedef struct {
43     GMutex *lock;
44
45     gchar *name;                /* Descriptive name for the filesystem */
46     GHashTable *inodes;         /* Cached inodes */
47     uint64_t next_inum;         /* Next available inode for allocation */
48 } BlueSkyFS;
49
50 /* Inode number of the root directory. */
51 #define BLUESKY_ROOT_INUM 1
52
53 /* Timestamp, measured in microseconds since the Unix epoch. */
54 typedef int64_t bluesky_time;
55
56 /* In-memory representation of an inode within a Blue Sky server.  This
57  * corresponds roughly with information that is committed to persistent
58  * storage. */
59 typedef struct {
60     gint refcnt;                /* May be accessed atomically without lock */
61     GMutex *lock;
62
63     BlueSkyFileType type;
64     uint32_t mode;
65     uint32_t uid, gid;
66     uint32_t nlink;
67
68     /* Rather than track an inode number and generation number, we will simply
69      * never re-use a fileid after a file is deleted.  64 bits should be enough
70      * that we don't exhaust the identifier space. */
71     uint64_t inum;
72
73     uint64_t change_count;      /* Incremented each with each change made */
74     int64_t atime;              /* Microseconds since the Unix epoch */
75     int64_t ctime;
76     int64_t mtime;
77     int64_t ntime;              /* "new time": time object was created */
78
79     /* File-specific fields */
80     uint64_t size;
81     GArray *blocks;
82
83     /* Directory-specific fields */
84     GSequence *dirents;         /* List of entries for READDIR */
85     GHashTable *dirhash;        /* Hash table by name for LOOKUP */
86     uint64_t parent_inum;       /* inode for ".."; 0 if the root directory */
87 } BlueSkyInode;
88
89 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
90  * Each directory entry is listed in two indices: dirents is indexed by cookie
91  * and dirhash by name.  The cookie is a randomly-assigned 32-bit value, unique
92  * within the directory, that remains unchanged until the entry is deleted.  It
93  * is used to provide a stable key for restarting a READDIR call. */
94 typedef struct {
95     gchar *name;
96     uint32_t cookie;
97     uint64_t inum;
98 } BlueSkyDirent;
99
100 /* File data is divided into fixed-size blocks (except the last block which may
101  * be short?).  These blocks are backed by storage in a key/value store, but
102  * may also be dirty if modifications have been made in-memory that have not
103  * been committed. */
104 #define BLUESKY_BLOCK_SIZE 32768ULL
105 #define BLUESKY_MAX_FILE_SIZE (BLUESKY_BLOCK_SIZE << 24)
106 typedef enum {
107     BLUESKY_BLOCK_ZERO = 0,     /* Data is all zeroes, not explicitly stored */
108     BLUESKY_BLOCK_REF = 1,      /* Reference to key/value store, not cached */
109     BLUESKY_BLOCK_CACHED = 2,   /* Data is cached in memory, clean */
110     BLUESKY_BLOCK_DIRTY = 3,    /* Data needs to be committed to store */
111 } BlueSkyBlockType;
112
113 typedef struct {
114     BlueSkyBlockType type;
115     gchar *ref;                 /* Name of data block in the backing store */
116     gchar *data;                /* Pointer to data in memory */
117 } BlueSkyBlock;
118
119 BlueSkyFS *bluesky_new_fs(gchar *name);
120 int64_t bluesky_get_current_time();
121 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime);
122 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
123 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type);
124
125 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum);
126 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode);
127
128 void bluesky_dirent_destroy(gpointer dirent);
129 uint64_t bluesky_directory_hash(gchar *name);
130 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
131 gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name,
132                                   uint64_t inum);
133 void bluesky_directory_dump(BlueSkyInode *dir);
134
135 void bluesky_block_touch(BlueSkyInode *inode, uint64_t i);
136 void bluesky_block_flush(BlueSkyBlock *block);
137 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size);
138 void bluesky_file_write(BlueSkyInode *inode, uint64_t offset,
139                         const char *data, gint len);
140 void bluesky_file_read(BlueSkyInode *inode, uint64_t offset,
141                        char *buf, gint len);
142
143 #endif