split-index.c 14 KB

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