]> wimlib.net Git - wimlib/blob - src/hardlink.c
Minor cleanups
[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
55 int init_inode_table(struct inode_table *table, size_t capacity)
56 {
57         table->array = CALLOC(capacity, sizeof(table->array[0]));
58         if (!table->array) {
59                 ERROR("Cannot initalize inode table: out of memory");
60                 return WIMLIB_ERR_NOMEM;
61         }
62         table->num_entries  = 0;
63         table->capacity     = capacity;
64         INIT_HLIST_HEAD(&table->extra_inodes);
65         return 0;
66 }
67
68
69 static inline size_t inode_link_count(const struct inode *inode)
70 {
71         const struct list_head *cur;
72         size_t size = 0;
73         list_for_each(cur, &inode->dentry_list)
74                 size++;
75         return size;
76 }
77
78 /*
79  * Insert a dentry into the inode table based on its inode
80  * ID.
81  *
82  * If there is already a dentry in the table having the same inode ID,
83  * and the inode ID is not 0, the dentry is added to the circular
84  * linked list for that inode.
85  *
86  * If the inode ID is 0, this indicates a dentry that's in a hard link
87  * inode by itself (has a link count of 1).  We can't insert it into the hash
88  * table itself because we don't know what inode numbers are available to
89  * give it (this could be kept track of but would be more difficult).  Instead
90  * we keep a linked list of the single dentries, and assign them inode
91  * numbers later.
92  */
93 int inode_table_insert(struct dentry *dentry, void *__table)
94 {
95         struct inode_table *table = __table;
96         struct inode *d_inode = dentry->d_inode;
97
98         if (d_inode->ino == 0) {
99                 /* Single inode--- Add to the list of extra inodes (we can't put
100                  * it in the table itself because all the singles have a link
101                  * inode ID of 0) */
102                 hlist_add_head(&d_inode->hlist, &table->extra_inodes);
103
104                 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
105                 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
106                 wimlib_assert(d_inode->link_count == 1);
107         } else {
108                 /* Inode that may have multiple corresponding dentries (the code
109                  * will work even if the inode actually contains only 1 dentry
110                  * though) */
111
112                 size_t pos;
113                 struct inode *inode;
114                 struct hlist_node *cur;
115
116                 /* Try adding to existing inode */
117                 pos = d_inode->ino % table->capacity;
118                 hlist_for_each_entry(inode, cur, &table->array[pos], hlist) {
119                         if (inode->ino == d_inode->ino) {
120                                 inode_add_dentry(dentry, inode);
121                                 inode->link_count++;
122                                 return 0;
123                         }
124                 }
125
126                 /* Add new inode to the table */
127                 hlist_add_head(&d_inode->hlist, &table->array[pos]);
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
133                 /* XXX Make the table grow when too many entries have been
134                  * inserted. */
135                 table->num_entries++;
136         }
137         return 0;
138 }
139
140 /* Assign the inode numbers to dentries in a inode table, and return the
141  * next available inode ID. */
142 u64 assign_inode_numbers(struct hlist_head *inode_list)
143 {
144         DEBUG("Assigning inode numbers");
145         struct inode *inode;
146         struct hlist_node *cur;
147         u64 cur_ino = 1;
148         hlist_for_each_entry(inode, cur, inode_list, hlist) {
149                 inode->ino = cur_ino;
150                 cur_ino++;
151         }
152         return cur_ino;
153 }
154
155
156 static void print_inode_dentries(const struct inode *inode)
157 {
158         struct dentry *dentry;
159         inode_for_each_dentry(dentry, inode)
160                 printf("`%s'\n", dentry->full_path_utf8);
161 }
162
163 static void inconsistent_inode(const struct inode *inode)
164 {
165         ERROR("An inconsistent hard link group that cannot be corrected has "
166               "been detected");
167         ERROR("The dentries are located at the following paths:");
168 #ifdef ENABLE_ERROR_MESSAGES
169         print_inode_dentries(inode);
170 #endif
171 }
172
173 static bool ref_inodes_consistent(const struct inode * restrict ref_inode_1,
174                                   const struct inode * restrict ref_inode_2)
175 {
176         wimlib_assert(ref_inode_1 != ref_inode_2);
177
178         if (ref_inode_1->num_ads != ref_inode_2->num_ads)
179                 return false;
180         if (ref_inode_1->security_id != ref_inode_2->security_id
181             || ref_inode_1->attributes != ref_inode_2->attributes)
182                 return false;
183         for (unsigned i = 0; i <= ref_inode_1->num_ads; i++) {
184                 const u8 *ref_1_hash, *ref_2_hash;
185                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
186                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
187                 if (!hashes_equal(ref_1_hash, ref_2_hash))
188                         return false;
189                 if (i && !ads_entries_have_same_name(&ref_inode_1->ads_entries[i - 1],
190                                                      &ref_inode_2->ads_entries[i - 1]))
191                         return false;
192
193         }
194         return true;
195 }
196
197 static bool inodes_consistent(const struct inode * restrict ref_inode,
198                               const struct inode * restrict inode)
199 {
200         wimlib_assert(ref_inode != inode);
201
202         if (ref_inode->num_ads != inode->num_ads &&
203             inode->num_ads != 0)
204                 return false;
205         if (ref_inode->security_id != inode->security_id
206             || ref_inode->attributes != inode->attributes)
207                 return false;
208         for (unsigned i = 0; i <= min(ref_inode->num_ads, inode->num_ads); i++) {
209                 const u8 *ref_hash, *hash;
210                 ref_hash = inode_stream_hash(ref_inode, i);
211                 hash = inode_stream_hash(inode, i);
212                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
213                         return false;
214                 if (i && !ads_entries_have_same_name(&ref_inode->ads_entries[i - 1],
215                                                      &inode->ads_entries[i - 1]))
216                         return false;
217         }
218         return true;
219 }
220
221 /* Fix up a "true" inode and check for inconsistencies */
222 static int fix_true_inode(struct inode *inode, struct hlist_head *inode_list)
223 {
224         struct dentry *dentry;
225         struct dentry *ref_dentry = NULL;
226         struct inode *ref_inode;
227         u64 last_ctime = 0;
228         u64 last_mtime = 0;
229         u64 last_atime = 0;
230
231         inode_for_each_dentry(dentry, inode) {
232                 if (!ref_dentry || dentry->d_inode->num_ads > ref_dentry->d_inode->num_ads)
233                         ref_dentry = dentry;
234                 if (dentry->d_inode->creation_time > last_ctime)
235                         last_ctime = dentry->d_inode->creation_time;
236                 if (dentry->d_inode->last_write_time > last_mtime)
237                         last_mtime = dentry->d_inode->last_write_time;
238                 if (dentry->d_inode->last_access_time > last_atime)
239                         last_atime = dentry->d_inode->last_access_time;
240         }
241
242         ref_inode = ref_dentry->d_inode;
243         ref_inode->link_count = 1;
244         hlist_add_head(&ref_inode->hlist, inode_list);
245
246         list_del(&inode->dentry_list);
247         list_add(&ref_inode->dentry_list, &ref_dentry->inode_dentry_list);
248
249         inode_for_each_dentry(dentry, ref_inode) {
250                 if (dentry != ref_dentry) {
251                         if (!inodes_consistent(ref_inode, dentry->d_inode)) {
252                                 inconsistent_inode(ref_inode);
253                                 return WIMLIB_ERR_INVALID_DENTRY;
254                         }
255                         /* Free the unneeded `struct inode'. */
256                         free_inode(dentry->d_inode);
257                         dentry->d_inode = ref_inode;
258                         ref_inode->link_count++;
259                 }
260         }
261         ref_inode->creation_time = last_ctime;
262         ref_inode->last_write_time = last_mtime;
263         ref_inode->last_access_time = last_atime;
264         wimlib_assert(inode_link_count(ref_inode) == ref_inode->link_count);
265         return 0;
266 }
267
268 /*
269  * Fixes up a nominal inode.
270  *
271  * By a nominal inode we mean a group of two or more dentries that share
272  * the same hard link group ID.
273  *
274  * If dentries in the inode are found to be inconsistent, we may split the inode
275  * into several "true" inodes.
276  *
277  * After splitting up each nominal inode into the "true" inodes we will
278  * canonicalize the link group by getting rid of all the unnecessary `struct
279  * inodes'.  There will be just one `struct inode' for each hard link group
280  * remaining.
281  */
282 static int
283 fix_nominal_inode(struct inode *inode, struct hlist_head *inode_list)
284 {
285         struct dentry *dentry;
286         struct hlist_node *cur, *tmp;
287         int ret;
288         size_t num_true_inodes;
289
290         wimlib_assert(inode->link_count == inode_link_count(inode));
291
292         LIST_HEAD(dentries_with_data_streams);
293         LIST_HEAD(dentries_with_no_data_streams);
294         HLIST_HEAD(true_inodes);
295
296         /* Create a list of dentries in the nominal inode that have at
297          * least one data stream with a non-zero hash, and another list that
298          * contains the dentries that have a zero hash for all data streams. */
299         inode_for_each_dentry(dentry, inode) {
300                 for (unsigned i = 0; i <= dentry->d_inode->num_ads; i++) {
301                         const u8 *hash;
302                         hash = inode_stream_hash(dentry->d_inode, i);
303                         if (!is_zero_hash(hash)) {
304                                 list_add(&dentry->tmp_list,
305                                          &dentries_with_data_streams);
306                                 goto next_dentry;
307                         }
308                 }
309                 list_add(&dentry->tmp_list,
310                          &dentries_with_no_data_streams);
311         next_dentry:
312                 ;
313         }
314
315         /* If there are no dentries with data streams, we require the nominal
316          * inode to be a true inode */
317         if (list_empty(&dentries_with_data_streams)) {
318         #ifdef ENABLE_DEBUG
319                 if (inode->link_count > 1) {
320                         DEBUG("Found link group of size %u without "
321                               "any data streams:", inode->link_count);
322                         print_inode_dentries(inode);
323                         DEBUG("We are going to interpret it as true "
324                               "link group, provided that the dentries "
325                               "are consistent.");
326                 }
327         #endif
328                 return fix_true_inode(inode, inode_list);
329         }
330
331         /* One or more dentries had data streams specified.  We check each of
332          * these dentries for consistency with the others to form a set of true
333          * inodes. */
334         num_true_inodes = 0;
335         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
336                 /* Look for a true inode that is consistent with this dentry and
337                  * add this dentry to it.  Or, if none of the true inodes are
338                  * consistent with this dentry, add a new one (if that happens,
339                  * we have split the hard link group). */
340                 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
341                         if (ref_inodes_consistent(inode, dentry->d_inode)) {
342                                 inode_add_dentry(dentry, inode);
343                                 goto next_dentry_2;
344                         }
345                 }
346                 num_true_inodes++;
347                 INIT_LIST_HEAD(&dentry->d_inode->dentry_list);
348                 inode_add_dentry(dentry, dentry->d_inode);
349                 hlist_add_head(&dentry->d_inode->hlist, &true_inodes);
350 next_dentry_2:
351                 ;
352         }
353
354         wimlib_assert(num_true_inodes != 0);
355
356         /* If there were dentries with no data streams, we require there to only
357          * be one true inode so that we know which inode to assign the
358          * streamless dentries to. */
359         if (!list_empty(&dentries_with_no_data_streams)) {
360                 if (num_true_inodes != 1) {
361                         ERROR("Hard inode ambiguity detected!");
362                         ERROR("We split up inode 0x%"PRIx64" due to "
363                               "inconsistencies,", inode->ino);
364                         ERROR("but dentries with no stream information remained. "
365                               "We don't know which inode");
366                         ERROR("to assign them to.");
367                         return WIMLIB_ERR_INVALID_DENTRY;
368                 }
369                 inode = container_of(true_inodes.first, struct inode, hlist);
370                 /* Assign the streamless dentries to the one and only true
371                  * inode. */
372                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
373                         inode_add_dentry(dentry, inode);
374         }
375         #ifdef ENABLE_DEBUG
376         if (num_true_inodes != 1) {
377                 inode = container_of(true_inodes.first, struct inode, hlist);
378
379                 printf("Split nominal inode 0x%"PRIx64" into %zu "
380                        "inodes:\n",
381                        inode->ino, num_true_inodes);
382                 puts("------------------------------------------------------------------------------");
383                 size_t i = 1;
384                 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
385                         printf("[Split inode %zu]\n", i++);
386                         print_inode_dentries(inode);
387                         putchar('\n');
388                 }
389                 puts("------------------------------------------------------------------------------");
390         }
391         #endif
392
393         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, hlist) {
394                 ret = fix_true_inode(inode, inode_list);
395                 if (ret != 0)
396                         return ret;
397         }
398         return 0;
399 }
400
401 /*
402  * Goes through each hard link group (dentries sharing the same hard link group
403  * ID field) that's been inserted into the inode table and shares the `struct
404  * inode's among members of each hard link group.
405  *
406  * In the process, the dentries belonging to each inode are checked for
407  * consistency.  If they contain data features that indicate they cannot really
408  * correspond to the same inode, this should be an error, but in reality this
409  * case needs to be handled, so we split the dentries into different inodes.
410  *
411  * After this function returns, the inodes are no longer in the inode table, and
412  * the inode table should be destroyed.  A list of the inodes, including all
413  * split inodes as well as the inodes that were good before, is returned in the
414  * list @inode_list.
415  */
416 int fix_inodes(struct inode_table *table, struct hlist_head *inode_list)
417 {
418         struct inode *inode;
419         struct hlist_node *cur, *tmp;
420         int ret;
421         INIT_HLIST_HEAD(inode_list);
422         for (u64 i = 0; i < table->capacity; i++) {
423                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], hlist) {
424                         ret = fix_nominal_inode(inode, inode_list);
425                         if (ret != 0)
426                                 return ret;
427                 }
428         }
429         hlist_for_each_safe(cur, tmp, &table->extra_inodes)
430                 hlist_add_head(cur, inode_list);
431         return 0;
432 }