4 * Code to deal with hard links in WIMs. Essentially, the WIM dentries are put
5 * into a hash table indexed by the inode ID field, then for each hard
6 * inode, a linked list is made to connect the dentries.
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
31 #include "lookup_table.h"
36 * / \ ----------- -----------
37 * | dentry<---| struct | | struct |---> dentry
38 * \ / | inode | | inode |
39 * dentry ------------ ------------
43 * ----------- ----------- / \
44 * dentry<---| struct | | struct |---> dentry dentry
45 * / | inode | | inode | \ /
46 * dentry ------------ ------------ dentry
50 * inode_table->array | idx 0 | idx 1 |
55 int init_inode_table(struct inode_table *table, size_t capacity)
57 table->array = CALLOC(capacity, sizeof(table->array[0]));
59 ERROR("Cannot initalize inode table: out of memory");
60 return WIMLIB_ERR_NOMEM;
62 table->num_entries = 0;
63 table->capacity = capacity;
64 INIT_HLIST_HEAD(&table->extra_inodes);
69 static size_t inode_link_count(const struct inode *inode)
71 const struct list_head *cur;
73 list_for_each(cur, &inode->dentry_list)
79 * Insert a dentry into the inode table based on its inode
82 * If there is already a dentry in the table having the same inode ID,
83 * and the inode ID is not 0, the dentry is added to the circular
84 * linked list for that inode.
86 * If the inode ID is 0, this indicates a dentry that's in a hard link
87 * inode by itself (has a link count of 1). We can't insert it into the hash
88 * table itself because we don't know what inode numbers are available to
89 * give it (this could be kept track of but would be more difficult). Instead
90 * we keep a linked list of the single dentries, and assign them inode
93 int inode_table_insert(struct dentry *dentry, void *__table)
95 struct inode_table *table = __table;
96 struct inode *d_inode = dentry->d_inode;
98 if (d_inode->ino == 0) {
99 /* Single inode--- Add to the list of extra inodes (we can't put
100 * it in the table itself because all the singles have a link
102 hlist_add_head(&d_inode->hlist, &table->extra_inodes);
104 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
105 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
106 wimlib_assert(d_inode->link_count == 1);
108 /* Inode that may have multiple corresponding dentries (the code
109 * will work even if the inode actually contains only 1 dentry
114 struct hlist_node *cur;
116 /* Try adding to existing inode */
117 pos = d_inode->ino % table->capacity;
118 hlist_for_each_entry(inode, cur, &table->array[pos], hlist) {
119 if (inode->ino == d_inode->ino) {
120 inode_add_dentry(dentry, inode);
126 /* Add new inode to the table */
127 hlist_add_head(&d_inode->hlist, &table->array[pos]);
129 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
130 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
131 wimlib_assert(d_inode->link_count == 1);
133 /* XXX Make the table grow when too many entries have been
135 table->num_entries++;
140 /* Assign the inode numbers to dentries in a inode table, and return the
141 * next available inode ID. */
142 u64 assign_inode_numbers(struct hlist_head *inode_list)
144 DEBUG("Assigning inode numbers");
146 struct hlist_node *cur;
148 hlist_for_each_entry(inode, cur, inode_list, hlist) {
149 inode->ino = cur_ino;
156 static void print_inode_dentries(const struct inode *inode)
158 struct dentry *dentry;
159 inode_for_each_dentry(dentry, inode)
160 printf("`%s'\n", dentry->full_path_utf8);
163 static void inconsistent_inode(const struct inode *inode)
165 ERROR("An inconsistent hard link group that we cannot correct has been "
167 ERROR("The dentries are located at the following paths:");
168 print_inode_dentries(inode);
171 static bool ref_inodes_consistent(const struct inode * restrict ref_inode_1,
172 const struct inode * restrict ref_inode_2)
174 wimlib_assert(ref_inode_1 != ref_inode_2);
176 if (ref_inode_1->num_ads != ref_inode_2->num_ads)
178 if (ref_inode_1->security_id != ref_inode_2->security_id
179 || ref_inode_1->attributes != ref_inode_2->attributes)
181 for (unsigned i = 0; i <= ref_inode_1->num_ads; i++) {
182 const u8 *ref_1_hash, *ref_2_hash;
183 ref_1_hash = inode_stream_hash(ref_inode_1, i);
184 ref_2_hash = inode_stream_hash(ref_inode_2, i);
185 if (!hashes_equal(ref_1_hash, ref_2_hash))
187 if (i && !ads_entries_have_same_name(&ref_inode_1->ads_entries[i - 1],
188 &ref_inode_2->ads_entries[i - 1]))
195 static bool inodes_consistent(const struct inode * restrict ref_inode,
196 const struct inode * restrict inode)
198 wimlib_assert(ref_inode != inode);
200 if (ref_inode->num_ads != inode->num_ads &&
203 if (ref_inode->security_id != inode->security_id
204 || ref_inode->attributes != inode->attributes)
206 for (unsigned i = 0; i <= min(ref_inode->num_ads, inode->num_ads); i++) {
207 const u8 *ref_hash, *hash;
208 ref_hash = inode_stream_hash(ref_inode, i);
209 hash = inode_stream_hash(inode, i);
210 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
212 if (i && !ads_entries_have_same_name(&ref_inode->ads_entries[i - 1],
213 &inode->ads_entries[i - 1]))
219 /* Fix up a "true" inode and check for inconsistencies */
220 static int fix_true_inode(struct inode *inode, struct hlist_head *inode_list)
222 struct dentry *dentry;
223 struct dentry *ref_dentry = NULL;
224 struct inode *ref_inode;
229 inode_for_each_dentry(dentry, inode) {
230 if (!ref_dentry || dentry->d_inode->num_ads > ref_dentry->d_inode->num_ads)
232 if (dentry->d_inode->creation_time > last_ctime)
233 last_ctime = dentry->d_inode->creation_time;
234 if (dentry->d_inode->last_write_time > last_mtime)
235 last_mtime = dentry->d_inode->last_write_time;
236 if (dentry->d_inode->last_access_time > last_atime)
237 last_atime = dentry->d_inode->last_access_time;
240 ref_inode = ref_dentry->d_inode;
241 ref_inode->link_count = 1;
242 hlist_add_head(&ref_inode->hlist, inode_list);
244 list_del(&inode->dentry_list);
245 list_add(&ref_inode->dentry_list, &ref_dentry->inode_dentry_list);
247 inode_for_each_dentry(dentry, ref_inode) {
248 if (dentry != ref_dentry) {
249 if (!inodes_consistent(ref_inode, dentry->d_inode)) {
250 inconsistent_inode(ref_inode);
251 return WIMLIB_ERR_INVALID_DENTRY;
253 /* Free the unneeded `struct inode'. */
254 free_inode(dentry->d_inode);
255 dentry->d_inode = ref_inode;
256 ref_inode->link_count++;
259 ref_inode->creation_time = last_ctime;
260 ref_inode->last_write_time = last_mtime;
261 ref_inode->last_access_time = last_atime;
262 wimlib_assert(inode_link_count(ref_inode) == ref_inode->link_count);
267 * Fixes up a nominal inode.
269 * By a nominal inode we mean a group of two or more dentries that share
270 * the same hard link group ID.
272 * If dentries in the inode are found to be inconsistent, we may split the inode
273 * into several "true" inodes.
275 * After splitting up each nominal inode into the "true" inodes we will
276 * canonicalize the link group by getting rid of all the unnecessary `struct
277 * inodes'. There will be just one `struct inode' for each hard link group
281 fix_nominal_inode(struct inode *inode, struct hlist_head *inode_list)
283 struct dentry *dentry;
284 struct hlist_node *cur, *tmp;
286 size_t num_true_inodes;
288 wimlib_assert(inode->link_count == inode_link_count(inode));
290 LIST_HEAD(dentries_with_data_streams);
291 LIST_HEAD(dentries_with_no_data_streams);
292 HLIST_HEAD(true_inodes);
294 /* Create a list of dentries in the nominal inode that have at
295 * least one data stream with a non-zero hash, and another list that
296 * contains the dentries that have a zero hash for all data streams. */
297 inode_for_each_dentry(dentry, inode) {
298 for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
300 hash = inode_stream_hash(dentry->d_inode, i);
301 if (!is_zero_hash(hash)) {
302 list_add(&dentry->tmp_list,
303 &dentries_with_data_streams);
307 list_add(&dentry->tmp_list,
308 &dentries_with_no_data_streams);
313 /* If there are no dentries with data streams, we require the nominal
314 * inode to be a true inode */
315 if (list_empty(&dentries_with_data_streams)) {
317 if (inode->link_count > 1) {
318 DEBUG("Found link group of size %u without "
319 "any data streams:", inode->link_count);
320 print_inode_dentries(inode);
321 DEBUG("We are going to interpret it as true "
322 "link group, provided that the dentries "
326 return fix_true_inode(inode, inode_list);
329 /* One or more dentries had data streams specified. We check each of
330 * these dentries for consistency with the others to form a set of true
333 list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
334 /* Look for a true inode that is consistent with this dentry and
335 * add this dentry to it. Or, if none of the true inodes are
336 * consistent with this dentry, add a new one (if that happens,
337 * we have split the hard link group). */
338 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
339 if (ref_inodes_consistent(inode, dentry->d_inode)) {
340 inode_add_dentry(dentry, inode);
345 INIT_LIST_HEAD(&dentry->d_inode->dentry_list);
346 inode_add_dentry(dentry, dentry->d_inode);
347 hlist_add_head(&dentry->d_inode->hlist, &true_inodes);
352 wimlib_assert(num_true_inodes != 0);
354 /* If there were dentries with no data streams, we require there to only
355 * be one true inode so that we know which inode to assign the
356 * streamless dentries to. */
357 if (!list_empty(&dentries_with_no_data_streams)) {
358 if (num_true_inodes != 1) {
359 ERROR("Hard inode ambiguity detected!");
360 ERROR("We split up inode 0x%"PRIx64" due to "
361 "inconsistencies,", inode->ino);
362 ERROR("but dentries with no stream information remained. "
363 "We don't know which inode");
364 ERROR("to assign them to.");
365 return WIMLIB_ERR_INVALID_DENTRY;
367 inode = container_of(true_inodes.first, struct inode, hlist);
368 /* Assign the streamless dentries to the one and only true
370 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
371 inode_add_dentry(dentry, inode);
374 if (num_true_inodes != 1) {
375 inode = container_of(true_inodes.first, struct inode, hlist);
377 printf("Split nominal inode 0x%"PRIx64" into %zu "
379 inode->ino, num_true_inodes);
380 puts("------------------------------------------------------------------------------");
382 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
383 printf("[Split inode %zu]\n", i++);
384 print_inode_dentries(inode);
387 puts("------------------------------------------------------------------------------");
391 hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, hlist) {
392 ret = fix_true_inode(inode, inode_list);
400 * Goes through each hard link group (dentries sharing the same hard link group
401 * ID field) that's been inserted into the inode table and shares the `struct
402 * inode's among members of each hard link group.
404 * In the process, the dentries belonging to each inode are checked for
405 * consistency. If they contain data features that indicate they cannot really
406 * correspond to the same inode, this should be an error, but in reality this
407 * case needs to be handled, so we split the dentries into different inodes.
409 * After this function returns, the inodes are no longer in the inode table, and
410 * the inode table should be destroyed. A list of the inodes, including all
411 * split inodes as well as the inodes that were good before, is returned in the
414 int fix_inodes(struct inode_table *table, struct hlist_head *inode_list)
417 struct hlist_node *cur, *tmp;
419 INIT_HLIST_HEAD(inode_list);
420 for (u64 i = 0; i < table->capacity; i++) {
421 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], hlist) {
422 ret = fix_nominal_inode(inode, inode_list);
427 hlist_for_each_safe(cur, tmp, &table->extra_inodes)
428 hlist_add_head(cur, inode_list);