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