File: repl_instance.c

package info (click to toggle)
fis-gtm 7.1-006-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,908 kB
  • sloc: ansic: 344,906; asm: 5,184; csh: 4,859; sh: 2,000; awk: 294; makefile: 73; sed: 13
file content (1539 lines) | stat: -rw-r--r-- 72,081 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
/****************************************************************
 *								*
 * Copyright (c) 2001-2024 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"

#include "gtm_stdlib.h"
#include "gtm_stdio.h"
#include "gtm_string.h"
#include "gtm_unistd.h"
#include "gtm_fcntl.h"
#include "gtm_stat.h"
#include "gtm_inet.h"
#include "gtm_time.h"

#include <sys/sem.h>
#include <sys/mman.h>
#include <errno.h>

#include "eintr_wrappers.h"
#include "gdsroot.h"
#include "gdsblk.h"
#include "gtm_facility.h"
#include "fileinfo.h"
#include "gdsbt.h"
#include "gdsfhead.h"
#include "filestruct.h"
#include "jnl.h"
#include "repl_msg.h"
#include "gtmsource.h"
#include "gtmrecv.h"
#include "iosp.h"
#include "gtmio.h"
#include "gtm_logicals.h"
#include "trans_log_name.h"
#include "gtmmsg.h"
#include "repl_sem.h"
#include "repl_instance.h"
#include "ftok_sems.h"
#include "error.h"
#include "gds_rundown.h"
#include "buddy_list.h"		/* needed for muprec.h */
#include "hashtab_int4.h"	/* needed for muprec.h */
#include "hashtab_int8.h"	/* needed for muprec.h */
#include "hashtab_mname.h"	/* needed for muprec.h */
#include "muprec.h"
#include "have_crit.h"
#include "anticipatory_freeze.h"
#include "gtmimagename.h"
#ifdef __MVS__
#include "gtm_zos_io.h"
#endif

GBLREF	jnlpool_addrs_ptr_t	jnlpool;
GBLREF	recvpool_addrs		recvpool;
GBLREF	boolean_t		in_repl_inst_edit;	/* Used by an assert in repl_inst_read/repl_inst_write */
GBLREF	boolean_t		in_repl_inst_create;	/* Used by repl_inst_read/repl_inst_write */
GBLREF	boolean_t		in_mupip_ftok;		/* Used by an assert in repl_inst_read */
GBLREF	jnl_gbls_t		jgbl;
GBLREF	gd_addr			*gd_header;
GBLREF	gd_region		*gv_cur_region;
GBLREF	sgmnt_addrs		*cs_addrs;
GBLREF	sgmnt_data_ptr_t	cs_data;
GBLREF	bool			in_backup;
GBLREF	int4			strm_index;
GBLREF	boolean_t		is_src_server;
GBLREF	boolean_t		holds_sem[NUM_SEM_SETS][NUM_SRC_SEMS];
GBLREF	boolean_t		is_rcvr_server;
GBLREF	char			repl_instfilename[];
GBLREF	gd_addr			*repl_inst_from_gld;
GBLREF	uint4			process_id;
#ifdef DEBUG
GBLREF	bool			only_usr_jnlpool_flush;
#endif
GBLREF int			in_rlbk;
GBLREF boolean_t		repl_inst_rlbk_fd;

ZOS_ONLY(error_def(ERR_BADTAG);)
error_def(ERR_LOGTOOLONG);
error_def(ERR_NOTALLDBOPN);
error_def(ERR_REPLFTOKSEM);
error_def(ERR_REPLINSTACC);
error_def(ERR_REPLINSTCLOSE);
error_def(ERR_REPLINSTCREATE);
error_def(ERR_REPLINSTFMT);
error_def(ERR_REPLINSTNOHIST);
error_def(ERR_REPLINSTOPEN);
error_def(ERR_REPLINSTREAD);
error_def(ERR_REPLINSTRECR);
error_def(ERR_REPLINSTSEQORD);
error_def(ERR_REPLINSTUNDEF);
error_def(ERR_REPLINSTWRITE);
error_def(ERR_SYSCALL);
error_def(ERR_TEXT);

/* Description:
 *	Get the environment of replication instance.
 * Parameters:
 *	fn : repl instance file name it gets
 *	fn_len: length of fn.
 *	bufsize: the buffer size caller gives. If exceeded, it trucates file name.
 *	gd_ptr: global directory for extended reference, otherwise NULL.
 * Return Value:
 *	non NULL, on success - 1 if from gtm_repl_instance, otherwise global directory
 *	NULL, otherwise.
 */
struct gd_addr_struct *repl_inst_get_name(char *fn, unsigned int *fn_len, unsigned int bufsize, instname_act error_action,
						struct gd_addr_struct *gd_ptr)
{
	char		temp_inst_fn[MAX_FN_LEN + 1];
	mstr		log_nam, trans_name;
	gd_addr		*gd_local;
	uint4		ustatus;
	int4		status;
	boolean_t	ret, inst_from_gld;

	/* check global directory first */
	gd_local = (NULL == gd_ptr) ? gd_header : (gd_addr *)gd_ptr;
	SETUP_INST_INFO(gd_local, log_nam, inst_from_gld);	/* set log_nam from gld or environment variable */
	trans_name.addr = temp_inst_fn;
	trans_name.len = 0;
	ret = FALSE;
	GET_INSTFILE_NAME(do_sendmsg_on_log2long, issue_gtm_putmsg);
	if (inst_from_gld && (SS_NOLOGNAM == status) && (0 != trans_name.len))
		status = SS_NORMAL;
	if ((0 == trans_name.len) && (SS_NORMAL == status))
		status = SS_NOLOGNAM;
	if (FALSE == ret)
	{
		if (issue_rts_error == error_action)
		{
			if (SS_LOG2LONG == status)
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_LOGTOOLONG, 3, log_nam.len, log_nam.addr,
					SIZEOF(temp_inst_fn) - 1);
			else if (!inst_from_gld)
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_REPLINSTUNDEF);
			else
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTACC, 2, log_nam.len, log_nam.addr,
					ERR_TEXT, 2, RTS_ERROR_LITERAL("from global directory"));
		} else if (issue_gtm_putmsg == error_action)
		{
			if (SS_LOG2LONG == status)
				gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(5) ERR_LOGTOOLONG, 3, log_nam.len, log_nam.addr,
					       SIZEOF(temp_inst_fn) - 1);
			else if (!inst_from_gld)
				gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(1) ERR_REPLINSTUNDEF);
			else
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTACC, 2, log_nam.len, log_nam.addr,
					ERR_TEXT, 2, RTS_ERROR_LITERAL("from global directory"));
		}
	} else if (!inst_from_gld && ('\0' == repl_instfilename[0]))
	{	/* save expanded environment variable gtm_repl_instance for gtmpcat */
		memcpy(repl_instfilename, fn, *fn_len + 1);	/* include null from get_full_path */
	}
	if (FALSE == ret)
		return NULL;
	else if (inst_from_gld)
		return gd_local;
	else
		return INST_NOT_GLD;
}

/* Description:
 *	Reads "buflen" bytes of data into "buff" from the file "fn" at offset "offset"
 * Parameters:
 *	fn    : Instance file name.
 *	offset: Offset at which to read
 *	buff  : Buffer to read into
 *	buflen: Number of bytes to read
 * Return Value:
 *	None
 */
