File: gate.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (1589 lines) | stat: -rw-r--r-- 46,898 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
/*
 *  gate.c
 *
 *  $Id$
 *
 *  Hyperspace and gate.
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "sqlnode.h"

#define BUF_SEEMS_LEAF(buf, map) \
  (map->pm_count < 2 || KV_LEAF_PTR != IE_KEY_VERSION (buf->bd_buffer + map->pm_entries[1]))



long in_while_read;
long second_reads;
int is_read_pending;
int checkpoint_while_read = 0;
unsigned int bp_hit_ctr;




int
itc_async_read_1 (it_cursor_t * itc, dp_addr_t dp, dp_addr_t phys_dp,
		  buffer_desc_t * buf, buffer_desc_t * decoy)
{
#if 0
#error Not supposed to be on with 5.0
  /* if the iq for the dp to read is busy, put the read in the iq instead of doing it here.
   * Called outside map, returns 1 inside map if the read was scheduled, otherwise returns 0 outside map. */
  int n;
  io_queue_t * iq = db_io_queue (ITC_STORAGE (itc), phys_dp);
  buffer_desc_t * queued;
  if (!iq || !iq_is_on ())
    return 0;
  queued = iq->iq_first;
  for (n = 0; n < 10; n++)
    {
      if (!queued)
	return 0;
      queued = queued->bd_iq_next;
    }
  /* items in the queue.  Add to the queue. */
  buf->bd_being_read = 1;
  ITC_IN_MAP (itc);
  buf->bd_page = 0;
  isp_set_buffer (itc->itc_space, dp, phys_dp, buf);
  buf->bd_write_waiting = NULL;
  itc->itc_n_reads++;
  ITC_MARK_READ (itc);
  buf->bd_waiting_read = decoy->bd_waiting_read;
  buf->bd_readers = 0;
  ITC_LEAVE_MAP (itc);
  iq_schedule (&buf, 1);
  ITC_IN_MAP (itc);
  return 1;
#else
  return 0;
#endif
}



#ifdef ADAPTIVE_LAND

void
itc_adaptive_read_inc (it_cursor_t * itc, buffer_desc_t * dest_buf)
{
  page_map_t * map = dest_buf->bd_content_map;
  if (!dest_buf->bd_readers && map && BUF_SEEMS_LEAF (dest_buf, map))
    {
      BD_SET_IS_WRITE (dest_buf, 1);
      itc->itc_dive_mode = PA_WRITE;
    }
  else
    dest_buf->bd_readers++;
}

#define ADAPTIVE_READ_INC(itc, dest_buf) \
  itc_adaptive_read_inc (itc, dest_buf)

#ifdef PAGE_DEBUG
#define AL_WAIT_SET_WRITER(buf) \
  { \
    if ((buf) && (buf)->bd_is_write) \
      (buf)->bd_writer = THREAD_CURRENT_THREAD; \
    BUF_DBG_ENTER (buf); \
  }
#else
#define AL_WAIT_SET_WRITER(buf)
#endif
#else

#define ADAPTIVE_READ_INC(itc, dest_buf) \
  dest_buf->bd_readers++

#define AL_WAIT_SET_WRITER(buf)

#endif

buffer_desc_t * bounds_check_buf;

