4 * Code to deal with hard links in WIMs.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
12 * wimlib is free software; you can redistribute it and/or modify it under the
13 * terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option)
17 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 * A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with wimlib; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/capture.h"
31 #include "wimlib/dentry.h"
32 #include "wimlib/error.h"
33 #include "wimlib/lookup_table.h"
38 * / \ ----------- -----------
39 * | dentry<---| struct | | struct |---> dentry
40 * \ / | wim_inode| | wim_inode|
41 * dentry ------------ ------------
45 * ----------- ----------- / \
46 * dentry<---| struct | | struct |---> dentry dentry
47 * / | wim_inode| | wim_inode| \ /
48 * dentry ------------ ------------ dentry
52 * wim_inode_table->array | idx 0 | idx 1 |
58 init_inode_table(struct wim_inode_table *table, size_t capacity)
60 table->array = CALLOC(capacity, sizeof(table->array[0]));
62 ERROR("Cannot initalize inode table: out of memory");
63 return WIMLIB_ERR_NOMEM;
65 table->num_entries = 0;
66 table->capacity = capacity;
67 INIT_LIST_HEAD(&table->extra_inodes);
72 inode_link_count(const struct wim_inode *inode)
74 const struct list_head *cur;
76 list_for_each(cur, &inode->i_dentry)
81 /* Insert a dentry into the inode table based on the inode number of the
82 * attached inode (which came from the hard link group ID field of the on-disk
85 inode_table_insert(struct wim_dentry *dentry, void *_table)
87 struct wim_inode_table *table = _table;
88 struct wim_inode *d_inode = dentry->d_inode;
90 if (d_inode->i_ino == 0) {
91 /* A dentry with a hard link group ID of 0 indicates that it's
92 * in a hard link group by itself. Add it to the list of extra
93 * inodes rather than inserting it into the hash lists. */
94 list_add_tail(&d_inode->i_list, &table->extra_inodes);
97 struct wim_inode *inode;
98 struct hlist_node *cur;
100 /* Try adding this dentry to an existing inode */
101 pos = d_inode->i_ino % table->capacity;
102 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
103 if (inode->i_ino == d_inode->i_ino) {
104 if (unlikely((inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) ||
105 (d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)))
107 ERROR("Unsupported directory hard link "
108 "\"%"TS"\" <=> \"%"TS"\"",
109 dentry_full_path(dentry),
110 dentry_full_path(inode_first_dentry(inode)));
111 return WIMLIB_ERR_INVALID_DENTRY;
113 inode_add_dentry(dentry, inode);
118 /* No inode in the table has the same number as this one, so add
119 * it to the table. */
120 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
122 /* XXX Make the table grow when too many entries have been
124 table->num_entries++;
129 static struct wim_inode *
130 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
132 u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
133 size_t pos = hash % table->capacity;
134 struct wim_inode *inode;
135 struct hlist_node *cur;
137 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
138 if (inode->i_ino == ino && inode->i_devno == devno) {
139 DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
145 inode = new_timeless_inode();
148 inode->i_devno = devno;
149 hlist_add_head(&inode->i_hlist, &table->array[pos]);
150 table->num_entries++;
156 inode_ref_streams(struct wim_inode *inode)
158 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
159 struct wim_lookup_table_entry *lte;
160 lte = inode_stream_lte_resolved(inode, i);
166 /* Given a directory entry with the name @name for the file with the inode
167 * number @ino and device number @devno, create a new WIM dentry with an
168 * associated inode, where the inode is shared if an inode with the same @ino
169 * and @devno has already been created. On success, the new WIM dentry is
170 * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
171 * existing inode was used.
174 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
175 u64 ino, u64 devno, bool noshare,
176 struct wim_dentry **dentry_ret)
178 struct wim_dentry *dentry;
179 struct wim_inode *inode;
183 /* File that cannot be hardlinked--- Return a new inode with its
184 * inode and device numbers left at 0. */
185 ret = new_dentry_with_timeless_inode(name, &dentry);
188 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
190 /* File that can be hardlinked--- search the table for an
191 * existing inode matching the inode number and device;
192 * otherwise create a new inode. */
193 ret = new_dentry(name, &dentry);
196 inode = inode_table_get_inode(table, ino, devno);
199 return WIMLIB_ERR_NOMEM;
201 /* If using an existing inode, we need to gain a reference to
202 * each of its streams. */
203 if (inode->i_nlink > 1)
204 inode_ref_streams(inode);
205 dentry->d_inode = inode;
206 inode_add_dentry(dentry, inode);
208 *dentry_ret = dentry;
212 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
214 print_inode_dentries(const struct wim_inode *inode)
216 struct wim_dentry *dentry;
217 inode_for_each_dentry(dentry, inode)
218 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
223 inconsistent_inode(const struct wim_inode *inode)
225 #ifdef ENABLE_ERROR_MESSAGES
226 ERROR("An inconsistent hard link group that cannot be corrected has "
228 ERROR("The dentries are located at the following paths:");
229 print_inode_dentries(inode);
234 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
235 const struct wim_inode * restrict ref_inode_2)
237 wimlib_assert(ref_inode_1 != ref_inode_2);
239 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
241 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
242 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
244 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
245 const u8 *ref_1_hash, *ref_2_hash;
246 ref_1_hash = inode_stream_hash(ref_inode_1, i);
247 ref_2_hash = inode_stream_hash(ref_inode_2, i);
248 if (!hashes_equal(ref_1_hash, ref_2_hash))
250 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
251 &ref_inode_2->i_ads_entries[i - 1]))
259 inodes_consistent(const struct wim_inode * restrict ref_inode,
260 const struct wim_inode * restrict inode)
262 wimlib_assert(ref_inode != inode);
264 if (ref_inode->i_num_ads != inode->i_num_ads &&
265 inode->i_num_ads != 0)
267 if (ref_inode->i_security_id != inode->i_security_id
268 || ref_inode->i_attributes != inode->i_attributes)
270 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
271 const u8 *ref_hash, *hash;
272 ref_hash = inode_stream_hash(ref_inode, i);
273 hash = inode_stream_hash(inode, i);
274 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
276 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
277 &inode->i_ads_entries[i - 1]))
283 /* Fix up a "true" inode and check for inconsistencies */
285 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
287 struct wim_dentry *dentry;
288 struct wim_dentry *ref_dentry = NULL;
289 struct wim_inode *ref_inode;
294 inode_for_each_dentry(dentry, inode) {
295 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
297 if (dentry->d_inode->i_creation_time > last_ctime)
298 last_ctime = dentry->d_inode->i_creation_time;
299 if (dentry->d_inode->i_last_write_time > last_mtime)
300 last_mtime = dentry->d_inode->i_last_write_time;
301 if (dentry->d_inode->i_last_access_time > last_atime)
302 last_atime = dentry->d_inode->i_last_access_time;
305 ref_inode = ref_dentry->d_inode;
306 wimlib_assert(ref_inode->i_nlink == 1);
307 list_add_tail(&ref_inode->i_list, inode_list);
309 list_del(&inode->i_dentry);
310 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
312 inode_for_each_dentry(dentry, ref_inode) {
313 if (dentry != ref_dentry) {
314 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
315 inconsistent_inode(ref_inode);
316 return WIMLIB_ERR_INVALID_DENTRY;
318 /* Free the unneeded `struct wim_inode'. */
319 wimlib_assert(dentry->d_inode->i_nlink == 1);
320 free_inode(dentry->d_inode);
321 dentry->d_inode = ref_inode;
322 ref_inode->i_nlink++;
325 ref_inode->i_creation_time = last_ctime;
326 ref_inode->i_last_write_time = last_mtime;
327 ref_inode->i_last_access_time = last_atime;
328 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
333 * Fixes up a nominal inode.
335 * By a nominal inode we mean a group of two or more dentries that share the
336 * same hard link group ID.
338 * If dentries in the inode are found to be inconsistent, we may split the inode
339 * into several "true" inodes.
341 * After splitting up each nominal inode into the "true" inodes we will
342 * canonicalize the link group by getting rid of all the unnecessary `struct
343 * wim_inode's. There will be just one `struct wim_inode' for each hard link
347 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
348 bool *ino_changes_needed)
350 struct wim_dentry *dentry;
351 struct hlist_node *cur, *tmp;
353 size_t num_true_inodes;
354 unsigned nominal_group_size = inode_link_count(inode);
356 LIST_HEAD(dentries_with_data_streams);
357 LIST_HEAD(dentries_with_no_data_streams);
358 HLIST_HEAD(true_inodes);
360 /* Create a list of dentries in the nominal inode that have at
361 * least one data stream with a non-zero hash, and another list that
362 * contains the dentries that have a zero hash for all data streams. */
363 inode_for_each_dentry(dentry, inode) {
364 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
366 hash = inode_stream_hash(dentry->d_inode, i);
367 if (!is_zero_hash(hash)) {
368 list_add(&dentry->tmp_list,
369 &dentries_with_data_streams);
373 list_add(&dentry->tmp_list,
374 &dentries_with_no_data_streams);
379 /* If there are no dentries with data streams, we require the nominal
380 * inode to be a true inode */
381 if (list_empty(&dentries_with_data_streams)) {
383 if (nominal_group_size > 1) {
384 DEBUG("Found link group of size %u without "
385 "any data streams:", nominal_group_size);
386 print_inode_dentries(inode);
387 DEBUG("We are going to interpret it as true "
388 "link group, provided that the dentries "
392 return fix_true_inode(inode, inode_list);
395 /* One or more dentries had data streams specified. We check each of
396 * these dentries for consistency with the others to form a set of true
399 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
400 /* Look for a true inode that is consistent with this dentry and
401 * add this dentry to it. Or, if none of the true inodes are
402 * consistent with this dentry, add a new one (if that happens,
403 * we have split the hard link group). */
404 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
405 if (ref_inodes_consistent(inode, dentry->d_inode)) {
406 inode_add_dentry(dentry, inode);
411 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
412 inode_add_dentry(dentry, dentry->d_inode);
413 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
418 wimlib_assert(num_true_inodes != 0);
420 /* If there were dentries with no data streams, we require there to only
421 * be one true inode so that we know which inode to assign the
422 * streamless dentries to. */
423 if (!list_empty(&dentries_with_no_data_streams)) {
424 if (num_true_inodes != 1) {
425 ERROR("Hard inode ambiguity detected!");
426 ERROR("We split up inode 0x%"PRIx64" due to "
427 "inconsistencies,", inode->i_ino);
428 ERROR("but dentries with no stream information remained. "
429 "We don't know which inode");
430 ERROR("to assign them to.");
431 ret = WIMLIB_ERR_INVALID_DENTRY;
432 goto out_cleanup_true_inode_list;
434 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
435 /* Assign the streamless dentries to the one and only true
437 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
438 inode_add_dentry(dentry, inode);
440 if (num_true_inodes != 1) {
442 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
444 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
445 "inodes:\n"), inode->i_ino, num_true_inodes);
446 tputs(T("----------------------------------------------------"
447 "--------------------------"));
449 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
450 tprintf(T("[Split inode %zu]\n"), i++);
451 print_inode_dentries(inode);
454 tputs(T("----------------------------------------------------"
455 "--------------------------"));
457 *ino_changes_needed = true;
460 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
461 hlist_del_init(&inode->i_hlist);
462 ret = fix_true_inode(inode, inode_list);
464 goto out_cleanup_true_inode_list;
468 out_cleanup_true_inode_list:
469 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist)
470 hlist_del_init(&inode->i_hlist);
476 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
477 bool *ino_changes_needed)
479 struct wim_inode *inode;
480 struct hlist_node *cur, *tmp;
482 INIT_LIST_HEAD(inode_list);
483 for (u64 i = 0; i < table->capacity; i++) {
484 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
485 hlist_del_init(&inode->i_hlist);
486 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
491 list_splice_tail(&table->extra_inodes, inode_list);
496 * dentry_tree_fix_inodes():
498 * This function takes as input a tree of WIM dentries that initially has a
499 * different inode associated with each dentry. Sets of dentries that should
500 * share the same inode (a.k.a. hard link groups) are built using the i_ino
501 * field of each inode, then the link count and alias list for one inode in each
502 * set is set correctly and the unnecessary struct wim_inode's freed. The
503 * effect is to correctly associate exactly one struct wim_inode with each
504 * original inode, regardless of how many dentries are aliases for that inode.
506 * The special inode number of 0 indicates that the dentry is in a hard link
507 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
510 * This function also checks the dentries in each hard link group for
511 * consistency. In some WIMs, such as install.wim for some versions of Windows
512 * 7, dentries can share the same hard link group ID but not actually be hard
513 * linked to each other (based on conflicting information, such as file
514 * contents). This should be an error, but this case needs be handled. So,
515 * each "nominal" inode (the inode based on the inode numbers provided in the
516 * WIM) is examined for consistency and may be split into multiple "true" inodes
517 * that are maximally sized consistent sets of dentries.
519 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
520 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
521 * is returned in the hlist @inode_list.
524 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
526 struct wim_inode_table inode_tab;
528 bool ino_changes_needed;
529 struct wim_inode *inode;
531 DEBUG("Inserting dentries into inode table");
532 ret = init_inode_table(&inode_tab, 9001);
536 ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
538 goto out_destroy_inode_table;
540 DEBUG("Cleaning up the hard link groups");
541 ino_changes_needed = false;
542 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
544 goto out_destroy_inode_table;
546 if (ino_changes_needed) {
549 WARNING("Re-assigning inode numbers due to inode inconsistencies");
550 list_for_each_entry(inode, inode_list, i_list) {
551 if (inode->i_nlink > 1)
552 inode->i_ino = cur_ino++;
557 /* On success, all the inodes have been moved to the image inode list,
558 * so there's no need to delete from from the hash lists in the inode
559 * table before freeing the hash buckets array directly. */
561 goto out_destroy_inode_table_raw;
562 out_destroy_inode_table:
563 for (size_t i = 0; i < inode_tab.capacity; i++) {
564 struct hlist_node *cur, *tmp;
565 hlist_for_each_entry_safe(inode, cur, tmp, &inode_tab.array[i], i_hlist)
566 hlist_del_init(&inode->i_hlist);
569 struct wim_inode *tmp;
570 list_for_each_entry_safe(inode, tmp, &inode_tab.extra_inodes, i_list)
571 list_del_init(&inode->i_list);
573 out_destroy_inode_table_raw:
574 destroy_inode_table(&inode_tab);
579 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
580 * and append the inodes to a single list @head that contains the inodes already
581 * existing in the WIM image. */
583 inode_table_prepare_inode_list(struct wim_inode_table *table,
584 struct list_head *head)
586 struct wim_inode *inode;
587 struct hlist_node *cur, *tmp;
590 /* Re-assign inode numbers in the existing list to avoid duplicates. */
591 list_for_each_entry(inode, head, i_list) {
592 if (inode->i_nlink > 1)
593 inode->i_ino = cur_ino++;
598 /* Assign inode numbers to the new inodes and move them to the image's
600 for (size_t i = 0; i < table->capacity; i++) {
601 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
603 if (inode->i_nlink > 1)
604 inode->i_ino = cur_ino++;
607 list_add_tail(&inode->i_list, head);
609 INIT_HLIST_HEAD(&table->array[i]);
611 list_splice_tail(&table->extra_inodes, head);
612 table->num_entries = 0;