xref: /onnv-gate/usr/src/uts/common/fs/ufs/lufs.c (revision 7455:61a9fc287cd0)
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 /*
22*7455SWolfgang.Schremser@Sun.COM  * Copyright 2008 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>
49*7455SWolfgang.Schremser@Sun.COM #include <sys/sdt.h>
50*7455SWolfgang.Schremser@Sun.COM 
51*7455SWolfgang.Schremser@Sun.COM #define	LUFS_GENID_PRIME	UINT64_C(4294967291)
52*7455SWolfgang.Schremser@Sun.COM #define	LUFS_GENID_BASE		UINT64_C(311)
53*7455SWolfgang.Schremser@Sun.COM #define	LUFS_NEXT_ID(id)	((uint32_t)(((id) * LUFS_GENID_BASE) % \
54*7455SWolfgang.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;
680Sstevel@tonic-gate uint32_t	ldl_divisor	= LDL_DIVISOR;
690Sstevel@tonic-gate uint32_t	ldl_mintransfer	= LDL_MINTRANSFER;
700Sstevel@tonic-gate uint32_t	ldl_maxtransfer	= LDL_MAXTRANSFER;
710Sstevel@tonic-gate uint32_t	ldl_minbufsize	= LDL_MINBUFSIZE;
720Sstevel@tonic-gate 
73*7455SWolfgang.Schremser@Sun.COM /* Generation of header ids */
74*7455SWolfgang.Schremser@Sun.COM kmutex_t	genid_mutex;
75*7455SWolfgang.Schremser@Sun.COM uint32_t	last_loghead_ident = UINT32_C(0);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Logging delta and roll statistics
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate struct delta_kstats {
810Sstevel@tonic-gate 	kstat_named_t ds_superblock_deltas;
820Sstevel@tonic-gate 	kstat_named_t ds_bitmap_deltas;
830Sstevel@tonic-gate 	kstat_named_t ds_suminfo_deltas;
840Sstevel@tonic-gate 	kstat_named_t ds_allocblk_deltas;
850Sstevel@tonic-gate 	kstat_named_t ds_ab0_deltas;
860Sstevel@tonic-gate 	kstat_named_t ds_dir_deltas;
870Sstevel@tonic-gate 	kstat_named_t ds_inode_deltas;
880Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_deltas;
890Sstevel@tonic-gate 	kstat_named_t ds_quota_deltas;
900Sstevel@tonic-gate 	kstat_named_t ds_shadow_deltas;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	kstat_named_t ds_superblock_rolled;
930Sstevel@tonic-gate 	kstat_named_t ds_bitmap_rolled;
940Sstevel@tonic-gate 	kstat_named_t ds_suminfo_rolled;
950Sstevel@tonic-gate 	kstat_named_t ds_allocblk_rolled;
960Sstevel@tonic-gate 	kstat_named_t ds_ab0_rolled;
970Sstevel@tonic-gate 	kstat_named_t ds_dir_rolled;
980Sstevel@tonic-gate 	kstat_named_t ds_inode_rolled;
990Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_rolled;
1000Sstevel@tonic-gate 	kstat_named_t ds_quota_rolled;
1010Sstevel@tonic-gate 	kstat_named_t ds_shadow_rolled;
1020Sstevel@tonic-gate } dkstats = {
1030Sstevel@tonic-gate 	{ "superblock_deltas",	KSTAT_DATA_UINT64 },
1040Sstevel@tonic-gate 	{ "bitmap_deltas",	KSTAT_DATA_UINT64 },
1050Sstevel@tonic-gate 	{ "suminfo_deltas",	KSTAT_DATA_UINT64 },
1060Sstevel@tonic-gate 	{ "allocblk_deltas",	KSTAT_DATA_UINT64 },
1070Sstevel@tonic-gate 	{ "ab0_deltas",		KSTAT_DATA_UINT64 },
1080Sstevel@tonic-gate 	{ "dir_deltas",		KSTAT_DATA_UINT64 },
1090Sstevel@tonic-gate 	{ "inode_deltas",	KSTAT_DATA_UINT64 },
1100Sstevel@tonic-gate 	{ "fbiwrite_deltas",	KSTAT_DATA_UINT64 },
1110Sstevel@tonic-gate 	{ "quota_deltas",	KSTAT_DATA_UINT64 },
1120Sstevel@tonic-gate 	{ "shadow_deltas",	KSTAT_DATA_UINT64 },
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	{ "superblock_rolled",	KSTAT_DATA_UINT64 },
1150Sstevel@tonic-gate 	{ "bitmap_rolled",	KSTAT_DATA_UINT64 },
1160Sstevel@tonic-gate 	{ "suminfo_rolled",	KSTAT_DATA_UINT64 },
1170Sstevel@tonic-gate 	{ "allocblk_rolled",	KSTAT_DATA_UINT64 },
1180Sstevel@tonic-gate 	{ "ab0_rolled",		KSTAT_DATA_UINT64 },
1190Sstevel@tonic-gate 	{ "dir_rolled",		KSTAT_DATA_UINT64 },
1200Sstevel@tonic-gate 	{ "inode_rolled",	KSTAT_DATA_UINT64 },
1210Sstevel@tonic-gate 	{ "fbiwrite_rolled",	KSTAT_DATA_UINT64 },
1220Sstevel@tonic-gate 	{ "quota_rolled",	KSTAT_DATA_UINT64 },
1230Sstevel@tonic-gate 	{ "shadow_rolled",	KSTAT_DATA_UINT64 }
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate uint64_t delta_stats[DT_MAX];
1270Sstevel@tonic-gate uint64_t roll_stats[DT_MAX];
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1300Sstevel@tonic-gate  * General logging kstats
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate struct logstats logstats = {
1330Sstevel@tonic-gate 	{ "master_reads",		KSTAT_DATA_UINT64 },
1340Sstevel@tonic-gate 	{ "master_writes",		KSTAT_DATA_UINT64 },
1350Sstevel@tonic-gate 	{ "log_reads_inmem",		KSTAT_DATA_UINT64 },
1360Sstevel@tonic-gate 	{ "log_reads",			KSTAT_DATA_UINT64 },
1370Sstevel@tonic-gate 	{ "log_writes",			KSTAT_DATA_UINT64 },
1380Sstevel@tonic-gate 	{ "log_master_reads",		KSTAT_DATA_UINT64 },
1390Sstevel@tonic-gate 	{ "log_roll_reads",		KSTAT_DATA_UINT64 },
1400Sstevel@tonic-gate 	{ "log_roll_writes",		KSTAT_DATA_UINT64 }
1410Sstevel@tonic-gate };
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate int
1440Sstevel@tonic-gate trans_not_done(struct buf *cb)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	sema_v(&cb->b_io);
1470Sstevel@tonic-gate 	return (0);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate static void
1510Sstevel@tonic-gate trans_wait_panic(struct buf *cb)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	while ((cb->b_flags & B_DONE) == 0)
1540Sstevel@tonic-gate 		drv_usecwait(10);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
1580Sstevel@tonic-gate trans_not_wait(struct buf *cb)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	/*
1610Sstevel@tonic-gate 	 * In case of panic, busy wait for completion
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	if (panicstr)
1640Sstevel@tonic-gate 		trans_wait_panic(cb);
1650Sstevel@tonic-gate 	else
1660Sstevel@tonic-gate 		sema_p(&cb->b_io);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	return (geterror(cb));
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate int
1720Sstevel@tonic-gate trans_wait(struct buf *cb)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	/*
1750Sstevel@tonic-gate 	 * In case of panic, busy wait for completion and run md daemon queues
1760Sstevel@tonic-gate 	 */
1770Sstevel@tonic-gate 	if (panicstr)
1780Sstevel@tonic-gate 		trans_wait_panic(cb);
1790Sstevel@tonic-gate 	return (biowait(cb));
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate static void
1830Sstevel@tonic-gate setsum(int32_t *sp, int32_t *lp, int nb)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	int32_t csum = 0;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	*sp = 0;
1880Sstevel@tonic-gate 	nb /= sizeof (int32_t);
1890Sstevel@tonic-gate 	while (nb--)
1900Sstevel@tonic-gate 		csum += *lp++;
1910Sstevel@tonic-gate 	*sp = csum;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate static int
1950Sstevel@tonic-gate checksum(int32_t *sp, int32_t *lp, int nb)
1960Sstevel@tonic-gate {
1970Sstevel@tonic-gate 	int32_t ssum = *sp;
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	setsum(sp, lp, nb);
2000Sstevel@tonic-gate 	if (ssum != *sp) {
2010Sstevel@tonic-gate 		*sp = ssum;
2020Sstevel@tonic-gate 		return (0);
2030Sstevel@tonic-gate 	}
2040Sstevel@tonic-gate 	return (1);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate void
2080Sstevel@tonic-gate lufs_unsnarf(ufsvfs_t *ufsvfsp)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	ml_unit_t *ul;
2110Sstevel@tonic-gate 	mt_map_t *mtm;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
2140Sstevel@tonic-gate 	if (ul == NULL)
2150Sstevel@tonic-gate 		return;
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	mtm = ul->un_logmap;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	/*
2200Sstevel@tonic-gate 	 * Wait for a pending top_issue_sync which is
2210Sstevel@tonic-gate 	 * dispatched (via taskq_dispatch()) but hasnt completed yet.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	mutex_enter(&mtm->mtm_lock);
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	while (mtm->mtm_taskq_sync_count != 0) {
2270Sstevel@tonic-gate 		cv_wait(&mtm->mtm_cv, &mtm->mtm_lock);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	mutex_exit(&mtm->mtm_lock);
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	/* Roll committed transactions */
2330Sstevel@tonic-gate 	logmap_roll_dev(ul);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* Kill the roll thread */
2360Sstevel@tonic-gate 	logmap_kill_roll(ul);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/* release saved alloction info */
2390Sstevel@tonic-gate 	if (ul->un_ebp)
2400Sstevel@tonic-gate 		kmem_free(ul->un_ebp, ul->un_nbeb);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/* release circular bufs */
2430Sstevel@tonic-gate 	free_cirbuf(&ul->un_rdbuf);
2440Sstevel@tonic-gate 	free_cirbuf(&ul->un_wrbuf);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/* release maps */
2470Sstevel@tonic-gate 	if (ul->un_logmap)
2480Sstevel@tonic-gate 		ul->un_logmap = map_put(ul->un_logmap);
2490Sstevel@tonic-gate 	if (ul->un_deltamap)
2500Sstevel@tonic-gate 		ul->un_deltamap = map_put(ul->un_deltamap);
2510Sstevel@tonic-gate 	if (ul->un_matamap)
2520Sstevel@tonic-gate 		ul->un_matamap = map_put(ul->un_matamap);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	mutex_destroy(&ul->un_log_mutex);
2550Sstevel@tonic-gate 	mutex_destroy(&ul->un_state_mutex);
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/* release state buffer MUST BE LAST!! (contains our ondisk data) */
2580Sstevel@tonic-gate 	if (ul->un_bp)
2590Sstevel@tonic-gate 		brelse(ul->un_bp);
2600Sstevel@tonic-gate 	kmem_free(ul, sizeof (*ul));
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	ufsvfsp->vfs_log = NULL;
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate int
2660Sstevel@tonic-gate lufs_snarf(ufsvfs_t *ufsvfsp, struct fs *fs, int ronly)
2670Sstevel@tonic-gate {
2680Sstevel@tonic-gate 	buf_t		*bp, *tbp;
2690Sstevel@tonic-gate 	ml_unit_t	*ul;
2700Sstevel@tonic-gate 	extent_block_t	*ebp;
2710Sstevel@tonic-gate 	ic_extent_block_t  *nebp;
2720Sstevel@tonic-gate 	size_t		nb;
2730Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
2740Sstevel@tonic-gate 	int		i;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
2770Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Get the allocation table
2810Sstevel@tonic-gate 	 *	During a remount the superblock pointed to by the ufsvfsp
2820Sstevel@tonic-gate 	 *	is out of date.  Hence the need for the ``new'' superblock
2830Sstevel@tonic-gate 	 *	pointer, fs, passed in as a parameter.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, fs->fs_logbno),
2860Sstevel@tonic-gate 	    fs->fs_bsize);
2870Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
2880Sstevel@tonic-gate 		brelse(bp);
2890Sstevel@tonic-gate 		return (EIO);
2900Sstevel@tonic-gate 	}
2910Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
2920Sstevel@tonic-gate 	if (!checksum(&ebp->chksum, (int32_t *)bp->b_un.b_addr,
2934662Sfrankho 	    fs->fs_bsize)) {
2940Sstevel@tonic-gate 		brelse(bp);
2950Sstevel@tonic-gate 		return (ENODEV);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * It is possible to get log blocks with all zeros.
3000Sstevel@tonic-gate 	 * We should also check for nextents to be zero in such case.
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0) {
3030Sstevel@tonic-gate 		brelse(bp);
3040Sstevel@tonic-gate 		return (EDOM);
3050Sstevel@tonic-gate 	}
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Put allocation into memory.  This requires conversion between
3080Sstevel@tonic-gate 	 * on the ondisk format of the extent (type extent_t) and the
3090Sstevel@tonic-gate 	 * in-core format of the extent (type ic_extent_t).  The
3100Sstevel@tonic-gate 	 * difference is the in-core form of the extent block stores
3110Sstevel@tonic-gate 	 * the physical offset of the extent in disk blocks, which
3120Sstevel@tonic-gate 	 * can require more than a 32-bit field.
3130Sstevel@tonic-gate 	 */
3140Sstevel@tonic-gate 	nb = (size_t)(sizeof (ic_extent_block_t) +
3154662Sfrankho 	    ((ebp->nextents - 1) * sizeof (ic_extent_t)));
3160Sstevel@tonic-gate 	nebp = kmem_alloc(nb, KM_SLEEP);
3170Sstevel@tonic-gate 	nebp->ic_nextents = ebp->nextents;
3180Sstevel@tonic-gate 	nebp->ic_nbytes = ebp->nbytes;
3190Sstevel@tonic-gate 	nebp->ic_nextbno = ebp->nextbno;
3200Sstevel@tonic-gate 	for (i = 0; i < ebp->nextents; i++) {
3210Sstevel@tonic-gate 		nebp->ic_extents[i].ic_lbno = ebp->extents[i].lbno;
3220Sstevel@tonic-gate 		nebp->ic_extents[i].ic_nbno = ebp->extents[i].nbno;
3230Sstevel@tonic-gate 		nebp->ic_extents[i].ic_pbno =
3240Sstevel@tonic-gate 		    logbtodb(fs, ebp->extents[i].pbno);
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	brelse(bp);
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/*
3290Sstevel@tonic-gate 	 * Get the log state
3300Sstevel@tonic-gate 	 */
3310Sstevel@tonic-gate 	bno = nebp->ic_extents[0].ic_pbno;
3320Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, DEV_BSIZE);
3330Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
3340Sstevel@tonic-gate 		brelse(bp);
3350Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno + 1, DEV_BSIZE);
3360Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
3370Sstevel@tonic-gate 			brelse(bp);
3380Sstevel@tonic-gate 			kmem_free(nebp, nb);
3390Sstevel@tonic-gate 			return (EIO);
3400Sstevel@tonic-gate 		}
3410Sstevel@tonic-gate 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * Put ondisk struct into an anonymous buffer
3450Sstevel@tonic-gate 	 *	This buffer will contain the memory for the ml_odunit struct
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	tbp = ngeteblk(dbtob(LS_SECTORS));
3480Sstevel@tonic-gate 	tbp->b_edev = bp->b_edev;
3490Sstevel@tonic-gate 	tbp->b_dev = bp->b_dev;
3500Sstevel@tonic-gate 	tbp->b_blkno = bno;
3510Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
3520Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
3530Sstevel@tonic-gate 	bp->b_flags |= (B_STALE | B_AGE);
3540Sstevel@tonic-gate 	brelse(bp);
3550Sstevel@tonic-gate 	bp = tbp;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/*
3580Sstevel@tonic-gate 	 * Verify the log state
3590Sstevel@tonic-gate 	 *
3600Sstevel@tonic-gate 	 * read/only mounts w/bad logs are allowed.  umount will
3610Sstevel@tonic-gate 	 * eventually roll the bad log until the first IO error.
3620Sstevel@tonic-gate 	 * fsck will then repair the file system.
3630Sstevel@tonic-gate 	 *
3640Sstevel@tonic-gate 	 * read/write mounts with bad logs are not allowed.
3650Sstevel@tonic-gate 	 *
3660Sstevel@tonic-gate 	 */
3670Sstevel@tonic-gate 	ul = (ml_unit_t *)kmem_zalloc(sizeof (*ul), KM_SLEEP);
3680Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, &ul->un_ondisk, sizeof (ml_odunit_t));
3690Sstevel@tonic-gate 	if ((ul->un_chksum != ul->un_head_ident + ul->un_tail_ident) ||
3700Sstevel@tonic-gate 	    (ul->un_version != LUFS_VERSION_LATEST) ||
3710Sstevel@tonic-gate 	    (!ronly && ul->un_badlog)) {
3720Sstevel@tonic-gate 		kmem_free(ul, sizeof (*ul));
3730Sstevel@tonic-gate 		brelse(bp);
3740Sstevel@tonic-gate 		kmem_free(nebp, nb);
3750Sstevel@tonic-gate 		return (EIO);
3760Sstevel@tonic-gate 	}
3770Sstevel@tonic-gate 	/*
3780Sstevel@tonic-gate 	 * Initialize the incore-only fields
3790Sstevel@tonic-gate 	 */
3800Sstevel@tonic-gate 	if (ronly)
3810Sstevel@tonic-gate 		ul->un_flags |= LDL_NOROLL;
3820Sstevel@tonic-gate 	ul->un_bp = bp;
3830Sstevel@tonic-gate 	ul->un_ufsvfs = ufsvfsp;
3840Sstevel@tonic-gate 	ul->un_dev = ufsvfsp->vfs_dev;
3850Sstevel@tonic-gate 	ul->un_ebp = nebp;
3860Sstevel@tonic-gate 	ul->un_nbeb = nb;
3870Sstevel@tonic-gate 	ul->un_maxresv = btodb(ul->un_logsize) * LDL_USABLE_BSIZE;
3880Sstevel@tonic-gate 	ul->un_deltamap = map_get(ul, deltamaptype, DELTAMAP_NHASH);
3890Sstevel@tonic-gate 	ul->un_logmap = map_get(ul, logmaptype, LOGMAP_NHASH);
3900Sstevel@tonic-gate 	if (ul->un_debug & MT_MATAMAP)
3910Sstevel@tonic-gate 		ul->un_matamap = map_get(ul, matamaptype, DELTAMAP_NHASH);
3920Sstevel@tonic-gate 	mutex_init(&ul->un_log_mutex, NULL, MUTEX_DEFAULT, NULL);
3930Sstevel@tonic-gate 	mutex_init(&ul->un_state_mutex, NULL, MUTEX_DEFAULT, NULL);
394921Sbatschul 
395921Sbatschul 	/*
396921Sbatschul 	 * Aquire the ufs_scan_lock before linking the mtm data
397921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
398921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
399921Sbatschul 	 * progress of enabling/disabling logging.
400921Sbatschul 	 */
401921Sbatschul 	mutex_enter(&ufs_scan_lock);
4020Sstevel@tonic-gate 	ufsvfsp->vfs_log = ul;
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate 	/* remember the state of the log before the log scan */
4050Sstevel@tonic-gate 	logmap_logscan(ul);
406921Sbatschul 	mutex_exit(&ufs_scan_lock);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	/*
4090Sstevel@tonic-gate 	 * Error during scan
4100Sstevel@tonic-gate 	 *
4110Sstevel@tonic-gate 	 * If this is a read/only mount; ignore the error.
4120Sstevel@tonic-gate 	 * At a later time umount/fsck will repair the fs.
4130Sstevel@tonic-gate 	 *
4140Sstevel@tonic-gate 	 */
4150Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
4160Sstevel@tonic-gate 		if (!ronly) {
417921Sbatschul 			/*
418921Sbatschul 			 * Aquire the ufs_scan_lock before de-linking
419921Sbatschul 			 * the mtm data structure so that we keep ufs_sync()
420921Sbatschul 			 * and ufs_update() away when they execute the
421921Sbatschul 			 * ufs_scan_inodes() run while we're in progress of
422921Sbatschul 			 * enabling/disabling logging.
423921Sbatschul 			 */
424921Sbatschul 			mutex_enter(&ufs_scan_lock);
4250Sstevel@tonic-gate 			lufs_unsnarf(ufsvfsp);
426921Sbatschul 			mutex_exit(&ufs_scan_lock);
4270Sstevel@tonic-gate 			return (EIO);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 		ul->un_flags &= ~LDL_ERROR;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	if (!ronly)
4320Sstevel@tonic-gate 		logmap_start_roll(ul);
4330Sstevel@tonic-gate 	return (0);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
436*7455SWolfgang.Schremser@Sun.COM uint32_t
437*7455SWolfgang.Schremser@Sun.COM lufs_hd_genid(const ml_unit_t *up)
438*7455SWolfgang.Schremser@Sun.COM {
439*7455SWolfgang.Schremser@Sun.COM 	uint32_t id;
440*7455SWolfgang.Schremser@Sun.COM 
441*7455SWolfgang.Schremser@Sun.COM 	mutex_enter(&genid_mutex);
442*7455SWolfgang.Schremser@Sun.COM 
443*7455SWolfgang.Schremser@Sun.COM 	/*
444*7455SWolfgang.Schremser@Sun.COM 	 * The formula below implements an exponential, modular sequence.
445*7455SWolfgang.Schremser@Sun.COM 	 *
446*7455SWolfgang.Schremser@Sun.COM 	 * ID(N) = (SEED * (BASE^N)) % PRIME
447*7455SWolfgang.Schremser@Sun.COM 	 *
448*7455SWolfgang.Schremser@Sun.COM 	 * The numbers will be pseudo random.  They depend on SEED, BASE, PRIME,
449*7455SWolfgang.Schremser@Sun.COM 	 * but will sweep through almost all of the range 1....PRIME-1.
450*7455SWolfgang.Schremser@Sun.COM 	 * Most  importantly  they  will  not  repeat  for PRIME-2 (4294967289)
451*7455SWolfgang.Schremser@Sun.COM 	 * repetitions.  If they would repeat that  could possibly cause  hangs,
452*7455SWolfgang.Schremser@Sun.COM 	 * panics at mount/umount and failed mount operations.
453*7455SWolfgang.Schremser@Sun.COM 	 */
454*7455SWolfgang.Schremser@Sun.COM 	id = LUFS_NEXT_ID(last_loghead_ident);
455*7455SWolfgang.Schremser@Sun.COM 
456*7455SWolfgang.Schremser@Sun.COM 	/* Checking if new identity used already */
457*7455SWolfgang.Schremser@Sun.COM 	if (up != NULL && up->un_head_ident == id) {
458*7455SWolfgang.Schremser@Sun.COM 		DTRACE_PROBE1(head_ident_collision, uint32_t, id);
459*7455SWolfgang.Schremser@Sun.COM 
460*7455SWolfgang.Schremser@Sun.COM 		/*
461*7455SWolfgang.Schremser@Sun.COM 		 * The  following  preserves  the  algorithm  for  the fix  for
462*7455SWolfgang.Schremser@Sun.COM 		 * "panic: free: freeing free frag, dev:0x2000000018, blk:34605,
463*7455SWolfgang.Schremser@Sun.COM 		 * cg:26, ino:148071,".
464*7455SWolfgang.Schremser@Sun.COM 		 * If  the header identities  un_head_ident  are  equal  to the
465*7455SWolfgang.Schremser@Sun.COM 		 * present element  in the sequence,  the next element  of  the
466*7455SWolfgang.Schremser@Sun.COM 		 * sequence is returned instead.
467*7455SWolfgang.Schremser@Sun.COM 		 */
468*7455SWolfgang.Schremser@Sun.COM 		id = LUFS_NEXT_ID(id);
469*7455SWolfgang.Schremser@Sun.COM 	}
470*7455SWolfgang.Schremser@Sun.COM 
471*7455SWolfgang.Schremser@Sun.COM 	last_loghead_ident = id;
472*7455SWolfgang.Schremser@Sun.COM 
473*7455SWolfgang.Schremser@Sun.COM 	mutex_exit(&genid_mutex);
474*7455SWolfgang.Schremser@Sun.COM 
475*7455SWolfgang.Schremser@Sun.COM 	return (id);
476*7455SWolfgang.Schremser@Sun.COM }
477*7455SWolfgang.Schremser@Sun.COM 
478*7455SWolfgang.Schremser@Sun.COM static void
479*7455SWolfgang.Schremser@Sun.COM lufs_genid_init(void)
480*7455SWolfgang.Schremser@Sun.COM {
481*7455SWolfgang.Schremser@Sun.COM 	uint64_t seed;
482*7455SWolfgang.Schremser@Sun.COM 
483*7455SWolfgang.Schremser@Sun.COM 	/* Initialization */
484*7455SWolfgang.Schremser@Sun.COM 	mutex_init(&genid_mutex, NULL, MUTEX_DEFAULT, NULL);
485*7455SWolfgang.Schremser@Sun.COM 
486*7455SWolfgang.Schremser@Sun.COM 	/* Seed the algorithm */
487*7455SWolfgang.Schremser@Sun.COM 	do {
488*7455SWolfgang.Schremser@Sun.COM 		timestruc_t tv;
489*7455SWolfgang.Schremser@Sun.COM 
490*7455SWolfgang.Schremser@Sun.COM 		gethrestime(&tv);
491*7455SWolfgang.Schremser@Sun.COM 
492*7455SWolfgang.Schremser@Sun.COM 		seed = (tv.tv_nsec << 3);
493*7455SWolfgang.Schremser@Sun.COM 		seed ^= tv.tv_sec;
494*7455SWolfgang.Schremser@Sun.COM 
495*7455SWolfgang.Schremser@Sun.COM 		last_loghead_ident = (uint32_t)(seed % LUFS_GENID_PRIME);
496*7455SWolfgang.Schremser@Sun.COM 	} while (last_loghead_ident == UINT32_C(0));
497*7455SWolfgang.Schremser@Sun.COM }
498*7455SWolfgang.Schremser@Sun.COM 
4990Sstevel@tonic-gate static int
5000Sstevel@tonic-gate lufs_initialize(
5010Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp,
5020Sstevel@tonic-gate 	daddr_t bno,
5030Sstevel@tonic-gate 	size_t nb,
5040Sstevel@tonic-gate 	struct fiolog *flp)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	ml_odunit_t	*ud, *ud2;
5070Sstevel@tonic-gate 	buf_t		*bp;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
5100Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
5110Sstevel@tonic-gate 	ASSERT(nb >= ldl_minlogsize);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev, bno, dbtob(LS_SECTORS));
5140Sstevel@tonic-gate 	bzero(bp->b_un.b_addr, bp->b_bcount);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	ud = (void *)bp->b_un.b_addr;
5170Sstevel@tonic-gate 	ud->od_version = LUFS_VERSION_LATEST;
5180Sstevel@tonic-gate 	ud->od_maxtransfer = MIN(ufsvfsp->vfs_iotransz, ldl_maxtransfer);
5190Sstevel@tonic-gate 	if (ud->od_maxtransfer < ldl_mintransfer)
5200Sstevel@tonic-gate 		ud->od_maxtransfer = ldl_mintransfer;
5210Sstevel@tonic-gate 	ud->od_devbsize = DEV_BSIZE;
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	ud->od_requestsize = flp->nbytes_actual;
5240Sstevel@tonic-gate 	ud->od_statesize = dbtob(LS_SECTORS);
5250Sstevel@tonic-gate 	ud->od_logsize = nb - ud->od_statesize;
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	ud->od_statebno = INT32_C(0);
5280Sstevel@tonic-gate 
529*7455SWolfgang.Schremser@Sun.COM 	ud->od_head_ident = lufs_hd_genid(NULL);
5300Sstevel@tonic-gate 	ud->od_tail_ident = ud->od_head_ident;
5310Sstevel@tonic-gate 	ud->od_chksum = ud->od_head_ident + ud->od_tail_ident;
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 	ud->od_bol_lof = dbtob(ud->od_statebno) + ud->od_statesize;
5340Sstevel@tonic-gate 	ud->od_eol_lof = ud->od_bol_lof + ud->od_logsize;
5350Sstevel@tonic-gate 	ud->od_head_lof = ud->od_bol_lof;
5360Sstevel@tonic-gate 	ud->od_tail_lof = ud->od_bol_lof;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	ASSERT(lufs_initialize_debug(ud));
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	ud2 = (void *)(bp->b_un.b_addr + DEV_BSIZE);
5410Sstevel@tonic-gate 	bcopy(ud, ud2, sizeof (*ud));
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
5440Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
5450Sstevel@tonic-gate 		brelse(bp);
5460Sstevel@tonic-gate 		return (EIO);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	brelse(bp);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	return (0);
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate /*
5540Sstevel@tonic-gate  * Free log space
5550Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
5560Sstevel@tonic-gate  */
5570Sstevel@tonic-gate static int
5580Sstevel@tonic-gate lufs_free(struct ufsvfs *ufsvfsp)
5590Sstevel@tonic-gate {
5600Sstevel@tonic-gate 	int		error = 0, i, j;
5610Sstevel@tonic-gate 	buf_t		*bp = NULL;
5620Sstevel@tonic-gate 	extent_t	*ep;
5630Sstevel@tonic-gate 	extent_block_t	*ebp;
5640Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
5650Sstevel@tonic-gate 	daddr_t		fno;
5660Sstevel@tonic-gate 	int32_t		logbno;
5670Sstevel@tonic-gate 	long		nfno;
5680Sstevel@tonic-gate 	inode_t		*ip = NULL;
5690Sstevel@tonic-gate 	char		clean;
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	/*
5720Sstevel@tonic-gate 	 * Nothing to free
5730Sstevel@tonic-gate 	 */
5740Sstevel@tonic-gate 	if (fs->fs_logbno == 0)
5750Sstevel@tonic-gate 		return (0);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	/*
5780Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE and no log but honor the
5790Sstevel@tonic-gate 	 * current value of fs_reclaim.  The reclaim thread could have
5800Sstevel@tonic-gate 	 * been active when lufs_disable() was called and if fs_reclaim
5810Sstevel@tonic-gate 	 * is reset to zero here it could lead to lost inodes.
5820Sstevel@tonic-gate 	 */
5830Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
5840Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
5850Sstevel@tonic-gate 	clean = fs->fs_clean;
5860Sstevel@tonic-gate 	logbno = fs->fs_logbno;
5870Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
5880Sstevel@tonic-gate 	fs->fs_logbno = INT32_C(0);
5890Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
5900Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
5910Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
5920Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
5930Sstevel@tonic-gate 		error = EIO;
5940Sstevel@tonic-gate 		fs->fs_clean = clean;
5950Sstevel@tonic-gate 		fs->fs_logbno = logbno;
5960Sstevel@tonic-gate 		goto errout;
5970Sstevel@tonic-gate 	}
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	/*
6000Sstevel@tonic-gate 	 * fetch the allocation block
6010Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
6020Sstevel@tonic-gate 	 */
6030Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, logbno),
6040Sstevel@tonic-gate 	    fs->fs_bsize);
6050Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6060Sstevel@tonic-gate 		error = EIO;
6070Sstevel@tonic-gate 		goto errout;
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * Free up the allocated space (dummy inode needed for free())
6120Sstevel@tonic-gate 	 */
6130Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
6140Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
6150Sstevel@tonic-gate 	for (i = 0, ep = &ebp->extents[0]; i < ebp->nextents; ++i, ++ep) {
6160Sstevel@tonic-gate 		fno = logbtofrag(fs, ep->pbno);
6170Sstevel@tonic-gate 		nfno = dbtofsb(fs, ep->nbno);
6180Sstevel@tonic-gate 		for (j = 0; j < nfno; j += fs->fs_frag, fno += fs->fs_frag)
6190Sstevel@tonic-gate 			free(ip, fno, fs->fs_bsize, 0);
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 	free(ip, logbtofrag(fs, logbno), fs->fs_bsize, 0);
6220Sstevel@tonic-gate 	brelse(bp);
6230Sstevel@tonic-gate 	bp = NULL;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	/*
6260Sstevel@tonic-gate 	 * Push the metadata dirtied during the allocations
6270Sstevel@tonic-gate 	 */
6280Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
6290Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
6300Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
6310Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
6320Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 0);
6330Sstevel@tonic-gate 	if (error)
6340Sstevel@tonic-gate 		goto errout;
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	/*
6370Sstevel@tonic-gate 	 * Free the dummy inode
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 	ufs_free_inode(ip);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	return (0);
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate errout:
6440Sstevel@tonic-gate 	/*
6450Sstevel@tonic-gate 	 * Free up all resources
6460Sstevel@tonic-gate 	 */
6470Sstevel@tonic-gate 	if (bp)
6480Sstevel@tonic-gate 		brelse(bp);
6490Sstevel@tonic-gate 	if (ip)
6500Sstevel@tonic-gate 		ufs_free_inode(ip);
6510Sstevel@tonic-gate 	return (error);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate /*
6550Sstevel@tonic-gate  * Allocate log space
6560Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
6570Sstevel@tonic-gate  */
6580Sstevel@tonic-gate static int
6590Sstevel@tonic-gate lufs_alloc(struct ufsvfs *ufsvfsp, struct fiolog *flp, cred_t *cr)
6600Sstevel@tonic-gate {
6610Sstevel@tonic-gate 	int		error = 0;
6620Sstevel@tonic-gate 	buf_t		*bp = NULL;
6630Sstevel@tonic-gate 	extent_t	*ep, *nep;
6640Sstevel@tonic-gate 	extent_block_t	*ebp;
6650Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
6660Sstevel@tonic-gate 	daddr_t		fno;	/* in frags */
6670Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
6680Sstevel@tonic-gate 	int32_t		logbno = INT32_C(0);	/* will be fs_logbno */
6690Sstevel@tonic-gate 	struct inode	*ip = NULL;
6700Sstevel@tonic-gate 	size_t		nb = flp->nbytes_actual;
6710Sstevel@tonic-gate 	size_t		tb = 0;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/*
6740Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE
6750Sstevel@tonic-gate 	 */
6760Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
6770Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
6780Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
6790Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
6800Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
6810Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * Allocate the allocation block (need dummy shadow inode;
6850Sstevel@tonic-gate 	 * we use a shadow inode so the quota sub-system ignores
6860Sstevel@tonic-gate 	 * the block allocations.)
6870Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
6880Sstevel@tonic-gate 	 */
6890Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
6900Sstevel@tonic-gate 	ip->i_mode = IFSHAD;		/* make the dummy a shadow inode */
6910Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
6920Sstevel@tonic-gate 	fno = contigpref(ufsvfsp, nb + fs->fs_bsize);
6930Sstevel@tonic-gate 	error = alloc(ip, fno, fs->fs_bsize, &fno, cr);
6940Sstevel@tonic-gate 	if (error)
6950Sstevel@tonic-gate 		goto errout;
6960Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, fs->fs_bsize);
6990Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
7000Sstevel@tonic-gate 		error = EIO;
7010Sstevel@tonic-gate 		goto errout;
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
7050Sstevel@tonic-gate 	ebp->type = LUFS_EXTENTS;
7060Sstevel@tonic-gate 	ebp->nextbno = UINT32_C(0);
7070Sstevel@tonic-gate 	ebp->nextents = UINT32_C(0);
7080Sstevel@tonic-gate 	ebp->chksum = INT32_C(0);
7090Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
7100Sstevel@tonic-gate 		logbno = bno;
7110Sstevel@tonic-gate 	else
7120Sstevel@tonic-gate 		logbno = dbtofsb(fs, bno);
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/*
7150Sstevel@tonic-gate 	 * Initialize the first extent
7160Sstevel@tonic-gate 	 */
7170Sstevel@tonic-gate 	ep = &ebp->extents[0];
7180Sstevel@tonic-gate 	error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
7190Sstevel@tonic-gate 	if (error)
7200Sstevel@tonic-gate 		goto errout;
7210Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	ep->lbno = UINT32_C(0);
7240Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
7250Sstevel@tonic-gate 		ep->pbno = (uint32_t)bno;
7260Sstevel@tonic-gate 	else
7270Sstevel@tonic-gate 		ep->pbno = (uint32_t)fno;
7280Sstevel@tonic-gate 	ep->nbno = (uint32_t)fsbtodb(fs, fs->fs_frag);
7290Sstevel@tonic-gate 	ebp->nextents = UINT32_C(1);
7300Sstevel@tonic-gate 	tb = fs->fs_bsize;
7310Sstevel@tonic-gate 	nb -= fs->fs_bsize;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	while (nb) {
7340Sstevel@tonic-gate 		error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
7350Sstevel@tonic-gate 		if (error) {
7360Sstevel@tonic-gate 			if (tb < ldl_minlogsize)
7370Sstevel@tonic-gate 				goto errout;
7380Sstevel@tonic-gate 			error = 0;
7390Sstevel@tonic-gate 			break;
7400Sstevel@tonic-gate 		}
7410Sstevel@tonic-gate 		bno = fsbtodb(fs, fno);
7420Sstevel@tonic-gate 		if ((daddr_t)((logbtodb(fs, ep->pbno) + ep->nbno) == bno))
7430Sstevel@tonic-gate 			ep->nbno += (uint32_t)(fsbtodb(fs, fs->fs_frag));
7440Sstevel@tonic-gate 		else {
7450Sstevel@tonic-gate 			nep = ep + 1;
7460Sstevel@tonic-gate 			if ((caddr_t)(nep + 1) >
7470Sstevel@tonic-gate 			    (bp->b_un.b_addr + fs->fs_bsize)) {
7480Sstevel@tonic-gate 				free(ip, fno, fs->fs_bsize, 0);
7490Sstevel@tonic-gate 				break;
7500Sstevel@tonic-gate 			}
7510Sstevel@tonic-gate 			nep->lbno = ep->lbno + ep->nbno;
7520Sstevel@tonic-gate 			if (fs->fs_magic == FS_MAGIC)
7530Sstevel@tonic-gate 				nep->pbno = (uint32_t)bno;
7540Sstevel@tonic-gate 			else
7550Sstevel@tonic-gate 				nep->pbno = (uint32_t)fno;
7560Sstevel@tonic-gate 			nep->nbno = (uint32_t)(fsbtodb(fs, fs->fs_frag));
7570Sstevel@tonic-gate 			ebp->nextents++;
7580Sstevel@tonic-gate 			ep = nep;
7590Sstevel@tonic-gate 		}
7600Sstevel@tonic-gate 		tb += fs->fs_bsize;
7610Sstevel@tonic-gate 		nb -= fs->fs_bsize;
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 	ebp->nbytes = (uint32_t)tb;
7640Sstevel@tonic-gate 	setsum(&ebp->chksum, (int32_t *)bp->b_un.b_addr, fs->fs_bsize);
7650Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
7660Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
7670Sstevel@tonic-gate 		error = EIO;
7680Sstevel@tonic-gate 		goto errout;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	/*
7710Sstevel@tonic-gate 	 * Initialize the first two sectors of the log
7720Sstevel@tonic-gate 	 */
7730Sstevel@tonic-gate 	error = lufs_initialize(ufsvfsp, logbtodb(fs, ebp->extents[0].pbno),
7740Sstevel@tonic-gate 	    tb, flp);
7750Sstevel@tonic-gate 	if (error)
7760Sstevel@tonic-gate 		goto errout;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/*
7790Sstevel@tonic-gate 	 * We are done initializing the allocation block and the log
7800Sstevel@tonic-gate 	 */
7810Sstevel@tonic-gate 	brelse(bp);
7820Sstevel@tonic-gate 	bp = NULL;
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * Update the superblock and push the dirty metadata
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
7880Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
7890Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
7900Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
7910Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 1);
7920Sstevel@tonic-gate 	if (error)
7930Sstevel@tonic-gate 		goto errout;
7940Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
7950Sstevel@tonic-gate 		error = EIO;
7960Sstevel@tonic-gate 		goto errout;
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	/*
8000Sstevel@tonic-gate 	 * Everything is safely on disk; update log space pointer in sb
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
8030Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
8040Sstevel@tonic-gate 	fs->fs_logbno = (uint32_t)logbno;
8050Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
8060Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
8070Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	/*
8100Sstevel@tonic-gate 	 * Free the dummy inode
8110Sstevel@tonic-gate 	 */
8120Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
8130Sstevel@tonic-gate 	ufs_free_inode(ip);
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/* inform user of real log size */
8160Sstevel@tonic-gate 	flp->nbytes_actual = tb;
8170Sstevel@tonic-gate 	return (0);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate errout:
8200Sstevel@tonic-gate 	/*
8210Sstevel@tonic-gate 	 * Free all resources
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	if (bp)
8240Sstevel@tonic-gate 		brelse(bp);
8250Sstevel@tonic-gate 	if (logbno) {
8260Sstevel@tonic-gate 		fs->fs_logbno = logbno;
8270Sstevel@tonic-gate 		(void) lufs_free(ufsvfsp);
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	if (ip) {
8300Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
8310Sstevel@tonic-gate 		ufs_free_inode(ip);
8320Sstevel@tonic-gate 	}
8330Sstevel@tonic-gate 	return (error);
8340Sstevel@tonic-gate }
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate /*
8370Sstevel@tonic-gate  * Disable logging
8380Sstevel@tonic-gate  */
8390Sstevel@tonic-gate int
8400Sstevel@tonic-gate lufs_disable(vnode_t *vp, struct fiolog *flp)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	int		error = 0;
8430Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
8440Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
8450Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
8460Sstevel@tonic-gate 	struct lockfs	lf;
8470Sstevel@tonic-gate 	struct ulockfs	*ulp;
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	/*
8520Sstevel@tonic-gate 	 * Logging is already disabled; done
8530Sstevel@tonic-gate 	 */
8540Sstevel@tonic-gate 	if (fs->fs_logbno == 0 || ufsvfsp->vfs_log == NULL)
8550Sstevel@tonic-gate 		return (0);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	/*
8580Sstevel@tonic-gate 	 * Readonly file system
8590Sstevel@tonic-gate 	 */
8600Sstevel@tonic-gate 	if (fs->fs_ronly) {
8610Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
8620Sstevel@tonic-gate 		return (0);
8630Sstevel@tonic-gate 	}
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/*
8660Sstevel@tonic-gate 	 * File system must be write locked to disable logging
8670Sstevel@tonic-gate 	 */
8680Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
8690Sstevel@tonic-gate 	if (error) {
8700Sstevel@tonic-gate 		return (error);
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
8730Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
8740Sstevel@tonic-gate 		return (0);
8750Sstevel@tonic-gate 	}
8760Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
8770Sstevel@tonic-gate 	lf.lf_flags = 0;
8780Sstevel@tonic-gate 	lf.lf_comment = NULL;
8790Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
8800Sstevel@tonic-gate 	if (error) {
8810Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
8820Sstevel@tonic-gate 		return (0);
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (ufsvfsp->vfs_log == NULL || fs->fs_logbno == 0)
8860Sstevel@tonic-gate 		goto errout;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 	/*
8890Sstevel@tonic-gate 	 * WE ARE COMMITTED TO DISABLING LOGGING PAST THIS POINT
8900Sstevel@tonic-gate 	 */
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/*
8930Sstevel@tonic-gate 	 * Disable logging:
8940Sstevel@tonic-gate 	 * Suspend the reclaim thread and force the delete thread to exit.
8950Sstevel@tonic-gate 	 *	When a nologging mount has completed there may still be
8960Sstevel@tonic-gate 	 *	work for reclaim to do so just suspend this thread until
8970Sstevel@tonic-gate 	 *	it's [deadlock-] safe for it to continue.  The delete
8980Sstevel@tonic-gate 	 *	thread won't be needed as ufs_iinactive() calls
8990Sstevel@tonic-gate 	 *	ufs_delete() when logging is disabled.
9000Sstevel@tonic-gate 	 * Freeze and drain reader ops.
9010Sstevel@tonic-gate 	 *	Commit any outstanding reader transactions (ufs_flush).
9020Sstevel@tonic-gate 	 *	Set the ``unmounted'' bit in the ufstrans struct.
9030Sstevel@tonic-gate 	 *	If debug, remove metadata from matamap.
9040Sstevel@tonic-gate 	 *	Disable matamap processing.
9050Sstevel@tonic-gate 	 *	NULL the trans ops table.
9060Sstevel@tonic-gate 	 *	Free all of the incore structs related to logging.
9070Sstevel@tonic-gate 	 * Allow reader ops.
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_reclaim);
9100Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_delete);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	vfs_lock_wait(ufsvfsp->vfs_vfs);
9130Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
9140Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
915329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
9160Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	(void) ufs_flush(ufsvfsp->vfs_vfs);
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 	TRANS_MATA_UMOUNT(ufsvfsp);
9210Sstevel@tonic-gate 	ufsvfsp->vfs_domatamap = 0;
9220Sstevel@tonic-gate 
9230Sstevel@tonic-gate 	/*
9240Sstevel@tonic-gate 	 * Free all of the incore structs
925921Sbatschul 	 * Aquire the ufs_scan_lock before de-linking the mtm data
926921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
927921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
928921Sbatschul 	 * progress of enabling/disabling logging.
9290Sstevel@tonic-gate 	 */
930921Sbatschul 	mutex_enter(&ufs_scan_lock);
9310Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
932921Sbatschul 	mutex_exit(&ufs_scan_lock);
9330Sstevel@tonic-gate 
934329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
9350Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
9360Sstevel@tonic-gate 	vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_NOLOGGING, NULL, 0);
9370Sstevel@tonic-gate 	vfs_unlock(ufsvfsp->vfs_vfs);
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	fs->fs_rolled = FS_ALL_ROLLED;
9400Sstevel@tonic-gate 	ufsvfsp->vfs_nolog_si = 0;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	/*
9430Sstevel@tonic-gate 	 * Free the log space and mark the superblock as FSACTIVE
9440Sstevel@tonic-gate 	 */
9450Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
9480Sstevel@tonic-gate 	 * Allow the reclaim thread to continue.
9490Sstevel@tonic-gate 	 */
9500Sstevel@tonic-gate 	ufs_thread_continue(&ufsvfsp->vfs_reclaim);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * Unlock the file system
9540Sstevel@tonic-gate 	 */
9550Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
9560Sstevel@tonic-gate 	lf.lf_flags = 0;
9570Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
9580Sstevel@tonic-gate 	if (error)
9590Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	return (0);
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate errout:
9640Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
9650Sstevel@tonic-gate 	lf.lf_flags = 0;
9660Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
9670Sstevel@tonic-gate 	return (error);
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Enable logging
9720Sstevel@tonic-gate  */
9730Sstevel@tonic-gate int
9740Sstevel@tonic-gate lufs_enable(struct vnode *vp, struct fiolog *flp, cred_t *cr)
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate 	int		error;
9770Sstevel@tonic-gate 	int		reclaim;
9780Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
9790Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
9800Sstevel@tonic-gate 	struct fs	*fs;
9810Sstevel@tonic-gate 	ml_unit_t	*ul;
9820Sstevel@tonic-gate 	struct lockfs	lf;
9830Sstevel@tonic-gate 	struct ulockfs	*ulp;
9840Sstevel@tonic-gate 	vfs_t		*vfsp = ufsvfsp->vfs_vfs;
9850Sstevel@tonic-gate 	uint64_t	tmp_nbytes_actual;
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	/*
9880Sstevel@tonic-gate 	 * Check if logging is already enabled
9890Sstevel@tonic-gate 	 */
9900Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
9910Sstevel@tonic-gate 		flp->error = FIOLOG_ETRANS;
9920Sstevel@tonic-gate 		/* for root ensure logging option is set */
9930Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
9940Sstevel@tonic-gate 		return (0);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	/*
9990Sstevel@tonic-gate 	 * Come back here to recheck if we had to disable the log.
10000Sstevel@tonic-gate 	 */
10010Sstevel@tonic-gate recheck:
10020Sstevel@tonic-gate 	error = 0;
10030Sstevel@tonic-gate 	reclaim = 0;
10040Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	/*
10070Sstevel@tonic-gate 	 * Adjust requested log size
10080Sstevel@tonic-gate 	 */
10090Sstevel@tonic-gate 	flp->nbytes_actual = flp->nbytes_requested;
10100Sstevel@tonic-gate 	if (flp->nbytes_actual == 0) {
10110Sstevel@tonic-gate 		tmp_nbytes_actual =
10120Sstevel@tonic-gate 		    (((uint64_t)fs->fs_size) / ldl_divisor) << fs->fs_fshift;
10130Sstevel@tonic-gate 		flp->nbytes_actual = (uint_t)MIN(tmp_nbytes_actual, INT_MAX);
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	flp->nbytes_actual = MAX(flp->nbytes_actual, ldl_minlogsize);
10160Sstevel@tonic-gate 	flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_maxlogsize);
10170Sstevel@tonic-gate 	flp->nbytes_actual = blkroundup(fs, flp->nbytes_actual);
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/*
10200Sstevel@tonic-gate 	 * logging is enabled and the log is the right size; done
10210Sstevel@tonic-gate 	 */
10220Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
10230Sstevel@tonic-gate 	if (ul && fs->fs_logbno && (flp->nbytes_actual == ul->un_requestsize))
10240Sstevel@tonic-gate 			return (0);
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 	/*
10270Sstevel@tonic-gate 	 * Readonly file system
10280Sstevel@tonic-gate 	 */
10290Sstevel@tonic-gate 	if (fs->fs_ronly) {
10300Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
10310Sstevel@tonic-gate 		return (0);
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/*
10350Sstevel@tonic-gate 	 * File system must be write locked to enable logging
10360Sstevel@tonic-gate 	 */
10370Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
10380Sstevel@tonic-gate 	if (error) {
10390Sstevel@tonic-gate 		return (error);
10400Sstevel@tonic-gate 	}
10410Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
10420Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
10430Sstevel@tonic-gate 		return (0);
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
10460Sstevel@tonic-gate 	lf.lf_flags = 0;
10470Sstevel@tonic-gate 	lf.lf_comment = NULL;
10480Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
10490Sstevel@tonic-gate 	if (error) {
10500Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
10510Sstevel@tonic-gate 		return (0);
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/*
1055921Sbatschul 	 * Grab appropriate locks to synchronize with the rest
1056921Sbatschul 	 * of the system
1057921Sbatschul 	 */
1058921Sbatschul 	vfs_lock_wait(vfsp);
1059921Sbatschul 	ulp = &ufsvfsp->vfs_ulockfs;
1060921Sbatschul 	mutex_enter(&ulp->ul_lock);
1061921Sbatschul 
1062921Sbatschul 	/*
10630Sstevel@tonic-gate 	 * File system must be fairly consistent to enable logging
10640Sstevel@tonic-gate 	 */
10650Sstevel@tonic-gate 	if (fs->fs_clean != FSLOG &&
10660Sstevel@tonic-gate 	    fs->fs_clean != FSACTIVE &&
10670Sstevel@tonic-gate 	    fs->fs_clean != FSSTABLE &&
10680Sstevel@tonic-gate 	    fs->fs_clean != FSCLEAN) {
10690Sstevel@tonic-gate 		flp->error = FIOLOG_ECLEAN;
10700Sstevel@tonic-gate 		goto unlockout;
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	/*
10740Sstevel@tonic-gate 	 * A write-locked file system is only active if there are
10750Sstevel@tonic-gate 	 * open deleted files; so remember to set FS_RECLAIM later.
10760Sstevel@tonic-gate 	 */
10770Sstevel@tonic-gate 	if (fs->fs_clean == FSACTIVE)
10780Sstevel@tonic-gate 		reclaim = FS_RECLAIM;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	/*
10810Sstevel@tonic-gate 	 * Logging is already enabled; must be changing the log's size
10820Sstevel@tonic-gate 	 */
10830Sstevel@tonic-gate 	if (fs->fs_logbno && ufsvfsp->vfs_log) {
10840Sstevel@tonic-gate 		/*
10850Sstevel@tonic-gate 		 * Before we can disable logging, we must give up our
10860Sstevel@tonic-gate 		 * lock.  As a consequence of unlocking and disabling the
10870Sstevel@tonic-gate 		 * log, the fs structure may change.  Because of this, when
10880Sstevel@tonic-gate 		 * disabling is complete, we will go back to recheck to
10890Sstevel@tonic-gate 		 * repeat all of the checks that we performed to get to
10900Sstevel@tonic-gate 		 * this point.  Disabling sets fs->fs_logbno to 0, so this
10910Sstevel@tonic-gate 		 * will not put us into an infinite loop.
10920Sstevel@tonic-gate 		 */
1093921Sbatschul 		mutex_exit(&ulp->ul_lock);
1094921Sbatschul 		vfs_unlock(vfsp);
1095921Sbatschul 
10960Sstevel@tonic-gate 		lf.lf_lock = LOCKFS_ULOCK;
10970Sstevel@tonic-gate 		lf.lf_flags = 0;
10980Sstevel@tonic-gate 		error = ufs_fiolfs(vp, &lf, 1);
10990Sstevel@tonic-gate 		if (error) {
11000Sstevel@tonic-gate 			flp->error = FIOLOG_ENOULOCK;
11010Sstevel@tonic-gate 			return (0);
11020Sstevel@tonic-gate 		}
11030Sstevel@tonic-gate 		error = lufs_disable(vp, flp);
11040Sstevel@tonic-gate 		if (error || (flp->error != FIOLOG_ENONE))
11050Sstevel@tonic-gate 			return (0);
11060Sstevel@tonic-gate 		goto recheck;
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	error = lufs_alloc(ufsvfsp, flp, cr);
11100Sstevel@tonic-gate 	if (error)
11110Sstevel@tonic-gate 		goto errout;
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	/*
11140Sstevel@tonic-gate 	 * Create all of the incore structs
11150Sstevel@tonic-gate 	 */
11160Sstevel@tonic-gate 	error = lufs_snarf(ufsvfsp, fs, 0);
11170Sstevel@tonic-gate 	if (error)
11180Sstevel@tonic-gate 		goto errout;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 	/*
11210Sstevel@tonic-gate 	 * DON'T ``GOTO ERROUT'' PAST THIS POINT
11220Sstevel@tonic-gate 	 */
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	/*
11250Sstevel@tonic-gate 	 * Pretend we were just mounted with logging enabled
11260Sstevel@tonic-gate 	 *		Get the ops vector
11270Sstevel@tonic-gate 	 *		If debug, record metadata locations with log subsystem
11280Sstevel@tonic-gate 	 *		Start the delete thread
11290Sstevel@tonic-gate 	 *		Start the reclaim thread, if necessary
11300Sstevel@tonic-gate 	 */
11310Sstevel@tonic-gate 	vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	TRANS_DOMATAMAP(ufsvfsp);
11340Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
11350Sstevel@tonic-gate 	TRANS_MATA_SI(ufsvfsp, fs);
11360Sstevel@tonic-gate 	ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
11370Sstevel@tonic-gate 	if (fs->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
11380Sstevel@tonic-gate 		fs->fs_reclaim &= ~FS_RECLAIM;
11390Sstevel@tonic-gate 		fs->fs_reclaim |=  FS_RECLAIMING;
11400Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
11414662Sfrankho 		    ufs_thread_reclaim, vfsp);
11420Sstevel@tonic-gate 	} else
11430Sstevel@tonic-gate 		fs->fs_reclaim |= reclaim;
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
11460Sstevel@tonic-gate 	vfs_unlock(vfsp);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	/*
11490Sstevel@tonic-gate 	 * Unlock the file system
11500Sstevel@tonic-gate 	 */
11510Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
11520Sstevel@tonic-gate 	lf.lf_flags = 0;
11530Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
11540Sstevel@tonic-gate 	if (error) {
11550Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
11560Sstevel@tonic-gate 		return (0);
11570Sstevel@tonic-gate 	}
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/*
11600Sstevel@tonic-gate 	 * There's nothing in the log yet (we've just allocated it)
11610Sstevel@tonic-gate 	 * so directly write out the super block.
11620Sstevel@tonic-gate 	 * Note, we have to force this sb out to disk
11630Sstevel@tonic-gate 	 * (not just to the log) so that if we crash we know we are logging
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
11660Sstevel@tonic-gate 	fs->fs_clean = FSLOG;
11670Sstevel@tonic-gate 	fs->fs_rolled = FS_NEED_ROLL; /* Mark the fs as unrolled */
11680Sstevel@tonic-gate 	UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
11690Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	return (0);
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate errout:
1174921Sbatschul 	/*
1175921Sbatschul 	 * Aquire the ufs_scan_lock before de-linking the mtm data
1176921Sbatschul 	 * structure so that we keep ufs_sync() and ufs_update() away
1177921Sbatschul 	 * when they execute the ufs_scan_inodes() run while we're in
1178921Sbatschul 	 * progress of enabling/disabling logging.
1179921Sbatschul 	 */
1180921Sbatschul 	mutex_enter(&ufs_scan_lock);
11810Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
1182921Sbatschul 	mutex_exit(&ufs_scan_lock);
1183921Sbatschul 
11840Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
11850Sstevel@tonic-gate unlockout:
1186921Sbatschul 	mutex_exit(&ulp->ul_lock);
1187921Sbatschul 	vfs_unlock(vfsp);
1188921Sbatschul 
11890Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
11900Sstevel@tonic-gate 	lf.lf_flags = 0;
11910Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
11920Sstevel@tonic-gate 	return (error);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate void
11960Sstevel@tonic-gate lufs_read_strategy(ml_unit_t *ul, buf_t *bp)
11970Sstevel@tonic-gate {
11980Sstevel@tonic-gate 	mt_map_t	*logmap	= ul->un_logmap;
11990Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
12000Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
12010Sstevel@tonic-gate 	mapentry_t	*age;
12020Sstevel@tonic-gate 	char		*va;
12030Sstevel@tonic-gate 	int		(*saviodone)();
12040Sstevel@tonic-gate 	int		entire_range;
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate 	/*
12070Sstevel@tonic-gate 	 * get a linked list of overlapping deltas
12080Sstevel@tonic-gate 	 * returns with &mtm->mtm_rwlock held
12090Sstevel@tonic-gate 	 */
12100Sstevel@tonic-gate 	entire_range = logmap_list_get(logmap, mof, nb, &age);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/*
12130Sstevel@tonic-gate 	 * no overlapping deltas were found; read master
12140Sstevel@tonic-gate 	 */
12150Sstevel@tonic-gate 	if (age == NULL) {
12160Sstevel@tonic-gate 		rw_exit(&logmap->mtm_rwlock);
12170Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
12180Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
12190Sstevel@tonic-gate 			bp->b_error = EIO;
12200Sstevel@tonic-gate 			biodone(bp);
12210Sstevel@tonic-gate 		} else {
12220Sstevel@tonic-gate 			ul->un_ufsvfs->vfs_iotstamp = lbolt;
12230Sstevel@tonic-gate 			logstats.ls_lreads.value.ui64++;
12240Sstevel@tonic-gate 			(void) bdev_strategy(bp);
12250Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
12260Sstevel@tonic-gate 		}
12270Sstevel@tonic-gate 		return;
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	va = bp_mapin_common(bp, VM_SLEEP);
12310Sstevel@tonic-gate 	/*
12320Sstevel@tonic-gate 	 * if necessary, sync read the data from master
12330Sstevel@tonic-gate 	 *	errors are returned in bp
12340Sstevel@tonic-gate 	 */
12350Sstevel@tonic-gate 	if (!entire_range) {
12360Sstevel@tonic-gate 		saviodone = bp->b_iodone;
12370Sstevel@tonic-gate 		bp->b_iodone = trans_not_done;
12380Sstevel@tonic-gate 		logstats.ls_mreads.value.ui64++;
12390Sstevel@tonic-gate 		(void) bdev_strategy(bp);
12400Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
12410Sstevel@tonic-gate 		if (trans_not_wait(bp))
12420Sstevel@tonic-gate 			ldl_seterror(ul, "Error reading master");
12430Sstevel@tonic-gate 		bp->b_iodone = saviodone;
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	/*
12470Sstevel@tonic-gate 	 * sync read the data from the log
12480Sstevel@tonic-gate 	 *	errors are returned inline
12490Sstevel@tonic-gate 	 */
12500Sstevel@tonic-gate 	if (ldl_read(ul, va, mof, nb, age)) {
12510Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
12520Sstevel@tonic-gate 		bp->b_error = EIO;
12530Sstevel@tonic-gate 	}
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 	/*
12560Sstevel@tonic-gate 	 * unlist the deltas
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	logmap_list_put(logmap, age);
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 	/*
12610Sstevel@tonic-gate 	 * all done
12620Sstevel@tonic-gate 	 */
12630Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
12640Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
12650Sstevel@tonic-gate 		bp->b_error = EIO;
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 	biodone(bp);
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate void
12710Sstevel@tonic-gate lufs_write_strategy(ml_unit_t *ul, buf_t *bp)
12720Sstevel@tonic-gate {
12730Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
12740Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
12750Sstevel@tonic-gate 	char		*va;
12760Sstevel@tonic-gate 	mapentry_t	*me;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	ASSERT((nb & DEV_BMASK) == 0);
12790Sstevel@tonic-gate 	ul->un_logmap->mtm_ref = 1;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	/*
12820Sstevel@tonic-gate 	 * if there are deltas, move into log
12830Sstevel@tonic-gate 	 */
12840Sstevel@tonic-gate 	me = deltamap_remove(ul->un_deltamap, mof, nb);
12850Sstevel@tonic-gate 	if (me) {
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 		va = bp_mapin_common(bp, VM_SLEEP);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 		ASSERT(((ul->un_debug & MT_WRITE_CHECK) == 0) ||
12904662Sfrankho 		    (ul->un_matamap == NULL)||
12914662Sfrankho 		    matamap_within(ul->un_matamap, mof, nb));
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 		/*
12940Sstevel@tonic-gate 		 * move to logmap
12950Sstevel@tonic-gate 		 */
12960Sstevel@tonic-gate 		if (ufs_crb_enable) {
12970Sstevel@tonic-gate 			logmap_add_buf(ul, va, mof, me,
12980Sstevel@tonic-gate 			    bp->b_un.b_addr, nb);
12990Sstevel@tonic-gate 		} else {
13000Sstevel@tonic-gate 			logmap_add(ul, va, mof, me);
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
13040Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
13050Sstevel@tonic-gate 			bp->b_error = EIO;
13060Sstevel@tonic-gate 		}
13070Sstevel@tonic-gate 		biodone(bp);
13080Sstevel@tonic-gate 		return;
13090Sstevel@tonic-gate 	}
13100Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
13110Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
13120Sstevel@tonic-gate 		bp->b_error = EIO;
13130Sstevel@tonic-gate 		biodone(bp);
13140Sstevel@tonic-gate 		return;
13150Sstevel@tonic-gate 	}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * Check that we are not updating metadata, or if so then via B_PHYS.
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	ASSERT((ul->un_matamap == NULL) ||
13214662Sfrankho 	    !(matamap_overlap(ul->un_matamap, mof, nb) &&
13224662Sfrankho 	    ((bp->b_flags & B_PHYS) == 0)));
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 	ul->un_ufsvfs->vfs_iotstamp = lbolt;
13250Sstevel@tonic-gate 	logstats.ls_lwrites.value.ui64++;
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate 	/* If snapshots are enabled, write through the snapshot driver */
13280Sstevel@tonic-gate 	if (ul->un_ufsvfs->vfs_snapshot)
13290Sstevel@tonic-gate 		fssnap_strategy(&ul->un_ufsvfs->vfs_snapshot, bp);
13300Sstevel@tonic-gate 	else
13310Sstevel@tonic-gate 		(void) bdev_strategy(bp);
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	lwp_stat_update(LWP_STAT_OUBLK, 1);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate void
13370Sstevel@tonic-gate lufs_strategy(ml_unit_t *ul, buf_t *bp)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	if (bp->b_flags & B_READ)
13400Sstevel@tonic-gate 		lufs_read_strategy(ul, bp);
13410Sstevel@tonic-gate 	else
13420Sstevel@tonic-gate 		lufs_write_strategy(ul, bp);
13430Sstevel@tonic-gate }
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate /* ARGSUSED */
13460Sstevel@tonic-gate static int
13470Sstevel@tonic-gate delta_stats_update(kstat_t *ksp, int rw)
13480Sstevel@tonic-gate {
13490Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
13500Sstevel@tonic-gate 		delta_stats[DT_SB] = dkstats.ds_superblock_deltas.value.ui64;
13510Sstevel@tonic-gate 		delta_stats[DT_CG] = dkstats.ds_bitmap_deltas.value.ui64;
13520Sstevel@tonic-gate 		delta_stats[DT_SI] = dkstats.ds_suminfo_deltas.value.ui64;
13530Sstevel@tonic-gate 		delta_stats[DT_AB] = dkstats.ds_allocblk_deltas.value.ui64;
13540Sstevel@tonic-gate 		delta_stats[DT_ABZERO] = dkstats.ds_ab0_deltas.value.ui64;
13550Sstevel@tonic-gate 		delta_stats[DT_DIR] = dkstats.ds_dir_deltas.value.ui64;
13560Sstevel@tonic-gate 		delta_stats[DT_INODE] = dkstats.ds_inode_deltas.value.ui64;
13570Sstevel@tonic-gate 		delta_stats[DT_FBI] = dkstats.ds_fbiwrite_deltas.value.ui64;
13580Sstevel@tonic-gate 		delta_stats[DT_QR] = dkstats.ds_quota_deltas.value.ui64;
13590Sstevel@tonic-gate 		delta_stats[DT_SHAD] = dkstats.ds_shadow_deltas.value.ui64;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 		roll_stats[DT_SB] = dkstats.ds_superblock_rolled.value.ui64;
13620Sstevel@tonic-gate 		roll_stats[DT_CG] = dkstats.ds_bitmap_rolled.value.ui64;
13630Sstevel@tonic-gate 		roll_stats[DT_SI] = dkstats.ds_suminfo_rolled.value.ui64;
13640Sstevel@tonic-gate 		roll_stats[DT_AB] = dkstats.ds_allocblk_rolled.value.ui64;
13650Sstevel@tonic-gate 		roll_stats[DT_ABZERO] = dkstats.ds_ab0_rolled.value.ui64;
13660Sstevel@tonic-gate 		roll_stats[DT_DIR] = dkstats.ds_dir_rolled.value.ui64;
13670Sstevel@tonic-gate 		roll_stats[DT_INODE] = dkstats.ds_inode_rolled.value.ui64;
13680Sstevel@tonic-gate 		roll_stats[DT_FBI] = dkstats.ds_fbiwrite_rolled.value.ui64;
13690Sstevel@tonic-gate 		roll_stats[DT_QR] = dkstats.ds_quota_rolled.value.ui64;
13700Sstevel@tonic-gate 		roll_stats[DT_SHAD] = dkstats.ds_shadow_rolled.value.ui64;
13710Sstevel@tonic-gate 	} else {
13720Sstevel@tonic-gate 		dkstats.ds_superblock_deltas.value.ui64 = delta_stats[DT_SB];
13730Sstevel@tonic-gate 		dkstats.ds_bitmap_deltas.value.ui64 = delta_stats[DT_CG];
13740Sstevel@tonic-gate 		dkstats.ds_suminfo_deltas.value.ui64 = delta_stats[DT_SI];
13750Sstevel@tonic-gate 		dkstats.ds_allocblk_deltas.value.ui64 = delta_stats[DT_AB];
13760Sstevel@tonic-gate 		dkstats.ds_ab0_deltas.value.ui64 = delta_stats[DT_ABZERO];
13770Sstevel@tonic-gate 		dkstats.ds_dir_deltas.value.ui64 = delta_stats[DT_DIR];
13780Sstevel@tonic-gate 		dkstats.ds_inode_deltas.value.ui64 = delta_stats[DT_INODE];
13790Sstevel@tonic-gate 		dkstats.ds_fbiwrite_deltas.value.ui64 = delta_stats[DT_FBI];
13800Sstevel@tonic-gate 		dkstats.ds_quota_deltas.value.ui64 = delta_stats[DT_QR];
13810Sstevel@tonic-gate 		dkstats.ds_shadow_deltas.value.ui64 = delta_stats[DT_SHAD];
13820Sstevel@tonic-gate 
13830Sstevel@tonic-gate 		dkstats.ds_superblock_rolled.value.ui64 = roll_stats[DT_SB];
13840Sstevel@tonic-gate 		dkstats.ds_bitmap_rolled.value.ui64 = roll_stats[DT_CG];
13850Sstevel@tonic-gate 		dkstats.ds_suminfo_rolled.value.ui64 = roll_stats[DT_SI];
13860Sstevel@tonic-gate 		dkstats.ds_allocblk_rolled.value.ui64 = roll_stats[DT_AB];
13870Sstevel@tonic-gate 		dkstats.ds_ab0_rolled.value.ui64 = roll_stats[DT_ABZERO];
13880Sstevel@tonic-gate 		dkstats.ds_dir_rolled.value.ui64 = roll_stats[DT_DIR];
13890Sstevel@tonic-gate 		dkstats.ds_inode_rolled.value.ui64 = roll_stats[DT_INODE];
13900Sstevel@tonic-gate 		dkstats.ds_fbiwrite_rolled.value.ui64 = roll_stats[DT_FBI];
13910Sstevel@tonic-gate 		dkstats.ds_quota_rolled.value.ui64 = roll_stats[DT_QR];
13920Sstevel@tonic-gate 		dkstats.ds_shadow_rolled.value.ui64 = roll_stats[DT_SHAD];
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate 	return (0);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate extern size_t ufs_crb_limit;
13980Sstevel@tonic-gate extern int ufs_max_crb_divisor;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate void
14010Sstevel@tonic-gate lufs_init(void)
14020Sstevel@tonic-gate {
14030Sstevel@tonic-gate 	kstat_t *ksp;
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 	/* Create kmem caches */
14060Sstevel@tonic-gate 	lufs_sv = kmem_cache_create("lufs_save", sizeof (lufs_save_t), 0,
14070Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
14080Sstevel@tonic-gate 	lufs_bp = kmem_cache_create("lufs_bufs", sizeof (lufs_buf_t), 0,
14090Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	mutex_init(&log_mutex, NULL, MUTEX_DEFAULT, NULL);
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	_init_top();
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate 	if (&bio_lufs_strategy != NULL)
14160Sstevel@tonic-gate 		bio_lufs_strategy = (void (*) (void *, buf_t *)) lufs_strategy;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	/*
14190Sstevel@tonic-gate 	 * Initialise general logging and delta kstats
14200Sstevel@tonic-gate 	 */
14210Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "logstats", "ufs", KSTAT_TYPE_NAMED,
14220Sstevel@tonic-gate 	    sizeof (logstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
14230Sstevel@tonic-gate 	if (ksp) {
14240Sstevel@tonic-gate 		ksp->ks_data = (void *) &logstats;
14250Sstevel@tonic-gate 		kstat_install(ksp);
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "deltastats", "ufs", KSTAT_TYPE_NAMED,
14290Sstevel@tonic-gate 	    sizeof (dkstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
14300Sstevel@tonic-gate 	if (ksp) {
14310Sstevel@tonic-gate 		ksp->ks_data = (void *) &dkstats;
14320Sstevel@tonic-gate 		ksp->ks_update = delta_stats_update;
14330Sstevel@tonic-gate 		kstat_install(ksp);
14340Sstevel@tonic-gate 	}
14350Sstevel@tonic-gate 
1436*7455SWolfgang.Schremser@Sun.COM 	/* Initialize  generation of logging ids */
1437*7455SWolfgang.Schremser@Sun.COM 	lufs_genid_init();
1438*7455SWolfgang.Schremser@Sun.COM 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * Set up the maximum amount of kmem that the crbs (system wide)
14410Sstevel@tonic-gate 	 * can use.
14420Sstevel@tonic-gate 	 */
14430Sstevel@tonic-gate 	ufs_crb_limit = kmem_maxavail() / ufs_max_crb_divisor;
14440Sstevel@tonic-gate }
1445