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