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