int
DBGP_NAME (page_wait_access) (DBGP_PARAMS it_cursor_t * itc, dp_addr_t dp,  buffer_desc_t * buf_from,
    buffer_desc_t ** buf_ret, int mode, int max_change)
{
  buffer_desc_t decoy;
  buffer_desc_t *buf;
  dp_addr_t phys_dp;
  itc->itc_to_reset = RWG_NO_WAIT;
  itc->itc_max_transit_change = max_change;
  itc->itc_must_kill_trx = 0;
  if (!dp)
    GPF_T1 ("Zero DP in page_fault_map_sem");

  if (buf_from)
    {
      ITC_IN_TRANSIT (itc, dp, buf_from->bd_page);
    }
  else
    ASSERT_IN_MAP (itc->itc_tree, dp);

  buf = IT_DP_TO_BUF (itc->itc_tree, dp);
  if (!buf)
    {
      ra_req_t * ra = NULL;
      IT_DP_REMAP (itc->itc_tree, dp, phys_dp);
#ifdef MTX_DEBUG
      em_check_dp (itc->itc_tree->it_extent_map, phys_dp);
      if (phys_dp != dp)
	em_check_dp (itc->itc_tree->it_extent_map, dp);
#endif
      if ((DP_DELETED == phys_dp || dbs_is_free_page (itc->itc_tree->it_storage, phys_dp))
	  && !strchr (wi_inst.wi_open_mode, 'a'))
	{
	  log_error ("Reference to page with free remap dp = %ld, remap = %ld",
		     (long) phys_dp, (long) dp);
	  if (0 && DBS_PAGE_IN_RANGE (itc->itc_tree->it_storage, phys_dp))
	    dbs_page_allocated (itc->itc_tree->it_storage, phys_dp);
	  else
	    {
	      *buf_ret = PF_OF_DELETED;
	      itc->itc_must_kill_trx = 1;
	      itc->itc_to_reset = RWG_WAIT_ANY;
	      ITC_LEAVE_MAPS (itc);
	      return RWG_WAIT_ANY;
	    }
	}
      memset (&decoy, 0, sizeof (buffer_desc_t));
      decoy.bd_being_read = 1;
      if (PA_READ == mode)
	decoy.bd_readers = 1;
      else
	BD_SET_IS_WRITE (&decoy, 1);
      sethash (DP_ADDR2VOID (dp), &IT_DP_MAP (itc->itc_tree, dp)->itm_dp_to_buf, (void*)&decoy);
      ITC_LEAVE_MAPS (itc);
      buf = bp_get_buffer (NULL, BP_BUF_REQUIRED);
      is_read_pending++;
      buf->bd_being_read = 1;
      buf->bd_page = dp;
      buf->bd_storage = itc->itc_tree->it_storage;
      buf->bd_physical_page = phys_dp;
      BD_SET_IS_WRITE (buf, 0);
      buf->bd_write_waiting = NULL;
      if (buf_from && !itc->itc_landed)
	ra = itc_read_aside (itc, buf_from, dp);
      itc->itc_n_reads++;
      ITC_MARK_READ (itc);
      buf->bd_tree = itc->itc_tree;
      buf_disk_read (buf);
      is_read_pending--;
      if (ra)
	itc_read_ahead_blob (itc, ra, RAB_SPECULATIVE);

      if (buf_from)
	{
	  ITC_IN_TRANSIT (itc, dp, buf_from->bd_page)
	    }
	  else
	    ITC_IN_KNOWN_MAP (itc, dp);
      sethash (DP_ADDR2VOID (dp), &IT_DP_MAP (itc->itc_tree, dp)->itm_dp_to_buf, (void*) buf);
      buf->bd_pl = (page_lock_t *) gethash (DP_ADDR2VOID (dp), &IT_DP_MAP (itc->itc_tree, dp)->itm_locks);
      itc->itc_pl = buf->bd_pl;
      DBG_PT_READ (buf, itc->itc_ltrx);
      buf->bd_being_read = 0;
      buf->bd_readers = decoy.bd_readers;
      BD_SET_IS_WRITE (buf, decoy.bd_is_write);
      buf->bd_read_waiting = decoy.bd_read_waiting;
      buf->bd_write_waiting = decoy.bd_write_waiting;
      /* this thread has read the buffer and is first in.  It will now let others in if this is read access and reads are waiting.  Otherwise what waits gets released when the itc leaves the page */
      /* There is no change.  The only one in is this one and it did only the read */
      if (buf_from)
	page_leave_inner (buf_from);
      page_mark_change (buf, RWG_WAIT_DISK);
      if (PA_READ == mode)
	page_release_read (buf);

      ITC_LEAVE_MAPS (itc);
      /* complete transit.  This counts as no change since itc was all the time in source and dest was acquired without possibility of interference */
      BUF_BOUNDS_CHECK (buf);
      buf_ext_check (buf);
      *buf_ret = buf;
      BUF_DBG_ENTER (buf);
      return itc->itc_to_reset;
    }
  if (buf->bd_being_read)
    {
      second_reads++;
      if (PA_READ == mode  && !buf->bd_is_write)
	{
	  /* cheat here.  No registration needed because a read-read wait is safe */
	  page_read_queue_add (buf, itc);
	  if (buf_from)
	    page_leave_inner (buf_from);
	  ITC_SEM_WAIT (itc);
	  *buf_ret = itc->itc_buf_entered; /* since this was a read-read, the  itc is now in one the buffer, out of map, since the thread which did the read put it there */
	  AL_WAIT_SET_WRITER (*buf_ret);
	  return itc->itc_to_reset;
	}
      if (itc->itc_landed && buf_from
	  && RWG_WAIT_NO_ENTRY_IF_WAIT != max_change)
	{
	  if (itc->itc_desc_order)
	    itc_skip_entry (itc, buf_from);
	  itc_register (itc, buf_from);
	}
      itc->itc_to_reset = RWG_WAIT_NO_CHANGE;
      if (PA_READ == mode)
	page_read_queue_add (buf, itc);
      else
	page_write_queue_add (buf, itc);
      if (buf_from && RWG_WAIT_NO_ENTRY_IF_WAIT != max_change)
	page_leave_inner (buf_from);
      ITC_SEM_WAIT (itc);
      *buf_ret = itc->itc_buf_entered;
      /*  transit complete. The caller for landed transit must check if unregister is needed.  If this was for no entry, as in up trans wait, then the itc will still be on the source. */
      AL_WAIT_SET_WRITER (*buf_ret);
      return itc->itc_to_reset;
    }
  /* there is a buffer.  See if can transit */
  if (PA_READ == mode)
    {
      if (!buf->bd_is_write
	  && !buf->bd_write_waiting)
	{
	  buf->bd_readers++;
	  if (buf_from)
	    page_leave_inner (buf_from);
	  itc->itc_pl = buf->bd_pl;
	  ITC_LEAVE_MAPS (itc);
	  BUF_BOUNDS_CHECK (buf);
	  *buf_ret = buf;
	  BUF_TOUCH (buf);
	  BUF_DBG_ENTER (buf);
	  return  itc->itc_to_reset;
	}
      else
	{
	  page_read_queue_add (buf, itc);
	  if (buf_from)
	    page_leave_inner (buf_from);
	  ITC_SEM_WAIT (itc);
	}
    }
  else
    {
      if (!buf->bd_is_write
	  && 0 == buf->bd_readers)
	{
	  if (buf_from)
	    page_leave_inner (buf_from);
	  BD_SET_IS_WRITE (buf, 1);
	  ITC_LEAVE_MAPS (itc);
	  BUF_BOUNDS_CHECK (buf);
	  itc->itc_pl = buf->bd_pl;
	  *buf_ret = buf;
	  BUF_TOUCH (buf);
	  BUF_DBG_ENTER (buf);
	  return itc->itc_to_reset;
	}
      else
	{
	  itc->itc_to_reset = RWG_WAIT_NO_CHANGE;
	  page_write_queue_add (buf, itc);
	  if (itc->itc_landed && buf_from  && RWG_WAIT_NO_ENTRY_IF_WAIT  != max_change)
	    {
	      if (itc->itc_desc_order)
		itc_skip_entry (itc, buf_from);
	      itc_register (itc, buf_from);
	    }
	  if (buf_from && RWG_WAIT_NO_ENTRY_IF_WAIT != max_change)
	    page_leave_inner (buf_from);
	  ITC_SEM_WAIT (itc);
	}
    }
  if (itc->itc_to_reset <= max_change && itc->itc_to_reset != RWG_WAIT_NO_ENTRY_IF_WAIT)
    {
      *buf_ret = buf;
      AL_WAIT_SET_WRITER (buf);
      BUF_BOUNDS_CHECK (buf);
      BUF_TOUCH (buf);
    }
  else
    *buf_ret = NULL;
  ASSERT_OUTSIDE_MAPS (itc);
  return (itc->itc_to_reset);
}


void
page_release_read (buffer_desc_t * buf)
{
  it_cursor_t *waiting = buf->bd_read_waiting;
  it_cursor_t *next;
  buf->bd_read_waiting = NULL;
  while (waiting)
    {
      next = waiting->itc_next_waiting;
      waiting->itc_read_waits++;
      dbg_printf (("Release busted at %ld,\n", buf->bd_page));
      if (waiting->itc_to_reset <= waiting->itc_max_transit_change)
	{
	  BUF_BOUNDS_CHECK (buf);
	  waiting->itc_buf_entered = buf;
	  waiting->itc_pl = buf->bd_pl;
#ifdef ADAPTIVE_LAND
	  assert (PA_WRITE != waiting->itc_dive_mode);
	  ADAPTIVE_READ_INC (waiting, buf);
	  if (PA_WRITE == waiting->itc_dive_mode)
	    {
	      /* if adaptive landing made this a write, then free no more cursors. */
#ifdef PAGE_DEBUG
	      buf->bd_writer = waiting->itc_thread;
#endif
	      buf->bd_read_waiting = next;
	      semaphore_leave (waiting->itc_thread->thr_sem);
	      return;
	    }
#else
	  buf->bd_readers++;
#endif
	}
      else
	{
	  rdbg_printf (("pw reset itc %p chg %d max %d landed %d a for %d itc_page %d \n",
			waiting, (int) waiting->itc_to_reset,
			(int) waiting->itc_max_transit_change, (int)waiting->itc_landed,
			buf->bd_page, waiting->itc_page));
	  waiting->itc_buf_entered = NULL;
	  TC (tc_page_wait_reset);
	}
      semaphore_leave (waiting->itc_thread->thr_sem);
      waiting = next;
    }
}

#define W_RELEASED 1
#define W_NOT_RELEASED 0

int
page_release_writes (buffer_desc_t * buf)
{
  it_cursor_t *waiting = buf->bd_write_waiting;
  while (waiting)
    {
      it_cursor_t *next = waiting->itc_next_waiting;
      waiting->itc_write_waits++;
      if (waiting->itc_to_reset <= waiting->itc_max_transit_change)
	{
	  rdbg_printf (("pw release write itc %x chg %d max %d w for %ld \n",
			waiting, (int) waiting->itc_to_reset, (int) waiting->itc_max_transit_change, buf->bd_page));
	  waiting->itc_buf_entered = buf;
	  BUF_BOUNDS_CHECK (buf);
	  waiting->itc_pl = buf->bd_pl;
	  BD_SET_IS_WRITE (buf, 1);
#ifdef PAGE_DEBUG
	  buf->bd_writer = waiting->itc_thread;
#endif
	  buf->bd_write_waiting = next;
	  semaphore_leave (waiting->itc_thread->thr_sem);
	  return W_RELEASED;
	}
      else
	{
	  rdbg_printf (("pw reset itc %x chg %d max %d landed %d w for %ld itc_page %ld \n",
			waiting, (int) waiting->itc_to_reset,
			(int) waiting->itc_max_transit_change, (int)waiting->itc_landed,
			buf->bd_page, waiting->itc_page));
	  TC (tc_page_wait_reset);
	  if (RWG_WAIT_NO_ENTRY_IF_WAIT == waiting->itc_max_transit_change)
	    waiting->itc_to_reset = RWG_WAIT_NO_ENTRY_IF_WAIT;
	  /*the point above is to set the itc_to_reset to indicate wait+no entry.  The case where there is entry with no wait has itc_to_reset set to RWG_NO_WAIT */
	  waiting->itc_buf_entered = NULL;
	  buf->bd_write_waiting = next;
	  semaphore_leave (waiting->itc_thread->thr_sem);
	}
      waiting = next;
    }
  buf->bd_write_waiting = NULL;
  return W_NOT_RELEASED;
}



