xref: /illumos-gate/usr/src/lib/krb5/plugins/kdb/db2/libdb2/recno/rec_put.c (revision 54925bf60766fbb4f1f2d7c843721406a7b7a3fb)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*-
4  * Copyright (c) 1990, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)rec_put.c	8.7 (Berkeley) 8/18/94";
38 #endif /* LIBC_SCCS and not lint */
39 
40 #include <sys/types.h>
41 
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 
47 #include "db-int.h"
48 #include "recno.h"
49 
50 /*
51  * __REC_PUT -- Add a recno item to the tree.
52  *
53  * Parameters:
54  *	dbp:	pointer to access method
55  *	key:	key
56  *	data:	data
57  *	flag:	R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
58  *
59  * Returns:
60  *	RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
61  *	already in the tree and R_NOOVERWRITE specified.
62  */
63 int
64 __rec_put(dbp, key, data, flags)
65 	const DB *dbp;
66 	DBT *key;
67 	const DBT *data;
68 	u_int flags;
69 {
70 	BTREE *t;
71 	DBT fdata, tdata;
72 	recno_t nrec;
73 	int status;
74 
75 	t = dbp->internal;
76 
77 	/* Toss any page pinned across calls. */
78 	if (t->bt_pinned != NULL) {
79 		mpool_put(t->bt_mp, t->bt_pinned, 0);
80 		t->bt_pinned = NULL;
81 	}
82 
83 	/*
84 	 * If using fixed-length records, and the record is long, return
85 	 * EINVAL.  If it's short, pad it out.  Use the record data return
86 	 * memory, it's only short-term.
87 	 */
88 	if (F_ISSET(t, R_FIXLEN) && data->size != t->bt_reclen) {
89 		if (data->size > t->bt_reclen)
90 			goto einval;
91 
92 		if (t->bt_rdata.size < t->bt_reclen) {
93 			t->bt_rdata.data = t->bt_rdata.data == NULL ?
94 			    malloc(t->bt_reclen) :
95 			    realloc(t->bt_rdata.data, t->bt_reclen);
96 			if (t->bt_rdata.data == NULL)
97 				return (RET_ERROR);
98 			t->bt_rdata.size = t->bt_reclen;
99 		}
100 		memmove(t->bt_rdata.data, data->data, data->size);
101 		memset((char *)t->bt_rdata.data + data->size,
102 		    t->bt_bval, t->bt_reclen - data->size);
103 		fdata.data = t->bt_rdata.data;
104 		fdata.size = t->bt_reclen;
105 	} else {
106 		fdata.data = data->data;
107 		fdata.size = data->size;
108 	}
109 
110 	switch (flags) {
111 	case R_CURSOR:
112 		if (!F_ISSET(&t->bt_cursor, CURS_INIT))
113 			goto einval;
114 		nrec = t->bt_cursor.rcursor;
115 		break;
116 	case R_SETCURSOR:
117 		if ((nrec = *(recno_t *)key->data) == 0)
118 			goto einval;
119 		break;
120 	case R_IAFTER:
121 		if ((nrec = *(recno_t *)key->data) == 0) {
122 			nrec = 1;
123 			flags = R_IBEFORE;
124 		}
125 		break;
126 	case 0:
127 	case R_IBEFORE:
128 		if ((nrec = *(recno_t *)key->data) == 0)
129 			goto einval;
130 		break;
131 	case R_NOOVERWRITE:
132 		if ((nrec = *(recno_t *)key->data) == 0)
133 			goto einval;
134 		if (nrec <= t->bt_nrecs)
135 			return (RET_SPECIAL);
136 		break;
137 	default:
138 einval:		errno = EINVAL;
139 		return (RET_ERROR);
140 	}
141 
142 	/*
143 	 * Make sure that records up to and including the put record are
144 	 * already in the database.  If skipping records, create empty ones.
145 	 */
146 	if (nrec > t->bt_nrecs) {
147 		if (!F_ISSET(t, R_EOF | R_INMEM) &&
148 		    t->bt_irec(t, nrec) == RET_ERROR)
149 			return (RET_ERROR);
150 		if (nrec > t->bt_nrecs + 1) {
151 			if (F_ISSET(t, R_FIXLEN)) {
152 				if ((tdata.data =
153 				    (void *)malloc(t->bt_reclen)) == NULL)
154 					return (RET_ERROR);
155 				tdata.size = t->bt_reclen;
156 				memset(tdata.data, t->bt_bval, tdata.size);
157 			} else {
158 				tdata.data = NULL;
159 				tdata.size = 0;
160 			}
161 			while (nrec > t->bt_nrecs + 1)
162 				if (__rec_iput(t,
163 				    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
164 					return (RET_ERROR);
165 			if (F_ISSET(t, R_FIXLEN))
166 				free(tdata.data);
167 		}
168 	}
169 
170 	if ((status = __rec_iput(t, nrec - 1, &fdata, flags)) != RET_SUCCESS)
171 		return (status);
172 
173 	if (flags == R_SETCURSOR)
174 		t->bt_cursor.rcursor = nrec;
175 
176 	F_SET(t, R_MODIFIED);
177 	return (__rec_ret(t, NULL, nrec, key, NULL));
178 }
179 
180 /*
181  * __REC_IPUT -- Add a recno item to the tree.
182  *
183  * Parameters:
184  *	t:	tree
185  *	nrec:	record number
186  *	data:	data
187  *
188  * Returns:
189  *	RET_ERROR, RET_SUCCESS
190  */
191 int
192 __rec_iput(t, nrec, data, flags)
193 	BTREE *t;
194 	recno_t nrec;
195 	const DBT *data;
196 	u_int flags;
197 {
198 	DBT tdata;
199 	EPG *e;
200 	PAGE *h;
201 	indx_t idx, nxtindex;
202 	db_pgno_t pg;
203 	u_int32_t nbytes;
204 	int dflags, status;
205 	char *dest, db[NOVFLSIZE];
206 
207 	/*
208 	 * If the data won't fit on a page, store it on indirect pages.
209 	 *
210 	 * XXX
211 	 * If the insert fails later on, these pages aren't recovered.
212 	 */
213 	if (data->size > t->bt_ovflsize) {
214 		if (__ovfl_put(t, data, &pg) == RET_ERROR)
215 			return (RET_ERROR);
216 		tdata.data = db;
217 		tdata.size = NOVFLSIZE;
218 		*(db_pgno_t *)db = pg;
219 		*(u_int32_t *)(db + sizeof(db_pgno_t)) = data->size;
220 		dflags = P_BIGDATA;
221 		data = &tdata;
222 	} else
223 		dflags = 0;
224 
225 	/* __rec_search pins the returned page. */
226 	if ((e = __rec_search(t, nrec,
227 	    nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
228 	    SINSERT : SEARCH)) == NULL)
229 		return (RET_ERROR);
230 
231 	h = e->page;
232 	idx = e->index;
233 
234 	/*
235 	 * Add the specified key/data pair to the tree.  The R_IAFTER and
236 	 * R_IBEFORE flags insert the key after/before the specified key.
237 	 *
238 	 * Pages are split as required.
239 	 */
240 	switch (flags) {
241 	case R_IAFTER:
242 		++idx;
243 		break;
244 	case R_IBEFORE:
245 		break;
246 	default:
247 		if (nrec < t->bt_nrecs &&
248 		    __rec_dleaf(t, h, idx) == RET_ERROR) {
249 			mpool_put(t->bt_mp, h, 0);
250 			return (RET_ERROR);
251 		}
252 		break;
253 	}
254 
255 	/*
256 	 * If not enough room, split the page.  The split code will insert
257 	 * the key and data and unpin the current page.  If inserting into
258 	 * the offset array, shift the pointers up.
259 	 */
260 	nbytes = NRLEAFDBT(data->size);
261 	if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
262 		status = __bt_split(t, h, NULL, data, dflags, nbytes, idx);
263 		if (status == RET_SUCCESS)
264 			++t->bt_nrecs;
265 		return (status);
266 	}
267 
268 	if (idx < (nxtindex = NEXTINDEX(h)))
269 		memmove(h->linp + idx + 1, h->linp + idx,
270 		    (nxtindex - idx) * sizeof(indx_t));
271 	h->lower += sizeof(indx_t);
272 
273 	h->linp[idx] = h->upper -= nbytes;
274 	dest = (char *)h + h->upper;
275 	WR_RLEAF(dest, data, dflags);
276 
277 	++t->bt_nrecs;
278 	F_SET(t, B_MODIFIED);
279 	mpool_put(t->bt_mp, h, MPOOL_DIRTY);
280 
281 	return (RET_SUCCESS);
282 }
283