summaryrefslogtreecommitdiff
path: root/bdb/db/db_dispatch.c
blob: c9beac401a727d825590cd405ab55eb0bd6f2057 (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998, 1999, 2000
 *	Sleepycat Software.  All rights reserved.
 */
/*
 * Copyright (c) 1995, 1996
 *	The President and Fellows of Harvard University.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Margo Seltzer.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "db_config.h"

#ifndef lint
static const char revid[] = "$Id: db_dispatch.c,v 11.41 2001/01/11 18:19:50 bostic Exp $";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#endif

#include "db_int.h"
#include "db_page.h"
#include "db_dispatch.h"
#include "db_am.h"
#include "log_auto.h"
#include "txn.h"
#include "txn_auto.h"
#include "log.h"

static int __db_txnlist_find_internal __P((void *, db_txnlist_type,
	     u_int32_t, u_int8_t [DB_FILE_ID_LEN], DB_TXNLIST **, int));

/*
 * __db_dispatch --
 *
 * This is the transaction dispatch function used by the db access methods.
 * It is designed to handle the record format used by all the access
 * methods (the one automatically generated by the db_{h,log,read}.sh
 * scripts in the tools directory).  An application using a different
 * recovery paradigm will supply a different dispatch function to txn_open.
 *
 * PUBLIC: int __db_dispatch __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
 */
int
__db_dispatch(dbenv, db, lsnp, redo, info)
	DB_ENV *dbenv;		/* The environment. */
	DBT *db;		/* The log record upon which to dispatch. */
	DB_LSN *lsnp;		/* The lsn of the record being dispatched. */
	db_recops redo;		/* Redo this op (or undo it). */
	void *info;
{
	u_int32_t rectype, txnid;
	int make_call, ret;

	memcpy(&rectype, db->data, sizeof(rectype));
	memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
	make_call = ret = 0;

	/*
	 * If we find a record that is in the user's number space and they
	 * have specified a recovery routine, let them handle it.  If they
	 * didn't specify a recovery routine, then we expect that they've
	 * followed all our rules and registered new recovery functions.
	 */
	switch (redo) {
	case DB_TXN_ABORT:
		/*
		 * XXX
		 * db_printlog depends on DB_TXN_ABORT not examining the TXN
		 * list.  If that ever changes, fix db_printlog too.
		 */
		make_call = 1;
		break;
	case DB_TXN_OPENFILES:
		if (rectype == DB_log_register)
			return (dbenv->dtab[rectype](dbenv,
			    db, lsnp, redo, info));
		break;
	case DB_TXN_BACKWARD_ROLL:
		/*
		 * Running full recovery in the backward pass.  If we've
		 * seen this txnid before and added to it our commit list,
		 * then we do nothing during this pass, unless this is a child
		 * commit record, in which case we need to process it.  If
		 * we've never seen it, then we call the appropriate recovery
		 * routine.
		 *
		 * We need to always undo DB_db_noop records, so that we
		 * properly handle any aborts before the file was closed.
		 */
		if (rectype == DB_log_register ||
		    rectype == DB_txn_ckp || rectype == DB_db_noop
		    || rectype == DB_txn_child || (txnid != 0 &&
		    (ret = __db_txnlist_find(info, txnid)) != 0)) {
			make_call = 1;
			if (ret == DB_NOTFOUND && rectype != DB_txn_regop &&
			    rectype != DB_txn_xa_regop && (ret =
			    __db_txnlist_add(dbenv, info, txnid, 1)) != 0)
				return (ret);
		}
		break;
	case DB_TXN_FORWARD_ROLL:
		/*
		 * In the forward pass, if we haven't seen the transaction,
		 * do nothing, else recovery it.
		 *
		 * We need to always redo DB_db_noop records, so that we
		 * properly handle any commits after the file was closed.
		 */
		if (rectype == DB_log_register ||
		    rectype == DB_txn_ckp ||
		    rectype == DB_db_noop ||
		    __db_txnlist_find(info, txnid) == 0)
			make_call = 1;
		break;
	default:
		return (__db_unknown_flag(dbenv, "__db_dispatch", redo));
	}

	if (make_call) {
		if (rectype >= DB_user_BEGIN && dbenv->tx_recover != NULL)
			return (dbenv->tx_recover(dbenv, db, lsnp, redo));
		else
			return (dbenv->dtab[rectype](dbenv, db, lsnp, redo, info));
	}

	return (0);
}

/*
 * __db_add_recovery --
 *
 * PUBLIC: int __db_add_recovery __P((DB_ENV *,
 * PUBLIC:    int (*)(DB_ENV *, DBT *, DB_LSN *, db_recops, void *), u_int32_t));
 */
int
__db_add_recovery(dbenv, func, ndx)
	DB_ENV *dbenv;
	int (*func) __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
	u_int32_t ndx;
{
	u_int32_t i, nsize;
	int ret;

	/* Check if we have to grow the table. */
	if (ndx >= dbenv->dtab_size) {
		nsize = ndx + 40;
		if ((ret = __os_realloc(dbenv,
		    nsize * sizeof(dbenv->dtab[0]), NULL, &dbenv->dtab)) != 0)
			return (ret);
		for (i = dbenv->dtab_size; i < nsize; ++i)
			dbenv->dtab[i] = NULL;
		dbenv->dtab_size = nsize;
	}

	dbenv->dtab[ndx] = func;
	return (0);
}

/*
 * __deprecated_recover --
 *	Stub routine for deprecated recovery functions.
 *
 * PUBLIC: int __deprecated_recover
 * PUBLIC:     __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
 */
int
__deprecated_recover(dbenv, dbtp, lsnp, op, info)
	DB_ENV *dbenv;
	DBT *dbtp;
	DB_LSN *lsnp;
	db_recops op;
	void *info;
{
	COMPQUIET(dbenv, NULL);
	COMPQUIET(dbtp, NULL);
	COMPQUIET(lsnp, NULL);
	COMPQUIET(op, 0);
	COMPQUIET(info, NULL);
	return (EINVAL);
}

/*
 * __db_txnlist_init --
 *	Initialize transaction linked list.
 *
 * PUBLIC: int __db_txnlist_init __P((DB_ENV *, void *));
 */
int
__db_txnlist_init(dbenv, retp)
	DB_ENV *dbenv;
	void *retp;
{
	DB_TXNHEAD *headp;
	int ret;

	if ((ret = __os_malloc(dbenv, sizeof(DB_TXNHEAD), NULL, &headp)) != 0)
		return (ret);

	LIST_INIT(&headp->head);
	headp->maxid = 0;
	headp->generation = 1;

	*(void **)retp = headp;
	return (0);
}

/*
 * __db_txnlist_add --
 *	Add an element to our transaction linked list.
 *
 * PUBLIC: int __db_txnlist_add __P((DB_ENV *, void *, u_int32_t, int32_t));
 */
int
__db_txnlist_add(dbenv, listp, txnid, aborted)
	DB_ENV *dbenv;
	void *listp;
	u_int32_t txnid;
	int32_t aborted;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *elp;
	int ret;

	if ((ret = __os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &elp)) != 0)
		return (ret);

	hp = (DB_TXNHEAD *)listp;
	LIST_INSERT_HEAD(&hp->head, elp, links);

	elp->type = TXNLIST_TXNID;
	elp->u.t.txnid = txnid;
	elp->u.t.aborted = aborted;
	if (txnid > hp->maxid)
		hp->maxid = txnid;
	elp->u.t.generation = hp->generation;

	return (0);
}
/*
 * __db_txnlist_remove --
 *	Remove an element from our transaction linked list.
 *
 * PUBLIC: int __db_txnlist_remove __P((void *, u_int32_t));
 */
