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