summaryrefslogtreecommitdiff
path: root/db/dbreg/dbreg.c
blob: 21334d8d4592fdf873a9e71ec4936d45ee60be90 (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996,2007 Oracle.  All rights reserved.
 *
 * $Id: dbreg.c,v 12.28 2007/06/13 18:59:35 bostic Exp $
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/log.h"
#include "dbinc/txn.h"
#include "dbinc/db_am.h"

static int __dbreg_push_id __P((DB_ENV *, int32_t));
static int __dbreg_pop_id __P((DB_ENV *, int32_t *));
static int __dbreg_pluck_id __P((DB_ENV *, int32_t));

/*
 * The dbreg subsystem, as its name implies, registers database handles so
 * that we can associate log messages with them without logging a filename
 * or a full, unique DB ID.  Instead, we assign each dbp an int32_t which is
 * easy and cheap to log, and use this subsystem to map back and forth.
 *
 * Overview of how dbreg ids are managed:
 *
 * OPEN
 *	dbreg_setup (Creates FNAME struct.)
 *	dbreg_new_id (Assigns new ID to dbp and logs it.  May be postponed
 *	until we attempt to log something else using that dbp, if the dbp
 *	was opened on a replication client.)
 *
 * CLOSE
 *	dbreg_close_id  (Logs closure of dbp/revocation of ID.)
 *	dbreg_revoke_id (As name implies, revokes ID.)
 *	dbreg_teardown (Destroys FNAME.)
 *
 * RECOVERY
 *	dbreg_setup
 *	dbreg_assign_id (Assigns a particular ID we have in the log to a dbp.)
 *
 *	sometimes: dbreg_revoke_id; dbreg_teardown
 *	other times: normal close path
 *
 * A note about locking:
 *
 *	FNAME structures are referenced only by their corresponding dbp's
 *	until they have a valid id.
 *
 *	Once they have a valid id, they must get linked into the log
 *	region list so they can get logged on checkpoints.
 *
 *	An FNAME that may/does have a valid id must be accessed under
 *	protection of the mtx_filelist, with the following exception:
 *
 *	We don't want to have to grab the mtx_filelist on every log
 *	record, and it should be safe not to do so when we're just
 *	looking at the id, because once allocated, the id should
 *	not change under a handle until the handle is closed.
 *
 *	If a handle is closed during an attempt by another thread to
 *	log with it, well, the application doing the close deserves to
 *	go down in flames and a lot else is about to fail anyway.
 *
 *	When in the course of logging we encounter an invalid id
 *	and go to allocate it lazily, we *do* need to check again
 *	after grabbing the mutex, because it's possible to race with
 *	another thread that has also decided that it needs to allocate
 *	a id lazily.
 *
 * See SR #5623 for further discussion of the new dbreg design.
 */

/*
 * __dbreg_setup --
 *	Allocate and initialize an FNAME structure.  The FNAME structures
 * live in the log shared region and map one-to-one with open database handles.
 * When the handle needs to be logged, the FNAME should have a valid fid
 * allocated.  If the handle currently isn't logged, it still has an FNAME
 * entry.  If we later discover that the handle needs to be logged, we can
 * allocate a id for it later.  (This happens when the handle is on a
 * replication client that later becomes a master.)
 *
 * PUBLIC: int __dbreg_setup __P((DB *, const char *, u_int32_t));
 */
int
__dbreg_setup(dbp, name, create_txnid)
	DB *dbp;
	const char *name;
	u_int32_t create_txnid;
{
	DB_ENV *dbenv;
	DB_LOG *dblp;
	FNAME *fnp;
	REGINFO *infop;
	int ret;
	size_t len;
	void *namep;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	infop = &dblp->reginfo;

	fnp = NULL;
	namep = NULL;

	/* Allocate an FNAME and, if necessary, a buffer for the name itself. */
	LOG_SYSTEM_LOCK(dbenv);
	if ((ret = __env_alloc(infop, sizeof(FNAME), &fnp)) != 0)
		goto err;
	memset(fnp, 0, sizeof(FNAME));
	if (name != NULL) {
		len = strlen(name) + 1;
		if ((ret = __env_alloc(infop, len, &namep)) != 0)
			goto err;
		fnp->name_off = R_OFFSET(infop, namep);
		memcpy(namep, name, len);
	} else
		fnp->name_off = INVALID_ROFF;

	LOG_SYSTEM_UNLOCK(dbenv);

	/*
	 * Fill in all the remaining info that we'll need later to register
	 * the file, if we use it for logging.
	 */
	fnp->id = fnp->old_id = DB_LOGFILEID_INVALID;
	fnp->s_type = dbp->type;
	memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN);
	fnp->meta_pgno = dbp->meta_pgno;
	fnp->create_txnid = create_txnid;

	if (F_ISSET(dbp, DB_AM_INMEM))
		F_SET(fnp, DB_FNAME_INMEM);
	if (F_ISSET(dbp, DB_AM_RECOVER))
		F_SET(fnp, DB_FNAME_RECOVER);
	fnp->txn_ref = 1;
	fnp->mutex = dbp->mutex;

	dbp->log_filename = fnp;

	return (0);

err:	LOG_SYSTEM_UNLOCK(dbenv);
	if (ret == ENOMEM)
		__db_errx(dbenv,
    "Logging region out of memory; you may need to increase its size");

	return (ret);
}

