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/.
26 #include "wimlib_internal.h"
29 #include "lookup_table.h"
34 * / \ ----------- -----------
35 * | dentry<---| struct | | struct |---> dentry
36 * \ / | wim_inode| | wim_inode|
37 * dentry ------------ ------------
41 * ----------- ----------- / \
42 * dentry<---| struct | | struct |---> dentry dentry
43 * / | wim_inode| | wim_inode| \ /
44 * dentry ------------ ------------ dentry
48 * wim_inode_table->array | idx 0 | idx 1 |
52 /* Hash table to find inodes, identified by their inode ID.
54 struct wim_inode_table {
55 /* Fields for the hash table */
56 struct hlist_head *array;
61 * Linked list of "extra" inodes. These may be:
63 * - inodes with link count 1, which are all allowed to have 0 for their
64 * inode number, meaning we cannot insert them into the hash table
65 * before calling assign_inode_numbers().
67 * - Groups we create ourselves by splitting a nominal inode due to
68 * inconsistencies in the dentries. These inodes will share a inode
69 * number with some other inode until assign_inode_numbers() is
72 struct hlist_head extra_inodes;
76 init_inode_table(struct wim_inode_table *table, size_t capacity)
78 table->array = CALLOC(capacity, sizeof(table->array[0]));
80 ERROR("Cannot initalize inode table: out of memory");
81 return WIMLIB_ERR_NOMEM;
83 table->num_entries = 0;
84 table->capacity = capacity;
85 INIT_HLIST_HEAD(&table->extra_inodes);
90 destroy_inode_table(struct wim_inode_table *table)
96 inode_link_count(const struct wim_inode *inode)
98 const struct list_head *cur;
100 list_for_each(cur, &inode->i_dentry)
105 /* Insert a dentry into the inode table based on the inode number of the
106 * attached inode (which came from the hard link group ID field of the on-disk
109 inode_table_insert(struct wim_dentry *dentry, void *_table)
111 struct wim_inode_table *table = _table;
112 struct wim_inode *d_inode = dentry->d_inode;
114 if (d_inode->i_ino == 0) {
115 /* A dentry with a hard link group ID of 0 indicates that it's
116 * in a hard link group by itself. Add it to the list of extra
117 * inodes rather than inserting it into the hash lists. */
118 hlist_add_head(&d_inode->i_hlist, &table->extra_inodes);
120 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
121 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
122 wimlib_assert(d_inode->i_nlink == 1);
125 struct wim_inode *inode;
126 struct hlist_node *cur;
128 /* Try adding this dentry to an existing inode */
129 pos = d_inode->i_ino % table->capacity;
130 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
131 if (inode->i_ino == d_inode->i_ino) {
132 inode_add_dentry(dentry, inode);
138 /* No inode in the table has the same number as this one, so add
139 * it to the table. */
140 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
142 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
143 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
144 wimlib_assert(d_inode->i_nlink == 1);
146 /* XXX Make the table grow when too many entries have been
148 table->num_entries++;
153 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
155 print_inode_dentries(const struct wim_inode *inode)
157 struct wim_dentry *dentry;
158 inode_for_each_dentry(dentry, inode)
159 tprintf(T("`%"TS"'\n"), dentry->full_path);
164 inconsistent_inode(const struct wim_inode *inode)
166 #ifdef ENABLE_ERROR_MESSAGES
167 ERROR("An inconsistent hard link group that cannot be corrected has "
169 ERROR("The dentries are located at the following paths:");
170 print_inode_dentries(inode);
175 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
176 const struct wim_inode * restrict ref_inode_2)
178 wimlib_assert(ref_inode_1 != ref_inode_2);
180 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
182 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
183 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
185 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
186 const u8 *ref_1_hash, *ref_2_hash;
187 ref_1_hash = inode_stream_hash(ref_inode_1, i);
188 ref_2_hash = inode_stream_hash(ref_inode_2, i);
189 if (!hashes_equal(ref_1_hash, ref_2_hash))
191 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
192 &ref_inode_2->i_ads_entries[i - 1]))
200 inodes_consistent(const struct wim_inode * restrict ref_inode,
201 const struct wim_inode * restrict inode)
203 wimlib_assert(ref_inode != inode);
205 if (ref_inode->i_num_ads != inode->i_num_ads &&
206 inode->i_num_ads != 0)
208 if (ref_inode->i_security_id != inode->i_security_id
209 || ref_inode->i_attributes != inode->i_attributes)
211 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
212 const u8 *ref_hash, *hash;
213 ref_hash = inode_stream_hash(ref_inode, i);
214 hash = inode_stream_hash(inode, i);
215 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
217 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
218 &inode->i_ads_entries[i - 1]))
224 /* Fix up a "true" inode and check for inconsistencies */
226 fix_true_inode(struct wim_inode *inode, struct hlist_head *inode_list)
228 struct wim_dentry *dentry;
229 struct wim_dentry *ref_dentry = NULL;
230 struct wim_inode *ref_inode;
235 inode_for_each_dentry(dentry, inode) {
236 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
238 if (dentry->d_inode->i_creation_time > last_ctime)
239 last_ctime = dentry->d_inode->i_creation_time;
240 if (dentry->d_inode->i_last_write_time > last_mtime)
241 last_mtime = dentry->d_inode->i_last_write_time;
242 if (dentry->d_inode->i_last_access_time > last_atime)
243 last_atime = dentry->d_inode->i_last_access_time;
246 ref_inode = ref_dentry->d_inode;
247 ref_inode->i_nlink = 1;
248 hlist_add_head(&ref_inode->i_hlist, inode_list);
250 list_del(&inode->i_dentry);
251 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
253 inode_for_each_dentry(dentry, ref_inode) {
254 if (dentry != ref_dentry) {
255 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
256 inconsistent_inode(ref_inode);
257 return WIMLIB_ERR_INVALID_DENTRY;
259 /* Free the unneeded `struct wim_inode'. */
260 dentry->d_inode->i_hlist.next = NULL;
261 dentry->d_inode->i_hlist.pprev = NULL;
262 free_inode(dentry->d_inode);
263 dentry->d_inode = ref_inode;
264 ref_inode->i_nlink++;
267 ref_inode->i_creation_time = last_ctime;
268 ref_inode->i_last_write_time = last_mtime;
269 ref_inode->i_last_access_time = last_atime;
270 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
275 * Fixes up a nominal inode.
277 * By a nominal inode we mean a group of two or more dentries that share the
278 * same hard link group ID.
280 * If dentries in the inode are found to be inconsistent, we may split the inode
281 * into several "true" inodes.
283 * After splitting up each nominal inode into the "true" inodes we will
284 * canonicalize the link group by getting rid of all the unnecessary `struct
285 * wim_inode's. There will be just one `struct wim_inode' for each hard link
289 fix_nominal_inode(struct wim_inode *inode, struct hlist_head *inode_list)
291 struct wim_dentry *dentry;
292 struct hlist_node *cur, *tmp;
294 size_t num_true_inodes;
296 wimlib_assert(inode->i_nlink == inode_link_count(inode));
298 LIST_HEAD(dentries_with_data_streams);
299 LIST_HEAD(dentries_with_no_data_streams);
300 HLIST_HEAD(true_inodes);
302 /* Create a list of dentries in the nominal inode that have at
303 * least one data stream with a non-zero hash, and another list that
304 * contains the dentries that have a zero hash for all data streams. */
305 inode_for_each_dentry(dentry, inode) {
306 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
308 hash = inode_stream_hash(dentry->d_inode, i);
309 if (!is_zero_hash(hash)) {
310 list_add(&dentry->tmp_list,
311 &dentries_with_data_streams);
315 list_add(&dentry->tmp_list,
316 &dentries_with_no_data_streams);
321 /* If there are no dentries with data streams, we require the nominal
322 * inode to be a true inode */
323 if (list_empty(&dentries_with_data_streams)) {
325 if (inode->i_nlink > 1) {
326 DEBUG("Found link group of size %u without "
327 "any data streams:", inode->i_nlink);
328 print_inode_dentries(inode);
329 DEBUG("We are going to interpret it as true "
330 "link group, provided that the dentries "
334 return fix_true_inode(inode, inode_list);
337 /* One or more dentries had data streams specified. We check each of
338 * these dentries for consistency with the others to form a set of true
341 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
342 /* Look for a true inode that is consistent with this dentry and
343 * add this dentry to it. Or, if none of the true inodes are
344 * consistent with this dentry, add a new one (if that happens,
345 * we have split the hard link group). */
346 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
347 if (ref_inodes_consistent(inode, dentry->d_inode)) {
348 inode_add_dentry(dentry, inode);
353 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
354 inode_add_dentry(dentry, dentry->d_inode);
355 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
360 wimlib_assert(num_true_inodes != 0);
362 /* If there were dentries with no data streams, we require there to only
363 * be one true inode so that we know which inode to assign the
364 * streamless dentries to. */
365 if (!list_empty(&dentries_with_no_data_streams)) {
366 if (num_true_inodes != 1) {
367 ERROR("Hard inode ambiguity detected!");
368 ERROR("We split up inode 0x%"PRIx64" due to "
369 "inconsistencies,", inode->i_ino);
370 ERROR("but dentries with no stream information remained. "
371 "We don't know which inode");
372 ERROR("to assign them to.");
373 return WIMLIB_ERR_INVALID_DENTRY;
375 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
376 /* Assign the streamless dentries to the one and only true
378 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
379 inode_add_dentry(dentry, inode);
382 if (num_true_inodes != 1) {
383 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
385 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
386 "inodes:\n"), inode->i_ino, num_true_inodes);
387 tputs(T("----------------------------------------------------"
388 "--------------------------"));
390 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
391 tprintf(T("[Split inode %zu]\n"), i++);
392 print_inode_dentries(inode);
395 tputs(T("----------------------------------------------------"
396 "--------------------------"));
400 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
401 ret = fix_true_inode(inode, inode_list);
409 fix_inodes(struct wim_inode_table *table, struct hlist_head *inode_list)
411 struct wim_inode *inode;
412 struct hlist_node *cur, *tmp;
414 INIT_HLIST_HEAD(inode_list);
415 for (u64 i = 0; i < table->capacity; i++) {
416 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
417 ret = fix_nominal_inode(inode, inode_list);
422 hlist_for_each_safe(cur, tmp, &table->extra_inodes)
423 hlist_add_head(cur, inode_list);
428 * dentry_tree_fix_inodes():
430 * This function takes as input a tree of WIM dentries that initially has a
431 * different inode associated with each dentry. Sets of dentries that should
432 * share the same inode (a.k.a. hard link groups) are built using the i_ino
433 * field of each inode, then the link count and alias list for one inode in each
434 * set is set correctly and the unnecessary struct wim_inode's freed. The
435 * effect is to correctly associate exactly one struct wim_inode with each
436 * original inode, regardless of how many dentries are aliases for that inode.
438 * The special inode number of 0 indicates that the dentry is in a hard link
439 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
442 * This function also checks the dentries in each hard link group for
443 * consistency. In some WIMs, such as install.wim for some versions of Windows
444 * 7, dentries can share the same hard link group ID but not actually be hard
445 * linked to each other (based on conflicting information, such as file
446 * contents). This should be an error, but this case needs be handled. So,
447 * each "nominal" inode (the inode based on the inode numbers provided in the
448 * WIM) is examined for consistency and may be split into multiple "true" inodes
449 * that are maximally sized consistent sets of dentries.
451 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
452 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
453 * is returned in the hlist @inode_list.
456 dentry_tree_fix_inodes(struct wim_dentry *root, struct hlist_head *inode_list)
458 struct wim_inode_table inode_tab;
461 DEBUG("Inserting dentries into inode table");
462 ret = init_inode_table(&inode_tab, 9001);
466 for_dentry_in_tree(root, inode_table_insert, &inode_tab);
468 DEBUG("Cleaning up the hard link groups");
469 ret = fix_inodes(&inode_tab, inode_list);
470 destroy_inode_table(&inode_tab);
474 /* Assign inode numbers to a list of inodes and return the next available
477 assign_inode_numbers(struct hlist_head *inode_list)
479 DEBUG("Assigning inode numbers");
480 struct wim_inode *inode;
481 struct hlist_node *cur;
483 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
484 inode->i_ino = cur_ino;