1 /* Blue Sky: File Systems in the Cloud
3 * Copyright (C) 2009 The Regents of the University of California
4 * Written by Michael Vrable <mvrable@cs.ucsd.edu>
15 /* Core filesystem: handling of directories. */
17 void bluesky_dirent_destroy(gpointer data)
19 BlueSkyDirent *dirent = (BlueSkyDirent *)data;
24 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
27 uint32_t hash1 = ((const BlueSkyDirent *)a)->cookie;
28 uint32_t hash2 = ((const BlueSkyDirent *)b)->cookie;
32 else if (hash1 > hash2)
38 /* Perform a lookup for a file name within a directory. Returns the inode
39 * number if found, or 0 if not (0 is never a valid inode number). Should be
40 * called with the inode lock already held. */
41 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name)
43 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
44 g_return_val_if_fail(inode->dirhash != NULL, 0);
46 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash, name);
53 /* Insert a new entry into a directory. Should be called with the inode lock
55 gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name, uint64_t inum)
57 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
59 /* Check that no entry with the given name already exists. */
60 if (g_hash_table_lookup(dir->dirhash, name) != NULL)
63 BlueSkyDirent *d = g_new(BlueSkyDirent, 1);
64 d->name = g_strdup(name);
67 GSequence *dirents = dir->dirents;
69 /* Pick an unused cookie value for the directory at random. Restrict
70 * ourselves to positive 32-bit values (even if treated as signed), and
71 * keep the first four slots free. */
74 d->cookie = g_random_int() & 0x7fffffff;
75 } while (d->cookie < 4);
77 /* If the directory is empty, we can't possibly have a collision, so
78 * just go with the first key chosen. */
79 if (g_sequence_get_length(dirents) == 0)
82 /* Otherwise, try looking up the generated cookie. If we do not find a
83 * match, we can use this cookie value, otherwise we need to generate a
84 * new one and try again. Because of the check above for an empty
85 * directory, we know that the lookup will return something so no need
86 * to worry about NULL. */
87 GSequenceIter *i = g_sequence_search(dir->dirents, d,
88 bluesky_dirent_compare, NULL);
89 i = g_sequence_iter_prev(i);
90 if (((BlueSkyDirent *)g_sequence_get(i))->cookie != d->cookie)
94 /* Add the directory entry to both indices. */
95 g_sequence_insert_sorted(dirents, d, bluesky_dirent_compare, NULL);
96 g_hash_table_insert(dir->dirhash, d->name, d);
98 bluesky_inode_update_ctime(dir, 1);
99 bluesky_inode_flush(dir->fs, dir);
104 /* Remove an from a directory. Should be called with the inode lock already
106 gboolean bluesky_directory_remove(BlueSkyInode *dir, gchar *name)
108 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
110 BlueSkyDirent *d = g_hash_table_lookup(dir->dirhash, name);
115 g_hash_table_remove(dir->dirhash, name);
117 GSequenceIter *i = g_sequence_search(dir->dirents, d,
118 bluesky_dirent_compare, NULL);
119 i = g_sequence_iter_prev(i);
121 /* Assertion check, this ought to succeed */
122 g_return_val_if_fail(g_sequence_get(i) == d, FALSE);
124 g_sequence_remove(i);
126 bluesky_inode_update_ctime(dir, 1);
127 bluesky_inode_flush(dir->fs, dir);
132 /* Dump the contents of a directory to stdout. Debugging only. */
133 void bluesky_directory_dump(BlueSkyInode *dir)
135 g_print("Directory dump:\n");
137 GSequenceIter *i = g_sequence_get_begin_iter(dir->dirents);
139 while (!g_sequence_iter_is_end(i)) {
140 BlueSkyDirent *d = g_sequence_get(i);
141 g_print(" 0x%08x [inum=%"PRIu64"] %s\n",
142 d->cookie, d->inum, d->name);
143 i = g_sequence_iter_next(i);