void
DBGP_NAME (page_leave_inner) (DBGP_PARAMS buffer_desc_t * buf)
{
#ifdef MTX_DEBUG
  if (!is_crash_dump && buf->bd_tree)
    ASSERT_IN_MAP (buf->bd_tree, buf->bd_page);
#endif
  BUF_BOUNDS_CHECK(buf);
  BUF_DBG_LEAVE (buf);
  if (buf->bd_readers)
    {
      buf->bd_readers--;
      if (0 == buf->bd_readers)
	{
	  if (!buf->bd_write_waiting || W_NOT_RELEASED == page_release_writes (buf))
	    {
	      if (buf->bd_read_waiting)
		page_release_read (buf);
	    }
	}
      else if (buf->bd_readers < 0)
	GPF_T;			/* Negative readers */
    }
  else
    {
      if (!buf->bd_is_write)
	GPF_T1 ("Leaving a buffer with neither read or write access");
      BD_SET_IS_WRITE (buf, 0);
      if (!buf->bd_write_waiting || W_NOT_RELEASED == page_release_writes (buf))
	{
	  if (buf->bd_read_waiting)
	    page_release_read (buf);
	}
    }
  /* set the space to null for deld buffer after all else.  This is so that buffer replacement will wait for the page map so it does not do dirty reads on intermediate states of releasing the buffer */
}


void
page_leave_as_deleted (buffer_desc_t * buf)
{
  buf_set_last (buf);
  page_mark_change (buf, RWG_WAIT_ANY + 1);
  page_leave_inner (buf);
  if (buf->bd_is_write || buf->bd_readers)
    GPF_T1 ("when a buffer was left for deleted, none of the waiting itc should have entered");
  buf->bd_tree = NULL;
  buf->bd_page = 0;
}


void
page_mark_change (buffer_desc_t * buf, int change)
{
  it_cursor_t *itc;
  if (!buf->bd_is_write && !buf->bd_readers)
    GPF_T1 ("Must have read or write access on buf to mark change");
  ASSERT_IN_MAP (buf->bd_tree, buf->bd_page);
  for (itc = buf->bd_write_waiting; itc; itc = itc->itc_next_waiting)
    itc->itc_to_reset = MAX (itc->itc_to_reset, change);
  for (itc = buf->bd_read_waiting; itc; itc = itc->itc_next_waiting)
    itc->itc_to_reset = MAX (itc->itc_to_reset, change);
}


void
page_write_queue_add (buffer_desc_t * buf, it_cursor_t * itc)
{
  it_cursor_t **last = &buf->bd_write_waiting;
  ITC_KEY_INC (itc, key_write_wait);
  TC (tc_write_wait);
  itc->itc_thread = THREAD_CURRENT_THREAD;
  while (*last)
    last = &((*last)->itc_next_waiting);
  *last = itc;
  itc->itc_next_waiting = NULL;
}

void
page_read_queue_add (buffer_desc_t * buf, it_cursor_t * itc)
{
  TC (tc_read_wait);
  ITC_KEY_INC (itc, key_read_wait);
  itc->itc_thread = THREAD_CURRENT_THREAD;
  itc->itc_next_waiting = buf->bd_read_waiting;
  buf->bd_read_waiting = itc;
}



#if 0

void
it_clear_right_edge_cache (dp_addr_t dp)
{
  dbe_key_t *key = (dbe_key_t *) gethash (DP_ADDR2VOID (dp), it->it_right_edges);
  if (key)
    {
      key->key_is_last_right_edge = 0;
      remhash (DP_ADDR2VOID (dp), it->it_right_edges);
    }
}


void
itc_set_right_edge_cache (it_cursor_t * itc)
{
  if (itc->itc_search_mode == SM_INSERT && itc->itc_is_index_end)
    {
      itc_clear_tight_edge_cache (itc->itc_page);
    }
}
#endif


int
itc_try_land (it_cursor_t * itc, buffer_desc_t ** buf_ret)
{
  buffer_desc_t *buf = *buf_ret;
  if (PA_WRITE == itc->itc_dive_mode)
    {
      ITC_MARK_LANDED (itc);
      itc->itc_landed = 1;
      itc->itc_to_reset = RWG_NO_WAIT;
      return RWG_NO_WAIT;
    }
  if (buf->bd_is_write)
    GPF_T1 ("Can't land while buffer being written");

  itc->itc_to_reset = RWG_NO_WAIT;
  ITC_IN_KNOWN_MAP (itc, itc->itc_page);

  if (buf->bd_readers > 1)
    {
      /* the page is being read also by others.  Must wait.  It is in principle possible that a write itc comes here, via transact, via reenter, via landed transit.
       * The write itc would come in first.  It could split or delete the page.  Hence, if transit change is more than data, just reset.
       * Registration is not needed here since position does not have to be maintained since any change that would move the itc results in reset */
      TC (tc_try_land_write);
      ITC_KEY_INC (itc, key_landing_wait);
      itc->itc_to_reset = RWG_NO_WAIT;
      itc->itc_max_transit_change = RWG_WAIT_DATA;
      page_write_queue_add (buf, itc);
      buf->bd_readers--;
      ITC_SEM_WAIT (itc);
      if (itc->itc_to_reset <= RWG_WAIT_DATA)
	{
#ifdef PAGE_DEBUG
	  buf->bd_writer = THREAD_CURRENT_THREAD;
#endif
	  itc->itc_pl = buf->bd_pl;
	  itc->itc_landed = 1;
	}
      else
	TC (tc_try_land_reset);
    }
  else
    {
      buf->bd_readers = 0;
      BD_SET_IS_WRITE (buf, 1);
      ITC_FIND_PL (itc, buf);
      ITC_LEAVE_MAP_NC (itc);
      itc->itc_landed = 1;
    }
  if (itc->itc_landed)
    {
      dbe_key_t *key = itc->itc_insert_key;
      if (key)
	{
	  ITC_MARK_LANDED (itc);
	  if (itc->itc_page == key->key_last_page)
	    {
	      key->key_n_last_page_hits++;
	      key->key_total_last_page_hits++;
	    }
	  else
	    {
	      key->key_last_page = itc->itc_page;
	      key->key_n_last_page_hits = 0;
	    }
	}
    }
  return (itc->itc_to_reset);
}


