mp_sync.c 22.8 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1996-2005
unknown's avatar
unknown committed
5
 *	Sleepycat Software.  All rights reserved.
unknown's avatar
unknown committed
6
 *
unknown's avatar
unknown committed
7
 * $Id: mp_sync.c,v 12.11 2005/10/07 20:21:33 ubell Exp $
unknown's avatar
unknown committed
8 9
 */

unknown's avatar
unknown committed
10
#include "db_config.h"
unknown's avatar
unknown committed
11 12 13 14 15 16 17 18

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <stdlib.h>
#endif

#include "db_int.h"
unknown's avatar
unknown committed
19
#include "dbinc/db_shash.h"
unknown's avatar
unknown committed
20
#include "dbinc/log.h"
unknown's avatar
unknown committed
21
#include "dbinc/mp.h"
unknown's avatar
unknown committed
22

unknown's avatar
unknown committed
23 24 25 26 27 28
typedef struct {
	DB_MPOOL_HASH *track_hp;	/* Hash bucket. */

	roff_t	  track_off;		/* Page file offset. */
	db_pgno_t track_pgno;		/* Page number. */
} BH_TRACK;
unknown's avatar
unknown committed
29 30

static int __bhcmp __P((const void *, const void *));
unknown's avatar
unknown committed
31
static int __memp_close_flush_files __P((DB_ENV *, DB_MPOOL *, int));
unknown's avatar
unknown committed
32
static int __memp_sync_files __P((DB_ENV *, DB_MPOOL *));
unknown's avatar
unknown committed
33 34

/*
unknown's avatar
unknown committed
35 36
 * __memp_sync_pp --
 *	DB_ENV->memp_sync pre/post processing.
unknown's avatar
unknown committed
37
 *
unknown's avatar
unknown committed
38
 * PUBLIC: int __memp_sync_pp __P((DB_ENV *, DB_LSN *));
unknown's avatar
unknown committed
39 40
 */
int
unknown's avatar
unknown committed
41
__memp_sync_pp(dbenv, lsnp)
unknown's avatar
unknown committed
42 43 44
	DB_ENV *dbenv;
	DB_LSN *lsnp;
{
unknown's avatar
unknown committed
45 46
	DB_THREAD_INFO *ip;
	int ret;
unknown's avatar
unknown committed
47 48

	PANIC_CHECK(dbenv);
unknown's avatar
unknown committed
49 50
	ENV_REQUIRES_CONFIG(dbenv,
	    dbenv->mp_handle, "memp_sync", DB_INIT_MPOOL);
unknown's avatar
unknown committed
51 52

	/*
unknown's avatar
unknown committed
53 54
	 * If no LSN is provided, flush the entire cache (reasonable usage
	 * even if there's no log subsystem configured).
unknown's avatar
unknown committed
55
	 */
unknown's avatar
unknown committed
56 57 58
	if (lsnp != NULL)
		ENV_REQUIRES_CONFIG(dbenv,
		    dbenv->lg_handle, "memp_sync", DB_INIT_LOG);
unknown's avatar
unknown committed
59

unknown's avatar
unknown committed
60 61 62
	ENV_ENTER(dbenv, ip);
	REPLICATION_WRAP(dbenv, (__memp_sync(dbenv, lsnp)), ret);
	ENV_LEAVE(dbenv, ip);
unknown's avatar
unknown committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	return (ret);
}

/*
 * __memp_sync --
 *	DB_ENV->memp_sync.
 *
 * PUBLIC: int __memp_sync __P((DB_ENV *, DB_LSN *));
 */
int
__memp_sync(dbenv, lsnp)
	DB_ENV *dbenv;
	DB_LSN *lsnp;
{
	DB_MPOOL *dbmp;
	MPOOL *mp;
	int ret;

unknown's avatar
unknown committed
81 82
	dbmp = dbenv->mp_handle;
	mp = dbmp->reginfo[0].primary;
unknown's avatar
unknown committed
83

unknown's avatar
unknown committed
84 85
	/* If we've flushed to the requested LSN, return that information. */
	if (lsnp != NULL) {
unknown's avatar
unknown committed
86
		MPOOL_SYSTEM_LOCK(dbenv);
unknown's avatar
unknown committed
87
		if (log_compare(lsnp, &mp->lsn) <= 0) {
unknown's avatar
unknown committed
88 89
			*lsnp = mp->lsn;

unknown's avatar
unknown committed
90
			MPOOL_SYSTEM_UNLOCK(dbenv);
unknown's avatar
unknown committed
91 92
			return (0);
		}
unknown's avatar
unknown committed
93
		MPOOL_SYSTEM_UNLOCK(dbenv);
unknown's avatar
unknown committed
94 95
	}

unknown's avatar
unknown committed
96
	if ((ret = __memp_sync_int(dbenv, NULL, 0, DB_SYNC_CACHE, NULL)) != 0)
unknown's avatar
unknown committed
97 98
		return (ret);

unknown's avatar
unknown committed
99
	if (lsnp != NULL) {
unknown's avatar
unknown committed
100
		MPOOL_SYSTEM_LOCK(dbenv);
unknown's avatar
unknown committed
101 102
		if (log_compare(lsnp, &mp->lsn) > 0)
			mp->lsn = *lsnp;
unknown's avatar
unknown committed
103
		MPOOL_SYSTEM_UNLOCK(dbenv);
unknown's avatar
unknown committed
104 105
	}

unknown's avatar
unknown committed
106
	return (0);
unknown's avatar
unknown committed
107 108 109
}

