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);
96 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
97 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
98 wimlib_assert(d_inode->i_nlink == 1);
101 struct wim_inode *inode;
102 struct hlist_node *cur;
104 /* Try adding this dentry to an existing inode */
105 pos = d_inode->i_ino % table->capacity;
106 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
107 if (inode->i_ino == d_inode->i_ino) {
108 if (unlikely((inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) ||
109 (d_inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY)))
111 ERROR("Unsupported directory hard link "
112 "\"%"TS"\" <=> \"%"TS"\"",
113 dentry_full_path(dentry),
114 dentry_full_path(inode_first_dentry(inode)));
115 return WIMLIB_ERR_INVALID_DENTRY;
117 inode_add_dentry(dentry, inode);
123 /* No inode in the table has the same number as this one, so add
124 * it to the table. */
125 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
127 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
128 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
129 wimlib_assert(d_inode->i_nlink == 1);
131 /* XXX Make the table grow when too many entries have been
133 table->num_entries++;
138 static struct wim_inode *
139 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
141 u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
142 size_t pos = hash % table->capacity;
143 struct wim_inode *inode;
144 struct hlist_node *cur;
146 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
147 if (inode->i_ino == ino && inode->i_devno == devno) {
148 DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
154 inode = new_timeless_inode();
157 inode->i_devno = devno;
158 hlist_add_head(&inode->i_hlist, &table->array[pos]);
159 table->num_entries++;
165 inode_ref_streams(struct wim_inode *inode)
167 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
168 struct wim_lookup_table_entry *lte;
169 lte = inode_stream_lte_resolved(inode, i);
175 /* Given a directory entry with the name @name for the file with the inode
176 * number @ino and device number @devno, create a new WIM dentry with an
177 * associated inode, where the inode is shared if an inode with the same @ino
178 * and @devno has already been created. On success, the new WIM dentry is
179 * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
180 * existing inode was used.
183 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
184 u64 ino, u64 devno, bool noshare,
185 struct wim_dentry **dentry_ret)
187 struct wim_dentry *dentry;
188 struct wim_inode *inode;
192 ret = new_dentry_with_timeless_inode(name, &dentry);
195 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
197 ret = new_dentry(name, &dentry);
200 inode = inode_table_get_inode(table, ino, devno);
203 return WIMLIB_ERR_NOMEM;
205 if (inode->i_nlink > 1)
206 inode_ref_streams(inode);
207 dentry->d_inode = inode;
208 inode_add_dentry(dentry, inode);
210 *dentry_ret = dentry;
214 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
216 print_inode_dentries(const struct wim_inode *inode)
218 struct wim_dentry *dentry;
219 inode_for_each_dentry(dentry, inode)
220 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
225 inconsistent_inode(const struct wim_inode *inode)
227 #ifdef ENABLE_ERROR_MESSAGES
228 ERROR("An inconsistent hard link group that cannot be corrected has "
230 ERROR("The dentries are located at the following paths:");
231 print_inode_dentries(inode);
236 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
237 const struct wim_inode * restrict ref_inode_2)
239 wimlib_assert(ref_inode_1 != ref_inode_2);
241 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
243 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
244 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
246 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
247 const u8 *ref_1_hash, *ref_2_hash;
248 ref_1_hash = inode_stream_hash(ref_inode_1, i);
249 ref_2_hash = inode_stream_hash(ref_inode_2, i);
250 if (!hashes_equal(ref_1_hash, ref_2_hash))
252 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
253 &ref_inode_2->i_ads_entries[i - 1]))
261 inodes_consistent(const struct wim_inode * restrict ref_inode,
262 const struct wim_inode * restrict inode)
264 wimlib_assert(ref_inode != inode);
266 if (ref_inode->i_num_ads != inode->i_num_ads &&
267 inode->i_num_ads != 0)
269 if (ref_inode->i_security_id != inode->i_security_id
270 || ref_inode->i_attributes != inode->i_attributes)
272 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
273 const u8 *ref_hash, *hash;
274 ref_hash = inode_stream_hash(ref_inode, i);
275 hash = inode_stream_hash(inode, i);
276 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
278 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
279 &inode->i_ads_entries[i - 1]))
285 /* Fix up a "true" inode and check for inconsistencies */
287 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
289 struct wim_dentry *dentry;
290 struct wim_dentry *ref_dentry = NULL;
291 struct wim_inode *ref_inode;
296 inode_for_each_dentry(dentry, inode) {
297 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
299 if (dentry->d_inode->i_creation_time > last_ctime)
300 last_ctime = dentry->d_inode->i_creation_time;
301 if (dentry->d_inode->i_last_write_time > last_mtime)
302 last_mtime = dentry->d_inode->i_last_write_time;
303 if (dentry->d_inode->i_last_access_time > last_atime)
304 last_atime = dentry->d_inode->i_last_access_time;
307 ref_inode = ref_dentry->d_inode;
308 ref_inode->i_nlink = 1;
309 list_add_tail(&ref_inode->i_list, inode_list);
311 list_del(&inode->i_dentry);
312 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
314 inode_for_each_dentry(dentry, ref_inode) {
315 if (dentry != ref_dentry) {
316 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
317 inconsistent_inode(ref_inode);
318 return WIMLIB_ERR_INVALID_DENTRY;
320 /* Free the unneeded `struct wim_inode'. */
321 free_inode(dentry->d_inode);
322 dentry->d_inode = ref_inode;
323 ref_inode->i_nlink++;
326 ref_inode->i_creation_time = last_ctime;
327 ref_inode->i_last_write_time = last_mtime;
328 ref_inode->i_last_access_time = last_atime;
329 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
334 * Fixes up a nominal inode.
336 * By a nominal inode we mean a group of two or more dentries that share the
337 * same hard link group ID.
339 * If dentries in the inode are found to be inconsistent, we may split the inode
340 * into several "true" inodes.
342 * After splitting up each nominal inode into the "true" inodes we will
343 * canonicalize the link group by getting rid of all the unnecessary `struct
344 * wim_inode's. There will be just one `struct wim_inode' for each hard link
348 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
349 bool *ino_changes_needed)
351 struct wim_dentry *dentry;
352 struct hlist_node *cur, *tmp;
354 size_t num_true_inodes;
356 wimlib_assert(inode->i_nlink == inode_link_count(inode));
358 if (inode->i_nlink > 1 &&
359 (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
361 ERROR("Found unsupported directory hard link!");
362 return WIMLIB_ERR_INVALID_DENTRY;
365 LIST_HEAD(dentries_with_data_streams);
366 LIST_HEAD(dentries_with_no_data_streams);
367 HLIST_HEAD(true_inodes);
369 /* Create a list of dentries in the nominal inode that have at
370 * least one data stream with a non-zero hash, and another list that
371 * contains the dentries that have a zero hash for all data streams. */
372 inode_for_each_dentry(dentry, inode) {
373 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
375 hash = inode_stream_hash(dentry->d_inode, i);
376 if (!is_zero_hash(hash)) {
377 list_add(&dentry->tmp_list,
378 &dentries_with_data_streams);
382 list_add(&dentry->tmp_list,
383 &dentries_with_no_data_streams);
388 /* If there are no dentries with data streams, we require the nominal
389 * inode to be a true inode */
390 if (list_empty(&dentries_with_data_streams)) {
392 if (inode->i_nlink > 1) {
393 DEBUG("Found link group of size %u without "
394 "any data streams:", inode->i_nlink);
395 print_inode_dentries(inode);
396 DEBUG("We are going to interpret it as true "
397 "link group, provided that the dentries "
401 return fix_true_inode(inode, inode_list);
404 /* One or more dentries had data streams specified. We check each of
405 * these dentries for consistency with the others to form a set of true
408 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
409 /* Look for a true inode that is consistent with this dentry and
410 * add this dentry to it. Or, if none of the true inodes are
411 * consistent with this dentry, add a new one (if that happens,
412 * we have split the hard link group). */
413 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
414 if (ref_inodes_consistent(inode, dentry->d_inode)) {
415 inode_add_dentry(dentry, inode);
420 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
421 inode_add_dentry(dentry, dentry->d_inode);
422 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
427 wimlib_assert(num_true_inodes != 0);
429 /* If there were dentries with no data streams, we require there to only
430 * be one true inode so that we know which inode to assign the
431 * streamless dentries to. */
432 if (!list_empty(&dentries_with_no_data_streams)) {
433 if (num_true_inodes != 1) {
434 ERROR("Hard inode ambiguity detected!");
435 ERROR("We split up inode 0x%"PRIx64" due to "
436 "inconsistencies,", inode->i_ino);
437 ERROR("but dentries with no stream information remained. "
438 "We don't know which inode");
439 ERROR("to assign them to.");
440 return WIMLIB_ERR_INVALID_DENTRY;
442 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
443 /* Assign the streamless dentries to the one and only true
445 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
446 inode_add_dentry(dentry, inode);
448 if (num_true_inodes != 1) {
450 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
452 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
453 "inodes:\n"), inode->i_ino, num_true_inodes);
454 tputs(T("----------------------------------------------------"
455 "--------------------------"));
457 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
458 tprintf(T("[Split inode %zu]\n"), i++);
459 print_inode_dentries(inode);
462 tputs(T("----------------------------------------------------"
463 "--------------------------"));
465 *ino_changes_needed = true;
468 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
469 ret = fix_true_inode(inode, inode_list);
477 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
478 bool *ino_changes_needed)
480 struct wim_inode *inode;
481 struct hlist_node *cur, *tmp;
483 INIT_LIST_HEAD(inode_list);
484 for (u64 i = 0; i < table->capacity; i++) {
485 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
486 INIT_LIST_HEAD(&inode->i_list);
487 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
492 list_splice_tail(&table->extra_inodes, inode_list);
497 * dentry_tree_fix_inodes():
499 * This function takes as input a tree of WIM dentries that initially has a
500 * different inode associated with each dentry. Sets of dentries that should
501 * share the same inode (a.k.a. hard link groups) are built using the i_ino
502 * field of each inode, then the link count and alias list for one inode in each
503 * set is set correctly and the unnecessary struct wim_inode's freed. The
504 * effect is to correctly associate exactly one struct wim_inode with each
505 * original inode, regardless of how many dentries are aliases for that inode.
507 * The special inode number of 0 indicates that the dentry is in a hard link
508 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
511 * This function also checks the dentries in each hard link group for
512 * consistency. In some WIMs, such as install.wim for some versions of Windows
513 * 7, dentries can share the same hard link group ID but not actually be hard
514 * linked to each other (based on conflicting information, such as file
515 * contents). This should be an error, but this case needs be handled. So,
516 * each "nominal" inode (the inode based on the inode numbers provided in the
517 * WIM) is examined for consistency and may be split into multiple "true" inodes
518 * that are maximally sized consistent sets of dentries.
520 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
521 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
522 * is returned in the hlist @inode_list.
525 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
527 struct wim_inode_table inode_tab;
529 bool ino_changes_needed;
531 DEBUG("Inserting dentries into inode table");
532 ret = init_inode_table(&inode_tab, 9001);
536 ret = for_dentry_in_tree(root, inode_table_insert, &inode_tab);
538 goto out_destroy_image_table;
540 DEBUG("Cleaning up the hard link groups");
541 ino_changes_needed = false;
542 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
544 goto out_destroy_image_table;
546 if (ino_changes_needed) {
548 struct wim_inode *inode;
550 WARNING("Re-assigning inode numbers due to inode inconsistencies");
551 list_for_each_entry(inode, inode_list, i_list) {
552 if (inode->i_nlink > 1)
553 inode->i_ino = cur_ino++;
559 out_destroy_image_table:
560 destroy_inode_table(&inode_tab);
565 /* Assign consecutive inode numbers to the inodes in the inode table, and move
566 * the inodes to a single list @head. */
568 inode_table_prepare_inode_list(struct wim_inode_table *table,
569 struct list_head *head)
571 struct wim_inode *inode;
572 struct hlist_node *cur, *tmp;
575 list_for_each_entry(inode, head, i_list) {
576 if (inode->i_nlink > 1)
577 inode->i_ino = cur_ino++;
582 for (size_t i = 0; i < table->capacity; i++) {
583 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
585 if (inode->i_nlink > 1)
586 inode->i_ino = cur_ino++;
589 list_add_tail(&inode->i_list, head);
591 INIT_HLIST_HEAD(&table->array[i]);
593 list_splice_tail(&table->extra_inodes, head);
594 table->num_entries = 0;