void	repl_inst_read(char *fn, off_t offset, sm_uc_ptr_t buff, size_t buflen)
{
	int			status, fd, oflag;
	size_t			actual_readlen;
	unix_db_info		*udi;
	gd_region		*reg;
	repl_inst_hdr_ptr_t	replhdr;

	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	/* Assert that except for MUPIP REPLIC -INSTANCE_CREATE or -EDITINSTANCE or MUPIP FTOK, all callers hold the FTOK semaphore
	 * on the replication instance file OR the journal pool lock. Note that the instance file might be pointed to by one of the
	 * two region pointers "jnlpool->jnlpool_dummy_reg" or "recvpool.recvpool_dummy_reg" depending on whether the journal pool
	 * or the receive pool was attached to first by this particular process. If both of them are non-NULL, both the region
	 * pointers should be identical. This is also asserted below.
	 * Note: Typically, journal pool lock should have sufficed. However, in certain places like jnlpool_init and recvpool_init,
	 * the journal pool is not yet created and hence grab_lock/rel_lock does not make sense. In those cases we need the FTOK
	 * lock on the instance file. The ONLY exception to this is ROLLBACK in which case it does NOT hold the FTOK semaphore and
	 * since it is NOT necessary for ROLLBACK to have a journal pool open, grab_lock will not be done either. Assert
	 * accordingly.
	 */
	assert(((NULL == jnlpool) || (NULL == jnlpool->jnlpool_dummy_reg)) || (NULL == recvpool.recvpool_dummy_reg)
		|| ((NULL != jnlpool) && (jnlpool->jnlpool_dummy_reg == recvpool.recvpool_dummy_reg)));
	reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
	if (NULL == reg)
		reg = recvpool.recvpool_dummy_reg;
	assert(((NULL == reg) && (in_repl_inst_create || in_repl_inst_edit || in_mupip_ftok))
		|| ((NULL != reg) && !in_repl_inst_create && !in_repl_inst_edit && !in_mupip_ftok)
		|| ((NULL != reg) && (NULL != jnlpool) && in_repl_inst_edit));
	if (NULL != reg)
	{
		udi = FILE_INFO(reg);
		assert(udi->grabbed_ftok_sem || ((jnlpool && jnlpool->jnlpool_ctl) && udi->s_addrs.now_crit)
				|| jgbl.mur_rollback || ((NULL != jnlpool) && in_repl_inst_edit));
	}
	oflag = in_rlbk ? O_RDWR : O_RDONLY; /* for onrlbk hold fd for repl_inst_write() so use O_RDWR */
	if (!in_rlbk || (in_rlbk && (FD_INVALID == repl_inst_rlbk_fd)))
	{
		OPENFILE_CLOEXEC(fn, oflag, fd);
		if (FD_INVALID == fd)
			rts_error_csa(CSA_ARG(NULL) VARLSTCNT(5) ERR_REPLINSTOPEN, 2, LEN_AND_STR(fn), errno);
	} else
		fd = repl_inst_rlbk_fd;
	assert(0 < buflen);
	if (0 != offset)
	{
		LSEEKREAD(fd, offset, buff, buflen, status);
	} else
	{	/* Read starts from the replication instance file header. Assert that the entire file header was requested.
		 * The only exception is MUPIP REPLIC -EDIT -CHANGE -OFFSET=0 -SIZE=xxx where xxx is < REPL_INST_HDR_SIZE.
		 * "in_repl_inst_edit" being equal to IN_REPL_INST_EDIT_CHANGE_OFFSET identifies that situation.
		 */
		assert((REPL_INST_HDR_SIZE <= buflen) || (IN_REPL_INST_EDIT_CHANGE_OFFSET == in_repl_inst_edit));
		/* Use LSEEKREAD_AVAILABLE macro instead of LSEEKREAD. This is because if we are not able to read the entire
		 * fileheader, we still want to see if the "label" field of the file header got read in which case we can
		 * do the format check first. It is important to do the format check before checking "status" returned from
		 * LSEEKREAD* macros since the inability to read the entire file header might actually be due to the
		 * older format replication instance file being smaller than even the newer format instance file header.
		 */
		LSEEKREAD_AVAILABLE(fd, offset, buff, buflen, actual_readlen, status);
		/* Skip error checking if we are inside a MUPIP REPLIC -EDITINSTANCE -CHANGE */
		if (IN_REPL_INST_EDIT_CHANGE_OFFSET != in_repl_inst_edit)
		{
			if (GDS_REPL_INST_LABEL_SZ <= actual_readlen)
			{	/* Have read the entire label in the instance file header. Check if it is the right version */
				if (memcmp(buff, GDS_REPL_INST_LABEL, GDS_REPL_INST_LABEL_SZ - 1))
				{
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTFMT, 6, LEN_AND_STR(fn),
						GDS_REPL_INST_LABEL_SZ - 1, GDS_REPL_INST_LABEL, GDS_REPL_INST_LABEL_SZ - 1, buff);
				}
			}
			if (0 == status)
			{	/* Check a few other fields in the file-header for compatibility */
				assert(actual_readlen == buflen);
				replhdr = (repl_inst_hdr_ptr_t)buff;
				/* Check endianness match */
				if (GTM_IS_LITTLE_ENDIAN != replhdr->is_little_endian)
				{
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTFMT, 6, LEN_AND_STR(fn),
						LEN_AND_LIT(ENDIANTHIS), LEN_AND_LIT(ENDIANOTHER));
				}
				/* Check 64bitness match */
				if (GTM_IS_64BIT != replhdr->is_64bit)
				{
					RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTFMT, 6, LEN_AND_STR(fn),
						LEN_AND_LIT(GTM_BITNESS_THIS), LEN_AND_LIT(GTM_BITNESS_OTHER));
				}
				/* At the time of this writing, the only minor version supported is 1.
				 * Whenever this gets updated, we need to add code to do the online upgrade.
				 * Add an assert as a reminder to do this.
				 */
				assert(1 == replhdr->replinst_minorver);
				/* Check if on-the-fly minor-version upgrade is necessary */
				if (GDS_REPL_INST_MINOR_LABEL != replhdr->replinst_minorver)
					assert(FALSE);
			}
		}
	}
	assert((0 == status) || in_repl_inst_edit || in_mupip_ftok);
	if (0 != status)
	{
		if (-1 == status)
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(6) ERR_REPLINSTREAD, 4, buflen, (qw_off_t *)&offset, LEN_AND_STR(fn));
		else
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(7) ERR_REPLINSTREAD, 4, buflen, (qw_off_t *)&offset, LEN_AND_STR(fn),
				status);
	}
	if (in_rlbk)
		repl_inst_rlbk_fd = fd;
	else
	{
		CLOSEFILE_RESET(fd, status);	/* resets "fd" to FD_INVALID */
		assert(0 == status);
		if (0 != status)
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTCLOSE, 2, LEN_AND_STR(fn), status);
	}
}

/* Description:
 *	Writes "buflen" bytes of data from "buff" into the file "fn" at offset "offset"
 * Parameters:
 *	fn    : Instance file name.
 *	offset: Offset at which to write
 *	buff  : Buffer to write from
 *	buflen: Number of bytes to write
 * Return Value:
 *	None.
 */
void	repl_inst_write(char *fn, off_t offset, sm_uc_ptr_t buff, size_t buflen)
{
	int			status, fd, oflag;
	unix_db_info		*udi;
	gd_region		*reg;
	ZOS_ONLY(int		realfiletag;)
#	ifdef DEBUG
	int			i;
	int4			latch_pid;
	size_t			gtmsrc_lcl_start, gtmsrc_lcl_top;
	gtmsource_local_ptr_t	gtmsourcelocal_ptr;
#	endif
	boolean_t		replfilegone, recreatereplfile;
#define CPBUFSIZE		4096
	char			cpbuf[CPBUFSIZE];
	ssize_t			bytesread, byteswritten;
	off_t			lseekreturn;
	int			save_errno;

	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	/* Assert that except for MUPIP REPLIC -INSTANCE_CREATE or -EDITINSTANCE, all callers hold the FTOK semaphore on the
	 * replication instance file OR the journal pool lock. Note that the instance file might be pointed to by one of the
	 * two region pointers "jnlpool->jnlpool_dummy_reg" or "recvpool.recvpool_dummy_reg" depending on whether the journal pool
	 * or the receive pool was attached to first by this particular process. If both of them are non-NULL, both the region
	 * pointers should be identical. This is also asserted below.
	 * Note: Typically, journal pool lock should have sufficed. However, in certain places like jnlpool_init and recvpool_init,
	 * the journal pool is not yet created and hence grab_lock/rel_lock does not make sense. In those case we need the FTOK
	 * lock on the instance file. The ONLY exception to this is ROLLBACK in which case it does NOT hold the FTOK semaphore and
	 * since it is NOT necessary for ROLLBACK to have a journal pool open, grab_lock will not be done either. Assert
	 * accordingly.
	 */
	assert(((NULL == jnlpool) || (NULL == jnlpool->jnlpool_dummy_reg)) || (NULL == recvpool.recvpool_dummy_reg)
		|| ((NULL != jnlpool) && (jnlpool->jnlpool_dummy_reg == recvpool.recvpool_dummy_reg)));
	DEBUG_ONLY(
		reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
		if (NULL == reg)
			reg = recvpool.recvpool_dummy_reg;
	)
	assert(((NULL == reg) && (in_repl_inst_create || in_repl_inst_edit))
		|| ((NULL != reg) && !in_repl_inst_create && !in_repl_inst_edit)
		|| ((NULL != reg) && (NULL != jnlpool) && in_repl_inst_edit));
#	ifdef DEBUG
	if (NULL != reg)
	{
		udi = FILE_INFO(reg);
		assert(udi->grabbed_ftok_sem
				|| (jnlpool && (NULL != jnlpool->jnlpool_ctl) && udi->s_addrs.now_crit)
				|| jgbl.mur_rollback
				|| ((NULL != jnlpool) && in_repl_inst_edit)
				|| ((jnlpool && jnlpool->gtmsource_local)
					&& (process_id == jnlpool->gtmsource_local->gtmsource_srv_latch.u.parts.latch_pid)));
		for (i = 0; i < NUM_GTMSRC_LCL; i++)
		{
			gtmsrc_lcl_start = ((REPL_INST_HDR_SIZE + (SIZEOF(gtmsrc_lcl) * i)));
			gtmsrc_lcl_top = gtmsrc_lcl_start + SIZEOF(gtmsrc_lcl);
			if ((offset < gtmsrc_lcl_top) && ((offset + buflen) > gtmsrc_lcl_start))
			{
				gtmsourcelocal_ptr = NULL;
				latch_pid = 0;
				if (jnlpool && jnlpool->gtmsource_local_array)
				{
					gtmsourcelocal_ptr = &jnlpool->gtmsource_local_array[i];
					latch_pid = gtmsourcelocal_ptr->gtmsource_srv_latch.u.parts.latch_pid;
				}
				if (jgbl.mur_rollback)
				{	/* Assert that we properly exclude everyone we need to. */
					assert(only_usr_jnlpool_flush || (gtmsourcelocal_ptr && (process_id == latch_pid)));
				} else if (!jnlpool)
				{
					/* If there's no journalpool, assert that we have the ftok on the file */
					assert(udi->grabbed_ftok_sem);
				} else if (in_repl_inst_edit)
				{
					if (!gtmsourcelocal_ptr || (process_id != latch_pid))
					{
						assert(udi->grabbed_ftok_sem);
						assert((NULL == jnlpool->repl_inst_filehdr)
								|| (INVALID_SEMID == jnlpool->repl_inst_filehdr->jnlpool_semid)
								|| (INVALID_SHMID == jnlpool->repl_inst_filehdr->jnlpool_shmid));
					}
				} else if (!only_usr_jnlpool_flush)
				{	/* Otherwise, if there are jnlpool users, assert that we've got the appropriate lock */
					assert(gtmsourcelocal_ptr && (process_id == latch_pid));
				}
			}
		}
	}
#	endif
	replfilegone = (access(fn, F_OK) != 0);
	/* RLBKEXITING == in_rlbk means we are about to exit rollback so re-create repl inst file if needed */
	recreatereplfile = ((RLBKEXITING == in_rlbk) && replfilegone && (FD_INVALID != repl_inst_rlbk_fd));
	oflag = O_RDWR;
	if ((in_repl_inst_create) || recreatereplfile )
		oflag |= (O_CREAT | O_EXCL);
	if (!in_rlbk || (in_rlbk && (FD_INVALID == repl_inst_rlbk_fd)) || recreatereplfile)
	{
		OPENFILE3_CLOEXEC(fn, oflag, 0666, fd);
		if (FD_INVALID == fd)
		{
			if (!in_repl_inst_create)
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTOPEN, 2, LEN_AND_STR(fn), errno);
			else
				RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTCREATE, 2, LEN_AND_STR(fn), errno);
		}
	}
	else
		fd = repl_inst_rlbk_fd;
#ifdef __MVS__
	if (-1 == (in_repl_inst_create ? gtm_zos_set_tag(fd, TAG_BINARY, TAG_NOTTEXT, TAG_FORCE, &realfiletag) :
					 gtm_zos_tag_to_policy(fd, TAG_BINARY, &realfiletag)))
		TAG_POLICY_GTM_PUTMSG(fn, errno, realfiletag, TAG_BINARY);