/*
unknown's avatar
unknown committed
110 111
 * __memp_fsync_pp --
 *	DB_MPOOLFILE->sync pre/post processing.
unknown's avatar
unknown committed
112
 *
unknown's avatar
unknown committed
113
 * PUBLIC: int __memp_fsync_pp __P((DB_MPOOLFILE *));
unknown's avatar
unknown committed
114 115
 */
int
unknown's avatar
unknown committed
116
__memp_fsync_pp(dbmfp)
unknown's avatar
unknown committed
117 118 119
	DB_MPOOLFILE *dbmfp;
{
	DB_ENV *dbenv;
unknown's avatar
unknown committed
120 121
	DB_THREAD_INFO *ip;
	int ret;
unknown's avatar
unknown committed
122

unknown's avatar
unknown committed
123
	dbenv = dbmfp->dbenv;
unknown's avatar
unknown committed
124 125

	PANIC_CHECK(dbenv);
unknown's avatar
unknown committed
126 127
	MPF_ILLEGAL_BEFORE_OPEN(dbmfp, "DB_MPOOLFILE->sync");

unknown's avatar
unknown committed
128 129 130
	ENV_ENTER(dbenv, ip);
	REPLICATION_WRAP(dbenv, (__memp_fsync(dbmfp)), ret);
	ENV_LEAVE(dbenv, ip);
unknown's avatar
unknown committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	return (ret);
}

/*
 * __memp_fsync --
 *	DB_MPOOLFILE->sync.
 *
 * PUBLIC: int __memp_fsync __P((DB_MPOOLFILE *));
 */
int
__memp_fsync(dbmfp)
	DB_MPOOLFILE *dbmfp;
{
	MPOOLFILE *mfp;

	mfp = dbmfp->mfp;
unknown's avatar
unknown committed
147 148 149

	/*
	 * If this handle doesn't have a file descriptor that's open for
unknown's avatar
unknown committed
150 151 152
	 * writing, or if the file is a temporary, or if the file hasn't
	 * been written since it was flushed, there's no reason to proceed
	 * further.
unknown's avatar
unknown committed
153 154 155 156
	 */
	if (F_ISSET(dbmfp, MP_READONLY))
		return (0);

unknown's avatar
unknown committed
157
	if (F_ISSET(dbmfp->mfp, MP_TEMP) || dbmfp->mfp->no_backing_file)
unknown's avatar
unknown committed
158 159
		return (0);

unknown's avatar
unknown committed
160 161 162 163
	if (mfp->file_written == 0)
		return (0);

	return (__memp_sync_int(dbmfp->dbenv, dbmfp, 0, DB_SYNC_FILE, NULL));
unknown's avatar
unknown committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
}

/*
 * __mp_xxx_fh --
 *	Return a file descriptor for DB 1.85 compatibility locking.
 *
 * PUBLIC: int __mp_xxx_fh __P((DB_MPOOLFILE *, DB_FH **));
 */
int
__mp_xxx_fh(dbmfp, fhp)
	DB_MPOOLFILE *dbmfp;
	DB_FH **fhp;
{
	/*
	 * This is a truly spectacular layering violation, intended ONLY to
	 * support compatibility for the DB 1.85 DB->fd call.
	 *
	 * Sync the database file to disk, creating the file as necessary.
	 *
	 * We skip the MP_READONLY and MP_TEMP tests done by memp_fsync(3).
	 * The MP_READONLY test isn't interesting because we will either
	 * already have a file descriptor (we opened the database file for
	 * reading) or we aren't readonly (we created the database which
	 * requires write privileges).  The MP_TEMP test isn't interesting
	 * because we want to write to the backing file regardless so that
	 * we get a file descriptor to return.
	 */
unknown's avatar
unknown committed
191
	if ((*fhp = dbmfp->fhp) != NULL)
unknown's avatar
unknown committed
192 193
		return (0);

unknown's avatar
unknown committed
194
	return (__memp_sync_int(dbmfp->dbenv, dbmfp, 0, DB_SYNC_FILE, NULL));
unknown's avatar
unknown committed
195 196 197
}

/*
unknown's avatar
unknown committed
198 199 200
 * __memp_sync_int --
 *	Mpool sync internal function.
 *
unknown's avatar
unknown committed
201 202
 * PUBLIC: int __memp_sync_int __P((DB_ENV *,
 * PUBLIC:     DB_MPOOLFILE *, u_int32_t, db_sync_op, u_int32_t *));
unknown's avatar
unknown committed
203
 */
unknown's avatar
unknown committed
204
int
unknown's avatar
unknown committed
205
__memp_sync_int(dbenv, dbmfp, trickle_max, op, wrotep)
unknown's avatar
unknown committed
206
	DB_ENV *dbenv;
unknown's avatar
unknown committed
207
	DB_MPOOLFILE *dbmfp;
unknown's avatar
unknown committed
208
	u_int32_t trickle_max, *wrotep;
unknown's avatar
unknown committed
209
	db_sync_op op;
