17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
580d34432Sfrankho  * Common Development and Distribution License (the "License").
680d34432Sfrankho  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*d3d50737SRafael Vanoni  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/systm.h>
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
297c478bd9Sstevel@tonic-gate #include <sys/errno.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/debug.h>
327c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/proc.h>
357c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
367c478bd9Sstevel@tonic-gate #include <sys/fssnap_if.h>
377c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
397c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
407c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
417c478bd9Sstevel@tonic-gate #include <sys/inttypes.h>
427c478bd9Sstevel@tonic-gate #include <sys/callb.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Kernel threads for logging
467c478bd9Sstevel@tonic-gate  * Currently only one for rolling the log (one per log).
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #define	LUFS_DEFAULT_NUM_ROLL_BUFS 16
507c478bd9Sstevel@tonic-gate #define	LUFS_DEFAULT_MIN_ROLL_BUFS 4
517c478bd9Sstevel@tonic-gate #define	LUFS_DEFAULT_MAX_ROLL_BUFS 64
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * Macros
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate #define	logmap_need_roll(logmap) ((logmap)->mtm_nme > logmap_maxnme)
577c478bd9Sstevel@tonic-gate #define	ldl_empty(ul) ((ul)->un_head_lof == (ul)->un_tail_lof)
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * Tunables
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate uint32_t lufs_num_roll_bufs = LUFS_DEFAULT_NUM_ROLL_BUFS;
637c478bd9Sstevel@tonic-gate uint32_t lufs_min_roll_bufs = LUFS_DEFAULT_MIN_ROLL_BUFS;
647c478bd9Sstevel@tonic-gate uint32_t lufs_max_roll_bufs = LUFS_DEFAULT_MAX_ROLL_BUFS;
657c478bd9Sstevel@tonic-gate long logmap_maxnme = 1536;
667c478bd9Sstevel@tonic-gate int trans_roll_tics = 0;
677c478bd9Sstevel@tonic-gate uint64_t trans_roll_new_delta = 0;
687c478bd9Sstevel@tonic-gate uint64_t lrr_wait = 0;
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Key for thread specific data for the roll thread to
717c478bd9Sstevel@tonic-gate  * bypass snapshot throttling
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate uint_t bypass_snapshot_throttle_key;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * externs
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate extern kmutex_t		ml_scan;
797c478bd9Sstevel@tonic-gate extern kcondvar_t	ml_scan_cv;
807c478bd9Sstevel@tonic-gate extern int		maxphys;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static void
trans_roll_wait(mt_map_t * logmap,callb_cpr_t * cprinfop)837c478bd9Sstevel@tonic-gate trans_roll_wait(mt_map_t *logmap, callb_cpr_t *cprinfop)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	mutex_enter(&logmap->mtm_mutex);
867c478bd9Sstevel@tonic-gate 	logmap->mtm_ref = 0;
877c478bd9Sstevel@tonic-gate 	if (logmap->mtm_flags & MTM_FORCE_ROLL) {
887c478bd9Sstevel@tonic-gate 		cv_broadcast(&logmap->mtm_from_roll_cv);
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 	logmap->mtm_flags &= ~(MTM_FORCE_ROLL | MTM_ROLLING);
917c478bd9Sstevel@tonic-gate 	CALLB_CPR_SAFE_BEGIN(cprinfop);
92*d3d50737SRafael Vanoni 	(void) cv_reltimedwait(&logmap->mtm_to_roll_cv, &logmap->mtm_mutex,
93*d3d50737SRafael Vanoni 	    trans_roll_tics, TR_CLOCK_TICK);
947c478bd9Sstevel@tonic-gate 	CALLB_CPR_SAFE_END(cprinfop, &logmap->mtm_mutex);
957c478bd9Sstevel@tonic-gate 	logmap->mtm_flags |= MTM_ROLLING;
967c478bd9Sstevel@tonic-gate 	mutex_exit(&logmap->mtm_mutex);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * returns the number of 8K buffers to use for rolling the log
1017c478bd9Sstevel@tonic-gate  */
1027c478bd9Sstevel@tonic-gate static uint32_t
log_roll_buffers()1037c478bd9Sstevel@tonic-gate log_roll_buffers()
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	/*
1067c478bd9Sstevel@tonic-gate 	 * sanity validate the tunable lufs_num_roll_bufs
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	if (lufs_num_roll_bufs < lufs_min_roll_bufs) {
1097c478bd9Sstevel@tonic-gate 		return (lufs_min_roll_bufs);
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	if (lufs_num_roll_bufs > lufs_max_roll_bufs) {
1127c478bd9Sstevel@tonic-gate 		return (lufs_max_roll_bufs);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 	return (lufs_num_roll_bufs);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /*
1187c478bd9Sstevel@tonic-gate  * Find something to roll, then if we don't have cached roll buffers
1197c478bd9Sstevel@tonic-gate  * covering all the deltas in that MAPBLOCK then read the master
1207c478bd9Sstevel@tonic-gate  * and overlay the deltas.
1217c478bd9Sstevel@tonic-gate  * returns;
1227c478bd9Sstevel@tonic-gate  * 	0 if sucessful
1237c478bd9Sstevel@tonic-gate  *	1 on finding nothing to roll
1247c478bd9Sstevel@tonic-gate  *	2 on error
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate int
log_roll_read(ml_unit_t * ul,rollbuf_t * rbs,int nmblk,caddr_t roll_bufs,int * retnbuf)1277c478bd9Sstevel@tonic-gate log_roll_read(ml_unit_t *ul, rollbuf_t *rbs, int nmblk, caddr_t roll_bufs,
1287c478bd9Sstevel@tonic-gate     int *retnbuf)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	offset_t	mof;
1317c478bd9Sstevel@tonic-gate 	buf_t		*bp;
1327c478bd9Sstevel@tonic-gate 	rollbuf_t	*rbp;
1337c478bd9Sstevel@tonic-gate 	mt_map_t	*logmap = ul->un_logmap;
1347c478bd9Sstevel@tonic-gate 	daddr_t		mblkno;
1357c478bd9Sstevel@tonic-gate 	int		i;
1367c478bd9Sstevel@tonic-gate 	int		error;
1377c478bd9Sstevel@tonic-gate 	int		nbuf;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * Make sure there is really something to roll
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	mof = 0;
1437c478bd9Sstevel@tonic-gate 	if (!logmap_next_roll(logmap, &mof)) {
1447c478bd9Sstevel@tonic-gate 		return (1);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	/*
1487c478bd9Sstevel@tonic-gate 	 * build some master blocks + deltas to roll forward
1497c478bd9Sstevel@tonic-gate 	 */
1507c478bd9Sstevel@tonic-gate 	rw_enter(&logmap->mtm_rwlock, RW_READER);
1517c478bd9Sstevel@tonic-gate 	nbuf = 0;
1527c478bd9Sstevel@tonic-gate 	do {
1537c478bd9Sstevel@tonic-gate 		mof = mof & (offset_t)MAPBLOCKMASK;
1547c478bd9Sstevel@tonic-gate 		mblkno = lbtodb(mof);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		/*
1577c478bd9Sstevel@tonic-gate 		 * Check for the case of a new delta to a set up buffer
1587c478bd9Sstevel@tonic-gate 		 */
1597c478bd9Sstevel@tonic-gate 		for (i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
1607c478bd9Sstevel@tonic-gate 			if (P2ALIGN(rbp->rb_bh.b_blkno,
1617c478bd9Sstevel@tonic-gate 			    MAPBLOCKSIZE / DEV_BSIZE) == mblkno) {
1627c478bd9Sstevel@tonic-gate 				trans_roll_new_delta++;
1637c478bd9Sstevel@tonic-gate 				/* Flush out the current set of buffers */
1647c478bd9Sstevel@tonic-gate 				goto flush_bufs;
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 		/*
1697c478bd9Sstevel@tonic-gate 		 * Work out what to roll next. If it isn't cached then read
1707c478bd9Sstevel@tonic-gate 		 * it asynchronously from the master.
1717c478bd9Sstevel@tonic-gate 		 */
1727c478bd9Sstevel@tonic-gate 		bp = &rbp->rb_bh;
1737c478bd9Sstevel@tonic-gate 		bp->b_blkno = mblkno;
1747c478bd9Sstevel@tonic-gate 		bp->b_flags = B_READ;
1757c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = roll_bufs + (nbuf << MAPBLOCKSHIFT);
1767c478bd9Sstevel@tonic-gate 		bp->b_bufsize = MAPBLOCKSIZE;
1777c478bd9Sstevel@tonic-gate 		if (top_read_roll(rbp, ul)) {
1787c478bd9Sstevel@tonic-gate 			/* logmap deltas were in use */
1797c478bd9Sstevel@tonic-gate 			if (nbuf == 0) {
1807c478bd9Sstevel@tonic-gate 				/*
1817c478bd9Sstevel@tonic-gate 				 * On first buffer wait for the logmap user
1827c478bd9Sstevel@tonic-gate 				 * to finish by grabbing the logmap lock
1837c478bd9Sstevel@tonic-gate 				 * exclusively rather than spinning
1847c478bd9Sstevel@tonic-gate 				 */
1857c478bd9Sstevel@tonic-gate 				rw_exit(&logmap->mtm_rwlock);
1867c478bd9Sstevel@tonic-gate 				lrr_wait++;
1877c478bd9Sstevel@tonic-gate 				rw_enter(&logmap->mtm_rwlock, RW_WRITER);
1887c478bd9Sstevel@tonic-gate 				rw_exit(&logmap->mtm_rwlock);
1897c478bd9Sstevel@tonic-gate 				return (1);
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 			/* we have at least one buffer - flush it */
1927c478bd9Sstevel@tonic-gate 			goto flush_bufs;
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 		if ((bp->b_flags & B_INVAL) == 0) {
1957c478bd9Sstevel@tonic-gate 			nbuf++;
1967c478bd9Sstevel@tonic-gate 		}
1977c478bd9Sstevel@tonic-gate 		mof += MAPBLOCKSIZE;
1987c478bd9Sstevel@tonic-gate 	} while ((nbuf < nmblk) && logmap_next_roll(logmap, &mof));
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/*
2017c478bd9Sstevel@tonic-gate 	 * If there was nothing to roll cycle back
2027c478bd9Sstevel@tonic-gate 	 */
2037c478bd9Sstevel@tonic-gate 	if (nbuf == 0) {
2047c478bd9Sstevel@tonic-gate 		rw_exit(&logmap->mtm_rwlock);
2057c478bd9Sstevel@tonic-gate 		return (1);
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate flush_bufs:
2097c478bd9Sstevel@tonic-gate 	/*
2107c478bd9Sstevel@tonic-gate 	 * For each buffer, if it isn't cached then wait for the read to
2117c478bd9Sstevel@tonic-gate 	 * finish and overlay the deltas.
2127c478bd9Sstevel@tonic-gate 	 */
2137c478bd9Sstevel@tonic-gate 	for (error = 0, i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
2147c478bd9Sstevel@tonic-gate 		if (!rbp->rb_crb) {
2157c478bd9Sstevel@tonic-gate 			bp = &rbp->rb_bh;
2167c478bd9Sstevel@tonic-gate 			if (trans_not_wait(bp)) {
2177c478bd9Sstevel@tonic-gate 				ldl_seterror(ul,
2187c478bd9Sstevel@tonic-gate 				    "Error reading master during ufs log roll");
2197c478bd9Sstevel@tonic-gate 				error = 1;
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 			/*
2227c478bd9Sstevel@tonic-gate 			 * sync read the data from the log
2237c478bd9Sstevel@tonic-gate 			 */
2247c478bd9Sstevel@tonic-gate 			if (ldl_read(ul, bp->b_un.b_addr,
2257c478bd9Sstevel@tonic-gate 			    ldbtob(bp->b_blkno) & (offset_t)MAPBLOCKMASK,
2267c478bd9Sstevel@tonic-gate 			    MAPBLOCKSIZE, rbp->rb_age)) {
2277c478bd9Sstevel@tonic-gate 				error = 1;
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		/*
2327c478bd9Sstevel@tonic-gate 		 * reset the age bit in the age list
2337c478bd9Sstevel@tonic-gate 		 */
2347c478bd9Sstevel@tonic-gate 		logmap_list_put_roll(logmap, rbp->rb_age);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
2377c478bd9Sstevel@tonic-gate 			error = 1;
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	rw_exit(&logmap->mtm_rwlock);
2417c478bd9Sstevel@tonic-gate 	if (error)
2427c478bd9Sstevel@tonic-gate 		return (2);
2437c478bd9Sstevel@tonic-gate 	*retnbuf = nbuf;
2447c478bd9Sstevel@tonic-gate 	return (0);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * Write out a cached roll buffer
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate void
log_roll_write_crb(ufsvfs_t * ufsvfsp,rollbuf_t * rbp)2517c478bd9Sstevel@tonic-gate log_roll_write_crb(ufsvfs_t *ufsvfsp, rollbuf_t *rbp)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	crb_t *crb = rbp->rb_crb;
2547c478bd9Sstevel@tonic-gate 	buf_t *bp = &rbp->rb_bh;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	bp->b_blkno = lbtodb(crb->c_mof);
2577c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = crb->c_buf;
2587c478bd9Sstevel@tonic-gate 	bp->b_bcount = crb->c_nb;
2597c478bd9Sstevel@tonic-gate 	bp->b_bufsize = crb->c_nb;
2607c478bd9Sstevel@tonic-gate 	ASSERT((crb->c_nb & DEV_BMASK) == 0);
2617c478bd9Sstevel@tonic-gate 	bp->b_flags = B_WRITE;
2627c478bd9Sstevel@tonic-gate 	logstats.ls_rwrites.value.ui64++;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/* if snapshots are enabled, call it */
2657c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_snapshot) {
2667c478bd9Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		(void) bdev_strategy(bp);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  * Write out a set of non cached roll buffers
2747c478bd9Sstevel@tonic-gate  */
2757c478bd9Sstevel@tonic-gate void
log_roll_write_bufs(ufsvfs_t * ufsvfsp,rollbuf_t * rbp)2767c478bd9Sstevel@tonic-gate log_roll_write_bufs(ufsvfs_t *ufsvfsp, rollbuf_t *rbp)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	buf_t		*bp = &rbp->rb_bh;
2797c478bd9Sstevel@tonic-gate 	buf_t		*bp2;
2807c478bd9Sstevel@tonic-gate 	rbsecmap_t	secmap = rbp->rb_secmap;
2817c478bd9Sstevel@tonic-gate 	int		j, k;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	ASSERT(secmap);
2847c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_flags & B_INVAL) == 0);
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	do { /* for each contiguous block of sectors */
2877c478bd9Sstevel@tonic-gate 		/* find start of next sector to write */
2887c478bd9Sstevel@tonic-gate 		for (j = 0; j < 16; ++j) {
2897c478bd9Sstevel@tonic-gate 			if (secmap & UINT16_C(1))
2907c478bd9Sstevel@tonic-gate 				break;
2917c478bd9Sstevel@tonic-gate 			secmap >>= 1;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr += (j << DEV_BSHIFT);
2947c478bd9Sstevel@tonic-gate 		bp->b_blkno += j;
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 		/* calculate number of sectors */
2977c478bd9Sstevel@tonic-gate 		secmap >>= 1;
2987c478bd9Sstevel@tonic-gate 		j++;
2997c478bd9Sstevel@tonic-gate 		for (k = 1; j < 16; ++j) {
3007c478bd9Sstevel@tonic-gate 			if ((secmap & UINT16_C(1)) == 0)
3017c478bd9Sstevel@tonic-gate 				break;
3027c478bd9Sstevel@tonic-gate 			secmap >>= 1;
3037c478bd9Sstevel@tonic-gate 			k++;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 		bp->b_bcount = k << DEV_BSHIFT;
3067c478bd9Sstevel@tonic-gate 		bp->b_flags = B_WRITE;
3077c478bd9Sstevel@tonic-gate 		logstats.ls_rwrites.value.ui64++;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		/* if snapshots are enabled, call it */
3107c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_snapshot)
3117c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
3127c478bd9Sstevel@tonic-gate 		else
3137c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
3147c478bd9Sstevel@tonic-gate 		if (secmap) {
3157c478bd9Sstevel@tonic-gate 			/*
3167c478bd9Sstevel@tonic-gate 			 * Allocate another buf_t to handle
3177c478bd9Sstevel@tonic-gate 			 * the next write in this MAPBLOCK
3187c478bd9Sstevel@tonic-gate 			 * Chain them via b_list.
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			bp2 = kmem_alloc(sizeof (buf_t), KM_SLEEP);
3217c478bd9Sstevel@tonic-gate 			bp->b_list = bp2;
3227c478bd9Sstevel@tonic-gate 			bioinit(bp2);
3237c478bd9Sstevel@tonic-gate 			bp2->b_iodone = trans_not_done;
3247c478bd9Sstevel@tonic-gate 			bp2->b_bufsize = MAPBLOCKSIZE;
3257c478bd9Sstevel@tonic-gate 			bp2->b_edev = bp->b_edev;
3267c478bd9Sstevel@tonic-gate 			bp2->b_un.b_addr =
3277c478bd9Sstevel@tonic-gate 			    bp->b_un.b_addr + bp->b_bcount;
3287c478bd9Sstevel@tonic-gate 			bp2->b_blkno = bp->b_blkno + k;
3297c478bd9Sstevel@tonic-gate 			bp = bp2;
3307c478bd9Sstevel@tonic-gate 		}
3317c478bd9Sstevel@tonic-gate 	} while (secmap);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * Asynchronously roll the deltas, using the sector map
3367c478bd9Sstevel@tonic-gate  * in each rollbuf_t.
3377c478bd9Sstevel@tonic-gate  */
3387c478bd9Sstevel@tonic-gate int
log_roll_write(ml_unit_t * ul,rollbuf_t * rbs,int nbuf)3397c478bd9Sstevel@tonic-gate log_roll_write(ml_unit_t *ul, rollbuf_t *rbs, int nbuf)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ul->un_ufsvfs;
3437c478bd9Sstevel@tonic-gate 	rollbuf_t	*rbp;
3447c478bd9Sstevel@tonic-gate 	buf_t		*bp, *bp2;
3457c478bd9Sstevel@tonic-gate 	rollbuf_t	*head, *prev, *rbp2;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * Order the buffers by blkno
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 	ASSERT(nbuf > 0);
3517c478bd9Sstevel@tonic-gate #ifdef lint
3527c478bd9Sstevel@tonic-gate 	prev = rbs;
3537c478bd9Sstevel@tonic-gate #endif
3547c478bd9Sstevel@tonic-gate 	for (head = rbs, rbp = rbs + 1; rbp < rbs + nbuf; rbp++) {
3557c478bd9Sstevel@tonic-gate 		for (rbp2 = head; rbp2; prev = rbp2, rbp2 = rbp2->rb_next) {
3567c478bd9Sstevel@tonic-gate 			if (rbp->rb_bh.b_blkno < rbp2->rb_bh.b_blkno) {
3577c478bd9Sstevel@tonic-gate 				if (rbp2 == head) {
3587c478bd9Sstevel@tonic-gate 					rbp->rb_next = head;
3597c478bd9Sstevel@tonic-gate 					head = rbp;
3607c478bd9Sstevel@tonic-gate 				} else {
3617c478bd9Sstevel@tonic-gate 					prev->rb_next = rbp;
3627c478bd9Sstevel@tonic-gate 					rbp->rb_next = rbp2;
3637c478bd9Sstevel@tonic-gate 				}
3647c478bd9Sstevel@tonic-gate 				break;
3657c478bd9Sstevel@tonic-gate 			}
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		if (rbp2 == NULL) {
3687c478bd9Sstevel@tonic-gate 			prev->rb_next = rbp;
3697c478bd9Sstevel@tonic-gate 			rbp->rb_next = NULL;
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * issue the in-order writes
3757c478bd9Sstevel@tonic-gate 	 */
3767c478bd9Sstevel@tonic-gate 	for (rbp = head; rbp; rbp = rbp2) {
3777c478bd9Sstevel@tonic-gate 		if (rbp->rb_crb) {
3787c478bd9Sstevel@tonic-gate 			log_roll_write_crb(ufsvfsp, rbp);
3797c478bd9Sstevel@tonic-gate 		} else {
3807c478bd9Sstevel@tonic-gate 			log_roll_write_bufs(ufsvfsp, rbp);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		/* null out the rb_next link for next set of rolling */
3837c478bd9Sstevel@tonic-gate 		rbp2 = rbp->rb_next;
3847c478bd9Sstevel@tonic-gate 		rbp->rb_next = NULL;
3857c478bd9Sstevel@tonic-gate 	}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * wait for all the writes to finish
3897c478bd9Sstevel@tonic-gate 	 */
3907c478bd9Sstevel@tonic-gate 	for (rbp = rbs; rbp < rbs + nbuf; rbp++) {
3917c478bd9Sstevel@tonic-gate 		bp = &rbp->rb_bh;
3927c478bd9Sstevel@tonic-gate 		if (trans_not_wait(bp)) {
3937c478bd9Sstevel@tonic-gate 			ldl_seterror(ul,
3947c478bd9Sstevel@tonic-gate 			    "Error writing master during ufs log roll");
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		/*
3987c478bd9Sstevel@tonic-gate 		 * Now wait for all the "cloned" buffer writes (if any)
3997c478bd9Sstevel@tonic-gate 		 * and free those headers
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		bp2 = bp->b_list;
4027c478bd9Sstevel@tonic-gate 		bp->b_list = NULL;
4037c478bd9Sstevel@tonic-gate 		while (bp2) {
4047c478bd9Sstevel@tonic-gate 			if (trans_not_wait(bp2)) {
4057c478bd9Sstevel@tonic-gate 				ldl_seterror(ul,
4067c478bd9Sstevel@tonic-gate 				    "Error writing master during ufs log roll");
4077c478bd9Sstevel@tonic-gate 			}
4087c478bd9Sstevel@tonic-gate 			bp = bp2;
4097c478bd9Sstevel@tonic-gate 			bp2 = bp2->b_list;
4107c478bd9Sstevel@tonic-gate 			kmem_free(bp, sizeof (buf_t));
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR)
4157c478bd9Sstevel@tonic-gate 		return (1);
4167c478bd9Sstevel@tonic-gate 	return (0);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate void
trans_roll(ml_unit_t * ul)4207c478bd9Sstevel@tonic-gate trans_roll(ml_unit_t *ul)
4217c478bd9Sstevel@tonic-gate {
4227c478bd9Sstevel@tonic-gate 	callb_cpr_t	cprinfo;
4237c478bd9Sstevel@tonic-gate 	mt_map_t	*logmap = ul->un_logmap;
4247c478bd9Sstevel@tonic-gate 	rollbuf_t	*rbs;
4257c478bd9Sstevel@tonic-gate 	rollbuf_t	*rbp;
4267c478bd9Sstevel@tonic-gate 	buf_t		*bp;
4277c478bd9Sstevel@tonic-gate 	caddr_t		roll_bufs;
4287c478bd9Sstevel@tonic-gate 	uint32_t	nmblk;
4297c478bd9Sstevel@tonic-gate 	int		i;
4307c478bd9Sstevel@tonic-gate 	int		doingforceroll;
4317c478bd9Sstevel@tonic-gate 	int		nbuf;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &logmap->mtm_mutex, callb_generic_cpr,
4347c478bd9Sstevel@tonic-gate 	    "trans_roll");
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/*
4377c478bd9Sstevel@tonic-gate 	 * We do not want the roll thread's writes to be
4387c478bd9Sstevel@tonic-gate 	 * throttled by the snapshot.
4397c478bd9Sstevel@tonic-gate 	 * If they are throttled then we can have a deadlock
4407c478bd9Sstevel@tonic-gate 	 * between the roll thread and the snapshot taskq thread:
4417c478bd9Sstevel@tonic-gate 	 * roll thread wants the throttling semaphore and
4427c478bd9Sstevel@tonic-gate 	 * the snapshot taskq thread cannot release the semaphore
4437c478bd9Sstevel@tonic-gate 	 * because it is writing to the log and the log is full.
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	(void) tsd_set(bypass_snapshot_throttle_key, (void*)1);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * setup some roll parameters
4507c478bd9Sstevel@tonic-gate 	 */
4517c478bd9Sstevel@tonic-gate 	if (trans_roll_tics == 0)
4527c478bd9Sstevel@tonic-gate 		trans_roll_tics = 5 * hz;
4537c478bd9Sstevel@tonic-gate 	nmblk = log_roll_buffers();
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * allocate the buffers and buffer headers
4577c478bd9Sstevel@tonic-gate 	 */
4587c478bd9Sstevel@tonic-gate 	roll_bufs = kmem_alloc(nmblk * MAPBLOCKSIZE, KM_SLEEP);
4597c478bd9Sstevel@tonic-gate 	rbs = kmem_alloc(nmblk * sizeof (rollbuf_t), KM_SLEEP);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	/*
4627c478bd9Sstevel@tonic-gate 	 * initialize the buffer headers
4637c478bd9Sstevel@tonic-gate 	 */
4647c478bd9Sstevel@tonic-gate 	for (i = 0, rbp = rbs; i < nmblk; ++i, ++rbp) {
4657c478bd9Sstevel@tonic-gate 		rbp->rb_next = NULL;
4667c478bd9Sstevel@tonic-gate 		bp = &rbp->rb_bh;
4677c478bd9Sstevel@tonic-gate 		bioinit(bp);
4687c478bd9Sstevel@tonic-gate 		bp->b_edev = ul->un_dev;
4697c478bd9Sstevel@tonic-gate 		bp->b_iodone = trans_not_done;
4707c478bd9Sstevel@tonic-gate 		bp->b_bufsize = MAPBLOCKSIZE;
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	doingforceroll = 0;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate again:
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * LOOP FOREVER
4787c478bd9Sstevel@tonic-gate 	 */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * exit on demand
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	mutex_enter(&logmap->mtm_mutex);
4847c478bd9Sstevel@tonic-gate 	if ((ul->un_flags & LDL_ERROR) || (logmap->mtm_flags & MTM_ROLL_EXIT)) {
4857c478bd9Sstevel@tonic-gate 		kmem_free(rbs, nmblk * sizeof (rollbuf_t));
4867c478bd9Sstevel@tonic-gate 		kmem_free(roll_bufs, nmblk * MAPBLOCKSIZE);
4877c478bd9Sstevel@tonic-gate 		logmap->mtm_flags &= ~(MTM_FORCE_ROLL | MTM_ROLL_RUNNING |
4887c478bd9Sstevel@tonic-gate 		    MTM_ROLL_EXIT | MTM_ROLLING);
4897c478bd9Sstevel@tonic-gate 		cv_broadcast(&logmap->mtm_from_roll_cv);
4907c478bd9Sstevel@tonic-gate 		CALLB_CPR_EXIT(&cprinfo);
4917c478bd9Sstevel@tonic-gate 		thread_exit();
4927c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * MT_SCAN debug mode
4977c478bd9Sstevel@tonic-gate 	 *	don't roll except in FORCEROLL situations
4987c478bd9Sstevel@tonic-gate 	 */
4997c478bd9Sstevel@tonic-gate 	if (logmap->mtm_debug & MT_SCAN)
5007c478bd9Sstevel@tonic-gate 		if ((logmap->mtm_flags & MTM_FORCE_ROLL) == 0) {
5017c478bd9Sstevel@tonic-gate 			mutex_exit(&logmap->mtm_mutex);
5027c478bd9Sstevel@tonic-gate 			trans_roll_wait(logmap, &cprinfo);
5037c478bd9Sstevel@tonic-gate 			goto again;
5047c478bd9Sstevel@tonic-gate 		}
5057c478bd9Sstevel@tonic-gate 	ASSERT(logmap->mtm_trimlof == 0);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	/*
5087c478bd9Sstevel@tonic-gate 	 * If we've finished a force roll cycle then wakeup any
5097c478bd9Sstevel@tonic-gate 	 * waiters.
5107c478bd9Sstevel@tonic-gate 	 */
5117c478bd9Sstevel@tonic-gate 	if (doingforceroll) {
5127c478bd9Sstevel@tonic-gate 		doingforceroll = 0;
5137c478bd9Sstevel@tonic-gate 		logmap->mtm_flags &= ~MTM_FORCE_ROLL;
5147c478bd9Sstevel@tonic-gate 		mutex_exit(&logmap->mtm_mutex);
5157c478bd9Sstevel@tonic-gate 		cv_broadcast(&logmap->mtm_from_roll_cv);
5167c478bd9Sstevel@tonic-gate 	} else {
5177c478bd9Sstevel@tonic-gate 		mutex_exit(&logmap->mtm_mutex);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	/*
5217c478bd9Sstevel@tonic-gate 	 * If someone wants us to roll something; then do it
5227c478bd9Sstevel@tonic-gate 	 */
5237c478bd9Sstevel@tonic-gate 	if (logmap->mtm_flags & MTM_FORCE_ROLL) {
5247c478bd9Sstevel@tonic-gate 		doingforceroll = 1;
5257c478bd9Sstevel@tonic-gate 		goto rollsomething;
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * Log is busy, check if logmap is getting full.
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	if (logmap_need_roll(logmap)) {
5327c478bd9Sstevel@tonic-gate 		goto rollsomething;
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * Check if the log is idle and is not empty
5377c478bd9Sstevel@tonic-gate 	 */
5387c478bd9Sstevel@tonic-gate 	if (!logmap->mtm_ref && !ldl_empty(ul)) {
5397c478bd9Sstevel@tonic-gate 		goto rollsomething;
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Log is busy, check if its getting full
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	if (ldl_need_roll(ul)) {
5467c478bd9Sstevel@tonic-gate 		goto rollsomething;
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	/*
5507c478bd9Sstevel@tonic-gate 	 * nothing to do; wait a bit and then start over
5517c478bd9Sstevel@tonic-gate 	 */
5527c478bd9Sstevel@tonic-gate 	trans_roll_wait(logmap, &cprinfo);
5537c478bd9Sstevel@tonic-gate 	goto again;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * ROLL SOMETHING
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate rollsomething:
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Use the cached roll buffers, or read the master
5627c478bd9Sstevel@tonic-gate 	 * and overlay the deltas
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	switch (log_roll_read(ul, rbs, nmblk, roll_bufs, &nbuf)) {
5657c478bd9Sstevel@tonic-gate 	case 1: trans_roll_wait(logmap, &cprinfo);
5667c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
5677c478bd9Sstevel@tonic-gate 	case 2: goto again;
5687c478bd9Sstevel@tonic-gate 	/* default case is success */
5697c478bd9Sstevel@tonic-gate 	}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Asynchronously write out the deltas
5737c478bd9Sstevel@tonic-gate 	 */
5747c478bd9Sstevel@tonic-gate 	if (log_roll_write(ul, rbs, nbuf))
5757c478bd9Sstevel@tonic-gate 		goto again;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	/*
5787c478bd9Sstevel@tonic-gate 	 * free up the deltas in the logmap
5797c478bd9Sstevel@tonic-gate 	 */
5807c478bd9Sstevel@tonic-gate 	for (i = 0, rbp = rbs; i < nbuf; ++i, ++rbp) {
5817c478bd9Sstevel@tonic-gate 		bp = &rbp->rb_bh;
5827c478bd9Sstevel@tonic-gate 		logmap_remove_roll(logmap,
5837c478bd9Sstevel@tonic-gate 		    ldbtob(bp->b_blkno) & (offset_t)MAPBLOCKMASK, MAPBLOCKSIZE);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	/*
5877c478bd9Sstevel@tonic-gate 	 * free up log space; if possible
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 	logmap_sethead(logmap, ul);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * LOOP
5937c478bd9Sstevel@tonic-gate 	 */
5947c478bd9Sstevel@tonic-gate 	goto again;
5957c478bd9Sstevel@tonic-gate }
596