/*
 * __dbreg_teardown --
 *	Destroy a DB handle's FNAME struct.  This is only called when closing
 * the DB.
 *
 * PUBLIC: int __dbreg_teardown __P((DB *));
 */
int
__dbreg_teardown(dbp)
	DB *dbp;
{
	int ret;

	/*
	 * We may not have an FNAME if we were never opened.  This is not an
	 * error.
	 */
	if (dbp->log_filename == NULL)
		return (0);

	ret = __dbreg_teardown_int(dbp->dbenv, dbp->log_filename);

	/* We freed the copy of the mutex from the FNAME. */
	dbp->log_filename = NULL;
	dbp->mutex = MUTEX_INVALID;

	return (ret);
}

/*
 * __dbreg_teardown_int --
 *	Destroy an FNAME struct.
 *
 * PUBLIC: int __dbreg_teardown_int __P((DB_ENV *, FNAME *));
 */
int
__dbreg_teardown_int(dbenv, fnp)
	DB_ENV *dbenv;
	FNAME *fnp;
{
	DB_LOG *dblp;
	REGINFO *infop;
	int ret;

	if (F_ISSET(fnp, DB_FNAME_NOTLOGGED))
		return (0);
	dblp = dbenv->lg_handle;
	infop = &dblp->reginfo;

	DB_ASSERT(dbenv, fnp->id == DB_LOGFILEID_INVALID);
	ret = __mutex_free(dbenv, &fnp->mutex);

	LOG_SYSTEM_LOCK(dbenv);
	if (fnp->name_off != INVALID_ROFF)
		__env_alloc_free(infop, R_ADDR(infop, fnp->name_off));
	__env_alloc_free(infop, fnp);
	LOG_SYSTEM_UNLOCK(dbenv);

	return (ret);
}

/*
 * __dbreg_new_id --
 *	Get an unused dbreg id to this database handle.
 *	Used as a wrapper to acquire the mutex and
 *	only set the id on success.
 *
 * PUBLIC: int __dbreg_new_id __P((DB *, DB_TXN *));
 */
int
__dbreg_new_id(dbp, txn)
	DB *dbp;
	DB_TXN *txn;
{
	DB_ENV *dbenv;
	DB_LOG *dblp;
	FNAME *fnp;
	LOG *lp;
	int32_t id;
	int ret;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	fnp = dbp->log_filename;

	/* The mtx_filelist protects the FNAME list and id management. */
	MUTEX_LOCK(dbenv, lp->mtx_filelist);
	if (fnp->id != DB_LOGFILEID_INVALID) {
		MUTEX_UNLOCK(dbenv, lp->mtx_filelist);
		return (0);
	}
	if ((ret = __dbreg_get_id(dbp, txn, &id)) == 0)
		fnp->id = id;
	MUTEX_UNLOCK(dbenv, lp->mtx_filelist);
	return (ret);
}

/*
 * __dbreg_get_id --
 *	Assign an unused dbreg id to this database handle.
 *	Assume the caller holds the mtx_filelist locked.  Assume the
 *	caller will set the fnp->id field with the id we return.
 *
 * PUBLIC: int __dbreg_get_id __P((DB *, DB_TXN *, int32_t *));
 */
