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_METADATA_RESOURCE;
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;
213 print_inode_dentries(const struct wim_inode *inode)
215 struct wim_dentry *dentry;
216 inode_for_each_dentry(dentry, inode)
217 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
221 inconsistent_inode(const struct wim_inode *inode)
223 if (wimlib_print_errors) {
224 ERROR("An inconsistent hard link group that cannot be corrected has "
226 ERROR("The dentries are located at the following paths:");
227 print_inode_dentries(inode);
232 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
233 const struct wim_inode * restrict ref_inode_2)
235 wimlib_assert(ref_inode_1 != ref_inode_2);
237 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
239 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
240 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
242 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
243 const u8 *ref_1_hash, *ref_2_hash;
244 ref_1_hash = inode_stream_hash(ref_inode_1, i);
245 ref_2_hash = inode_stream_hash(ref_inode_2, i);
246 if (!hashes_equal(ref_1_hash, ref_2_hash))
248 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
249 &ref_inode_2->i_ads_entries[i - 1]))
257 inodes_consistent(const struct wim_inode * restrict ref_inode,
258 const struct wim_inode * restrict inode)
260 wimlib_assert(ref_inode != inode);
262 if (ref_inode->i_num_ads != inode->i_num_ads &&
263 inode->i_num_ads != 0)
265 if (ref_inode->i_security_id != inode->i_security_id
266 || ref_inode->i_attributes != inode->i_attributes)
268 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
269 const u8 *ref_hash, *hash;
270 ref_hash = inode_stream_hash(ref_inode, i);
271 hash = inode_stream_hash(inode, i);
272 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
274 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
275 &inode->i_ads_entries[i - 1]))
281 /* Fix up a "true" inode and check for inconsistencies */
283 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
285 struct wim_dentry *dentry;
286 struct wim_dentry *ref_dentry = NULL;
287 struct wim_inode *ref_inode;
292 inode_for_each_dentry(dentry, inode) {
293 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
295 if (dentry->d_inode->i_creation_time > last_ctime)
296 last_ctime = dentry->d_inode->i_creation_time;
297 if (dentry->d_inode->i_last_write_time > last_mtime)
298 last_mtime = dentry->d_inode->i_last_write_time;
299 if (dentry->d_inode->i_last_access_time > last_atime)
300 last_atime = dentry->d_inode->i_last_access_time;
303 ref_inode = ref_dentry->d_inode;
304 wimlib_assert(ref_inode->i_nlink == 1);
305 list_add_tail(&ref_inode->i_list, inode_list);
307 list_del(&inode->i_dentry);
308 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
310 inode_for_each_dentry(dentry, ref_inode) {
311 if (dentry != ref_dentry) {
312 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
313 inconsistent_inode(ref_inode);
314 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
316 /* Free the unneeded `struct wim_inode'. */
317 wimlib_assert(dentry->d_inode->i_nlink == 1);
318 free_inode(dentry->d_inode);
319 dentry->d_inode = ref_inode;
320 ref_inode->i_nlink++;
323 ref_inode->i_creation_time = last_ctime;
324 ref_inode->i_last_write_time = last_mtime;
325 ref_inode->i_last_access_time = last_atime;
326 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
331 * Fixes up a nominal inode.
333 * By a nominal inode we mean a group of two or more dentries that share the
334 * same hard link group ID.
336 * If dentries in the inode are found to be inconsistent, we may split the inode
337 * into several "true" inodes.
339 * After splitting up each nominal inode into the "true" inodes we will
340 * canonicalize the link group by getting rid of all the unnecessary `struct
341 * wim_inode's. There will be just one `struct wim_inode' for each hard link
345 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
346 bool *ino_changes_needed)
348 struct wim_dentry *dentry;
349 struct hlist_node *cur, *tmp;
351 size_t num_true_inodes;
353 LIST_HEAD(dentries_with_data_streams);
354 LIST_HEAD(dentries_with_no_data_streams);
355 HLIST_HEAD(true_inodes);
357 /* Create a list of dentries in the nominal inode that have at
358 * least one data stream with a non-zero hash, and another list that
359 * contains the dentries that have a zero hash for all data streams. */
360 inode_for_each_dentry(dentry, inode) {
361 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
363 hash = inode_stream_hash(dentry->d_inode, i);
364 if (!is_zero_hash(hash)) {
365 list_add(&dentry->tmp_list,
366 &dentries_with_data_streams);
370 list_add(&dentry->tmp_list,
371 &dentries_with_no_data_streams);
376 /* If there are no dentries with data streams, we require the nominal
377 * inode to be a true inode */
378 if (list_empty(&dentries_with_data_streams)) {
380 unsigned nominal_group_size = inode_link_count(inode);
381 if (nominal_group_size > 1) {
382 DEBUG("Found link group of size %u without "
383 "any data streams:", nominal_group_size);
384 print_inode_dentries(inode);
385 DEBUG("We are going to interpret it as true "
386 "link group, provided that the dentries "
390 return fix_true_inode(inode, inode_list);
393 /* One or more dentries had data streams specified. We check each of
394 * these dentries for consistency with the others to form a set of true
397 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
398 /* Look for a true inode that is consistent with this dentry and
399 * add this dentry to it. Or, if none of the true inodes are
400 * consistent with this dentry, add a new one (if that happens,
401 * we have split the hard link group). */
402 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
403 if (ref_inodes_consistent(inode, dentry->d_inode)) {
404 inode_add_dentry(dentry, inode);
409 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
410 inode_add_dentry(dentry, dentry->d_inode);
411 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
416 wimlib_assert(num_true_inodes != 0);
418 /* If there were dentries with no data streams, we require there to only
419 * be one true inode so that we know which inode to assign the
420 * streamless dentries to. */
421 if (!list_empty(&dentries_with_no_data_streams)) {
422 if (num_true_inodes != 1) {
423 ERROR("Hard link ambiguity detected!");
424 ERROR("We split up inode 0x%"PRIx64" due to "
425 "inconsistencies,", inode->i_ino);
426 ERROR("but dentries with no stream information remained. "
427 "We don't know which inode");
428 ERROR("to assign them to.");
429 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
430 goto out_cleanup_true_inode_list;
432 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
433 /* Assign the streamless dentries to the one and only true
435 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
436 inode_add_dentry(dentry, inode);
438 if (num_true_inodes != 1) {
440 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
442 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
443 "inodes:\n"), inode->i_ino, num_true_inodes);
444 tputs(T("----------------------------------------------------"
445 "--------------------------"));
447 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
448 tprintf(T("[Split inode %zu]\n"), i++);
449 print_inode_dentries(inode);
452 tputs(T("----------------------------------------------------"
453 "--------------------------"));
455 *ino_changes_needed = true;
458 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
459 hlist_del_init(&inode->i_hlist);
460 ret = fix_true_inode(inode, inode_list);
462 goto out_cleanup_true_inode_list;
466 out_cleanup_true_inode_list:
467 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist)
468 hlist_del_init(&inode->i_hlist);
474 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
475 bool *ino_changes_needed)
477 struct wim_inode *inode;
478 struct hlist_node *cur, *tmp;
480 INIT_LIST_HEAD(inode_list);
481 for (u64 i = 0; i < table->capacity; i++) {
482 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
483 hlist_del_init(&inode->i_hlist);
484 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
489 list_splice_tail(&table->extra_inodes, inode_list);
494 * dentry_tree_fix_inodes():
496 * This function takes as input a tree of WIM dentries that initially has a
497 * different inode associated with each dentry. Sets of dentries that should
498 * share the same inode (a.k.a. hard link groups) are built using the i_ino
499 * field of each inode, then the link count and alias list for one inode in each
500 * set is set correctly and the unnecessary struct wim_inode's freed. The
501 * effect is to correctly associate exactly one struct wim_inode with each
502 * original inode, regardless of how many dentries are aliases for that inode.
504 * The special inode number of 0 indicates that the dentry is in a hard link
505 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
508 * This function also checks the dentries in each hard link group for
509 * consistency. In some WIMs, such as install.wim for some versions of Windows
510 * 7, dentries can share the same hard link group ID but not actually be hard
511 * linked to each other (based on conflicting information, such as file
512 * contents). This should be an error, but this case needs be handled. So,
513 * each "nominal" inode (the inode based on the inode numbers provided in the
514 * WIM) is examined for consistency and may be split into multiple "true" inodes
515 * that are maximally sized consistent sets of dentries.
517 * On success, the list of "true" inodes, linked by the i_hlist field,
518 * is returned in the hlist @inode_list.
521 * WIMLIB_ERR_SUCCESS (0)
522 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
526 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
528 struct wim_inode_table inode_tab;
530 bool ino_changes_needed;
531 struct wim_inode *inode;
533 DEBUG("Inserting dentries into inode table");
534 ret = init_inode_table(&inode_tab, 9001);
538 ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
540 goto out_destroy_inode_table;
542 DEBUG("Cleaning up the hard link groups");
543 ino_changes_needed = false;
544 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
546 goto out_destroy_inode_table;
548 if (ino_changes_needed) {
551 WARNING("Re-assigning inode numbers due to inode inconsistencies");
552 list_for_each_entry(inode, inode_list, i_list) {
553 if (inode->i_nlink > 1)
554 inode->i_ino = cur_ino++;
559 /* On success, all the inodes have been moved to the image inode list,
560 * so there's no need to delete from from the hash lists in the inode
561 * table before freeing the hash buckets array directly. */
563 goto out_destroy_inode_table_raw;
564 out_destroy_inode_table:
565 for (size_t i = 0; i < inode_tab.capacity; i++) {
566 struct hlist_node *cur, *tmp;
567 hlist_for_each_entry_safe(inode, cur, tmp, &inode_tab.array[i], i_hlist)
568 hlist_del_init(&inode->i_hlist);
571 struct wim_inode *tmp;
572 list_for_each_entry_safe(inode, tmp, &inode_tab.extra_inodes, i_list)
573 list_del_init(&inode->i_list);
575 out_destroy_inode_table_raw:
576 destroy_inode_table(&inode_tab);
581 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
582 * and append the inodes to a single list @head that contains the inodes already
583 * existing in the WIM image. */
585 inode_table_prepare_inode_list(struct wim_inode_table *table,
586 struct list_head *head)
588 struct wim_inode *inode, *tmp_inode;
589 struct hlist_node *cur, *tmp;
592 /* Re-assign inode numbers in the existing list to avoid duplicates. */
593 list_for_each_entry(inode, head, i_list)
594 inode->i_ino = cur_ino++;
596 /* Assign inode numbers to the new inodes and move them to the image's
598 for (size_t i = 0; i < table->capacity; i++) {
599 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
601 inode->i_ino = cur_ino++;
603 list_add_tail(&inode->i_list, head);
605 INIT_HLIST_HEAD(&table->array[i]);
607 list_for_each_entry_safe(inode, tmp_inode, &table->extra_inodes, i_list)
609 inode->i_ino = cur_ino++;
611 list_add_tail(&inode->i_list, head);
613 INIT_LIST_HEAD(&table->extra_inodes);
614 table->num_entries = 0;