4 * Code to deal with hard links in WIMs. Essentially, the WIM dentries are put
5 * into a hash table indexed by the hard link group ID field, then for each hard
6 * link group, 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 Lesser General Public License as published by the Free
16 * Software Foundation; either version 2.1 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 Lesser General Public License for more
24 * You should have received a copy of the GNU Lesser 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
38 * \ / |link_group| |link_group|
39 * dentry ------------ ------------
43 * ----------- ----------- / \
44 * dentry<---| struct | | struct |---> dentry dentry
45 * / |link_group| |link_group| \ /
46 * dentry ------------ ------------ dentry
50 * link_group_table->array | idx 0 | idx 1 |
54 /* Hard link group; it's identified by its hard link group ID and points to a
55 * circularly linked list of dentries. */
59 /* Pointer to use to make a singly-linked list of link groups. */
60 struct link_group *next;
62 /* This is a pointer to the circle and not part of the circle itself.
63 * This makes it easy to iterate through other dentries hard-linked to a
64 * given dentry without having to find the "head" of the list first. */
65 struct list_head *dentry_list;
68 /* Hash table to find hard link groups, identified by their hard link group ID.
70 struct link_group_table {
71 /* Fields for the hash table */
72 struct link_group **array;
77 * Linked list of "extra" groups. These may be:
79 * - Hard link groups of size 1, which are all allowed to have 0 for
80 * their hard link group ID, meaning we cannot insert them into the
81 * hash table before calling assign_link_group_ids().
83 * - Groups we create ourselves by splitting a nominal hard link group
84 * due to inconsistencies in the dentries. These groups will share a
85 * hard link group ID with some other group until
86 * assign_link_group_ids() is called.
88 struct link_group *extra_groups;
91 /* Returns pointer to a new link group table having the specified capacity */
92 struct link_group_table *new_link_group_table(size_t capacity)
94 struct link_group_table *table;
95 struct link_group **array;
97 table = MALLOC(sizeof(struct link_group_table));
100 array = CALLOC(capacity, sizeof(array[0]));
105 table->num_entries = 0;
106 table->capacity = capacity;
107 table->array = array;
108 table->extra_groups = NULL;
111 ERROR("Failed to allocate memory for link group table with capacity %zu",
117 * Insert a dentry into the hard link group table based on its hard link group
120 * If there is already a dentry in the table having the same hard link group ID,
121 * and the hard link group ID is not 0, the dentry is added to the circular
122 * linked list for that hard link group.
124 * If the hard link group ID is 0, this indicates a dentry that's in a hard link
125 * group by itself (has a link count of 1). We can't insert it into the hash
126 * table itself because we don't know what hard link group IDs are available to
127 * give it (this could be kept track of but would be more difficult). Instead
128 * we keep a linked list of the single dentries, and assign them hard link group
131 int link_group_table_insert(struct dentry *dentry, void *__table)
133 struct link_group_table *table = __table;
135 struct link_group *group;
137 if (dentry->link_group_id == 0) {
138 /* Single group--- Add to the list of extra groups (we can't put
139 * it in the table itself because all the singles have a link
141 group = MALLOC(sizeof(struct link_group));
143 return WIMLIB_ERR_NOMEM;
144 group->link_group_id = 0;
145 group->next = table->extra_groups;
146 table->extra_groups = group;
147 INIT_LIST_HEAD(&dentry->link_group_list);
148 group->dentry_list = &dentry->link_group_list;
150 /* Hard link group that may contain multiple dentries (the code
151 * will work even if the group actually contains only 1 dentry
154 /* Try adding to existing hard link group */
155 pos = dentry->link_group_id % table->capacity;
156 group = table->array[pos];
158 if (group->link_group_id == dentry->link_group_id) {
159 list_add(&dentry->link_group_list,
166 /* Add new hard link group to the table */
168 group = MALLOC(sizeof(struct link_group));
170 return WIMLIB_ERR_NOMEM;
171 group->link_group_id = dentry->link_group_id;
172 group->next = table->array[pos];
173 INIT_LIST_HEAD(&dentry->link_group_list);
174 group->dentry_list = &dentry->link_group_list;
175 table->array[pos] = group;
177 /* XXX Make the table grow when too many entries have been
179 table->num_entries++;
184 static void free_link_group_list(struct link_group *group)
186 struct link_group *next_group;
188 next_group = group->next;
194 /* Frees a link group table. */
195 void free_link_group_table(struct link_group_table *table)
197 struct link_group *single, *next;
201 for (size_t i = 0; i < table->capacity; i++)
202 free_link_group_list(table->array[i]);
203 free_link_group_list(table->extra_groups);
208 u64 assign_link_group_ids_to_list(struct link_group *group, u64 id,
209 struct link_group **extra_groups)
211 struct dentry *dentry;
212 struct list_head *cur_head;
213 struct link_group *prev_group = NULL;
214 struct link_group *cur_group = group;
216 cur_head = cur_group->dentry_list;
218 dentry = container_of(cur_head,
221 dentry->link_group_id = id;
222 cur_head = cur_head->next;
223 } while (cur_head != cur_group->dentry_list);
224 cur_group->link_group_id = id;
226 prev_group = cur_group;
227 cur_group = cur_group->next;
229 if (group && extra_groups) {
230 prev_group->next = *extra_groups;
231 *extra_groups = group;
236 /* Insert the link groups in the `extra_groups' list into the hash table */
237 static void insert_extra_groups(struct link_group_table *table)
239 struct link_group *group, *next_group;
242 group = table->extra_groups;
244 next_group = group->next;
245 pos = group->link_group_id % table->capacity;
246 group->next = table->array[pos];
247 table->array[pos] = group;
250 table->extra_groups = NULL;
253 /* Assign the link group IDs to dentries in a link group table, and return the
254 * next available link group ID. */
255 u64 assign_link_group_ids(struct link_group_table *table)
257 DEBUG("Assigning link groups");
258 struct link_group *extra_groups = table->extra_groups;
260 /* Assign consecutive link group IDs to each link group in the hash
263 for (size_t i = 0; i < table->capacity; i++) {
264 id = assign_link_group_ids_to_list(table->array[i], id,
265 &table->extra_groups);
266 table->array[i] = NULL;
269 /* Assign link group IDs to the "extra" link groups and insert them into
271 id = assign_link_group_ids_to_list(extra_groups, id, NULL);
272 insert_extra_groups(table);
278 static void inconsistent_link_group(const struct dentry *first_dentry)
280 const struct dentry *dentry = first_dentry;
282 ERROR("An inconsistent hard link group that we cannot correct has been "
284 ERROR("The dentries are located at the following paths:");
286 ERROR("`%s'", dentry->full_path_utf8);
287 } while ((dentry = container_of(dentry->link_group_list.next,
289 link_group_list)) != first_dentry);
292 static bool ref_dentries_consistent(const struct dentry * restrict ref_dentry_1,
293 const struct dentry * restrict ref_dentry_2)
295 wimlib_assert(ref_dentry_1 != ref_dentry_2);
297 if (ref_dentry_1->num_ads != ref_dentry_2->num_ads)
299 if (ref_dentry_1->security_id != ref_dentry_2->security_id
300 || ref_dentry_1->attributes != ref_dentry_2->attributes)
302 for (unsigned i = 0; i <= ref_dentry_1->num_ads; i++) {
303 const u8 *ref_1_hash, *ref_2_hash;
304 ref_1_hash = dentry_stream_hash(ref_dentry_1, i);
305 ref_2_hash = dentry_stream_hash(ref_dentry_2, i);
306 if (!hashes_equal(ref_1_hash, ref_2_hash))
308 if (i && !ads_entries_have_same_name(&ref_dentry_1->ads_entries[i - 1],
309 &ref_dentry_2->ads_entries[i - 1]))
316 static bool dentries_consistent(const struct dentry * restrict ref_dentry,
317 const struct dentry * restrict dentry)
319 wimlib_assert(ref_dentry != dentry);
321 if (ref_dentry->num_ads != dentry->num_ads && dentry->num_ads != 0)
323 if (ref_dentry->security_id != dentry->security_id
324 || ref_dentry->attributes != dentry->attributes)
326 for (unsigned i = 0; i <= min(ref_dentry->num_ads, dentry->num_ads); i++) {
327 const u8 *ref_hash, *hash;
328 ref_hash = dentry_stream_hash(ref_dentry, i);
329 hash = dentry_stream_hash(dentry, i);
330 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
332 if (i && !ads_entries_have_same_name(&ref_dentry->ads_entries[i - 1],
333 &dentry->ads_entries[i - 1]))
341 print_dentry_list(const struct dentry *first_dentry)
343 const struct dentry *dentry = first_dentry;
345 printf("`%s'\n", dentry->full_path_utf8);
346 } while ((dentry = container_of(dentry->link_group_list.next,
348 link_group_list)) != first_dentry);
352 /* Fix up a "true" link group and check for inconsistencies */
354 fix_true_link_group(struct dentry *first_dentry)
356 struct dentry *dentry;
357 struct dentry *ref_dentry = NULL;
362 dentry = first_dentry;
364 if (!ref_dentry || ref_dentry->num_ads == 0)
366 if (dentry->creation_time > last_ctime)
367 last_ctime = dentry->creation_time;
368 if (dentry->last_write_time > last_mtime)
369 last_mtime = dentry->last_write_time;
370 if (dentry->last_access_time > last_atime)
371 last_atime = dentry->last_access_time;
372 } while ((dentry = container_of(dentry->link_group_list.next,
374 link_group_list)) != first_dentry);
377 ref_dentry->ads_entries_status = ADS_ENTRIES_OWNER;
378 dentry = first_dentry;
380 if (dentry != ref_dentry) {
381 if (!dentries_consistent(ref_dentry, dentry)) {
382 inconsistent_link_group(first_dentry);
383 return WIMLIB_ERR_INVALID_DENTRY;
385 copy_hash(dentry->hash, ref_dentry->hash);
386 dentry_free_ads_entries(dentry);
387 dentry->num_ads = ref_dentry->num_ads;
388 dentry->ads_entries = ref_dentry->ads_entries;
389 dentry->ads_entries_status = ADS_ENTRIES_USER;
391 dentry->creation_time = last_ctime;
392 dentry->last_write_time = last_mtime;
393 dentry->last_access_time = last_atime;
394 } while ((dentry = container_of(dentry->link_group_list.next,
396 link_group_list)) != first_dentry);
401 * Fixes up a nominal link group.
403 * By a nominal link group we mean a group of two or more dentries that share
404 * the same hard link group ID.
406 * If dentries in the group are found to be inconsistent, we may split the group
407 * into several "true" hard link groups. @new_groups points to a linked list of
408 * these split groups, and if we create any, they will be added to this list.
410 * After splitting up each nominal link group into the "true" link groups we
411 * will canonicalize the link groups. To do this, we:
413 * - Assign all the dentries in the link group the most recent timestamp
414 * among all the corresponding timestamps in the link group, for each of
415 * the three categories of time stamps.
417 * - Make sure the dentry->hash field is valid in all the dentries, if
418 * possible (this field may be all zeroes, and in the context of a hard
419 * link group this must be interpreted as implicitly refering to the same
420 * stream as another dentry in the hard link group that does NOT have all
421 * zeroes for this field).
423 * - Make sure dentry->num_ads is the same in all the dentries in the link
424 * group. In some cases, it's possible for it to be set to 0 when it
425 * actually must be interpreted as being the same as the number of
426 * alternate data streams in another dentry in the hard link group that has
427 * a nonzero number of alternate data streams.
429 * - Make sure only the dentry->ads_entries array is only allocated for one
430 * dentry in the hard link group. This dentry will have
431 * dentry->ads_entries_status set to ADS_ENTRIES_OWNER, while the others
432 * will have dentry->ads_entries_status set to ADS_ENTRIES_USER.
435 fix_nominal_link_group(struct link_group *group,
436 struct link_group **new_groups)
438 struct dentry *tmp, *dentry, *ref_dentry;
440 size_t num_true_link_groups;
441 struct list_head *head;
444 LIST_HEAD(dentries_with_data_streams);
445 LIST_HEAD(dentries_with_no_data_streams);
446 LIST_HEAD(true_link_groups);
448 /* Create a list of dentries in the nominal hard link group that have at
449 * least one data stream with a non-zero hash, and another list that
450 * contains the dentries that have a zero hash for all data streams. */
451 dentry = container_of(group->dentry_list, struct dentry,
454 for (unsigned i = 0; i <= dentry->num_ads; i++) {
456 hash = dentry_stream_hash(dentry, i);
457 if (!is_zero_hash(hash)) {
458 list_add(&dentry->tmp_list,
459 &dentries_with_data_streams);
463 list_add(&dentry->tmp_list,
464 &dentries_with_no_data_streams);
466 dentry = container_of(dentry->link_group_list.next,
469 } while (&dentry->link_group_list != group->dentry_list);
471 /* If there are no dentries with data streams, we require the nominal
472 * link group to be a true link group */
473 if (list_empty(&dentries_with_data_streams)) {
475 DEBUG("Found link group of size %zu without any data streams:",
476 dentry_link_group_size(dentry));
477 print_dentry_list(dentry);
478 DEBUG("We are going to interpret it as true link group, provided "
479 "that the dentries are consistent.");
481 return fix_true_link_group(container_of(group->dentry_list,
486 /* One or more dentries had data streams specified. We check each of
487 * these dentries for consistency with the others to form a set of true
489 num_true_link_groups = 0;
490 list_for_each_entry_safe(dentry, tmp, &dentries_with_data_streams,
493 list_del(&dentry->tmp_list);
495 /* Look for a true link group that is consistent with
496 * this dentry and add this dentry to it. Or, if none
497 * of the true link groups are consistent with this
498 * dentry, make a new one. */
499 list_for_each_entry(ref_dentry, &true_link_groups, tmp_list) {
500 if (ref_dentries_consistent(ref_dentry, dentry)) {
501 list_add(&dentry->link_group_list,
502 &ref_dentry->link_group_list);
506 num_true_link_groups++;
507 list_add(&dentry->tmp_list, &true_link_groups);
508 INIT_LIST_HEAD(&dentry->link_group_list);
513 wimlib_assert(num_true_link_groups != 0);
515 /* If there were dentries with no data streams, we require there to only
516 * be one true link group so that we know which link group to assign the
517 * streamless dentries to. */
518 if (!list_empty(&dentries_with_no_data_streams)) {
519 if (num_true_link_groups != 1) {
520 ERROR("Hard link group ambiguity detected!");
521 ERROR("We split up hard link group 0x%"PRIx64" due to "
522 "inconsistencies,", group->link_group_id);
523 ERROR("but dentries with no stream information remained. "
524 "We don't know which true hard link");
525 ERROR("group to assign them to.");
526 return WIMLIB_ERR_INVALID_DENTRY;
528 /* Assign the streamless dentries to the one and only true link
530 ref_dentry = container_of(true_link_groups.next,
533 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
534 list_add(&dentry->link_group_list, &ref_dentry->link_group_list);
536 if (num_true_link_groups != 1) {
539 printf("Split nominal link group 0x%"PRIx64" into %zu "
541 group->link_group_id, num_true_link_groups);
542 puts("------------------------------------------------------------------------------");
544 list_for_each_entry(dentry, &true_link_groups, tmp_list) {
545 printf("[Split link group %zu]\n", i++);
546 print_dentry_list(dentry);
549 puts("------------------------------------------------------------------------------");
554 list_for_each_entry(dentry, &true_link_groups, tmp_list) {
555 ret = fix_true_link_group(dentry);
560 /* Make new `struct link_group's for the new link groups */
561 for (head = true_link_groups.next->next;
562 head != &true_link_groups;
565 dentry = container_of(head, struct dentry, tmp_list);
566 group = MALLOC(sizeof(*group));
568 ERROR("Out of memory");
569 return WIMLIB_ERR_NOMEM;
571 group->link_group_id = link_group_id;
572 group->dentry_list = &dentry->link_group_list;
573 group->next = *new_groups;
580 * Goes through each link group and shares the ads_entries (Alternate Data
581 * Stream entries) field of each dentry among members of a hard link group.
583 * In the process, the dentries in each link group are checked for consistency.
584 * If they contain data features that indicate they cannot really be in the same
585 * hard link group, this should be an error, but in reality this case needs to
586 * be handled, so we split the dentries into different hard link groups.
588 * One of the dentries in each hard link group group is arbitrarily assigned the
589 * role of "owner" of the memory pointed to by the @ads_entries field,
590 * (ADS_ENTRIES_OWNER), while the others are "users" (ADS_ENTRIES_USER) who are
591 * not allowed to free the memory.
593 int fix_link_groups(struct link_group_table *table)
595 for (u64 i = 0; i < table->capacity; i++) {
596 struct link_group *group = table->array[i];
599 ret = fix_nominal_link_group(group, &table->extra_groups);