Update.
[glibc.git] / db2 / mp / mp_bh.c
blob12c53417d9304b2601b33fa4f351f309b62b459d
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
6 */
7 #include "config.h"
9 #ifndef lint
10 static const char sccsid[] = "@(#)mp_bh.c 10.45 (Sleepycat) 11/25/98";
11 #endif /* not lint */
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <unistd.h>
19 #endif
21 #include "db_int.h"
22 #include "shqueue.h"
23 #include "db_shash.h"
24 #include "mp.h"
25 #include "common_ext.h"
27 static int __memp_upgrade __P((DB_MPOOL *, DB_MPOOLFILE *, MPOOLFILE *));
30 * __memp_bhwrite --
31 * Write the page associated with a given bucket header.
33 * PUBLIC: int __memp_bhwrite
34 * PUBLIC: __P((DB_MPOOL *, MPOOLFILE *, BH *, int *, int *));
36 int
37 __memp_bhwrite(dbmp, mfp, bhp, restartp, wrotep)
38 DB_MPOOL *dbmp;
39 MPOOLFILE *mfp;
40 BH *bhp;
41 int *restartp, *wrotep;
43 DB_MPOOLFILE *dbmfp;
44 DB_MPREG *mpreg;
45 int incremented, ret;
47 if (restartp != NULL)
48 *restartp = 0;
49 if (wrotep != NULL)
50 *wrotep = 0;
51 incremented = 0;
54 * Walk the process' DB_MPOOLFILE list and find a file descriptor for
55 * the file. We also check that the descriptor is open for writing.
56 * If we find a descriptor on the file that's not open for writing, we
57 * try and upgrade it to make it writeable. If that fails, we're done.
59 LOCKHANDLE(dbmp, dbmp->mutexp);
60 for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
61 dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q))
62 if (dbmfp->mfp == mfp) {
63 if (F_ISSET(dbmfp, MP_READONLY) &&
64 __memp_upgrade(dbmp, dbmfp, mfp)) {
65 UNLOCKHANDLE(dbmp, dbmp->mutexp);
66 return (0);
70 * Increment the reference count -- see the comment in
71 * memp_fclose().
73 ++dbmfp->ref;
74 incremented = 1;
75 break;
77 UNLOCKHANDLE(dbmp, dbmp->mutexp);
78 if (dbmfp != NULL)
79 goto found;
82 * It's not a page from a file we've opened. If the file requires
83 * input/output processing, see if this process has ever registered
84 * information as to how to write this type of file. If not, there's
85 * nothing we can do.
87 if (mfp->ftype != 0) {
88 LOCKHANDLE(dbmp, dbmp->mutexp);
89 for (mpreg = LIST_FIRST(&dbmp->dbregq);
90 mpreg != NULL; mpreg = LIST_NEXT(mpreg, q))
91 if (mpreg->ftype == mfp->ftype)
92 break;
93 UNLOCKHANDLE(dbmp, dbmp->mutexp);
94 if (mpreg == NULL)
95 return (0);
99 * Try and open the file, attaching to the underlying shared area.
101 * XXX
102 * Don't try to attach to temporary files. There are two problems in
103 * trying to do that. First, if we have different privileges than the
104 * process that "owns" the temporary file, we might create the backing
105 * disk file such that the owning process couldn't read/write its own
106 * buffers, e.g., memp_trickle() running as root creating a file owned
107 * as root, mode 600. Second, if the temporary file has already been
108 * created, we don't have any way of finding out what its real name is,
109 * and, even if we did, it was already unlinked (so that it won't be
110 * left if the process dies horribly). This decision causes a problem,
111 * however: if the temporary file consumes the entire buffer cache,
112 * and the owner doesn't flush the buffers to disk, we could end up
113 * with resource starvation, and the memp_trickle() thread couldn't do
114 * anything about it. That's a pretty unlikely scenario, though.
116 * XXX
117 * There's no negative cache, so we may repeatedly try and open files
118 * that we have previously tried (and failed) to open.
120 * Ignore any error, assume it's a permissions problem.
122 if (F_ISSET(mfp, MP_TEMP))
123 return (0);
125 if (__memp_fopen(dbmp, mfp, R_ADDR(dbmp, mfp->path_off),
126 0, 0, mfp->stat.st_pagesize, 0, NULL, &dbmfp) != 0)
127 return (0);
129 found: ret = __memp_pgwrite(dbmfp, bhp, restartp, wrotep);
131 if (incremented) {
132 LOCKHANDLE(dbmp, dbmp->mutexp);
133 --dbmfp->ref;
134 UNLOCKHANDLE(dbmp, dbmp->mutexp);
137 return (ret);
141 * __memp_pgread --
142 * Read a page from a file.
144 * PUBLIC: int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
147 __memp_pgread(dbmfp, bhp, can_create)
148 DB_MPOOLFILE *dbmfp;
149 BH *bhp;
150 int can_create;
152 DB_IO db_io;
153 DB_MPOOL *dbmp;
154 MPOOLFILE *mfp;
155 size_t len, pagesize;
156 ssize_t nr;
157 int created, ret;
159 dbmp = dbmfp->dbmp;
160 mfp = dbmfp->mfp;
161 pagesize = mfp->stat.st_pagesize;
163 F_SET(bhp, BH_LOCKED | BH_TRASH);
164 LOCKBUFFER(dbmp, bhp);
165 UNLOCKREGION(dbmp);
168 * Temporary files may not yet have been created. We don't create
169 * them now, we create them when the pages have to be flushed.
171 nr = 0;
172 if (dbmfp->fd == -1)
173 ret = 0;
174 else {
176 * Ignore read errors if we have permission to create the page.
177 * Assume that the page doesn't exist, and that we'll create it
178 * when we write it out.
180 db_io.fd_io = dbmfp->fd;
181 db_io.fd_lock = dbmp->reginfo.fd;
182 db_io.mutexp =
183 F_ISSET(dbmp, MP_LOCKHANDLE) ? dbmfp->mutexp : NULL;
184 db_io.pagesize = db_io.bytes = pagesize;
185 db_io.pgno = bhp->pgno;
186 db_io.buf = bhp->buf;
188 ret = __os_io(&db_io, DB_IO_READ, &nr);
191 created = 0;
192 if (nr < (ssize_t)pagesize) {
193 if (can_create)
194 created = 1;
195 else {
196 /* If we had a short read, ret may be 0. */
197 if (ret == 0)
198 ret = EIO;
199 __db_err(dbmp->dbenv,
200 "%s: page %lu doesn't exist, create flag not set",
201 __memp_fn(dbmfp), (u_long)bhp->pgno);
202 goto err;
207 * Clear any bytes we didn't read that need to be cleared. If we're
208 * running in diagnostic mode, smash any bytes on the page that are
209 * unknown quantities for the caller.
211 if (nr != (ssize_t)pagesize) {
212 len = mfp->clear_len == 0 ? pagesize : mfp->clear_len;
213 if (nr < (ssize_t)len)
214 memset(bhp->buf + nr, 0, len - nr);
215 #ifdef DIAGNOSTIC
216 if (nr > (ssize_t)len)
217 len = nr;
218 if (len < pagesize)
219 memset(bhp->buf + len, 0xdb, pagesize - len);
220 #endif
223 /* Call any pgin function. */
224 ret = mfp->ftype == 0 ? 0 : __memp_pg(dbmfp, bhp, 1);
226 /* Unlock the buffer and reacquire the region lock. */
227 err: UNLOCKBUFFER(dbmp, bhp);
228 LOCKREGION(dbmp);
231 * If no errors occurred, the data is now valid, clear the BH_TRASH
232 * flag; regardless, clear the lock bit and let other threads proceed.
234 F_CLR(bhp, BH_LOCKED);
235 if (ret == 0) {
236 F_CLR(bhp, BH_TRASH);
238 /* Update the statistics. */
239 if (created) {
240 ++dbmp->mp->stat.st_page_create;
241 ++mfp->stat.st_page_create;
242 } else {
243 ++dbmp->mp->stat.st_page_in;
244 ++mfp->stat.st_page_in;
248 return (ret);
252 * __memp_pgwrite --
253 * Write a page to a file.
255 * PUBLIC: int __memp_pgwrite __P((DB_MPOOLFILE *, BH *, int *, int *));
258 __memp_pgwrite(dbmfp, bhp, restartp, wrotep)
259 DB_MPOOLFILE *dbmfp;
260 BH *bhp;
261 int *restartp, *wrotep;
263 DB_ENV *dbenv;
264 DB_IO db_io;
265 DB_LOG *lg_info;
266 DB_LSN lsn;
267 DB_MPOOL *dbmp;
268 MPOOL *mp;
269 MPOOLFILE *mfp;
270 ssize_t nw;
271 int callpgin, ret, syncfail;
272 const char *fail;
274 dbmp = dbmfp->dbmp;
275 dbenv = dbmp->dbenv;
276 mp = dbmp->mp;
277 mfp = dbmfp->mfp;
279 if (restartp != NULL)
280 *restartp = 0;
281 if (wrotep != NULL)
282 *wrotep = 0;
283 callpgin = 0;
286 * Check the dirty bit -- this buffer may have been written since we
287 * decided to write it.
289 if (!F_ISSET(bhp, BH_DIRTY)) {
290 if (wrotep != NULL)
291 *wrotep = 1;
292 return (0);
295 LOCKBUFFER(dbmp, bhp);
298 * If there were two writers, we may have just been waiting while the
299 * other writer completed I/O on this buffer. Check the dirty bit one
300 * more time.
302 if (!F_ISSET(bhp, BH_DIRTY)) {
303 UNLOCKBUFFER(dbmp, bhp);
305 if (wrotep != NULL)
306 *wrotep = 1;
307 return (0);
310 F_SET(bhp, BH_LOCKED);
311 UNLOCKREGION(dbmp);
313 if (restartp != NULL)
314 *restartp = 1;
316 /* Copy the LSN off the page if we're going to need it. */
317 lg_info = dbenv->lg_info;
318 if (lg_info != NULL || F_ISSET(bhp, BH_WRITE))
319 memcpy(&lsn, bhp->buf + mfp->lsn_off, sizeof(DB_LSN));
321 /* Ensure the appropriate log records are on disk. */
322 if (lg_info != NULL && (ret = log_flush(lg_info, &lsn)) != 0)
323 goto err;
326 * Call any pgout function. We set the callpgin flag so that we flag
327 * that the contents of the buffer will need to be passed through pgin
328 * before they are reused.
330 if (mfp->ftype == 0)
331 ret = 0;
332 else {
333 callpgin = 1;
334 if ((ret = __memp_pg(dbmfp, bhp, 0)) != 0)
335 goto err;
338 /* Temporary files may not yet have been created. */
339 if (dbmfp->fd == -1) {
340 LOCKHANDLE(dbmp, dbmfp->mutexp);
341 if (dbmfp->fd == -1 && ((ret = __db_appname(dbenv,
342 DB_APP_TMP, NULL, NULL, DB_CREATE | DB_EXCL | DB_TEMPORARY,
343 &dbmfp->fd, NULL)) != 0 || dbmfp->fd == -1)) {
344 UNLOCKHANDLE(dbmp, dbmfp->mutexp);
345 __db_err(dbenv,
346 "unable to create temporary backing file");
347 goto err;
349 UNLOCKHANDLE(dbmp, dbmfp->mutexp);
352 /* Write the page. */
353 db_io.fd_io = dbmfp->fd;
354 db_io.fd_lock = dbmp->reginfo.fd;
355 db_io.mutexp = F_ISSET(dbmp, MP_LOCKHANDLE) ? dbmfp->mutexp : NULL;
356 db_io.pagesize = db_io.bytes = mfp->stat.st_pagesize;
357 db_io.pgno = bhp->pgno;
358 db_io.buf = bhp->buf;
359 if ((ret = __os_io(&db_io, DB_IO_WRITE, &nw)) != 0) {
360 __db_panic(dbenv, ret);
361 fail = "write";
362 goto syserr;
364 if (nw != (ssize_t)mfp->stat.st_pagesize) {
365 ret = EIO;
366 fail = "write";
367 goto syserr;
370 if (wrotep != NULL)
371 *wrotep = 1;
373 /* Unlock the buffer and reacquire the region lock. */
374 UNLOCKBUFFER(dbmp, bhp);
375 LOCKREGION(dbmp);
378 * Clean up the flags based on a successful write.
380 * If we rewrote the page, it will need processing by the pgin
381 * routine before reuse.
383 if (callpgin)
384 F_SET(bhp, BH_CALLPGIN);
385 F_CLR(bhp, BH_DIRTY | BH_LOCKED);
388 * If we write a buffer for which a checkpoint is waiting, update
389 * the count of pending buffers (both in the mpool as a whole and
390 * for this file). If the count for this file goes to zero, flush
391 * the writes.
393 * XXX:
394 * Don't lock the region around the sync, fsync(2) has no atomicity
395 * issues.
397 * XXX:
398 * We ignore errors from the sync -- it makes no sense to return an
399 * error to the calling process, so set a flag causing the checkpoint
400 * to be retried later.
402 if (F_ISSET(bhp, BH_WRITE)) {
403 if (mfp->lsn_cnt == 1) {
404 UNLOCKREGION(dbmp);
405 syncfail = __os_fsync(dbmfp->fd) != 0;
406 LOCKREGION(dbmp);
407 if (syncfail)
408 F_SET(mp, MP_LSN_RETRY);
412 F_CLR(bhp, BH_WRITE);
415 * If the buffer just written has a larger LSN than the current
416 * max LSN written for this checkpoint, update the saved value.
418 if (log_compare(&lsn, &mp->lsn) > 0)
419 mp->lsn = lsn;
421 --mp->lsn_cnt;
422 --mfp->lsn_cnt;
425 /* Update the page clean/dirty statistics. */
426 ++mp->stat.st_page_clean;
427 --mp->stat.st_page_dirty;
429 /* Update I/O statistics. */
430 ++mp->stat.st_page_out;
431 ++mfp->stat.st_page_out;
433 return (0);
435 syserr: __db_err(dbenv, "%s: %s failed for page %lu",
436 __memp_fn(dbmfp), fail, (u_long)bhp->pgno);
438 err: /* Unlock the buffer and reacquire the region lock. */
439 UNLOCKBUFFER(dbmp, bhp);
440 LOCKREGION(dbmp);
443 * Clean up the flags based on a failure.
445 * The page remains dirty but we remove our lock. If we rewrote the
446 * page, it will need processing by the pgin routine before reuse.
448 if (callpgin)
449 F_SET(bhp, BH_CALLPGIN);
450 F_CLR(bhp, BH_LOCKED);
452 return (ret);
456 * __memp_pg --
457 * Call the pgin/pgout routine.
459 * PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
462 __memp_pg(dbmfp, bhp, is_pgin)
463 DB_MPOOLFILE *dbmfp;
464 BH *bhp;
465 int is_pgin;
467 DBT dbt, *dbtp;
468 DB_MPOOL *dbmp;
469 DB_MPREG *mpreg;
470 MPOOLFILE *mfp;
471 int ftype, ret;
473 dbmp = dbmfp->dbmp;
474 mfp = dbmfp->mfp;
476 LOCKHANDLE(dbmp, dbmp->mutexp);
478 ftype = mfp->ftype;
479 for (mpreg = LIST_FIRST(&dbmp->dbregq);
480 mpreg != NULL; mpreg = LIST_NEXT(mpreg, q)) {
481 if (ftype != mpreg->ftype)
482 continue;
483 if (mfp->pgcookie_len == 0)
484 dbtp = NULL;
485 else {
486 dbt.size = mfp->pgcookie_len;
487 dbt.data = R_ADDR(dbmp, mfp->pgcookie_off);
488 dbtp = &dbt;
490 UNLOCKHANDLE(dbmp, dbmp->mutexp);
492 if (is_pgin) {
493 if (mpreg->pgin != NULL && (ret =
494 mpreg->pgin(bhp->pgno, bhp->buf, dbtp)) != 0)
495 goto err;
496 } else
497 if (mpreg->pgout != NULL && (ret =
498 mpreg->pgout(bhp->pgno, bhp->buf, dbtp)) != 0)
499 goto err;
500 break;
503 if (mpreg == NULL)
504 UNLOCKHANDLE(dbmp, dbmp->mutexp);
506 return (0);
508 err: UNLOCKHANDLE(dbmp, dbmp->mutexp);
509 __db_err(dbmp->dbenv, "%s: %s failed for page %lu",
510 __memp_fn(dbmfp), is_pgin ? "pgin" : "pgout", (u_long)bhp->pgno);
511 return (ret);
515 * __memp_bhfree --
516 * Free a bucket header and its referenced data.
518 * PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
520 void
521 __memp_bhfree(dbmp, mfp, bhp, free_mem)
522 DB_MPOOL *dbmp;
523 MPOOLFILE *mfp;
524 BH *bhp;
525 int free_mem;
527 size_t off;
529 /* Delete the buffer header from the hash bucket queue. */
530 off = BUCKET(dbmp->mp, R_OFFSET(dbmp, mfp), bhp->pgno);
531 SH_TAILQ_REMOVE(&dbmp->htab[off], bhp, hq, __bh);
533 /* Delete the buffer header from the LRU queue. */
534 SH_TAILQ_REMOVE(&dbmp->mp->bhq, bhp, q, __bh);
537 * If we're not reusing it immediately, free the buffer header
538 * and data for real.
540 if (free_mem) {
541 __db_shalloc_free(dbmp->addr, bhp);
542 --dbmp->mp->stat.st_page_clean;
547 * __memp_upgrade --
548 * Upgrade a file descriptor from readonly to readwrite.
550 static int
551 __memp_upgrade(dbmp, dbmfp, mfp)
552 DB_MPOOL *dbmp;
553 DB_MPOOLFILE *dbmfp;
554 MPOOLFILE *mfp;
556 int fd, ret;
557 char *rpath;
560 * !!!
561 * We expect the handle to already be locked.
564 /* Check to see if we've already upgraded. */
565 if (F_ISSET(dbmfp, MP_UPGRADE))
566 return (0);
568 /* Check to see if we've already failed. */
569 if (F_ISSET(dbmfp, MP_UPGRADE_FAIL))
570 return (1);
573 * Calculate the real name for this file and try to open it read/write.
574 * We know we have a valid pathname for the file because it's the only
575 * way we could have gotten a file descriptor of any kind.
577 if ((ret = __db_appname(dbmp->dbenv, DB_APP_DATA,
578 NULL, R_ADDR(dbmp, mfp->path_off), 0, NULL, &rpath)) != 0)
579 return (ret);
580 if (__db_open(rpath, 0, 0, 0, &fd) != 0) {
581 F_SET(dbmfp, MP_UPGRADE_FAIL);
582 ret = 1;
583 } else {
584 /* Swap the descriptors and set the upgrade flag. */
585 (void)__os_close(dbmfp->fd);
586 dbmfp->fd = fd;
587 F_SET(dbmfp, MP_UPGRADE);
588 ret = 0;
590 __os_freestr(rpath);
591 return (ret);