00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "config.h"
00044
00045 #ifndef lint
00046 static const char revid[] = "$Id: db__overflow_8c-source.html,v 1.1 2008/06/08 10:17:55 sebdiaz Exp $";
00047 #endif
00048
00049 #ifndef NO_SYSTEM_INCLUDES
00050 #include <sys/types.h>
00051
00052 #include <errno.h>
00053 #include <string.h>
00054 #endif
00055
00056 #include "db_int.h"
00057 #include "db_page.h"
00058 #include "db_am.h"
00059 #include "db_verify.h"
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 int
00078 CDB___db_goff(dbp, dbt, tlen, pgno, bpp, bpsz)
00079 DB *dbp;
00080 DBT *dbt;
00081 u_int32_t tlen;
00082 db_pgno_t pgno;
00083 void **bpp;
00084 u_int32_t *bpsz;
00085 {
00086 DB_ENV *dbenv;
00087 PAGE *h;
00088 db_indx_t bytes;
00089 u_int32_t curoff, needed, start;
00090 u_int8_t *p, *src;
00091 int ret;
00092
00093 dbenv = dbp->dbenv;
00094
00095
00096
00097
00098
00099
00100
00101 if (F_ISSET(dbt, DB_DBT_PARTIAL)) {
00102 start = dbt->doff;
00103 needed = dbt->dlen;
00104 } else {
00105 start = 0;
00106 needed = tlen;
00107 }
00108
00109
00110 if (F_ISSET(dbt, DB_DBT_USERMEM)) {
00111 if (needed > dbt->ulen) {
00112 dbt->size = needed;
00113 return (ENOMEM);
00114 }
00115 } else if (F_ISSET(dbt, DB_DBT_MALLOC)) {
00116 if ((ret = CDB___os_malloc(dbenv,
00117 needed, dbp->db_malloc, &dbt->data)) != 0)
00118 return (ret);
00119 } else if (F_ISSET(dbt, DB_DBT_REALLOC)) {
00120 if ((ret = CDB___os_realloc(dbenv,
00121 needed, dbp->db_realloc, &dbt->data)) != 0)
00122 return (ret);
00123 } else if (*bpsz == 0 || *bpsz < needed) {
00124 if ((ret = CDB___os_realloc(dbenv, needed, NULL, bpp)) != 0)
00125 return (ret);
00126 *bpsz = needed;
00127 dbt->data = *bpp;
00128 } else
00129 dbt->data = *bpp;
00130
00131
00132
00133
00134
00135 dbt->size = needed;
00136 for (curoff = 0, p = dbt->data; pgno != PGNO_INVALID && needed > 0;) {
00137 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
00138 (void)CDB___db_pgerr(dbp, pgno);
00139 return (ret);
00140 }
00141
00142 if (curoff + OV_LEN(h) >= start) {
00143 src = (u_int8_t *)h + P_OVERHEAD;
00144 bytes = OV_LEN(h);
00145 if (start > curoff) {
00146 src += start - curoff;
00147 bytes -= start - curoff;
00148 }
00149 if (bytes > needed)
00150 bytes = needed;
00151 memcpy(p, src, bytes);
00152 p += bytes;
00153 needed -= bytes;
00154 }
00155 curoff += OV_LEN(h);
00156 pgno = h->next_pgno;
00157 CDB_memp_fput(dbp->mpf, h, 0);
00158 }
00159 return (0);
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 int
00169 CDB___db_poff(dbc, dbt, pgnop)
00170 DBC *dbc;
00171 const DBT *dbt;
00172 db_pgno_t *pgnop;
00173 {
00174 DB *dbp;
00175 PAGE *pagep, *lastp;
00176 DB_LSN new_lsn, null_lsn;
00177 DBT tmp_dbt;
00178 db_indx_t pagespace;
00179 u_int32_t sz;
00180 u_int8_t *p;
00181 int ret;
00182
00183
00184
00185
00186
00187
00188 dbp = dbc->dbp;
00189 pagespace = P_MAXSPACE(dbp->pgsize);
00190
00191 lastp = NULL;
00192 for (p = dbt->data,
00193 sz = dbt->size; sz > 0; p += pagespace, sz -= pagespace) {
00194
00195
00196
00197
00198 if (sz < pagespace)
00199 pagespace = sz;
00200
00201
00202
00203
00204
00205
00206 if ((ret = CDB___db_new(dbc, (P_OVERFLOW | dbp->tags), &pagep)) != 0)
00207 return (ret);
00208 if (DB_LOGGING(dbc)) {
00209 tmp_dbt.data = p;
00210 tmp_dbt.size = pagespace;
00211 ZERO_LSN(null_lsn);
00212 if ((ret = CDB___db_big_log(dbp->dbenv, dbc->txn,
00213 &new_lsn, 0, DB_ADD_BIG, dbp->log_fileid,
00214 PGNO(pagep), lastp ? PGNO(lastp) : PGNO_INVALID,
00215 PGNO_INVALID, &tmp_dbt, &LSN(pagep),
00216 lastp == NULL ? &null_lsn : &LSN(lastp),
00217 &null_lsn)) != 0)
00218 return (ret);
00219
00220
00221 if (lastp)
00222 LSN(lastp) = new_lsn;
00223 LSN(pagep) = new_lsn;
00224 }
00225
00226 P_INIT(pagep, dbp->pgsize,
00227 PGNO(pagep), PGNO_INVALID, PGNO_INVALID, 0, P_OVERFLOW, dbp->tags);
00228 OV_LEN(pagep) = pagespace;
00229 OV_REF(pagep) = 1;
00230 memcpy((u_int8_t *)pagep + P_OVERHEAD, p, pagespace);
00231
00232
00233
00234
00235
00236
00237 if (lastp == NULL)
00238 *pgnop = PGNO(pagep);
00239 else {
00240 lastp->next_pgno = PGNO(pagep);
00241 pagep->prev_pgno = PGNO(lastp);
00242 (void)CDB_memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
00243 }
00244 lastp = pagep;
00245 }
00246 (void)CDB_memp_fput(dbp->mpf, lastp, DB_MPOOL_DIRTY);
00247 return (0);
00248 }
00249
00250
00251
00252
00253
00254
00255
00256 int
00257 CDB___db_ovref(dbc, pgno, adjust)
00258 DBC *dbc;
00259 db_pgno_t pgno;
00260 int32_t adjust;
00261 {
00262 DB *dbp;
00263 PAGE *h;
00264 int ret;
00265
00266 dbp = dbc->dbp;
00267 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0) {
00268 (void)CDB___db_pgerr(dbp, pgno);
00269 return (ret);
00270 }
00271
00272 if (DB_LOGGING(dbc))
00273 if ((ret = CDB___db_ovref_log(dbp->dbenv, dbc->txn,
00274 &LSN(h), 0, dbp->log_fileid, h->pgno, adjust,
00275 &LSN(h))) != 0)
00276 return (ret);
00277 OV_REF(h) += adjust;
00278
00279 (void)CDB_memp_fput(dbp->mpf, h, DB_MPOOL_DIRTY);
00280 return (0);
00281 }
00282
00283
00284
00285
00286
00287
00288
00289 int
00290 CDB___db_doff(dbc, pgno)
00291 DBC *dbc;
00292 db_pgno_t pgno;
00293 {
00294 DB *dbp;
00295 PAGE *pagep;
00296 DB_LSN null_lsn;
00297 DBT tmp_dbt;
00298 int ret;
00299
00300 dbp = dbc->dbp;
00301 do {
00302 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0) {
00303 (void)CDB___db_pgerr(dbp, pgno);
00304 return (ret);
00305 }
00306
00307
00308
00309
00310
00311 if (TYPE(pagep) == P_OVERFLOW && OV_REF(pagep) > 1) {
00312 (void)CDB_memp_fput(dbp->mpf, pagep, 0);
00313 return (CDB___db_ovref(dbc, pgno, -1));
00314 }
00315
00316 if (DB_LOGGING(dbc)) {
00317 tmp_dbt.data = (u_int8_t *)pagep + P_OVERHEAD;
00318 tmp_dbt.size = OV_LEN(pagep);
00319 ZERO_LSN(null_lsn);
00320 if ((ret = CDB___db_big_log(dbp->dbenv, dbc->txn,
00321 &LSN(pagep), 0, DB_REM_BIG, dbp->log_fileid,
00322 PGNO(pagep), PREV_PGNO(pagep), NEXT_PGNO(pagep),
00323 &tmp_dbt, &LSN(pagep), &null_lsn, &null_lsn)) != 0)
00324 return (ret);
00325 }
00326 pgno = pagep->next_pgno;
00327 if ((ret = CDB___db_free(dbc, pagep)) != 0)
00328 return (ret);
00329 } while (pgno != PGNO_INVALID);
00330
00331 return (0);
00332 }
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 int
00348 CDB___db_moff(dbp, dbt, pgno, tlen, cmpfunc, cmpp)
00349 DB *dbp;
00350 const DBT *dbt;
00351 db_pgno_t pgno;
00352 u_int32_t tlen;
00353 int (*cmpfunc) __P((const DBT *, const DBT *));
00354 int *cmpp;
00355 {
00356 PAGE *pagep;
00357 DBT local_dbt;
00358 void *buf;
00359 u_int32_t bufsize, cmp_bytes, key_left;
00360 u_int8_t *p1, *p2;
00361 int ret;
00362
00363
00364
00365
00366
00367 if (cmpfunc != NULL) {
00368 memset(&local_dbt, 0, sizeof(local_dbt));
00369 buf = NULL;
00370 bufsize = 0;
00371
00372 if ((ret = CDB___db_goff(dbp,
00373 &local_dbt, tlen, pgno, &buf, &bufsize)) != 0)
00374 return (ret);
00375
00376 *cmpp = cmpfunc(dbt, &local_dbt);
00377 CDB___os_free(buf, bufsize);
00378 return (0);
00379 }
00380
00381
00382 for (*cmpp = 0, p1 = dbt->data,
00383 key_left = dbt->size; key_left > 0 && pgno != PGNO_INVALID;) {
00384 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &pagep)) != 0)
00385 return (ret);
00386
00387 cmp_bytes = OV_LEN(pagep) < key_left ? OV_LEN(pagep) : key_left;
00388 tlen -= cmp_bytes;
00389 key_left -= cmp_bytes;
00390 for (p2 =
00391 (u_int8_t *)pagep + P_OVERHEAD; cmp_bytes-- > 0; ++p1, ++p2)
00392 if (*p1 != *p2) {
00393 *cmpp = (long)*p1 - (long)*p2;
00394 break;
00395 }
00396 pgno = NEXT_PGNO(pagep);
00397 if ((ret = CDB_memp_fput(dbp->mpf, pagep, 0)) != 0)
00398 return (ret);
00399 if (*cmpp != 0)
00400 return (0);
00401 }
00402 if (key_left > 0)
00403 *cmpp = 1;
00404 else if (tlen > 0)
00405 *cmpp = -1;
00406 else
00407 *cmpp = 0;
00408
00409 return (0);
00410 }
00411
00412
00413
00414
00415
00416
00417
00418
00419 int
00420 CDB___db_vrfy_overflow(dbp, vdp, h, pgno, flags)
00421 DB *dbp;
00422 VRFY_DBINFO *vdp;
00423 PAGE *h;
00424 db_pgno_t pgno;
00425 u_int32_t flags;
00426 {
00427 VRFY_PAGEINFO *pip;
00428 int isbad, ret, t_ret;
00429
00430 isbad = 0;
00431 if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
00432 return (ret);
00433
00434 if ((ret = CDB___db_vrfy_datapage(dbp, vdp, h, pgno, flags)) != 0) {
00435 if (ret == DB_VERIFY_BAD)
00436 isbad = 1;
00437 else
00438 goto err;
00439 }
00440
00441 pip->refcount = OV_REF(h);
00442 if (pip->refcount < 1) {
00443 EPRINT((dbp->dbenv, "Overflow page %lu has zero reference count",
00444 pgno));
00445 isbad = 1;
00446 }
00447
00448
00449 pip->olen = HOFFSET(h);
00450
00451 err: if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0)
00452 ret = t_ret;
00453 return ((ret == 0 && isbad == 1) ? DB_VERIFY_BAD : ret);
00454 }
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 int
00465 CDB___db_vrfy_ovfl_structure(dbp, vdp, pgno, tlen, flags)
00466 DB *dbp;
00467 VRFY_DBINFO *vdp;
00468 db_pgno_t pgno;
00469 u_int32_t tlen;
00470 u_int32_t flags;
00471 {
00472 DB *pgset;
00473 VRFY_PAGEINFO *pip;
00474 db_pgno_t next, prev;
00475 int isbad, p, ret, t_ret;
00476 u_int32_t refcount;
00477
00478 pgset = vdp->pgset;
00479 DB_ASSERT(pgset != NULL);
00480 isbad = 0;
00481
00482
00483 if (!IS_VALID_PGNO(pgno))
00484 return (DB_VERIFY_BAD);
00485
00486
00487
00488
00489
00490 if ((ret = CDB___db_vrfy_getpageinfo(vdp, pgno, &pip)) != 0)
00491 return (ret);
00492
00493
00494 refcount = pip->refcount;
00495
00496 if (pip->type != P_OVERFLOW) {
00497 EPRINT((dbp->dbenv,
00498 "Overflow page %lu of invalid type", pgno, pip->type));
00499 ret = DB_VERIFY_BAD;
00500 goto err;
00501 }
00502
00503 prev = pip->prev_pgno;
00504 if (prev != PGNO_INVALID) {
00505 EPRINT((dbp->dbenv,
00506 "First overflow page %lu has a prev_pgno", pgno));
00507 isbad = 1;
00508 }
00509
00510 for (;;) {
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 if (LF_ISSET(ST_OVFL_LEAF)) {
00528 if (F_ISSET(pip, VRFY_OVFL_LEAFSEEN)) {
00529 if ((ret =
00530 CDB___db_vrfy_pgset_dec(pgset, pgno)) != 0)
00531 goto err;
00532 } else
00533 F_SET(pip, VRFY_OVFL_LEAFSEEN);
00534 }
00535
00536 if ((ret = CDB___db_vrfy_pgset_get(pgset, pgno, &p)) != 0)
00537 goto err;
00538
00539
00540
00541
00542
00543 if ((u_int32_t)p > refcount) {
00544 EPRINT((dbp->dbenv,
00545 "Page %lu encountered twice in overflow traversal",
00546 pgno));
00547 ret = DB_VERIFY_BAD;
00548 goto err;
00549 }
00550 if ((ret = CDB___db_vrfy_pgset_inc(pgset, pgno)) != 0)
00551 goto err;
00552
00553
00554 tlen -= pip->olen;
00555
00556 next = pip->next_pgno;
00557
00558
00559 if (next == PGNO_INVALID)
00560 break;
00561
00562
00563
00564
00565
00566 if (!IS_VALID_PGNO(next)) {
00567 DB_ASSERT(0);
00568 EPRINT((dbp->dbenv,
00569 "Overflow page %lu has bad next_pgno",
00570 pgno));
00571 ret = DB_VERIFY_BAD;
00572 goto err;
00573 }
00574
00575 if ((ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 ||
00576 (ret = CDB___db_vrfy_getpageinfo(vdp, next, &pip)) != 0)
00577 return (ret);
00578 if (pip->prev_pgno != pgno) {
00579 EPRINT((dbp->dbenv,
00580 "Overflow page %lu has bogus prev_pgno value",
00581 next));
00582 isbad = 1;
00583
00584
00585
00586
00587 }
00588
00589 pgno = next;
00590 }
00591
00592 if (tlen > 0) {
00593 isbad = 1;
00594 EPRINT((dbp->dbenv,
00595 "Overflow item incomplete on page %lu", pgno));
00596 }
00597
00598 err: if ((t_ret = CDB___db_vrfy_putpageinfo(vdp, pip)) != 0 && ret == 0)
00599 ret = t_ret;
00600 return ((ret == 0 && isbad == 1) ? DB_VERIFY_BAD : ret);
00601 }
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611 int
00612 CDB___db_safe_goff(dbp, vdp, pgno, dbt, buf, flags)
00613 DB *dbp;
00614 VRFY_DBINFO *vdp;
00615 db_pgno_t pgno;
00616 DBT *dbt;
00617 void **buf;
00618 u_int32_t flags;
00619 {
00620 PAGE *h;
00621 int ret, err_ret;
00622 u_int32_t bytesgot, bytes;
00623 u_int8_t *src, *dest;
00624
00625 ret = DB_VERIFY_BAD;
00626 err_ret = 0;
00627 bytesgot = bytes = 0;
00628
00629 while ((pgno != PGNO_INVALID) && (IS_VALID_PGNO(pgno))) {
00630
00631
00632
00633
00634 if ((ret = CDB___db_salvage_markdone(vdp, pgno)) != 0)
00635 break;
00636
00637 if ((ret = CDB_memp_fget(dbp->mpf, &pgno, 0, &h)) != 0)
00638 break;
00639
00640
00641
00642
00643
00644 if (!LF_ISSET(DB_AGGRESSIVE) && TYPE(h) != P_OVERFLOW) {
00645 ret = DB_VERIFY_BAD;
00646 break;
00647 }
00648
00649 src = (u_int8_t *)h + P_OVERHEAD;
00650 bytes = OV_LEN(h);
00651
00652 if (bytes + P_OVERHEAD > dbp->pgsize)
00653 bytes = dbp->pgsize - P_OVERHEAD;
00654
00655 if ((ret = CDB___os_realloc(dbp->dbenv,
00656 bytesgot + bytes, 0, buf)) != 0)
00657 break;
00658
00659 dest = (u_int8_t *)*buf + bytesgot;
00660 bytesgot += bytes;
00661
00662 memcpy(dest, src, bytes);
00663
00664 pgno = NEXT_PGNO(h);
00665
00666 if ((ret = CDB_memp_fput(dbp->mpf, h, 0)) != 0)
00667 err_ret = ret;
00668 }
00669
00670 if (ret == 0) {
00671 dbt->size = bytesgot;
00672 dbt->data = *buf;
00673 }
00674
00675 return ((err_ret != 0 && ret == 0) ? err_ret : ret);
00676 }