buffer_desc_t *
pl_enter (placeholder_t * pl, it_cursor_t * ctl_itc)
{
  /* get the buffer that corresponds to the pl.  The pl may move anytime.  On return, the buffer is on excl access and the pl agrees with the buffer.
   * ctl_itc is used for entry and maps etc.  it can be == pl. */
  buffer_desc_t *buf;
  dp_addr_t volatile target_dp;
  dp_addr_t volatile target_dp_1;
  pl->itc_owns_page  = 0; /* can have committed or moved etc since last on the page */
  if (!pl->itc_is_registered)
    GPF_T1 ("pl must be registered in pl_enter");
retry:
  if (ctl_itc->itc_ltrx && ctl_itc->itc_ltrx->lt_status != LT_CLOSING)
    {
      /* make an exception for closing since deletes inside commit or rollback can call this */
      if (!ctl_itc->itc_fail_context) GPF_T1 ("must have fail ctx in pl_enter");
      CHECK_TRX_DEAD (ctl_itc, NULL, ITC_BUST_CONTINUABLE);
    }

  target_dp_1 = pl->itc_page;
  ITC_IN_KNOWN_MAP (ctl_itc, target_dp_1);
  target_dp = pl->itc_page; /* now it is in the map but it can still move because anybody owning the specific buffer can move it */
  /* check that we are at least in the right map. */
  if (target_dp != target_dp_1)
    {
      ITC_LEAVE_MAP_NC (ctl_itc);
      TC (tc_pl_moved_in_reentry);
      goto retry;
    }
  if (pl->itc_bp.bp_transiting)
    {
      ITC_LEAVE_MAP_NC (ctl_itc);
      TC (tc_enter_transiting_bm_inx);
      virtuoso_sleep (0, 100);
      goto retry;
    }
  buf = pl->itc_buf_registered;
  if (!buf
      || buf->bd_page != target_dp_1)
    {
      /* the buf could just disappear for transit or could have changed so we're no longer in the right map */
      TC (tc_pl_moved_in_reentry);
      ITC_LEAVE_MAP_NC (ctl_itc);
      goto retry;
    }
  if (!buf->bd_is_write && !buf->bd_readers
      && BUF_NONE_WAITING (buf))
    {
      if (buf->bd_page != pl->itc_page)
	{
	  if (pl->itc_page == target_dp_1)
	    GPF_T1 ("the registered buf is is a different page than the itc page but the pl has not moved during the reentry");
	  TC (tc_pl_moved_in_reentry);
	  ITC_LEAVE_MAP_NC (ctl_itc);
	  goto retry;
	}
      BD_SET_IS_WRITE (buf, 1);
      ITC_LEAVE_MAP_NC (ctl_itc);
      return buf;
    }
  ITC_KEY_INC (ctl_itc, key_pl_wait);
  page_wait_access (ctl_itc, target_dp, NULL, &buf, PA_WRITE, RWG_WAIT_KEY);
  if (ctl_itc->itc_to_reset > RWG_WAIT_KEY)
    {
      TC (tc_reentry_split);
      if (ctl_itc->itc_must_kill_trx) /* reference to free remap, kill trx */
	{
	  ctl_itc->itc_must_kill_trx = 0;
	  if (!wi_inst.wi_checkpoint_atomic && ctl_itc->itc_ltrx)
	    itc_bust_this_trx (ctl_itc, NULL, ITC_BUST_THROW);
	}
      goto retry;
    }
  if (PF_OF_DELETED == buf)
    {
      if (pl->itc_page == target_dp)
	GPF_T1 ("reentry of registered on deld page and the pl had not moved");
      ITC_LEAVE_MAP_NC (ctl_itc);
      goto retry;
    }
  if (pl->itc_page != target_dp)
    {
      TC (tc_pl_moved_in_reentry);
      ITC_LEAVE_MAPS (ctl_itc);
      page_leave_outside_map (buf);
      goto retry;
    }
  return buf;
}


buffer_desc_t *
page_reenter_excl (it_cursor_t * itc)
{
  buffer_desc_t *buf;
  buf = pl_enter ((placeholder_t *) itc, itc);
  itc_unregister_inner (itc, buf, 0);
  ITC_FIND_PL (itc, buf);
  ITC_LEAVE_MAPS (itc); /* only because itc_find_pl may enter ifdef mtx_debug */
  /* the itc_page, itc_map_pos are correct since it was registered */
  return buf;
}


void
itc_fix_back_link (it_cursor_t * itc, buffer_desc_t ** buf, dp_addr_t dp_from,
		   buffer_desc_t * old_buf, dp_addr_t back_link, int was_after_wait)
{
  if (itc->itc_read_waits || itc->itc_write_waits)
    was_after_wait = 1;

  rdbg_printf ((
		"Bad parent link in %ld, coming from %ld, parent link = %ld.\n",
		(*buf)->bd_page, old_buf->bd_page, back_link));
  if (1 || was_after_wait)
    {
      int was_landed = itc->itc_landed, pos;
      buffer_desc_t * parent;
      if (!itc->itc_landed)
	{
	  itc_try_land (itc, buf);
	  if (!itc->itc_landed)
	    {
	      *buf = itc_reset (itc);
	      return;
	    }
	}
      parent = itc_write_parent (itc, *buf);
      if (!parent && was_landed)
	return;
      if (!parent)
	{
	  itc_page_leave (itc, *buf);
	  *buf = itc_reset (itc);
	  return;
	}
      pos = page_find_leaf (parent, (*buf)->bd_page);
      if (pos == ITC_AT_END)
	{
	  log_error ("A bad parent link has been detected by looking at the would be parent and finding no leaf pointer.  The pages follow, parent first.");
	  dbg_page_map (parent);
	  dbg_page_map (*buf);
	  if (!correct_parent_links)
	    {
	      log_error ("Consult your documentation on how to recover from "
			 "this situation.  Doe a crush dump and restore or try the autocorrect options in the virtuoso.ini file");
	      GPF_T1 ("fatal consistency check failure");
	    }
	  LONG_SET ((*buf)->bd_buffer + DP_PARENT, dp_from);
	  (*buf)->bd_is_dirty = 1;
	  log_info ("Bad parent link corrected");
	}
      page_leave_outside_map (parent);
      if (!was_landed)
	{
	  itc_page_leave (itc, *buf);
	  *buf = itc_reset (itc);
	}
      return;
    }
}


long tc_excl_dive;