unknown's avatar
unknown committed
210
{
unknown's avatar
unknown committed
211 212
	BH *bhp;
	BH_TRACK *bharray;
unknown's avatar
unknown committed
213
	DB_MPOOL *dbmp;
unknown's avatar
unknown committed
214
	DB_MPOOL_HASH *hp;
unknown's avatar
unknown committed
215
	MPOOL *c_mp, *mp;
unknown's avatar
unknown committed
216
	MPOOLFILE *mfp;
unknown's avatar
unknown committed
217
	db_mutex_t mutex;
unknown's avatar
unknown committed
218 219 220 221
	roff_t last_mf_offset;
	u_int32_t ar_cnt, ar_max, i, n_cache, remaining, wrote;
	int filecnt, hb_lock, maxopenfd, maxwrite, maxwrite_sleep;
	int pass, ret, t_ret, wait_cnt, write_cnt;
unknown's avatar
unknown committed
222

unknown's avatar
unknown committed
223
	dbmp = dbenv->mp_handle;
unknown's avatar
unknown committed
224
	mp = dbmp->reginfo[0].primary;
unknown's avatar
unknown committed
225 226
	last_mf_offset = INVALID_ROFF;
	filecnt = pass = wrote = 0;
unknown's avatar
unknown committed
227

unknown's avatar
unknown committed
228
	/* Get shared configuration information. */
unknown's avatar
unknown committed
229
	MPOOL_SYSTEM_LOCK(dbenv);
unknown's avatar
unknown committed
230 231 232
	maxopenfd = mp->mp_maxopenfd;
	maxwrite = mp->mp_maxwrite;
	maxwrite_sleep = mp->mp_maxwrite_sleep;
unknown's avatar
unknown committed
233
	MPOOL_SYSTEM_UNLOCK(dbenv);
unknown's avatar
unknown committed
234

unknown's avatar
unknown committed
235 236
	/* Assume one dirty page per bucket. */
	ar_max = mp->nreg * mp->htab_buckets;
unknown's avatar
unknown committed
237
	if ((ret =
unknown's avatar
unknown committed
238
	    __os_malloc(dbenv, ar_max * sizeof(BH_TRACK), &bharray)) != 0)
unknown's avatar
unknown committed
239 240 241 242
		return (ret);

	/*
	 * Walk each cache's list of buffers and mark all dirty buffers to be
unknown's avatar
unknown committed
243 244
	 * written and all pinned buffers to be potentially written, depending
	 * on our flags.
unknown's avatar
unknown committed
245
	 */
unknown's avatar
unknown committed
246 247
	for (ar_cnt = 0, n_cache = 0; n_cache < mp->nreg; ++n_cache) {
		c_mp = dbmp->reginfo[n_cache].primary;
unknown's avatar
unknown committed
248

unknown's avatar
unknown committed
249 250
		hp = R_ADDR(&dbmp->reginfo[n_cache], c_mp->htab);
		for (i = 0; i < c_mp->htab_buckets; i++, hp++) {
unknown's avatar
unknown committed
251
			/*
unknown's avatar
unknown committed
252 253 254 255
			 * We can check for empty buckets before locking as we
			 * only care if the pointer is zero or non-zero.  We
			 * can ignore empty buckets because we only need write
			 * buffers that were dirty before we started.
unknown's avatar
unknown committed
256
			 */
unknown's avatar
unknown committed
257 258 259
			if (SH_TAILQ_FIRST(&hp->hash_bucket, __bh) == NULL)
				continue;

unknown's avatar
unknown committed
260
			MUTEX_LOCK(dbenv, hp->mtx_hash);
unknown's avatar
unknown committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			for (bhp = SH_TAILQ_FIRST(&hp->hash_bucket, __bh);
			    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, hq, __bh)) {
				/* Always ignore unreferenced, clean pages. */
				if (bhp->ref == 0 && !F_ISSET(bhp, BH_DIRTY))
					continue;

				/*
				 * Checkpoints have to wait on all pinned pages,
				 * as pages may be marked dirty when returned to
				 * the cache.
				 *
				 * File syncs only wait on pages both pinned and
				 * dirty.  (We don't care if pages are marked
				 * dirty when returned to the cache, that means
				 * there's another writing thread and flushing
				 * the cache for this handle is meaningless.)
				 */
				if (op == DB_SYNC_FILE &&
				    !F_ISSET(bhp, BH_DIRTY))
					continue;

				mfp = R_ADDR(dbmp->reginfo, bhp->mf_offset);

				/*
unknown's avatar
unknown committed
285 286 287
				 * Ignore in-memory files, even if they are
				 * temp files to whom a backing file has been
				 * allocated.
unknown's avatar
unknown committed
288
				 */
unknown's avatar
unknown committed
289 290
				if (mfp->no_backing_file ||
				    F_ISSET(mfp, MP_TEMP))
unknown's avatar
unknown committed
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
					continue;

				/*
				 * If we're flushing a specific file, see if
				 * this page is from that file.
				 */
				if (dbmfp != NULL && mfp != dbmfp->mfp)
					continue;

				/*
				 * Ignore files that aren't involved in DB's
				 * transactional operations during checkpoints.
				 */
				if (dbmfp == NULL && mfp->lsn_off == -1)
					continue;

				/* Track the buffer, we want it. */
				bharray[ar_cnt].track_hp = hp;
				bharray[ar_cnt].track_pgno = bhp->pgno;
				bharray[ar_cnt].track_off = bhp->mf_offset;
				ar_cnt++;

unknown's avatar
unknown committed
313 314 315 316 317 318
				/*
				 * If we run out of space, double and continue.
				 * Don't stop at trickle_max, we want to sort
				 * as large a sample set as possible in order
				 * to minimize disk seeks.
				 */
unknown's avatar
unknown committed
319 320 321 322 323 324 325
				if (ar_cnt >= ar_max) {
					if ((ret = __os_realloc(dbenv,
					    (ar_max * 2) * sizeof(BH_TRACK),
					    &bharray)) != 0)
						break;
					ar_max *= 2;
				}
unknown's avatar
unknown committed
326
			}
unknown's avatar
unknown committed
327
			MUTEX_UNLOCK(dbenv, hp->mtx_hash);
unknown's avatar
unknown committed
328 329 330

			if (ret != 0)
				goto err;
unknown's avatar
unknown committed
331 332 333
		}
	}

