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 ERROR("An inconsistent hard link group that cannot be corrected has "
61 ERROR("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 * restrict ref_inode_1,
77 const struct wim_inode * restrict ref_inode_2)
79 wimlib_assert(ref_inode_1 != ref_inode_2);
81 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
83 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
84 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
86 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
87 const u8 *ref_1_hash, *ref_2_hash;
88 ref_1_hash = inode_stream_hash(ref_inode_1, i);
89 ref_2_hash = inode_stream_hash(ref_inode_2, i);
90 if (!hashes_equal(ref_1_hash, ref_2_hash))
92 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
93 &ref_inode_2->i_ads_entries[i - 1]))
100 /* Returns true iff the specified inode has any data streams with nonzero hash.
103 inode_has_data_streams(const struct wim_inode *inode)
105 for (unsigned i = 0; i <= inode->i_num_ads; i++)
106 if (!is_zero_hash(inode_stream_hash(inode, i)))
111 /* Returns true iff the specified dentry has any data streams with nonzero hash.
114 dentry_has_data_streams(const struct wim_dentry *dentry)
116 return inode_has_data_streams(dentry->d_inode);
120 inodes_consistent(const struct wim_inode *ref_inode,
121 const struct wim_inode *inode)
123 if (ref_inode->i_security_id != inode->i_security_id)
126 if (ref_inode->i_attributes != inode->i_attributes)
129 if (inode_has_data_streams(inode)) {
130 if (ref_inode->i_num_ads != inode->i_num_ads)
132 for (unsigned i = 0; i <= ref_inode->i_num_ads; i++) {
133 const u8 *ref_hash, *hash;
135 ref_hash = inode_stream_hash(ref_inode, i);
136 hash = inode_stream_hash(inode, i);
137 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
139 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
140 &inode->i_ads_entries[i - 1]))
147 /* Fix up a "true" inode and check for inconsistencies */
149 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
151 struct wim_dentry *dentry;
152 struct wim_dentry *ref_dentry = NULL;
153 struct wim_inode *ref_inode;
158 inode_for_each_dentry(dentry, inode) {
159 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
161 if (dentry->d_inode->i_creation_time > last_ctime)
162 last_ctime = dentry->d_inode->i_creation_time;
163 if (dentry->d_inode->i_last_write_time > last_mtime)
164 last_mtime = dentry->d_inode->i_last_write_time;
165 if (dentry->d_inode->i_last_access_time > last_atime)
166 last_atime = dentry->d_inode->i_last_access_time;
169 ref_inode = ref_dentry->d_inode;
170 wimlib_assert(ref_inode->i_nlink == 1);
171 list_add_tail(&ref_inode->i_list, inode_list);
173 list_del(&inode->i_dentry);
174 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
176 inode_for_each_dentry(dentry, ref_inode) {
177 if (dentry != ref_dentry) {
178 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
179 inconsistent_inode(ref_inode);
180 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
182 /* Free the unneeded `struct wim_inode'. */
183 wimlib_assert(dentry->d_inode->i_nlink == 1);
184 free_inode(dentry->d_inode);
185 dentry->d_inode = ref_inode;
186 ref_inode->i_nlink++;
189 ref_inode->i_creation_time = last_ctime;
190 ref_inode->i_last_write_time = last_mtime;
191 ref_inode->i_last_access_time = last_atime;
192 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
197 * Fixes up a nominal inode.
199 * By a nominal inode we mean a group of two or more dentries that share the
200 * same hard link group ID.
202 * If dentries in the inode are found to be inconsistent, we may split the inode
203 * into several "true" inodes.
205 * After splitting up each nominal inode into the "true" inodes we will
206 * canonicalize the link group by getting rid of all the unnecessary `struct
207 * wim_inode's. There will be just one `struct wim_inode' for each hard link
211 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
212 bool *ino_changes_needed)
214 struct wim_dentry *dentry;
215 struct hlist_node *cur, *tmp;
217 size_t num_true_inodes;
219 LIST_HEAD(dentries_with_data_streams);
220 LIST_HEAD(dentries_with_no_data_streams);
221 HLIST_HEAD(true_inodes);
223 /* Create a list of dentries in the nominal inode that have at
224 * least one data stream with a non-zero hash, and another list that
225 * contains the dentries that have a zero hash for all data streams. */
226 inode_for_each_dentry(dentry, inode) {
227 if (dentry_has_data_streams(dentry))
228 list_add(&dentry->tmp_list, &dentries_with_data_streams);
230 list_add(&dentry->tmp_list, &dentries_with_no_data_streams);
233 /* If there are no dentries with data streams, we require the nominal
234 * inode to be a true inode */
235 if (list_empty(&dentries_with_data_streams)) {
237 unsigned nominal_group_size = inode_link_count(inode);
238 if (nominal_group_size > 1) {
239 DEBUG("Found link group of size %u without "
240 "any data streams:", nominal_group_size);
241 print_inode_dentries(inode);
242 DEBUG("We are going to interpret it as true "
243 "link group, provided that the dentries "
247 return fix_true_inode(inode, inode_list);
250 /* One or more dentries had data streams specified. We check each of
251 * these dentries for consistency with the others to form a set of true
254 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
255 /* Look for a true inode that is consistent with this dentry and
256 * add this dentry to it. Or, if none of the true inodes are
257 * consistent with this dentry, add a new one (if that happens,
258 * we have split the hard link group). */
259 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
260 if (ref_inodes_consistent(inode, dentry->d_inode)) {
261 inode_add_dentry(dentry, inode);
266 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
267 inode_add_dentry(dentry, dentry->d_inode);
268 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
273 wimlib_assert(num_true_inodes != 0);
275 /* If there were dentries with no data streams, we require there to only
276 * be one true inode so that we know which inode to assign the
277 * streamless dentries to. */
278 if (!list_empty(&dentries_with_no_data_streams)) {
279 if (num_true_inodes != 1) {
280 ERROR("Hard link ambiguity detected!");
281 ERROR("We split up inode 0x%"PRIx64" due to "
282 "inconsistencies,", inode->i_ino);
283 ERROR("but dentries with no stream information remained. "
284 "We don't know which inode");
285 ERROR("to assign them to.");
286 ret = WIMLIB_ERR_INVALID_METADATA_RESOURCE;
287 goto out_cleanup_true_inode_list;
289 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
290 /* Assign the streamless dentries to the one and only true
292 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
293 inode_add_dentry(dentry, inode);
295 if (num_true_inodes != 1) {
297 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
299 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
300 "inodes:\n"), inode->i_ino, num_true_inodes);
301 tputs(T("----------------------------------------------------"
302 "--------------------------"));
304 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
305 tprintf(T("[Split inode %zu]\n"), i++);
306 print_inode_dentries(inode);
309 tputs(T("----------------------------------------------------"
310 "--------------------------"));
312 *ino_changes_needed = true;
315 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
316 hlist_del_init(&inode->i_hlist);
317 ret = fix_true_inode(inode, inode_list);
319 goto out_cleanup_true_inode_list;
323 out_cleanup_true_inode_list:
324 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist)
325 hlist_del_init(&inode->i_hlist);
331 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
332 bool *ino_changes_needed)
334 struct wim_inode *inode;
335 struct hlist_node *cur, *tmp;
337 INIT_LIST_HEAD(inode_list);
338 for (u64 i = 0; i < table->capacity; i++) {
339 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
340 hlist_del_init(&inode->i_hlist);
341 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
346 list_splice_tail(&table->extra_inodes, inode_list);
350 /* Insert a dentry into the inode table based on the inode number of the
351 * attached inode (which came from the hard link group ID field of the on-disk
354 inode_table_insert(struct wim_dentry *dentry, void *_table)
356 struct wim_inode_table *table = _table;
357 struct wim_inode *d_inode = dentry->d_inode;
359 if (d_inode->i_ino == 0) {
360 /* A dentry with a hard link group ID of 0 indicates that it's
361 * in a hard link group by itself. Add it to the list of extra
362 * inodes rather than inserting it into the hash lists. */
363 list_add_tail(&d_inode->i_list, &table->extra_inodes);
366 struct wim_inode *inode;
367 struct hlist_node *cur;
369 /* Try adding this dentry to an existing inode */
370 pos = d_inode->i_ino % table->capacity;
371 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
372 if (inode->i_ino == d_inode->i_ino) {
373 if (unlikely((inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) ||
374 (d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)))
376 ERROR("Unsupported directory hard link "
377 "\"%"TS"\" <=> \"%"TS"\"",
378 dentry_full_path(dentry),
379 dentry_full_path(inode_first_dentry(inode)));
380 return WIMLIB_ERR_INVALID_METADATA_RESOURCE;
382 inode_add_dentry(dentry, inode);
387 /* No inode in the table has the same number as this one, so add
388 * it to the table. */
389 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
391 /* XXX Make the table grow when too many entries have been
393 table->num_entries++;
400 * dentry_tree_fix_inodes():
402 * This function takes as input a tree of WIM dentries that initially has a
403 * different inode associated with each dentry. Sets of dentries that should
404 * share the same inode (a.k.a. hard link groups) are built using the i_ino
405 * field of each inode, then the link count and alias list for one inode in each
406 * set is set correctly and the unnecessary struct wim_inode's freed. The
407 * effect is to correctly associate exactly one struct wim_inode with each
408 * original inode, regardless of how many dentries are aliases for that inode.
410 * The special inode number of 0 indicates that the dentry is in a hard link
411 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
414 * This function also checks the dentries in each hard link group for
415 * consistency. In some WIMs, such as install.wim for some versions of Windows
416 * 7, dentries can share the same hard link group ID but not actually be hard
417 * linked to each other (based on conflicting information, such as file
418 * contents). This should be an error, but this case needs be handled. So,
419 * each "nominal" inode (the inode based on the inode numbers provided in the
420 * WIM) is examined for consistency and may be split into multiple "true" inodes
421 * that are maximally sized consistent sets of dentries.
423 * On success, the list of "true" inodes, linked by the i_hlist field,
424 * is returned in the hlist @inode_list.
427 * WIMLIB_ERR_SUCCESS (0)
428 * WIMLIB_ERR_INVALID_METADATA_RESOURCE
432 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
434 struct wim_inode_table inode_tab;
436 bool ino_changes_needed;
437 struct wim_inode *inode;
439 DEBUG("Inserting dentries into inode table");
440 ret = init_inode_table(&inode_tab, 9001);
444 ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
446 goto out_destroy_inode_table;
448 DEBUG("Cleaning up the hard link groups");
449 ino_changes_needed = false;
450 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
452 goto out_destroy_inode_table;
454 if (ino_changes_needed) {
457 WARNING("The WIM image contains invalid hard links. Fixing.");
459 list_for_each_entry(inode, inode_list, i_list) {
460 if (inode->i_nlink > 1)
461 inode->i_ino = cur_ino++;
466 /* On success, all the inodes have been moved to the image inode list,
467 * so there's no need to delete from from the hash lists in the inode
468 * table before freeing the hash buckets array directly. */
470 goto out_destroy_inode_table_raw;
471 out_destroy_inode_table:
472 for (size_t i = 0; i < inode_tab.capacity; i++) {
473 struct hlist_node *cur, *tmp;
474 hlist_for_each_entry_safe(inode, cur, tmp, &inode_tab.array[i], i_hlist)
475 hlist_del_init(&inode->i_hlist);
478 struct wim_inode *tmp;
479 list_for_each_entry_safe(inode, tmp, &inode_tab.extra_inodes, i_list)
480 list_del_init(&inode->i_list);
482 out_destroy_inode_table_raw:
483 destroy_inode_table(&inode_tab);