void
itc_dive_transit (it_cursor_t * itc, buffer_desc_t ** buf_ret, dp_addr_t to)
{
  buffer_desc_t *old_buf = *buf_ret;
  int waited = 0;
  buffer_desc_t *volatile dest_buf = NULL;
  index_tree_t * tree = itc->itc_tree;
  dk_hash_t * ht;
  dp_addr_t dp_from = itc->itc_page, back_link;
  it_map_t * itm1 = IT_DP_MAP (tree, dp_from);
  it_map_t * itm2 = IT_DP_MAP (tree, to);
  /* cache place, use when looking for next sibling *
   *itc->itc_parent_page = itc->itc_page;
   *itc->itc_pos_on_parent = itc->itc_position;
   */

  if (PA_READ != itc->itc_dive_mode)
    {
      TC (tc_excl_dive);
      goto general_case;
    }
  ht = &itm2->itm_dp_to_buf;
  if (itm2 <= itm1)
    {
      /* here we lock dest mtx first, translate the address and last of all get the source mtx and dec the read */
      mutex_enter (&itm2->itm_mtx);
      GETHASH (DP_ADDR2VOID (to), ht, dest_buf, not_found_2_1);
      if (dest_buf->bd_is_write || !BUF_NONE_WAITING (dest_buf))
	goto not_found_2_1;
      if (itm1 != itm2)
	mutex_enter (&itm1->itm_mtx);
      ADAPTIVE_READ_INC (itc, dest_buf);
      if (itm1 != itm2)
	mutex_leave (&itm2->itm_mtx);
      if (BUF_NONE_WAITING (old_buf))
	{
	  BUF_DBG_LEAVE_INL (old_buf);
	old_buf->bd_readers--;
	}
      else
	page_leave_inner (old_buf);
      /* If the itms were different, the target itm is already exited */
      mutex_leave (&itm1->itm_mtx);
      itc->itc_pl = dest_buf->bd_pl;
	*buf_ret = dest_buf;
      goto check_link;
    not_found_2_1:
      itc->itc_itm1 = itm2;
      if (itm1 != itm2)
	{
	  itc->itc_itm2 = itm1;
	  mutex_enter (&itm1->itm_mtx);
	}
      goto general_case;
    }
  else
#if DIVE_WDEAD
    {
      /* now the mtx for the destination is higher than for the source.  We lock in wrong order and reverse if we can't have the second mtx */
      mutex_enter (&itm2->itm_mtx);
      GETHASH (DP_ADDR2VOID (to), ht, dest_buf, not_found_1_2);
      if (dest_buf->bd_is_write || !BUF_NONE_WAITING (dest_buf))
	goto not_found_1_2;
      if (!mutex_try_enter (&itm1->itm_mtx))
	goto dive_deadlock;
      dest_buf->bd_readers++;
      mutex_leave (&itm2->itm_mtx);
      if (BUF_NONE_WAITING (old_buf))
	{
	  BUF_DBG_LEAVE_INL (old_buf);
	old_buf->bd_readers--;
	}
      else
	page_leave_inner (old_buf);
      mutex_leave (&itm1->itm_mtx);
      *buf_ret = dest_buf;
      goto check_link;
    not_found_1_2:
      if (mutex_try_enter (&itm1->itm_mtx))
	{
	  itc->itc_itm1 = itm1;
	  itc->itc_itm2 = itm2;
	  /* we own both mtxs and itc->itm* is set, sp [page_wait_access will not enter transit again */
	  goto general_case;
	}
      dive_deadlock:
      /* we tried lock the higher first and failed to get the lower. */
      mutex_leave (&itm2->itm_mtx);
      TC (tc_dive_would_deadlock);
      /* itc->itm1 and 2 are NULL, we are not inside any mtx, page_wait_access will then enter the transit in the general case */
      goto general_case;
    }
#else
    {
      /* now the mtx for the destination is higher than for the source.  */
      mutex_enter (&itm1->itm_mtx);
      mutex_enter (&itm2->itm_mtx);
      GETHASH (DP_ADDR2VOID (to), ht, dest_buf, not_found_1_2);
      if (dest_buf->bd_is_write || !BUF_NONE_WAITING (dest_buf))
	goto not_found_1_2;
      ADAPTIVE_READ_INC (itc, dest_buf);
      mutex_leave (&itm2->itm_mtx);
      if (BUF_NONE_WAITING (old_buf))
	{
	  BUF_DBG_LEAVE_INL (old_buf);
	old_buf->bd_readers--;
	}
      else
	page_leave_inner (old_buf);
      mutex_leave (&itm1->itm_mtx);
      *buf_ret = dest_buf;
      itc->itc_pl = dest_buf->bd_pl;
      goto check_link;
    not_found_1_2:
      itc->itc_itm1 = itm1;
      itc->itc_itm2 = itm2;
      /* we own both mtxs and itc->itm* is set, sp [page_wait_access will not enter transit again */
    }
#endif

 general_case:
  page_wait_access (itc, to, *buf_ret, buf_ret, itc->itc_dive_mode, RWG_WAIT_KEY);
  if (itc->itc_to_reset >= RWG_WAIT_SPLIT)
    {
      TC (tc_dive_split);
      *buf_ret = itc_reset (itc);
      itc->itc_read_waits += 1000;
      return;
    }
  if (itc->itc_read_waits || itc->itc_write_waits)
    waited = 1;
  dest_buf = *buf_ret;
  goto check_link_2;
 check_link:
  BUF_TOUCH (dest_buf);
  BUF_DBG_ENTER_INL (dest_buf);
 check_link_2:
  back_link = LONG_REF (dest_buf->bd_buffer + DP_PARENT);
  itc->itc_page = dest_buf->bd_page;
  if (back_link != dp_from)
    itc_fix_back_link (itc, buf_ret, dp_from, old_buf, back_link, waited);
  itc->itc_map_pos = ITC_AT_END;
#ifndef NDEBUG
  if ((*buf_ret)->bd_readers <= 0 && !(*buf_ret)->bd_is_write)
    GPF_T1 ("dive transit ends in bd_readers <= 0 or is_write > 0");
#endif
  if ((*buf_ret)->bd_is_dirty && (*buf_ret)->bd_physical_page != (*buf_ret)->bd_page)
    {
      it_map_t * itm = IT_DP_MAP (tree, to);
      mutex_enter (&itm->itm_mtx);
      if (!gethash ((void*)(void*)(ptrlong)to, &itm->itm_remap))
	GPF_T;
      mutex_leave (&itm->itm_mtx);
    }
}


void
DBGP_NAME (itc_landed_down_transit) (DBGP_PARAMS it_cursor_t * itc, buffer_desc_t ** buf_ret, dp_addr_t to)
{
  buffer_desc_t *old_buf = *buf_ret;
  int waited = 0;
  dp_addr_t dp_from = itc->itc_page, back_link;
  page_map_t * pm;
  /* itc->itc_parent_page = itc->itc_page;
   * itc->itc_pos_on_parent = itc->itc_position; */
  /* commented out.  cache place, use when looking for next sibling */

#ifndef NDEBUG
  if (!old_buf->bd_is_write) GPF_T1 ("landed transit got no write");
#endif
  itc->itc_is_on_row = 1;
  DBGP_NAME (page_wait_access) (DBGP_ARGS itc, to, *buf_ret, buf_ret, PA_WRITE, RWG_NO_WAIT);
  if (itc->itc_to_reset > RWG_NO_WAIT)
    {
      /* there was a wait and the itc was registered.  To unregister, reenter the page and go back to itc_search to see where to goo. The leaf could have been deld or split */
      TC (tc_dtrans_split);
      *buf_ret = page_reenter_excl (itc);
      if (itc->itc_desc_order
	  && (itc->itc_is_on_row || ITC_AT_END == itc->itc_map_pos))
	{
	  /* The itrc was registered on the entry to the right of the leaf targeted.  If the leaf targeted was the rightmost entry, the itc was registered at 0, meaning past end.  So if it was 0, put the itc to the rightmost, else one to the left of the registered pos. If the registered pos was the leftmost, then going one to the left gives 0, which is OK since itc_search will then go to the parent */
	  if (ITC_AT_END == itc->itc_map_pos)
	    {
	      page_map_t * pm = (*buf_ret)->bd_content_map;
	      itc->itc_map_pos = pm->pm_count - 1;
	    }
	  else
	    itc_prev_entry (itc, *buf_ret);
	}
      return;
    }
  if (itc->itc_is_registered)
    GPF_T1 ("itc in down transit registered even if no wait");
  itc->itc_page = (*buf_ret)->bd_page;
#ifdef NEW_HASH
  itc_hi_source_page_used (itc, itc->itc_page);
#endif

  ITC_FIND_PL (itc, *buf_ret);
  back_link = LONG_REF ((*buf_ret)->bd_buffer + DP_PARENT);
  if (back_link != dp_from)
    itc_fix_back_link (itc, buf_ret, dp_from, old_buf, back_link, waited);
  ITC_LEAVE_MAPS (itc);
  itc->itc_nth_seq_page++;
  pm = (*buf_ret)->bd_content_map;
  if (itc->itc_desc_order)
    {
        itc->itc_map_pos = pm->pm_count - 1;
    }
  else
    itc->itc_map_pos = 0;
}

