4 * Code to deal with hard links in WIMs. Essentially, the WIM dentries are put
5 * into a hash table indexed by the inode ID field, then for each hard
6 * inode, a linked list is made to connect the dentries.
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
36 * / \ ----------- -----------
37 * | dentry<---| struct | | struct |---> dentry
39 * dentry ------------ ------------
43 * ----------- ----------- / \
44 * dentry<---| struct | | struct |---> dentry dentry
45 * / |inode| |inode| \ /
46 * dentry ------------ ------------ dentry
50 * inode_table->array | idx 0 | idx 1 |
55 int init_inode_table(struct inode_table *table, size_t capacity)
57 table->array = CALLOC(capacity, sizeof(table->array[0]));
59 ERROR("Cannot initalize inode table: out of memory");
60 return WIMLIB_ERR_NOMEM;
62 table->num_entries = 0;
63 table->capacity = capacity;
64 INIT_HLIST_HEAD(&table->extra_inodes);
69 static size_t inode_link_count(const struct inode *inode)
71 const struct list_head *cur;
73 list_for_each(cur, &inode->dentry_list)
78 static struct dentry *inode_first_dentry(struct inode *inode)
80 return container_of(inode->dentry_list.next, struct dentry,
85 * Insert a dentry into the inode table based on its inode
88 * If there is already a dentry in the table having the same inode ID,
89 * and the inode ID is not 0, the dentry is added to the circular
90 * linked list for that inode.
92 * If the inode ID is 0, this indicates a dentry that's in a hard link
93 * inode by itself (has a link count of 1). We can't insert it into the hash
94 * table itself because we don't know what inode numbers are available to
95 * give it (this could be kept track of but would be more difficult). Instead
96 * we keep a linked list of the single dentries, and assign them inode
99 int inode_table_insert(struct dentry *dentry, void *__table)
101 struct inode_table *table = __table;
102 struct inode *d_inode = dentry->inode;
104 if (d_inode->ino == 0) {
105 /* Single inode--- Add to the list of extra inodes (we can't put
106 * it in the table itself because all the singles have a link
108 hlist_add_head(&d_inode->hlist, &table->extra_inodes);
110 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
111 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
112 wimlib_assert(d_inode->link_count == 1);
114 /* Inode that may have multiple corresponding dentries (the code
115 * will work even if the inode actually contains only 1 dentry
120 struct hlist_node *cur;
122 /* Try adding to existing inode */
123 pos = d_inode->ino % table->capacity;
124 hlist_for_each_entry(inode, cur, &table->array[pos], hlist) {
125 if (inode->ino == d_inode->ino) {
126 list_add(&dentry->inode_dentry_list,
127 &inode->dentry_list);
133 /* Add new inode to the table */
134 hlist_add_head(&d_inode->hlist, &table->array[pos]);
136 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
137 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
138 wimlib_assert(d_inode->link_count == 1);
140 /* XXX Make the table grow when too many entries have been
142 table->num_entries++;
147 /* Assign the inode numbers to dentries in a inode table, and return the
148 * next available inode ID. */
149 u64 assign_inode_numbers(struct hlist_head *inode_list)
152 struct hlist_node *cur;
154 struct dentry *dentry;
155 hlist_for_each_entry(inode, cur, inode_list, hlist) {
156 inode->ino = cur_ino;
164 print_inode_dentries(const struct inode *inode)
166 struct dentry *dentry;
167 list_for_each_entry(dentry, &inode->dentry_list, inode_dentry_list)
168 printf("`%s'\n", dentry->full_path_utf8);
171 static void inconsistent_inode(const struct inode *inode)
173 ERROR("An inconsistent hard link group that we cannot correct has been "
175 ERROR("The dentries are located at the following paths:");
176 print_inode_dentries(inode);
179 static bool ref_inodes_consistent(const struct inode * restrict ref_inode_1,
180 const struct inode * restrict ref_inode_2)
182 wimlib_assert(ref_inode_1 != ref_inode_2);
184 if (ref_inode_1->num_ads != ref_inode_2->num_ads)
186 if (ref_inode_1->security_id != ref_inode_2->security_id
187 || ref_inode_1->attributes != ref_inode_2->attributes)
189 for (unsigned i = 0; i <= ref_inode_1->num_ads; i++) {
190 const u8 *ref_1_hash, *ref_2_hash;
191 ref_1_hash = inode_stream_hash(ref_inode_1, i);
192 ref_2_hash = inode_stream_hash(ref_inode_2, i);
193 if (!hashes_equal(ref_1_hash, ref_2_hash))
195 if (i && !ads_entries_have_same_name(ref_inode_1->ads_entries[i - 1],
196 ref_inode_2->ads_entries[i - 1]))
203 static bool inodes_consistent(const struct inode * restrict ref_inode,
204 const struct inode * restrict inode)
206 wimlib_assert(ref_inode != inode);
208 if (ref_inode->num_ads != inode->num_ads &&
211 if (ref_inode->security_id != inode->security_id
212 || ref_inode->attributes != inode->attributes)
214 for (unsigned i = 0; i <= min(ref_inode->num_ads, inode->num_ads); i++) {
215 const u8 *ref_hash, *hash;
216 ref_hash = inode_stream_hash(ref_inode, i);
217 hash = inode_stream_hash(inode, i);
218 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
220 if (i && !ads_entries_have_same_name(ref_inode->ads_entries[i - 1],
221 inode->ads_entries[i - 1]))
229 print_dentry_list(const struct dentry *first_dentry)
231 const struct dentry *dentry = first_dentry;
233 printf("`%s'\n", dentry->full_path_utf8);
234 } while ((dentry = container_of(dentry->inode_dentry_list.next,
236 inode_dentry_list)) != first_dentry);
242 /* Fix up a "true" inode and check for inconsistencies */
243 static int fix_true_inode(struct inode *inode)
245 struct dentry *dentry;
246 struct dentry *ref_dentry = NULL;
247 struct inode *ref_inode;
251 bool found_short_name = false;
253 list_for_each_entry(dentry, &inode->dentry_list, inode_dentry_list) {
254 if (!ref_dentry || ref_dentry->inode->num_ads == 0)
256 if (dentry->short_name_len) {
257 if (found_short_name) {
258 ERROR("Multiple short names in hard link "
260 inconsistent_inode(inode);
261 return WIMLIB_ERR_INVALID_DENTRY;
263 found_short_name = true;
266 if (dentry->inode->creation_time > last_ctime)
267 last_ctime = dentry->inode->creation_time;
268 if (dentry->inode->last_write_time > last_mtime)
269 last_mtime = dentry->inode->last_write_time;
270 if (dentry->inode->last_access_time > last_atime)
271 last_atime = dentry->inode->last_access_time;
274 ref_inode = ref_dentry->inode;
275 ref_inode->link_count = 1;
277 list_for_each_entry(dentry, &inode->dentry_list, inode_dentry_list) {
278 if (dentry != ref_dentry) {
279 if (!inodes_consistent(ref_inode, dentry->inode)) {
280 inconsistent_inode(dentry->inode);
281 return WIMLIB_ERR_INVALID_DENTRY;
283 /* Free the unneeded `struct inode'. */
284 free_inode(dentry->inode);
285 dentry->inode = ref_inode;
286 ref_inode->link_count++;
289 ref_inode->creation_time = last_ctime;
290 ref_inode->last_write_time = last_mtime;
291 ref_inode->last_access_time = last_atime;
292 wimlib_assert(inode_link_count(ref_inode) == ref_inode->link_count);
297 * Fixes up a nominal inode.
299 * By a nominal inode we mean a group of two or more dentries that share
300 * the same hard link group ID.
302 * If dentries in the inode are found to be inconsistent, we may split the inode
303 * into several "true" inodes. @new_inodes points to a linked list of
304 * these split inodes, and if we create any, they will be added to this list.
306 * After splitting up each nominal inode into the "true" inodes we
307 * will canonicalize the link group by getting rid of all the superfluous
308 * `struct inodes'. There will be just one `struct inode' for each hard link
312 fix_nominal_inode(struct inode *inode, struct hlist_head *inode_list)
314 struct dentry *dentry, *ref_dentry;
315 struct hlist_node *cur, *tmp;
317 size_t num_true_inodes;
319 wimlib_assert(inode->link_count == inode_link_count(inode));
321 LIST_HEAD(dentries_with_data_streams);
322 LIST_HEAD(dentries_with_no_data_streams);
323 HLIST_HEAD(true_inodes);
325 /* Create a list of dentries in the nominal inode that have at
326 * least one data stream with a non-zero hash, and another list that
327 * contains the dentries that have a zero hash for all data streams. */
328 list_for_each_entry(dentry, &inode->dentry_list, inode_dentry_list) {
329 for (unsigned i = 0; i <= dentry->inode->num_ads; i++) {
331 hash = inode_stream_hash(dentry->inode, i);
332 if (!is_zero_hash(hash)) {
333 list_add(&dentry->tmp_list,
334 &dentries_with_data_streams);
338 list_add(&dentry->tmp_list,
339 &dentries_with_no_data_streams);
344 /* If there are no dentries with data streams, we require the nominal
345 * inode to be a true inode */
346 if (list_empty(&dentries_with_data_streams)) {
347 DEBUG("No data streams");
350 if (inode->link_count > 1) {
351 DEBUG("Found link group of size %zu without "
352 "any data streams:", inode->link_count);
353 print_inode_dentries(inode);
354 DEBUG("We are going to interpret it as true "
355 "link group, provided that the dentries "
360 hlist_add_head(&inode->hlist, inode_list);
361 return fix_true_inode(inode);
364 /* One or more dentries had data streams specified. We check each of
365 * these dentries for consistency with the others to form a set of true
368 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list)
370 /* Look for a true inode that is consistent with
371 * this dentry and add this dentry to it. Or, if none
372 * of the true inodes are consistent with this
373 * dentry, make a new one. */
374 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
375 if (ref_inodes_consistent(inode, dentry->inode)) {
376 list_add(&dentry->inode_dentry_list,
377 &inode->dentry_list);
382 INIT_LIST_HEAD(&dentry->inode->dentry_list);
383 list_add(&dentry->inode_dentry_list, &dentry->inode->dentry_list);
384 hlist_add_head(&dentry->inode->hlist, &true_inodes);
389 wimlib_assert(num_true_inodes != 0);
391 /* If there were dentries with no data streams, we require there to only
392 * be one true inode so that we know which inode to assign the
393 * streamless dentries to. */
394 if (!list_empty(&dentries_with_no_data_streams)) {
395 if (num_true_inodes != 1) {
396 ERROR("Hard inode ambiguity detected!");
397 ERROR("We split up inode 0x%"PRIx64" due to "
398 "inconsistencies,", inode->ino);
399 ERROR("but dentries with no stream information remained. "
400 "We don't know which true hard link");
401 ERROR("inode to assign them to.");
402 return WIMLIB_ERR_INVALID_DENTRY;
404 inode = container_of(true_inodes.first,
407 /* Assign the streamless dentries to the one and only true link
409 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
410 list_add(&dentry->inode_dentry_list, &inode->dentry_list);
412 if (num_true_inodes != 1) {
415 printf("Split nominal inode 0x%"PRIx64" into %zu "
417 inode->ino, num_true_inodes);
418 puts("------------------------------------------------------------------------------");
420 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
421 printf("[Split inode %zu]\n", i++);
422 print_inode_dentries(inode);
425 puts("------------------------------------------------------------------------------");
430 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, hlist) {
431 hlist_add_head(&inode->hlist, inode_list);
432 ret = fix_true_inode(inode);
440 * Goes through each inode and shares the inodes among members of a hard
443 * In the process, the dentries in each inode are checked for consistency.
444 * If they contain data features that indicate they cannot really be in the same
445 * inode, this should be an error, but in reality this case needs to
446 * be handled, so we split the dentries into different inodes.
448 int fix_inodes(struct inode_table *table, struct hlist_head *inode_list)
451 struct hlist_node *cur, *tmp;
453 INIT_HLIST_HEAD(inode_list);
454 for (u64 i = 0; i < table->capacity; i++) {
455 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], hlist) {
456 ret = fix_nominal_inode(inode, inode_list);
461 hlist_for_each_safe(cur, tmp, &table->extra_inodes)
462 hlist_add_head(cur, inode_list);