068173f8c2c2874b06b90487e7102336168224ea
[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 enum BlueSkyFileType {
18     BLUESKY_INVALID = 0,
19     BLUESKY_REGULAR = 1,
20     BLUESKY_DIRECTORY = 2,
21     BLUESKY_BLOCK = 3,
22     BLUESKY_CHARACTER = 4,
23     BLUESKY_SYMLINK = 5,
24     BLUESKY_SOCKET = 6,
25     BLUESKY_FIFO = 7,
26 };
27
28 /* Filesystem state.  Each filesystem which is exported is represented by a
29  * single bluesky_fs structure in memory. */
30 typedef struct {
31     GMutex *lock;
32
33     gchar *name;                /* Descriptive name for the filesystem */
34     GHashTable *inodes;         /* Cached inodes */
35     uint64_t next_inum;         /* Next available inode for allocation */
36 } BlueSkyFS;
37
38 /* Timestamp, measured in microseconds since the Unix epoch. */
39 typedef int64_t bluesky_time;
40
41 /* In-memory representation of an inode within a Blue Sky server.  This
42  * corresponds roughly with information that is committed to persistent
43  * storage. */
44 typedef struct {
45     gint refcnt;                /* May be accessed atomically without lock */
46     GMutex *lock;
47
48     int type;
49     uint32_t mode;
50     uint32_t uid, gid;
51     uint32_t nlink;
52
53     /* Rather than track an inode number and generation number, we will simply
54      * never re-use a fileid after a file is deleted.  64 bits should be enough
55      * that we don't exhaust the identifier space. */
56     uint64_t inum;
57
58     uint64_t change_count;      /* Incremented each with each change made */
59     int64_t atime;              /* Microseconds since the Unix epoch */
60     int64_t ctime;
61     int64_t mtime;
62     int64_t ntime;              /* "new time": time object was created */
63
64     /* File-specific fields */
65     uint64_t size;
66
67     /* Directory-specific fields */
68     GSequence *dirents;
69 } BlueSkyInode;
70
71 /* A directory entry.  The name is UTF-8 and is a freshly-allocated string.
72  * The name is hashed to a 64-bit value, and the directory entries are sorted
73  * by hash value (the hash value can then be used as a cookie for resuming a
74  * READDIR call). */
75 typedef struct {
76     gchar *name;
77     uint64_t hash;
78     uint64_t inum;
79 } BlueSkyDirent ;
80
81 int64_t bluesky_get_current_time();
82 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs);
83 BlueSkyInode *bluesky_new_inode(uint64_t inum);
84
85 void bluesky_dirent_destroy(gpointer dirent);
86 uint64_t bluesky_directory_hash(gchar *name);
87 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name);
88 gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name,
89                                   uint64_t inum);
90 void bluesky_directory_dump(BlueSkyInode *dir);
91
92 #endif