#endif
	assert(0 < buflen);
	if (recreatereplfile && (FD_INVALID != repl_inst_rlbk_fd))
	{
		/* For the online_rollback/orlbkinstrecr test, which only runs in DEBUG, ensure we have up-to-date
		 * ipc info otherwise other processes might have issues exiting.
		 */
#       	ifdef DEBUG
		REPL_INST_LSEEKWRITE(fd, offset, buff, buflen, status);
		REPL_INST_LSEEKWRITE(repl_inst_rlbk_fd, offset, buff, buflen, status);
#		endif
		/* Go to the beginning using the file descriptor we have held open */
		lseekreturn = lseek(repl_inst_rlbk_fd, 0, SEEK_SET);
		save_errno = errno;
lseekfail:
		if (-1 != lseekreturn)
		{
			for (;;)
			{	/* read from the file descriptor we have held open ... */
				DOREADRL(repl_inst_rlbk_fd, cpbuf, CPBUFSIZE, bytesread);
				save_errno = errno;
bytesreadfail:
				if (bytesread > 0)
				{
					/* write to the recreated repl instance file */
					DOWRITERL(fd, cpbuf, bytesread, byteswritten);
					save_errno = errno;
byteswrittenfail:
					if (bytesread != byteswritten)
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTRECR, 2, LEN_AND_STR(fn),
							save_errno);
				} else if (0 == bytesread)
						break; /* No more bytes to copy so we are done */
					else
						RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTRECR, 2, LEN_AND_STR(fn),
							save_errno);
			}
		} else
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTRECR, 2, LEN_AND_STR(fn), save_errno);
		CLOSEFILE_RESET(repl_inst_rlbk_fd, status);    /* resets "repl_inst_rlbk_fd" to FD_INVALID */
	}
	REPL_INST_LSEEKWRITE(fd, offset, buff, buflen, status);
	assert(0 == status);
	if (0 != status)
		RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(7) ERR_REPLINSTWRITE, 4, buflen, (qw_off_t *)&offset, LEN_AND_STR(fn),
			status);
	if (in_rlbk)
		repl_inst_rlbk_fd = fd;
	else
	{
		CLOSEFILE_RESET(fd, status);	/* resets "fd" to FD_INVALID */
		assert(0 == status);
		if (0 != status)
		{
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTCLOSE, 2, LEN_AND_STR(fn), status);
			assert(FALSE);
			/* Dummy gotos satisfy the compiler that labels used for breakpoints testing are legit */
			goto lseekfail;
			goto bytesreadfail;
			goto byteswrittenfail;
		}
	}
}

/* Description:
 *	Hardens all pending writes for the instance file to disk
 * Parameters:
 *	fn    : Instance file name.
 * Return Value:
 *	None.
 */
void	repl_inst_sync(char *fn)
{
	int		status, fd, oflag;
	unix_db_info	*udi;
	gd_region	*reg;

	/* Assert that except for MUPIP REPLIC -INSTANCE_CREATE or -EDITINSTANCE, all callers hold the FTOK semaphore
	 * on the replication instance file. Note that the instance file might be pointed to by one of the two region
	 * pointers "jnlpool->jnlpool_dummy_reg" or "recvpool.recvpool_dummy_reg" depending on whether the journal pool
	 * or the receive pool was attached to first by this particular process. If both of them are non-NULL, both the
	 * region pointers should be identical. This is also asserted below.
	 */
	assert(((NULL != jnlpool) && (NULL == jnlpool->jnlpool_dummy_reg)) || (NULL == recvpool.recvpool_dummy_reg)
		|| ((NULL != jnlpool) && (jnlpool->jnlpool_dummy_reg == recvpool.recvpool_dummy_reg)));
	DEBUG_ONLY(
		reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
		if (NULL == reg)
			reg = recvpool.recvpool_dummy_reg;
	)
	DEBUG_ONLY(
		assert(NULL != reg);
		udi = FILE_INFO(reg);
		assert((NULL != jnlpool) && (NULL != jnlpool->jnlpool_ctl) && udi->s_addrs.now_crit);
	)
	oflag = O_RDWR;
	if (!in_rlbk || (in_rlbk && (FD_INVALID == repl_inst_rlbk_fd)))
	{
		OPENFILE3_CLOEXEC(fn, oflag, 0666, fd);
		if (FD_INVALID == fd)
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTOPEN, 2, LEN_AND_STR(fn), errno);
	}
	else
		fd = repl_inst_rlbk_fd;
	GTM_REPL_INST_FSYNC(fd, status);
	assert(0 == status);
	if (0 != status)
		RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_SYSCALL, 5, RTS_ERROR_LITERAL("fsync()"), CALLFROM, errno);
	if (in_rlbk)
		repl_inst_rlbk_fd = fd;
	else
	{
		CLOSEFILE_RESET(fd, status);	/* resets "fd" to FD_INVALID */
		assert(0 == status);
		if (0 != status)
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(5) ERR_REPLINSTCLOSE, 2, LEN_AND_STR(fn), status);
	}
}

/* Description:
 *	Reset journal pool shmid and semid in replication instance file.
 * Parameters:
 *	None
 * Return Value:
 *	None
 */
void repl_inst_jnlpool_reset(void)
{
	repl_inst_hdr	repl_instance;
	unix_db_info	*udi;
	gd_region	*reg;

	if ((NULL != jnlpool) && (NULL != jnlpool->repl_inst_filehdr))
	{	/* If journal pool exists, reset sem/shm ids in the file header in the journal pool and flush changes to disk */
		jnlpool->repl_inst_filehdr->jnlpool_semid = INVALID_SEMID;
		jnlpool->repl_inst_filehdr->jnlpool_shmid = INVALID_SHMID;
		jnlpool->repl_inst_filehdr->jnlpool_semid_ctime = 0;
		jnlpool->repl_inst_filehdr->jnlpool_shmid_ctime = 0;
		repl_inst_flush_filehdr();
	} else
	{	/* If journal pool does not exist, reset sem/shm ids directly in the replication instance file header on disk */
		reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
		if (NULL == reg)
			reg = recvpool.recvpool_dummy_reg;
		assert(NULL != reg);
		udi = FILE_INFO(reg);
		assert(udi->grabbed_ftok_sem);
		repl_inst_read((char *)udi->fn, (off_t)0, (sm_uc_ptr_t)&repl_instance, SIZEOF(repl_inst_hdr));
		repl_instance.jnlpool_semid = INVALID_SEMID;
		repl_instance.jnlpool_shmid = INVALID_SHMID;
		repl_instance.jnlpool_semid_ctime = 0;
		repl_instance.jnlpool_shmid_ctime = 0;
		repl_inst_write((char *)udi->fn, (off_t)0, (sm_uc_ptr_t)&repl_instance, SIZEOF(repl_inst_hdr));
	}
}

/* Description:
 *	Reset receiver pool shmid and semid in replication instance file.
 * Parameters:
 *	None
 * Return Value:
 *	None
 */
void repl_inst_recvpool_reset(void)
{
	repl_inst_hdr	repl_instance;
	unix_db_info	*udi;

	assert(NULL != recvpool.recvpool_dummy_reg);
	udi = FILE_INFO(recvpool.recvpool_dummy_reg);
	assert(udi->grabbed_ftok_sem);
	if ((NULL != jnlpool) && (NULL != jnlpool->repl_inst_filehdr))
	{	/* If journal pool exists, reset sem/shm ids in the file header in the journal pool and flush changes to disk */
		jnlpool->repl_inst_filehdr->recvpool_semid = INVALID_SEMID;
		jnlpool->repl_inst_filehdr->recvpool_shmid = INVALID_SHMID;
		jnlpool->repl_inst_filehdr->recvpool_semid_ctime = 0;
		jnlpool->repl_inst_filehdr->recvpool_shmid_ctime = 0;
		repl_inst_flush_filehdr();
	} else
	{	/* If journal pool does not exist, reset sem/shm ids directly in the replication instance file header on disk */
		repl_inst_read((char *)udi->fn, (off_t)0, (sm_uc_ptr_t)&repl_instance, SIZEOF(repl_inst_hdr));
		repl_instance.recvpool_semid = INVALID_SEMID;
		repl_instance.recvpool_shmid = INVALID_SHMID;
		repl_instance.recvpool_semid_ctime = 0;
		repl_instance.recvpool_shmid_ctime = 0;
		repl_inst_write((char *)udi->fn, (off_t)0, (sm_uc_ptr_t)&repl_instance, SIZEOF(repl_inst_hdr));
	}
}

/* Wrapper routine to GRAB the ftok semaphore lock of the replication instance file and to test for errors */
void	repl_inst_ftok_sem_lock(void)
{
	gd_region	*reg;
	unix_db_info	*udi;

	assert(!jgbl.mur_rollback); /* Rollback already has standalone access and will not ask for ftok lock */
	assert(((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg)) || (NULL != recvpool.recvpool_dummy_reg));
	assert(((NULL == jnlpool) || (NULL == jnlpool->jnlpool_dummy_reg)) || (NULL == recvpool.recvpool_dummy_reg)
		|| ((NULL != jnlpool) && ((recvpool.recvpool_dummy_reg == jnlpool->jnlpool_dummy_reg))));
	reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
	if (NULL == reg)
		reg = recvpool.recvpool_dummy_reg;
	assert(NULL != reg);
	udi = FILE_INFO(reg);
	assert(!udi->grabbed_ftok_sem);
	if (!udi->grabbed_ftok_sem)
	{
		assert(0 == have_crit(CRIT_HAVE_ANY_REG));
		if (!ftok_sem_lock(reg, FALSE))
		{
			assert(FALSE);
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(4) ERR_REPLFTOKSEM, 2, LEN_AND_STR(udi->fn));
		}
	}
	assert(udi->grabbed_ftok_sem);
}

/* Wrapper routine to RELEASE the ftok semaphore lock of the replication instance file and to test for errors */
void	repl_inst_ftok_sem_release(void)
{
	gd_region	*reg;
	unix_db_info	*udi;

	assert(!jgbl.mur_rollback); /* Rollback already has standalone access and will not ask for ftok lock */
	assert(((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg)) || (NULL != recvpool.recvpool_dummy_reg));
	assert(((NULL == jnlpool) || (NULL == jnlpool->jnlpool_dummy_reg)) || (NULL == recvpool.recvpool_dummy_reg)
		|| ((NULL != jnlpool) && ((recvpool.recvpool_dummy_reg == jnlpool->jnlpool_dummy_reg))));
	reg = jnlpool ? jnlpool->jnlpool_dummy_reg : NULL;
	if (NULL == reg)
		reg = recvpool.recvpool_dummy_reg;
	assert(NULL != reg);
	udi = FILE_INFO(reg);
	assert(udi->grabbed_ftok_sem);
	if (udi->grabbed_ftok_sem) /* Be safe in PRO and avoid releasing if we do not hold the ftok semaphore */
	{
		assert(0 == have_crit(CRIT_HAVE_ANY_REG));
		if (!ftok_sem_release(reg, FALSE, FALSE))
		{
			assert(FALSE);
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(4) ERR_REPLFTOKSEM, 2, LEN_AND_STR(udi->fn));
		}
	}
	assert(!udi->grabbed_ftok_sem);
}

