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.38 (Sleepycat) 5/20/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
;
52 * Walk the process' DB_MPOOLFILE list and find a file descriptor for
53 * the file. We also check that the descriptor is open for writing.
54 * If we find a descriptor on the file that's not open for writing, we
55 * try and upgrade it to make it writeable. If that fails, we're done.
57 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
58 for (dbmfp
= TAILQ_FIRST(&dbmp
->dbmfq
);
59 dbmfp
!= NULL
; dbmfp
= TAILQ_NEXT(dbmfp
, q
))
60 if (dbmfp
->mfp
== mfp
) {
61 if (F_ISSET(dbmfp
, MP_READONLY
) &&
62 __memp_upgrade(dbmp
, dbmfp
, mfp
)) {
63 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
68 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
73 * It's not a page from a file we've opened. If the file requires
74 * input/output processing, see if this process has ever registered
75 * information as to how to write this type of file. If not, there's
78 if (mfp
->ftype
!= 0) {
79 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
80 for (mpreg
= LIST_FIRST(&dbmp
->dbregq
);
81 mpreg
!= NULL
; mpreg
= LIST_NEXT(mpreg
, q
))
82 if (mpreg
->ftype
== mfp
->ftype
)
84 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
90 * Try and open the file, attaching to the underlying shared area.
93 * Don't try to attach to temporary files. There are two problems in
94 * trying to do that. First, if we have different privileges than the
95 * process that "owns" the temporary file, we might create the backing
96 * disk file such that the owning process couldn't read/write its own
97 * buffers, e.g., memp_trickle() running as root creating a file owned
98 * as root, mode 600. Second, if the temporary file has already been
99 * created, we don't have any way of finding out what its real name is,
100 * and, even if we did, it was already unlinked (so that it won't be
101 * left if the process dies horribly). This decision causes a problem,
102 * however: if the temporary file consumes the entire buffer cache,
103 * and the owner doesn't flush the buffers to disk, we could end up
104 * with resource starvation, and the memp_trickle() thread couldn't do
105 * anything about it. That's a pretty unlikely scenario, though.
108 * There's no negative cache, so we may repeatedly try and open files
109 * that we have previously tried (and failed) to open.
111 * Ignore any error, assume it's a permissions problem.
113 if (F_ISSET(mfp
, MP_TEMP
))
116 if (__memp_fopen(dbmp
, mfp
, R_ADDR(dbmp
, mfp
->path_off
),
117 0, 0, mfp
->stat
.st_pagesize
, 0, NULL
, &dbmfp
) != 0)
120 found
: return (__memp_pgwrite(dbmfp
, bhp
, restartp
, wrotep
));
125 * Read a page from a file.
127 * PUBLIC: int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
130 __memp_pgread(dbmfp
, bhp
, can_create
)
143 pagesize
= mfp
->stat
.st_pagesize
;
145 F_SET(bhp
, BH_LOCKED
| BH_TRASH
);
146 LOCKBUFFER(dbmp
, bhp
);
150 * Temporary files may not yet have been created.
152 * Seek to the page location.
155 LOCKHANDLE(dbmp
, dbmfp
->mutexp
);
156 if (dbmfp
->fd
== -1 || (ret
=
157 __db_seek(dbmfp
->fd
, pagesize
, bhp
->pgno
, 0, 0, SEEK_SET
)) != 0) {
161 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
162 __db_err(dbmp
->dbenv
,
163 "%s: page %lu doesn't exist, create flag not set",
164 __memp_fn(dbmfp
), (u_long
)bhp
->pgno
);
167 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
169 /* Clear the created page. */
170 if (mfp
->clear_len
== 0)
171 memset(bhp
->buf
, 0, pagesize
);
173 memset(bhp
->buf
, 0, mfp
->clear_len
);
175 memset(bhp
->buf
+ mfp
->clear_len
,
176 0xff, pagesize
- mfp
->clear_len
);
184 * Read the page; short reads are treated like creates, although
185 * any valid data is preserved.
187 ret
= __db_read(dbmfp
->fd
, bhp
->buf
, pagesize
, &nr
);
188 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
192 if (nr
== (ssize_t
)pagesize
)
201 * If we didn't fail until we tried the read, don't clear the
202 * whole page, it wouldn't be insane for a filesystem to just
203 * always behave that way. Else, clear any uninitialized data.
207 mfp
->clear_len
== 0 ? pagesize
: mfp
->clear_len
);
209 memset(bhp
->buf
+ nr
, 0, pagesize
- nr
);
212 /* Call any pgin function. */
213 pgin
: ret
= mfp
->ftype
== 0 ? 0 : __memp_pg(dbmfp
, bhp
, 1);
215 /* Unlock the buffer and reacquire the region lock. */
216 err
: UNLOCKBUFFER(dbmp
, bhp
);
220 * If no errors occurred, the data is now valid, clear the BH_TRASH
221 * flag; regardless, clear the lock bit and let other threads proceed.
223 F_CLR(bhp
, BH_LOCKED
);
225 F_CLR(bhp
, BH_TRASH
);
227 /* Update the statistics. */
229 ++dbmp
->mp
->stat
.st_page_create
;
230 ++mfp
->stat
.st_page_create
;
232 ++dbmp
->mp
->stat
.st_page_in
;
233 ++mfp
->stat
.st_page_in
;
242 * Write a page to a file.
244 * PUBLIC: int __memp_pgwrite __P((DB_MPOOLFILE *, BH *, int *, int *));
247 __memp_pgwrite(dbmfp
, bhp
, restartp
, wrotep
)
250 int *restartp
, *wrotep
;
260 int callpgin
, ret
, syncfail
;
268 if (restartp
!= NULL
)
273 pagesize
= mfp
->stat
.st_pagesize
;
276 * Check the dirty bit -- this buffer may have been written since we
277 * decided to write it.
279 if (!F_ISSET(bhp
, BH_DIRTY
)) {
285 LOCKBUFFER(dbmp
, bhp
);
288 * If there were two writers, we may have just been waiting while the
289 * other writer completed I/O on this buffer. Check the dirty bit one
292 if (!F_ISSET(bhp
, BH_DIRTY
)) {
293 UNLOCKBUFFER(dbmp
, bhp
);
300 F_SET(bhp
, BH_LOCKED
);
303 if (restartp
!= NULL
)
306 /* Copy the LSN off the page if we're going to need it. */
307 lg_info
= dbenv
->lg_info
;
308 if (lg_info
!= NULL
|| F_ISSET(bhp
, BH_WRITE
))
309 memcpy(&lsn
, bhp
->buf
+ mfp
->lsn_off
, sizeof(DB_LSN
));
311 /* Ensure the appropriate log records are on disk. */
312 if (lg_info
!= NULL
&& (ret
= log_flush(lg_info
, &lsn
)) != 0)
316 * Call any pgout function. We set the callpgin flag so that we flag
317 * that the contents of the buffer will need to be passed through pgin
318 * before they are reused.
324 if ((ret
= __memp_pg(dbmfp
, bhp
, 0)) != 0)
328 /* Temporary files may not yet have been created. */
329 LOCKHANDLE(dbmp
, dbmfp
->mutexp
);
330 if (dbmfp
->fd
== -1 &&
331 ((ret
= __db_appname(dbenv
, DB_APP_TMP
, NULL
, NULL
,
332 DB_CREATE
| DB_EXCL
| DB_TEMPORARY
, &dbmfp
->fd
, NULL
)) != 0 ||
334 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
335 __db_err(dbenv
, "unable to create temporary backing file");
340 * Write the page out.
343 * Shut the compiler up; it doesn't understand the correlation between
344 * the failing clauses to __db_lseek and __db_write and this ret != 0.
346 COMPQUIET(fail
, NULL
);
348 __db_seek(dbmfp
->fd
, pagesize
, bhp
->pgno
, 0, 0, SEEK_SET
)) != 0)
350 else if ((ret
= __db_write(dbmfp
->fd
, bhp
->buf
, pagesize
, &nw
)) != 0)
352 UNLOCKHANDLE(dbmp
, dbmfp
->mutexp
);
356 if (nw
!= (ssize_t
)pagesize
) {
365 /* Unlock the buffer and reacquire the region lock. */
366 UNLOCKBUFFER(dbmp
, bhp
);
370 * Clean up the flags based on a successful write.
372 * If we rewrote the page, it will need processing by the pgin
373 * routine before reuse.
376 F_SET(bhp
, BH_CALLPGIN
);
377 F_CLR(bhp
, BH_DIRTY
| BH_LOCKED
);
380 * If we write a buffer for which a checkpoint is waiting, update
381 * the count of pending buffers (both in the mpool as a whole and
382 * for this file). If the count for this file goes to zero, flush
386 * Don't lock the region around the sync, fsync(2) has no atomicity
390 * We ignore errors from the sync -- it makes no sense to return an
391 * error to the calling process, so set a flag causing the checkpoint
392 * to be retried later.
394 if (F_ISSET(bhp
, BH_WRITE
)) {
395 if (mfp
->lsn_cnt
== 1) {
397 syncfail
= __db_fsync(dbmfp
->fd
) != 0;
400 F_SET(mp
, MP_LSN_RETRY
);
404 F_CLR(bhp
, BH_WRITE
);
407 * If the buffer just written has a larger LSN than the current
408 * max LSN written for this checkpoint, update the saved value.
410 if (log_compare(&lsn
, &mp
->lsn
) > 0)
417 /* Update the page clean/dirty statistics. */
418 ++mp
->stat
.st_page_clean
;
419 --mp
->stat
.st_page_dirty
;
421 /* Update I/O statistics. */
422 ++mp
->stat
.st_page_out
;
423 ++mfp
->stat
.st_page_out
;
427 syserr
: __db_err(dbenv
, "%s: %s failed for page %lu",
428 __memp_fn(dbmfp
), fail
, (u_long
)bhp
->pgno
);
430 err
: /* Unlock the buffer and reacquire the region lock. */
431 UNLOCKBUFFER(dbmp
, bhp
);
435 * Clean up the flags based on a failure.
437 * The page remains dirty but we remove our lock. If we rewrote the
438 * page, it will need processing by the pgin routine before reuse.
441 F_SET(bhp
, BH_CALLPGIN
);
442 F_CLR(bhp
, BH_LOCKED
);
449 * Call the pgin/pgout routine.
451 * PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
454 __memp_pg(dbmfp
, bhp
, is_pgin
)
468 LOCKHANDLE(dbmp
, dbmp
->mutexp
);
471 for (mpreg
= LIST_FIRST(&dbmp
->dbregq
);
472 mpreg
!= NULL
; mpreg
= LIST_NEXT(mpreg
, q
)) {
473 if (ftype
!= mpreg
->ftype
)
475 if (mfp
->pgcookie_len
== 0)
478 dbt
.size
= mfp
->pgcookie_len
;
479 dbt
.data
= R_ADDR(dbmp
, mfp
->pgcookie_off
);
482 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
485 if (mpreg
->pgin
!= NULL
&& (ret
=
486 mpreg
->pgin(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
489 if (mpreg
->pgout
!= NULL
&& (ret
=
490 mpreg
->pgout(bhp
->pgno
, bhp
->buf
, dbtp
)) != 0)
496 UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
500 err
: UNLOCKHANDLE(dbmp
, dbmp
->mutexp
);
501 __db_err(dbmp
->dbenv
, "%s: %s failed for page %lu",
502 __memp_fn(dbmfp
), is_pgin
? "pgin" : "pgout", (u_long
)bhp
->pgno
);
508 * Free a bucket header and its referenced data.
510 * PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
513 __memp_bhfree(dbmp
, mfp
, bhp
, free_mem
)
521 /* Delete the buffer header from the hash bucket queue. */
522 off
= BUCKET(dbmp
->mp
, R_OFFSET(dbmp
, mfp
), bhp
->pgno
);
523 SH_TAILQ_REMOVE(&dbmp
->htab
[off
], bhp
, hq
, __bh
);
525 /* Delete the buffer header from the LRU queue. */
526 SH_TAILQ_REMOVE(&dbmp
->mp
->bhq
, bhp
, q
, __bh
);
529 * If we're not reusing it immediately, free the buffer header
533 __db_shalloc_free(dbmp
->addr
, bhp
);
534 --dbmp
->mp
->stat
.st_page_clean
;
540 * Upgrade a file descriptor from readonly to readwrite.
543 __memp_upgrade(dbmp
, dbmfp
, mfp
)
553 * We expect the handle to already be locked.
556 /* Check to see if we've already upgraded. */
557 if (F_ISSET(dbmfp
, MP_UPGRADE
))
560 /* Check to see if we've already failed. */
561 if (F_ISSET(dbmfp
, MP_UPGRADE_FAIL
))
565 * Calculate the real name for this file and try to open it read/write.
566 * We know we have a valid pathname for the file because it's the only
567 * way we could have gotten a file descriptor of any kind.
569 if ((ret
= __db_appname(dbmp
->dbenv
, DB_APP_DATA
,
570 NULL
, R_ADDR(dbmp
, mfp
->path_off
), 0, NULL
, &rpath
)) != 0)
572 if (__db_open(rpath
, 0, 0, 0, &fd
) != 0) {
573 F_SET(dbmfp
, MP_UPGRADE_FAIL
);
576 /* Swap the descriptors and set the upgrade flag. */
577 (void)__db_close(dbmfp
->fd
);
579 F_SET(dbmfp
, MP_UPGRADE
);