void
DBGP_NAME (itc_down_transit) (DBGP_PARAMS it_cursor_t * itc, buffer_desc_t ** buf_ret, dp_addr_t to)
{
  if (itc->itc_landed)
    DBGP_NAME (itc_landed_down_transit) (DBGP_ARGS itc, buf_ret, to);
  else
    itc_dive_transit (itc, buf_ret, to);
}


int
itc_up_transit (it_cursor_t * itc, buffer_desc_t ** buf_ret)
{
  buffer_desc_t * parent_buf = NULL;
  int up_ctr = 0;
  dp_addr_t up;
  volatile dp_addr_t * up_field;
  up_field = (dp_addr_t *) ((*buf_ret)->bd_buffer + DP_PARENT);
up_again:
  up = LONG_REF (up_field);
  ITC_IN_TRANSIT (itc, up, (*buf_ret)->bd_page)
    if (! LONG_REF (up_field))
      {
	/* The parent got deld by itc_single_leaf_delete while waiting for parent access */
	ITC_LEAVE_MAPS (itc);
	return DVC_INDEX_END;
      }
  if (LONG_REF (up_field) != up)
      {
	ITC_LEAVE_MAPS(itc);
	TC (tc_up_transit_parent_change);
	goto up_again;
      }
    parent_buf = NULL;
  page_wait_access (itc, up, *buf_ret, &parent_buf, PA_WRITE, RWG_WAIT_NO_ENTRY_IF_WAIT);
  /* the no entry case applies only to a wait. Also, if there is a wait, the original buffer will stay occupied by this itc.
  * So if we got no buffer, there was a wait and if we got one the transit had no wait but could have disk read. */
  if (!parent_buf)
    {
      up_ctr ++;
      rdbg_printf (("up transit wait itc %x from %ld to %ld\n", itc, (*buf_ret)->bd_page, up));
      TC (tc_up_transit_wait);
      goto up_again;
    }
  *buf_ret = parent_buf;
#ifndef NDEBUG
  if (!(*buf_ret)->bd_is_write)
    GPF_T1 ("up transit leaves with no write on buffer");
#endif
  itc->itc_pl = parent_buf->bd_pl;
  itc->itc_page = parent_buf->bd_page;
#ifdef NEW_HASH
  itc_hi_source_page_used (itc, itc->itc_page);
#endif
  itc->itc_map_pos = ITC_AT_END;
  return DVC_MATCH;
}

void
itc_root_cache_enter (it_cursor_t * itc, buffer_desc_t ** buf_ret, dp_addr_t leaf)
{
  index_tree_t * tree = itc->itc_tree;
  it_map_t * itm = IT_DP_MAP (tree, leaf);
  buffer_desc_t * buf = NULL;
  if (!leaf)
    {
      tree->it_is_single_page = 1;
      *buf_ret = NULL;
    }
  mutex_enter (&itm->itm_mtx);
  if (tree->it_root_image_version != itc->itc_root_image_version)
    {
      mutex_leave (&itm->itm_mtx);
      *buf_ret = NULL;
      TC (tc_root_image_miss);
      return;
    }
  GETHASH (DP_ADDR2VOID (leaf), (&itm->itm_dp_to_buf), buf, not_present);
  if (buf && !buf->bd_is_write && !buf->bd_being_read && BUF_NONE_WAITING (buf))
    {
      ADAPTIVE_READ_INC (itc, buf);
      mutex_leave (&itm->itm_mtx);
      buf->bd_timestamp = buf->bd_pool->bp_ts;
      *buf_ret = buf;
      itc->itc_pl = buf->bd_pl;
      itc->itc_page = buf->bd_page;
      return;
    }
 not_present:
  if (!buf)
    {
      dp_addr_t remap;
      IT_DP_REMAP (tree, leaf, remap);
      if (DP_DELETED == remap)
	{
	  TC (tc_root_image_ref_deleted);
	  mutex_leave (&itm->itm_mtx);
	  *buf_ret = NULL;
	  return;
	}
    }
  itc->itc_itm1 = itm;
  page_wait_access (itc, leaf, NULL, buf_ret, PA_READ, RWG_WAIT_KEY);
  if (tree->it_root_image_version != itc->itc_root_image_version)
    {
      TC (tc_root_image_miss);
      if (itc->itc_to_reset <= RWG_WAIT_KEY)
	page_leave_outside_map (*buf_ret);
      *buf_ret = NULL;
      return;
    }
  if (itc->itc_to_reset <= RWG_WAIT_KEY)
    {
      itc->itc_page = (*buf_ret)->bd_page;
      return;
    }
  *buf_ret = NULL;
}


buffer_desc_t *
itc_root_image_lookup (it_cursor_t * itc)
{
  index_tree_t * tree = itc->itc_tree;
  buffer_desc_t * volatile image = tree->it_root_image;
  volatile int start_version = tree->it_root_image_version;
  buffer_desc_t * rc_buf = NULL;
  if (!start_version || !image)
    return NULL;
  itc->itc_root_image_version = start_version;
  rc_buf = image;
  if (!itc->itc_no_bitmap && itc->itc_insert_key && itc->itc_insert_key->key_is_bitmap)
    itc_init_bm_search (itc);

  if (!itc->itc_key_spec.ksp_key_cmp)
    itc->itc_key_spec.ksp_key_cmp = SM_READ == itc->itc_search_mode ? pg_key_compare : pg_insert_key_compare;

  if (itc->itc_search_mode == SM_READ)
    itc_page_split_search (itc, &rc_buf);
  else
    itc_page_insert_search (itc, &rc_buf);
  if (rc_buf && rc_buf->bd_is_ro_cache)
    {
      tree->it_is_single_page = 1;
      return NULL;
    }
  return rc_buf;
}


void
it_new_root_image (index_tree_t * tree, buffer_desc_t * buf)
{
  int v, new_map_sz;
  buffer_desc_t * new_image;
  dk_mutex_t * root_cache_replace_mtx = &tree->it_maps[0].itm_mtx;
  page_map_t * map = buf->bd_content_map;
  if (BUF_SEEMS_LEAF (buf, map))
    {
      tree->it_is_single_page = 1;
      return;
    }
  mutex_enter (root_cache_replace_mtx);
  if (tree->it_root_image_version
      || buf->bd_page != tree->it_root)
    {
      mutex_leave (root_cache_replace_mtx);
      return;
    }
  v = ++tree->it_root_version_ctr;
  if (!v)
    tree->it_root_version_ctr = v = 1;
  new_image = buffer_allocate (DPF_INDEX);
  new_image->bd_is_ro_cache = 1;
  memcpy (new_image->bd_buffer, buf->bd_buffer, MIN (buf->bd_content_map->pm_filled_to + MAX_KV_GAP_BYTES, PAGE_SZ));
  new_image->bd_tree = tree;
  /* keep the actual size of the new map.  It may be that the old map's size if greater than the size implied by its count */
  new_image->bd_content_map = resource_get (PM_RC (PM_SIZE (buf->bd_content_map->pm_count)));
  new_map_sz = new_image->bd_content_map->pm_size;
  memcpy (new_image->bd_content_map, buf->bd_content_map, PM_ENTRIES_OFFSET + buf->bd_content_map->pm_count * sizeof (short));
  new_image->bd_content_map->pm_size = new_map_sz;
  tree->it_root_image = new_image;
  tree->it_root_image_version = v;
  mutex_leave (root_cache_replace_mtx);
}

buffer_desc_t * old_root_images;
dk_mutex_t * old_roots_mtx;


