xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_subr.c (revision 11066:cebb50cbe4f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54662Sfrankho  * Common Development and Distribution License (the "License").
64662Sfrankho  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*11066Srafael.vanoni@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  * The Regents of the University of California
320Sstevel@tonic-gate  * All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  * contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/t_lock.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef _KERNEL
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/systm.h>
490Sstevel@tonic-gate #include <sys/sysmacros.h>
500Sstevel@tonic-gate #include <sys/buf.h>
510Sstevel@tonic-gate #include <sys/conf.h>
520Sstevel@tonic-gate #include <sys/user.h>
530Sstevel@tonic-gate #include <sys/var.h>
540Sstevel@tonic-gate #include <sys/vfs.h>
550Sstevel@tonic-gate #include <sys/vnode.h>
560Sstevel@tonic-gate #include <sys/proc.h>
570Sstevel@tonic-gate #include <sys/debug.h>
580Sstevel@tonic-gate #include <sys/fssnap_if.h>
590Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
600Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
610Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
620Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
630Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
640Sstevel@tonic-gate #include <sys/kmem.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate #include <vm/hat.h>
670Sstevel@tonic-gate #include <vm/as.h>
680Sstevel@tonic-gate #include <vm/seg.h>
690Sstevel@tonic-gate #include <vm/pvn.h>
700Sstevel@tonic-gate #include <vm/seg_map.h>
710Sstevel@tonic-gate #include <sys/swap.h>
720Sstevel@tonic-gate #include <vm/seg_kmem.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #else  /* _KERNEL */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #define	ASSERT(x)		/* don't use asserts for fsck et al */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate #endif  /* _KERNEL */
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #ifdef _KERNEL
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * Used to verify that a given entry on the ufs_instances list (see below)
840Sstevel@tonic-gate  * still refers to a mounted file system.
850Sstevel@tonic-gate  *
860Sstevel@tonic-gate  * XXX:	This is a crock that substitutes for proper locking to coordinate
870Sstevel@tonic-gate  *	updates to and uses of the entries in ufs_instances.
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate struct check_node {
900Sstevel@tonic-gate 	struct vfs *vfsp;
910Sstevel@tonic-gate 	struct ufsvfs *ufsvfs;
920Sstevel@tonic-gate 	dev_t vfs_dev;
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
950Sstevel@tonic-gate static vfs_t *still_mounted(struct check_node *);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * All ufs file system instances are linked together into a list starting at
990Sstevel@tonic-gate  * ufs_instances.  The list is updated as part of mount and unmount.  It's
1000Sstevel@tonic-gate  * consulted in ufs_update, to allow syncing out all ufs file system instances
1010Sstevel@tonic-gate  * in a batch.
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  * ufsvfs_mutex guards access to this list and to the {,old}ufsvfslist
1040Sstevel@tonic-gate  * manipulated in ufs_funmount_cleanup.  (A given ufs instance is always on
1050Sstevel@tonic-gate  * exactly one of these lists except while it's being allocated or
1060Sstevel@tonic-gate  * deallocated.)
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate struct ufsvfs	*ufs_instances;
1090Sstevel@tonic-gate extern kmutex_t		ufsvfs_mutex;	/* XXX: move this to ufs_inode.h? */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate  * ufsvfs list manipulation routines
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate  * Link ufsp in at the head of the list of ufs_instances.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate void
ufs_vfs_add(struct ufsvfs * ufsp)1190Sstevel@tonic-gate ufs_vfs_add(struct ufsvfs *ufsp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	mutex_enter(&ufsvfs_mutex);
1220Sstevel@tonic-gate 	ufsp->vfs_next = ufs_instances;
1230Sstevel@tonic-gate 	ufs_instances = ufsp;
1240Sstevel@tonic-gate 	mutex_exit(&ufsvfs_mutex);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Remove ufsp from the list of ufs_instances.
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * Does no error checking; ufsp is assumed to actually be on the list.
1310Sstevel@tonic-gate  */
1320Sstevel@tonic-gate void
ufs_vfs_remove(struct ufsvfs * ufsp)1330Sstevel@tonic-gate ufs_vfs_remove(struct ufsvfs *ufsp)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	struct ufsvfs	**delpt = &ufs_instances;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	mutex_enter(&ufsvfs_mutex);
1380Sstevel@tonic-gate 	for (; *delpt != NULL; delpt = &((*delpt)->vfs_next)) {
1390Sstevel@tonic-gate 		if (*delpt == ufsp) {
1400Sstevel@tonic-gate 			*delpt = ufsp->vfs_next;
1410Sstevel@tonic-gate 			ufsp->vfs_next = NULL;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 	mutex_exit(&ufsvfs_mutex);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * Clean up state resulting from a forcible unmount that couldn't be handled
1500Sstevel@tonic-gate  * directly during the unmount.  (See commentary in the unmount code for more
1510Sstevel@tonic-gate  * info.)
1520Sstevel@tonic-gate  */
1530Sstevel@tonic-gate static void
ufs_funmount_cleanup()1540Sstevel@tonic-gate ufs_funmount_cleanup()
1550Sstevel@tonic-gate {
1560Sstevel@tonic-gate 	struct ufsvfs		*ufsvfsp;
1570Sstevel@tonic-gate 	extern struct ufsvfs	*oldufsvfslist, *ufsvfslist;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	/*
1600Sstevel@tonic-gate 	 * Assumption: it's now safe to blow away the entries on
1610Sstevel@tonic-gate 	 * oldufsvfslist.
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	mutex_enter(&ufsvfs_mutex);
1640Sstevel@tonic-gate 	while ((ufsvfsp = oldufsvfslist) != NULL) {
1650Sstevel@tonic-gate 		oldufsvfslist = ufsvfsp->vfs_next;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		mutex_destroy(&ufsvfsp->vfs_lock);
1680Sstevel@tonic-gate 		kmem_free(ufsvfsp, sizeof (struct ufsvfs));
1690Sstevel@tonic-gate 	}
1700Sstevel@tonic-gate 	/*
1710Sstevel@tonic-gate 	 * Rotate more recent unmount entries into place in preparation for
1720Sstevel@tonic-gate 	 * the next time around.
1730Sstevel@tonic-gate 	 */
1740Sstevel@tonic-gate 	oldufsvfslist = ufsvfslist;
1750Sstevel@tonic-gate 	ufsvfslist = NULL;
1760Sstevel@tonic-gate 	mutex_exit(&ufsvfs_mutex);
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * ufs_update performs the ufs part of `sync'.  It goes through the disk
1820Sstevel@tonic-gate  * queues to initiate sandbagged IO; goes through the inodes to write
1830Sstevel@tonic-gate  * modified nodes; and it goes through the mount table to initiate
1840Sstevel@tonic-gate  * the writing of the modified super blocks.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate extern time_t	time;
1870Sstevel@tonic-gate time_t		ufs_sync_time;
1880Sstevel@tonic-gate time_t		ufs_sync_time_secs = 1;
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate extern kmutex_t	ufs_scan_lock;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate void
ufs_update(int flag)1930Sstevel@tonic-gate ufs_update(int flag)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	struct vfs *vfsp;
1960Sstevel@tonic-gate 	struct fs *fs;
1970Sstevel@tonic-gate 	struct ufsvfs *ufsp;
1980Sstevel@tonic-gate 	struct ufsvfs *ufsnext;
1990Sstevel@tonic-gate 	struct ufsvfs *update_list = NULL;
2000Sstevel@tonic-gate 	int check_cnt = 0;
2010Sstevel@tonic-gate 	size_t check_size;
2020Sstevel@tonic-gate 	struct check_node *check_list, *ptr;
2030Sstevel@tonic-gate 	int cheap = flag & SYNC_ATTR;
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * This is a hack.  A design flaw in the forced unmount protocol
2070Sstevel@tonic-gate 	 * could allow a thread to attempt to use a kmem_freed ufsvfs
2080Sstevel@tonic-gate 	 * structure in ufs_lockfs_begin/ufs_check_lockfs.  This window
2090Sstevel@tonic-gate 	 * is difficult to hit, even during the lockfs stress tests.
2100Sstevel@tonic-gate 	 * So the hacky fix is to wait awhile before kmem_free'ing the
2110Sstevel@tonic-gate 	 * ufsvfs structures for forcibly unmounted file systems.  `Awhile'
2120Sstevel@tonic-gate 	 * is defined as every other call from fsflush (~60 seconds).
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	if (cheap)
2150Sstevel@tonic-gate 		ufs_funmount_cleanup();
2160Sstevel@tonic-gate 
2170Sstevel@tonic-gate 	/*
2180Sstevel@tonic-gate 	 * Examine all ufsvfs structures and add those that we can lock to the
2190Sstevel@tonic-gate 	 * update list.  This is so that we don't hold the list lock for a
2200Sstevel@tonic-gate 	 * long time.  If vfs_lock fails for a file system instance, then skip
2210Sstevel@tonic-gate 	 * it because somebody is doing a unmount on it.
2220Sstevel@tonic-gate 	 */
2230Sstevel@tonic-gate 	mutex_enter(&ufsvfs_mutex);
2240Sstevel@tonic-gate 	for (ufsp = ufs_instances; ufsp != NULL; ufsp = ufsp->vfs_next) {
2250Sstevel@tonic-gate 		vfsp = ufsp->vfs_vfs;
2260Sstevel@tonic-gate 		if (vfs_lock(vfsp) != 0)
2270Sstevel@tonic-gate 			continue;
2280Sstevel@tonic-gate 		ufsp->vfs_wnext = update_list;
2290Sstevel@tonic-gate 		update_list = ufsp;
2300Sstevel@tonic-gate 		check_cnt++;
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	mutex_exit(&ufsvfs_mutex);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (update_list == NULL)
2350Sstevel@tonic-gate 		return;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	check_size = sizeof (struct check_node) * check_cnt;
2380Sstevel@tonic-gate 	check_list = ptr = kmem_alloc(check_size, KM_NOSLEEP);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * Write back modified superblocks.
2420Sstevel@tonic-gate 	 * Consistency check that the superblock of
2430Sstevel@tonic-gate 	 * each file system is still in the buffer cache.
2440Sstevel@tonic-gate 	 *
2450Sstevel@tonic-gate 	 * Note that the update_list traversal is done without the protection
2460Sstevel@tonic-gate 	 * of an overall list lock, so it's necessary to rely on the fact that
2470Sstevel@tonic-gate 	 * each entry of the list is vfs_locked when moving from one entry to
2480Sstevel@tonic-gate 	 * the next.  This works because a concurrent attempt to add an entry
2490Sstevel@tonic-gate 	 * to another thread's update_list won't find it, since it'll already
2500Sstevel@tonic-gate 	 * be locked.
2510Sstevel@tonic-gate 	 */
2520Sstevel@tonic-gate 	check_cnt = 0;
2530Sstevel@tonic-gate 	for (ufsp = update_list; ufsp != NULL; ufsp = ufsnext) {
2540Sstevel@tonic-gate 		/*
2550Sstevel@tonic-gate 		 * Need to grab the next ptr before we unlock this one so
2560Sstevel@tonic-gate 		 * another thread doesn't grab it and change it before we move
2570Sstevel@tonic-gate 		 * on to the next vfs.  (Once we unlock it, it's ok if another
2580Sstevel@tonic-gate 		 * thread finds it to add it to its own update_list; we don't
2590Sstevel@tonic-gate 		 * attempt to refer to it through our list any more.)
2600Sstevel@tonic-gate 		 */
2610Sstevel@tonic-gate 		ufsnext = ufsp->vfs_wnext;
2620Sstevel@tonic-gate 		vfsp = ufsp->vfs_vfs;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 		/*
2650Sstevel@tonic-gate 		 * Seems like this can't happen, so perhaps it should become
2660Sstevel@tonic-gate 		 * an ASSERT(vfsp->vfs_data != NULL).
2670Sstevel@tonic-gate 		 */
2680Sstevel@tonic-gate 		if (!vfsp->vfs_data) {
2690Sstevel@tonic-gate 			vfs_unlock(vfsp);
2700Sstevel@tonic-gate 			continue;
2710Sstevel@tonic-gate 		}
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 		fs = ufsp->vfs_fs;
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate 		/*
2760Sstevel@tonic-gate 		 * don't update a locked superblock during a panic; it
2770Sstevel@tonic-gate 		 * may be in an inconsistent state
2780Sstevel@tonic-gate 		 */
2790Sstevel@tonic-gate 		if (panicstr) {
2800Sstevel@tonic-gate 			if (!mutex_tryenter(&ufsp->vfs_lock)) {
2810Sstevel@tonic-gate 				vfs_unlock(vfsp);
2820Sstevel@tonic-gate 				continue;
2830Sstevel@tonic-gate 			}
2840Sstevel@tonic-gate 		} else
2850Sstevel@tonic-gate 			mutex_enter(&ufsp->vfs_lock);
2860Sstevel@tonic-gate 		/*
2870Sstevel@tonic-gate 		 * Build up the STABLE check list, so we can unlock the vfs
2880Sstevel@tonic-gate 		 * until we do the actual checking.
2890Sstevel@tonic-gate 		 */
2900Sstevel@tonic-gate 		if (check_list != NULL) {
2910Sstevel@tonic-gate 			if ((fs->fs_ronly == 0) &&
2920Sstevel@tonic-gate 			    (fs->fs_clean != FSBAD) &&
2930Sstevel@tonic-gate 			    (fs->fs_clean != FSSUSPEND)) {
2940Sstevel@tonic-gate 				ptr->vfsp = vfsp;
2950Sstevel@tonic-gate 				ptr->ufsvfs = ufsp;
2960Sstevel@tonic-gate 				ptr->vfs_dev = vfsp->vfs_dev;
2970Sstevel@tonic-gate 				ptr++;
2980Sstevel@tonic-gate 				check_cnt++;
2990Sstevel@tonic-gate 			}
3000Sstevel@tonic-gate 		}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 		/*
3030Sstevel@tonic-gate 		 * superblock is not modified
3040Sstevel@tonic-gate 		 */
3050Sstevel@tonic-gate 		if (fs->fs_fmod == 0) {
3060Sstevel@tonic-gate 			mutex_exit(&ufsp->vfs_lock);
3070Sstevel@tonic-gate 			vfs_unlock(vfsp);
3080Sstevel@tonic-gate 			continue;
3090Sstevel@tonic-gate 		}
3100Sstevel@tonic-gate 		if (fs->fs_ronly != 0) {
3110Sstevel@tonic-gate 			mutex_exit(&ufsp->vfs_lock);
3120Sstevel@tonic-gate 			vfs_unlock(vfsp);
3130Sstevel@tonic-gate 			(void) ufs_fault(ufsp->vfs_root,
3144662Sfrankho 			    "fs = %s update: ro fs mod\n", fs->fs_fsmnt);
3150Sstevel@tonic-gate 			/*
3160Sstevel@tonic-gate 			 * XXX:	Why is this a return instead of a continue?
3170Sstevel@tonic-gate 			 *	This may be an attempt to replace a panic with
3180Sstevel@tonic-gate 			 *	something less drastic, but there's cleanup we
3190Sstevel@tonic-gate 			 *	should be doing that's not being done (e.g.,
3200Sstevel@tonic-gate 			 *	unlocking the remaining entries on the list).
3210Sstevel@tonic-gate 			 */
3220Sstevel@tonic-gate 			return;
3230Sstevel@tonic-gate 		}
3240Sstevel@tonic-gate 		fs->fs_fmod = 0;
3250Sstevel@tonic-gate 		mutex_exit(&ufsp->vfs_lock);
3260Sstevel@tonic-gate 		TRANS_SBUPDATE(ufsp, vfsp, TOP_SBUPDATE_UPDATE);
3270Sstevel@tonic-gate 		vfs_unlock(vfsp);
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	ufs_sync_time = time;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * Avoid racing with ufs_unmount() and ufs_sync().
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	mutex_enter(&ufs_scan_lock);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	(void) ufs_scan_inodes(1, ufs_sync_inode, (void *)(uintptr_t)cheap,
3380Sstevel@tonic-gate 	    NULL);
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	mutex_exit(&ufs_scan_lock);
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Force stale buffer cache information to be flushed,
3440Sstevel@tonic-gate 	 * for all devices.  This should cause any remaining control
3450Sstevel@tonic-gate 	 * information (e.g., cg and inode info) to be flushed back.
3460Sstevel@tonic-gate 	 */
3470Sstevel@tonic-gate 	bflush((dev_t)NODEV);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (check_list == NULL)
3500Sstevel@tonic-gate 		return;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * For each UFS filesystem in the STABLE check_list, update
3540Sstevel@tonic-gate 	 * the clean flag if warranted.
3550Sstevel@tonic-gate 	 */
3560Sstevel@tonic-gate 	for (ptr = check_list; check_cnt > 0; check_cnt--, ptr++) {
3570Sstevel@tonic-gate 		int	error;
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 		/*
3600Sstevel@tonic-gate 		 * still_mounted() returns with vfsp and the vfs_reflock
3610Sstevel@tonic-gate 		 * held if ptr refers to a vfs that is still mounted.
3620Sstevel@tonic-gate 		 */
3630Sstevel@tonic-gate 		if ((vfsp = still_mounted(ptr)) == NULL)
3640Sstevel@tonic-gate 			continue;
3650Sstevel@tonic-gate 		ufs_checkclean(vfsp);
3660Sstevel@tonic-gate 		/*
3670Sstevel@tonic-gate 		 * commit any outstanding async transactions
3680Sstevel@tonic-gate 		 */
3690Sstevel@tonic-gate 		ufsp = (struct ufsvfs *)vfsp->vfs_data;
3700Sstevel@tonic-gate 		curthread->t_flag |= T_DONTBLOCK;
3710Sstevel@tonic-gate 		TRANS_BEGIN_SYNC(ufsp, TOP_COMMIT_UPDATE, TOP_COMMIT_SIZE,
3720Sstevel@tonic-gate 		    error);
3730Sstevel@tonic-gate 		if (!error) {
3740Sstevel@tonic-gate 			TRANS_END_SYNC(ufsp, error, TOP_COMMIT_UPDATE,
3754662Sfrankho 			    TOP_COMMIT_SIZE);
3760Sstevel@tonic-gate 		}
3770Sstevel@tonic-gate 		curthread->t_flag &= ~T_DONTBLOCK;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		vfs_unlock(vfsp);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	kmem_free(check_list, check_size);
3830Sstevel@tonic-gate }
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate int
ufs_sync_inode(struct inode * ip,void * arg)3860Sstevel@tonic-gate ufs_sync_inode(struct inode *ip, void *arg)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate 	int cheap = (int)(uintptr_t)arg;
3890Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
3900Sstevel@tonic-gate 	uint_t flag = ip->i_flag;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	if (cheap && ((flag & (IUPD|IACC|ICHG|IMOD|IMODACC|IATTCHG)) == 0))
3930Sstevel@tonic-gate 		return (0);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	/*
3960Sstevel@tonic-gate 	 * if we are panic'ing; then don't update the inode if this
3970Sstevel@tonic-gate 	 * file system is FSSTABLE.  Otherwise, we would have to
3980Sstevel@tonic-gate 	 * force the superblock to FSACTIVE and the superblock
3990Sstevel@tonic-gate 	 * may not be in a good state.  Also, if the inode is
4000Sstevel@tonic-gate 	 * IREF'ed then it may be in an inconsistent state.  Don't
4010Sstevel@tonic-gate 	 * push it.  Finally, don't push the inode if the fs is
4020Sstevel@tonic-gate 	 * logging; the transaction will be discarded at boot.
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	if (panicstr) {
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 		if (flag & IREF)
4070Sstevel@tonic-gate 			return (0);
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 		if (ip->i_ufsvfs == NULL ||
4100Sstevel@tonic-gate 		    (ip->i_fs->fs_clean == FSSTABLE ||
4110Sstevel@tonic-gate 		    ip->i_fs->fs_clean == FSLOG))
4120Sstevel@tonic-gate 				return (0);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * Limit access time only updates
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	if (((flag & (IMOD|IMODACC|IUPD|ICHG|IACC)) == IMODACC) && ufsvfsp) {
4210Sstevel@tonic-gate 		/*
4220Sstevel@tonic-gate 		 * if file system has deferred access time turned on and there
4230Sstevel@tonic-gate 		 * was no IO recently, don't bother flushing it. It will be
4240Sstevel@tonic-gate 		 * flushed when I/Os start again.
4250Sstevel@tonic-gate 		 */
4260Sstevel@tonic-gate 		if (cheap && (ufsvfsp->vfs_dfritime & UFS_DFRATIME) &&
427*11066Srafael.vanoni@sun.com 		    (ufsvfsp->vfs_iotstamp + ufs_iowait < ddi_get_lbolt()))
4280Sstevel@tonic-gate 			return (0);
4290Sstevel@tonic-gate 		/*
4300Sstevel@tonic-gate 		 * an app issueing a sync() can take forever on a trans device
4310Sstevel@tonic-gate 		 * when NetWorker or find is running because all of the
4320Sstevel@tonic-gate 		 * directorys' access times have to be updated. So, we limit
4330Sstevel@tonic-gate 		 * the time we spend updating access times per sync.
4340Sstevel@tonic-gate 		 */
4350Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) && ((ufs_sync_time +
4360Sstevel@tonic-gate 		    ufs_sync_time_secs) < time))
4370Sstevel@tonic-gate 			return (0);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	/*
4410Sstevel@tonic-gate 	 * if we are running on behalf of the flush thread or this is
4420Sstevel@tonic-gate 	 * a swap file, then simply do a delay update of the inode.
4430Sstevel@tonic-gate 	 * Otherwise, push the pages and then do a delayed inode update.
4440Sstevel@tonic-gate 	 */
4450Sstevel@tonic-gate 	if (cheap || IS_SWAPVP(ITOV(ip))) {
4460Sstevel@tonic-gate 		TRANS_IUPDAT(ip, 0);
4470Sstevel@tonic-gate 	} else {
4480Sstevel@tonic-gate 		(void) TRANS_SYNCIP(ip, B_ASYNC, I_ASYNC, TOP_SYNCIP_SYNC);
4490Sstevel@tonic-gate 	}
4500Sstevel@tonic-gate 	return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate  * Flush all the pages associated with an inode using the given 'flags',
4550Sstevel@tonic-gate  * then force inode information to be written back using the given 'waitfor'.
4560Sstevel@tonic-gate  */
4570Sstevel@tonic-gate int
ufs_syncip(struct inode * ip,int flags,int waitfor,top_t topid)4580Sstevel@tonic-gate ufs_syncip(struct inode *ip, int flags, int waitfor, top_t topid)
4590Sstevel@tonic-gate {
4600Sstevel@tonic-gate 	int	error;
4610Sstevel@tonic-gate 	struct vnode *vp = ITOV(ip);
4620Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
4630Sstevel@tonic-gate 	int dotrans = 0;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	/*
4660Sstevel@tonic-gate 	 * Return if file system has been forcibly umounted.
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	if (ufsvfsp == NULL)
4690Sstevel@tonic-gate 		return (EIO);
4700Sstevel@tonic-gate 	/*
4710Sstevel@tonic-gate 	 * don't need to VOP_PUTPAGE if there are no pages
4720Sstevel@tonic-gate 	 */
4730Sstevel@tonic-gate 	if (!vn_has_cached_data(vp) || vp->v_type == VCHR) {
4740Sstevel@tonic-gate 		error = 0;
4750Sstevel@tonic-gate 	} else {
4760Sstevel@tonic-gate 		/*
4770Sstevel@tonic-gate 		 * if the inode we're working on is a shadow inode
4780Sstevel@tonic-gate 		 * or quota inode we need to make sure that the
4790Sstevel@tonic-gate 		 * ufs_putpage call is inside a transaction as this
4800Sstevel@tonic-gate 		 * could include meta data changes.
4810Sstevel@tonic-gate 		 */
4820Sstevel@tonic-gate 		if ((ip->i_mode & IFMT) == IFSHAD ||
4834662Sfrankho 		    ufsvfsp->vfs_qinod == ip) {
4840Sstevel@tonic-gate 			dotrans = 1;
4850Sstevel@tonic-gate 			curthread->t_flag |= T_DONTBLOCK;
4860Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_PUTPAGE,
4870Sstevel@tonic-gate 			    TOP_PUTPAGE_SIZE(ip));
4880Sstevel@tonic-gate 		}
4895331Samw 		error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
4905331Samw 		    flags, CRED(), NULL);
4910Sstevel@tonic-gate 		if (dotrans) {
4920Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_PUTPAGE,
4930Sstevel@tonic-gate 			    TOP_PUTPAGE_SIZE(ip));
4940Sstevel@tonic-gate 			curthread->t_flag &= ~T_DONTBLOCK;
4950Sstevel@tonic-gate 			dotrans = 0;
4960Sstevel@tonic-gate 		}
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 	if (panicstr && TRANS_ISTRANS(ufsvfsp))
4990Sstevel@tonic-gate 		goto out;
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * waitfor represents two things -
5020Sstevel@tonic-gate 	 * 1. whether data sync or file sync.
5030Sstevel@tonic-gate 	 * 2. if file sync then ufs_iupdat should 'waitfor' disk i/o or not.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if (waitfor == I_DSYNC) {
5060Sstevel@tonic-gate 		/*
5070Sstevel@tonic-gate 		 * If data sync, only IATTCHG (size/block change) requires
5080Sstevel@tonic-gate 		 * inode update, fdatasync()/FDSYNC implementation.
5090Sstevel@tonic-gate 		 */
5100Sstevel@tonic-gate 		if (ip->i_flag & (IBDWRITE|IATTCHG)) {
5110Sstevel@tonic-gate 			/*
5120Sstevel@tonic-gate 			 * Enter a transaction to provide mutual exclusion
5130Sstevel@tonic-gate 			 * with deltamap_push and avoid a race where
5140Sstevel@tonic-gate 			 * the inode flush could get dropped.
5150Sstevel@tonic-gate 			 */
5160Sstevel@tonic-gate 			if ((curthread->t_flag & T_DONTBLOCK) == 0) {
5170Sstevel@tonic-gate 				dotrans = 1;
5180Sstevel@tonic-gate 				curthread->t_flag |= T_DONTBLOCK;
5190Sstevel@tonic-gate 				TRANS_BEGIN_ASYNC(ufsvfsp, topid,
5200Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
5210Sstevel@tonic-gate 			}
5220Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
5230Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
5240Sstevel@tonic-gate 			ip->i_flag &= ~IMODTIME;
5250Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
5260Sstevel@tonic-gate 			ufs_iupdat(ip, 1);
5270Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5280Sstevel@tonic-gate 			if (dotrans) {
5290Sstevel@tonic-gate 				TRANS_END_ASYNC(ufsvfsp, topid,
5300Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
5310Sstevel@tonic-gate 				curthread->t_flag &= ~T_DONTBLOCK;
5320Sstevel@tonic-gate 			}
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 	} else {
5350Sstevel@tonic-gate 		/* For file sync, any inode change requires inode update */
5360Sstevel@tonic-gate 		if (ip->i_flag & (IBDWRITE|IUPD|IACC|ICHG|IMOD|IMODACC)) {
5370Sstevel@tonic-gate 			/*
5380Sstevel@tonic-gate 			 * Enter a transaction to provide mutual exclusion
5390Sstevel@tonic-gate 			 * with deltamap_push and avoid a race where
5400Sstevel@tonic-gate 			 * the inode flush could get dropped.
5410Sstevel@tonic-gate 			 */
5420Sstevel@tonic-gate 			if ((curthread->t_flag & T_DONTBLOCK) == 0) {
5430Sstevel@tonic-gate 				dotrans = 1;
5440Sstevel@tonic-gate 				curthread->t_flag |= T_DONTBLOCK;
5450Sstevel@tonic-gate 				TRANS_BEGIN_ASYNC(ufsvfsp, topid,
5460Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
5470Sstevel@tonic-gate 			}
5480Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
5490Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
5500Sstevel@tonic-gate 			ip->i_flag &= ~IMODTIME;
5510Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
5520Sstevel@tonic-gate 			ufs_iupdat(ip, waitfor);
5530Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5540Sstevel@tonic-gate 			if (dotrans) {
5550Sstevel@tonic-gate 				TRANS_END_ASYNC(ufsvfsp, topid,
5560Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
5570Sstevel@tonic-gate 				curthread->t_flag &= ~T_DONTBLOCK;
5580Sstevel@tonic-gate 			}
5590Sstevel@tonic-gate 		}
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate out:
5630Sstevel@tonic-gate 	return (error);
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate /*
5660Sstevel@tonic-gate  * Flush all indirect blocks related to an inode.
5670Sstevel@tonic-gate  * Supports triple indirect blocks also.
5680Sstevel@tonic-gate  */
5690Sstevel@tonic-gate int
ufs_sync_indir(struct inode * ip)5700Sstevel@tonic-gate ufs_sync_indir(struct inode *ip)
5710Sstevel@tonic-gate {
5720Sstevel@tonic-gate 	int i;
5730Sstevel@tonic-gate 	daddr_t blkno;
5740Sstevel@tonic-gate 	daddr_t lbn;	/* logical blkno of last blk in file */
5750Sstevel@tonic-gate 	daddr_t clbn;	/* current logical blk */
5760Sstevel@tonic-gate 	daddr32_t *bap;
5770Sstevel@tonic-gate 	struct fs *fs;
5780Sstevel@tonic-gate 	struct buf *bp;
5790Sstevel@tonic-gate 	int bsize;
5800Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
5810Sstevel@tonic-gate 	int j;
5820Sstevel@tonic-gate 	daddr_t indirect_blkno;
5830Sstevel@tonic-gate 	daddr32_t *indirect_bap;
5840Sstevel@tonic-gate 	struct buf *indirect_bp;
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
5870Sstevel@tonic-gate 	/*
5880Sstevel@tonic-gate 	 * unnecessary when logging; allocation blocks are kept up-to-date
5890Sstevel@tonic-gate 	 */
5900Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp))
5910Sstevel@tonic-gate 		return (0);
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
5940Sstevel@tonic-gate 	bsize = fs->fs_bsize;
5950Sstevel@tonic-gate 	lbn = (daddr_t)lblkno(fs, ip->i_size - 1);
5960Sstevel@tonic-gate 	if (lbn < NDADDR)
5970Sstevel@tonic-gate 		return (0);	/* No indirect blocks used */
5980Sstevel@tonic-gate 	if (lbn < NDADDR + NINDIR(fs)) {
5990Sstevel@tonic-gate 		/* File has one indirect block. */
6000Sstevel@tonic-gate 		blkflush(ip->i_dev, (daddr_t)fsbtodb(fs, ip->i_ib[0]));
6010Sstevel@tonic-gate 		return (0);
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	/* Write out all the first level indirect blocks */
6050Sstevel@tonic-gate 	for (i = 0; i <= NIADDR; i++) {
6060Sstevel@tonic-gate 		if ((blkno = ip->i_ib[i]) == 0)
6070Sstevel@tonic-gate 			continue;
6080Sstevel@tonic-gate 		blkflush(ip->i_dev, (daddr_t)fsbtodb(fs, blkno));
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	/* Write out second level of indirect blocks */
6110Sstevel@tonic-gate 	if ((blkno = ip->i_ib[1]) == 0)
6120Sstevel@tonic-gate 		return (0);
6130Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, blkno), bsize);
6140Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6150Sstevel@tonic-gate 		brelse(bp);
6160Sstevel@tonic-gate 		return (EIO);
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	bap = bp->b_un.b_daddr;
6190Sstevel@tonic-gate 	clbn = NDADDR + NINDIR(fs);
6200Sstevel@tonic-gate 	for (i = 0; i < NINDIR(fs); i++) {
6210Sstevel@tonic-gate 		if (clbn > lbn)
6220Sstevel@tonic-gate 			break;
6230Sstevel@tonic-gate 		clbn += NINDIR(fs);
6240Sstevel@tonic-gate 		if ((blkno = bap[i]) == 0)
6250Sstevel@tonic-gate 			continue;
6260Sstevel@tonic-gate 		blkflush(ip->i_dev, (daddr_t)fsbtodb(fs, blkno));
6270Sstevel@tonic-gate 	}
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate 	brelse(bp);
6300Sstevel@tonic-gate 	/* write out third level indirect blocks */
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 	if ((blkno = ip->i_ib[2]) == 0)
6330Sstevel@tonic-gate 		return (0);
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	bp = UFS_BREAD(ufsvfsp, ip->i_dev, (daddr_t)fsbtodb(fs, blkno), bsize);
6360Sstevel@tonic-gate 	if (bp->b_flags & B_ERROR) {
6370Sstevel@tonic-gate 		brelse(bp);
6380Sstevel@tonic-gate 		return (EIO);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 	bap = bp->b_un.b_daddr;
6410Sstevel@tonic-gate 	clbn = NDADDR + NINDIR(fs) + (NINDIR(fs) * NINDIR(fs));
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	for (i = 0; i < NINDIR(fs); i++) {
6440Sstevel@tonic-gate 		if (clbn > lbn)
6450Sstevel@tonic-gate 			break;
6460Sstevel@tonic-gate 		if ((indirect_blkno = bap[i]) == 0)
6470Sstevel@tonic-gate 			continue;
6480Sstevel@tonic-gate 		blkflush(ip->i_dev, (daddr_t)fsbtodb(fs, indirect_blkno));
6490Sstevel@tonic-gate 		indirect_bp = UFS_BREAD(ufsvfsp, ip->i_dev,
6504662Sfrankho 		    (daddr_t)fsbtodb(fs, indirect_blkno), bsize);
6510Sstevel@tonic-gate 		if (indirect_bp->b_flags & B_ERROR) {
6520Sstevel@tonic-gate 			brelse(indirect_bp);
6530Sstevel@tonic-gate 			brelse(bp);
6540Sstevel@tonic-gate 			return (EIO);
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 		indirect_bap = indirect_bp->b_un.b_daddr;
6570Sstevel@tonic-gate 		for (j = 0; j < NINDIR(fs); j++) {
6580Sstevel@tonic-gate 			if (clbn > lbn)
6590Sstevel@tonic-gate 				break;
6600Sstevel@tonic-gate 			clbn += NINDIR(fs);
6610Sstevel@tonic-gate 			if ((blkno = indirect_bap[j]) == 0)
6620Sstevel@tonic-gate 				continue;
6630Sstevel@tonic-gate 			blkflush(ip->i_dev, (daddr_t)fsbtodb(fs, blkno));
6640Sstevel@tonic-gate 		}
6650Sstevel@tonic-gate 		brelse(indirect_bp);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 	brelse(bp);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	return (0);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate  * Flush all indirect blocks related to an offset of a file.
6740Sstevel@tonic-gate  * read/write in sync mode may have to flush indirect blocks.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate int
ufs_indirblk_sync(struct inode * ip,offset_t off)6770Sstevel@tonic-gate ufs_indirblk_sync(struct inode *ip, offset_t off)
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	daddr_t	lbn;
6800Sstevel@tonic-gate 	struct	fs *fs;
6810Sstevel@tonic-gate 	struct	buf *bp;
6820Sstevel@tonic-gate 	int	i, j, shft;
6830Sstevel@tonic-gate 	daddr_t	ob, nb, tbn;
6840Sstevel@tonic-gate 	daddr32_t *bap;
6850Sstevel@tonic-gate 	int	nindirshift, nindiroffset;
6860Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
6890Sstevel@tonic-gate 	/*
6900Sstevel@tonic-gate 	 * unnecessary when logging; allocation blocks are kept up-to-date
6910Sstevel@tonic-gate 	 */
6920Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp))
6930Sstevel@tonic-gate 		return (0);
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 	lbn = (daddr_t)lblkno(fs, off);
6980Sstevel@tonic-gate 	if (lbn < 0)
6990Sstevel@tonic-gate 		return (EFBIG);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	/* The first NDADDR are direct so nothing to do */
7020Sstevel@tonic-gate 	if (lbn < NDADDR)
7030Sstevel@tonic-gate 		return (0);
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	nindirshift = ip->i_ufsvfs->vfs_nindirshift;
7060Sstevel@tonic-gate 	nindiroffset = ip->i_ufsvfs->vfs_nindiroffset;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/* Determine level of indirect blocks */
7090Sstevel@tonic-gate 	shft = 0;
7100Sstevel@tonic-gate 	tbn = lbn - NDADDR;
7110Sstevel@tonic-gate 	for (j = NIADDR; j > 0; j--) {
7120Sstevel@tonic-gate 		longlong_t	sh;
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 		shft += nindirshift;
7150Sstevel@tonic-gate 		sh = 1LL << shft;
7160Sstevel@tonic-gate 		if (tbn < sh)
7170Sstevel@tonic-gate 			break;
7180Sstevel@tonic-gate 		tbn -= (daddr_t)sh;
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	if (j == 0)
7220Sstevel@tonic-gate 		return (EFBIG);
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	if ((nb = ip->i_ib[NIADDR - j]) == 0)
7250Sstevel@tonic-gate 			return (0);		/* UFS Hole */
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	/* Flush first level indirect block */
7280Sstevel@tonic-gate 	blkflush(ip->i_dev, fsbtodb(fs, nb));
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	/* Fetch through next levels */
7310Sstevel@tonic-gate 	for (; j < NIADDR; j++) {
7320Sstevel@tonic-gate 		ob = nb;
7330Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp,
7344662Sfrankho 		    ip->i_dev, fsbtodb(fs, ob), fs->fs_bsize);
7350Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
7360Sstevel@tonic-gate 			brelse(bp);
7370Sstevel@tonic-gate 			return (EIO);
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		bap = bp->b_un.b_daddr;
7400Sstevel@tonic-gate 		shft -= nindirshift;		/* sh / nindir */
7410Sstevel@tonic-gate 		i = (tbn >> shft) & nindiroffset; /* (tbn /sh) & nindir */
7420Sstevel@tonic-gate 		nb = bap[i];
7430Sstevel@tonic-gate 		brelse(bp);
7440Sstevel@tonic-gate 		if (nb == 0) {
7450Sstevel@tonic-gate 			return (0); 		/* UFS hole */
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 		blkflush(ip->i_dev, fsbtodb(fs, nb));
7480Sstevel@tonic-gate 	}
7490Sstevel@tonic-gate 	return (0);
7500Sstevel@tonic-gate }
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate #ifdef DEBUG
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate /*
7550Sstevel@tonic-gate  * The bad block checking routines: ufs_indir_badblock() and ufs_badblock()
7560Sstevel@tonic-gate  * are very expensive. It's been found from profiling that we're
7570Sstevel@tonic-gate  * spending 6-7% of our time in ufs_badblock, and another 1-2% in
7580Sstevel@tonic-gate  * ufs_indir_badblock. They are only called via ASSERTs (from debug kernels).
7590Sstevel@tonic-gate  * In addition from experience no failures have been found in recent
7600Sstevel@tonic-gate  * years. So the following tunable can be set to enable checking.
7610Sstevel@tonic-gate  */
7620Sstevel@tonic-gate int ufs_badblock_checks = 0;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate /*
7650Sstevel@tonic-gate  * Check that a given indirect block contains blocks in range
7660Sstevel@tonic-gate  */
7670Sstevel@tonic-gate int
ufs_indir_badblock(struct inode * ip,daddr32_t * bap)7680Sstevel@tonic-gate ufs_indir_badblock(struct inode *ip, daddr32_t *bap)
7690Sstevel@tonic-gate {
7700Sstevel@tonic-gate 	int i;
7710Sstevel@tonic-gate 	int err = 0;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	if (ufs_badblock_checks) {
7740Sstevel@tonic-gate 		for (i = 0; i < NINDIR(ip->i_fs) - 1; i++)
7750Sstevel@tonic-gate 			if (bap[i] != 0 && (err = ufs_badblock(ip, bap[i])))
7760Sstevel@tonic-gate 				break;
7770Sstevel@tonic-gate 	}
7780Sstevel@tonic-gate 	return (err);
7790Sstevel@tonic-gate }
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate /*
7820Sstevel@tonic-gate  * Check that a specified block number is in range.
7830Sstevel@tonic-gate  */
7840Sstevel@tonic-gate int
ufs_badblock(struct inode * ip,daddr_t bn)7850Sstevel@tonic-gate ufs_badblock(struct inode *ip, daddr_t bn)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate 	long	c;
7880Sstevel@tonic-gate 	daddr_t	sum;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	if (!ufs_badblock_checks)
7910Sstevel@tonic-gate 		return (0);
7920Sstevel@tonic-gate 	ASSERT(bn);
7930Sstevel@tonic-gate 	if (bn <= 0 || bn > ip->i_fs->fs_size)
7940Sstevel@tonic-gate 		return (bn);
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	sum = 0;
7970Sstevel@tonic-gate 	c = dtog(ip->i_fs, bn);
7980Sstevel@tonic-gate 	if (c == 0) {
7990Sstevel@tonic-gate 		sum = howmany(ip->i_fs->fs_cssize, ip->i_fs->fs_fsize);
8000Sstevel@tonic-gate 	}
8010Sstevel@tonic-gate 	/*
8020Sstevel@tonic-gate 	 * if block no. is below this cylinder group,
8030Sstevel@tonic-gate 	 * within the space reserved for superblock, inodes, (summary data)
8040Sstevel@tonic-gate 	 * or if it is above this cylinder group
8050Sstevel@tonic-gate 	 * then its invalid
8060Sstevel@tonic-gate 	 * It's hard to see how we'd be outside this cyl, but let's be careful.
8070Sstevel@tonic-gate 	 */
8080Sstevel@tonic-gate 	if ((bn < cgbase(ip->i_fs, c)) ||
8090Sstevel@tonic-gate 	    (bn >= cgsblock(ip->i_fs, c) && bn < cgdmin(ip->i_fs, c)+sum) ||
8100Sstevel@tonic-gate 	    (bn >= (unsigned)cgbase(ip->i_fs, c+1)))
8110Sstevel@tonic-gate 		return (bn);
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 	return (0);	/* not a bad block */
8140Sstevel@tonic-gate }
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate #endif /* DEBUG */
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * When i_rwlock is write-locked or has a writer pended, then the inode
8200Sstevel@tonic-gate  * is going to change in a way that the filesystem will be marked as
8210Sstevel@tonic-gate  * active. So no need to let the filesystem be mark as stable now.
8220Sstevel@tonic-gate  * Also to ensure the filesystem consistency during the directory
8230Sstevel@tonic-gate  * operations, filesystem cannot be marked as stable if i_rwlock of
8240Sstevel@tonic-gate  * the directory inode is write-locked.
8250Sstevel@tonic-gate  */
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * Check for busy inodes for this filesystem.
8290Sstevel@tonic-gate  * NOTE: Needs better way to do this expensive operation in the future.
8300Sstevel@tonic-gate  */
8310Sstevel@tonic-gate static void
ufs_icheck(struct ufsvfs * ufsvfsp,int * isbusyp,int * isreclaimp)8320Sstevel@tonic-gate ufs_icheck(struct ufsvfs *ufsvfsp, int *isbusyp, int *isreclaimp)
8330Sstevel@tonic-gate {
8340Sstevel@tonic-gate 	union  ihead	*ih;
8350Sstevel@tonic-gate 	struct inode	*ip;
8360Sstevel@tonic-gate 	int		i;
8370Sstevel@tonic-gate 	int		isnottrans	= !TRANS_ISTRANS(ufsvfsp);
8380Sstevel@tonic-gate 	int		isbusy		= *isbusyp;
8390Sstevel@tonic-gate 	int		isreclaim	= *isreclaimp;
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	for (i = 0, ih = ihead; i < inohsz; i++, ih++) {
8420Sstevel@tonic-gate 		mutex_enter(&ih_lock[i]);
8430Sstevel@tonic-gate 		for (ip = ih->ih_chain[0];
8440Sstevel@tonic-gate 		    ip != (struct inode *)ih;
8450Sstevel@tonic-gate 		    ip = ip->i_forw) {
8460Sstevel@tonic-gate 			/*
8470Sstevel@tonic-gate 			 * if inode is busy/modified/deleted, filesystem is busy
8480Sstevel@tonic-gate 			 */
8490Sstevel@tonic-gate 			if (ip->i_ufsvfs != ufsvfsp)
8500Sstevel@tonic-gate 				continue;
8510Sstevel@tonic-gate 			if ((ip->i_flag & (IMOD | IUPD | ICHG)) ||
8520Sstevel@tonic-gate 			    (RW_ISWRITER(&ip->i_rwlock)))
8530Sstevel@tonic-gate 				isbusy = 1;
8540Sstevel@tonic-gate 			if ((ip->i_nlink <= 0) && (ip->i_flag & IREF))
8550Sstevel@tonic-gate 				isreclaim = 1;
8560Sstevel@tonic-gate 			if (isbusy && (isreclaim || isnottrans))
8570Sstevel@tonic-gate 				break;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 		mutex_exit(&ih_lock[i]);
8600Sstevel@tonic-gate 		if (isbusy && (isreclaim || isnottrans))
8610Sstevel@tonic-gate 			break;
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 	*isbusyp = isbusy;
8640Sstevel@tonic-gate 	*isreclaimp = isreclaim;
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate /*
8680Sstevel@tonic-gate  * As part of the ufs 'sync' operation, this routine is called to mark
8690Sstevel@tonic-gate  * the filesystem as STABLE if there is no modified metadata in memory.
8700Sstevel@tonic-gate  */
8710Sstevel@tonic-gate void
ufs_checkclean(struct vfs * vfsp)8720Sstevel@tonic-gate ufs_checkclean(struct vfs *vfsp)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= (struct ufsvfs *)vfsp->vfs_data;
8750Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
8760Sstevel@tonic-gate 	int		isbusy;
8770Sstevel@tonic-gate 	int		isreclaim;
8780Sstevel@tonic-gate 	int		updatesb;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	ASSERT(vfs_lock_held(vfsp));
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	/*
8830Sstevel@tonic-gate 	 * filesystem is stable or cleanflag processing is disabled; do nothing
8840Sstevel@tonic-gate 	 *	no transitions when panic'ing
8850Sstevel@tonic-gate 	 */
8860Sstevel@tonic-gate 	if (fs->fs_ronly ||
8870Sstevel@tonic-gate 	    fs->fs_clean == FSBAD ||
8880Sstevel@tonic-gate 	    fs->fs_clean == FSSUSPEND ||
8890Sstevel@tonic-gate 	    fs->fs_clean == FSSTABLE ||
8900Sstevel@tonic-gate 	    panicstr)
8910Sstevel@tonic-gate 		return;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 	/*
8940Sstevel@tonic-gate 	 * if logging and nothing to reclaim; do nothing
8950Sstevel@tonic-gate 	 */
8960Sstevel@tonic-gate 	if ((fs->fs_clean == FSLOG) &&
8970Sstevel@tonic-gate 	    (((fs->fs_reclaim & FS_RECLAIM) == 0) ||
8980Sstevel@tonic-gate 	    (fs->fs_reclaim & FS_RECLAIMING)))
8990Sstevel@tonic-gate 		return;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * FS_CHECKCLEAN is reset if the file system goes dirty
9030Sstevel@tonic-gate 	 * FS_CHECKRECLAIM is reset if a file gets deleted
9040Sstevel@tonic-gate 	 */
9050Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
9060Sstevel@tonic-gate 	fs->fs_reclaim |= (FS_CHECKCLEAN | FS_CHECKRECLAIM);
9070Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	updatesb = 0;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 	/*
9120Sstevel@tonic-gate 	 * if logging or buffers are busy; do nothing
9130Sstevel@tonic-gate 	 */
9140Sstevel@tonic-gate 	isbusy = isreclaim = 0;
9150Sstevel@tonic-gate 	if ((fs->fs_clean == FSLOG) ||
9160Sstevel@tonic-gate 	    (bcheck(vfsp->vfs_dev, ufsvfsp->vfs_bufp)))
9170Sstevel@tonic-gate 		isbusy = 1;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	/*
9200Sstevel@tonic-gate 	 * isreclaim == TRUE means can't change the state of fs_reclaim
9210Sstevel@tonic-gate 	 */
9220Sstevel@tonic-gate 	isreclaim =
9234662Sfrankho 	    ((fs->fs_clean == FSLOG) &&
9244662Sfrankho 	    (((fs->fs_reclaim & FS_RECLAIM) == 0) ||
9254662Sfrankho 	    (fs->fs_reclaim & FS_RECLAIMING)));
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	/*
9280Sstevel@tonic-gate 	 * if fs is busy or can't change the state of fs_reclaim; do nothing
9290Sstevel@tonic-gate 	 */
9300Sstevel@tonic-gate 	if (isbusy && isreclaim)
9310Sstevel@tonic-gate 		return;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	/*
9340Sstevel@tonic-gate 	 * look for busy or deleted inodes; (deleted == needs reclaim)
9350Sstevel@tonic-gate 	 */
9360Sstevel@tonic-gate 	ufs_icheck(ufsvfsp, &isbusy, &isreclaim);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * IF POSSIBLE, RESET RECLAIM
9420Sstevel@tonic-gate 	 */
9430Sstevel@tonic-gate 	/*
9440Sstevel@tonic-gate 	 * the reclaim thread is not running
9450Sstevel@tonic-gate 	 */
9460Sstevel@tonic-gate 	if ((fs->fs_reclaim & FS_RECLAIMING) == 0)
9470Sstevel@tonic-gate 		/*
9480Sstevel@tonic-gate 		 * no files were deleted during the scan
9490Sstevel@tonic-gate 		 */
9500Sstevel@tonic-gate 		if (fs->fs_reclaim & FS_CHECKRECLAIM)
9510Sstevel@tonic-gate 			/*
9520Sstevel@tonic-gate 			 * no deleted files were found in the inode cache
9530Sstevel@tonic-gate 			 */
9540Sstevel@tonic-gate 			if ((isreclaim == 0) && (fs->fs_reclaim & FS_RECLAIM)) {
9550Sstevel@tonic-gate 				fs->fs_reclaim &= ~FS_RECLAIM;
9560Sstevel@tonic-gate 				updatesb = 1;
9570Sstevel@tonic-gate 			}
9580Sstevel@tonic-gate 	/*
9590Sstevel@tonic-gate 	 * IF POSSIBLE, SET STABLE
9600Sstevel@tonic-gate 	 */
9610Sstevel@tonic-gate 	/*
9620Sstevel@tonic-gate 	 * not logging
9630Sstevel@tonic-gate 	 */
9640Sstevel@tonic-gate 	if (fs->fs_clean != FSLOG)
9650Sstevel@tonic-gate 		/*
9660Sstevel@tonic-gate 		 * file system has not gone dirty since the scan began
9670Sstevel@tonic-gate 		 */
9680Sstevel@tonic-gate 		if (fs->fs_reclaim & FS_CHECKCLEAN)
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * nothing dirty was found in the buffer or inode cache
9710Sstevel@tonic-gate 			 */
9720Sstevel@tonic-gate 			if ((isbusy == 0) && (isreclaim == 0) &&
9730Sstevel@tonic-gate 			    (fs->fs_clean != FSSTABLE)) {
9740Sstevel@tonic-gate 				fs->fs_clean = FSSTABLE;
9750Sstevel@tonic-gate 				updatesb = 1;
9760Sstevel@tonic-gate 			}
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
9790Sstevel@tonic-gate 	if (updatesb) {
9800Sstevel@tonic-gate 		TRANS_SBWRITE(ufsvfsp, TOP_SBWRITE_STABLE);
9810Sstevel@tonic-gate 	}
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate /*
9850Sstevel@tonic-gate  * called whenever an unlink occurs
9860Sstevel@tonic-gate  */
9870Sstevel@tonic-gate void
ufs_setreclaim(struct inode * ip)9880Sstevel@tonic-gate ufs_setreclaim(struct inode *ip)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
9910Sstevel@tonic-gate 	struct fs	*fs		= ufsvfsp->vfs_fs;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (ip->i_nlink || fs->fs_ronly || (fs->fs_clean != FSLOG))
9940Sstevel@tonic-gate 		return;
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9970Sstevel@tonic-gate 	 * reclaim-needed bit is already set or we need to tell
9980Sstevel@tonic-gate 	 * ufs_checkclean that a file has been deleted
9990Sstevel@tonic-gate 	 */
10000Sstevel@tonic-gate 	if ((fs->fs_reclaim & (FS_RECLAIM | FS_CHECKRECLAIM)) == FS_RECLAIM)
10010Sstevel@tonic-gate 		return;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
10040Sstevel@tonic-gate 	/*
10050Sstevel@tonic-gate 	 * inform ufs_checkclean that the file system has gone dirty
10060Sstevel@tonic-gate 	 */
10070Sstevel@tonic-gate 	fs->fs_reclaim &= ~FS_CHECKRECLAIM;
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 	/*
10100Sstevel@tonic-gate 	 * set the reclaim-needed bit
10110Sstevel@tonic-gate 	 */
10120Sstevel@tonic-gate 	if ((fs->fs_reclaim & FS_RECLAIM) == 0) {
10130Sstevel@tonic-gate 		fs->fs_reclaim |= FS_RECLAIM;
10140Sstevel@tonic-gate 		ufs_sbwrite(ufsvfsp);
10150Sstevel@tonic-gate 	}
10160Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * Before any modified metadata written back to the disk, this routine
10210Sstevel@tonic-gate  * is called to mark the filesystem as ACTIVE.
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate void
ufs_notclean(struct ufsvfs * ufsvfsp)10240Sstevel@tonic-gate ufs_notclean(struct ufsvfs *ufsvfsp)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	struct fs *fs = ufsvfsp->vfs_fs;
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock));
10290Sstevel@tonic-gate 	ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 	/*
10320Sstevel@tonic-gate 	 * inform ufs_checkclean that the file system has gone dirty
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	fs->fs_reclaim &= ~FS_CHECKCLEAN;
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 	/*
10370Sstevel@tonic-gate 	 * ignore if active or bad or suspended or readonly or logging
10380Sstevel@tonic-gate 	 */
10390Sstevel@tonic-gate 	if ((fs->fs_clean == FSACTIVE) || (fs->fs_clean == FSLOG) ||
10400Sstevel@tonic-gate 	    (fs->fs_clean == FSBAD) || (fs->fs_clean == FSSUSPEND) ||
10410Sstevel@tonic-gate 	    (fs->fs_ronly)) {
10420Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
10430Sstevel@tonic-gate 		return;
10440Sstevel@tonic-gate 	}
10450Sstevel@tonic-gate 	fs->fs_clean = FSACTIVE;
10460Sstevel@tonic-gate 	/*
10470Sstevel@tonic-gate 	 * write superblock synchronously
10480Sstevel@tonic-gate 	 */
10490Sstevel@tonic-gate 	ufs_sbwrite(ufsvfsp);
10500Sstevel@tonic-gate 	mutex_exit(&ufsvfsp->vfs_lock);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate /*
10540Sstevel@tonic-gate  * ufs specific fbwrite()
10550Sstevel@tonic-gate  */
10560Sstevel@tonic-gate int
ufs_fbwrite(struct fbuf * fbp,struct inode * ip)10570Sstevel@tonic-gate ufs_fbwrite(struct fbuf *fbp, struct inode *ip)
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp))
10620Sstevel@tonic-gate 		return (fbwrite(fbp));
10630Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
10640Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
10650Sstevel@tonic-gate 	return ((ufsvfsp->vfs_dio) ? fbdwrite(fbp) : fbwrite(fbp));
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate  * ufs specific fbiwrite()
10700Sstevel@tonic-gate  */
10710Sstevel@tonic-gate int
ufs_fbiwrite(struct fbuf * fbp,struct inode * ip,daddr_t bn,long bsize)10720Sstevel@tonic-gate ufs_fbiwrite(struct fbuf *fbp, struct inode *ip, daddr_t bn, long bsize)
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= ip->i_ufsvfs;
10750Sstevel@tonic-gate 	o_mode_t	ifmt		= ip->i_mode & IFMT;
10760Sstevel@tonic-gate 	buf_t		*bp;
10770Sstevel@tonic-gate 	int		error;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
10800Sstevel@tonic-gate 	ufs_notclean(ufsvfsp);
10810Sstevel@tonic-gate 	if (ifmt == IFDIR || ifmt == IFSHAD || ifmt == IFATTRDIR ||
10820Sstevel@tonic-gate 	    (ip->i_ufsvfs->vfs_qinod == ip)) {
10830Sstevel@tonic-gate 		TRANS_DELTA(ufsvfsp, ldbtob(bn * (offset_t)(btod(bsize))),
10844662Sfrankho 		    fbp->fb_count, DT_FBI, 0, 0);
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * Inlined version of fbiwrite()
10880Sstevel@tonic-gate 	 */
10890Sstevel@tonic-gate 	bp = pageio_setup((struct page *)NULL, fbp->fb_count,
10904662Sfrankho 	    ip->i_devvp, B_WRITE);
10910Sstevel@tonic-gate 	bp->b_flags &= ~B_PAGEIO;
10920Sstevel@tonic-gate 	bp->b_un.b_addr = fbp->fb_addr;
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	bp->b_blkno = bn * btod(bsize);
10950Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);	/* store in old dev format */
10960Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
10970Sstevel@tonic-gate 	bp->b_proc = NULL;			/* i.e. the kernel */
10980Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
10990Sstevel@tonic-gate 	bp->b_offset = -1;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
11020Sstevel@tonic-gate 		lufs_write_strategy(ufsvfsp->vfs_log, bp);
11030Sstevel@tonic-gate 	} else if (ufsvfsp->vfs_snapshot) {
11040Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
11050Sstevel@tonic-gate 	} else {
1106*11066Srafael.vanoni@sun.com 		ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
11070Sstevel@tonic-gate 		ub.ub_fbiwrites.value.ul++;
11080Sstevel@tonic-gate 		(void) bdev_strategy(bp);
11090Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_OUBLK, 1);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 	error = biowait(bp);
11120Sstevel@tonic-gate 	pageio_done(bp);
11130Sstevel@tonic-gate 	fbrelse(fbp, S_OTHER);
11140Sstevel@tonic-gate 	return (error);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Write the ufs superblock only.
11190Sstevel@tonic-gate  */
11200Sstevel@tonic-gate void
ufs_sbwrite(struct ufsvfs * ufsvfsp)11210Sstevel@tonic-gate ufs_sbwrite(struct ufsvfs *ufsvfsp)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	char sav_fs_fmod;
11240Sstevel@tonic-gate 	struct fs *fs = ufsvfsp->vfs_fs;
11250Sstevel@tonic-gate 	struct buf *bp = ufsvfsp->vfs_bufp;
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&ufsvfsp->vfs_lock));
11280Sstevel@tonic-gate 
11290Sstevel@tonic-gate 	/*
11300Sstevel@tonic-gate 	 * for ulockfs processing, limit the superblock writes
11310Sstevel@tonic-gate 	 */
11320Sstevel@tonic-gate 	if ((ufsvfsp->vfs_ulockfs.ul_sbowner) &&
11330Sstevel@tonic-gate 	    (curthread != ufsvfsp->vfs_ulockfs.ul_sbowner)) {
11340Sstevel@tonic-gate 		/* try again later */
11350Sstevel@tonic-gate 		fs->fs_fmod = 1;
11360Sstevel@tonic-gate 		return;
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	ULOCKFS_SET_MOD((&ufsvfsp->vfs_ulockfs));
11400Sstevel@tonic-gate 	/*
11410Sstevel@tonic-gate 	 * update superblock timestamp and fs_clean checksum
11420Sstevel@tonic-gate 	 * if marked FSBAD, we always want an erroneous
11430Sstevel@tonic-gate 	 * checksum to force repair
11440Sstevel@tonic-gate 	 */
11450Sstevel@tonic-gate 	fs->fs_time = gethrestime_sec();
11464662Sfrankho 	fs->fs_state = (fs->fs_clean != FSBAD) ?
11474662Sfrankho 	    FSOKAY - fs->fs_time : -(FSOKAY - fs->fs_time);
11480Sstevel@tonic-gate 	switch (fs->fs_clean) {
11490Sstevel@tonic-gate 	case FSCLEAN:
11500Sstevel@tonic-gate 	case FSSTABLE:
11510Sstevel@tonic-gate 		fs->fs_reclaim &= ~FS_RECLAIM;
11520Sstevel@tonic-gate 		break;
11530Sstevel@tonic-gate 	case FSACTIVE:
11540Sstevel@tonic-gate 	case FSSUSPEND:
11550Sstevel@tonic-gate 	case FSBAD:
11560Sstevel@tonic-gate 	case FSLOG:
11570Sstevel@tonic-gate 		break;
11580Sstevel@tonic-gate 	default:
11590Sstevel@tonic-gate 		fs->fs_clean = FSACTIVE;
11600Sstevel@tonic-gate 		break;
11610Sstevel@tonic-gate 	}
11620Sstevel@tonic-gate 	/*
11630Sstevel@tonic-gate 	 * reset incore only bits
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	fs->fs_reclaim &= ~(FS_CHECKCLEAN | FS_CHECKRECLAIM);
11660Sstevel@tonic-gate 
11670Sstevel@tonic-gate 	/*
11680Sstevel@tonic-gate 	 * delta the whole superblock
11690Sstevel@tonic-gate 	 */
11700Sstevel@tonic-gate 	TRANS_DELTA(ufsvfsp, ldbtob(SBLOCK), sizeof (struct fs),
11714662Sfrankho 	    DT_SB, NULL, 0);
11720Sstevel@tonic-gate 	/*
11730Sstevel@tonic-gate 	 * retain the incore state of fs_fmod; set the ondisk state to 0
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	sav_fs_fmod = fs->fs_fmod;
11760Sstevel@tonic-gate 	fs->fs_fmod = 0;
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * Don't release the buffer after written to the disk
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	UFS_BWRITE2(ufsvfsp, bp);
11820Sstevel@tonic-gate 	fs->fs_fmod = sav_fs_fmod;	/* reset fs_fmod's incore state */
11830Sstevel@tonic-gate }
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate /*
11860Sstevel@tonic-gate  * Returns vfs pointer if vfs still being mounted. vfs lock is held.
11870Sstevel@tonic-gate  * Otherwise, returns NULL.
11880Sstevel@tonic-gate  *
11890Sstevel@tonic-gate  * For our purposes, "still mounted" means that the file system still appears
11900Sstevel@tonic-gate  * on the list of UFS file system instances.
11910Sstevel@tonic-gate  */
11920Sstevel@tonic-gate static vfs_t *
still_mounted(struct check_node * checkp)11930Sstevel@tonic-gate still_mounted(struct check_node *checkp)
11940Sstevel@tonic-gate {
11950Sstevel@tonic-gate 	struct vfs	*vfsp;
11960Sstevel@tonic-gate 	struct ufsvfs	*ufsp;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	mutex_enter(&ufsvfs_mutex);
11990Sstevel@tonic-gate 	for (ufsp = ufs_instances; ufsp != NULL; ufsp = ufsp->vfs_next) {
12000Sstevel@tonic-gate 		if (ufsp != checkp->ufsvfs)
12010Sstevel@tonic-gate 			continue;
12020Sstevel@tonic-gate 		/*
12030Sstevel@tonic-gate 		 * Tentative match:  verify it and try to lock.  (It's not at
12040Sstevel@tonic-gate 		 * all clear how the verification could fail, given that we've
12050Sstevel@tonic-gate 		 * gotten this far.  We would have had to reallocate the
12060Sstevel@tonic-gate 		 * ufsvfs struct at hand for a new incarnation; is that really
12070Sstevel@tonic-gate 		 * possible in the interval from constructing the check_node
12080Sstevel@tonic-gate 		 * to here?)
12090Sstevel@tonic-gate 		 */
12100Sstevel@tonic-gate 		vfsp = ufsp->vfs_vfs;
12110Sstevel@tonic-gate 		if (vfsp != checkp->vfsp)
12120Sstevel@tonic-gate 			continue;
12130Sstevel@tonic-gate 		if (vfsp->vfs_dev != checkp->vfs_dev)
12140Sstevel@tonic-gate 			continue;
12150Sstevel@tonic-gate 		if (vfs_lock(vfsp) != 0)
12160Sstevel@tonic-gate 			continue;
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 		mutex_exit(&ufsvfs_mutex);
12190Sstevel@tonic-gate 		return (vfsp);
12200Sstevel@tonic-gate 	}
12210Sstevel@tonic-gate 	mutex_exit(&ufsvfs_mutex);
12220Sstevel@tonic-gate 	return (NULL);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate int
ufs_si_io_done(struct buf * bp)12260Sstevel@tonic-gate ufs_si_io_done(struct buf *bp)
12270Sstevel@tonic-gate {
12280Sstevel@tonic-gate 	sema_v(&bp->b_io);
12290Sstevel@tonic-gate 	return (0);
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate #define	SI_BUFSZ roundup(sizeof (struct cg), DEV_BSIZE)
12330Sstevel@tonic-gate #define	NSIBUF 32
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * ufs_construct_si()
12370Sstevel@tonic-gate  * Read each cylinder group in turn and construct the summary information
12380Sstevel@tonic-gate  */
12390Sstevel@tonic-gate static int
ufs_construct_si(dev_t dev,struct fs * fs,struct ufsvfs * ufsvfsp)12400Sstevel@tonic-gate ufs_construct_si(dev_t dev, struct fs *fs, struct ufsvfs *ufsvfsp)
12410Sstevel@tonic-gate {
12420Sstevel@tonic-gate 	buf_t *bps, *bp;
12430Sstevel@tonic-gate 	char *bufs;
12440Sstevel@tonic-gate 	struct csum *sip = fs->fs_u.fs_csp;
12450Sstevel@tonic-gate 	struct cg *cgp;
12460Sstevel@tonic-gate 	int i, ncg;
12470Sstevel@tonic-gate 	int error = 0, cg = 0;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	bps = kmem_alloc(NSIBUF * sizeof (buf_t), KM_SLEEP);
12500Sstevel@tonic-gate 	bufs = kmem_alloc(NSIBUF * SI_BUFSZ, KM_SLEEP);
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/*
12530Sstevel@tonic-gate 	 * Initialise the buffer headers
12540Sstevel@tonic-gate 	 */
12550Sstevel@tonic-gate 	for (bp = bps, i = 0; i < NSIBUF; i++, bp++) {
12560Sstevel@tonic-gate 		bioinit(bp);
12570Sstevel@tonic-gate 		bp->b_iodone = ufs_si_io_done;
12580Sstevel@tonic-gate 		bp->b_bufsize = bp->b_bcount = SI_BUFSZ;
12590Sstevel@tonic-gate 		bp->b_flags = B_READ;
12600Sstevel@tonic-gate 		bp->b_un.b_addr = bufs + (i * SI_BUFSZ);
12610Sstevel@tonic-gate 		bp->b_edev = dev;
12620Sstevel@tonic-gate 	}
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	/*
12650Sstevel@tonic-gate 	 * Repeat while there are cylinder groups left to read.
12660Sstevel@tonic-gate 	 */
12670Sstevel@tonic-gate 	do {
12680Sstevel@tonic-gate 		/*
12690Sstevel@tonic-gate 		 * Issue upto NSIBUF asynchronous reads
12700Sstevel@tonic-gate 		 */
12710Sstevel@tonic-gate 		ncg = MIN(NSIBUF, (fs->fs_ncg - cg));
12720Sstevel@tonic-gate 		for (bp = bps, i = 0; i < ncg; i++, bp++) {
12730Sstevel@tonic-gate 			bp->b_blkno = (daddr_t)fsbtodb(fs, cgtod(fs, cg + i));
12740Sstevel@tonic-gate 			if (ufsvfsp->vfs_log) {
12750Sstevel@tonic-gate 				lufs_read_strategy(ufsvfsp->vfs_log, bp);
12760Sstevel@tonic-gate 			} else {
12770Sstevel@tonic-gate 				(void) bdev_strategy(bp);
12780Sstevel@tonic-gate 			}
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		/*
12820Sstevel@tonic-gate 		 * wait for each read to finish;
12830Sstevel@tonic-gate 		 * check for errors and copy the csum info
12840Sstevel@tonic-gate 		 */
12850Sstevel@tonic-gate 		for (bp = bps, i = 0; i < ncg; i++, bp++) {
12860Sstevel@tonic-gate 			sema_p(&bp->b_io);
12870Sstevel@tonic-gate 			if (!error) {
12880Sstevel@tonic-gate 				cgp = bp->b_un.b_cg;
12890Sstevel@tonic-gate 				sip[cg + i] = cgp->cg_cs;
12900Sstevel@tonic-gate 				error = geterror(bp);
12910Sstevel@tonic-gate 			}
12920Sstevel@tonic-gate 		}
12930Sstevel@tonic-gate 		if (error) {
12940Sstevel@tonic-gate 			goto err;
12950Sstevel@tonic-gate 		}
12960Sstevel@tonic-gate 		cg += ncg;
12970Sstevel@tonic-gate 	} while (cg < fs->fs_ncg);
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate err:
13000Sstevel@tonic-gate 	kmem_free(bps, NSIBUF * sizeof (buf_t));
13010Sstevel@tonic-gate 	kmem_free(bufs, NSIBUF * SI_BUFSZ);
13020Sstevel@tonic-gate 	return (error);
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate  * ufs_getsummaryinfo
13070Sstevel@tonic-gate  */
13080Sstevel@tonic-gate int
ufs_getsummaryinfo(dev_t dev,struct ufsvfs * ufsvfsp,struct fs * fs)13090Sstevel@tonic-gate ufs_getsummaryinfo(dev_t dev, struct ufsvfs *ufsvfsp, struct fs *fs)
13100Sstevel@tonic-gate {
13110Sstevel@tonic-gate 	int		i;		/* `for' loop counter */
13120Sstevel@tonic-gate 	ssize_t		size;		/* bytes of summary info to read */
13130Sstevel@tonic-gate 	daddr_t		frags;		/* frags of summary info to read */
13140Sstevel@tonic-gate 	caddr_t		sip;		/* summary info */
13150Sstevel@tonic-gate 	struct buf	*tp;		/* tmp buf */
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	/*
13180Sstevel@tonic-gate 	 * maintain metadata map for trans device (debug only)
13190Sstevel@tonic-gate 	 */
13200Sstevel@tonic-gate 	TRANS_MATA_SI(ufsvfsp, fs);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * Compute #frags and allocate space for summary info
13240Sstevel@tonic-gate 	 */
13250Sstevel@tonic-gate 	frags = howmany(fs->fs_cssize, fs->fs_fsize);
13260Sstevel@tonic-gate 	sip = kmem_alloc((size_t)fs->fs_cssize, KM_SLEEP);
13270Sstevel@tonic-gate 	fs->fs_u.fs_csp = (struct csum *)sip;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	if (fs->fs_si == FS_SI_BAD) {
13300Sstevel@tonic-gate 		/*
13310Sstevel@tonic-gate 		 * The summary information is unknown, read it in from
13320Sstevel@tonic-gate 		 * the cylinder groups.
13330Sstevel@tonic-gate 		 */
13340Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) && !TRANS_ISERROR(ufsvfsp) &&
13350Sstevel@tonic-gate 		    ufsvfsp->vfs_log->un_logmap) {
13360Sstevel@tonic-gate 			logmap_roll_dev(ufsvfsp->vfs_log); /* flush the log */
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 		bzero(sip, (size_t)fs->fs_cssize);
13390Sstevel@tonic-gate 		if (ufs_construct_si(dev, fs, ufsvfsp)) {
13400Sstevel@tonic-gate 			kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
13410Sstevel@tonic-gate 			fs->fs_u.fs_csp = NULL;
13420Sstevel@tonic-gate 			return (EIO);
13430Sstevel@tonic-gate 		}
13440Sstevel@tonic-gate 	} else {
13450Sstevel@tonic-gate 		/* Read summary info a fs block at a time */
13460Sstevel@tonic-gate 		size = fs->fs_bsize;
13470Sstevel@tonic-gate 		for (i = 0; i < frags; i += fs->fs_frag) {
13480Sstevel@tonic-gate 			if (i + fs->fs_frag > frags)
13490Sstevel@tonic-gate 				/*
13500Sstevel@tonic-gate 				 * This happens only the last iteration, so
13510Sstevel@tonic-gate 				 * don't worry about size being reset
13520Sstevel@tonic-gate 				 */
13530Sstevel@tonic-gate 				size = (frags - i) * fs->fs_fsize;
13540Sstevel@tonic-gate 			tp = UFS_BREAD(ufsvfsp, dev,
13550Sstevel@tonic-gate 			    (daddr_t)fsbtodb(fs, fs->fs_csaddr+i), size);
13560Sstevel@tonic-gate 			tp->b_flags |= B_STALE | B_AGE;
13570Sstevel@tonic-gate 			if (tp->b_flags & B_ERROR) {
13580Sstevel@tonic-gate 				kmem_free(fs->fs_u.fs_csp, fs->fs_cssize);
13590Sstevel@tonic-gate 				fs->fs_u.fs_csp = NULL;
13600Sstevel@tonic-gate 				brelse(tp);
13610Sstevel@tonic-gate 				return (EIO);
13620Sstevel@tonic-gate 			}
13630Sstevel@tonic-gate 			bcopy(tp->b_un.b_addr, sip, size);
13640Sstevel@tonic-gate 			sip += size;
13650Sstevel@tonic-gate 			brelse(tp);
13660Sstevel@tonic-gate 		}
13670Sstevel@tonic-gate 	}
13680Sstevel@tonic-gate 	bzero((caddr_t)&fs->fs_cstotal, sizeof (fs->fs_cstotal));
13690Sstevel@tonic-gate 	for (i = 0; i < fs->fs_ncg; ++i) {
13700Sstevel@tonic-gate 		fs->fs_cstotal.cs_ndir += fs->fs_cs(fs, i).cs_ndir;
13710Sstevel@tonic-gate 		fs->fs_cstotal.cs_nbfree += fs->fs_cs(fs, i).cs_nbfree;
13720Sstevel@tonic-gate 		fs->fs_cstotal.cs_nifree += fs->fs_cs(fs, i).cs_nifree;
13730Sstevel@tonic-gate 		fs->fs_cstotal.cs_nffree += fs->fs_cs(fs, i).cs_nffree;
13740Sstevel@tonic-gate 	}
13750Sstevel@tonic-gate 	return (0);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * ufs_putsummaryinfo() stores all the cylinder group summary information
13800Sstevel@tonic-gate  * This is only used when logging, but the file system may not
13810Sstevel@tonic-gate  * be logging at the time, eg a read-only mount to flush the log
13820Sstevel@tonic-gate  * may push the summary info out.
13830Sstevel@tonic-gate  */
13840Sstevel@tonic-gate int
ufs_putsummaryinfo(dev_t dev,struct ufsvfs * ufsvfsp,struct fs * fs)13850Sstevel@tonic-gate ufs_putsummaryinfo(dev_t dev, struct ufsvfs *ufsvfsp, struct fs *fs)
13860Sstevel@tonic-gate {
13870Sstevel@tonic-gate 	struct buf	b, *bp;		/* tmp buf */
13880Sstevel@tonic-gate 	caddr_t		sip;		/* summary info */
13890Sstevel@tonic-gate 	ssize_t		size;		/* bytes of summary info to write */
13900Sstevel@tonic-gate 	daddr_t		frags;		/* frags of summary info to write */
13910Sstevel@tonic-gate 	int		i;		/* `for' loop counter */
13920Sstevel@tonic-gate 	int		error;		/* error */
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 	if (TRANS_ISERROR(ufsvfsp)) {
13950Sstevel@tonic-gate 		return (EIO);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	if ((fs->fs_si != FS_SI_BAD) || !ufsvfsp->vfs_nolog_si) {
13990Sstevel@tonic-gate 		return (0);
14000Sstevel@tonic-gate 	}
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	bp = &b;
14030Sstevel@tonic-gate 	bioinit(bp);
14040Sstevel@tonic-gate 	bp->b_iodone = ufs_si_io_done;
14050Sstevel@tonic-gate 	bp->b_bufsize = size = fs->fs_bsize;
14060Sstevel@tonic-gate 	bp->b_flags = B_WRITE;
14070Sstevel@tonic-gate 	bp->b_un.b_addr = kmem_alloc(size, KM_SLEEP);
14080Sstevel@tonic-gate 	bp->b_edev = dev;
14090Sstevel@tonic-gate 	frags = howmany(fs->fs_cssize, fs->fs_fsize);
14100Sstevel@tonic-gate 	sip = (caddr_t)fs->fs_u.fs_csp;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	/* Write summary info one fs block at a time */
14130Sstevel@tonic-gate 	for (error = 0, i = 0; (i < frags) && (error == 0); i += fs->fs_frag) {
14140Sstevel@tonic-gate 		if (i + fs->fs_frag > frags) {
14150Sstevel@tonic-gate 			/*
14160Sstevel@tonic-gate 			 * This happens only the last iteration, so
14170Sstevel@tonic-gate 			 * don't worry about size being reset
14180Sstevel@tonic-gate 			 */
14190Sstevel@tonic-gate 			size = (frags - i) * fs->fs_fsize;
14200Sstevel@tonic-gate 		}
14210Sstevel@tonic-gate 		bcopy(sip, bp->b_un.b_addr, size);
14220Sstevel@tonic-gate 		bp->b_blkno = (daddr_t)fsbtodb(fs, fs->fs_csaddr+i);
14230Sstevel@tonic-gate 		bp->b_bcount = size;
14240Sstevel@tonic-gate 		(void) bdev_strategy(bp);
14250Sstevel@tonic-gate 		sema_p(&bp->b_io); /* wait for write to complete */
14260Sstevel@tonic-gate 		error = geterror(bp);
14270Sstevel@tonic-gate 		sip += size;
14280Sstevel@tonic-gate 	}
14290Sstevel@tonic-gate 	kmem_free(bp->b_un.b_addr, fs->fs_bsize);
14300Sstevel@tonic-gate 	if (!error) {
14310Sstevel@tonic-gate 		fs->fs_si = FS_SI_OK;
14320Sstevel@tonic-gate 	}
14330Sstevel@tonic-gate 	return (error);
14340Sstevel@tonic-gate }
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate /*
14370Sstevel@tonic-gate  * Decide whether it is okay to remove within a sticky directory.
14380Sstevel@tonic-gate  * Two conditions need to be met:  write access to the directory
14390Sstevel@tonic-gate  * is needed.  In sticky directories, write access is not sufficient;
14400Sstevel@tonic-gate  * you can remove entries from a directory only if you own the directory,
14410Sstevel@tonic-gate  * if you are privileged, if you own the entry or if the entry is
14420Sstevel@tonic-gate  * a plain file and you have write access to that file.
14430Sstevel@tonic-gate  * Function returns 0 if remove access is granted.
14447737SFrank.Batschulat@Sun.COM  * Note, the caller is responsible for holding the i_contents lock
14457737SFrank.Batschulat@Sun.COM  * at least as reader on the inquired inode 'ip'.
14460Sstevel@tonic-gate  */
14470Sstevel@tonic-gate int
ufs_sticky_remove_access(struct inode * dp,struct inode * ip,struct cred * cr)14480Sstevel@tonic-gate ufs_sticky_remove_access(struct inode *dp, struct inode *ip, struct cred *cr)
14490Sstevel@tonic-gate {
14500Sstevel@tonic-gate 	uid_t uid;
14517737SFrank.Batschulat@Sun.COM 
14527737SFrank.Batschulat@Sun.COM 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
14537737SFrank.Batschulat@Sun.COM 
14540Sstevel@tonic-gate 	if ((dp->i_mode & ISVTX) &&
14550Sstevel@tonic-gate 	    (uid = crgetuid(cr)) != dp->i_uid &&
14560Sstevel@tonic-gate 	    uid != ip->i_uid &&
14570Sstevel@tonic-gate 	    ((ip->i_mode & IFMT) != IFREG ||
14587737SFrank.Batschulat@Sun.COM 	    ufs_iaccess(ip, IWRITE, cr, 0) != 0))
14590Sstevel@tonic-gate 		return (secpolicy_vnode_remove(cr));
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	return (0);
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate #endif	/* _KERNEL */
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate extern	int around[9];
14660Sstevel@tonic-gate extern	int inside[9];
14670Sstevel@tonic-gate extern	uchar_t *fragtbl[];
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate /*
14700Sstevel@tonic-gate  * Update the frsum fields to reflect addition or deletion
14710Sstevel@tonic-gate  * of some frags.
14720Sstevel@tonic-gate  */
14730Sstevel@tonic-gate void
fragacct(struct fs * fs,int fragmap,int32_t * fraglist,int cnt)14740Sstevel@tonic-gate fragacct(struct fs *fs, int fragmap, int32_t *fraglist, int cnt)
14750Sstevel@tonic-gate {
14760Sstevel@tonic-gate 	int inblk;
14770Sstevel@tonic-gate 	int field, subfield;
14780Sstevel@tonic-gate 	int siz, pos;
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 	/*
14810Sstevel@tonic-gate 	 * ufsvfsp->vfs_lock is held when calling this.
14820Sstevel@tonic-gate 	 */
14830Sstevel@tonic-gate 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
14840Sstevel@tonic-gate 	fragmap <<= 1;
14850Sstevel@tonic-gate 	for (siz = 1; siz < fs->fs_frag; siz++) {
14860Sstevel@tonic-gate 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
14870Sstevel@tonic-gate 			continue;
14880Sstevel@tonic-gate 		field = around[siz];
14890Sstevel@tonic-gate 		subfield = inside[siz];
14900Sstevel@tonic-gate 		for (pos = siz; pos <= fs->fs_frag; pos++) {
14910Sstevel@tonic-gate 			if ((fragmap & field) == subfield) {
14920Sstevel@tonic-gate 				fraglist[siz] += cnt;
14930Sstevel@tonic-gate 				ASSERT(fraglist[siz] >= 0);
14940Sstevel@tonic-gate 				pos += siz;
14950Sstevel@tonic-gate 				field <<= siz;
14960Sstevel@tonic-gate 				subfield <<= siz;
14970Sstevel@tonic-gate 			}
14980Sstevel@tonic-gate 			field <<= 1;
14990Sstevel@tonic-gate 			subfield <<= 1;
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate /*
15050Sstevel@tonic-gate  * Block operations
15060Sstevel@tonic-gate  */
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate /*
15090Sstevel@tonic-gate  * Check if a block is available
15100Sstevel@tonic-gate  */
15110Sstevel@tonic-gate int
isblock(struct fs * fs,uchar_t * cp,daddr_t h)15120Sstevel@tonic-gate isblock(struct fs *fs, uchar_t *cp, daddr_t h)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate 	uchar_t mask;
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	ASSERT(fs->fs_frag == 8 || fs->fs_frag == 4 || fs->fs_frag == 2 || \
15174662Sfrankho 	    fs->fs_frag == 1);
15180Sstevel@tonic-gate 	/*
15190Sstevel@tonic-gate 	 * ufsvfsp->vfs_lock is held when calling this.
15200Sstevel@tonic-gate 	 */
15210Sstevel@tonic-gate 	switch ((int)fs->fs_frag) {
15220Sstevel@tonic-gate 	case 8:
15230Sstevel@tonic-gate 		return (cp[h] == 0xff);
15240Sstevel@tonic-gate 	case 4:
15250Sstevel@tonic-gate 		mask = 0x0f << ((h & 0x1) << 2);
15260Sstevel@tonic-gate 		return ((cp[h >> 1] & mask) == mask);
15270Sstevel@tonic-gate 	case 2:
15280Sstevel@tonic-gate 		mask = 0x03 << ((h & 0x3) << 1);
15290Sstevel@tonic-gate 		return ((cp[h >> 2] & mask) == mask);
15300Sstevel@tonic-gate 	case 1:
15310Sstevel@tonic-gate 		mask = 0x01 << (h & 0x7);
15320Sstevel@tonic-gate 		return ((cp[h >> 3] & mask) == mask);
15330Sstevel@tonic-gate 	default:
15340Sstevel@tonic-gate #ifndef _KERNEL
15350Sstevel@tonic-gate 		cmn_err(CE_PANIC, "isblock: illegal fs->fs_frag value (%d)",
15364662Sfrankho 		    fs->fs_frag);
15370Sstevel@tonic-gate #endif /* _KERNEL */
15380Sstevel@tonic-gate 		return (0);
15390Sstevel@tonic-gate 	}
15400Sstevel@tonic-gate }
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate /*
15430Sstevel@tonic-gate  * Take a block out of the map
15440Sstevel@tonic-gate  */
15450Sstevel@tonic-gate void
clrblock(struct fs * fs,uchar_t * cp,daddr_t h)15460Sstevel@tonic-gate clrblock(struct fs *fs, uchar_t *cp, daddr_t h)
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	ASSERT(fs->fs_frag == 8 || fs->fs_frag == 4 || fs->fs_frag == 2 || \
15494662Sfrankho 	    fs->fs_frag == 1);
15500Sstevel@tonic-gate 	/*
15510Sstevel@tonic-gate 	 * ufsvfsp->vfs_lock is held when calling this.
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	switch ((int)fs->fs_frag) {
15540Sstevel@tonic-gate 	case 8:
15550Sstevel@tonic-gate 		cp[h] = 0;
15560Sstevel@tonic-gate 		return;
15570Sstevel@tonic-gate 	case 4:
15580Sstevel@tonic-gate 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
15590Sstevel@tonic-gate 		return;
15600Sstevel@tonic-gate 	case 2:
15610Sstevel@tonic-gate 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
15620Sstevel@tonic-gate 		return;
15630Sstevel@tonic-gate 	case 1:
15640Sstevel@tonic-gate 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
15650Sstevel@tonic-gate 		return;
15660Sstevel@tonic-gate 	default:
15670Sstevel@tonic-gate #ifndef _KERNEL
15680Sstevel@tonic-gate 		cmn_err(CE_PANIC, "clrblock: illegal fs->fs_frag value (%d)",
15694662Sfrankho 		    fs->fs_frag);
15700Sstevel@tonic-gate #endif /* _KERNEL */
15710Sstevel@tonic-gate 		return;
15720Sstevel@tonic-gate 	}
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Is block allocated?
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate int
isclrblock(struct fs * fs,uchar_t * cp,daddr_t h)15790Sstevel@tonic-gate isclrblock(struct fs *fs, uchar_t *cp, daddr_t h)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	uchar_t	mask;
15820Sstevel@tonic-gate 	int	frag;
15830Sstevel@tonic-gate 	/*
15840Sstevel@tonic-gate 	 * ufsvfsp->vfs_lock is held when calling this.
15850Sstevel@tonic-gate 	 */
15860Sstevel@tonic-gate 	frag = fs->fs_frag;
15870Sstevel@tonic-gate 	ASSERT(frag == 8 || frag == 4 || frag == 2 || frag == 1);
15880Sstevel@tonic-gate 	switch (frag) {
15890Sstevel@tonic-gate 	case 8:
15900Sstevel@tonic-gate 		return (cp[h] == 0);
15910Sstevel@tonic-gate 	case 4:
15920Sstevel@tonic-gate 		mask = ~(0x0f << ((h & 0x1) << 2));
15930Sstevel@tonic-gate 		return (cp[h >> 1] == (cp[h >> 1] & mask));
15940Sstevel@tonic-gate 	case 2:
15950Sstevel@tonic-gate 		mask =	~(0x03 << ((h & 0x3) << 1));
15960Sstevel@tonic-gate 		return (cp[h >> 2] == (cp[h >> 2] & mask));
15970Sstevel@tonic-gate 	case 1:
15980Sstevel@tonic-gate 		mask = ~(0x01 << (h & 0x7));
15990Sstevel@tonic-gate 		return (cp[h >> 3] == (cp[h >> 3] & mask));
16000Sstevel@tonic-gate 	default:
16010Sstevel@tonic-gate #ifndef _KERNEL
16020Sstevel@tonic-gate 		cmn_err(CE_PANIC, "isclrblock: illegal fs->fs_frag value (%d)",
16034662Sfrankho 		    fs->fs_frag);
16040Sstevel@tonic-gate #endif /* _KERNEL */
16050Sstevel@tonic-gate 		break;
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 	return (0);
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate /*
16110Sstevel@tonic-gate  * Put a block into the map
16120Sstevel@tonic-gate  */
16130Sstevel@tonic-gate void
setblock(struct fs * fs,uchar_t * cp,daddr_t h)16140Sstevel@tonic-gate setblock(struct fs *fs, uchar_t *cp, daddr_t h)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	ASSERT(fs->fs_frag == 8 || fs->fs_frag == 4 || fs->fs_frag == 2 || \
16174662Sfrankho 	    fs->fs_frag == 1);
16180Sstevel@tonic-gate 	/*
16190Sstevel@tonic-gate 	 * ufsvfsp->vfs_lock is held when calling this.
16200Sstevel@tonic-gate 	 */
16210Sstevel@tonic-gate 	switch ((int)fs->fs_frag) {
16220Sstevel@tonic-gate 	case 8:
16230Sstevel@tonic-gate 		cp[h] = 0xff;
16240Sstevel@tonic-gate 		return;
16250Sstevel@tonic-gate 	case 4:
16260Sstevel@tonic-gate 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
16270Sstevel@tonic-gate 		return;
16280Sstevel@tonic-gate 	case 2:
16290Sstevel@tonic-gate 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
16300Sstevel@tonic-gate 		return;
16310Sstevel@tonic-gate 	case 1:
16320Sstevel@tonic-gate 		cp[h >> 3] |= (0x01 << (h & 0x7));
16330Sstevel@tonic-gate 		return;
16340Sstevel@tonic-gate 	default:
16350Sstevel@tonic-gate #ifndef _KERNEL
16360Sstevel@tonic-gate 		cmn_err(CE_PANIC, "setblock: illegal fs->fs_frag value (%d)",
16374662Sfrankho 		    fs->fs_frag);
16380Sstevel@tonic-gate #endif /* _KERNEL */
16390Sstevel@tonic-gate 		return;
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate int
skpc(char c,uint_t len,char * cp)16440Sstevel@tonic-gate skpc(char c, uint_t len, char *cp)
16450Sstevel@tonic-gate {
16460Sstevel@tonic-gate 	if (len == 0)
16470Sstevel@tonic-gate 		return (0);
16480Sstevel@tonic-gate 	while (*cp++ == c && --len)
16490Sstevel@tonic-gate 		;
16500Sstevel@tonic-gate 	return (len);
16510Sstevel@tonic-gate }
1652