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