unknown's avatar
unknown committed
334 335
	/* If there no buffers to write, we're done. */
	if (ar_cnt == 0)
unknown's avatar
unknown committed
336 337
		goto done;

unknown's avatar
unknown committed
338 339 340 341 342
	/*
	 * Write the buffers in file/page order, trying to reduce seeks by the
	 * filesystem and, when pages are smaller than filesystem block sizes,
	 * reduce the actual number of writes.
	 */
unknown's avatar
unknown committed
343
	if (ar_cnt > 1)
unknown's avatar
unknown committed
344
		qsort(bharray, ar_cnt, sizeof(BH_TRACK), __bhcmp);
unknown's avatar
unknown committed
345

unknown's avatar
unknown committed
346 347
	/*
	 * If we're trickling buffers, only write enough to reach the correct
unknown's avatar
unknown committed
348
	 * percentage.
unknown's avatar
unknown committed
349
	 */
unknown's avatar
unknown committed
350 351
	if (op == DB_SYNC_TRICKLE && ar_cnt > trickle_max)
		ar_cnt = trickle_max;
unknown's avatar
unknown committed
352 353 354 355 356 357 358 359 360

	/*
	 * Flush the log.  We have to ensure the log records reflecting the
	 * changes on the database pages we're writing have already made it
	 * to disk.  We still have to check the log each time we write a page
	 * (because pages we are about to write may be modified after we have
	 * flushed the log), but in general this will at least avoid any I/O
	 * on the log's part.
	 */
unknown's avatar
unknown committed
361
	if (LOGGING_ON(dbenv) && (ret = __log_flush(dbenv, NULL)) != 0)
unknown's avatar
unknown committed
362 363 364 365 366 367 368
		goto err;

	/*
	 * Walk the array, writing buffers.  When we write a buffer, we NULL
	 * out its hash bucket pointer so we don't process a slot more than
	 * once.
	 */
unknown's avatar
unknown committed
369
	for (i = pass = write_cnt = 0, remaining = ar_cnt; remaining > 0; ++i) {
unknown's avatar
unknown committed
370 371 372 373 374 375 376 377 378
		if (i >= ar_cnt) {
			i = 0;
			++pass;
			__os_sleep(dbenv, 1, 0);
		}
		if ((hp = bharray[i].track_hp) == NULL)
			continue;

		/* Lock the hash bucket and find the buffer. */
unknown's avatar
unknown committed
379 380
		mutex = hp->mtx_hash;
		MUTEX_LOCK(dbenv, mutex);
unknown's avatar
unknown committed
381 382 383 384 385
		for (bhp = SH_TAILQ_FIRST(&hp->hash_bucket, __bh);
		    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, hq, __bh))
			if (bhp->pgno == bharray[i].track_pgno &&
			    bhp->mf_offset == bharray[i].track_off)
				break;
unknown's avatar
unknown committed
386 387

		/*
unknown's avatar
unknown committed
388 389 390 391 392
		 * If we can't find the buffer we're done, somebody else had
		 * to have written it.
		 *
		 * If the buffer isn't pinned or dirty, we're done, there's
		 * no work needed.
unknown's avatar
unknown committed
393
		 */
unknown's avatar
unknown committed
394
		if (bhp == NULL || (bhp->ref == 0 && !F_ISSET(bhp, BH_DIRTY))) {
unknown's avatar
unknown committed
395
			MUTEX_UNLOCK(dbenv, mutex);
unknown's avatar
unknown committed
396 397
			--remaining;
			bharray[i].track_hp = NULL;
unknown's avatar
unknown committed
398 399 400
			continue;
		}

unknown's avatar
unknown committed
401 402 403 404 405 406 407 408 409 410 411 412
		/*
		 * If the buffer is locked by another thread, ignore it, we'll
		 * come back to it.
		 *
		 * If the buffer is pinned and it's only the first or second
		 * time we have looked at it, ignore it, we'll come back to
		 * it.
		 *
		 * In either case, skip the buffer if we're not required to
		 * write it.
		 */
		if (F_ISSET(bhp, BH_LOCKED) || (bhp->ref != 0 && pass < 2)) {
unknown's avatar
unknown committed
413
			MUTEX_UNLOCK(dbenv, mutex);
unknown's avatar
unknown committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427
			if (op != DB_SYNC_CACHE && op != DB_SYNC_FILE) {
				--remaining;
				bharray[i].track_hp = NULL;
			}
			continue;
		}

		/*
		 * The buffer is either pinned or dirty.
		 *
		 * Set the sync wait-for count, used to count down outstanding
		 * references to this buffer as they are returned to the cache.
		 */
		bhp->ref_sync = bhp->ref;
unknown's avatar
unknown committed
428

unknown's avatar
unknown committed
429 430 431
		/* Pin the buffer into memory and lock it. */
		++bhp->ref;
		F_SET(bhp, BH_LOCKED);
