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 inode_add_dentry(dentry, inode);
114 /* No inode in the table has the same number as this one, so add
115 * it to the table. */
116 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
118 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
119 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
120 wimlib_assert(d_inode->i_nlink == 1);
122 /* XXX Make the table grow when too many entries have been
124 table->num_entries++;
129 static struct wim_inode *
130 inode_table_get_inode(struct wim_inode_table *table, u64 ino, u64 devno)
132 u64 hash = hash_u64(hash_u64(ino) + hash_u64(devno));
133 size_t pos = hash % table->capacity;
134 struct wim_inode *inode;
135 struct hlist_node *cur;
137 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
138 if (inode->i_ino == ino && inode->i_devno == devno) {
139 DEBUG("Using existing inode {devno=%"PRIu64", ino=%"PRIu64"}",
145 inode = new_timeless_inode();
148 inode->i_devno = devno;
149 hlist_add_head(&inode->i_hlist, &table->array[pos]);
150 table->num_entries++;
156 inode_ref_streams(struct wim_inode *inode)
158 for (unsigned i = 0; i <= inode->i_num_ads; i++) {
159 struct wim_lookup_table_entry *lte;
160 lte = inode_stream_lte_resolved(inode, i);
166 /* Given a directory entry with the name @name for the file with the inode
167 * number @ino and device number @devno, create a new WIM dentry with an
168 * associated inode, where the inode is shared if an inode with the same @ino
169 * and @devno has already been created. On success, the new WIM dentry is
170 * written to *dentry_ret, and its inode has i_nlink > 1 if a previously
171 * existing inode was used.
174 inode_table_new_dentry(struct wim_inode_table *table, const tchar *name,
175 u64 ino, u64 devno, bool noshare,
176 struct wim_dentry **dentry_ret)
178 struct wim_dentry *dentry;
179 struct wim_inode *inode;
183 ret = new_dentry_with_timeless_inode(name, &dentry);
186 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
188 ret = new_dentry(name, &dentry);
191 inode = inode_table_get_inode(table, ino, devno);
194 return WIMLIB_ERR_NOMEM;
196 if (inode->i_nlink > 1)
197 inode_ref_streams(inode);
198 dentry->d_inode = inode;
199 inode_add_dentry(dentry, inode);
201 *dentry_ret = dentry;
205 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
207 print_inode_dentries(const struct wim_inode *inode)
209 struct wim_dentry *dentry;
210 inode_for_each_dentry(dentry, inode)
211 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
216 inconsistent_inode(const struct wim_inode *inode)
218 #ifdef ENABLE_ERROR_MESSAGES
219 ERROR("An inconsistent hard link group that cannot be corrected has "
221 ERROR("The dentries are located at the following paths:");
222 print_inode_dentries(inode);
227 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
228 const struct wim_inode * restrict ref_inode_2)
230 wimlib_assert(ref_inode_1 != ref_inode_2);
232 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
234 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
235 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
237 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
238 const u8 *ref_1_hash, *ref_2_hash;
239 ref_1_hash = inode_stream_hash(ref_inode_1, i);
240 ref_2_hash = inode_stream_hash(ref_inode_2, i);
241 if (!hashes_equal(ref_1_hash, ref_2_hash))
243 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
244 &ref_inode_2->i_ads_entries[i - 1]))
252 inodes_consistent(const struct wim_inode * restrict ref_inode,
253 const struct wim_inode * restrict inode)
255 wimlib_assert(ref_inode != inode);
257 if (ref_inode->i_num_ads != inode->i_num_ads &&
258 inode->i_num_ads != 0)
260 if (ref_inode->i_security_id != inode->i_security_id
261 || ref_inode->i_attributes != inode->i_attributes)
263 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
264 const u8 *ref_hash, *hash;
265 ref_hash = inode_stream_hash(ref_inode, i);
266 hash = inode_stream_hash(inode, i);
267 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
269 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
270 &inode->i_ads_entries[i - 1]))
276 /* Fix up a "true" inode and check for inconsistencies */
278 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
280 struct wim_dentry *dentry;
281 struct wim_dentry *ref_dentry = NULL;
282 struct wim_inode *ref_inode;
287 inode_for_each_dentry(dentry, inode) {
288 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
290 if (dentry->d_inode->i_creation_time > last_ctime)
291 last_ctime = dentry->d_inode->i_creation_time;
292 if (dentry->d_inode->i_last_write_time > last_mtime)
293 last_mtime = dentry->d_inode->i_last_write_time;
294 if (dentry->d_inode->i_last_access_time > last_atime)
295 last_atime = dentry->d_inode->i_last_access_time;
298 ref_inode = ref_dentry->d_inode;
299 ref_inode->i_nlink = 1;
300 list_add_tail(&ref_inode->i_list, inode_list);
302 list_del(&inode->i_dentry);
303 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
305 inode_for_each_dentry(dentry, ref_inode) {
306 if (dentry != ref_dentry) {
307 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
308 inconsistent_inode(ref_inode);
309 return WIMLIB_ERR_INVALID_DENTRY;
311 /* Free the unneeded `struct wim_inode'. */
312 free_inode(dentry->d_inode);
313 dentry->d_inode = ref_inode;
314 ref_inode->i_nlink++;
317 ref_inode->i_creation_time = last_ctime;
318 ref_inode->i_last_write_time = last_mtime;
319 ref_inode->i_last_access_time = last_atime;
320 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
325 * Fixes up a nominal inode.
327 * By a nominal inode we mean a group of two or more dentries that share the
328 * same hard link group ID.
330 * If dentries in the inode are found to be inconsistent, we may split the inode
331 * into several "true" inodes.
333 * After splitting up each nominal inode into the "true" inodes we will
334 * canonicalize the link group by getting rid of all the unnecessary `struct
335 * wim_inode's. There will be just one `struct wim_inode' for each hard link
339 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
340 bool *ino_changes_needed)
342 struct wim_dentry *dentry;
343 struct hlist_node *cur, *tmp;
345 size_t num_true_inodes;
347 wimlib_assert(inode->i_nlink == inode_link_count(inode));
349 if (inode->i_nlink > 1 &&
350 (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY))
352 ERROR("Found unsupported directory hard link!");
353 return WIMLIB_ERR_INVALID_DENTRY;
356 LIST_HEAD(dentries_with_data_streams);
357 LIST_HEAD(dentries_with_no_data_streams);
358 HLIST_HEAD(true_inodes);
360 /* Create a list of dentries in the nominal inode that have at
361 * least one data stream with a non-zero hash, and another list that
362 * contains the dentries that have a zero hash for all data streams. */
363 inode_for_each_dentry(dentry, inode) {
364 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
366 hash = inode_stream_hash(dentry->d_inode, i);
367 if (!is_zero_hash(hash)) {
368 list_add(&dentry->tmp_list,
369 &dentries_with_data_streams);
373 list_add(&dentry->tmp_list,
374 &dentries_with_no_data_streams);
379 /* If there are no dentries with data streams, we require the nominal
380 * inode to be a true inode */
381 if (list_empty(&dentries_with_data_streams)) {
383 if (inode->i_nlink > 1) {
384 DEBUG("Found link group of size %u without "
385 "any data streams:", inode->i_nlink);
386 print_inode_dentries(inode);
387 DEBUG("We are going to interpret it as true "
388 "link group, provided that the dentries "
392 return fix_true_inode(inode, inode_list);
395 /* One or more dentries had data streams specified. We check each of
396 * these dentries for consistency with the others to form a set of true
399 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
400 /* Look for a true inode that is consistent with this dentry and
401 * add this dentry to it. Or, if none of the true inodes are
402 * consistent with this dentry, add a new one (if that happens,
403 * we have split the hard link group). */
404 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
405 if (ref_inodes_consistent(inode, dentry->d_inode)) {
406 inode_add_dentry(dentry, inode);
411 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
412 inode_add_dentry(dentry, dentry->d_inode);
413 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
418 wimlib_assert(num_true_inodes != 0);
420 /* If there were dentries with no data streams, we require there to only
421 * be one true inode so that we know which inode to assign the
422 * streamless dentries to. */
423 if (!list_empty(&dentries_with_no_data_streams)) {
424 if (num_true_inodes != 1) {
425 ERROR("Hard inode ambiguity detected!");
426 ERROR("We split up inode 0x%"PRIx64" due to "
427 "inconsistencies,", inode->i_ino);
428 ERROR("but dentries with no stream information remained. "
429 "We don't know which inode");
430 ERROR("to assign them to.");
431 return WIMLIB_ERR_INVALID_DENTRY;
433 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
434 /* Assign the streamless dentries to the one and only true
436 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
437 inode_add_dentry(dentry, inode);
439 if (num_true_inodes != 1) {
441 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
443 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
444 "inodes:\n"), inode->i_ino, num_true_inodes);
445 tputs(T("----------------------------------------------------"
446 "--------------------------"));
448 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
449 tprintf(T("[Split inode %zu]\n"), i++);
450 print_inode_dentries(inode);
453 tputs(T("----------------------------------------------------"
454 "--------------------------"));
456 *ino_changes_needed = true;
459 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
460 ret = fix_true_inode(inode, inode_list);
468 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
469 bool *ino_changes_needed)
471 struct wim_inode *inode;
472 struct hlist_node *cur, *tmp;
474 INIT_LIST_HEAD(inode_list);
475 for (u64 i = 0; i < table->capacity; i++) {
476 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
477 INIT_LIST_HEAD(&inode->i_list);
478 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
483 list_splice_tail(&table->extra_inodes, inode_list);
488 * dentry_tree_fix_inodes():
490 * This function takes as input a tree of WIM dentries that initially has a
491 * different inode associated with each dentry. Sets of dentries that should
492 * share the same inode (a.k.a. hard link groups) are built using the i_ino
493 * field of each inode, then the link count and alias list for one inode in each
494 * set is set correctly and the unnecessary struct wim_inode's freed. The
495 * effect is to correctly associate exactly one struct wim_inode with each
496 * original inode, regardless of how many dentries are aliases for that inode.
498 * The special inode number of 0 indicates that the dentry is in a hard link
499 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
502 * This function also checks the dentries in each hard link group for
503 * consistency. In some WIMs, such as install.wim for some versions of Windows
504 * 7, dentries can share the same hard link group ID but not actually be hard
505 * linked to each other (based on conflicting information, such as file
506 * contents). This should be an error, but this case needs be handled. So,
507 * each "nominal" inode (the inode based on the inode numbers provided in the
508 * WIM) is examined for consistency and may be split into multiple "true" inodes
509 * that are maximally sized consistent sets of dentries.
511 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
512 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
513 * is returned in the hlist @inode_list.
516 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
518 struct wim_inode_table inode_tab;
520 bool ino_changes_needed;
522 DEBUG("Inserting dentries into inode table");
523 ret = init_inode_table(&inode_tab, 9001);
527 for_dentry_in_tree(root, inode_table_insert, &inode_tab);
529 DEBUG("Cleaning up the hard link groups");
530 ino_changes_needed = false;
531 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
532 destroy_inode_table(&inode_tab);
534 if (ret == 0 && ino_changes_needed) {
536 struct wim_inode *inode;
538 WARNING("Re-assigning inode numbers due to inode inconsistencies");
539 list_for_each_entry(inode, inode_list, i_list) {
540 if (inode->i_nlink > 1)
541 inode->i_ino = cur_ino++;
549 /* Assign consecutive inode numbers to the inodes in the inode table, and move
550 * the inodes to a single list @head. */
552 inode_table_prepare_inode_list(struct wim_inode_table *table,
553 struct list_head *head)
555 struct wim_inode *inode;
556 struct hlist_node *cur, *tmp;
559 list_for_each_entry(inode, head, i_list) {
560 if (inode->i_nlink > 1)
561 inode->i_ino = cur_ino++;
566 for (size_t i = 0; i < table->capacity; i++) {
567 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
569 if (inode->i_nlink > 1)
570 inode->i_ino = cur_ino++;
573 list_add_tail(&inode->i_list, head);
575 INIT_HLIST_HEAD(&table->array[i]);
577 list_splice_tail(&table->extra_inodes, head);
578 table->num_entries = 0;