void
it_root_image_invalidate (index_tree_t * tree)
{
  /* this is serialized on having write on the confirmed root */
  buffer_desc_t * old_image;
  tree->it_root_image_version = 0;
  if ((old_image = tree->it_root_image))
    {
      old_image->bd_timestamp = approx_msec_real_time ();
      tree->it_root_image = NULL;
      mutex_enter (old_roots_mtx);
      old_image->bd_next = old_root_images;
      old_root_images = old_image;
      mutex_leave (old_roots_mtx);
    }
}


buffer_desc_t *
DBGP_NAME (itc_reset) (DBGP_PARAMS it_cursor_t * it)
{
  /* Enter to root in read mode and return the buffer */
  buffer_desc_t *buf;
  index_tree_t * tree = it->itc_tree;
  dp_addr_t dp, back_link;
  it->itc_landed = 0;
  it->itc_dive_mode = PA_READ;
  it->itc_is_on_row = 0;
  it->itc_owns_page = 0;
  it->itc_nth_seq_page = 0;
  it->itc_n_lock_escalations = 0;
  it->itc_ra_root_fill = 0;
  it->itc_n_reads = 0;
  it->itc_at_data_level = 0;

  if (it->itc_tree->it_hi)
    {
      if (it->itc_is_registered)
	GPF_T1 ("should not be registered when resetting to a hi");
      ITC_IN_KNOWN_MAP (it, it->itc_tree->it_hash_first);
        DBGP_NAME (page_wait_access) (DBGP_ARGS it, it->itc_tree->it_hash_first, NULL, &buf, PA_WRITE, RWG_WAIT_ANY);
	ITC_LEAVE_MAPS (it);
	it->itc_map_pos = DP_DATA + HASH_HEAD_LEN; /* offset into the page, not inx of page map */
      it->itc_page = buf->bd_page;
      it->itc_landed = 1;
	return buf;
    }

  if (it->itc_is_registered)
    {
      itc_unregister (it);
    }
  if (tree->it_root_image &&  RANDOM_SEARCH_OFF == it->itc_random_search)
    {
      buf = itc_root_image_lookup (it);
      if (buf)
	return buf;
    }
  if (tree->it_is_single_page
    )
    it->itc_dive_mode = PA_WRITE;

  for (;;)
    {
      ITC_IN_VOLATILE_MAP (it, tree->it_root);
      dp = tree->it_root;
      buf = tree->it_root_buf;
      /* if we are in the confirmed map of dp in tree and buf has this dp and this tree, then it is safe since this can only change in another thread holding this same map */
      if (buf && buf->bd_page == dp && buf->bd_tree == tree
	  &&BUF_NONE_WAITING (buf))
	{
	  if (PA_READ == it->itc_dive_mode)
	    {
	      if (!buf->bd_is_write)
		{
		  buf->bd_readers++;
		  ITC_LEAVE_MAP_NC (it);
		  it->itc_pl = buf->bd_pl;
		  break;
		}
	    }
	  else
	    {
	      if (!buf->bd_is_write && !buf->bd_readers)
		{
		  BD_SET_IS_WRITE (buf, 1);
		  ITC_LEAVE_MAP_NC (it);
		  it->itc_pl = buf->bd_pl;
		  break;
		}
	    }
	}
      TC (tc_root_cache_miss);
      DBGP_NAME (page_wait_access) (DBGP_ARGS it, dp, NULL, &buf, it->itc_dive_mode, RWG_WAIT_KEY);
      if (buf == PF_OF_DELETED)
	GPF_T1 ("The root page of an index is free do a crash dump for recovery.");
      if (it->itc_to_reset > RWG_WAIT_KEY)
	continue;
      tree->it_root_buf = buf;
      break;
    }
  BUF_TOUCH (buf);
  if (!tree->it_is_single_page && !tree->it_root_image_version
      && tree->it_key->key_id != KI_TEMP
     )
    {
      it_new_root_image (tree, buf);
    }
  back_link = LONG_REF (buf->bd_buffer + DP_PARENT);
  if (back_link)
    {
      log_error ("Bad parent link in the tree start page %ld, parent link=%ld."
		 " Please do a dump and restore.",
		 dp, back_link);
    }
  it->itc_page = dp;
#ifdef NEW_HASH
  itc_hi_source_page_used (it, it->itc_page);
#endif
  if (!it->itc_no_bitmap && it->itc_insert_key && it->itc_insert_key->key_is_bitmap)
    itc_init_bm_search (it);
  return buf;
}



buffer_desc_t *
itc_write_parent (it_cursor_t * itc,buffer_desc_t * buf)
{
  /* in insert split, in delete, in single leaf node delete.
   * This gets write access on buf's parent without leaving the buf.
   * If the buf is root, NULL is returned.  Otherwise this will always succeed. */
  volatile dp_addr_t * parent_field = (dp_addr_t*) (buf->bd_buffer + DP_PARENT);
  buffer_desc_t * parent_buf;
  for (;;)
    {
      dp_addr_t parent = LONG_REF (parent_field);
      if (!parent)
	return NULL;
      ITC_IN_TRANSIT (itc, itc->itc_page, parent);
      if (LONG_REF (parent_field) != parent)
	{
	  ITC_LEAVE_MAPS (itc);
	  continue;
    }
      page_wait_access (itc, parent, NULL, &parent_buf, PA_WRITE, RWG_WAIT_DATA);
      ITC_LEAVE_MAPS (itc);
      if (itc->itc_to_reset <= RWG_WAIT_DATA)
	break;
      TC (tc_delete_parent_waits);
    }
  if (BUF_NEEDS_DELTA (parent_buf))
    {
      ITC_IN_KNOWN_MAP (itc, parent_buf->bd_page);
      itc_delta_this_buffer (itc, parent_buf, DELTA_MAY_LEAVE);
      ITC_LEAVE_MAP_NC (itc);
    }

  if (!LONG_REF (parent_buf->bd_buffer + DP_PARENT))
    {
      TC (tc_root_write);
      it_root_image_invalidate (parent_buf->bd_tree);
    }
  return parent_buf;
}


void
itc_set_parent_link (it_cursor_t * itc, dp_addr_t child_dp, dp_addr_t new_parent)
{
  dp_addr_t prev_dp = itc->itc_page; /* this is usually the parent of the right side.  This is stably occupied, can save w/o registration */
  /* in split of intermediate node, set the parent of nodes now under the right side of the split */
  buffer_desc_t * buf;
 try_again:
  itc->itc_page = child_dp;
  ITC_IN_KNOWN_MAP (itc, child_dp);
  buf = (buffer_desc_t *) gethash (DP_ADDR2VOID (child_dp), &IT_DP_MAP (itc->itc_tree, child_dp)->itm_dp_to_buf);
  if (!buf)
    {
      page_wait_access (itc, child_dp, NULL, &buf, PA_WRITE, RWG_WAIT_ANY);
      if (PF_OF_DELETED == buf)
	GPF_T1 ("setting parent of deleted page in split");
      ITC_IN_KNOWN_MAP (itc, buf->bd_page);
      itc_delta_this_buffer (itc, buf, DELTA_MAY_LEAVE);
      rdbg_printf_2 (("	 After disk read: Parent of L=%d from L=%d to L=%d .\n",
		      buf->bd_page,
		      LONG_REF (buf->bd_buffer + DP_PARENT), new_parent));
      LONG_SET (buf->bd_buffer + DP_PARENT, new_parent);
      buf->bd_is_dirty = 1;

      itc_page_leave (itc, buf);
      itc->itc_page = prev_dp;
      return;
    }
  if (buf->bd_being_read)
    {
      /* this is rare but can happen if there is a reentry that needs to read the child page
       * We must wait until the page really is in and then  be in map and set the parent.  Can't simply request access because this could deadlock with the reentered page wanting to access the parent which is all this time busy.  So this will be a busy wait here.  */
      TC (tc_dp_set_parent_being_read);
      ITC_LEAVE_MAPS (itc);
      rdbg_printf_2 (("Retry after finding buf being read: Parent of L=%d from L=%d to L=%d.\n",
		      buf->bd_page,
		      LONG_REF (buf->bd_buffer + DP_PARENT), new_parent));

      virtuoso_sleep (0, 1000);
      goto try_again;
    }
  /* there is the  buffer, possibly occupied but since this is in the map, it is safe to set
   * the parent.i  It can only be read in up transit or delete while inside the map. */
  itc_delta_this_buffer (itc, buf, DELTA_STAY_INSIDE);
  rdbg_printf_2 (("	Parent of L=%d from L=%d to L=%d.\n",
		  buf->bd_page,
		  LONG_REF (buf->bd_buffer + DP_PARENT), new_parent));
  LONG_SET (buf->bd_buffer + DP_PARENT, new_parent);
  BUF_TOUCH (buf);
  buf->bd_is_dirty = 1;

  ITC_LEAVE_MAPS (itc);
  itc->itc_page = prev_dp;
}



