1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1996, 1997, 1998, 1999, 2000
5  *	Sleepycat Software.  All rights reserved.
6  */
7 /*
8  * Copyright (c) 1995, 1996
9  *	The President and Fellows of Harvard University.  All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * Margo Seltzer.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #include "config.h"
40 
41 #ifndef lint
42 static const char revid[] = "$Id: db_dispatch.c,v 1.4 2014/04/17 20:27:26 sebdiaz Exp $";
43 #endif /* not lint */
44 
45 #ifndef NO_SYSTEM_INCLUDES
46 #include <sys/types.h>
47 
48 #include <errno.h>
49 #include <stddef.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #endif
53 
54 #include "db_int.h"
55 #include "db_page.h"
56 #include "db_dispatch.h"
57 #include "db_am.h"
58 #include "log_auto.h"
59 #include "txn.h"
60 #include "txn_auto.h"
61 #include "log.h"
62 
63 /*
64  * CDB___db_dispatch --
65  *
66  * This is the transaction dispatch function used by the db access methods.
67  * It is designed to handle the record format used by all the access
68  * methods (the one automatically generated by the db_{h,log,read}.sh
69  * scripts in the tools directory).  An application using a different
70  * recovery paradigm will supply a different dispatch function to txn_open.
71  *
72  * PUBLIC: int CDB___db_dispatch __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
73  */
74 int
CDB___db_dispatch(dbenv,db,lsnp,redo,info)75 CDB___db_dispatch(dbenv, db, lsnp, redo, info)
76 	DB_ENV *dbenv;		/* The environment. */
77 	DBT *db;		/* The log record upon which to dispatch. */
78 	DB_LSN *lsnp;		/* The lsn of the record being dispatched. */
79 	db_recops redo;		/* Redo this op (or undo it). */
80 	void *info;
81 {
82 	u_int32_t rectype, txnid;
83 
84 	memcpy(&rectype, db->data, sizeof(rectype));
85 	memcpy(&txnid, (u_int8_t *)db->data + sizeof(rectype), sizeof(txnid));
86 
87 	switch (redo) {
88 	case DB_TXN_ABORT:
89 		return ((dbenv->dtab[rectype])(dbenv, db, lsnp, redo, info));
90 	case DB_TXN_OPENFILES:
91 		if (rectype == DB_log_register)
92 			return (dbenv->dtab[rectype](dbenv,
93 			    db, lsnp, redo, info));
94 		break;
95 	case DB_TXN_BACKWARD_ROLL:
96 		/*
97 		 * Running full recovery in the backward pass.  If we've
98 		 * seen this txnid before and added to it our commit list,
99 		 * then we do nothing during this pass.  If we've never
100 		 * seen it, then we call the appropriate recovery routine
101 		 * in "abort mode".
102 		 *
103 		 * We need to always undo DB_db_noop records, so that we
104 		 * properly handle any aborts before the file was closed.
105 		 */
106 		if (rectype == DB_log_register || rectype == DB_txn_ckp ||
107 		    rectype == DB_db_noop ||
108 		    (CDB___db_txnlist_find(info, txnid) == DB_NOTFOUND &&
109 		    txnid != 0))
110 			return (dbenv->dtab[rectype](dbenv,
111 			    db, lsnp, DB_TXN_BACKWARD_ROLL, info));
112 		break;
113 	case DB_TXN_FORWARD_ROLL:
114 		/*
115 		 * In the forward pass, if we haven't seen the transaction,
116 		 * do nothing, else recovery it.
117 		 *
118 		 * We need to always redo DB_db_noop records, so that we
119 		 * properly handle any commits after the file was closed.
120 		 */
121 		if (rectype == DB_log_register || rectype == DB_txn_ckp ||
122 		    rectype == DB_db_noop ||
123 		    CDB___db_txnlist_find(info, txnid) != DB_NOTFOUND)
124 			return (dbenv->dtab[rectype](dbenv,
125 			    db, lsnp, DB_TXN_FORWARD_ROLL, info));
126 		break;
127 	default:
128 		return (CDB___db_unknown_flag(dbenv, "CDB___db_dispatch", redo));
129 	}
130 	return (0);
131 }
132 
133 /*
134  * CDB___db_add_recovery --
135  *
136  * PUBLIC: int CDB___db_add_recovery __P((DB_ENV *,
137  * PUBLIC:    int (*)(DB_ENV *, DBT *, DB_LSN *, db_recops, void *), u_int32_t));
138  */
139 int
CDB___db_add_recovery(dbenv,func,ndx)140 CDB___db_add_recovery(dbenv, func, ndx)
141 	DB_ENV *dbenv;
142 	int (*func) __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
143 	u_int32_t ndx;
144 {
145 	u_int32_t i, nsize;
146 	int ret;
147 
148 	/* Check if we have to grow the table. */
149 	if (ndx >= dbenv->dtab_size) {
150 		nsize = ndx + 40;
151 		if ((ret = CDB___os_realloc(dbenv,
152 		    nsize * sizeof(dbenv->dtab[0]), NULL, &dbenv->dtab)) != 0)
153 			return (ret);
154 		for (i = dbenv->dtab_size; i < nsize; ++i)
155 			dbenv->dtab[i] = NULL;
156 		dbenv->dtab_size = nsize;
157 	}
158 
159 	dbenv->dtab[ndx] = func;
160 	return (0);
161 }
162 
163 /*
164  * CDB___deprecated_recover --
165  *	Stub routine for deprecated recovery functions.
166  *
167  * PUBLIC: int CDB___deprecated_recover
168  * PUBLIC:     __P((DB_ENV *, DBT *, DB_LSN *, db_recops, void *));
169  */
170 int
CDB___deprecated_recover(dbenv,dbtp,lsnp,op,info)171 CDB___deprecated_recover(dbenv, dbtp, lsnp, op, info)
172 	DB_ENV *dbenv;
173 	DBT *dbtp;
174 	DB_LSN *lsnp;
175 	db_recops op;
176 	void *info;
177 {
178 	if (dbenv||dbtp||lsnp||op||info){}
179 	COMPQUIET(dbenv, NULL);
180 	COMPQUIET(dbtp, NULL);
181 	COMPQUIET(lsnp, NULL);
182 	COMPQUIET(op, 0);
183 	COMPQUIET(info, NULL);
184 	return (EINVAL);
185 }
186 
187 /*
188  * CDB___db_txnlist_init --
189  *	Initialize transaction linked list.
190  *
191  * PUBLIC: int CDB___db_txnlist_init __P((DB_ENV *, void *));
192  */
193 int
CDB___db_txnlist_init(dbenv,retp)194 CDB___db_txnlist_init(dbenv, retp)
195 	DB_ENV *dbenv;
196 	void *retp;
197 {
198 	DB_TXNHEAD *headp;
199 	int ret;
200 
201 	if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNHEAD), NULL, &headp)) != 0)
202 		return (ret);
203 
204 	LIST_INIT(&headp->head);
205 	headp->maxid = 0;
206 	headp->generation = 1;
207 
208 	*(void **)retp = headp;
209 	return (0);
210 }
211 
212 /*
213  * CDB___db_txnlist_add --
214  *	Add an element to our transaction linked list.
215  *
216  * PUBLIC: int CDB___db_txnlist_add __P((DB_ENV *, void *, u_int32_t));
217  */
218 int
CDB___db_txnlist_add(dbenv,listp,txnid)219 CDB___db_txnlist_add(dbenv, listp, txnid)
220 	DB_ENV *dbenv;
221 	void *listp;
222 	u_int32_t txnid;
223 {
224 	DB_TXNHEAD *hp;
225 	DB_TXNLIST *elp;
226 	int ret;
227 
228 	if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &elp)) != 0)
229 		return (ret);
230 
231 	hp = (DB_TXNHEAD *)listp;
232 	LIST_INSERT_HEAD(&hp->head, elp, links);
233 
234 	elp->type = TXNLIST_TXNID;
235 	elp->u.t.txnid = txnid;
236 	if (txnid > hp->maxid)
237 		hp->maxid = txnid;
238 	elp->u.t.generation = hp->generation;
239 
240 	return (0);
241 }
242 
243 /* CDB___db_txnlist_close --
244  *
245  *	Call this when we close a file.  It allows us to reconcile whether
246  * we have done any operations on this file with whether the file appears
247  * to have been deleted.  If you never do any operations on a file, then
248  * we assume it's OK to appear deleted.
249  *
250  * PUBLIC: int CDB___db_txnlist_close __P((void *, int32_t, u_int32_t));
251  */
252 
253 int
CDB___db_txnlist_close(listp,lid,count)254 CDB___db_txnlist_close(listp, lid, count)
255 	void *listp;
256 	int32_t lid;
257 	u_int32_t count;
258 {
259 	DB_TXNHEAD *hp;
260 	DB_TXNLIST *p;
261 
262 	hp = (DB_TXNHEAD *)listp;
263 	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
264 		if (p->type == TXNLIST_DELETE)
265 			if (lid == p->u.d.fileid &&
266 			    !F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED)) {
267 				p->u.d.count += count;
268 				return (0);
269 			}
270 	}
271 
272 	return (0);
273 }
274 
275 /*
276  * CDB___db_txnlist_delete --
277  *
278  *	Record that a file was missing or deleted.  If the deleted
279  * flag is set, then we've encountered a delete of a file, else we've
280  * just encountered a file that is missing.  The lid is the log fileid
281  * and is only meaningful if deleted is not equal to 0.
282  *
283  * PUBLIC: int CDB___db_txnlist_delete __P((DB_ENV *,
284  * PUBLIC:     void *, char *, u_int32_t, int));
285  */
286 int
CDB___db_txnlist_delete(dbenv,listp,name,lid,deleted)287 CDB___db_txnlist_delete(dbenv, listp, name, lid, deleted)
288 	DB_ENV *dbenv;
289 	void *listp;
290 	char *name;
291 	u_int32_t lid;
292 	int deleted;
293 {
294 	DB_TXNHEAD *hp;
295 	DB_TXNLIST *p;
296 	int ret;
297 
298 	hp = (DB_TXNHEAD *)listp;
299 	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
300 		if (p->type == TXNLIST_DELETE)
301 			if (strcmp(name, p->u.d.fname) == 0) {
302 				if (deleted)
303 					F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
304 				else
305 					F_CLR(&p->u.d, TXNLIST_FLAG_CLOSED);
306 				return (0);
307 			}
308 	}
309 
310 	/* Need to add it. */
311 	if ((ret = CDB___os_malloc(dbenv, sizeof(DB_TXNLIST), NULL, &p)) != 0)
312 		return (ret);
313 	LIST_INSERT_HEAD(&hp->head, p, links);
314 
315 	p->type = TXNLIST_DELETE;
316 	p->u.d.flags = 0;
317 	if (deleted)
318 		F_SET(&p->u.d, TXNLIST_FLAG_DELETED);
319 	p->u.d.fileid = lid;
320 	p->u.d.count = 0;
321 	ret = CDB___os_strdup(dbenv, name, &p->u.d.fname);
322 
323 	return (ret);
324 }
325 
326 /*
327  * CDB___db_txnlist_end --
328  *	Discard transaction linked list. Print out any error messages
329  * for deleted files.
330  *
331  * PUBLIC: void CDB___db_txnlist_end __P((DB_ENV *, void *));
332  */
333 void
CDB___db_txnlist_end(dbenv,listp)334 CDB___db_txnlist_end(dbenv, listp)
335 	DB_ENV *dbenv;
336 	void *listp;
337 {
338 	DB_TXNHEAD *hp;
339 	DB_TXNLIST *p;
340 	DB_LOG *lp;
341 
342 	hp = (DB_TXNHEAD *)listp;
343 	lp = (DB_LOG *)dbenv->lg_handle;
344 	while (hp != NULL &&
345 	    (p = LIST_FIRST(&hp->head)) != LIST_END(&hp->head)) {
346 		LIST_REMOVE(p, links);
347 		if (p->type == TXNLIST_DELETE) {
348 			/*
349 			 * If we have a file that is not deleted and has
350 			 * some operations, we flag the warning.  Since
351 			 * the file could still be open, we need to check
352 			 * the actual log table as well.
353 			 */
354 			if ((!F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) &&
355 			    p->u.d.count != 0) ||
356 			    (!F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) &&
357 			    p->u.d.fileid != (int32_t) TXNLIST_INVALID_ID &&
358 			    p->u.d.fileid < lp->dbentry_cnt &&
359 			    lp->dbentry[p->u.d.fileid].count != 0))
360 				CDB___db_err(dbenv, "warning: %s: %s",
361 				    p->u.d.fname, CDB_db_strerror(ENOENT));
362 			CDB___os_freestr(p->u.d.fname);
363 		}
364 		CDB___os_free(p, sizeof(DB_TXNLIST));
365 	}
366 	CDB___os_free(listp, sizeof(DB_TXNHEAD));
367 }
368 
369 /*
370  * CDB___db_txnlist_find --
371  *	Checks to see if a txnid with the current generation is in the
372  *	txnid list.
373  *
374  * PUBLIC: int CDB___db_txnlist_find __P((void *, u_int32_t));
375  */
376 int
CDB___db_txnlist_find(listp,txnid)377 CDB___db_txnlist_find(listp, txnid)
378 	void *listp;
379 	u_int32_t txnid;
380 {
381 	DB_TXNHEAD *hp;
382 	DB_TXNLIST *p;
383 
384 	if (txnid == 0 || (hp = (DB_TXNHEAD *)listp) == NULL)
385 		return (DB_NOTFOUND);
386 
387 	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
388 		if (p->type != TXNLIST_TXNID)
389 			continue;
390 		if (p->u.t.txnid == txnid &&
391 		    hp->generation == p->u.t.generation) {
392 			/* Move it to head of list. */
393 			if (p != LIST_FIRST(&hp->head)) {
394 				LIST_REMOVE(p, links);
395 				LIST_INSERT_HEAD(&hp->head, p, links);
396 			}
397 			return (0);
398 		}
399 	}
400 
401 	return (DB_NOTFOUND);
402 }
403 
404 /*
405  * CDB___db_txnlist_gen --
406  *	Change the current generation number.
407  *
408  * PUBLIC: void CDB___db_txnlist_gen __P((void *, int));
409  */
410 void
CDB___db_txnlist_gen(listp,incr)411 CDB___db_txnlist_gen(listp, incr)
412 	void *listp;
413 	int incr;
414 {
415 	DB_TXNHEAD *hp;
416 
417 	/*
418 	 * During recovery generation numbers keep track of how many "restart"
419 	 * checkpoints we've seen.  Restart checkpoints occur whenever we take
420 	 * a checkpoint and there are no outstanding transactions.  When that
421 	 * happens, we can reset transaction IDs back to 1.  It always happens
422 	 * at recovery and it prevents us from exhausting the transaction IDs
423 	 * name space.
424 	 */
425 	hp = (DB_TXNHEAD *)listp;
426 	hp->generation += incr;
427 }
428 
429 #ifdef DEBUG
430 /*
431  * CDB___db_txnlist_print --
432  *	Print out the transaction list.
433  *
434  * PUBLIC: void CDB___db_txnlist_print __P((void *));
435  */
436 void
CDB___db_txnlist_print(listp)437 CDB___db_txnlist_print(listp)
438 	void *listp;
439 {
440 	DB_TXNHEAD *hp;
441 	DB_TXNLIST *p;
442 
443 	hp = (DB_TXNHEAD *)listp;
444 
445 	printf("Maxid: %lu Generation: %lu\n",
446 	    (u_long)hp->maxid, (u_long)hp->generation);
447 	for (p = LIST_FIRST(&hp->head); p != NULL; p = LIST_NEXT(p, links)) {
448 		switch (p->type) {
449 		case TXNLIST_TXNID:
450 			printf("TXNID: %lu(%lu)\n",
451 			    (u_long)p->u.t.txnid, (u_long)p->u.t.generation);
452 			break;
453 		case TXNLIST_DELETE:
454 			printf("FILE: %s id=%d ops=%d %s %s\n",
455 			    p->u.d.fname, p->u.d.fileid, p->u.d.count,
456 			    F_ISSET(&p->u.d, TXNLIST_FLAG_DELETED) ?
457 			    "(deleted)" : "(missing)",
458 			    F_ISSET(&p->u.d, TXNLIST_FLAG_CLOSED) ?
459 			    "(closed)" : "(open)");
460 
461 			break;
462 		default:
463 			printf("Unrecognized type: %d\n", p->type);
464 			break;
465 		}
466 	}
467 }
468 #endif
469