int
__dbreg_get_id(dbp, txn, idp)
	DB *dbp;
	DB_TXN *txn;
	int32_t *idp;
{
	DB_ENV *dbenv;
	DB_LOG *dblp;
	FNAME *fnp;
	LOG *lp;
	int32_t id;
	int ret;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	fnp = dbp->log_filename;

	/*
	 * It's possible that after deciding we needed to call this function,
	 * someone else allocated an ID before we grabbed the lock.  Check
	 * to make sure there was no race and we have something useful to do.
	 */
	/* Get an unused ID from the free list. */
	if ((ret = __dbreg_pop_id(dbenv, &id)) != 0)
		goto err;

	/* If no ID was found, allocate a new one. */
	if (id == DB_LOGFILEID_INVALID)
		id = lp->fid_max++;

	/* If the file is durable (i.e., not, not-durable), mark it as such. */
	if (!F_ISSET(dbp, DB_AM_NOT_DURABLE))
		F_SET(fnp, DB_FNAME_DURABLE);

	/* Hook the FNAME into the list of open files. */
	SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname);

	/*
	 * Log the registry.  We should only request a new ID in situations
	 * where logging is reasonable.
	 */
	DB_ASSERT(dbenv, !F_ISSET(dbp, DB_AM_RECOVER));

	if ((ret = __dbreg_log_id(dbp, txn, id, 0)) != 0)
		goto err;

	/*
	 * Once we log the create_txnid, we need to make sure we never
	 * log it again (as might happen if this is a replication client
	 * that later upgrades to a master).
	 */
	fnp->create_txnid = TXN_INVALID;

	DB_ASSERT(dbenv, dbp->type == fnp->s_type);
	DB_ASSERT(dbenv, dbp->meta_pgno == fnp->meta_pgno);

	if ((ret = __dbreg_add_dbentry(dbenv, dblp, dbp, id)) != 0)
		goto err;
	/*
	 * If we have a successful call, set the ID.  Otherwise
	 * we have to revoke it and remove it from all the lists
	 * it has been added to, and return an invalid id.
	 */
err:
	if (ret != 0 && id != DB_LOGFILEID_INVALID) {
		(void)__dbreg_revoke_id(dbp, 1, id);
		id = DB_LOGFILEID_INVALID;
	}
	*idp = id;
	return (ret);
}

/*
 * __dbreg_assign_id --
 *	Assign a particular dbreg id to this database handle.
 *
 * PUBLIC: int __dbreg_assign_id __P((DB *, int32_t));
 */
int
__dbreg_assign_id(dbp, id)
	DB *dbp;
	int32_t id;
{
	DB *close_dbp;
	DB_ENV *dbenv;
	DB_LOG *dblp;
	FNAME *close_fnp, *fnp;
	LOG *lp;
	int ret;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	fnp = dbp->log_filename;

	close_dbp = NULL;
	close_fnp = NULL;

	/* The mtx_filelist protects the FNAME list and id management. */
	MUTEX_LOCK(dbenv, lp->mtx_filelist);

	/* We should only call this on DB handles that have no ID. */
	DB_ASSERT(dbenv, fnp->id == DB_LOGFILEID_INVALID);

	/*
	 * Make sure there isn't already a file open with this ID. There can
	 * be in recovery, if we're recovering across a point where an ID got
	 * reused.
	 */
	if (__dbreg_id_to_fname(dblp, id, 1, &close_fnp) == 0) {
		/*
		 * We want to save off any dbp we have open with this id.  We
		 * can't safely close it now, because we hold the mtx_filelist,
		 * but we should be able to rely on it being open in this
		 * process, and we're running recovery, so no other thread
		 * should muck with it if we just put off closing it until
		 * we're ready to return.
		 *
		 * Once we have the dbp, revoke its id;  we're about to
		 * reuse it.
		 */
		ret = __dbreg_id_to_db_int(dbenv, NULL, &close_dbp, id, 0, 0);
		if (ret == ENOENT) {
			ret = 0;
			goto cont;
		} else if (ret != 0)
			goto err;

		if ((ret = __dbreg_revoke_id(close_dbp, 1,
		    DB_LOGFILEID_INVALID)) != 0)
			goto err;
	}

	/*
	 * Remove this ID from the free list, if it's there, and make sure
	 * we don't allocate it anew.
	 */
cont:	if ((ret = __dbreg_pluck_id(dbenv, id)) != 0)
		goto err;
	if (id >= lp->fid_max)
		lp->fid_max = id + 1;

	/* Now go ahead and assign the id to our dbp. */
	fnp->id = id;
	/* If the file is durable (i.e., not, not-durable), mark it as such. */
	if (!F_ISSET(dbp, DB_AM_NOT_DURABLE))
		F_SET(fnp, DB_FNAME_DURABLE);
	SH_TAILQ_INSERT_HEAD(&lp->fq, fnp, q, __fname);

	/*
	 * If we get an error adding the dbentry, revoke the id.
	 * We void the return value since we want to retain and
	 * return the original error in ret anyway.
	 */
	if ((ret = __dbreg_add_dbentry(dbenv, dblp, dbp, id)) != 0)
		(void)__dbreg_revoke_id(dbp, 1, id);

err:	MUTEX_UNLOCK(dbenv, lp->mtx_filelist);

	/* There's nothing useful that our caller can do if this close fails. */
	if (close_dbp != NULL)
		(void)__db_close(close_dbp, NULL, DB_NOSYNC);

	return (ret);
}

