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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "bluesky-private.h"
37 /* Core filesystem: handling of directories. */
39 void bluesky_dirent_destroy(gpointer data)
41 BlueSkyDirent *dirent = (BlueSkyDirent *)data;
43 g_free(dirent->name_folded);
47 gint bluesky_dirent_compare(gconstpointer a, gconstpointer b,
50 uint32_t hash1 = ((const BlueSkyDirent *)a)->cookie;
51 uint32_t hash2 = ((const BlueSkyDirent *)b)->cookie;
55 else if (hash1 > hash2)
61 /* Perform a lookup for a file name within a directory. Returns the inode
62 * number if found, or 0 if not (0 is never a valid inode number). Should be
63 * called with the inode lock already held. */
64 uint64_t bluesky_directory_lookup(BlueSkyInode *inode, gchar *name)
66 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
67 g_return_val_if_fail(inode->dirhash != NULL, 0);
69 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash, name);
76 /* Case-insensitive lookup. */
77 uint64_t bluesky_directory_ilookup(BlueSkyInode *inode, gchar *name)
79 g_return_val_if_fail(inode->type == BLUESKY_DIRECTORY, 0);
80 g_return_val_if_fail(inode->dirhash_folded != NULL, 0);
82 name = bluesky_lowercase(name);
83 BlueSkyDirent *d = g_hash_table_lookup(inode->dirhash_folded, name);
92 /* Iterate through a directory listing. This returns one directory entry at a
93 * time, finding the first entry with a directory cookie value larger than the
94 * supplied one. Use a cookie of 0 to start reading from the start of a
96 BlueSkyDirent *bluesky_directory_read(BlueSkyInode *dir, uint32_t cookie)
98 BlueSkyDirent start = {NULL, NULL, cookie, 0};
99 GSequenceIter *i = g_sequence_search(dir->dirents, &start,
100 bluesky_dirent_compare, NULL);
102 if (g_sequence_iter_is_end(i))
105 return g_sequence_get(i);
108 /* Insert a new entry into a directory. Should be called with the inode lock
110 gboolean bluesky_directory_insert(BlueSkyInode *dir,
111 const gchar *name, uint64_t inum)
113 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
115 /* Check that no entry with the given name already exists. */
116 if (g_hash_table_lookup(dir->dirhash, name) != NULL)
119 BlueSkyDirent *d = g_new(BlueSkyDirent, 1);
120 d->name = g_strdup(name);
121 d->name_folded = bluesky_lowercase(name);
124 GSequence *dirents = dir->dirents;
126 /* Pick an unused cookie value for the directory at random. Restrict
127 * ourselves to positive 32-bit values (even if treated as signed), and
128 * keep the first four slots free. */
131 d->cookie = g_random_int() & 0x7fffffff;
132 } while (d->cookie < 4);
134 /* If the directory is empty, we can't possibly have a collision, so
135 * just go with the first key chosen. */
136 if (g_sequence_get_length(dirents) == 0)
139 /* Otherwise, try looking up the generated cookie. If we do not find a
140 * match, we can use this cookie value, otherwise we need to generate a
141 * new one and try again. Because of the check above for an empty
142 * directory, we know that the lookup will return something so no need
143 * to worry about NULL. */
144 GSequenceIter *i = g_sequence_search(dir->dirents, d,
145 bluesky_dirent_compare, NULL);
146 i = g_sequence_iter_prev(i);
147 if (((BlueSkyDirent *)g_sequence_get(i))->cookie != d->cookie)
151 /* Add the directory entry to both indices. */
152 g_sequence_insert_sorted(dirents, d, bluesky_dirent_compare, NULL);
153 g_hash_table_insert(dir->dirhash, d->name, d);
154 g_hash_table_insert(dir->dirhash_folded, d->name_folded, d);
156 bluesky_inode_update_ctime(dir, 1);
157 //bluesky_inode_do_sync(dir); // TODO: Needed?
162 /* Remove an entry from a directory. Should be called with the inode lock
164 gboolean bluesky_directory_remove(BlueSkyInode *dir, gchar *name)
166 g_return_val_if_fail(dir->type == BLUESKY_DIRECTORY, FALSE);
168 BlueSkyDirent *d = g_hash_table_lookup(dir->dirhash, name);
173 g_hash_table_remove(dir->dirhash, d->name);
174 g_hash_table_remove(dir->dirhash_folded, d->name_folded);
176 GSequenceIter *i = g_sequence_search(dir->dirents, d,
177 bluesky_dirent_compare, NULL);
178 i = g_sequence_iter_prev(i);
180 /* Assertion check, this ought to succeed */
181 g_return_val_if_fail(g_sequence_get(i) == d, FALSE);
183 g_sequence_remove(i);
185 bluesky_inode_update_ctime(dir, 1);
190 /* Rename a file. If desired (if overwrite is true) and if the target already
191 * exists, it will be unlinked first. */
192 gboolean bluesky_rename(BlueSkyInode *dir1, gchar *name1,
193 BlueSkyInode *dir2, gchar *name2,
194 gboolean case_sensitive,
197 g_return_val_if_fail(dir1->type == BLUESKY_DIRECTORY, FALSE);
198 g_return_val_if_fail(dir2->type == BLUESKY_DIRECTORY, FALSE);
200 BlueSkyDirent *d1, *d2;
202 d1 = g_hash_table_lookup(case_sensitive ? dir1->dirhash
203 : dir1->dirhash_folded, name1);
204 d2 = g_hash_table_lookup(case_sensitive ? dir2->dirhash
205 : dir2->dirhash_folded, name2);
210 uint64_t inum = d1->inum;
212 /* Check that this rename does not cause a directory to be moved into one
213 * of its descendants, as that would create a loop of directories
214 * disconnected from the root. */
221 bluesky_directory_remove(dir2, name2);
223 // TODO: Drop inode reference
226 bluesky_directory_remove(dir1, name1);
227 bluesky_directory_insert(dir2, name2, inum);
232 /* Dump the contents of a directory to stdout. Debugging only. */
233 void bluesky_directory_dump(BlueSkyInode *dir)
235 g_print("Directory dump:\n");
237 GSequenceIter *i = g_sequence_get_begin_iter(dir->dirents);
239 while (!g_sequence_iter_is_end(i)) {
240 BlueSkyDirent *d = g_sequence_get(i);
241 g_print(" 0x%08x [inum=%"PRIu64"] %s\n",
242 d->cookie, d->inum, d->name);
243 i = g_sequence_iter_next(i);