/* Description:
 *	Get the 'n'th histinfo record from the instance file.
 * Parameters:
 *	index  : The number of the histinfo record to be read. 0 for the first histinfo record, 1 for the second and so on...
 *	histinfo : A pointer to the repl_histinfo structure to be filled in.
 * Return Value:
 *	0, on success
 *	ERR_REPLINSTNOHIST, if "index" is not a valid histinfo index.
 */
int4	repl_inst_histinfo_get(int4 index, repl_histinfo *histinfo)
{
	off_t			offset;
	unix_db_info		*udi;
	repl_inst_hdr_ptr_t	repl_inst_filehdr;

	assert(NULL != jnlpool);
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	assert(udi->s_addrs.now_crit || jgbl.mur_rollback);
	if (0 > index)
		return ERR_REPLINSTNOHIST;
	repl_inst_filehdr = jnlpool->repl_inst_filehdr;
	assert(NULL != repl_inst_filehdr);
	assert(index < repl_inst_filehdr->num_histinfo);
		/* assert that no caller should request a get of an unused (but allocated) histinfo */
	if (index >= repl_inst_filehdr->num_alloc_histinfo)
		return ERR_REPLINSTNOHIST;
	offset = REPL_INST_HISTINFO_START + (index * SIZEOF(repl_histinfo));
	repl_inst_read((char *)udi->fn, offset, (sm_uc_ptr_t)histinfo, SIZEOF(repl_histinfo));
	assert(histinfo->histinfo_num == index);
	return 0;
}

/*
 * Parameters:
 *	seqno      : The journal seqno that is to be searched in the instance file history.
 *	strm_idx   : -1, 0, 1, 2, ... 15 indicating the stream # within which to search.
 *	           : -1 (aka INVALID_SUPPL_STRM) implies search across ALL streams.
 *	histinfo   : A pointer to the repl_histinfo to be filled in. Contents might have been modified even on error return.
 * Description:
 *	If strm_idx=-1
 *	-----------------
 *		Given an input "seqno", locate the histinfo record (from ANY stream) in the instance file whose "start_seqno"
 *			corresponds to "seqno-1".
 *	If strm_idx=0
 *	----------------
 *		Given an input "seqno", locate the histinfo record (from 0th stream) in the instance file whose "start_seqno"
 *			corresponds to "seqno-1".
 *	If strm_idx=1,2,...,15
 *	-------------------------
 *		Given an input "seqno", locate the histinfo record (from "strm_index"th stream) in the instance file
 *			whose "strm_seqno" (not start_seqno) corresponds to "seqno-1".
 * Return Value:
 *	0, on success
 *	ERR_REPLINSTNOHIST, if "seqno" is NOT present in the instance file history range. There are two cases to consider here.
 *	If there was an error fetching a history record, "histinfo->histinfo_num" will be set to INVALID_HISTINFO_NUM.
 *	Otherwise, if we ran out of history records, "histinfo" will point to the 0th history record corresponding to "strm_idx".
 */
int4	repl_inst_histinfo_find_seqno(seq_num seqno, int4 strm_idx, repl_histinfo *histinfo)
{
	unix_db_info		*udi;
	int4			histnum, status;
	seq_num			cur_seqno;
#	ifdef DEBUG
	seq_num			prev_seqno;
	int4			prev_histnum;
#	endif
	repl_inst_hdr_ptr_t	inst_hdr;

	assert((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg));
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	assert(udi->s_addrs.now_crit || jgbl.mur_rollback);
	assert(0 != seqno);
	inst_hdr = jnlpool->repl_inst_filehdr;
	assert(NULL != inst_hdr);
	assert((INVALID_SUPPL_STRM == strm_idx)
			|| (inst_hdr->is_supplementary && (0 <= strm_idx) && (MAX_SUPPL_STRMS > strm_idx)));
	assert(inst_hdr->num_histinfo <= inst_hdr->num_alloc_histinfo);
	if (INVALID_SUPPL_STRM == strm_idx)
		histnum = inst_hdr->num_histinfo - 1;
	else
		histnum = inst_hdr->last_histinfo_num[strm_idx];
	assert(-1 == INVALID_HISTINFO_NUM);	/* so we can safely decrement 0 and reach -1 i.e. an invalid history number */
	DEBUG_ONLY(prev_seqno = 0;)
	do
	{
		assert(histnum < inst_hdr->num_histinfo);
		assert(INVALID_HISTINFO_NUM <= histnum);
		if (INVALID_HISTINFO_NUM == histnum)
			return ERR_REPLINSTNOHIST;
		status = repl_inst_histinfo_get(histnum, histinfo);
		if (0 != status)
		{
			assert(FALSE);
			histinfo->histinfo_num = INVALID_HISTINFO_NUM;	/* signal to caller this is an out-of-design situation */
			return ERR_REPLINSTNOHIST;
		}
		assert((INVALID_SUPPL_STRM == strm_idx) || (strm_idx == histinfo->strm_index));
		cur_seqno = (0 < strm_idx) ? histinfo->strm_seqno : histinfo->start_seqno;
		assert(cur_seqno);
		assert((0 == prev_seqno) || (prev_seqno > cur_seqno)
			|| ((INVALID_SUPPL_STRM == strm_idx) && (prev_seqno == cur_seqno)));
		DEBUG_ONLY(prev_seqno = cur_seqno;)
		if (seqno > cur_seqno)
			break;
		DEBUG_ONLY(prev_histnum = histnum;)
		histnum = (INVALID_SUPPL_STRM == strm_idx) ? (histnum - 1) : histinfo->prev_histinfo_num;
	} while (TRUE);
	return 0;
}

/* This function finds the histinfo in the local replication instance file corresponding to seqno "seqno-1".
 * It is a wrapper on top of the function "repl_inst_histinfo_find_seqno" which additionally does error checking.
 * For the case where "repl_inst_histinfo_find_seqno" returns 0 with a -1 histinfo_num, this function returns ERR_REPLINSTNOHIST.
 */
int4	repl_inst_wrapper_histinfo_find_seqno(seq_num seqno, int4 strm_idx, repl_histinfo *local_histinfo)
{
	unix_db_info	*udi;
	char		histdetail[MAX_REPL_OPMSG_LEN];
	int4		status;
	repl_histinfo	*next_histinfo;

	assert((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg));
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	assert(udi->s_addrs.now_crit || jgbl.mur_rollback);
	assert(NULL != jnlpool->repl_inst_filehdr);	/* journal pool should be set up */
	assert((is_src_server && ((INVALID_SUPPL_STRM == strm_index) || (0 == strm_index)))
		|| (!is_src_server && ((INVALID_SUPPL_STRM == strm_index)
						|| ((0 <= strm_index) && (MAX_SUPPL_STRMS > strm_index)))));
	status = repl_inst_histinfo_find_seqno(seqno, strm_idx, local_histinfo);
	assert((0 == status) || (ERR_REPLINSTNOHIST == status)); /* the only error returned by "repl_inst_histinfo_find_seqno" */
	if (0 != status)
	{
		status = ERR_REPLINSTNOHIST;
		SNPRINTF(histdetail, MAX_REPL_OPMSG_LEN, "seqno "INT8_FMT" "INT8_FMTX, seqno - 1, seqno - 1);
		gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(6) ERR_REPLINSTNOHIST, 4, LEN_AND_STR(histdetail), LEN_AND_STR(udi->fn));
	} else
		assert(0 <= local_histinfo->histinfo_num);
	return status;
}

/* Description:
 *	Add a new histinfo record to the replication instance file.
 * Parameters:
 *	histinfo : A pointer to the histinfo structure to be added to the instance file.
 * Return Value:
 *	None
 * Errors:
 *	Issues ERR_REPLINSTSEQORD error if new histinfo will cause seqno to be out of order.
 */