unknown's avatar
unknown committed
432
		MUTEX_LOCK(dbenv, bhp->mtx_bh);
unknown's avatar
unknown committed
433

unknown's avatar
unknown committed
434 435 436 437 438 439 440 441 442 443 444 445 446
		/*
		 * Unlock the hash bucket and wait for the wait-for count to
		 * go to 0.   No new thread can acquire the buffer because we
		 * have it locked.
		 *
		 * If a thread attempts to re-pin a page, the wait-for count
		 * will never go to 0 (the thread spins on our buffer lock,
		 * while we spin on the thread's ref count).  Give up if we
		 * don't get the buffer in 3 seconds, we can try again later.
		 *
		 * If, when the wait-for count goes to 0, the buffer is found
		 * to be dirty, write it.
		 */
unknown's avatar
unknown committed
447
		MUTEX_UNLOCK(dbenv, mutex);
unknown's avatar
unknown committed
448 449 450
		for (wait_cnt = 1;
		    bhp->ref_sync != 0 && wait_cnt < 4; ++wait_cnt)
			__os_sleep(dbenv, 1, 0);
unknown's avatar
unknown committed
451
		MUTEX_LOCK(dbenv, mutex);
unknown's avatar
unknown committed
452 453
		hb_lock = 1;

unknown's avatar
unknown committed
454 455 456 457 458 459 460 461 462 463 464 465 466 467
		/*
		 * If we've switched files, check to see if we're configured
		 * to close file descriptors.
		 */
		if (maxopenfd != 0 && bhp->mf_offset != last_mf_offset) {
			if (++filecnt >= maxopenfd) {
				filecnt = 0;
				if ((ret = __memp_close_flush_files(
				    dbenv, dbmp, 1)) != 0)
					break;
			}
			last_mf_offset = bhp->mf_offset;
		}

unknown's avatar
unknown committed
468 469 470 471 472 473 474
		/*
		 * If the ref_sync count has gone to 0, we're going to be done
		 * with this buffer no matter what happens.
		 */
		if (bhp->ref_sync == 0) {
			--remaining;
			bharray[i].track_hp = NULL;
unknown's avatar
unknown committed
475 476 477
		}

		/*
unknown's avatar
unknown committed
478 479 480 481 482
		 * If the ref_sync count has gone to 0 and the buffer is still
		 * dirty, we write it.  We only try to write the buffer once.
		 */
		if (bhp->ref_sync == 0 && F_ISSET(bhp, BH_DIRTY)) {
			hb_lock = 0;
unknown's avatar
unknown committed
483
			MUTEX_UNLOCK(dbenv, mutex);
unknown's avatar
unknown committed
484 485 486 487

			mfp = R_ADDR(dbmp->reginfo, bhp->mf_offset);
			if ((ret = __memp_bhwrite(dbmp, hp, mfp, bhp, 1)) == 0)
				++wrote;
unknown's avatar
unknown committed
488
			else
unknown's avatar
unknown committed
489 490
				__db_err(dbenv, "%s: unable to flush page: %lu",
				    __memp_fns(dbmp, mfp), (u_long)bhp->pgno);
unknown's avatar
unknown committed
491 492 493 494 495 496 497 498 499

			/*
			 * Avoid saturating the disk, sleep once we've done
			 * some number of writes.
			 */
			if (maxwrite != 0 && ++write_cnt >= maxwrite) {
				write_cnt = 0;
				__os_sleep(dbenv, 0, (u_long)maxwrite_sleep);
			}
unknown's avatar
unknown committed
500 501 502 503 504 505 506 507 508 509 510 511
		}

		/*
		 * If ref_sync count never went to 0, the buffer was written
		 * by another thread, or the write failed, we still have the
		 * buffer locked.
		 *
		 * We may or may not currently hold the hash bucket mutex.  If
		 * the __memp_bhwrite -> __memp_pgwrite call was successful,
		 * then __memp_pgwrite will have swapped the buffer lock for
		 * the hash lock.  All other call paths will leave us without
		 * the hash bucket lock.
unknown's avatar
unknown committed
512
		 *
unknown's avatar
unknown committed
513 514 515 516
		 * The order of mutexes above was to acquire the buffer lock
		 * while holding the hash bucket lock.  Don't deadlock here,
		 * release the buffer lock and then acquire the hash bucket
		 * lock.
unknown's avatar
unknown committed
517
		 */
unknown's avatar
unknown committed
518 519
		if (F_ISSET(bhp, BH_LOCKED)) {
			F_CLR(bhp, BH_LOCKED);
unknown's avatar
unknown committed
520
			MUTEX_UNLOCK(dbenv, bhp->mtx_bh);
unknown's avatar
unknown committed
521

unknown's avatar
unknown committed
522
			if (!hb_lock)
unknown's avatar
unknown committed
523
				MUTEX_LOCK(dbenv, mutex);
unknown's avatar
unknown committed
524 525
		}

unknown's avatar
unknown committed
526 527 528 529 530 531 532 533
		/*
		 * Reset the ref_sync count regardless of our success, we're
		 * done with this buffer for now.
		 */
		bhp->ref_sync = 0;

		/* Discard our reference and unlock the bucket. */
		--bhp->ref;
unknown's avatar
unknown committed
534
		MUTEX_UNLOCK(dbenv, mutex);
unknown's avatar
unknown committed
535

unknown's avatar
unknown committed
536 537 538 539
		if (ret != 0)
			break;
	}

