xref: /illumos-gate/usr/src/uts/common/fs/ufs/ufs_vnops.c (revision 87bfe94c)
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
55b024a5bSbatschul  * Common Development and Distribution License (the "License").
65b024a5bSbatschul  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21342440ecSPrasad Singamsetty 
227c478bd9Sstevel@tonic-gate /*
239b5097eeSOwen Roberts  * Copyright (c) 1984, 2010, Oracle and/or its affiliates. All rights reserved.
24284ce987SPatrick Mooney  * Copyright 2018 Joyent, Inc.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
292575b441SToomas Soome /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
337c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
387c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
397c478bd9Sstevel@tonic-gate #include <sys/param.h>
407c478bd9Sstevel@tonic-gate #include <sys/time.h>
417c478bd9Sstevel@tonic-gate #include <sys/systm.h>
427c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
437c478bd9Sstevel@tonic-gate #include <sys/resource.h>
447c478bd9Sstevel@tonic-gate #include <sys/signal.h>
457c478bd9Sstevel@tonic-gate #include <sys/cred.h>
467c478bd9Sstevel@tonic-gate #include <sys/user.h>
477c478bd9Sstevel@tonic-gate #include <sys/buf.h>
487c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
49aa59c4cbSrsb #include <sys/vfs_opreg.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>
56bc69f433Saguzovsk #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/filio.h>
677c478bd9Sstevel@tonic-gate #include <sys/policy.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
707c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_lockfs.h>
717c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_filio.h>
727c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
737c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fsdir.h>
747c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_quota.h>
757c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_log.h>
767c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_snap.h>
777c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_trans.h>
787c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_panic.h>
797c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_bio.h>
807c478bd9Sstevel@tonic-gate #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
817c478bd9Sstevel@tonic-gate #include <sys/errno.h>
827c478bd9Sstevel@tonic-gate #include <sys/fssnap_if.h>
837c478bd9Sstevel@tonic-gate #include <sys/unistd.h>
847c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <sys/filio.h>		/* _FIOIO */
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #include <vm/hat.h>
897c478bd9Sstevel@tonic-gate #include <vm/page.h>
907c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
917c478bd9Sstevel@tonic-gate #include <vm/as.h>
927c478bd9Sstevel@tonic-gate #include <vm/seg.h>
937c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
947c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
957c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
967c478bd9Sstevel@tonic-gate #include <vm/rm.h>
977c478bd9Sstevel@tonic-gate #include <sys/swap.h>
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
1007c478bd9Sstevel@tonic-gate 
101986fd29aSsetje #include <sys/fs/decomp.h>
102986fd29aSsetje 
1037c478bd9Sstevel@tonic-gate static struct instats ins;
1047c478bd9Sstevel@tonic-gate 
1052575b441SToomas Soome static	int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
1067c478bd9Sstevel@tonic-gate static	int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
1077c478bd9Sstevel@tonic-gate 		caddr_t, struct page **, size_t, enum seg_rw, int);
108da6c28aaSamw static	int ufs_open(struct vnode **, int, struct cred *, caller_context_t *);
109da6c28aaSamw static	int ufs_close(struct vnode *, int, int, offset_t, struct cred *,
110da6c28aaSamw 		caller_context_t *);
1117c478bd9Sstevel@tonic-gate static	int ufs_read(struct vnode *, struct uio *, int, struct cred *,
112da6c28aaSamw 		struct caller_context *);
1137c478bd9Sstevel@tonic-gate static	int ufs_write(struct vnode *, struct uio *, int, struct cred *,
114da6c28aaSamw 		struct caller_context *);
115da6c28aaSamw static	int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *,
116da6c28aaSamw 		int *, caller_context_t *);
117da6c28aaSamw static	int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *,
118da6c28aaSamw 		caller_context_t *);
1197c478bd9Sstevel@tonic-gate static	int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
120da6c28aaSamw 		caller_context_t *);
121da6c28aaSamw static	int ufs_access(struct vnode *, int, int, struct cred *,
122da6c28aaSamw 		caller_context_t *);
1237c478bd9Sstevel@tonic-gate static	int ufs_lookup(struct vnode *, char *, struct vnode **,
124da6c28aaSamw 		struct pathname *, int, struct vnode *, struct cred *,
125da6c28aaSamw 		caller_context_t *, int *, pathname_t *);
1267c478bd9Sstevel@tonic-gate static	int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
127da6c28aaSamw 		int, struct vnode **, struct cred *, int,
128da6c28aaSamw 		caller_context_t *, vsecattr_t  *);
129da6c28aaSamw static	int ufs_remove(struct vnode *, char *, struct cred *,
130da6c28aaSamw 		caller_context_t *, int);
131da6c28aaSamw static	int ufs_link(struct vnode *, struct vnode *, char *, struct cred *,
132da6c28aaSamw 		caller_context_t *, int);
1337c478bd9Sstevel@tonic-gate static	int ufs_rename(struct vnode *, char *, struct vnode *, char *,
134da6c28aaSamw 		struct cred *, caller_context_t *, int);
1357c478bd9Sstevel@tonic-gate static	int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
136da6c28aaSamw 		struct cred *, caller_context_t *, int, vsecattr_t *);
137da6c28aaSamw static	int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *,
138da6c28aaSamw 		caller_context_t *, int);
139da6c28aaSamw static	int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *,
140da6c28aaSamw 		caller_context_t *, int);
1417c478bd9Sstevel@tonic-gate static	int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
142da6c28aaSamw 		struct cred *, caller_context_t *, int);
143da6c28aaSamw static	int ufs_readlink(struct vnode *, struct uio *, struct cred *,
144da6c28aaSamw 		caller_context_t *);
145da6c28aaSamw static	int ufs_fsync(struct vnode *, int, struct cred *, caller_context_t *);
146da6c28aaSamw static	void ufs_inactive(struct vnode *, struct cred *, caller_context_t *);
147da6c28aaSamw static	int ufs_fid(struct vnode *, struct fid *, caller_context_t *);
1487c478bd9Sstevel@tonic-gate static	int ufs_rwlock(struct vnode *, int, caller_context_t *);
1497c478bd9Sstevel@tonic-gate static	void ufs_rwunlock(struct vnode *, int, caller_context_t *);
150da6c28aaSamw static	int ufs_seek(struct vnode *, offset_t, offset_t *, caller_context_t *);
1517c478bd9Sstevel@tonic-gate static	int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
152da6c28aaSamw 		struct flk_callback *, struct cred *,
153da6c28aaSamw 		caller_context_t *);
1547c478bd9Sstevel@tonic-gate static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
1557c478bd9Sstevel@tonic-gate 		cred_t *, caller_context_t *);
1567c478bd9Sstevel@tonic-gate static	int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
1577c478bd9Sstevel@tonic-gate 		struct page **, size_t, struct seg *, caddr_t,
158da6c28aaSamw 		enum seg_rw, struct cred *, caller_context_t *);
159da6c28aaSamw static	int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *,
160da6c28aaSamw 		caller_context_t *);
1617c478bd9Sstevel@tonic-gate static	int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
1627c478bd9Sstevel@tonic-gate static	int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
163da6c28aaSamw 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
1647c478bd9Sstevel@tonic-gate static	int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
165da6c28aaSamw 		uchar_t, uchar_t, uint_t, struct cred *, caller_context_t *);
1667c478bd9Sstevel@tonic-gate static	int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
167da6c28aaSamw 		uint_t, uint_t, uint_t, struct cred *, caller_context_t *);
168da6c28aaSamw static	int ufs_poll(vnode_t *, short, int, short *, struct pollhead **,
169da6c28aaSamw 		caller_context_t *);
170d7334e51Srm static	int ufs_dump(vnode_t *, caddr_t, offset_t, offset_t,
171d7334e51Srm     caller_context_t *);
172da6c28aaSamw static	int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *,
173da6c28aaSamw 		caller_context_t *);
1747c478bd9Sstevel@tonic-gate static	int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
175da6c28aaSamw 		struct cred *, caller_context_t *);
176d7334e51Srm static	int ufs_dumpctl(vnode_t *, int, offset_t *, caller_context_t *);
1777c478bd9Sstevel@tonic-gate static	daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
178da6c28aaSamw 		daddr32_t *, int, int);
179da6c28aaSamw static	int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
180da6c28aaSamw 		caller_context_t *);
181da6c28aaSamw static	int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *,
182da6c28aaSamw 		caller_context_t *);
18360c8e821SFrank Batschulat static	int ufs_priv_access(void *, int, struct cred *);
1849b5097eeSOwen Roberts static	int ufs_eventlookup(struct vnode *, char *, struct cred *,
1859b5097eeSOwen Roberts     struct vnode **);
18602ff05a9Svsakar 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate struct vnodeops *ufs_vnodeops;
1937c478bd9Sstevel@tonic-gate 
194aa59c4cbSrsb /* NOTE: "not blkd" below  means that the operation isn't blocked by lockfs */
1957c478bd9Sstevel@tonic-gate const fs_operation_def_t ufs_vnodeops_template[] = {
196aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = ufs_open },	/* not blkd */
197aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = ufs_close },	/* not blkd */
198aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = ufs_read },
199aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = ufs_write },
200aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = ufs_ioctl },
201aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = ufs_getattr },
202aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = ufs_setattr },
203aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = ufs_access },
204aa59c4cbSrsb 	VOPNAME_LOOKUP,		{ .vop_lookup = ufs_lookup },
205aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = ufs_create },
206aa59c4cbSrsb 	VOPNAME_REMOVE,		{ .vop_remove = ufs_remove },
207aa59c4cbSrsb 	VOPNAME_LINK,		{ .vop_link = ufs_link },
208aa59c4cbSrsb 	VOPNAME_RENAME,		{ .vop_rename = ufs_rename },
209aa59c4cbSrsb 	VOPNAME_MKDIR,		{ .vop_mkdir = ufs_mkdir },
210aa59c4cbSrsb 	VOPNAME_RMDIR,		{ .vop_rmdir = ufs_rmdir },
211aa59c4cbSrsb 	VOPNAME_READDIR,	{ .vop_readdir = ufs_readdir },
212aa59c4cbSrsb 	VOPNAME_SYMLINK,	{ .vop_symlink = ufs_symlink },
213aa59c4cbSrsb 	VOPNAME_READLINK,	{ .vop_readlink = ufs_readlink },
214aa59c4cbSrsb 	VOPNAME_FSYNC,		{ .vop_fsync = ufs_fsync },
215aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = ufs_inactive }, /* not blkd */
216aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = ufs_fid },
217aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = ufs_rwlock },	/* not blkd */
218aa59c4cbSrsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = ufs_rwunlock }, /* not blkd */
219aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = ufs_seek },
220aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .vop_frlock = ufs_frlock },
221aa59c4cbSrsb 	VOPNAME_SPACE,		{ .vop_space = ufs_space },
222aa59c4cbSrsb 	VOPNAME_GETPAGE,	{ .vop_getpage = ufs_getpage },
223aa59c4cbSrsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = ufs_putpage },
224aa59c4cbSrsb 	VOPNAME_MAP,		{ .vop_map = ufs_map },
225aa59c4cbSrsb 	VOPNAME_ADDMAP,		{ .vop_addmap = ufs_addmap },	/* not blkd */
226aa59c4cbSrsb 	VOPNAME_DELMAP,		{ .vop_delmap = ufs_delmap },	/* not blkd */
227aa59c4cbSrsb 	VOPNAME_POLL,		{ .vop_poll = ufs_poll },	/* not blkd */
228aa59c4cbSrsb 	VOPNAME_DUMP,		{ .vop_dump = ufs_dump },
229aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = ufs_l_pathconf },
230aa59c4cbSrsb 	VOPNAME_PAGEIO,		{ .vop_pageio = ufs_pageio },
231aa59c4cbSrsb 	VOPNAME_DUMPCTL,	{ .vop_dumpctl = ufs_dumpctl },
232aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = ufs_getsecattr },
233aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = ufs_setsecattr },
234aa59c4cbSrsb 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
235aa59c4cbSrsb 	NULL,			NULL
2367c478bd9Sstevel@tonic-gate };
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate #define	MAX_BACKFILE_COUNT	9999
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * Created by ufs_dumpctl() to store a file's disk block info into memory.
2427c478bd9Sstevel@tonic-gate  * Used by ufs_dump() to dump data to disk directly.
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate struct dump {
2457c478bd9Sstevel@tonic-gate 	struct inode	*ip;		/* the file we contain */
2467c478bd9Sstevel@tonic-gate 	daddr_t		fsbs;		/* number of blocks stored */
2477c478bd9Sstevel@tonic-gate 	struct timeval32 time;		/* time stamp for the struct */
2482575b441SToomas Soome 	daddr32_t	dblk[1];	/* place holder for block info */
2497c478bd9Sstevel@tonic-gate };
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static struct dump *dump_info = NULL;
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Previously there was no special action required for ordinary files.
2557c478bd9Sstevel@tonic-gate  * (Devices are handled through the device file system.)
2567c478bd9Sstevel@tonic-gate  * Now we support Large Files and Large File API requires open to
2577c478bd9Sstevel@tonic-gate  * fail if file is large.
2587c478bd9Sstevel@tonic-gate  * We could take care to prevent data corruption
2597c478bd9Sstevel@tonic-gate  * by doing an atomic check of size and truncate if file is opened with
2607c478bd9Sstevel@tonic-gate  * FTRUNC flag set but traditionally this is being done by the vfs/vnode
2617c478bd9Sstevel@tonic-gate  * layers. So taking care of truncation here is a change in the existing
2627c478bd9Sstevel@tonic-gate  * semantics of VOP_OPEN and therefore we chose not to implement any thing
2637c478bd9Sstevel@tonic-gate  * here. The check for the size of the file > 2GB is being done at the
2647c478bd9Sstevel@tonic-gate  * vfs layer in routine vn_open().
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /* ARGSUSED */
2687c478bd9Sstevel@tonic-gate static int
ufs_open(struct vnode ** vpp,int flag,struct cred * cr,caller_context_t * ct)269da6c28aaSamw ufs_open(struct vnode **vpp, int flag, struct cred *cr, caller_context_t *ct)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate 	return (0);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2757c478bd9Sstevel@tonic-gate static int
ufs_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cr,caller_context_t * ct)2767c478bd9Sstevel@tonic-gate ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
27780d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
2807c478bd9Sstevel@tonic-gate 	cleanshares(vp, ttoproc(curthread)->p_pid);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * Push partially filled cluster at last close.
2847c478bd9Sstevel@tonic-gate 	 * ``last close'' is approximated because the dnlc
2857c478bd9Sstevel@tonic-gate 	 * may have a hold on the vnode.
2867c478bd9Sstevel@tonic-gate 	 * Checking for VBAD here will also act as a forced umount check.
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
2897c478bd9Sstevel@tonic-gate 		struct inode *ip = VTOI(vp);
2907c478bd9Sstevel@tonic-gate 		if (ip->i_delaylen) {
2917c478bd9Sstevel@tonic-gate 			ins.in_poc.value.ul++;
2927c478bd9Sstevel@tonic-gate 			(void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
29380d34432Sfrankho 			    B_ASYNC | B_FREE, cr);
2947c478bd9Sstevel@tonic-gate 			ip->i_delaylen = 0;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3027c478bd9Sstevel@tonic-gate static int
ufs_read(struct vnode * vp,struct uio * uiop,int ioflag,struct cred * cr,struct caller_context * ct)3037c478bd9Sstevel@tonic-gate ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
30480d5689fSPatrick Mooney     struct caller_context *ct)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
3077c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
3087c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp = NULL;
3097c478bd9Sstevel@tonic-gate 	int error = 0;
3107c478bd9Sstevel@tonic-gate 	int intrans = 0;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	/*
3157c478bd9Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
3167c478bd9Sstevel@tonic-gate 	 * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
3177c478bd9Sstevel@tonic-gate 	 */
3187c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
3197c478bd9Sstevel@tonic-gate 		/*
3207c478bd9Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
3217c478bd9Sstevel@tonic-gate 		 */
3227c478bd9Sstevel@tonic-gate 		error = chklock(vp, FREAD, uiop->uio_loffset,
32380d34432Sfrankho 		    uiop->uio_resid, uiop->uio_fmode, ct);
3247c478bd9Sstevel@tonic-gate 		if (error)
3257c478bd9Sstevel@tonic-gate 			goto out;
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
3297c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
3307c478bd9Sstevel@tonic-gate 	if (error)
3317c478bd9Sstevel@tonic-gate 		goto out;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * In the case that a directory is opened for reading as a file
3357c478bd9Sstevel@tonic-gate 	 * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
3367c478bd9Sstevel@tonic-gate 	 * The locking order had to be changed to avoid a deadlock with
3377c478bd9Sstevel@tonic-gate 	 * an update taking place on that directory at the same time.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	if ((ip->i_mode & IFMT) == IFDIR) {
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3427c478bd9Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3437c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		if (error) {
3467c478bd9Sstevel@tonic-gate 			if (ulp)
3477c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
3487c478bd9Sstevel@tonic-gate 			goto out;
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3527c478bd9Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3537c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
3547c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3557c478bd9Sstevel@tonic-gate 			    error);
3567c478bd9Sstevel@tonic-gate 			ASSERT(!error);
3577c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3587c478bd9Sstevel@tonic-gate 			    TOP_READ_SIZE);
3597c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_READER);
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 	} else {
3627c478bd9Sstevel@tonic-gate 		/*
3637c478bd9Sstevel@tonic-gate 		 * Only transact reads to files opened for sync-read and
3647c478bd9Sstevel@tonic-gate 		 * sync-write on a file system that is not write locked.
3657c478bd9Sstevel@tonic-gate 		 *
3667c478bd9Sstevel@tonic-gate 		 * The ``not write locked'' check prevents problems with
3677c478bd9Sstevel@tonic-gate 		 * enabling/disabling logging on a busy file system.  E.g.,
3687c478bd9Sstevel@tonic-gate 		 * logging exists at the beginning of the read but does not
3697c478bd9Sstevel@tonic-gate 		 * at the end.
3707c478bd9Sstevel@tonic-gate 		 *
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
3737c478bd9Sstevel@tonic-gate 		    TRANS_ISTRANS(ufsvfsp)) {
3747c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
3757c478bd9Sstevel@tonic-gate 			    error);
3767c478bd9Sstevel@tonic-gate 			ASSERT(!error);
3777c478bd9Sstevel@tonic-gate 			intrans = 1;
3787c478bd9Sstevel@tonic-gate 		}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
3817c478bd9Sstevel@tonic-gate 		error = rdip(ip, uiop, ioflag, cr);
3827c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 		if (intrans) {
3857c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
3867c478bd9Sstevel@tonic-gate 			    TOP_READ_SIZE);
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if (ulp) {
3917c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate out:
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 	return (error);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate extern	int	ufs_HW;		/* high water mark */
3997c478bd9Sstevel@tonic-gate extern	int	ufs_LW;		/* low water mark */
4007c478bd9Sstevel@tonic-gate int	ufs_WRITES = 1;		/* XXX - enable/disable */
4017c478bd9Sstevel@tonic-gate int	ufs_throttles = 0;	/* throttling count */
4027c478bd9Sstevel@tonic-gate int	ufs_allow_shared_writes = 1;	/* directio shared writes */
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static int
ufs_check_rewrite(struct inode * ip,struct uio * uiop,int ioflag)4057c478bd9Sstevel@tonic-gate ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
4067c478bd9Sstevel@tonic-gate {
407f90bab20Sswilcox 	int	shared_write;
408f90bab20Sswilcox 
409f90bab20Sswilcox 	/*
410f90bab20Sswilcox 	 * If the FDSYNC flag is set then ignore the global
411f90bab20Sswilcox 	 * ufs_allow_shared_writes in this case.
412f90bab20Sswilcox 	 */
413f90bab20Sswilcox 	shared_write = (ioflag & FDSYNC) | ufs_allow_shared_writes;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/*
4167c478bd9Sstevel@tonic-gate 	 * Filter to determine if this request is suitable as a
4177c478bd9Sstevel@tonic-gate 	 * concurrent rewrite. This write must not allocate blocks
4187c478bd9Sstevel@tonic-gate 	 * by extending the file or filling in holes. No use trying
4197c478bd9Sstevel@tonic-gate 	 * through FSYNC descriptors as the inode will be synchronously
4207c478bd9Sstevel@tonic-gate 	 * updated after the write. The uio structure has not yet been
4217c478bd9Sstevel@tonic-gate 	 * checked for sanity, so assume nothing.
4227c478bd9Sstevel@tonic-gate 	 */
4237c478bd9Sstevel@tonic-gate 	return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
42480d34432Sfrankho 	    (uiop->uio_loffset >= (offset_t)0) &&
42580d34432Sfrankho 	    (uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
42680d34432Sfrankho 	    ((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
42780d34432Sfrankho 	    !(ioflag & FSYNC) && !bmap_has_holes(ip) &&
42880d34432Sfrankho 	    shared_write);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4327c478bd9Sstevel@tonic-gate static int
ufs_write(struct vnode * vp,struct uio * uiop,int ioflag,cred_t * cr,caller_context_t * ct)4337c478bd9Sstevel@tonic-gate ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
43480d5689fSPatrick Mooney     caller_context_t *ct)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
4377c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
4387c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
4397c478bd9Sstevel@tonic-gate 	int retry = 1;
4407c478bd9Sstevel@tonic-gate 	int error, resv, resid = 0;
4417c478bd9Sstevel@tonic-gate 	int directio_status;
4427c478bd9Sstevel@tonic-gate 	int exclusive;
443f90bab20Sswilcox 	int rewriteflg;
4447c478bd9Sstevel@tonic-gate 	long start_resid = uiop->uio_resid;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate retry_mandlock:
4497c478bd9Sstevel@tonic-gate 	/*
4507c478bd9Sstevel@tonic-gate 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
4517c478bd9Sstevel@tonic-gate 	 * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
4527c478bd9Sstevel@tonic-gate 	 * Check for forced unmounts normally done in ufs_lockfs_begin().
4537c478bd9Sstevel@tonic-gate 	 */
4547c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
4557c478bd9Sstevel@tonic-gate 		error = EIO;
4567c478bd9Sstevel@tonic-gate 		goto out;
4577c478bd9Sstevel@tonic-gate 	}
4587c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		/*
4637c478bd9Sstevel@tonic-gate 		 * ufs_getattr ends up being called by chklock
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		error = chklock(vp, FWRITE, uiop->uio_loffset,
46680d34432Sfrankho 		    uiop->uio_resid, uiop->uio_fmode, ct);
4677c478bd9Sstevel@tonic-gate 		if (error)
4687c478bd9Sstevel@tonic-gate 			goto out;
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/* i_rwlock can change in chklock */
4727c478bd9Sstevel@tonic-gate 	exclusive = rw_write_held(&ip->i_rwlock);
473f90bab20Sswilcox 	rewriteflg = ufs_check_rewrite(ip, uiop, ioflag);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Check for fast-path special case of directio re-writes.
4777c478bd9Sstevel@tonic-gate 	 */
4787c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
479f90bab20Sswilcox 	    !exclusive && rewriteflg) {
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
4827c478bd9Sstevel@tonic-gate 		if (error)
4837c478bd9Sstevel@tonic-gate 			goto out;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
4867c478bd9Sstevel@tonic-gate 		error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
48780d34432Sfrankho 		    &directio_status);
4887c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS) {
4897c478bd9Sstevel@tonic-gate 			uint_t i_flag_save;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 			if (start_resid != uiop->uio_resid)
4927c478bd9Sstevel@tonic-gate 				error = 0;
4937c478bd9Sstevel@tonic-gate 			/*
4947c478bd9Sstevel@tonic-gate 			 * Special treatment of access times for re-writes.
4957c478bd9Sstevel@tonic-gate 			 * If IMOD is not already set, then convert it
4967c478bd9Sstevel@tonic-gate 			 * to IMODACC for this operation. This defers
4977c478bd9Sstevel@tonic-gate 			 * entering a delta into the log until the inode
4987c478bd9Sstevel@tonic-gate 			 * is flushed. This mimics what is done for read
4997c478bd9Sstevel@tonic-gate 			 * operations and inode access time.
5007c478bd9Sstevel@tonic-gate 			 */
5017c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
5027c478bd9Sstevel@tonic-gate 			i_flag_save = ip->i_flag;
5037c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
5047c478bd9Sstevel@tonic-gate 			ip->i_seq++;
5057c478bd9Sstevel@tonic-gate 			ITIMES_NOLOCK(ip);
5067c478bd9Sstevel@tonic-gate 			if ((i_flag_save & IMOD) == 0) {
5077c478bd9Sstevel@tonic-gate 				ip->i_flag &= ~IMOD;
5087c478bd9Sstevel@tonic-gate 				ip->i_flag |= IMODACC;
5097c478bd9Sstevel@tonic-gate 			}
5107c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
5117c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
5127c478bd9Sstevel@tonic-gate 			if (ulp)
5137c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
5147c478bd9Sstevel@tonic-gate 			goto out;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
5177c478bd9Sstevel@tonic-gate 		if (ulp)
5187c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
5227c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
5237c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Mandatory locking could have been enabled
5267c478bd9Sstevel@tonic-gate 		 * after dropping the i_rwlock.
5277c478bd9Sstevel@tonic-gate 		 */
5287c478bd9Sstevel@tonic-gate 		if (MANDLOCK(vp, ip->i_mode))
5297c478bd9Sstevel@tonic-gate 			goto retry_mandlock;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
5337c478bd9Sstevel@tonic-gate 	if (error)
5347c478bd9Sstevel@tonic-gate 		goto out;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	/*
5377c478bd9Sstevel@tonic-gate 	 * Amount of log space needed for this write
5387c478bd9Sstevel@tonic-gate 	 */
539f90bab20Sswilcox 	if (!rewriteflg || !(ioflag & FDSYNC))
540f90bab20Sswilcox 		TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Throttle writes.
5447c478bd9Sstevel@tonic-gate 	 */
5457c478bd9Sstevel@tonic-gate 	if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
5467c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
5477c478bd9Sstevel@tonic-gate 		while (ip->i_writes > ufs_HW) {
5487c478bd9Sstevel@tonic-gate 			ufs_throttles++;
5497c478bd9Sstevel@tonic-gate 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	/*
5557c478bd9Sstevel@tonic-gate 	 * Enter Transaction
556f90bab20Sswilcox 	 *
557f90bab20Sswilcox 	 * If the write is a rewrite there is no need to open a transaction
558f90bab20Sswilcox 	 * if the FDSYNC flag is set and not the FSYNC.  In this case just
559f90bab20Sswilcox 	 * set the IMODACC flag to modify do the update at a later time
560f90bab20Sswilcox 	 * thus avoiding the overhead of the logging transaction that is
561f90bab20Sswilcox 	 * not required.
5627c478bd9Sstevel@tonic-gate 	 */
5637c478bd9Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
5647c478bd9Sstevel@tonic-gate 		if (ulp) {
565f90bab20Sswilcox 			if (rewriteflg) {
566f90bab20Sswilcox 				uint_t i_flag_save;
567f90bab20Sswilcox 
568f90bab20Sswilcox 				rw_enter(&ip->i_contents, RW_READER);
569f90bab20Sswilcox 				mutex_enter(&ip->i_tlock);
570f90bab20Sswilcox 				i_flag_save = ip->i_flag;
571f90bab20Sswilcox 				ip->i_flag |= IUPD | ICHG;
572f90bab20Sswilcox 				ip->i_seq++;
573f90bab20Sswilcox 				ITIMES_NOLOCK(ip);
574f90bab20Sswilcox 				if ((i_flag_save & IMOD) == 0) {
575f90bab20Sswilcox 					ip->i_flag &= ~IMOD;
576f90bab20Sswilcox 					ip->i_flag |= IMODACC;
577f90bab20Sswilcox 				}
578f90bab20Sswilcox 				mutex_exit(&ip->i_tlock);
579f90bab20Sswilcox 				rw_exit(&ip->i_contents);
580f90bab20Sswilcox 			} else {
581f90bab20Sswilcox 				int terr = 0;
582f90bab20Sswilcox 				TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv,
583f90bab20Sswilcox 				    terr);
584f90bab20Sswilcox 				ASSERT(!terr);
585f90bab20Sswilcox 			}
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 	} else {
5887c478bd9Sstevel@tonic-gate 		if (ulp)
5897c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Write the file
5947c478bd9Sstevel@tonic-gate 	 */
5957c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
5967c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
5977c478bd9Sstevel@tonic-gate 	if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
5987c478bd9Sstevel@tonic-gate 		/*
5997c478bd9Sstevel@tonic-gate 		 * In append mode start at end of file.
6007c478bd9Sstevel@tonic-gate 		 */
6017c478bd9Sstevel@tonic-gate 		uiop->uio_loffset = ip->i_size;
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * Mild optimisation, don't call ufs_trans_write() unless we have to
6067c478bd9Sstevel@tonic-gate 	 * Also, suppress file system full messages if we will retry.
6077c478bd9Sstevel@tonic-gate 	 */
6087c478bd9Sstevel@tonic-gate 	if (retry)
6097c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
6107c478bd9Sstevel@tonic-gate 	if (resid) {
6117c478bd9Sstevel@tonic-gate 		TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
6127c478bd9Sstevel@tonic-gate 	} else {
6137c478bd9Sstevel@tonic-gate 		error = wrip(ip, uiop, ioflag, cr);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
6187c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Leave Transaction
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	if (ulp) {
6247c478bd9Sstevel@tonic-gate 		if (ioflag & (FSYNC|FDSYNC)) {
625f90bab20Sswilcox 			if (!rewriteflg) {
626f90bab20Sswilcox 				int terr = 0;
627f90bab20Sswilcox 
628f90bab20Sswilcox 				TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC,
62980d34432Sfrankho 				    resv);
630f90bab20Sswilcox 				if (error == 0)
631f90bab20Sswilcox 					error = terr;
632f90bab20Sswilcox 			}
6337c478bd9Sstevel@tonic-gate 		} else {
6347c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate out:
6397c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * Any blocks tied up in pending deletes?
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
6447c478bd9Sstevel@tonic-gate 		retry = 0;
6457c478bd9Sstevel@tonic-gate 		goto retry_mandlock;
6467c478bd9Sstevel@tonic-gate 	}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	if (error == ENOSPC && (start_resid != uiop->uio_resid))
6497c478bd9Sstevel@tonic-gate 		error = 0;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	return (error);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate /*
6557c478bd9Sstevel@tonic-gate  * Don't cache write blocks to files with the sticky bit set.
6567c478bd9Sstevel@tonic-gate  * Used to keep swap files from blowing the page cache on a server.
6577c478bd9Sstevel@tonic-gate  */
6587c478bd9Sstevel@tonic-gate int stickyhack = 1;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * wrip does the real work of write requests for ufs.
6627c478bd9Sstevel@tonic-gate  */
6637c478bd9Sstevel@tonic-gate int
wrip(struct inode * ip,struct uio * uio,int ioflag,struct cred * cr)6647c478bd9Sstevel@tonic-gate wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate 	rlim64_t limit = uio->uio_llimit;
6677c478bd9Sstevel@tonic-gate 	u_offset_t off;
6687c478bd9Sstevel@tonic-gate 	u_offset_t old_i_size;
6697c478bd9Sstevel@tonic-gate 	struct fs *fs;
6707c478bd9Sstevel@tonic-gate 	struct vnode *vp;
6717c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
6727c478bd9Sstevel@tonic-gate 	caddr_t base;
6737c478bd9Sstevel@tonic-gate 	long start_resid = uio->uio_resid;	/* save starting resid */
6747c478bd9Sstevel@tonic-gate 	long premove_resid;			/* resid before uiomove() */
6757c478bd9Sstevel@tonic-gate 	uint_t flags;
6767c478bd9Sstevel@tonic-gate 	int newpage;
6777c478bd9Sstevel@tonic-gate 	int iupdat_flag, directio_status;
6787c478bd9Sstevel@tonic-gate 	int n, on, mapon;
6797c478bd9Sstevel@tonic-gate 	int error, pagecreate;
6807c478bd9Sstevel@tonic-gate 	int do_dqrwlock;		/* drop/reacquire vfs_dqrwlock */
6817c478bd9Sstevel@tonic-gate 	int32_t	iblocks;
6827c478bd9Sstevel@tonic-gate 	int	new_iblocks;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * ip->i_size is incremented before the uiomove
6867c478bd9Sstevel@tonic-gate 	 * is done on a write.  If the move fails (bad user
6877c478bd9Sstevel@tonic-gate 	 * address) reset ip->i_size.
6887c478bd9Sstevel@tonic-gate 	 * The better way would be to increment ip->i_size
6897c478bd9Sstevel@tonic-gate 	 * only if the uiomove succeeds.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	int i_size_changed = 0;
6927c478bd9Sstevel@tonic-gate 	o_mode_t type;
6937c478bd9Sstevel@tonic-gate 	int i_seq_needed = 0;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	/*
6987c478bd9Sstevel@tonic-gate 	 * check for forced unmount - should not happen as
6997c478bd9Sstevel@tonic-gate 	 * the request passed the lockfs checks.
7007c478bd9Sstevel@tonic-gate 	 */
7017c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
7027c478bd9Sstevel@tonic-gate 		return (EIO);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	/* check for valid filetype */
7097c478bd9Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
7107c478bd9Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
7117c478bd9Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
7127c478bd9Sstevel@tonic-gate 		return (EIO);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	/*
7167c478bd9Sstevel@tonic-gate 	 * the actual limit of UFS file size
7177c478bd9Sstevel@tonic-gate 	 * is UFS_MAXOFFSET_T
7187c478bd9Sstevel@tonic-gate 	 */
7197c478bd9Sstevel@tonic-gate 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
7207c478bd9Sstevel@tonic-gate 		limit = MAXOFFSET_T;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= limit) {
7237c478bd9Sstevel@tonic-gate 		proc_t *p = ttoproc(curthread);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7267c478bd9Sstevel@tonic-gate 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
7277c478bd9Sstevel@tonic-gate 		    p, RCA_UNSAFE_SIGINFO);
7287c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
7297c478bd9Sstevel@tonic-gate 		return (EFBIG);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
7337c478bd9Sstevel@tonic-gate 	 * if largefiles are disallowed, the limit is
7347c478bd9Sstevel@tonic-gate 	 * the pre-largefiles value of 2GB
7357c478bd9Sstevel@tonic-gate 	 */
7367c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
7377c478bd9Sstevel@tonic-gate 		limit = MIN(UFS_MAXOFFSET_T, limit);
7387c478bd9Sstevel@tonic-gate 	else
7397c478bd9Sstevel@tonic-gate 		limit = MIN(MAXOFF32_T, limit);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
7427c478bd9Sstevel@tonic-gate 		return (EINVAL);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
7457c478bd9Sstevel@tonic-gate 		return (0);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset >= limit)
7497c478bd9Sstevel@tonic-gate 		return (EFBIG);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	ip->i_flag |= INOACC;	/* don't update ref time in getpage */
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	if (ioflag & (FSYNC|FDSYNC)) {
7547c478bd9Sstevel@tonic-gate 		ip->i_flag |= ISYNC;
7557c478bd9Sstevel@tonic-gate 		iupdat_flag = 1;
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 	/*
7587c478bd9Sstevel@tonic-gate 	 * Try to go direct
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
7617c478bd9Sstevel@tonic-gate 		uio->uio_llimit = limit;
7627c478bd9Sstevel@tonic-gate 		error = ufs_directio_write(ip, uio, ioflag, 0, cr,
76380d34432Sfrankho 		    &directio_status);
7647c478bd9Sstevel@tonic-gate 		/*
7657c478bd9Sstevel@tonic-gate 		 * If ufs_directio wrote to the file or set the flags,
7667c478bd9Sstevel@tonic-gate 		 * we need to update i_seq, but it may be deferred.
7677c478bd9Sstevel@tonic-gate 		 */
7687c478bd9Sstevel@tonic-gate 		if (start_resid != uio->uio_resid ||
76980d34432Sfrankho 		    (ip->i_flag & (ICHG|IUPD))) {
7707c478bd9Sstevel@tonic-gate 			i_seq_needed = 1;
7717c478bd9Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
7747c478bd9Sstevel@tonic-gate 			goto out;
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
7797c478bd9Sstevel@tonic-gate 	 *
7807c478bd9Sstevel@tonic-gate 	 * o shadow inodes: vfs_dqrwlock is not held at all
7817c478bd9Sstevel@tonic-gate 	 * o quota updates: vfs_dqrwlock is read or write held
7827c478bd9Sstevel@tonic-gate 	 * o other updates: vfs_dqrwlock is read held
7837c478bd9Sstevel@tonic-gate 	 *
7847c478bd9Sstevel@tonic-gate 	 * The first case is the only one where we do not hold
7857c478bd9Sstevel@tonic-gate 	 * vfs_dqrwlock at all while entering wrip().
7867c478bd9Sstevel@tonic-gate 	 * We must make sure not to downgrade/drop vfs_dqrwlock if we
7877c478bd9Sstevel@tonic-gate 	 * have it as writer, i.e. if we are updating the quota inode.
7887c478bd9Sstevel@tonic-gate 	 * There is no potential deadlock scenario in this case as
7897c478bd9Sstevel@tonic-gate 	 * ufs_getpage() takes care of this and avoids reacquiring
7907c478bd9Sstevel@tonic-gate 	 * vfs_dqrwlock in that case.
7917c478bd9Sstevel@tonic-gate 	 *
7927c478bd9Sstevel@tonic-gate 	 * This check is done here since the above conditions do not change
7937c478bd9Sstevel@tonic-gate 	 * and we possibly loop below, so save a few cycles.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	if ((type == IFSHAD) ||
79680d34432Sfrankho 	    (rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
797986fd29aSsetje 		do_dqrwlock = 0;
7987c478bd9Sstevel@tonic-gate 	} else {
7997c478bd9Sstevel@tonic-gate 		do_dqrwlock = 1;
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/*
8037c478bd9Sstevel@tonic-gate 	 * Large Files: We cast MAXBMASK to offset_t
8047c478bd9Sstevel@tonic-gate 	 * inorder to mask out the higher bits. Since offset_t
8057c478bd9Sstevel@tonic-gate 	 * is a signed value, the high order bit set in MAXBMASK
8067c478bd9Sstevel@tonic-gate 	 * value makes it do the right thing by having all bits 1
8077c478bd9Sstevel@tonic-gate 	 * in the higher word. May be removed for _SOLARIS64_.
8087c478bd9Sstevel@tonic-gate 	 */
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
8117c478bd9Sstevel@tonic-gate 	do {
8127c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
8137c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
8147c478bd9Sstevel@tonic-gate 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
8157c478bd9Sstevel@tonic-gate 		on = (int)blkoff(fs, uoff);
8167c478bd9Sstevel@tonic-gate 		n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
8177c478bd9Sstevel@tonic-gate 		new_iblocks = 1;
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 		if (type == IFREG && uoff + n >= limit) {
8207c478bd9Sstevel@tonic-gate 			if (uoff >= limit) {
8217c478bd9Sstevel@tonic-gate 				error = EFBIG;
8227c478bd9Sstevel@tonic-gate 				goto out;
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 			/*
8257c478bd9Sstevel@tonic-gate 			 * since uoff + n >= limit,
8267c478bd9Sstevel@tonic-gate 			 * therefore n >= limit - uoff, and n is an int
8277c478bd9Sstevel@tonic-gate 			 * so it is safe to cast it to an int
8287c478bd9Sstevel@tonic-gate 			 */
8297c478bd9Sstevel@tonic-gate 			n = (int)(limit - (rlim64_t)uoff);
8307c478bd9Sstevel@tonic-gate 		}
8317c478bd9Sstevel@tonic-gate 		if (uoff + n > ip->i_size) {
8327c478bd9Sstevel@tonic-gate 			/*
8337c478bd9Sstevel@tonic-gate 			 * We are extending the length of the file.
8347c478bd9Sstevel@tonic-gate 			 * bmap is used so that we are sure that
8357c478bd9Sstevel@tonic-gate 			 * if we need to allocate new blocks, that it
8367c478bd9Sstevel@tonic-gate 			 * is done here before we up the file size.
8377c478bd9Sstevel@tonic-gate 			 */
8387c478bd9Sstevel@tonic-gate 			error = bmap_write(ip, uoff, (int)(on + n),
839303bf60bSsdebnath 			    mapon == 0, NULL, cr);
8407c478bd9Sstevel@tonic-gate 			/*
8417c478bd9Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
8427c478bd9Sstevel@tonic-gate 			 * the flags are set it changed the file.
8437c478bd9Sstevel@tonic-gate 			 */
8447c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
8457c478bd9Sstevel@tonic-gate 				i_seq_needed = 1;
8467c478bd9Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			if (error)
8497c478bd9Sstevel@tonic-gate 				break;
8507c478bd9Sstevel@tonic-gate 			/*
8517c478bd9Sstevel@tonic-gate 			 * There is a window of vulnerability here.
8527c478bd9Sstevel@tonic-gate 			 * The sequence of operations: allocate file
8537c478bd9Sstevel@tonic-gate 			 * system blocks, uiomove the data into pages,
8547c478bd9Sstevel@tonic-gate 			 * and then update the size of the file in the
8557c478bd9Sstevel@tonic-gate 			 * inode, must happen atomically.  However, due
8567c478bd9Sstevel@tonic-gate 			 * to current locking constraints, this can not
8577c478bd9Sstevel@tonic-gate 			 * be done.
8587c478bd9Sstevel@tonic-gate 			 */
8597c478bd9Sstevel@tonic-gate 			ASSERT(ip->i_writer == NULL);
8607c478bd9Sstevel@tonic-gate 			ip->i_writer = curthread;
8617c478bd9Sstevel@tonic-gate 			i_size_changed = 1;
8627c478bd9Sstevel@tonic-gate 			/*
8637c478bd9Sstevel@tonic-gate 			 * If we are writing from the beginning of
8647c478bd9Sstevel@tonic-gate 			 * the mapping, we can just create the
8657c478bd9Sstevel@tonic-gate 			 * pages without having to read them.
8667c478bd9Sstevel@tonic-gate 			 */
8677c478bd9Sstevel@tonic-gate 			pagecreate = (mapon == 0);
8687c478bd9Sstevel@tonic-gate 		} else if (n == MAXBSIZE) {
8697c478bd9Sstevel@tonic-gate 			/*
8707c478bd9Sstevel@tonic-gate 			 * Going to do a whole mappings worth,
8717c478bd9Sstevel@tonic-gate 			 * so we can just create the pages w/o
8727c478bd9Sstevel@tonic-gate 			 * having to read them in.  But before
8737c478bd9Sstevel@tonic-gate 			 * we do that, we need to make sure any
8747c478bd9Sstevel@tonic-gate 			 * needed blocks are allocated first.
8757c478bd9Sstevel@tonic-gate 			 */
8767c478bd9Sstevel@tonic-gate 			iblocks = ip->i_blocks;
877303bf60bSsdebnath 			error = bmap_write(ip, uoff, (int)(on + n),
878303bf60bSsdebnath 			    BI_ALLOC_ONLY, NULL, cr);
8797c478bd9Sstevel@tonic-gate 			/*
8807c478bd9Sstevel@tonic-gate 			 * bmap_write never drops i_contents so if
8817c478bd9Sstevel@tonic-gate 			 * the flags are set it changed the file.
8827c478bd9Sstevel@tonic-gate 			 */
8837c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD)) {
8847c478bd9Sstevel@tonic-gate 				i_seq_needed = 1;
8857c478bd9Sstevel@tonic-gate 				ip->i_flag |= ISEQ;
8867c478bd9Sstevel@tonic-gate 			}
8877c478bd9Sstevel@tonic-gate 			if (error)
8887c478bd9Sstevel@tonic-gate 				break;
8897c478bd9Sstevel@tonic-gate 			pagecreate = 1;
8907c478bd9Sstevel@tonic-gate 			/*
8917c478bd9Sstevel@tonic-gate 			 * check if the new created page needed the
8927c478bd9Sstevel@tonic-gate 			 * allocation of new disk blocks.
8937c478bd9Sstevel@tonic-gate 			 */
8947c478bd9Sstevel@tonic-gate 			if (iblocks == ip->i_blocks)
8957c478bd9Sstevel@tonic-gate 				new_iblocks = 0; /* no new blocks allocated */
8967c478bd9Sstevel@tonic-gate 		} else {
8977c478bd9Sstevel@tonic-gate 			pagecreate = 0;
8987c478bd9Sstevel@tonic-gate 			/*
8997c478bd9Sstevel@tonic-gate 			 * In sync mode flush the indirect blocks which
9007c478bd9Sstevel@tonic-gate 			 * may have been allocated and not written on
9017c478bd9Sstevel@tonic-gate 			 * disk. In above cases bmap_write will allocate
9027c478bd9Sstevel@tonic-gate 			 * in sync mode.
9037c478bd9Sstevel@tonic-gate 			 */
9047c478bd9Sstevel@tonic-gate 			if (ioflag & (FSYNC|FDSYNC)) {
9057c478bd9Sstevel@tonic-gate 				error = ufs_indirblk_sync(ip, uoff);
9067c478bd9Sstevel@tonic-gate 				if (error)
9077c478bd9Sstevel@tonic-gate 					break;
9087c478bd9Sstevel@tonic-gate 			}
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one
9137c478bd9Sstevel@tonic-gate 		 * of two ways:
9147c478bd9Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
9157c478bd9Sstevel@tonic-gate 		 *    forcefault parameter is true (pagecreate == 0)
9167c478bd9Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
9177c478bd9Sstevel@tonic-gate 		 *
9187c478bd9Sstevel@tonic-gate 		 * We have to drop the contents lock to prevent the VM
919da6c28aaSamw 		 * system from trying to reacquire it in ufs_getpage()
9207c478bd9Sstevel@tonic-gate 		 * should the uiomove cause a pagefault.
9217c478bd9Sstevel@tonic-gate 		 *
9227c478bd9Sstevel@tonic-gate 		 * We have to drop the reader vfs_dqrwlock here as well.
9237c478bd9Sstevel@tonic-gate 		 */
9247c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
9257c478bd9Sstevel@tonic-gate 		if (do_dqrwlock) {
9267c478bd9Sstevel@tonic-gate 			ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
9277c478bd9Sstevel@tonic-gate 			ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
9287c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
9297c478bd9Sstevel@tonic-gate 		}
9307c478bd9Sstevel@tonic-gate 
931a5652762Spraks 		newpage = 0;
932a5652762Spraks 		premove_resid = uio->uio_resid;
9336f5f1c63SDonghai Qiao 
9346f5f1c63SDonghai Qiao 		/*
9356f5f1c63SDonghai Qiao 		 * Touch the page and fault it in if it is not in core
9366f5f1c63SDonghai Qiao 		 * before segmap_getmapflt or vpm_data_copy can lock it.
9376f5f1c63SDonghai Qiao 		 * This is to avoid the deadlock if the buffer is mapped
9386f5f1c63SDonghai Qiao 		 * to the same file through mmap which we want to write.
9396f5f1c63SDonghai Qiao 		 */
9406f5f1c63SDonghai Qiao 		uio_prefaultpages((long)n, uio);
9416f5f1c63SDonghai Qiao 
942a5652762Spraks 		if (vpm_enable) {
943a5652762Spraks 			/*
944a5652762Spraks 			 * Copy data. If new pages are created, part of
945a5652762Spraks 			 * the page that is not written will be initizliazed
946a5652762Spraks 			 * with zeros.
947a5652762Spraks 			 */
948a5652762Spraks 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
94980d34432Sfrankho 			    uio, !pagecreate, &newpage, 0, S_WRITE);
950a5652762Spraks 		} else {
951a5652762Spraks 
952a5652762Spraks 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
95380d34432Sfrankho 			    (uint_t)n, !pagecreate, S_WRITE);
9547c478bd9Sstevel@tonic-gate 
955a5652762Spraks 			/*
956a5652762Spraks 			 * segmap_pagecreate() returns 1 if it calls
957a5652762Spraks 			 * page_create_va() to allocate any pages.
958a5652762Spraks 			 */
9597c478bd9Sstevel@tonic-gate 
960a5652762Spraks 			if (pagecreate)
961a5652762Spraks 				newpage = segmap_pagecreate(segkmap, base,
962a5652762Spraks 				    (size_t)n, 0);
9637c478bd9Sstevel@tonic-gate 
964a5652762Spraks 			error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
965a5652762Spraks 		}
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 		/*
9687c478bd9Sstevel@tonic-gate 		 * If "newpage" is set, then a new page was created and it
9697c478bd9Sstevel@tonic-gate 		 * does not contain valid data, so it needs to be initialized
9707c478bd9Sstevel@tonic-gate 		 * at this point.
9717c478bd9Sstevel@tonic-gate 		 * Otherwise the page contains old data, which was overwritten
9727c478bd9Sstevel@tonic-gate 		 * partially or as a whole in uiomove.
9737c478bd9Sstevel@tonic-gate 		 * If there is only one iovec structure within uio, then
9747c478bd9Sstevel@tonic-gate 		 * on error uiomove will not be able to update uio->uio_loffset
9757c478bd9Sstevel@tonic-gate 		 * and we would zero the whole page here!
9767c478bd9Sstevel@tonic-gate 		 *
9777c478bd9Sstevel@tonic-gate 		 * If uiomove fails because of an error, the old valid data
9787c478bd9Sstevel@tonic-gate 		 * is kept instead of filling the rest of the page with zero's.
9797c478bd9Sstevel@tonic-gate 		 */
980a5652762Spraks 		if (!vpm_enable && newpage &&
9817c478bd9Sstevel@tonic-gate 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
9827c478bd9Sstevel@tonic-gate 			/*
9837c478bd9Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
9847c478bd9Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
9857c478bd9Sstevel@tonic-gate 			 * This happens on most EOF write cases and if
9867c478bd9Sstevel@tonic-gate 			 * we had some sort of error during the uiomove.
9877c478bd9Sstevel@tonic-gate 			 */
9887c478bd9Sstevel@tonic-gate 			int nzero, nmoved;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 			nmoved = (int)(uio->uio_loffset - (off + mapon));
9917c478bd9Sstevel@tonic-gate 			ASSERT(nmoved >= 0 && nmoved <= n);
9927c478bd9Sstevel@tonic-gate 			nzero = roundup(on + n, PAGESIZE) - nmoved;
9937c478bd9Sstevel@tonic-gate 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
9947c478bd9Sstevel@tonic-gate 			(void) kzero(base + mapon + nmoved, (uint_t)nzero);
9957c478bd9Sstevel@tonic-gate 		}
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		/*
9987c478bd9Sstevel@tonic-gate 		 * Unlock the pages allocated by page_create_va()
9997c478bd9Sstevel@tonic-gate 		 * in segmap_pagecreate()
10007c478bd9Sstevel@tonic-gate 		 */
1001a5652762Spraks 		if (!vpm_enable && newpage)
10027c478bd9Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		/*
10057c478bd9Sstevel@tonic-gate 		 * If the size of the file changed, then update the
10067c478bd9Sstevel@tonic-gate 		 * size field in the inode now.  This can't be done
10077c478bd9Sstevel@tonic-gate 		 * before the call to segmap_pageunlock or there is
10087c478bd9Sstevel@tonic-gate 		 * a potential deadlock with callers to ufs_putpage().
10097c478bd9Sstevel@tonic-gate 		 * They will be holding i_contents and trying to lock
10107c478bd9Sstevel@tonic-gate 		 * a page, while this thread is holding a page locked
10117c478bd9Sstevel@tonic-gate 		 * and trying to acquire i_contents.
10127c478bd9Sstevel@tonic-gate 		 */
10137c478bd9Sstevel@tonic-gate 		if (i_size_changed) {
10147c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
10157c478bd9Sstevel@tonic-gate 			old_i_size = ip->i_size;
10167c478bd9Sstevel@tonic-gate 			UFS_SET_ISIZE(uoff + n, ip);
10177c478bd9Sstevel@tonic-gate 			TRANS_INODE(ufsvfsp, ip);
10187c478bd9Sstevel@tonic-gate 			/*
10197c478bd9Sstevel@tonic-gate 			 * file has grown larger than 2GB. Set flag
10207c478bd9Sstevel@tonic-gate 			 * in superblock to indicate this, if it
10217c478bd9Sstevel@tonic-gate 			 * is not already set.
10227c478bd9Sstevel@tonic-gate 			 */
10237c478bd9Sstevel@tonic-gate 			if ((ip->i_size > MAXOFF32_T) &&
10247c478bd9Sstevel@tonic-gate 			    !(fs->fs_flags & FSLARGEFILES)) {
10257c478bd9Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
10267c478bd9Sstevel@tonic-gate 				mutex_enter(&ufsvfsp->vfs_lock);
10277c478bd9Sstevel@tonic-gate 				fs->fs_flags |= FSLARGEFILES;
10287c478bd9Sstevel@tonic-gate 				ufs_sbwrite(ufsvfsp);
10297c478bd9Sstevel@tonic-gate 				mutex_exit(&ufsvfsp->vfs_lock);
10307c478bd9Sstevel@tonic-gate 			}
10317c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
10327c478bd9Sstevel@tonic-gate 			ip->i_writer = NULL;
10337c478bd9Sstevel@tonic-gate 			cv_broadcast(&ip->i_wrcv);
10347c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
10357c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 		if (error) {
10397c478bd9Sstevel@tonic-gate 			/*
10407c478bd9Sstevel@tonic-gate 			 * If we failed on a write, we may have already
10417c478bd9Sstevel@tonic-gate 			 * allocated file blocks as well as pages.  It's
10427c478bd9Sstevel@tonic-gate 			 * hard to undo the block allocation, but we must
10437c478bd9Sstevel@tonic-gate 			 * be sure to invalidate any pages that may have
10447c478bd9Sstevel@tonic-gate 			 * been allocated.
10457c478bd9Sstevel@tonic-gate 			 *
10467c478bd9Sstevel@tonic-gate 			 * If the page was created without initialization
10477c478bd9Sstevel@tonic-gate 			 * then we must check if it should be possible
10487c478bd9Sstevel@tonic-gate 			 * to destroy the new page and to keep the old data
10497c478bd9Sstevel@tonic-gate 			 * on the disk.
10507c478bd9Sstevel@tonic-gate 			 *
10517c478bd9Sstevel@tonic-gate 			 * It is possible to destroy the page without
10527c478bd9Sstevel@tonic-gate 			 * having to write back its contents only when
10537c478bd9Sstevel@tonic-gate 			 * - the size of the file keeps unchanged
10547c478bd9Sstevel@tonic-gate 			 * - bmap_write() did not allocate new disk blocks
10557c478bd9Sstevel@tonic-gate 			 *   it is possible to create big files using "seek" and
10567c478bd9Sstevel@tonic-gate 			 *   write to the end of the file. A "write" to a
10577c478bd9Sstevel@tonic-gate 			 *   position before the end of the file would not
10587c478bd9Sstevel@tonic-gate 			 *   change the size of the file but it would allocate
10597c478bd9Sstevel@tonic-gate 			 *   new disk blocks.
10607c478bd9Sstevel@tonic-gate 			 * - uiomove intended to overwrite the whole page.
10617c478bd9Sstevel@tonic-gate 			 * - a new page was created (newpage == 1).
10627c478bd9Sstevel@tonic-gate 			 */
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 			if (i_size_changed == 0 && new_iblocks == 0 &&
10657c478bd9Sstevel@tonic-gate 			    newpage) {
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 				/* unwind what uiomove eventually last did */
10687c478bd9Sstevel@tonic-gate 				uio->uio_resid = premove_resid;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 				/*
10717c478bd9Sstevel@tonic-gate 				 * destroy the page, do not write ambiguous
10727c478bd9Sstevel@tonic-gate 				 * data to the disk.
10737c478bd9Sstevel@tonic-gate 				 */
10747c478bd9Sstevel@tonic-gate 				flags = SM_DESTROY;
10757c478bd9Sstevel@tonic-gate 			} else {
10767c478bd9Sstevel@tonic-gate 				/*
10777c478bd9Sstevel@tonic-gate 				 * write the page back to the disk, if dirty,
10787c478bd9Sstevel@tonic-gate 				 * and remove the page from the cache.
10797c478bd9Sstevel@tonic-gate 				 */
10807c478bd9Sstevel@tonic-gate 				flags = SM_INVAL;
10817c478bd9Sstevel@tonic-gate 			}
1082a5652762Spraks 
1083a5652762Spraks 			if (vpm_enable) {
1084a5652762Spraks 				/*
1085a5652762Spraks 				 *  Flush pages.
1086a5652762Spraks 				 */
1087a5652762Spraks 				(void) vpm_sync_pages(vp, off, n, flags);
1088a5652762Spraks 			} else {
1089a5652762Spraks 				(void) segmap_release(segkmap, base, flags);
1090a5652762Spraks 			}
10917c478bd9Sstevel@tonic-gate 		} else {
10927c478bd9Sstevel@tonic-gate 			flags = 0;
10937c478bd9Sstevel@tonic-gate 			/*
10947c478bd9Sstevel@tonic-gate 			 * Force write back for synchronous write cases.
10957c478bd9Sstevel@tonic-gate 			 */
10967c478bd9Sstevel@tonic-gate 			if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
10977c478bd9Sstevel@tonic-gate 				/*
10987c478bd9Sstevel@tonic-gate 				 * If the sticky bit is set but the
10997c478bd9Sstevel@tonic-gate 				 * execute bit is not set, we do a
11007c478bd9Sstevel@tonic-gate 				 * synchronous write back and free
11017c478bd9Sstevel@tonic-gate 				 * the page when done.  We set up swap
11027c478bd9Sstevel@tonic-gate 				 * files to be handled this way to
11037c478bd9Sstevel@tonic-gate 				 * prevent servers from keeping around
11047c478bd9Sstevel@tonic-gate 				 * the client's swap pages too long.
11057c478bd9Sstevel@tonic-gate 				 * XXX - there ought to be a better way.
11067c478bd9Sstevel@tonic-gate 				 */
11077c478bd9Sstevel@tonic-gate 				if (IS_SWAPVP(vp)) {
11087c478bd9Sstevel@tonic-gate 					flags = SM_WRITE | SM_FREE |
11097c478bd9Sstevel@tonic-gate 					    SM_DONTNEED;
11107c478bd9Sstevel@tonic-gate 					iupdat_flag = 0;
11117c478bd9Sstevel@tonic-gate 				} else {
11127c478bd9Sstevel@tonic-gate 					flags = SM_WRITE;
11137c478bd9Sstevel@tonic-gate 				}
11147c478bd9Sstevel@tonic-gate 			} else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
11157c478bd9Sstevel@tonic-gate 				/*
11167c478bd9Sstevel@tonic-gate 				 * Have written a whole block.
11177c478bd9Sstevel@tonic-gate 				 * Start an asynchronous write and
11187c478bd9Sstevel@tonic-gate 				 * mark the buffer to indicate that
11197c478bd9Sstevel@tonic-gate 				 * it won't be needed again soon.
11207c478bd9Sstevel@tonic-gate 				 */
11217c478bd9Sstevel@tonic-gate 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
11227c478bd9Sstevel@tonic-gate 			}
1123a5652762Spraks 			if (vpm_enable) {
1124a5652762Spraks 				/*
1125a5652762Spraks 				 * Flush pages.
1126a5652762Spraks 				 */
11273bd1497bSpraks 				error = vpm_sync_pages(vp, off, n, flags);
1128a5652762Spraks 			} else {
11293bd1497bSpraks 				error = segmap_release(segkmap, base, flags);
1130a5652762Spraks 			}
11317c478bd9Sstevel@tonic-gate 			/*
11327c478bd9Sstevel@tonic-gate 			 * If the operation failed and is synchronous,
11337c478bd9Sstevel@tonic-gate 			 * then we need to unwind what uiomove() last
11347c478bd9Sstevel@tonic-gate 			 * did so we can potentially return an error to
11357c478bd9Sstevel@tonic-gate 			 * the caller.  If this write operation was
11367c478bd9Sstevel@tonic-gate 			 * done in two pieces and the first succeeded,
11377c478bd9Sstevel@tonic-gate 			 * then we won't return an error for the second
11387c478bd9Sstevel@tonic-gate 			 * piece that failed.  However, we only want to
11397c478bd9Sstevel@tonic-gate 			 * return a resid value that reflects what was
11407c478bd9Sstevel@tonic-gate 			 * really done.
11417c478bd9Sstevel@tonic-gate 			 *
11427c478bd9Sstevel@tonic-gate 			 * Failures for non-synchronous operations can
11437c478bd9Sstevel@tonic-gate 			 * be ignored since the page subsystem will
11447c478bd9Sstevel@tonic-gate 			 * retry the operation until it succeeds or the
11457c478bd9Sstevel@tonic-gate 			 * file system is unmounted.
11467c478bd9Sstevel@tonic-gate 			 */
11477c478bd9Sstevel@tonic-gate 			if (error) {
11487c478bd9Sstevel@tonic-gate 				if ((ioflag & (FSYNC | FDSYNC)) ||
11497c478bd9Sstevel@tonic-gate 				    type == IFDIR) {
11507c478bd9Sstevel@tonic-gate 					uio->uio_resid = premove_resid;
11517c478bd9Sstevel@tonic-gate 				} else {
11527c478bd9Sstevel@tonic-gate 					error = 0;
11537c478bd9Sstevel@tonic-gate 				}
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 		}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 		/*
11587c478bd9Sstevel@tonic-gate 		 * Re-acquire contents lock.
11597c478bd9Sstevel@tonic-gate 		 * If it was dropped, reacquire reader vfs_dqrwlock as well.
11607c478bd9Sstevel@tonic-gate 		 */
11617c478bd9Sstevel@tonic-gate 		if (do_dqrwlock)
11627c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
11637c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 		/*
11667c478bd9Sstevel@tonic-gate 		 * If the uiomove() failed or if a synchronous
11677c478bd9Sstevel@tonic-gate 		 * page push failed, fix up i_size.
11687c478bd9Sstevel@tonic-gate 		 */
11697c478bd9Sstevel@tonic-gate 		if (error) {
11707c478bd9Sstevel@tonic-gate 			if (i_size_changed) {
11717c478bd9Sstevel@tonic-gate 				/*
11727c478bd9Sstevel@tonic-gate 				 * The uiomove failed, and we
11737c478bd9Sstevel@tonic-gate 				 * allocated blocks,so get rid
11747c478bd9Sstevel@tonic-gate 				 * of them.
11757c478bd9Sstevel@tonic-gate 				 */
11767c478bd9Sstevel@tonic-gate 				(void) ufs_itrunc(ip, old_i_size, 0, cr);
11777c478bd9Sstevel@tonic-gate 			}
11787c478bd9Sstevel@tonic-gate 		} else {
11797c478bd9Sstevel@tonic-gate 			/*
11807c478bd9Sstevel@tonic-gate 			 * XXX - Can this be out of the loop?
11817c478bd9Sstevel@tonic-gate 			 */
11827c478bd9Sstevel@tonic-gate 			ip->i_flag |= IUPD | ICHG;
11837c478bd9Sstevel@tonic-gate 			/*
11847c478bd9Sstevel@tonic-gate 			 * Only do one increase of i_seq for multiple
11857c478bd9Sstevel@tonic-gate 			 * pieces.  Because we drop locks, record
11867c478bd9Sstevel@tonic-gate 			 * the fact that we changed the timestamp and
11877c478bd9Sstevel@tonic-gate 			 * are deferring the increase in case another thread
11887c478bd9Sstevel@tonic-gate 			 * pushes our timestamp update.
11897c478bd9Sstevel@tonic-gate 			 */
11907c478bd9Sstevel@tonic-gate 			i_seq_needed = 1;
11917c478bd9Sstevel@tonic-gate 			ip->i_flag |= ISEQ;
11927c478bd9Sstevel@tonic-gate 			if (i_size_changed)
11937c478bd9Sstevel@tonic-gate 				ip->i_flag |= IATTCHG;
11947c478bd9Sstevel@tonic-gate 			if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
11957c478bd9Sstevel@tonic-gate 			    (IEXEC >> 6))) != 0 &&
11967c478bd9Sstevel@tonic-gate 			    (ip->i_mode & (ISUID | ISGID)) != 0 &&
11977c478bd9Sstevel@tonic-gate 			    secpolicy_vnode_setid_retain(cr,
11987c478bd9Sstevel@tonic-gate 			    (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
11997c478bd9Sstevel@tonic-gate 				/*
12007c478bd9Sstevel@tonic-gate 				 * Clear Set-UID & Set-GID bits on
12017c478bd9Sstevel@tonic-gate 				 * successful write if not privileged
12027c478bd9Sstevel@tonic-gate 				 * and at least one of the execute bits
12037c478bd9Sstevel@tonic-gate 				 * is set.  If we always clear Set-GID,
12047c478bd9Sstevel@tonic-gate 				 * mandatory file and record locking is
12057c478bd9Sstevel@tonic-gate 				 * unuseable.
12067c478bd9Sstevel@tonic-gate 				 */
12077c478bd9Sstevel@tonic-gate 				ip->i_mode &= ~(ISUID | ISGID);
12087c478bd9Sstevel@tonic-gate 			}
12097c478bd9Sstevel@tonic-gate 		}
1210f90bab20Sswilcox 		/*
1211f90bab20Sswilcox 		 * In the case the FDSYNC flag is set and this is a
1212f90bab20Sswilcox 		 * "rewrite" we won't log a delta.
1213f90bab20Sswilcox 		 * The FSYNC flag overrides all cases.
1214f90bab20Sswilcox 		 */
1215f90bab20Sswilcox 		if (!ufs_check_rewrite(ip, uio, ioflag) || !(ioflag & FDSYNC)) {
1216f90bab20Sswilcox 			TRANS_INODE(ufsvfsp, ip);
1217f90bab20Sswilcox 		}
12187c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate out:
12217c478bd9Sstevel@tonic-gate 	/*
12227c478bd9Sstevel@tonic-gate 	 * Make sure i_seq is increased at least once per write
12237c478bd9Sstevel@tonic-gate 	 */
12247c478bd9Sstevel@tonic-gate 	if (i_seq_needed) {
12257c478bd9Sstevel@tonic-gate 		ip->i_seq++;
12267c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~ISEQ;	/* no longer deferred */
12277c478bd9Sstevel@tonic-gate 	}
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table -
12317c478bd9Sstevel@tonic-gate 	 *
12327c478bd9Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
12337c478bd9Sstevel@tonic-gate 	 *   --------------------------
12347c478bd9Sstevel@tonic-gate 	 *   always@	  IATTCHG|IBDWRITE
12357c478bd9Sstevel@tonic-gate 	 *
12362575b441SToomas Soome 	 * @ -	If we are doing synchronous write the only time we should
12377c478bd9Sstevel@tonic-gate 	 *	not be sync'ing the ip here is if we have the stickyhack
12387c478bd9Sstevel@tonic-gate 	 *	activated, the file is marked with the sticky bit and
12397c478bd9Sstevel@tonic-gate 	 *	no exec bit, the file length has not been changed and
12407c478bd9Sstevel@tonic-gate 	 *	no new blocks have been allocated during this write.
12417c478bd9Sstevel@tonic-gate 	 */
12427c478bd9Sstevel@tonic-gate 
12437c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & ISYNC) != 0) {
12447c478bd9Sstevel@tonic-gate 		/*
12457c478bd9Sstevel@tonic-gate 		 * we have eliminated nosync
12467c478bd9Sstevel@tonic-gate 		 */
12477c478bd9Sstevel@tonic-gate 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
124880d34432Sfrankho 		    ((ioflag & FSYNC) && iupdat_flag)) {
12497c478bd9Sstevel@tonic-gate 			ufs_iupdat(ip, 1);
12507c478bd9Sstevel@tonic-gate 		}
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/*
12547c478bd9Sstevel@tonic-gate 	 * If we've already done a partial-write, terminate
12557c478bd9Sstevel@tonic-gate 	 * the write but return no error unless the error is ENOSPC
12567c478bd9Sstevel@tonic-gate 	 * because the caller can detect this and free resources and
12577c478bd9Sstevel@tonic-gate 	 * try again.
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	if ((start_resid != uio->uio_resid) && (error != ENOSPC))
12607c478bd9Sstevel@tonic-gate 		error = 0;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~(INOACC | ISYNC);
12637c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);
12647c478bd9Sstevel@tonic-gate 	return (error);
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate /*
12687c478bd9Sstevel@tonic-gate  * rdip does the real work of read requests for ufs.
12697c478bd9Sstevel@tonic-gate  */
12707c478bd9Sstevel@tonic-gate int
rdip(struct inode * ip,struct uio * uio,int ioflag,cred_t * cr)12717c478bd9Sstevel@tonic-gate rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	u_offset_t off;
12747c478bd9Sstevel@tonic-gate 	caddr_t base;
12757c478bd9Sstevel@tonic-gate 	struct fs *fs;
12767c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
12777c478bd9Sstevel@tonic-gate 	struct vnode *vp;
12787c478bd9Sstevel@tonic-gate 	long oresid = uio->uio_resid;
12797c478bd9Sstevel@tonic-gate 	u_offset_t n, on, mapon;
12807c478bd9Sstevel@tonic-gate 	int error = 0;
12817c478bd9Sstevel@tonic-gate 	int doupdate = 1;
1282562eee46Srbourbon 	uint_t flags;
12839c65d7dbSBryan Cantrill 	int directio_status;
12847c478bd9Sstevel@tonic-gate 	krw_t rwtype;
12857c478bd9Sstevel@tonic-gate 	o_mode_t type;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	vp = ITOV(ip);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
12947c478bd9Sstevel@tonic-gate 		return (EIO);
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	/* check for valid filetype */
12997c478bd9Sstevel@tonic-gate 	type = ip->i_mode & IFMT;
13007c478bd9Sstevel@tonic-gate 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
13017c478bd9Sstevel@tonic-gate 	    (type != IFLNK) && (type != IFSHAD)) {
13027c478bd9Sstevel@tonic-gate 		return (EIO);
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 
13057c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset > UFS_MAXOFFSET_T) {
13067c478bd9Sstevel@tonic-gate 		error = 0;
13077c478bd9Sstevel@tonic-gate 		goto out;
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 	if (uio->uio_loffset < (offset_t)0) {
13107c478bd9Sstevel@tonic-gate 		return (EINVAL);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 	if (uio->uio_resid == 0) {
13137c478bd9Sstevel@tonic-gate 		return (0);
13147c478bd9Sstevel@tonic-gate 	}
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
131780d34432Sfrankho 	    (!ufsvfsp->vfs_noatime)) {
13187c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
13197c478bd9Sstevel@tonic-gate 		ip->i_flag |= IACC;
13207c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 	/*
13237c478bd9Sstevel@tonic-gate 	 * Try to go direct
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
13267c478bd9Sstevel@tonic-gate 		error = ufs_directio_read(ip, uio, cr, &directio_status);
13277c478bd9Sstevel@tonic-gate 		if (directio_status == DIRECTIO_SUCCESS)
13287c478bd9Sstevel@tonic-gate 			goto out;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	do {
13347c478bd9Sstevel@tonic-gate 		offset_t diff;
13357c478bd9Sstevel@tonic-gate 		u_offset_t uoff = uio->uio_loffset;
13367c478bd9Sstevel@tonic-gate 		off = uoff & (offset_t)MAXBMASK;
13377c478bd9Sstevel@tonic-gate 		mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
13387c478bd9Sstevel@tonic-gate 		on = (u_offset_t)blkoff(fs, uoff);
13397c478bd9Sstevel@tonic-gate 		n = MIN((u_offset_t)fs->fs_bsize - on,
134080d34432Sfrankho 		    (u_offset_t)uio->uio_resid);
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate 		diff = ip->i_size - uoff;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 		if (diff <= (offset_t)0) {
13457c478bd9Sstevel@tonic-gate 			error = 0;
13467c478bd9Sstevel@tonic-gate 			goto out;
13477c478bd9Sstevel@tonic-gate 		}
13487c478bd9Sstevel@tonic-gate 		if (diff < (offset_t)n)
13497c478bd9Sstevel@tonic-gate 			n = (int)diff;
1350562eee46Srbourbon 
13517c478bd9Sstevel@tonic-gate 		/*
13527c478bd9Sstevel@tonic-gate 		 * At this point we can enter ufs_getpage() in one of two
13537c478bd9Sstevel@tonic-gate 		 * ways:
13547c478bd9Sstevel@tonic-gate 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
13557c478bd9Sstevel@tonic-gate 		 *    forcefault parameter is true (value of 1 is passed)
13567c478bd9Sstevel@tonic-gate 		 * 2) uiomove() causes a page fault.
13577c478bd9Sstevel@tonic-gate 		 *
13587c478bd9Sstevel@tonic-gate 		 * We cannot hold onto an i_contents reader lock without
13597c478bd9Sstevel@tonic-gate 		 * risking deadlock in ufs_getpage() so drop a reader lock.
13607c478bd9Sstevel@tonic-gate 		 * The ufs_getpage() dolock logic already allows for a
13617c478bd9Sstevel@tonic-gate 		 * thread holding i_contents as writer to work properly
13627c478bd9Sstevel@tonic-gate 		 * so we keep a writer lock.
13637c478bd9Sstevel@tonic-gate 		 */
13647c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER)
13657c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
13667c478bd9Sstevel@tonic-gate 
1367a5652762Spraks 		if (vpm_enable) {
1368a5652762Spraks 			/*
1369a5652762Spraks 			 * Copy data.
1370a5652762Spraks 			 */
1371a5652762Spraks 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
137280d34432Sfrankho 			    uio, 1, NULL, 0, S_READ);
1373a5652762Spraks 		} else {
1374a5652762Spraks 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
137580d34432Sfrankho 			    (uint_t)n, 1, S_READ);
1376a5652762Spraks 			error = uiomove(base + mapon, (long)n, UIO_READ, uio);
1377a5652762Spraks 		}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 		flags = 0;
13807c478bd9Sstevel@tonic-gate 		if (!error) {
13817c478bd9Sstevel@tonic-gate 			/*
13827c478bd9Sstevel@tonic-gate 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
13837c478bd9Sstevel@tonic-gate 			 * we want to make sure that the page which has
13847c478bd9Sstevel@tonic-gate 			 * been read, is written on disk if it is dirty.
13857c478bd9Sstevel@tonic-gate 			 * And corresponding indirect blocks should also
13867c478bd9Sstevel@tonic-gate 			 * be flushed out.
13877c478bd9Sstevel@tonic-gate 			 */
13887c478bd9Sstevel@tonic-gate 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
13897c478bd9Sstevel@tonic-gate 				flags |= SM_WRITE;
13907c478bd9Sstevel@tonic-gate 			}
1391a5652762Spraks 			if (vpm_enable) {
1392a5652762Spraks 				error = vpm_sync_pages(vp, off, n, flags);
1393a5652762Spraks 			} else {
1394a5652762Spraks 				error = segmap_release(segkmap, base, flags);
1395a5652762Spraks 			}
1396a5652762Spraks 		} else {
1397a5652762Spraks 			if (vpm_enable) {
1398a5652762Spraks 				(void) vpm_sync_pages(vp, off, n, flags);
1399a5652762Spraks 			} else {
1400a5652762Spraks 				(void) segmap_release(segkmap, base, flags);
1401a5652762Spraks 			}
1402a5652762Spraks 		}
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 		if (rwtype == RW_READER)
14057c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, rwtype);
14067c478bd9Sstevel@tonic-gate 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
14077c478bd9Sstevel@tonic-gate out:
14087c478bd9Sstevel@tonic-gate 	/*
14097c478bd9Sstevel@tonic-gate 	 * Inode is updated according to this table if FRSYNC is set.
14107c478bd9Sstevel@tonic-gate 	 *
14117c478bd9Sstevel@tonic-gate 	 *   FSYNC	  FDSYNC(posix.4)
14127c478bd9Sstevel@tonic-gate 	 *   --------------------------
14137c478bd9Sstevel@tonic-gate 	 *   always	  IATTCHG|IBDWRITE
14147c478bd9Sstevel@tonic-gate 	 */
14157c478bd9Sstevel@tonic-gate 	/*
14167c478bd9Sstevel@tonic-gate 	 * The inode is not updated if we're logging and the inode is a
14177c478bd9Sstevel@tonic-gate 	 * directory with FRSYNC, FSYNC and FDSYNC flags set.
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	if (ioflag & FRSYNC) {
14207c478bd9Sstevel@tonic-gate 		if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
1421986fd29aSsetje 			doupdate = 0;
14227c478bd9Sstevel@tonic-gate 		}
14237c478bd9Sstevel@tonic-gate 		if (doupdate) {
14247c478bd9Sstevel@tonic-gate 			if ((ioflag & FSYNC) ||
14257c478bd9Sstevel@tonic-gate 			    ((ioflag & FDSYNC) &&
14267c478bd9Sstevel@tonic-gate 			    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
14277c478bd9Sstevel@tonic-gate 				ufs_iupdat(ip, 1);
14287c478bd9Sstevel@tonic-gate 			}
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 	}
14317c478bd9Sstevel@tonic-gate 	/*
14327c478bd9Sstevel@tonic-gate 	 * If we've already done a partial read, terminate
14337c478bd9Sstevel@tonic-gate 	 * the read but return no error.
14347c478bd9Sstevel@tonic-gate 	 */
14357c478bd9Sstevel@tonic-gate 	if (oresid != uio->uio_resid)
14367c478bd9Sstevel@tonic-gate 		error = 0;
14377c478bd9Sstevel@tonic-gate 	ITIMES(ip);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	return (error);
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate /* ARGSUSED */
14437c478bd9Sstevel@tonic-gate static int
ufs_ioctl(struct vnode * vp,int cmd,intptr_t arg,int flag,struct cred * cr,int * rvalp,caller_context_t * ct)14447c478bd9Sstevel@tonic-gate ufs_ioctl(
14457c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
14467c478bd9Sstevel@tonic-gate 	int		cmd,
14477c478bd9Sstevel@tonic-gate 	intptr_t	arg,
14487c478bd9Sstevel@tonic-gate 	int		flag,
14497c478bd9Sstevel@tonic-gate 	struct cred	*cr,
1450da6c28aaSamw 	int		*rvalp,
1451da6c28aaSamw 	caller_context_t *ct)
14527c478bd9Sstevel@tonic-gate {
14537c478bd9Sstevel@tonic-gate 	struct lockfs	lockfs, lockfs_out;
14547c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
14557c478bd9Sstevel@tonic-gate 	char		*comment, *original_comment;
14567c478bd9Sstevel@tonic-gate 	struct fs	*fs;
14577c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
14587c478bd9Sstevel@tonic-gate 	offset_t	off;
14597c478bd9Sstevel@tonic-gate 	extern int	maxphys;
14607c478bd9Sstevel@tonic-gate 	int		error;
14617c478bd9Sstevel@tonic-gate 	int		issync;
14627c478bd9Sstevel@tonic-gate 	int		trans_size;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	/*
14667c478bd9Sstevel@tonic-gate 	 * forcibly unmounted
14677c478bd9Sstevel@tonic-gate 	 */
146802ffed0eSjr 	if (ufsvfsp == NULL || vp->v_vfsp == NULL ||
146902ffed0eSjr 	    vp->v_vfsp->vfs_flag & VFS_UNMOUNTED)
14707c478bd9Sstevel@tonic-gate 		return (EIO);
14717c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 	if (cmd == Q_QUOTACTL) {
14747c478bd9Sstevel@tonic-gate 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
14757c478bd9Sstevel@tonic-gate 		if (error)
14767c478bd9Sstevel@tonic-gate 			return (error);
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		if (ulp) {
14797c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
148080d34432Sfrankho 			    TOP_SETQUOTA_SIZE(fs));
14817c478bd9Sstevel@tonic-gate 		}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		error = quotactl(vp, arg, flag, cr);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 		if (ulp) {
14867c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
148780d34432Sfrankho 			    TOP_SETQUOTA_SIZE(fs));
14887c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
14897c478bd9Sstevel@tonic-gate 		}
14907c478bd9Sstevel@tonic-gate 		return (error);
14917c478bd9Sstevel@tonic-gate 	}
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	switch (cmd) {
14947c478bd9Sstevel@tonic-gate 		case _FIOLFS:
14957c478bd9Sstevel@tonic-gate 			/*
14967c478bd9Sstevel@tonic-gate 			 * file system locking
14977c478bd9Sstevel@tonic-gate 			 */
14987c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
14997c478bd9Sstevel@tonic-gate 				return (EPERM);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
15027c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
150380d34432Sfrankho 				    sizeof (struct lockfs)))
15047c478bd9Sstevel@tonic-gate 					return (EFAULT);
15057c478bd9Sstevel@tonic-gate 			}
15067c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
15077c478bd9Sstevel@tonic-gate 			else {
15087c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
15097c478bd9Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
15107c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
15117c478bd9Sstevel@tonic-gate 				    sizeof (struct lockfs32)))
15127c478bd9Sstevel@tonic-gate 					return (EFAULT);
15137c478bd9Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
15147c478bd9Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
15157c478bd9Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
15167c478bd9Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
15177c478bd9Sstevel@tonic-gate 				lockfs.lf_comment =
151880d34432Sfrankho 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
15197c478bd9Sstevel@tonic-gate 			}
15207c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 			if (lockfs.lf_comlen) {
15237c478bd9Sstevel@tonic-gate 				if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
15247c478bd9Sstevel@tonic-gate 					return (ENAMETOOLONG);
152580d34432Sfrankho 				comment =
152680d34432Sfrankho 				    kmem_alloc(lockfs.lf_comlen, KM_SLEEP);
15277c478bd9Sstevel@tonic-gate 				if (copyin(lockfs.lf_comment, comment,
152880d34432Sfrankho 				    lockfs.lf_comlen)) {
15297c478bd9Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
15307c478bd9Sstevel@tonic-gate 					return (EFAULT);
15317c478bd9Sstevel@tonic-gate 				}
15327c478bd9Sstevel@tonic-gate 				original_comment = lockfs.lf_comment;
15337c478bd9Sstevel@tonic-gate 				lockfs.lf_comment = comment;
15347c478bd9Sstevel@tonic-gate 			}
15357c478bd9Sstevel@tonic-gate 			if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
15367c478bd9Sstevel@tonic-gate 				lockfs.lf_comment = original_comment;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 				if ((flag & DATAMODEL_MASK) ==
15397c478bd9Sstevel@tonic-gate 				    DATAMODEL_NATIVE) {
15407c478bd9Sstevel@tonic-gate 					(void) copyout(&lockfs, (caddr_t)arg,
15417c478bd9Sstevel@tonic-gate 					    sizeof (struct lockfs));
15427c478bd9Sstevel@tonic-gate 				}
15437c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
15447c478bd9Sstevel@tonic-gate 				else {
15457c478bd9Sstevel@tonic-gate 					struct lockfs32	lockfs32;
15467c478bd9Sstevel@tonic-gate 					/* Translate LP64 to ILP32 lockfs */
15477c478bd9Sstevel@tonic-gate 					lockfs32.lf_lock =
15487c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_lock;
15497c478bd9Sstevel@tonic-gate 					lockfs32.lf_flags =
15507c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_flags;
15517c478bd9Sstevel@tonic-gate 					lockfs32.lf_key =
15527c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_key;
15537c478bd9Sstevel@tonic-gate 					lockfs32.lf_comlen =
15547c478bd9Sstevel@tonic-gate 					    (uint32_t)lockfs.lf_comlen;
15557c478bd9Sstevel@tonic-gate 					lockfs32.lf_comment =
155680d34432Sfrankho 					    (uint32_t)(uintptr_t)
155780d34432Sfrankho 					    lockfs.lf_comment;
15587c478bd9Sstevel@tonic-gate 					(void) copyout(&lockfs32, (caddr_t)arg,
15597c478bd9Sstevel@tonic-gate 					    sizeof (struct lockfs32));
15607c478bd9Sstevel@tonic-gate 				}
15617c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 			} else {
15647c478bd9Sstevel@tonic-gate 				if (lockfs.lf_comlen)
15657c478bd9Sstevel@tonic-gate 					kmem_free(comment, lockfs.lf_comlen);
15667c478bd9Sstevel@tonic-gate 			}
15677c478bd9Sstevel@tonic-gate 			return (error);
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 		case _FIOLFSS:
15707c478bd9Sstevel@tonic-gate 			/*
15717c478bd9Sstevel@tonic-gate 			 * get file system locking status
15727c478bd9Sstevel@tonic-gate 			 */
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
15757c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs,
157680d34432Sfrankho 				    sizeof (struct lockfs)))
15777c478bd9Sstevel@tonic-gate 					return (EFAULT);
15787c478bd9Sstevel@tonic-gate 			}
15797c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
15807c478bd9Sstevel@tonic-gate 			else {
15817c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
15827c478bd9Sstevel@tonic-gate 				/* Translate ILP32 lockfs to LP64 lockfs */
15837c478bd9Sstevel@tonic-gate 				if (copyin((caddr_t)arg, &lockfs32,
158480d34432Sfrankho 				    sizeof (struct lockfs32)))
15857c478bd9Sstevel@tonic-gate 					return (EFAULT);
15867c478bd9Sstevel@tonic-gate 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
15877c478bd9Sstevel@tonic-gate 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
15887c478bd9Sstevel@tonic-gate 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
15897c478bd9Sstevel@tonic-gate 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
15907c478bd9Sstevel@tonic-gate 				lockfs.lf_comment =
159180d34432Sfrankho 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
15927c478bd9Sstevel@tonic-gate 			}
15937c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 			if (error =  ufs_fiolfss(vp, &lockfs_out))
15967c478bd9Sstevel@tonic-gate 				return (error);
15977c478bd9Sstevel@tonic-gate 			lockfs.lf_lock = lockfs_out.lf_lock;
15987c478bd9Sstevel@tonic-gate 			lockfs.lf_key = lockfs_out.lf_key;
15997c478bd9Sstevel@tonic-gate 			lockfs.lf_flags = lockfs_out.lf_flags;
16007c478bd9Sstevel@tonic-gate 			lockfs.lf_comlen = MIN(lockfs.lf_comlen,
160180d34432Sfrankho 			    lockfs_out.lf_comlen);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
16047c478bd9Sstevel@tonic-gate 				if (copyout(&lockfs, (caddr_t)arg,
160580d34432Sfrankho 				    sizeof (struct lockfs)))
16067c478bd9Sstevel@tonic-gate 					return (EFAULT);
16077c478bd9Sstevel@tonic-gate 			}
16087c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16097c478bd9Sstevel@tonic-gate 			else {
16107c478bd9Sstevel@tonic-gate 				/* Translate LP64 to ILP32 lockfs */
16117c478bd9Sstevel@tonic-gate 				struct lockfs32	lockfs32;
16127c478bd9Sstevel@tonic-gate 				lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
16137c478bd9Sstevel@tonic-gate 				lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
16147c478bd9Sstevel@tonic-gate 				lockfs32.lf_key = (uint32_t)lockfs.lf_key;
16157c478bd9Sstevel@tonic-gate 				lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
16167c478bd9Sstevel@tonic-gate 				lockfs32.lf_comment =
161780d34432Sfrankho 				    (uint32_t)(uintptr_t)lockfs.lf_comment;
16187c478bd9Sstevel@tonic-gate 				if (copyout(&lockfs32, (caddr_t)arg,
161980d34432Sfrankho 				    sizeof (struct lockfs32)))
16207c478bd9Sstevel@tonic-gate 					return (EFAULT);
16217c478bd9Sstevel@tonic-gate 			}
16227c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 			if (lockfs.lf_comlen &&
16257c478bd9Sstevel@tonic-gate 			    lockfs.lf_comment && lockfs_out.lf_comment)
16267c478bd9Sstevel@tonic-gate 				if (copyout(lockfs_out.lf_comment,
162780d34432Sfrankho 				    lockfs.lf_comment, lockfs.lf_comlen))
16287c478bd9Sstevel@tonic-gate 					return (EFAULT);
16297c478bd9Sstevel@tonic-gate 			return (0);
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 		case _FIOSATIME:
16327c478bd9Sstevel@tonic-gate 			/*
16337c478bd9Sstevel@tonic-gate 			 * set access time
16347c478bd9Sstevel@tonic-gate 			 */
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 			/*
16377c478bd9Sstevel@tonic-gate 			 * if mounted w/o atime, return quietly.
16387c478bd9Sstevel@tonic-gate 			 * I briefly thought about returning ENOSYS, but
16397c478bd9Sstevel@tonic-gate 			 * figured that most apps would consider this fatal
16407c478bd9Sstevel@tonic-gate 			 * but the idea is to make this as seamless as poss.
16417c478bd9Sstevel@tonic-gate 			 */
16427c478bd9Sstevel@tonic-gate 			if (ufsvfsp->vfs_noatime)
16437c478bd9Sstevel@tonic-gate 				return (0);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
164680d34432Sfrankho 			    ULOCKFS_SETATTR_MASK);
16477c478bd9Sstevel@tonic-gate 			if (error)
16487c478bd9Sstevel@tonic-gate 				return (error);
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 			if (ulp) {
16517c478bd9Sstevel@tonic-gate 				trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
16527c478bd9Sstevel@tonic-gate 				TRANS_BEGIN_CSYNC(ufsvfsp, issync,
165380d34432Sfrankho 				    TOP_SETATTR, trans_size);
16547c478bd9Sstevel@tonic-gate 			}
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 			error = ufs_fiosatime(vp, (struct timeval *)arg,
165780d34432Sfrankho 			    flag, cr);
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 			if (ulp) {
16607c478bd9Sstevel@tonic-gate 				TRANS_END_CSYNC(ufsvfsp, error, issync,
166180d34432Sfrankho 				    TOP_SETATTR, trans_size);
16627c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
16637c478bd9Sstevel@tonic-gate 			}
16647c478bd9Sstevel@tonic-gate 			return (error);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		case _FIOSDIO:
16677c478bd9Sstevel@tonic-gate 			/*
16687c478bd9Sstevel@tonic-gate 			 * set delayed-io
16697c478bd9Sstevel@tonic-gate 			 */
16707c478bd9Sstevel@tonic-gate 			return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 		case _FIOGDIO:
16737c478bd9Sstevel@tonic-gate 			/*
16747c478bd9Sstevel@tonic-gate 			 * get delayed-io
16757c478bd9Sstevel@tonic-gate 			 */
16767c478bd9Sstevel@tonic-gate 			return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		case _FIOIO:
16797c478bd9Sstevel@tonic-gate 			/*
16807c478bd9Sstevel@tonic-gate 			 * inode open
16817c478bd9Sstevel@tonic-gate 			 */
16827c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
168380d34432Sfrankho 			    ULOCKFS_VGET_MASK);
16847c478bd9Sstevel@tonic-gate 			if (error)
16857c478bd9Sstevel@tonic-gate 				return (error);
16867c478bd9Sstevel@tonic-gate 
16877c478bd9Sstevel@tonic-gate 			error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 			if (ulp) {
16907c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
16917c478bd9Sstevel@tonic-gate 			}
16927c478bd9Sstevel@tonic-gate 			return (error);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 		case _FIOFFS:
16957c478bd9Sstevel@tonic-gate 			/*
16967c478bd9Sstevel@tonic-gate 			 * file system flush (push w/invalidate)
16977c478bd9Sstevel@tonic-gate 			 */
16987c478bd9Sstevel@tonic-gate 			if ((caddr_t)arg != NULL)
16997c478bd9Sstevel@tonic-gate 				return (EINVAL);
17007c478bd9Sstevel@tonic-gate 			return (ufs_fioffs(vp, NULL, cr));
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 		case _FIOISBUSY:
17037c478bd9Sstevel@tonic-gate 			/*
17047c478bd9Sstevel@tonic-gate 			 * Contract-private interface for Legato
17057c478bd9Sstevel@tonic-gate 			 * Purge this vnode from the DNLC and decide
17067c478bd9Sstevel@tonic-gate 			 * if this vnode is busy (*arg == 1) or not
17077c478bd9Sstevel@tonic-gate 			 * (*arg == 0)
17087c478bd9Sstevel@tonic-gate 			 */
17097c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
17107c478bd9Sstevel@tonic-gate 				return (EPERM);
17117c478bd9Sstevel@tonic-gate 			error = ufs_fioisbusy(vp, (int *)arg, cr);
17127c478bd9Sstevel@tonic-gate 			return (error);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 		case _FIODIRECTIO:
17157c478bd9Sstevel@tonic-gate 			return (ufs_fiodirectio(vp, (int)arg, cr));
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 		case _FIOTUNE:
17187c478bd9Sstevel@tonic-gate 			/*
17197c478bd9Sstevel@tonic-gate 			 * Tune the file system (aka setting fs attributes)
17207c478bd9Sstevel@tonic-gate 			 */
17217c478bd9Sstevel@tonic-gate 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
172280d34432Sfrankho 			    ULOCKFS_SETATTR_MASK);
17237c478bd9Sstevel@tonic-gate 			if (error)
17247c478bd9Sstevel@tonic-gate 				return (error);
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 			error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			if (ulp)
17297c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
17307c478bd9Sstevel@tonic-gate 			return (error);
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 		case _FIOLOGENABLE:
17337c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
17347c478bd9Sstevel@tonic-gate 				return (EPERM);
17357c478bd9Sstevel@tonic-gate 			return (ufs_fiologenable(vp, (void *)arg, cr, flag));
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 		case _FIOLOGDISABLE:
17387c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
17397c478bd9Sstevel@tonic-gate 				return (EPERM);
17407c478bd9Sstevel@tonic-gate 			return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 		case _FIOISLOG:
17437c478bd9Sstevel@tonic-gate 			return (ufs_fioislog(vp, (void *)arg, cr, flag));
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 		case _FIOSNAPSHOTCREATE_MULTI:
17467c478bd9Sstevel@tonic-gate 		{
17477c478bd9Sstevel@tonic-gate 			struct fiosnapcreate_multi	fc, *fcp;
17487c478bd9Sstevel@tonic-gate 			size_t	fcm_size;
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
17517c478bd9Sstevel@tonic-gate 				return (EFAULT);
17527c478bd9Sstevel@tonic-gate 			if (fc.backfilecount > MAX_BACKFILE_COUNT)
17537c478bd9Sstevel@tonic-gate 				return (EINVAL);
17547c478bd9Sstevel@tonic-gate 			fcm_size = sizeof (struct fiosnapcreate_multi) +
17557c478bd9Sstevel@tonic-gate 			    (fc.backfilecount - 1) * sizeof (int);
17567c478bd9Sstevel@tonic-gate 			fcp = (struct fiosnapcreate_multi *)
17577c478bd9Sstevel@tonic-gate 			    kmem_alloc(fcm_size, KM_SLEEP);
17587c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, fcp, fcm_size)) {
17597c478bd9Sstevel@tonic-gate 				kmem_free(fcp, fcm_size);
17607c478bd9Sstevel@tonic-gate 				return (EFAULT);
17617c478bd9Sstevel@tonic-gate 			}
17627c478bd9Sstevel@tonic-gate 			error = ufs_snap_create(vp, fcp, cr);
176395b7dc1dSmishra 			/*
176495b7dc1dSmishra 			 * Do copyout even if there is an error because
176595b7dc1dSmishra 			 * the details of error is stored in fcp.
176695b7dc1dSmishra 			 */
176795b7dc1dSmishra 			if (copyout(fcp, (void *)arg, fcm_size))
17687c478bd9Sstevel@tonic-gate 				error = EFAULT;
17697c478bd9Sstevel@tonic-gate 			kmem_free(fcp, fcm_size);
17707c478bd9Sstevel@tonic-gate 			return (error);
17717c478bd9Sstevel@tonic-gate 		}
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 		case _FIOSNAPSHOTDELETE:
17747c478bd9Sstevel@tonic-gate 		{
17757c478bd9Sstevel@tonic-gate 			struct fiosnapdelete	fc;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			if (copyin((void *)arg, &fc, sizeof (fc)))
17787c478bd9Sstevel@tonic-gate 				return (EFAULT);
17797c478bd9Sstevel@tonic-gate 			error = ufs_snap_delete(vp, &fc, cr);
17807c478bd9Sstevel@tonic-gate 			if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
17817c478bd9Sstevel@tonic-gate 				error = EFAULT;
17827c478bd9Sstevel@tonic-gate 			return (error);
17837c478bd9Sstevel@tonic-gate 		}
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		case _FIOGETSUPERBLOCK:
17867c478bd9Sstevel@tonic-gate 			if (copyout(fs, (void *)arg, SBSIZE))
17877c478bd9Sstevel@tonic-gate 				return (EFAULT);
17887c478bd9Sstevel@tonic-gate 			return (0);
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 		case _FIOGETMAXPHYS:
17917c478bd9Sstevel@tonic-gate 			if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
17927c478bd9Sstevel@tonic-gate 				return (EFAULT);
17937c478bd9Sstevel@tonic-gate 			return (0);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 		/*
17967c478bd9Sstevel@tonic-gate 		 * The following 3 ioctls are for TSufs support
17977c478bd9Sstevel@tonic-gate 		 * although could potentially be used elsewhere
17987c478bd9Sstevel@tonic-gate 		 */
17997c478bd9Sstevel@tonic-gate 		case _FIO_SET_LUFS_DEBUG:
18007c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
18017c478bd9Sstevel@tonic-gate 				return (EPERM);
18027c478bd9Sstevel@tonic-gate 			lufs_debug = (uint32_t)arg;
18037c478bd9Sstevel@tonic-gate 			return (0);
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 		case _FIO_SET_LUFS_ERROR:
18067c478bd9Sstevel@tonic-gate 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
18077c478bd9Sstevel@tonic-gate 				return (EPERM);
18087c478bd9Sstevel@tonic-gate 			TRANS_SETERROR(ufsvfsp);
18097c478bd9Sstevel@tonic-gate 			return (0);
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 		case _FIO_GET_TOP_STATS:
18127c478bd9Sstevel@tonic-gate 		{
18137c478bd9Sstevel@tonic-gate 			fio_lufs_stats_t *ls;
18147c478bd9Sstevel@tonic-gate 			ml_unit_t *ul = ufsvfsp->vfs_log;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 			ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
18177c478bd9Sstevel@tonic-gate 			ls->ls_debug = ul->un_debug; /* return debug value */
18187c478bd9Sstevel@tonic-gate 			/* Copy stucture if statistics are being kept */
18197c478bd9Sstevel@tonic-gate 			if (ul->un_logmap->mtm_tops) {
18207c478bd9Sstevel@tonic-gate 				ls->ls_topstats = *(ul->un_logmap->mtm_tops);
18217c478bd9Sstevel@tonic-gate 			}
18227c478bd9Sstevel@tonic-gate 			error = 0;
18237c478bd9Sstevel@tonic-gate 			if (copyout(ls, (void *)arg, sizeof (*ls)))
18247c478bd9Sstevel@tonic-gate 				error = EFAULT;
18257c478bd9Sstevel@tonic-gate 			kmem_free(ls, sizeof (*ls));
18267c478bd9Sstevel@tonic-gate 			return (error);
18277c478bd9Sstevel@tonic-gate 		}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 		case _FIO_SEEK_DATA:
18307c478bd9Sstevel@tonic-gate 		case _FIO_SEEK_HOLE:
18317c478bd9Sstevel@tonic-gate 			if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
18327c478bd9Sstevel@tonic-gate 				return (EFAULT);
18337c478bd9Sstevel@tonic-gate 			/* offset paramater is in/out */
18347c478bd9Sstevel@tonic-gate 			error = ufs_fio_holey(vp, cmd, &off);
18357c478bd9Sstevel@tonic-gate 			if (error)
18367c478bd9Sstevel@tonic-gate 				return (error);
18377c478bd9Sstevel@tonic-gate 			if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
18387c478bd9Sstevel@tonic-gate 				return (EFAULT);
18397c478bd9Sstevel@tonic-gate 			return (0);
18407c478bd9Sstevel@tonic-gate 
1841986fd29aSsetje 		case _FIO_COMPRESSED:
1842986fd29aSsetje 		{
1843986fd29aSsetje 			/*
1844986fd29aSsetje 			 * This is a project private ufs ioctl() to mark
1845986fd29aSsetje 			 * the inode as that belonging to a compressed
1846986fd29aSsetje 			 * file. This is used to mark individual
1847342440ecSPrasad Singamsetty 			 * compressed files in a miniroot archive.
1848986fd29aSsetje 			 * The files compressed in this manner are
1849986fd29aSsetje 			 * automatically decompressed by the dcfs filesystem
1850986fd29aSsetje 			 * (via an interception in ufs_lookup - see decompvp())
1851986fd29aSsetje 			 * which is layered on top of ufs on a system running
1852342440ecSPrasad Singamsetty 			 * from the archive. See uts/common/fs/dcfs for details.
1853986fd29aSsetje 			 * This ioctl only marks the file as compressed - the
1854986fd29aSsetje 			 * actual compression is done by fiocompress (a
1855986fd29aSsetje 			 * userland utility) which invokes this ioctl().
1856986fd29aSsetje 			 */
1857986fd29aSsetje 			struct inode *ip = VTOI(vp);
1858986fd29aSsetje 
1859986fd29aSsetje 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1860986fd29aSsetje 			    ULOCKFS_SETATTR_MASK);
1861986fd29aSsetje 			if (error)
1862986fd29aSsetje 				return (error);
1863986fd29aSsetje 
1864986fd29aSsetje 			if (ulp) {
1865986fd29aSsetje 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_IUPDAT,
1866986fd29aSsetje 				    TOP_IUPDAT_SIZE(ip));
1867986fd29aSsetje 			}
1868986fd29aSsetje 
1869986fd29aSsetje 			error = ufs_mark_compressed(vp);
1870986fd29aSsetje 
1871986fd29aSsetje 			if (ulp) {
1872986fd29aSsetje 				TRANS_END_ASYNC(ufsvfsp, TOP_IUPDAT,
1873986fd29aSsetje 				    TOP_IUPDAT_SIZE(ip));
1874986fd29aSsetje 				ufs_lockfs_end(ulp);
1875986fd29aSsetje 			}
1876986fd29aSsetje 
1877986fd29aSsetje 			return (error);
1878986fd29aSsetje 
1879986fd29aSsetje 		}
1880986fd29aSsetje 
18817c478bd9Sstevel@tonic-gate 		default:
18827c478bd9Sstevel@tonic-gate 			return (ENOTTY);
18837c478bd9Sstevel@tonic-gate 	}
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
1886986fd29aSsetje 
18877c478bd9Sstevel@tonic-gate /* ARGSUSED */
18887c478bd9Sstevel@tonic-gate static int
ufs_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)18897c478bd9Sstevel@tonic-gate ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
189080d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
18937c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
18947c478bd9Sstevel@tonic-gate 	int err;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	if (vap->va_mask == AT_SIZE) {
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * for performance, if only the size is requested don't bother
18997c478bd9Sstevel@tonic-gate 		 * with anything else.
19007c478bd9Sstevel@tonic-gate 		 */
19017c478bd9Sstevel@tonic-gate 		UFS_GET_ISIZE(&vap->va_size, ip);
19027c478bd9Sstevel@tonic-gate 		return (0);
19037c478bd9Sstevel@tonic-gate 	}
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/*
19067c478bd9Sstevel@tonic-gate 	 * inlined lockfs checks
19077c478bd9Sstevel@tonic-gate 	 */
19087c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
19097c478bd9Sstevel@tonic-gate 	if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
19107c478bd9Sstevel@tonic-gate 		err = EIO;
19117c478bd9Sstevel@tonic-gate 		goto out;
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
19157c478bd9Sstevel@tonic-gate 	/*
19167c478bd9Sstevel@tonic-gate 	 * Return all the attributes.  This should be refined so
19177c478bd9Sstevel@tonic-gate 	 * that it only returns what's asked for.
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	/*
19217c478bd9Sstevel@tonic-gate 	 * Copy from inode table.
19227c478bd9Sstevel@tonic-gate 	 */
19237c478bd9Sstevel@tonic-gate 	vap->va_type = vp->v_type;
19247c478bd9Sstevel@tonic-gate 	vap->va_mode = ip->i_mode & MODEMASK;
19257c478bd9Sstevel@tonic-gate 	/*
19267c478bd9Sstevel@tonic-gate 	 * If there is an ACL and there is a mask entry, then do the
19277c478bd9Sstevel@tonic-gate 	 * extra work that completes the equivalent of an acltomode(3)
19287c478bd9Sstevel@tonic-gate 	 * call.  According to POSIX P1003.1e, the acl mask should be
19297c478bd9Sstevel@tonic-gate 	 * returned in the group permissions field.
19307c478bd9Sstevel@tonic-gate 	 *
19317c478bd9Sstevel@tonic-gate 	 * - start with the original permission and mode bits (from above)
19327c478bd9Sstevel@tonic-gate 	 * - clear the group owner bits
19337c478bd9Sstevel@tonic-gate 	 * - add in the mask bits.
19347c478bd9Sstevel@tonic-gate 	 */
19357c478bd9Sstevel@tonic-gate 	if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
19367c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
19377c478bd9Sstevel@tonic-gate 		vap->va_mode |=
19387c478bd9Sstevel@tonic-gate 		    (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
19397c478bd9Sstevel@tonic-gate 	}
19407c478bd9Sstevel@tonic-gate 	vap->va_uid = ip->i_uid;
19417c478bd9Sstevel@tonic-gate 	vap->va_gid = ip->i_gid;
19427c478bd9Sstevel@tonic-gate 	vap->va_fsid = ip->i_dev;
19437c478bd9Sstevel@tonic-gate 	vap->va_nodeid = (ino64_t)ip->i_number;
19447c478bd9Sstevel@tonic-gate 	vap->va_nlink = ip->i_nlink;
19457c478bd9Sstevel@tonic-gate 	vap->va_size = ip->i_size;
19467c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR || vp->v_type == VBLK)
19477c478bd9Sstevel@tonic-gate 		vap->va_rdev = ip->i_rdev;
19487c478bd9Sstevel@tonic-gate 	else
19497c478bd9Sstevel@tonic-gate 		vap->va_rdev = 0;	/* not a b/c spec. */
19507c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
19517c478bd9Sstevel@tonic-gate 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
19527c478bd9Sstevel@tonic-gate 	vap->va_seq = ip->i_seq;
19537c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
19547c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
19557c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
19567c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
19577c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
19587c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
19597c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	switch (ip->i_mode & IFMT) {
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	case IFBLK:
19647c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;		/* was BLKDEV_IOSIZE */
19657c478bd9Sstevel@tonic-gate 		break;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 	case IFCHR:
19687c478bd9Sstevel@tonic-gate 		vap->va_blksize = MAXBSIZE;
19697c478bd9Sstevel@tonic-gate 		break;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	default:
19727c478bd9Sstevel@tonic-gate 		vap->va_blksize = ip->i_fs->fs_bsize;
19737c478bd9Sstevel@tonic-gate 		break;
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 	vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
19767c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
19777c478bd9Sstevel@tonic-gate 	err = 0;
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate out:
19807c478bd9Sstevel@tonic-gate 	return (err);
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate 
198360c8e821SFrank Batschulat /*
198460c8e821SFrank Batschulat  * Special wrapper to provide a callback for secpolicy_vnode_setattr().
198560c8e821SFrank Batschulat  * The i_contents lock is already held by the caller and we need to
198660c8e821SFrank Batschulat  * declare the inode as 'void *' argument.
198760c8e821SFrank Batschulat  */
198860c8e821SFrank Batschulat static int
ufs_priv_access(void * vip,int mode,struct cred * cr)198960c8e821SFrank Batschulat ufs_priv_access(void *vip, int mode, struct cred *cr)
199060c8e821SFrank Batschulat {
199160c8e821SFrank Batschulat 	struct inode *ip = vip;
199260c8e821SFrank Batschulat 
199360c8e821SFrank Batschulat 	return (ufs_iaccess(ip, mode, cr, 0));
199460c8e821SFrank Batschulat }
199560c8e821SFrank Batschulat 
19967c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
19977c478bd9Sstevel@tonic-gate static int
ufs_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)199880d5689fSPatrick Mooney ufs_setattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr,
199980d5689fSPatrick Mooney     caller_context_t *ct)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
20027c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
20037c478bd9Sstevel@tonic-gate 	struct fs *fs;
20047c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
20057c478bd9Sstevel@tonic-gate 	char *errmsg1;
20067c478bd9Sstevel@tonic-gate 	char *errmsg2;
20077c478bd9Sstevel@tonic-gate 	long blocks;
20087c478bd9Sstevel@tonic-gate 	long int mask = vap->va_mask;
20097c478bd9Sstevel@tonic-gate 	size_t len1, len2;
20107c478bd9Sstevel@tonic-gate 	int issync;
20117c478bd9Sstevel@tonic-gate 	int trans_size;
20127c478bd9Sstevel@tonic-gate 	int dotrans;
20137c478bd9Sstevel@tonic-gate 	int dorwlock;
20147c478bd9Sstevel@tonic-gate 	int error;
20157c478bd9Sstevel@tonic-gate 	int owner_change;
20167c478bd9Sstevel@tonic-gate 	int dodqlock;
20177c478bd9Sstevel@tonic-gate 	timestruc_t now;
20187c478bd9Sstevel@tonic-gate 	vattr_t oldva;
20197c478bd9Sstevel@tonic-gate 	int retry = 1;
202002ff05a9Svsakar 	int indeadlock;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * Cannot set these attributes.
20247c478bd9Sstevel@tonic-gate 	 */
2025da6c28aaSamw 	if ((mask & AT_NOSET) || (mask & AT_XVATTR))
2026da6c28aaSamw 		return (EINVAL);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 	/*
20297c478bd9Sstevel@tonic-gate 	 * check for forced unmount
20307c478bd9Sstevel@tonic-gate 	 */
20317c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
20327c478bd9Sstevel@tonic-gate 		return (EIO);
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
20357c478bd9Sstevel@tonic-gate 	if (fs->fs_ronly != 0)
20367c478bd9Sstevel@tonic-gate 		return (EROFS);
20377c478bd9Sstevel@tonic-gate 
20387c478bd9Sstevel@tonic-gate again:
20397c478bd9Sstevel@tonic-gate 	errmsg1 = NULL;
20407c478bd9Sstevel@tonic-gate 	errmsg2 = NULL;
20417c478bd9Sstevel@tonic-gate 	dotrans = 0;
20427c478bd9Sstevel@tonic-gate 	dorwlock = 0;
20437c478bd9Sstevel@tonic-gate 	dodqlock = 0;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
20467c478bd9Sstevel@tonic-gate 	if (error)
20477c478bd9Sstevel@tonic-gate 		goto out;
20487c478bd9Sstevel@tonic-gate 
20497c478bd9Sstevel@tonic-gate 	/*
20507c478bd9Sstevel@tonic-gate 	 * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
20517c478bd9Sstevel@tonic-gate 	 * This follows the protocol for read()/write().
20527c478bd9Sstevel@tonic-gate 	 */
20537c478bd9Sstevel@tonic-gate 	if (vp->v_type != VDIR) {
205402ff05a9Svsakar 		/*
205502ff05a9Svsakar 		 * ufs_tryirwlock uses rw_tryenter and checks for SLOCK to
205602ff05a9Svsakar 		 * avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
205702ff05a9Svsakar 		 * possible, retries the operation.
205802ff05a9Svsakar 		 */
205902ff05a9Svsakar 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_file);
206002ff05a9Svsakar 		if (indeadlock) {
206102ff05a9Svsakar 			if (ulp)
206202ff05a9Svsakar 				ufs_lockfs_end(ulp);
206302ff05a9Svsakar 			goto again;
206402ff05a9Svsakar 		}
20657c478bd9Sstevel@tonic-gate 		dorwlock = 1;
20667c478bd9Sstevel@tonic-gate 	}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * Truncate file.  Must have write permission and not be a directory.
20707c478bd9Sstevel@tonic-gate 	 */
20717c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
20727c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
20737c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
20747c478bd9Sstevel@tonic-gate 			error = EISDIR;
20757c478bd9Sstevel@tonic-gate 			goto update_inode;
20767c478bd9Sstevel@tonic-gate 		}
207760c8e821SFrank Batschulat 		if (error = ufs_iaccess(ip, IWRITE, cr, 0))
20787c478bd9Sstevel@tonic-gate 			goto update_inode;
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
20817c478bd9Sstevel@tonic-gate 		error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
20827c478bd9Sstevel@tonic-gate 		if (error) {
20837c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
20847c478bd9Sstevel@tonic-gate 			goto update_inode;
20857c478bd9Sstevel@tonic-gate 		}
208672102e74SBryan Cantrill 
208772102e74SBryan Cantrill 		if (error == 0 && vap->va_size)
208872102e74SBryan Cantrill 			vnevent_truncate(vp, ct);
20897c478bd9Sstevel@tonic-gate 	}
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	if (ulp) {
20927c478bd9Sstevel@tonic-gate 		trans_size = (int)TOP_SETATTR_SIZE(ip);
20937c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
20947c478bd9Sstevel@tonic-gate 		++dotrans;
20957c478bd9Sstevel@tonic-gate 	}
20967c478bd9Sstevel@tonic-gate 
20977c478bd9Sstevel@tonic-gate 	/*
20987c478bd9Sstevel@tonic-gate 	 * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
20997c478bd9Sstevel@tonic-gate 	 * This follows the protocol established by
21007c478bd9Sstevel@tonic-gate 	 * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
21017c478bd9Sstevel@tonic-gate 	 */
21027c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
210302ff05a9Svsakar 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_SETATTR,
210480d34432Sfrankho 		    retry_dir);
210502ff05a9Svsakar 		if (indeadlock)
210602ff05a9Svsakar 			goto again;
21077c478bd9Sstevel@tonic-gate 		dorwlock = 1;
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 
21107c478bd9Sstevel@tonic-gate 	/*
21117c478bd9Sstevel@tonic-gate 	 * Grab quota lock if we are changing the file's owner.
21127c478bd9Sstevel@tonic-gate 	 */
21137c478bd9Sstevel@tonic-gate 	if (mask & AT_UID) {
21147c478bd9Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
21157c478bd9Sstevel@tonic-gate 		dodqlock = 1;
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate 	oldva.va_mode = ip->i_mode;
21207c478bd9Sstevel@tonic-gate 	oldva.va_uid = ip->i_uid;
21217c478bd9Sstevel@tonic-gate 	oldva.va_gid = ip->i_gid;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	vap->va_mask &= ~AT_SIZE;
212460c8e821SFrank Batschulat 
21257c478bd9Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
212660c8e821SFrank Batschulat 	    ufs_priv_access, ip);
21277c478bd9Sstevel@tonic-gate 	if (error)
21287c478bd9Sstevel@tonic-gate 		goto update_inode;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	/*
21337c478bd9Sstevel@tonic-gate 	 * Change file access modes.
21347c478bd9Sstevel@tonic-gate 	 */
21357c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
21367c478bd9Sstevel@tonic-gate 		ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
21377c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
21387c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
21397c478bd9Sstevel@tonic-gate 		if (stickyhack) {
21407c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
21417c478bd9Sstevel@tonic-gate 			if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
21427c478bd9Sstevel@tonic-gate 				vp->v_flag |= VSWAPLIKE;
21437c478bd9Sstevel@tonic-gate 			else
21447c478bd9Sstevel@tonic-gate 				vp->v_flag &= ~VSWAPLIKE;
21457c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 	}
21487c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
21497c478bd9Sstevel@tonic-gate 		if (mask & AT_UID) {
21507c478bd9Sstevel@tonic-gate 			/*
21517c478bd9Sstevel@tonic-gate 			 * Don't change ownership of the quota inode.
21527c478bd9Sstevel@tonic-gate 			 */
21537c478bd9Sstevel@tonic-gate 			if (ufsvfsp->vfs_qinod == ip) {
21547c478bd9Sstevel@tonic-gate 				ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
21557c478bd9Sstevel@tonic-gate 				error = EINVAL;
21567c478bd9Sstevel@tonic-gate 				goto update_inode;
21577c478bd9Sstevel@tonic-gate 			}
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate 			/*
21607c478bd9Sstevel@tonic-gate 			 * No real ownership change.
21617c478bd9Sstevel@tonic-gate 			 */
21627c478bd9Sstevel@tonic-gate 			if (ip->i_uid == vap->va_uid) {
21637c478bd9Sstevel@tonic-gate 				blocks = 0;
21647c478bd9Sstevel@tonic-gate 				owner_change = 0;
21657c478bd9Sstevel@tonic-gate 			}
21667c478bd9Sstevel@tonic-gate 			/*
21677c478bd9Sstevel@tonic-gate 			 * Remove the blocks and the file, from the old user's
21687c478bd9Sstevel@tonic-gate 			 * quota.
21697c478bd9Sstevel@tonic-gate 			 */
21707c478bd9Sstevel@tonic-gate 			else {
21717c478bd9Sstevel@tonic-gate 				blocks = ip->i_blocks;
21727c478bd9Sstevel@tonic-gate 				owner_change = 1;
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 				(void) chkdq(ip, -blocks, /* force */ 1, cr,
217580d34432Sfrankho 				    (char **)NULL, (size_t *)NULL);
21767c478bd9Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ -1, ip,
217780d34432Sfrankho 				    (uid_t)ip->i_uid, /* force */ 1, cr,
217880d34432Sfrankho 				    (char **)NULL, (size_t *)NULL);
21797c478bd9Sstevel@tonic-gate 				dqrele(ip->i_dquot);
21807c478bd9Sstevel@tonic-gate 			}
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 			ip->i_uid = vap->va_uid;
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 			/*
21857c478bd9Sstevel@tonic-gate 			 * There is a real ownership change.
21867c478bd9Sstevel@tonic-gate 			 */
21877c478bd9Sstevel@tonic-gate 			if (owner_change) {
21887c478bd9Sstevel@tonic-gate 				/*
21897c478bd9Sstevel@tonic-gate 				 * Add the blocks and the file to the new
21907c478bd9Sstevel@tonic-gate 				 * user's quota.
21917c478bd9Sstevel@tonic-gate 				 */
21927c478bd9Sstevel@tonic-gate 				ip->i_dquot = getinoquota(ip);
21937c478bd9Sstevel@tonic-gate 				(void) chkdq(ip, blocks, /* force */ 1, cr,
219480d34432Sfrankho 				    &errmsg1, &len1);
21957c478bd9Sstevel@tonic-gate 				(void) chkiq(ufsvfsp, /* change */ 1,
219680d34432Sfrankho 				    (struct inode *)NULL, (uid_t)ip->i_uid,
219780d34432Sfrankho 				    /* force */ 1, cr, &errmsg2, &len2);
21987c478bd9Sstevel@tonic-gate 			}
21997c478bd9Sstevel@tonic-gate 		}
22007c478bd9Sstevel@tonic-gate 		if (mask & AT_GID) {
22017c478bd9Sstevel@tonic-gate 			ip->i_gid = vap->va_gid;
22027c478bd9Sstevel@tonic-gate 		}
22037c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
22047c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * Change file access or modified times.
22087c478bd9Sstevel@tonic-gate 	 */
22097c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
22107c478bd9Sstevel@tonic-gate 		/* Check that the time value is within ufs range */
22117c478bd9Sstevel@tonic-gate 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
22127c478bd9Sstevel@tonic-gate 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
22137c478bd9Sstevel@tonic-gate 			error = EOVERFLOW;
22147c478bd9Sstevel@tonic-gate 			goto update_inode;
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		/*
22187c478bd9Sstevel@tonic-gate 		 * if the "noaccess" mount option is set and only atime
22197c478bd9Sstevel@tonic-gate 		 * update is requested, do nothing. No error is returned.
22207c478bd9Sstevel@tonic-gate 		 */
22217c478bd9Sstevel@tonic-gate 		if ((ufsvfsp->vfs_noatime) &&
22227c478bd9Sstevel@tonic-gate 		    ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
22237c478bd9Sstevel@tonic-gate 			goto skip_atime;
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate 		if (mask & AT_ATIME) {
22267c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
22277c478bd9Sstevel@tonic-gate 			ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
22287c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~IACC;
22297c478bd9Sstevel@tonic-gate 		}
22307c478bd9Sstevel@tonic-gate 		if (mask & AT_MTIME) {
22317c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
22327c478bd9Sstevel@tonic-gate 			ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
22337c478bd9Sstevel@tonic-gate 			gethrestime(&now);
22347c478bd9Sstevel@tonic-gate 			if (now.tv_sec > TIME32_MAX) {
22357c478bd9Sstevel@tonic-gate 				/*
22367c478bd9Sstevel@tonic-gate 				 * In 2038, ctime sticks forever..
22377c478bd9Sstevel@tonic-gate 				 */
22387c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_sec = TIME32_MAX;
22397c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_usec = 0;
22407c478bd9Sstevel@tonic-gate 			} else {
22417c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_sec = now.tv_sec;
22427c478bd9Sstevel@tonic-gate 				ip->i_ctime.tv_usec = now.tv_nsec / 1000;
22437c478bd9Sstevel@tonic-gate 			}
22447c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~(IUPD|ICHG);
22457c478bd9Sstevel@tonic-gate 			ip->i_flag |= IMODTIME;
22467c478bd9Sstevel@tonic-gate 		}
22477c478bd9Sstevel@tonic-gate 		TRANS_INODE(ufsvfsp, ip);
22487c478bd9Sstevel@tonic-gate 		ip->i_flag |= IMOD;
22497c478bd9Sstevel@tonic-gate 	}
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate skip_atime:
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * The presence of a shadow inode may indicate an ACL, but does
22547c478bd9Sstevel@tonic-gate 	 * not imply an ACL.  Future FSD types should be handled here too
22557c478bd9Sstevel@tonic-gate 	 * and check for the presence of the attribute-specific data
22567c478bd9Sstevel@tonic-gate 	 * before referencing it.
22577c478bd9Sstevel@tonic-gate 	 */
22587c478bd9Sstevel@tonic-gate 	if (ip->i_shadow) {
22597c478bd9Sstevel@tonic-gate 		/*
22607c478bd9Sstevel@tonic-gate 		 * XXX if ufs_iupdat is changed to sandbagged write fix
22617c478bd9Sstevel@tonic-gate 		 * ufs_acl_setattr to push ip to keep acls consistent
22627c478bd9Sstevel@tonic-gate 		 *
22637c478bd9Sstevel@tonic-gate 		 * Suppress out of inodes messages if we will retry.
22647c478bd9Sstevel@tonic-gate 		 */
22657c478bd9Sstevel@tonic-gate 		if (retry)
22667c478bd9Sstevel@tonic-gate 			ip->i_flag |= IQUIET;
22677c478bd9Sstevel@tonic-gate 		error = ufs_acl_setattr(ip, vap, cr);
22687c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~IQUIET;
22697c478bd9Sstevel@tonic-gate 	}
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate update_inode:
22727c478bd9Sstevel@tonic-gate 	/*
22737c478bd9Sstevel@tonic-gate 	 * Setattr always increases the sequence number
22747c478bd9Sstevel@tonic-gate 	 */
22757c478bd9Sstevel@tonic-gate 	ip->i_seq++;
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	/*
22787c478bd9Sstevel@tonic-gate 	 * if nfsd and not logging; push synchronously
22797c478bd9Sstevel@tonic-gate 	 */
22807c478bd9Sstevel@tonic-gate 	if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
22817c478bd9Sstevel@tonic-gate 		ufs_iupdat(ip, 1);
22827c478bd9Sstevel@tonic-gate 	} else {
22837c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
22877c478bd9Sstevel@tonic-gate 	if (dodqlock) {
22887c478bd9Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
22897c478bd9Sstevel@tonic-gate 	}
22907c478bd9Sstevel@tonic-gate 	if (dorwlock)
22917c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	if (ulp) {
22947c478bd9Sstevel@tonic-gate 		if (dotrans) {
22957c478bd9Sstevel@tonic-gate 			int terr = 0;
22967c478bd9Sstevel@tonic-gate 			TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
22977c478bd9Sstevel@tonic-gate 			    trans_size);
22987c478bd9Sstevel@tonic-gate 			if (error == 0)
22997c478bd9Sstevel@tonic-gate 				error = terr;
23007c478bd9Sstevel@tonic-gate 		}
23017c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate out:
23047c478bd9Sstevel@tonic-gate 	/*
23057c478bd9Sstevel@tonic-gate 	 * If out of inodes or blocks, see if we can free something
23067c478bd9Sstevel@tonic-gate 	 * up from the delete queue.
23077c478bd9Sstevel@tonic-gate 	 */
23087c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
23097c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
23107c478bd9Sstevel@tonic-gate 		retry = 0;
23117c478bd9Sstevel@tonic-gate 		if (errmsg1 != NULL)
23127c478bd9Sstevel@tonic-gate 			kmem_free(errmsg1, len1);
23137c478bd9Sstevel@tonic-gate 		if (errmsg2 != NULL)
23147c478bd9Sstevel@tonic-gate 			kmem_free(errmsg2, len2);
23157c478bd9Sstevel@tonic-gate 		goto again;
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate 	if (errmsg1 != NULL) {
23187c478bd9Sstevel@tonic-gate 		uprintf(errmsg1);
23197c478bd9Sstevel@tonic-gate 		kmem_free(errmsg1, len1);
23207c478bd9Sstevel@tonic-gate 	}
23217c478bd9Sstevel@tonic-gate 	if (errmsg2 != NULL) {
23227c478bd9Sstevel@tonic-gate 		uprintf(errmsg2);
23237c478bd9Sstevel@tonic-gate 		kmem_free(errmsg2, len2);
23247c478bd9Sstevel@tonic-gate 	}
23257c478bd9Sstevel@tonic-gate 	return (error);
23267c478bd9Sstevel@tonic-gate }
23277c478bd9Sstevel@tonic-gate 
23287c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23297c478bd9Sstevel@tonic-gate static int
ufs_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)2330da6c28aaSamw ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr,
233180d5689fSPatrick Mooney     caller_context_t *ct)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
23347c478bd9Sstevel@tonic-gate 
23357c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
23367c478bd9Sstevel@tonic-gate 		return (EIO);
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate 	/*
23397c478bd9Sstevel@tonic-gate 	 * The ufs_iaccess function wants to be called with
23407c478bd9Sstevel@tonic-gate 	 * mode bits expressed as "ufs specific" bits.
23417c478bd9Sstevel@tonic-gate 	 * I.e., VWRITE|VREAD|VEXEC do not make sense to
23427c478bd9Sstevel@tonic-gate 	 * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
23437c478bd9Sstevel@tonic-gate 	 * But since they're the same we just pass the vnode mode
23447c478bd9Sstevel@tonic-gate 	 * bit but just verify that assumption at compile time.
23457c478bd9Sstevel@tonic-gate 	 */
23467c478bd9Sstevel@tonic-gate #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
23477c478bd9Sstevel@tonic-gate #error "ufs_access needs to map Vmodes to Imodes"
23487c478bd9Sstevel@tonic-gate #endif
234960c8e821SFrank Batschulat 	return (ufs_iaccess(ip, mode, cr, 1));
23507c478bd9Sstevel@tonic-gate }
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate /* ARGSUSED */
23537c478bd9Sstevel@tonic-gate static int
ufs_readlink(struct vnode * vp,struct uio * uiop,struct cred * cr,caller_context_t * ct)2354da6c28aaSamw ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr,
235580d5689fSPatrick Mooney     caller_context_t *ct)
23567c478bd9Sstevel@tonic-gate {
23577c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
23587c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
23597c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
23607c478bd9Sstevel@tonic-gate 	int error;
23617c478bd9Sstevel@tonic-gate 	int fastsymlink;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK) {
23647c478bd9Sstevel@tonic-gate 		error = EINVAL;
23657c478bd9Sstevel@tonic-gate 		goto nolockout;
23667c478bd9Sstevel@tonic-gate 	}
23677c478bd9Sstevel@tonic-gate 
23687c478bd9Sstevel@tonic-gate 	/*
23697c478bd9Sstevel@tonic-gate 	 * If the symbolic link is empty there is nothing to read.
23707c478bd9Sstevel@tonic-gate 	 * Fast-track these empty symbolic links
23717c478bd9Sstevel@tonic-gate 	 */
23727c478bd9Sstevel@tonic-gate 	if (ip->i_size == 0) {
23737c478bd9Sstevel@tonic-gate 		error = 0;
23747c478bd9Sstevel@tonic-gate 		goto nolockout;
23757c478bd9Sstevel@tonic-gate 	}
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
23787c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
23797c478bd9Sstevel@tonic-gate 	if (error)
23807c478bd9Sstevel@tonic-gate 		goto nolockout;
23817c478bd9Sstevel@tonic-gate 	/*
23827c478bd9Sstevel@tonic-gate 	 * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
23837c478bd9Sstevel@tonic-gate 	 */
23847c478bd9Sstevel@tonic-gate again:
23857c478bd9Sstevel@tonic-gate 	fastsymlink = 0;
23867c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IFASTSYMLNK) {
23877c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
23887c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
23897c478bd9Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
23907c478bd9Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
23917c478bd9Sstevel@tonic-gate 			    (ip->i_fs->fs_ronly == 0) &&
23927c478bd9Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
23937c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
23947c478bd9Sstevel@tonic-gate 				ip->i_flag |= IACC;
23957c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
23967c478bd9Sstevel@tonic-gate 			}
23977c478bd9Sstevel@tonic-gate 			error = uiomove((caddr_t)&ip->i_db[1],
239880d34432Sfrankho 			    MIN(ip->i_size, uiop->uio_resid),
239980d34432Sfrankho 			    UIO_READ, uiop);
24007c478bd9Sstevel@tonic-gate 			ITIMES(ip);
24017c478bd9Sstevel@tonic-gate 			++fastsymlink;
24027c478bd9Sstevel@tonic-gate 		}
24037c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
24047c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
24057c478bd9Sstevel@tonic-gate 	}
24067c478bd9Sstevel@tonic-gate 	if (!fastsymlink) {
24077c478bd9Sstevel@tonic-gate 		ssize_t size;	/* number of bytes read  */
24087c478bd9Sstevel@tonic-gate 		caddr_t basep;	/* pointer to input data */
24097c478bd9Sstevel@tonic-gate 		ino_t ino;
24107c478bd9Sstevel@tonic-gate 		long  igen;
24117c478bd9Sstevel@tonic-gate 		struct uio tuio;	/* temp uio struct */
24127c478bd9Sstevel@tonic-gate 		struct uio *tuiop;
24137c478bd9Sstevel@tonic-gate 		iovec_t tiov;		/* temp iovec struct */
24147c478bd9Sstevel@tonic-gate 		char kbuf[FSL_SIZE];	/* buffer to hold fast symlink */
24157c478bd9Sstevel@tonic-gate 		int tflag = 0;		/* flag to indicate temp vars used */
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 		ino = ip->i_number;
24187c478bd9Sstevel@tonic-gate 		igen = ip->i_gen;
24197c478bd9Sstevel@tonic-gate 		size = uiop->uio_resid;
24207c478bd9Sstevel@tonic-gate 		basep = uiop->uio_iov->iov_base;
24217c478bd9Sstevel@tonic-gate 		tuiop = uiop;
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
24247c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
24257c478bd9Sstevel@tonic-gate 		if (ip->i_flag & IFASTSYMLNK) {
24267c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
24277c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
24287c478bd9Sstevel@tonic-gate 			goto again;
24297c478bd9Sstevel@tonic-gate 		}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 		/* can this be a fast symlink and is it a user buffer? */
24327c478bd9Sstevel@tonic-gate 		if (ip->i_size <= FSL_SIZE &&
24337c478bd9Sstevel@tonic-gate 		    (uiop->uio_segflg == UIO_USERSPACE ||
24347c478bd9Sstevel@tonic-gate 		    uiop->uio_segflg == UIO_USERISPACE)) {
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 			bzero(&tuio, sizeof (struct uio));
24377c478bd9Sstevel@tonic-gate 			/*
24387c478bd9Sstevel@tonic-gate 			 * setup a kernel buffer to read link into.  this
24397c478bd9Sstevel@tonic-gate 			 * is to fix a race condition where the user buffer
24407c478bd9Sstevel@tonic-gate 			 * got corrupted before copying it into the inode.
24417c478bd9Sstevel@tonic-gate 			 */
24427c478bd9Sstevel@tonic-gate 			size = ip->i_size;
24437c478bd9Sstevel@tonic-gate 			tiov.iov_len = size;
24447c478bd9Sstevel@tonic-gate 			tiov.iov_base = kbuf;
24457c478bd9Sstevel@tonic-gate 			tuio.uio_iov = &tiov;
24467c478bd9Sstevel@tonic-gate 			tuio.uio_iovcnt = 1;
24477c478bd9Sstevel@tonic-gate 			tuio.uio_offset = uiop->uio_offset;
24487c478bd9Sstevel@tonic-gate 			tuio.uio_segflg = UIO_SYSSPACE;
24497c478bd9Sstevel@tonic-gate 			tuio.uio_fmode = uiop->uio_fmode;
24507c478bd9Sstevel@tonic-gate 			tuio.uio_extflg = uiop->uio_extflg;
24517c478bd9Sstevel@tonic-gate 			tuio.uio_limit = uiop->uio_limit;
24527c478bd9Sstevel@tonic-gate 			tuio.uio_resid = size;
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate 			basep = tuio.uio_iov->iov_base;
24557c478bd9Sstevel@tonic-gate 			tuiop = &tuio;
24567c478bd9Sstevel@tonic-gate 			tflag = 1;
24577c478bd9Sstevel@tonic-gate 		}
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 		error = rdip(ip, tuiop, 0, cr);
24607c478bd9Sstevel@tonic-gate 		if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
24617c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
24627c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
24637c478bd9Sstevel@tonic-gate 			goto out;
24647c478bd9Sstevel@tonic-gate 		}
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 		if (tflag == 0)
24677c478bd9Sstevel@tonic-gate 			size -= uiop->uio_resid;
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate 		if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
24707c478bd9Sstevel@tonic-gate 		    ip->i_size == size) || (tflag == 1 &&
24717c478bd9Sstevel@tonic-gate 		    tuio.uio_resid == 0)) {
24727c478bd9Sstevel@tonic-gate 			error = kcopy(basep, &ip->i_db[1], ip->i_size);
24737c478bd9Sstevel@tonic-gate 			if (error == 0) {
24747c478bd9Sstevel@tonic-gate 				ip->i_flag |= IFASTSYMLNK;
24757c478bd9Sstevel@tonic-gate 				/*
24767c478bd9Sstevel@tonic-gate 				 * free page
24777c478bd9Sstevel@tonic-gate 				 */
24787c478bd9Sstevel@tonic-gate 				(void) VOP_PUTPAGE(ITOV(ip),
24797c478bd9Sstevel@tonic-gate 				    (offset_t)0, PAGESIZE,
24807c478bd9Sstevel@tonic-gate 				    (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
2481da6c28aaSamw 				    cr, ct);
24827c478bd9Sstevel@tonic-gate 			} else {
24837c478bd9Sstevel@tonic-gate 				int i;
24847c478bd9Sstevel@tonic-gate 				/* error, clear garbage left behind */
24857c478bd9Sstevel@tonic-gate 				for (i = 1; i < NDADDR; i++)
24867c478bd9Sstevel@tonic-gate 					ip->i_db[i] = 0;
24877c478bd9Sstevel@tonic-gate 				for (i = 0; i < NIADDR; i++)
24887c478bd9Sstevel@tonic-gate 					ip->i_ib[i] = 0;
24897c478bd9Sstevel@tonic-gate 			}
24907c478bd9Sstevel@tonic-gate 		}
24917c478bd9Sstevel@tonic-gate 		if (tflag == 1) {
24927c478bd9Sstevel@tonic-gate 			/* now, copy it into the user buffer */
24937c478bd9Sstevel@tonic-gate 			error = uiomove((caddr_t)kbuf,
249480d34432Sfrankho 			    MIN(size, uiop->uio_resid),
249580d34432Sfrankho 			    UIO_READ, uiop);
24967c478bd9Sstevel@tonic-gate 		}
24977c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
24987c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
24997c478bd9Sstevel@tonic-gate 	}
25007c478bd9Sstevel@tonic-gate out:
25017c478bd9Sstevel@tonic-gate 	if (ulp) {
25027c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
25037c478bd9Sstevel@tonic-gate 	}
25047c478bd9Sstevel@tonic-gate nolockout:
25057c478bd9Sstevel@tonic-gate 	return (error);
25067c478bd9Sstevel@tonic-gate }
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate /* ARGSUSED */
25097c478bd9Sstevel@tonic-gate static int
ufs_fsync(struct vnode * vp,int syncflag,struct cred * cr,caller_context_t * ct)251080d5689fSPatrick Mooney ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr, caller_context_t *ct)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
25137c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
25147c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
25157c478bd9Sstevel@tonic-gate 	int error;
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
25187c478bd9Sstevel@tonic-gate 	if (error)
25197c478bd9Sstevel@tonic-gate 		return (error);
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
25227c478bd9Sstevel@tonic-gate 		/*
25237c478bd9Sstevel@tonic-gate 		 * First push out any data pages
25247c478bd9Sstevel@tonic-gate 		 */
25257c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
25267c478bd9Sstevel@tonic-gate 		    (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
25277c478bd9Sstevel@tonic-gate 			error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
2528da6c28aaSamw 			    0, CRED(), ct);
25297c478bd9Sstevel@tonic-gate 			if (error)
25307c478bd9Sstevel@tonic-gate 				goto out;
25317c478bd9Sstevel@tonic-gate 		}
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 		/*
25347c478bd9Sstevel@tonic-gate 		 * Delta any delayed inode times updates
25357c478bd9Sstevel@tonic-gate 		 * and push inode to log.
25367c478bd9Sstevel@tonic-gate 		 * All other inode deltas will have already been delta'd
25377c478bd9Sstevel@tonic-gate 		 * and will be pushed during the commit.
25387c478bd9Sstevel@tonic-gate 		 */
25397c478bd9Sstevel@tonic-gate 		if (!(syncflag & FDSYNC) &&
25407c478bd9Sstevel@tonic-gate 		    ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
25417c478bd9Sstevel@tonic-gate 			if (ulp) {
25427c478bd9Sstevel@tonic-gate 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
25437c478bd9Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
25447c478bd9Sstevel@tonic-gate 			}
25457c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
25467c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
25477c478bd9Sstevel@tonic-gate 			ip->i_flag &= ~IMODTIME;
25487c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
25497c478bd9Sstevel@tonic-gate 			ufs_iupdat(ip, I_SYNC);
25507c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
25517c478bd9Sstevel@tonic-gate 			if (ulp) {
25527c478bd9Sstevel@tonic-gate 				TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
25537c478bd9Sstevel@tonic-gate 				    TOP_SYNCIP_SIZE);
25547c478bd9Sstevel@tonic-gate 			}
25557c478bd9Sstevel@tonic-gate 		}
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 		/*
25587c478bd9Sstevel@tonic-gate 		 * Commit the Moby transaction
25597c478bd9Sstevel@tonic-gate 		 *
25607c478bd9Sstevel@tonic-gate 		 * Deltas have already been made so we just need to
25617c478bd9Sstevel@tonic-gate 		 * commit them with a synchronous transaction.
25627c478bd9Sstevel@tonic-gate 		 * TRANS_BEGIN_SYNC() will return an error
25637c478bd9Sstevel@tonic-gate 		 * if there are no deltas to commit, for an
25647c478bd9Sstevel@tonic-gate 		 * empty transaction.
25657c478bd9Sstevel@tonic-gate 		 */
25667c478bd9Sstevel@tonic-gate 		if (ulp) {
25677c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
25687c478bd9Sstevel@tonic-gate 			    error);
25697c478bd9Sstevel@tonic-gate 			if (error) {
25707c478bd9Sstevel@tonic-gate 				error = 0; /* commit wasn't needed */
25717c478bd9Sstevel@tonic-gate 				goto out;
25727c478bd9Sstevel@tonic-gate 			}
25737c478bd9Sstevel@tonic-gate 			TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
25747c478bd9Sstevel@tonic-gate 			    TOP_COMMIT_SIZE);
25757c478bd9Sstevel@tonic-gate 		}
25767c478bd9Sstevel@tonic-gate 	} else {	/* not logging */
25777c478bd9Sstevel@tonic-gate 		if (!(IS_SWAPVP(vp)))
25787c478bd9Sstevel@tonic-gate 			if (syncflag & FNODSYNC) {
25797c478bd9Sstevel@tonic-gate 				/* Just update the inode only */
25807c478bd9Sstevel@tonic-gate 				TRANS_IUPDAT(ip, 1);
25817c478bd9Sstevel@tonic-gate 				error = 0;
25827c478bd9Sstevel@tonic-gate 			} else if (syncflag & FDSYNC)
25837c478bd9Sstevel@tonic-gate 				/* Do data-synchronous writes */
25847c478bd9Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
25857c478bd9Sstevel@tonic-gate 			else
25867c478bd9Sstevel@tonic-gate 				/* Do synchronous writes */
25877c478bd9Sstevel@tonic-gate 				error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
25907c478bd9Sstevel@tonic-gate 		if (!error)
25917c478bd9Sstevel@tonic-gate 			error = ufs_sync_indir(ip);
25927c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
25937c478bd9Sstevel@tonic-gate 	}
25947c478bd9Sstevel@tonic-gate out:
25957c478bd9Sstevel@tonic-gate 	if (ulp) {
25967c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
25977c478bd9Sstevel@tonic-gate 	}
25987c478bd9Sstevel@tonic-gate 	return (error);
25997c478bd9Sstevel@tonic-gate }
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26027c478bd9Sstevel@tonic-gate static void
ufs_inactive(struct vnode * vp,struct cred * cr,caller_context_t * ct)2603da6c28aaSamw ufs_inactive(struct vnode *vp, struct cred *cr, caller_context_t *ct)
26047c478bd9Sstevel@tonic-gate {
26057c478bd9Sstevel@tonic-gate 	ufs_iinactive(VTOI(vp));
26067c478bd9Sstevel@tonic-gate }
26077c478bd9Sstevel@tonic-gate 
26087c478bd9Sstevel@tonic-gate /*
26097c478bd9Sstevel@tonic-gate  * Unix file system operations having to do with directory manipulation.
26107c478bd9Sstevel@tonic-gate  */
26117c478bd9Sstevel@tonic-gate int ufs_lookup_idle_count = 2;	/* Number of inodes to idle each time */
26127c478bd9Sstevel@tonic-gate /* ARGSUSED */
26137c478bd9Sstevel@tonic-gate static int
ufs_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cr,caller_context_t * ct,int * direntflags,pathname_t * realpnp)26147c478bd9Sstevel@tonic-gate ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
261580d5689fSPatrick Mooney     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr,
261680d5689fSPatrick Mooney     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
26177c478bd9Sstevel@tonic-gate {
26187c478bd9Sstevel@tonic-gate 	struct inode *ip;
26197c478bd9Sstevel@tonic-gate 	struct inode *sip;
26207c478bd9Sstevel@tonic-gate 	struct inode *xip;
26217c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
26227c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
26237c478bd9Sstevel@tonic-gate 	struct vnode *vp;
26247c478bd9Sstevel@tonic-gate 	int error;
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	/*
26277c478bd9Sstevel@tonic-gate 	 * Check flags for type of lookup (regular file or attribute file)
26287c478bd9Sstevel@tonic-gate 	 */
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
26337c478bd9Sstevel@tonic-gate 
2634da6c28aaSamw 		/*
2635da6c28aaSamw 		 * If not mounted with XATTR support then return EINVAL
2636da6c28aaSamw 		 */
2637da6c28aaSamw 
2638da6c28aaSamw 		if (!(ip->i_ufsvfs->vfs_vfs->vfs_flag & VFS_XATTR))
2639da6c28aaSamw 			return (EINVAL);
26407c478bd9Sstevel@tonic-gate 		/*
26417c478bd9Sstevel@tonic-gate 		 * We don't allow recursive attributes...
26427c478bd9Sstevel@tonic-gate 		 * Maybe someday we will.
26437c478bd9Sstevel@tonic-gate 		 */
26447c478bd9Sstevel@tonic-gate 		if ((ip->i_cflags & IXATTR)) {
26457c478bd9Sstevel@tonic-gate 			return (EINVAL);
26467c478bd9Sstevel@tonic-gate 		}
26477c478bd9Sstevel@tonic-gate 
26487c478bd9Sstevel@tonic-gate 		if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
26497c478bd9Sstevel@tonic-gate 			error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
26507c478bd9Sstevel@tonic-gate 			if (error) {
26517c478bd9Sstevel@tonic-gate 				*vpp = NULL;
26527c478bd9Sstevel@tonic-gate 				goto out;
26537c478bd9Sstevel@tonic-gate 			}
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 			vp = ITOV(sip);
26567c478bd9Sstevel@tonic-gate 			dnlc_update(dvp, XATTR_DIR_NAME, vp);
26577c478bd9Sstevel@tonic-gate 		}
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 		/*
26607c478bd9Sstevel@tonic-gate 		 * Check accessibility of directory.
26617c478bd9Sstevel@tonic-gate 		 */
26627c478bd9Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
26637c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
26647c478bd9Sstevel@tonic-gate 			error = ENOENT;
26657c478bd9Sstevel@tonic-gate 			goto out;
26667c478bd9Sstevel@tonic-gate 		}
266760c8e821SFrank Batschulat 		if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr, 1)) != 0) {
26687c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
26697c478bd9Sstevel@tonic-gate 			goto out;
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 		*vpp = vp;
26737c478bd9Sstevel@tonic-gate 		return (0);
26747c478bd9Sstevel@tonic-gate 	}
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	/*
26777c478bd9Sstevel@tonic-gate 	 * Check for a null component, which we should treat as
26787c478bd9Sstevel@tonic-gate 	 * looking at dvp from within it's parent, so we don't
26797c478bd9Sstevel@tonic-gate 	 * need a call to ufs_iaccess(), as it has already been
26807c478bd9Sstevel@tonic-gate 	 * done.
26817c478bd9Sstevel@tonic-gate 	 */
26827c478bd9Sstevel@tonic-gate 	if (nm[0] == 0) {
26837c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
26847c478bd9Sstevel@tonic-gate 		error = 0;
26857c478bd9Sstevel@tonic-gate 		*vpp = dvp;
26867c478bd9Sstevel@tonic-gate 		goto out;
26877c478bd9Sstevel@tonic-gate 	}
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	/*
26907c478bd9Sstevel@tonic-gate 	 * Check for "." ie itself. this is a quick check and
26917c478bd9Sstevel@tonic-gate 	 * avoids adding "." into the dnlc (which have been seen
26927c478bd9Sstevel@tonic-gate 	 * to occupy >10% of the cache).
26937c478bd9Sstevel@tonic-gate 	 */
26947c478bd9Sstevel@tonic-gate 	if ((nm[0] == '.') && (nm[1] == 0)) {
26957c478bd9Sstevel@tonic-gate 		/*
26967c478bd9Sstevel@tonic-gate 		 * Don't return without checking accessibility
26977c478bd9Sstevel@tonic-gate 		 * of the directory. We only need the lock if
26987c478bd9Sstevel@tonic-gate 		 * we are going to return it.
26997c478bd9Sstevel@tonic-gate 		 */
270060c8e821SFrank Batschulat 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) == 0) {
27017c478bd9Sstevel@tonic-gate 			VN_HOLD(dvp);
27027c478bd9Sstevel@tonic-gate 			*vpp = dvp;
27037c478bd9Sstevel@tonic-gate 		}
27047c478bd9Sstevel@tonic-gate 		goto out;
27057c478bd9Sstevel@tonic-gate 	}
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	/*
27087c478bd9Sstevel@tonic-gate 	 * Fast path: Check the directory name lookup cache.
27097c478bd9Sstevel@tonic-gate 	 */
27107c478bd9Sstevel@tonic-gate 	if (vp = dnlc_lookup(dvp, nm)) {
27117c478bd9Sstevel@tonic-gate 		/*
27127c478bd9Sstevel@tonic-gate 		 * Check accessibility of directory.
27137c478bd9Sstevel@tonic-gate 		 */
271460c8e821SFrank Batschulat 		if ((error = ufs_iaccess(ip, IEXEC, cr, 1)) != 0) {
27157c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27167c478bd9Sstevel@tonic-gate 			goto out;
27177c478bd9Sstevel@tonic-gate 		}
27187c478bd9Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE) {
27197c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
27207c478bd9Sstevel@tonic-gate 			error = ENOENT;
27217c478bd9Sstevel@tonic-gate 			goto out;
27227c478bd9Sstevel@tonic-gate 		}
27237c478bd9Sstevel@tonic-gate 		xip = VTOI(vp);
27247c478bd9Sstevel@tonic-gate 		ulp = NULL;
27257c478bd9Sstevel@tonic-gate 		goto fastpath;
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	/*
27297c478bd9Sstevel@tonic-gate 	 * Keep the idle queue from getting too long by
27307c478bd9Sstevel@tonic-gate 	 * idling two inodes before attempting to allocate another.
27317c478bd9Sstevel@tonic-gate 	 *    This operation must be performed before entering
27327c478bd9Sstevel@tonic-gate 	 *    lockfs or a transaction.
27337c478bd9Sstevel@tonic-gate 	 */
27347c478bd9Sstevel@tonic-gate 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
27357c478bd9Sstevel@tonic-gate 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
27367c478bd9Sstevel@tonic-gate 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
27377c478bd9Sstevel@tonic-gate 			ufs_idle_some(ufs_lookup_idle_count);
27387c478bd9Sstevel@tonic-gate 		}
27397c478bd9Sstevel@tonic-gate 
274002ff05a9Svsakar retry_lookup:
274160c8e821SFrank Batschulat 	/*
274260c8e821SFrank Batschulat 	 * Check accessibility of directory.
274360c8e821SFrank Batschulat 	 */
274460c8e821SFrank Batschulat 	if (error = ufs_diraccess(ip, IEXEC, cr))
274560c8e821SFrank Batschulat 		goto out;
274660c8e821SFrank Batschulat 
27477c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
27487c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
27497c478bd9Sstevel@tonic-gate 	if (error)
27507c478bd9Sstevel@tonic-gate 		goto out;
27517c478bd9Sstevel@tonic-gate 
27529b5097eeSOwen Roberts 	error = ufs_dirlook(ip, nm, &xip, cr, 1, 0);
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate fastpath:
27557c478bd9Sstevel@tonic-gate 	if (error == 0) {
27567c478bd9Sstevel@tonic-gate 		ip = xip;
27577c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
27587c478bd9Sstevel@tonic-gate 
27597c478bd9Sstevel@tonic-gate 		/*
27607c478bd9Sstevel@tonic-gate 		 * If vnode is a device return special vnode instead.
27617c478bd9Sstevel@tonic-gate 		 */
27627c478bd9Sstevel@tonic-gate 		if (IS_DEVVP(*vpp)) {
27637c478bd9Sstevel@tonic-gate 			struct vnode *newvp;
27647c478bd9Sstevel@tonic-gate 
27657c478bd9Sstevel@tonic-gate 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
27667c478bd9Sstevel@tonic-gate 			    cr);
27677c478bd9Sstevel@tonic-gate 			VN_RELE(*vpp);
27687c478bd9Sstevel@tonic-gate 			if (newvp == NULL)
27697c478bd9Sstevel@tonic-gate 				error = ENOSYS;
27707c478bd9Sstevel@tonic-gate 			else
27717c478bd9Sstevel@tonic-gate 				*vpp = newvp;
2772986fd29aSsetje 		} else if (ip->i_cflags & ICOMPRESS) {
2773986fd29aSsetje 			struct vnode *newvp;
2774986fd29aSsetje 
2775986fd29aSsetje 			/*
2776986fd29aSsetje 			 * Compressed file, substitute dcfs vnode
2777986fd29aSsetje 			 */
2778986fd29aSsetje 			newvp = decompvp(*vpp, cr, ct);
2779986fd29aSsetje 			VN_RELE(*vpp);
2780986fd29aSsetje 			if (newvp == NULL)
2781986fd29aSsetje 				error = ENOSYS;
2782986fd29aSsetje 			else
2783986fd29aSsetje 				*vpp = newvp;
27847c478bd9Sstevel@tonic-gate 		}
27857c478bd9Sstevel@tonic-gate 	}
27867c478bd9Sstevel@tonic-gate 	if (ulp) {
27877c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
27887c478bd9Sstevel@tonic-gate 	}
27897c478bd9Sstevel@tonic-gate 
279002ff05a9Svsakar 	if (error == EAGAIN)
279102ff05a9Svsakar 		goto retry_lookup;
279202ff05a9Svsakar 
27937c478bd9Sstevel@tonic-gate out:
27947c478bd9Sstevel@tonic-gate 	return (error);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
2797da6c28aaSamw /*ARGSUSED*/
27987c478bd9Sstevel@tonic-gate static int
ufs_create(struct vnode * dvp,char * name,struct vattr * vap,enum vcexcl excl,int mode,struct vnode ** vpp,struct cred * cr,int flag,caller_context_t * ct,vsecattr_t * vsecp)27997c478bd9Sstevel@tonic-gate ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
280080d5689fSPatrick Mooney     int mode, struct vnode **vpp, struct cred *cr, int flag,
280180d5689fSPatrick Mooney     caller_context_t *ct, vsecattr_t *vsecp)
28027c478bd9Sstevel@tonic-gate {
28037c478bd9Sstevel@tonic-gate 	struct inode *ip;
28047c478bd9Sstevel@tonic-gate 	struct inode *xip;
28057c478bd9Sstevel@tonic-gate 	struct inode *dip;
28067c478bd9Sstevel@tonic-gate 	struct vnode *xvp;
28077c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
28087c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
28097c478bd9Sstevel@tonic-gate 	int error;
28107c478bd9Sstevel@tonic-gate 	int issync;
28117c478bd9Sstevel@tonic-gate 	int truncflag;
28127c478bd9Sstevel@tonic-gate 	int trans_size;
28137c478bd9Sstevel@tonic-gate 	int noentry;
28147c478bd9Sstevel@tonic-gate 	int defer_dip_seq_update = 0;	/* need to defer update of dip->i_seq */
28157c478bd9Sstevel@tonic-gate 	int retry = 1;
281602ff05a9Svsakar 	int indeadlock;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate again:
28197c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
28207c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
28217c478bd9Sstevel@tonic-gate 	truncflag = 0;
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
28247c478bd9Sstevel@tonic-gate 	if (error)
28257c478bd9Sstevel@tonic-gate 		goto out;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 	if (ulp) {
28287c478bd9Sstevel@tonic-gate 		trans_size = (int)TOP_CREATE_SIZE(ip);
28297c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
28337c478bd9Sstevel@tonic-gate 		vap->va_mode &= ~VSVTX;
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	if (*name == '\0') {
28367c478bd9Sstevel@tonic-gate 		/*
28377c478bd9Sstevel@tonic-gate 		 * Null component name refers to the directory itself.
28387c478bd9Sstevel@tonic-gate 		 */
28397c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
28407c478bd9Sstevel@tonic-gate 		/*
28417c478bd9Sstevel@tonic-gate 		 * Even though this is an error case, we need to grab the
28427c478bd9Sstevel@tonic-gate 		 * quota lock since the error handling code below is common.
28437c478bd9Sstevel@tonic-gate 		 */
28447c478bd9Sstevel@tonic-gate 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
28457c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
28467c478bd9Sstevel@tonic-gate 		error = EEXIST;
28477c478bd9Sstevel@tonic-gate 	} else {
28487c478bd9Sstevel@tonic-gate 		xip = NULL;
28497c478bd9Sstevel@tonic-gate 		noentry = 0;
285002ff05a9Svsakar 		/*
285102ff05a9Svsakar 		 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
285202ff05a9Svsakar 		 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
285302ff05a9Svsakar 		 * possible, retries the operation.
285402ff05a9Svsakar 		 */
285502ff05a9Svsakar 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_CREATE,
285680d34432Sfrankho 		    retry_dir);
285702ff05a9Svsakar 		if (indeadlock)
285802ff05a9Svsakar 			goto again;
285902ff05a9Svsakar 
28607c478bd9Sstevel@tonic-gate 		xvp = dnlc_lookup(dvp, name);
28617c478bd9Sstevel@tonic-gate 		if (xvp == DNLC_NO_VNODE) {
28627c478bd9Sstevel@tonic-gate 			noentry = 1;
28637c478bd9Sstevel@tonic-gate 			VN_RELE(xvp);
28647c478bd9Sstevel@tonic-gate 			xvp = NULL;
28657c478bd9Sstevel@tonic-gate 		}
28667c478bd9Sstevel@tonic-gate 		if (xvp) {
28677c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
286860c8e821SFrank Batschulat 			if (error = ufs_iaccess(ip, IEXEC, cr, 1)) {
28697c478bd9Sstevel@tonic-gate 				VN_RELE(xvp);
28707c478bd9Sstevel@tonic-gate 			} else {
28717c478bd9Sstevel@tonic-gate 				error = EEXIST;
28727c478bd9Sstevel@tonic-gate 				xip = VTOI(xvp);
28737c478bd9Sstevel@tonic-gate 			}
28747c478bd9Sstevel@tonic-gate 		} else {
28757c478bd9Sstevel@tonic-gate 			/*
28767c478bd9Sstevel@tonic-gate 			 * Suppress file system full message if we will retry
28777c478bd9Sstevel@tonic-gate 			 */
28787c478bd9Sstevel@tonic-gate 			error = ufs_direnter_cm(ip, name, DE_CREATE,
287980d34432Sfrankho 			    vap, &xip, cr, (noentry | (retry ? IQUIET : 0)));
288002ff05a9Svsakar 			if (error == EAGAIN) {
288102ff05a9Svsakar 				if (ulp) {
288202ff05a9Svsakar 					TRANS_END_CSYNC(ufsvfsp, error, issync,
288380d34432Sfrankho 					    TOP_CREATE, trans_size);
288402ff05a9Svsakar 					ufs_lockfs_end(ulp);
288502ff05a9Svsakar 				}
288602ff05a9Svsakar 				goto again;
288702ff05a9Svsakar 			}
28887c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
28897c478bd9Sstevel@tonic-gate 		}
28907c478bd9Sstevel@tonic-gate 		ip = xip;
28917c478bd9Sstevel@tonic-gate 		if (ip != NULL) {
28927c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
28937c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_WRITER);
28947c478bd9Sstevel@tonic-gate 		}
28957c478bd9Sstevel@tonic-gate 	}
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	/*
28987c478bd9Sstevel@tonic-gate 	 * If the file already exists and this is a non-exclusive create,
28997c478bd9Sstevel@tonic-gate 	 * check permissions and allow access for non-directories.
29007c478bd9Sstevel@tonic-gate 	 * Read-only create of an existing directory is also allowed.
29017c478bd9Sstevel@tonic-gate 	 * We fail an exclusive create of anything which already exists.
29027c478bd9Sstevel@tonic-gate 	 */
29037c478bd9Sstevel@tonic-gate 	if (error == EEXIST) {
29047c478bd9Sstevel@tonic-gate 		dip = VTOI(dvp);
29057c478bd9Sstevel@tonic-gate 		if (excl == NONEXCL) {
29067c478bd9Sstevel@tonic-gate 			if ((((ip->i_mode & IFMT) == IFDIR) ||
29077c478bd9Sstevel@tonic-gate 			    ((ip->i_mode & IFMT) == IFATTRDIR)) &&
29087c478bd9Sstevel@tonic-gate 			    (mode & IWRITE))
29097c478bd9Sstevel@tonic-gate 				error = EISDIR;
29107c478bd9Sstevel@tonic-gate 			else if (mode)
291160c8e821SFrank Batschulat 				error = ufs_iaccess(ip, mode, cr, 0);
29127c478bd9Sstevel@tonic-gate 			else
29137c478bd9Sstevel@tonic-gate 				error = 0;
29147c478bd9Sstevel@tonic-gate 		}
29157c478bd9Sstevel@tonic-gate 		if (error) {
29167c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
29177c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
29187c478bd9Sstevel@tonic-gate 			VN_RELE(ITOV(ip));
29197c478bd9Sstevel@tonic-gate 			goto unlock;
29207c478bd9Sstevel@tonic-gate 		}
29217c478bd9Sstevel@tonic-gate 		/*
29227c478bd9Sstevel@tonic-gate 		 * If the error EEXIST was set, then i_seq can not
29237c478bd9Sstevel@tonic-gate 		 * have been updated. The sequence number interface
29247c478bd9Sstevel@tonic-gate 		 * is defined such that a non-error VOP_CREATE must
29257c478bd9Sstevel@tonic-gate 		 * increase the dir va_seq it by at least one. If we
29267c478bd9Sstevel@tonic-gate 		 * have cleared the error, increase i_seq. Note that
29277c478bd9Sstevel@tonic-gate 		 * we are increasing the dir i_seq and in rare cases
29287c478bd9Sstevel@tonic-gate 		 * ip may actually be from the dvp, so we already have
29297c478bd9Sstevel@tonic-gate 		 * the locks and it will not be subject to truncation.
29307c478bd9Sstevel@tonic-gate 		 * In case we have to update i_seq of the parent
29317c478bd9Sstevel@tonic-gate 		 * directory dip, we have to defer it till we have
29327c478bd9Sstevel@tonic-gate 		 * released our locks on ip due to lock ordering requirements.
29337c478bd9Sstevel@tonic-gate 		 */
29347c478bd9Sstevel@tonic-gate 		if (ip != dip)
29357c478bd9Sstevel@tonic-gate 			defer_dip_seq_update = 1;
29367c478bd9Sstevel@tonic-gate 		else
29377c478bd9Sstevel@tonic-gate 			ip->i_seq++;
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate 		if (((ip->i_mode & IFMT) == IFREG) &&
29407c478bd9Sstevel@tonic-gate 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
29417c478bd9Sstevel@tonic-gate 			/*
29427c478bd9Sstevel@tonic-gate 			 * Truncate regular files, if requested by caller.
29437c478bd9Sstevel@tonic-gate 			 * Grab i_rwlock to make sure no one else is
29447c478bd9Sstevel@tonic-gate 			 * currently writing to the file (we promised
29457c478bd9Sstevel@tonic-gate 			 * bmap we would do this).
29467c478bd9Sstevel@tonic-gate 			 * Must get the locks in the correct order.
29477c478bd9Sstevel@tonic-gate 			 */
29487c478bd9Sstevel@tonic-gate 			if (ip->i_size == 0) {
29497c478bd9Sstevel@tonic-gate 				ip->i_flag |= ICHG | IUPD;
29507c478bd9Sstevel@tonic-gate 				ip->i_seq++;
29517c478bd9Sstevel@tonic-gate 				TRANS_INODE(ufsvfsp, ip);
29527c478bd9Sstevel@tonic-gate 			} else {
29537c478bd9Sstevel@tonic-gate 				/*
29547c478bd9Sstevel@tonic-gate 				 * Large Files: Why this check here?
29557c478bd9Sstevel@tonic-gate 				 * Though we do it in vn_create() we really
29567c478bd9Sstevel@tonic-gate 				 * want to guarantee that we do not destroy
29577c478bd9Sstevel@tonic-gate 				 * Large file data by atomically checking
29587c478bd9Sstevel@tonic-gate 				 * the size while holding the contents
29597c478bd9Sstevel@tonic-gate 				 * lock.
29607c478bd9Sstevel@tonic-gate 				 */
29617c478bd9Sstevel@tonic-gate 				if (flag && !(flag & FOFFMAX) &&
29627c478bd9Sstevel@tonic-gate 				    ((ip->i_mode & IFMT) == IFREG) &&
29637c478bd9Sstevel@tonic-gate 				    (ip->i_size > (offset_t)MAXOFF32_T)) {
29647c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
29657c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
29667c478bd9Sstevel@tonic-gate 					error = EOVERFLOW;
29677c478bd9Sstevel@tonic-gate 					goto unlock;
29687c478bd9Sstevel@tonic-gate 				}
29697c478bd9Sstevel@tonic-gate 				if (TRANS_ISTRANS(ufsvfsp))
29707c478bd9Sstevel@tonic-gate 					truncflag++;
29717c478bd9Sstevel@tonic-gate 				else {
29727c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
29737c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
297402ff05a9Svsakar 					ufs_tryirwlock_trans(&ip->i_rwlock,
297580d34432Sfrankho 					    RW_WRITER, TOP_CREATE,
297680d34432Sfrankho 					    retry_file);
297702ff05a9Svsakar 					if (indeadlock) {
297802ff05a9Svsakar 						VN_RELE(ITOV(ip));
297902ff05a9Svsakar 						goto again;
298002ff05a9Svsakar 					}
29817c478bd9Sstevel@tonic-gate 					rw_enter(&ufsvfsp->vfs_dqrwlock,
298280d34432Sfrankho 					    RW_READER);
29837c478bd9Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
29847c478bd9Sstevel@tonic-gate 					(void) ufs_itrunc(ip, (u_offset_t)0, 0,
298580d34432Sfrankho 					    cr);
29867c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_rwlock);
29877c478bd9Sstevel@tonic-gate 				}
2988df2381bfSpraks 
2989df2381bfSpraks 			}
2990df2381bfSpraks 			if (error == 0) {
2991da6c28aaSamw 				vnevent_create(ITOV(ip), ct);
29927c478bd9Sstevel@tonic-gate 			}
29937c478bd9Sstevel@tonic-gate 		}
29947c478bd9Sstevel@tonic-gate 	}
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	if (error) {
29977c478bd9Sstevel@tonic-gate 		if (ip != NULL) {
29987c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
29997c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
30007c478bd9Sstevel@tonic-gate 		}
30017c478bd9Sstevel@tonic-gate 		goto unlock;
30027c478bd9Sstevel@tonic-gate 	}
30037c478bd9Sstevel@tonic-gate 
30047c478bd9Sstevel@tonic-gate 	*vpp = ITOV(ip);
30057c478bd9Sstevel@tonic-gate 	ITIMES(ip);
30067c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
30077c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 	/*
30107c478bd9Sstevel@tonic-gate 	 * If vnode is a device return special vnode instead.
30117c478bd9Sstevel@tonic-gate 	 */
30127c478bd9Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
30137c478bd9Sstevel@tonic-gate 		struct vnode *newvp;
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
30167c478bd9Sstevel@tonic-gate 		VN_RELE(*vpp);
30177c478bd9Sstevel@tonic-gate 		if (newvp == NULL) {
30187c478bd9Sstevel@tonic-gate 			error = ENOSYS;
30197c478bd9Sstevel@tonic-gate 			goto unlock;
30207c478bd9Sstevel@tonic-gate 		}
30217c478bd9Sstevel@tonic-gate 		truncflag = 0;
30227c478bd9Sstevel@tonic-gate 		*vpp = newvp;
30237c478bd9Sstevel@tonic-gate 	}
30247c478bd9Sstevel@tonic-gate unlock:
30257c478bd9Sstevel@tonic-gate 
30267c478bd9Sstevel@tonic-gate 	/*
30277c478bd9Sstevel@tonic-gate 	 * Do the deferred update of the parent directory's sequence
30287c478bd9Sstevel@tonic-gate 	 * number now.
30297c478bd9Sstevel@tonic-gate 	 */
30307c478bd9Sstevel@tonic-gate 	if (defer_dip_seq_update == 1) {
30317c478bd9Sstevel@tonic-gate 		rw_enter(&dip->i_contents, RW_READER);
30327c478bd9Sstevel@tonic-gate 		mutex_enter(&dip->i_tlock);
30337c478bd9Sstevel@tonic-gate 		dip->i_seq++;
30347c478bd9Sstevel@tonic-gate 		mutex_exit(&dip->i_tlock);
30357c478bd9Sstevel@tonic-gate 		rw_exit(&dip->i_contents);
30367c478bd9Sstevel@tonic-gate 	}
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 	if (ulp) {
30397c478bd9Sstevel@tonic-gate 		int terr = 0;
30407c478bd9Sstevel@tonic-gate 
30417c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
30427c478bd9Sstevel@tonic-gate 		    trans_size);
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 		/*
30457c478bd9Sstevel@tonic-gate 		 * If we haven't had a more interesting failure
30467c478bd9Sstevel@tonic-gate 		 * already, then anything that might've happened
30477c478bd9Sstevel@tonic-gate 		 * here should be reported.
30487c478bd9Sstevel@tonic-gate 		 */
30497c478bd9Sstevel@tonic-gate 		if (error == 0)
30507c478bd9Sstevel@tonic-gate 			error = terr;
30517c478bd9Sstevel@tonic-gate 	}
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 	if (!error && truncflag) {
305402ff05a9Svsakar 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_trunc);
305502ff05a9Svsakar 		if (indeadlock) {
305602ff05a9Svsakar 			if (ulp)
305702ff05a9Svsakar 				ufs_lockfs_end(ulp);
305802ff05a9Svsakar 			VN_RELE(ITOV(ip));
305902ff05a9Svsakar 			goto again;
306002ff05a9Svsakar 		}
30617c478bd9Sstevel@tonic-gate 		(void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
30627c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
30637c478bd9Sstevel@tonic-gate 	}
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	if (ulp)
30667c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	/*
30697c478bd9Sstevel@tonic-gate 	 * If no inodes available, try to free one up out of the
30707c478bd9Sstevel@tonic-gate 	 * pending delete queue.
30717c478bd9Sstevel@tonic-gate 	 */
30727c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
30737c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
30747c478bd9Sstevel@tonic-gate 		retry = 0;
30757c478bd9Sstevel@tonic-gate 		goto again;
30767c478bd9Sstevel@tonic-gate 	}
30777c478bd9Sstevel@tonic-gate 
30787c478bd9Sstevel@tonic-gate out:
30797c478bd9Sstevel@tonic-gate 	return (error);
30807c478bd9Sstevel@tonic-gate }
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate extern int ufs_idle_max;
30837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
30847c478bd9Sstevel@tonic-gate static int
ufs_remove(struct vnode * vp,char * nm,struct cred * cr,caller_context_t * ct,int flags)308580d5689fSPatrick Mooney ufs_remove(struct vnode *vp, char *nm, struct cred *cr, caller_context_t *ct,
308680d5689fSPatrick Mooney     int flags)
30877c478bd9Sstevel@tonic-gate {
30887c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
30897c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp	= ip->i_ufsvfs;
30907c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
30917c478bd9Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode corresponding to name being removed */
309202ff05a9Svsakar 	int indeadlock;
30937c478bd9Sstevel@tonic-gate 	int error;
30947c478bd9Sstevel@tonic-gate 	int issync;
30957c478bd9Sstevel@tonic-gate 	int trans_size;
30967c478bd9Sstevel@tonic-gate 
30977c478bd9Sstevel@tonic-gate 	/*
30987c478bd9Sstevel@tonic-gate 	 * don't let the delete queue get too long
30997c478bd9Sstevel@tonic-gate 	 */
31007c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
31017c478bd9Sstevel@tonic-gate 		error = EIO;
31027c478bd9Sstevel@tonic-gate 		goto out;
31037c478bd9Sstevel@tonic-gate 	}
31047c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
31057c478bd9Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
31067c478bd9Sstevel@tonic-gate 
31079b5097eeSOwen Roberts 	error = ufs_eventlookup(vp, nm, cr, &rmvp);
31089b5097eeSOwen Roberts 	if (rmvp != NULL) {
31099b5097eeSOwen Roberts 		/* Only send the event if there were no errors */
31109b5097eeSOwen Roberts 		if (error == 0)
31119b5097eeSOwen Roberts 			vnevent_remove(rmvp, vp, nm, ct);
31129b5097eeSOwen Roberts 		VN_RELE(rmvp);
31139b5097eeSOwen Roberts 	}
31149b5097eeSOwen Roberts 
311502ff05a9Svsakar retry_remove:
31167c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
31177c478bd9Sstevel@tonic-gate 	if (error)
31187c478bd9Sstevel@tonic-gate 		goto out;
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 	if (ulp)
31217c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
31227c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
31237c478bd9Sstevel@tonic-gate 
312402ff05a9Svsakar 	/*
312502ff05a9Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
312602ff05a9Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
312702ff05a9Svsakar 	 * possible, retries the operation.
312802ff05a9Svsakar 	 */
312902ff05a9Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_REMOVE, retry);
313002ff05a9Svsakar 	if (indeadlock)
313102ff05a9Svsakar 		goto retry_remove;
31327c478bd9Sstevel@tonic-gate 	error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
31339b5097eeSOwen Roberts 	    DR_REMOVE, cr);
31347c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	if (ulp) {
31377c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
31387c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
31397c478bd9Sstevel@tonic-gate 	}
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate out:
31427c478bd9Sstevel@tonic-gate 	return (error);
31437c478bd9Sstevel@tonic-gate }
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate /*
31467c478bd9Sstevel@tonic-gate  * Link a file or a directory.  Only privileged processes are allowed to
31477c478bd9Sstevel@tonic-gate  * make links to directories.
31487c478bd9Sstevel@tonic-gate  */
3149da6c28aaSamw /*ARGSUSED*/
31507c478bd9Sstevel@tonic-gate static int
ufs_link(struct vnode * tdvp,struct vnode * svp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)3151da6c28aaSamw ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr,
315280d5689fSPatrick Mooney     caller_context_t *ct, int flags)
31537c478bd9Sstevel@tonic-gate {
31547c478bd9Sstevel@tonic-gate 	struct inode *sip;
31557c478bd9Sstevel@tonic-gate 	struct inode *tdp = VTOI(tdvp);
31567c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
31577c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
31587c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
31597c478bd9Sstevel@tonic-gate 	int error;
31607c478bd9Sstevel@tonic-gate 	int issync;
31617c478bd9Sstevel@tonic-gate 	int trans_size;
31627c478bd9Sstevel@tonic-gate 	int isdev;
316302ff05a9Svsakar 	int indeadlock;
31647c478bd9Sstevel@tonic-gate 
316502ff05a9Svsakar retry_link:
31667c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
31677c478bd9Sstevel@tonic-gate 	if (error)
31687c478bd9Sstevel@tonic-gate 		goto out;
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	if (ulp)
31717c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
31727c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
31737c478bd9Sstevel@tonic-gate 
3174da6c28aaSamw 	if (VOP_REALVP(svp, &realvp, ct) == 0)
31757c478bd9Sstevel@tonic-gate 		svp = realvp;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	/*
31787c478bd9Sstevel@tonic-gate 	 * Make sure link for extended attributes is valid
31797c478bd9Sstevel@tonic-gate 	 * We only support hard linking of attr in ATTRDIR to ATTRDIR
31807c478bd9Sstevel@tonic-gate 	 *
31817c478bd9Sstevel@tonic-gate 	 * Make certain we don't attempt to look at a device node as
31827c478bd9Sstevel@tonic-gate 	 * a ufs inode.
31837c478bd9Sstevel@tonic-gate 	 */
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	isdev = IS_DEVVP(svp);
31867c478bd9Sstevel@tonic-gate 	if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
31877c478bd9Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
31887c478bd9Sstevel@tonic-gate 	    ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
31897c478bd9Sstevel@tonic-gate 	    ((tdp->i_mode & IFMT) == IFDIR))) {
31907c478bd9Sstevel@tonic-gate 		error = EINVAL;
31917c478bd9Sstevel@tonic-gate 		goto unlock;
31927c478bd9Sstevel@tonic-gate 	}
31937c478bd9Sstevel@tonic-gate 
31947c478bd9Sstevel@tonic-gate 	sip = VTOI(svp);
31957c478bd9Sstevel@tonic-gate 	if ((svp->v_type == VDIR &&
31967c478bd9Sstevel@tonic-gate 	    secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
31977c478bd9Sstevel@tonic-gate 	    (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
31987c478bd9Sstevel@tonic-gate 		error = EPERM;
31997c478bd9Sstevel@tonic-gate 		goto unlock;
32007c478bd9Sstevel@tonic-gate 	}
320102ff05a9Svsakar 
320202ff05a9Svsakar 	/*
320302ff05a9Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
320402ff05a9Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
320502ff05a9Svsakar 	 * possible, retries the operation.
320602ff05a9Svsakar 	 */
320702ff05a9Svsakar 	ufs_tryirwlock_trans(&tdp->i_rwlock, RW_WRITER, TOP_LINK, retry);
320802ff05a9Svsakar 	if (indeadlock)
320902ff05a9Svsakar 		goto retry_link;
32107c478bd9Sstevel@tonic-gate 	error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
32119b5097eeSOwen Roberts 	    sip, cr);
32127c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate unlock:
32157c478bd9Sstevel@tonic-gate 	if (ulp) {
32167c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
32177c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
32187c478bd9Sstevel@tonic-gate 	}
3219df2381bfSpraks 
3220df2381bfSpraks 	if (!error) {
3221da6c28aaSamw 		vnevent_link(svp, ct);
3222df2381bfSpraks 	}
32237c478bd9Sstevel@tonic-gate out:
32247c478bd9Sstevel@tonic-gate 	return (error);
32257c478bd9Sstevel@tonic-gate }
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate uint64_t ufs_rename_retry_cnt;
32287c478bd9Sstevel@tonic-gate uint64_t ufs_rename_upgrade_retry_cnt;
32297c478bd9Sstevel@tonic-gate uint64_t ufs_rename_dircheck_retry_cnt;
32307c478bd9Sstevel@tonic-gate clock_t	 ufs_rename_backoff_delay = 1;
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate /*
32337c478bd9Sstevel@tonic-gate  * Rename a file or directory.
32347c478bd9Sstevel@tonic-gate  * We are given the vnode and entry string of the source and the
32357c478bd9Sstevel@tonic-gate  * vnode and entry string of the place we want to move the source
32367c478bd9Sstevel@tonic-gate  * to (the target). The essential operation is:
32377c478bd9Sstevel@tonic-gate  *	unlink(target);
32387c478bd9Sstevel@tonic-gate  *	link(source, target);
32397c478bd9Sstevel@tonic-gate  *	unlink(source);
32407c478bd9Sstevel@tonic-gate  * but "atomically".  Can't do full commit without saving state in
32417c478bd9Sstevel@tonic-gate  * the inode on disk, which isn't feasible at this time.  Best we
32427c478bd9Sstevel@tonic-gate  * can do is always guarantee that the TARGET exists.
32437c478bd9Sstevel@tonic-gate  */
3244baa4d099Sswilcox 
32457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
32467c478bd9Sstevel@tonic-gate static int
ufs_rename(struct vnode * sdvp,char * snm,struct vnode * tdvp,char * tnm,struct cred * cr,caller_context_t * ct,int flags)324780d5689fSPatrick Mooney ufs_rename(struct vnode *sdvp, char *snm, struct vnode *tdvp, char *tnm,
324880d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct, int flags)
32497c478bd9Sstevel@tonic-gate {
32507c478bd9Sstevel@tonic-gate 	struct inode *sip = NULL;	/* source inode */
3251baa4d099Sswilcox 	struct inode *ip = NULL;	/* check inode */
32527c478bd9Sstevel@tonic-gate 	struct inode *sdp;		/* old (source) parent inode */
32537c478bd9Sstevel@tonic-gate 	struct inode *tdp;		/* new (target) parent inode */
32549b5097eeSOwen Roberts 	struct vnode *svp = NULL;	/* source vnode */
32557c478bd9Sstevel@tonic-gate 	struct vnode *tvp = NULL;	/* target vnode, if it exists */
32567c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
32577c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
325854207fd2SJerry Jelinek 	struct ulockfs *ulp = NULL;
32595b024a5bSbatschul 	struct ufs_slot slot;
3260baa4d099Sswilcox 	timestruc_t now;
32617c478bd9Sstevel@tonic-gate 	int error;
32627c478bd9Sstevel@tonic-gate 	int issync;
32637c478bd9Sstevel@tonic-gate 	int trans_size;
326402ff05a9Svsakar 	krwlock_t *first_lock;
326502ff05a9Svsakar 	krwlock_t *second_lock;
326602ff05a9Svsakar 	krwlock_t *reverse_lock;
32679b5097eeSOwen Roberts 	int serr, terr;
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	sdp = VTOI(sdvp);
3270baa4d099Sswilcox 	slot.fbp = NULL;
32717c478bd9Sstevel@tonic-gate 	ufsvfsp = sdp->i_ufsvfs;
32729b5097eeSOwen Roberts 
32739b5097eeSOwen Roberts 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
32749b5097eeSOwen Roberts 		tdvp = realvp;
32759b5097eeSOwen Roberts 
327654207fd2SJerry Jelinek 	/* Must do this before taking locks in case of DNLC miss */
32779b5097eeSOwen Roberts 	terr = ufs_eventlookup(tdvp, tnm, cr, &tvp);
32789b5097eeSOwen Roberts 	serr = ufs_eventlookup(sdvp, snm, cr, &svp);
32799b5097eeSOwen Roberts 
32809b5097eeSOwen Roberts 	if ((serr == 0) && ((terr == 0) || (terr == ENOENT))) {
32819b5097eeSOwen Roberts 		if (tvp != NULL)
328254207fd2SJerry Jelinek 			vnevent_pre_rename_dest(tvp, tdvp, tnm, ct);
32839b5097eeSOwen Roberts 
32849b5097eeSOwen Roberts 		/*
32859b5097eeSOwen Roberts 		 * Notify the target directory of the rename event
32869b5097eeSOwen Roberts 		 * if source and target directories are not the same.
32879b5097eeSOwen Roberts 		 */
32889b5097eeSOwen Roberts 		if (sdvp != tdvp)
328954207fd2SJerry Jelinek 			vnevent_pre_rename_dest_dir(tdvp, svp, tnm, ct);
32909b5097eeSOwen Roberts 
32919b5097eeSOwen Roberts 		if (svp != NULL)
329254207fd2SJerry Jelinek 			vnevent_pre_rename_src(svp, sdvp, snm, ct);
32939b5097eeSOwen Roberts 	}
32949b5097eeSOwen Roberts 
32959b5097eeSOwen Roberts 	if (svp != NULL)
32969b5097eeSOwen Roberts 		VN_RELE(svp);
32979b5097eeSOwen Roberts 
329802ff05a9Svsakar retry_rename:
32997c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
33007c478bd9Sstevel@tonic-gate 	if (error)
330154207fd2SJerry Jelinek 		goto unlock;
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	if (ulp)
33047c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
33057c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_RENAME_SIZE(sdp));
33067c478bd9Sstevel@tonic-gate 
3307da6c28aaSamw 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
33087c478bd9Sstevel@tonic-gate 		tdvp = realvp;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	tdp = VTOI(tdvp);
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	/*
33137c478bd9Sstevel@tonic-gate 	 * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
33147c478bd9Sstevel@tonic-gate 	 */
33157c478bd9Sstevel@tonic-gate 	if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
33167c478bd9Sstevel@tonic-gate 		error = EINVAL;
33177c478bd9Sstevel@tonic-gate 		goto unlock;
33187c478bd9Sstevel@tonic-gate 	}
33197c478bd9Sstevel@tonic-gate 
332060c8e821SFrank Batschulat 	/*
332160c8e821SFrank Batschulat 	 * Check accessibility of directory.
332260c8e821SFrank Batschulat 	 */
332360c8e821SFrank Batschulat 	if (error = ufs_diraccess(sdp, IEXEC, cr))
332460c8e821SFrank Batschulat 		goto unlock;
332560c8e821SFrank Batschulat 
33267c478bd9Sstevel@tonic-gate 	/*
33277c478bd9Sstevel@tonic-gate 	 * Look up inode of file we're supposed to rename.
33287c478bd9Sstevel@tonic-gate 	 */
3329baa4d099Sswilcox 	gethrestime(&now);
33309b5097eeSOwen Roberts 	if (error = ufs_dirlook(sdp, snm, &sip, cr, 0, 0)) {
333102ff05a9Svsakar 		if (error == EAGAIN) {
333202ff05a9Svsakar 			if (ulp) {
333302ff05a9Svsakar 				TRANS_END_CSYNC(ufsvfsp, error, issync,
333480d34432Sfrankho 				    TOP_RENAME, trans_size);
333502ff05a9Svsakar 				ufs_lockfs_end(ulp);
333602ff05a9Svsakar 			}
333702ff05a9Svsakar 			goto retry_rename;
333802ff05a9Svsakar 		}
333902ff05a9Svsakar 
33407c478bd9Sstevel@tonic-gate 		goto unlock;
33417c478bd9Sstevel@tonic-gate 	}
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	/*
33447c478bd9Sstevel@tonic-gate 	 * Lock both the source and target directories (they may be
33457c478bd9Sstevel@tonic-gate 	 * the same) to provide the atomicity semantics that was
33467c478bd9Sstevel@tonic-gate 	 * previously provided by the per file system vfs_rename_lock
33477c478bd9Sstevel@tonic-gate 	 *
33487c478bd9Sstevel@tonic-gate 	 * with vfs_rename_lock removed to allow simultaneous renames
33497c478bd9Sstevel@tonic-gate 	 * within a file system, ufs_dircheckpath can deadlock while
33507c478bd9Sstevel@tonic-gate 	 * traversing back to ensure that source is not a parent directory
33517c478bd9Sstevel@tonic-gate 	 * of target parent directory. This is because we get into
33527c478bd9Sstevel@tonic-gate 	 * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
33537c478bd9Sstevel@tonic-gate 	 * If the tdp and sdp of the simultaneous renames happen to be
33547c478bd9Sstevel@tonic-gate 	 * in the path of each other, it can lead to a deadlock. This
33557c478bd9Sstevel@tonic-gate 	 * can be avoided by getting the locks as RW_READER here and then
33567c478bd9Sstevel@tonic-gate 	 * upgrading to RW_WRITER after completing the ufs_dircheckpath.
335702ff05a9Svsakar 	 *
335802ff05a9Svsakar 	 * We hold the target directory's i_rwlock after calling
335902ff05a9Svsakar 	 * ufs_lockfs_begin but in many other operations (like ufs_readdir)
336002ff05a9Svsakar 	 * VOP_RWLOCK is explicitly called by the filesystem independent code
336102ff05a9Svsakar 	 * before calling the file system operation. In these cases the order
336202ff05a9Svsakar 	 * is reversed (i.e i_rwlock is taken first and then ufs_lockfs_begin
336302ff05a9Svsakar 	 * is called). This is fine as long as ufs_lockfs_begin acts as a VOP
336402ff05a9Svsakar 	 * counter but with ufs_quiesce setting the SLOCK bit this becomes a
336502ff05a9Svsakar 	 * synchronizing object which might lead to a deadlock. So we use
336602ff05a9Svsakar 	 * rw_tryenter instead of rw_enter. If we fail to get this lock and
336702ff05a9Svsakar 	 * find that SLOCK bit is set, we call ufs_lockfs_end and restart the
336802ff05a9Svsakar 	 * operation.
33697c478bd9Sstevel@tonic-gate 	 */
33707c478bd9Sstevel@tonic-gate retry:
337102ff05a9Svsakar 	first_lock = &tdp->i_rwlock;
337202ff05a9Svsakar 	second_lock = &sdp->i_rwlock;
337302ff05a9Svsakar retry_firstlock:
337402ff05a9Svsakar 	if (!rw_tryenter(first_lock, RW_READER)) {
33757c478bd9Sstevel@tonic-gate 		/*
337602ff05a9Svsakar 		 * We didn't get the lock. Check if the SLOCK is set in the
337702ff05a9Svsakar 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
337802ff05a9Svsakar 		 * and wait for SLOCK to be cleared.
33797c478bd9Sstevel@tonic-gate 		 */
338002ff05a9Svsakar 
338102ff05a9Svsakar 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
338202ff05a9Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
338380d34432Sfrankho 			    trans_size);
338402ff05a9Svsakar 			ufs_lockfs_end(ulp);
338502ff05a9Svsakar 			goto retry_rename;
338602ff05a9Svsakar 
338702ff05a9Svsakar 		} else {
33887c478bd9Sstevel@tonic-gate 			/*
338902ff05a9Svsakar 			 * SLOCK isn't set so this is a genuine synchronization
339002ff05a9Svsakar 			 * case. Let's try again after giving them a breather.
33917c478bd9Sstevel@tonic-gate 			 */
339202ff05a9Svsakar 			delay(RETRY_LOCK_DELAY);
339302ff05a9Svsakar 			goto  retry_firstlock;
339402ff05a9Svsakar 		}
339502ff05a9Svsakar 	}
339602ff05a9Svsakar 	/*
339702ff05a9Svsakar 	 * Need to check if the tdp and sdp are same !!!
339802ff05a9Svsakar 	 */
339902ff05a9Svsakar 	if ((tdp != sdp) && (!rw_tryenter(second_lock, RW_READER))) {
340002ff05a9Svsakar 		/*
340102ff05a9Svsakar 		 * We didn't get the lock. Check if the SLOCK is set in the
340202ff05a9Svsakar 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
340302ff05a9Svsakar 		 * and wait for SLOCK to be cleared.
340402ff05a9Svsakar 		 */
340502ff05a9Svsakar 
340602ff05a9Svsakar 		rw_exit(first_lock);
340702ff05a9Svsakar 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
340802ff05a9Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
340980d34432Sfrankho 			    trans_size);
341002ff05a9Svsakar 			ufs_lockfs_end(ulp);
341102ff05a9Svsakar 			goto retry_rename;
341202ff05a9Svsakar 
341302ff05a9Svsakar 		} else {
341402ff05a9Svsakar 			/*
341502ff05a9Svsakar 			 * So we couldn't get the second level peer lock *and*
341602ff05a9Svsakar 			 * the SLOCK bit isn't set. Too bad we can be
341702ff05a9Svsakar 			 * contentding with someone wanting these locks otherway
341802ff05a9Svsakar 			 * round. Reverse the locks in case there is a heavy
341902ff05a9Svsakar 			 * contention for the second level lock.
342002ff05a9Svsakar 			 */
342102ff05a9Svsakar 			reverse_lock = first_lock;
342202ff05a9Svsakar 			first_lock = second_lock;
342302ff05a9Svsakar 			second_lock = reverse_lock;
342402ff05a9Svsakar 			ufs_rename_retry_cnt++;
342502ff05a9Svsakar 			goto  retry_firstlock;
34267c478bd9Sstevel@tonic-gate 		}
34277c478bd9Sstevel@tonic-gate 	}
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	if (sip == tdp) {
34307c478bd9Sstevel@tonic-gate 		error = EINVAL;
34317c478bd9Sstevel@tonic-gate 		goto errout;
34327c478bd9Sstevel@tonic-gate 	}
34337c478bd9Sstevel@tonic-gate 	/*
34347c478bd9Sstevel@tonic-gate 	 * Make sure we can delete the source entry.  This requires
34357c478bd9Sstevel@tonic-gate 	 * write permission on the containing directory.
34367c478bd9Sstevel@tonic-gate 	 * Check for sticky directories.
34377c478bd9Sstevel@tonic-gate 	 */
34387c478bd9Sstevel@tonic-gate 	rw_enter(&sdp->i_contents, RW_READER);
34397c478bd9Sstevel@tonic-gate 	rw_enter(&sip->i_contents, RW_READER);
344060c8e821SFrank Batschulat 	if ((error = ufs_iaccess(sdp, IWRITE, cr, 0)) != 0 ||
34417c478bd9Sstevel@tonic-gate 	    (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
34427c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
34437c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
34447c478bd9Sstevel@tonic-gate 		goto errout;
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 	/*
34487c478bd9Sstevel@tonic-gate 	 * If this is a rename of a directory and the parent is
34497c478bd9Sstevel@tonic-gate 	 * different (".." must be changed), then the source
34507c478bd9Sstevel@tonic-gate 	 * directory must not be in the directory hierarchy
34517c478bd9Sstevel@tonic-gate 	 * above the target, as this would orphan everything
34527c478bd9Sstevel@tonic-gate 	 * below the source directory.  Also the user must have
34537c478bd9Sstevel@tonic-gate 	 * write permission in the source so as to be able to
34547c478bd9Sstevel@tonic-gate 	 * change "..".
34557c478bd9Sstevel@tonic-gate 	 */
34567c478bd9Sstevel@tonic-gate 	if ((((sip->i_mode & IFMT) == IFDIR) ||
34577c478bd9Sstevel@tonic-gate 	    ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
34587c478bd9Sstevel@tonic-gate 		ino_t	inum;
34597c478bd9Sstevel@tonic-gate 
346060c8e821SFrank Batschulat 		if (error = ufs_iaccess(sip, IWRITE, cr, 0)) {
34617c478bd9Sstevel@tonic-gate 			rw_exit(&sip->i_contents);
34627c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_contents);
34637c478bd9Sstevel@tonic-gate 			goto errout;
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 		inum = sip->i_number;
34667c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
34677c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
34687c478bd9Sstevel@tonic-gate 		if ((error = ufs_dircheckpath(inum, tdp, sdp, cr))) {
34697c478bd9Sstevel@tonic-gate 			/*
34707c478bd9Sstevel@tonic-gate 			 * If we got EAGAIN ufs_dircheckpath detected a
34717c478bd9Sstevel@tonic-gate 			 * potential deadlock and backed out. We need
34727c478bd9Sstevel@tonic-gate 			 * to retry the operation since sdp and tdp have
34737c478bd9Sstevel@tonic-gate 			 * to be released to avoid the deadlock.
34747c478bd9Sstevel@tonic-gate 			 */
34757c478bd9Sstevel@tonic-gate 			if (error == EAGAIN) {
34767c478bd9Sstevel@tonic-gate 				rw_exit(&tdp->i_rwlock);
34777c478bd9Sstevel@tonic-gate 				if (tdp != sdp)
34787c478bd9Sstevel@tonic-gate 					rw_exit(&sdp->i_rwlock);
34797c478bd9Sstevel@tonic-gate 				delay(ufs_rename_backoff_delay);
34807c478bd9Sstevel@tonic-gate 				ufs_rename_dircheck_retry_cnt++;
34817c478bd9Sstevel@tonic-gate 				goto retry;
34827c478bd9Sstevel@tonic-gate 			}
34837c478bd9Sstevel@tonic-gate 			goto errout;
34847c478bd9Sstevel@tonic-gate 		}
34857c478bd9Sstevel@tonic-gate 	} else {
34867c478bd9Sstevel@tonic-gate 		rw_exit(&sip->i_contents);
34877c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_contents);
34887c478bd9Sstevel@tonic-gate 	}
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 
34917c478bd9Sstevel@tonic-gate 	/*
34927c478bd9Sstevel@tonic-gate 	 * Check for renaming '.' or '..' or alias of '.'
34937c478bd9Sstevel@tonic-gate 	 */
34947c478bd9Sstevel@tonic-gate 	if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0 || sdp == sip) {
34957c478bd9Sstevel@tonic-gate 		error = EINVAL;
34967c478bd9Sstevel@tonic-gate 		goto errout;
34977c478bd9Sstevel@tonic-gate 	}
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate 	/*
35007c478bd9Sstevel@tonic-gate 	 * Simultaneous renames can deadlock in ufs_dircheckpath since it
35017c478bd9Sstevel@tonic-gate 	 * tries to traverse back the file tree with both tdp and sdp held
35027c478bd9Sstevel@tonic-gate 	 * as RW_WRITER. To avoid that we have to hold the tdp and sdp locks
35037c478bd9Sstevel@tonic-gate 	 * as RW_READERS  till ufs_dircheckpath is done.
35047c478bd9Sstevel@tonic-gate 	 * Now that ufs_dircheckpath is done with, we can upgrade the locks
35057c478bd9Sstevel@tonic-gate 	 * to RW_WRITER.
35067c478bd9Sstevel@tonic-gate 	 */
35077c478bd9Sstevel@tonic-gate 	if (!rw_tryupgrade(&tdp->i_rwlock)) {
35087c478bd9Sstevel@tonic-gate 		/*
35097c478bd9Sstevel@tonic-gate 		 * The upgrade failed. We got to give away the lock
35107c478bd9Sstevel@tonic-gate 		 * as to avoid deadlocking with someone else who is
35117c478bd9Sstevel@tonic-gate 		 * waiting for writer lock. With the lock gone, we
35127c478bd9Sstevel@tonic-gate 		 * cannot be sure the checks done above will hold
35137c478bd9Sstevel@tonic-gate 		 * good when we eventually get them back as writer.
35147c478bd9Sstevel@tonic-gate 		 * So if we can't upgrade we drop the locks and retry
35157c478bd9Sstevel@tonic-gate 		 * everything again.
35167c478bd9Sstevel@tonic-gate 		 */
35177c478bd9Sstevel@tonic-gate 		rw_exit(&tdp->i_rwlock);
35187c478bd9Sstevel@tonic-gate 		if (tdp != sdp)
35197c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
35207c478bd9Sstevel@tonic-gate 		delay(ufs_rename_backoff_delay);
35217c478bd9Sstevel@tonic-gate 		ufs_rename_upgrade_retry_cnt++;
35227c478bd9Sstevel@tonic-gate 		goto retry;
35237c478bd9Sstevel@tonic-gate 	}
35247c478bd9Sstevel@tonic-gate 	if (tdp != sdp) {
35257c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&sdp->i_rwlock)) {
35267c478bd9Sstevel@tonic-gate 			/*
35277c478bd9Sstevel@tonic-gate 			 * The upgrade failed. We got to give away the lock
35287c478bd9Sstevel@tonic-gate 			 * as to avoid deadlocking with someone else who is
35297c478bd9Sstevel@tonic-gate 			 * waiting for writer lock. With the lock gone, we
35307c478bd9Sstevel@tonic-gate 			 * cannot be sure the checks done above will hold
35317c478bd9Sstevel@tonic-gate 			 * good when we eventually get them back as writer.
35327c478bd9Sstevel@tonic-gate 			 * So if we can't upgrade we drop the locks and retry
35337c478bd9Sstevel@tonic-gate 			 * everything again.
35347c478bd9Sstevel@tonic-gate 			 */
35357c478bd9Sstevel@tonic-gate 			rw_exit(&tdp->i_rwlock);
35367c478bd9Sstevel@tonic-gate 			rw_exit(&sdp->i_rwlock);
35377c478bd9Sstevel@tonic-gate 			delay(ufs_rename_backoff_delay);
35387c478bd9Sstevel@tonic-gate 			ufs_rename_upgrade_retry_cnt++;
35397c478bd9Sstevel@tonic-gate 			goto retry;
35407c478bd9Sstevel@tonic-gate 		}
35417c478bd9Sstevel@tonic-gate 	}
3542baa4d099Sswilcox 
3543baa4d099Sswilcox 	/*
3544baa4d099Sswilcox 	 * Now that all the locks are held check to make sure another thread
3545baa4d099Sswilcox 	 * didn't slip in and take out the sip.
3546baa4d099Sswilcox 	 */
3547baa4d099Sswilcox 	slot.status = NONE;
3548baa4d099Sswilcox 	if ((sip->i_ctime.tv_usec * 1000) > now.tv_nsec ||
3549baa4d099Sswilcox 	    sip->i_ctime.tv_sec > now.tv_sec) {
3550baa4d099Sswilcox 		rw_enter(&sdp->i_ufsvfs->vfs_dqrwlock, RW_READER);
3551baa4d099Sswilcox 		rw_enter(&sdp->i_contents, RW_WRITER);
3552baa4d099Sswilcox 		error = ufs_dircheckforname(sdp, snm, strlen(snm), &slot,
3553baa4d099Sswilcox 		    &ip, cr, 0);
3554baa4d099Sswilcox 		rw_exit(&sdp->i_contents);
3555baa4d099Sswilcox 		rw_exit(&sdp->i_ufsvfs->vfs_dqrwlock);
3556baa4d099Sswilcox 		if (error) {
3557baa4d099Sswilcox 			goto errout;
3558baa4d099Sswilcox 		}
3559baa4d099Sswilcox 		if (ip == NULL) {
3560baa4d099Sswilcox 			error = ENOENT;
3561baa4d099Sswilcox 			goto errout;
3562baa4d099Sswilcox 		} else {
3563baa4d099Sswilcox 			/*
3564baa4d099Sswilcox 			 * If the inode was found need to drop the v_count
3565baa4d099Sswilcox 			 * so as not to keep the filesystem from being
3566baa4d099Sswilcox 			 * unmounted at a later time.
3567baa4d099Sswilcox 			 */
3568baa4d099Sswilcox 			VN_RELE(ITOV(ip));
3569baa4d099Sswilcox 		}
3570baa4d099Sswilcox 
3571baa4d099Sswilcox 		/*
3572baa4d099Sswilcox 		 * Release the slot.fbp that has the page mapped and
3573baa4d099Sswilcox 		 * locked SE_SHARED, and could be used in in
3574baa4d099Sswilcox 		 * ufs_direnter_lr() which needs to get the SE_EXCL lock
3575baa4d099Sswilcox 		 * on said page.
3576baa4d099Sswilcox 		 */
3577baa4d099Sswilcox 		if (slot.fbp) {
3578baa4d099Sswilcox 			fbrelse(slot.fbp, S_OTHER);
3579baa4d099Sswilcox 			slot.fbp = NULL;
3580baa4d099Sswilcox 		}
3581baa4d099Sswilcox 	}
3582baa4d099Sswilcox 
35837c478bd9Sstevel@tonic-gate 	/*
35849b5097eeSOwen Roberts 	 * Link source to the target.
35857c478bd9Sstevel@tonic-gate 	 */
35869b5097eeSOwen Roberts 	if (error = ufs_direnter_lr(tdp, tnm, DE_RENAME, sdp, sip, cr)) {
35877c478bd9Sstevel@tonic-gate 		/*
35887c478bd9Sstevel@tonic-gate 		 * ESAME isn't really an error; it indicates that the
35897c478bd9Sstevel@tonic-gate 		 * operation should not be done because the source and target
35907c478bd9Sstevel@tonic-gate 		 * are the same file, but that no error should be reported.
35917c478bd9Sstevel@tonic-gate 		 */
35927c478bd9Sstevel@tonic-gate 		if (error == ESAME)
35937c478bd9Sstevel@tonic-gate 			error = 0;
35947c478bd9Sstevel@tonic-gate 		goto errout;
35957c478bd9Sstevel@tonic-gate 	}
35967c478bd9Sstevel@tonic-gate 
359754207fd2SJerry Jelinek 	if (error == 0 && tvp != NULL)
359854207fd2SJerry Jelinek 		vnevent_rename_dest(tvp, tdvp, tnm, ct);
359954207fd2SJerry Jelinek 
36007c478bd9Sstevel@tonic-gate 	/*
36017c478bd9Sstevel@tonic-gate 	 * Unlink the source.
36027c478bd9Sstevel@tonic-gate 	 * Remove the source entry.  ufs_dirremove() checks that the entry
36037c478bd9Sstevel@tonic-gate 	 * still reflects sip, and returns an error if it doesn't.
36047c478bd9Sstevel@tonic-gate 	 * If the entry has changed just forget about it.  Release
36057c478bd9Sstevel@tonic-gate 	 * the source inode.
36067c478bd9Sstevel@tonic-gate 	 */
36077c478bd9Sstevel@tonic-gate 	if ((error = ufs_dirremove(sdp, snm, sip, (struct vnode *)0,
36089b5097eeSOwen Roberts 	    DR_RENAME, cr)) == ENOENT)
36097c478bd9Sstevel@tonic-gate 		error = 0;
36107c478bd9Sstevel@tonic-gate 
361154207fd2SJerry Jelinek 	if (error == 0) {
361254207fd2SJerry Jelinek 		vnevent_rename_src(ITOV(sip), sdvp, snm, ct);
361354207fd2SJerry Jelinek 		/*
361454207fd2SJerry Jelinek 		 * Notify the target directory of the rename event
361554207fd2SJerry Jelinek 		 * if source and target directories are not the same.
361654207fd2SJerry Jelinek 		 */
361754207fd2SJerry Jelinek 		if (sdvp != tdvp)
361854207fd2SJerry Jelinek 			vnevent_rename_dest_dir(tdvp, ct);
361954207fd2SJerry Jelinek 	}
362054207fd2SJerry Jelinek 
36217c478bd9Sstevel@tonic-gate errout:
3622baa4d099Sswilcox 	if (slot.fbp)
3623baa4d099Sswilcox 		fbrelse(slot.fbp, S_OTHER);
3624baa4d099Sswilcox 
36257c478bd9Sstevel@tonic-gate 	rw_exit(&tdp->i_rwlock);
36267c478bd9Sstevel@tonic-gate 	if (sdp != tdp) {
36277c478bd9Sstevel@tonic-gate 		rw_exit(&sdp->i_rwlock);
36287c478bd9Sstevel@tonic-gate 	}
36297c478bd9Sstevel@tonic-gate 
36307c478bd9Sstevel@tonic-gate unlock:
363154207fd2SJerry Jelinek 	if (tvp != NULL)
363254207fd2SJerry Jelinek 		VN_RELE(tvp);
363354207fd2SJerry Jelinek 	if (sip != NULL)
363454207fd2SJerry Jelinek 		VN_RELE(ITOV(sip));
363554207fd2SJerry Jelinek 
36367c478bd9Sstevel@tonic-gate 	if (ulp) {
36377c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME, trans_size);
36387c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
36397c478bd9Sstevel@tonic-gate 	}
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate 	return (error);
36427c478bd9Sstevel@tonic-gate }
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
36457c478bd9Sstevel@tonic-gate static int
ufs_mkdir(struct vnode * dvp,char * dirname,struct vattr * vap,struct vnode ** vpp,struct cred * cr,caller_context_t * ct,int flags,vsecattr_t * vsecp)36467c478bd9Sstevel@tonic-gate ufs_mkdir(struct vnode *dvp, char *dirname, struct vattr *vap,
364780d5689fSPatrick Mooney     struct vnode **vpp, struct cred *cr, caller_context_t *ct, int flags,
364880d5689fSPatrick Mooney     vsecattr_t *vsecp)
36497c478bd9Sstevel@tonic-gate {
36507c478bd9Sstevel@tonic-gate 	struct inode *ip;
36517c478bd9Sstevel@tonic-gate 	struct inode *xip;
36527c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
36537c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
36547c478bd9Sstevel@tonic-gate 	int error;
36557c478bd9Sstevel@tonic-gate 	int issync;
36567c478bd9Sstevel@tonic-gate 	int trans_size;
365702ff05a9Svsakar 	int indeadlock;
36587c478bd9Sstevel@tonic-gate 	int retry = 1;
36597c478bd9Sstevel@tonic-gate 
36607c478bd9Sstevel@tonic-gate 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	/*
36637c478bd9Sstevel@tonic-gate 	 * Can't make directory in attr hidden dir
36647c478bd9Sstevel@tonic-gate 	 */
36657c478bd9Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
36667c478bd9Sstevel@tonic-gate 		return (EINVAL);
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate again:
36697c478bd9Sstevel@tonic-gate 	ip = VTOI(dvp);
36707c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
36717c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MKDIR_MASK);
36727c478bd9Sstevel@tonic-gate 	if (error)
36737c478bd9Sstevel@tonic-gate 		goto out;
36747c478bd9Sstevel@tonic-gate 	if (ulp)
36757c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_MKDIR,
36767c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_MKDIR_SIZE(ip));
36777c478bd9Sstevel@tonic-gate 
367802ff05a9Svsakar 	/*
367902ff05a9Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
368002ff05a9Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
368102ff05a9Svsakar 	 * possible, retries the operation.
368202ff05a9Svsakar 	 */
368302ff05a9Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_MKDIR, retry);
368402ff05a9Svsakar 	if (indeadlock)
368502ff05a9Svsakar 		goto again;
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 	error = ufs_direnter_cm(ip, dirname, DE_MKDIR, vap, &xip, cr,
368880d34432Sfrankho 	    (retry ? IQUIET : 0));
368902ff05a9Svsakar 	if (error == EAGAIN) {
369002ff05a9Svsakar 		if (ulp) {
369102ff05a9Svsakar 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_MKDIR,
369280d34432Sfrankho 			    trans_size);
369302ff05a9Svsakar 			ufs_lockfs_end(ulp);
369402ff05a9Svsakar 		}
369502ff05a9Svsakar 		goto again;
369602ff05a9Svsakar 	}
36977c478bd9Sstevel@tonic-gate 
36987c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
36997c478bd9Sstevel@tonic-gate 	if (error == 0) {
37007c478bd9Sstevel@tonic-gate 		ip = xip;
37017c478bd9Sstevel@tonic-gate 		*vpp = ITOV(ip);
37027c478bd9Sstevel@tonic-gate 	} else if (error == EEXIST)
37037c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(xip));
37047c478bd9Sstevel@tonic-gate 
37057c478bd9Sstevel@tonic-gate 	if (ulp) {
37067c478bd9Sstevel@tonic-gate 		int terr = 0;
37077c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_MKDIR, trans_size);
37087c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
37097c478bd9Sstevel@tonic-gate 		if (error == 0)
37107c478bd9Sstevel@tonic-gate 			error = terr;
37117c478bd9Sstevel@tonic-gate 	}
37127c478bd9Sstevel@tonic-gate out:
37137c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
37147c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
37157c478bd9Sstevel@tonic-gate 		retry = 0;
37167c478bd9Sstevel@tonic-gate 		goto again;
37177c478bd9Sstevel@tonic-gate 	}
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	return (error);
37207c478bd9Sstevel@tonic-gate }
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate /*ARGSUSED*/
37237c478bd9Sstevel@tonic-gate static int
ufs_rmdir(struct vnode * vp,char * nm,struct vnode * cdir,struct cred * cr,caller_context_t * ct,int flags)3724da6c28aaSamw ufs_rmdir(struct vnode *vp, char *nm, struct vnode *cdir, struct cred *cr,
372580d5689fSPatrick Mooney     caller_context_t *ct, int flags)
37267c478bd9Sstevel@tonic-gate {
37277c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
37287c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
37297c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
37307c478bd9Sstevel@tonic-gate 	vnode_t *rmvp = NULL;	/* Vnode of removed directory */
37317c478bd9Sstevel@tonic-gate 	int error;
37327c478bd9Sstevel@tonic-gate 	int issync;
373302ff05a9Svsakar 	int trans_size;
373402ff05a9Svsakar 	int indeadlock;
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 	/*
37377c478bd9Sstevel@tonic-gate 	 * don't let the delete queue get too long
37387c478bd9Sstevel@tonic-gate 	 */
37397c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
37407c478bd9Sstevel@tonic-gate 		error = EIO;
37417c478bd9Sstevel@tonic-gate 		goto out;
37427c478bd9Sstevel@tonic-gate 	}
37437c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
37447c478bd9Sstevel@tonic-gate 		ufs_delete_drain(vp->v_vfsp, 1, 1);
37457c478bd9Sstevel@tonic-gate 
37469b5097eeSOwen Roberts 	error = ufs_eventlookup(vp, nm, cr, &rmvp);
37479b5097eeSOwen Roberts 	if (rmvp != NULL) {
37489b5097eeSOwen Roberts 		/* Only send the event if there were no errors */
37499b5097eeSOwen Roberts 		if (error == 0)
37509b5097eeSOwen Roberts 			vnevent_rmdir(rmvp, vp, nm, ct);
37519b5097eeSOwen Roberts 		VN_RELE(rmvp);
37529b5097eeSOwen Roberts 	}
37539b5097eeSOwen Roberts 
375402ff05a9Svsakar retry_rmdir:
37557c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RMDIR_MASK);
37567c478bd9Sstevel@tonic-gate 	if (error)
37577c478bd9Sstevel@tonic-gate 		goto out;
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	if (ulp)
376002ff05a9Svsakar 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RMDIR,
376180d34432Sfrankho 		    trans_size = TOP_RMDIR_SIZE);
37627c478bd9Sstevel@tonic-gate 
376302ff05a9Svsakar 	/*
376402ff05a9Svsakar 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
376502ff05a9Svsakar 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
376602ff05a9Svsakar 	 * possible, retries the operation.
376702ff05a9Svsakar 	 */
376802ff05a9Svsakar 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_RMDIR, retry);
376902ff05a9Svsakar 	if (indeadlock)
377002ff05a9Svsakar 		goto retry_rmdir;
37719b5097eeSOwen Roberts 	error = ufs_dirremove(ip, nm, (struct inode *)0, cdir, DR_RMDIR, cr);
37729b5097eeSOwen Roberts 
37737c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate 	if (ulp) {
37767c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RMDIR,
377780d34432Sfrankho 		    trans_size);
37787c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
37797c478bd9Sstevel@tonic-gate 	}
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate out:
37827c478bd9Sstevel@tonic-gate 	return (error);
37837c478bd9Sstevel@tonic-gate }
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate /* ARGSUSED */
37867c478bd9Sstevel@tonic-gate static int
ufs_readdir(struct vnode * vp,struct uio * uiop,struct cred * cr,int * eofp,caller_context_t * ct,int flags)378780d5689fSPatrick Mooney ufs_readdir(struct vnode *vp, struct uio *uiop, struct cred *cr, int *eofp,
378880d5689fSPatrick Mooney     caller_context_t *ct, int flags)
37897c478bd9Sstevel@tonic-gate {
37907c478bd9Sstevel@tonic-gate 	struct iovec *iovp;
37917c478bd9Sstevel@tonic-gate 	struct inode *ip;
37927c478bd9Sstevel@tonic-gate 	struct direct *idp;
37937c478bd9Sstevel@tonic-gate 	struct dirent64 *odp;
37947c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
37957c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
37967c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
37977c478bd9Sstevel@tonic-gate 	caddr_t outbuf;
37987c478bd9Sstevel@tonic-gate 	size_t bufsize;
37997c478bd9Sstevel@tonic-gate 	uint_t offset;
38007c478bd9Sstevel@tonic-gate 	uint_t bytes_wanted, total_bytes_wanted;
38017c478bd9Sstevel@tonic-gate 	int incount = 0;
38027c478bd9Sstevel@tonic-gate 	int outcount = 0;
38037c478bd9Sstevel@tonic-gate 	int error;
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	ip = VTOI(vp);
38067c478bd9Sstevel@tonic-gate 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
38077c478bd9Sstevel@tonic-gate 
38087c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset >= MAXOFF32_T) {
38097c478bd9Sstevel@tonic-gate 		if (eofp)
38107c478bd9Sstevel@tonic-gate 			*eofp = 1;
38117c478bd9Sstevel@tonic-gate 		return (0);
38127c478bd9Sstevel@tonic-gate 	}
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate 	/*
38157c478bd9Sstevel@tonic-gate 	 * Check if we have been called with a valid iov_len
38167c478bd9Sstevel@tonic-gate 	 * and bail out if not, otherwise we may potentially loop
38177c478bd9Sstevel@tonic-gate 	 * forever further down.
38187c478bd9Sstevel@tonic-gate 	 */
38197c478bd9Sstevel@tonic-gate 	if (uiop->uio_iov->iov_len <= 0) {
38207c478bd9Sstevel@tonic-gate 		error = EINVAL;
38217c478bd9Sstevel@tonic-gate 		goto out;
38227c478bd9Sstevel@tonic-gate 	}
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	/*
38257c478bd9Sstevel@tonic-gate 	 * Large Files: When we come here we are guaranteed that
38267c478bd9Sstevel@tonic-gate 	 * uio_offset can be used safely. The high word is zero.
38277c478bd9Sstevel@tonic-gate 	 */
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
38307c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READDIR_MASK);
38317c478bd9Sstevel@tonic-gate 	if (error)
38327c478bd9Sstevel@tonic-gate 		goto out;
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	iovp = uiop->uio_iov;
38357c478bd9Sstevel@tonic-gate 	total_bytes_wanted = iovp->iov_len;
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 	/* Large Files: directory files should not be "large" */
38387c478bd9Sstevel@tonic-gate 
38397c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_size <= MAXOFF32_T);
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate 	/* Force offset to be valid (to guard against bogus lseek() values) */
38427c478bd9Sstevel@tonic-gate 	offset = (uint_t)uiop->uio_offset & ~(DIRBLKSIZ - 1);
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	/* Quit if at end of file or link count of zero (posix) */
38457c478bd9Sstevel@tonic-gate 	if (offset >= (uint_t)ip->i_size || ip->i_nlink <= 0) {
38467c478bd9Sstevel@tonic-gate 		if (eofp)
38477c478bd9Sstevel@tonic-gate 			*eofp = 1;
38487c478bd9Sstevel@tonic-gate 		error = 0;
38497c478bd9Sstevel@tonic-gate 		goto unlock;
38507c478bd9Sstevel@tonic-gate 	}
38517c478bd9Sstevel@tonic-gate 
38527c478bd9Sstevel@tonic-gate 	/*
38537c478bd9Sstevel@tonic-gate 	 * Get space to change directory entries into fs independent format.
38547c478bd9Sstevel@tonic-gate 	 * Do fast alloc for the most commonly used-request size (filesystem
38557c478bd9Sstevel@tonic-gate 	 * block size).
38567c478bd9Sstevel@tonic-gate 	 */
38577c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1) {
38587c478bd9Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
38597c478bd9Sstevel@tonic-gate 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
38607c478bd9Sstevel@tonic-gate 		odp = (struct dirent64 *)outbuf;
38617c478bd9Sstevel@tonic-gate 	} else {
38627c478bd9Sstevel@tonic-gate 		bufsize = total_bytes_wanted;
38637c478bd9Sstevel@tonic-gate 		odp = (struct dirent64 *)iovp->iov_base;
38647c478bd9Sstevel@tonic-gate 	}
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate nextblk:
38677c478bd9Sstevel@tonic-gate 	bytes_wanted = total_bytes_wanted;
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 	/* Truncate request to file size */
38707c478bd9Sstevel@tonic-gate 	if (offset + bytes_wanted > (int)ip->i_size)
38717c478bd9Sstevel@tonic-gate 		bytes_wanted = (int)(ip->i_size - offset);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	/* Comply with MAXBSIZE boundary restrictions of fbread() */
38747c478bd9Sstevel@tonic-gate 	if ((offset & MAXBOFFSET) + bytes_wanted > MAXBSIZE)
38757c478bd9Sstevel@tonic-gate 		bytes_wanted = MAXBSIZE - (offset & MAXBOFFSET);
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 	/*
38787c478bd9Sstevel@tonic-gate 	 * Read in the next chunk.
38797c478bd9Sstevel@tonic-gate 	 * We are still holding the i_rwlock.
38807c478bd9Sstevel@tonic-gate 	 */
38817c478bd9Sstevel@tonic-gate 	error = fbread(vp, (offset_t)offset, bytes_wanted, S_OTHER, &fbp);
38827c478bd9Sstevel@tonic-gate 
38837c478bd9Sstevel@tonic-gate 	if (error)
38847c478bd9Sstevel@tonic-gate 		goto update_inode;
38857c478bd9Sstevel@tonic-gate 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (ip->i_fs->fs_ronly == 0) &&
38867c478bd9Sstevel@tonic-gate 	    (!ufsvfsp->vfs_noatime)) {
38877c478bd9Sstevel@tonic-gate 		ip->i_flag |= IACC;
38887c478bd9Sstevel@tonic-gate 	}
38897c478bd9Sstevel@tonic-gate 	incount = 0;
38907c478bd9Sstevel@tonic-gate 	idp = (struct direct *)fbp->fb_addr;
389180d34432Sfrankho 	if (idp->d_ino == 0 && idp->d_reclen == 0 && idp->d_namlen == 0) {
38927c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "ufs_readdir: bad dir, inumber = %llu, "
389380d34432Sfrankho 		    "fs = %s\n",
389480d34432Sfrankho 		    (u_longlong_t)ip->i_number, ufsvfsp->vfs_fs->fs_fsmnt);
38957c478bd9Sstevel@tonic-gate 		fbrelse(fbp, S_OTHER);
38967c478bd9Sstevel@tonic-gate 		error = ENXIO;
38977c478bd9Sstevel@tonic-gate 		goto update_inode;
38987c478bd9Sstevel@tonic-gate 	}
38997c478bd9Sstevel@tonic-gate 	/* Transform to file-system independent format */
39007c478bd9Sstevel@tonic-gate 	while (incount < bytes_wanted) {
39017c478bd9Sstevel@tonic-gate 		/*
39027c478bd9Sstevel@tonic-gate 		 * If the current directory entry is mangled, then skip
39037c478bd9Sstevel@tonic-gate 		 * to the next block.  It would be nice to set the FSBAD
39047c478bd9Sstevel@tonic-gate 		 * flag in the super-block so that a fsck is forced on
39057c478bd9Sstevel@tonic-gate 		 * next reboot, but locking is a problem.
39067c478bd9Sstevel@tonic-gate 		 */
39077c478bd9Sstevel@tonic-gate 		if (idp->d_reclen & 0x3) {
39087c478bd9Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
39097c478bd9Sstevel@tonic-gate 			break;
39107c478bd9Sstevel@tonic-gate 		}
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 		/* Skip to requested offset and skip empty entries */
39137c478bd9Sstevel@tonic-gate 		if (idp->d_ino != 0 && offset >= (uint_t)uiop->uio_offset) {
39147c478bd9Sstevel@tonic-gate 			ushort_t this_reclen =
39157c478bd9Sstevel@tonic-gate 			    DIRENT64_RECLEN(idp->d_namlen);
39167c478bd9Sstevel@tonic-gate 			/* Buffer too small for any entries */
39177c478bd9Sstevel@tonic-gate 			if (!outcount && this_reclen > bufsize) {
39187c478bd9Sstevel@tonic-gate 				fbrelse(fbp, S_OTHER);
39197c478bd9Sstevel@tonic-gate 				error = EINVAL;
39207c478bd9Sstevel@tonic-gate 				goto update_inode;
39217c478bd9Sstevel@tonic-gate 			}
39227c478bd9Sstevel@tonic-gate 			/* If would overrun the buffer, quit */
39237c478bd9Sstevel@tonic-gate 			if (outcount + this_reclen > bufsize) {
39247c478bd9Sstevel@tonic-gate 				break;
39257c478bd9Sstevel@tonic-gate 			}
39267c478bd9Sstevel@tonic-gate 			/* Take this entry */
39277c478bd9Sstevel@tonic-gate 			odp->d_ino = (ino64_t)idp->d_ino;
39287c478bd9Sstevel@tonic-gate 			odp->d_reclen = (ushort_t)this_reclen;
39297c478bd9Sstevel@tonic-gate 			odp->d_off = (offset_t)(offset + idp->d_reclen);
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 			/* use strncpy(9f) to zero out uninitialized bytes */
39327c478bd9Sstevel@tonic-gate 
39337c478bd9Sstevel@tonic-gate 			ASSERT(strlen(idp->d_name) + 1 <=
39347c478bd9Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
39357c478bd9Sstevel@tonic-gate 			(void) strncpy(odp->d_name, idp->d_name,
39367c478bd9Sstevel@tonic-gate 			    DIRENT64_NAMELEN(this_reclen));
39377c478bd9Sstevel@tonic-gate 			outcount += odp->d_reclen;
393880d34432Sfrankho 			odp = (struct dirent64 *)
393980d34432Sfrankho 			    ((intptr_t)odp + odp->d_reclen);
39407c478bd9Sstevel@tonic-gate 			ASSERT(outcount <= bufsize);
39417c478bd9Sstevel@tonic-gate 		}
39427c478bd9Sstevel@tonic-gate 		if (idp->d_reclen) {
39437c478bd9Sstevel@tonic-gate 			incount += idp->d_reclen;
39447c478bd9Sstevel@tonic-gate 			offset += idp->d_reclen;
39457c478bd9Sstevel@tonic-gate 			idp = (struct direct *)((intptr_t)idp + idp->d_reclen);
39467c478bd9Sstevel@tonic-gate 		} else {
39477c478bd9Sstevel@tonic-gate 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
39487c478bd9Sstevel@tonic-gate 			break;
39497c478bd9Sstevel@tonic-gate 		}
39507c478bd9Sstevel@tonic-gate 	}
39517c478bd9Sstevel@tonic-gate 	/* Release the chunk */
39527c478bd9Sstevel@tonic-gate 	fbrelse(fbp, S_OTHER);
39537c478bd9Sstevel@tonic-gate 
39547c478bd9Sstevel@tonic-gate 	/* Read whole block, but got no entries, read another if not eof */
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	/*
39577c478bd9Sstevel@tonic-gate 	 * Large Files: casting i_size to int here is not a problem
39587c478bd9Sstevel@tonic-gate 	 * because directory sizes are always less than MAXOFF32_T.
39597c478bd9Sstevel@tonic-gate 	 * See assertion above.
39607c478bd9Sstevel@tonic-gate 	 */
39617c478bd9Sstevel@tonic-gate 
39627c478bd9Sstevel@tonic-gate 	if (offset < (int)ip->i_size && !outcount)
39637c478bd9Sstevel@tonic-gate 		goto nextblk;
39647c478bd9Sstevel@tonic-gate 
39657c478bd9Sstevel@tonic-gate 	/* Copy out the entry data */
39667c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg == UIO_SYSSPACE && uiop->uio_iovcnt == 1) {
39677c478bd9Sstevel@tonic-gate 		iovp->iov_base += outcount;
39687c478bd9Sstevel@tonic-gate 		iovp->iov_len -= outcount;
39697c478bd9Sstevel@tonic-gate 		uiop->uio_resid -= outcount;
39707c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
39717c478bd9Sstevel@tonic-gate 	} else if ((error = uiomove(outbuf, (long)outcount, UIO_READ,
397280d34432Sfrankho 	    uiop)) == 0)
39737c478bd9Sstevel@tonic-gate 		uiop->uio_offset = offset;
39747c478bd9Sstevel@tonic-gate update_inode:
39757c478bd9Sstevel@tonic-gate 	ITIMES(ip);
39767c478bd9Sstevel@tonic-gate 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1)
39777c478bd9Sstevel@tonic-gate 		kmem_free(outbuf, bufsize);
39787c478bd9Sstevel@tonic-gate 
39797c478bd9Sstevel@tonic-gate 	if (eofp && error == 0)
39807c478bd9Sstevel@tonic-gate 		*eofp = (uiop->uio_offset >= (int)ip->i_size);
39817c478bd9Sstevel@tonic-gate unlock:
39827c478bd9Sstevel@tonic-gate 	if (ulp) {
39837c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
39847c478bd9Sstevel@tonic-gate 	}
39857c478bd9Sstevel@tonic-gate out:
39867c478bd9Sstevel@tonic-gate 	return (error);
39877c478bd9Sstevel@tonic-gate }
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
39907c478bd9Sstevel@tonic-gate static int
ufs_symlink(struct vnode * dvp,char * linkname,struct vattr * vap,char * target,struct cred * cr,caller_context_t * ct,int flags)399180d5689fSPatrick Mooney ufs_symlink(struct vnode *dvp, char *linkname, struct vattr *vap, char *target,
399280d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct, int flags)
39937c478bd9Sstevel@tonic-gate {
39947c478bd9Sstevel@tonic-gate 	struct inode *ip, *dip = VTOI(dvp);
39957c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = dip->i_ufsvfs;
39967c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
39977c478bd9Sstevel@tonic-gate 	int error;
39987c478bd9Sstevel@tonic-gate 	int issync;
39997c478bd9Sstevel@tonic-gate 	int trans_size;
40007c478bd9Sstevel@tonic-gate 	int residual;
40017c478bd9Sstevel@tonic-gate 	int ioflag;
40027c478bd9Sstevel@tonic-gate 	int retry = 1;
40037c478bd9Sstevel@tonic-gate 
40047c478bd9Sstevel@tonic-gate 	/*
40057c478bd9Sstevel@tonic-gate 	 * No symlinks in attrdirs at this time
40067c478bd9Sstevel@tonic-gate 	 */
40077c478bd9Sstevel@tonic-gate 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
40087c478bd9Sstevel@tonic-gate 		return (EINVAL);
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate again:
40117c478bd9Sstevel@tonic-gate 	ip = (struct inode *)NULL;
40127c478bd9Sstevel@tonic-gate 	vap->va_type = VLNK;
40137c478bd9Sstevel@tonic-gate 	vap->va_rdev = 0;
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SYMLINK_MASK);
40167c478bd9Sstevel@tonic-gate 	if (error)
40177c478bd9Sstevel@tonic-gate 		goto out;
40187c478bd9Sstevel@tonic-gate 
40197c478bd9Sstevel@tonic-gate 	if (ulp)
40207c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SYMLINK,
40217c478bd9Sstevel@tonic-gate 		    trans_size = (int)TOP_SYMLINK_SIZE(dip));
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	/*
40247c478bd9Sstevel@tonic-gate 	 * We must create the inode before the directory entry, to avoid
40257c478bd9Sstevel@tonic-gate 	 * racing with readlink().  ufs_dirmakeinode requires that we
40267c478bd9Sstevel@tonic-gate 	 * hold the quota lock as reader, and directory locks as writer.
40277c478bd9Sstevel@tonic-gate 	 */
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
40307c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
40317c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_contents, RW_WRITER);
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	/*
40347c478bd9Sstevel@tonic-gate 	 * Suppress any out of inodes messages if we will retry on
40357c478bd9Sstevel@tonic-gate 	 * ENOSP
40367c478bd9Sstevel@tonic-gate 	 */
40377c478bd9Sstevel@tonic-gate 	if (retry)
40387c478bd9Sstevel@tonic-gate 		dip->i_flag |= IQUIET;
40397c478bd9Sstevel@tonic-gate 
40407c478bd9Sstevel@tonic-gate 	error = ufs_dirmakeinode(dip, &ip, vap, DE_SYMLINK, cr);
40417c478bd9Sstevel@tonic-gate 
40427c478bd9Sstevel@tonic-gate 	dip->i_flag &= ~IQUIET;
40437c478bd9Sstevel@tonic-gate 
40447c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_contents);
40457c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
40467c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
40477c478bd9Sstevel@tonic-gate 
40487c478bd9Sstevel@tonic-gate 	if (error)
40497c478bd9Sstevel@tonic-gate 		goto unlock;
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 	/*
40527c478bd9Sstevel@tonic-gate 	 * OK.  The inode has been created.  Write out the data of the
40537c478bd9Sstevel@tonic-gate 	 * symbolic link.  Since symbolic links are metadata, and should
40547c478bd9Sstevel@tonic-gate 	 * remain consistent across a system crash, we need to force the
40557c478bd9Sstevel@tonic-gate 	 * data out synchronously.
40567c478bd9Sstevel@tonic-gate 	 *
40577c478bd9Sstevel@tonic-gate 	 * (This is a change from the semantics in earlier releases, which
40587c478bd9Sstevel@tonic-gate 	 * only created symbolic links synchronously if the semi-documented
40597c478bd9Sstevel@tonic-gate 	 * 'syncdir' option was set, or if we were being invoked by the NFS
40607c478bd9Sstevel@tonic-gate 	 * server, which requires symbolic links to be created synchronously.)
40617c478bd9Sstevel@tonic-gate 	 *
40627c478bd9Sstevel@tonic-gate 	 * We need to pass in a pointer for the residual length; otherwise
40637c478bd9Sstevel@tonic-gate 	 * ufs_rdwri() will always return EIO if it can't write the data,
40647c478bd9Sstevel@tonic-gate 	 * even if the error was really ENOSPC or EDQUOT.
40657c478bd9Sstevel@tonic-gate 	 */
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate 	ioflag = FWRITE | FDSYNC;
40687c478bd9Sstevel@tonic-gate 	residual = 0;
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
40717c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
40727c478bd9Sstevel@tonic-gate 
40737c478bd9Sstevel@tonic-gate 	/*
40747c478bd9Sstevel@tonic-gate 	 * Suppress file system full messages if we will retry
40757c478bd9Sstevel@tonic-gate 	 */
40767c478bd9Sstevel@tonic-gate 	if (retry)
40777c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
40787c478bd9Sstevel@tonic-gate 
40797c478bd9Sstevel@tonic-gate 	error = ufs_rdwri(UIO_WRITE, ioflag, ip, target, strlen(target),
40807c478bd9Sstevel@tonic-gate 	    (offset_t)0, UIO_SYSSPACE, &residual, cr);
40817c478bd9Sstevel@tonic-gate 
40827c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
40837c478bd9Sstevel@tonic-gate 
40847c478bd9Sstevel@tonic-gate 	if (error) {
40857c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
40867c478bd9Sstevel@tonic-gate 		rw_exit(&ufsvfsp->vfs_dqrwlock);
40877c478bd9Sstevel@tonic-gate 		goto remove;
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	/*
40917c478bd9Sstevel@tonic-gate 	 * If the link's data is small enough, we can cache it in the inode.
40927c478bd9Sstevel@tonic-gate 	 * This is a "fast symbolic link".  We don't use the first direct
40937c478bd9Sstevel@tonic-gate 	 * block because that's actually used to point at the symbolic link's
40947c478bd9Sstevel@tonic-gate 	 * contents on disk; but we know that none of the other direct or
40957c478bd9Sstevel@tonic-gate 	 * indirect blocks can be used because symbolic links are restricted
40967c478bd9Sstevel@tonic-gate 	 * to be smaller than a file system block.
40977c478bd9Sstevel@tonic-gate 	 */
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate 	ASSERT(MAXPATHLEN <= VBSIZE(ITOV(ip)));
41007c478bd9Sstevel@tonic-gate 
41017c478bd9Sstevel@tonic-gate 	if (ip->i_size > 0 && ip->i_size <= FSL_SIZE) {
41027c478bd9Sstevel@tonic-gate 		if (kcopy(target, &ip->i_db[1], ip->i_size) == 0) {
41037c478bd9Sstevel@tonic-gate 			ip->i_flag |= IFASTSYMLNK;
41047c478bd9Sstevel@tonic-gate 		} else {
41057c478bd9Sstevel@tonic-gate 			int i;
41067c478bd9Sstevel@tonic-gate 			/* error, clear garbage left behind */
41077c478bd9Sstevel@tonic-gate 			for (i = 1; i < NDADDR; i++)
41087c478bd9Sstevel@tonic-gate 				ip->i_db[i] = 0;
41097c478bd9Sstevel@tonic-gate 			for (i = 0; i < NIADDR; i++)
41107c478bd9Sstevel@tonic-gate 				ip->i_ib[i] = 0;
41117c478bd9Sstevel@tonic-gate 		}
41127c478bd9Sstevel@tonic-gate 	}
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
41157c478bd9Sstevel@tonic-gate 	rw_exit(&ufsvfsp->vfs_dqrwlock);
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	/*
41187c478bd9Sstevel@tonic-gate 	 * OK.  We've successfully created the symbolic link.  All that
41197c478bd9Sstevel@tonic-gate 	 * remains is to insert it into the appropriate directory.
41207c478bd9Sstevel@tonic-gate 	 */
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate 	rw_enter(&dip->i_rwlock, RW_WRITER);
41239b5097eeSOwen Roberts 	error = ufs_direnter_lr(dip, linkname, DE_SYMLINK, NULL, ip, cr);
41247c478bd9Sstevel@tonic-gate 	rw_exit(&dip->i_rwlock);
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 	/*
41277c478bd9Sstevel@tonic-gate 	 * Fall through into remove-on-error code.  We're either done, or we
41287c478bd9Sstevel@tonic-gate 	 * need to remove the inode (if we couldn't insert it).
41297c478bd9Sstevel@tonic-gate 	 */
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate remove:
41327c478bd9Sstevel@tonic-gate 	if (error && (ip != NULL)) {
41337c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_WRITER);
41347c478bd9Sstevel@tonic-gate 		ip->i_nlink--;
41357c478bd9Sstevel@tonic-gate 		ip->i_flag |= ICHG;
41367c478bd9Sstevel@tonic-gate 		ip->i_seq++;
41377c478bd9Sstevel@tonic-gate 		ufs_setreclaim(ip);
41387c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
41397c478bd9Sstevel@tonic-gate 	}
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate unlock:
41427c478bd9Sstevel@tonic-gate 	if (ip != NULL)
41437c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
41447c478bd9Sstevel@tonic-gate 
41457c478bd9Sstevel@tonic-gate 	if (ulp) {
41467c478bd9Sstevel@tonic-gate 		int terr = 0;
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SYMLINK,
414980d34432Sfrankho 		    trans_size);
41507c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
41517c478bd9Sstevel@tonic-gate 		if (error == 0)
41527c478bd9Sstevel@tonic-gate 			error = terr;
41537c478bd9Sstevel@tonic-gate 	}
41547c478bd9Sstevel@tonic-gate 
41557c478bd9Sstevel@tonic-gate 	/*
41567c478bd9Sstevel@tonic-gate 	 * We may have failed due to lack of an inode or of a block to
41577c478bd9Sstevel@tonic-gate 	 * store the target in.  Try flushing the delete queue to free
41587c478bd9Sstevel@tonic-gate 	 * logically-available things up and try again.
41597c478bd9Sstevel@tonic-gate 	 */
41607c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
41617c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
41627c478bd9Sstevel@tonic-gate 		retry = 0;
41637c478bd9Sstevel@tonic-gate 		goto again;
41647c478bd9Sstevel@tonic-gate 	}
41657c478bd9Sstevel@tonic-gate 
41667c478bd9Sstevel@tonic-gate out:
41677c478bd9Sstevel@tonic-gate 	return (error);
41687c478bd9Sstevel@tonic-gate }
41697c478bd9Sstevel@tonic-gate 
41707c478bd9Sstevel@tonic-gate /*
41717c478bd9Sstevel@tonic-gate  * Ufs specific routine used to do ufs io.
41727c478bd9Sstevel@tonic-gate  */
41737c478bd9Sstevel@tonic-gate int
ufs_rdwri(enum uio_rw rw,int ioflag,struct inode * ip,caddr_t base,ssize_t len,offset_t offset,enum uio_seg seg,int * aresid,struct cred * cr)41747c478bd9Sstevel@tonic-gate ufs_rdwri(enum uio_rw rw, int ioflag, struct inode *ip, caddr_t base,
417580d5689fSPatrick Mooney     ssize_t len, offset_t offset, enum uio_seg seg, int *aresid,
417680d5689fSPatrick Mooney     struct cred *cr)
41777c478bd9Sstevel@tonic-gate {
41787c478bd9Sstevel@tonic-gate 	struct uio auio;
41797c478bd9Sstevel@tonic-gate 	struct iovec aiov;
41807c478bd9Sstevel@tonic-gate 	int error;
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
41837c478bd9Sstevel@tonic-gate 
41847c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&auio, sizeof (uio_t));
41857c478bd9Sstevel@tonic-gate 	bzero((caddr_t)&aiov, sizeof (iovec_t));
41867c478bd9Sstevel@tonic-gate 
41877c478bd9Sstevel@tonic-gate 	aiov.iov_base = base;
41887c478bd9Sstevel@tonic-gate 	aiov.iov_len = len;
41897c478bd9Sstevel@tonic-gate 	auio.uio_iov = &aiov;
41907c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
41917c478bd9Sstevel@tonic-gate 	auio.uio_loffset = offset;
41927c478bd9Sstevel@tonic-gate 	auio.uio_segflg = (short)seg;
41937c478bd9Sstevel@tonic-gate 	auio.uio_resid = len;
41947c478bd9Sstevel@tonic-gate 
41957c478bd9Sstevel@tonic-gate 	if (rw == UIO_WRITE) {
41967c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FWRITE;
41977c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_DEFAULT;
41987c478bd9Sstevel@tonic-gate 		auio.uio_llimit = curproc->p_fsz_ctl;
41997c478bd9Sstevel@tonic-gate 		error = wrip(ip, &auio, ioflag, cr);
42007c478bd9Sstevel@tonic-gate 	} else {
42017c478bd9Sstevel@tonic-gate 		auio.uio_fmode = FREAD;
42027c478bd9Sstevel@tonic-gate 		auio.uio_extflg = UIO_COPY_CACHED;
42037c478bd9Sstevel@tonic-gate 		auio.uio_llimit = MAXOFFSET_T;
42047c478bd9Sstevel@tonic-gate 		error = rdip(ip, &auio, ioflag, cr);
42057c478bd9Sstevel@tonic-gate 	}
42067c478bd9Sstevel@tonic-gate 
42077c478bd9Sstevel@tonic-gate 	if (aresid) {
42087c478bd9Sstevel@tonic-gate 		*aresid = auio.uio_resid;
42097c478bd9Sstevel@tonic-gate 	} else if (auio.uio_resid) {
42107c478bd9Sstevel@tonic-gate 		error = EIO;
42117c478bd9Sstevel@tonic-gate 	}
42127c478bd9Sstevel@tonic-gate 	return (error);
42137c478bd9Sstevel@tonic-gate }
42147c478bd9Sstevel@tonic-gate 
4215da6c28aaSamw /*ARGSUSED*/
42167c478bd9Sstevel@tonic-gate static int
ufs_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)4217da6c28aaSamw ufs_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
42187c478bd9Sstevel@tonic-gate {
42197c478bd9Sstevel@tonic-gate 	struct ufid *ufid;
42207c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
42237c478bd9Sstevel@tonic-gate 		return (EIO);
42247c478bd9Sstevel@tonic-gate 
42257c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct ufid) - sizeof (ushort_t))) {
42267c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct ufid) - sizeof (ushort_t);
42277c478bd9Sstevel@tonic-gate 		return (ENOSPC);
42287c478bd9Sstevel@tonic-gate 	}
42297c478bd9Sstevel@tonic-gate 
42307c478bd9Sstevel@tonic-gate 	ufid = (struct ufid *)fidp;
42317c478bd9Sstevel@tonic-gate 	bzero((char *)ufid, sizeof (struct ufid));
42327c478bd9Sstevel@tonic-gate 	ufid->ufid_len = sizeof (struct ufid) - sizeof (ushort_t);
42337c478bd9Sstevel@tonic-gate 	ufid->ufid_ino = ip->i_number;
42347c478bd9Sstevel@tonic-gate 	ufid->ufid_gen = ip->i_gen;
42357c478bd9Sstevel@tonic-gate 
42367c478bd9Sstevel@tonic-gate 	return (0);
42377c478bd9Sstevel@tonic-gate }
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
42407c478bd9Sstevel@tonic-gate static int
ufs_rwlock(struct vnode * vp,int write_lock,caller_context_t * ctp)42417c478bd9Sstevel@tonic-gate ufs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
42427c478bd9Sstevel@tonic-gate {
42437c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
42447c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
42457c478bd9Sstevel@tonic-gate 	int		forcedirectio;
42467c478bd9Sstevel@tonic-gate 
42477c478bd9Sstevel@tonic-gate 	/*
42487c478bd9Sstevel@tonic-gate 	 * Read case is easy.
42497c478bd9Sstevel@tonic-gate 	 */
42507c478bd9Sstevel@tonic-gate 	if (!write_lock) {
42517c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
42527c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_FALSE);
42537c478bd9Sstevel@tonic-gate 	}
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 	/*
42567c478bd9Sstevel@tonic-gate 	 * Caller has requested a writer lock, but that inhibits any
42577c478bd9Sstevel@tonic-gate 	 * concurrency in the VOPs that follow. Acquire the lock shared
42587c478bd9Sstevel@tonic-gate 	 * and defer exclusive access until it is known to be needed in
42597c478bd9Sstevel@tonic-gate 	 * other VOP handlers. Some cases can be determined here.
42607c478bd9Sstevel@tonic-gate 	 */
42617c478bd9Sstevel@tonic-gate 
42627c478bd9Sstevel@tonic-gate 	/*
42637c478bd9Sstevel@tonic-gate 	 * If directio is not set, there is no chance of concurrency,
42647c478bd9Sstevel@tonic-gate 	 * so just acquire the lock exclusive. Beware of a forced
42657c478bd9Sstevel@tonic-gate 	 * unmount before looking at the mount option.
42667c478bd9Sstevel@tonic-gate 	 */
42677c478bd9Sstevel@tonic-gate 	ufsvfsp = ip->i_ufsvfs;
42687c478bd9Sstevel@tonic-gate 	forcedirectio = ufsvfsp ? ufsvfsp->vfs_forcedirectio : 0;
42697c478bd9Sstevel@tonic-gate 	if (!(ip->i_flag & IDIRECTIO || forcedirectio) ||
42707c478bd9Sstevel@tonic-gate 	    !ufs_allow_shared_writes) {
42717c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
42727c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
42737c478bd9Sstevel@tonic-gate 	}
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	/*
42767c478bd9Sstevel@tonic-gate 	 * Mandatory locking forces acquiring i_rwlock exclusive.
42777c478bd9Sstevel@tonic-gate 	 */
42787c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
42797c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
42807c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
42817c478bd9Sstevel@tonic-gate 	}
42827c478bd9Sstevel@tonic-gate 
42837c478bd9Sstevel@tonic-gate 	/*
42847c478bd9Sstevel@tonic-gate 	 * Acquire the lock shared in case a concurrent write follows.
42857c478bd9Sstevel@tonic-gate 	 * Mandatory locking could have become enabled before the lock
42867c478bd9Sstevel@tonic-gate 	 * was acquired. Re-check and upgrade if needed.
42877c478bd9Sstevel@tonic-gate 	 */
42887c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_rwlock, RW_READER);
42897c478bd9Sstevel@tonic-gate 	if (MANDLOCK(vp, ip->i_mode)) {
42907c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
42917c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
42927c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_TRUE);
42937c478bd9Sstevel@tonic-gate 	}
42947c478bd9Sstevel@tonic-gate 	return (V_WRITELOCK_FALSE);
42957c478bd9Sstevel@tonic-gate }
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
42987c478bd9Sstevel@tonic-gate static void
ufs_rwunlock(struct vnode * vp,int write_lock,caller_context_t * ctp)42997c478bd9Sstevel@tonic-gate ufs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
43007c478bd9Sstevel@tonic-gate {
43017c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
43027c478bd9Sstevel@tonic-gate 
43037c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_rwlock);
43047c478bd9Sstevel@tonic-gate }
43057c478bd9Sstevel@tonic-gate 
43067c478bd9Sstevel@tonic-gate /* ARGSUSED */
43077c478bd9Sstevel@tonic-gate static int
ufs_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)430880d5689fSPatrick Mooney ufs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
43097c478bd9Sstevel@tonic-gate {
43107c478bd9Sstevel@tonic-gate 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
43117c478bd9Sstevel@tonic-gate }
43127c478bd9Sstevel@tonic-gate 
43137c478bd9Sstevel@tonic-gate /* ARGSUSED */
43147c478bd9Sstevel@tonic-gate static int
ufs_frlock(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,struct cred * cr,caller_context_t * ct)43157c478bd9Sstevel@tonic-gate ufs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
431680d5689fSPatrick Mooney     offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
431780d5689fSPatrick Mooney     caller_context_t *ct)
43187c478bd9Sstevel@tonic-gate {
43197c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
43227c478bd9Sstevel@tonic-gate 		return (EIO);
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	/*
43257c478bd9Sstevel@tonic-gate 	 * If file is being mapped, disallow frlock.
43267c478bd9Sstevel@tonic-gate 	 * XXX I am not holding tlock while checking i_mapcnt because the
43277c478bd9Sstevel@tonic-gate 	 * current locking strategy drops all locks before calling fs_frlock.
43287c478bd9Sstevel@tonic-gate 	 * So, mapcnt could change before we enter fs_frlock making is
43297c478bd9Sstevel@tonic-gate 	 * meaningless to have held tlock in the first place.
43307c478bd9Sstevel@tonic-gate 	 */
43317c478bd9Sstevel@tonic-gate 	if (ip->i_mapcnt > 0 && MANDLOCK(vp, ip->i_mode))
43327c478bd9Sstevel@tonic-gate 		return (EAGAIN);
4333da6c28aaSamw 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
43347c478bd9Sstevel@tonic-gate }
43357c478bd9Sstevel@tonic-gate 
43367c478bd9Sstevel@tonic-gate /* ARGSUSED */
43377c478bd9Sstevel@tonic-gate static int
ufs_space(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,cred_t * cr,caller_context_t * ct)4338303bf60bSsdebnath ufs_space(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
433980d5689fSPatrick Mooney     offset_t offset, cred_t *cr, caller_context_t *ct)
43407c478bd9Sstevel@tonic-gate {
4341303bf60bSsdebnath 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
43427c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
43437c478bd9Sstevel@tonic-gate 	int error;
43447c478bd9Sstevel@tonic-gate 
4345303bf60bSsdebnath 	if ((error = convoff(vp, bfp, 0, offset)) == 0) {
4346303bf60bSsdebnath 		if (cmd == F_FREESP) {
4347303bf60bSsdebnath 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4348303bf60bSsdebnath 			    ULOCKFS_SPACE_MASK);
4349303bf60bSsdebnath 			if (error)
4350303bf60bSsdebnath 				return (error);
4351303bf60bSsdebnath 			error = ufs_freesp(vp, bfp, flag, cr);
435272102e74SBryan Cantrill 
435372102e74SBryan Cantrill 			if (error == 0 && bfp->l_start == 0)
435472102e74SBryan Cantrill 				vnevent_truncate(vp, ct);
4355303bf60bSsdebnath 		} else if (cmd == F_ALLOCSP) {
4356303bf60bSsdebnath 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4357303bf60bSsdebnath 			    ULOCKFS_FALLOCATE_MASK);
4358303bf60bSsdebnath 			if (error)
4359303bf60bSsdebnath 				return (error);
4360303bf60bSsdebnath 			error = ufs_allocsp(vp, bfp, cr);
4361303bf60bSsdebnath 		} else
4362303bf60bSsdebnath 			return (EINVAL); /* Command not handled here */
43637c478bd9Sstevel@tonic-gate 
4364303bf60bSsdebnath 		if (ulp)
4365303bf60bSsdebnath 			ufs_lockfs_end(ulp);
43667c478bd9Sstevel@tonic-gate 
4367303bf60bSsdebnath 	}
43687c478bd9Sstevel@tonic-gate 	return (error);
43697c478bd9Sstevel@tonic-gate }
43707c478bd9Sstevel@tonic-gate 
43717c478bd9Sstevel@tonic-gate /*
43727c478bd9Sstevel@tonic-gate  * Used to determine if read ahead should be done. Also used to
43737c478bd9Sstevel@tonic-gate  * to determine when write back occurs.
43747c478bd9Sstevel@tonic-gate  */
43757c478bd9Sstevel@tonic-gate #define	CLUSTSZ(ip)		((ip)->i_ufsvfs->vfs_ioclustsz)
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate /*
43787c478bd9Sstevel@tonic-gate  * A faster version of ufs_getpage.
43797c478bd9Sstevel@tonic-gate  *
43807c478bd9Sstevel@tonic-gate  * We optimize by inlining the pvn_getpages iterator, eliminating
43817c478bd9Sstevel@tonic-gate  * calls to bmap_read if file doesn't have UFS holes, and avoiding
43827c478bd9Sstevel@tonic-gate  * the overhead of page_exists().
43837c478bd9Sstevel@tonic-gate  *
43847c478bd9Sstevel@tonic-gate  * When files has UFS_HOLES and ufs_getpage is called with S_READ,
43857c478bd9Sstevel@tonic-gate  * we set *protp to PROT_READ to avoid calling bmap_read. This approach
43867c478bd9Sstevel@tonic-gate  * victimizes performance when a file with UFS holes is faulted
43877c478bd9Sstevel@tonic-gate  * first in the S_READ mode, and then in the S_WRITE mode. We will get
43887c478bd9Sstevel@tonic-gate  * two MMU faults in this case.
43897c478bd9Sstevel@tonic-gate  *
43907c478bd9Sstevel@tonic-gate  * XXX - the inode fields which control the sequential mode are not
43917c478bd9Sstevel@tonic-gate  *	 protected by any mutex. The read ahead will act wild if
43927c478bd9Sstevel@tonic-gate  *	 multiple processes will access the file concurrently and
43937c478bd9Sstevel@tonic-gate  *	 some of them in sequential mode. One particulary bad case
43947c478bd9Sstevel@tonic-gate  *	 is if another thread will change the value of i_nextrio between
43957c478bd9Sstevel@tonic-gate  *	 the time this thread tests the i_nextrio value and then reads it
43967c478bd9Sstevel@tonic-gate  *	 again to use it as the offset for the read ahead.
43977c478bd9Sstevel@tonic-gate  */
4398da6c28aaSamw /*ARGSUSED*/
43997c478bd9Sstevel@tonic-gate static int
ufs_getpage(struct vnode * vp,offset_t off,size_t len,uint_t * protp,page_t * plarr[],size_t plsz,struct seg * seg,caddr_t addr,enum seg_rw rw,struct cred * cr,caller_context_t * ct)44007c478bd9Sstevel@tonic-gate ufs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
440180d5689fSPatrick Mooney     page_t *plarr[], size_t plsz, struct seg *seg, caddr_t addr,
440280d5689fSPatrick Mooney     enum seg_rw rw, struct cred *cr, caller_context_t *ct)
44037c478bd9Sstevel@tonic-gate {
44047c478bd9Sstevel@tonic-gate 	u_offset_t	uoff = (u_offset_t)off; /* type conversion */
44057c478bd9Sstevel@tonic-gate 	u_offset_t	pgoff;
44067c478bd9Sstevel@tonic-gate 	u_offset_t	eoff;
44072575b441SToomas Soome 	struct inode	*ip = VTOI(vp);
44087c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
44092575b441SToomas Soome 	struct fs	*fs;
44107c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
44117c478bd9Sstevel@tonic-gate 	page_t		**pl;
44127c478bd9Sstevel@tonic-gate 	caddr_t		pgaddr;
44137c478bd9Sstevel@tonic-gate 	krw_t		rwtype;
44142575b441SToomas Soome 	int		err;
44157c478bd9Sstevel@tonic-gate 	int		has_holes;
44167c478bd9Sstevel@tonic-gate 	int		beyond_eof;
44177c478bd9Sstevel@tonic-gate 	int		seqmode;
44187c478bd9Sstevel@tonic-gate 	int		pgsize = PAGESIZE;
44197c478bd9Sstevel@tonic-gate 	int		dolock;
44207c478bd9Sstevel@tonic-gate 	int		do_qlock;
44217c478bd9Sstevel@tonic-gate 	int		trans_size;
44227c478bd9Sstevel@tonic-gate 
44237c478bd9Sstevel@tonic-gate 	ASSERT((uoff & PAGEOFFSET) == 0);
44247c478bd9Sstevel@tonic-gate 
44257c478bd9Sstevel@tonic-gate 	if (protp)
44267c478bd9Sstevel@tonic-gate 		*protp = PROT_ALL;
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 	/*
44297c478bd9Sstevel@tonic-gate 	 * Obey the lockfs protocol
44307c478bd9Sstevel@tonic-gate 	 */
44317c478bd9Sstevel@tonic-gate 	err = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, seg,
443280d34432Sfrankho 	    rw == S_READ || rw == S_EXEC, protp);
44337c478bd9Sstevel@tonic-gate 	if (err)
44347c478bd9Sstevel@tonic-gate 		goto out;
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	fs = ufsvfsp->vfs_fs;
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	if (ulp && (rw == S_CREATE || rw == S_WRITE) &&
44397c478bd9Sstevel@tonic-gate 	    !(vp->v_flag & VISSWAP)) {
44407c478bd9Sstevel@tonic-gate 		/*
44417c478bd9Sstevel@tonic-gate 		 * Try to start a transaction, will return if blocking is
44427c478bd9Sstevel@tonic-gate 		 * expected to occur and the address space is not the
44437c478bd9Sstevel@tonic-gate 		 * kernel address space.
44447c478bd9Sstevel@tonic-gate 		 */
44457c478bd9Sstevel@tonic-gate 		trans_size = TOP_GETPAGE_SIZE(ip);
44467c478bd9Sstevel@tonic-gate 		if (seg->s_as != &kas) {
44477c478bd9Sstevel@tonic-gate 			TRANS_TRY_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE,
444880d34432Sfrankho 			    trans_size, err)
44497c478bd9Sstevel@tonic-gate 			if (err == EWOULDBLOCK) {
44507c478bd9Sstevel@tonic-gate 				/*
44517c478bd9Sstevel@tonic-gate 				 * Use EDEADLK here because the VM code
44527c478bd9Sstevel@tonic-gate 				 * can normally never see this error.
44537c478bd9Sstevel@tonic-gate 				 */
44547c478bd9Sstevel@tonic-gate 				err = EDEADLK;
44557c478bd9Sstevel@tonic-gate 				ufs_lockfs_end(ulp);
44567c478bd9Sstevel@tonic-gate 				goto out;
44577c478bd9Sstevel@tonic-gate 			}
44587c478bd9Sstevel@tonic-gate 		} else {
44597c478bd9Sstevel@tonic-gate 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
44607c478bd9Sstevel@tonic-gate 		}
44617c478bd9Sstevel@tonic-gate 	}
44627c478bd9Sstevel@tonic-gate 
44637c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
44647c478bd9Sstevel@tonic-gate 		err = ENOSYS;
44657c478bd9Sstevel@tonic-gate 		goto unlock;
44667c478bd9Sstevel@tonic-gate 	}
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate 	seqmode = ip->i_nextr == uoff && rw != S_CREATE;
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	rwtype = RW_READER;		/* start as a reader */
44717c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
44727c478bd9Sstevel@tonic-gate 	/*
44737c478bd9Sstevel@tonic-gate 	 * If this thread owns the lock, i.e., this thread grabbed it
44747c478bd9Sstevel@tonic-gate 	 * as writer somewhere above, then we don't need to grab the
44757c478bd9Sstevel@tonic-gate 	 * lock as reader in this routine.
44767c478bd9Sstevel@tonic-gate 	 */
44777c478bd9Sstevel@tonic-gate 	do_qlock = (rw_owner(&ufsvfsp->vfs_dqrwlock) != curthread);
44787c478bd9Sstevel@tonic-gate 
44797c478bd9Sstevel@tonic-gate retrylock:
44807c478bd9Sstevel@tonic-gate 	if (dolock) {
44817c478bd9Sstevel@tonic-gate 		/*
44827c478bd9Sstevel@tonic-gate 		 * Grab the quota lock if we need to call
44837c478bd9Sstevel@tonic-gate 		 * bmap_write() below (with i_contents as writer).
44847c478bd9Sstevel@tonic-gate 		 */
44857c478bd9Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
44867c478bd9Sstevel@tonic-gate 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
44877c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, rwtype);
44887c478bd9Sstevel@tonic-gate 	}
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	/*
44917c478bd9Sstevel@tonic-gate 	 * We may be getting called as a side effect of a bmap using
44927c478bd9Sstevel@tonic-gate 	 * fbread() when the blocks might be being allocated and the
44937c478bd9Sstevel@tonic-gate 	 * size has not yet been up'ed.  In this case we want to be
44947c478bd9Sstevel@tonic-gate 	 * able to return zero pages if we get back UFS_HOLE from
44957c478bd9Sstevel@tonic-gate 	 * calling bmap for a non write case here.  We also might have
44967c478bd9Sstevel@tonic-gate 	 * to read some frags from the disk into a page if we are
44977c478bd9Sstevel@tonic-gate 	 * extending the number of frags for a given lbn in bmap().
44987c478bd9Sstevel@tonic-gate 	 * Large Files: The read of i_size here is atomic because
44997c478bd9Sstevel@tonic-gate 	 * i_contents is held here. If dolock is zero, the lock
45007c478bd9Sstevel@tonic-gate 	 * is held in bmap routines.
45017c478bd9Sstevel@tonic-gate 	 */
4502120107ddSmc 	beyond_eof = uoff + len >
4503bcfec3c7Smc 	    P2ROUNDUP_TYPED(ip->i_size, PAGESIZE, u_offset_t);
45047c478bd9Sstevel@tonic-gate 	if (beyond_eof && seg != segkmap) {
45057c478bd9Sstevel@tonic-gate 		if (dolock) {
45067c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
45077c478bd9Sstevel@tonic-gate 			if (do_qlock && rwtype == RW_WRITER)
45087c478bd9Sstevel@tonic-gate 				rw_exit(&ufsvfsp->vfs_dqrwlock);
45097c478bd9Sstevel@tonic-gate 		}
45107c478bd9Sstevel@tonic-gate 		err = EFAULT;
45117c478bd9Sstevel@tonic-gate 		goto unlock;
45127c478bd9Sstevel@tonic-gate 	}
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	/*
45157c478bd9Sstevel@tonic-gate 	 * Must hold i_contents lock throughout the call to pvn_getpages
45167c478bd9Sstevel@tonic-gate 	 * since locked pages are returned from each call to ufs_getapage.
45177c478bd9Sstevel@tonic-gate 	 * Must *not* return locked pages and then try for contents lock
45187c478bd9Sstevel@tonic-gate 	 * due to lock ordering requirements (inode > page)
45197c478bd9Sstevel@tonic-gate 	 */
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate 	has_holes = bmap_has_holes(ip);
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 	if ((rw == S_WRITE || rw == S_CREATE) && has_holes && !beyond_eof) {
45247c478bd9Sstevel@tonic-gate 		int	blk_size;
45257c478bd9Sstevel@tonic-gate 		u_offset_t offset;
45267c478bd9Sstevel@tonic-gate 
45277c478bd9Sstevel@tonic-gate 		/*
45287c478bd9Sstevel@tonic-gate 		 * We must acquire the RW_WRITER lock in order to
45297c478bd9Sstevel@tonic-gate 		 * call bmap_write().
45307c478bd9Sstevel@tonic-gate 		 */
45317c478bd9Sstevel@tonic-gate 		if (dolock && rwtype == RW_READER) {
45327c478bd9Sstevel@tonic-gate 			rwtype = RW_WRITER;
45337c478bd9Sstevel@tonic-gate 
45347c478bd9Sstevel@tonic-gate 			/*
45357c478bd9Sstevel@tonic-gate 			 * Grab the quota lock before
45367c478bd9Sstevel@tonic-gate 			 * upgrading i_contents, but if we can't grab it
45377c478bd9Sstevel@tonic-gate 			 * don't wait here due to lock order:
45387c478bd9Sstevel@tonic-gate 			 * vfs_dqrwlock > i_contents.
45397c478bd9Sstevel@tonic-gate 			 */
454080d34432Sfrankho 			if (do_qlock &&
454180d34432Sfrankho 			    rw_tryenter(&ufsvfsp->vfs_dqrwlock, RW_READER)
454280d34432Sfrankho 			    == 0) {
45437c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
45447c478bd9Sstevel@tonic-gate 				goto retrylock;
45457c478bd9Sstevel@tonic-gate 			}
45467c478bd9Sstevel@tonic-gate 			if (!rw_tryupgrade(&ip->i_contents)) {
45477c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
45487c478bd9Sstevel@tonic-gate 				if (do_qlock)
45497c478bd9Sstevel@tonic-gate 					rw_exit(&ufsvfsp->vfs_dqrwlock);
45507c478bd9Sstevel@tonic-gate 				goto retrylock;
45517c478bd9Sstevel@tonic-gate 			}
45527c478bd9Sstevel@tonic-gate 		}
45537c478bd9Sstevel@tonic-gate 
45547c478bd9Sstevel@tonic-gate 		/*
45557c478bd9Sstevel@tonic-gate 		 * May be allocating disk blocks for holes here as
45567c478bd9Sstevel@tonic-gate 		 * a result of mmap faults. write(2) does the bmap_write
45577c478bd9Sstevel@tonic-gate 		 * in rdip/wrip, not here. We are not dealing with frags
45587c478bd9Sstevel@tonic-gate 		 * in this case.
45597c478bd9Sstevel@tonic-gate 		 */
45607c478bd9Sstevel@tonic-gate 		/*
45617c478bd9Sstevel@tonic-gate 		 * Large Files: We cast fs_bmask field to offset_t
45627c478bd9Sstevel@tonic-gate 		 * just as we do for MAXBMASK because uoff is a 64-bit
45637c478bd9Sstevel@tonic-gate 		 * data type. fs_bmask will still be a 32-bit type
45647c478bd9Sstevel@tonic-gate 		 * as we cannot change any ondisk data structures.
45657c478bd9Sstevel@tonic-gate 		 */
45667c478bd9Sstevel@tonic-gate 
45677c478bd9Sstevel@tonic-gate 		offset = uoff & (offset_t)fs->fs_bmask;
45687c478bd9Sstevel@tonic-gate 		while (offset < uoff + len) {
45697c478bd9Sstevel@tonic-gate 			blk_size = (int)blksize(fs, ip, lblkno(fs, offset));
4570303bf60bSsdebnath 			err = bmap_write(ip, offset, blk_size,
4571303bf60bSsdebnath 			    BI_NORMAL, NULL, cr);
45727c478bd9Sstevel@tonic-gate 			if (ip->i_flag & (ICHG|IUPD))
45737c478bd9Sstevel@tonic-gate 				ip->i_seq++;
45747c478bd9Sstevel@tonic-gate 			if (err)
45757c478bd9Sstevel@tonic-gate 				goto update_inode;
45767c478bd9Sstevel@tonic-gate 			offset += blk_size; /* XXX - make this contig */
45777c478bd9Sstevel@tonic-gate 		}
45787c478bd9Sstevel@tonic-gate 	}
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate 	/*
45817c478bd9Sstevel@tonic-gate 	 * Can be a reader from now on.
45827c478bd9Sstevel@tonic-gate 	 */
45837c478bd9Sstevel@tonic-gate 	if (dolock && rwtype == RW_WRITER) {
45847c478bd9Sstevel@tonic-gate 		rw_downgrade(&ip->i_contents);
45857c478bd9Sstevel@tonic-gate 		/*
45867c478bd9Sstevel@tonic-gate 		 * We can release vfs_dqrwlock early so do it, but make
45877c478bd9Sstevel@tonic-gate 		 * sure we don't try to release it again at the bottom.
45887c478bd9Sstevel@tonic-gate 		 */
45897c478bd9Sstevel@tonic-gate 		if (do_qlock) {
45907c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
45917c478bd9Sstevel@tonic-gate 			do_qlock = 0;
45927c478bd9Sstevel@tonic-gate 		}
45937c478bd9Sstevel@tonic-gate 	}
45947c478bd9Sstevel@tonic-gate 
45957c478bd9Sstevel@tonic-gate 	/*
45967c478bd9Sstevel@tonic-gate 	 * We remove PROT_WRITE in cases when the file has UFS holes
45977c478bd9Sstevel@tonic-gate 	 * because we don't  want to call bmap_read() to check each
45987c478bd9Sstevel@tonic-gate 	 * page if it is backed with a disk block.
45997c478bd9Sstevel@tonic-gate 	 */
46007c478bd9Sstevel@tonic-gate 	if (protp && has_holes && rw != S_WRITE && rw != S_CREATE)
46017c478bd9Sstevel@tonic-gate 		*protp &= ~PROT_WRITE;
46027c478bd9Sstevel@tonic-gate 
46037c478bd9Sstevel@tonic-gate 	err = 0;
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 	/*
46067c478bd9Sstevel@tonic-gate 	 * The loop looks up pages in the range [off, off + len).
46077c478bd9Sstevel@tonic-gate 	 * For each page, we first check if we should initiate an asynchronous
46087c478bd9Sstevel@tonic-gate 	 * read ahead before we call page_lookup (we may sleep in page_lookup
46097c478bd9Sstevel@tonic-gate 	 * for a previously initiated disk read).
46107c478bd9Sstevel@tonic-gate 	 */
46117c478bd9Sstevel@tonic-gate 	eoff = (uoff + len);
46127c478bd9Sstevel@tonic-gate 	for (pgoff = uoff, pgaddr = addr, pl = plarr;
46137c478bd9Sstevel@tonic-gate 	    pgoff < eoff; /* empty */) {
46147c478bd9Sstevel@tonic-gate 		page_t	*pp;
46157c478bd9Sstevel@tonic-gate 		u_offset_t	nextrio;
46167c478bd9Sstevel@tonic-gate 		se_t	se;
46177c478bd9Sstevel@tonic-gate 		int retval;
46187c478bd9Sstevel@tonic-gate 
46197c478bd9Sstevel@tonic-gate 		se = ((rw == S_CREATE || rw == S_OTHER) ? SE_EXCL : SE_SHARED);
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate 		/* Handle async getpage (faultahead) */
46227c478bd9Sstevel@tonic-gate 		if (plarr == NULL) {
46237c478bd9Sstevel@tonic-gate 			ip->i_nextrio = pgoff;
46247c478bd9Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, pgoff, seg, pgaddr);
46257c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
46267c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
46277c478bd9Sstevel@tonic-gate 			continue;
46287c478bd9Sstevel@tonic-gate 		}
46297c478bd9Sstevel@tonic-gate 		/*
46307c478bd9Sstevel@tonic-gate 		 * Check if we should initiate read ahead of next cluster.
46317c478bd9Sstevel@tonic-gate 		 * We call page_exists only when we need to confirm that
46327c478bd9Sstevel@tonic-gate 		 * we have the current page before we initiate the read ahead.
46337c478bd9Sstevel@tonic-gate 		 */
46347c478bd9Sstevel@tonic-gate 		nextrio = ip->i_nextrio;
46357c478bd9Sstevel@tonic-gate 		if (seqmode &&
46367c478bd9Sstevel@tonic-gate 		    pgoff + CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
46377c478bd9Sstevel@tonic-gate 		    nextrio < ip->i_size && page_exists(vp, pgoff)) {
46387c478bd9Sstevel@tonic-gate 			retval = ufs_getpage_ra(vp, pgoff, seg, pgaddr);
46397c478bd9Sstevel@tonic-gate 			/*
46407c478bd9Sstevel@tonic-gate 			 * We always read ahead the next cluster of data
46417c478bd9Sstevel@tonic-gate 			 * starting from i_nextrio. If the page (vp,nextrio)
46427c478bd9Sstevel@tonic-gate 			 * is actually in core at this point, the routine
46437c478bd9Sstevel@tonic-gate 			 * ufs_getpage_ra() will stop pre-fetching data
46447c478bd9Sstevel@tonic-gate 			 * until we read that page in a synchronized manner
46457c478bd9Sstevel@tonic-gate 			 * through ufs_getpage_miss(). So, we should increase
46467c478bd9Sstevel@tonic-gate 			 * i_nextrio if the page (vp, nextrio) exists.
46477c478bd9Sstevel@tonic-gate 			 */
46487c478bd9Sstevel@tonic-gate 			if ((retval == 0) && page_exists(vp, nextrio)) {
46497c478bd9Sstevel@tonic-gate 				ip->i_nextrio = nextrio + pgsize;
46507c478bd9Sstevel@tonic-gate 			}
46517c478bd9Sstevel@tonic-gate 		}
46527c478bd9Sstevel@tonic-gate 
46537c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
46547c478bd9Sstevel@tonic-gate 			/*
46557c478bd9Sstevel@tonic-gate 			 * We found the page in the page cache.
46567c478bd9Sstevel@tonic-gate 			 */
46577c478bd9Sstevel@tonic-gate 			*pl++ = pp;
46587c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
46597c478bd9Sstevel@tonic-gate 			pgaddr += pgsize;
46607c478bd9Sstevel@tonic-gate 			len -= pgsize;
46617c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
46627c478bd9Sstevel@tonic-gate 		} else  {
46637c478bd9Sstevel@tonic-gate 			/*
46647c478bd9Sstevel@tonic-gate 			 * We have to create the page, or read it from disk.
46657c478bd9Sstevel@tonic-gate 			 */
46667c478bd9Sstevel@tonic-gate 			if (err = ufs_getpage_miss(vp, pgoff, len, seg, pgaddr,
46677c478bd9Sstevel@tonic-gate 			    pl, plsz, rw, seqmode))
46687c478bd9Sstevel@tonic-gate 				goto error;
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 			while (*pl != NULL) {
46717c478bd9Sstevel@tonic-gate 				pl++;
46727c478bd9Sstevel@tonic-gate 				pgoff += pgsize;
46737c478bd9Sstevel@tonic-gate 				pgaddr += pgsize;
46747c478bd9Sstevel@tonic-gate 				len -= pgsize;
46757c478bd9Sstevel@tonic-gate 				plsz -= pgsize;
46767c478bd9Sstevel@tonic-gate 			}
46777c478bd9Sstevel@tonic-gate 		}
46787c478bd9Sstevel@tonic-gate 	}
46797c478bd9Sstevel@tonic-gate 
46807c478bd9Sstevel@tonic-gate 	/*
46817c478bd9Sstevel@tonic-gate 	 * Return pages up to plsz if they are in the page cache.
46827c478bd9Sstevel@tonic-gate 	 * We cannot return pages if there is a chance that they are
46837c478bd9Sstevel@tonic-gate 	 * backed with a UFS hole and rw is S_WRITE or S_CREATE.
46847c478bd9Sstevel@tonic-gate 	 */
46857c478bd9Sstevel@tonic-gate 	if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate 		ASSERT((protp == NULL) ||
468880d34432Sfrankho 		    !(has_holes && (*protp & PROT_WRITE)));
46897c478bd9Sstevel@tonic-gate 
46907c478bd9Sstevel@tonic-gate 		eoff = pgoff + plsz;
46917c478bd9Sstevel@tonic-gate 		while (pgoff < eoff) {
46927c478bd9Sstevel@tonic-gate 			page_t		*pp;
46937c478bd9Sstevel@tonic-gate 
46947c478bd9Sstevel@tonic-gate 			if ((pp = page_lookup_nowait(vp, pgoff,
46957c478bd9Sstevel@tonic-gate 			    SE_SHARED)) == NULL)
46967c478bd9Sstevel@tonic-gate 				break;
46977c478bd9Sstevel@tonic-gate 
46987c478bd9Sstevel@tonic-gate 			*pl++ = pp;
46997c478bd9Sstevel@tonic-gate 			pgoff += pgsize;
47007c478bd9Sstevel@tonic-gate 			plsz -= pgsize;
47017c478bd9Sstevel@tonic-gate 		}
47027c478bd9Sstevel@tonic-gate 	}
47037c478bd9Sstevel@tonic-gate 
47047c478bd9Sstevel@tonic-gate 	if (plarr)
47057c478bd9Sstevel@tonic-gate 		*pl = NULL;			/* Terminate page list */
47067c478bd9Sstevel@tonic-gate 	ip->i_nextr = pgoff;
47077c478bd9Sstevel@tonic-gate 
47087c478bd9Sstevel@tonic-gate error:
47097c478bd9Sstevel@tonic-gate 	if (err && plarr) {
47107c478bd9Sstevel@tonic-gate 		/*
47117c478bd9Sstevel@tonic-gate 		 * Release any pages we have locked.
47127c478bd9Sstevel@tonic-gate 		 */
47137c478bd9Sstevel@tonic-gate 		while (pl > &plarr[0])
47147c478bd9Sstevel@tonic-gate 			page_unlock(*--pl);
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 		plarr[0] = NULL;
47177c478bd9Sstevel@tonic-gate 	}
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate update_inode:
47207c478bd9Sstevel@tonic-gate 	/*
47217c478bd9Sstevel@tonic-gate 	 * If the inode is not already marked for IACC (in rdip() for read)
47227c478bd9Sstevel@tonic-gate 	 * and the inode is not marked for no access time update (in wrip()
47237c478bd9Sstevel@tonic-gate 	 * for write) then update the inode access time and mod time now.
47247c478bd9Sstevel@tonic-gate 	 */
47257c478bd9Sstevel@tonic-gate 	if ((ip->i_flag & (IACC | INOACC)) == 0) {
47267c478bd9Sstevel@tonic-gate 		if ((rw != S_OTHER) && (ip->i_mode & IFMT) != IFDIR) {
47277c478bd9Sstevel@tonic-gate 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
47287c478bd9Sstevel@tonic-gate 			    (fs->fs_ronly == 0) &&
47297c478bd9Sstevel@tonic-gate 			    (!ufsvfsp->vfs_noatime)) {
47307c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
47317c478bd9Sstevel@tonic-gate 				ip->i_flag |= IACC;
47327c478bd9Sstevel@tonic-gate 				ITIMES_NOLOCK(ip);
47337c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
47347c478bd9Sstevel@tonic-gate 			}
47357c478bd9Sstevel@tonic-gate 		}
47367c478bd9Sstevel@tonic-gate 	}
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 	if (dolock) {
47397c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
47407c478bd9Sstevel@tonic-gate 		if (do_qlock && rwtype == RW_WRITER)
47417c478bd9Sstevel@tonic-gate 			rw_exit(&ufsvfsp->vfs_dqrwlock);
47427c478bd9Sstevel@tonic-gate 	}
47437c478bd9Sstevel@tonic-gate 
47447c478bd9Sstevel@tonic-gate unlock:
47457c478bd9Sstevel@tonic-gate 	if (ulp) {
47467c478bd9Sstevel@tonic-gate 		if ((rw == S_CREATE || rw == S_WRITE) &&
47477c478bd9Sstevel@tonic-gate 		    !(vp->v_flag & VISSWAP)) {
47487c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
47497c478bd9Sstevel@tonic-gate 		}
47507c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
47517c478bd9Sstevel@tonic-gate 	}
47527c478bd9Sstevel@tonic-gate out:
47537c478bd9Sstevel@tonic-gate 	return (err);
47547c478bd9Sstevel@tonic-gate }
47557c478bd9Sstevel@tonic-gate 
47567c478bd9Sstevel@tonic-gate /*
47577c478bd9Sstevel@tonic-gate  * ufs_getpage_miss is called when ufs_getpage missed the page in the page
47587c478bd9Sstevel@tonic-gate  * cache. The page is either read from the disk, or it's created.
47597c478bd9Sstevel@tonic-gate  * A page is created (without disk read) if rw == S_CREATE, or if
47607c478bd9Sstevel@tonic-gate  * the page is not backed with a real disk block (UFS hole).
47617c478bd9Sstevel@tonic-gate  */
47627c478bd9Sstevel@tonic-gate /* ARGSUSED */
47637c478bd9Sstevel@tonic-gate static int
ufs_getpage_miss(struct vnode * vp,u_offset_t off,size_t len,struct seg * seg,caddr_t addr,page_t * pl[],size_t plsz,enum seg_rw rw,int seq)47647c478bd9Sstevel@tonic-gate ufs_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
476580d5689fSPatrick Mooney     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int seq)
47667c478bd9Sstevel@tonic-gate {
47677c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
47687c478bd9Sstevel@tonic-gate 	page_t		*pp;
47697c478bd9Sstevel@tonic-gate 	daddr_t		bn;
47707c478bd9Sstevel@tonic-gate 	size_t		io_len;
4771303bf60bSsdebnath 	int		crpage = 0;
47727c478bd9Sstevel@tonic-gate 	int		err;
47737c478bd9Sstevel@tonic-gate 	int		contig;
47747c478bd9Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
47757c478bd9Sstevel@tonic-gate 
47767c478bd9Sstevel@tonic-gate 	/*
47777c478bd9Sstevel@tonic-gate 	 * Figure out whether the page can be created, or must be
47787c478bd9Sstevel@tonic-gate 	 * must be read from the disk.
47797c478bd9Sstevel@tonic-gate 	 */
47807c478bd9Sstevel@tonic-gate 	if (rw == S_CREATE)
47817c478bd9Sstevel@tonic-gate 		crpage = 1;
47827c478bd9Sstevel@tonic-gate 	else {
47837c478bd9Sstevel@tonic-gate 		contig = 0;
47847c478bd9Sstevel@tonic-gate 		if (err = bmap_read(ip, off, &bn, &contig))
47857c478bd9Sstevel@tonic-gate 			return (err);
4786303bf60bSsdebnath 
47877c478bd9Sstevel@tonic-gate 		crpage = (bn == UFS_HOLE);
4788303bf60bSsdebnath 
4789303bf60bSsdebnath 		/*
4790303bf60bSsdebnath 		 * If its also a fallocated block that hasn't been written to
4791303bf60bSsdebnath 		 * yet, we will treat it just like a UFS_HOLE and create
4792303bf60bSsdebnath 		 * a zero page for it
4793303bf60bSsdebnath 		 */
4794303bf60bSsdebnath 		if (ISFALLOCBLK(ip, bn))
4795303bf60bSsdebnath 			crpage = 1;
47967c478bd9Sstevel@tonic-gate 	}
47977c478bd9Sstevel@tonic-gate 
47987c478bd9Sstevel@tonic-gate 	if (crpage) {
47997c478bd9Sstevel@tonic-gate 		if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT, seg,
48007c478bd9Sstevel@tonic-gate 		    addr)) == NULL) {
48017c478bd9Sstevel@tonic-gate 			return (ufs_fault(vp,
480280d34432Sfrankho 			    "ufs_getpage_miss: page_create == NULL"));
48037c478bd9Sstevel@tonic-gate 		}
48047c478bd9Sstevel@tonic-gate 
48057c478bd9Sstevel@tonic-gate 		if (rw != S_CREATE)
48067c478bd9Sstevel@tonic-gate 			pagezero(pp, 0, PAGESIZE);
4807303bf60bSsdebnath 
48087c478bd9Sstevel@tonic-gate 		io_len = PAGESIZE;
48097c478bd9Sstevel@tonic-gate 	} else {
48107c478bd9Sstevel@tonic-gate 		u_offset_t	io_off;
48117c478bd9Sstevel@tonic-gate 		uint_t	xlen;
48127c478bd9Sstevel@tonic-gate 		struct buf	*bp;
48137c478bd9Sstevel@tonic-gate 		ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 		/*
48167c478bd9Sstevel@tonic-gate 		 * If access is not in sequential order, we read from disk
48177c478bd9Sstevel@tonic-gate 		 * in bsize units.
48187c478bd9Sstevel@tonic-gate 		 *
48197c478bd9Sstevel@tonic-gate 		 * We limit the size of the transfer to bsize if we are reading
48207c478bd9Sstevel@tonic-gate 		 * from the beginning of the file. Note in this situation we
48217c478bd9Sstevel@tonic-gate 		 * will hedge our bets and initiate an async read ahead of
48227c478bd9Sstevel@tonic-gate 		 * the second block.
48237c478bd9Sstevel@tonic-gate 		 */
48247c478bd9Sstevel@tonic-gate 		if (!seq || off == 0)
48257c478bd9Sstevel@tonic-gate 			contig = MIN(contig, bsize);
48267c478bd9Sstevel@tonic-gate 
48277c478bd9Sstevel@tonic-gate 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
48287c478bd9Sstevel@tonic-gate 		    &io_len, off, contig, 0);
48297c478bd9Sstevel@tonic-gate 
48307c478bd9Sstevel@tonic-gate 		/*
48317c478bd9Sstevel@tonic-gate 		 * Some other thread has entered the page.
48327c478bd9Sstevel@tonic-gate 		 * ufs_getpage will retry page_lookup.
48337c478bd9Sstevel@tonic-gate 		 */
48347c478bd9Sstevel@tonic-gate 		if (pp == NULL) {
48357c478bd9Sstevel@tonic-gate 			pl[0] = NULL;
48367c478bd9Sstevel@tonic-gate 			return (0);
48377c478bd9Sstevel@tonic-gate 		}
48387c478bd9Sstevel@tonic-gate 
48397c478bd9Sstevel@tonic-gate 		/*
48407c478bd9Sstevel@tonic-gate 		 * Zero part of the page which we are not
48417c478bd9Sstevel@tonic-gate 		 * going to read from the disk.
48427c478bd9Sstevel@tonic-gate 		 */
48437c478bd9Sstevel@tonic-gate 		xlen = io_len & PAGEOFFSET;
48447c478bd9Sstevel@tonic-gate 		if (xlen != 0)
48457c478bd9Sstevel@tonic-gate 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
48467c478bd9Sstevel@tonic-gate 
48477c478bd9Sstevel@tonic-gate 		bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ);
48487c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
48497c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
48507c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
48517c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
48527c478bd9Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
48537c478bd9Sstevel@tonic-gate 		bp->b_offset = off;
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
48567c478bd9Sstevel@tonic-gate 			lufs_read_strategy(ufsvfsp->vfs_log, bp);
48577c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
48587c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
48597c478bd9Sstevel@tonic-gate 		} else {
4860d3d50737SRafael Vanoni 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
48617c478bd9Sstevel@tonic-gate 			ub.ub_getpages.value.ul++;
48627c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
48637c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
48647c478bd9Sstevel@tonic-gate 		}
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate 		ip->i_nextrio = off + ((io_len + PAGESIZE - 1) & PAGEMASK);
48677c478bd9Sstevel@tonic-gate 
48687c478bd9Sstevel@tonic-gate 		/*
48697c478bd9Sstevel@tonic-gate 		 * If the file access is sequential, initiate read ahead
48707c478bd9Sstevel@tonic-gate 		 * of the next cluster.
48717c478bd9Sstevel@tonic-gate 		 */
48727c478bd9Sstevel@tonic-gate 		if (seq && ip->i_nextrio < ip->i_size)
48737c478bd9Sstevel@tonic-gate 			(void) ufs_getpage_ra(vp, off, seg, addr);
48747c478bd9Sstevel@tonic-gate 		err = biowait(bp);
48757c478bd9Sstevel@tonic-gate 		pageio_done(bp);
48767c478bd9Sstevel@tonic-gate 
48777c478bd9Sstevel@tonic-gate 		if (err) {
48787c478bd9Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
48797c478bd9Sstevel@tonic-gate 			return (err);
48807c478bd9Sstevel@tonic-gate 		}
48817c478bd9Sstevel@tonic-gate 	}
48827c478bd9Sstevel@tonic-gate 
48837c478bd9Sstevel@tonic-gate 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
48847c478bd9Sstevel@tonic-gate 	return (0);
48857c478bd9Sstevel@tonic-gate }
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate /*
48887c478bd9Sstevel@tonic-gate  * Read ahead a cluster from the disk. Returns the length in bytes.
48897c478bd9Sstevel@tonic-gate  */
48907c478bd9Sstevel@tonic-gate static int
ufs_getpage_ra(struct vnode * vp,u_offset_t off,struct seg * seg,caddr_t addr)48917c478bd9Sstevel@tonic-gate ufs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
48927c478bd9Sstevel@tonic-gate {
48937c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
48947c478bd9Sstevel@tonic-gate 	page_t		*pp;
48957c478bd9Sstevel@tonic-gate 	u_offset_t	io_off = ip->i_nextrio;
48967c478bd9Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp;
48977c478bd9Sstevel@tonic-gate 	caddr_t		addr2 = addr + (io_off - off);
48987c478bd9Sstevel@tonic-gate 	struct buf	*bp;
48997c478bd9Sstevel@tonic-gate 	daddr_t		bn;
49007c478bd9Sstevel@tonic-gate 	size_t		io_len;
4901303bf60bSsdebnath 	int		err;
49027c478bd9Sstevel@tonic-gate 	int		contig;
49037c478bd9Sstevel@tonic-gate 	int		xlen;
49047c478bd9Sstevel@tonic-gate 	int		bsize = ip->i_fs->fs_bsize;
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate 	/*
49077c478bd9Sstevel@tonic-gate 	 * If the directio advisory is in effect on this file,
49087c478bd9Sstevel@tonic-gate 	 * then do not do buffered read ahead. Read ahead makes
49097c478bd9Sstevel@tonic-gate 	 * it more difficult on threads using directio as they
49107c478bd9Sstevel@tonic-gate 	 * will be forced to flush the pages from this vnode.
49117c478bd9Sstevel@tonic-gate 	 */
49127c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
49137c478bd9Sstevel@tonic-gate 		return (0);
49147c478bd9Sstevel@tonic-gate 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio)
49157c478bd9Sstevel@tonic-gate 		return (0);
49167c478bd9Sstevel@tonic-gate 
49177c478bd9Sstevel@tonic-gate 	/*
49187c478bd9Sstevel@tonic-gate 	 * Is this test needed?
49197c478bd9Sstevel@tonic-gate 	 */
49207c478bd9Sstevel@tonic-gate 	if (addr2 >= seg->s_base + seg->s_size)
49217c478bd9Sstevel@tonic-gate 		return (0);
49227c478bd9Sstevel@tonic-gate 
49237c478bd9Sstevel@tonic-gate 	contig = 0;
4924303bf60bSsdebnath 	err = bmap_read(ip, io_off, &bn, &contig);
4925303bf60bSsdebnath 	/*
4926303bf60bSsdebnath 	 * If its a UFS_HOLE or a fallocated block, do not perform
4927303bf60bSsdebnath 	 * any read ahead's since there probably is nothing to read ahead
4928303bf60bSsdebnath 	 */
4929303bf60bSsdebnath 	if (err || bn == UFS_HOLE || ISFALLOCBLK(ip, bn))
49307c478bd9Sstevel@tonic-gate 		return (0);
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	/*
49337c478bd9Sstevel@tonic-gate 	 * Limit the transfer size to bsize if this is the 2nd block.
49347c478bd9Sstevel@tonic-gate 	 */
49357c478bd9Sstevel@tonic-gate 	if (io_off == (u_offset_t)bsize)
49367c478bd9Sstevel@tonic-gate 		contig = MIN(contig, bsize);
49377c478bd9Sstevel@tonic-gate 
49387c478bd9Sstevel@tonic-gate 	if ((pp = pvn_read_kluster(vp, io_off, seg, addr2, &io_off,
49397c478bd9Sstevel@tonic-gate 	    &io_len, io_off, contig, 1)) == NULL)
49407c478bd9Sstevel@tonic-gate 		return (0);
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate 	/*
49437c478bd9Sstevel@tonic-gate 	 * Zero part of page which we are not going to read from disk
49447c478bd9Sstevel@tonic-gate 	 */
49457c478bd9Sstevel@tonic-gate 	if ((xlen = (io_len & PAGEOFFSET)) > 0)
49467c478bd9Sstevel@tonic-gate 		pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
49477c478bd9Sstevel@tonic-gate 
49487c478bd9Sstevel@tonic-gate 	ip->i_nextrio = (io_off + io_len + PAGESIZE - 1) & PAGEMASK;
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ | B_ASYNC);
49517c478bd9Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
49527c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
49537c478bd9Sstevel@tonic-gate 	bp->b_blkno = bn;
49547c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
49557c478bd9Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
49567c478bd9Sstevel@tonic-gate 	bp->b_offset = off;
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_log) {
49597c478bd9Sstevel@tonic-gate 		lufs_read_strategy(ufsvfsp->vfs_log, bp);
49607c478bd9Sstevel@tonic-gate 	} else if (ufsvfsp->vfs_snapshot) {
49617c478bd9Sstevel@tonic-gate 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
49627c478bd9Sstevel@tonic-gate 	} else {
4963d3d50737SRafael Vanoni 		ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
49647c478bd9Sstevel@tonic-gate 		ub.ub_getras.value.ul++;
49657c478bd9Sstevel@tonic-gate 		(void) bdev_strategy(bp);
49667c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
49677c478bd9Sstevel@tonic-gate 	}
49687c478bd9Sstevel@tonic-gate 
49697c478bd9Sstevel@tonic-gate 	return (io_len);
49707c478bd9Sstevel@tonic-gate }
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate int	ufs_delay = 1;
49737c478bd9Sstevel@tonic-gate /*
49747c478bd9Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE, B_ASYNC}
49757c478bd9Sstevel@tonic-gate  *
49767c478bd9Sstevel@tonic-gate  * LMXXX - the inode really ought to contain a pointer to one of these
49777c478bd9Sstevel@tonic-gate  * async args.  Stuff gunk in there and just hand the whole mess off.
49787c478bd9Sstevel@tonic-gate  * This would replace i_delaylen, i_delayoff.
49797c478bd9Sstevel@tonic-gate  */
49807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
49817c478bd9Sstevel@tonic-gate static int
ufs_putpage(struct vnode * vp,offset_t off,size_t len,int flags,struct cred * cr,caller_context_t * ct)49827c478bd9Sstevel@tonic-gate ufs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
498380d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct)
49847c478bd9Sstevel@tonic-gate {
49857c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
49867c478bd9Sstevel@tonic-gate 	int err = 0;
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0) {
49897c478bd9Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpage: bad v_count == 0"));
49907c478bd9Sstevel@tonic-gate 	}
49917c478bd9Sstevel@tonic-gate 
49927c478bd9Sstevel@tonic-gate 	/*
49937c478bd9Sstevel@tonic-gate 	 * XXX - Why should this check be made here?
49947c478bd9Sstevel@tonic-gate 	 */
49957c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
49967c478bd9Sstevel@tonic-gate 		err = ENOSYS;
49977c478bd9Sstevel@tonic-gate 		goto errout;
49987c478bd9Sstevel@tonic-gate 	}
49997c478bd9Sstevel@tonic-gate 
50007c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL) {
50017c478bd9Sstevel@tonic-gate 		err = EIO;
50027c478bd9Sstevel@tonic-gate 		goto errout;
50037c478bd9Sstevel@tonic-gate 	}
50047c478bd9Sstevel@tonic-gate 
50057c478bd9Sstevel@tonic-gate 	if (flags & B_ASYNC) {
50067c478bd9Sstevel@tonic-gate 		if (ufs_delay && len &&
50077c478bd9Sstevel@tonic-gate 		    (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
50087c478bd9Sstevel@tonic-gate 			mutex_enter(&ip->i_tlock);
50097c478bd9Sstevel@tonic-gate 			/*
50107c478bd9Sstevel@tonic-gate 			 * If nobody stalled, start a new cluster.
50117c478bd9Sstevel@tonic-gate 			 */
50127c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen == 0) {
50137c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
50147c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
50157c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
50167c478bd9Sstevel@tonic-gate 				goto errout;
50177c478bd9Sstevel@tonic-gate 			}
50187c478bd9Sstevel@tonic-gate 			/*
50197c478bd9Sstevel@tonic-gate 			 * If we have a full cluster or they are not contig,
50207c478bd9Sstevel@tonic-gate 			 * then push last cluster and start over.
50217c478bd9Sstevel@tonic-gate 			 */
50227c478bd9Sstevel@tonic-gate 			if (ip->i_delaylen >= CLUSTSZ(ip) ||
50237c478bd9Sstevel@tonic-gate 			    ip->i_delayoff + ip->i_delaylen != off) {
50247c478bd9Sstevel@tonic-gate 				u_offset_t doff;
50257c478bd9Sstevel@tonic-gate 				size_t dlen;
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 				doff = ip->i_delayoff;
50287c478bd9Sstevel@tonic-gate 				dlen = ip->i_delaylen;
50297c478bd9Sstevel@tonic-gate 				ip->i_delayoff = off;
50307c478bd9Sstevel@tonic-gate 				ip->i_delaylen = len;
50317c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
50327c478bd9Sstevel@tonic-gate 				err = ufs_putpages(vp, doff, dlen,
50337c478bd9Sstevel@tonic-gate 				    flags, cr);
50347c478bd9Sstevel@tonic-gate 				/* LMXXX - flags are new val, not old */
50357c478bd9Sstevel@tonic-gate 				goto errout;
50367c478bd9Sstevel@tonic-gate 			}
50377c478bd9Sstevel@tonic-gate 			/*
50387c478bd9Sstevel@tonic-gate 			 * There is something there, it's not full, and
50397c478bd9Sstevel@tonic-gate 			 * it is contig.
50407c478bd9Sstevel@tonic-gate 			 */
50417c478bd9Sstevel@tonic-gate 			ip->i_delaylen += len;
50427c478bd9Sstevel@tonic-gate 			mutex_exit(&ip->i_tlock);
50437c478bd9Sstevel@tonic-gate 			goto errout;
50447c478bd9Sstevel@tonic-gate 		}
50457c478bd9Sstevel@tonic-gate 		/*
50467c478bd9Sstevel@tonic-gate 		 * Must have weird flags or we are not clustering.
50477c478bd9Sstevel@tonic-gate 		 */
50487c478bd9Sstevel@tonic-gate 	}
50497c478bd9Sstevel@tonic-gate 
50507c478bd9Sstevel@tonic-gate 	err = ufs_putpages(vp, off, len, flags, cr);
50517c478bd9Sstevel@tonic-gate 
50527c478bd9Sstevel@tonic-gate errout:
50537c478bd9Sstevel@tonic-gate 	return (err);
50547c478bd9Sstevel@tonic-gate }
50557c478bd9Sstevel@tonic-gate 
50567c478bd9Sstevel@tonic-gate /*
50577c478bd9Sstevel@tonic-gate  * If len == 0, do from off to EOF.
50587c478bd9Sstevel@tonic-gate  *
50597c478bd9Sstevel@tonic-gate  * The normal cases should be len == 0 & off == 0 (entire vp list),
50607c478bd9Sstevel@tonic-gate  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
50617c478bd9Sstevel@tonic-gate  * (from pageout).
50627c478bd9Sstevel@tonic-gate  */
50637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
50647c478bd9Sstevel@tonic-gate static int
ufs_putpages(struct vnode * vp,offset_t off,size_t len,int flags,struct cred * cr)506580d5689fSPatrick Mooney ufs_putpages(struct vnode *vp, offset_t off, size_t len, int flags,
506680d5689fSPatrick Mooney     struct cred *cr)
50677c478bd9Sstevel@tonic-gate {
50687c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
50697c478bd9Sstevel@tonic-gate 	u_offset_t eoff;
50707c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
50717c478bd9Sstevel@tonic-gate 	page_t *pp;
50727c478bd9Sstevel@tonic-gate 	size_t io_len;
50737c478bd9Sstevel@tonic-gate 	int err = 0;
50747c478bd9Sstevel@tonic-gate 	int dolock;
50757c478bd9Sstevel@tonic-gate 
50767c478bd9Sstevel@tonic-gate 	if (vp->v_count == 0)
50777c478bd9Sstevel@tonic-gate 		return (ufs_fault(vp, "ufs_putpages: v_count == 0"));
50787c478bd9Sstevel@tonic-gate 	/*
50797c478bd9Sstevel@tonic-gate 	 * Acquire the readers/write inode lock before locking
50807c478bd9Sstevel@tonic-gate 	 * any pages in this inode.
50817c478bd9Sstevel@tonic-gate 	 * The inode lock is held during i/o.
50827c478bd9Sstevel@tonic-gate 	 */
50837c478bd9Sstevel@tonic-gate 	if (len == 0) {
50847c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
50857c478bd9Sstevel@tonic-gate 		ip->i_delayoff = ip->i_delaylen = 0;
50867c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
50877c478bd9Sstevel@tonic-gate 	}
50887c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
50897c478bd9Sstevel@tonic-gate 	if (dolock) {
50907c478bd9Sstevel@tonic-gate 		/*
50917c478bd9Sstevel@tonic-gate 		 * Must synchronize this thread and any possible thread
50927c478bd9Sstevel@tonic-gate 		 * operating in the window of vulnerability in wrip().
50937c478bd9Sstevel@tonic-gate 		 * It is dangerous to allow both a thread doing a putpage
50947c478bd9Sstevel@tonic-gate 		 * and a thread writing, so serialize them.  The exception
50957c478bd9Sstevel@tonic-gate 		 * is when the thread in wrip() does something which causes
50967c478bd9Sstevel@tonic-gate 		 * a putpage operation.  Then, the thread must be allowed
50977c478bd9Sstevel@tonic-gate 		 * to continue.  It may encounter a bmap_read problem in
50987c478bd9Sstevel@tonic-gate 		 * ufs_putapage, but that is handled in ufs_putapage.
50997c478bd9Sstevel@tonic-gate 		 * Allow async writers to proceed, we don't want to block
51007c478bd9Sstevel@tonic-gate 		 * the pageout daemon.
51017c478bd9Sstevel@tonic-gate 		 */
51027c478bd9Sstevel@tonic-gate 		if (ip->i_writer == curthread)
51037c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
51047c478bd9Sstevel@tonic-gate 		else {
51057c478bd9Sstevel@tonic-gate 			for (;;) {
51067c478bd9Sstevel@tonic-gate 				rw_enter(&ip->i_contents, RW_READER);
51077c478bd9Sstevel@tonic-gate 				mutex_enter(&ip->i_tlock);
51087c478bd9Sstevel@tonic-gate 				/*
51097c478bd9Sstevel@tonic-gate 				 * If there is no thread in the critical
51107c478bd9Sstevel@tonic-gate 				 * section of wrip(), then proceed.
51117c478bd9Sstevel@tonic-gate 				 * Otherwise, wait until there isn't one.
51127c478bd9Sstevel@tonic-gate 				 */
51137c478bd9Sstevel@tonic-gate 				if (ip->i_writer == NULL) {
51147c478bd9Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
51157c478bd9Sstevel@tonic-gate 					break;
51167c478bd9Sstevel@tonic-gate 				}
51177c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
51187c478bd9Sstevel@tonic-gate 				/*
51197c478bd9Sstevel@tonic-gate 				 * Bounce async writers when we have a writer
51207c478bd9Sstevel@tonic-gate 				 * working on this file so we don't deadlock
51217c478bd9Sstevel@tonic-gate 				 * the pageout daemon.
51227c478bd9Sstevel@tonic-gate 				 */
51237c478bd9Sstevel@tonic-gate 				if (flags & B_ASYNC) {
51247c478bd9Sstevel@tonic-gate 					mutex_exit(&ip->i_tlock);
51257c478bd9Sstevel@tonic-gate 					return (0);
51267c478bd9Sstevel@tonic-gate 				}
51277c478bd9Sstevel@tonic-gate 				cv_wait(&ip->i_wrcv, &ip->i_tlock);
51287c478bd9Sstevel@tonic-gate 				mutex_exit(&ip->i_tlock);
51297c478bd9Sstevel@tonic-gate 			}
51307c478bd9Sstevel@tonic-gate 		}
51317c478bd9Sstevel@tonic-gate 	}
51327c478bd9Sstevel@tonic-gate 
51337c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp)) {
51347c478bd9Sstevel@tonic-gate 		if (dolock)
51357c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
51367c478bd9Sstevel@tonic-gate 		return (0);
51377c478bd9Sstevel@tonic-gate 	}
51387c478bd9Sstevel@tonic-gate 
51397c478bd9Sstevel@tonic-gate 	if (len == 0) {
51407c478bd9Sstevel@tonic-gate 		/*
51417c478bd9Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off.
51427c478bd9Sstevel@tonic-gate 		 */
51437c478bd9Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, (u_offset_t)off, ufs_putapage,
514480d34432Sfrankho 		    flags, cr);
51457c478bd9Sstevel@tonic-gate 	} else {
51467c478bd9Sstevel@tonic-gate 		/*
51477c478bd9Sstevel@tonic-gate 		 * Loop over all offsets in the range looking for
51487c478bd9Sstevel@tonic-gate 		 * pages to deal with.
51497c478bd9Sstevel@tonic-gate 		 */
51507c478bd9Sstevel@tonic-gate 		if ((eoff = blkroundup(ip->i_fs, ip->i_size)) != 0)
51517c478bd9Sstevel@tonic-gate 			eoff = MIN(off + len, eoff);
51527c478bd9Sstevel@tonic-gate 		else
51537c478bd9Sstevel@tonic-gate 			eoff = off + len;
51547c478bd9Sstevel@tonic-gate 
51557c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < eoff; io_off += io_len) {
51567c478bd9Sstevel@tonic-gate 			/*
51577c478bd9Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
51587c478bd9Sstevel@tonic-gate 			 * freeing or writing pages, use the routine
51597c478bd9Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
51607c478bd9Sstevel@tonic-gate 			 * them from the free list.
51617c478bd9Sstevel@tonic-gate 			 */
51627c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
51637c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
516480d34432Sfrankho 				    (flags & (B_INVAL | B_FREE)) ?
516580d34432Sfrankho 				    SE_EXCL : SE_SHARED);
51667c478bd9Sstevel@tonic-gate 			} else {
51677c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
516880d34432Sfrankho 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
51697c478bd9Sstevel@tonic-gate 			}
51707c478bd9Sstevel@tonic-gate 
51717c478bd9Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
51727c478bd9Sstevel@tonic-gate 				io_len = PAGESIZE;
51737c478bd9Sstevel@tonic-gate 			else {
51747c478bd9Sstevel@tonic-gate 				u_offset_t *io_offp = &io_off;
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 				err = ufs_putapage(vp, pp, io_offp, &io_len,
51777c478bd9Sstevel@tonic-gate 				    flags, cr);
51787c478bd9Sstevel@tonic-gate 				if (err != 0)
51797c478bd9Sstevel@tonic-gate 					break;
51807c478bd9Sstevel@tonic-gate 				/*
51817c478bd9Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
51827c478bd9Sstevel@tonic-gate 				 * the range of pages we actually wrote.
51837c478bd9Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
51847c478bd9Sstevel@tonic-gate 				 * since several pages may've been dealt
51857c478bd9Sstevel@tonic-gate 				 * with by this iteration of the loop.
51867c478bd9Sstevel@tonic-gate 				 */
51877c478bd9Sstevel@tonic-gate 			}
51887c478bd9Sstevel@tonic-gate 		}
51897c478bd9Sstevel@tonic-gate 	}
51907c478bd9Sstevel@tonic-gate 	if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
51917c478bd9Sstevel@tonic-gate 		/*
51927c478bd9Sstevel@tonic-gate 		 * We have just sync'ed back all the pages on
51937c478bd9Sstevel@tonic-gate 		 * the inode, turn off the IMODTIME flag.
51947c478bd9Sstevel@tonic-gate 		 */
51957c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
51967c478bd9Sstevel@tonic-gate 		ip->i_flag &= ~IMODTIME;
51977c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
51987c478bd9Sstevel@tonic-gate 	}
51997c478bd9Sstevel@tonic-gate 	if (dolock)
52007c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
52017c478bd9Sstevel@tonic-gate 	return (err);
52027c478bd9Sstevel@tonic-gate }
52037c478bd9Sstevel@tonic-gate 
52042575b441SToomas Soome static int
ufs_iodone(buf_t * bp)52057c478bd9Sstevel@tonic-gate ufs_iodone(buf_t *bp)
52067c478bd9Sstevel@tonic-gate {
52077c478bd9Sstevel@tonic-gate 	struct inode *ip;
52087c478bd9Sstevel@tonic-gate 
52097c478bd9Sstevel@tonic-gate 	ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
52107c478bd9Sstevel@tonic-gate 
52117c478bd9Sstevel@tonic-gate 	bp->b_iodone = NULL;
52127c478bd9Sstevel@tonic-gate 
52137c478bd9Sstevel@tonic-gate 	ip = VTOI(bp->b_pages->p_vnode);
52147c478bd9Sstevel@tonic-gate 
52157c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
52167c478bd9Sstevel@tonic-gate 	if (ip->i_writes >= ufs_LW) {
52177c478bd9Sstevel@tonic-gate 		if ((ip->i_writes -= bp->b_bcount) <= ufs_LW)
52187c478bd9Sstevel@tonic-gate 			if (ufs_WRITES)
52197c478bd9Sstevel@tonic-gate 				cv_broadcast(&ip->i_wrcv); /* wake all up */
52207c478bd9Sstevel@tonic-gate 	} else {
52217c478bd9Sstevel@tonic-gate 		ip->i_writes -= bp->b_bcount;
52227c478bd9Sstevel@tonic-gate 	}
52237c478bd9Sstevel@tonic-gate 
52247c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
52257c478bd9Sstevel@tonic-gate 	iodone(bp);
52262575b441SToomas Soome 	return (0);
52277c478bd9Sstevel@tonic-gate }
52287c478bd9Sstevel@tonic-gate 
52297c478bd9Sstevel@tonic-gate /*
52307c478bd9Sstevel@tonic-gate  * Write out a single page, possibly klustering adjacent
52317c478bd9Sstevel@tonic-gate  * dirty pages.  The inode lock must be held.
52327c478bd9Sstevel@tonic-gate  *
52337c478bd9Sstevel@tonic-gate  * LMXXX - bsize < pagesize not done.
52347c478bd9Sstevel@tonic-gate  */
52357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
52367c478bd9Sstevel@tonic-gate int
ufs_putapage(struct vnode * vp,page_t * pp,u_offset_t * offp,size_t * lenp,int flags,struct cred * cr)523780d5689fSPatrick Mooney ufs_putapage(struct vnode *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
523880d5689fSPatrick Mooney     int flags, struct cred *cr)
52397c478bd9Sstevel@tonic-gate {
52407c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
52417c478bd9Sstevel@tonic-gate 	u_offset_t off;
52427c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
52437c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
52447c478bd9Sstevel@tonic-gate 	struct fs *fs;
52457c478bd9Sstevel@tonic-gate 	struct buf *bp;
52467c478bd9Sstevel@tonic-gate 	size_t io_len;
52477c478bd9Sstevel@tonic-gate 	daddr_t bn;
52487c478bd9Sstevel@tonic-gate 	int err;
52497c478bd9Sstevel@tonic-gate 	int contig;
5250b9dc641bSfrankho 	int dotrans;
52517c478bd9Sstevel@tonic-gate 
52527c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL) {
52557c478bd9Sstevel@tonic-gate 		err = EIO;
52567c478bd9Sstevel@tonic-gate 		goto out_trace;
52577c478bd9Sstevel@tonic-gate 	}
52587c478bd9Sstevel@tonic-gate 
52597c478bd9Sstevel@tonic-gate 	fs = ip->i_fs;
52607c478bd9Sstevel@tonic-gate 	ASSERT(fs->fs_ronly == 0);
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate 	/*
52637c478bd9Sstevel@tonic-gate 	 * If the modified time on the inode has not already been
5264956bc340Sdm 	 * set elsewhere (e.g. for write/setattr) we set the time now.
52657c478bd9Sstevel@tonic-gate 	 * This gives us approximate modified times for mmap'ed files
52667c478bd9Sstevel@tonic-gate 	 * which are modified via stores in the user address space.
52677c478bd9Sstevel@tonic-gate 	 */
5268956bc340Sdm 	if ((ip->i_flag & IMODTIME) == 0) {
52697c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
52707c478bd9Sstevel@tonic-gate 		ip->i_flag |= IUPD;
52717c478bd9Sstevel@tonic-gate 		ip->i_seq++;
52727c478bd9Sstevel@tonic-gate 		ITIMES_NOLOCK(ip);
52737c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
52747c478bd9Sstevel@tonic-gate 	}
52757c478bd9Sstevel@tonic-gate 
52767c478bd9Sstevel@tonic-gate 	/*
52777c478bd9Sstevel@tonic-gate 	 * Align the request to a block boundry (for old file systems),
52787c478bd9Sstevel@tonic-gate 	 * and go ask bmap() how contiguous things are for this file.
52797c478bd9Sstevel@tonic-gate 	 */
52807c478bd9Sstevel@tonic-gate 	off = pp->p_offset & (offset_t)fs->fs_bmask;	/* block align it */
52817c478bd9Sstevel@tonic-gate 	contig = 0;
52827c478bd9Sstevel@tonic-gate 	err = bmap_read(ip, off, &bn, &contig);
52837c478bd9Sstevel@tonic-gate 	if (err)
52847c478bd9Sstevel@tonic-gate 		goto out;
52857c478bd9Sstevel@tonic-gate 	if (bn == UFS_HOLE) {			/* putpage never allocates */
52867c478bd9Sstevel@tonic-gate 		/*
52877c478bd9Sstevel@tonic-gate 		 * logging device is in error mode; simply return EIO
52887c478bd9Sstevel@tonic-gate 		 */
52897c478bd9Sstevel@tonic-gate 		if (TRANS_ISERROR(ufsvfsp)) {
52907c478bd9Sstevel@tonic-gate 			err = EIO;
52917c478bd9Sstevel@tonic-gate 			goto out;
52927c478bd9Sstevel@tonic-gate 		}
52937c478bd9Sstevel@tonic-gate 		/*
52947c478bd9Sstevel@tonic-gate 		 * Oops, the thread in the window in wrip() did some
52957c478bd9Sstevel@tonic-gate 		 * sort of operation which caused a putpage in the bad
52967c478bd9Sstevel@tonic-gate 		 * range.  In this case, just return an error which will
52977c478bd9Sstevel@tonic-gate 		 * cause the software modified bit on the page to set
52987c478bd9Sstevel@tonic-gate 		 * and the page will get written out again later.
52997c478bd9Sstevel@tonic-gate 		 */
53007c478bd9Sstevel@tonic-gate 		if (ip->i_writer == curthread) {
53017c478bd9Sstevel@tonic-gate 			err = EIO;
53027c478bd9Sstevel@tonic-gate 			goto out;
53037c478bd9Sstevel@tonic-gate 		}
53047c478bd9Sstevel@tonic-gate 		/*
53057c478bd9Sstevel@tonic-gate 		 * If the pager is trying to push a page in the bad range
530648bbca81SDaniel Hoffman 		 * just tell it to try again later when things are better.
53077c478bd9Sstevel@tonic-gate 		 */
53087c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
53097c478bd9Sstevel@tonic-gate 			err = EAGAIN;
53107c478bd9Sstevel@tonic-gate 			goto out;
53117c478bd9Sstevel@tonic-gate 		}
53127c478bd9Sstevel@tonic-gate 		err = ufs_fault(ITOV(ip), "ufs_putapage: bn == UFS_HOLE");
53137c478bd9Sstevel@tonic-gate 		goto out;
53147c478bd9Sstevel@tonic-gate 	}
53157c478bd9Sstevel@tonic-gate 
5316303bf60bSsdebnath 	/*
5317303bf60bSsdebnath 	 * If it is an fallocate'd block, reverse the negativity since
5318303bf60bSsdebnath 	 * we are now writing to it
5319303bf60bSsdebnath 	 */
5320303bf60bSsdebnath 	if (ISFALLOCBLK(ip, bn)) {
5321303bf60bSsdebnath 		err = bmap_set_bn(vp, off, dbtofsb(fs, -bn));
5322303bf60bSsdebnath 		if (err)
5323303bf60bSsdebnath 			goto out;
5324303bf60bSsdebnath 
5325303bf60bSsdebnath 		bn = -bn;
5326303bf60bSsdebnath 	}
5327303bf60bSsdebnath 
53287c478bd9Sstevel@tonic-gate 	/*
53297c478bd9Sstevel@tonic-gate 	 * Take the length (of contiguous bytes) passed back from bmap()
53307c478bd9Sstevel@tonic-gate 	 * and _try_ and get a set of pages covering that extent.
53317c478bd9Sstevel@tonic-gate 	 */
53327c478bd9Sstevel@tonic-gate 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, off, contig, flags);
53337c478bd9Sstevel@tonic-gate 
53347c478bd9Sstevel@tonic-gate 	/*
53357c478bd9Sstevel@tonic-gate 	 * May have run out of memory and not clustered backwards.
53367c478bd9Sstevel@tonic-gate 	 * off		p_offset
53377c478bd9Sstevel@tonic-gate 	 * [  pp - 1  ][   pp   ]
53387c478bd9Sstevel@tonic-gate 	 * [	block		]
53397c478bd9Sstevel@tonic-gate 	 * We told bmap off, so we have to adjust the bn accordingly.
53407c478bd9Sstevel@tonic-gate 	 */
53417c478bd9Sstevel@tonic-gate 	if (io_off > off) {
53427c478bd9Sstevel@tonic-gate 		bn += btod(io_off - off);
53437c478bd9Sstevel@tonic-gate 		contig -= (io_off - off);
53447c478bd9Sstevel@tonic-gate 	}
53457c478bd9Sstevel@tonic-gate 
53467c478bd9Sstevel@tonic-gate 	/*
53477c478bd9Sstevel@tonic-gate 	 * bmap was carefull to tell us the right size so use that.
53487c478bd9Sstevel@tonic-gate 	 * There might be unallocated frags at the end.
53497c478bd9Sstevel@tonic-gate 	 * LMXXX - bzero the end of the page?  We must be writing after EOF.
53507c478bd9Sstevel@tonic-gate 	 */
53517c478bd9Sstevel@tonic-gate 	if (io_len > contig) {
53527c478bd9Sstevel@tonic-gate 		ASSERT(io_len - contig < fs->fs_bsize);
53537c478bd9Sstevel@tonic-gate 		io_len -= (io_len - contig);
53547c478bd9Sstevel@tonic-gate 	}
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 	/*
53577c478bd9Sstevel@tonic-gate 	 * Handle the case where we are writing the last page after EOF.
53587c478bd9Sstevel@tonic-gate 	 *
53597c478bd9Sstevel@tonic-gate 	 * XXX - just a patch for i-mt3.
53607c478bd9Sstevel@tonic-gate 	 */
53617c478bd9Sstevel@tonic-gate 	if (io_len == 0) {
536280d34432Sfrankho 		ASSERT(pp->p_offset >=
536380d34432Sfrankho 		    (u_offset_t)(roundup(ip->i_size, PAGESIZE)));
53647c478bd9Sstevel@tonic-gate 		io_len = PAGESIZE;
53657c478bd9Sstevel@tonic-gate 	}
53667c478bd9Sstevel@tonic-gate 
53677c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_WRITE | flags);
53687c478bd9Sstevel@tonic-gate 
53697c478bd9Sstevel@tonic-gate 	ULOCKFS_SET_MOD(ITOUL(ip));
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 	bp->b_edev = ip->i_dev;
53727c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(ip->i_dev);
53737c478bd9Sstevel@tonic-gate 	bp->b_blkno = bn;
53747c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
53757c478bd9Sstevel@tonic-gate 	bp->b_file = ip->i_vnode;
53767c478bd9Sstevel@tonic-gate 
5377b9dc641bSfrankho 	/*
5378b9dc641bSfrankho 	 * File contents of shadow or quota inodes are metadata, and updates
5379b9dc641bSfrankho 	 * to these need to be put into a logging transaction. All direct
5380b9dc641bSfrankho 	 * callers in UFS do that, but fsflush can come here _before_ the
5381b9dc641bSfrankho 	 * normal codepath. An example would be updating ACL information, for
5382b9dc641bSfrankho 	 * which the normal codepath would be:
5383b9dc641bSfrankho 	 *	ufs_si_store()
5384b9dc641bSfrankho 	 *	ufs_rdwri()
5385b9dc641bSfrankho 	 *	wrip()
5386b9dc641bSfrankho 	 *	segmap_release()
5387b9dc641bSfrankho 	 *	VOP_PUTPAGE()
5388b9dc641bSfrankho 	 * Here, fsflush can pick up the dirty page before segmap_release()
5389b9dc641bSfrankho 	 * forces it out. If that happens, there's no transaction.
5390b9dc641bSfrankho 	 * We therefore need to test whether a transaction exists, and if not
5391b9dc641bSfrankho 	 * create one - for fsflush.
5392b9dc641bSfrankho 	 */
5393b9dc641bSfrankho 	dotrans =
5394b9dc641bSfrankho 	    (((ip->i_mode & IFMT) == IFSHAD || ufsvfsp->vfs_qinod == ip) &&
5395b9dc641bSfrankho 	    ((curthread->t_flag & T_DONTBLOCK) == 0) &&
5396b9dc641bSfrankho 	    (TRANS_ISTRANS(ufsvfsp)));
5397b9dc641bSfrankho 
5398b9dc641bSfrankho 	if (dotrans) {
5399b9dc641bSfrankho 		curthread->t_flag |= T_DONTBLOCK;
5400b9dc641bSfrankho 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
5401b9dc641bSfrankho 	}
54027c478bd9Sstevel@tonic-gate 	if (TRANS_ISTRANS(ufsvfsp)) {
54037c478bd9Sstevel@tonic-gate 		if ((ip->i_mode & IFMT) == IFSHAD) {
54047c478bd9Sstevel@tonic-gate 			TRANS_BUF(ufsvfsp, 0, io_len, bp, DT_SHAD);
54057c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_qinod == ip) {
54067c478bd9Sstevel@tonic-gate 			TRANS_DELTA(ufsvfsp, ldbtob(bn), bp->b_bcount, DT_QR,
54077c478bd9Sstevel@tonic-gate 			    0, 0);
54087c478bd9Sstevel@tonic-gate 		}
54097c478bd9Sstevel@tonic-gate 	}
5410b9dc641bSfrankho 	if (dotrans) {
5411b9dc641bSfrankho 		TRANS_END_ASYNC(ufsvfsp, TOP_PUTPAGE, TOP_PUTPAGE_SIZE(ip));
5412b9dc641bSfrankho 		curthread->t_flag &= ~T_DONTBLOCK;
5413b9dc641bSfrankho 	}
54147c478bd9Sstevel@tonic-gate 
54157c478bd9Sstevel@tonic-gate 	/* write throttle */
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_iodone == NULL);
54182575b441SToomas Soome 	bp->b_iodone = ufs_iodone;
54197c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
54207c478bd9Sstevel@tonic-gate 	ip->i_writes += bp->b_bcount;
54217c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
54227c478bd9Sstevel@tonic-gate 
54237c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_ASYNC) {
54247c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
54257c478bd9Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
54267c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
54277c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
54287c478bd9Sstevel@tonic-gate 		} else {
5429d3d50737SRafael Vanoni 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
54307c478bd9Sstevel@tonic-gate 			ub.ub_putasyncs.value.ul++;
54317c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
54327c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
54337c478bd9Sstevel@tonic-gate 		}
54347c478bd9Sstevel@tonic-gate 	} else {
54357c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_log) {
54367c478bd9Sstevel@tonic-gate 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
54377c478bd9Sstevel@tonic-gate 		} else if (ufsvfsp->vfs_snapshot) {
54387c478bd9Sstevel@tonic-gate 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
54397c478bd9Sstevel@tonic-gate 		} else {
5440d3d50737SRafael Vanoni 			ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
54417c478bd9Sstevel@tonic-gate 			ub.ub_putsyncs.value.ul++;
54427c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
54437c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
54447c478bd9Sstevel@tonic-gate 		}
54457c478bd9Sstevel@tonic-gate 		err = biowait(bp);
54467c478bd9Sstevel@tonic-gate 		pageio_done(bp);
54477c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
54487c478bd9Sstevel@tonic-gate 	}
54497c478bd9Sstevel@tonic-gate 
54507c478bd9Sstevel@tonic-gate 	pp = NULL;
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate out:
54537c478bd9Sstevel@tonic-gate 	if (err != 0 && pp != NULL)
54547c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, B_ERROR | B_WRITE | flags);
54557c478bd9Sstevel@tonic-gate 
54567c478bd9Sstevel@tonic-gate 	if (offp)
54577c478bd9Sstevel@tonic-gate 		*offp = io_off;
54587c478bd9Sstevel@tonic-gate 	if (lenp)
54597c478bd9Sstevel@tonic-gate 		*lenp = io_len;
54607c478bd9Sstevel@tonic-gate out_trace:
54617c478bd9Sstevel@tonic-gate 	return (err);
54627c478bd9Sstevel@tonic-gate }
54637c478bd9Sstevel@tonic-gate 
54646ac3b8a8Svsakar uint64_t ufs_map_alock_retry_cnt;
54656ac3b8a8Svsakar uint64_t ufs_map_lockfs_retry_cnt;
54666ac3b8a8Svsakar 
54677c478bd9Sstevel@tonic-gate /* ARGSUSED */
54687c478bd9Sstevel@tonic-gate static int
ufs_map(struct vnode * vp,offset_t off,struct as * as,caddr_t * addrp,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)546980d5689fSPatrick Mooney ufs_map(struct vnode *vp, offset_t off, struct as *as, caddr_t *addrp,
547080d5689fSPatrick Mooney     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, struct cred *cr,
547180d5689fSPatrick Mooney     caller_context_t *ct)
54727c478bd9Sstevel@tonic-gate {
54737c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
54747c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
54757c478bd9Sstevel@tonic-gate 	struct ulockfs *ulp;
54766ac3b8a8Svsakar 	int error, sig;
54776ac3b8a8Svsakar 	k_sigset_t smask;
547857846e82Svsakar 	caddr_t hint = *addrp;
54797c478bd9Sstevel@tonic-gate 
54807c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
54817c478bd9Sstevel@tonic-gate 		error = ENOSYS;
54826ac3b8a8Svsakar 		goto out;
54837c478bd9Sstevel@tonic-gate 	}
54847c478bd9Sstevel@tonic-gate 
54857c478bd9Sstevel@tonic-gate 	if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0) {
54867c478bd9Sstevel@tonic-gate 		error = ENXIO;
54876ac3b8a8Svsakar 		goto out;
54887c478bd9Sstevel@tonic-gate 	}
54897c478bd9Sstevel@tonic-gate 
54907c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG) {
54917c478bd9Sstevel@tonic-gate 		error = ENODEV;
54926ac3b8a8Svsakar 		goto out;
54937c478bd9Sstevel@tonic-gate 	}
54947c478bd9Sstevel@tonic-gate 
54956ac3b8a8Svsakar retry_map:
549657846e82Svsakar 	*addrp = hint;
54977c478bd9Sstevel@tonic-gate 	/*
54987c478bd9Sstevel@tonic-gate 	 * If file is being locked, disallow mapping.
54997c478bd9Sstevel@tonic-gate 	 */
55007c478bd9Sstevel@tonic-gate 	if (vn_has_mandatory_locks(vp, VTOI(vp)->i_mode)) {
55017c478bd9Sstevel@tonic-gate 		error = EAGAIN;
55026ac3b8a8Svsakar 		goto out;
55037c478bd9Sstevel@tonic-gate 	}
55047c478bd9Sstevel@tonic-gate 
55057c478bd9Sstevel@tonic-gate 	as_rangelock(as);
550660946fe0Smec 	/*
550760946fe0Smec 	 * Note that if we are retrying (because ufs_lockfs_trybegin failed in
550860946fe0Smec 	 * the previous attempt), some other thread could have grabbed
550960946fe0Smec 	 * the same VA range if MAP_FIXED is set. In that case, choose_addr
551060946fe0Smec 	 * would unmap the valid VA range, that is ok.
551160946fe0Smec 	 */
551260946fe0Smec 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
551360946fe0Smec 	if (error != 0) {
551460946fe0Smec 		as_rangeunlock(as);
551560946fe0Smec 		goto out;
55167c478bd9Sstevel@tonic-gate 	}
55177c478bd9Sstevel@tonic-gate 
55186ac3b8a8Svsakar 	/*
55196ac3b8a8Svsakar 	 * a_lock has to be acquired before entering the lockfs protocol
55206ac3b8a8Svsakar 	 * because that is the order in which pagefault works. Also we cannot
55216ac3b8a8Svsakar 	 * block on a_lock here because this waiting writer will prevent
55226ac3b8a8Svsakar 	 * further readers like ufs_read from progressing and could cause
55236ac3b8a8Svsakar 	 * deadlock between ufs_read/ufs_map/pagefault when a quiesce is
55246ac3b8a8Svsakar 	 * pending.
55256ac3b8a8Svsakar 	 */
5526dc32d872SJosef 'Jeff' Sipek 	while (!AS_LOCK_TRYENTER(as, RW_WRITER)) {
55276ac3b8a8Svsakar 		ufs_map_alock_retry_cnt++;
55286ac3b8a8Svsakar 		delay(RETRY_LOCK_DELAY);
55296ac3b8a8Svsakar 	}
55306ac3b8a8Svsakar 
55316ac3b8a8Svsakar 	/*
55326ac3b8a8Svsakar 	 * We can't hold as->a_lock and wait for lockfs to succeed because
55336ac3b8a8Svsakar 	 * the proc tools might hang on a_lock, so call ufs_lockfs_trybegin()
55346ac3b8a8Svsakar 	 * instead.
55356ac3b8a8Svsakar 	 */
55366ac3b8a8Svsakar 	if (error = ufs_lockfs_trybegin(ufsvfsp, &ulp, ULOCKFS_MAP_MASK)) {
55376ac3b8a8Svsakar 		/*
55386ac3b8a8Svsakar 		 * ufs_lockfs_trybegin() did not succeed. It is safer to give up
55396ac3b8a8Svsakar 		 * as->a_lock and wait for ulp->ul_fs_lock status to change.
55406ac3b8a8Svsakar 		 */
55416ac3b8a8Svsakar 		ufs_map_lockfs_retry_cnt++;
5542dc32d872SJosef 'Jeff' Sipek 		AS_LOCK_EXIT(as);
55436ac3b8a8Svsakar 		as_rangeunlock(as);
55446ac3b8a8Svsakar 		if (error == EIO)
55456ac3b8a8Svsakar 			goto out;
55466ac3b8a8Svsakar 
55476ac3b8a8Svsakar 		mutex_enter(&ulp->ul_lock);
55486ac3b8a8Svsakar 		while (ulp->ul_fs_lock & ULOCKFS_MAP_MASK) {
55496ac3b8a8Svsakar 			if (ULOCKFS_IS_SLOCK(ulp) || ufsvfsp->vfs_nointr) {
55506ac3b8a8Svsakar 				cv_wait(&ulp->ul_cv, &ulp->ul_lock);
55516ac3b8a8Svsakar 			} else {
55526ac3b8a8Svsakar 				sigintr(&smask, 1);
55536ac3b8a8Svsakar 				sig = cv_wait_sig(&ulp->ul_cv, &ulp->ul_lock);
55546ac3b8a8Svsakar 				sigunintr(&smask);
55556ac3b8a8Svsakar 				if (((ulp->ul_fs_lock & ULOCKFS_MAP_MASK) &&
55566ac3b8a8Svsakar 				    !sig) || ufsvfsp->vfs_dontblock) {
55576ac3b8a8Svsakar 					mutex_exit(&ulp->ul_lock);
55586ac3b8a8Svsakar 					return (EINTR);
55596ac3b8a8Svsakar 				}
55606ac3b8a8Svsakar 			}
55616ac3b8a8Svsakar 		}
55626ac3b8a8Svsakar 		mutex_exit(&ulp->ul_lock);
55636ac3b8a8Svsakar 		goto retry_map;
55646ac3b8a8Svsakar 	}
55656ac3b8a8Svsakar 
55667c478bd9Sstevel@tonic-gate 	vn_a.vp = vp;
55677c478bd9Sstevel@tonic-gate 	vn_a.offset = (u_offset_t)off;
55687c478bd9Sstevel@tonic-gate 	vn_a.type = flags & MAP_TYPE;
55697c478bd9Sstevel@tonic-gate 	vn_a.prot = prot;
55707c478bd9Sstevel@tonic-gate 	vn_a.maxprot = maxprot;
55717c478bd9Sstevel@tonic-gate 	vn_a.cred = cr;
55727c478bd9Sstevel@tonic-gate 	vn_a.amp = NULL;
55737c478bd9Sstevel@tonic-gate 	vn_a.flags = flags & ~MAP_TYPE;
55747c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
55757c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
55767c478bd9Sstevel@tonic-gate 
557702ff05a9Svsakar 	error = as_map_locked(as, *addrp, len, segvn_create, &vn_a);
55786ac3b8a8Svsakar 	if (ulp)
55797c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
55806ac3b8a8Svsakar 	as_rangeunlock(as);
55817c478bd9Sstevel@tonic-gate out:
55827c478bd9Sstevel@tonic-gate 	return (error);
55837c478bd9Sstevel@tonic-gate }
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate /* ARGSUSED */
55867c478bd9Sstevel@tonic-gate static int
ufs_addmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uchar_t prot,uchar_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)558780d5689fSPatrick Mooney ufs_addmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
558880d5689fSPatrick Mooney     size_t len, uchar_t  prot, uchar_t  maxprot, uint_t    flags,
558980d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct)
55907c478bd9Sstevel@tonic-gate {
55917c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
55927c478bd9Sstevel@tonic-gate 
55937c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
55947c478bd9Sstevel@tonic-gate 		return (ENOSYS);
55957c478bd9Sstevel@tonic-gate 	}
55967c478bd9Sstevel@tonic-gate 
55977c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
55987c478bd9Sstevel@tonic-gate 	ip->i_mapcnt += btopr(len);
55997c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
56007c478bd9Sstevel@tonic-gate 	return (0);
56017c478bd9Sstevel@tonic-gate }
56027c478bd9Sstevel@tonic-gate 
56037c478bd9Sstevel@tonic-gate /*ARGSUSED*/
56047c478bd9Sstevel@tonic-gate static int
ufs_delmap(struct vnode * vp,offset_t off,struct as * as,caddr_t addr,size_t len,uint_t prot,uint_t maxprot,uint_t flags,struct cred * cr,caller_context_t * ct)56057c478bd9Sstevel@tonic-gate ufs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
560680d5689fSPatrick Mooney     size_t len, uint_t prot,  uint_t maxprot,  uint_t flags, struct cred *cr,
560780d5689fSPatrick Mooney     caller_context_t *ct)
56087c478bd9Sstevel@tonic-gate {
56097c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP) {
56127c478bd9Sstevel@tonic-gate 		return (ENOSYS);
56137c478bd9Sstevel@tonic-gate 	}
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
56162575b441SToomas Soome 	ip->i_mapcnt -= btopr(len);	/* Count released mappings */
56177c478bd9Sstevel@tonic-gate 	ASSERT(ip->i_mapcnt >= 0);
56187c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
56197c478bd9Sstevel@tonic-gate 	return (0);
56207c478bd9Sstevel@tonic-gate }
56217c478bd9Sstevel@tonic-gate /*
56227c478bd9Sstevel@tonic-gate  * Return the answer requested to poll() for non-device files
56237c478bd9Sstevel@tonic-gate  */
56247c478bd9Sstevel@tonic-gate struct pollhead ufs_pollhd;
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate /* ARGSUSED */
56277c478bd9Sstevel@tonic-gate int
ufs_poll(vnode_t * vp,short ev,int any,short * revp,struct pollhead ** phpp,caller_context_t * ct)5628da6c28aaSamw ufs_poll(vnode_t *vp, short ev, int any, short *revp, struct pollhead **phpp,
562980d5689fSPatrick Mooney     caller_context_t *ct)
56307c478bd9Sstevel@tonic-gate {
56317c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp;
56327c478bd9Sstevel@tonic-gate 
563380d5689fSPatrick Mooney 	/*
5634*87bfe94cSPatrick Mooney 	 * Regular files reject epollers (and edge-triggered pollers).
563580d5689fSPatrick Mooney 	 * See the comment in fs_poll() for a more detailed explanation.
563680d5689fSPatrick Mooney 	 */
5637*87bfe94cSPatrick Mooney 	if (fs_reject_epoll() || (ev & POLLET) != 0) {
563880d5689fSPatrick Mooney 		return (EPERM);
563980d5689fSPatrick Mooney 	}
564080d5689fSPatrick Mooney 
56417c478bd9Sstevel@tonic-gate 	*revp = 0;
56427c478bd9Sstevel@tonic-gate 	ufsvfsp = VTOI(vp)->i_ufsvfs;
56437c478bd9Sstevel@tonic-gate 
56447c478bd9Sstevel@tonic-gate 	if (!ufsvfsp) {
56457c478bd9Sstevel@tonic-gate 		*revp = POLLHUP;
56467c478bd9Sstevel@tonic-gate 		goto out;
56477c478bd9Sstevel@tonic-gate 	}
56487c478bd9Sstevel@tonic-gate 
56497c478bd9Sstevel@tonic-gate 	if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs) ||
56507c478bd9Sstevel@tonic-gate 	    ULOCKFS_IS_ELOCK(&ufsvfsp->vfs_ulockfs)) {
56517c478bd9Sstevel@tonic-gate 		*revp |= POLLERR;
56527c478bd9Sstevel@tonic-gate 
56537c478bd9Sstevel@tonic-gate 	} else {
56547c478bd9Sstevel@tonic-gate 		if ((ev & POLLOUT) && !ufsvfsp->vfs_fs->fs_ronly &&
56557c478bd9Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
56567c478bd9Sstevel@tonic-gate 			*revp |= POLLOUT;
56577c478bd9Sstevel@tonic-gate 
56587c478bd9Sstevel@tonic-gate 		if ((ev & POLLWRBAND) && !ufsvfsp->vfs_fs->fs_ronly &&
56597c478bd9Sstevel@tonic-gate 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
56607c478bd9Sstevel@tonic-gate 			*revp |= POLLWRBAND;
56617c478bd9Sstevel@tonic-gate 
56627c478bd9Sstevel@tonic-gate 		if (ev & POLLIN)
56637c478bd9Sstevel@tonic-gate 			*revp |= POLLIN;
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 		if (ev & POLLRDNORM)
56667c478bd9Sstevel@tonic-gate 			*revp |= POLLRDNORM;
56677c478bd9Sstevel@tonic-gate 
56687c478bd9Sstevel@tonic-gate 		if (ev & POLLRDBAND)
56697c478bd9Sstevel@tonic-gate 			*revp |= POLLRDBAND;
56707c478bd9Sstevel@tonic-gate 	}
56717c478bd9Sstevel@tonic-gate 
56727c478bd9Sstevel@tonic-gate 	if ((ev & POLLPRI) && (*revp & (POLLERR|POLLHUP)))
56737c478bd9Sstevel@tonic-gate 		*revp |= POLLPRI;
56747c478bd9Sstevel@tonic-gate out:
567580d5689fSPatrick Mooney 	if (*revp == 0 && ! any) {
567680d5689fSPatrick Mooney 		*phpp = &ufs_pollhd;
567780d5689fSPatrick Mooney 	}
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 	return (0);
56807c478bd9Sstevel@tonic-gate }
56817c478bd9Sstevel@tonic-gate 
56827c478bd9Sstevel@tonic-gate /* ARGSUSED */
56837c478bd9Sstevel@tonic-gate static int
ufs_l_pathconf(struct vnode * vp,int cmd,ulong_t * valp,struct cred * cr,caller_context_t * ct)5684da6c28aaSamw ufs_l_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr,
568580d5689fSPatrick Mooney     caller_context_t *ct)
56867c478bd9Sstevel@tonic-gate {
56877c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
56887c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
56892575b441SToomas Soome 	struct inode	*sip = NULL;
56907c478bd9Sstevel@tonic-gate 	int		error;
56912575b441SToomas Soome 	struct inode	*ip = VTOI(vp);
56927c478bd9Sstevel@tonic-gate 	int		issync;
56937c478bd9Sstevel@tonic-gate 
56947c478bd9Sstevel@tonic-gate 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_PATHCONF_MASK);
56957c478bd9Sstevel@tonic-gate 	if (error)
56967c478bd9Sstevel@tonic-gate 		return (error);
56977c478bd9Sstevel@tonic-gate 
56987c478bd9Sstevel@tonic-gate 	switch (cmd) {
56997c478bd9Sstevel@tonic-gate 		/*
57007c478bd9Sstevel@tonic-gate 		 * Have to handle _PC_NAME_MAX here, because the normal way
57017c478bd9Sstevel@tonic-gate 		 * [fs_pathconf() -> VOP_STATVFS() -> ufs_statvfs()]
57027c478bd9Sstevel@tonic-gate 		 * results in a lock ordering reversal between
57037c478bd9Sstevel@tonic-gate 		 * ufs_lockfs_{begin,end}() and
57047c478bd9Sstevel@tonic-gate 		 * ufs_thread_{suspend,continue}().
57057c478bd9Sstevel@tonic-gate 		 *
57067c478bd9Sstevel@tonic-gate 		 * Keep in sync with ufs_statvfs().
57077c478bd9Sstevel@tonic-gate 		 */
57087c478bd9Sstevel@tonic-gate 	case _PC_NAME_MAX:
57097c478bd9Sstevel@tonic-gate 		*valp = MAXNAMLEN;
57107c478bd9Sstevel@tonic-gate 		break;
57117c478bd9Sstevel@tonic-gate 
57127c478bd9Sstevel@tonic-gate 	case _PC_FILESIZEBITS:
57137c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
57147c478bd9Sstevel@tonic-gate 			*valp = UFS_FILESIZE_BITS;
57157c478bd9Sstevel@tonic-gate 		else
57167c478bd9Sstevel@tonic-gate 			*valp = 32;
57177c478bd9Sstevel@tonic-gate 		break;
57187c478bd9Sstevel@tonic-gate 
57197c478bd9Sstevel@tonic-gate 	case _PC_XATTR_EXISTS:
57207c478bd9Sstevel@tonic-gate 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
57217c478bd9Sstevel@tonic-gate 
572280d34432Sfrankho 			error =
572380d34432Sfrankho 			    ufs_xattr_getattrdir(vp, &sip, LOOKUP_XATTR, cr);
57247c478bd9Sstevel@tonic-gate 			if (error ==  0 && sip != NULL) {
57257c478bd9Sstevel@tonic-gate 				/* Start transaction */
57267c478bd9Sstevel@tonic-gate 				if (ulp) {
57277c478bd9Sstevel@tonic-gate 					TRANS_BEGIN_CSYNC(ufsvfsp, issync,
57287c478bd9Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
57297c478bd9Sstevel@tonic-gate 				}
57307c478bd9Sstevel@tonic-gate 				/*
57317c478bd9Sstevel@tonic-gate 				 * Is directory empty
57327c478bd9Sstevel@tonic-gate 				 */
57337c478bd9Sstevel@tonic-gate 				rw_enter(&sip->i_rwlock, RW_WRITER);
57347c478bd9Sstevel@tonic-gate 				rw_enter(&sip->i_contents, RW_WRITER);
57357c478bd9Sstevel@tonic-gate 				if (ufs_xattrdirempty(sip,
573680d34432Sfrankho 				    sip->i_number, CRED())) {
57377c478bd9Sstevel@tonic-gate 					rw_enter(&ip->i_contents, RW_WRITER);
57387c478bd9Sstevel@tonic-gate 					ufs_unhook_shadow(ip, sip);
57397c478bd9Sstevel@tonic-gate 					rw_exit(&ip->i_contents);
57407c478bd9Sstevel@tonic-gate 
57417c478bd9Sstevel@tonic-gate 					*valp = 0;
57427c478bd9Sstevel@tonic-gate 
57437c478bd9Sstevel@tonic-gate 				} else
57447c478bd9Sstevel@tonic-gate 					*valp = 1;
57457c478bd9Sstevel@tonic-gate 				rw_exit(&sip->i_contents);
57467c478bd9Sstevel@tonic-gate 				rw_exit(&sip->i_rwlock);
57477c478bd9Sstevel@tonic-gate 				if (ulp) {
57487c478bd9Sstevel@tonic-gate 					TRANS_END_CSYNC(ufsvfsp, error, issync,
57497c478bd9Sstevel@tonic-gate 					    TOP_RMDIR, TOP_RMDIR_SIZE);
57507c478bd9Sstevel@tonic-gate 				}
57517c478bd9Sstevel@tonic-gate 				VN_RELE(ITOV(sip));
57527c478bd9Sstevel@tonic-gate 			} else if (error == ENOENT) {
57537c478bd9Sstevel@tonic-gate 				*valp = 0;
57547c478bd9Sstevel@tonic-gate 				error = 0;
57557c478bd9Sstevel@tonic-gate 			}
57567c478bd9Sstevel@tonic-gate 		} else {
5757da6c28aaSamw 			error = fs_pathconf(vp, cmd, valp, cr, ct);
57587c478bd9Sstevel@tonic-gate 		}
57597c478bd9Sstevel@tonic-gate 		break;
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
57627c478bd9Sstevel@tonic-gate 		*valp = _ACL_ACLENT_ENABLED;
57637c478bd9Sstevel@tonic-gate 		break;
57647c478bd9Sstevel@tonic-gate 
57657c478bd9Sstevel@tonic-gate 	case _PC_MIN_HOLE_SIZE:
57667c478bd9Sstevel@tonic-gate 		*valp = (ulong_t)ip->i_fs->fs_bsize;
57677c478bd9Sstevel@tonic-gate 		break;
57687c478bd9Sstevel@tonic-gate 
5769da6c28aaSamw 	case _PC_SATTR_ENABLED:
5770da6c28aaSamw 	case _PC_SATTR_EXISTS:
57719660e5cbSJanice Chang 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5772da6c28aaSamw 		    (vp->v_type == VREG || vp->v_type == VDIR);
5773da6c28aaSamw 		break;
5774da6c28aaSamw 
57753b862e9aSRoger A. Faulkner 	case _PC_TIMESTAMP_RESOLUTION:
57763b862e9aSRoger A. Faulkner 		/*
57773b862e9aSRoger A. Faulkner 		 * UFS keeps only microsecond timestamp resolution.
57783b862e9aSRoger A. Faulkner 		 * This is historical and will probably never change.
57793b862e9aSRoger A. Faulkner 		 */
57803b862e9aSRoger A. Faulkner 		*valp = 1000L;
57813b862e9aSRoger A. Faulkner 		break;
57823b862e9aSRoger A. Faulkner 
57837c478bd9Sstevel@tonic-gate 	default:
5784da6c28aaSamw 		error = fs_pathconf(vp, cmd, valp, cr, ct);
57853b862e9aSRoger A. Faulkner 		break;
57867c478bd9Sstevel@tonic-gate 	}
57877c478bd9Sstevel@tonic-gate 
57887c478bd9Sstevel@tonic-gate 	if (ulp != NULL) {
57897c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
57907c478bd9Sstevel@tonic-gate 	}
57917c478bd9Sstevel@tonic-gate 	return (error);
57927c478bd9Sstevel@tonic-gate }
57937c478bd9Sstevel@tonic-gate 
57947c478bd9Sstevel@tonic-gate int ufs_pageio_writes, ufs_pageio_reads;
57957c478bd9Sstevel@tonic-gate 
57967c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57977c478bd9Sstevel@tonic-gate static int
ufs_pageio(struct vnode * vp,page_t * pp,u_offset_t io_off,size_t io_len,int flags,struct cred * cr,caller_context_t * ct)57987c478bd9Sstevel@tonic-gate ufs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
579980d5689fSPatrick Mooney     int flags, struct cred *cr, caller_context_t *ct)
58007c478bd9Sstevel@tonic-gate {
58017c478bd9Sstevel@tonic-gate 	struct inode *ip = VTOI(vp);
58027c478bd9Sstevel@tonic-gate 	struct ufsvfs *ufsvfsp;
58037c478bd9Sstevel@tonic-gate 	page_t *npp = NULL, *opp = NULL, *cpp = pp;
58047c478bd9Sstevel@tonic-gate 	struct buf *bp;
58057c478bd9Sstevel@tonic-gate 	daddr_t bn;
58067c478bd9Sstevel@tonic-gate 	size_t done_len = 0, cur_len = 0;
58077c478bd9Sstevel@tonic-gate 	int err = 0;
58087c478bd9Sstevel@tonic-gate 	int contig = 0;
58097c478bd9Sstevel@tonic-gate 	int dolock;
58107c478bd9Sstevel@tonic-gate 	int vmpss = 0;
5811bc69f433Saguzovsk 	struct ulockfs *ulp;
58127c478bd9Sstevel@tonic-gate 
58137c478bd9Sstevel@tonic-gate 	if ((flags & B_READ) && pp != NULL && pp->p_vnode == vp &&
58147c478bd9Sstevel@tonic-gate 	    vp->v_mpssdata != NULL) {
58157c478bd9Sstevel@tonic-gate 		vmpss = 1;
58167c478bd9Sstevel@tonic-gate 	}
58177c478bd9Sstevel@tonic-gate 
58187c478bd9Sstevel@tonic-gate 	dolock = (rw_owner(&ip->i_contents) != curthread);
58197c478bd9Sstevel@tonic-gate 	/*
58207c478bd9Sstevel@tonic-gate 	 * We need a better check.  Ideally, we would use another
58217c478bd9Sstevel@tonic-gate 	 * vnodeops so that hlocked and forcibly unmounted file
58227c478bd9Sstevel@tonic-gate 	 * systems would return EIO where appropriate and w/o the
58237c478bd9Sstevel@tonic-gate 	 * need for these checks.
58247c478bd9Sstevel@tonic-gate 	 */
58257c478bd9Sstevel@tonic-gate 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
58267c478bd9Sstevel@tonic-gate 		return (EIO);
58277c478bd9Sstevel@tonic-gate 
5828bc69f433Saguzovsk 	/*
5829bc69f433Saguzovsk 	 * For vmpss (pp can be NULL) case respect the quiesce protocol.
5830bc69f433Saguzovsk 	 * ul_lock must be taken before locking pages so we can't use it here
5831bc69f433Saguzovsk 	 * if pp is non NULL because segvn already locked pages
5832bc69f433Saguzovsk 	 * SE_EXCL. Instead we rely on the fact that a forced umount or
5833bc69f433Saguzovsk 	 * applying a filesystem lock via ufs_fiolfs() will block in the
5834bc69f433Saguzovsk 	 * implicit call to ufs_flush() until we unlock the pages after the
5835bc69f433Saguzovsk 	 * return to segvn. Other ufs_quiesce() callers keep ufs_quiesce_pend
5836bc69f433Saguzovsk 	 * above 0 until they are done. We have to be careful not to increment
5837bc69f433Saguzovsk 	 * ul_vnops_cnt here after forceful unmount hlocks the file system.
5838bc69f433Saguzovsk 	 *
5839bc69f433Saguzovsk 	 * If pp is NULL use ul_lock to make sure we don't increment
5840bc69f433Saguzovsk 	 * ul_vnops_cnt after forceful unmount hlocks the file system.
5841bc69f433Saguzovsk 	 */
5842bc69f433Saguzovsk 	if (vmpss || pp == NULL) {
5843bc69f433Saguzovsk 		ulp = &ufsvfsp->vfs_ulockfs;
5844bc69f433Saguzovsk 		if (pp == NULL)
5845bc69f433Saguzovsk 			mutex_enter(&ulp->ul_lock);
5846bc69f433Saguzovsk 		if (ulp->ul_fs_lock & ULOCKFS_GETREAD_MASK) {
5847bc69f433Saguzovsk 			if (pp == NULL) {
5848bc69f433Saguzovsk 				mutex_exit(&ulp->ul_lock);
5849bc69f433Saguzovsk 			}
5850bc69f433Saguzovsk 			return (vmpss ? EIO : EINVAL);
5851bc69f433Saguzovsk 		}
58521a5e258fSJosef 'Jeff' Sipek 		atomic_inc_ulong(&ulp->ul_vnops_cnt);
5853bc69f433Saguzovsk 		if (pp == NULL)
5854bc69f433Saguzovsk 			mutex_exit(&ulp->ul_lock);
5855bc69f433Saguzovsk 		if (ufs_quiesce_pend) {
58561a5e258fSJosef 'Jeff' Sipek 			if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5857bc69f433Saguzovsk 				cv_broadcast(&ulp->ul_cv);
5858bc69f433Saguzovsk 			return (vmpss ? EIO : EINVAL);
5859bc69f433Saguzovsk 		}
5860bc69f433Saguzovsk 	}
5861bc69f433Saguzovsk 
58627c478bd9Sstevel@tonic-gate 	if (dolock) {
58637c478bd9Sstevel@tonic-gate 		/*
58647c478bd9Sstevel@tonic-gate 		 * segvn may call VOP_PAGEIO() instead of VOP_GETPAGE() to
58657c478bd9Sstevel@tonic-gate 		 * handle a fault against a segment that maps vnode pages with
58667c478bd9Sstevel@tonic-gate 		 * large mappings.  Segvn creates pages and holds them locked
58677c478bd9Sstevel@tonic-gate 		 * SE_EXCL during VOP_PAGEIO() call. In this case we have to
58687c478bd9Sstevel@tonic-gate 		 * use rw_tryenter() to avoid a potential deadlock since in
58697c478bd9Sstevel@tonic-gate 		 * lock order i_contents needs to be taken first.
58707c478bd9Sstevel@tonic-gate 		 * Segvn will retry via VOP_GETPAGE() if VOP_PAGEIO() fails.
58717c478bd9Sstevel@tonic-gate 		 */
58727c478bd9Sstevel@tonic-gate 		if (!vmpss) {
58737c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_contents, RW_READER);
58747c478bd9Sstevel@tonic-gate 		} else if (!rw_tryenter(&ip->i_contents, RW_READER)) {
58751a5e258fSJosef 'Jeff' Sipek 			if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5876bc69f433Saguzovsk 				cv_broadcast(&ulp->ul_cv);
58777c478bd9Sstevel@tonic-gate 			return (EDEADLK);
58787c478bd9Sstevel@tonic-gate 		}
58797c478bd9Sstevel@tonic-gate 	}
58807c478bd9Sstevel@tonic-gate 
588137fbc076Saguzovsk 	/*
588237fbc076Saguzovsk 	 * Return an error to segvn because the pagefault request is beyond
588337fbc076Saguzovsk 	 * PAGESIZE rounded EOF.
588437fbc076Saguzovsk 	 */
588537fbc076Saguzovsk 	if (vmpss && btopr(io_off + io_len) > btopr(ip->i_size)) {
588637fbc076Saguzovsk 		if (dolock)
588737fbc076Saguzovsk 			rw_exit(&ip->i_contents);
58881a5e258fSJosef 'Jeff' Sipek 		if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5889bc69f433Saguzovsk 			cv_broadcast(&ulp->ul_cv);
589037fbc076Saguzovsk 		return (EFAULT);
589137fbc076Saguzovsk 	}
589237fbc076Saguzovsk 
58937c478bd9Sstevel@tonic-gate 	if (pp == NULL) {
58947c478bd9Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
58957c478bd9Sstevel@tonic-gate 			err = ENOSYS;
58967c478bd9Sstevel@tonic-gate 		} else {
58977c478bd9Sstevel@tonic-gate 			err = EINVAL;
58987c478bd9Sstevel@tonic-gate 		}
58997c478bd9Sstevel@tonic-gate 		if (dolock)
59007c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
59011a5e258fSJosef 'Jeff' Sipek 		if (!atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
5902bc69f433Saguzovsk 			cv_broadcast(&ulp->ul_cv);
59037c478bd9Sstevel@tonic-gate 		return (err);
59047c478bd9Sstevel@tonic-gate 	}
59057c478bd9Sstevel@tonic-gate 
59067c478bd9Sstevel@tonic-gate 	/*
59077c478bd9Sstevel@tonic-gate 	 * Break the io request into chunks, one for each contiguous
59087c478bd9Sstevel@tonic-gate 	 * stretch of disk blocks in the target file.
59097c478bd9Sstevel@tonic-gate 	 */
59107c478bd9Sstevel@tonic-gate 	while (done_len < io_len) {
59117c478bd9Sstevel@tonic-gate 		ASSERT(cpp);
59127c478bd9Sstevel@tonic-gate 		contig = 0;
59137c478bd9Sstevel@tonic-gate 		if (err = bmap_read(ip, (u_offset_t)(io_off + done_len),
591480d34432Sfrankho 		    &bn, &contig))
59157c478bd9Sstevel@tonic-gate 			break;
59167c478bd9Sstevel@tonic-gate 
59177c478bd9Sstevel@tonic-gate 		if (bn == UFS_HOLE) {	/* No holey swapfiles */
59187c478bd9Sstevel@tonic-gate 			if (vmpss) {
59197c478bd9Sstevel@tonic-gate 				err = EFAULT;
59207c478bd9Sstevel@tonic-gate 				break;
59217c478bd9Sstevel@tonic-gate 			}
59227c478bd9Sstevel@tonic-gate 			err = ufs_fault(ITOV(ip), "ufs_pageio: bn == UFS_HOLE");
59237c478bd9Sstevel@tonic-gate 			break;
59247c478bd9Sstevel@tonic-gate 		}
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 		cur_len = MIN(io_len - done_len, contig);
59277c478bd9Sstevel@tonic-gate 		/*
59287c478bd9Sstevel@tonic-gate 		 * Zero out a page beyond EOF, when the last block of
59297c478bd9Sstevel@tonic-gate 		 * a file is a UFS fragment so that ufs_pageio() can be used
59307c478bd9Sstevel@tonic-gate 		 * instead of ufs_getpage() to handle faults against
59317c478bd9Sstevel@tonic-gate 		 * segvn segments that use large pages.
59327c478bd9Sstevel@tonic-gate 		 */
59337c478bd9Sstevel@tonic-gate 		page_list_break(&cpp, &npp, btopr(cur_len));
59347c478bd9Sstevel@tonic-gate 		if ((flags & B_READ) && (cur_len & PAGEOFFSET)) {
59357c478bd9Sstevel@tonic-gate 			size_t xlen = cur_len & PAGEOFFSET;
59367c478bd9Sstevel@tonic-gate 			pagezero(cpp->p_prev, xlen, PAGESIZE - xlen);
59377c478bd9Sstevel@tonic-gate 		}
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate 		bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
59407c478bd9Sstevel@tonic-gate 		ASSERT(bp != NULL);
59417c478bd9Sstevel@tonic-gate 
59427c478bd9Sstevel@tonic-gate 		bp->b_edev = ip->i_dev;
59437c478bd9Sstevel@tonic-gate 		bp->b_dev = cmpdev(ip->i_dev);
59447c478bd9Sstevel@tonic-gate 		bp->b_blkno = bn;
59457c478bd9Sstevel@tonic-gate 		bp->b_un.b_addr = (caddr_t)0;
59467c478bd9Sstevel@tonic-gate 		bp->b_file = ip->i_vnode;
59477c478bd9Sstevel@tonic-gate 
5948d3d50737SRafael Vanoni 		ufsvfsp->vfs_iotstamp = ddi_get_lbolt();
59497c478bd9Sstevel@tonic-gate 		ub.ub_pageios.value.ul++;
59507c478bd9Sstevel@tonic-gate 		if (ufsvfsp->vfs_snapshot)
59517c478bd9Sstevel@tonic-gate 			fssnap_strategy(&(ufsvfsp->vfs_snapshot), bp);
59527c478bd9Sstevel@tonic-gate 		else
59537c478bd9Sstevel@tonic-gate 			(void) bdev_strategy(bp);
59547c478bd9Sstevel@tonic-gate 
59557c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
59567c478bd9Sstevel@tonic-gate 			ufs_pageio_reads++;
59577c478bd9Sstevel@tonic-gate 		else
59587c478bd9Sstevel@tonic-gate 			ufs_pageio_writes++;
59597c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
59607c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_INBLK, 1);
59617c478bd9Sstevel@tonic-gate 		else
59627c478bd9Sstevel@tonic-gate 			lwp_stat_update(LWP_STAT_OUBLK, 1);
59637c478bd9Sstevel@tonic-gate 		/*
59647c478bd9Sstevel@tonic-gate 		 * If the request is not B_ASYNC, wait for i/o to complete
59657c478bd9Sstevel@tonic-gate 		 * and re-assemble the page list to return to the caller.
59667c478bd9Sstevel@tonic-gate 		 * If it is B_ASYNC we leave the page list in pieces and
59677c478bd9Sstevel@tonic-gate 		 * cleanup() will dispose of them.
59687c478bd9Sstevel@tonic-gate 		 */
59697c478bd9Sstevel@tonic-gate 		if ((flags & B_ASYNC) == 0) {
59707c478bd9Sstevel@tonic-gate 			err = biowait(bp);
59717c478bd9Sstevel@tonic-gate 			pageio_done(bp);
59727c478bd9Sstevel@tonic-gate 			if (err)
59737c478bd9Sstevel@tonic-gate 				break;
59747c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
59757c478bd9Sstevel@tonic-gate 		}
59767c478bd9Sstevel@tonic-gate 		cpp = npp;
59777c478bd9Sstevel@tonic-gate 		npp = NULL;
59787c478bd9Sstevel@tonic-gate 		if (flags & B_READ)
59797c478bd9Sstevel@tonic-gate 			cur_len = P2ROUNDUP_TYPED(cur_len, PAGESIZE, size_t);
59807c478bd9Sstevel@tonic-gate 		done_len += cur_len;
59817c478bd9Sstevel@tonic-gate 	}
59827c478bd9Sstevel@tonic-gate 	ASSERT(err || (cpp == NULL && npp == NULL && done_len == io_len));
59837c478bd9Sstevel@tonic-gate 	if (err) {
59847c478bd9Sstevel@tonic-gate 		if (flags & B_ASYNC) {
59857c478bd9Sstevel@tonic-gate 			/* Cleanup unprocessed parts of list */
59867c478bd9Sstevel@tonic-gate 			page_list_concat(&cpp, &npp);
59877c478bd9Sstevel@tonic-gate 			if (flags & B_READ)
59887c478bd9Sstevel@tonic-gate 				pvn_read_done(cpp, B_ERROR);
59897c478bd9Sstevel@tonic-gate 			else
59907c478bd9Sstevel@tonic-gate 				pvn_write_done(cpp, B_ERROR);
59917c478bd9Sstevel@tonic-gate 		} else {
59927c478bd9Sstevel@tonic-gate 			/* Re-assemble list and let caller clean up */
59937c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &cpp);
59947c478bd9Sstevel@tonic-gate 			page_list_concat(&opp, &npp);
59957c478bd9Sstevel@tonic-gate 		}
59967c478bd9Sstevel@tonic-gate 	}
5997bc69f433Saguzovsk 
5998bc69f433Saguzovsk 	if (vmpss && !(ip->i_flag & IACC) && !ULOCKFS_IS_NOIACC(ulp) &&
5999bc69f433Saguzovsk 	    ufsvfsp->vfs_fs->fs_ronly == 0 && !ufsvfsp->vfs_noatime) {
6000bc69f433Saguzovsk 		mutex_enter(&ip->i_tlock);
6001bc69f433Saguzovsk 		ip->i_flag |= IACC;
6002bc69f433Saguzovsk 		ITIMES_NOLOCK(ip);
6003bc69f433Saguzovsk 		mutex_exit(&ip->i_tlock);
6004bc69f433Saguzovsk 	}
6005bc69f433Saguzovsk 
60067c478bd9Sstevel@tonic-gate 	if (dolock)
60077c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
60081a5e258fSJosef 'Jeff' Sipek 	if (vmpss && !atomic_dec_ulong_nv(&ulp->ul_vnops_cnt))
6009bc69f433Saguzovsk 		cv_broadcast(&ulp->ul_cv);
60107c478bd9Sstevel@tonic-gate 	return (err);
60117c478bd9Sstevel@tonic-gate }
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate /*
60147c478bd9Sstevel@tonic-gate  * Called when the kernel is in a frozen state to dump data
60157c478bd9Sstevel@tonic-gate  * directly to the device. It uses a private dump data structure,
60167c478bd9Sstevel@tonic-gate  * set up by dump_ctl, to locate the correct disk block to which to dump.
60177c478bd9Sstevel@tonic-gate  */
6018da6c28aaSamw /*ARGSUSED*/
60197c478bd9Sstevel@tonic-gate static int
ufs_dump(vnode_t * vp,caddr_t addr,offset_t ldbn,offset_t dblks,caller_context_t * ct)6020d7334e51Srm ufs_dump(vnode_t *vp, caddr_t addr, offset_t ldbn, offset_t dblks,
6021d7334e51Srm     caller_context_t *ct)
60227c478bd9Sstevel@tonic-gate {
60237c478bd9Sstevel@tonic-gate 	u_offset_t	file_size;
60247c478bd9Sstevel@tonic-gate 	struct inode    *ip = VTOI(vp);
60257c478bd9Sstevel@tonic-gate 	struct fs	*fs = ip->i_fs;
60267c478bd9Sstevel@tonic-gate 	daddr_t		dbn, lfsbn;
60277c478bd9Sstevel@tonic-gate 	int		disk_blks = fs->fs_bsize >> DEV_BSHIFT;
60287c478bd9Sstevel@tonic-gate 	int		error = 0;
60297c478bd9Sstevel@tonic-gate 	int		ndbs, nfsbs;
60307c478bd9Sstevel@tonic-gate 
60317c478bd9Sstevel@tonic-gate 	/*
60327c478bd9Sstevel@tonic-gate 	 * forced unmount case
60337c478bd9Sstevel@tonic-gate 	 */
60347c478bd9Sstevel@tonic-gate 	if (ip->i_ufsvfs == NULL)
60357c478bd9Sstevel@tonic-gate 		return (EIO);
60367c478bd9Sstevel@tonic-gate 	/*
60377c478bd9Sstevel@tonic-gate 	 * Validate the inode that it has not been modified since
60387c478bd9Sstevel@tonic-gate 	 * the dump structure is allocated.
60397c478bd9Sstevel@tonic-gate 	 */
60407c478bd9Sstevel@tonic-gate 	mutex_enter(&ip->i_tlock);
60417c478bd9Sstevel@tonic-gate 	if ((dump_info == NULL) ||
60427c478bd9Sstevel@tonic-gate 	    (dump_info->ip != ip) ||
60437c478bd9Sstevel@tonic-gate 	    (dump_info->time.tv_sec != ip->i_mtime.tv_sec) ||
60447c478bd9Sstevel@tonic-gate 	    (dump_info->time.tv_usec != ip->i_mtime.tv_usec)) {
60457c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
60467c478bd9Sstevel@tonic-gate 		return (-1);
60477c478bd9Sstevel@tonic-gate 	}
60487c478bd9Sstevel@tonic-gate 	mutex_exit(&ip->i_tlock);
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate 	/*
60517c478bd9Sstevel@tonic-gate 	 * See that the file has room for this write
60527c478bd9Sstevel@tonic-gate 	 */
60537c478bd9Sstevel@tonic-gate 	UFS_GET_ISIZE(&file_size, ip);
60547c478bd9Sstevel@tonic-gate 
6055d7334e51Srm 	if (ldbtob(ldbn + dblks) > file_size)
60567c478bd9Sstevel@tonic-gate 		return (ENOSPC);
60577c478bd9Sstevel@tonic-gate 
60587c478bd9Sstevel@tonic-gate 	/*
60597c478bd9Sstevel@tonic-gate 	 * Find the physical disk block numbers from the dump
60607c478bd9Sstevel@tonic-gate 	 * private data structure directly and write out the data
60617c478bd9Sstevel@tonic-gate 	 * in contiguous block lumps
60627c478bd9Sstevel@tonic-gate 	 */
60637c478bd9Sstevel@tonic-gate 	while (dblks > 0 && !error) {
6064d7334e51Srm 		lfsbn = (daddr_t)lblkno(fs, ldbtob(ldbn));
60657c478bd9Sstevel@tonic-gate 		dbn = fsbtodb(fs, dump_info->dblk[lfsbn]) + ldbn % disk_blks;
60667c478bd9Sstevel@tonic-gate 		nfsbs = 1;
60677c478bd9Sstevel@tonic-gate 		ndbs = disk_blks - ldbn % disk_blks;
60687c478bd9Sstevel@tonic-gate 		while (ndbs < dblks && fsbtodb(fs, dump_info->dblk[lfsbn +
60697c478bd9Sstevel@tonic-gate 		    nfsbs]) == dbn + ndbs) {
60707c478bd9Sstevel@tonic-gate 			nfsbs++;
60717c478bd9Sstevel@tonic-gate 			ndbs += disk_blks;
60727c478bd9Sstevel@tonic-gate 		}
60737c478bd9Sstevel@tonic-gate 		if (ndbs > dblks)
60747c478bd9Sstevel@tonic-gate 			ndbs = dblks;
60757c478bd9Sstevel@tonic-gate 		error = bdev_dump(ip->i_dev, addr, dbn, ndbs);
60767c478bd9Sstevel@tonic-gate 		addr += ldbtob((offset_t)ndbs);
60777c478bd9Sstevel@tonic-gate 		dblks -= ndbs;
60787c478bd9Sstevel@tonic-gate 		ldbn += ndbs;
60797c478bd9Sstevel@tonic-gate 	}
60807c478bd9Sstevel@tonic-gate 	return (error);
60817c478bd9Sstevel@tonic-gate 
60827c478bd9Sstevel@tonic-gate }
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate /*
60857c478bd9Sstevel@tonic-gate  * Prepare the file system before and after the dump operation.
60867c478bd9Sstevel@tonic-gate  *
60877c478bd9Sstevel@tonic-gate  * action = DUMP_ALLOC:
60887c478bd9Sstevel@tonic-gate  * Preparation before dump, allocate dump private data structure
60897c478bd9Sstevel@tonic-gate  * to hold all the direct and indirect block info for dump.
60907c478bd9Sstevel@tonic-gate  *
60917c478bd9Sstevel@tonic-gate  * action = DUMP_FREE:
60927c478bd9Sstevel@tonic-gate  * Clean up after dump, deallocate the dump private data structure.
60937c478bd9Sstevel@tonic-gate  *
60947c478bd9Sstevel@tonic-gate  * action = DUMP_SCAN:
60957c478bd9Sstevel@tonic-gate  * Scan dump_info for *blkp DEV_BSIZE blocks of contig fs space;
60967c478bd9Sstevel@tonic-gate  * if found, the starting file-relative DEV_BSIZE lbn is written
60977c478bd9Sstevel@tonic-gate  * to *bklp; that lbn is intended for use with VOP_DUMP()
60987c478bd9Sstevel@tonic-gate  */
6099da6c28aaSamw /*ARGSUSED*/
61007c478bd9Sstevel@tonic-gate static int
ufs_dumpctl(vnode_t * vp,int action,offset_t * blkp,caller_context_t * ct)6101d7334e51Srm ufs_dumpctl(vnode_t *vp, int action, offset_t *blkp, caller_context_t *ct)
61027c478bd9Sstevel@tonic-gate {
61037c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
61047c478bd9Sstevel@tonic-gate 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
61057c478bd9Sstevel@tonic-gate 	struct fs	*fs;
61067c478bd9Sstevel@tonic-gate 	daddr32_t	*dblk, *storeblk;
61077c478bd9Sstevel@tonic-gate 	daddr32_t	*nextblk, *endblk;
61087c478bd9Sstevel@tonic-gate 	struct buf	*bp;
61097c478bd9Sstevel@tonic-gate 	int		i, entry, entries;
61107c478bd9Sstevel@tonic-gate 	int		n, ncontig;
61117c478bd9Sstevel@tonic-gate 
61127c478bd9Sstevel@tonic-gate 	/*
61137c478bd9Sstevel@tonic-gate 	 * check for forced unmount
61147c478bd9Sstevel@tonic-gate 	 */
61157c478bd9Sstevel@tonic-gate 	if (ufsvfsp == NULL)
61167c478bd9Sstevel@tonic-gate 		return (EIO);
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 	if (action == DUMP_ALLOC) {
61197c478bd9Sstevel@tonic-gate 		/*
61207c478bd9Sstevel@tonic-gate 		 * alloc and record dump_info
61217c478bd9Sstevel@tonic-gate 		 */
61227c478bd9Sstevel@tonic-gate 		if (dump_info != NULL)
61237c478bd9Sstevel@tonic-gate 			return (EINVAL);
61247c478bd9Sstevel@tonic-gate 
61257c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VREG);
61267c478bd9Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
61277c478bd9Sstevel@tonic-gate 
61287c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
61297c478bd9Sstevel@tonic-gate 
61307c478bd9Sstevel@tonic-gate 		if (bmap_has_holes(ip)) {
61317c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_contents);
61327c478bd9Sstevel@tonic-gate 			return (EFAULT);
61337c478bd9Sstevel@tonic-gate 		}
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 		/*
61367c478bd9Sstevel@tonic-gate 		 * calculate and allocate space needed according to i_size
61377c478bd9Sstevel@tonic-gate 		 */
61387c478bd9Sstevel@tonic-gate 		entries = (int)lblkno(fs, blkroundup(fs, ip->i_size));
613980d34432Sfrankho 		dump_info = kmem_alloc(sizeof (struct dump) +
614080d34432Sfrankho 		    (entries - 1) * sizeof (daddr32_t), KM_NOSLEEP);
614180d34432Sfrankho 		if (dump_info == NULL) {
614280d34432Sfrankho 			rw_exit(&ip->i_contents);
614380d34432Sfrankho 			return (ENOMEM);
61447c478bd9Sstevel@tonic-gate 		}
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate 		/* Start saving the info */
61477c478bd9Sstevel@tonic-gate 		dump_info->fsbs = entries;
61487c478bd9Sstevel@tonic-gate 		dump_info->ip = ip;
61497c478bd9Sstevel@tonic-gate 		storeblk = &dump_info->dblk[0];
61507c478bd9Sstevel@tonic-gate 
61517c478bd9Sstevel@tonic-gate 		/* Direct Blocks */
61527c478bd9Sstevel@tonic-gate 		for (entry = 0; entry < NDADDR && entry < entries; entry++)
61537c478bd9Sstevel@tonic-gate 			*storeblk++ = ip->i_db[entry];
61547c478bd9Sstevel@tonic-gate 
61557c478bd9Sstevel@tonic-gate 		/* Indirect Blocks */
61567c478bd9Sstevel@tonic-gate 		for (i = 0; i < NIADDR; i++) {
61577c478bd9Sstevel@tonic-gate 			int error = 0;
61587c478bd9Sstevel@tonic-gate 
61597c478bd9Sstevel@tonic-gate 			bp = UFS_BREAD(ufsvfsp,
616080d34432Sfrankho 			    ip->i_dev, fsbtodb(fs, ip->i_ib[i]), fs->fs_bsize);
61617c478bd9Sstevel@tonic-gate 			if (bp->b_flags & B_ERROR)
61627c478bd9Sstevel@tonic-gate 				error = EIO;
61637c478bd9Sstevel@tonic-gate 			else {
61647c478bd9Sstevel@tonic-gate 				dblk = bp->b_un.b_daddr;
61657c478bd9Sstevel@tonic-gate 				if ((storeblk = save_dblks(ip, ufsvfsp,
61667c478bd9Sstevel@tonic-gate 				    storeblk, dblk, i, entries)) == NULL)
61677c478bd9Sstevel@tonic-gate 					error = EIO;
61687c478bd9Sstevel@tonic-gate 			}
61697c478bd9Sstevel@tonic-gate 
61707c478bd9Sstevel@tonic-gate 			brelse(bp);
61717c478bd9Sstevel@tonic-gate 
61727c478bd9Sstevel@tonic-gate 			if (error != 0) {
61737c478bd9Sstevel@tonic-gate 				kmem_free(dump_info, sizeof (struct dump) +
61747c478bd9Sstevel@tonic-gate 				    (entries - 1) * sizeof (daddr32_t));
61757c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_contents);
61767c478bd9Sstevel@tonic-gate 				dump_info = NULL;
61777c478bd9Sstevel@tonic-gate 				return (error);
61787c478bd9Sstevel@tonic-gate 			}
61797c478bd9Sstevel@tonic-gate 		}
61807c478bd9Sstevel@tonic-gate 		/* and time stamp the information */
61817c478bd9Sstevel@tonic-gate 		mutex_enter(&ip->i_tlock);
61827c478bd9Sstevel@tonic-gate 		dump_info->time = ip->i_mtime;
61837c478bd9Sstevel@tonic-gate 		mutex_exit(&ip->i_tlock);
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
61867c478bd9Sstevel@tonic-gate 	} else if (action == DUMP_FREE) {
61877c478bd9Sstevel@tonic-gate 		/*
61887c478bd9Sstevel@tonic-gate 		 * free dump_info
61897c478bd9Sstevel@tonic-gate 		 */
61907c478bd9Sstevel@tonic-gate 		if (dump_info == NULL)
61917c478bd9Sstevel@tonic-gate 			return (EINVAL);
61927c478bd9Sstevel@tonic-gate 		entries = dump_info->fsbs - 1;
61937c478bd9Sstevel@tonic-gate 		kmem_free(dump_info, sizeof (struct dump) +
61947c478bd9Sstevel@tonic-gate 		    entries * sizeof (daddr32_t));
61957c478bd9Sstevel@tonic-gate 		dump_info = NULL;
61967c478bd9Sstevel@tonic-gate 	} else if (action == DUMP_SCAN) {
61977c478bd9Sstevel@tonic-gate 		/*
61987c478bd9Sstevel@tonic-gate 		 * scan dump_info
61997c478bd9Sstevel@tonic-gate 		 */
62007c478bd9Sstevel@tonic-gate 		if (dump_info == NULL)
62017c478bd9Sstevel@tonic-gate 			return (EINVAL);
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 		dblk = dump_info->dblk;
62047c478bd9Sstevel@tonic-gate 		nextblk = dblk + 1;
62057c478bd9Sstevel@tonic-gate 		endblk = dblk + dump_info->fsbs - 1;
62067c478bd9Sstevel@tonic-gate 		fs = ufsvfsp->vfs_fs;
62077c478bd9Sstevel@tonic-gate 		ncontig = *blkp >> (fs->fs_bshift - DEV_BSHIFT);
62087c478bd9Sstevel@tonic-gate 
62097c478bd9Sstevel@tonic-gate 		/*
62107c478bd9Sstevel@tonic-gate 		 * scan dblk[] entries; contig fs space is found when:
62117c478bd9Sstevel@tonic-gate 		 * ((current blkno + frags per block) == next blkno)
62127c478bd9Sstevel@tonic-gate 		 */
62137c478bd9Sstevel@tonic-gate 		n = 0;
62147c478bd9Sstevel@tonic-gate 		while (n < ncontig && dblk < endblk) {
62157c478bd9Sstevel@tonic-gate 			if ((*dblk + fs->fs_frag) == *nextblk)
62167c478bd9Sstevel@tonic-gate 				n++;
62177c478bd9Sstevel@tonic-gate 			else
62187c478bd9Sstevel@tonic-gate 				n = 0;
62197c478bd9Sstevel@tonic-gate 			dblk++;
62207c478bd9Sstevel@tonic-gate 			nextblk++;
62217c478bd9Sstevel@tonic-gate 		}
62227c478bd9Sstevel@tonic-gate 
62237c478bd9Sstevel@tonic-gate 		/*
62247c478bd9Sstevel@tonic-gate 		 * index is where size bytes of contig space begins;
62257c478bd9Sstevel@tonic-gate 		 * conversion from index to the file's DEV_BSIZE lbn
62267c478bd9Sstevel@tonic-gate 		 * is equivalent to:  (index * fs_bsize) / DEV_BSIZE
62277c478bd9Sstevel@tonic-gate 		 */
62287c478bd9Sstevel@tonic-gate 		if (n == ncontig) {
62297c478bd9Sstevel@tonic-gate 			i = (dblk - dump_info->dblk) - ncontig;
62307c478bd9Sstevel@tonic-gate 			*blkp = i << (fs->fs_bshift - DEV_BSHIFT);
62317c478bd9Sstevel@tonic-gate 		} else
62327c478bd9Sstevel@tonic-gate 			return (EFAULT);
62337c478bd9Sstevel@tonic-gate 	}
62347c478bd9Sstevel@tonic-gate 	return (0);
62357c478bd9Sstevel@tonic-gate }
62367c478bd9Sstevel@tonic-gate 
62377c478bd9Sstevel@tonic-gate /*
62387c478bd9Sstevel@tonic-gate  * Recursive helper function for ufs_dumpctl().  It follows the indirect file
62397c478bd9Sstevel@tonic-gate  * system  blocks until it reaches the the disk block addresses, which are
62407c478bd9Sstevel@tonic-gate  * then stored into the given buffer, storeblk.
62417c478bd9Sstevel@tonic-gate  */
62427c478bd9Sstevel@tonic-gate static daddr32_t *
save_dblks(struct inode * ip,struct ufsvfs * ufsvfsp,daddr32_t * storeblk,daddr32_t * dblk,int level,int entries)62437c478bd9Sstevel@tonic-gate save_dblks(struct inode *ip, struct ufsvfs *ufsvfsp,  daddr32_t *storeblk,
62447c478bd9Sstevel@tonic-gate     daddr32_t *dblk, int level, int entries)
62457c478bd9Sstevel@tonic-gate {
62467c478bd9Sstevel@tonic-gate 	struct fs	*fs = ufsvfsp->vfs_fs;
62477c478bd9Sstevel@tonic-gate 	struct buf	*bp;
62487c478bd9Sstevel@tonic-gate 	int		i;
62497c478bd9Sstevel@tonic-gate 
62507c478bd9Sstevel@tonic-gate 	if (level == 0) {
62517c478bd9Sstevel@tonic-gate 		for (i = 0; i < NINDIR(fs); i++) {
62527c478bd9Sstevel@tonic-gate 			if (storeblk - dump_info->dblk >= entries)
62537c478bd9Sstevel@tonic-gate 				break;
62547c478bd9Sstevel@tonic-gate 			*storeblk++ = dblk[i];
62557c478bd9Sstevel@tonic-gate 		}
62567c478bd9Sstevel@tonic-gate 		return (storeblk);
62577c478bd9Sstevel@tonic-gate 	}
62587c478bd9Sstevel@tonic-gate 	for (i = 0; i < NINDIR(fs); i++) {
62597c478bd9Sstevel@tonic-gate 		if (storeblk - dump_info->dblk >= entries)
62607c478bd9Sstevel@tonic-gate 			break;
62617c478bd9Sstevel@tonic-gate 		bp = UFS_BREAD(ufsvfsp,
626280d34432Sfrankho 		    ip->i_dev, fsbtodb(fs, dblk[i]), fs->fs_bsize);
62637c478bd9Sstevel@tonic-gate 		if (bp->b_flags & B_ERROR) {
62647c478bd9Sstevel@tonic-gate 			brelse(bp);
62657c478bd9Sstevel@tonic-gate 			return (NULL);
62667c478bd9Sstevel@tonic-gate 		}
62677c478bd9Sstevel@tonic-gate 		storeblk = save_dblks(ip, ufsvfsp, storeblk, bp->b_un.b_daddr,
62687c478bd9Sstevel@tonic-gate 		    level - 1, entries);
62697c478bd9Sstevel@tonic-gate 		brelse(bp);
62707c478bd9Sstevel@tonic-gate 
62717c478bd9Sstevel@tonic-gate 		if (storeblk == NULL)
62727c478bd9Sstevel@tonic-gate 			return (NULL);
62737c478bd9Sstevel@tonic-gate 	}
62747c478bd9Sstevel@tonic-gate 	return (storeblk);
62757c478bd9Sstevel@tonic-gate }
62767c478bd9Sstevel@tonic-gate 
62777c478bd9Sstevel@tonic-gate /* ARGSUSED */
62787c478bd9Sstevel@tonic-gate static int
ufs_getsecattr(struct vnode * vp,vsecattr_t * vsap,int flag,struct cred * cr,caller_context_t * ct)62797c478bd9Sstevel@tonic-gate ufs_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag,
628080d5689fSPatrick Mooney     struct cred *cr, caller_context_t *ct)
62817c478bd9Sstevel@tonic-gate {
62827c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
62837c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp;
62847c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
62857c478bd9Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
62867c478bd9Sstevel@tonic-gate 	int		err = EINVAL;
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 	/*
62917c478bd9Sstevel@tonic-gate 	 * Only grab locks if needed - they're not needed to check vsa_mask
62927c478bd9Sstevel@tonic-gate 	 * or if the mask contains no acl flags.
62937c478bd9Sstevel@tonic-gate 	 */
62947c478bd9Sstevel@tonic-gate 	if (vsa_mask != 0) {
62957c478bd9Sstevel@tonic-gate 		if (err = ufs_lockfs_begin(ufsvfsp, &ulp,
62967c478bd9Sstevel@tonic-gate 		    ULOCKFS_GETATTR_MASK))
62977c478bd9Sstevel@tonic-gate 			return (err);
62987c478bd9Sstevel@tonic-gate 
62997c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_contents, RW_READER);
63007c478bd9Sstevel@tonic-gate 		err = ufs_acl_get(ip, vsap, flag, cr);
63017c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
63027c478bd9Sstevel@tonic-gate 
63037c478bd9Sstevel@tonic-gate 		if (ulp)
63047c478bd9Sstevel@tonic-gate 			ufs_lockfs_end(ulp);
63057c478bd9Sstevel@tonic-gate 	}
63067c478bd9Sstevel@tonic-gate 	return (err);
63077c478bd9Sstevel@tonic-gate }
63087c478bd9Sstevel@tonic-gate 
63097c478bd9Sstevel@tonic-gate /* ARGSUSED */
63107c478bd9Sstevel@tonic-gate static int
ufs_setsecattr(struct vnode * vp,vsecattr_t * vsap,int flag,struct cred * cr,caller_context_t * ct)6311da6c28aaSamw ufs_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr,
631280d5689fSPatrick Mooney     caller_context_t *ct)
63137c478bd9Sstevel@tonic-gate {
63147c478bd9Sstevel@tonic-gate 	struct inode	*ip = VTOI(vp);
63157c478bd9Sstevel@tonic-gate 	struct ulockfs	*ulp = NULL;
63167c478bd9Sstevel@tonic-gate 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
63177c478bd9Sstevel@tonic-gate 	ulong_t		vsa_mask = vsap->vsa_mask;
63187c478bd9Sstevel@tonic-gate 	int		err;
63197c478bd9Sstevel@tonic-gate 	int		haverwlock = 1;
63207c478bd9Sstevel@tonic-gate 	int		trans_size;
63217c478bd9Sstevel@tonic-gate 	int		donetrans = 0;
63227c478bd9Sstevel@tonic-gate 	int		retry = 1;
63237c478bd9Sstevel@tonic-gate 
63247c478bd9Sstevel@tonic-gate 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
63257c478bd9Sstevel@tonic-gate 
63267c478bd9Sstevel@tonic-gate 	/* Abort now if the request is either empty or invalid. */
63277c478bd9Sstevel@tonic-gate 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
63287c478bd9Sstevel@tonic-gate 	if ((vsa_mask == 0) ||
63297c478bd9Sstevel@tonic-gate 	    ((vsap->vsa_aclentp == NULL) &&
63307c478bd9Sstevel@tonic-gate 	    (vsap->vsa_dfaclentp == NULL))) {
63317c478bd9Sstevel@tonic-gate 		err = EINVAL;
63327c478bd9Sstevel@tonic-gate 		goto out;
63337c478bd9Sstevel@tonic-gate 	}
63347c478bd9Sstevel@tonic-gate 
63357c478bd9Sstevel@tonic-gate 	/*
63367c478bd9Sstevel@tonic-gate 	 * Following convention, if this is a directory then we acquire the
63377c478bd9Sstevel@tonic-gate 	 * inode's i_rwlock after starting a UFS logging transaction;
63387c478bd9Sstevel@tonic-gate 	 * otherwise, we acquire it beforehand. Since we were called (and
63397c478bd9Sstevel@tonic-gate 	 * must therefore return) with the lock held, we will have to drop it,
63407c478bd9Sstevel@tonic-gate 	 * and later reacquire it, if operating on a directory.
63417c478bd9Sstevel@tonic-gate 	 */
63427c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
63437c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_rwlock);
63447c478bd9Sstevel@tonic-gate 		haverwlock = 0;
63457c478bd9Sstevel@tonic-gate 	} else {
63467c478bd9Sstevel@tonic-gate 		/* Upgrade the lock if required. */
63477c478bd9Sstevel@tonic-gate 		if (!rw_write_held(&ip->i_rwlock)) {
63487c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
63497c478bd9Sstevel@tonic-gate 			rw_enter(&ip->i_rwlock, RW_WRITER);
63507c478bd9Sstevel@tonic-gate 		}
63517c478bd9Sstevel@tonic-gate 	}
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate again:
63547c478bd9Sstevel@tonic-gate 	ASSERT(!(vp->v_type == VDIR && haverwlock));
63557c478bd9Sstevel@tonic-gate 	if (err = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK)) {
63567c478bd9Sstevel@tonic-gate 		ulp = NULL;
63577c478bd9Sstevel@tonic-gate 		retry = 0;
63587c478bd9Sstevel@tonic-gate 		goto out;
63597c478bd9Sstevel@tonic-gate 	}
63607c478bd9Sstevel@tonic-gate 
63617c478bd9Sstevel@tonic-gate 	/*
63627c478bd9Sstevel@tonic-gate 	 * Check that the file system supports this operation. Note that
63637c478bd9Sstevel@tonic-gate 	 * ufs_lockfs_begin() will have checked that the file system had
63647c478bd9Sstevel@tonic-gate 	 * not been forcibly unmounted.
63657c478bd9Sstevel@tonic-gate 	 */
63667c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_fs->fs_ronly) {
63677c478bd9Sstevel@tonic-gate 		err = EROFS;
63687c478bd9Sstevel@tonic-gate 		goto out;
63697c478bd9Sstevel@tonic-gate 	}
63707c478bd9Sstevel@tonic-gate 	if (ufsvfsp->vfs_nosetsec) {
63717c478bd9Sstevel@tonic-gate 		err = ENOSYS;
63727c478bd9Sstevel@tonic-gate 		goto out;
63737c478bd9Sstevel@tonic-gate 	}
63747c478bd9Sstevel@tonic-gate 
63757c478bd9Sstevel@tonic-gate 	if (ulp) {
63767c478bd9Sstevel@tonic-gate 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SETSECATTR,
637780d34432Sfrankho 		    trans_size = TOP_SETSECATTR_SIZE(VTOI(vp)));
63787c478bd9Sstevel@tonic-gate 		donetrans = 1;
63797c478bd9Sstevel@tonic-gate 	}
63807c478bd9Sstevel@tonic-gate 
63817c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR) {
63827c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_WRITER);
63837c478bd9Sstevel@tonic-gate 		haverwlock = 1;
63847c478bd9Sstevel@tonic-gate 	}
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 	ASSERT(haverwlock);
63877c478bd9Sstevel@tonic-gate 
63887c478bd9Sstevel@tonic-gate 	/* Do the actual work. */
63897c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_WRITER);
63907c478bd9Sstevel@tonic-gate 	/*
63917c478bd9Sstevel@tonic-gate 	 * Suppress out of inodes messages if we will retry.
63927c478bd9Sstevel@tonic-gate 	 */
63937c478bd9Sstevel@tonic-gate 	if (retry)
63947c478bd9Sstevel@tonic-gate 		ip->i_flag |= IQUIET;
63957c478bd9Sstevel@tonic-gate 	err = ufs_acl_set(ip, vsap, flag, cr);
63967c478bd9Sstevel@tonic-gate 	ip->i_flag &= ~IQUIET;
63977c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
63987c478bd9Sstevel@tonic-gate 
63997c478bd9Sstevel@tonic-gate out:
64007c478bd9Sstevel@tonic-gate 	if (ulp) {
64017c478bd9Sstevel@tonic-gate 		if (donetrans) {
64027c478bd9Sstevel@tonic-gate 			/*
64037c478bd9Sstevel@tonic-gate 			 * top_end_async() can eventually call
64047c478bd9Sstevel@tonic-gate 			 * top_end_sync(), which can block. We must
64057c478bd9Sstevel@tonic-gate 			 * therefore observe the lock-ordering protocol
64067c478bd9Sstevel@tonic-gate 			 * here as well.
64077c478bd9Sstevel@tonic-gate 			 */
64087c478bd9Sstevel@tonic-gate 			if (vp->v_type == VDIR) {
64097c478bd9Sstevel@tonic-gate 				rw_exit(&ip->i_rwlock);
64107c478bd9Sstevel@tonic-gate 				haverwlock = 0;
64117c478bd9Sstevel@tonic-gate 			}
64127c478bd9Sstevel@tonic-gate 			TRANS_END_ASYNC(ufsvfsp, TOP_SETSECATTR, trans_size);
64137c478bd9Sstevel@tonic-gate 		}
64147c478bd9Sstevel@tonic-gate 		ufs_lockfs_end(ulp);
64157c478bd9Sstevel@tonic-gate 	}
64167c478bd9Sstevel@tonic-gate 	/*
64177c478bd9Sstevel@tonic-gate 	 * If no inodes available, try scaring a logically-
64187c478bd9Sstevel@tonic-gate 	 * free one out of the delete queue to someplace
64197c478bd9Sstevel@tonic-gate 	 * that we can find it.
64207c478bd9Sstevel@tonic-gate 	 */
64217c478bd9Sstevel@tonic-gate 	if ((err == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
64227c478bd9Sstevel@tonic-gate 		ufs_delete_drain_wait(ufsvfsp, 1);
64237c478bd9Sstevel@tonic-gate 		retry = 0;
64247c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR && haverwlock) {
64257c478bd9Sstevel@tonic-gate 			rw_exit(&ip->i_rwlock);
64267c478bd9Sstevel@tonic-gate 			haverwlock = 0;
64277c478bd9Sstevel@tonic-gate 		}
64287c478bd9Sstevel@tonic-gate 		goto again;
64297c478bd9Sstevel@tonic-gate 	}
64307c478bd9Sstevel@tonic-gate 	/*
64317c478bd9Sstevel@tonic-gate 	 * If we need to reacquire the lock then it is safe to do so
64327c478bd9Sstevel@tonic-gate 	 * as a reader. This is because ufs_rwunlock(), which will be
64337c478bd9Sstevel@tonic-gate 	 * called by our caller after we return, does not differentiate
64347c478bd9Sstevel@tonic-gate 	 * between shared and exclusive locks.
64357c478bd9Sstevel@tonic-gate 	 */
64367c478bd9Sstevel@tonic-gate 	if (!haverwlock) {
64377c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VDIR);
64387c478bd9Sstevel@tonic-gate 		rw_enter(&ip->i_rwlock, RW_READER);
64397c478bd9Sstevel@tonic-gate 	}
64407c478bd9Sstevel@tonic-gate 
64417c478bd9Sstevel@tonic-gate 	return (err);
64427c478bd9Sstevel@tonic-gate }
64439b5097eeSOwen Roberts 
64449b5097eeSOwen Roberts /*
64459b5097eeSOwen Roberts  * Locate the vnode to be used for an event notification. As this will
64469b5097eeSOwen Roberts  * be called prior to the name space change perform basic verification
64479b5097eeSOwen Roberts  * that the change will be allowed.
64489b5097eeSOwen Roberts  */
64499b5097eeSOwen Roberts 
64509b5097eeSOwen Roberts static int
ufs_eventlookup(struct vnode * dvp,char * nm,struct cred * cr,struct vnode ** vpp)64519b5097eeSOwen Roberts ufs_eventlookup(struct vnode *dvp, char *nm, struct cred *cr,
64529b5097eeSOwen Roberts     struct vnode **vpp)
64539b5097eeSOwen Roberts {
64549b5097eeSOwen Roberts 	int	namlen;
64559b5097eeSOwen Roberts 	int	error;
64569b5097eeSOwen Roberts 	struct vnode	*vp;
64579b5097eeSOwen Roberts 	struct inode	*ip;
64589b5097eeSOwen Roberts 	struct inode	*xip;
64599b5097eeSOwen Roberts 	struct ufsvfs	*ufsvfsp;
64609b5097eeSOwen Roberts 	struct ulockfs	*ulp;
64619b5097eeSOwen Roberts 
64629b5097eeSOwen Roberts 	ip = VTOI(dvp);
64639b5097eeSOwen Roberts 	*vpp = NULL;
64649b5097eeSOwen Roberts 
64659b5097eeSOwen Roberts 	if ((namlen = strlen(nm)) == 0)
64669b5097eeSOwen Roberts 		return (EINVAL);
64679b5097eeSOwen Roberts 
64689b5097eeSOwen Roberts 	if (nm[0] == '.') {
64699b5097eeSOwen Roberts 		if (namlen == 1)
64709b5097eeSOwen Roberts 			return (EINVAL);
64719b5097eeSOwen Roberts 		else if ((namlen == 2) && nm[1] == '.') {
64729b5097eeSOwen Roberts 			return (EEXIST);
64739b5097eeSOwen Roberts 		}
64749b5097eeSOwen Roberts 	}
64759b5097eeSOwen Roberts 
64769b5097eeSOwen Roberts 	/*
64779b5097eeSOwen Roberts 	 * Check accessibility and write access of parent directory as we
64789b5097eeSOwen Roberts 	 * only want to post the event if we're able to make a change.
64799b5097eeSOwen Roberts 	 */
64809b5097eeSOwen Roberts 	if (error = ufs_diraccess(ip, IEXEC|IWRITE, cr))
64819b5097eeSOwen Roberts 		return (error);
64829b5097eeSOwen Roberts 
64839b5097eeSOwen Roberts 	if (vp = dnlc_lookup(dvp, nm)) {
64849b5097eeSOwen Roberts 		if (vp == DNLC_NO_VNODE) {
64859b5097eeSOwen Roberts 			VN_RELE(vp);
64869b5097eeSOwen Roberts 			return (ENOENT);
64879b5097eeSOwen Roberts 		}
64889b5097eeSOwen Roberts 
64899b5097eeSOwen Roberts 		*vpp = vp;
64909b5097eeSOwen Roberts 		return (0);
64919b5097eeSOwen Roberts 	}
64929b5097eeSOwen Roberts 
64939b5097eeSOwen Roberts 	/*
64949b5097eeSOwen Roberts 	 * Keep the idle queue from getting too long by idling two
64959b5097eeSOwen Roberts 	 * inodes before attempting to allocate another.
64969b5097eeSOwen Roberts 	 * This operation must be performed before entering lockfs
64979b5097eeSOwen Roberts 	 * or a transaction.
64989b5097eeSOwen Roberts 	 */
64999b5097eeSOwen Roberts 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
65009b5097eeSOwen Roberts 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
65019b5097eeSOwen Roberts 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
65029b5097eeSOwen Roberts 			ufs_idle_some(ufs_lookup_idle_count);
65039b5097eeSOwen Roberts 		}
65049b5097eeSOwen Roberts 
65059b5097eeSOwen Roberts 	ufsvfsp = ip->i_ufsvfs;
65069b5097eeSOwen Roberts 
65079b5097eeSOwen Roberts retry_lookup:
65089b5097eeSOwen Roberts 	if (error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK))
65099b5097eeSOwen Roberts 		return (error);
65109b5097eeSOwen Roberts 
65119b5097eeSOwen Roberts 	if ((error = ufs_dirlook(ip, nm, &xip, cr, 1, 1)) == 0) {
65129b5097eeSOwen Roberts 		vp = ITOV(xip);
65139b5097eeSOwen Roberts 		*vpp = vp;
65149b5097eeSOwen Roberts 	}
65159b5097eeSOwen Roberts 
65169b5097eeSOwen Roberts 	if (ulp) {
65179b5097eeSOwen Roberts 		ufs_lockfs_end(ulp);
65189b5097eeSOwen Roberts 	}
65199b5097eeSOwen Roberts 
65209b5097eeSOwen Roberts 	if (error == EAGAIN)
65219b5097eeSOwen Roberts 		goto retry_lookup;
65229b5097eeSOwen Roberts 
65239b5097eeSOwen Roberts 	return (error);
65249b5097eeSOwen Roberts }
6525