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, bool noshare,
172 struct wim_dentry **dentry_ret)
174 struct wim_dentry *dentry;
175 struct wim_inode *inode;
179 ret = new_dentry_with_timeless_inode(name, &dentry);
182 list_add_tail(&dentry->d_inode->i_list, &table->extra_inodes);
184 ret = new_dentry(name, &dentry);
187 inode = inode_table_get_inode(table, ino, devno);
190 return WIMLIB_ERR_NOMEM;
192 if (inode->i_nlink > 1)
193 inode_ref_streams(inode);
194 dentry->d_inode = inode;
195 inode_add_dentry(dentry, inode);
197 *dentry_ret = dentry;
201 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
203 print_inode_dentries(const struct wim_inode *inode)
205 struct wim_dentry *dentry;
206 inode_for_each_dentry(dentry, inode)
207 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
212 inconsistent_inode(const struct wim_inode *inode)
214 #ifdef ENABLE_ERROR_MESSAGES
215 ERROR("An inconsistent hard link group that cannot be corrected has "
217 ERROR("The dentries are located at the following paths:");
218 print_inode_dentries(inode);
223 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
224 const struct wim_inode * restrict ref_inode_2)
226 wimlib_assert(ref_inode_1 != ref_inode_2);
228 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
230 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
231 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
233 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
234 const u8 *ref_1_hash, *ref_2_hash;
235 ref_1_hash = inode_stream_hash(ref_inode_1, i);
236 ref_2_hash = inode_stream_hash(ref_inode_2, i);
237 if (!hashes_equal(ref_1_hash, ref_2_hash))
239 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
240 &ref_inode_2->i_ads_entries[i - 1]))
248 inodes_consistent(const struct wim_inode * restrict ref_inode,
249 const struct wim_inode * restrict inode)
251 wimlib_assert(ref_inode != inode);
253 if (ref_inode->i_num_ads != inode->i_num_ads &&
254 inode->i_num_ads != 0)
256 if (ref_inode->i_security_id != inode->i_security_id
257 || ref_inode->i_attributes != inode->i_attributes)
259 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
260 const u8 *ref_hash, *hash;
261 ref_hash = inode_stream_hash(ref_inode, i);
262 hash = inode_stream_hash(inode, i);
263 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
265 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
266 &inode->i_ads_entries[i - 1]))
272 /* Fix up a "true" inode and check for inconsistencies */
274 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
276 struct wim_dentry *dentry;
277 struct wim_dentry *ref_dentry = NULL;
278 struct wim_inode *ref_inode;
283 inode_for_each_dentry(dentry, inode) {
284 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
286 if (dentry->d_inode->i_creation_time > last_ctime)
287 last_ctime = dentry->d_inode->i_creation_time;
288 if (dentry->d_inode->i_last_write_time > last_mtime)
289 last_mtime = dentry->d_inode->i_last_write_time;
290 if (dentry->d_inode->i_last_access_time > last_atime)
291 last_atime = dentry->d_inode->i_last_access_time;
294 ref_inode = ref_dentry->d_inode;
295 ref_inode->i_nlink = 1;
296 list_add_tail(&ref_inode->i_list, inode_list);
298 list_del(&inode->i_dentry);
299 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
301 inode_for_each_dentry(dentry, ref_inode) {
302 if (dentry != ref_dentry) {
303 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
304 inconsistent_inode(ref_inode);
305 return WIMLIB_ERR_INVALID_DENTRY;
307 /* Free the unneeded `struct wim_inode'. */
308 free_inode(dentry->d_inode);
309 dentry->d_inode = ref_inode;
310 ref_inode->i_nlink++;
313 ref_inode->i_creation_time = last_ctime;
314 ref_inode->i_last_write_time = last_mtime;
315 ref_inode->i_last_access_time = last_atime;
316 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
321 * Fixes up a nominal inode.
323 * By a nominal inode we mean a group of two or more dentries that share the
324 * same hard link group ID.
326 * If dentries in the inode are found to be inconsistent, we may split the inode
327 * into several "true" inodes.
329 * After splitting up each nominal inode into the "true" inodes we will
330 * canonicalize the link group by getting rid of all the unnecessary `struct
331 * wim_inode's. There will be just one `struct wim_inode' for each hard link
335 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
336 bool *ino_changes_needed)
338 struct wim_dentry *dentry;
339 struct hlist_node *cur, *tmp;
341 size_t num_true_inodes;
343 wimlib_assert(inode->i_nlink == inode_link_count(inode));
345 LIST_HEAD(dentries_with_data_streams);
346 LIST_HEAD(dentries_with_no_data_streams);
347 HLIST_HEAD(true_inodes);
349 /* Create a list of dentries in the nominal inode that have at
350 * least one data stream with a non-zero hash, and another list that
351 * contains the dentries that have a zero hash for all data streams. */
352 inode_for_each_dentry(dentry, inode) {
353 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
355 hash = inode_stream_hash(dentry->d_inode, i);
356 if (!is_zero_hash(hash)) {
357 list_add(&dentry->tmp_list,
358 &dentries_with_data_streams);
362 list_add(&dentry->tmp_list,
363 &dentries_with_no_data_streams);
368 /* If there are no dentries with data streams, we require the nominal
369 * inode to be a true inode */
370 if (list_empty(&dentries_with_data_streams)) {
372 if (inode->i_nlink > 1) {
373 DEBUG("Found link group of size %u without "
374 "any data streams:", inode->i_nlink);
375 print_inode_dentries(inode);
376 DEBUG("We are going to interpret it as true "
377 "link group, provided that the dentries "
381 return fix_true_inode(inode, inode_list);
384 /* One or more dentries had data streams specified. We check each of
385 * these dentries for consistency with the others to form a set of true
388 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
389 /* Look for a true inode that is consistent with this dentry and
390 * add this dentry to it. Or, if none of the true inodes are
391 * consistent with this dentry, add a new one (if that happens,
392 * we have split the hard link group). */
393 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
394 if (ref_inodes_consistent(inode, dentry->d_inode)) {
395 inode_add_dentry(dentry, inode);
400 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
401 inode_add_dentry(dentry, dentry->d_inode);
402 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
407 wimlib_assert(num_true_inodes != 0);
409 /* If there were dentries with no data streams, we require there to only
410 * be one true inode so that we know which inode to assign the
411 * streamless dentries to. */
412 if (!list_empty(&dentries_with_no_data_streams)) {
413 if (num_true_inodes != 1) {
414 ERROR("Hard inode ambiguity detected!");
415 ERROR("We split up inode 0x%"PRIx64" due to "
416 "inconsistencies,", inode->i_ino);
417 ERROR("but dentries with no stream information remained. "
418 "We don't know which inode");
419 ERROR("to assign them to.");
420 return WIMLIB_ERR_INVALID_DENTRY;
422 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
423 /* Assign the streamless dentries to the one and only true
425 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
426 inode_add_dentry(dentry, inode);
428 if (num_true_inodes != 1) {
430 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
432 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
433 "inodes:\n"), inode->i_ino, num_true_inodes);
434 tputs(T("----------------------------------------------------"
435 "--------------------------"));
437 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
438 tprintf(T("[Split inode %zu]\n"), i++);
439 print_inode_dentries(inode);
442 tputs(T("----------------------------------------------------"
443 "--------------------------"));
445 *ino_changes_needed = true;
448 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
449 ret = fix_true_inode(inode, inode_list);
457 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
458 bool *ino_changes_needed)
460 struct wim_inode *inode;
461 struct hlist_node *cur, *tmp;
463 INIT_LIST_HEAD(inode_list);
464 for (u64 i = 0; i < table->capacity; i++) {
465 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
466 INIT_LIST_HEAD(&inode->i_list);
467 ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
472 list_splice_tail(&table->extra_inodes, inode_list);
477 * dentry_tree_fix_inodes():
479 * This function takes as input a tree of WIM dentries that initially has a
480 * different inode associated with each dentry. Sets of dentries that should
481 * share the same inode (a.k.a. hard link groups) are built using the i_ino
482 * field of each inode, then the link count and alias list for one inode in each
483 * set is set correctly and the unnecessary struct wim_inode's freed. The
484 * effect is to correctly associate exactly one struct wim_inode with each
485 * original inode, regardless of how many dentries are aliases for that inode.
487 * The special inode number of 0 indicates that the dentry is in a hard link
488 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
491 * This function also checks the dentries in each hard link group for
492 * consistency. In some WIMs, such as install.wim for some versions of Windows
493 * 7, dentries can share the same hard link group ID but not actually be hard
494 * linked to each other (based on conflicting information, such as file
495 * contents). This should be an error, but this case needs be handled. So,
496 * each "nominal" inode (the inode based on the inode numbers provided in the
497 * WIM) is examined for consistency and may be split into multiple "true" inodes
498 * that are maximally sized consistent sets of dentries.
500 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
501 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
502 * is returned in the hlist @inode_list.
505 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
507 struct wim_inode_table inode_tab;
509 bool ino_changes_needed;
511 DEBUG("Inserting dentries into inode table");
512 ret = init_inode_table(&inode_tab, 9001);
516 for_dentry_in_tree(root, inode_table_insert, &inode_tab);
518 DEBUG("Cleaning up the hard link groups");
519 ino_changes_needed = false;
520 ret = fix_inodes(&inode_tab, inode_list, &ino_changes_needed);
521 destroy_inode_table(&inode_tab);
523 if (ret == 0 && ino_changes_needed) {
525 struct wim_inode *inode;
527 WARNING("Re-assigning inode numbers due to inode inconsistencies");
528 list_for_each_entry(inode, inode_list, i_list) {
529 if (inode->i_nlink > 1)
530 inode->i_ino = cur_ino++;
538 /* Assign consecutive inode numbers to the inodes in the inode table, and move
539 * the inodes to a single list @head. */
541 inode_table_prepare_inode_list(struct wim_inode_table *table,
542 struct list_head *head)
544 struct wim_inode *inode;
545 struct hlist_node *cur, *tmp;
548 list_for_each_entry(inode, head, i_list) {
549 if (inode->i_nlink > 1)
550 inode->i_ino = cur_ino++;
553 list_add_tail(&inode->i_list, head);
556 for (size_t i = 0; i < table->capacity; i++) {
557 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
559 if (inode->i_nlink > 1)
560 inode->i_ino = cur_ino++;
563 list_add_tail(&inode->i_list, head);
565 INIT_HLIST_HEAD(&table->array[i]);
567 list_splice_tail(&table->extra_inodes, head);
568 table->num_entries = 0;