xref: /onnv-gate/usr/src/uts/common/fs/ufs/ufs_vnops.c (revision 12170:1eb0a8fe1e90)
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
51268Sbatschul  * Common Development and Distribution License (the "License").
61268Sbatschul  * 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  */
217563SPrasad.Singamsetty@Sun.COM 
220Sstevel@tonic-gate /*
23*12170SOwen.Roberts@Sun.Com  * Copyright (c) 1984, 2010, Oracle and/or its affiliates. All rights reserved.
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  * Portions of this source code were derived from Berkeley 4.3 BSD
310Sstevel@tonic-gate  * under license from the Regents of the University of California.
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/t_lock.h>
360Sstevel@tonic-gate #include <sys/ksynch.h>
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <sys/time.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/sysmacros.h>
410Sstevel@tonic-gate #include <sys/resource.h>
420Sstevel@tonic-gate #include <sys/signal.h>
430Sstevel@tonic-gate #include <sys/cred.h>
440Sstevel@tonic-gate #include <sys/user.h>
450Sstevel@tonic-gate #include <sys/buf.h>
460Sstevel@tonic-gate #include <sys/vfs.h>
473898Srsb #include <sys/vfs_opreg.h>
480Sstevel@tonic-gate #include <sys/vnode.h>
490Sstevel@tonic-gate #include <sys/proc.h>
500Sstevel@tonic-gate #include <sys/disp.h>
510Sstevel@tonic-gate #include <sys/file.h>
520Sstevel@tonic-gate #include <sys/fcntl.h>
530Sstevel@tonic-gate #include <sys/flock.h>
54329Saguzovsk #include <sys/atomic.h>
550Sstevel@tonic-gate #include <sys/kmem.h>
560Sstevel@tonic-gate #include <sys/uio.h>
570Sstevel@tonic-gate #include <sys/dnlc.h>
580Sstevel@tonic-gate #include <sys/conf.h>
590Sstevel@tonic-gate #include <sys/mman.h>
600Sstevel@tonic-gate #include <sys/pathname.h>
610Sstevel@tonic-gate #include <sys/debug.h>
620Sstevel@tonic-gate #include <sys/vmsystm.h>
630Sstevel@tonic-gate #include <sys/cmn_err.h>
640Sstevel@tonic-gate #include <sys/filio.h>
650Sstevel@tonic-gate #include <sys/policy.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
680Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
690Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
700Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
710Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
720Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
730Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
740Sstevel@tonic-gate #include <sys/fs/ufs_snap.h>
750Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
760Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
770Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
780Sstevel@tonic-gate #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
790Sstevel@tonic-gate #include <sys/errno.h>
800Sstevel@tonic-gate #include <sys/fssnap_if.h>
810Sstevel@tonic-gate #include <sys/unistd.h>
820Sstevel@tonic-gate #include <sys/sunddi.h>
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #include <sys/filio.h>		/* _FIOIO */
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <vm/hat.h>
870Sstevel@tonic-gate #include <vm/page.h>
880Sstevel@tonic-gate #include <vm/pvn.h>
890Sstevel@tonic-gate #include <vm/as.h>
900Sstevel@tonic-gate #include <vm/seg.h>
910Sstevel@tonic-gate #include <vm/seg_map.h>
920Sstevel@tonic-gate #include <vm/seg_vn.h>
930Sstevel@tonic-gate #include <vm/seg_kmem.h>
940Sstevel@tonic-gate #include <vm/rm.h>
950Sstevel@tonic-gate #include <sys/swap.h>
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #include <fs/fs_subr.h>
980Sstevel@tonic-gate 
995648Ssetje #include <sys/fs/decomp.h>
1005648Ssetje 
1010Sstevel@tonic-gate static struct instats ins;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate static 	int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
1040Sstevel@tonic-gate static	int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
1050Sstevel@tonic-gate 		caddr_t, struct page **, size_t, enum seg_rw, int);
1065331Samw static	int ufs_open(struct vnode **, int, struct cred *, caller_context_t *);
1075331Samw static	int ufs_close(struct vnode *, int, int, offset_t, struct cred *,
1085331Samw 		caller_context_t *);
1090Sstevel@tonic-gate static	int ufs_read(struct vnode *, struct uio *, int, struct cred *,
1105331Samw 		struct caller_context *);
1110Sstevel@tonic-gate static	int ufs_write(struct vnode *, struct uio *, int, struct cred *,
1125331Samw 		struct caller_context *);
1135331Samw static	int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *,
1145331Samw 		int *, caller_context_t *);
1155331Samw static	int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *,
1165331Samw 		caller_context_t *);
1170Sstevel@tonic-gate static	int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
1185331Samw 		caller_context_t *);
1195331Samw static	int ufs_access(struct vnode *, int, int, struct cred *,
1205331Samw 		caller_context_t *);
1210Sstevel@tonic-gate static	int ufs_lookup(struct vnode *, char *, struct vnode **,
1225331Samw 		struct pathname *, int, struct vnode *, struct cred *,
1235331Samw 		caller_context_t *, int *, pathname_t *);
1240Sstevel@tonic-gate static	int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
1255331Samw 		int, struct vnode **, struct cred *, int,
1265331Samw 		caller_context_t *, vsecattr_t  *);
1275331Samw static	int ufs_remove(struct vnode *, char *, struct cred *,
1285331Samw 		caller_context_t *, int);
1295331Samw static	int ufs_link(struct vnode *, struct vnode *, char *, struct cred *,
1305331Samw 		caller_context_t *, int);
1310Sstevel@tonic-gate static	int ufs_rename(struct vnode *, char *, struct vnode *, char *,
1325331Samw 		struct cred *, caller_context_t *, int);
1330Sstevel@tonic-gate static	int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
1345331Samw 		struct cred *, caller_context_t *, int, vsecattr_t *);
1355331Samw static	int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *,
1365331Samw 		caller_context_t *, int);
1375331Samw static	int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *,
1385331Samw 		caller_context_t *, int);
1390Sstevel@tonic-gate static	int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
1405331Samw 		struct cred *, caller_context_t *, int);
1415331Samw static	int ufs_readlink(struct vnode *, struct uio *, struct cred *,
1425331Samw 		caller_context_t *);
1435331Samw static	int ufs_fsync(struct vnode *, int, struct cred *, caller_context_t *);
1445331Samw static	void ufs_inactive(struct vnode *, struct cred *, caller_context_t *);
1455331Samw static	int ufs_fid(struct vnode *, struct fid *, caller_context_t *);
1460Sstevel@tonic-gate static	int ufs_rwlock(struct vnode *, int, caller_context_t *);
1470Sstevel@tonic-gate static	void ufs_rwunlock(struct vnode *, int, caller_context_t *);
1485331Samw static	int ufs_seek(struct vnode *, offset_t, offset_t *, caller_context_t *);
1490Sstevel@tonic-gate static	int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
1505331Samw 		struct flk_callback *, struct cred *,
1515331Samw 		caller_context_t *);
1520Sstevel@tonic-gate static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
1530Sstevel@tonic-gate 		cred_t *, caller_context_t *);
1540Sstevel@tonic-gate static	int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
1550Sstevel@tonic-gate 		struct page **, size_t, struct seg *, caddr_t,
1565331Samw 		enum seg_rw, struct cred *, caller_context_t *);
1575331Samw static	int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
1585331Samw 		caller_context_t *);
1590Sstevel@tonic-gate static	int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
1600Sstevel@tonic-gate static	int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
1615331Samw 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
1620Sstevel@tonic-gate static	int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
1635331Samw 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
1640Sstevel@tonic-gate static	int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
1655331Samw 		uint_t, uint_t, uint_t, struct cred *, caller_context_t *);
1665331Samw static	int ufs_poll(vnode_t *, short, int, short *, struct pollhead **,
1675331Samw 		caller_context_t *);
1686264Srm15945 static	int ufs_dump(vnode_t *, caddr_t, offset_t, offset_t,
1696264Srm15945     caller_context_t *);
1705331Samw static	int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *,
1715331Samw 		caller_context_t *);
1720Sstevel@tonic-gate static	int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
1735331Samw 		struct cred *, caller_context_t *);
1746264Srm15945 static	int ufs_dumpctl(vnode_t *, int, offset_t *, caller_context_t *);
1750Sstevel@tonic-gate static	daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
1765331Samw 		daddr32_t *, int, int);
1775331Samw static	int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
1785331Samw 		caller_context_t *);
1795331Samw static	int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
1805331Samw 		caller_context_t *);
1817737SFrank.Batschulat@Sun.COM static	int ufs_priv_access(void *, int, struct cred *);
182*12170SOwen.Roberts@Sun.Com static	int ufs_eventlookup(struct vnode *, char *, struct cred *,
183*12170SOwen.Roberts@Sun.Com     struct vnode **);
1841899Svsakar extern int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
1851899Svsakar 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate struct vnodeops *ufs_vnodeops;
1920Sstevel@tonic-gate 
1933898Srsb /* NOTE: "not blkd" below  means that the operation isn't blocked by lockfs */
1940Sstevel@tonic-gate const fs_operation_def_t ufs_vnodeops_template[] = {
1953898Srsb 	VOPNAME_OPEN,		{ .vop_open = ufs_open },	/* not blkd */
1963898Srsb 	VOPNAME_CLOSE,		{ .vop_close = ufs_close },	/* not blkd */
1973898Srsb 	VOPNAME_READ,		{ .vop_read = ufs_read },
1983898Srsb 	VOPNAME_WRITE,		{ .vop_write = ufs_write },
1993898Srsb 	VOPNAME_IOCTL,		{ .vop_ioctl = ufs_ioctl },
2003898Srsb 	VOPNAME_GETATTR,	{ .vop_getattr = ufs_getattr },
2013898Srsb 	VOPNAME_SETATTR,	{ .vop_setattr = ufs_setattr },
2023898Srsb 	VOPNAME_ACCESS,		{ .vop_access = ufs_access },
2033898Srsb 	VOPNAME_LOOKUP,		{ .vop_lookup = ufs_lookup },
2043898Srsb 	VOPNAME_CREATE,		{ .vop_create = ufs_create },
2053898Srsb 	VOPNAME_REMOVE,		{ .vop_remove = ufs_remove },
2063898Srsb 	VOPNAME_LINK,		{ .vop_link = ufs_link },
2073898Srsb 	VOPNAME_RENAME,		{ .vop_rename = ufs_rename },
2083898Srsb 	VOPNAME_MKDIR,		{ .vop_mkdir = ufs_mkdir },
2093898Srsb 	VOPNAME_RMDIR,		{ .vop_rmdir = ufs_rmdir },
2103898Srsb 	VOPNAME_READDIR,	{ .vop_readdir = ufs_readdir },
2113898Srsb 	VOPNAME_SYMLINK,	{ .vop_symlink = ufs_symlink },
2123898Srsb 	VOPNAME_READLINK,	{ .vop_readlink = ufs_readlink },
2133898Srsb 	VOPNAME_FSYNC,		{ .vop_fsync = ufs_fsync },
2143898Srsb 	VOPNAME_INACTIVE,	{ .vop_inactive = ufs_inactive }, /* not blkd */
2153898Srsb 	VOPNAME_FID,		{ .vop_fid = ufs_fid },
2163898Srsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = ufs_rwlock },	/* not blkd */
2173898Srsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = ufs_rwunlock }, /* not blkd */
2183898Srsb 	VOPNAME_SEEK,		{ .vop_seek = ufs_seek },
2193898Srsb 	VOPNAME_FRLOCK,		{ .vop_frlock = ufs_frlock },
2203898Srsb 	VOPNAME_SPACE,		{ .vop_space = ufs_space },
2213898Srsb 	VOPNAME_GETPAGE,	{ .vop_getpage = ufs_getpage },
2223898Srsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = ufs_putpage },
2233898Srsb 	VOPNAME_MAP,		{ .vop_map = ufs_map },
2243898Srsb 	VOPNAME_ADDMAP,		{ .vop_addmap = ufs_addmap },	/* not blkd */
2253898Srsb 	VOPNAME_DELMAP,		{ .vop_delmap = ufs_delmap },	/* not blkd */
2263898Srsb 	VOPNAME_POLL,		{ .vop_poll = ufs_poll },	/* not blkd */
2273898Srsb 	VOPNAME_DUMP,		{ .vop_dump = ufs_dump },
2283898Srsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = ufs_l_pathconf },
2293898Srsb 	VOPNAME_PAGEIO,		{ .vop_pageio = ufs_pageio },
2303898Srsb 	VOPNAME_DUMPCTL,	{ .vop_dumpctl = ufs_dumpctl },
2313898Srsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = ufs_getsecattr },
2323898Srsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = ufs_setsecattr },
2333898Srsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
2343898Srsb 	NULL,			NULL
2350Sstevel@tonic-gate };
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate #define	MAX_BACKFILE_COUNT	9999
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate  * Created by ufs_dumpctl() to store a file's disk block info into memory.
2410Sstevel@tonic-gate  * Used by ufs_dump() to dump data to disk directly.
2420Sstevel@tonic-gate  */
2430Sstevel@tonic-gate struct dump {
2440Sstevel@tonic-gate 	struct inode	*ip;		/* the file we contain */
2450Sstevel@tonic-gate 	daddr_t		fsbs;		/* number of blocks stored */
2460Sstevel@tonic-gate 	struct timeval32 time;		/* time stamp for the struct */
2470Sstevel@tonic-gate 	daddr32_t 	dblk[1];	/* place holder for block info */
2480Sstevel@tonic-gate };
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static struct dump *dump_info = NULL;
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate /*
2530Sstevel@tonic-gate  * Previously there was no special action required for ordinary files.
2540Sstevel@tonic-gate  * (Devices are handled through the device file system.)
2550Sstevel@tonic-gate  * Now we support Large Files and Large File API requires open to
2560Sstevel@tonic-gate  * fail if file is large.
2570Sstevel@tonic-gate  * We could take care to prevent data corruption
2580Sstevel@tonic-gate  * by doing an atomic check of size and truncate if file is opened with
2590Sstevel@tonic-gate  * FTRUNC flag set but traditionally this is being done by the vfs/vnode
2600Sstevel@tonic-gate  * layers. So taking care of truncation here is a change in the existing
2610Sstevel@tonic-gate  * semantics of VOP_OPEN and therefore we chose not to implement any thing
2620Sstevel@tonic-gate  * here. The check for the size of the file > 2GB is being done at the
2630Sstevel@tonic-gate  * vfs layer in routine vn_open().
2640Sstevel@tonic-gate  */
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate /* ARGSUSED */
2670Sstevel@tonic-gate static int
ufs_open(struct vnode ** vpp,int flag,struct cred * cr,caller_context_t * ct)2685331Samw ufs_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate /*ARGSUSED*/
2740Sstevel@tonic-gate static int
ufs_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cr,caller_context_t * ct)2750Sstevel@tonic-gate ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
2765331Samw 	struct cred *cr, caller_context_t *ct)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2790Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	/*
2820Sstevel@tonic-gate 	 * Push partially filled cluster at last close.
2830Sstevel@tonic-gate 	 * ``last close'' is approximated because the dnlc
2840Sstevel@tonic-gate 	 * may have a hold on the vnode.
2850Sstevel@tonic-gate 	 * Checking for VBAD here will also act as a forced umount check.
2860Sstevel@tonic-gate 	 */
2870Sstevel@tonic-gate 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
2880Sstevel@tonic-gate 		struct inode *ip = VTOI(vp);
2890Sstevel@tonic-gate 		if (ip->i_delaylen) {
2900Sstevel@tonic-gate 			ins.in_poc.value.ul++;
2910Sstevel@tonic-gate 			(void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
2924662Sfrankho 			    B_ASYNC | B_FREE, cr);
2930Sstevel@tonic-gate 			ip->i_delaylen = 0;
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	return (0);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate /*ARGSUSED*/
3010Sstevel@tonic-gate static int
ufs_read(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cr,struct caller_context * ct)3020Sstevel@tonic-gate ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
3030Sstevel@tonic-gate 	struct caller_context *ct)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
3060Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
3070Sstevel@tonic-gate 	struct ulockfs *ulp = NULL;
3080Sstevel@tonic-gate 	int error = 0;
3090Sstevel@tonic-gate 	int intrans = 0;
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
3150Sstevel@tonic-gate 	 * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
3180Sstevel@tonic-gate 		/*
3190Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
3200Sstevel@tonic-gate 		 */
3210Sstevel@tonic-gate 		error = chklock(vp, FREAD, uiop->uio_loffset,
3224662Sfrankho 		    uiop->uio_resid, uiop->uio_fmode, ct);
3230Sstevel@tonic-gate 		if (error)
3240Sstevel@tonic-gate 			goto out;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
3280Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
3290Sstevel@tonic-gate 	if (error)
3300Sstevel@tonic-gate 		goto out;
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * In the case that a directory is opened for reading as a file
3340Sstevel@tonic-gate 	 * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
3350Sstevel@tonic-gate 	 * The locking order had to be changed to avoid a deadlock with
3360Sstevel@tonic-gate 	 * an update taking place on that directory at the same time.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if ((ip->i_mode & IFMT) == IFDIR) {
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3410Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3420Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 		if (error) {
3450Sstevel@tonic-gate 			if (ulp)
3460Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
3470Sstevel@tonic-gate 			goto out;
3480Sstevel@tonic-gate 		}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3510Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3520Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
3530Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3540Sstevel@tonic-gate 			    error);
3550Sstevel@tonic-gate 			ASSERT(!error);
3560Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3570Sstevel@tonic-gate 			    TOP_READ_SIZE);
3580Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_READER);
3590Sstevel@tonic-gate 		}
3600Sstevel@tonic-gate 	} else {
3610Sstevel@tonic-gate 		/*
3620Sstevel@tonic-gate 		 * Only transact reads to files opened for sync-read and
3630Sstevel@tonic-gate 		 * sync-write on a file system that is not write locked.
3640Sstevel@tonic-gate 		 *
3650Sstevel@tonic-gate 		 * The ``not write locked'' check prevents problems with
3660Sstevel@tonic-gate 		 * enabling/disabling logging on a busy file system.  E.g.,
3670Sstevel@tonic-gate 		 * logging exists at the beginning of the read but does not
3680Sstevel@tonic-gate 		 * at the end.
3690Sstevel@tonic-gate 		 *
3700Sstevel@tonic-gate 		 */
3710Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3720Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3730Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3740Sstevel@tonic-gate 			    error);
3750Sstevel@tonic-gate 			ASSERT(!error);
3760Sstevel@tonic-gate 			intrans = 1;
3770Sstevel@tonic-gate 		}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3800Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3810Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		if (intrans) {
3840Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3850Sstevel@tonic-gate 			    TOP_READ_SIZE);
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 	if (ulp) {
3900Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
3910Sstevel@tonic-gate 	}
3920Sstevel@tonic-gate out:
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 	return (error);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate extern	int	ufs_HW;		/* high water mark */
3980Sstevel@tonic-gate extern	int	ufs_LW;		/* low water mark */
3990Sstevel@tonic-gate int	ufs_WRITES = 1;		/* XXX - enable/disable */
4000Sstevel@tonic-gate int	ufs_throttles = 0;	/* throttling count */
4010Sstevel@tonic-gate int	ufs_allow_shared_writes = 1;	/* directio shared writes */
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate static int
ufs_check_rewrite(struct inode * ip,struct uio * uiop,int ioflag)4040Sstevel@tonic-gate ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
4050Sstevel@tonic-gate {
406612Sswilcox 	int	shared_write;
407612Sswilcox 
408612Sswilcox 	/*
409612Sswilcox 	 * If the FDSYNC flag is set then ignore the global
410612Sswilcox 	 * ufs_allow_shared_writes in this case.
411612Sswilcox 	 */
412612Sswilcox 	shared_write = (ioflag & FDSYNC) | ufs_allow_shared_writes;
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/*
4150Sstevel@tonic-gate 	 * Filter to determine if this request is suitable as a
4160Sstevel@tonic-gate 	 * concurrent rewrite. This write must not allocate blocks
4170Sstevel@tonic-gate 	 * by extending the file or filling in holes. No use trying
4180Sstevel@tonic-gate 	 * through FSYNC descriptors as the inode will be synchronously
4190Sstevel@tonic-gate 	 * updated after the write. The uio structure has not yet been
4200Sstevel@tonic-gate 	 * checked for sanity, so assume nothing.
4210Sstevel@tonic-gate 	 */
4220Sstevel@tonic-gate 	return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
4234662Sfrankho 	    (uiop->uio_loffset >= (offset_t)0) &&
4244662Sfrankho 	    (uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
4254662Sfrankho 	    ((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
4264662Sfrankho 	    !(ioflag & FSYNC) && !bmap_has_holes(ip) &&
4274662Sfrankho 	    shared_write);
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate /*ARGSUSED*/
4310Sstevel@tonic-gate static int
ufs_write(struct vnode * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)4320Sstevel@tonic-gate ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
4330Sstevel@tonic-gate 	caller_context_t *ct)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
4360Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
4370Sstevel@tonic-gate 	struct ulockfs *ulp;
4380Sstevel@tonic-gate 	int retry = 1;
4390Sstevel@tonic-gate 	int error, resv, resid = 0;
4400Sstevel@tonic-gate 	int directio_status;
4410Sstevel@tonic-gate 	int exclusive;
442612Sswilcox 	int rewriteflg;
4430Sstevel@tonic-gate 	long start_resid = uiop->uio_resid;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate retry_mandlock:
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
4500Sstevel@tonic-gate 	 * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
4510Sstevel@tonic-gate 	 * Check for forced unmounts normally done in ufs_lockfs_begin().
4520Sstevel@tonic-gate 	 */
4530Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
4540Sstevel@tonic-gate 		error = EIO;
4550Sstevel@tonic-gate 		goto out;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 		/*
4620Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
4630Sstevel@tonic-gate 		 */
4640Sstevel@tonic-gate 		error = chklock(vp, FWRITE, uiop->uio_loffset,
4654662Sfrankho 		    uiop->uio_resid, uiop->uio_fmode, ct);
4660Sstevel@tonic-gate 		if (error)
4670Sstevel@tonic-gate 			goto out;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	/* i_rwlock can change in chklock */
4710Sstevel@tonic-gate 	exclusive = rw_write_held(&ip->i_rwlock);
472612Sswilcox 	rewriteflg = ufs_check_rewrite(ip, uiop, ioflag);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Check for fast-path special case of directio re-writes.
4760Sstevel@tonic-gate 	 */
4770Sstevel@tonic-gate 	if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
478612Sswilcox 	    !exclusive && rewriteflg) {
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
4810Sstevel@tonic-gate 		if (error)
4820Sstevel@tonic-gate 			goto out;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
4850Sstevel@tonic-gate 		error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
4864662Sfrankho 		    &directio_status);
4870Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS) {
4880Sstevel@tonic-gate 			uint_t i_flag_save;
4890Sstevel@tonic-gate 
4900Sstevel@tonic-gate 			if (start_resid != uiop->uio_resid)
4910Sstevel@tonic-gate 				error = 0;
4920Sstevel@tonic-gate 			/*
4930Sstevel@tonic-gate 			 * Special treatment of access times for re-writes.
4940Sstevel@tonic-gate 			 * If IMOD is not already set, then convert it
4950Sstevel@tonic-gate 			 * to IMODACC for this operation. This defers
4960Sstevel@tonic-gate 			 * entering a delta into the log until the inode
4970Sstevel@tonic-gate 			 * is flushed. This mimics what is done for read
4980Sstevel@tonic-gate 			 * operations and inode access time.
4990Sstevel@tonic-gate 			 */
5000Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
5010Sstevel@tonic-gate 			i_flag_save = ip->i_flag;
5020Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
5030Sstevel@tonic-gate 			ip->i_seq++;
5040Sstevel@tonic-gate 			ITIMES_NOLOCK(ip);
5050Sstevel@tonic-gate 			if ((i_flag_save & IMOD) == 0) {
5060Sstevel@tonic-gate 				ip->i_flag &= ~IMOD;
5070Sstevel@tonic-gate 				ip->i_flag |= IMODACC;
5080Sstevel@tonic-gate 			}
5090Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
5100Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5110Sstevel@tonic-gate 			if (ulp)
5120Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
5130Sstevel@tonic-gate 			goto out;
5140Sstevel@tonic-gate 		}
5150Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
5160Sstevel@tonic-gate 		if (ulp)
5170Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
5210Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
5220Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
5230Sstevel@tonic-gate 		/*
5240Sstevel@tonic-gate 		 * Mandatory locking could have been enabled
5250Sstevel@tonic-gate 		 * after dropping the i_rwlock.
5260Sstevel@tonic-gate 		 */
5270Sstevel@tonic-gate 		if (MANDLOCK(vp, ip->i_mode))
5280Sstevel@tonic-gate 			goto retry_mandlock;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
5320Sstevel@tonic-gate 	if (error)
5330Sstevel@tonic-gate 		goto out;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	/*
5360Sstevel@tonic-gate 	 * Amount of log space needed for this write
5370Sstevel@tonic-gate 	 */
538612Sswilcox 	if (!rewriteflg || !(ioflag & FDSYNC))
539612Sswilcox 		TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 	/*
5420Sstevel@tonic-gate 	 * Throttle writes.
5430Sstevel@tonic-gate 	 */
5440Sstevel@tonic-gate 	if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
5450Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5460Sstevel@tonic-gate 		while (ip->i_writes > ufs_HW) {
5470Sstevel@tonic-gate 			ufs_throttles++;
5480Sstevel@tonic-gate 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * Enter Transaction
555612Sswilcox 	 *
556612Sswilcox 	 * If the write is a rewrite there is no need to open a transaction
557612Sswilcox 	 * if the FDSYNC flag is set and not the FSYNC.  In this case just
558612Sswilcox 	 * set the IMODACC flag to modify do the update at a later time
559612Sswilcox 	 * thus avoiding the overhead of the logging transaction that is
560612Sswilcox 	 * not required.
5610Sstevel@tonic-gate 	 */
5620Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
5630Sstevel@tonic-gate 		if (ulp) {
564612Sswilcox 			if (rewriteflg) {
565612Sswilcox 				uint_t i_flag_save;
566612Sswilcox 
567612Sswilcox 				rw_enter(&ip->i_contents, RW_READER);
568612Sswilcox 				mutex_enter(&ip->i_tlock);
569612Sswilcox 				i_flag_save = ip->i_flag;
570612Sswilcox 				ip->i_flag |= IUPD | ICHG;
571612Sswilcox 				ip->i_seq++;
572612Sswilcox 				ITIMES_NOLOCK(ip);
573612Sswilcox 				if ((i_flag_save & IMOD) == 0) {
574612Sswilcox 					ip->i_flag &= ~IMOD;
575612Sswilcox 					ip->i_flag |= IMODACC;
576612Sswilcox 				}
577612Sswilcox 				mutex_exit(&ip->i_tlock);
578612Sswilcox 				rw_exit(&ip->i_contents);
579612Sswilcox 			} else {
580612Sswilcox 				int terr = 0;
581612Sswilcox 				TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv,
582612Sswilcox 				    terr);
583612Sswilcox 				ASSERT(!terr);
584612Sswilcox 			}
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 	} else {
5870Sstevel@tonic-gate 		if (ulp)
5880Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * Write the file
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
5950Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
5960Sstevel@tonic-gate 	if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
5970Sstevel@tonic-gate 		/*
5980Sstevel@tonic-gate 		 * In append mode start at end of file.
5990Sstevel@tonic-gate 		 */
6000Sstevel@tonic-gate 		uiop->uio_loffset = ip->i_size;
6010Sstevel@tonic-gate 	}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 	/*
6040Sstevel@tonic-gate 	 * Mild optimisation, don't call ufs_trans_write() unless we have to
6050Sstevel@tonic-gate 	 * Also, suppress file system full messages if we will retry.
6060Sstevel@tonic-gate 	 */
6070Sstevel@tonic-gate 	if (retry)
6080Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
6090Sstevel@tonic-gate 	if (resid) {
6100Sstevel@tonic-gate 		TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
6110Sstevel@tonic-gate 	} else {
6120Sstevel@tonic-gate 		error = wrip(ip, uiop, ioflag, cr);
6130Sstevel@tonic-gate 	}
6140Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
6170Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	/*
6200Sstevel@tonic-gate 	 * Leave Transaction
6210Sstevel@tonic-gate 	 */
6220Sstevel@tonic-gate 	if (ulp) {
6230Sstevel@tonic-gate 		if (ioflag & (FSYNC|FDSYNC)) {
624612Sswilcox 			if (!rewriteflg) {
625612Sswilcox 				int terr = 0;
626612Sswilcox 
627612Sswilcox 				TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC,
6284662Sfrankho 				    resv);
629612Sswilcox 				if (error == 0)
630612Sswilcox 					error = terr;
631612Sswilcox 			}
6320Sstevel@tonic-gate 		} else {
6330Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
6340Sstevel@tonic-gate 		}
6350Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
6360Sstevel@tonic-gate 	}
6370Sstevel@tonic-gate out:
6380Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * Any blocks tied up in pending deletes?
6410Sstevel@tonic-gate 		 */
6420Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
6430Sstevel@tonic-gate 		retry = 0;
6440Sstevel@tonic-gate 		goto retry_mandlock;
6450Sstevel@tonic-gate 	}
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (error == ENOSPC && (start_resid != uiop->uio_resid))
6480Sstevel@tonic-gate 		error = 0;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	return (error);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*
6540Sstevel@tonic-gate  * Don't cache write blocks to files with the sticky bit set.
6550Sstevel@tonic-gate  * Used to keep swap files from blowing the page cache on a server.
6560Sstevel@tonic-gate  */
6570Sstevel@tonic-gate int stickyhack = 1;
6580Sstevel@tonic-gate 
6590Sstevel@tonic-gate /*
6600Sstevel@tonic-gate  * Free behind hacks.  The pager is busted.
6610Sstevel@tonic-gate  * XXX - need to pass the information down to writedone() in a flag like B_SEQ
6620Sstevel@tonic-gate  * or B_FREE_IF_TIGHT_ON_MEMORY.
6630Sstevel@tonic-gate  */
6640Sstevel@tonic-gate int	freebehind = 1;
665543Srbourbon int	smallfile = 0;
666543Srbourbon u_offset_t smallfile64 = 32 * 1024;
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * While we should, in most cases, cache the pages for write, we
6700Sstevel@tonic-gate  * may also want to cache the pages for read as long as they are
6710Sstevel@tonic-gate  * frequently re-usable.
6720Sstevel@tonic-gate  *
6730Sstevel@tonic-gate  * If cache_read_ahead = 1, the pages for read will go to the tail
6740Sstevel@tonic-gate  * of the cache list when they are released, otherwise go to the head.
6750Sstevel@tonic-gate  */
6760Sstevel@tonic-gate int	cache_read_ahead = 0;
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
679543Srbourbon  * Freebehind exists  so that as we read  large files  sequentially we
680543Srbourbon  * don't consume most of memory with pages  from a few files. It takes
681543Srbourbon  * longer to re-read from disk multiple small files as it does reading
682543Srbourbon  * one large one sequentially.  As system  memory grows customers need
683543Srbourbon  * to retain bigger chunks   of files in  memory.   The advent of  the
684543Srbourbon  * cachelist opens up of the possibility freeing pages  to the head or
685543Srbourbon  * tail of the list.
686543Srbourbon  *
687543Srbourbon  * Not freeing a page is a bet that the page will be read again before
688543Srbourbon  * it's segmap slot is needed for something else. If we loose the bet,
689543Srbourbon  * it means some  other thread is  burdened with the  page free we did
690543Srbourbon  * not do. If we win we save a free and reclaim.
691543Srbourbon  *
692543Srbourbon  * Freeing it at the tail  vs the head of cachelist  is a bet that the
693543Srbourbon  * page will survive until the next  read.  It's also saying that this
694543Srbourbon  * page is more likely to  be re-used than a  page freed some time ago
695543Srbourbon  * and never reclaimed.
696543Srbourbon  *
697543Srbourbon  * Freebehind maintains a  range of  file offset [smallfile1; smallfile2]
698543Srbourbon  *
699543Srbourbon  *            0 < offset < smallfile1 : pages are not freed.
700543Srbourbon  *   smallfile1 < offset < smallfile2 : pages freed to tail of cachelist.
701543Srbourbon  *   smallfile2 < offset              : pages freed to head of cachelist.
702543Srbourbon  *
703543Srbourbon  * The range  is  computed  at most  once  per second  and  depends on
704543Srbourbon  * freemem  and  ncpus_online.  Both parameters  are   bounded to be
705543Srbourbon  * >= smallfile && >= smallfile64.
706543Srbourbon  *
707543Srbourbon  * smallfile1 = (free memory / ncpu) / 1000
708543Srbourbon  * smallfile2 = (free memory / ncpu) / 10
709543Srbourbon  *
710543Srbourbon  * A few examples values:
711543Srbourbon  *
712543Srbourbon  *       Free Mem (in Bytes) [smallfile1; smallfile2]  [smallfile1; smallfile2]
713543Srbourbon  *                                 ncpus_online = 4          ncpus_online = 64
714543Srbourbon  *       ------------------  -----------------------   -----------------------
715543Srbourbon  *             1G                   [256K;  25M]               [32K; 1.5M]
716543Srbourbon  *            10G                   [2.5M; 250M]              [156K; 15M]
717543Srbourbon  *           100G                    [25M; 2.5G]              [1.5M; 150M]
718543Srbourbon  *
719543Srbourbon  */
720543Srbourbon 
721543Srbourbon #define	SMALLFILE1_D 1000
722543Srbourbon #define	SMALLFILE2_D 10
723543Srbourbon static u_offset_t smallfile1 = 32 * 1024;
724543Srbourbon static u_offset_t smallfile2 = 32 * 1024;
725543Srbourbon static clock_t smallfile_update = 0; /* lbolt value of when to recompute */
726543Srbourbon uint_t smallfile1_d = SMALLFILE1_D;
727543Srbourbon uint_t smallfile2_d = SMALLFILE2_D;
728543Srbourbon 
729543Srbourbon /*
7300Sstevel@tonic-gate  * wrip does the real work of write requests for ufs.
7310Sstevel@tonic-gate  */
7320Sstevel@tonic-gate int
wrip(struct inode * ip,struct uio * uio,int ioflag,struct cred * cr)7330Sstevel@tonic-gate wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
7340Sstevel@tonic-gate {
7350Sstevel@tonic-gate 	rlim64_t limit = uio->uio_llimit;
7360Sstevel@tonic-gate 	u_offset_t off;
7370Sstevel@tonic-gate 	u_offset_t old_i_size;
7380Sstevel@tonic-gate 	struct fs *fs;
7390Sstevel@tonic-gate 	struct vnode *vp;
7400Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
7410Sstevel@tonic-gate 	caddr_t base;
7420Sstevel@tonic-gate 	long start_resid = uio->uio_resid;	/* save starting resid */
7430Sstevel@tonic-gate 	long premove_resid;			/* resid before uiomove() */
7440Sstevel@tonic-gate 	uint_t flags;
7450Sstevel@tonic-gate 	int newpage;
7460Sstevel@tonic-gate 	int iupdat_flag, directio_status;
7470Sstevel@tonic-gate 	int n, on, mapon;
7480Sstevel@tonic-gate 	int error, pagecreate;
7490Sstevel@tonic-gate 	int do_dqrwlock;		/* drop/reacquire vfs_dqrwlock */
7500Sstevel@tonic-gate 	int32_t	iblocks;
7510Sstevel@tonic-gate 	int	new_iblocks;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	/*
7540Sstevel@tonic-gate 	 * ip->i_size is incremented before the uiomove
7550Sstevel@tonic-gate 	 * is done on a write.  If the move fails (bad user
7560Sstevel@tonic-gate 	 * address) reset ip->i_size.
7570Sstevel@tonic-gate 	 * The better way would be to increment ip->i_size
7580Sstevel@tonic-gate 	 * only if the uiomove succeeds.
7590Sstevel@tonic-gate 	 */
7600Sstevel@tonic-gate 	int i_size_changed = 0;
7610Sstevel@tonic-gate 	o_mode_t type;
7620Sstevel@tonic-gate 	int i_seq_needed = 0;
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	vp = ITOV(ip);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * check for forced unmount - should not happen as
7680Sstevel@tonic-gate 	 * the request passed the lockfs checks.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
7710Sstevel@tonic-gate 		return (EIO);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	fs = ip->i_fs;
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/* check for valid filetype */
7780Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
7790Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
7800Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
7810Sstevel@tonic-gate 		return (EIO);
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	/*
7850Sstevel@tonic-gate 	 * the actual limit of UFS file size
7860Sstevel@tonic-gate 	 * is UFS_MAXOFFSET_T
7870Sstevel@tonic-gate 	 */
7880Sstevel@tonic-gate 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
7890Sstevel@tonic-gate 		limit = MAXOFFSET_T;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	if (uio->uio_loffset >= limit) {
7920Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7950Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
7960Sstevel@tonic-gate 		    p, RCA_UNSAFE_SIGINFO);
7970Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7980Sstevel@tonic-gate 		return (EFBIG);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/*
8020Sstevel@tonic-gate 	 * if largefiles are disallowed, the limit is
8030Sstevel@tonic-gate 	 * the pre-largefiles value of 2GB
8040Sstevel@tonic-gate 	 */
8050Sstevel@tonic-gate 	if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
8060Sstevel@tonic-gate 		limit = MIN(UFS_MAXOFFSET_T, limit);
8070Sstevel@tonic-gate 	else
8080Sstevel@tonic-gate 		limit = MIN(MAXOFF32_T, limit);
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
8110Sstevel@tonic-gate 		return (EINVAL);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
8140Sstevel@tonic-gate 		return (0);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	if (uio->uio_loffset >= limit)
8180Sstevel@tonic-gate 		return (EFBIG);
8190Sstevel@tonic-gate 
8200Sstevel@tonic-gate 	ip->i_flag |= INOACC;	/* don't update ref time in getpage */
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
8230Sstevel@tonic-gate 		ip->i_flag |= ISYNC;
8240Sstevel@tonic-gate 		iupdat_flag = 1;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	/*
8270Sstevel@tonic-gate 	 * Try to go direct
8280Sstevel@tonic-gate 	 */
8290Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
8300Sstevel@tonic-gate 		uio->uio_llimit = limit;
8310Sstevel@tonic-gate 		error = ufs_directio_write(ip, uio, ioflag, 0, cr,
8324662Sfrankho 		    &directio_status);
8330Sstevel@tonic-gate 		/*
8340Sstevel@tonic-gate 		 * If ufs_directio wrote to the file or set the flags,
8350Sstevel@tonic-gate 		 * we need to update i_seq, but it may be deferred.
8360Sstevel@tonic-gate 		 */
8370Sstevel@tonic-gate 		if (start_resid != uio->uio_resid ||
8384662Sfrankho 		    (ip->i_flag & (ICHG|IUPD))) {
8390Sstevel@tonic-gate 			i_seq_needed = 1;
8400Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
8410Sstevel@tonic-gate 		}
8420Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
8430Sstevel@tonic-gate 			goto out;
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
8480Sstevel@tonic-gate 	 *
8490Sstevel@tonic-gate 	 * o shadow inodes: vfs_dqrwlock is not held at all
8500Sstevel@tonic-gate 	 * o quota updates: vfs_dqrwlock is read or write held
8510Sstevel@tonic-gate 	 * o other updates: vfs_dqrwlock is read held
8520Sstevel@tonic-gate 	 *
8530Sstevel@tonic-gate 	 * The first case is the only one where we do not hold
8540Sstevel@tonic-gate 	 * vfs_dqrwlock at all while entering wrip().
8550Sstevel@tonic-gate 	 * We must make sure not to downgrade/drop vfs_dqrwlock if we
8560Sstevel@tonic-gate 	 * have it as writer, i.e. if we are updating the quota inode.
8570Sstevel@tonic-gate 	 * There is no potential deadlock scenario in this case as
8580Sstevel@tonic-gate 	 * ufs_getpage() takes care of this and avoids reacquiring
8590Sstevel@tonic-gate 	 * vfs_dqrwlock in that case.
8600Sstevel@tonic-gate 	 *
8610Sstevel@tonic-gate 	 * This check is done here since the above conditions do not change
8620Sstevel@tonic-gate 	 * and we possibly loop below, so save a few cycles.
8630Sstevel@tonic-gate 	 */
8640Sstevel@tonic-gate 	if ((type == IFSHAD) ||
8654662Sfrankho 	    (rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
8665648Ssetje 		do_dqrwlock = 0;
8670Sstevel@tonic-gate 	} else {
8680Sstevel@tonic-gate 		do_dqrwlock = 1;
8690Sstevel@tonic-gate 	}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 	/*
8720Sstevel@tonic-gate 	 * Large Files: We cast MAXBMASK to offset_t
8730Sstevel@tonic-gate 	 * inorder to mask out the higher bits. Since offset_t
8740Sstevel@tonic-gate 	 * is a signed value, the high order bit set in MAXBMASK
8750Sstevel@tonic-gate 	 * value makes it do the right thing by having all bits 1
8760Sstevel@tonic-gate 	 * in the higher word. May be removed for _SOLARIS64_.
8770Sstevel@tonic-gate 	 */
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	fs = ip->i_fs;
8800Sstevel@tonic-gate 	do {
8810Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
8820Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
8830Sstevel@tonic-gate 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
8840Sstevel@tonic-gate 		on = (int)blkoff(fs, uoff);
8850Sstevel@tonic-gate 		n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
8860Sstevel@tonic-gate 		new_iblocks = 1;
8870Sstevel@tonic-gate 
8880Sstevel@tonic-gate 		if (type == IFREG && uoff + n >= limit) {
8890Sstevel@tonic-gate 			if (uoff >= limit) {
8900Sstevel@tonic-gate 				error = EFBIG;
8910Sstevel@tonic-gate 				goto out;
8920Sstevel@tonic-gate 			}
8930Sstevel@tonic-gate 			/*
8940Sstevel@tonic-gate 			 * since uoff + n >= limit,
8950Sstevel@tonic-gate 			 * therefore n >= limit - uoff, and n is an int
8960Sstevel@tonic-gate 			 * so it is safe to cast it to an int
8970Sstevel@tonic-gate 			 */
8980Sstevel@tonic-gate 			n = (int)(limit - (rlim64_t)uoff);
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 		if (uoff + n > ip->i_size) {
9010Sstevel@tonic-gate 			/*
9020Sstevel@tonic-gate 			 * We are extending the length of the file.
9030Sstevel@tonic-gate 			 * bmap is used so that we are sure that
9040Sstevel@tonic-gate 			 * if we need to allocate new blocks, that it
9050Sstevel@tonic-gate 			 * is done here before we up the file size.
9060Sstevel@tonic-gate 			 */
9070Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (int)(on + n),
908923Ssdebnath 			    mapon == 0, NULL, cr);
9090Sstevel@tonic-gate 			/*
9100Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
9110Sstevel@tonic-gate 			 * the flags are set it changed the file.
9120Sstevel@tonic-gate 			 */
9130Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
9140Sstevel@tonic-gate 				i_seq_needed = 1;
9150Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 			if (error)
9180Sstevel@tonic-gate 				break;
9190Sstevel@tonic-gate 			/*
9200Sstevel@tonic-gate 			 * There is a window of vulnerability here.
9210Sstevel@tonic-gate 			 * The sequence of operations: allocate file
9220Sstevel@tonic-gate 			 * system blocks, uiomove the data into pages,
9230Sstevel@tonic-gate 			 * and then update the size of the file in the
9240Sstevel@tonic-gate 			 * inode, must happen atomically.  However, due
9250Sstevel@tonic-gate 			 * to current locking constraints, this can not
9260Sstevel@tonic-gate 			 * be done.
9270Sstevel@tonic-gate 			 */
9280Sstevel@tonic-gate 			ASSERT(ip->i_writer == NULL);
9290Sstevel@tonic-gate 			ip->i_writer = curthread;
9300Sstevel@tonic-gate 			i_size_changed = 1;
9310Sstevel@tonic-gate 			/*
9320Sstevel@tonic-gate 			 * If we are writing from the beginning of
9330Sstevel@tonic-gate 			 * the mapping, we can just create the
9340Sstevel@tonic-gate 			 * pages without having to read them.
9350Sstevel@tonic-gate 			 */
9360Sstevel@tonic-gate 			pagecreate = (mapon == 0);
9370Sstevel@tonic-gate 		} else if (n == MAXBSIZE) {
9380Sstevel@tonic-gate 			/*
9390Sstevel@tonic-gate 			 * Going to do a whole mappings worth,
9400Sstevel@tonic-gate 			 * so we can just create the pages w/o
9410Sstevel@tonic-gate 			 * having to read them in.  But before
9420Sstevel@tonic-gate 			 * we do that, we need to make sure any
9430Sstevel@tonic-gate 			 * needed blocks are allocated first.
9440Sstevel@tonic-gate 			 */
9450Sstevel@tonic-gate 			iblocks = ip->i_blocks;
946923Ssdebnath 			error = bmap_write(ip, uoff, (int)(on + n),
947923Ssdebnath 			    BI_ALLOC_ONLY, NULL, cr);
9480Sstevel@tonic-gate 			/*
9490Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
9500Sstevel@tonic-gate 			 * the flags are set it changed the file.
9510Sstevel@tonic-gate 			 */
9520Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
9530Sstevel@tonic-gate 				i_seq_needed = 1;
9540Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
9550Sstevel@tonic-gate 			}
9560Sstevel@tonic-gate 			if (error)
9570Sstevel@tonic-gate 				break;
9580Sstevel@tonic-gate 			pagecreate = 1;
9590Sstevel@tonic-gate 			/*
9600Sstevel@tonic-gate 			 * check if the new created page needed the
9610Sstevel@tonic-gate 			 * allocation of new disk blocks.
9620Sstevel@tonic-gate 			 */
9630Sstevel@tonic-gate 			if (iblocks == ip->i_blocks)
9640Sstevel@tonic-gate 				new_iblocks = 0; /* no new blocks allocated */
9650Sstevel@tonic-gate 		} else {
9660Sstevel@tonic-gate 			pagecreate = 0;
9670Sstevel@tonic-gate 			/*
9680Sstevel@tonic-gate 			 * In sync mode flush the indirect blocks which
9690Sstevel@tonic-gate 			 * may have been allocated and not written on
9700Sstevel@tonic-gate 			 * disk. In above cases bmap_write will allocate
9710Sstevel@tonic-gate 			 * in sync mode.
9720Sstevel@tonic-gate 			 */
9730Sstevel@tonic-gate 			if (ioflag & (FSYNC|FDSYNC)) {
9740Sstevel@tonic-gate 				error = ufs_indirblk_sync(ip, uoff);
9750Sstevel@tonic-gate 				if (error)
9760Sstevel@tonic-gate 					break;
9770Sstevel@tonic-gate 			}
9780Sstevel@tonic-gate 		}
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 		/*
9810Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one
9820Sstevel@tonic-gate 		 * of two ways:
9830Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
9840Sstevel@tonic-gate 		 *    forcefault parameter is true (pagecreate == 0)
9850Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
9860Sstevel@tonic-gate 		 *
9870Sstevel@tonic-gate 		 * We have to drop the contents lock to prevent the VM
9885331Samw 		 * system from trying to reacquire it in ufs_getpage()
9890Sstevel@tonic-gate 		 * should the uiomove cause a pagefault.
9900Sstevel@tonic-gate 		 *
9910Sstevel@tonic-gate 		 * We have to drop the reader vfs_dqrwlock here as well.
9920Sstevel@tonic-gate 		 */
9930Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
9940Sstevel@tonic-gate 		if (do_dqrwlock) {
9950Sstevel@tonic-gate 			ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
9960Sstevel@tonic-gate 			ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
9970Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
9980Sstevel@tonic-gate 		}
9990Sstevel@tonic-gate 
10001841Spraks 		newpage = 0;
10011841Spraks 		premove_resid = uio->uio_resid;
10028059SDonghai.Qiao@Sun.COM 
10038059SDonghai.Qiao@Sun.COM 		/*
10048059SDonghai.Qiao@Sun.COM 		 * Touch the page and fault it in if it is not in core
10058059SDonghai.Qiao@Sun.COM 		 * before segmap_getmapflt or vpm_data_copy can lock it.
10068059SDonghai.Qiao@Sun.COM 		 * This is to avoid the deadlock if the buffer is mapped
10078059SDonghai.Qiao@Sun.COM 		 * to the same file through mmap which we want to write.
10088059SDonghai.Qiao@Sun.COM 		 */
10098059SDonghai.Qiao@Sun.COM 		uio_prefaultpages((long)n, uio);
10108059SDonghai.Qiao@Sun.COM 
10111841Spraks 		if (vpm_enable) {
10121841Spraks 			/*
10131841Spraks 			 * Copy data. If new pages are created, part of
10141841Spraks 			 * the page that is not written will be initizliazed
10151841Spraks 			 * with zeros.
10161841Spraks 			 */
10171841Spraks 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
10184662Sfrankho 			    uio, !pagecreate, &newpage, 0, S_WRITE);
10191841Spraks 		} else {
10201841Spraks 
10211841Spraks 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
10224662Sfrankho 			    (uint_t)n, !pagecreate, S_WRITE);
10230Sstevel@tonic-gate 
10241841Spraks 			/*
10251841Spraks 			 * segmap_pagecreate() returns 1 if it calls
10261841Spraks 			 * page_create_va() to allocate any pages.
10271841Spraks 			 */
10281841Spraks 
10291841Spraks 			if (pagecreate)
10301841Spraks 				newpage = segmap_pagecreate(segkmap, base,
10311841Spraks 				    (size_t)n, 0);
10321841Spraks 
10331841Spraks 			error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
10341841Spraks 		}
10350Sstevel@tonic-gate 
10360Sstevel@tonic-gate 		/*
10370Sstevel@tonic-gate 		 * If "newpage" is set, then a new page was created and it
10380Sstevel@tonic-gate 		 * does not contain valid data, so it needs to be initialized
10390Sstevel@tonic-gate 		 * at this point.
10400Sstevel@tonic-gate 		 * Otherwise the page contains old data, which was overwritten
10410Sstevel@tonic-gate 		 * partially or as a whole in uiomove.
10420Sstevel@tonic-gate 		 * If there is only one iovec structure within uio, then
10430Sstevel@tonic-gate 		 * on error uiomove will not be able to update uio->uio_loffset
10440Sstevel@tonic-gate 		 * and we would zero the whole page here!
10450Sstevel@tonic-gate 		 *
10460Sstevel@tonic-gate 		 * If uiomove fails because of an error, the old valid data
10470Sstevel@tonic-gate 		 * is kept instead of filling the rest of the page with zero's.
10480Sstevel@tonic-gate 		 */
10491841Spraks 		if (!vpm_enable && newpage &&
10500Sstevel@tonic-gate 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
10510Sstevel@tonic-gate 			/*
10520Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
10530Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
10540Sstevel@tonic-gate 			 * This happens on most EOF write cases and if
10550Sstevel@tonic-gate 			 * we had some sort of error during the uiomove.
10560Sstevel@tonic-gate 			 */
10570Sstevel@tonic-gate 			int nzero, nmoved;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 			nmoved = (int)(uio->uio_loffset - (off + mapon));
10600Sstevel@tonic-gate 			ASSERT(nmoved >= 0 && nmoved <= n);
10610Sstevel@tonic-gate 			nzero = roundup(on + n, PAGESIZE) - nmoved;
10620Sstevel@tonic-gate 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
10630Sstevel@tonic-gate 			(void) kzero(base + mapon + nmoved, (uint_t)nzero);
10640Sstevel@tonic-gate 		}
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 		/*
10670Sstevel@tonic-gate 		 * Unlock the pages allocated by page_create_va()
10680Sstevel@tonic-gate 		 * in segmap_pagecreate()
10690Sstevel@tonic-gate 		 */
10701841Spraks 		if (!vpm_enable && newpage)
10710Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 		/*
10740Sstevel@tonic-gate 		 * If the size of the file changed, then update the
10750Sstevel@tonic-gate 		 * size field in the inode now.  This can't be done
10760Sstevel@tonic-gate 		 * before the call to segmap_pageunlock or there is
10770Sstevel@tonic-gate 		 * a potential deadlock with callers to ufs_putpage().
10780Sstevel@tonic-gate 		 * They will be holding i_contents and trying to lock
10790Sstevel@tonic-gate 		 * a page, while this thread is holding a page locked
10800Sstevel@tonic-gate 		 * and trying to acquire i_contents.
10810Sstevel@tonic-gate 		 */
10820Sstevel@tonic-gate 		if (i_size_changed) {
10830Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
10840Sstevel@tonic-gate 			old_i_size = ip->i_size;
10850Sstevel@tonic-gate 			UFS_SET_ISIZE(uoff + n, ip);
10860Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
10870Sstevel@tonic-gate 			/*
10880Sstevel@tonic-gate 			 * file has grown larger than 2GB. Set flag
10890Sstevel@tonic-gate 			 * in superblock to indicate this, if it
10900Sstevel@tonic-gate 			 * is not already set.
10910Sstevel@tonic-gate 			 */
10920Sstevel@tonic-gate 			if ((ip->i_size > MAXOFF32_T) &&
10930Sstevel@tonic-gate 			    !(fs->fs_flags & FSLARGEFILES)) {
10940Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
10950Sstevel@tonic-gate 				mutex_enter(&ufsvfsp->vfs_lock);
10960Sstevel@tonic-gate 				fs->fs_flags |= FSLARGEFILES;
10970Sstevel@tonic-gate 				ufs_sbwrite(ufsvfsp);
10980Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
10990Sstevel@tonic-gate 			}
11000Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
11010Sstevel@tonic-gate 			ip->i_writer = NULL;
11020Sstevel@tonic-gate 			cv_broadcast(&ip->i_wrcv);
11030Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
11040Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
11050Sstevel@tonic-gate 		}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 		if (error) {
11080Sstevel@tonic-gate 			/*
11090Sstevel@tonic-gate 			 * If we failed on a write, we may have already
11100Sstevel@tonic-gate 			 * allocated file blocks as well as pages.  It's
11110Sstevel@tonic-gate 			 * hard to undo the block allocation, but we must
11120Sstevel@tonic-gate 			 * be sure to invalidate any pages that may have
11130Sstevel@tonic-gate 			 * been allocated.
11140Sstevel@tonic-gate 			 *
11150Sstevel@tonic-gate 			 * If the page was created without initialization
11160Sstevel@tonic-gate 			 * then we must check if it should be possible
11170Sstevel@tonic-gate 			 * to destroy the new page and to keep the old data
11180Sstevel@tonic-gate 			 * on the disk.
11190Sstevel@tonic-gate 			 *
11200Sstevel@tonic-gate 			 * It is possible to destroy the page without
11210Sstevel@tonic-gate 			 * having to write back its contents only when
11220Sstevel@tonic-gate 			 * - the size of the file keeps unchanged
11230Sstevel@tonic-gate 			 * - bmap_write() did not allocate new disk blocks
11240Sstevel@tonic-gate 			 *   it is possible to create big files using "seek" and
11250Sstevel@tonic-gate 			 *   write to the end of the file. A "write" to a
11260Sstevel@tonic-gate 			 *   position before the end of the file would not
11270Sstevel@tonic-gate 			 *   change the size of the file but it would allocate
11280Sstevel@tonic-gate 			 *   new disk blocks.
11290Sstevel@tonic-gate 			 * - uiomove intended to overwrite the whole page.
11300Sstevel@tonic-gate 			 * - a new page was created (newpage == 1).
11310Sstevel@tonic-gate 			 */
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 			if (i_size_changed == 0 && new_iblocks == 0 &&
11340Sstevel@tonic-gate 			    newpage) {
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 				/* unwind what uiomove eventually last did */
11370Sstevel@tonic-gate 				uio->uio_resid = premove_resid;
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 				/*
11400Sstevel@tonic-gate 				 * destroy the page, do not write ambiguous
11410Sstevel@tonic-gate 				 * data to the disk.
11420Sstevel@tonic-gate 				 */
11430Sstevel@tonic-gate 				flags = SM_DESTROY;
11440Sstevel@tonic-gate 			} else {
11450Sstevel@tonic-gate 				/*
11460Sstevel@tonic-gate 				 * write the page back to the disk, if dirty,
11470Sstevel@tonic-gate 				 * and remove the page from the cache.
11480Sstevel@tonic-gate 				 */
11490Sstevel@tonic-gate 				flags = SM_INVAL;
11500Sstevel@tonic-gate 			}
11511841Spraks 
11521841Spraks 			if (vpm_enable) {
11531841Spraks 				/*
11541841Spraks 				 *  Flush pages.
11551841Spraks 				 */
11561841Spraks 				(void) vpm_sync_pages(vp, off, n, flags);
11571841Spraks 			} else {
11581841Spraks 				(void) segmap_release(segkmap, base, flags);
11591841Spraks 			}
11600Sstevel@tonic-gate 		} else {
11610Sstevel@tonic-gate 			flags = 0;
11620Sstevel@tonic-gate 			/*
11630Sstevel@tonic-gate 			 * Force write back for synchronous write cases.
11640Sstevel@tonic-gate 			 */
11650Sstevel@tonic-gate 			if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
11660Sstevel@tonic-gate 				/*
11670Sstevel@tonic-gate 				 * If the sticky bit is set but the
11680Sstevel@tonic-gate 				 * execute bit is not set, we do a
11690Sstevel@tonic-gate 				 * synchronous write back and free
11700Sstevel@tonic-gate 				 * the page when done.  We set up swap
11710Sstevel@tonic-gate 				 * files to be handled this way to
11720Sstevel@tonic-gate 				 * prevent servers from keeping around
11730Sstevel@tonic-gate 				 * the client's swap pages too long.
11740Sstevel@tonic-gate 				 * XXX - there ought to be a better way.
11750Sstevel@tonic-gate 				 */
11760Sstevel@tonic-gate 				if (IS_SWAPVP(vp)) {
11770Sstevel@tonic-gate 					flags = SM_WRITE | SM_FREE |
11780Sstevel@tonic-gate 					    SM_DONTNEED;
11790Sstevel@tonic-gate 					iupdat_flag = 0;
11800Sstevel@tonic-gate 				} else {
11810Sstevel@tonic-gate 					flags = SM_WRITE;
11820Sstevel@tonic-gate 				}
11830Sstevel@tonic-gate 			} else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
11840Sstevel@tonic-gate 				/*
11850Sstevel@tonic-gate 				 * Have written a whole block.
11860Sstevel@tonic-gate 				 * Start an asynchronous write and
11870Sstevel@tonic-gate 				 * mark the buffer to indicate that
11880Sstevel@tonic-gate 				 * it won't be needed again soon.
11890Sstevel@tonic-gate 				 */
11900Sstevel@tonic-gate 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
11910Sstevel@tonic-gate 			}
11921841Spraks 			if (vpm_enable) {
11931841Spraks 				/*
11941841Spraks 				 * Flush pages.
11951841Spraks 				 */
11962353Spraks 				error = vpm_sync_pages(vp, off, n, flags);
11971841Spraks 			} else {
11982353Spraks 				error = segmap_release(segkmap, base, flags);
11991841Spraks 			}
12000Sstevel@tonic-gate 			/*
12010Sstevel@tonic-gate 			 * If the operation failed and is synchronous,
12020Sstevel@tonic-gate 			 * then we need to unwind what uiomove() last
12030Sstevel@tonic-gate 			 * did so we can potentially return an error to
12040Sstevel@tonic-gate 			 * the caller.  If this write operation was
12050Sstevel@tonic-gate 			 * done in two pieces and the first succeeded,
12060Sstevel@tonic-gate 			 * then we won't return an error for the second
12070Sstevel@tonic-gate 			 * piece that failed.  However, we only want to
12080Sstevel@tonic-gate 			 * return a resid value that reflects what was
12090Sstevel@tonic-gate 			 * really done.
12100Sstevel@tonic-gate 			 *
12110Sstevel@tonic-gate 			 * Failures for non-synchronous operations can
12120Sstevel@tonic-gate 			 * be ignored since the page subsystem will
12130Sstevel@tonic-gate 			 * retry the operation until it succeeds or the
12140Sstevel@tonic-gate 			 * file system is unmounted.
12150Sstevel@tonic-gate 			 */
12160Sstevel@tonic-gate 			if (error) {
12170Sstevel@tonic-gate 				if ((ioflag & (FSYNC | FDSYNC)) ||
12180Sstevel@tonic-gate 				    type == IFDIR) {
12190Sstevel@tonic-gate 					uio->uio_resid = premove_resid;
12200Sstevel@tonic-gate 				} else {
12210Sstevel@tonic-gate 					error = 0;
12220Sstevel@tonic-gate 				}
12230Sstevel@tonic-gate 			}
12240Sstevel@tonic-gate 		}
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 		/*
12270Sstevel@tonic-gate 		 * Re-acquire contents lock.
12280Sstevel@tonic-gate 		 * If it was dropped, reacquire reader vfs_dqrwlock as well.
12290Sstevel@tonic-gate 		 */
12300Sstevel@tonic-gate 		if (do_dqrwlock)
12310Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
12320Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 		/*
12350Sstevel@tonic-gate 		 * If the uiomove() failed or if a synchronous
12360Sstevel@tonic-gate 		 * page push failed, fix up i_size.
12370Sstevel@tonic-gate 		 */
12380Sstevel@tonic-gate 		if (error) {
12390Sstevel@tonic-gate 			if (i_size_changed) {
12400Sstevel@tonic-gate 				/*
12410Sstevel@tonic-gate 				 * The uiomove failed, and we
12420Sstevel@tonic-gate 				 * allocated blocks,so get rid
12430Sstevel@tonic-gate 				 * of them.
12440Sstevel@tonic-gate 				 */
12450Sstevel@tonic-gate 				(void) ufs_itrunc(ip, old_i_size, 0, cr);
12460Sstevel@tonic-gate 			}
12470Sstevel@tonic-gate 		} else {
12480Sstevel@tonic-gate 			/*
12490Sstevel@tonic-gate 			 * XXX - Can this be out of the loop?
12500Sstevel@tonic-gate 			 */
12510Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
12520Sstevel@tonic-gate 			/*
12530Sstevel@tonic-gate 			 * Only do one increase of i_seq for multiple
12540Sstevel@tonic-gate 			 * pieces.  Because we drop locks, record
12550Sstevel@tonic-gate 			 * the fact that we changed the timestamp and
12560Sstevel@tonic-gate 			 * are deferring the increase in case another thread
12570Sstevel@tonic-gate 			 * pushes our timestamp update.
12580Sstevel@tonic-gate 			 */
12590Sstevel@tonic-gate 			i_seq_needed = 1;
12600Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
12610Sstevel@tonic-gate 			if (i_size_changed)
12620Sstevel@tonic-gate 				ip->i_flag |= IATTCHG;
12630Sstevel@tonic-gate 			if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
12640Sstevel@tonic-gate 			    (IEXEC >> 6))) != 0 &&
12650Sstevel@tonic-gate 			    (ip->i_mode & (ISUID | ISGID)) != 0 &&
12660Sstevel@tonic-gate 			    secpolicy_vnode_setid_retain(cr,
12670Sstevel@tonic-gate 			    (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
12680Sstevel@tonic-gate 				/*
12690Sstevel@tonic-gate 				 * Clear Set-UID & Set-GID bits on
12700Sstevel@tonic-gate 				 * successful write if not privileged
12710Sstevel@tonic-gate 				 * and at least one of the execute bits
12720Sstevel@tonic-gate 				 * is set.  If we always clear Set-GID,
12730Sstevel@tonic-gate 				 * mandatory file and record locking is
12740Sstevel@tonic-gate 				 * unuseable.
12750Sstevel@tonic-gate 				 */
12760Sstevel@tonic-gate 				ip->i_mode &= ~(ISUID | ISGID);
12770Sstevel@tonic-gate 			}
12780Sstevel@tonic-gate 		}
1279612Sswilcox 		/*
1280612Sswilcox 		 * In the case the FDSYNC flag is set and this is a
1281612Sswilcox 		 * "rewrite" we won't log a delta.
1282612Sswilcox 		 * The FSYNC flag overrides all cases.
1283612Sswilcox 		 */
1284612Sswilcox 		if (!ufs_check_rewrite(ip, uio, ioflag) || !(ioflag & FDSYNC)) {
1285612Sswilcox 			TRANS_INODE(ufsvfsp, ip);
1286612Sswilcox 		}
12870Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate out:
12900Sstevel@tonic-gate 	/*
12910Sstevel@tonic-gate 	 * Make sure i_seq is increased at least once per write
12920Sstevel@tonic-gate 	 */
12930Sstevel@tonic-gate 	if (i_seq_needed) {
12940Sstevel@tonic-gate 		ip->i_seq++;
12950Sstevel@tonic-gate 		ip->i_flag &= ~ISEQ;	/* no longer deferred */
12960Sstevel@tonic-gate 	}
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate 	/*
12990Sstevel@tonic-gate 	 * Inode is updated according to this table -
13000Sstevel@tonic-gate 	 *
13010Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
13020Sstevel@tonic-gate 	 *   --------------------------
13030Sstevel@tonic-gate 	 *   always@	  IATTCHG|IBDWRITE
13040Sstevel@tonic-gate 	 *
13050Sstevel@tonic-gate 	 * @ - 	If we are doing synchronous write the only time we should
13060Sstevel@tonic-gate 	 *	not be sync'ing the ip here is if we have the stickyhack
13070Sstevel@tonic-gate 	 *	activated, the file is marked with the sticky bit and
13080Sstevel@tonic-gate 	 *	no exec bit, the file length has not been changed and
13090Sstevel@tonic-gate 	 *	no new blocks have been allocated during this write.
13100Sstevel@tonic-gate 	 */
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	if ((ip->i_flag & ISYNC) != 0) {
13130Sstevel@tonic-gate 		/*
13140Sstevel@tonic-gate 		 * we have eliminated nosync
13150Sstevel@tonic-gate 		 */
13160Sstevel@tonic-gate 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
13174662Sfrankho 		    ((ioflag & FSYNC) && iupdat_flag)) {
13180Sstevel@tonic-gate 			ufs_iupdat(ip, 1);
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 	}
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	/*
13230Sstevel@tonic-gate 	 * If we've already done a partial-write, terminate
13240Sstevel@tonic-gate 	 * the write but return no error unless the error is ENOSPC
13250Sstevel@tonic-gate 	 * because the caller can detect this and free resources and
13260Sstevel@tonic-gate 	 * try again.
13270Sstevel@tonic-gate 	 */
13280Sstevel@tonic-gate 	if ((start_resid != uio->uio_resid) && (error != ENOSPC))
13290Sstevel@tonic-gate 		error = 0;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	ip->i_flag &= ~(INOACC | ISYNC);
13320Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);
13330Sstevel@tonic-gate 	return (error);
13340Sstevel@tonic-gate }
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate /*
13370Sstevel@tonic-gate  * rdip does the real work of read requests for ufs.
13380Sstevel@tonic-gate  */
13390Sstevel@tonic-gate int
rdip(struct inode * ip,struct uio * uio,int ioflag,cred_t * cr)13400Sstevel@tonic-gate rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
13410Sstevel@tonic-gate {
13420Sstevel@tonic-gate 	u_offset_t off;
13430Sstevel@tonic-gate 	caddr_t base;
13440Sstevel@tonic-gate 	struct fs *fs;
13450Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
13460Sstevel@tonic-gate 	struct vnode *vp;
13470Sstevel@tonic-gate 	long oresid = uio->uio_resid;
13480Sstevel@tonic-gate 	u_offset_t n, on, mapon;
13490Sstevel@tonic-gate 	int error = 0;
13500Sstevel@tonic-gate 	int doupdate = 1;
1351543Srbourbon 	uint_t flags;
13520Sstevel@tonic-gate 	int dofree, directio_status;
13530Sstevel@tonic-gate 	krw_t rwtype;
13540Sstevel@tonic-gate 	o_mode_t type;
135511066Srafael.vanoni@sun.com 	clock_t	now;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 	vp = ITOV(ip);
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	if (ufsvfsp == NULL)
13640Sstevel@tonic-gate 		return (EIO);
13650Sstevel@tonic-gate 
13660Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	/* check for valid filetype */
13690Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
13700Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
13710Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
13720Sstevel@tonic-gate 		return (EIO);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	if (uio->uio_loffset > UFS_MAXOFFSET_T) {
13760Sstevel@tonic-gate 		error = 0;
13770Sstevel@tonic-gate 		goto out;
13780Sstevel@tonic-gate 	}
13790Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
13800Sstevel@tonic-gate 		return (EINVAL);
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
13830Sstevel@tonic-gate 		return (0);
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
13874662Sfrankho 	    (!ufsvfsp->vfs_noatime)) {
13880Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
13890Sstevel@tonic-gate 		ip->i_flag |= IACC;
13900Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 	/*
13930Sstevel@tonic-gate 	 * Try to go direct
13940Sstevel@tonic-gate 	 */
13950Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
13960Sstevel@tonic-gate 		error = ufs_directio_read(ip, uio, cr, &directio_status);
13970Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
13980Sstevel@tonic-gate 			goto out;
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	do {
14040Sstevel@tonic-gate 		offset_t diff;
14050Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
14060Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
14070Sstevel@tonic-gate 		mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
14080Sstevel@tonic-gate 		on = (u_offset_t)blkoff(fs, uoff);
14090Sstevel@tonic-gate 		n = MIN((u_offset_t)fs->fs_bsize - on,
14104662Sfrankho 		    (u_offset_t)uio->uio_resid);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		diff = ip->i_size - uoff;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 		if (diff <= (offset_t)0) {
14150Sstevel@tonic-gate 			error = 0;
14160Sstevel@tonic-gate 			goto out;
14170Sstevel@tonic-gate 		}
14180Sstevel@tonic-gate 		if (diff < (offset_t)n)
14190Sstevel@tonic-gate 			n = (int)diff;
1420543Srbourbon 
1421543Srbourbon 		/*
1422543Srbourbon 		 * We update smallfile2 and smallfile1 at most every second.
1423543Srbourbon 		 */
142411066Srafael.vanoni@sun.com 		now = ddi_get_lbolt();
142511066Srafael.vanoni@sun.com 		if (now >= smallfile_update) {
1426543Srbourbon 			uint64_t percpufreeb;
1427543Srbourbon 			if (smallfile1_d == 0) smallfile1_d = SMALLFILE1_D;
1428543Srbourbon 			if (smallfile2_d == 0) smallfile2_d = SMALLFILE2_D;
1429543Srbourbon 			percpufreeb = ptob((uint64_t)freemem) / ncpus_online;
1430543Srbourbon 			smallfile1 = percpufreeb / smallfile1_d;
1431543Srbourbon 			smallfile2 = percpufreeb / smallfile2_d;
1432543Srbourbon 			smallfile1 = MAX(smallfile1, smallfile);
1433543Srbourbon 			smallfile1 = MAX(smallfile1, smallfile64);
1434543Srbourbon 			smallfile2 = MAX(smallfile1, smallfile2);
143511066Srafael.vanoni@sun.com 			smallfile_update = now + hz;
1436543Srbourbon 		}
1437543Srbourbon 
14380Sstevel@tonic-gate 		dofree = freebehind &&
1439543Srbourbon 		    ip->i_nextr == (off & PAGEMASK) && off > smallfile1;
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate 		/*
14420Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one of two
14430Sstevel@tonic-gate 		 * ways:
14440Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
14450Sstevel@tonic-gate 		 *    forcefault parameter is true (value of 1 is passed)
14460Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
14470Sstevel@tonic-gate 		 *
14480Sstevel@tonic-gate 		 * We cannot hold onto an i_contents reader lock without
14490Sstevel@tonic-gate 		 * risking deadlock in ufs_getpage() so drop a reader lock.
14500Sstevel@tonic-gate 		 * The ufs_getpage() dolock logic already allows for a
14510Sstevel@tonic-gate 		 * thread holding i_contents as writer to work properly
14520Sstevel@tonic-gate 		 * so we keep a writer lock.
14530Sstevel@tonic-gate 		 */
14540Sstevel@tonic-gate 		if (rwtype == RW_READER)
14550Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
14561841Spraks 
14571841Spraks 		if (vpm_enable) {
14581841Spraks 			/*
14591841Spraks 			 * Copy data.
14601841Spraks 			 */
14611841Spraks 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
14624662Sfrankho 			    uio, 1, NULL, 0, S_READ);
14631841Spraks 		} else {
14641841Spraks 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
14654662Sfrankho 			    (uint_t)n, 1, S_READ);
14661841Spraks 			error = uiomove(base + mapon, (long)n, UIO_READ, uio);
14671841Spraks 		}
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 		flags = 0;
14700Sstevel@tonic-gate 		if (!error) {
14710Sstevel@tonic-gate 			/*
1472543Srbourbon 			 * If  reading sequential  we won't need  this
1473543Srbourbon 			 * buffer again  soon.  For  offsets in  range
1474543Srbourbon 			 * [smallfile1,  smallfile2] release the pages
1475543Srbourbon 			 * at   the  tail  of the   cache list, larger
1476543Srbourbon 			 * offsets are released at the head.
14770Sstevel@tonic-gate 			 */
14780Sstevel@tonic-gate 			if (dofree) {
1479543Srbourbon 				flags = SM_FREE | SM_ASYNC;
1480543Srbourbon 				if ((cache_read_ahead == 0) &&
14814662Sfrankho 				    (off > smallfile2))
1482543Srbourbon 					flags |=  SM_DONTNEED;
14830Sstevel@tonic-gate 			}
14840Sstevel@tonic-gate 			/*
14850Sstevel@tonic-gate 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
14860Sstevel@tonic-gate 			 * we want to make sure that the page which has
14870Sstevel@tonic-gate 			 * been read, is written on disk if it is dirty.
14880Sstevel@tonic-gate 			 * And corresponding indirect blocks should also
14890Sstevel@tonic-gate 			 * be flushed out.
14900Sstevel@tonic-gate 			 */
14910Sstevel@tonic-gate 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
14920Sstevel@tonic-gate 				flags &= ~SM_ASYNC;
14930Sstevel@tonic-gate 				flags |= SM_WRITE;
14940Sstevel@tonic-gate 			}
14951841Spraks 			if (vpm_enable) {
14961841Spraks 				error = vpm_sync_pages(vp, off, n, flags);
14971841Spraks 			} else {
14981841Spraks 				error = segmap_release(segkmap, base, flags);
14991841Spraks 			}
15001841Spraks 		} else {
15011841Spraks 			if (vpm_enable) {
15021841Spraks 				(void) vpm_sync_pages(vp, off, n, flags);
15031841Spraks 			} else {
15041841Spraks 				(void) segmap_release(segkmap, base, flags);
15051841Spraks 			}
15061841Spraks 		}
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 		if (rwtype == RW_READER)
15090Sstevel@tonic-gate 			rw_enter(&ip->i_contents, rwtype);
15100Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
15110Sstevel@tonic-gate out:
15120Sstevel@tonic-gate 	/*
15130Sstevel@tonic-gate 	 * Inode is updated according to this table if FRSYNC is set.
15140Sstevel@tonic-gate 	 *
15150Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
15160Sstevel@tonic-gate 	 *   --------------------------
15170Sstevel@tonic-gate 	 *   always	  IATTCHG|IBDWRITE
15180Sstevel@tonic-gate 	 */
15190Sstevel@tonic-gate 	/*
15200Sstevel@tonic-gate 	 * The inode is not updated if we're logging and the inode is a
15210Sstevel@tonic-gate 	 * directory with FRSYNC, FSYNC and FDSYNC flags set.
15220Sstevel@tonic-gate 	 */
15230Sstevel@tonic-gate 	if (ioflag & FRSYNC) {
15240Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
15255648Ssetje 			doupdate = 0;
15260Sstevel@tonic-gate 		}
15270Sstevel@tonic-gate 		if (doupdate) {
15280Sstevel@tonic-gate 			if ((ioflag & FSYNC) ||
15290Sstevel@tonic-gate 			    ((ioflag & FDSYNC) &&
15300Sstevel@tonic-gate 			    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
15310Sstevel@tonic-gate 				ufs_iupdat(ip, 1);
15320Sstevel@tonic-gate 			}
15330Sstevel@tonic-gate 		}
15340Sstevel@tonic-gate 	}
15350Sstevel@tonic-gate 	/*
15360Sstevel@tonic-gate 	 * If we've already done a partial read, terminate
15370Sstevel@tonic-gate 	 * the read but return no error.
15380Sstevel@tonic-gate 	 */
15390Sstevel@tonic-gate 	if (oresid != uio->uio_resid)
15400Sstevel@tonic-gate 		error = 0;
15410Sstevel@tonic-gate 	ITIMES(ip);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	return (error);
15440Sstevel@tonic-gate }
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate /* ARGSUSED */
15470Sstevel@tonic-gate static int
ufs_ioctl(struct vnode * vp,int cmd,intptr_t arg,int flag,struct cred * cr,int * rvalp,caller_context_t * ct)15480Sstevel@tonic-gate ufs_ioctl(
15490Sstevel@tonic-gate 	struct vnode	*vp,
15500Sstevel@tonic-gate 	int		cmd,
15510Sstevel@tonic-gate 	intptr_t	arg,
15520Sstevel@tonic-gate 	int		flag,
15530Sstevel@tonic-gate 	struct cred	*cr,
15545331Samw 	int		*rvalp,
15555331Samw 	caller_context_t *ct)
15560Sstevel@tonic-gate {
15570Sstevel@tonic-gate 	struct lockfs	lockfs, lockfs_out;
15580Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
15590Sstevel@tonic-gate 	char		*comment, *original_comment;
15600Sstevel@tonic-gate 	struct fs	*fs;
15610Sstevel@tonic-gate 	struct ulockfs	*ulp;
15620Sstevel@tonic-gate 	offset_t	off;
15630Sstevel@tonic-gate 	extern int	maxphys;
15640Sstevel@tonic-gate 	int		error;
15650Sstevel@tonic-gate 	int		issync;
15660Sstevel@tonic-gate 	int		trans_size;
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 	/*
15700Sstevel@tonic-gate 	 * forcibly unmounted
15710Sstevel@tonic-gate 	 */
15727276Sjr26306 	if (ufsvfsp == NULL || vp->v_vfsp == NULL ||
15737276Sjr26306 	    vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
15740Sstevel@tonic-gate 		return (EIO);
15750Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	if (cmd == Q_QUOTACTL) {
15780Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
15790Sstevel@tonic-gate 		if (error)
15800Sstevel@tonic-gate 			return (error);
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		if (ulp) {
15830Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
15844662Sfrankho 			    TOP_SETQUOTA_SIZE(fs));
15850Sstevel@tonic-gate 		}
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 		error = quotactl(vp, arg, flag, cr);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		if (ulp) {
15900Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
15914662Sfrankho 			    TOP_SETQUOTA_SIZE(fs));
15920Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
15930Sstevel@tonic-gate 		}
15940Sstevel@tonic-gate 		return (error);
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	switch (cmd) {
15980Sstevel@tonic-gate 		case _FIOLFS:
15990Sstevel@tonic-gate 			/*
16000Sstevel@tonic-gate 			 * file system locking
16010Sstevel@tonic-gate 			 */
16020Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
16030Sstevel@tonic-gate 				return (EPERM);
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
16060Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
16074662Sfrankho 				    sizeof (struct lockfs)))
16080Sstevel@tonic-gate 					return (EFAULT);
16090Sstevel@tonic-gate 			}
16100Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16110Sstevel@tonic-gate 			else {
16120Sstevel@tonic-gate 				struct lockfs32	lockfs32;
16130Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
16140Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
16150Sstevel@tonic-gate 				    sizeof (struct lockfs32)))
16160Sstevel@tonic-gate 					return (EFAULT);
16170Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
16180Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
16190Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
16200Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
16210Sstevel@tonic-gate 				lockfs.lf_comment =
16224662Sfrankho 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
16230Sstevel@tonic-gate 			}
16240Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 			if (lockfs.lf_comlen) {
16270Sstevel@tonic-gate 				if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
16280Sstevel@tonic-gate 					return (ENAMETOOLONG);
16294662Sfrankho 				comment =
16304662Sfrankho 				    kmem_alloc(lockfs.lf_comlen, KM_SLEEP);
16310Sstevel@tonic-gate 				if (copyin(lockfs.lf_comment, comment,
16324662Sfrankho 				    lockfs.lf_comlen)) {
16330Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
16340Sstevel@tonic-gate 					return (EFAULT);
16350Sstevel@tonic-gate 				}
16360Sstevel@tonic-gate 				original_comment = lockfs.lf_comment;
16370Sstevel@tonic-gate 				lockfs.lf_comment = comment;
16380Sstevel@tonic-gate 			}
16390Sstevel@tonic-gate 			if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
16400Sstevel@tonic-gate 				lockfs.lf_comment = original_comment;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 				if ((flag & DATAMODEL_MASK) ==
16430Sstevel@tonic-gate 				    DATAMODEL_NATIVE) {
16440Sstevel@tonic-gate 					(void) copyout(&lockfs, (caddr_t)arg,
16450Sstevel@tonic-gate 					    sizeof (struct lockfs));
16460Sstevel@tonic-gate 				}
16470Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16480Sstevel@tonic-gate 				else {
16490Sstevel@tonic-gate 					struct lockfs32	lockfs32;
16500Sstevel@tonic-gate 					/* Translate LP64 to ILP32 lockfs */
16510Sstevel@tonic-gate 					lockfs32.lf_lock =
16520Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_lock;
16530Sstevel@tonic-gate 					lockfs32.lf_flags =
16540Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_flags;
16550Sstevel@tonic-gate 					lockfs32.lf_key =
16560Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_key;
16570Sstevel@tonic-gate 					lockfs32.lf_comlen =
16580Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_comlen;
16590Sstevel@tonic-gate 					lockfs32.lf_comment =
16604662Sfrankho 					    (uint32_t)(uintptr_t)
16614662Sfrankho 					    lockfs.lf_comment;
16620Sstevel@tonic-gate 					(void) copyout(&lockfs32, (caddr_t)arg,
16630Sstevel@tonic-gate 					    sizeof (struct lockfs32));
16640Sstevel@tonic-gate 				}
16650Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 			} else {
16680Sstevel@tonic-gate 				if (lockfs.lf_comlen)
16690Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
16700Sstevel@tonic-gate 			}
16710Sstevel@tonic-gate 			return (error);
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 		case _FIOLFSS:
16740Sstevel@tonic-gate 			/*
16750Sstevel@tonic-gate 			 * get file system locking status
16760Sstevel@tonic-gate 			 */
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
16790Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
16804662Sfrankho 				    sizeof (struct lockfs)))
16810Sstevel@tonic-gate 					return (EFAULT);
16820Sstevel@tonic-gate 			}
16830Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16840Sstevel@tonic-gate 			else {
16850Sstevel@tonic-gate 				struct lockfs32	lockfs32;
16860Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
16870Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
16884662Sfrankho 				    sizeof (struct lockfs32)))
16890Sstevel@tonic-gate 					return (EFAULT);
16900Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
16910Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
16920Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
16930Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
16940Sstevel@tonic-gate 				lockfs.lf_comment =
16954662Sfrankho 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
16960Sstevel@tonic-gate 			}
16970Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 			if (error =  ufs_fiolfss(vp, &lockfs_out))
17000Sstevel@tonic-gate 				return (error);
17010Sstevel@tonic-gate 			lockfs.lf_lock = lockfs_out.lf_lock;
17020Sstevel@tonic-gate 			lockfs.lf_key = lockfs_out.lf_key;
17030Sstevel@tonic-gate 			lockfs.lf_flags = lockfs_out.lf_flags;
17040Sstevel@tonic-gate 			lockfs.lf_comlen = MIN(lockfs.lf_comlen,
17054662Sfrankho 			    lockfs_out.lf_comlen);
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
17080Sstevel@tonic-gate 				if (copyout(&lockfs, (caddr_t)arg,
17094662Sfrankho 				    sizeof (struct lockfs)))
17100Sstevel@tonic-gate 					return (EFAULT);
17110Sstevel@tonic-gate 			}
17120Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
17130Sstevel@tonic-gate 			else {
17140Sstevel@tonic-gate 				/* Translate LP64 to ILP32 lockfs */
17150Sstevel@tonic-gate 				struct lockfs32	lockfs32;
17160Sstevel@tonic-gate 				lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
17170Sstevel@tonic-gate 				lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
17180Sstevel@tonic-gate 				lockfs32.lf_key = (uint32_t)lockfs.lf_key;
17190Sstevel@tonic-gate 				lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
17200Sstevel@tonic-gate 				lockfs32.lf_comment =
17214662Sfrankho 				    (uint32_t)(uintptr_t)lockfs.lf_comment;
17220Sstevel@tonic-gate 				if (copyout(&lockfs32, (caddr_t)arg,
17234662Sfrankho 				    sizeof (struct lockfs32)))
17240Sstevel@tonic-gate 					return (EFAULT);
17250Sstevel@tonic-gate 			}
17260Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 			if (lockfs.lf_comlen &&
17290Sstevel@tonic-gate 			    lockfs.lf_comment && lockfs_out.lf_comment)
17300Sstevel@tonic-gate 				if (copyout(lockfs_out.lf_comment,
17314662Sfrankho 				    lockfs.lf_comment, lockfs.lf_comlen))
17320Sstevel@tonic-gate 					return (EFAULT);
17330Sstevel@tonic-gate 			return (0);
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate 		case _FIOSATIME:
17360Sstevel@tonic-gate 			/*
17370Sstevel@tonic-gate 			 * set access time
17380Sstevel@tonic-gate 			 */
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 			/*
17410Sstevel@tonic-gate 			 * if mounted w/o atime, return quietly.
17420Sstevel@tonic-gate 			 * I briefly thought about returning ENOSYS, but
17430Sstevel@tonic-gate 			 * figured that most apps would consider this fatal
17440Sstevel@tonic-gate 			 * but the idea is to make this as seamless as poss.
17450Sstevel@tonic-gate 			 */
17460Sstevel@tonic-gate 			if (ufsvfsp->vfs_noatime)
17470Sstevel@tonic-gate 				return (0);
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
17504662Sfrankho 			    ULOCKFS_SETATTR_MASK);
17510Sstevel@tonic-gate 			if (error)
17520Sstevel@tonic-gate 				return (error);
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate 			if (ulp) {
17550Sstevel@tonic-gate 				trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
17560Sstevel@tonic-gate 				TRANS_BEGIN_CSYNC(ufsvfsp, issync,
17574662Sfrankho 				    TOP_SETATTR, trans_size);
17580Sstevel@tonic-gate 			}
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 			error = ufs_fiosatime(vp, (struct timeval *)arg,
17614662Sfrankho 			    flag, cr);
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 			if (ulp) {
17640Sstevel@tonic-gate 				TRANS_END_CSYNC(ufsvfsp, error, issync,
17654662Sfrankho 				    TOP_SETATTR, trans_size);
17660Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
17670Sstevel@tonic-gate 			}
17680Sstevel@tonic-gate 			return (error);
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 		case _FIOSDIO:
17710Sstevel@tonic-gate 			/*
17720Sstevel@tonic-gate 			 * set delayed-io
17730Sstevel@tonic-gate 			 */
17740Sstevel@tonic-gate 			return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 		case _FIOGDIO:
17770Sstevel@tonic-gate 			/*
17780Sstevel@tonic-gate 			 * get delayed-io
17790Sstevel@tonic-gate 			 */
17800Sstevel@tonic-gate 			return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 		case _FIOIO:
17830Sstevel@tonic-gate 			/*
17840Sstevel@tonic-gate 			 * inode open
17850Sstevel@tonic-gate 			 */
17860Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
17874662Sfrankho 			    ULOCKFS_VGET_MASK);
17880Sstevel@tonic-gate 			if (error)
17890Sstevel@tonic-gate 				return (error);
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 			error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 			if (ulp) {
17940Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
17950Sstevel@tonic-gate 			}
17960Sstevel@tonic-gate 			return (error);
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		case _FIOFFS:
17990Sstevel@tonic-gate 			/*
18000Sstevel@tonic-gate 			 * file system flush (push w/invalidate)
18010Sstevel@tonic-gate 			 */
18020Sstevel@tonic-gate 			if ((caddr_t)arg != NULL)
18030Sstevel@tonic-gate 				return (EINVAL);
18040Sstevel@tonic-gate 			return (ufs_fioffs(vp, NULL, cr));
18050Sstevel@tonic-gate 
18060Sstevel@tonic-gate 		case _FIOISBUSY:
18070Sstevel@tonic-gate 			/*
18080Sstevel@tonic-gate 			 * Contract-private interface for Legato
18090Sstevel@tonic-gate 			 * Purge this vnode from the DNLC and decide
18100Sstevel@tonic-gate 			 * if this vnode is busy (*arg == 1) or not
18110Sstevel@tonic-gate 			 * (*arg == 0)
18120Sstevel@tonic-gate 			 */
18130Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
18140Sstevel@tonic-gate 				return (EPERM);
18150Sstevel@tonic-gate 			error = ufs_fioisbusy(vp, (int *)arg, cr);
18160Sstevel@tonic-gate 			return (error);
18170Sstevel@tonic-gate 
18180Sstevel@tonic-gate 		case _FIODIRECTIO:
18190Sstevel@tonic-gate 			return (ufs_fiodirectio(vp, (int)arg, cr));
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 		case _FIOTUNE:
18220Sstevel@tonic-gate 			/*
18230Sstevel@tonic-gate 			 * Tune the file system (aka setting fs attributes)
18240Sstevel@tonic-gate 			 */
18250Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
18264662Sfrankho 			    ULOCKFS_SETATTR_MASK);
18270Sstevel@tonic-gate 			if (error)
18280Sstevel@tonic-gate 				return (error);
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 			error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 			if (ulp)
18330Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
18340Sstevel@tonic-gate 			return (error);
18350Sstevel@tonic-gate 
18360Sstevel@tonic-gate 		case _FIOLOGENABLE:
18370Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
18380Sstevel@tonic-gate 				return (EPERM);
18390Sstevel@tonic-gate 			return (ufs_fiologenable(vp, (void *)arg, cr, flag));
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 		case _FIOLOGDISABLE:
18420Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
18430Sstevel@tonic-gate 				return (EPERM);
18440Sstevel@tonic-gate 			return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 		case _FIOISLOG:
18470Sstevel@tonic-gate 			return (ufs_fioislog(vp, (void *)arg, cr, flag));
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 		case _FIOSNAPSHOTCREATE_MULTI:
18500Sstevel@tonic-gate 		{
18510Sstevel@tonic-gate 			struct fiosnapcreate_multi	fc, *fcp;
18520Sstevel@tonic-gate 			size_t	fcm_size;
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
18550Sstevel@tonic-gate 				return (EFAULT);
18560Sstevel@tonic-gate 			if (fc.backfilecount > MAX_BACKFILE_COUNT)
18570Sstevel@tonic-gate 				return (EINVAL);
18580Sstevel@tonic-gate 			fcm_size = sizeof (struct fiosnapcreate_multi) +
18590Sstevel@tonic-gate 			    (fc.backfilecount - 1) * sizeof (int);
18600Sstevel@tonic-gate 			fcp = (struct fiosnapcreate_multi *)
18610Sstevel@tonic-gate 			    kmem_alloc(fcm_size, KM_SLEEP);
18620Sstevel@tonic-gate 			if (copyin((void *)arg, fcp, fcm_size)) {
18630Sstevel@tonic-gate 				kmem_free(fcp, fcm_size);
18640Sstevel@tonic-gate 				return (EFAULT);
18650Sstevel@tonic-gate 			}
18660Sstevel@tonic-gate 			error = ufs_snap_create(vp, fcp, cr);
18675437Smishra 			/*
18685437Smishra 			 * Do copyout even if there is an error because
18695437Smishra 			 * the details of error is stored in fcp.
18705437Smishra 			 */
18715437Smishra 			if (copyout(fcp, (void *)arg, fcm_size))
18720Sstevel@tonic-gate 				error = EFAULT;
18730Sstevel@tonic-gate 			kmem_free(fcp, fcm_size);
18740Sstevel@tonic-gate 			return (error);
18750Sstevel@tonic-gate 		}
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 		case _FIOSNAPSHOTDELETE:
18780Sstevel@tonic-gate 		{
18790Sstevel@tonic-gate 			struct fiosnapdelete	fc;
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
18820Sstevel@tonic-gate 				return (EFAULT);
18830Sstevel@tonic-gate 			error = ufs_snap_delete(vp, &fc, cr);
18840Sstevel@tonic-gate 			if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
18850Sstevel@tonic-gate 				error = EFAULT;
18860Sstevel@tonic-gate 			return (error);
18870Sstevel@tonic-gate 		}
18880Sstevel@tonic-gate 
18890Sstevel@tonic-gate 		case _FIOGETSUPERBLOCK:
18900Sstevel@tonic-gate 			if (copyout(fs, (void *)arg, SBSIZE))
18910Sstevel@tonic-gate 				return (EFAULT);
18920Sstevel@tonic-gate 			return (0);
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 		case _FIOGETMAXPHYS:
18950Sstevel@tonic-gate 			if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
18960Sstevel@tonic-gate 				return (EFAULT);
18970Sstevel@tonic-gate 			return (0);
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate 		/*
19000Sstevel@tonic-gate 		 * The following 3 ioctls are for TSufs support
19010Sstevel@tonic-gate 		 * although could potentially be used elsewhere
19020Sstevel@tonic-gate 		 */
19030Sstevel@tonic-gate 		case _FIO_SET_LUFS_DEBUG:
19040Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
19050Sstevel@tonic-gate 				return (EPERM);
19060Sstevel@tonic-gate 			lufs_debug = (uint32_t)arg;
19070Sstevel@tonic-gate 			return (0);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		case _FIO_SET_LUFS_ERROR:
19100Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
19110Sstevel@tonic-gate 				return (EPERM);
19120Sstevel@tonic-gate 			TRANS_SETERROR(ufsvfsp);
19130Sstevel@tonic-gate 			return (0);
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 		case _FIO_GET_TOP_STATS:
19160Sstevel@tonic-gate 		{
19170Sstevel@tonic-gate 			fio_lufs_stats_t *ls;
19180Sstevel@tonic-gate 			ml_unit_t *ul = ufsvfsp->vfs_log;
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 			ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
19210Sstevel@tonic-gate 			ls->ls_debug = ul->un_debug; /* return debug value */
19220Sstevel@tonic-gate 			/* Copy stucture if statistics are being kept */
19230Sstevel@tonic-gate 			if (ul->un_logmap->mtm_tops) {
19240Sstevel@tonic-gate 				ls->ls_topstats = *(ul->un_logmap->mtm_tops);
19250Sstevel@tonic-gate 			}
19260Sstevel@tonic-gate 			error = 0;
19270Sstevel@tonic-gate 			if (copyout(ls, (void *)arg, sizeof (*ls)))
19280Sstevel@tonic-gate 				error = EFAULT;
19290Sstevel@tonic-gate 			kmem_free(ls, sizeof (*ls));
19300Sstevel@tonic-gate 			return (error);
19310Sstevel@tonic-gate 		}
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 		case _FIO_SEEK_DATA:
19340Sstevel@tonic-gate 		case _FIO_SEEK_HOLE:
19350Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
19360Sstevel@tonic-gate 				return (EFAULT);
19370Sstevel@tonic-gate 			/* offset paramater is in/out */
19380Sstevel@tonic-gate 			error = ufs_fio_holey(vp, cmd, &off);
19390Sstevel@tonic-gate 			if (error)
19400Sstevel@tonic-gate 				return (error);
19410Sstevel@tonic-gate 			if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
19420Sstevel@tonic-gate 				return (EFAULT);
19430Sstevel@tonic-gate 			return (0);
19440Sstevel@tonic-gate 
19455648Ssetje 		case _FIO_COMPRESSED:
19465648Ssetje 		{
19475648Ssetje 			/*
19485648Ssetje 			 * This is a project private ufs ioctl() to mark
19495648Ssetje 			 * the inode as that belonging to a compressed
19505648Ssetje 			 * file. This is used to mark individual
19517563SPrasad.Singamsetty@Sun.COM 			 * compressed files in a miniroot archive.
19525648Ssetje 			 * The files compressed in this manner are
19535648Ssetje 			 * automatically decompressed by the dcfs filesystem
19545648Ssetje 			 * (via an interception in ufs_lookup - see decompvp())
19555648Ssetje 			 * which is layered on top of ufs on a system running
19567563SPrasad.Singamsetty@Sun.COM 			 * from the archive. See uts/common/fs/dcfs for details.
19575648Ssetje 			 * This ioctl only marks the file as compressed - the
19585648Ssetje 			 * actual compression is done by fiocompress (a
19595648Ssetje 			 * userland utility) which invokes this ioctl().
19605648Ssetje 			 */
19615648Ssetje 			struct inode *ip = VTOI(vp);
19625648Ssetje 
19635648Ssetje 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
19645648Ssetje 			    ULOCKFS_SETATTR_MASK);
19655648Ssetje 			if (error)
19665648Ssetje 				return (error);
19675648Ssetje 
19685648Ssetje 			if (ulp) {
19695648Ssetje 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_IUPDAT,
19705648Ssetje 				    TOP_IUPDAT_SIZE(ip));
19715648Ssetje 			}
19725648Ssetje 
19735648Ssetje 			error = ufs_mark_compressed(vp);
19745648Ssetje 
19755648Ssetje 			if (ulp) {
19765648Ssetje 				TRANS_END_ASYNC(ufsvfsp, TOP_IUPDAT,
19775648Ssetje 				    TOP_IUPDAT_SIZE(ip));
19785648Ssetje 				ufs_lockfs_end(ulp);
19795648Ssetje 			}
19805648Ssetje 
19815648Ssetje 			return (error);
19825648Ssetje 
19835648Ssetje 		}
19845648Ssetje 
19850Sstevel@tonic-gate 		default:
19860Sstevel@tonic-gate 			return (ENOTTY);
19870Sstevel@tonic-gate 	}
19880Sstevel@tonic-gate }
19890Sstevel@tonic-gate 
19905648Ssetje 
19910Sstevel@tonic-gate /* ARGSUSED */
19920Sstevel@tonic-gate static int
ufs_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)19930Sstevel@tonic-gate ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
19945331Samw 	struct cred *cr, caller_context_t *ct)
19950Sstevel@tonic-gate {
19960Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
19970Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
19980Sstevel@tonic-gate 	int err;
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate 	if (vap->va_mask == AT_SIZE) {
20010Sstevel@tonic-gate 		/*
20020Sstevel@tonic-gate 		 * for performance, if only the size is requested don't bother
20030Sstevel@tonic-gate 		 * with anything else.
20040Sstevel@tonic-gate 		 */
20050Sstevel@tonic-gate 		UFS_GET_ISIZE(&vap->va_size, ip);
20060Sstevel@tonic-gate 		return (0);
20070Sstevel@tonic-gate 	}
20080Sstevel@tonic-gate 
20090Sstevel@tonic-gate 	/*
20100Sstevel@tonic-gate 	 * inlined lockfs checks
20110Sstevel@tonic-gate 	 */
20120Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
20130Sstevel@tonic-gate 	if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
20140Sstevel@tonic-gate 		err = EIO;
20150Sstevel@tonic-gate 		goto out;
20160Sstevel@tonic-gate 	}
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
20190Sstevel@tonic-gate 	/*
20200Sstevel@tonic-gate 	 * Return all the attributes.  This should be refined so
20210Sstevel@tonic-gate 	 * that it only returns what's asked for.
20220Sstevel@tonic-gate 	 */
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 	/*
20250Sstevel@tonic-gate 	 * Copy from inode table.
20260Sstevel@tonic-gate 	 */
20270Sstevel@tonic-gate 	vap->va_type = vp->v_type;
20280Sstevel@tonic-gate 	vap->va_mode = ip->i_mode & MODEMASK;
20290Sstevel@tonic-gate 	/*
20300Sstevel@tonic-gate 	 * If there is an ACL and there is a mask entry, then do the
20310Sstevel@tonic-gate 	 * extra work that completes the equivalent of an acltomode(3)
20320Sstevel@tonic-gate 	 * call.  According to POSIX P1003.1e, the acl mask should be
20330Sstevel@tonic-gate 	 * returned in the group permissions field.
20340Sstevel@tonic-gate 	 *
20350Sstevel@tonic-gate 	 * - start with the original permission and mode bits (from above)
20360Sstevel@tonic-gate 	 * - clear the group owner bits
20370Sstevel@tonic-gate 	 * - add in the mask bits.
20380Sstevel@tonic-gate 	 */
20390Sstevel@tonic-gate 	if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
20400Sstevel@tonic-gate 		vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
20410Sstevel@tonic-gate 		vap->va_mode |=
20420Sstevel@tonic-gate 		    (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
20430Sstevel@tonic-gate 	}
20440Sstevel@tonic-gate 	vap->va_uid = ip->i_uid;
20450Sstevel@tonic-gate 	vap->va_gid = ip->i_gid;
20460Sstevel@tonic-gate 	vap->va_fsid = ip->i_dev;
20470Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)ip->i_number;
20480Sstevel@tonic-gate 	vap->va_nlink = ip->i_nlink;
20490Sstevel@tonic-gate 	vap->va_size = ip->i_size;
20500Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK)
20510Sstevel@tonic-gate 		vap->va_rdev = ip->i_rdev;
20520Sstevel@tonic-gate 	else
20530Sstevel@tonic-gate 		vap->va_rdev = 0;	/* not a b/c spec. */
20540Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
20550Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
20560Sstevel@tonic-gate 	vap->va_seq = ip->i_seq;
20570Sstevel@tonic-gate 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
20580Sstevel@tonic-gate 	vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
20590Sstevel@tonic-gate 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
20600Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
20610Sstevel@tonic-gate 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
20620Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
20630Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
20640Sstevel@tonic-gate 
20650Sstevel@tonic-gate 	switch (ip->i_mode & IFMT) {
20660Sstevel@tonic-gate 
20670Sstevel@tonic-gate 	case IFBLK:
20680Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;		/* was BLKDEV_IOSIZE */
20690Sstevel@tonic-gate 		break;
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 	case IFCHR:
20720Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
20730Sstevel@tonic-gate 		break;
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 	default:
20760Sstevel@tonic-gate 		vap->va_blksize = ip->i_fs->fs_bsize;
20770Sstevel@tonic-gate 		break;
20780Sstevel@tonic-gate 	}
20790Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
20800Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
20810Sstevel@tonic-gate 	err = 0;
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate out:
20840Sstevel@tonic-gate 	return (err);
20850Sstevel@tonic-gate }
20860Sstevel@tonic-gate 
20877737SFrank.Batschulat@Sun.COM /*
20887737SFrank.Batschulat@Sun.COM  * Special wrapper to provide a callback for secpolicy_vnode_setattr().
20897737SFrank.Batschulat@Sun.COM  * The i_contents lock is already held by the caller and we need to
20907737SFrank.Batschulat@Sun.COM  * declare the inode as 'void *' argument.
20917737SFrank.Batschulat@Sun.COM  */
20927737SFrank.Batschulat@Sun.COM static int
ufs_priv_access(void * vip,int mode,struct cred * cr)20937737SFrank.Batschulat@Sun.COM ufs_priv_access(void *vip, int mode, struct cred *cr)
20947737SFrank.Batschulat@Sun.COM {
20957737SFrank.Batschulat@Sun.COM 	struct inode *ip = vip;
20967737SFrank.Batschulat@Sun.COM 
20977737SFrank.Batschulat@Sun.COM 	return (ufs_iaccess(ip, mode, cr, 0));
20987737SFrank.Batschulat@Sun.COM }
20997737SFrank.Batschulat@Sun.COM 
21000Sstevel@tonic-gate /*ARGSUSED4*/
21010Sstevel@tonic-gate static int
ufs_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)21020Sstevel@tonic-gate ufs_setattr(
21030Sstevel@tonic-gate 	struct vnode *vp,
21040Sstevel@tonic-gate 	struct vattr *vap,
21050Sstevel@tonic-gate 	int flags,
21060Sstevel@tonic-gate 	struct cred *cr,
21070Sstevel@tonic-gate 	caller_context_t *ct)
21080Sstevel@tonic-gate {
21090Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
21100Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
21110Sstevel@tonic-gate 	struct fs *fs;
21120Sstevel@tonic-gate 	struct ulockfs *ulp;
21130Sstevel@tonic-gate 	char *errmsg1;
21140Sstevel@tonic-gate 	char *errmsg2;
21150Sstevel@tonic-gate 	long blocks;
21160Sstevel@tonic-gate 	long int mask = vap->va_mask;
21170Sstevel@tonic-gate 	size_t len1, len2;
21180Sstevel@tonic-gate 	int issync;
21190Sstevel@tonic-gate 	int trans_size;
21200Sstevel@tonic-gate 	int dotrans;
21210Sstevel@tonic-gate 	int dorwlock;
21220Sstevel@tonic-gate 	int error;
21230Sstevel@tonic-gate 	int owner_change;
21240Sstevel@tonic-gate 	int dodqlock;
21250Sstevel@tonic-gate 	timestruc_t now;
21260Sstevel@tonic-gate 	vattr_t oldva;
21270Sstevel@tonic-gate 	int retry = 1;
21281899Svsakar 	int indeadlock;
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	/*
21310Sstevel@tonic-gate 	 * Cannot set these attributes.
21320Sstevel@tonic-gate 	 */
21335331Samw 	if ((mask & AT_NOSET) || (mask & AT_XVATTR))
21345331Samw 		return (EINVAL);
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 	/*
21370Sstevel@tonic-gate 	 * check for forced unmount
21380Sstevel@tonic-gate 	 */
21390Sstevel@tonic-gate 	if (ufsvfsp == NULL)
21400Sstevel@tonic-gate 		return (EIO);
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
21430Sstevel@tonic-gate 	if (fs->fs_ronly != 0)
21440Sstevel@tonic-gate 		return (EROFS);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate again:
21470Sstevel@tonic-gate 	errmsg1 = NULL;
21480Sstevel@tonic-gate 	errmsg2 = NULL;
21490Sstevel@tonic-gate 	dotrans = 0;
21500Sstevel@tonic-gate 	dorwlock = 0;
21510Sstevel@tonic-gate 	dodqlock = 0;
21520Sstevel@tonic-gate 
21530Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
21540Sstevel@tonic-gate 	if (error)
21550Sstevel@tonic-gate 		goto out;
21560Sstevel@tonic-gate 
21570Sstevel@tonic-gate 	/*
21580Sstevel@tonic-gate 	 * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
21590Sstevel@tonic-gate 	 * This follows the protocol for read()/write().
21600Sstevel@tonic-gate 	 */
21610Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
21621899Svsakar 		/*
21631899Svsakar 		 * ufs_tryirwlock uses rw_tryenter and checks for SLOCK to
21641899Svsakar 		 * avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
21651899Svsakar 		 * possible, retries the operation.
21661899Svsakar 		 */
21671899Svsakar 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_file);
21681899Svsakar 		if (indeadlock) {
21691899Svsakar 			if (ulp)
21701899Svsakar 				ufs_lockfs_end(ulp);
21711899Svsakar 			goto again;
21721899Svsakar 		}
21730Sstevel@tonic-gate 		dorwlock = 1;
21740Sstevel@tonic-gate 	}
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 	/*
21770Sstevel@tonic-gate 	 * Truncate file.  Must have write permission and not be a directory.
21780Sstevel@tonic-gate 	 */
21790Sstevel@tonic-gate 	if (mask & AT_SIZE) {
21800Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
21810Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
21820Sstevel@tonic-gate 			error = EISDIR;
21830Sstevel@tonic-gate 			goto update_inode;
21840Sstevel@tonic-gate 		}
21857737SFrank.Batschulat@Sun.COM 		if (error = ufs_iaccess(ip, IWRITE, cr, 0))
21860Sstevel@tonic-gate 			goto update_inode;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
21890Sstevel@tonic-gate 		error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
21900Sstevel@tonic-gate 		if (error) {
21910Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
21920Sstevel@tonic-gate 			goto update_inode;
21930Sstevel@tonic-gate 		}
21940Sstevel@tonic-gate 	}
21950Sstevel@tonic-gate 
21960Sstevel@tonic-gate 	if (ulp) {
21970Sstevel@tonic-gate 		trans_size = (int)TOP_SETATTR_SIZE(ip);
21980Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
21990Sstevel@tonic-gate 		++dotrans;
22000Sstevel@tonic-gate 	}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 	/*
22030Sstevel@tonic-gate 	 * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
22040Sstevel@tonic-gate 	 * This follows the protocol established by
22050Sstevel@tonic-gate 	 * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
22060Sstevel@tonic-gate 	 */
22070Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
22081899Svsakar 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_SETATTR,
22094662Sfrankho 		    retry_dir);
22101899Svsakar 		if (indeadlock)
22111899Svsakar 			goto again;
22120Sstevel@tonic-gate 		dorwlock = 1;
22130Sstevel@tonic-gate 	}
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	/*
22160Sstevel@tonic-gate 	 * Grab quota lock if we are changing the file's owner.
22170Sstevel@tonic-gate 	 */
22180Sstevel@tonic-gate 	if (mask & AT_UID) {
22190Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
22200Sstevel@tonic-gate 		dodqlock = 1;
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
22230Sstevel@tonic-gate 
22240Sstevel@tonic-gate 	oldva.va_mode = ip->i_mode;
22250Sstevel@tonic-gate 	oldva.va_uid = ip->i_uid;
22260Sstevel@tonic-gate 	oldva.va_gid = ip->i_gid;
22270Sstevel@tonic-gate 
22280Sstevel@tonic-gate 	vap->va_mask &= ~AT_SIZE;
22297737SFrank.Batschulat@Sun.COM 
22300Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
22317737SFrank.Batschulat@Sun.COM 	    ufs_priv_access, ip);
22320Sstevel@tonic-gate 	if (error)
22330Sstevel@tonic-gate 		goto update_inode;
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 	mask = vap->va_mask;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * Change file access modes.
22390Sstevel@tonic-gate 	 */
22400Sstevel@tonic-gate 	if (mask & AT_MODE) {
22410Sstevel@tonic-gate 		ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
22420Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
22430Sstevel@tonic-gate 		ip->i_flag |= ICHG;
22440Sstevel@tonic-gate 		if (stickyhack) {
22450Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
22460Sstevel@tonic-gate 			if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
22470Sstevel@tonic-gate 				vp->v_flag |= VSWAPLIKE;
22480Sstevel@tonic-gate 			else
22490Sstevel@tonic-gate 				vp->v_flag &= ~VSWAPLIKE;
22500Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
22510Sstevel@tonic-gate 		}
22520Sstevel@tonic-gate 	}
22530Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
22540Sstevel@tonic-gate 		if (mask & AT_UID) {
22550Sstevel@tonic-gate 			/*
22560Sstevel@tonic-gate 			 * Don't change ownership of the quota inode.
22570Sstevel@tonic-gate 			 */
22580Sstevel@tonic-gate 			if (ufsvfsp->vfs_qinod == ip) {
22590Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
22600Sstevel@tonic-gate 				error = EINVAL;
22610Sstevel@tonic-gate 				goto update_inode;
22620Sstevel@tonic-gate 			}
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 			/*
22650Sstevel@tonic-gate 			 * No real ownership change.
22660Sstevel@tonic-gate 			 */
22670Sstevel@tonic-gate 			if (ip->i_uid == vap->va_uid) {
22680Sstevel@tonic-gate 				blocks = 0;
22690Sstevel@tonic-gate 				owner_change = 0;
22700Sstevel@tonic-gate 			}
22710Sstevel@tonic-gate 			/*
22720Sstevel@tonic-gate 			 * Remove the blocks and the file, from the old user's
22730Sstevel@tonic-gate 			 * quota.
22740Sstevel@tonic-gate 			 */
22750Sstevel@tonic-gate 			else {
22760Sstevel@tonic-gate 				blocks = ip->i_blocks;
22770Sstevel@tonic-gate 				owner_change = 1;
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 				(void) chkdq(ip, -blocks, /* force */ 1, cr,
22804662Sfrankho 				    (char **)NULL, (size_t *)NULL);
22810Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ -1, ip,
22824662Sfrankho 				    (uid_t)ip->i_uid, /* force */ 1, cr,
22834662Sfrankho 				    (char **)NULL, (size_t *)NULL);
22840Sstevel@tonic-gate 				dqrele(ip->i_dquot);
22850Sstevel@tonic-gate 			}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 			ip->i_uid = vap->va_uid;
22880Sstevel@tonic-gate 
22890Sstevel@tonic-gate 			/*
22900Sstevel@tonic-gate 			 * There is a real ownership change.
22910Sstevel@tonic-gate 			 */
22920Sstevel@tonic-gate 			if (owner_change) {
22930Sstevel@tonic-gate 				/*
22940Sstevel@tonic-gate 				 * Add the blocks and the file to the new
22950Sstevel@tonic-gate 				 * user's quota.
22960Sstevel@tonic-gate 				 */
22970Sstevel@tonic-gate 				ip->i_dquot = getinoquota(ip);
22980Sstevel@tonic-gate 				(void) chkdq(ip, blocks, /* force */ 1, cr,
22994662Sfrankho 				    &errmsg1, &len1);
23000Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ 1,
23014662Sfrankho 				    (struct inode *)NULL, (uid_t)ip->i_uid,
23024662Sfrankho 				    /* force */ 1, cr, &errmsg2, &len2);
23030Sstevel@tonic-gate 			}
23040Sstevel@tonic-gate 		}
23050Sstevel@tonic-gate 		if (mask & AT_GID) {
23060Sstevel@tonic-gate 			ip->i_gid = vap->va_gid;
23070Sstevel@tonic-gate 		}
23080Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
23090Sstevel@tonic-gate 		ip->i_flag |= ICHG;
23100Sstevel@tonic-gate 	}
23110Sstevel@tonic-gate 	/*
23120Sstevel@tonic-gate 	 * Change file access or modified times.
23130Sstevel@tonic-gate 	 */
23140Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
23150Sstevel@tonic-gate 		/* Check that the time value is within ufs range */
23160Sstevel@tonic-gate 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
23170Sstevel@tonic-gate 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
23180Sstevel@tonic-gate 			error = EOVERFLOW;
23190Sstevel@tonic-gate 			goto update_inode;
23200Sstevel@tonic-gate 		}
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 		/*
23230Sstevel@tonic-gate 		 * if the "noaccess" mount option is set and only atime
23240Sstevel@tonic-gate 		 * update is requested, do nothing. No error is returned.
23250Sstevel@tonic-gate 		 */
23260Sstevel@tonic-gate 		if ((ufsvfsp->vfs_noatime) &&
23270Sstevel@tonic-gate 		    ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
23280Sstevel@tonic-gate 			goto skip_atime;
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 		if (mask & AT_ATIME) {
23310Sstevel@tonic-gate 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
23320Sstevel@tonic-gate 			ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
23330Sstevel@tonic-gate 			ip->i_flag &= ~IACC;
23340Sstevel@tonic-gate 		}
23350Sstevel@tonic-gate 		if (mask & AT_MTIME) {
23360Sstevel@tonic-gate 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
23370Sstevel@tonic-gate 			ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
23380Sstevel@tonic-gate 			gethrestime(&now);
23390Sstevel@tonic-gate 			if (now.tv_sec > TIME32_MAX) {
23400Sstevel@tonic-gate 				/*
23410Sstevel@tonic-gate 				 * In 2038, ctime sticks forever..
23420Sstevel@tonic-gate 				 */
23430Sstevel@tonic-gate 				ip->i_ctime.tv_sec = TIME32_MAX;
23440Sstevel@tonic-gate 				ip->i_ctime.tv_usec = 0;
23450Sstevel@tonic-gate 			} else {
23460Sstevel@tonic-gate 				ip->i_ctime.tv_sec = now.tv_sec;
23470Sstevel@tonic-gate 				ip->i_ctime.tv_usec = now.tv_nsec / 1000;
23480Sstevel@tonic-gate 			}
23490Sstevel@tonic-gate 			ip->i_flag &= ~(IUPD|ICHG);
23500Sstevel@tonic-gate 			ip->i_flag |= IMODTIME;
23510Sstevel@tonic-gate 		}
23520Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
23530Sstevel@tonic-gate 		ip->i_flag |= IMOD;
23540Sstevel@tonic-gate 	}
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate skip_atime:
23570Sstevel@tonic-gate 	/*
23580Sstevel@tonic-gate 	 * The presence of a shadow inode may indicate an ACL, but does
23590Sstevel@tonic-gate 	 * not imply an ACL.  Future FSD types should be handled here too
23600Sstevel@tonic-gate 	 * and check for the presence of the attribute-specific data
23610Sstevel@tonic-gate 	 * before referencing it.
23620Sstevel@tonic-gate 	 */
23630Sstevel@tonic-gate 	if (ip->i_shadow) {
23640Sstevel@tonic-gate 		/*
23650Sstevel@tonic-gate 		 * XXX if ufs_iupdat is changed to sandbagged write fix
23660Sstevel@tonic-gate 		 * ufs_acl_setattr to push ip to keep acls consistent
23670Sstevel@tonic-gate 		 *
23680Sstevel@tonic-gate 		 * Suppress out of inodes messages if we will retry.
23690Sstevel@tonic-gate 		 */
23700Sstevel@tonic-gate 		if (retry)
23710Sstevel@tonic-gate 			ip->i_flag |= IQUIET;
23720Sstevel@tonic-gate 		error = ufs_acl_setattr(ip, vap, cr);
23730Sstevel@tonic-gate 		ip->i_flag &= ~IQUIET;
23740Sstevel@tonic-gate 	}
23750Sstevel@tonic-gate 
23760Sstevel@tonic-gate update_inode:
23770Sstevel@tonic-gate 	/*
23780Sstevel@tonic-gate 	 * Setattr always increases the sequence number
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	ip->i_seq++;
23810Sstevel@tonic-gate 
23820Sstevel@tonic-gate 	/*
23830Sstevel@tonic-gate 	 * if nfsd and not logging; push synchronously
23840Sstevel@tonic-gate 	 */
23850Sstevel@tonic-gate 	if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
23860Sstevel@tonic-gate 		ufs_iupdat(ip, 1);
23870Sstevel@tonic-gate 	} else {
23880Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 
23910Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
23920Sstevel@tonic-gate 	if (dodqlock) {
23930Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
23940Sstevel@tonic-gate 	}
23950Sstevel@tonic-gate 	if (dorwlock)
23960Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 	if (ulp) {
23990Sstevel@tonic-gate 		if (dotrans) {
24000Sstevel@tonic-gate 			int terr = 0;
24010Sstevel@tonic-gate 			TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
24020Sstevel@tonic-gate 			    trans_size);
24030Sstevel@tonic-gate 			if (error == 0)
24040Sstevel@tonic-gate 				error = terr;
24050Sstevel@tonic-gate 		}
24060Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
24070Sstevel@tonic-gate 	}
24080Sstevel@tonic-gate out:
24090Sstevel@tonic-gate 	/*
24100Sstevel@tonic-gate 	 * If out of inodes or blocks, see if we can free something
24110Sstevel@tonic-gate 	 * up from the delete queue.
24120Sstevel@tonic-gate 	 */
24130Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
24140Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
24150Sstevel@tonic-gate 		retry = 0;
24160Sstevel@tonic-gate 		if (errmsg1 != NULL)
24170Sstevel@tonic-gate 			kmem_free(errmsg1, len1);
24180Sstevel@tonic-gate 		if (errmsg2 != NULL)
24190Sstevel@tonic-gate 			kmem_free(errmsg2, len2);
24200Sstevel@tonic-gate 		goto again;
24210Sstevel@tonic-gate 	}
24220Sstevel@tonic-gate 	if (errmsg1 != NULL) {
24230Sstevel@tonic-gate 		uprintf(errmsg1);
24240Sstevel@tonic-gate 		kmem_free(errmsg1, len1);
24250Sstevel@tonic-gate 	}
24260Sstevel@tonic-gate 	if (errmsg2 != NULL) {
24270Sstevel@tonic-gate 		uprintf(errmsg2);
24280Sstevel@tonic-gate 		kmem_free(errmsg2, len2);
24290Sstevel@tonic-gate 	}
24300Sstevel@tonic-gate 	return (error);
24310Sstevel@tonic-gate }
24320Sstevel@tonic-gate 
24330Sstevel@tonic-gate /*ARGSUSED*/
24340Sstevel@tonic-gate static int
ufs_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)24355331Samw ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
24365331Samw 	caller_context_t *ct)
24370Sstevel@tonic-gate {
24380Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
24410Sstevel@tonic-gate 		return (EIO);
24420Sstevel@tonic-gate 
24430Sstevel@tonic-gate 	/*
24440Sstevel@tonic-gate 	 * The ufs_iaccess function wants to be called with
24450Sstevel@tonic-gate 	 * mode bits expressed as "ufs specific" bits.
24460Sstevel@tonic-gate 	 * I.e., VWRITE|VREAD|VEXEC do not make sense to
24470Sstevel@tonic-gate 	 * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
24480Sstevel@tonic-gate 	 * But since they're the same we just pass the vnode mode
24490Sstevel@tonic-gate 	 * bit but just verify that assumption at compile time.
24500Sstevel@tonic-gate 	 */
24510Sstevel@tonic-gate #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
24520Sstevel@tonic-gate #error "ufs_access needs to map Vmodes to Imodes"
24530Sstevel@tonic-gate #endif
24547737SFrank.Batschulat@Sun.COM 	return (ufs_iaccess(ip, mode, cr, 1));
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate /* ARGSUSED */
24580Sstevel@tonic-gate static int
ufs_readlink(struct vnode * vp,struct uio * uiop,struct cred * cr,caller_context_t * ct)24595331Samw ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr,
24605331Samw 	caller_context_t *ct)
24610Sstevel@tonic-gate {
24620Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
24630Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
24640Sstevel@tonic-gate 	struct ulockfs *ulp;
24650Sstevel@tonic-gate 	int error;
24660Sstevel@tonic-gate 	int fastsymlink;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 	if (vp->v_type != VLNK) {
24690Sstevel@tonic-gate 		error = EINVAL;
24700Sstevel@tonic-gate 		goto nolockout;
24710Sstevel@tonic-gate 	}
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 	/*
24740Sstevel@tonic-gate 	 * If the symbolic link is empty there is nothing to read.
24750Sstevel@tonic-gate 	 * Fast-track these empty symbolic links
24760Sstevel@tonic-gate 	 */
24770Sstevel@tonic-gate 	if (ip->i_size == 0) {
24780Sstevel@tonic-gate 		error = 0;
24790Sstevel@tonic-gate 		goto nolockout;
24800Sstevel@tonic-gate 	}
24810Sstevel@tonic-gate 
24820Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
24830Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
24840Sstevel@tonic-gate 	if (error)
24850Sstevel@tonic-gate 		goto nolockout;
24860Sstevel@tonic-gate 	/*
24870Sstevel@tonic-gate 	 * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
24880Sstevel@tonic-gate 	 */
24890Sstevel@tonic-gate again:
24900Sstevel@tonic-gate 	fastsymlink = 0;
24910Sstevel@tonic-gate 	if (ip->i_flag & IFASTSYMLNK) {
24920Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
24930Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
24940Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
24950Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
24960Sstevel@tonic-gate 			    (ip->i_fs->fs_ronly == 0) &&
24970Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
24980Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
24990Sstevel@tonic-gate 				ip->i_flag |= IACC;
25000Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
25010Sstevel@tonic-gate 			}
25020Sstevel@tonic-gate 			error = uiomove((caddr_t)&ip->i_db[1],
25034662Sfrankho 			    MIN(ip->i_size, uiop->uio_resid),
25044662Sfrankho 			    UIO_READ, uiop);
25050Sstevel@tonic-gate 			ITIMES(ip);
25060Sstevel@tonic-gate 			++fastsymlink;
25070Sstevel@tonic-gate 		}
25080Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
25090Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
25100Sstevel@tonic-gate 	}
25110Sstevel@tonic-gate 	if (!fastsymlink) {
25120Sstevel@tonic-gate 		ssize_t size;	/* number of bytes read  */
25130Sstevel@tonic-gate 		caddr_t basep;	/* pointer to input data */
25140Sstevel@tonic-gate 		ino_t ino;
25150Sstevel@tonic-gate 		long  igen;
25160Sstevel@tonic-gate 		struct uio tuio;	/* temp uio struct */
25170Sstevel@tonic-gate 		struct uio *tuiop;
25180Sstevel@tonic-gate 		iovec_t tiov;		/* temp iovec struct */
25190Sstevel@tonic-gate 		char kbuf[FSL_SIZE];	/* buffer to hold fast symlink */
25200Sstevel@tonic-gate 		int tflag = 0;		/* flag to indicate temp vars used */
25210Sstevel@tonic-gate 
25220Sstevel@tonic-gate 		ino = ip->i_number;
25230Sstevel@tonic-gate 		igen = ip->i_gen;
25240Sstevel@tonic-gate 		size = uiop->uio_resid;
25250Sstevel@tonic-gate 		basep = uiop->uio_iov->iov_base;
25260Sstevel@tonic-gate 		tuiop = uiop;
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
25290Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
25300Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
25310Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
25320Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
25330Sstevel@tonic-gate 			goto again;
25340Sstevel@tonic-gate 		}
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 		/* can this be a fast symlink and is it a user buffer? */
25370Sstevel@tonic-gate 		if (ip->i_size <= FSL_SIZE &&
25380Sstevel@tonic-gate 		    (uiop->uio_segflg == UIO_USERSPACE ||
25390Sstevel@tonic-gate 		    uiop->uio_segflg == UIO_USERISPACE)) {
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 			bzero(&tuio, sizeof (struct uio));
25420Sstevel@tonic-gate 			/*
25430Sstevel@tonic-gate 			 * setup a kernel buffer to read link into.  this
25440Sstevel@tonic-gate 			 * is to fix a race condition where the user buffer
25450Sstevel@tonic-gate 			 * got corrupted before copying it into the inode.
25460Sstevel@tonic-gate 			 */
25470Sstevel@tonic-gate 			size = ip->i_size;
25480Sstevel@tonic-gate 			tiov.iov_len = size;
25490Sstevel@tonic-gate 			tiov.iov_base = kbuf;
25500Sstevel@tonic-gate 			tuio.uio_iov = &tiov;
25510Sstevel@tonic-gate 			tuio.uio_iovcnt = 1;
25520Sstevel@tonic-gate 			tuio.uio_offset = uiop->uio_offset;
25530Sstevel@tonic-gate 			tuio.uio_segflg = UIO_SYSSPACE;
25540Sstevel@tonic-gate 			tuio.uio_fmode = uiop->uio_fmode;
25550Sstevel@tonic-gate 			tuio.uio_extflg = uiop->uio_extflg;
25560Sstevel@tonic-gate 			tuio.uio_limit = uiop->uio_limit;
25570Sstevel@tonic-gate 			tuio.uio_resid = size;
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 			basep = tuio.uio_iov->iov_base;
25600Sstevel@tonic-gate 			tuiop = &tuio;
25610Sstevel@tonic-gate 			tflag = 1;
25620Sstevel@tonic-gate 		}
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 		error = rdip(ip, tuiop, 0, cr);
25650Sstevel@tonic-gate 		if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
25660Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
25670Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
25680Sstevel@tonic-gate 			goto out;
25690Sstevel@tonic-gate 		}
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 		if (tflag == 0)
25720Sstevel@tonic-gate 			size -= uiop->uio_resid;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 		if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
25750Sstevel@tonic-gate 		    ip->i_size == size) || (tflag == 1 &&
25760Sstevel@tonic-gate 		    tuio.uio_resid == 0)) {
25770Sstevel@tonic-gate 			error = kcopy(basep, &ip->i_db[1], ip->i_size);
25780Sstevel@tonic-gate 			if (error == 0) {
25790Sstevel@tonic-gate 				ip->i_flag |= IFASTSYMLNK;
25800Sstevel@tonic-gate 				/*
25810Sstevel@tonic-gate 				 * free page
25820Sstevel@tonic-gate 				 */
25830Sstevel@tonic-gate 				(void) VOP_PUTPAGE(ITOV(ip),
25840Sstevel@tonic-gate 				    (offset_t)0, PAGESIZE,
25850Sstevel@tonic-gate 				    (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
25865331Samw 				    cr, ct);
25870Sstevel@tonic-gate 			} else {
25880Sstevel@tonic-gate 				int i;
25890Sstevel@tonic-gate 				/* error, clear garbage left behind */
25900Sstevel@tonic-gate 				for (i = 1; i < NDADDR; i++)
25910Sstevel@tonic-gate 					ip->i_db[i] = 0;
25920Sstevel@tonic-gate 				for (i = 0; i < NIADDR; i++)
25930Sstevel@tonic-gate 					ip->i_ib[i] = 0;
25940Sstevel@tonic-gate 			}
25950Sstevel@tonic-gate 		}
25960Sstevel@tonic-gate 		if (tflag == 1) {
25970Sstevel@tonic-gate 			/* now, copy it into the user buffer */
25980Sstevel@tonic-gate 			error = uiomove((caddr_t)kbuf,
25994662Sfrankho 			    MIN(size, uiop->uio_resid),
26004662Sfrankho 			    UIO_READ, uiop);
26010Sstevel@tonic-gate 		}
26020Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
26030Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
26040Sstevel@tonic-gate 	}
26050Sstevel@tonic-gate out:
26060Sstevel@tonic-gate 	if (ulp) {
26070Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
26080Sstevel@tonic-gate 	}
26090Sstevel@tonic-gate nolockout:
26100Sstevel@tonic-gate 	return (error);
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate /* ARGSUSED */
26140Sstevel@tonic-gate static int
ufs_fsync(struct vnode * vp,int syncflag,struct cred * cr,caller_context_t * ct)26155331Samw ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr,
26165331Samw 	caller_context_t *ct)
26170Sstevel@tonic-gate {
26180Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
26190Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
26200Sstevel@tonic-gate 	struct ulockfs *ulp;
26210Sstevel@tonic-gate 	int error;
26220Sstevel@tonic-gate 
26230Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
26240Sstevel@tonic-gate 	if (error)
26250Sstevel@tonic-gate 		return (error);
26260Sstevel@tonic-gate 
26270Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
26280Sstevel@tonic-gate 		/*
26290Sstevel@tonic-gate 		 * First push out any data pages
26300Sstevel@tonic-gate 		 */
26310Sstevel@tonic-gate 		if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
26320Sstevel@tonic-gate 		    (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
26330Sstevel@tonic-gate 			error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
26345331Samw 			    0, CRED(), ct);
26350Sstevel@tonic-gate 			if (error)
26360Sstevel@tonic-gate 				goto out;
26370Sstevel@tonic-gate 		}
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate 		/*
26400Sstevel@tonic-gate 		 * Delta any delayed inode times updates
26410Sstevel@tonic-gate 		 * and push inode to log.
26420Sstevel@tonic-gate 		 * All other inode deltas will have already been delta'd
26430Sstevel@tonic-gate 		 * and will be pushed during the commit.
26440Sstevel@tonic-gate 		 */
26450Sstevel@tonic-gate 		if (!(syncflag & FDSYNC) &&
26460Sstevel@tonic-gate 		    ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
26470Sstevel@tonic-gate 			if (ulp) {
26480Sstevel@tonic-gate 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
26490Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
26500Sstevel@tonic-gate 			}
26510Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
26520Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
26530Sstevel@tonic-gate 			ip->i_flag &= ~IMODTIME;
26540Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
26550Sstevel@tonic-gate 			ufs_iupdat(ip, I_SYNC);
26560Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
26570Sstevel@tonic-gate 			if (ulp) {
26580Sstevel@tonic-gate 				TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
26590Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
26600Sstevel@tonic-gate 			}
26610Sstevel@tonic-gate 		}
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 		/*
26640Sstevel@tonic-gate 		 * Commit the Moby transaction
26650Sstevel@tonic-gate 		 *
26660Sstevel@tonic-gate 		 * Deltas have already been made so we just need to
26670Sstevel@tonic-gate 		 * commit them with a synchronous transaction.
26680Sstevel@tonic-gate 		 * TRANS_BEGIN_SYNC() will return an error
26690Sstevel@tonic-gate 		 * if there are no deltas to commit, for an
26700Sstevel@tonic-gate 		 * empty transaction.
26710Sstevel@tonic-gate 		 */
26720Sstevel@tonic-gate 		if (ulp) {
26730Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
26740Sstevel@tonic-gate 			    error);
26750Sstevel@tonic-gate 			if (error) {
26760Sstevel@tonic-gate 				error = 0; /* commit wasn't needed */
26770Sstevel@tonic-gate 				goto out;
26780Sstevel@tonic-gate 			}
26790Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
26800Sstevel@tonic-gate 			    TOP_COMMIT_SIZE);
26810Sstevel@tonic-gate 		}
26820Sstevel@tonic-gate 	} else {	/* not logging */
26830Sstevel@tonic-gate 		if (!(IS_SWAPVP(vp)))
26840Sstevel@tonic-gate 			if (syncflag & FNODSYNC) {
26850Sstevel@tonic-gate 				/* Just update the inode only */
26860Sstevel@tonic-gate 				TRANS_IUPDAT(ip, 1);
26870Sstevel@tonic-gate 				error = 0;
26880Sstevel@tonic-gate 			} else if (syncflag & FDSYNC)
26890Sstevel@tonic-gate 				/* Do data-synchronous writes */
26900Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
26910Sstevel@tonic-gate 			else
26920Sstevel@tonic-gate 				/* Do synchronous writes */
26930Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
26940Sstevel@tonic-gate 
26950Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
26960Sstevel@tonic-gate 		if (!error)
26970Sstevel@tonic-gate 			error = ufs_sync_indir(ip);
26980Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
26990Sstevel@tonic-gate 	}
27000Sstevel@tonic-gate out:
27010Sstevel@tonic-gate 	if (ulp) {
27020Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
27030Sstevel@tonic-gate 	}
27040Sstevel@tonic-gate 	return (error);
27050Sstevel@tonic-gate }
27060Sstevel@tonic-gate 
27070Sstevel@tonic-gate /*ARGSUSED*/
27080Sstevel@tonic-gate static void
ufs_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)27095331Samw ufs_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
27100Sstevel@tonic-gate {
27110Sstevel@tonic-gate 	ufs_iinactive(VTOI(vp));
27120Sstevel@tonic-gate }
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate /*
27150Sstevel@tonic-gate  * Unix file system operations having to do with directory manipulation.
27160Sstevel@tonic-gate  */
27170Sstevel@tonic-gate int ufs_lookup_idle_count = 2;	/* Number of inodes to idle each time */
27180Sstevel@tonic-gate /* ARGSUSED */
27190Sstevel@tonic-gate static int
ufs_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)27200Sstevel@tonic-gate ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
27215331Samw 	struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr,
27225331Samw 	caller_context_t *ct, int *direntflags, pathname_t *realpnp)
27230Sstevel@tonic-gate {
27240Sstevel@tonic-gate 	struct inode *ip;
27250Sstevel@tonic-gate 	struct inode *sip;
27260Sstevel@tonic-gate 	struct inode *xip;
27270Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
27280Sstevel@tonic-gate 	struct ulockfs *ulp;
27290Sstevel@tonic-gate 	struct vnode *vp;
27300Sstevel@tonic-gate 	int error;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	/*
27330Sstevel@tonic-gate 	 * Check flags for type of lookup (regular file or attribute file)
27340Sstevel@tonic-gate 	 */
27350Sstevel@tonic-gate 
27360Sstevel@tonic-gate 	ip = VTOI(dvp);
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 		/*
27415331Samw 		 * If not mounted with XATTR support then return EINVAL
27425331Samw 		 */
27435331Samw 
27445331Samw 		if (!(ip->i_ufsvfs->vfs_vfs->vfs_flag & VFS_XATTR))
27455331Samw 			return (EINVAL);
27465331Samw 		/*
27470Sstevel@tonic-gate 		 * We don't allow recursive attributes...
27480Sstevel@tonic-gate 		 * Maybe someday we will.
27490Sstevel@tonic-gate 		 */
27500Sstevel@tonic-gate 		if ((ip->i_cflags & IXATTR)) {
27510Sstevel@tonic-gate 			return (EINVAL);
27520Sstevel@tonic-gate 		}
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 		if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
27550Sstevel@tonic-gate 			error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
27560Sstevel@tonic-gate 			if (error) {
27570Sstevel@tonic-gate 				*vpp = NULL;
27580Sstevel@tonic-gate 				goto out;
27590Sstevel@tonic-gate 			}
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 			vp = ITOV(sip);
27620Sstevel@tonic-gate 			dnlc_update(dvp, XATTR_DIR_NAME, vp);
27630Sstevel@tonic-gate 		}
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 		/*
27660Sstevel@tonic-gate 		 * Check accessibility of directory.
27670Sstevel@tonic-gate 		 */
27680Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
27690Sstevel@tonic-gate 			VN_RELE(vp);
27700Sstevel@tonic-gate 			error = ENOENT;
27710Sstevel@tonic-gate 			goto out;
27720Sstevel@tonic-gate 		}
27737737SFrank.Batschulat@Sun.COM 		if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr, 1)) != 0) {
27740Sstevel@tonic-gate 			VN_RELE(vp);
27750Sstevel@tonic-gate 			goto out;
27760Sstevel@tonic-gate 		}
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 		*vpp = vp;
27790Sstevel@tonic-gate 		return (0);
27800Sstevel@tonic-gate 	}
27810Sstevel@tonic-gate 
27820Sstevel@tonic-gate 	/*
27830Sstevel@tonic-gate 	 * Check for a null component, which we should treat as
27840Sstevel@tonic-gate 	 * looking at dvp from within it's parent, so we don't
27850Sstevel@tonic-gate 	 * need a call to ufs_iaccess(), as it has already been
27860Sstevel@tonic-gate 	 * done.
27870Sstevel@tonic-gate 	 */
27880Sstevel@tonic-gate 	if (nm[0] == 0) {
27890Sstevel@tonic-gate 		VN_HOLD(dvp);
27900Sstevel@tonic-gate 		error = 0;
27910Sstevel@tonic-gate 		*vpp = dvp;
27920Sstevel@tonic-gate 		goto out;
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 	/*
27960Sstevel@tonic-gate 	 * Check for "." ie itself. this is a quick check and
27970Sstevel@tonic-gate 	 * avoids adding "." into the dnlc (which have been seen
27980Sstevel@tonic-gate 	 * to occupy >10% of the cache).
27990Sstevel@tonic-gate 	 */
28000Sstevel@tonic-gate 	if ((nm[0] == '.') && (nm[1] == 0)) {
28010Sstevel@tonic-gate 		/*
28020Sstevel@tonic-gate 		 * Don't return without checking accessibility
28030Sstevel@tonic-gate 		 * of the directory. We only need the lock if
28040Sstevel@tonic-gate 		 * we are going to return it.
28050Sstevel@tonic-gate 		 */
28067737SFrank.Batschulat@Sun.COM 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) == 0) {
28070Sstevel@tonic-gate 			VN_HOLD(dvp);
28080Sstevel@tonic-gate 			*vpp = dvp;
28090Sstevel@tonic-gate 		}
28100Sstevel@tonic-gate 		goto out;
28110Sstevel@tonic-gate 	}
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	/*
28140Sstevel@tonic-gate 	 * Fast path: Check the directory name lookup cache.
28150Sstevel@tonic-gate 	 */
28160Sstevel@tonic-gate 	if (vp = dnlc_lookup(dvp, nm)) {
28170Sstevel@tonic-gate 		/*
28180Sstevel@tonic-gate 		 * Check accessibility of directory.
28190Sstevel@tonic-gate 		 */
28207737SFrank.Batschulat@Sun.COM 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) != 0) {
28210Sstevel@tonic-gate 			VN_RELE(vp);
28220Sstevel@tonic-gate 			goto out;
28230Sstevel@tonic-gate 		}
28240Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
28250Sstevel@tonic-gate 			VN_RELE(vp);
28260Sstevel@tonic-gate 			error = ENOENT;
28270Sstevel@tonic-gate 			goto out;
28280Sstevel@tonic-gate 		}
28290Sstevel@tonic-gate 		xip = VTOI(vp);
28300Sstevel@tonic-gate 		ulp = NULL;
28310Sstevel@tonic-gate 		goto fastpath;
28320Sstevel@tonic-gate 	}
28330Sstevel@tonic-gate 
28340Sstevel@tonic-gate 	/*
28350Sstevel@tonic-gate 	 * Keep the idle queue from getting too long by
28360Sstevel@tonic-gate 	 * idling two inodes before attempting to allocate another.
28370Sstevel@tonic-gate 	 *    This operation must be performed before entering
28380Sstevel@tonic-gate 	 *    lockfs or a transaction.
28390Sstevel@tonic-gate 	 */
28400Sstevel@tonic-gate 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
28410Sstevel@tonic-gate 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
28420Sstevel@tonic-gate 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
28430Sstevel@tonic-gate 			ufs_idle_some(ufs_lookup_idle_count);
28440Sstevel@tonic-gate 		}
28450Sstevel@tonic-gate 
28461899Svsakar retry_lookup:
28477737SFrank.Batschulat@Sun.COM 	/*
28487737SFrank.Batschulat@Sun.COM 	 * Check accessibility of directory.
28497737SFrank.Batschulat@Sun.COM 	 */
28507737SFrank.Batschulat@Sun.COM 	if (error = ufs_diraccess(ip, IEXEC, cr))
28517737SFrank.Batschulat@Sun.COM 		goto out;
28527737SFrank.Batschulat@Sun.COM 
28530Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
28540Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
28550Sstevel@tonic-gate 	if (error)
28560Sstevel@tonic-gate 		goto out;
28570Sstevel@tonic-gate 
2858*12170SOwen.Roberts@Sun.Com 	error = ufs_dirlook(ip, nm, &xip, cr, 1, 0);
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate fastpath:
28610Sstevel@tonic-gate 	if (error == 0) {
28620Sstevel@tonic-gate 		ip = xip;
28630Sstevel@tonic-gate 		*vpp = ITOV(ip);
28640Sstevel@tonic-gate 
28650Sstevel@tonic-gate 		/*
28660Sstevel@tonic-gate 		 * If vnode is a device return special vnode instead.
28670Sstevel@tonic-gate 		 */
28680Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
28690Sstevel@tonic-gate 			struct vnode *newvp;
28700Sstevel@tonic-gate 
28710Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
28720Sstevel@tonic-gate 			    cr);
28730Sstevel@tonic-gate 			VN_RELE(*vpp);
28740Sstevel@tonic-gate 			if (newvp == NULL)
28750Sstevel@tonic-gate 				error = ENOSYS;
28760Sstevel@tonic-gate 			else
28770Sstevel@tonic-gate 				*vpp = newvp;
28785648Ssetje 		} else if (ip->i_cflags & ICOMPRESS) {
28795648Ssetje 			struct vnode *newvp;
28805648Ssetje 
28815648Ssetje 			/*
28825648Ssetje 			 * Compressed file, substitute dcfs vnode
28835648Ssetje 			 */
28845648Ssetje 			newvp = decompvp(*vpp, cr, ct);
28855648Ssetje 			VN_RELE(*vpp);
28865648Ssetje 			if (newvp == NULL)
28875648Ssetje 				error = ENOSYS;
28885648Ssetje 			else
28895648Ssetje 				*vpp = newvp;
28900Sstevel@tonic-gate 		}
28910Sstevel@tonic-gate 	}
28920Sstevel@tonic-gate 	if (ulp) {
28930Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
28940Sstevel@tonic-gate 	}
28950Sstevel@tonic-gate 
28961899Svsakar 	if (error == EAGAIN)
28971899Svsakar 		goto retry_lookup;
28981899Svsakar 
28990Sstevel@tonic-gate out:
29000Sstevel@tonic-gate 	return (error);
29010Sstevel@tonic-gate }
29020Sstevel@tonic-gate 
29035331Samw /*ARGSUSED*/
29040Sstevel@tonic-gate static int
ufs_create(struct vnode * dvp,char * name,struct vattr * vap,enum vcexcl excl,int mode,struct vnode ** vpp,struct cred * cr,int flag,caller_context_t * ct,vsecattr_t * vsecp)29050Sstevel@tonic-gate ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
29065331Samw 	int mode, struct vnode **vpp, struct cred *cr, int flag,
29075331Samw 	caller_context_t *ct, vsecattr_t *vsecp)
29080Sstevel@tonic-gate {
29090Sstevel@tonic-gate 	struct inode *ip;
29100Sstevel@tonic-gate 	struct inode *xip;
29110Sstevel@tonic-gate 	struct inode *dip;
29120Sstevel@tonic-gate 	struct vnode *xvp;
29130Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
29140Sstevel@tonic-gate 	struct ulockfs *ulp;
29150Sstevel@tonic-gate 	int error;
29160Sstevel@tonic-gate 	int issync;
29170Sstevel@tonic-gate 	int truncflag;
29180Sstevel@tonic-gate 	int trans_size;
29190Sstevel@tonic-gate 	int noentry;
29200Sstevel@tonic-gate 	int defer_dip_seq_update = 0;	/* need to defer update of dip->i_seq */
29210Sstevel@tonic-gate 	int retry = 1;
29221899Svsakar 	int indeadlock;
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate again:
29250Sstevel@tonic-gate 	ip = VTOI(dvp);
29260Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
29270Sstevel@tonic-gate 	truncflag = 0;
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
29300Sstevel@tonic-gate 	if (error)
29310Sstevel@tonic-gate 		goto out;
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	if (ulp) {
29340Sstevel@tonic-gate 		trans_size = (int)TOP_CREATE_SIZE(ip);
29350Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
29360Sstevel@tonic-gate 	}
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
29390Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
29400Sstevel@tonic-gate 
29410Sstevel@tonic-gate 	if (*name == '\0') {
29420Sstevel@tonic-gate 		/*
29430Sstevel@tonic-gate 		 * Null component name refers to the directory itself.
29440Sstevel@tonic-gate 		 */
29450Sstevel@tonic-gate 		VN_HOLD(dvp);
29460Sstevel@tonic-gate 		/*
29470Sstevel@tonic-gate 		 * Even though this is an error case, we need to grab the
29480Sstevel@tonic-gate 		 * quota lock since the error handling code below is common.
29490Sstevel@tonic-gate 		 */
29500Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
29510Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
29520Sstevel@tonic-gate 		error = EEXIST;
29530Sstevel@tonic-gate 	} else {
29540Sstevel@tonic-gate 		xip = NULL;
29550Sstevel@tonic-gate 		noentry = 0;
29561899Svsakar 		/*
29571899Svsakar 		 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
29581899Svsakar 		 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
29591899Svsakar 		 * possible, retries the operation.
29601899Svsakar 		 */
29611899Svsakar 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_CREATE,
29624662Sfrankho 		    retry_dir);
29631899Svsakar 		if (indeadlock)
29641899Svsakar 			goto again;
29651899Svsakar 
29660Sstevel@tonic-gate 		xvp = dnlc_lookup(dvp, name);
29670Sstevel@tonic-gate 		if (xvp == DNLC_NO_VNODE) {
29680Sstevel@tonic-gate 			noentry = 1;
29690Sstevel@tonic-gate 			VN_RELE(xvp);
29700Sstevel@tonic-gate 			xvp = NULL;
29710Sstevel@tonic-gate 		}
29720Sstevel@tonic-gate 		if (xvp) {
29730Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
29747737SFrank.Batschulat@Sun.COM 			if (error = ufs_iaccess(ip, IEXEC, cr, 1)) {
29750Sstevel@tonic-gate 				VN_RELE(xvp);
29760Sstevel@tonic-gate 			} else {
29770Sstevel@tonic-gate 				error = EEXIST;
29780Sstevel@tonic-gate 				xip = VTOI(xvp);
29790Sstevel@tonic-gate 			}
29800Sstevel@tonic-gate 		} else {
29810Sstevel@tonic-gate 			/*
29820Sstevel@tonic-gate 			 * Suppress file system full message if we will retry
29830Sstevel@tonic-gate 			 */
29840Sstevel@tonic-gate 			error = ufs_direnter_cm(ip, name, DE_CREATE,
29854662Sfrankho 			    vap, &xip, cr, (noentry | (retry ? IQUIET : 0)));
29861899Svsakar 			if (error == EAGAIN) {
29871899Svsakar 				if (ulp) {
29881899Svsakar 					TRANS_END_CSYNC(ufsvfsp, error, issync,
29894662Sfrankho 					    TOP_CREATE, trans_size);
29901899Svsakar 					ufs_lockfs_end(ulp);
29911899Svsakar 				}
29921899Svsakar 				goto again;
29931899Svsakar 			}
29940Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
29950Sstevel@tonic-gate 		}
29960Sstevel@tonic-gate 		ip = xip;
29970Sstevel@tonic-gate 		if (ip != NULL) {
29980Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
29990Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
30000Sstevel@tonic-gate 		}
30010Sstevel@tonic-gate 	}
30020Sstevel@tonic-gate 
30030Sstevel@tonic-gate 	/*
30040Sstevel@tonic-gate 	 * If the file already exists and this is a non-exclusive create,
30050Sstevel@tonic-gate 	 * check permissions and allow access for non-directories.
30060Sstevel@tonic-gate 	 * Read-only create of an existing directory is also allowed.
30070Sstevel@tonic-gate 	 * We fail an exclusive create of anything which already exists.
30080Sstevel@tonic-gate 	 */
30090Sstevel@tonic-gate 	if (error == EEXIST) {
30100Sstevel@tonic-gate 		dip = VTOI(dvp);
30110Sstevel@tonic-gate 		if (excl == NONEXCL) {
30120Sstevel@tonic-gate 			if ((((ip->i_mode & IFMT) == IFDIR) ||
30130Sstevel@tonic-gate 			    ((ip->i_mode & IFMT) == IFATTRDIR)) &&
30140Sstevel@tonic-gate 			    (mode & IWRITE))
30150Sstevel@tonic-gate 				error = EISDIR;
30160Sstevel@tonic-gate 			else if (mode)
30177737SFrank.Batschulat@Sun.COM 				error = ufs_iaccess(ip, mode, cr, 0);
30180Sstevel@tonic-gate 			else
30190Sstevel@tonic-gate 				error = 0;
30200Sstevel@tonic-gate 		}
30210Sstevel@tonic-gate 		if (error) {
30220Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
30230Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
30240Sstevel@tonic-gate 			VN_RELE(ITOV(ip));
30250Sstevel@tonic-gate 			goto unlock;
30260Sstevel@tonic-gate 		}
30270Sstevel@tonic-gate 		/*
30280Sstevel@tonic-gate 		 * If the error EEXIST was set, then i_seq can not
30290Sstevel@tonic-gate 		 * have been updated. The sequence number interface
30300Sstevel@tonic-gate 		 * is defined such that a non-error VOP_CREATE must
30310Sstevel@tonic-gate 		 * increase the dir va_seq it by at least one. If we
30320Sstevel@tonic-gate 		 * have cleared the error, increase i_seq. Note that
30330Sstevel@tonic-gate 		 * we are increasing the dir i_seq and in rare cases
30340Sstevel@tonic-gate 		 * ip may actually be from the dvp, so we already have
30350Sstevel@tonic-gate 		 * the locks and it will not be subject to truncation.
30360Sstevel@tonic-gate 		 * In case we have to update i_seq of the parent
30370Sstevel@tonic-gate 		 * directory dip, we have to defer it till we have
30380Sstevel@tonic-gate 		 * released our locks on ip due to lock ordering requirements.
30390Sstevel@tonic-gate 		 */
30400Sstevel@tonic-gate 		if (ip != dip)
30410Sstevel@tonic-gate 			defer_dip_seq_update = 1;
30420Sstevel@tonic-gate 		else
30430Sstevel@tonic-gate 			ip->i_seq++;
30440Sstevel@tonic-gate 
30450Sstevel@tonic-gate 		if (((ip->i_mode & IFMT) == IFREG) &&
30460Sstevel@tonic-gate 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
30470Sstevel@tonic-gate 			/*
30480Sstevel@tonic-gate 			 * Truncate regular files, if requested by caller.
30490Sstevel@tonic-gate 			 * Grab i_rwlock to make sure no one else is
30500Sstevel@tonic-gate 			 * currently writing to the file (we promised
30510Sstevel@tonic-gate 			 * bmap we would do this).
30520Sstevel@tonic-gate 			 * Must get the locks in the correct order.
30530Sstevel@tonic-gate 			 */
30540Sstevel@tonic-gate 			if (ip->i_size == 0) {
30550Sstevel@tonic-gate 				ip->i_flag |= ICHG | IUPD;
30560Sstevel@tonic-gate 				ip->i_seq++;
30570Sstevel@tonic-gate 				TRANS_INODE(ufsvfsp, ip);
30580Sstevel@tonic-gate 			} else {
30590Sstevel@tonic-gate 				/*
30600Sstevel@tonic-gate 				 * Large Files: Why this check here?
30610Sstevel@tonic-gate 				 * Though we do it in vn_create() we really
30620Sstevel@tonic-gate 				 * want to guarantee that we do not destroy
30630Sstevel@tonic-gate 				 * Large file data by atomically checking
30640Sstevel@tonic-gate 				 * the size while holding the contents
30650Sstevel@tonic-gate 				 * lock.
30660Sstevel@tonic-gate 				 */
30670Sstevel@tonic-gate 				if (flag && !(flag & FOFFMAX) &&
30680Sstevel@tonic-gate 				    ((ip->i_mode & IFMT) == IFREG) &&
30690Sstevel@tonic-gate 				    (ip->i_size > (offset_t)MAXOFF32_T)) {
30700Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
30710Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
30720Sstevel@tonic-gate 					error = EOVERFLOW;
30730Sstevel@tonic-gate 					goto unlock;
30740Sstevel@tonic-gate 				}
30750Sstevel@tonic-gate 				if (TRANS_ISTRANS(ufsvfsp))
30760Sstevel@tonic-gate 					truncflag++;
30770Sstevel@tonic-gate 				else {
30780Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
30790Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
30801899Svsakar 					ufs_tryirwlock_trans(&ip->i_rwlock,
30814662Sfrankho 					    RW_WRITER, TOP_CREATE,
30824662Sfrankho 					    retry_file);
30831899Svsakar 					if (indeadlock) {
30841899Svsakar 						VN_RELE(ITOV(ip));
30851899Svsakar 						goto again;
30861899Svsakar 					}
30870Sstevel@tonic-gate 					rw_enter(&ufsvfsp->vfs_dqrwlock,
30884662Sfrankho 					    RW_READER);
30890Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
30900Sstevel@tonic-gate 					(void) ufs_itrunc(ip, (u_offset_t)0, 0,
30914662Sfrankho 					    cr);
30920Sstevel@tonic-gate 					rw_exit(&ip->i_rwlock);
30930Sstevel@tonic-gate 				}
30944863Spraks 
30954863Spraks 			}
30964863Spraks 			if (error == 0) {
30975331Samw 				vnevent_create(ITOV(ip), ct);
30980Sstevel@tonic-gate 			}
30990Sstevel@tonic-gate 		}
31000Sstevel@tonic-gate 	}
31010Sstevel@tonic-gate 
31020Sstevel@tonic-gate 	if (error) {
31030Sstevel@tonic-gate 		if (ip != NULL) {
31040Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
31050Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
31060Sstevel@tonic-gate 		}
31070Sstevel@tonic-gate 		goto unlock;
31080Sstevel@tonic-gate 	}
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	*vpp = ITOV(ip);
31110Sstevel@tonic-gate 	ITIMES(ip);
31120Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
31130Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	/*
31160Sstevel@tonic-gate 	 * If vnode is a device return special vnode instead.
31170Sstevel@tonic-gate 	 */
31180Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
31190Sstevel@tonic-gate 		struct vnode *newvp;
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
31220Sstevel@tonic-gate 		VN_RELE(*vpp);
31230Sstevel@tonic-gate 		if (newvp == NULL) {
31240Sstevel@tonic-gate 			error = ENOSYS;
31250Sstevel@tonic-gate 			goto unlock;
31260Sstevel@tonic-gate 		}
31270Sstevel@tonic-gate 		truncflag = 0;
31280Sstevel@tonic-gate 		*vpp = newvp;
31290Sstevel@tonic-gate 	}
31300Sstevel@tonic-gate unlock:
31310Sstevel@tonic-gate 
31320Sstevel@tonic-gate 	/*
31330Sstevel@tonic-gate 	 * Do the deferred update of the parent directory's sequence
31340Sstevel@tonic-gate 	 * number now.
31350Sstevel@tonic-gate 	 */
31360Sstevel@tonic-gate 	if (defer_dip_seq_update == 1) {
31370Sstevel@tonic-gate 		rw_enter(&dip->i_contents, RW_READER);
31380Sstevel@tonic-gate 		mutex_enter(&dip->i_tlock);
31390Sstevel@tonic-gate 		dip->i_seq++;
31400Sstevel@tonic-gate 		mutex_exit(&dip->i_tlock);
31410Sstevel@tonic-gate 		rw_exit(&dip->i_contents);
31420Sstevel@tonic-gate 	}
31430Sstevel@tonic-gate 
31440Sstevel@tonic-gate 	if (ulp) {
31450Sstevel@tonic-gate 		int terr = 0;
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
31480Sstevel@tonic-gate 		    trans_size);
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 		/*
31510Sstevel@tonic-gate 		 * If we haven't had a more interesting failure
31520Sstevel@tonic-gate 		 * already, then anything that might've happened
31530Sstevel@tonic-gate 		 * here should be reported.
31540Sstevel@tonic-gate 		 */
31550Sstevel@tonic-gate 		if (error == 0)
31560Sstevel@tonic-gate 			error = terr;
31570Sstevel@tonic-gate 	}
31580Sstevel@tonic-gate 
31590Sstevel@tonic-gate 	if (!error && truncflag) {
31601899Svsakar 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_trunc);
31611899Svsakar 		if (indeadlock) {
31621899Svsakar 			if (ulp)
31631899Svsakar 				ufs_lockfs_end(ulp);
31641899Svsakar 			VN_RELE(ITOV(ip));
31651899Svsakar 			goto again;
31661899Svsakar 		}
31670Sstevel@tonic-gate 		(void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
31680Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
31690Sstevel@tonic-gate 	}
31700Sstevel@tonic-gate 
31710Sstevel@tonic-gate 	if (ulp)
31720Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
31730Sstevel@tonic-gate 
31740Sstevel@tonic-gate 	/*
31750Sstevel@tonic-gate 	 * If no inodes available, try to free one up out of the
31760Sstevel@tonic-gate 	 * pending delete queue.
31770Sstevel@tonic-gate 	 */
31780Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
31790Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
31800Sstevel@tonic-gate 		retry = 0;
31810Sstevel@tonic-gate 		goto again;
31820Sstevel@tonic-gate 	}
31830Sstevel@tonic-gate 
31840Sstevel@tonic-gate out:
31850Sstevel@tonic-gate 	return (error);
31860Sstevel@tonic-gate }
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate extern int ufs_idle_max;
31890Sstevel@tonic-gate /*ARGSUSED*/
31900Sstevel@tonic-gate static int
ufs_remove(struct vnode * vp,char * nm,struct cred * cr,caller_context_t * ct,int flags)31915331Samw ufs_remove(struct vnode *vp, char *nm, struct cred *cr,
31925331Samw 	caller_context_t *ct, int flags)
31930Sstevel@tonic-gate {
31940Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
31950Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp	= ip->i_ufsvfs;
31960Sstevel@tonic-gate 	struct ulockfs *ulp;
31970Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode corresponding to name being removed */
31981899Svsakar 	int indeadlock;
31990Sstevel@tonic-gate 	int error;
32000Sstevel@tonic-gate 	int issync;
32010Sstevel@tonic-gate 	int trans_size;
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	/*
32040Sstevel@tonic-gate 	 * don't let the delete queue get too long
32050Sstevel@tonic-gate 	 */
32060Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
32070Sstevel@tonic-gate 		error = EIO;
32080Sstevel@tonic-gate 		goto out;
32090Sstevel@tonic-gate 	}
32100Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
32110Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
32120Sstevel@tonic-gate 
3213*12170SOwen.Roberts@Sun.Com 	error = ufs_eventlookup(vp, nm, cr, &rmvp);
3214*12170SOwen.Roberts@Sun.Com 	if (rmvp != NULL) {
3215*12170SOwen.Roberts@Sun.Com 		/* Only send the event if there were no errors */
3216*12170SOwen.Roberts@Sun.Com 		if (error == 0)
3217*12170SOwen.Roberts@Sun.Com 			vnevent_remove(rmvp, vp, nm, ct);
3218*12170SOwen.Roberts@Sun.Com 		VN_RELE(rmvp);
3219*12170SOwen.Roberts@Sun.Com 	}
3220*12170SOwen.Roberts@Sun.Com 
32211899Svsakar retry_remove:
32220Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
32230Sstevel@tonic-gate 	if (error)
32240Sstevel@tonic-gate 		goto out;
32250Sstevel@tonic-gate 
32260Sstevel@tonic-gate 	if (ulp)
32270Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
32280Sstevel@tonic-gate 		    trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
32290Sstevel@tonic-gate 
32301899Svsakar 	/*
32311899Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
32321899Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
32331899Svsakar 	 * possible, retries the operation.
32341899Svsakar 	 */
32351899Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_REMOVE, retry);
32361899Svsakar 	if (indeadlock)
32371899Svsakar 		goto retry_remove;
32380Sstevel@tonic-gate 	error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
3239*12170SOwen.Roberts@Sun.Com 	    DR_REMOVE, cr);
32400Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
32410Sstevel@tonic-gate 
32420Sstevel@tonic-gate 	if (ulp) {
32430Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
32440Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
32450Sstevel@tonic-gate 	}
32460Sstevel@tonic-gate 
32470Sstevel@tonic-gate out:
32480Sstevel@tonic-gate 	return (error);
32490Sstevel@tonic-gate }
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate /*
32520Sstevel@tonic-gate  * Link a file or a directory.  Only privileged processes are allowed to
32530Sstevel@tonic-gate  * make links to directories.
32540Sstevel@tonic-gate  */
32555331Samw /*ARGSUSED*/
32560Sstevel@tonic-gate static int
ufs_link(struct vnode * tdvp,struct vnode * svp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)32575331Samw ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr,
32585331Samw 	caller_context_t *ct, int flags)
32590Sstevel@tonic-gate {
32600Sstevel@tonic-gate 	struct inode *sip;
32610Sstevel@tonic-gate 	struct inode *tdp = VTOI(tdvp);
32620Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
32630Sstevel@tonic-gate 	struct ulockfs *ulp;
32640Sstevel@tonic-gate 	struct vnode *realvp;
32650Sstevel@tonic-gate 	int error;
32660Sstevel@tonic-gate 	int issync;
32670Sstevel@tonic-gate 	int trans_size;
32680Sstevel@tonic-gate 	int isdev;
32691899Svsakar 	int indeadlock;
32700Sstevel@tonic-gate 
32711899Svsakar retry_link:
32720Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
32730Sstevel@tonic-gate 	if (error)
32740Sstevel@tonic-gate 		goto out;
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 	if (ulp)
32770Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
32780Sstevel@tonic-gate 		    trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
32790Sstevel@tonic-gate 
32805331Samw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
32810Sstevel@tonic-gate 		svp = realvp;
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 	/*
32840Sstevel@tonic-gate 	 * Make sure link for extended attributes is valid
32850Sstevel@tonic-gate 	 * We only support hard linking of attr in ATTRDIR to ATTRDIR
32860Sstevel@tonic-gate 	 *
32870Sstevel@tonic-gate 	 * Make certain we don't attempt to look at a device node as
32880Sstevel@tonic-gate 	 * a ufs inode.
32890Sstevel@tonic-gate 	 */
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	isdev = IS_DEVVP(svp);
32920Sstevel@tonic-gate 	if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
32930Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
32940Sstevel@tonic-gate 	    ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
32950Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFDIR))) {
32960Sstevel@tonic-gate 		error = EINVAL;
32970Sstevel@tonic-gate 		goto unlock;
32980Sstevel@tonic-gate 	}
32990Sstevel@tonic-gate 
33000Sstevel@tonic-gate 	sip = VTOI(svp);
33010Sstevel@tonic-gate 	if ((svp->v_type == VDIR &&
33020Sstevel@tonic-gate 	    secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
33030Sstevel@tonic-gate 	    (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
33040Sstevel@tonic-gate 		error = EPERM;
33050Sstevel@tonic-gate 		goto unlock;
33060Sstevel@tonic-gate 	}
33071899Svsakar 
33081899Svsakar 	/*
33091899Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
33101899Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
33111899Svsakar 	 * possible, retries the operation.
33121899Svsakar 	 */
33131899Svsakar 	ufs_tryirwlock_trans(&tdp->i_rwlock, RW_WRITER, TOP_LINK, retry);
33141899Svsakar 	if (indeadlock)
33151899Svsakar 		goto retry_link;
33160Sstevel@tonic-gate 	error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
3317*12170SOwen.Roberts@Sun.Com 	    sip, cr);
33180Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate unlock:
33210Sstevel@tonic-gate 	if (ulp) {
33220Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
33230Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
33240Sstevel@tonic-gate 	}
33254863Spraks 
33264863Spraks 	if (!error) {
33275331Samw 		vnevent_link(svp, ct);
33284863Spraks 	}
33290Sstevel@tonic-gate out:
33300Sstevel@tonic-gate 	return (error);
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate uint64_t ufs_rename_retry_cnt;
33340Sstevel@tonic-gate uint64_t ufs_rename_upgrade_retry_cnt;
33350Sstevel@tonic-gate uint64_t ufs_rename_dircheck_retry_cnt;
33360Sstevel@tonic-gate clock_t	 ufs_rename_backoff_delay = 1;
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate /*
33390Sstevel@tonic-gate  * Rename a file or directory.
33400Sstevel@tonic-gate  * We are given the vnode and entry string of the source and the
33410Sstevel@tonic-gate  * vnode and entry string of the place we want to move the source
33420Sstevel@tonic-gate  * to (the target). The essential operation is:
33430Sstevel@tonic-gate  *	unlink(target);
33440Sstevel@tonic-gate  *	link(source, target);
33450Sstevel@tonic-gate  *	unlink(source);
33460Sstevel@tonic-gate  * but "atomically".  Can't do full commit without saving state in
33470Sstevel@tonic-gate  * the inode on disk, which isn't feasible at this time.  Best we
33480Sstevel@tonic-gate  * can do is always guarantee that the TARGET exists.
33490Sstevel@tonic-gate  */
3350613Sswilcox 
33510Sstevel@tonic-gate /*ARGSUSED*/
33520Sstevel@tonic-gate static int
ufs_rename(struct vnode * sdvp,char * snm,struct vnode * tdvp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)33530Sstevel@tonic-gate ufs_rename(
33540Sstevel@tonic-gate 	struct vnode *sdvp,		/* old (source) parent vnode */
33550Sstevel@tonic-gate 	char *snm,			/* old (source) entry name */
33560Sstevel@tonic-gate 	struct vnode *tdvp,		/* new (target) parent vnode */
33570Sstevel@tonic-gate 	char *tnm,			/* new (target) entry name */
33585331Samw 	struct cred *cr,
33595331Samw 	caller_context_t *ct,
33605331Samw 	int flags)
33610Sstevel@tonic-gate {
33620Sstevel@tonic-gate 	struct inode *sip = NULL;	/* source inode */
3363613Sswilcox 	struct inode *ip = NULL;	/* check inode */
33640Sstevel@tonic-gate 	struct inode *sdp;		/* old (source) parent inode */
33650Sstevel@tonic-gate 	struct inode *tdp;		/* new (target) parent inode */
3366*12170SOwen.Roberts@Sun.Com 	struct vnode *svp = NULL;	/* source vnode */
33670Sstevel@tonic-gate 	struct vnode *tvp = NULL;	/* target vnode, if it exists */
33680Sstevel@tonic-gate 	struct vnode *realvp;
33690Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
33700Sstevel@tonic-gate 	struct ulockfs *ulp;
33711268Sbatschul 	struct ufs_slot slot;
3372613Sswilcox 	timestruc_t now;
33730Sstevel@tonic-gate 	int error;
33740Sstevel@tonic-gate 	int issync;
33750Sstevel@tonic-gate 	int trans_size;
33761899Svsakar 	krwlock_t *first_lock;
33771899Svsakar 	krwlock_t *second_lock;
33781899Svsakar 	krwlock_t *reverse_lock;
3379*12170SOwen.Roberts@Sun.Com 	int serr, terr;
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate 	sdp = VTOI(sdvp);
3382613Sswilcox 	slot.fbp = NULL;
33830Sstevel@tonic-gate 	ufsvfsp = sdp->i_ufsvfs;
3384*12170SOwen.Roberts@Sun.Com 
3385*12170SOwen.Roberts@Sun.Com 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3386*12170SOwen.Roberts@Sun.Com 		tdvp = realvp;
3387*12170SOwen.Roberts@Sun.Com 
3388*12170SOwen.Roberts@Sun.Com 	terr = ufs_eventlookup(tdvp, tnm, cr, &tvp);
3389*12170SOwen.Roberts@Sun.Com 	serr = ufs_eventlookup(sdvp, snm, cr, &svp);
3390*12170SOwen.Roberts@Sun.Com 
3391*12170SOwen.Roberts@Sun.Com 	if ((serr == 0) && ((terr == 0) || (terr == ENOENT))) {
3392*12170SOwen.Roberts@Sun.Com 		if (tvp != NULL)
3393*12170SOwen.Roberts@Sun.Com 			vnevent_rename_dest(tvp, tdvp, tnm, ct);
3394*12170SOwen.Roberts@Sun.Com 
3395*12170SOwen.Roberts@Sun.Com 		/*
3396*12170SOwen.Roberts@Sun.Com 		 * Notify the target directory of the rename event
3397*12170SOwen.Roberts@Sun.Com 		 * if source and target directories are not the same.
3398*12170SOwen.Roberts@Sun.Com 		 */
3399*12170SOwen.Roberts@Sun.Com 		if (sdvp != tdvp)
3400*12170SOwen.Roberts@Sun.Com 			vnevent_rename_dest_dir(tdvp, ct);
3401*12170SOwen.Roberts@Sun.Com 
3402*12170SOwen.Roberts@Sun.Com 		if (svp != NULL)
3403*12170SOwen.Roberts@Sun.Com 			vnevent_rename_src(svp, sdvp, snm, ct);
3404*12170SOwen.Roberts@Sun.Com 	}
3405*12170SOwen.Roberts@Sun.Com 
3406*12170SOwen.Roberts@Sun.Com 	if (tvp != NULL)
3407*12170SOwen.Roberts@Sun.Com 		VN_RELE(tvp);
3408*12170SOwen.Roberts@Sun.Com 
3409*12170SOwen.Roberts@Sun.Com 	if (svp != NULL)
3410*12170SOwen.Roberts@Sun.Com 		VN_RELE(svp);
3411*12170SOwen.Roberts@Sun.Com 
34121899Svsakar retry_rename:
34130Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
34140Sstevel@tonic-gate 	if (error)
34150Sstevel@tonic-gate 		goto out;
34160Sstevel@tonic-gate 
34170Sstevel@tonic-gate 	if (ulp)
34180Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
34190Sstevel@tonic-gate 		    trans_size = (int)TOP_RENAME_SIZE(sdp));
34200Sstevel@tonic-gate 
34215331Samw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
34220Sstevel@tonic-gate 		tdvp = realvp;
34230Sstevel@tonic-gate 
34240Sstevel@tonic-gate 	tdp = VTOI(tdvp);
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 	/*
34270Sstevel@tonic-gate 	 * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
34280Sstevel@tonic-gate 	 */
34290Sstevel@tonic-gate 	if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
34300Sstevel@tonic-gate 		error = EINVAL;
34310Sstevel@tonic-gate 		goto unlock;
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	/*
34357737SFrank.Batschulat@Sun.COM 	 * Check accessibility of directory.
34367737SFrank.Batschulat@Sun.COM 	 */
34377737SFrank.Batschulat@Sun.COM 	if (error = ufs_diraccess(sdp, IEXEC, cr))
34387737SFrank.Batschulat@Sun.COM 		goto unlock;
34397737SFrank.Batschulat@Sun.COM 
34407737SFrank.Batschulat@Sun.COM 	/*
34410Sstevel@tonic-gate 	 * Look up inode of file we're supposed to rename.
34420Sstevel@tonic-gate 	 */
3443613Sswilcox 	gethrestime(&now);
3444*12170SOwen.Roberts@Sun.Com 	if (error = ufs_dirlook(sdp, snm, &sip, cr, 0, 0)) {
34451899Svsakar 		if (error == EAGAIN) {
34461899Svsakar 			if (ulp) {
34471899Svsakar 				TRANS_END_CSYNC(ufsvfsp, error, issync,
34484662Sfrankho 				    TOP_RENAME, trans_size);
34491899Svsakar 				ufs_lockfs_end(ulp);
34501899Svsakar 			}
34511899Svsakar 			goto retry_rename;
34521899Svsakar 		}
34531899Svsakar 
34540Sstevel@tonic-gate 		goto unlock;
34550Sstevel@tonic-gate 	}
34560Sstevel@tonic-gate 
34570Sstevel@tonic-gate 	/*
34580Sstevel@tonic-gate 	 * Lock both the source and target directories (they may be
34590Sstevel@tonic-gate 	 * the same) to provide the atomicity semantics that was
34600Sstevel@tonic-gate 	 * previously provided by the per file system vfs_rename_lock
34610Sstevel@tonic-gate 	 *
34620Sstevel@tonic-gate 	 * with vfs_rename_lock removed to allow simultaneous renames
34630Sstevel@tonic-gate 	 * within a file system, ufs_dircheckpath can deadlock while
34640Sstevel@tonic-gate 	 * traversing back to ensure that source is not a parent directory
34650Sstevel@tonic-gate 	 * of target parent directory. This is because we get into
34660Sstevel@tonic-gate 	 * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
34670Sstevel@tonic-gate 	 * If the tdp and sdp of the simultaneous renames happen to be
34680Sstevel@tonic-gate 	 * in the path of each other, it can lead to a deadlock. This
34690Sstevel@tonic-gate 	 * can be avoided by getting the locks as RW_READER here and then
34700Sstevel@tonic-gate 	 * upgrading to RW_WRITER after completing the ufs_dircheckpath.
34711899Svsakar 	 *
34721899Svsakar 	 * We hold the target directory's i_rwlock after calling
34731899Svsakar 	 * ufs_lockfs_begin but in many other operations (like ufs_readdir)
34741899Svsakar 	 * VOP_RWLOCK is explicitly called by the filesystem independent code
34751899Svsakar 	 * before calling the file system operation. In these cases the order
34761899Svsakar 	 * is reversed (i.e i_rwlock is taken first and then ufs_lockfs_begin
34771899Svsakar 	 * is called). This is fine as long as ufs_lockfs_begin acts as a VOP
34781899Svsakar 	 * counter but with ufs_quiesce setting the SLOCK bit this becomes a
34791899Svsakar 	 * synchronizing object which might lead to a deadlock. So we use
34801899Svsakar 	 * rw_tryenter instead of rw_enter. If we fail to get this lock and
34811899Svsakar 	 * find that SLOCK bit is set, we call ufs_lockfs_end and restart the
34821899Svsakar 	 * operation.
34830Sstevel@tonic-gate 	 */
34840Sstevel@tonic-gate retry:
34851899Svsakar 	first_lock = &tdp->i_rwlock;
34861899Svsakar 	second_lock = &sdp->i_rwlock;
34871899Svsakar retry_firstlock:
34881899Svsakar 	if (!rw_tryenter(first_lock, RW_READER)) {
34890Sstevel@tonic-gate 		/*
34901899Svsakar 		 * We didn't get the lock. Check if the SLOCK is set in the
34911899Svsakar 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
34921899Svsakar 		 * and wait for SLOCK to be cleared.
34930Sstevel@tonic-gate 		 */
34941899Svsakar 
34951899Svsakar 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
34961899Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
34974662Sfrankho 			    trans_size);
34981899Svsakar 			ufs_lockfs_end(ulp);
34991899Svsakar 			goto retry_rename;
35001899Svsakar 
35011899Svsakar 		} else {
35020Sstevel@tonic-gate 			/*
35031899Svsakar 			 * SLOCK isn't set so this is a genuine synchronization
35041899Svsakar 			 * case. Let's try again after giving them a breather.
35050Sstevel@tonic-gate 			 */
35061899Svsakar 			delay(RETRY_LOCK_DELAY);
35071899Svsakar 			goto  retry_firstlock;
35081899Svsakar 		}
35091899Svsakar 	}
35101899Svsakar 	/*
35111899Svsakar 	 * Need to check if the tdp and sdp are same !!!
35121899Svsakar 	 */
35131899Svsakar 	if ((tdp != sdp) && (!rw_tryenter(second_lock, RW_READER))) {
35141899Svsakar 		/*
35151899Svsakar 		 * We didn't get the lock. Check if the SLOCK is set in the
35161899Svsakar 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
35171899Svsakar 		 * and wait for SLOCK to be cleared.
35181899Svsakar 		 */
35191899Svsakar 
35201899Svsakar 		rw_exit(first_lock);
35211899Svsakar 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
35221899Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
35234662Sfrankho 			    trans_size);
35241899Svsakar 			ufs_lockfs_end(ulp);
35251899Svsakar 			goto retry_rename;
35261899Svsakar 
35271899Svsakar 		} else {
35281899Svsakar 			/*
35291899Svsakar 			 * So we couldn't get the second level peer lock *and*
35301899Svsakar 			 * the SLOCK bit isn't set. Too bad we can be
35311899Svsakar 			 * contentding with someone wanting these locks otherway
35321899Svsakar 			 * round. Reverse the locks in case there is a heavy
35331899Svsakar 			 * contention for the second level lock.
35341899Svsakar 			 */
35351899Svsakar 			reverse_lock = first_lock;
35361899Svsakar 			first_lock = second_lock;
35371899Svsakar 			second_lock = reverse_lock;
35381899Svsakar 			ufs_rename_retry_cnt++;
35391899Svsakar 			goto  retry_firstlock;
35400Sstevel@tonic-gate 		}
35410Sstevel@tonic-gate 	}
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	if (sip == tdp) {
35440Sstevel@tonic-gate 		error = EINVAL;
35450Sstevel@tonic-gate 		goto errout;
35460Sstevel@tonic-gate 	}
35470Sstevel@tonic-gate 	/*
35480Sstevel@tonic-gate 	 * Make sure we can delete the source entry.  This requires
35490Sstevel@tonic-gate 	 * write permission on the containing directory.
35500Sstevel@tonic-gate 	 * Check for sticky directories.
35510Sstevel@tonic-gate 	 */
35520Sstevel@tonic-gate 	rw_enter(&sdp->i_contents, RW_READER);
35530Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_READER);
35547737SFrank.Batschulat@Sun.COM 	if ((error = ufs_iaccess(sdp, IWRITE, cr, 0)) != 0 ||
35550Sstevel@tonic-gate 	    (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
35560Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
35570Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
35580Sstevel@tonic-gate 		goto errout;
35590Sstevel@tonic-gate 	}
35600Sstevel@tonic-gate 
35610Sstevel@tonic-gate 	/*
35620Sstevel@tonic-gate 	 * If this is a rename of a directory and the parent is
35630Sstevel@tonic-gate 	 * different (".." must be changed), then the source
35640Sstevel@tonic-gate 	 * directory must not be in the directory hierarchy
35650Sstevel@tonic-gate 	 * above the target, as this would orphan everything
35660Sstevel@tonic-gate 	 * below the source directory.  Also the user must have
35670Sstevel@tonic-gate 	 * write permission in the source so as to be able to
35680Sstevel@tonic-gate 	 * change "..".
35690Sstevel@tonic-gate 	 */
35700Sstevel@tonic-gate 	if ((((sip->i_mode & IFMT) == IFDIR) ||
35710Sstevel@tonic-gate 	    ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
35720Sstevel@tonic-gate 		ino_t	inum;
35730Sstevel@tonic-gate 
35747737SFrank.Batschulat@Sun.COM 		if (error = ufs_iaccess(sip, IWRITE, cr, 0)) {
35750Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
35760Sstevel@tonic-gate 			rw_exit(&sdp->i_contents);
35770Sstevel@tonic-gate 			goto errout;
35780Sstevel@tonic-gate 		}
35790Sstevel@tonic-gate 		inum = sip->i_number;
35800Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
35810Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
35820Sstevel@tonic-gate 		if ((error = ufs_dircheckpath(inum, tdp, sdp, cr))) {
35830Sstevel@tonic-gate 			/*
35840Sstevel@tonic-gate 			 * If we got EAGAIN ufs_dircheckpath detected a
35850Sstevel@tonic-gate 			 * potential deadlock and backed out. We need
35860Sstevel@tonic-gate 			 * to retry the operation since sdp and tdp have
35870Sstevel@tonic-gate 			 * to be released to avoid the deadlock.
35880Sstevel@tonic-gate 			 */
35890Sstevel@tonic-gate 			if (error == EAGAIN) {
35900Sstevel@tonic-gate 				rw_exit(&tdp->i_rwlock);
35910Sstevel@tonic-gate 				if (tdp != sdp)
35920Sstevel@tonic-gate 					rw_exit(&sdp->i_rwlock);
35930Sstevel@tonic-gate 				delay(ufs_rename_backoff_delay);
35940Sstevel@tonic-gate 				ufs_rename_dircheck_retry_cnt++;
35950Sstevel@tonic-gate 				goto retry;
35960Sstevel@tonic-gate 			}
35970Sstevel@tonic-gate 			goto errout;
35980Sstevel@tonic-gate 		}
35990Sstevel@tonic-gate 	} else {
36000Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
36010Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
36020Sstevel@tonic-gate 	}
36030Sstevel@tonic-gate 
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	/*
36060Sstevel@tonic-gate 	 * Check for renaming '.' or '..' or alias of '.'
36070Sstevel@tonic-gate 	 */
36080Sstevel@tonic-gate 	if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0 || sdp == sip) {
36090Sstevel@tonic-gate 		error = EINVAL;
36100Sstevel@tonic-gate 		goto errout;
36110Sstevel@tonic-gate 	}
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	/*
36140Sstevel@tonic-gate 	 * Simultaneous renames can deadlock in ufs_dircheckpath since it
36150Sstevel@tonic-gate 	 * tries to traverse back the file tree with both tdp and sdp held
36160Sstevel@tonic-gate 	 * as RW_WRITER. To avoid that we have to hold the tdp and sdp locks
36170Sstevel@tonic-gate 	 * as RW_READERS  till ufs_dircheckpath is done.
36180Sstevel@tonic-gate 	 * Now that ufs_dircheckpath is done with, we can upgrade the locks
36190Sstevel@tonic-gate 	 * to RW_WRITER.
36200Sstevel@tonic-gate 	 */
36210Sstevel@tonic-gate 	if (!rw_tryupgrade(&tdp->i_rwlock)) {
36220Sstevel@tonic-gate 		/*
36230Sstevel@tonic-gate 		 * The upgrade failed. We got to give away the lock
36240Sstevel@tonic-gate 		 * as to avoid deadlocking with someone else who is
36250Sstevel@tonic-gate 		 * waiting for writer lock. With the lock gone, we
36260Sstevel@tonic-gate 		 * cannot be sure the checks done above will hold
36270Sstevel@tonic-gate 		 * good when we eventually get them back as writer.
36280Sstevel@tonic-gate 		 * So if we can't upgrade we drop the locks and retry
36290Sstevel@tonic-gate 		 * everything again.
36300Sstevel@tonic-gate 		 */
36310Sstevel@tonic-gate 		rw_exit(&tdp->i_rwlock);
36320Sstevel@tonic-gate 		if (tdp != sdp)
36330Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
36340Sstevel@tonic-gate 		delay(ufs_rename_backoff_delay);
36350Sstevel@tonic-gate 		ufs_rename_upgrade_retry_cnt++;
36360Sstevel@tonic-gate 		goto retry;
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 	if (tdp != sdp) {
36390Sstevel@tonic-gate 		if (!rw_tryupgrade(&sdp->i_rwlock)) {
36400Sstevel@tonic-gate 			/*
36410Sstevel@tonic-gate 			 * The upgrade failed. We got to give away the lock
36420Sstevel@tonic-gate 			 * as to avoid deadlocking with someone else who is
36430Sstevel@tonic-gate 			 * waiting for writer lock. With the lock gone, we
36440Sstevel@tonic-gate 			 * cannot be sure the checks done above will hold
36450Sstevel@tonic-gate 			 * good when we eventually get them back as writer.
36460Sstevel@tonic-gate 			 * So if we can't upgrade we drop the locks and retry
36470Sstevel@tonic-gate 			 * everything again.
36480Sstevel@tonic-gate 			 */
36490Sstevel@tonic-gate 			rw_exit(&tdp->i_rwlock);
36500Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
36510Sstevel@tonic-gate 			delay(ufs_rename_backoff_delay);
36520Sstevel@tonic-gate 			ufs_rename_upgrade_retry_cnt++;
36530Sstevel@tonic-gate 			goto retry;
36540Sstevel@tonic-gate 		}
36550Sstevel@tonic-gate 	}
3656613Sswilcox 
3657613Sswilcox 	/*
3658613Sswilcox 	 * Now that all the locks are held check to make sure another thread
3659613Sswilcox 	 * didn't slip in and take out the sip.
3660613Sswilcox 	 */
3661613Sswilcox 	slot.status = NONE;
3662613Sswilcox 	if ((sip->i_ctime.tv_usec * 1000) > now.tv_nsec ||
3663613Sswilcox 	    sip->i_ctime.tv_sec > now.tv_sec) {
3664613Sswilcox 		rw_enter(&sdp->i_ufsvfs->vfs_dqrwlock, RW_READER);
3665613Sswilcox 		rw_enter(&sdp->i_contents, RW_WRITER);
3666613Sswilcox 		error = ufs_dircheckforname(sdp, snm, strlen(snm), &slot,
3667613Sswilcox 		    &ip, cr, 0);
3668613Sswilcox 		rw_exit(&sdp->i_contents);
3669613Sswilcox 		rw_exit(&sdp->i_ufsvfs->vfs_dqrwlock);
3670613Sswilcox 		if (error) {
3671613Sswilcox 			goto errout;
3672613Sswilcox 		}
3673613Sswilcox 		if (ip == NULL) {
3674613Sswilcox 			error = ENOENT;
3675613Sswilcox 			goto errout;
3676613Sswilcox 		} else {
3677613Sswilcox 			/*
3678613Sswilcox 			 * If the inode was found need to drop the v_count
3679613Sswilcox 			 * so as not to keep the filesystem from being
3680613Sswilcox 			 * unmounted at a later time.
3681613Sswilcox 			 */
3682613Sswilcox 			VN_RELE(ITOV(ip));
3683613Sswilcox 		}
3684613Sswilcox 
3685613Sswilcox 		/*
3686613Sswilcox 		 * Release the slot.fbp that has the page mapped and
3687613Sswilcox 		 * locked SE_SHARED, and could be used in in
3688613Sswilcox 		 * ufs_direnter_lr() which needs to get the SE_EXCL lock
3689613Sswilcox 		 * on said page.
3690613Sswilcox 		 */
3691613Sswilcox 		if (slot.fbp) {
3692613Sswilcox 			fbrelse(slot.fbp, S_OTHER);
3693613Sswilcox 			slot.fbp = NULL;
3694613Sswilcox 		}
3695613Sswilcox 	}
3696613Sswilcox 
36970Sstevel@tonic-gate 	/*
3698*12170SOwen.Roberts@Sun.Com 	 * Link source to the target.
36990Sstevel@tonic-gate 	 */
3700*12170SOwen.Roberts@Sun.Com 	if (error = ufs_direnter_lr(tdp, tnm, DE_RENAME, sdp, sip, cr)) {
37010Sstevel@tonic-gate 		/*
37020Sstevel@tonic-gate 		 * ESAME isn't really an error; it indicates that the
37030Sstevel@tonic-gate 		 * operation should not be done because the source and target
37040Sstevel@tonic-gate 		 * are the same file, but that no error should be reported.
37050Sstevel@tonic-gate 		 */
37060Sstevel@tonic-gate 		if (error == ESAME)
37070Sstevel@tonic-gate 			error = 0;
37080Sstevel@tonic-gate 		goto errout;
37090Sstevel@tonic-gate 	}
37100Sstevel@tonic-gate 
37110Sstevel@tonic-gate 	/*
37120Sstevel@tonic-gate 	 * Unlink the source.
37130Sstevel@tonic-gate 	 * Remove the source entry.  ufs_dirremove() checks that the entry
37140Sstevel@tonic-gate 	 * still reflects sip, and returns an error if it doesn't.
37150Sstevel@tonic-gate 	 * If the entry has changed just forget about it.  Release
37160Sstevel@tonic-gate 	 * the source inode.
37170Sstevel@tonic-gate 	 */
37180Sstevel@tonic-gate 	if ((error = ufs_dirremove(sdp, snm, sip, (struct vnode *)0,
3719*12170SOwen.Roberts@Sun.Com 	    DR_RENAME, cr)) == ENOENT)
37200Sstevel@tonic-gate 		error = 0;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate errout:
3723613Sswilcox 	if (slot.fbp)
3724613Sswilcox 		fbrelse(slot.fbp, S_OTHER);
3725613Sswilcox 
37260Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
37270Sstevel@tonic-gate 	if (sdp != tdp) {
37280Sstevel@tonic-gate 		rw_exit(&sdp->i_rwlock);
37290Sstevel@tonic-gate 	}
37300Sstevel@tonic-gate 
3731*12170SOwen.Roberts@Sun.Com 	VN_RELE(ITOV(sip));
3732*12170SOwen.Roberts@Sun.Com 
37330Sstevel@tonic-gate unlock:
37340Sstevel@tonic-gate 	if (ulp) {
37350Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME, trans_size);
37360Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
37370Sstevel@tonic-gate 	}
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate out:
37400Sstevel@tonic-gate 	return (error);
37410Sstevel@tonic-gate }
37420Sstevel@tonic-gate 
37430Sstevel@tonic-gate /*ARGSUSED*/
37440Sstevel@tonic-gate static int
ufs_mkdir(struct vnode * dvp,char * dirname,struct vattr * vap,struct vnode ** vpp,struct cred * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)37450Sstevel@tonic-gate ufs_mkdir(struct vnode *dvp, char *dirname, struct vattr *vap,
37465331Samw 	struct vnode **vpp, struct cred *cr, caller_context_t *ct, int flags,
37475331Samw 	vsecattr_t *vsecp)
37480Sstevel@tonic-gate {
37490Sstevel@tonic-gate 	struct inode *ip;
37500Sstevel@tonic-gate 	struct inode *xip;
37510Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
37520Sstevel@tonic-gate 	struct ulockfs *ulp;
37530Sstevel@tonic-gate 	int error;
37540Sstevel@tonic-gate 	int issync;
37550Sstevel@tonic-gate 	int trans_size;
37561899Svsakar 	int indeadlock;
37570Sstevel@tonic-gate 	int retry = 1;
37580Sstevel@tonic-gate 
37590Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 	/*
37620Sstevel@tonic-gate 	 * Can't make directory in attr hidden dir
37630Sstevel@tonic-gate 	 */
37640Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
37650Sstevel@tonic-gate 		return (EINVAL);
37660Sstevel@tonic-gate 
37670Sstevel@tonic-gate again:
37680Sstevel@tonic-gate 	ip = VTOI(dvp);
37690Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
37700Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MKDIR_MASK);
37710Sstevel@tonic-gate 	if (error)
37720Sstevel@tonic-gate 		goto out;
37730Sstevel@tonic-gate 	if (ulp)
37740Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_MKDIR,
37750Sstevel@tonic-gate 		    trans_size = (int)TOP_MKDIR_SIZE(ip));
37760Sstevel@tonic-gate 
37771899Svsakar 	/*
37781899Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
37791899Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
37801899Svsakar 	 * possible, retries the operation.
37811899Svsakar 	 */
37821899Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_MKDIR, retry);
37831899Svsakar 	if (indeadlock)
37841899Svsakar 		goto again;
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	error = ufs_direnter_cm(ip, dirname, DE_MKDIR, vap, &xip, cr,
37874662Sfrankho 	    (retry ? IQUIET : 0));
37881899Svsakar 	if (error == EAGAIN) {
37891899Svsakar 		if (ulp) {
37901899Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_MKDIR,
37914662Sfrankho 			    trans_size);
37921899Svsakar 			ufs_lockfs_end(ulp);
37931899Svsakar 		}
37941899Svsakar 		goto again;
37951899Svsakar 	}
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
37980Sstevel@tonic-gate 	if (error == 0) {
37990Sstevel@tonic-gate 		ip = xip;
38000Sstevel@tonic-gate 		*vpp = ITOV(ip);
38010Sstevel@tonic-gate 	} else if (error == EEXIST)
38020Sstevel@tonic-gate 		VN_RELE(ITOV(xip));
38030Sstevel@tonic-gate 
38040Sstevel@tonic-gate 	if (ulp) {
38050Sstevel@tonic-gate 		int terr = 0;
38060Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_MKDIR, trans_size);
38070Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
38080Sstevel@tonic-gate 		if (error == 0)
38090Sstevel@tonic-gate 			error = terr;
38100Sstevel@tonic-gate 	}
38110Sstevel@tonic-gate out:
38120Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
38130Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
38140Sstevel@tonic-gate 		retry = 0;
38150Sstevel@tonic-gate 		goto again;
38160Sstevel@tonic-gate 	}
38170Sstevel@tonic-gate 
38180Sstevel@tonic-gate 	return (error);
38190Sstevel@tonic-gate }
38200Sstevel@tonic-gate 
38210Sstevel@tonic-gate /*ARGSUSED*/
38220Sstevel@tonic-gate static int
ufs_rmdir(struct vnode * vp,char * nm,struct vnode * cdir,struct cred * cr,caller_context_t * ct,int flags)38235331Samw ufs_rmdir(struct vnode *vp, char *nm, struct vnode *cdir, struct cred *cr,
38245331Samw 	caller_context_t *ct, int flags)
38250Sstevel@tonic-gate {
38260Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
38270Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
38280Sstevel@tonic-gate 	struct ulockfs *ulp;
38290Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode of removed directory */
38300Sstevel@tonic-gate 	int error;
38310Sstevel@tonic-gate 	int issync;
38321899Svsakar 	int trans_size;
38331899Svsakar 	int indeadlock;
38340Sstevel@tonic-gate 
38350Sstevel@tonic-gate 	/*
38360Sstevel@tonic-gate 	 * don't let the delete queue get too long
38370Sstevel@tonic-gate 	 */
38380Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
38390Sstevel@tonic-gate 		error = EIO;
38400Sstevel@tonic-gate 		goto out;
38410Sstevel@tonic-gate 	}
38420Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
38430Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
38440Sstevel@tonic-gate 
3845*12170SOwen.Roberts@Sun.Com 	error = ufs_eventlookup(vp, nm, cr, &rmvp);
3846*12170SOwen.Roberts@Sun.Com 	if (rmvp != NULL) {
3847*12170SOwen.Roberts@Sun.Com 		/* Only send the event if there were no errors */
3848*12170SOwen.Roberts@Sun.Com 		if (error == 0)
3849*12170SOwen.Roberts@Sun.Com 			vnevent_rmdir(rmvp, vp, nm, ct);
3850*12170SOwen.Roberts@Sun.Com 		VN_RELE(rmvp);
3851*12170SOwen.Roberts@Sun.Com 	}
3852*12170SOwen.Roberts@Sun.Com 
38531899Svsakar retry_rmdir:
38540Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RMDIR_MASK);
38550Sstevel@tonic-gate 	if (error)
38560Sstevel@tonic-gate 		goto out;
38570Sstevel@tonic-gate 
38580Sstevel@tonic-gate 	if (ulp)
38591899Svsakar 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RMDIR,
38604662Sfrankho 		    trans_size = TOP_RMDIR_SIZE);
38611899Svsakar 
38621899Svsakar 	/*
38631899Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
38641899Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
38651899Svsakar 	 * possible, retries the operation.
38661899Svsakar 	 */
38671899Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_RMDIR, retry);
38681899Svsakar 	if (indeadlock)
38691899Svsakar 		goto retry_rmdir;
3870*12170SOwen.Roberts@Sun.Com 	error = ufs_dirremove(ip, nm, (struct inode *)0, cdir, DR_RMDIR, cr);
3871*12170SOwen.Roberts@Sun.Com 
38720Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate 	if (ulp) {
38750Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RMDIR,
38764662Sfrankho 		    trans_size);
38770Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
38780Sstevel@tonic-gate 	}
38790Sstevel@tonic-gate 
38800Sstevel@tonic-gate out:
38810Sstevel@tonic-gate 	return (error);
38820Sstevel@tonic-gate }
38830Sstevel@tonic-gate 
38840Sstevel@tonic-gate /* ARGSUSED */
38850Sstevel@tonic-gate static int
ufs_readdir(struct vnode * vp,struct uio * uiop,struct cred * cr,int * eofp,caller_context_t * ct,int flags)38860Sstevel@tonic-gate ufs_readdir(
38870Sstevel@tonic-gate 	struct vnode *vp,
38880Sstevel@tonic-gate 	struct uio *uiop,
38890Sstevel@tonic-gate 	struct cred *cr,
38905331Samw 	int *eofp,
38915331Samw 	caller_context_t *ct,
38925331Samw 	int flags)
38930Sstevel@tonic-gate {
38940Sstevel@tonic-gate 	struct iovec *iovp;
38950Sstevel@tonic-gate 	struct inode *ip;
38960Sstevel@tonic-gate 	struct direct *idp;
38970Sstevel@tonic-gate 	struct dirent64 *odp;
38980Sstevel@tonic-gate 	struct fbuf *fbp;
38990Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
39000Sstevel@tonic-gate 	struct ulockfs *ulp;
39010Sstevel@tonic-gate 	caddr_t outbuf;
39020Sstevel@tonic-gate 	size_t bufsize;
39030Sstevel@tonic-gate 	uint_t offset;
39040Sstevel@tonic-gate 	uint_t bytes_wanted, total_bytes_wanted;
39050Sstevel@tonic-gate 	int incount = 0;
39060Sstevel@tonic-gate 	int outcount = 0;
39070Sstevel@tonic-gate 	int error;
39080Sstevel@tonic-gate 
39090Sstevel@tonic-gate 	ip = VTOI(vp);
39100Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
39110Sstevel@tonic-gate 
39120Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF32_T) {
39130Sstevel@tonic-gate 		if (eofp)
39140Sstevel@tonic-gate 			*eofp = 1;
39150Sstevel@tonic-gate 		return (0);
39160Sstevel@tonic-gate 	}
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate 	/*
39190Sstevel@tonic-gate 	 * Check if we have been called with a valid iov_len
39200Sstevel@tonic-gate 	 * and bail out if not, otherwise we may potentially loop
39210Sstevel@tonic-gate 	 * forever further down.
39220Sstevel@tonic-gate 	 */
39230Sstevel@tonic-gate 	if (uiop->uio_iov->iov_len <= 0) {
39240Sstevel@tonic-gate 		error = EINVAL;
39250Sstevel@tonic-gate 		goto out;
39260Sstevel@tonic-gate 	}
39270Sstevel@tonic-gate 
39280Sstevel@tonic-gate 	/*
39290Sstevel@tonic-gate 	 * Large Files: When we come here we are guaranteed that
39300Sstevel@tonic-gate 	 * uio_offset can be used safely. The high word is zero.
39310Sstevel@tonic-gate 	 */
39320Sstevel@tonic-gate 
39330Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
39340Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READDIR_MASK);
39350Sstevel@tonic-gate 	if (error)
39360Sstevel@tonic-gate 		goto out;
39370Sstevel@tonic-gate 
39380Sstevel@tonic-gate 	iovp = uiop->uio_iov;
39390Sstevel@tonic-gate 	total_bytes_wanted = iovp->iov_len;
39400Sstevel@tonic-gate 
39410Sstevel@tonic-gate 	/* Large Files: directory files should not be "large" */
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate 	ASSERT(ip->i_size <= MAXOFF32_T);
39440Sstevel@tonic-gate 
39450Sstevel@tonic-gate 	/* Force offset to be valid (to guard against bogus lseek() values) */
39460Sstevel@tonic-gate 	offset = (uint_t)uiop->uio_offset & ~(DIRBLKSIZ - 1);
39470Sstevel@tonic-gate 
39480Sstevel@tonic-gate 	/* Quit if at end of file or link count of zero (posix) */
39490Sstevel@tonic-gate 	if (offset >= (uint_t)ip->i_size || ip->i_nlink <= 0) {
39500Sstevel@tonic-gate 		if (eofp)
39510Sstevel@tonic-gate 			*eofp = 1;
39520Sstevel@tonic-gate 		error = 0;
39530Sstevel@tonic-gate 		goto unlock;
39540Sstevel@tonic-gate 	}
39550Sstevel@tonic-gate 
39560Sstevel@tonic-gate 	/*
39570Sstevel@tonic-gate 	 * Get space to change directory entries into fs independent format.
39580Sstevel@tonic-gate 	 * Do fast alloc for the most commonly used-request size (filesystem
39590Sstevel@tonic-gate 	 * block size).
39600Sstevel@tonic-gate 	 */
39610Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1) {
39620Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
39630Sstevel@tonic-gate 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
39640Sstevel@tonic-gate 		odp = (struct dirent64 *)outbuf;
39650Sstevel@tonic-gate 	} else {
39660Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
39670Sstevel@tonic-gate 		odp = (struct dirent64 *)iovp->iov_base;
39680Sstevel@tonic-gate 	}
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate nextblk:
39710Sstevel@tonic-gate 	bytes_wanted = total_bytes_wanted;
39720Sstevel@tonic-gate 
39730Sstevel@tonic-gate 	/* Truncate request to file size */
39740Sstevel@tonic-gate 	if (offset + bytes_wanted > (int)ip->i_size)
39750Sstevel@tonic-gate 		bytes_wanted = (int)(ip->i_size - offset);
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 	/* Comply with MAXBSIZE boundary restrictions of fbread() */
39780Sstevel@tonic-gate 	if ((offset & MAXBOFFSET) + bytes_wanted > MAXBSIZE)
39790Sstevel@tonic-gate 		bytes_wanted = MAXBSIZE - (offset & MAXBOFFSET);
39800Sstevel@tonic-gate 
39810Sstevel@tonic-gate 	/*
39820Sstevel@tonic-gate 	 * Read in the next chunk.
39830Sstevel@tonic-gate 	 * We are still holding the i_rwlock.
39840Sstevel@tonic-gate 	 */
39850Sstevel@tonic-gate 	error = fbread(vp, (offset_t)offset, bytes_wanted, S_OTHER, &fbp);
39860Sstevel@tonic-gate 
39870Sstevel@tonic-gate 	if (error)
39880Sstevel@tonic-gate 		goto update_inode;
39890Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (ip->i_fs->fs_ronly == 0) &&
39900Sstevel@tonic-gate 	    (!ufsvfsp->vfs_noatime)) {
39910Sstevel@tonic-gate 		ip->i_flag |= IACC;
39920Sstevel@tonic-gate 	}
39930Sstevel@tonic-gate 	incount = 0;
39940Sstevel@tonic-gate 	idp = (struct direct *)fbp->fb_addr;
39954662Sfrankho 	if (idp->d_ino == 0 && idp->d_reclen == 0 && idp->d_namlen == 0) {
39960Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufs_readdir: bad dir, inumber = %llu, "
39974662Sfrankho 		    "fs = %s\n",
39984662Sfrankho 		    (u_longlong_t)ip->i_number, ufsvfsp->vfs_fs->fs_fsmnt);
39990Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
40000Sstevel@tonic-gate 		error = ENXIO;
40010Sstevel@tonic-gate 		goto update_inode;
40020Sstevel@tonic-gate 	}
40030Sstevel@tonic-gate 	/* Transform to file-system independent format */
40040Sstevel@tonic-gate 	while (incount < bytes_wanted) {
40050Sstevel@tonic-gate 		/*
40060Sstevel@tonic-gate 		 * If the current directory entry is mangled, then skip
40070Sstevel@tonic-gate 		 * to the next block.  It would be nice to set the FSBAD
40080Sstevel@tonic-gate 		 * flag in the super-block so that a fsck is forced on
40090Sstevel@tonic-gate 		 * next reboot, but locking is a problem.
40100Sstevel@tonic-gate 		 */
40110Sstevel@tonic-gate 		if (idp->d_reclen & 0x3) {
40120Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
40130Sstevel@tonic-gate 			break;
40140Sstevel@tonic-gate 		}
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 		/* Skip to requested offset and skip empty entries */
40170Sstevel@tonic-gate 		if (idp->d_ino != 0 && offset >= (uint_t)uiop->uio_offset) {
40180Sstevel@tonic-gate 			ushort_t this_reclen =
40190Sstevel@tonic-gate 			    DIRENT64_RECLEN(idp->d_namlen);
40200Sstevel@tonic-gate 			/* Buffer too small for any entries */
40210Sstevel@tonic-gate 			if (!outcount && this_reclen > bufsize) {
40220Sstevel@tonic-gate 				fbrelse(fbp, S_OTHER);
40230Sstevel@tonic-gate 				error = EINVAL;
40240Sstevel@tonic-gate 				goto update_inode;
40250Sstevel@tonic-gate 			}
40260Sstevel@tonic-gate 			/* If would overrun the buffer, quit */
40270Sstevel@tonic-gate 			if (outcount + this_reclen > bufsize) {
40280Sstevel@tonic-gate 				break;
40290Sstevel@tonic-gate 			}
40300Sstevel@tonic-gate 			/* Take this entry */
40310Sstevel@tonic-gate 			odp->d_ino = (ino64_t)idp->d_ino;
40320Sstevel@tonic-gate 			odp->d_reclen = (ushort_t)this_reclen;
40330Sstevel@tonic-gate 			odp->d_off = (offset_t)(offset + idp->d_reclen);
40340Sstevel@tonic-gate 
40350Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
40360Sstevel@tonic-gate 
40370Sstevel@tonic-gate 			ASSERT(strlen(idp->d_name) + 1 <=
40380Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
40390Sstevel@tonic-gate 			(void) strncpy(odp->d_name, idp->d_name,
40400Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
40410Sstevel@tonic-gate 			outcount += odp->d_reclen;
40424662Sfrankho 			odp = (struct dirent64 *)
40434662Sfrankho 			    ((intptr_t)odp + odp->d_reclen);
40440Sstevel@tonic-gate 			ASSERT(outcount <= bufsize);
40450Sstevel@tonic-gate 		}
40460Sstevel@tonic-gate 		if (idp->d_reclen) {
40470Sstevel@tonic-gate 			incount += idp->d_reclen;
40480Sstevel@tonic-gate 			offset += idp->d_reclen;
40490Sstevel@tonic-gate 			idp = (struct direct *)((intptr_t)idp + idp->d_reclen);
40500Sstevel@tonic-gate 		} else {
40510Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
40520Sstevel@tonic-gate 			break;
40530Sstevel@tonic-gate 		}
40540Sstevel@tonic-gate 	}
40550Sstevel@tonic-gate 	/* Release the chunk */
40560Sstevel@tonic-gate 	fbrelse(fbp, S_OTHER);
40570Sstevel@tonic-gate 
40580Sstevel@tonic-gate 	/* Read whole block, but got no entries, read another if not eof */
40590Sstevel@tonic-gate 
40600Sstevel@tonic-gate 	/*
40610Sstevel@tonic-gate 	 * Large Files: casting i_size to int here is not a problem
40620Sstevel@tonic-gate 	 * because directory sizes are always less than MAXOFF32_T.
40630Sstevel@tonic-gate 	 * See assertion above.
40640Sstevel@tonic-gate 	 */
40650Sstevel@tonic-gate 
40660Sstevel@tonic-gate 	if (offset < (int)ip->i_size && !outcount)
40670Sstevel@tonic-gate 		goto nextblk;
40680Sstevel@tonic-gate 
40690Sstevel@tonic-gate 	/* Copy out the entry data */
40700Sstevel@tonic-gate 	if (uiop->uio_segflg == UIO_SYSSPACE && uiop->uio_iovcnt == 1) {
40710Sstevel@tonic-gate 		iovp->iov_base += outcount;
40720Sstevel@tonic-gate 		iovp->iov_len -= outcount;
40730Sstevel@tonic-gate 		uiop->uio_resid -= outcount;
40740Sstevel@tonic-gate 		uiop->uio_offset = offset;
40750Sstevel@tonic-gate 	} else if ((error = uiomove(outbuf, (long)outcount, UIO_READ,
40764662Sfrankho 	    uiop)) == 0)
40770Sstevel@tonic-gate 		uiop->uio_offset = offset;
40780Sstevel@tonic-gate update_inode:
40790Sstevel@tonic-gate 	ITIMES(ip);
40800Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1)
40810Sstevel@tonic-gate 		kmem_free(outbuf, bufsize);
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	if (eofp && error == 0)
40840Sstevel@tonic-gate 		*eofp = (uiop->uio_offset >= (int)ip->i_size);
40850Sstevel@tonic-gate unlock:
40860Sstevel@tonic-gate 	if (ulp) {
40870Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
40880Sstevel@tonic-gate 	}
40890Sstevel@tonic-gate out:
40900Sstevel@tonic-gate 	return (error);
40910Sstevel@tonic-gate }
40920Sstevel@tonic-gate 
40930Sstevel@tonic-gate /*ARGSUSED*/
40940Sstevel@tonic-gate static int
ufs_symlink(struct vnode * dvp,char * linkname,struct vattr * vap,char * target,struct cred * cr,caller_context_t * ct,int flags)40950Sstevel@tonic-gate ufs_symlink(
40960Sstevel@tonic-gate 	struct vnode *dvp,		/* ptr to parent dir vnode */
40970Sstevel@tonic-gate 	char *linkname,			/* name of symbolic link */
40980Sstevel@tonic-gate 	struct vattr *vap,		/* attributes */
40990Sstevel@tonic-gate 	char *target,			/* target path */
41005331Samw 	struct cred *cr,		/* user credentials */
41015331Samw 	caller_context_t *ct,
41025331Samw 	int flags)
41030Sstevel@tonic-gate {
41040Sstevel@tonic-gate 	struct inode *ip, *dip = VTOI(dvp);
41050Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = dip->i_ufsvfs;
41060Sstevel@tonic-gate 	struct ulockfs *ulp;
41070Sstevel@tonic-gate 	int error;
41080Sstevel@tonic-gate 	int issync;
41090Sstevel@tonic-gate 	int trans_size;
41100Sstevel@tonic-gate 	int residual;
41110Sstevel@tonic-gate 	int ioflag;
41120Sstevel@tonic-gate 	int retry = 1;
41130Sstevel@tonic-gate 
41140Sstevel@tonic-gate 	/*
41150Sstevel@tonic-gate 	 * No symlinks in attrdirs at this time
41160Sstevel@tonic-gate 	 */
41170Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
41180Sstevel@tonic-gate 		return (EINVAL);
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate again:
41210Sstevel@tonic-gate 	ip = (struct inode *)NULL;
41220Sstevel@tonic-gate 	vap->va_type = VLNK;
41230Sstevel@tonic-gate 	vap->va_rdev = 0;
41240Sstevel@tonic-gate 
41250Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SYMLINK_MASK);
41260Sstevel@tonic-gate 	if (error)
41270Sstevel@tonic-gate 		goto out;
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate 	if (ulp)
41300Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SYMLINK,
41310Sstevel@tonic-gate 		    trans_size = (int)TOP_SYMLINK_SIZE(dip));
41320Sstevel@tonic-gate 
41330Sstevel@tonic-gate 	/*
41340Sstevel@tonic-gate 	 * We must create the inode before the directory entry, to avoid
41350Sstevel@tonic-gate 	 * racing with readlink().  ufs_dirmakeinode requires that we
41360Sstevel@tonic-gate 	 * hold the quota lock as reader, and directory locks as writer.
41370Sstevel@tonic-gate 	 */
41380Sstevel@tonic-gate 
41390Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
41400Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
41410Sstevel@tonic-gate 	rw_enter(&dip->i_contents, RW_WRITER);
41420Sstevel@tonic-gate 
41430Sstevel@tonic-gate 	/*
41440Sstevel@tonic-gate 	 * Suppress any out of inodes messages if we will retry on
41450Sstevel@tonic-gate 	 * ENOSP
41460Sstevel@tonic-gate 	 */
41470Sstevel@tonic-gate 	if (retry)
41480Sstevel@tonic-gate 		dip->i_flag |= IQUIET;
41490Sstevel@tonic-gate 
41500Sstevel@tonic-gate 	error = ufs_dirmakeinode(dip, &ip, vap, DE_SYMLINK, cr);
41510Sstevel@tonic-gate 
41520Sstevel@tonic-gate 	dip->i_flag &= ~IQUIET;
41530Sstevel@tonic-gate 
41540Sstevel@tonic-gate 	rw_exit(&dip->i_contents);
41550Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
41560Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
41570Sstevel@tonic-gate 
41580Sstevel@tonic-gate 	if (error)
41590Sstevel@tonic-gate 		goto unlock;
41600Sstevel@tonic-gate 
41610Sstevel@tonic-gate 	/*
41620Sstevel@tonic-gate 	 * OK.  The inode has been created.  Write out the data of the
41630Sstevel@tonic-gate 	 * symbolic link.  Since symbolic links are metadata, and should
41640Sstevel@tonic-gate 	 * remain consistent across a system crash, we need to force the
41650Sstevel@tonic-gate 	 * data out synchronously.
41660Sstevel@tonic-gate 	 *
41670Sstevel@tonic-gate 	 * (This is a change from the semantics in earlier releases, which
41680Sstevel@tonic-gate 	 * only created symbolic links synchronously if the semi-documented
41690Sstevel@tonic-gate 	 * 'syncdir' option was set, or if we were being invoked by the NFS
41700Sstevel@tonic-gate 	 * server, which requires symbolic links to be created synchronously.)
41710Sstevel@tonic-gate 	 *
41720Sstevel@tonic-gate 	 * We need to pass in a pointer for the residual length; otherwise
41730Sstevel@tonic-gate 	 * ufs_rdwri() will always return EIO if it can't write the data,
41740Sstevel@tonic-gate 	 * even if the error was really ENOSPC or EDQUOT.
41750Sstevel@tonic-gate 	 */
41760Sstevel@tonic-gate 
41770Sstevel@tonic-gate 	ioflag = FWRITE | FDSYNC;
41780Sstevel@tonic-gate 	residual = 0;
41790Sstevel@tonic-gate 
41800Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
41810Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
41820Sstevel@tonic-gate 
41830Sstevel@tonic-gate 	/*
41840Sstevel@tonic-gate 	 * Suppress file system full messages if we will retry
41850Sstevel@tonic-gate 	 */
41860Sstevel@tonic-gate 	if (retry)
41870Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
41880Sstevel@tonic-gate 
41890Sstevel@tonic-gate 	error = ufs_rdwri(UIO_WRITE, ioflag, ip, target, strlen(target),
41900Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, &residual, cr);
41910Sstevel@tonic-gate 
41920Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
41930Sstevel@tonic-gate 
41940Sstevel@tonic-gate 	if (error) {
41950Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
41960Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
41970Sstevel@tonic-gate 		goto remove;
41980Sstevel@tonic-gate 	}
41990Sstevel@tonic-gate 
42000Sstevel@tonic-gate 	/*
42010Sstevel@tonic-gate 	 * If the link's data is small enough, we can cache it in the inode.
42020Sstevel@tonic-gate 	 * This is a "fast symbolic link".  We don't use the first direct
42030Sstevel@tonic-gate 	 * block because that's actually used to point at the symbolic link's
42040Sstevel@tonic-gate 	 * contents on disk; but we know that none of the other direct or
42050Sstevel@tonic-gate 	 * indirect blocks can be used because symbolic links are restricted
42060Sstevel@tonic-gate 	 * to be smaller than a file system block.
42070Sstevel@tonic-gate 	 */
42080Sstevel@tonic-gate 
42090Sstevel@tonic-gate 	ASSERT(MAXPATHLEN <= VBSIZE(ITOV(ip)));
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate 	if (ip->i_size > 0 && ip->i_size <= FSL_SIZE) {
42120Sstevel@tonic-gate 		if (kcopy(target, &ip->i_db[1], ip->i_size) == 0) {
42130Sstevel@tonic-gate 			ip->i_flag |= IFASTSYMLNK;
42140Sstevel@tonic-gate 		} else {
42150Sstevel@tonic-gate 			int i;
42160Sstevel@tonic-gate 			/* error, clear garbage left behind */
42170Sstevel@tonic-gate 			for (i = 1; i < NDADDR; i++)
42180Sstevel@tonic-gate 				ip->i_db[i] = 0;
42190Sstevel@tonic-gate 			for (i = 0; i < NIADDR; i++)
42200Sstevel@tonic-gate 				ip->i_ib[i] = 0;
42210Sstevel@tonic-gate 		}
42220Sstevel@tonic-gate 	}
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
42250Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
42260Sstevel@tonic-gate 
42270Sstevel@tonic-gate 	/*
42280Sstevel@tonic-gate 	 * OK.  We've successfully created the symbolic link.  All that
42290Sstevel@tonic-gate 	 * remains is to insert it into the appropriate directory.
42300Sstevel@tonic-gate 	 */
42310Sstevel@tonic-gate 
42320Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
4233*12170SOwen.Roberts@Sun.Com 	error = ufs_direnter_lr(dip, linkname, DE_SYMLINK, NULL, ip, cr);
42340Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
42350Sstevel@tonic-gate 
42360Sstevel@tonic-gate 	/*
42370Sstevel@tonic-gate 	 * Fall through into remove-on-error code.  We're either done, or we
42380Sstevel@tonic-gate 	 * need to remove the inode (if we couldn't insert it).
42390Sstevel@tonic-gate 	 */
42400Sstevel@tonic-gate 
42410Sstevel@tonic-gate remove:
42420Sstevel@tonic-gate 	if (error && (ip != NULL)) {
42430Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
42440Sstevel@tonic-gate 		ip->i_nlink--;
42450Sstevel@tonic-gate 		ip->i_flag |= ICHG;
42460Sstevel@tonic-gate 		ip->i_seq++;
42470Sstevel@tonic-gate 		ufs_setreclaim(ip);
42480Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
42490Sstevel@tonic-gate 	}
42500Sstevel@tonic-gate 
42510Sstevel@tonic-gate unlock:
42520Sstevel@tonic-gate 	if (ip != NULL)
42530Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
42540Sstevel@tonic-gate 
42550Sstevel@tonic-gate 	if (ulp) {
42560Sstevel@tonic-gate 		int terr = 0;
42570Sstevel@tonic-gate 
42580Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SYMLINK,
42594662Sfrankho 		    trans_size);
42600Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
42610Sstevel@tonic-gate 		if (error == 0)
42620Sstevel@tonic-gate 			error = terr;
42630Sstevel@tonic-gate 	}
42640Sstevel@tonic-gate 
42650Sstevel@tonic-gate 	/*
42660Sstevel@tonic-gate 	 * We may have failed due to lack of an inode or of a block to
42670Sstevel@tonic-gate 	 * store the target in.  Try flushing the delete queue to free
42680Sstevel@tonic-gate 	 * logically-available things up and try again.
42690Sstevel@tonic-gate 	 */
42700Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
42710Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
42720Sstevel@tonic-gate 		retry = 0;
42730Sstevel@tonic-gate 		goto again;
42740Sstevel@tonic-gate 	}
42750Sstevel@tonic-gate 
42760Sstevel@tonic-gate out:
42770Sstevel@tonic-gate 	return (error);
42780Sstevel@tonic-gate }
42790Sstevel@tonic-gate 
42800Sstevel@tonic-gate /*
42810Sstevel@tonic-gate  * Ufs specific routine used to do ufs io.
42820Sstevel@tonic-gate  */
42830Sstevel@tonic-gate int
ufs_rdwri(enum uio_rw rw,int ioflag,struct inode * ip,caddr_t base,ssize_t len,offset_t offset,enum uio_seg seg,int * aresid,struct cred * cr)42840Sstevel@tonic-gate ufs_rdwri(enum uio_rw rw, int ioflag, struct inode *ip, caddr_t base,
42850Sstevel@tonic-gate 	ssize_t len, offset_t offset, enum uio_seg seg, int *aresid,
42860Sstevel@tonic-gate 	struct cred *cr)
42870Sstevel@tonic-gate {
42880Sstevel@tonic-gate 	struct uio auio;
42890Sstevel@tonic-gate 	struct iovec aiov;
42900Sstevel@tonic-gate 	int error;
42910Sstevel@tonic-gate 
42920Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
42930Sstevel@tonic-gate 
42940Sstevel@tonic-gate 	bzero((caddr_t)&auio, sizeof (uio_t));
42950Sstevel@tonic-gate 	bzero((caddr_t)&aiov, sizeof (iovec_t));
42960Sstevel@tonic-gate 
42970Sstevel@tonic-gate 	aiov.iov_base = base;
42980Sstevel@tonic-gate 	aiov.iov_len = len;
42990Sstevel@tonic-gate 	auio.uio_iov = &aiov;
43000Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
43010Sstevel@tonic-gate 	auio.uio_loffset = offset;
43020Sstevel@tonic-gate 	auio.uio_segflg = (short)seg;
43030Sstevel@tonic-gate 	auio.uio_resid = len;
43040Sstevel@tonic-gate 
43050Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
43060Sstevel@tonic-gate 		auio.uio_fmode = FWRITE;
43070Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
43080Sstevel@tonic-gate 		auio.uio_llimit = curproc->p_fsz_ctl;
43090Sstevel@tonic-gate 		error = wrip(ip, &auio, ioflag, cr);
43100Sstevel@tonic-gate 	} else {
43110Sstevel@tonic-gate 		auio.uio_fmode = FREAD;
43120Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
43130Sstevel@tonic-gate 		auio.uio_llimit = MAXOFFSET_T;
43140Sstevel@tonic-gate 		error = rdip(ip, &auio, ioflag, cr);
43150Sstevel@tonic-gate 	}
43160Sstevel@tonic-gate 
43170Sstevel@tonic-gate 	if (aresid) {
43180Sstevel@tonic-gate 		*aresid = auio.uio_resid;
43190Sstevel@tonic-gate 	} else if (auio.uio_resid) {
43200Sstevel@tonic-gate 		error = EIO;
43210Sstevel@tonic-gate 	}
43220Sstevel@tonic-gate 	return (error);
43230Sstevel@tonic-gate }
43240Sstevel@tonic-gate 
43255331Samw /*ARGSUSED*/
43260Sstevel@tonic-gate static int
ufs_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)43275331Samw ufs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
43280Sstevel@tonic-gate {
43290Sstevel@tonic-gate 	struct ufid *ufid;
43300Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
43310Sstevel@tonic-gate 
43320Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
43330Sstevel@tonic-gate 		return (EIO);
43340Sstevel@tonic-gate 
43350Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct ufid) - sizeof (ushort_t))) {
43360Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct ufid) - sizeof (ushort_t);
43370Sstevel@tonic-gate 		return (ENOSPC);
43380Sstevel@tonic-gate 	}
43390Sstevel@tonic-gate 
43400Sstevel@tonic-gate 	ufid = (struct ufid *)fidp;
43410Sstevel@tonic-gate 	bzero((char *)ufid, sizeof (struct ufid));
43420Sstevel@tonic-gate 	ufid->ufid_len = sizeof (struct ufid) - sizeof (ushort_t);
43430Sstevel@tonic-gate 	ufid->ufid_ino = ip->i_number;
43440Sstevel@tonic-gate 	ufid->ufid_gen = ip->i_gen;
43450Sstevel@tonic-gate 
43460Sstevel@tonic-gate 	return (0);
43470Sstevel@tonic-gate }
43480Sstevel@tonic-gate 
43490Sstevel@tonic-gate /* ARGSUSED2 */
43500Sstevel@tonic-gate static int
ufs_rwlock(struct vnode * vp,int write_lock,caller_context_t * ctp)43510Sstevel@tonic-gate ufs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
43520Sstevel@tonic-gate {
43530Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
43540Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
43550Sstevel@tonic-gate 	int		forcedirectio;
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 	/*
43580Sstevel@tonic-gate 	 * Read case is easy.
43590Sstevel@tonic-gate 	 */
43600Sstevel@tonic-gate 	if (!write_lock) {
43610Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
43620Sstevel@tonic-gate 		return (V_WRITELOCK_FALSE);
43630Sstevel@tonic-gate 	}
43640Sstevel@tonic-gate 
43650Sstevel@tonic-gate 	/*
43660Sstevel@tonic-gate 	 * Caller has requested a writer lock, but that inhibits any
43670Sstevel@tonic-gate 	 * concurrency in the VOPs that follow. Acquire the lock shared
43680Sstevel@tonic-gate 	 * and defer exclusive access until it is known to be needed in
43690Sstevel@tonic-gate 	 * other VOP handlers. Some cases can be determined here.
43700Sstevel@tonic-gate 	 */
43710Sstevel@tonic-gate 
43720Sstevel@tonic-gate 	/*
43730Sstevel@tonic-gate 	 * If directio is not set, there is no chance of concurrency,
43740Sstevel@tonic-gate 	 * so just acquire the lock exclusive. Beware of a forced
43750Sstevel@tonic-gate 	 * unmount before looking at the mount option.
43760Sstevel@tonic-gate 	 */
43770Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
43780Sstevel@tonic-gate 	forcedirectio = ufsvfsp ? ufsvfsp->vfs_forcedirectio : 0;
43790Sstevel@tonic-gate 	if (!(ip->i_flag & IDIRECTIO || forcedirectio) ||
43800Sstevel@tonic-gate 	    !ufs_allow_shared_writes) {
43810Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
43820Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
43830Sstevel@tonic-gate 	}
43840Sstevel@tonic-gate 
43850Sstevel@tonic-gate 	/*
43860Sstevel@tonic-gate 	 * Mandatory locking forces acquiring i_rwlock exclusive.
43870Sstevel@tonic-gate 	 */
43880Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
43890Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
43900Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
43910Sstevel@tonic-gate 	}
43920Sstevel@tonic-gate 
43930Sstevel@tonic-gate 	/*
43940Sstevel@tonic-gate 	 * Acquire the lock shared in case a concurrent write follows.
43950Sstevel@tonic-gate 	 * Mandatory locking could have become enabled before the lock
43960Sstevel@tonic-gate 	 * was acquired. Re-check and upgrade if needed.
43970Sstevel@tonic-gate 	 */
43980Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_READER);
43990Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
44000Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
44010Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
44020Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
44030Sstevel@tonic-gate 	}
44040Sstevel@tonic-gate 	return (V_WRITELOCK_FALSE);
44050Sstevel@tonic-gate }
44060Sstevel@tonic-gate 
44070Sstevel@tonic-gate /*ARGSUSED*/
44080Sstevel@tonic-gate static void
ufs_rwunlock(struct vnode * vp,int write_lock,caller_context_t * ctp)44090Sstevel@tonic-gate ufs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
44100Sstevel@tonic-gate {
44110Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
44120Sstevel@tonic-gate 
44130Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
44140Sstevel@tonic-gate }
44150Sstevel@tonic-gate 
44160Sstevel@tonic-gate /* ARGSUSED */
44170Sstevel@tonic-gate static int
ufs_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)44185331Samw ufs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
44195331Samw 	caller_context_t *ct)
44200Sstevel@tonic-gate {
44210Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
44220Sstevel@tonic-gate }
44230Sstevel@tonic-gate 
44240Sstevel@tonic-gate /* ARGSUSED */
44250Sstevel@tonic-gate static int
ufs_frlock(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,struct cred * cr,caller_context_t * ct)44260Sstevel@tonic-gate ufs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
44275331Samw 	offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
44285331Samw 	caller_context_t *ct)
44290Sstevel@tonic-gate {
44300Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
44310Sstevel@tonic-gate 
44320Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
44330Sstevel@tonic-gate 		return (EIO);
44340Sstevel@tonic-gate 
44350Sstevel@tonic-gate 	/*
44360Sstevel@tonic-gate 	 * If file is being mapped, disallow frlock.
44370Sstevel@tonic-gate 	 * XXX I am not holding tlock while checking i_mapcnt because the
44380Sstevel@tonic-gate 	 * current locking strategy drops all locks before calling fs_frlock.
44390Sstevel@tonic-gate 	 * So, mapcnt could change before we enter fs_frlock making is
44400Sstevel@tonic-gate 	 * meaningless to have held tlock in the first place.
44410Sstevel@tonic-gate 	 */
44420Sstevel@tonic-gate 	if (ip->i_mapcnt > 0 && MANDLOCK(vp, ip->i_mode))
44430Sstevel@tonic-gate 		return (EAGAIN);
44445331Samw 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
44450Sstevel@tonic-gate }
44460Sstevel@tonic-gate 
44470Sstevel@tonic-gate /* ARGSUSED */
44480Sstevel@tonic-gate static int
ufs_space(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)4449923Ssdebnath ufs_space(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
4450923Ssdebnath 	offset_t offset, cred_t *cr, caller_context_t *ct)
44510Sstevel@tonic-gate {
4452923Ssdebnath 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
44530Sstevel@tonic-gate 	struct ulockfs *ulp;
44540Sstevel@tonic-gate 	int error;
44550Sstevel@tonic-gate 
4456923Ssdebnath 	if ((error = convoff(vp, bfp, 0, offset)) == 0) {
4457923Ssdebnath 		if (cmd == F_FREESP) {
4458923Ssdebnath 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4459923Ssdebnath 			    ULOCKFS_SPACE_MASK);
4460923Ssdebnath 			if (error)
4461923Ssdebnath 				return (error);
4462923Ssdebnath 			error = ufs_freesp(vp, bfp, flag, cr);
4463923Ssdebnath 		} else if (cmd == F_ALLOCSP) {
4464923Ssdebnath 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4465923Ssdebnath 			    ULOCKFS_FALLOCATE_MASK);
4466923Ssdebnath 			if (error)
4467923Ssdebnath 				return (error);
4468923Ssdebnath 			error = ufs_allocsp(vp, bfp, cr);
4469923Ssdebnath 		} else
4470923Ssdebnath 			return (EINVAL); /* Command not handled here */
4471923Ssdebnath 
4472923Ssdebnath 		if (ulp)
4473923Ssdebnath 			ufs_lockfs_end(ulp);
4474923Ssdebnath 
4475923Ssdebnath 	}
44760Sstevel@tonic-gate 	return (error);
44770Sstevel@tonic-gate }
44780Sstevel@tonic-gate 
44790Sstevel@tonic-gate /*
44800Sstevel@tonic-gate  * Used to determine if read ahead should be done. Also used to
44810Sstevel@tonic-gate  * to determine when write back occurs.
44820Sstevel@tonic-gate  */
44830Sstevel@tonic-gate #define	CLUSTSZ(ip)		((ip)->i_ufsvfs->vfs_ioclustsz)
44840Sstevel@tonic-gate 
44850Sstevel@tonic-gate /*
44860Sstevel@tonic-gate  * A faster version of ufs_getpage.
44870Sstevel@tonic-gate  *
44880Sstevel@tonic-gate  * We optimize by inlining the pvn_getpages iterator, eliminating
44890Sstevel@tonic-gate  * calls to bmap_read if file doesn't have UFS holes, and avoiding
44900Sstevel@tonic-gate  * the overhead of page_exists().
44910Sstevel@tonic-gate  *
44920Sstevel@tonic-gate  * When files has UFS_HOLES and ufs_getpage is called with S_READ,
44930Sstevel@tonic-gate  * we set *protp to PROT_READ to avoid calling bmap_read. This approach
44940Sstevel@tonic-gate  * victimizes performance when a file with UFS holes is faulted
44950Sstevel@tonic-gate  * first in the S_READ mode, and then in the S_WRITE mode. We will get
44960Sstevel@tonic-gate  * two MMU faults in this case.
44970Sstevel@tonic-gate  *
44980Sstevel@tonic-gate  * XXX - the inode fields which control the sequential mode are not
44990Sstevel@tonic-gate  *	 protected by any mutex. The read ahead will act wild if
45000Sstevel@tonic-gate  *	 multiple processes will access the file concurrently and
45010Sstevel@tonic-gate  *	 some of them in sequential mode. One particulary bad case
45020Sstevel@tonic-gate  *	 is if another thread will change the value of i_nextrio between
45030Sstevel@tonic-gate  *	 the time this thread tests the i_nextrio value and then reads it
45040Sstevel@tonic-gate  *	 again to use it as the offset for the read ahead.
45050Sstevel@tonic-gate  */
45065331Samw /*ARGSUSED*/
45070Sstevel@tonic-gate static int
ufs_getpage(struct vnode * vp,offset_t off,size_t len,uint_t * protp,page_t * plarr[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,struct cred * cr,caller_context_t * ct)45080Sstevel@tonic-gate ufs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
45090Sstevel@tonic-gate 	page_t *plarr[], size_t plsz, struct seg *seg, caddr_t addr,
45105331Samw 	enum seg_rw rw, struct cred *cr, caller_context_t *ct)
45110Sstevel@tonic-gate {
45120Sstevel@tonic-gate 	u_offset_t	uoff = (u_offset_t)off; /* type conversion */
45130Sstevel@tonic-gate 	u_offset_t	pgoff;
45140Sstevel@tonic-gate 	u_offset_t	eoff;
45150Sstevel@tonic-gate 	struct inode 	*ip = VTOI(vp);
45160Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
45170Sstevel@tonic-gate 	struct fs 	*fs;
45180Sstevel@tonic-gate 	struct ulockfs	*ulp;
45190Sstevel@tonic-gate 	page_t		**pl;
45200Sstevel@tonic-gate 	caddr_t		pgaddr;
45210Sstevel@tonic-gate 	krw_t		rwtype;
45220Sstevel@tonic-gate 	int 		err;
45230Sstevel@tonic-gate 	int		has_holes;
45240Sstevel@tonic-gate 	int		beyond_eof;
45250Sstevel@tonic-gate 	int		seqmode;
45260Sstevel@tonic-gate 	int		pgsize = PAGESIZE;
45270Sstevel@tonic-gate 	int		dolock;
45280Sstevel@tonic-gate 	int		do_qlock;
45290Sstevel@tonic-gate 	int		trans_size;
45300Sstevel@tonic-gate 
45310Sstevel@tonic-gate 	ASSERT((uoff & PAGEOFFSET) == 0);
45320Sstevel@tonic-gate 
45330Sstevel@tonic-gate 	if (protp)
45340Sstevel@tonic-gate 		*protp = PROT_ALL;
45350Sstevel@tonic-gate 
45360Sstevel@tonic-gate 	/*
45370Sstevel@tonic-gate 	 * Obey the lockfs protocol
45380Sstevel@tonic-gate 	 */
45390Sstevel@tonic-gate 	err = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, seg,
45404662Sfrankho 	    rw == S_READ || rw == S_EXEC, protp);
45410Sstevel@tonic-gate 	if (err)
45420Sstevel@tonic-gate 		goto out;
45430Sstevel@tonic-gate 
45440Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
45450Sstevel@tonic-gate 
45460Sstevel@tonic-gate 	if (ulp && (rw == S_CREATE || rw == S_WRITE) &&
45470Sstevel@tonic-gate 	    !(vp->v_flag & VISSWAP)) {
45480Sstevel@tonic-gate 		/*
45490Sstevel@tonic-gate 		 * Try to start a transaction, will return if blocking is
45500Sstevel@tonic-gate 		 * expected to occur and the address space is not the
45510Sstevel@tonic-gate 		 * kernel address space.
45520Sstevel@tonic-gate 		 */
45530Sstevel@tonic-gate 		trans_size = TOP_GETPAGE_SIZE(ip);
45540Sstevel@tonic-gate 		if (seg->s_as != &kas) {
45550Sstevel@tonic-gate 			TRANS_TRY_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE,
45564662Sfrankho 			    trans_size, err)
45570Sstevel@tonic-gate 			if (err == EWOULDBLOCK) {
45580Sstevel@tonic-gate 				/*
45590Sstevel@tonic-gate 				 * Use EDEADLK here because the VM code
45600Sstevel@tonic-gate 				 * can normally never see this error.
45610Sstevel@tonic-gate 				 */
45620Sstevel@tonic-gate 				err = EDEADLK;
45630Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
45640Sstevel@tonic-gate 				goto out;
45650Sstevel@tonic-gate 			}
45660Sstevel@tonic-gate 		} else {
45670Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
45680Sstevel@tonic-gate 		}
45690Sstevel@tonic-gate 	}
45700Sstevel@tonic-gate 
45710Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
45720Sstevel@tonic-gate 		err = ENOSYS;
45730Sstevel@tonic-gate 		goto unlock;
45740Sstevel@tonic-gate 	}
45750Sstevel@tonic-gate 
45760Sstevel@tonic-gate 	seqmode = ip->i_nextr == uoff && rw != S_CREATE;
45770Sstevel@tonic-gate 
45780Sstevel@tonic-gate 	rwtype = RW_READER;		/* start as a reader */
45790Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
45800Sstevel@tonic-gate 	/*
45810Sstevel@tonic-gate 	 * If this thread owns the lock, i.e., this thread grabbed it
45820Sstevel@tonic-gate 	 * as writer somewhere above, then we don't need to grab the
45830Sstevel@tonic-gate 	 * lock as reader in this routine.
45840Sstevel@tonic-gate 	 */
45850Sstevel@tonic-gate 	do_qlock = (rw_owner(&ufsvfsp->vfs_dqrwlock) != curthread);
45860Sstevel@tonic-gate 
45870Sstevel@tonic-gate retrylock:
45880Sstevel@tonic-gate 	if (dolock) {
45890Sstevel@tonic-gate 		/*
45900Sstevel@tonic-gate 		 * Grab the quota lock if we need to call
45910Sstevel@tonic-gate 		 * bmap_write() below (with i_contents as writer).
45920Sstevel@tonic-gate 		 */
45930Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
45940Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
45950Sstevel@tonic-gate 		rw_enter(&ip->i_contents, rwtype);
45960Sstevel@tonic-gate 	}
45970Sstevel@tonic-gate 
45980Sstevel@tonic-gate 	/*
45990Sstevel@tonic-gate 	 * We may be getting called as a side effect of a bmap using
46000Sstevel@tonic-gate 	 * fbread() when the blocks might be being allocated and the
46010Sstevel@tonic-gate 	 * size has not yet been up'ed.  In this case we want to be
46020Sstevel@tonic-gate 	 * able to return zero pages if we get back UFS_HOLE from
46030Sstevel@tonic-gate 	 * calling bmap for a non write case here.  We also might have
46040Sstevel@tonic-gate 	 * to read some frags from the disk into a page if we are
46050Sstevel@tonic-gate 	 * extending the number of frags for a given lbn in bmap().
46060Sstevel@tonic-gate 	 * Large Files: The read of i_size here is atomic because
46070Sstevel@tonic-gate 	 * i_contents is held here. If dolock is zero, the lock
46080Sstevel@tonic-gate 	 * is held in bmap routines.
46090Sstevel@tonic-gate 	 */
46106184Smc208700 	beyond_eof = uoff + len >
46116782Smc208700 	    P2ROUNDUP_TYPED(ip->i_size, PAGESIZE, u_offset_t);
46120Sstevel@tonic-gate 	if (beyond_eof && seg != segkmap) {
46130Sstevel@tonic-gate 		if (dolock) {
46140Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
46150Sstevel@tonic-gate 			if (do_qlock && rwtype == RW_WRITER)
46160Sstevel@tonic-gate 				rw_exit(&ufsvfsp->vfs_dqrwlock);
46170Sstevel@tonic-gate 		}
46180Sstevel@tonic-gate 		err = EFAULT;
46190Sstevel@tonic-gate 		goto unlock;
46200Sstevel@tonic-gate 	}
46210Sstevel@tonic-gate 
46220Sstevel@tonic-gate 	/*
46230Sstevel@tonic-gate 	 * Must hold i_contents lock throughout the call to pvn_getpages
46240Sstevel@tonic-gate 	 * since locked pages are returned from each call to ufs_getapage.
46250Sstevel@tonic-gate 	 * Must *not* return locked pages and then try for contents lock
46260Sstevel@tonic-gate 	 * due to lock ordering requirements (inode > page)
46270Sstevel@tonic-gate 	 */
46280Sstevel@tonic-gate 
46290Sstevel@tonic-gate 	has_holes = bmap_has_holes(ip);
46300Sstevel@tonic-gate 
46310Sstevel@tonic-gate 	if ((rw == S_WRITE || rw == S_CREATE) && has_holes && !beyond_eof) {
46320Sstevel@tonic-gate 		int	blk_size;
46330Sstevel@tonic-gate 		u_offset_t offset;
46340Sstevel@tonic-gate 
46350Sstevel@tonic-gate 		/*
46360Sstevel@tonic-gate 		 * We must acquire the RW_WRITER lock in order to
46370Sstevel@tonic-gate 		 * call bmap_write().
46380Sstevel@tonic-gate 		 */
46390Sstevel@tonic-gate 		if (dolock && rwtype == RW_READER) {
46400Sstevel@tonic-gate 			rwtype = RW_WRITER;
46410Sstevel@tonic-gate 
46420Sstevel@tonic-gate 			/*
46430Sstevel@tonic-gate 			 * Grab the quota lock before
46440Sstevel@tonic-gate 			 * upgrading i_contents, but if we can't grab it
46450Sstevel@tonic-gate 			 * don't wait here due to lock order:
46460Sstevel@tonic-gate 			 * vfs_dqrwlock > i_contents.
46470Sstevel@tonic-gate 			 */
46484662Sfrankho 			if (do_qlock &&
46494662Sfrankho 			    rw_tryenter(&ufsvfsp->vfs_dqrwlock, RW_READER)
46504662Sfrankho 			    == 0) {
46510Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
46520Sstevel@tonic-gate 				goto retrylock;
46530Sstevel@tonic-gate 			}
46540Sstevel@tonic-gate 			if (!rw_tryupgrade(&ip->i_contents)) {
46550Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
46560Sstevel@tonic-gate 				if (do_qlock)
46570Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
46580Sstevel@tonic-gate 				goto retrylock;
46590Sstevel@tonic-gate 			}
46600Sstevel@tonic-gate 		}
46610Sstevel@tonic-gate 
46620Sstevel@tonic-gate 		/*
46630Sstevel@tonic-gate 		 * May be allocating disk blocks for holes here as
46640Sstevel@tonic-gate 		 * a result of mmap faults. write(2) does the bmap_write
46650Sstevel@tonic-gate 		 * in rdip/wrip, not here. We are not dealing with frags
46660Sstevel@tonic-gate 		 * in this case.
46670Sstevel@tonic-gate 		 */
46680Sstevel@tonic-gate 		/*
46690Sstevel@tonic-gate 		 * Large Files: We cast fs_bmask field to offset_t
46700Sstevel@tonic-gate 		 * just as we do for MAXBMASK because uoff is a 64-bit
46710Sstevel@tonic-gate 		 * data type. fs_bmask will still be a 32-bit type
46720Sstevel@tonic-gate 		 * as we cannot change any ondisk data structures.
46730Sstevel@tonic-gate 		 */
46740Sstevel@tonic-gate 
46750Sstevel@tonic-gate 		offset = uoff & (offset_t)fs->fs_bmask;
46760Sstevel@tonic-gate 		while (offset < uoff + len) {
46770Sstevel@tonic-gate 			blk_size = (int)blksize(fs, ip, lblkno(fs, offset));
4678923Ssdebnath 			err = bmap_write(ip, offset, blk_size,
4679923Ssdebnath 			    BI_NORMAL, NULL, cr);
46800Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD))
46810Sstevel@tonic-gate 				ip->i_seq++;
46820Sstevel@tonic-gate 			if (err)
46830Sstevel@tonic-gate 				goto update_inode;
46840Sstevel@tonic-gate 			offset += blk_size; /* XXX - make this contig */
46850Sstevel@tonic-gate 		}
46860Sstevel@tonic-gate 	}
46870Sstevel@tonic-gate 
46880Sstevel@tonic-gate 	/*
46890Sstevel@tonic-gate 	 * Can be a reader from now on.
46900Sstevel@tonic-gate 	 */
46910Sstevel@tonic-gate 	if (dolock && rwtype == RW_WRITER) {
46920Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
46930Sstevel@tonic-gate 		/*
46940Sstevel@tonic-gate 		 * We can release vfs_dqrwlock early so do it, but make
46950Sstevel@tonic-gate 		 * sure we don't try to release it again at the bottom.
46960Sstevel@tonic-gate 		 */
46970Sstevel@tonic-gate 		if (do_qlock) {
46980Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
46990Sstevel@tonic-gate 			do_qlock = 0;
47000Sstevel@tonic-gate 		}
47010Sstevel@tonic-gate 	}
47020Sstevel@tonic-gate 
47030Sstevel@tonic-gate 	/*
47040Sstevel@tonic-gate 	 * We remove PROT_WRITE in cases when the file has UFS holes
47050Sstevel@tonic-gate 	 * because we don't  want to call bmap_read() to check each
47060Sstevel@tonic-gate 	 * page if it is backed with a disk block.
47070Sstevel@tonic-gate 	 */
47080Sstevel@tonic-gate 	if (protp && has_holes && rw != S_WRITE && rw != S_CREATE)
47090Sstevel@tonic-gate 		*protp &= ~PROT_WRITE;
47100Sstevel@tonic-gate 
47110Sstevel@tonic-gate 	err = 0;
47120Sstevel@tonic-gate 
47130Sstevel@tonic-gate 	/*
47140Sstevel@tonic-gate 	 * The loop looks up pages in the range [off, off + len).
47150Sstevel@tonic-gate 	 * For each page, we first check if we should initiate an asynchronous
47160Sstevel@tonic-gate 	 * read ahead before we call page_lookup (we may sleep in page_lookup
47170Sstevel@tonic-gate 	 * for a previously initiated disk read).
47180Sstevel@tonic-gate 	 */
47190Sstevel@tonic-gate 	eoff = (uoff + len);
47200Sstevel@tonic-gate 	for (pgoff = uoff, pgaddr = addr, pl = plarr;
47210Sstevel@tonic-gate 	    pgoff < eoff; /* empty */) {
47220Sstevel@tonic-gate 		page_t	*pp;
47230Sstevel@tonic-gate 		u_offset_t	nextrio;
47240Sstevel@tonic-gate 		se_t	se;
47250Sstevel@tonic-gate 		int retval;
47260Sstevel@tonic-gate 
47270Sstevel@tonic-gate 		se = ((rw == S_CREATE || rw == S_OTHER) ? SE_EXCL : SE_SHARED);
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate 		/* Handle async getpage (faultahead) */
47300Sstevel@tonic-gate 		if (plarr == NULL) {
47310Sstevel@tonic-gate 			ip->i_nextrio = pgoff;
47320Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, pgoff, seg, pgaddr);
47330Sstevel@tonic-gate 			pgoff += pgsize;
47340Sstevel@tonic-gate 			pgaddr += pgsize;
47350Sstevel@tonic-gate 			continue;
47360Sstevel@tonic-gate 		}
47370Sstevel@tonic-gate 		/*
47380Sstevel@tonic-gate 		 * Check if we should initiate read ahead of next cluster.
47390Sstevel@tonic-gate 		 * We call page_exists only when we need to confirm that
47400Sstevel@tonic-gate 		 * we have the current page before we initiate the read ahead.
47410Sstevel@tonic-gate 		 */
47420Sstevel@tonic-gate 		nextrio = ip->i_nextrio;
47430Sstevel@tonic-gate 		if (seqmode &&
47440Sstevel@tonic-gate 		    pgoff + CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
47450Sstevel@tonic-gate 		    nextrio < ip->i_size && page_exists(vp, pgoff)) {
47460Sstevel@tonic-gate 			retval = ufs_getpage_ra(vp, pgoff, seg, pgaddr);
47470Sstevel@tonic-gate 			/*
47480Sstevel@tonic-gate 			 * We always read ahead the next cluster of data
47490Sstevel@tonic-gate 			 * starting from i_nextrio. If the page (vp,nextrio)
47500Sstevel@tonic-gate 			 * is actually in core at this point, the routine
47510Sstevel@tonic-gate 			 * ufs_getpage_ra() will stop pre-fetching data
47520Sstevel@tonic-gate 			 * until we read that page in a synchronized manner
47530Sstevel@tonic-gate 			 * through ufs_getpage_miss(). So, we should increase
47540Sstevel@tonic-gate 			 * i_nextrio if the page (vp, nextrio) exists.
47550Sstevel@tonic-gate 			 */
47560Sstevel@tonic-gate 			if ((retval == 0) && page_exists(vp, nextrio)) {
47570Sstevel@tonic-gate 				ip->i_nextrio = nextrio + pgsize;
47580Sstevel@tonic-gate 			}
47590Sstevel@tonic-gate 		}
47600Sstevel@tonic-gate 
47610Sstevel@tonic-gate 		if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
47620Sstevel@tonic-gate 			/*
47630Sstevel@tonic-gate 			 * We found the page in the page cache.
47640Sstevel@tonic-gate 			 */
47650Sstevel@tonic-gate 			*pl++ = pp;
47660Sstevel@tonic-gate 			pgoff += pgsize;
47670Sstevel@tonic-gate 			pgaddr += pgsize;
47680Sstevel@tonic-gate 			len -= pgsize;
47690Sstevel@tonic-gate 			plsz -= pgsize;
47700Sstevel@tonic-gate 		} else  {
47710Sstevel@tonic-gate 			/*
47720Sstevel@tonic-gate 			 * We have to create the page, or read it from disk.
47730Sstevel@tonic-gate 			 */
47740Sstevel@tonic-gate 			if (err = ufs_getpage_miss(vp, pgoff, len, seg, pgaddr,
47750Sstevel@tonic-gate 			    pl, plsz, rw, seqmode))
47760Sstevel@tonic-gate 				goto error;
47770Sstevel@tonic-gate 
47780Sstevel@tonic-gate 			while (*pl != NULL) {
47790Sstevel@tonic-gate 				pl++;
47800Sstevel@tonic-gate 				pgoff += pgsize;
47810Sstevel@tonic-gate 				pgaddr += pgsize;
47820Sstevel@tonic-gate 				len -= pgsize;
47830Sstevel@tonic-gate 				plsz -= pgsize;
47840Sstevel@tonic-gate 			}
47850Sstevel@tonic-gate 		}
47860Sstevel@tonic-gate 	}
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 	/*
47890Sstevel@tonic-gate 	 * Return pages up to plsz if they are in the page cache.
47900Sstevel@tonic-gate 	 * We cannot return pages if there is a chance that they are
47910Sstevel@tonic-gate 	 * backed with a UFS hole and rw is S_WRITE or S_CREATE.
47920Sstevel@tonic-gate 	 */
47930Sstevel@tonic-gate 	if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
47940Sstevel@tonic-gate 
47950Sstevel@tonic-gate 		ASSERT((protp == NULL) ||
47964662Sfrankho 		    !(has_holes && (*protp & PROT_WRITE)));
47970Sstevel@tonic-gate 
47980Sstevel@tonic-gate 		eoff = pgoff + plsz;
47990Sstevel@tonic-gate 		while (pgoff < eoff) {
48000Sstevel@tonic-gate 			page_t		*pp;
48010Sstevel@tonic-gate 
48020Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(vp, pgoff,
48030Sstevel@tonic-gate 			    SE_SHARED)) == NULL)
48040Sstevel@tonic-gate 				break;
48050Sstevel@tonic-gate 
48060Sstevel@tonic-gate 			*pl++ = pp;
48070Sstevel@tonic-gate 			pgoff += pgsize;
48080Sstevel@tonic-gate 			plsz -= pgsize;
48090Sstevel@tonic-gate 		}
48100Sstevel@tonic-gate 	}
48110Sstevel@tonic-gate 
48120Sstevel@tonic-gate 	if (plarr)
48130Sstevel@tonic-gate 		*pl = NULL;			/* Terminate page list */
48140Sstevel@tonic-gate 	ip->i_nextr = pgoff;
48150Sstevel@tonic-gate 
48160Sstevel@tonic-gate error:
48170Sstevel@tonic-gate 	if (err && plarr) {
48180Sstevel@tonic-gate 		/*
48190Sstevel@tonic-gate 		 * Release any pages we have locked.
48200Sstevel@tonic-gate 		 */
48210Sstevel@tonic-gate 		while (pl > &plarr[0])
48220Sstevel@tonic-gate 			page_unlock(*--pl);
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate 		plarr[0] = NULL;
48250Sstevel@tonic-gate 	}
48260Sstevel@tonic-gate 
48270Sstevel@tonic-gate update_inode:
48280Sstevel@tonic-gate 	/*
48290Sstevel@tonic-gate 	 * If the inode is not already marked for IACC (in rdip() for read)
48300Sstevel@tonic-gate 	 * and the inode is not marked for no access time update (in wrip()
48310Sstevel@tonic-gate 	 * for write) then update the inode access time and mod time now.
48320Sstevel@tonic-gate 	 */
48330Sstevel@tonic-gate 	if ((ip->i_flag & (IACC | INOACC)) == 0) {
48340Sstevel@tonic-gate 		if ((rw != S_OTHER) && (ip->i_mode & IFMT) != IFDIR) {
48350Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
48360Sstevel@tonic-gate 			    (fs->fs_ronly == 0) &&
48370Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
48380Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
48390Sstevel@tonic-gate 				ip->i_flag |= IACC;
48400Sstevel@tonic-gate 				ITIMES_NOLOCK(ip);
48410Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
48420Sstevel@tonic-gate 			}
48430Sstevel@tonic-gate 		}
48440Sstevel@tonic-gate 	}
48450Sstevel@tonic-gate 
48460Sstevel@tonic-gate 	if (dolock) {
48470Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
48480Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
48490Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
48500Sstevel@tonic-gate 	}
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate unlock:
48530Sstevel@tonic-gate 	if (ulp) {
48540Sstevel@tonic-gate 		if ((rw == S_CREATE || rw == S_WRITE) &&
48550Sstevel@tonic-gate 		    !(vp->v_flag & VISSWAP)) {
48560Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
48570Sstevel@tonic-gate 		}
48580Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
48590Sstevel@tonic-gate 	}
48600Sstevel@tonic-gate out:
48610Sstevel@tonic-gate 	return (err);
48620Sstevel@tonic-gate }
48630Sstevel@tonic-gate 
48640Sstevel@tonic-gate /*
48650Sstevel@tonic-gate  * ufs_getpage_miss is called when ufs_getpage missed the page in the page
48660Sstevel@tonic-gate  * cache. The page is either read from the disk, or it's created.
48670Sstevel@tonic-gate  * A page is created (without disk read) if rw == S_CREATE, or if
48680Sstevel@tonic-gate  * the page is not backed with a real disk block (UFS hole).
48690Sstevel@tonic-gate  */
48700Sstevel@tonic-gate /* ARGSUSED */
48710Sstevel@tonic-gate static int
ufs_getpage_miss(struct vnode * vp,u_offset_t off,size_t len,struct seg * seg,caddr_t addr,page_t * pl[],size_t plsz,enum seg_rw rw,int seq)48720Sstevel@tonic-gate ufs_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
48730Sstevel@tonic-gate 	caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int seq)
48740Sstevel@tonic-gate {
48750Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
48760Sstevel@tonic-gate 	page_t		*pp;
48770Sstevel@tonic-gate 	daddr_t		bn;
48780Sstevel@tonic-gate 	size_t		io_len;
4879923Ssdebnath 	int		crpage = 0;
48800Sstevel@tonic-gate 	int		err;
48810Sstevel@tonic-gate 	int		contig;
48820Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
48830Sstevel@tonic-gate 
48840Sstevel@tonic-gate 	/*
48850Sstevel@tonic-gate 	 * Figure out whether the page can be created, or must be
48860Sstevel@tonic-gate 	 * must be read from the disk.
48870Sstevel@tonic-gate 	 */
48880Sstevel@tonic-gate 	if (rw == S_CREATE)
48890Sstevel@tonic-gate 		crpage = 1;
48900Sstevel@tonic-gate 	else {
48910Sstevel@tonic-gate 		contig = 0;
48920Sstevel@tonic-gate 		if (err = bmap_read(ip, off, &bn, &contig))
48930Sstevel@tonic-gate 			return (err);
4894923Ssdebnath 
48950Sstevel@tonic-gate 		crpage = (bn == UFS_HOLE);
4896923Ssdebnath 
4897923Ssdebnath 		/*
4898923Ssdebnath 		 * If its also a fallocated block that hasn't been written to
4899923Ssdebnath 		 * yet, we will treat it just like a UFS_HOLE and create
4900923Ssdebnath 		 * a zero page for it
4901923Ssdebnath 		 */
4902923Ssdebnath 		if (ISFALLOCBLK(ip, bn))
4903923Ssdebnath 			crpage = 1;
49040Sstevel@tonic-gate 	}
49050Sstevel@tonic-gate 
49060Sstevel@tonic-gate 	if (crpage) {
49070Sstevel@tonic-gate 		if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT, seg,
49080Sstevel@tonic-gate 		    addr)) == NULL) {
49090Sstevel@tonic-gate 			return (ufs_fault(vp,
49104662Sfrankho 			    "ufs_getpage_miss: page_create == NULL"));
49110Sstevel@tonic-gate 		}
49120Sstevel@tonic-gate 
49130Sstevel@tonic-gate 		if (rw != S_CREATE)
49140Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
4915923Ssdebnath 
49160Sstevel@tonic-gate 		io_len = PAGESIZE;
49170Sstevel@tonic-gate 	} else {
49180Sstevel@tonic-gate 		u_offset_t	io_off;
49190Sstevel@tonic-gate 		uint_t	xlen;
49200Sstevel@tonic-gate 		struct buf	*bp;
49210Sstevel@tonic-gate 		ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
49220Sstevel@tonic-gate 
49230Sstevel@tonic-gate 		/*
49240Sstevel@tonic-gate 		 * If access is not in sequential order, we read from disk
49250Sstevel@tonic-gate 		 * in bsize units.
49260Sstevel@tonic-gate 		 *
49270Sstevel@tonic-gate 		 * We limit the size of the transfer to bsize if we are reading
49280Sstevel@tonic-gate 		 * from the beginning of the file. Note in this situation we
49290Sstevel@tonic-gate 		 * will hedge our bets and initiate an async read ahead of
49300Sstevel@tonic-gate 		 * the second block.
49310Sstevel@tonic-gate 		 */
49320Sstevel@tonic-gate 		if (!seq || off == 0)
49330Sstevel@tonic-gate 			contig = MIN(contig, bsize);
49340Sstevel@tonic-gate 
49350Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
49360Sstevel@tonic-gate 		    &io_len, off, contig, 0);
49370Sstevel@tonic-gate 
49380Sstevel@tonic-gate 		/*
49390Sstevel@tonic-gate 		 * Some other thread has entered the page.
49400Sstevel@tonic-gate 		 * ufs_getpage will retry page_lookup.
49410Sstevel@tonic-gate 		 */
49420Sstevel@tonic-gate 		if (pp == NULL) {
49430Sstevel@tonic-gate 			pl[0] = NULL;
49440Sstevel@tonic-gate 			return (0);
49450Sstevel@tonic-gate 		}
49460Sstevel@tonic-gate 
49470Sstevel@tonic-gate 		/*
49480Sstevel@tonic-gate 		 * Zero part of the page which we are not
49490Sstevel@tonic-gate 		 * going to read from the disk.
49500Sstevel@tonic-gate 		 */
49510Sstevel@tonic-gate 		xlen = io_len & PAGEOFFSET;
49520Sstevel@tonic-gate 		if (xlen != 0)
49530Sstevel@tonic-gate 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
49540Sstevel@tonic-gate 
49550Sstevel@tonic-gate 		bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ);
49560Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
49570Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
49580Sstevel@tonic-gate 		bp->b_blkno = bn;
49590Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
49600Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
49610Sstevel@tonic-gate 		bp->b_offset = off;
49620Sstevel@tonic-gate 
49630Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
49640Sstevel@tonic-gate 			lufs_read_strategy(ufsvfsp->vfs_log, bp);
49650Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
49660Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
49670Sstevel@tonic-gate 		} else {
496811066Srafael.vanoni@sun.com 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
49690Sstevel@tonic-gate 			ub.ub_getpages.value.ul++;
49700Sstevel@tonic-gate 			(void) bdev_strategy(bp);
49710Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
49720Sstevel@tonic-gate 		}
49730Sstevel@tonic-gate 
49740Sstevel@tonic-gate 		ip->i_nextrio = off + ((io_len + PAGESIZE - 1) & PAGEMASK);
49750Sstevel@tonic-gate 
49760Sstevel@tonic-gate 		/*
49770Sstevel@tonic-gate 		 * If the file access is sequential, initiate read ahead
49780Sstevel@tonic-gate 		 * of the next cluster.
49790Sstevel@tonic-gate 		 */
49800Sstevel@tonic-gate 		if (seq && ip->i_nextrio < ip->i_size)
49810Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, off, seg, addr);
49820Sstevel@tonic-gate 		err = biowait(bp);
49830Sstevel@tonic-gate 		pageio_done(bp);
49840Sstevel@tonic-gate 
49850Sstevel@tonic-gate 		if (err) {
49860Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
49870Sstevel@tonic-gate 			return (err);
49880Sstevel@tonic-gate 		}
49890Sstevel@tonic-gate 	}
49900Sstevel@tonic-gate 
49910Sstevel@tonic-gate 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
49920Sstevel@tonic-gate 	return (0);
49930Sstevel@tonic-gate }
49940Sstevel@tonic-gate 
49950Sstevel@tonic-gate /*
49960Sstevel@tonic-gate  * Read ahead a cluster from the disk. Returns the length in bytes.
49970Sstevel@tonic-gate  */
49980Sstevel@tonic-gate static int
ufs_getpage_ra(struct vnode * vp,u_offset_t off,struct seg * seg,caddr_t addr)49990Sstevel@tonic-gate ufs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
50000Sstevel@tonic-gate {
50010Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
50020Sstevel@tonic-gate 	page_t		*pp;
50030Sstevel@tonic-gate 	u_offset_t	io_off = ip->i_nextrio;
50040Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp;
50050Sstevel@tonic-gate 	caddr_t		addr2 = addr + (io_off - off);
50060Sstevel@tonic-gate 	struct buf	*bp;
50070Sstevel@tonic-gate 	daddr_t		bn;
50080Sstevel@tonic-gate 	size_t		io_len;
5009923Ssdebnath 	int		err;
50100Sstevel@tonic-gate 	int		contig;
50110Sstevel@tonic-gate 	int		xlen;
50120Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
50130Sstevel@tonic-gate 
50140Sstevel@tonic-gate 	/*
50150Sstevel@tonic-gate 	 * If the directio advisory is in effect on this file,
50160Sstevel@tonic-gate 	 * then do not do buffered read ahead. Read ahead makes
50170Sstevel@tonic-gate 	 * it more difficult on threads using directio as they
50180Sstevel@tonic-gate 	 * will be forced to flush the pages from this vnode.
50190Sstevel@tonic-gate 	 */
50200Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
50210Sstevel@tonic-gate 		return (0);
50220Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio)
50230Sstevel@tonic-gate 		return (0);
50240Sstevel@tonic-gate 
50250Sstevel@tonic-gate 	/*
50260Sstevel@tonic-gate 	 * Is this test needed?
50270Sstevel@tonic-gate 	 */
50280Sstevel@tonic-gate 	if (addr2 >= seg->s_base + seg->s_size)
50290Sstevel@tonic-gate 		return (0);
50300Sstevel@tonic-gate 
50310Sstevel@tonic-gate 	contig = 0;
5032923Ssdebnath 	err = bmap_read(ip, io_off, &bn, &contig);
5033923Ssdebnath 	/*
5034923Ssdebnath 	 * If its a UFS_HOLE or a fallocated block, do not perform
5035923Ssdebnath 	 * any read ahead's since there probably is nothing to read ahead
5036923Ssdebnath 	 */
5037923Ssdebnath 	if (err || bn == UFS_HOLE || ISFALLOCBLK(ip, bn))
50380Sstevel@tonic-gate 		return (0);
50390Sstevel@tonic-gate 
50400Sstevel@tonic-gate 	/*
50410Sstevel@tonic-gate 	 * Limit the transfer size to bsize if this is the 2nd block.
50420Sstevel@tonic-gate 	 */
50430Sstevel@tonic-gate 	if (io_off == (u_offset_t)bsize)
50440Sstevel@tonic-gate 		contig = MIN(contig, bsize);
50450Sstevel@tonic-gate 
50460Sstevel@tonic-gate 	if ((pp = pvn_read_kluster(vp, io_off, seg, addr2, &io_off,
50470Sstevel@tonic-gate 	    &io_len, io_off, contig, 1)) == NULL)
50480Sstevel@tonic-gate 		return (0);
50490Sstevel@tonic-gate 
50500Sstevel@tonic-gate 	/*
50510Sstevel@tonic-gate 	 * Zero part of page which we are not going to read from disk
50520Sstevel@tonic-gate 	 */
50530Sstevel@tonic-gate 	if ((xlen = (io_len & PAGEOFFSET)) > 0)
50540Sstevel@tonic-gate 		pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
50550Sstevel@tonic-gate 
50560Sstevel@tonic-gate 	ip->i_nextrio = (io_off + io_len + PAGESIZE - 1) & PAGEMASK;
50570Sstevel@tonic-gate 
50580Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ | B_ASYNC);
50590Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
50600Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
50610Sstevel@tonic-gate 	bp->b_blkno = bn;
50620Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
50630Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
50640Sstevel@tonic-gate 	bp->b_offset = off;
50650Sstevel@tonic-gate 
50660Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
50670Sstevel@tonic-gate 		lufs_read_strategy(ufsvfsp->vfs_log, bp);
50680Sstevel@tonic-gate 	} else if (ufsvfsp->vfs_snapshot) {
50690Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
50700Sstevel@tonic-gate 	} else {
507111066Srafael.vanoni@sun.com 		ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
50720Sstevel@tonic-gate 		ub.ub_getras.value.ul++;
50730Sstevel@tonic-gate 		(void) bdev_strategy(bp);
50740Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
50750Sstevel@tonic-gate 	}
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 	return (io_len);
50780Sstevel@tonic-gate }
50790Sstevel@tonic-gate 
50800Sstevel@tonic-gate int	ufs_delay = 1;
50810Sstevel@tonic-gate /*
50820Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE, B_ASYNC}
50830Sstevel@tonic-gate  *
50840Sstevel@tonic-gate  * LMXXX - the inode really ought to contain a pointer to one of these
50850Sstevel@tonic-gate  * async args.  Stuff gunk in there and just hand the whole mess off.
50860Sstevel@tonic-gate  * This would replace i_delaylen, i_delayoff.
50870Sstevel@tonic-gate  */
50880Sstevel@tonic-gate /*ARGSUSED*/
50890Sstevel@tonic-gate static int
ufs_putpage(struct vnode * vp,offset_t off,size_t len,int flags,struct cred * cr,caller_context_t * ct)50900Sstevel@tonic-gate ufs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
50915331Samw 	struct cred *cr, caller_context_t *ct)
50920Sstevel@tonic-gate {
50930Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
50940Sstevel@tonic-gate 	int err = 0;
50950Sstevel@tonic-gate 
50960Sstevel@tonic-gate 	if (vp->v_count == 0) {
50970Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpage: bad v_count == 0"));
50980Sstevel@tonic-gate 	}
50990Sstevel@tonic-gate 
51000Sstevel@tonic-gate 	/*
51010Sstevel@tonic-gate 	 * XXX - Why should this check be made here?
51020Sstevel@tonic-gate 	 */
51030Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
51040Sstevel@tonic-gate 		err = ENOSYS;
51050Sstevel@tonic-gate 		goto errout;
51060Sstevel@tonic-gate 	}
51070Sstevel@tonic-gate 
51080Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL) {
51090Sstevel@tonic-gate 		err = EIO;
51100Sstevel@tonic-gate 		goto errout;
51110Sstevel@tonic-gate 	}
51120Sstevel@tonic-gate 
51130Sstevel@tonic-gate 	if (flags & B_ASYNC) {
51140Sstevel@tonic-gate 		if (ufs_delay && len &&
51150Sstevel@tonic-gate 		    (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
51160Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
51170Sstevel@tonic-gate 			/*
51180Sstevel@tonic-gate 			 * If nobody stalled, start a new cluster.
51190Sstevel@tonic-gate 			 */
51200Sstevel@tonic-gate 			if (ip->i_delaylen == 0) {
51210Sstevel@tonic-gate 				ip->i_delayoff = off;
51220Sstevel@tonic-gate 				ip->i_delaylen = len;
51230Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
51240Sstevel@tonic-gate 				goto errout;
51250Sstevel@tonic-gate 			}
51260Sstevel@tonic-gate 			/*
51270Sstevel@tonic-gate 			 * If we have a full cluster or they are not contig,
51280Sstevel@tonic-gate 			 * then push last cluster and start over.
51290Sstevel@tonic-gate 			 */
51300Sstevel@tonic-gate 			if (ip->i_delaylen >= CLUSTSZ(ip) ||
51310Sstevel@tonic-gate 			    ip->i_delayoff + ip->i_delaylen != off) {
51320Sstevel@tonic-gate 				u_offset_t doff;
51330Sstevel@tonic-gate 				size_t dlen;
51340Sstevel@tonic-gate 
51350Sstevel@tonic-gate 				doff = ip->i_delayoff;
51360Sstevel@tonic-gate 				dlen = ip->i_delaylen;
51370Sstevel@tonic-gate 				ip->i_delayoff = off;
51380Sstevel@tonic-gate 				ip->i_delaylen = len;
51390Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
51400Sstevel@tonic-gate 				err = ufs_putpages(vp, doff, dlen,
51410Sstevel@tonic-gate 				    flags, cr);
51420Sstevel@tonic-gate 				/* LMXXX - flags are new val, not old */
51430Sstevel@tonic-gate 				goto errout;
51440Sstevel@tonic-gate 			}
51450Sstevel@tonic-gate 			/*
51460Sstevel@tonic-gate 			 * There is something there, it's not full, and
51470Sstevel@tonic-gate 			 * it is contig.
51480Sstevel@tonic-gate 			 */
51490Sstevel@tonic-gate 			ip->i_delaylen += len;
51500Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
51510Sstevel@tonic-gate 			goto errout;
51520Sstevel@tonic-gate 		}
51530Sstevel@tonic-gate 		/*
51540Sstevel@tonic-gate 		 * Must have weird flags or we are not clustering.
51550Sstevel@tonic-gate 		 */
51560Sstevel@tonic-gate 	}
51570Sstevel@tonic-gate 
51580Sstevel@tonic-gate 	err = ufs_putpages(vp, off, len, flags, cr);
51590Sstevel@tonic-gate 
51600Sstevel@tonic-gate errout:
51610Sstevel@tonic-gate 	return (err);
51620Sstevel@tonic-gate }
51630Sstevel@tonic-gate 
51640Sstevel@tonic-gate /*
51650Sstevel@tonic-gate  * If len == 0, do from off to EOF.
51660Sstevel@tonic-gate  *
51670Sstevel@tonic-gate  * The normal cases should be len == 0 & off == 0 (entire vp list),
51680Sstevel@tonic-gate  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
51690Sstevel@tonic-gate  * (from pageout).
51700Sstevel@tonic-gate  */
51710Sstevel@tonic-gate /*ARGSUSED*/
51720Sstevel@tonic-gate static int
ufs_putpages(struct vnode * vp,offset_t off,size_t len,int flags,struct cred * cr)51730Sstevel@tonic-gate ufs_putpages(
51740Sstevel@tonic-gate 	struct vnode *vp,
51750Sstevel@tonic-gate 	offset_t off,
51760Sstevel@tonic-gate 	size_t len,
51770Sstevel@tonic-gate 	int flags,
51780Sstevel@tonic-gate 	struct cred *cr)
51790Sstevel@tonic-gate {
51800Sstevel@tonic-gate 	u_offset_t io_off;
51810Sstevel@tonic-gate 	u_offset_t eoff;
51820Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
51830Sstevel@tonic-gate 	page_t *pp;
51840Sstevel@tonic-gate 	size_t io_len;
51850Sstevel@tonic-gate 	int err = 0;
51860Sstevel@tonic-gate 	int dolock;
51870Sstevel@tonic-gate 
51880Sstevel@tonic-gate 	if (vp->v_count == 0)
51890Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpages: v_count == 0"));
51900Sstevel@tonic-gate 	/*
51910Sstevel@tonic-gate 	 * Acquire the readers/write inode lock before locking
51920Sstevel@tonic-gate 	 * any pages in this inode.
51930Sstevel@tonic-gate 	 * The inode lock is held during i/o.
51940Sstevel@tonic-gate 	 */
51950Sstevel@tonic-gate 	if (len == 0) {
51960Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
51970Sstevel@tonic-gate 		ip->i_delayoff = ip->i_delaylen = 0;
51980Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
51990Sstevel@tonic-gate 	}
52000Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
52010Sstevel@tonic-gate 	if (dolock) {
52020Sstevel@tonic-gate 		/*
52030Sstevel@tonic-gate 		 * Must synchronize this thread and any possible thread
52040Sstevel@tonic-gate 		 * operating in the window of vulnerability in wrip().
52050Sstevel@tonic-gate 		 * It is dangerous to allow both a thread doing a putpage
52060Sstevel@tonic-gate 		 * and a thread writing, so serialize them.  The exception
52070Sstevel@tonic-gate 		 * is when the thread in wrip() does something which causes
52080Sstevel@tonic-gate 		 * a putpage operation.  Then, the thread must be allowed
52090Sstevel@tonic-gate 		 * to continue.  It may encounter a bmap_read problem in
52100Sstevel@tonic-gate 		 * ufs_putapage, but that is handled in ufs_putapage.
52110Sstevel@tonic-gate 		 * Allow async writers to proceed, we don't want to block
52120Sstevel@tonic-gate 		 * the pageout daemon.
52130Sstevel@tonic-gate 		 */
52140Sstevel@tonic-gate 		if (ip->i_writer == curthread)
52150Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
52160Sstevel@tonic-gate 		else {
52170Sstevel@tonic-gate 			for (;;) {
52180Sstevel@tonic-gate 				rw_enter(&ip->i_contents, RW_READER);
52190Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
52200Sstevel@tonic-gate 				/*
52210Sstevel@tonic-gate 				 * If there is no thread in the critical
52220Sstevel@tonic-gate 				 * section of wrip(), then proceed.
52230Sstevel@tonic-gate 				 * Otherwise, wait until there isn't one.
52240Sstevel@tonic-gate 				 */
52250Sstevel@tonic-gate 				if (ip->i_writer == NULL) {
52260Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
52270Sstevel@tonic-gate 					break;
52280Sstevel@tonic-gate 				}
52290Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
52300Sstevel@tonic-gate 				/*
52310Sstevel@tonic-gate 				 * Bounce async writers when we have a writer
52320Sstevel@tonic-gate 				 * working on this file so we don't deadlock
52330Sstevel@tonic-gate 				 * the pageout daemon.
52340Sstevel@tonic-gate 				 */
52350Sstevel@tonic-gate 				if (flags & B_ASYNC) {
52360Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
52370Sstevel@tonic-gate 					return (0);
52380Sstevel@tonic-gate 				}
52390Sstevel@tonic-gate 				cv_wait(&ip->i_wrcv, &ip->i_tlock);
52400Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
52410Sstevel@tonic-gate 			}
52420Sstevel@tonic-gate 		}
52430Sstevel@tonic-gate 	}
52440Sstevel@tonic-gate 
52450Sstevel@tonic-gate 	if (!vn_has_cached_data(vp)) {
52460Sstevel@tonic-gate 		if (dolock)
52470Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
52480Sstevel@tonic-gate 		return (0);
52490Sstevel@tonic-gate 	}
52500Sstevel@tonic-gate 
52510Sstevel@tonic-gate 	if (len == 0) {
52520Sstevel@tonic-gate 		/*
52530Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off.
52540Sstevel@tonic-gate 		 */
52550Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, (u_offset_t)off, ufs_putapage,
52564662Sfrankho 		    flags, cr);
52570Sstevel@tonic-gate 	} else {
52580Sstevel@tonic-gate 		/*
52590Sstevel@tonic-gate 		 * Loop over all offsets in the range looking for
52600Sstevel@tonic-gate 		 * pages to deal with.
52610Sstevel@tonic-gate 		 */
52620Sstevel@tonic-gate 		if ((eoff = blkroundup(ip->i_fs, ip->i_size)) != 0)
52630Sstevel@tonic-gate 			eoff = MIN(off + len, eoff);
52640Sstevel@tonic-gate 		else
52650Sstevel@tonic-gate 			eoff = off + len;
52660Sstevel@tonic-gate 
52670Sstevel@tonic-gate 		for (io_off = off; io_off < eoff; io_off += io_len) {
52680Sstevel@tonic-gate 			/*
52690Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
52700Sstevel@tonic-gate 			 * freeing or writing pages, use the routine
52710Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
52720Sstevel@tonic-gate 			 * them from the free list.
52730Sstevel@tonic-gate 			 */
52740Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
52750Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
52764662Sfrankho 				    (flags & (B_INVAL | B_FREE)) ?
52774662Sfrankho 				    SE_EXCL : SE_SHARED);
52780Sstevel@tonic-gate 			} else {
52790Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
52804662Sfrankho 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
52810Sstevel@tonic-gate 			}
52820Sstevel@tonic-gate 
52830Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
52840Sstevel@tonic-gate 				io_len = PAGESIZE;
52850Sstevel@tonic-gate 			else {
52860Sstevel@tonic-gate 				u_offset_t *io_offp = &io_off;
52870Sstevel@tonic-gate 
52880Sstevel@tonic-gate 				err = ufs_putapage(vp, pp, io_offp, &io_len,
52890Sstevel@tonic-gate 				    flags, cr);
52900Sstevel@tonic-gate 				if (err != 0)
52910Sstevel@tonic-gate 					break;
52920Sstevel@tonic-gate 				/*
52930Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
52940Sstevel@tonic-gate 				 * the range of pages we actually wrote.
52950Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
52960Sstevel@tonic-gate 				 * since several pages may've been dealt
52970Sstevel@tonic-gate 				 * with by this iteration of the loop.
52980Sstevel@tonic-gate 				 */
52990Sstevel@tonic-gate 			}
53000Sstevel@tonic-gate 		}
53010Sstevel@tonic-gate 	}
53020Sstevel@tonic-gate 	if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
53030Sstevel@tonic-gate 		/*
53040Sstevel@tonic-gate 		 * We have just sync'ed back all the pages on
53050Sstevel@tonic-gate 		 * the inode, turn off the IMODTIME flag.
53060Sstevel@tonic-gate 		 */
53070Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
53080Sstevel@tonic-gate 		ip->i_flag &= ~IMODTIME;
53090Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
53100Sstevel@tonic-gate 	}
53110Sstevel@tonic-gate 	if (dolock)
53120Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
53130Sstevel@tonic-gate 	return (err);
53140Sstevel@tonic-gate }
53150Sstevel@tonic-gate 
53160Sstevel@tonic-gate static void
ufs_iodone(buf_t * bp)53170Sstevel@tonic-gate ufs_iodone(buf_t *bp)
53180Sstevel@tonic-gate {
53190Sstevel@tonic-gate 	struct inode *ip;
53200Sstevel@tonic-gate 
53210Sstevel@tonic-gate 	ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
53220Sstevel@tonic-gate 
53230Sstevel@tonic-gate 	bp->b_iodone = NULL;
53240Sstevel@tonic-gate 
53250Sstevel@tonic-gate 	ip = VTOI(bp->b_pages->p_vnode);
53260Sstevel@tonic-gate 
53270Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
53280Sstevel@tonic-gate 	if (ip->i_writes >= ufs_LW) {
53290Sstevel@tonic-gate 		if ((ip->i_writes -= bp->b_bcount) <= ufs_LW)
53300Sstevel@tonic-gate 			if (ufs_WRITES)
53310Sstevel@tonic-gate 				cv_broadcast(&ip->i_wrcv); /* wake all up */
53320Sstevel@tonic-gate 	} else {
53330Sstevel@tonic-gate 		ip->i_writes -= bp->b_bcount;
53340Sstevel@tonic-gate 	}
53350Sstevel@tonic-gate 
53360Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
53370Sstevel@tonic-gate 	iodone(bp);
53380Sstevel@tonic-gate }
53390Sstevel@tonic-gate 
53400Sstevel@tonic-gate /*
53410Sstevel@tonic-gate  * Write out a single page, possibly klustering adjacent
53420Sstevel@tonic-gate  * dirty pages.  The inode lock must be held.
53430Sstevel@tonic-gate  *
53440Sstevel@tonic-gate  * LMXXX - bsize < pagesize not done.
53450Sstevel@tonic-gate  */
53460Sstevel@tonic-gate /*ARGSUSED*/
53470Sstevel@tonic-gate int
ufs_putapage(struct vnode * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,struct cred * cr)53480Sstevel@tonic-gate ufs_putapage(
53490Sstevel@tonic-gate 	struct vnode *vp,
53500Sstevel@tonic-gate 	page_t *pp,
53510Sstevel@tonic-gate 	u_offset_t *offp,
53520Sstevel@tonic-gate 	size_t *lenp,		/* return values */
53530Sstevel@tonic-gate 	int flags,
53540Sstevel@tonic-gate 	struct cred *cr)
53550Sstevel@tonic-gate {
53560Sstevel@tonic-gate 	u_offset_t io_off;
53570Sstevel@tonic-gate 	u_offset_t off;
53580Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
53590Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
53600Sstevel@tonic-gate 	struct fs *fs;
53610Sstevel@tonic-gate 	struct buf *bp;
53620Sstevel@tonic-gate 	size_t io_len;
53630Sstevel@tonic-gate 	daddr_t bn;
53640Sstevel@tonic-gate 	int err;
53650Sstevel@tonic-gate 	int contig;
53665479Sfrankho 	int dotrans;
53670Sstevel@tonic-gate 
53680Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
53690Sstevel@tonic-gate 
53700Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
53710Sstevel@tonic-gate 		err = EIO;
53720Sstevel@tonic-gate 		goto out_trace;
53730Sstevel@tonic-gate 	}
53740Sstevel@tonic-gate 
53750Sstevel@tonic-gate 	fs = ip->i_fs;
53760Sstevel@tonic-gate 	ASSERT(fs->fs_ronly == 0);
53770Sstevel@tonic-gate 
53780Sstevel@tonic-gate 	/*
53790Sstevel@tonic-gate 	 * If the modified time on the inode has not already been
53805027Sdm120769 	 * set elsewhere (e.g. for write/setattr) we set the time now.
53810Sstevel@tonic-gate 	 * This gives us approximate modified times for mmap'ed files
53820Sstevel@tonic-gate 	 * which are modified via stores in the user address space.
53830Sstevel@tonic-gate 	 */
53845027Sdm120769 	if ((ip->i_flag & IMODTIME) == 0) {
53850Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
53860Sstevel@tonic-gate 		ip->i_flag |= IUPD;
53870Sstevel@tonic-gate 		ip->i_seq++;
53880Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
53890Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
53900Sstevel@tonic-gate 	}
53910Sstevel@tonic-gate 
53920Sstevel@tonic-gate 	/*
53930Sstevel@tonic-gate 	 * Align the request to a block boundry (for old file systems),
53940Sstevel@tonic-gate 	 * and go ask bmap() how contiguous things are for this file.
53950Sstevel@tonic-gate 	 */
53960Sstevel@tonic-gate 	off = pp->p_offset & (offset_t)fs->fs_bmask;	/* block align it */
53970Sstevel@tonic-gate 	contig = 0;
53980Sstevel@tonic-gate 	err = bmap_read(ip, off, &bn, &contig);
53990Sstevel@tonic-gate 	if (err)
54000Sstevel@tonic-gate 		goto out;
54010Sstevel@tonic-gate 	if (bn == UFS_HOLE) {			/* putpage never allocates */
54020Sstevel@tonic-gate 		/*
54030Sstevel@tonic-gate 		 * logging device is in error mode; simply return EIO
54040Sstevel@tonic-gate 		 */
54050Sstevel@tonic-gate 		if (TRANS_ISERROR(ufsvfsp)) {
54060Sstevel@tonic-gate 			err = EIO;
54070Sstevel@tonic-gate 			goto out;
54080Sstevel@tonic-gate 		}
54090Sstevel@tonic-gate 		/*
54100Sstevel@tonic-gate 		 * Oops, the thread in the window in wrip() did some
54110Sstevel@tonic-gate 		 * sort of operation which caused a putpage in the bad
54120Sstevel@tonic-gate 		 * range.  In this case, just return an error which will
54130Sstevel@tonic-gate 		 * cause the software modified bit on the page to set
54140Sstevel@tonic-gate 		 * and the page will get written out again later.
54150Sstevel@tonic-gate 		 */
54160Sstevel@tonic-gate 		if (ip->i_writer == curthread) {
54170Sstevel@tonic-gate 			err = EIO;
54180Sstevel@tonic-gate 			goto out;
54190Sstevel@tonic-gate 		}
54200Sstevel@tonic-gate 		/*
54210Sstevel@tonic-gate 		 * If the pager is trying to push a page in the bad range
54220Sstevel@tonic-gate 		 * just tell him to try again later when things are better.
54230Sstevel@tonic-gate 		 */
54240Sstevel@tonic-gate 		if (flags & B_ASYNC) {
54250Sstevel@tonic-gate 			err = EAGAIN;
54260Sstevel@tonic-gate 			goto out;
54270Sstevel@tonic-gate 		}
54280Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip), "ufs_putapage: bn == UFS_HOLE");
54290Sstevel@tonic-gate 		goto out;
54300Sstevel@tonic-gate 	}
54310Sstevel@tonic-gate 
54320Sstevel@tonic-gate 	/*
5433923Ssdebnath 	 * If it is an fallocate'd block, reverse the negativity since
5434923Ssdebnath 	 * we are now writing to it
5435923Ssdebnath 	 */
5436923Ssdebnath 	if (ISFALLOCBLK(ip, bn)) {
5437923Ssdebnath 		err = bmap_set_bn(vp, off, dbtofsb(fs, -bn));
5438923Ssdebnath 		if (err)
5439923Ssdebnath 			goto out;
5440923Ssdebnath 
5441923Ssdebnath 		bn = -bn;
5442923Ssdebnath 	}
5443923Ssdebnath 
5444923Ssdebnath 	/*
54450Sstevel@tonic-gate 	 * Take the length (of contiguous bytes) passed back from bmap()
54460Sstevel@tonic-gate 	 * and _try_ and get a set of pages covering that extent.
54470Sstevel@tonic-gate 	 */
54480Sstevel@tonic-gate 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, off, contig, flags);
54490Sstevel@tonic-gate 
54500Sstevel@tonic-gate 	/*
54510Sstevel@tonic-gate 	 * May have run out of memory and not clustered backwards.
54520Sstevel@tonic-gate 	 * off		p_offset
54530Sstevel@tonic-gate 	 * [  pp - 1  ][   pp   ]
54540Sstevel@tonic-gate 	 * [	block		]
54550Sstevel@tonic-gate 	 * We told bmap off, so we have to adjust the bn accordingly.
54560Sstevel@tonic-gate 	 */
54570Sstevel@tonic-gate 	if (io_off > off) {
54580Sstevel@tonic-gate 		bn += btod(io_off - off);
54590Sstevel@tonic-gate 		contig -= (io_off - off);
54600Sstevel@tonic-gate 	}
54610Sstevel@tonic-gate 
54620Sstevel@tonic-gate 	/*
54630Sstevel@tonic-gate 	 * bmap was carefull to tell us the right size so use that.
54640Sstevel@tonic-gate 	 * There might be unallocated frags at the end.
54650Sstevel@tonic-gate 	 * LMXXX - bzero the end of the page?  We must be writing after EOF.
54660Sstevel@tonic-gate 	 */
54670Sstevel@tonic-gate 	if (io_len > contig) {
54680Sstevel@tonic-gate 		ASSERT(io_len - contig < fs->fs_bsize);
54690Sstevel@tonic-gate 		io_len -= (io_len - contig);
54700Sstevel@tonic-gate 	}
54710Sstevel@tonic-gate 
54720Sstevel@tonic-gate 	/*
54730Sstevel@tonic-gate 	 * Handle the case where we are writing the last page after EOF.
54740Sstevel@tonic-gate 	 *
54750Sstevel@tonic-gate 	 * XXX - just a patch for i-mt3.
54760Sstevel@tonic-gate 	 */
54770Sstevel@tonic-gate 	if (io_len == 0) {
54784662Sfrankho 		ASSERT(pp->p_offset >=
54794662Sfrankho 		    (u_offset_t)(roundup(ip->i_size, PAGESIZE)));
54800Sstevel@tonic-gate 		io_len = PAGESIZE;
54810Sstevel@tonic-gate 	}
54820Sstevel@tonic-gate 
54830Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_WRITE | flags);
54840Sstevel@tonic-gate 
54850Sstevel@tonic-gate 	ULOCKFS_SET_MOD(ITOUL(ip));
54860Sstevel@tonic-gate 
54870Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
54880Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
54890Sstevel@tonic-gate 	bp->b_blkno = bn;
54900Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
54910Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
54920Sstevel@tonic-gate 
54935479Sfrankho 	/*
54945479Sfrankho 	 * File contents of shadow or quota inodes are metadata, and updates
54955479Sfrankho 	 * to these need to be put into a logging transaction. All direct
54965479Sfrankho 	 * callers in UFS do that, but fsflush can come here _before_ the
54975479Sfrankho 	 * normal codepath. An example would be updating ACL information, for
54985479Sfrankho 	 * which the normal codepath would be:
54995479Sfrankho 	 *	ufs_si_store()
55005479Sfrankho 	 *	ufs_rdwri()
55015479Sfrankho 	 *	wrip()
55025479Sfrankho 	 *	segmap_release()
55035479Sfrankho 	 *	VOP_PUTPAGE()
55045479Sfrankho 	 * Here, fsflush can pick up the dirty page before segmap_release()
55055479Sfrankho 	 * forces it out. If that happens, there's no transaction.
55065479Sfrankho 	 * We therefore need to test whether a transaction exists, and if not
55075479Sfrankho 	 * create one - for fsflush.
55085479Sfrankho 	 */
55095479Sfrankho 	dotrans =
55105479Sfrankho 	    (((ip->i_mode & IFMT) == IFSHAD || ufsvfsp->vfs_qinod == ip) &&
55115479Sfrankho 	    ((curthread->t_flag & T_DONTBLOCK) == 0) &&
55125479Sfrankho 	    (TRANS_ISTRANS(ufsvfsp)));
55135479Sfrankho 
55145479Sfrankho 	if (dotrans) {
55155479Sfrankho 		curthread->t_flag |= T_DONTBLOCK;
55165479Sfrankho 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
55175479Sfrankho 	}
55180Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
55190Sstevel@tonic-gate 		if ((ip->i_mode & IFMT) == IFSHAD) {
55200Sstevel@tonic-gate 			TRANS_BUF(ufsvfsp, 0, io_len, bp, DT_SHAD);
55210Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_qinod == ip) {
55220Sstevel@tonic-gate 			TRANS_DELTA(ufsvfsp, ldbtob(bn), bp->b_bcount, DT_QR,
55230Sstevel@tonic-gate 			    0, 0);
55240Sstevel@tonic-gate 		}
55250Sstevel@tonic-gate 	}
55265479Sfrankho 	if (dotrans) {
55275479Sfrankho 		TRANS_END_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
55285479Sfrankho 		curthread->t_flag &= ~T_DONTBLOCK;
55295479Sfrankho 	}
55300Sstevel@tonic-gate 
55310Sstevel@tonic-gate 	/* write throttle */
55320Sstevel@tonic-gate 
55330Sstevel@tonic-gate 	ASSERT(bp->b_iodone == NULL);
55340Sstevel@tonic-gate 	bp->b_iodone = (int (*)())ufs_iodone;
55350Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
55360Sstevel@tonic-gate 	ip->i_writes += bp->b_bcount;
55370Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
55380Sstevel@tonic-gate 
55390Sstevel@tonic-gate 	if (bp->b_flags & B_ASYNC) {
55400Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
55410Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
55420Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
55430Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
55440Sstevel@tonic-gate 		} else {
554511066Srafael.vanoni@sun.com 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
55460Sstevel@tonic-gate 			ub.ub_putasyncs.value.ul++;
55470Sstevel@tonic-gate 			(void) bdev_strategy(bp);
55480Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
55490Sstevel@tonic-gate 		}
55500Sstevel@tonic-gate 	} else {
55510Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
55520Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
55530Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
55540Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
55550Sstevel@tonic-gate 		} else {
555611066Srafael.vanoni@sun.com 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
55570Sstevel@tonic-gate 			ub.ub_putsyncs.value.ul++;
55580Sstevel@tonic-gate 			(void) bdev_strategy(bp);
55590Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
55600Sstevel@tonic-gate 		}
55610Sstevel@tonic-gate 		err = biowait(bp);
55620Sstevel@tonic-gate 		pageio_done(bp);
55630Sstevel@tonic-gate 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
55640Sstevel@tonic-gate 	}
55650Sstevel@tonic-gate 
55660Sstevel@tonic-gate 	pp = NULL;
55670Sstevel@tonic-gate 
55680Sstevel@tonic-gate out:
55690Sstevel@tonic-gate 	if (err != 0 && pp != NULL)
55700Sstevel@tonic-gate 		pvn_write_done(pp, B_ERROR | B_WRITE | flags);
55710Sstevel@tonic-gate 
55720Sstevel@tonic-gate 	if (offp)
55730Sstevel@tonic-gate 		*offp = io_off;
55740Sstevel@tonic-gate 	if (lenp)
55750Sstevel@tonic-gate 		*lenp = io_len;
55760Sstevel@tonic-gate out_trace:
55770Sstevel@tonic-gate 	return (err);
55780Sstevel@tonic-gate }
55790Sstevel@tonic-gate 
55805859Svsakar uint64_t ufs_map_alock_retry_cnt;
55815859Svsakar uint64_t ufs_map_lockfs_retry_cnt;
55825859Svsakar 
55830Sstevel@tonic-gate /* ARGSUSED */
55840Sstevel@tonic-gate static int
ufs_map(struct vnode * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)55850Sstevel@tonic-gate ufs_map(struct vnode *vp,
55860Sstevel@tonic-gate 	offset_t off,
55870Sstevel@tonic-gate 	struct as *as,
55880Sstevel@tonic-gate 	caddr_t *addrp,
55890Sstevel@tonic-gate 	size_t len,
55900Sstevel@tonic-gate 	uchar_t prot,
55910Sstevel@tonic-gate 	uchar_t maxprot,
55920Sstevel@tonic-gate 	uint_t flags,
55935331Samw 	struct cred *cr,
55945331Samw 	caller_context_t *ct)
55950Sstevel@tonic-gate {
55960Sstevel@tonic-gate 	struct segvn_crargs vn_a;
55970Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
55980Sstevel@tonic-gate 	struct ulockfs *ulp;
55995859Svsakar 	int error, sig;
56005859Svsakar 	k_sigset_t smask;
56016715Svsakar 	caddr_t hint = *addrp;
56020Sstevel@tonic-gate 
56030Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
56040Sstevel@tonic-gate 		error = ENOSYS;
56055859Svsakar 		goto out;
56060Sstevel@tonic-gate 	}
56070Sstevel@tonic-gate 
56080Sstevel@tonic-gate 	if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0) {
56090Sstevel@tonic-gate 		error = ENXIO;
56105859Svsakar 		goto out;
56110Sstevel@tonic-gate 	}
56120Sstevel@tonic-gate 
56130Sstevel@tonic-gate 	if (vp->v_type != VREG) {
56140Sstevel@tonic-gate 		error = ENODEV;
56155859Svsakar 		goto out;
56165859Svsakar 	}
56175859Svsakar 
56185859Svsakar retry_map:
56196715Svsakar 	*addrp = hint;
56200Sstevel@tonic-gate 	/*
56210Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
56220Sstevel@tonic-gate 	 */
56230Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOI(vp)->i_mode)) {
56240Sstevel@tonic-gate 		error = EAGAIN;
56255859Svsakar 		goto out;
56260Sstevel@tonic-gate 	}
56270Sstevel@tonic-gate 
56280Sstevel@tonic-gate 	as_rangelock(as);
56296036Smec 	/*
56306036Smec 	 * Note that if we are retrying (because ufs_lockfs_trybegin failed in
56316036Smec 	 * the previous attempt), some other thread could have grabbed
56326036Smec 	 * the same VA range if MAP_FIXED is set. In that case, choose_addr
56336036Smec 	 * would unmap the valid VA range, that is ok.
56346036Smec 	 */
56356036Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
56366036Smec 	if (error != 0) {
56376036Smec 		as_rangeunlock(as);
56386036Smec 		goto out;
56390Sstevel@tonic-gate 	}
56400Sstevel@tonic-gate 
56415859Svsakar 	/*
56425859Svsakar 	 * a_lock has to be acquired before entering the lockfs protocol
56435859Svsakar 	 * because that is the order in which pagefault works. Also we cannot
56445859Svsakar 	 * block on a_lock here because this waiting writer will prevent
56455859Svsakar 	 * further readers like ufs_read from progressing and could cause
56465859Svsakar 	 * deadlock between ufs_read/ufs_map/pagefault when a quiesce is
56475859Svsakar 	 * pending.
56485859Svsakar 	 */
56495859Svsakar 	while (!AS_LOCK_TRYENTER(as, &as->a_lock, RW_WRITER)) {
56505859Svsakar 		ufs_map_alock_retry_cnt++;
56515859Svsakar 		delay(RETRY_LOCK_DELAY);
56525859Svsakar 	}
56535859Svsakar 
56545859Svsakar 	/*
56555859Svsakar 	 * We can't hold as->a_lock and wait for lockfs to succeed because
56565859Svsakar 	 * the proc tools might hang on a_lock, so call ufs_lockfs_trybegin()
56575859Svsakar 	 * instead.
56585859Svsakar 	 */
56595859Svsakar 	if (error = ufs_lockfs_trybegin(ufsvfsp, &ulp, ULOCKFS_MAP_MASK)) {
56605859Svsakar 		/*
56615859Svsakar 		 * ufs_lockfs_trybegin() did not succeed. It is safer to give up
56625859Svsakar 		 * as->a_lock and wait for ulp->ul_fs_lock status to change.
56635859Svsakar 		 */
56645859Svsakar 		ufs_map_lockfs_retry_cnt++;
56655859Svsakar 		AS_LOCK_EXIT(as, &as->a_lock);
56665859Svsakar 		as_rangeunlock(as);
56675859Svsakar 		if (error == EIO)
56685859Svsakar 			goto out;
56695859Svsakar 
56705859Svsakar 		mutex_enter(&ulp->ul_lock);
56715859Svsakar 		while (ulp->ul_fs_lock & ULOCKFS_MAP_MASK) {
56725859Svsakar 			if (ULOCKFS_IS_SLOCK(ulp) || ufsvfsp->vfs_nointr) {
56735859Svsakar 				cv_wait(&ulp->ul_cv, &ulp->ul_lock);
56745859Svsakar 			} else {
56755859Svsakar 				sigintr(&smask, 1);
56765859Svsakar 				sig = cv_wait_sig(&ulp->ul_cv, &ulp->ul_lock);
56775859Svsakar 				sigunintr(&smask);
56785859Svsakar 				if (((ulp->ul_fs_lock & ULOCKFS_MAP_MASK) &&
56795859Svsakar 				    !sig) || ufsvfsp->vfs_dontblock) {
56805859Svsakar 					mutex_exit(&ulp->ul_lock);
56815859Svsakar 					return (EINTR);
56825859Svsakar 				}
56835859Svsakar 			}
56845859Svsakar 		}
56855859Svsakar 		mutex_exit(&ulp->ul_lock);
56865859Svsakar 		goto retry_map;
56875859Svsakar 	}
56885859Svsakar 
56890Sstevel@tonic-gate 	vn_a.vp = vp;
56900Sstevel@tonic-gate 	vn_a.offset = (u_offset_t)off;
56910Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
56920Sstevel@tonic-gate 	vn_a.prot = prot;
56930Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
56940Sstevel@tonic-gate 	vn_a.cred = cr;
56950Sstevel@tonic-gate 	vn_a.amp = NULL;
56960Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
56970Sstevel@tonic-gate 	vn_a.szc = 0;
56980Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
56990Sstevel@tonic-gate 
57001899Svsakar 	error = as_map_locked(as, *addrp, len, segvn_create, &vn_a);
57015859Svsakar 	if (ulp)
57025859Svsakar 		ufs_lockfs_end(ulp);
57030Sstevel@tonic-gate 	as_rangeunlock(as);
57040Sstevel@tonic-gate out:
57050Sstevel@tonic-gate 	return (error);
57060Sstevel@tonic-gate }
57070Sstevel@tonic-gate 
57080Sstevel@tonic-gate /* ARGSUSED */
57090Sstevel@tonic-gate static int
ufs_addmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)57100Sstevel@tonic-gate ufs_addmap(struct vnode *vp,
57110Sstevel@tonic-gate 	offset_t off,
57120Sstevel@tonic-gate 	struct as *as,
57130Sstevel@tonic-gate 	caddr_t addr,
57140Sstevel@tonic-gate 	size_t	len,
57150Sstevel@tonic-gate 	uchar_t  prot,
57160Sstevel@tonic-gate 	uchar_t  maxprot,
57170Sstevel@tonic-gate 	uint_t    flags,
57185331Samw 	struct cred *cr,
57195331Samw 	caller_context_t *ct)
57200Sstevel@tonic-gate {
57210Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
57220Sstevel@tonic-gate 
57230Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
57240Sstevel@tonic-gate 		return (ENOSYS);
57250Sstevel@tonic-gate 	}
57260Sstevel@tonic-gate 
57270Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
57280Sstevel@tonic-gate 	ip->i_mapcnt += btopr(len);
57290Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
57300Sstevel@tonic-gate 	return (0);
57310Sstevel@tonic-gate }
57320Sstevel@tonic-gate 
57330Sstevel@tonic-gate /*ARGSUSED*/
57340Sstevel@tonic-gate static int
ufs_delmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)57350Sstevel@tonic-gate ufs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
57360Sstevel@tonic-gate 	size_t len, uint_t prot,  uint_t maxprot,  uint_t flags,
57375331Samw 	struct cred *cr, caller_context_t *ct)
57380Sstevel@tonic-gate {
57390Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
57400Sstevel@tonic-gate 
57410Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
57420Sstevel@tonic-gate 		return (ENOSYS);
57430Sstevel@tonic-gate 	}
57440Sstevel@tonic-gate 
57450Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
57460Sstevel@tonic-gate 	ip->i_mapcnt -= btopr(len); 	/* Count released mappings */
57470Sstevel@tonic-gate 	ASSERT(ip->i_mapcnt >= 0);
57480Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
57490Sstevel@tonic-gate 	return (0);
57500Sstevel@tonic-gate }
57510Sstevel@tonic-gate /*
57520Sstevel@tonic-gate  * Return the answer requested to poll() for non-device files
57530Sstevel@tonic-gate  */
57540Sstevel@tonic-gate struct pollhead ufs_pollhd;
57550Sstevel@tonic-gate 
57560Sstevel@tonic-gate /* ARGSUSED */
57570Sstevel@tonic-gate int
ufs_poll(vnode_t * vp,short ev,int any,short * revp,struct pollhead ** phpp,caller_context_t * ct)57585331Samw ufs_poll(vnode_t *vp, short ev, int any, short *revp, struct pollhead **phpp,
57595331Samw 	caller_context_t *ct)
57600Sstevel@tonic-gate {
57610Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
57620Sstevel@tonic-gate 
57630Sstevel@tonic-gate 	*revp = 0;
57640Sstevel@tonic-gate 	ufsvfsp = VTOI(vp)->i_ufsvfs;
57650Sstevel@tonic-gate 
57660Sstevel@tonic-gate 	if (!ufsvfsp) {
57670Sstevel@tonic-gate 		*revp = POLLHUP;
57680Sstevel@tonic-gate 		goto out;
57690Sstevel@tonic-gate 	}
57700Sstevel@tonic-gate 
57710Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs) ||
57720Sstevel@tonic-gate 	    ULOCKFS_IS_ELOCK(&ufsvfsp->vfs_ulockfs)) {
57730Sstevel@tonic-gate 		*revp |= POLLERR;
57740Sstevel@tonic-gate 
57750Sstevel@tonic-gate 	} else {
57760Sstevel@tonic-gate 		if ((ev & POLLOUT) && !ufsvfsp->vfs_fs->fs_ronly &&
57770Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
57780Sstevel@tonic-gate 			*revp |= POLLOUT;
57790Sstevel@tonic-gate 
57800Sstevel@tonic-gate 		if ((ev & POLLWRBAND) && !ufsvfsp->vfs_fs->fs_ronly &&
57810Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
57820Sstevel@tonic-gate 			*revp |= POLLWRBAND;
57830Sstevel@tonic-gate 
57840Sstevel@tonic-gate 		if (ev & POLLIN)
57850Sstevel@tonic-gate 			*revp |= POLLIN;
57860Sstevel@tonic-gate 
57870Sstevel@tonic-gate 		if (ev & POLLRDNORM)
57880Sstevel@tonic-gate 			*revp |= POLLRDNORM;
57890Sstevel@tonic-gate 
57900Sstevel@tonic-gate 		if (ev & POLLRDBAND)
57910Sstevel@tonic-gate 			*revp |= POLLRDBAND;
57920Sstevel@tonic-gate 	}
57930Sstevel@tonic-gate 
57940Sstevel@tonic-gate 	if ((ev & POLLPRI) && (*revp & (POLLERR|POLLHUP)))
57950Sstevel@tonic-gate 		*revp |= POLLPRI;
57960Sstevel@tonic-gate out:
57970Sstevel@tonic-gate 	*phpp = !any && !*revp ? &ufs_pollhd : (struct pollhead *)NULL;
57980Sstevel@tonic-gate 
57990Sstevel@tonic-gate 	return (0);
58000Sstevel@tonic-gate }
58010Sstevel@tonic-gate 
58020Sstevel@tonic-gate /* ARGSUSED */
58030Sstevel@tonic-gate static int
ufs_l_pathconf(struct vnode * vp,int cmd,ulong_t * valp,struct cred * cr,caller_context_t * ct)58045331Samw ufs_l_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr,
58055331Samw 	caller_context_t *ct)
58060Sstevel@tonic-gate {
58070Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
58080Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
58090Sstevel@tonic-gate 	struct inode 	*sip = NULL;
58100Sstevel@tonic-gate 	int		error;
58110Sstevel@tonic-gate 	struct inode 	*ip = VTOI(vp);
58120Sstevel@tonic-gate 	int		issync;
58130Sstevel@tonic-gate 
58140Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_PATHCONF_MASK);
58150Sstevel@tonic-gate 	if (error)
58160Sstevel@tonic-gate 		return (error);
58170Sstevel@tonic-gate 
58180Sstevel@tonic-gate 	switch (cmd) {
58190Sstevel@tonic-gate 		/*
58200Sstevel@tonic-gate 		 * Have to handle _PC_NAME_MAX here, because the normal way
58210Sstevel@tonic-gate 		 * [fs_pathconf() -> VOP_STATVFS() -> ufs_statvfs()]
58220Sstevel@tonic-gate 		 * results in a lock ordering reversal between
58230Sstevel@tonic-gate 		 * ufs_lockfs_{begin,end}() and
58240Sstevel@tonic-gate 		 * ufs_thread_{suspend,continue}().
58250Sstevel@tonic-gate 		 *
58260Sstevel@tonic-gate 		 * Keep in sync with ufs_statvfs().
58270Sstevel@tonic-gate 		 */
58280Sstevel@tonic-gate 	case _PC_NAME_MAX:
58290Sstevel@tonic-gate 		*valp = MAXNAMLEN;
58300Sstevel@tonic-gate 		break;
58310Sstevel@tonic-gate 
58320Sstevel@tonic-gate 	case _PC_FILESIZEBITS:
58330Sstevel@tonic-gate 		if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
58340Sstevel@tonic-gate 			*valp = UFS_FILESIZE_BITS;
58350Sstevel@tonic-gate 		else
58360Sstevel@tonic-gate 			*valp = 32;
58370Sstevel@tonic-gate 		break;
58380Sstevel@tonic-gate 
58390Sstevel@tonic-gate 	case _PC_XATTR_EXISTS:
58400Sstevel@tonic-gate 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
58410Sstevel@tonic-gate 
58424662Sfrankho 			error =
58434662Sfrankho 			    ufs_xattr_getattrdir(vp, &sip, LOOKUP_XATTR, cr);
58440Sstevel@tonic-gate 			if (error ==  0 && sip != NULL) {
58450Sstevel@tonic-gate 				/* Start transaction */
58460Sstevel@tonic-gate 				if (ulp) {
58470Sstevel@tonic-gate 					TRANS_BEGIN_CSYNC(ufsvfsp, issync,
58480Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
58490Sstevel@tonic-gate 				}
58500Sstevel@tonic-gate 				/*
58510Sstevel@tonic-gate 				 * Is directory empty
58520Sstevel@tonic-gate 				 */
58530Sstevel@tonic-gate 				rw_enter(&sip->i_rwlock, RW_WRITER);
58540Sstevel@tonic-gate 				rw_enter(&sip->i_contents, RW_WRITER);
58550Sstevel@tonic-gate 				if (ufs_xattrdirempty(sip,
58564662Sfrankho 				    sip->i_number, CRED())) {
58570Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
58580Sstevel@tonic-gate 					ufs_unhook_shadow(ip, sip);
58590Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
58600Sstevel@tonic-gate 
58610Sstevel@tonic-gate 					*valp = 0;
58620Sstevel@tonic-gate 
58630Sstevel@tonic-gate 				} else
58640Sstevel@tonic-gate 					*valp = 1;
58650Sstevel@tonic-gate 				rw_exit(&sip->i_contents);
58660Sstevel@tonic-gate 				rw_exit(&sip->i_rwlock);
58670Sstevel@tonic-gate 				if (ulp) {
58680Sstevel@tonic-gate 					TRANS_END_CSYNC(ufsvfsp, error, issync,
58690Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
58700Sstevel@tonic-gate 				}
58710Sstevel@tonic-gate 				VN_RELE(ITOV(sip));
58720Sstevel@tonic-gate 			} else if (error == ENOENT) {
58730Sstevel@tonic-gate 				*valp = 0;
58740Sstevel@tonic-gate 				error = 0;
58750Sstevel@tonic-gate 			}
58760Sstevel@tonic-gate 		} else {
58775331Samw 			error = fs_pathconf(vp, cmd, valp, cr, ct);
58780Sstevel@tonic-gate 		}
58790Sstevel@tonic-gate 		break;
58800Sstevel@tonic-gate 
58810Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
58820Sstevel@tonic-gate 		*valp = _ACL_ACLENT_ENABLED;
58830Sstevel@tonic-gate 		break;
58840Sstevel@tonic-gate 
58850Sstevel@tonic-gate 	case _PC_MIN_HOLE_SIZE:
58860Sstevel@tonic-gate 		*valp = (ulong_t)ip->i_fs->fs_bsize;
58870Sstevel@tonic-gate 		break;
58880Sstevel@tonic-gate 
58895331Samw 	case _PC_SATTR_ENABLED:
58905331Samw 	case _PC_SATTR_EXISTS:
58917757SJanice.Chang@Sun.COM 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
58925331Samw 		    (vp->v_type == VREG || vp->v_type == VDIR);
58935331Samw 		break;
58945331Samw 
589510440SRoger.Faulkner@Sun.COM 	case _PC_TIMESTAMP_RESOLUTION:
589610440SRoger.Faulkner@Sun.COM 		/*
589710440SRoger.Faulkner@Sun.COM 		 * UFS keeps only microsecond timestamp resolution.
589810440SRoger.Faulkner@Sun.COM 		 * This is historical and will probably never change.
589910440SRoger.Faulkner@Sun.COM 		 */
590010440SRoger.Faulkner@Sun.COM 		*valp = 1000L;
590110440SRoger.Faulkner@Sun.COM 		break;
590210440SRoger.Faulkner@Sun.COM 
59030Sstevel@tonic-gate 	default:
59045331Samw 		error = fs_pathconf(vp, cmd, valp, cr, ct);
590510440SRoger.Faulkner@Sun.COM 		break;
59060Sstevel@tonic-gate 	}
59070Sstevel@tonic-gate 
59080Sstevel@tonic-gate 	if (ulp != NULL) {
59090Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
59100Sstevel@tonic-gate 	}
59110Sstevel@tonic-gate 	return (error);
59120Sstevel@tonic-gate }
59130Sstevel@tonic-gate 
59140Sstevel@tonic-gate int ufs_pageio_writes, ufs_pageio_reads;
59150Sstevel@tonic-gate 
59160Sstevel@tonic-gate /*ARGSUSED*/
59170Sstevel@tonic-gate static int
ufs_pageio(struct vnode * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,struct cred * cr,caller_context_t * ct)59180Sstevel@tonic-gate ufs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
59195331Samw 	int flags, struct cred *cr, caller_context_t *ct)
59200Sstevel@tonic-gate {
59210Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
59220Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
59230Sstevel@tonic-gate 	page_t *npp = NULL, *opp = NULL, *cpp = pp;
59240Sstevel@tonic-gate 	struct buf *bp;
59250Sstevel@tonic-gate 	daddr_t bn;
59260Sstevel@tonic-gate 	size_t done_len = 0, cur_len = 0;
59270Sstevel@tonic-gate 	int err = 0;
59280Sstevel@tonic-gate 	int contig = 0;
59290Sstevel@tonic-gate 	int dolock;
59300Sstevel@tonic-gate 	int vmpss = 0;
5931329Saguzovsk 	struct ulockfs *ulp;
59320Sstevel@tonic-gate 
59330Sstevel@tonic-gate 	if ((flags & B_READ) && pp != NULL && pp->p_vnode == vp &&
59340Sstevel@tonic-gate 	    vp->v_mpssdata != NULL) {
59350Sstevel@tonic-gate 		vmpss = 1;
59360Sstevel@tonic-gate 	}
59370Sstevel@tonic-gate 
59380Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
59390Sstevel@tonic-gate 	/*
59400Sstevel@tonic-gate 	 * We need a better check.  Ideally, we would use another
59410Sstevel@tonic-gate 	 * vnodeops so that hlocked and forcibly unmounted file
59420Sstevel@tonic-gate 	 * systems would return EIO where appropriate and w/o the
59430Sstevel@tonic-gate 	 * need for these checks.
59440Sstevel@tonic-gate 	 */
59450Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
59460Sstevel@tonic-gate 		return (EIO);
59470Sstevel@tonic-gate 
5948329Saguzovsk 	/*
5949329Saguzovsk 	 * For vmpss (pp can be NULL) case respect the quiesce protocol.
5950329Saguzovsk 	 * ul_lock must be taken before locking pages so we can't use it here
5951329Saguzovsk 	 * if pp is non NULL because segvn already locked pages
5952329Saguzovsk 	 * SE_EXCL. Instead we rely on the fact that a forced umount or
5953329Saguzovsk 	 * applying a filesystem lock via ufs_fiolfs() will block in the
5954329Saguzovsk 	 * implicit call to ufs_flush() until we unlock the pages after the
5955329Saguzovsk 	 * return to segvn. Other ufs_quiesce() callers keep ufs_quiesce_pend
5956329Saguzovsk 	 * above 0 until they are done. We have to be careful not to increment
5957329Saguzovsk 	 * ul_vnops_cnt here after forceful unmount hlocks the file system.
5958329Saguzovsk 	 *
5959329Saguzovsk 	 * If pp is NULL use ul_lock to make sure we don't increment
5960329Saguzovsk 	 * ul_vnops_cnt after forceful unmount hlocks the file system.
5961329Saguzovsk 	 */
5962329Saguzovsk 	if (vmpss || pp == NULL) {
5963329Saguzovsk 		ulp = &ufsvfsp->vfs_ulockfs;
5964329Saguzovsk 		if (pp == NULL)
5965329Saguzovsk 			mutex_enter(&ulp->ul_lock);
5966329Saguzovsk 		if (ulp->ul_fs_lock & ULOCKFS_GETREAD_MASK) {
5967329Saguzovsk 			if (pp == NULL) {
5968329Saguzovsk 				mutex_exit(&ulp->ul_lock);
5969329Saguzovsk 			}
5970329Saguzovsk 			return (vmpss ? EIO : EINVAL);
5971329Saguzovsk 		}
5972329Saguzovsk 		atomic_add_long(&ulp->ul_vnops_cnt, 1);
5973329Saguzovsk 		if (pp == NULL)
5974329Saguzovsk 			mutex_exit(&ulp->ul_lock);
5975329Saguzovsk 		if (ufs_quiesce_pend) {
5976329Saguzovsk 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5977329Saguzovsk 				cv_broadcast(&ulp->ul_cv);
5978329Saguzovsk 			return (vmpss ? EIO : EINVAL);
5979329Saguzovsk 		}
5980329Saguzovsk 	}
5981329Saguzovsk 
59820Sstevel@tonic-gate 	if (dolock) {
59830Sstevel@tonic-gate 		/*
59840Sstevel@tonic-gate 		 * segvn may call VOP_PAGEIO() instead of VOP_GETPAGE() to
59850Sstevel@tonic-gate 		 * handle a fault against a segment that maps vnode pages with
59860Sstevel@tonic-gate 		 * large mappings.  Segvn creates pages and holds them locked
59870Sstevel@tonic-gate 		 * SE_EXCL during VOP_PAGEIO() call. In this case we have to
59880Sstevel@tonic-gate 		 * use rw_tryenter() to avoid a potential deadlock since in
59890Sstevel@tonic-gate 		 * lock order i_contents needs to be taken first.
59900Sstevel@tonic-gate 		 * Segvn will retry via VOP_GETPAGE() if VOP_PAGEIO() fails.
59910Sstevel@tonic-gate 		 */
59920Sstevel@tonic-gate 		if (!vmpss) {
59930Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
59940Sstevel@tonic-gate 		} else if (!rw_tryenter(&ip->i_contents, RW_READER)) {
5995329Saguzovsk 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5996329Saguzovsk 				cv_broadcast(&ulp->ul_cv);
59970Sstevel@tonic-gate 			return (EDEADLK);
59980Sstevel@tonic-gate 		}
59990Sstevel@tonic-gate 	}
60000Sstevel@tonic-gate 
600163Saguzovsk 	/*
600263Saguzovsk 	 * Return an error to segvn because the pagefault request is beyond
600363Saguzovsk 	 * PAGESIZE rounded EOF.
600463Saguzovsk 	 */
600563Saguzovsk 	if (vmpss && btopr(io_off + io_len) > btopr(ip->i_size)) {
600663Saguzovsk 		if (dolock)
600763Saguzovsk 			rw_exit(&ip->i_contents);
6008329Saguzovsk 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
6009329Saguzovsk 			cv_broadcast(&ulp->ul_cv);
601063Saguzovsk 		return (EFAULT);
601163Saguzovsk 	}
601263Saguzovsk 
60130Sstevel@tonic-gate 	if (pp == NULL) {
60140Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
60150Sstevel@tonic-gate 			err = ENOSYS;
60160Sstevel@tonic-gate 		} else {
60170Sstevel@tonic-gate 			err = EINVAL;
60180Sstevel@tonic-gate 		}
60190Sstevel@tonic-gate 		if (dolock)
60200Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
6021329Saguzovsk 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
6022329Saguzovsk 			cv_broadcast(&ulp->ul_cv);
60230Sstevel@tonic-gate 		return (err);
60240Sstevel@tonic-gate 	}
60250Sstevel@tonic-gate 
60260Sstevel@tonic-gate 	/*
60270Sstevel@tonic-gate 	 * Break the io request into chunks, one for each contiguous
60280Sstevel@tonic-gate 	 * stretch of disk blocks in the target file.
60290Sstevel@tonic-gate 	 */
60300Sstevel@tonic-gate 	while (done_len < io_len) {
60310Sstevel@tonic-gate 		ASSERT(cpp);
60320Sstevel@tonic-gate 		contig = 0;
60330Sstevel@tonic-gate 		if (err = bmap_read(ip, (u_offset_t)(io_off + done_len),
60344662Sfrankho 		    &bn, &contig))
60350Sstevel@tonic-gate 			break;
60360Sstevel@tonic-gate 
60370Sstevel@tonic-gate 		if (bn == UFS_HOLE) {	/* No holey swapfiles */
60380Sstevel@tonic-gate 			if (vmpss) {
60390Sstevel@tonic-gate 				err = EFAULT;
60400Sstevel@tonic-gate 				break;
60410Sstevel@tonic-gate 			}
60420Sstevel@tonic-gate 			err = ufs_fault(ITOV(ip), "ufs_pageio: bn == UFS_HOLE");
60430Sstevel@tonic-gate 			break;
60440Sstevel@tonic-gate 		}
60450Sstevel@tonic-gate 
60460Sstevel@tonic-gate 		cur_len = MIN(io_len - done_len, contig);
60470Sstevel@tonic-gate 		/*
60480Sstevel@tonic-gate 		 * Zero out a page beyond EOF, when the last block of
60490Sstevel@tonic-gate 		 * a file is a UFS fragment so that ufs_pageio() can be used
60500Sstevel@tonic-gate 		 * instead of ufs_getpage() to handle faults against
60510Sstevel@tonic-gate 		 * segvn segments that use large pages.
60520Sstevel@tonic-gate 		 */
60530Sstevel@tonic-gate 		page_list_break(&cpp, &npp, btopr(cur_len));
60540Sstevel@tonic-gate 		if ((flags & B_READ) && (cur_len & PAGEOFFSET)) {
60550Sstevel@tonic-gate 			size_t xlen = cur_len & PAGEOFFSET;
60560Sstevel@tonic-gate 			pagezero(cpp->p_prev, xlen, PAGESIZE - xlen);
60570Sstevel@tonic-gate 		}
60580Sstevel@tonic-gate 
60590Sstevel@tonic-gate 		bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
60600Sstevel@tonic-gate 		ASSERT(bp != NULL);
60610Sstevel@tonic-gate 
60620Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
60630Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
60640Sstevel@tonic-gate 		bp->b_blkno = bn;
60650Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
60660Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
60670Sstevel@tonic-gate 
606811066Srafael.vanoni@sun.com 		ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
60690Sstevel@tonic-gate 		ub.ub_pageios.value.ul++;
60700Sstevel@tonic-gate 		if (ufsvfsp->vfs_snapshot)
60710Sstevel@tonic-gate 			fssnap_strategy(&(ufsvfsp->vfs_snapshot), bp);
60720Sstevel@tonic-gate 		else
60730Sstevel@tonic-gate 			(void) bdev_strategy(bp);
60740Sstevel@tonic-gate 
60750Sstevel@tonic-gate 		if (flags & B_READ)
60760Sstevel@tonic-gate 			ufs_pageio_reads++;
60770Sstevel@tonic-gate 		else
60780Sstevel@tonic-gate 			ufs_pageio_writes++;
60790Sstevel@tonic-gate 		if (flags & B_READ)
60800Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
60810Sstevel@tonic-gate 		else
60820Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
60830Sstevel@tonic-gate 		/*
60840Sstevel@tonic-gate 		 * If the request is not B_ASYNC, wait for i/o to complete
60850Sstevel@tonic-gate 		 * and re-assemble the page list to return to the caller.
60860Sstevel@tonic-gate 		 * If it is B_ASYNC we leave the page list in pieces and
60870Sstevel@tonic-gate 		 * cleanup() will dispose of them.
60880Sstevel@tonic-gate 		 */
60890Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
60900Sstevel@tonic-gate 			err = biowait(bp);
60910Sstevel@tonic-gate 			pageio_done(bp);
60920Sstevel@tonic-gate 			if (err)
60930Sstevel@tonic-gate 				break;
60940Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
60950Sstevel@tonic-gate 		}
60960Sstevel@tonic-gate 		cpp = npp;
60970Sstevel@tonic-gate 		npp = NULL;
60980Sstevel@tonic-gate 		if (flags & B_READ)
60990Sstevel@tonic-gate 			cur_len = P2ROUNDUP_TYPED(cur_len, PAGESIZE, size_t);
61000Sstevel@tonic-gate 		done_len += cur_len;
61010Sstevel@tonic-gate 	}
61020Sstevel@tonic-gate 	ASSERT(err || (cpp == NULL && npp == NULL && done_len == io_len));
61030Sstevel@tonic-gate 	if (err) {
61040Sstevel@tonic-gate 		if (flags & B_ASYNC) {
61050Sstevel@tonic-gate 			/* Cleanup unprocessed parts of list */
61060Sstevel@tonic-gate 			page_list_concat(&cpp, &npp);
61070Sstevel@tonic-gate 			if (flags & B_READ)
61080Sstevel@tonic-gate 				pvn_read_done(cpp, B_ERROR);
61090Sstevel@tonic-gate 			else
61100Sstevel@tonic-gate 				pvn_write_done(cpp, B_ERROR);
61110Sstevel@tonic-gate 		} else {
61120Sstevel@tonic-gate 			/* Re-assemble list and let caller clean up */
61130Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
61140Sstevel@tonic-gate 			page_list_concat(&opp, &npp);
61150Sstevel@tonic-gate 		}
61160Sstevel@tonic-gate 	}
6117329Saguzovsk 
6118329Saguzovsk 	if (vmpss && !(ip->i_flag & IACC) && !ULOCKFS_IS_NOIACC(ulp) &&
6119329Saguzovsk 	    ufsvfsp->vfs_fs->fs_ronly == 0 && !ufsvfsp->vfs_noatime) {
6120329Saguzovsk 		mutex_enter(&ip->i_tlock);
6121329Saguzovsk 		ip->i_flag |= IACC;
6122329Saguzovsk 		ITIMES_NOLOCK(ip);
6123329Saguzovsk 		mutex_exit(&ip->i_tlock);
6124329Saguzovsk 	}
6125329Saguzovsk 
61260Sstevel@tonic-gate 	if (dolock)
61270Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
6128329Saguzovsk 	if (vmpss && !atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
6129329Saguzovsk 		cv_broadcast(&ulp->ul_cv);
61300Sstevel@tonic-gate 	return (err);
61310Sstevel@tonic-gate }
61320Sstevel@tonic-gate 
61330Sstevel@tonic-gate /*
61340Sstevel@tonic-gate  * Called when the kernel is in a frozen state to dump data
61350Sstevel@tonic-gate  * directly to the device. It uses a private dump data structure,
61360Sstevel@tonic-gate  * set up by dump_ctl, to locate the correct disk block to which to dump.
61370Sstevel@tonic-gate  */
61385331Samw /*ARGSUSED*/
61390Sstevel@tonic-gate static int
ufs_dump(vnode_t * vp,caddr_t addr,offset_t ldbn,offset_t dblks,caller_context_t * ct)61406264Srm15945 ufs_dump(vnode_t *vp, caddr_t addr, offset_t ldbn, offset_t dblks,
61416264Srm15945     caller_context_t *ct)
61420Sstevel@tonic-gate {
61430Sstevel@tonic-gate 	u_offset_t	file_size;
61440Sstevel@tonic-gate 	struct inode    *ip = VTOI(vp);
61450Sstevel@tonic-gate 	struct fs	*fs = ip->i_fs;
61460Sstevel@tonic-gate 	daddr_t		dbn, lfsbn;
61470Sstevel@tonic-gate 	int		disk_blks = fs->fs_bsize >> DEV_BSHIFT;
61480Sstevel@tonic-gate 	int		error = 0;
61490Sstevel@tonic-gate 	int		ndbs, nfsbs;
61500Sstevel@tonic-gate 
61510Sstevel@tonic-gate 	/*
61520Sstevel@tonic-gate 	 * forced unmount case
61530Sstevel@tonic-gate 	 */
61540Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
61550Sstevel@tonic-gate 		return (EIO);
61560Sstevel@tonic-gate 	/*
61570Sstevel@tonic-gate 	 * Validate the inode that it has not been modified since
61580Sstevel@tonic-gate 	 * the dump structure is allocated.
61590Sstevel@tonic-gate 	 */
61600Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
61610Sstevel@tonic-gate 	if ((dump_info == NULL) ||
61620Sstevel@tonic-gate 	    (dump_info->ip != ip) ||
61630Sstevel@tonic-gate 	    (dump_info->time.tv_sec != ip->i_mtime.tv_sec) ||
61640Sstevel@tonic-gate 	    (dump_info->time.tv_usec != ip->i_mtime.tv_usec)) {
61650Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
61660Sstevel@tonic-gate 		return (-1);
61670Sstevel@tonic-gate 	}
61680Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
61690Sstevel@tonic-gate 
61700Sstevel@tonic-gate 	/*
61710Sstevel@tonic-gate 	 * See that the file has room for this write
61720Sstevel@tonic-gate 	 */
61730Sstevel@tonic-gate 	UFS_GET_ISIZE(&file_size, ip);
61740Sstevel@tonic-gate 
61756264Srm15945 	if (ldbtob(ldbn + dblks) > file_size)
61760Sstevel@tonic-gate 		return (ENOSPC);
61770Sstevel@tonic-gate 
61780Sstevel@tonic-gate 	/*
61790Sstevel@tonic-gate 	 * Find the physical disk block numbers from the dump
61800Sstevel@tonic-gate 	 * private data structure directly and write out the data
61810Sstevel@tonic-gate 	 * in contiguous block lumps
61820Sstevel@tonic-gate 	 */
61830Sstevel@tonic-gate 	while (dblks > 0 && !error) {
61846264Srm15945 		lfsbn = (daddr_t)lblkno(fs, ldbtob(ldbn));
61850Sstevel@tonic-gate 		dbn = fsbtodb(fs, dump_info->dblk[lfsbn]) + ldbn % disk_blks;
61860Sstevel@tonic-gate 		nfsbs = 1;
61870Sstevel@tonic-gate 		ndbs = disk_blks - ldbn % disk_blks;
61880Sstevel@tonic-gate 		while (ndbs < dblks && fsbtodb(fs, dump_info->dblk[lfsbn +
61890Sstevel@tonic-gate 		    nfsbs]) == dbn + ndbs) {
61900Sstevel@tonic-gate 			nfsbs++;
61910Sstevel@tonic-gate 			ndbs += disk_blks;
61920Sstevel@tonic-gate 		}
61930Sstevel@tonic-gate 		if (ndbs > dblks)
61940Sstevel@tonic-gate 			ndbs = dblks;
61950Sstevel@tonic-gate 		error = bdev_dump(ip->i_dev, addr, dbn, ndbs);
61960Sstevel@tonic-gate 		addr += ldbtob((offset_t)ndbs);
61970Sstevel@tonic-gate 		dblks -= ndbs;
61980Sstevel@tonic-gate 		ldbn += ndbs;
61990Sstevel@tonic-gate 	}
62000Sstevel@tonic-gate 	return (error);
62010Sstevel@tonic-gate 
62020Sstevel@tonic-gate }
62030Sstevel@tonic-gate 
62040Sstevel@tonic-gate /*
62050Sstevel@tonic-gate  * Prepare the file system before and after the dump operation.
62060Sstevel@tonic-gate  *
62070Sstevel@tonic-gate  * action = DUMP_ALLOC:
62080Sstevel@tonic-gate  * Preparation before dump, allocate dump private data structure
62090Sstevel@tonic-gate  * to hold all the direct and indirect block info for dump.
62100Sstevel@tonic-gate  *
62110Sstevel@tonic-gate  * action = DUMP_FREE:
62120Sstevel@tonic-gate  * Clean up after dump, deallocate the dump private data structure.
62130Sstevel@tonic-gate  *
62140Sstevel@tonic-gate  * action = DUMP_SCAN:
62150Sstevel@tonic-gate  * Scan dump_info for *blkp DEV_BSIZE blocks of contig fs space;
62160Sstevel@tonic-gate  * if found, the starting file-relative DEV_BSIZE lbn is written
62170Sstevel@tonic-gate  * to *bklp; that lbn is intended for use with VOP_DUMP()
62180Sstevel@tonic-gate  */
62195331Samw /*ARGSUSED*/
62200Sstevel@tonic-gate static int
ufs_dumpctl(vnode_t * vp,int action,offset_t * blkp,caller_context_t * ct)62216264Srm15945 ufs_dumpctl(vnode_t *vp, int action, offset_t *blkp, caller_context_t *ct)
62220Sstevel@tonic-gate {
62230Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
62240Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
62250Sstevel@tonic-gate 	struct fs	*fs;
62260Sstevel@tonic-gate 	daddr32_t	*dblk, *storeblk;
62270Sstevel@tonic-gate 	daddr32_t	*nextblk, *endblk;
62280Sstevel@tonic-gate 	struct buf	*bp;
62290Sstevel@tonic-gate 	int		i, entry, entries;
62300Sstevel@tonic-gate 	int		n, ncontig;
62310Sstevel@tonic-gate 
62320Sstevel@tonic-gate 	/*
62330Sstevel@tonic-gate 	 * check for forced unmount
62340Sstevel@tonic-gate 	 */
62350Sstevel@tonic-gate 	if (ufsvfsp == NULL)
62360Sstevel@tonic-gate 		return (EIO);
62370Sstevel@tonic-gate 
62380Sstevel@tonic-gate 	if (action == DUMP_ALLOC) {
62390Sstevel@tonic-gate 		/*
62400Sstevel@tonic-gate 		 * alloc and record dump_info
62410Sstevel@tonic-gate 		 */
62420Sstevel@tonic-gate 		if (dump_info != NULL)
62430Sstevel@tonic-gate 			return (EINVAL);
62440Sstevel@tonic-gate 
62450Sstevel@tonic-gate 		ASSERT(vp->v_type == VREG);
62460Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
62470Sstevel@tonic-gate 
62480Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
62490Sstevel@tonic-gate 
62500Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
62510Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
62520Sstevel@tonic-gate 			return (EFAULT);
62530Sstevel@tonic-gate 		}
62540Sstevel@tonic-gate 
62550Sstevel@tonic-gate 		/*
62560Sstevel@tonic-gate 		 * calculate and allocate space needed according to i_size
62570Sstevel@tonic-gate 		 */
62580Sstevel@tonic-gate 		entries = (int)lblkno(fs, blkroundup(fs, ip->i_size));
62594662Sfrankho 		dump_info = kmem_alloc(sizeof (struct dump) +
62604662Sfrankho 		    (entries - 1) * sizeof (daddr32_t), KM_NOSLEEP);
62614662Sfrankho 		if (dump_info == NULL) {
62624662Sfrankho 			rw_exit(&ip->i_contents);
62634662Sfrankho 			return (ENOMEM);
62640Sstevel@tonic-gate 		}
62650Sstevel@tonic-gate 
62660Sstevel@tonic-gate 		/* Start saving the info */
62670Sstevel@tonic-gate 		dump_info->fsbs = entries;
62680Sstevel@tonic-gate 		dump_info->ip = ip;
62690Sstevel@tonic-gate 		storeblk = &dump_info->dblk[0];
62700Sstevel@tonic-gate 
62710Sstevel@tonic-gate 		/* Direct Blocks */
62720Sstevel@tonic-gate 		for (entry = 0; entry < NDADDR && entry < entries; entry++)
62730Sstevel@tonic-gate 			*storeblk++ = ip->i_db[entry];
62740Sstevel@tonic-gate 
62750Sstevel@tonic-gate 		/* Indirect Blocks */
62760Sstevel@tonic-gate 		for (i = 0; i < NIADDR; i++) {
62770Sstevel@tonic-gate 			int error = 0;
62780Sstevel@tonic-gate 
62790Sstevel@tonic-gate 			bp = UFS_BREAD(ufsvfsp,
62804662Sfrankho 			    ip->i_dev, fsbtodb(fs, ip->i_ib[i]), fs->fs_bsize);
62810Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR)
62820Sstevel@tonic-gate 				error = EIO;
62830Sstevel@tonic-gate 			else {
62840Sstevel@tonic-gate 				dblk = bp->b_un.b_daddr;
62850Sstevel@tonic-gate 				if ((storeblk = save_dblks(ip, ufsvfsp,
62860Sstevel@tonic-gate 				    storeblk, dblk, i, entries)) == NULL)
62870Sstevel@tonic-gate 					error = EIO;
62880Sstevel@tonic-gate 			}
62890Sstevel@tonic-gate 
62900Sstevel@tonic-gate 			brelse(bp);
62910Sstevel@tonic-gate 
62920Sstevel@tonic-gate 			if (error != 0) {
62930Sstevel@tonic-gate 				kmem_free(dump_info, sizeof (struct dump) +
62940Sstevel@tonic-gate 				    (entries - 1) * sizeof (daddr32_t));
62950Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
62960Sstevel@tonic-gate 				dump_info = NULL;
62970Sstevel@tonic-gate 				return (error);
62980Sstevel@tonic-gate 			}
62990Sstevel@tonic-gate 		}
63000Sstevel@tonic-gate 		/* and time stamp the information */
63010Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
63020Sstevel@tonic-gate 		dump_info->time = ip->i_mtime;
63030Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
63040Sstevel@tonic-gate 
63050Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
63060Sstevel@tonic-gate 	} else if (action == DUMP_FREE) {
63070Sstevel@tonic-gate 		/*
63080Sstevel@tonic-gate 		 * free dump_info
63090Sstevel@tonic-gate 		 */
63100Sstevel@tonic-gate 		if (dump_info == NULL)
63110Sstevel@tonic-gate 			return (EINVAL);
63120Sstevel@tonic-gate 		entries = dump_info->fsbs - 1;
63130Sstevel@tonic-gate 		kmem_free(dump_info, sizeof (struct dump) +
63140Sstevel@tonic-gate 		    entries * sizeof (daddr32_t));
63150Sstevel@tonic-gate 		dump_info = NULL;
63160Sstevel@tonic-gate 	} else if (action == DUMP_SCAN) {
63170Sstevel@tonic-gate 		/*
63180Sstevel@tonic-gate 		 * scan dump_info
63190Sstevel@tonic-gate 		 */
63200Sstevel@tonic-gate 		if (dump_info == NULL)
63210Sstevel@tonic-gate 			return (EINVAL);
63220Sstevel@tonic-gate 
63230Sstevel@tonic-gate 		dblk = dump_info->dblk;
63240Sstevel@tonic-gate 		nextblk = dblk + 1;
63250Sstevel@tonic-gate 		endblk = dblk + dump_info->fsbs - 1;
63260Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
63270Sstevel@tonic-gate 		ncontig = *blkp >> (fs->fs_bshift - DEV_BSHIFT);
63280Sstevel@tonic-gate 
63290Sstevel@tonic-gate 		/*
63300Sstevel@tonic-gate 		 * scan dblk[] entries; contig fs space is found when:
63310Sstevel@tonic-gate 		 * ((current blkno + frags per block) == next blkno)
63320Sstevel@tonic-gate 		 */
63330Sstevel@tonic-gate 		n = 0;
63340Sstevel@tonic-gate 		while (n < ncontig && dblk < endblk) {
63350Sstevel@tonic-gate 			if ((*dblk + fs->fs_frag) == *nextblk)
63360Sstevel@tonic-gate 				n++;
63370Sstevel@tonic-gate 			else
63380Sstevel@tonic-gate 				n = 0;
63390Sstevel@tonic-gate 			dblk++;
63400Sstevel@tonic-gate 			nextblk++;
63410Sstevel@tonic-gate 		}
63420Sstevel@tonic-gate 
63430Sstevel@tonic-gate 		/*
63440Sstevel@tonic-gate 		 * index is where size bytes of contig space begins;
63450Sstevel@tonic-gate 		 * conversion from index to the file's DEV_BSIZE lbn
63460Sstevel@tonic-gate 		 * is equivalent to:  (index * fs_bsize) / DEV_BSIZE
63470Sstevel@tonic-gate 		 */
63480Sstevel@tonic-gate 		if (n == ncontig) {
63490Sstevel@tonic-gate 			i = (dblk - dump_info->dblk) - ncontig;
63500Sstevel@tonic-gate 			*blkp = i << (fs->fs_bshift - DEV_BSHIFT);
63510Sstevel@tonic-gate 		} else
63520Sstevel@tonic-gate 			return (EFAULT);
63530Sstevel@tonic-gate 	}
63540Sstevel@tonic-gate 	return (0);
63550Sstevel@tonic-gate }
63560Sstevel@tonic-gate 
63570Sstevel@tonic-gate /*
63580Sstevel@tonic-gate  * Recursive helper function for ufs_dumpctl().  It follows the indirect file
63590Sstevel@tonic-gate  * system  blocks until it reaches the the disk block addresses, which are
63600Sstevel@tonic-gate  * then stored into the given buffer, storeblk.
63610Sstevel@tonic-gate  */
63620Sstevel@tonic-gate static daddr32_t *
save_dblks(struct inode * ip,struct ufsvfs * ufsvfsp,daddr32_t * storeblk,daddr32_t * dblk,int level,int entries)63630Sstevel@tonic-gate save_dblks(struct inode *ip, struct ufsvfs *ufsvfsp,  daddr32_t *storeblk,
63640Sstevel@tonic-gate     daddr32_t *dblk, int level, int entries)
63650Sstevel@tonic-gate {
63660Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
63670Sstevel@tonic-gate 	struct buf	*bp;
63680Sstevel@tonic-gate 	int		i;
63690Sstevel@tonic-gate 
63700Sstevel@tonic-gate 	if (level == 0) {
63710Sstevel@tonic-gate 		for (i = 0; i < NINDIR(fs); i++) {
63720Sstevel@tonic-gate 			if (storeblk - dump_info->dblk >= entries)
63730Sstevel@tonic-gate 				break;
63740Sstevel@tonic-gate 			*storeblk++ = dblk[i];
63750Sstevel@tonic-gate 		}
63760Sstevel@tonic-gate 		return (storeblk);
63770Sstevel@tonic-gate 	}
63780Sstevel@tonic-gate 	for (i = 0; i < NINDIR(fs); i++) {
63790Sstevel@tonic-gate 		if (storeblk - dump_info->dblk >= entries)
63800Sstevel@tonic-gate 			break;
63810Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp,
63824662Sfrankho 		    ip->i_dev, fsbtodb(fs, dblk[i]), fs->fs_bsize);
63830Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
63840Sstevel@tonic-gate 			brelse(bp);
63850Sstevel@tonic-gate 			return (NULL);
63860Sstevel@tonic-gate 		}
63870Sstevel@tonic-gate 		storeblk = save_dblks(ip, ufsvfsp, storeblk, bp->b_un.b_daddr,
63880Sstevel@tonic-gate 		    level - 1, entries);
63890Sstevel@tonic-gate 		brelse(bp);
63900Sstevel@tonic-gate 
63910Sstevel@tonic-gate 		if (storeblk == NULL)
63920Sstevel@tonic-gate 			return (NULL);
63930Sstevel@tonic-gate 	}
63940Sstevel@tonic-gate 	return (storeblk);
63950Sstevel@tonic-gate }
63960Sstevel@tonic-gate 
63970Sstevel@tonic-gate /* ARGSUSED */
63980Sstevel@tonic-gate static int
ufs_getsecattr(struct vnode * vp,vsecattr_t * vsap,int flag,struct cred * cr,caller_context_t * ct)63990Sstevel@tonic-gate ufs_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag,
64005331Samw 	struct cred *cr, caller_context_t *ct)
64010Sstevel@tonic-gate {
64020Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
64030Sstevel@tonic-gate 	struct ulockfs	*ulp;
64040Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
64050Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
64060Sstevel@tonic-gate 	int		err = EINVAL;
64070Sstevel@tonic-gate 
64080Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
64090Sstevel@tonic-gate 
64100Sstevel@tonic-gate 	/*
64110Sstevel@tonic-gate 	 * Only grab locks if needed - they're not needed to check vsa_mask
64120Sstevel@tonic-gate 	 * or if the mask contains no acl flags.
64130Sstevel@tonic-gate 	 */
64140Sstevel@tonic-gate 	if (vsa_mask != 0) {
64150Sstevel@tonic-gate 		if (err = ufs_lockfs_begin(ufsvfsp, &ulp,
64160Sstevel@tonic-gate 		    ULOCKFS_GETATTR_MASK))
64170Sstevel@tonic-gate 			return (err);
64180Sstevel@tonic-gate 
64190Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
64200Sstevel@tonic-gate 		err = ufs_acl_get(ip, vsap, flag, cr);
64210Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
64220Sstevel@tonic-gate 
64230Sstevel@tonic-gate 		if (ulp)
64240Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
64250Sstevel@tonic-gate 	}
64260Sstevel@tonic-gate 	return (err);
64270Sstevel@tonic-gate }
64280Sstevel@tonic-gate 
64290Sstevel@tonic-gate /* ARGSUSED */
64300Sstevel@tonic-gate static int
ufs_setsecattr(struct vnode * vp,vsecattr_t * vsap,int flag,struct cred * cr,caller_context_t * ct)64315331Samw ufs_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr,
64325331Samw 	caller_context_t *ct)
64330Sstevel@tonic-gate {
64340Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
64350Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
64360Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
64370Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
64380Sstevel@tonic-gate 	int		err;
64390Sstevel@tonic-gate 	int		haverwlock = 1;
64400Sstevel@tonic-gate 	int		trans_size;
64410Sstevel@tonic-gate 	int		donetrans = 0;
64420Sstevel@tonic-gate 	int		retry = 1;
64430Sstevel@tonic-gate 
64440Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
64450Sstevel@tonic-gate 
64460Sstevel@tonic-gate 	/* Abort now if the request is either empty or invalid. */
64470Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
64480Sstevel@tonic-gate 	if ((vsa_mask == 0) ||
64490Sstevel@tonic-gate 	    ((vsap->vsa_aclentp == NULL) &&
64500Sstevel@tonic-gate 	    (vsap->vsa_dfaclentp == NULL))) {
64510Sstevel@tonic-gate 		err = EINVAL;
64520Sstevel@tonic-gate 		goto out;
64530Sstevel@tonic-gate 	}
64540Sstevel@tonic-gate 
64550Sstevel@tonic-gate 	/*
64560Sstevel@tonic-gate 	 * Following convention, if this is a directory then we acquire the
64570Sstevel@tonic-gate 	 * inode's i_rwlock after starting a UFS logging transaction;
64580Sstevel@tonic-gate 	 * otherwise, we acquire it beforehand. Since we were called (and
64590Sstevel@tonic-gate 	 * must therefore return) with the lock held, we will have to drop it,
64600Sstevel@tonic-gate 	 * and later reacquire it, if operating on a directory.
64610Sstevel@tonic-gate 	 */
64620Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
64630Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
64640Sstevel@tonic-gate 		haverwlock = 0;
64650Sstevel@tonic-gate 	} else {
64660Sstevel@tonic-gate 		/* Upgrade the lock if required. */
64670Sstevel@tonic-gate 		if (!rw_write_held(&ip->i_rwlock)) {
64680Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
64690Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_WRITER);
64700Sstevel@tonic-gate 		}
64710Sstevel@tonic-gate 	}
64720Sstevel@tonic-gate 
64730Sstevel@tonic-gate again:
64740Sstevel@tonic-gate 	ASSERT(!(vp->v_type == VDIR && haverwlock));
64750Sstevel@tonic-gate 	if (err = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK)) {
64760Sstevel@tonic-gate 		ulp = NULL;
64770Sstevel@tonic-gate 		retry = 0;
64780Sstevel@tonic-gate 		goto out;
64790Sstevel@tonic-gate 	}
64800Sstevel@tonic-gate 
64810Sstevel@tonic-gate 	/*
64820Sstevel@tonic-gate 	 * Check that the file system supports this operation. Note that
64830Sstevel@tonic-gate 	 * ufs_lockfs_begin() will have checked that the file system had
64840Sstevel@tonic-gate 	 * not been forcibly unmounted.
64850Sstevel@tonic-gate 	 */
64860Sstevel@tonic-gate 	if (ufsvfsp->vfs_fs->fs_ronly) {
64870Sstevel@tonic-gate 		err = EROFS;
64880Sstevel@tonic-gate 		goto out;
64890Sstevel@tonic-gate 	}
64900Sstevel@tonic-gate 	if (ufsvfsp->vfs_nosetsec) {
64910Sstevel@tonic-gate 		err = ENOSYS;
64920Sstevel@tonic-gate 		goto out;
64930Sstevel@tonic-gate 	}
64940Sstevel@tonic-gate 
64950Sstevel@tonic-gate 	if (ulp) {
64960Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SETSECATTR,
64974662Sfrankho 		    trans_size = TOP_SETSECATTR_SIZE(VTOI(vp)));
64980Sstevel@tonic-gate 		donetrans = 1;
64990Sstevel@tonic-gate 	}
65000Sstevel@tonic-gate 
65010Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
65020Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
65030Sstevel@tonic-gate 		haverwlock = 1;
65040Sstevel@tonic-gate 	}
65050Sstevel@tonic-gate 
65060Sstevel@tonic-gate 	ASSERT(haverwlock);
65070Sstevel@tonic-gate 
65080Sstevel@tonic-gate 	/* Do the actual work. */
65090Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
65100Sstevel@tonic-gate 	/*
65110Sstevel@tonic-gate 	 * Suppress out of inodes messages if we will retry.
65120Sstevel@tonic-gate 	 */
65130Sstevel@tonic-gate 	if (retry)
65140Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
65150Sstevel@tonic-gate 	err = ufs_acl_set(ip, vsap, flag, cr);
65160Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
65170Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
65180Sstevel@tonic-gate 
65190Sstevel@tonic-gate out:
65200Sstevel@tonic-gate 	if (ulp) {
65210Sstevel@tonic-gate 		if (donetrans) {
65220Sstevel@tonic-gate 			/*
65230Sstevel@tonic-gate 			 * top_end_async() can eventually call
65240Sstevel@tonic-gate 			 * top_end_sync(), which can block. We must
65250Sstevel@tonic-gate 			 * therefore observe the lock-ordering protocol
65260Sstevel@tonic-gate 			 * here as well.
65270Sstevel@tonic-gate 			 */
65280Sstevel@tonic-gate 			if (vp->v_type == VDIR) {
65290Sstevel@tonic-gate 				rw_exit(&ip->i_rwlock);
65300Sstevel@tonic-gate 				haverwlock = 0;
65310Sstevel@tonic-gate 			}
65320Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_SETSECATTR, trans_size);
65330Sstevel@tonic-gate 		}
65340Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
65350Sstevel@tonic-gate 	}
65360Sstevel@tonic-gate 	/*
65370Sstevel@tonic-gate 	 * If no inodes available, try scaring a logically-
65380Sstevel@tonic-gate 	 * free one out of the delete queue to someplace
65390Sstevel@tonic-gate 	 * that we can find it.
65400Sstevel@tonic-gate 	 */
65410Sstevel@tonic-gate 	if ((err == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
65420Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
65430Sstevel@tonic-gate 		retry = 0;
65440Sstevel@tonic-gate 		if (vp->v_type == VDIR && haverwlock) {
65450Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
65460Sstevel@tonic-gate 			haverwlock = 0;
65470Sstevel@tonic-gate 		}
65480Sstevel@tonic-gate 		goto again;
65490Sstevel@tonic-gate 	}
65500Sstevel@tonic-gate 	/*
65510Sstevel@tonic-gate 	 * If we need to reacquire the lock then it is safe to do so
65520Sstevel@tonic-gate 	 * as a reader. This is because ufs_rwunlock(), which will be
65530Sstevel@tonic-gate 	 * called by our caller after we return, does not differentiate
65540Sstevel@tonic-gate 	 * between shared and exclusive locks.
65550Sstevel@tonic-gate 	 */
65560Sstevel@tonic-gate 	if (!haverwlock) {
65570Sstevel@tonic-gate 		ASSERT(vp->v_type == VDIR);
65580Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
65590Sstevel@tonic-gate 	}
65600Sstevel@tonic-gate 
65610Sstevel@tonic-gate 	return (err);
65620Sstevel@tonic-gate }
6563*12170SOwen.Roberts@Sun.Com 
6564*12170SOwen.Roberts@Sun.Com /*
6565*12170SOwen.Roberts@Sun.Com  * Locate the vnode to be used for an event notification. As this will
6566*12170SOwen.Roberts@Sun.Com  * be called prior to the name space change perform basic verification
6567*12170SOwen.Roberts@Sun.Com  * that the change will be allowed.
6568*12170SOwen.Roberts@Sun.Com  */
6569*12170SOwen.Roberts@Sun.Com 
6570*12170SOwen.Roberts@Sun.Com static int
ufs_eventlookup(struct vnode * dvp,char * nm,struct cred * cr,struct vnode ** vpp)6571*12170SOwen.Roberts@Sun.Com ufs_eventlookup(struct vnode *dvp, char *nm, struct cred *cr,
6572*12170SOwen.Roberts@Sun.Com     struct vnode **vpp)
6573*12170SOwen.Roberts@Sun.Com {
6574*12170SOwen.Roberts@Sun.Com 	int	namlen;
6575*12170SOwen.Roberts@Sun.Com 	int	error;
6576*12170SOwen.Roberts@Sun.Com 	struct vnode	*vp;
6577*12170SOwen.Roberts@Sun.Com 	struct inode	*ip;
6578*12170SOwen.Roberts@Sun.Com 	struct inode	*xip;
6579*12170SOwen.Roberts@Sun.Com 	struct ufsvfs	*ufsvfsp;
6580*12170SOwen.Roberts@Sun.Com 	struct ulockfs	*ulp;
6581*12170SOwen.Roberts@Sun.Com 
6582*12170SOwen.Roberts@Sun.Com 	ip = VTOI(dvp);
6583*12170SOwen.Roberts@Sun.Com 	*vpp = NULL;
6584*12170SOwen.Roberts@Sun.Com 
6585*12170SOwen.Roberts@Sun.Com 	if ((namlen = strlen(nm)) == 0)
6586*12170SOwen.Roberts@Sun.Com 		return (EINVAL);
6587*12170SOwen.Roberts@Sun.Com 
6588*12170SOwen.Roberts@Sun.Com 	if (nm[0] == '.') {
6589*12170SOwen.Roberts@Sun.Com 		if (namlen == 1)
6590*12170SOwen.Roberts@Sun.Com 			return (EINVAL);
6591*12170SOwen.Roberts@Sun.Com 		else if ((namlen == 2) && nm[1] == '.') {
6592*12170SOwen.Roberts@Sun.Com 			return (EEXIST);
6593*12170SOwen.Roberts@Sun.Com 		}
6594*12170SOwen.Roberts@Sun.Com 	}
6595*12170SOwen.Roberts@Sun.Com 
6596*12170SOwen.Roberts@Sun.Com 	/*
6597*12170SOwen.Roberts@Sun.Com 	 * Check accessibility and write access of parent directory as we
6598*12170SOwen.Roberts@Sun.Com 	 * only want to post the event if we're able to make a change.
6599*12170SOwen.Roberts@Sun.Com 	 */
6600*12170SOwen.Roberts@Sun.Com 	if (error = ufs_diraccess(ip, IEXEC|IWRITE, cr))
6601*12170SOwen.Roberts@Sun.Com 		return (error);
6602*12170SOwen.Roberts@Sun.Com 
6603*12170SOwen.Roberts@Sun.Com 	if (vp = dnlc_lookup(dvp, nm)) {
6604*12170SOwen.Roberts@Sun.Com 		if (vp == DNLC_NO_VNODE) {
6605*12170SOwen.Roberts@Sun.Com 			VN_RELE(vp);
6606*12170SOwen.Roberts@Sun.Com 			return (ENOENT);
6607*12170SOwen.Roberts@Sun.Com 		}
6608*12170SOwen.Roberts@Sun.Com 
6609*12170SOwen.Roberts@Sun.Com 		*vpp = vp;
6610*12170SOwen.Roberts@Sun.Com 		return (0);
6611*12170SOwen.Roberts@Sun.Com 	}
6612*12170SOwen.Roberts@Sun.Com 
6613*12170SOwen.Roberts@Sun.Com 	/*
6614*12170SOwen.Roberts@Sun.Com 	 * Keep the idle queue from getting too long by idling two
6615*12170SOwen.Roberts@Sun.Com 	 * inodes before attempting to allocate another.
6616*12170SOwen.Roberts@Sun.Com 	 * This operation must be performed before entering lockfs
6617*12170SOwen.Roberts@Sun.Com 	 * or a transaction.
6618*12170SOwen.Roberts@Sun.Com 	 */
6619*12170SOwen.Roberts@Sun.Com 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
6620*12170SOwen.Roberts@Sun.Com 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
6621*12170SOwen.Roberts@Sun.Com 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
6622*12170SOwen.Roberts@Sun.Com 			ufs_idle_some(ufs_lookup_idle_count);
6623*12170SOwen.Roberts@Sun.Com 		}
6624*12170SOwen.Roberts@Sun.Com 
6625*12170SOwen.Roberts@Sun.Com 	ufsvfsp = ip->i_ufsvfs;
6626*12170SOwen.Roberts@Sun.Com 
6627*12170SOwen.Roberts@Sun.Com retry_lookup:
6628*12170SOwen.Roberts@Sun.Com 	if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK))
6629*12170SOwen.Roberts@Sun.Com 		return (error);
6630*12170SOwen.Roberts@Sun.Com 
6631*12170SOwen.Roberts@Sun.Com 	if ((error = ufs_dirlook(ip, nm, &xip, cr, 1, 1)) == 0) {
6632*12170SOwen.Roberts@Sun.Com 		vp = ITOV(xip);
6633*12170SOwen.Roberts@Sun.Com 		*vpp = vp;
6634*12170SOwen.Roberts@Sun.Com 	}
6635*12170SOwen.Roberts@Sun.Com 
6636*12170SOwen.Roberts@Sun.Com 	if (ulp) {
6637*12170SOwen.Roberts@Sun.Com 		ufs_lockfs_end(ulp);
6638*12170SOwen.Roberts@Sun.Com 	}
6639*12170SOwen.Roberts@Sun.Com 
6640*12170SOwen.Roberts@Sun.Com 	if (error == EAGAIN)
6641*12170SOwen.Roberts@Sun.Com 		goto retry_lookup;
6642*12170SOwen.Roberts@Sun.Com 
6643*12170SOwen.Roberts@Sun.Com 	return (error);
6644*12170SOwen.Roberts@Sun.Com }
6645