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