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
);
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
);
216 if (nr
> (ssize_t
)len
)
219 memset(bhp
->buf
+ len
, 0xdb, pagesize
- len
);
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
);
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
);
236 F_CLR(bhp
, BH_TRASH
);
238 /* Update the statistics. */
240 ++dbmp
->mp
->stat
.st_page_create
;
241 ++mfp
->stat
.st_page_create
;
243 ++dbmp
->mp
->stat
.st_page_in
;
244 ++mfp
->stat
.st_page_in
;
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
)
261 int *restartp
, *wrotep
;
271 int callpgin
, ret
, syncfail
;
279 if (restartp
!= NULL
)
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
)) {
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
302 if (!F_ISSET(bhp
, BH_DIRTY
)) {
303 UNLOCKBUFFER(dbmp
, bhp
);
310 F_SET(bhp
, BH_LOCKED
);
313 if (restartp
!= NULL
)
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)
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.
334 if ((ret
= __memp_pg(dbmfp
, bhp
, 0)) != 0)
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
);
346 "unable to create temporary backing file");
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
);
364 if (nw
!= (ssize_t
)mfp
->stat
.st_pagesize
) {
373 /* Unlock the buffer and reacquire the region lock. */
374 UNLOCKBUFFER(dbmp
, bhp
);
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.
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
394 * Don't lock the region around the sync, fsync(2) has no atomicity
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) {
405 syncfail
= __os_fsync(dbmfp
->fd
) != 0;
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)
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
;
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
);
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.
449 F_SET(bhp
, BH_CALLPGIN
);
450 F_CLR(bhp
, BH_LOCKED
);
457 * Call the pgin/pgout routine.
459 * PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
462 __memp_pg(dbmfp
, bhp
, is_pgin
)
476 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
479 for (mpreg
= LIST_FIRST(&dbmp
->dbregq
);
480 mpreg
!= NULL
; mpreg
= LIST_NEXT(mpreg
, q
)) {
481 if (ftype
!= mpreg
->ftype
)
483 if (mfp
->pgcookie_len
== 0)
486 dbt
.size
= mfp
->pgcookie_len
;
487 dbt
.data
= R_ADDR(dbmp
, mfp
->pgcookie_off
);
490 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
493 if (mpreg
->pgin
!= NULL
&& (ret
=
494 mpreg
->pgin(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
497 if (mpreg
->pgout
!= NULL
&& (ret
=
498 mpreg
->pgout(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
504 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
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
);
516 * Free a bucket header and its referenced data.
518 * PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
521 __memp_bhfree(dbmp
, mfp
, bhp
, free_mem
)
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
541 __db_shalloc_free(dbmp
->addr
, bhp
);
542 --dbmp
->mp
->stat
.st_page_clean
;
548 * Upgrade a file descriptor from readonly to readwrite.
551 __memp_upgrade(dbmp
, dbmfp
, mfp
)
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
))
568 /* Check to see if we've already failed. */
569 if (F_ISSET(dbmfp
, MP_UPGRADE_FAIL
))
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)
580 if (__db_open(rpath
, 0, 0, 0, &fd
) != 0) {
581 F_SET(dbmfp
, MP_UPGRADE_FAIL
);
584 /* Swap the descriptors and set the upgrade flag. */
585 (void)__os_close(dbmfp
->fd
);
587 F_SET(dbmfp
, MP_UPGRADE
);