int
__db_txnlist_remove(listp, txnid)
	void *listp;
	u_int32_t txnid;
{
	DB_TXNLIST *entry;

	return (__db_txnlist_find_internal(listp,
	     TXNLIST_TXNID, txnid, NULL, &entry, 1));
}

/* __db_txnlist_close --
 *
 *	Call this when we close a file.  It allows us to reconcile whether
 * we have done any operations on this file with whether the file appears
 * to have been deleted.  If you never do any operations on a file, then
 * we assume it's OK to appear deleted.
 *
 * PUBLIC: int __db_txnlist_close __P((void *, int32_t, u_int32_t));
 */

int
__db_txnlist_close(listp, lid, count)
	void *listp;
	int32_t lid;
	u_int32_t count;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *p;

	hp = (DB_TXNHEAD *)listp;
	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
		if (p->type == TXNLIST_DELETE)
			if (lid == p->u.d.fileid &&
			    !F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED)) {
				p->u.d.count += count;
				return (0);
			}
	}

	return (0);
}

/*
 * __db_txnlist_delete --
 *
 *	Record that a file was missing or deleted.  If the deleted
 * flag is set, then we've encountered a delete of a file, else we've
 * just encountered a file that is missing.  The lid is the log fileid
 * and is only meaningful if deleted is not equal to 0.
 *
 * PUBLIC: int __db_txnlist_delete __P((DB_ENV *,
 * PUBLIC:     void *, char *, u_int32_t, int));
 */
