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