void	repl_inst_histinfo_add(repl_histinfo *histinfo)
{
	boolean_t	is_supplementary, start_seqno_equal;
	int4		histinfo_num, strm_histinfo_num, prev_histinfo_num, status;
	int		strm_idx, idx;
	off_t		offset;
	repl_histinfo	*last_histinfo, last_histrec, *last_strm_histinfo, last_strm_histrec;
	repl_histinfo	last2_histinfo, *prev_strm_histinfo, prev_strm_histrec;
	seq_num		histinfo_strm_seqno, prev_strm_seqno;
	unix_db_info	*udi;

	assert((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg));
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	assert(udi->s_addrs.now_crit);
	assert(NULL != jnlpool->repl_inst_filehdr);
	assert(jnlpool->repl_inst_filehdr->num_histinfo <= jnlpool->repl_inst_filehdr->num_alloc_histinfo);
	histinfo_num = jnlpool->repl_inst_filehdr->num_histinfo;
	assert(0 <= histinfo_num);
	strm_idx = histinfo->strm_index;
	/* Assert that the very first history record in any instance file (irrespective of whether the
	 * instance is a root primary or propagating primary) should correspond to stream-0.
	 */
	assert((0 < histinfo_num) || (0 == strm_idx));
	is_supplementary = jnlpool->repl_inst_filehdr->is_supplementary;
	assert((!is_supplementary && (0 == strm_idx)) || (is_supplementary && (0 <= strm_idx) && (MAX_SUPPL_STRMS > strm_idx)));
	/* If -updateresync is specified and instance is not supplementary, then there better be NO history records */
	assert((HISTINFO_TYPE_UPDRESYNC != histinfo->history_type) || is_supplementary || (0 == histinfo_num));
	assert(NULL != jnlpool->jnlpool_ctl);
	if (strm_idx && !jnlpool->jnlpool_ctl->upd_disabled)
	{	/* A non-supplementary stream history record is being written into a supplementary root primary instance.
		 * Convert the history record as appropriate. See below macro definition for more comments on the conversion.
		 */
		CONVERT_NONSUPPL2SUPPL_HISTINFO(histinfo, jnlpool->jnlpool_ctl)
	}
	if (0 < histinfo_num)
	{
		last_histinfo = &last_histrec;
		status = repl_inst_histinfo_get(histinfo_num - 1, last_histinfo);
		assert(0 == status);	/* Since histinfo_num-1 we are passing is >=0 and < num_histinfo */
		assert(jnlpool->jnlpool_ctl->last_histinfo_seqno == last_histinfo->start_seqno);
		if (histinfo->start_seqno < last_histinfo->start_seqno)
		{	/* cannot create histinfo with out-of-order start_seqno */
			RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(8) ERR_REPLINSTSEQORD, 6, LEN_AND_LIT("New history record"),
				&histinfo->start_seqno, &last_histinfo->start_seqno, LEN_AND_STR(udi->fn));
		}
	}
	strm_histinfo_num = jnlpool->repl_inst_filehdr->last_histinfo_num[strm_idx];
	prev_histinfo_num = strm_histinfo_num;
	if (0 <= strm_histinfo_num)
	{
		assert(strm_histinfo_num < histinfo_num);
		if (strm_histinfo_num != (histinfo_num - 1))
		{
			last_strm_histinfo = &last_strm_histrec;
			status = repl_inst_histinfo_get(strm_histinfo_num, last_strm_histinfo);
			assert(0 == status);	/* Since the strm_histinfo_num we are passing is >=0 and < num_histinfo */
		} else
		{	/* Had read this history record just now from the instance file. Use it and avoid another read */
			last_strm_histinfo = last_histinfo;
		}
		assert(strm_idx == last_strm_histinfo->strm_index);
		/* Check if the history record to be added has the same histinfo content as the last history record
		 * already present in the instance file (in the stream of interest). This is possible in case of a secondary
		 * where the receiver was receiving journal records (from the primary) for a while, was shut down and then
		 * restarted. Same instance is sending information so no new histinfo information needed. Return right away.
		 * The only exception is if this is a supplementary instance and the new history record is an UPDATERESYNC
		 * type of record in which case it is possible the two histories have the histinfo content identical but
		 * have different start_seqnos. In this case, some updates went in between the two histories so we want
		 * to record the input history as a separate record instead of returning (since this signals the beginning
		 * of a new stream of updates).
		 */
		if ((!is_supplementary || (HISTINFO_TYPE_UPDRESYNC != histinfo->history_type))
			&& !STRCMP(last_strm_histinfo->root_primary_instname, histinfo->root_primary_instname)
			&& (last_strm_histinfo->root_primary_cycle == histinfo->root_primary_cycle)
			&& (last_strm_histinfo->creator_pid == histinfo->creator_pid)
			&& (last_strm_histinfo->created_time == histinfo->created_time))
		{
			return;
		}
		assert((histinfo->start_seqno != last_strm_histinfo->start_seqno)
				|| (histinfo->strm_seqno == last_strm_histinfo->strm_seqno)
				|| (HISTINFO_TYPE_NORESYNC == histinfo->history_type)
				|| (HISTINFO_TYPE_UPDRESYNC == histinfo->history_type));
		/* If stream seqnos match between input history and last stream specific history in the instance file,
		 * make sure the to-be-written history record skips past the last stream specific history record (as we
		 * expect a decreasing sequence of strm_seqnos in the "prev_histinfo_num" linked list of history records).
		 * The only exception is if we are a supplementary instance and this is stream # 0. In that case, only if
		 * the start_seqno is also equal, will we skip. This is because if start_seqno is not equal, the stream # 0
		 * history records identify a range of updates that happened (even if the updates happened in non-zero
		 * stream #s) and that is used by history record matching between two supplementary instances at replication
		 * connection time.
		 * The same skipping logic applies to "start_seqno" in case the instance is non-supplementary (in which case
		 * the "strm_seqno" field is 0).
		 */
		histinfo_strm_seqno = histinfo->strm_seqno;
		prev_strm_seqno = last_strm_histinfo->strm_seqno;
		if (histinfo_strm_seqno == prev_strm_seqno)
		{
			start_seqno_equal = (histinfo->start_seqno == last_strm_histinfo->start_seqno);
			if ((histinfo_strm_seqno && strm_idx) || start_seqno_equal)
			{
				assert(prev_histinfo_num > last_strm_histinfo->prev_histinfo_num);
				prev_histinfo_num = last_strm_histinfo->prev_histinfo_num;
			}
			if (start_seqno_equal && (strm_histinfo_num == (histinfo_num - 1))
				&& (histinfo->history_type == last_strm_histinfo->history_type))
			{	/* Starting seqno of the last histinfo in the instance file matches the input histinfo.
				 * This means there are no journal records corresponding to the input stream in the journal
				 * files after the last histinfo (which happens to be same as the input stream) was written
				 * in the instance file. Overwrite the last histinfo with the new histinfo information before
				 * writing new journal records.
				 * Note: The check for history_type above is to take into account a supplementary
				 * instance where a HISTINFO_TYPE_UPDRESYNC type history record is first written when A->P
				 * connect for the first time and later a HISTINFO_TYPE_NORMAL record is written when A->P
				 * connect for the second time. If there were no intervening updates on P from the disconnect
				 * to the reconnect, we do not want to overwrite the HISTINFO_TYPE_UPDRESYNC type record
				 * as that will confuse Q in a A->P->Q configuration when Q receives updates from A (GTM-8657).
				 * If the history types do not match, treat the two history records as different and avoid
				 * overwriting even if the start_seqno matches.
				 */
				histinfo_num--;
			}
		} else if (HISTINFO_TYPE_NORESYNC == histinfo->history_type)
		{	/* Determine the correct value of "prev_histinfo_num" */
			prev_strm_histinfo = &prev_strm_histrec;
			prev_strm_histrec = *last_strm_histinfo;
			assert(prev_strm_seqno == prev_strm_histinfo->strm_seqno);
			while (histinfo_strm_seqno <= prev_strm_seqno)
			{
				prev_histinfo_num = prev_strm_histinfo->prev_histinfo_num;
				assert(INVALID_HISTINFO_NUM != prev_histinfo_num);
				if (INVALID_HISTINFO_NUM == prev_histinfo_num)
					break;
				status = repl_inst_histinfo_get(prev_histinfo_num, prev_strm_histinfo);
				assert(0 == status); /* Since prev_histinfo_num we are passing is >=0 and < num_histinfo */
				assert(prev_strm_seqno > prev_strm_histinfo->strm_seqno);
				prev_strm_seqno = prev_strm_histinfo->strm_seqno;
			}
		}
	}
	/* Assert that the history record we are going to add is in sync with the current seqno state of the instance */
	assert(jnlpool->jnlpool_ctl->jnl_seqno == histinfo->start_seqno);
	assert(jnlpool->jnlpool_ctl->strm_seqno[histinfo->strm_index] == histinfo->strm_seqno);
	offset = REPL_INST_HISTINFO_START + (SIZEOF(repl_histinfo) * (off_t)histinfo_num);
	/* Initialize the following members of the repl_histinfo structure. Everything else should be initialized by caller.
	 *	histinfo_num
	 *	prev_histinfo_num
	 *	last_histinfo_num[]
	 */
	histinfo->histinfo_num = histinfo_num;
	histinfo->prev_histinfo_num = (HISTINFO_TYPE_UPDRESYNC == histinfo->history_type)
					? INVALID_HISTINFO_NUM : prev_histinfo_num;
	assert(histinfo->prev_histinfo_num < histinfo->histinfo_num);
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
	{
		assert((jnlpool->repl_inst_filehdr->last_histinfo_num[idx] < histinfo_num)
			|| ((idx == strm_idx) && (jnlpool->repl_inst_filehdr->last_histinfo_num[idx] == histinfo_num)));
		histinfo->last_histinfo_num[idx] = jnlpool->repl_inst_filehdr->last_histinfo_num[idx];
	}
	if (strm_histinfo_num == histinfo_num)
	{	/* The last history record in the instance file is going to be overwritten with another history record of
		 * the same stream. In this case, jnlpool->repl_inst_filehdr->last_histinfo_num[strm_idx] would not reflect a
		 * state of the instance file BEFORE this history record was added. So find the correct value. Thankfully
		 * the last history record (that we are about to overwrite) already has this value so copy it over.
		 */
		histinfo->last_histinfo_num[strm_idx] = last_histinfo->last_histinfo_num[strm_idx];
	}
	assert(strm_histinfo_num == jnlpool->repl_inst_filehdr->last_histinfo_num[strm_idx]);
	assert(strm_histinfo_num <= histinfo_num);
	assert(strm_histinfo_num >= prev_histinfo_num);
	assert(histinfo_num > prev_histinfo_num);
	assert((INVALID_HISTINFO_NUM == histinfo->prev_histinfo_num) || (0 <= histinfo->prev_histinfo_num));
	assert(is_supplementary || (prev_histinfo_num == (histinfo_num - 1)));
#	ifdef DEBUG
	/* Assert that the prev_histinfo_num list of history records have decreasing "start_seqno" and "strm_seqno" values.
	 * The only exception is stream # 0 for a supplementary instance as described in a previous comment in this function.
	 */
	if (INVALID_HISTINFO_NUM != histinfo->prev_histinfo_num)
	{
		assert(histinfo->prev_histinfo_num == prev_histinfo_num);
		status = repl_inst_histinfo_get(prev_histinfo_num, &last2_histinfo);
		assert(0 == status);	/* Since the strm_histinfo_num we are passing is >=0 and < num_histinfo */
		assert(strm_idx == last2_histinfo.strm_index);	/* they both better have the same stream # */
		assert(histinfo->start_seqno > last2_histinfo.start_seqno);
		assert(!histinfo->strm_seqno || (histinfo->strm_seqno > last2_histinfo.strm_seqno) || (0 == strm_idx));
	}
	/* Assert that the last_histinfo_num fields reflect a state of the instance file that does not include the about-to-be
	 * added history record. This ensures the instance file header will get restored to a valid state in case of a rollback
	 * that truncates exactly at this history record boundary.
	 */
	for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
		assert(histinfo->last_histinfo_num[idx] < histinfo_num);
#	endif
	/* Assert that if this is not the first history record being written into the instance file
	 * it should have a valid 0th stream history record number. This is relied upon by "gtmsource_send_new_histrec"
	 */
	assert((0 == histinfo_num) || (INVALID_HISTINFO_NUM != histinfo->last_histinfo_num[0]));
	repl_inst_write(udi->fn, offset, (sm_uc_ptr_t)histinfo, SIZEOF(repl_histinfo));
	/* Update stream specific history number fields in the file header to reflect the latest history addition to this stream */
	jnlpool->repl_inst_filehdr->last_histinfo_num[strm_idx] = histinfo_num;
	/* If -updateresync history record for a non-zero stream #, then initialize strm_group_info in file header */
	if ((0 < strm_idx) && (HISTINFO_TYPE_UPDRESYNC == histinfo->history_type))
		jnlpool->repl_inst_filehdr->strm_group_info[strm_idx - 1] = histinfo->lms_group;
	histinfo_num++;
	if (jnlpool->repl_inst_filehdr->num_alloc_histinfo < histinfo_num)
		jnlpool->repl_inst_filehdr->num_alloc_histinfo = histinfo_num;
	jnlpool->repl_inst_filehdr->num_histinfo = histinfo_num;
	/* Since we are going to flush the file header, take this opportunity to update the current jnl seqno in it.
	 * Note that the current stream jnl seqnos are already udpated inside "repl_inst_flush_filehdr" by the
	 * "COPY_JCTL_STRMSEQNO_TO_INSTHDR_IF_NEEDED" macro. We dont do the current jnl seqno to there because
	 * "repl_inst_histinfo_truncate" invokes "repl_inst_flush_filehdr" with a different jnl seqno than the current.
	 */
	jnlpool->repl_inst_filehdr->jnl_seqno = jnlpool->jnlpool_ctl->jnl_seqno;
	repl_inst_flush_filehdr();
	jnlpool->jnlpool_ctl->last_histinfo_seqno = histinfo->start_seqno;
	repl_inst_sync(udi->fn);	/* Harden the new histinfo to disk before any logical records for this arrive. */
	return;
}

