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