1 /* $OpenBSD: bt_overflow.c,v 1.8 2005/03/23 19:34:58 otto 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #if defined(LIBC_SCCS) && !defined(lint) 36 #if 0 37 static char sccsid[] = "@(#)bt_overflow.c 8.5 (Berkeley) 7/16/94"; 38 #else 39 static const char rcsid[] = "$OpenBSD: bt_overflow.c,v 1.8 2005/03/23 19:34:58 otto Exp $"; 40 #endif 41 #endif /* LIBC_SCCS and not lint */ 42 43 #include <sys/param.h> 44 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #include <db.h> 50 #include "btree.h" 51 52 /* 53 * Big key/data code. 54 * 55 * Big key and data entries are stored on linked lists of pages. The initial 56 * reference is byte string stored with the key or data and is the page number 57 * and size. The actual record is stored in a chain of pages linked by the 58 * nextpg field of the PAGE header. 59 * 60 * The first page of the chain has a special property. If the record is used 61 * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set 62 * in the header. 63 * 64 * XXX 65 * A single DBT is written to each chain, so a lot of space on the last page 66 * is wasted. This is a fairly major bug for some data sets. 67 */ 68 69 /* 70 * __OVFL_GET -- Get an overflow key/data item. 71 * 72 * Parameters: 73 * t: tree 74 * p: pointer to { pgno_t, u_int32_t } 75 * buf: storage address 76 * bufsz: storage size 77 * 78 * Returns: 79 * RET_ERROR, RET_SUCCESS 80 */ 81 int 82 __ovfl_get(BTREE *t, void *p, size_t *ssz, void **buf, size_t *bufsz) 83 { 84 PAGE *h; 85 pgno_t pg; 86 size_t nb, plen; 87 u_int32_t sz; 88 void *tp; 89 90 memmove(&pg, p, sizeof(pgno_t)); 91 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t)); 92 *ssz = sz; 93 94 #ifdef DEBUG 95 if (pg == P_INVALID || sz == 0) 96 abort(); 97 #endif 98 /* Make the buffer bigger as necessary. */ 99 if (*bufsz < sz) { 100 tp = (char *)(*buf == NULL ? malloc(sz) : realloc(*buf, sz)); 101 if (tp == NULL) 102 return (RET_ERROR); 103 *buf = tp; 104 *bufsz = sz; 105 } 106 107 /* 108 * Step through the linked list of pages, copying the data on each one 109 * into the buffer. Never copy more than the data's length. 110 */ 111 plen = t->bt_psize - BTDATAOFF; 112 for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) { 113 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 114 return (RET_ERROR); 115 116 nb = MIN(sz, plen); 117 memmove(p, (char *)h + BTDATAOFF, nb); 118 mpool_put(t->bt_mp, h, 0); 119 120 if ((sz -= nb) == 0) 121 break; 122 } 123 return (RET_SUCCESS); 124 } 125 126 /* 127 * __OVFL_PUT -- Store an overflow key/data item. 128 * 129 * Parameters: 130 * t: tree 131 * data: DBT to store 132 * pgno: storage page number 133 * 134 * Returns: 135 * RET_ERROR, RET_SUCCESS 136 */ 137 int 138 __ovfl_put(BTREE *t, const DBT *dbt, pgno_t *pg) 139 { 140 PAGE *h, *last; 141 void *p; 142 pgno_t npg; 143 size_t nb, plen; 144 u_int32_t sz; 145 146 /* 147 * Allocate pages and copy the key/data record into them. Store the 148 * number of the first page in the chain. 149 */ 150 plen = t->bt_psize - BTDATAOFF; 151 for (last = NULL, p = dbt->data, sz = dbt->size;; 152 p = (char *)p + plen, last = h) { 153 if ((h = __bt_new(t, &npg)) == NULL) 154 return (RET_ERROR); 155 156 h->pgno = npg; 157 h->nextpg = h->prevpg = P_INVALID; 158 h->flags = P_OVERFLOW; 159 h->lower = h->upper = 0; 160 161 nb = MIN(sz, plen); 162 memmove((char *)h + BTDATAOFF, p, nb); 163 164 if (last) { 165 last->nextpg = h->pgno; 166 mpool_put(t->bt_mp, last, MPOOL_DIRTY); 167 } else 168 *pg = h->pgno; 169 170 if ((sz -= nb) == 0) { 171 mpool_put(t->bt_mp, h, MPOOL_DIRTY); 172 break; 173 } 174 } 175 return (RET_SUCCESS); 176 } 177 178 /* 179 * __OVFL_DELETE -- Delete an overflow chain. 180 * 181 * Parameters: 182 * t: tree 183 * p: pointer to { pgno_t, u_int32_t } 184 * 185 * Returns: 186 * RET_ERROR, RET_SUCCESS 187 */ 188 int 189 __ovfl_delete(BTREE *t, void *p) 190 { 191 PAGE *h; 192 pgno_t pg; 193 size_t plen; 194 u_int32_t sz; 195 196 memmove(&pg, p, sizeof(pgno_t)); 197 memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(u_int32_t)); 198 199 #ifdef DEBUG 200 if (pg == P_INVALID || sz == 0) 201 abort(); 202 #endif 203 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 204 return (RET_ERROR); 205 206 /* Don't delete chains used by internal pages. */ 207 if (h->flags & P_PRESERVE) { 208 mpool_put(t->bt_mp, h, 0); 209 return (RET_SUCCESS); 210 } 211 212 /* Step through the chain, calling the free routine for each page. */ 213 for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) { 214 pg = h->nextpg; 215 __bt_free(t, h); 216 if (sz <= plen) 217 break; 218 if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL) 219 return (RET_ERROR); 220 } 221 return (RET_SUCCESS); 222 } 223