Update ctime/mtime on write.
[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 (int64_t)t.tv_sec * 1000000 + t.tv_usec;
25 }
26
27 /* Update an inode to indicate that a modification was made.  This increases
28  * the change counter, updates the ctime to the current time, and optionally
29  * updates the mtime. */
30 void bluesky_inode_update_ctime(BlueSkyInode *inode, gboolean update_mtime)
31 {
32     int64_t now = bluesky_get_current_time();
33     inode->change_count++;
34     inode->ctime = now;
35     if (update_mtime)
36         inode->mtime = now;
37 }
38
39 /* Unfortunately a glib hash table is only guaranteed to be able to store
40  * 32-bit keys if we use the key directly.  If we want 64-bit inode numbers,
41  * we'll have to allocate memory to store the 64-bit inumber, and use a pointer
42  * to it.  Rather than allocate the memory for the key, we'll just include a
43  * pointer to the 64-bit inum stored in the inode itself, so that we don't need
44  * to do any more memory management.  */
45 static guint bluesky_fs_key_hash_func(gconstpointer key)
46 {
47     uint64_t inum = *(const uint64_t *)key;
48     return (guint)inum;
49 }
50
51 static gboolean bluesky_fs_key_equal_func(gconstpointer a, gconstpointer b)
52 {
53     uint64_t i1 = *(const uint64_t *)a;
54     uint64_t i2 = *(const uint64_t *)b;
55     return i1 == i2;
56 }
57
58 /* Filesystem-level operations.  A filesystem is like a directory tree that we
59  * are willing to export. */
60 BlueSkyFS *bluesky_new_fs(gchar *name)
61 {
62     BlueSkyFS *fs = g_new0(BlueSkyFS, 1);
63     fs->lock = g_mutex_new();
64     fs->name = g_strdup(name);
65     fs->inodes = g_hash_table_new(bluesky_fs_key_hash_func,
66                                   bluesky_fs_key_equal_func);
67     fs->next_inum = BLUESKY_ROOT_INUM + 1;
68
69     return fs;
70 }
71
72 /* Allocate a fresh inode number which has not been used before within a
73  * filesystem. */
74 uint64_t bluesky_fs_alloc_inode(BlueSkyFS *fs)
75 {
76     uint64_t inum;
77
78     g_mutex_lock(fs->lock);
79     inum = fs->next_inum;
80     fs->next_inum++;
81     g_mutex_unlock(fs->lock);
82
83     return inum;
84 }
85
86 BlueSkyInode *bluesky_new_inode(uint64_t inum, BlueSkyFileType type)
87 {
88     BlueSkyInode *i = g_new0(BlueSkyInode, 1);
89
90     i->lock = g_mutex_new();
91     i->type = type;
92     i->inum = inum;
93
94     switch (type) {
95     case BLUESKY_REGULAR:
96         i->blocks = g_array_new(FALSE, TRUE, sizeof(BlueSkyBlock));
97         break;
98     case BLUESKY_DIRECTORY:
99         i->dirents = g_sequence_new(bluesky_dirent_destroy);
100         i->dirhash = g_hash_table_new(g_str_hash, g_str_equal);
101         break;
102     case BLUESKY_BLOCK:
103     case BLUESKY_CHARACTER:
104     case BLUESKY_SYMLINK:
105     case BLUESKY_SOCKET:
106     case BLUESKY_FIFO:
107         break;
108     }
109
110     return i;
111 }
112
113 /* Retrieve an inode from the filesystem.  Eventually this will be a cache and
114  * so we might need to go fetch the inode from elsewhere; for now all
115  * filesystem state is stored here. */
116 BlueSkyInode *bluesky_get_inode(BlueSkyFS *fs, uint64_t inum)
117 {
118     BlueSkyInode *inode = NULL;
119
120     g_mutex_lock(fs->lock);
121     inode = (BlueSkyInode *)g_hash_table_lookup(fs->inodes, &inum);
122     g_mutex_unlock(fs->lock);
123
124     return inode;
125 }
126
127 /* Insert an inode into the filesystem inode cache. */
128 void bluesky_insert_inode(BlueSkyFS *fs, BlueSkyInode *inode)
129 {
130     g_mutex_lock(fs->lock);
131     g_hash_table_insert(fs->inodes, &inode->inum, inode);
132     g_mutex_unlock(fs->lock);
133 }
134
135 /* Set the size of a file.  This will truncate or extend the file as needed.
136  * Newly-allocated bytes are zeroed. */
137 void bluesky_file_truncate(BlueSkyInode *inode, uint64_t size)
138 {
139     g_return_if_fail(size <= BLUESKY_MAX_FILE_SIZE);
140
141     if (size == inode->size)
142         return;
143
144     uint64_t blocks = (size + BLUESKY_BLOCK_SIZE - 1) / BLUESKY_MAX_FILE_SIZE;
145
146     if (blocks > inode->blocks->len) {
147         /* Need to add new blocks to the end of a file.  New block structures
148          * are automatically zeroed, which initializes them to be pointers to
149          * zero blocks so we don't need to do any more work. */
150         g_array_set_size(inode->blocks, blocks);
151     } else if (blocks < inode->blocks->len) {
152         /* Delete blocks from a file.  Must reclaim memory. */
153         for (guint i = inode->blocks->len; i < blocks; i++) {
154             BlueSkyBlock *b = &g_array_index(inode->blocks, BlueSkyBlock, i);
155             g_free(b->ref);
156             g_free(b->data);
157         }
158         g_array_set_size(inode->blocks, blocks);
159     }
160
161     /* TODO: Zero out partial blocks if needed? */
162
163     inode->size = size;
164     bluesky_inode_update_ctime(inode, 1);
165 }