xref: /onnv-gate/usr/src/uts/common/fs/ufs/lufs.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54662Sfrankho  * Common Development and Distribution License (the "License").
64662Sfrankho  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229915SOwen.Roberts@Sun.Com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/systm.h>
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/vnode.h>
290Sstevel@tonic-gate #include <sys/buf.h>
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/fssnap_if.h>
320Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
330Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/modctl.h>
360Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
370Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
380Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
390Sstevel@tonic-gate #include <sys/debug.h>
40329Saguzovsk #include <sys/atomic.h>
410Sstevel@tonic-gate #include <sys/kmem.h>
420Sstevel@tonic-gate #include <sys/inttypes.h>
430Sstevel@tonic-gate #include <sys/vfs.h>
440Sstevel@tonic-gate #include <sys/mntent.h>
450Sstevel@tonic-gate #include <sys/conf.h>
460Sstevel@tonic-gate #include <sys/param.h>
470Sstevel@tonic-gate #include <sys/kstat.h>
480Sstevel@tonic-gate #include <sys/cmn_err.h>
497455SWolfgang.Schremser@Sun.COM #include <sys/sdt.h>
507455SWolfgang.Schremser@Sun.COM 
517455SWolfgang.Schremser@Sun.COM #define	LUFS_GENID_PRIME	UINT64_C(4294967291)
527455SWolfgang.Schremser@Sun.COM #define	LUFS_GENID_BASE		UINT64_C(311)
537455SWolfgang.Schremser@Sun.COM #define	LUFS_NEXT_ID(id)	((uint32_t)(((id) * LUFS_GENID_BASE) % \
547455SWolfgang.Schremser@Sun.COM 				    LUFS_GENID_PRIME))
550Sstevel@tonic-gate 
56921Sbatschul extern	kmutex_t	ufs_scan_lock;
57921Sbatschul 
580Sstevel@tonic-gate static kmutex_t	log_mutex;	/* general purpose log layer lock */
590Sstevel@tonic-gate kmutex_t	ml_scan;	/* Scan thread syncronization */
600Sstevel@tonic-gate kcondvar_t	ml_scan_cv;	/* Scan thread syncronization */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate struct kmem_cache	*lufs_sv;
630Sstevel@tonic-gate struct kmem_cache	*lufs_bp;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /* Tunables */
660Sstevel@tonic-gate uint_t		ldl_maxlogsize	= LDL_MAXLOGSIZE;
670Sstevel@tonic-gate uint_t		ldl_minlogsize	= LDL_MINLOGSIZE;
689915SOwen.Roberts@Sun.Com uint_t		ldl_softlogcap	= LDL_SOFTLOGCAP;
690Sstevel@tonic-gate uint32_t	ldl_divisor	= LDL_DIVISOR;
700Sstevel@tonic-gate uint32_t	ldl_mintransfer	= LDL_MINTRANSFER;
710Sstevel@tonic-gate uint32_t	ldl_maxtransfer	= LDL_MAXTRANSFER;
720Sstevel@tonic-gate uint32_t	ldl_minbufsize	= LDL_MINBUFSIZE;
739915SOwen.Roberts@Sun.Com uint32_t	ldl_cgsizereq	= 0;
740Sstevel@tonic-gate 
757455SWolfgang.Schremser@Sun.COM /* Generation of header ids */
767789SMilan.Cermak@Sun.COM static kmutex_t	genid_mutex;
777789SMilan.Cermak@Sun.COM static uint32_t	last_loghead_ident = UINT32_C(0);
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Logging delta and roll statistics
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate struct delta_kstats {
830Sstevel@tonic-gate 	kstat_named_t ds_superblock_deltas;
840Sstevel@tonic-gate 	kstat_named_t ds_bitmap_deltas;
850Sstevel@tonic-gate 	kstat_named_t ds_suminfo_deltas;
860Sstevel@tonic-gate 	kstat_named_t ds_allocblk_deltas;
870Sstevel@tonic-gate 	kstat_named_t ds_ab0_deltas;
880Sstevel@tonic-gate 	kstat_named_t ds_dir_deltas;
890Sstevel@tonic-gate 	kstat_named_t ds_inode_deltas;
900Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_deltas;
910Sstevel@tonic-gate 	kstat_named_t ds_quota_deltas;
920Sstevel@tonic-gate 	kstat_named_t ds_shadow_deltas;
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	kstat_named_t ds_superblock_rolled;
950Sstevel@tonic-gate 	kstat_named_t ds_bitmap_rolled;
960Sstevel@tonic-gate 	kstat_named_t ds_suminfo_rolled;
970Sstevel@tonic-gate 	kstat_named_t ds_allocblk_rolled;
980Sstevel@tonic-gate 	kstat_named_t ds_ab0_rolled;
990Sstevel@tonic-gate 	kstat_named_t ds_dir_rolled;
1000Sstevel@tonic-gate 	kstat_named_t ds_inode_rolled;
1010Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_rolled;
1020Sstevel@tonic-gate 	kstat_named_t ds_quota_rolled;
1030Sstevel@tonic-gate 	kstat_named_t ds_shadow_rolled;
1040Sstevel@tonic-gate } dkstats = {
1050Sstevel@tonic-gate 	{ "superblock_deltas",	KSTAT_DATA_UINT64 },
1060Sstevel@tonic-gate 	{ "bitmap_deltas",	KSTAT_DATA_UINT64 },
1070Sstevel@tonic-gate 	{ "suminfo_deltas",	KSTAT_DATA_UINT64 },
1080Sstevel@tonic-gate 	{ "allocblk_deltas",	KSTAT_DATA_UINT64 },
1090Sstevel@tonic-gate 	{ "ab0_deltas",		KSTAT_DATA_UINT64 },
1100Sstevel@tonic-gate 	{ "dir_deltas",		KSTAT_DATA_UINT64 },
1110Sstevel@tonic-gate 	{ "inode_deltas",	KSTAT_DATA_UINT64 },
1120Sstevel@tonic-gate 	{ "fbiwrite_deltas",	KSTAT_DATA_UINT64 },
1130Sstevel@tonic-gate 	{ "quota_deltas",	KSTAT_DATA_UINT64 },
1140Sstevel@tonic-gate 	{ "shadow_deltas",	KSTAT_DATA_UINT64 },
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	{ "superblock_rolled",	KSTAT_DATA_UINT64 },
1170Sstevel@tonic-gate 	{ "bitmap_rolled",	KSTAT_DATA_UINT64 },
1180Sstevel@tonic-gate 	{ "suminfo_rolled",	KSTAT_DATA_UINT64 },
1190Sstevel@tonic-gate 	{ "allocblk_rolled",	KSTAT_DATA_UINT64 },
1200Sstevel@tonic-gate 	{ "ab0_rolled",		KSTAT_DATA_UINT64 },
1210Sstevel@tonic-gate 	{ "dir_rolled",		KSTAT_DATA_UINT64 },
1220Sstevel@tonic-gate 	{ "inode_rolled",	KSTAT_DATA_UINT64 },
1230Sstevel@tonic-gate 	{ "fbiwrite_rolled",	KSTAT_DATA_UINT64 },
1240Sstevel@tonic-gate 	{ "quota_rolled",	KSTAT_DATA_UINT64 },
1250Sstevel@tonic-gate 	{ "shadow_rolled",	KSTAT_DATA_UINT64 }
1260Sstevel@tonic-gate };
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate uint64_t delta_stats[DT_MAX];
1290Sstevel@tonic-gate uint64_t roll_stats[DT_MAX];
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * General logging kstats
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate struct logstats logstats = {
1350Sstevel@tonic-gate 	{ "master_reads",		KSTAT_DATA_UINT64 },
1360Sstevel@tonic-gate 	{ "master_writes",		KSTAT_DATA_UINT64 },
1370Sstevel@tonic-gate 	{ "log_reads_inmem",		KSTAT_DATA_UINT64 },
1380Sstevel@tonic-gate 	{ "log_reads",			KSTAT_DATA_UINT64 },
1390Sstevel@tonic-gate 	{ "log_writes",			KSTAT_DATA_UINT64 },
1400Sstevel@tonic-gate 	{ "log_master_reads",		KSTAT_DATA_UINT64 },
1410Sstevel@tonic-gate 	{ "log_roll_reads",		KSTAT_DATA_UINT64 },
1420Sstevel@tonic-gate 	{ "log_roll_writes",		KSTAT_DATA_UINT64 }
1430Sstevel@tonic-gate };
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate int
trans_not_done(struct buf * cb)1460Sstevel@tonic-gate trans_not_done(struct buf *cb)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	sema_v(&cb->b_io);
1490Sstevel@tonic-gate 	return (0);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate static void
trans_wait_panic(struct buf * cb)1530Sstevel@tonic-gate trans_wait_panic(struct buf *cb)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	while ((cb->b_flags & B_DONE) == 0)
1560Sstevel@tonic-gate 		drv_usecwait(10);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate int
trans_not_wait(struct buf * cb)1600Sstevel@tonic-gate trans_not_wait(struct buf *cb)
1610Sstevel@tonic-gate {
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * In case of panic, busy wait for completion
1640Sstevel@tonic-gate 	 */
1650Sstevel@tonic-gate 	if (panicstr)
1660Sstevel@tonic-gate 		trans_wait_panic(cb);
1670Sstevel@tonic-gate 	else
1680Sstevel@tonic-gate 		sema_p(&cb->b_io);
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	return (geterror(cb));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate int
trans_wait(struct buf * cb)1740Sstevel@tonic-gate trans_wait(struct buf *cb)
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 * In case of panic, busy wait for completion and run md daemon queues
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	if (panicstr)
1800Sstevel@tonic-gate 		trans_wait_panic(cb);
1810Sstevel@tonic-gate 	return (biowait(cb));
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate static void
setsum(int32_t * sp,int32_t * lp,int nb)1850Sstevel@tonic-gate setsum(int32_t *sp, int32_t *lp, int nb)
1860Sstevel@tonic-gate {
1870Sstevel@tonic-gate 	int32_t csum = 0;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	*sp = 0;
1900Sstevel@tonic-gate 	nb /= sizeof (int32_t);
1910Sstevel@tonic-gate 	while (nb--)
1920Sstevel@tonic-gate 		csum += *lp++;
1930Sstevel@tonic-gate 	*sp = csum;
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate static int
checksum(int32_t * sp,int32_t * lp,int nb)1970Sstevel@tonic-gate checksum(int32_t *sp, int32_t *lp, int nb)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	int32_t ssum = *sp;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	setsum(sp, lp, nb);
2020Sstevel@tonic-gate 	if (ssum != *sp) {
2030Sstevel@tonic-gate 		*sp = ssum;
2040Sstevel@tonic-gate 		return (0);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	return (1);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate void
lufs_unsnarf(ufsvfs_t * ufsvfsp)2100Sstevel@tonic-gate lufs_unsnarf(ufsvfs_t *ufsvfsp)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	ml_unit_t *ul;
2130Sstevel@tonic-gate 	mt_map_t *mtm;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
2160Sstevel@tonic-gate 	if (ul == NULL)
2170Sstevel@tonic-gate 		return;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	mtm = ul->un_logmap;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * Wait for a pending top_issue_sync which is
2230Sstevel@tonic-gate 	 * dispatched (via taskq_dispatch()) but hasnt completed yet.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	mutex_enter(&mtm->mtm_lock);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	while (mtm->mtm_taskq_sync_count != 0) {
2290Sstevel@tonic-gate 		cv_wait(&mtm->mtm_cv, &mtm->mtm_lock);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	mutex_exit(&mtm->mtm_lock);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/* Roll committed transactions */
2350Sstevel@tonic-gate 	logmap_roll_dev(ul);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	/* Kill the roll thread */
2380Sstevel@tonic-gate 	logmap_kill_roll(ul);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/* release saved alloction info */
2410Sstevel@tonic-gate 	if (ul->un_ebp)
2420Sstevel@tonic-gate 		kmem_free(ul->un_ebp, ul->un_nbeb);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	/* release circular bufs */
2450Sstevel@tonic-gate 	free_cirbuf(&ul->un_rdbuf);
2460Sstevel@tonic-gate 	free_cirbuf(&ul->un_wrbuf);
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/* release maps */
2490Sstevel@tonic-gate 	if (ul->un_logmap)
2500Sstevel@tonic-gate 		ul->un_logmap = map_put(ul->un_logmap);
2510Sstevel@tonic-gate 	if (ul->un_deltamap)
2520Sstevel@tonic-gate 		ul->un_deltamap = map_put(ul->un_deltamap);
2530Sstevel@tonic-gate 	if (ul->un_matamap)
2540Sstevel@tonic-gate 		ul->un_matamap = map_put(ul->un_matamap);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	mutex_destroy(&ul->un_log_mutex);
2570Sstevel@tonic-gate 	mutex_destroy(&ul->un_state_mutex);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	/* release state buffer MUST BE LAST!! (contains our ondisk data) */
2600Sstevel@tonic-gate 	if (ul->un_bp)
2610Sstevel@tonic-gate 		brelse(ul->un_bp);
2620Sstevel@tonic-gate 	kmem_free(ul, sizeof (*ul));
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	ufsvfsp->vfs_log = NULL;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate int
lufs_snarf(ufsvfs_t * ufsvfsp,struct fs * fs,int ronly)2680Sstevel@tonic-gate lufs_snarf(ufsvfs_t *ufsvfsp, struct fs *fs, int ronly)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	buf_t		*bp, *tbp;
2710Sstevel@tonic-gate 	ml_unit_t	*ul;
2720Sstevel@tonic-gate 	extent_block_t	*ebp;
2730Sstevel@tonic-gate 	ic_extent_block_t  *nebp;
2740Sstevel@tonic-gate 	size_t		nb;
2750Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
2760Sstevel@tonic-gate 	int		i;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
2790Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Get the allocation table
2830Sstevel@tonic-gate 	 *	During a remount the superblock pointed to by the ufsvfsp
2840Sstevel@tonic-gate 	 *	is out of date.  Hence the need for the ``new'' superblock
2850Sstevel@tonic-gate 	 *	pointer, fs, passed in as a parameter.
2860Sstevel@tonic-gate 	 */
2870Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, fs->fs_logbno),
2880Sstevel@tonic-gate 	    fs->fs_bsize);
2890Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
2900Sstevel@tonic-gate 		brelse(bp);
2910Sstevel@tonic-gate 		return (EIO);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
2940Sstevel@tonic-gate 	if (!checksum(&ebp->chksum, (int32_t *)bp->b_un.b_addr,
2954662Sfrankho 	    fs->fs_bsize)) {
2960Sstevel@tonic-gate 		brelse(bp);
2970Sstevel@tonic-gate 		return (ENODEV);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	/*
3010Sstevel@tonic-gate 	 * It is possible to get log blocks with all zeros.
3020Sstevel@tonic-gate 	 * We should also check for nextents to be zero in such case.
3030Sstevel@tonic-gate 	 */
3040Sstevel@tonic-gate 	if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0) {
3050Sstevel@tonic-gate 		brelse(bp);
3060Sstevel@tonic-gate 		return (EDOM);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	/*
3090Sstevel@tonic-gate 	 * Put allocation into memory.  This requires conversion between
3100Sstevel@tonic-gate 	 * on the ondisk format of the extent (type extent_t) and the
3110Sstevel@tonic-gate 	 * in-core format of the extent (type ic_extent_t).  The
3120Sstevel@tonic-gate 	 * difference is the in-core form of the extent block stores
3130Sstevel@tonic-gate 	 * the physical offset of the extent in disk blocks, which
3140Sstevel@tonic-gate 	 * can require more than a 32-bit field.
3150Sstevel@tonic-gate 	 */
3160Sstevel@tonic-gate 	nb = (size_t)(sizeof (ic_extent_block_t) +
3174662Sfrankho 	    ((ebp->nextents - 1) * sizeof (ic_extent_t)));
3180Sstevel@tonic-gate 	nebp = kmem_alloc(nb, KM_SLEEP);
3190Sstevel@tonic-gate 	nebp->ic_nextents = ebp->nextents;
3200Sstevel@tonic-gate 	nebp->ic_nbytes = ebp->nbytes;
3210Sstevel@tonic-gate 	nebp->ic_nextbno = ebp->nextbno;
3220Sstevel@tonic-gate 	for (i = 0; i < ebp->nextents; i++) {
3230Sstevel@tonic-gate 		nebp->ic_extents[i].ic_lbno = ebp->extents[i].lbno;
3240Sstevel@tonic-gate 		nebp->ic_extents[i].ic_nbno = ebp->extents[i].nbno;
3250Sstevel@tonic-gate 		nebp->ic_extents[i].ic_pbno =
3260Sstevel@tonic-gate 		    logbtodb(fs, ebp->extents[i].pbno);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	brelse(bp);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * Get the log state
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	bno = nebp->ic_extents[0].ic_pbno;
3340Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, DEV_BSIZE);
3350Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
3360Sstevel@tonic-gate 		brelse(bp);
3370Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno + 1, DEV_BSIZE);
3380Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
3390Sstevel@tonic-gate 			brelse(bp);
3400Sstevel@tonic-gate 			kmem_free(nebp, nb);
3410Sstevel@tonic-gate 			return (EIO);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	/*
3460Sstevel@tonic-gate 	 * Put ondisk struct into an anonymous buffer
3470Sstevel@tonic-gate 	 *	This buffer will contain the memory for the ml_odunit struct
3480Sstevel@tonic-gate 	 */
3490Sstevel@tonic-gate 	tbp = ngeteblk(dbtob(LS_SECTORS));
3500Sstevel@tonic-gate 	tbp->b_edev = bp->b_edev;
3510Sstevel@tonic-gate 	tbp->b_dev = bp->b_dev;
3520Sstevel@tonic-gate 	tbp->b_blkno = bno;
3530Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
3540Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
3550Sstevel@tonic-gate 	bp->b_flags |= (B_STALE | B_AGE);
3560Sstevel@tonic-gate 	brelse(bp);
3570Sstevel@tonic-gate 	bp = tbp;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	/*
3600Sstevel@tonic-gate 	 * Verify the log state
3610Sstevel@tonic-gate 	 *
3620Sstevel@tonic-gate 	 * read/only mounts w/bad logs are allowed.  umount will
3630Sstevel@tonic-gate 	 * eventually roll the bad log until the first IO error.
3640Sstevel@tonic-gate 	 * fsck will then repair the file system.
3650Sstevel@tonic-gate 	 *
3660Sstevel@tonic-gate 	 * read/write mounts with bad logs are not allowed.
3670Sstevel@tonic-gate 	 *
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	ul = (ml_unit_t *)kmem_zalloc(sizeof (*ul), KM_SLEEP);
3700Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, &ul->un_ondisk, sizeof (ml_odunit_t));
3710Sstevel@tonic-gate 	if ((ul->un_chksum != ul->un_head_ident + ul->un_tail_ident) ||
3720Sstevel@tonic-gate 	    (ul->un_version != LUFS_VERSION_LATEST) ||
3730Sstevel@tonic-gate 	    (!ronly && ul->un_badlog)) {
3740Sstevel@tonic-gate 		kmem_free(ul, sizeof (*ul));
3750Sstevel@tonic-gate 		brelse(bp);
3760Sstevel@tonic-gate 		kmem_free(nebp, nb);
3770Sstevel@tonic-gate 		return (EIO);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	/*
3800Sstevel@tonic-gate 	 * Initialize the incore-only fields
3810Sstevel@tonic-gate 	 */
3820Sstevel@tonic-gate 	if (ronly)
3830Sstevel@tonic-gate 		ul->un_flags |= LDL_NOROLL;
3840Sstevel@tonic-gate 	ul->un_bp = bp;
3850Sstevel@tonic-gate 	ul->un_ufsvfs = ufsvfsp;
3860Sstevel@tonic-gate 	ul->un_dev = ufsvfsp->vfs_dev;
3870Sstevel@tonic-gate 	ul->un_ebp = nebp;
3880Sstevel@tonic-gate 	ul->un_nbeb = nb;
3890Sstevel@tonic-gate 	ul->un_maxresv = btodb(ul->un_logsize) * LDL_USABLE_BSIZE;
3900Sstevel@tonic-gate 	ul->un_deltamap = map_get(ul, deltamaptype, DELTAMAP_NHASH);
3910Sstevel@tonic-gate 	ul->un_logmap = map_get(ul, logmaptype, LOGMAP_NHASH);
3920Sstevel@tonic-gate 	if (ul->un_debug & MT_MATAMAP)
3930Sstevel@tonic-gate 		ul->un_matamap = map_get(ul, matamaptype, DELTAMAP_NHASH);
3940Sstevel@tonic-gate 	mutex_init(&ul->un_log_mutex, NULL, MUTEX_DEFAULT, NULL);
3950Sstevel@tonic-gate 	mutex_init(&ul->un_state_mutex, NULL, MUTEX_DEFAULT, NULL);
396921Sbatschul 
397921Sbatschul 	/*
398921Sbatschul 	 * Aquire the ufs_scan_lock before linking the mtm data
399921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
400921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
401921Sbatschul 	 * progress of enabling/disabling logging.
402921Sbatschul 	 */
403921Sbatschul 	mutex_enter(&ufs_scan_lock);
4040Sstevel@tonic-gate 	ufsvfsp->vfs_log = ul;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	/* remember the state of the log before the log scan */
4070Sstevel@tonic-gate 	logmap_logscan(ul);
408921Sbatschul 	mutex_exit(&ufs_scan_lock);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/*
4110Sstevel@tonic-gate 	 * Error during scan
4120Sstevel@tonic-gate 	 *
4130Sstevel@tonic-gate 	 * If this is a read/only mount; ignore the error.
4140Sstevel@tonic-gate 	 * At a later time umount/fsck will repair the fs.
4150Sstevel@tonic-gate 	 *
4160Sstevel@tonic-gate 	 */
4170Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
4180Sstevel@tonic-gate 		if (!ronly) {
419921Sbatschul 			/*
420921Sbatschul 			 * Aquire the ufs_scan_lock before de-linking
421921Sbatschul 			 * the mtm data structure so that we keep ufs_sync()
422921Sbatschul 			 * and ufs_update() away when they execute the
423921Sbatschul 			 * ufs_scan_inodes() run while we're in progress of
424921Sbatschul 			 * enabling/disabling logging.
425921Sbatschul 			 */
426921Sbatschul 			mutex_enter(&ufs_scan_lock);
4270Sstevel@tonic-gate 			lufs_unsnarf(ufsvfsp);
428921Sbatschul 			mutex_exit(&ufs_scan_lock);
4290Sstevel@tonic-gate 			return (EIO);
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 		ul->un_flags &= ~LDL_ERROR;
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 	if (!ronly)
4340Sstevel@tonic-gate 		logmap_start_roll(ul);
4350Sstevel@tonic-gate 	return (0);
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate 
4387455SWolfgang.Schremser@Sun.COM uint32_t
lufs_hd_genid(const ml_unit_t * up)4397455SWolfgang.Schremser@Sun.COM lufs_hd_genid(const ml_unit_t *up)
4407455SWolfgang.Schremser@Sun.COM {
4417455SWolfgang.Schremser@Sun.COM 	uint32_t id;
4427455SWolfgang.Schremser@Sun.COM 
4437455SWolfgang.Schremser@Sun.COM 	mutex_enter(&genid_mutex);
4447455SWolfgang.Schremser@Sun.COM 
4457455SWolfgang.Schremser@Sun.COM 	/*
4467455SWolfgang.Schremser@Sun.COM 	 * The formula below implements an exponential, modular sequence.
4477455SWolfgang.Schremser@Sun.COM 	 *
4487455SWolfgang.Schremser@Sun.COM 	 * ID(N) = (SEED * (BASE^N)) % PRIME
4497455SWolfgang.Schremser@Sun.COM 	 *
4507455SWolfgang.Schremser@Sun.COM 	 * The numbers will be pseudo random.  They depend on SEED, BASE, PRIME,
4517455SWolfgang.Schremser@Sun.COM 	 * but will sweep through almost all of the range 1....PRIME-1.
4527455SWolfgang.Schremser@Sun.COM 	 * Most  importantly  they  will  not  repeat  for PRIME-2 (4294967289)
4537455SWolfgang.Schremser@Sun.COM 	 * repetitions.  If they would repeat that  could possibly cause  hangs,
4547455SWolfgang.Schremser@Sun.COM 	 * panics at mount/umount and failed mount operations.
4557455SWolfgang.Schremser@Sun.COM 	 */
4567455SWolfgang.Schremser@Sun.COM 	id = LUFS_NEXT_ID(last_loghead_ident);
4577455SWolfgang.Schremser@Sun.COM 
4587455SWolfgang.Schremser@Sun.COM 	/* Checking if new identity used already */
4597455SWolfgang.Schremser@Sun.COM 	if (up != NULL && up->un_head_ident == id) {
4607455SWolfgang.Schremser@Sun.COM 		DTRACE_PROBE1(head_ident_collision, uint32_t, id);
4617455SWolfgang.Schremser@Sun.COM 
4627455SWolfgang.Schremser@Sun.COM 		/*
4637455SWolfgang.Schremser@Sun.COM 		 * The  following  preserves  the  algorithm  for  the fix  for
4647455SWolfgang.Schremser@Sun.COM 		 * "panic: free: freeing free frag, dev:0x2000000018, blk:34605,
4657455SWolfgang.Schremser@Sun.COM 		 * cg:26, ino:148071,".
4667455SWolfgang.Schremser@Sun.COM 		 * If  the header identities  un_head_ident  are  equal  to the
4677455SWolfgang.Schremser@Sun.COM 		 * present element  in the sequence,  the next element  of  the
4687455SWolfgang.Schremser@Sun.COM 		 * sequence is returned instead.
4697455SWolfgang.Schremser@Sun.COM 		 */
4707455SWolfgang.Schremser@Sun.COM 		id = LUFS_NEXT_ID(id);
4717455SWolfgang.Schremser@Sun.COM 	}
4727455SWolfgang.Schremser@Sun.COM 
4737455SWolfgang.Schremser@Sun.COM 	last_loghead_ident = id;
4747455SWolfgang.Schremser@Sun.COM 
4757455SWolfgang.Schremser@Sun.COM 	mutex_exit(&genid_mutex);
4767455SWolfgang.Schremser@Sun.COM 
4777455SWolfgang.Schremser@Sun.COM 	return (id);
4787455SWolfgang.Schremser@Sun.COM }
4797455SWolfgang.Schremser@Sun.COM 
4807455SWolfgang.Schremser@Sun.COM static void
lufs_genid_init(void)4817455SWolfgang.Schremser@Sun.COM lufs_genid_init(void)
4827455SWolfgang.Schremser@Sun.COM {
4837455SWolfgang.Schremser@Sun.COM 	uint64_t seed;
4847455SWolfgang.Schremser@Sun.COM 
4857455SWolfgang.Schremser@Sun.COM 	/* Initialization */
4867455SWolfgang.Schremser@Sun.COM 	mutex_init(&genid_mutex, NULL, MUTEX_DEFAULT, NULL);
4877455SWolfgang.Schremser@Sun.COM 
4887455SWolfgang.Schremser@Sun.COM 	/* Seed the algorithm */
4897455SWolfgang.Schremser@Sun.COM 	do {
4907455SWolfgang.Schremser@Sun.COM 		timestruc_t tv;
4917455SWolfgang.Schremser@Sun.COM 
4927455SWolfgang.Schremser@Sun.COM 		gethrestime(&tv);
4937455SWolfgang.Schremser@Sun.COM 
4947455SWolfgang.Schremser@Sun.COM 		seed = (tv.tv_nsec << 3);
4957455SWolfgang.Schremser@Sun.COM 		seed ^= tv.tv_sec;
4967455SWolfgang.Schremser@Sun.COM 
4977455SWolfgang.Schremser@Sun.COM 		last_loghead_ident = (uint32_t)(seed % LUFS_GENID_PRIME);
4987455SWolfgang.Schremser@Sun.COM 	} while (last_loghead_ident == UINT32_C(0));
4997455SWolfgang.Schremser@Sun.COM }
5007455SWolfgang.Schremser@Sun.COM 
5010Sstevel@tonic-gate static int
lufs_initialize(ufsvfs_t * ufsvfsp,daddr_t bno,size_t nb,struct fiolog * flp)5020Sstevel@tonic-gate lufs_initialize(
5030Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp,
5040Sstevel@tonic-gate 	daddr_t bno,
5050Sstevel@tonic-gate 	size_t nb,
5060Sstevel@tonic-gate 	struct fiolog *flp)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	ml_odunit_t	*ud, *ud2;
5090Sstevel@tonic-gate 	buf_t		*bp;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
5120Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
5130Sstevel@tonic-gate 	ASSERT(nb >= ldl_minlogsize);
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev, bno, dbtob(LS_SECTORS));
5160Sstevel@tonic-gate 	bzero(bp->b_un.b_addr, bp->b_bcount);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	ud = (void *)bp->b_un.b_addr;
5190Sstevel@tonic-gate 	ud->od_version = LUFS_VERSION_LATEST;
5200Sstevel@tonic-gate 	ud->od_maxtransfer = MIN(ufsvfsp->vfs_iotransz, ldl_maxtransfer);
5210Sstevel@tonic-gate 	if (ud->od_maxtransfer < ldl_mintransfer)
5220Sstevel@tonic-gate 		ud->od_maxtransfer = ldl_mintransfer;
5230Sstevel@tonic-gate 	ud->od_devbsize = DEV_BSIZE;
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	ud->od_requestsize = flp->nbytes_actual;
5260Sstevel@tonic-gate 	ud->od_statesize = dbtob(LS_SECTORS);
5270Sstevel@tonic-gate 	ud->od_logsize = nb - ud->od_statesize;
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	ud->od_statebno = INT32_C(0);
5300Sstevel@tonic-gate 
5317455SWolfgang.Schremser@Sun.COM 	ud->od_head_ident = lufs_hd_genid(NULL);
5320Sstevel@tonic-gate 	ud->od_tail_ident = ud->od_head_ident;
5330Sstevel@tonic-gate 	ud->od_chksum = ud->od_head_ident + ud->od_tail_ident;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	ud->od_bol_lof = dbtob(ud->od_statebno) + ud->od_statesize;
5360Sstevel@tonic-gate 	ud->od_eol_lof = ud->od_bol_lof + ud->od_logsize;
5370Sstevel@tonic-gate 	ud->od_head_lof = ud->od_bol_lof;
5380Sstevel@tonic-gate 	ud->od_tail_lof = ud->od_bol_lof;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	ASSERT(lufs_initialize_debug(ud));
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	ud2 = (void *)(bp->b_un.b_addr + DEV_BSIZE);
5430Sstevel@tonic-gate 	bcopy(ud, ud2, sizeof (*ud));
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
5460Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
5470Sstevel@tonic-gate 		brelse(bp);
5480Sstevel@tonic-gate 		return (EIO);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	brelse(bp);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	return (0);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * Free log space
5570Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
5580Sstevel@tonic-gate  */
5590Sstevel@tonic-gate static int
lufs_free(struct ufsvfs * ufsvfsp)5600Sstevel@tonic-gate lufs_free(struct ufsvfs *ufsvfsp)
5610Sstevel@tonic-gate {
5620Sstevel@tonic-gate 	int		error = 0, i, j;
5630Sstevel@tonic-gate 	buf_t		*bp = NULL;
5640Sstevel@tonic-gate 	extent_t	*ep;
5650Sstevel@tonic-gate 	extent_block_t	*ebp;
5660Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
5670Sstevel@tonic-gate 	daddr_t		fno;
5680Sstevel@tonic-gate 	int32_t		logbno;
5690Sstevel@tonic-gate 	long		nfno;
5700Sstevel@tonic-gate 	inode_t		*ip = NULL;
5710Sstevel@tonic-gate 	char		clean;
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 	/*
5740Sstevel@tonic-gate 	 * Nothing to free
5750Sstevel@tonic-gate 	 */
5760Sstevel@tonic-gate 	if (fs->fs_logbno == 0)
5770Sstevel@tonic-gate 		return (0);
5780Sstevel@tonic-gate 
5790Sstevel@tonic-gate 	/*
5800Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE and no log but honor the
5810Sstevel@tonic-gate 	 * current value of fs_reclaim.  The reclaim thread could have
5820Sstevel@tonic-gate 	 * been active when lufs_disable() was called and if fs_reclaim
5830Sstevel@tonic-gate 	 * is reset to zero here it could lead to lost inodes.
5840Sstevel@tonic-gate 	 */
5850Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
5860Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
5870Sstevel@tonic-gate 	clean = fs->fs_clean;
5880Sstevel@tonic-gate 	logbno = fs->fs_logbno;
5890Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
5900Sstevel@tonic-gate 	fs->fs_logbno = INT32_C(0);
5910Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
5920Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
5930Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
5940Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
5950Sstevel@tonic-gate 		error = EIO;
5960Sstevel@tonic-gate 		fs->fs_clean = clean;
5970Sstevel@tonic-gate 		fs->fs_logbno = logbno;
5980Sstevel@tonic-gate 		goto errout;
5990Sstevel@tonic-gate 	}
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * fetch the allocation block
6030Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
6040Sstevel@tonic-gate 	 */
6050Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, logbno),
6060Sstevel@tonic-gate 	    fs->fs_bsize);
6070Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6080Sstevel@tonic-gate 		error = EIO;
6090Sstevel@tonic-gate 		goto errout;
6100Sstevel@tonic-gate 	}
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * Free up the allocated space (dummy inode needed for free())
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
6160Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
6170Sstevel@tonic-gate 	for (i = 0, ep = &ebp->extents[0]; i < ebp->nextents; ++i, ++ep) {
6180Sstevel@tonic-gate 		fno = logbtofrag(fs, ep->pbno);
6190Sstevel@tonic-gate 		nfno = dbtofsb(fs, ep->nbno);
6200Sstevel@tonic-gate 		for (j = 0; j < nfno; j += fs->fs_frag, fno += fs->fs_frag)
6210Sstevel@tonic-gate 			free(ip, fno, fs->fs_bsize, 0);
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 	free(ip, logbtofrag(fs, logbno), fs->fs_bsize, 0);
6240Sstevel@tonic-gate 	brelse(bp);
6250Sstevel@tonic-gate 	bp = NULL;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	/*
6280Sstevel@tonic-gate 	 * Push the metadata dirtied during the allocations
6290Sstevel@tonic-gate 	 */
6300Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
6310Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
6320Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
6330Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
6340Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 0);
6350Sstevel@tonic-gate 	if (error)
6360Sstevel@tonic-gate 		goto errout;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * Free the dummy inode
6400Sstevel@tonic-gate 	 */
6410Sstevel@tonic-gate 	ufs_free_inode(ip);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	return (0);
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate errout:
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * Free up all resources
6480Sstevel@tonic-gate 	 */
6490Sstevel@tonic-gate 	if (bp)
6500Sstevel@tonic-gate 		brelse(bp);
6510Sstevel@tonic-gate 	if (ip)
6520Sstevel@tonic-gate 		ufs_free_inode(ip);
6530Sstevel@tonic-gate 	return (error);
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate  * Allocate log space
6580Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
6590Sstevel@tonic-gate  */
6600Sstevel@tonic-gate static int
lufs_alloc(struct ufsvfs * ufsvfsp,struct fiolog * flp,size_t minb,cred_t * cr)6619915SOwen.Roberts@Sun.Com lufs_alloc(struct ufsvfs *ufsvfsp, struct fiolog *flp, size_t minb, cred_t *cr)
6620Sstevel@tonic-gate {
6630Sstevel@tonic-gate 	int		error = 0;
6640Sstevel@tonic-gate 	buf_t		*bp = NULL;
6650Sstevel@tonic-gate 	extent_t	*ep, *nep;
6660Sstevel@tonic-gate 	extent_block_t	*ebp;
6670Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
6680Sstevel@tonic-gate 	daddr_t		fno;	/* in frags */
6690Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
6700Sstevel@tonic-gate 	int32_t		logbno = INT32_C(0);	/* will be fs_logbno */
6710Sstevel@tonic-gate 	struct inode	*ip = NULL;
6720Sstevel@tonic-gate 	size_t		nb = flp->nbytes_actual;
6730Sstevel@tonic-gate 	size_t		tb = 0;
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 	/*
6760Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE
6770Sstevel@tonic-gate 	 */
6780Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
6790Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
6800Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
6810Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
6820Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
6830Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	/*
6860Sstevel@tonic-gate 	 * Allocate the allocation block (need dummy shadow inode;
6870Sstevel@tonic-gate 	 * we use a shadow inode so the quota sub-system ignores
6880Sstevel@tonic-gate 	 * the block allocations.)
6890Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
6920Sstevel@tonic-gate 	ip->i_mode = IFSHAD;		/* make the dummy a shadow inode */
6930Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
6949915SOwen.Roberts@Sun.Com 	fno = contigpref(ufsvfsp, nb + fs->fs_bsize, minb);
6950Sstevel@tonic-gate 	error = alloc(ip, fno, fs->fs_bsize, &fno, cr);
6960Sstevel@tonic-gate 	if (error)
6970Sstevel@tonic-gate 		goto errout;
6980Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, fs->fs_bsize);
7010Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
7020Sstevel@tonic-gate 		error = EIO;
7030Sstevel@tonic-gate 		goto errout;
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
7070Sstevel@tonic-gate 	ebp->type = LUFS_EXTENTS;
7080Sstevel@tonic-gate 	ebp->nextbno = UINT32_C(0);
7090Sstevel@tonic-gate 	ebp->nextents = UINT32_C(0);
7100Sstevel@tonic-gate 	ebp->chksum = INT32_C(0);
7110Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
7120Sstevel@tonic-gate 		logbno = bno;
7130Sstevel@tonic-gate 	else
7140Sstevel@tonic-gate 		logbno = dbtofsb(fs, bno);
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	/*
7170Sstevel@tonic-gate 	 * Initialize the first extent
7180Sstevel@tonic-gate 	 */
7190Sstevel@tonic-gate 	ep = &ebp->extents[0];
7200Sstevel@tonic-gate 	error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
7210Sstevel@tonic-gate 	if (error)
7220Sstevel@tonic-gate 		goto errout;
7230Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	ep->lbno = UINT32_C(0);
7260Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
7270Sstevel@tonic-gate 		ep->pbno = (uint32_t)bno;
7280Sstevel@tonic-gate 	else
7290Sstevel@tonic-gate 		ep->pbno = (uint32_t)fno;
7300Sstevel@tonic-gate 	ep->nbno = (uint32_t)fsbtodb(fs, fs->fs_frag);
7310Sstevel@tonic-gate 	ebp->nextents = UINT32_C(1);
7320Sstevel@tonic-gate 	tb = fs->fs_bsize;
7330Sstevel@tonic-gate 	nb -= fs->fs_bsize;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 	while (nb) {
7360Sstevel@tonic-gate 		error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
7370Sstevel@tonic-gate 		if (error) {
7389915SOwen.Roberts@Sun.Com 			if (tb < minb)
7390Sstevel@tonic-gate 				goto errout;
7400Sstevel@tonic-gate 			error = 0;
7410Sstevel@tonic-gate 			break;
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 		bno = fsbtodb(fs, fno);
7440Sstevel@tonic-gate 		if ((daddr_t)((logbtodb(fs, ep->pbno) + ep->nbno) == bno))
7450Sstevel@tonic-gate 			ep->nbno += (uint32_t)(fsbtodb(fs, fs->fs_frag));
7460Sstevel@tonic-gate 		else {
7470Sstevel@tonic-gate 			nep = ep + 1;
7480Sstevel@tonic-gate 			if ((caddr_t)(nep + 1) >
7490Sstevel@tonic-gate 			    (bp->b_un.b_addr + fs->fs_bsize)) {
7500Sstevel@tonic-gate 				free(ip, fno, fs->fs_bsize, 0);
7510Sstevel@tonic-gate 				break;
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 			nep->lbno = ep->lbno + ep->nbno;
7540Sstevel@tonic-gate 			if (fs->fs_magic == FS_MAGIC)
7550Sstevel@tonic-gate 				nep->pbno = (uint32_t)bno;
7560Sstevel@tonic-gate 			else
7570Sstevel@tonic-gate 				nep->pbno = (uint32_t)fno;
7580Sstevel@tonic-gate 			nep->nbno = (uint32_t)(fsbtodb(fs, fs->fs_frag));
7590Sstevel@tonic-gate 			ebp->nextents++;
7600Sstevel@tonic-gate 			ep = nep;
7610Sstevel@tonic-gate 		}
7620Sstevel@tonic-gate 		tb += fs->fs_bsize;
7630Sstevel@tonic-gate 		nb -= fs->fs_bsize;
7640Sstevel@tonic-gate 	}
7659915SOwen.Roberts@Sun.Com 
7669915SOwen.Roberts@Sun.Com 	if (tb < minb) {	/* Failed to reach minimum log size */
7679915SOwen.Roberts@Sun.Com 		error = ENOSPC;
7689915SOwen.Roberts@Sun.Com 		goto errout;
7699915SOwen.Roberts@Sun.Com 	}
7709915SOwen.Roberts@Sun.Com 
7710Sstevel@tonic-gate 	ebp->nbytes = (uint32_t)tb;
7720Sstevel@tonic-gate 	setsum(&ebp->chksum, (int32_t *)bp->b_un.b_addr, fs->fs_bsize);
7730Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
7740Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
7750Sstevel@tonic-gate 		error = EIO;
7760Sstevel@tonic-gate 		goto errout;
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * Initialize the first two sectors of the log
7800Sstevel@tonic-gate 	 */
7810Sstevel@tonic-gate 	error = lufs_initialize(ufsvfsp, logbtodb(fs, ebp->extents[0].pbno),
7820Sstevel@tonic-gate 	    tb, flp);
7830Sstevel@tonic-gate 	if (error)
7840Sstevel@tonic-gate 		goto errout;
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	/*
7870Sstevel@tonic-gate 	 * We are done initializing the allocation block and the log
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	brelse(bp);
7900Sstevel@tonic-gate 	bp = NULL;
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/*
7930Sstevel@tonic-gate 	 * Update the superblock and push the dirty metadata
7940Sstevel@tonic-gate 	 */
7950Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
7960Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
7970Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
7980Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
7990Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 1);
8000Sstevel@tonic-gate 	if (error)
8010Sstevel@tonic-gate 		goto errout;
8020Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
8030Sstevel@tonic-gate 		error = EIO;
8040Sstevel@tonic-gate 		goto errout;
8050Sstevel@tonic-gate 	}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/*
8080Sstevel@tonic-gate 	 * Everything is safely on disk; update log space pointer in sb
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
8110Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
8120Sstevel@tonic-gate 	fs->fs_logbno = (uint32_t)logbno;
8130Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
8140Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
8150Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	/*
8180Sstevel@tonic-gate 	 * Free the dummy inode
8190Sstevel@tonic-gate 	 */
8200Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
8210Sstevel@tonic-gate 	ufs_free_inode(ip);
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	/* inform user of real log size */
8240Sstevel@tonic-gate 	flp->nbytes_actual = tb;
8250Sstevel@tonic-gate 	return (0);
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate errout:
8280Sstevel@tonic-gate 	/*
8290Sstevel@tonic-gate 	 * Free all resources
8300Sstevel@tonic-gate 	 */
8310Sstevel@tonic-gate 	if (bp)
8320Sstevel@tonic-gate 		brelse(bp);
8330Sstevel@tonic-gate 	if (logbno) {
8340Sstevel@tonic-gate 		fs->fs_logbno = logbno;
8350Sstevel@tonic-gate 		(void) lufs_free(ufsvfsp);
8360Sstevel@tonic-gate 	}
8370Sstevel@tonic-gate 	if (ip) {
8380Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
8390Sstevel@tonic-gate 		ufs_free_inode(ip);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 	return (error);
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /*
8450Sstevel@tonic-gate  * Disable logging
8460Sstevel@tonic-gate  */
8470Sstevel@tonic-gate int
lufs_disable(vnode_t * vp,struct fiolog * flp)8480Sstevel@tonic-gate lufs_disable(vnode_t *vp, struct fiolog *flp)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	int		error = 0;
8510Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
8520Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
8530Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
8540Sstevel@tonic-gate 	struct lockfs	lf;
8550Sstevel@tonic-gate 	struct ulockfs	*ulp;
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * Logging is already disabled; done
8610Sstevel@tonic-gate 	 */
8620Sstevel@tonic-gate 	if (fs->fs_logbno == 0 || ufsvfsp->vfs_log == NULL)
8630Sstevel@tonic-gate 		return (0);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * Readonly file system
8670Sstevel@tonic-gate 	 */
8680Sstevel@tonic-gate 	if (fs->fs_ronly) {
8690Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
8700Sstevel@tonic-gate 		return (0);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/*
8740Sstevel@tonic-gate 	 * File system must be write locked to disable logging
8750Sstevel@tonic-gate 	 */
8760Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
8770Sstevel@tonic-gate 	if (error) {
8780Sstevel@tonic-gate 		return (error);
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
8810Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
8820Sstevel@tonic-gate 		return (0);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
8850Sstevel@tonic-gate 	lf.lf_flags = 0;
8860Sstevel@tonic-gate 	lf.lf_comment = NULL;
8870Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
8880Sstevel@tonic-gate 	if (error) {
8890Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
8900Sstevel@tonic-gate 		return (0);
8910Sstevel@tonic-gate 	}
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	if (ufsvfsp->vfs_log == NULL || fs->fs_logbno == 0)
8940Sstevel@tonic-gate 		goto errout;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	/*
8970Sstevel@tonic-gate 	 * WE ARE COMMITTED TO DISABLING LOGGING PAST THIS POINT
8980Sstevel@tonic-gate 	 */
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/*
9010Sstevel@tonic-gate 	 * Disable logging:
9020Sstevel@tonic-gate 	 * Suspend the reclaim thread and force the delete thread to exit.
9030Sstevel@tonic-gate 	 *	When a nologging mount has completed there may still be
9040Sstevel@tonic-gate 	 *	work for reclaim to do so just suspend this thread until
9050Sstevel@tonic-gate 	 *	it's [deadlock-] safe for it to continue.  The delete
9060Sstevel@tonic-gate 	 *	thread won't be needed as ufs_iinactive() calls
9070Sstevel@tonic-gate 	 *	ufs_delete() when logging is disabled.
9080Sstevel@tonic-gate 	 * Freeze and drain reader ops.
9090Sstevel@tonic-gate 	 *	Commit any outstanding reader transactions (ufs_flush).
9100Sstevel@tonic-gate 	 *	Set the ``unmounted'' bit in the ufstrans struct.
9110Sstevel@tonic-gate 	 *	If debug, remove metadata from matamap.
9120Sstevel@tonic-gate 	 *	Disable matamap processing.
9130Sstevel@tonic-gate 	 *	NULL the trans ops table.
9140Sstevel@tonic-gate 	 *	Free all of the incore structs related to logging.
9150Sstevel@tonic-gate 	 * Allow reader ops.
9160Sstevel@tonic-gate 	 */
9170Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_reclaim);
9180Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_delete);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	vfs_lock_wait(ufsvfsp->vfs_vfs);
9210Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
9220Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
923329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
9240Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 	(void) ufs_flush(ufsvfsp->vfs_vfs);
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	TRANS_MATA_UMOUNT(ufsvfsp);
9290Sstevel@tonic-gate 	ufsvfsp->vfs_domatamap = 0;
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * Free all of the incore structs
933921Sbatschul 	 * Aquire the ufs_scan_lock before de-linking the mtm data
934921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
935921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
936921Sbatschul 	 * progress of enabling/disabling logging.
9370Sstevel@tonic-gate 	 */
938921Sbatschul 	mutex_enter(&ufs_scan_lock);
9390Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
940921Sbatschul 	mutex_exit(&ufs_scan_lock);
9410Sstevel@tonic-gate 
942329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
9430Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
9440Sstevel@tonic-gate 	vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_NOLOGGING, NULL, 0);
9450Sstevel@tonic-gate 	vfs_unlock(ufsvfsp->vfs_vfs);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	fs->fs_rolled = FS_ALL_ROLLED;
9480Sstevel@tonic-gate 	ufsvfsp->vfs_nolog_si = 0;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	/*
9510Sstevel@tonic-gate 	 * Free the log space and mark the superblock as FSACTIVE
9520Sstevel@tonic-gate 	 */
9530Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 	/*
9560Sstevel@tonic-gate 	 * Allow the reclaim thread to continue.
9570Sstevel@tonic-gate 	 */
9580Sstevel@tonic-gate 	ufs_thread_continue(&ufsvfsp->vfs_reclaim);
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	/*
9610Sstevel@tonic-gate 	 * Unlock the file system
9620Sstevel@tonic-gate 	 */
9630Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
9640Sstevel@tonic-gate 	lf.lf_flags = 0;
9650Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
9660Sstevel@tonic-gate 	if (error)
9670Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 	return (0);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate errout:
9720Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
9730Sstevel@tonic-gate 	lf.lf_flags = 0;
9740Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
9750Sstevel@tonic-gate 	return (error);
9760Sstevel@tonic-gate }
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate /*
9790Sstevel@tonic-gate  * Enable logging
9800Sstevel@tonic-gate  */
9810Sstevel@tonic-gate int
lufs_enable(struct vnode * vp,struct fiolog * flp,cred_t * cr)9820Sstevel@tonic-gate lufs_enable(struct vnode *vp, struct fiolog *flp, cred_t *cr)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	int		error;
9850Sstevel@tonic-gate 	int		reclaim;
9860Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
9870Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
9880Sstevel@tonic-gate 	struct fs	*fs;
9890Sstevel@tonic-gate 	ml_unit_t	*ul;
9900Sstevel@tonic-gate 	struct lockfs	lf;
9910Sstevel@tonic-gate 	struct ulockfs	*ulp;
9920Sstevel@tonic-gate 	vfs_t		*vfsp = ufsvfsp->vfs_vfs;
9930Sstevel@tonic-gate 	uint64_t	tmp_nbytes_actual;
9949915SOwen.Roberts@Sun.Com 	uint64_t	cg_minlogsize;
9959915SOwen.Roberts@Sun.Com 	uint32_t	cgsize;
9969915SOwen.Roberts@Sun.Com 	static int	minlogsizewarn = 0;
9979915SOwen.Roberts@Sun.Com 	static int	maxlogsizewarn = 0;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	/*
10000Sstevel@tonic-gate 	 * Check if logging is already enabled
10010Sstevel@tonic-gate 	 */
10020Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
10030Sstevel@tonic-gate 		flp->error = FIOLOG_ETRANS;
10040Sstevel@tonic-gate 		/* for root ensure logging option is set */
10050Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
10060Sstevel@tonic-gate 		return (0);
10070Sstevel@tonic-gate 	}
10080Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	/*
10110Sstevel@tonic-gate 	 * Come back here to recheck if we had to disable the log.
10120Sstevel@tonic-gate 	 */
10130Sstevel@tonic-gate recheck:
10140Sstevel@tonic-gate 	error = 0;
10150Sstevel@tonic-gate 	reclaim = 0;
10160Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 	/*
10199915SOwen.Roberts@Sun.Com 	 * The size of the ufs log is determined using the following rules:
10209915SOwen.Roberts@Sun.Com 	 *
10219915SOwen.Roberts@Sun.Com 	 * 1) If no size is requested the log size is calculated as a
10229915SOwen.Roberts@Sun.Com 	 *    ratio of the total file system size. By default this is
10239915SOwen.Roberts@Sun.Com 	 *    1MB of log per 1GB of file system. This calculation is then
10249915SOwen.Roberts@Sun.Com 	 *    capped at the log size specified by ldl_softlogcap.
10259915SOwen.Roberts@Sun.Com 	 * 2) The log size requested may then be increased based on the
10269915SOwen.Roberts@Sun.Com 	 *    number of cylinder groups contained in the file system.
10279915SOwen.Roberts@Sun.Com 	 *    To prevent a hang the log has to be large enough to contain a
10289915SOwen.Roberts@Sun.Com 	 *    single transaction that alters every cylinder group in the file
10299915SOwen.Roberts@Sun.Com 	 *    system. This is calculated as cg_minlogsize.
10309915SOwen.Roberts@Sun.Com 	 * 3) Finally a check is made that the log size requested is within
10319915SOwen.Roberts@Sun.Com 	 *    the limits of ldl_minlogsize and ldl_maxlogsize.
10329915SOwen.Roberts@Sun.Com 	 */
10339915SOwen.Roberts@Sun.Com 
10349915SOwen.Roberts@Sun.Com 	/*
10350Sstevel@tonic-gate 	 * Adjust requested log size
10360Sstevel@tonic-gate 	 */
10370Sstevel@tonic-gate 	flp->nbytes_actual = flp->nbytes_requested;
10380Sstevel@tonic-gate 	if (flp->nbytes_actual == 0) {
10390Sstevel@tonic-gate 		tmp_nbytes_actual =
10400Sstevel@tonic-gate 		    (((uint64_t)fs->fs_size) / ldl_divisor) << fs->fs_fshift;
10410Sstevel@tonic-gate 		flp->nbytes_actual = (uint_t)MIN(tmp_nbytes_actual, INT_MAX);
10429915SOwen.Roberts@Sun.Com 		/*
10439915SOwen.Roberts@Sun.Com 		 * The 1MB per 1GB log size allocation only applies up to
10449915SOwen.Roberts@Sun.Com 		 * ldl_softlogcap size of log.
10459915SOwen.Roberts@Sun.Com 		 */
10469915SOwen.Roberts@Sun.Com 		flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_softlogcap);
10470Sstevel@tonic-gate 	}
10489915SOwen.Roberts@Sun.Com 
10499915SOwen.Roberts@Sun.Com 	cgsize = ldl_cgsizereq ? ldl_cgsizereq : LDL_CGSIZEREQ(fs);
10509915SOwen.Roberts@Sun.Com 
10519915SOwen.Roberts@Sun.Com 	/*
10529915SOwen.Roberts@Sun.Com 	 * Determine the log size required based on the number of cylinder
10539915SOwen.Roberts@Sun.Com 	 * groups in the file system. The log has to be at least this size
10549915SOwen.Roberts@Sun.Com 	 * to prevent possible hangs due to log space exhaustion.
10559915SOwen.Roberts@Sun.Com 	 */
10569915SOwen.Roberts@Sun.Com 	cg_minlogsize = cgsize * fs->fs_ncg;
10579915SOwen.Roberts@Sun.Com 
10589915SOwen.Roberts@Sun.Com 	/*
10599915SOwen.Roberts@Sun.Com 	 * Ensure that the minimum log size isn't so small that it could lead
10609915SOwen.Roberts@Sun.Com 	 * to a full log hang.
10619915SOwen.Roberts@Sun.Com 	 */
10629915SOwen.Roberts@Sun.Com 	if (ldl_minlogsize < LDL_MINLOGSIZE) {
10639915SOwen.Roberts@Sun.Com 		ldl_minlogsize = LDL_MINLOGSIZE;
10649915SOwen.Roberts@Sun.Com 		if (!minlogsizewarn) {
10659915SOwen.Roberts@Sun.Com 			cmn_err(CE_WARN, "ldl_minlogsize too small, increasing "
10669915SOwen.Roberts@Sun.Com 			    "to 0x%x", LDL_MINLOGSIZE);
10679915SOwen.Roberts@Sun.Com 			minlogsizewarn = 1;
10689915SOwen.Roberts@Sun.Com 		}
10699915SOwen.Roberts@Sun.Com 	}
10709915SOwen.Roberts@Sun.Com 
10719915SOwen.Roberts@Sun.Com 	/*
10729915SOwen.Roberts@Sun.Com 	 * Ensure that the maximum log size isn't greater than INT_MAX as the
10739915SOwen.Roberts@Sun.Com 	 * logical log offset fields would overflow.
10749915SOwen.Roberts@Sun.Com 	 */
10759915SOwen.Roberts@Sun.Com 	if (ldl_maxlogsize > INT_MAX) {
10769915SOwen.Roberts@Sun.Com 		ldl_maxlogsize = INT_MAX;
10779915SOwen.Roberts@Sun.Com 		if (!maxlogsizewarn) {
10789915SOwen.Roberts@Sun.Com 			cmn_err(CE_WARN, "ldl_maxlogsize too large, reducing "
10799915SOwen.Roberts@Sun.Com 			    "to 0x%x", INT_MAX);
10809915SOwen.Roberts@Sun.Com 			maxlogsizewarn = 1;
10819915SOwen.Roberts@Sun.Com 		}
10829915SOwen.Roberts@Sun.Com 	}
10839915SOwen.Roberts@Sun.Com 
10849915SOwen.Roberts@Sun.Com 	if (cg_minlogsize > ldl_maxlogsize) {
10859915SOwen.Roberts@Sun.Com 		cmn_err(CE_WARN,
10869915SOwen.Roberts@Sun.Com 		    "%s: reducing calculated log size from 0x%x to "
10879915SOwen.Roberts@Sun.Com 		    "ldl_maxlogsize (0x%x).", fs->fs_fsmnt, (int)cg_minlogsize,
10889915SOwen.Roberts@Sun.Com 		    ldl_maxlogsize);
10899915SOwen.Roberts@Sun.Com 	}
10909915SOwen.Roberts@Sun.Com 
10919915SOwen.Roberts@Sun.Com 	cg_minlogsize = MAX(cg_minlogsize, ldl_minlogsize);
10929915SOwen.Roberts@Sun.Com 	cg_minlogsize = MIN(cg_minlogsize, ldl_maxlogsize);
10939915SOwen.Roberts@Sun.Com 
10949915SOwen.Roberts@Sun.Com 	flp->nbytes_actual = MAX(flp->nbytes_actual, cg_minlogsize);
10950Sstevel@tonic-gate 	flp->nbytes_actual = MAX(flp->nbytes_actual, ldl_minlogsize);
10960Sstevel@tonic-gate 	flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_maxlogsize);
10970Sstevel@tonic-gate 	flp->nbytes_actual = blkroundup(fs, flp->nbytes_actual);
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * logging is enabled and the log is the right size; done
11010Sstevel@tonic-gate 	 */
11020Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
11030Sstevel@tonic-gate 	if (ul && fs->fs_logbno && (flp->nbytes_actual == ul->un_requestsize))
11040Sstevel@tonic-gate 			return (0);
11050Sstevel@tonic-gate 
11060Sstevel@tonic-gate 	/*
11070Sstevel@tonic-gate 	 * Readonly file system
11080Sstevel@tonic-gate 	 */
11090Sstevel@tonic-gate 	if (fs->fs_ronly) {
11100Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
11110Sstevel@tonic-gate 		return (0);
11120Sstevel@tonic-gate 	}
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/*
11150Sstevel@tonic-gate 	 * File system must be write locked to enable logging
11160Sstevel@tonic-gate 	 */
11170Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
11180Sstevel@tonic-gate 	if (error) {
11190Sstevel@tonic-gate 		return (error);
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
11220Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
11230Sstevel@tonic-gate 		return (0);
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
11260Sstevel@tonic-gate 	lf.lf_flags = 0;
11270Sstevel@tonic-gate 	lf.lf_comment = NULL;
11280Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
11290Sstevel@tonic-gate 	if (error) {
11300Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
11310Sstevel@tonic-gate 		return (0);
11320Sstevel@tonic-gate 	}
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	/*
1135921Sbatschul 	 * Grab appropriate locks to synchronize with the rest
1136921Sbatschul 	 * of the system
1137921Sbatschul 	 */
1138921Sbatschul 	vfs_lock_wait(vfsp);
1139921Sbatschul 	ulp = &ufsvfsp->vfs_ulockfs;
1140921Sbatschul 	mutex_enter(&ulp->ul_lock);
1141921Sbatschul 
1142921Sbatschul 	/*
11430Sstevel@tonic-gate 	 * File system must be fairly consistent to enable logging
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	if (fs->fs_clean != FSLOG &&
11460Sstevel@tonic-gate 	    fs->fs_clean != FSACTIVE &&
11470Sstevel@tonic-gate 	    fs->fs_clean != FSSTABLE &&
11480Sstevel@tonic-gate 	    fs->fs_clean != FSCLEAN) {
11490Sstevel@tonic-gate 		flp->error = FIOLOG_ECLEAN;
11500Sstevel@tonic-gate 		goto unlockout;
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/*
11540Sstevel@tonic-gate 	 * A write-locked file system is only active if there are
11550Sstevel@tonic-gate 	 * open deleted files; so remember to set FS_RECLAIM later.
11560Sstevel@tonic-gate 	 */
11570Sstevel@tonic-gate 	if (fs->fs_clean == FSACTIVE)
11580Sstevel@tonic-gate 		reclaim = FS_RECLAIM;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/*
11610Sstevel@tonic-gate 	 * Logging is already enabled; must be changing the log's size
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (fs->fs_logbno && ufsvfsp->vfs_log) {
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * Before we can disable logging, we must give up our
11660Sstevel@tonic-gate 		 * lock.  As a consequence of unlocking and disabling the
11670Sstevel@tonic-gate 		 * log, the fs structure may change.  Because of this, when
11680Sstevel@tonic-gate 		 * disabling is complete, we will go back to recheck to
11690Sstevel@tonic-gate 		 * repeat all of the checks that we performed to get to
11700Sstevel@tonic-gate 		 * this point.  Disabling sets fs->fs_logbno to 0, so this
11710Sstevel@tonic-gate 		 * will not put us into an infinite loop.
11720Sstevel@tonic-gate 		 */
1173921Sbatschul 		mutex_exit(&ulp->ul_lock);
1174921Sbatschul 		vfs_unlock(vfsp);
1175921Sbatschul 
11760Sstevel@tonic-gate 		lf.lf_lock = LOCKFS_ULOCK;
11770Sstevel@tonic-gate 		lf.lf_flags = 0;
11780Sstevel@tonic-gate 		error = ufs_fiolfs(vp, &lf, 1);
11790Sstevel@tonic-gate 		if (error) {
11800Sstevel@tonic-gate 			flp->error = FIOLOG_ENOULOCK;
11810Sstevel@tonic-gate 			return (0);
11820Sstevel@tonic-gate 		}
11830Sstevel@tonic-gate 		error = lufs_disable(vp, flp);
11840Sstevel@tonic-gate 		if (error || (flp->error != FIOLOG_ENONE))
11850Sstevel@tonic-gate 			return (0);
11860Sstevel@tonic-gate 		goto recheck;
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 
11899915SOwen.Roberts@Sun.Com 	error = lufs_alloc(ufsvfsp, flp, cg_minlogsize, cr);
11900Sstevel@tonic-gate 	if (error)
11910Sstevel@tonic-gate 		goto errout;
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 	/*
11940Sstevel@tonic-gate 	 * Create all of the incore structs
11950Sstevel@tonic-gate 	 */
11960Sstevel@tonic-gate 	error = lufs_snarf(ufsvfsp, fs, 0);
11970Sstevel@tonic-gate 	if (error)
11980Sstevel@tonic-gate 		goto errout;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	/*
12010Sstevel@tonic-gate 	 * DON'T ``GOTO ERROUT'' PAST THIS POINT
12020Sstevel@tonic-gate 	 */
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	/*
12050Sstevel@tonic-gate 	 * Pretend we were just mounted with logging enabled
12060Sstevel@tonic-gate 	 *		Get the ops vector
12070Sstevel@tonic-gate 	 *		If debug, record metadata locations with log subsystem
12080Sstevel@tonic-gate 	 *		Start the delete thread
12090Sstevel@tonic-gate 	 *		Start the reclaim thread, if necessary
12100Sstevel@tonic-gate 	 */
12110Sstevel@tonic-gate 	vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	TRANS_DOMATAMAP(ufsvfsp);
12140Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
12150Sstevel@tonic-gate 	TRANS_MATA_SI(ufsvfsp, fs);
12160Sstevel@tonic-gate 	ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
12170Sstevel@tonic-gate 	if (fs->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
12180Sstevel@tonic-gate 		fs->fs_reclaim &= ~FS_RECLAIM;
12190Sstevel@tonic-gate 		fs->fs_reclaim |=  FS_RECLAIMING;
12200Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
12214662Sfrankho 		    ufs_thread_reclaim, vfsp);
12220Sstevel@tonic-gate 	} else
12230Sstevel@tonic-gate 		fs->fs_reclaim |= reclaim;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
12260Sstevel@tonic-gate 	vfs_unlock(vfsp);
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 	/*
12290Sstevel@tonic-gate 	 * Unlock the file system
12300Sstevel@tonic-gate 	 */
12310Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
12320Sstevel@tonic-gate 	lf.lf_flags = 0;
12330Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
12340Sstevel@tonic-gate 	if (error) {
12350Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
12360Sstevel@tonic-gate 		return (0);
12370Sstevel@tonic-gate 	}
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate 	/*
12400Sstevel@tonic-gate 	 * There's nothing in the log yet (we've just allocated it)
12410Sstevel@tonic-gate 	 * so directly write out the super block.
12420Sstevel@tonic-gate 	 * Note, we have to force this sb out to disk
12430Sstevel@tonic-gate 	 * (not just to the log) so that if we crash we know we are logging
12440Sstevel@tonic-gate 	 */
12450Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
12460Sstevel@tonic-gate 	fs->fs_clean = FSLOG;
12470Sstevel@tonic-gate 	fs->fs_rolled = FS_NEED_ROLL; /* Mark the fs as unrolled */
12480Sstevel@tonic-gate 	UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
12490Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	return (0);
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate errout:
1254921Sbatschul 	/*
1255921Sbatschul 	 * Aquire the ufs_scan_lock before de-linking the mtm data
1256921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
1257921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
1258921Sbatschul 	 * progress of enabling/disabling logging.
1259921Sbatschul 	 */
1260921Sbatschul 	mutex_enter(&ufs_scan_lock);
12610Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
1262921Sbatschul 	mutex_exit(&ufs_scan_lock);
1263921Sbatschul 
12640Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
12650Sstevel@tonic-gate unlockout:
1266921Sbatschul 	mutex_exit(&ulp->ul_lock);
1267921Sbatschul 	vfs_unlock(vfsp);
1268921Sbatschul 
12690Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
12700Sstevel@tonic-gate 	lf.lf_flags = 0;
12710Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
12720Sstevel@tonic-gate 	return (error);
12730Sstevel@tonic-gate }
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate void
lufs_read_strategy(ml_unit_t * ul,buf_t * bp)12760Sstevel@tonic-gate lufs_read_strategy(ml_unit_t *ul, buf_t *bp)
12770Sstevel@tonic-gate {
12780Sstevel@tonic-gate 	mt_map_t	*logmap	= ul->un_logmap;
12790Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
12800Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
12810Sstevel@tonic-gate 	mapentry_t	*age;
12820Sstevel@tonic-gate 	char		*va;
12830Sstevel@tonic-gate 	int		(*saviodone)();
12840Sstevel@tonic-gate 	int		entire_range;
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/*
12870Sstevel@tonic-gate 	 * get a linked list of overlapping deltas
12880Sstevel@tonic-gate 	 * returns with &mtm->mtm_rwlock held
12890Sstevel@tonic-gate 	 */
12900Sstevel@tonic-gate 	entire_range = logmap_list_get(logmap, mof, nb, &age);
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	/*
12930Sstevel@tonic-gate 	 * no overlapping deltas were found; read master
12940Sstevel@tonic-gate 	 */
12950Sstevel@tonic-gate 	if (age == NULL) {
12960Sstevel@tonic-gate 		rw_exit(&logmap->mtm_rwlock);
12970Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
12980Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
12990Sstevel@tonic-gate 			bp->b_error = EIO;
13000Sstevel@tonic-gate 			biodone(bp);
13010Sstevel@tonic-gate 		} else {
1302*11066Srafael.vanoni@sun.com 			ul->un_ufsvfs->vfs_iotstamp = ddi_get_lbolt();
13030Sstevel@tonic-gate 			logstats.ls_lreads.value.ui64++;
13040Sstevel@tonic-gate 			(void) bdev_strategy(bp);
13050Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 		return;
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	va = bp_mapin_common(bp, VM_SLEEP);
13110Sstevel@tonic-gate 	/*
13120Sstevel@tonic-gate 	 * if necessary, sync read the data from master
13130Sstevel@tonic-gate 	 *	errors are returned in bp
13140Sstevel@tonic-gate 	 */
13150Sstevel@tonic-gate 	if (!entire_range) {
13160Sstevel@tonic-gate 		saviodone = bp->b_iodone;
13170Sstevel@tonic-gate 		bp->b_iodone = trans_not_done;
13180Sstevel@tonic-gate 		logstats.ls_mreads.value.ui64++;
13190Sstevel@tonic-gate 		(void) bdev_strategy(bp);
13200Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
13210Sstevel@tonic-gate 		if (trans_not_wait(bp))
13220Sstevel@tonic-gate 			ldl_seterror(ul, "Error reading master");
13230Sstevel@tonic-gate 		bp->b_iodone = saviodone;
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * sync read the data from the log
13280Sstevel@tonic-gate 	 *	errors are returned inline
13290Sstevel@tonic-gate 	 */
13300Sstevel@tonic-gate 	if (ldl_read(ul, va, mof, nb, age)) {
13310Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
13320Sstevel@tonic-gate 		bp->b_error = EIO;
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * unlist the deltas
13370Sstevel@tonic-gate 	 */
13380Sstevel@tonic-gate 	logmap_list_put(logmap, age);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * all done
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
13440Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
13450Sstevel@tonic-gate 		bp->b_error = EIO;
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 	biodone(bp);
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate void
lufs_write_strategy(ml_unit_t * ul,buf_t * bp)13510Sstevel@tonic-gate lufs_write_strategy(ml_unit_t *ul, buf_t *bp)
13520Sstevel@tonic-gate {
13530Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
13540Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
13550Sstevel@tonic-gate 	char		*va;
13560Sstevel@tonic-gate 	mapentry_t	*me;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	ASSERT((nb & DEV_BMASK) == 0);
13590Sstevel@tonic-gate 	ul->un_logmap->mtm_ref = 1;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13620Sstevel@tonic-gate 	 * if there are deltas, move into log
13630Sstevel@tonic-gate 	 */
13640Sstevel@tonic-gate 	me = deltamap_remove(ul->un_deltamap, mof, nb);
13650Sstevel@tonic-gate 	if (me) {
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 		va = bp_mapin_common(bp, VM_SLEEP);
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		ASSERT(((ul->un_debug & MT_WRITE_CHECK) == 0) ||
13704662Sfrankho 		    (ul->un_matamap == NULL)||
13714662Sfrankho 		    matamap_within(ul->un_matamap, mof, nb));
13720Sstevel@tonic-gate 
13730Sstevel@tonic-gate 		/*
13740Sstevel@tonic-gate 		 * move to logmap
13750Sstevel@tonic-gate 		 */
13760Sstevel@tonic-gate 		if (ufs_crb_enable) {
13770Sstevel@tonic-gate 			logmap_add_buf(ul, va, mof, me,
13780Sstevel@tonic-gate 			    bp->b_un.b_addr, nb);
13790Sstevel@tonic-gate 		} else {
13800Sstevel@tonic-gate 			logmap_add(ul, va, mof, me);
13810Sstevel@tonic-gate 		}
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
13840Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
13850Sstevel@tonic-gate 			bp->b_error = EIO;
13860Sstevel@tonic-gate 		}
13870Sstevel@tonic-gate 		biodone(bp);
13880Sstevel@tonic-gate 		return;
13890Sstevel@tonic-gate 	}
13900Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
13910Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
13920Sstevel@tonic-gate 		bp->b_error = EIO;
13930Sstevel@tonic-gate 		biodone(bp);
13940Sstevel@tonic-gate 		return;
13950Sstevel@tonic-gate 	}
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 	/*
13980Sstevel@tonic-gate 	 * Check that we are not updating metadata, or if so then via B_PHYS.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	ASSERT((ul->un_matamap == NULL) ||
14014662Sfrankho 	    !(matamap_overlap(ul->un_matamap, mof, nb) &&
14024662Sfrankho 	    ((bp->b_flags & B_PHYS) == 0)));
14030Sstevel@tonic-gate 
1404*11066Srafael.vanoni@sun.com 	ul->un_ufsvfs->vfs_iotstamp = ddi_get_lbolt();
14050Sstevel@tonic-gate 	logstats.ls_lwrites.value.ui64++;
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate 	/* If snapshots are enabled, write through the snapshot driver */
14080Sstevel@tonic-gate 	if (ul->un_ufsvfs->vfs_snapshot)
14090Sstevel@tonic-gate 		fssnap_strategy(&ul->un_ufsvfs->vfs_snapshot, bp);
14100Sstevel@tonic-gate 	else
14110Sstevel@tonic-gate 		(void) bdev_strategy(bp);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	lwp_stat_update(LWP_STAT_OUBLK, 1);
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate void
lufs_strategy(ml_unit_t * ul,buf_t * bp)14170Sstevel@tonic-gate lufs_strategy(ml_unit_t *ul, buf_t *bp)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	if (bp->b_flags & B_READ)
14200Sstevel@tonic-gate 		lufs_read_strategy(ul, bp);
14210Sstevel@tonic-gate 	else
14220Sstevel@tonic-gate 		lufs_write_strategy(ul, bp);
14230Sstevel@tonic-gate }
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate /* ARGSUSED */
14260Sstevel@tonic-gate static int
delta_stats_update(kstat_t * ksp,int rw)14270Sstevel@tonic-gate delta_stats_update(kstat_t *ksp, int rw)
14280Sstevel@tonic-gate {
14290Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
14300Sstevel@tonic-gate 		delta_stats[DT_SB] = dkstats.ds_superblock_deltas.value.ui64;
14310Sstevel@tonic-gate 		delta_stats[DT_CG] = dkstats.ds_bitmap_deltas.value.ui64;
14320Sstevel@tonic-gate 		delta_stats[DT_SI] = dkstats.ds_suminfo_deltas.value.ui64;
14330Sstevel@tonic-gate 		delta_stats[DT_AB] = dkstats.ds_allocblk_deltas.value.ui64;
14340Sstevel@tonic-gate 		delta_stats[DT_ABZERO] = dkstats.ds_ab0_deltas.value.ui64;
14350Sstevel@tonic-gate 		delta_stats[DT_DIR] = dkstats.ds_dir_deltas.value.ui64;
14360Sstevel@tonic-gate 		delta_stats[DT_INODE] = dkstats.ds_inode_deltas.value.ui64;
14370Sstevel@tonic-gate 		delta_stats[DT_FBI] = dkstats.ds_fbiwrite_deltas.value.ui64;
14380Sstevel@tonic-gate 		delta_stats[DT_QR] = dkstats.ds_quota_deltas.value.ui64;
14390Sstevel@tonic-gate 		delta_stats[DT_SHAD] = dkstats.ds_shadow_deltas.value.ui64;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 		roll_stats[DT_SB] = dkstats.ds_superblock_rolled.value.ui64;
14420Sstevel@tonic-gate 		roll_stats[DT_CG] = dkstats.ds_bitmap_rolled.value.ui64;
14430Sstevel@tonic-gate 		roll_stats[DT_SI] = dkstats.ds_suminfo_rolled.value.ui64;
14440Sstevel@tonic-gate 		roll_stats[DT_AB] = dkstats.ds_allocblk_rolled.value.ui64;
14450Sstevel@tonic-gate 		roll_stats[DT_ABZERO] = dkstats.ds_ab0_rolled.value.ui64;
14460Sstevel@tonic-gate 		roll_stats[DT_DIR] = dkstats.ds_dir_rolled.value.ui64;
14470Sstevel@tonic-gate 		roll_stats[DT_INODE] = dkstats.ds_inode_rolled.value.ui64;
14480Sstevel@tonic-gate 		roll_stats[DT_FBI] = dkstats.ds_fbiwrite_rolled.value.ui64;
14490Sstevel@tonic-gate 		roll_stats[DT_QR] = dkstats.ds_quota_rolled.value.ui64;
14500Sstevel@tonic-gate 		roll_stats[DT_SHAD] = dkstats.ds_shadow_rolled.value.ui64;
14510Sstevel@tonic-gate 	} else {
14520Sstevel@tonic-gate 		dkstats.ds_superblock_deltas.value.ui64 = delta_stats[DT_SB];
14530Sstevel@tonic-gate 		dkstats.ds_bitmap_deltas.value.ui64 = delta_stats[DT_CG];
14540Sstevel@tonic-gate 		dkstats.ds_suminfo_deltas.value.ui64 = delta_stats[DT_SI];
14550Sstevel@tonic-gate 		dkstats.ds_allocblk_deltas.value.ui64 = delta_stats[DT_AB];
14560Sstevel@tonic-gate 		dkstats.ds_ab0_deltas.value.ui64 = delta_stats[DT_ABZERO];
14570Sstevel@tonic-gate 		dkstats.ds_dir_deltas.value.ui64 = delta_stats[DT_DIR];
14580Sstevel@tonic-gate 		dkstats.ds_inode_deltas.value.ui64 = delta_stats[DT_INODE];
14590Sstevel@tonic-gate 		dkstats.ds_fbiwrite_deltas.value.ui64 = delta_stats[DT_FBI];
14600Sstevel@tonic-gate 		dkstats.ds_quota_deltas.value.ui64 = delta_stats[DT_QR];
14610Sstevel@tonic-gate 		dkstats.ds_shadow_deltas.value.ui64 = delta_stats[DT_SHAD];
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 		dkstats.ds_superblock_rolled.value.ui64 = roll_stats[DT_SB];
14640Sstevel@tonic-gate 		dkstats.ds_bitmap_rolled.value.ui64 = roll_stats[DT_CG];
14650Sstevel@tonic-gate 		dkstats.ds_suminfo_rolled.value.ui64 = roll_stats[DT_SI];
14660Sstevel@tonic-gate 		dkstats.ds_allocblk_rolled.value.ui64 = roll_stats[DT_AB];
14670Sstevel@tonic-gate 		dkstats.ds_ab0_rolled.value.ui64 = roll_stats[DT_ABZERO];
14680Sstevel@tonic-gate 		dkstats.ds_dir_rolled.value.ui64 = roll_stats[DT_DIR];
14690Sstevel@tonic-gate 		dkstats.ds_inode_rolled.value.ui64 = roll_stats[DT_INODE];
14700Sstevel@tonic-gate 		dkstats.ds_fbiwrite_rolled.value.ui64 = roll_stats[DT_FBI];
14710Sstevel@tonic-gate 		dkstats.ds_quota_rolled.value.ui64 = roll_stats[DT_QR];
14720Sstevel@tonic-gate 		dkstats.ds_shadow_rolled.value.ui64 = roll_stats[DT_SHAD];
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 	return (0);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate extern size_t ufs_crb_limit;
14780Sstevel@tonic-gate extern int ufs_max_crb_divisor;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate void
lufs_init(void)14810Sstevel@tonic-gate lufs_init(void)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate 	kstat_t *ksp;
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	/* Create kmem caches */
14860Sstevel@tonic-gate 	lufs_sv = kmem_cache_create("lufs_save", sizeof (lufs_save_t), 0,
14870Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
14880Sstevel@tonic-gate 	lufs_bp = kmem_cache_create("lufs_bufs", sizeof (lufs_buf_t), 0,
14890Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	mutex_init(&log_mutex, NULL, MUTEX_DEFAULT, NULL);
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	_init_top();
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (&bio_lufs_strategy != NULL)
14960Sstevel@tonic-gate 		bio_lufs_strategy = (void (*) (void *, buf_t *)) lufs_strategy;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/*
14990Sstevel@tonic-gate 	 * Initialise general logging and delta kstats
15000Sstevel@tonic-gate 	 */
15010Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "logstats", "ufs", KSTAT_TYPE_NAMED,
15020Sstevel@tonic-gate 	    sizeof (logstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
15030Sstevel@tonic-gate 	if (ksp) {
15040Sstevel@tonic-gate 		ksp->ks_data = (void *) &logstats;
15050Sstevel@tonic-gate 		kstat_install(ksp);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "deltastats", "ufs", KSTAT_TYPE_NAMED,
15090Sstevel@tonic-gate 	    sizeof (dkstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
15100Sstevel@tonic-gate 	if (ksp) {
15110Sstevel@tonic-gate 		ksp->ks_data = (void *) &dkstats;
15120Sstevel@tonic-gate 		ksp->ks_update = delta_stats_update;
15130Sstevel@tonic-gate 		kstat_install(ksp);
15140Sstevel@tonic-gate 	}
15150Sstevel@tonic-gate 
15167455SWolfgang.Schremser@Sun.COM 	/* Initialize  generation of logging ids */
15177455SWolfgang.Schremser@Sun.COM 	lufs_genid_init();
15187455SWolfgang.Schremser@Sun.COM 
15190Sstevel@tonic-gate 	/*
15200Sstevel@tonic-gate 	 * Set up the maximum amount of kmem that the crbs (system wide)
15210Sstevel@tonic-gate 	 * can use.
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 	ufs_crb_limit = kmem_maxavail() / ufs_max_crb_divisor;
15240Sstevel@tonic-gate }
1525