xref: /onnv-gate/usr/src/uts/common/fs/ufs/lufs.c (revision 329:a4b8082fb0ab)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/vnode.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/fssnap_if.h>
350Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
360Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/modctl.h>
390Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
400Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
410Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
420Sstevel@tonic-gate #include <sys/debug.h>
43*329Saguzovsk #include <sys/atomic.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/inttypes.h>
460Sstevel@tonic-gate #include <sys/vfs.h>
470Sstevel@tonic-gate #include <sys/mntent.h>
480Sstevel@tonic-gate #include <sys/conf.h>
490Sstevel@tonic-gate #include <sys/param.h>
500Sstevel@tonic-gate #include <sys/kstat.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate static kmutex_t	log_mutex;	/* general purpose log layer lock */
540Sstevel@tonic-gate kmutex_t	ml_scan;	/* Scan thread syncronization */
550Sstevel@tonic-gate kcondvar_t	ml_scan_cv;	/* Scan thread syncronization */
560Sstevel@tonic-gate 
570Sstevel@tonic-gate struct kmem_cache	*lufs_sv;
580Sstevel@tonic-gate struct kmem_cache	*lufs_bp;
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Tunables */
610Sstevel@tonic-gate uint_t		ldl_maxlogsize	= LDL_MAXLOGSIZE;
620Sstevel@tonic-gate uint_t		ldl_minlogsize	= LDL_MINLOGSIZE;
630Sstevel@tonic-gate uint32_t	ldl_divisor	= LDL_DIVISOR;
640Sstevel@tonic-gate uint32_t	ldl_mintransfer	= LDL_MINTRANSFER;
650Sstevel@tonic-gate uint32_t	ldl_maxtransfer	= LDL_MAXTRANSFER;
660Sstevel@tonic-gate uint32_t	ldl_minbufsize	= LDL_MINBUFSIZE;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate uint32_t	last_loghead_ident = 0;
690Sstevel@tonic-gate 
700Sstevel@tonic-gate /*
710Sstevel@tonic-gate  * Logging delta and roll statistics
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate struct delta_kstats {
740Sstevel@tonic-gate 	kstat_named_t ds_superblock_deltas;
750Sstevel@tonic-gate 	kstat_named_t ds_bitmap_deltas;
760Sstevel@tonic-gate 	kstat_named_t ds_suminfo_deltas;
770Sstevel@tonic-gate 	kstat_named_t ds_allocblk_deltas;
780Sstevel@tonic-gate 	kstat_named_t ds_ab0_deltas;
790Sstevel@tonic-gate 	kstat_named_t ds_dir_deltas;
800Sstevel@tonic-gate 	kstat_named_t ds_inode_deltas;
810Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_deltas;
820Sstevel@tonic-gate 	kstat_named_t ds_quota_deltas;
830Sstevel@tonic-gate 	kstat_named_t ds_shadow_deltas;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	kstat_named_t ds_superblock_rolled;
860Sstevel@tonic-gate 	kstat_named_t ds_bitmap_rolled;
870Sstevel@tonic-gate 	kstat_named_t ds_suminfo_rolled;
880Sstevel@tonic-gate 	kstat_named_t ds_allocblk_rolled;
890Sstevel@tonic-gate 	kstat_named_t ds_ab0_rolled;
900Sstevel@tonic-gate 	kstat_named_t ds_dir_rolled;
910Sstevel@tonic-gate 	kstat_named_t ds_inode_rolled;
920Sstevel@tonic-gate 	kstat_named_t ds_fbiwrite_rolled;
930Sstevel@tonic-gate 	kstat_named_t ds_quota_rolled;
940Sstevel@tonic-gate 	kstat_named_t ds_shadow_rolled;
950Sstevel@tonic-gate } dkstats = {
960Sstevel@tonic-gate 	{ "superblock_deltas",	KSTAT_DATA_UINT64 },
970Sstevel@tonic-gate 	{ "bitmap_deltas",	KSTAT_DATA_UINT64 },
980Sstevel@tonic-gate 	{ "suminfo_deltas",	KSTAT_DATA_UINT64 },
990Sstevel@tonic-gate 	{ "allocblk_deltas",	KSTAT_DATA_UINT64 },
1000Sstevel@tonic-gate 	{ "ab0_deltas",		KSTAT_DATA_UINT64 },
1010Sstevel@tonic-gate 	{ "dir_deltas",		KSTAT_DATA_UINT64 },
1020Sstevel@tonic-gate 	{ "inode_deltas",	KSTAT_DATA_UINT64 },
1030Sstevel@tonic-gate 	{ "fbiwrite_deltas",	KSTAT_DATA_UINT64 },
1040Sstevel@tonic-gate 	{ "quota_deltas",	KSTAT_DATA_UINT64 },
1050Sstevel@tonic-gate 	{ "shadow_deltas",	KSTAT_DATA_UINT64 },
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	{ "superblock_rolled",	KSTAT_DATA_UINT64 },
1080Sstevel@tonic-gate 	{ "bitmap_rolled",	KSTAT_DATA_UINT64 },
1090Sstevel@tonic-gate 	{ "suminfo_rolled",	KSTAT_DATA_UINT64 },
1100Sstevel@tonic-gate 	{ "allocblk_rolled",	KSTAT_DATA_UINT64 },
1110Sstevel@tonic-gate 	{ "ab0_rolled",		KSTAT_DATA_UINT64 },
1120Sstevel@tonic-gate 	{ "dir_rolled",		KSTAT_DATA_UINT64 },
1130Sstevel@tonic-gate 	{ "inode_rolled",	KSTAT_DATA_UINT64 },
1140Sstevel@tonic-gate 	{ "fbiwrite_rolled",	KSTAT_DATA_UINT64 },
1150Sstevel@tonic-gate 	{ "quota_rolled",	KSTAT_DATA_UINT64 },
1160Sstevel@tonic-gate 	{ "shadow_rolled",	KSTAT_DATA_UINT64 }
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate uint64_t delta_stats[DT_MAX];
1200Sstevel@tonic-gate uint64_t roll_stats[DT_MAX];
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * General logging kstats
1240Sstevel@tonic-gate  */
1250Sstevel@tonic-gate struct logstats logstats = {
1260Sstevel@tonic-gate 	{ "master_reads",		KSTAT_DATA_UINT64 },
1270Sstevel@tonic-gate 	{ "master_writes",		KSTAT_DATA_UINT64 },
1280Sstevel@tonic-gate 	{ "log_reads_inmem",		KSTAT_DATA_UINT64 },
1290Sstevel@tonic-gate 	{ "log_reads",			KSTAT_DATA_UINT64 },
1300Sstevel@tonic-gate 	{ "log_writes",			KSTAT_DATA_UINT64 },
1310Sstevel@tonic-gate 	{ "log_master_reads",		KSTAT_DATA_UINT64 },
1320Sstevel@tonic-gate 	{ "log_roll_reads",		KSTAT_DATA_UINT64 },
1330Sstevel@tonic-gate 	{ "log_roll_writes",		KSTAT_DATA_UINT64 }
1340Sstevel@tonic-gate };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate int
1370Sstevel@tonic-gate trans_not_done(struct buf *cb)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate 	sema_v(&cb->b_io);
1400Sstevel@tonic-gate 	return (0);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static void
1440Sstevel@tonic-gate trans_wait_panic(struct buf *cb)
1450Sstevel@tonic-gate {
1460Sstevel@tonic-gate 	while ((cb->b_flags & B_DONE) == 0)
1470Sstevel@tonic-gate 		drv_usecwait(10);
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate int
1510Sstevel@tonic-gate trans_not_wait(struct buf *cb)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * In case of panic, busy wait for completion
1550Sstevel@tonic-gate 	 */
1560Sstevel@tonic-gate 	if (panicstr)
1570Sstevel@tonic-gate 		trans_wait_panic(cb);
1580Sstevel@tonic-gate 	else
1590Sstevel@tonic-gate 		sema_p(&cb->b_io);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	return (geterror(cb));
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate int
1650Sstevel@tonic-gate trans_wait(struct buf *cb)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	/*
1680Sstevel@tonic-gate 	 * In case of panic, busy wait for completion and run md daemon queues
1690Sstevel@tonic-gate 	 */
1700Sstevel@tonic-gate 	if (panicstr)
1710Sstevel@tonic-gate 		trans_wait_panic(cb);
1720Sstevel@tonic-gate 	return (biowait(cb));
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static void
1760Sstevel@tonic-gate setsum(int32_t *sp, int32_t *lp, int nb)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	int32_t csum = 0;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	*sp = 0;
1810Sstevel@tonic-gate 	nb /= sizeof (int32_t);
1820Sstevel@tonic-gate 	while (nb--)
1830Sstevel@tonic-gate 		csum += *lp++;
1840Sstevel@tonic-gate 	*sp = csum;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate static int
1880Sstevel@tonic-gate checksum(int32_t *sp, int32_t *lp, int nb)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate 	int32_t ssum = *sp;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	setsum(sp, lp, nb);
1930Sstevel@tonic-gate 	if (ssum != *sp) {
1940Sstevel@tonic-gate 		*sp = ssum;
1950Sstevel@tonic-gate 		return (0);
1960Sstevel@tonic-gate 	}
1970Sstevel@tonic-gate 	return (1);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate void
2010Sstevel@tonic-gate lufs_unsnarf(ufsvfs_t *ufsvfsp)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	ml_unit_t *ul;
2040Sstevel@tonic-gate 	mt_map_t *mtm;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
2070Sstevel@tonic-gate 	if (ul == NULL)
2080Sstevel@tonic-gate 		return;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	mtm = ul->un_logmap;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Wait for a pending top_issue_sync which is
2140Sstevel@tonic-gate 	 * dispatched (via taskq_dispatch()) but hasnt completed yet.
2150Sstevel@tonic-gate 	 */
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	mutex_enter(&mtm->mtm_lock);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	while (mtm->mtm_taskq_sync_count != 0) {
2200Sstevel@tonic-gate 		cv_wait(&mtm->mtm_cv, &mtm->mtm_lock);
2210Sstevel@tonic-gate 	}
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	mutex_exit(&mtm->mtm_lock);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* Roll committed transactions */
2260Sstevel@tonic-gate 	logmap_roll_dev(ul);
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* Kill the roll thread */
2290Sstevel@tonic-gate 	logmap_kill_roll(ul);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/* release saved alloction info */
2320Sstevel@tonic-gate 	if (ul->un_ebp)
2330Sstevel@tonic-gate 		kmem_free(ul->un_ebp, ul->un_nbeb);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	/* release circular bufs */
2360Sstevel@tonic-gate 	free_cirbuf(&ul->un_rdbuf);
2370Sstevel@tonic-gate 	free_cirbuf(&ul->un_wrbuf);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* release maps */
2400Sstevel@tonic-gate 	if (ul->un_logmap)
2410Sstevel@tonic-gate 		ul->un_logmap = map_put(ul->un_logmap);
2420Sstevel@tonic-gate 	if (ul->un_deltamap)
2430Sstevel@tonic-gate 		ul->un_deltamap = map_put(ul->un_deltamap);
2440Sstevel@tonic-gate 	if (ul->un_matamap)
2450Sstevel@tonic-gate 		ul->un_matamap = map_put(ul->un_matamap);
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate 	mutex_destroy(&ul->un_log_mutex);
2480Sstevel@tonic-gate 	mutex_destroy(&ul->un_state_mutex);
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	/* release state buffer MUST BE LAST!! (contains our ondisk data) */
2510Sstevel@tonic-gate 	if (ul->un_bp)
2520Sstevel@tonic-gate 		brelse(ul->un_bp);
2530Sstevel@tonic-gate 	kmem_free(ul, sizeof (*ul));
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	ufsvfsp->vfs_log = NULL;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate int
2590Sstevel@tonic-gate lufs_snarf(ufsvfs_t *ufsvfsp, struct fs *fs, int ronly)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	buf_t		*bp, *tbp;
2620Sstevel@tonic-gate 	ml_unit_t	*ul;
2630Sstevel@tonic-gate 	extent_block_t	*ebp;
2640Sstevel@tonic-gate 	ic_extent_block_t  *nebp;
2650Sstevel@tonic-gate 	size_t		nb;
2660Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
2670Sstevel@tonic-gate 	int		i;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
2700Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	/*
2730Sstevel@tonic-gate 	 * Get the allocation table
2740Sstevel@tonic-gate 	 *	During a remount the superblock pointed to by the ufsvfsp
2750Sstevel@tonic-gate 	 *	is out of date.  Hence the need for the ``new'' superblock
2760Sstevel@tonic-gate 	 *	pointer, fs, passed in as a parameter.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, fs->fs_logbno),
2790Sstevel@tonic-gate 	    fs->fs_bsize);
2800Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
2810Sstevel@tonic-gate 		brelse(bp);
2820Sstevel@tonic-gate 		return (EIO);
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
2850Sstevel@tonic-gate 	if (!checksum(&ebp->chksum, (int32_t *)bp->b_un.b_addr,
2860Sstevel@tonic-gate 		fs->fs_bsize)) {
2870Sstevel@tonic-gate 		brelse(bp);
2880Sstevel@tonic-gate 		return (ENODEV);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 * It is possible to get log blocks with all zeros.
2930Sstevel@tonic-gate 	 * We should also check for nextents to be zero in such case.
2940Sstevel@tonic-gate 	 */
2950Sstevel@tonic-gate 	if (ebp->type != LUFS_EXTENTS || ebp->nextents == 0) {
2960Sstevel@tonic-gate 		brelse(bp);
2970Sstevel@tonic-gate 		return (EDOM);
2980Sstevel@tonic-gate 	}
2990Sstevel@tonic-gate 	/*
3000Sstevel@tonic-gate 	 * Put allocation into memory.  This requires conversion between
3010Sstevel@tonic-gate 	 * on the ondisk format of the extent (type extent_t) and the
3020Sstevel@tonic-gate 	 * in-core format of the extent (type ic_extent_t).  The
3030Sstevel@tonic-gate 	 * difference is the in-core form of the extent block stores
3040Sstevel@tonic-gate 	 * the physical offset of the extent in disk blocks, which
3050Sstevel@tonic-gate 	 * can require more than a 32-bit field.
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	nb = (size_t)(sizeof (ic_extent_block_t) +
3080Sstevel@tonic-gate 			((ebp->nextents - 1) * sizeof (ic_extent_t)));
3090Sstevel@tonic-gate 	nebp = kmem_alloc(nb, KM_SLEEP);
3100Sstevel@tonic-gate 	nebp->ic_nextents = ebp->nextents;
3110Sstevel@tonic-gate 	nebp->ic_nbytes = ebp->nbytes;
3120Sstevel@tonic-gate 	nebp->ic_nextbno = ebp->nextbno;
3130Sstevel@tonic-gate 	for (i = 0; i < ebp->nextents; i++) {
3140Sstevel@tonic-gate 		nebp->ic_extents[i].ic_lbno = ebp->extents[i].lbno;
3150Sstevel@tonic-gate 		nebp->ic_extents[i].ic_nbno = ebp->extents[i].nbno;
3160Sstevel@tonic-gate 		nebp->ic_extents[i].ic_pbno =
3170Sstevel@tonic-gate 		    logbtodb(fs, ebp->extents[i].pbno);
3180Sstevel@tonic-gate 	}
3190Sstevel@tonic-gate 	brelse(bp);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Get the log state
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	bno = nebp->ic_extents[0].ic_pbno;
3250Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, DEV_BSIZE);
3260Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
3270Sstevel@tonic-gate 		brelse(bp);
3280Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno + 1, DEV_BSIZE);
3290Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
3300Sstevel@tonic-gate 			brelse(bp);
3310Sstevel@tonic-gate 			kmem_free(nebp, nb);
3320Sstevel@tonic-gate 			return (EIO);
3330Sstevel@tonic-gate 		}
3340Sstevel@tonic-gate 	}
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Put ondisk struct into an anonymous buffer
3380Sstevel@tonic-gate 	 *	This buffer will contain the memory for the ml_odunit struct
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	tbp = ngeteblk(dbtob(LS_SECTORS));
3410Sstevel@tonic-gate 	tbp->b_edev = bp->b_edev;
3420Sstevel@tonic-gate 	tbp->b_dev = bp->b_dev;
3430Sstevel@tonic-gate 	tbp->b_blkno = bno;
3440Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr, DEV_BSIZE);
3450Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, tbp->b_un.b_addr + DEV_BSIZE, DEV_BSIZE);
3460Sstevel@tonic-gate 	bp->b_flags |= (B_STALE | B_AGE);
3470Sstevel@tonic-gate 	brelse(bp);
3480Sstevel@tonic-gate 	bp = tbp;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * Verify the log state
3520Sstevel@tonic-gate 	 *
3530Sstevel@tonic-gate 	 * read/only mounts w/bad logs are allowed.  umount will
3540Sstevel@tonic-gate 	 * eventually roll the bad log until the first IO error.
3550Sstevel@tonic-gate 	 * fsck will then repair the file system.
3560Sstevel@tonic-gate 	 *
3570Sstevel@tonic-gate 	 * read/write mounts with bad logs are not allowed.
3580Sstevel@tonic-gate 	 *
3590Sstevel@tonic-gate 	 */
3600Sstevel@tonic-gate 	ul = (ml_unit_t *)kmem_zalloc(sizeof (*ul), KM_SLEEP);
3610Sstevel@tonic-gate 	bcopy(bp->b_un.b_addr, &ul->un_ondisk, sizeof (ml_odunit_t));
3620Sstevel@tonic-gate 	if ((ul->un_chksum != ul->un_head_ident + ul->un_tail_ident) ||
3630Sstevel@tonic-gate 	    (ul->un_version != LUFS_VERSION_LATEST) ||
3640Sstevel@tonic-gate 	    (!ronly && ul->un_badlog)) {
3650Sstevel@tonic-gate 		kmem_free(ul, sizeof (*ul));
3660Sstevel@tonic-gate 		brelse(bp);
3670Sstevel@tonic-gate 		kmem_free(nebp, nb);
3680Sstevel@tonic-gate 		return (EIO);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * Initialize the incore-only fields
3720Sstevel@tonic-gate 	 */
3730Sstevel@tonic-gate 	if (ronly)
3740Sstevel@tonic-gate 		ul->un_flags |= LDL_NOROLL;
3750Sstevel@tonic-gate 	ul->un_bp = bp;
3760Sstevel@tonic-gate 	ul->un_ufsvfs = ufsvfsp;
3770Sstevel@tonic-gate 	ul->un_dev = ufsvfsp->vfs_dev;
3780Sstevel@tonic-gate 	ul->un_ebp = nebp;
3790Sstevel@tonic-gate 	ul->un_nbeb = nb;
3800Sstevel@tonic-gate 	ul->un_maxresv = btodb(ul->un_logsize) * LDL_USABLE_BSIZE;
3810Sstevel@tonic-gate 	ul->un_deltamap = map_get(ul, deltamaptype, DELTAMAP_NHASH);
3820Sstevel@tonic-gate 	ul->un_logmap = map_get(ul, logmaptype, LOGMAP_NHASH);
3830Sstevel@tonic-gate 	if (ul->un_debug & MT_MATAMAP)
3840Sstevel@tonic-gate 		ul->un_matamap = map_get(ul, matamaptype, DELTAMAP_NHASH);
3850Sstevel@tonic-gate 	mutex_init(&ul->un_log_mutex, NULL, MUTEX_DEFAULT, NULL);
3860Sstevel@tonic-gate 	mutex_init(&ul->un_state_mutex, NULL, MUTEX_DEFAULT, NULL);
3870Sstevel@tonic-gate 	ufsvfsp->vfs_log = ul;
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	/* remember the state of the log before the log scan */
3900Sstevel@tonic-gate 	logmap_logscan(ul);
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * Error during scan
3940Sstevel@tonic-gate 	 *
3950Sstevel@tonic-gate 	 * If this is a read/only mount; ignore the error.
3960Sstevel@tonic-gate 	 * At a later time umount/fsck will repair the fs.
3970Sstevel@tonic-gate 	 *
3980Sstevel@tonic-gate 	 */
3990Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
4000Sstevel@tonic-gate 		if (!ronly) {
4010Sstevel@tonic-gate 			lufs_unsnarf(ufsvfsp);
4020Sstevel@tonic-gate 			return (EIO);
4030Sstevel@tonic-gate 		}
4040Sstevel@tonic-gate 		ul->un_flags &= ~LDL_ERROR;
4050Sstevel@tonic-gate 	}
4060Sstevel@tonic-gate 	if (!ronly)
4070Sstevel@tonic-gate 		logmap_start_roll(ul);
4080Sstevel@tonic-gate 	return (0);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate static int
4120Sstevel@tonic-gate lufs_initialize(
4130Sstevel@tonic-gate 	ufsvfs_t *ufsvfsp,
4140Sstevel@tonic-gate 	daddr_t bno,
4150Sstevel@tonic-gate 	size_t nb,
4160Sstevel@tonic-gate 	struct fiolog *flp)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	ml_odunit_t	*ud, *ud2;
4190Sstevel@tonic-gate 	buf_t		*bp;
4200Sstevel@tonic-gate 	struct timeval	tv;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/* LINTED: warning: logical expression always true: op "||" */
4230Sstevel@tonic-gate 	ASSERT(sizeof (ml_odunit_t) < DEV_BSIZE);
4240Sstevel@tonic-gate 	ASSERT(nb >= ldl_minlogsize);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	bp = UFS_GETBLK(ufsvfsp, ufsvfsp->vfs_dev, bno, dbtob(LS_SECTORS));
4270Sstevel@tonic-gate 	bzero(bp->b_un.b_addr, bp->b_bcount);
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 	ud = (void *)bp->b_un.b_addr;
4300Sstevel@tonic-gate 	ud->od_version = LUFS_VERSION_LATEST;
4310Sstevel@tonic-gate 	ud->od_maxtransfer = MIN(ufsvfsp->vfs_iotransz, ldl_maxtransfer);
4320Sstevel@tonic-gate 	if (ud->od_maxtransfer < ldl_mintransfer)
4330Sstevel@tonic-gate 		ud->od_maxtransfer = ldl_mintransfer;
4340Sstevel@tonic-gate 	ud->od_devbsize = DEV_BSIZE;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	ud->od_requestsize = flp->nbytes_actual;
4370Sstevel@tonic-gate 	ud->od_statesize = dbtob(LS_SECTORS);
4380Sstevel@tonic-gate 	ud->od_logsize = nb - ud->od_statesize;
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	ud->od_statebno = INT32_C(0);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	uniqtime(&tv);
4430Sstevel@tonic-gate 	if (tv.tv_usec == last_loghead_ident) {
4440Sstevel@tonic-gate 		tv.tv_usec++;
4450Sstevel@tonic-gate 	}
4460Sstevel@tonic-gate 	last_loghead_ident = tv.tv_usec;
4470Sstevel@tonic-gate 	ud->od_head_ident = tv.tv_usec;
4480Sstevel@tonic-gate 	ud->od_tail_ident = ud->od_head_ident;
4490Sstevel@tonic-gate 	ud->od_chksum = ud->od_head_ident + ud->od_tail_ident;
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	ud->od_bol_lof = dbtob(ud->od_statebno) + ud->od_statesize;
4520Sstevel@tonic-gate 	ud->od_eol_lof = ud->od_bol_lof + ud->od_logsize;
4530Sstevel@tonic-gate 	ud->od_head_lof = ud->od_bol_lof;
4540Sstevel@tonic-gate 	ud->od_tail_lof = ud->od_bol_lof;
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	ASSERT(lufs_initialize_debug(ud));
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	ud2 = (void *)(bp->b_un.b_addr + DEV_BSIZE);
4590Sstevel@tonic-gate 	bcopy(ud, ud2, sizeof (*ud));
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
4620Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
4630Sstevel@tonic-gate 		brelse(bp);
4640Sstevel@tonic-gate 		return (EIO);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	brelse(bp);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	return (0);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Free log space
4730Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
4740Sstevel@tonic-gate  */
4750Sstevel@tonic-gate static int
4760Sstevel@tonic-gate lufs_free(struct ufsvfs *ufsvfsp)
4770Sstevel@tonic-gate {
4780Sstevel@tonic-gate 	int		error = 0, i, j;
4790Sstevel@tonic-gate 	buf_t		*bp = NULL;
4800Sstevel@tonic-gate 	extent_t	*ep;
4810Sstevel@tonic-gate 	extent_block_t	*ebp;
4820Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
4830Sstevel@tonic-gate 	daddr_t		fno;
4840Sstevel@tonic-gate 	int32_t		logbno;
4850Sstevel@tonic-gate 	long		nfno;
4860Sstevel@tonic-gate 	inode_t		*ip = NULL;
4870Sstevel@tonic-gate 	char		clean;
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate 	/*
4900Sstevel@tonic-gate 	 * Nothing to free
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	if (fs->fs_logbno == 0)
4930Sstevel@tonic-gate 		return (0);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE and no log but honor the
4970Sstevel@tonic-gate 	 * current value of fs_reclaim.  The reclaim thread could have
4980Sstevel@tonic-gate 	 * been active when lufs_disable() was called and if fs_reclaim
4990Sstevel@tonic-gate 	 * is reset to zero here it could lead to lost inodes.
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
5020Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
5030Sstevel@tonic-gate 	clean = fs->fs_clean;
5040Sstevel@tonic-gate 	logbno = fs->fs_logbno;
5050Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
5060Sstevel@tonic-gate 	fs->fs_logbno = INT32_C(0);
5070Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
5080Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
5090Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
5100Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
5110Sstevel@tonic-gate 		error = EIO;
5120Sstevel@tonic-gate 		fs->fs_clean = clean;
5130Sstevel@tonic-gate 		fs->fs_logbno = logbno;
5140Sstevel@tonic-gate 		goto errout;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * fetch the allocation block
5190Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
5200Sstevel@tonic-gate 	 */
5210Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, logbtodb(fs, logbno),
5220Sstevel@tonic-gate 	    fs->fs_bsize);
5230Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
5240Sstevel@tonic-gate 		error = EIO;
5250Sstevel@tonic-gate 		goto errout;
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	/*
5290Sstevel@tonic-gate 	 * Free up the allocated space (dummy inode needed for free())
5300Sstevel@tonic-gate 	 */
5310Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
5320Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
5330Sstevel@tonic-gate 	for (i = 0, ep = &ebp->extents[0]; i < ebp->nextents; ++i, ++ep) {
5340Sstevel@tonic-gate 		fno = logbtofrag(fs, ep->pbno);
5350Sstevel@tonic-gate 		nfno = dbtofsb(fs, ep->nbno);
5360Sstevel@tonic-gate 		for (j = 0; j < nfno; j += fs->fs_frag, fno += fs->fs_frag)
5370Sstevel@tonic-gate 			free(ip, fno, fs->fs_bsize, 0);
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate 	free(ip, logbtofrag(fs, logbno), fs->fs_bsize, 0);
5400Sstevel@tonic-gate 	brelse(bp);
5410Sstevel@tonic-gate 	bp = NULL;
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/*
5440Sstevel@tonic-gate 	 * Push the metadata dirtied during the allocations
5450Sstevel@tonic-gate 	 */
5460Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
5470Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
5480Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
5490Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
5500Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 0);
5510Sstevel@tonic-gate 	if (error)
5520Sstevel@tonic-gate 		goto errout;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	/*
5550Sstevel@tonic-gate 	 * Free the dummy inode
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	ufs_free_inode(ip);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	return (0);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate errout:
5620Sstevel@tonic-gate 	/*
5630Sstevel@tonic-gate 	 * Free up all resources
5640Sstevel@tonic-gate 	 */
5650Sstevel@tonic-gate 	if (bp)
5660Sstevel@tonic-gate 		brelse(bp);
5670Sstevel@tonic-gate 	if (ip)
5680Sstevel@tonic-gate 		ufs_free_inode(ip);
5690Sstevel@tonic-gate 	return (error);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate  * Allocate log space
5740Sstevel@tonic-gate  *	Assumes the file system is write locked and is not logging
5750Sstevel@tonic-gate  */
5760Sstevel@tonic-gate static int
5770Sstevel@tonic-gate lufs_alloc(struct ufsvfs *ufsvfsp, struct fiolog *flp, cred_t *cr)
5780Sstevel@tonic-gate {
5790Sstevel@tonic-gate 	int		error = 0;
5800Sstevel@tonic-gate 	buf_t		*bp = NULL;
5810Sstevel@tonic-gate 	extent_t	*ep, *nep;
5820Sstevel@tonic-gate 	extent_block_t	*ebp;
5830Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
5840Sstevel@tonic-gate 	daddr_t		fno;	/* in frags */
5850Sstevel@tonic-gate 	daddr_t		bno;	/* in disk blocks */
5860Sstevel@tonic-gate 	int32_t		logbno = INT32_C(0);	/* will be fs_logbno */
5870Sstevel@tonic-gate 	struct inode	*ip = NULL;
5880Sstevel@tonic-gate 	size_t		nb = flp->nbytes_actual;
5890Sstevel@tonic-gate 	size_t		tb = 0;
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * Mark the file system as FSACTIVE
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
5950Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
5960Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
5970Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
5980Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
5990Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 	/*
6020Sstevel@tonic-gate 	 * Allocate the allocation block (need dummy shadow inode;
6030Sstevel@tonic-gate 	 * we use a shadow inode so the quota sub-system ignores
6040Sstevel@tonic-gate 	 * the block allocations.)
6050Sstevel@tonic-gate 	 *	superblock -> one block of extents -> log data
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	ip = ufs_alloc_inode(ufsvfsp, UFSROOTINO);
6080Sstevel@tonic-gate 	ip->i_mode = IFSHAD;		/* make the dummy a shadow inode */
6090Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
6100Sstevel@tonic-gate 	fno = contigpref(ufsvfsp, nb + fs->fs_bsize);
6110Sstevel@tonic-gate 	error = alloc(ip, fno, fs->fs_bsize, &fno, cr);
6120Sstevel@tonic-gate 	if (error)
6130Sstevel@tonic-gate 		goto errout;
6140Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ufsvfsp->vfs_dev, bno, fs->fs_bsize);
6170Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6180Sstevel@tonic-gate 		error = EIO;
6190Sstevel@tonic-gate 		goto errout;
6200Sstevel@tonic-gate 	}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	ebp = (void *)bp->b_un.b_addr;
6230Sstevel@tonic-gate 	ebp->type = LUFS_EXTENTS;
6240Sstevel@tonic-gate 	ebp->nextbno = UINT32_C(0);
6250Sstevel@tonic-gate 	ebp->nextents = UINT32_C(0);
6260Sstevel@tonic-gate 	ebp->chksum = INT32_C(0);
6270Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
6280Sstevel@tonic-gate 		logbno = bno;
6290Sstevel@tonic-gate 	else
6300Sstevel@tonic-gate 		logbno = dbtofsb(fs, bno);
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	/*
6330Sstevel@tonic-gate 	 * Initialize the first extent
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	ep = &ebp->extents[0];
6360Sstevel@tonic-gate 	error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
6370Sstevel@tonic-gate 	if (error)
6380Sstevel@tonic-gate 		goto errout;
6390Sstevel@tonic-gate 	bno = fsbtodb(fs, fno);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	ep->lbno = UINT32_C(0);
6420Sstevel@tonic-gate 	if (fs->fs_magic == FS_MAGIC)
6430Sstevel@tonic-gate 		ep->pbno = (uint32_t)bno;
6440Sstevel@tonic-gate 	else
6450Sstevel@tonic-gate 		ep->pbno = (uint32_t)fno;
6460Sstevel@tonic-gate 	ep->nbno = (uint32_t)fsbtodb(fs, fs->fs_frag);
6470Sstevel@tonic-gate 	ebp->nextents = UINT32_C(1);
6480Sstevel@tonic-gate 	tb = fs->fs_bsize;
6490Sstevel@tonic-gate 	nb -= fs->fs_bsize;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	while (nb) {
6520Sstevel@tonic-gate 		error = alloc(ip, fno + fs->fs_frag, fs->fs_bsize, &fno, cr);
6530Sstevel@tonic-gate 		if (error) {
6540Sstevel@tonic-gate 			if (tb < ldl_minlogsize)
6550Sstevel@tonic-gate 				goto errout;
6560Sstevel@tonic-gate 			error = 0;
6570Sstevel@tonic-gate 			break;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 		bno = fsbtodb(fs, fno);
6600Sstevel@tonic-gate 		if ((daddr_t)((logbtodb(fs, ep->pbno) + ep->nbno) == bno))
6610Sstevel@tonic-gate 			ep->nbno += (uint32_t)(fsbtodb(fs, fs->fs_frag));
6620Sstevel@tonic-gate 		else {
6630Sstevel@tonic-gate 			nep = ep + 1;
6640Sstevel@tonic-gate 			if ((caddr_t)(nep + 1) >
6650Sstevel@tonic-gate 			    (bp->b_un.b_addr + fs->fs_bsize)) {
6660Sstevel@tonic-gate 				free(ip, fno, fs->fs_bsize, 0);
6670Sstevel@tonic-gate 				break;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			nep->lbno = ep->lbno + ep->nbno;
6700Sstevel@tonic-gate 			if (fs->fs_magic == FS_MAGIC)
6710Sstevel@tonic-gate 				nep->pbno = (uint32_t)bno;
6720Sstevel@tonic-gate 			else
6730Sstevel@tonic-gate 				nep->pbno = (uint32_t)fno;
6740Sstevel@tonic-gate 			nep->nbno = (uint32_t)(fsbtodb(fs, fs->fs_frag));
6750Sstevel@tonic-gate 			ebp->nextents++;
6760Sstevel@tonic-gate 			ep = nep;
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 		tb += fs->fs_bsize;
6790Sstevel@tonic-gate 		nb -= fs->fs_bsize;
6800Sstevel@tonic-gate 	}
6810Sstevel@tonic-gate 	ebp->nbytes = (uint32_t)tb;
6820Sstevel@tonic-gate 	setsum(&ebp->chksum, (int32_t *)bp->b_un.b_addr, fs->fs_bsize);
6830Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
6840Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6850Sstevel@tonic-gate 		error = EIO;
6860Sstevel@tonic-gate 		goto errout;
6870Sstevel@tonic-gate 	}
6880Sstevel@tonic-gate 	/*
6890Sstevel@tonic-gate 	 * Initialize the first two sectors of the log
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	error = lufs_initialize(ufsvfsp, logbtodb(fs, ebp->extents[0].pbno),
6920Sstevel@tonic-gate 	    tb, flp);
6930Sstevel@tonic-gate 	if (error)
6940Sstevel@tonic-gate 		goto errout;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	/*
6970Sstevel@tonic-gate 	 * We are done initializing the allocation block and the log
6980Sstevel@tonic-gate 	 */
6990Sstevel@tonic-gate 	brelse(bp);
7000Sstevel@tonic-gate 	bp = NULL;
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 	/*
7030Sstevel@tonic-gate 	 * Update the superblock and push the dirty metadata
7040Sstevel@tonic-gate 	 */
7050Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
7060Sstevel@tonic-gate 	sbupdate(ufsvfsp->vfs_vfs);
7070Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
7080Sstevel@tonic-gate 	bflush(ufsvfsp->vfs_dev);
7090Sstevel@tonic-gate 	error = bfinval(ufsvfsp->vfs_dev, 1);
7100Sstevel@tonic-gate 	if (error)
7110Sstevel@tonic-gate 		goto errout;
7120Sstevel@tonic-gate 	if (ufsvfsp->vfs_bufp->b_flags & B_ERROR) {
7130Sstevel@tonic-gate 		error = EIO;
7140Sstevel@tonic-gate 		goto errout;
7150Sstevel@tonic-gate 	}
7160Sstevel@tonic-gate 
7170Sstevel@tonic-gate 	/*
7180Sstevel@tonic-gate 	 * Everything is safely on disk; update log space pointer in sb
7190Sstevel@tonic-gate 	 */
7200Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = curthread;
7210Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
7220Sstevel@tonic-gate 	fs->fs_logbno = (uint32_t)logbno;
7230Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
7240Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
7250Sstevel@tonic-gate 	ufsvfsp->vfs_ulockfs.ul_sbowner = (kthread_id_t)-1;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/*
7280Sstevel@tonic-gate 	 * Free the dummy inode
7290Sstevel@tonic-gate 	 */
7300Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
7310Sstevel@tonic-gate 	ufs_free_inode(ip);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	/* inform user of real log size */
7340Sstevel@tonic-gate 	flp->nbytes_actual = tb;
7350Sstevel@tonic-gate 	return (0);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate errout:
7380Sstevel@tonic-gate 	/*
7390Sstevel@tonic-gate 	 * Free all resources
7400Sstevel@tonic-gate 	 */
7410Sstevel@tonic-gate 	if (bp)
7420Sstevel@tonic-gate 		brelse(bp);
7430Sstevel@tonic-gate 	if (logbno) {
7440Sstevel@tonic-gate 		fs->fs_logbno = logbno;
7450Sstevel@tonic-gate 		(void) lufs_free(ufsvfsp);
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	if (ip) {
7480Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
7490Sstevel@tonic-gate 		ufs_free_inode(ip);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 	return (error);
7520Sstevel@tonic-gate }
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * Disable logging
7560Sstevel@tonic-gate  */
7570Sstevel@tonic-gate int
7580Sstevel@tonic-gate lufs_disable(vnode_t *vp, struct fiolog *flp)
7590Sstevel@tonic-gate {
7600Sstevel@tonic-gate 	int		error = 0;
7610Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
7620Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
7630Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
7640Sstevel@tonic-gate 	struct lockfs	lf;
7650Sstevel@tonic-gate 	struct ulockfs	*ulp;
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/*
7700Sstevel@tonic-gate 	 * Logging is already disabled; done
7710Sstevel@tonic-gate 	 */
7720Sstevel@tonic-gate 	if (fs->fs_logbno == 0 || ufsvfsp->vfs_log == NULL)
7730Sstevel@tonic-gate 		return (0);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/*
7760Sstevel@tonic-gate 	 * Readonly file system
7770Sstevel@tonic-gate 	 */
7780Sstevel@tonic-gate 	if (fs->fs_ronly) {
7790Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
7800Sstevel@tonic-gate 		return (0);
7810Sstevel@tonic-gate 	}
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * File system must be write locked to disable logging
7850Sstevel@tonic-gate 	 */
7860Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
7870Sstevel@tonic-gate 	if (error) {
7880Sstevel@tonic-gate 		return (error);
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
7910Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
7920Sstevel@tonic-gate 		return (0);
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
7950Sstevel@tonic-gate 	lf.lf_flags = 0;
7960Sstevel@tonic-gate 	lf.lf_comment = NULL;
7970Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
7980Sstevel@tonic-gate 	if (error) {
7990Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
8000Sstevel@tonic-gate 		return (0);
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	if (ufsvfsp->vfs_log == NULL || fs->fs_logbno == 0)
8040Sstevel@tonic-gate 		goto errout;
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate 	/*
8070Sstevel@tonic-gate 	 * WE ARE COMMITTED TO DISABLING LOGGING PAST THIS POINT
8080Sstevel@tonic-gate 	 */
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	/*
8110Sstevel@tonic-gate 	 * Disable logging:
8120Sstevel@tonic-gate 	 * Suspend the reclaim thread and force the delete thread to exit.
8130Sstevel@tonic-gate 	 *	When a nologging mount has completed there may still be
8140Sstevel@tonic-gate 	 *	work for reclaim to do so just suspend this thread until
8150Sstevel@tonic-gate 	 *	it's [deadlock-] safe for it to continue.  The delete
8160Sstevel@tonic-gate 	 *	thread won't be needed as ufs_iinactive() calls
8170Sstevel@tonic-gate 	 *	ufs_delete() when logging is disabled.
8180Sstevel@tonic-gate 	 * Freeze and drain reader ops.
8190Sstevel@tonic-gate 	 *	Commit any outstanding reader transactions (ufs_flush).
8200Sstevel@tonic-gate 	 *	Set the ``unmounted'' bit in the ufstrans struct.
8210Sstevel@tonic-gate 	 *	If debug, remove metadata from matamap.
8220Sstevel@tonic-gate 	 *	Disable matamap processing.
8230Sstevel@tonic-gate 	 *	NULL the trans ops table.
8240Sstevel@tonic-gate 	 *	Free all of the incore structs related to logging.
8250Sstevel@tonic-gate 	 * Allow reader ops.
8260Sstevel@tonic-gate 	 */
8270Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_reclaim);
8280Sstevel@tonic-gate 	ufs_thread_exit(&ufsvfsp->vfs_delete);
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	vfs_lock_wait(ufsvfsp->vfs_vfs);
8310Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
8320Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
833*329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
8340Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	(void) ufs_flush(ufsvfsp->vfs_vfs);
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	TRANS_MATA_UMOUNT(ufsvfsp);
8390Sstevel@tonic-gate 	ufsvfsp->vfs_domatamap = 0;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	/*
8420Sstevel@tonic-gate 	 * Free all of the incore structs
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
8450Sstevel@tonic-gate 
846*329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
8470Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
8480Sstevel@tonic-gate 	vfs_setmntopt(ufsvfsp->vfs_vfs, MNTOPT_NOLOGGING, NULL, 0);
8490Sstevel@tonic-gate 	vfs_unlock(ufsvfsp->vfs_vfs);
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	fs->fs_rolled = FS_ALL_ROLLED;
8520Sstevel@tonic-gate 	ufsvfsp->vfs_nolog_si = 0;
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	/*
8550Sstevel@tonic-gate 	 * Free the log space and mark the superblock as FSACTIVE
8560Sstevel@tonic-gate 	 */
8570Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
8580Sstevel@tonic-gate 
8590Sstevel@tonic-gate 	/*
8600Sstevel@tonic-gate 	 * Allow the reclaim thread to continue.
8610Sstevel@tonic-gate 	 */
8620Sstevel@tonic-gate 	ufs_thread_continue(&ufsvfsp->vfs_reclaim);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*
8650Sstevel@tonic-gate 	 * Unlock the file system
8660Sstevel@tonic-gate 	 */
8670Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
8680Sstevel@tonic-gate 	lf.lf_flags = 0;
8690Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
8700Sstevel@tonic-gate 	if (error)
8710Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	return (0);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate errout:
8760Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
8770Sstevel@tonic-gate 	lf.lf_flags = 0;
8780Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
8790Sstevel@tonic-gate 	return (error);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate /*
8830Sstevel@tonic-gate  * Enable logging
8840Sstevel@tonic-gate  */
8850Sstevel@tonic-gate int
8860Sstevel@tonic-gate lufs_enable(struct vnode *vp, struct fiolog *flp, cred_t *cr)
8870Sstevel@tonic-gate {
8880Sstevel@tonic-gate 	int		error;
8890Sstevel@tonic-gate 	int		reclaim;
8900Sstevel@tonic-gate 	inode_t		*ip = VTOI(vp);
8910Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
8920Sstevel@tonic-gate 	struct fs	*fs;
8930Sstevel@tonic-gate 	ml_unit_t	*ul;
8940Sstevel@tonic-gate 	struct lockfs	lf;
8950Sstevel@tonic-gate 	struct ulockfs	*ulp;
8960Sstevel@tonic-gate 	vfs_t		*vfsp = ufsvfsp->vfs_vfs;
8970Sstevel@tonic-gate 	uint64_t	tmp_nbytes_actual;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * Check if logging is already enabled
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
9030Sstevel@tonic-gate 		flp->error = FIOLOG_ETRANS;
9040Sstevel@tonic-gate 		/* for root ensure logging option is set */
9050Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
9060Sstevel@tonic-gate 		return (0);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	/*
9110Sstevel@tonic-gate 	 * Come back here to recheck if we had to disable the log.
9120Sstevel@tonic-gate 	 */
9130Sstevel@tonic-gate recheck:
9140Sstevel@tonic-gate 	error = 0;
9150Sstevel@tonic-gate 	reclaim = 0;
9160Sstevel@tonic-gate 	flp->error = FIOLOG_ENONE;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/*
9190Sstevel@tonic-gate 	 * Adjust requested log size
9200Sstevel@tonic-gate 	 */
9210Sstevel@tonic-gate 	flp->nbytes_actual = flp->nbytes_requested;
9220Sstevel@tonic-gate 	if (flp->nbytes_actual == 0) {
9230Sstevel@tonic-gate 		tmp_nbytes_actual =
9240Sstevel@tonic-gate 		    (((uint64_t)fs->fs_size) / ldl_divisor) << fs->fs_fshift;
9250Sstevel@tonic-gate 		flp->nbytes_actual = (uint_t)MIN(tmp_nbytes_actual, INT_MAX);
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 	flp->nbytes_actual = MAX(flp->nbytes_actual, ldl_minlogsize);
9280Sstevel@tonic-gate 	flp->nbytes_actual = MIN(flp->nbytes_actual, ldl_maxlogsize);
9290Sstevel@tonic-gate 	flp->nbytes_actual = blkroundup(fs, flp->nbytes_actual);
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/*
9320Sstevel@tonic-gate 	 * logging is enabled and the log is the right size; done
9330Sstevel@tonic-gate 	 */
9340Sstevel@tonic-gate 	ul = ufsvfsp->vfs_log;
9350Sstevel@tonic-gate 	if (ul && fs->fs_logbno && (flp->nbytes_actual == ul->un_requestsize))
9360Sstevel@tonic-gate 			return (0);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	/*
9390Sstevel@tonic-gate 	 * Readonly file system
9400Sstevel@tonic-gate 	 */
9410Sstevel@tonic-gate 	if (fs->fs_ronly) {
9420Sstevel@tonic-gate 		flp->error = FIOLOG_EROFS;
9430Sstevel@tonic-gate 		return (0);
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	/*
9470Sstevel@tonic-gate 	 * File system must be write locked to enable logging
9480Sstevel@tonic-gate 	 */
9490Sstevel@tonic-gate 	error = ufs_fiolfss(vp, &lf);
9500Sstevel@tonic-gate 	if (error) {
9510Sstevel@tonic-gate 		return (error);
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 	if (!LOCKFS_IS_ULOCK(&lf)) {
9540Sstevel@tonic-gate 		flp->error = FIOLOG_EULOCK;
9550Sstevel@tonic-gate 		return (0);
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_WLOCK;
9580Sstevel@tonic-gate 	lf.lf_flags = 0;
9590Sstevel@tonic-gate 	lf.lf_comment = NULL;
9600Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
9610Sstevel@tonic-gate 	if (error) {
9620Sstevel@tonic-gate 		flp->error = FIOLOG_EWLOCK;
9630Sstevel@tonic-gate 		return (0);
9640Sstevel@tonic-gate 	}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	/*
9670Sstevel@tonic-gate 	 * File system must be fairly consistent to enable logging
9680Sstevel@tonic-gate 	 */
9690Sstevel@tonic-gate 	if (fs->fs_clean != FSLOG &&
9700Sstevel@tonic-gate 	    fs->fs_clean != FSACTIVE &&
9710Sstevel@tonic-gate 	    fs->fs_clean != FSSTABLE &&
9720Sstevel@tonic-gate 	    fs->fs_clean != FSCLEAN) {
9730Sstevel@tonic-gate 		flp->error = FIOLOG_ECLEAN;
9740Sstevel@tonic-gate 		goto unlockout;
9750Sstevel@tonic-gate 	}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	/*
9780Sstevel@tonic-gate 	 * A write-locked file system is only active if there are
9790Sstevel@tonic-gate 	 * open deleted files; so remember to set FS_RECLAIM later.
9800Sstevel@tonic-gate 	 */
9810Sstevel@tonic-gate 	if (fs->fs_clean == FSACTIVE)
9820Sstevel@tonic-gate 		reclaim = FS_RECLAIM;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	/*
9850Sstevel@tonic-gate 	 * Logging is already enabled; must be changing the log's size
9860Sstevel@tonic-gate 	 */
9870Sstevel@tonic-gate 	if (fs->fs_logbno && ufsvfsp->vfs_log) {
9880Sstevel@tonic-gate 		/*
9890Sstevel@tonic-gate 		 * Before we can disable logging, we must give up our
9900Sstevel@tonic-gate 		 * lock.  As a consequence of unlocking and disabling the
9910Sstevel@tonic-gate 		 * log, the fs structure may change.  Because of this, when
9920Sstevel@tonic-gate 		 * disabling is complete, we will go back to recheck to
9930Sstevel@tonic-gate 		 * repeat all of the checks that we performed to get to
9940Sstevel@tonic-gate 		 * this point.  Disabling sets fs->fs_logbno to 0, so this
9950Sstevel@tonic-gate 		 * will not put us into an infinite loop.
9960Sstevel@tonic-gate 		 */
9970Sstevel@tonic-gate 		lf.lf_lock = LOCKFS_ULOCK;
9980Sstevel@tonic-gate 		lf.lf_flags = 0;
9990Sstevel@tonic-gate 		error = ufs_fiolfs(vp, &lf, 1);
10000Sstevel@tonic-gate 		if (error) {
10010Sstevel@tonic-gate 			flp->error = FIOLOG_ENOULOCK;
10020Sstevel@tonic-gate 			return (0);
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 		error = lufs_disable(vp, flp);
10050Sstevel@tonic-gate 		if (error || (flp->error != FIOLOG_ENONE))
10060Sstevel@tonic-gate 			return (0);
10070Sstevel@tonic-gate 		goto recheck;
10080Sstevel@tonic-gate 	}
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 	error = lufs_alloc(ufsvfsp, flp, cr);
10110Sstevel@tonic-gate 	if (error)
10120Sstevel@tonic-gate 		goto errout;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 	/*
10150Sstevel@tonic-gate 	 * Create all of the incore structs
10160Sstevel@tonic-gate 	 */
10170Sstevel@tonic-gate 	error = lufs_snarf(ufsvfsp, fs, 0);
10180Sstevel@tonic-gate 	if (error)
10190Sstevel@tonic-gate 		goto errout;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate 	/*
10220Sstevel@tonic-gate 	 * DON'T ``GOTO ERROUT'' PAST THIS POINT
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 	/*
10260Sstevel@tonic-gate 	 * Pretend we were just mounted with logging enabled
10270Sstevel@tonic-gate 	 *	freeze and drain the file system of readers
10280Sstevel@tonic-gate 	 *		Get the ops vector
10290Sstevel@tonic-gate 	 *		If debug, record metadata locations with log subsystem
10300Sstevel@tonic-gate 	 *		Start the delete thread
10310Sstevel@tonic-gate 	 *		Start the reclaim thread, if necessary
10320Sstevel@tonic-gate 	 *	Thaw readers
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	vfs_lock_wait(vfsp);
10350Sstevel@tonic-gate 	vfs_setmntopt(vfsp, MNTOPT_LOGGING, NULL, 0);
10360Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
10370Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
1038*329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
10390Sstevel@tonic-gate 	(void) ufs_quiesce(ulp);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	TRANS_DOMATAMAP(ufsvfsp);
10420Sstevel@tonic-gate 	TRANS_MATA_MOUNT(ufsvfsp);
10430Sstevel@tonic-gate 	TRANS_MATA_SI(ufsvfsp, fs);
10440Sstevel@tonic-gate 	ufs_thread_start(&ufsvfsp->vfs_delete, ufs_thread_delete, vfsp);
10450Sstevel@tonic-gate 	if (fs->fs_reclaim & (FS_RECLAIM|FS_RECLAIMING)) {
10460Sstevel@tonic-gate 		fs->fs_reclaim &= ~FS_RECLAIM;
10470Sstevel@tonic-gate 		fs->fs_reclaim |=  FS_RECLAIMING;
10480Sstevel@tonic-gate 		ufs_thread_start(&ufsvfsp->vfs_reclaim,
10490Sstevel@tonic-gate 					ufs_thread_reclaim, vfsp);
10500Sstevel@tonic-gate 	} else
10510Sstevel@tonic-gate 		fs->fs_reclaim |= reclaim;
10520Sstevel@tonic-gate 
1053*329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
10540Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
10550Sstevel@tonic-gate 	vfs_unlock(vfsp);
10560Sstevel@tonic-gate 
10570Sstevel@tonic-gate 	/*
10580Sstevel@tonic-gate 	 * Unlock the file system
10590Sstevel@tonic-gate 	 */
10600Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
10610Sstevel@tonic-gate 	lf.lf_flags = 0;
10620Sstevel@tonic-gate 	error = ufs_fiolfs(vp, &lf, 1);
10630Sstevel@tonic-gate 	if (error) {
10640Sstevel@tonic-gate 		flp->error = FIOLOG_ENOULOCK;
10650Sstevel@tonic-gate 		return (0);
10660Sstevel@tonic-gate 	}
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	/*
10690Sstevel@tonic-gate 	 * There's nothing in the log yet (we've just allocated it)
10700Sstevel@tonic-gate 	 * so directly write out the super block.
10710Sstevel@tonic-gate 	 * Note, we have to force this sb out to disk
10720Sstevel@tonic-gate 	 * (not just to the log) so that if we crash we know we are logging
10730Sstevel@tonic-gate 	 */
10740Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
10750Sstevel@tonic-gate 	fs->fs_clean = FSLOG;
10760Sstevel@tonic-gate 	fs->fs_rolled = FS_NEED_ROLL; /* Mark the fs as unrolled */
10770Sstevel@tonic-gate 	UFS_BWRITE2(NULL, ufsvfsp->vfs_bufp);
10780Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	return (0);
10810Sstevel@tonic-gate 
10820Sstevel@tonic-gate errout:
10830Sstevel@tonic-gate 	(void) lufs_unsnarf(ufsvfsp);
10840Sstevel@tonic-gate 	(void) lufs_free(ufsvfsp);
10850Sstevel@tonic-gate unlockout:
10860Sstevel@tonic-gate 	lf.lf_lock = LOCKFS_ULOCK;
10870Sstevel@tonic-gate 	lf.lf_flags = 0;
10880Sstevel@tonic-gate 	(void) ufs_fiolfs(vp, &lf, 1);
10890Sstevel@tonic-gate 	return (error);
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate void
10930Sstevel@tonic-gate lufs_read_strategy(ml_unit_t *ul, buf_t *bp)
10940Sstevel@tonic-gate {
10950Sstevel@tonic-gate 	mt_map_t	*logmap	= ul->un_logmap;
10960Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
10970Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
10980Sstevel@tonic-gate 	mapentry_t	*age;
10990Sstevel@tonic-gate 	char		*va;
11000Sstevel@tonic-gate 	int		(*saviodone)();
11010Sstevel@tonic-gate 	int		entire_range;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	/*
11040Sstevel@tonic-gate 	 * get a linked list of overlapping deltas
11050Sstevel@tonic-gate 	 * returns with &mtm->mtm_rwlock held
11060Sstevel@tonic-gate 	 */
11070Sstevel@tonic-gate 	entire_range = logmap_list_get(logmap, mof, nb, &age);
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 	/*
11100Sstevel@tonic-gate 	 * no overlapping deltas were found; read master
11110Sstevel@tonic-gate 	 */
11120Sstevel@tonic-gate 	if (age == NULL) {
11130Sstevel@tonic-gate 		rw_exit(&logmap->mtm_rwlock);
11140Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
11150Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
11160Sstevel@tonic-gate 			bp->b_error = EIO;
11170Sstevel@tonic-gate 			biodone(bp);
11180Sstevel@tonic-gate 		} else {
11190Sstevel@tonic-gate 			ul->un_ufsvfs->vfs_iotstamp = lbolt;
11200Sstevel@tonic-gate 			logstats.ls_lreads.value.ui64++;
11210Sstevel@tonic-gate 			(void) bdev_strategy(bp);
11220Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
11230Sstevel@tonic-gate 		}
11240Sstevel@tonic-gate 		return;
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	va = bp_mapin_common(bp, VM_SLEEP);
11280Sstevel@tonic-gate 	/*
11290Sstevel@tonic-gate 	 * if necessary, sync read the data from master
11300Sstevel@tonic-gate 	 *	errors are returned in bp
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if (!entire_range) {
11330Sstevel@tonic-gate 		saviodone = bp->b_iodone;
11340Sstevel@tonic-gate 		bp->b_iodone = trans_not_done;
11350Sstevel@tonic-gate 		logstats.ls_mreads.value.ui64++;
11360Sstevel@tonic-gate 		(void) bdev_strategy(bp);
11370Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
11380Sstevel@tonic-gate 		if (trans_not_wait(bp))
11390Sstevel@tonic-gate 			ldl_seterror(ul, "Error reading master");
11400Sstevel@tonic-gate 		bp->b_iodone = saviodone;
11410Sstevel@tonic-gate 	}
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/*
11440Sstevel@tonic-gate 	 * sync read the data from the log
11450Sstevel@tonic-gate 	 *	errors are returned inline
11460Sstevel@tonic-gate 	 */
11470Sstevel@tonic-gate 	if (ldl_read(ul, va, mof, nb, age)) {
11480Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
11490Sstevel@tonic-gate 		bp->b_error = EIO;
11500Sstevel@tonic-gate 	}
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * unlist the deltas
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 	logmap_list_put(logmap, age);
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	/*
11580Sstevel@tonic-gate 	 * all done
11590Sstevel@tonic-gate 	 */
11600Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
11610Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
11620Sstevel@tonic-gate 		bp->b_error = EIO;
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	biodone(bp);
11650Sstevel@tonic-gate }
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate void
11680Sstevel@tonic-gate lufs_write_strategy(ml_unit_t *ul, buf_t *bp)
11690Sstevel@tonic-gate {
11700Sstevel@tonic-gate 	offset_t	mof	= ldbtob(bp->b_blkno);
11710Sstevel@tonic-gate 	off_t		nb	= bp->b_bcount;
11720Sstevel@tonic-gate 	char		*va;
11730Sstevel@tonic-gate 	mapentry_t	*me;
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	ASSERT((nb & DEV_BMASK) == 0);
11760Sstevel@tonic-gate 	ul->un_logmap->mtm_ref = 1;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * if there are deltas, move into log
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	me = deltamap_remove(ul->un_deltamap, mof, nb);
11820Sstevel@tonic-gate 	if (me) {
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 		va = bp_mapin_common(bp, VM_SLEEP);
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		ASSERT(((ul->un_debug & MT_WRITE_CHECK) == 0) ||
11870Sstevel@tonic-gate 			(ul->un_matamap == NULL)||
11880Sstevel@tonic-gate 			matamap_within(ul->un_matamap, mof, nb));
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 		/*
11910Sstevel@tonic-gate 		 * move to logmap
11920Sstevel@tonic-gate 		 */
11930Sstevel@tonic-gate 		if (ufs_crb_enable) {
11940Sstevel@tonic-gate 			logmap_add_buf(ul, va, mof, me,
11950Sstevel@tonic-gate 			    bp->b_un.b_addr, nb);
11960Sstevel@tonic-gate 		} else {
11970Sstevel@tonic-gate 			logmap_add(ul, va, mof, me);
11980Sstevel@tonic-gate 		}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		if (ul->un_flags & LDL_ERROR) {
12010Sstevel@tonic-gate 			bp->b_flags |= B_ERROR;
12020Sstevel@tonic-gate 			bp->b_error = EIO;
12030Sstevel@tonic-gate 		}
12040Sstevel@tonic-gate 		biodone(bp);
12050Sstevel@tonic-gate 		return;
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 	if (ul->un_flags & LDL_ERROR) {
12080Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
12090Sstevel@tonic-gate 		bp->b_error = EIO;
12100Sstevel@tonic-gate 		biodone(bp);
12110Sstevel@tonic-gate 		return;
12120Sstevel@tonic-gate 	}
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 	/*
12150Sstevel@tonic-gate 	 * Check that we are not updating metadata, or if so then via B_PHYS.
12160Sstevel@tonic-gate 	 */
12170Sstevel@tonic-gate 	ASSERT((ul->un_matamap == NULL) ||
12180Sstevel@tonic-gate 		!(matamap_overlap(ul->un_matamap, mof, nb) &&
12190Sstevel@tonic-gate 		((bp->b_flags & B_PHYS) == 0)));
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	ul->un_ufsvfs->vfs_iotstamp = lbolt;
12220Sstevel@tonic-gate 	logstats.ls_lwrites.value.ui64++;
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 	/* If snapshots are enabled, write through the snapshot driver */
12250Sstevel@tonic-gate 	if (ul->un_ufsvfs->vfs_snapshot)
12260Sstevel@tonic-gate 		fssnap_strategy(&ul->un_ufsvfs->vfs_snapshot, bp);
12270Sstevel@tonic-gate 	else
12280Sstevel@tonic-gate 		(void) bdev_strategy(bp);
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate 	lwp_stat_update(LWP_STAT_OUBLK, 1);
12310Sstevel@tonic-gate }
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate void
12340Sstevel@tonic-gate lufs_strategy(ml_unit_t *ul, buf_t *bp)
12350Sstevel@tonic-gate {
12360Sstevel@tonic-gate 	if (bp->b_flags & B_READ)
12370Sstevel@tonic-gate 		lufs_read_strategy(ul, bp);
12380Sstevel@tonic-gate 	else
12390Sstevel@tonic-gate 		lufs_write_strategy(ul, bp);
12400Sstevel@tonic-gate }
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate /* ARGSUSED */
12430Sstevel@tonic-gate static int
12440Sstevel@tonic-gate delta_stats_update(kstat_t *ksp, int rw)
12450Sstevel@tonic-gate {
12460Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {
12470Sstevel@tonic-gate 		delta_stats[DT_SB] = dkstats.ds_superblock_deltas.value.ui64;
12480Sstevel@tonic-gate 		delta_stats[DT_CG] = dkstats.ds_bitmap_deltas.value.ui64;
12490Sstevel@tonic-gate 		delta_stats[DT_SI] = dkstats.ds_suminfo_deltas.value.ui64;
12500Sstevel@tonic-gate 		delta_stats[DT_AB] = dkstats.ds_allocblk_deltas.value.ui64;
12510Sstevel@tonic-gate 		delta_stats[DT_ABZERO] = dkstats.ds_ab0_deltas.value.ui64;
12520Sstevel@tonic-gate 		delta_stats[DT_DIR] = dkstats.ds_dir_deltas.value.ui64;
12530Sstevel@tonic-gate 		delta_stats[DT_INODE] = dkstats.ds_inode_deltas.value.ui64;
12540Sstevel@tonic-gate 		delta_stats[DT_FBI] = dkstats.ds_fbiwrite_deltas.value.ui64;
12550Sstevel@tonic-gate 		delta_stats[DT_QR] = dkstats.ds_quota_deltas.value.ui64;
12560Sstevel@tonic-gate 		delta_stats[DT_SHAD] = dkstats.ds_shadow_deltas.value.ui64;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 		roll_stats[DT_SB] = dkstats.ds_superblock_rolled.value.ui64;
12590Sstevel@tonic-gate 		roll_stats[DT_CG] = dkstats.ds_bitmap_rolled.value.ui64;
12600Sstevel@tonic-gate 		roll_stats[DT_SI] = dkstats.ds_suminfo_rolled.value.ui64;
12610Sstevel@tonic-gate 		roll_stats[DT_AB] = dkstats.ds_allocblk_rolled.value.ui64;
12620Sstevel@tonic-gate 		roll_stats[DT_ABZERO] = dkstats.ds_ab0_rolled.value.ui64;
12630Sstevel@tonic-gate 		roll_stats[DT_DIR] = dkstats.ds_dir_rolled.value.ui64;
12640Sstevel@tonic-gate 		roll_stats[DT_INODE] = dkstats.ds_inode_rolled.value.ui64;
12650Sstevel@tonic-gate 		roll_stats[DT_FBI] = dkstats.ds_fbiwrite_rolled.value.ui64;
12660Sstevel@tonic-gate 		roll_stats[DT_QR] = dkstats.ds_quota_rolled.value.ui64;
12670Sstevel@tonic-gate 		roll_stats[DT_SHAD] = dkstats.ds_shadow_rolled.value.ui64;
12680Sstevel@tonic-gate 	} else {
12690Sstevel@tonic-gate 		dkstats.ds_superblock_deltas.value.ui64 = delta_stats[DT_SB];
12700Sstevel@tonic-gate 		dkstats.ds_bitmap_deltas.value.ui64 = delta_stats[DT_CG];
12710Sstevel@tonic-gate 		dkstats.ds_suminfo_deltas.value.ui64 = delta_stats[DT_SI];
12720Sstevel@tonic-gate 		dkstats.ds_allocblk_deltas.value.ui64 = delta_stats[DT_AB];
12730Sstevel@tonic-gate 		dkstats.ds_ab0_deltas.value.ui64 = delta_stats[DT_ABZERO];
12740Sstevel@tonic-gate 		dkstats.ds_dir_deltas.value.ui64 = delta_stats[DT_DIR];
12750Sstevel@tonic-gate 		dkstats.ds_inode_deltas.value.ui64 = delta_stats[DT_INODE];
12760Sstevel@tonic-gate 		dkstats.ds_fbiwrite_deltas.value.ui64 = delta_stats[DT_FBI];
12770Sstevel@tonic-gate 		dkstats.ds_quota_deltas.value.ui64 = delta_stats[DT_QR];
12780Sstevel@tonic-gate 		dkstats.ds_shadow_deltas.value.ui64 = delta_stats[DT_SHAD];
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		dkstats.ds_superblock_rolled.value.ui64 = roll_stats[DT_SB];
12810Sstevel@tonic-gate 		dkstats.ds_bitmap_rolled.value.ui64 = roll_stats[DT_CG];
12820Sstevel@tonic-gate 		dkstats.ds_suminfo_rolled.value.ui64 = roll_stats[DT_SI];
12830Sstevel@tonic-gate 		dkstats.ds_allocblk_rolled.value.ui64 = roll_stats[DT_AB];
12840Sstevel@tonic-gate 		dkstats.ds_ab0_rolled.value.ui64 = roll_stats[DT_ABZERO];
12850Sstevel@tonic-gate 		dkstats.ds_dir_rolled.value.ui64 = roll_stats[DT_DIR];
12860Sstevel@tonic-gate 		dkstats.ds_inode_rolled.value.ui64 = roll_stats[DT_INODE];
12870Sstevel@tonic-gate 		dkstats.ds_fbiwrite_rolled.value.ui64 = roll_stats[DT_FBI];
12880Sstevel@tonic-gate 		dkstats.ds_quota_rolled.value.ui64 = roll_stats[DT_QR];
12890Sstevel@tonic-gate 		dkstats.ds_shadow_rolled.value.ui64 = roll_stats[DT_SHAD];
12900Sstevel@tonic-gate 	}
12910Sstevel@tonic-gate 	return (0);
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate 
12940Sstevel@tonic-gate extern size_t ufs_crb_limit;
12950Sstevel@tonic-gate extern int ufs_max_crb_divisor;
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate void
12980Sstevel@tonic-gate lufs_init(void)
12990Sstevel@tonic-gate {
13000Sstevel@tonic-gate 	kstat_t *ksp;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 	/* Create kmem caches */
13030Sstevel@tonic-gate 	lufs_sv = kmem_cache_create("lufs_save", sizeof (lufs_save_t), 0,
13040Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
13050Sstevel@tonic-gate 	lufs_bp = kmem_cache_create("lufs_bufs", sizeof (lufs_buf_t), 0,
13060Sstevel@tonic-gate 	    NULL, NULL, NULL, NULL, NULL, 0);
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	mutex_init(&log_mutex, NULL, MUTEX_DEFAULT, NULL);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	_init_top();
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	if (&bio_lufs_strategy != NULL)
13130Sstevel@tonic-gate 		bio_lufs_strategy = (void (*) (void *, buf_t *)) lufs_strategy;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * Initialise general logging and delta kstats
13170Sstevel@tonic-gate 	 */
13180Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "logstats", "ufs", KSTAT_TYPE_NAMED,
13190Sstevel@tonic-gate 	    sizeof (logstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
13200Sstevel@tonic-gate 	if (ksp) {
13210Sstevel@tonic-gate 		ksp->ks_data = (void *) &logstats;
13220Sstevel@tonic-gate 		kstat_install(ksp);
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	ksp = kstat_create("ufs_log", 0, "deltastats", "ufs", KSTAT_TYPE_NAMED,
13260Sstevel@tonic-gate 	    sizeof (dkstats) / sizeof (kstat_named_t), KSTAT_FLAG_VIRTUAL);
13270Sstevel@tonic-gate 	if (ksp) {
13280Sstevel@tonic-gate 		ksp->ks_data = (void *) &dkstats;
13290Sstevel@tonic-gate 		ksp->ks_update = delta_stats_update;
13300Sstevel@tonic-gate 		kstat_install(ksp);
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate 	/*
13340Sstevel@tonic-gate 	 * Set up the maximum amount of kmem that the crbs (system wide)
13350Sstevel@tonic-gate 	 * can use.
13360Sstevel@tonic-gate 	 */
13370Sstevel@tonic-gate 	ufs_crb_limit = kmem_maxavail() / ufs_max_crb_divisor;
13380Sstevel@tonic-gate }
1339