int
__db_txnlist_delete(dbenv, listp, name, lid, deleted)
	DB_ENV *dbenv;
	void *listp;
	char *name;
	u_int32_t lid;
	int deleted;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *p;
	int ret;

	hp = (DB_TXNHEAD *)listp;
	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
		if (p->type == TXNLIST_DELETE)
			if (strcmp(name, p->u.d.fname) == 0) {
				if (deleted)
					F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
				else
					F_CLR(&p->u.d, TXNLIST_FLAG_CLOSED);
				return (0);
			}
	}

	/* Need to add it. */
	if ((ret = __os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &p)) != 0)
		return (ret);
	LIST_INSERT_HEAD(&hp->head, p, links);

	p->type = TXNLIST_DELETE;
	p->u.d.flags = 0;
	if (deleted)
		F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
	p->u.d.fileid = lid;
	p->u.d.count = 0;
	ret = __os_strdup(dbenv, name, &p->u.d.fname);

	return (ret);
}

/*
 * __db_txnlist_end --
 *	Discard transaction linked list. Print out any error messages
 * for deleted files.
 *
 * PUBLIC: void __db_txnlist_end __P((DB_ENV *, void *));
 */
void
__db_txnlist_end(dbenv, listp)
	DB_ENV *dbenv;
	void *listp;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *p;
	DB_LOG *lp;

	hp = (DB_TXNHEAD *)listp;
	lp = (DB_LOG *)dbenv->lg_handle;
	while (hp != NULL && (p = LIST_FIRST(&hp->head)) != NULL) {
		LIST_REMOVE(p, links);
		switch (p->type) {
		case TXNLIST_DELETE:
			/*
			 * If we have a file that is not deleted and has
			 * some operations, we flag the warning.  Since
			 * the file could still be open, we need to check
			 * the actual log table as well.
			 */
			if ((!F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) &&
			    p->u.d.count != 0) ||
			    (!F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) &&
			    p->u.d.fileid != (int32_t) TXNLIST_INVALID_ID &&
			    p->u.d.fileid < lp->dbentry_cnt &&
			    lp->dbentry[p->u.d.fileid].count != 0))
				__db_err(dbenv, "warning: %s: %s",
				    p->u.d.fname, db_strerror(ENOENT));
			__os_freestr(p->u.d.fname);
			break;
		case TXNLIST_LSN:
			__os_free(p->u.l.lsn_array,
			    p->u.l.maxn * sizeof(DB_LSN));
			break;
		default:
			/* Possibly an incomplete DB_TXNLIST;  just free it. */
			break;
		}
		__os_free(p, sizeof(DB_TXNLIST));
	}
	__os_free(listp, sizeof(DB_TXNHEAD));
}

/*
 * __db_txnlist_find --
 *	Checks to see if a txnid with the current generation is in the
 *	txnid list.  This returns DB_NOTFOUND if the item isn't in the
 *	list otherwise it returns (like __db_txnlist_find_internal) a
 *	1 or 0 indicating if the transaction is aborted or not.  A txnid
 *	of 0 means the record was generated while not in a transaction.
 *
 * PUBLIC: int __db_txnlist_find __P((void *, u_int32_t));
 */
int
__db_txnlist_find(listp, txnid)
	void *listp;
	u_int32_t txnid;
{
	DB_TXNLIST *entry;

	if (txnid == 0)
		return (DB_NOTFOUND);
	return (__db_txnlist_find_internal(listp,
	     TXNLIST_TXNID, txnid, NULL, &entry, 0));
}