/* Description:
 *	Given an input "rollback_seqno", virtually truncate all histinfo records that correspond to seqnos >= "rollback_seqno"
 *	This function also updates other fields (unrelated to histinfo truncation) in the file header
 *	to reflect a clean shutdown by MUPIP JOURNAL ROLLBACK. This function is also invoked by MUPIP BACKUP in order
 *	to ensure the backed up instance file is initialized to reflect a clean shutdown.
 * Parameters:
 *	rollback_seqno : The seqno after which all histinfo records have to be truncated.
 *			 Note: In case of a supplementary instance file, this function expects the caller to have
 *			 set "inst_hdr->strm_seqno[]" to reflect the "rollback_seqno".
 * Return Value:
 *	Sequence number (start_seqno) of the last history record in the instance file
 * Errors:
 *	Issues ERR_REPLINSTNOHIST message if the call to "repl_inst_histinfo_find_seqno" returned an error.
 */
seq_num	repl_inst_histinfo_truncate(seq_num rollback_seqno)
{
	char			histdetail[MAX_REPL_OPMSG_LEN];
	int4			status, index, num_histinfo, last_histnum;
	int			idx;
	repl_histinfo		temphistinfo, nexthistinfo, strmhistinfo;
	repl_inst_hdr_ptr_t	inst_hdr;
	unix_db_info		*udi;
	gd_region		*reg;
	seq_num			last_histinfo_seqno = 0;

	assert(NULL != jnlpool);
	reg = jnlpool->jnlpool_dummy_reg;
	if (NULL == reg)
		reg = recvpool.recvpool_dummy_reg;
	assert(NULL != reg);
	udi = FILE_INFO(reg);
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	assert(in_backup || jgbl.mur_rollback); /* Only ROLLBACK or BACKUP calls this function */
	assert(udi->s_addrs.now_crit || jgbl.mur_rollback);
	inst_hdr = jnlpool->repl_inst_filehdr;
	assert(NULL != inst_hdr); /* Should have been set when mupip rollback invoked "mu_replpool_grab_sem" */
	num_histinfo = inst_hdr->num_histinfo;
	if (0 != num_histinfo)
	{
		status = repl_inst_histinfo_find_seqno(rollback_seqno, INVALID_SUPPL_STRM, &temphistinfo);
		if (0 != status)
		{
			assert(ERR_REPLINSTNOHIST == status);	/* the only error returned by "repl_inst_histinfo_find_seqno" */
			if ((INVALID_HISTINFO_NUM == temphistinfo.histinfo_num) || (temphistinfo.start_seqno != rollback_seqno))
			{	/* The truncation seqno is not the starting seqno of the instance file. In that case, issue
				 * a RELINSTNOHIST warning message even though rollback is going to proceed anycase.
				 */
				assert(FALSE);
				NON_GTM64_ONLY(SNPRINTF(histdetail, MAX_REPL_OPMSG_LEN, "seqno [0x%llx]", rollback_seqno - 1));
				GTM64_ONLY(SNPRINTF(histdetail, MAX_REPL_OPMSG_LEN, "seqno [0x%lx]", rollback_seqno - 1));
				gtm_putmsg_csa(CSA_ARG(NULL) VARLSTCNT(6) MAKE_MSG_WARNING(ERR_REPLINSTNOHIST), 4,
					       LEN_AND_STR(histdetail), LEN_AND_STR(udi->fn));
			}
			index = -1;
			/* Since we are rolling back all history records in the instance file,
			 * clear all of "strm_group_info[]" and "last_histinfo_num[]" arrays.
			 * The following logic is similar to that in "repl_inst_create" to initialize the above 2 fields.
			 * Note that we keep "jnl_seqno" and "strm_seqno" set to whatever value it came in with (as opposed
			 * to setting it to 0). This is different from what is done in "repl_inst_create" because we want
			 * to keep these set to a non-zero value if possible (see detailed comment below where "jnl_seqno"
			 * gets set). Keeping "jnl_seqno" at a non-zero value necessitates keeping "strm_seqno" at a non-zero
			 * value as well in order to avoid REPLINSTDBSTRM errors at source server startup.
			 */
			assert(MAX_SUPPL_STRMS == ARRAYSIZE(inst_hdr->last_histinfo_num));
			for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
				inst_hdr->last_histinfo_num[idx] = INVALID_HISTINFO_NUM;
			if (inst_hdr->is_supplementary)
			{
				assert(MAX_SUPPL_STRMS == ARRAYSIZE(inst_hdr->strm_seqno));
				assert(SIZEOF(seq_num) == SIZEOF(inst_hdr->strm_seqno[0]));
				assert((MAX_SUPPL_STRMS - 1) == ARRAYSIZE(inst_hdr->strm_group_info));
				assert(SIZEOF(repl_inst_uuid) == SIZEOF(inst_hdr->strm_group_info[0]));
				/* Keep the strm_seqno 0 for those streams which this instance has never used/communicated. For all
				 * other stream#, set the strm_seqno to 1 if the current value of strm_seqno is 0. If the current
				 * value of strm_seqno is non-zero, let it stay as it is (see comment above about strm_seqno).
				 * This way, if this instance reconnects after the ROLLBACK to the same instance it was
				 * communicating before, we avoid issuing REPLINSTNOHIST thereby making it user-friendly.
				 * Note: The LMS group info for stream# "i" is found in strm_group_info[i - 1] (used below)
				 */
				for (idx = 0; idx < MAX_SUPPL_STRMS; idx++)
				{
					if ((idx == 0) || (IS_REPL_INST_UUID_NON_NULL(inst_hdr->strm_group_info[idx - 1])))
					{
						if (0 == inst_hdr->strm_seqno[idx])
							inst_hdr->strm_seqno[idx] = 1;
					}
#					ifdef DEBUG
					else
						assert(0 == inst_hdr->strm_seqno[idx]);
#					endif
				}
				/* Leave the LMS group information as-is in the instance file header. By doing so, we avoid cases
				 * where receiver server continuing after the rollback issues an INSUNKNOWN error. While this is
				 * a valid error, we try to make it as user-friendly as possible.
				 */
			}
		} else
		{
			index = temphistinfo.histinfo_num;
			assert(temphistinfo.start_seqno < rollback_seqno);
			assert(0 <= index);
			assert(index <= (num_histinfo - 1));
			last_histinfo_seqno = temphistinfo.start_seqno;
			if (index < (num_histinfo - 1))
			{
				status = repl_inst_histinfo_get(index + 1, &nexthistinfo);
				assert(0 == status);	/* Since the histinfo_num we are passing is >=0 and <= num_histinfo */
				assert(nexthistinfo.start_seqno >= rollback_seqno);
				assert(nexthistinfo.histinfo_num == (index + 1));
				/* Copy over information from this history record back to the instance file header */
				assert(SIZEOF(inst_hdr->last_histinfo_num) == SIZEOF(nexthistinfo.last_histinfo_num));
				memcpy(inst_hdr->last_histinfo_num, nexthistinfo.last_histinfo_num,
					SIZEOF(nexthistinfo.last_histinfo_num));
				if (inst_hdr->is_supplementary)
				{
					/* inst_hdr->strm_seqno[] is already set by caller */
					assert((MAX_SUPPL_STRMS - 1) == ARRAYSIZE(inst_hdr->strm_group_info));
					for (idx = 0; idx < (MAX_SUPPL_STRMS - 1); idx++)
					{
						last_histnum = nexthistinfo.last_histinfo_num[idx + 1];
						assert(INVALID_HISTINFO_NUM <= last_histnum);
						assert(last_histnum < nexthistinfo.histinfo_num);
						if (INVALID_HISTINFO_NUM != last_histnum)
						{
							status = repl_inst_histinfo_get(last_histnum, &strmhistinfo);
							assert(0 == status);
							assert(strmhistinfo.histinfo_num == last_histnum);
							assert(strmhistinfo.start_seqno < rollback_seqno);
							assert(strmhistinfo.strm_index);
							assert(MAX_SUPPL_STRMS > strmhistinfo.strm_index);
							assert(IS_REPL_INST_UUID_NON_NULL(strmhistinfo.lms_group));
							inst_hdr->strm_group_info[idx] = strmhistinfo.lms_group;
						} else if (IS_REPL_INST_UUID_NON_NULL(inst_hdr->strm_group_info[idx]))
						{	/* stream# (idx + 1) has a non-zero UUID information in the file header
							 * but all the history records corresponding to this stream are now
							 * truncated. This also implies that strm_seqno of this stream is reset
							 * to zero by ROLLBACK. To avoid REPLINSTNOHIST next time a communication
							 * happens with the instance corresponding to stream# idx + 1, set the
							 * strm_seqno to 1.
							 * Note: The LMS group info for stream-i is found in strm_group_info[i - 1]
							 */
							inst_hdr->strm_seqno[idx + 1] = 1;
							/* Also, leave the LMS group information for stream# idx + 1 as-is in the
							 * instance file header By doing so, we avoid cases where receiver server
							 * continuing after the rollback issues an INSUNKNOWN error. While this is
							 * a valid error, we try to mae it as user-friendly as possible.
							 */
						}
					}
				}
			}
			/* else index == "num_histinfo - 1" so no changes needed to "last_histinfo_num[]"
			 *	or "strm_seqno[]" or "strm_group_info[]" arrays.
			 */
		}
		index++;
		assert((index == inst_hdr->num_histinfo)
			|| ((inst_hdr->num_histinfo >= 0) && (inst_hdr->num_alloc_histinfo > index)));
		inst_hdr->num_histinfo = index;
	}
	/* Reset "jnl_seqno" to the rollback seqno so future REPLINSTDBMATCH errors are avoided in "gtmsource_seqno_init".
	 * Note that it is possible inst_hdr->num_histinfo is 0 at this point (i.e. no history records). In that case,
	 * repl_inst_create sets the "jnl_seqno" to 0 whereas we might set it here to a potentially non-zero value.
	 * That is because repl_inst_create does not go through the database and get the max of the reg_seqnos to figure
	 * out the instance jnl_seqno. Hence it sets it to a value of 0 indicating the source server that starts up the
	 * instance to fill it in with a non-zero value. On the other hand, rollback or backup (both of which can call
	 * this function "repl_inst_histinfo_truncate") know exactly what the instance seqno is and so can safely set the
	 * "jnl_seqno" to a non-zero value even though there are no history records. Setting it to a non-zero value whenever
	 * possible is useful for example when we ship a backup of a freshly created live non-supplementary instance (with
	 * jnl_seqno of 1) to be used as input to the -updateresync qualifier of a receiver startup on a supplementary
	 * instance. In this case, if the backup had a jnl_seqno of 0, the startup would fail. But since it has a non-zero
	 * "jnl_seqno" (even though there are no history records), the initial handshake between the non-supplementary and
	 * supplementary instances is possible (they avoid history record exchanges due to jnl_seqno == 1). A zero jnl_seqno
	 * would have resulted in a UPDSYNCINSTFILE error in the initial handshake.
	 */
	inst_hdr->jnl_seqno = rollback_seqno;
	/* Reset sem/shm ids to reflect a clean shutdown so future REPLREQRUNDOWN errors are avoided at "jnlpool_init" time */
	if (!jgbl.mur_rollback)
	{	/* Reset semid/sem_ctime fields in the instance file header. */
		/* Reset "crash" to FALSE so future REPLREQROLLBACK errors are avoided at "jnlpool_init" time */
		inst_hdr->crash = FALSE;
		inst_hdr->jnlpool_semid = INVALID_SEMID;
		inst_hdr->jnlpool_shmid = INVALID_SHMID;
		inst_hdr->jnlpool_semid_ctime = 0;
		inst_hdr->jnlpool_shmid_ctime = 0;
		inst_hdr->recvpool_semid = INVALID_SEMID;	/* Just in case it is not already reset */
		inst_hdr->recvpool_shmid = INVALID_SHMID;	/* Just in case it is not already reset */
		inst_hdr->recvpool_semid_ctime = 0;
		inst_hdr->recvpool_shmid_ctime = 0;
	} /* else for rollback, we reset the IPC fields in mu_replpool_release_sem() and crash in mur_close_files */
	/* Flush all file header changes in jnlpool->repl_inst_filehdr to disk */
	repl_inst_flush_filehdr();
	assert((0 == inst_hdr->num_histinfo) || (0 < last_histinfo_seqno));
	return last_histinfo_seqno;
}

