]> wimlib.net Git - wimlib/blob - src/hardlink.c
read_metadata_resource(): Check for named root here
[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_DENTRY;
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 #if defined(ENABLE_ERROR_MESSAGES) || defined(ENABLE_DEBUG)
213 static void
214 print_inode_dentries(const struct wim_inode *inode)
215 {
216         struct wim_dentry *dentry;
217         inode_for_each_dentry(dentry, inode)
218                 tfprintf(stderr, T("%"TS"\n"), dentry_full_path(dentry));
219 }
220 #endif
221
222 static void
223 inconsistent_inode(const struct wim_inode *inode)
224 {
225 #ifdef ENABLE_ERROR_MESSAGES
226         ERROR("An inconsistent hard link group that cannot be corrected has "
227               "been detected");
228         ERROR("The dentries are located at the following paths:");
229         print_inode_dentries(inode);
230 #endif
231 }
232
233 static bool
234 ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
235                       const struct wim_inode * restrict ref_inode_2)
236 {
237         wimlib_assert(ref_inode_1 != ref_inode_2);
238
239         if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
240                 return false;
241         if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
242             || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
243                 return false;
244         for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
245                 const u8 *ref_1_hash, *ref_2_hash;
246                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
247                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
248                 if (!hashes_equal(ref_1_hash, ref_2_hash))
249                         return false;
250                 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
251                                                      &ref_inode_2->i_ads_entries[i - 1]))
252                         return false;
253
254         }
255         return true;
256 }
257
258 static bool
259 inodes_consistent(const struct wim_inode * restrict ref_inode,
260                   const struct wim_inode * restrict inode)
261 {
262         wimlib_assert(ref_inode != inode);
263
264         if (ref_inode->i_num_ads != inode->i_num_ads &&
265             inode->i_num_ads != 0)
266                 return false;
267         if (ref_inode->i_security_id != inode->i_security_id
268             || ref_inode->i_attributes != inode->i_attributes)
269                 return false;
270         for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
271                 const u8 *ref_hash, *hash;
272                 ref_hash = inode_stream_hash(ref_inode, i);
273                 hash = inode_stream_hash(inode, i);
274                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
275                         return false;
276                 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
277                                                      &inode->i_ads_entries[i - 1]))
278                         return false;
279         }
280         return true;
281 }
282
283 /* Fix up a "true" inode and check for inconsistencies */
284 static int
285 fix_true_inode(struct wim_inode *inode, struct list_head *inode_list)
286 {
287         struct wim_dentry *dentry;
288         struct wim_dentry *ref_dentry = NULL;
289         struct wim_inode *ref_inode;
290         u64 last_ctime = 0;
291         u64 last_mtime = 0;
292         u64 last_atime = 0;
293
294         inode_for_each_dentry(dentry, inode) {
295                 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
296                         ref_dentry = dentry;
297                 if (dentry->d_inode->i_creation_time > last_ctime)
298                         last_ctime = dentry->d_inode->i_creation_time;
299                 if (dentry->d_inode->i_last_write_time > last_mtime)
300                         last_mtime = dentry->d_inode->i_last_write_time;
301                 if (dentry->d_inode->i_last_access_time > last_atime)
302                         last_atime = dentry->d_inode->i_last_access_time;
303         }
304
305         ref_inode = ref_dentry->d_inode;
306         wimlib_assert(ref_inode->i_nlink == 1);
307         list_add_tail(&ref_inode->i_list, inode_list);
308
309         list_del(&inode->i_dentry);
310         list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
311
312         inode_for_each_dentry(dentry, ref_inode) {
313                 if (dentry != ref_dentry) {
314                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
315                                 inconsistent_inode(ref_inode);
316                                 return WIMLIB_ERR_INVALID_DENTRY;
317                         }
318                         /* Free the unneeded `struct wim_inode'. */
319                         wimlib_assert(dentry->d_inode->i_nlink == 1);
320                         free_inode(dentry->d_inode);
321                         dentry->d_inode = ref_inode;
322                         ref_inode->i_nlink++;
323                 }
324         }
325         ref_inode->i_creation_time = last_ctime;
326         ref_inode->i_last_write_time = last_mtime;
327         ref_inode->i_last_access_time = last_atime;
328         wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
329         return 0;
330 }
331
332 /*
333  * Fixes up a nominal inode.
334  *
335  * By a nominal inode we mean a group of two or more dentries that share the
336  * same hard link group ID.
337  *
338  * If dentries in the inode are found to be inconsistent, we may split the inode
339  * into several "true" inodes.
340  *
341  * After splitting up each nominal inode into the "true" inodes we will
342  * canonicalize the link group by getting rid of all the unnecessary `struct
343  * wim_inode's.  There will be just one `struct wim_inode' for each hard link
344  * group remaining.
345  */
346 static int
347 fix_nominal_inode(struct wim_inode *inode, struct list_head *inode_list,
348                   bool *ino_changes_needed)
349 {
350         struct wim_dentry *dentry;
351         struct hlist_node *cur, *tmp;
352         int ret;
353         size_t num_true_inodes;
354         unsigned nominal_group_size = inode_link_count(inode);
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 (nominal_group_size > 1) {
384                         DEBUG("Found link group of size %u without "
385                               "any data streams:", nominal_group_size);
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                         ret = WIMLIB_ERR_INVALID_DENTRY;
432                         goto out_cleanup_true_inode_list;
433                 }
434                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
435                 /* Assign the streamless dentries to the one and only true
436                  * inode. */
437                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
438                         inode_add_dentry(dentry, inode);
439         }
440         if (num_true_inodes != 1) {
441         #ifdef ENABLE_DEBUG
442                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
443
444                 tprintf(T("Split nominal inode 0x%"PRIx64" into %zu "
445                           "inodes:\n"), inode->i_ino, num_true_inodes);
446                 tputs(T("----------------------------------------------------"
447                         "--------------------------"));
448                 size_t i = 1;
449                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
450                         tprintf(T("[Split inode %zu]\n"), i++);
451                         print_inode_dentries(inode);
452                         tputchar(T('\n'));
453                 }
454                 tputs(T("----------------------------------------------------"
455                         "--------------------------"));
456         #endif
457                 *ino_changes_needed = true;
458         }
459
460         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
461                 hlist_del_init(&inode->i_hlist);
462                 ret = fix_true_inode(inode, inode_list);
463                 if (ret)
464                         goto out_cleanup_true_inode_list;
465         }
466         ret = 0;
467         goto out;
468 out_cleanup_true_inode_list:
469         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist)
470                 hlist_del_init(&inode->i_hlist);
471 out:
472         return ret;
473 }
474
475 static int
476 fix_inodes(struct wim_inode_table *table, struct list_head *inode_list,
477            bool *ino_changes_needed)
478 {
479         struct wim_inode *inode;
480         struct hlist_node *cur, *tmp;
481         int ret;
482         INIT_LIST_HEAD(inode_list);
483         for (u64 i = 0; i < table->capacity; i++) {
484                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
485                         hlist_del_init(&inode->i_hlist);
486                         ret = fix_nominal_inode(inode, inode_list, ino_changes_needed);
487                         if (ret)
488                                 return ret;
489                 }
490         }
491         list_splice_tail(&table->extra_inodes, inode_list);
492         return 0;
493 }
494
495 /*
496  * dentry_tree_fix_inodes():
497  *
498  * This function takes as input a tree of WIM dentries that initially has a
499  * different inode associated with each dentry.  Sets of dentries that should
500  * share the same inode (a.k.a. hard link groups) are built using the i_ino
501  * field of each inode, then the link count and alias list for one inode in each
502  * set is set correctly and the unnecessary struct wim_inode's freed.  The
503  * effect is to correctly associate exactly one struct wim_inode with each
504  * original inode, regardless of how many dentries are aliases for that inode.
505  *
506  * The special inode number of 0 indicates that the dentry is in a hard link
507  * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
508  * itself.
509  *
510  * This function also checks the dentries in each hard link group for
511  * consistency.  In some WIMs, such as install.wim for some versions of Windows
512  * 7, dentries can share the same hard link group ID but not actually be hard
513  * linked to each other (based on conflicting information, such as file
514  * contents).  This should be an error, but this case needs be handled.  So,
515  * each "nominal" inode (the inode based on the inode numbers provided in the
516  * WIM) is examined for consistency and may be split into multiple "true" inodes
517  * that are maximally sized consistent sets of dentries.
518  *
519  * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
520  * failure.  On success, the list of "true" inodes, linked by the i_hlist field,
521  * is returned in the hlist @inode_list.
522  */
523 int
524 dentry_tree_fix_inodes(struct wim_dentry *root, struct list_head *inode_list)
525 {
526         struct wim_inode_table inode_tab;
527         int ret;
528         bool ino_changes_needed;
529         struct wim_inode *inode;
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_inode_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_inode_table;
545
546         if (ino_changes_needed) {
547                 u64 cur_ino = 1;
548
549                 WARNING("Re-assigning inode numbers due to inode inconsistencies");
550                 list_for_each_entry(inode, inode_list, i_list) {
551                         if (inode->i_nlink > 1)
552                                 inode->i_ino = cur_ino++;
553                         else
554                                 inode->i_ino = 0;
555                 }
556         }
557         /* On success, all the inodes have been moved to the image inode list,
558          * so there's no need to delete from from the hash lists in the inode
559          * table before freeing the hash buckets array directly. */
560         ret = 0;
561         goto out_destroy_inode_table_raw;
562 out_destroy_inode_table:
563         for (size_t i = 0; i < inode_tab.capacity; i++) {
564                 struct hlist_node *cur, *tmp;
565                 hlist_for_each_entry_safe(inode, cur, tmp, &inode_tab.array[i], i_hlist)
566                         hlist_del_init(&inode->i_hlist);
567         }
568         {
569                 struct wim_inode *tmp;
570                 list_for_each_entry_safe(inode, tmp, &inode_tab.extra_inodes, i_list)
571                         list_del_init(&inode->i_list);
572         }
573 out_destroy_inode_table_raw:
574         destroy_inode_table(&inode_tab);
575 out:
576         return ret;
577 }
578
579 /* Assign consecutive inode numbers to a new set of inodes from the inode table,
580  * and append the inodes to a single list @head that contains the inodes already
581  * existing in the WIM image.  */
582 void
583 inode_table_prepare_inode_list(struct wim_inode_table *table,
584                                struct list_head *head)
585 {
586         struct wim_inode *inode;
587         struct hlist_node *cur, *tmp;
588         u64 cur_ino = 1;
589
590         /* Re-assign inode numbers in the existing list to avoid duplicates. */
591         list_for_each_entry(inode, head, i_list) {
592                 if (inode->i_nlink > 1)
593                         inode->i_ino = cur_ino++;
594                 else
595                         inode->i_ino = 0;
596         }
597
598         /* Assign inode numbers to the new inodes and move them to the image's
599          * inode list. */
600         for (size_t i = 0; i < table->capacity; i++) {
601                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist)
602                 {
603                         if (inode->i_nlink > 1)
604                                 inode->i_ino = cur_ino++;
605                         else
606                                 inode->i_ino = 0;
607                         list_add_tail(&inode->i_list, head);
608                 }
609                 INIT_HLIST_HEAD(&table->array[i]);
610         }
611         list_splice_tail(&table->extra_inodes, head);
612         table->num_entries = 0;
613 }