/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Olson.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/param.h>3536#include <stdio.h>37#include <stdlib.h>38#include <string.h>3940#include <db.h>41#include "btree.h"4243/*44* Big key/data code.45*46* Big key and data entries are stored on linked lists of pages. The initial47* reference is byte string stored with the key or data and is the page number48* and size. The actual record is stored in a chain of pages linked by the49* nextpg field of the PAGE header.50*51* The first page of the chain has a special property. If the record is used52* by an internal page, it cannot be deleted and the P_PRESERVE bit will be set53* in the header.54*55* XXX56* A single DBT is written to each chain, so a lot of space on the last page57* is wasted. This is a fairly major bug for some data sets.58*/5960/*61* __OVFL_GET -- Get an overflow key/data item.62*63* Parameters:64* t: tree65* p: pointer to { pgno_t, u_int32_t }66* buf: storage address67* bufsz: storage size68*69* Returns:70* RET_ERROR, RET_SUCCESS71*/72int73__ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz)74{75PAGE *h;76pgno_t pg;77size_t nb, plen;78u_int32_t sz;7980memmove(&pg, p, sizeof(pgno_t));81memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));82*ssz = sz;8384#ifdef DEBUG85if (pg == P_INVALID || sz == 0)86abort();87#endif88/* Make the buffer bigger as necessary. */89if (*bufsz < sz) {90*buf = reallocf(*buf, sz);91if (*buf == NULL)92return (RET_ERROR);93*bufsz = sz;94}9596/*97* Step through the linked list of pages, copying the data on each one98* into the buffer. Never copy more than the data's length.99*/100plen = t->bt_psize - BTDATAOFF;101for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {102if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)103return (RET_ERROR);104105nb = MIN(sz, plen);106memmove(p, (char *)h + BTDATAOFF, nb);107mpool_put(t->bt_mp, h, 0);108109if ((sz -= nb) == 0)110break;111}112return (RET_SUCCESS);113}114115/*116* __OVFL_PUT -- Store an overflow key/data item.117*118* Parameters:119* t: tree120* data: DBT to store121* pgno: storage page number122*123* Returns:124* RET_ERROR, RET_SUCCESS125*/126int127__ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg)128{129PAGE *h, *last;130void *p;131pgno_t npg;132size_t nb, plen;133u_int32_t sz;134135/*136* Allocate pages and copy the key/data record into them. Store the137* number of the first page in the chain.138*/139plen = t->bt_psize - BTDATAOFF;140for (last = NULL, p = dbt->data, sz = dbt->size;;141p = (char *)p + plen, last = h) {142if ((h = __bt_new(t, &npg)) == NULL)143return (RET_ERROR);144145h->pgno = npg;146h->nextpg = h->prevpg = P_INVALID;147h->flags = P_OVERFLOW;148h->lower = h->upper = 0;149150nb = MIN(sz, plen);151memmove((char *)h + BTDATAOFF, p, nb);152153if (last) {154last->nextpg = h->pgno;155mpool_put(t->bt_mp, last, MPOOL_DIRTY);156} else157*pg = h->pgno;158159if ((sz -= nb) == 0) {160mpool_put(t->bt_mp, h, MPOOL_DIRTY);161break;162}163}164return (RET_SUCCESS);165}166167/*168* __OVFL_DELETE -- Delete an overflow chain.169*170* Parameters:171* t: tree172* p: pointer to { pgno_t, u_int32_t }173*174* Returns:175* RET_ERROR, RET_SUCCESS176*/177int178__ovfl_delete(BTREE *t, void *p)179{180PAGE *h;181pgno_t pg;182size_t plen;183u_int32_t sz;184185memmove(&pg, p, sizeof(pgno_t));186memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t));187188#ifdef DEBUG189if (pg == P_INVALID || sz == 0)190abort();191#endif192if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)193return (RET_ERROR);194195/* Don't delete chains used by internal pages. */196if (h->flags & P_PRESERVE) {197mpool_put(t->bt_mp, h, 0);198return (RET_SUCCESS);199}200201/* Step through the chain, calling the free routine for each page. */202for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {203pg = h->nextpg;204__bt_free(t, h);205if (sz <= plen)206break;207if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)208return (RET_ERROR);209}210return (RET_SUCCESS);211}212213214