/*
 * __db_txnlist_find_internal --
 *	Find an entry on the transaction list.
 * If the entry is not there or the list pointeris not initialized
 * we return DB_NOTFOUND.  If the item is found, we return the aborted
 * status (1 for aborted, 0 for not aborted).  Currently we always call
 * this with an initialized list pointer but checking for NULL keeps it general.
 */
static int
__db_txnlist_find_internal(listp, type, txnid, uid, txnlistp, delete)
	void *listp;
	db_txnlist_type type;
	u_int32_t txnid;
	u_int8_t uid[DB_FILE_ID_LEN];
	DB_TXNLIST **txnlistp;
	int delete;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *p;
	int ret;

	if ((hp = (DB_TXNHEAD *)listp) == NULL)
		return (DB_NOTFOUND);

	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
		if (p->type != type)
			continue;
		switch (type) {
		case TXNLIST_TXNID:
			if (p->u.t.txnid != txnid ||
			    hp->generation != p->u.t.generation)
				continue;
			ret = p->u.t.aborted;
			break;

		case TXNLIST_PGNO:
			if (memcmp(uid, p->u.p.uid, DB_FILE_ID_LEN) != 0)
				continue;

			ret = 0;
			break;
		default:
			DB_ASSERT(0);
			ret = EINVAL;
		}
		if (delete == 1) {
			LIST_REMOVE(p, links);
			__os_free(p, sizeof(DB_TXNLIST));
		} else if (p != LIST_FIRST(&hp->head)) {
			/* Move it to head of list. */
			LIST_REMOVE(p, links);
			LIST_INSERT_HEAD(&hp->head, p, links);
		}
		*txnlistp = p;
		return (ret);
	}

	return (DB_NOTFOUND);
}

/*
 * __db_txnlist_gen --
 *	Change the current generation number.
 *
 * PUBLIC: void __db_txnlist_gen __P((void *, int));
 */
void
__db_txnlist_gen(listp, incr)
	void *listp;
	int incr;
{
	DB_TXNHEAD *hp;

	/*
	 * During recovery generation numbers keep track of how many "restart"
	 * checkpoints we've seen.  Restart checkpoints occur whenever we take
	 * a checkpoint and there are no outstanding transactions.  When that
	 * happens, we can reset transaction IDs back to 1.  It always happens
	 * at recovery and it prevents us from exhausting the transaction IDs
	 * name space.
	 */
	hp = (DB_TXNHEAD *)listp;
	hp->generation += incr;
}

#define	TXN_BUBBLE(AP, MAX) {						\
	int __j;							\
	DB_LSN __tmp;							\
									\
	for (__j = 0; __j < MAX - 1; __j++)				\
		if (log_compare(&AP[__j], &AP[__j + 1]) < 0) {		\
			__tmp = AP[__j];				\
			AP[__j] = AP[__j + 1];				\
			AP[__j + 1] = __tmp;				\
		}							\
}

/*
 * __db_txnlist_lsnadd --
 *	Add to or re-sort the transaction list lsn entry.
 * Note that since this is used during an abort, the __txn_undo
 * code calls into the "recovery" subsystem explicitly, and there
 * is only a single TXNLIST_LSN entry on the list.
 *
 * PUBLIC: int __db_txnlist_lsnadd __P((DB_ENV *, void *, DB_LSN *, u_int32_t));
 */
int
__db_txnlist_lsnadd(dbenv, listp, lsnp, flags)
	DB_ENV *dbenv;
	void *listp;
	DB_LSN *lsnp;
	u_int32_t flags;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *elp;
	int i, ret;

	hp = (DB_TXNHEAD *)listp;

	for (elp = LIST_FIRST(&hp->head);
	    elp != NULL; elp = LIST_NEXT(elp, links))
		if (elp->type == TXNLIST_LSN)
			break;

	if (elp == NULL)
		return (EINVAL);

	if (LF_ISSET(TXNLIST_NEW)) {
		if (elp->u.l.ntxns >= elp->u.l.maxn) {
			if ((ret = __os_realloc(dbenv,
			    2 * elp->u.l.maxn * sizeof(DB_LSN),
			    NULL, &elp->u.l.lsn_array)) != 0)
				return (ret);
			elp->u.l.maxn *= 2;
		}
		elp->u.l.lsn_array[elp->u.l.ntxns++] = *lsnp;
	} else
		/* Simply replace the 0th element. */
		elp->u.l.lsn_array[0] = *lsnp;

	/*
	 * If we just added a new entry and there may be NULL
	 * entries, so we have to do a complete bubble sort,
	 * not just trickle a changed entry around.
	 */
	for (i = 0; i < (!LF_ISSET(TXNLIST_NEW) ? 1 : elp->u.l.ntxns); i++)
		TXN_BUBBLE(elp->u.l.lsn_array, elp->u.l.ntxns);

	*lsnp = elp->u.l.lsn_array[0];

	return (0);
}

