xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_vnops.c (revision bc69f433dafeba50eb91394b62dfd2d145407bc3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
397c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
407c478bd9Sstevel@tonic-gate #include <sys/param.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <sys/systm.h>
437c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
447c478bd9Sstevel@tonic-gate #include <sys/resource.h>
457c478bd9Sstevel@tonic-gate #include <sys/signal.h>
467c478bd9Sstevel@tonic-gate #include <sys/cred.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/buf.h>
497c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
507c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
517c478bd9Sstevel@tonic-gate #include <sys/proc.h>
527c478bd9Sstevel@tonic-gate #include <sys/disp.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
557c478bd9Sstevel@tonic-gate #include <sys/flock.h>
56*bc69f433Saguzovsk #include <sys/atomic.h>
577c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
587c478bd9Sstevel@tonic-gate #include <sys/uio.h>
597c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
607c478bd9Sstevel@tonic-gate #include <sys/conf.h>
617c478bd9Sstevel@tonic-gate #include <sys/mman.h>
627c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
637c478bd9Sstevel@tonic-gate #include <sys/debug.h>
647c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
657c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
667c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
677c478bd9Sstevel@tonic-gate #include <sys/filio.h>
687c478bd9Sstevel@tonic-gate #include <sys/policy.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
717c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
727c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
737c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
747c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
757c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
767c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
777c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_snap.h>
787c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
797c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
807c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
817c478bd9Sstevel@tonic-gate #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
827c478bd9Sstevel@tonic-gate #include <sys/errno.h>
837c478bd9Sstevel@tonic-gate #include <sys/fssnap_if.h>
847c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
857c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #include <sys/filio.h>		/* _FIOIO */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #include <vm/hat.h>
907c478bd9Sstevel@tonic-gate #include <vm/page.h>
917c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
927c478bd9Sstevel@tonic-gate #include <vm/as.h>
937c478bd9Sstevel@tonic-gate #include <vm/seg.h>
947c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
957c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
967c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
977c478bd9Sstevel@tonic-gate #include <vm/rm.h>
987c478bd9Sstevel@tonic-gate #include <sys/swap.h>
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static struct instats ins;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static 	int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
1057c478bd9Sstevel@tonic-gate static	int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
1067c478bd9Sstevel@tonic-gate 		caddr_t, struct page **, size_t, enum seg_rw, int);
1077c478bd9Sstevel@tonic-gate static	int ufs_open(struct vnode **, int, struct cred *);
1087c478bd9Sstevel@tonic-gate static	int ufs_close(struct vnode *, int, int, offset_t, struct cred *);
1097c478bd9Sstevel@tonic-gate static	int ufs_read(struct vnode *, struct uio *, int, struct cred *,
1107c478bd9Sstevel@tonic-gate 			struct caller_context *);
1117c478bd9Sstevel@tonic-gate static	int ufs_write(struct vnode *, struct uio *, int, struct cred *,
1127c478bd9Sstevel@tonic-gate 			struct caller_context *);
1137c478bd9Sstevel@tonic-gate static	int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *, int *);
1147c478bd9Sstevel@tonic-gate static	int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *);
1157c478bd9Sstevel@tonic-gate static	int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
1167c478bd9Sstevel@tonic-gate 			caller_context_t *);
1177c478bd9Sstevel@tonic-gate static	int ufs_access(struct vnode *, int, int, struct cred *);
1187c478bd9Sstevel@tonic-gate static	int ufs_lookup(struct vnode *, char *, struct vnode **,
1197c478bd9Sstevel@tonic-gate 		struct pathname *, int, struct vnode *, struct cred *);
1207c478bd9Sstevel@tonic-gate static	int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
1217c478bd9Sstevel@tonic-gate 			int, struct vnode **, struct cred *, int);
1227c478bd9Sstevel@tonic-gate static	int ufs_remove(struct vnode *, char *, struct cred *);
1237c478bd9Sstevel@tonic-gate static	int ufs_link(struct vnode *, struct vnode *, char *, struct cred *);
1247c478bd9Sstevel@tonic-gate static	int ufs_rename(struct vnode *, char *, struct vnode *, char *,
1257c478bd9Sstevel@tonic-gate 			struct cred *);
1267c478bd9Sstevel@tonic-gate static	int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
1277c478bd9Sstevel@tonic-gate 			struct cred *);
1287c478bd9Sstevel@tonic-gate static	int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *);
1297c478bd9Sstevel@tonic-gate static	int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *);
1307c478bd9Sstevel@tonic-gate static	int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
1317c478bd9Sstevel@tonic-gate 			struct cred *);
1327c478bd9Sstevel@tonic-gate static	int ufs_readlink(struct vnode *, struct uio *, struct cred *);
1337c478bd9Sstevel@tonic-gate static	int ufs_fsync(struct vnode *, int, struct cred *);
1347c478bd9Sstevel@tonic-gate static	void ufs_inactive(struct vnode *, struct cred *);
1357c478bd9Sstevel@tonic-gate static	int ufs_fid(struct vnode *, struct fid *);
1367c478bd9Sstevel@tonic-gate static	int ufs_rwlock(struct vnode *, int, caller_context_t *);
1377c478bd9Sstevel@tonic-gate static	void ufs_rwunlock(struct vnode *, int, caller_context_t *);
1387c478bd9Sstevel@tonic-gate static	int ufs_seek(struct vnode *, offset_t, offset_t *);
1397c478bd9Sstevel@tonic-gate static	int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
1407c478bd9Sstevel@tonic-gate 			struct flk_callback *, struct cred *);
1417c478bd9Sstevel@tonic-gate static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
1427c478bd9Sstevel@tonic-gate 		cred_t *, caller_context_t *);
1437c478bd9Sstevel@tonic-gate static	int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
1447c478bd9Sstevel@tonic-gate 		struct page **, size_t, struct seg *, caddr_t,
1457c478bd9Sstevel@tonic-gate 		enum seg_rw, struct cred *);
1467c478bd9Sstevel@tonic-gate static	int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *);
1477c478bd9Sstevel@tonic-gate static	int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
1487c478bd9Sstevel@tonic-gate static	int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
1497c478bd9Sstevel@tonic-gate 			uchar_t, uchar_t, uint_t, struct cred *);
1507c478bd9Sstevel@tonic-gate static	int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
1517c478bd9Sstevel@tonic-gate 			uchar_t, uchar_t, uint_t, struct cred *);
1527c478bd9Sstevel@tonic-gate static	int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
1537c478bd9Sstevel@tonic-gate 			uint_t, uint_t, uint_t, struct cred *);
1547c478bd9Sstevel@tonic-gate static	int ufs_poll(vnode_t *, short, int, short *, struct pollhead **);
1557c478bd9Sstevel@tonic-gate static	int ufs_dump(vnode_t *, caddr_t, int, int);
1567c478bd9Sstevel@tonic-gate static	int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *);
1577c478bd9Sstevel@tonic-gate static	int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
1587c478bd9Sstevel@tonic-gate 			struct cred *);
1597c478bd9Sstevel@tonic-gate static	int ufs_dump(vnode_t *, caddr_t, int, int);
1607c478bd9Sstevel@tonic-gate static	int ufs_dumpctl(vnode_t *, int, int *);
1617c478bd9Sstevel@tonic-gate static	daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
1627c478bd9Sstevel@tonic-gate 			daddr32_t *, int, int);
1637c478bd9Sstevel@tonic-gate static	int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
1647c478bd9Sstevel@tonic-gate static	int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
1707c478bd9Sstevel@tonic-gate  */
1717c478bd9Sstevel@tonic-gate struct vnodeops *ufs_vnodeops;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate const fs_operation_def_t ufs_vnodeops_template[] = {
1747c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, ufs_open,	/* will not be blocked by lockfs */
1757c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, ufs_close,	/* will not be blocked by lockfs */
1767c478bd9Sstevel@tonic-gate 	VOPNAME_READ, ufs_read,
1777c478bd9Sstevel@tonic-gate 	VOPNAME_WRITE, ufs_write,
1787c478bd9Sstevel@tonic-gate 	VOPNAME_IOCTL, ufs_ioctl,
1797c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, ufs_getattr,
1807c478bd9Sstevel@tonic-gate 	VOPNAME_SETATTR, ufs_setattr,
1817c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, ufs_access,
1827c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, ufs_lookup,
1837c478bd9Sstevel@tonic-gate 	VOPNAME_CREATE, ufs_create,
1847c478bd9Sstevel@tonic-gate 	VOPNAME_REMOVE, ufs_remove,
1857c478bd9Sstevel@tonic-gate 	VOPNAME_LINK, ufs_link,
1867c478bd9Sstevel@tonic-gate 	VOPNAME_RENAME, ufs_rename,
1877c478bd9Sstevel@tonic-gate 	VOPNAME_MKDIR, ufs_mkdir,
1887c478bd9Sstevel@tonic-gate 	VOPNAME_RMDIR, ufs_rmdir,
1897c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, ufs_readdir,
1907c478bd9Sstevel@tonic-gate 	VOPNAME_SYMLINK, ufs_symlink,
1917c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, ufs_readlink,
1927c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, ufs_fsync,
1937c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, (fs_generic_func_p) ufs_inactive,  /* not blocked */
1947c478bd9Sstevel@tonic-gate 	VOPNAME_FID, ufs_fid,
1957c478bd9Sstevel@tonic-gate 	VOPNAME_RWLOCK, ufs_rwlock, /* not blocked */
1967c478bd9Sstevel@tonic-gate 	VOPNAME_RWUNLOCK, (fs_generic_func_p) ufs_rwunlock,  /* not blocked */
1977c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, ufs_seek,
1987c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, ufs_frlock,
1997c478bd9Sstevel@tonic-gate 	VOPNAME_SPACE, ufs_space,
2007c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, ufs_getpage,
2017c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, ufs_putpage,
2027c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, (fs_generic_func_p) ufs_map,
2037c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, (fs_generic_func_p) ufs_addmap,	/* not blocked */
2047c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, ufs_delmap,	/* will not be blocked by lockfs */
2057c478bd9Sstevel@tonic-gate 	VOPNAME_POLL, (fs_generic_func_p) ufs_poll,	/* not blocked */
2067c478bd9Sstevel@tonic-gate 	VOPNAME_DUMP, ufs_dump,
2077c478bd9Sstevel@tonic-gate 	VOPNAME_PATHCONF, ufs_l_pathconf,
2087c478bd9Sstevel@tonic-gate 	VOPNAME_PAGEIO, ufs_pageio,
2097c478bd9Sstevel@tonic-gate 	VOPNAME_DUMPCTL, ufs_dumpctl,
2107c478bd9Sstevel@tonic-gate 	VOPNAME_GETSECATTR, ufs_getsecattr,
2117c478bd9Sstevel@tonic-gate 	VOPNAME_SETSECATTR, ufs_setsecattr,
2127c478bd9Sstevel@tonic-gate 	VOPNAME_VNEVENT, fs_vnevent_support,
2137c478bd9Sstevel@tonic-gate 	NULL, NULL
2147c478bd9Sstevel@tonic-gate };
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate #define	MAX_BACKFILE_COUNT	9999
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Created by ufs_dumpctl() to store a file's disk block info into memory.
2207c478bd9Sstevel@tonic-gate  * Used by ufs_dump() to dump data to disk directly.
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate struct dump {
2237c478bd9Sstevel@tonic-gate 	struct inode	*ip;		/* the file we contain */
2247c478bd9Sstevel@tonic-gate 	daddr_t		fsbs;		/* number of blocks stored */
2257c478bd9Sstevel@tonic-gate 	struct timeval32 time;		/* time stamp for the struct */
2267c478bd9Sstevel@tonic-gate 	daddr32_t 	dblk[1];	/* place holder for block info */
2277c478bd9Sstevel@tonic-gate };
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static struct dump *dump_info = NULL;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Previously there was no special action required for ordinary files.
2337c478bd9Sstevel@tonic-gate  * (Devices are handled through the device file system.)
2347c478bd9Sstevel@tonic-gate  * Now we support Large Files and Large File API requires open to
2357c478bd9Sstevel@tonic-gate  * fail if file is large.
2367c478bd9Sstevel@tonic-gate  * We could take care to prevent data corruption
2377c478bd9Sstevel@tonic-gate  * by doing an atomic check of size and truncate if file is opened with
2387c478bd9Sstevel@tonic-gate  * FTRUNC flag set but traditionally this is being done by the vfs/vnode
2397c478bd9Sstevel@tonic-gate  * layers. So taking care of truncation here is a change in the existing
2407c478bd9Sstevel@tonic-gate  * semantics of VOP_OPEN and therefore we chose not to implement any thing
2417c478bd9Sstevel@tonic-gate  * here. The check for the size of the file > 2GB is being done at the
2427c478bd9Sstevel@tonic-gate  * vfs layer in routine vn_open().
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /* ARGSUSED */
2467c478bd9Sstevel@tonic-gate static int
2477c478bd9Sstevel@tonic-gate ufs_open(struct vnode **vpp, int flag, struct cred *cr)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_OPEN, "ufs_open:vpp %p", vpp);
2507c478bd9Sstevel@tonic-gate 	return (0);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2547c478bd9Sstevel@tonic-gate static int
2557c478bd9Sstevel@tonic-gate ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
2567c478bd9Sstevel@tonic-gate 	struct cred *cr)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_CLOSE, "ufs_close:vp %p", vp);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2617c478bd9Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * Push partially filled cluster at last close.
2657c478bd9Sstevel@tonic-gate 	 * ``last close'' is approximated because the dnlc
2667c478bd9Sstevel@tonic-gate 	 * may have a hold on the vnode.
2677c478bd9Sstevel@tonic-gate 	 * Checking for VBAD here will also act as a forced umount check.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
2707c478bd9Sstevel@tonic-gate 		struct inode *ip = VTOI(vp);
2717c478bd9Sstevel@tonic-gate 		if (ip->i_delaylen) {
2727c478bd9Sstevel@tonic-gate 			ins.in_poc.value.ul++;
2737c478bd9Sstevel@tonic-gate 			(void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
2747c478bd9Sstevel@tonic-gate 					B_ASYNC | B_FREE, cr);
2757c478bd9Sstevel@tonic-gate 			ip->i_delaylen = 0;
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	return (0);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
2857c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
2887c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
2897c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp = NULL;
2907c478bd9Sstevel@tonic-gate 	int error = 0;
2917c478bd9Sstevel@tonic-gate 	int intrans = 0;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
2947c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_READ_START,
2957c478bd9Sstevel@tonic-gate 		"ufs_read_start:vp %p uiop %p ioflag %x",
2967c478bd9Sstevel@tonic-gate 		vp, uiop, ioflag);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
3007c478bd9Sstevel@tonic-gate 	 * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
3017c478bd9Sstevel@tonic-gate 	 */
3027c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
3037c478bd9Sstevel@tonic-gate 		/*
3047c478bd9Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
3057c478bd9Sstevel@tonic-gate 		 */
3067c478bd9Sstevel@tonic-gate 		error = chklock(vp, FREAD, uiop->uio_loffset,
3077c478bd9Sstevel@tonic-gate 				uiop->uio_resid, uiop->uio_fmode, ct);
3087c478bd9Sstevel@tonic-gate 		if (error)
3097c478bd9Sstevel@tonic-gate 			goto out;
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
3137c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
3147c478bd9Sstevel@tonic-gate 	if (error)
3157c478bd9Sstevel@tonic-gate 		goto out;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * In the case that a directory is opened for reading as a file
3197c478bd9Sstevel@tonic-gate 	 * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
3207c478bd9Sstevel@tonic-gate 	 * The locking order had to be changed to avoid a deadlock with
3217c478bd9Sstevel@tonic-gate 	 * an update taking place on that directory at the same time.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if ((ip->i_mode & IFMT) == IFDIR) {
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3267c478bd9Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3277c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		if (error) {
3307c478bd9Sstevel@tonic-gate 			if (ulp)
3317c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
3327c478bd9Sstevel@tonic-gate 			goto out;
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3367c478bd9Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3377c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
3387c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3397c478bd9Sstevel@tonic-gate 			    error);
3407c478bd9Sstevel@tonic-gate 			ASSERT(!error);
3417c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3427c478bd9Sstevel@tonic-gate 			    TOP_READ_SIZE);
3437c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_READER);
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 	} else {
3467c478bd9Sstevel@tonic-gate 		/*
3477c478bd9Sstevel@tonic-gate 		 * Only transact reads to files opened for sync-read and
3487c478bd9Sstevel@tonic-gate 		 * sync-write on a file system that is not write locked.
3497c478bd9Sstevel@tonic-gate 		 *
3507c478bd9Sstevel@tonic-gate 		 * The ``not write locked'' check prevents problems with
3517c478bd9Sstevel@tonic-gate 		 * enabling/disabling logging on a busy file system.  E.g.,
3527c478bd9Sstevel@tonic-gate 		 * logging exists at the beginning of the read but does not
3537c478bd9Sstevel@tonic-gate 		 * at the end.
3547c478bd9Sstevel@tonic-gate 		 *
3557c478bd9Sstevel@tonic-gate 		 */
3567c478bd9Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3577c478bd9Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3587c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3597c478bd9Sstevel@tonic-gate 			    error);
3607c478bd9Sstevel@tonic-gate 			ASSERT(!error);
3617c478bd9Sstevel@tonic-gate 			intrans = 1;
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3657c478bd9Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3667c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		if (intrans) {
3697c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3707c478bd9Sstevel@tonic-gate 			    TOP_READ_SIZE);
3717c478bd9Sstevel@tonic-gate 		}
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (ulp) {
3757c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate out:
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_READ_END,
3807c478bd9Sstevel@tonic-gate 		"ufs_read_end:vp %p error %d", vp, error);
3817c478bd9Sstevel@tonic-gate 	return (error);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate extern	int	ufs_HW;		/* high water mark */
3857c478bd9Sstevel@tonic-gate extern	int	ufs_LW;		/* low water mark */
3867c478bd9Sstevel@tonic-gate int	ufs_WRITES = 1;		/* XXX - enable/disable */
3877c478bd9Sstevel@tonic-gate int	ufs_throttles = 0;	/* throttling count */
3887c478bd9Sstevel@tonic-gate int	ufs_allow_shared_writes = 1;	/* directio shared writes */
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate static int
3917c478bd9Sstevel@tonic-gate ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * Filter to determine if this request is suitable as a
3967c478bd9Sstevel@tonic-gate 	 * concurrent rewrite. This write must not allocate blocks
3977c478bd9Sstevel@tonic-gate 	 * by extending the file or filling in holes. No use trying
3987c478bd9Sstevel@tonic-gate 	 * through FSYNC descriptors as the inode will be synchronously
3997c478bd9Sstevel@tonic-gate 	 * updated after the write. The uio structure has not yet been
4007c478bd9Sstevel@tonic-gate 	 * checked for sanity, so assume nothing.
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
4037c478bd9Sstevel@tonic-gate 		(uiop->uio_loffset >= (offset_t)0) &&
4047c478bd9Sstevel@tonic-gate 		(uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
4057c478bd9Sstevel@tonic-gate 		((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
4067c478bd9Sstevel@tonic-gate 		!(ioflag & FSYNC) && !bmap_has_holes(ip) &&
4077c478bd9Sstevel@tonic-gate 		ufs_allow_shared_writes);
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4117c478bd9Sstevel@tonic-gate static int
4127c478bd9Sstevel@tonic-gate ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
4137c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
4167c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
4177c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
4187c478bd9Sstevel@tonic-gate 	int retry = 1;
4197c478bd9Sstevel@tonic-gate 	int error, resv, resid = 0;
4207c478bd9Sstevel@tonic-gate 	int directio_status;
4217c478bd9Sstevel@tonic-gate 	int exclusive;
4227c478bd9Sstevel@tonic-gate 	long start_resid = uiop->uio_resid;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_WRITE_START,
4257c478bd9Sstevel@tonic-gate 		"ufs_write_start:vp %p uiop %p ioflag %x",
4267c478bd9Sstevel@tonic-gate 		vp, uiop, ioflag);
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate retry_mandlock:
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
4337c478bd9Sstevel@tonic-gate 	 * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
4347c478bd9Sstevel@tonic-gate 	 * Check for forced unmounts normally done in ufs_lockfs_begin().
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
4377c478bd9Sstevel@tonic-gate 		error = EIO;
4387c478bd9Sstevel@tonic-gate 		goto out;
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 		/*
4457c478bd9Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
4467c478bd9Sstevel@tonic-gate 		 */
4477c478bd9Sstevel@tonic-gate 		error = chklock(vp, FWRITE, uiop->uio_loffset,
4487c478bd9Sstevel@tonic-gate 				uiop->uio_resid, uiop->uio_fmode, ct);
4497c478bd9Sstevel@tonic-gate 		if (error)
4507c478bd9Sstevel@tonic-gate 			goto out;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	/* i_rwlock can change in chklock */
4547c478bd9Sstevel@tonic-gate 	exclusive = rw_write_held(&ip->i_rwlock);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * Check for fast-path special case of directio re-writes.
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
4607c478bd9Sstevel@tonic-gate 	    !exclusive && ufs_check_rewrite(ip, uiop, ioflag)) {
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
4637c478bd9Sstevel@tonic-gate 		if (error)
4647c478bd9Sstevel@tonic-gate 			goto out;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
4677c478bd9Sstevel@tonic-gate 		error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
4687c478bd9Sstevel@tonic-gate 			&directio_status);
4697c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS) {
4707c478bd9Sstevel@tonic-gate 			uint_t i_flag_save;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 			if (start_resid != uiop->uio_resid)
4737c478bd9Sstevel@tonic-gate 				error = 0;
4747c478bd9Sstevel@tonic-gate 			/*
4757c478bd9Sstevel@tonic-gate 			 * Special treatment of access times for re-writes.
4767c478bd9Sstevel@tonic-gate 			 * If IMOD is not already set, then convert it
4777c478bd9Sstevel@tonic-gate 			 * to IMODACC for this operation. This defers
4787c478bd9Sstevel@tonic-gate 			 * entering a delta into the log until the inode
4797c478bd9Sstevel@tonic-gate 			 * is flushed. This mimics what is done for read
4807c478bd9Sstevel@tonic-gate 			 * operations and inode access time.
4817c478bd9Sstevel@tonic-gate 			 */
4827c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
4837c478bd9Sstevel@tonic-gate 			i_flag_save = ip->i_flag;
4847c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
4857c478bd9Sstevel@tonic-gate 			ip->i_seq++;
4867c478bd9Sstevel@tonic-gate 			ITIMES_NOLOCK(ip);
4877c478bd9Sstevel@tonic-gate 			if ((i_flag_save & IMOD) == 0) {
4887c478bd9Sstevel@tonic-gate 				ip->i_flag &= ~IMOD;
4897c478bd9Sstevel@tonic-gate 				ip->i_flag |= IMODACC;
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
4927c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
4937c478bd9Sstevel@tonic-gate 			if (ulp)
4947c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
4957c478bd9Sstevel@tonic-gate 			goto out;
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
4987c478bd9Sstevel@tonic-gate 		if (ulp)
4997c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
5037c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
5047c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
5057c478bd9Sstevel@tonic-gate 		/*
5067c478bd9Sstevel@tonic-gate 		 * Mandatory locking could have been enabled
5077c478bd9Sstevel@tonic-gate 		 * after dropping the i_rwlock.
5087c478bd9Sstevel@tonic-gate 		 */
5097c478bd9Sstevel@tonic-gate 		if (MANDLOCK(vp, ip->i_mode))
5107c478bd9Sstevel@tonic-gate 			goto retry_mandlock;
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
5147c478bd9Sstevel@tonic-gate 	if (error)
5157c478bd9Sstevel@tonic-gate 		goto out;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	/*
5187c478bd9Sstevel@tonic-gate 	 * Amount of log space needed for this write
5197c478bd9Sstevel@tonic-gate 	 */
5207c478bd9Sstevel@tonic-gate 	TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	/*
5237c478bd9Sstevel@tonic-gate 	 * Throttle writes.
5247c478bd9Sstevel@tonic-gate 	 */
5257c478bd9Sstevel@tonic-gate 	if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
5267c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5277c478bd9Sstevel@tonic-gate 		while (ip->i_writes > ufs_HW) {
5287c478bd9Sstevel@tonic-gate 			ufs_throttles++;
5297c478bd9Sstevel@tonic-gate 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Enter Transaction
5367c478bd9Sstevel@tonic-gate 	 */
5377c478bd9Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
5387c478bd9Sstevel@tonic-gate 		if (ulp) {
5397c478bd9Sstevel@tonic-gate 			int terr = 0;
5407c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv, terr);
5417c478bd9Sstevel@tonic-gate 			ASSERT(!terr);
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 	} else {
5447c478bd9Sstevel@tonic-gate 		if (ulp)
5457c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	/*
5497c478bd9Sstevel@tonic-gate 	 * Write the file
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
5527c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
5537c478bd9Sstevel@tonic-gate 	if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
5547c478bd9Sstevel@tonic-gate 		/*
5557c478bd9Sstevel@tonic-gate 		 * In append mode start at end of file.
5567c478bd9Sstevel@tonic-gate 		 */
5577c478bd9Sstevel@tonic-gate 		uiop->uio_loffset = ip->i_size;
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	/*
5617c478bd9Sstevel@tonic-gate 	 * Mild optimisation, don't call ufs_trans_write() unless we have to
5627c478bd9Sstevel@tonic-gate 	 * Also, suppress file system full messages if we will retry.
5637c478bd9Sstevel@tonic-gate 	 */
5647c478bd9Sstevel@tonic-gate 	if (retry)
5657c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
5667c478bd9Sstevel@tonic-gate 	if (resid) {
5677c478bd9Sstevel@tonic-gate 		TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
5687c478bd9Sstevel@tonic-gate 	} else {
5697c478bd9Sstevel@tonic-gate 		error = wrip(ip, uiop, ioflag, cr);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
5747c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	/*
5777c478bd9Sstevel@tonic-gate 	 * Leave Transaction
5787c478bd9Sstevel@tonic-gate 	 */
5797c478bd9Sstevel@tonic-gate 	if (ulp) {
5807c478bd9Sstevel@tonic-gate 		if (ioflag & (FSYNC|FDSYNC)) {
5817c478bd9Sstevel@tonic-gate 			int terr = 0;
5827c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC, resv);
5837c478bd9Sstevel@tonic-gate 			if (error == 0)
5847c478bd9Sstevel@tonic-gate 				error = terr;
5857c478bd9Sstevel@tonic-gate 		} else {
5867c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate out:
5917c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
5927c478bd9Sstevel@tonic-gate 		/*
5937c478bd9Sstevel@tonic-gate 		 * Any blocks tied up in pending deletes?
5947c478bd9Sstevel@tonic-gate 		 */
5957c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
5967c478bd9Sstevel@tonic-gate 		retry = 0;
5977c478bd9Sstevel@tonic-gate 		goto retry_mandlock;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (error == ENOSPC && (start_resid != uiop->uio_resid))
6017c478bd9Sstevel@tonic-gate 		error = 0;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_WRITE_END,
6047c478bd9Sstevel@tonic-gate 		"ufs_write_end:vp %p error %d", vp, error);
6057c478bd9Sstevel@tonic-gate 	return (error);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * Don't cache write blocks to files with the sticky bit set.
6107c478bd9Sstevel@tonic-gate  * Used to keep swap files from blowing the page cache on a server.
6117c478bd9Sstevel@tonic-gate  */
6127c478bd9Sstevel@tonic-gate int stickyhack = 1;
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * Free behind hacks.  The pager is busted.
6167c478bd9Sstevel@tonic-gate  * XXX - need to pass the information down to writedone() in a flag like B_SEQ
6177c478bd9Sstevel@tonic-gate  * or B_FREE_IF_TIGHT_ON_MEMORY.
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate int	freebehind = 1;
6207c478bd9Sstevel@tonic-gate int	smallfile = 32 * 1024;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * While we should, in most cases, cache the pages for write, we
6247c478bd9Sstevel@tonic-gate  * may also want to cache the pages for read as long as they are
6257c478bd9Sstevel@tonic-gate  * frequently re-usable.
6267c478bd9Sstevel@tonic-gate  *
6277c478bd9Sstevel@tonic-gate  * If cache_read_ahead = 1, the pages for read will go to the tail
6287c478bd9Sstevel@tonic-gate  * of the cache list when they are released, otherwise go to the head.
6297c478bd9Sstevel@tonic-gate  */
6307c478bd9Sstevel@tonic-gate int	cache_read_ahead = 0;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /*
6337c478bd9Sstevel@tonic-gate  * wrip does the real work of write requests for ufs.
6347c478bd9Sstevel@tonic-gate  */
6357c478bd9Sstevel@tonic-gate int
6367c478bd9Sstevel@tonic-gate wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	rlim64_t limit = uio->uio_llimit;
6397c478bd9Sstevel@tonic-gate 	u_offset_t off;
6407c478bd9Sstevel@tonic-gate 	u_offset_t old_i_size;
6417c478bd9Sstevel@tonic-gate 	struct fs *fs;
6427c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6437c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
6447c478bd9Sstevel@tonic-gate 	caddr_t base;
6457c478bd9Sstevel@tonic-gate 	long start_resid = uio->uio_resid;	/* save starting resid */
6467c478bd9Sstevel@tonic-gate 	long premove_resid;			/* resid before uiomove() */
6477c478bd9Sstevel@tonic-gate 	uint_t flags;
6487c478bd9Sstevel@tonic-gate 	int newpage;
6497c478bd9Sstevel@tonic-gate 	int iupdat_flag, directio_status;
6507c478bd9Sstevel@tonic-gate 	int n, on, mapon;
6517c478bd9Sstevel@tonic-gate 	int error, pagecreate;
6527c478bd9Sstevel@tonic-gate 	int do_dqrwlock;		/* drop/reacquire vfs_dqrwlock */
6537c478bd9Sstevel@tonic-gate 	int32_t	iblocks;
6547c478bd9Sstevel@tonic-gate 	int	new_iblocks;
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate 	/*
6577c478bd9Sstevel@tonic-gate 	 * ip->i_size is incremented before the uiomove
6587c478bd9Sstevel@tonic-gate 	 * is done on a write.  If the move fails (bad user
6597c478bd9Sstevel@tonic-gate 	 * address) reset ip->i_size.
6607c478bd9Sstevel@tonic-gate 	 * The better way would be to increment ip->i_size
6617c478bd9Sstevel@tonic-gate 	 * only if the uiomove succeeds.
6627c478bd9Sstevel@tonic-gate 	 */
6637c478bd9Sstevel@tonic-gate 	int i_size_changed = 0;
6647c478bd9Sstevel@tonic-gate 	o_mode_t type;
6657c478bd9Sstevel@tonic-gate 	int i_seq_needed = 0;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 	/*
6707c478bd9Sstevel@tonic-gate 	 * check for forced unmount - should not happen as
6717c478bd9Sstevel@tonic-gate 	 * the request passed the lockfs checks.
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
6747c478bd9Sstevel@tonic-gate 		return (EIO);
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_RWIP_START,
6797c478bd9Sstevel@tonic-gate 		"ufs_wrip_start:vp %p", vp);
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	/* check for valid filetype */
6847c478bd9Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
6857c478bd9Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
6867c478bd9Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
6877c478bd9Sstevel@tonic-gate 		return (EIO);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	/*
6917c478bd9Sstevel@tonic-gate 	 * the actual limit of UFS file size
6927c478bd9Sstevel@tonic-gate 	 * is UFS_MAXOFFSET_T
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
6957c478bd9Sstevel@tonic-gate 		limit = MAXOFFSET_T;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= limit) {
6987c478bd9Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
7017c478bd9Sstevel@tonic-gate 			"ufs_wrip_end:vp %p error %d", vp, EINVAL);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7047c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
7057c478bd9Sstevel@tonic-gate 		    p, RCA_UNSAFE_SIGINFO);
7067c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7077c478bd9Sstevel@tonic-gate 		return (EFBIG);
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * if largefiles are disallowed, the limit is
7127c478bd9Sstevel@tonic-gate 	 * the pre-largefiles value of 2GB
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
7157c478bd9Sstevel@tonic-gate 		limit = MIN(UFS_MAXOFFSET_T, limit);
7167c478bd9Sstevel@tonic-gate 	else
7177c478bd9Sstevel@tonic-gate 		limit = MIN(MAXOFF32_T, limit);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
7207c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
7217c478bd9Sstevel@tonic-gate 			"ufs_wrip_end:vp %p error %d", vp, EINVAL);
7227c478bd9Sstevel@tonic-gate 		return (EINVAL);
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
7257c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
7267c478bd9Sstevel@tonic-gate 			"ufs_wrip_end:vp %p error %d", vp, 0);
7277c478bd9Sstevel@tonic-gate 		return (0);
7287c478bd9Sstevel@tonic-gate 	}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= limit)
7317c478bd9Sstevel@tonic-gate 		return (EFBIG);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	ip->i_flag |= INOACC;	/* don't update ref time in getpage */
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
7367c478bd9Sstevel@tonic-gate 		ip->i_flag |= ISYNC;
7377c478bd9Sstevel@tonic-gate 		iupdat_flag = 1;
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 	/*
7407c478bd9Sstevel@tonic-gate 	 * Try to go direct
7417c478bd9Sstevel@tonic-gate 	 */
7427c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
7437c478bd9Sstevel@tonic-gate 		uio->uio_llimit = limit;
7447c478bd9Sstevel@tonic-gate 		error = ufs_directio_write(ip, uio, ioflag, 0, cr,
7457c478bd9Sstevel@tonic-gate 			&directio_status);
7467c478bd9Sstevel@tonic-gate 		/*
7477c478bd9Sstevel@tonic-gate 		 * If ufs_directio wrote to the file or set the flags,
7487c478bd9Sstevel@tonic-gate 		 * we need to update i_seq, but it may be deferred.
7497c478bd9Sstevel@tonic-gate 		 */
7507c478bd9Sstevel@tonic-gate 		if (start_resid != uio->uio_resid ||
7517c478bd9Sstevel@tonic-gate 					(ip->i_flag & (ICHG|IUPD))) {
7527c478bd9Sstevel@tonic-gate 			i_seq_needed = 1;
7537c478bd9Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
7567c478bd9Sstevel@tonic-gate 			goto out;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
7617c478bd9Sstevel@tonic-gate 	 *
7627c478bd9Sstevel@tonic-gate 	 * o shadow inodes: vfs_dqrwlock is not held at all
7637c478bd9Sstevel@tonic-gate 	 * o quota updates: vfs_dqrwlock is read or write held
7647c478bd9Sstevel@tonic-gate 	 * o other updates: vfs_dqrwlock is read held
7657c478bd9Sstevel@tonic-gate 	 *
7667c478bd9Sstevel@tonic-gate 	 * The first case is the only one where we do not hold
7677c478bd9Sstevel@tonic-gate 	 * vfs_dqrwlock at all while entering wrip().
7687c478bd9Sstevel@tonic-gate 	 * We must make sure not to downgrade/drop vfs_dqrwlock if we
7697c478bd9Sstevel@tonic-gate 	 * have it as writer, i.e. if we are updating the quota inode.
7707c478bd9Sstevel@tonic-gate 	 * There is no potential deadlock scenario in this case as
7717c478bd9Sstevel@tonic-gate 	 * ufs_getpage() takes care of this and avoids reacquiring
7727c478bd9Sstevel@tonic-gate 	 * vfs_dqrwlock in that case.
7737c478bd9Sstevel@tonic-gate 	 *
7747c478bd9Sstevel@tonic-gate 	 * This check is done here since the above conditions do not change
7757c478bd9Sstevel@tonic-gate 	 * and we possibly loop below, so save a few cycles.
7767c478bd9Sstevel@tonic-gate 	 */
7777c478bd9Sstevel@tonic-gate 	if ((type == IFSHAD) ||
7787c478bd9Sstevel@tonic-gate 		(rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
7797c478bd9Sstevel@tonic-gate 			do_dqrwlock = 0;
7807c478bd9Sstevel@tonic-gate 	} else {
7817c478bd9Sstevel@tonic-gate 		do_dqrwlock = 1;
7827c478bd9Sstevel@tonic-gate 	}
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	/*
7857c478bd9Sstevel@tonic-gate 	 * Large Files: We cast MAXBMASK to offset_t
7867c478bd9Sstevel@tonic-gate 	 * inorder to mask out the higher bits. Since offset_t
7877c478bd9Sstevel@tonic-gate 	 * is a signed value, the high order bit set in MAXBMASK
7887c478bd9Sstevel@tonic-gate 	 * value makes it do the right thing by having all bits 1
7897c478bd9Sstevel@tonic-gate 	 * in the higher word. May be removed for _SOLARIS64_.
7907c478bd9Sstevel@tonic-gate 	 */
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
7937c478bd9Sstevel@tonic-gate 	do {
7947c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
7957c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
7967c478bd9Sstevel@tonic-gate 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
7977c478bd9Sstevel@tonic-gate 		on = (int)blkoff(fs, uoff);
7987c478bd9Sstevel@tonic-gate 		n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
7997c478bd9Sstevel@tonic-gate 		new_iblocks = 1;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 		if (type == IFREG && uoff + n >= limit) {
8027c478bd9Sstevel@tonic-gate 			if (uoff >= limit) {
8037c478bd9Sstevel@tonic-gate 				error = EFBIG;
8047c478bd9Sstevel@tonic-gate 				goto out;
8057c478bd9Sstevel@tonic-gate 			}
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * since uoff + n >= limit,
8087c478bd9Sstevel@tonic-gate 			 * therefore n >= limit - uoff, and n is an int
8097c478bd9Sstevel@tonic-gate 			 * so it is safe to cast it to an int
8107c478bd9Sstevel@tonic-gate 			 */
8117c478bd9Sstevel@tonic-gate 			n = (int)(limit - (rlim64_t)uoff);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		if (uoff + n > ip->i_size) {
8147c478bd9Sstevel@tonic-gate 			/*
8157c478bd9Sstevel@tonic-gate 			 * We are extending the length of the file.
8167c478bd9Sstevel@tonic-gate 			 * bmap is used so that we are sure that
8177c478bd9Sstevel@tonic-gate 			 * if we need to allocate new blocks, that it
8187c478bd9Sstevel@tonic-gate 			 * is done here before we up the file size.
8197c478bd9Sstevel@tonic-gate 			 */
8207c478bd9Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (int)(on + n),
8217c478bd9Sstevel@tonic-gate 							mapon == 0, cr);
8227c478bd9Sstevel@tonic-gate 			/*
8237c478bd9Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
8247c478bd9Sstevel@tonic-gate 			 * the flags are set it changed the file.
8257c478bd9Sstevel@tonic-gate 			 */
8267c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
8277c478bd9Sstevel@tonic-gate 				i_seq_needed = 1;
8287c478bd9Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
8297c478bd9Sstevel@tonic-gate 			}
8307c478bd9Sstevel@tonic-gate 			if (error)
8317c478bd9Sstevel@tonic-gate 				break;
8327c478bd9Sstevel@tonic-gate 			/*
8337c478bd9Sstevel@tonic-gate 			 * There is a window of vulnerability here.
8347c478bd9Sstevel@tonic-gate 			 * The sequence of operations: allocate file
8357c478bd9Sstevel@tonic-gate 			 * system blocks, uiomove the data into pages,
8367c478bd9Sstevel@tonic-gate 			 * and then update the size of the file in the
8377c478bd9Sstevel@tonic-gate 			 * inode, must happen atomically.  However, due
8387c478bd9Sstevel@tonic-gate 			 * to current locking constraints, this can not
8397c478bd9Sstevel@tonic-gate 			 * be done.
8407c478bd9Sstevel@tonic-gate 			 */
8417c478bd9Sstevel@tonic-gate 			ASSERT(ip->i_writer == NULL);
8427c478bd9Sstevel@tonic-gate 			ip->i_writer = curthread;
8437c478bd9Sstevel@tonic-gate 			i_size_changed = 1;
8447c478bd9Sstevel@tonic-gate 			/*
8457c478bd9Sstevel@tonic-gate 			 * If we are writing from the beginning of
8467c478bd9Sstevel@tonic-gate 			 * the mapping, we can just create the
8477c478bd9Sstevel@tonic-gate 			 * pages without having to read them.
8487c478bd9Sstevel@tonic-gate 			 */
8497c478bd9Sstevel@tonic-gate 			pagecreate = (mapon == 0);
8507c478bd9Sstevel@tonic-gate 		} else if (n == MAXBSIZE) {
8517c478bd9Sstevel@tonic-gate 			/*
8527c478bd9Sstevel@tonic-gate 			 * Going to do a whole mappings worth,
8537c478bd9Sstevel@tonic-gate 			 * so we can just create the pages w/o
8547c478bd9Sstevel@tonic-gate 			 * having to read them in.  But before
8557c478bd9Sstevel@tonic-gate 			 * we do that, we need to make sure any
8567c478bd9Sstevel@tonic-gate 			 * needed blocks are allocated first.
8577c478bd9Sstevel@tonic-gate 			 */
8587c478bd9Sstevel@tonic-gate 			iblocks = ip->i_blocks;
8597c478bd9Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (int)(on + n), 1, cr);
8607c478bd9Sstevel@tonic-gate 			/*
8617c478bd9Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
8627c478bd9Sstevel@tonic-gate 			 * the flags are set it changed the file.
8637c478bd9Sstevel@tonic-gate 			 */
8647c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
8657c478bd9Sstevel@tonic-gate 				i_seq_needed = 1;
8667c478bd9Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
8677c478bd9Sstevel@tonic-gate 			}
8687c478bd9Sstevel@tonic-gate 			if (error)
8697c478bd9Sstevel@tonic-gate 				break;
8707c478bd9Sstevel@tonic-gate 			pagecreate = 1;
8717c478bd9Sstevel@tonic-gate 			/*
8727c478bd9Sstevel@tonic-gate 			 * check if the new created page needed the
8737c478bd9Sstevel@tonic-gate 			 * allocation of new disk blocks.
8747c478bd9Sstevel@tonic-gate 			 */
8757c478bd9Sstevel@tonic-gate 			if (iblocks == ip->i_blocks)
8767c478bd9Sstevel@tonic-gate 				new_iblocks = 0; /* no new blocks allocated */
8777c478bd9Sstevel@tonic-gate 		} else {
8787c478bd9Sstevel@tonic-gate 			pagecreate = 0;
8797c478bd9Sstevel@tonic-gate 			/*
8807c478bd9Sstevel@tonic-gate 			 * In sync mode flush the indirect blocks which
8817c478bd9Sstevel@tonic-gate 			 * may have been allocated and not written on
8827c478bd9Sstevel@tonic-gate 			 * disk. In above cases bmap_write will allocate
8837c478bd9Sstevel@tonic-gate 			 * in sync mode.
8847c478bd9Sstevel@tonic-gate 			 */
8857c478bd9Sstevel@tonic-gate 			if (ioflag & (FSYNC|FDSYNC)) {
8867c478bd9Sstevel@tonic-gate 				error = ufs_indirblk_sync(ip, uoff);
8877c478bd9Sstevel@tonic-gate 				if (error)
8887c478bd9Sstevel@tonic-gate 					break;
8897c478bd9Sstevel@tonic-gate 			}
8907c478bd9Sstevel@tonic-gate 		}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 		/*
8937c478bd9Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one
8947c478bd9Sstevel@tonic-gate 		 * of two ways:
8957c478bd9Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
8967c478bd9Sstevel@tonic-gate 		 *    forcefault parameter is true (pagecreate == 0)
8977c478bd9Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
8987c478bd9Sstevel@tonic-gate 		 *
8997c478bd9Sstevel@tonic-gate 		 * We have to drop the contents lock to prevent the VM
9007c478bd9Sstevel@tonic-gate 		 * system from trying to reaquire it in ufs_getpage()
9017c478bd9Sstevel@tonic-gate 		 * should the uiomove cause a pagefault.
9027c478bd9Sstevel@tonic-gate 		 *
9037c478bd9Sstevel@tonic-gate 		 * We have to drop the reader vfs_dqrwlock here as well.
9047c478bd9Sstevel@tonic-gate 		 */
9057c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
9067c478bd9Sstevel@tonic-gate 		if (do_dqrwlock) {
9077c478bd9Sstevel@tonic-gate 			ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
9087c478bd9Sstevel@tonic-gate 			ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
9097c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
9107c478bd9Sstevel@tonic-gate 		}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		base = segmap_getmapflt(segkmap, vp, (off + mapon),
9137c478bd9Sstevel@tonic-gate 					(uint_t)n, !pagecreate, S_WRITE);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		/*
9167c478bd9Sstevel@tonic-gate 		 * segmap_pagecreate() returns 1 if it calls
9177c478bd9Sstevel@tonic-gate 		 * page_create_va() to allocate any pages.
9187c478bd9Sstevel@tonic-gate 		 */
9197c478bd9Sstevel@tonic-gate 		newpage = 0;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 		if (pagecreate)
9227c478bd9Sstevel@tonic-gate 			newpage = segmap_pagecreate(segkmap, base,
9237c478bd9Sstevel@tonic-gate 			    (size_t)n, 0);
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		premove_resid = uio->uio_resid;
9267c478bd9Sstevel@tonic-gate 		error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 		/*
9297c478bd9Sstevel@tonic-gate 		 * If "newpage" is set, then a new page was created and it
9307c478bd9Sstevel@tonic-gate 		 * does not contain valid data, so it needs to be initialized
9317c478bd9Sstevel@tonic-gate 		 * at this point.
9327c478bd9Sstevel@tonic-gate 		 * Otherwise the page contains old data, which was overwritten
9337c478bd9Sstevel@tonic-gate 		 * partially or as a whole in uiomove.
9347c478bd9Sstevel@tonic-gate 		 * If there is only one iovec structure within uio, then
9357c478bd9Sstevel@tonic-gate 		 * on error uiomove will not be able to update uio->uio_loffset
9367c478bd9Sstevel@tonic-gate 		 * and we would zero the whole page here!
9377c478bd9Sstevel@tonic-gate 		 *
9387c478bd9Sstevel@tonic-gate 		 * If uiomove fails because of an error, the old valid data
9397c478bd9Sstevel@tonic-gate 		 * is kept instead of filling the rest of the page with zero's.
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		if (newpage &&
9427c478bd9Sstevel@tonic-gate 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
9437c478bd9Sstevel@tonic-gate 			/*
9447c478bd9Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
9457c478bd9Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
9467c478bd9Sstevel@tonic-gate 			 * This happens on most EOF write cases and if
9477c478bd9Sstevel@tonic-gate 			 * we had some sort of error during the uiomove.
9487c478bd9Sstevel@tonic-gate 			 */
9497c478bd9Sstevel@tonic-gate 			int nzero, nmoved;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 			nmoved = (int)(uio->uio_loffset - (off + mapon));
9527c478bd9Sstevel@tonic-gate 			ASSERT(nmoved >= 0 && nmoved <= n);
9537c478bd9Sstevel@tonic-gate 			nzero = roundup(on + n, PAGESIZE) - nmoved;
9547c478bd9Sstevel@tonic-gate 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
9557c478bd9Sstevel@tonic-gate 			(void) kzero(base + mapon + nmoved, (uint_t)nzero);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 		/*
9597c478bd9Sstevel@tonic-gate 		 * Unlock the pages allocated by page_create_va()
9607c478bd9Sstevel@tonic-gate 		 * in segmap_pagecreate()
9617c478bd9Sstevel@tonic-gate 		 */
9627c478bd9Sstevel@tonic-gate 		if (newpage)
9637c478bd9Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 		/*
9667c478bd9Sstevel@tonic-gate 		 * If the size of the file changed, then update the
9677c478bd9Sstevel@tonic-gate 		 * size field in the inode now.  This can't be done
9687c478bd9Sstevel@tonic-gate 		 * before the call to segmap_pageunlock or there is
9697c478bd9Sstevel@tonic-gate 		 * a potential deadlock with callers to ufs_putpage().
9707c478bd9Sstevel@tonic-gate 		 * They will be holding i_contents and trying to lock
9717c478bd9Sstevel@tonic-gate 		 * a page, while this thread is holding a page locked
9727c478bd9Sstevel@tonic-gate 		 * and trying to acquire i_contents.
9737c478bd9Sstevel@tonic-gate 		 */
9747c478bd9Sstevel@tonic-gate 		if (i_size_changed) {
9757c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
9767c478bd9Sstevel@tonic-gate 			old_i_size = ip->i_size;
9777c478bd9Sstevel@tonic-gate 			UFS_SET_ISIZE(uoff + n, ip);
9787c478bd9Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
9797c478bd9Sstevel@tonic-gate 			/*
9807c478bd9Sstevel@tonic-gate 			 * file has grown larger than 2GB. Set flag
9817c478bd9Sstevel@tonic-gate 			 * in superblock to indicate this, if it
9827c478bd9Sstevel@tonic-gate 			 * is not already set.
9837c478bd9Sstevel@tonic-gate 			 */
9847c478bd9Sstevel@tonic-gate 			if ((ip->i_size > MAXOFF32_T) &&
9857c478bd9Sstevel@tonic-gate 			    !(fs->fs_flags & FSLARGEFILES)) {
9867c478bd9Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
9877c478bd9Sstevel@tonic-gate 				mutex_enter(&ufsvfsp->vfs_lock);
9887c478bd9Sstevel@tonic-gate 				fs->fs_flags |= FSLARGEFILES;
9897c478bd9Sstevel@tonic-gate 				ufs_sbwrite(ufsvfsp);
9907c478bd9Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
9917c478bd9Sstevel@tonic-gate 			}
9927c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
9937c478bd9Sstevel@tonic-gate 			ip->i_writer = NULL;
9947c478bd9Sstevel@tonic-gate 			cv_broadcast(&ip->i_wrcv);
9957c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
9967c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
9977c478bd9Sstevel@tonic-gate 		}
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		if (error) {
10007c478bd9Sstevel@tonic-gate 			/*
10017c478bd9Sstevel@tonic-gate 			 * If we failed on a write, we may have already
10027c478bd9Sstevel@tonic-gate 			 * allocated file blocks as well as pages.  It's
10037c478bd9Sstevel@tonic-gate 			 * hard to undo the block allocation, but we must
10047c478bd9Sstevel@tonic-gate 			 * be sure to invalidate any pages that may have
10057c478bd9Sstevel@tonic-gate 			 * been allocated.
10067c478bd9Sstevel@tonic-gate 			 *
10077c478bd9Sstevel@tonic-gate 			 * If the page was created without initialization
10087c478bd9Sstevel@tonic-gate 			 * then we must check if it should be possible
10097c478bd9Sstevel@tonic-gate 			 * to destroy the new page and to keep the old data
10107c478bd9Sstevel@tonic-gate 			 * on the disk.
10117c478bd9Sstevel@tonic-gate 			 *
10127c478bd9Sstevel@tonic-gate 			 * It is possible to destroy the page without
10137c478bd9Sstevel@tonic-gate 			 * having to write back its contents only when
10147c478bd9Sstevel@tonic-gate 			 * - the size of the file keeps unchanged
10157c478bd9Sstevel@tonic-gate 			 * - bmap_write() did not allocate new disk blocks
10167c478bd9Sstevel@tonic-gate 			 *   it is possible to create big files using "seek" and
10177c478bd9Sstevel@tonic-gate 			 *   write to the end of the file. A "write" to a
10187c478bd9Sstevel@tonic-gate 			 *   position before the end of the file would not
10197c478bd9Sstevel@tonic-gate 			 *   change the size of the file but it would allocate
10207c478bd9Sstevel@tonic-gate 			 *   new disk blocks.
10217c478bd9Sstevel@tonic-gate 			 * - uiomove intended to overwrite the whole page.
10227c478bd9Sstevel@tonic-gate 			 * - a new page was created (newpage == 1).
10237c478bd9Sstevel@tonic-gate 			 */
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 			if (i_size_changed == 0 && new_iblocks == 0 &&
10267c478bd9Sstevel@tonic-gate 			    newpage) {
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 				/* unwind what uiomove eventually last did */
10297c478bd9Sstevel@tonic-gate 				uio->uio_resid = premove_resid;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 				/*
10327c478bd9Sstevel@tonic-gate 				 * destroy the page, do not write ambiguous
10337c478bd9Sstevel@tonic-gate 				 * data to the disk.
10347c478bd9Sstevel@tonic-gate 				 */
10357c478bd9Sstevel@tonic-gate 				flags = SM_DESTROY;
10367c478bd9Sstevel@tonic-gate 			} else {
10377c478bd9Sstevel@tonic-gate 				/*
10387c478bd9Sstevel@tonic-gate 				 * write the page back to the disk, if dirty,
10397c478bd9Sstevel@tonic-gate 				 * and remove the page from the cache.
10407c478bd9Sstevel@tonic-gate 				 */
10417c478bd9Sstevel@tonic-gate 				flags = SM_INVAL;
10427c478bd9Sstevel@tonic-gate 			}
10437c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, flags);
10447c478bd9Sstevel@tonic-gate 		} else {
10457c478bd9Sstevel@tonic-gate 			flags = 0;
10467c478bd9Sstevel@tonic-gate 			/*
10477c478bd9Sstevel@tonic-gate 			 * Force write back for synchronous write cases.
10487c478bd9Sstevel@tonic-gate 			 */
10497c478bd9Sstevel@tonic-gate 			if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
10507c478bd9Sstevel@tonic-gate 				/*
10517c478bd9Sstevel@tonic-gate 				 * If the sticky bit is set but the
10527c478bd9Sstevel@tonic-gate 				 * execute bit is not set, we do a
10537c478bd9Sstevel@tonic-gate 				 * synchronous write back and free
10547c478bd9Sstevel@tonic-gate 				 * the page when done.  We set up swap
10557c478bd9Sstevel@tonic-gate 				 * files to be handled this way to
10567c478bd9Sstevel@tonic-gate 				 * prevent servers from keeping around
10577c478bd9Sstevel@tonic-gate 				 * the client's swap pages too long.
10587c478bd9Sstevel@tonic-gate 				 * XXX - there ought to be a better way.
10597c478bd9Sstevel@tonic-gate 				 */
10607c478bd9Sstevel@tonic-gate 				if (IS_SWAPVP(vp)) {
10617c478bd9Sstevel@tonic-gate 					flags = SM_WRITE | SM_FREE |
10627c478bd9Sstevel@tonic-gate 					    SM_DONTNEED;
10637c478bd9Sstevel@tonic-gate 					iupdat_flag = 0;
10647c478bd9Sstevel@tonic-gate 				} else {
10657c478bd9Sstevel@tonic-gate 					flags = SM_WRITE;
10667c478bd9Sstevel@tonic-gate 				}
10677c478bd9Sstevel@tonic-gate 			} else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
10687c478bd9Sstevel@tonic-gate 				/*
10697c478bd9Sstevel@tonic-gate 				 * Have written a whole block.
10707c478bd9Sstevel@tonic-gate 				 * Start an asynchronous write and
10717c478bd9Sstevel@tonic-gate 				 * mark the buffer to indicate that
10727c478bd9Sstevel@tonic-gate 				 * it won't be needed again soon.
10737c478bd9Sstevel@tonic-gate 				 */
10747c478bd9Sstevel@tonic-gate 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
10757c478bd9Sstevel@tonic-gate 			}
10767c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
10777c478bd9Sstevel@tonic-gate 			/*
10787c478bd9Sstevel@tonic-gate 			 * If the operation failed and is synchronous,
10797c478bd9Sstevel@tonic-gate 			 * then we need to unwind what uiomove() last
10807c478bd9Sstevel@tonic-gate 			 * did so we can potentially return an error to
10817c478bd9Sstevel@tonic-gate 			 * the caller.  If this write operation was
10827c478bd9Sstevel@tonic-gate 			 * done in two pieces and the first succeeded,
10837c478bd9Sstevel@tonic-gate 			 * then we won't return an error for the second
10847c478bd9Sstevel@tonic-gate 			 * piece that failed.  However, we only want to
10857c478bd9Sstevel@tonic-gate 			 * return a resid value that reflects what was
10867c478bd9Sstevel@tonic-gate 			 * really done.
10877c478bd9Sstevel@tonic-gate 			 *
10887c478bd9Sstevel@tonic-gate 			 * Failures for non-synchronous operations can
10897c478bd9Sstevel@tonic-gate 			 * be ignored since the page subsystem will
10907c478bd9Sstevel@tonic-gate 			 * retry the operation until it succeeds or the
10917c478bd9Sstevel@tonic-gate 			 * file system is unmounted.
10927c478bd9Sstevel@tonic-gate 			 */
10937c478bd9Sstevel@tonic-gate 			if (error) {
10947c478bd9Sstevel@tonic-gate 				if ((ioflag & (FSYNC | FDSYNC)) ||
10957c478bd9Sstevel@tonic-gate 				    type == IFDIR) {
10967c478bd9Sstevel@tonic-gate 					uio->uio_resid = premove_resid;
10977c478bd9Sstevel@tonic-gate 				} else {
10987c478bd9Sstevel@tonic-gate 					error = 0;
10997c478bd9Sstevel@tonic-gate 				}
11007c478bd9Sstevel@tonic-gate 			}
11017c478bd9Sstevel@tonic-gate 		}
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		/*
11047c478bd9Sstevel@tonic-gate 		 * Re-acquire contents lock.
11057c478bd9Sstevel@tonic-gate 		 * If it was dropped, reacquire reader vfs_dqrwlock as well.
11067c478bd9Sstevel@tonic-gate 		 */
11077c478bd9Sstevel@tonic-gate 		if (do_dqrwlock)
11087c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
11097c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 		/*
11127c478bd9Sstevel@tonic-gate 		 * If the uiomove() failed or if a synchronous
11137c478bd9Sstevel@tonic-gate 		 * page push failed, fix up i_size.
11147c478bd9Sstevel@tonic-gate 		 */
11157c478bd9Sstevel@tonic-gate 		if (error) {
11167c478bd9Sstevel@tonic-gate 			if (i_size_changed) {
11177c478bd9Sstevel@tonic-gate 				/*
11187c478bd9Sstevel@tonic-gate 				 * The uiomove failed, and we
11197c478bd9Sstevel@tonic-gate 				 * allocated blocks,so get rid
11207c478bd9Sstevel@tonic-gate 				 * of them.
11217c478bd9Sstevel@tonic-gate 				 */
11227c478bd9Sstevel@tonic-gate 				(void) ufs_itrunc(ip, old_i_size, 0, cr);
11237c478bd9Sstevel@tonic-gate 			}
11247c478bd9Sstevel@tonic-gate 		} else {
11257c478bd9Sstevel@tonic-gate 			/*
11267c478bd9Sstevel@tonic-gate 			 * XXX - Can this be out of the loop?
11277c478bd9Sstevel@tonic-gate 			 */
11287c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
11297c478bd9Sstevel@tonic-gate 			/*
11307c478bd9Sstevel@tonic-gate 			 * Only do one increase of i_seq for multiple
11317c478bd9Sstevel@tonic-gate 			 * pieces.  Because we drop locks, record
11327c478bd9Sstevel@tonic-gate 			 * the fact that we changed the timestamp and
11337c478bd9Sstevel@tonic-gate 			 * are deferring the increase in case another thread
11347c478bd9Sstevel@tonic-gate 			 * pushes our timestamp update.
11357c478bd9Sstevel@tonic-gate 			 */
11367c478bd9Sstevel@tonic-gate 			i_seq_needed = 1;
11377c478bd9Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
11387c478bd9Sstevel@tonic-gate 			if (i_size_changed)
11397c478bd9Sstevel@tonic-gate 				ip->i_flag |= IATTCHG;
11407c478bd9Sstevel@tonic-gate 			if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
11417c478bd9Sstevel@tonic-gate 			    (IEXEC >> 6))) != 0 &&
11427c478bd9Sstevel@tonic-gate 			    (ip->i_mode & (ISUID | ISGID)) != 0 &&
11437c478bd9Sstevel@tonic-gate 			    secpolicy_vnode_setid_retain(cr,
11447c478bd9Sstevel@tonic-gate 			    (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
11457c478bd9Sstevel@tonic-gate 				/*
11467c478bd9Sstevel@tonic-gate 				 * Clear Set-UID & Set-GID bits on
11477c478bd9Sstevel@tonic-gate 				 * successful write if not privileged
11487c478bd9Sstevel@tonic-gate 				 * and at least one of the execute bits
11497c478bd9Sstevel@tonic-gate 				 * is set.  If we always clear Set-GID,
11507c478bd9Sstevel@tonic-gate 				 * mandatory file and record locking is
11517c478bd9Sstevel@tonic-gate 				 * unuseable.
11527c478bd9Sstevel@tonic-gate 				 */
11537c478bd9Sstevel@tonic-gate 				ip->i_mode &= ~(ISUID | ISGID);
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
11577c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate out:
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * Make sure i_seq is increased at least once per write
11627c478bd9Sstevel@tonic-gate 	 */
11637c478bd9Sstevel@tonic-gate 	if (i_seq_needed) {
11647c478bd9Sstevel@tonic-gate 		ip->i_seq++;
11657c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~ISEQ;	/* no longer deferred */
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/*
11697c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table -
11707c478bd9Sstevel@tonic-gate 	 *
11717c478bd9Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
11727c478bd9Sstevel@tonic-gate 	 *   --------------------------
11737c478bd9Sstevel@tonic-gate 	 *   always@	  IATTCHG|IBDWRITE
11747c478bd9Sstevel@tonic-gate 	 *
11757c478bd9Sstevel@tonic-gate 	 * @ - 	If we are doing synchronous write the only time we should
11767c478bd9Sstevel@tonic-gate 	 *	not be sync'ing the ip here is if we have the stickyhack
11777c478bd9Sstevel@tonic-gate 	 *	activated, the file is marked with the sticky bit and
11787c478bd9Sstevel@tonic-gate 	 *	no exec bit, the file length has not been changed and
11797c478bd9Sstevel@tonic-gate 	 *	no new blocks have been allocated during this write.
11807c478bd9Sstevel@tonic-gate 	 */
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & ISYNC) != 0) {
11837c478bd9Sstevel@tonic-gate 		/*
11847c478bd9Sstevel@tonic-gate 		 * we have eliminated nosync
11857c478bd9Sstevel@tonic-gate 		 */
11867c478bd9Sstevel@tonic-gate 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
11877c478bd9Sstevel@tonic-gate 			((ioflag & FSYNC) && iupdat_flag)) {
11887c478bd9Sstevel@tonic-gate 			ufs_iupdat(ip, 1);
11897c478bd9Sstevel@tonic-gate 		}
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	/*
11937c478bd9Sstevel@tonic-gate 	 * If we've already done a partial-write, terminate
11947c478bd9Sstevel@tonic-gate 	 * the write but return no error unless the error is ENOSPC
11957c478bd9Sstevel@tonic-gate 	 * because the caller can detect this and free resources and
11967c478bd9Sstevel@tonic-gate 	 * try again.
11977c478bd9Sstevel@tonic-gate 	 */
11987c478bd9Sstevel@tonic-gate 	if ((start_resid != uio->uio_resid) && (error != ENOSPC))
11997c478bd9Sstevel@tonic-gate 		error = 0;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~(INOACC | ISYNC);
12027c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);
12037c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
12047c478bd9Sstevel@tonic-gate 		"ufs_wrip_end:vp %p error %d", vp, error);
12057c478bd9Sstevel@tonic-gate 	return (error);
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate /*
12097c478bd9Sstevel@tonic-gate  * rdip does the real work of read requests for ufs.
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate int
12127c478bd9Sstevel@tonic-gate rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	u_offset_t off;
12157c478bd9Sstevel@tonic-gate 	caddr_t base;
12167c478bd9Sstevel@tonic-gate 	struct fs *fs;
12177c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
12187c478bd9Sstevel@tonic-gate 	struct vnode *vp;
12197c478bd9Sstevel@tonic-gate 	long oresid = uio->uio_resid;
12207c478bd9Sstevel@tonic-gate 	u_offset_t n, on, mapon;
12217c478bd9Sstevel@tonic-gate 	int error = 0;
12227c478bd9Sstevel@tonic-gate 	int doupdate = 1;
12237c478bd9Sstevel@tonic-gate 	uint_t flags, cachemode;
12247c478bd9Sstevel@tonic-gate 	int dofree, directio_status;
12257c478bd9Sstevel@tonic-gate 	krw_t rwtype;
12267c478bd9Sstevel@tonic-gate 	o_mode_t type;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_RWIP_START,
12317c478bd9Sstevel@tonic-gate 		"ufs_rdip_start:vp %p", vp);
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
12367c478bd9Sstevel@tonic-gate 
12377c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
12387c478bd9Sstevel@tonic-gate 		return (EIO);
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 	/* check for valid filetype */
12437c478bd9Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
12447c478bd9Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
12457c478bd9Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
12467c478bd9Sstevel@tonic-gate 		return (EIO);
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset > UFS_MAXOFFSET_T) {
12507c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
12517c478bd9Sstevel@tonic-gate 			"ufs_rdip_end:vp %p error %d", vp, EINVAL);
12527c478bd9Sstevel@tonic-gate 		error = 0;
12537c478bd9Sstevel@tonic-gate 		goto out;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
12567c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
12577c478bd9Sstevel@tonic-gate 			"ufs_rdip_end:vp %p error %d", vp, EINVAL);
12587c478bd9Sstevel@tonic-gate 		return (EINVAL);
12597c478bd9Sstevel@tonic-gate 	}
12607c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
12617c478bd9Sstevel@tonic-gate 		TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
12627c478bd9Sstevel@tonic-gate 			"ufs_rdip_end:vp %p error %d", vp, 0);
12637c478bd9Sstevel@tonic-gate 		return (0);
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
12677c478bd9Sstevel@tonic-gate 		(!ufsvfsp->vfs_noatime)) {
12687c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
12697c478bd9Sstevel@tonic-gate 		ip->i_flag |= IACC;
12707c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
12717c478bd9Sstevel@tonic-gate 	}
12727c478bd9Sstevel@tonic-gate 	/*
12737c478bd9Sstevel@tonic-gate 	 * Try to go direct
12747c478bd9Sstevel@tonic-gate 	 */
12757c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
12767c478bd9Sstevel@tonic-gate 		error = ufs_directio_read(ip, uio, cr, &directio_status);
12777c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
12787c478bd9Sstevel@tonic-gate 			goto out;
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	/*
12847c478bd9Sstevel@tonic-gate 	 * If cache_read_ahead is enabled, we will
12857c478bd9Sstevel@tonic-gate 	 * release the pages at the tail of the cache
12867c478bd9Sstevel@tonic-gate 	 * list, otherwise we will put them at the head.
12877c478bd9Sstevel@tonic-gate 	 */
12887c478bd9Sstevel@tonic-gate 	if (cache_read_ahead)
12897c478bd9Sstevel@tonic-gate 		cachemode = SM_FREE | SM_ASYNC;
12907c478bd9Sstevel@tonic-gate 	else
12917c478bd9Sstevel@tonic-gate 		cachemode = SM_FREE | SM_DONTNEED | SM_ASYNC;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	do {
12947c478bd9Sstevel@tonic-gate 		offset_t diff;
12957c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
12967c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
12977c478bd9Sstevel@tonic-gate 		mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
12987c478bd9Sstevel@tonic-gate 		on = (u_offset_t)blkoff(fs, uoff);
12997c478bd9Sstevel@tonic-gate 		n = MIN((u_offset_t)fs->fs_bsize - on,
13007c478bd9Sstevel@tonic-gate 			(u_offset_t)uio->uio_resid);
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 		diff = ip->i_size - uoff;
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 		if (diff <= (offset_t)0) {
13057c478bd9Sstevel@tonic-gate 			error = 0;
13067c478bd9Sstevel@tonic-gate 			goto out;
13077c478bd9Sstevel@tonic-gate 		}
13087c478bd9Sstevel@tonic-gate 		if (diff < (offset_t)n)
13097c478bd9Sstevel@tonic-gate 			n = (int)diff;
13107c478bd9Sstevel@tonic-gate 		dofree = freebehind &&
13117c478bd9Sstevel@tonic-gate 		    ip->i_nextr == (off & PAGEMASK) && off > smallfile;
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 		/*
13147c478bd9Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one of two
13157c478bd9Sstevel@tonic-gate 		 * ways:
13167c478bd9Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
13177c478bd9Sstevel@tonic-gate 		 *    forcefault parameter is true (value of 1 is passed)
13187c478bd9Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
13197c478bd9Sstevel@tonic-gate 		 *
13207c478bd9Sstevel@tonic-gate 		 * We cannot hold onto an i_contents reader lock without
13217c478bd9Sstevel@tonic-gate 		 * risking deadlock in ufs_getpage() so drop a reader lock.
13227c478bd9Sstevel@tonic-gate 		 * The ufs_getpage() dolock logic already allows for a
13237c478bd9Sstevel@tonic-gate 		 * thread holding i_contents as writer to work properly
13247c478bd9Sstevel@tonic-gate 		 * so we keep a writer lock.
13257c478bd9Sstevel@tonic-gate 		 */
13267c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER)
13277c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
13287c478bd9Sstevel@tonic-gate 		base = segmap_getmapflt(segkmap, vp, (off + mapon),
13297c478bd9Sstevel@tonic-gate 					(uint_t)n, 1, S_READ);
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 		error = uiomove(base + mapon, (long)n, UIO_READ, uio);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		flags = 0;
13347c478bd9Sstevel@tonic-gate 		if (!error) {
13357c478bd9Sstevel@tonic-gate 			/*
13367c478bd9Sstevel@tonic-gate 			 * If reading sequential we won't need
13377c478bd9Sstevel@tonic-gate 			 * this buffer again soon.
13387c478bd9Sstevel@tonic-gate 			 */
13397c478bd9Sstevel@tonic-gate 			if (dofree) {
13407c478bd9Sstevel@tonic-gate 				flags = cachemode;
13417c478bd9Sstevel@tonic-gate 			}
13427c478bd9Sstevel@tonic-gate 			/*
13437c478bd9Sstevel@tonic-gate 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
13447c478bd9Sstevel@tonic-gate 			 * we want to make sure that the page which has
13457c478bd9Sstevel@tonic-gate 			 * been read, is written on disk if it is dirty.
13467c478bd9Sstevel@tonic-gate 			 * And corresponding indirect blocks should also
13477c478bd9Sstevel@tonic-gate 			 * be flushed out.
13487c478bd9Sstevel@tonic-gate 			 */
13497c478bd9Sstevel@tonic-gate 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
13507c478bd9Sstevel@tonic-gate 				flags &= ~SM_ASYNC;
13517c478bd9Sstevel@tonic-gate 				flags |= SM_WRITE;
13527c478bd9Sstevel@tonic-gate 			}
13537c478bd9Sstevel@tonic-gate 			error = segmap_release(segkmap, base, flags);
13547c478bd9Sstevel@tonic-gate 		} else
13557c478bd9Sstevel@tonic-gate 			(void) segmap_release(segkmap, base, flags);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER)
13587c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, rwtype);
13597c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
13607c478bd9Sstevel@tonic-gate out:
13617c478bd9Sstevel@tonic-gate 	/*
13627c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table if FRSYNC is set.
13637c478bd9Sstevel@tonic-gate 	 *
13647c478bd9Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
13657c478bd9Sstevel@tonic-gate 	 *   --------------------------
13667c478bd9Sstevel@tonic-gate 	 *   always	  IATTCHG|IBDWRITE
13677c478bd9Sstevel@tonic-gate 	 */
13687c478bd9Sstevel@tonic-gate 	/*
13697c478bd9Sstevel@tonic-gate 	 * The inode is not updated if we're logging and the inode is a
13707c478bd9Sstevel@tonic-gate 	 * directory with FRSYNC, FSYNC and FDSYNC flags set.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	if (ioflag & FRSYNC) {
13737c478bd9Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
13747c478bd9Sstevel@tonic-gate 				doupdate = 0;
13757c478bd9Sstevel@tonic-gate 		}
13767c478bd9Sstevel@tonic-gate 		if (doupdate) {
13777c478bd9Sstevel@tonic-gate 			if ((ioflag & FSYNC) ||
13787c478bd9Sstevel@tonic-gate 			    ((ioflag & FDSYNC) &&
13797c478bd9Sstevel@tonic-gate 			    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
13807c478bd9Sstevel@tonic-gate 				ufs_iupdat(ip, 1);
13817c478bd9Sstevel@tonic-gate 			}
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 	}
13847c478bd9Sstevel@tonic-gate 	/*
13857c478bd9Sstevel@tonic-gate 	 * If we've already done a partial read, terminate
13867c478bd9Sstevel@tonic-gate 	 * the read but return no error.
13877c478bd9Sstevel@tonic-gate 	 */
13887c478bd9Sstevel@tonic-gate 	if (oresid != uio->uio_resid)
13897c478bd9Sstevel@tonic-gate 		error = 0;
13907c478bd9Sstevel@tonic-gate 	ITIMES(ip);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_RWIP_END,
13937c478bd9Sstevel@tonic-gate 		"ufs_rdip_end:vp %p error %d", vp, error);
13947c478bd9Sstevel@tonic-gate 	return (error);
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /* ARGSUSED */
13987c478bd9Sstevel@tonic-gate static int
13997c478bd9Sstevel@tonic-gate ufs_ioctl(
14007c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
14017c478bd9Sstevel@tonic-gate 	int		cmd,
14027c478bd9Sstevel@tonic-gate 	intptr_t	arg,
14037c478bd9Sstevel@tonic-gate 	int		flag,
14047c478bd9Sstevel@tonic-gate 	struct cred	*cr,
14057c478bd9Sstevel@tonic-gate 	int		*rvalp)
14067c478bd9Sstevel@tonic-gate {
14077c478bd9Sstevel@tonic-gate 	struct lockfs	lockfs, lockfs_out;
14087c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
14097c478bd9Sstevel@tonic-gate 	char		*comment, *original_comment;
14107c478bd9Sstevel@tonic-gate 	struct fs	*fs;
14117c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
14127c478bd9Sstevel@tonic-gate 	offset_t	off;
14137c478bd9Sstevel@tonic-gate 	extern int	maxphys;
14147c478bd9Sstevel@tonic-gate 	int		error;
14157c478bd9Sstevel@tonic-gate 	int		issync;
14167c478bd9Sstevel@tonic-gate 	int		trans_size;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * forcibly unmounted
14217c478bd9Sstevel@tonic-gate 	 */
14227c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
14237c478bd9Sstevel@tonic-gate 		return (EIO);
14247c478bd9Sstevel@tonic-gate 	}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	if (cmd == Q_QUOTACTL) {
14297c478bd9Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
14307c478bd9Sstevel@tonic-gate 		if (error)
14317c478bd9Sstevel@tonic-gate 			return (error);
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 		if (ulp) {
14347c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
14357c478bd9Sstevel@tonic-gate 						TOP_SETQUOTA_SIZE(fs));
14367c478bd9Sstevel@tonic-gate 		}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 		error = quotactl(vp, arg, flag, cr);
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 		if (ulp) {
14417c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
14427c478bd9Sstevel@tonic-gate 						TOP_SETQUOTA_SIZE(fs));
14437c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
14447c478bd9Sstevel@tonic-gate 		}
14457c478bd9Sstevel@tonic-gate 		return (error);
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	switch (cmd) {
14497c478bd9Sstevel@tonic-gate 		case _FIOLFS:
14507c478bd9Sstevel@tonic-gate 			/*
14517c478bd9Sstevel@tonic-gate 			 * file system locking
14527c478bd9Sstevel@tonic-gate 			 */
14537c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
14547c478bd9Sstevel@tonic-gate 				return (EPERM);
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
14577c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
14587c478bd9Sstevel@tonic-gate 						sizeof (struct lockfs)))
14597c478bd9Sstevel@tonic-gate 					return (EFAULT);
14607c478bd9Sstevel@tonic-gate 			}
14617c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14627c478bd9Sstevel@tonic-gate 			else {
14637c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
14647c478bd9Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
14657c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
14667c478bd9Sstevel@tonic-gate 				    sizeof (struct lockfs32)))
14677c478bd9Sstevel@tonic-gate 					return (EFAULT);
14687c478bd9Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
14697c478bd9Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
14707c478bd9Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
14717c478bd9Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
14727c478bd9Sstevel@tonic-gate 				lockfs.lf_comment =
14737c478bd9Sstevel@tonic-gate 					(caddr_t)(uintptr_t)lockfs32.lf_comment;
14747c478bd9Sstevel@tonic-gate 			}
14757c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 			if (lockfs.lf_comlen) {
14787c478bd9Sstevel@tonic-gate 				if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
14797c478bd9Sstevel@tonic-gate 					return (ENAMETOOLONG);
14807c478bd9Sstevel@tonic-gate 				comment = kmem_alloc(lockfs.lf_comlen,
14817c478bd9Sstevel@tonic-gate 						KM_SLEEP);
14827c478bd9Sstevel@tonic-gate 				if (copyin(lockfs.lf_comment, comment,
14837c478bd9Sstevel@tonic-gate 					lockfs.lf_comlen)) {
14847c478bd9Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
14857c478bd9Sstevel@tonic-gate 					return (EFAULT);
14867c478bd9Sstevel@tonic-gate 				}
14877c478bd9Sstevel@tonic-gate 				original_comment = lockfs.lf_comment;
14887c478bd9Sstevel@tonic-gate 				lockfs.lf_comment = comment;
14897c478bd9Sstevel@tonic-gate 			}
14907c478bd9Sstevel@tonic-gate 			if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
14917c478bd9Sstevel@tonic-gate 				lockfs.lf_comment = original_comment;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 				if ((flag & DATAMODEL_MASK) ==
14947c478bd9Sstevel@tonic-gate 				    DATAMODEL_NATIVE) {
14957c478bd9Sstevel@tonic-gate 					(void) copyout(&lockfs, (caddr_t)arg,
14967c478bd9Sstevel@tonic-gate 					    sizeof (struct lockfs));
14977c478bd9Sstevel@tonic-gate 				}
14987c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
14997c478bd9Sstevel@tonic-gate 				else {
15007c478bd9Sstevel@tonic-gate 					struct lockfs32	lockfs32;
15017c478bd9Sstevel@tonic-gate 					/* Translate LP64 to ILP32 lockfs */
15027c478bd9Sstevel@tonic-gate 					lockfs32.lf_lock =
15037c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_lock;
15047c478bd9Sstevel@tonic-gate 					lockfs32.lf_flags =
15057c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_flags;
15067c478bd9Sstevel@tonic-gate 					lockfs32.lf_key =
15077c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_key;
15087c478bd9Sstevel@tonic-gate 					lockfs32.lf_comlen =
15097c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_comlen;
15107c478bd9Sstevel@tonic-gate 					lockfs32.lf_comment =
15117c478bd9Sstevel@tonic-gate 					(uint32_t)(uintptr_t)lockfs.lf_comment;
15127c478bd9Sstevel@tonic-gate 					(void) copyout(&lockfs32, (caddr_t)arg,
15137c478bd9Sstevel@tonic-gate 					    sizeof (struct lockfs32));
15147c478bd9Sstevel@tonic-gate 				}
15157c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate 			} else {
15187c478bd9Sstevel@tonic-gate 				if (lockfs.lf_comlen)
15197c478bd9Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
15207c478bd9Sstevel@tonic-gate 			}
15217c478bd9Sstevel@tonic-gate 			return (error);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 		case _FIOLFSS:
15247c478bd9Sstevel@tonic-gate 			/*
15257c478bd9Sstevel@tonic-gate 			 * get file system locking status
15267c478bd9Sstevel@tonic-gate 			 */
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
15297c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
15307c478bd9Sstevel@tonic-gate 						sizeof (struct lockfs)))
15317c478bd9Sstevel@tonic-gate 					return (EFAULT);
15327c478bd9Sstevel@tonic-gate 			}
15337c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
15347c478bd9Sstevel@tonic-gate 			else {
15357c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
15367c478bd9Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
15377c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
15387c478bd9Sstevel@tonic-gate 						sizeof (struct lockfs32)))
15397c478bd9Sstevel@tonic-gate 					return (EFAULT);
15407c478bd9Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
15417c478bd9Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
15427c478bd9Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
15437c478bd9Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
15447c478bd9Sstevel@tonic-gate 				lockfs.lf_comment =
15457c478bd9Sstevel@tonic-gate 					(caddr_t)(uintptr_t)lockfs32.lf_comment;
15467c478bd9Sstevel@tonic-gate 			}
15477c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 			if (error =  ufs_fiolfss(vp, &lockfs_out))
15507c478bd9Sstevel@tonic-gate 				return (error);
15517c478bd9Sstevel@tonic-gate 			lockfs.lf_lock = lockfs_out.lf_lock;
15527c478bd9Sstevel@tonic-gate 			lockfs.lf_key = lockfs_out.lf_key;
15537c478bd9Sstevel@tonic-gate 			lockfs.lf_flags = lockfs_out.lf_flags;
15547c478bd9Sstevel@tonic-gate 			lockfs.lf_comlen = MIN(lockfs.lf_comlen,
15557c478bd9Sstevel@tonic-gate 				lockfs_out.lf_comlen);
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
15587c478bd9Sstevel@tonic-gate 				if (copyout(&lockfs, (caddr_t)arg,
15597c478bd9Sstevel@tonic-gate 						sizeof (struct lockfs)))
15607c478bd9Sstevel@tonic-gate 					return (EFAULT);
15617c478bd9Sstevel@tonic-gate 			}
15627c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
15637c478bd9Sstevel@tonic-gate 			else {
15647c478bd9Sstevel@tonic-gate 				/* Translate LP64 to ILP32 lockfs */
15657c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
15667c478bd9Sstevel@tonic-gate 				lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
15677c478bd9Sstevel@tonic-gate 				lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
15687c478bd9Sstevel@tonic-gate 				lockfs32.lf_key = (uint32_t)lockfs.lf_key;
15697c478bd9Sstevel@tonic-gate 				lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
15707c478bd9Sstevel@tonic-gate 				lockfs32.lf_comment =
15717c478bd9Sstevel@tonic-gate 					(uint32_t)(uintptr_t)lockfs.lf_comment;
15727c478bd9Sstevel@tonic-gate 				if (copyout(&lockfs32, (caddr_t)arg,
15737c478bd9Sstevel@tonic-gate 					    sizeof (struct lockfs32)))
15747c478bd9Sstevel@tonic-gate 					return (EFAULT);
15757c478bd9Sstevel@tonic-gate 			}
15767c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 			if (lockfs.lf_comlen &&
15797c478bd9Sstevel@tonic-gate 			    lockfs.lf_comment && lockfs_out.lf_comment)
15807c478bd9Sstevel@tonic-gate 				if (copyout(lockfs_out.lf_comment,
15817c478bd9Sstevel@tonic-gate 					lockfs.lf_comment,
15827c478bd9Sstevel@tonic-gate 					lockfs.lf_comlen))
15837c478bd9Sstevel@tonic-gate 					return (EFAULT);
15847c478bd9Sstevel@tonic-gate 			return (0);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 		case _FIOSATIME:
15877c478bd9Sstevel@tonic-gate 			/*
15887c478bd9Sstevel@tonic-gate 			 * set access time
15897c478bd9Sstevel@tonic-gate 			 */
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 			/*
15927c478bd9Sstevel@tonic-gate 			 * if mounted w/o atime, return quietly.
15937c478bd9Sstevel@tonic-gate 			 * I briefly thought about returning ENOSYS, but
15947c478bd9Sstevel@tonic-gate 			 * figured that most apps would consider this fatal
15957c478bd9Sstevel@tonic-gate 			 * but the idea is to make this as seamless as poss.
15967c478bd9Sstevel@tonic-gate 			 */
15977c478bd9Sstevel@tonic-gate 			if (ufsvfsp->vfs_noatime)
15987c478bd9Sstevel@tonic-gate 				return (0);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
16017c478bd9Sstevel@tonic-gate 					ULOCKFS_SETATTR_MASK);
16027c478bd9Sstevel@tonic-gate 			if (error)
16037c478bd9Sstevel@tonic-gate 				return (error);
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate 			if (ulp) {
16067c478bd9Sstevel@tonic-gate 				trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
16077c478bd9Sstevel@tonic-gate 				TRANS_BEGIN_CSYNC(ufsvfsp, issync,
16087c478bd9Sstevel@tonic-gate 						TOP_SETATTR, trans_size);
16097c478bd9Sstevel@tonic-gate 			}
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 			error = ufs_fiosatime(vp, (struct timeval *)arg,
16127c478bd9Sstevel@tonic-gate 					flag, cr);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 			if (ulp) {
16157c478bd9Sstevel@tonic-gate 				TRANS_END_CSYNC(ufsvfsp, error, issync,
16167c478bd9Sstevel@tonic-gate 						TOP_SETATTR, trans_size);
16177c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
16187c478bd9Sstevel@tonic-gate 			}
16197c478bd9Sstevel@tonic-gate 			return (error);
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 		case _FIOSDIO:
16227c478bd9Sstevel@tonic-gate 			/*
16237c478bd9Sstevel@tonic-gate 			 * set delayed-io
16247c478bd9Sstevel@tonic-gate 			 */
16257c478bd9Sstevel@tonic-gate 			return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		case _FIOGDIO:
16287c478bd9Sstevel@tonic-gate 			/*
16297c478bd9Sstevel@tonic-gate 			 * get delayed-io
16307c478bd9Sstevel@tonic-gate 			 */
16317c478bd9Sstevel@tonic-gate 			return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		case _FIOIO:
16347c478bd9Sstevel@tonic-gate 			/*
16357c478bd9Sstevel@tonic-gate 			 * inode open
16367c478bd9Sstevel@tonic-gate 			 */
16377c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
16387c478bd9Sstevel@tonic-gate 					ULOCKFS_VGET_MASK);
16397c478bd9Sstevel@tonic-gate 			if (error)
16407c478bd9Sstevel@tonic-gate 				return (error);
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 			error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 			if (ulp) {
16457c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
16467c478bd9Sstevel@tonic-gate 			}
16477c478bd9Sstevel@tonic-gate 			return (error);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 		case _FIOFFS:
16507c478bd9Sstevel@tonic-gate 			/*
16517c478bd9Sstevel@tonic-gate 			 * file system flush (push w/invalidate)
16527c478bd9Sstevel@tonic-gate 			 */
16537c478bd9Sstevel@tonic-gate 			if ((caddr_t)arg != NULL)
16547c478bd9Sstevel@tonic-gate 				return (EINVAL);
16557c478bd9Sstevel@tonic-gate 			return (ufs_fioffs(vp, NULL, cr));
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate 		case _FIOISBUSY:
16587c478bd9Sstevel@tonic-gate 			/*
16597c478bd9Sstevel@tonic-gate 			 * Contract-private interface for Legato
16607c478bd9Sstevel@tonic-gate 			 * Purge this vnode from the DNLC and decide
16617c478bd9Sstevel@tonic-gate 			 * if this vnode is busy (*arg == 1) or not
16627c478bd9Sstevel@tonic-gate 			 * (*arg == 0)
16637c478bd9Sstevel@tonic-gate 			 */
16647c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
16657c478bd9Sstevel@tonic-gate 				return (EPERM);
16667c478bd9Sstevel@tonic-gate 			error = ufs_fioisbusy(vp, (int *)arg, cr);
16677c478bd9Sstevel@tonic-gate 			return (error);
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 		case _FIODIRECTIO:
16707c478bd9Sstevel@tonic-gate 			return (ufs_fiodirectio(vp, (int)arg, cr));
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		case _FIOTUNE:
16737c478bd9Sstevel@tonic-gate 			/*
16747c478bd9Sstevel@tonic-gate 			 * Tune the file system (aka setting fs attributes)
16757c478bd9Sstevel@tonic-gate 			 */
16767c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
16777c478bd9Sstevel@tonic-gate 					ULOCKFS_SETATTR_MASK);
16787c478bd9Sstevel@tonic-gate 			if (error)
16797c478bd9Sstevel@tonic-gate 				return (error);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 			error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 			if (ulp)
16847c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
16857c478bd9Sstevel@tonic-gate 			return (error);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 		case _FIOLOGENABLE:
16887c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
16897c478bd9Sstevel@tonic-gate 				return (EPERM);
16907c478bd9Sstevel@tonic-gate 			return (ufs_fiologenable(vp, (void *)arg, cr, flag));
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 		case _FIOLOGDISABLE:
16937c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
16947c478bd9Sstevel@tonic-gate 				return (EPERM);
16957c478bd9Sstevel@tonic-gate 			return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 		case _FIOISLOG:
16987c478bd9Sstevel@tonic-gate 			return (ufs_fioislog(vp, (void *)arg, cr, flag));
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 		case _FIOSNAPSHOTCREATE_MULTI:
17017c478bd9Sstevel@tonic-gate 		{
17027c478bd9Sstevel@tonic-gate 			struct fiosnapcreate_multi	fc, *fcp;
17037c478bd9Sstevel@tonic-gate 			size_t	fcm_size;
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
17067c478bd9Sstevel@tonic-gate 				return (EFAULT);
17077c478bd9Sstevel@tonic-gate 			if (fc.backfilecount > MAX_BACKFILE_COUNT)
17087c478bd9Sstevel@tonic-gate 				return (EINVAL);
17097c478bd9Sstevel@tonic-gate 			fcm_size = sizeof (struct fiosnapcreate_multi) +
17107c478bd9Sstevel@tonic-gate 			    (fc.backfilecount - 1) * sizeof (int);
17117c478bd9Sstevel@tonic-gate 			fcp = (struct fiosnapcreate_multi *)
17127c478bd9Sstevel@tonic-gate 			    kmem_alloc(fcm_size, KM_SLEEP);
17137c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, fcp, fcm_size)) {
17147c478bd9Sstevel@tonic-gate 				kmem_free(fcp, fcm_size);
17157c478bd9Sstevel@tonic-gate 				return (EFAULT);
17167c478bd9Sstevel@tonic-gate 			}
17177c478bd9Sstevel@tonic-gate 			error = ufs_snap_create(vp, fcp, cr);
17187c478bd9Sstevel@tonic-gate 			if (!error && copyout(fcp, (void *)arg, fcm_size))
17197c478bd9Sstevel@tonic-gate 				error = EFAULT;
17207c478bd9Sstevel@tonic-gate 			kmem_free(fcp, fcm_size);
17217c478bd9Sstevel@tonic-gate 			return (error);
17227c478bd9Sstevel@tonic-gate 		}
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 		case _FIOSNAPSHOTDELETE:
17257c478bd9Sstevel@tonic-gate 		{
17267c478bd9Sstevel@tonic-gate 			struct fiosnapdelete	fc;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
17297c478bd9Sstevel@tonic-gate 				return (EFAULT);
17307c478bd9Sstevel@tonic-gate 			error = ufs_snap_delete(vp, &fc, cr);
17317c478bd9Sstevel@tonic-gate 			if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
17327c478bd9Sstevel@tonic-gate 				error = EFAULT;
17337c478bd9Sstevel@tonic-gate 			return (error);
17347c478bd9Sstevel@tonic-gate 		}
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 		case _FIOGETSUPERBLOCK:
17377c478bd9Sstevel@tonic-gate 			if (copyout(fs, (void *)arg, SBSIZE))
17387c478bd9Sstevel@tonic-gate 				return (EFAULT);
17397c478bd9Sstevel@tonic-gate 			return (0);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 		case _FIOGETMAXPHYS:
17427c478bd9Sstevel@tonic-gate 			if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
17437c478bd9Sstevel@tonic-gate 				return (EFAULT);
17447c478bd9Sstevel@tonic-gate 			return (0);
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 		/*
17477c478bd9Sstevel@tonic-gate 		 * The following 3 ioctls are for TSufs support
17487c478bd9Sstevel@tonic-gate 		 * although could potentially be used elsewhere
17497c478bd9Sstevel@tonic-gate 		 */
17507c478bd9Sstevel@tonic-gate 		case _FIO_SET_LUFS_DEBUG:
17517c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
17527c478bd9Sstevel@tonic-gate 				return (EPERM);
17537c478bd9Sstevel@tonic-gate 			lufs_debug = (uint32_t)arg;
17547c478bd9Sstevel@tonic-gate 			return (0);
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 		case _FIO_SET_LUFS_ERROR:
17577c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
17587c478bd9Sstevel@tonic-gate 				return (EPERM);
17597c478bd9Sstevel@tonic-gate 			TRANS_SETERROR(ufsvfsp);
17607c478bd9Sstevel@tonic-gate 			return (0);
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		case _FIO_GET_TOP_STATS:
17637c478bd9Sstevel@tonic-gate 		{
17647c478bd9Sstevel@tonic-gate 			fio_lufs_stats_t *ls;
17657c478bd9Sstevel@tonic-gate 			ml_unit_t *ul = ufsvfsp->vfs_log;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 			ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
17687c478bd9Sstevel@tonic-gate 			ls->ls_debug = ul->un_debug; /* return debug value */
17697c478bd9Sstevel@tonic-gate 			/* Copy stucture if statistics are being kept */
17707c478bd9Sstevel@tonic-gate 			if (ul->un_logmap->mtm_tops) {
17717c478bd9Sstevel@tonic-gate 				ls->ls_topstats = *(ul->un_logmap->mtm_tops);
17727c478bd9Sstevel@tonic-gate 			}
17737c478bd9Sstevel@tonic-gate 			error = 0;
17747c478bd9Sstevel@tonic-gate 			if (copyout(ls, (void *)arg, sizeof (*ls)))
17757c478bd9Sstevel@tonic-gate 				error = EFAULT;
17767c478bd9Sstevel@tonic-gate 			kmem_free(ls, sizeof (*ls));
17777c478bd9Sstevel@tonic-gate 			return (error);
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 
17807c478bd9Sstevel@tonic-gate 		case _FIO_SEEK_DATA:
17817c478bd9Sstevel@tonic-gate 		case _FIO_SEEK_HOLE:
17827c478bd9Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
17837c478bd9Sstevel@tonic-gate 				return (EFAULT);
17847c478bd9Sstevel@tonic-gate 			/* offset paramater is in/out */
17857c478bd9Sstevel@tonic-gate 			error = ufs_fio_holey(vp, cmd, &off);
17867c478bd9Sstevel@tonic-gate 			if (error)
17877c478bd9Sstevel@tonic-gate 				return (error);
17887c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
17897c478bd9Sstevel@tonic-gate 				return (EFAULT);
17907c478bd9Sstevel@tonic-gate 			return (0);
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 		default:
17937c478bd9Sstevel@tonic-gate 			return (ENOTTY);
17947c478bd9Sstevel@tonic-gate 	}
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate /* ARGSUSED */
17987c478bd9Sstevel@tonic-gate static int
17997c478bd9Sstevel@tonic-gate ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
18007c478bd9Sstevel@tonic-gate 	struct cred *cr)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
18037c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
18047c478bd9Sstevel@tonic-gate 	int err;
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_GETATTR_START,
18077c478bd9Sstevel@tonic-gate 		"ufs_getattr_start:vp %p flags %x", vp, flags);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	if (vap->va_mask == AT_SIZE) {
18107c478bd9Sstevel@tonic-gate 		/*
18117c478bd9Sstevel@tonic-gate 		 * for performance, if only the size is requested don't bother
18127c478bd9Sstevel@tonic-gate 		 * with anything else.
18137c478bd9Sstevel@tonic-gate 		 */
18147c478bd9Sstevel@tonic-gate 		UFS_GET_ISIZE(&vap->va_size, ip);
18157c478bd9Sstevel@tonic-gate 		TRACE_1(TR_FAC_UFS, TR_UFS_GETATTR_END,
18167c478bd9Sstevel@tonic-gate 			"ufs_getattr_end:vp %p", vp);
18177c478bd9Sstevel@tonic-gate 		return (0);
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	/*
18217c478bd9Sstevel@tonic-gate 	 * inlined lockfs checks
18227c478bd9Sstevel@tonic-gate 	 */
18237c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
18247c478bd9Sstevel@tonic-gate 	if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
18257c478bd9Sstevel@tonic-gate 		err = EIO;
18267c478bd9Sstevel@tonic-gate 		goto out;
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
18307c478bd9Sstevel@tonic-gate 	/*
18317c478bd9Sstevel@tonic-gate 	 * Return all the attributes.  This should be refined so
18327c478bd9Sstevel@tonic-gate 	 * that it only returns what's asked for.
18337c478bd9Sstevel@tonic-gate 	 */
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	/*
18367c478bd9Sstevel@tonic-gate 	 * Copy from inode table.
18377c478bd9Sstevel@tonic-gate 	 */
18387c478bd9Sstevel@tonic-gate 	vap->va_type = vp->v_type;
18397c478bd9Sstevel@tonic-gate 	vap->va_mode = ip->i_mode & MODEMASK;
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * If there is an ACL and there is a mask entry, then do the
18427c478bd9Sstevel@tonic-gate 	 * extra work that completes the equivalent of an acltomode(3)
18437c478bd9Sstevel@tonic-gate 	 * call.  According to POSIX P1003.1e, the acl mask should be
18447c478bd9Sstevel@tonic-gate 	 * returned in the group permissions field.
18457c478bd9Sstevel@tonic-gate 	 *
18467c478bd9Sstevel@tonic-gate 	 * - start with the original permission and mode bits (from above)
18477c478bd9Sstevel@tonic-gate 	 * - clear the group owner bits
18487c478bd9Sstevel@tonic-gate 	 * - add in the mask bits.
18497c478bd9Sstevel@tonic-gate 	 */
18507c478bd9Sstevel@tonic-gate 	if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
18517c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
18527c478bd9Sstevel@tonic-gate 		vap->va_mode |=
18537c478bd9Sstevel@tonic-gate 		    (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 	vap->va_uid = ip->i_uid;
18567c478bd9Sstevel@tonic-gate 	vap->va_gid = ip->i_gid;
18577c478bd9Sstevel@tonic-gate 	vap->va_fsid = ip->i_dev;
18587c478bd9Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)ip->i_number;
18597c478bd9Sstevel@tonic-gate 	vap->va_nlink = ip->i_nlink;
18607c478bd9Sstevel@tonic-gate 	vap->va_size = ip->i_size;
18617c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK)
18627c478bd9Sstevel@tonic-gate 		vap->va_rdev = ip->i_rdev;
18637c478bd9Sstevel@tonic-gate 	else
18647c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;	/* not a b/c spec. */
18657c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
18667c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
18677c478bd9Sstevel@tonic-gate 	vap->va_seq = ip->i_seq;
18687c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
18697c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
18707c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
18717c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
18727c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
18737c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
18747c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	switch (ip->i_mode & IFMT) {
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	case IFBLK:
18797c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;		/* was BLKDEV_IOSIZE */
18807c478bd9Sstevel@tonic-gate 		break;
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	case IFCHR:
18837c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
18847c478bd9Sstevel@tonic-gate 		break;
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	default:
18877c478bd9Sstevel@tonic-gate 		vap->va_blksize = ip->i_fs->fs_bsize;
18887c478bd9Sstevel@tonic-gate 		break;
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
18917c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
18927c478bd9Sstevel@tonic-gate 	err = 0;
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate out:
18957c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_GETATTR_END, "ufs_getattr_end:vp %p", vp);
18967c478bd9Sstevel@tonic-gate 
18977c478bd9Sstevel@tonic-gate 	return (err);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
19017c478bd9Sstevel@tonic-gate static int
19027c478bd9Sstevel@tonic-gate ufs_setattr(
19037c478bd9Sstevel@tonic-gate 	struct vnode *vp,
19047c478bd9Sstevel@tonic-gate 	struct vattr *vap,
19057c478bd9Sstevel@tonic-gate 	int flags,
19067c478bd9Sstevel@tonic-gate 	struct cred *cr,
19077c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
19087c478bd9Sstevel@tonic-gate {
19097c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
19107c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
19117c478bd9Sstevel@tonic-gate 	struct fs *fs;
19127c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
19137c478bd9Sstevel@tonic-gate 	char *errmsg1;
19147c478bd9Sstevel@tonic-gate 	char *errmsg2;
19157c478bd9Sstevel@tonic-gate 	long blocks;
19167c478bd9Sstevel@tonic-gate 	long int mask = vap->va_mask;
19177c478bd9Sstevel@tonic-gate 	size_t len1, len2;
19187c478bd9Sstevel@tonic-gate 	int issync;
19197c478bd9Sstevel@tonic-gate 	int trans_size;
19207c478bd9Sstevel@tonic-gate 	int dotrans;
19217c478bd9Sstevel@tonic-gate 	int dorwlock;
19227c478bd9Sstevel@tonic-gate 	int error;
19237c478bd9Sstevel@tonic-gate 	int owner_change;
19247c478bd9Sstevel@tonic-gate 	int dodqlock;
19257c478bd9Sstevel@tonic-gate 	timestruc_t now;
19267c478bd9Sstevel@tonic-gate 	vattr_t oldva;
19277c478bd9Sstevel@tonic-gate 	int retry = 1;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_SETATTR_START,
19307c478bd9Sstevel@tonic-gate 		"ufs_setattr_start:vp %p flags %x", vp, flags);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	/*
19337c478bd9Sstevel@tonic-gate 	 * Cannot set these attributes.
19347c478bd9Sstevel@tonic-gate 	 */
19357c478bd9Sstevel@tonic-gate 	if (mask & AT_NOSET) {
19367c478bd9Sstevel@tonic-gate 		error = EINVAL;
19377c478bd9Sstevel@tonic-gate 		goto out;
19387c478bd9Sstevel@tonic-gate 	}
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	/*
19417c478bd9Sstevel@tonic-gate 	 * check for forced unmount
19427c478bd9Sstevel@tonic-gate 	 */
19437c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
19447c478bd9Sstevel@tonic-gate 		return (EIO);
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
19477c478bd9Sstevel@tonic-gate 	if (fs->fs_ronly != 0)
19487c478bd9Sstevel@tonic-gate 		return (EROFS);
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate again:
19517c478bd9Sstevel@tonic-gate 	errmsg1 = NULL;
19527c478bd9Sstevel@tonic-gate 	errmsg2 = NULL;
19537c478bd9Sstevel@tonic-gate 	dotrans = 0;
19547c478bd9Sstevel@tonic-gate 	dorwlock = 0;
19557c478bd9Sstevel@tonic-gate 	dodqlock = 0;
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
19587c478bd9Sstevel@tonic-gate 	if (error)
19597c478bd9Sstevel@tonic-gate 		goto out;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
19637c478bd9Sstevel@tonic-gate 	 * This follows the protocol for read()/write().
19647c478bd9Sstevel@tonic-gate 	 */
19657c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
19667c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
19677c478bd9Sstevel@tonic-gate 		dorwlock = 1;
19687c478bd9Sstevel@tonic-gate 	}
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	/*
19717c478bd9Sstevel@tonic-gate 	 * Truncate file.  Must have write permission and not be a directory.
19727c478bd9Sstevel@tonic-gate 	 */
19737c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
19747c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
19757c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
19767c478bd9Sstevel@tonic-gate 			error = EISDIR;
19777c478bd9Sstevel@tonic-gate 			goto update_inode;
19787c478bd9Sstevel@tonic-gate 		}
19797c478bd9Sstevel@tonic-gate 		if (error = ufs_iaccess(ip, IWRITE, cr))
19807c478bd9Sstevel@tonic-gate 			goto update_inode;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
19837c478bd9Sstevel@tonic-gate 		error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
19847c478bd9Sstevel@tonic-gate 		if (error) {
19857c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
19867c478bd9Sstevel@tonic-gate 			goto update_inode;
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	if (ulp) {
19917c478bd9Sstevel@tonic-gate 		trans_size = (int)TOP_SETATTR_SIZE(ip);
19927c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
19937c478bd9Sstevel@tonic-gate 		++dotrans;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
19987c478bd9Sstevel@tonic-gate 	 * This follows the protocol established by
19997c478bd9Sstevel@tonic-gate 	 * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
20007c478bd9Sstevel@tonic-gate 	 */
20017c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
20027c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
20037c478bd9Sstevel@tonic-gate 		dorwlock = 1;
20047c478bd9Sstevel@tonic-gate 	}
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate 	/*
20077c478bd9Sstevel@tonic-gate 	 * Grab quota lock if we are changing the file's owner.
20087c478bd9Sstevel@tonic-gate 	 */
20097c478bd9Sstevel@tonic-gate 	if (mask & AT_UID) {
20107c478bd9Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
20117c478bd9Sstevel@tonic-gate 		dodqlock = 1;
20127c478bd9Sstevel@tonic-gate 	}
20137c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	oldva.va_mode = ip->i_mode;
20167c478bd9Sstevel@tonic-gate 	oldva.va_uid = ip->i_uid;
20177c478bd9Sstevel@tonic-gate 	oldva.va_gid = ip->i_gid;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 	vap->va_mask &= ~AT_SIZE;
20207c478bd9Sstevel@tonic-gate 	/*
20217c478bd9Sstevel@tonic-gate 	 * ufs_iaccess is "close enough"; that's because it doesn't
20227c478bd9Sstevel@tonic-gate 	 * map the defines.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
20257c478bd9Sstevel@tonic-gate 				ufs_iaccess, ip);
20267c478bd9Sstevel@tonic-gate 	if (error)
20277c478bd9Sstevel@tonic-gate 		goto update_inode;
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate 	/*
20327c478bd9Sstevel@tonic-gate 	 * Change file access modes.
20337c478bd9Sstevel@tonic-gate 	 */
20347c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
20357c478bd9Sstevel@tonic-gate 		ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
20367c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
20377c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
20387c478bd9Sstevel@tonic-gate 		if (stickyhack) {
20397c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
20407c478bd9Sstevel@tonic-gate 			if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
20417c478bd9Sstevel@tonic-gate 				vp->v_flag |= VSWAPLIKE;
20427c478bd9Sstevel@tonic-gate 			else
20437c478bd9Sstevel@tonic-gate 				vp->v_flag &= ~VSWAPLIKE;
20447c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
20457c478bd9Sstevel@tonic-gate 		}
20467c478bd9Sstevel@tonic-gate 	}
20477c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
20487c478bd9Sstevel@tonic-gate 		if (mask & AT_UID) {
20497c478bd9Sstevel@tonic-gate 			/*
20507c478bd9Sstevel@tonic-gate 			 * Don't change ownership of the quota inode.
20517c478bd9Sstevel@tonic-gate 			 */
20527c478bd9Sstevel@tonic-gate 			if (ufsvfsp->vfs_qinod == ip) {
20537c478bd9Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
20547c478bd9Sstevel@tonic-gate 				error = EINVAL;
20557c478bd9Sstevel@tonic-gate 				goto update_inode;
20567c478bd9Sstevel@tonic-gate 			}
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 			/*
20597c478bd9Sstevel@tonic-gate 			 * No real ownership change.
20607c478bd9Sstevel@tonic-gate 			 */
20617c478bd9Sstevel@tonic-gate 			if (ip->i_uid == vap->va_uid) {
20627c478bd9Sstevel@tonic-gate 				blocks = 0;
20637c478bd9Sstevel@tonic-gate 				owner_change = 0;
20647c478bd9Sstevel@tonic-gate 			}
20657c478bd9Sstevel@tonic-gate 			/*
20667c478bd9Sstevel@tonic-gate 			 * Remove the blocks and the file, from the old user's
20677c478bd9Sstevel@tonic-gate 			 * quota.
20687c478bd9Sstevel@tonic-gate 			 */
20697c478bd9Sstevel@tonic-gate 			else {
20707c478bd9Sstevel@tonic-gate 				blocks = ip->i_blocks;
20717c478bd9Sstevel@tonic-gate 				owner_change = 1;
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 				(void) chkdq(ip, -blocks, /* force */ 1, cr,
20747c478bd9Sstevel@tonic-gate 						(char **)NULL, (size_t *)NULL);
20757c478bd9Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ -1, ip,
20767c478bd9Sstevel@tonic-gate 						(uid_t)ip->i_uid,
20777c478bd9Sstevel@tonic-gate 						/* force */ 1, cr,
20787c478bd9Sstevel@tonic-gate 						(char **)NULL, (size_t *)NULL);
20797c478bd9Sstevel@tonic-gate 				dqrele(ip->i_dquot);
20807c478bd9Sstevel@tonic-gate 			}
20817c478bd9Sstevel@tonic-gate 
20827c478bd9Sstevel@tonic-gate 			ip->i_uid = vap->va_uid;
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 			/*
20857c478bd9Sstevel@tonic-gate 			 * There is a real ownership change.
20867c478bd9Sstevel@tonic-gate 			 */
20877c478bd9Sstevel@tonic-gate 			if (owner_change) {
20887c478bd9Sstevel@tonic-gate 				/*
20897c478bd9Sstevel@tonic-gate 				 * Add the blocks and the file to the new
20907c478bd9Sstevel@tonic-gate 				 * user's quota.
20917c478bd9Sstevel@tonic-gate 				 */
20927c478bd9Sstevel@tonic-gate 				ip->i_dquot = getinoquota(ip);
20937c478bd9Sstevel@tonic-gate 				(void) chkdq(ip, blocks, /* force */ 1, cr,
20947c478bd9Sstevel@tonic-gate 						&errmsg1, &len1);
20957c478bd9Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ 1,
20967c478bd9Sstevel@tonic-gate 						(struct inode *)NULL,
20977c478bd9Sstevel@tonic-gate 						(uid_t)ip->i_uid,
20987c478bd9Sstevel@tonic-gate 						/* force */ 1, cr,
20997c478bd9Sstevel@tonic-gate 						&errmsg2, &len2);
21007c478bd9Sstevel@tonic-gate 			}
21017c478bd9Sstevel@tonic-gate 		}
21027c478bd9Sstevel@tonic-gate 		if (mask & AT_GID) {
21037c478bd9Sstevel@tonic-gate 			ip->i_gid = vap->va_gid;
21047c478bd9Sstevel@tonic-gate 		}
21057c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
21067c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * Change file access or modified times.
21107c478bd9Sstevel@tonic-gate 	 */
21117c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
21127c478bd9Sstevel@tonic-gate 		/* Check that the time value is within ufs range */
21137c478bd9Sstevel@tonic-gate 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
21147c478bd9Sstevel@tonic-gate 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
21157c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
21167c478bd9Sstevel@tonic-gate 			goto update_inode;
21177c478bd9Sstevel@tonic-gate 		}
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 		/*
21207c478bd9Sstevel@tonic-gate 		 * if the "noaccess" mount option is set and only atime
21217c478bd9Sstevel@tonic-gate 		 * update is requested, do nothing. No error is returned.
21227c478bd9Sstevel@tonic-gate 		 */
21237c478bd9Sstevel@tonic-gate 		if ((ufsvfsp->vfs_noatime) &&
21247c478bd9Sstevel@tonic-gate 		    ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
21257c478bd9Sstevel@tonic-gate 			goto skip_atime;
21267c478bd9Sstevel@tonic-gate 
21277c478bd9Sstevel@tonic-gate 		if (mask & AT_ATIME) {
21287c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
21297c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
21307c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~IACC;
21317c478bd9Sstevel@tonic-gate 		}
21327c478bd9Sstevel@tonic-gate 		if (mask & AT_MTIME) {
21337c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
21347c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
21357c478bd9Sstevel@tonic-gate 			gethrestime(&now);
21367c478bd9Sstevel@tonic-gate 			if (now.tv_sec > TIME32_MAX) {
21377c478bd9Sstevel@tonic-gate 				/*
21387c478bd9Sstevel@tonic-gate 				 * In 2038, ctime sticks forever..
21397c478bd9Sstevel@tonic-gate 				 */
21407c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_sec = TIME32_MAX;
21417c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_usec = 0;
21427c478bd9Sstevel@tonic-gate 			} else {
21437c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_sec = now.tv_sec;
21447c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_usec = now.tv_nsec / 1000;
21457c478bd9Sstevel@tonic-gate 			}
21467c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~(IUPD|ICHG);
21477c478bd9Sstevel@tonic-gate 			ip->i_flag |= IMODTIME;
21487c478bd9Sstevel@tonic-gate 		}
21497c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
21507c478bd9Sstevel@tonic-gate 		ip->i_flag |= IMOD;
21517c478bd9Sstevel@tonic-gate 	}
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate skip_atime:
21547c478bd9Sstevel@tonic-gate 	/*
21557c478bd9Sstevel@tonic-gate 	 * The presence of a shadow inode may indicate an ACL, but does
21567c478bd9Sstevel@tonic-gate 	 * not imply an ACL.  Future FSD types should be handled here too
21577c478bd9Sstevel@tonic-gate 	 * and check for the presence of the attribute-specific data
21587c478bd9Sstevel@tonic-gate 	 * before referencing it.
21597c478bd9Sstevel@tonic-gate 	 */
21607c478bd9Sstevel@tonic-gate 	if (ip->i_shadow) {
21617c478bd9Sstevel@tonic-gate 		/*
21627c478bd9Sstevel@tonic-gate 		 * XXX if ufs_iupdat is changed to sandbagged write fix
21637c478bd9Sstevel@tonic-gate 		 * ufs_acl_setattr to push ip to keep acls consistent
21647c478bd9Sstevel@tonic-gate 		 *
21657c478bd9Sstevel@tonic-gate 		 * Suppress out of inodes messages if we will retry.
21667c478bd9Sstevel@tonic-gate 		 */
21677c478bd9Sstevel@tonic-gate 		if (retry)
21687c478bd9Sstevel@tonic-gate 			ip->i_flag |= IQUIET;
21697c478bd9Sstevel@tonic-gate 		error = ufs_acl_setattr(ip, vap, cr);
21707c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~IQUIET;
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate update_inode:
21747c478bd9Sstevel@tonic-gate 	/*
21757c478bd9Sstevel@tonic-gate 	 * Setattr always increases the sequence number
21767c478bd9Sstevel@tonic-gate 	 */
21777c478bd9Sstevel@tonic-gate 	ip->i_seq++;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	/*
21807c478bd9Sstevel@tonic-gate 	 * if nfsd and not logging; push synchronously
21817c478bd9Sstevel@tonic-gate 	 */
21827c478bd9Sstevel@tonic-gate 	if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
21837c478bd9Sstevel@tonic-gate 		ufs_iupdat(ip, 1);
21847c478bd9Sstevel@tonic-gate 	} else {
21857c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
21897c478bd9Sstevel@tonic-gate 	if (dodqlock) {
21907c478bd9Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
21917c478bd9Sstevel@tonic-gate 	}
21927c478bd9Sstevel@tonic-gate 	if (dorwlock)
21937c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (ulp) {
21967c478bd9Sstevel@tonic-gate 		if (dotrans) {
21977c478bd9Sstevel@tonic-gate 			int terr = 0;
21987c478bd9Sstevel@tonic-gate 			TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
21997c478bd9Sstevel@tonic-gate 			    trans_size);
22007c478bd9Sstevel@tonic-gate 			if (error == 0)
22017c478bd9Sstevel@tonic-gate 				error = terr;
22027c478bd9Sstevel@tonic-gate 		}
22037c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate out:
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * If out of inodes or blocks, see if we can free something
22087c478bd9Sstevel@tonic-gate 	 * up from the delete queue.
22097c478bd9Sstevel@tonic-gate 	 */
22107c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
22117c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
22127c478bd9Sstevel@tonic-gate 		retry = 0;
22137c478bd9Sstevel@tonic-gate 		if (errmsg1 != NULL)
22147c478bd9Sstevel@tonic-gate 			kmem_free(errmsg1, len1);
22157c478bd9Sstevel@tonic-gate 		if (errmsg2 != NULL)
22167c478bd9Sstevel@tonic-gate 			kmem_free(errmsg2, len2);
22177c478bd9Sstevel@tonic-gate 		goto again;
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_SETATTR_END,
22207c478bd9Sstevel@tonic-gate 		"ufs_setattr_end:vp %p error %d", vp, error);
22217c478bd9Sstevel@tonic-gate 	if (errmsg1 != NULL) {
22227c478bd9Sstevel@tonic-gate 		uprintf(errmsg1);
22237c478bd9Sstevel@tonic-gate 		kmem_free(errmsg1, len1);
22247c478bd9Sstevel@tonic-gate 	}
22257c478bd9Sstevel@tonic-gate 	if (errmsg2 != NULL) {
22267c478bd9Sstevel@tonic-gate 		uprintf(errmsg2);
22277c478bd9Sstevel@tonic-gate 		kmem_free(errmsg2, len2);
22287c478bd9Sstevel@tonic-gate 	}
22297c478bd9Sstevel@tonic-gate 	return (error);
22307c478bd9Sstevel@tonic-gate }
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
22337c478bd9Sstevel@tonic-gate static int
22347c478bd9Sstevel@tonic-gate ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr)
22357c478bd9Sstevel@tonic-gate {
22367c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
22377c478bd9Sstevel@tonic-gate 	int error;
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_ACCESS_START,
22407c478bd9Sstevel@tonic-gate 		"ufs_access_start:vp %p mode %x flags %x", vp, mode, flags);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
22437c478bd9Sstevel@tonic-gate 		return (EIO);
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	/*
22487c478bd9Sstevel@tonic-gate 	 * The ufs_iaccess function wants to be called with
22497c478bd9Sstevel@tonic-gate 	 * mode bits expressed as "ufs specific" bits.
22507c478bd9Sstevel@tonic-gate 	 * I.e., VWRITE|VREAD|VEXEC do not make sense to
22517c478bd9Sstevel@tonic-gate 	 * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
22527c478bd9Sstevel@tonic-gate 	 * But since they're the same we just pass the vnode mode
22537c478bd9Sstevel@tonic-gate 	 * bit but just verify that assumption at compile time.
22547c478bd9Sstevel@tonic-gate 	 */
22557c478bd9Sstevel@tonic-gate #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
22567c478bd9Sstevel@tonic-gate #error "ufs_access needs to map Vmodes to Imodes"
22577c478bd9Sstevel@tonic-gate #endif
22587c478bd9Sstevel@tonic-gate 	error = ufs_iaccess(ip, mode, cr);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_ACCESS_END,
22637c478bd9Sstevel@tonic-gate 		"ufs_access_end:vp %p error %d", vp, error);
22647c478bd9Sstevel@tonic-gate 	return (error);
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /* ARGSUSED */
22687c478bd9Sstevel@tonic-gate static int
22697c478bd9Sstevel@tonic-gate ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr)
22707c478bd9Sstevel@tonic-gate {
22717c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
22727c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
22737c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
22747c478bd9Sstevel@tonic-gate 	int error;
22757c478bd9Sstevel@tonic-gate 	int fastsymlink;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_READLINK_START,
22787c478bd9Sstevel@tonic-gate 		"ufs_readlink_start:vp %p uiop %p", uiop, vp);
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK) {
22817c478bd9Sstevel@tonic-gate 		error = EINVAL;
22827c478bd9Sstevel@tonic-gate 		goto nolockout;
22837c478bd9Sstevel@tonic-gate 	}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	/*
22867c478bd9Sstevel@tonic-gate 	 * If the symbolic link is empty there is nothing to read.
22877c478bd9Sstevel@tonic-gate 	 * Fast-track these empty symbolic links
22887c478bd9Sstevel@tonic-gate 	 */
22897c478bd9Sstevel@tonic-gate 	if (ip->i_size == 0) {
22907c478bd9Sstevel@tonic-gate 		error = 0;
22917c478bd9Sstevel@tonic-gate 		goto nolockout;
22927c478bd9Sstevel@tonic-gate 	}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
22957c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
22967c478bd9Sstevel@tonic-gate 	if (error)
22977c478bd9Sstevel@tonic-gate 		goto nolockout;
22987c478bd9Sstevel@tonic-gate 	/*
22997c478bd9Sstevel@tonic-gate 	 * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
23007c478bd9Sstevel@tonic-gate 	 */
23017c478bd9Sstevel@tonic-gate again:
23027c478bd9Sstevel@tonic-gate 	fastsymlink = 0;
23037c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IFASTSYMLNK) {
23047c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
23057c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
23067c478bd9Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
23077c478bd9Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
23087c478bd9Sstevel@tonic-gate 			    (ip->i_fs->fs_ronly == 0) &&
23097c478bd9Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
23107c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
23117c478bd9Sstevel@tonic-gate 				ip->i_flag |= IACC;
23127c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
23137c478bd9Sstevel@tonic-gate 			}
23147c478bd9Sstevel@tonic-gate 			error = uiomove((caddr_t)&ip->i_db[1],
23157c478bd9Sstevel@tonic-gate 				MIN(ip->i_size, uiop->uio_resid),
23167c478bd9Sstevel@tonic-gate 				UIO_READ, uiop);
23177c478bd9Sstevel@tonic-gate 			ITIMES(ip);
23187c478bd9Sstevel@tonic-gate 			++fastsymlink;
23197c478bd9Sstevel@tonic-gate 		}
23207c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
23217c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 	if (!fastsymlink) {
23247c478bd9Sstevel@tonic-gate 		ssize_t size;	/* number of bytes read  */
23257c478bd9Sstevel@tonic-gate 		caddr_t basep;	/* pointer to input data */
23267c478bd9Sstevel@tonic-gate 		ino_t ino;
23277c478bd9Sstevel@tonic-gate 		long  igen;
23287c478bd9Sstevel@tonic-gate 		struct uio tuio;	/* temp uio struct */
23297c478bd9Sstevel@tonic-gate 		struct uio *tuiop;
23307c478bd9Sstevel@tonic-gate 		iovec_t tiov;		/* temp iovec struct */
23317c478bd9Sstevel@tonic-gate 		char kbuf[FSL_SIZE];	/* buffer to hold fast symlink */
23327c478bd9Sstevel@tonic-gate 		int tflag = 0;		/* flag to indicate temp vars used */
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 		ino = ip->i_number;
23357c478bd9Sstevel@tonic-gate 		igen = ip->i_gen;
23367c478bd9Sstevel@tonic-gate 		size = uiop->uio_resid;
23377c478bd9Sstevel@tonic-gate 		basep = uiop->uio_iov->iov_base;
23387c478bd9Sstevel@tonic-gate 		tuiop = uiop;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
23417c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
23427c478bd9Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
23437c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
23447c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
23457c478bd9Sstevel@tonic-gate 			goto again;
23467c478bd9Sstevel@tonic-gate 		}
23477c478bd9Sstevel@tonic-gate 
23487c478bd9Sstevel@tonic-gate 		/* can this be a fast symlink and is it a user buffer? */
23497c478bd9Sstevel@tonic-gate 		if (ip->i_size <= FSL_SIZE &&
23507c478bd9Sstevel@tonic-gate 		    (uiop->uio_segflg == UIO_USERSPACE ||
23517c478bd9Sstevel@tonic-gate 		    uiop->uio_segflg == UIO_USERISPACE)) {
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 			bzero(&tuio, sizeof (struct uio));
23547c478bd9Sstevel@tonic-gate 			/*
23557c478bd9Sstevel@tonic-gate 			 * setup a kernel buffer to read link into.  this
23567c478bd9Sstevel@tonic-gate 			 * is to fix a race condition where the user buffer
23577c478bd9Sstevel@tonic-gate 			 * got corrupted before copying it into the inode.
23587c478bd9Sstevel@tonic-gate 			 */
23597c478bd9Sstevel@tonic-gate 			size = ip->i_size;
23607c478bd9Sstevel@tonic-gate 			tiov.iov_len = size;
23617c478bd9Sstevel@tonic-gate 			tiov.iov_base = kbuf;
23627c478bd9Sstevel@tonic-gate 			tuio.uio_iov = &tiov;
23637c478bd9Sstevel@tonic-gate 			tuio.uio_iovcnt = 1;
23647c478bd9Sstevel@tonic-gate 			tuio.uio_offset = uiop->uio_offset;
23657c478bd9Sstevel@tonic-gate 			tuio.uio_segflg = UIO_SYSSPACE;
23667c478bd9Sstevel@tonic-gate 			tuio.uio_fmode = uiop->uio_fmode;
23677c478bd9Sstevel@tonic-gate 			tuio.uio_extflg = uiop->uio_extflg;
23687c478bd9Sstevel@tonic-gate 			tuio.uio_limit = uiop->uio_limit;
23697c478bd9Sstevel@tonic-gate 			tuio.uio_resid = size;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 			basep = tuio.uio_iov->iov_base;
23727c478bd9Sstevel@tonic-gate 			tuiop = &tuio;
23737c478bd9Sstevel@tonic-gate 			tflag = 1;
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 
23767c478bd9Sstevel@tonic-gate 		error = rdip(ip, tuiop, 0, cr);
23777c478bd9Sstevel@tonic-gate 		if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
23787c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
23797c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
23807c478bd9Sstevel@tonic-gate 			goto out;
23817c478bd9Sstevel@tonic-gate 		}
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 		if (tflag == 0)
23847c478bd9Sstevel@tonic-gate 			size -= uiop->uio_resid;
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
23877c478bd9Sstevel@tonic-gate 		    ip->i_size == size) || (tflag == 1 &&
23887c478bd9Sstevel@tonic-gate 		    tuio.uio_resid == 0)) {
23897c478bd9Sstevel@tonic-gate 			error = kcopy(basep, &ip->i_db[1], ip->i_size);
23907c478bd9Sstevel@tonic-gate 			if (error == 0) {
23917c478bd9Sstevel@tonic-gate 				ip->i_flag |= IFASTSYMLNK;
23927c478bd9Sstevel@tonic-gate 				/*
23937c478bd9Sstevel@tonic-gate 				 * free page
23947c478bd9Sstevel@tonic-gate 				 */
23957c478bd9Sstevel@tonic-gate 				(void) VOP_PUTPAGE(ITOV(ip),
23967c478bd9Sstevel@tonic-gate 				    (offset_t)0, PAGESIZE,
23977c478bd9Sstevel@tonic-gate 				    (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
23987c478bd9Sstevel@tonic-gate 				    cr);
23997c478bd9Sstevel@tonic-gate 			} else {
24007c478bd9Sstevel@tonic-gate 				int i;
24017c478bd9Sstevel@tonic-gate 				/* error, clear garbage left behind */
24027c478bd9Sstevel@tonic-gate 				for (i = 1; i < NDADDR; i++)
24037c478bd9Sstevel@tonic-gate 					ip->i_db[i] = 0;
24047c478bd9Sstevel@tonic-gate 				for (i = 0; i < NIADDR; i++)
24057c478bd9Sstevel@tonic-gate 					ip->i_ib[i] = 0;
24067c478bd9Sstevel@tonic-gate 			}
24077c478bd9Sstevel@tonic-gate 		}
24087c478bd9Sstevel@tonic-gate 		if (tflag == 1) {
24097c478bd9Sstevel@tonic-gate 			/* now, copy it into the user buffer */
24107c478bd9Sstevel@tonic-gate 			error = uiomove((caddr_t)kbuf,
24117c478bd9Sstevel@tonic-gate 				MIN(size, uiop->uio_resid),
24127c478bd9Sstevel@tonic-gate 				UIO_READ, uiop);
24137c478bd9Sstevel@tonic-gate 		}
24147c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
24157c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
24167c478bd9Sstevel@tonic-gate 	}
24177c478bd9Sstevel@tonic-gate out:
24187c478bd9Sstevel@tonic-gate 	if (ulp) {
24197c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
24207c478bd9Sstevel@tonic-gate 	}
24217c478bd9Sstevel@tonic-gate nolockout:
24227c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_READLINK_END,
24237c478bd9Sstevel@tonic-gate 		"ufs_readlink_end:vp %p error %d", vp, error);
24247c478bd9Sstevel@tonic-gate 
24257c478bd9Sstevel@tonic-gate 	return (error);
24267c478bd9Sstevel@tonic-gate }
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate /* ARGSUSED */
24297c478bd9Sstevel@tonic-gate static int
24307c478bd9Sstevel@tonic-gate ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr)
24317c478bd9Sstevel@tonic-gate {
24327c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
24337c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
24347c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
24357c478bd9Sstevel@tonic-gate 	int error;
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_FSYNC_START,
24387c478bd9Sstevel@tonic-gate 		"ufs_fsync_start:vp %p", vp);
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
24417c478bd9Sstevel@tonic-gate 	if (error)
24427c478bd9Sstevel@tonic-gate 		return (error);
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
24457c478bd9Sstevel@tonic-gate 		/*
24467c478bd9Sstevel@tonic-gate 		 * First push out any data pages
24477c478bd9Sstevel@tonic-gate 		 */
24487c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
24497c478bd9Sstevel@tonic-gate 		    (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
24507c478bd9Sstevel@tonic-gate 			error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
24517c478bd9Sstevel@tonic-gate 			    0, CRED());
24527c478bd9Sstevel@tonic-gate 			if (error)
24537c478bd9Sstevel@tonic-gate 				goto out;
24547c478bd9Sstevel@tonic-gate 		}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 		/*
24577c478bd9Sstevel@tonic-gate 		 * Delta any delayed inode times updates
24587c478bd9Sstevel@tonic-gate 		 * and push inode to log.
24597c478bd9Sstevel@tonic-gate 		 * All other inode deltas will have already been delta'd
24607c478bd9Sstevel@tonic-gate 		 * and will be pushed during the commit.
24617c478bd9Sstevel@tonic-gate 		 */
24627c478bd9Sstevel@tonic-gate 		if (!(syncflag & FDSYNC) &&
24637c478bd9Sstevel@tonic-gate 		    ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
24647c478bd9Sstevel@tonic-gate 			if (ulp) {
24657c478bd9Sstevel@tonic-gate 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
24667c478bd9Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
24677c478bd9Sstevel@tonic-gate 			}
24687c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
24697c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
24707c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~IMODTIME;
24717c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
24727c478bd9Sstevel@tonic-gate 			ufs_iupdat(ip, I_SYNC);
24737c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
24747c478bd9Sstevel@tonic-gate 			if (ulp) {
24757c478bd9Sstevel@tonic-gate 				TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
24767c478bd9Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
24777c478bd9Sstevel@tonic-gate 			}
24787c478bd9Sstevel@tonic-gate 		}
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 		/*
24817c478bd9Sstevel@tonic-gate 		 * Commit the Moby transaction
24827c478bd9Sstevel@tonic-gate 		 *
24837c478bd9Sstevel@tonic-gate 		 * Deltas have already been made so we just need to
24847c478bd9Sstevel@tonic-gate 		 * commit them with a synchronous transaction.
24857c478bd9Sstevel@tonic-gate 		 * TRANS_BEGIN_SYNC() will return an error
24867c478bd9Sstevel@tonic-gate 		 * if there are no deltas to commit, for an
24877c478bd9Sstevel@tonic-gate 		 * empty transaction.
24887c478bd9Sstevel@tonic-gate 		 */
24897c478bd9Sstevel@tonic-gate 		if (ulp) {
24907c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
24917c478bd9Sstevel@tonic-gate 			    error);
24927c478bd9Sstevel@tonic-gate 			if (error) {
24937c478bd9Sstevel@tonic-gate 				error = 0; /* commit wasn't needed */
24947c478bd9Sstevel@tonic-gate 				goto out;
24957c478bd9Sstevel@tonic-gate 			}
24967c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
24977c478bd9Sstevel@tonic-gate 			    TOP_COMMIT_SIZE);
24987c478bd9Sstevel@tonic-gate 		}
24997c478bd9Sstevel@tonic-gate 	} else {	/* not logging */
25007c478bd9Sstevel@tonic-gate 		if (!(IS_SWAPVP(vp)))
25017c478bd9Sstevel@tonic-gate 			if (syncflag & FNODSYNC) {
25027c478bd9Sstevel@tonic-gate 				/* Just update the inode only */
25037c478bd9Sstevel@tonic-gate 				TRANS_IUPDAT(ip, 1);
25047c478bd9Sstevel@tonic-gate 				error = 0;
25057c478bd9Sstevel@tonic-gate 			} else if (syncflag & FDSYNC)
25067c478bd9Sstevel@tonic-gate 				/* Do data-synchronous writes */
25077c478bd9Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
25087c478bd9Sstevel@tonic-gate 			else
25097c478bd9Sstevel@tonic-gate 				/* Do synchronous writes */
25107c478bd9Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
25137c478bd9Sstevel@tonic-gate 		if (!error)
25147c478bd9Sstevel@tonic-gate 			error = ufs_sync_indir(ip);
25157c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
25167c478bd9Sstevel@tonic-gate 	}
25177c478bd9Sstevel@tonic-gate out:
25187c478bd9Sstevel@tonic-gate 	if (ulp) {
25197c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
25207c478bd9Sstevel@tonic-gate 	}
25217c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_FSYNC_END,
25227c478bd9Sstevel@tonic-gate 		"ufs_fsync_end:vp %p error %d", vp, error);
25237c478bd9Sstevel@tonic-gate 	return (error);
25247c478bd9Sstevel@tonic-gate }
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25277c478bd9Sstevel@tonic-gate static void
25287c478bd9Sstevel@tonic-gate ufs_inactive(struct vnode *vp, struct cred *cr)
25297c478bd9Sstevel@tonic-gate {
25307c478bd9Sstevel@tonic-gate 	ufs_iinactive(VTOI(vp));
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate /*
25347c478bd9Sstevel@tonic-gate  * Unix file system operations having to do with directory manipulation.
25357c478bd9Sstevel@tonic-gate  */
25367c478bd9Sstevel@tonic-gate int ufs_lookup_idle_count = 2;	/* Number of inodes to idle each time */
25377c478bd9Sstevel@tonic-gate /* ARGSUSED */
25387c478bd9Sstevel@tonic-gate static int
25397c478bd9Sstevel@tonic-gate ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
25407c478bd9Sstevel@tonic-gate 	struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	struct inode *ip;
25437c478bd9Sstevel@tonic-gate 	struct inode *sip;
25447c478bd9Sstevel@tonic-gate 	struct inode *xip;
25457c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
25467c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
25477c478bd9Sstevel@tonic-gate 	struct vnode *vp;
25487c478bd9Sstevel@tonic-gate 	int error;
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_LOOKUP_START,
25517c478bd9Sstevel@tonic-gate 		"ufs_lookup_start:dvp %p name %s", dvp, nm);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 	/*
25557c478bd9Sstevel@tonic-gate 	 * Check flags for type of lookup (regular file or attribute file)
25567c478bd9Sstevel@tonic-gate 	 */
25577c478bd9Sstevel@tonic-gate 
25587c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 		/*
25637c478bd9Sstevel@tonic-gate 		 * We don't allow recursive attributes...
25647c478bd9Sstevel@tonic-gate 		 * Maybe someday we will.
25657c478bd9Sstevel@tonic-gate 		 */
25667c478bd9Sstevel@tonic-gate 		if ((ip->i_cflags & IXATTR)) {
25677c478bd9Sstevel@tonic-gate 			return (EINVAL);
25687c478bd9Sstevel@tonic-gate 		}
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate 		if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
25717c478bd9Sstevel@tonic-gate 			error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
25727c478bd9Sstevel@tonic-gate 			if (error) {
25737c478bd9Sstevel@tonic-gate 				*vpp = NULL;
25747c478bd9Sstevel@tonic-gate 				goto out;
25757c478bd9Sstevel@tonic-gate 			}
25767c478bd9Sstevel@tonic-gate 
25777c478bd9Sstevel@tonic-gate 			vp = ITOV(sip);
25787c478bd9Sstevel@tonic-gate 			dnlc_update(dvp, XATTR_DIR_NAME, vp);
25797c478bd9Sstevel@tonic-gate 		}
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 		/*
25827c478bd9Sstevel@tonic-gate 		 * Check accessibility of directory.
25837c478bd9Sstevel@tonic-gate 		 */
25847c478bd9Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
25857c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
25867c478bd9Sstevel@tonic-gate 			error = ENOENT;
25877c478bd9Sstevel@tonic-gate 			goto out;
25887c478bd9Sstevel@tonic-gate 		}
25897c478bd9Sstevel@tonic-gate 		if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr)) != 0) {
25907c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
25917c478bd9Sstevel@tonic-gate 			goto out;
25927c478bd9Sstevel@tonic-gate 		}
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate 		*vpp = vp;
25957c478bd9Sstevel@tonic-gate 		return (0);
25967c478bd9Sstevel@tonic-gate 	}
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	/*
25997c478bd9Sstevel@tonic-gate 	 * Check for a null component, which we should treat as
26007c478bd9Sstevel@tonic-gate 	 * looking at dvp from within it's parent, so we don't
26017c478bd9Sstevel@tonic-gate 	 * need a call to ufs_iaccess(), as it has already been
26027c478bd9Sstevel@tonic-gate 	 * done.
26037c478bd9Sstevel@tonic-gate 	 */
26047c478bd9Sstevel@tonic-gate 	if (nm[0] == 0) {
26057c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
26067c478bd9Sstevel@tonic-gate 		error = 0;
26077c478bd9Sstevel@tonic-gate 		*vpp = dvp;
26087c478bd9Sstevel@tonic-gate 		goto out;
26097c478bd9Sstevel@tonic-gate 	}
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate 	/*
26127c478bd9Sstevel@tonic-gate 	 * Check for "." ie itself. this is a quick check and
26137c478bd9Sstevel@tonic-gate 	 * avoids adding "." into the dnlc (which have been seen
26147c478bd9Sstevel@tonic-gate 	 * to occupy >10% of the cache).
26157c478bd9Sstevel@tonic-gate 	 */
26167c478bd9Sstevel@tonic-gate 	if ((nm[0] == '.') && (nm[1] == 0)) {
26177c478bd9Sstevel@tonic-gate 		/*
26187c478bd9Sstevel@tonic-gate 		 * Don't return without checking accessibility
26197c478bd9Sstevel@tonic-gate 		 * of the directory. We only need the lock if
26207c478bd9Sstevel@tonic-gate 		 * we are going to return it.
26217c478bd9Sstevel@tonic-gate 		 */
26227c478bd9Sstevel@tonic-gate 		if ((error = ufs_iaccess(ip, IEXEC, cr)) == 0) {
26237c478bd9Sstevel@tonic-gate 			VN_HOLD(dvp);
26247c478bd9Sstevel@tonic-gate 			*vpp = dvp;
26257c478bd9Sstevel@tonic-gate 		}
26267c478bd9Sstevel@tonic-gate 		goto out;
26277c478bd9Sstevel@tonic-gate 	}
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	/*
26307c478bd9Sstevel@tonic-gate 	 * Fast path: Check the directory name lookup cache.
26317c478bd9Sstevel@tonic-gate 	 */
26327c478bd9Sstevel@tonic-gate 	if (vp = dnlc_lookup(dvp, nm)) {
26337c478bd9Sstevel@tonic-gate 		/*
26347c478bd9Sstevel@tonic-gate 		 * Check accessibility of directory.
26357c478bd9Sstevel@tonic-gate 		 */
26367c478bd9Sstevel@tonic-gate 		if ((error = ufs_iaccess(ip, IEXEC, cr)) != 0) {
26377c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
26387c478bd9Sstevel@tonic-gate 			goto out;
26397c478bd9Sstevel@tonic-gate 		}
26407c478bd9Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
26417c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
26427c478bd9Sstevel@tonic-gate 			error = ENOENT;
26437c478bd9Sstevel@tonic-gate 			goto out;
26447c478bd9Sstevel@tonic-gate 		}
26457c478bd9Sstevel@tonic-gate 		xip = VTOI(vp);
26467c478bd9Sstevel@tonic-gate 		ulp = NULL;
26477c478bd9Sstevel@tonic-gate 		goto fastpath;
26487c478bd9Sstevel@tonic-gate 	}
26497c478bd9Sstevel@tonic-gate 
26507c478bd9Sstevel@tonic-gate 	/*
26517c478bd9Sstevel@tonic-gate 	 * Keep the idle queue from getting too long by
26527c478bd9Sstevel@tonic-gate 	 * idling two inodes before attempting to allocate another.
26537c478bd9Sstevel@tonic-gate 	 *    This operation must be performed before entering
26547c478bd9Sstevel@tonic-gate 	 *    lockfs or a transaction.
26557c478bd9Sstevel@tonic-gate 	 */
26567c478bd9Sstevel@tonic-gate 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
26577c478bd9Sstevel@tonic-gate 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
26587c478bd9Sstevel@tonic-gate 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
26597c478bd9Sstevel@tonic-gate 			ufs_idle_some(ufs_lookup_idle_count);
26607c478bd9Sstevel@tonic-gate 		}
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
26637c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
26647c478bd9Sstevel@tonic-gate 	if (error)
26657c478bd9Sstevel@tonic-gate 		goto out;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	error = ufs_dirlook(ip, nm, &xip, cr, 1);
26687c478bd9Sstevel@tonic-gate 
26697c478bd9Sstevel@tonic-gate fastpath:
26707c478bd9Sstevel@tonic-gate 	if (error == 0) {
26717c478bd9Sstevel@tonic-gate 		ip = xip;
26727c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 		/*
26757c478bd9Sstevel@tonic-gate 		 * If vnode is a device return special vnode instead.
26767c478bd9Sstevel@tonic-gate 		 */
26777c478bd9Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
26787c478bd9Sstevel@tonic-gate 			struct vnode *newvp;
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
26817c478bd9Sstevel@tonic-gate 			    cr);
26827c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
26837c478bd9Sstevel@tonic-gate 			if (newvp == NULL)
26847c478bd9Sstevel@tonic-gate 				error = ENOSYS;
26857c478bd9Sstevel@tonic-gate 			else
26867c478bd9Sstevel@tonic-gate 				*vpp = newvp;
26877c478bd9Sstevel@tonic-gate 		}
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 	if (ulp) {
26907c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
26917c478bd9Sstevel@tonic-gate 	}
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate out:
26947c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_LOOKUP_END,
26957c478bd9Sstevel@tonic-gate 		"ufs_lookup_end:dvp %p name %s error %d", vpp, nm, error);
26967c478bd9Sstevel@tonic-gate 	return (error);
26977c478bd9Sstevel@tonic-gate }
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate static int
27007c478bd9Sstevel@tonic-gate ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
27017c478bd9Sstevel@tonic-gate 	int mode, struct vnode **vpp, struct cred *cr, int flag)
27027c478bd9Sstevel@tonic-gate {
27037c478bd9Sstevel@tonic-gate 	struct inode *ip;
27047c478bd9Sstevel@tonic-gate 	struct inode *xip;
27057c478bd9Sstevel@tonic-gate 	struct inode *dip;
27067c478bd9Sstevel@tonic-gate 	struct vnode *xvp;
27077c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
27087c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
27097c478bd9Sstevel@tonic-gate 	int error;
27107c478bd9Sstevel@tonic-gate 	int issync;
27117c478bd9Sstevel@tonic-gate 	int truncflag;
27127c478bd9Sstevel@tonic-gate 	int trans_size;
27137c478bd9Sstevel@tonic-gate 	int noentry;
27147c478bd9Sstevel@tonic-gate 	int defer_dip_seq_update = 0;	/* need to defer update of dip->i_seq */
27157c478bd9Sstevel@tonic-gate 	int retry = 1;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_CREATE_START,
27187c478bd9Sstevel@tonic-gate 		"ufs_create_start:dvp %p", dvp);
27197c478bd9Sstevel@tonic-gate 
27207c478bd9Sstevel@tonic-gate again:
27217c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
27227c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
27237c478bd9Sstevel@tonic-gate 	truncflag = 0;
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
27267c478bd9Sstevel@tonic-gate 	if (error)
27277c478bd9Sstevel@tonic-gate 		goto out;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	if (ulp) {
27307c478bd9Sstevel@tonic-gate 		trans_size = (int)TOP_CREATE_SIZE(ip);
27317c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
27327c478bd9Sstevel@tonic-gate 	}
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
27357c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
27367c478bd9Sstevel@tonic-gate 
27377c478bd9Sstevel@tonic-gate 	if (*name == '\0') {
27387c478bd9Sstevel@tonic-gate 		/*
27397c478bd9Sstevel@tonic-gate 		 * Null component name refers to the directory itself.
27407c478bd9Sstevel@tonic-gate 		 */
27417c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
27427c478bd9Sstevel@tonic-gate 		/*
27437c478bd9Sstevel@tonic-gate 		 * Even though this is an error case, we need to grab the
27447c478bd9Sstevel@tonic-gate 		 * quota lock since the error handling code below is common.
27457c478bd9Sstevel@tonic-gate 		 */
27467c478bd9Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
27477c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
27487c478bd9Sstevel@tonic-gate 		error = EEXIST;
27497c478bd9Sstevel@tonic-gate 	} else {
27507c478bd9Sstevel@tonic-gate 		xip = NULL;
27517c478bd9Sstevel@tonic-gate 		noentry = 0;
27527c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
27537c478bd9Sstevel@tonic-gate 		xvp = dnlc_lookup(dvp, name);
27547c478bd9Sstevel@tonic-gate 		if (xvp == DNLC_NO_VNODE) {
27557c478bd9Sstevel@tonic-gate 			noentry = 1;
27567c478bd9Sstevel@tonic-gate 			VN_RELE(xvp);
27577c478bd9Sstevel@tonic-gate 			xvp = NULL;
27587c478bd9Sstevel@tonic-gate 		}
27597c478bd9Sstevel@tonic-gate 		if (xvp) {
27607c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
27617c478bd9Sstevel@tonic-gate 			if (error = ufs_iaccess(ip, IEXEC, cr)) {
27627c478bd9Sstevel@tonic-gate 				VN_RELE(xvp);
27637c478bd9Sstevel@tonic-gate 			} else {
27647c478bd9Sstevel@tonic-gate 				error = EEXIST;
27657c478bd9Sstevel@tonic-gate 				xip = VTOI(xvp);
27667c478bd9Sstevel@tonic-gate 			}
27677c478bd9Sstevel@tonic-gate 		} else {
27687c478bd9Sstevel@tonic-gate 			/*
27697c478bd9Sstevel@tonic-gate 			 * Suppress file system full message if we will retry
27707c478bd9Sstevel@tonic-gate 			 */
27717c478bd9Sstevel@tonic-gate 			error = ufs_direnter_cm(ip, name, DE_CREATE,
27727c478bd9Sstevel@tonic-gate 				vap, &xip, cr,
27737c478bd9Sstevel@tonic-gate 				(noentry | (retry ? IQUIET : 0)));
27747c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
27757c478bd9Sstevel@tonic-gate 		}
27767c478bd9Sstevel@tonic-gate 		ip = xip;
27777c478bd9Sstevel@tonic-gate 		if (ip != NULL) {
27787c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
27797c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
27807c478bd9Sstevel@tonic-gate 		}
27817c478bd9Sstevel@tonic-gate 	}
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	/*
27847c478bd9Sstevel@tonic-gate 	 * If the file already exists and this is a non-exclusive create,
27857c478bd9Sstevel@tonic-gate 	 * check permissions and allow access for non-directories.
27867c478bd9Sstevel@tonic-gate 	 * Read-only create of an existing directory is also allowed.
27877c478bd9Sstevel@tonic-gate 	 * We fail an exclusive create of anything which already exists.
27887c478bd9Sstevel@tonic-gate 	 */
27897c478bd9Sstevel@tonic-gate 	if (error == EEXIST) {
27907c478bd9Sstevel@tonic-gate 		dip = VTOI(dvp);
27917c478bd9Sstevel@tonic-gate 		if (excl == NONEXCL) {
27927c478bd9Sstevel@tonic-gate 			if ((((ip->i_mode & IFMT) == IFDIR) ||
27937c478bd9Sstevel@tonic-gate 			    ((ip->i_mode & IFMT) == IFATTRDIR)) &&
27947c478bd9Sstevel@tonic-gate 			    (mode & IWRITE))
27957c478bd9Sstevel@tonic-gate 				error = EISDIR;
27967c478bd9Sstevel@tonic-gate 			else if (mode)
27977c478bd9Sstevel@tonic-gate 				error = ufs_iaccess(ip, mode, cr);
27987c478bd9Sstevel@tonic-gate 			else
27997c478bd9Sstevel@tonic-gate 				error = 0;
28007c478bd9Sstevel@tonic-gate 		}
28017c478bd9Sstevel@tonic-gate 		if (error) {
28027c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
28037c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
28047c478bd9Sstevel@tonic-gate 			VN_RELE(ITOV(ip));
28057c478bd9Sstevel@tonic-gate 			goto unlock;
28067c478bd9Sstevel@tonic-gate 		}
28077c478bd9Sstevel@tonic-gate 		/*
28087c478bd9Sstevel@tonic-gate 		 * If the error EEXIST was set, then i_seq can not
28097c478bd9Sstevel@tonic-gate 		 * have been updated. The sequence number interface
28107c478bd9Sstevel@tonic-gate 		 * is defined such that a non-error VOP_CREATE must
28117c478bd9Sstevel@tonic-gate 		 * increase the dir va_seq it by at least one. If we
28127c478bd9Sstevel@tonic-gate 		 * have cleared the error, increase i_seq. Note that
28137c478bd9Sstevel@tonic-gate 		 * we are increasing the dir i_seq and in rare cases
28147c478bd9Sstevel@tonic-gate 		 * ip may actually be from the dvp, so we already have
28157c478bd9Sstevel@tonic-gate 		 * the locks and it will not be subject to truncation.
28167c478bd9Sstevel@tonic-gate 		 * In case we have to update i_seq of the parent
28177c478bd9Sstevel@tonic-gate 		 * directory dip, we have to defer it till we have
28187c478bd9Sstevel@tonic-gate 		 * released our locks on ip due to lock ordering requirements.
28197c478bd9Sstevel@tonic-gate 		 */
28207c478bd9Sstevel@tonic-gate 		if (ip != dip)
28217c478bd9Sstevel@tonic-gate 			defer_dip_seq_update = 1;
28227c478bd9Sstevel@tonic-gate 		else
28237c478bd9Sstevel@tonic-gate 			ip->i_seq++;
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 		if (((ip->i_mode & IFMT) == IFREG) &&
28267c478bd9Sstevel@tonic-gate 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
28277c478bd9Sstevel@tonic-gate 			/*
28287c478bd9Sstevel@tonic-gate 			 * Truncate regular files, if requested by caller.
28297c478bd9Sstevel@tonic-gate 			 * Grab i_rwlock to make sure no one else is
28307c478bd9Sstevel@tonic-gate 			 * currently writing to the file (we promised
28317c478bd9Sstevel@tonic-gate 			 * bmap we would do this).
28327c478bd9Sstevel@tonic-gate 			 * Must get the locks in the correct order.
28337c478bd9Sstevel@tonic-gate 			 */
28347c478bd9Sstevel@tonic-gate 			if (ip->i_size == 0) {
28357c478bd9Sstevel@tonic-gate 				ip->i_flag |= ICHG | IUPD;
28367c478bd9Sstevel@tonic-gate 				ip->i_seq++;
28377c478bd9Sstevel@tonic-gate 				TRANS_INODE(ufsvfsp, ip);
28387c478bd9Sstevel@tonic-gate 			} else {
28397c478bd9Sstevel@tonic-gate 				/*
28407c478bd9Sstevel@tonic-gate 				 * Large Files: Why this check here?
28417c478bd9Sstevel@tonic-gate 				 * Though we do it in vn_create() we really
28427c478bd9Sstevel@tonic-gate 				 * want to guarantee that we do not destroy
28437c478bd9Sstevel@tonic-gate 				 * Large file data by atomically checking
28447c478bd9Sstevel@tonic-gate 				 * the size while holding the contents
28457c478bd9Sstevel@tonic-gate 				 * lock.
28467c478bd9Sstevel@tonic-gate 				 */
28477c478bd9Sstevel@tonic-gate 				if (flag && !(flag & FOFFMAX) &&
28487c478bd9Sstevel@tonic-gate 				    ((ip->i_mode & IFMT) == IFREG) &&
28497c478bd9Sstevel@tonic-gate 				    (ip->i_size > (offset_t)MAXOFF32_T)) {
28507c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
28517c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
28527c478bd9Sstevel@tonic-gate 					error = EOVERFLOW;
28537c478bd9Sstevel@tonic-gate 					goto unlock;
28547c478bd9Sstevel@tonic-gate 				}
28557c478bd9Sstevel@tonic-gate 				if (TRANS_ISTRANS(ufsvfsp))
28567c478bd9Sstevel@tonic-gate 					truncflag++;
28577c478bd9Sstevel@tonic-gate 				else {
28587c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
28597c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
28607c478bd9Sstevel@tonic-gate 					rw_enter(&ip->i_rwlock, RW_WRITER);
28617c478bd9Sstevel@tonic-gate 					rw_enter(&ufsvfsp->vfs_dqrwlock,
28627c478bd9Sstevel@tonic-gate 							RW_READER);
28637c478bd9Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
28647c478bd9Sstevel@tonic-gate 					(void) ufs_itrunc(ip, (u_offset_t)0, 0,
28657c478bd9Sstevel@tonic-gate 								cr);
28667c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_rwlock);
28677c478bd9Sstevel@tonic-gate 				}
28687c478bd9Sstevel@tonic-gate 			}
28697c478bd9Sstevel@tonic-gate 		}
28707c478bd9Sstevel@tonic-gate 	}
28717c478bd9Sstevel@tonic-gate 
28727c478bd9Sstevel@tonic-gate 	if (error) {
28737c478bd9Sstevel@tonic-gate 		if (ip != NULL) {
28747c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
28757c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
28767c478bd9Sstevel@tonic-gate 		}
28777c478bd9Sstevel@tonic-gate 		goto unlock;
28787c478bd9Sstevel@tonic-gate 	}
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 	*vpp = ITOV(ip);
28817c478bd9Sstevel@tonic-gate 	ITIMES(ip);
28827c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
28837c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	/*
28867c478bd9Sstevel@tonic-gate 	 * If vnode is a device return special vnode instead.
28877c478bd9Sstevel@tonic-gate 	 */
28887c478bd9Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
28897c478bd9Sstevel@tonic-gate 		struct vnode *newvp;
28907c478bd9Sstevel@tonic-gate 
28917c478bd9Sstevel@tonic-gate 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
28927c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
28937c478bd9Sstevel@tonic-gate 		if (newvp == NULL) {
28947c478bd9Sstevel@tonic-gate 			error = ENOSYS;
28957c478bd9Sstevel@tonic-gate 			goto unlock;
28967c478bd9Sstevel@tonic-gate 		}
28977c478bd9Sstevel@tonic-gate 		truncflag = 0;
28987c478bd9Sstevel@tonic-gate 		*vpp = newvp;
28997c478bd9Sstevel@tonic-gate 	}
29007c478bd9Sstevel@tonic-gate unlock:
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 	/*
29037c478bd9Sstevel@tonic-gate 	 * Do the deferred update of the parent directory's sequence
29047c478bd9Sstevel@tonic-gate 	 * number now.
29057c478bd9Sstevel@tonic-gate 	 */
29067c478bd9Sstevel@tonic-gate 	if (defer_dip_seq_update == 1) {
29077c478bd9Sstevel@tonic-gate 		rw_enter(&dip->i_contents, RW_READER);
29087c478bd9Sstevel@tonic-gate 		mutex_enter(&dip->i_tlock);
29097c478bd9Sstevel@tonic-gate 		dip->i_seq++;
29107c478bd9Sstevel@tonic-gate 		mutex_exit(&dip->i_tlock);
29117c478bd9Sstevel@tonic-gate 		rw_exit(&dip->i_contents);
29127c478bd9Sstevel@tonic-gate 	}
29137c478bd9Sstevel@tonic-gate 
29147c478bd9Sstevel@tonic-gate 	if (ulp) {
29157c478bd9Sstevel@tonic-gate 		int terr = 0;
29167c478bd9Sstevel@tonic-gate 
29177c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
29187c478bd9Sstevel@tonic-gate 		    trans_size);
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 		/*
29217c478bd9Sstevel@tonic-gate 		 * If we haven't had a more interesting failure
29227c478bd9Sstevel@tonic-gate 		 * already, then anything that might've happened
29237c478bd9Sstevel@tonic-gate 		 * here should be reported.
29247c478bd9Sstevel@tonic-gate 		 */
29257c478bd9Sstevel@tonic-gate 		if (error == 0)
29267c478bd9Sstevel@tonic-gate 			error = terr;
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if (!error && truncflag) {
29307c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
29317c478bd9Sstevel@tonic-gate 		(void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
29327c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (ulp)
29367c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	/*
29397c478bd9Sstevel@tonic-gate 	 * If no inodes available, try to free one up out of the
29407c478bd9Sstevel@tonic-gate 	 * pending delete queue.
29417c478bd9Sstevel@tonic-gate 	 */
29427c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
29437c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
29447c478bd9Sstevel@tonic-gate 		retry = 0;
29457c478bd9Sstevel@tonic-gate 		goto again;
29467c478bd9Sstevel@tonic-gate 	}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate out:
29497c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_CREATE_END,
29507c478bd9Sstevel@tonic-gate 		"ufs_create_end:dvp %p name %s error %d", vpp, name, error);
29517c478bd9Sstevel@tonic-gate 	return (error);
29527c478bd9Sstevel@tonic-gate }
29537c478bd9Sstevel@tonic-gate 
29547c478bd9Sstevel@tonic-gate extern int ufs_idle_max;
29557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
29567c478bd9Sstevel@tonic-gate static int
29577c478bd9Sstevel@tonic-gate ufs_remove(struct vnode *vp, char *nm, struct cred *cr)
29587c478bd9Sstevel@tonic-gate {
29597c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
29607c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp	= ip->i_ufsvfs;
29617c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
29627c478bd9Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode corresponding to name being removed */
29637c478bd9Sstevel@tonic-gate 	int error;
29647c478bd9Sstevel@tonic-gate 	int issync;
29657c478bd9Sstevel@tonic-gate 	int trans_size;
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_REMOVE_START,
29687c478bd9Sstevel@tonic-gate 		"ufs_remove_start:vp %p", vp);
29697c478bd9Sstevel@tonic-gate 
29707c478bd9Sstevel@tonic-gate 	/*
29717c478bd9Sstevel@tonic-gate 	 * don't let the delete queue get too long
29727c478bd9Sstevel@tonic-gate 	 */
29737c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
29747c478bd9Sstevel@tonic-gate 		error = EIO;
29757c478bd9Sstevel@tonic-gate 		goto out;
29767c478bd9Sstevel@tonic-gate 	}
29777c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
29787c478bd9Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
29797c478bd9Sstevel@tonic-gate 
29807c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
29817c478bd9Sstevel@tonic-gate 	if (error)
29827c478bd9Sstevel@tonic-gate 		goto out;
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	if (ulp)
29857c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
29867c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
29897c478bd9Sstevel@tonic-gate 	error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
29907c478bd9Sstevel@tonic-gate 	    DR_REMOVE, cr, &rmvp);
29917c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	if (ulp) {
29947c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
29957c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
29967c478bd9Sstevel@tonic-gate 	}
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	/*
29997c478bd9Sstevel@tonic-gate 	 * This must be called after the remove transaction is closed.
30007c478bd9Sstevel@tonic-gate 	 */
30017c478bd9Sstevel@tonic-gate 	if (rmvp != NULL) {
30027c478bd9Sstevel@tonic-gate 		/* Only send the event if there were no errors */
30037c478bd9Sstevel@tonic-gate 		if (error == 0)
30047c478bd9Sstevel@tonic-gate 			vnevent_remove(rmvp);
30057c478bd9Sstevel@tonic-gate 		VN_RELE(rmvp);
30067c478bd9Sstevel@tonic-gate 	}
30077c478bd9Sstevel@tonic-gate out:
30087c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_REMOVE_END,
30097c478bd9Sstevel@tonic-gate 		"ufs_remove_end:vp %p name %s error %d", vp, nm, error);
30107c478bd9Sstevel@tonic-gate 	return (error);
30117c478bd9Sstevel@tonic-gate }
30127c478bd9Sstevel@tonic-gate 
30137c478bd9Sstevel@tonic-gate /*
30147c478bd9Sstevel@tonic-gate  * Link a file or a directory.  Only privileged processes are allowed to
30157c478bd9Sstevel@tonic-gate  * make links to directories.
30167c478bd9Sstevel@tonic-gate  */
30177c478bd9Sstevel@tonic-gate static int
30187c478bd9Sstevel@tonic-gate ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr)
30197c478bd9Sstevel@tonic-gate {
30207c478bd9Sstevel@tonic-gate 	struct inode *sip;
30217c478bd9Sstevel@tonic-gate 	struct inode *tdp = VTOI(tdvp);
30227c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
30237c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
30247c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
30257c478bd9Sstevel@tonic-gate 	int error;
30267c478bd9Sstevel@tonic-gate 	int issync;
30277c478bd9Sstevel@tonic-gate 	int trans_size;
30287c478bd9Sstevel@tonic-gate 	int isdev;
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_LINK_START,
30317c478bd9Sstevel@tonic-gate 		"ufs_link_start:tdvp %p", tdvp);
30327c478bd9Sstevel@tonic-gate 
30337c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
30347c478bd9Sstevel@tonic-gate 	if (error)
30357c478bd9Sstevel@tonic-gate 		goto out;
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	if (ulp)
30387c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
30397c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(svp, &realvp) == 0)
30427c478bd9Sstevel@tonic-gate 		svp = realvp;
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 	/*
30457c478bd9Sstevel@tonic-gate 	 * Make sure link for extended attributes is valid
30467c478bd9Sstevel@tonic-gate 	 * We only support hard linking of attr in ATTRDIR to ATTRDIR
30477c478bd9Sstevel@tonic-gate 	 *
30487c478bd9Sstevel@tonic-gate 	 * Make certain we don't attempt to look at a device node as
30497c478bd9Sstevel@tonic-gate 	 * a ufs inode.
30507c478bd9Sstevel@tonic-gate 	 */
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate 	isdev = IS_DEVVP(svp);
30537c478bd9Sstevel@tonic-gate 	if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
30547c478bd9Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
30557c478bd9Sstevel@tonic-gate 	    ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
30567c478bd9Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFDIR))) {
30577c478bd9Sstevel@tonic-gate 		error = EINVAL;
30587c478bd9Sstevel@tonic-gate 		goto unlock;
30597c478bd9Sstevel@tonic-gate 	}
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 	sip = VTOI(svp);
30627c478bd9Sstevel@tonic-gate 	if ((svp->v_type == VDIR &&
30637c478bd9Sstevel@tonic-gate 	    secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
30647c478bd9Sstevel@tonic-gate 	    (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
30657c478bd9Sstevel@tonic-gate 		error = EPERM;
30667c478bd9Sstevel@tonic-gate 		goto unlock;
30677c478bd9Sstevel@tonic-gate 	}
30687c478bd9Sstevel@tonic-gate 	rw_enter(&tdp->i_rwlock, RW_WRITER);
30697c478bd9Sstevel@tonic-gate 	error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
30707c478bd9Sstevel@tonic-gate 	    sip, cr, NULL);
30717c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate unlock:
30747c478bd9Sstevel@tonic-gate 	if (ulp) {
30757c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
30767c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
30777c478bd9Sstevel@tonic-gate 	}
30787c478bd9Sstevel@tonic-gate out:
30797c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_LINK_END,
30807c478bd9Sstevel@tonic-gate 		"ufs_link_end:tdvp %p error %d", tdvp, error);
30817c478bd9Sstevel@tonic-gate 	return (error);
30827c478bd9Sstevel@tonic-gate }
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate uint64_t ufs_rename_retry_cnt;
30857c478bd9Sstevel@tonic-gate uint64_t ufs_rename_upgrade_retry_cnt;
30867c478bd9Sstevel@tonic-gate uint64_t ufs_rename_dircheck_retry_cnt;
30877c478bd9Sstevel@tonic-gate clock_t	 ufs_rename_backoff_delay = 1;
30887c478bd9Sstevel@tonic-gate 
30897c478bd9Sstevel@tonic-gate /*
30907c478bd9Sstevel@tonic-gate  * Rename a file or directory.
30917c478bd9Sstevel@tonic-gate  * We are given the vnode and entry string of the source and the
30927c478bd9Sstevel@tonic-gate  * vnode and entry string of the place we want to move the source
30937c478bd9Sstevel@tonic-gate  * to (the target). The essential operation is:
30947c478bd9Sstevel@tonic-gate  *	unlink(target);
30957c478bd9Sstevel@tonic-gate  *	link(source, target);
30967c478bd9Sstevel@tonic-gate  *	unlink(source);
30977c478bd9Sstevel@tonic-gate  * but "atomically".  Can't do full commit without saving state in
30987c478bd9Sstevel@tonic-gate  * the inode on disk, which isn't feasible at this time.  Best we
30997c478bd9Sstevel@tonic-gate  * can do is always guarantee that the TARGET exists.
31007c478bd9Sstevel@tonic-gate  */
31017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
31027c478bd9Sstevel@tonic-gate static int
31037c478bd9Sstevel@tonic-gate ufs_rename(
31047c478bd9Sstevel@tonic-gate 	struct vnode *sdvp,		/* old (source) parent vnode */
31057c478bd9Sstevel@tonic-gate 	char *snm,			/* old (source) entry name */
31067c478bd9Sstevel@tonic-gate 	struct vnode *tdvp,		/* new (target) parent vnode */
31077c478bd9Sstevel@tonic-gate 	char *tnm,			/* new (target) entry name */
31087c478bd9Sstevel@tonic-gate 	struct cred *cr)
31097c478bd9Sstevel@tonic-gate {
31107c478bd9Sstevel@tonic-gate 	struct inode *sip = NULL;	/* source inode */
31117c478bd9Sstevel@tonic-gate 	struct inode *sdp;		/* old (source) parent inode */
31127c478bd9Sstevel@tonic-gate 	struct inode *tdp;		/* new (target) parent inode */
31137c478bd9Sstevel@tonic-gate 	struct vnode *tvp = NULL;	/* target vnode, if it exists */
31147c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
31157c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
31167c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
31177c478bd9Sstevel@tonic-gate 	int error;
31187c478bd9Sstevel@tonic-gate 	int issync;
31197c478bd9Sstevel@tonic-gate 	int trans_size;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_RENAME_START,
31227c478bd9Sstevel@tonic-gate 		"ufs_rename_start:sdvp %p", sdvp);
31237c478bd9Sstevel@tonic-gate 
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	sdp = VTOI(sdvp);
31267c478bd9Sstevel@tonic-gate 	ufsvfsp = sdp->i_ufsvfs;
31277c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
31287c478bd9Sstevel@tonic-gate 	if (error)
31297c478bd9Sstevel@tonic-gate 		goto out;
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	if (ulp)
31327c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
31337c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_RENAME_SIZE(sdp));
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(tdvp, &realvp) == 0)
31367c478bd9Sstevel@tonic-gate 		tdvp = realvp;
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	tdp = VTOI(tdvp);
31397c478bd9Sstevel@tonic-gate 
31407c478bd9Sstevel@tonic-gate 	/*
31417c478bd9Sstevel@tonic-gate 	 * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
31427c478bd9Sstevel@tonic-gate 	 */
31437c478bd9Sstevel@tonic-gate 	if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
31447c478bd9Sstevel@tonic-gate 		error = EINVAL;
31457c478bd9Sstevel@tonic-gate 		goto unlock;
31467c478bd9Sstevel@tonic-gate 	}
31477c478bd9Sstevel@tonic-gate 
31487c478bd9Sstevel@tonic-gate 	/*
31497c478bd9Sstevel@tonic-gate 	 * Look up inode of file we're supposed to rename.
31507c478bd9Sstevel@tonic-gate 	 */
31517c478bd9Sstevel@tonic-gate 	if (error = ufs_dirlook(sdp, snm, &sip, cr, 0)) {
31527c478bd9Sstevel@tonic-gate 		goto unlock;
31537c478bd9Sstevel@tonic-gate 	}
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/*
31567c478bd9Sstevel@tonic-gate 	 * Lock both the source and target directories (they may be
31577c478bd9Sstevel@tonic-gate 	 * the same) to provide the atomicity semantics that was
31587c478bd9Sstevel@tonic-gate 	 * previously provided by the per file system vfs_rename_lock
31597c478bd9Sstevel@tonic-gate 	 *
31607c478bd9Sstevel@tonic-gate 	 * with vfs_rename_lock removed to allow simultaneous renames
31617c478bd9Sstevel@tonic-gate 	 * within a file system, ufs_dircheckpath can deadlock while
31627c478bd9Sstevel@tonic-gate 	 * traversing back to ensure that source is not a parent directory
31637c478bd9Sstevel@tonic-gate 	 * of target parent directory. This is because we get into
31647c478bd9Sstevel@tonic-gate 	 * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
31657c478bd9Sstevel@tonic-gate 	 * If the tdp and sdp of the simultaneous renames happen to be
31667c478bd9Sstevel@tonic-gate 	 * in the path of each other, it can lead to a deadlock. This
31677c478bd9Sstevel@tonic-gate 	 * can be avoided by getting the locks as RW_READER here and then
31687c478bd9Sstevel@tonic-gate 	 * upgrading to RW_WRITER after completing the ufs_dircheckpath.
31697c478bd9Sstevel@tonic-gate 	 */
31707c478bd9Sstevel@tonic-gate retry:
31717c478bd9Sstevel@tonic-gate 	rw_enter(&tdp->i_rwlock, RW_READER);
31727c478bd9Sstevel@tonic-gate 	if (tdp != sdp) {
31737c478bd9Sstevel@tonic-gate 		/*
31747c478bd9Sstevel@tonic-gate 		 * We're locking 2 peer level locks, so must use tryenter
31757c478bd9Sstevel@tonic-gate 		 * on the 2nd to avoid deadlocks that would occur
31767c478bd9Sstevel@tonic-gate 		 * if we renamed a->b and b->a concurrently.
31777c478bd9Sstevel@tonic-gate 		 */
31787c478bd9Sstevel@tonic-gate 		if (!rw_tryenter(&sdp->i_rwlock, RW_READER)) {
31797c478bd9Sstevel@tonic-gate 			/*
31807c478bd9Sstevel@tonic-gate 			 * Reverse the lock grabs in case we have heavy
31817c478bd9Sstevel@tonic-gate 			 * contention on the 2nd lock.
31827c478bd9Sstevel@tonic-gate 			 */
31837c478bd9Sstevel@tonic-gate 			rw_exit(&tdp->i_rwlock);
31847c478bd9Sstevel@tonic-gate 			rw_enter(&sdp->i_rwlock, RW_READER);
31857c478bd9Sstevel@tonic-gate 			if (!rw_tryenter(&tdp->i_rwlock, RW_READER)) {
31867c478bd9Sstevel@tonic-gate 				ufs_rename_retry_cnt++;
31877c478bd9Sstevel@tonic-gate 				rw_exit(&sdp->i_rwlock);
31887c478bd9Sstevel@tonic-gate 				goto retry;
31897c478bd9Sstevel@tonic-gate 			}
31907c478bd9Sstevel@tonic-gate 		}
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate 	if (sip == tdp) {
31947c478bd9Sstevel@tonic-gate 		error = EINVAL;
31957c478bd9Sstevel@tonic-gate 		goto errout;
31967c478bd9Sstevel@tonic-gate 	}
31977c478bd9Sstevel@tonic-gate 	/*
31987c478bd9Sstevel@tonic-gate 	 * Make sure we can delete the source entry.  This requires
31997c478bd9Sstevel@tonic-gate 	 * write permission on the containing directory.
32007c478bd9Sstevel@tonic-gate 	 * Check for sticky directories.
32017c478bd9Sstevel@tonic-gate 	 */
32027c478bd9Sstevel@tonic-gate 	rw_enter(&sdp->i_contents, RW_READER);
32037c478bd9Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_READER);
32047c478bd9Sstevel@tonic-gate 	if ((error = ufs_iaccess(sdp, IWRITE, cr)) != 0 ||
32057c478bd9Sstevel@tonic-gate 	    (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
32067c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
32077c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
32087c478bd9Sstevel@tonic-gate 		goto errout;
32097c478bd9Sstevel@tonic-gate 	}
32107c478bd9Sstevel@tonic-gate 
32117c478bd9Sstevel@tonic-gate 	/*
32127c478bd9Sstevel@tonic-gate 	 * If this is a rename of a directory and the parent is
32137c478bd9Sstevel@tonic-gate 	 * different (".." must be changed), then the source
32147c478bd9Sstevel@tonic-gate 	 * directory must not be in the directory hierarchy
32157c478bd9Sstevel@tonic-gate 	 * above the target, as this would orphan everything
32167c478bd9Sstevel@tonic-gate 	 * below the source directory.  Also the user must have
32177c478bd9Sstevel@tonic-gate 	 * write permission in the source so as to be able to
32187c478bd9Sstevel@tonic-gate 	 * change "..".
32197c478bd9Sstevel@tonic-gate 	 */
32207c478bd9Sstevel@tonic-gate 	if ((((sip->i_mode & IFMT) == IFDIR) ||
32217c478bd9Sstevel@tonic-gate 	    ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
32227c478bd9Sstevel@tonic-gate 		ino_t	inum;
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 		if ((error = ufs_iaccess(sip, IWRITE, cr))) {
32257c478bd9Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
32267c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_contents);
32277c478bd9Sstevel@tonic-gate 			goto errout;
32287c478bd9Sstevel@tonic-gate 		}
32297c478bd9Sstevel@tonic-gate 		inum = sip->i_number;
32307c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
32317c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
32327c478bd9Sstevel@tonic-gate 		if ((error = ufs_dircheckpath(inum, tdp, sdp, cr))) {
32337c478bd9Sstevel@tonic-gate 			/*
32347c478bd9Sstevel@tonic-gate 			 * If we got EAGAIN ufs_dircheckpath detected a
32357c478bd9Sstevel@tonic-gate 			 * potential deadlock and backed out. We need
32367c478bd9Sstevel@tonic-gate 			 * to retry the operation since sdp and tdp have
32377c478bd9Sstevel@tonic-gate 			 * to be released to avoid the deadlock.
32387c478bd9Sstevel@tonic-gate 			 */
32397c478bd9Sstevel@tonic-gate 			if (error == EAGAIN) {
32407c478bd9Sstevel@tonic-gate 				rw_exit(&tdp->i_rwlock);
32417c478bd9Sstevel@tonic-gate 				if (tdp != sdp)
32427c478bd9Sstevel@tonic-gate 					rw_exit(&sdp->i_rwlock);
32437c478bd9Sstevel@tonic-gate 				delay(ufs_rename_backoff_delay);
32447c478bd9Sstevel@tonic-gate 				ufs_rename_dircheck_retry_cnt++;
32457c478bd9Sstevel@tonic-gate 				goto retry;
32467c478bd9Sstevel@tonic-gate 			}
32477c478bd9Sstevel@tonic-gate 			goto errout;
32487c478bd9Sstevel@tonic-gate 		}
32497c478bd9Sstevel@tonic-gate 	} else {
32507c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
32517c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	/*
32567c478bd9Sstevel@tonic-gate 	 * Check for renaming '.' or '..' or alias of '.'
32577c478bd9Sstevel@tonic-gate 	 */
32587c478bd9Sstevel@tonic-gate 	if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0 || sdp == sip) {
32597c478bd9Sstevel@tonic-gate 		error = EINVAL;
32607c478bd9Sstevel@tonic-gate 		goto errout;
32617c478bd9Sstevel@tonic-gate 	}
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	/*
32647c478bd9Sstevel@tonic-gate 	 * Simultaneous renames can deadlock in ufs_dircheckpath since it
32657c478bd9Sstevel@tonic-gate 	 * tries to traverse back the file tree with both tdp and sdp held
32667c478bd9Sstevel@tonic-gate 	 * as RW_WRITER. To avoid that we have to hold the tdp and sdp locks
32677c478bd9Sstevel@tonic-gate 	 * as RW_READERS  till ufs_dircheckpath is done.
32687c478bd9Sstevel@tonic-gate 	 * Now that ufs_dircheckpath is done with, we can upgrade the locks
32697c478bd9Sstevel@tonic-gate 	 * to RW_WRITER.
32707c478bd9Sstevel@tonic-gate 	 */
32717c478bd9Sstevel@tonic-gate 	if (!rw_tryupgrade(&tdp->i_rwlock)) {
32727c478bd9Sstevel@tonic-gate 		/*
32737c478bd9Sstevel@tonic-gate 		 * The upgrade failed. We got to give away the lock
32747c478bd9Sstevel@tonic-gate 		 * as to avoid deadlocking with someone else who is
32757c478bd9Sstevel@tonic-gate 		 * waiting for writer lock. With the lock gone, we
32767c478bd9Sstevel@tonic-gate 		 * cannot be sure the checks done above will hold
32777c478bd9Sstevel@tonic-gate 		 * good when we eventually get them back as writer.
32787c478bd9Sstevel@tonic-gate 		 * So if we can't upgrade we drop the locks and retry
32797c478bd9Sstevel@tonic-gate 		 * everything again.
32807c478bd9Sstevel@tonic-gate 		 */
32817c478bd9Sstevel@tonic-gate 		rw_exit(&tdp->i_rwlock);
32827c478bd9Sstevel@tonic-gate 		if (tdp != sdp)
32837c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
32847c478bd9Sstevel@tonic-gate 		delay(ufs_rename_backoff_delay);
32857c478bd9Sstevel@tonic-gate 		ufs_rename_upgrade_retry_cnt++;
32867c478bd9Sstevel@tonic-gate 		goto retry;
32877c478bd9Sstevel@tonic-gate 	}
32887c478bd9Sstevel@tonic-gate 	if (tdp != sdp) {
32897c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&sdp->i_rwlock)) {
32907c478bd9Sstevel@tonic-gate 			/*
32917c478bd9Sstevel@tonic-gate 			 * The upgrade failed. We got to give away the lock
32927c478bd9Sstevel@tonic-gate 			 * as to avoid deadlocking with someone else who is
32937c478bd9Sstevel@tonic-gate 			 * waiting for writer lock. With the lock gone, we
32947c478bd9Sstevel@tonic-gate 			 * cannot be sure the checks done above will hold
32957c478bd9Sstevel@tonic-gate 			 * good when we eventually get them back as writer.
32967c478bd9Sstevel@tonic-gate 			 * So if we can't upgrade we drop the locks and retry
32977c478bd9Sstevel@tonic-gate 			 * everything again.
32987c478bd9Sstevel@tonic-gate 			 */
32997c478bd9Sstevel@tonic-gate 			rw_exit(&tdp->i_rwlock);
33007c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
33017c478bd9Sstevel@tonic-gate 			delay(ufs_rename_backoff_delay);
33027c478bd9Sstevel@tonic-gate 			ufs_rename_upgrade_retry_cnt++;
33037c478bd9Sstevel@tonic-gate 			goto retry;
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 	/*
33077c478bd9Sstevel@tonic-gate 	 * Link source to the target.  If a target exists, return its
33087c478bd9Sstevel@tonic-gate 	 * vnode pointer in tvp.  We'll release it after sending the
33097c478bd9Sstevel@tonic-gate 	 * vnevent.
33107c478bd9Sstevel@tonic-gate 	 */
33117c478bd9Sstevel@tonic-gate 	if (error = ufs_direnter_lr(tdp, tnm, DE_RENAME, sdp, sip, cr, &tvp)) {
33127c478bd9Sstevel@tonic-gate 		/*
33137c478bd9Sstevel@tonic-gate 		 * ESAME isn't really an error; it indicates that the
33147c478bd9Sstevel@tonic-gate 		 * operation should not be done because the source and target
33157c478bd9Sstevel@tonic-gate 		 * are the same file, but that no error should be reported.
33167c478bd9Sstevel@tonic-gate 		 */
33177c478bd9Sstevel@tonic-gate 		if (error == ESAME)
33187c478bd9Sstevel@tonic-gate 			error = 0;
33197c478bd9Sstevel@tonic-gate 		goto errout;
33207c478bd9Sstevel@tonic-gate 	}
33217c478bd9Sstevel@tonic-gate 
33227c478bd9Sstevel@tonic-gate 	/*
33237c478bd9Sstevel@tonic-gate 	 * Unlink the source.
33247c478bd9Sstevel@tonic-gate 	 * Remove the source entry.  ufs_dirremove() checks that the entry
33257c478bd9Sstevel@tonic-gate 	 * still reflects sip, and returns an error if it doesn't.
33267c478bd9Sstevel@tonic-gate 	 * If the entry has changed just forget about it.  Release
33277c478bd9Sstevel@tonic-gate 	 * the source inode.
33287c478bd9Sstevel@tonic-gate 	 */
33297c478bd9Sstevel@tonic-gate 	if ((error = ufs_dirremove(sdp, snm, sip, (struct vnode *)0,
33307c478bd9Sstevel@tonic-gate 	    DR_RENAME, cr, NULL)) == ENOENT)
33317c478bd9Sstevel@tonic-gate 		error = 0;
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate errout:
33347c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
33357c478bd9Sstevel@tonic-gate 	if (sdp != tdp) {
33367c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_rwlock);
33377c478bd9Sstevel@tonic-gate 	}
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate unlock:
33407c478bd9Sstevel@tonic-gate 	if (ulp) {
33417c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME, trans_size);
33427c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
33437c478bd9Sstevel@tonic-gate 	}
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	/*
33467c478bd9Sstevel@tonic-gate 	 * If no errors, send the appropriate events on the source
33477c478bd9Sstevel@tonic-gate 	 * and destination (a.k.a, target) vnodes, if they exist.
33487c478bd9Sstevel@tonic-gate 	 * This has to be done after the rename transaction has closed.
33497c478bd9Sstevel@tonic-gate 	 */
33507c478bd9Sstevel@tonic-gate 	if (error == 0) {
33517c478bd9Sstevel@tonic-gate 		if (tvp != NULL)
33527c478bd9Sstevel@tonic-gate 			vnevent_rename_dest(tvp);
33537c478bd9Sstevel@tonic-gate 		/*
33547c478bd9Sstevel@tonic-gate 		 * Note that if ufs_direnter_lr() returned ESAME then
33557c478bd9Sstevel@tonic-gate 		 * this event will still be sent.  This isn't expected
33567c478bd9Sstevel@tonic-gate 		 * to be a problem for anticipated usage by consumers.
33577c478bd9Sstevel@tonic-gate 		 */
33587c478bd9Sstevel@tonic-gate 		if (sip != NULL)
33597c478bd9Sstevel@tonic-gate 			vnevent_rename_src(ITOV(sip));
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	if (tvp != NULL)
33637c478bd9Sstevel@tonic-gate 		VN_RELE(tvp);
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	if (sip != NULL)
33667c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(sip));
33677c478bd9Sstevel@tonic-gate 
33687c478bd9Sstevel@tonic-gate out:
33697c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_UFS, TR_UFS_RENAME_END,
33707c478bd9Sstevel@tonic-gate 		"ufs_rename_end:sdvp %p snm %s tdvp %p tnm %s error %d",
33717c478bd9Sstevel@tonic-gate 			sdvp, snm, tdvp, tnm, error);
33727c478bd9Sstevel@tonic-gate 	return (error);
33737c478bd9Sstevel@tonic-gate }
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33767c478bd9Sstevel@tonic-gate static int
33777c478bd9Sstevel@tonic-gate ufs_mkdir(struct vnode *dvp, char *dirname, struct vattr *vap,
33787c478bd9Sstevel@tonic-gate 	struct vnode **vpp, struct cred *cr)
33797c478bd9Sstevel@tonic-gate {
33807c478bd9Sstevel@tonic-gate 	struct inode *ip;
33817c478bd9Sstevel@tonic-gate 	struct inode *xip;
33827c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
33837c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
33847c478bd9Sstevel@tonic-gate 	int error;
33857c478bd9Sstevel@tonic-gate 	int issync;
33867c478bd9Sstevel@tonic-gate 	int trans_size;
33877c478bd9Sstevel@tonic-gate 	int retry = 1;
33887c478bd9Sstevel@tonic-gate 
33897c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
33907c478bd9Sstevel@tonic-gate 
33917c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_MKDIR_START,
33927c478bd9Sstevel@tonic-gate 		"ufs_mkdir_start:dvp %p", dvp);
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	/*
33957c478bd9Sstevel@tonic-gate 	 * Can't make directory in attr hidden dir
33967c478bd9Sstevel@tonic-gate 	 */
33977c478bd9Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
33987c478bd9Sstevel@tonic-gate 		return (EINVAL);
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate again:
34017c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
34027c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
34037c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MKDIR_MASK);
34047c478bd9Sstevel@tonic-gate 	if (error)
34057c478bd9Sstevel@tonic-gate 		goto out;
34067c478bd9Sstevel@tonic-gate 	if (ulp)
34077c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_MKDIR,
34087c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_MKDIR_SIZE(ip));
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	error = ufs_direnter_cm(ip, dirname, DE_MKDIR, vap, &xip, cr,
34137c478bd9Sstevel@tonic-gate 		(retry ? IQUIET : 0));
34147c478bd9Sstevel@tonic-gate 
34157c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
34167c478bd9Sstevel@tonic-gate 	if (error == 0) {
34177c478bd9Sstevel@tonic-gate 		ip = xip;
34187c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
34197c478bd9Sstevel@tonic-gate 	} else if (error == EEXIST)
34207c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(xip));
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate 	if (ulp) {
34237c478bd9Sstevel@tonic-gate 		int terr = 0;
34247c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_MKDIR, trans_size);
34257c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
34267c478bd9Sstevel@tonic-gate 		if (error == 0)
34277c478bd9Sstevel@tonic-gate 			error = terr;
34287c478bd9Sstevel@tonic-gate 	}
34297c478bd9Sstevel@tonic-gate out:
34307c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
34317c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
34327c478bd9Sstevel@tonic-gate 		retry = 0;
34337c478bd9Sstevel@tonic-gate 		goto again;
34347c478bd9Sstevel@tonic-gate 	}
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_MKDIR_END,
34377c478bd9Sstevel@tonic-gate 		"ufs_mkdir_end:dvp %p error %d", dvp, error);
34387c478bd9Sstevel@tonic-gate 	return (error);
34397c478bd9Sstevel@tonic-gate }
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
34427c478bd9Sstevel@tonic-gate static int
34437c478bd9Sstevel@tonic-gate ufs_rmdir(struct vnode *vp, char *nm, struct vnode *cdir, struct cred *cr)
34447c478bd9Sstevel@tonic-gate {
34457c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
34467c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
34477c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
34487c478bd9Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode of removed directory */
34497c478bd9Sstevel@tonic-gate 	int error;
34507c478bd9Sstevel@tonic-gate 	int issync;
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_RMDIR_START,
34537c478bd9Sstevel@tonic-gate 		"ufs_rmdir_start:vp %p", vp);
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 	/*
34567c478bd9Sstevel@tonic-gate 	 * don't let the delete queue get too long
34577c478bd9Sstevel@tonic-gate 	 */
34587c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
34597c478bd9Sstevel@tonic-gate 		error = EIO;
34607c478bd9Sstevel@tonic-gate 		goto out;
34617c478bd9Sstevel@tonic-gate 	}
34627c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
34637c478bd9Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
34647c478bd9Sstevel@tonic-gate 
34657c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RMDIR_MASK);
34667c478bd9Sstevel@tonic-gate 	if (error)
34677c478bd9Sstevel@tonic-gate 		goto out;
34687c478bd9Sstevel@tonic-gate 
34697c478bd9Sstevel@tonic-gate 	if (ulp)
34707c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RMDIR, TOP_RMDIR_SIZE);
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_WRITER);
34737c478bd9Sstevel@tonic-gate 	error = ufs_dirremove(ip, nm, (struct inode *)0, cdir, DR_RMDIR, cr,
34747c478bd9Sstevel@tonic-gate 									&rmvp);
34757c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 	if (ulp) {
34787c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RMDIR,
34797c478bd9Sstevel@tonic-gate 				TOP_RMDIR_SIZE);
34807c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
34817c478bd9Sstevel@tonic-gate 	}
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate 	/*
34847c478bd9Sstevel@tonic-gate 	 * This must be done AFTER the rmdir transaction has closed.
34857c478bd9Sstevel@tonic-gate 	 */
34867c478bd9Sstevel@tonic-gate 	if (rmvp != NULL) {
34877c478bd9Sstevel@tonic-gate 		/* Only send the event if there were no errors */
34887c478bd9Sstevel@tonic-gate 		if (error == 0)
34897c478bd9Sstevel@tonic-gate 			vnevent_rmdir(rmvp);
34907c478bd9Sstevel@tonic-gate 		VN_RELE(rmvp);
34917c478bd9Sstevel@tonic-gate 	}
34927c478bd9Sstevel@tonic-gate out:
34937c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_RMDIR_END,
34947c478bd9Sstevel@tonic-gate 		"ufs_rmdir_end:vp %p error %d", vp, error);
34957c478bd9Sstevel@tonic-gate 
34967c478bd9Sstevel@tonic-gate 	return (error);
34977c478bd9Sstevel@tonic-gate }
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate /* ARGSUSED */
35007c478bd9Sstevel@tonic-gate static int
35017c478bd9Sstevel@tonic-gate ufs_readdir(
35027c478bd9Sstevel@tonic-gate 	struct vnode *vp,
35037c478bd9Sstevel@tonic-gate 	struct uio *uiop,
35047c478bd9Sstevel@tonic-gate 	struct cred *cr,
35057c478bd9Sstevel@tonic-gate 	int *eofp)
35067c478bd9Sstevel@tonic-gate {
35077c478bd9Sstevel@tonic-gate 	struct iovec *iovp;
35087c478bd9Sstevel@tonic-gate 	struct inode *ip;
35097c478bd9Sstevel@tonic-gate 	struct direct *idp;
35107c478bd9Sstevel@tonic-gate 	struct dirent64 *odp;
35117c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
35127c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
35137c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
35147c478bd9Sstevel@tonic-gate 	caddr_t outbuf;
35157c478bd9Sstevel@tonic-gate 	size_t bufsize;
35167c478bd9Sstevel@tonic-gate 	uint_t offset;
35177c478bd9Sstevel@tonic-gate 	uint_t bytes_wanted, total_bytes_wanted;
35187c478bd9Sstevel@tonic-gate 	int incount = 0;
35197c478bd9Sstevel@tonic-gate 	int outcount = 0;
35207c478bd9Sstevel@tonic-gate 	int error;
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
35237c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
35247c478bd9Sstevel@tonic-gate 
35257c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_READDIR_START,
35267c478bd9Sstevel@tonic-gate 		"ufs_readdir_start:vp %p uiop %p", vp, uiop);
35277c478bd9Sstevel@tonic-gate 
35287c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF32_T) {
35297c478bd9Sstevel@tonic-gate 		if (eofp)
35307c478bd9Sstevel@tonic-gate 			*eofp = 1;
35317c478bd9Sstevel@tonic-gate 		return (0);
35327c478bd9Sstevel@tonic-gate 	}
35337c478bd9Sstevel@tonic-gate 
35347c478bd9Sstevel@tonic-gate 	/*
35357c478bd9Sstevel@tonic-gate 	 * Check if we have been called with a valid iov_len
35367c478bd9Sstevel@tonic-gate 	 * and bail out if not, otherwise we may potentially loop
35377c478bd9Sstevel@tonic-gate 	 * forever further down.
35387c478bd9Sstevel@tonic-gate 	 */
35397c478bd9Sstevel@tonic-gate 	if (uiop->uio_iov->iov_len <= 0) {
35407c478bd9Sstevel@tonic-gate 		error = EINVAL;
35417c478bd9Sstevel@tonic-gate 		goto out;
35427c478bd9Sstevel@tonic-gate 	}
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	/*
35457c478bd9Sstevel@tonic-gate 	 * Large Files: When we come here we are guaranteed that
35467c478bd9Sstevel@tonic-gate 	 * uio_offset can be used safely. The high word is zero.
35477c478bd9Sstevel@tonic-gate 	 */
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
35507c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READDIR_MASK);
35517c478bd9Sstevel@tonic-gate 	if (error)
35527c478bd9Sstevel@tonic-gate 		goto out;
35537c478bd9Sstevel@tonic-gate 
35547c478bd9Sstevel@tonic-gate 	iovp = uiop->uio_iov;
35557c478bd9Sstevel@tonic-gate 	total_bytes_wanted = iovp->iov_len;
35567c478bd9Sstevel@tonic-gate 
35577c478bd9Sstevel@tonic-gate 	/* Large Files: directory files should not be "large" */
35587c478bd9Sstevel@tonic-gate 
35597c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_size <= MAXOFF32_T);
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 	/* Force offset to be valid (to guard against bogus lseek() values) */
35627c478bd9Sstevel@tonic-gate 	offset = (uint_t)uiop->uio_offset & ~(DIRBLKSIZ - 1);
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	/* Quit if at end of file or link count of zero (posix) */
35657c478bd9Sstevel@tonic-gate 	if (offset >= (uint_t)ip->i_size || ip->i_nlink <= 0) {
35667c478bd9Sstevel@tonic-gate 		if (eofp)
35677c478bd9Sstevel@tonic-gate 			*eofp = 1;
35687c478bd9Sstevel@tonic-gate 		error = 0;
35697c478bd9Sstevel@tonic-gate 		goto unlock;
35707c478bd9Sstevel@tonic-gate 	}
35717c478bd9Sstevel@tonic-gate 
35727c478bd9Sstevel@tonic-gate 	/*
35737c478bd9Sstevel@tonic-gate 	 * Get space to change directory entries into fs independent format.
35747c478bd9Sstevel@tonic-gate 	 * Do fast alloc for the most commonly used-request size (filesystem
35757c478bd9Sstevel@tonic-gate 	 * block size).
35767c478bd9Sstevel@tonic-gate 	 */
35777c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1) {
35787c478bd9Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
35797c478bd9Sstevel@tonic-gate 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
35807c478bd9Sstevel@tonic-gate 		odp = (struct dirent64 *)outbuf;
35817c478bd9Sstevel@tonic-gate 	} else {
35827c478bd9Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
35837c478bd9Sstevel@tonic-gate 		odp = (struct dirent64 *)iovp->iov_base;
35847c478bd9Sstevel@tonic-gate 	}
35857c478bd9Sstevel@tonic-gate 
35867c478bd9Sstevel@tonic-gate nextblk:
35877c478bd9Sstevel@tonic-gate 	bytes_wanted = total_bytes_wanted;
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	/* Truncate request to file size */
35907c478bd9Sstevel@tonic-gate 	if (offset + bytes_wanted > (int)ip->i_size)
35917c478bd9Sstevel@tonic-gate 		bytes_wanted = (int)(ip->i_size - offset);
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 	/* Comply with MAXBSIZE boundary restrictions of fbread() */
35947c478bd9Sstevel@tonic-gate 	if ((offset & MAXBOFFSET) + bytes_wanted > MAXBSIZE)
35957c478bd9Sstevel@tonic-gate 		bytes_wanted = MAXBSIZE - (offset & MAXBOFFSET);
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	/*
35987c478bd9Sstevel@tonic-gate 	 * Read in the next chunk.
35997c478bd9Sstevel@tonic-gate 	 * We are still holding the i_rwlock.
36007c478bd9Sstevel@tonic-gate 	 */
36017c478bd9Sstevel@tonic-gate 	error = fbread(vp, (offset_t)offset, bytes_wanted, S_OTHER, &fbp);
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	if (error)
36047c478bd9Sstevel@tonic-gate 		goto update_inode;
36057c478bd9Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (ip->i_fs->fs_ronly == 0) &&
36067c478bd9Sstevel@tonic-gate 	    (!ufsvfsp->vfs_noatime)) {
36077c478bd9Sstevel@tonic-gate 		ip->i_flag |= IACC;
36087c478bd9Sstevel@tonic-gate 	}
36097c478bd9Sstevel@tonic-gate 	incount = 0;
36107c478bd9Sstevel@tonic-gate 	idp = (struct direct *)fbp->fb_addr;
36117c478bd9Sstevel@tonic-gate 	if (idp->d_ino == 0 && idp->d_reclen == 0 &&
36127c478bd9Sstevel@tonic-gate 		idp->d_namlen == 0) {
36137c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufs_readdir: bad dir, inumber = %llu, "
36147c478bd9Sstevel@tonic-gate 			"fs = %s\n",
36157c478bd9Sstevel@tonic-gate 			(u_longlong_t)ip->i_number, ufsvfsp->vfs_fs->fs_fsmnt);
36167c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
36177c478bd9Sstevel@tonic-gate 		error = ENXIO;
36187c478bd9Sstevel@tonic-gate 		goto update_inode;
36197c478bd9Sstevel@tonic-gate 	}
36207c478bd9Sstevel@tonic-gate 	/* Transform to file-system independent format */
36217c478bd9Sstevel@tonic-gate 	while (incount < bytes_wanted) {
36227c478bd9Sstevel@tonic-gate 		/*
36237c478bd9Sstevel@tonic-gate 		 * If the current directory entry is mangled, then skip
36247c478bd9Sstevel@tonic-gate 		 * to the next block.  It would be nice to set the FSBAD
36257c478bd9Sstevel@tonic-gate 		 * flag in the super-block so that a fsck is forced on
36267c478bd9Sstevel@tonic-gate 		 * next reboot, but locking is a problem.
36277c478bd9Sstevel@tonic-gate 		 */
36287c478bd9Sstevel@tonic-gate 		if (idp->d_reclen & 0x3) {
36297c478bd9Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
36307c478bd9Sstevel@tonic-gate 			break;
36317c478bd9Sstevel@tonic-gate 		}
36327c478bd9Sstevel@tonic-gate 
36337c478bd9Sstevel@tonic-gate 		/* Skip to requested offset and skip empty entries */
36347c478bd9Sstevel@tonic-gate 		if (idp->d_ino != 0 && offset >= (uint_t)uiop->uio_offset) {
36357c478bd9Sstevel@tonic-gate 			ushort_t this_reclen =
36367c478bd9Sstevel@tonic-gate 			    DIRENT64_RECLEN(idp->d_namlen);
36377c478bd9Sstevel@tonic-gate 			/* Buffer too small for any entries */
36387c478bd9Sstevel@tonic-gate 			if (!outcount && this_reclen > bufsize) {
36397c478bd9Sstevel@tonic-gate 				fbrelse(fbp, S_OTHER);
36407c478bd9Sstevel@tonic-gate 				error = EINVAL;
36417c478bd9Sstevel@tonic-gate 				goto update_inode;
36427c478bd9Sstevel@tonic-gate 			}
36437c478bd9Sstevel@tonic-gate 			/* If would overrun the buffer, quit */
36447c478bd9Sstevel@tonic-gate 			if (outcount + this_reclen > bufsize) {
36457c478bd9Sstevel@tonic-gate 				break;
36467c478bd9Sstevel@tonic-gate 			}
36477c478bd9Sstevel@tonic-gate 			/* Take this entry */
36487c478bd9Sstevel@tonic-gate 			odp->d_ino = (ino64_t)idp->d_ino;
36497c478bd9Sstevel@tonic-gate 			odp->d_reclen = (ushort_t)this_reclen;
36507c478bd9Sstevel@tonic-gate 			odp->d_off = (offset_t)(offset + idp->d_reclen);
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
36537c478bd9Sstevel@tonic-gate 
36547c478bd9Sstevel@tonic-gate 			ASSERT(strlen(idp->d_name) + 1 <=
36557c478bd9Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
36567c478bd9Sstevel@tonic-gate 			(void) strncpy(odp->d_name, idp->d_name,
36577c478bd9Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
36587c478bd9Sstevel@tonic-gate 			outcount += odp->d_reclen;
36597c478bd9Sstevel@tonic-gate 			odp = (struct dirent64 *)((intptr_t)odp +
36607c478bd9Sstevel@tonic-gate 				    odp->d_reclen);
36617c478bd9Sstevel@tonic-gate 			ASSERT(outcount <= bufsize);
36627c478bd9Sstevel@tonic-gate 		}
36637c478bd9Sstevel@tonic-gate 		if (idp->d_reclen) {
36647c478bd9Sstevel@tonic-gate 			incount += idp->d_reclen;
36657c478bd9Sstevel@tonic-gate 			offset += idp->d_reclen;
36667c478bd9Sstevel@tonic-gate 			idp = (struct direct *)((intptr_t)idp + idp->d_reclen);
36677c478bd9Sstevel@tonic-gate 		} else {
36687c478bd9Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
36697c478bd9Sstevel@tonic-gate 			break;
36707c478bd9Sstevel@tonic-gate 		}
36717c478bd9Sstevel@tonic-gate 	}
36727c478bd9Sstevel@tonic-gate 	/* Release the chunk */
36737c478bd9Sstevel@tonic-gate 	fbrelse(fbp, S_OTHER);
36747c478bd9Sstevel@tonic-gate 
36757c478bd9Sstevel@tonic-gate 	/* Read whole block, but got no entries, read another if not eof */
36767c478bd9Sstevel@tonic-gate 
36777c478bd9Sstevel@tonic-gate 	/*
36787c478bd9Sstevel@tonic-gate 	 * Large Files: casting i_size to int here is not a problem
36797c478bd9Sstevel@tonic-gate 	 * because directory sizes are always less than MAXOFF32_T.
36807c478bd9Sstevel@tonic-gate 	 * See assertion above.
36817c478bd9Sstevel@tonic-gate 	 */
36827c478bd9Sstevel@tonic-gate 
36837c478bd9Sstevel@tonic-gate 	if (offset < (int)ip->i_size && !outcount)
36847c478bd9Sstevel@tonic-gate 		goto nextblk;
36857c478bd9Sstevel@tonic-gate 
36867c478bd9Sstevel@tonic-gate 	/* Copy out the entry data */
36877c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg == UIO_SYSSPACE && uiop->uio_iovcnt == 1) {
36887c478bd9Sstevel@tonic-gate 		iovp->iov_base += outcount;
36897c478bd9Sstevel@tonic-gate 		iovp->iov_len -= outcount;
36907c478bd9Sstevel@tonic-gate 		uiop->uio_resid -= outcount;
36917c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
36927c478bd9Sstevel@tonic-gate 	} else if ((error = uiomove(outbuf, (long)outcount, UIO_READ,
36937c478bd9Sstevel@tonic-gate 				    uiop)) == 0)
36947c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
36957c478bd9Sstevel@tonic-gate update_inode:
36967c478bd9Sstevel@tonic-gate 	ITIMES(ip);
36977c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1)
36987c478bd9Sstevel@tonic-gate 		kmem_free(outbuf, bufsize);
36997c478bd9Sstevel@tonic-gate 
37007c478bd9Sstevel@tonic-gate 	if (eofp && error == 0)
37017c478bd9Sstevel@tonic-gate 		*eofp = (uiop->uio_offset >= (int)ip->i_size);
37027c478bd9Sstevel@tonic-gate unlock:
37037c478bd9Sstevel@tonic-gate 	if (ulp) {
37047c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
37057c478bd9Sstevel@tonic-gate 	}
37067c478bd9Sstevel@tonic-gate out:
37077c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_READDIR_END,
37087c478bd9Sstevel@tonic-gate 		"ufs_readdir_end:vp %p error %d", vp, error);
37097c478bd9Sstevel@tonic-gate 	return (error);
37107c478bd9Sstevel@tonic-gate }
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
37137c478bd9Sstevel@tonic-gate static int
37147c478bd9Sstevel@tonic-gate ufs_symlink(
37157c478bd9Sstevel@tonic-gate 	struct vnode *dvp,		/* ptr to parent dir vnode */
37167c478bd9Sstevel@tonic-gate 	char *linkname,			/* name of symbolic link */
37177c478bd9Sstevel@tonic-gate 	struct vattr *vap,		/* attributes */
37187c478bd9Sstevel@tonic-gate 	char *target,			/* target path */
37197c478bd9Sstevel@tonic-gate 	struct cred *cr)		/* user credentials */
37207c478bd9Sstevel@tonic-gate {
37217c478bd9Sstevel@tonic-gate 	struct inode *ip, *dip = VTOI(dvp);
37227c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = dip->i_ufsvfs;
37237c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
37247c478bd9Sstevel@tonic-gate 	int error;
37257c478bd9Sstevel@tonic-gate 	int issync;
37267c478bd9Sstevel@tonic-gate 	int trans_size;
37277c478bd9Sstevel@tonic-gate 	int residual;
37287c478bd9Sstevel@tonic-gate 	int ioflag;
37297c478bd9Sstevel@tonic-gate 	int retry = 1;
37307c478bd9Sstevel@tonic-gate 
37317c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_SYMLINK_START,
37327c478bd9Sstevel@tonic-gate 		"ufs_symlink_start:dvp %p", dvp);
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	/*
37357c478bd9Sstevel@tonic-gate 	 * No symlinks in attrdirs at this time
37367c478bd9Sstevel@tonic-gate 	 */
37377c478bd9Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
37387c478bd9Sstevel@tonic-gate 		return (EINVAL);
37397c478bd9Sstevel@tonic-gate 
37407c478bd9Sstevel@tonic-gate again:
37417c478bd9Sstevel@tonic-gate 	ip = (struct inode *)NULL;
37427c478bd9Sstevel@tonic-gate 	vap->va_type = VLNK;
37437c478bd9Sstevel@tonic-gate 	vap->va_rdev = 0;
37447c478bd9Sstevel@tonic-gate 
37457c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SYMLINK_MASK);
37467c478bd9Sstevel@tonic-gate 	if (error)
37477c478bd9Sstevel@tonic-gate 		goto out;
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 	if (ulp)
37507c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SYMLINK,
37517c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_SYMLINK_SIZE(dip));
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	/*
37547c478bd9Sstevel@tonic-gate 	 * We must create the inode before the directory entry, to avoid
37557c478bd9Sstevel@tonic-gate 	 * racing with readlink().  ufs_dirmakeinode requires that we
37567c478bd9Sstevel@tonic-gate 	 * hold the quota lock as reader, and directory locks as writer.
37577c478bd9Sstevel@tonic-gate 	 */
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
37607c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
37617c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_contents, RW_WRITER);
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	/*
37647c478bd9Sstevel@tonic-gate 	 * Suppress any out of inodes messages if we will retry on
37657c478bd9Sstevel@tonic-gate 	 * ENOSP
37667c478bd9Sstevel@tonic-gate 	 */
37677c478bd9Sstevel@tonic-gate 	if (retry)
37687c478bd9Sstevel@tonic-gate 		dip->i_flag |= IQUIET;
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 	error = ufs_dirmakeinode(dip, &ip, vap, DE_SYMLINK, cr);
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	dip->i_flag &= ~IQUIET;
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_contents);
37757c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
37767c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
37777c478bd9Sstevel@tonic-gate 
37787c478bd9Sstevel@tonic-gate 	if (error)
37797c478bd9Sstevel@tonic-gate 		goto unlock;
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	/*
37827c478bd9Sstevel@tonic-gate 	 * OK.  The inode has been created.  Write out the data of the
37837c478bd9Sstevel@tonic-gate 	 * symbolic link.  Since symbolic links are metadata, and should
37847c478bd9Sstevel@tonic-gate 	 * remain consistent across a system crash, we need to force the
37857c478bd9Sstevel@tonic-gate 	 * data out synchronously.
37867c478bd9Sstevel@tonic-gate 	 *
37877c478bd9Sstevel@tonic-gate 	 * (This is a change from the semantics in earlier releases, which
37887c478bd9Sstevel@tonic-gate 	 * only created symbolic links synchronously if the semi-documented
37897c478bd9Sstevel@tonic-gate 	 * 'syncdir' option was set, or if we were being invoked by the NFS
37907c478bd9Sstevel@tonic-gate 	 * server, which requires symbolic links to be created synchronously.)
37917c478bd9Sstevel@tonic-gate 	 *
37927c478bd9Sstevel@tonic-gate 	 * We need to pass in a pointer for the residual length; otherwise
37937c478bd9Sstevel@tonic-gate 	 * ufs_rdwri() will always return EIO if it can't write the data,
37947c478bd9Sstevel@tonic-gate 	 * even if the error was really ENOSPC or EDQUOT.
37957c478bd9Sstevel@tonic-gate 	 */
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	ioflag = FWRITE | FDSYNC;
37987c478bd9Sstevel@tonic-gate 	residual = 0;
37997c478bd9Sstevel@tonic-gate 
38007c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
38017c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
38027c478bd9Sstevel@tonic-gate 
38037c478bd9Sstevel@tonic-gate 	/*
38047c478bd9Sstevel@tonic-gate 	 * Suppress file system full messages if we will retry
38057c478bd9Sstevel@tonic-gate 	 */
38067c478bd9Sstevel@tonic-gate 	if (retry)
38077c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 	error = ufs_rdwri(UIO_WRITE, ioflag, ip, target, strlen(target),
38107c478bd9Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, &residual, cr);
38117c478bd9Sstevel@tonic-gate 
38127c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 	if (error) {
38157c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
38167c478bd9Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
38177c478bd9Sstevel@tonic-gate 		goto remove;
38187c478bd9Sstevel@tonic-gate 	}
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	/*
38217c478bd9Sstevel@tonic-gate 	 * If the link's data is small enough, we can cache it in the inode.
38227c478bd9Sstevel@tonic-gate 	 * This is a "fast symbolic link".  We don't use the first direct
38237c478bd9Sstevel@tonic-gate 	 * block because that's actually used to point at the symbolic link's
38247c478bd9Sstevel@tonic-gate 	 * contents on disk; but we know that none of the other direct or
38257c478bd9Sstevel@tonic-gate 	 * indirect blocks can be used because symbolic links are restricted
38267c478bd9Sstevel@tonic-gate 	 * to be smaller than a file system block.
38277c478bd9Sstevel@tonic-gate 	 */
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	ASSERT(MAXPATHLEN <= VBSIZE(ITOV(ip)));
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	if (ip->i_size > 0 && ip->i_size <= FSL_SIZE) {
38327c478bd9Sstevel@tonic-gate 		if (kcopy(target, &ip->i_db[1], ip->i_size) == 0) {
38337c478bd9Sstevel@tonic-gate 			ip->i_flag |= IFASTSYMLNK;
38347c478bd9Sstevel@tonic-gate 		} else {
38357c478bd9Sstevel@tonic-gate 			int i;
38367c478bd9Sstevel@tonic-gate 			/* error, clear garbage left behind */
38377c478bd9Sstevel@tonic-gate 			for (i = 1; i < NDADDR; i++)
38387c478bd9Sstevel@tonic-gate 				ip->i_db[i] = 0;
38397c478bd9Sstevel@tonic-gate 			for (i = 0; i < NIADDR; i++)
38407c478bd9Sstevel@tonic-gate 				ip->i_ib[i] = 0;
38417c478bd9Sstevel@tonic-gate 		}
38427c478bd9Sstevel@tonic-gate 	}
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
38457c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 	/*
38487c478bd9Sstevel@tonic-gate 	 * OK.  We've successfully created the symbolic link.  All that
38497c478bd9Sstevel@tonic-gate 	 * remains is to insert it into the appropriate directory.
38507c478bd9Sstevel@tonic-gate 	 */
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
38537c478bd9Sstevel@tonic-gate 	error = ufs_direnter_lr(dip, linkname, DE_SYMLINK, NULL, ip, cr, NULL);
38547c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
38557c478bd9Sstevel@tonic-gate 
38567c478bd9Sstevel@tonic-gate 	/*
38577c478bd9Sstevel@tonic-gate 	 * Fall through into remove-on-error code.  We're either done, or we
38587c478bd9Sstevel@tonic-gate 	 * need to remove the inode (if we couldn't insert it).
38597c478bd9Sstevel@tonic-gate 	 */
38607c478bd9Sstevel@tonic-gate 
38617c478bd9Sstevel@tonic-gate remove:
38627c478bd9Sstevel@tonic-gate 	if (error && (ip != NULL)) {
38637c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
38647c478bd9Sstevel@tonic-gate 		ip->i_nlink--;
38657c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
38667c478bd9Sstevel@tonic-gate 		ip->i_seq++;
38677c478bd9Sstevel@tonic-gate 		ufs_setreclaim(ip);
38687c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
38697c478bd9Sstevel@tonic-gate 	}
38707c478bd9Sstevel@tonic-gate 
38717c478bd9Sstevel@tonic-gate unlock:
38727c478bd9Sstevel@tonic-gate 	if (ip != NULL)
38737c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
38747c478bd9Sstevel@tonic-gate 
38757c478bd9Sstevel@tonic-gate 	if (ulp) {
38767c478bd9Sstevel@tonic-gate 		int terr = 0;
38777c478bd9Sstevel@tonic-gate 
38787c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SYMLINK,
38797c478bd9Sstevel@tonic-gate 				trans_size);
38807c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
38817c478bd9Sstevel@tonic-gate 		if (error == 0)
38827c478bd9Sstevel@tonic-gate 			error = terr;
38837c478bd9Sstevel@tonic-gate 	}
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 	/*
38867c478bd9Sstevel@tonic-gate 	 * We may have failed due to lack of an inode or of a block to
38877c478bd9Sstevel@tonic-gate 	 * store the target in.  Try flushing the delete queue to free
38887c478bd9Sstevel@tonic-gate 	 * logically-available things up and try again.
38897c478bd9Sstevel@tonic-gate 	 */
38907c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
38917c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
38927c478bd9Sstevel@tonic-gate 		retry = 0;
38937c478bd9Sstevel@tonic-gate 		goto again;
38947c478bd9Sstevel@tonic-gate 	}
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate out:
38977c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_SYMLINK_END,
38987c478bd9Sstevel@tonic-gate 		"ufs_symlink_end:dvp %p error %d", dvp, error);
38997c478bd9Sstevel@tonic-gate 	return (error);
39007c478bd9Sstevel@tonic-gate }
39017c478bd9Sstevel@tonic-gate 
39027c478bd9Sstevel@tonic-gate /*
39037c478bd9Sstevel@tonic-gate  * Ufs specific routine used to do ufs io.
39047c478bd9Sstevel@tonic-gate  */
39057c478bd9Sstevel@tonic-gate int
39067c478bd9Sstevel@tonic-gate ufs_rdwri(enum uio_rw rw, int ioflag, struct inode *ip, caddr_t base,
39077c478bd9Sstevel@tonic-gate 	ssize_t len, offset_t offset, enum uio_seg seg, int *aresid,
39087c478bd9Sstevel@tonic-gate 	struct cred *cr)
39097c478bd9Sstevel@tonic-gate {
39107c478bd9Sstevel@tonic-gate 	struct uio auio;
39117c478bd9Sstevel@tonic-gate 	struct iovec aiov;
39127c478bd9Sstevel@tonic-gate 	int error;
39137c478bd9Sstevel@tonic-gate 
39147c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&auio, sizeof (uio_t));
39177c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&aiov, sizeof (iovec_t));
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 	aiov.iov_base = base;
39207c478bd9Sstevel@tonic-gate 	aiov.iov_len = len;
39217c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
39227c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
39237c478bd9Sstevel@tonic-gate 	auio.uio_loffset = offset;
39247c478bd9Sstevel@tonic-gate 	auio.uio_segflg = (short)seg;
39257c478bd9Sstevel@tonic-gate 	auio.uio_resid = len;
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
39287c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FWRITE;
39297c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
39307c478bd9Sstevel@tonic-gate 		auio.uio_llimit = curproc->p_fsz_ctl;
39317c478bd9Sstevel@tonic-gate 		error = wrip(ip, &auio, ioflag, cr);
39327c478bd9Sstevel@tonic-gate 	} else {
39337c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FREAD;
39347c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
39357c478bd9Sstevel@tonic-gate 		auio.uio_llimit = MAXOFFSET_T;
39367c478bd9Sstevel@tonic-gate 		error = rdip(ip, &auio, ioflag, cr);
39377c478bd9Sstevel@tonic-gate 	}
39387c478bd9Sstevel@tonic-gate 
39397c478bd9Sstevel@tonic-gate 	if (aresid) {
39407c478bd9Sstevel@tonic-gate 		*aresid = auio.uio_resid;
39417c478bd9Sstevel@tonic-gate 	} else if (auio.uio_resid) {
39427c478bd9Sstevel@tonic-gate 		error = EIO;
39437c478bd9Sstevel@tonic-gate 	}
39447c478bd9Sstevel@tonic-gate 	return (error);
39457c478bd9Sstevel@tonic-gate }
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate static int
39487c478bd9Sstevel@tonic-gate ufs_fid(vp, fidp)
39497c478bd9Sstevel@tonic-gate 	struct vnode *vp;
39507c478bd9Sstevel@tonic-gate 	struct fid *fidp;
39517c478bd9Sstevel@tonic-gate {
39527c478bd9Sstevel@tonic-gate 	struct ufid *ufid;
39537c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
39547c478bd9Sstevel@tonic-gate 
39557c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
39567c478bd9Sstevel@tonic-gate 		return (EIO);
39577c478bd9Sstevel@tonic-gate 
39587c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct ufid) - sizeof (ushort_t))) {
39597c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct ufid) - sizeof (ushort_t);
39607c478bd9Sstevel@tonic-gate 		return (ENOSPC);
39617c478bd9Sstevel@tonic-gate 	}
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	ufid = (struct ufid *)fidp;
39647c478bd9Sstevel@tonic-gate 	bzero((char *)ufid, sizeof (struct ufid));
39657c478bd9Sstevel@tonic-gate 	ufid->ufid_len = sizeof (struct ufid) - sizeof (ushort_t);
39667c478bd9Sstevel@tonic-gate 	ufid->ufid_ino = ip->i_number;
39677c478bd9Sstevel@tonic-gate 	ufid->ufid_gen = ip->i_gen;
39687c478bd9Sstevel@tonic-gate 
39697c478bd9Sstevel@tonic-gate 	return (0);
39707c478bd9Sstevel@tonic-gate }
39717c478bd9Sstevel@tonic-gate 
39727c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
39737c478bd9Sstevel@tonic-gate static int
39747c478bd9Sstevel@tonic-gate ufs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
39757c478bd9Sstevel@tonic-gate {
39767c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
39777c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
39787c478bd9Sstevel@tonic-gate 	int		forcedirectio;
39797c478bd9Sstevel@tonic-gate 
39807c478bd9Sstevel@tonic-gate 	/*
39817c478bd9Sstevel@tonic-gate 	 * Read case is easy.
39827c478bd9Sstevel@tonic-gate 	 */
39837c478bd9Sstevel@tonic-gate 	if (!write_lock) {
39847c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
39857c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_FALSE);
39867c478bd9Sstevel@tonic-gate 	}
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	/*
39897c478bd9Sstevel@tonic-gate 	 * Caller has requested a writer lock, but that inhibits any
39907c478bd9Sstevel@tonic-gate 	 * concurrency in the VOPs that follow. Acquire the lock shared
39917c478bd9Sstevel@tonic-gate 	 * and defer exclusive access until it is known to be needed in
39927c478bd9Sstevel@tonic-gate 	 * other VOP handlers. Some cases can be determined here.
39937c478bd9Sstevel@tonic-gate 	 */
39947c478bd9Sstevel@tonic-gate 
39957c478bd9Sstevel@tonic-gate 	/*
39967c478bd9Sstevel@tonic-gate 	 * If directio is not set, there is no chance of concurrency,
39977c478bd9Sstevel@tonic-gate 	 * so just acquire the lock exclusive. Beware of a forced
39987c478bd9Sstevel@tonic-gate 	 * unmount before looking at the mount option.
39997c478bd9Sstevel@tonic-gate 	 */
40007c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
40017c478bd9Sstevel@tonic-gate 	forcedirectio = ufsvfsp ? ufsvfsp->vfs_forcedirectio : 0;
40027c478bd9Sstevel@tonic-gate 	if (!(ip->i_flag & IDIRECTIO || forcedirectio) ||
40037c478bd9Sstevel@tonic-gate 	    !ufs_allow_shared_writes) {
40047c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
40057c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
40067c478bd9Sstevel@tonic-gate 	}
40077c478bd9Sstevel@tonic-gate 
40087c478bd9Sstevel@tonic-gate 	/*
40097c478bd9Sstevel@tonic-gate 	 * Mandatory locking forces acquiring i_rwlock exclusive.
40107c478bd9Sstevel@tonic-gate 	 */
40117c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
40127c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
40137c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
40147c478bd9Sstevel@tonic-gate 	}
40157c478bd9Sstevel@tonic-gate 
40167c478bd9Sstevel@tonic-gate 	/*
40177c478bd9Sstevel@tonic-gate 	 * Acquire the lock shared in case a concurrent write follows.
40187c478bd9Sstevel@tonic-gate 	 * Mandatory locking could have become enabled before the lock
40197c478bd9Sstevel@tonic-gate 	 * was acquired. Re-check and upgrade if needed.
40207c478bd9Sstevel@tonic-gate 	 */
40217c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_READER);
40227c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
40237c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
40247c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
40257c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
40267c478bd9Sstevel@tonic-gate 	}
40277c478bd9Sstevel@tonic-gate 	return (V_WRITELOCK_FALSE);
40287c478bd9Sstevel@tonic-gate }
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate /*ARGSUSED*/
40317c478bd9Sstevel@tonic-gate static void
40327c478bd9Sstevel@tonic-gate ufs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
40337c478bd9Sstevel@tonic-gate {
40347c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
40377c478bd9Sstevel@tonic-gate }
40387c478bd9Sstevel@tonic-gate 
40397c478bd9Sstevel@tonic-gate /* ARGSUSED */
40407c478bd9Sstevel@tonic-gate static int
40417c478bd9Sstevel@tonic-gate ufs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
40427c478bd9Sstevel@tonic-gate {
40437c478bd9Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
40447c478bd9Sstevel@tonic-gate }
40457c478bd9Sstevel@tonic-gate 
40467c478bd9Sstevel@tonic-gate /* ARGSUSED */
40477c478bd9Sstevel@tonic-gate static int
40487c478bd9Sstevel@tonic-gate ufs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
40497c478bd9Sstevel@tonic-gate 	offset_t offset, struct flk_callback *flk_cbp, struct cred *cr)
40507c478bd9Sstevel@tonic-gate {
40517c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
40547c478bd9Sstevel@tonic-gate 		return (EIO);
40557c478bd9Sstevel@tonic-gate 
40567c478bd9Sstevel@tonic-gate 	/*
40577c478bd9Sstevel@tonic-gate 	 * If file is being mapped, disallow frlock.
40587c478bd9Sstevel@tonic-gate 	 * XXX I am not holding tlock while checking i_mapcnt because the
40597c478bd9Sstevel@tonic-gate 	 * current locking strategy drops all locks before calling fs_frlock.
40607c478bd9Sstevel@tonic-gate 	 * So, mapcnt could change before we enter fs_frlock making is
40617c478bd9Sstevel@tonic-gate 	 * meaningless to have held tlock in the first place.
40627c478bd9Sstevel@tonic-gate 	 */
40637c478bd9Sstevel@tonic-gate 	if (ip->i_mapcnt > 0 && MANDLOCK(vp, ip->i_mode))
40647c478bd9Sstevel@tonic-gate 		return (EAGAIN);
40657c478bd9Sstevel@tonic-gate 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
40667c478bd9Sstevel@tonic-gate }
40677c478bd9Sstevel@tonic-gate 
40687c478bd9Sstevel@tonic-gate /* ARGSUSED */
40697c478bd9Sstevel@tonic-gate static int
40707c478bd9Sstevel@tonic-gate ufs_space(
40717c478bd9Sstevel@tonic-gate 	struct vnode *vp,
40727c478bd9Sstevel@tonic-gate 	int cmd,
40737c478bd9Sstevel@tonic-gate 	struct flock64 *bfp,
40747c478bd9Sstevel@tonic-gate 	int flag,
40757c478bd9Sstevel@tonic-gate 	offset_t offset,
40767c478bd9Sstevel@tonic-gate 	cred_t *cr,
40777c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
40787c478bd9Sstevel@tonic-gate {
40797c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp	= VTOI(vp)->i_ufsvfs;
40807c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
40817c478bd9Sstevel@tonic-gate 	int error;
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SPACE_MASK);
40847c478bd9Sstevel@tonic-gate 	if (error)
40857c478bd9Sstevel@tonic-gate 		return (error);
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	if (cmd != F_FREESP)
40897c478bd9Sstevel@tonic-gate 		error =  EINVAL;
40907c478bd9Sstevel@tonic-gate 	else if ((error = convoff(vp, bfp, 0, offset)) == 0)
40917c478bd9Sstevel@tonic-gate 		error = ufs_freesp(vp, bfp, flag, cr);
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate 	if (ulp)
40947c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
40957c478bd9Sstevel@tonic-gate 	return (error);
40967c478bd9Sstevel@tonic-gate }
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate /*
40997c478bd9Sstevel@tonic-gate  * Used to determine if read ahead should be done. Also used to
41007c478bd9Sstevel@tonic-gate  * to determine when write back occurs.
41017c478bd9Sstevel@tonic-gate  */
41027c478bd9Sstevel@tonic-gate #define	CLUSTSZ(ip)		((ip)->i_ufsvfs->vfs_ioclustsz)
41037c478bd9Sstevel@tonic-gate 
41047c478bd9Sstevel@tonic-gate /*
41057c478bd9Sstevel@tonic-gate  * A faster version of ufs_getpage.
41067c478bd9Sstevel@tonic-gate  *
41077c478bd9Sstevel@tonic-gate  * We optimize by inlining the pvn_getpages iterator, eliminating
41087c478bd9Sstevel@tonic-gate  * calls to bmap_read if file doesn't have UFS holes, and avoiding
41097c478bd9Sstevel@tonic-gate  * the overhead of page_exists().
41107c478bd9Sstevel@tonic-gate  *
41117c478bd9Sstevel@tonic-gate  * When files has UFS_HOLES and ufs_getpage is called with S_READ,
41127c478bd9Sstevel@tonic-gate  * we set *protp to PROT_READ to avoid calling bmap_read. This approach
41137c478bd9Sstevel@tonic-gate  * victimizes performance when a file with UFS holes is faulted
41147c478bd9Sstevel@tonic-gate  * first in the S_READ mode, and then in the S_WRITE mode. We will get
41157c478bd9Sstevel@tonic-gate  * two MMU faults in this case.
41167c478bd9Sstevel@tonic-gate  *
41177c478bd9Sstevel@tonic-gate  * XXX - the inode fields which control the sequential mode are not
41187c478bd9Sstevel@tonic-gate  *	 protected by any mutex. The read ahead will act wild if
41197c478bd9Sstevel@tonic-gate  *	 multiple processes will access the file concurrently and
41207c478bd9Sstevel@tonic-gate  *	 some of them in sequential mode. One particulary bad case
41217c478bd9Sstevel@tonic-gate  *	 is if another thread will change the value of i_nextrio between
41227c478bd9Sstevel@tonic-gate  *	 the time this thread tests the i_nextrio value and then reads it
41237c478bd9Sstevel@tonic-gate  *	 again to use it as the offset for the read ahead.
41247c478bd9Sstevel@tonic-gate  */
41257c478bd9Sstevel@tonic-gate static int
41267c478bd9Sstevel@tonic-gate ufs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
41277c478bd9Sstevel@tonic-gate 	page_t *plarr[], size_t plsz, struct seg *seg, caddr_t addr,
41287c478bd9Sstevel@tonic-gate 	enum seg_rw rw, struct cred *cr)
41297c478bd9Sstevel@tonic-gate {
41307c478bd9Sstevel@tonic-gate 	u_offset_t	uoff = (u_offset_t)off; /* type conversion */
41317c478bd9Sstevel@tonic-gate 	u_offset_t	pgoff;
41327c478bd9Sstevel@tonic-gate 	u_offset_t	eoff;
41337c478bd9Sstevel@tonic-gate 	struct inode 	*ip = VTOI(vp);
41347c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
41357c478bd9Sstevel@tonic-gate 	struct fs 	*fs;
41367c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
41377c478bd9Sstevel@tonic-gate 	page_t		**pl;
41387c478bd9Sstevel@tonic-gate 	caddr_t		pgaddr;
41397c478bd9Sstevel@tonic-gate 	krw_t		rwtype;
41407c478bd9Sstevel@tonic-gate 	int 		err;
41417c478bd9Sstevel@tonic-gate 	int		has_holes;
41427c478bd9Sstevel@tonic-gate 	int		beyond_eof;
41437c478bd9Sstevel@tonic-gate 	int		seqmode;
41447c478bd9Sstevel@tonic-gate 	int		pgsize = PAGESIZE;
41457c478bd9Sstevel@tonic-gate 	int		dolock;
41467c478bd9Sstevel@tonic-gate 	int		do_qlock;
41477c478bd9Sstevel@tonic-gate 	int		trans_size;
41487c478bd9Sstevel@tonic-gate 
41497c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_GETPAGE_START,
41507c478bd9Sstevel@tonic-gate 		"ufs_getpage_start:vp %p", vp);
41517c478bd9Sstevel@tonic-gate 
41527c478bd9Sstevel@tonic-gate 	ASSERT((uoff & PAGEOFFSET) == 0);
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	if (protp)
41557c478bd9Sstevel@tonic-gate 		*protp = PROT_ALL;
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate 	/*
41587c478bd9Sstevel@tonic-gate 	 * Obey the lockfs protocol
41597c478bd9Sstevel@tonic-gate 	 */
41607c478bd9Sstevel@tonic-gate 	err = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, seg,
41617c478bd9Sstevel@tonic-gate 			rw == S_READ || rw == S_EXEC, protp);
41627c478bd9Sstevel@tonic-gate 	if (err)
41637c478bd9Sstevel@tonic-gate 		goto out;
41647c478bd9Sstevel@tonic-gate 
41657c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
41667c478bd9Sstevel@tonic-gate 
41677c478bd9Sstevel@tonic-gate 	if (ulp && (rw == S_CREATE || rw == S_WRITE) &&
41687c478bd9Sstevel@tonic-gate 	    !(vp->v_flag & VISSWAP)) {
41697c478bd9Sstevel@tonic-gate 		/*
41707c478bd9Sstevel@tonic-gate 		 * Try to start a transaction, will return if blocking is
41717c478bd9Sstevel@tonic-gate 		 * expected to occur and the address space is not the
41727c478bd9Sstevel@tonic-gate 		 * kernel address space.
41737c478bd9Sstevel@tonic-gate 		 */
41747c478bd9Sstevel@tonic-gate 		trans_size = TOP_GETPAGE_SIZE(ip);
41757c478bd9Sstevel@tonic-gate 		if (seg->s_as != &kas) {
41767c478bd9Sstevel@tonic-gate 			TRANS_TRY_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE,
41777c478bd9Sstevel@tonic-gate 				trans_size, err)
41787c478bd9Sstevel@tonic-gate 			if (err == EWOULDBLOCK) {
41797c478bd9Sstevel@tonic-gate 				/*
41807c478bd9Sstevel@tonic-gate 				 * Use EDEADLK here because the VM code
41817c478bd9Sstevel@tonic-gate 				 * can normally never see this error.
41827c478bd9Sstevel@tonic-gate 				 */
41837c478bd9Sstevel@tonic-gate 				err = EDEADLK;
41847c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
41857c478bd9Sstevel@tonic-gate 				goto out;
41867c478bd9Sstevel@tonic-gate 			}
41877c478bd9Sstevel@tonic-gate 		} else {
41887c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
41897c478bd9Sstevel@tonic-gate 		}
41907c478bd9Sstevel@tonic-gate 	}
41917c478bd9Sstevel@tonic-gate 
41927c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
41937c478bd9Sstevel@tonic-gate 		err = ENOSYS;
41947c478bd9Sstevel@tonic-gate 		goto unlock;
41957c478bd9Sstevel@tonic-gate 	}
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 	seqmode = ip->i_nextr == uoff && rw != S_CREATE;
41987c478bd9Sstevel@tonic-gate 
41997c478bd9Sstevel@tonic-gate 	rwtype = RW_READER;		/* start as a reader */
42007c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
42017c478bd9Sstevel@tonic-gate 	/*
42027c478bd9Sstevel@tonic-gate 	 * If this thread owns the lock, i.e., this thread grabbed it
42037c478bd9Sstevel@tonic-gate 	 * as writer somewhere above, then we don't need to grab the
42047c478bd9Sstevel@tonic-gate 	 * lock as reader in this routine.
42057c478bd9Sstevel@tonic-gate 	 */
42067c478bd9Sstevel@tonic-gate 	do_qlock = (rw_owner(&ufsvfsp->vfs_dqrwlock) != curthread);
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate retrylock:
42097c478bd9Sstevel@tonic-gate 	if (dolock) {
42107c478bd9Sstevel@tonic-gate 		/*
42117c478bd9Sstevel@tonic-gate 		 * Grab the quota lock if we need to call
42127c478bd9Sstevel@tonic-gate 		 * bmap_write() below (with i_contents as writer).
42137c478bd9Sstevel@tonic-gate 		 */
42147c478bd9Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
42157c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
42167c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, rwtype);
42177c478bd9Sstevel@tonic-gate 	}
42187c478bd9Sstevel@tonic-gate 
42197c478bd9Sstevel@tonic-gate 	/*
42207c478bd9Sstevel@tonic-gate 	 * We may be getting called as a side effect of a bmap using
42217c478bd9Sstevel@tonic-gate 	 * fbread() when the blocks might be being allocated and the
42227c478bd9Sstevel@tonic-gate 	 * size has not yet been up'ed.  In this case we want to be
42237c478bd9Sstevel@tonic-gate 	 * able to return zero pages if we get back UFS_HOLE from
42247c478bd9Sstevel@tonic-gate 	 * calling bmap for a non write case here.  We also might have
42257c478bd9Sstevel@tonic-gate 	 * to read some frags from the disk into a page if we are
42267c478bd9Sstevel@tonic-gate 	 * extending the number of frags for a given lbn in bmap().
42277c478bd9Sstevel@tonic-gate 	 * Large Files: The read of i_size here is atomic because
42287c478bd9Sstevel@tonic-gate 	 * i_contents is held here. If dolock is zero, the lock
42297c478bd9Sstevel@tonic-gate 	 * is held in bmap routines.
42307c478bd9Sstevel@tonic-gate 	 */
42317c478bd9Sstevel@tonic-gate 	beyond_eof = uoff + len > ip->i_size + PAGEOFFSET;
42327c478bd9Sstevel@tonic-gate 	if (beyond_eof && seg != segkmap) {
42337c478bd9Sstevel@tonic-gate 		if (dolock) {
42347c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
42357c478bd9Sstevel@tonic-gate 			if (do_qlock && rwtype == RW_WRITER)
42367c478bd9Sstevel@tonic-gate 				rw_exit(&ufsvfsp->vfs_dqrwlock);
42377c478bd9Sstevel@tonic-gate 		}
42387c478bd9Sstevel@tonic-gate 		err = EFAULT;
42397c478bd9Sstevel@tonic-gate 		goto unlock;
42407c478bd9Sstevel@tonic-gate 	}
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	/*
42437c478bd9Sstevel@tonic-gate 	 * Must hold i_contents lock throughout the call to pvn_getpages
42447c478bd9Sstevel@tonic-gate 	 * since locked pages are returned from each call to ufs_getapage.
42457c478bd9Sstevel@tonic-gate 	 * Must *not* return locked pages and then try for contents lock
42467c478bd9Sstevel@tonic-gate 	 * due to lock ordering requirements (inode > page)
42477c478bd9Sstevel@tonic-gate 	 */
42487c478bd9Sstevel@tonic-gate 
42497c478bd9Sstevel@tonic-gate 	has_holes = bmap_has_holes(ip);
42507c478bd9Sstevel@tonic-gate 
42517c478bd9Sstevel@tonic-gate 	if ((rw == S_WRITE || rw == S_CREATE) && has_holes && !beyond_eof) {
42527c478bd9Sstevel@tonic-gate 		int	blk_size;
42537c478bd9Sstevel@tonic-gate 		u_offset_t offset;
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 		/*
42567c478bd9Sstevel@tonic-gate 		 * We must acquire the RW_WRITER lock in order to
42577c478bd9Sstevel@tonic-gate 		 * call bmap_write().
42587c478bd9Sstevel@tonic-gate 		 */
42597c478bd9Sstevel@tonic-gate 		if (dolock && rwtype == RW_READER) {
42607c478bd9Sstevel@tonic-gate 			rwtype = RW_WRITER;
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 			/*
42637c478bd9Sstevel@tonic-gate 			 * Grab the quota lock before
42647c478bd9Sstevel@tonic-gate 			 * upgrading i_contents, but if we can't grab it
42657c478bd9Sstevel@tonic-gate 			 * don't wait here due to lock order:
42667c478bd9Sstevel@tonic-gate 			 * vfs_dqrwlock > i_contents.
42677c478bd9Sstevel@tonic-gate 			 */
42687c478bd9Sstevel@tonic-gate 			if (do_qlock && rw_tryenter(&ufsvfsp->vfs_dqrwlock,
42697c478bd9Sstevel@tonic-gate 							RW_READER) == 0) {
42707c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
42717c478bd9Sstevel@tonic-gate 				goto retrylock;
42727c478bd9Sstevel@tonic-gate 			}
42737c478bd9Sstevel@tonic-gate 			if (!rw_tryupgrade(&ip->i_contents)) {
42747c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
42757c478bd9Sstevel@tonic-gate 				if (do_qlock)
42767c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
42777c478bd9Sstevel@tonic-gate 				goto retrylock;
42787c478bd9Sstevel@tonic-gate 			}
42797c478bd9Sstevel@tonic-gate 		}
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 		/*
42827c478bd9Sstevel@tonic-gate 		 * May be allocating disk blocks for holes here as
42837c478bd9Sstevel@tonic-gate 		 * a result of mmap faults. write(2) does the bmap_write
42847c478bd9Sstevel@tonic-gate 		 * in rdip/wrip, not here. We are not dealing with frags
42857c478bd9Sstevel@tonic-gate 		 * in this case.
42867c478bd9Sstevel@tonic-gate 		 */
42877c478bd9Sstevel@tonic-gate 		/*
42887c478bd9Sstevel@tonic-gate 		 * Large Files: We cast fs_bmask field to offset_t
42897c478bd9Sstevel@tonic-gate 		 * just as we do for MAXBMASK because uoff is a 64-bit
42907c478bd9Sstevel@tonic-gate 		 * data type. fs_bmask will still be a 32-bit type
42917c478bd9Sstevel@tonic-gate 		 * as we cannot change any ondisk data structures.
42927c478bd9Sstevel@tonic-gate 		 */
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 		offset = uoff & (offset_t)fs->fs_bmask;
42957c478bd9Sstevel@tonic-gate 		while (offset < uoff + len) {
42967c478bd9Sstevel@tonic-gate 			blk_size = (int)blksize(fs, ip, lblkno(fs, offset));
42977c478bd9Sstevel@tonic-gate 			err = bmap_write(ip, offset, blk_size, 0, cr);
42987c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD))
42997c478bd9Sstevel@tonic-gate 				ip->i_seq++;
43007c478bd9Sstevel@tonic-gate 			if (err)
43017c478bd9Sstevel@tonic-gate 				goto update_inode;
43027c478bd9Sstevel@tonic-gate 			offset += blk_size; /* XXX - make this contig */
43037c478bd9Sstevel@tonic-gate 		}
43047c478bd9Sstevel@tonic-gate 	}
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate 	/*
43077c478bd9Sstevel@tonic-gate 	 * Can be a reader from now on.
43087c478bd9Sstevel@tonic-gate 	 */
43097c478bd9Sstevel@tonic-gate 	if (dolock && rwtype == RW_WRITER) {
43107c478bd9Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
43117c478bd9Sstevel@tonic-gate 		/*
43127c478bd9Sstevel@tonic-gate 		 * We can release vfs_dqrwlock early so do it, but make
43137c478bd9Sstevel@tonic-gate 		 * sure we don't try to release it again at the bottom.
43147c478bd9Sstevel@tonic-gate 		 */
43157c478bd9Sstevel@tonic-gate 		if (do_qlock) {
43167c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
43177c478bd9Sstevel@tonic-gate 			do_qlock = 0;
43187c478bd9Sstevel@tonic-gate 		}
43197c478bd9Sstevel@tonic-gate 	}
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	/*
43227c478bd9Sstevel@tonic-gate 	 * We remove PROT_WRITE in cases when the file has UFS holes
43237c478bd9Sstevel@tonic-gate 	 * because we don't  want to call bmap_read() to check each
43247c478bd9Sstevel@tonic-gate 	 * page if it is backed with a disk block.
43257c478bd9Sstevel@tonic-gate 	 */
43267c478bd9Sstevel@tonic-gate 	if (protp && has_holes && rw != S_WRITE && rw != S_CREATE)
43277c478bd9Sstevel@tonic-gate 		*protp &= ~PROT_WRITE;
43287c478bd9Sstevel@tonic-gate 
43297c478bd9Sstevel@tonic-gate 	err = 0;
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 	/*
43327c478bd9Sstevel@tonic-gate 	 * The loop looks up pages in the range [off, off + len).
43337c478bd9Sstevel@tonic-gate 	 * For each page, we first check if we should initiate an asynchronous
43347c478bd9Sstevel@tonic-gate 	 * read ahead before we call page_lookup (we may sleep in page_lookup
43357c478bd9Sstevel@tonic-gate 	 * for a previously initiated disk read).
43367c478bd9Sstevel@tonic-gate 	 */
43377c478bd9Sstevel@tonic-gate 	eoff = (uoff + len);
43387c478bd9Sstevel@tonic-gate 	for (pgoff = uoff, pgaddr = addr, pl = plarr;
43397c478bd9Sstevel@tonic-gate 	    pgoff < eoff; /* empty */) {
43407c478bd9Sstevel@tonic-gate 		page_t	*pp;
43417c478bd9Sstevel@tonic-gate 		u_offset_t	nextrio;
43427c478bd9Sstevel@tonic-gate 		se_t	se;
43437c478bd9Sstevel@tonic-gate 		int retval;
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 		se = ((rw == S_CREATE || rw == S_OTHER) ? SE_EXCL : SE_SHARED);
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 		/* Handle async getpage (faultahead) */
43487c478bd9Sstevel@tonic-gate 		if (plarr == NULL) {
43497c478bd9Sstevel@tonic-gate 			ip->i_nextrio = pgoff;
43507c478bd9Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, pgoff, seg, pgaddr);
43517c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
43527c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
43537c478bd9Sstevel@tonic-gate 			continue;
43547c478bd9Sstevel@tonic-gate 		}
43557c478bd9Sstevel@tonic-gate 		/*
43567c478bd9Sstevel@tonic-gate 		 * Check if we should initiate read ahead of next cluster.
43577c478bd9Sstevel@tonic-gate 		 * We call page_exists only when we need to confirm that
43587c478bd9Sstevel@tonic-gate 		 * we have the current page before we initiate the read ahead.
43597c478bd9Sstevel@tonic-gate 		 */
43607c478bd9Sstevel@tonic-gate 		nextrio = ip->i_nextrio;
43617c478bd9Sstevel@tonic-gate 		if (seqmode &&
43627c478bd9Sstevel@tonic-gate 		    pgoff + CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
43637c478bd9Sstevel@tonic-gate 		    nextrio < ip->i_size && page_exists(vp, pgoff)) {
43647c478bd9Sstevel@tonic-gate 			retval = ufs_getpage_ra(vp, pgoff, seg, pgaddr);
43657c478bd9Sstevel@tonic-gate 			/*
43667c478bd9Sstevel@tonic-gate 			 * We always read ahead the next cluster of data
43677c478bd9Sstevel@tonic-gate 			 * starting from i_nextrio. If the page (vp,nextrio)
43687c478bd9Sstevel@tonic-gate 			 * is actually in core at this point, the routine
43697c478bd9Sstevel@tonic-gate 			 * ufs_getpage_ra() will stop pre-fetching data
43707c478bd9Sstevel@tonic-gate 			 * until we read that page in a synchronized manner
43717c478bd9Sstevel@tonic-gate 			 * through ufs_getpage_miss(). So, we should increase
43727c478bd9Sstevel@tonic-gate 			 * i_nextrio if the page (vp, nextrio) exists.
43737c478bd9Sstevel@tonic-gate 			 */
43747c478bd9Sstevel@tonic-gate 			if ((retval == 0) && page_exists(vp, nextrio)) {
43757c478bd9Sstevel@tonic-gate 				ip->i_nextrio = nextrio + pgsize;
43767c478bd9Sstevel@tonic-gate 			}
43777c478bd9Sstevel@tonic-gate 		}
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
43807c478bd9Sstevel@tonic-gate 			/*
43817c478bd9Sstevel@tonic-gate 			 * We found the page in the page cache.
43827c478bd9Sstevel@tonic-gate 			 */
43837c478bd9Sstevel@tonic-gate 			*pl++ = pp;
43847c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
43857c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
43867c478bd9Sstevel@tonic-gate 			len -= pgsize;
43877c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
43887c478bd9Sstevel@tonic-gate 		} else  {
43897c478bd9Sstevel@tonic-gate 			/*
43907c478bd9Sstevel@tonic-gate 			 * We have to create the page, or read it from disk.
43917c478bd9Sstevel@tonic-gate 			 */
43927c478bd9Sstevel@tonic-gate 			if (err = ufs_getpage_miss(vp, pgoff, len, seg, pgaddr,
43937c478bd9Sstevel@tonic-gate 			    pl, plsz, rw, seqmode))
43947c478bd9Sstevel@tonic-gate 				goto error;
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 			while (*pl != NULL) {
43977c478bd9Sstevel@tonic-gate 				pl++;
43987c478bd9Sstevel@tonic-gate 				pgoff += pgsize;
43997c478bd9Sstevel@tonic-gate 				pgaddr += pgsize;
44007c478bd9Sstevel@tonic-gate 				len -= pgsize;
44017c478bd9Sstevel@tonic-gate 				plsz -= pgsize;
44027c478bd9Sstevel@tonic-gate 			}
44037c478bd9Sstevel@tonic-gate 		}
44047c478bd9Sstevel@tonic-gate 	}
44057c478bd9Sstevel@tonic-gate 
44067c478bd9Sstevel@tonic-gate 	/*
44077c478bd9Sstevel@tonic-gate 	 * Return pages up to plsz if they are in the page cache.
44087c478bd9Sstevel@tonic-gate 	 * We cannot return pages if there is a chance that they are
44097c478bd9Sstevel@tonic-gate 	 * backed with a UFS hole and rw is S_WRITE or S_CREATE.
44107c478bd9Sstevel@tonic-gate 	 */
44117c478bd9Sstevel@tonic-gate 	if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 		ASSERT((protp == NULL) ||
44147c478bd9Sstevel@tonic-gate 			!(has_holes && (*protp & PROT_WRITE)));
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate 		eoff = pgoff + plsz;
44177c478bd9Sstevel@tonic-gate 		while (pgoff < eoff) {
44187c478bd9Sstevel@tonic-gate 			page_t		*pp;
44197c478bd9Sstevel@tonic-gate 
44207c478bd9Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(vp, pgoff,
44217c478bd9Sstevel@tonic-gate 			    SE_SHARED)) == NULL)
44227c478bd9Sstevel@tonic-gate 				break;
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 			*pl++ = pp;
44257c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
44267c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
44277c478bd9Sstevel@tonic-gate 		}
44287c478bd9Sstevel@tonic-gate 	}
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate 	if (plarr)
44317c478bd9Sstevel@tonic-gate 		*pl = NULL;			/* Terminate page list */
44327c478bd9Sstevel@tonic-gate 	ip->i_nextr = pgoff;
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate error:
44357c478bd9Sstevel@tonic-gate 	if (err && plarr) {
44367c478bd9Sstevel@tonic-gate 		/*
44377c478bd9Sstevel@tonic-gate 		 * Release any pages we have locked.
44387c478bd9Sstevel@tonic-gate 		 */
44397c478bd9Sstevel@tonic-gate 		while (pl > &plarr[0])
44407c478bd9Sstevel@tonic-gate 			page_unlock(*--pl);
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 		plarr[0] = NULL;
44437c478bd9Sstevel@tonic-gate 	}
44447c478bd9Sstevel@tonic-gate 
44457c478bd9Sstevel@tonic-gate update_inode:
44467c478bd9Sstevel@tonic-gate 	/*
44477c478bd9Sstevel@tonic-gate 	 * If the inode is not already marked for IACC (in rdip() for read)
44487c478bd9Sstevel@tonic-gate 	 * and the inode is not marked for no access time update (in wrip()
44497c478bd9Sstevel@tonic-gate 	 * for write) then update the inode access time and mod time now.
44507c478bd9Sstevel@tonic-gate 	 */
44517c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & (IACC | INOACC)) == 0) {
44527c478bd9Sstevel@tonic-gate 		if ((rw != S_OTHER) && (ip->i_mode & IFMT) != IFDIR) {
44537c478bd9Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
44547c478bd9Sstevel@tonic-gate 			    (fs->fs_ronly == 0) &&
44557c478bd9Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
44567c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
44577c478bd9Sstevel@tonic-gate 				ip->i_flag |= IACC;
44587c478bd9Sstevel@tonic-gate 				ITIMES_NOLOCK(ip);
44597c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
44607c478bd9Sstevel@tonic-gate 			}
44617c478bd9Sstevel@tonic-gate 		}
44627c478bd9Sstevel@tonic-gate 	}
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate 	if (dolock) {
44657c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
44667c478bd9Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
44677c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
44687c478bd9Sstevel@tonic-gate 	}
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate unlock:
44717c478bd9Sstevel@tonic-gate 	if (ulp) {
44727c478bd9Sstevel@tonic-gate 		if ((rw == S_CREATE || rw == S_WRITE) &&
44737c478bd9Sstevel@tonic-gate 		    !(vp->v_flag & VISSWAP)) {
44747c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
44757c478bd9Sstevel@tonic-gate 		}
44767c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
44777c478bd9Sstevel@tonic-gate 	}
44787c478bd9Sstevel@tonic-gate out:
44797c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_GETPAGE_END,
44807c478bd9Sstevel@tonic-gate 		"ufs_getpage_end:vp %p error %d", vp, err);
44817c478bd9Sstevel@tonic-gate 	return (err);
44827c478bd9Sstevel@tonic-gate }
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate /*
44857c478bd9Sstevel@tonic-gate  * ufs_getpage_miss is called when ufs_getpage missed the page in the page
44867c478bd9Sstevel@tonic-gate  * cache. The page is either read from the disk, or it's created.
44877c478bd9Sstevel@tonic-gate  * A page is created (without disk read) if rw == S_CREATE, or if
44887c478bd9Sstevel@tonic-gate  * the page is not backed with a real disk block (UFS hole).
44897c478bd9Sstevel@tonic-gate  */
44907c478bd9Sstevel@tonic-gate /* ARGSUSED */
44917c478bd9Sstevel@tonic-gate static int
44927c478bd9Sstevel@tonic-gate ufs_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
44937c478bd9Sstevel@tonic-gate 	caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int seq)
44947c478bd9Sstevel@tonic-gate {
44957c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
44967c478bd9Sstevel@tonic-gate 	page_t		*pp;
44977c478bd9Sstevel@tonic-gate 	daddr_t		bn;
44987c478bd9Sstevel@tonic-gate 	size_t		io_len;
44997c478bd9Sstevel@tonic-gate 	int		crpage;
45007c478bd9Sstevel@tonic-gate 	int		err;
45017c478bd9Sstevel@tonic-gate 	int		contig;
45027c478bd9Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
45037c478bd9Sstevel@tonic-gate 
45047c478bd9Sstevel@tonic-gate 	/*
45057c478bd9Sstevel@tonic-gate 	 * Figure out whether the page can be created, or must be
45067c478bd9Sstevel@tonic-gate 	 * must be read from the disk.
45077c478bd9Sstevel@tonic-gate 	 */
45087c478bd9Sstevel@tonic-gate 	if (rw == S_CREATE)
45097c478bd9Sstevel@tonic-gate 		crpage = 1;
45107c478bd9Sstevel@tonic-gate 	else {
45117c478bd9Sstevel@tonic-gate 		contig = 0;
45127c478bd9Sstevel@tonic-gate 		if (err = bmap_read(ip, off, &bn, &contig))
45137c478bd9Sstevel@tonic-gate 			return (err);
45147c478bd9Sstevel@tonic-gate 		crpage = (bn == UFS_HOLE);
45157c478bd9Sstevel@tonic-gate 	}
45167c478bd9Sstevel@tonic-gate 
45177c478bd9Sstevel@tonic-gate 	if (crpage) {
45187c478bd9Sstevel@tonic-gate 		if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT, seg,
45197c478bd9Sstevel@tonic-gate 		    addr)) == NULL) {
45207c478bd9Sstevel@tonic-gate 			return (ufs_fault(vp,
45217c478bd9Sstevel@tonic-gate 				    "ufs_getpage_miss: page_create == NULL"));
45227c478bd9Sstevel@tonic-gate 		}
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 		if (rw != S_CREATE)
45257c478bd9Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
45267c478bd9Sstevel@tonic-gate 		io_len = PAGESIZE;
45277c478bd9Sstevel@tonic-gate 	} else {
45287c478bd9Sstevel@tonic-gate 		u_offset_t	io_off;
45297c478bd9Sstevel@tonic-gate 		uint_t	xlen;
45307c478bd9Sstevel@tonic-gate 		struct buf	*bp;
45317c478bd9Sstevel@tonic-gate 		ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 		/*
45347c478bd9Sstevel@tonic-gate 		 * If access is not in sequential order, we read from disk
45357c478bd9Sstevel@tonic-gate 		 * in bsize units.
45367c478bd9Sstevel@tonic-gate 		 *
45377c478bd9Sstevel@tonic-gate 		 * We limit the size of the transfer to bsize if we are reading
45387c478bd9Sstevel@tonic-gate 		 * from the beginning of the file. Note in this situation we
45397c478bd9Sstevel@tonic-gate 		 * will hedge our bets and initiate an async read ahead of
45407c478bd9Sstevel@tonic-gate 		 * the second block.
45417c478bd9Sstevel@tonic-gate 		 */
45427c478bd9Sstevel@tonic-gate 		if (!seq || off == 0)
45437c478bd9Sstevel@tonic-gate 			contig = MIN(contig, bsize);
45447c478bd9Sstevel@tonic-gate 
45457c478bd9Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
45467c478bd9Sstevel@tonic-gate 		    &io_len, off, contig, 0);
45477c478bd9Sstevel@tonic-gate 
45487c478bd9Sstevel@tonic-gate 		/*
45497c478bd9Sstevel@tonic-gate 		 * Some other thread has entered the page.
45507c478bd9Sstevel@tonic-gate 		 * ufs_getpage will retry page_lookup.
45517c478bd9Sstevel@tonic-gate 		 */
45527c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
45537c478bd9Sstevel@tonic-gate 			pl[0] = NULL;
45547c478bd9Sstevel@tonic-gate 			return (0);
45557c478bd9Sstevel@tonic-gate 		}
45567c478bd9Sstevel@tonic-gate 
45577c478bd9Sstevel@tonic-gate 		/*
45587c478bd9Sstevel@tonic-gate 		 * Zero part of the page which we are not
45597c478bd9Sstevel@tonic-gate 		 * going to read from the disk.
45607c478bd9Sstevel@tonic-gate 		 */
45617c478bd9Sstevel@tonic-gate 		xlen = io_len & PAGEOFFSET;
45627c478bd9Sstevel@tonic-gate 		if (xlen != 0)
45637c478bd9Sstevel@tonic-gate 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
45647c478bd9Sstevel@tonic-gate 
45657c478bd9Sstevel@tonic-gate 		bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ);
45667c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
45677c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
45687c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
45697c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
45707c478bd9Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
45717c478bd9Sstevel@tonic-gate 		bp->b_offset = off;
45727c478bd9Sstevel@tonic-gate 
45737c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
45747c478bd9Sstevel@tonic-gate 			lufs_read_strategy(ufsvfsp->vfs_log, bp);
45757c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
45767c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
45777c478bd9Sstevel@tonic-gate 		} else {
45787c478bd9Sstevel@tonic-gate 			ufsvfsp->vfs_iotstamp = lbolt;
45797c478bd9Sstevel@tonic-gate 			ub.ub_getpages.value.ul++;
45807c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
45817c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
45827c478bd9Sstevel@tonic-gate 		}
45837c478bd9Sstevel@tonic-gate 
45847c478bd9Sstevel@tonic-gate 		ip->i_nextrio = off + ((io_len + PAGESIZE - 1) & PAGEMASK);
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 		/*
45877c478bd9Sstevel@tonic-gate 		 * If the file access is sequential, initiate read ahead
45887c478bd9Sstevel@tonic-gate 		 * of the next cluster.
45897c478bd9Sstevel@tonic-gate 		 */
45907c478bd9Sstevel@tonic-gate 		if (seq && ip->i_nextrio < ip->i_size)
45917c478bd9Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, off, seg, addr);
45927c478bd9Sstevel@tonic-gate 		err = biowait(bp);
45937c478bd9Sstevel@tonic-gate 		pageio_done(bp);
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 		if (err) {
45967c478bd9Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
45977c478bd9Sstevel@tonic-gate 			return (err);
45987c478bd9Sstevel@tonic-gate 		}
45997c478bd9Sstevel@tonic-gate 	}
46007c478bd9Sstevel@tonic-gate 
46017c478bd9Sstevel@tonic-gate 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
46027c478bd9Sstevel@tonic-gate 	return (0);
46037c478bd9Sstevel@tonic-gate }
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate /*
46067c478bd9Sstevel@tonic-gate  * Read ahead a cluster from the disk. Returns the length in bytes.
46077c478bd9Sstevel@tonic-gate  */
46087c478bd9Sstevel@tonic-gate static int
46097c478bd9Sstevel@tonic-gate ufs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
46107c478bd9Sstevel@tonic-gate {
46117c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
46127c478bd9Sstevel@tonic-gate 	page_t		*pp;
46137c478bd9Sstevel@tonic-gate 	u_offset_t	io_off = ip->i_nextrio;
46147c478bd9Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp;
46157c478bd9Sstevel@tonic-gate 	caddr_t		addr2 = addr + (io_off - off);
46167c478bd9Sstevel@tonic-gate 	struct buf	*bp;
46177c478bd9Sstevel@tonic-gate 	daddr_t		bn;
46187c478bd9Sstevel@tonic-gate 	size_t		io_len;
46197c478bd9Sstevel@tonic-gate 	int		contig;
46207c478bd9Sstevel@tonic-gate 	int		xlen;
46217c478bd9Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
46227c478bd9Sstevel@tonic-gate 
46237c478bd9Sstevel@tonic-gate 	/*
46247c478bd9Sstevel@tonic-gate 	 * If the directio advisory is in effect on this file,
46257c478bd9Sstevel@tonic-gate 	 * then do not do buffered read ahead. Read ahead makes
46267c478bd9Sstevel@tonic-gate 	 * it more difficult on threads using directio as they
46277c478bd9Sstevel@tonic-gate 	 * will be forced to flush the pages from this vnode.
46287c478bd9Sstevel@tonic-gate 	 */
46297c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
46307c478bd9Sstevel@tonic-gate 		return (0);
46317c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio)
46327c478bd9Sstevel@tonic-gate 		return (0);
46337c478bd9Sstevel@tonic-gate 
46347c478bd9Sstevel@tonic-gate 	/*
46357c478bd9Sstevel@tonic-gate 	 * Is this test needed?
46367c478bd9Sstevel@tonic-gate 	 */
46377c478bd9Sstevel@tonic-gate 	if (addr2 >= seg->s_base + seg->s_size)
46387c478bd9Sstevel@tonic-gate 		return (0);
46397c478bd9Sstevel@tonic-gate 
46407c478bd9Sstevel@tonic-gate 	contig = 0;
46417c478bd9Sstevel@tonic-gate 	if (bmap_read(ip, io_off, &bn, &contig) != 0 || bn == UFS_HOLE)
46427c478bd9Sstevel@tonic-gate 		return (0);
46437c478bd9Sstevel@tonic-gate 
46447c478bd9Sstevel@tonic-gate 	/*
46457c478bd9Sstevel@tonic-gate 	 * Limit the transfer size to bsize if this is the 2nd block.
46467c478bd9Sstevel@tonic-gate 	 */
46477c478bd9Sstevel@tonic-gate 	if (io_off == (u_offset_t)bsize)
46487c478bd9Sstevel@tonic-gate 		contig = MIN(contig, bsize);
46497c478bd9Sstevel@tonic-gate 
46507c478bd9Sstevel@tonic-gate 	if ((pp = pvn_read_kluster(vp, io_off, seg, addr2, &io_off,
46517c478bd9Sstevel@tonic-gate 	    &io_len, io_off, contig, 1)) == NULL)
46527c478bd9Sstevel@tonic-gate 		return (0);
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 	/*
46557c478bd9Sstevel@tonic-gate 	 * Zero part of page which we are not going to read from disk
46567c478bd9Sstevel@tonic-gate 	 */
46577c478bd9Sstevel@tonic-gate 	if ((xlen = (io_len & PAGEOFFSET)) > 0)
46587c478bd9Sstevel@tonic-gate 		pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
46597c478bd9Sstevel@tonic-gate 
46607c478bd9Sstevel@tonic-gate 	ip->i_nextrio = (io_off + io_len + PAGESIZE - 1) & PAGEMASK;
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ | B_ASYNC);
46637c478bd9Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
46647c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
46657c478bd9Sstevel@tonic-gate 	bp->b_blkno = bn;
46667c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
46677c478bd9Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
46687c478bd9Sstevel@tonic-gate 	bp->b_offset = off;
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
46717c478bd9Sstevel@tonic-gate 		lufs_read_strategy(ufsvfsp->vfs_log, bp);
46727c478bd9Sstevel@tonic-gate 	} else if (ufsvfsp->vfs_snapshot) {
46737c478bd9Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
46747c478bd9Sstevel@tonic-gate 	} else {
46757c478bd9Sstevel@tonic-gate 		ufsvfsp->vfs_iotstamp = lbolt;
46767c478bd9Sstevel@tonic-gate 		ub.ub_getras.value.ul++;
46777c478bd9Sstevel@tonic-gate 		(void) bdev_strategy(bp);
46787c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
46797c478bd9Sstevel@tonic-gate 	}
46807c478bd9Sstevel@tonic-gate 
46817c478bd9Sstevel@tonic-gate 	return (io_len);
46827c478bd9Sstevel@tonic-gate }
46837c478bd9Sstevel@tonic-gate 
46847c478bd9Sstevel@tonic-gate int	ufs_delay = 1;
46857c478bd9Sstevel@tonic-gate /*
46867c478bd9Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE, B_ASYNC}
46877c478bd9Sstevel@tonic-gate  *
46887c478bd9Sstevel@tonic-gate  * LMXXX - the inode really ought to contain a pointer to one of these
46897c478bd9Sstevel@tonic-gate  * async args.  Stuff gunk in there and just hand the whole mess off.
46907c478bd9Sstevel@tonic-gate  * This would replace i_delaylen, i_delayoff.
46917c478bd9Sstevel@tonic-gate  */
46927c478bd9Sstevel@tonic-gate /*ARGSUSED*/
46937c478bd9Sstevel@tonic-gate static int
46947c478bd9Sstevel@tonic-gate ufs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
46957c478bd9Sstevel@tonic-gate 	struct cred *cr)
46967c478bd9Sstevel@tonic-gate {
46977c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
46987c478bd9Sstevel@tonic-gate 	int err = 0;
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
47017c478bd9Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpage: bad v_count == 0"));
47027c478bd9Sstevel@tonic-gate 	}
47037c478bd9Sstevel@tonic-gate 
47047c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_PUTPAGE_START,
47057c478bd9Sstevel@tonic-gate 		"ufs_putpage_start:vp %p", vp);
47067c478bd9Sstevel@tonic-gate 
47077c478bd9Sstevel@tonic-gate 	/*
47087c478bd9Sstevel@tonic-gate 	 * XXX - Why should this check be made here?
47097c478bd9Sstevel@tonic-gate 	 */
47107c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
47117c478bd9Sstevel@tonic-gate 		err = ENOSYS;
47127c478bd9Sstevel@tonic-gate 		goto errout;
47137c478bd9Sstevel@tonic-gate 	}
47147c478bd9Sstevel@tonic-gate 
47157c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL) {
47167c478bd9Sstevel@tonic-gate 		err = EIO;
47177c478bd9Sstevel@tonic-gate 		goto errout;
47187c478bd9Sstevel@tonic-gate 	}
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 	if (flags & B_ASYNC) {
47217c478bd9Sstevel@tonic-gate 		if (ufs_delay && len &&
47227c478bd9Sstevel@tonic-gate 		    (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
47237c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
47247c478bd9Sstevel@tonic-gate 			/*
47257c478bd9Sstevel@tonic-gate 			 * If nobody stalled, start a new cluster.
47267c478bd9Sstevel@tonic-gate 			 */
47277c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen == 0) {
47287c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
47297c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
47307c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
47317c478bd9Sstevel@tonic-gate 				goto errout;
47327c478bd9Sstevel@tonic-gate 			}
47337c478bd9Sstevel@tonic-gate 			/*
47347c478bd9Sstevel@tonic-gate 			 * If we have a full cluster or they are not contig,
47357c478bd9Sstevel@tonic-gate 			 * then push last cluster and start over.
47367c478bd9Sstevel@tonic-gate 			 */
47377c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen >= CLUSTSZ(ip) ||
47387c478bd9Sstevel@tonic-gate 			    ip->i_delayoff + ip->i_delaylen != off) {
47397c478bd9Sstevel@tonic-gate 				u_offset_t doff;
47407c478bd9Sstevel@tonic-gate 				size_t dlen;
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate 				doff = ip->i_delayoff;
47437c478bd9Sstevel@tonic-gate 				dlen = ip->i_delaylen;
47447c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
47457c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
47467c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
47477c478bd9Sstevel@tonic-gate 				err = ufs_putpages(vp, doff, dlen,
47487c478bd9Sstevel@tonic-gate 				    flags, cr);
47497c478bd9Sstevel@tonic-gate 				/* LMXXX - flags are new val, not old */
47507c478bd9Sstevel@tonic-gate 				goto errout;
47517c478bd9Sstevel@tonic-gate 			}
47527c478bd9Sstevel@tonic-gate 			/*
47537c478bd9Sstevel@tonic-gate 			 * There is something there, it's not full, and
47547c478bd9Sstevel@tonic-gate 			 * it is contig.
47557c478bd9Sstevel@tonic-gate 			 */
47567c478bd9Sstevel@tonic-gate 			ip->i_delaylen += len;
47577c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
47587c478bd9Sstevel@tonic-gate 			goto errout;
47597c478bd9Sstevel@tonic-gate 		}
47607c478bd9Sstevel@tonic-gate 		/*
47617c478bd9Sstevel@tonic-gate 		 * Must have weird flags or we are not clustering.
47627c478bd9Sstevel@tonic-gate 		 */
47637c478bd9Sstevel@tonic-gate 	}
47647c478bd9Sstevel@tonic-gate 
47657c478bd9Sstevel@tonic-gate 	err = ufs_putpages(vp, off, len, flags, cr);
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate errout:
47687c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_PUTPAGE_END,
47697c478bd9Sstevel@tonic-gate 		"ufs_putpage_end:vp %p error %d", vp, err);
47707c478bd9Sstevel@tonic-gate 	return (err);
47717c478bd9Sstevel@tonic-gate }
47727c478bd9Sstevel@tonic-gate 
47737c478bd9Sstevel@tonic-gate /*
47747c478bd9Sstevel@tonic-gate  * If len == 0, do from off to EOF.
47757c478bd9Sstevel@tonic-gate  *
47767c478bd9Sstevel@tonic-gate  * The normal cases should be len == 0 & off == 0 (entire vp list),
47777c478bd9Sstevel@tonic-gate  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
47787c478bd9Sstevel@tonic-gate  * (from pageout).
47797c478bd9Sstevel@tonic-gate  */
47807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47817c478bd9Sstevel@tonic-gate static int
47827c478bd9Sstevel@tonic-gate ufs_putpages(
47837c478bd9Sstevel@tonic-gate 	struct vnode *vp,
47847c478bd9Sstevel@tonic-gate 	offset_t off,
47857c478bd9Sstevel@tonic-gate 	size_t len,
47867c478bd9Sstevel@tonic-gate 	int flags,
47877c478bd9Sstevel@tonic-gate 	struct cred *cr)
47887c478bd9Sstevel@tonic-gate {
47897c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
47907c478bd9Sstevel@tonic-gate 	u_offset_t eoff;
47917c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
47927c478bd9Sstevel@tonic-gate 	page_t *pp;
47937c478bd9Sstevel@tonic-gate 	size_t io_len;
47947c478bd9Sstevel@tonic-gate 	int err = 0;
47957c478bd9Sstevel@tonic-gate 	int dolock;
47967c478bd9Sstevel@tonic-gate 
47977c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
47987c478bd9Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpages: v_count == 0"));
47997c478bd9Sstevel@tonic-gate 	/*
48007c478bd9Sstevel@tonic-gate 	 * Acquire the readers/write inode lock before locking
48017c478bd9Sstevel@tonic-gate 	 * any pages in this inode.
48027c478bd9Sstevel@tonic-gate 	 * The inode lock is held during i/o.
48037c478bd9Sstevel@tonic-gate 	 */
48047c478bd9Sstevel@tonic-gate 	if (len == 0) {
48057c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
48067c478bd9Sstevel@tonic-gate 		ip->i_delayoff = ip->i_delaylen = 0;
48077c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
48087c478bd9Sstevel@tonic-gate 	}
48097c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
48107c478bd9Sstevel@tonic-gate 	if (dolock) {
48117c478bd9Sstevel@tonic-gate 		/*
48127c478bd9Sstevel@tonic-gate 		 * Must synchronize this thread and any possible thread
48137c478bd9Sstevel@tonic-gate 		 * operating in the window of vulnerability in wrip().
48147c478bd9Sstevel@tonic-gate 		 * It is dangerous to allow both a thread doing a putpage
48157c478bd9Sstevel@tonic-gate 		 * and a thread writing, so serialize them.  The exception
48167c478bd9Sstevel@tonic-gate 		 * is when the thread in wrip() does something which causes
48177c478bd9Sstevel@tonic-gate 		 * a putpage operation.  Then, the thread must be allowed
48187c478bd9Sstevel@tonic-gate 		 * to continue.  It may encounter a bmap_read problem in
48197c478bd9Sstevel@tonic-gate 		 * ufs_putapage, but that is handled in ufs_putapage.
48207c478bd9Sstevel@tonic-gate 		 * Allow async writers to proceed, we don't want to block
48217c478bd9Sstevel@tonic-gate 		 * the pageout daemon.
48227c478bd9Sstevel@tonic-gate 		 */
48237c478bd9Sstevel@tonic-gate 		if (ip->i_writer == curthread)
48247c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
48257c478bd9Sstevel@tonic-gate 		else {
48267c478bd9Sstevel@tonic-gate 			for (;;) {
48277c478bd9Sstevel@tonic-gate 				rw_enter(&ip->i_contents, RW_READER);
48287c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
48297c478bd9Sstevel@tonic-gate 				/*
48307c478bd9Sstevel@tonic-gate 				 * If there is no thread in the critical
48317c478bd9Sstevel@tonic-gate 				 * section of wrip(), then proceed.
48327c478bd9Sstevel@tonic-gate 				 * Otherwise, wait until there isn't one.
48337c478bd9Sstevel@tonic-gate 				 */
48347c478bd9Sstevel@tonic-gate 				if (ip->i_writer == NULL) {
48357c478bd9Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
48367c478bd9Sstevel@tonic-gate 					break;
48377c478bd9Sstevel@tonic-gate 				}
48387c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
48397c478bd9Sstevel@tonic-gate 				/*
48407c478bd9Sstevel@tonic-gate 				 * Bounce async writers when we have a writer
48417c478bd9Sstevel@tonic-gate 				 * working on this file so we don't deadlock
48427c478bd9Sstevel@tonic-gate 				 * the pageout daemon.
48437c478bd9Sstevel@tonic-gate 				 */
48447c478bd9Sstevel@tonic-gate 				if (flags & B_ASYNC) {
48457c478bd9Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
48467c478bd9Sstevel@tonic-gate 					return (0);
48477c478bd9Sstevel@tonic-gate 				}
48487c478bd9Sstevel@tonic-gate 				cv_wait(&ip->i_wrcv, &ip->i_tlock);
48497c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
48507c478bd9Sstevel@tonic-gate 			}
48517c478bd9Sstevel@tonic-gate 		}
48527c478bd9Sstevel@tonic-gate 	}
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp)) {
48557c478bd9Sstevel@tonic-gate 		if (dolock)
48567c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
48577c478bd9Sstevel@tonic-gate 		return (0);
48587c478bd9Sstevel@tonic-gate 	}
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	if (len == 0) {
48617c478bd9Sstevel@tonic-gate 		/*
48627c478bd9Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off.
48637c478bd9Sstevel@tonic-gate 		 */
48647c478bd9Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, (u_offset_t)off, ufs_putapage,
48657c478bd9Sstevel@tonic-gate 					flags, cr);
48667c478bd9Sstevel@tonic-gate 	} else {
48677c478bd9Sstevel@tonic-gate 		/*
48687c478bd9Sstevel@tonic-gate 		 * Loop over all offsets in the range looking for
48697c478bd9Sstevel@tonic-gate 		 * pages to deal with.
48707c478bd9Sstevel@tonic-gate 		 */
48717c478bd9Sstevel@tonic-gate 		if ((eoff = blkroundup(ip->i_fs, ip->i_size)) != 0)
48727c478bd9Sstevel@tonic-gate 			eoff = MIN(off + len, eoff);
48737c478bd9Sstevel@tonic-gate 		else
48747c478bd9Sstevel@tonic-gate 			eoff = off + len;
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < eoff; io_off += io_len) {
48777c478bd9Sstevel@tonic-gate 			/*
48787c478bd9Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
48797c478bd9Sstevel@tonic-gate 			 * freeing or writing pages, use the routine
48807c478bd9Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
48817c478bd9Sstevel@tonic-gate 			 * them from the free list.
48827c478bd9Sstevel@tonic-gate 			 */
48837c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
48847c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
48857c478bd9Sstevel@tonic-gate 					(flags & (B_INVAL | B_FREE)) ?
48867c478bd9Sstevel@tonic-gate 					    SE_EXCL : SE_SHARED);
48877c478bd9Sstevel@tonic-gate 			} else {
48887c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
48897c478bd9Sstevel@tonic-gate 					(flags & B_FREE) ? SE_EXCL : SE_SHARED);
48907c478bd9Sstevel@tonic-gate 			}
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
48937c478bd9Sstevel@tonic-gate 				io_len = PAGESIZE;
48947c478bd9Sstevel@tonic-gate 			else {
48957c478bd9Sstevel@tonic-gate 				u_offset_t *io_offp = &io_off;
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate 				err = ufs_putapage(vp, pp, io_offp, &io_len,
48987c478bd9Sstevel@tonic-gate 				    flags, cr);
48997c478bd9Sstevel@tonic-gate 				if (err != 0)
49007c478bd9Sstevel@tonic-gate 					break;
49017c478bd9Sstevel@tonic-gate 				/*
49027c478bd9Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
49037c478bd9Sstevel@tonic-gate 				 * the range of pages we actually wrote.
49047c478bd9Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
49057c478bd9Sstevel@tonic-gate 				 * since several pages may've been dealt
49067c478bd9Sstevel@tonic-gate 				 * with by this iteration of the loop.
49077c478bd9Sstevel@tonic-gate 				 */
49087c478bd9Sstevel@tonic-gate 			}
49097c478bd9Sstevel@tonic-gate 		}
49107c478bd9Sstevel@tonic-gate 	}
49117c478bd9Sstevel@tonic-gate 	if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
49127c478bd9Sstevel@tonic-gate 		/*
49137c478bd9Sstevel@tonic-gate 		 * We have just sync'ed back all the pages on
49147c478bd9Sstevel@tonic-gate 		 * the inode, turn off the IMODTIME flag.
49157c478bd9Sstevel@tonic-gate 		 */
49167c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
49177c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~IMODTIME;
49187c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
49197c478bd9Sstevel@tonic-gate 	}
49207c478bd9Sstevel@tonic-gate 	if (dolock)
49217c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
49227c478bd9Sstevel@tonic-gate 	return (err);
49237c478bd9Sstevel@tonic-gate }
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate static void
49267c478bd9Sstevel@tonic-gate ufs_iodone(buf_t *bp)
49277c478bd9Sstevel@tonic-gate {
49287c478bd9Sstevel@tonic-gate 	struct inode *ip;
49297c478bd9Sstevel@tonic-gate 
49307c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	bp->b_iodone = NULL;
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate 	ip = VTOI(bp->b_pages->p_vnode);
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
49377c478bd9Sstevel@tonic-gate 	if (ip->i_writes >= ufs_LW) {
49387c478bd9Sstevel@tonic-gate 		if ((ip->i_writes -= bp->b_bcount) <= ufs_LW)
49397c478bd9Sstevel@tonic-gate 			if (ufs_WRITES)
49407c478bd9Sstevel@tonic-gate 				cv_broadcast(&ip->i_wrcv); /* wake all up */
49417c478bd9Sstevel@tonic-gate 	} else {
49427c478bd9Sstevel@tonic-gate 		ip->i_writes -= bp->b_bcount;
49437c478bd9Sstevel@tonic-gate 	}
49447c478bd9Sstevel@tonic-gate 
49457c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
49467c478bd9Sstevel@tonic-gate 	iodone(bp);
49477c478bd9Sstevel@tonic-gate }
49487c478bd9Sstevel@tonic-gate 
49497c478bd9Sstevel@tonic-gate /*
49507c478bd9Sstevel@tonic-gate  * Write out a single page, possibly klustering adjacent
49517c478bd9Sstevel@tonic-gate  * dirty pages.  The inode lock must be held.
49527c478bd9Sstevel@tonic-gate  *
49537c478bd9Sstevel@tonic-gate  * LMXXX - bsize < pagesize not done.
49547c478bd9Sstevel@tonic-gate  */
49557c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49567c478bd9Sstevel@tonic-gate int
49577c478bd9Sstevel@tonic-gate ufs_putapage(
49587c478bd9Sstevel@tonic-gate 	struct vnode *vp,
49597c478bd9Sstevel@tonic-gate 	page_t *pp,
49607c478bd9Sstevel@tonic-gate 	u_offset_t *offp,
49617c478bd9Sstevel@tonic-gate 	size_t *lenp,		/* return values */
49627c478bd9Sstevel@tonic-gate 	int flags,
49637c478bd9Sstevel@tonic-gate 	struct cred *cr)
49647c478bd9Sstevel@tonic-gate {
49657c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
49667c478bd9Sstevel@tonic-gate 	u_offset_t off;
49677c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
49687c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
49697c478bd9Sstevel@tonic-gate 	struct fs *fs;
49707c478bd9Sstevel@tonic-gate 	struct buf *bp;
49717c478bd9Sstevel@tonic-gate 	size_t io_len;
49727c478bd9Sstevel@tonic-gate 	daddr_t bn;
49737c478bd9Sstevel@tonic-gate 	int err;
49747c478bd9Sstevel@tonic-gate 	int contig;
49757c478bd9Sstevel@tonic-gate 
49767c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
49777c478bd9Sstevel@tonic-gate 
49787c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_PUTAPAGE_START,
49797c478bd9Sstevel@tonic-gate 		"ufs_putapage_start:vp %p", vp);
49807c478bd9Sstevel@tonic-gate 
49817c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
49827c478bd9Sstevel@tonic-gate 		err = EIO;
49837c478bd9Sstevel@tonic-gate 		goto out_trace;
49847c478bd9Sstevel@tonic-gate 	}
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
49877c478bd9Sstevel@tonic-gate 	ASSERT(fs->fs_ronly == 0);
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate 	/*
49907c478bd9Sstevel@tonic-gate 	 * If the modified time on the inode has not already been
49917c478bd9Sstevel@tonic-gate 	 * set elsewhere (e.g. for write/setattr) we set the time now.
49927c478bd9Sstevel@tonic-gate 	 * This gives us approximate modified times for mmap'ed files
49937c478bd9Sstevel@tonic-gate 	 * which are modified via stores in the user address space.
49947c478bd9Sstevel@tonic-gate 	 */
49957c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & IMODTIME) == 0) {
49967c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
49977c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD;
49987c478bd9Sstevel@tonic-gate 		ip->i_seq++;
49997c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
50007c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
50017c478bd9Sstevel@tonic-gate 	}
50027c478bd9Sstevel@tonic-gate 
50037c478bd9Sstevel@tonic-gate 	/*
50047c478bd9Sstevel@tonic-gate 	 * Align the request to a block boundry (for old file systems),
50057c478bd9Sstevel@tonic-gate 	 * and go ask bmap() how contiguous things are for this file.
50067c478bd9Sstevel@tonic-gate 	 */
50077c478bd9Sstevel@tonic-gate 	off = pp->p_offset & (offset_t)fs->fs_bmask;	/* block align it */
50087c478bd9Sstevel@tonic-gate 	contig = 0;
50097c478bd9Sstevel@tonic-gate 	err = bmap_read(ip, off, &bn, &contig);
50107c478bd9Sstevel@tonic-gate 	if (err)
50117c478bd9Sstevel@tonic-gate 		goto out;
50127c478bd9Sstevel@tonic-gate 	if (bn == UFS_HOLE) {			/* putpage never allocates */
50137c478bd9Sstevel@tonic-gate 		/*
50147c478bd9Sstevel@tonic-gate 		 * logging device is in error mode; simply return EIO
50157c478bd9Sstevel@tonic-gate 		 */
50167c478bd9Sstevel@tonic-gate 		if (TRANS_ISERROR(ufsvfsp)) {
50177c478bd9Sstevel@tonic-gate 			err = EIO;
50187c478bd9Sstevel@tonic-gate 			goto out;
50197c478bd9Sstevel@tonic-gate 		}
50207c478bd9Sstevel@tonic-gate 		/*
50217c478bd9Sstevel@tonic-gate 		 * Oops, the thread in the window in wrip() did some
50227c478bd9Sstevel@tonic-gate 		 * sort of operation which caused a putpage in the bad
50237c478bd9Sstevel@tonic-gate 		 * range.  In this case, just return an error which will
50247c478bd9Sstevel@tonic-gate 		 * cause the software modified bit on the page to set
50257c478bd9Sstevel@tonic-gate 		 * and the page will get written out again later.
50267c478bd9Sstevel@tonic-gate 		 */
50277c478bd9Sstevel@tonic-gate 		if (ip->i_writer == curthread) {
50287c478bd9Sstevel@tonic-gate 			err = EIO;
50297c478bd9Sstevel@tonic-gate 			goto out;
50307c478bd9Sstevel@tonic-gate 		}
50317c478bd9Sstevel@tonic-gate 		/*
50327c478bd9Sstevel@tonic-gate 		 * If the pager is trying to push a page in the bad range
50337c478bd9Sstevel@tonic-gate 		 * just tell him to try again later when things are better.
50347c478bd9Sstevel@tonic-gate 		 */
50357c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
50367c478bd9Sstevel@tonic-gate 			err = EAGAIN;
50377c478bd9Sstevel@tonic-gate 			goto out;
50387c478bd9Sstevel@tonic-gate 		}
50397c478bd9Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip), "ufs_putapage: bn == UFS_HOLE");
50407c478bd9Sstevel@tonic-gate 		goto out;
50417c478bd9Sstevel@tonic-gate 	}
50427c478bd9Sstevel@tonic-gate 
50437c478bd9Sstevel@tonic-gate 	/*
50447c478bd9Sstevel@tonic-gate 	 * Take the length (of contiguous bytes) passed back from bmap()
50457c478bd9Sstevel@tonic-gate 	 * and _try_ and get a set of pages covering that extent.
50467c478bd9Sstevel@tonic-gate 	 */
50477c478bd9Sstevel@tonic-gate 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, off, contig, flags);
50487c478bd9Sstevel@tonic-gate 
50497c478bd9Sstevel@tonic-gate 	/*
50507c478bd9Sstevel@tonic-gate 	 * May have run out of memory and not clustered backwards.
50517c478bd9Sstevel@tonic-gate 	 * off		p_offset
50527c478bd9Sstevel@tonic-gate 	 * [  pp - 1  ][   pp   ]
50537c478bd9Sstevel@tonic-gate 	 * [	block		]
50547c478bd9Sstevel@tonic-gate 	 * We told bmap off, so we have to adjust the bn accordingly.
50557c478bd9Sstevel@tonic-gate 	 */
50567c478bd9Sstevel@tonic-gate 	if (io_off > off) {
50577c478bd9Sstevel@tonic-gate 		bn += btod(io_off - off);
50587c478bd9Sstevel@tonic-gate 		contig -= (io_off - off);
50597c478bd9Sstevel@tonic-gate 	}
50607c478bd9Sstevel@tonic-gate 
50617c478bd9Sstevel@tonic-gate 	/*
50627c478bd9Sstevel@tonic-gate 	 * bmap was carefull to tell us the right size so use that.
50637c478bd9Sstevel@tonic-gate 	 * There might be unallocated frags at the end.
50647c478bd9Sstevel@tonic-gate 	 * LMXXX - bzero the end of the page?  We must be writing after EOF.
50657c478bd9Sstevel@tonic-gate 	 */
50667c478bd9Sstevel@tonic-gate 	if (io_len > contig) {
50677c478bd9Sstevel@tonic-gate 		ASSERT(io_len - contig < fs->fs_bsize);
50687c478bd9Sstevel@tonic-gate 		io_len -= (io_len - contig);
50697c478bd9Sstevel@tonic-gate 	}
50707c478bd9Sstevel@tonic-gate 
50717c478bd9Sstevel@tonic-gate 	/*
50727c478bd9Sstevel@tonic-gate 	 * Handle the case where we are writing the last page after EOF.
50737c478bd9Sstevel@tonic-gate 	 *
50747c478bd9Sstevel@tonic-gate 	 * XXX - just a patch for i-mt3.
50757c478bd9Sstevel@tonic-gate 	 */
50767c478bd9Sstevel@tonic-gate 	if (io_len == 0) {
50777c478bd9Sstevel@tonic-gate 		ASSERT(pp->p_offset >= (u_offset_t)(roundup(ip->i_size,
50787c478bd9Sstevel@tonic-gate 							    PAGESIZE)));
50797c478bd9Sstevel@tonic-gate 		io_len = PAGESIZE;
50807c478bd9Sstevel@tonic-gate 	}
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_WRITE | flags);
50837c478bd9Sstevel@tonic-gate 
50847c478bd9Sstevel@tonic-gate 	ULOCKFS_SET_MOD(ITOUL(ip));
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
50877c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
50887c478bd9Sstevel@tonic-gate 	bp->b_blkno = bn;
50897c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
50907c478bd9Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
50917c478bd9Sstevel@tonic-gate 
50927c478bd9Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
50937c478bd9Sstevel@tonic-gate 		if ((ip->i_mode & IFMT) == IFSHAD) {
50947c478bd9Sstevel@tonic-gate 			TRANS_BUF(ufsvfsp, 0, io_len, bp, DT_SHAD);
50957c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_qinod == ip) {
50967c478bd9Sstevel@tonic-gate 			TRANS_DELTA(ufsvfsp, ldbtob(bn), bp->b_bcount, DT_QR,
50977c478bd9Sstevel@tonic-gate 			    0, 0);
50987c478bd9Sstevel@tonic-gate 		}
50997c478bd9Sstevel@tonic-gate 	}
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 	/* write throttle */
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_iodone == NULL);
51047c478bd9Sstevel@tonic-gate 	bp->b_iodone = (int (*)())ufs_iodone;
51057c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
51067c478bd9Sstevel@tonic-gate 	ip->i_writes += bp->b_bcount;
51077c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
51087c478bd9Sstevel@tonic-gate 
51097c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ASYNC) {
51107c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
51117c478bd9Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
51127c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
51137c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
51147c478bd9Sstevel@tonic-gate 		} else {
51157c478bd9Sstevel@tonic-gate 			ufsvfsp->vfs_iotstamp = lbolt;
51167c478bd9Sstevel@tonic-gate 			ub.ub_putasyncs.value.ul++;
51177c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
51187c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
51197c478bd9Sstevel@tonic-gate 		}
51207c478bd9Sstevel@tonic-gate 	} else {
51217c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
51227c478bd9Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
51237c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
51247c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
51257c478bd9Sstevel@tonic-gate 		} else {
51267c478bd9Sstevel@tonic-gate 			ufsvfsp->vfs_iotstamp = lbolt;
51277c478bd9Sstevel@tonic-gate 			ub.ub_putsyncs.value.ul++;
51287c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
51297c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
51307c478bd9Sstevel@tonic-gate 		}
51317c478bd9Sstevel@tonic-gate 		err = biowait(bp);
51327c478bd9Sstevel@tonic-gate 		pageio_done(bp);
51337c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
51347c478bd9Sstevel@tonic-gate 	}
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	pp = NULL;
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate out:
51397c478bd9Sstevel@tonic-gate 	if (err != 0 && pp != NULL)
51407c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, B_ERROR | B_WRITE | flags);
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 	if (offp)
51437c478bd9Sstevel@tonic-gate 		*offp = io_off;
51447c478bd9Sstevel@tonic-gate 	if (lenp)
51457c478bd9Sstevel@tonic-gate 		*lenp = io_len;
51467c478bd9Sstevel@tonic-gate out_trace:
51477c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_PUTAPAGE_END,
51487c478bd9Sstevel@tonic-gate 		"ufs_putapage_end:vp %p error %d", vp, err);
51497c478bd9Sstevel@tonic-gate 	return (err);
51507c478bd9Sstevel@tonic-gate }
51517c478bd9Sstevel@tonic-gate 
51527c478bd9Sstevel@tonic-gate /* ARGSUSED */
51537c478bd9Sstevel@tonic-gate static int
51547c478bd9Sstevel@tonic-gate ufs_map(struct vnode *vp,
51557c478bd9Sstevel@tonic-gate 	offset_t off,
51567c478bd9Sstevel@tonic-gate 	struct as *as,
51577c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
51587c478bd9Sstevel@tonic-gate 	size_t len,
51597c478bd9Sstevel@tonic-gate 	uchar_t prot,
51607c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
51617c478bd9Sstevel@tonic-gate 	uint_t flags,
51627c478bd9Sstevel@tonic-gate 	struct cred *cr)
51637c478bd9Sstevel@tonic-gate {
51647c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
51657c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
51667c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
51677c478bd9Sstevel@tonic-gate 	int error;
51687c478bd9Sstevel@tonic-gate 
51697c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_MAP_START,
51707c478bd9Sstevel@tonic-gate 		"ufs_map_start:vp %p", vp);
51717c478bd9Sstevel@tonic-gate 
51727c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MAP_MASK);
51737c478bd9Sstevel@tonic-gate 	if (error)
51747c478bd9Sstevel@tonic-gate 		goto out;
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
51777c478bd9Sstevel@tonic-gate 		error = ENOSYS;
51787c478bd9Sstevel@tonic-gate 		goto unlock;
51797c478bd9Sstevel@tonic-gate 	}
51807c478bd9Sstevel@tonic-gate 
51817c478bd9Sstevel@tonic-gate 	if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0) {
51827c478bd9Sstevel@tonic-gate 		error = ENXIO;
51837c478bd9Sstevel@tonic-gate 		goto unlock;
51847c478bd9Sstevel@tonic-gate 	}
51857c478bd9Sstevel@tonic-gate 
51867c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
51877c478bd9Sstevel@tonic-gate 		error = ENODEV;
51887c478bd9Sstevel@tonic-gate 		goto unlock;
51897c478bd9Sstevel@tonic-gate 	}
51907c478bd9Sstevel@tonic-gate 
51917c478bd9Sstevel@tonic-gate 	/*
51927c478bd9Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
51937c478bd9Sstevel@tonic-gate 	 */
51947c478bd9Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOI(vp)->i_mode)) {
51957c478bd9Sstevel@tonic-gate 		error = EAGAIN;
51967c478bd9Sstevel@tonic-gate 		goto unlock;
51977c478bd9Sstevel@tonic-gate 	}
51987c478bd9Sstevel@tonic-gate 
51997c478bd9Sstevel@tonic-gate 	as_rangelock(as);
52007c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
52017c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, off, 1, flags);
52027c478bd9Sstevel@tonic-gate 		if (*addrp == NULL) {
52037c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
52047c478bd9Sstevel@tonic-gate 			error = ENOMEM;
52057c478bd9Sstevel@tonic-gate 			goto unlock;
52067c478bd9Sstevel@tonic-gate 		}
52077c478bd9Sstevel@tonic-gate 	} else {
52087c478bd9Sstevel@tonic-gate 		/*
52097c478bd9Sstevel@tonic-gate 		 * User specified address - blow away any previous mappings
52107c478bd9Sstevel@tonic-gate 		 */
52117c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
52127c478bd9Sstevel@tonic-gate 	}
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate 	vn_a.vp = vp;
52157c478bd9Sstevel@tonic-gate 	vn_a.offset = (u_offset_t)off;
52167c478bd9Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
52177c478bd9Sstevel@tonic-gate 	vn_a.prot = prot;
52187c478bd9Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
52197c478bd9Sstevel@tonic-gate 	vn_a.cred = cr;
52207c478bd9Sstevel@tonic-gate 	vn_a.amp = NULL;
52217c478bd9Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
52227c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
52237c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
52247c478bd9Sstevel@tonic-gate 
52257c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
52267c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
52277c478bd9Sstevel@tonic-gate 
52287c478bd9Sstevel@tonic-gate unlock:
52297c478bd9Sstevel@tonic-gate 	if (ulp) {
52307c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
52317c478bd9Sstevel@tonic-gate 	}
52327c478bd9Sstevel@tonic-gate out:
52337c478bd9Sstevel@tonic-gate 	TRACE_2(TR_FAC_UFS, TR_UFS_MAP_END,
52347c478bd9Sstevel@tonic-gate 		"ufs_map_end:vp %p error %d", vp, error);
52357c478bd9Sstevel@tonic-gate 	return (error);
52367c478bd9Sstevel@tonic-gate }
52377c478bd9Sstevel@tonic-gate 
52387c478bd9Sstevel@tonic-gate /* ARGSUSED */
52397c478bd9Sstevel@tonic-gate static int
52407c478bd9Sstevel@tonic-gate ufs_addmap(struct vnode *vp,
52417c478bd9Sstevel@tonic-gate 	offset_t off,
52427c478bd9Sstevel@tonic-gate 	struct as *as,
52437c478bd9Sstevel@tonic-gate 	caddr_t addr,
52447c478bd9Sstevel@tonic-gate 	size_t	len,
52457c478bd9Sstevel@tonic-gate 	uchar_t  prot,
52467c478bd9Sstevel@tonic-gate 	uchar_t  maxprot,
52477c478bd9Sstevel@tonic-gate 	uint_t    flags,
52487c478bd9Sstevel@tonic-gate 	struct cred *cr)
52497c478bd9Sstevel@tonic-gate {
52507c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
52517c478bd9Sstevel@tonic-gate 
52527c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
52537c478bd9Sstevel@tonic-gate 		return (ENOSYS);
52547c478bd9Sstevel@tonic-gate 	}
52557c478bd9Sstevel@tonic-gate 
52567c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
52577c478bd9Sstevel@tonic-gate 	ip->i_mapcnt += btopr(len);
52587c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
52597c478bd9Sstevel@tonic-gate 	return (0);
52607c478bd9Sstevel@tonic-gate }
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52637c478bd9Sstevel@tonic-gate static int
52647c478bd9Sstevel@tonic-gate ufs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
52657c478bd9Sstevel@tonic-gate 	size_t len, uint_t prot,  uint_t maxprot,  uint_t flags,
52667c478bd9Sstevel@tonic-gate 	struct cred *cr)
52677c478bd9Sstevel@tonic-gate {
52687c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
52697c478bd9Sstevel@tonic-gate 
52707c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
52717c478bd9Sstevel@tonic-gate 		return (ENOSYS);
52727c478bd9Sstevel@tonic-gate 	}
52737c478bd9Sstevel@tonic-gate 
52747c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
52757c478bd9Sstevel@tonic-gate 	ip->i_mapcnt -= btopr(len); 	/* Count released mappings */
52767c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_mapcnt >= 0);
52777c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
52787c478bd9Sstevel@tonic-gate 	return (0);
52797c478bd9Sstevel@tonic-gate }
52807c478bd9Sstevel@tonic-gate /*
52817c478bd9Sstevel@tonic-gate  * Return the answer requested to poll() for non-device files
52827c478bd9Sstevel@tonic-gate  */
52837c478bd9Sstevel@tonic-gate struct pollhead ufs_pollhd;
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate /* ARGSUSED */
52867c478bd9Sstevel@tonic-gate int
52877c478bd9Sstevel@tonic-gate ufs_poll(vnode_t *vp, short ev, int any, short *revp, struct pollhead **phpp)
52887c478bd9Sstevel@tonic-gate {
52897c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
52907c478bd9Sstevel@tonic-gate 
52917c478bd9Sstevel@tonic-gate 	*revp = 0;
52927c478bd9Sstevel@tonic-gate 	ufsvfsp = VTOI(vp)->i_ufsvfs;
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 	if (!ufsvfsp) {
52957c478bd9Sstevel@tonic-gate 		*revp = POLLHUP;
52967c478bd9Sstevel@tonic-gate 		goto out;
52977c478bd9Sstevel@tonic-gate 	}
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs) ||
53007c478bd9Sstevel@tonic-gate 	    ULOCKFS_IS_ELOCK(&ufsvfsp->vfs_ulockfs)) {
53017c478bd9Sstevel@tonic-gate 		*revp |= POLLERR;
53027c478bd9Sstevel@tonic-gate 
53037c478bd9Sstevel@tonic-gate 	} else {
53047c478bd9Sstevel@tonic-gate 		if ((ev & POLLOUT) && !ufsvfsp->vfs_fs->fs_ronly &&
53057c478bd9Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
53067c478bd9Sstevel@tonic-gate 			*revp |= POLLOUT;
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate 		if ((ev & POLLWRBAND) && !ufsvfsp->vfs_fs->fs_ronly &&
53097c478bd9Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
53107c478bd9Sstevel@tonic-gate 			*revp |= POLLWRBAND;
53117c478bd9Sstevel@tonic-gate 
53127c478bd9Sstevel@tonic-gate 		if (ev & POLLIN)
53137c478bd9Sstevel@tonic-gate 			*revp |= POLLIN;
53147c478bd9Sstevel@tonic-gate 
53157c478bd9Sstevel@tonic-gate 		if (ev & POLLRDNORM)
53167c478bd9Sstevel@tonic-gate 			*revp |= POLLRDNORM;
53177c478bd9Sstevel@tonic-gate 
53187c478bd9Sstevel@tonic-gate 		if (ev & POLLRDBAND)
53197c478bd9Sstevel@tonic-gate 			*revp |= POLLRDBAND;
53207c478bd9Sstevel@tonic-gate 	}
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 	if ((ev & POLLPRI) && (*revp & (POLLERR|POLLHUP)))
53237c478bd9Sstevel@tonic-gate 		*revp |= POLLPRI;
53247c478bd9Sstevel@tonic-gate out:
53257c478bd9Sstevel@tonic-gate 	*phpp = !any && !*revp ? &ufs_pollhd : (struct pollhead *)NULL;
53267c478bd9Sstevel@tonic-gate 
53277c478bd9Sstevel@tonic-gate 	return (0);
53287c478bd9Sstevel@tonic-gate }
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate /* ARGSUSED */
53317c478bd9Sstevel@tonic-gate static int
53327c478bd9Sstevel@tonic-gate ufs_l_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr)
53337c478bd9Sstevel@tonic-gate {
53347c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
53357c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
53367c478bd9Sstevel@tonic-gate 	struct inode 	*sip = NULL;
53377c478bd9Sstevel@tonic-gate 	int		error;
53387c478bd9Sstevel@tonic-gate 	struct inode 	*ip = VTOI(vp);
53397c478bd9Sstevel@tonic-gate 	int		issync;
53407c478bd9Sstevel@tonic-gate 
53417c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_PATHCONF_MASK);
53427c478bd9Sstevel@tonic-gate 	if (error)
53437c478bd9Sstevel@tonic-gate 		return (error);
53447c478bd9Sstevel@tonic-gate 
53457c478bd9Sstevel@tonic-gate 	switch (cmd) {
53467c478bd9Sstevel@tonic-gate 		/*
53477c478bd9Sstevel@tonic-gate 		 * Have to handle _PC_NAME_MAX here, because the normal way
53487c478bd9Sstevel@tonic-gate 		 * [fs_pathconf() -> VOP_STATVFS() -> ufs_statvfs()]
53497c478bd9Sstevel@tonic-gate 		 * results in a lock ordering reversal between
53507c478bd9Sstevel@tonic-gate 		 * ufs_lockfs_{begin,end}() and
53517c478bd9Sstevel@tonic-gate 		 * ufs_thread_{suspend,continue}().
53527c478bd9Sstevel@tonic-gate 		 *
53537c478bd9Sstevel@tonic-gate 		 * Keep in sync with ufs_statvfs().
53547c478bd9Sstevel@tonic-gate 		 */
53557c478bd9Sstevel@tonic-gate 	case _PC_NAME_MAX:
53567c478bd9Sstevel@tonic-gate 		*valp = MAXNAMLEN;
53577c478bd9Sstevel@tonic-gate 		break;
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 	case _PC_FILESIZEBITS:
53607c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
53617c478bd9Sstevel@tonic-gate 			*valp = UFS_FILESIZE_BITS;
53627c478bd9Sstevel@tonic-gate 		else
53637c478bd9Sstevel@tonic-gate 			*valp = 32;
53647c478bd9Sstevel@tonic-gate 		break;
53657c478bd9Sstevel@tonic-gate 
53667c478bd9Sstevel@tonic-gate 	case _PC_XATTR_EXISTS:
53677c478bd9Sstevel@tonic-gate 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 			error = ufs_xattr_getattrdir(vp, &sip, LOOKUP_XATTR,
53707c478bd9Sstevel@tonic-gate 							cr);
53717c478bd9Sstevel@tonic-gate 			if (error ==  0 && sip != NULL) {
53727c478bd9Sstevel@tonic-gate 				/* Start transaction */
53737c478bd9Sstevel@tonic-gate 				if (ulp) {
53747c478bd9Sstevel@tonic-gate 					TRANS_BEGIN_CSYNC(ufsvfsp, issync,
53757c478bd9Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
53767c478bd9Sstevel@tonic-gate 				}
53777c478bd9Sstevel@tonic-gate 				/*
53787c478bd9Sstevel@tonic-gate 				 * Is directory empty
53797c478bd9Sstevel@tonic-gate 				 */
53807c478bd9Sstevel@tonic-gate 				rw_enter(&sip->i_rwlock, RW_WRITER);
53817c478bd9Sstevel@tonic-gate 				rw_enter(&sip->i_contents, RW_WRITER);
53827c478bd9Sstevel@tonic-gate 				if (ufs_xattrdirempty(sip,
53837c478bd9Sstevel@tonic-gate 						sip->i_number, CRED())) {
53847c478bd9Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
53857c478bd9Sstevel@tonic-gate 					ufs_unhook_shadow(ip, sip);
53867c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
53877c478bd9Sstevel@tonic-gate 
53887c478bd9Sstevel@tonic-gate 					*valp = 0;
53897c478bd9Sstevel@tonic-gate 
53907c478bd9Sstevel@tonic-gate 				} else
53917c478bd9Sstevel@tonic-gate 					*valp = 1;
53927c478bd9Sstevel@tonic-gate 				rw_exit(&sip->i_contents);
53937c478bd9Sstevel@tonic-gate 				rw_exit(&sip->i_rwlock);
53947c478bd9Sstevel@tonic-gate 				if (ulp) {
53957c478bd9Sstevel@tonic-gate 					TRANS_END_CSYNC(ufsvfsp, error, issync,
53967c478bd9Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
53977c478bd9Sstevel@tonic-gate 				}
53987c478bd9Sstevel@tonic-gate 				VN_RELE(ITOV(sip));
53997c478bd9Sstevel@tonic-gate 			} else if (error == ENOENT) {
54007c478bd9Sstevel@tonic-gate 				*valp = 0;
54017c478bd9Sstevel@tonic-gate 				error = 0;
54027c478bd9Sstevel@tonic-gate 			}
54037c478bd9Sstevel@tonic-gate 		} else {
54047c478bd9Sstevel@tonic-gate 			error = fs_pathconf(vp, cmd, valp, cr);
54057c478bd9Sstevel@tonic-gate 		}
54067c478bd9Sstevel@tonic-gate 		break;
54077c478bd9Sstevel@tonic-gate 
54087c478bd9Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
54097c478bd9Sstevel@tonic-gate 		*valp = _ACL_ACLENT_ENABLED;
54107c478bd9Sstevel@tonic-gate 		break;
54117c478bd9Sstevel@tonic-gate 
54127c478bd9Sstevel@tonic-gate 	case _PC_MIN_HOLE_SIZE:
54137c478bd9Sstevel@tonic-gate 		*valp = (ulong_t)ip->i_fs->fs_bsize;
54147c478bd9Sstevel@tonic-gate 		break;
54157c478bd9Sstevel@tonic-gate 
54167c478bd9Sstevel@tonic-gate 	default:
54177c478bd9Sstevel@tonic-gate 		error = fs_pathconf(vp, cmd, valp, cr);
54187c478bd9Sstevel@tonic-gate 	}
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 	if (ulp != NULL) {
54217c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
54227c478bd9Sstevel@tonic-gate 	}
54237c478bd9Sstevel@tonic-gate 	return (error);
54247c478bd9Sstevel@tonic-gate }
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate int ufs_pageio_writes, ufs_pageio_reads;
54277c478bd9Sstevel@tonic-gate 
54287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
54297c478bd9Sstevel@tonic-gate static int
54307c478bd9Sstevel@tonic-gate ufs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
54317c478bd9Sstevel@tonic-gate 	int flags, struct cred *cr)
54327c478bd9Sstevel@tonic-gate {
54337c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
54347c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
54357c478bd9Sstevel@tonic-gate 	page_t *npp = NULL, *opp = NULL, *cpp = pp;
54367c478bd9Sstevel@tonic-gate 	struct buf *bp;
54377c478bd9Sstevel@tonic-gate 	daddr_t bn;
54387c478bd9Sstevel@tonic-gate 	size_t done_len = 0, cur_len = 0;
54397c478bd9Sstevel@tonic-gate 	int err = 0;
54407c478bd9Sstevel@tonic-gate 	int contig = 0;
54417c478bd9Sstevel@tonic-gate 	int dolock;
54427c478bd9Sstevel@tonic-gate 	int vmpss = 0;
5443*bc69f433Saguzovsk 	struct ulockfs *ulp;
54447c478bd9Sstevel@tonic-gate 
54457c478bd9Sstevel@tonic-gate 	if ((flags & B_READ) && pp != NULL && pp->p_vnode == vp &&
54467c478bd9Sstevel@tonic-gate 	    vp->v_mpssdata != NULL) {
54477c478bd9Sstevel@tonic-gate 		vmpss = 1;
54487c478bd9Sstevel@tonic-gate 	}
54497c478bd9Sstevel@tonic-gate 
54507c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
54517c478bd9Sstevel@tonic-gate 	/*
54527c478bd9Sstevel@tonic-gate 	 * We need a better check.  Ideally, we would use another
54537c478bd9Sstevel@tonic-gate 	 * vnodeops so that hlocked and forcibly unmounted file
54547c478bd9Sstevel@tonic-gate 	 * systems would return EIO where appropriate and w/o the
54557c478bd9Sstevel@tonic-gate 	 * need for these checks.
54567c478bd9Sstevel@tonic-gate 	 */
54577c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
54587c478bd9Sstevel@tonic-gate 		return (EIO);
54597c478bd9Sstevel@tonic-gate 
5460*bc69f433Saguzovsk 	/*
5461*bc69f433Saguzovsk 	 * For vmpss (pp can be NULL) case respect the quiesce protocol.
5462*bc69f433Saguzovsk 	 * ul_lock must be taken before locking pages so we can't use it here
5463*bc69f433Saguzovsk 	 * if pp is non NULL because segvn already locked pages
5464*bc69f433Saguzovsk 	 * SE_EXCL. Instead we rely on the fact that a forced umount or
5465*bc69f433Saguzovsk 	 * applying a filesystem lock via ufs_fiolfs() will block in the
5466*bc69f433Saguzovsk 	 * implicit call to ufs_flush() until we unlock the pages after the
5467*bc69f433Saguzovsk 	 * return to segvn. Other ufs_quiesce() callers keep ufs_quiesce_pend
5468*bc69f433Saguzovsk 	 * above 0 until they are done. We have to be careful not to increment
5469*bc69f433Saguzovsk 	 * ul_vnops_cnt here after forceful unmount hlocks the file system.
5470*bc69f433Saguzovsk 	 *
5471*bc69f433Saguzovsk 	 * If pp is NULL use ul_lock to make sure we don't increment
5472*bc69f433Saguzovsk 	 * ul_vnops_cnt after forceful unmount hlocks the file system.
5473*bc69f433Saguzovsk 	 */
5474*bc69f433Saguzovsk 	if (vmpss || pp == NULL) {
5475*bc69f433Saguzovsk 		ulp = &ufsvfsp->vfs_ulockfs;
5476*bc69f433Saguzovsk 		if (pp == NULL)
5477*bc69f433Saguzovsk 			mutex_enter(&ulp->ul_lock);
5478*bc69f433Saguzovsk 		if (ulp->ul_fs_lock & ULOCKFS_GETREAD_MASK) {
5479*bc69f433Saguzovsk 			if (pp == NULL) {
5480*bc69f433Saguzovsk 				mutex_exit(&ulp->ul_lock);
5481*bc69f433Saguzovsk 			}
5482*bc69f433Saguzovsk 			return (vmpss ? EIO : EINVAL);
5483*bc69f433Saguzovsk 		}
5484*bc69f433Saguzovsk 		atomic_add_long(&ulp->ul_vnops_cnt, 1);
5485*bc69f433Saguzovsk 		if (pp == NULL)
5486*bc69f433Saguzovsk 			mutex_exit(&ulp->ul_lock);
5487*bc69f433Saguzovsk 		if (ufs_quiesce_pend) {
5488*bc69f433Saguzovsk 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5489*bc69f433Saguzovsk 				cv_broadcast(&ulp->ul_cv);
5490*bc69f433Saguzovsk 			return (vmpss ? EIO : EINVAL);
5491*bc69f433Saguzovsk 		}
5492*bc69f433Saguzovsk 	}
5493*bc69f433Saguzovsk 
54947c478bd9Sstevel@tonic-gate 	if (dolock) {
54957c478bd9Sstevel@tonic-gate 		/*
54967c478bd9Sstevel@tonic-gate 		 * segvn may call VOP_PAGEIO() instead of VOP_GETPAGE() to
54977c478bd9Sstevel@tonic-gate 		 * handle a fault against a segment that maps vnode pages with
54987c478bd9Sstevel@tonic-gate 		 * large mappings.  Segvn creates pages and holds them locked
54997c478bd9Sstevel@tonic-gate 		 * SE_EXCL during VOP_PAGEIO() call. In this case we have to
55007c478bd9Sstevel@tonic-gate 		 * use rw_tryenter() to avoid a potential deadlock since in
55017c478bd9Sstevel@tonic-gate 		 * lock order i_contents needs to be taken first.
55027c478bd9Sstevel@tonic-gate 		 * Segvn will retry via VOP_GETPAGE() if VOP_PAGEIO() fails.
55037c478bd9Sstevel@tonic-gate 		 */
55047c478bd9Sstevel@tonic-gate 		if (!vmpss) {
55057c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
55067c478bd9Sstevel@tonic-gate 		} else if (!rw_tryenter(&ip->i_contents, RW_READER)) {
5507*bc69f433Saguzovsk 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5508*bc69f433Saguzovsk 				cv_broadcast(&ulp->ul_cv);
55097c478bd9Sstevel@tonic-gate 			return (EDEADLK);
55107c478bd9Sstevel@tonic-gate 		}
55117c478bd9Sstevel@tonic-gate 	}
55127c478bd9Sstevel@tonic-gate 
551337fbc076Saguzovsk 	/*
551437fbc076Saguzovsk 	 * Return an error to segvn because the pagefault request is beyond
551537fbc076Saguzovsk 	 * PAGESIZE rounded EOF.
551637fbc076Saguzovsk 	 */
551737fbc076Saguzovsk 	if (vmpss && btopr(io_off + io_len) > btopr(ip->i_size)) {
551837fbc076Saguzovsk 		if (dolock)
551937fbc076Saguzovsk 			rw_exit(&ip->i_contents);
5520*bc69f433Saguzovsk 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5521*bc69f433Saguzovsk 			cv_broadcast(&ulp->ul_cv);
552237fbc076Saguzovsk 		return (EFAULT);
552337fbc076Saguzovsk 	}
552437fbc076Saguzovsk 
55257c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
55267c478bd9Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
55277c478bd9Sstevel@tonic-gate 			err = ENOSYS;
55287c478bd9Sstevel@tonic-gate 		} else {
55297c478bd9Sstevel@tonic-gate 			err = EINVAL;
55307c478bd9Sstevel@tonic-gate 		}
55317c478bd9Sstevel@tonic-gate 		if (dolock)
55327c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5533*bc69f433Saguzovsk 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5534*bc69f433Saguzovsk 			cv_broadcast(&ulp->ul_cv);
55357c478bd9Sstevel@tonic-gate 		return (err);
55367c478bd9Sstevel@tonic-gate 	}
55377c478bd9Sstevel@tonic-gate 
55387c478bd9Sstevel@tonic-gate 	/*
55397c478bd9Sstevel@tonic-gate 	 * Break the io request into chunks, one for each contiguous
55407c478bd9Sstevel@tonic-gate 	 * stretch of disk blocks in the target file.
55417c478bd9Sstevel@tonic-gate 	 */
55427c478bd9Sstevel@tonic-gate 	while (done_len < io_len) {
55437c478bd9Sstevel@tonic-gate 		ASSERT(cpp);
55447c478bd9Sstevel@tonic-gate 		contig = 0;
55457c478bd9Sstevel@tonic-gate 		if (err = bmap_read(ip, (u_offset_t)(io_off + done_len),
55467c478bd9Sstevel@tonic-gate 				    &bn, &contig))
55477c478bd9Sstevel@tonic-gate 			break;
55487c478bd9Sstevel@tonic-gate 
55497c478bd9Sstevel@tonic-gate 		if (bn == UFS_HOLE) {	/* No holey swapfiles */
55507c478bd9Sstevel@tonic-gate 			if (vmpss) {
55517c478bd9Sstevel@tonic-gate 				err = EFAULT;
55527c478bd9Sstevel@tonic-gate 				break;
55537c478bd9Sstevel@tonic-gate 			}
55547c478bd9Sstevel@tonic-gate 			err = ufs_fault(ITOV(ip), "ufs_pageio: bn == UFS_HOLE");
55557c478bd9Sstevel@tonic-gate 			break;
55567c478bd9Sstevel@tonic-gate 		}
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 		cur_len = MIN(io_len - done_len, contig);
55597c478bd9Sstevel@tonic-gate 		/*
55607c478bd9Sstevel@tonic-gate 		 * Zero out a page beyond EOF, when the last block of
55617c478bd9Sstevel@tonic-gate 		 * a file is a UFS fragment so that ufs_pageio() can be used
55627c478bd9Sstevel@tonic-gate 		 * instead of ufs_getpage() to handle faults against
55637c478bd9Sstevel@tonic-gate 		 * segvn segments that use large pages.
55647c478bd9Sstevel@tonic-gate 		 */
55657c478bd9Sstevel@tonic-gate 		page_list_break(&cpp, &npp, btopr(cur_len));
55667c478bd9Sstevel@tonic-gate 		if ((flags & B_READ) && (cur_len & PAGEOFFSET)) {
55677c478bd9Sstevel@tonic-gate 			size_t xlen = cur_len & PAGEOFFSET;
55687c478bd9Sstevel@tonic-gate 			pagezero(cpp->p_prev, xlen, PAGESIZE - xlen);
55697c478bd9Sstevel@tonic-gate 		}
55707c478bd9Sstevel@tonic-gate 
55717c478bd9Sstevel@tonic-gate 		bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
55727c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL);
55737c478bd9Sstevel@tonic-gate 
55747c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
55757c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
55767c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
55777c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
55787c478bd9Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
55797c478bd9Sstevel@tonic-gate 
55807c478bd9Sstevel@tonic-gate 		ufsvfsp->vfs_iotstamp = lbolt;
55817c478bd9Sstevel@tonic-gate 		ub.ub_pageios.value.ul++;
55827c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_snapshot)
55837c478bd9Sstevel@tonic-gate 			fssnap_strategy(&(ufsvfsp->vfs_snapshot), bp);
55847c478bd9Sstevel@tonic-gate 		else
55857c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
55867c478bd9Sstevel@tonic-gate 
55877c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
55887c478bd9Sstevel@tonic-gate 			ufs_pageio_reads++;
55897c478bd9Sstevel@tonic-gate 		else
55907c478bd9Sstevel@tonic-gate 			ufs_pageio_writes++;
55917c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
55927c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
55937c478bd9Sstevel@tonic-gate 		else
55947c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
55957c478bd9Sstevel@tonic-gate 		/*
55967c478bd9Sstevel@tonic-gate 		 * If the request is not B_ASYNC, wait for i/o to complete
55977c478bd9Sstevel@tonic-gate 		 * and re-assemble the page list to return to the caller.
55987c478bd9Sstevel@tonic-gate 		 * If it is B_ASYNC we leave the page list in pieces and
55997c478bd9Sstevel@tonic-gate 		 * cleanup() will dispose of them.
56007c478bd9Sstevel@tonic-gate 		 */
56017c478bd9Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
56027c478bd9Sstevel@tonic-gate 			err = biowait(bp);
56037c478bd9Sstevel@tonic-gate 			pageio_done(bp);
56047c478bd9Sstevel@tonic-gate 			if (err)
56057c478bd9Sstevel@tonic-gate 				break;
56067c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
56077c478bd9Sstevel@tonic-gate 		}
56087c478bd9Sstevel@tonic-gate 		cpp = npp;
56097c478bd9Sstevel@tonic-gate 		npp = NULL;
56107c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
56117c478bd9Sstevel@tonic-gate 			cur_len = P2ROUNDUP_TYPED(cur_len, PAGESIZE, size_t);
56127c478bd9Sstevel@tonic-gate 		done_len += cur_len;
56137c478bd9Sstevel@tonic-gate 	}
56147c478bd9Sstevel@tonic-gate 	ASSERT(err || (cpp == NULL && npp == NULL && done_len == io_len));
56157c478bd9Sstevel@tonic-gate 	if (err) {
56167c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
56177c478bd9Sstevel@tonic-gate 			/* Cleanup unprocessed parts of list */
56187c478bd9Sstevel@tonic-gate 			page_list_concat(&cpp, &npp);
56197c478bd9Sstevel@tonic-gate 			if (flags & B_READ)
56207c478bd9Sstevel@tonic-gate 				pvn_read_done(cpp, B_ERROR);
56217c478bd9Sstevel@tonic-gate 			else
56227c478bd9Sstevel@tonic-gate 				pvn_write_done(cpp, B_ERROR);
56237c478bd9Sstevel@tonic-gate 		} else {
56247c478bd9Sstevel@tonic-gate 			/* Re-assemble list and let caller clean up */
56257c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
56267c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &npp);
56277c478bd9Sstevel@tonic-gate 		}
56287c478bd9Sstevel@tonic-gate 	}
5629*bc69f433Saguzovsk 
5630*bc69f433Saguzovsk 	if (vmpss && !(ip->i_flag & IACC) && !ULOCKFS_IS_NOIACC(ulp) &&
5631*bc69f433Saguzovsk 	    ufsvfsp->vfs_fs->fs_ronly == 0 && !ufsvfsp->vfs_noatime) {
5632*bc69f433Saguzovsk 		mutex_enter(&ip->i_tlock);
5633*bc69f433Saguzovsk 		ip->i_flag |= IACC;
5634*bc69f433Saguzovsk 		ITIMES_NOLOCK(ip);
5635*bc69f433Saguzovsk 		mutex_exit(&ip->i_tlock);
5636*bc69f433Saguzovsk 	}
5637*bc69f433Saguzovsk 
56387c478bd9Sstevel@tonic-gate 	if (dolock)
56397c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
5640*bc69f433Saguzovsk 	if (vmpss && !atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5641*bc69f433Saguzovsk 		cv_broadcast(&ulp->ul_cv);
56427c478bd9Sstevel@tonic-gate 	return (err);
56437c478bd9Sstevel@tonic-gate }
56447c478bd9Sstevel@tonic-gate 
56457c478bd9Sstevel@tonic-gate /*
56467c478bd9Sstevel@tonic-gate  * Called when the kernel is in a frozen state to dump data
56477c478bd9Sstevel@tonic-gate  * directly to the device. It uses a private dump data structure,
56487c478bd9Sstevel@tonic-gate  * set up by dump_ctl, to locate the correct disk block to which to dump.
56497c478bd9Sstevel@tonic-gate  */
56507c478bd9Sstevel@tonic-gate static int
56517c478bd9Sstevel@tonic-gate ufs_dump(vnode_t *vp, caddr_t addr, int ldbn, int dblks)
56527c478bd9Sstevel@tonic-gate {
56537c478bd9Sstevel@tonic-gate 	u_offset_t	file_size;
56547c478bd9Sstevel@tonic-gate 	struct inode    *ip = VTOI(vp);
56557c478bd9Sstevel@tonic-gate 	struct fs	*fs = ip->i_fs;
56567c478bd9Sstevel@tonic-gate 	daddr_t		dbn, lfsbn;
56577c478bd9Sstevel@tonic-gate 	int		disk_blks = fs->fs_bsize >> DEV_BSHIFT;
56587c478bd9Sstevel@tonic-gate 	int		error = 0;
56597c478bd9Sstevel@tonic-gate 	int		ndbs, nfsbs;
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 	/*
56627c478bd9Sstevel@tonic-gate 	 * forced unmount case
56637c478bd9Sstevel@tonic-gate 	 */
56647c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
56657c478bd9Sstevel@tonic-gate 		return (EIO);
56667c478bd9Sstevel@tonic-gate 	/*
56677c478bd9Sstevel@tonic-gate 	 * Validate the inode that it has not been modified since
56687c478bd9Sstevel@tonic-gate 	 * the dump structure is allocated.
56697c478bd9Sstevel@tonic-gate 	 */
56707c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
56717c478bd9Sstevel@tonic-gate 	if ((dump_info == NULL) ||
56727c478bd9Sstevel@tonic-gate 	    (dump_info->ip != ip) ||
56737c478bd9Sstevel@tonic-gate 	    (dump_info->time.tv_sec != ip->i_mtime.tv_sec) ||
56747c478bd9Sstevel@tonic-gate 	    (dump_info->time.tv_usec != ip->i_mtime.tv_usec)) {
56757c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
56767c478bd9Sstevel@tonic-gate 		return (-1);
56777c478bd9Sstevel@tonic-gate 	}
56787c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
56797c478bd9Sstevel@tonic-gate 
56807c478bd9Sstevel@tonic-gate 	/*
56817c478bd9Sstevel@tonic-gate 	 * See that the file has room for this write
56827c478bd9Sstevel@tonic-gate 	 */
56837c478bd9Sstevel@tonic-gate 	UFS_GET_ISIZE(&file_size, ip);
56847c478bd9Sstevel@tonic-gate 
56857c478bd9Sstevel@tonic-gate 	if (ldbtob((offset_t)(ldbn + dblks)) > file_size)
56867c478bd9Sstevel@tonic-gate 		return (ENOSPC);
56877c478bd9Sstevel@tonic-gate 
56887c478bd9Sstevel@tonic-gate 	/*
56897c478bd9Sstevel@tonic-gate 	 * Find the physical disk block numbers from the dump
56907c478bd9Sstevel@tonic-gate 	 * private data structure directly and write out the data
56917c478bd9Sstevel@tonic-gate 	 * in contiguous block lumps
56927c478bd9Sstevel@tonic-gate 	 */
56937c478bd9Sstevel@tonic-gate 	while (dblks > 0 && !error) {
56947c478bd9Sstevel@tonic-gate 		lfsbn = (daddr_t)lblkno(fs, ldbtob((offset_t)ldbn));
56957c478bd9Sstevel@tonic-gate 		dbn = fsbtodb(fs, dump_info->dblk[lfsbn]) + ldbn % disk_blks;
56967c478bd9Sstevel@tonic-gate 		nfsbs = 1;
56977c478bd9Sstevel@tonic-gate 		ndbs = disk_blks - ldbn % disk_blks;
56987c478bd9Sstevel@tonic-gate 		while (ndbs < dblks && fsbtodb(fs, dump_info->dblk[lfsbn +
56997c478bd9Sstevel@tonic-gate 		    nfsbs]) == dbn + ndbs) {
57007c478bd9Sstevel@tonic-gate 			nfsbs++;
57017c478bd9Sstevel@tonic-gate 			ndbs += disk_blks;
57027c478bd9Sstevel@tonic-gate 		}
57037c478bd9Sstevel@tonic-gate 		if (ndbs > dblks)
57047c478bd9Sstevel@tonic-gate 			ndbs = dblks;
57057c478bd9Sstevel@tonic-gate 		error = bdev_dump(ip->i_dev, addr, dbn, ndbs);
57067c478bd9Sstevel@tonic-gate 		addr += ldbtob((offset_t)ndbs);
57077c478bd9Sstevel@tonic-gate 		dblks -= ndbs;
57087c478bd9Sstevel@tonic-gate 		ldbn += ndbs;
57097c478bd9Sstevel@tonic-gate 	}
57107c478bd9Sstevel@tonic-gate 	return (error);
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate }
57137c478bd9Sstevel@tonic-gate 
57147c478bd9Sstevel@tonic-gate /*
57157c478bd9Sstevel@tonic-gate  * Prepare the file system before and after the dump operation.
57167c478bd9Sstevel@tonic-gate  *
57177c478bd9Sstevel@tonic-gate  * action = DUMP_ALLOC:
57187c478bd9Sstevel@tonic-gate  * Preparation before dump, allocate dump private data structure
57197c478bd9Sstevel@tonic-gate  * to hold all the direct and indirect block info for dump.
57207c478bd9Sstevel@tonic-gate  *
57217c478bd9Sstevel@tonic-gate  * action = DUMP_FREE:
57227c478bd9Sstevel@tonic-gate  * Clean up after dump, deallocate the dump private data structure.
57237c478bd9Sstevel@tonic-gate  *
57247c478bd9Sstevel@tonic-gate  * action = DUMP_SCAN:
57257c478bd9Sstevel@tonic-gate  * Scan dump_info for *blkp DEV_BSIZE blocks of contig fs space;
57267c478bd9Sstevel@tonic-gate  * if found, the starting file-relative DEV_BSIZE lbn is written
57277c478bd9Sstevel@tonic-gate  * to *bklp; that lbn is intended for use with VOP_DUMP()
57287c478bd9Sstevel@tonic-gate  */
57297c478bd9Sstevel@tonic-gate static int
57307c478bd9Sstevel@tonic-gate ufs_dumpctl(vnode_t *vp, int action, int *blkp)
57317c478bd9Sstevel@tonic-gate {
57327c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
57337c478bd9Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
57347c478bd9Sstevel@tonic-gate 	struct fs	*fs;
57357c478bd9Sstevel@tonic-gate 	daddr32_t	*dblk, *storeblk;
57367c478bd9Sstevel@tonic-gate 	daddr32_t	*nextblk, *endblk;
57377c478bd9Sstevel@tonic-gate 	struct buf	*bp;
57387c478bd9Sstevel@tonic-gate 	int		i, entry, entries;
57397c478bd9Sstevel@tonic-gate 	int		n, ncontig;
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 	/*
57427c478bd9Sstevel@tonic-gate 	 * check for forced unmount
57437c478bd9Sstevel@tonic-gate 	 */
57447c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
57457c478bd9Sstevel@tonic-gate 		return (EIO);
57467c478bd9Sstevel@tonic-gate 
57477c478bd9Sstevel@tonic-gate 	if (action == DUMP_ALLOC) {
57487c478bd9Sstevel@tonic-gate 		/*
57497c478bd9Sstevel@tonic-gate 		 * alloc and record dump_info
57507c478bd9Sstevel@tonic-gate 		 */
57517c478bd9Sstevel@tonic-gate 		if (dump_info != NULL)
57527c478bd9Sstevel@tonic-gate 			return (EINVAL);
57537c478bd9Sstevel@tonic-gate 
57547c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VREG);
57557c478bd9Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
57567c478bd9Sstevel@tonic-gate 
57577c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
57587c478bd9Sstevel@tonic-gate 
57597c478bd9Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
57607c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
57617c478bd9Sstevel@tonic-gate 			return (EFAULT);
57627c478bd9Sstevel@tonic-gate 		}
57637c478bd9Sstevel@tonic-gate 
57647c478bd9Sstevel@tonic-gate 		/*
57657c478bd9Sstevel@tonic-gate 		 * calculate and allocate space needed according to i_size
57667c478bd9Sstevel@tonic-gate 		 */
57677c478bd9Sstevel@tonic-gate 		entries = (int)lblkno(fs, blkroundup(fs, ip->i_size));
57687c478bd9Sstevel@tonic-gate 		if ((dump_info = (struct dump *)
57697c478bd9Sstevel@tonic-gate 		    kmem_alloc(sizeof (struct dump) +
57707c478bd9Sstevel@tonic-gate 		    (entries - 1) * sizeof (daddr32_t), KM_NOSLEEP)) == NULL) {
57717c478bd9Sstevel@tonic-gate 			    rw_exit(&ip->i_contents);
57727c478bd9Sstevel@tonic-gate 			    return (ENOMEM);
57737c478bd9Sstevel@tonic-gate 		}
57747c478bd9Sstevel@tonic-gate 
57757c478bd9Sstevel@tonic-gate 		/* Start saving the info */
57767c478bd9Sstevel@tonic-gate 		dump_info->fsbs = entries;
57777c478bd9Sstevel@tonic-gate 		dump_info->ip = ip;
57787c478bd9Sstevel@tonic-gate 		storeblk = &dump_info->dblk[0];
57797c478bd9Sstevel@tonic-gate 
57807c478bd9Sstevel@tonic-gate 		/* Direct Blocks */
57817c478bd9Sstevel@tonic-gate 		for (entry = 0; entry < NDADDR && entry < entries; entry++)
57827c478bd9Sstevel@tonic-gate 			*storeblk++ = ip->i_db[entry];
57837c478bd9Sstevel@tonic-gate 
57847c478bd9Sstevel@tonic-gate 		/* Indirect Blocks */
57857c478bd9Sstevel@tonic-gate 		for (i = 0; i < NIADDR; i++) {
57867c478bd9Sstevel@tonic-gate 			int error = 0;
57877c478bd9Sstevel@tonic-gate 
57887c478bd9Sstevel@tonic-gate 			bp = UFS_BREAD(ufsvfsp,
57897c478bd9Sstevel@tonic-gate 				ip->i_dev, fsbtodb(fs, ip->i_ib[i]),
57907c478bd9Sstevel@tonic-gate 				fs->fs_bsize);
57917c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR)
57927c478bd9Sstevel@tonic-gate 				error = EIO;
57937c478bd9Sstevel@tonic-gate 			else {
57947c478bd9Sstevel@tonic-gate 				dblk = bp->b_un.b_daddr;
57957c478bd9Sstevel@tonic-gate 				if ((storeblk = save_dblks(ip, ufsvfsp,
57967c478bd9Sstevel@tonic-gate 				    storeblk, dblk, i, entries)) == NULL)
57977c478bd9Sstevel@tonic-gate 					error = EIO;
57987c478bd9Sstevel@tonic-gate 			}
57997c478bd9Sstevel@tonic-gate 
58007c478bd9Sstevel@tonic-gate 			brelse(bp);
58017c478bd9Sstevel@tonic-gate 
58027c478bd9Sstevel@tonic-gate 			if (error != 0) {
58037c478bd9Sstevel@tonic-gate 				kmem_free(dump_info, sizeof (struct dump) +
58047c478bd9Sstevel@tonic-gate 				    (entries - 1) * sizeof (daddr32_t));
58057c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
58067c478bd9Sstevel@tonic-gate 				dump_info = NULL;
58077c478bd9Sstevel@tonic-gate 				return (error);
58087c478bd9Sstevel@tonic-gate 			}
58097c478bd9Sstevel@tonic-gate 		}
58107c478bd9Sstevel@tonic-gate 		/* and time stamp the information */
58117c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
58127c478bd9Sstevel@tonic-gate 		dump_info->time = ip->i_mtime;
58137c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
58147c478bd9Sstevel@tonic-gate 
58157c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
58167c478bd9Sstevel@tonic-gate 	} else if (action == DUMP_FREE) {
58177c478bd9Sstevel@tonic-gate 		/*
58187c478bd9Sstevel@tonic-gate 		 * free dump_info
58197c478bd9Sstevel@tonic-gate 		 */
58207c478bd9Sstevel@tonic-gate 		if (dump_info == NULL)
58217c478bd9Sstevel@tonic-gate 			return (EINVAL);
58227c478bd9Sstevel@tonic-gate 		entries = dump_info->fsbs - 1;
58237c478bd9Sstevel@tonic-gate 		kmem_free(dump_info, sizeof (struct dump) +
58247c478bd9Sstevel@tonic-gate 		    entries * sizeof (daddr32_t));
58257c478bd9Sstevel@tonic-gate 		dump_info = NULL;
58267c478bd9Sstevel@tonic-gate 	} else if (action == DUMP_SCAN) {
58277c478bd9Sstevel@tonic-gate 		/*
58287c478bd9Sstevel@tonic-gate 		 * scan dump_info
58297c478bd9Sstevel@tonic-gate 		 */
58307c478bd9Sstevel@tonic-gate 		if (dump_info == NULL)
58317c478bd9Sstevel@tonic-gate 			return (EINVAL);
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 		dblk = dump_info->dblk;
58347c478bd9Sstevel@tonic-gate 		nextblk = dblk + 1;
58357c478bd9Sstevel@tonic-gate 		endblk = dblk + dump_info->fsbs - 1;
58367c478bd9Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
58377c478bd9Sstevel@tonic-gate 		ncontig = *blkp >> (fs->fs_bshift - DEV_BSHIFT);
58387c478bd9Sstevel@tonic-gate 
58397c478bd9Sstevel@tonic-gate 		/*
58407c478bd9Sstevel@tonic-gate 		 * scan dblk[] entries; contig fs space is found when:
58417c478bd9Sstevel@tonic-gate 		 * ((current blkno + frags per block) == next blkno)
58427c478bd9Sstevel@tonic-gate 		 */
58437c478bd9Sstevel@tonic-gate 		n = 0;
58447c478bd9Sstevel@tonic-gate 		while (n < ncontig && dblk < endblk) {
58457c478bd9Sstevel@tonic-gate 			if ((*dblk + fs->fs_frag) == *nextblk)
58467c478bd9Sstevel@tonic-gate 				n++;
58477c478bd9Sstevel@tonic-gate 			else
58487c478bd9Sstevel@tonic-gate 				n = 0;
58497c478bd9Sstevel@tonic-gate 			dblk++;
58507c478bd9Sstevel@tonic-gate 			nextblk++;
58517c478bd9Sstevel@tonic-gate 		}
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate 		/*
58547c478bd9Sstevel@tonic-gate 		 * index is where size bytes of contig space begins;
58557c478bd9Sstevel@tonic-gate 		 * conversion from index to the file's DEV_BSIZE lbn
58567c478bd9Sstevel@tonic-gate 		 * is equivalent to:  (index * fs_bsize) / DEV_BSIZE
58577c478bd9Sstevel@tonic-gate 		 */
58587c478bd9Sstevel@tonic-gate 		if (n == ncontig) {
58597c478bd9Sstevel@tonic-gate 			i = (dblk - dump_info->dblk) - ncontig;
58607c478bd9Sstevel@tonic-gate 			*blkp = i << (fs->fs_bshift - DEV_BSHIFT);
58617c478bd9Sstevel@tonic-gate 		} else
58627c478bd9Sstevel@tonic-gate 			return (EFAULT);
58637c478bd9Sstevel@tonic-gate 	}
58647c478bd9Sstevel@tonic-gate 	return (0);
58657c478bd9Sstevel@tonic-gate }
58667c478bd9Sstevel@tonic-gate 
58677c478bd9Sstevel@tonic-gate /*
58687c478bd9Sstevel@tonic-gate  * Recursive helper function for ufs_dumpctl().  It follows the indirect file
58697c478bd9Sstevel@tonic-gate  * system  blocks until it reaches the the disk block addresses, which are
58707c478bd9Sstevel@tonic-gate  * then stored into the given buffer, storeblk.
58717c478bd9Sstevel@tonic-gate  */
58727c478bd9Sstevel@tonic-gate static daddr32_t *
58737c478bd9Sstevel@tonic-gate save_dblks(struct inode *ip, struct ufsvfs *ufsvfsp,  daddr32_t *storeblk,
58747c478bd9Sstevel@tonic-gate     daddr32_t *dblk, int level, int entries)
58757c478bd9Sstevel@tonic-gate {
58767c478bd9Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
58777c478bd9Sstevel@tonic-gate 	struct buf	*bp;
58787c478bd9Sstevel@tonic-gate 	int		i;
58797c478bd9Sstevel@tonic-gate 
58807c478bd9Sstevel@tonic-gate 	if (level == 0) {
58817c478bd9Sstevel@tonic-gate 		for (i = 0; i < NINDIR(fs); i++) {
58827c478bd9Sstevel@tonic-gate 			if (storeblk - dump_info->dblk >= entries)
58837c478bd9Sstevel@tonic-gate 				break;
58847c478bd9Sstevel@tonic-gate 			*storeblk++ = dblk[i];
58857c478bd9Sstevel@tonic-gate 		}
58867c478bd9Sstevel@tonic-gate 		return (storeblk);
58877c478bd9Sstevel@tonic-gate 	}
58887c478bd9Sstevel@tonic-gate 	for (i = 0; i < NINDIR(fs); i++) {
58897c478bd9Sstevel@tonic-gate 		if (storeblk - dump_info->dblk >= entries)
58907c478bd9Sstevel@tonic-gate 			break;
58917c478bd9Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp,
58927c478bd9Sstevel@tonic-gate 				ip->i_dev, fsbtodb(fs, dblk[i]), fs->fs_bsize);
58937c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
58947c478bd9Sstevel@tonic-gate 			brelse(bp);
58957c478bd9Sstevel@tonic-gate 			return (NULL);
58967c478bd9Sstevel@tonic-gate 		}
58977c478bd9Sstevel@tonic-gate 		storeblk = save_dblks(ip, ufsvfsp, storeblk, bp->b_un.b_daddr,
58987c478bd9Sstevel@tonic-gate 		    level - 1, entries);
58997c478bd9Sstevel@tonic-gate 		brelse(bp);
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate 		if (storeblk == NULL)
59027c478bd9Sstevel@tonic-gate 			return (NULL);
59037c478bd9Sstevel@tonic-gate 	}
59047c478bd9Sstevel@tonic-gate 	return (storeblk);
59057c478bd9Sstevel@tonic-gate }
59067c478bd9Sstevel@tonic-gate 
59077c478bd9Sstevel@tonic-gate /* ARGSUSED */
59087c478bd9Sstevel@tonic-gate static int
59097c478bd9Sstevel@tonic-gate ufs_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag,
59107c478bd9Sstevel@tonic-gate 	struct cred *cr)
59117c478bd9Sstevel@tonic-gate {
59127c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
59137c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
59147c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
59157c478bd9Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
59167c478bd9Sstevel@tonic-gate 	int		err = EINVAL;
59177c478bd9Sstevel@tonic-gate 
59187c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_GETSECATTR_START,
59197c478bd9Sstevel@tonic-gate 	    "ufs_getsecattr_start:vp %p, vsap %p, flags %x", vp, vsap, flag);
59207c478bd9Sstevel@tonic-gate 
59217c478bd9Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
59227c478bd9Sstevel@tonic-gate 
59237c478bd9Sstevel@tonic-gate 	/*
59247c478bd9Sstevel@tonic-gate 	 * Only grab locks if needed - they're not needed to check vsa_mask
59257c478bd9Sstevel@tonic-gate 	 * or if the mask contains no acl flags.
59267c478bd9Sstevel@tonic-gate 	 */
59277c478bd9Sstevel@tonic-gate 	if (vsa_mask != 0) {
59287c478bd9Sstevel@tonic-gate 		if (err = ufs_lockfs_begin(ufsvfsp, &ulp,
59297c478bd9Sstevel@tonic-gate 		    ULOCKFS_GETATTR_MASK))
59307c478bd9Sstevel@tonic-gate 			return (err);
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
59337c478bd9Sstevel@tonic-gate 		err = ufs_acl_get(ip, vsap, flag, cr);
59347c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
59357c478bd9Sstevel@tonic-gate 
59367c478bd9Sstevel@tonic-gate 		if (ulp)
59377c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
59387c478bd9Sstevel@tonic-gate 	}
59397c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_GETSECATTR_END,
59407c478bd9Sstevel@tonic-gate 	    "ufs_getsecattr_end:vp %p", vp);
59417c478bd9Sstevel@tonic-gate 	return (err);
59427c478bd9Sstevel@tonic-gate }
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate /* ARGSUSED */
59457c478bd9Sstevel@tonic-gate static int
59467c478bd9Sstevel@tonic-gate ufs_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
59477c478bd9Sstevel@tonic-gate {
59487c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
59497c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
59507c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
59517c478bd9Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
59527c478bd9Sstevel@tonic-gate 	int		err;
59537c478bd9Sstevel@tonic-gate 	int		haverwlock = 1;
59547c478bd9Sstevel@tonic-gate 	int		trans_size;
59557c478bd9Sstevel@tonic-gate 	int		donetrans = 0;
59567c478bd9Sstevel@tonic-gate 	int		retry = 1;
59577c478bd9Sstevel@tonic-gate 
59587c478bd9Sstevel@tonic-gate 
59597c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_UFS, TR_UFS_SETSECATTR_START,
59607c478bd9Sstevel@tonic-gate 	    "ufs_setsecattr_start:vp %p, vsap %p, flags %x", vp, vsap, flag);
59617c478bd9Sstevel@tonic-gate 
59627c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
59637c478bd9Sstevel@tonic-gate 
59647c478bd9Sstevel@tonic-gate 	/* Abort now if the request is either empty or invalid. */
59657c478bd9Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
59667c478bd9Sstevel@tonic-gate 	if ((vsa_mask == 0) ||
59677c478bd9Sstevel@tonic-gate 	    ((vsap->vsa_aclentp == NULL) &&
59687c478bd9Sstevel@tonic-gate 	    (vsap->vsa_dfaclentp == NULL))) {
59697c478bd9Sstevel@tonic-gate 		err = EINVAL;
59707c478bd9Sstevel@tonic-gate 		goto out;
59717c478bd9Sstevel@tonic-gate 	}
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 	/*
59747c478bd9Sstevel@tonic-gate 	 * Following convention, if this is a directory then we acquire the
59757c478bd9Sstevel@tonic-gate 	 * inode's i_rwlock after starting a UFS logging transaction;
59767c478bd9Sstevel@tonic-gate 	 * otherwise, we acquire it beforehand. Since we were called (and
59777c478bd9Sstevel@tonic-gate 	 * must therefore return) with the lock held, we will have to drop it,
59787c478bd9Sstevel@tonic-gate 	 * and later reacquire it, if operating on a directory.
59797c478bd9Sstevel@tonic-gate 	 */
59807c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
59817c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
59827c478bd9Sstevel@tonic-gate 		haverwlock = 0;
59837c478bd9Sstevel@tonic-gate 	} else {
59847c478bd9Sstevel@tonic-gate 		/* Upgrade the lock if required. */
59857c478bd9Sstevel@tonic-gate 		if (!rw_write_held(&ip->i_rwlock)) {
59867c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
59877c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_WRITER);
59887c478bd9Sstevel@tonic-gate 		}
59897c478bd9Sstevel@tonic-gate 	}
59907c478bd9Sstevel@tonic-gate 
59917c478bd9Sstevel@tonic-gate again:
59927c478bd9Sstevel@tonic-gate 	ASSERT(!(vp->v_type == VDIR && haverwlock));
59937c478bd9Sstevel@tonic-gate 	if (err = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK)) {
59947c478bd9Sstevel@tonic-gate 		ulp = NULL;
59957c478bd9Sstevel@tonic-gate 		retry = 0;
59967c478bd9Sstevel@tonic-gate 		goto out;
59977c478bd9Sstevel@tonic-gate 	}
59987c478bd9Sstevel@tonic-gate 
59997c478bd9Sstevel@tonic-gate 	/*
60007c478bd9Sstevel@tonic-gate 	 * Check that the file system supports this operation. Note that
60017c478bd9Sstevel@tonic-gate 	 * ufs_lockfs_begin() will have checked that the file system had
60027c478bd9Sstevel@tonic-gate 	 * not been forcibly unmounted.
60037c478bd9Sstevel@tonic-gate 	 */
60047c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_fs->fs_ronly) {
60057c478bd9Sstevel@tonic-gate 		err = EROFS;
60067c478bd9Sstevel@tonic-gate 		goto out;
60077c478bd9Sstevel@tonic-gate 	}
60087c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_nosetsec) {
60097c478bd9Sstevel@tonic-gate 		err = ENOSYS;
60107c478bd9Sstevel@tonic-gate 		goto out;
60117c478bd9Sstevel@tonic-gate 	}
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	if (ulp) {
60147c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SETSECATTR,
60157c478bd9Sstevel@tonic-gate 			trans_size = TOP_SETSECATTR_SIZE(VTOI(vp)));
60167c478bd9Sstevel@tonic-gate 		donetrans = 1;
60177c478bd9Sstevel@tonic-gate 	}
60187c478bd9Sstevel@tonic-gate 
60197c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
60207c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
60217c478bd9Sstevel@tonic-gate 		haverwlock = 1;
60227c478bd9Sstevel@tonic-gate 	}
60237c478bd9Sstevel@tonic-gate 
60247c478bd9Sstevel@tonic-gate 	ASSERT(haverwlock);
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 	/* Do the actual work. */
60277c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
60287c478bd9Sstevel@tonic-gate 	/*
60297c478bd9Sstevel@tonic-gate 	 * Suppress out of inodes messages if we will retry.
60307c478bd9Sstevel@tonic-gate 	 */
60317c478bd9Sstevel@tonic-gate 	if (retry)
60327c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
60337c478bd9Sstevel@tonic-gate 	err = ufs_acl_set(ip, vsap, flag, cr);
60347c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
60357c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
60367c478bd9Sstevel@tonic-gate 
60377c478bd9Sstevel@tonic-gate out:
60387c478bd9Sstevel@tonic-gate 	if (ulp) {
60397c478bd9Sstevel@tonic-gate 		if (donetrans) {
60407c478bd9Sstevel@tonic-gate 			/*
60417c478bd9Sstevel@tonic-gate 			 * top_end_async() can eventually call
60427c478bd9Sstevel@tonic-gate 			 * top_end_sync(), which can block. We must
60437c478bd9Sstevel@tonic-gate 			 * therefore observe the lock-ordering protocol
60447c478bd9Sstevel@tonic-gate 			 * here as well.
60457c478bd9Sstevel@tonic-gate 			 */
60467c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR) {
60477c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_rwlock);
60487c478bd9Sstevel@tonic-gate 				haverwlock = 0;
60497c478bd9Sstevel@tonic-gate 			}
60507c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_SETSECATTR, trans_size);
60517c478bd9Sstevel@tonic-gate 		}
60527c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
60537c478bd9Sstevel@tonic-gate 	}
60547c478bd9Sstevel@tonic-gate 	/*
60557c478bd9Sstevel@tonic-gate 	 * If no inodes available, try scaring a logically-
60567c478bd9Sstevel@tonic-gate 	 * free one out of the delete queue to someplace
60577c478bd9Sstevel@tonic-gate 	 * that we can find it.
60587c478bd9Sstevel@tonic-gate 	 */
60597c478bd9Sstevel@tonic-gate 	if ((err == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
60607c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
60617c478bd9Sstevel@tonic-gate 		retry = 0;
60627c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR && haverwlock) {
60637c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
60647c478bd9Sstevel@tonic-gate 			haverwlock = 0;
60657c478bd9Sstevel@tonic-gate 		}
60667c478bd9Sstevel@tonic-gate 		goto again;
60677c478bd9Sstevel@tonic-gate 	}
60687c478bd9Sstevel@tonic-gate 	/*
60697c478bd9Sstevel@tonic-gate 	 * If we need to reacquire the lock then it is safe to do so
60707c478bd9Sstevel@tonic-gate 	 * as a reader. This is because ufs_rwunlock(), which will be
60717c478bd9Sstevel@tonic-gate 	 * called by our caller after we return, does not differentiate
60727c478bd9Sstevel@tonic-gate 	 * between shared and exclusive locks.
60737c478bd9Sstevel@tonic-gate 	 */
60747c478bd9Sstevel@tonic-gate 	if (!haverwlock) {
60757c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VDIR);
60767c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
60777c478bd9Sstevel@tonic-gate 	}
60787c478bd9Sstevel@tonic-gate 
60797c478bd9Sstevel@tonic-gate 	TRACE_1(TR_FAC_UFS, TR_UFS_SETSECATTR_END,
60807c478bd9Sstevel@tonic-gate 	    "ufs_setsecattr_end:vp %p", vp);
60817c478bd9Sstevel@tonic-gate 	return (err);
60827c478bd9Sstevel@tonic-gate }
6083