/* Description:
 *	Flushes the instance file header pointed to by "jnlpool->repl_inst_filehdr" to disk.
 * Parameters:
 *	None
 * Return Value:
 *	None
 */
void	repl_inst_flush_filehdr()
{
	unix_db_info	*udi;

	assert(NULL != jnlpool);
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	/* We could come here from several paths. If journal pool exists, we would have done a grab_lock. This covers most of the
	 * cases. If the journal pool doesn't exist, then we could come here from one of the following places
	 *
	 * ROLLBACK (online/noonline):
	 *   We already hold standalone access on the journal pool and if the journal pool exists, we also hold the journal pool
	 *   lock
	 *
	 * MUPIP RUNDOWN -> mu_rndwn_repl_instance:
	 *   We hold the ftok on the instance file and have already made sure that no one else is attached to the journal pool. Even
	 *   though we don't hold the access control on the journal pool, no one else can startup at this point because they need
	 *   the ftok for which they will have to wait.
	 *
	 * gtmsource_shutdown -> repl_inst_jnlpool_reset:
	 *   We hold the ftok on the instance file and have already made sure that no one else is attached to the journal pool. Even
	 *   though we don't hold the access control on the journal pool, no one else can startup at this point because they need
	 *   the ftok for which they will have to wait.
	 *
	 * gtmrecv_shutdown -> repl_inst_recvpool_reset:
	 *   Same as above.
	 * So, in all cases, we are guaranteed that the following code is mutually exclusive (which is what we want).
	 */
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	assert(udi->s_addrs.now_crit || udi->grabbed_ftok_sem || (jgbl.mur_rollback && holds_sem[SOURCE][JNL_POOL_ACCESS_SEM]));
	if (jnlpool->jnlpool_dummy_reg->open)
		COPY_JCTL_STRMSEQNO_TO_INSTHDR_IF_NEEDED; /* Keep the file header copy of "strm_seqno" uptodate with jnlpool_ctl */
	assert((NULL == jnlpool->jnlpool_ctl) || udi->s_addrs.now_crit);
	assert(NULL != jnlpool->repl_inst_filehdr);
	/* flush the instance file header */
	repl_inst_write(udi->fn, (off_t)0, (sm_uc_ptr_t)jnlpool->repl_inst_filehdr, REPL_INST_HDR_SIZE);
}

/* Description:
 *	Flushes the "gtmsrc_lcl" structure corresponding to the jnlpool->gtmsource_local structure for the
 *	calling source server. Updates "gtmsource_local->last_flush_resync_seqno" to equal "gtmsource_local->read_jnl_seqno"
 * Parameters:
 *	None
 * Return Value:
 *	None
 */
void	repl_inst_flush_gtmsrc_lcl()
{
	unix_db_info		*udi;
	int4			index;
	off_t			offset;
	gtmsrc_lcl_ptr_t	gtmsrclcl_ptr;
#	ifdef DEBUG
	seq_num			tmp_rseqno, tmp_cseqno;
	tmp_rseqno = jnlpool->gtmsource_local->read_jnl_seqno;
	tmp_cseqno = jnlpool->gtmsource_local->connect_jnl_seqno;
#	endif

	assert(NULL != jnlpool);
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	assert(!jgbl.mur_rollback); /* Rollback should never reach here */
	assert(process_id == jnlpool->gtmsource_local->gtmsource_srv_latch.u.parts.latch_pid);
	assert(NULL != jnlpool->gtmsource_local);
	index = jnlpool->gtmsource_local->gtmsrc_lcl_array_index;
	assert(0 <= index);
	assert(jnlpool->gtmsource_local == &jnlpool->gtmsource_local_array[index]);
	gtmsrclcl_ptr = &jnlpool->gtmsrc_lcl_array[index];
	assert(jnlpool->jnlpool_dummy_reg->open);	/* journal pool exists and this process has done "jnlpool_init" */
	/* Copy each field from "gtmsource_local" to "gtmsrc_lcl" before flushing it to disk.
	 * Do not need the journal pool lock, as we are the only ones reading/updating the below fields
	 * in "gtmsource_local" or "gtmsrc_lcl".
	 */
	COPY_GTMSOURCELOCAL_TO_GTMSRCLCL(jnlpool->gtmsource_local, gtmsrclcl_ptr);
	offset = REPL_INST_HDR_SIZE + (SIZEOF(gtmsrc_lcl) * (off_t)index);
#	ifdef DEBUG
	if (WBTEST_ENABLED(WBTEST_FLUSH_FH))
	{	/* Sleep for a long enough time to provide substantial opportunity for any competitors for these fields to try
		 * to access/modify them and to test the concurrency control system that protects them.
		 */
		LONG_SLEEP(5);
	}
#	endif
	repl_inst_write(udi->fn, offset, (sm_uc_ptr_t)gtmsrclcl_ptr, SIZEOF(gtmsrc_lcl));
	assert((tmp_rseqno == gtmsrclcl_ptr->resync_seqno) && (tmp_rseqno == jnlpool->gtmsource_local->read_jnl_seqno));
	assert((tmp_cseqno == gtmsrclcl_ptr->connect_jnl_seqno) && (tmp_cseqno == jnlpool->gtmsource_local->connect_jnl_seqno));
	jnlpool->gtmsource_local->last_flush_resync_seqno = jnlpool->gtmsource_local->read_jnl_seqno;
}

/* Description:
 *	Flushes the "repl_inst_hdr" and "gtmsrc_lcl" sections in the journal pool to the on disk copy of the instance file.
 * Parameters:
 *	None
 * Return Value:
 *	None
 */
void	repl_inst_flush_jnlpool(boolean_t reset_replpool_fields, boolean_t reset_crash)
{
	unix_db_info		*udi;
	int4			index;
	gtmsrc_lcl_ptr_t	gtmsrclcl_ptr;
	gtmsource_local_ptr_t	gtmsourcelocal_ptr;

	assert((NULL != jnlpool) && (NULL != jnlpool->jnlpool_dummy_reg));
	udi = FILE_INFO(jnlpool->jnlpool_dummy_reg);
	/* This function should be invoked only if the caller determines this is last process attached to the journal pool.
	 * Since the ftok lock on the instance file is already held, no other process will be allowed to attach to the
	 * journal pool and hence this is the only process having access to the journal pool during this function. The only
	 * exception is if it is invoked from mur_open_files for Online Rollback. But, in that case Online Rollback will be
	 * holding the access control. Any process calling this function, needs the access control semaphore and hence will
	 * wait for Online Rollback to complete.
	 */
	assert(udi->grabbed_ftok_sem || (jgbl.onlnrlbk && udi->s_addrs.now_crit));
	assert(holds_sem[SOURCE][JNL_POOL_ACCESS_SEM]);
	assert(NULL != jnlpool->gtmsource_local_array);
	assert(NULL != jnlpool->gtmsrc_lcl_array);
	assert(NULL != jnlpool->repl_inst_filehdr);
	assert(NULL != jnlpool->jnlpool_ctl);
	assert((sm_uc_ptr_t)jnlpool->gtmsrc_lcl_array == (sm_uc_ptr_t)jnlpool->repl_inst_filehdr + REPL_INST_HDR_SIZE);
	/* Reset the instance file header fields (if needed) before flushing and removing the journal pool shared memory */
	if (reset_crash)
		jnlpool->repl_inst_filehdr->crash = FALSE;
	if (!jgbl.onlnrlbk)
	{
		if (reset_replpool_fields)
		{
			jnlpool->repl_inst_filehdr->jnlpool_semid = INVALID_SEMID;
			jnlpool->repl_inst_filehdr->jnlpool_shmid = INVALID_SHMID;
			jnlpool->repl_inst_filehdr->recvpool_semid = INVALID_SEMID;	/* Just in case it is not already reset */
			jnlpool->repl_inst_filehdr->recvpool_shmid = INVALID_SHMID;	/* Just in case it is not already reset */
		}
	}
	/* If the source server that created the journal pool died before it was completely initialized in "gtmsource_seqno_init"
	 * do not copy seqnos from the journal pool into the instance file header. Instead keep the instance file header unchanged.
	 */
	if (jnlpool->jnlpool_ctl->pool_initialized)
	{
		assert(jnlpool->jnlpool_ctl->start_jnl_seqno);
		assert(jnlpool->jnlpool_ctl->jnl_seqno);
		jnlpool->repl_inst_filehdr->jnl_seqno = jnlpool->jnlpool_ctl->jnl_seqno;
		COPY_JCTL_STRMSEQNO_TO_INSTHDR_IF_NEEDED; /* Keep the file header copy of "strm_seqno" uptodate with jnlpool_ctl */
		/* Copy all "gtmsource_local" to corresponding "gtmsrc_lcl" structures before flushing to instance file */
		gtmsourcelocal_ptr = &jnlpool->gtmsource_local_array[0];
		gtmsrclcl_ptr = &jnlpool->gtmsrc_lcl_array[0];
		for (index = 0; index < NUM_GTMSRC_LCL; index++, gtmsourcelocal_ptr++, gtmsrclcl_ptr++)
			COPY_GTMSOURCELOCAL_TO_GTMSRCLCL(gtmsourcelocal_ptr, gtmsrclcl_ptr);
		repl_inst_write(udi->fn, (off_t)0, (sm_uc_ptr_t)jnlpool->repl_inst_filehdr, REPL_INST_HDR_SIZE + GTMSRC_LCL_SIZE);
	} else
		repl_inst_write(udi->fn, (off_t)0, (sm_uc_ptr_t)jnlpool->repl_inst_filehdr, REPL_INST_HDR_SIZE);
}