/*
 * __db_txnlist_lsnhead --
 *	Return a pointer to the beginning of the lsn_array.
 *
 * PUBLIC: int __db_txnlist_lsnhead __P((void *, DB_LSN **));
 */
int
__db_txnlist_lsnhead(listp, lsnpp)
	void *listp;
	DB_LSN **lsnpp;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *elp;

	hp = (DB_TXNHEAD *)listp;

	for (elp = LIST_FIRST(&hp->head);
	    elp != NULL; elp = LIST_NEXT(elp, links))
		if (elp->type == TXNLIST_LSN)
			break;

	if (elp == NULL)
		return (EINVAL);

	*lsnpp = &elp->u.l.lsn_array[0];

	return (0);
}

/*
 * __db_txnlist_lsninit --
 *	Initialize a transaction list with an lsn array entry.
 *
 * PUBLIC: int __db_txnlist_lsninit __P((DB_ENV *, DB_TXNHEAD *, DB_LSN *));
 */
int
__db_txnlist_lsninit(dbenv, hp, lsnp)
	DB_ENV *dbenv;
	DB_TXNHEAD *hp;
	DB_LSN *lsnp;
{
	DB_TXNLIST *elp;
	int ret;

	elp = NULL;

	if ((ret = __os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &elp)) != 0)
		goto err;
	LIST_INSERT_HEAD(&hp->head, elp, links);

	if ((ret = __os_malloc(dbenv,
	    12 * sizeof(DB_LSN), NULL, &elp->u.l.lsn_array)) != 0)
		goto err;
	elp->type = TXNLIST_LSN;
	elp->u.l.maxn = 12;
	elp->u.l.ntxns = 1;
	elp->u.l.lsn_array[0] = *lsnp;

	return (0);

err:	__db_txnlist_end(dbenv, hp);
	return (ret);
}

/*
 * __db_add_limbo -- add pages to the limbo list.
 *	Get the file information and call pgnoadd
 * for each page.
 *
 * PUBLIC: int __db_add_limbo __P((DB_ENV *,
 * PUBLIC:      void *, int32_t, db_pgno_t, int32_t));
 */
int
__db_add_limbo(dbenv, info, fileid, pgno, count)
	DB_ENV *dbenv;
	void *info;
	int32_t fileid;
	db_pgno_t pgno;
	int32_t count;
{
	DB_LOG *dblp;
	FNAME *fnp;
	int ret;

	dblp = dbenv->lg_handle;
	if ((ret = __log_lid_to_fname(dblp, fileid, &fnp)) != 0)
		return (ret);

	do {
		if ((ret =
		    __db_txnlist_pgnoadd(dbenv, info, fileid, fnp->ufid,
		    R_ADDR(&dblp->reginfo, fnp->name_off), pgno)) != 0)
			return (ret);
		pgno++;
	} while (--count != 0);

	return (0);
}

/*
 * __db_do_the_limbo -- move pages from limbo to free.
 *
 * If we are in recovery we add things to the free list without
 * logging becasue we want to incrementaly apply logs that
 * may be generated on another copy of this environment.
 * Otherwise we just call __db_free to put the pages on
 * the free list and log the activity.
 *
 * PUBLIC: int __db_do_the_limbo __P((DB_ENV *, DB_TXNHEAD *));
 */
