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>
14 /* Core filesystem: handling of directories. */
16 void bluesky_dirent_destroy(gpointer data)
18 BlueSkyDirent *dirent = (BlueSkyDirent *)data;
23 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
26 uint32_t hash1 = ((const BlueSkyDirent *)a)->cookie;
27 uint32_t hash2 = ((const BlueSkyDirent *)b)->cookie;
31 else if (hash1 > hash2)
37 /* Perform a lookup for a file name within a directory. Returns the inode
38 * number if found, or 0 if not (0 is never a valid inode number). Should be
39 * called with the inode lock already held. */
40 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name)
42 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
43 g_return_val_if_fail(inode->dirhash != NULL, 0);
45 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash, name);
52 /* Insert a new entry into a directory. Should be called with the inode lock
54 gboolean bluesky_directory_insert(BlueSkyInode *dir, gchar *name, uint64_t inum)
56 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
58 /* Check that no entry with the given name already exists. */
59 if (g_hash_table_lookup(dir->dirhash, name) != NULL)
62 BlueSkyDirent *d = g_new(BlueSkyDirent, 1);
63 d->name = g_strdup(name);
66 GSequence *dirents = dir->dirents;
68 /* Pick an unused cookie value for the directory at random. Restrict
69 * ourselves to positive 32-bit values (even if treated as signed), and
70 * keep the first four slots free. */
73 d->cookie = g_random_int() & 0x7fffffff;
74 } while (d->cookie < 4);
76 /* If the directory is empty, we can't possibly have a collision, so
77 * just go with the first key chosen. */
78 if (g_sequence_get_length(dirents) == 0)
81 /* Otherwise, try looking up the generated cookie. If we do not find a
82 * match, we can use this cookie value, otherwise we need to generate a
83 * new one and try again. Because of the check above for an empty
84 * directory, we know that the lookup will return something so no need
85 * to worry about NULL. */
86 GSequenceIter *i = g_sequence_search(dir->dirents, d,
87 bluesky_dirent_compare, NULL);
88 i = g_sequence_iter_prev(i);
89 if (((BlueSkyDirent *)g_sequence_get(i))->cookie != d->cookie)
93 /* Add the directory entry to both indices. */
94 g_sequence_insert_sorted(dirents, d, bluesky_dirent_compare, NULL);
95 g_hash_table_insert(dir->dirhash, d->name, d);
97 bluesky_inode_update_ctime(dir, 1);
98 bluesky_inode_flush(dir->fs, dir);
103 /* Remove an from a directory. Should be called with the inode lock already
105 gboolean bluesky_directory_remove(BlueSkyInode *dir, gchar *name)
107 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
109 BlueSkyDirent *d = g_hash_table_lookup(dir->dirhash, name);
114 g_hash_table_remove(dir->dirhash, name);
116 GSequenceIter *i = g_sequence_search(dir->dirents, d,
117 bluesky_dirent_compare, NULL);
118 i = g_sequence_iter_prev(i);
120 /* Assertion check, this ought to succeed */
121 g_return_val_if_fail(g_sequence_get(i) == d, FALSE);
123 g_sequence_remove(i);
125 bluesky_inode_update_ctime(dir, 1);
126 bluesky_inode_flush(dir->fs, dir);
131 /* Dump the contents of a directory to stdout. Debugging only. */
132 void bluesky_directory_dump(BlueSkyInode *dir)
134 g_print("Directory dump:\n");
136 GSequenceIter *i = g_sequence_get_begin_iter(dir->dirents);
138 while (!g_sequence_iter_is_end(i)) {
139 BlueSkyDirent *d = g_sequence_get(i);
140 g_print(" 0x%08x [inum=%lld] %s\n", d->cookie, d->inum, d->name);
141 i = g_sequence_iter_next(i);