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