xref: /titanic_41/usr/src/uts/common/fs/ufs/ufs_vnops.c (revision 31ceb98b622e1a310256f4c4a1472beb92046db3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/types.h>
37 #include <sys/t_lock.h>
38 #include <sys/ksynch.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/systm.h>
42 #include <sys/sysmacros.h>
43 #include <sys/resource.h>
44 #include <sys/signal.h>
45 #include <sys/cred.h>
46 #include <sys/user.h>
47 #include <sys/buf.h>
48 #include <sys/vfs.h>
49 #include <sys/vfs_opreg.h>
50 #include <sys/vnode.h>
51 #include <sys/proc.h>
52 #include <sys/disp.h>
53 #include <sys/file.h>
54 #include <sys/fcntl.h>
55 #include <sys/flock.h>
56 #include <sys/atomic.h>
57 #include <sys/kmem.h>
58 #include <sys/uio.h>
59 #include <sys/dnlc.h>
60 #include <sys/conf.h>
61 #include <sys/mman.h>
62 #include <sys/pathname.h>
63 #include <sys/debug.h>
64 #include <sys/vmsystm.h>
65 #include <sys/cmn_err.h>
66 #include <sys/filio.h>
67 #include <sys/policy.h>
68 
69 #include <sys/fs/ufs_fs.h>
70 #include <sys/fs/ufs_lockfs.h>
71 #include <sys/fs/ufs_filio.h>
72 #include <sys/fs/ufs_inode.h>
73 #include <sys/fs/ufs_fsdir.h>
74 #include <sys/fs/ufs_quota.h>
75 #include <sys/fs/ufs_log.h>
76 #include <sys/fs/ufs_snap.h>
77 #include <sys/fs/ufs_trans.h>
78 #include <sys/fs/ufs_panic.h>
79 #include <sys/fs/ufs_bio.h>
80 #include <sys/dirent.h>		/* must be AFTER <sys/fs/fsdir.h>! */
81 #include <sys/errno.h>
82 #include <sys/fssnap_if.h>
83 #include <sys/unistd.h>
84 #include <sys/sunddi.h>
85 
86 #include <sys/filio.h>		/* _FIOIO */
87 
88 #include <vm/hat.h>
89 #include <vm/page.h>
90 #include <vm/pvn.h>
91 #include <vm/as.h>
92 #include <vm/seg.h>
93 #include <vm/seg_map.h>
94 #include <vm/seg_vn.h>
95 #include <vm/seg_kmem.h>
96 #include <vm/rm.h>
97 #include <sys/swap.h>
98 
99 #include <fs/fs_subr.h>
100 
101 static struct instats ins;
102 
103 static 	int ufs_getpage_ra(struct vnode *, u_offset_t, struct seg *, caddr_t);
104 static	int ufs_getpage_miss(struct vnode *, u_offset_t, size_t, struct seg *,
105 		caddr_t, struct page **, size_t, enum seg_rw, int);
106 static	int ufs_open(struct vnode **, int, struct cred *);
107 static	int ufs_close(struct vnode *, int, int, offset_t, struct cred *);
108 static	int ufs_read(struct vnode *, struct uio *, int, struct cred *,
109 			struct caller_context *);
110 static	int ufs_write(struct vnode *, struct uio *, int, struct cred *,
111 			struct caller_context *);
112 static	int ufs_ioctl(struct vnode *, int, intptr_t, int, struct cred *, int *);
113 static	int ufs_getattr(struct vnode *, struct vattr *, int, struct cred *);
114 static	int ufs_setattr(struct vnode *, struct vattr *, int, struct cred *,
115 			caller_context_t *);
116 static	int ufs_access(struct vnode *, int, int, struct cred *);
117 static	int ufs_lookup(struct vnode *, char *, struct vnode **,
118 		struct pathname *, int, struct vnode *, struct cred *);
119 static	int ufs_create(struct vnode *, char *, struct vattr *, enum vcexcl,
120 			int, struct vnode **, struct cred *, int);
121 static	int ufs_remove(struct vnode *, char *, struct cred *);
122 static	int ufs_link(struct vnode *, struct vnode *, char *, struct cred *);
123 static	int ufs_rename(struct vnode *, char *, struct vnode *, char *,
124 			struct cred *);
125 static	int ufs_mkdir(struct vnode *, char *, struct vattr *, struct vnode **,
126 			struct cred *);
127 static	int ufs_rmdir(struct vnode *, char *, struct vnode *, struct cred *);
128 static	int ufs_readdir(struct vnode *, struct uio *, struct cred *, int *);
129 static	int ufs_symlink(struct vnode *, char *, struct vattr *, char *,
130 			struct cred *);
131 static	int ufs_readlink(struct vnode *, struct uio *, struct cred *);
132 static	int ufs_fsync(struct vnode *, int, struct cred *);
133 static	void ufs_inactive(struct vnode *, struct cred *);
134 static	int ufs_fid(struct vnode *, struct fid *);
135 static	int ufs_rwlock(struct vnode *, int, caller_context_t *);
136 static	void ufs_rwunlock(struct vnode *, int, caller_context_t *);
137 static	int ufs_seek(struct vnode *, offset_t, offset_t *);
138 static	int ufs_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
139 			struct flk_callback *, struct cred *);
140 static  int ufs_space(struct vnode *, int, struct flock64 *, int, offset_t,
141 		cred_t *, caller_context_t *);
142 static	int ufs_getpage(struct vnode *, offset_t, size_t, uint_t *,
143 		struct page **, size_t, struct seg *, caddr_t,
144 		enum seg_rw, struct cred *);
145 static	int ufs_putpage(struct vnode *, offset_t, size_t, int, struct cred *);
146 static	int ufs_putpages(struct vnode *, offset_t, size_t, int, struct cred *);
147 static	int ufs_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
148 			uchar_t, uchar_t, uint_t, struct cred *);
149 static	int ufs_addmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
150 			uchar_t, uchar_t, uint_t, struct cred *);
151 static	int ufs_delmap(struct vnode *, offset_t, struct as *, caddr_t,  size_t,
152 			uint_t, uint_t, uint_t, struct cred *);
153 static	int ufs_poll(vnode_t *, short, int, short *, struct pollhead **);
154 static	int ufs_dump(vnode_t *, caddr_t, int, int);
155 static	int ufs_l_pathconf(struct vnode *, int, ulong_t *, struct cred *);
156 static	int ufs_pageio(struct vnode *, struct page *, u_offset_t, size_t, int,
157 			struct cred *);
158 static	int ufs_dump(vnode_t *, caddr_t, int, int);
159 static	int ufs_dumpctl(vnode_t *, int, int *);
160 static	daddr32_t *save_dblks(struct inode *, struct ufsvfs *, daddr32_t *,
161 			daddr32_t *, int, int);
162 static	int ufs_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
163 static	int ufs_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
164 
165 extern int as_map_locked(struct as *, caddr_t, size_t, int ((*)()), void *);
166 
167 /*
168  * For lockfs: ulockfs begin/end is now inlined in the ufs_xxx functions.
169  *
170  * XXX - ULOCKFS in fs_pathconf and ufs_ioctl is not inlined yet.
171  */
172 struct vnodeops *ufs_vnodeops;
173 
174 /* NOTE: "not blkd" below  means that the operation isn't blocked by lockfs */
175 const fs_operation_def_t ufs_vnodeops_template[] = {
176 	VOPNAME_OPEN,		{ .vop_open = ufs_open },	/* not blkd */
177 	VOPNAME_CLOSE,		{ .vop_close = ufs_close },	/* not blkd */
178 	VOPNAME_READ,		{ .vop_read = ufs_read },
179 	VOPNAME_WRITE,		{ .vop_write = ufs_write },
180 	VOPNAME_IOCTL,		{ .vop_ioctl = ufs_ioctl },
181 	VOPNAME_GETATTR,	{ .vop_getattr = ufs_getattr },
182 	VOPNAME_SETATTR,	{ .vop_setattr = ufs_setattr },
183 	VOPNAME_ACCESS,		{ .vop_access = ufs_access },
184 	VOPNAME_LOOKUP,		{ .vop_lookup = ufs_lookup },
185 	VOPNAME_CREATE,		{ .vop_create = ufs_create },
186 	VOPNAME_REMOVE,		{ .vop_remove = ufs_remove },
187 	VOPNAME_LINK,		{ .vop_link = ufs_link },
188 	VOPNAME_RENAME,		{ .vop_rename = ufs_rename },
189 	VOPNAME_MKDIR,		{ .vop_mkdir = ufs_mkdir },
190 	VOPNAME_RMDIR,		{ .vop_rmdir = ufs_rmdir },
191 	VOPNAME_READDIR,	{ .vop_readdir = ufs_readdir },
192 	VOPNAME_SYMLINK,	{ .vop_symlink = ufs_symlink },
193 	VOPNAME_READLINK,	{ .vop_readlink = ufs_readlink },
194 	VOPNAME_FSYNC,		{ .vop_fsync = ufs_fsync },
195 	VOPNAME_INACTIVE,	{ .vop_inactive = ufs_inactive }, /* not blkd */
196 	VOPNAME_FID,		{ .vop_fid = ufs_fid },
197 	VOPNAME_RWLOCK,		{ .vop_rwlock = ufs_rwlock },	/* not blkd */
198 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = ufs_rwunlock }, /* not blkd */
199 	VOPNAME_SEEK,		{ .vop_seek = ufs_seek },
200 	VOPNAME_FRLOCK,		{ .vop_frlock = ufs_frlock },
201 	VOPNAME_SPACE,		{ .vop_space = ufs_space },
202 	VOPNAME_GETPAGE,	{ .vop_getpage = ufs_getpage },
203 	VOPNAME_PUTPAGE,	{ .vop_putpage = ufs_putpage },
204 	VOPNAME_MAP,		{ .vop_map = ufs_map },
205 	VOPNAME_ADDMAP,		{ .vop_addmap = ufs_addmap },	/* not blkd */
206 	VOPNAME_DELMAP,		{ .vop_delmap = ufs_delmap },	/* not blkd */
207 	VOPNAME_POLL,		{ .vop_poll = ufs_poll },	/* not blkd */
208 	VOPNAME_DUMP,		{ .vop_dump = ufs_dump },
209 	VOPNAME_PATHCONF,	{ .vop_pathconf = ufs_l_pathconf },
210 	VOPNAME_PAGEIO,		{ .vop_pageio = ufs_pageio },
211 	VOPNAME_DUMPCTL,	{ .vop_dumpctl = ufs_dumpctl },
212 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = ufs_getsecattr },
213 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = ufs_setsecattr },
214 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
215 	NULL,			NULL
216 };
217 
218 #define	MAX_BACKFILE_COUNT	9999
219 
220 /*
221  * Created by ufs_dumpctl() to store a file's disk block info into memory.
222  * Used by ufs_dump() to dump data to disk directly.
223  */
224 struct dump {
225 	struct inode	*ip;		/* the file we contain */
226 	daddr_t		fsbs;		/* number of blocks stored */
227 	struct timeval32 time;		/* time stamp for the struct */
228 	daddr32_t 	dblk[1];	/* place holder for block info */
229 };
230 
231 static struct dump *dump_info = NULL;
232 
233 /*
234  * Previously there was no special action required for ordinary files.
235  * (Devices are handled through the device file system.)
236  * Now we support Large Files and Large File API requires open to
237  * fail if file is large.
238  * We could take care to prevent data corruption
239  * by doing an atomic check of size and truncate if file is opened with
240  * FTRUNC flag set but traditionally this is being done by the vfs/vnode
241  * layers. So taking care of truncation here is a change in the existing
242  * semantics of VOP_OPEN and therefore we chose not to implement any thing
243  * here. The check for the size of the file > 2GB is being done at the
244  * vfs layer in routine vn_open().
245  */
246 
247 /* ARGSUSED */
248 static int
249 ufs_open(struct vnode **vpp, int flag, struct cred *cr)
250 {
251 	return (0);
252 }
253 
254 /*ARGSUSED*/
255 static int
256 ufs_close(struct vnode *vp, int flag, int count, offset_t offset,
257 	struct cred *cr)
258 {
259 	cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
260 	cleanshares(vp, ttoproc(curthread)->p_pid);
261 
262 	/*
263 	 * Push partially filled cluster at last close.
264 	 * ``last close'' is approximated because the dnlc
265 	 * may have a hold on the vnode.
266 	 * Checking for VBAD here will also act as a forced umount check.
267 	 */
268 	if (vp->v_count <= 2 && vp->v_type != VBAD) {
269 		struct inode *ip = VTOI(vp);
270 		if (ip->i_delaylen) {
271 			ins.in_poc.value.ul++;
272 			(void) ufs_putpages(vp, ip->i_delayoff, ip->i_delaylen,
273 			    B_ASYNC | B_FREE, cr);
274 			ip->i_delaylen = 0;
275 		}
276 	}
277 
278 	return (0);
279 }
280 
281 /*ARGSUSED*/
282 static int
283 ufs_read(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cr,
284 	struct caller_context *ct)
285 {
286 	struct inode *ip = VTOI(vp);
287 	struct ufsvfs *ufsvfsp;
288 	struct ulockfs *ulp = NULL;
289 	int error = 0;
290 	int intrans = 0;
291 
292 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
293 
294 	/*
295 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
296 	 * and TRANS_BEGIN_SYNC() calls since mandatory locks can sleep.
297 	 */
298 	if (MANDLOCK(vp, ip->i_mode)) {
299 		/*
300 		 * ufs_getattr ends up being called by chklock
301 		 */
302 		error = chklock(vp, FREAD, uiop->uio_loffset,
303 		    uiop->uio_resid, uiop->uio_fmode, ct);
304 		if (error)
305 			goto out;
306 	}
307 
308 	ufsvfsp = ip->i_ufsvfs;
309 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READ_MASK);
310 	if (error)
311 		goto out;
312 
313 	/*
314 	 * In the case that a directory is opened for reading as a file
315 	 * (eg "cat .") with the  O_RSYNC, O_SYNC and O_DSYNC flags set.
316 	 * The locking order had to be changed to avoid a deadlock with
317 	 * an update taking place on that directory at the same time.
318 	 */
319 	if ((ip->i_mode & IFMT) == IFDIR) {
320 
321 		rw_enter(&ip->i_contents, RW_READER);
322 		error = rdip(ip, uiop, ioflag, cr);
323 		rw_exit(&ip->i_contents);
324 
325 		if (error) {
326 			if (ulp)
327 				ufs_lockfs_end(ulp);
328 			goto out;
329 		}
330 
331 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
332 		    TRANS_ISTRANS(ufsvfsp)) {
333 			rw_exit(&ip->i_rwlock);
334 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
335 			    error);
336 			ASSERT(!error);
337 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
338 			    TOP_READ_SIZE);
339 			rw_enter(&ip->i_rwlock, RW_READER);
340 		}
341 	} else {
342 		/*
343 		 * Only transact reads to files opened for sync-read and
344 		 * sync-write on a file system that is not write locked.
345 		 *
346 		 * The ``not write locked'' check prevents problems with
347 		 * enabling/disabling logging on a busy file system.  E.g.,
348 		 * logging exists at the beginning of the read but does not
349 		 * at the end.
350 		 *
351 		 */
352 		if (ulp && (ioflag & FRSYNC) && (ioflag & (FSYNC | FDSYNC)) &&
353 		    TRANS_ISTRANS(ufsvfsp)) {
354 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_READ_SYNC, TOP_READ_SIZE,
355 			    error);
356 			ASSERT(!error);
357 			intrans = 1;
358 		}
359 
360 		rw_enter(&ip->i_contents, RW_READER);
361 		error = rdip(ip, uiop, ioflag, cr);
362 		rw_exit(&ip->i_contents);
363 
364 		if (intrans) {
365 			TRANS_END_SYNC(ufsvfsp, error, TOP_READ_SYNC,
366 			    TOP_READ_SIZE);
367 		}
368 	}
369 
370 	if (ulp) {
371 		ufs_lockfs_end(ulp);
372 	}
373 out:
374 
375 	return (error);
376 }
377 
378 extern	int	ufs_HW;		/* high water mark */
379 extern	int	ufs_LW;		/* low water mark */
380 int	ufs_WRITES = 1;		/* XXX - enable/disable */
381 int	ufs_throttles = 0;	/* throttling count */
382 int	ufs_allow_shared_writes = 1;	/* directio shared writes */
383 
384 static int
385 ufs_check_rewrite(struct inode *ip, struct uio *uiop, int ioflag)
386 {
387 	int	shared_write;
388 
389 	/*
390 	 * If the FDSYNC flag is set then ignore the global
391 	 * ufs_allow_shared_writes in this case.
392 	 */
393 	shared_write = (ioflag & FDSYNC) | ufs_allow_shared_writes;
394 
395 	/*
396 	 * Filter to determine if this request is suitable as a
397 	 * concurrent rewrite. This write must not allocate blocks
398 	 * by extending the file or filling in holes. No use trying
399 	 * through FSYNC descriptors as the inode will be synchronously
400 	 * updated after the write. The uio structure has not yet been
401 	 * checked for sanity, so assume nothing.
402 	 */
403 	return (((ip->i_mode & IFMT) == IFREG) && !(ioflag & FAPPEND) &&
404 	    (uiop->uio_loffset >= (offset_t)0) &&
405 	    (uiop->uio_loffset < ip->i_size) && (uiop->uio_resid > 0) &&
406 	    ((ip->i_size - uiop->uio_loffset) >= uiop->uio_resid) &&
407 	    !(ioflag & FSYNC) && !bmap_has_holes(ip) &&
408 	    shared_write);
409 }
410 
411 /*ARGSUSED*/
412 static int
413 ufs_write(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cr,
414 	caller_context_t *ct)
415 {
416 	struct inode *ip = VTOI(vp);
417 	struct ufsvfs *ufsvfsp;
418 	struct ulockfs *ulp;
419 	int retry = 1;
420 	int error, resv, resid = 0;
421 	int directio_status;
422 	int exclusive;
423 	int rewriteflg;
424 	long start_resid = uiop->uio_resid;
425 
426 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
427 
428 retry_mandlock:
429 	/*
430 	 * Mandatory locking needs to be done before ufs_lockfs_begin()
431 	 * and TRANS_BEGIN_[A]SYNC() calls since mandatory locks can sleep.
432 	 * Check for forced unmounts normally done in ufs_lockfs_begin().
433 	 */
434 	if ((ufsvfsp = ip->i_ufsvfs) == NULL) {
435 		error = EIO;
436 		goto out;
437 	}
438 	if (MANDLOCK(vp, ip->i_mode)) {
439 
440 		ASSERT(RW_WRITE_HELD(&ip->i_rwlock));
441 
442 		/*
443 		 * ufs_getattr ends up being called by chklock
444 		 */
445 		error = chklock(vp, FWRITE, uiop->uio_loffset,
446 		    uiop->uio_resid, uiop->uio_fmode, ct);
447 		if (error)
448 			goto out;
449 	}
450 
451 	/* i_rwlock can change in chklock */
452 	exclusive = rw_write_held(&ip->i_rwlock);
453 	rewriteflg = ufs_check_rewrite(ip, uiop, ioflag);
454 
455 	/*
456 	 * Check for fast-path special case of directio re-writes.
457 	 */
458 	if ((ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) &&
459 	    !exclusive && rewriteflg) {
460 
461 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
462 		if (error)
463 			goto out;
464 
465 		rw_enter(&ip->i_contents, RW_READER);
466 		error = ufs_directio_write(ip, uiop, ioflag, 1, cr,
467 		    &directio_status);
468 		if (directio_status == DIRECTIO_SUCCESS) {
469 			uint_t i_flag_save;
470 
471 			if (start_resid != uiop->uio_resid)
472 				error = 0;
473 			/*
474 			 * Special treatment of access times for re-writes.
475 			 * If IMOD is not already set, then convert it
476 			 * to IMODACC for this operation. This defers
477 			 * entering a delta into the log until the inode
478 			 * is flushed. This mimics what is done for read
479 			 * operations and inode access time.
480 			 */
481 			mutex_enter(&ip->i_tlock);
482 			i_flag_save = ip->i_flag;
483 			ip->i_flag |= IUPD | ICHG;
484 			ip->i_seq++;
485 			ITIMES_NOLOCK(ip);
486 			if ((i_flag_save & IMOD) == 0) {
487 				ip->i_flag &= ~IMOD;
488 				ip->i_flag |= IMODACC;
489 			}
490 			mutex_exit(&ip->i_tlock);
491 			rw_exit(&ip->i_contents);
492 			if (ulp)
493 				ufs_lockfs_end(ulp);
494 			goto out;
495 		}
496 		rw_exit(&ip->i_contents);
497 		if (ulp)
498 			ufs_lockfs_end(ulp);
499 	}
500 
501 	if (!exclusive && !rw_tryupgrade(&ip->i_rwlock)) {
502 		rw_exit(&ip->i_rwlock);
503 		rw_enter(&ip->i_rwlock, RW_WRITER);
504 		/*
505 		 * Mandatory locking could have been enabled
506 		 * after dropping the i_rwlock.
507 		 */
508 		if (MANDLOCK(vp, ip->i_mode))
509 			goto retry_mandlock;
510 	}
511 
512 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_WRITE_MASK);
513 	if (error)
514 		goto out;
515 
516 	/*
517 	 * Amount of log space needed for this write
518 	 */
519 	if (!rewriteflg || !(ioflag & FDSYNC))
520 		TRANS_WRITE_RESV(ip, uiop, ulp, &resv, &resid);
521 
522 	/*
523 	 * Throttle writes.
524 	 */
525 	if (ufs_WRITES && (ip->i_writes > ufs_HW)) {
526 		mutex_enter(&ip->i_tlock);
527 		while (ip->i_writes > ufs_HW) {
528 			ufs_throttles++;
529 			cv_wait(&ip->i_wrcv, &ip->i_tlock);
530 		}
531 		mutex_exit(&ip->i_tlock);
532 	}
533 
534 	/*
535 	 * Enter Transaction
536 	 *
537 	 * If the write is a rewrite there is no need to open a transaction
538 	 * if the FDSYNC flag is set and not the FSYNC.  In this case just
539 	 * set the IMODACC flag to modify do the update at a later time
540 	 * thus avoiding the overhead of the logging transaction that is
541 	 * not required.
542 	 */
543 	if (ioflag & (FSYNC|FDSYNC)) {
544 		if (ulp) {
545 			if (rewriteflg) {
546 				uint_t i_flag_save;
547 
548 				rw_enter(&ip->i_contents, RW_READER);
549 				mutex_enter(&ip->i_tlock);
550 				i_flag_save = ip->i_flag;
551 				ip->i_flag |= IUPD | ICHG;
552 				ip->i_seq++;
553 				ITIMES_NOLOCK(ip);
554 				if ((i_flag_save & IMOD) == 0) {
555 					ip->i_flag &= ~IMOD;
556 					ip->i_flag |= IMODACC;
557 				}
558 				mutex_exit(&ip->i_tlock);
559 				rw_exit(&ip->i_contents);
560 			} else {
561 				int terr = 0;
562 				TRANS_BEGIN_SYNC(ufsvfsp, TOP_WRITE_SYNC, resv,
563 				    terr);
564 				ASSERT(!terr);
565 			}
566 		}
567 	} else {
568 		if (ulp)
569 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_WRITE, resv);
570 	}
571 
572 	/*
573 	 * Write the file
574 	 */
575 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
576 	rw_enter(&ip->i_contents, RW_WRITER);
577 	if ((ioflag & FAPPEND) != 0 && (ip->i_mode & IFMT) == IFREG) {
578 		/*
579 		 * In append mode start at end of file.
580 		 */
581 		uiop->uio_loffset = ip->i_size;
582 	}
583 
584 	/*
585 	 * Mild optimisation, don't call ufs_trans_write() unless we have to
586 	 * Also, suppress file system full messages if we will retry.
587 	 */
588 	if (retry)
589 		ip->i_flag |= IQUIET;
590 	if (resid) {
591 		TRANS_WRITE(ip, uiop, ioflag, error, ulp, cr, resv, resid);
592 	} else {
593 		error = wrip(ip, uiop, ioflag, cr);
594 	}
595 	ip->i_flag &= ~IQUIET;
596 
597 	rw_exit(&ip->i_contents);
598 	rw_exit(&ufsvfsp->vfs_dqrwlock);
599 
600 	/*
601 	 * Leave Transaction
602 	 */
603 	if (ulp) {
604 		if (ioflag & (FSYNC|FDSYNC)) {
605 			if (!rewriteflg) {
606 				int terr = 0;
607 
608 				TRANS_END_SYNC(ufsvfsp, terr, TOP_WRITE_SYNC,
609 				    resv);
610 				if (error == 0)
611 					error = terr;
612 			}
613 		} else {
614 			TRANS_END_ASYNC(ufsvfsp, TOP_WRITE, resv);
615 		}
616 		ufs_lockfs_end(ulp);
617 	}
618 out:
619 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
620 		/*
621 		 * Any blocks tied up in pending deletes?
622 		 */
623 		ufs_delete_drain_wait(ufsvfsp, 1);
624 		retry = 0;
625 		goto retry_mandlock;
626 	}
627 
628 	if (error == ENOSPC && (start_resid != uiop->uio_resid))
629 		error = 0;
630 
631 	return (error);
632 }
633 
634 /*
635  * Don't cache write blocks to files with the sticky bit set.
636  * Used to keep swap files from blowing the page cache on a server.
637  */
638 int stickyhack = 1;
639 
640 /*
641  * Free behind hacks.  The pager is busted.
642  * XXX - need to pass the information down to writedone() in a flag like B_SEQ
643  * or B_FREE_IF_TIGHT_ON_MEMORY.
644  */
645 int	freebehind = 1;
646 int	smallfile = 0;
647 u_offset_t smallfile64 = 32 * 1024;
648 
649 /*
650  * While we should, in most cases, cache the pages for write, we
651  * may also want to cache the pages for read as long as they are
652  * frequently re-usable.
653  *
654  * If cache_read_ahead = 1, the pages for read will go to the tail
655  * of the cache list when they are released, otherwise go to the head.
656  */
657 int	cache_read_ahead = 0;
658 
659 /*
660  * Freebehind exists  so that as we read  large files  sequentially we
661  * don't consume most of memory with pages  from a few files. It takes
662  * longer to re-read from disk multiple small files as it does reading
663  * one large one sequentially.  As system  memory grows customers need
664  * to retain bigger chunks   of files in  memory.   The advent of  the
665  * cachelist opens up of the possibility freeing pages  to the head or
666  * tail of the list.
667  *
668  * Not freeing a page is a bet that the page will be read again before
669  * it's segmap slot is needed for something else. If we loose the bet,
670  * it means some  other thread is  burdened with the  page free we did
671  * not do. If we win we save a free and reclaim.
672  *
673  * Freeing it at the tail  vs the head of cachelist  is a bet that the
674  * page will survive until the next  read.  It's also saying that this
675  * page is more likely to  be re-used than a  page freed some time ago
676  * and never reclaimed.
677  *
678  * Freebehind maintains a  range of  file offset [smallfile1; smallfile2]
679  *
680  *            0 < offset < smallfile1 : pages are not freed.
681  *   smallfile1 < offset < smallfile2 : pages freed to tail of cachelist.
682  *   smallfile2 < offset              : pages freed to head of cachelist.
683  *
684  * The range  is  computed  at most  once  per second  and  depends on
685  * freemem  and  ncpus_online.  Both parameters  are   bounded to be
686  * >= smallfile && >= smallfile64.
687  *
688  * smallfile1 = (free memory / ncpu) / 1000
689  * smallfile2 = (free memory / ncpu) / 10
690  *
691  * A few examples values:
692  *
693  *       Free Mem (in Bytes) [smallfile1; smallfile2]  [smallfile1; smallfile2]
694  *                                 ncpus_online = 4          ncpus_online = 64
695  *       ------------------  -----------------------   -----------------------
696  *             1G                   [256K;  25M]               [32K; 1.5M]
697  *            10G                   [2.5M; 250M]              [156K; 15M]
698  *           100G                    [25M; 2.5G]              [1.5M; 150M]
699  *
700  */
701 
702 #define	SMALLFILE1_D 1000
703 #define	SMALLFILE2_D 10
704 static u_offset_t smallfile1 = 32 * 1024;
705 static u_offset_t smallfile2 = 32 * 1024;
706 static clock_t smallfile_update = 0; /* lbolt value of when to recompute */
707 uint_t smallfile1_d = SMALLFILE1_D;
708 uint_t smallfile2_d = SMALLFILE2_D;
709 
710 /*
711  * wrip does the real work of write requests for ufs.
712  */
713 int
714 wrip(struct inode *ip, struct uio *uio, int ioflag, struct cred *cr)
715 {
716 	rlim64_t limit = uio->uio_llimit;
717 	u_offset_t off;
718 	u_offset_t old_i_size;
719 	struct fs *fs;
720 	struct vnode *vp;
721 	struct ufsvfs *ufsvfsp;
722 	caddr_t base;
723 	long start_resid = uio->uio_resid;	/* save starting resid */
724 	long premove_resid;			/* resid before uiomove() */
725 	uint_t flags;
726 	int newpage;
727 	int iupdat_flag, directio_status;
728 	int n, on, mapon;
729 	int error, pagecreate;
730 	int do_dqrwlock;		/* drop/reacquire vfs_dqrwlock */
731 	int32_t	iblocks;
732 	int	new_iblocks;
733 
734 	/*
735 	 * ip->i_size is incremented before the uiomove
736 	 * is done on a write.  If the move fails (bad user
737 	 * address) reset ip->i_size.
738 	 * The better way would be to increment ip->i_size
739 	 * only if the uiomove succeeds.
740 	 */
741 	int i_size_changed = 0;
742 	o_mode_t type;
743 	int i_seq_needed = 0;
744 
745 	vp = ITOV(ip);
746 
747 	/*
748 	 * check for forced unmount - should not happen as
749 	 * the request passed the lockfs checks.
750 	 */
751 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
752 		return (EIO);
753 
754 	fs = ip->i_fs;
755 
756 	ASSERT(RW_WRITE_HELD(&ip->i_contents));
757 
758 	/* check for valid filetype */
759 	type = ip->i_mode & IFMT;
760 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
761 	    (type != IFLNK) && (type != IFSHAD)) {
762 		return (EIO);
763 	}
764 
765 	/*
766 	 * the actual limit of UFS file size
767 	 * is UFS_MAXOFFSET_T
768 	 */
769 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
770 		limit = MAXOFFSET_T;
771 
772 	if (uio->uio_loffset >= limit) {
773 		proc_t *p = ttoproc(curthread);
774 
775 		mutex_enter(&p->p_lock);
776 		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
777 		    p, RCA_UNSAFE_SIGINFO);
778 		mutex_exit(&p->p_lock);
779 		return (EFBIG);
780 	}
781 
782 	/*
783 	 * if largefiles are disallowed, the limit is
784 	 * the pre-largefiles value of 2GB
785 	 */
786 	if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
787 		limit = MIN(UFS_MAXOFFSET_T, limit);
788 	else
789 		limit = MIN(MAXOFF32_T, limit);
790 
791 	if (uio->uio_loffset < (offset_t)0) {
792 		return (EINVAL);
793 	}
794 	if (uio->uio_resid == 0) {
795 		return (0);
796 	}
797 
798 	if (uio->uio_loffset >= limit)
799 		return (EFBIG);
800 
801 	ip->i_flag |= INOACC;	/* don't update ref time in getpage */
802 
803 	if (ioflag & (FSYNC|FDSYNC)) {
804 		ip->i_flag |= ISYNC;
805 		iupdat_flag = 1;
806 	}
807 	/*
808 	 * Try to go direct
809 	 */
810 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
811 		uio->uio_llimit = limit;
812 		error = ufs_directio_write(ip, uio, ioflag, 0, cr,
813 		    &directio_status);
814 		/*
815 		 * If ufs_directio wrote to the file or set the flags,
816 		 * we need to update i_seq, but it may be deferred.
817 		 */
818 		if (start_resid != uio->uio_resid ||
819 		    (ip->i_flag & (ICHG|IUPD))) {
820 			i_seq_needed = 1;
821 			ip->i_flag |= ISEQ;
822 		}
823 		if (directio_status == DIRECTIO_SUCCESS)
824 			goto out;
825 	}
826 
827 	/*
828 	 * Behavior with respect to dropping/reacquiring vfs_dqrwlock:
829 	 *
830 	 * o shadow inodes: vfs_dqrwlock is not held at all
831 	 * o quota updates: vfs_dqrwlock is read or write held
832 	 * o other updates: vfs_dqrwlock is read held
833 	 *
834 	 * The first case is the only one where we do not hold
835 	 * vfs_dqrwlock at all while entering wrip().
836 	 * We must make sure not to downgrade/drop vfs_dqrwlock if we
837 	 * have it as writer, i.e. if we are updating the quota inode.
838 	 * There is no potential deadlock scenario in this case as
839 	 * ufs_getpage() takes care of this and avoids reacquiring
840 	 * vfs_dqrwlock in that case.
841 	 *
842 	 * This check is done here since the above conditions do not change
843 	 * and we possibly loop below, so save a few cycles.
844 	 */
845 	if ((type == IFSHAD) ||
846 	    (rw_owner(&ufsvfsp->vfs_dqrwlock) == curthread)) {
847 			do_dqrwlock = 0;
848 	} else {
849 		do_dqrwlock = 1;
850 	}
851 
852 	/*
853 	 * Large Files: We cast MAXBMASK to offset_t
854 	 * inorder to mask out the higher bits. Since offset_t
855 	 * is a signed value, the high order bit set in MAXBMASK
856 	 * value makes it do the right thing by having all bits 1
857 	 * in the higher word. May be removed for _SOLARIS64_.
858 	 */
859 
860 	fs = ip->i_fs;
861 	do {
862 		u_offset_t uoff = uio->uio_loffset;
863 		off = uoff & (offset_t)MAXBMASK;
864 		mapon = (int)(uoff & (offset_t)MAXBOFFSET);
865 		on = (int)blkoff(fs, uoff);
866 		n = (int)MIN(fs->fs_bsize - on, uio->uio_resid);
867 		new_iblocks = 1;
868 
869 		if (type == IFREG && uoff + n >= limit) {
870 			if (uoff >= limit) {
871 				error = EFBIG;
872 				goto out;
873 			}
874 			/*
875 			 * since uoff + n >= limit,
876 			 * therefore n >= limit - uoff, and n is an int
877 			 * so it is safe to cast it to an int
878 			 */
879 			n = (int)(limit - (rlim64_t)uoff);
880 		}
881 		if (uoff + n > ip->i_size) {
882 			/*
883 			 * We are extending the length of the file.
884 			 * bmap is used so that we are sure that
885 			 * if we need to allocate new blocks, that it
886 			 * is done here before we up the file size.
887 			 */
888 			error = bmap_write(ip, uoff, (int)(on + n),
889 			    mapon == 0, NULL, cr);
890 			/*
891 			 * bmap_write never drops i_contents so if
892 			 * the flags are set it changed the file.
893 			 */
894 			if (ip->i_flag & (ICHG|IUPD)) {
895 				i_seq_needed = 1;
896 				ip->i_flag |= ISEQ;
897 			}
898 			if (error)
899 				break;
900 			/*
901 			 * There is a window of vulnerability here.
902 			 * The sequence of operations: allocate file
903 			 * system blocks, uiomove the data into pages,
904 			 * and then update the size of the file in the
905 			 * inode, must happen atomically.  However, due
906 			 * to current locking constraints, this can not
907 			 * be done.
908 			 */
909 			ASSERT(ip->i_writer == NULL);
910 			ip->i_writer = curthread;
911 			i_size_changed = 1;
912 			/*
913 			 * If we are writing from the beginning of
914 			 * the mapping, we can just create the
915 			 * pages without having to read them.
916 			 */
917 			pagecreate = (mapon == 0);
918 		} else if (n == MAXBSIZE) {
919 			/*
920 			 * Going to do a whole mappings worth,
921 			 * so we can just create the pages w/o
922 			 * having to read them in.  But before
923 			 * we do that, we need to make sure any
924 			 * needed blocks are allocated first.
925 			 */
926 			iblocks = ip->i_blocks;
927 			error = bmap_write(ip, uoff, (int)(on + n),
928 			    BI_ALLOC_ONLY, NULL, cr);
929 			/*
930 			 * bmap_write never drops i_contents so if
931 			 * the flags are set it changed the file.
932 			 */
933 			if (ip->i_flag & (ICHG|IUPD)) {
934 				i_seq_needed = 1;
935 				ip->i_flag |= ISEQ;
936 			}
937 			if (error)
938 				break;
939 			pagecreate = 1;
940 			/*
941 			 * check if the new created page needed the
942 			 * allocation of new disk blocks.
943 			 */
944 			if (iblocks == ip->i_blocks)
945 				new_iblocks = 0; /* no new blocks allocated */
946 		} else {
947 			pagecreate = 0;
948 			/*
949 			 * In sync mode flush the indirect blocks which
950 			 * may have been allocated and not written on
951 			 * disk. In above cases bmap_write will allocate
952 			 * in sync mode.
953 			 */
954 			if (ioflag & (FSYNC|FDSYNC)) {
955 				error = ufs_indirblk_sync(ip, uoff);
956 				if (error)
957 					break;
958 			}
959 		}
960 
961 		/*
962 		 * At this point we can enter ufs_getpage() in one
963 		 * of two ways:
964 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
965 		 *    forcefault parameter is true (pagecreate == 0)
966 		 * 2) uiomove() causes a page fault.
967 		 *
968 		 * We have to drop the contents lock to prevent the VM
969 		 * system from trying to reaquire it in ufs_getpage()
970 		 * should the uiomove cause a pagefault.
971 		 *
972 		 * We have to drop the reader vfs_dqrwlock here as well.
973 		 */
974 		rw_exit(&ip->i_contents);
975 		if (do_dqrwlock) {
976 			ASSERT(RW_LOCK_HELD(&ufsvfsp->vfs_dqrwlock));
977 			ASSERT(!(RW_WRITE_HELD(&ufsvfsp->vfs_dqrwlock)));
978 			rw_exit(&ufsvfsp->vfs_dqrwlock);
979 		}
980 
981 		newpage = 0;
982 		premove_resid = uio->uio_resid;
983 		if (vpm_enable) {
984 			/*
985 			 * Copy data. If new pages are created, part of
986 			 * the page that is not written will be initizliazed
987 			 * with zeros.
988 			 */
989 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
990 			    uio, !pagecreate, &newpage, 0, S_WRITE);
991 		} else {
992 
993 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
994 			    (uint_t)n, !pagecreate, S_WRITE);
995 
996 			/*
997 			 * segmap_pagecreate() returns 1 if it calls
998 			 * page_create_va() to allocate any pages.
999 			 */
1000 
1001 			if (pagecreate)
1002 				newpage = segmap_pagecreate(segkmap, base,
1003 				    (size_t)n, 0);
1004 
1005 			error = uiomove(base + mapon, (long)n, UIO_WRITE, uio);
1006 		}
1007 
1008 		/*
1009 		 * If "newpage" is set, then a new page was created and it
1010 		 * does not contain valid data, so it needs to be initialized
1011 		 * at this point.
1012 		 * Otherwise the page contains old data, which was overwritten
1013 		 * partially or as a whole in uiomove.
1014 		 * If there is only one iovec structure within uio, then
1015 		 * on error uiomove will not be able to update uio->uio_loffset
1016 		 * and we would zero the whole page here!
1017 		 *
1018 		 * If uiomove fails because of an error, the old valid data
1019 		 * is kept instead of filling the rest of the page with zero's.
1020 		 */
1021 		if (!vpm_enable && newpage &&
1022 		    uio->uio_loffset < roundup(off + mapon + n, PAGESIZE)) {
1023 			/*
1024 			 * We created pages w/o initializing them completely,
1025 			 * thus we need to zero the part that wasn't set up.
1026 			 * This happens on most EOF write cases and if
1027 			 * we had some sort of error during the uiomove.
1028 			 */
1029 			int nzero, nmoved;
1030 
1031 			nmoved = (int)(uio->uio_loffset - (off + mapon));
1032 			ASSERT(nmoved >= 0 && nmoved <= n);
1033 			nzero = roundup(on + n, PAGESIZE) - nmoved;
1034 			ASSERT(nzero > 0 && mapon + nmoved + nzero <= MAXBSIZE);
1035 			(void) kzero(base + mapon + nmoved, (uint_t)nzero);
1036 		}
1037 
1038 		/*
1039 		 * Unlock the pages allocated by page_create_va()
1040 		 * in segmap_pagecreate()
1041 		 */
1042 		if (!vpm_enable && newpage)
1043 			segmap_pageunlock(segkmap, base, (size_t)n, S_WRITE);
1044 
1045 		/*
1046 		 * If the size of the file changed, then update the
1047 		 * size field in the inode now.  This can't be done
1048 		 * before the call to segmap_pageunlock or there is
1049 		 * a potential deadlock with callers to ufs_putpage().
1050 		 * They will be holding i_contents and trying to lock
1051 		 * a page, while this thread is holding a page locked
1052 		 * and trying to acquire i_contents.
1053 		 */
1054 		if (i_size_changed) {
1055 			rw_enter(&ip->i_contents, RW_WRITER);
1056 			old_i_size = ip->i_size;
1057 			UFS_SET_ISIZE(uoff + n, ip);
1058 			TRANS_INODE(ufsvfsp, ip);
1059 			/*
1060 			 * file has grown larger than 2GB. Set flag
1061 			 * in superblock to indicate this, if it
1062 			 * is not already set.
1063 			 */
1064 			if ((ip->i_size > MAXOFF32_T) &&
1065 			    !(fs->fs_flags & FSLARGEFILES)) {
1066 				ASSERT(ufsvfsp->vfs_lfflags & UFS_LARGEFILES);
1067 				mutex_enter(&ufsvfsp->vfs_lock);
1068 				fs->fs_flags |= FSLARGEFILES;
1069 				ufs_sbwrite(ufsvfsp);
1070 				mutex_exit(&ufsvfsp->vfs_lock);
1071 			}
1072 			mutex_enter(&ip->i_tlock);
1073 			ip->i_writer = NULL;
1074 			cv_broadcast(&ip->i_wrcv);
1075 			mutex_exit(&ip->i_tlock);
1076 			rw_exit(&ip->i_contents);
1077 		}
1078 
1079 		if (error) {
1080 			/*
1081 			 * If we failed on a write, we may have already
1082 			 * allocated file blocks as well as pages.  It's
1083 			 * hard to undo the block allocation, but we must
1084 			 * be sure to invalidate any pages that may have
1085 			 * been allocated.
1086 			 *
1087 			 * If the page was created without initialization
1088 			 * then we must check if it should be possible
1089 			 * to destroy the new page and to keep the old data
1090 			 * on the disk.
1091 			 *
1092 			 * It is possible to destroy the page without
1093 			 * having to write back its contents only when
1094 			 * - the size of the file keeps unchanged
1095 			 * - bmap_write() did not allocate new disk blocks
1096 			 *   it is possible to create big files using "seek" and
1097 			 *   write to the end of the file. A "write" to a
1098 			 *   position before the end of the file would not
1099 			 *   change the size of the file but it would allocate
1100 			 *   new disk blocks.
1101 			 * - uiomove intended to overwrite the whole page.
1102 			 * - a new page was created (newpage == 1).
1103 			 */
1104 
1105 			if (i_size_changed == 0 && new_iblocks == 0 &&
1106 			    newpage) {
1107 
1108 				/* unwind what uiomove eventually last did */
1109 				uio->uio_resid = premove_resid;
1110 
1111 				/*
1112 				 * destroy the page, do not write ambiguous
1113 				 * data to the disk.
1114 				 */
1115 				flags = SM_DESTROY;
1116 			} else {
1117 				/*
1118 				 * write the page back to the disk, if dirty,
1119 				 * and remove the page from the cache.
1120 				 */
1121 				flags = SM_INVAL;
1122 			}
1123 
1124 			if (vpm_enable) {
1125 				/*
1126 				 *  Flush pages.
1127 				 */
1128 				(void) vpm_sync_pages(vp, off, n, flags);
1129 			} else {
1130 				(void) segmap_release(segkmap, base, flags);
1131 			}
1132 		} else {
1133 			flags = 0;
1134 			/*
1135 			 * Force write back for synchronous write cases.
1136 			 */
1137 			if ((ioflag & (FSYNC|FDSYNC)) || type == IFDIR) {
1138 				/*
1139 				 * If the sticky bit is set but the
1140 				 * execute bit is not set, we do a
1141 				 * synchronous write back and free
1142 				 * the page when done.  We set up swap
1143 				 * files to be handled this way to
1144 				 * prevent servers from keeping around
1145 				 * the client's swap pages too long.
1146 				 * XXX - there ought to be a better way.
1147 				 */
1148 				if (IS_SWAPVP(vp)) {
1149 					flags = SM_WRITE | SM_FREE |
1150 					    SM_DONTNEED;
1151 					iupdat_flag = 0;
1152 				} else {
1153 					flags = SM_WRITE;
1154 				}
1155 			} else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
1156 				/*
1157 				 * Have written a whole block.
1158 				 * Start an asynchronous write and
1159 				 * mark the buffer to indicate that
1160 				 * it won't be needed again soon.
1161 				 */
1162 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
1163 			}
1164 			if (vpm_enable) {
1165 				/*
1166 				 * Flush pages.
1167 				 */
1168 				error = vpm_sync_pages(vp, off, n, flags);
1169 			} else {
1170 				error = segmap_release(segkmap, base, flags);
1171 			}
1172 			/*
1173 			 * If the operation failed and is synchronous,
1174 			 * then we need to unwind what uiomove() last
1175 			 * did so we can potentially return an error to
1176 			 * the caller.  If this write operation was
1177 			 * done in two pieces and the first succeeded,
1178 			 * then we won't return an error for the second
1179 			 * piece that failed.  However, we only want to
1180 			 * return a resid value that reflects what was
1181 			 * really done.
1182 			 *
1183 			 * Failures for non-synchronous operations can
1184 			 * be ignored since the page subsystem will
1185 			 * retry the operation until it succeeds or the
1186 			 * file system is unmounted.
1187 			 */
1188 			if (error) {
1189 				if ((ioflag & (FSYNC | FDSYNC)) ||
1190 				    type == IFDIR) {
1191 					uio->uio_resid = premove_resid;
1192 				} else {
1193 					error = 0;
1194 				}
1195 			}
1196 		}
1197 
1198 		/*
1199 		 * Re-acquire contents lock.
1200 		 * If it was dropped, reacquire reader vfs_dqrwlock as well.
1201 		 */
1202 		if (do_dqrwlock)
1203 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
1204 		rw_enter(&ip->i_contents, RW_WRITER);
1205 
1206 		/*
1207 		 * If the uiomove() failed or if a synchronous
1208 		 * page push failed, fix up i_size.
1209 		 */
1210 		if (error) {
1211 			if (i_size_changed) {
1212 				/*
1213 				 * The uiomove failed, and we
1214 				 * allocated blocks,so get rid
1215 				 * of them.
1216 				 */
1217 				(void) ufs_itrunc(ip, old_i_size, 0, cr);
1218 			}
1219 		} else {
1220 			/*
1221 			 * XXX - Can this be out of the loop?
1222 			 */
1223 			ip->i_flag |= IUPD | ICHG;
1224 			/*
1225 			 * Only do one increase of i_seq for multiple
1226 			 * pieces.  Because we drop locks, record
1227 			 * the fact that we changed the timestamp and
1228 			 * are deferring the increase in case another thread
1229 			 * pushes our timestamp update.
1230 			 */
1231 			i_seq_needed = 1;
1232 			ip->i_flag |= ISEQ;
1233 			if (i_size_changed)
1234 				ip->i_flag |= IATTCHG;
1235 			if ((ip->i_mode & (IEXEC | (IEXEC >> 3) |
1236 			    (IEXEC >> 6))) != 0 &&
1237 			    (ip->i_mode & (ISUID | ISGID)) != 0 &&
1238 			    secpolicy_vnode_setid_retain(cr,
1239 			    (ip->i_mode & ISUID) != 0 && ip->i_uid == 0) != 0) {
1240 				/*
1241 				 * Clear Set-UID & Set-GID bits on
1242 				 * successful write if not privileged
1243 				 * and at least one of the execute bits
1244 				 * is set.  If we always clear Set-GID,
1245 				 * mandatory file and record locking is
1246 				 * unuseable.
1247 				 */
1248 				ip->i_mode &= ~(ISUID | ISGID);
1249 			}
1250 		}
1251 		/*
1252 		 * In the case the FDSYNC flag is set and this is a
1253 		 * "rewrite" we won't log a delta.
1254 		 * The FSYNC flag overrides all cases.
1255 		 */
1256 		if (!ufs_check_rewrite(ip, uio, ioflag) || !(ioflag & FDSYNC)) {
1257 			TRANS_INODE(ufsvfsp, ip);
1258 		}
1259 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
1260 
1261 out:
1262 	/*
1263 	 * Make sure i_seq is increased at least once per write
1264 	 */
1265 	if (i_seq_needed) {
1266 		ip->i_seq++;
1267 		ip->i_flag &= ~ISEQ;	/* no longer deferred */
1268 	}
1269 
1270 	/*
1271 	 * Inode is updated according to this table -
1272 	 *
1273 	 *   FSYNC	  FDSYNC(posix.4)
1274 	 *   --------------------------
1275 	 *   always@	  IATTCHG|IBDWRITE
1276 	 *
1277 	 * @ - 	If we are doing synchronous write the only time we should
1278 	 *	not be sync'ing the ip here is if we have the stickyhack
1279 	 *	activated, the file is marked with the sticky bit and
1280 	 *	no exec bit, the file length has not been changed and
1281 	 *	no new blocks have been allocated during this write.
1282 	 */
1283 
1284 	if ((ip->i_flag & ISYNC) != 0) {
1285 		/*
1286 		 * we have eliminated nosync
1287 		 */
1288 		if ((ip->i_flag & (IATTCHG|IBDWRITE)) ||
1289 		    ((ioflag & FSYNC) && iupdat_flag)) {
1290 			ufs_iupdat(ip, 1);
1291 		}
1292 	}
1293 
1294 	/*
1295 	 * If we've already done a partial-write, terminate
1296 	 * the write but return no error unless the error is ENOSPC
1297 	 * because the caller can detect this and free resources and
1298 	 * try again.
1299 	 */
1300 	if ((start_resid != uio->uio_resid) && (error != ENOSPC))
1301 		error = 0;
1302 
1303 	ip->i_flag &= ~(INOACC | ISYNC);
1304 	ITIMES_NOLOCK(ip);
1305 	return (error);
1306 }
1307 
1308 /*
1309  * rdip does the real work of read requests for ufs.
1310  */
1311 int
1312 rdip(struct inode *ip, struct uio *uio, int ioflag, cred_t *cr)
1313 {
1314 	u_offset_t off;
1315 	caddr_t base;
1316 	struct fs *fs;
1317 	struct ufsvfs *ufsvfsp;
1318 	struct vnode *vp;
1319 	long oresid = uio->uio_resid;
1320 	u_offset_t n, on, mapon;
1321 	int error = 0;
1322 	int doupdate = 1;
1323 	uint_t flags;
1324 	int dofree, directio_status;
1325 	krw_t rwtype;
1326 	o_mode_t type;
1327 
1328 	vp = ITOV(ip);
1329 
1330 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
1331 
1332 	ufsvfsp = ip->i_ufsvfs;
1333 
1334 	if (ufsvfsp == NULL)
1335 		return (EIO);
1336 
1337 	fs = ufsvfsp->vfs_fs;
1338 
1339 	/* check for valid filetype */
1340 	type = ip->i_mode & IFMT;
1341 	if ((type != IFREG) && (type != IFDIR) && (type != IFATTRDIR) &&
1342 	    (type != IFLNK) && (type != IFSHAD)) {
1343 		return (EIO);
1344 	}
1345 
1346 	if (uio->uio_loffset > UFS_MAXOFFSET_T) {
1347 		error = 0;
1348 		goto out;
1349 	}
1350 	if (uio->uio_loffset < (offset_t)0) {
1351 		return (EINVAL);
1352 	}
1353 	if (uio->uio_resid == 0) {
1354 		return (0);
1355 	}
1356 
1357 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (fs->fs_ronly == 0) &&
1358 	    (!ufsvfsp->vfs_noatime)) {
1359 		mutex_enter(&ip->i_tlock);
1360 		ip->i_flag |= IACC;
1361 		mutex_exit(&ip->i_tlock);
1362 	}
1363 	/*
1364 	 * Try to go direct
1365 	 */
1366 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio) {
1367 		error = ufs_directio_read(ip, uio, cr, &directio_status);
1368 		if (directio_status == DIRECTIO_SUCCESS)
1369 			goto out;
1370 	}
1371 
1372 	rwtype = (rw_write_held(&ip->i_contents)?RW_WRITER:RW_READER);
1373 
1374 	do {
1375 		offset_t diff;
1376 		u_offset_t uoff = uio->uio_loffset;
1377 		off = uoff & (offset_t)MAXBMASK;
1378 		mapon = (u_offset_t)(uoff & (offset_t)MAXBOFFSET);
1379 		on = (u_offset_t)blkoff(fs, uoff);
1380 		n = MIN((u_offset_t)fs->fs_bsize - on,
1381 		    (u_offset_t)uio->uio_resid);
1382 
1383 		diff = ip->i_size - uoff;
1384 
1385 		if (diff <= (offset_t)0) {
1386 			error = 0;
1387 			goto out;
1388 		}
1389 		if (diff < (offset_t)n)
1390 			n = (int)diff;
1391 
1392 		/*
1393 		 * We update smallfile2 and smallfile1 at most every second.
1394 		 */
1395 		if (lbolt >= smallfile_update) {
1396 			uint64_t percpufreeb;
1397 			if (smallfile1_d == 0) smallfile1_d = SMALLFILE1_D;
1398 			if (smallfile2_d == 0) smallfile2_d = SMALLFILE2_D;
1399 			percpufreeb = ptob((uint64_t)freemem) / ncpus_online;
1400 			smallfile1 = percpufreeb / smallfile1_d;
1401 			smallfile2 = percpufreeb / smallfile2_d;
1402 			smallfile1 = MAX(smallfile1, smallfile);
1403 			smallfile1 = MAX(smallfile1, smallfile64);
1404 			smallfile2 = MAX(smallfile1, smallfile2);
1405 			smallfile_update = lbolt + hz;
1406 		}
1407 
1408 		dofree = freebehind &&
1409 		    ip->i_nextr == (off & PAGEMASK) && off > smallfile1;
1410 
1411 		/*
1412 		 * At this point we can enter ufs_getpage() in one of two
1413 		 * ways:
1414 		 * 1) segmap_getmapflt() calls ufs_getpage() when the
1415 		 *    forcefault parameter is true (value of 1 is passed)
1416 		 * 2) uiomove() causes a page fault.
1417 		 *
1418 		 * We cannot hold onto an i_contents reader lock without
1419 		 * risking deadlock in ufs_getpage() so drop a reader lock.
1420 		 * The ufs_getpage() dolock logic already allows for a
1421 		 * thread holding i_contents as writer to work properly
1422 		 * so we keep a writer lock.
1423 		 */
1424 		if (rwtype == RW_READER)
1425 			rw_exit(&ip->i_contents);
1426 
1427 		if (vpm_enable) {
1428 			/*
1429 			 * Copy data.
1430 			 */
1431 			error = vpm_data_copy(vp, (off + mapon), (uint_t)n,
1432 			    uio, 1, NULL, 0, S_READ);
1433 		} else {
1434 			base = segmap_getmapflt(segkmap, vp, (off + mapon),
1435 			    (uint_t)n, 1, S_READ);
1436 			error = uiomove(base + mapon, (long)n, UIO_READ, uio);
1437 		}
1438 
1439 		flags = 0;
1440 		if (!error) {
1441 			/*
1442 			 * If  reading sequential  we won't need  this
1443 			 * buffer again  soon.  For  offsets in  range
1444 			 * [smallfile1,  smallfile2] release the pages
1445 			 * at   the  tail  of the   cache list, larger
1446 			 * offsets are released at the head.
1447 			 */
1448 			if (dofree) {
1449 				flags = SM_FREE | SM_ASYNC;
1450 				if ((cache_read_ahead == 0) &&
1451 				    (off > smallfile2))
1452 					flags |=  SM_DONTNEED;
1453 			}
1454 			/*
1455 			 * In POSIX SYNC (FSYNC and FDSYNC) read mode,
1456 			 * we want to make sure that the page which has
1457 			 * been read, is written on disk if it is dirty.
1458 			 * And corresponding indirect blocks should also
1459 			 * be flushed out.
1460 			 */
1461 			if ((ioflag & FRSYNC) && (ioflag & (FSYNC|FDSYNC))) {
1462 				flags &= ~SM_ASYNC;
1463 				flags |= SM_WRITE;
1464 			}
1465 			if (vpm_enable) {
1466 				error = vpm_sync_pages(vp, off, n, flags);
1467 			} else {
1468 				error = segmap_release(segkmap, base, flags);
1469 			}
1470 		} else {
1471 			if (vpm_enable) {
1472 				(void) vpm_sync_pages(vp, off, n, flags);
1473 			} else {
1474 				(void) segmap_release(segkmap, base, flags);
1475 			}
1476 		}
1477 
1478 		if (rwtype == RW_READER)
1479 			rw_enter(&ip->i_contents, rwtype);
1480 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
1481 out:
1482 	/*
1483 	 * Inode is updated according to this table if FRSYNC is set.
1484 	 *
1485 	 *   FSYNC	  FDSYNC(posix.4)
1486 	 *   --------------------------
1487 	 *   always	  IATTCHG|IBDWRITE
1488 	 */
1489 	/*
1490 	 * The inode is not updated if we're logging and the inode is a
1491 	 * directory with FRSYNC, FSYNC and FDSYNC flags set.
1492 	 */
1493 	if (ioflag & FRSYNC) {
1494 		if (TRANS_ISTRANS(ufsvfsp) && ((ip->i_mode & IFMT) == IFDIR)) {
1495 				doupdate = 0;
1496 		}
1497 		if (doupdate) {
1498 			if ((ioflag & FSYNC) ||
1499 			    ((ioflag & FDSYNC) &&
1500 			    (ip->i_flag & (IATTCHG|IBDWRITE)))) {
1501 				ufs_iupdat(ip, 1);
1502 			}
1503 		}
1504 	}
1505 	/*
1506 	 * If we've already done a partial read, terminate
1507 	 * the read but return no error.
1508 	 */
1509 	if (oresid != uio->uio_resid)
1510 		error = 0;
1511 	ITIMES(ip);
1512 
1513 	return (error);
1514 }
1515 
1516 /* ARGSUSED */
1517 static int
1518 ufs_ioctl(
1519 	struct vnode	*vp,
1520 	int		cmd,
1521 	intptr_t	arg,
1522 	int		flag,
1523 	struct cred	*cr,
1524 	int		*rvalp)
1525 {
1526 	struct lockfs	lockfs, lockfs_out;
1527 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
1528 	char		*comment, *original_comment;
1529 	struct fs	*fs;
1530 	struct ulockfs	*ulp;
1531 	offset_t	off;
1532 	extern int	maxphys;
1533 	int		error;
1534 	int		issync;
1535 	int		trans_size;
1536 
1537 
1538 	/*
1539 	 * forcibly unmounted
1540 	 */
1541 	if (ufsvfsp == NULL) {
1542 		return (EIO);
1543 	}
1544 
1545 	fs = ufsvfsp->vfs_fs;
1546 
1547 	if (cmd == Q_QUOTACTL) {
1548 		error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_QUOTA_MASK);
1549 		if (error)
1550 			return (error);
1551 
1552 		if (ulp) {
1553 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_QUOTA,
1554 			    TOP_SETQUOTA_SIZE(fs));
1555 		}
1556 
1557 		error = quotactl(vp, arg, flag, cr);
1558 
1559 		if (ulp) {
1560 			TRANS_END_ASYNC(ufsvfsp, TOP_QUOTA,
1561 			    TOP_SETQUOTA_SIZE(fs));
1562 			ufs_lockfs_end(ulp);
1563 		}
1564 		return (error);
1565 	}
1566 
1567 	switch (cmd) {
1568 		case _FIOLFS:
1569 			/*
1570 			 * file system locking
1571 			 */
1572 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1573 				return (EPERM);
1574 
1575 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1576 				if (copyin((caddr_t)arg, &lockfs,
1577 				    sizeof (struct lockfs)))
1578 					return (EFAULT);
1579 			}
1580 #ifdef _SYSCALL32_IMPL
1581 			else {
1582 				struct lockfs32	lockfs32;
1583 				/* Translate ILP32 lockfs to LP64 lockfs */
1584 				if (copyin((caddr_t)arg, &lockfs32,
1585 				    sizeof (struct lockfs32)))
1586 					return (EFAULT);
1587 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1588 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1589 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1590 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1591 				lockfs.lf_comment =
1592 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
1593 			}
1594 #endif /* _SYSCALL32_IMPL */
1595 
1596 			if (lockfs.lf_comlen) {
1597 				if (lockfs.lf_comlen > LOCKFS_MAXCOMMENTLEN)
1598 					return (ENAMETOOLONG);
1599 				comment =
1600 				    kmem_alloc(lockfs.lf_comlen, KM_SLEEP);
1601 				if (copyin(lockfs.lf_comment, comment,
1602 				    lockfs.lf_comlen)) {
1603 					kmem_free(comment, lockfs.lf_comlen);
1604 					return (EFAULT);
1605 				}
1606 				original_comment = lockfs.lf_comment;
1607 				lockfs.lf_comment = comment;
1608 			}
1609 			if ((error = ufs_fiolfs(vp, &lockfs, 0)) == 0) {
1610 				lockfs.lf_comment = original_comment;
1611 
1612 				if ((flag & DATAMODEL_MASK) ==
1613 				    DATAMODEL_NATIVE) {
1614 					(void) copyout(&lockfs, (caddr_t)arg,
1615 					    sizeof (struct lockfs));
1616 				}
1617 #ifdef _SYSCALL32_IMPL
1618 				else {
1619 					struct lockfs32	lockfs32;
1620 					/* Translate LP64 to ILP32 lockfs */
1621 					lockfs32.lf_lock =
1622 					    (uint32_t)lockfs.lf_lock;
1623 					lockfs32.lf_flags =
1624 					    (uint32_t)lockfs.lf_flags;
1625 					lockfs32.lf_key =
1626 					    (uint32_t)lockfs.lf_key;
1627 					lockfs32.lf_comlen =
1628 					    (uint32_t)lockfs.lf_comlen;
1629 					lockfs32.lf_comment =
1630 					    (uint32_t)(uintptr_t)
1631 					    lockfs.lf_comment;
1632 					(void) copyout(&lockfs32, (caddr_t)arg,
1633 					    sizeof (struct lockfs32));
1634 				}
1635 #endif /* _SYSCALL32_IMPL */
1636 
1637 			} else {
1638 				if (lockfs.lf_comlen)
1639 					kmem_free(comment, lockfs.lf_comlen);
1640 			}
1641 			return (error);
1642 
1643 		case _FIOLFSS:
1644 			/*
1645 			 * get file system locking status
1646 			 */
1647 
1648 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1649 				if (copyin((caddr_t)arg, &lockfs,
1650 				    sizeof (struct lockfs)))
1651 					return (EFAULT);
1652 			}
1653 #ifdef _SYSCALL32_IMPL
1654 			else {
1655 				struct lockfs32	lockfs32;
1656 				/* Translate ILP32 lockfs to LP64 lockfs */
1657 				if (copyin((caddr_t)arg, &lockfs32,
1658 				    sizeof (struct lockfs32)))
1659 					return (EFAULT);
1660 				lockfs.lf_lock = (ulong_t)lockfs32.lf_lock;
1661 				lockfs.lf_flags = (ulong_t)lockfs32.lf_flags;
1662 				lockfs.lf_key = (ulong_t)lockfs32.lf_key;
1663 				lockfs.lf_comlen = (ulong_t)lockfs32.lf_comlen;
1664 				lockfs.lf_comment =
1665 				    (caddr_t)(uintptr_t)lockfs32.lf_comment;
1666 			}
1667 #endif /* _SYSCALL32_IMPL */
1668 
1669 			if (error =  ufs_fiolfss(vp, &lockfs_out))
1670 				return (error);
1671 			lockfs.lf_lock = lockfs_out.lf_lock;
1672 			lockfs.lf_key = lockfs_out.lf_key;
1673 			lockfs.lf_flags = lockfs_out.lf_flags;
1674 			lockfs.lf_comlen = MIN(lockfs.lf_comlen,
1675 			    lockfs_out.lf_comlen);
1676 
1677 			if ((flag & DATAMODEL_MASK) == DATAMODEL_NATIVE) {
1678 				if (copyout(&lockfs, (caddr_t)arg,
1679 				    sizeof (struct lockfs)))
1680 					return (EFAULT);
1681 			}
1682 #ifdef _SYSCALL32_IMPL
1683 			else {
1684 				/* Translate LP64 to ILP32 lockfs */
1685 				struct lockfs32	lockfs32;
1686 				lockfs32.lf_lock = (uint32_t)lockfs.lf_lock;
1687 				lockfs32.lf_flags = (uint32_t)lockfs.lf_flags;
1688 				lockfs32.lf_key = (uint32_t)lockfs.lf_key;
1689 				lockfs32.lf_comlen = (uint32_t)lockfs.lf_comlen;
1690 				lockfs32.lf_comment =
1691 				    (uint32_t)(uintptr_t)lockfs.lf_comment;
1692 				if (copyout(&lockfs32, (caddr_t)arg,
1693 				    sizeof (struct lockfs32)))
1694 					return (EFAULT);
1695 			}
1696 #endif /* _SYSCALL32_IMPL */
1697 
1698 			if (lockfs.lf_comlen &&
1699 			    lockfs.lf_comment && lockfs_out.lf_comment)
1700 				if (copyout(lockfs_out.lf_comment,
1701 				    lockfs.lf_comment, lockfs.lf_comlen))
1702 					return (EFAULT);
1703 			return (0);
1704 
1705 		case _FIOSATIME:
1706 			/*
1707 			 * set access time
1708 			 */
1709 
1710 			/*
1711 			 * if mounted w/o atime, return quietly.
1712 			 * I briefly thought about returning ENOSYS, but
1713 			 * figured that most apps would consider this fatal
1714 			 * but the idea is to make this as seamless as poss.
1715 			 */
1716 			if (ufsvfsp->vfs_noatime)
1717 				return (0);
1718 
1719 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1720 			    ULOCKFS_SETATTR_MASK);
1721 			if (error)
1722 				return (error);
1723 
1724 			if (ulp) {
1725 				trans_size = (int)TOP_SETATTR_SIZE(VTOI(vp));
1726 				TRANS_BEGIN_CSYNC(ufsvfsp, issync,
1727 				    TOP_SETATTR, trans_size);
1728 			}
1729 
1730 			error = ufs_fiosatime(vp, (struct timeval *)arg,
1731 			    flag, cr);
1732 
1733 			if (ulp) {
1734 				TRANS_END_CSYNC(ufsvfsp, error, issync,
1735 				    TOP_SETATTR, trans_size);
1736 				ufs_lockfs_end(ulp);
1737 			}
1738 			return (error);
1739 
1740 		case _FIOSDIO:
1741 			/*
1742 			 * set delayed-io
1743 			 */
1744 			return (ufs_fiosdio(vp, (uint_t *)arg, flag, cr));
1745 
1746 		case _FIOGDIO:
1747 			/*
1748 			 * get delayed-io
1749 			 */
1750 			return (ufs_fiogdio(vp, (uint_t *)arg, flag, cr));
1751 
1752 		case _FIOIO:
1753 			/*
1754 			 * inode open
1755 			 */
1756 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1757 			    ULOCKFS_VGET_MASK);
1758 			if (error)
1759 				return (error);
1760 
1761 			error = ufs_fioio(vp, (struct fioio *)arg, flag, cr);
1762 
1763 			if (ulp) {
1764 				ufs_lockfs_end(ulp);
1765 			}
1766 			return (error);
1767 
1768 		case _FIOFFS:
1769 			/*
1770 			 * file system flush (push w/invalidate)
1771 			 */
1772 			if ((caddr_t)arg != NULL)
1773 				return (EINVAL);
1774 			return (ufs_fioffs(vp, NULL, cr));
1775 
1776 		case _FIOISBUSY:
1777 			/*
1778 			 * Contract-private interface for Legato
1779 			 * Purge this vnode from the DNLC and decide
1780 			 * if this vnode is busy (*arg == 1) or not
1781 			 * (*arg == 0)
1782 			 */
1783 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1784 				return (EPERM);
1785 			error = ufs_fioisbusy(vp, (int *)arg, cr);
1786 			return (error);
1787 
1788 		case _FIODIRECTIO:
1789 			return (ufs_fiodirectio(vp, (int)arg, cr));
1790 
1791 		case _FIOTUNE:
1792 			/*
1793 			 * Tune the file system (aka setting fs attributes)
1794 			 */
1795 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
1796 			    ULOCKFS_SETATTR_MASK);
1797 			if (error)
1798 				return (error);
1799 
1800 			error = ufs_fiotune(vp, (struct fiotune *)arg, cr);
1801 
1802 			if (ulp)
1803 				ufs_lockfs_end(ulp);
1804 			return (error);
1805 
1806 		case _FIOLOGENABLE:
1807 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1808 				return (EPERM);
1809 			return (ufs_fiologenable(vp, (void *)arg, cr, flag));
1810 
1811 		case _FIOLOGDISABLE:
1812 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1813 				return (EPERM);
1814 			return (ufs_fiologdisable(vp, (void *)arg, cr, flag));
1815 
1816 		case _FIOISLOG:
1817 			return (ufs_fioislog(vp, (void *)arg, cr, flag));
1818 
1819 		case _FIOSNAPSHOTCREATE_MULTI:
1820 		{
1821 			struct fiosnapcreate_multi	fc, *fcp;
1822 			size_t	fcm_size;
1823 
1824 			if (copyin((void *)arg, &fc, sizeof (fc)))
1825 				return (EFAULT);
1826 			if (fc.backfilecount > MAX_BACKFILE_COUNT)
1827 				return (EINVAL);
1828 			fcm_size = sizeof (struct fiosnapcreate_multi) +
1829 			    (fc.backfilecount - 1) * sizeof (int);
1830 			fcp = (struct fiosnapcreate_multi *)
1831 			    kmem_alloc(fcm_size, KM_SLEEP);
1832 			if (copyin((void *)arg, fcp, fcm_size)) {
1833 				kmem_free(fcp, fcm_size);
1834 				return (EFAULT);
1835 			}
1836 			error = ufs_snap_create(vp, fcp, cr);
1837 			if (!error && copyout(fcp, (void *)arg, fcm_size))
1838 				error = EFAULT;
1839 			kmem_free(fcp, fcm_size);
1840 			return (error);
1841 		}
1842 
1843 		case _FIOSNAPSHOTDELETE:
1844 		{
1845 			struct fiosnapdelete	fc;
1846 
1847 			if (copyin((void *)arg, &fc, sizeof (fc)))
1848 				return (EFAULT);
1849 			error = ufs_snap_delete(vp, &fc, cr);
1850 			if (!error && copyout(&fc, (void *)arg, sizeof (fc)))
1851 				error = EFAULT;
1852 			return (error);
1853 		}
1854 
1855 		case _FIOGETSUPERBLOCK:
1856 			if (copyout(fs, (void *)arg, SBSIZE))
1857 				return (EFAULT);
1858 			return (0);
1859 
1860 		case _FIOGETMAXPHYS:
1861 			if (copyout(&maxphys, (void *)arg, sizeof (maxphys)))
1862 				return (EFAULT);
1863 			return (0);
1864 
1865 		/*
1866 		 * The following 3 ioctls are for TSufs support
1867 		 * although could potentially be used elsewhere
1868 		 */
1869 		case _FIO_SET_LUFS_DEBUG:
1870 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1871 				return (EPERM);
1872 			lufs_debug = (uint32_t)arg;
1873 			return (0);
1874 
1875 		case _FIO_SET_LUFS_ERROR:
1876 			if (secpolicy_fs_config(cr, ufsvfsp->vfs_vfs) != 0)
1877 				return (EPERM);
1878 			TRANS_SETERROR(ufsvfsp);
1879 			return (0);
1880 
1881 		case _FIO_GET_TOP_STATS:
1882 		{
1883 			fio_lufs_stats_t *ls;
1884 			ml_unit_t *ul = ufsvfsp->vfs_log;
1885 
1886 			ls = kmem_zalloc(sizeof (*ls), KM_SLEEP);
1887 			ls->ls_debug = ul->un_debug; /* return debug value */
1888 			/* Copy stucture if statistics are being kept */
1889 			if (ul->un_logmap->mtm_tops) {
1890 				ls->ls_topstats = *(ul->un_logmap->mtm_tops);
1891 			}
1892 			error = 0;
1893 			if (copyout(ls, (void *)arg, sizeof (*ls)))
1894 				error = EFAULT;
1895 			kmem_free(ls, sizeof (*ls));
1896 			return (error);
1897 		}
1898 
1899 		case _FIO_SEEK_DATA:
1900 		case _FIO_SEEK_HOLE:
1901 			if (ddi_copyin((void *)arg, &off, sizeof (off), flag))
1902 				return (EFAULT);
1903 			/* offset paramater is in/out */
1904 			error = ufs_fio_holey(vp, cmd, &off);
1905 			if (error)
1906 				return (error);
1907 			if (ddi_copyout(&off, (void *)arg, sizeof (off), flag))
1908 				return (EFAULT);
1909 			return (0);
1910 
1911 		default:
1912 			return (ENOTTY);
1913 	}
1914 }
1915 
1916 /* ARGSUSED */
1917 static int
1918 ufs_getattr(struct vnode *vp, struct vattr *vap, int flags,
1919 	struct cred *cr)
1920 {
1921 	struct inode *ip = VTOI(vp);
1922 	struct ufsvfs *ufsvfsp;
1923 	int err;
1924 
1925 	if (vap->va_mask == AT_SIZE) {
1926 		/*
1927 		 * for performance, if only the size is requested don't bother
1928 		 * with anything else.
1929 		 */
1930 		UFS_GET_ISIZE(&vap->va_size, ip);
1931 		return (0);
1932 	}
1933 
1934 	/*
1935 	 * inlined lockfs checks
1936 	 */
1937 	ufsvfsp = ip->i_ufsvfs;
1938 	if ((ufsvfsp == NULL) || ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs)) {
1939 		err = EIO;
1940 		goto out;
1941 	}
1942 
1943 	rw_enter(&ip->i_contents, RW_READER);
1944 	/*
1945 	 * Return all the attributes.  This should be refined so
1946 	 * that it only returns what's asked for.
1947 	 */
1948 
1949 	/*
1950 	 * Copy from inode table.
1951 	 */
1952 	vap->va_type = vp->v_type;
1953 	vap->va_mode = ip->i_mode & MODEMASK;
1954 	/*
1955 	 * If there is an ACL and there is a mask entry, then do the
1956 	 * extra work that completes the equivalent of an acltomode(3)
1957 	 * call.  According to POSIX P1003.1e, the acl mask should be
1958 	 * returned in the group permissions field.
1959 	 *
1960 	 * - start with the original permission and mode bits (from above)
1961 	 * - clear the group owner bits
1962 	 * - add in the mask bits.
1963 	 */
1964 	if (ip->i_ufs_acl && ip->i_ufs_acl->aclass.acl_ismask) {
1965 		vap->va_mode &= ~((VREAD | VWRITE | VEXEC) >> 3);
1966 		vap->va_mode |=
1967 		    (ip->i_ufs_acl->aclass.acl_maskbits & PERMMASK) << 3;
1968 	}
1969 	vap->va_uid = ip->i_uid;
1970 	vap->va_gid = ip->i_gid;
1971 	vap->va_fsid = ip->i_dev;
1972 	vap->va_nodeid = (ino64_t)ip->i_number;
1973 	vap->va_nlink = ip->i_nlink;
1974 	vap->va_size = ip->i_size;
1975 	if (vp->v_type == VCHR || vp->v_type == VBLK)
1976 		vap->va_rdev = ip->i_rdev;
1977 	else
1978 		vap->va_rdev = 0;	/* not a b/c spec. */
1979 	mutex_enter(&ip->i_tlock);
1980 	ITIMES_NOLOCK(ip);	/* mark correct time in inode */
1981 	vap->va_seq = ip->i_seq;
1982 	vap->va_atime.tv_sec = (time_t)ip->i_atime.tv_sec;
1983 	vap->va_atime.tv_nsec = ip->i_atime.tv_usec*1000;
1984 	vap->va_mtime.tv_sec = (time_t)ip->i_mtime.tv_sec;
1985 	vap->va_mtime.tv_nsec = ip->i_mtime.tv_usec*1000;
1986 	vap->va_ctime.tv_sec = (time_t)ip->i_ctime.tv_sec;
1987 	vap->va_ctime.tv_nsec = ip->i_ctime.tv_usec*1000;
1988 	mutex_exit(&ip->i_tlock);
1989 
1990 	switch (ip->i_mode & IFMT) {
1991 
1992 	case IFBLK:
1993 		vap->va_blksize = MAXBSIZE;		/* was BLKDEV_IOSIZE */
1994 		break;
1995 
1996 	case IFCHR:
1997 		vap->va_blksize = MAXBSIZE;
1998 		break;
1999 
2000 	default:
2001 		vap->va_blksize = ip->i_fs->fs_bsize;
2002 		break;
2003 	}
2004 	vap->va_nblocks = (fsblkcnt64_t)ip->i_blocks;
2005 	rw_exit(&ip->i_contents);
2006 	err = 0;
2007 
2008 out:
2009 	return (err);
2010 }
2011 
2012 /*ARGSUSED4*/
2013 static int
2014 ufs_setattr(
2015 	struct vnode *vp,
2016 	struct vattr *vap,
2017 	int flags,
2018 	struct cred *cr,
2019 	caller_context_t *ct)
2020 {
2021 	struct inode *ip = VTOI(vp);
2022 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2023 	struct fs *fs;
2024 	struct ulockfs *ulp;
2025 	char *errmsg1;
2026 	char *errmsg2;
2027 	long blocks;
2028 	long int mask = vap->va_mask;
2029 	size_t len1, len2;
2030 	int issync;
2031 	int trans_size;
2032 	int dotrans;
2033 	int dorwlock;
2034 	int error;
2035 	int owner_change;
2036 	int dodqlock;
2037 	timestruc_t now;
2038 	vattr_t oldva;
2039 	int retry = 1;
2040 	int indeadlock;
2041 
2042 	/*
2043 	 * Cannot set these attributes.
2044 	 */
2045 	if (mask & AT_NOSET) {
2046 		error = EINVAL;
2047 		goto out;
2048 	}
2049 
2050 	/*
2051 	 * check for forced unmount
2052 	 */
2053 	if (ufsvfsp == NULL)
2054 		return (EIO);
2055 
2056 	fs = ufsvfsp->vfs_fs;
2057 	if (fs->fs_ronly != 0)
2058 		return (EROFS);
2059 
2060 again:
2061 	errmsg1 = NULL;
2062 	errmsg2 = NULL;
2063 	dotrans = 0;
2064 	dorwlock = 0;
2065 	dodqlock = 0;
2066 
2067 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK);
2068 	if (error)
2069 		goto out;
2070 
2071 	/*
2072 	 * Acquire i_rwlock before TRANS_BEGIN_CSYNC() if this is a file.
2073 	 * This follows the protocol for read()/write().
2074 	 */
2075 	if (vp->v_type != VDIR) {
2076 		/*
2077 		 * ufs_tryirwlock uses rw_tryenter and checks for SLOCK to
2078 		 * avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2079 		 * possible, retries the operation.
2080 		 */
2081 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_file);
2082 		if (indeadlock) {
2083 			if (ulp)
2084 				ufs_lockfs_end(ulp);
2085 			goto again;
2086 		}
2087 		dorwlock = 1;
2088 	}
2089 
2090 	/*
2091 	 * Truncate file.  Must have write permission and not be a directory.
2092 	 */
2093 	if (mask & AT_SIZE) {
2094 		rw_enter(&ip->i_contents, RW_WRITER);
2095 		if (vp->v_type == VDIR) {
2096 			error = EISDIR;
2097 			goto update_inode;
2098 		}
2099 		if (error = ufs_iaccess(ip, IWRITE, cr))
2100 			goto update_inode;
2101 
2102 		rw_exit(&ip->i_contents);
2103 		error = TRANS_ITRUNC(ip, vap->va_size, 0, cr);
2104 		if (error) {
2105 			rw_enter(&ip->i_contents, RW_WRITER);
2106 			goto update_inode;
2107 		}
2108 	}
2109 
2110 	if (ulp) {
2111 		trans_size = (int)TOP_SETATTR_SIZE(ip);
2112 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SETATTR, trans_size);
2113 		++dotrans;
2114 	}
2115 
2116 	/*
2117 	 * Acquire i_rwlock after TRANS_BEGIN_CSYNC() if this is a directory.
2118 	 * This follows the protocol established by
2119 	 * ufs_link/create/remove/rename/mkdir/rmdir/symlink.
2120 	 */
2121 	if (vp->v_type == VDIR) {
2122 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_SETATTR,
2123 		    retry_dir);
2124 		if (indeadlock)
2125 			goto again;
2126 		dorwlock = 1;
2127 	}
2128 
2129 	/*
2130 	 * Grab quota lock if we are changing the file's owner.
2131 	 */
2132 	if (mask & AT_UID) {
2133 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2134 		dodqlock = 1;
2135 	}
2136 	rw_enter(&ip->i_contents, RW_WRITER);
2137 
2138 	oldva.va_mode = ip->i_mode;
2139 	oldva.va_uid = ip->i_uid;
2140 	oldva.va_gid = ip->i_gid;
2141 
2142 	vap->va_mask &= ~AT_SIZE;
2143 	/*
2144 	 * ufs_iaccess is "close enough"; that's because it doesn't
2145 	 * map the defines.
2146 	 */
2147 	error = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2148 	    ufs_iaccess, ip);
2149 	if (error)
2150 		goto update_inode;
2151 
2152 	mask = vap->va_mask;
2153 
2154 	/*
2155 	 * Change file access modes.
2156 	 */
2157 	if (mask & AT_MODE) {
2158 		ip->i_mode = (ip->i_mode & IFMT) | (vap->va_mode & ~IFMT);
2159 		TRANS_INODE(ufsvfsp, ip);
2160 		ip->i_flag |= ICHG;
2161 		if (stickyhack) {
2162 			mutex_enter(&vp->v_lock);
2163 			if ((ip->i_mode & (ISVTX | IEXEC | IFDIR)) == ISVTX)
2164 				vp->v_flag |= VSWAPLIKE;
2165 			else
2166 				vp->v_flag &= ~VSWAPLIKE;
2167 			mutex_exit(&vp->v_lock);
2168 		}
2169 	}
2170 	if (mask & (AT_UID|AT_GID)) {
2171 		if (mask & AT_UID) {
2172 			/*
2173 			 * Don't change ownership of the quota inode.
2174 			 */
2175 			if (ufsvfsp->vfs_qinod == ip) {
2176 				ASSERT(ufsvfsp->vfs_qflags & MQ_ENABLED);
2177 				error = EINVAL;
2178 				goto update_inode;
2179 			}
2180 
2181 			/*
2182 			 * No real ownership change.
2183 			 */
2184 			if (ip->i_uid == vap->va_uid) {
2185 				blocks = 0;
2186 				owner_change = 0;
2187 			}
2188 			/*
2189 			 * Remove the blocks and the file, from the old user's
2190 			 * quota.
2191 			 */
2192 			else {
2193 				blocks = ip->i_blocks;
2194 				owner_change = 1;
2195 
2196 				(void) chkdq(ip, -blocks, /* force */ 1, cr,
2197 				    (char **)NULL, (size_t *)NULL);
2198 				(void) chkiq(ufsvfsp, /* change */ -1, ip,
2199 				    (uid_t)ip->i_uid, /* force */ 1, cr,
2200 				    (char **)NULL, (size_t *)NULL);
2201 				dqrele(ip->i_dquot);
2202 			}
2203 
2204 			ip->i_uid = vap->va_uid;
2205 
2206 			/*
2207 			 * There is a real ownership change.
2208 			 */
2209 			if (owner_change) {
2210 				/*
2211 				 * Add the blocks and the file to the new
2212 				 * user's quota.
2213 				 */
2214 				ip->i_dquot = getinoquota(ip);
2215 				(void) chkdq(ip, blocks, /* force */ 1, cr,
2216 				    &errmsg1, &len1);
2217 				(void) chkiq(ufsvfsp, /* change */ 1,
2218 				    (struct inode *)NULL, (uid_t)ip->i_uid,
2219 				    /* force */ 1, cr, &errmsg2, &len2);
2220 			}
2221 		}
2222 		if (mask & AT_GID) {
2223 			ip->i_gid = vap->va_gid;
2224 		}
2225 		TRANS_INODE(ufsvfsp, ip);
2226 		ip->i_flag |= ICHG;
2227 	}
2228 	/*
2229 	 * Change file access or modified times.
2230 	 */
2231 	if (mask & (AT_ATIME|AT_MTIME)) {
2232 		/* Check that the time value is within ufs range */
2233 		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2234 		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2235 			error = EOVERFLOW;
2236 			goto update_inode;
2237 		}
2238 
2239 		/*
2240 		 * if the "noaccess" mount option is set and only atime
2241 		 * update is requested, do nothing. No error is returned.
2242 		 */
2243 		if ((ufsvfsp->vfs_noatime) &&
2244 		    ((mask & (AT_ATIME|AT_MTIME)) == AT_ATIME))
2245 			goto skip_atime;
2246 
2247 		if (mask & AT_ATIME) {
2248 			ip->i_atime.tv_sec = vap->va_atime.tv_sec;
2249 			ip->i_atime.tv_usec = vap->va_atime.tv_nsec / 1000;
2250 			ip->i_flag &= ~IACC;
2251 		}
2252 		if (mask & AT_MTIME) {
2253 			ip->i_mtime.tv_sec = vap->va_mtime.tv_sec;
2254 			ip->i_mtime.tv_usec = vap->va_mtime.tv_nsec / 1000;
2255 			gethrestime(&now);
2256 			if (now.tv_sec > TIME32_MAX) {
2257 				/*
2258 				 * In 2038, ctime sticks forever..
2259 				 */
2260 				ip->i_ctime.tv_sec = TIME32_MAX;
2261 				ip->i_ctime.tv_usec = 0;
2262 			} else {
2263 				ip->i_ctime.tv_sec = now.tv_sec;
2264 				ip->i_ctime.tv_usec = now.tv_nsec / 1000;
2265 			}
2266 			ip->i_flag &= ~(IUPD|ICHG);
2267 			ip->i_flag |= IMODTIME;
2268 		}
2269 		TRANS_INODE(ufsvfsp, ip);
2270 		ip->i_flag |= IMOD;
2271 	}
2272 
2273 skip_atime:
2274 	/*
2275 	 * The presence of a shadow inode may indicate an ACL, but does
2276 	 * not imply an ACL.  Future FSD types should be handled here too
2277 	 * and check for the presence of the attribute-specific data
2278 	 * before referencing it.
2279 	 */
2280 	if (ip->i_shadow) {
2281 		/*
2282 		 * XXX if ufs_iupdat is changed to sandbagged write fix
2283 		 * ufs_acl_setattr to push ip to keep acls consistent
2284 		 *
2285 		 * Suppress out of inodes messages if we will retry.
2286 		 */
2287 		if (retry)
2288 			ip->i_flag |= IQUIET;
2289 		error = ufs_acl_setattr(ip, vap, cr);
2290 		ip->i_flag &= ~IQUIET;
2291 	}
2292 
2293 update_inode:
2294 	/*
2295 	 * Setattr always increases the sequence number
2296 	 */
2297 	ip->i_seq++;
2298 
2299 	/*
2300 	 * if nfsd and not logging; push synchronously
2301 	 */
2302 	if ((curthread->t_flag & T_DONTPEND) && !TRANS_ISTRANS(ufsvfsp)) {
2303 		ufs_iupdat(ip, 1);
2304 	} else {
2305 		ITIMES_NOLOCK(ip);
2306 	}
2307 
2308 	rw_exit(&ip->i_contents);
2309 	if (dodqlock) {
2310 		rw_exit(&ufsvfsp->vfs_dqrwlock);
2311 	}
2312 	if (dorwlock)
2313 		rw_exit(&ip->i_rwlock);
2314 
2315 	if (ulp) {
2316 		if (dotrans) {
2317 			int terr = 0;
2318 			TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SETATTR,
2319 			    trans_size);
2320 			if (error == 0)
2321 				error = terr;
2322 		}
2323 		ufs_lockfs_end(ulp);
2324 	}
2325 out:
2326 	/*
2327 	 * If out of inodes or blocks, see if we can free something
2328 	 * up from the delete queue.
2329 	 */
2330 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
2331 		ufs_delete_drain_wait(ufsvfsp, 1);
2332 		retry = 0;
2333 		if (errmsg1 != NULL)
2334 			kmem_free(errmsg1, len1);
2335 		if (errmsg2 != NULL)
2336 			kmem_free(errmsg2, len2);
2337 		goto again;
2338 	}
2339 	if (errmsg1 != NULL) {
2340 		uprintf(errmsg1);
2341 		kmem_free(errmsg1, len1);
2342 	}
2343 	if (errmsg2 != NULL) {
2344 		uprintf(errmsg2);
2345 		kmem_free(errmsg2, len2);
2346 	}
2347 	return (error);
2348 }
2349 
2350 /*ARGSUSED*/
2351 static int
2352 ufs_access(struct vnode *vp, int mode, int flags, struct cred *cr)
2353 {
2354 	struct inode *ip = VTOI(vp);
2355 	int error;
2356 
2357 	if (ip->i_ufsvfs == NULL)
2358 		return (EIO);
2359 
2360 	rw_enter(&ip->i_contents, RW_READER);
2361 
2362 	/*
2363 	 * The ufs_iaccess function wants to be called with
2364 	 * mode bits expressed as "ufs specific" bits.
2365 	 * I.e., VWRITE|VREAD|VEXEC do not make sense to
2366 	 * ufs_iaccess() but IWRITE|IREAD|IEXEC do.
2367 	 * But since they're the same we just pass the vnode mode
2368 	 * bit but just verify that assumption at compile time.
2369 	 */
2370 #if IWRITE != VWRITE || IREAD != VREAD || IEXEC != VEXEC
2371 #error "ufs_access needs to map Vmodes to Imodes"
2372 #endif
2373 	error = ufs_iaccess(ip, mode, cr);
2374 
2375 	rw_exit(&ip->i_contents);
2376 
2377 	return (error);
2378 }
2379 
2380 /* ARGSUSED */
2381 static int
2382 ufs_readlink(struct vnode *vp, struct uio *uiop, struct cred *cr)
2383 {
2384 	struct inode *ip = VTOI(vp);
2385 	struct ufsvfs *ufsvfsp;
2386 	struct ulockfs *ulp;
2387 	int error;
2388 	int fastsymlink;
2389 
2390 	if (vp->v_type != VLNK) {
2391 		error = EINVAL;
2392 		goto nolockout;
2393 	}
2394 
2395 	/*
2396 	 * If the symbolic link is empty there is nothing to read.
2397 	 * Fast-track these empty symbolic links
2398 	 */
2399 	if (ip->i_size == 0) {
2400 		error = 0;
2401 		goto nolockout;
2402 	}
2403 
2404 	ufsvfsp = ip->i_ufsvfs;
2405 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READLINK_MASK);
2406 	if (error)
2407 		goto nolockout;
2408 	/*
2409 	 * The ip->i_rwlock protects the data blocks used for FASTSYMLINK
2410 	 */
2411 again:
2412 	fastsymlink = 0;
2413 	if (ip->i_flag & IFASTSYMLNK) {
2414 		rw_enter(&ip->i_rwlock, RW_READER);
2415 		rw_enter(&ip->i_contents, RW_READER);
2416 		if (ip->i_flag & IFASTSYMLNK) {
2417 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
2418 			    (ip->i_fs->fs_ronly == 0) &&
2419 			    (!ufsvfsp->vfs_noatime)) {
2420 				mutex_enter(&ip->i_tlock);
2421 				ip->i_flag |= IACC;
2422 				mutex_exit(&ip->i_tlock);
2423 			}
2424 			error = uiomove((caddr_t)&ip->i_db[1],
2425 			    MIN(ip->i_size, uiop->uio_resid),
2426 			    UIO_READ, uiop);
2427 			ITIMES(ip);
2428 			++fastsymlink;
2429 		}
2430 		rw_exit(&ip->i_contents);
2431 		rw_exit(&ip->i_rwlock);
2432 	}
2433 	if (!fastsymlink) {
2434 		ssize_t size;	/* number of bytes read  */
2435 		caddr_t basep;	/* pointer to input data */
2436 		ino_t ino;
2437 		long  igen;
2438 		struct uio tuio;	/* temp uio struct */
2439 		struct uio *tuiop;
2440 		iovec_t tiov;		/* temp iovec struct */
2441 		char kbuf[FSL_SIZE];	/* buffer to hold fast symlink */
2442 		int tflag = 0;		/* flag to indicate temp vars used */
2443 
2444 		ino = ip->i_number;
2445 		igen = ip->i_gen;
2446 		size = uiop->uio_resid;
2447 		basep = uiop->uio_iov->iov_base;
2448 		tuiop = uiop;
2449 
2450 		rw_enter(&ip->i_rwlock, RW_WRITER);
2451 		rw_enter(&ip->i_contents, RW_WRITER);
2452 		if (ip->i_flag & IFASTSYMLNK) {
2453 			rw_exit(&ip->i_contents);
2454 			rw_exit(&ip->i_rwlock);
2455 			goto again;
2456 		}
2457 
2458 		/* can this be a fast symlink and is it a user buffer? */
2459 		if (ip->i_size <= FSL_SIZE &&
2460 		    (uiop->uio_segflg == UIO_USERSPACE ||
2461 		    uiop->uio_segflg == UIO_USERISPACE)) {
2462 
2463 			bzero(&tuio, sizeof (struct uio));
2464 			/*
2465 			 * setup a kernel buffer to read link into.  this
2466 			 * is to fix a race condition where the user buffer
2467 			 * got corrupted before copying it into the inode.
2468 			 */
2469 			size = ip->i_size;
2470 			tiov.iov_len = size;
2471 			tiov.iov_base = kbuf;
2472 			tuio.uio_iov = &tiov;
2473 			tuio.uio_iovcnt = 1;
2474 			tuio.uio_offset = uiop->uio_offset;
2475 			tuio.uio_segflg = UIO_SYSSPACE;
2476 			tuio.uio_fmode = uiop->uio_fmode;
2477 			tuio.uio_extflg = uiop->uio_extflg;
2478 			tuio.uio_limit = uiop->uio_limit;
2479 			tuio.uio_resid = size;
2480 
2481 			basep = tuio.uio_iov->iov_base;
2482 			tuiop = &tuio;
2483 			tflag = 1;
2484 		}
2485 
2486 		error = rdip(ip, tuiop, 0, cr);
2487 		if (!(error == 0 && ip->i_number == ino && ip->i_gen == igen)) {
2488 			rw_exit(&ip->i_contents);
2489 			rw_exit(&ip->i_rwlock);
2490 			goto out;
2491 		}
2492 
2493 		if (tflag == 0)
2494 			size -= uiop->uio_resid;
2495 
2496 		if ((tflag == 0 && ip->i_size <= FSL_SIZE &&
2497 		    ip->i_size == size) || (tflag == 1 &&
2498 		    tuio.uio_resid == 0)) {
2499 			error = kcopy(basep, &ip->i_db[1], ip->i_size);
2500 			if (error == 0) {
2501 				ip->i_flag |= IFASTSYMLNK;
2502 				/*
2503 				 * free page
2504 				 */
2505 				(void) VOP_PUTPAGE(ITOV(ip),
2506 				    (offset_t)0, PAGESIZE,
2507 				    (B_DONTNEED | B_FREE | B_FORCE | B_ASYNC),
2508 				    cr);
2509 			} else {
2510 				int i;
2511 				/* error, clear garbage left behind */
2512 				for (i = 1; i < NDADDR; i++)
2513 					ip->i_db[i] = 0;
2514 				for (i = 0; i < NIADDR; i++)
2515 					ip->i_ib[i] = 0;
2516 			}
2517 		}
2518 		if (tflag == 1) {
2519 			/* now, copy it into the user buffer */
2520 			error = uiomove((caddr_t)kbuf,
2521 			    MIN(size, uiop->uio_resid),
2522 			    UIO_READ, uiop);
2523 		}
2524 		rw_exit(&ip->i_contents);
2525 		rw_exit(&ip->i_rwlock);
2526 	}
2527 out:
2528 	if (ulp) {
2529 		ufs_lockfs_end(ulp);
2530 	}
2531 nolockout:
2532 	return (error);
2533 }
2534 
2535 /* ARGSUSED */
2536 static int
2537 ufs_fsync(struct vnode *vp, int syncflag, struct cred *cr)
2538 {
2539 	struct inode *ip = VTOI(vp);
2540 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
2541 	struct ulockfs *ulp;
2542 	int error;
2543 
2544 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_FSYNC_MASK);
2545 	if (error)
2546 		return (error);
2547 
2548 	if (TRANS_ISTRANS(ufsvfsp)) {
2549 		/*
2550 		 * First push out any data pages
2551 		 */
2552 		if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2553 		    (vp->v_type != VCHR) && !(IS_SWAPVP(vp))) {
2554 			error = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0,
2555 			    0, CRED());
2556 			if (error)
2557 				goto out;
2558 		}
2559 
2560 		/*
2561 		 * Delta any delayed inode times updates
2562 		 * and push inode to log.
2563 		 * All other inode deltas will have already been delta'd
2564 		 * and will be pushed during the commit.
2565 		 */
2566 		if (!(syncflag & FDSYNC) &&
2567 		    ((ip->i_flag & (IMOD|IMODACC)) == IMODACC)) {
2568 			if (ulp) {
2569 				TRANS_BEGIN_ASYNC(ufsvfsp, TOP_FSYNC,
2570 				    TOP_SYNCIP_SIZE);
2571 			}
2572 			rw_enter(&ip->i_contents, RW_READER);
2573 			mutex_enter(&ip->i_tlock);
2574 			ip->i_flag &= ~IMODTIME;
2575 			mutex_exit(&ip->i_tlock);
2576 			ufs_iupdat(ip, I_SYNC);
2577 			rw_exit(&ip->i_contents);
2578 			if (ulp) {
2579 				TRANS_END_ASYNC(ufsvfsp, TOP_FSYNC,
2580 				    TOP_SYNCIP_SIZE);
2581 			}
2582 		}
2583 
2584 		/*
2585 		 * Commit the Moby transaction
2586 		 *
2587 		 * Deltas have already been made so we just need to
2588 		 * commit them with a synchronous transaction.
2589 		 * TRANS_BEGIN_SYNC() will return an error
2590 		 * if there are no deltas to commit, for an
2591 		 * empty transaction.
2592 		 */
2593 		if (ulp) {
2594 			TRANS_BEGIN_SYNC(ufsvfsp, TOP_FSYNC, TOP_COMMIT_SIZE,
2595 			    error);
2596 			if (error) {
2597 				error = 0; /* commit wasn't needed */
2598 				goto out;
2599 			}
2600 			TRANS_END_SYNC(ufsvfsp, error, TOP_FSYNC,
2601 			    TOP_COMMIT_SIZE);
2602 		}
2603 	} else {	/* not logging */
2604 		if (!(IS_SWAPVP(vp)))
2605 			if (syncflag & FNODSYNC) {
2606 				/* Just update the inode only */
2607 				TRANS_IUPDAT(ip, 1);
2608 				error = 0;
2609 			} else if (syncflag & FDSYNC)
2610 				/* Do data-synchronous writes */
2611 				error = TRANS_SYNCIP(ip, 0, I_DSYNC, TOP_FSYNC);
2612 			else
2613 				/* Do synchronous writes */
2614 				error = TRANS_SYNCIP(ip, 0, I_SYNC, TOP_FSYNC);
2615 
2616 		rw_enter(&ip->i_contents, RW_WRITER);
2617 		if (!error)
2618 			error = ufs_sync_indir(ip);
2619 		rw_exit(&ip->i_contents);
2620 	}
2621 out:
2622 	if (ulp) {
2623 		ufs_lockfs_end(ulp);
2624 	}
2625 	return (error);
2626 }
2627 
2628 /*ARGSUSED*/
2629 static void
2630 ufs_inactive(struct vnode *vp, struct cred *cr)
2631 {
2632 	ufs_iinactive(VTOI(vp));
2633 }
2634 
2635 /*
2636  * Unix file system operations having to do with directory manipulation.
2637  */
2638 int ufs_lookup_idle_count = 2;	/* Number of inodes to idle each time */
2639 /* ARGSUSED */
2640 static int
2641 ufs_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
2642 	struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cr)
2643 {
2644 	struct inode *ip;
2645 	struct inode *sip;
2646 	struct inode *xip;
2647 	struct ufsvfs *ufsvfsp;
2648 	struct ulockfs *ulp;
2649 	struct vnode *vp;
2650 	int error;
2651 
2652 	/*
2653 	 * Check flags for type of lookup (regular file or attribute file)
2654 	 */
2655 
2656 	ip = VTOI(dvp);
2657 
2658 	if (flags & LOOKUP_XATTR) {
2659 
2660 		/*
2661 		 * We don't allow recursive attributes...
2662 		 * Maybe someday we will.
2663 		 */
2664 		if ((ip->i_cflags & IXATTR)) {
2665 			return (EINVAL);
2666 		}
2667 
2668 		if ((vp = dnlc_lookup(dvp, XATTR_DIR_NAME)) == NULL) {
2669 			error = ufs_xattr_getattrdir(dvp, &sip, flags, cr);
2670 			if (error) {
2671 				*vpp = NULL;
2672 				goto out;
2673 			}
2674 
2675 			vp = ITOV(sip);
2676 			dnlc_update(dvp, XATTR_DIR_NAME, vp);
2677 		}
2678 
2679 		/*
2680 		 * Check accessibility of directory.
2681 		 */
2682 		if (vp == DNLC_NO_VNODE) {
2683 			VN_RELE(vp);
2684 			error = ENOENT;
2685 			goto out;
2686 		}
2687 		if ((error = ufs_iaccess(VTOI(vp), IEXEC, cr)) != 0) {
2688 			VN_RELE(vp);
2689 			goto out;
2690 		}
2691 
2692 		*vpp = vp;
2693 		return (0);
2694 	}
2695 
2696 	/*
2697 	 * Check for a null component, which we should treat as
2698 	 * looking at dvp from within it's parent, so we don't
2699 	 * need a call to ufs_iaccess(), as it has already been
2700 	 * done.
2701 	 */
2702 	if (nm[0] == 0) {
2703 		VN_HOLD(dvp);
2704 		error = 0;
2705 		*vpp = dvp;
2706 		goto out;
2707 	}
2708 
2709 	/*
2710 	 * Check for "." ie itself. this is a quick check and
2711 	 * avoids adding "." into the dnlc (which have been seen
2712 	 * to occupy >10% of the cache).
2713 	 */
2714 	if ((nm[0] == '.') && (nm[1] == 0)) {
2715 		/*
2716 		 * Don't return without checking accessibility
2717 		 * of the directory. We only need the lock if
2718 		 * we are going to return it.
2719 		 */
2720 		if ((error = ufs_iaccess(ip, IEXEC, cr)) == 0) {
2721 			VN_HOLD(dvp);
2722 			*vpp = dvp;
2723 		}
2724 		goto out;
2725 	}
2726 
2727 	/*
2728 	 * Fast path: Check the directory name lookup cache.
2729 	 */
2730 	if (vp = dnlc_lookup(dvp, nm)) {
2731 		/*
2732 		 * Check accessibility of directory.
2733 		 */
2734 		if ((error = ufs_iaccess(ip, IEXEC, cr)) != 0) {
2735 			VN_RELE(vp);
2736 			goto out;
2737 		}
2738 		if (vp == DNLC_NO_VNODE) {
2739 			VN_RELE(vp);
2740 			error = ENOENT;
2741 			goto out;
2742 		}
2743 		xip = VTOI(vp);
2744 		ulp = NULL;
2745 		goto fastpath;
2746 	}
2747 
2748 	/*
2749 	 * Keep the idle queue from getting too long by
2750 	 * idling two inodes before attempting to allocate another.
2751 	 *    This operation must be performed before entering
2752 	 *    lockfs or a transaction.
2753 	 */
2754 	if (ufs_idle_q.uq_ne > ufs_idle_q.uq_hiwat)
2755 		if ((curthread->t_flag & T_DONTBLOCK) == 0) {
2756 			ins.in_lidles.value.ul += ufs_lookup_idle_count;
2757 			ufs_idle_some(ufs_lookup_idle_count);
2758 		}
2759 
2760 retry_lookup:
2761 	ufsvfsp = ip->i_ufsvfs;
2762 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LOOKUP_MASK);
2763 	if (error)
2764 		goto out;
2765 
2766 	error = ufs_dirlook(ip, nm, &xip, cr, 1);
2767 
2768 fastpath:
2769 	if (error == 0) {
2770 		ip = xip;
2771 		*vpp = ITOV(ip);
2772 
2773 		/*
2774 		 * If vnode is a device return special vnode instead.
2775 		 */
2776 		if (IS_DEVVP(*vpp)) {
2777 			struct vnode *newvp;
2778 
2779 			newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
2780 			    cr);
2781 			VN_RELE(*vpp);
2782 			if (newvp == NULL)
2783 				error = ENOSYS;
2784 			else
2785 				*vpp = newvp;
2786 		}
2787 	}
2788 	if (ulp) {
2789 		ufs_lockfs_end(ulp);
2790 	}
2791 
2792 	if (error == EAGAIN)
2793 		goto retry_lookup;
2794 
2795 out:
2796 	return (error);
2797 }
2798 
2799 static int
2800 ufs_create(struct vnode *dvp, char *name, struct vattr *vap, enum vcexcl excl,
2801 	int mode, struct vnode **vpp, struct cred *cr, int flag)
2802 {
2803 	struct inode *ip;
2804 	struct inode *xip;
2805 	struct inode *dip;
2806 	struct vnode *xvp;
2807 	struct ufsvfs *ufsvfsp;
2808 	struct ulockfs *ulp;
2809 	int error;
2810 	int issync;
2811 	int truncflag;
2812 	int trans_size;
2813 	int noentry;
2814 	int defer_dip_seq_update = 0;	/* need to defer update of dip->i_seq */
2815 	int retry = 1;
2816 	int indeadlock;
2817 
2818 again:
2819 	ip = VTOI(dvp);
2820 	ufsvfsp = ip->i_ufsvfs;
2821 	truncflag = 0;
2822 
2823 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_CREATE_MASK);
2824 	if (error)
2825 		goto out;
2826 
2827 	if (ulp) {
2828 		trans_size = (int)TOP_CREATE_SIZE(ip);
2829 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_CREATE, trans_size);
2830 	}
2831 
2832 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr) != 0)
2833 		vap->va_mode &= ~VSVTX;
2834 
2835 	if (*name == '\0') {
2836 		/*
2837 		 * Null component name refers to the directory itself.
2838 		 */
2839 		VN_HOLD(dvp);
2840 		/*
2841 		 * Even though this is an error case, we need to grab the
2842 		 * quota lock since the error handling code below is common.
2843 		 */
2844 		rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2845 		rw_enter(&ip->i_contents, RW_WRITER);
2846 		error = EEXIST;
2847 	} else {
2848 		xip = NULL;
2849 		noentry = 0;
2850 		/*
2851 		 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
2852 		 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
2853 		 * possible, retries the operation.
2854 		 */
2855 		ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_CREATE,
2856 		    retry_dir);
2857 		if (indeadlock)
2858 			goto again;
2859 
2860 		xvp = dnlc_lookup(dvp, name);
2861 		if (xvp == DNLC_NO_VNODE) {
2862 			noentry = 1;
2863 			VN_RELE(xvp);
2864 			xvp = NULL;
2865 		}
2866 		if (xvp) {
2867 			rw_exit(&ip->i_rwlock);
2868 			if (error = ufs_iaccess(ip, IEXEC, cr)) {
2869 				VN_RELE(xvp);
2870 			} else {
2871 				error = EEXIST;
2872 				xip = VTOI(xvp);
2873 			}
2874 		} else {
2875 			/*
2876 			 * Suppress file system full message if we will retry
2877 			 */
2878 			error = ufs_direnter_cm(ip, name, DE_CREATE,
2879 			    vap, &xip, cr, (noentry | (retry ? IQUIET : 0)));
2880 			if (error == EAGAIN) {
2881 				if (ulp) {
2882 					TRANS_END_CSYNC(ufsvfsp, error, issync,
2883 					    TOP_CREATE, trans_size);
2884 					ufs_lockfs_end(ulp);
2885 				}
2886 				goto again;
2887 			}
2888 			rw_exit(&ip->i_rwlock);
2889 		}
2890 		ip = xip;
2891 		if (ip != NULL) {
2892 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
2893 			rw_enter(&ip->i_contents, RW_WRITER);
2894 		}
2895 	}
2896 
2897 	/*
2898 	 * If the file already exists and this is a non-exclusive create,
2899 	 * check permissions and allow access for non-directories.
2900 	 * Read-only create of an existing directory is also allowed.
2901 	 * We fail an exclusive create of anything which already exists.
2902 	 */
2903 	if (error == EEXIST) {
2904 		dip = VTOI(dvp);
2905 		if (excl == NONEXCL) {
2906 			if ((((ip->i_mode & IFMT) == IFDIR) ||
2907 			    ((ip->i_mode & IFMT) == IFATTRDIR)) &&
2908 			    (mode & IWRITE))
2909 				error = EISDIR;
2910 			else if (mode)
2911 				error = ufs_iaccess(ip, mode, cr);
2912 			else
2913 				error = 0;
2914 		}
2915 		if (error) {
2916 			rw_exit(&ip->i_contents);
2917 			rw_exit(&ufsvfsp->vfs_dqrwlock);
2918 			VN_RELE(ITOV(ip));
2919 			goto unlock;
2920 		}
2921 		/*
2922 		 * If the error EEXIST was set, then i_seq can not
2923 		 * have been updated. The sequence number interface
2924 		 * is defined such that a non-error VOP_CREATE must
2925 		 * increase the dir va_seq it by at least one. If we
2926 		 * have cleared the error, increase i_seq. Note that
2927 		 * we are increasing the dir i_seq and in rare cases
2928 		 * ip may actually be from the dvp, so we already have
2929 		 * the locks and it will not be subject to truncation.
2930 		 * In case we have to update i_seq of the parent
2931 		 * directory dip, we have to defer it till we have
2932 		 * released our locks on ip due to lock ordering requirements.
2933 		 */
2934 		if (ip != dip)
2935 			defer_dip_seq_update = 1;
2936 		else
2937 			ip->i_seq++;
2938 
2939 		if (((ip->i_mode & IFMT) == IFREG) &&
2940 		    (vap->va_mask & AT_SIZE) && vap->va_size == 0) {
2941 			/*
2942 			 * Truncate regular files, if requested by caller.
2943 			 * Grab i_rwlock to make sure no one else is
2944 			 * currently writing to the file (we promised
2945 			 * bmap we would do this).
2946 			 * Must get the locks in the correct order.
2947 			 */
2948 			if (ip->i_size == 0) {
2949 				ip->i_flag |= ICHG | IUPD;
2950 				ip->i_seq++;
2951 				TRANS_INODE(ufsvfsp, ip);
2952 			} else {
2953 				/*
2954 				 * Large Files: Why this check here?
2955 				 * Though we do it in vn_create() we really
2956 				 * want to guarantee that we do not destroy
2957 				 * Large file data by atomically checking
2958 				 * the size while holding the contents
2959 				 * lock.
2960 				 */
2961 				if (flag && !(flag & FOFFMAX) &&
2962 				    ((ip->i_mode & IFMT) == IFREG) &&
2963 				    (ip->i_size > (offset_t)MAXOFF32_T)) {
2964 					rw_exit(&ip->i_contents);
2965 					rw_exit(&ufsvfsp->vfs_dqrwlock);
2966 					error = EOVERFLOW;
2967 					goto unlock;
2968 				}
2969 				if (TRANS_ISTRANS(ufsvfsp))
2970 					truncflag++;
2971 				else {
2972 					rw_exit(&ip->i_contents);
2973 					rw_exit(&ufsvfsp->vfs_dqrwlock);
2974 					ufs_tryirwlock_trans(&ip->i_rwlock,
2975 					    RW_WRITER, TOP_CREATE,
2976 					    retry_file);
2977 					if (indeadlock) {
2978 						VN_RELE(ITOV(ip));
2979 						goto again;
2980 					}
2981 					rw_enter(&ufsvfsp->vfs_dqrwlock,
2982 					    RW_READER);
2983 					rw_enter(&ip->i_contents, RW_WRITER);
2984 					(void) ufs_itrunc(ip, (u_offset_t)0, 0,
2985 					    cr);
2986 					rw_exit(&ip->i_rwlock);
2987 				}
2988 
2989 			}
2990 			if (error == 0) {
2991 				vnevent_create(ITOV(ip));
2992 			}
2993 		}
2994 	}
2995 
2996 	if (error) {
2997 		if (ip != NULL) {
2998 			rw_exit(&ufsvfsp->vfs_dqrwlock);
2999 			rw_exit(&ip->i_contents);
3000 		}
3001 		goto unlock;
3002 	}
3003 
3004 	*vpp = ITOV(ip);
3005 	ITIMES(ip);
3006 	rw_exit(&ip->i_contents);
3007 	rw_exit(&ufsvfsp->vfs_dqrwlock);
3008 
3009 	/*
3010 	 * If vnode is a device return special vnode instead.
3011 	 */
3012 	if (!error && IS_DEVVP(*vpp)) {
3013 		struct vnode *newvp;
3014 
3015 		newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
3016 		VN_RELE(*vpp);
3017 		if (newvp == NULL) {
3018 			error = ENOSYS;
3019 			goto unlock;
3020 		}
3021 		truncflag = 0;
3022 		*vpp = newvp;
3023 	}
3024 unlock:
3025 
3026 	/*
3027 	 * Do the deferred update of the parent directory's sequence
3028 	 * number now.
3029 	 */
3030 	if (defer_dip_seq_update == 1) {
3031 		rw_enter(&dip->i_contents, RW_READER);
3032 		mutex_enter(&dip->i_tlock);
3033 		dip->i_seq++;
3034 		mutex_exit(&dip->i_tlock);
3035 		rw_exit(&dip->i_contents);
3036 	}
3037 
3038 	if (ulp) {
3039 		int terr = 0;
3040 
3041 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_CREATE,
3042 		    trans_size);
3043 
3044 		/*
3045 		 * If we haven't had a more interesting failure
3046 		 * already, then anything that might've happened
3047 		 * here should be reported.
3048 		 */
3049 		if (error == 0)
3050 			error = terr;
3051 	}
3052 
3053 	if (!error && truncflag) {
3054 		ufs_tryirwlock(&ip->i_rwlock, RW_WRITER, retry_trunc);
3055 		if (indeadlock) {
3056 			if (ulp)
3057 				ufs_lockfs_end(ulp);
3058 			VN_RELE(ITOV(ip));
3059 			goto again;
3060 		}
3061 		(void) TRANS_ITRUNC(ip, (u_offset_t)0, 0, cr);
3062 		rw_exit(&ip->i_rwlock);
3063 	}
3064 
3065 	if (ulp)
3066 		ufs_lockfs_end(ulp);
3067 
3068 	/*
3069 	 * If no inodes available, try to free one up out of the
3070 	 * pending delete queue.
3071 	 */
3072 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
3073 		ufs_delete_drain_wait(ufsvfsp, 1);
3074 		retry = 0;
3075 		goto again;
3076 	}
3077 
3078 out:
3079 	return (error);
3080 }
3081 
3082 extern int ufs_idle_max;
3083 /*ARGSUSED*/
3084 static int
3085 ufs_remove(struct vnode *vp, char *nm, struct cred *cr)
3086 {
3087 	struct inode *ip = VTOI(vp);
3088 	struct ufsvfs *ufsvfsp	= ip->i_ufsvfs;
3089 	struct ulockfs *ulp;
3090 	vnode_t *rmvp = NULL;	/* Vnode corresponding to name being removed */
3091 	int indeadlock;
3092 	int error;
3093 	int issync;
3094 	int trans_size;
3095 
3096 	/*
3097 	 * don't let the delete queue get too long
3098 	 */
3099 	if (ufsvfsp == NULL) {
3100 		error = EIO;
3101 		goto out;
3102 	}
3103 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
3104 		ufs_delete_drain(vp->v_vfsp, 1, 1);
3105 
3106 retry_remove:
3107 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_REMOVE_MASK);
3108 	if (error)
3109 		goto out;
3110 
3111 	if (ulp)
3112 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_REMOVE,
3113 		    trans_size = (int)TOP_REMOVE_SIZE(VTOI(vp)));
3114 
3115 	/*
3116 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3117 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3118 	 * possible, retries the operation.
3119 	 */
3120 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_REMOVE, retry);
3121 	if (indeadlock)
3122 		goto retry_remove;
3123 	error = ufs_dirremove(ip, nm, (struct inode *)0, (struct vnode *)0,
3124 	    DR_REMOVE, cr, &rmvp);
3125 	rw_exit(&ip->i_rwlock);
3126 
3127 	if (ulp) {
3128 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_REMOVE, trans_size);
3129 		ufs_lockfs_end(ulp);
3130 	}
3131 
3132 	/*
3133 	 * This must be called after the remove transaction is closed.
3134 	 */
3135 	if (rmvp != NULL) {
3136 		/* Only send the event if there were no errors */
3137 		if (error == 0)
3138 			vnevent_remove(rmvp, vp, nm);
3139 		VN_RELE(rmvp);
3140 	}
3141 out:
3142 	return (error);
3143 }
3144 
3145 /*
3146  * Link a file or a directory.  Only privileged processes are allowed to
3147  * make links to directories.
3148  */
3149 static int
3150 ufs_link(struct vnode *tdvp, struct vnode *svp, char *tnm, struct cred *cr)
3151 {
3152 	struct inode *sip;
3153 	struct inode *tdp = VTOI(tdvp);
3154 	struct ufsvfs *ufsvfsp = tdp->i_ufsvfs;
3155 	struct ulockfs *ulp;
3156 	struct vnode *realvp;
3157 	int error;
3158 	int issync;
3159 	int trans_size;
3160 	int isdev;
3161 	int indeadlock;
3162 
3163 retry_link:
3164 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_LINK_MASK);
3165 	if (error)
3166 		goto out;
3167 
3168 	if (ulp)
3169 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_LINK,
3170 		    trans_size = (int)TOP_LINK_SIZE(VTOI(tdvp)));
3171 
3172 	if (VOP_REALVP(svp, &realvp) == 0)
3173 		svp = realvp;
3174 
3175 	/*
3176 	 * Make sure link for extended attributes is valid
3177 	 * We only support hard linking of attr in ATTRDIR to ATTRDIR
3178 	 *
3179 	 * Make certain we don't attempt to look at a device node as
3180 	 * a ufs inode.
3181 	 */
3182 
3183 	isdev = IS_DEVVP(svp);
3184 	if (((isdev == 0) && ((VTOI(svp)->i_cflags & IXATTR) == 0) &&
3185 	    ((tdp->i_mode & IFMT) == IFATTRDIR)) ||
3186 	    ((isdev == 0) && (VTOI(svp)->i_cflags & IXATTR) &&
3187 	    ((tdp->i_mode & IFMT) == IFDIR))) {
3188 		error = EINVAL;
3189 		goto unlock;
3190 	}
3191 
3192 	sip = VTOI(svp);
3193 	if ((svp->v_type == VDIR &&
3194 	    secpolicy_fs_linkdir(cr, ufsvfsp->vfs_vfs) != 0) ||
3195 	    (sip->i_uid != crgetuid(cr) && secpolicy_basic_link(cr) != 0)) {
3196 		error = EPERM;
3197 		goto unlock;
3198 	}
3199 
3200 	/*
3201 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3202 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3203 	 * possible, retries the operation.
3204 	 */
3205 	ufs_tryirwlock_trans(&tdp->i_rwlock, RW_WRITER, TOP_LINK, retry);
3206 	if (indeadlock)
3207 		goto retry_link;
3208 	error = ufs_direnter_lr(tdp, tnm, DE_LINK, (struct inode *)0,
3209 	    sip, cr, NULL);
3210 	rw_exit(&tdp->i_rwlock);
3211 
3212 unlock:
3213 	if (ulp) {
3214 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_LINK, trans_size);
3215 		ufs_lockfs_end(ulp);
3216 	}
3217 
3218 	if (!error) {
3219 		vnevent_link(svp);
3220 	}
3221 out:
3222 	return (error);
3223 }
3224 
3225 uint64_t ufs_rename_retry_cnt;
3226 uint64_t ufs_rename_upgrade_retry_cnt;
3227 uint64_t ufs_rename_dircheck_retry_cnt;
3228 clock_t	 ufs_rename_backoff_delay = 1;
3229 
3230 /*
3231  * Rename a file or directory.
3232  * We are given the vnode and entry string of the source and the
3233  * vnode and entry string of the place we want to move the source
3234  * to (the target). The essential operation is:
3235  *	unlink(target);
3236  *	link(source, target);
3237  *	unlink(source);
3238  * but "atomically".  Can't do full commit without saving state in
3239  * the inode on disk, which isn't feasible at this time.  Best we
3240  * can do is always guarantee that the TARGET exists.
3241  */
3242 
3243 /*ARGSUSED*/
3244 static int
3245 ufs_rename(
3246 	struct vnode *sdvp,		/* old (source) parent vnode */
3247 	char *snm,			/* old (source) entry name */
3248 	struct vnode *tdvp,		/* new (target) parent vnode */
3249 	char *tnm,			/* new (target) entry name */
3250 	struct cred *cr)
3251 {
3252 	struct inode *sip = NULL;	/* source inode */
3253 	struct inode *ip = NULL;	/* check inode */
3254 	struct inode *sdp;		/* old (source) parent inode */
3255 	struct inode *tdp;		/* new (target) parent inode */
3256 	struct vnode *tvp = NULL;	/* target vnode, if it exists */
3257 	struct vnode *realvp;
3258 	struct ufsvfs *ufsvfsp;
3259 	struct ulockfs *ulp;
3260 	struct ufs_slot slot;
3261 	timestruc_t now;
3262 	int error;
3263 	int issync;
3264 	int trans_size;
3265 	krwlock_t *first_lock;
3266 	krwlock_t *second_lock;
3267 	krwlock_t *reverse_lock;
3268 
3269 	sdp = VTOI(sdvp);
3270 	slot.fbp = NULL;
3271 	ufsvfsp = sdp->i_ufsvfs;
3272 retry_rename:
3273 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RENAME_MASK);
3274 	if (error)
3275 		goto out;
3276 
3277 	if (ulp)
3278 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RENAME,
3279 		    trans_size = (int)TOP_RENAME_SIZE(sdp));
3280 
3281 	if (VOP_REALVP(tdvp, &realvp) == 0)
3282 		tdvp = realvp;
3283 
3284 	tdp = VTOI(tdvp);
3285 
3286 
3287 	/*
3288 	 * We only allow renaming of attributes from ATTRDIR to ATTRDIR.
3289 	 */
3290 	if ((tdp->i_mode & IFMT) != (sdp->i_mode & IFMT)) {
3291 		error = EINVAL;
3292 		goto unlock;
3293 	}
3294 
3295 	/*
3296 	 * Look up inode of file we're supposed to rename.
3297 	 */
3298 	gethrestime(&now);
3299 	if (error = ufs_dirlook(sdp, snm, &sip, cr, 0)) {
3300 		if (error == EAGAIN) {
3301 			if (ulp) {
3302 				TRANS_END_CSYNC(ufsvfsp, error, issync,
3303 				    TOP_RENAME, trans_size);
3304 				ufs_lockfs_end(ulp);
3305 			}
3306 			goto retry_rename;
3307 		}
3308 
3309 		goto unlock;
3310 	}
3311 
3312 	/*
3313 	 * Lock both the source and target directories (they may be
3314 	 * the same) to provide the atomicity semantics that was
3315 	 * previously provided by the per file system vfs_rename_lock
3316 	 *
3317 	 * with vfs_rename_lock removed to allow simultaneous renames
3318 	 * within a file system, ufs_dircheckpath can deadlock while
3319 	 * traversing back to ensure that source is not a parent directory
3320 	 * of target parent directory. This is because we get into
3321 	 * ufs_dircheckpath with the sdp and tdp locks held as RW_WRITER.
3322 	 * If the tdp and sdp of the simultaneous renames happen to be
3323 	 * in the path of each other, it can lead to a deadlock. This
3324 	 * can be avoided by getting the locks as RW_READER here and then
3325 	 * upgrading to RW_WRITER after completing the ufs_dircheckpath.
3326 	 *
3327 	 * We hold the target directory's i_rwlock after calling
3328 	 * ufs_lockfs_begin but in many other operations (like ufs_readdir)
3329 	 * VOP_RWLOCK is explicitly called by the filesystem independent code
3330 	 * before calling the file system operation. In these cases the order
3331 	 * is reversed (i.e i_rwlock is taken first and then ufs_lockfs_begin
3332 	 * is called). This is fine as long as ufs_lockfs_begin acts as a VOP
3333 	 * counter but with ufs_quiesce setting the SLOCK bit this becomes a
3334 	 * synchronizing object which might lead to a deadlock. So we use
3335 	 * rw_tryenter instead of rw_enter. If we fail to get this lock and
3336 	 * find that SLOCK bit is set, we call ufs_lockfs_end and restart the
3337 	 * operation.
3338 	 */
3339 retry:
3340 	first_lock = &tdp->i_rwlock;
3341 	second_lock = &sdp->i_rwlock;
3342 retry_firstlock:
3343 	if (!rw_tryenter(first_lock, RW_READER)) {
3344 		/*
3345 		 * We didn't get the lock. Check if the SLOCK is set in the
3346 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3347 		 * and wait for SLOCK to be cleared.
3348 		 */
3349 
3350 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3351 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3352 			    trans_size);
3353 			ufs_lockfs_end(ulp);
3354 			goto retry_rename;
3355 
3356 		} else {
3357 			/*
3358 			 * SLOCK isn't set so this is a genuine synchronization
3359 			 * case. Let's try again after giving them a breather.
3360 			 */
3361 			delay(RETRY_LOCK_DELAY);
3362 			goto  retry_firstlock;
3363 		}
3364 	}
3365 	/*
3366 	 * Need to check if the tdp and sdp are same !!!
3367 	 */
3368 	if ((tdp != sdp) && (!rw_tryenter(second_lock, RW_READER))) {
3369 		/*
3370 		 * We didn't get the lock. Check if the SLOCK is set in the
3371 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
3372 		 * and wait for SLOCK to be cleared.
3373 		 */
3374 
3375 		rw_exit(first_lock);
3376 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
3377 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME,
3378 			    trans_size);
3379 			ufs_lockfs_end(ulp);
3380 			goto retry_rename;
3381 
3382 		} else {
3383 			/*
3384 			 * So we couldn't get the second level peer lock *and*
3385 			 * the SLOCK bit isn't set. Too bad we can be
3386 			 * contentding with someone wanting these locks otherway
3387 			 * round. Reverse the locks in case there is a heavy
3388 			 * contention for the second level lock.
3389 			 */
3390 			reverse_lock = first_lock;
3391 			first_lock = second_lock;
3392 			second_lock = reverse_lock;
3393 			ufs_rename_retry_cnt++;
3394 			goto  retry_firstlock;
3395 		}
3396 	}
3397 
3398 	if (sip == tdp) {
3399 		error = EINVAL;
3400 		goto errout;
3401 	}
3402 	/*
3403 	 * Make sure we can delete the source entry.  This requires
3404 	 * write permission on the containing directory.
3405 	 * Check for sticky directories.
3406 	 */
3407 	rw_enter(&sdp->i_contents, RW_READER);
3408 	rw_enter(&sip->i_contents, RW_READER);
3409 	if ((error = ufs_iaccess(sdp, IWRITE, cr)) != 0 ||
3410 	    (error = ufs_sticky_remove_access(sdp, sip, cr)) != 0) {
3411 		rw_exit(&sip->i_contents);
3412 		rw_exit(&sdp->i_contents);
3413 		goto errout;
3414 	}
3415 
3416 	/*
3417 	 * If this is a rename of a directory and the parent is
3418 	 * different (".." must be changed), then the source
3419 	 * directory must not be in the directory hierarchy
3420 	 * above the target, as this would orphan everything
3421 	 * below the source directory.  Also the user must have
3422 	 * write permission in the source so as to be able to
3423 	 * change "..".
3424 	 */
3425 	if ((((sip->i_mode & IFMT) == IFDIR) ||
3426 	    ((sip->i_mode & IFMT) == IFATTRDIR)) && sdp != tdp) {
3427 		ino_t	inum;
3428 
3429 		if ((error = ufs_iaccess(sip, IWRITE, cr))) {
3430 			rw_exit(&sip->i_contents);
3431 			rw_exit(&sdp->i_contents);
3432 			goto errout;
3433 		}
3434 		inum = sip->i_number;
3435 		rw_exit(&sip->i_contents);
3436 		rw_exit(&sdp->i_contents);
3437 		if ((error = ufs_dircheckpath(inum, tdp, sdp, cr))) {
3438 			/*
3439 			 * If we got EAGAIN ufs_dircheckpath detected a
3440 			 * potential deadlock and backed out. We need
3441 			 * to retry the operation since sdp and tdp have
3442 			 * to be released to avoid the deadlock.
3443 			 */
3444 			if (error == EAGAIN) {
3445 				rw_exit(&tdp->i_rwlock);
3446 				if (tdp != sdp)
3447 					rw_exit(&sdp->i_rwlock);
3448 				delay(ufs_rename_backoff_delay);
3449 				ufs_rename_dircheck_retry_cnt++;
3450 				goto retry;
3451 			}
3452 			goto errout;
3453 		}
3454 	} else {
3455 		rw_exit(&sip->i_contents);
3456 		rw_exit(&sdp->i_contents);
3457 	}
3458 
3459 
3460 	/*
3461 	 * Check for renaming '.' or '..' or alias of '.'
3462 	 */
3463 	if (strcmp(snm, ".") == 0 || strcmp(snm, "..") == 0 || sdp == sip) {
3464 		error = EINVAL;
3465 		goto errout;
3466 	}
3467 
3468 	/*
3469 	 * Simultaneous renames can deadlock in ufs_dircheckpath since it
3470 	 * tries to traverse back the file tree with both tdp and sdp held
3471 	 * as RW_WRITER. To avoid that we have to hold the tdp and sdp locks
3472 	 * as RW_READERS  till ufs_dircheckpath is done.
3473 	 * Now that ufs_dircheckpath is done with, we can upgrade the locks
3474 	 * to RW_WRITER.
3475 	 */
3476 	if (!rw_tryupgrade(&tdp->i_rwlock)) {
3477 		/*
3478 		 * The upgrade failed. We got to give away the lock
3479 		 * as to avoid deadlocking with someone else who is
3480 		 * waiting for writer lock. With the lock gone, we
3481 		 * cannot be sure the checks done above will hold
3482 		 * good when we eventually get them back as writer.
3483 		 * So if we can't upgrade we drop the locks and retry
3484 		 * everything again.
3485 		 */
3486 		rw_exit(&tdp->i_rwlock);
3487 		if (tdp != sdp)
3488 			rw_exit(&sdp->i_rwlock);
3489 		delay(ufs_rename_backoff_delay);
3490 		ufs_rename_upgrade_retry_cnt++;
3491 		goto retry;
3492 	}
3493 	if (tdp != sdp) {
3494 		if (!rw_tryupgrade(&sdp->i_rwlock)) {
3495 			/*
3496 			 * The upgrade failed. We got to give away the lock
3497 			 * as to avoid deadlocking with someone else who is
3498 			 * waiting for writer lock. With the lock gone, we
3499 			 * cannot be sure the checks done above will hold
3500 			 * good when we eventually get them back as writer.
3501 			 * So if we can't upgrade we drop the locks and retry
3502 			 * everything again.
3503 			 */
3504 			rw_exit(&tdp->i_rwlock);
3505 			rw_exit(&sdp->i_rwlock);
3506 			delay(ufs_rename_backoff_delay);
3507 			ufs_rename_upgrade_retry_cnt++;
3508 			goto retry;
3509 		}
3510 	}
3511 
3512 	/*
3513 	 * Now that all the locks are held check to make sure another thread
3514 	 * didn't slip in and take out the sip.
3515 	 */
3516 	slot.status = NONE;
3517 	if ((sip->i_ctime.tv_usec * 1000) > now.tv_nsec ||
3518 	    sip->i_ctime.tv_sec > now.tv_sec) {
3519 		rw_enter(&sdp->i_ufsvfs->vfs_dqrwlock, RW_READER);
3520 		rw_enter(&sdp->i_contents, RW_WRITER);
3521 		error = ufs_dircheckforname(sdp, snm, strlen(snm), &slot,
3522 		    &ip, cr, 0);
3523 		rw_exit(&sdp->i_contents);
3524 		rw_exit(&sdp->i_ufsvfs->vfs_dqrwlock);
3525 		if (error) {
3526 			goto errout;
3527 		}
3528 		if (ip == NULL) {
3529 			error = ENOENT;
3530 			goto errout;
3531 		} else {
3532 			/*
3533 			 * If the inode was found need to drop the v_count
3534 			 * so as not to keep the filesystem from being
3535 			 * unmounted at a later time.
3536 			 */
3537 			VN_RELE(ITOV(ip));
3538 		}
3539 
3540 		/*
3541 		 * Release the slot.fbp that has the page mapped and
3542 		 * locked SE_SHARED, and could be used in in
3543 		 * ufs_direnter_lr() which needs to get the SE_EXCL lock
3544 		 * on said page.
3545 		 */
3546 		if (slot.fbp) {
3547 			fbrelse(slot.fbp, S_OTHER);
3548 			slot.fbp = NULL;
3549 		}
3550 	}
3551 
3552 	/*
3553 	 * Link source to the target.  If a target exists, return its
3554 	 * vnode pointer in tvp.  We'll release it after sending the
3555 	 * vnevent.
3556 	 */
3557 	if (error = ufs_direnter_lr(tdp, tnm, DE_RENAME, sdp, sip, cr, &tvp)) {
3558 		/*
3559 		 * ESAME isn't really an error; it indicates that the
3560 		 * operation should not be done because the source and target
3561 		 * are the same file, but that no error should be reported.
3562 		 */
3563 		if (error == ESAME)
3564 			error = 0;
3565 		goto errout;
3566 	}
3567 
3568 	/*
3569 	 * Unlink the source.
3570 	 * Remove the source entry.  ufs_dirremove() checks that the entry
3571 	 * still reflects sip, and returns an error if it doesn't.
3572 	 * If the entry has changed just forget about it.  Release
3573 	 * the source inode.
3574 	 */
3575 	if ((error = ufs_dirremove(sdp, snm, sip, (struct vnode *)0,
3576 	    DR_RENAME, cr, NULL)) == ENOENT)
3577 		error = 0;
3578 
3579 errout:
3580 	if (slot.fbp)
3581 		fbrelse(slot.fbp, S_OTHER);
3582 
3583 	rw_exit(&tdp->i_rwlock);
3584 	if (sdp != tdp) {
3585 		rw_exit(&sdp->i_rwlock);
3586 	}
3587 
3588 unlock:
3589 	if (ulp) {
3590 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RENAME, trans_size);
3591 		ufs_lockfs_end(ulp);
3592 	}
3593 
3594 	/*
3595 	 * If no errors, send the appropriate events on the source
3596 	 * and destination (a.k.a, target) vnodes, if they exist.
3597 	 * This has to be done after the rename transaction has closed.
3598 	 */
3599 	if (error == 0) {
3600 		if (tvp != NULL)
3601 			vnevent_rename_dest(tvp, tdvp, tnm);
3602 
3603 		/*
3604 		 * Notify the target directory of the rename event
3605 		 * if source and target directories are not same.
3606 		 */
3607 		if (sdvp != tdvp)
3608 			vnevent_rename_dest_dir(tdvp);
3609 
3610 		/*
3611 		 * Note that if ufs_direnter_lr() returned ESAME then
3612 		 * this event will still be sent.  This isn't expected
3613 		 * to be a problem for anticipated usage by consumers.
3614 		 */
3615 		if (sip != NULL)
3616 			vnevent_rename_src(ITOV(sip), sdvp, snm);
3617 	}
3618 
3619 	if (tvp != NULL)
3620 		VN_RELE(tvp);
3621 
3622 	if (sip != NULL)
3623 		VN_RELE(ITOV(sip));
3624 
3625 out:
3626 	return (error);
3627 }
3628 
3629 /*ARGSUSED*/
3630 static int
3631 ufs_mkdir(struct vnode *dvp, char *dirname, struct vattr *vap,
3632 	struct vnode **vpp, struct cred *cr)
3633 {
3634 	struct inode *ip;
3635 	struct inode *xip;
3636 	struct ufsvfs *ufsvfsp;
3637 	struct ulockfs *ulp;
3638 	int error;
3639 	int issync;
3640 	int trans_size;
3641 	int indeadlock;
3642 	int retry = 1;
3643 
3644 	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));
3645 
3646 	/*
3647 	 * Can't make directory in attr hidden dir
3648 	 */
3649 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
3650 		return (EINVAL);
3651 
3652 again:
3653 	ip = VTOI(dvp);
3654 	ufsvfsp = ip->i_ufsvfs;
3655 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MKDIR_MASK);
3656 	if (error)
3657 		goto out;
3658 	if (ulp)
3659 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_MKDIR,
3660 		    trans_size = (int)TOP_MKDIR_SIZE(ip));
3661 
3662 	/*
3663 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3664 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3665 	 * possible, retries the operation.
3666 	 */
3667 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_MKDIR, retry);
3668 	if (indeadlock)
3669 		goto again;
3670 
3671 	error = ufs_direnter_cm(ip, dirname, DE_MKDIR, vap, &xip, cr,
3672 	    (retry ? IQUIET : 0));
3673 	if (error == EAGAIN) {
3674 		if (ulp) {
3675 			TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_MKDIR,
3676 			    trans_size);
3677 			ufs_lockfs_end(ulp);
3678 		}
3679 		goto again;
3680 	}
3681 
3682 	rw_exit(&ip->i_rwlock);
3683 	if (error == 0) {
3684 		ip = xip;
3685 		*vpp = ITOV(ip);
3686 	} else if (error == EEXIST)
3687 		VN_RELE(ITOV(xip));
3688 
3689 	if (ulp) {
3690 		int terr = 0;
3691 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_MKDIR, trans_size);
3692 		ufs_lockfs_end(ulp);
3693 		if (error == 0)
3694 			error = terr;
3695 	}
3696 out:
3697 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
3698 		ufs_delete_drain_wait(ufsvfsp, 1);
3699 		retry = 0;
3700 		goto again;
3701 	}
3702 
3703 	return (error);
3704 }
3705 
3706 /*ARGSUSED*/
3707 static int
3708 ufs_rmdir(struct vnode *vp, char *nm, struct vnode *cdir, struct cred *cr)
3709 {
3710 	struct inode *ip = VTOI(vp);
3711 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
3712 	struct ulockfs *ulp;
3713 	vnode_t *rmvp = NULL;	/* Vnode of removed directory */
3714 	int error;
3715 	int issync;
3716 	int trans_size;
3717 	int indeadlock;
3718 
3719 	/*
3720 	 * don't let the delete queue get too long
3721 	 */
3722 	if (ufsvfsp == NULL) {
3723 		error = EIO;
3724 		goto out;
3725 	}
3726 	if (ufsvfsp->vfs_delete.uq_ne > ufs_idle_max)
3727 		ufs_delete_drain(vp->v_vfsp, 1, 1);
3728 
3729 retry_rmdir:
3730 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_RMDIR_MASK);
3731 	if (error)
3732 		goto out;
3733 
3734 	if (ulp)
3735 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_RMDIR,
3736 		    trans_size = TOP_RMDIR_SIZE);
3737 
3738 	/*
3739 	 * ufs_tryirwlock_trans uses rw_tryenter and checks for SLOCK
3740 	 * to avoid i_rwlock, ufs_lockfs_begin deadlock. If deadlock
3741 	 * possible, retries the operation.
3742 	 */
3743 	ufs_tryirwlock_trans(&ip->i_rwlock, RW_WRITER, TOP_RMDIR, retry);
3744 	if (indeadlock)
3745 		goto retry_rmdir;
3746 	error = ufs_dirremove(ip, nm, (struct inode *)0, cdir, DR_RMDIR, cr,
3747 	    &rmvp);
3748 	rw_exit(&ip->i_rwlock);
3749 
3750 	if (ulp) {
3751 		TRANS_END_CSYNC(ufsvfsp, error, issync, TOP_RMDIR,
3752 		    trans_size);
3753 		ufs_lockfs_end(ulp);
3754 	}
3755 
3756 	/*
3757 	 * This must be done AFTER the rmdir transaction has closed.
3758 	 */
3759 	if (rmvp != NULL) {
3760 		/* Only send the event if there were no errors */
3761 		if (error == 0)
3762 			vnevent_rmdir(rmvp, vp, nm);
3763 		VN_RELE(rmvp);
3764 	}
3765 out:
3766 	return (error);
3767 }
3768 
3769 /* ARGSUSED */
3770 static int
3771 ufs_readdir(
3772 	struct vnode *vp,
3773 	struct uio *uiop,
3774 	struct cred *cr,
3775 	int *eofp)
3776 {
3777 	struct iovec *iovp;
3778 	struct inode *ip;
3779 	struct direct *idp;
3780 	struct dirent64 *odp;
3781 	struct fbuf *fbp;
3782 	struct ufsvfs *ufsvfsp;
3783 	struct ulockfs *ulp;
3784 	caddr_t outbuf;
3785 	size_t bufsize;
3786 	uint_t offset;
3787 	uint_t bytes_wanted, total_bytes_wanted;
3788 	int incount = 0;
3789 	int outcount = 0;
3790 	int error;
3791 
3792 	ip = VTOI(vp);
3793 	ASSERT(RW_READ_HELD(&ip->i_rwlock));
3794 
3795 	if (uiop->uio_loffset >= MAXOFF32_T) {
3796 		if (eofp)
3797 			*eofp = 1;
3798 		return (0);
3799 	}
3800 
3801 	/*
3802 	 * Check if we have been called with a valid iov_len
3803 	 * and bail out if not, otherwise we may potentially loop
3804 	 * forever further down.
3805 	 */
3806 	if (uiop->uio_iov->iov_len <= 0) {
3807 		error = EINVAL;
3808 		goto out;
3809 	}
3810 
3811 	/*
3812 	 * Large Files: When we come here we are guaranteed that
3813 	 * uio_offset can be used safely. The high word is zero.
3814 	 */
3815 
3816 	ufsvfsp = ip->i_ufsvfs;
3817 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_READDIR_MASK);
3818 	if (error)
3819 		goto out;
3820 
3821 	iovp = uiop->uio_iov;
3822 	total_bytes_wanted = iovp->iov_len;
3823 
3824 	/* Large Files: directory files should not be "large" */
3825 
3826 	ASSERT(ip->i_size <= MAXOFF32_T);
3827 
3828 	/* Force offset to be valid (to guard against bogus lseek() values) */
3829 	offset = (uint_t)uiop->uio_offset & ~(DIRBLKSIZ - 1);
3830 
3831 	/* Quit if at end of file or link count of zero (posix) */
3832 	if (offset >= (uint_t)ip->i_size || ip->i_nlink <= 0) {
3833 		if (eofp)
3834 			*eofp = 1;
3835 		error = 0;
3836 		goto unlock;
3837 	}
3838 
3839 	/*
3840 	 * Get space to change directory entries into fs independent format.
3841 	 * Do fast alloc for the most commonly used-request size (filesystem
3842 	 * block size).
3843 	 */
3844 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1) {
3845 		bufsize = total_bytes_wanted;
3846 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
3847 		odp = (struct dirent64 *)outbuf;
3848 	} else {
3849 		bufsize = total_bytes_wanted;
3850 		odp = (struct dirent64 *)iovp->iov_base;
3851 	}
3852 
3853 nextblk:
3854 	bytes_wanted = total_bytes_wanted;
3855 
3856 	/* Truncate request to file size */
3857 	if (offset + bytes_wanted > (int)ip->i_size)
3858 		bytes_wanted = (int)(ip->i_size - offset);
3859 
3860 	/* Comply with MAXBSIZE boundary restrictions of fbread() */
3861 	if ((offset & MAXBOFFSET) + bytes_wanted > MAXBSIZE)
3862 		bytes_wanted = MAXBSIZE - (offset & MAXBOFFSET);
3863 
3864 	/*
3865 	 * Read in the next chunk.
3866 	 * We are still holding the i_rwlock.
3867 	 */
3868 	error = fbread(vp, (offset_t)offset, bytes_wanted, S_OTHER, &fbp);
3869 
3870 	if (error)
3871 		goto update_inode;
3872 	if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) && (ip->i_fs->fs_ronly == 0) &&
3873 	    (!ufsvfsp->vfs_noatime)) {
3874 		ip->i_flag |= IACC;
3875 	}
3876 	incount = 0;
3877 	idp = (struct direct *)fbp->fb_addr;
3878 	if (idp->d_ino == 0 && idp->d_reclen == 0 && idp->d_namlen == 0) {
3879 		cmn_err(CE_WARN, "ufs_readdir: bad dir, inumber = %llu, "
3880 		    "fs = %s\n",
3881 		    (u_longlong_t)ip->i_number, ufsvfsp->vfs_fs->fs_fsmnt);
3882 		fbrelse(fbp, S_OTHER);
3883 		error = ENXIO;
3884 		goto update_inode;
3885 	}
3886 	/* Transform to file-system independent format */
3887 	while (incount < bytes_wanted) {
3888 		/*
3889 		 * If the current directory entry is mangled, then skip
3890 		 * to the next block.  It would be nice to set the FSBAD
3891 		 * flag in the super-block so that a fsck is forced on
3892 		 * next reboot, but locking is a problem.
3893 		 */
3894 		if (idp->d_reclen & 0x3) {
3895 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
3896 			break;
3897 		}
3898 
3899 		/* Skip to requested offset and skip empty entries */
3900 		if (idp->d_ino != 0 && offset >= (uint_t)uiop->uio_offset) {
3901 			ushort_t this_reclen =
3902 			    DIRENT64_RECLEN(idp->d_namlen);
3903 			/* Buffer too small for any entries */
3904 			if (!outcount && this_reclen > bufsize) {
3905 				fbrelse(fbp, S_OTHER);
3906 				error = EINVAL;
3907 				goto update_inode;
3908 			}
3909 			/* If would overrun the buffer, quit */
3910 			if (outcount + this_reclen > bufsize) {
3911 				break;
3912 			}
3913 			/* Take this entry */
3914 			odp->d_ino = (ino64_t)idp->d_ino;
3915 			odp->d_reclen = (ushort_t)this_reclen;
3916 			odp->d_off = (offset_t)(offset + idp->d_reclen);
3917 
3918 			/* use strncpy(9f) to zero out uninitialized bytes */
3919 
3920 			ASSERT(strlen(idp->d_name) + 1 <=
3921 			    DIRENT64_NAMELEN(this_reclen));
3922 			(void) strncpy(odp->d_name, idp->d_name,
3923 			    DIRENT64_NAMELEN(this_reclen));
3924 			outcount += odp->d_reclen;
3925 			odp = (struct dirent64 *)
3926 			    ((intptr_t)odp + odp->d_reclen);
3927 			ASSERT(outcount <= bufsize);
3928 		}
3929 		if (idp->d_reclen) {
3930 			incount += idp->d_reclen;
3931 			offset += idp->d_reclen;
3932 			idp = (struct direct *)((intptr_t)idp + idp->d_reclen);
3933 		} else {
3934 			offset = (offset + DIRBLKSIZ) & ~(DIRBLKSIZ-1);
3935 			break;
3936 		}
3937 	}
3938 	/* Release the chunk */
3939 	fbrelse(fbp, S_OTHER);
3940 
3941 	/* Read whole block, but got no entries, read another if not eof */
3942 
3943 	/*
3944 	 * Large Files: casting i_size to int here is not a problem
3945 	 * because directory sizes are always less than MAXOFF32_T.
3946 	 * See assertion above.
3947 	 */
3948 
3949 	if (offset < (int)ip->i_size && !outcount)
3950 		goto nextblk;
3951 
3952 	/* Copy out the entry data */
3953 	if (uiop->uio_segflg == UIO_SYSSPACE && uiop->uio_iovcnt == 1) {
3954 		iovp->iov_base += outcount;
3955 		iovp->iov_len -= outcount;
3956 		uiop->uio_resid -= outcount;
3957 		uiop->uio_offset = offset;
3958 	} else if ((error = uiomove(outbuf, (long)outcount, UIO_READ,
3959 	    uiop)) == 0)
3960 		uiop->uio_offset = offset;
3961 update_inode:
3962 	ITIMES(ip);
3963 	if (uiop->uio_segflg != UIO_SYSSPACE || uiop->uio_iovcnt != 1)
3964 		kmem_free(outbuf, bufsize);
3965 
3966 	if (eofp && error == 0)
3967 		*eofp = (uiop->uio_offset >= (int)ip->i_size);
3968 unlock:
3969 	if (ulp) {
3970 		ufs_lockfs_end(ulp);
3971 	}
3972 out:
3973 	return (error);
3974 }
3975 
3976 /*ARGSUSED*/
3977 static int
3978 ufs_symlink(
3979 	struct vnode *dvp,		/* ptr to parent dir vnode */
3980 	char *linkname,			/* name of symbolic link */
3981 	struct vattr *vap,		/* attributes */
3982 	char *target,			/* target path */
3983 	struct cred *cr)		/* user credentials */
3984 {
3985 	struct inode *ip, *dip = VTOI(dvp);
3986 	struct ufsvfs *ufsvfsp = dip->i_ufsvfs;
3987 	struct ulockfs *ulp;
3988 	int error;
3989 	int issync;
3990 	int trans_size;
3991 	int residual;
3992 	int ioflag;
3993 	int retry = 1;
3994 
3995 	/*
3996 	 * No symlinks in attrdirs at this time
3997 	 */
3998 	if ((VTOI(dvp)->i_mode & IFMT) == IFATTRDIR)
3999 		return (EINVAL);
4000 
4001 again:
4002 	ip = (struct inode *)NULL;
4003 	vap->va_type = VLNK;
4004 	vap->va_rdev = 0;
4005 
4006 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SYMLINK_MASK);
4007 	if (error)
4008 		goto out;
4009 
4010 	if (ulp)
4011 		TRANS_BEGIN_CSYNC(ufsvfsp, issync, TOP_SYMLINK,
4012 		    trans_size = (int)TOP_SYMLINK_SIZE(dip));
4013 
4014 	/*
4015 	 * We must create the inode before the directory entry, to avoid
4016 	 * racing with readlink().  ufs_dirmakeinode requires that we
4017 	 * hold the quota lock as reader, and directory locks as writer.
4018 	 */
4019 
4020 	rw_enter(&dip->i_rwlock, RW_WRITER);
4021 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4022 	rw_enter(&dip->i_contents, RW_WRITER);
4023 
4024 	/*
4025 	 * Suppress any out of inodes messages if we will retry on
4026 	 * ENOSP
4027 	 */
4028 	if (retry)
4029 		dip->i_flag |= IQUIET;
4030 
4031 	error = ufs_dirmakeinode(dip, &ip, vap, DE_SYMLINK, cr);
4032 
4033 	dip->i_flag &= ~IQUIET;
4034 
4035 	rw_exit(&dip->i_contents);
4036 	rw_exit(&ufsvfsp->vfs_dqrwlock);
4037 	rw_exit(&dip->i_rwlock);
4038 
4039 	if (error)
4040 		goto unlock;
4041 
4042 	/*
4043 	 * OK.  The inode has been created.  Write out the data of the
4044 	 * symbolic link.  Since symbolic links are metadata, and should
4045 	 * remain consistent across a system crash, we need to force the
4046 	 * data out synchronously.
4047 	 *
4048 	 * (This is a change from the semantics in earlier releases, which
4049 	 * only created symbolic links synchronously if the semi-documented
4050 	 * 'syncdir' option was set, or if we were being invoked by the NFS
4051 	 * server, which requires symbolic links to be created synchronously.)
4052 	 *
4053 	 * We need to pass in a pointer for the residual length; otherwise
4054 	 * ufs_rdwri() will always return EIO if it can't write the data,
4055 	 * even if the error was really ENOSPC or EDQUOT.
4056 	 */
4057 
4058 	ioflag = FWRITE | FDSYNC;
4059 	residual = 0;
4060 
4061 	rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4062 	rw_enter(&ip->i_contents, RW_WRITER);
4063 
4064 	/*
4065 	 * Suppress file system full messages if we will retry
4066 	 */
4067 	if (retry)
4068 		ip->i_flag |= IQUIET;
4069 
4070 	error = ufs_rdwri(UIO_WRITE, ioflag, ip, target, strlen(target),
4071 	    (offset_t)0, UIO_SYSSPACE, &residual, cr);
4072 
4073 	ip->i_flag &= ~IQUIET;
4074 
4075 	if (error) {
4076 		rw_exit(&ip->i_contents);
4077 		rw_exit(&ufsvfsp->vfs_dqrwlock);
4078 		goto remove;
4079 	}
4080 
4081 	/*
4082 	 * If the link's data is small enough, we can cache it in the inode.
4083 	 * This is a "fast symbolic link".  We don't use the first direct
4084 	 * block because that's actually used to point at the symbolic link's
4085 	 * contents on disk; but we know that none of the other direct or
4086 	 * indirect blocks can be used because symbolic links are restricted
4087 	 * to be smaller than a file system block.
4088 	 */
4089 
4090 	ASSERT(MAXPATHLEN <= VBSIZE(ITOV(ip)));
4091 
4092 	if (ip->i_size > 0 && ip->i_size <= FSL_SIZE) {
4093 		if (kcopy(target, &ip->i_db[1], ip->i_size) == 0) {
4094 			ip->i_flag |= IFASTSYMLNK;
4095 		} else {
4096 			int i;
4097 			/* error, clear garbage left behind */
4098 			for (i = 1; i < NDADDR; i++)
4099 				ip->i_db[i] = 0;
4100 			for (i = 0; i < NIADDR; i++)
4101 				ip->i_ib[i] = 0;
4102 		}
4103 	}
4104 
4105 	rw_exit(&ip->i_contents);
4106 	rw_exit(&ufsvfsp->vfs_dqrwlock);
4107 
4108 	/*
4109 	 * OK.  We've successfully created the symbolic link.  All that
4110 	 * remains is to insert it into the appropriate directory.
4111 	 */
4112 
4113 	rw_enter(&dip->i_rwlock, RW_WRITER);
4114 	error = ufs_direnter_lr(dip, linkname, DE_SYMLINK, NULL, ip, cr, NULL);
4115 	rw_exit(&dip->i_rwlock);
4116 
4117 	/*
4118 	 * Fall through into remove-on-error code.  We're either done, or we
4119 	 * need to remove the inode (if we couldn't insert it).
4120 	 */
4121 
4122 remove:
4123 	if (error && (ip != NULL)) {
4124 		rw_enter(&ip->i_contents, RW_WRITER);
4125 		ip->i_nlink--;
4126 		ip->i_flag |= ICHG;
4127 		ip->i_seq++;
4128 		ufs_setreclaim(ip);
4129 		rw_exit(&ip->i_contents);
4130 	}
4131 
4132 unlock:
4133 	if (ip != NULL)
4134 		VN_RELE(ITOV(ip));
4135 
4136 	if (ulp) {
4137 		int terr = 0;
4138 
4139 		TRANS_END_CSYNC(ufsvfsp, terr, issync, TOP_SYMLINK,
4140 		    trans_size);
4141 		ufs_lockfs_end(ulp);
4142 		if (error == 0)
4143 			error = terr;
4144 	}
4145 
4146 	/*
4147 	 * We may have failed due to lack of an inode or of a block to
4148 	 * store the target in.  Try flushing the delete queue to free
4149 	 * logically-available things up and try again.
4150 	 */
4151 	if ((error == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
4152 		ufs_delete_drain_wait(ufsvfsp, 1);
4153 		retry = 0;
4154 		goto again;
4155 	}
4156 
4157 out:
4158 	return (error);
4159 }
4160 
4161 /*
4162  * Ufs specific routine used to do ufs io.
4163  */
4164 int
4165 ufs_rdwri(enum uio_rw rw, int ioflag, struct inode *ip, caddr_t base,
4166 	ssize_t len, offset_t offset, enum uio_seg seg, int *aresid,
4167 	struct cred *cr)
4168 {
4169 	struct uio auio;
4170 	struct iovec aiov;
4171 	int error;
4172 
4173 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
4174 
4175 	bzero((caddr_t)&auio, sizeof (uio_t));
4176 	bzero((caddr_t)&aiov, sizeof (iovec_t));
4177 
4178 	aiov.iov_base = base;
4179 	aiov.iov_len = len;
4180 	auio.uio_iov = &aiov;
4181 	auio.uio_iovcnt = 1;
4182 	auio.uio_loffset = offset;
4183 	auio.uio_segflg = (short)seg;
4184 	auio.uio_resid = len;
4185 
4186 	if (rw == UIO_WRITE) {
4187 		auio.uio_fmode = FWRITE;
4188 		auio.uio_extflg = UIO_COPY_DEFAULT;
4189 		auio.uio_llimit = curproc->p_fsz_ctl;
4190 		error = wrip(ip, &auio, ioflag, cr);
4191 	} else {
4192 		auio.uio_fmode = FREAD;
4193 		auio.uio_extflg = UIO_COPY_CACHED;
4194 		auio.uio_llimit = MAXOFFSET_T;
4195 		error = rdip(ip, &auio, ioflag, cr);
4196 	}
4197 
4198 	if (aresid) {
4199 		*aresid = auio.uio_resid;
4200 	} else if (auio.uio_resid) {
4201 		error = EIO;
4202 	}
4203 	return (error);
4204 }
4205 
4206 static int
4207 ufs_fid(vp, fidp)
4208 	struct vnode *vp;
4209 	struct fid *fidp;
4210 {
4211 	struct ufid *ufid;
4212 	struct inode *ip = VTOI(vp);
4213 
4214 	if (ip->i_ufsvfs == NULL)
4215 		return (EIO);
4216 
4217 	if (fidp->fid_len < (sizeof (struct ufid) - sizeof (ushort_t))) {
4218 		fidp->fid_len = sizeof (struct ufid) - sizeof (ushort_t);
4219 		return (ENOSPC);
4220 	}
4221 
4222 	ufid = (struct ufid *)fidp;
4223 	bzero((char *)ufid, sizeof (struct ufid));
4224 	ufid->ufid_len = sizeof (struct ufid) - sizeof (ushort_t);
4225 	ufid->ufid_ino = ip->i_number;
4226 	ufid->ufid_gen = ip->i_gen;
4227 
4228 	return (0);
4229 }
4230 
4231 /* ARGSUSED2 */
4232 static int
4233 ufs_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
4234 {
4235 	struct inode	*ip = VTOI(vp);
4236 	struct ufsvfs	*ufsvfsp;
4237 	int		forcedirectio;
4238 
4239 	/*
4240 	 * Read case is easy.
4241 	 */
4242 	if (!write_lock) {
4243 		rw_enter(&ip->i_rwlock, RW_READER);
4244 		return (V_WRITELOCK_FALSE);
4245 	}
4246 
4247 	/*
4248 	 * Caller has requested a writer lock, but that inhibits any
4249 	 * concurrency in the VOPs that follow. Acquire the lock shared
4250 	 * and defer exclusive access until it is known to be needed in
4251 	 * other VOP handlers. Some cases can be determined here.
4252 	 */
4253 
4254 	/*
4255 	 * If directio is not set, there is no chance of concurrency,
4256 	 * so just acquire the lock exclusive. Beware of a forced
4257 	 * unmount before looking at the mount option.
4258 	 */
4259 	ufsvfsp = ip->i_ufsvfs;
4260 	forcedirectio = ufsvfsp ? ufsvfsp->vfs_forcedirectio : 0;
4261 	if (!(ip->i_flag & IDIRECTIO || forcedirectio) ||
4262 	    !ufs_allow_shared_writes) {
4263 		rw_enter(&ip->i_rwlock, RW_WRITER);
4264 		return (V_WRITELOCK_TRUE);
4265 	}
4266 
4267 	/*
4268 	 * Mandatory locking forces acquiring i_rwlock exclusive.
4269 	 */
4270 	if (MANDLOCK(vp, ip->i_mode)) {
4271 		rw_enter(&ip->i_rwlock, RW_WRITER);
4272 		return (V_WRITELOCK_TRUE);
4273 	}
4274 
4275 	/*
4276 	 * Acquire the lock shared in case a concurrent write follows.
4277 	 * Mandatory locking could have become enabled before the lock
4278 	 * was acquired. Re-check and upgrade if needed.
4279 	 */
4280 	rw_enter(&ip->i_rwlock, RW_READER);
4281 	if (MANDLOCK(vp, ip->i_mode)) {
4282 		rw_exit(&ip->i_rwlock);
4283 		rw_enter(&ip->i_rwlock, RW_WRITER);
4284 		return (V_WRITELOCK_TRUE);
4285 	}
4286 	return (V_WRITELOCK_FALSE);
4287 }
4288 
4289 /*ARGSUSED*/
4290 static void
4291 ufs_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
4292 {
4293 	struct inode	*ip = VTOI(vp);
4294 
4295 	rw_exit(&ip->i_rwlock);
4296 }
4297 
4298 /* ARGSUSED */
4299 static int
4300 ufs_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
4301 {
4302 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4303 }
4304 
4305 /* ARGSUSED */
4306 static int
4307 ufs_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
4308 	offset_t offset, struct flk_callback *flk_cbp, struct cred *cr)
4309 {
4310 	struct inode *ip = VTOI(vp);
4311 
4312 	if (ip->i_ufsvfs == NULL)
4313 		return (EIO);
4314 
4315 	/*
4316 	 * If file is being mapped, disallow frlock.
4317 	 * XXX I am not holding tlock while checking i_mapcnt because the
4318 	 * current locking strategy drops all locks before calling fs_frlock.
4319 	 * So, mapcnt could change before we enter fs_frlock making is
4320 	 * meaningless to have held tlock in the first place.
4321 	 */
4322 	if (ip->i_mapcnt > 0 && MANDLOCK(vp, ip->i_mode))
4323 		return (EAGAIN);
4324 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
4325 }
4326 
4327 /* ARGSUSED */
4328 static int
4329 ufs_space(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
4330 	offset_t offset, cred_t *cr, caller_context_t *ct)
4331 {
4332 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
4333 	struct ulockfs *ulp;
4334 	int error;
4335 
4336 	if ((error = convoff(vp, bfp, 0, offset)) == 0) {
4337 		if (cmd == F_FREESP) {
4338 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4339 			    ULOCKFS_SPACE_MASK);
4340 			if (error)
4341 				return (error);
4342 			error = ufs_freesp(vp, bfp, flag, cr);
4343 		} else if (cmd == F_ALLOCSP) {
4344 			error = ufs_lockfs_begin(ufsvfsp, &ulp,
4345 			    ULOCKFS_FALLOCATE_MASK);
4346 			if (error)
4347 				return (error);
4348 			error = ufs_allocsp(vp, bfp, cr);
4349 		} else
4350 			return (EINVAL); /* Command not handled here */
4351 
4352 		if (ulp)
4353 			ufs_lockfs_end(ulp);
4354 
4355 	}
4356 	return (error);
4357 }
4358 
4359 /*
4360  * Used to determine if read ahead should be done. Also used to
4361  * to determine when write back occurs.
4362  */
4363 #define	CLUSTSZ(ip)		((ip)->i_ufsvfs->vfs_ioclustsz)
4364 
4365 /*
4366  * A faster version of ufs_getpage.
4367  *
4368  * We optimize by inlining the pvn_getpages iterator, eliminating
4369  * calls to bmap_read if file doesn't have UFS holes, and avoiding
4370  * the overhead of page_exists().
4371  *
4372  * When files has UFS_HOLES and ufs_getpage is called with S_READ,
4373  * we set *protp to PROT_READ to avoid calling bmap_read. This approach
4374  * victimizes performance when a file with UFS holes is faulted
4375  * first in the S_READ mode, and then in the S_WRITE mode. We will get
4376  * two MMU faults in this case.
4377  *
4378  * XXX - the inode fields which control the sequential mode are not
4379  *	 protected by any mutex. The read ahead will act wild if
4380  *	 multiple processes will access the file concurrently and
4381  *	 some of them in sequential mode. One particulary bad case
4382  *	 is if another thread will change the value of i_nextrio between
4383  *	 the time this thread tests the i_nextrio value and then reads it
4384  *	 again to use it as the offset for the read ahead.
4385  */
4386 static int
4387 ufs_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
4388 	page_t *plarr[], size_t plsz, struct seg *seg, caddr_t addr,
4389 	enum seg_rw rw, struct cred *cr)
4390 {
4391 	u_offset_t	uoff = (u_offset_t)off; /* type conversion */
4392 	u_offset_t	pgoff;
4393 	u_offset_t	eoff;
4394 	struct inode 	*ip = VTOI(vp);
4395 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
4396 	struct fs 	*fs;
4397 	struct ulockfs	*ulp;
4398 	page_t		**pl;
4399 	caddr_t		pgaddr;
4400 	krw_t		rwtype;
4401 	int 		err;
4402 	int		has_holes;
4403 	int		beyond_eof;
4404 	int		seqmode;
4405 	int		pgsize = PAGESIZE;
4406 	int		dolock;
4407 	int		do_qlock;
4408 	int		trans_size;
4409 
4410 	ASSERT((uoff & PAGEOFFSET) == 0);
4411 
4412 	if (protp)
4413 		*protp = PROT_ALL;
4414 
4415 	/*
4416 	 * Obey the lockfs protocol
4417 	 */
4418 	err = ufs_lockfs_begin_getpage(ufsvfsp, &ulp, seg,
4419 	    rw == S_READ || rw == S_EXEC, protp);
4420 	if (err)
4421 		goto out;
4422 
4423 	fs = ufsvfsp->vfs_fs;
4424 
4425 	if (ulp && (rw == S_CREATE || rw == S_WRITE) &&
4426 	    !(vp->v_flag & VISSWAP)) {
4427 		/*
4428 		 * Try to start a transaction, will return if blocking is
4429 		 * expected to occur and the address space is not the
4430 		 * kernel address space.
4431 		 */
4432 		trans_size = TOP_GETPAGE_SIZE(ip);
4433 		if (seg->s_as != &kas) {
4434 			TRANS_TRY_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE,
4435 			    trans_size, err)
4436 			if (err == EWOULDBLOCK) {
4437 				/*
4438 				 * Use EDEADLK here because the VM code
4439 				 * can normally never see this error.
4440 				 */
4441 				err = EDEADLK;
4442 				ufs_lockfs_end(ulp);
4443 				goto out;
4444 			}
4445 		} else {
4446 			TRANS_BEGIN_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
4447 		}
4448 	}
4449 
4450 	if (vp->v_flag & VNOMAP) {
4451 		err = ENOSYS;
4452 		goto unlock;
4453 	}
4454 
4455 	seqmode = ip->i_nextr == uoff && rw != S_CREATE;
4456 
4457 	rwtype = RW_READER;		/* start as a reader */
4458 	dolock = (rw_owner(&ip->i_contents) != curthread);
4459 	/*
4460 	 * If this thread owns the lock, i.e., this thread grabbed it
4461 	 * as writer somewhere above, then we don't need to grab the
4462 	 * lock as reader in this routine.
4463 	 */
4464 	do_qlock = (rw_owner(&ufsvfsp->vfs_dqrwlock) != curthread);
4465 
4466 retrylock:
4467 	if (dolock) {
4468 		/*
4469 		 * Grab the quota lock if we need to call
4470 		 * bmap_write() below (with i_contents as writer).
4471 		 */
4472 		if (do_qlock && rwtype == RW_WRITER)
4473 			rw_enter(&ufsvfsp->vfs_dqrwlock, RW_READER);
4474 		rw_enter(&ip->i_contents, rwtype);
4475 	}
4476 
4477 	/*
4478 	 * We may be getting called as a side effect of a bmap using
4479 	 * fbread() when the blocks might be being allocated and the
4480 	 * size has not yet been up'ed.  In this case we want to be
4481 	 * able to return zero pages if we get back UFS_HOLE from
4482 	 * calling bmap for a non write case here.  We also might have
4483 	 * to read some frags from the disk into a page if we are
4484 	 * extending the number of frags for a given lbn in bmap().
4485 	 * Large Files: The read of i_size here is atomic because
4486 	 * i_contents is held here. If dolock is zero, the lock
4487 	 * is held in bmap routines.
4488 	 */
4489 	beyond_eof = uoff + len > ip->i_size + PAGEOFFSET;
4490 	if (beyond_eof && seg != segkmap) {
4491 		if (dolock) {
4492 			rw_exit(&ip->i_contents);
4493 			if (do_qlock && rwtype == RW_WRITER)
4494 				rw_exit(&ufsvfsp->vfs_dqrwlock);
4495 		}
4496 		err = EFAULT;
4497 		goto unlock;
4498 	}
4499 
4500 	/*
4501 	 * Must hold i_contents lock throughout the call to pvn_getpages
4502 	 * since locked pages are returned from each call to ufs_getapage.
4503 	 * Must *not* return locked pages and then try for contents lock
4504 	 * due to lock ordering requirements (inode > page)
4505 	 */
4506 
4507 	has_holes = bmap_has_holes(ip);
4508 
4509 	if ((rw == S_WRITE || rw == S_CREATE) && has_holes && !beyond_eof) {
4510 		int	blk_size;
4511 		u_offset_t offset;
4512 
4513 		/*
4514 		 * We must acquire the RW_WRITER lock in order to
4515 		 * call bmap_write().
4516 		 */
4517 		if (dolock && rwtype == RW_READER) {
4518 			rwtype = RW_WRITER;
4519 
4520 			/*
4521 			 * Grab the quota lock before
4522 			 * upgrading i_contents, but if we can't grab it
4523 			 * don't wait here due to lock order:
4524 			 * vfs_dqrwlock > i_contents.
4525 			 */
4526 			if (do_qlock &&
4527 			    rw_tryenter(&ufsvfsp->vfs_dqrwlock, RW_READER)
4528 			    == 0) {
4529 				rw_exit(&ip->i_contents);
4530 				goto retrylock;
4531 			}
4532 			if (!rw_tryupgrade(&ip->i_contents)) {
4533 				rw_exit(&ip->i_contents);
4534 				if (do_qlock)
4535 					rw_exit(&ufsvfsp->vfs_dqrwlock);
4536 				goto retrylock;
4537 			}
4538 		}
4539 
4540 		/*
4541 		 * May be allocating disk blocks for holes here as
4542 		 * a result of mmap faults. write(2) does the bmap_write
4543 		 * in rdip/wrip, not here. We are not dealing with frags
4544 		 * in this case.
4545 		 */
4546 		/*
4547 		 * Large Files: We cast fs_bmask field to offset_t
4548 		 * just as we do for MAXBMASK because uoff is a 64-bit
4549 		 * data type. fs_bmask will still be a 32-bit type
4550 		 * as we cannot change any ondisk data structures.
4551 		 */
4552 
4553 		offset = uoff & (offset_t)fs->fs_bmask;
4554 		while (offset < uoff + len) {
4555 			blk_size = (int)blksize(fs, ip, lblkno(fs, offset));
4556 			err = bmap_write(ip, offset, blk_size,
4557 			    BI_NORMAL, NULL, cr);
4558 			if (ip->i_flag & (ICHG|IUPD))
4559 				ip->i_seq++;
4560 			if (err)
4561 				goto update_inode;
4562 			offset += blk_size; /* XXX - make this contig */
4563 		}
4564 	}
4565 
4566 	/*
4567 	 * Can be a reader from now on.
4568 	 */
4569 	if (dolock && rwtype == RW_WRITER) {
4570 		rw_downgrade(&ip->i_contents);
4571 		/*
4572 		 * We can release vfs_dqrwlock early so do it, but make
4573 		 * sure we don't try to release it again at the bottom.
4574 		 */
4575 		if (do_qlock) {
4576 			rw_exit(&ufsvfsp->vfs_dqrwlock);
4577 			do_qlock = 0;
4578 		}
4579 	}
4580 
4581 	/*
4582 	 * We remove PROT_WRITE in cases when the file has UFS holes
4583 	 * because we don't  want to call bmap_read() to check each
4584 	 * page if it is backed with a disk block.
4585 	 */
4586 	if (protp && has_holes && rw != S_WRITE && rw != S_CREATE)
4587 		*protp &= ~PROT_WRITE;
4588 
4589 	err = 0;
4590 
4591 	/*
4592 	 * The loop looks up pages in the range [off, off + len).
4593 	 * For each page, we first check if we should initiate an asynchronous
4594 	 * read ahead before we call page_lookup (we may sleep in page_lookup
4595 	 * for a previously initiated disk read).
4596 	 */
4597 	eoff = (uoff + len);
4598 	for (pgoff = uoff, pgaddr = addr, pl = plarr;
4599 	    pgoff < eoff; /* empty */) {
4600 		page_t	*pp;
4601 		u_offset_t	nextrio;
4602 		se_t	se;
4603 		int retval;
4604 
4605 		se = ((rw == S_CREATE || rw == S_OTHER) ? SE_EXCL : SE_SHARED);
4606 
4607 		/* Handle async getpage (faultahead) */
4608 		if (plarr == NULL) {
4609 			ip->i_nextrio = pgoff;
4610 			(void) ufs_getpage_ra(vp, pgoff, seg, pgaddr);
4611 			pgoff += pgsize;
4612 			pgaddr += pgsize;
4613 			continue;
4614 		}
4615 		/*
4616 		 * Check if we should initiate read ahead of next cluster.
4617 		 * We call page_exists only when we need to confirm that
4618 		 * we have the current page before we initiate the read ahead.
4619 		 */
4620 		nextrio = ip->i_nextrio;
4621 		if (seqmode &&
4622 		    pgoff + CLUSTSZ(ip) >= nextrio && pgoff <= nextrio &&
4623 		    nextrio < ip->i_size && page_exists(vp, pgoff)) {
4624 			retval = ufs_getpage_ra(vp, pgoff, seg, pgaddr);
4625 			/*
4626 			 * We always read ahead the next cluster of data
4627 			 * starting from i_nextrio. If the page (vp,nextrio)
4628 			 * is actually in core at this point, the routine
4629 			 * ufs_getpage_ra() will stop pre-fetching data
4630 			 * until we read that page in a synchronized manner
4631 			 * through ufs_getpage_miss(). So, we should increase
4632 			 * i_nextrio if the page (vp, nextrio) exists.
4633 			 */
4634 			if ((retval == 0) && page_exists(vp, nextrio)) {
4635 				ip->i_nextrio = nextrio + pgsize;
4636 			}
4637 		}
4638 
4639 		if ((pp = page_lookup(vp, pgoff, se)) != NULL) {
4640 			/*
4641 			 * We found the page in the page cache.
4642 			 */
4643 			*pl++ = pp;
4644 			pgoff += pgsize;
4645 			pgaddr += pgsize;
4646 			len -= pgsize;
4647 			plsz -= pgsize;
4648 		} else  {
4649 			/*
4650 			 * We have to create the page, or read it from disk.
4651 			 */
4652 			if (err = ufs_getpage_miss(vp, pgoff, len, seg, pgaddr,
4653 			    pl, plsz, rw, seqmode))
4654 				goto error;
4655 
4656 			while (*pl != NULL) {
4657 				pl++;
4658 				pgoff += pgsize;
4659 				pgaddr += pgsize;
4660 				len -= pgsize;
4661 				plsz -= pgsize;
4662 			}
4663 		}
4664 	}
4665 
4666 	/*
4667 	 * Return pages up to plsz if they are in the page cache.
4668 	 * We cannot return pages if there is a chance that they are
4669 	 * backed with a UFS hole and rw is S_WRITE or S_CREATE.
4670 	 */
4671 	if (plarr && !(has_holes && (rw == S_WRITE || rw == S_CREATE))) {
4672 
4673 		ASSERT((protp == NULL) ||
4674 		    !(has_holes && (*protp & PROT_WRITE)));
4675 
4676 		eoff = pgoff + plsz;
4677 		while (pgoff < eoff) {
4678 			page_t		*pp;
4679 
4680 			if ((pp = page_lookup_nowait(vp, pgoff,
4681 			    SE_SHARED)) == NULL)
4682 				break;
4683 
4684 			*pl++ = pp;
4685 			pgoff += pgsize;
4686 			plsz -= pgsize;
4687 		}
4688 	}
4689 
4690 	if (plarr)
4691 		*pl = NULL;			/* Terminate page list */
4692 	ip->i_nextr = pgoff;
4693 
4694 error:
4695 	if (err && plarr) {
4696 		/*
4697 		 * Release any pages we have locked.
4698 		 */
4699 		while (pl > &plarr[0])
4700 			page_unlock(*--pl);
4701 
4702 		plarr[0] = NULL;
4703 	}
4704 
4705 update_inode:
4706 	/*
4707 	 * If the inode is not already marked for IACC (in rdip() for read)
4708 	 * and the inode is not marked for no access time update (in wrip()
4709 	 * for write) then update the inode access time and mod time now.
4710 	 */
4711 	if ((ip->i_flag & (IACC | INOACC)) == 0) {
4712 		if ((rw != S_OTHER) && (ip->i_mode & IFMT) != IFDIR) {
4713 			if (!ULOCKFS_IS_NOIACC(ITOUL(ip)) &&
4714 			    (fs->fs_ronly == 0) &&
4715 			    (!ufsvfsp->vfs_noatime)) {
4716 				mutex_enter(&ip->i_tlock);
4717 				ip->i_flag |= IACC;
4718 				ITIMES_NOLOCK(ip);
4719 				mutex_exit(&ip->i_tlock);
4720 			}
4721 		}
4722 	}
4723 
4724 	if (dolock) {
4725 		rw_exit(&ip->i_contents);
4726 		if (do_qlock && rwtype == RW_WRITER)
4727 			rw_exit(&ufsvfsp->vfs_dqrwlock);
4728 	}
4729 
4730 unlock:
4731 	if (ulp) {
4732 		if ((rw == S_CREATE || rw == S_WRITE) &&
4733 		    !(vp->v_flag & VISSWAP)) {
4734 			TRANS_END_ASYNC(ufsvfsp, TOP_GETPAGE, trans_size);
4735 		}
4736 		ufs_lockfs_end(ulp);
4737 	}
4738 out:
4739 	return (err);
4740 }
4741 
4742 /*
4743  * ufs_getpage_miss is called when ufs_getpage missed the page in the page
4744  * cache. The page is either read from the disk, or it's created.
4745  * A page is created (without disk read) if rw == S_CREATE, or if
4746  * the page is not backed with a real disk block (UFS hole).
4747  */
4748 /* ARGSUSED */
4749 static int
4750 ufs_getpage_miss(struct vnode *vp, u_offset_t off, size_t len, struct seg *seg,
4751 	caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw, int seq)
4752 {
4753 	struct inode	*ip = VTOI(vp);
4754 	page_t		*pp;
4755 	daddr_t		bn;
4756 	size_t		io_len;
4757 	int		crpage = 0;
4758 	int		err;
4759 	int		contig;
4760 	int		bsize = ip->i_fs->fs_bsize;
4761 
4762 	/*
4763 	 * Figure out whether the page can be created, or must be
4764 	 * must be read from the disk.
4765 	 */
4766 	if (rw == S_CREATE)
4767 		crpage = 1;
4768 	else {
4769 		contig = 0;
4770 		if (err = bmap_read(ip, off, &bn, &contig))
4771 			return (err);
4772 
4773 		crpage = (bn == UFS_HOLE);
4774 
4775 		/*
4776 		 * If its also a fallocated block that hasn't been written to
4777 		 * yet, we will treat it just like a UFS_HOLE and create
4778 		 * a zero page for it
4779 		 */
4780 		if (ISFALLOCBLK(ip, bn))
4781 			crpage = 1;
4782 	}
4783 
4784 	if (crpage) {
4785 		if ((pp = page_create_va(vp, off, PAGESIZE, PG_WAIT, seg,
4786 		    addr)) == NULL) {
4787 			return (ufs_fault(vp,
4788 			    "ufs_getpage_miss: page_create == NULL"));
4789 		}
4790 
4791 		if (rw != S_CREATE)
4792 			pagezero(pp, 0, PAGESIZE);
4793 
4794 		io_len = PAGESIZE;
4795 	} else {
4796 		u_offset_t	io_off;
4797 		uint_t	xlen;
4798 		struct buf	*bp;
4799 		ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
4800 
4801 		/*
4802 		 * If access is not in sequential order, we read from disk
4803 		 * in bsize units.
4804 		 *
4805 		 * We limit the size of the transfer to bsize if we are reading
4806 		 * from the beginning of the file. Note in this situation we
4807 		 * will hedge our bets and initiate an async read ahead of
4808 		 * the second block.
4809 		 */
4810 		if (!seq || off == 0)
4811 			contig = MIN(contig, bsize);
4812 
4813 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4814 		    &io_len, off, contig, 0);
4815 
4816 		/*
4817 		 * Some other thread has entered the page.
4818 		 * ufs_getpage will retry page_lookup.
4819 		 */
4820 		if (pp == NULL) {
4821 			pl[0] = NULL;
4822 			return (0);
4823 		}
4824 
4825 		/*
4826 		 * Zero part of the page which we are not
4827 		 * going to read from the disk.
4828 		 */
4829 		xlen = io_len & PAGEOFFSET;
4830 		if (xlen != 0)
4831 			pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
4832 
4833 		bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ);
4834 		bp->b_edev = ip->i_dev;
4835 		bp->b_dev = cmpdev(ip->i_dev);
4836 		bp->b_blkno = bn;
4837 		bp->b_un.b_addr = (caddr_t)0;
4838 		bp->b_file = ip->i_vnode;
4839 		bp->b_offset = off;
4840 
4841 		if (ufsvfsp->vfs_log) {
4842 			lufs_read_strategy(ufsvfsp->vfs_log, bp);
4843 		} else if (ufsvfsp->vfs_snapshot) {
4844 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
4845 		} else {
4846 			ufsvfsp->vfs_iotstamp = lbolt;
4847 			ub.ub_getpages.value.ul++;
4848 			(void) bdev_strategy(bp);
4849 			lwp_stat_update(LWP_STAT_INBLK, 1);
4850 		}
4851 
4852 		ip->i_nextrio = off + ((io_len + PAGESIZE - 1) & PAGEMASK);
4853 
4854 		/*
4855 		 * If the file access is sequential, initiate read ahead
4856 		 * of the next cluster.
4857 		 */
4858 		if (seq && ip->i_nextrio < ip->i_size)
4859 			(void) ufs_getpage_ra(vp, off, seg, addr);
4860 		err = biowait(bp);
4861 		pageio_done(bp);
4862 
4863 		if (err) {
4864 			pvn_read_done(pp, B_ERROR);
4865 			return (err);
4866 		}
4867 	}
4868 
4869 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4870 	return (0);
4871 }
4872 
4873 /*
4874  * Read ahead a cluster from the disk. Returns the length in bytes.
4875  */
4876 static int
4877 ufs_getpage_ra(struct vnode *vp, u_offset_t off, struct seg *seg, caddr_t addr)
4878 {
4879 	struct inode	*ip = VTOI(vp);
4880 	page_t		*pp;
4881 	u_offset_t	io_off = ip->i_nextrio;
4882 	ufsvfs_t	*ufsvfsp;
4883 	caddr_t		addr2 = addr + (io_off - off);
4884 	struct buf	*bp;
4885 	daddr_t		bn;
4886 	size_t		io_len;
4887 	int		err;
4888 	int		contig;
4889 	int		xlen;
4890 	int		bsize = ip->i_fs->fs_bsize;
4891 
4892 	/*
4893 	 * If the directio advisory is in effect on this file,
4894 	 * then do not do buffered read ahead. Read ahead makes
4895 	 * it more difficult on threads using directio as they
4896 	 * will be forced to flush the pages from this vnode.
4897 	 */
4898 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
4899 		return (0);
4900 	if (ip->i_flag & IDIRECTIO || ufsvfsp->vfs_forcedirectio)
4901 		return (0);
4902 
4903 	/*
4904 	 * Is this test needed?
4905 	 */
4906 	if (addr2 >= seg->s_base + seg->s_size)
4907 		return (0);
4908 
4909 	contig = 0;
4910 	err = bmap_read(ip, io_off, &bn, &contig);
4911 	/*
4912 	 * If its a UFS_HOLE or a fallocated block, do not perform
4913 	 * any read ahead's since there probably is nothing to read ahead
4914 	 */
4915 	if (err || bn == UFS_HOLE || ISFALLOCBLK(ip, bn))
4916 		return (0);
4917 
4918 	/*
4919 	 * Limit the transfer size to bsize if this is the 2nd block.
4920 	 */
4921 	if (io_off == (u_offset_t)bsize)
4922 		contig = MIN(contig, bsize);
4923 
4924 	if ((pp = pvn_read_kluster(vp, io_off, seg, addr2, &io_off,
4925 	    &io_len, io_off, contig, 1)) == NULL)
4926 		return (0);
4927 
4928 	/*
4929 	 * Zero part of page which we are not going to read from disk
4930 	 */
4931 	if ((xlen = (io_len & PAGEOFFSET)) > 0)
4932 		pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
4933 
4934 	ip->i_nextrio = (io_off + io_len + PAGESIZE - 1) & PAGEMASK;
4935 
4936 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_READ | B_ASYNC);
4937 	bp->b_edev = ip->i_dev;
4938 	bp->b_dev = cmpdev(ip->i_dev);
4939 	bp->b_blkno = bn;
4940 	bp->b_un.b_addr = (caddr_t)0;
4941 	bp->b_file = ip->i_vnode;
4942 	bp->b_offset = off;
4943 
4944 	if (ufsvfsp->vfs_log) {
4945 		lufs_read_strategy(ufsvfsp->vfs_log, bp);
4946 	} else if (ufsvfsp->vfs_snapshot) {
4947 		fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
4948 	} else {
4949 		ufsvfsp->vfs_iotstamp = lbolt;
4950 		ub.ub_getras.value.ul++;
4951 		(void) bdev_strategy(bp);
4952 		lwp_stat_update(LWP_STAT_INBLK, 1);
4953 	}
4954 
4955 	return (io_len);
4956 }
4957 
4958 int	ufs_delay = 1;
4959 /*
4960  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE, B_ASYNC}
4961  *
4962  * LMXXX - the inode really ought to contain a pointer to one of these
4963  * async args.  Stuff gunk in there and just hand the whole mess off.
4964  * This would replace i_delaylen, i_delayoff.
4965  */
4966 /*ARGSUSED*/
4967 static int
4968 ufs_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
4969 	struct cred *cr)
4970 {
4971 	struct inode *ip = VTOI(vp);
4972 	int err = 0;
4973 
4974 	if (vp->v_count == 0) {
4975 		return (ufs_fault(vp, "ufs_putpage: bad v_count == 0"));
4976 	}
4977 
4978 	/*
4979 	 * XXX - Why should this check be made here?
4980 	 */
4981 	if (vp->v_flag & VNOMAP) {
4982 		err = ENOSYS;
4983 		goto errout;
4984 	}
4985 
4986 	if (ip->i_ufsvfs == NULL) {
4987 		err = EIO;
4988 		goto errout;
4989 	}
4990 
4991 	if (flags & B_ASYNC) {
4992 		if (ufs_delay && len &&
4993 		    (flags & ~(B_ASYNC|B_DONTNEED|B_FREE)) == 0) {
4994 			mutex_enter(&ip->i_tlock);
4995 			/*
4996 			 * If nobody stalled, start a new cluster.
4997 			 */
4998 			if (ip->i_delaylen == 0) {
4999 				ip->i_delayoff = off;
5000 				ip->i_delaylen = len;
5001 				mutex_exit(&ip->i_tlock);
5002 				goto errout;
5003 			}
5004 			/*
5005 			 * If we have a full cluster or they are not contig,
5006 			 * then push last cluster and start over.
5007 			 */
5008 			if (ip->i_delaylen >= CLUSTSZ(ip) ||
5009 			    ip->i_delayoff + ip->i_delaylen != off) {
5010 				u_offset_t doff;
5011 				size_t dlen;
5012 
5013 				doff = ip->i_delayoff;
5014 				dlen = ip->i_delaylen;
5015 				ip->i_delayoff = off;
5016 				ip->i_delaylen = len;
5017 				mutex_exit(&ip->i_tlock);
5018 				err = ufs_putpages(vp, doff, dlen,
5019 				    flags, cr);
5020 				/* LMXXX - flags are new val, not old */
5021 				goto errout;
5022 			}
5023 			/*
5024 			 * There is something there, it's not full, and
5025 			 * it is contig.
5026 			 */
5027 			ip->i_delaylen += len;
5028 			mutex_exit(&ip->i_tlock);
5029 			goto errout;
5030 		}
5031 		/*
5032 		 * Must have weird flags or we are not clustering.
5033 		 */
5034 	}
5035 
5036 	err = ufs_putpages(vp, off, len, flags, cr);
5037 
5038 errout:
5039 	return (err);
5040 }
5041 
5042 /*
5043  * If len == 0, do from off to EOF.
5044  *
5045  * The normal cases should be len == 0 & off == 0 (entire vp list),
5046  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
5047  * (from pageout).
5048  */
5049 /*ARGSUSED*/
5050 static int
5051 ufs_putpages(
5052 	struct vnode *vp,
5053 	offset_t off,
5054 	size_t len,
5055 	int flags,
5056 	struct cred *cr)
5057 {
5058 	u_offset_t io_off;
5059 	u_offset_t eoff;
5060 	struct inode *ip = VTOI(vp);
5061 	page_t *pp;
5062 	size_t io_len;
5063 	int err = 0;
5064 	int dolock;
5065 
5066 	if (vp->v_count == 0)
5067 		return (ufs_fault(vp, "ufs_putpages: v_count == 0"));
5068 	/*
5069 	 * Acquire the readers/write inode lock before locking
5070 	 * any pages in this inode.
5071 	 * The inode lock is held during i/o.
5072 	 */
5073 	if (len == 0) {
5074 		mutex_enter(&ip->i_tlock);
5075 		ip->i_delayoff = ip->i_delaylen = 0;
5076 		mutex_exit(&ip->i_tlock);
5077 	}
5078 	dolock = (rw_owner(&ip->i_contents) != curthread);
5079 	if (dolock) {
5080 		/*
5081 		 * Must synchronize this thread and any possible thread
5082 		 * operating in the window of vulnerability in wrip().
5083 		 * It is dangerous to allow both a thread doing a putpage
5084 		 * and a thread writing, so serialize them.  The exception
5085 		 * is when the thread in wrip() does something which causes
5086 		 * a putpage operation.  Then, the thread must be allowed
5087 		 * to continue.  It may encounter a bmap_read problem in
5088 		 * ufs_putapage, but that is handled in ufs_putapage.
5089 		 * Allow async writers to proceed, we don't want to block
5090 		 * the pageout daemon.
5091 		 */
5092 		if (ip->i_writer == curthread)
5093 			rw_enter(&ip->i_contents, RW_READER);
5094 		else {
5095 			for (;;) {
5096 				rw_enter(&ip->i_contents, RW_READER);
5097 				mutex_enter(&ip->i_tlock);
5098 				/*
5099 				 * If there is no thread in the critical
5100 				 * section of wrip(), then proceed.
5101 				 * Otherwise, wait until there isn't one.
5102 				 */
5103 				if (ip->i_writer == NULL) {
5104 					mutex_exit(&ip->i_tlock);
5105 					break;
5106 				}
5107 				rw_exit(&ip->i_contents);
5108 				/*
5109 				 * Bounce async writers when we have a writer
5110 				 * working on this file so we don't deadlock
5111 				 * the pageout daemon.
5112 				 */
5113 				if (flags & B_ASYNC) {
5114 					mutex_exit(&ip->i_tlock);
5115 					return (0);
5116 				}
5117 				cv_wait(&ip->i_wrcv, &ip->i_tlock);
5118 				mutex_exit(&ip->i_tlock);
5119 			}
5120 		}
5121 	}
5122 
5123 	if (!vn_has_cached_data(vp)) {
5124 		if (dolock)
5125 			rw_exit(&ip->i_contents);
5126 		return (0);
5127 	}
5128 
5129 	if (len == 0) {
5130 		/*
5131 		 * Search the entire vp list for pages >= off.
5132 		 */
5133 		err = pvn_vplist_dirty(vp, (u_offset_t)off, ufs_putapage,
5134 		    flags, cr);
5135 	} else {
5136 		/*
5137 		 * Loop over all offsets in the range looking for
5138 		 * pages to deal with.
5139 		 */
5140 		if ((eoff = blkroundup(ip->i_fs, ip->i_size)) != 0)
5141 			eoff = MIN(off + len, eoff);
5142 		else
5143 			eoff = off + len;
5144 
5145 		for (io_off = off; io_off < eoff; io_off += io_len) {
5146 			/*
5147 			 * If we are not invalidating, synchronously
5148 			 * freeing or writing pages, use the routine
5149 			 * page_lookup_nowait() to prevent reclaiming
5150 			 * them from the free list.
5151 			 */
5152 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
5153 				pp = page_lookup(vp, io_off,
5154 				    (flags & (B_INVAL | B_FREE)) ?
5155 				    SE_EXCL : SE_SHARED);
5156 			} else {
5157 				pp = page_lookup_nowait(vp, io_off,
5158 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
5159 			}
5160 
5161 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
5162 				io_len = PAGESIZE;
5163 			else {
5164 				u_offset_t *io_offp = &io_off;
5165 
5166 				err = ufs_putapage(vp, pp, io_offp, &io_len,
5167 				    flags, cr);
5168 				if (err != 0)
5169 					break;
5170 				/*
5171 				 * "io_off" and "io_len" are returned as
5172 				 * the range of pages we actually wrote.
5173 				 * This allows us to skip ahead more quickly
5174 				 * since several pages may've been dealt
5175 				 * with by this iteration of the loop.
5176 				 */
5177 			}
5178 		}
5179 	}
5180 	if (err == 0 && off == 0 && (len == 0 || len >= ip->i_size)) {
5181 		/*
5182 		 * We have just sync'ed back all the pages on
5183 		 * the inode, turn off the IMODTIME flag.
5184 		 */
5185 		mutex_enter(&ip->i_tlock);
5186 		ip->i_flag &= ~IMODTIME;
5187 		mutex_exit(&ip->i_tlock);
5188 	}
5189 	if (dolock)
5190 		rw_exit(&ip->i_contents);
5191 	return (err);
5192 }
5193 
5194 static void
5195 ufs_iodone(buf_t *bp)
5196 {
5197 	struct inode *ip;
5198 
5199 	ASSERT((bp->b_pages->p_vnode != NULL) && !(bp->b_flags & B_READ));
5200 
5201 	bp->b_iodone = NULL;
5202 
5203 	ip = VTOI(bp->b_pages->p_vnode);
5204 
5205 	mutex_enter(&ip->i_tlock);
5206 	if (ip->i_writes >= ufs_LW) {
5207 		if ((ip->i_writes -= bp->b_bcount) <= ufs_LW)
5208 			if (ufs_WRITES)
5209 				cv_broadcast(&ip->i_wrcv); /* wake all up */
5210 	} else {
5211 		ip->i_writes -= bp->b_bcount;
5212 	}
5213 
5214 	mutex_exit(&ip->i_tlock);
5215 	iodone(bp);
5216 }
5217 
5218 /*
5219  * Write out a single page, possibly klustering adjacent
5220  * dirty pages.  The inode lock must be held.
5221  *
5222  * LMXXX - bsize < pagesize not done.
5223  */
5224 /*ARGSUSED*/
5225 int
5226 ufs_putapage(
5227 	struct vnode *vp,
5228 	page_t *pp,
5229 	u_offset_t *offp,
5230 	size_t *lenp,		/* return values */
5231 	int flags,
5232 	struct cred *cr)
5233 {
5234 	u_offset_t io_off;
5235 	u_offset_t off;
5236 	struct inode *ip = VTOI(vp);
5237 	struct ufsvfs *ufsvfsp = ip->i_ufsvfs;
5238 	struct fs *fs;
5239 	struct buf *bp;
5240 	size_t io_len;
5241 	daddr_t bn;
5242 	int err;
5243 	int contig;
5244 
5245 	ASSERT(RW_LOCK_HELD(&ip->i_contents));
5246 
5247 	if (ufsvfsp == NULL) {
5248 		err = EIO;
5249 		goto out_trace;
5250 	}
5251 
5252 	fs = ip->i_fs;
5253 	ASSERT(fs->fs_ronly == 0);
5254 
5255 	/*
5256 	 * If the modified time on the inode has not already been
5257 	 * set elsewhere (e.g. for write/setattr) we set the time now.
5258 	 * This gives us approximate modified times for mmap'ed files
5259 	 * which are modified via stores in the user address space.
5260 	 */
5261 	if ((ip->i_flag & IMODTIME) == 0) {
5262 		mutex_enter(&ip->i_tlock);
5263 		ip->i_flag |= IUPD;
5264 		ip->i_seq++;
5265 		ITIMES_NOLOCK(ip);
5266 		mutex_exit(&ip->i_tlock);
5267 	}
5268 
5269 	/*
5270 	 * Align the request to a block boundry (for old file systems),
5271 	 * and go ask bmap() how contiguous things are for this file.
5272 	 */
5273 	off = pp->p_offset & (offset_t)fs->fs_bmask;	/* block align it */
5274 	contig = 0;
5275 	err = bmap_read(ip, off, &bn, &contig);
5276 	if (err)
5277 		goto out;
5278 	if (bn == UFS_HOLE) {			/* putpage never allocates */
5279 		/*
5280 		 * logging device is in error mode; simply return EIO
5281 		 */
5282 		if (TRANS_ISERROR(ufsvfsp)) {
5283 			err = EIO;
5284 			goto out;
5285 		}
5286 		/*
5287 		 * Oops, the thread in the window in wrip() did some
5288 		 * sort of operation which caused a putpage in the bad
5289 		 * range.  In this case, just return an error which will
5290 		 * cause the software modified bit on the page to set
5291 		 * and the page will get written out again later.
5292 		 */
5293 		if (ip->i_writer == curthread) {
5294 			err = EIO;
5295 			goto out;
5296 		}
5297 		/*
5298 		 * If the pager is trying to push a page in the bad range
5299 		 * just tell him to try again later when things are better.
5300 		 */
5301 		if (flags & B_ASYNC) {
5302 			err = EAGAIN;
5303 			goto out;
5304 		}
5305 		err = ufs_fault(ITOV(ip), "ufs_putapage: bn == UFS_HOLE");
5306 		goto out;
5307 	}
5308 
5309 	/*
5310 	 * If it is an fallocate'd block, reverse the negativity since
5311 	 * we are now writing to it
5312 	 */
5313 	if (ISFALLOCBLK(ip, bn)) {
5314 		err = bmap_set_bn(vp, off, dbtofsb(fs, -bn));
5315 		if (err)
5316 			goto out;
5317 
5318 		bn = -bn;
5319 	}
5320 
5321 	/*
5322 	 * Take the length (of contiguous bytes) passed back from bmap()
5323 	 * and _try_ and get a set of pages covering that extent.
5324 	 */
5325 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, off, contig, flags);
5326 
5327 	/*
5328 	 * May have run out of memory and not clustered backwards.
5329 	 * off		p_offset
5330 	 * [  pp - 1  ][   pp   ]
5331 	 * [	block		]
5332 	 * We told bmap off, so we have to adjust the bn accordingly.
5333 	 */
5334 	if (io_off > off) {
5335 		bn += btod(io_off - off);
5336 		contig -= (io_off - off);
5337 	}
5338 
5339 	/*
5340 	 * bmap was carefull to tell us the right size so use that.
5341 	 * There might be unallocated frags at the end.
5342 	 * LMXXX - bzero the end of the page?  We must be writing after EOF.
5343 	 */
5344 	if (io_len > contig) {
5345 		ASSERT(io_len - contig < fs->fs_bsize);
5346 		io_len -= (io_len - contig);
5347 	}
5348 
5349 	/*
5350 	 * Handle the case where we are writing the last page after EOF.
5351 	 *
5352 	 * XXX - just a patch for i-mt3.
5353 	 */
5354 	if (io_len == 0) {
5355 		ASSERT(pp->p_offset >=
5356 		    (u_offset_t)(roundup(ip->i_size, PAGESIZE)));
5357 		io_len = PAGESIZE;
5358 	}
5359 
5360 	bp = pageio_setup(pp, io_len, ip->i_devvp, B_WRITE | flags);
5361 
5362 	ULOCKFS_SET_MOD(ITOUL(ip));
5363 
5364 	bp->b_edev = ip->i_dev;
5365 	bp->b_dev = cmpdev(ip->i_dev);
5366 	bp->b_blkno = bn;
5367 	bp->b_un.b_addr = (caddr_t)0;
5368 	bp->b_file = ip->i_vnode;
5369 
5370 	if (TRANS_ISTRANS(ufsvfsp)) {
5371 		if ((ip->i_mode & IFMT) == IFSHAD) {
5372 			TRANS_BUF(ufsvfsp, 0, io_len, bp, DT_SHAD);
5373 		} else if (ufsvfsp->vfs_qinod == ip) {
5374 			TRANS_DELTA(ufsvfsp, ldbtob(bn), bp->b_bcount, DT_QR,
5375 			    0, 0);
5376 		}
5377 	}
5378 
5379 	/* write throttle */
5380 
5381 	ASSERT(bp->b_iodone == NULL);
5382 	bp->b_iodone = (int (*)())ufs_iodone;
5383 	mutex_enter(&ip->i_tlock);
5384 	ip->i_writes += bp->b_bcount;
5385 	mutex_exit(&ip->i_tlock);
5386 
5387 	if (bp->b_flags & B_ASYNC) {
5388 		if (ufsvfsp->vfs_log) {
5389 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
5390 		} else if (ufsvfsp->vfs_snapshot) {
5391 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
5392 		} else {
5393 			ufsvfsp->vfs_iotstamp = lbolt;
5394 			ub.ub_putasyncs.value.ul++;
5395 			(void) bdev_strategy(bp);
5396 			lwp_stat_update(LWP_STAT_OUBLK, 1);
5397 		}
5398 	} else {
5399 		if (ufsvfsp->vfs_log) {
5400 			lufs_write_strategy(ufsvfsp->vfs_log, bp);
5401 		} else if (ufsvfsp->vfs_snapshot) {
5402 			fssnap_strategy(&ufsvfsp->vfs_snapshot, bp);
5403 		} else {
5404 			ufsvfsp->vfs_iotstamp = lbolt;
5405 			ub.ub_putsyncs.value.ul++;
5406 			(void) bdev_strategy(bp);
5407 			lwp_stat_update(LWP_STAT_OUBLK, 1);
5408 		}
5409 		err = biowait(bp);
5410 		pageio_done(bp);
5411 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
5412 	}
5413 
5414 	pp = NULL;
5415 
5416 out:
5417 	if (err != 0 && pp != NULL)
5418 		pvn_write_done(pp, B_ERROR | B_WRITE | flags);
5419 
5420 	if (offp)
5421 		*offp = io_off;
5422 	if (lenp)
5423 		*lenp = io_len;
5424 out_trace:
5425 	return (err);
5426 }
5427 
5428 /* ARGSUSED */
5429 static int
5430 ufs_map(struct vnode *vp,
5431 	offset_t off,
5432 	struct as *as,
5433 	caddr_t *addrp,
5434 	size_t len,
5435 	uchar_t prot,
5436 	uchar_t maxprot,
5437 	uint_t flags,
5438 	struct cred *cr)
5439 {
5440 	struct segvn_crargs vn_a;
5441 	struct ufsvfs *ufsvfsp = VTOI(vp)->i_ufsvfs;
5442 	struct ulockfs *ulp;
5443 	int error;
5444 
5445 retry_map:
5446 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_MAP_MASK);
5447 	if (error)
5448 		goto out;
5449 
5450 	if (vp->v_flag & VNOMAP) {
5451 		error = ENOSYS;
5452 		goto unlock;
5453 	}
5454 
5455 	if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0) {
5456 		error = ENXIO;
5457 		goto unlock;
5458 	}
5459 
5460 	if (vp->v_type != VREG) {
5461 		error = ENODEV;
5462 		goto unlock;
5463 	}
5464 
5465 	/*
5466 	 * If file is being locked, disallow mapping.
5467 	 */
5468 	if (vn_has_mandatory_locks(vp, VTOI(vp)->i_mode)) {
5469 		error = EAGAIN;
5470 		goto unlock;
5471 	}
5472 
5473 	as_rangelock(as);
5474 	if ((flags & MAP_FIXED) == 0) {
5475 		map_addr(addrp, len, off, 1, flags);
5476 		if (*addrp == NULL) {
5477 			as_rangeunlock(as);
5478 			error = ENOMEM;
5479 			goto unlock;
5480 		}
5481 	} else {
5482 		/*
5483 		 * User specified address - blow away any previous mappings
5484 		 */
5485 		(void) as_unmap(as, *addrp, len);
5486 	}
5487 
5488 	vn_a.vp = vp;
5489 	vn_a.offset = (u_offset_t)off;
5490 	vn_a.type = flags & MAP_TYPE;
5491 	vn_a.prot = prot;
5492 	vn_a.maxprot = maxprot;
5493 	vn_a.cred = cr;
5494 	vn_a.amp = NULL;
5495 	vn_a.flags = flags & ~MAP_TYPE;
5496 	vn_a.szc = 0;
5497 	vn_a.lgrp_mem_policy_flags = 0;
5498 
5499 retry_lock:
5500 	if (!AS_LOCK_TRYENTER(ias, &as->a_lock, RW_WRITER)) {
5501 		/*
5502 		 * We didn't get the lock. Check if the SLOCK is set in the
5503 		 * ufsvfs. If yes, we might be in a deadlock. Safer to give up
5504 		 * and wait for SLOCK to be cleared.
5505 		 */
5506 
5507 		if (ulp && ULOCKFS_IS_SLOCK(ulp)) {
5508 			as_rangeunlock(as);
5509 			ufs_lockfs_end(ulp);
5510 			goto retry_map;
5511 		} else {
5512 			/*
5513 			 * SLOCK isn't set so this is a genuine synchronization
5514 			 * case. Let's try again after giving them a breather.
5515 			 */
5516 			delay(RETRY_LOCK_DELAY);
5517 			goto  retry_lock;
5518 		}
5519 	}
5520 	error = as_map_locked(as, *addrp, len, segvn_create, &vn_a);
5521 	as_rangeunlock(as);
5522 
5523 unlock:
5524 	if (ulp) {
5525 		ufs_lockfs_end(ulp);
5526 	}
5527 out:
5528 	return (error);
5529 }
5530 
5531 /* ARGSUSED */
5532 static int
5533 ufs_addmap(struct vnode *vp,
5534 	offset_t off,
5535 	struct as *as,
5536 	caddr_t addr,
5537 	size_t	len,
5538 	uchar_t  prot,
5539 	uchar_t  maxprot,
5540 	uint_t    flags,
5541 	struct cred *cr)
5542 {
5543 	struct inode *ip = VTOI(vp);
5544 
5545 	if (vp->v_flag & VNOMAP) {
5546 		return (ENOSYS);
5547 	}
5548 
5549 	mutex_enter(&ip->i_tlock);
5550 	ip->i_mapcnt += btopr(len);
5551 	mutex_exit(&ip->i_tlock);
5552 	return (0);
5553 }
5554 
5555 /*ARGSUSED*/
5556 static int
5557 ufs_delmap(struct vnode *vp, offset_t off, struct as *as, caddr_t addr,
5558 	size_t len, uint_t prot,  uint_t maxprot,  uint_t flags,
5559 	struct cred *cr)
5560 {
5561 	struct inode *ip = VTOI(vp);
5562 
5563 	if (vp->v_flag & VNOMAP) {
5564 		return (ENOSYS);
5565 	}
5566 
5567 	mutex_enter(&ip->i_tlock);
5568 	ip->i_mapcnt -= btopr(len); 	/* Count released mappings */
5569 	ASSERT(ip->i_mapcnt >= 0);
5570 	mutex_exit(&ip->i_tlock);
5571 	return (0);
5572 }
5573 /*
5574  * Return the answer requested to poll() for non-device files
5575  */
5576 struct pollhead ufs_pollhd;
5577 
5578 /* ARGSUSED */
5579 int
5580 ufs_poll(vnode_t *vp, short ev, int any, short *revp, struct pollhead **phpp)
5581 {
5582 	struct ufsvfs	*ufsvfsp;
5583 
5584 	*revp = 0;
5585 	ufsvfsp = VTOI(vp)->i_ufsvfs;
5586 
5587 	if (!ufsvfsp) {
5588 		*revp = POLLHUP;
5589 		goto out;
5590 	}
5591 
5592 	if (ULOCKFS_IS_HLOCK(&ufsvfsp->vfs_ulockfs) ||
5593 	    ULOCKFS_IS_ELOCK(&ufsvfsp->vfs_ulockfs)) {
5594 		*revp |= POLLERR;
5595 
5596 	} else {
5597 		if ((ev & POLLOUT) && !ufsvfsp->vfs_fs->fs_ronly &&
5598 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
5599 			*revp |= POLLOUT;
5600 
5601 		if ((ev & POLLWRBAND) && !ufsvfsp->vfs_fs->fs_ronly &&
5602 		    !ULOCKFS_IS_WLOCK(&ufsvfsp->vfs_ulockfs))
5603 			*revp |= POLLWRBAND;
5604 
5605 		if (ev & POLLIN)
5606 			*revp |= POLLIN;
5607 
5608 		if (ev & POLLRDNORM)
5609 			*revp |= POLLRDNORM;
5610 
5611 		if (ev & POLLRDBAND)
5612 			*revp |= POLLRDBAND;
5613 	}
5614 
5615 	if ((ev & POLLPRI) && (*revp & (POLLERR|POLLHUP)))
5616 		*revp |= POLLPRI;
5617 out:
5618 	*phpp = !any && !*revp ? &ufs_pollhd : (struct pollhead *)NULL;
5619 
5620 	return (0);
5621 }
5622 
5623 /* ARGSUSED */
5624 static int
5625 ufs_l_pathconf(struct vnode *vp, int cmd, ulong_t *valp, struct cred *cr)
5626 {
5627 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
5628 	struct ulockfs	*ulp = NULL;
5629 	struct inode 	*sip = NULL;
5630 	int		error;
5631 	struct inode 	*ip = VTOI(vp);
5632 	int		issync;
5633 
5634 	error = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_PATHCONF_MASK);
5635 	if (error)
5636 		return (error);
5637 
5638 	switch (cmd) {
5639 		/*
5640 		 * Have to handle _PC_NAME_MAX here, because the normal way
5641 		 * [fs_pathconf() -> VOP_STATVFS() -> ufs_statvfs()]
5642 		 * results in a lock ordering reversal between
5643 		 * ufs_lockfs_{begin,end}() and
5644 		 * ufs_thread_{suspend,continue}().
5645 		 *
5646 		 * Keep in sync with ufs_statvfs().
5647 		 */
5648 	case _PC_NAME_MAX:
5649 		*valp = MAXNAMLEN;
5650 		break;
5651 
5652 	case _PC_FILESIZEBITS:
5653 		if (ufsvfsp->vfs_lfflags & UFS_LARGEFILES)
5654 			*valp = UFS_FILESIZE_BITS;
5655 		else
5656 			*valp = 32;
5657 		break;
5658 
5659 	case _PC_XATTR_EXISTS:
5660 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5661 
5662 			error =
5663 			    ufs_xattr_getattrdir(vp, &sip, LOOKUP_XATTR, cr);
5664 			if (error ==  0 && sip != NULL) {
5665 				/* Start transaction */
5666 				if (ulp) {
5667 					TRANS_BEGIN_CSYNC(ufsvfsp, issync,
5668 					    TOP_RMDIR, TOP_RMDIR_SIZE);
5669 				}
5670 				/*
5671 				 * Is directory empty
5672 				 */
5673 				rw_enter(&sip->i_rwlock, RW_WRITER);
5674 				rw_enter(&sip->i_contents, RW_WRITER);
5675 				if (ufs_xattrdirempty(sip,
5676 				    sip->i_number, CRED())) {
5677 					rw_enter(&ip->i_contents, RW_WRITER);
5678 					ufs_unhook_shadow(ip, sip);
5679 					rw_exit(&ip->i_contents);
5680 
5681 					*valp = 0;
5682 
5683 				} else
5684 					*valp = 1;
5685 				rw_exit(&sip->i_contents);
5686 				rw_exit(&sip->i_rwlock);
5687 				if (ulp) {
5688 					TRANS_END_CSYNC(ufsvfsp, error, issync,
5689 					    TOP_RMDIR, TOP_RMDIR_SIZE);
5690 				}
5691 				VN_RELE(ITOV(sip));
5692 			} else if (error == ENOENT) {
5693 				*valp = 0;
5694 				error = 0;
5695 			}
5696 		} else {
5697 			error = fs_pathconf(vp, cmd, valp, cr);
5698 		}
5699 		break;
5700 
5701 	case _PC_ACL_ENABLED:
5702 		*valp = _ACL_ACLENT_ENABLED;
5703 		break;
5704 
5705 	case _PC_MIN_HOLE_SIZE:
5706 		*valp = (ulong_t)ip->i_fs->fs_bsize;
5707 		break;
5708 
5709 	default:
5710 		error = fs_pathconf(vp, cmd, valp, cr);
5711 	}
5712 
5713 	if (ulp != NULL) {
5714 		ufs_lockfs_end(ulp);
5715 	}
5716 	return (error);
5717 }
5718 
5719 int ufs_pageio_writes, ufs_pageio_reads;
5720 
5721 /*ARGSUSED*/
5722 static int
5723 ufs_pageio(struct vnode *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5724 	int flags, struct cred *cr)
5725 {
5726 	struct inode *ip = VTOI(vp);
5727 	struct ufsvfs *ufsvfsp;
5728 	page_t *npp = NULL, *opp = NULL, *cpp = pp;
5729 	struct buf *bp;
5730 	daddr_t bn;
5731 	size_t done_len = 0, cur_len = 0;
5732 	int err = 0;
5733 	int contig = 0;
5734 	int dolock;
5735 	int vmpss = 0;
5736 	struct ulockfs *ulp;
5737 
5738 	if ((flags & B_READ) && pp != NULL && pp->p_vnode == vp &&
5739 	    vp->v_mpssdata != NULL) {
5740 		vmpss = 1;
5741 	}
5742 
5743 	dolock = (rw_owner(&ip->i_contents) != curthread);
5744 	/*
5745 	 * We need a better check.  Ideally, we would use another
5746 	 * vnodeops so that hlocked and forcibly unmounted file
5747 	 * systems would return EIO where appropriate and w/o the
5748 	 * need for these checks.
5749 	 */
5750 	if ((ufsvfsp = ip->i_ufsvfs) == NULL)
5751 		return (EIO);
5752 
5753 	/*
5754 	 * For vmpss (pp can be NULL) case respect the quiesce protocol.
5755 	 * ul_lock must be taken before locking pages so we can't use it here
5756 	 * if pp is non NULL because segvn already locked pages
5757 	 * SE_EXCL. Instead we rely on the fact that a forced umount or
5758 	 * applying a filesystem lock via ufs_fiolfs() will block in the
5759 	 * implicit call to ufs_flush() until we unlock the pages after the
5760 	 * return to segvn. Other ufs_quiesce() callers keep ufs_quiesce_pend
5761 	 * above 0 until they are done. We have to be careful not to increment
5762 	 * ul_vnops_cnt here after forceful unmount hlocks the file system.
5763 	 *
5764 	 * If pp is NULL use ul_lock to make sure we don't increment
5765 	 * ul_vnops_cnt after forceful unmount hlocks the file system.
5766 	 */
5767 	if (vmpss || pp == NULL) {
5768 		ulp = &ufsvfsp->vfs_ulockfs;
5769 		if (pp == NULL)
5770 			mutex_enter(&ulp->ul_lock);
5771 		if (ulp->ul_fs_lock & ULOCKFS_GETREAD_MASK) {
5772 			if (pp == NULL) {
5773 				mutex_exit(&ulp->ul_lock);
5774 			}
5775 			return (vmpss ? EIO : EINVAL);
5776 		}
5777 		atomic_add_long(&ulp->ul_vnops_cnt, 1);
5778 		if (pp == NULL)
5779 			mutex_exit(&ulp->ul_lock);
5780 		if (ufs_quiesce_pend) {
5781 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5782 				cv_broadcast(&ulp->ul_cv);
5783 			return (vmpss ? EIO : EINVAL);
5784 		}
5785 	}
5786 
5787 	if (dolock) {
5788 		/*
5789 		 * segvn may call VOP_PAGEIO() instead of VOP_GETPAGE() to
5790 		 * handle a fault against a segment that maps vnode pages with
5791 		 * large mappings.  Segvn creates pages and holds them locked
5792 		 * SE_EXCL during VOP_PAGEIO() call. In this case we have to
5793 		 * use rw_tryenter() to avoid a potential deadlock since in
5794 		 * lock order i_contents needs to be taken first.
5795 		 * Segvn will retry via VOP_GETPAGE() if VOP_PAGEIO() fails.
5796 		 */
5797 		if (!vmpss) {
5798 			rw_enter(&ip->i_contents, RW_READER);
5799 		} else if (!rw_tryenter(&ip->i_contents, RW_READER)) {
5800 			if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5801 				cv_broadcast(&ulp->ul_cv);
5802 			return (EDEADLK);
5803 		}
5804 	}
5805 
5806 	/*
5807 	 * Return an error to segvn because the pagefault request is beyond
5808 	 * PAGESIZE rounded EOF.
5809 	 */
5810 	if (vmpss && btopr(io_off + io_len) > btopr(ip->i_size)) {
5811 		if (dolock)
5812 			rw_exit(&ip->i_contents);
5813 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5814 			cv_broadcast(&ulp->ul_cv);
5815 		return (EFAULT);
5816 	}
5817 
5818 	if (pp == NULL) {
5819 		if (bmap_has_holes(ip)) {
5820 			err = ENOSYS;
5821 		} else {
5822 			err = EINVAL;
5823 		}
5824 		if (dolock)
5825 			rw_exit(&ip->i_contents);
5826 		if (!atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5827 			cv_broadcast(&ulp->ul_cv);
5828 		return (err);
5829 	}
5830 
5831 	/*
5832 	 * Break the io request into chunks, one for each contiguous
5833 	 * stretch of disk blocks in the target file.
5834 	 */
5835 	while (done_len < io_len) {
5836 		ASSERT(cpp);
5837 		contig = 0;
5838 		if (err = bmap_read(ip, (u_offset_t)(io_off + done_len),
5839 		    &bn, &contig))
5840 			break;
5841 
5842 		if (bn == UFS_HOLE) {	/* No holey swapfiles */
5843 			if (vmpss) {
5844 				err = EFAULT;
5845 				break;
5846 			}
5847 			err = ufs_fault(ITOV(ip), "ufs_pageio: bn == UFS_HOLE");
5848 			break;
5849 		}
5850 
5851 		cur_len = MIN(io_len - done_len, contig);
5852 		/*
5853 		 * Zero out a page beyond EOF, when the last block of
5854 		 * a file is a UFS fragment so that ufs_pageio() can be used
5855 		 * instead of ufs_getpage() to handle faults against
5856 		 * segvn segments that use large pages.
5857 		 */
5858 		page_list_break(&cpp, &npp, btopr(cur_len));
5859 		if ((flags & B_READ) && (cur_len & PAGEOFFSET)) {
5860 			size_t xlen = cur_len & PAGEOFFSET;
5861 			pagezero(cpp->p_prev, xlen, PAGESIZE - xlen);
5862 		}
5863 
5864 		bp = pageio_setup(cpp, cur_len, ip->i_devvp, flags);
5865 		ASSERT(bp != NULL);
5866 
5867 		bp->b_edev = ip->i_dev;
5868 		bp->b_dev = cmpdev(ip->i_dev);
5869 		bp->b_blkno = bn;
5870 		bp->b_un.b_addr = (caddr_t)0;
5871 		bp->b_file = ip->i_vnode;
5872 
5873 		ufsvfsp->vfs_iotstamp = lbolt;
5874 		ub.ub_pageios.value.ul++;
5875 		if (ufsvfsp->vfs_snapshot)
5876 			fssnap_strategy(&(ufsvfsp->vfs_snapshot), bp);
5877 		else
5878 			(void) bdev_strategy(bp);
5879 
5880 		if (flags & B_READ)
5881 			ufs_pageio_reads++;
5882 		else
5883 			ufs_pageio_writes++;
5884 		if (flags & B_READ)
5885 			lwp_stat_update(LWP_STAT_INBLK, 1);
5886 		else
5887 			lwp_stat_update(LWP_STAT_OUBLK, 1);
5888 		/*
5889 		 * If the request is not B_ASYNC, wait for i/o to complete
5890 		 * and re-assemble the page list to return to the caller.
5891 		 * If it is B_ASYNC we leave the page list in pieces and
5892 		 * cleanup() will dispose of them.
5893 		 */
5894 		if ((flags & B_ASYNC) == 0) {
5895 			err = biowait(bp);
5896 			pageio_done(bp);
5897 			if (err)
5898 				break;
5899 			page_list_concat(&opp, &cpp);
5900 		}
5901 		cpp = npp;
5902 		npp = NULL;
5903 		if (flags & B_READ)
5904 			cur_len = P2ROUNDUP_TYPED(cur_len, PAGESIZE, size_t);
5905 		done_len += cur_len;
5906 	}
5907 	ASSERT(err || (cpp == NULL && npp == NULL && done_len == io_len));
5908 	if (err) {
5909 		if (flags & B_ASYNC) {
5910 			/* Cleanup unprocessed parts of list */
5911 			page_list_concat(&cpp, &npp);
5912 			if (flags & B_READ)
5913 				pvn_read_done(cpp, B_ERROR);
5914 			else
5915 				pvn_write_done(cpp, B_ERROR);
5916 		} else {
5917 			/* Re-assemble list and let caller clean up */
5918 			page_list_concat(&opp, &cpp);
5919 			page_list_concat(&opp, &npp);
5920 		}
5921 	}
5922 
5923 	if (vmpss && !(ip->i_flag & IACC) && !ULOCKFS_IS_NOIACC(ulp) &&
5924 	    ufsvfsp->vfs_fs->fs_ronly == 0 && !ufsvfsp->vfs_noatime) {
5925 		mutex_enter(&ip->i_tlock);
5926 		ip->i_flag |= IACC;
5927 		ITIMES_NOLOCK(ip);
5928 		mutex_exit(&ip->i_tlock);
5929 	}
5930 
5931 	if (dolock)
5932 		rw_exit(&ip->i_contents);
5933 	if (vmpss && !atomic_add_long_nv(&ulp->ul_vnops_cnt, -1))
5934 		cv_broadcast(&ulp->ul_cv);
5935 	return (err);
5936 }
5937 
5938 /*
5939  * Called when the kernel is in a frozen state to dump data
5940  * directly to the device. It uses a private dump data structure,
5941  * set up by dump_ctl, to locate the correct disk block to which to dump.
5942  */
5943 static int
5944 ufs_dump(vnode_t *vp, caddr_t addr, int ldbn, int dblks)
5945 {
5946 	u_offset_t	file_size;
5947 	struct inode    *ip = VTOI(vp);
5948 	struct fs	*fs = ip->i_fs;
5949 	daddr_t		dbn, lfsbn;
5950 	int		disk_blks = fs->fs_bsize >> DEV_BSHIFT;
5951 	int		error = 0;
5952 	int		ndbs, nfsbs;
5953 
5954 	/*
5955 	 * forced unmount case
5956 	 */
5957 	if (ip->i_ufsvfs == NULL)
5958 		return (EIO);
5959 	/*
5960 	 * Validate the inode that it has not been modified since
5961 	 * the dump structure is allocated.
5962 	 */
5963 	mutex_enter(&ip->i_tlock);
5964 	if ((dump_info == NULL) ||
5965 	    (dump_info->ip != ip) ||
5966 	    (dump_info->time.tv_sec != ip->i_mtime.tv_sec) ||
5967 	    (dump_info->time.tv_usec != ip->i_mtime.tv_usec)) {
5968 		mutex_exit(&ip->i_tlock);
5969 		return (-1);
5970 	}
5971 	mutex_exit(&ip->i_tlock);
5972 
5973 	/*
5974 	 * See that the file has room for this write
5975 	 */
5976 	UFS_GET_ISIZE(&file_size, ip);
5977 
5978 	if (ldbtob((offset_t)(ldbn + dblks)) > file_size)
5979 		return (ENOSPC);
5980 
5981 	/*
5982 	 * Find the physical disk block numbers from the dump
5983 	 * private data structure directly and write out the data
5984 	 * in contiguous block lumps
5985 	 */
5986 	while (dblks > 0 && !error) {
5987 		lfsbn = (daddr_t)lblkno(fs, ldbtob((offset_t)ldbn));
5988 		dbn = fsbtodb(fs, dump_info->dblk[lfsbn]) + ldbn % disk_blks;
5989 		nfsbs = 1;
5990 		ndbs = disk_blks - ldbn % disk_blks;
5991 		while (ndbs < dblks && fsbtodb(fs, dump_info->dblk[lfsbn +
5992 		    nfsbs]) == dbn + ndbs) {
5993 			nfsbs++;
5994 			ndbs += disk_blks;
5995 		}
5996 		if (ndbs > dblks)
5997 			ndbs = dblks;
5998 		error = bdev_dump(ip->i_dev, addr, dbn, ndbs);
5999 		addr += ldbtob((offset_t)ndbs);
6000 		dblks -= ndbs;
6001 		ldbn += ndbs;
6002 	}
6003 	return (error);
6004 
6005 }
6006 
6007 /*
6008  * Prepare the file system before and after the dump operation.
6009  *
6010  * action = DUMP_ALLOC:
6011  * Preparation before dump, allocate dump private data structure
6012  * to hold all the direct and indirect block info for dump.
6013  *
6014  * action = DUMP_FREE:
6015  * Clean up after dump, deallocate the dump private data structure.
6016  *
6017  * action = DUMP_SCAN:
6018  * Scan dump_info for *blkp DEV_BSIZE blocks of contig fs space;
6019  * if found, the starting file-relative DEV_BSIZE lbn is written
6020  * to *bklp; that lbn is intended for use with VOP_DUMP()
6021  */
6022 static int
6023 ufs_dumpctl(vnode_t *vp, int action, int *blkp)
6024 {
6025 	struct inode	*ip = VTOI(vp);
6026 	ufsvfs_t	*ufsvfsp = ip->i_ufsvfs;
6027 	struct fs	*fs;
6028 	daddr32_t	*dblk, *storeblk;
6029 	daddr32_t	*nextblk, *endblk;
6030 	struct buf	*bp;
6031 	int		i, entry, entries;
6032 	int		n, ncontig;
6033 
6034 	/*
6035 	 * check for forced unmount
6036 	 */
6037 	if (ufsvfsp == NULL)
6038 		return (EIO);
6039 
6040 	if (action == DUMP_ALLOC) {
6041 		/*
6042 		 * alloc and record dump_info
6043 		 */
6044 		if (dump_info != NULL)
6045 			return (EINVAL);
6046 
6047 		ASSERT(vp->v_type == VREG);
6048 		fs = ufsvfsp->vfs_fs;
6049 
6050 		rw_enter(&ip->i_contents, RW_READER);
6051 
6052 		if (bmap_has_holes(ip)) {
6053 			rw_exit(&ip->i_contents);
6054 			return (EFAULT);
6055 		}
6056 
6057 		/*
6058 		 * calculate and allocate space needed according to i_size
6059 		 */
6060 		entries = (int)lblkno(fs, blkroundup(fs, ip->i_size));
6061 		dump_info = kmem_alloc(sizeof (struct dump) +
6062 		    (entries - 1) * sizeof (daddr32_t), KM_NOSLEEP);
6063 		if (dump_info == NULL) {
6064 			rw_exit(&ip->i_contents);
6065 			return (ENOMEM);
6066 		}
6067 
6068 		/* Start saving the info */
6069 		dump_info->fsbs = entries;
6070 		dump_info->ip = ip;
6071 		storeblk = &dump_info->dblk[0];
6072 
6073 		/* Direct Blocks */
6074 		for (entry = 0; entry < NDADDR && entry < entries; entry++)
6075 			*storeblk++ = ip->i_db[entry];
6076 
6077 		/* Indirect Blocks */
6078 		for (i = 0; i < NIADDR; i++) {
6079 			int error = 0;
6080 
6081 			bp = UFS_BREAD(ufsvfsp,
6082 			    ip->i_dev, fsbtodb(fs, ip->i_ib[i]), fs->fs_bsize);
6083 			if (bp->b_flags & B_ERROR)
6084 				error = EIO;
6085 			else {
6086 				dblk = bp->b_un.b_daddr;
6087 				if ((storeblk = save_dblks(ip, ufsvfsp,
6088 				    storeblk, dblk, i, entries)) == NULL)
6089 					error = EIO;
6090 			}
6091 
6092 			brelse(bp);
6093 
6094 			if (error != 0) {
6095 				kmem_free(dump_info, sizeof (struct dump) +
6096 				    (entries - 1) * sizeof (daddr32_t));
6097 				rw_exit(&ip->i_contents);
6098 				dump_info = NULL;
6099 				return (error);
6100 			}
6101 		}
6102 		/* and time stamp the information */
6103 		mutex_enter(&ip->i_tlock);
6104 		dump_info->time = ip->i_mtime;
6105 		mutex_exit(&ip->i_tlock);
6106 
6107 		rw_exit(&ip->i_contents);
6108 	} else if (action == DUMP_FREE) {
6109 		/*
6110 		 * free dump_info
6111 		 */
6112 		if (dump_info == NULL)
6113 			return (EINVAL);
6114 		entries = dump_info->fsbs - 1;
6115 		kmem_free(dump_info, sizeof (struct dump) +
6116 		    entries * sizeof (daddr32_t));
6117 		dump_info = NULL;
6118 	} else if (action == DUMP_SCAN) {
6119 		/*
6120 		 * scan dump_info
6121 		 */
6122 		if (dump_info == NULL)
6123 			return (EINVAL);
6124 
6125 		dblk = dump_info->dblk;
6126 		nextblk = dblk + 1;
6127 		endblk = dblk + dump_info->fsbs - 1;
6128 		fs = ufsvfsp->vfs_fs;
6129 		ncontig = *blkp >> (fs->fs_bshift - DEV_BSHIFT);
6130 
6131 		/*
6132 		 * scan dblk[] entries; contig fs space is found when:
6133 		 * ((current blkno + frags per block) == next blkno)
6134 		 */
6135 		n = 0;
6136 		while (n < ncontig && dblk < endblk) {
6137 			if ((*dblk + fs->fs_frag) == *nextblk)
6138 				n++;
6139 			else
6140 				n = 0;
6141 			dblk++;
6142 			nextblk++;
6143 		}
6144 
6145 		/*
6146 		 * index is where size bytes of contig space begins;
6147 		 * conversion from index to the file's DEV_BSIZE lbn
6148 		 * is equivalent to:  (index * fs_bsize) / DEV_BSIZE
6149 		 */
6150 		if (n == ncontig) {
6151 			i = (dblk - dump_info->dblk) - ncontig;
6152 			*blkp = i << (fs->fs_bshift - DEV_BSHIFT);
6153 		} else
6154 			return (EFAULT);
6155 	}
6156 	return (0);
6157 }
6158 
6159 /*
6160  * Recursive helper function for ufs_dumpctl().  It follows the indirect file
6161  * system  blocks until it reaches the the disk block addresses, which are
6162  * then stored into the given buffer, storeblk.
6163  */
6164 static daddr32_t *
6165 save_dblks(struct inode *ip, struct ufsvfs *ufsvfsp,  daddr32_t *storeblk,
6166     daddr32_t *dblk, int level, int entries)
6167 {
6168 	struct fs	*fs = ufsvfsp->vfs_fs;
6169 	struct buf	*bp;
6170 	int		i;
6171 
6172 	if (level == 0) {
6173 		for (i = 0; i < NINDIR(fs); i++) {
6174 			if (storeblk - dump_info->dblk >= entries)
6175 				break;
6176 			*storeblk++ = dblk[i];
6177 		}
6178 		return (storeblk);
6179 	}
6180 	for (i = 0; i < NINDIR(fs); i++) {
6181 		if (storeblk - dump_info->dblk >= entries)
6182 			break;
6183 		bp = UFS_BREAD(ufsvfsp,
6184 		    ip->i_dev, fsbtodb(fs, dblk[i]), fs->fs_bsize);
6185 		if (bp->b_flags & B_ERROR) {
6186 			brelse(bp);
6187 			return (NULL);
6188 		}
6189 		storeblk = save_dblks(ip, ufsvfsp, storeblk, bp->b_un.b_daddr,
6190 		    level - 1, entries);
6191 		brelse(bp);
6192 
6193 		if (storeblk == NULL)
6194 			return (NULL);
6195 	}
6196 	return (storeblk);
6197 }
6198 
6199 /* ARGSUSED */
6200 static int
6201 ufs_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag,
6202 	struct cred *cr)
6203 {
6204 	struct inode	*ip = VTOI(vp);
6205 	struct ulockfs	*ulp;
6206 	struct ufsvfs	*ufsvfsp = ip->i_ufsvfs;
6207 	ulong_t		vsa_mask = vsap->vsa_mask;
6208 	int		err = EINVAL;
6209 
6210 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
6211 
6212 	/*
6213 	 * Only grab locks if needed - they're not needed to check vsa_mask
6214 	 * or if the mask contains no acl flags.
6215 	 */
6216 	if (vsa_mask != 0) {
6217 		if (err = ufs_lockfs_begin(ufsvfsp, &ulp,
6218 		    ULOCKFS_GETATTR_MASK))
6219 			return (err);
6220 
6221 		rw_enter(&ip->i_contents, RW_READER);
6222 		err = ufs_acl_get(ip, vsap, flag, cr);
6223 		rw_exit(&ip->i_contents);
6224 
6225 		if (ulp)
6226 			ufs_lockfs_end(ulp);
6227 	}
6228 	return (err);
6229 }
6230 
6231 /* ARGSUSED */
6232 static int
6233 ufs_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
6234 {
6235 	struct inode	*ip = VTOI(vp);
6236 	struct ulockfs	*ulp = NULL;
6237 	struct ufsvfs	*ufsvfsp = VTOI(vp)->i_ufsvfs;
6238 	ulong_t		vsa_mask = vsap->vsa_mask;
6239 	int		err;
6240 	int		haverwlock = 1;
6241 	int		trans_size;
6242 	int		donetrans = 0;
6243 	int		retry = 1;
6244 
6245 	ASSERT(RW_LOCK_HELD(&ip->i_rwlock));
6246 
6247 	/* Abort now if the request is either empty or invalid. */
6248 	vsa_mask &= (VSA_ACL | VSA_ACLCNT | VSA_DFACL | VSA_DFACLCNT);
6249 	if ((vsa_mask == 0) ||
6250 	    ((vsap->vsa_aclentp == NULL) &&
6251 	    (vsap->vsa_dfaclentp == NULL))) {
6252 		err = EINVAL;
6253 		goto out;
6254 	}
6255 
6256 	/*
6257 	 * Following convention, if this is a directory then we acquire the
6258 	 * inode's i_rwlock after starting a UFS logging transaction;
6259 	 * otherwise, we acquire it beforehand. Since we were called (and
6260 	 * must therefore return) with the lock held, we will have to drop it,
6261 	 * and later reacquire it, if operating on a directory.
6262 	 */
6263 	if (vp->v_type == VDIR) {
6264 		rw_exit(&ip->i_rwlock);
6265 		haverwlock = 0;
6266 	} else {
6267 		/* Upgrade the lock if required. */
6268 		if (!rw_write_held(&ip->i_rwlock)) {
6269 			rw_exit(&ip->i_rwlock);
6270 			rw_enter(&ip->i_rwlock, RW_WRITER);
6271 		}
6272 	}
6273 
6274 again:
6275 	ASSERT(!(vp->v_type == VDIR && haverwlock));
6276 	if (err = ufs_lockfs_begin(ufsvfsp, &ulp, ULOCKFS_SETATTR_MASK)) {
6277 		ulp = NULL;
6278 		retry = 0;
6279 		goto out;
6280 	}
6281 
6282 	/*
6283 	 * Check that the file system supports this operation. Note that
6284 	 * ufs_lockfs_begin() will have checked that the file system had
6285 	 * not been forcibly unmounted.
6286 	 */
6287 	if (ufsvfsp->vfs_fs->fs_ronly) {
6288 		err = EROFS;
6289 		goto out;
6290 	}
6291 	if (ufsvfsp->vfs_nosetsec) {
6292 		err = ENOSYS;
6293 		goto out;
6294 	}
6295 
6296 	if (ulp) {
6297 		TRANS_BEGIN_ASYNC(ufsvfsp, TOP_SETSECATTR,
6298 		    trans_size = TOP_SETSECATTR_SIZE(VTOI(vp)));
6299 		donetrans = 1;
6300 	}
6301 
6302 	if (vp->v_type == VDIR) {
6303 		rw_enter(&ip->i_rwlock, RW_WRITER);
6304 		haverwlock = 1;
6305 	}
6306 
6307 	ASSERT(haverwlock);
6308 
6309 	/* Do the actual work. */
6310 	rw_enter(&ip->i_contents, RW_WRITER);
6311 	/*
6312 	 * Suppress out of inodes messages if we will retry.
6313 	 */
6314 	if (retry)
6315 		ip->i_flag |= IQUIET;
6316 	err = ufs_acl_set(ip, vsap, flag, cr);
6317 	ip->i_flag &= ~IQUIET;
6318 	rw_exit(&ip->i_contents);
6319 
6320 out:
6321 	if (ulp) {
6322 		if (donetrans) {
6323 			/*
6324 			 * top_end_async() can eventually call
6325 			 * top_end_sync(), which can block. We must
6326 			 * therefore observe the lock-ordering protocol
6327 			 * here as well.
6328 			 */
6329 			if (vp->v_type == VDIR) {
6330 				rw_exit(&ip->i_rwlock);
6331 				haverwlock = 0;
6332 			}
6333 			TRANS_END_ASYNC(ufsvfsp, TOP_SETSECATTR, trans_size);
6334 		}
6335 		ufs_lockfs_end(ulp);
6336 	}
6337 	/*
6338 	 * If no inodes available, try scaring a logically-
6339 	 * free one out of the delete queue to someplace
6340 	 * that we can find it.
6341 	 */
6342 	if ((err == ENOSPC) && retry && TRANS_ISTRANS(ufsvfsp)) {
6343 		ufs_delete_drain_wait(ufsvfsp, 1);
6344 		retry = 0;
6345 		if (vp->v_type == VDIR && haverwlock) {
6346 			rw_exit(&ip->i_rwlock);
6347 			haverwlock = 0;
6348 		}
6349 		goto again;
6350 	}
6351 	/*
6352 	 * If we need to reacquire the lock then it is safe to do so
6353 	 * as a reader. This is because ufs_rwunlock(), which will be
6354 	 * called by our caller after we return, does not differentiate
6355 	 * between shared and exclusive locks.
6356 	 */
6357 	if (!haverwlock) {
6358 		ASSERT(vp->v_type == VDIR);
6359 		rw_enter(&ip->i_rwlock, RW_READER);
6360 	}
6361 
6362 	return (err);
6363 }
6364