]> wimlib.net Git - wimlib/blob - src/hardlink.c
Fix when debugging enabled but error messages are not
[wimlib] / src / hardlink.c
1 /*
2  * hardlink.c
3  *
4  * Code to deal with hard links in WIMs.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
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)
15  * any later version.
16  *
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
20  * details.
21  *
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/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "dentry.h"
28 #include "list.h"
29 #include "lookup_table.h"
30
31 /*                             NULL        NULL
32  *                              ^           ^
33  *         dentry               |           |
34  *        /     \          -----------  -----------
35  *        |      dentry<---|  struct  | |  struct  |---> dentry
36  *        \     /          | wim_inode| | wim_inode|
37  *         dentry          ------------ ------------
38  *                              ^           ^
39  *                              |           |
40  *                              |           |                   dentry
41  *                         -----------  -----------            /      \
42  *               dentry<---|  struct  | |  struct  |---> dentry        dentry
43  *              /          | wim_inode| | wim_inode|           \      /
44  *         dentry          ------------ ------------            dentry
45  *                              ^           ^
46  *                              |           |
47  *                            -----------------
48  *    wim_inode_table->array  | idx 0 | idx 1 |
49  *                            -----------------
50  */
51
52 /* Hash table to find inodes, identified by their inode ID.
53  * */
54 struct wim_inode_table {
55         /* Fields for the hash table */
56         struct hlist_head *array;
57         u64 num_entries;
58         u64 capacity;
59
60         /*
61          * Linked list of "extra" inodes.  These may be:
62          *
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().
66          *
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
70          *   called.
71          */
72         struct hlist_head extra_inodes;
73 };
74
75 static int
76 init_inode_table(struct wim_inode_table *table, size_t capacity)
77 {
78         table->array = CALLOC(capacity, sizeof(table->array[0]));
79         if (!table->array) {
80                 ERROR("Cannot initalize inode table: out of memory");
81                 return WIMLIB_ERR_NOMEM;
82         }
83         table->num_entries  = 0;
84         table->capacity     = capacity;
85         INIT_HLIST_HEAD(&table->extra_inodes);
86         return 0;
87 }
88
89 static inline void
90 destroy_inode_table(struct wim_inode_table *table)
91 {
92         FREE(table->array);
93 }
94
95 static inline size_t
96 inode_link_count(const struct wim_inode *inode)
97 {
98         const struct list_head *cur;
99         size_t size = 0;
100         list_for_each(cur, &inode->i_dentry)
101                 size++;
102         return size;
103 }
104
105 /* Insert a dentry into the inode table based on the inode number of the
106  * attached inode (which came from the hard link group ID field of the on-disk
107  * WIM dentry) */
108 static int
109 inode_table_insert(struct wim_dentry *dentry, void *_table)
110 {
111         struct wim_inode_table *table = _table;
112         struct wim_inode *d_inode = dentry->d_inode;
113
114         if (d_inode->i_ino == 0) {
115                 /* A dentry with a hard link group ID of 0 indicates that it's
116                  * in a hard link group by itself.  Add it to the list of extra
117                  * inodes rather than inserting it into the hash lists. */
118                 hlist_add_head(&d_inode->i_hlist, &table->extra_inodes);
119
120                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
121                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
122                 wimlib_assert(d_inode->i_nlink == 1);
123         } else {
124                 size_t pos;
125                 struct wim_inode *inode;
126                 struct hlist_node *cur;
127
128                 /* Try adding this dentry to an existing inode */
129                 pos = d_inode->i_ino % table->capacity;
130                 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
131                         if (inode->i_ino == d_inode->i_ino) {
132                                 inode_add_dentry(dentry, inode);
133                                 inode->i_nlink++;
134                                 return 0;
135                         }
136                 }
137
138                 /* No inode in the table has the same number as this one, so add
139                  * it to the table. */
140                 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
141
142                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
143                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
144                 wimlib_assert(d_inode->i_nlink == 1);
145
146                 /* XXX Make the table grow when too many entries have been
147                  * inserted. */
148                 table->num_entries++;
149         }
150         return 0;
151 }
152
153 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
154 static void
155 print_inode_dentries(const struct wim_inode *inode)
156 {
157         struct wim_dentry *dentry;
158         inode_for_each_dentry(dentry, inode)
159                 printf("`%s'\n", dentry->full_path);
160 }
161 #endif
162
163 static void
164 inconsistent_inode(const struct wim_inode *inode)
165 {
166 #ifdef ENABLE_ERROR_MESSAGES
167         ERROR("An inconsistent hard link group that cannot be corrected has "
168               "been detected");
169         ERROR("The dentries are located at the following paths:");
170         print_inode_dentries(inode);
171 #endif
172 }
173
174 static bool
175 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
176                       const struct wim_inode * restrict ref_inode_2)
177 {
178         wimlib_assert(ref_inode_1 != ref_inode_2);
179
180         if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
181                 return false;
182         if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
183             || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
184                 return false;
185         for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
186                 const u8 *ref_1_hash, *ref_2_hash;
187                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
188                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
189                 if (!hashes_equal(ref_1_hash, ref_2_hash))
190                         return false;
191                 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
192                                                      &ref_inode_2->i_ads_entries[i - 1]))
193                         return false;
194
195         }
196         return true;
197 }
198
199 static bool
200 inodes_consistent(const struct wim_inode * restrict ref_inode,
201                   const struct wim_inode * restrict inode)
202 {
203         wimlib_assert(ref_inode != inode);
204
205         if (ref_inode->i_num_ads != inode->i_num_ads &&
206             inode->i_num_ads != 0)
207                 return false;
208         if (ref_inode->i_security_id != inode->i_security_id
209             || ref_inode->i_attributes != inode->i_attributes)
210                 return false;
211         for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
212                 const u8 *ref_hash, *hash;
213                 ref_hash = inode_stream_hash(ref_inode, i);
214                 hash = inode_stream_hash(inode, i);
215                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
216                         return false;
217                 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
218                                                      &inode->i_ads_entries[i - 1]))
219                         return false;
220         }
221         return true;
222 }
223
224 /* Fix up a "true" inode and check for inconsistencies */
225 static int
226 fix_true_inode(struct wim_inode *inode, struct hlist_head *inode_list)
227 {
228         struct wim_dentry *dentry;
229         struct wim_dentry *ref_dentry = NULL;
230         struct wim_inode *ref_inode;
231         u64 last_ctime = 0;
232         u64 last_mtime = 0;
233         u64 last_atime = 0;
234
235         inode_for_each_dentry(dentry, inode) {
236                 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
237                         ref_dentry = dentry;
238                 if (dentry->d_inode->i_creation_time > last_ctime)
239                         last_ctime = dentry->d_inode->i_creation_time;
240                 if (dentry->d_inode->i_last_write_time > last_mtime)
241                         last_mtime = dentry->d_inode->i_last_write_time;
242                 if (dentry->d_inode->i_last_access_time > last_atime)
243                         last_atime = dentry->d_inode->i_last_access_time;
244         }
245
246         ref_inode = ref_dentry->d_inode;
247         ref_inode->i_nlink = 1;
248         hlist_add_head(&ref_inode->i_hlist, inode_list);
249
250         list_del(&inode->i_dentry);
251         list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
252
253         inode_for_each_dentry(dentry, ref_inode) {
254                 if (dentry != ref_dentry) {
255                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
256                                 inconsistent_inode(ref_inode);
257                                 return WIMLIB_ERR_INVALID_DENTRY;
258                         }
259                         /* Free the unneeded `struct wim_inode'. */
260                         dentry->d_inode->i_hlist.next = NULL;
261                         dentry->d_inode->i_hlist.pprev = NULL;
262                         free_inode(dentry->d_inode);
263                         dentry->d_inode = ref_inode;
264                         ref_inode->i_nlink++;
265                 }
266         }
267         ref_inode->i_creation_time = last_ctime;
268         ref_inode->i_last_write_time = last_mtime;
269         ref_inode->i_last_access_time = last_atime;
270         wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
271         return 0;
272 }
273
274 /*
275  * Fixes up a nominal inode.
276  *
277  * By a nominal inode we mean a group of two or more dentries that share the
278  * same hard link group ID.
279  *
280  * If dentries in the inode are found to be inconsistent, we may split the inode
281  * into several "true" inodes.
282  *
283  * After splitting up each nominal inode into the "true" inodes we will
284  * canonicalize the link group by getting rid of all the unnecessary `struct
285  * wim_inode's.  There will be just one `struct wim_inode' for each hard link
286  * group remaining.
287  */
288 static int
289 fix_nominal_inode(struct wim_inode *inode, struct hlist_head *inode_list)
290 {
291         struct wim_dentry *dentry;
292         struct hlist_node *cur, *tmp;
293         int ret;
294         size_t num_true_inodes;
295
296         wimlib_assert(inode->i_nlink == inode_link_count(inode));
297
298         LIST_HEAD(dentries_with_data_streams);
299         LIST_HEAD(dentries_with_no_data_streams);
300         HLIST_HEAD(true_inodes);
301
302         /* Create a list of dentries in the nominal inode that have at
303          * least one data stream with a non-zero hash, and another list that
304          * contains the dentries that have a zero hash for all data streams. */
305         inode_for_each_dentry(dentry, inode) {
306                 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
307                         const u8 *hash;
308                         hash = inode_stream_hash(dentry->d_inode, i);
309                         if (!is_zero_hash(hash)) {
310                                 list_add(&dentry->tmp_list,
311                                          &dentries_with_data_streams);
312                                 goto next_dentry;
313                         }
314                 }
315                 list_add(&dentry->tmp_list,
316                          &dentries_with_no_data_streams);
317         next_dentry:
318                 ;
319         }
320
321         /* If there are no dentries with data streams, we require the nominal
322          * inode to be a true inode */
323         if (list_empty(&dentries_with_data_streams)) {
324         #ifdef ENABLE_DEBUG
325                 if (inode->i_nlink > 1) {
326                         DEBUG("Found link group of size %u without "
327                               "any data streams:", inode->i_nlink);
328                         print_inode_dentries(inode);
329                         DEBUG("We are going to interpret it as true "
330                               "link group, provided that the dentries "
331                               "are consistent.");
332                 }
333         #endif
334                 return fix_true_inode(inode, inode_list);
335         }
336
337         /* One or more dentries had data streams specified.  We check each of
338          * these dentries for consistency with the others to form a set of true
339          * inodes. */
340         num_true_inodes = 0;
341         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
342                 /* Look for a true inode that is consistent with this dentry and
343                  * add this dentry to it.  Or, if none of the true inodes are
344                  * consistent with this dentry, add a new one (if that happens,
345                  * we have split the hard link group). */
346                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
347                         if (ref_inodes_consistent(inode, dentry->d_inode)) {
348                                 inode_add_dentry(dentry, inode);
349                                 goto next_dentry_2;
350                         }
351                 }
352                 num_true_inodes++;
353                 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
354                 inode_add_dentry(dentry, dentry->d_inode);
355                 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
356 next_dentry_2:
357                 ;
358         }
359
360         wimlib_assert(num_true_inodes != 0);
361
362         /* If there were dentries with no data streams, we require there to only
363          * be one true inode so that we know which inode to assign the
364          * streamless dentries to. */
365         if (!list_empty(&dentries_with_no_data_streams)) {
366                 if (num_true_inodes != 1) {
367                         ERROR("Hard inode ambiguity detected!");
368                         ERROR("We split up inode 0x%"PRIx64" due to "
369                               "inconsistencies,", inode->i_ino);
370                         ERROR("but dentries with no stream information remained. "
371                               "We don't know which inode");
372                         ERROR("to assign them to.");
373                         return WIMLIB_ERR_INVALID_DENTRY;
374                 }
375                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
376                 /* Assign the streamless dentries to the one and only true
377                  * inode. */
378                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
379                         inode_add_dentry(dentry, inode);
380         }
381         #ifdef ENABLE_DEBUG
382         if (num_true_inodes != 1) {
383                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
384
385                 printf("Split nominal inode 0x%"PRIx64" into %zu "
386                        "inodes:\n",
387                        inode->i_ino, num_true_inodes);
388                 puts("------------------------------------------------------------------------------");
389                 size_t i = 1;
390                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
391                         printf("[Split inode %zu]\n", i++);
392                         print_inode_dentries(inode);
393                         putchar('\n');
394                 }
395                 puts("------------------------------------------------------------------------------");
396         }
397         #endif
398
399         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
400                 ret = fix_true_inode(inode, inode_list);
401                 if (ret != 0)
402                         return ret;
403         }
404         return 0;
405 }
406
407 static int
408 fix_inodes(struct wim_inode_table *table, struct hlist_head *inode_list)
409 {
410         struct wim_inode *inode;
411         struct hlist_node *cur, *tmp;
412         int ret;
413         INIT_HLIST_HEAD(inode_list);
414         for (u64 i = 0; i < table->capacity; i++) {
415                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
416                         ret = fix_nominal_inode(inode, inode_list);
417                         if (ret != 0)
418                                 return ret;
419                 }
420         }
421         hlist_for_each_safe(cur, tmp, &table->extra_inodes)
422                 hlist_add_head(cur, inode_list);
423         return 0;
424 }
425
426 /*
427  * dentry_tree_fix_inodes():
428  *
429  * This function takes as input a tree of WIM dentries that initially has a
430  * different inode associated with each dentry.  Sets of dentries that should
431  * share the same inode (a.k.a. hard link groups) are built using the i_ino
432  * field of each inode, then the link count and alias list for one inode in each
433  * set is set correctly and the unnecessary struct wim_inode's freed.  The
434  * effect is to correctly associate exactly one struct wim_inode with each
435  * original inode, regardless of how many dentries are aliases for that inode.
436  *
437  * The special inode number of 0 indicates that the dentry is in a hard link
438  * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
439  * itself.
440  *
441  * This function also checks the dentries in each hard link group for
442  * consistency.  In some WIMs, such as install.wim for some versions of Windows
443  * 7, dentries can share the same hard link group ID but not actually be hard
444  * linked to each other (based on conflicting information, such as file
445  * contents).  This should be an error, but this case needs be handled.  So,
446  * each "nominal" inode (the inode based on the inode numbers provided in the
447  * WIM) is examined for consistency and may be split into multiple "true" inodes
448  * that are maximally sized consistent sets of dentries.
449  *
450  * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
451  * failure.  On success, the list of "true" inodes, linked by the i_hlist field,
452  * is returned in the hlist @inode_list.
453  */
454 int
455 dentry_tree_fix_inodes(struct wim_dentry *root, struct hlist_head *inode_list)
456 {
457         struct wim_inode_table inode_tab;
458         int ret;
459
460         DEBUG("Inserting dentries into inode table");
461         ret = init_inode_table(&inode_tab, 9001);
462         if (ret != 0)
463                 return ret;
464
465         for_dentry_in_tree(root, inode_table_insert, &inode_tab);
466
467         DEBUG("Cleaning up the hard link groups");
468         ret = fix_inodes(&inode_tab, inode_list);
469         destroy_inode_table(&inode_tab);
470         return ret;
471 }
472
473 /* Assign inode numbers to a list of inodes and return the next available
474  * number. */
475 u64
476 assign_inode_numbers(struct hlist_head *inode_list)
477 {
478         DEBUG("Assigning inode numbers");
479         struct wim_inode *inode;
480         struct hlist_node *cur;
481         u64 cur_ino = 1;
482         hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
483                 inode->i_ino = cur_ino;
484                 cur_ino++;
485         }
486         return cur_ino;
487 }