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