xref: /titanic_44/usr/src/uts/common/fs/zfs/zfs_vnops.c (revision b38f097029665f4ece801ca129913d36f757b49c)
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 /* Portions Copyright 2007 Jeremy Teo */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/resource.h>
36 #include <sys/vfs.h>
37 #include <sys/vfs_opreg.h>
38 #include <sys/vnode.h>
39 #include <sys/file.h>
40 #include <sys/stat.h>
41 #include <sys/kmem.h>
42 #include <sys/taskq.h>
43 #include <sys/uio.h>
44 #include <sys/vmsystm.h>
45 #include <sys/atomic.h>
46 #include <sys/vm.h>
47 #include <vm/seg_vn.h>
48 #include <vm/pvn.h>
49 #include <vm/as.h>
50 #include <sys/mman.h>
51 #include <sys/pathname.h>
52 #include <sys/cmn_err.h>
53 #include <sys/errno.h>
54 #include <sys/unistd.h>
55 #include <sys/zfs_dir.h>
56 #include <sys/zfs_acl.h>
57 #include <sys/zfs_ioctl.h>
58 #include <sys/fs/zfs.h>
59 #include <sys/dmu.h>
60 #include <sys/spa.h>
61 #include <sys/txg.h>
62 #include <sys/dbuf.h>
63 #include <sys/zap.h>
64 #include <sys/dirent.h>
65 #include <sys/policy.h>
66 #include <sys/sunddi.h>
67 #include <sys/filio.h>
68 #include "fs/fs_subr.h"
69 #include <sys/zfs_ctldir.h>
70 #include <sys/zfs_fuid.h>
71 #include <sys/dnlc.h>
72 #include <sys/zfs_rlock.h>
73 #include <sys/extdirent.h>
74 #include <sys/kidmap.h>
75 #include <sys/cred_impl.h>
76 #include <sys/attr.h>
77 
78 /*
79  * Programming rules.
80  *
81  * Each vnode op performs some logical unit of work.  To do this, the ZPL must
82  * properly lock its in-core state, create a DMU transaction, do the work,
83  * record this work in the intent log (ZIL), commit the DMU transaction,
84  * and wait for the intent log to commit if it is a synchronous operation.
85  * Moreover, the vnode ops must work in both normal and log replay context.
86  * The ordering of events is important to avoid deadlocks and references
87  * to freed memory.  The example below illustrates the following Big Rules:
88  *
89  *  (1) A check must be made in each zfs thread for a mounted file system.
90  *	This is done avoiding races using ZFS_ENTER(zfsvfs).
91  *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
92  *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
93  *      can return EIO from the calling function.
94  *
95  *  (2)	VN_RELE() should always be the last thing except for zil_commit()
96  *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
97  *	First, if it's the last reference, the vnode/znode
98  *	can be freed, so the zp may point to freed memory.  Second, the last
99  *	reference will call zfs_zinactive(), which may induce a lot of work --
100  *	pushing cached pages (which acquires range locks) and syncing out
101  *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
102  *	which could deadlock the system if you were already holding one.
103  *
104  *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
105  *	as they can span dmu_tx_assign() calls.
106  *
107  *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
108  *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
109  *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
110  *	This is critical because we don't want to block while holding locks.
111  *	Note, in particular, that if a lock is sometimes acquired before
112  *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
113  *	use a non-blocking assign can deadlock the system.  The scenario:
114  *
115  *	Thread A has grabbed a lock before calling dmu_tx_assign().
116  *	Thread B is in an already-assigned tx, and blocks for this lock.
117  *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
118  *	forever, because the previous txg can't quiesce until B's tx commits.
119  *
120  *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
121  *	then drop all locks, call dmu_tx_wait(), and try again.
122  *
123  *  (5)	If the operation succeeded, generate the intent log entry for it
124  *	before dropping locks.  This ensures that the ordering of events
125  *	in the intent log matches the order in which they actually occurred.
126  *
127  *  (6)	At the end of each vnode op, the DMU tx must always commit,
128  *	regardless of whether there were any errors.
129  *
130  *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
131  *	to ensure that synchronous semantics are provided when necessary.
132  *
133  * In general, this is how things should be ordered in each vnode op:
134  *
135  *	ZFS_ENTER(zfsvfs);		// exit if unmounted
136  * top:
137  *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
138  *	rw_enter(...);			// grab any other locks you need
139  *	tx = dmu_tx_create(...);	// get DMU tx
140  *	dmu_tx_hold_*();		// hold each object you might modify
141  *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
142  *	if (error) {
143  *		rw_exit(...);		// drop locks
144  *		zfs_dirent_unlock(dl);	// unlock directory entry
145  *		VN_RELE(...);		// release held vnodes
146  *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
147  *			dmu_tx_wait(tx);
148  *			dmu_tx_abort(tx);
149  *			goto top;
150  *		}
151  *		dmu_tx_abort(tx);	// abort DMU tx
152  *		ZFS_EXIT(zfsvfs);	// finished in zfs
153  *		return (error);		// really out of space
154  *	}
155  *	error = do_real_work();		// do whatever this VOP does
156  *	if (error == 0)
157  *		zfs_log_*(...);		// on success, make ZIL entry
158  *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
159  *	rw_exit(...);			// drop locks
160  *	zfs_dirent_unlock(dl);		// unlock directory entry
161  *	VN_RELE(...);			// release held vnodes
162  *	zil_commit(zilog, seq, foid);	// synchronous when necessary
163  *	ZFS_EXIT(zfsvfs);		// finished in zfs
164  *	return (error);			// done, report error
165  */
166 
167 /* ARGSUSED */
168 static int
169 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
170 {
171 	znode_t	*zp = VTOZ(*vpp);
172 
173 	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
174 	    ((flag & FAPPEND) == 0)) {
175 		return (EPERM);
176 	}
177 
178 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
179 	    ZTOV(zp)->v_type == VREG &&
180 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
181 	    zp->z_phys->zp_size > 0)
182 		if (fs_vscan(*vpp, cr, 0) != 0)
183 			return (EACCES);
184 
185 	/* Keep a count of the synchronous opens in the znode */
186 	if (flag & (FSYNC | FDSYNC))
187 		atomic_inc_32(&zp->z_sync_cnt);
188 
189 	return (0);
190 }
191 
192 /* ARGSUSED */
193 static int
194 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
195     caller_context_t *ct)
196 {
197 	znode_t	*zp = VTOZ(vp);
198 
199 	/* Decrement the synchronous opens in the znode */
200 	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
201 		atomic_dec_32(&zp->z_sync_cnt);
202 
203 	/*
204 	 * Clean up any locks held by this process on the vp.
205 	 */
206 	cleanlocks(vp, ddi_get_pid(), 0);
207 	cleanshares(vp, ddi_get_pid());
208 
209 	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
210 	    ZTOV(zp)->v_type == VREG &&
211 	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
212 	    zp->z_phys->zp_size > 0)
213 		VERIFY(fs_vscan(vp, cr, 1) == 0);
214 
215 	return (0);
216 }
217 
218 /*
219  * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
220  * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
221  */
222 static int
223 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
224 {
225 	znode_t	*zp = VTOZ(vp);
226 	uint64_t noff = (uint64_t)*off; /* new offset */
227 	uint64_t file_sz;
228 	int error;
229 	boolean_t hole;
230 
231 	file_sz = zp->z_phys->zp_size;
232 	if (noff >= file_sz)  {
233 		return (ENXIO);
234 	}
235 
236 	if (cmd == _FIO_SEEK_HOLE)
237 		hole = B_TRUE;
238 	else
239 		hole = B_FALSE;
240 
241 	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
242 
243 	/* end of file? */
244 	if ((error == ESRCH) || (noff > file_sz)) {
245 		/*
246 		 * Handle the virtual hole at the end of file.
247 		 */
248 		if (hole) {
249 			*off = file_sz;
250 			return (0);
251 		}
252 		return (ENXIO);
253 	}
254 
255 	if (noff < *off)
256 		return (error);
257 	*off = noff;
258 	return (error);
259 }
260 
261 /* ARGSUSED */
262 static int
263 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
264     int *rvalp, caller_context_t *ct)
265 {
266 	offset_t off;
267 	int error;
268 	zfsvfs_t *zfsvfs;
269 	znode_t *zp;
270 
271 	switch (com) {
272 	case _FIOFFS:
273 		return (zfs_sync(vp->v_vfsp, 0, cred));
274 
275 		/*
276 		 * The following two ioctls are used by bfu.  Faking out,
277 		 * necessary to avoid bfu errors.
278 		 */
279 	case _FIOGDIO:
280 	case _FIOSDIO:
281 		return (0);
282 
283 	case _FIO_SEEK_DATA:
284 	case _FIO_SEEK_HOLE:
285 		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
286 			return (EFAULT);
287 
288 		zp = VTOZ(vp);
289 		zfsvfs = zp->z_zfsvfs;
290 		ZFS_ENTER(zfsvfs);
291 		ZFS_VERIFY_ZP(zp);
292 
293 		/* offset parameter is in/out */
294 		error = zfs_holey(vp, com, &off);
295 		ZFS_EXIT(zfsvfs);
296 		if (error)
297 			return (error);
298 		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
299 			return (EFAULT);
300 		return (0);
301 	}
302 	return (ENOTTY);
303 }
304 
305 /*
306  * When a file is memory mapped, we must keep the IO data synchronized
307  * between the DMU cache and the memory mapped pages.  What this means:
308  *
309  * On Write:	If we find a memory mapped page, we write to *both*
310  *		the page and the dmu buffer.
311  *
312  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
313  *	the file is memory mapped.
314  */
315 static int
316 mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
317 {
318 	znode_t	*zp = VTOZ(vp);
319 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
320 	int64_t	start, off;
321 	int len = nbytes;
322 	int error = 0;
323 
324 	start = uio->uio_loffset;
325 	off = start & PAGEOFFSET;
326 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
327 		page_t *pp;
328 		uint64_t bytes = MIN(PAGESIZE - off, len);
329 		uint64_t woff = uio->uio_loffset;
330 
331 		/*
332 		 * We don't want a new page to "appear" in the middle of
333 		 * the file update (because it may not get the write
334 		 * update data), so we grab a lock to block
335 		 * zfs_getpage().
336 		 */
337 		rw_enter(&zp->z_map_lock, RW_WRITER);
338 		if (pp = page_lookup(vp, start, SE_SHARED)) {
339 			caddr_t va;
340 
341 			rw_exit(&zp->z_map_lock);
342 			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
343 			error = uiomove(va+off, bytes, UIO_WRITE, uio);
344 			if (error == 0) {
345 				dmu_write(zfsvfs->z_os, zp->z_id,
346 				    woff, bytes, va+off, tx);
347 			}
348 			ppmapout(va);
349 			page_unlock(pp);
350 		} else {
351 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
352 			    uio, bytes, tx);
353 			rw_exit(&zp->z_map_lock);
354 		}
355 		len -= bytes;
356 		off = 0;
357 		if (error)
358 			break;
359 	}
360 	return (error);
361 }
362 
363 /*
364  * When a file is memory mapped, we must keep the IO data synchronized
365  * between the DMU cache and the memory mapped pages.  What this means:
366  *
367  * On Read:	We "read" preferentially from memory mapped pages,
368  *		else we default from the dmu buffer.
369  *
370  * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
371  *	the file is memory mapped.
372  */
373 static int
374 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
375 {
376 	znode_t *zp = VTOZ(vp);
377 	objset_t *os = zp->z_zfsvfs->z_os;
378 	int64_t	start, off;
379 	int len = nbytes;
380 	int error = 0;
381 
382 	start = uio->uio_loffset;
383 	off = start & PAGEOFFSET;
384 	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
385 		page_t *pp;
386 		uint64_t bytes = MIN(PAGESIZE - off, len);
387 
388 		if (pp = page_lookup(vp, start, SE_SHARED)) {
389 			caddr_t va;
390 
391 			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
392 			error = uiomove(va + off, bytes, UIO_READ, uio);
393 			ppmapout(va);
394 			page_unlock(pp);
395 		} else {
396 			error = dmu_read_uio(os, zp->z_id, uio, bytes);
397 		}
398 		len -= bytes;
399 		off = 0;
400 		if (error)
401 			break;
402 	}
403 	return (error);
404 }
405 
406 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
407 
408 /*
409  * Read bytes from specified file into supplied buffer.
410  *
411  *	IN:	vp	- vnode of file to be read from.
412  *		uio	- structure supplying read location, range info,
413  *			  and return buffer.
414  *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
415  *		cr	- credentials of caller.
416  *		ct	- caller context
417  *
418  *	OUT:	uio	- updated offset and range, buffer filled.
419  *
420  *	RETURN:	0 if success
421  *		error code if failure
422  *
423  * Side Effects:
424  *	vp - atime updated if byte count > 0
425  */
426 /* ARGSUSED */
427 static int
428 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
429 {
430 	znode_t		*zp = VTOZ(vp);
431 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
432 	objset_t	*os;
433 	ssize_t		n, nbytes;
434 	int		error;
435 	rl_t		*rl;
436 
437 	ZFS_ENTER(zfsvfs);
438 	ZFS_VERIFY_ZP(zp);
439 	os = zfsvfs->z_os;
440 
441 	/*
442 	 * Validate file offset
443 	 */
444 	if (uio->uio_loffset < (offset_t)0) {
445 		ZFS_EXIT(zfsvfs);
446 		return (EINVAL);
447 	}
448 
449 	/*
450 	 * Fasttrack empty reads
451 	 */
452 	if (uio->uio_resid == 0) {
453 		ZFS_EXIT(zfsvfs);
454 		return (0);
455 	}
456 
457 	/*
458 	 * Check for mandatory locks
459 	 */
460 	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
461 		if (error = chklock(vp, FREAD,
462 		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
463 			ZFS_EXIT(zfsvfs);
464 			return (error);
465 		}
466 	}
467 
468 	/*
469 	 * If we're in FRSYNC mode, sync out this znode before reading it.
470 	 */
471 	if (ioflag & FRSYNC)
472 		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
473 
474 	/*
475 	 * Lock the range against changes.
476 	 */
477 	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
478 
479 	/*
480 	 * If we are reading past end-of-file we can skip
481 	 * to the end; but we might still need to set atime.
482 	 */
483 	if (uio->uio_loffset >= zp->z_phys->zp_size) {
484 		error = 0;
485 		goto out;
486 	}
487 
488 	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
489 	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
490 
491 	while (n > 0) {
492 		nbytes = MIN(n, zfs_read_chunk_size -
493 		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
494 
495 		if (vn_has_cached_data(vp))
496 			error = mappedread(vp, nbytes, uio);
497 		else
498 			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
499 		if (error)
500 			break;
501 
502 		n -= nbytes;
503 	}
504 
505 out:
506 	zfs_range_unlock(rl);
507 
508 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
509 	ZFS_EXIT(zfsvfs);
510 	return (error);
511 }
512 
513 /*
514  * Fault in the pages of the first n bytes specified by the uio structure.
515  * 1 byte in each page is touched and the uio struct is unmodified.
516  * Any error will exit this routine as this is only a best
517  * attempt to get the pages resident. This is a copy of ufs_trans_touch().
518  */
519 static void
520 zfs_prefault_write(ssize_t n, struct uio *uio)
521 {
522 	struct iovec *iov;
523 	ulong_t cnt, incr;
524 	caddr_t p;
525 	uint8_t tmp;
526 
527 	iov = uio->uio_iov;
528 
529 	while (n) {
530 		cnt = MIN(iov->iov_len, n);
531 		if (cnt == 0) {
532 			/* empty iov entry */
533 			iov++;
534 			continue;
535 		}
536 		n -= cnt;
537 		/*
538 		 * touch each page in this segment.
539 		 */
540 		p = iov->iov_base;
541 		while (cnt) {
542 			switch (uio->uio_segflg) {
543 			case UIO_USERSPACE:
544 			case UIO_USERISPACE:
545 				if (fuword8(p, &tmp))
546 					return;
547 				break;
548 			case UIO_SYSSPACE:
549 				if (kcopy(p, &tmp, 1))
550 					return;
551 				break;
552 			}
553 			incr = MIN(cnt, PAGESIZE);
554 			p += incr;
555 			cnt -= incr;
556 		}
557 		/*
558 		 * touch the last byte in case it straddles a page.
559 		 */
560 		p--;
561 		switch (uio->uio_segflg) {
562 		case UIO_USERSPACE:
563 		case UIO_USERISPACE:
564 			if (fuword8(p, &tmp))
565 				return;
566 			break;
567 		case UIO_SYSSPACE:
568 			if (kcopy(p, &tmp, 1))
569 				return;
570 			break;
571 		}
572 		iov++;
573 	}
574 }
575 
576 /*
577  * Write the bytes to a file.
578  *
579  *	IN:	vp	- vnode of file to be written to.
580  *		uio	- structure supplying write location, range info,
581  *			  and data buffer.
582  *		ioflag	- FAPPEND flag set if in append mode.
583  *		cr	- credentials of caller.
584  *		ct	- caller context (NFS/CIFS fem monitor only)
585  *
586  *	OUT:	uio	- updated offset and range.
587  *
588  *	RETURN:	0 if success
589  *		error code if failure
590  *
591  * Timestamps:
592  *	vp - ctime|mtime updated if byte count > 0
593  */
594 /* ARGSUSED */
595 static int
596 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
597 {
598 	znode_t		*zp = VTOZ(vp);
599 	rlim64_t	limit = uio->uio_llimit;
600 	ssize_t		start_resid = uio->uio_resid;
601 	ssize_t		tx_bytes;
602 	uint64_t	end_size;
603 	dmu_tx_t	*tx;
604 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
605 	zilog_t		*zilog;
606 	offset_t	woff;
607 	ssize_t		n, nbytes;
608 	rl_t		*rl;
609 	int		max_blksz = zfsvfs->z_max_blksz;
610 	uint64_t	pflags = zp->z_phys->zp_flags;
611 	int		error;
612 
613 	/*
614 	 * If immutable or not appending then return EPERM
615 	 */
616 	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
617 	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
618 	    (uio->uio_loffset < zp->z_phys->zp_size)))
619 		return (EPERM);
620 
621 	/*
622 	 * Fasttrack empty write
623 	 */
624 	n = start_resid;
625 	if (n == 0)
626 		return (0);
627 
628 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
629 		limit = MAXOFFSET_T;
630 
631 	ZFS_ENTER(zfsvfs);
632 	ZFS_VERIFY_ZP(zp);
633 	zilog = zfsvfs->z_log;
634 
635 	/*
636 	 * Pre-fault the pages to ensure slow (eg NFS) pages
637 	 * don't hold up txg.
638 	 */
639 	zfs_prefault_write(n, uio);
640 
641 	/*
642 	 * If in append mode, set the io offset pointer to eof.
643 	 */
644 	if (ioflag & FAPPEND) {
645 		/*
646 		 * Range lock for a file append:
647 		 * The value for the start of range will be determined by
648 		 * zfs_range_lock() (to guarantee append semantics).
649 		 * If this write will cause the block size to increase,
650 		 * zfs_range_lock() will lock the entire file, so we must
651 		 * later reduce the range after we grow the block size.
652 		 */
653 		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
654 		if (rl->r_len == UINT64_MAX) {
655 			/* overlocked, zp_size can't change */
656 			woff = uio->uio_loffset = zp->z_phys->zp_size;
657 		} else {
658 			woff = uio->uio_loffset = rl->r_off;
659 		}
660 	} else {
661 		woff = uio->uio_loffset;
662 		/*
663 		 * Validate file offset
664 		 */
665 		if (woff < 0) {
666 			ZFS_EXIT(zfsvfs);
667 			return (EINVAL);
668 		}
669 
670 		/*
671 		 * If we need to grow the block size then zfs_range_lock()
672 		 * will lock a wider range than we request here.
673 		 * Later after growing the block size we reduce the range.
674 		 */
675 		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
676 	}
677 
678 	if (woff >= limit) {
679 		zfs_range_unlock(rl);
680 		ZFS_EXIT(zfsvfs);
681 		return (EFBIG);
682 	}
683 
684 	if ((woff + n) > limit || woff > (limit - n))
685 		n = limit - woff;
686 
687 	/*
688 	 * Check for mandatory locks
689 	 */
690 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
691 	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
692 		zfs_range_unlock(rl);
693 		ZFS_EXIT(zfsvfs);
694 		return (error);
695 	}
696 	end_size = MAX(zp->z_phys->zp_size, woff + n);
697 
698 	/*
699 	 * Write the file in reasonable size chunks.  Each chunk is written
700 	 * in a separate transaction; this keeps the intent log records small
701 	 * and allows us to do more fine-grained space accounting.
702 	 */
703 	while (n > 0) {
704 		/*
705 		 * Start a transaction.
706 		 */
707 		woff = uio->uio_loffset;
708 		tx = dmu_tx_create(zfsvfs->z_os);
709 		dmu_tx_hold_bonus(tx, zp->z_id);
710 		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
711 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
712 		if (error) {
713 			if (error == ERESTART &&
714 			    zfsvfs->z_assign == TXG_NOWAIT) {
715 				dmu_tx_wait(tx);
716 				dmu_tx_abort(tx);
717 				continue;
718 			}
719 			dmu_tx_abort(tx);
720 			break;
721 		}
722 
723 		/*
724 		 * If zfs_range_lock() over-locked we grow the blocksize
725 		 * and then reduce the lock range.  This will only happen
726 		 * on the first iteration since zfs_range_reduce() will
727 		 * shrink down r_len to the appropriate size.
728 		 */
729 		if (rl->r_len == UINT64_MAX) {
730 			uint64_t new_blksz;
731 
732 			if (zp->z_blksz > max_blksz) {
733 				ASSERT(!ISP2(zp->z_blksz));
734 				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
735 			} else {
736 				new_blksz = MIN(end_size, max_blksz);
737 			}
738 			zfs_grow_blocksize(zp, new_blksz, tx);
739 			zfs_range_reduce(rl, woff, n);
740 		}
741 
742 		/*
743 		 * XXX - should we really limit each write to z_max_blksz?
744 		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
745 		 */
746 		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
747 		rw_enter(&zp->z_map_lock, RW_READER);
748 
749 		tx_bytes = uio->uio_resid;
750 		if (vn_has_cached_data(vp)) {
751 			rw_exit(&zp->z_map_lock);
752 			error = mappedwrite(vp, nbytes, uio, tx);
753 		} else {
754 			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
755 			    uio, nbytes, tx);
756 			rw_exit(&zp->z_map_lock);
757 		}
758 		tx_bytes -= uio->uio_resid;
759 
760 		/*
761 		 * If we made no progress, we're done.  If we made even
762 		 * partial progress, update the znode and ZIL accordingly.
763 		 */
764 		if (tx_bytes == 0) {
765 			dmu_tx_commit(tx);
766 			ASSERT(error != 0);
767 			break;
768 		}
769 
770 		/*
771 		 * Clear Set-UID/Set-GID bits on successful write if not
772 		 * privileged and at least one of the excute bits is set.
773 		 *
774 		 * It would be nice to to this after all writes have
775 		 * been done, but that would still expose the ISUID/ISGID
776 		 * to another app after the partial write is committed.
777 		 *
778 		 * Note: we don't call zfs_fuid_map_id() here because
779 		 * user 0 is not an ephemeral uid.
780 		 */
781 		mutex_enter(&zp->z_acl_lock);
782 		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
783 		    (S_IXUSR >> 6))) != 0 &&
784 		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
785 		    secpolicy_vnode_setid_retain(cr,
786 		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
787 		    zp->z_phys->zp_uid == 0) != 0) {
788 			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
789 		}
790 		mutex_exit(&zp->z_acl_lock);
791 
792 		/*
793 		 * Update time stamp.  NOTE: This marks the bonus buffer as
794 		 * dirty, so we don't have to do it again for zp_size.
795 		 */
796 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
797 
798 		/*
799 		 * Update the file size (zp_size) if it has changed;
800 		 * account for possible concurrent updates.
801 		 */
802 		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
803 			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
804 			    uio->uio_loffset);
805 		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
806 		dmu_tx_commit(tx);
807 
808 		if (error != 0)
809 			break;
810 		ASSERT(tx_bytes == nbytes);
811 		n -= nbytes;
812 	}
813 
814 	zfs_range_unlock(rl);
815 
816 	/*
817 	 * If we're in replay mode, or we made no progress, return error.
818 	 * Otherwise, it's at least a partial write, so it's successful.
819 	 */
820 	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
821 		ZFS_EXIT(zfsvfs);
822 		return (error);
823 	}
824 
825 	if (ioflag & (FSYNC | FDSYNC))
826 		zil_commit(zilog, zp->z_last_itx, zp->z_id);
827 
828 	ZFS_EXIT(zfsvfs);
829 	return (0);
830 }
831 
832 void
833 zfs_get_done(dmu_buf_t *db, void *vzgd)
834 {
835 	zgd_t *zgd = (zgd_t *)vzgd;
836 	rl_t *rl = zgd->zgd_rl;
837 	vnode_t *vp = ZTOV(rl->r_zp);
838 
839 	dmu_buf_rele(db, vzgd);
840 	zfs_range_unlock(rl);
841 	VN_RELE(vp);
842 	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
843 	kmem_free(zgd, sizeof (zgd_t));
844 }
845 
846 /*
847  * Get data to generate a TX_WRITE intent log record.
848  */
849 int
850 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
851 {
852 	zfsvfs_t *zfsvfs = arg;
853 	objset_t *os = zfsvfs->z_os;
854 	znode_t *zp;
855 	uint64_t off = lr->lr_offset;
856 	dmu_buf_t *db;
857 	rl_t *rl;
858 	zgd_t *zgd;
859 	int dlen = lr->lr_length;		/* length of user data */
860 	int error = 0;
861 
862 	ASSERT(zio);
863 	ASSERT(dlen != 0);
864 
865 	/*
866 	 * Nothing to do if the file has been removed
867 	 */
868 	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
869 		return (ENOENT);
870 	if (zp->z_unlinked) {
871 		VN_RELE(ZTOV(zp));
872 		return (ENOENT);
873 	}
874 
875 	/*
876 	 * Write records come in two flavors: immediate and indirect.
877 	 * For small writes it's cheaper to store the data with the
878 	 * log record (immediate); for large writes it's cheaper to
879 	 * sync the data and get a pointer to it (indirect) so that
880 	 * we don't have to write the data twice.
881 	 */
882 	if (buf != NULL) { /* immediate write */
883 		rl = zfs_range_lock(zp, off, dlen, RL_READER);
884 		/* test for truncation needs to be done while range locked */
885 		if (off >= zp->z_phys->zp_size) {
886 			error = ENOENT;
887 			goto out;
888 		}
889 		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
890 	} else { /* indirect write */
891 		uint64_t boff; /* block starting offset */
892 
893 		/*
894 		 * Have to lock the whole block to ensure when it's
895 		 * written out and it's checksum is being calculated
896 		 * that no one can change the data. We need to re-check
897 		 * blocksize after we get the lock in case it's changed!
898 		 */
899 		for (;;) {
900 			if (ISP2(zp->z_blksz)) {
901 				boff = P2ALIGN_TYPED(off, zp->z_blksz,
902 				    uint64_t);
903 			} else {
904 				boff = 0;
905 			}
906 			dlen = zp->z_blksz;
907 			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
908 			if (zp->z_blksz == dlen)
909 				break;
910 			zfs_range_unlock(rl);
911 		}
912 		/* test for truncation needs to be done while range locked */
913 		if (off >= zp->z_phys->zp_size) {
914 			error = ENOENT;
915 			goto out;
916 		}
917 		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
918 		zgd->zgd_rl = rl;
919 		zgd->zgd_zilog = zfsvfs->z_log;
920 		zgd->zgd_bp = &lr->lr_blkptr;
921 		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
922 		ASSERT(boff == db->db_offset);
923 		lr->lr_blkoff = off - boff;
924 		error = dmu_sync(zio, db, &lr->lr_blkptr,
925 		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
926 		ASSERT((error && error != EINPROGRESS) ||
927 		    lr->lr_length <= zp->z_blksz);
928 		if (error == 0) {
929 			zil_add_vdev(zfsvfs->z_log,
930 			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
931 		}
932 		/*
933 		 * If we get EINPROGRESS, then we need to wait for a
934 		 * write IO initiated by dmu_sync() to complete before
935 		 * we can release this dbuf.  We will finish everything
936 		 * up in the zfs_get_done() callback.
937 		 */
938 		if (error == EINPROGRESS)
939 			return (0);
940 		dmu_buf_rele(db, zgd);
941 		kmem_free(zgd, sizeof (zgd_t));
942 	}
943 out:
944 	zfs_range_unlock(rl);
945 	VN_RELE(ZTOV(zp));
946 	return (error);
947 }
948 
949 /*ARGSUSED*/
950 static int
951 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
952     caller_context_t *ct)
953 {
954 	znode_t *zp = VTOZ(vp);
955 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
956 	int error;
957 
958 	ZFS_ENTER(zfsvfs);
959 	ZFS_VERIFY_ZP(zp);
960 
961 	if (flag & V_ACE_MASK)
962 		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
963 	else
964 		error = zfs_zaccess_rwx(zp, mode, flag, cr);
965 
966 	ZFS_EXIT(zfsvfs);
967 	return (error);
968 }
969 
970 /*
971  * Lookup an entry in a directory, or an extended attribute directory.
972  * If it exists, return a held vnode reference for it.
973  *
974  *	IN:	dvp	- vnode of directory to search.
975  *		nm	- name of entry to lookup.
976  *		pnp	- full pathname to lookup [UNUSED].
977  *		flags	- LOOKUP_XATTR set if looking for an attribute.
978  *		rdir	- root directory vnode [UNUSED].
979  *		cr	- credentials of caller.
980  *		ct	- caller context
981  *		direntflags - directory lookup flags
982  *		realpnp - returned pathname.
983  *
984  *	OUT:	vpp	- vnode of located entry, NULL if not found.
985  *
986  *	RETURN:	0 if success
987  *		error code if failure
988  *
989  * Timestamps:
990  *	NA
991  */
992 /* ARGSUSED */
993 static int
994 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
995     int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
996     int *direntflags, pathname_t *realpnp)
997 {
998 	znode_t *zdp = VTOZ(dvp);
999 	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1000 	int	error;
1001 
1002 	ZFS_ENTER(zfsvfs);
1003 	ZFS_VERIFY_ZP(zdp);
1004 
1005 	*vpp = NULL;
1006 
1007 	if (flags & LOOKUP_XATTR) {
1008 		/*
1009 		 * If the xattr property is off, refuse the lookup request.
1010 		 */
1011 		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1012 			ZFS_EXIT(zfsvfs);
1013 			return (EINVAL);
1014 		}
1015 
1016 		/*
1017 		 * We don't allow recursive attributes..
1018 		 * Maybe someday we will.
1019 		 */
1020 		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
1021 			ZFS_EXIT(zfsvfs);
1022 			return (EINVAL);
1023 		}
1024 
1025 		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1026 			ZFS_EXIT(zfsvfs);
1027 			return (error);
1028 		}
1029 
1030 		/*
1031 		 * Do we have permission to get into attribute directory?
1032 		 */
1033 
1034 		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1035 		    B_FALSE, cr)) {
1036 			VN_RELE(*vpp);
1037 			*vpp = NULL;
1038 		}
1039 
1040 		ZFS_EXIT(zfsvfs);
1041 		return (error);
1042 	}
1043 
1044 	if (dvp->v_type != VDIR) {
1045 		ZFS_EXIT(zfsvfs);
1046 		return (ENOTDIR);
1047 	}
1048 
1049 	/*
1050 	 * Check accessibility of directory.
1051 	 */
1052 
1053 	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1054 		ZFS_EXIT(zfsvfs);
1055 		return (error);
1056 	}
1057 
1058 	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1059 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1060 		ZFS_EXIT(zfsvfs);
1061 		return (EILSEQ);
1062 	}
1063 
1064 	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1065 	if (error == 0) {
1066 		/*
1067 		 * Convert device special files
1068 		 */
1069 		if (IS_DEVVP(*vpp)) {
1070 			vnode_t	*svp;
1071 
1072 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1073 			VN_RELE(*vpp);
1074 			if (svp == NULL)
1075 				error = ENOSYS;
1076 			else
1077 				*vpp = svp;
1078 		}
1079 	}
1080 
1081 	ZFS_EXIT(zfsvfs);
1082 	return (error);
1083 }
1084 
1085 /*
1086  * Attempt to create a new entry in a directory.  If the entry
1087  * already exists, truncate the file if permissible, else return
1088  * an error.  Return the vp of the created or trunc'd file.
1089  *
1090  *	IN:	dvp	- vnode of directory to put new file entry in.
1091  *		name	- name of new file entry.
1092  *		vap	- attributes of new file.
1093  *		excl	- flag indicating exclusive or non-exclusive mode.
1094  *		mode	- mode to open file with.
1095  *		cr	- credentials of caller.
1096  *		flag	- large file flag [UNUSED].
1097  *		ct	- caller context
1098  *		vsecp 	- ACL to be set
1099  *
1100  *	OUT:	vpp	- vnode of created or trunc'd entry.
1101  *
1102  *	RETURN:	0 if success
1103  *		error code if failure
1104  *
1105  * Timestamps:
1106  *	dvp - ctime|mtime updated if new entry created
1107  *	 vp - ctime|mtime always, atime if new
1108  */
1109 
1110 /* ARGSUSED */
1111 static int
1112 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1113     int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1114     vsecattr_t *vsecp)
1115 {
1116 	znode_t		*zp, *dzp = VTOZ(dvp);
1117 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1118 	zilog_t		*zilog;
1119 	objset_t	*os;
1120 	zfs_dirlock_t	*dl;
1121 	dmu_tx_t	*tx;
1122 	int		error;
1123 	zfs_acl_t	*aclp = NULL;
1124 	zfs_fuid_info_t *fuidp = NULL;
1125 
1126 	/*
1127 	 * If we have an ephemeral id, ACL, or XVATTR then
1128 	 * make sure file system is at proper version
1129 	 */
1130 
1131 	if (zfsvfs->z_use_fuids == B_FALSE &&
1132 	    (vsecp || (vap->va_mask & AT_XVATTR) ||
1133 	    IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))))
1134 		return (EINVAL);
1135 
1136 	ZFS_ENTER(zfsvfs);
1137 	ZFS_VERIFY_ZP(dzp);
1138 	os = zfsvfs->z_os;
1139 	zilog = zfsvfs->z_log;
1140 
1141 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1142 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1143 		ZFS_EXIT(zfsvfs);
1144 		return (EILSEQ);
1145 	}
1146 
1147 	if (vap->va_mask & AT_XVATTR) {
1148 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1149 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1150 			ZFS_EXIT(zfsvfs);
1151 			return (error);
1152 		}
1153 	}
1154 top:
1155 	*vpp = NULL;
1156 
1157 	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1158 		vap->va_mode &= ~VSVTX;
1159 
1160 	if (*name == '\0') {
1161 		/*
1162 		 * Null component name refers to the directory itself.
1163 		 */
1164 		VN_HOLD(dvp);
1165 		zp = dzp;
1166 		dl = NULL;
1167 		error = 0;
1168 	} else {
1169 		/* possible VN_HOLD(zp) */
1170 		int zflg = 0;
1171 
1172 		if (flag & FIGNORECASE)
1173 			zflg |= ZCILOOK;
1174 
1175 		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1176 		    NULL, NULL);
1177 		if (error) {
1178 			if (strcmp(name, "..") == 0)
1179 				error = EISDIR;
1180 			ZFS_EXIT(zfsvfs);
1181 			if (aclp)
1182 				zfs_acl_free(aclp);
1183 			return (error);
1184 		}
1185 	}
1186 	if (vsecp && aclp == NULL) {
1187 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
1188 		if (error) {
1189 			ZFS_EXIT(zfsvfs);
1190 			if (dl)
1191 				zfs_dirent_unlock(dl);
1192 			return (error);
1193 		}
1194 	}
1195 
1196 	if (zp == NULL) {
1197 		uint64_t txtype;
1198 
1199 		/*
1200 		 * Create a new file object and update the directory
1201 		 * to reference it.
1202 		 */
1203 		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1204 			goto out;
1205 		}
1206 
1207 		/*
1208 		 * We only support the creation of regular files in
1209 		 * extended attribute directories.
1210 		 */
1211 		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
1212 		    (vap->va_type != VREG)) {
1213 			error = EINVAL;
1214 			goto out;
1215 		}
1216 
1217 		tx = dmu_tx_create(os);
1218 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1219 		if (zfsvfs->z_fuid_obj == 0) {
1220 			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1221 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1222 			    SPA_MAXBLOCKSIZE);
1223 			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1224 		} else {
1225 			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1226 			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1227 			    SPA_MAXBLOCKSIZE);
1228 		}
1229 		dmu_tx_hold_bonus(tx, dzp->z_id);
1230 		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1231 		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
1232 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1233 			    0, SPA_MAXBLOCKSIZE);
1234 		}
1235 		error = dmu_tx_assign(tx, zfsvfs->z_assign);
1236 		if (error) {
1237 			zfs_dirent_unlock(dl);
1238 			if (error == ERESTART &&
1239 			    zfsvfs->z_assign == TXG_NOWAIT) {
1240 				dmu_tx_wait(tx);
1241 				dmu_tx_abort(tx);
1242 				goto top;
1243 			}
1244 			dmu_tx_abort(tx);
1245 			ZFS_EXIT(zfsvfs);
1246 			if (aclp)
1247 				zfs_acl_free(aclp);
1248 			return (error);
1249 		}
1250 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1251 		(void) zfs_link_create(dl, zp, tx, ZNEW);
1252 		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1253 		if (flag & FIGNORECASE)
1254 			txtype |= TX_CI;
1255 		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1256 		    vsecp, fuidp, vap);
1257 		if (fuidp)
1258 			zfs_fuid_info_free(fuidp);
1259 		dmu_tx_commit(tx);
1260 	} else {
1261 		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1262 
1263 		/*
1264 		 * A directory entry already exists for this name.
1265 		 */
1266 		/*
1267 		 * Can't truncate an existing file if in exclusive mode.
1268 		 */
1269 		if (excl == EXCL) {
1270 			error = EEXIST;
1271 			goto out;
1272 		}
1273 		/*
1274 		 * Can't open a directory for writing.
1275 		 */
1276 		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1277 			error = EISDIR;
1278 			goto out;
1279 		}
1280 		/*
1281 		 * Verify requested access to file.
1282 		 */
1283 		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1284 			goto out;
1285 		}
1286 
1287 		mutex_enter(&dzp->z_lock);
1288 		dzp->z_seq++;
1289 		mutex_exit(&dzp->z_lock);
1290 
1291 		/*
1292 		 * Truncate regular files if requested.
1293 		 */
1294 		if ((ZTOV(zp)->v_type == VREG) &&
1295 		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1296 			error = zfs_freesp(zp, 0, 0, mode, TRUE);
1297 			if (error == ERESTART &&
1298 			    zfsvfs->z_assign == TXG_NOWAIT) {
1299 				/* NB: we already did dmu_tx_wait() */
1300 				zfs_dirent_unlock(dl);
1301 				VN_RELE(ZTOV(zp));
1302 				goto top;
1303 			}
1304 
1305 			if (error == 0) {
1306 				vnevent_create(ZTOV(zp), ct);
1307 			}
1308 		}
1309 	}
1310 out:
1311 
1312 	if (dl)
1313 		zfs_dirent_unlock(dl);
1314 
1315 	if (error) {
1316 		if (zp)
1317 			VN_RELE(ZTOV(zp));
1318 	} else {
1319 		*vpp = ZTOV(zp);
1320 		/*
1321 		 * If vnode is for a device return a specfs vnode instead.
1322 		 */
1323 		if (IS_DEVVP(*vpp)) {
1324 			struct vnode *svp;
1325 
1326 			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1327 			VN_RELE(*vpp);
1328 			if (svp == NULL) {
1329 				error = ENOSYS;
1330 			}
1331 			*vpp = svp;
1332 		}
1333 	}
1334 	if (aclp)
1335 		zfs_acl_free(aclp);
1336 
1337 	ZFS_EXIT(zfsvfs);
1338 	return (error);
1339 }
1340 
1341 /*
1342  * Remove an entry from a directory.
1343  *
1344  *	IN:	dvp	- vnode of directory to remove entry from.
1345  *		name	- name of entry to remove.
1346  *		cr	- credentials of caller.
1347  *		ct	- caller context
1348  *		flags	- case flags
1349  *
1350  *	RETURN:	0 if success
1351  *		error code if failure
1352  *
1353  * Timestamps:
1354  *	dvp - ctime|mtime
1355  *	 vp - ctime (if nlink > 0)
1356  */
1357 /*ARGSUSED*/
1358 static int
1359 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1360     int flags)
1361 {
1362 	znode_t		*zp, *dzp = VTOZ(dvp);
1363 	znode_t		*xzp = NULL;
1364 	vnode_t		*vp;
1365 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1366 	zilog_t		*zilog;
1367 	uint64_t	acl_obj, xattr_obj;
1368 	zfs_dirlock_t	*dl;
1369 	dmu_tx_t	*tx;
1370 	boolean_t	may_delete_now, delete_now = FALSE;
1371 	boolean_t	unlinked;
1372 	uint64_t	txtype;
1373 	pathname_t	*realnmp = NULL;
1374 	pathname_t	realnm;
1375 	int		error;
1376 	int		zflg = ZEXISTS;
1377 
1378 	ZFS_ENTER(zfsvfs);
1379 	ZFS_VERIFY_ZP(dzp);
1380 	zilog = zfsvfs->z_log;
1381 
1382 	if (flags & FIGNORECASE) {
1383 		zflg |= ZCILOOK;
1384 		pn_alloc(&realnm);
1385 		realnmp = &realnm;
1386 	}
1387 
1388 top:
1389 	/*
1390 	 * Attempt to lock directory; fail if entry doesn't exist.
1391 	 */
1392 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1393 	    NULL, realnmp)) {
1394 		if (realnmp)
1395 			pn_free(realnmp);
1396 		ZFS_EXIT(zfsvfs);
1397 		return (error);
1398 	}
1399 
1400 	vp = ZTOV(zp);
1401 
1402 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1403 		goto out;
1404 	}
1405 
1406 	/*
1407 	 * Need to use rmdir for removing directories.
1408 	 */
1409 	if (vp->v_type == VDIR) {
1410 		error = EPERM;
1411 		goto out;
1412 	}
1413 
1414 	vnevent_remove(vp, dvp, name, ct);
1415 
1416 	if (realnmp)
1417 		dnlc_remove(dvp, realnmp->pn_path);
1418 	else
1419 		dnlc_remove(dvp, name);
1420 
1421 	mutex_enter(&vp->v_lock);
1422 	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1423 	mutex_exit(&vp->v_lock);
1424 
1425 	/*
1426 	 * We may delete the znode now, or we may put it in the unlinked set;
1427 	 * it depends on whether we're the last link, and on whether there are
1428 	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
1429 	 * allow for either case.
1430 	 */
1431 	tx = dmu_tx_create(zfsvfs->z_os);
1432 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1433 	dmu_tx_hold_bonus(tx, zp->z_id);
1434 	if (may_delete_now)
1435 		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
1436 
1437 	/* are there any extended attributes? */
1438 	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
1439 		/* XXX - do we need this if we are deleting? */
1440 		dmu_tx_hold_bonus(tx, xattr_obj);
1441 	}
1442 
1443 	/* are there any additional acls */
1444 	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
1445 	    may_delete_now)
1446 		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1447 
1448 	/* charge as an update -- would be nice not to charge at all */
1449 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1450 
1451 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1452 	if (error) {
1453 		zfs_dirent_unlock(dl);
1454 		VN_RELE(vp);
1455 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1456 			dmu_tx_wait(tx);
1457 			dmu_tx_abort(tx);
1458 			goto top;
1459 		}
1460 		if (realnmp)
1461 			pn_free(realnmp);
1462 		dmu_tx_abort(tx);
1463 		ZFS_EXIT(zfsvfs);
1464 		return (error);
1465 	}
1466 
1467 	/*
1468 	 * Remove the directory entry.
1469 	 */
1470 	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1471 
1472 	if (error) {
1473 		dmu_tx_commit(tx);
1474 		goto out;
1475 	}
1476 
1477 	if (unlinked) {
1478 		mutex_enter(&vp->v_lock);
1479 		delete_now = may_delete_now &&
1480 		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
1481 		    zp->z_phys->zp_xattr == xattr_obj &&
1482 		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
1483 		mutex_exit(&vp->v_lock);
1484 	}
1485 
1486 	if (delete_now) {
1487 		if (zp->z_phys->zp_xattr) {
1488 			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
1489 			ASSERT3U(error, ==, 0);
1490 			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
1491 			dmu_buf_will_dirty(xzp->z_dbuf, tx);
1492 			mutex_enter(&xzp->z_lock);
1493 			xzp->z_unlinked = 1;
1494 			xzp->z_phys->zp_links = 0;
1495 			mutex_exit(&xzp->z_lock);
1496 			zfs_unlinked_add(xzp, tx);
1497 			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
1498 		}
1499 		mutex_enter(&zp->z_lock);
1500 		mutex_enter(&vp->v_lock);
1501 		vp->v_count--;
1502 		ASSERT3U(vp->v_count, ==, 0);
1503 		mutex_exit(&vp->v_lock);
1504 		mutex_exit(&zp->z_lock);
1505 		zfs_znode_delete(zp, tx);
1506 	} else if (unlinked) {
1507 		zfs_unlinked_add(zp, tx);
1508 	}
1509 
1510 	txtype = TX_REMOVE;
1511 	if (flags & FIGNORECASE)
1512 		txtype |= TX_CI;
1513 	zfs_log_remove(zilog, tx, txtype, dzp, name);
1514 
1515 	dmu_tx_commit(tx);
1516 out:
1517 	if (realnmp)
1518 		pn_free(realnmp);
1519 
1520 	zfs_dirent_unlock(dl);
1521 
1522 	if (!delete_now) {
1523 		VN_RELE(vp);
1524 	} else if (xzp) {
1525 		/* this rele delayed to prevent nesting transactions */
1526 		VN_RELE(ZTOV(xzp));
1527 	}
1528 
1529 	ZFS_EXIT(zfsvfs);
1530 	return (error);
1531 }
1532 
1533 /*
1534  * Create a new directory and insert it into dvp using the name
1535  * provided.  Return a pointer to the inserted directory.
1536  *
1537  *	IN:	dvp	- vnode of directory to add subdir to.
1538  *		dirname	- name of new directory.
1539  *		vap	- attributes of new directory.
1540  *		cr	- credentials of caller.
1541  *		ct	- caller context
1542  *		vsecp	- ACL to be set
1543  *
1544  *	OUT:	vpp	- vnode of created directory.
1545  *
1546  *	RETURN:	0 if success
1547  *		error code if failure
1548  *
1549  * Timestamps:
1550  *	dvp - ctime|mtime updated
1551  *	 vp - ctime|mtime|atime updated
1552  */
1553 /*ARGSUSED*/
1554 static int
1555 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1556     caller_context_t *ct, int flags, vsecattr_t *vsecp)
1557 {
1558 	znode_t		*zp, *dzp = VTOZ(dvp);
1559 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1560 	zilog_t		*zilog;
1561 	zfs_dirlock_t	*dl;
1562 	uint64_t	txtype;
1563 	dmu_tx_t	*tx;
1564 	int		error;
1565 	zfs_acl_t	*aclp = NULL;
1566 	zfs_fuid_info_t	*fuidp = NULL;
1567 	int		zf = ZNEW;
1568 
1569 	ASSERT(vap->va_type == VDIR);
1570 
1571 	/*
1572 	 * If we have an ephemeral id, ACL, or XVATTR then
1573 	 * make sure file system is at proper version
1574 	 */
1575 
1576 	if (zfsvfs->z_use_fuids == B_FALSE &&
1577 	    (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))||
1578 	    IS_EPHEMERAL(crgetgid(cr))))
1579 		return (EINVAL);
1580 
1581 	ZFS_ENTER(zfsvfs);
1582 	ZFS_VERIFY_ZP(dzp);
1583 	zilog = zfsvfs->z_log;
1584 
1585 	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
1586 		ZFS_EXIT(zfsvfs);
1587 		return (EINVAL);
1588 	}
1589 
1590 	if (zfsvfs->z_utf8 && u8_validate(dirname,
1591 	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1592 		ZFS_EXIT(zfsvfs);
1593 		return (EILSEQ);
1594 	}
1595 	if (flags & FIGNORECASE)
1596 		zf |= ZCILOOK;
1597 
1598 	if (vap->va_mask & AT_XVATTR)
1599 		if ((error = secpolicy_xvattr((xvattr_t *)vap,
1600 		    crgetuid(cr), cr, vap->va_type)) != 0) {
1601 			ZFS_EXIT(zfsvfs);
1602 			return (error);
1603 		}
1604 
1605 	/*
1606 	 * First make sure the new directory doesn't exist.
1607 	 */
1608 top:
1609 	*vpp = NULL;
1610 
1611 	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1612 	    NULL, NULL)) {
1613 		ZFS_EXIT(zfsvfs);
1614 		return (error);
1615 	}
1616 
1617 	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1618 		zfs_dirent_unlock(dl);
1619 		ZFS_EXIT(zfsvfs);
1620 		return (error);
1621 	}
1622 
1623 	if (vsecp && aclp == NULL) {
1624 		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
1625 		if (error) {
1626 			zfs_dirent_unlock(dl);
1627 			ZFS_EXIT(zfsvfs);
1628 			return (error);
1629 		}
1630 	}
1631 	/*
1632 	 * Add a new entry to the directory.
1633 	 */
1634 	tx = dmu_tx_create(zfsvfs->z_os);
1635 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1636 	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1637 	if (zfsvfs->z_fuid_obj == 0) {
1638 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1639 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1640 		    SPA_MAXBLOCKSIZE);
1641 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1642 	} else {
1643 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1644 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1645 		    SPA_MAXBLOCKSIZE);
1646 	}
1647 	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
1648 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1649 		    0, SPA_MAXBLOCKSIZE);
1650 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1651 	if (error) {
1652 		zfs_dirent_unlock(dl);
1653 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1654 			dmu_tx_wait(tx);
1655 			dmu_tx_abort(tx);
1656 			goto top;
1657 		}
1658 		dmu_tx_abort(tx);
1659 		ZFS_EXIT(zfsvfs);
1660 		if (aclp)
1661 			zfs_acl_free(aclp);
1662 		return (error);
1663 	}
1664 
1665 	/*
1666 	 * Create new node.
1667 	 */
1668 	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
1669 
1670 	if (aclp)
1671 		zfs_acl_free(aclp);
1672 
1673 	/*
1674 	 * Now put new name in parent dir.
1675 	 */
1676 	(void) zfs_link_create(dl, zp, tx, ZNEW);
1677 
1678 	*vpp = ZTOV(zp);
1679 
1680 	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1681 	if (flags & FIGNORECASE)
1682 		txtype |= TX_CI;
1683 	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
1684 
1685 	if (fuidp)
1686 		zfs_fuid_info_free(fuidp);
1687 	dmu_tx_commit(tx);
1688 
1689 	zfs_dirent_unlock(dl);
1690 
1691 	ZFS_EXIT(zfsvfs);
1692 	return (0);
1693 }
1694 
1695 /*
1696  * Remove a directory subdir entry.  If the current working
1697  * directory is the same as the subdir to be removed, the
1698  * remove will fail.
1699  *
1700  *	IN:	dvp	- vnode of directory to remove from.
1701  *		name	- name of directory to be removed.
1702  *		cwd	- vnode of current working directory.
1703  *		cr	- credentials of caller.
1704  *		ct	- caller context
1705  *		flags	- case flags
1706  *
1707  *	RETURN:	0 if success
1708  *		error code if failure
1709  *
1710  * Timestamps:
1711  *	dvp - ctime|mtime updated
1712  */
1713 /*ARGSUSED*/
1714 static int
1715 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1716     caller_context_t *ct, int flags)
1717 {
1718 	znode_t		*dzp = VTOZ(dvp);
1719 	znode_t		*zp;
1720 	vnode_t		*vp;
1721 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
1722 	zilog_t		*zilog;
1723 	zfs_dirlock_t	*dl;
1724 	dmu_tx_t	*tx;
1725 	int		error;
1726 	int		zflg = ZEXISTS;
1727 
1728 	ZFS_ENTER(zfsvfs);
1729 	ZFS_VERIFY_ZP(dzp);
1730 	zilog = zfsvfs->z_log;
1731 
1732 	if (flags & FIGNORECASE)
1733 		zflg |= ZCILOOK;
1734 top:
1735 	zp = NULL;
1736 
1737 	/*
1738 	 * Attempt to lock directory; fail if entry doesn't exist.
1739 	 */
1740 	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1741 	    NULL, NULL)) {
1742 		ZFS_EXIT(zfsvfs);
1743 		return (error);
1744 	}
1745 
1746 	vp = ZTOV(zp);
1747 
1748 	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1749 		goto out;
1750 	}
1751 
1752 	if (vp->v_type != VDIR) {
1753 		error = ENOTDIR;
1754 		goto out;
1755 	}
1756 
1757 	if (vp == cwd) {
1758 		error = EINVAL;
1759 		goto out;
1760 	}
1761 
1762 	vnevent_rmdir(vp, dvp, name, ct);
1763 
1764 	/*
1765 	 * Grab a lock on the directory to make sure that noone is
1766 	 * trying to add (or lookup) entries while we are removing it.
1767 	 */
1768 	rw_enter(&zp->z_name_lock, RW_WRITER);
1769 
1770 	/*
1771 	 * Grab a lock on the parent pointer to make sure we play well
1772 	 * with the treewalk and directory rename code.
1773 	 */
1774 	rw_enter(&zp->z_parent_lock, RW_WRITER);
1775 
1776 	tx = dmu_tx_create(zfsvfs->z_os);
1777 	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1778 	dmu_tx_hold_bonus(tx, zp->z_id);
1779 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1780 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
1781 	if (error) {
1782 		rw_exit(&zp->z_parent_lock);
1783 		rw_exit(&zp->z_name_lock);
1784 		zfs_dirent_unlock(dl);
1785 		VN_RELE(vp);
1786 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
1787 			dmu_tx_wait(tx);
1788 			dmu_tx_abort(tx);
1789 			goto top;
1790 		}
1791 		dmu_tx_abort(tx);
1792 		ZFS_EXIT(zfsvfs);
1793 		return (error);
1794 	}
1795 
1796 	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
1797 
1798 	if (error == 0) {
1799 		uint64_t txtype = TX_RMDIR;
1800 		if (flags & FIGNORECASE)
1801 			txtype |= TX_CI;
1802 		zfs_log_remove(zilog, tx, txtype, dzp, name);
1803 	}
1804 
1805 	dmu_tx_commit(tx);
1806 
1807 	rw_exit(&zp->z_parent_lock);
1808 	rw_exit(&zp->z_name_lock);
1809 out:
1810 	zfs_dirent_unlock(dl);
1811 
1812 	VN_RELE(vp);
1813 
1814 	ZFS_EXIT(zfsvfs);
1815 	return (error);
1816 }
1817 
1818 /*
1819  * Read as many directory entries as will fit into the provided
1820  * buffer from the given directory cursor position (specified in
1821  * the uio structure.
1822  *
1823  *	IN:	vp	- vnode of directory to read.
1824  *		uio	- structure supplying read location, range info,
1825  *			  and return buffer.
1826  *		cr	- credentials of caller.
1827  *		ct	- caller context
1828  *		flags	- case flags
1829  *
1830  *	OUT:	uio	- updated offset and range, buffer filled.
1831  *		eofp	- set to true if end-of-file detected.
1832  *
1833  *	RETURN:	0 if success
1834  *		error code if failure
1835  *
1836  * Timestamps:
1837  *	vp - atime updated
1838  *
1839  * Note that the low 4 bits of the cookie returned by zap is always zero.
1840  * This allows us to use the low range for "special" directory entries:
1841  * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
1842  * we use the offset 2 for the '.zfs' directory.
1843  */
1844 /* ARGSUSED */
1845 static int
1846 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
1847     caller_context_t *ct, int flags)
1848 {
1849 	znode_t		*zp = VTOZ(vp);
1850 	iovec_t		*iovp;
1851 	edirent_t	*eodp;
1852 	dirent64_t	*odp;
1853 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
1854 	objset_t	*os;
1855 	caddr_t		outbuf;
1856 	size_t		bufsize;
1857 	zap_cursor_t	zc;
1858 	zap_attribute_t	zap;
1859 	uint_t		bytes_wanted;
1860 	uint64_t	offset; /* must be unsigned; checks for < 1 */
1861 	int		local_eof;
1862 	int		outcount;
1863 	int		error;
1864 	uint8_t		prefetch;
1865 	boolean_t	check_sysattrs;
1866 
1867 	ZFS_ENTER(zfsvfs);
1868 	ZFS_VERIFY_ZP(zp);
1869 
1870 	/*
1871 	 * If we are not given an eof variable,
1872 	 * use a local one.
1873 	 */
1874 	if (eofp == NULL)
1875 		eofp = &local_eof;
1876 
1877 	/*
1878 	 * Check for valid iov_len.
1879 	 */
1880 	if (uio->uio_iov->iov_len <= 0) {
1881 		ZFS_EXIT(zfsvfs);
1882 		return (EINVAL);
1883 	}
1884 
1885 	/*
1886 	 * Quit if directory has been removed (posix)
1887 	 */
1888 	if ((*eofp = zp->z_unlinked) != 0) {
1889 		ZFS_EXIT(zfsvfs);
1890 		return (0);
1891 	}
1892 
1893 	error = 0;
1894 	os = zfsvfs->z_os;
1895 	offset = uio->uio_loffset;
1896 	prefetch = zp->z_zn_prefetch;
1897 
1898 	/*
1899 	 * Initialize the iterator cursor.
1900 	 */
1901 	if (offset <= 3) {
1902 		/*
1903 		 * Start iteration from the beginning of the directory.
1904 		 */
1905 		zap_cursor_init(&zc, os, zp->z_id);
1906 	} else {
1907 		/*
1908 		 * The offset is a serialized cursor.
1909 		 */
1910 		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
1911 	}
1912 
1913 	/*
1914 	 * Get space to change directory entries into fs independent format.
1915 	 */
1916 	iovp = uio->uio_iov;
1917 	bytes_wanted = iovp->iov_len;
1918 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
1919 		bufsize = bytes_wanted;
1920 		outbuf = kmem_alloc(bufsize, KM_SLEEP);
1921 		odp = (struct dirent64 *)outbuf;
1922 	} else {
1923 		bufsize = bytes_wanted;
1924 		odp = (struct dirent64 *)iovp->iov_base;
1925 	}
1926 	eodp = (struct edirent *)odp;
1927 
1928 	/*
1929 	 * If this VFS supports system attributes; and we're looking at an
1930 	 * extended attribute directory; and we care about normalization
1931 	 * conflicts on this vfs; then we must check for normalization
1932 	 * conflicts with the sysattr name space.
1933 	 */
1934 	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
1935 	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
1936 	    (flags & V_RDDIR_ENTFLAGS);
1937 
1938 	/*
1939 	 * Transform to file-system independent format
1940 	 */
1941 	outcount = 0;
1942 	while (outcount < bytes_wanted) {
1943 		ino64_t objnum;
1944 		ushort_t reclen;
1945 		off64_t *next;
1946 
1947 		/*
1948 		 * Special case `.', `..', and `.zfs'.
1949 		 */
1950 		if (offset == 0) {
1951 			(void) strcpy(zap.za_name, ".");
1952 			zap.za_normalization_conflict = 0;
1953 			objnum = zp->z_id;
1954 		} else if (offset == 1) {
1955 			(void) strcpy(zap.za_name, "..");
1956 			zap.za_normalization_conflict = 0;
1957 			objnum = zp->z_phys->zp_parent;
1958 		} else if (offset == 2 && zfs_show_ctldir(zp)) {
1959 			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
1960 			zap.za_normalization_conflict = 0;
1961 			objnum = ZFSCTL_INO_ROOT;
1962 		} else {
1963 			/*
1964 			 * Grab next entry.
1965 			 */
1966 			if (error = zap_cursor_retrieve(&zc, &zap)) {
1967 				if ((*eofp = (error == ENOENT)) != 0)
1968 					break;
1969 				else
1970 					goto update;
1971 			}
1972 
1973 			if (zap.za_integer_length != 8 ||
1974 			    zap.za_num_integers != 1) {
1975 				cmn_err(CE_WARN, "zap_readdir: bad directory "
1976 				    "entry, obj = %lld, offset = %lld\n",
1977 				    (u_longlong_t)zp->z_id,
1978 				    (u_longlong_t)offset);
1979 				error = ENXIO;
1980 				goto update;
1981 			}
1982 
1983 			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
1984 			/*
1985 			 * MacOS X can extract the object type here such as:
1986 			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
1987 			 */
1988 
1989 			if (check_sysattrs && !zap.za_normalization_conflict) {
1990 				zap.za_normalization_conflict =
1991 				    xattr_sysattr_casechk(zap.za_name);
1992 			}
1993 		}
1994 
1995 		if (flags & V_RDDIR_ENTFLAGS)
1996 			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
1997 		else
1998 			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
1999 
2000 		/*
2001 		 * Will this entry fit in the buffer?
2002 		 */
2003 		if (outcount + reclen > bufsize) {
2004 			/*
2005 			 * Did we manage to fit anything in the buffer?
2006 			 */
2007 			if (!outcount) {
2008 				error = EINVAL;
2009 				goto update;
2010 			}
2011 			break;
2012 		}
2013 		if (flags & V_RDDIR_ENTFLAGS) {
2014 			/*
2015 			 * Add extended flag entry:
2016 			 */
2017 			eodp->ed_ino = objnum;
2018 			eodp->ed_reclen = reclen;
2019 			/* NOTE: ed_off is the offset for the *next* entry */
2020 			next = &(eodp->ed_off);
2021 			eodp->ed_eflags = zap.za_normalization_conflict ?
2022 			    ED_CASE_CONFLICT : 0;
2023 			(void) strncpy(eodp->ed_name, zap.za_name,
2024 			    EDIRENT_NAMELEN(reclen));
2025 			eodp = (edirent_t *)((intptr_t)eodp + reclen);
2026 		} else {
2027 			/*
2028 			 * Add normal entry:
2029 			 */
2030 			odp->d_ino = objnum;
2031 			odp->d_reclen = reclen;
2032 			/* NOTE: d_off is the offset for the *next* entry */
2033 			next = &(odp->d_off);
2034 			(void) strncpy(odp->d_name, zap.za_name,
2035 			    DIRENT64_NAMELEN(reclen));
2036 			odp = (dirent64_t *)((intptr_t)odp + reclen);
2037 		}
2038 		outcount += reclen;
2039 
2040 		ASSERT(outcount <= bufsize);
2041 
2042 		/* Prefetch znode */
2043 		if (prefetch)
2044 			dmu_prefetch(os, objnum, 0, 0);
2045 
2046 		/*
2047 		 * Move to the next entry, fill in the previous offset.
2048 		 */
2049 		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2050 			zap_cursor_advance(&zc);
2051 			offset = zap_cursor_serialize(&zc);
2052 		} else {
2053 			offset += 1;
2054 		}
2055 		*next = offset;
2056 	}
2057 	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2058 
2059 	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2060 		iovp->iov_base += outcount;
2061 		iovp->iov_len -= outcount;
2062 		uio->uio_resid -= outcount;
2063 	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2064 		/*
2065 		 * Reset the pointer.
2066 		 */
2067 		offset = uio->uio_loffset;
2068 	}
2069 
2070 update:
2071 	zap_cursor_fini(&zc);
2072 	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2073 		kmem_free(outbuf, bufsize);
2074 
2075 	if (error == ENOENT)
2076 		error = 0;
2077 
2078 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2079 
2080 	uio->uio_loffset = offset;
2081 	ZFS_EXIT(zfsvfs);
2082 	return (error);
2083 }
2084 
2085 ulong_t zfs_fsync_sync_cnt = 4;
2086 
2087 static int
2088 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2089 {
2090 	znode_t	*zp = VTOZ(vp);
2091 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2092 
2093 	/*
2094 	 * Regardless of whether this is required for standards conformance,
2095 	 * this is the logical behavior when fsync() is called on a file with
2096 	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
2097 	 * going to be pushed out as part of the zil_commit().
2098 	 */
2099 	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2100 	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2101 		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2102 
2103 	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2104 
2105 	ZFS_ENTER(zfsvfs);
2106 	ZFS_VERIFY_ZP(zp);
2107 	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
2108 	ZFS_EXIT(zfsvfs);
2109 	return (0);
2110 }
2111 
2112 
2113 /*
2114  * Get the requested file attributes and place them in the provided
2115  * vattr structure.
2116  *
2117  *	IN:	vp	- vnode of file.
2118  *		vap	- va_mask identifies requested attributes.
2119  *			  If AT_XVATTR set, then optional attrs are requested
2120  *		flags	- ATTR_NOACLCHECK (CIFS server context)
2121  *		cr	- credentials of caller.
2122  *		ct	- caller context
2123  *
2124  *	OUT:	vap	- attribute values.
2125  *
2126  *	RETURN:	0 (always succeeds)
2127  */
2128 /* ARGSUSED */
2129 static int
2130 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2131     caller_context_t *ct)
2132 {
2133 	znode_t *zp = VTOZ(vp);
2134 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2135 	znode_phys_t *pzp;
2136 	int	error = 0;
2137 	uint64_t links;
2138 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2139 	xoptattr_t *xoap = NULL;
2140 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2141 
2142 	ZFS_ENTER(zfsvfs);
2143 	ZFS_VERIFY_ZP(zp);
2144 	pzp = zp->z_phys;
2145 
2146 	mutex_enter(&zp->z_lock);
2147 
2148 	/*
2149 	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2150 	 * Also, if we are the owner don't bother, since owner should
2151 	 * always be allowed to read basic attributes of file.
2152 	 */
2153 	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
2154 	    (pzp->zp_uid != crgetuid(cr))) {
2155 		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2156 		    skipaclchk, cr)) {
2157 			mutex_exit(&zp->z_lock);
2158 			ZFS_EXIT(zfsvfs);
2159 			return (error);
2160 		}
2161 	}
2162 
2163 	/*
2164 	 * Return all attributes.  It's cheaper to provide the answer
2165 	 * than to determine whether we were asked the question.
2166 	 */
2167 
2168 	vap->va_type = vp->v_type;
2169 	vap->va_mode = pzp->zp_mode & MODEMASK;
2170 	zfs_fuid_map_ids(zp, &vap->va_uid, &vap->va_gid);
2171 	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2172 	vap->va_nodeid = zp->z_id;
2173 	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2174 		links = pzp->zp_links + 1;
2175 	else
2176 		links = pzp->zp_links;
2177 	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
2178 	vap->va_size = pzp->zp_size;
2179 	vap->va_rdev = vp->v_rdev;
2180 	vap->va_seq = zp->z_seq;
2181 
2182 	/*
2183 	 * Add in any requested optional attributes and the create time.
2184 	 * Also set the corresponding bits in the returned attribute bitmap.
2185 	 */
2186 	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2187 		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2188 			xoap->xoa_archive =
2189 			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
2190 			XVA_SET_RTN(xvap, XAT_ARCHIVE);
2191 		}
2192 
2193 		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2194 			xoap->xoa_readonly =
2195 			    ((pzp->zp_flags & ZFS_READONLY) != 0);
2196 			XVA_SET_RTN(xvap, XAT_READONLY);
2197 		}
2198 
2199 		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2200 			xoap->xoa_system =
2201 			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
2202 			XVA_SET_RTN(xvap, XAT_SYSTEM);
2203 		}
2204 
2205 		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2206 			xoap->xoa_hidden =
2207 			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
2208 			XVA_SET_RTN(xvap, XAT_HIDDEN);
2209 		}
2210 
2211 		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2212 			xoap->xoa_nounlink =
2213 			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
2214 			XVA_SET_RTN(xvap, XAT_NOUNLINK);
2215 		}
2216 
2217 		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2218 			xoap->xoa_immutable =
2219 			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
2220 			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2221 		}
2222 
2223 		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2224 			xoap->xoa_appendonly =
2225 			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
2226 			XVA_SET_RTN(xvap, XAT_APPENDONLY);
2227 		}
2228 
2229 		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2230 			xoap->xoa_nodump =
2231 			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
2232 			XVA_SET_RTN(xvap, XAT_NODUMP);
2233 		}
2234 
2235 		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2236 			xoap->xoa_opaque =
2237 			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
2238 			XVA_SET_RTN(xvap, XAT_OPAQUE);
2239 		}
2240 
2241 		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2242 			xoap->xoa_av_quarantined =
2243 			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
2244 			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2245 		}
2246 
2247 		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2248 			xoap->xoa_av_modified =
2249 			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
2250 			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2251 		}
2252 
2253 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2254 		    vp->v_type == VREG &&
2255 		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
2256 			size_t len;
2257 			dmu_object_info_t doi;
2258 
2259 			/*
2260 			 * Only VREG files have anti-virus scanstamps, so we
2261 			 * won't conflict with symlinks in the bonus buffer.
2262 			 */
2263 			dmu_object_info_from_db(zp->z_dbuf, &doi);
2264 			len = sizeof (xoap->xoa_av_scanstamp) +
2265 			    sizeof (znode_phys_t);
2266 			if (len <= doi.doi_bonus_size) {
2267 				/*
2268 				 * pzp points to the start of the
2269 				 * znode_phys_t. pzp + 1 points to the
2270 				 * first byte after the znode_phys_t.
2271 				 */
2272 				(void) memcpy(xoap->xoa_av_scanstamp,
2273 				    pzp + 1,
2274 				    sizeof (xoap->xoa_av_scanstamp));
2275 				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
2276 			}
2277 		}
2278 
2279 		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2280 			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
2281 			XVA_SET_RTN(xvap, XAT_CREATETIME);
2282 		}
2283 	}
2284 
2285 	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
2286 	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
2287 	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
2288 
2289 	mutex_exit(&zp->z_lock);
2290 
2291 	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
2292 
2293 	if (zp->z_blksz == 0) {
2294 		/*
2295 		 * Block size hasn't been set; suggest maximal I/O transfers.
2296 		 */
2297 		vap->va_blksize = zfsvfs->z_max_blksz;
2298 	}
2299 
2300 	ZFS_EXIT(zfsvfs);
2301 	return (0);
2302 }
2303 
2304 /*
2305  * Set the file attributes to the values contained in the
2306  * vattr structure.
2307  *
2308  *	IN:	vp	- vnode of file to be modified.
2309  *		vap	- new attribute values.
2310  *			  If AT_XVATTR set, then optional attrs are being set
2311  *		flags	- ATTR_UTIME set if non-default time values provided.
2312  *			- ATTR_NOACLCHECK (CIFS context only).
2313  *		cr	- credentials of caller.
2314  *		ct	- caller context
2315  *
2316  *	RETURN:	0 if success
2317  *		error code if failure
2318  *
2319  * Timestamps:
2320  *	vp - ctime updated, mtime updated if size changed.
2321  */
2322 /* ARGSUSED */
2323 static int
2324 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2325 	caller_context_t *ct)
2326 {
2327 	znode_t		*zp = VTOZ(vp);
2328 	znode_phys_t	*pzp;
2329 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
2330 	zilog_t		*zilog;
2331 	dmu_tx_t	*tx;
2332 	vattr_t		oldva;
2333 	uint_t		mask = vap->va_mask;
2334 	uint_t		saved_mask;
2335 	int		trim_mask = 0;
2336 	uint64_t	new_mode;
2337 	znode_t		*attrzp;
2338 	int		need_policy = FALSE;
2339 	int		err;
2340 	zfs_fuid_info_t *fuidp = NULL;
2341 	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
2342 	xoptattr_t	*xoap;
2343 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2344 
2345 	if (mask == 0)
2346 		return (0);
2347 
2348 	if (mask & AT_NOSET)
2349 		return (EINVAL);
2350 
2351 	ZFS_ENTER(zfsvfs);
2352 	ZFS_VERIFY_ZP(zp);
2353 
2354 	pzp = zp->z_phys;
2355 	zilog = zfsvfs->z_log;
2356 
2357 	/*
2358 	 * Make sure that if we have ephemeral uid/gid or xvattr specified
2359 	 * that file system is at proper version level
2360 	 */
2361 
2362 	if (zfsvfs->z_use_fuids == B_FALSE &&
2363 	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2364 	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2365 	    (mask & AT_XVATTR))) {
2366 		ZFS_EXIT(zfsvfs);
2367 		return (EINVAL);
2368 	}
2369 
2370 	if (mask & AT_SIZE && vp->v_type == VDIR) {
2371 		ZFS_EXIT(zfsvfs);
2372 		return (EISDIR);
2373 	}
2374 
2375 	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2376 		ZFS_EXIT(zfsvfs);
2377 		return (EINVAL);
2378 	}
2379 
2380 	/*
2381 	 * If this is an xvattr_t, then get a pointer to the structure of
2382 	 * optional attributes.  If this is NULL, then we have a vattr_t.
2383 	 */
2384 	xoap = xva_getxoptattr(xvap);
2385 
2386 	/*
2387 	 * Immutable files can only alter immutable bit and atime
2388 	 */
2389 	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
2390 	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2391 	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2392 		ZFS_EXIT(zfsvfs);
2393 		return (EPERM);
2394 	}
2395 
2396 	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
2397 		ZFS_EXIT(zfsvfs);
2398 		return (EPERM);
2399 	}
2400 
2401 top:
2402 	attrzp = NULL;
2403 
2404 	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2405 		ZFS_EXIT(zfsvfs);
2406 		return (EROFS);
2407 	}
2408 
2409 	/*
2410 	 * First validate permissions
2411 	 */
2412 
2413 	if (mask & AT_SIZE) {
2414 		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2415 		if (err) {
2416 			ZFS_EXIT(zfsvfs);
2417 			return (err);
2418 		}
2419 		/*
2420 		 * XXX - Note, we are not providing any open
2421 		 * mode flags here (like FNDELAY), so we may
2422 		 * block if there are locks present... this
2423 		 * should be addressed in openat().
2424 		 */
2425 		do {
2426 			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2427 			/* NB: we already did dmu_tx_wait() if necessary */
2428 		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
2429 		if (err) {
2430 			ZFS_EXIT(zfsvfs);
2431 			return (err);
2432 		}
2433 	}
2434 
2435 	if (mask & (AT_ATIME|AT_MTIME) ||
2436 	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2437 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2438 	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2439 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2440 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
2441 		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2442 		    skipaclchk, cr);
2443 
2444 	if (mask & (AT_UID|AT_GID)) {
2445 		int	idmask = (mask & (AT_UID|AT_GID));
2446 		int	take_owner;
2447 		int	take_group;
2448 
2449 		/*
2450 		 * NOTE: even if a new mode is being set,
2451 		 * we may clear S_ISUID/S_ISGID bits.
2452 		 */
2453 
2454 		if (!(mask & AT_MODE))
2455 			vap->va_mode = pzp->zp_mode;
2456 
2457 		/*
2458 		 * Take ownership or chgrp to group we are a member of
2459 		 */
2460 
2461 		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2462 		take_group = (mask & AT_GID) &&
2463 		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
2464 
2465 		/*
2466 		 * If both AT_UID and AT_GID are set then take_owner and
2467 		 * take_group must both be set in order to allow taking
2468 		 * ownership.
2469 		 *
2470 		 * Otherwise, send the check through secpolicy_vnode_setattr()
2471 		 *
2472 		 */
2473 
2474 		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2475 		    ((idmask == AT_UID) && take_owner) ||
2476 		    ((idmask == AT_GID) && take_group)) {
2477 			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2478 			    skipaclchk, cr) == 0) {
2479 				/*
2480 				 * Remove setuid/setgid for non-privileged users
2481 				 */
2482 				secpolicy_setid_clear(vap, cr);
2483 				trim_mask = (mask & (AT_UID|AT_GID));
2484 			} else {
2485 				need_policy =  TRUE;
2486 			}
2487 		} else {
2488 			need_policy =  TRUE;
2489 		}
2490 	}
2491 
2492 	mutex_enter(&zp->z_lock);
2493 	oldva.va_mode = pzp->zp_mode;
2494 	zfs_fuid_map_ids(zp, &oldva.va_uid, &oldva.va_gid);
2495 	if (mask & AT_XVATTR) {
2496 		if ((need_policy == FALSE) &&
2497 		    (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) &&
2498 		    xoap->xoa_appendonly !=
2499 		    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) ||
2500 		    (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) &&
2501 		    xoap->xoa_nounlink !=
2502 		    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) ||
2503 		    (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) &&
2504 		    xoap->xoa_immutable !=
2505 		    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) ||
2506 		    (XVA_ISSET_REQ(xvap, XAT_NODUMP) &&
2507 		    xoap->xoa_nodump !=
2508 		    ((pzp->zp_flags & ZFS_NODUMP) != 0)) ||
2509 		    (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) &&
2510 		    xoap->xoa_av_modified !=
2511 		    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) ||
2512 		    ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) &&
2513 		    ((vp->v_type != VREG && xoap->xoa_av_quarantined) ||
2514 		    xoap->xoa_av_quarantined !=
2515 		    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) ||
2516 		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) ||
2517 		    (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2518 			need_policy = TRUE;
2519 		}
2520 	}
2521 
2522 	mutex_exit(&zp->z_lock);
2523 
2524 	if (mask & AT_MODE) {
2525 		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2526 			err = secpolicy_setid_setsticky_clear(vp, vap,
2527 			    &oldva, cr);
2528 			if (err) {
2529 				ZFS_EXIT(zfsvfs);
2530 				return (err);
2531 			}
2532 			trim_mask |= AT_MODE;
2533 		} else {
2534 			need_policy = TRUE;
2535 		}
2536 	}
2537 
2538 	if (need_policy) {
2539 		/*
2540 		 * If trim_mask is set then take ownership
2541 		 * has been granted or write_acl is present and user
2542 		 * has the ability to modify mode.  In that case remove
2543 		 * UID|GID and or MODE from mask so that
2544 		 * secpolicy_vnode_setattr() doesn't revoke it.
2545 		 */
2546 
2547 		if (trim_mask) {
2548 			saved_mask = vap->va_mask;
2549 			vap->va_mask &= ~trim_mask;
2550 		}
2551 		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2552 		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2553 		if (err) {
2554 			ZFS_EXIT(zfsvfs);
2555 			return (err);
2556 		}
2557 
2558 		if (trim_mask)
2559 			vap->va_mask |= saved_mask;
2560 	}
2561 
2562 	/*
2563 	 * secpolicy_vnode_setattr, or take ownership may have
2564 	 * changed va_mask
2565 	 */
2566 	mask = vap->va_mask;
2567 
2568 	tx = dmu_tx_create(zfsvfs->z_os);
2569 	dmu_tx_hold_bonus(tx, zp->z_id);
2570 	if (zfsvfs->z_fuid_obj == 0) {
2571 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
2572 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
2573 		    SPA_MAXBLOCKSIZE);
2574 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
2575 	} else {
2576 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
2577 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
2578 		    SPA_MAXBLOCKSIZE);
2579 	}
2580 
2581 	if (mask & AT_MODE) {
2582 		uint64_t pmode = pzp->zp_mode;
2583 
2584 		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2585 
2586 		if (pzp->zp_acl.z_acl_extern_obj) {
2587 			/* Are we upgrading ACL from old V0 format to new V1 */
2588 			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
2589 			    pzp->zp_acl.z_acl_version ==
2590 			    ZFS_ACL_VERSION_INITIAL) {
2591 				dmu_tx_hold_free(tx,
2592 				    pzp->zp_acl.z_acl_extern_obj, 0,
2593 				    DMU_OBJECT_END);
2594 				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2595 				    0, sizeof (zfs_object_ace_t) * 2048 + 6);
2596 			} else {
2597 				dmu_tx_hold_write(tx,
2598 				    pzp->zp_acl.z_acl_extern_obj, 0,
2599 				    SPA_MAXBLOCKSIZE);
2600 			}
2601 		} else {
2602 			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
2603 			    0, sizeof (zfs_object_ace_t) * 2048 + 6);
2604 		}
2605 	}
2606 
2607 	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
2608 		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
2609 		if (err) {
2610 			dmu_tx_abort(tx);
2611 			ZFS_EXIT(zfsvfs);
2612 			return (err);
2613 		}
2614 		dmu_tx_hold_bonus(tx, attrzp->z_id);
2615 	}
2616 
2617 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
2618 	if (err) {
2619 		if (attrzp)
2620 			VN_RELE(ZTOV(attrzp));
2621 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2622 			dmu_tx_wait(tx);
2623 			dmu_tx_abort(tx);
2624 			goto top;
2625 		}
2626 		dmu_tx_abort(tx);
2627 		ZFS_EXIT(zfsvfs);
2628 		return (err);
2629 	}
2630 
2631 	dmu_buf_will_dirty(zp->z_dbuf, tx);
2632 
2633 	/*
2634 	 * Set each attribute requested.
2635 	 * We group settings according to the locks they need to acquire.
2636 	 *
2637 	 * Note: you cannot set ctime directly, although it will be
2638 	 * updated as a side-effect of calling this function.
2639 	 */
2640 
2641 	mutex_enter(&zp->z_lock);
2642 
2643 	if (mask & AT_MODE) {
2644 		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
2645 		ASSERT3U(err, ==, 0);
2646 	}
2647 
2648 	if (attrzp)
2649 		mutex_enter(&attrzp->z_lock);
2650 
2651 	if (mask & AT_UID) {
2652 		pzp->zp_uid = zfs_fuid_create(zfsvfs,
2653 		    vap->va_uid, ZFS_OWNER, tx, &fuidp);
2654 		if (attrzp) {
2655 			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
2656 			    vap->va_uid,  ZFS_OWNER, tx, &fuidp);
2657 		}
2658 	}
2659 
2660 	if (mask & AT_GID) {
2661 		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
2662 		    ZFS_GROUP, tx, &fuidp);
2663 		if (attrzp)
2664 			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
2665 			    vap->va_gid, ZFS_GROUP, tx, &fuidp);
2666 	}
2667 
2668 	if (attrzp)
2669 		mutex_exit(&attrzp->z_lock);
2670 
2671 	if (mask & AT_ATIME)
2672 		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
2673 
2674 	if (mask & AT_MTIME)
2675 		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
2676 
2677 	if (mask & AT_SIZE)
2678 		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
2679 	else if (mask != 0)
2680 		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
2681 	/*
2682 	 * Do this after setting timestamps to prevent timestamp
2683 	 * update from toggling bit
2684 	 */
2685 
2686 	if (xoap && (mask & AT_XVATTR)) {
2687 		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
2688 			size_t len;
2689 			dmu_object_info_t doi;
2690 
2691 			ASSERT(vp->v_type == VREG);
2692 
2693 			/* Grow the bonus buffer if necessary. */
2694 			dmu_object_info_from_db(zp->z_dbuf, &doi);
2695 			len = sizeof (xoap->xoa_av_scanstamp) +
2696 			    sizeof (znode_phys_t);
2697 			if (len > doi.doi_bonus_size)
2698 				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
2699 		}
2700 		zfs_xvattr_set(zp, xvap);
2701 	}
2702 
2703 	if (mask != 0)
2704 		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
2705 
2706 	if (fuidp)
2707 		zfs_fuid_info_free(fuidp);
2708 	mutex_exit(&zp->z_lock);
2709 
2710 	if (attrzp)
2711 		VN_RELE(ZTOV(attrzp));
2712 
2713 	dmu_tx_commit(tx);
2714 
2715 	ZFS_EXIT(zfsvfs);
2716 	return (err);
2717 }
2718 
2719 typedef struct zfs_zlock {
2720 	krwlock_t	*zl_rwlock;	/* lock we acquired */
2721 	znode_t		*zl_znode;	/* znode we held */
2722 	struct zfs_zlock *zl_next;	/* next in list */
2723 } zfs_zlock_t;
2724 
2725 /*
2726  * Drop locks and release vnodes that were held by zfs_rename_lock().
2727  */
2728 static void
2729 zfs_rename_unlock(zfs_zlock_t **zlpp)
2730 {
2731 	zfs_zlock_t *zl;
2732 
2733 	while ((zl = *zlpp) != NULL) {
2734 		if (zl->zl_znode != NULL)
2735 			VN_RELE(ZTOV(zl->zl_znode));
2736 		rw_exit(zl->zl_rwlock);
2737 		*zlpp = zl->zl_next;
2738 		kmem_free(zl, sizeof (*zl));
2739 	}
2740 }
2741 
2742 /*
2743  * Search back through the directory tree, using the ".." entries.
2744  * Lock each directory in the chain to prevent concurrent renames.
2745  * Fail any attempt to move a directory into one of its own descendants.
2746  * XXX - z_parent_lock can overlap with map or grow locks
2747  */
2748 static int
2749 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
2750 {
2751 	zfs_zlock_t	*zl;
2752 	znode_t		*zp = tdzp;
2753 	uint64_t	rootid = zp->z_zfsvfs->z_root;
2754 	uint64_t	*oidp = &zp->z_id;
2755 	krwlock_t	*rwlp = &szp->z_parent_lock;
2756 	krw_t		rw = RW_WRITER;
2757 
2758 	/*
2759 	 * First pass write-locks szp and compares to zp->z_id.
2760 	 * Later passes read-lock zp and compare to zp->z_parent.
2761 	 */
2762 	do {
2763 		if (!rw_tryenter(rwlp, rw)) {
2764 			/*
2765 			 * Another thread is renaming in this path.
2766 			 * Note that if we are a WRITER, we don't have any
2767 			 * parent_locks held yet.
2768 			 */
2769 			if (rw == RW_READER && zp->z_id > szp->z_id) {
2770 				/*
2771 				 * Drop our locks and restart
2772 				 */
2773 				zfs_rename_unlock(&zl);
2774 				*zlpp = NULL;
2775 				zp = tdzp;
2776 				oidp = &zp->z_id;
2777 				rwlp = &szp->z_parent_lock;
2778 				rw = RW_WRITER;
2779 				continue;
2780 			} else {
2781 				/*
2782 				 * Wait for other thread to drop its locks
2783 				 */
2784 				rw_enter(rwlp, rw);
2785 			}
2786 		}
2787 
2788 		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
2789 		zl->zl_rwlock = rwlp;
2790 		zl->zl_znode = NULL;
2791 		zl->zl_next = *zlpp;
2792 		*zlpp = zl;
2793 
2794 		if (*oidp == szp->z_id)		/* We're a descendant of szp */
2795 			return (EINVAL);
2796 
2797 		if (*oidp == rootid)		/* We've hit the top */
2798 			return (0);
2799 
2800 		if (rw == RW_READER) {		/* i.e. not the first pass */
2801 			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
2802 			if (error)
2803 				return (error);
2804 			zl->zl_znode = zp;
2805 		}
2806 		oidp = &zp->z_phys->zp_parent;
2807 		rwlp = &zp->z_parent_lock;
2808 		rw = RW_READER;
2809 
2810 	} while (zp->z_id != sdzp->z_id);
2811 
2812 	return (0);
2813 }
2814 
2815 /*
2816  * Move an entry from the provided source directory to the target
2817  * directory.  Change the entry name as indicated.
2818  *
2819  *	IN:	sdvp	- Source directory containing the "old entry".
2820  *		snm	- Old entry name.
2821  *		tdvp	- Target directory to contain the "new entry".
2822  *		tnm	- New entry name.
2823  *		cr	- credentials of caller.
2824  *		ct	- caller context
2825  *		flags	- case flags
2826  *
2827  *	RETURN:	0 if success
2828  *		error code if failure
2829  *
2830  * Timestamps:
2831  *	sdvp,tdvp - ctime|mtime updated
2832  */
2833 /*ARGSUSED*/
2834 static int
2835 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
2836     caller_context_t *ct, int flags)
2837 {
2838 	znode_t		*tdzp, *szp, *tzp;
2839 	znode_t		*sdzp = VTOZ(sdvp);
2840 	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
2841 	zilog_t		*zilog;
2842 	vnode_t		*realvp;
2843 	zfs_dirlock_t	*sdl, *tdl;
2844 	dmu_tx_t	*tx;
2845 	zfs_zlock_t	*zl;
2846 	int		cmp, serr, terr;
2847 	int		error = 0;
2848 	int		zflg = 0;
2849 
2850 	ZFS_ENTER(zfsvfs);
2851 	ZFS_VERIFY_ZP(sdzp);
2852 	zilog = zfsvfs->z_log;
2853 
2854 	/*
2855 	 * Make sure we have the real vp for the target directory.
2856 	 */
2857 	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
2858 		tdvp = realvp;
2859 
2860 	if (tdvp->v_vfsp != sdvp->v_vfsp) {
2861 		ZFS_EXIT(zfsvfs);
2862 		return (EXDEV);
2863 	}
2864 
2865 	tdzp = VTOZ(tdvp);
2866 	ZFS_VERIFY_ZP(tdzp);
2867 	if (zfsvfs->z_utf8 && u8_validate(tnm,
2868 	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
2869 		ZFS_EXIT(zfsvfs);
2870 		return (EILSEQ);
2871 	}
2872 
2873 	if (flags & FIGNORECASE)
2874 		zflg |= ZCILOOK;
2875 
2876 top:
2877 	szp = NULL;
2878 	tzp = NULL;
2879 	zl = NULL;
2880 
2881 	/*
2882 	 * This is to prevent the creation of links into attribute space
2883 	 * by renaming a linked file into/outof an attribute directory.
2884 	 * See the comment in zfs_link() for why this is considered bad.
2885 	 */
2886 	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
2887 	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
2888 		ZFS_EXIT(zfsvfs);
2889 		return (EINVAL);
2890 	}
2891 
2892 	/*
2893 	 * Lock source and target directory entries.  To prevent deadlock,
2894 	 * a lock ordering must be defined.  We lock the directory with
2895 	 * the smallest object id first, or if it's a tie, the one with
2896 	 * the lexically first name.
2897 	 */
2898 	if (sdzp->z_id < tdzp->z_id) {
2899 		cmp = -1;
2900 	} else if (sdzp->z_id > tdzp->z_id) {
2901 		cmp = 1;
2902 	} else {
2903 		/*
2904 		 * First compare the two name arguments without
2905 		 * considering any case folding.
2906 		 */
2907 		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
2908 
2909 		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
2910 		ASSERT(error == 0 || !zfsvfs->z_utf8);
2911 		if (cmp == 0) {
2912 			/*
2913 			 * POSIX: "If the old argument and the new argument
2914 			 * both refer to links to the same existing file,
2915 			 * the rename() function shall return successfully
2916 			 * and perform no other action."
2917 			 */
2918 			ZFS_EXIT(zfsvfs);
2919 			return (0);
2920 		}
2921 		/*
2922 		 * If the file system is case-folding, then we may
2923 		 * have some more checking to do.  A case-folding file
2924 		 * system is either supporting mixed case sensitivity
2925 		 * access or is completely case-insensitive.  Note
2926 		 * that the file system is always case preserving.
2927 		 *
2928 		 * In mixed sensitivity mode case sensitive behavior
2929 		 * is the default.  FIGNORECASE must be used to
2930 		 * explicitly request case insensitive behavior.
2931 		 *
2932 		 * If the source and target names provided differ only
2933 		 * by case (e.g., a request to rename 'tim' to 'Tim'),
2934 		 * we will treat this as a special case in the
2935 		 * case-insensitive mode: as long as the source name
2936 		 * is an exact match, we will allow this to proceed as
2937 		 * a name-change request.
2938 		 */
2939 		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
2940 		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
2941 		    flags & FIGNORECASE)) &&
2942 		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
2943 		    &error) == 0) {
2944 			/*
2945 			 * case preserving rename request, require exact
2946 			 * name matches
2947 			 */
2948 			zflg |= ZCIEXACT;
2949 			zflg &= ~ZCILOOK;
2950 		}
2951 	}
2952 
2953 	if (cmp < 0) {
2954 		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
2955 		    ZEXISTS | zflg, NULL, NULL);
2956 		terr = zfs_dirent_lock(&tdl,
2957 		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
2958 	} else {
2959 		terr = zfs_dirent_lock(&tdl,
2960 		    tdzp, tnm, &tzp, zflg, NULL, NULL);
2961 		serr = zfs_dirent_lock(&sdl,
2962 		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
2963 		    NULL, NULL);
2964 	}
2965 
2966 	if (serr) {
2967 		/*
2968 		 * Source entry invalid or not there.
2969 		 */
2970 		if (!terr) {
2971 			zfs_dirent_unlock(tdl);
2972 			if (tzp)
2973 				VN_RELE(ZTOV(tzp));
2974 		}
2975 		if (strcmp(snm, "..") == 0)
2976 			serr = EINVAL;
2977 		ZFS_EXIT(zfsvfs);
2978 		return (serr);
2979 	}
2980 	if (terr) {
2981 		zfs_dirent_unlock(sdl);
2982 		VN_RELE(ZTOV(szp));
2983 		if (strcmp(tnm, "..") == 0)
2984 			terr = EINVAL;
2985 		ZFS_EXIT(zfsvfs);
2986 		return (terr);
2987 	}
2988 
2989 	/*
2990 	 * Must have write access at the source to remove the old entry
2991 	 * and write access at the target to create the new entry.
2992 	 * Note that if target and source are the same, this can be
2993 	 * done in a single check.
2994 	 */
2995 
2996 	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
2997 		goto out;
2998 
2999 	if (ZTOV(szp)->v_type == VDIR) {
3000 		/*
3001 		 * Check to make sure rename is valid.
3002 		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3003 		 */
3004 		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3005 			goto out;
3006 	}
3007 
3008 	/*
3009 	 * Does target exist?
3010 	 */
3011 	if (tzp) {
3012 		/*
3013 		 * Source and target must be the same type.
3014 		 */
3015 		if (ZTOV(szp)->v_type == VDIR) {
3016 			if (ZTOV(tzp)->v_type != VDIR) {
3017 				error = ENOTDIR;
3018 				goto out;
3019 			}
3020 		} else {
3021 			if (ZTOV(tzp)->v_type == VDIR) {
3022 				error = EISDIR;
3023 				goto out;
3024 			}
3025 		}
3026 		/*
3027 		 * POSIX dictates that when the source and target
3028 		 * entries refer to the same file object, rename
3029 		 * must do nothing and exit without error.
3030 		 */
3031 		if (szp->z_id == tzp->z_id) {
3032 			error = 0;
3033 			goto out;
3034 		}
3035 	}
3036 
3037 	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3038 	if (tzp)
3039 		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3040 
3041 	/*
3042 	 * notify the target directory if it is not the same
3043 	 * as source directory.
3044 	 */
3045 	if (tdvp != sdvp) {
3046 		vnevent_rename_dest_dir(tdvp, ct);
3047 	}
3048 
3049 	tx = dmu_tx_create(zfsvfs->z_os);
3050 	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
3051 	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
3052 	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3053 	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3054 	if (sdzp != tdzp)
3055 		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
3056 	if (tzp)
3057 		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
3058 	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3059 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3060 	if (error) {
3061 		if (zl != NULL)
3062 			zfs_rename_unlock(&zl);
3063 		zfs_dirent_unlock(sdl);
3064 		zfs_dirent_unlock(tdl);
3065 		VN_RELE(ZTOV(szp));
3066 		if (tzp)
3067 			VN_RELE(ZTOV(tzp));
3068 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3069 			dmu_tx_wait(tx);
3070 			dmu_tx_abort(tx);
3071 			goto top;
3072 		}
3073 		dmu_tx_abort(tx);
3074 		ZFS_EXIT(zfsvfs);
3075 		return (error);
3076 	}
3077 
3078 	if (tzp)	/* Attempt to remove the existing target */
3079 		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3080 
3081 	if (error == 0) {
3082 		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3083 		if (error == 0) {
3084 			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3085 
3086 			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3087 			ASSERT(error == 0);
3088 
3089 			zfs_log_rename(zilog, tx,
3090 			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3091 			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
3092 		}
3093 	}
3094 
3095 	dmu_tx_commit(tx);
3096 out:
3097 	if (zl != NULL)
3098 		zfs_rename_unlock(&zl);
3099 
3100 	zfs_dirent_unlock(sdl);
3101 	zfs_dirent_unlock(tdl);
3102 
3103 	VN_RELE(ZTOV(szp));
3104 	if (tzp)
3105 		VN_RELE(ZTOV(tzp));
3106 
3107 	ZFS_EXIT(zfsvfs);
3108 	return (error);
3109 }
3110 
3111 /*
3112  * Insert the indicated symbolic reference entry into the directory.
3113  *
3114  *	IN:	dvp	- Directory to contain new symbolic link.
3115  *		link	- Name for new symlink entry.
3116  *		vap	- Attributes of new entry.
3117  *		target	- Target path of new symlink.
3118  *		cr	- credentials of caller.
3119  *		ct	- caller context
3120  *		flags	- case flags
3121  *
3122  *	RETURN:	0 if success
3123  *		error code if failure
3124  *
3125  * Timestamps:
3126  *	dvp - ctime|mtime updated
3127  */
3128 /*ARGSUSED*/
3129 static int
3130 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3131     caller_context_t *ct, int flags)
3132 {
3133 	znode_t		*zp, *dzp = VTOZ(dvp);
3134 	zfs_dirlock_t	*dl;
3135 	dmu_tx_t	*tx;
3136 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3137 	zilog_t		*zilog;
3138 	int		len = strlen(link);
3139 	int		error;
3140 	int		zflg = ZNEW;
3141 	zfs_fuid_info_t *fuidp = NULL;
3142 
3143 	ASSERT(vap->va_type == VLNK);
3144 
3145 	ZFS_ENTER(zfsvfs);
3146 	ZFS_VERIFY_ZP(dzp);
3147 	zilog = zfsvfs->z_log;
3148 
3149 	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3150 	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3151 		ZFS_EXIT(zfsvfs);
3152 		return (EILSEQ);
3153 	}
3154 	if (flags & FIGNORECASE)
3155 		zflg |= ZCILOOK;
3156 top:
3157 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3158 		ZFS_EXIT(zfsvfs);
3159 		return (error);
3160 	}
3161 
3162 	if (len > MAXPATHLEN) {
3163 		ZFS_EXIT(zfsvfs);
3164 		return (ENAMETOOLONG);
3165 	}
3166 
3167 	/*
3168 	 * Attempt to lock directory; fail if entry already exists.
3169 	 */
3170 	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3171 	if (error) {
3172 		ZFS_EXIT(zfsvfs);
3173 		return (error);
3174 	}
3175 
3176 	tx = dmu_tx_create(zfsvfs->z_os);
3177 	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3178 	dmu_tx_hold_bonus(tx, dzp->z_id);
3179 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3180 	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
3181 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3182 	if (zfsvfs->z_fuid_obj == 0) {
3183 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
3184 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3185 		    SPA_MAXBLOCKSIZE);
3186 		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
3187 	} else {
3188 		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
3189 		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
3190 		    SPA_MAXBLOCKSIZE);
3191 	}
3192 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3193 	if (error) {
3194 		zfs_dirent_unlock(dl);
3195 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3196 			dmu_tx_wait(tx);
3197 			dmu_tx_abort(tx);
3198 			goto top;
3199 		}
3200 		dmu_tx_abort(tx);
3201 		ZFS_EXIT(zfsvfs);
3202 		return (error);
3203 	}
3204 
3205 	dmu_buf_will_dirty(dzp->z_dbuf, tx);
3206 
3207 	/*
3208 	 * Create a new object for the symlink.
3209 	 * Put the link content into bonus buffer if it will fit;
3210 	 * otherwise, store it just like any other file data.
3211 	 */
3212 	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
3213 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
3214 		if (len != 0)
3215 			bcopy(link, zp->z_phys + 1, len);
3216 	} else {
3217 		dmu_buf_t *dbp;
3218 
3219 		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
3220 		/*
3221 		 * Nothing can access the znode yet so no locking needed
3222 		 * for growing the znode's blocksize.
3223 		 */
3224 		zfs_grow_blocksize(zp, len, tx);
3225 
3226 		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
3227 		    zp->z_id, 0, FTAG, &dbp));
3228 		dmu_buf_will_dirty(dbp, tx);
3229 
3230 		ASSERT3U(len, <=, dbp->db_size);
3231 		bcopy(link, dbp->db_data, len);
3232 		dmu_buf_rele(dbp, FTAG);
3233 	}
3234 	zp->z_phys->zp_size = len;
3235 
3236 	/*
3237 	 * Insert the new object into the directory.
3238 	 */
3239 	(void) zfs_link_create(dl, zp, tx, ZNEW);
3240 out:
3241 	if (error == 0) {
3242 		uint64_t txtype = TX_SYMLINK;
3243 		if (flags & FIGNORECASE)
3244 			txtype |= TX_CI;
3245 		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3246 	}
3247 	if (fuidp)
3248 		zfs_fuid_info_free(fuidp);
3249 
3250 	dmu_tx_commit(tx);
3251 
3252 	zfs_dirent_unlock(dl);
3253 
3254 	VN_RELE(ZTOV(zp));
3255 
3256 	ZFS_EXIT(zfsvfs);
3257 	return (error);
3258 }
3259 
3260 /*
3261  * Return, in the buffer contained in the provided uio structure,
3262  * the symbolic path referred to by vp.
3263  *
3264  *	IN:	vp	- vnode of symbolic link.
3265  *		uoip	- structure to contain the link path.
3266  *		cr	- credentials of caller.
3267  *		ct	- caller context
3268  *
3269  *	OUT:	uio	- structure to contain the link path.
3270  *
3271  *	RETURN:	0 if success
3272  *		error code if failure
3273  *
3274  * Timestamps:
3275  *	vp - atime updated
3276  */
3277 /* ARGSUSED */
3278 static int
3279 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3280 {
3281 	znode_t		*zp = VTOZ(vp);
3282 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3283 	size_t		bufsz;
3284 	int		error;
3285 
3286 	ZFS_ENTER(zfsvfs);
3287 	ZFS_VERIFY_ZP(zp);
3288 
3289 	bufsz = (size_t)zp->z_phys->zp_size;
3290 	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
3291 		error = uiomove(zp->z_phys + 1,
3292 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3293 	} else {
3294 		dmu_buf_t *dbp;
3295 		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
3296 		if (error) {
3297 			ZFS_EXIT(zfsvfs);
3298 			return (error);
3299 		}
3300 		error = uiomove(dbp->db_data,
3301 		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
3302 		dmu_buf_rele(dbp, FTAG);
3303 	}
3304 
3305 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3306 	ZFS_EXIT(zfsvfs);
3307 	return (error);
3308 }
3309 
3310 /*
3311  * Insert a new entry into directory tdvp referencing svp.
3312  *
3313  *	IN:	tdvp	- Directory to contain new entry.
3314  *		svp	- vnode of new entry.
3315  *		name	- name of new entry.
3316  *		cr	- credentials of caller.
3317  *		ct	- caller context
3318  *
3319  *	RETURN:	0 if success
3320  *		error code if failure
3321  *
3322  * Timestamps:
3323  *	tdvp - ctime|mtime updated
3324  *	 svp - ctime updated
3325  */
3326 /* ARGSUSED */
3327 static int
3328 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3329     caller_context_t *ct, int flags)
3330 {
3331 	znode_t		*dzp = VTOZ(tdvp);
3332 	znode_t		*tzp, *szp;
3333 	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
3334 	zilog_t		*zilog;
3335 	zfs_dirlock_t	*dl;
3336 	dmu_tx_t	*tx;
3337 	vnode_t		*realvp;
3338 	int		error;
3339 	int		zf = ZNEW;
3340 	uid_t		owner;
3341 
3342 	ASSERT(tdvp->v_type == VDIR);
3343 
3344 	ZFS_ENTER(zfsvfs);
3345 	ZFS_VERIFY_ZP(dzp);
3346 	zilog = zfsvfs->z_log;
3347 
3348 	if (VOP_REALVP(svp, &realvp, ct) == 0)
3349 		svp = realvp;
3350 
3351 	if (svp->v_vfsp != tdvp->v_vfsp) {
3352 		ZFS_EXIT(zfsvfs);
3353 		return (EXDEV);
3354 	}
3355 	szp = VTOZ(svp);
3356 	ZFS_VERIFY_ZP(szp);
3357 
3358 	if (zfsvfs->z_utf8 && u8_validate(name,
3359 	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3360 		ZFS_EXIT(zfsvfs);
3361 		return (EILSEQ);
3362 	}
3363 	if (flags & FIGNORECASE)
3364 		zf |= ZCILOOK;
3365 
3366 top:
3367 	/*
3368 	 * We do not support links between attributes and non-attributes
3369 	 * because of the potential security risk of creating links
3370 	 * into "normal" file space in order to circumvent restrictions
3371 	 * imposed in attribute space.
3372 	 */
3373 	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
3374 	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
3375 		ZFS_EXIT(zfsvfs);
3376 		return (EINVAL);
3377 	}
3378 
3379 	/*
3380 	 * POSIX dictates that we return EPERM here.
3381 	 * Better choices include ENOTSUP or EISDIR.
3382 	 */
3383 	if (svp->v_type == VDIR) {
3384 		ZFS_EXIT(zfsvfs);
3385 		return (EPERM);
3386 	}
3387 
3388 	zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, ZFS_OWNER, &owner);
3389 	if (owner != crgetuid(cr) &&
3390 	    secpolicy_basic_link(cr) != 0) {
3391 		ZFS_EXIT(zfsvfs);
3392 		return (EPERM);
3393 	}
3394 
3395 	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3396 		ZFS_EXIT(zfsvfs);
3397 		return (error);
3398 	}
3399 
3400 	/*
3401 	 * Attempt to lock directory; fail if entry already exists.
3402 	 */
3403 	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3404 	if (error) {
3405 		ZFS_EXIT(zfsvfs);
3406 		return (error);
3407 	}
3408 
3409 	tx = dmu_tx_create(zfsvfs->z_os);
3410 	dmu_tx_hold_bonus(tx, szp->z_id);
3411 	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3412 	error = dmu_tx_assign(tx, zfsvfs->z_assign);
3413 	if (error) {
3414 		zfs_dirent_unlock(dl);
3415 		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3416 			dmu_tx_wait(tx);
3417 			dmu_tx_abort(tx);
3418 			goto top;
3419 		}
3420 		dmu_tx_abort(tx);
3421 		ZFS_EXIT(zfsvfs);
3422 		return (error);
3423 	}
3424 
3425 	error = zfs_link_create(dl, szp, tx, 0);
3426 
3427 	if (error == 0) {
3428 		uint64_t txtype = TX_LINK;
3429 		if (flags & FIGNORECASE)
3430 			txtype |= TX_CI;
3431 		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3432 	}
3433 
3434 	dmu_tx_commit(tx);
3435 
3436 	zfs_dirent_unlock(dl);
3437 
3438 	if (error == 0) {
3439 		vnevent_link(svp, ct);
3440 	}
3441 
3442 	ZFS_EXIT(zfsvfs);
3443 	return (error);
3444 }
3445 
3446 /*
3447  * zfs_null_putapage() is used when the file system has been force
3448  * unmounted. It just drops the pages.
3449  */
3450 /* ARGSUSED */
3451 static int
3452 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3453 		size_t *lenp, int flags, cred_t *cr)
3454 {
3455 	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
3456 	return (0);
3457 }
3458 
3459 /*
3460  * Push a page out to disk, klustering if possible.
3461  *
3462  *	IN:	vp	- file to push page to.
3463  *		pp	- page to push.
3464  *		flags	- additional flags.
3465  *		cr	- credentials of caller.
3466  *
3467  *	OUT:	offp	- start of range pushed.
3468  *		lenp	- len of range pushed.
3469  *
3470  *	RETURN:	0 if success
3471  *		error code if failure
3472  *
3473  * NOTE: callers must have locked the page to be pushed.  On
3474  * exit, the page (and all other pages in the kluster) must be
3475  * unlocked.
3476  */
3477 /* ARGSUSED */
3478 static int
3479 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
3480 		size_t *lenp, int flags, cred_t *cr)
3481 {
3482 	znode_t		*zp = VTOZ(vp);
3483 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3484 	zilog_t		*zilog = zfsvfs->z_log;
3485 	dmu_tx_t	*tx;
3486 	rl_t		*rl;
3487 	u_offset_t	off, koff;
3488 	size_t		len, klen;
3489 	uint64_t	filesz;
3490 	int		err;
3491 
3492 	filesz = zp->z_phys->zp_size;
3493 	off = pp->p_offset;
3494 	len = PAGESIZE;
3495 	/*
3496 	 * If our blocksize is bigger than the page size, try to kluster
3497 	 * muiltiple pages so that we write a full block (thus avoiding
3498 	 * a read-modify-write).
3499 	 */
3500 	if (off < filesz && zp->z_blksz > PAGESIZE) {
3501 		if (!ISP2(zp->z_blksz)) {
3502 			/* Only one block in the file. */
3503 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3504 			koff = 0;
3505 		} else {
3506 			klen = zp->z_blksz;
3507 			koff = P2ALIGN(off, (u_offset_t)klen);
3508 		}
3509 		ASSERT(koff <= filesz);
3510 		if (koff + klen > filesz)
3511 			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
3512 		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
3513 	}
3514 	ASSERT3U(btop(len), ==, btopr(len));
3515 top:
3516 	rl = zfs_range_lock(zp, off, len, RL_WRITER);
3517 	/*
3518 	 * Can't push pages past end-of-file.
3519 	 */
3520 	filesz = zp->z_phys->zp_size;
3521 	if (off >= filesz) {
3522 		/* ignore all pages */
3523 		err = 0;
3524 		goto out;
3525 	} else if (off + len > filesz) {
3526 		int npages = btopr(filesz - off);
3527 		page_t *trunc;
3528 
3529 		page_list_break(&pp, &trunc, npages);
3530 		/* ignore pages past end of file */
3531 		if (trunc)
3532 			pvn_write_done(trunc, flags);
3533 		len = filesz - off;
3534 	}
3535 
3536 	tx = dmu_tx_create(zfsvfs->z_os);
3537 	dmu_tx_hold_write(tx, zp->z_id, off, len);
3538 	dmu_tx_hold_bonus(tx, zp->z_id);
3539 	err = dmu_tx_assign(tx, zfsvfs->z_assign);
3540 	if (err != 0) {
3541 		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
3542 			zfs_range_unlock(rl);
3543 			dmu_tx_wait(tx);
3544 			dmu_tx_abort(tx);
3545 			err = 0;
3546 			goto top;
3547 		}
3548 		dmu_tx_abort(tx);
3549 		goto out;
3550 	}
3551 
3552 	if (zp->z_blksz <= PAGESIZE) {
3553 		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
3554 		ASSERT3U(len, <=, PAGESIZE);
3555 		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
3556 		ppmapout(va);
3557 	} else {
3558 		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
3559 	}
3560 
3561 	if (err == 0) {
3562 		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
3563 		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
3564 		dmu_tx_commit(tx);
3565 	}
3566 
3567 out:
3568 	zfs_range_unlock(rl);
3569 	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
3570 	if (offp)
3571 		*offp = off;
3572 	if (lenp)
3573 		*lenp = len;
3574 
3575 	return (err);
3576 }
3577 
3578 /*
3579  * Copy the portion of the file indicated from pages into the file.
3580  * The pages are stored in a page list attached to the files vnode.
3581  *
3582  *	IN:	vp	- vnode of file to push page data to.
3583  *		off	- position in file to put data.
3584  *		len	- amount of data to write.
3585  *		flags	- flags to control the operation.
3586  *		cr	- credentials of caller.
3587  *		ct	- caller context.
3588  *
3589  *	RETURN:	0 if success
3590  *		error code if failure
3591  *
3592  * Timestamps:
3593  *	vp - ctime|mtime updated
3594  */
3595 /*ARGSUSED*/
3596 static int
3597 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
3598     caller_context_t *ct)
3599 {
3600 	znode_t		*zp = VTOZ(vp);
3601 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3602 	page_t		*pp;
3603 	size_t		io_len;
3604 	u_offset_t	io_off;
3605 	uint64_t	filesz;
3606 	int		error = 0;
3607 
3608 	ZFS_ENTER(zfsvfs);
3609 	ZFS_VERIFY_ZP(zp);
3610 
3611 	if (len == 0) {
3612 		/*
3613 		 * Search the entire vp list for pages >= off.
3614 		 */
3615 		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
3616 		    flags, cr);
3617 		goto out;
3618 	}
3619 
3620 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3621 	if (off > filesz) {
3622 		/* past end of file */
3623 		ZFS_EXIT(zfsvfs);
3624 		return (0);
3625 	}
3626 
3627 	len = MIN(len, filesz - off);
3628 
3629 	for (io_off = off; io_off < off + len; io_off += io_len) {
3630 		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
3631 			pp = page_lookup(vp, io_off,
3632 			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
3633 		} else {
3634 			pp = page_lookup_nowait(vp, io_off,
3635 			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
3636 		}
3637 
3638 		if (pp != NULL && pvn_getdirty(pp, flags)) {
3639 			int err;
3640 
3641 			/*
3642 			 * Found a dirty page to push
3643 			 */
3644 			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
3645 			if (err)
3646 				error = err;
3647 		} else {
3648 			io_len = PAGESIZE;
3649 		}
3650 	}
3651 out:
3652 	if ((flags & B_ASYNC) == 0)
3653 		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
3654 	ZFS_EXIT(zfsvfs);
3655 	return (error);
3656 }
3657 
3658 /*ARGSUSED*/
3659 void
3660 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
3661 {
3662 	znode_t	*zp = VTOZ(vp);
3663 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3664 	int error;
3665 
3666 	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
3667 	if (zp->z_dbuf == NULL) {
3668 		/*
3669 		 * The fs has been unmounted, or we did a
3670 		 * suspend/resume and this file no longer exists.
3671 		 */
3672 		if (vn_has_cached_data(vp)) {
3673 			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
3674 			    B_INVAL, cr);
3675 		}
3676 
3677 		mutex_enter(&zp->z_lock);
3678 		vp->v_count = 0; /* count arrives as 1 */
3679 		mutex_exit(&zp->z_lock);
3680 		rw_exit(&zfsvfs->z_teardown_inactive_lock);
3681 		zfs_znode_free(zp);
3682 		return;
3683 	}
3684 
3685 	/*
3686 	 * Attempt to push any data in the page cache.  If this fails
3687 	 * we will get kicked out later in zfs_zinactive().
3688 	 */
3689 	if (vn_has_cached_data(vp)) {
3690 		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
3691 		    cr);
3692 	}
3693 
3694 	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
3695 		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
3696 
3697 		dmu_tx_hold_bonus(tx, zp->z_id);
3698 		error = dmu_tx_assign(tx, TXG_WAIT);
3699 		if (error) {
3700 			dmu_tx_abort(tx);
3701 		} else {
3702 			dmu_buf_will_dirty(zp->z_dbuf, tx);
3703 			mutex_enter(&zp->z_lock);
3704 			zp->z_atime_dirty = 0;
3705 			mutex_exit(&zp->z_lock);
3706 			dmu_tx_commit(tx);
3707 		}
3708 	}
3709 
3710 	zfs_zinactive(zp);
3711 	rw_exit(&zfsvfs->z_teardown_inactive_lock);
3712 }
3713 
3714 /*
3715  * Bounds-check the seek operation.
3716  *
3717  *	IN:	vp	- vnode seeking within
3718  *		ooff	- old file offset
3719  *		noffp	- pointer to new file offset
3720  *		ct	- caller context
3721  *
3722  *	RETURN:	0 if success
3723  *		EINVAL if new offset invalid
3724  */
3725 /* ARGSUSED */
3726 static int
3727 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
3728     caller_context_t *ct)
3729 {
3730 	if (vp->v_type == VDIR)
3731 		return (0);
3732 	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
3733 }
3734 
3735 /*
3736  * Pre-filter the generic locking function to trap attempts to place
3737  * a mandatory lock on a memory mapped file.
3738  */
3739 static int
3740 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
3741     flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
3742 {
3743 	znode_t *zp = VTOZ(vp);
3744 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3745 	int error;
3746 
3747 	ZFS_ENTER(zfsvfs);
3748 	ZFS_VERIFY_ZP(zp);
3749 
3750 	/*
3751 	 * We are following the UFS semantics with respect to mapcnt
3752 	 * here: If we see that the file is mapped already, then we will
3753 	 * return an error, but we don't worry about races between this
3754 	 * function and zfs_map().
3755 	 */
3756 	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
3757 		ZFS_EXIT(zfsvfs);
3758 		return (EAGAIN);
3759 	}
3760 	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
3761 	ZFS_EXIT(zfsvfs);
3762 	return (error);
3763 }
3764 
3765 /*
3766  * If we can't find a page in the cache, we will create a new page
3767  * and fill it with file data.  For efficiency, we may try to fill
3768  * multiple pages at once (klustering).
3769  */
3770 static int
3771 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
3772     caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
3773 {
3774 	znode_t *zp = VTOZ(vp);
3775 	page_t *pp, *cur_pp;
3776 	objset_t *os = zp->z_zfsvfs->z_os;
3777 	caddr_t va;
3778 	u_offset_t io_off, total;
3779 	uint64_t oid = zp->z_id;
3780 	size_t io_len;
3781 	uint64_t filesz;
3782 	int err;
3783 
3784 	/*
3785 	 * If we are only asking for a single page don't bother klustering.
3786 	 */
3787 	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3788 	if (off >= filesz)
3789 		return (EFAULT);
3790 	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
3791 		io_off = off;
3792 		io_len = PAGESIZE;
3793 		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
3794 	} else {
3795 		/*
3796 		 * Try to fill a kluster of pages (a blocks worth).
3797 		 */
3798 		size_t klen;
3799 		u_offset_t koff;
3800 
3801 		if (!ISP2(zp->z_blksz)) {
3802 			/* Only one block in the file. */
3803 			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
3804 			koff = 0;
3805 		} else {
3806 			/*
3807 			 * It would be ideal to align our offset to the
3808 			 * blocksize but doing so has resulted in some
3809 			 * strange application crashes. For now, we
3810 			 * leave the offset as is and only adjust the
3811 			 * length if we are off the end of the file.
3812 			 */
3813 			koff = off;
3814 			klen = plsz;
3815 		}
3816 		ASSERT(koff <= filesz);
3817 		if (koff + klen > filesz)
3818 			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
3819 		ASSERT3U(off, >=, koff);
3820 		ASSERT3U(off, <, koff + klen);
3821 		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3822 		    &io_len, koff, klen, 0);
3823 	}
3824 	if (pp == NULL) {
3825 		/*
3826 		 * Some other thread entered the page before us.
3827 		 * Return to zfs_getpage to retry the lookup.
3828 		 */
3829 		*pl = NULL;
3830 		return (0);
3831 	}
3832 
3833 	/*
3834 	 * Fill the pages in the kluster.
3835 	 */
3836 	cur_pp = pp;
3837 	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
3838 		ASSERT3U(io_off, ==, cur_pp->p_offset);
3839 		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
3840 		err = dmu_read(os, oid, io_off, PAGESIZE, va);
3841 		ppmapout(va);
3842 		if (err) {
3843 			/* On error, toss the entire kluster */
3844 			pvn_read_done(pp, B_ERROR);
3845 			return (err);
3846 		}
3847 		cur_pp = cur_pp->p_next;
3848 	}
3849 out:
3850 	/*
3851 	 * Fill in the page list array from the kluster.  If
3852 	 * there are too many pages in the kluster, return
3853 	 * as many pages as possible starting from the desired
3854 	 * offset `off'.
3855 	 * NOTE: the page list will always be null terminated.
3856 	 */
3857 	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3858 
3859 	return (0);
3860 }
3861 
3862 /*
3863  * Return pointers to the pages for the file region [off, off + len]
3864  * in the pl array.  If plsz is greater than len, this function may
3865  * also return page pointers from before or after the specified
3866  * region (i.e. some region [off', off' + plsz]).  These additional
3867  * pages are only returned if they are already in the cache, or were
3868  * created as part of a klustered read.
3869  *
3870  *	IN:	vp	- vnode of file to get data from.
3871  *		off	- position in file to get data from.
3872  *		len	- amount of data to retrieve.
3873  *		plsz	- length of provided page list.
3874  *		seg	- segment to obtain pages for.
3875  *		addr	- virtual address of fault.
3876  *		rw	- mode of created pages.
3877  *		cr	- credentials of caller.
3878  *		ct	- caller context.
3879  *
3880  *	OUT:	protp	- protection mode of created pages.
3881  *		pl	- list of pages created.
3882  *
3883  *	RETURN:	0 if success
3884  *		error code if failure
3885  *
3886  * Timestamps:
3887  *	vp - atime updated
3888  */
3889 /* ARGSUSED */
3890 static int
3891 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3892 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3893 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3894 {
3895 	znode_t		*zp = VTOZ(vp);
3896 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
3897 	page_t		*pp, **pl0 = pl;
3898 	int		need_unlock = 0, err = 0;
3899 	offset_t	orig_off;
3900 
3901 	ZFS_ENTER(zfsvfs);
3902 	ZFS_VERIFY_ZP(zp);
3903 
3904 	if (protp)
3905 		*protp = PROT_ALL;
3906 
3907 	/* no faultahead (for now) */
3908 	if (pl == NULL) {
3909 		ZFS_EXIT(zfsvfs);
3910 		return (0);
3911 	}
3912 
3913 	/* can't fault past EOF */
3914 	if (off >= zp->z_phys->zp_size) {
3915 		ZFS_EXIT(zfsvfs);
3916 		return (EFAULT);
3917 	}
3918 	orig_off = off;
3919 
3920 	/*
3921 	 * If we already own the lock, then we must be page faulting
3922 	 * in the middle of a write to this file (i.e., we are writing
3923 	 * to this file using data from a mapped region of the file).
3924 	 */
3925 	if (rw_owner(&zp->z_map_lock) != curthread) {
3926 		rw_enter(&zp->z_map_lock, RW_WRITER);
3927 		need_unlock = TRUE;
3928 	}
3929 
3930 	/*
3931 	 * Loop through the requested range [off, off + len] looking
3932 	 * for pages.  If we don't find a page, we will need to create
3933 	 * a new page and fill it with data from the file.
3934 	 */
3935 	while (len > 0) {
3936 		if (plsz < PAGESIZE)
3937 			break;
3938 		if (pp = page_lookup(vp, off, SE_SHARED)) {
3939 			*pl++ = pp;
3940 			off += PAGESIZE;
3941 			addr += PAGESIZE;
3942 			len -= PAGESIZE;
3943 			plsz -= PAGESIZE;
3944 		} else {
3945 			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
3946 			if (err)
3947 				goto out;
3948 			/*
3949 			 * klustering may have changed our region
3950 			 * to be block aligned.
3951 			 */
3952 			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
3953 				int delta = off - pp->p_offset;
3954 				len += delta;
3955 				off -= delta;
3956 				addr -= delta;
3957 			}
3958 			while (*pl) {
3959 				pl++;
3960 				off += PAGESIZE;
3961 				addr += PAGESIZE;
3962 				plsz -= PAGESIZE;
3963 				if (len > PAGESIZE)
3964 					len -= PAGESIZE;
3965 				else
3966 					len = 0;
3967 			}
3968 		}
3969 	}
3970 
3971 	/*
3972 	 * Fill out the page array with any pages already in the cache.
3973 	 */
3974 	while (plsz > 0) {
3975 		pp = page_lookup_nowait(vp, off, SE_SHARED);
3976 		if (pp == NULL)
3977 			break;
3978 		*pl++ = pp;
3979 		off += PAGESIZE;
3980 		plsz -= PAGESIZE;
3981 	}
3982 
3983 	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3984 out:
3985 	/*
3986 	 * We can't grab the range lock for the page as reader which would
3987 	 * stop truncation as this leads to deadlock. So we need to recheck
3988 	 * the file size.
3989 	 */
3990 	if (orig_off >= zp->z_phys->zp_size)
3991 		err = EFAULT;
3992 	if (err) {
3993 		/*
3994 		 * Release any pages we have previously locked.
3995 		 */
3996 		while (pl > pl0)
3997 			page_unlock(*--pl);
3998 	}
3999 
4000 	*pl = NULL;
4001 
4002 	if (need_unlock)
4003 		rw_exit(&zp->z_map_lock);
4004 
4005 	ZFS_EXIT(zfsvfs);
4006 	return (err);
4007 }
4008 
4009 /*
4010  * Request a memory map for a section of a file.  This code interacts
4011  * with common code and the VM system as follows:
4012  *
4013  *	common code calls mmap(), which ends up in smmap_common()
4014  *
4015  *	this calls VOP_MAP(), which takes you into (say) zfs
4016  *
4017  *	zfs_map() calls as_map(), passing segvn_create() as the callback
4018  *
4019  *	segvn_create() creates the new segment and calls VOP_ADDMAP()
4020  *
4021  *	zfs_addmap() updates z_mapcnt
4022  */
4023 /*ARGSUSED*/
4024 static int
4025 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4026     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4027     caller_context_t *ct)
4028 {
4029 	znode_t *zp = VTOZ(vp);
4030 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4031 	segvn_crargs_t	vn_a;
4032 	int		error;
4033 
4034 	if ((prot & PROT_WRITE) &&
4035 	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
4036 	    ZFS_APPENDONLY)))
4037 		return (EPERM);
4038 
4039 	ZFS_ENTER(zfsvfs);
4040 	ZFS_VERIFY_ZP(zp);
4041 
4042 	if (vp->v_flag & VNOMAP) {
4043 		ZFS_EXIT(zfsvfs);
4044 		return (ENOSYS);
4045 	}
4046 
4047 	if (off < 0 || len > MAXOFFSET_T - off) {
4048 		ZFS_EXIT(zfsvfs);
4049 		return (ENXIO);
4050 	}
4051 
4052 	if (vp->v_type != VREG) {
4053 		ZFS_EXIT(zfsvfs);
4054 		return (ENODEV);
4055 	}
4056 
4057 	/*
4058 	 * If file is locked, disallow mapping.
4059 	 */
4060 	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
4061 		ZFS_EXIT(zfsvfs);
4062 		return (EAGAIN);
4063 	}
4064 
4065 	as_rangelock(as);
4066 	if ((flags & MAP_FIXED) == 0) {
4067 		map_addr(addrp, len, off, 1, flags);
4068 		if (*addrp == NULL) {
4069 			as_rangeunlock(as);
4070 			ZFS_EXIT(zfsvfs);
4071 			return (ENOMEM);
4072 		}
4073 	} else {
4074 		/*
4075 		 * User specified address - blow away any previous mappings
4076 		 */
4077 		(void) as_unmap(as, *addrp, len);
4078 	}
4079 
4080 	vn_a.vp = vp;
4081 	vn_a.offset = (u_offset_t)off;
4082 	vn_a.type = flags & MAP_TYPE;
4083 	vn_a.prot = prot;
4084 	vn_a.maxprot = maxprot;
4085 	vn_a.cred = cr;
4086 	vn_a.amp = NULL;
4087 	vn_a.flags = flags & ~MAP_TYPE;
4088 	vn_a.szc = 0;
4089 	vn_a.lgrp_mem_policy_flags = 0;
4090 
4091 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4092 
4093 	as_rangeunlock(as);
4094 	ZFS_EXIT(zfsvfs);
4095 	return (error);
4096 }
4097 
4098 /* ARGSUSED */
4099 static int
4100 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4101     size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4102     caller_context_t *ct)
4103 {
4104 	uint64_t pages = btopr(len);
4105 
4106 	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4107 	return (0);
4108 }
4109 
4110 /*
4111  * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4112  * more accurate mtime for the associated file.  Since we don't have a way of
4113  * detecting when the data was actually modified, we have to resort to
4114  * heuristics.  If an explicit msync() is done, then we mark the mtime when the
4115  * last page is pushed.  The problem occurs when the msync() call is omitted,
4116  * which by far the most common case:
4117  *
4118  * 	open()
4119  * 	mmap()
4120  * 	<modify memory>
4121  * 	munmap()
4122  * 	close()
4123  * 	<time lapse>
4124  * 	putpage() via fsflush
4125  *
4126  * If we wait until fsflush to come along, we can have a modification time that
4127  * is some arbitrary point in the future.  In order to prevent this in the
4128  * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4129  * torn down.
4130  */
4131 /* ARGSUSED */
4132 static int
4133 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4134     size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4135     caller_context_t *ct)
4136 {
4137 	uint64_t pages = btopr(len);
4138 
4139 	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4140 	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4141 
4142 	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4143 	    vn_has_cached_data(vp))
4144 		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4145 
4146 	return (0);
4147 }
4148 
4149 /*
4150  * Free or allocate space in a file.  Currently, this function only
4151  * supports the `F_FREESP' command.  However, this command is somewhat
4152  * misnamed, as its functionality includes the ability to allocate as
4153  * well as free space.
4154  *
4155  *	IN:	vp	- vnode of file to free data in.
4156  *		cmd	- action to take (only F_FREESP supported).
4157  *		bfp	- section of file to free/alloc.
4158  *		flag	- current file open mode flags.
4159  *		offset	- current file offset.
4160  *		cr	- credentials of caller [UNUSED].
4161  *		ct	- caller context.
4162  *
4163  *	RETURN:	0 if success
4164  *		error code if failure
4165  *
4166  * Timestamps:
4167  *	vp - ctime|mtime updated
4168  */
4169 /* ARGSUSED */
4170 static int
4171 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4172     offset_t offset, cred_t *cr, caller_context_t *ct)
4173 {
4174 	znode_t		*zp = VTOZ(vp);
4175 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4176 	uint64_t	off, len;
4177 	int		error;
4178 
4179 	ZFS_ENTER(zfsvfs);
4180 	ZFS_VERIFY_ZP(zp);
4181 
4182 top:
4183 	if (cmd != F_FREESP) {
4184 		ZFS_EXIT(zfsvfs);
4185 		return (EINVAL);
4186 	}
4187 
4188 	if (error = convoff(vp, bfp, 0, offset)) {
4189 		ZFS_EXIT(zfsvfs);
4190 		return (error);
4191 	}
4192 
4193 	if (bfp->l_len < 0) {
4194 		ZFS_EXIT(zfsvfs);
4195 		return (EINVAL);
4196 	}
4197 
4198 	off = bfp->l_start;
4199 	len = bfp->l_len; /* 0 means from off to end of file */
4200 
4201 	do {
4202 		error = zfs_freesp(zp, off, len, flag, TRUE);
4203 		/* NB: we already did dmu_tx_wait() if necessary */
4204 	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
4205 
4206 	ZFS_EXIT(zfsvfs);
4207 	return (error);
4208 }
4209 
4210 /*ARGSUSED*/
4211 static int
4212 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4213 {
4214 	znode_t		*zp = VTOZ(vp);
4215 	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
4216 	uint32_t	gen;
4217 	uint64_t	object = zp->z_id;
4218 	zfid_short_t	*zfid;
4219 	int		size, i;
4220 
4221 	ZFS_ENTER(zfsvfs);
4222 	ZFS_VERIFY_ZP(zp);
4223 	gen = (uint32_t)zp->z_gen;
4224 
4225 	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4226 	if (fidp->fid_len < size) {
4227 		fidp->fid_len = size;
4228 		ZFS_EXIT(zfsvfs);
4229 		return (ENOSPC);
4230 	}
4231 
4232 	zfid = (zfid_short_t *)fidp;
4233 
4234 	zfid->zf_len = size;
4235 
4236 	for (i = 0; i < sizeof (zfid->zf_object); i++)
4237 		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4238 
4239 	/* Must have a non-zero generation number to distinguish from .zfs */
4240 	if (gen == 0)
4241 		gen = 1;
4242 	for (i = 0; i < sizeof (zfid->zf_gen); i++)
4243 		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4244 
4245 	if (size == LONG_FID_LEN) {
4246 		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
4247 		zfid_long_t	*zlfid;
4248 
4249 		zlfid = (zfid_long_t *)fidp;
4250 
4251 		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4252 			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4253 
4254 		/* XXX - this should be the generation number for the objset */
4255 		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4256 			zlfid->zf_setgen[i] = 0;
4257 	}
4258 
4259 	ZFS_EXIT(zfsvfs);
4260 	return (0);
4261 }
4262 
4263 static int
4264 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4265     caller_context_t *ct)
4266 {
4267 	znode_t		*zp, *xzp;
4268 	zfsvfs_t	*zfsvfs;
4269 	zfs_dirlock_t	*dl;
4270 	int		error;
4271 
4272 	switch (cmd) {
4273 	case _PC_LINK_MAX:
4274 		*valp = ULONG_MAX;
4275 		return (0);
4276 
4277 	case _PC_FILESIZEBITS:
4278 		*valp = 64;
4279 		return (0);
4280 
4281 	case _PC_XATTR_EXISTS:
4282 		zp = VTOZ(vp);
4283 		zfsvfs = zp->z_zfsvfs;
4284 		ZFS_ENTER(zfsvfs);
4285 		ZFS_VERIFY_ZP(zp);
4286 		*valp = 0;
4287 		error = zfs_dirent_lock(&dl, zp, "", &xzp,
4288 		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4289 		if (error == 0) {
4290 			zfs_dirent_unlock(dl);
4291 			if (!zfs_dirempty(xzp))
4292 				*valp = 1;
4293 			VN_RELE(ZTOV(xzp));
4294 		} else if (error == ENOENT) {
4295 			/*
4296 			 * If there aren't extended attributes, it's the
4297 			 * same as having zero of them.
4298 			 */
4299 			error = 0;
4300 		}
4301 		ZFS_EXIT(zfsvfs);
4302 		return (error);
4303 
4304 	case _PC_SATTR_ENABLED:
4305 	case _PC_SATTR_EXISTS:
4306 		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) &&
4307 		    (vp->v_type == VREG || vp->v_type == VDIR);
4308 		return (0);
4309 
4310 	case _PC_ACL_ENABLED:
4311 		*valp = _ACL_ACE_ENABLED;
4312 		return (0);
4313 
4314 	case _PC_MIN_HOLE_SIZE:
4315 		*valp = (ulong_t)SPA_MINBLOCKSIZE;
4316 		return (0);
4317 
4318 	default:
4319 		return (fs_pathconf(vp, cmd, valp, cr, ct));
4320 	}
4321 }
4322 
4323 /*ARGSUSED*/
4324 static int
4325 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4326     caller_context_t *ct)
4327 {
4328 	znode_t *zp = VTOZ(vp);
4329 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4330 	int error;
4331 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4332 
4333 	ZFS_ENTER(zfsvfs);
4334 	ZFS_VERIFY_ZP(zp);
4335 	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4336 	ZFS_EXIT(zfsvfs);
4337 
4338 	return (error);
4339 }
4340 
4341 /*ARGSUSED*/
4342 static int
4343 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4344     caller_context_t *ct)
4345 {
4346 	znode_t *zp = VTOZ(vp);
4347 	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4348 	int error;
4349 	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4350 
4351 	ZFS_ENTER(zfsvfs);
4352 	ZFS_VERIFY_ZP(zp);
4353 	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4354 	ZFS_EXIT(zfsvfs);
4355 	return (error);
4356 }
4357 
4358 /*
4359  * Predeclare these here so that the compiler assumes that
4360  * this is an "old style" function declaration that does
4361  * not include arguments => we won't get type mismatch errors
4362  * in the initializations that follow.
4363  */
4364 static int zfs_inval();
4365 static int zfs_isdir();
4366 
4367 static int
4368 zfs_inval()
4369 {
4370 	return (EINVAL);
4371 }
4372 
4373 static int
4374 zfs_isdir()
4375 {
4376 	return (EISDIR);
4377 }
4378 /*
4379  * Directory vnode operations template
4380  */
4381 vnodeops_t *zfs_dvnodeops;
4382 const fs_operation_def_t zfs_dvnodeops_template[] = {
4383 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4384 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4385 	VOPNAME_READ,		{ .error = zfs_isdir },
4386 	VOPNAME_WRITE,		{ .error = zfs_isdir },
4387 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4388 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4389 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4390 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4391 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4392 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
4393 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
4394 	VOPNAME_LINK,		{ .vop_link = zfs_link },
4395 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4396 	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
4397 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
4398 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
4399 	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
4400 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4401 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4402 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4403 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4404 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4405 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4406 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4407 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
4408 	NULL,			NULL
4409 };
4410 
4411 /*
4412  * Regular file vnode operations template
4413  */
4414 vnodeops_t *zfs_fvnodeops;
4415 const fs_operation_def_t zfs_fvnodeops_template[] = {
4416 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4417 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4418 	VOPNAME_READ,		{ .vop_read = zfs_read },
4419 	VOPNAME_WRITE,		{ .vop_write = zfs_write },
4420 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4421 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4422 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4423 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4424 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4425 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4426 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4427 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4428 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4429 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4430 	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
4431 	VOPNAME_SPACE,		{ .vop_space = zfs_space },
4432 	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
4433 	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
4434 	VOPNAME_MAP,		{ .vop_map = zfs_map },
4435 	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
4436 	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
4437 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4438 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4439 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4440 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4441 	NULL,			NULL
4442 };
4443 
4444 /*
4445  * Symbolic link vnode operations template
4446  */
4447 vnodeops_t *zfs_symvnodeops;
4448 const fs_operation_def_t zfs_symvnodeops_template[] = {
4449 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4450 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4451 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4452 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4453 	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
4454 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4455 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4456 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4457 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4458 	NULL,			NULL
4459 };
4460 
4461 /*
4462  * Extended attribute directory vnode operations template
4463  *	This template is identical to the directory vnodes
4464  *	operation template except for restricted operations:
4465  *		VOP_MKDIR()
4466  *		VOP_SYMLINK()
4467  * Note that there are other restrictions embedded in:
4468  *	zfs_create()	- restrict type to VREG
4469  *	zfs_link()	- no links into/out of attribute space
4470  *	zfs_rename()	- no moves into/out of attribute space
4471  */
4472 vnodeops_t *zfs_xdvnodeops;
4473 const fs_operation_def_t zfs_xdvnodeops_template[] = {
4474 	VOPNAME_OPEN,		{ .vop_open = zfs_open },
4475 	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
4476 	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
4477 	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
4478 	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
4479 	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
4480 	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
4481 	VOPNAME_CREATE,		{ .vop_create = zfs_create },
4482 	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
4483 	VOPNAME_LINK,		{ .vop_link = zfs_link },
4484 	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
4485 	VOPNAME_MKDIR,		{ .error = zfs_inval },
4486 	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
4487 	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
4488 	VOPNAME_SYMLINK,	{ .error = zfs_inval },
4489 	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
4490 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4491 	VOPNAME_FID,		{ .vop_fid = zfs_fid },
4492 	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
4493 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4494 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
4495 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4496 	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
4497 	NULL,			NULL
4498 };
4499 
4500 /*
4501  * Error vnode operations template
4502  */
4503 vnodeops_t *zfs_evnodeops;
4504 const fs_operation_def_t zfs_evnodeops_template[] = {
4505 	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
4506 	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
4507 	NULL,			NULL
4508 };
4509