]> wimlib.net Git - wimlib/blob - src/hardlink.c
Win32 capture
[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 /* Hash table to find inodes, identified by their inode ID.
53  * */
54 struct wim_inode_table {
55         /* Fields for the hash table */
56         struct hlist_head *array;
57         u64 num_entries;
58         u64 capacity;
59
60         /*
61          * Linked list of "extra" inodes.  These may be:
62          *
63          * - inodes with link count 1, which are all allowed to have 0 for their
64          *   inode number, meaning we cannot insert them into the hash table
65          *   before calling assign_inode_numbers().
66          *
67          * - Groups we create ourselves by splitting a nominal inode due to
68          *   inconsistencies in the dentries.  These inodes will share a inode
69          *   number with some other inode until assign_inode_numbers() is
70          *   called.
71          */
72         struct hlist_head extra_inodes;
73 };
74
75 static inline void destroy_inode_table(struct wim_inode_table *table)
76 {
77         FREE(table->array);
78 }
79
80 static int init_inode_table(struct wim_inode_table *table, size_t capacity)
81 {
82         table->array = CALLOC(capacity, sizeof(table->array[0]));
83         if (!table->array) {
84                 ERROR("Cannot initalize inode table: out of memory");
85                 return WIMLIB_ERR_NOMEM;
86         }
87         table->num_entries  = 0;
88         table->capacity     = capacity;
89         INIT_HLIST_HEAD(&table->extra_inodes);
90         return 0;
91 }
92
93 static inline size_t inode_link_count(const struct wim_inode *inode)
94 {
95         const struct list_head *cur;
96         size_t size = 0;
97         list_for_each(cur, &inode->i_dentry)
98                 size++;
99         return size;
100 }
101
102 /* Insert a dentry into the inode table based on the inode number of the
103  * attached inode (which came from the hard link group ID field of the on-disk
104  * WIM dentry) */
105 static int inode_table_insert(struct wim_dentry *dentry, void *__table)
106 {
107         struct wim_inode_table *table = __table;
108         struct wim_inode *d_inode = dentry->d_inode;
109
110         if (d_inode->i_ino == 0) {
111                 /* A dentry with a hard link group ID of 0 indicates that it's
112                  * in a hard link group by itself.  Add it to the list of extra
113                  * inodes rather than inserting it into the hash lists. */
114                 hlist_add_head(&d_inode->i_hlist, &table->extra_inodes);
115
116                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
117                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
118                 wimlib_assert(d_inode->i_nlink == 1);
119         } else {
120                 size_t pos;
121                 struct wim_inode *inode;
122                 struct hlist_node *cur;
123
124                 /* Try adding this dentry to an existing inode */
125                 pos = d_inode->i_ino % table->capacity;
126                 hlist_for_each_entry(inode, cur, &table->array[pos], i_hlist) {
127                         if (inode->i_ino == d_inode->i_ino) {
128                                 inode_add_dentry(dentry, inode);
129                                 inode->i_nlink++;
130                                 return 0;
131                         }
132                 }
133
134                 /* No inode in the table has the same number as this one, so add
135                  * it to the table. */
136                 hlist_add_head(&d_inode->i_hlist, &table->array[pos]);
137
138                 wimlib_assert(d_inode->i_dentry.next == &dentry->d_alias);
139                 wimlib_assert(d_inode->i_dentry.prev == &dentry->d_alias);
140                 wimlib_assert(d_inode->i_nlink == 1);
141
142                 /* XXX Make the table grow when too many entries have been
143                  * inserted. */
144                 table->num_entries++;
145         }
146         return 0;
147 }
148
149 static void print_inode_dentries(const struct wim_inode *inode)
150 {
151         struct wim_dentry *dentry;
152         inode_for_each_dentry(dentry, inode)
153                 printf("`%s'\n", dentry->full_path_utf8);
154 }
155
156 static void inconsistent_inode(const struct wim_inode *inode)
157 {
158         ERROR("An inconsistent hard link group that cannot be corrected has "
159               "been detected");
160         ERROR("The dentries are located at the following paths:");
161 #ifdef ENABLE_ERROR_MESSAGES
162         print_inode_dentries(inode);
163 #endif
164 }
165
166 static bool ref_inodes_consistent(const struct wim_inode * restrict ref_inode_1,
167                                   const struct wim_inode * restrict ref_inode_2)
168 {
169         wimlib_assert(ref_inode_1 != ref_inode_2);
170
171         if (ref_inode_1->i_num_ads != ref_inode_2->i_num_ads)
172                 return false;
173         if (ref_inode_1->i_security_id != ref_inode_2->i_security_id
174             || ref_inode_1->i_attributes != ref_inode_2->i_attributes)
175                 return false;
176         for (unsigned i = 0; i <= ref_inode_1->i_num_ads; i++) {
177                 const u8 *ref_1_hash, *ref_2_hash;
178                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
179                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
180                 if (!hashes_equal(ref_1_hash, ref_2_hash))
181                         return false;
182                 if (i && !ads_entries_have_same_name(&ref_inode_1->i_ads_entries[i - 1],
183                                                      &ref_inode_2->i_ads_entries[i - 1]))
184                         return false;
185
186         }
187         return true;
188 }
189
190 static bool inodes_consistent(const struct wim_inode * restrict ref_inode,
191                               const struct wim_inode * restrict inode)
192 {
193         wimlib_assert(ref_inode != inode);
194
195         if (ref_inode->i_num_ads != inode->i_num_ads &&
196             inode->i_num_ads != 0)
197                 return false;
198         if (ref_inode->i_security_id != inode->i_security_id
199             || ref_inode->i_attributes != inode->i_attributes)
200                 return false;
201         for (unsigned i = 0; i <= min(ref_inode->i_num_ads, inode->i_num_ads); i++) {
202                 const u8 *ref_hash, *hash;
203                 ref_hash = inode_stream_hash(ref_inode, i);
204                 hash = inode_stream_hash(inode, i);
205                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
206                         return false;
207                 if (i && !ads_entries_have_same_name(&ref_inode->i_ads_entries[i - 1],
208                                                      &inode->i_ads_entries[i - 1]))
209                         return false;
210         }
211         return true;
212 }
213
214 /* Fix up a "true" inode and check for inconsistencies */
215 static int fix_true_inode(struct wim_inode *inode, struct hlist_head *inode_list)
216 {
217         struct wim_dentry *dentry;
218         struct wim_dentry *ref_dentry = NULL;
219         struct wim_inode *ref_inode;
220         u64 last_ctime = 0;
221         u64 last_mtime = 0;
222         u64 last_atime = 0;
223
224         inode_for_each_dentry(dentry, inode) {
225                 if (!ref_dentry || dentry->d_inode->i_num_ads > ref_dentry->d_inode->i_num_ads)
226                         ref_dentry = dentry;
227                 if (dentry->d_inode->i_creation_time > last_ctime)
228                         last_ctime = dentry->d_inode->i_creation_time;
229                 if (dentry->d_inode->i_last_write_time > last_mtime)
230                         last_mtime = dentry->d_inode->i_last_write_time;
231                 if (dentry->d_inode->i_last_access_time > last_atime)
232                         last_atime = dentry->d_inode->i_last_access_time;
233         }
234
235         ref_inode = ref_dentry->d_inode;
236         ref_inode->i_nlink = 1;
237         hlist_add_head(&ref_inode->i_hlist, inode_list);
238
239         list_del(&inode->i_dentry);
240         list_add(&ref_inode->i_dentry, &ref_dentry->d_alias);
241
242         inode_for_each_dentry(dentry, ref_inode) {
243                 if (dentry != ref_dentry) {
244                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
245                                 inconsistent_inode(ref_inode);
246                                 return WIMLIB_ERR_INVALID_DENTRY;
247                         }
248                         /* Free the unneeded `struct wim_inode'. */
249                         dentry->d_inode->i_hlist.next = NULL;
250                         dentry->d_inode->i_hlist.pprev = NULL;
251                         free_inode(dentry->d_inode);
252                         dentry->d_inode = ref_inode;
253                         ref_inode->i_nlink++;
254                 }
255         }
256         ref_inode->i_creation_time = last_ctime;
257         ref_inode->i_last_write_time = last_mtime;
258         ref_inode->i_last_access_time = last_atime;
259         wimlib_assert(inode_link_count(ref_inode) == ref_inode->i_nlink);
260         return 0;
261 }
262
263 /*
264  * Fixes up a nominal inode.
265  *
266  * By a nominal inode we mean a group of two or more dentries that share the
267  * same hard link group ID.
268  *
269  * If dentries in the inode are found to be inconsistent, we may split the inode
270  * into several "true" inodes.
271  *
272  * After splitting up each nominal inode into the "true" inodes we will
273  * canonicalize the link group by getting rid of all the unnecessary `struct
274  * wim_inode's.  There will be just one `struct wim_inode' for each hard link
275  * group remaining.
276  */
277 static int fix_nominal_inode(struct wim_inode *inode,
278                              struct hlist_head *inode_list)
279 {
280         struct wim_dentry *dentry;
281         struct hlist_node *cur, *tmp;
282         int ret;
283         size_t num_true_inodes;
284
285         wimlib_assert(inode->i_nlink == inode_link_count(inode));
286
287         LIST_HEAD(dentries_with_data_streams);
288         LIST_HEAD(dentries_with_no_data_streams);
289         HLIST_HEAD(true_inodes);
290
291         /* Create a list of dentries in the nominal inode that have at
292          * least one data stream with a non-zero hash, and another list that
293          * contains the dentries that have a zero hash for all data streams. */
294         inode_for_each_dentry(dentry, inode) {
295                 for (unsigned i = 0; i <= dentry->d_inode->i_num_ads; i++) {
296                         const u8 *hash;
297                         hash = inode_stream_hash(dentry->d_inode, i);
298                         if (!is_zero_hash(hash)) {
299                                 list_add(&dentry->tmp_list,
300                                          &dentries_with_data_streams);
301                                 goto next_dentry;
302                         }
303                 }
304                 list_add(&dentry->tmp_list,
305                          &dentries_with_no_data_streams);
306         next_dentry:
307                 ;
308         }
309
310         /* If there are no dentries with data streams, we require the nominal
311          * inode to be a true inode */
312         if (list_empty(&dentries_with_data_streams)) {
313         #ifdef ENABLE_DEBUG
314                 if (inode->i_nlink > 1) {
315                         DEBUG("Found link group of size %u without "
316                               "any data streams:", inode->i_nlink);
317                         print_inode_dentries(inode);
318                         DEBUG("We are going to interpret it as true "
319                               "link group, provided that the dentries "
320                               "are consistent.");
321                 }
322         #endif
323                 return fix_true_inode(inode, inode_list);
324         }
325
326         /* One or more dentries had data streams specified.  We check each of
327          * these dentries for consistency with the others to form a set of true
328          * inodes. */
329         num_true_inodes = 0;
330         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
331                 /* Look for a true inode that is consistent with this dentry and
332                  * add this dentry to it.  Or, if none of the true inodes are
333                  * consistent with this dentry, add a new one (if that happens,
334                  * we have split the hard link group). */
335                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
336                         if (ref_inodes_consistent(inode, dentry->d_inode)) {
337                                 inode_add_dentry(dentry, inode);
338                                 goto next_dentry_2;
339                         }
340                 }
341                 num_true_inodes++;
342                 INIT_LIST_HEAD(&dentry->d_inode->i_dentry);
343                 inode_add_dentry(dentry, dentry->d_inode);
344                 hlist_add_head(&dentry->d_inode->i_hlist, &true_inodes);
345 next_dentry_2:
346                 ;
347         }
348
349         wimlib_assert(num_true_inodes != 0);
350
351         /* If there were dentries with no data streams, we require there to only
352          * be one true inode so that we know which inode to assign the
353          * streamless dentries to. */
354         if (!list_empty(&dentries_with_no_data_streams)) {
355                 if (num_true_inodes != 1) {
356                         ERROR("Hard inode ambiguity detected!");
357                         ERROR("We split up inode 0x%"PRIx64" due to "
358                               "inconsistencies,", inode->i_ino);
359                         ERROR("but dentries with no stream information remained. "
360                               "We don't know which inode");
361                         ERROR("to assign them to.");
362                         return WIMLIB_ERR_INVALID_DENTRY;
363                 }
364                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
365                 /* Assign the streamless dentries to the one and only true
366                  * inode. */
367                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
368                         inode_add_dentry(dentry, inode);
369         }
370         #ifdef ENABLE_DEBUG
371         if (num_true_inodes != 1) {
372                 inode = container_of(true_inodes.first, struct wim_inode, i_hlist);
373
374                 printf("Split nominal inode 0x%"PRIx64" into %zu "
375                        "inodes:\n",
376                        inode->i_ino, num_true_inodes);
377                 puts("------------------------------------------------------------------------------");
378                 size_t i = 1;
379                 hlist_for_each_entry(inode, cur, &true_inodes, i_hlist) {
380                         printf("[Split inode %zu]\n", i++);
381                         print_inode_dentries(inode);
382                         putchar('\n');
383                 }
384                 puts("------------------------------------------------------------------------------");
385         }
386         #endif
387
388         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, i_hlist) {
389                 ret = fix_true_inode(inode, inode_list);
390                 if (ret != 0)
391                         return ret;
392         }
393         return 0;
394 }
395
396 static int fix_inodes(struct wim_inode_table *table,
397                       struct hlist_head *inode_list)
398 {
399         struct wim_inode *inode;
400         struct hlist_node *cur, *tmp;
401         int ret;
402         INIT_HLIST_HEAD(inode_list);
403         for (u64 i = 0; i < table->capacity; i++) {
404                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], i_hlist) {
405                         ret = fix_nominal_inode(inode, inode_list);
406                         if (ret != 0)
407                                 return ret;
408                 }
409         }
410         hlist_for_each_safe(cur, tmp, &table->extra_inodes)
411                 hlist_add_head(cur, inode_list);
412         return 0;
413 }
414
415 /*
416  * dentry_tree_fix_inodes():
417  *
418  * This function takes as input a tree of WIM dentries that initially has a
419  * different inode associated with each dentry.  Sets of dentries that should
420  * share the same inode (a.k.a. hard link groups) are built using the i_ino
421  * field of each inode, then the link count and alias list for one inode in each
422  * set is set correctly and the unnecessary struct wim_inode's freed.  The
423  * effect is to correctly associate exactly one struct wim_inode with each
424  * original inode, regardless of how many dentries are aliases for that inode.
425  *
426  * The special inode number of 0 indicates that the dentry is in a hard link
427  * group by itself, and therefore has a 'struct wim_inode' with i_nlink=1 to
428  * itself.
429  *
430  * This function also checks the dentries in each hard link group for
431  * consistency.  In some WIMs, such as install.wim for some versions of Windows
432  * 7, dentries can share the same hard link group ID but not actually be hard
433  * linked to each other (based on conflicting information, such as file
434  * contents).  This should be an error, but this case needs be handled.  So,
435  * each "nominal" inode (the inode based on the inode numbers provided in the
436  * WIM) is examined for consistency and may be split into multiple "true" inodes
437  * that are maximally sized consistent sets of dentries.
438  *
439  * Return 0 on success; WIMLIB_ERR_NOMEM or WIMLIB_ERR_INVALID_DENTRY on
440  * failure.  On success, the list of "true" inodes, linked by the i_hlist field,
441  * is returned in the hlist @inode_list.
442  */
443 int dentry_tree_fix_inodes(struct wim_dentry *root, struct hlist_head *inode_list)
444 {
445         struct wim_inode_table inode_tab;
446         int ret;
447
448         DEBUG("Inserting dentries into inode table");
449         ret = init_inode_table(&inode_tab, 9001);
450         if (ret != 0)
451                 return ret;
452
453         for_dentry_in_tree(root, inode_table_insert, &inode_tab);
454
455         DEBUG("Cleaning up the hard link groups");
456         ret = fix_inodes(&inode_tab, inode_list);
457         destroy_inode_table(&inode_tab);
458         return ret;
459 }
460
461 /* Assign inode numbers to a list of inodes and return the next available
462  * number. */
463 u64 assign_inode_numbers(struct hlist_head *inode_list)
464 {
465         DEBUG("Assigning inode numbers");
466         struct wim_inode *inode;
467         struct hlist_node *cur;
468         u64 cur_ino = 1;
469         hlist_for_each_entry(inode, cur, inode_list, i_hlist) {
470                 inode->i_ino = cur_ino;
471                 cur_ino++;
472         }
473         return cur_ino;
474 }