unknown's avatar
unknown committed
540
done:	/*
unknown's avatar
unknown committed
541 542 543 544 545
	 * If doing a checkpoint or flushing a file for the application, we
	 * have to force the pages to disk.  We don't do this as we go along
	 * because we want to give the OS as much time as possible to lazily
	 * flush, and because we have to flush files that might not even have
	 * had dirty buffers in the cache, so we have to walk the files list.
unknown's avatar
unknown committed
546
	 */
unknown's avatar
unknown committed
547 548 549 550 551 552 553
	if (ret == 0 && (op == DB_SYNC_CACHE || op == DB_SYNC_FILE)) {
		if (dbmfp == NULL)
			ret = __memp_sync_files(dbenv, dbmp);
		else
			ret = __os_fsync(dbenv, dbmfp->fhp);
	}

unknown's avatar
unknown committed
554 555 556 557
	/* If we've opened files to flush pages, close them. */
	if ((t_ret = __memp_close_flush_files(dbenv, dbmp, 0)) != 0 && ret == 0)
		ret = t_ret;

unknown's avatar
unknown committed
558 559 560
err:	__os_free(dbenv, bharray);
	if (wrotep != NULL)
		*wrotep = wrote;
unknown's avatar
unknown committed
561 562 563 564 565

	return (ret);
}

/*
unknown's avatar
unknown committed
566 567
 * __memp_sync_files --
 *	Sync all the files in the environment, open or not.
unknown's avatar
unknown committed
568
 */
unknown's avatar
unknown committed
569 570
static
int __memp_sync_files(dbenv, dbmp)
unknown's avatar
unknown committed
571 572
	DB_ENV *dbenv;
	DB_MPOOL *dbmp;
unknown's avatar
unknown committed
573 574 575
{
	DB_MPOOLFILE *dbmfp;
	MPOOL *mp;
unknown's avatar
unknown committed
576 577
	MPOOLFILE *mfp, *next_mfp;
	int need_discard_pass, ret, t_ret;
unknown's avatar
unknown committed
578

unknown's avatar
unknown committed
579
	need_discard_pass = ret = 0;
unknown's avatar
unknown committed
580 581
	mp = dbmp->reginfo[0].primary;

unknown's avatar
unknown committed
582
	MPOOL_SYSTEM_LOCK(dbenv);
unknown's avatar
unknown committed
583 584
	for (mfp = SH_TAILQ_FIRST(&mp->mpfq, __mpoolfile);
	    mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile)) {
unknown's avatar
unknown committed
585
		if (!mfp->file_written || mfp->no_backing_file ||
unknown's avatar
unknown committed
586
		    mfp->deadfile || F_ISSET(mfp, MP_TEMP))
unknown's avatar
unknown committed
587
			continue;
unknown's avatar
unknown committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
		/*
		 * Pin the MPOOLFILE structure into memory, and release the
		 * region mutex allowing us to walk the linked list.  We'll
		 * re-acquire that mutex to move to the next entry in the list.
		 *
		 * This works because we only need to flush current entries,
		 * we don't care about new entries being added, and the linked
		 * list is never re-ordered, a single pass is sufficient.  It
		 * requires MPOOLFILE structures removed before we get to them
		 * be flushed to disk, but that's nothing new, they could have
		 * been removed while checkpoint was running, too.
		 *
		 * Once we have the MPOOLFILE lock, re-check the MPOOLFILE is
		 * not being discarded.  (A thread removing the MPOOLFILE
		 * will: hold the MPOOLFILE mutex, set deadfile, drop the
		 * MPOOLFILE mutex and then acquire the region MUTEX to walk
		 * the linked list and remove the MPOOLFILE structure.  Make
		 * sure the MPOOLFILE wasn't marked dead while we waited for
		 * the mutex.
		 */
		MUTEX_LOCK(dbenv, mfp->mutex);
		if (!mfp->file_written || mfp->deadfile) {
			MUTEX_UNLOCK(dbenv, mfp->mutex);
			continue;
		}
		MPOOL_SYSTEM_UNLOCK(dbenv);
		++mfp->mpf_cnt;
		MUTEX_UNLOCK(dbenv, mfp->mutex);
unknown's avatar
unknown committed
616

unknown's avatar
unknown committed
617 618 619 620
		/*
		 * Look for an already open, writeable handle (fsync doesn't
		 * work on read-only Windows handles).
		 */
unknown's avatar
unknown committed
621
		MUTEX_LOCK(dbenv, dbmp->mutex);
unknown's avatar
unknown committed
622
		for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
unknown's avatar
unknown committed
623 624 625
		    dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q)) {
			if (dbmfp->mfp != mfp || F_ISSET(dbmfp, MP_READONLY))
				continue;
unknown's avatar
unknown committed
626 627 628 629 630 631
			/*
			 * We don't want to hold the mutex while calling sync.
			 * Increment the DB_MPOOLFILE handle ref count to pin
			 * it into memory.
			 */
			++dbmfp->ref;
unknown's avatar
unknown committed
632 633
			break;
		}
