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