int
__db_do_the_limbo(dbenv, hp)
	DB_ENV *dbenv;
	DB_TXNHEAD *hp;
{
	DB *dbp;
	DBC *dbc;
	DBMETA *meta;
	DB_TXN *txn;
	DB_TXNLIST *elp;
	PAGE *pagep;
	db_pgno_t last_pgno, pgno;
	int i, in_recover, put_page, ret, t_ret;

	dbp = NULL;
	dbc = NULL;
	txn = NULL;
	ret = 0;

	/* Are we in recovery? */
	in_recover = F_ISSET((DB_LOG *)dbenv->lg_handle, DBLOG_RECOVER);

	for (elp = LIST_FIRST(&hp->head);
	    elp != NULL; elp = LIST_NEXT(elp, links)) {
		if (elp->type != TXNLIST_PGNO)
			continue;

		if (in_recover) {
			if ((ret = db_create(&dbp, dbenv, 0)) != 0)
				goto err;

			/*
			 * It is ok if the file is nolonger there.
			 */
			dbp->type = DB_UNKNOWN;
			ret = __db_dbopen(dbp,
			    elp->u.p.fname, 0, __db_omode("rw----"), 0);
		} else {
			/*
			 * If we are in transaction undo, then we know
			 * the fileid is still correct.
			 */
			if ((ret =
			    __db_fileid_to_db(dbenv, &dbp,
			    elp->u.p.fileid, 0)) != 0 && ret != DB_DELETED)
				goto err;
			/* File is being destroyed. */
			if (F_ISSET(dbp, DB_AM_DISCARD))
				ret = DB_DELETED;
		}
		/*
		 * Verify that we are opening the same file that we were
		 * referring to when we wrote this log record.
		 */
		if (ret == 0 &&
		    memcmp(elp->u.p.uid, dbp->fileid, DB_FILE_ID_LEN) == 0) {
			last_pgno = PGNO_INVALID;
			if (in_recover) {
				pgno = PGNO_BASE_MD;
				if ((ret = memp_fget(dbp->mpf,
				    &pgno, 0, (PAGE **)&meta)) != 0)
					goto err;
				last_pgno = meta->free;
				/*
				 * Check to see if the head of the free
				 * list is any of the pages we are about
				 * to link in.  We could have crashed
				 * after linking them in and before writing
				 * a checkpoint.
				 * It may not be the last one since
				 * any page may get reallocated before here.
				 */
				if (last_pgno != PGNO_INVALID)
					for (i = 0; i < elp->u.p.nentries; i++)
						if (last_pgno
						     == elp->u.p.pgno_array[i])
							goto done_it;
			}

			for (i = 0; i < elp->u.p.nentries; i++) {
				pgno = elp->u.p.pgno_array[i];
				if ((ret = memp_fget(dbp->mpf,
				    &pgno, DB_MPOOL_CREATE, &pagep)) != 0)
					goto err;

				put_page = 1;
				if (IS_ZERO_LSN(LSN(pagep))) {
					P_INIT(pagep, dbp->pgsize,
					    pgno, PGNO_INVALID,
					    last_pgno, 0, P_INVALID);

					if (in_recover) {
						LSN(pagep) = LSN(meta);
						last_pgno = pgno;
					} else {
						/*
						 * Starting the transaction
						 * is postponed until we know
						 * we have something to do.
						 */
						if (txn == NULL &&
						    (ret = txn_begin(dbenv,
						    NULL, &txn, 0)) != 0)
							goto err;

						if (dbc == NULL &&
						    (ret = dbp->cursor(dbp,
						    txn, &dbc, 0)) != 0)
							goto err;
						/* Turn off locking. */
						F_SET(dbc, DBC_COMPENSATE);

						/* __db_free puts the page. */
						if ((ret =
						    __db_free(dbc, pagep)) != 0)
							goto err;
						put_page = 0;
					}
				}

				if (put_page == 1 &&
				    (ret = memp_fput(dbp->mpf,
				    pagep, DB_MPOOL_DIRTY)) != 0)
					goto err;
			}
			if (in_recover) {
				if (last_pgno == meta->free) {
done_it:
					if ((ret =
					    memp_fput(dbp->mpf, meta, 0)) != 0)
						goto err;
				} else {
					/*
					 * Flush the new free list then
					 * update the metapage.  This is
					 * unlogged so we cannot have the
					 * metapage pointing at pages that
					 * are not on disk.
					 */
					dbp->sync(dbp, 0);
					meta->free = last_pgno;
					if ((ret = memp_fput(dbp->mpf,
					    meta, DB_MPOOL_DIRTY)) != 0)
						goto err;
				}
			}
			if (dbc != NULL && (ret = dbc->c_close(dbc)) != 0)
				goto err;
			dbc = NULL;
		}
		if (in_recover && (t_ret = dbp->close(dbp, 0)) != 0 && ret == 0)
			ret = t_ret;
		dbp = NULL;
		__os_free(elp->u.p.fname, 0);
		__os_free(elp->u.p.pgno_array, 0);
		if (ret == ENOENT)
			ret = 0;
		else if (ret != 0)
			goto err;
	}

	if (txn != NULL) {
		ret = txn_commit(txn, 0);
		txn = NULL;
	}
err:
	if (dbc != NULL)
		(void)dbc->c_close(dbc);
	if (in_recover && dbp != NULL)
		(void)dbp->close(dbp, 0);
	if (txn != NULL)
		(void)txn_abort(txn);
	return (ret);

}

