]>
Commit | Line | Data |
---|---|---|
5fc2fc8f NTND |
1 | #include "cache.h" |
2 | #include "split-index.h" | |
96a1d8d3 | 3 | #include "ewah/ewok.h" |
5fc2fc8f NTND |
4 | |
5 | struct split_index *init_split_index(struct index_state *istate) | |
6 | { | |
7 | if (!istate->split_index) { | |
451b66c5 JS |
8 | if (istate->sparse_index) |
9 | die(_("cannot use split index with a sparse index")); | |
10 | ||
ca56dadb | 11 | CALLOC_ARRAY(istate->split_index, 1); |
5fc2fc8f NTND |
12 | istate->split_index->refcount = 1; |
13 | } | |
14 | return istate->split_index; | |
15 | } | |
16 | ||
17 | int read_link_extension(struct index_state *istate, | |
18 | const void *data_, unsigned long sz) | |
19 | { | |
20 | const unsigned char *data = data_; | |
21 | struct split_index *si; | |
76b07b37 NTND |
22 | int ret; |
23 | ||
2182abd9 | 24 | if (sz < the_hash_algo->rawsz) |
5fc2fc8f NTND |
25 | return error("corrupt link extension (too short)"); |
26 | si = init_split_index(istate); | |
92e2cab9 | 27 | oidread(&si->base_oid, data); |
2182abd9 | 28 | data += the_hash_algo->rawsz; |
29 | sz -= the_hash_algo->rawsz; | |
76b07b37 NTND |
30 | if (!sz) |
31 | return 0; | |
32 | si->delete_bitmap = ewah_new(); | |
33 | ret = ewah_read_mmap(si->delete_bitmap, data, sz); | |
34 | if (ret < 0) | |
35 | return error("corrupt delete bitmap in link extension"); | |
36 | data += ret; | |
37 | sz -= ret; | |
38 | si->replace_bitmap = ewah_new(); | |
39 | ret = ewah_read_mmap(si->replace_bitmap, data, sz); | |
40 | if (ret < 0) | |
41 | return error("corrupt replace bitmap in link extension"); | |
42 | if (ret != sz) | |
5fc2fc8f NTND |
43 | return error("garbage at the end of link extension"); |
44 | return 0; | |
45 | } | |
46 | ||
47 | int write_link_extension(struct strbuf *sb, | |
48 | struct index_state *istate) | |
49 | { | |
50 | struct split_index *si = istate->split_index; | |
2182abd9 | 51 | strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz); |
96a1d8d3 NTND |
52 | if (!si->delete_bitmap && !si->replace_bitmap) |
53 | return 0; | |
be0d9d53 NTND |
54 | ewah_serialize_strbuf(si->delete_bitmap, sb); |
55 | ewah_serialize_strbuf(si->replace_bitmap, sb); | |
5fc2fc8f NTND |
56 | return 0; |
57 | } | |
58 | ||
59 | static void mark_base_index_entries(struct index_state *base) | |
60 | { | |
61 | int i; | |
62 | /* | |
63 | * To keep track of the shared entries between | |
64 | * istate->base->cache[] and istate->cache[], base entry | |
65 | * position is stored in each base entry. All positions start | |
832c0e5e | 66 | * from 1 instead of 0, which is reserved to say "this is a new |
5fc2fc8f NTND |
67 | * entry". |
68 | */ | |
69 | for (i = 0; i < base->cache_nr; i++) | |
70 | base->cache[i]->index = i + 1; | |
71 | } | |
72 | ||
c18b80a0 NTND |
73 | void move_cache_to_base_index(struct index_state *istate) |
74 | { | |
75 | struct split_index *si = istate->split_index; | |
76 | int i; | |
77 | ||
78 | /* | |
8e72d675 JM |
79 | * If there was a previous base index, then transfer ownership of allocated |
80 | * entries to the parent index. | |
c18b80a0 | 81 | */ |
8e72d675 JM |
82 | if (si->base && |
83 | si->base->ce_mem_pool) { | |
84 | ||
44c7e1a7 EN |
85 | if (!istate->ce_mem_pool) { |
86 | istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool)); | |
87 | mem_pool_init(istate->ce_mem_pool, 0); | |
88 | } | |
8e72d675 JM |
89 | |
90 | mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool); | |
91 | } | |
92 | ||
ca56dadb | 93 | CALLOC_ARRAY(si->base, 1); |
c18b80a0 NTND |
94 | si->base->version = istate->version; |
95 | /* zero timestamp disables racy test in ce_write_index() */ | |
96 | si->base->timestamp = istate->timestamp; | |
97 | ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc); | |
98 | si->base->cache_nr = istate->cache_nr; | |
8e72d675 JM |
99 | |
100 | /* | |
101 | * The mem_pool needs to move with the allocated entries. | |
102 | */ | |
103 | si->base->ce_mem_pool = istate->ce_mem_pool; | |
104 | istate->ce_mem_pool = NULL; | |
105 | ||
45ccef87 | 106 | COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr); |
c18b80a0 NTND |
107 | mark_base_index_entries(si->base); |
108 | for (i = 0; i < si->base->cache_nr; i++) | |
109 | si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE; | |
110 | } | |
111 | ||
76b07b37 NTND |
112 | static void mark_entry_for_delete(size_t pos, void *data) |
113 | { | |
114 | struct index_state *istate = data; | |
115 | if (pos >= istate->cache_nr) | |
116 | die("position for delete %d exceeds base index size %d", | |
117 | (int)pos, istate->cache_nr); | |
118 | istate->cache[pos]->ce_flags |= CE_REMOVE; | |
2034e848 | 119 | istate->split_index->nr_deletions++; |
76b07b37 NTND |
120 | } |
121 | ||
122 | static void replace_entry(size_t pos, void *data) | |
123 | { | |
124 | struct index_state *istate = data; | |
125 | struct split_index *si = istate->split_index; | |
126 | struct cache_entry *dst, *src; | |
b3c96fb1 | 127 | |
76b07b37 NTND |
128 | if (pos >= istate->cache_nr) |
129 | die("position for replacement %d exceeds base index size %d", | |
130 | (int)pos, istate->cache_nr); | |
131 | if (si->nr_replacements >= si->saved_cache_nr) | |
132 | die("too many replacements (%d vs %d)", | |
133 | si->nr_replacements, si->saved_cache_nr); | |
134 | dst = istate->cache[pos]; | |
135 | if (dst->ce_flags & CE_REMOVE) | |
136 | die("entry %d is marked as both replaced and deleted", | |
137 | (int)pos); | |
138 | src = si->saved_cache[si->nr_replacements]; | |
b3c96fb1 NTND |
139 | if (ce_namelen(src)) |
140 | die("corrupt link extension, entry %d should have " | |
141 | "zero length name", (int)pos); | |
76b07b37 NTND |
142 | src->index = pos + 1; |
143 | src->ce_flags |= CE_UPDATE_IN_BASE; | |
b3c96fb1 NTND |
144 | src->ce_namelen = dst->ce_namelen; |
145 | copy_cache_entry(dst, src); | |
a849735b | 146 | discard_cache_entry(src); |
76b07b37 NTND |
147 | si->nr_replacements++; |
148 | } | |
149 | ||
5fc2fc8f NTND |
150 | void merge_base_index(struct index_state *istate) |
151 | { | |
152 | struct split_index *si = istate->split_index; | |
76b07b37 | 153 | unsigned int i; |
5fc2fc8f NTND |
154 | |
155 | mark_base_index_entries(si->base); | |
76b07b37 NTND |
156 | |
157 | si->saved_cache = istate->cache; | |
158 | si->saved_cache_nr = istate->cache_nr; | |
159 | istate->cache_nr = si->base->cache_nr; | |
160 | istate->cache = NULL; | |
161 | istate->cache_alloc = 0; | |
5fc2fc8f | 162 | ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc); |
45ccef87 | 163 | COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr); |
76b07b37 NTND |
164 | |
165 | si->nr_deletions = 0; | |
166 | si->nr_replacements = 0; | |
167 | ewah_each_bit(si->replace_bitmap, replace_entry, istate); | |
168 | ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate); | |
169 | if (si->nr_deletions) | |
6fdc2057 | 170 | remove_marked_cache_entries(istate, 0); |
76b07b37 NTND |
171 | |
172 | for (i = si->nr_replacements; i < si->saved_cache_nr; i++) { | |
b3c96fb1 NTND |
173 | if (!ce_namelen(si->saved_cache[i])) |
174 | die("corrupt link extension, entry %d should " | |
175 | "have non-zero length name", i); | |
76b07b37 NTND |
176 | add_index_entry(istate, si->saved_cache[i], |
177 | ADD_CACHE_OK_TO_ADD | | |
ce7c614b | 178 | ADD_CACHE_KEEP_CACHE_TREE | |
76b07b37 NTND |
179 | /* |
180 | * we may have to replay what | |
181 | * merge-recursive.c:update_stages() | |
182 | * does, which has this flag on | |
183 | */ | |
184 | ADD_CACHE_SKIP_DFCHECK); | |
185 | si->saved_cache[i] = NULL; | |
186 | } | |
187 | ||
188 | ewah_free(si->delete_bitmap); | |
189 | ewah_free(si->replace_bitmap); | |
88ce3ef6 | 190 | FREE_AND_NULL(si->saved_cache); |
76b07b37 NTND |
191 | si->delete_bitmap = NULL; |
192 | si->replace_bitmap = NULL; | |
76b07b37 | 193 | si->saved_cache_nr = 0; |
5fc2fc8f NTND |
194 | } |
195 | ||
e3d83798 SG |
196 | /* |
197 | * Compare most of the fields in two cache entries, i.e. all except the | |
198 | * hashmap_entry and the name. | |
199 | */ | |
200 | static int compare_ce_content(struct cache_entry *a, struct cache_entry *b) | |
201 | { | |
202 | const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID | | |
203 | CE_EXTENDED_FLAGS; | |
204 | unsigned int ce_flags = a->ce_flags; | |
205 | unsigned int base_flags = b->ce_flags; | |
206 | int ret; | |
207 | ||
208 | /* only on-disk flags matter */ | |
209 | a->ce_flags &= ondisk_flags; | |
210 | b->ce_flags &= ondisk_flags; | |
211 | ret = memcmp(&a->ce_stat_data, &b->ce_stat_data, | |
212 | offsetof(struct cache_entry, name) - | |
09751bf1 AH |
213 | offsetof(struct cache_entry, oid)) || |
214 | !oideq(&a->oid, &b->oid); | |
e3d83798 SG |
215 | a->ce_flags = ce_flags; |
216 | b->ce_flags = base_flags; | |
217 | ||
218 | return ret; | |
219 | } | |
220 | ||
5fc2fc8f NTND |
221 | void prepare_to_write_split_index(struct index_state *istate) |
222 | { | |
223 | struct split_index *si = init_split_index(istate); | |
96a1d8d3 NTND |
224 | struct cache_entry **entries = NULL, *ce; |
225 | int i, nr_entries = 0, nr_alloc = 0; | |
226 | ||
227 | si->delete_bitmap = ewah_new(); | |
228 | si->replace_bitmap = ewah_new(); | |
229 | ||
230 | if (si->base) { | |
231 | /* Go through istate->cache[] and mark CE_MATCHED to | |
232 | * entry with positive index. We'll go through | |
233 | * base->cache[] later to delete all entries in base | |
753c4515 | 234 | * that are not marked with either CE_MATCHED or |
96a1d8d3 NTND |
235 | * CE_UPDATE_IN_BASE. If istate->cache[i] is a |
236 | * duplicate, deduplicate it. | |
237 | */ | |
238 | for (i = 0; i < istate->cache_nr; i++) { | |
239 | struct cache_entry *base; | |
96a1d8d3 | 240 | ce = istate->cache[i]; |
e3d83798 SG |
241 | if (!ce->index) { |
242 | /* | |
243 | * During simple update index operations this | |
244 | * is a cache entry that is not present in | |
245 | * the shared index. It will be added to the | |
246 | * split index. | |
247 | * | |
248 | * However, it might also represent a file | |
249 | * that already has a cache entry in the | |
250 | * shared index, but a new index has just | |
251 | * been constructed by unpack_trees(), and | |
252 | * this entry now refers to different content | |
253 | * than what was recorded in the original | |
254 | * index, e.g. during 'read-tree -m HEAD^' or | |
255 | * 'checkout HEAD^'. In this case the | |
256 | * original entry in the shared index will be | |
257 | * marked as deleted, and this entry will be | |
258 | * added to the split index. | |
259 | */ | |
96a1d8d3 | 260 | continue; |
e3d83798 | 261 | } |
96a1d8d3 | 262 | if (ce->index > si->base->cache_nr) { |
4c490f3d SG |
263 | BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d", |
264 | ce->index, si->base->cache_nr); | |
96a1d8d3 NTND |
265 | } |
266 | ce->ce_flags |= CE_MATCHED; /* or "shared" */ | |
267 | base = si->base->cache[ce->index - 1]; | |
5581a019 SG |
268 | if (ce == base) { |
269 | /* The entry is present in the shared index. */ | |
270 | if (ce->ce_flags & CE_UPDATE_IN_BASE) { | |
271 | /* | |
272 | * Already marked for inclusion in | |
273 | * the split index, either because | |
274 | * the corresponding file was | |
275 | * modified and the cached stat data | |
276 | * was refreshed, or because there | |
277 | * is already a replacement entry in | |
278 | * the split index. | |
279 | * Nothing more to do here. | |
280 | */ | |
281 | } else if (!ce_uptodate(ce) && | |
282 | is_racy_timestamp(istate, ce)) { | |
283 | /* | |
284 | * A racily clean cache entry stored | |
285 | * only in the shared index: it must | |
286 | * be added to the split index, so | |
287 | * the subsequent do_write_index() | |
288 | * can smudge its stat data. | |
289 | */ | |
290 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
291 | } else { | |
292 | /* | |
293 | * The entry is only present in the | |
294 | * shared index and it was not | |
295 | * refreshed. | |
296 | * Just leave it there. | |
297 | */ | |
298 | } | |
96a1d8d3 | 299 | continue; |
5581a019 | 300 | } |
96a1d8d3 NTND |
301 | if (ce->ce_namelen != base->ce_namelen || |
302 | strcmp(ce->name, base->name)) { | |
303 | ce->index = 0; | |
304 | continue; | |
305 | } | |
e3d83798 SG |
306 | /* |
307 | * This is the copy of a cache entry that is present | |
308 | * in the shared index, created by unpack_trees() | |
309 | * while it constructed a new index. | |
310 | */ | |
311 | if (ce->ce_flags & CE_UPDATE_IN_BASE) { | |
312 | /* | |
313 | * Already marked for inclusion in the split | |
314 | * index, either because the corresponding | |
315 | * file was modified and the cached stat data | |
316 | * was refreshed, or because the original | |
317 | * entry already had a replacement entry in | |
318 | * the split index. | |
319 | * Nothing to do. | |
320 | */ | |
5581a019 SG |
321 | } else if (!ce_uptodate(ce) && |
322 | is_racy_timestamp(istate, ce)) { | |
323 | /* | |
324 | * A copy of a racily clean cache entry from | |
325 | * the shared index. It must be added to | |
326 | * the split index, so the subsequent | |
327 | * do_write_index() can smudge its stat data. | |
328 | */ | |
329 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
e3d83798 SG |
330 | } else { |
331 | /* | |
332 | * Thoroughly compare the cached data to see | |
333 | * whether it should be marked for inclusion | |
334 | * in the split index. | |
335 | * | |
336 | * This comparison might be unnecessary, as | |
337 | * code paths modifying the cached data do | |
338 | * set CE_UPDATE_IN_BASE as well. | |
339 | */ | |
340 | if (compare_ce_content(ce, base)) | |
341 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
342 | } | |
a849735b | 343 | discard_cache_entry(base); |
96a1d8d3 NTND |
344 | si->base->cache[ce->index - 1] = ce; |
345 | } | |
346 | for (i = 0; i < si->base->cache_nr; i++) { | |
347 | ce = si->base->cache[i]; | |
348 | if ((ce->ce_flags & CE_REMOVE) || | |
349 | !(ce->ce_flags & CE_MATCHED)) | |
350 | ewah_set(si->delete_bitmap, i); | |
351 | else if (ce->ce_flags & CE_UPDATE_IN_BASE) { | |
352 | ewah_set(si->replace_bitmap, i); | |
b3c96fb1 | 353 | ce->ce_flags |= CE_STRIP_NAME; |
96a1d8d3 NTND |
354 | ALLOC_GROW(entries, nr_entries+1, nr_alloc); |
355 | entries[nr_entries++] = ce; | |
356 | } | |
4bddd983 TG |
357 | if (is_null_oid(&ce->oid)) |
358 | istate->drop_cache_tree = 1; | |
96a1d8d3 NTND |
359 | } |
360 | } | |
361 | ||
362 | for (i = 0; i < istate->cache_nr; i++) { | |
363 | ce = istate->cache[i]; | |
364 | if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) { | |
b3c96fb1 | 365 | assert(!(ce->ce_flags & CE_STRIP_NAME)); |
96a1d8d3 NTND |
366 | ALLOC_GROW(entries, nr_entries+1, nr_alloc); |
367 | entries[nr_entries++] = ce; | |
368 | } | |
369 | ce->ce_flags &= ~CE_MATCHED; | |
370 | } | |
371 | ||
372 | /* | |
373 | * take cache[] out temporarily, put entries[] in its place | |
374 | * for writing | |
375 | */ | |
376 | si->saved_cache = istate->cache; | |
5fc2fc8f | 377 | si->saved_cache_nr = istate->cache_nr; |
96a1d8d3 NTND |
378 | istate->cache = entries; |
379 | istate->cache_nr = nr_entries; | |
5fc2fc8f NTND |
380 | } |
381 | ||
382 | void finish_writing_split_index(struct index_state *istate) | |
383 | { | |
384 | struct split_index *si = init_split_index(istate); | |
96a1d8d3 NTND |
385 | |
386 | ewah_free(si->delete_bitmap); | |
387 | ewah_free(si->replace_bitmap); | |
388 | si->delete_bitmap = NULL; | |
389 | si->replace_bitmap = NULL; | |
390 | free(istate->cache); | |
391 | istate->cache = si->saved_cache; | |
5fc2fc8f NTND |
392 | istate->cache_nr = si->saved_cache_nr; |
393 | } | |
394 | ||
395 | void discard_split_index(struct index_state *istate) | |
396 | { | |
397 | struct split_index *si = istate->split_index; | |
398 | if (!si) | |
399 | return; | |
400 | istate->split_index = NULL; | |
401 | si->refcount--; | |
402 | if (si->refcount) | |
403 | return; | |
404 | if (si->base) { | |
405 | discard_index(si->base); | |
406 | free(si->base); | |
407 | } | |
408 | free(si); | |
409 | } | |
045113a5 NTND |
410 | |
411 | void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce) | |
412 | { | |
413 | if (ce->index && | |
414 | istate->split_index && | |
415 | istate->split_index->base && | |
416 | ce->index <= istate->split_index->base->cache_nr && | |
417 | ce == istate->split_index->base->cache[ce->index - 1]) | |
418 | ce->ce_flags |= CE_REMOVE; | |
419 | else | |
a849735b | 420 | discard_cache_entry(ce); |
045113a5 | 421 | } |
078a58e8 NTND |
422 | |
423 | void replace_index_entry_in_base(struct index_state *istate, | |
75b7b971 BW |
424 | struct cache_entry *old_entry, |
425 | struct cache_entry *new_entry) | |
078a58e8 | 426 | { |
75b7b971 | 427 | if (old_entry->index && |
078a58e8 NTND |
428 | istate->split_index && |
429 | istate->split_index->base && | |
75b7b971 BW |
430 | old_entry->index <= istate->split_index->base->cache_nr) { |
431 | new_entry->index = old_entry->index; | |
432 | if (old_entry != istate->split_index->base->cache[new_entry->index - 1]) | |
a849735b | 433 | discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]); |
75b7b971 | 434 | istate->split_index->base->cache[new_entry->index - 1] = new_entry; |
078a58e8 NTND |
435 | } |
436 | } | |
cef4fc7e CC |
437 | |
438 | void add_split_index(struct index_state *istate) | |
439 | { | |
440 | if (!istate->split_index) { | |
441 | init_split_index(istate); | |
442 | istate->cache_changed |= SPLIT_INDEX_ORDERED; | |
443 | } | |
444 | } | |
445 | ||
446 | void remove_split_index(struct index_state *istate) | |
447 | { | |
64719b11 | 448 | if (istate->split_index) { |
6e37c8ed NTND |
449 | if (istate->split_index->base) { |
450 | /* | |
451 | * When removing the split index, we need to move | |
452 | * ownership of the mem_pool associated with the | |
453 | * base index to the main index. There may be cache entries | |
454 | * allocated from the base's memory pool that are shared with | |
455 | * the_index.cache[]. | |
456 | */ | |
457 | mem_pool_combine(istate->ce_mem_pool, | |
458 | istate->split_index->base->ce_mem_pool); | |
8e72d675 | 459 | |
6e37c8ed NTND |
460 | /* |
461 | * The split index no longer owns the mem_pool backing | |
462 | * its cache array. As we are discarding this index, | |
463 | * mark the index as having no cache entries, so it | |
464 | * will not attempt to clean up the cache entries or | |
465 | * validate them. | |
466 | */ | |
8e72d675 | 467 | istate->split_index->base->cache_nr = 0; |
6e37c8ed | 468 | } |
8e72d675 JM |
469 | |
470 | /* | |
471 | * We can discard the split index because its | |
472 | * memory pool has been incorporated into the | |
473 | * memory pool associated with the the_index. | |
474 | */ | |
475 | discard_split_index(istate); | |
476 | ||
64719b11 JH |
477 | istate->cache_changed |= SOMETHING_CHANGED; |
478 | } | |
cef4fc7e | 479 | } |