/*
 * __dbreg_revoke_id --
 *	Take a log id away from a dbp, in preparation for closing it,
 *	but without logging the close.
 *
 * PUBLIC: int __dbreg_revoke_id __P((DB *, int, int32_t));
 */
int
__dbreg_revoke_id(dbp, have_lock, force_id)
	DB *dbp;
	int have_lock;
	int32_t force_id;
{
	DB_ENV *dbenv;
	DB_REP *db_rep;
	int push;

	dbenv = dbp->dbenv;

	/*
	 * If we are not in recovery but the file was opened for a recovery
	 * operation, then this process aborted a transaction for another
	 * process and the id may still be in use, so don't reuse this id.
	 * If our fid generation in replication has changed, this fid
	 * should not be reused
	 */
	db_rep = dbenv->rep_handle;
	push = (!F_ISSET(dbp, DB_AM_RECOVER) || IS_RECOVERING(dbenv)) &&
	    (!REP_ON(dbenv) || ((REP *)db_rep->region)->gen == dbp->fid_gen);

	return (__dbreg_revoke_id_int(dbp->dbenv,
	      dbp->log_filename, have_lock, push, force_id));
}
/*
 * __dbreg_revoke_id_int --
 *	Revoke a log, in preparation for closing it, but without logging
 *	the close.
 *
 * PUBLIC: int __dbreg_revoke_id_int
 * PUBLIC:     __P((DB_ENV *, FNAME *, int, int, int32_t));
 */
int
__dbreg_revoke_id_int(dbenv, fnp, have_lock, push, force_id)
	DB_ENV *dbenv;
	FNAME *fnp;
	int have_lock, push;
	int32_t force_id;
{
	DB_LOG *dblp;
	LOG *lp;
	int32_t id;
	int ret;

	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	ret = 0;

	/* If we lack an ID, this is a null-op. */
	if (fnp == NULL)
		return (0);

	/*
	 * If we have a force_id, we had an error after allocating
	 * the id, and putting it on the fq list, but before we
	 * finished setting up fnp.  So, if we have a force_id use it.
	 */
	if (force_id != DB_LOGFILEID_INVALID)
		id = force_id;
	else if (fnp->id == DB_LOGFILEID_INVALID) {
		if (fnp->old_id == DB_LOGFILEID_INVALID)
			return (0);
		id = fnp->old_id;
	} else
		id = fnp->id;
	if (!have_lock)
		MUTEX_LOCK(dbenv, lp->mtx_filelist);

	fnp->id = DB_LOGFILEID_INVALID;
	fnp->old_id = DB_LOGFILEID_INVALID;

	/* Remove the FNAME from the list of open files. */
	SH_TAILQ_REMOVE(&lp->fq, fnp, q, __fname);

	/*
	 * This FNAME may be for a DBP which is already closed.  Its ID may
	 * still be in use by an aborting transaction.  If not,
	 * remove this id from the dbentry table and push it onto the
	 * free list.
	 */
	if (!F_ISSET(fnp, DB_FNAME_CLOSED) &&
	    (ret = __dbreg_rem_dbentry(dblp, id)) == 0 && push)
		ret = __dbreg_push_id(dbenv, id);

	if (!have_lock)
		MUTEX_UNLOCK(dbenv, lp->mtx_filelist);
	return (ret);
}

