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 |
54 init_inode_table(struct wim_inode_table *table, size_t capacity)
56 table->array = CALLOC(capacity, sizeof(table->array[0]));
58 ERROR("Cannot initalize inode table: out of memory");
59 return WIMLIB_ERR_NOMEM;
61 table->num_entries = 0;
62 table->capacity = capacity;
63 INIT_LIST_HEAD(&table->extra_inodes);
68 inode_link_count(const struct wim_inode *inode)
70 const struct list_head *cur;
72 list_for_each(cur, &inode->i_dentry)
77 /* Insert a dentry into the inode table based on the inode number of the
78 * attached inode (which came from the hard link group ID field of the on-disk
81 inode_table_insert(struct wim_dentry *dentry, void *_table)
83 struct wim_inode_table *table = _table;
84 struct wim_inode *d_inode = dentry->d_inode;
86 if (d_inode->i_ino == 0) {
87 /* A dentry with a hard link group ID of 0 indicates that it's
88 * in a hard link group by itself. Add it to the list of extra
89 * inodes rather than inserting it into the hash lists. */
90 list_add_tail(&d_inode->i_list, &table->extra_inodes);
92 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
93 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
94 wimlib_assert(d_inode->i_nlink == 1);
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 inode_add_dentry(dentry, inode);
110 /* No inode in the table has the same number as this one, so add
111 * it to the table. */
112 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
114 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
115 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
116 wimlib_assert(d_inode->i_nlink == 1);
118 /* XXX Make the table grow when too many entries have been
120 table->num_entries++;
125 static struct wim_inode *
126 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
128 u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
129 size_t pos = hash % table->capacity;
130 struct wim_inode *inode;
131 struct hlist_node *cur;
133 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
134 if (inode->i_ino == ino && inode->i_devno == devno) {
135 DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
141 inode = new_timeless_inode();
144 inode->i_devno = devno;
145 hlist_add_head(&inode->i_hlist, &table->array[pos]);
146 table->num_entries++;
152 inode_ref_streams(struct wim_inode *inode)
154 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
155 struct wim_lookup_table_entry *lte;
156 lte = inode_stream_lte_resolved(inode, i);
162 /* Given a directory entry with the name @name for the file with the inode
163 * number @ino and device number @devno, create a new WIM dentry with an
164 * associated inode, where the inode is shared if an inode with the same @ino
165 * and @devno has already been created. On success, the new WIM dentry is
166 * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
167 * existing inode was used.
170 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
171 u64 ino, u64 devno, struct wim_dentry **dentry_ret)
173 struct wim_dentry *dentry;
174 struct wim_inode *inode;
177 ret = new_dentry(name, &dentry);
181 inode = inode_table_get_inode(table, ino, devno);
184 return WIMLIB_ERR_NOMEM;
186 if (inode->i_nlink > 1)
187 inode_ref_streams(inode);
188 dentry->d_inode = inode;
189 inode_add_dentry(dentry, inode);
190 *dentry_ret = dentry;
194 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
196 print_inode_dentries(const struct wim_inode *inode)
198 struct wim_dentry *dentry;
199 inode_for_each_dentry(dentry, inode)
200 tprintf(T("%"TS), dentry_full_path(dentry));
205 inconsistent_inode(const struct wim_inode *inode)
207 #ifdef ENABLE_ERROR_MESSAGES
208 ERROR("An inconsistent hard link group that cannot be corrected has "
210 ERROR("The dentries are located at the following paths:");
211 print_inode_dentries(inode);
216 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
217 const struct wim_inode * restrict ref_inode_2)
219 wimlib_assert(ref_inode_1 != ref_inode_2);
221 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
223 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
224 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
226 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
227 const u8 *ref_1_hash, *ref_2_hash;
228 ref_1_hash = inode_stream_hash(ref_inode_1, i);
229 ref_2_hash = inode_stream_hash(ref_inode_2, i);
230 if (!hashes_equal(ref_1_hash, ref_2_hash))
232 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
233 &ref_inode_2->i_ads_entries[i - 1]))
241 inodes_consistent(const struct wim_inode * restrict ref_inode,
242 const struct wim_inode * restrict inode)
244 wimlib_assert(ref_inode != inode);
246 if (ref_inode->i_num_ads != inode->i_num_ads &&
247 inode->i_num_ads != 0)
249 if (ref_inode->i_security_id != inode->i_security_id
250 || ref_inode->i_attributes != inode->i_attributes)
252 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
253 const u8 *ref_hash, *hash;
254 ref_hash = inode_stream_hash(ref_inode, i);
255 hash = inode_stream_hash(inode, i);
256 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
258 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
259 &inode->i_ads_entries[i - 1]))
265 /* Fix up a "true" inode and check for inconsistencies */
267 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
269 struct wim_dentry *dentry;
270 struct wim_dentry *ref_dentry = NULL;
271 struct wim_inode *ref_inode;
276 inode_for_each_dentry(dentry, inode) {
277 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
279 if (dentry->d_inode->i_creation_time > last_ctime)
280 last_ctime = dentry->d_inode->i_creation_time;
281 if (dentry->d_inode->i_last_write_time > last_mtime)
282 last_mtime = dentry->d_inode->i_last_write_time;
283 if (dentry->d_inode->i_last_access_time > last_atime)
284 last_atime = dentry->d_inode->i_last_access_time;
287 ref_inode = ref_dentry->d_inode;
288 ref_inode->i_nlink = 1;
289 list_add_tail(&ref_inode->i_list, inode_list);
291 list_del(&inode->i_dentry);
292 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
294 inode_for_each_dentry(dentry, ref_inode) {
295 if (dentry != ref_dentry) {
296 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
297 inconsistent_inode(ref_inode);
298 return WIMLIB_ERR_INVALID_DENTRY;
300 /* Free the unneeded `struct wim_inode'. */
301 dentry->d_inode->i_hlist.next = NULL;
302 dentry->d_inode->i_hlist.pprev = NULL;
303 free_inode(dentry->d_inode);
304 dentry->d_inode = ref_inode;
305 ref_inode->i_nlink++;
308 ref_inode->i_creation_time = last_ctime;
309 ref_inode->i_last_write_time = last_mtime;
310 ref_inode->i_last_access_time = last_atime;
311 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
316 * Fixes up a nominal inode.
318 * By a nominal inode we mean a group of two or more dentries that share the
319 * same hard link group ID.
321 * If dentries in the inode are found to be inconsistent, we may split the inode
322 * into several "true" inodes.
324 * After splitting up each nominal inode into the "true" inodes we will
325 * canonicalize the link group by getting rid of all the unnecessary `struct
326 * wim_inode's. There will be just one `struct wim_inode' for each hard link
330 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
331 bool *ino_changes_needed)
333 struct wim_dentry *dentry;
334 struct hlist_node *cur, *tmp;
336 size_t num_true_inodes;
338 wimlib_assert(inode->i_nlink == inode_link_count(inode));
340 LIST_HEAD(dentries_with_data_streams);
341 LIST_HEAD(dentries_with_no_data_streams);
342 HLIST_HEAD(true_inodes);
344 /* Create a list of dentries in the nominal inode that have at
345 * least one data stream with a non-zero hash, and another list that
346 * contains the dentries that have a zero hash for all data streams. */
347 inode_for_each_dentry(dentry, inode) {
348 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
350 hash = inode_stream_hash(dentry->d_inode, i);
351 if (!is_zero_hash(hash)) {
352 list_add(&dentry->tmp_list,
353 &dentries_with_data_streams);
357 list_add(&dentry->tmp_list,
358 &dentries_with_no_data_streams);
363 /* If there are no dentries with data streams, we require the nominal
364 * inode to be a true inode */
365 if (list_empty(&dentries_with_data_streams)) {
367 if (inode->i_nlink > 1) {
368 DEBUG("Found link group of size %u without "
369 "any data streams:", inode->i_nlink);
370 print_inode_dentries(inode);
371 DEBUG("We are going to interpret it as true "
372 "link group, provided that the dentries "
376 return fix_true_inode(inode, inode_list);
379 /* One or more dentries had data streams specified. We check each of
380 * these dentries for consistency with the others to form a set of true
383 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
384 /* Look for a true inode that is consistent with this dentry and
385 * add this dentry to it. Or, if none of the true inodes are
386 * consistent with this dentry, add a new one (if that happens,
387 * we have split the hard link group). */
388 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
389 if (ref_inodes_consistent(inode, dentry->d_inode)) {
390 inode_add_dentry(dentry, inode);
395 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
396 inode_add_dentry(dentry, dentry->d_inode);
397 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
402 wimlib_assert(num_true_inodes != 0);
404 /* If there were dentries with no data streams, we require there to only
405 * be one true inode so that we know which inode to assign the
406 * streamless dentries to. */
407 if (!list_empty(&dentries_with_no_data_streams)) {
408 if (num_true_inodes != 1) {
409 ERROR("Hard inode ambiguity detected!");
410 ERROR("We split up inode 0x%"PRIx64" due to "
411 "inconsistencies,", inode->i_ino);
412 ERROR("but dentries with no stream information remained. "
413 "We don't know which inode");
414 ERROR("to assign them to.");
415 return WIMLIB_ERR_INVALID_DENTRY;
417 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
418 /* Assign the streamless dentries to the one and only true
420 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
421 inode_add_dentry(dentry, inode);
423 if (num_true_inodes != 1) {
425 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
427 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
428 "inodes:\n"), inode->i_ino, num_true_inodes);
429 tputs(T("----------------------------------------------------"
430 "--------------------------"));
432 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
433 tprintf(T("[Split inode %zu]\n"), i++);
434 print_inode_dentries(inode);
437 tputs(T("----------------------------------------------------"
438 "--------------------------"));
440 *ino_changes_needed = true;
443 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
444 ret = fix_true_inode(inode, inode_list);
452 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
453 bool *ino_changes_needed)
455 struct wim_inode *inode;
456 struct hlist_node *cur, *tmp;
458 INIT_LIST_HEAD(inode_list);
459 for (u64 i = 0; i < table->capacity; i++) {
460 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
461 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
466 list_splice_tail(&table->extra_inodes, inode_list);
471 * dentry_tree_fix_inodes():
473 * This function takes as input a tree of WIM dentries that initially has a
474 * different inode associated with each dentry. Sets of dentries that should
475 * share the same inode (a.k.a. hard link groups) are built using the i_ino
476 * field of each inode, then the link count and alias list for one inode in each
477 * set is set correctly and the unnecessary struct wim_inode's freed. The
478 * effect is to correctly associate exactly one struct wim_inode with each
479 * original inode, regardless of how many dentries are aliases for that inode.
481 * The special inode number of 0 indicates that the dentry is in a hard link
482 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
485 * This function also checks the dentries in each hard link group for
486 * consistency. In some WIMs, such as install.wim for some versions of Windows
487 * 7, dentries can share the same hard link group ID but not actually be hard
488 * linked to each other (based on conflicting information, such as file
489 * contents). This should be an error, but this case needs be handled. So,
490 * each "nominal" inode (the inode based on the inode numbers provided in the
491 * WIM) is examined for consistency and may be split into multiple "true" inodes
492 * that are maximally sized consistent sets of dentries.
494 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
495 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
496 * is returned in the hlist @inode_list.
499 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
501 struct wim_inode_table inode_tab;
503 bool ino_changes_needed;
505 DEBUG("Inserting dentries into inode table");
506 ret = init_inode_table(&inode_tab, 9001);
510 for_dentry_in_tree(root, inode_table_insert, &inode_tab);
512 DEBUG("Cleaning up the hard link groups");
513 ino_changes_needed = false;
514 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
515 destroy_inode_table(&inode_tab);
517 if (ret == 0 && ino_changes_needed) {
519 struct wim_inode *inode;
521 WARNING("Re-assigning inode numbers due to inode inconsistencies");
522 list_for_each_entry(inode, inode_list, i_list) {
523 if (inode->i_nlink > 1)
524 inode->i_ino = cur_ino++;
532 /* Assign consecutive inode numbers to the inodes in the inode table, and move
533 * the inodes to a single list @head. */
535 inode_table_prepare_inode_list(struct wim_inode_table *table,
536 struct list_head *head)
538 struct wim_inode *inode;
539 struct hlist_node *cur, *tmp;
542 INIT_LIST_HEAD(head);
543 for (size_t i = 0; i < table->capacity; i++) {
544 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
546 if (inode->i_nlink > 1)
547 inode->i_ino = cur_ino++;
550 list_add_tail(&inode->i_list, head);
552 INIT_HLIST_HEAD(&table->array[i]);
554 table->num_entries = 0;