unknown's avatar
unknown committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
		MUTEX_UNLOCK(dbenv, dbmp->mutex);

		/* If we don't find a handle we can use, open one. */
		if (dbmfp == NULL) {
			if ((t_ret = __memp_mf_sync(dbmp, mfp, 0)) != 0) {
				__db_err(dbenv,
				    "%s: unable to flush: %s", (char *)
				    R_ADDR(dbmp->reginfo, mfp->path_off),
				    db_strerror(t_ret));
				if (ret == 0)
					ret = t_ret;
			}
		} else {
			if ((t_ret =
			    __os_fsync(dbenv, dbmfp->fhp)) != 0 && ret == 0)
				ret = t_ret;
unknown's avatar
unknown committed
650

unknown's avatar
unknown committed
651 652
			if ((t_ret = __memp_fclose(dbmfp, 0)) != 0 && ret == 0)
				ret = t_ret;
unknown's avatar
unknown committed
653 654
		}

unknown's avatar
unknown committed
655 656 657 658 659 660 661 662 663 664 665
		/*
		 * Re-acquire the region lock, we need it to move to the next
		 * MPOOLFILE.
		 *
		 * Re-acquire the MPOOLFILE mutex, we need it to modify the
		 * reference count.
		 */
		MPOOL_SYSTEM_LOCK(dbenv);
		MUTEX_LOCK(dbenv, mfp->mutex);
		--mfp->mpf_cnt;

unknown's avatar
unknown committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
		/*
		 * If we wrote the file and there are no open handles (or there
		 * is a single open handle, and it's the one we opened to write
		 * buffers during checkpoint), clear the file_written flag.  We
		 * do this so that applications opening thousands of files don't
		 * loop here opening and flushing those files during checkpoint.
		 *
		 * The danger here is if a buffer were to be written as part of
		 * a checkpoint, and then not be flushed to disk.  This cannot
		 * happen because we only clear file_written when there are no
		 * other users of the MPOOLFILE in the system, and, as we hold
		 * the region lock, no possibility of another thread of control
		 * racing with us to open a MPOOLFILE.
		 */
		if (mfp->mpf_cnt == 0 || (mfp->mpf_cnt == 1 &&
unknown's avatar
unknown committed
681
		    dbmfp != NULL && F_ISSET(dbmfp, MP_FLUSH))) {
unknown's avatar
unknown committed
682
			mfp->file_written = 0;
unknown's avatar
unknown committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697

			/*
			 * We may be the last reference for a MPOOLFILE, as we
			 * weren't holding the MPOOLFILE mutex when flushing
			 * it's buffers to disk.  If we can discard it, set
			 * a flag to schedule a clean-out pass.   (Not likely,
			 * I mean, what are the chances that there aren't any
			 * buffers in the pool?  Regardless, it might happen.)
			 */
			if (mfp->mpf_cnt == 0 && mfp->block_cnt == 0)
				need_discard_pass = 1;
		}

		/* Unlock the MPOOLFILE, and move to the next entry. */
		MUTEX_UNLOCK(dbenv, mfp->mutex);
unknown's avatar
unknown committed
698 699
	}

unknown's avatar
unknown committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
	/*
	 * We exit the loop holding the region lock.
	 *
	 * We may need to do a last pass through the MPOOLFILE list -- if we
	 * were the last reference to an MPOOLFILE, we need to clean it out.
	 */
	if (need_discard_pass)
		for (mfp = SH_TAILQ_FIRST(
		    &mp->mpfq, __mpoolfile); mfp != NULL; mfp = next_mfp) {
			next_mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile);

			/*
			 * Do a fast check -- we can check for zero/non-zero
			 * without a mutex on the MPOOLFILE.  If likely to
			 * succeed, lock the MPOOLFILE down and look for real.
			 */
			if (mfp->block_cnt != 0 || mfp->mpf_cnt != 0)
				continue;

			MUTEX_LOCK(dbenv, mfp->mutex);
			if (mfp->block_cnt == 0 && mfp->mpf_cnt == 0)
				(void)__memp_mf_discard(dbmp, mfp);
			else
				MUTEX_UNLOCK(dbenv, mfp->mutex);
		}
	MPOOL_SYSTEM_UNLOCK(dbenv);

	return (ret);
unknown's avatar
unknown committed
728 729 730 731
}

/*
 * __memp_mf_sync --
unknown's avatar
unknown committed
732
 *	Flush an MPOOLFILE, when no currently open handle is available.
unknown's avatar
unknown committed
733
 *
unknown's avatar
unknown committed
734
 * PUBLIC: int __memp_mf_sync __P((DB_MPOOL *, MPOOLFILE *, int));
unknown's avatar
unknown committed
735 736
 */
int
unknown's avatar
unknown committed
737
__memp_mf_sync(dbmp, mfp, region_locked)
unknown's avatar
unknown committed
738 739
	DB_MPOOL *dbmp;
	MPOOLFILE *mfp;
unknown's avatar
unknown committed
740
	int region_locked;
unknown's avatar
unknown committed
741 742 743 744 745 746 747 748 749
{
	DB_ENV *dbenv;
	DB_FH *fhp;
	int ret, t_ret;
	char *rpath;

	dbenv = dbmp->dbenv;

	/*
unknown's avatar
unknown committed
750 751
	 * We need to be holding the region lock: we're using the path name
	 * and __memp_nameop might try and rename the file.
unknown's avatar
unknown committed
752
	 */
unknown's avatar
unknown committed
753 754 755
	if (!region_locked)
		MPOOL_SYSTEM_LOCK(dbenv);

unknown's avatar
unknown committed
756
	if ((ret = __db_appname(dbenv, DB_APP_DATA,
unknown's avatar
unknown committed
757
	    R_ADDR(dbmp->reginfo, mfp->path_off), 0, NULL, &rpath)) == 0) {
unknown's avatar
unknown committed
758 759 760 761 762 763 764 765 766
		if ((ret = __os_open(dbenv, rpath, 0, 0, &fhp)) == 0) {
			ret = __os_fsync(dbenv, fhp);
			if ((t_ret =
			    __os_closehandle(dbenv, fhp)) != 0 && ret == 0)
				ret = t_ret;
		}
		__os_free(dbenv, rpath);
	}

unknown's avatar
unknown committed
767 768 769
	if (!region_locked)
		MPOOL_SYSTEM_UNLOCK(dbenv);

unknown's avatar
unknown committed
770 771 772 773 774 775 776 777
	return (ret);
}