/*
 * __dbreg_close_id --
 *	Take a dbreg id away from a dbp that we're closing, and log
 * the unregistry if the refcount goes to 0.
 *
 * PUBLIC: int __dbreg_close_id __P((DB *, DB_TXN *, u_int32_t));
 */
int
__dbreg_close_id(dbp, txn, op)
	DB *dbp;
	DB_TXN *txn;
	u_int32_t op;
{
	DB_ENV *dbenv;
	DB_LOG *dblp;
	FNAME *fnp;
	LOG *lp;
	int ret, t_ret;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	fnp = dbp->log_filename;

	/* If we lack an ID, this is a null-op. */
	if (fnp == NULL)
		return (0);

	if (fnp->id == DB_LOGFILEID_INVALID)
		return (__dbreg_revoke_id(dbp, 0, DB_LOGFILEID_INVALID));

	/*
	 * If we are the last reference to this db then we need to log it
	 * as closed.  Otherwise the last transaction will do the logging.
	 * Remove the DBP from the db entry table since it can nolonger
	 * be used.  If we abort it will have to be reopened.
	 */
	ret = 0;
	DB_ASSERT(dbenv, fnp->txn_ref > 0);
	if (fnp->txn_ref > 1) {
		MUTEX_LOCK(dbenv, dbp->mutex);
		if (fnp->txn_ref > 1) {
			fnp->txn_ref--;
			if (!F_ISSET(fnp, DB_FNAME_CLOSED) &&
			    (t_ret = __dbreg_rem_dbentry(
			    dbenv->lg_handle, fnp->id)) != 0 && ret == 0)
				ret = t_ret;

			/*
			 * The DB handle has been closed in the logging system.
			 * Transactions may still have a ref to this name.
			 * Mark it so that if recovery reopens the file id
			 * the transaction will not close the wrong handle.
			 */
			F_SET(fnp, DB_FNAME_CLOSED);
			MUTEX_UNLOCK(dbenv, dbp->mutex);
			/* The mutex now lives only in the FNAME. */
			dbp->mutex = MUTEX_INVALID;
			dbp->log_filename = NULL;
			goto no_log;
		}
	}
	MUTEX_LOCK(dbenv, lp->mtx_filelist);

	if ((ret = __dbreg_log_close(dbenv, fnp, txn, op)) != 0)
		goto err;
	ret = __dbreg_revoke_id(dbp, 1, DB_LOGFILEID_INVALID);

err:	MUTEX_UNLOCK(dbenv, lp->mtx_filelist);

	if ((t_ret = __dbreg_teardown(dbp)) != 0 && ret == 0)
		ret = t_ret;
no_log:
	return (ret);
}
/*
 * __dbreg_close_id_int --
 *	Close down a dbreg id and log the unregistry.  This is called only
 * when a transaction has the last ref to the fname.
 *
 * PUBLIC: int __dbreg_close_id_int __P((DB_ENV *, FNAME *, u_int32_t, int));
 */
int
__dbreg_close_id_int(dbenv, fnp, op, locked)
	DB_ENV *dbenv;
	FNAME *fnp;
	u_int32_t op;
	int locked;
{
	DB_LOG *dblp;
	LOG *lp;
	int ret, t_ret;

	DB_ASSERT(dbenv, fnp->txn_ref == 1);
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;

	if (fnp->id == DB_LOGFILEID_INVALID)
		return (__dbreg_revoke_id_int(dbenv,
		     fnp, locked, 1, DB_LOGFILEID_INVALID));

	if (F_ISSET(fnp, DB_FNAME_RECOVER))
		return (__dbreg_close_file(dbenv, fnp));
	/*
	 * If log_close fails then it will mark the name DB_FNAME_NOTLOGGED
	 * and the id must persist.
	 */
	if (!locked)
		MUTEX_LOCK(dbenv, lp->mtx_filelist);
	if ((ret = __dbreg_log_close(dbenv, fnp, NULL, op)) != 0)
		goto err;

	ret = __dbreg_revoke_id_int(dbenv, fnp, 1, 1, DB_LOGFILEID_INVALID);

err:	if (!locked)
		MUTEX_UNLOCK(dbenv, lp->mtx_filelist);

	if ((t_ret = __dbreg_teardown_int(dbenv, fnp)) != 0 && ret == 0)
		ret = t_ret;
	return (ret);
}
/*
 * __dbreg_log_close --
 *
 * Log a close of a database.  Called when closing a file or when a
 * replication client is becoming a master.  That closes all the
 * files it previously had open.
 *
 * Assumes caller holds the lp->mutex_filelist lock already.
 *
 * PUBLIC: int __dbreg_log_close __P((DB_ENV *, FNAME *,
 * PUBLIC:    DB_TXN *, u_int32_t));
 */
