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