/* This function determines if this replication instance was formerly a root primary. It finds this out by looking at the
 * last histinfo record in the instance file and comparing the "root_primary_instname" field there with this instance name.
 * If they are the same, it means the last histinfo was generated by this instance and hence was a root primary then. This
 * function will only be invoked by a propagating primary instance (RECEIVER SERVER or ROLLBACK -FETCHRESYNC) or a rootprimary
 * supplementary instance.
 *
 * It returns TRUE only if the instance file header field "was_rootprimary" is TRUE and if the last histinfo record was generated
 * by this instance. It returns FALSE otherwise.
 */
boolean_t	repl_inst_was_rootprimary(void)
{
	int4		histinfo_num, status;
	repl_histinfo	temphistinfo, *last_histinfo = &temphistinfo;
	boolean_t	was_rootprimary, was_crit = FALSE;
	sgmnt_addrs	*csa;
	DCL_THREADGBL_ACCESS;

	SETUP_THREADGBL_ACCESS;
	assert(!jgbl.mur_rollback || !jgbl.mur_options_forward); /* ROLLBACK -FORWARD should not call this function */
	if ((NULL != jnlpool) && (NULL != jnlpool->jnlpool_ctl))
	{	/* If the journal pool is available (indicated by NULL != jnlpool_ctl), we expect jnlpool_dummy_reg to be open.
		 * The only exception is online rollback which doesn't do a jnlpool_init thereby leaving jnlpool_dummy_reg->open
		 * to be FALSE. Assert accordingly.
		 */
		assert(((NULL != jnlpool->jnlpool_dummy_reg) && jnlpool->jnlpool_dummy_reg->open)
				|| jgbl.onlnrlbk || (jgbl.mur_rollback && INST_FREEZE_ON_ERROR_POLICY));
		csa = &FILE_INFO(jnlpool->jnlpool_dummy_reg)->s_addrs;
		ASSERT_VALID_JNLPOOL(csa);
		assert(csa->now_crit);
		if (!jnlpool->jnlpool_ctl->upd_disabled)
		{	/* This instance is a root primary supplementary instance where an online rollback is being run
			 * (either explicitly or implicitly through a receiver server started with -autorollback)
			 * Since this instance is currently a rootprimary, was_rootprimary is FALSE. Return right away.
			 */
			return FALSE;
		}
	} else
		assert(jgbl.mur_rollback); /* ROLLBACK (holding access control lock) can come here without journal pool */
	/* If this is a supplementary instance, look at the last history record corresponding to the 0th stream index.
	 * If not, look at the last history record. This is okay since there is no multiple streams in this case.
	 */
	histinfo_num = (!jnlpool->repl_inst_filehdr->is_supplementary) ? (jnlpool->repl_inst_filehdr->num_histinfo - 1)
									: jnlpool->repl_inst_filehdr->last_histinfo_num[0];
	was_rootprimary = jnlpool->repl_inst_filehdr->was_rootprimary;
	assert(histinfo_num < jnlpool->repl_inst_filehdr->num_alloc_histinfo);
	if (was_rootprimary && (0 <= histinfo_num))
	{
		status = repl_inst_histinfo_get(histinfo_num, last_histinfo);
		assert(0 == status);	/* Since the histinfo_num we are passing is >=0 and < num_histinfo */
		was_rootprimary = !STRCMP(last_histinfo->root_primary_instname,
					jnlpool->repl_inst_filehdr->inst_info.this_instname);
	} else
		was_rootprimary = FALSE;
	return was_rootprimary;
}

/* This function resets "zqgblmod_seqno" and "zqgblmod_tn" in all replicated database file headers to 0.
 * This shares a lot of its code with the function "gtmsource_update_zqgblmod_seqno_and_tn".
 * Any changes there might need to be reflected here.
 */
int4	repl_inst_reset_zqgblmod_seqno_and_tn(void)
{
	gd_region		*reg, *reg_top;
	int			ret;
	boolean_t		all_files_open;
	sgmnt_addrs		*repl_csa;

	ret = SS_NORMAL; /* assume success */
	/* source server calls this from gtmsource_losttncomplete which always holds the journal pool access control semaphore
	 * Assert this.
	 */
	assert(is_rcvr_server || holds_sem[SOURCE][JNL_POOL_ACCESS_SEM]);
	if (0 == jnlpool->jnlpool_ctl->max_zqgblmod_seqno)
	{	/* Already reset to 0 by a previous call to this function. No need to do it again. */
		return ret;
	}
	/* This function is currently ONLY called by receiver server AND mupip replic -source -losttncomplete
	 * The receiver should have NO GBLDIR or REGION OPEN at this time. Assert that.
	 * is_src_server is not set for -losttncomplete so other check needed
	 */
	assert(holds_sem[SOURCE][JNL_POOL_ACCESS_SEM] || (NULL == gd_header));
	if (NULL == gd_header)
		gvinit();
	/* We use the same code dse uses to open all regions but we must make sure they are all open before proceeding. */
	all_files_open = region_init(FALSE);
	if (!all_files_open)
		RTS_ERROR_CSA_ABT(NULL, VARLSTCNT(1) ERR_NOTALLDBOPN);
	repl_csa = &FILE_INFO(jnlpool->jnlpool_dummy_reg)->s_addrs;
	for (reg = gd_header->regions, reg_top = reg + gd_header->n_regions;  reg < reg_top;  reg++)
	{
		assert(reg->open);
		TP_CHANGE_REG(reg);
		if (!REPL_ALLOWED(cs_data))
			continue;
		/* csa->hdr->zqgblmod_seqno is modified by the source server and an online rollback (both of these hold the
		 * database crit while doing so). It is also read by fileheader_sync() which does so while holding crit.
		 * To avoid the latter from reading an inconsistent value (i.e neither the pre-update nor the post-update
		 * value, which is possible if the 8-byte operation is not atomic but a sequence of two 4-byte operations
		 * AND if the pre-update and post-update value differ in their most significant 4-bytes) we grab_crit. We
		 * could have used QWCHANGE_IS_READER_CONSISTENT macro (which checks for most significant 4-byte difference)
		 * instead to determine if it is really necessary to grab crit. But, since the update to zqgblmod_seqno is a
		 * rare operation, we decided to play it safe.
		 */
		assert(!cs_addrs->hold_onto_crit);	/* this ensures we can safely do unconditional grab_crit and rel_crit */
		grab_crit(reg, WS_37);
		if (cs_addrs->onln_rlbk_cycle != cs_addrs->nl->onln_rlbk_cycle)
		{	/* concurrent online rollback */
			assert(is_rcvr_server);
			SYNC_ONLN_RLBK_CYCLES;
			rel_crit(reg);
			ret = -1; /* failure */
			break;
		}
		cs_addrs->hdr->zqgblmod_seqno = (seq_num)0;
		cs_addrs->hdr->zqgblmod_tn = (trans_num)0;
		rel_crit(reg);
	}
	assert((SS_NORMAL == ret) || (reg < reg_top));
	if (reg >= reg_top)
	{
		assert(!repl_csa->hold_onto_crit); /* so we can do unconditional grab_lock and rel_lock */
		/* Since the source server holds the access control at this point, a concurrent online rollback is NOT possible.
		 * But, if we are here from receiver code, then we cannot guarantee this. So, get the journal pool lock and if
		 * an online rollback is detected, return without resetting max_zqgblmod_seqno. The caller knows to take appropriate
		 * action (on seeing -1 as the return code).
		 */
		grab_lock(jnlpool->jnlpool_dummy_reg, TRUE, GRAB_LOCK_ONLY);
		if (repl_csa->onln_rlbk_cycle != jnlpool->jnlpool_ctl->onln_rlbk_cycle)
		{
			assert(is_rcvr_server);
			SYNC_ONLN_RLBK_CYCLES;
			rel_lock(jnlpool->jnlpool_dummy_reg);
			ret = -1; /* failure */
		} else
		{
			jnlpool->jnlpool_ctl->max_zqgblmod_seqno = 0;
			rel_lock(jnlpool->jnlpool_dummy_reg);
		}
	}
	for (reg = gd_header->regions, reg_top = reg + gd_header->n_regions;  reg < reg_top;  reg++)
	{	/* Rundown all databases that we opened as we dont need them anymore. This is not done in the previous
		 * loop as it has to wait until the ftok semaphore of the instance file has been released as otherwise
		 * an assert in "gds_rundown" will fail as it tries to get the ftok semaphore of the database while holding
		 * another ftok semaphore already.
		 */
		assert(reg->open);
		TP_CHANGE_REG(reg);
		assert(!cs_addrs->now_crit);
		ret |= gds_rundown(CLEANUP_UDI_TRUE, FALSE);
	}
	assert(!repl_csa->now_crit);
	return ret;
}