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