]> wimlib.net Git - wimlib/blob - src/hardlink.c
93030d8f7e10e6b89874a089c28da575c63e850d
[wimlib] / src / hardlink.c
1 /*
2  * hardlink.c
3  *
4  * Code to deal with hard links in WIMs.  Essentially, the WIM dentries are put
5  * into a hash table indexed by the inode ID field, then for each hard
6  * inode, a linked list is made to connect the dentries.
7  */
8
9 /*
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
14  * wimlib is free software; you can redistribute it and/or modify it under the
15  * terms of the GNU General Public License as published by the Free
16  * Software Foundation; either version 3 of the License, or (at your option)
17  * any later version.
18  *
19  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21  * A PARTICULAR PURPOSE. See the GNU General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with wimlib; if not, see http://www.gnu.org/licenses/.
26  */
27
28 #include "wimlib_internal.h"
29 #include "dentry.h"
30 #include "list.h"
31 #include "lookup_table.h"
32
33 /*                             NULL        NULL
34  *                              ^           ^
35  *         dentry               |           |
36  *        /     \          -----------  -----------
37  *        |      dentry<---|  struct  | |  struct  |---> dentry
38  *        \     /          |  inode   | |  inode   |
39  *         dentry          ------------ ------------
40  *                              ^           ^
41  *                              |           |
42  *                              |           |                   dentry
43  *                         -----------  -----------            /      \
44  *               dentry<---|  struct  | |  struct  |---> dentry        dentry
45  *              /          |  inode   | |  inode   |           \      /
46  *         dentry          ------------ ------------            dentry
47  *                              ^           ^
48  *                              |           |
49  *                            -----------------
50  *        inode_table->array  | idx 0 | idx 1 |
51  *                            -----------------
52  */
53
54 /* Hash table to find inodes, identified by their inode ID.
55  * */
56 struct inode_table {
57         /* Fields for the hash table */
58         struct hlist_head *array;
59         u64 num_entries;
60         u64 capacity;
61
62         /*
63          * Linked list of "extra" inodes.  These may be:
64          *
65          * - inodes with link count 1, which are all allowed to have 0 for their
66          *   inode number, meaning we cannot insert them into the hash table
67          *   before calling assign_inode_numbers().
68          *
69          * - Groups we create ourselves by splitting a nominal inode due to
70          *   inconsistencies in the dentries.  These inodes will share a inode
71          *   ID with some other inode until assign_inode_numbers() is called.
72          */
73         struct hlist_head extra_inodes;
74 };
75
76 static inline void destroy_inode_table(struct inode_table *table)
77 {
78         FREE(table->array);
79 }
80
81 static int init_inode_table(struct inode_table *table, size_t capacity)
82 {
83         table->array = CALLOC(capacity, sizeof(table->array[0]));
84         if (!table->array) {
85                 ERROR("Cannot initalize inode table: out of memory");
86                 return WIMLIB_ERR_NOMEM;
87         }
88         table->num_entries  = 0;
89         table->capacity     = capacity;
90         INIT_HLIST_HEAD(&table->extra_inodes);
91         return 0;
92 }
93
94 static inline size_t inode_link_count(const struct inode *inode)
95 {
96         const struct list_head *cur;
97         size_t size = 0;
98         list_for_each(cur, &inode->dentry_list)
99                 size++;
100         return size;
101 }
102
103 /*
104  * Insert a dentry into the inode table based on its inode
105  * ID.
106  *
107  * If there is already a dentry in the table having the same inode ID,
108  * and the inode ID is not 0, the dentry is added to the circular
109  * linked list for that inode.
110  *
111  * If the inode ID is 0, this indicates a dentry that's in a hard link
112  * inode by itself (has a link count of 1).  We can't insert it into the hash
113  * table itself because we don't know what inode numbers are available to
114  * give it (this could be kept track of but would be more difficult).  Instead
115  * we keep a linked list of the single dentries, and assign them inode
116  * numbers later.
117  */
118 static int inode_table_insert(struct dentry *dentry, void *__table)
119 {
120         struct inode_table *table = __table;
121         struct inode *d_inode = dentry->d_inode;
122
123         if (d_inode->ino == 0) {
124                 /* Single inode--- Add to the list of extra inodes (we can't put
125                  * it in the table itself because all the singles have a link
126                  * inode ID of 0) */
127                 hlist_add_head(&d_inode->hlist, &table->extra_inodes);
128
129                 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
130                 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
131                 wimlib_assert(d_inode->link_count == 1);
132         } else {
133                 /* Inode that may have multiple corresponding dentries (the code
134                  * will work even if the inode actually contains only 1 dentry
135                  * though) */
136
137                 size_t pos;
138                 struct inode *inode;
139                 struct hlist_node *cur;
140
141                 /* Try adding to existing inode */
142                 pos = d_inode->ino % table->capacity;
143                 hlist_for_each_entry(inode, cur, &table->array[pos], hlist) {
144                         if (inode->ino == d_inode->ino) {
145                                 inode_add_dentry(dentry, inode);
146                                 inode->link_count++;
147                                 return 0;
148                         }
149                 }
150
151                 /* Add new inode to the table */
152                 hlist_add_head(&d_inode->hlist, &table->array[pos]);
153
154                 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
155                 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
156                 wimlib_assert(d_inode->link_count == 1);
157
158                 /* XXX Make the table grow when too many entries have been
159                  * inserted. */
160                 table->num_entries++;
161         }
162         return 0;
163 }
164
165 /* Assign the inode numbers to dentries in a inode table, and return the
166  * next available inode ID. */
167 u64 assign_inode_numbers(struct hlist_head *inode_list)
168 {
169         DEBUG("Assigning inode numbers");
170         struct inode *inode;
171         struct hlist_node *cur;
172         u64 cur_ino = 1;
173         hlist_for_each_entry(inode, cur, inode_list, hlist) {
174                 inode->ino = cur_ino;
175                 cur_ino++;
176         }
177         return cur_ino;
178 }
179
180
181 static void print_inode_dentries(const struct inode *inode)
182 {
183         struct dentry *dentry;
184         inode_for_each_dentry(dentry, inode)
185                 printf("`%s'\n", dentry->full_path_utf8);
186 }
187
188 static void inconsistent_inode(const struct inode *inode)
189 {
190         ERROR("An inconsistent hard link group that cannot be corrected has "
191               "been detected");
192         ERROR("The dentries are located at the following paths:");
193 #ifdef ENABLE_ERROR_MESSAGES
194         print_inode_dentries(inode);
195 #endif
196 }
197
198 static bool ref_inodes_consistent(const struct inode * restrict ref_inode_1,
199                                   const struct inode * restrict ref_inode_2)
200 {
201         wimlib_assert(ref_inode_1 != ref_inode_2);
202
203         if (ref_inode_1->num_ads != ref_inode_2->num_ads)
204                 return false;
205         if (ref_inode_1->security_id != ref_inode_2->security_id
206             || ref_inode_1->attributes != ref_inode_2->attributes)
207                 return false;
208         for (unsigned i = 0; i <= ref_inode_1->num_ads; i++) {
209                 const u8 *ref_1_hash, *ref_2_hash;
210                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
211                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
212                 if (!hashes_equal(ref_1_hash, ref_2_hash))
213                         return false;
214                 if (i && !ads_entries_have_same_name(&ref_inode_1->ads_entries[i - 1],
215                                                      &ref_inode_2->ads_entries[i - 1]))
216                         return false;
217
218         }
219         return true;
220 }
221
222 static bool inodes_consistent(const struct inode * restrict ref_inode,
223                               const struct inode * restrict inode)
224 {
225         wimlib_assert(ref_inode != inode);
226
227         if (ref_inode->num_ads != inode->num_ads &&
228             inode->num_ads != 0)
229                 return false;
230         if (ref_inode->security_id != inode->security_id
231             || ref_inode->attributes != inode->attributes)
232                 return false;
233         for (unsigned i = 0; i <= min(ref_inode->num_ads, inode->num_ads); i++) {
234                 const u8 *ref_hash, *hash;
235                 ref_hash = inode_stream_hash(ref_inode, i);
236                 hash = inode_stream_hash(inode, i);
237                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
238                         return false;
239                 if (i && !ads_entries_have_same_name(&ref_inode->ads_entries[i - 1],
240                                                      &inode->ads_entries[i - 1]))
241                         return false;
242         }
243         return true;
244 }
245
246 /* Fix up a "true" inode and check for inconsistencies */
247 static int fix_true_inode(struct inode *inode, struct hlist_head *inode_list)
248 {
249         struct dentry *dentry;
250         struct dentry *ref_dentry = NULL;
251         struct inode *ref_inode;
252         u64 last_ctime = 0;
253         u64 last_mtime = 0;
254         u64 last_atime = 0;
255
256         inode_for_each_dentry(dentry, inode) {
257                 if (!ref_dentry || dentry->d_inode->num_ads > ref_dentry->d_inode->num_ads)
258                         ref_dentry = dentry;
259                 if (dentry->d_inode->creation_time > last_ctime)
260                         last_ctime = dentry->d_inode->creation_time;
261                 if (dentry->d_inode->last_write_time > last_mtime)
262                         last_mtime = dentry->d_inode->last_write_time;
263                 if (dentry->d_inode->last_access_time > last_atime)
264                         last_atime = dentry->d_inode->last_access_time;
265         }
266
267         ref_inode = ref_dentry->d_inode;
268         ref_inode->link_count = 1;
269         hlist_add_head(&ref_inode->hlist, inode_list);
270
271         list_del(&inode->dentry_list);
272         list_add(&ref_inode->dentry_list, &ref_dentry->inode_dentry_list);
273
274         inode_for_each_dentry(dentry, ref_inode) {
275                 if (dentry != ref_dentry) {
276                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
277                                 inconsistent_inode(ref_inode);
278                                 return WIMLIB_ERR_INVALID_DENTRY;
279                         }
280                         /* Free the unneeded `struct inode'. */
281                         dentry->d_inode->hlist.next = NULL;
282                         dentry->d_inode->hlist.pprev = NULL;
283                         free_inode(dentry->d_inode);
284                         dentry->d_inode = ref_inode;
285                         ref_inode->link_count++;
286                 }
287         }
288         ref_inode->creation_time = last_ctime;
289         ref_inode->last_write_time = last_mtime;
290         ref_inode->last_access_time = last_atime;
291         wimlib_assert(inode_link_count(ref_inode) == ref_inode->link_count);
292         return 0;
293 }
294
295 /*
296  * Fixes up a nominal inode.
297  *
298  * By a nominal inode we mean a group of two or more dentries that share
299  * the same hard link group ID.
300  *
301  * If dentries in the inode are found to be inconsistent, we may split the inode
302  * into several "true" inodes.
303  *
304  * After splitting up each nominal inode into the "true" inodes we will
305  * canonicalize the link group by getting rid of all the unnecessary `struct
306  * inodes'.  There will be just one `struct inode' for each hard link group
307  * remaining.
308  */
309 static int fix_nominal_inode(struct inode *inode,
310                              struct hlist_head *inode_list)
311 {
312         struct dentry *dentry;
313         struct hlist_node *cur, *tmp;
314         int ret;
315         size_t num_true_inodes;
316
317         wimlib_assert(inode->link_count == inode_link_count(inode));
318
319         LIST_HEAD(dentries_with_data_streams);
320         LIST_HEAD(dentries_with_no_data_streams);
321         HLIST_HEAD(true_inodes);
322
323         /* Create a list of dentries in the nominal inode that have at
324          * least one data stream with a non-zero hash, and another list that
325          * contains the dentries that have a zero hash for all data streams. */
326         inode_for_each_dentry(dentry, inode) {
327                 for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
328                         const u8 *hash;
329                         hash = inode_stream_hash(dentry->d_inode, i);
330                         if (!is_zero_hash(hash)) {
331                                 list_add(&dentry->tmp_list,
332                                          &dentries_with_data_streams);
333                                 goto next_dentry;
334                         }
335                 }
336                 list_add(&dentry->tmp_list,
337                          &dentries_with_no_data_streams);
338         next_dentry:
339                 ;
340         }
341
342         /* If there are no dentries with data streams, we require the nominal
343          * inode to be a true inode */
344         if (list_empty(&dentries_with_data_streams)) {
345         #ifdef ENABLE_DEBUG
346                 if (inode->link_count > 1) {
347                         DEBUG("Found link group of size %u without "
348                               "any data streams:", inode->link_count);
349                         print_inode_dentries(inode);
350                         DEBUG("We are going to interpret it as true "
351                               "link group, provided that the dentries "
352                               "are consistent.");
353                 }
354         #endif
355                 return fix_true_inode(inode, inode_list);
356         }
357
358         /* One or more dentries had data streams specified.  We check each of
359          * these dentries for consistency with the others to form a set of true
360          * inodes. */
361         num_true_inodes = 0;
362         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
363                 /* Look for a true inode that is consistent with this dentry and
364                  * add this dentry to it.  Or, if none of the true inodes are
365                  * consistent with this dentry, add a new one (if that happens,
366                  * we have split the hard link group). */
367                 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
368                         if (ref_inodes_consistent(inode, dentry->d_inode)) {
369                                 inode_add_dentry(dentry, inode);
370                                 goto next_dentry_2;
371                         }
372                 }
373                 num_true_inodes++;
374                 INIT_LIST_HEAD(&dentry->d_inode->dentry_list);
375                 inode_add_dentry(dentry, dentry->d_inode);
376                 hlist_add_head(&dentry->d_inode->hlist, &true_inodes);
377 next_dentry_2:
378                 ;
379         }
380
381         wimlib_assert(num_true_inodes != 0);
382
383         /* If there were dentries with no data streams, we require there to only
384          * be one true inode so that we know which inode to assign the
385          * streamless dentries to. */
386         if (!list_empty(&dentries_with_no_data_streams)) {
387                 if (num_true_inodes != 1) {
388                         ERROR("Hard inode ambiguity detected!");
389                         ERROR("We split up inode 0x%"PRIx64" due to "
390                               "inconsistencies,", inode->ino);
391                         ERROR("but dentries with no stream information remained. "
392                               "We don't know which inode");
393                         ERROR("to assign them to.");
394                         return WIMLIB_ERR_INVALID_DENTRY;
395                 }
396                 inode = container_of(true_inodes.first, struct inode, hlist);
397                 /* Assign the streamless dentries to the one and only true
398                  * inode. */
399                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
400                         inode_add_dentry(dentry, inode);
401         }
402         #ifdef ENABLE_DEBUG
403         if (num_true_inodes != 1) {
404                 inode = container_of(true_inodes.first, struct inode, hlist);
405
406                 printf("Split nominal inode 0x%"PRIx64" into %zu "
407                        "inodes:\n",
408                        inode->ino, num_true_inodes);
409                 puts("------------------------------------------------------------------------------");
410                 size_t i = 1;
411                 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
412                         printf("[Split inode %zu]\n", i++);
413                         print_inode_dentries(inode);
414                         putchar('\n');
415                 }
416                 puts("------------------------------------------------------------------------------");
417         }
418         #endif
419
420         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, hlist) {
421                 ret = fix_true_inode(inode, inode_list);
422                 if (ret != 0)
423                         return ret;
424         }
425         return 0;
426 }
427
428 /*
429  * Goes through each hard link group (dentries sharing the same hard link group
430  * ID field) that's been inserted into the inode table and shares the `struct
431  * inode's among members of each hard link group.
432  *
433  * In the process, the dentries belonging to each inode are checked for
434  * consistency.  If they contain data features that indicate they cannot really
435  * correspond to the same inode, this should be an error, but in reality this
436  * case needs to be handled, so we split the dentries into different inodes.
437  *
438  * After this function returns, the inodes are no longer in the inode table, and
439  * the inode table should be destroyed.  A list of the inodes, including all
440  * split inodes as well as the inodes that were good before, is returned in the
441  * list @inode_list.
442  */
443 static int fix_inodes(struct inode_table *table, struct hlist_head *inode_list)
444 {
445         struct inode *inode;
446         struct hlist_node *cur, *tmp;
447         int ret;
448         INIT_HLIST_HEAD(inode_list);
449         for (u64 i = 0; i < table->capacity; i++) {
450                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], hlist) {
451                         ret = fix_nominal_inode(inode, inode_list);
452                         if (ret != 0)
453                                 return ret;
454                 }
455         }
456         hlist_for_each_safe(cur, tmp, &table->extra_inodes)
457                 hlist_add_head(cur, inode_list);
458         return 0;
459 }
460
461 int dentry_tree_fix_inodes(struct dentry *root, struct hlist_head *inode_list)
462 {
463         struct inode_table inode_tab;
464         int ret;
465
466         DEBUG("Inserting dentries into inode table");
467         ret = init_inode_table(&inode_tab, 9001);
468         if (ret != 0)
469                 return ret;
470
471         for_dentry_in_tree(root, inode_table_insert, &inode_tab);
472
473         DEBUG("Cleaning up the hard link groups");
474         ret = fix_inodes(&inode_tab, inode_list);
475         destroy_inode_table(&inode_tab);
476         return ret;
477 }