1 /* This file contains directory entry management and the name lookup hashtable.
3 * The entry points into this file are:
4 * init_dentry initialize the directory entry name lookup hashtable
5 * lookup_dentry find an inode based on parent directory and name
6 * add_dentry add an inode as directory entry to a parent directory
7 * del_dentry delete an inode from its parent directory
10 * April 2009 (D.C. van Moolenbroek)
15 static LIST_HEAD(hash_head
, inode
) hash_table
[NUM_HASH_SLOTS
];
17 static void del_one_dentry(struct inode
*ino
);
18 static unsigned int hash_dentry(struct inode
*parent
, char *name
);
20 /*===========================================================================*
22 *===========================================================================*/
25 /* Initialize the names hashtable.
29 for (i
= 0; i
< NUM_HASH_SLOTS
; i
++)
30 LIST_INIT(&hash_table
[i
]);
33 /*===========================================================================*
35 *===========================================================================*/
36 struct inode
*lookup_dentry(parent
, name
)
40 /* Given a directory inode and a component name, look up the inode associated
41 * with that directory entry. Return the inode (with increased reference
42 * count) if found, or NULL otherwise.
47 assert(IS_DIR(parent
));
49 slot
= hash_dentry(parent
, name
);
51 LIST_FOREACH(ino
, &hash_table
[slot
], i_hash
) {
52 if (compare_name(ino
->i_name
, name
) == TRUE
)
64 /*===========================================================================*
66 *===========================================================================*/
67 void add_dentry(parent
, name
, ino
)
72 /* Add an entry to a parent inode, in the form of a new inode, with the given
73 * name. An entry with this name must not already exist.
77 assert(IS_DIR(parent
));
78 assert(parent
->i_ref
> 0);
79 assert(ino
->i_ref
> 0);
81 assert(strlen(name
) <= NAME_MAX
);
83 link_inode(parent
, ino
);
85 strlcpy(ino
->i_name
, name
, sizeof(ino
->i_name
));
88 slot
= hash_dentry(parent
, ino
->i_name
);
89 LIST_INSERT_HEAD(&hash_table
[slot
], ino
, i_hash
);
92 /*===========================================================================*
94 *===========================================================================*/
95 static void del_one_dentry(ino
)
98 /* This inode has become inaccessible by name. Disassociate it from its parent
99 * and remove it from the names hash table.
102 /* There can and must be exactly one root inode, so don't delete it! */
106 /* INUSE -> DELETED, CACHED -> FREE */
108 /* Remove the entry from the hashtable.
109 * Decrease parent's refcount, possibly adding it to the free list.
110 * Do not touch open handles. Do not add to the free list.
113 assert(ino
->i_parent
!= NULL
);
116 LIST_REMOVE(ino
, i_hash
);
123 /*===========================================================================*
125 *===========================================================================*/
129 /* Disassociate an inode from its parent, effectively deleting it. Recursively
130 * delete all its children as well, fragmenting the deleted branch into single
133 LIST_HEAD(work_list
, inode
) work_list
;
138 /* Quick way out: one directory entry that itself has no children. */
139 if (!HAS_CHILDREN(ino
))
142 /* Recursively delete all children of the inode as well.
143 * Iterative version: this is potentially 128 levels deep.
146 LIST_INIT(&work_list
);
147 LIST_INSERT_HEAD(&work_list
, ino
, i_next
);
150 ino
= LIST_FIRST(&work_list
);
151 LIST_REMOVE(ino
, i_next
);
155 while (!LIST_EMPTY(&ino
->i_child
)) {
156 child
= LIST_FIRST(&ino
->i_child
);
157 LIST_REMOVE(child
, i_next
);
159 del_one_dentry(child
);
161 if (HAS_CHILDREN(child
))
162 LIST_INSERT_HEAD(&work_list
, child
, i_next
);
164 } while (!LIST_EMPTY(&work_list
));
167 /*===========================================================================*
169 *===========================================================================*/
170 static unsigned int hash_dentry(parent
, name
)
171 struct inode
*parent
;
174 /* Generate a hash value for a given name. Normalize the name first, so that
175 * different variations of the name will result in the same hash value.
178 char buf
[NAME_MAX
+1], *p
;
180 dprintf(("%s: hash_dentry for '%s'\n", sffs_name
, name
));
182 normalize_name(buf
, name
);
184 /* djb2 string hash algorithm, XOR variant */
186 for (p
= buf
; *p
; p
++)
187 val
= ((val
<< 5) + val
) ^ *p
;
189 /* Mix with inode number: typically, many file names occur in several
190 * different directories.
192 return (val
^ parent
->i_num
) % NUM_HASH_SLOTS
;