]> wimlib.net Git - wimlib/blob - src/hardlink.c
inode updates (IN PROGRESS)
[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 /* 
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->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                                 list_add(&dentry->inode_dentry_list,
121                                          &inode->dentry_list);
122                                 inode->link_count++;
123                                 return 0;
124                         }
125                 }
126
127                 /* Add new inode to the table */
128                 hlist_add_head(&d_inode->hlist, &table->array[pos]);
129
130                 wimlib_assert(d_inode->dentry_list.next == &dentry->inode_dentry_list);
131                 wimlib_assert(d_inode->dentry_list.prev == &dentry->inode_dentry_list);
132                 wimlib_assert(d_inode->link_count == 1);
133
134                 /* XXX Make the table grow when too many entries have been
135                  * inserted. */
136                 table->num_entries++;
137         }
138         return 0;
139 }
140
141 /* Assign the inode numbers to dentries in a inode table, and return the
142  * next available inode ID. */
143 u64 assign_inode_numbers(struct hlist_head *inode_list)
144 {
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
157 print_inode_dentries(const struct inode *inode)
158 {
159         struct dentry *dentry;
160         list_for_each_entry(dentry, &inode->dentry_list, inode_dentry_list)
161                 printf("`%s'\n", dentry->full_path_utf8);
162 }
163
164 static void inconsistent_inode(const struct inode *inode)
165 {
166         ERROR("An inconsistent hard link group that we cannot correct has been "
167               "detected");
168         ERROR("The dentries are located at the following paths:");
169         print_inode_dentries(inode);
170 }
171
172 static bool ref_inodes_consistent(const struct inode * restrict ref_inode_1,
173                                   const struct inode * restrict ref_inode_2)
174 {
175         wimlib_assert(ref_inode_1 != ref_inode_2);
176
177         if (ref_inode_1->num_ads != ref_inode_2->num_ads)
178                 return false;
179         if (ref_inode_1->security_id != ref_inode_2->security_id
180             || ref_inode_1->attributes != ref_inode_2->attributes)
181                 return false;
182         for (unsigned i = 0; i <= ref_inode_1->num_ads; i++) {
183                 const u8 *ref_1_hash, *ref_2_hash;
184                 ref_1_hash = inode_stream_hash(ref_inode_1, i);
185                 ref_2_hash = inode_stream_hash(ref_inode_2, i);
186                 if (!hashes_equal(ref_1_hash, ref_2_hash))
187                         return false;
188                 if (i && !ads_entries_have_same_name(ref_inode_1->ads_entries[i - 1],
189                                                      ref_inode_2->ads_entries[i - 1]))
190                         return false;
191
192         }
193         return true;
194 }
195
196 static bool inodes_consistent(const struct inode * restrict ref_inode,
197                               const struct inode * restrict inode)
198 {
199         wimlib_assert(ref_inode != inode);
200
201         if (ref_inode->num_ads != inode->num_ads &&
202             inode->num_ads != 0)
203                 return false;
204         if (ref_inode->security_id != inode->security_id
205             || ref_inode->attributes != inode->attributes)
206                 return false;
207         for (unsigned i = 0; i <= min(ref_inode->num_ads, inode->num_ads); i++) {
208                 const u8 *ref_hash, *hash;
209                 ref_hash = inode_stream_hash(ref_inode, i);
210                 hash = inode_stream_hash(inode, i);
211                 if (!hashes_equal(ref_hash, hash) && !is_zero_hash(hash))
212                         return false;
213                 if (i && !ads_entries_have_same_name(ref_inode->ads_entries[i - 1],
214                                                      inode->ads_entries[i - 1]))
215                         return false;
216         }
217         return true;
218 }
219
220 #ifdef ENABLE_DEBUG
221 static void
222 print_dentry_list(const struct dentry *first_dentry)
223 {
224         const struct dentry *dentry = first_dentry;
225         do {
226                 printf("`%s'\n", dentry->full_path_utf8);
227         } while ((dentry = container_of(dentry->inode_dentry_list.next,
228                                         struct dentry,
229                                         inode_dentry_list)) != first_dentry);
230 }
231
232 #endif
233
234
235 /* Fix up a "true" inode and check for inconsistencies */
236 static int fix_true_inode(struct inode *inode)
237 {
238         struct dentry *dentry;
239         struct dentry *ref_dentry = NULL;
240         struct inode *ref_inode;
241         u64 last_ctime = 0;
242         u64 last_mtime = 0;
243         u64 last_atime = 0;
244         bool found_short_name = false;
245
246         inode_for_each_dentry(dentry, inode) {
247                 if (!ref_dentry || ref_dentry->inode->num_ads == 0)
248                         ref_dentry = dentry;
249                 if (dentry->short_name_len) {
250                         if (found_short_name) {
251                                 ERROR("Multiple short names in hard link "
252                                       "group!");
253                                 inconsistent_inode(inode);
254                                 return WIMLIB_ERR_INVALID_DENTRY;
255                         } else {
256                                 found_short_name = true;
257                         }
258                 }
259                 if (dentry->inode->creation_time > last_ctime)
260                         last_ctime = dentry->inode->creation_time;
261                 if (dentry->inode->last_write_time > last_mtime)
262                         last_mtime = dentry->inode->last_write_time;
263                 if (dentry->inode->last_access_time > last_atime)
264                         last_atime = dentry->inode->last_access_time;
265         }
266
267         ref_inode = ref_dentry->inode;
268         ref_inode->link_count = 1;
269
270         inode_for_each_dentry(dentry, inode) {
271                 if (dentry != ref_dentry) {
272                         if (!inodes_consistent(ref_inode, dentry->inode)) {
273                                 inconsistent_inode(dentry->inode);
274                                 return WIMLIB_ERR_INVALID_DENTRY;
275                         }
276                         /* Free the unneeded `struct inode'. */
277                         free_inode(dentry->inode);
278                         dentry->inode = ref_inode;
279                         ref_inode->link_count++;
280                 }
281         }
282         ref_inode->creation_time = last_ctime;
283         ref_inode->last_write_time = last_mtime;
284         ref_inode->last_access_time = last_atime;
285         wimlib_assert(inode_link_count(ref_inode) == ref_inode->link_count);
286         return 0;
287 }
288
289 /* 
290  * Fixes up a nominal inode.
291  *
292  * By a nominal inode we mean a group of two or more dentries that share
293  * the same hard link group ID.
294  *
295  * If dentries in the inode are found to be inconsistent, we may split the inode
296  * into several "true" inodes.  @new_inodes points to a linked list of
297  * these split inodes, and if we create any, they will be added to this list.
298  *
299  * After splitting up each nominal inode into the "true" inodes we
300  * will canonicalize the link group by getting rid of all the superfluous
301  * `struct inodes'.  There will be just one `struct inode' for each hard link
302  * group remaining.
303  */
304 static int
305 fix_nominal_inode(struct inode *inode, struct hlist_head *inode_list)
306 {
307         struct dentry *dentry, *ref_dentry;
308         struct hlist_node *cur, *tmp;
309         int ret;
310         size_t num_true_inodes;
311
312         wimlib_assert(inode->link_count == inode_link_count(inode));
313
314         LIST_HEAD(dentries_with_data_streams);
315         LIST_HEAD(dentries_with_no_data_streams);
316         HLIST_HEAD(true_inodes);
317
318         /* Create a list of dentries in the nominal inode that have at
319          * least one data stream with a non-zero hash, and another list that
320          * contains the dentries that have a zero hash for all data streams. */
321         inode_for_each_dentry(dentry, inode) {
322                 for (unsigned i = 0; i <= dentry->inode->num_ads; i++) {
323                         const u8 *hash;
324                         hash = inode_stream_hash(dentry->inode, i);
325                         if (!is_zero_hash(hash)) {
326                                 list_add(&dentry->tmp_list,
327                                          &dentries_with_data_streams);
328                                 goto next_dentry;
329                         }
330                 }
331                 list_add(&dentry->tmp_list,
332                          &dentries_with_no_data_streams);
333         next_dentry:
334                 ;
335         }
336
337         /* If there are no dentries with data streams, we require the nominal
338          * inode to be a true inode */
339         if (list_empty(&dentries_with_data_streams)) {
340                 DEBUG("No data streams");
341         #ifdef ENABLE_DEBUG
342                 {
343                         if (inode->link_count > 1) {
344                                 DEBUG("Found link group of size %zu without "
345                                       "any data streams:", inode->link_count);
346                                 print_inode_dentries(inode);
347                                 DEBUG("We are going to interpret it as true "
348                                       "link group, provided that the dentries "
349                                       "are consistent.");
350                         }
351                 }
352         #endif
353                 hlist_add_head(&inode->hlist, inode_list);
354                 return fix_true_inode(inode);
355         }
356
357         /* One or more dentries had data streams specified.  We check each of
358          * these dentries for consistency with the others to form a set of true
359          * inodes. */
360         num_true_inodes = 0;
361         list_for_each_entry(dentry, &dentries_with_data_streams, tmp_list) {
362                 /* Look for a true inode that is consistent with
363                  * this dentry and add this dentry to it.  Or, if none
364                  * of the true inodes are consistent with this
365                  * dentry, make a new one. */
366                 hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
367                         if (ref_inodes_consistent(inode, dentry->inode)) {
368                                 list_add(&dentry->inode_dentry_list,
369                                          &inode->dentry_list);
370                                 goto next_dentry_2;
371                         }
372                 }
373                 num_true_inodes++;
374                 INIT_LIST_HEAD(&dentry->inode->dentry_list);
375                 list_add(&dentry->inode_dentry_list, &dentry->inode->dentry_list);
376                 hlist_add_head(&dentry->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 true hard link");
393                         ERROR("inode to assign them to.");
394                         return WIMLIB_ERR_INVALID_DENTRY;
395                 }
396                 inode = container_of(true_inodes.first,
397                                      struct inode,
398                                      hlist);
399                 /* Assign the streamless dentries to the one and only true
400                  * inode. */
401                 list_for_each_entry(dentry, &dentries_with_no_data_streams, tmp_list)
402                         list_add(&dentry->inode_dentry_list, &inode->dentry_list);
403         }
404         if (num_true_inodes != 1) {
405                 #ifdef ENABLE_DEBUG
406                 {
407                         inode = container_of(true_inodes.first,
408                                              struct inode,
409                                              hlist);
410
411                         printf("Split nominal inode 0x%"PRIx64" into %zu "
412                                "inodes:\n",
413                                inode->ino, num_true_inodes);
414                         puts("------------------------------------------------------------------------------");
415                         size_t i = 1;
416                         hlist_for_each_entry(inode, cur, &true_inodes, hlist) {
417                                 printf("[Split inode %zu]\n", i++);
418                                 print_inode_dentries(inode);
419                                 putchar('\n');
420                         }
421                         puts("------------------------------------------------------------------------------");
422                 }
423                 #endif
424         }
425
426         hlist_for_each_entry_safe(inode, cur, tmp, &true_inodes, hlist) {
427                 hlist_add_head(&inode->hlist, inode_list);
428                 ret = fix_true_inode(inode);
429                 if (ret != 0)
430                         return ret;
431         }
432         return 0;
433 }
434
435 /*
436  * Goes through each inode and shares the inodes among members of a hard
437  * inode.
438  *
439  * In the process, the dentries in each inode are checked for consistency.
440  * If they contain data features that indicate they cannot really be in the same
441  * inode, this should be an error, but in reality this case needs to
442  * be handled, so we split the dentries into different inodes.
443  */
444 int fix_inodes(struct inode_table *table, struct hlist_head *inode_list)
445 {
446         struct inode *inode;
447         struct hlist_node *cur, *tmp;
448         int ret;
449         INIT_HLIST_HEAD(inode_list);
450         for (u64 i = 0; i < table->capacity; i++) {
451                 hlist_for_each_entry_safe(inode, cur, tmp, &table->array[i], hlist) {
452                         ret = fix_nominal_inode(inode, inode_list);
453                         if (ret != 0)
454                                 return ret;
455                 }
456         }
457         hlist_for_each_safe(cur, tmp, &table->extra_inodes)
458                 hlist_add_head(cur, inode_list);
459         return 0;
460 }