4 * Code to deal with hard links in WIMs.
8 * Copyright (C) 2012, 2013 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;
75 static inline void destroy_inode_table(struct wim_inode_table *table)
80 static int init_inode_table(struct wim_inode_table *table, size_t capacity)
82 table->array = CALLOC(capacity, sizeof(table->array[0]));
84 ERROR("Cannot initalize inode table: out of memory");
85 return WIMLIB_ERR_NOMEM;
87 table->num_entries = 0;
88 table->capacity = capacity;
89 INIT_HLIST_HEAD(&table->extra_inodes);
93 static inline size_t inode_link_count(const struct wim_inode *inode)
95 const struct list_head *cur;
97 list_for_each(cur, &inode->i_dentry)
102 /* Insert a dentry into the inode table based on the inode number of the
103 * attached inode (which came from the hard link group ID field of the on-disk
105 static int inode_table_insert(struct wim_dentry *dentry, void *__table)
107 struct wim_inode_table *table = __table;
108 struct wim_inode *d_inode = dentry->d_inode;
110 if (d_inode->i_ino == 0) {
111 /* A dentry with a hard link group ID of 0 indicates that it's
112 * in a hard link group by itself. Add it to the list of extra
113 * inodes rather than inserting it into the hash lists. */
114 hlist_add_head(&d_inode->i_hlist, &table->extra_inodes);
116 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
117 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
118 wimlib_assert(d_inode->i_nlink == 1);
121 struct wim_inode *inode;
122 struct hlist_node *cur;
124 /* Try adding this dentry to an existing inode */
125 pos = d_inode->i_ino % table->capacity;
126 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
127 if (inode->i_ino == d_inode->i_ino) {
128 inode_add_dentry(dentry, inode);
134 /* No inode in the table has the same number as this one, so add
135 * it to the table. */
136 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
138 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
139 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
140 wimlib_assert(d_inode->i_nlink == 1);
142 /* XXX Make the table grow when too many entries have been
144 table->num_entries++;
149 static void print_inode_dentries(const struct wim_inode *inode)
151 struct wim_dentry *dentry;
152 inode_for_each_dentry(dentry, inode)
153 printf("`%s'\n", dentry->full_path_utf8);
156 static void inconsistent_inode(const struct wim_inode *inode)
158 ERROR("An inconsistent hard link group that cannot be corrected has "
160 ERROR("The dentries are located at the following paths:");
161 #ifdef ENABLE_ERROR_MESSAGES
162 print_inode_dentries(inode);
166 static bool ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
167 const struct wim_inode * restrict ref_inode_2)
169 wimlib_assert(ref_inode_1 != ref_inode_2);
171 if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
173 if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
174 || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
176 for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
177 const u8 *ref_1_hash, *ref_2_hash;
178 ref_1_hash = inode_stream_hash(ref_inode_1, i);
179 ref_2_hash = inode_stream_hash(ref_inode_2, i);
180 if (!hashes_equal(ref_1_hash, ref_2_hash))
182 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
183 &ref_inode_2->i_ads_entries[i - 1]))
190 static bool inodes_consistent(const struct wim_inode * restrict ref_inode,
191 const struct wim_inode * restrict inode)
193 wimlib_assert(ref_inode != inode);
195 if (ref_inode->i_num_ads != inode->i_num_ads &&
196 inode->i_num_ads != 0)
198 if (ref_inode->i_security_id != inode->i_security_id
199 || ref_inode->i_attributes != inode->i_attributes)
201 for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
202 const u8 *ref_hash, *hash;
203 ref_hash = inode_stream_hash(ref_inode, i);
204 hash = inode_stream_hash(inode, i);
205 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
207 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
208 &inode->i_ads_entries[i - 1]))
214 /* Fix up a "true" inode and check for inconsistencies */
215 static int fix_true_inode(struct wim_inode *inode, struct hlist_head *inode_list)
217 struct wim_dentry *dentry;
218 struct wim_dentry *ref_dentry = NULL;
219 struct wim_inode *ref_inode;
224 inode_for_each_dentry(dentry, inode) {
225 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
227 if (dentry->d_inode->i_creation_time > last_ctime)
228 last_ctime = dentry->d_inode->i_creation_time;
229 if (dentry->d_inode->i_last_write_time > last_mtime)
230 last_mtime = dentry->d_inode->i_last_write_time;
231 if (dentry->d_inode->i_last_access_time > last_atime)
232 last_atime = dentry->d_inode->i_last_access_time;
235 ref_inode = ref_dentry->d_inode;
236 ref_inode->i_nlink = 1;
237 hlist_add_head(&ref_inode->i_hlist, inode_list);
239 list_del(&inode->i_dentry);
240 list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
242 inode_for_each_dentry(dentry, ref_inode) {
243 if (dentry != ref_dentry) {
244 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
245 inconsistent_inode(ref_inode);
246 return WIMLIB_ERR_INVALID_DENTRY;
248 /* Free the unneeded `struct wim_inode'. */
249 dentry->d_inode->i_hlist.next = NULL;
250 dentry->d_inode->i_hlist.pprev = NULL;
251 free_inode(dentry->d_inode);
252 dentry->d_inode = ref_inode;
253 ref_inode->i_nlink++;
256 ref_inode->i_creation_time = last_ctime;
257 ref_inode->i_last_write_time = last_mtime;
258 ref_inode->i_last_access_time = last_atime;
259 wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
264 * Fixes up a nominal inode.
266 * By a nominal inode we mean a group of two or more dentries that share the
267 * same hard link group ID.
269 * If dentries in the inode are found to be inconsistent, we may split the inode
270 * into several "true" inodes.
272 * After splitting up each nominal inode into the "true" inodes we will
273 * canonicalize the link group by getting rid of all the unnecessary `struct
274 * wim_inode's. There will be just one `struct wim_inode' for each hard link
277 static int fix_nominal_inode(struct wim_inode *inode,
278 struct hlist_head *inode_list)
280 struct wim_dentry *dentry;
281 struct hlist_node *cur, *tmp;
283 size_t num_true_inodes;
285 wimlib_assert(inode->i_nlink == inode_link_count(inode));
287 LIST_HEAD(dentries_with_data_streams);
288 LIST_HEAD(dentries_with_no_data_streams);
289 HLIST_HEAD(true_inodes);
291 /* Create a list of dentries in the nominal inode that have at
292 * least one data stream with a non-zero hash, and another list that
293 * contains the dentries that have a zero hash for all data streams. */
294 inode_for_each_dentry(dentry, inode) {
295 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
297 hash = inode_stream_hash(dentry->d_inode, i);
298 if (!is_zero_hash(hash)) {
299 list_add(&dentry->tmp_list,
300 &dentries_with_data_streams);
304 list_add(&dentry->tmp_list,
305 &dentries_with_no_data_streams);
310 /* If there are no dentries with data streams, we require the nominal
311 * inode to be a true inode */
312 if (list_empty(&dentries_with_data_streams)) {
314 if (inode->i_nlink > 1) {
315 DEBUG("Found link group of size %u without "
316 "any data streams:", inode->i_nlink);
317 print_inode_dentries(inode);
318 DEBUG("We are going to interpret it as true "
319 "link group, provided that the dentries "
323 return fix_true_inode(inode, inode_list);
326 /* One or more dentries had data streams specified. We check each of
327 * these dentries for consistency with the others to form a set of true
330 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
331 /* Look for a true inode that is consistent with this dentry and
332 * add this dentry to it. Or, if none of the true inodes are
333 * consistent with this dentry, add a new one (if that happens,
334 * we have split the hard link group). */
335 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
336 if (ref_inodes_consistent(inode, dentry->d_inode)) {
337 inode_add_dentry(dentry, inode);
342 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
343 inode_add_dentry(dentry, dentry->d_inode);
344 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
349 wimlib_assert(num_true_inodes != 0);
351 /* If there were dentries with no data streams, we require there to only
352 * be one true inode so that we know which inode to assign the
353 * streamless dentries to. */
354 if (!list_empty(&dentries_with_no_data_streams)) {
355 if (num_true_inodes != 1) {
356 ERROR("Hard inode ambiguity detected!");
357 ERROR("We split up inode 0x%"PRIx64" due to "
358 "inconsistencies,", inode->i_ino);
359 ERROR("but dentries with no stream information remained. "
360 "We don't know which inode");
361 ERROR("to assign them to.");
362 return WIMLIB_ERR_INVALID_DENTRY;
364 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
365 /* Assign the streamless dentries to the one and only true
367 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
368 inode_add_dentry(dentry, inode);
371 if (num_true_inodes != 1) {
372 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
374 printf("Split nominal inode 0x%"PRIx64" into %zu "
376 inode->i_ino, num_true_inodes);
377 puts("------------------------------------------------------------------------------");
379 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
380 printf("[Split inode %zu]\n", i++);
381 print_inode_dentries(inode);
384 puts("------------------------------------------------------------------------------");
388 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
389 ret = fix_true_inode(inode, inode_list);
396 static int fix_inodes(struct wim_inode_table *table,
397 struct hlist_head *inode_list)
399 struct wim_inode *inode;
400 struct hlist_node *cur, *tmp;
402 INIT_HLIST_HEAD(inode_list);
403 for (u64 i = 0; i < table->capacity; i++) {
404 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
405 ret = fix_nominal_inode(inode, inode_list);
410 hlist_for_each_safe(cur, tmp, &table->extra_inodes)
411 hlist_add_head(cur, inode_list);
416 * dentry_tree_fix_inodes():
418 * This function takes as input a tree of WIM dentries that initially has a
419 * different inode associated with each dentry. Sets of dentries that should
420 * share the same inode (a.k.a. hard link groups) are built using the i_ino
421 * field of each inode, then the link count and alias list for one inode in each
422 * set is set correctly and the unnecessary struct wim_inode's freed. The
423 * effect is to correctly associate exactly one struct wim_inode with each
424 * original inode, regardless of how many dentries are aliases for that inode.
426 * The special inode number of 0 indicates that the dentry is in a hard link
427 * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
430 * This function also checks the dentries in each hard link group for
431 * consistency. In some WIMs, such as install.wim for some versions of Windows
432 * 7, dentries can share the same hard link group ID but not actually be hard
433 * linked to each other (based on conflicting information, such as file
434 * contents). This should be an error, but this case needs be handled. So,
435 * each "nominal" inode (the inode based on the inode numbers provided in the
436 * WIM) is examined for consistency and may be split into multiple "true" inodes
437 * that are maximally sized consistent sets of dentries.
439 * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
440 * failure. On success, the list of "true" inodes, linked by the i_hlist field,
441 * is returned in the hlist @inode_list.
443 int dentry_tree_fix_inodes(struct wim_dentry *root, struct hlist_head *inode_list)
445 struct wim_inode_table inode_tab;
448 DEBUG("Inserting dentries into inode table");
449 ret = init_inode_table(&inode_tab, 9001);
453 for_dentry_in_tree(root, inode_table_insert, &inode_tab);
455 DEBUG("Cleaning up the hard link groups");
456 ret = fix_inodes(&inode_tab, inode_list);
457 destroy_inode_table(&inode_tab);
461 /* Assign inode numbers to a list of inodes and return the next available
463 u64 assign_inode_numbers(struct hlist_head *inode_list)
465 DEBUG("Assigning inode numbers");
466 struct wim_inode *inode;
467 struct hlist_node *cur;
469 hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
470 inode->i_ino = cur_ino;