1 /* $OpenBSD: bt_overflow.c,v 1.6 2003/05/01 20:23:40 avsm Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Mike Olson. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. 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 #if defined(LIBC_SCCS) && !defined(lint) 40 #if 0 41 static char sccsid[] = "@(#)bt_overflow.c 8.5 (Berkeley) 7/16/94"; 42 #else 43 static const char rcsid[] = "$OpenBSD: bt_overflow.c,v 1.6 2003/05/01 20:23:40 avsm Exp $"; 44 #endif 45 #endif /* LIBC_SCCS and not lint */ 46 47 #include <sys/param.h> 48 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 53 #include <db.h> 54 #include "btree.h" 55 56 /* 57 * Big key/data code. 58 * 59 * Big key and data entries are stored on linked lists of pages. The initial 60 * reference is byte string stored with the key or data and is the page number 61 * and size. The actual record is stored in a chain of pages linked by the 62 * nextpg field of the PAGE header. 63 * 64 * The first page of the chain has a special property. If the record is used 65 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set 66 * in the header. 67 * 68 * XXX 69 * A single DBT is written to each chain, so a lot of space on the last page 70 * is wasted. This is a fairly major bug for some data sets. 71 */ 72 73 /* 74 * __OVFL_GET -- Get an overflow key/data item. 75 * 76 * Parameters: 77 * t: tree 78 * p: pointer to { pgno_t, u_int32_t } 79 * buf: storage address 80 * bufsz: storage size 81 * 82 * Returns: 83 * RET_ERROR, RET_SUCCESS 84 */ 85 int 86 __ovfl_get(t, p, ssz, buf, bufsz) 87 BTREE *t; 88 void *p; 89 size_t *ssz; 90 void **buf; 91 size_t *bufsz; 92 { 93 PAGE *h; 94 pgno_t pg; 95 size_t nb, plen; 96 u_int32_t sz; 97 void *tp; 98 99 memmove(&pg, p, sizeof(pgno_t)); 100 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t)); 101 *ssz = sz; 102 103 #ifdef DEBUG 104 if (pg == P_INVALID || sz == 0) 105 abort(); 106 #endif 107 /* Make the buffer bigger as necessary. */ 108 if (*bufsz < sz) { 109 tp = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz)); 110 if (tp == NULL) 111 return (RET_ERROR); 112 *buf = tp; 113 *bufsz = sz; 114 } 115 116 /* 117 * Step through the linked list of pages, copying the data on each one 118 * into the buffer. Never copy more than the data's length. 119 */ 120 plen = t->bt_psize - BTDATAOFF; 121 for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) { 122 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 123 return (RET_ERROR); 124 125 nb = MIN(sz, plen); 126 memmove(p, (char *)h + BTDATAOFF, nb); 127 mpool_put(t->bt_mp, h, 0); 128 129 if ((sz -= nb) == 0) 130 break; 131 } 132 return (RET_SUCCESS); 133 } 134 135 /* 136 * __OVFL_PUT -- Store an overflow key/data item. 137 * 138 * Parameters: 139 * t: tree 140 * data: DBT to store 141 * pgno: storage page number 142 * 143 * Returns: 144 * RET_ERROR, RET_SUCCESS 145 */ 146 int 147 __ovfl_put(t, dbt, pg) 148 BTREE *t; 149 const DBT *dbt; 150 pgno_t *pg; 151 { 152 PAGE *h, *last; 153 void *p; 154 pgno_t npg; 155 size_t nb, plen; 156 u_int32_t sz; 157 158 /* 159 * Allocate pages and copy the key/data record into them. Store the 160 * number of the first page in the chain. 161 */ 162 plen = t->bt_psize - BTDATAOFF; 163 for (last = NULL, p = dbt->data, sz = dbt->size;; 164 p = (char *)p + plen, last = h) { 165 if ((h = __bt_new(t, &npg)) == NULL) 166 return (RET_ERROR); 167 168 h->pgno = npg; 169 h->nextpg = h->prevpg = P_INVALID; 170 h->flags = P_OVERFLOW; 171 h->lower = h->upper = 0; 172 173 nb = MIN(sz, plen); 174 memmove((char *)h + BTDATAOFF, p, nb); 175 176 if (last) { 177 last->nextpg = h->pgno; 178 mpool_put(t->bt_mp, last, MPOOL_DIRTY); 179 } else 180 *pg = h->pgno; 181 182 if ((sz -= nb) == 0) { 183 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 184 break; 185 } 186 } 187 return (RET_SUCCESS); 188 } 189 190 /* 191 * __OVFL_DELETE -- Delete an overflow chain. 192 * 193 * Parameters: 194 * t: tree 195 * p: pointer to { pgno_t, u_int32_t } 196 * 197 * Returns: 198 * RET_ERROR, RET_SUCCESS 199 */ 200 int 201 __ovfl_delete(t, p) 202 BTREE *t; 203 void *p; 204 { 205 PAGE *h; 206 pgno_t pg; 207 size_t plen; 208 u_int32_t sz; 209 210 memmove(&pg, p, sizeof(pgno_t)); 211 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t)); 212 213 #ifdef DEBUG 214 if (pg == P_INVALID || sz == 0) 215 abort(); 216 #endif 217 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 218 return (RET_ERROR); 219 220 /* Don't delete chains used by internal pages. */ 221 if (h->flags & P_PRESERVE) { 222 mpool_put(t->bt_mp, h, 0); 223 return (RET_SUCCESS); 224 } 225 226 /* Step through the chain, calling the free routine for each page. */ 227 for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) { 228 pg = h->nextpg; 229 __bt_free(t, h); 230 if (sz <= plen) 231 break; 232 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 233 return (RET_ERROR); 234 } 235 return (RET_SUCCESS); 236 } 237