#define rdbg_printf_2(a)


void
itc_register (it_cursor_t * itc, buffer_desc_t * buf)
{
  /* Called before going through hyperspace. If the gate busts you
     this will be the life line. */

  if (itc->itc_is_registered && itc->itc_buf_registered)
    {
      GPF_T1 ("double registration");
      return;			/* Already registered. */
    }
  if (buf->bd_page != itc->itc_page)
    GPF_T1 ("different itc_page and bd_page in register");
  if (!buf->bd_is_write)
    GPF_T1 ("must have write on the buffer for registration");
  rdbg_printf_2 (("  register itc=%x on L=%d \n", it, it->itc_page));
  itc->itc_is_registered = 1;
#ifndef NDEBUG
  {
    it_cursor_t * ck = buf->bd_registered;
    while (ck)
      {
	if (itc == ck)
	  GPF_T1 ("double itc registration");
	ck = ck->itc_next_on_page;
      }
  }
#endif

  itc->itc_next_on_page = buf->bd_registered;
  buf->bd_registered = itc;
  itc->itc_buf_registered = buf;
}



void
itc_unregister_inner (it_cursor_t * itc, buffer_desc_t * buf, int is_transit)
{
  it_cursor_t ** prev = &buf->bd_registered;
  it_cursor_t * reg = buf->bd_registered;
  int ctr = 0;
  for (;;)
    {
      if (!reg)
	GPF_T1 ("cursor not registered on buffer in unregister");
      if (itc == reg)
	{
	  *prev = itc->itc_next_on_page;
	  itc->itc_next_on_page = NULL;
	  if (!is_transit)
	    itc->itc_is_registered = 0; /* when reg'd moves from page to page the flag stays set. Otherwise ureg at the same time can be confused */
	  itc->itc_buf_registered = NULL;
	  return;
	}
      prev = &reg->itc_next_on_page;
      reg = reg->itc_next_on_page;
      if (++ctr > 10000)
	GPF_T1 ("loop in registered cursors");
    }
}



void
itc_unregister (it_cursor_t * it_in)
{
  placeholder_t * itc = (placeholder_t *) it_in;
  buffer_desc_t * reg_buf;
  it_map_t * itm;
  dp_addr_t dp;
  if (itc->itc_bp.bp_transiting)
    goto enter;
  if (!itc->itc_is_registered)
    return;
  dp = itc->itc_page;
  itm = IT_DP_MAP (itc->itc_tree, dp);
  mutex_enter (&itm->itm_mtx);
  reg_buf = itc->itc_buf_registered;
  if (reg_buf && !reg_buf->bd_is_write && !reg_buf->bd_readers
      && dp == itc->itc_page && reg_buf->bd_page == dp)
    {
      itc_unregister_inner ((it_cursor_t *) itc, reg_buf, 0);
      mutex_leave (&itm->itm_mtx);
      return;
    }

  mutex_leave (&itm->itm_mtx);
 enter:
  TC (tc_unregister_enter);
  {
    buffer_desc_t * buf;
    it_cursor_t itc_auto;
    it_cursor_t * reg_itc = &itc_auto;
    ITC_INIT (reg_itc, NULL, NULL);
    itc_from_it (reg_itc, itc->itc_tree);
    buf = itc_set_by_placeholder (reg_itc, itc);
    itc_unregister_inner ((it_cursor_t *)itc, buf, 0);
    itc_page_leave (reg_itc, buf);
  }
}


void
itc_unregister_while_on_page (it_cursor_t * it_in, it_cursor_t * preserve_itc, buffer_desc_t ** preserve_buf)
{
  placeholder_t * itc = (placeholder_t *) it_in;
  buffer_desc_t * reg_buf;
  it_map_t * itm;
  dp_addr_t dp;
  if (itc->itc_bp.bp_transiting)
    goto enter;
  if (!itc->itc_is_registered)
    return;
  dp = itc->itc_page;
  itm = IT_DP_MAP (itc->itc_tree, dp);
  mutex_enter (&itm->itm_mtx);
  reg_buf = itc->itc_buf_registered;
  if (reg_buf && !reg_buf->bd_is_write && !reg_buf->bd_readers
      && itc->itc_page == dp && reg_buf->bd_page ==dp)
    {
      itc_unregister_inner ((it_cursor_t *) itc, reg_buf, 0);
      mutex_leave (&itm->itm_mtx);
      return;
    }

  mutex_leave (&itm->itm_mtx);
 enter:
  TC (tc_unregister_enter);
  {
    buffer_desc_t * buf;
    it_cursor_t itc_auto;
    it_cursor_t * reg_itc = &itc_auto;
    ITC_INIT (reg_itc, NULL, NULL);
    if (!preserve_itc->itc_fail_context) GPF_T1 ("must have fail ctx in itc_unregister_while_on_page ");
    itc_from_it (reg_itc, itc->itc_tree);
    itc_register (preserve_itc, *preserve_buf);
    page_leave_outside_map (*preserve_buf);
    ITC_FAIL (reg_itc)
      {
	buf = itc_set_by_placeholder (reg_itc, itc);
      }
    ITC_FAILED
      {
      }
    END_FAIL (reg_itc);
    itc_unregister_inner ((it_cursor_t *)itc, buf, 0);
    itc_page_leave (reg_itc, buf);
    *preserve_buf = page_reenter_excl (preserve_itc);
  }
}

void
isp_unregister_itc_list (void * dp, buffer_desc_t * buf)
{
  placeholder_t * first_itc = (placeholder_t *) buf->bd_registered;
  while (first_itc)
    {
      first_itc->itc_is_registered = 0;
      first_itc->itc_buf_registered = NULL;
      first_itc = (placeholder_t *) first_itc->itc_next_on_page;
    }
  buf->bd_registered = NULL;
}


void
it_unregister_all (index_tree_t * it)
{
  /* unregister all itc's. This is done when closing a transaction
     where there may be a result set of a cursor. */
  int inx;
  for (inx = 0; inx < IT_N_MAPS; inx++)
    {
      it_map_t * itm = &it->it_maps[inx];
      mutex_enter (&itm->itm_mtx);
      maphash ((maphash_func) isp_unregister_itc_list, &itm->itm_dp_to_buf);
      mutex_leave (&itm->itm_mtx);
    }
}