#define	DB_TXNLIST_MAX_PGNO	8 /* A nice even number. */

/*
 * __db_txnlist_pgnoadd --
 *	Find the txnlist entry for a file and add this pgno,
 * or add the list entry for the file and then add the pgno.
 *
 * PUBLIC: int __db_txnlist_pgnoadd __P((DB_ENV *, DB_TXNHEAD *,
 * PUBLIC:      int32_t, u_int8_t [DB_FILE_ID_LEN], char *, db_pgno_t));
 */
int
__db_txnlist_pgnoadd(dbenv, hp, fileid, uid, fname, pgno)
	DB_ENV *dbenv;
	DB_TXNHEAD *hp;
	int32_t fileid;
	u_int8_t uid[DB_FILE_ID_LEN];
	char *fname;
	db_pgno_t pgno;
{
	DB_TXNLIST *elp;
	int len, ret;

	elp = NULL;

	if (__db_txnlist_find_internal(hp, TXNLIST_PGNO, 0, uid, &elp, 0) != 0) {
		if ((ret =
		    __os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &elp)) != 0)
			goto err;
		LIST_INSERT_HEAD(&hp->head, elp, links);
		elp->u.p.fileid = fileid;
		memcpy(elp->u.p.uid, uid, DB_FILE_ID_LEN);

		len = strlen(fname) + 1;
		if ((ret = __os_malloc(dbenv, len, NULL, &elp->u.p.fname)) != 0)
			goto err;
		memcpy(elp->u.p.fname, fname, len);

		elp->u.p.maxentry = 0;
		elp->type = TXNLIST_PGNO;
		if ((ret = __os_malloc(dbenv,
		    8 * sizeof(db_pgno_t), NULL, &elp->u.p.pgno_array)) != 0)
			goto err;
		elp->u.p.maxentry = DB_TXNLIST_MAX_PGNO;
		elp->u.p.nentries = 0;
	} else if (elp->u.p.nentries == elp->u.p.maxentry) {
		elp->u.p.maxentry <<= 1;
		if ((ret = __os_realloc(dbenv, elp->u.p.maxentry *
		    sizeof(db_pgno_t), NULL, &elp->u.p.pgno_array)) != 0)
			goto err;
	}

	elp->u.p.pgno_array[elp->u.p.nentries++] = pgno;

	return (0);

err:	__db_txnlist_end(dbenv, hp);
	return (ret);
}

#ifdef DEBUG
/*
 * __db_txnlist_print --
 *	Print out the transaction list.
 *
 * PUBLIC: void __db_txnlist_print __P((void *));
 */
void
__db_txnlist_print(listp)
	void *listp;
{
	DB_TXNHEAD *hp;
	DB_TXNLIST *p;

	hp = (DB_TXNHEAD *)listp;

	printf("Maxid: %lu Generation: %lu\n",
	    (u_long)hp->maxid, (u_long)hp->generation);
	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
		switch (p->type) {
		case TXNLIST_TXNID:
			printf("TXNID: %lu(%lu)\n",
			    (u_long)p->u.t.txnid, (u_long)p->u.t.generation);
			break;
		case TXNLIST_DELETE:
			printf("FILE: %s id=%d ops=%d %s %s\n",
			    p->u.d.fname, p->u.d.fileid, p->u.d.count,
			    F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) ?
			    "(deleted)" : "(missing)",
			    F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) ?
			    "(closed)" : "(open)");

			break;
		default:
			printf("Unrecognized type: %d\n", p->type);
			break;
		}
	}
}
#endif