/*
 * __memp_close_flush_files --
 *	Close files opened only to flush buffers.
 */
static int
unknown's avatar
unknown committed
778
__memp_close_flush_files(dbenv, dbmp, dosync)
unknown's avatar
unknown committed
779 780
	DB_ENV *dbenv;
	DB_MPOOL *dbmp;
unknown's avatar
unknown committed
781
	int dosync;
unknown's avatar
unknown committed
782 783
{
	DB_MPOOLFILE *dbmfp;
unknown's avatar
unknown committed
784
	MPOOLFILE *mfp;
unknown's avatar
unknown committed
785
	int ret;
unknown's avatar
unknown committed
786 787

	/*
unknown's avatar
unknown committed
788 789 790 791
	 * The routine exists because we must close files opened by sync to
	 * flush buffers.  There are two cases: first, extent files have to
	 * be closed so they may be removed when empty.  Second, regular
	 * files have to be closed so we don't run out of descriptors (for
unknown's avatar
unknown committed
792
	 * example, an application partitioning its data into databases
unknown's avatar
unknown committed
793 794 795 796 797 798
	 * based on timestamps, so there's a continually increasing set of
	 * files).
	 *
	 * We mark files opened in the __memp_bhwrite() function with the
	 * MP_FLUSH flag.  Here we walk through our file descriptor list,
	 * and, if a file was opened by __memp_bhwrite(), we close it.
unknown's avatar
unknown committed
799
	 */
unknown's avatar
unknown committed
800
retry:	MUTEX_LOCK(dbenv, dbmp->mutex);
unknown's avatar
unknown committed
801 802 803 804
	for (dbmfp = TAILQ_FIRST(&dbmp->dbmfq);
	    dbmfp != NULL; dbmfp = TAILQ_NEXT(dbmfp, q))
		if (F_ISSET(dbmfp, MP_FLUSH)) {
			F_CLR(dbmfp, MP_FLUSH);
unknown's avatar
unknown committed
805
			MUTEX_UNLOCK(dbenv, dbmp->mutex);
unknown's avatar
unknown committed
806 807
			if (dosync) {
				/*
unknown's avatar
unknown committed
808 809 810 811 812
				 * If we have the only open handle on the file,
				 * clear the dirty flag so we don't re-open and
				 * sync it again when discarding the MPOOLFILE
				 * structure.  Clear the flag before the sync
				 * so can't race with a thread writing the file.
unknown's avatar
unknown committed
813 814 815
				 */
				mfp = dbmfp->mfp;
				if (mfp->mpf_cnt == 1) {
unknown's avatar
unknown committed
816
					MUTEX_LOCK(dbenv, mfp->mutex);
unknown's avatar
unknown committed
817 818
					if (mfp->mpf_cnt == 1)
						mfp->file_written = 0;
unknown's avatar
unknown committed
819
					MUTEX_UNLOCK(dbenv, mfp->mutex);
unknown's avatar
unknown committed
820
				}
unknown's avatar
unknown committed
821 822
				if ((ret = __os_fsync(dbenv, dbmfp->fhp)) != 0)
					return (ret);
unknown's avatar
unknown committed
823 824
			}
			if ((ret = __memp_fclose(dbmfp, 0)) != 0)
unknown's avatar
unknown committed
825 826 827
				return (ret);
			goto retry;
		}
unknown's avatar
unknown committed
828
	MUTEX_UNLOCK(dbenv, dbmp->mutex);
unknown's avatar
unknown committed
829 830 831 832 833 834 835 836

	return (0);
}

static int
__bhcmp(p1, p2)
	const void *p1, *p2;
{
unknown's avatar
unknown committed
837
	BH_TRACK *bhp1, *bhp2;
unknown's avatar
unknown committed
838

unknown's avatar
unknown committed
839 840
	bhp1 = (BH_TRACK *)p1;
	bhp2 = (BH_TRACK *)p2;
unknown's avatar
unknown committed
841 842

	/* Sort by file (shared memory pool offset). */
unknown's avatar
unknown committed
843
	if (bhp1->track_off < bhp2->track_off)
unknown's avatar
unknown committed
844
		return (-1);
unknown's avatar
unknown committed
845
	if (bhp1->track_off > bhp2->track_off)
unknown's avatar
unknown committed
846 847 848 849 850 851 852
		return (1);

	/*
	 * !!!
	 * Defend against badly written quicksort code calling the comparison
	 * function with two identical pointers (e.g., WATCOM C++ (Power++)).
	 */
unknown's avatar
unknown committed
853
	if (bhp1->track_pgno < bhp2->track_pgno)
unknown's avatar
unknown committed
854
		return (-1);
unknown's avatar
unknown committed
855
	if (bhp1->track_pgno > bhp2->track_pgno)
unknown's avatar
unknown committed
856 857 858
		return (1);
	return (0);
}