int
__dbreg_log_close(dbenv, fnp, txn, op)
	DB_ENV *dbenv;
	FNAME *fnp;
	DB_TXN *txn;
	u_int32_t op;
{
	DB_LOG *dblp;
	DBT fid_dbt, r_name, *dbtp;
	DB_LSN r_unused;
	int ret;

	dblp = dbenv->lg_handle;
	ret = 0;

	if (fnp->name_off == INVALID_ROFF)
		dbtp = NULL;
	else {
		memset(&r_name, 0, sizeof(r_name));
		r_name.data = R_ADDR(&dblp->reginfo, fnp->name_off);
		r_name.size =
		    (u_int32_t)strlen((char *)r_name.data) + 1;
		dbtp = &r_name;
	}
	memset(&fid_dbt, 0, sizeof(fid_dbt));
	fid_dbt.data = fnp->ufid;
	fid_dbt.size = DB_FILE_ID_LEN;
	if ((ret = __dbreg_register_log(dbenv, txn, &r_unused,
	    F_ISSET(fnp, DB_FNAME_DURABLE) ? 0 : DB_LOG_NOT_DURABLE,
	    op, dbtp, &fid_dbt, fnp->id,
	    fnp->s_type, fnp->meta_pgno, TXN_INVALID)) != 0) {
		/*
		 * We are trying to close, but the log write failed.
		 * Unfortunately, close needs to plow forward, because
		 * the application can't do anything with the handle.
		 * Make the entry in the shared memory region so that
		 * when we close the environment, we know that this
		 * happened.  Also, make sure we remove this from the
		 * per-process table, so that we don't try to close it
		 * later.
		 */
		F_SET(fnp, DB_FNAME_NOTLOGGED);
		(void)__dbreg_rem_dbentry(dblp, fnp->id);
	}
	return (ret);
}

/*
 * __dbreg_push_id and __dbreg_pop_id --
 *	Dbreg ids from closed files are kept on a stack in shared memory
 * for recycling.  (We want to reuse them as much as possible because each
 * process keeps open files in an array by ID.)  Push them to the stack and
 * pop them from it, managing memory as appropriate.
 *
 * The stack is protected by the mtx_filelist, and both functions assume it
 * is already locked.
 */
static int
__dbreg_push_id(dbenv, id)
	DB_ENV *dbenv;
	int32_t id;
{
	DB_LOG *dblp;
	LOG *lp;
	REGINFO *infop;
	int32_t *stack, *newstack;
	int ret;

	dblp = dbenv->lg_handle;
	infop = &dblp->reginfo;
	lp = infop->primary;

	if (id == lp->fid_max - 1) {
		lp->fid_max--;
		return (0);
	}

	/* Check if we have room on the stack. */
	if (lp->free_fid_stack == INVALID_ROFF ||
	    lp->free_fids_alloced <= lp->free_fids + 1) {
		LOG_SYSTEM_LOCK(dbenv);
		if ((ret = __env_alloc(infop,
		    (lp->free_fids_alloced + 20) * sizeof(u_int32_t),
		    &newstack)) != 0) {
			LOG_SYSTEM_UNLOCK(dbenv);
			return (ret);
		}

		if (lp->free_fid_stack != INVALID_ROFF) {
			stack = R_ADDR(infop, lp->free_fid_stack);
			memcpy(newstack, stack,
			    lp->free_fids_alloced * sizeof(u_int32_t));
			__env_alloc_free(infop, stack);
		}
		lp->free_fid_stack = R_OFFSET(infop, newstack);
		lp->free_fids_alloced += 20;
		LOG_SYSTEM_UNLOCK(dbenv);
	}

	stack = R_ADDR(infop, lp->free_fid_stack);
	stack[lp->free_fids++] = id;
	return (0);
}

