xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_filio.c (revision 12607:2bc0f474d551)
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*12607Sjohn.levon@sun.com  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/t_lock.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/time.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <sys/resource.h>
320Sstevel@tonic-gate #include <sys/signal.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/user.h>
350Sstevel@tonic-gate #include <sys/buf.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/proc.h>
390Sstevel@tonic-gate #include <sys/disp.h>
400Sstevel@tonic-gate #include <sys/file.h>
410Sstevel@tonic-gate #include <sys/fcntl.h>
420Sstevel@tonic-gate #include <sys/flock.h>
43329Saguzovsk #include <sys/atomic.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/uio.h>
460Sstevel@tonic-gate #include <sys/conf.h>
470Sstevel@tonic-gate #include <sys/mman.h>
480Sstevel@tonic-gate #include <sys/pathname.h>
490Sstevel@tonic-gate #include <sys/debug.h>
500Sstevel@tonic-gate #include <sys/vmsystm.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/filio.h>
530Sstevel@tonic-gate #include <sys/dnlc.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
560Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
570Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
580Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
590Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
600Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
610Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
620Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
630Sstevel@tonic-gate #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
640Sstevel@tonic-gate #include <sys/errno.h>
650Sstevel@tonic-gate #include <sys/sysinfo.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <vm/hat.h>
680Sstevel@tonic-gate #include <vm/page.h>
690Sstevel@tonic-gate #include <vm/pvn.h>
700Sstevel@tonic-gate #include <vm/as.h>
710Sstevel@tonic-gate #include <vm/seg.h>
720Sstevel@tonic-gate #include <vm/seg_map.h>
730Sstevel@tonic-gate #include <vm/seg_vn.h>
740Sstevel@tonic-gate #include <vm/rm.h>
750Sstevel@tonic-gate #include <sys/swap.h>
760Sstevel@tonic-gate #include <sys/model.h>
770Sstevel@tonic-gate #include <sys/policy.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate #include "fs/fs_subr.h"
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * ufs_fioio is the ufs equivalent of NFS_CNVT and is tailored to
830Sstevel@tonic-gate  * metamucil's needs.  It may change at any time.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate /* ARGSUSED */
860Sstevel@tonic-gate int
ufs_fioio(struct vnode * vp,struct fioio * fiou,int flag,struct cred * cr)870Sstevel@tonic-gate ufs_fioio(
880Sstevel@tonic-gate 	struct vnode	*vp,		/* any file on the fs */
890Sstevel@tonic-gate 	struct fioio	*fiou,		/* fioio struct in userland */
900Sstevel@tonic-gate 	int		flag,		/* flag from VOP_IOCTL() */
910Sstevel@tonic-gate 	struct cred	*cr)		/* credentials from ufs_ioctl */
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	int		error	= 0;
940Sstevel@tonic-gate 	struct vnode	*vpio	= NULL;	/* vnode for inode open */
950Sstevel@tonic-gate 	struct inode	*ipio	= NULL;	/* inode for inode open */
960Sstevel@tonic-gate 	struct file	*fpio	= NULL;	/* file  for inode open */
970Sstevel@tonic-gate 	struct inode	*ip;		/* inode for file system */
980Sstevel@tonic-gate 	struct fs	*fs;		/* fs    for file system */
990Sstevel@tonic-gate 	STRUCT_DECL(fioio, fio);	/* copy of user's fioio struct */
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/*
1020Sstevel@tonic-gate 	 * must be privileged
1030Sstevel@tonic-gate 	 */
1040Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, vp->v_vfsp) != 0)
1050Sstevel@tonic-gate 		return (EPERM);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	STRUCT_INIT(fio, flag & DATAMODEL_MASK);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	/*
1100Sstevel@tonic-gate 	 * get user's copy of fioio struct
1110Sstevel@tonic-gate 	 */
1120Sstevel@tonic-gate 	if (copyin(fiou, STRUCT_BUF(fio), STRUCT_SIZE(fio)))
1130Sstevel@tonic-gate 		return (EFAULT);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	ip = VTOI(vp);
1160Sstevel@tonic-gate 	fs = ip->i_fs;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	/*
1190Sstevel@tonic-gate 	 * check the inode number against the fs's inode number bounds
1200Sstevel@tonic-gate 	 */
1210Sstevel@tonic-gate 	if (STRUCT_FGET(fio, fio_ino) < UFSROOTINO)
1220Sstevel@tonic-gate 		return (ESRCH);
1230Sstevel@tonic-gate 	if (STRUCT_FGET(fio, fio_ino) >= fs->fs_ncg * fs->fs_ipg)
1240Sstevel@tonic-gate 		return (ESRCH);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	rw_enter(&ip->i_ufsvfs->vfs_dqrwlock, RW_READER);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	/*
1290Sstevel@tonic-gate 	 * get the inode
1300Sstevel@tonic-gate 	 */
1310Sstevel@tonic-gate 	error = ufs_iget(ip->i_vfs, STRUCT_FGET(fio, fio_ino), &ipio, cr);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	rw_exit(&ip->i_ufsvfs->vfs_dqrwlock);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	if (error)
1360Sstevel@tonic-gate 		return (error);
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * check the generation number
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	rw_enter(&ipio->i_contents, RW_READER);
1420Sstevel@tonic-gate 	if (ipio->i_gen != STRUCT_FGET(fio, fio_gen)) {
1430Sstevel@tonic-gate 		error = ESTALE;
1440Sstevel@tonic-gate 		rw_exit(&ipio->i_contents);
1450Sstevel@tonic-gate 		goto errout;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/*
1490Sstevel@tonic-gate 	 * check if the inode is free
1500Sstevel@tonic-gate 	 */
1510Sstevel@tonic-gate 	if (ipio->i_mode == 0) {
1520Sstevel@tonic-gate 		error = ENOENT;
1530Sstevel@tonic-gate 		rw_exit(&ipio->i_contents);
1540Sstevel@tonic-gate 		goto errout;
1550Sstevel@tonic-gate 	}
1560Sstevel@tonic-gate 	rw_exit(&ipio->i_contents);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 *	Adapted from copen: get a file struct
1600Sstevel@tonic-gate 	 *	Large Files: We open this file descriptor with FOFFMAX flag
1610Sstevel@tonic-gate 	 *	set so that it will be like a large file open.
1620Sstevel@tonic-gate 	 */
1630Sstevel@tonic-gate 	if (falloc(NULL, (FREAD|FOFFMAX), &fpio, STRUCT_FADDR(fio, fio_fd)))
1640Sstevel@tonic-gate 		goto errout;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 *	Adapted from vn_open: check access and then open the file
1680Sstevel@tonic-gate 	 */
1690Sstevel@tonic-gate 	vpio = ITOV(ipio);
1705331Samw 	if (error = VOP_ACCESS(vpio, VREAD, 0, cr, NULL))
1710Sstevel@tonic-gate 		goto errout;
1720Sstevel@tonic-gate 
1735331Samw 	if (error = VOP_OPEN(&vpio, FREAD, cr, NULL))
1740Sstevel@tonic-gate 		goto errout;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	/*
1770Sstevel@tonic-gate 	 *	Adapted from copen: initialize the file struct
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	fpio->f_vnode = vpio;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	/*
1820Sstevel@tonic-gate 	 * return the fd
1830Sstevel@tonic-gate 	 */
1840Sstevel@tonic-gate 	if (copyout(STRUCT_BUF(fio), fiou, STRUCT_SIZE(fio))) {
1850Sstevel@tonic-gate 		error = EFAULT;
1860Sstevel@tonic-gate 		goto errout;
1870Sstevel@tonic-gate 	}
1880Sstevel@tonic-gate 	setf(STRUCT_FGET(fio, fio_fd), fpio);
1890Sstevel@tonic-gate 	mutex_exit(&fpio->f_tlock);
1900Sstevel@tonic-gate 	return (0);
1910Sstevel@tonic-gate errout:
1920Sstevel@tonic-gate 	/*
1930Sstevel@tonic-gate 	 * free the file struct and fd
1940Sstevel@tonic-gate 	 */
1950Sstevel@tonic-gate 	if (fpio) {
1960Sstevel@tonic-gate 		setf(STRUCT_FGET(fio, fio_fd), NULL);
1970Sstevel@tonic-gate 		unfalloc(fpio);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * release the hold on the inode
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	if (ipio)
2040Sstevel@tonic-gate 		VN_RELE(ITOV(ipio));
2050Sstevel@tonic-gate 	return (error);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * ufs_fiosatime
2100Sstevel@tonic-gate  *	set access time w/o altering change time.  This ioctl is tailored
2110Sstevel@tonic-gate  *	to metamucil's needs and may change at any time.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate int
ufs_fiosatime(struct vnode * vp,struct timeval * tvu,int flag,struct cred * cr)2140Sstevel@tonic-gate ufs_fiosatime(
2150Sstevel@tonic-gate 	struct vnode	*vp,		/* file's vnode */
2160Sstevel@tonic-gate 	struct timeval	*tvu,		/* struct timeval in userland */
2170Sstevel@tonic-gate 	int		flag,		/* flag from VOP_IOCTL() */
2180Sstevel@tonic-gate 	struct cred	*cr)		/* credentials from ufs_ioctl */
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	struct inode	*ip;		/* inode for vp */
2210Sstevel@tonic-gate 	struct timeval32 tv;		/* copy of user's timeval */
2220Sstevel@tonic-gate 	int now = 0;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/*
2250Sstevel@tonic-gate 	 * must have sufficient privileges
2260Sstevel@tonic-gate 	 */
2270Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, vp->v_vfsp) != 0)
2280Sstevel@tonic-gate 		return (EPERM);
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * get user's copy of timeval struct and check values
2320Sstevel@tonic-gate 	 * if input is NULL, will set time to now
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 	if (tvu == NULL) {
2350Sstevel@tonic-gate 		now = 1;
2360Sstevel@tonic-gate 	} else {
2370Sstevel@tonic-gate 		if ((flag & DATAMODEL_MASK) == DATAMODEL_ILP32) {
2380Sstevel@tonic-gate 			if (copyin(tvu, &tv, sizeof (tv)))
2390Sstevel@tonic-gate 				return (EFAULT);
2400Sstevel@tonic-gate 		} else {
2410Sstevel@tonic-gate 			struct timeval tv64;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 			if (copyin(tvu, &tv64, sizeof (tv64)))
2440Sstevel@tonic-gate 				return (EFAULT);
2450Sstevel@tonic-gate 			if (TIMEVAL_OVERFLOW(&tv64))
2460Sstevel@tonic-gate 				return (EOVERFLOW);
2470Sstevel@tonic-gate 			TIMEVAL_TO_TIMEVAL32(&tv, &tv64);
2480Sstevel@tonic-gate 		}
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 		if (tv.tv_usec < 0 || tv.tv_usec >= 1000000)
2510Sstevel@tonic-gate 			return (EINVAL);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2550Sstevel@tonic-gate 	 * update access time
2560Sstevel@tonic-gate 	 */
2570Sstevel@tonic-gate 	ip = VTOI(vp);
2580Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
2590Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);
2600Sstevel@tonic-gate 	if (now) {
2610Sstevel@tonic-gate 		mutex_enter(&ufs_iuniqtime_lock);
2620Sstevel@tonic-gate 		ip->i_atime = iuniqtime;
2630Sstevel@tonic-gate 		mutex_exit(&ufs_iuniqtime_lock);
2640Sstevel@tonic-gate 	} else {
2650Sstevel@tonic-gate 		ip->i_atime = tv;
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	ip->i_flag |= IMODACC;
2680Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate  * ufs_fiogdio
2750Sstevel@tonic-gate  *	Get delayed-io state.  This ioctl is tailored
2760Sstevel@tonic-gate  *	to metamucil's needs and may change at any time.
2770Sstevel@tonic-gate  */
2780Sstevel@tonic-gate /* ARGSUSED */
2790Sstevel@tonic-gate int
ufs_fiogdio(struct vnode * vp,uint_t * diop,int flag,struct cred * cr)2800Sstevel@tonic-gate ufs_fiogdio(
2810Sstevel@tonic-gate 	struct vnode	*vp,		/* file's vnode */
2820Sstevel@tonic-gate 	uint_t		*diop,		/* dio state returned here */
2830Sstevel@tonic-gate 	int		flag,		/* flag from ufs_ioctl */
2840Sstevel@tonic-gate 	struct cred	*cr)		/* credentials from ufs_ioctl */
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp	= VTOI(vp)->i_ufsvfs;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * forcibly unmounted
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	if (ufsvfsp == NULL)
2920Sstevel@tonic-gate 		return (EIO);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	if (suword32(diop, ufsvfsp->vfs_dio))
2950Sstevel@tonic-gate 		return (EFAULT);
2960Sstevel@tonic-gate 	return (0);
2970Sstevel@tonic-gate }
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /*
3000Sstevel@tonic-gate  * ufs_fiosdio
3010Sstevel@tonic-gate  *	Set delayed-io state.  This ioctl is tailored
3020Sstevel@tonic-gate  *	to metamucil's needs and may change at any time.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate int
ufs_fiosdio(struct vnode * vp,uint_t * diop,int flag,struct cred * cr)3050Sstevel@tonic-gate ufs_fiosdio(
3060Sstevel@tonic-gate 	struct vnode	*vp,		/* file's vnode */
3070Sstevel@tonic-gate 	uint_t		*diop,		/* dio flag */
3080Sstevel@tonic-gate 	int		flag,		/* flag from ufs_ioctl */
3090Sstevel@tonic-gate 	struct cred	*cr)		/* credentials from ufs_ioctl */
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	uint_t		dio;		/* copy of user's dio */
3120Sstevel@tonic-gate 	struct inode	*ip;		/* inode for vp */
3130Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
3140Sstevel@tonic-gate 	struct fs	*fs;
3150Sstevel@tonic-gate 	struct ulockfs	*ulp;
3160Sstevel@tonic-gate 	int		error = 0;
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate #ifdef lint
3190Sstevel@tonic-gate 	flag = flag;
3200Sstevel@tonic-gate #endif
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/* check input conditions */
3230Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, vp->v_vfsp) != 0)
3240Sstevel@tonic-gate 		return (EPERM);
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate 	if (copyin(diop, &dio, sizeof (dio)))
3270Sstevel@tonic-gate 		return (EFAULT);
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (dio > 1)
3300Sstevel@tonic-gate 		return (EINVAL);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/* file system has been forcibly unmounted */
3330Sstevel@tonic-gate 	if (VTOI(vp)->i_ufsvfs == NULL)
3340Sstevel@tonic-gate 		return (EIO);
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	ip = VTOI(vp);
3370Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
3380Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	/* logging file system; dio ignored */
3410Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp))
3420Sstevel@tonic-gate 		return (error);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/* hold the mutex to prevent race with a lockfs request */
3450Sstevel@tonic-gate 	vfs_lock_wait(vp->v_vfsp);
3460Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
347329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(ulp)) {
3500Sstevel@tonic-gate 		error = EIO;
3510Sstevel@tonic-gate 		goto out;
3520Sstevel@tonic-gate 	}
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (ULOCKFS_IS_ELOCK(ulp)) {
3550Sstevel@tonic-gate 		error = EBUSY;
3560Sstevel@tonic-gate 		goto out;
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate 	/* wait for outstanding accesses to finish */
3590Sstevel@tonic-gate 	if (error = ufs_quiesce(ulp))
3600Sstevel@tonic-gate 		goto out;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* flush w/invalidate */
3630Sstevel@tonic-gate 	if (error = ufs_flush(vp->v_vfsp))
3640Sstevel@tonic-gate 		goto out;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * update dio
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	mutex_enter(&ufsvfsp->vfs_lock);
3700Sstevel@tonic-gate 	ufsvfsp->vfs_dio = dio;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	/*
3730Sstevel@tonic-gate 	 * enable/disable clean flag processing
3740Sstevel@tonic-gate 	 */
3750Sstevel@tonic-gate 	fs = ip->i_fs;
3760Sstevel@tonic-gate 	if (fs->fs_ronly == 0 &&
3770Sstevel@tonic-gate 	    fs->fs_clean != FSBAD &&
3780Sstevel@tonic-gate 	    fs->fs_clean != FSLOG) {
3790Sstevel@tonic-gate 		if (dio)
3800Sstevel@tonic-gate 			fs->fs_clean = FSSUSPEND;
3810Sstevel@tonic-gate 		else
3820Sstevel@tonic-gate 			fs->fs_clean = FSACTIVE;
3830Sstevel@tonic-gate 		ufs_sbwrite(ufsvfsp);
3840Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
3850Sstevel@tonic-gate 	} else
3860Sstevel@tonic-gate 		mutex_exit(&ufsvfsp->vfs_lock);
3870Sstevel@tonic-gate out:
3880Sstevel@tonic-gate 	/*
3890Sstevel@tonic-gate 	 * we need this broadcast because of the ufs_quiesce call above
3900Sstevel@tonic-gate 	 */
391329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
3920Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
3930Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
3940Sstevel@tonic-gate 	vfs_unlock(vp->v_vfsp);
3950Sstevel@tonic-gate 	return (error);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate /*
3990Sstevel@tonic-gate  * ufs_fioffs - ioctl handler for flushing file system
4000Sstevel@tonic-gate  */
4010Sstevel@tonic-gate /* ARGSUSED */
4020Sstevel@tonic-gate int
ufs_fioffs(struct vnode * vp,char * vap,struct cred * cr)4030Sstevel@tonic-gate ufs_fioffs(
4040Sstevel@tonic-gate 	struct vnode	*vp,
4050Sstevel@tonic-gate 	char 		*vap,		/* must be NULL - reserved */
4060Sstevel@tonic-gate 	struct cred	*cr)		/* credentials from ufs_ioctl */
4070Sstevel@tonic-gate {
4080Sstevel@tonic-gate 	int error;
4090Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
4100Sstevel@tonic-gate 	struct ulockfs	*ulp;
4110Sstevel@tonic-gate 
4120Sstevel@tonic-gate 	/* file system has been forcibly unmounted */
4130Sstevel@tonic-gate 	ufsvfsp = VTOI(vp)->i_ufsvfs;
4140Sstevel@tonic-gate 	if (ufsvfsp == NULL)
4150Sstevel@tonic-gate 		return (EIO);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	ulp = &ufsvfsp->vfs_ulockfs;
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	/*
4200Sstevel@tonic-gate 	 * suspend the delete thread
4210Sstevel@tonic-gate 	 *	this must be done outside the lockfs locking protocol
4220Sstevel@tonic-gate 	 */
4237276Sjr26306 	vfs_lock_wait(vp->v_vfsp);
4240Sstevel@tonic-gate 	ufs_thread_suspend(&ufsvfsp->vfs_delete);
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	/* hold the mutex to prevent race with a lockfs request */
4270Sstevel@tonic-gate 	mutex_enter(&ulp->ul_lock);
428329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, 1);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(ulp)) {
4310Sstevel@tonic-gate 		error = EIO;
4320Sstevel@tonic-gate 		goto out;
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 	if (ULOCKFS_IS_ELOCK(ulp)) {
4350Sstevel@tonic-gate 		error = EBUSY;
4360Sstevel@tonic-gate 		goto out;
4370Sstevel@tonic-gate 	}
4380Sstevel@tonic-gate 	/* wait for outstanding accesses to finish */
4390Sstevel@tonic-gate 	if (error = ufs_quiesce(ulp))
4400Sstevel@tonic-gate 		goto out;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * If logging, and the logmap was marked as not rollable,
4440Sstevel@tonic-gate 	 * make it rollable now, and start the trans_roll thread and
4450Sstevel@tonic-gate 	 * the reclaim thread.  The log at this point is safe to write to.
4460Sstevel@tonic-gate 	 */
4470Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
4480Sstevel@tonic-gate 		ml_unit_t	*ul = ufsvfsp->vfs_log;
4490Sstevel@tonic-gate 		struct fs	*fsp = ufsvfsp->vfs_fs;
4500Sstevel@tonic-gate 		int		err;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 		if (ul->un_flags & LDL_NOROLL) {
4530Sstevel@tonic-gate 			ul->un_flags &= ~LDL_NOROLL;
4540Sstevel@tonic-gate 			logmap_start_roll(ul);
4550Sstevel@tonic-gate 			if (!fsp->fs_ronly && (fsp->fs_reclaim &
4564662Sfrankho 			    (FS_RECLAIM|FS_RECLAIMING))) {
4570Sstevel@tonic-gate 				fsp->fs_reclaim &= ~FS_RECLAIM;
4580Sstevel@tonic-gate 				fsp->fs_reclaim |= FS_RECLAIMING;
4590Sstevel@tonic-gate 				ufs_thread_start(&ufsvfsp->vfs_reclaim,
4604662Sfrankho 				    ufs_thread_reclaim, vp->v_vfsp);
4610Sstevel@tonic-gate 				if (!fsp->fs_ronly) {
4620Sstevel@tonic-gate 					TRANS_SBWRITE(ufsvfsp,
4634662Sfrankho 					    TOP_SBUPDATE_UPDATE);
4640Sstevel@tonic-gate 					if (err =
4650Sstevel@tonic-gate 					    geterror(ufsvfsp->vfs_bufp)) {
4660Sstevel@tonic-gate 						refstr_t	*mntpt;
4670Sstevel@tonic-gate 						mntpt = vfs_getmntpoint(
4684662Sfrankho 						    vp->v_vfsp);
4690Sstevel@tonic-gate 						cmn_err(CE_NOTE,
4704662Sfrankho 						    "Filesystem Flush "
4714662Sfrankho 						    "Failed to update "
4724662Sfrankho 						    "Reclaim Status for "
4734662Sfrankho 						    " %s, Write failed to "
4744662Sfrankho 						    "update superblock, "
4754662Sfrankho 						    "error %d",
4764662Sfrankho 						    refstr_value(mntpt),
4774662Sfrankho 						    err);
4780Sstevel@tonic-gate 						refstr_rele(mntpt);
4790Sstevel@tonic-gate 					}
4800Sstevel@tonic-gate 				}
4810Sstevel@tonic-gate 			}
4820Sstevel@tonic-gate 		}
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/* synchronously flush dirty data and metadata */
4860Sstevel@tonic-gate 	error = ufs_flush(vp->v_vfsp);
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate out:
489329Saguzovsk 	atomic_add_long(&ufs_quiesce_pend, -1);
4900Sstevel@tonic-gate 	cv_broadcast(&ulp->ul_cv);
4910Sstevel@tonic-gate 	mutex_exit(&ulp->ul_lock);
4920Sstevel@tonic-gate 	vfs_unlock(vp->v_vfsp);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/*
4950Sstevel@tonic-gate 	 * allow the delete thread to continue
4960Sstevel@tonic-gate 	 */
4970Sstevel@tonic-gate 	ufs_thread_continue(&ufsvfsp->vfs_delete);
4980Sstevel@tonic-gate 	return (error);
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * ufs_fioisbusy
5030Sstevel@tonic-gate  *	Get number of references on this vnode.
5040Sstevel@tonic-gate  *	Contract-private interface for Legato's NetWorker product.
5050Sstevel@tonic-gate  */
5060Sstevel@tonic-gate /* ARGSUSED */
5070Sstevel@tonic-gate int
ufs_fioisbusy(struct vnode * vp,int * isbusy,struct cred * cr)5080Sstevel@tonic-gate ufs_fioisbusy(struct vnode *vp, int *isbusy, struct cred *cr)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	int is_it_busy;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * The caller holds one reference, there may be one in the dnlc
5140Sstevel@tonic-gate 	 * so we need to flush it.
5150Sstevel@tonic-gate 	 */
5160Sstevel@tonic-gate 	if (vp->v_count > 1)
5170Sstevel@tonic-gate 		dnlc_purge_vp(vp);
5180Sstevel@tonic-gate 	/*
5190Sstevel@tonic-gate 	 * Since we've just flushed the dnlc and we hold a reference
5200Sstevel@tonic-gate 	 * to this vnode, then anything but 1 means busy (this had
5210Sstevel@tonic-gate 	 * BETTER not be zero!). Also, it's possible for someone to
5220Sstevel@tonic-gate 	 * have this file mmap'ed with no additional reference count.
5230Sstevel@tonic-gate 	 */
5240Sstevel@tonic-gate 	ASSERT(vp->v_count > 0);
5250Sstevel@tonic-gate 	if ((vp->v_count == 1) && (VTOI(vp)->i_mapcnt == 0))
5260Sstevel@tonic-gate 		is_it_busy = 0;
5270Sstevel@tonic-gate 	else
5280Sstevel@tonic-gate 		is_it_busy = 1;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	if (suword32(isbusy, is_it_busy))
5310Sstevel@tonic-gate 		return (EFAULT);
5320Sstevel@tonic-gate 	return (0);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /* ARGSUSED */
5360Sstevel@tonic-gate int
ufs_fiodirectio(struct vnode * vp,int cmd,struct cred * cr)5370Sstevel@tonic-gate ufs_fiodirectio(struct vnode *vp, int cmd, struct cred *cr)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	int		error	= 0;
5400Sstevel@tonic-gate 	struct inode	*ip	= VTOI(vp);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	/*
5430Sstevel@tonic-gate 	 * Acquire reader lock and set/reset direct mode
5440Sstevel@tonic-gate 	 */
5450Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
5460Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
5470Sstevel@tonic-gate 	if (cmd == DIRECTIO_ON)
5480Sstevel@tonic-gate 		ip->i_flag |= IDIRECTIO;	/* enable direct mode */
5490Sstevel@tonic-gate 	else if (cmd == DIRECTIO_OFF)
5500Sstevel@tonic-gate 		ip->i_flag &= ~IDIRECTIO;	/* disable direct mode */
5510Sstevel@tonic-gate 	else
5520Sstevel@tonic-gate 		error = EINVAL;
5530Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
5540Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
5550Sstevel@tonic-gate 	return (error);
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate /*
5590Sstevel@tonic-gate  * ufs_fiotune
5600Sstevel@tonic-gate  *	Allow some tunables to be set on a mounted fs
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate int
ufs_fiotune(struct vnode * vp,struct fiotune * uftp,struct cred * cr)5630Sstevel@tonic-gate ufs_fiotune(struct vnode *vp, struct fiotune *uftp, struct cred *cr)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	struct fiotune	ftp;
5660Sstevel@tonic-gate 	struct fs	*fs;
5670Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	/*
5700Sstevel@tonic-gate 	 * must have sufficient privileges
5710Sstevel@tonic-gate 	 */
5720Sstevel@tonic-gate 	if (secpolicy_fs_config(cr, vp->v_vfsp) != 0)
5730Sstevel@tonic-gate 		return (EPERM);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	/*
5760Sstevel@tonic-gate 	 * get user's copy
5770Sstevel@tonic-gate 	 */
5780Sstevel@tonic-gate 	if (copyin(uftp, &ftp, sizeof (ftp)))
5790Sstevel@tonic-gate 		return (EFAULT);
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate 	/*
5820Sstevel@tonic-gate 	 * some minimal sanity checks
5830Sstevel@tonic-gate 	 */
5840Sstevel@tonic-gate 	if ((ftp.maxcontig <= 0) ||
5850Sstevel@tonic-gate 	    (ftp.rotdelay != 0) ||
5860Sstevel@tonic-gate 	    (ftp.maxbpg <= 0) ||
5870Sstevel@tonic-gate 	    (ftp.minfree < 0) ||
5880Sstevel@tonic-gate 	    (ftp.minfree > 99) ||
5890Sstevel@tonic-gate 	    ((ftp.optim != FS_OPTTIME) && (ftp.optim != FS_OPTSPACE)))
5900Sstevel@tonic-gate 		return (EINVAL);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	/*
5930Sstevel@tonic-gate 	 * update superblock but don't write it!  If it gets out, fine.
5940Sstevel@tonic-gate 	 */
5950Sstevel@tonic-gate 	fs = VTOI(vp)->i_fs;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	fs->fs_maxcontig = ftp.maxcontig;
5980Sstevel@tonic-gate 	fs->fs_rotdelay = ftp.rotdelay;
5990Sstevel@tonic-gate 	fs->fs_maxbpg = ftp.maxbpg;
6000Sstevel@tonic-gate 	fs->fs_minfree = ftp.minfree;
6010Sstevel@tonic-gate 	fs->fs_optim = ftp.optim;
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * Adjust cluster based on the new maxcontig. The cluster size
6050Sstevel@tonic-gate 	 * can be any positive value. The check for this is done above.
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	ufsvfsp = VTOI(vp)->i_ufsvfs;
6080Sstevel@tonic-gate 	ufsvfsp->vfs_ioclustsz = fs->fs_bsize * fs->fs_maxcontig;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	/*
6110Sstevel@tonic-gate 	 * Adjust minfrags from minfree
6120Sstevel@tonic-gate 	 */
6130Sstevel@tonic-gate 	ufsvfsp->vfs_minfrags = (int)((int64_t)fs->fs_dsize *
6144662Sfrankho 	    fs->fs_minfree / 100);
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	/*
6170Sstevel@tonic-gate 	 * Write the superblock
6180Sstevel@tonic-gate 	 */
6190Sstevel@tonic-gate 	if (fs->fs_ronly == 0) {
6200Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SBUPDATE_UPDATE,
6210Sstevel@tonic-gate 		    TOP_SBWRITE_SIZE);
6220Sstevel@tonic-gate 		TRANS_SBWRITE(ufsvfsp, TOP_SBUPDATE_UPDATE);
6230Sstevel@tonic-gate 		TRANS_END_ASYNC(ufsvfsp, TOP_SBUPDATE_UPDATE, TOP_SBWRITE_SIZE);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	return (0);
6270Sstevel@tonic-gate }
6280Sstevel@tonic-gate 
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
6310Sstevel@tonic-gate  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
6320Sstevel@tonic-gate  */
6330Sstevel@tonic-gate int
ufs_fio_holey(vnode_t * vp,int cmd,offset_t * off)6340Sstevel@tonic-gate ufs_fio_holey(vnode_t *vp, int cmd, offset_t *off)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	inode_t	*ip = VTOI(vp);
6370Sstevel@tonic-gate 	u_offset_t noff = (u_offset_t)*off; /* new offset */
6380Sstevel@tonic-gate 	u_offset_t isz;
6390Sstevel@tonic-gate 	int error;
6400Sstevel@tonic-gate 	boolean_t hole;
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
64326Sperrin 	isz = ip->i_size;
64426Sperrin 	if (noff >= isz)  {
6450Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
6460Sstevel@tonic-gate 		return (ENXIO);
6470Sstevel@tonic-gate 	}
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	/*
6500Sstevel@tonic-gate 	 * Check for the usual case where a file has no holes.
6510Sstevel@tonic-gate 	 * If so we can optimise to set the end of the file as the first
6520Sstevel@tonic-gate 	 * (virtual) hole. This avoids bmap_find() searching through
6530Sstevel@tonic-gate 	 * every block in the file for a (non-existent) hole.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 	if (!bmap_has_holes(ip)) {
6560Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
6570Sstevel@tonic-gate 		if (cmd == _FIO_SEEK_HOLE) {
6580Sstevel@tonic-gate 			*off = isz;
6590Sstevel@tonic-gate 			return (0);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 		/* *off must already point to valid data (non hole) */
6620Sstevel@tonic-gate 		return (0);
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * Calling bmap_read() one block at a time on a 1TB file takes forever,
6670Sstevel@tonic-gate 	 * so we use a special function to search for holes or blocks.
6680Sstevel@tonic-gate 	 */
6690Sstevel@tonic-gate 	if (cmd == _FIO_SEEK_HOLE)
6700Sstevel@tonic-gate 		hole = B_TRUE;
6710Sstevel@tonic-gate 	else
6720Sstevel@tonic-gate 		hole = B_FALSE;
6730Sstevel@tonic-gate 	error = bmap_find(ip, hole, &noff);
6740Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/* end of file? */
6770Sstevel@tonic-gate 	if (error == ENXIO) {
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * Handle the virtual hole at the end of file.
6800Sstevel@tonic-gate 		 */
6810Sstevel@tonic-gate 		if (cmd == _FIO_SEEK_HOLE) {
6820Sstevel@tonic-gate 			*off = isz;
6830Sstevel@tonic-gate 			return (0);
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 		return (ENXIO);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	if (noff < *off)
6880Sstevel@tonic-gate 		return (error);
6890Sstevel@tonic-gate 	*off = noff;
6900Sstevel@tonic-gate 	return (error);
6910Sstevel@tonic-gate }
6925648Ssetje 
6935648Ssetje int
ufs_mark_compressed(struct vnode * vp)6945648Ssetje ufs_mark_compressed(struct vnode *vp)
6955648Ssetje {
6965648Ssetje 	struct inode *ip = VTOI(vp);
6975648Ssetje 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
6985648Ssetje 
6995648Ssetje 	if (vp->v_type != VREG)
7005648Ssetje 		return (EINVAL);
7015648Ssetje 
7025648Ssetje 	rw_enter(&ip->i_contents, RW_WRITER);
7035648Ssetje 	ip->i_cflags |= ICOMPRESS;
7045648Ssetje 	TRANS_INODE(ufsvfsp, ip);
7055648Ssetje 	ip->i_flag |= (ICHG|ISEQ);
7065648Ssetje 	ip->i_seq++;
7075648Ssetje 	if (!TRANS_ISTRANS(ufsvfsp))
7085648Ssetje 		ufs_iupdat(ip, I_ASYNC);
7095648Ssetje 	rw_exit(&ip->i_contents);
7105648Ssetje 
7115648Ssetje 	return (0);
7125648Ssetje }
713