xref: /onnv-gate/usr/src/cmd/sendmail/db/log/log_register.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*0Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*0Sstevel@tonic-gate  */
7*0Sstevel@tonic-gate #include "config.h"
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #ifndef lint
10*0Sstevel@tonic-gate static const char sccsid[] = "@(#)log_register.c	10.22 (Sleepycat) 9/27/98";
11*0Sstevel@tonic-gate #endif /* not lint */
12*0Sstevel@tonic-gate 
13*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
14*0Sstevel@tonic-gate #include <sys/types.h>
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #include <errno.h>
17*0Sstevel@tonic-gate #include <string.h>
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #include "db_int.h"
21*0Sstevel@tonic-gate #include "shqueue.h"
22*0Sstevel@tonic-gate #include "log.h"
23*0Sstevel@tonic-gate #include "common_ext.h"
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate /*
26*0Sstevel@tonic-gate  * log_register --
27*0Sstevel@tonic-gate  *	Register a file name.
28*0Sstevel@tonic-gate  */
29*0Sstevel@tonic-gate int
log_register(dblp,dbp,name,type,idp)30*0Sstevel@tonic-gate log_register(dblp, dbp, name, type, idp)
31*0Sstevel@tonic-gate 	DB_LOG *dblp;
32*0Sstevel@tonic-gate 	DB *dbp;
33*0Sstevel@tonic-gate 	const char *name;
34*0Sstevel@tonic-gate 	DBTYPE type;
35*0Sstevel@tonic-gate 	u_int32_t *idp;
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	DBT fid_dbt, r_name;
38*0Sstevel@tonic-gate 	DB_LSN r_unused;
39*0Sstevel@tonic-gate 	FNAME *fnp, *reuse_fnp;
40*0Sstevel@tonic-gate 	size_t len;
41*0Sstevel@tonic-gate 	u_int32_t maxid;
42*0Sstevel@tonic-gate 	int inserted, ret;
43*0Sstevel@tonic-gate 	char *fullname;
44*0Sstevel@tonic-gate 	void *namep;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	inserted = 0;
47*0Sstevel@tonic-gate 	fullname = NULL;
48*0Sstevel@tonic-gate 	fnp = namep = reuse_fnp = NULL;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	LOG_PANIC_CHECK(dblp);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	/* Check the arguments. */
53*0Sstevel@tonic-gate 	if (type != DB_BTREE && type != DB_HASH && type != DB_RECNO) {
54*0Sstevel@tonic-gate 		__db_err(dblp->dbenv, "log_register: unknown DB file type");
55*0Sstevel@tonic-gate 		return (EINVAL);
56*0Sstevel@tonic-gate 	}
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 	/* Get the log file id. */
59*0Sstevel@tonic-gate 	if ((ret = __db_appname(dblp->dbenv,
60*0Sstevel@tonic-gate 	    DB_APP_DATA, NULL, name, 0, NULL, &fullname)) != 0)
61*0Sstevel@tonic-gate 		return (ret);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 	LOCK_LOGREGION(dblp);
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	/*
66*0Sstevel@tonic-gate 	 * See if we've already got this file in the log, finding the
67*0Sstevel@tonic-gate 	 * (maximum+1) in-use file id and some available file id (if we
68*0Sstevel@tonic-gate 	 * find an available fid, we'll use it, else we'll have to allocate
69*0Sstevel@tonic-gate 	 * one after the maximum that we found).
70*0Sstevel@tonic-gate 	 */
71*0Sstevel@tonic-gate 	for (maxid = 0, fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
72*0Sstevel@tonic-gate 	    fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
73*0Sstevel@tonic-gate 		if (fnp->ref == 0) {		/* Entry is not in use. */
74*0Sstevel@tonic-gate 			if (reuse_fnp == NULL)
75*0Sstevel@tonic-gate 				reuse_fnp = fnp;
76*0Sstevel@tonic-gate 			continue;
77*0Sstevel@tonic-gate 		}
78*0Sstevel@tonic-gate 		if (!memcmp(dbp->fileid, fnp->ufid, DB_FILE_ID_LEN)) {
79*0Sstevel@tonic-gate 			++fnp->ref;
80*0Sstevel@tonic-gate 			goto found;
81*0Sstevel@tonic-gate 		}
82*0Sstevel@tonic-gate 		if (maxid <= fnp->id)
83*0Sstevel@tonic-gate 			maxid = fnp->id + 1;
84*0Sstevel@tonic-gate 	}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/* Fill in fnp structure. */
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	if (reuse_fnp != NULL)		/* Reuse existing one. */
89*0Sstevel@tonic-gate 		fnp = reuse_fnp;
90*0Sstevel@tonic-gate 	else if ((ret = __db_shalloc(dblp->addr, sizeof(FNAME), 0, &fnp)) != 0)
91*0Sstevel@tonic-gate 		goto err;
92*0Sstevel@tonic-gate 	else				/* Allocate a new one. */
93*0Sstevel@tonic-gate 		fnp->id = maxid;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	fnp->ref = 1;
96*0Sstevel@tonic-gate 	fnp->s_type = type;
97*0Sstevel@tonic-gate 	memcpy(fnp->ufid, dbp->fileid, DB_FILE_ID_LEN);
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	len = strlen(name) + 1;
100*0Sstevel@tonic-gate 	if ((ret = __db_shalloc(dblp->addr, len, 0, &namep)) != 0)
101*0Sstevel@tonic-gate 		goto err;
102*0Sstevel@tonic-gate 	fnp->name_off = R_OFFSET(dblp, namep);
103*0Sstevel@tonic-gate 	memcpy(namep, name, len);
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/* Only do the insert if we allocated a new fnp. */
106*0Sstevel@tonic-gate 	if (reuse_fnp == NULL)
107*0Sstevel@tonic-gate 		SH_TAILQ_INSERT_HEAD(&dblp->lp->fq, fnp, q, __fname);
108*0Sstevel@tonic-gate 	inserted = 1;
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate found:	/* Log the registry. */
111*0Sstevel@tonic-gate 	if (!F_ISSET(dblp, DBC_RECOVER)) {
112*0Sstevel@tonic-gate 		r_name.data = (void *)name;		/* XXX: Yuck! */
113*0Sstevel@tonic-gate 		r_name.size = strlen(name) + 1;
114*0Sstevel@tonic-gate 		memset(&fid_dbt, 0, sizeof(fid_dbt));
115*0Sstevel@tonic-gate 		fid_dbt.data = dbp->fileid;
116*0Sstevel@tonic-gate 		fid_dbt.size = DB_FILE_ID_LEN;
117*0Sstevel@tonic-gate 		if ((ret = __log_register_log(dblp, NULL, &r_unused,
118*0Sstevel@tonic-gate 		    0, LOG_OPEN, &r_name, &fid_dbt, fnp->id, type)) != 0)
119*0Sstevel@tonic-gate 			goto err;
120*0Sstevel@tonic-gate 		if ((ret = __log_add_logid(dblp, dbp, name, fnp->id)) != 0)
121*0Sstevel@tonic-gate 			goto err;
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if (0) {
125*0Sstevel@tonic-gate err:		/*
126*0Sstevel@tonic-gate 		 * XXX
127*0Sstevel@tonic-gate 		 * We should grow the region.
128*0Sstevel@tonic-gate 		 */
129*0Sstevel@tonic-gate 		if (inserted)
130*0Sstevel@tonic-gate 			SH_TAILQ_REMOVE(&dblp->lp->fq, fnp, q, __fname);
131*0Sstevel@tonic-gate 		if (namep != NULL)
132*0Sstevel@tonic-gate 			__db_shalloc_free(dblp->addr, namep);
133*0Sstevel@tonic-gate 		if (fnp != NULL)
134*0Sstevel@tonic-gate 			__db_shalloc_free(dblp->addr, fnp);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	if (idp != NULL)
138*0Sstevel@tonic-gate 		*idp = fnp->id;
139*0Sstevel@tonic-gate 	UNLOCK_LOGREGION(dblp);
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (fullname != NULL)
142*0Sstevel@tonic-gate 		__os_freestr(fullname);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	return (ret);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * log_unregister --
149*0Sstevel@tonic-gate  *	Discard a registered file name.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate int
log_unregister(dblp,fid)152*0Sstevel@tonic-gate log_unregister(dblp, fid)
153*0Sstevel@tonic-gate 	DB_LOG *dblp;
154*0Sstevel@tonic-gate 	u_int32_t fid;
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	DBT fid_dbt, r_name;
157*0Sstevel@tonic-gate 	DB_LSN r_unused;
158*0Sstevel@tonic-gate 	FNAME *fnp;
159*0Sstevel@tonic-gate 	int ret;
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	LOG_PANIC_CHECK(dblp);
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	ret = 0;
164*0Sstevel@tonic-gate 	LOCK_LOGREGION(dblp);
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	/* Find the entry in the log. */
167*0Sstevel@tonic-gate 	for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
168*0Sstevel@tonic-gate 	    fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname))
169*0Sstevel@tonic-gate 		if (fid == fnp->id)
170*0Sstevel@tonic-gate 			break;
171*0Sstevel@tonic-gate 	if (fnp == NULL) {
172*0Sstevel@tonic-gate 		__db_err(dblp->dbenv, "log_unregister: non-existent file id");
173*0Sstevel@tonic-gate 		ret = EINVAL;
174*0Sstevel@tonic-gate 		goto ret1;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/* Unlog the registry. */
178*0Sstevel@tonic-gate 	if (!F_ISSET(dblp, DBC_RECOVER)) {
179*0Sstevel@tonic-gate 		memset(&r_name, 0, sizeof(r_name));
180*0Sstevel@tonic-gate 		r_name.data = R_ADDR(dblp, fnp->name_off);
181*0Sstevel@tonic-gate 		r_name.size = strlen(r_name.data) + 1;
182*0Sstevel@tonic-gate 		memset(&fid_dbt, 0, sizeof(fid_dbt));
183*0Sstevel@tonic-gate 		fid_dbt.data = fnp->ufid;
184*0Sstevel@tonic-gate 		fid_dbt.size = DB_FILE_ID_LEN;
185*0Sstevel@tonic-gate 		if ((ret = __log_register_log(dblp, NULL, &r_unused,
186*0Sstevel@tonic-gate 		    0, LOG_CLOSE, &r_name, &fid_dbt, fid, fnp->s_type)) != 0)
187*0Sstevel@tonic-gate 			goto ret1;
188*0Sstevel@tonic-gate 	}
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	/*
191*0Sstevel@tonic-gate 	 * If more than 1 reference, just decrement the reference and return.
192*0Sstevel@tonic-gate 	 * Otherwise, free the name.
193*0Sstevel@tonic-gate 	 */
194*0Sstevel@tonic-gate 	--fnp->ref;
195*0Sstevel@tonic-gate 	if (fnp->ref == 0)
196*0Sstevel@tonic-gate 		__db_shalloc_free(dblp->addr, R_ADDR(dblp, fnp->name_off));
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	/*
199*0Sstevel@tonic-gate 	 * Remove from the process local table.  If this operation is taking
200*0Sstevel@tonic-gate 	 * place during recovery, then the logid was never added to the table,
201*0Sstevel@tonic-gate 	 * so do not remove it.
202*0Sstevel@tonic-gate 	 */
203*0Sstevel@tonic-gate 	if (!F_ISSET(dblp, DBC_RECOVER))
204*0Sstevel@tonic-gate 		__log_rem_logid(dblp, fid);
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate ret1:	UNLOCK_LOGREGION(dblp);
207*0Sstevel@tonic-gate 	return (ret);
208*0Sstevel@tonic-gate }
209