4 * See dentry_tree_fix_inodes() for description.
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/dentry.h"
31 #include "wimlib/error.h"
32 #include "wimlib/inode.h"
33 #include "wimlib/inode_table.h"
34 #include "wimlib/lookup_table.h"
36 /* Manual link count of inode (normally we can just check i_nlink) */
38 inode_link_count(const struct wim_inode *inode)
40 const struct list_head *cur;
42 list_for_each(cur, &inode->i_dentry)
48 print_inode_dentries(const struct wim_inode *inode)
50 struct wim_dentry *dentry;
51 inode_for_each_dentry(dentry, inode)
52 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
56 inconsistent_inode(const struct wim_inode *inode)
58 if (wimlib_print_errors) {
59 WARNING("An inconsistent hard link group that cannot be corrected has "
61 WARNING("The dentries are located at the following paths:");
62 print_inode_dentries(inode);
67 ads_entries_have_same_name(const struct wim_ads_entry *entry_1,
68 const struct wim_ads_entry *entry_2)
70 return entry_1->stream_name_nbytes == entry_2->stream_name_nbytes &&
71 memcmp(entry_1->stream_name, entry_2->stream_name,
72 entry_1->stream_name_nbytes) == 0;
76 ref_inodes_consistent(const struct wim_inode *ref_inode_1,
77 const struct wim_inode *ref_inode_2)
79 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
81 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
82 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
84 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
85 const u8 *ref_1_hash, *ref_2_hash;
86 ref_1_hash = inode_stream_hash(ref_inode_1, i);
87 ref_2_hash = inode_stream_hash(ref_inode_2, i);
88 if (!hashes_equal(ref_1_hash, ref_2_hash))
90 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
91 &ref_inode_2->i_ads_entries[i - 1]))
98 /* Returns true iff the specified inode has any data streams with nonzero hash.
101 inode_has_data_streams(const struct wim_inode *inode)
103 for (unsigned i = 0; i <= inode->i_num_ads; i++)
104 if (!is_zero_hash(inode_stream_hash(inode, i)))
109 /* Returns true iff the specified dentry has any data streams with nonzero hash.
112 dentry_has_data_streams(const struct wim_dentry *dentry)
114 return inode_has_data_streams(dentry->d_inode);
118 inodes_consistent(const struct wim_inode *ref_inode,
119 const struct wim_inode *inode)
121 if (ref_inode->i_security_id != inode->i_security_id) {
122 WARNING("Security ID mismatch: %d != %d",
123 ref_inode->i_security_id, inode->i_security_id);
127 if (ref_inode->i_attributes != inode->i_attributes) {
128 WARNING("Attributes mismatch: 0x%08x != 0x%08x",
129 ref_inode->i_attributes, inode->i_attributes);
133 if (inode_has_data_streams(inode)) {
134 if (ref_inode->i_num_ads != inode->i_num_ads) {
135 WARNING("Stream count mismatch: %u != %u",
136 ref_inode->i_num_ads, inode->i_num_ads);
139 for (unsigned i = 0; i <= ref_inode->i_num_ads; i++) {
140 const u8 *ref_hash, *hash;
142 ref_hash = inode_stream_hash(ref_inode, i);
143 hash = inode_stream_hash(inode, i);
144 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash)) {
145 WARNING("Stream hash mismatch");
148 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
149 &inode->i_ads_entries[i - 1]))
151 WARNING("Stream name mismatch");
159 /* Fix up a "true" inode and check for inconsistencies */
161 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
163 struct wim_dentry *dentry;
164 struct wim_dentry *ref_dentry = NULL;
165 struct wim_inode *ref_inode;
170 inode_for_each_dentry(dentry, inode) {
171 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
173 if (dentry->d_inode->i_creation_time > last_ctime)
174 last_ctime = dentry->d_inode->i_creation_time;
175 if (dentry->d_inode->i_last_write_time > last_mtime)
176 last_mtime = dentry->d_inode->i_last_write_time;
177 if (dentry->d_inode->i_last_access_time > last_atime)
178 last_atime = dentry->d_inode->i_last_access_time;
181 ref_inode = ref_dentry->d_inode;
182 wimlib_assert(ref_inode->i_nlink == 1);
183 list_add_tail(&ref_inode->i_list, inode_list);
185 list_del(&inode->i_dentry);
186 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
188 inode_for_each_dentry(dentry, ref_inode) {
189 if (dentry != ref_dentry) {
190 if (!inodes_consistent(ref_inode, dentry->d_inode))
191 inconsistent_inode(ref_inode);
192 /* Free the unneeded `struct wim_inode'. */
193 wimlib_assert(dentry->d_inode->i_nlink == 1);
194 free_inode(dentry->d_inode);
195 dentry->d_inode = ref_inode;
196 ref_inode->i_nlink++;
199 ref_inode->i_creation_time = last_ctime;
200 ref_inode->i_last_write_time = last_mtime;
201 ref_inode->i_last_access_time = last_atime;
202 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
207 * Fixes up a nominal inode.
209 * By a nominal inode we mean a group of two or more dentries that share the
210 * same hard link group ID.
212 * If dentries in the inode are found to be inconsistent, we may split the inode
213 * into several "true" inodes.
215 * After splitting up each nominal inode into the "true" inodes we will
216 * canonicalize the link group by getting rid of all the unnecessary `struct
217 * wim_inode's. There will be just one `struct wim_inode' for each hard link
221 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
222 bool *ino_changes_needed)
224 struct wim_dentry *dentry;
225 struct hlist_node *cur, *tmp;
227 size_t num_true_inodes;
229 LIST_HEAD(dentries_with_data_streams);
230 LIST_HEAD(dentries_with_no_data_streams);
231 HLIST_HEAD(true_inodes);
233 /* Create a list of dentries in the nominal inode that have at
234 * least one data stream with a non-zero hash, and another list that
235 * contains the dentries that have a zero hash for all data streams. */
236 inode_for_each_dentry(dentry, inode) {
237 if (dentry_has_data_streams(dentry))
238 list_add(&dentry->tmp_list, &dentries_with_data_streams);
240 list_add(&dentry->tmp_list, &dentries_with_no_data_streams);
243 /* If there are no dentries with data streams, we require the nominal
244 * inode to be a true inode */
245 if (list_empty(&dentries_with_data_streams)) {
247 unsigned nominal_group_size = inode_link_count(inode);
248 if (nominal_group_size > 1) {
249 DEBUG("Found link group of size %u without "
250 "any data streams:", nominal_group_size);
251 print_inode_dentries(inode);
252 DEBUG("We are going to interpret it as true "
253 "link group, provided that the dentries "
257 return fix_true_inode(inode, inode_list);
260 /* One or more dentries had data streams specified. We check each of
261 * these dentries for consistency with the others to form a set of true
264 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
265 /* Look for a true inode that is consistent with this dentry and
266 * add this dentry to it. Or, if none of the true inodes are
267 * consistent with this dentry, add a new one (if that happens,
268 * we have split the hard link group). */
269 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
270 if (ref_inodes_consistent(inode, dentry->d_inode)) {
271 inode_add_dentry(dentry, inode);
276 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
277 inode_add_dentry(dentry, dentry->d_inode);
278 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
283 wimlib_assert(num_true_inodes != 0);
285 /* If there were dentries with no data streams, we require there to only
286 * be one true inode so that we know which inode to assign the
287 * streamless dentries to. */
288 if (!list_empty(&dentries_with_no_data_streams)) {
289 if (num_true_inodes != 1) {
290 ERROR("Hard link ambiguity detected!");
291 ERROR("We split up inode 0x%"PRIx64" due to "
292 "inconsistencies,", inode->i_ino);
293 ERROR("but dentries with no stream information remained. "
294 "We don't know which inode");
295 ERROR("to assign them to.");
296 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
297 goto out_cleanup_true_inode_list;
299 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
300 /* Assign the streamless dentries to the one and only true
302 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
303 inode_add_dentry(dentry, inode);
305 if (num_true_inodes != 1) {
307 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
309 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
310 "inodes:\n"), inode->i_ino, num_true_inodes);
311 tputs(T("----------------------------------------------------"
312 "--------------------------"));
314 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
315 tprintf(T("[Split inode %zu]\n"), i++);
316 print_inode_dentries(inode);
319 tputs(T("----------------------------------------------------"
320 "--------------------------"));
322 *ino_changes_needed = true;
325 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
326 hlist_del_init(&inode->i_hlist);
327 ret = fix_true_inode(inode, inode_list);
329 goto out_cleanup_true_inode_list;
333 out_cleanup_true_inode_list:
334 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist)
335 hlist_del_init(&inode->i_hlist);
341 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
342 bool *ino_changes_needed)
344 struct wim_inode *inode;
345 struct hlist_node *cur, *tmp;
347 INIT_LIST_HEAD(inode_list);
348 for (size_t i = 0; i < table->capacity; i++) {
349 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
350 hlist_del_init(&inode->i_hlist);
351 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
356 list_splice_tail(&table->extra_inodes, inode_list);
360 /* Insert a dentry into the inode table based on the inode number of the
361 * attached inode (which came from the hard link group ID field of the on-disk
364 inode_table_insert(struct wim_dentry *dentry, void *_table)
366 struct wim_inode_table *table = _table;
367 struct wim_inode *d_inode = dentry->d_inode;
369 if (d_inode->i_ino == 0) {
370 /* A dentry with a hard link group ID of 0 indicates that it's
371 * in a hard link group by itself. Add it to the list of extra
372 * inodes rather than inserting it into the hash lists. */
373 list_add_tail(&d_inode->i_list, &table->extra_inodes);
376 struct wim_inode *inode;
377 struct hlist_node *cur;
379 /* Try adding this dentry to an existing inode */
380 pos = d_inode->i_ino % table->capacity;
381 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
382 if (inode->i_ino == d_inode->i_ino) {
383 if (unlikely((inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) ||
384 (d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)))
386 ERROR("Unsupported directory hard link "
387 "\"%"TS"\" <=> \"%"TS"\"",
388 dentry_full_path(dentry),
389 dentry_full_path(inode_first_dentry(inode)));
390 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
392 inode_add_dentry(dentry, inode);
397 /* No inode in the table has the same number as this one, so add
398 * it to the table. */
399 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
401 /* XXX Make the table grow when too many entries have been
403 table->num_entries++;
410 * dentry_tree_fix_inodes():
412 * This function takes as input a tree of WIM dentries that initially has a
413 * different inode associated with each dentry. Sets of dentries that should
414 * share the same inode (a.k.a. hard link groups) are built using the i_ino
415 * field of each inode, then the link count and alias list for one inode in each
416 * set is set correctly and the unnecessary struct wim_inode's freed. The
417 * effect is to correctly associate exactly one struct wim_inode with each
418 * original inode, regardless of how many dentries are aliases for that inode.
420 * The special inode number of 0 indicates that the dentry is in a hard link
421 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
424 * This function also checks the dentries in each hard link group for
425 * consistency. In some WIMs, such as install.wim for some versions of Windows
426 * 7, dentries can share the same hard link group ID but not actually be hard
427 * linked to each other (based on conflicting information, such as file
428 * contents). This should be an error, but this case needs be handled. So,
429 * each "nominal" inode (the inode based on the inode numbers provided in the
430 * WIM) is examined for consistency and may be split into multiple "true" inodes
431 * that are maximally sized consistent sets of dentries.
433 * On success, the list of "true" inodes, linked by the i_list field,
434 * is returned in the list @inode_list.
437 * WIMLIB_ERR_SUCCESS (0)
438 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
442 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
444 struct wim_inode_table inode_tab;
446 bool ino_changes_needed;
447 struct wim_inode *inode;
449 DEBUG("Inserting dentries into inode table");
450 ret = init_inode_table(&inode_tab, 9001);
454 ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
456 goto out_destroy_inode_table;
458 DEBUG("Cleaning up the hard link groups");
459 ino_changes_needed = false;
460 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
462 goto out_destroy_inode_table;
464 if (ino_changes_needed) {
467 WARNING("The WIM image contains invalid hard links. Fixing.");
469 list_for_each_entry(inode, inode_list, i_list) {
470 if (inode->i_nlink > 1)
471 inode->i_ino = cur_ino++;
476 /* On success, all the inodes have been moved to the image inode list,
477 * so there's no need to delete from the hash lists in the inode
478 * table before freeing the hash buckets array directly. */
480 goto out_destroy_inode_table_raw;
481 out_destroy_inode_table:
482 for (size_t i = 0; i < inode_tab.capacity; i++) {
483 struct hlist_node *cur, *tmp;
484 hlist_for_each_entry_safe(inode, cur, tmp, &inode_tab.array[i], i_hlist)
485 hlist_del_init(&inode->i_hlist);
488 struct wim_inode *tmp;
489 list_for_each_entry_safe(inode, tmp, &inode_tab.extra_inodes, i_list)
490 list_del_init(&inode->i_list);
492 out_destroy_inode_table_raw:
493 destroy_inode_table(&inode_tab);