F_CLR(bhp, BH_WRITE);
if (F_ISSET(bhp, BH_WRITE)) F_CLR(bhp, BH_WRITE);
memp_sync(dbmp, lsnp)	DB_MPOOL *dbmp;	DB_LSN *lsnp;{	BH *bhp, **bharray;	DB_ENV *dbenv;	MPOOL *mp;	MPOOLFILE *mfp;	int ar_cnt, cnt, nalloc, next, ret, wrote;	dbenv = dbmp->dbenv;	if (dbenv->lg_info == NULL) {		__db_err(dbenv, "memp_sync: requires logging");		return (EINVAL);	}	/*	 * We try and write the buffers in page order so that the underlying	 * filesystem doesn't have to seek and can write contiguous blocks,	 * plus, we don't want to hold the region lock while we write the	 * buffers.  Get memory to hold the buffer pointers.  Get a good-size	 * block, too, because we realloc while holding the region lock if we	 * run out.	 */	if ((bharray =	    (BH **)__db_malloc((nalloc = 1024) * sizeof(BH *))) == NULL)		return (ENOMEM);	LOCKREGION(dbmp);	/*	 * If the application is asking about a previous call to memp_sync(),	 * and we haven't found any buffers that the application holding the	 * pin couldn't write, return yes or no based on the current count.	 * Note, if the application is asking about a LSN *smaller* than one	 * we've already handled or are currently handling, then we return a	 * result based on the count for the larger LSN.	 */	mp = dbmp->mp;	if (!F_ISSET(mp, MP_LSN_RETRY) && log_compare(lsnp, &mp->lsn) <= 0) {		if (mp->lsn_cnt == 0) {			*lsnp = mp->lsn;			ret = 0;		} else			ret = DB_INCOMPLETE;		goto done;	}	/* Else, it's a new checkpoint. */	F_CLR(mp, MP_LSN_RETRY);	/*	 * Save the LSN.  We know that it's a new LSN or larger than the one	 * for which we were already doing a checkpoint.  (BTW, I don't expect	 * to see multiple LSN's from the same or multiple processes, but You	 * Just Never Know.  Responding as if they all called with the largest	 * of the LSNs specified makes everything work.)	 *	 * We don't currently use the LSN we save.  We could potentially save	 * the last-written LSN in each buffer header and use it to determine	 * what buffers need to be written.  The problem with this is that it's	 * sizeof(LSN) more bytes of buffer header.  We currently write all the	 * dirty buffers instead.	 *	 * Walk the list of shared memory segments clearing the count of	 * buffers waiting to be written.	 */	mp->lsn = *lsnp;	mp->lsn_cnt = 0;	for (mfp = SH_TAILQ_FIRST(&dbmp->mp->mpfq, __mpoolfile);	    mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile))		mfp->lsn_cnt = 0;	/*	 * Walk the list of buffers and mark all dirty buffers to be written	 * and all pinned buffers to be potentially written (we can't know if	 * we'll need to write them until the holding process returns them to	 * the cache).  We do this in one pass while holding the region locked	 * so that processes can't make new buffers dirty, causing us to never	 * finish.  Since the application may have restarted the sync, clear	 * any BH_WRITE flags that appear to be left over from previous calls.	 *	 * Keep a count of the total number of buffers we need to write in	 * MPOOL->lsn_cnt, and for each file, in MPOOLFILE->lsn_count.	 */	ar_cnt = 0;	for (bhp = SH_TAILQ_FIRST(&mp->bhq, __bh);	    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, q, __bh))		if (F_ISSET(bhp, BH_DIRTY) || bhp->ref != 0) {			F_SET(bhp, BH_WRITE);			++mp->lsn_cnt;			mfp = R_ADDR(dbmp, bhp->mf_offset);			++mfp->lsn_cnt;			/*			 * If the buffer isn't in use, we should be able to			 * write it immediately, so save a reference to it.			 */			if (bhp->ref == 0) {				if (ar_cnt == nalloc) {					nalloc *= 2;					if ((bharray =					    (BH **)__db_realloc(bharray,					    nalloc * sizeof(BH *))) == NULL) {						ret = ENOMEM;						goto err;					}				}				bharray[ar_cnt++] = bhp;			}		} else			F_CLR(bhp, BH_WRITE);	/* If there no buffers we can write immediately, we're done. */	if (ar_cnt == 0) {		ret = mp->lsn_cnt ? DB_INCOMPLETE : 0;		goto done;	}	/* Lock down the buffers and their contents. */	for (cnt = 0; cnt < ar_cnt; ++cnt)		++bharray[cnt]->ref;	UNLOCKREGION(dbmp);	/* Sort the buffers we're going to write. */	qsort(bharray, ar_cnt, sizeof(BH *), __bhcmp);	LOCKREGION(dbmp);	/* Walk the array, writing buffers. */	for (next = 0; next < ar_cnt; ++next) {		/*		 * It's possible for a thread to have gotten the buffer since		 * we listed it for writing.  If the reference count is still		 * 1, we're the only ones using the buffer, go ahead and write.		 * If it's >1, then skip the buffer and assume that it will be		 * written when it's returned to the cache.		 */		if (bharray[next]->ref > 1) {			--bharray[next]->ref;			continue;		}		/* Write the buffer. */		mfp = R_ADDR(dbmp, bharray[next]->mf_offset);		ret = __memp_bhwrite(dbmp, mfp, bharray[next], NULL, &wrote);		/* Release the buffer. */		--bharray[next]->ref;		/* If there's an error, release the rest of the buffers. */		if (ret != 0 || !wrote) {			/*			 * Any process syncing the shared memory buffer pool			 * had better be able to write to any underlying file.			 * Be understanding, but firm, on this point.			 */			if (ret == 0) {				__db_err(dbenv, "%s: unable to flush page: %lu",				    __memp_fns(dbmp, mfp),				    (u_long)bharray[next]->pgno);				ret = EPERM;			}			while (++next < ar_cnt)				--bharray[next]->ref;			goto err;		}	}	ret = mp->lsn_cnt ? DB_INCOMPLETE : 0;done:	if (0) {err:		/*		 * On error, clear:		 *	MPOOL->lsn_cnt (the total sync count)		 *	MPOOLFILE->lsn_cnt (the per-file sync count)		 *	BH_WRITE flag (the scheduled for writing flag)		 */		mp->lsn_cnt = 0;		for (mfp = SH_TAILQ_FIRST(&dbmp->mp->mpfq, __mpoolfile);		    mfp != NULL; mfp = SH_TAILQ_NEXT(mfp, q, __mpoolfile))			mfp->lsn_cnt = 0;		for (bhp = SH_TAILQ_FIRST(&mp->bhq, __bh);		    bhp != NULL; bhp = SH_TAILQ_NEXT(bhp, q, __bh))			F_CLR(bhp, BH_WRITE);	}	UNLOCKREGION(dbmp);	__db_free(bharray);	return (ret);}