static int
__dbreg_pop_id(dbenv, id)
	DB_ENV *dbenv;
	int32_t *id;
{
	DB_LOG *dblp;
	LOG *lp;
	int32_t *stack;

	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;

	/* Do we have anything to pop? */
	if (lp->free_fid_stack != INVALID_ROFF && lp->free_fids > 0) {
		stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack);
		*id = stack[--lp->free_fids];
	} else
		*id = DB_LOGFILEID_INVALID;

	return (0);
}

/*
 * __dbreg_pluck_id --
 *	Remove a particular dbreg id from the stack of free ids.  This is
 * used when we open a file, as in recovery, with a specific ID that might
 * be on the stack.
 *
 * Returns success whether or not the particular id was found, and like
 * push and pop, assumes that the mtx_filelist is locked.
 */
static int
__dbreg_pluck_id(dbenv, id)
	DB_ENV *dbenv;
	int32_t id;
{
	DB_LOG *dblp;
	LOG *lp;
	int32_t *stack;
	u_int i;

	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;

	if (id >= lp->fid_max)
		return (0);

	/* Do we have anything to look at? */
	if (lp->free_fid_stack != INVALID_ROFF) {
		stack = R_ADDR(&dblp->reginfo, lp->free_fid_stack);
		for (i = 0; i < lp->free_fids; i++)
			if (id == stack[i]) {
				/*
				 * Found it.  Overwrite it with the top
				 * id (which may harmlessly be itself),
				 * and shorten the stack by one.
				 */
				stack[i] = stack[lp->free_fids - 1];
				lp->free_fids--;
				return (0);
			}
	}

	return (0);
}

/*
 * __dbreg_log_id --
 *	Used for in-memory named files.  They are created in mpool and
 * are given id's early in the open process so that we can read and
 * create pages in the mpool for the files.  However, at the time that
 * the mpf is created, the file may not be fully created and/or its
 * meta-data may not be fully known, so we can't do a full dbregister.
 * This is a routine exported that will log a complete dbregister
 * record that will allow for both recovery and replication.
 *
 * PUBLIC: int __dbreg_log_id __P((DB *, DB_TXN *, int32_t, int));
 */
int
__dbreg_log_id(dbp, txn, id, needlock)
	DB *dbp;
	DB_TXN *txn;
	int32_t id;
	int needlock;
{
	DBT fid_dbt, r_name;
	DB_ENV *dbenv;
	DB_LOG *dblp;
	DB_LSN unused;
	FNAME *fnp;
	LOG *lp;
	u_int32_t op;
	int ret;

	dbenv = dbp->dbenv;
	dblp = dbenv->lg_handle;
	lp = dblp->reginfo.primary;
	fnp = dbp->log_filename;

	/* Verify that the fnp has been initialized. */
	if (fnp->s_type == DB_UNKNOWN) {
		memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN);
		fnp->s_type = dbp->type;
	}

	/*
	 * Log the registry.  We should only request a new ID in situations
	 * where logging is reasonable.
	 */
	memset(&fid_dbt, 0, sizeof(fid_dbt));
	memset(&r_name, 0, sizeof(r_name));

	if (needlock)
		MUTEX_LOCK(dbenv, lp->mtx_filelist);

	if (fnp->name_off != INVALID_ROFF) {
		r_name.data = R_ADDR(&dblp->reginfo, fnp->name_off);
		r_name.size = (u_int32_t)strlen((char *)r_name.data) + 1;
	}

	fid_dbt.data = dbp->fileid;
	fid_dbt.size = DB_FILE_ID_LEN;

	op = !F_ISSET(dbp, DB_AM_OPEN_CALLED) ? DBREG_PREOPEN :
	    (F_ISSET(dbp, DB_AM_INMEM) ? DBREG_REOPEN : DBREG_OPEN);
	ret = __dbreg_register_log(dbenv, txn, &unused,
	    F_ISSET(dbp, DB_AM_NOT_DURABLE) ? DB_LOG_NOT_DURABLE : 0,
	    op, r_name.size == 0 ? NULL : &r_name, &fid_dbt, id,
	    fnp->s_type, fnp->meta_pgno, fnp->create_txnid);

	if (needlock)
		MUTEX_UNLOCK(dbenv, lp->mtx_filelist);

	return (ret);
}