Hook NFS proxy together with BlueSky core.
[bluesky.git] / inode.c
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 #include <stdint.h>
10 #include <glib.h>
11
12 #include "bluesky.h"
13
14 /* Core filesystem.  Different proxies, such as the NFSv3 one, interface to
15  * this, but the core actually tracks the data which is stored.  So far we just
16  * implement an in-memory filesystem, but eventually this will be state which
17  * is persisted to the cloud. */
18
19 /* Return the current time, in microseconds since the epoch. */
20 int64_t bluesky_get_current_time()
21 {
22     GTimeVal t;
23     g_get_current_time(&t);
24     return t.tv_sec * 1000000 + t.tv_usec;
25 }
26
27 /* Unfortunately a glib hash table is only guaranteed to be able to store
28  * 32-bit keys if we use the key directly.  If we want 64-bit inode numbers,
29  * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
30  * to it.  Rather than allocate the memory for the key, we'll just include a
31  * pointer to the 64-bit inum stored in the inode itself, so that we don't need
32  * to do any more memory management.  */
33 static guint bluesky_fs_key_hash_func(gconstpointer key)
34 {
35     uint64_t inum = *(const uint64_t *)key;
36     return (guint)inum;
37 }
38
39 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
40 {
41     uint64_t i1 = *(const uint64_t *)a;
42     uint64_t i2 = *(const uint64_t *)b;
43     return i1 == i2;
44 }
45
46 /* Filesystem-level operations.  A filesystem is like a directory tree that we
47  * are willing to export. */
48 BlueSkyFS *bluesky_new_fs(gchar *name)
49 {
50     BlueSkyFS *fs = g_new0(BlueSkyFS, 1);
51     fs->lock = g_mutex_new();
52     fs->name = g_strdup(name);
53     fs->inodes = g_hash_table_new(bluesky_fs_key_hash_func,
54                                   bluesky_fs_key_equal_func);
55     fs->next_inum = BLUESKY_ROOT_INUM + 1;
56
57     return fs;
58 }
59
60 /* Allocate a fresh inode number which has not been used before within a
61  * filesystem. */
62 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
63 {
64     uint64_t inum;
65
66     g_mutex_lock(fs->lock);
67     inum = fs->next_inum;
68     fs->next_inum++;
69     g_mutex_unlock(fs->lock);
70
71     return inum;
72 }
73
74 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
75 {
76     BlueSkyInode *i = g_new0(BlueSkyInode, 1);
77
78     i->lock = g_mutex_new();
79     i->type = type;
80     i->inum = inum;
81
82     switch (type) {
83     case BLUESKY_REGULAR:
84         break;
85     case BLUESKY_DIRECTORY:
86         i->dirents = g_sequence_new(bluesky_dirent_destroy);
87     case BLUESKY_BLOCK:
88     case BLUESKY_CHARACTER:
89     case BLUESKY_SYMLINK:
90     case BLUESKY_SOCKET:
91     case BLUESKY_FIFO:
92         break;
93     }
94
95     return i;
96 }
97
98 /* Retrieve an inode from the filesystem.  Eventually this will be a cache and
99  * so we might need to go fetch the inode from elsewhere; for now all
100  * filesystem state is stored here. */
101 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
102 {
103     BlueSkyInode *inode = NULL;
104
105     g_mutex_lock(fs->lock);
106     inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
107     g_mutex_unlock(fs->lock);
108
109     return inode;
110 }
111
112 /* Insert an inode into the filesystem inode cache. */
113 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
114 {
115     g_mutex_lock(fs->lock);
116     g_hash_table_insert(fs->inodes, &inode->inum, inode);
117     g_mutex_unlock(fs->lock);
118 }