2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997, 1998
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid
[] = "@(#)mp_bh.c 10.45 (Sleepycat) 11/25/98";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
25 #include "common_ext.h"
27 static int __memp_upgrade
__P((DB_MPOOL
*, DB_MPOOLFILE
*, MPOOLFILE
*));
31 * Write the page associated with a given bucket header.
33 * PUBLIC: int __memp_bhwrite
34 * PUBLIC: __P((DB_MPOOL *, MPOOLFILE *, BH *, int *, int *));
37 __memp_bhwrite(dbmp
, mfp
, bhp
, restartp
, wrotep
)
41 int *restartp
, *wrotep
;
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
);
70 * Increment the reference count -- see the comment in
77 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
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
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
)
93 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
99 * Try and open the file, attaching to the underlying shared area.
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.
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
))
125 if (__memp_fopen(dbmp
, mfp
, R_ADDR(dbmp
, mfp
->path_off
),
126 0, 0, mfp
->stat
.st_pagesize
, 0, NULL
, &dbmfp
) != 0)
129 found
: ret
= __memp_pgwrite(dbmfp
, bhp
, restartp
, wrotep
);
132 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
134 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
142 * Read a page from a file.
144 * PUBLIC: int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
147 __memp_pgread(dbmfp
, bhp
, can_create
)
155 size_t len
, pagesize
;
161 pagesize
= mfp
->stat
.st_pagesize
;
163 F_SET(bhp
, BH_LOCKED
| BH_TRASH
);
164 LOCKBUFFER(dbmp
, bhp
);
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.
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
;
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
);
192 if (nr
< (ssize_t
)pagesize
)
196 /* If we had a short read, ret may be 0. */
199 __db_err(dbmp
->dbenv
,
200 "%s: page %lu doesn't exist, create flag not set",
201 __memp_fn(dbmfp
), (u_long
)bhp
->pgno
);
206 * Clear any bytes we didn't read that need to be cleared. If we're
207 * running in diagnostic mode, smash any bytes on the page that are
208 * unknown quantities for the caller.
210 if (nr
!= (ssize_t
)pagesize
) {
211 len
= mfp
->clear_len
== 0 ? pagesize
: mfp
->clear_len
;
212 if (nr
< (ssize_t
)len
)
213 memset(bhp
->buf
+ nr
, 0, len
- nr
);
215 if (nr
> (ssize_t
)len
)
218 memset(bhp
->buf
+ len
, 0xdb, pagesize
- len
);
222 /* Call any pgin function. */
223 ret
= mfp
->ftype
== 0 ? 0 : __memp_pg(dbmfp
, bhp
, 1);
225 /* Unlock the buffer and reacquire the region lock. */
226 err
: UNLOCKBUFFER(dbmp
, bhp
);
230 * If no errors occurred, the data is now valid, clear the BH_TRASH
231 * flag; regardless, clear the lock bit and let other threads proceed.
233 F_CLR(bhp
, BH_LOCKED
);
235 F_CLR(bhp
, BH_TRASH
);
237 /* Update the statistics. */
239 ++dbmp
->mp
->stat
.st_page_create
;
240 ++mfp
->stat
.st_page_create
;
242 ++dbmp
->mp
->stat
.st_page_in
;
243 ++mfp
->stat
.st_page_in
;
252 * Write a page to a file.
254 * PUBLIC: int __memp_pgwrite __P((DB_MPOOLFILE *, BH *, int *, int *));
257 __memp_pgwrite(dbmfp
, bhp
, restartp
, wrotep
)
260 int *restartp
, *wrotep
;
270 int callpgin
, dosync
, ret
, syncfail
;
278 if (restartp
!= NULL
)
285 * Check the dirty bit -- this buffer may have been written since we
286 * decided to write it.
288 if (!F_ISSET(bhp
, BH_DIRTY
)) {
294 LOCKBUFFER(dbmp
, bhp
);
297 * If there were two writers, we may have just been waiting while the
298 * other writer completed I/O on this buffer. Check the dirty bit one
301 if (!F_ISSET(bhp
, BH_DIRTY
)) {
302 UNLOCKBUFFER(dbmp
, bhp
);
309 F_SET(bhp
, BH_LOCKED
);
312 if (restartp
!= NULL
)
315 /* Copy the LSN off the page if we're going to need it. */
316 lg_info
= dbenv
->lg_info
;
317 if (lg_info
!= NULL
|| F_ISSET(bhp
, BH_WRITE
))
318 memcpy(&lsn
, bhp
->buf
+ mfp
->lsn_off
, sizeof(DB_LSN
));
320 /* Ensure the appropriate log records are on disk. */
321 if (lg_info
!= NULL
&& (ret
= log_flush(lg_info
, &lsn
)) != 0)
325 * Call any pgout function. We set the callpgin flag so that we flag
326 * that the contents of the buffer will need to be passed through pgin
327 * before they are reused.
333 if ((ret
= __memp_pg(dbmfp
, bhp
, 0)) != 0)
337 /* Temporary files may not yet have been created. */
338 if (dbmfp
->fd
== -1) {
339 LOCKHANDLE(dbmp
, dbmfp
->mutexp
);
340 if (dbmfp
->fd
== -1 && ((ret
= __db_appname(dbenv
,
341 DB_APP_TMP
, NULL
, NULL
, DB_CREATE
| DB_EXCL
| DB_TEMPORARY
,
342 &dbmfp
->fd
, NULL
)) != 0 || dbmfp
->fd
== -1)) {
343 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
345 "unable to create temporary backing file");
348 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
351 /* Write the page. */
352 db_io
.fd_io
= dbmfp
->fd
;
353 db_io
.fd_lock
= dbmp
->reginfo
.fd
;
354 db_io
.mutexp
= F_ISSET(dbmp
, MP_LOCKHANDLE
) ? dbmfp
->mutexp
: NULL
;
355 db_io
.pagesize
= db_io
.bytes
= mfp
->stat
.st_pagesize
;
356 db_io
.pgno
= bhp
->pgno
;
357 db_io
.buf
= bhp
->buf
;
358 if ((ret
= __os_io(&db_io
, DB_IO_WRITE
, &nw
)) != 0) {
359 __db_panic(dbenv
, ret
);
363 if (nw
!= (ssize_t
)mfp
->stat
.st_pagesize
) {
372 /* Unlock the buffer and reacquire the region lock. */
373 UNLOCKBUFFER(dbmp
, bhp
);
377 * Clean up the flags based on a successful write.
379 * If we rewrote the page, it will need processing by the pgin
380 * routine before reuse.
383 F_SET(bhp
, BH_CALLPGIN
);
384 F_CLR(bhp
, BH_DIRTY
| BH_LOCKED
);
387 * If we write a buffer for which a checkpoint is waiting, update
388 * the count of pending buffers (both in the mpool as a whole and
389 * for this file). If the count for this file goes to zero, set a
390 * flag so we flush the writes.
392 if (F_ISSET(bhp
, BH_WRITE
)) {
393 F_CLR(bhp
, BH_WRITE
);
396 dosync
= --mfp
->lsn_cnt
== 0 ? 1 : 0;
400 /* Update the page clean/dirty statistics. */
401 ++mp
->stat
.st_page_clean
;
402 --mp
->stat
.st_page_dirty
;
404 /* Update I/O statistics. */
405 ++mp
->stat
.st_page_out
;
406 ++mfp
->stat
.st_page_out
;
409 * Do the sync after everything else has been updated, so any incoming
410 * checkpoint doesn't see inconsistent information.
413 * Don't lock the region around the sync, fsync(2) has no atomicity
417 * We ignore errors from the sync -- it makes no sense to return an
418 * error to the calling process, so set a flag causing the checkpoint
419 * to be retried later. There is a possibility, of course, that a
420 * subsequent checkpoint was started and that we're going to force it
421 * to fail. That should be unlikely, and fixing it would be difficult.
425 syncfail
= __os_fsync(dbmfp
->fd
) != 0;
428 F_SET(mp
, MP_LSN_RETRY
);
433 syserr
: __db_err(dbenv
, "%s: %s failed for page %lu",
434 __memp_fn(dbmfp
), fail
, (u_long
)bhp
->pgno
);
436 err
: /* Unlock the buffer and reacquire the region lock. */
437 UNLOCKBUFFER(dbmp
, bhp
);
441 * Clean up the flags based on a failure.
443 * The page remains dirty but we remove our lock. If we rewrote the
444 * page, it will need processing by the pgin routine before reuse.
447 F_SET(bhp
, BH_CALLPGIN
);
448 F_CLR(bhp
, BH_LOCKED
);
455 * Call the pgin/pgout routine.
457 * PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
460 __memp_pg(dbmfp
, bhp
, is_pgin
)
474 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
477 for (mpreg
= LIST_FIRST(&dbmp
->dbregq
);
478 mpreg
!= NULL
; mpreg
= LIST_NEXT(mpreg
, q
)) {
479 if (ftype
!= mpreg
->ftype
)
481 if (mfp
->pgcookie_len
== 0)
484 dbt
.size
= mfp
->pgcookie_len
;
485 dbt
.data
= R_ADDR(dbmp
, mfp
->pgcookie_off
);
488 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
491 if (mpreg
->pgin
!= NULL
&& (ret
=
492 mpreg
->pgin(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
495 if (mpreg
->pgout
!= NULL
&& (ret
=
496 mpreg
->pgout(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
502 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
506 err
: UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
507 __db_err(dbmp
->dbenv
, "%s: %s failed for page %lu",
508 __memp_fn(dbmfp
), is_pgin
? "pgin" : "pgout", (u_long
)bhp
->pgno
);
514 * Free a bucket header and its referenced data.
516 * PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
519 __memp_bhfree(dbmp
, mfp
, bhp
, free_mem
)
527 /* Delete the buffer header from the hash bucket queue. */
528 off
= BUCKET(dbmp
->mp
, R_OFFSET(dbmp
, mfp
), bhp
->pgno
);
529 SH_TAILQ_REMOVE(&dbmp
->htab
[off
], bhp
, hq
, __bh
);
531 /* Delete the buffer header from the LRU queue. */
532 SH_TAILQ_REMOVE(&dbmp
->mp
->bhq
, bhp
, q
, __bh
);
535 * If we're not reusing it immediately, free the buffer header
539 __db_shalloc_free(dbmp
->addr
, bhp
);
540 --dbmp
->mp
->stat
.st_page_clean
;
546 * Upgrade a file descriptor from readonly to readwrite.
549 __memp_upgrade(dbmp
, dbmfp
, mfp
)
559 * We expect the handle to already be locked.
562 /* Check to see if we've already upgraded. */
563 if (F_ISSET(dbmfp
, MP_UPGRADE
))
566 /* Check to see if we've already failed. */
567 if (F_ISSET(dbmfp
, MP_UPGRADE_FAIL
))
571 * Calculate the real name for this file and try to open it read/write.
572 * We know we have a valid pathname for the file because it's the only
573 * way we could have gotten a file descriptor of any kind.
575 if ((ret
= __db_appname(dbmp
->dbenv
, DB_APP_DATA
,
576 NULL
, R_ADDR(dbmp
, mfp
->path_off
), 0, NULL
, &rpath
)) != 0)
578 if (__db_open(rpath
, 0, 0, 0, &fd
) != 0) {
579 F_SET(dbmfp
, MP_UPGRADE_FAIL
);
582 /* Swap the descriptors and set the upgrade flag. */
583 (void)__os_close(dbmfp
->fd
);
585 F_SET(dbmfp
, MP_UPGRADE
);